blob: 0905653dd3fc7eb6892a284b49f5b6feb34bc0bb [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;
Yan7d7d6062007-09-14 16:15:28 -040091 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040092 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040093 key.offset = 0;
94 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
95 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
96 if (ret < 0)
97 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -040098 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -050099 if (ret < 0)
100 return ret;
101 if (ret == 0) {
102 leaf = path->nodes[0];
103 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
104 if (key.objectid + key.offset > first_free)
105 first_free = key.objectid + key.offset;
106 }
Chris Masone37c9e62007-05-09 20:13:14 -0400107 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400108 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400109 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400110 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400111 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400112 if (ret < 0)
113 goto err;
Chris Masonde428b62007-05-18 13:28:27 -0400114 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -0400115 continue;
Chris Masonde428b62007-05-18 13:28:27 -0400116 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400117 break;
118 }
119 }
Chris Mason5f39d392007-10-15 16:14:19 -0400120 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -0400121 if (key.objectid < block_group->key.objectid) {
Yan7d7d6062007-09-14 16:15:28 -0400122 goto next;
123 }
Chris Masone37c9e62007-05-09 20:13:14 -0400124 if (key.objectid >= block_group->key.objectid +
125 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -0400126 break;
127 }
128
129 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
130 if (!found) {
131 last = first_free;
132 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400133 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400134 if (key.objectid > last) {
135 hole_size = key.objectid - last;
136 set_extent_dirty(free_space_cache, last,
137 last + hole_size - 1,
138 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400139 }
Yan7d7d6062007-09-14 16:15:28 -0400140 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400141 }
Yan7d7d6062007-09-14 16:15:28 -0400142next:
Chris Masone37c9e62007-05-09 20:13:14 -0400143 path->slots[0]++;
144 }
145
Yan7d7d6062007-09-14 16:15:28 -0400146 if (!found)
147 last = first_free;
148 if (block_group->key.objectid +
149 block_group->key.offset > last) {
150 hole_size = block_group->key.objectid +
151 block_group->key.offset - last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400152 set_extent_dirty(free_space_cache, last,
153 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400154 }
Chris Masone37c9e62007-05-09 20:13:14 -0400155 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400156err:
Chris Masone37c9e62007-05-09 20:13:14 -0400157 btrfs_free_path(path);
158 return 0;
159}
160
Chris Mason0ef3e662008-05-24 14:04:53 -0400161struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
162 btrfs_fs_info *info,
163 u64 bytenr)
164{
165 struct extent_io_tree *block_group_cache;
166 struct btrfs_block_group_cache *block_group = NULL;
167 u64 ptr;
168 u64 start;
169 u64 end;
170 int ret;
171
172 bytenr = max_t(u64, bytenr,
173 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
174 block_group_cache = &info->block_group_cache;
175 ret = find_first_extent_bit(block_group_cache,
176 bytenr, &start, &end,
177 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
178 BLOCK_GROUP_SYSTEM);
179 if (ret) {
180 return NULL;
181 }
182 ret = get_state_private(block_group_cache, start, &ptr);
183 if (ret)
184 return NULL;
185
186 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
187 return block_group;
188}
189
Chris Mason5276aed2007-06-11 21:33:38 -0400190struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
191 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400192 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400193{
Chris Masond1310b22008-01-24 16:13:08 -0500194 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -0400195 struct btrfs_block_group_cache *block_group = NULL;
196 u64 ptr;
197 u64 start;
198 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400199 int ret;
200
Chris Masona061fc82008-05-07 11:43:44 -0400201 bytenr = max_t(u64, bytenr,
202 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Mason96b51792007-10-15 16:15:19 -0400203 block_group_cache = &info->block_group_cache;
204 ret = find_first_extent_bit(block_group_cache,
Chris Masondb945352007-10-15 16:15:53 -0400205 bytenr, &start, &end,
Chris Mason0b86a832008-03-24 15:01:56 -0400206 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
207 BLOCK_GROUP_SYSTEM);
Chris Masonbe744172007-05-06 10:15:01 -0400208 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400209 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400210 }
Chris Mason96b51792007-10-15 16:15:19 -0400211 ret = get_state_private(block_group_cache, start, &ptr);
212 if (ret)
213 return NULL;
214
Jens Axboeae2f5412007-10-19 09:22:59 -0400215 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Yan5cf66422007-11-16 14:57:09 -0500216 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400217 block_group->key.objectid + block_group->key.offset)
218 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400219 return NULL;
220}
Chris Mason0b86a832008-03-24 15:01:56 -0400221
222static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
223{
Chris Mason593060d2008-03-25 16:50:33 -0400224 return (cache->flags & bits) == bits;
Chris Mason0b86a832008-03-24 15:01:56 -0400225}
226
227static int noinline find_search_start(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500228 struct btrfs_block_group_cache **cache_ret,
Chris Mason0ef3e662008-05-24 14:04:53 -0400229 u64 *start_ret, u64 num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400230{
Chris Masone37c9e62007-05-09 20:13:14 -0400231 int ret;
232 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masond7fc6402008-02-18 12:12:38 -0500233 struct extent_io_tree *free_space_cache;
Chris Mason7d1660d2008-03-24 15:02:03 -0400234 struct extent_state *state;
Chris Masone19caa52007-10-15 16:17:44 -0400235 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400236 u64 start = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500237 u64 cache_miss = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500238 u64 total_fs_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -0400239 u64 search_start = *start_ret;
Chris Masonf84a8b32007-11-06 10:26:29 -0500240 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400241
Chris Masonc31f8832008-01-08 15:46:31 -0500242 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masond7fc6402008-02-18 12:12:38 -0500243 free_space_cache = &root->fs_info->free_space_cache;
244
Chris Mason0ef3e662008-05-24 14:04:53 -0400245 if (!cache)
Chris Mason54aa1f42007-06-22 14:16:25 -0400246 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500247
Chris Mason0ef3e662008-05-24 14:04:53 -0400248again:
249 ret = cache_block_group(root, cache);
250 if (ret) {
251 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400252 }
Chris Masone19caa52007-10-15 16:17:44 -0400253
Chris Mason0ef3e662008-05-24 14:04:53 -0400254 last = max(search_start, cache->key.objectid);
255 if (!block_group_bits(cache, data) || cache->ro)
256 goto new_group;
257
Chris Mason7d1660d2008-03-24 15:02:03 -0400258 spin_lock_irq(&free_space_cache->lock);
259 state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
Chris Masone37c9e62007-05-09 20:13:14 -0400260 while(1) {
Chris Mason7d1660d2008-03-24 15:02:03 -0400261 if (!state) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500262 if (!cache_miss)
263 cache_miss = last;
Chris Mason7d1660d2008-03-24 15:02:03 -0400264 spin_unlock_irq(&free_space_cache->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400265 goto new_group;
266 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400267
Chris Mason7d1660d2008-03-24 15:02:03 -0400268 start = max(last, state->start);
269 last = state->end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500270 if (last - start < num) {
Chris Mason7d1660d2008-03-24 15:02:03 -0400271 do {
272 state = extent_state_next(state);
273 } while(state && !(state->state & EXTENT_DIRTY));
Chris Masonf510cfe2007-10-15 16:14:48 -0400274 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500275 }
Chris Mason7d1660d2008-03-24 15:02:03 -0400276 spin_unlock_irq(&free_space_cache->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -0400277 if (cache->ro) {
Chris Mason8f18cf12008-04-25 16:53:30 -0400278 goto new_group;
Chris Mason0ef3e662008-05-24 14:04:53 -0400279 }
Chris Mason0b86a832008-03-24 15:01:56 -0400280 if (start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400281 goto new_group;
Chris Mason8790d502008-04-03 16:29:03 -0400282 if (!block_group_bits(cache, data)) {
Chris Mason611f0e02008-04-03 16:29:03 -0400283 printk("block group bits don't match %Lu %d\n", cache->flags, data);
Chris Mason8790d502008-04-03 16:29:03 -0400284 }
Chris Mason0b86a832008-03-24 15:01:56 -0400285 *start_ret = start;
286 return 0;
Chris Mason8790d502008-04-03 16:29:03 -0400287 }
288out:
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500289 cache = btrfs_lookup_block_group(root->fs_info, search_start);
290 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -0400291 printk("Unable to find block group for %Lu\n", search_start);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500292 WARN_ON(1);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500293 }
Chris Mason0b86a832008-03-24 15:01:56 -0400294 return -ENOSPC;
Chris Masone37c9e62007-05-09 20:13:14 -0400295
296new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400297 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500298wrapped:
Chris Mason0ef3e662008-05-24 14:04:53 -0400299 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Chris Masonc31f8832008-01-08 15:46:31 -0500300 if (!cache || cache->key.objectid >= total_fs_bytes) {
Chris Mason0e4de582007-11-26 10:55:49 -0500301no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500302 if (!wrapped) {
303 wrapped = 1;
304 last = search_start;
Chris Masonf84a8b32007-11-06 10:26:29 -0500305 goto wrapped;
306 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500307 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400308 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500309 if (cache_miss && !cache->cached) {
310 cache_block_group(root, cache);
311 last = cache_miss;
Chris Mason0ef3e662008-05-24 14:04:53 -0400312 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Chris Mason257d0ce2007-11-07 21:08:16 -0500313 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400314 cache_miss = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400315 cache = __btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500316 if (!cache)
317 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400318 *cache_ret = cache;
319 goto again;
320}
321
Chris Mason84f54cf2007-06-12 07:43:08 -0400322static u64 div_factor(u64 num, int factor)
323{
Chris Mason257d0ce2007-11-07 21:08:16 -0500324 if (factor == 10)
325 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400326 num *= factor;
327 do_div(num, 10);
328 return num;
329}
330
Chris Mason6324fbf2008-03-24 15:01:59 -0400331static int block_group_state_bits(u64 flags)
332{
333 int bits = 0;
334 if (flags & BTRFS_BLOCK_GROUP_DATA)
335 bits |= BLOCK_GROUP_DATA;
336 if (flags & BTRFS_BLOCK_GROUP_METADATA)
337 bits |= BLOCK_GROUP_METADATA;
338 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
339 bits |= BLOCK_GROUP_SYSTEM;
340 return bits;
341}
342
Chris Mason925baed2008-06-25 16:01:30 -0400343static struct btrfs_block_group_cache *
344__btrfs_find_block_group(struct btrfs_root *root,
345 struct btrfs_block_group_cache *hint,
346 u64 search_start, int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400347{
Chris Mason96b51792007-10-15 16:15:19 -0400348 struct btrfs_block_group_cache *cache;
Chris Masond1310b22008-01-24 16:13:08 -0500349 struct extent_io_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400350 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400351 struct btrfs_fs_info *info = root->fs_info;
352 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400353 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400354 u64 start;
355 u64 end;
356 u64 free_check;
357 u64 ptr;
358 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400359 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400360 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400361 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400362 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400363
Chris Mason96b51792007-10-15 16:15:19 -0400364 block_group_cache = &info->block_group_cache;
365
Chris Masona236aed2008-04-29 09:38:00 -0400366 if (data & BTRFS_BLOCK_GROUP_METADATA)
367 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400368
Chris Mason6324fbf2008-03-24 15:01:59 -0400369 bit = block_group_state_bits(data);
Chris Masonbe744172007-05-06 10:15:01 -0400370
Chris Mason0ef3e662008-05-24 14:04:53 -0400371 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400372 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400373 shint = btrfs_lookup_first_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400374 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonbe744172007-05-06 10:15:01 -0400375 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500376 if (used + shint->pinned <
377 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400378 return shint;
379 }
380 }
381 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400382 if (hint && !hint->ro && block_group_bits(hint, data)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400383 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500384 if (used + hint->pinned <
385 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400386 return hint;
387 }
Chris Masone19caa52007-10-15 16:17:44 -0400388 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400389 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400390 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400391 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400392 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400393 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400394 }
Chris Mason31f3c992007-04-30 15:25:45 -0400395again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400396 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400397 ret = find_first_extent_bit(block_group_cache, last,
398 &start, &end, bit);
399 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400400 break;
Chris Mason96b51792007-10-15 16:15:19 -0400401
402 ret = get_state_private(block_group_cache, start, &ptr);
Chris Mason0ef3e662008-05-24 14:04:53 -0400403 if (ret) {
404 last = end + 1;
405 continue;
406 }
Chris Mason96b51792007-10-15 16:15:19 -0400407
Jens Axboeae2f5412007-10-19 09:22:59 -0400408 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400409 last = cache->key.objectid + cache->key.offset;
410 used = btrfs_block_group_used(&cache->item);
411
Chris Mason8f18cf12008-04-25 16:53:30 -0400412 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400413 free_check = div_factor(cache->key.offset, factor);
Chris Mason8790d502008-04-03 16:29:03 -0400414 if (used + cache->pinned < free_check) {
415 found_group = cache;
416 goto found;
417 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400418 }
Chris Masonde428b62007-05-18 13:28:27 -0400419 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400420 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400421 if (!wrapped) {
422 last = search_start;
423 wrapped = 1;
424 goto again;
425 }
426 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400427 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400428 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400429 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400430 goto again;
431 }
Chris Masonbe744172007-05-06 10:15:01 -0400432found:
Chris Mason31f3c992007-04-30 15:25:45 -0400433 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400434}
435
Chris Mason925baed2008-06-25 16:01:30 -0400436struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
437 struct btrfs_block_group_cache
438 *hint, u64 search_start,
439 int data, int owner)
440{
441
442 struct btrfs_block_group_cache *ret;
443 mutex_lock(&root->fs_info->alloc_mutex);
444 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
445 mutex_unlock(&root->fs_info->alloc_mutex);
446 return ret;
447}
Chris Mason7bb86312007-12-11 09:25:06 -0500448static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500449 u64 owner, u64 owner_offset)
450{
451 u32 high_crc = ~(u32)0;
452 u32 low_crc = ~(u32)0;
453 __le64 lenum;
Chris Mason74493f72007-12-11 09:25:06 -0500454 lenum = cpu_to_le64(root_objectid);
Miguela5eb62e2008-04-11 15:45:51 -0400455 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500456 lenum = cpu_to_le64(ref_generation);
Miguela5eb62e2008-04-11 15:45:51 -0400457 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500458 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
459 lenum = cpu_to_le64(owner);
Miguela5eb62e2008-04-11 15:45:51 -0400460 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500461 lenum = cpu_to_le64(owner_offset);
Miguela5eb62e2008-04-11 15:45:51 -0400462 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500463 }
Chris Mason74493f72007-12-11 09:25:06 -0500464 return ((u64)high_crc << 32) | (u64)low_crc;
465}
466
Chris Mason7bb86312007-12-11 09:25:06 -0500467static int match_extent_ref(struct extent_buffer *leaf,
468 struct btrfs_extent_ref *disk_ref,
469 struct btrfs_extent_ref *cpu_ref)
470{
471 int ret;
472 int len;
473
474 if (cpu_ref->objectid)
475 len = sizeof(*cpu_ref);
476 else
477 len = 2 * sizeof(u64);
478 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
479 len);
480 return ret == 0;
481}
482
Chris Mason98ed5172008-01-03 10:01:48 -0500483static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
484 struct btrfs_root *root,
485 struct btrfs_path *path, u64 bytenr,
486 u64 root_objectid,
487 u64 ref_generation, u64 owner,
488 u64 owner_offset, int del)
Chris Mason7bb86312007-12-11 09:25:06 -0500489{
490 u64 hash;
491 struct btrfs_key key;
492 struct btrfs_key found_key;
493 struct btrfs_extent_ref ref;
494 struct extent_buffer *leaf;
495 struct btrfs_extent_ref *disk_ref;
496 int ret;
497 int ret2;
498
499 btrfs_set_stack_ref_root(&ref, root_objectid);
500 btrfs_set_stack_ref_generation(&ref, ref_generation);
501 btrfs_set_stack_ref_objectid(&ref, owner);
502 btrfs_set_stack_ref_offset(&ref, owner_offset);
503
504 hash = hash_extent_ref(root_objectid, ref_generation, owner,
505 owner_offset);
506 key.offset = hash;
507 key.objectid = bytenr;
508 key.type = BTRFS_EXTENT_REF_KEY;
509
510 while (1) {
511 ret = btrfs_search_slot(trans, root, &key, path,
512 del ? -1 : 0, del);
513 if (ret < 0)
514 goto out;
515 leaf = path->nodes[0];
516 if (ret != 0) {
517 u32 nritems = btrfs_header_nritems(leaf);
518 if (path->slots[0] >= nritems) {
519 ret2 = btrfs_next_leaf(root, path);
520 if (ret2)
521 goto out;
522 leaf = path->nodes[0];
523 }
524 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
525 if (found_key.objectid != bytenr ||
526 found_key.type != BTRFS_EXTENT_REF_KEY)
527 goto out;
528 key.offset = found_key.offset;
529 if (del) {
530 btrfs_release_path(root, path);
531 continue;
532 }
533 }
534 disk_ref = btrfs_item_ptr(path->nodes[0],
535 path->slots[0],
536 struct btrfs_extent_ref);
537 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
538 ret = 0;
539 goto out;
540 }
541 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
542 key.offset = found_key.offset + 1;
543 btrfs_release_path(root, path);
544 }
545out:
546 return ret;
547}
548
Chris Masond8d5f3e2007-12-11 12:42:00 -0500549/*
550 * Back reference rules. Back refs have three main goals:
551 *
552 * 1) differentiate between all holders of references to an extent so that
553 * when a reference is dropped we can make sure it was a valid reference
554 * before freeing the extent.
555 *
556 * 2) Provide enough information to quickly find the holders of an extent
557 * if we notice a given block is corrupted or bad.
558 *
559 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
560 * maintenance. This is actually the same as #2, but with a slightly
561 * different use case.
562 *
563 * File extents can be referenced by:
564 *
565 * - multiple snapshots, subvolumes, or different generations in one subvol
566 * - different files inside a single subvolume (in theory, not implemented yet)
567 * - different offsets inside a file (bookend extents in file.c)
568 *
569 * The extent ref structure has fields for:
570 *
571 * - Objectid of the subvolume root
572 * - Generation number of the tree holding the reference
573 * - objectid of the file holding the reference
574 * - offset in the file corresponding to the key holding the reference
575 *
576 * When a file extent is allocated the fields are filled in:
577 * (root_key.objectid, trans->transid, inode objectid, offset in file)
578 *
579 * When a leaf is cow'd new references are added for every file extent found
580 * in the leaf. It looks the same as the create case, but trans->transid
581 * will be different when the block is cow'd.
582 *
583 * (root_key.objectid, trans->transid, inode objectid, offset in file)
584 *
585 * When a file extent is removed either during snapshot deletion or file
586 * truncation, the corresponding back reference is found
587 * by searching for:
588 *
589 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
590 * inode objectid, offset in file)
591 *
592 * Btree extents can be referenced by:
593 *
594 * - Different subvolumes
595 * - Different generations of the same subvolume
596 *
597 * Storing sufficient information for a full reverse mapping of a btree
598 * block would require storing the lowest key of the block in the backref,
599 * and it would require updating that lowest key either before write out or
600 * every time it changed. Instead, the objectid of the lowest key is stored
601 * along with the level of the tree block. This provides a hint
602 * about where in the btree the block can be found. Searches through the
603 * btree only need to look for a pointer to that block, so they stop one
604 * level higher than the level recorded in the backref.
605 *
606 * Some btrees do not do reference counting on their extents. These
607 * include the extent tree and the tree of tree roots. Backrefs for these
608 * trees always have a generation of zero.
609 *
610 * When a tree block is created, back references are inserted:
611 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500612 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500613 *
614 * When a tree block is cow'd in a reference counted root,
615 * new back references are added for all the blocks it points to.
616 * These are of the form (trans->transid will have increased since creation):
617 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500618 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500619 *
620 * Because the lowest_key_objectid and the level are just hints
621 * they are not used when backrefs are deleted. When a backref is deleted:
622 *
623 * if backref was for a tree root:
624 * root_objectid = root->root_key.objectid
625 * else
626 * root_objectid = btrfs_header_owner(parent)
627 *
628 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
629 *
630 * Back Reference Key hashing:
631 *
632 * Back references have four fields, each 64 bits long. Unfortunately,
633 * This is hashed into a single 64 bit number and placed into the key offset.
634 * The key objectid corresponds to the first byte in the extent, and the
635 * key type is set to BTRFS_EXTENT_REF_KEY
636 */
Chris Mason7bb86312007-12-11 09:25:06 -0500637int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
638 struct btrfs_root *root,
639 struct btrfs_path *path, u64 bytenr,
640 u64 root_objectid, u64 ref_generation,
641 u64 owner, u64 owner_offset)
Chris Mason74493f72007-12-11 09:25:06 -0500642{
643 u64 hash;
644 struct btrfs_key key;
645 struct btrfs_extent_ref ref;
Chris Mason7bb86312007-12-11 09:25:06 -0500646 struct btrfs_extent_ref *disk_ref;
Chris Mason74493f72007-12-11 09:25:06 -0500647 int ret;
648
649 btrfs_set_stack_ref_root(&ref, root_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500650 btrfs_set_stack_ref_generation(&ref, ref_generation);
Chris Mason74493f72007-12-11 09:25:06 -0500651 btrfs_set_stack_ref_objectid(&ref, owner);
652 btrfs_set_stack_ref_offset(&ref, owner_offset);
653
Chris Mason7bb86312007-12-11 09:25:06 -0500654 hash = hash_extent_ref(root_objectid, ref_generation, owner,
655 owner_offset);
Chris Mason74493f72007-12-11 09:25:06 -0500656 key.offset = hash;
657 key.objectid = bytenr;
658 key.type = BTRFS_EXTENT_REF_KEY;
659
660 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
661 while (ret == -EEXIST) {
Chris Mason7bb86312007-12-11 09:25:06 -0500662 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
663 struct btrfs_extent_ref);
664 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
665 goto out;
666 key.offset++;
667 btrfs_release_path(root, path);
668 ret = btrfs_insert_empty_item(trans, root, path, &key,
669 sizeof(ref));
Chris Mason74493f72007-12-11 09:25:06 -0500670 }
Chris Mason7bb86312007-12-11 09:25:06 -0500671 if (ret)
672 goto out;
673 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
674 struct btrfs_extent_ref);
675 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
676 sizeof(ref));
677 btrfs_mark_buffer_dirty(path->nodes[0]);
678out:
679 btrfs_release_path(root, path);
680 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500681}
682
Chris Mason925baed2008-06-25 16:01:30 -0400683static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Chris Masonb18c6682007-04-17 13:26:50 -0400684 struct btrfs_root *root,
Chris Mason74493f72007-12-11 09:25:06 -0500685 u64 bytenr, u64 num_bytes,
Chris Mason7bb86312007-12-11 09:25:06 -0500686 u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500687 u64 owner, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500688{
Chris Mason5caf2a02007-04-02 11:20:42 -0400689 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500690 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400691 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400692 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400693 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400694 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500695
Chris Masondb945352007-10-15 16:15:53 -0400696 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400697 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400698 if (!path)
699 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400700
Chris Mason3c12ac72008-04-21 12:01:38 -0400701 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400702 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400703 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400704 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400705 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400706 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400707 if (ret < 0)
708 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400709 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500710 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400711 }
Chris Mason02217ed2007-03-02 16:08:05 -0500712 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400713 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400714 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400715 refs = btrfs_extent_refs(l, item);
716 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400717 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500718
Chris Mason5caf2a02007-04-02 11:20:42 -0400719 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500720
Chris Mason3c12ac72008-04-21 12:01:38 -0400721 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -0500722 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
723 path, bytenr, root_objectid,
724 ref_generation, owner, owner_offset);
725 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400726 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400727 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500728
729 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500730 return 0;
731}
732
Chris Mason925baed2008-06-25 16:01:30 -0400733int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
734 struct btrfs_root *root,
735 u64 bytenr, u64 num_bytes,
736 u64 root_objectid, u64 ref_generation,
737 u64 owner, u64 owner_offset)
738{
739 int ret;
740
741 mutex_lock(&root->fs_info->alloc_mutex);
742 ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes,
743 root_objectid, ref_generation,
744 owner, owner_offset);
745 mutex_unlock(&root->fs_info->alloc_mutex);
746 return ret;
747}
748
Chris Masone9d0b132007-08-10 14:06:19 -0400749int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
750 struct btrfs_root *root)
751{
752 finish_current_insert(trans, root->fs_info->extent_root);
753 del_pending_extents(trans, root->fs_info->extent_root);
754 return 0;
755}
756
Chris Masonb18c6682007-04-17 13:26:50 -0400757static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400758 struct btrfs_root *root, u64 bytenr,
759 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500760{
Chris Mason5caf2a02007-04-02 11:20:42 -0400761 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500762 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400763 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400764 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400765 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400766
Chris Masondb945352007-10-15 16:15:53 -0400767 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400768 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400769 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400770 key.objectid = bytenr;
771 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400772 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400773 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400774 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400775 if (ret < 0)
776 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400777 if (ret != 0) {
778 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400779 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500780 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400781 }
782 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400783 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400784 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400785out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400786 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500787 return 0;
788}
789
Chris Masonbe20aa92007-12-17 20:14:01 -0500790u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
791 struct btrfs_path *count_path,
Chris Masona68d5932008-05-08 14:11:56 -0400792 u64 expected_owner,
Chris Masonbe20aa92007-12-17 20:14:01 -0500793 u64 first_extent)
794{
795 struct btrfs_root *extent_root = root->fs_info->extent_root;
796 struct btrfs_path *path;
797 u64 bytenr;
798 u64 found_objectid;
Chris Masona68d5932008-05-08 14:11:56 -0400799 u64 found_owner;
Chris Mason56b453c2008-01-03 09:08:27 -0500800 u64 root_objectid = root->root_key.objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500801 u32 total_count = 0;
Chris Masonbbaf5492008-05-08 16:31:21 -0400802 u32 extent_refs;
Chris Masonbe20aa92007-12-17 20:14:01 -0500803 u32 cur_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500804 u32 nritems;
805 int ret;
806 struct btrfs_key key;
807 struct btrfs_key found_key;
808 struct extent_buffer *l;
809 struct btrfs_extent_item *item;
810 struct btrfs_extent_ref *ref_item;
811 int level = -1;
812
Chris Mason925baed2008-06-25 16:01:30 -0400813 /* FIXME, needs locking */
814 BUG();
815
816 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonbe20aa92007-12-17 20:14:01 -0500817 path = btrfs_alloc_path();
818again:
819 if (level == -1)
820 bytenr = first_extent;
821 else
822 bytenr = count_path->nodes[level]->start;
823
824 cur_count = 0;
825 key.objectid = bytenr;
826 key.offset = 0;
827
828 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
829 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
830 if (ret < 0)
831 goto out;
832 BUG_ON(ret == 0);
833
834 l = path->nodes[0];
835 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
836
837 if (found_key.objectid != bytenr ||
838 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
839 goto out;
840 }
841
842 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masonbbaf5492008-05-08 16:31:21 -0400843 extent_refs = btrfs_extent_refs(l, item);
Chris Masonbe20aa92007-12-17 20:14:01 -0500844 while (1) {
Chris Masonbd098352008-01-03 13:23:19 -0500845 l = path->nodes[0];
Chris Masonbe20aa92007-12-17 20:14:01 -0500846 nritems = btrfs_header_nritems(l);
847 if (path->slots[0] >= nritems) {
848 ret = btrfs_next_leaf(extent_root, path);
849 if (ret == 0)
850 continue;
851 break;
852 }
853 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
854 if (found_key.objectid != bytenr)
855 break;
Chris Masonbd098352008-01-03 13:23:19 -0500856
Chris Masonbe20aa92007-12-17 20:14:01 -0500857 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
858 path->slots[0]++;
859 continue;
860 }
861
862 cur_count++;
863 ref_item = btrfs_item_ptr(l, path->slots[0],
864 struct btrfs_extent_ref);
865 found_objectid = btrfs_ref_root(l, ref_item);
866
Chris Mason56b453c2008-01-03 09:08:27 -0500867 if (found_objectid != root_objectid) {
868 total_count = 2;
Chris Mason4313b392008-01-03 09:08:48 -0500869 goto out;
Chris Mason56b453c2008-01-03 09:08:27 -0500870 }
Chris Masona68d5932008-05-08 14:11:56 -0400871 if (level == -1) {
872 found_owner = btrfs_ref_objectid(l, ref_item);
873 if (found_owner != expected_owner) {
874 total_count = 2;
875 goto out;
876 }
Chris Masonbbaf5492008-05-08 16:31:21 -0400877 /*
878 * nasty. we don't count a reference held by
879 * the running transaction. This allows nodatacow
880 * to avoid cow most of the time
881 */
882 if (found_owner >= BTRFS_FIRST_FREE_OBJECTID &&
883 btrfs_ref_generation(l, ref_item) ==
884 root->fs_info->generation) {
885 extent_refs--;
886 }
Chris Masona68d5932008-05-08 14:11:56 -0400887 }
Chris Mason56b453c2008-01-03 09:08:27 -0500888 total_count = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500889 path->slots[0]++;
890 }
Chris Masonbbaf5492008-05-08 16:31:21 -0400891 /*
892 * if there is more than one reference against a data extent,
893 * we have to assume the other ref is another snapshot
894 */
895 if (level == -1 && extent_refs > 1) {
896 total_count = 2;
897 goto out;
898 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500899 if (cur_count == 0) {
900 total_count = 0;
901 goto out;
902 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500903 if (level >= 0 && root->node == count_path->nodes[level])
904 goto out;
905 level++;
906 btrfs_release_path(root, path);
907 goto again;
908
909out:
910 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -0400911 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonbe20aa92007-12-17 20:14:01 -0500912 return total_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500913}
Chris Masonc5739bb2007-04-10 09:27:04 -0400914
Chris Masone089f052007-03-16 16:20:31 -0400915int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400916 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500917{
Chris Masondb945352007-10-15 16:15:53 -0400918 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400919 u32 nritems;
920 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400921 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500922 int i;
Chris Masondb945352007-10-15 16:15:53 -0400923 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400924 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400925 int faili;
Chris Masona28ec192007-03-06 20:08:01 -0500926
Chris Mason3768f362007-03-13 16:47:54 -0400927 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500928 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400929
Chris Mason925baed2008-06-25 16:01:30 -0400930 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400931 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400932 nritems = btrfs_header_nritems(buf);
933 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400934 if (level == 0) {
935 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400936 btrfs_item_key_to_cpu(buf, &key, i);
937 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400938 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400939 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400940 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400941 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454df2007-04-19 13:37:44 -0400942 BTRFS_FILE_EXTENT_INLINE)
943 continue;
Chris Masondb945352007-10-15 16:15:53 -0400944 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
945 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400946 continue;
Chris Mason925baed2008-06-25 16:01:30 -0400947 ret = __btrfs_inc_extent_ref(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500948 btrfs_file_extent_disk_num_bytes(buf, fi),
949 root->root_key.objectid, trans->transid,
950 key.objectid, key.offset);
Chris Mason54aa1f42007-06-22 14:16:25 -0400951 if (ret) {
952 faili = i;
953 goto fail;
954 }
Chris Mason6407bf62007-03-27 06:33:00 -0400955 } else {
Chris Masondb945352007-10-15 16:15:53 -0400956 bytenr = btrfs_node_blockptr(buf, i);
Chris Mason6caab482007-12-13 09:48:07 -0500957 btrfs_node_key_to_cpu(buf, &key, i);
Chris Mason925baed2008-06-25 16:01:30 -0400958 ret = __btrfs_inc_extent_ref(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500959 btrfs_level_size(root, level - 1),
960 root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500961 trans->transid,
962 level - 1, key.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400963 if (ret) {
964 faili = i;
965 goto fail;
966 }
Chris Mason6407bf62007-03-27 06:33:00 -0400967 }
Chris Mason02217ed2007-03-02 16:08:05 -0500968 }
Chris Mason925baed2008-06-25 16:01:30 -0400969 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason02217ed2007-03-02 16:08:05 -0500970 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400971fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400972 WARN_ON(1);
Chris Mason7bb86312007-12-11 09:25:06 -0500973#if 0
Chris Mason54aa1f42007-06-22 14:16:25 -0400974 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400975 if (level == 0) {
976 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400977 btrfs_item_key_to_cpu(buf, &key, i);
978 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400979 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400980 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400981 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400982 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400983 BTRFS_FILE_EXTENT_INLINE)
984 continue;
Chris Masondb945352007-10-15 16:15:53 -0400985 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
986 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400987 continue;
Chris Masondb945352007-10-15 16:15:53 -0400988 err = btrfs_free_extent(trans, root, disk_bytenr,
989 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400990 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400991 BUG_ON(err);
992 } else {
Chris Masondb945352007-10-15 16:15:53 -0400993 bytenr = btrfs_node_blockptr(buf, i);
994 err = btrfs_free_extent(trans, root, bytenr,
995 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400996 BUG_ON(err);
997 }
998 }
Chris Mason7bb86312007-12-11 09:25:06 -0500999#endif
Chris Mason925baed2008-06-25 16:01:30 -04001000 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason54aa1f42007-06-22 14:16:25 -04001001 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001002}
1003
Chris Mason9078a3e2007-04-26 16:46:15 -04001004static int write_one_cache_group(struct btrfs_trans_handle *trans,
1005 struct btrfs_root *root,
1006 struct btrfs_path *path,
1007 struct btrfs_block_group_cache *cache)
1008{
1009 int ret;
1010 int pending_ret;
1011 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001012 unsigned long bi;
1013 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001014
Chris Mason9078a3e2007-04-26 16:46:15 -04001015 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001016 if (ret < 0)
1017 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001018 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001019
1020 leaf = path->nodes[0];
1021 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1022 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1023 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001024 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001025fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001026 finish_current_insert(trans, extent_root);
1027 pending_ret = del_pending_extents(trans, extent_root);
1028 if (ret)
1029 return ret;
1030 if (pending_ret)
1031 return pending_ret;
1032 return 0;
1033
1034}
1035
Chris Mason96b51792007-10-15 16:15:19 -04001036int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1037 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001038{
Chris Masond1310b22008-01-24 16:13:08 -05001039 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -04001040 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001041 int ret;
1042 int err = 0;
1043 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001044 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001045 u64 last = 0;
1046 u64 start;
1047 u64 end;
1048 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -04001049
Chris Mason96b51792007-10-15 16:15:19 -04001050 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001051 path = btrfs_alloc_path();
1052 if (!path)
1053 return -ENOMEM;
1054
Chris Mason925baed2008-06-25 16:01:30 -04001055 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001056 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -04001057 ret = find_first_extent_bit(block_group_cache, last,
1058 &start, &end, BLOCK_GROUP_DIRTY);
1059 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -04001060 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001061
Chris Mason96b51792007-10-15 16:15:19 -04001062 last = end + 1;
1063 ret = get_state_private(block_group_cache, start, &ptr);
1064 if (ret)
1065 break;
Jens Axboeae2f5412007-10-19 09:22:59 -04001066 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -04001067 err = write_one_cache_group(trans, root,
1068 path, cache);
1069 /*
1070 * if we fail to write the cache group, we want
1071 * to keep it marked dirty in hopes that a later
1072 * write will work
1073 */
1074 if (err) {
1075 werr = err;
1076 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001077 }
Chris Mason96b51792007-10-15 16:15:19 -04001078 clear_extent_bits(block_group_cache, start, end,
1079 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001080 }
1081 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04001082 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001083 return werr;
1084}
1085
Chris Mason6324fbf2008-03-24 15:01:59 -04001086static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1087 u64 flags)
1088{
1089 struct list_head *head = &info->space_info;
1090 struct list_head *cur;
1091 struct btrfs_space_info *found;
1092 list_for_each(cur, head) {
1093 found = list_entry(cur, struct btrfs_space_info, list);
1094 if (found->flags == flags)
1095 return found;
1096 }
1097 return NULL;
1098
1099}
1100
Chris Mason593060d2008-03-25 16:50:33 -04001101static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1102 u64 total_bytes, u64 bytes_used,
1103 struct btrfs_space_info **space_info)
1104{
1105 struct btrfs_space_info *found;
1106
1107 found = __find_space_info(info, flags);
1108 if (found) {
1109 found->total_bytes += total_bytes;
1110 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001111 found->full = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001112 WARN_ON(found->total_bytes < found->bytes_used);
1113 *space_info = found;
1114 return 0;
1115 }
1116 found = kmalloc(sizeof(*found), GFP_NOFS);
1117 if (!found)
1118 return -ENOMEM;
1119
1120 list_add(&found->list, &info->space_info);
1121 found->flags = flags;
1122 found->total_bytes = total_bytes;
1123 found->bytes_used = bytes_used;
1124 found->bytes_pinned = 0;
1125 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001126 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001127 *space_info = found;
1128 return 0;
1129}
1130
Chris Mason8790d502008-04-03 16:29:03 -04001131static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1132{
1133 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001134 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001135 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001136 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001137 if (extra_flags) {
1138 if (flags & BTRFS_BLOCK_GROUP_DATA)
1139 fs_info->avail_data_alloc_bits |= extra_flags;
1140 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1141 fs_info->avail_metadata_alloc_bits |= extra_flags;
1142 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1143 fs_info->avail_system_alloc_bits |= extra_flags;
1144 }
1145}
Chris Mason593060d2008-03-25 16:50:33 -04001146
Chris Masona061fc82008-05-07 11:43:44 -04001147static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001148{
Chris Masona061fc82008-05-07 11:43:44 -04001149 u64 num_devices = root->fs_info->fs_devices->num_devices;
1150
1151 if (num_devices == 1)
1152 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1153 if (num_devices < 4)
1154 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1155
Chris Masonec44a352008-04-28 15:29:52 -04001156 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1157 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001158 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001159 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001160 }
Chris Masonec44a352008-04-28 15:29:52 -04001161
1162 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001163 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001164 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001165 }
Chris Masonec44a352008-04-28 15:29:52 -04001166
1167 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1168 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1169 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1170 (flags & BTRFS_BLOCK_GROUP_DUP)))
1171 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1172 return flags;
1173}
1174
Chris Mason6324fbf2008-03-24 15:01:59 -04001175static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1176 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001177 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001178{
1179 struct btrfs_space_info *space_info;
1180 u64 thresh;
1181 u64 start;
1182 u64 num_bytes;
1183 int ret;
1184
Chris Masona061fc82008-05-07 11:43:44 -04001185 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001186
Chris Mason6324fbf2008-03-24 15:01:59 -04001187 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001188 if (!space_info) {
1189 ret = update_space_info(extent_root->fs_info, flags,
1190 0, 0, &space_info);
1191 BUG_ON(ret);
1192 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001193 BUG_ON(!space_info);
1194
Chris Mason0ef3e662008-05-24 14:04:53 -04001195 if (space_info->force_alloc) {
1196 force = 1;
1197 space_info->force_alloc = 0;
1198 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001199 if (space_info->full)
Chris Mason925baed2008-06-25 16:01:30 -04001200 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001201
Chris Mason8790d502008-04-03 16:29:03 -04001202 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001203 if (!force &&
1204 (space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
Chris Mason6324fbf2008-03-24 15:01:59 -04001205 thresh)
Chris Mason925baed2008-06-25 16:01:30 -04001206 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001207
Chris Mason925baed2008-06-25 16:01:30 -04001208 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001209 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1210 if (ret == -ENOSPC) {
1211printk("space info full %Lu\n", flags);
1212 space_info->full = 1;
Chris Mason925baed2008-06-25 16:01:30 -04001213 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001214 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001215 BUG_ON(ret);
1216
1217 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001218 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001219 BUG_ON(ret);
Chris Mason925baed2008-06-25 16:01:30 -04001220 mutex_unlock(&extent_root->fs_info->chunk_mutex);
1221out:
Chris Mason6324fbf2008-03-24 15:01:59 -04001222 return 0;
1223}
1224
Chris Mason9078a3e2007-04-26 16:46:15 -04001225static int update_block_group(struct btrfs_trans_handle *trans,
1226 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001227 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001228 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001229{
1230 struct btrfs_block_group_cache *cache;
1231 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001232 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001233 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001234 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -04001235 u64 start;
1236 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -04001237
Chris Mason9078a3e2007-04-26 16:46:15 -04001238 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001239 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001240 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001241 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001242 }
Chris Masondb945352007-10-15 16:15:53 -04001243 byte_in_group = bytenr - cache->key.objectid;
1244 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -04001245 start = cache->key.objectid;
1246 end = start + cache->key.offset - 1;
1247 set_extent_bits(&info->block_group_cache, start, end,
1248 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001249
1250 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001251 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001252 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001253 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001254 cache->space_info->bytes_used += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -04001255 } else {
Chris Masondb945352007-10-15 16:15:53 -04001256 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001257 cache->space_info->bytes_used -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -04001258 if (mark_free) {
1259 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -04001260 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -04001261 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001262 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001263 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001264 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -04001265 total -= num_bytes;
1266 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001267 }
1268 return 0;
1269}
Chris Mason6324fbf2008-03-24 15:01:59 -04001270
Chris Masona061fc82008-05-07 11:43:44 -04001271static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1272{
1273 u64 start;
1274 u64 end;
1275 int ret;
1276 ret = find_first_extent_bit(&root->fs_info->block_group_cache,
1277 search_start, &start, &end,
1278 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
1279 BLOCK_GROUP_SYSTEM);
1280 if (ret)
1281 return 0;
1282 return start;
1283}
1284
1285
Yan324ae4d2007-11-16 14:57:08 -05001286static int update_pinned_extents(struct btrfs_root *root,
1287 u64 bytenr, u64 num, int pin)
1288{
1289 u64 len;
1290 struct btrfs_block_group_cache *cache;
1291 struct btrfs_fs_info *fs_info = root->fs_info;
1292
1293 if (pin) {
1294 set_extent_dirty(&fs_info->pinned_extents,
1295 bytenr, bytenr + num - 1, GFP_NOFS);
1296 } else {
1297 clear_extent_dirty(&fs_info->pinned_extents,
1298 bytenr, bytenr + num - 1, GFP_NOFS);
1299 }
1300 while (num > 0) {
1301 cache = btrfs_lookup_block_group(fs_info, bytenr);
Chris Masona061fc82008-05-07 11:43:44 -04001302 if (!cache) {
1303 u64 first = first_logical_byte(root, bytenr);
1304 WARN_ON(first < bytenr);
1305 len = min(first - bytenr, num);
1306 } else {
1307 len = min(num, cache->key.offset -
1308 (bytenr - cache->key.objectid));
1309 }
Yan324ae4d2007-11-16 14:57:08 -05001310 if (pin) {
Chris Masona061fc82008-05-07 11:43:44 -04001311 if (cache) {
1312 cache->pinned += len;
1313 cache->space_info->bytes_pinned += len;
1314 }
Yan324ae4d2007-11-16 14:57:08 -05001315 fs_info->total_pinned += len;
1316 } else {
Chris Masona061fc82008-05-07 11:43:44 -04001317 if (cache) {
1318 cache->pinned -= len;
1319 cache->space_info->bytes_pinned -= len;
1320 }
Yan324ae4d2007-11-16 14:57:08 -05001321 fs_info->total_pinned -= len;
1322 }
1323 bytenr += len;
1324 num -= len;
1325 }
1326 return 0;
1327}
Chris Mason9078a3e2007-04-26 16:46:15 -04001328
Chris Masond1310b22008-01-24 16:13:08 -05001329int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001330{
Chris Masonccd467d2007-06-28 15:57:36 -04001331 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001332 u64 start;
1333 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001334 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001335 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001336
1337 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001338 ret = find_first_extent_bit(pinned_extents, last,
1339 &start, &end, EXTENT_DIRTY);
1340 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001341 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001342 set_extent_dirty(copy, start, end, GFP_NOFS);
1343 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001344 }
1345 return 0;
1346}
1347
1348int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1349 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001350 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001351{
Chris Mason1a5bc162007-10-15 16:15:26 -04001352 u64 start;
1353 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001354 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001355 struct extent_io_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -04001356 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -05001357
Chris Mason925baed2008-06-25 16:01:30 -04001358 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001359 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001360 ret = find_first_extent_bit(unpin, 0, &start, &end,
1361 EXTENT_DIRTY);
1362 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001363 break;
Yan324ae4d2007-11-16 14:57:08 -05001364 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001365 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1366 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -05001367 }
Chris Mason925baed2008-06-25 16:01:30 -04001368 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001369 return 0;
1370}
1371
Chris Mason98ed5172008-01-03 10:01:48 -05001372static int finish_current_insert(struct btrfs_trans_handle *trans,
1373 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001374{
Chris Mason7bb86312007-12-11 09:25:06 -05001375 u64 start;
1376 u64 end;
1377 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001378 struct extent_buffer *eb;
Chris Mason7bb86312007-12-11 09:25:06 -05001379 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001380 struct btrfs_key ins;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001381 struct btrfs_disk_key first;
Chris Mason234b63a2007-03-13 10:46:10 -04001382 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001383 int ret;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001384 int level;
Chris Mason1a5bc162007-10-15 16:15:26 -04001385 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001386
Chris Mason5f39d392007-10-15 16:14:19 -04001387 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -04001388 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason7bb86312007-12-11 09:25:06 -05001389 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001390
Chris Mason26b80032007-08-08 20:17:12 -04001391 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001392 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1393 &end, EXTENT_LOCKED);
1394 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001395 break;
1396
Chris Mason1a5bc162007-10-15 16:15:26 -04001397 ins.objectid = start;
1398 ins.offset = end + 1 - start;
1399 err = btrfs_insert_item(trans, extent_root, &ins,
1400 &extent_item, sizeof(extent_item));
1401 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1402 GFP_NOFS);
Chris Masonca7a79a2008-05-12 12:59:19 -04001403 eb = read_tree_block(extent_root, ins.objectid, ins.offset,
1404 trans->transid);
Chris Mason925baed2008-06-25 16:01:30 -04001405 btrfs_tree_lock(eb);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001406 level = btrfs_header_level(eb);
1407 if (level == 0) {
1408 btrfs_item_key(eb, &first, 0);
1409 } else {
1410 btrfs_node_key(eb, &first, 0);
1411 }
Chris Mason925baed2008-06-25 16:01:30 -04001412 btrfs_tree_unlock(eb);
1413 free_extent_buffer(eb);
1414 /*
1415 * the first key is just a hint, so the race we've created
1416 * against reading it is fine
1417 */
Chris Mason7bb86312007-12-11 09:25:06 -05001418 err = btrfs_insert_extent_backref(trans, extent_root, path,
1419 start, extent_root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -05001420 0, level,
1421 btrfs_disk_key_objectid(&first));
Chris Mason7bb86312007-12-11 09:25:06 -05001422 BUG_ON(err);
Chris Mason037e6392007-03-07 11:50:24 -05001423 }
Chris Mason7bb86312007-12-11 09:25:06 -05001424 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001425 return 0;
1426}
1427
Chris Masondb945352007-10-15 16:15:53 -04001428static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1429 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -04001430{
Chris Mason1a5bc162007-10-15 16:15:26 -04001431 int err = 0;
Chris Mason78fae272007-03-25 11:35:08 -04001432
Chris Masonf4b9aa82007-03-27 11:05:53 -04001433 if (!pending) {
Chris Mason925baed2008-06-25 16:01:30 -04001434#if 0
1435 struct extent_buffer *buf;
Chris Masondb945352007-10-15 16:15:53 -04001436 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -04001437 if (buf) {
Chris Mason925baed2008-06-25 16:01:30 -04001438 if (!btrfs_try_tree_lock(buf) &&
1439 btrfs_buffer_uptodate(buf, 0)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001440 u64 transid =
1441 root->fs_info->running_transaction->transid;
Chris Masondc17ff82008-01-08 15:46:30 -05001442 u64 header_transid =
1443 btrfs_header_generation(buf);
Chris Mason6bc34672008-04-04 15:40:00 -04001444 if (header_transid == transid &&
1445 !btrfs_header_flag(buf,
1446 BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason55c69072008-01-09 15:55:33 -05001447 clean_tree_block(NULL, root, buf);
Chris Mason925baed2008-06-25 16:01:30 -04001448 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001449 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -05001450 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001451 }
Chris Mason925baed2008-06-25 16:01:30 -04001452 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001453 }
Chris Mason5f39d392007-10-15 16:14:19 -04001454 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -04001455 }
Chris Mason925baed2008-06-25 16:01:30 -04001456#endif
Yan324ae4d2007-11-16 14:57:08 -05001457 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001458 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -04001459 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -04001460 bytenr, bytenr + num_bytes - 1,
1461 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001462 }
Chris Masonbe744172007-05-06 10:15:01 -04001463 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001464 return 0;
1465}
1466
Chris Masona28ec192007-03-06 20:08:01 -05001467/*
1468 * remove an extent from the root, returns 0 on success
1469 */
Chris Masone089f052007-03-16 16:20:31 -04001470static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001471 *root, u64 bytenr, u64 num_bytes,
1472 u64 root_objectid, u64 ref_generation,
1473 u64 owner_objectid, u64 owner_offset, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -04001474 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001475{
Chris Mason5caf2a02007-04-02 11:20:42 -04001476 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001477 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001478 struct btrfs_fs_info *info = root->fs_info;
1479 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001480 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001481 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001482 int extent_slot = 0;
1483 int found_extent = 0;
1484 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001485 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001486 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001487
Chris Masondb945352007-10-15 16:15:53 -04001488 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001489 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001490 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001491 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001492 if (!path)
1493 return -ENOMEM;
1494
Chris Mason3c12ac72008-04-21 12:01:38 -04001495 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05001496 ret = lookup_extent_backref(trans, extent_root, path,
1497 bytenr, root_objectid,
1498 ref_generation,
1499 owner_objectid, owner_offset, 1);
1500 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001501 struct btrfs_key found_key;
1502 extent_slot = path->slots[0];
1503 while(extent_slot > 0) {
1504 extent_slot--;
1505 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1506 extent_slot);
1507 if (found_key.objectid != bytenr)
1508 break;
1509 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1510 found_key.offset == num_bytes) {
1511 found_extent = 1;
1512 break;
1513 }
1514 if (path->slots[0] - extent_slot > 5)
1515 break;
1516 }
1517 if (!found_extent)
1518 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001519 } else {
1520 btrfs_print_leaf(extent_root, path->nodes[0]);
1521 WARN_ON(1);
1522 printk("Unable to find ref byte nr %Lu root %Lu "
1523 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1524 root_objectid, ref_generation, owner_objectid,
1525 owner_offset);
1526 }
Chris Mason952fcca2008-02-18 16:33:44 -05001527 if (!found_extent) {
1528 btrfs_release_path(extent_root, path);
1529 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1530 if (ret < 0)
1531 return ret;
1532 BUG_ON(ret);
1533 extent_slot = path->slots[0];
1534 }
Chris Mason5f39d392007-10-15 16:14:19 -04001535
1536 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001537 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001538 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001539 refs = btrfs_extent_refs(leaf, ei);
1540 BUG_ON(refs == 0);
1541 refs -= 1;
1542 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001543
Chris Mason5f39d392007-10-15 16:14:19 -04001544 btrfs_mark_buffer_dirty(leaf);
1545
Chris Mason952fcca2008-02-18 16:33:44 -05001546 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1547 /* if the back ref and the extent are next to each other
1548 * they get deleted below in one shot
1549 */
1550 path->slots[0] = extent_slot;
1551 num_to_del = 2;
1552 } else if (found_extent) {
1553 /* otherwise delete the extent back ref */
1554 ret = btrfs_del_item(trans, extent_root, path);
1555 BUG_ON(ret);
1556 /* if refs are 0, we need to setup the path for deletion */
1557 if (refs == 0) {
1558 btrfs_release_path(extent_root, path);
1559 ret = btrfs_search_slot(trans, extent_root, &key, path,
1560 -1, 1);
1561 if (ret < 0)
1562 return ret;
1563 BUG_ON(ret);
1564 }
1565 }
1566
Chris Masoncf27e1e2007-03-13 09:49:06 -04001567 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001568 u64 super_used;
1569 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04001570
1571 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -04001572 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -05001573 if (ret > 0)
1574 mark_free = 1;
1575 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001576 }
1577
Josef Bacik58176a92007-08-29 15:47:34 -04001578 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04001579 spin_lock_irq(&info->delalloc_lock);
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);
Chris Masona2135012008-06-25 16:01:30 -04001583 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04001584
1585 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001586 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001587 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001588 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001589 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1590 num_to_del);
Chris Mason54aa1f42007-06-22 14:16:25 -04001591 if (ret) {
1592 return ret;
1593 }
Chris Masondb945352007-10-15 16:15:53 -04001594 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001595 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001596 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05001597 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001598 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001599 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05001600 return ret;
1601}
1602
1603/*
Chris Masonfec577f2007-02-26 10:40:21 -05001604 * find all the blocks marked as pending in the radix tree and remove
1605 * them from the extent map
1606 */
Chris Masone089f052007-03-16 16:20:31 -04001607static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1608 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05001609{
1610 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001611 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001612 u64 start;
1613 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001614 struct extent_io_tree *pending_del;
1615 struct extent_io_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -04001616
Chris Mason1a5bc162007-10-15 16:15:26 -04001617 pending_del = &extent_root->fs_info->pending_del;
1618 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -05001619
1620 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001621 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1622 EXTENT_LOCKED);
1623 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05001624 break;
Yan324ae4d2007-11-16 14:57:08 -05001625 update_pinned_extents(extent_root, start, end + 1 - start, 1);
Chris Mason1a5bc162007-10-15 16:15:26 -04001626 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1627 GFP_NOFS);
1628 ret = __free_extent(trans, extent_root,
Chris Mason7bb86312007-12-11 09:25:06 -05001629 start, end + 1 - start,
1630 extent_root->root_key.objectid,
1631 0, 0, 0, 0, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001632 if (ret)
1633 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001634 }
Chris Masone20d96d2007-03-22 12:13:20 -04001635 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05001636}
1637
1638/*
1639 * remove an extent from the root, returns 0 on success
1640 */
Chris Mason925baed2008-06-25 16:01:30 -04001641static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
1642 struct btrfs_root *root, u64 bytenr,
1643 u64 num_bytes, u64 root_objectid,
1644 u64 ref_generation, u64 owner_objectid,
1645 u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05001646{
Chris Mason9f5fae22007-03-20 14:38:32 -04001647 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05001648 int pending_ret;
1649 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05001650
Chris Masondb945352007-10-15 16:15:53 -04001651 WARN_ON(num_bytes < root->sectorsize);
Chris Mason7bb86312007-12-11 09:25:06 -05001652 if (!root->ref_cows)
1653 ref_generation = 0;
1654
Chris Masona28ec192007-03-06 20:08:01 -05001655 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -04001656 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -05001657 return 0;
1658 }
Chris Mason7bb86312007-12-11 09:25:06 -05001659 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1660 ref_generation, owner_objectid, owner_offset,
1661 pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001662 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05001663 return ret ? ret : pending_ret;
1664}
1665
Chris Mason925baed2008-06-25 16:01:30 -04001666int btrfs_free_extent(struct btrfs_trans_handle *trans,
1667 struct btrfs_root *root, u64 bytenr,
1668 u64 num_bytes, u64 root_objectid,
1669 u64 ref_generation, u64 owner_objectid,
1670 u64 owner_offset, int pin)
1671{
1672 int ret;
1673
1674 maybe_lock_mutex(root);
1675 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes,
1676 root_objectid, ref_generation,
1677 owner_objectid, owner_offset, pin);
1678 maybe_unlock_mutex(root);
1679 return ret;
1680}
1681
Chris Mason87ee04e2007-11-30 11:30:34 -05001682static u64 stripe_align(struct btrfs_root *root, u64 val)
1683{
1684 u64 mask = ((u64)root->stripesize - 1);
1685 u64 ret = (val + mask) & ~mask;
1686 return ret;
1687}
1688
Chris Masonfec577f2007-02-26 10:40:21 -05001689/*
1690 * walks the btree of allocated extents and find a hole of a given size.
1691 * The key ins is changed to record the hole:
1692 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04001693 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001694 * ins->offset == number of blocks
1695 * Any available blocks before search_start are skipped.
1696 */
Chris Mason98ed5172008-01-03 10:01:48 -05001697static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1698 struct btrfs_root *orig_root,
1699 u64 num_bytes, u64 empty_size,
1700 u64 search_start, u64 search_end,
1701 u64 hint_byte, struct btrfs_key *ins,
1702 u64 exclude_start, u64 exclude_nr,
1703 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001704{
Chris Mason87ee04e2007-11-30 11:30:34 -05001705 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04001706 u64 orig_search_start;
Chris Mason9f5fae22007-03-20 14:38:32 -04001707 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001708 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001709 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001710 u64 *last_ptr = NULL;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001711 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001712 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001713 int wrapped = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001714 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001715 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04001716 int allowed_chunk_alloc = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001717
Chris Masondb945352007-10-15 16:15:53 -04001718 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001719 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1720
Chris Mason0ef3e662008-05-24 14:04:53 -04001721 if (orig_root->ref_cows || empty_size)
1722 allowed_chunk_alloc = 1;
1723
Chris Mason239b14b2008-03-24 15:02:07 -04001724 if (data & BTRFS_BLOCK_GROUP_METADATA) {
1725 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04001726 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04001727 }
1728
1729 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
1730 last_ptr = &root->fs_info->last_data_alloc;
1731 }
1732
1733 if (last_ptr) {
1734 if (*last_ptr)
1735 hint_byte = *last_ptr;
1736 else {
1737 empty_size += empty_cluster;
1738 }
1739 }
1740
Chris Masona061fc82008-05-07 11:43:44 -04001741 search_start = max(search_start, first_logical_byte(root, 0));
1742 orig_search_start = search_start;
1743
Chris Mason7f93bf82008-03-24 15:01:28 -04001744 if (search_end == (u64)-1)
1745 search_end = btrfs_super_total_bytes(&info->super_copy);
Chris Mason0b86a832008-03-24 15:01:56 -04001746
Chris Masondb945352007-10-15 16:15:53 -04001747 if (hint_byte) {
Chris Mason0ef3e662008-05-24 14:04:53 -04001748 block_group = btrfs_lookup_first_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001749 if (!block_group)
1750 hint_byte = search_start;
Chris Mason925baed2008-06-25 16:01:30 -04001751 block_group = __btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -04001752 hint_byte, data, 1);
Chris Mason239b14b2008-03-24 15:02:07 -04001753 if (last_ptr && *last_ptr == 0 && block_group)
1754 hint_byte = block_group->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001755 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001756 block_group = __btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001757 trans->block_group,
1758 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001759 }
Chris Mason239b14b2008-03-24 15:02:07 -04001760 search_start = max(search_start, hint_byte);
Chris Masonbe744172007-05-06 10:15:01 -04001761
Chris Mason6702ed42007-08-07 16:15:09 -04001762 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001763
Chris Masonbe744172007-05-06 10:15:01 -04001764check_failed:
Chris Mason70b043f2007-12-13 09:02:46 -05001765 if (!block_group) {
Chris Mason0ef3e662008-05-24 14:04:53 -04001766 block_group = btrfs_lookup_first_block_group(info,
1767 search_start);
Chris Mason70b043f2007-12-13 09:02:46 -05001768 if (!block_group)
Chris Mason0ef3e662008-05-24 14:04:53 -04001769 block_group = btrfs_lookup_first_block_group(info,
Chris Mason70b043f2007-12-13 09:02:46 -05001770 orig_search_start);
1771 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001772 if (full_scan && !chunk_alloc_done) {
1773 if (allowed_chunk_alloc) {
1774 do_chunk_alloc(trans, root,
1775 num_bytes + 2 * 1024 * 1024, data, 1);
1776 allowed_chunk_alloc = 0;
1777 } else if (block_group && block_group_bits(block_group, data)) {
1778 block_group->space_info->force_alloc = 1;
1779 }
1780 chunk_alloc_done = 1;
1781 }
Chris Mason0b86a832008-03-24 15:01:56 -04001782 ret = find_search_start(root, &block_group, &search_start,
1783 total_needed, data);
Chris Mason239b14b2008-03-24 15:02:07 -04001784 if (ret == -ENOSPC && last_ptr && *last_ptr) {
1785 *last_ptr = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001786 block_group = btrfs_lookup_first_block_group(info,
1787 orig_search_start);
Chris Mason239b14b2008-03-24 15:02:07 -04001788 search_start = orig_search_start;
1789 ret = find_search_start(root, &block_group, &search_start,
1790 total_needed, data);
1791 }
1792 if (ret == -ENOSPC)
1793 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04001794 if (ret)
1795 goto error;
1796
Chris Mason239b14b2008-03-24 15:02:07 -04001797 if (last_ptr && *last_ptr && search_start != *last_ptr) {
1798 *last_ptr = 0;
1799 if (!empty_size) {
1800 empty_size += empty_cluster;
1801 total_needed += empty_size;
1802 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001803 block_group = btrfs_lookup_first_block_group(info,
Chris Mason239b14b2008-03-24 15:02:07 -04001804 orig_search_start);
1805 search_start = orig_search_start;
1806 ret = find_search_start(root, &block_group,
1807 &search_start, total_needed, data);
1808 if (ret == -ENOSPC)
1809 goto enospc;
1810 if (ret)
1811 goto error;
1812 }
1813
Chris Mason87ee04e2007-11-30 11:30:34 -05001814 search_start = stripe_align(root, search_start);
Chris Masonfec577f2007-02-26 10:40:21 -05001815 ins->objectid = search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001816 ins->offset = num_bytes;
Chris Masone37c9e62007-05-09 20:13:14 -04001817
Chris Masondb945352007-10-15 16:15:53 -04001818 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001819 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04001820
1821 if (ins->objectid + num_bytes >
1822 block_group->key.objectid + block_group->key.offset) {
Chris Masone19caa52007-10-15 16:17:44 -04001823 search_start = block_group->key.objectid +
1824 block_group->key.offset;
1825 goto new_group;
1826 }
Chris Mason0b86a832008-03-24 15:01:56 -04001827
Chris Mason1a5bc162007-10-15 16:15:26 -04001828 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001829 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1830 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001831 goto new_group;
1832 }
Chris Mason0b86a832008-03-24 15:01:56 -04001833
Chris Mason1a5bc162007-10-15 16:15:26 -04001834 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001835 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1836 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001837 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001838 }
Chris Mason0b86a832008-03-24 15:01:56 -04001839
Chris Masondb945352007-10-15 16:15:53 -04001840 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001841 ins->objectid < exclude_start + exclude_nr)) {
1842 search_start = exclude_start + exclude_nr;
1843 goto new_group;
1844 }
Chris Mason0b86a832008-03-24 15:01:56 -04001845
Chris Mason6324fbf2008-03-24 15:01:59 -04001846 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
Chris Mason5276aed2007-06-11 21:33:38 -04001847 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001848 if (block_group)
1849 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001850 }
Chris Masondb945352007-10-15 16:15:53 -04001851 ins->offset = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001852 if (last_ptr) {
1853 *last_ptr = ins->objectid + ins->offset;
1854 if (*last_ptr ==
1855 btrfs_super_total_bytes(&root->fs_info->super_copy)) {
1856 *last_ptr = 0;
1857 }
1858 }
Chris Masonfec577f2007-02-26 10:40:21 -05001859 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001860
1861new_group:
Chris Masondb945352007-10-15 16:15:53 -04001862 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001863enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001864 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001865 if (full_scan) {
1866 ret = -ENOSPC;
1867 goto error;
1868 }
Chris Mason6702ed42007-08-07 16:15:09 -04001869 if (wrapped) {
1870 if (!full_scan)
1871 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001872 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001873 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001874 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001875 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001876 block_group = btrfs_lookup_first_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001877 cond_resched();
Chris Mason925baed2008-06-25 16:01:30 -04001878 block_group = __btrfs_find_block_group(root, block_group,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001879 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001880 goto check_failed;
1881
Chris Mason0f70abe2007-02-28 16:46:22 -05001882error:
Chris Mason0f70abe2007-02-28 16:46:22 -05001883 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001884}
Chris Masonec44a352008-04-28 15:29:52 -04001885
Chris Masonfec577f2007-02-26 10:40:21 -05001886/*
Chris Masonfec577f2007-02-26 10:40:21 -05001887 * finds a free extent and does all the dirty work required for allocation
1888 * returns the key for the extent through ins, and a tree buffer for
1889 * the first block of the extent through buf.
1890 *
1891 * returns 0 if everything worked, non-zero otherwise.
1892 */
Chris Mason4d775672007-04-20 20:23:12 -04001893int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -05001894 struct btrfs_root *root,
Chris Mason98d20f62008-04-14 09:46:10 -04001895 u64 num_bytes, u64 min_alloc_size,
1896 u64 root_objectid, u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05001897 u64 owner, u64 owner_offset,
1898 u64 empty_size, u64 hint_byte,
Chris Masonec44a352008-04-28 15:29:52 -04001899 u64 search_end, struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05001900{
1901 int ret;
1902 int pending_ret;
Chris Masonc31f8832008-01-08 15:46:31 -05001903 u64 super_used;
1904 u64 root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001905 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04001906 u64 alloc_profile;
Chris Mason47e4bb92008-02-01 14:51:59 -05001907 u32 sizes[2];
Chris Mason1261ec42007-03-20 20:35:03 -04001908 struct btrfs_fs_info *info = root->fs_info;
1909 struct btrfs_root *extent_root = info->extent_root;
Chris Mason47e4bb92008-02-01 14:51:59 -05001910 struct btrfs_extent_item *extent_item;
1911 struct btrfs_extent_ref *ref;
Chris Mason7bb86312007-12-11 09:25:06 -05001912 struct btrfs_path *path;
Chris Mason47e4bb92008-02-01 14:51:59 -05001913 struct btrfs_key keys[2];
Chris Mason8f662a72008-01-02 10:01:11 -05001914
Chris Mason925baed2008-06-25 16:01:30 -04001915 maybe_lock_mutex(root);
1916
Chris Mason6324fbf2008-03-24 15:01:59 -04001917 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04001918 alloc_profile = info->avail_data_alloc_bits &
1919 info->data_alloc_profile;
1920 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001921 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04001922 alloc_profile = info->avail_system_alloc_bits &
1923 info->system_alloc_profile;
1924 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001925 } else {
Chris Mason8790d502008-04-03 16:29:03 -04001926 alloc_profile = info->avail_metadata_alloc_bits &
1927 info->metadata_alloc_profile;
1928 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001929 }
Chris Mason98d20f62008-04-14 09:46:10 -04001930again:
Chris Masona061fc82008-05-07 11:43:44 -04001931 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04001932 /*
1933 * the only place that sets empty_size is btrfs_realloc_node, which
1934 * is not called recursively on allocations
1935 */
1936 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04001937 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001938 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04001939 2 * 1024 * 1024,
1940 BTRFS_BLOCK_GROUP_METADATA |
1941 (info->metadata_alloc_profile &
1942 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04001943 BUG_ON(ret);
1944 }
1945 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04001946 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04001947 BUG_ON(ret);
1948 }
Chris Mason0b86a832008-03-24 15:01:56 -04001949
Chris Masondb945352007-10-15 16:15:53 -04001950 WARN_ON(num_bytes < root->sectorsize);
1951 ret = find_free_extent(trans, root, num_bytes, empty_size,
1952 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001953 trans->alloc_exclude_start,
1954 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04001955
Chris Mason98d20f62008-04-14 09:46:10 -04001956 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
1957 num_bytes = num_bytes >> 1;
1958 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04001959 do_chunk_alloc(trans, root->fs_info->extent_root,
1960 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04001961 goto again;
1962 }
Chris Masonec44a352008-04-28 15:29:52 -04001963 if (ret) {
1964 printk("allocation failed flags %Lu\n", data);
1965 }
Chris Mason925baed2008-06-25 16:01:30 -04001966 if (ret) {
1967 BUG();
1968 goto out;
1969 }
Chris Masonf2654de2007-06-26 12:20:46 -04001970
Josef Bacik58176a92007-08-29 15:47:34 -04001971 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04001972 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04001973 super_used = btrfs_super_bytes_used(&info->super_copy);
1974 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04001975 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04001976
Josef Bacik58176a92007-08-29 15:47:34 -04001977 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001978 root_used = btrfs_root_used(&root->root_item);
1979 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001980
Chris Masonf510cfe2007-10-15 16:14:48 -04001981 clear_extent_dirty(&root->fs_info->free_space_cache,
1982 ins->objectid, ins->objectid + ins->offset - 1,
1983 GFP_NOFS);
1984
Chris Mason26b80032007-08-08 20:17:12 -04001985 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001986 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1987 ins->objectid + ins->offset - 1,
1988 EXTENT_LOCKED, GFP_NOFS);
Chris Mason26b80032007-08-08 20:17:12 -04001989 goto update_block;
1990 }
1991
1992 WARN_ON(trans->alloc_exclude_nr);
1993 trans->alloc_exclude_start = ins->objectid;
1994 trans->alloc_exclude_nr = ins->offset;
Chris Mason037e6392007-03-07 11:50:24 -05001995
Chris Mason47e4bb92008-02-01 14:51:59 -05001996 memcpy(&keys[0], ins, sizeof(*ins));
1997 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
1998 owner, owner_offset);
1999 keys[1].objectid = ins->objectid;
2000 keys[1].type = BTRFS_EXTENT_REF_KEY;
2001 sizes[0] = sizeof(*extent_item);
2002 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002003
2004 path = btrfs_alloc_path();
2005 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002006
2007 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2008 sizes, 2);
Chris Mason26b80032007-08-08 20:17:12 -04002009
Chris Masonccd467d2007-06-28 15:57:36 -04002010 BUG_ON(ret);
Chris Mason47e4bb92008-02-01 14:51:59 -05002011 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2012 struct btrfs_extent_item);
2013 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2014 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2015 struct btrfs_extent_ref);
2016
2017 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2018 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2019 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2020 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
2021
2022 btrfs_mark_buffer_dirty(path->nodes[0]);
2023
2024 trans->alloc_exclude_start = 0;
2025 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002026 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002027 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002028 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04002029
Chris Mason925baed2008-06-25 16:01:30 -04002030 if (ret)
2031 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002032 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04002033 ret = pending_ret;
2034 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002035 }
Chris Mason26b80032007-08-08 20:17:12 -04002036
2037update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04002038 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002039 if (ret) {
2040 printk("update block group failed for %Lu %Lu\n",
2041 ins->objectid, ins->offset);
2042 BUG();
2043 }
Chris Mason925baed2008-06-25 16:01:30 -04002044out:
2045 maybe_unlock_mutex(root);
2046 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002047}
Chris Masonfec577f2007-02-26 10:40:21 -05002048/*
2049 * helper function to allocate a block for a given tree
2050 * returns the tree buffer or NULL.
2051 */
Chris Mason5f39d392007-10-15 16:14:19 -04002052struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04002053 struct btrfs_root *root,
Chris Mason7bb86312007-12-11 09:25:06 -05002054 u32 blocksize,
Chris Mason7bb86312007-12-11 09:25:06 -05002055 u64 root_objectid,
2056 u64 ref_generation,
2057 u64 first_objectid,
2058 int level,
2059 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04002060 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05002061{
Chris Masone2fa7222007-03-12 16:22:34 -04002062 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05002063 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002064 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05002065
Chris Mason98d20f62008-04-14 09:46:10 -04002066 ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
Chris Mason7bb86312007-12-11 09:25:06 -05002067 root_objectid, ref_generation,
Chris Masonf6dbff52007-12-13 11:13:32 -05002068 level, first_objectid, empty_size, hint,
Chris Masondb945352007-10-15 16:15:53 -04002069 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002070 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04002071 BUG_ON(ret > 0);
2072 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05002073 }
Chris Masondb945352007-10-15 16:15:53 -04002074 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002075 if (!buf) {
Chris Mason7bb86312007-12-11 09:25:06 -05002076 btrfs_free_extent(trans, root, ins.objectid, blocksize,
2077 root->root_key.objectid, ref_generation,
2078 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002079 return ERR_PTR(-ENOMEM);
2080 }
Chris Mason55c69072008-01-09 15:55:33 -05002081 btrfs_set_header_generation(buf, trans->transid);
Chris Mason925baed2008-06-25 16:01:30 -04002082 btrfs_tree_lock(buf);
Chris Mason55c69072008-01-09 15:55:33 -05002083 clean_tree_block(trans, root, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002084 btrfs_set_buffer_uptodate(buf);
Chris Mason55c69072008-01-09 15:55:33 -05002085
2086 if (PageDirty(buf->first_page)) {
2087 printk("page %lu dirty\n", buf->first_page->index);
2088 WARN_ON(1);
2089 }
2090
Chris Mason5f39d392007-10-15 16:14:19 -04002091 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2092 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason9afbb0b2008-02-15 13:19:35 -05002093 if (!btrfs_test_opt(root, SSD))
2094 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002095 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05002096 return buf;
2097}
Chris Masona28ec192007-03-06 20:08:01 -05002098
Chris Mason98ed5172008-01-03 10:01:48 -05002099static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2100 struct btrfs_root *root,
2101 struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04002102{
Chris Mason7bb86312007-12-11 09:25:06 -05002103 u64 leaf_owner;
2104 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04002105 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002106 struct btrfs_file_extent_item *fi;
2107 int i;
2108 int nritems;
2109 int ret;
2110
Chris Mason5f39d392007-10-15 16:14:19 -04002111 BUG_ON(!btrfs_is_leaf(leaf));
2112 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05002113 leaf_owner = btrfs_header_owner(leaf);
2114 leaf_generation = btrfs_header_generation(leaf);
2115
Chris Mason6407bf62007-03-27 06:33:00 -04002116 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002117 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002118
2119 btrfs_item_key_to_cpu(leaf, &key, i);
2120 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04002121 continue;
2122 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002123 if (btrfs_file_extent_type(leaf, fi) ==
2124 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04002125 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04002126 /*
2127 * FIXME make sure to insert a trans record that
2128 * repeats the snapshot del on crash
2129 */
Chris Masondb945352007-10-15 16:15:53 -04002130 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2131 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04002132 continue;
Chris Mason925baed2008-06-25 16:01:30 -04002133 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002134 btrfs_file_extent_disk_num_bytes(leaf, fi),
2135 leaf_owner, leaf_generation,
2136 key.objectid, key.offset, 0);
Chris Mason6407bf62007-03-27 06:33:00 -04002137 BUG_ON(ret);
2138 }
2139 return 0;
2140}
2141
Chris Mason98ed5172008-01-03 10:01:48 -05002142static void noinline reada_walk_down(struct btrfs_root *root,
Chris Masonbea495e2008-01-24 16:13:14 -05002143 struct extent_buffer *node,
2144 int slot)
Chris Masone0115992007-06-19 16:23:05 -04002145{
Chris Masondb945352007-10-15 16:15:53 -04002146 u64 bytenr;
Chris Masonbea495e2008-01-24 16:13:14 -05002147 u64 last = 0;
2148 u32 nritems;
Chris Masone0115992007-06-19 16:23:05 -04002149 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04002150 u32 blocksize;
Chris Masonbea495e2008-01-24 16:13:14 -05002151 int ret;
2152 int i;
2153 int level;
2154 int skipped = 0;
Chris Masone0115992007-06-19 16:23:05 -04002155
Chris Mason5f39d392007-10-15 16:14:19 -04002156 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04002157 level = btrfs_header_level(node);
Chris Masonbea495e2008-01-24 16:13:14 -05002158 if (level)
2159 return;
2160
2161 for (i = slot; i < nritems && skipped < 32; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002162 bytenr = btrfs_node_blockptr(node, i);
Chris Masonbea495e2008-01-24 16:13:14 -05002163 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2164 (last > bytenr && last - bytenr > 32 * 1024))) {
2165 skipped++;
Chris Masone0115992007-06-19 16:23:05 -04002166 continue;
Chris Masonbea495e2008-01-24 16:13:14 -05002167 }
2168 blocksize = btrfs_level_size(root, level - 1);
2169 if (i != slot) {
2170 ret = lookup_extent_ref(NULL, root, bytenr,
2171 blocksize, &refs);
2172 BUG_ON(ret);
2173 if (refs != 1) {
2174 skipped++;
2175 continue;
2176 }
2177 }
Chris Masona2135012008-06-25 16:01:30 -04002178 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonca7a79a2008-05-12 12:59:19 -04002179 ret = readahead_tree_block(root, bytenr, blocksize,
2180 btrfs_node_ptr_generation(node, i));
Chris Masonbea495e2008-01-24 16:13:14 -05002181 last = bytenr + blocksize;
Chris Mason409eb952007-08-08 20:17:12 -04002182 cond_resched();
Chris Masona2135012008-06-25 16:01:30 -04002183 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masone0115992007-06-19 16:23:05 -04002184 if (ret)
2185 break;
2186 }
2187}
2188
Chris Mason9aca1d52007-03-13 11:09:37 -04002189/*
2190 * helper function for drop_snapshot, this walks down the tree dropping ref
2191 * counts as it goes.
2192 */
Chris Mason98ed5172008-01-03 10:01:48 -05002193static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2194 struct btrfs_root *root,
2195 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002196{
Chris Mason7bb86312007-12-11 09:25:06 -05002197 u64 root_owner;
2198 u64 root_gen;
2199 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04002200 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002201 struct extent_buffer *next;
2202 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002203 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04002204 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002205 int ret;
2206 u32 refs;
2207
Chris Mason925baed2008-06-25 16:01:30 -04002208 mutex_lock(&root->fs_info->alloc_mutex);
2209
Chris Mason5caf2a02007-04-02 11:20:42 -04002210 WARN_ON(*level < 0);
2211 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04002212 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002213 path->nodes[*level]->start,
2214 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002215 BUG_ON(ret);
2216 if (refs > 1)
2217 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002218
Chris Mason9aca1d52007-03-13 11:09:37 -04002219 /*
2220 * walk down to the last node level and free all the leaves
2221 */
Chris Mason6407bf62007-03-27 06:33:00 -04002222 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002223 WARN_ON(*level < 0);
2224 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002225 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002226
Chris Mason5f39d392007-10-15 16:14:19 -04002227 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002228 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002229
Chris Mason7518a232007-03-12 12:01:18 -04002230 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002231 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002232 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002233 if (*level == 0) {
2234 ret = drop_leaf_ref(trans, root, cur);
2235 BUG_ON(ret);
2236 break;
2237 }
Chris Masondb945352007-10-15 16:15:53 -04002238 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04002239 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04002240 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002241
Chris Masondb945352007-10-15 16:15:53 -04002242 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002243 BUG_ON(ret);
2244 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002245 parent = path->nodes[*level];
2246 root_owner = btrfs_header_owner(parent);
2247 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002248 path->slots[*level]++;
Chris Mason925baed2008-06-25 16:01:30 -04002249 ret = __btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002250 blocksize, root_owner,
2251 root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002252 BUG_ON(ret);
2253 continue;
2254 }
Chris Masondb945352007-10-15 16:15:53 -04002255 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04002256 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002257 free_extent_buffer(next);
Chris Masonbea495e2008-01-24 16:13:14 -05002258 reada_walk_down(root, cur, path->slots[*level]);
Chris Masone9d0b132007-08-10 14:06:19 -04002259
Chris Mason925baed2008-06-25 16:01:30 -04002260 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonca7a79a2008-05-12 12:59:19 -04002261 next = read_tree_block(root, bytenr, blocksize,
2262 ptr_gen);
Chris Mason925baed2008-06-25 16:01:30 -04002263 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8790d502008-04-03 16:29:03 -04002264
2265 /* we've dropped the lock, double check */
Chris Masondb945352007-10-15 16:15:53 -04002266 ret = lookup_extent_ref(trans, root, bytenr,
2267 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002268 BUG_ON(ret);
2269 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002270 parent = path->nodes[*level];
2271 root_owner = btrfs_header_owner(parent);
2272 root_gen = btrfs_header_generation(parent);
2273
Chris Masone9d0b132007-08-10 14:06:19 -04002274 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04002275 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04002276 ret = __btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002277 blocksize,
2278 root_owner,
2279 root_gen, 0, 0, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04002280 BUG_ON(ret);
2281 continue;
2282 }
2283 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002284 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002285 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002286 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002287 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002288 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002289 path->slots[*level] = 0;
2290 }
2291out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002292 WARN_ON(*level < 0);
2293 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002294
2295 if (path->nodes[*level] == root->node) {
2296 root_owner = root->root_key.objectid;
2297 parent = path->nodes[*level];
2298 } else {
2299 parent = path->nodes[*level + 1];
2300 root_owner = btrfs_header_owner(parent);
2301 }
2302
2303 root_gen = btrfs_header_generation(parent);
Chris Mason925baed2008-06-25 16:01:30 -04002304 ret = __btrfs_free_extent(trans, root, path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002305 path->nodes[*level]->len,
2306 root_owner, root_gen, 0, 0, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002307 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05002308 path->nodes[*level] = NULL;
2309 *level += 1;
2310 BUG_ON(ret);
Chris Mason925baed2008-06-25 16:01:30 -04002311 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason20524f02007-03-10 06:35:47 -05002312 return 0;
2313}
2314
Chris Mason9aca1d52007-03-13 11:09:37 -04002315/*
2316 * helper for dropping snapshots. This walks back up the tree in the path
2317 * to find the first node higher up where we haven't yet gone through
2318 * all the slots
2319 */
Chris Mason98ed5172008-01-03 10:01:48 -05002320static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2321 struct btrfs_root *root,
2322 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002323{
Chris Mason7bb86312007-12-11 09:25:06 -05002324 u64 root_owner;
2325 u64 root_gen;
2326 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002327 int i;
2328 int slot;
2329 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04002330
Chris Mason234b63a2007-03-13 10:46:10 -04002331 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05002332 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04002333 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2334 struct extent_buffer *node;
2335 struct btrfs_disk_key disk_key;
2336 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05002337 path->slots[i]++;
2338 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04002339 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002340 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04002341 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04002342 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04002343 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05002344 return 0;
2345 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002346 if (path->nodes[*level] == root->node) {
2347 root_owner = root->root_key.objectid;
2348 root_gen =
2349 btrfs_header_generation(path->nodes[*level]);
2350 } else {
2351 struct extent_buffer *node;
2352 node = path->nodes[*level + 1];
2353 root_owner = btrfs_header_owner(node);
2354 root_gen = btrfs_header_generation(node);
2355 }
Chris Masone089f052007-03-16 16:20:31 -04002356 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002357 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002358 path->nodes[*level]->len,
2359 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04002360 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002361 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04002362 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05002363 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05002364 }
2365 }
2366 return 1;
2367}
2368
Chris Mason9aca1d52007-03-13 11:09:37 -04002369/*
2370 * drop the reference count on the tree rooted at 'snap'. This traverses
2371 * the tree freeing any blocks that have a ref count of zero after being
2372 * decremented.
2373 */
Chris Masone089f052007-03-16 16:20:31 -04002374int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04002375 *root)
Chris Mason20524f02007-03-10 06:35:47 -05002376{
Chris Mason3768f362007-03-13 16:47:54 -04002377 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04002378 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05002379 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04002380 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05002381 int i;
2382 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002383 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002384
Chris Masona2135012008-06-25 16:01:30 -04002385 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04002386 path = btrfs_alloc_path();
2387 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05002388
Chris Mason5f39d392007-10-15 16:14:19 -04002389 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05002390 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002391 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2392 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04002393 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04002394 path->slots[level] = 0;
2395 } else {
2396 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002397 struct btrfs_disk_key found_key;
2398 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04002399
Chris Mason9f3a7422007-08-07 15:52:19 -04002400 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04002401 level = root_item->drop_level;
2402 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002403 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04002404 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04002405 ret = wret;
2406 goto out;
2407 }
Chris Mason5f39d392007-10-15 16:14:19 -04002408 node = path->nodes[level];
2409 btrfs_node_key(node, &found_key, path->slots[level]);
2410 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2411 sizeof(found_key)));
Chris Mason925baed2008-06-25 16:01:30 -04002412 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
2413 if (path->nodes[i] && path->locks[i]) {
2414 path->locks[i] = 0;
2415 btrfs_tree_unlock(path->nodes[i]);
2416 }
2417 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002418 }
Chris Mason20524f02007-03-10 06:35:47 -05002419 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002420 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002421 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002422 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002423 if (wret < 0)
2424 ret = wret;
2425
Chris Mason5caf2a02007-04-02 11:20:42 -04002426 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002427 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002428 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002429 if (wret < 0)
2430 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04002431 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04002432 break;
Chris Mason20524f02007-03-10 06:35:47 -05002433 }
Chris Mason83e15a22007-03-12 09:03:27 -04002434 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002435 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04002436 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04002437 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04002438 }
Chris Mason20524f02007-03-10 06:35:47 -05002439 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002440out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002441 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04002442 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05002443}
Chris Mason9078a3e2007-04-26 16:46:15 -04002444
Chris Masonbe744172007-05-06 10:15:01 -04002445int btrfs_free_block_groups(struct btrfs_fs_info *info)
2446{
Chris Masonf510cfe2007-10-15 16:14:48 -04002447 u64 start;
2448 u64 end;
Yanb97f9202007-11-01 11:28:41 -04002449 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04002450 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04002451
2452 mutex_lock(&info->alloc_mutex);
Chris Mason96b51792007-10-15 16:15:19 -04002453 while(1) {
2454 ret = find_first_extent_bit(&info->block_group_cache, 0,
2455 &start, &end, (unsigned int)-1);
2456 if (ret)
2457 break;
Yanb97f9202007-11-01 11:28:41 -04002458 ret = get_state_private(&info->block_group_cache, start, &ptr);
2459 if (!ret)
2460 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04002461 clear_extent_bits(&info->block_group_cache, start,
2462 end, (unsigned int)-1, GFP_NOFS);
2463 }
Chris Masone37c9e62007-05-09 20:13:14 -04002464 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04002465 ret = find_first_extent_bit(&info->free_space_cache, 0,
2466 &start, &end, EXTENT_DIRTY);
2467 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04002468 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04002469 clear_extent_dirty(&info->free_space_cache, start,
2470 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04002471 }
Chris Mason925baed2008-06-25 16:01:30 -04002472 mutex_unlock(&info->alloc_mutex);
Chris Masonbe744172007-05-06 10:15:01 -04002473 return 0;
2474}
2475
Chris Mason8e7bf942008-04-28 09:02:36 -04002476static unsigned long calc_ra(unsigned long start, unsigned long last,
2477 unsigned long nr)
2478{
2479 return min(last, start + nr - 1);
2480}
2481
Chris Mason98ed5172008-01-03 10:01:48 -05002482static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2483 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05002484{
2485 u64 page_start;
2486 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002487 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05002488 unsigned long i;
2489 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05002490 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05002491 struct file_ra_state *ra;
Chris Mason8e7bf942008-04-28 09:02:36 -04002492 unsigned long total_read = 0;
2493 unsigned long ra_pages;
Chris Masona061fc82008-05-07 11:43:44 -04002494 struct btrfs_trans_handle *trans;
Chris Mason4313b392008-01-03 09:08:48 -05002495
2496 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002497
2498 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05002499 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05002500 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2501
Chris Mason8e7bf942008-04-28 09:02:36 -04002502 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
2503
Chris Mason4313b392008-01-03 09:08:48 -05002504 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05002505
Chris Mason4313b392008-01-03 09:08:48 -05002506 for (; i <= last_index; i++) {
Chris Mason8e7bf942008-04-28 09:02:36 -04002507 if (total_read % ra_pages == 0) {
2508 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
2509 calc_ra(i, last_index, ra_pages));
2510 }
2511 total_read++;
Chris Masona061fc82008-05-07 11:43:44 -04002512 if (((u64)i << PAGE_CACHE_SHIFT) > inode->i_size)
2513 goto truncate_racing;
2514
Chris Masonedbd8d42007-12-21 16:27:24 -05002515 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04002516 if (!page) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002517 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04002518 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002519 if (!PageUptodate(page)) {
2520 btrfs_readpage(NULL, page);
2521 lock_page(page);
2522 if (!PageUptodate(page)) {
2523 unlock_page(page);
2524 page_cache_release(page);
2525 goto out_unlock;
2526 }
2527 }
Chris Masonec44a352008-04-28 15:29:52 -04002528#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2529 ClearPageDirty(page);
2530#else
2531 cancel_dirty_page(page, PAGE_CACHE_SIZE);
2532#endif
2533 wait_on_page_writeback(page);
2534 set_page_extent_mapped(page);
Chris Masonedbd8d42007-12-21 16:27:24 -05002535 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2536 page_end = page_start + PAGE_CACHE_SIZE - 1;
2537
Chris Masond1310b22008-01-24 16:13:08 -05002538 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002539
Chris Masond1310b22008-01-24 16:13:08 -05002540 set_extent_delalloc(io_tree, page_start,
Chris Masonedbd8d42007-12-21 16:27:24 -05002541 page_end, GFP_NOFS);
Chris Masona061fc82008-05-07 11:43:44 -04002542 set_page_dirty(page);
Chris Masonedbd8d42007-12-21 16:27:24 -05002543
Chris Masond1310b22008-01-24 16:13:08 -05002544 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002545 unlock_page(page);
2546 page_cache_release(page);
2547 }
Chris Masona061fc82008-05-07 11:43:44 -04002548 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2549 total_read);
Chris Masonedbd8d42007-12-21 16:27:24 -05002550
2551out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04002552 kfree(ra);
Chris Masona061fc82008-05-07 11:43:44 -04002553 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
2554 if (trans) {
2555 btrfs_add_ordered_inode(inode);
2556 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
2557 mark_inode_dirty(inode);
2558 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002559 mutex_unlock(&inode->i_mutex);
2560 return 0;
Chris Masona061fc82008-05-07 11:43:44 -04002561
2562truncate_racing:
2563 vmtruncate(inode, inode->i_size);
2564 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2565 total_read);
2566 goto out_unlock;
Chris Masonedbd8d42007-12-21 16:27:24 -05002567}
2568
Chris Mason4313b392008-01-03 09:08:48 -05002569/*
Chris Masonbf4ef672008-05-08 13:26:18 -04002570 * The back references tell us which tree holds a ref on a block,
2571 * but it is possible for the tree root field in the reference to
2572 * reflect the original root before a snapshot was made. In this
2573 * case we should search through all the children of a given root
2574 * to find potential holders of references on a block.
2575 *
2576 * Instead, we do something a little less fancy and just search
2577 * all the roots for a given key/block combination.
2578 */
2579static int find_root_for_ref(struct btrfs_root *root,
2580 struct btrfs_path *path,
2581 struct btrfs_key *key0,
2582 int level,
2583 int file_key,
2584 struct btrfs_root **found_root,
2585 u64 bytenr)
2586{
2587 struct btrfs_key root_location;
2588 struct btrfs_root *cur_root = *found_root;
2589 struct btrfs_file_extent_item *file_extent;
2590 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
2591 u64 found_bytenr;
2592 int ret;
2593 int i;
2594
2595 root_location.offset = (u64)-1;
2596 root_location.type = BTRFS_ROOT_ITEM_KEY;
2597 path->lowest_level = level;
2598 path->reada = 0;
2599 while(1) {
2600 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
2601 found_bytenr = 0;
2602 if (ret == 0 && file_key) {
2603 struct extent_buffer *leaf = path->nodes[0];
2604 file_extent = btrfs_item_ptr(leaf, path->slots[0],
2605 struct btrfs_file_extent_item);
2606 if (btrfs_file_extent_type(leaf, file_extent) ==
2607 BTRFS_FILE_EXTENT_REG) {
2608 found_bytenr =
2609 btrfs_file_extent_disk_bytenr(leaf,
2610 file_extent);
2611 }
Chris Mason323da792008-05-09 11:46:48 -04002612 } else if (!file_key) {
Chris Masonbf4ef672008-05-08 13:26:18 -04002613 if (path->nodes[level])
2614 found_bytenr = path->nodes[level]->start;
2615 }
2616
2617 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2618 if (!path->nodes[i])
2619 break;
2620 free_extent_buffer(path->nodes[i]);
2621 path->nodes[i] = NULL;
2622 }
2623 btrfs_release_path(cur_root, path);
2624
2625 if (found_bytenr == bytenr) {
2626 *found_root = cur_root;
2627 ret = 0;
2628 goto out;
2629 }
2630 ret = btrfs_search_root(root->fs_info->tree_root,
2631 root_search_start, &root_search_start);
2632 if (ret)
2633 break;
2634
2635 root_location.objectid = root_search_start;
2636 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
2637 &root_location);
2638 if (!cur_root) {
2639 ret = 1;
2640 break;
2641 }
2642 }
2643out:
2644 path->lowest_level = 0;
2645 return ret;
2646}
2647
2648/*
Chris Mason4313b392008-01-03 09:08:48 -05002649 * note, this releases the path
2650 */
Chris Mason98ed5172008-01-03 10:01:48 -05002651static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05002652 struct btrfs_path *path,
Chris Mason0ef3e662008-05-24 14:04:53 -04002653 struct btrfs_key *extent_key,
2654 u64 *last_file_objectid,
2655 u64 *last_file_offset,
2656 u64 *last_file_root,
2657 u64 last_extent)
Chris Masonedbd8d42007-12-21 16:27:24 -05002658{
2659 struct inode *inode;
2660 struct btrfs_root *found_root;
Chris Masonbf4ef672008-05-08 13:26:18 -04002661 struct btrfs_key root_location;
2662 struct btrfs_key found_key;
Chris Mason4313b392008-01-03 09:08:48 -05002663 struct btrfs_extent_ref *ref;
2664 u64 ref_root;
2665 u64 ref_gen;
2666 u64 ref_objectid;
2667 u64 ref_offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05002668 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04002669 int level;
Chris Masonedbd8d42007-12-21 16:27:24 -05002670
Chris Mason4313b392008-01-03 09:08:48 -05002671 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2672 struct btrfs_extent_ref);
2673 ref_root = btrfs_ref_root(path->nodes[0], ref);
2674 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2675 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2676 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2677 btrfs_release_path(extent_root, path);
2678
Chris Masonbf4ef672008-05-08 13:26:18 -04002679 root_location.objectid = ref_root;
Chris Masonedbd8d42007-12-21 16:27:24 -05002680 if (ref_gen == 0)
Chris Masonbf4ef672008-05-08 13:26:18 -04002681 root_location.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002682 else
Chris Masonbf4ef672008-05-08 13:26:18 -04002683 root_location.offset = (u64)-1;
2684 root_location.type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05002685
2686 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Masonbf4ef672008-05-08 13:26:18 -04002687 &root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002688 BUG_ON(!found_root);
2689
2690 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Chris Masonbf4ef672008-05-08 13:26:18 -04002691 found_key.objectid = ref_objectid;
2692 found_key.type = BTRFS_EXTENT_DATA_KEY;
2693 found_key.offset = ref_offset;
2694 level = 0;
2695
Chris Mason0ef3e662008-05-24 14:04:53 -04002696 if (last_extent == extent_key->objectid &&
2697 *last_file_objectid == ref_objectid &&
2698 *last_file_offset == ref_offset &&
2699 *last_file_root == ref_root)
2700 goto out;
2701
Chris Masonbf4ef672008-05-08 13:26:18 -04002702 ret = find_root_for_ref(extent_root, path, &found_key,
2703 level, 1, &found_root,
2704 extent_key->objectid);
2705
2706 if (ret)
2707 goto out;
2708
Chris Mason0ef3e662008-05-24 14:04:53 -04002709 if (last_extent == extent_key->objectid &&
2710 *last_file_objectid == ref_objectid &&
2711 *last_file_offset == ref_offset &&
2712 *last_file_root == ref_root)
2713 goto out;
2714
Chris Masonedbd8d42007-12-21 16:27:24 -05002715 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2716 ref_objectid, found_root);
2717 if (inode->i_state & I_NEW) {
2718 /* the inode and parent dir are two different roots */
2719 BTRFS_I(inode)->root = found_root;
2720 BTRFS_I(inode)->location.objectid = ref_objectid;
2721 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2722 BTRFS_I(inode)->location.offset = 0;
2723 btrfs_read_locked_inode(inode);
2724 unlock_new_inode(inode);
2725
2726 }
2727 /* this can happen if the reference is not against
2728 * the latest version of the tree root
2729 */
2730 if (is_bad_inode(inode)) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002731 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);
Chris Masonedbd8d42007-12-21 16:27:24 -05002739 } else {
2740 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05002741 struct extent_buffer *eb;
Chris Masonedbd8d42007-12-21 16:27:24 -05002742 int i;
2743
Chris Masonedbd8d42007-12-21 16:27:24 -05002744 eb = read_tree_block(found_root, extent_key->objectid,
Chris Masonca7a79a2008-05-12 12:59:19 -04002745 extent_key->offset, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002746 btrfs_tree_lock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05002747 level = btrfs_header_level(eb);
2748
2749 if (level == 0)
2750 btrfs_item_key_to_cpu(eb, &found_key, 0);
2751 else
2752 btrfs_node_key_to_cpu(eb, &found_key, 0);
2753
Chris Mason925baed2008-06-25 16:01:30 -04002754 btrfs_tree_unlock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05002755 free_extent_buffer(eb);
2756
Chris Masonbf4ef672008-05-08 13:26:18 -04002757 ret = find_root_for_ref(extent_root, path, &found_key,
2758 level, 0, &found_root,
2759 extent_key->objectid);
2760
2761 if (ret)
2762 goto out;
2763
2764 trans = btrfs_start_transaction(found_root, 1);
2765
Chris Masonedbd8d42007-12-21 16:27:24 -05002766 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05002767 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002768 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2769 0, 1);
2770 path->lowest_level = 0;
2771 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2772 if (!path->nodes[i])
2773 break;
2774 free_extent_buffer(path->nodes[i]);
2775 path->nodes[i] = NULL;
2776 }
2777 btrfs_release_path(found_root, path);
Chris Mason0ef3e662008-05-24 14:04:53 -04002778 if (found_root == found_root->fs_info->extent_root)
2779 btrfs_extent_post_op(trans, found_root);
Chris Masonedbd8d42007-12-21 16:27:24 -05002780 btrfs_end_transaction(trans, found_root);
2781 }
2782
2783out:
2784 return 0;
2785}
2786
Chris Masona061fc82008-05-07 11:43:44 -04002787static int noinline del_extent_zero(struct btrfs_root *extent_root,
2788 struct btrfs_path *path,
2789 struct btrfs_key *extent_key)
2790{
2791 int ret;
2792 struct btrfs_trans_handle *trans;
2793
2794 trans = btrfs_start_transaction(extent_root, 1);
2795 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
2796 if (ret > 0) {
2797 ret = -EIO;
2798 goto out;
2799 }
2800 if (ret < 0)
2801 goto out;
2802 ret = btrfs_del_item(trans, extent_root, path);
2803out:
2804 btrfs_end_transaction(trans, extent_root);
2805 return ret;
2806}
2807
Chris Mason98ed5172008-01-03 10:01:48 -05002808static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2809 struct btrfs_path *path,
2810 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002811{
2812 struct btrfs_key key;
2813 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002814 struct extent_buffer *leaf;
Chris Mason0ef3e662008-05-24 14:04:53 -04002815 u64 last_file_objectid = 0;
2816 u64 last_file_root = 0;
2817 u64 last_file_offset = (u64)-1;
2818 u64 last_extent = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002819 u32 nritems;
2820 u32 item_size;
2821 int ret = 0;
2822
Chris Masona061fc82008-05-07 11:43:44 -04002823 if (extent_key->objectid == 0) {
2824 ret = del_extent_zero(extent_root, path, extent_key);
2825 goto out;
2826 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002827 key.objectid = extent_key->objectid;
2828 key.type = BTRFS_EXTENT_REF_KEY;
2829 key.offset = 0;
2830
2831 while(1) {
2832 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2833
Chris Masonedbd8d42007-12-21 16:27:24 -05002834 if (ret < 0)
2835 goto out;
2836
2837 ret = 0;
2838 leaf = path->nodes[0];
2839 nritems = btrfs_header_nritems(leaf);
Chris Masona061fc82008-05-07 11:43:44 -04002840 if (path->slots[0] == nritems) {
2841 ret = btrfs_next_leaf(extent_root, path);
2842 if (ret > 0) {
2843 ret = 0;
2844 goto out;
2845 }
2846 if (ret < 0)
2847 goto out;
Chris Masonbf4ef672008-05-08 13:26:18 -04002848 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04002849 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002850
2851 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masona061fc82008-05-07 11:43:44 -04002852 if (found_key.objectid != extent_key->objectid) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002853 break;
Chris Masona061fc82008-05-07 11:43:44 -04002854 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002855
Chris Masona061fc82008-05-07 11:43:44 -04002856 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002857 break;
Chris Masona061fc82008-05-07 11:43:44 -04002858 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002859
2860 key.offset = found_key.offset + 1;
2861 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2862
Chris Mason0ef3e662008-05-24 14:04:53 -04002863 ret = relocate_one_reference(extent_root, path, extent_key,
2864 &last_file_objectid,
2865 &last_file_offset,
2866 &last_file_root, last_extent);
Chris Masonedbd8d42007-12-21 16:27:24 -05002867 if (ret)
2868 goto out;
Chris Mason0ef3e662008-05-24 14:04:53 -04002869 last_extent = extent_key->objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05002870 }
2871 ret = 0;
2872out:
2873 btrfs_release_path(extent_root, path);
2874 return ret;
2875}
2876
Chris Masonec44a352008-04-28 15:29:52 -04002877static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
2878{
2879 u64 num_devices;
2880 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
2881 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
2882
Chris Masona061fc82008-05-07 11:43:44 -04002883 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04002884 if (num_devices == 1) {
2885 stripped |= BTRFS_BLOCK_GROUP_DUP;
2886 stripped = flags & ~stripped;
2887
2888 /* turn raid0 into single device chunks */
2889 if (flags & BTRFS_BLOCK_GROUP_RAID0)
2890 return stripped;
2891
2892 /* turn mirroring into duplication */
2893 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2894 BTRFS_BLOCK_GROUP_RAID10))
2895 return stripped | BTRFS_BLOCK_GROUP_DUP;
2896 return flags;
2897 } else {
2898 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04002899 if (flags & stripped)
2900 return flags;
2901
2902 stripped |= BTRFS_BLOCK_GROUP_DUP;
2903 stripped = flags & ~stripped;
2904
2905 /* switch duplicated blocks with raid1 */
2906 if (flags & BTRFS_BLOCK_GROUP_DUP)
2907 return stripped | BTRFS_BLOCK_GROUP_RAID1;
2908
2909 /* turn single device chunks into raid0 */
2910 return stripped | BTRFS_BLOCK_GROUP_RAID0;
2911 }
2912 return flags;
2913}
2914
Chris Mason0ef3e662008-05-24 14:04:53 -04002915int __alloc_chunk_for_shrink(struct btrfs_root *root,
2916 struct btrfs_block_group_cache *shrink_block_group,
2917 int force)
2918{
2919 struct btrfs_trans_handle *trans;
2920 u64 new_alloc_flags;
2921 u64 calc;
2922
2923 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
2924
2925 trans = btrfs_start_transaction(root, 1);
2926 new_alloc_flags = update_block_group_flags(root,
2927 shrink_block_group->flags);
2928 if (new_alloc_flags != shrink_block_group->flags) {
2929 calc =
2930 btrfs_block_group_used(&shrink_block_group->item);
2931 } else {
2932 calc = shrink_block_group->key.offset;
2933 }
2934 do_chunk_alloc(trans, root->fs_info->extent_root,
2935 calc + 2 * 1024 * 1024, new_alloc_flags, force);
2936 btrfs_end_transaction(trans, root);
2937 }
2938 return 0;
2939}
2940
Chris Mason8f18cf12008-04-25 16:53:30 -04002941int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05002942{
2943 struct btrfs_trans_handle *trans;
2944 struct btrfs_root *tree_root = root->fs_info->tree_root;
2945 struct btrfs_path *path;
2946 u64 cur_byte;
2947 u64 total_found;
Chris Mason8f18cf12008-04-25 16:53:30 -04002948 u64 shrink_last_byte;
2949 struct btrfs_block_group_cache *shrink_block_group;
Chris Masonedbd8d42007-12-21 16:27:24 -05002950 struct btrfs_fs_info *info = root->fs_info;
Chris Masonedbd8d42007-12-21 16:27:24 -05002951 struct btrfs_key key;
Yan73e48b22008-01-03 14:14:39 -05002952 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002953 struct extent_buffer *leaf;
2954 u32 nritems;
2955 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04002956 int progress;
Chris Masonedbd8d42007-12-21 16:27:24 -05002957
Chris Mason925baed2008-06-25 16:01:30 -04002958 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002959 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
2960 shrink_start);
2961 BUG_ON(!shrink_block_group);
2962
Chris Mason0ef3e662008-05-24 14:04:53 -04002963 shrink_last_byte = shrink_block_group->key.objectid +
2964 shrink_block_group->key.offset;
Chris Mason8f18cf12008-04-25 16:53:30 -04002965
2966 shrink_block_group->space_info->total_bytes -=
2967 shrink_block_group->key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05002968 path = btrfs_alloc_path();
2969 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05002970 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002971
Chris Mason323da792008-05-09 11:46:48 -04002972 printk("btrfs relocating block group %llu flags %llu\n",
2973 (unsigned long long)shrink_start,
2974 (unsigned long long)shrink_block_group->flags);
2975
Chris Mason0ef3e662008-05-24 14:04:53 -04002976 __alloc_chunk_for_shrink(root, shrink_block_group, 1);
Chris Mason323da792008-05-09 11:46:48 -04002977
Chris Mason0ef3e662008-05-24 14:04:53 -04002978again:
2979
Chris Mason8f18cf12008-04-25 16:53:30 -04002980 shrink_block_group->ro = 1;
2981
Chris Masonedbd8d42007-12-21 16:27:24 -05002982 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04002983 progress = 0;
Chris Mason8f18cf12008-04-25 16:53:30 -04002984 key.objectid = shrink_start;
Chris Masonedbd8d42007-12-21 16:27:24 -05002985 key.offset = 0;
2986 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05002987 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05002988
Yan73e48b22008-01-03 14:14:39 -05002989 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2990 if (ret < 0)
2991 goto out;
2992
Chris Mason0b86a832008-03-24 15:01:56 -04002993 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yan73e48b22008-01-03 14:14:39 -05002994 if (ret < 0)
2995 goto out;
Chris Mason8f18cf12008-04-25 16:53:30 -04002996
Yan73e48b22008-01-03 14:14:39 -05002997 if (ret == 0) {
2998 leaf = path->nodes[0];
2999 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003000 if (found_key.objectid + found_key.offset > shrink_start &&
3001 found_key.objectid < shrink_last_byte) {
Yan73e48b22008-01-03 14:14:39 -05003002 cur_byte = found_key.objectid;
3003 key.objectid = cur_byte;
3004 }
3005 }
3006 btrfs_release_path(root, path);
3007
3008 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003009 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3010 if (ret < 0)
3011 goto out;
Yan73e48b22008-01-03 14:14:39 -05003012
Chris Masonedbd8d42007-12-21 16:27:24 -05003013 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05003014 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05003015next:
3016 if (path->slots[0] >= nritems) {
3017 ret = btrfs_next_leaf(root, path);
3018 if (ret < 0)
3019 goto out;
3020 if (ret == 1) {
3021 ret = 0;
3022 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05003023 }
Yan73e48b22008-01-03 14:14:39 -05003024 leaf = path->nodes[0];
3025 nritems = btrfs_header_nritems(leaf);
3026 }
3027
3028 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05003029
Chris Mason8f18cf12008-04-25 16:53:30 -04003030 if (found_key.objectid >= shrink_last_byte)
3031 break;
3032
Chris Mason725c8462008-01-04 16:47:16 -05003033 if (progress && need_resched()) {
3034 memcpy(&key, &found_key, sizeof(key));
Chris Mason725c8462008-01-04 16:47:16 -05003035 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05003036 btrfs_release_path(root, path);
3037 btrfs_search_slot(NULL, root, &key, path, 0, 0);
3038 progress = 0;
3039 goto next;
3040 }
3041 progress = 1;
3042
Yan73e48b22008-01-03 14:14:39 -05003043 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
3044 found_key.objectid + found_key.offset <= cur_byte) {
Chris Mason0ef3e662008-05-24 14:04:53 -04003045 memcpy(&key, &found_key, sizeof(key));
3046 key.offset++;
Yan73e48b22008-01-03 14:14:39 -05003047 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003048 goto next;
3049 }
Yan73e48b22008-01-03 14:14:39 -05003050
Chris Masonedbd8d42007-12-21 16:27:24 -05003051 total_found++;
3052 cur_byte = found_key.objectid + found_key.offset;
3053 key.objectid = cur_byte;
3054 btrfs_release_path(root, path);
3055 ret = relocate_one_extent(root, path, &found_key);
Chris Mason0ef3e662008-05-24 14:04:53 -04003056 __alloc_chunk_for_shrink(root, shrink_block_group, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003057 }
3058
3059 btrfs_release_path(root, path);
3060
3061 if (total_found > 0) {
Chris Mason323da792008-05-09 11:46:48 -04003062 printk("btrfs relocate found %llu last extent was %llu\n",
3063 (unsigned long long)total_found,
3064 (unsigned long long)found_key.objectid);
Chris Masonedbd8d42007-12-21 16:27:24 -05003065 trans = btrfs_start_transaction(tree_root, 1);
3066 btrfs_commit_transaction(trans, tree_root);
3067
Chris Masonedbd8d42007-12-21 16:27:24 -05003068 btrfs_clean_old_snapshots(tree_root);
Chris Masonedbd8d42007-12-21 16:27:24 -05003069
3070 trans = btrfs_start_transaction(tree_root, 1);
3071 btrfs_commit_transaction(trans, tree_root);
3072 goto again;
3073 }
3074
Chris Mason8f18cf12008-04-25 16:53:30 -04003075 /*
3076 * we've freed all the extents, now remove the block
3077 * group item from the tree
3078 */
Chris Masonedbd8d42007-12-21 16:27:24 -05003079 trans = btrfs_start_transaction(root, 1);
Chris Mason8f18cf12008-04-25 16:53:30 -04003080 memcpy(&key, &shrink_block_group->key, sizeof(key));
Chris Mason4313b392008-01-03 09:08:48 -05003081
Chris Mason8f18cf12008-04-25 16:53:30 -04003082 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3083 if (ret > 0)
3084 ret = -EIO;
3085 if (ret < 0)
3086 goto out;
Yan73e48b22008-01-03 14:14:39 -05003087
Chris Mason0ef3e662008-05-24 14:04:53 -04003088 clear_extent_bits(&info->block_group_cache, key.objectid,
3089 key.objectid + key.offset - 1,
Chris Mason8f18cf12008-04-25 16:53:30 -04003090 (unsigned int)-1, GFP_NOFS);
Yan73e48b22008-01-03 14:14:39 -05003091
Chris Mason0ef3e662008-05-24 14:04:53 -04003092
3093 clear_extent_bits(&info->free_space_cache,
3094 key.objectid, key.objectid + key.offset - 1,
3095 (unsigned int)-1, GFP_NOFS);
3096
3097 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
3098 kfree(shrink_block_group);
3099
Chris Mason8f18cf12008-04-25 16:53:30 -04003100 btrfs_del_item(trans, root, path);
Chris Masonedbd8d42007-12-21 16:27:24 -05003101 btrfs_commit_transaction(trans, root);
Chris Mason0ef3e662008-05-24 14:04:53 -04003102
3103 /* the code to unpin extents might set a few bits in the free
3104 * space cache for this range again
3105 */
3106 clear_extent_bits(&info->free_space_cache,
3107 key.objectid, key.objectid + key.offset - 1,
3108 (unsigned int)-1, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003109out:
3110 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003111 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003112 return ret;
3113}
3114
Chris Mason0b86a832008-03-24 15:01:56 -04003115int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3116 struct btrfs_key *key)
3117{
Chris Mason925baed2008-06-25 16:01:30 -04003118 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003119 struct btrfs_key found_key;
3120 struct extent_buffer *leaf;
3121 int slot;
3122
3123 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3124 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003125 goto out;
3126
Chris Mason0b86a832008-03-24 15:01:56 -04003127 while(1) {
3128 slot = path->slots[0];
3129 leaf = path->nodes[0];
3130 if (slot >= btrfs_header_nritems(leaf)) {
3131 ret = btrfs_next_leaf(root, path);
3132 if (ret == 0)
3133 continue;
3134 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003135 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04003136 break;
3137 }
3138 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3139
3140 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04003141 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3142 ret = 0;
3143 goto out;
3144 }
Chris Mason0b86a832008-03-24 15:01:56 -04003145 path->slots[0]++;
3146 }
3147 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04003148out:
Chris Mason0b86a832008-03-24 15:01:56 -04003149 return ret;
3150}
3151
Chris Mason9078a3e2007-04-26 16:46:15 -04003152int btrfs_read_block_groups(struct btrfs_root *root)
3153{
3154 struct btrfs_path *path;
3155 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04003156 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04003157 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04003158 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003159 struct btrfs_space_info *space_info;
Chris Masond1310b22008-01-24 16:13:08 -05003160 struct extent_io_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003161 struct btrfs_key key;
3162 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04003163 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04003164
3165 block_group_cache = &info->block_group_cache;
Chris Masonbe744172007-05-06 10:15:01 -04003166 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04003167 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003168 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003169 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04003170 path = btrfs_alloc_path();
3171 if (!path)
3172 return -ENOMEM;
3173
Chris Mason925baed2008-06-25 16:01:30 -04003174 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04003175 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003176 ret = find_first_block_group(root, path, &key);
3177 if (ret > 0) {
3178 ret = 0;
3179 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04003180 }
Chris Mason0b86a832008-03-24 15:01:56 -04003181 if (ret != 0)
3182 goto error;
3183
Chris Mason5f39d392007-10-15 16:14:19 -04003184 leaf = path->nodes[0];
3185 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003186 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04003187 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04003188 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04003189 break;
3190 }
Chris Mason3e1ad542007-05-07 20:03:49 -04003191
Chris Mason5f39d392007-10-15 16:14:19 -04003192 read_extent_buffer(leaf, &cache->item,
3193 btrfs_item_ptr_offset(leaf, path->slots[0]),
3194 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04003195 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04003196
Chris Mason9078a3e2007-04-26 16:46:15 -04003197 key.objectid = found_key.objectid + found_key.offset;
3198 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04003199 cache->flags = btrfs_block_group_flags(&cache->item);
3200 bit = 0;
3201 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04003202 bit = BLOCK_GROUP_DATA;
Chris Mason0b86a832008-03-24 15:01:56 -04003203 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3204 bit = BLOCK_GROUP_SYSTEM;
3205 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason96b51792007-10-15 16:15:19 -04003206 bit = BLOCK_GROUP_METADATA;
Chris Mason31f3c992007-04-30 15:25:45 -04003207 }
Chris Mason8790d502008-04-03 16:29:03 -04003208 set_avail_alloc_bits(info, cache->flags);
Chris Mason96b51792007-10-15 16:15:19 -04003209
Chris Mason6324fbf2008-03-24 15:01:59 -04003210 ret = update_space_info(info, cache->flags, found_key.offset,
3211 btrfs_block_group_used(&cache->item),
3212 &space_info);
3213 BUG_ON(ret);
3214 cache->space_info = space_info;
3215
Chris Mason96b51792007-10-15 16:15:19 -04003216 /* use EXTENT_LOCKED to prevent merging */
3217 set_extent_bits(block_group_cache, found_key.objectid,
3218 found_key.objectid + found_key.offset - 1,
3219 bit | EXTENT_LOCKED, GFP_NOFS);
3220 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04003221 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04003222
Chris Mason9078a3e2007-04-26 16:46:15 -04003223 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04003224 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04003225 break;
3226 }
Chris Mason0b86a832008-03-24 15:01:56 -04003227 ret = 0;
3228error:
Chris Mason9078a3e2007-04-26 16:46:15 -04003229 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003230 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04003231 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003232}
Chris Mason6324fbf2008-03-24 15:01:59 -04003233
3234int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3235 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04003236 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04003237 u64 size)
3238{
3239 int ret;
3240 int bit = 0;
3241 struct btrfs_root *extent_root;
3242 struct btrfs_block_group_cache *cache;
3243 struct extent_io_tree *block_group_cache;
3244
3245 extent_root = root->fs_info->extent_root;
3246 block_group_cache = &root->fs_info->block_group_cache;
3247
Chris Mason8f18cf12008-04-25 16:53:30 -04003248 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04003249 BUG_ON(!cache);
Chris Masone17cade2008-04-15 15:41:47 -04003250 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04003251 cache->key.offset = size;
Chris Mason6324fbf2008-03-24 15:01:59 -04003252 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04003253
Chris Mason6324fbf2008-03-24 15:01:59 -04003254 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04003255 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3256 cache->flags = type;
3257 btrfs_set_block_group_flags(&cache->item, type);
3258
3259 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
3260 &cache->space_info);
3261 BUG_ON(ret);
3262
Chris Masond18a2c42008-04-04 15:40:00 -04003263 bit = block_group_state_bits(type);
Chris Masone17cade2008-04-15 15:41:47 -04003264 set_extent_bits(block_group_cache, chunk_offset,
3265 chunk_offset + size - 1,
Chris Mason6324fbf2008-03-24 15:01:59 -04003266 bit | EXTENT_LOCKED, GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04003267
Chris Masone17cade2008-04-15 15:41:47 -04003268 set_state_private(block_group_cache, chunk_offset,
3269 (unsigned long)cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04003270 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3271 sizeof(cache->item));
3272 BUG_ON(ret);
3273
3274 finish_current_insert(trans, extent_root);
3275 ret = del_pending_extents(trans, extent_root);
3276 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04003277 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04003278
Chris Mason6324fbf2008-03-24 15:01:59 -04003279 return 0;
3280}