blob: e6a3ba9dd1079f595987212a23cf20c87845a735 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
Chris Mason74493f72007-12-11 09:25:06 -050021#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040022#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050023#include "ctree.h"
24#include "disk-io.h"
25#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040026#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040027#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040028#include "locking.h"
Chris Masonfec577f2007-02-26 10:40:21 -050029
Chris Mason0b86a832008-03-24 15:01:56 -040030#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
Chris Mason96b51792007-10-15 16:15:19 -040031#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
Chris Mason0b86a832008-03-24 15:01:56 -040032#define BLOCK_GROUP_SYSTEM EXTENT_NEW
33
Chris Mason96b51792007-10-15 16:15:19 -040034#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
35
Chris Masone089f052007-03-16 16:20:31 -040036static int finish_current_insert(struct btrfs_trans_handle *trans, struct
37 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040038static int del_pending_extents(struct btrfs_trans_handle *trans, struct
39 btrfs_root *extent_root);
Chris Mason925baed2008-06-25 16:01:30 -040040static struct btrfs_block_group_cache *
41__btrfs_find_block_group(struct btrfs_root *root,
42 struct btrfs_block_group_cache *hint,
43 u64 search_start, int data, int owner);
Yand548ee52008-01-03 13:56:30 -050044
Chris Mason925baed2008-06-25 16:01:30 -040045void maybe_lock_mutex(struct btrfs_root *root)
46{
47 if (root != root->fs_info->extent_root &&
48 root != root->fs_info->chunk_root &&
49 root != root->fs_info->dev_root) {
50 mutex_lock(&root->fs_info->alloc_mutex);
51 }
52}
53
54void maybe_unlock_mutex(struct btrfs_root *root)
55{
56 if (root != root->fs_info->extent_root &&
57 root != root->fs_info->chunk_root &&
58 root != root->fs_info->dev_root) {
59 mutex_unlock(&root->fs_info->alloc_mutex);
60 }
61}
Chris Masonfec577f2007-02-26 10:40:21 -050062
Chris Masone37c9e62007-05-09 20:13:14 -040063static int cache_block_group(struct btrfs_root *root,
64 struct btrfs_block_group_cache *block_group)
65{
66 struct btrfs_path *path;
67 int ret;
68 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -040069 struct extent_buffer *leaf;
Chris Masond1310b22008-01-24 16:13:08 -050070 struct extent_io_tree *free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040071 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -040072 u64 last = 0;
73 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040074 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040075 int found = 0;
76
Chris Mason00f5c792007-11-30 10:09:33 -050077 if (!block_group)
78 return 0;
79
Chris Masone37c9e62007-05-09 20:13:14 -040080 root = root->fs_info->extent_root;
Chris Masonf510cfe2007-10-15 16:14:48 -040081 free_space_cache = &root->fs_info->free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040082
83 if (block_group->cached)
84 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -040085
Chris Masone37c9e62007-05-09 20:13:14 -040086 path = btrfs_alloc_path();
87 if (!path)
88 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040089
Chris Mason2cc58cf2007-08-27 16:49:44 -040090 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -040091 /*
92 * we get into deadlocks with paths held by callers of this function.
93 * since the alloc_mutex is protecting things right now, just
94 * skip the locking here
95 */
96 path->skip_locking = 1;
Yan7d7d6062007-09-14 16:15:28 -040097 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040098 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040099 key.offset = 0;
100 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
101 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
102 if (ret < 0)
103 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -0400104 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -0500105 if (ret < 0)
106 return ret;
107 if (ret == 0) {
108 leaf = path->nodes[0];
109 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
110 if (key.objectid + key.offset > first_free)
111 first_free = key.objectid + key.offset;
112 }
Chris Masone37c9e62007-05-09 20:13:14 -0400113 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400114 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400115 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400116 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400117 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400118 if (ret < 0)
119 goto err;
Chris Masonde428b62007-05-18 13:28:27 -0400120 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -0400121 continue;
Chris Masonde428b62007-05-18 13:28:27 -0400122 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400123 break;
124 }
125 }
Chris Mason5f39d392007-10-15 16:14:19 -0400126 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -0400127 if (key.objectid < block_group->key.objectid) {
Yan7d7d6062007-09-14 16:15:28 -0400128 goto next;
129 }
Chris Masone37c9e62007-05-09 20:13:14 -0400130 if (key.objectid >= block_group->key.objectid +
131 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -0400132 break;
133 }
134
135 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
136 if (!found) {
137 last = first_free;
138 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400139 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400140 if (key.objectid > last) {
141 hole_size = key.objectid - last;
142 set_extent_dirty(free_space_cache, last,
143 last + hole_size - 1,
144 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400145 }
Yan7d7d6062007-09-14 16:15:28 -0400146 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400147 }
Yan7d7d6062007-09-14 16:15:28 -0400148next:
Chris Masone37c9e62007-05-09 20:13:14 -0400149 path->slots[0]++;
150 }
151
Yan7d7d6062007-09-14 16:15:28 -0400152 if (!found)
153 last = first_free;
154 if (block_group->key.objectid +
155 block_group->key.offset > last) {
156 hole_size = block_group->key.objectid +
157 block_group->key.offset - last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400158 set_extent_dirty(free_space_cache, last,
159 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400160 }
Chris Masone37c9e62007-05-09 20:13:14 -0400161 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400162err:
Chris Masone37c9e62007-05-09 20:13:14 -0400163 btrfs_free_path(path);
164 return 0;
165}
166
Chris Mason0ef3e662008-05-24 14:04:53 -0400167struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
168 btrfs_fs_info *info,
169 u64 bytenr)
170{
171 struct extent_io_tree *block_group_cache;
172 struct btrfs_block_group_cache *block_group = NULL;
173 u64 ptr;
174 u64 start;
175 u64 end;
176 int ret;
177
178 bytenr = max_t(u64, bytenr,
179 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
180 block_group_cache = &info->block_group_cache;
181 ret = find_first_extent_bit(block_group_cache,
182 bytenr, &start, &end,
183 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
184 BLOCK_GROUP_SYSTEM);
185 if (ret) {
186 return NULL;
187 }
188 ret = get_state_private(block_group_cache, start, &ptr);
189 if (ret)
190 return NULL;
191
192 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
193 return block_group;
194}
195
Chris Mason5276aed2007-06-11 21:33:38 -0400196struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
197 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400198 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400199{
Chris Masond1310b22008-01-24 16:13:08 -0500200 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -0400201 struct btrfs_block_group_cache *block_group = NULL;
202 u64 ptr;
203 u64 start;
204 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400205 int ret;
206
Chris Masona061fc82008-05-07 11:43:44 -0400207 bytenr = max_t(u64, bytenr,
208 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Mason96b51792007-10-15 16:15:19 -0400209 block_group_cache = &info->block_group_cache;
210 ret = find_first_extent_bit(block_group_cache,
Chris Masondb945352007-10-15 16:15:53 -0400211 bytenr, &start, &end,
Chris Mason0b86a832008-03-24 15:01:56 -0400212 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
213 BLOCK_GROUP_SYSTEM);
Chris Masonbe744172007-05-06 10:15:01 -0400214 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400215 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400216 }
Chris Mason96b51792007-10-15 16:15:19 -0400217 ret = get_state_private(block_group_cache, start, &ptr);
218 if (ret)
219 return NULL;
220
Jens Axboeae2f5412007-10-19 09:22:59 -0400221 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Yan5cf66422007-11-16 14:57:09 -0500222 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400223 block_group->key.objectid + block_group->key.offset)
224 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400225 return NULL;
226}
Chris Mason0b86a832008-03-24 15:01:56 -0400227
228static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
229{
Chris Mason593060d2008-03-25 16:50:33 -0400230 return (cache->flags & bits) == bits;
Chris Mason0b86a832008-03-24 15:01:56 -0400231}
232
233static int noinline find_search_start(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500234 struct btrfs_block_group_cache **cache_ret,
Chris Mason0ef3e662008-05-24 14:04:53 -0400235 u64 *start_ret, u64 num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400236{
Chris Masone37c9e62007-05-09 20:13:14 -0400237 int ret;
238 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masond7fc6402008-02-18 12:12:38 -0500239 struct extent_io_tree *free_space_cache;
Chris Mason7d1660d2008-03-24 15:02:03 -0400240 struct extent_state *state;
Chris Masone19caa52007-10-15 16:17:44 -0400241 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400242 u64 start = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500243 u64 cache_miss = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500244 u64 total_fs_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -0400245 u64 search_start = *start_ret;
Chris Masonf84a8b32007-11-06 10:26:29 -0500246 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400247
Chris Mason7d9eb122008-07-08 14:19:17 -0400248 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Masonc31f8832008-01-08 15:46:31 -0500249 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masond7fc6402008-02-18 12:12:38 -0500250 free_space_cache = &root->fs_info->free_space_cache;
251
Chris Mason0ef3e662008-05-24 14:04:53 -0400252 if (!cache)
Chris Mason54aa1f42007-06-22 14:16:25 -0400253 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500254
Chris Mason0ef3e662008-05-24 14:04:53 -0400255again:
256 ret = cache_block_group(root, cache);
257 if (ret) {
258 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400259 }
Chris Masone19caa52007-10-15 16:17:44 -0400260
Chris Mason0ef3e662008-05-24 14:04:53 -0400261 last = max(search_start, cache->key.objectid);
262 if (!block_group_bits(cache, data) || cache->ro)
263 goto new_group;
264
Chris Mason7d1660d2008-03-24 15:02:03 -0400265 spin_lock_irq(&free_space_cache->lock);
266 state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
Chris Masone37c9e62007-05-09 20:13:14 -0400267 while(1) {
Chris Mason7d1660d2008-03-24 15:02:03 -0400268 if (!state) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500269 if (!cache_miss)
270 cache_miss = last;
Chris Mason7d1660d2008-03-24 15:02:03 -0400271 spin_unlock_irq(&free_space_cache->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400272 goto new_group;
273 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400274
Chris Mason7d1660d2008-03-24 15:02:03 -0400275 start = max(last, state->start);
276 last = state->end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500277 if (last - start < num) {
Chris Mason7d1660d2008-03-24 15:02:03 -0400278 do {
279 state = extent_state_next(state);
280 } while(state && !(state->state & EXTENT_DIRTY));
Chris Masonf510cfe2007-10-15 16:14:48 -0400281 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500282 }
Chris Mason7d1660d2008-03-24 15:02:03 -0400283 spin_unlock_irq(&free_space_cache->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -0400284 if (cache->ro) {
Chris Mason8f18cf12008-04-25 16:53:30 -0400285 goto new_group;
Chris Mason0ef3e662008-05-24 14:04:53 -0400286 }
Chris Mason0b86a832008-03-24 15:01:56 -0400287 if (start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400288 goto new_group;
Chris Mason8790d502008-04-03 16:29:03 -0400289 if (!block_group_bits(cache, data)) {
Chris Mason611f0e02008-04-03 16:29:03 -0400290 printk("block group bits don't match %Lu %d\n", cache->flags, data);
Chris Mason8790d502008-04-03 16:29:03 -0400291 }
Chris Mason0b86a832008-03-24 15:01:56 -0400292 *start_ret = start;
293 return 0;
Chris Mason8790d502008-04-03 16:29:03 -0400294 }
295out:
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500296 cache = btrfs_lookup_block_group(root->fs_info, search_start);
297 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -0400298 printk("Unable to find block group for %Lu\n", search_start);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500299 WARN_ON(1);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500300 }
Chris Mason0b86a832008-03-24 15:01:56 -0400301 return -ENOSPC;
Chris Masone37c9e62007-05-09 20:13:14 -0400302
303new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400304 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500305wrapped:
Chris Mason0ef3e662008-05-24 14:04:53 -0400306 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Chris Masonc31f8832008-01-08 15:46:31 -0500307 if (!cache || cache->key.objectid >= total_fs_bytes) {
Chris Mason0e4de582007-11-26 10:55:49 -0500308no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500309 if (!wrapped) {
310 wrapped = 1;
311 last = search_start;
Chris Masonf84a8b32007-11-06 10:26:29 -0500312 goto wrapped;
313 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500314 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400315 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500316 if (cache_miss && !cache->cached) {
317 cache_block_group(root, cache);
318 last = cache_miss;
Chris Mason0ef3e662008-05-24 14:04:53 -0400319 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Chris Mason257d0ce2007-11-07 21:08:16 -0500320 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400321 cache_miss = 0;
Chris Masonc286ac42008-07-22 23:06:41 -0400322 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500323 if (!cache)
324 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400325 *cache_ret = cache;
326 goto again;
327}
328
Chris Mason84f54cf2007-06-12 07:43:08 -0400329static u64 div_factor(u64 num, int factor)
330{
Chris Mason257d0ce2007-11-07 21:08:16 -0500331 if (factor == 10)
332 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400333 num *= factor;
334 do_div(num, 10);
335 return num;
336}
337
Chris Mason6324fbf2008-03-24 15:01:59 -0400338static int block_group_state_bits(u64 flags)
339{
340 int bits = 0;
341 if (flags & BTRFS_BLOCK_GROUP_DATA)
342 bits |= BLOCK_GROUP_DATA;
343 if (flags & BTRFS_BLOCK_GROUP_METADATA)
344 bits |= BLOCK_GROUP_METADATA;
345 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
346 bits |= BLOCK_GROUP_SYSTEM;
347 return bits;
348}
349
Chris Mason925baed2008-06-25 16:01:30 -0400350static struct btrfs_block_group_cache *
351__btrfs_find_block_group(struct btrfs_root *root,
352 struct btrfs_block_group_cache *hint,
353 u64 search_start, int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400354{
Chris Mason96b51792007-10-15 16:15:19 -0400355 struct btrfs_block_group_cache *cache;
Chris Masond1310b22008-01-24 16:13:08 -0500356 struct extent_io_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400357 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400358 struct btrfs_fs_info *info = root->fs_info;
359 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400360 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400361 u64 start;
362 u64 end;
363 u64 free_check;
364 u64 ptr;
365 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400366 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400367 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400368 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400369 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400370
Chris Mason96b51792007-10-15 16:15:19 -0400371 block_group_cache = &info->block_group_cache;
372
Chris Masona236aed2008-04-29 09:38:00 -0400373 if (data & BTRFS_BLOCK_GROUP_METADATA)
374 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400375
Chris Mason6324fbf2008-03-24 15:01:59 -0400376 bit = block_group_state_bits(data);
Chris Masonbe744172007-05-06 10:15:01 -0400377
Chris Mason0ef3e662008-05-24 14:04:53 -0400378 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400379 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400380 shint = btrfs_lookup_first_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400381 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonc286ac42008-07-22 23:06:41 -0400382 spin_lock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400383 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500384 if (used + shint->pinned <
385 div_factor(shint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400386 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400387 return shint;
388 }
Chris Masonc286ac42008-07-22 23:06:41 -0400389 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400390 }
391 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400392 if (hint && !hint->ro && block_group_bits(hint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400393 spin_lock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400394 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500395 if (used + hint->pinned <
396 div_factor(hint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400397 spin_unlock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400398 return hint;
399 }
Chris Masonc286ac42008-07-22 23:06:41 -0400400 spin_unlock(&hint->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400401 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400402 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400403 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400404 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400405 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400406 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400407 }
Chris Mason31f3c992007-04-30 15:25:45 -0400408again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400409 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400410 ret = find_first_extent_bit(block_group_cache, last,
411 &start, &end, bit);
412 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400413 break;
Chris Mason96b51792007-10-15 16:15:19 -0400414
415 ret = get_state_private(block_group_cache, start, &ptr);
Chris Mason0ef3e662008-05-24 14:04:53 -0400416 if (ret) {
417 last = end + 1;
418 continue;
419 }
Chris Mason96b51792007-10-15 16:15:19 -0400420
Jens Axboeae2f5412007-10-19 09:22:59 -0400421 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Masonc286ac42008-07-22 23:06:41 -0400422 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400423 last = cache->key.objectid + cache->key.offset;
424 used = btrfs_block_group_used(&cache->item);
425
Chris Mason8f18cf12008-04-25 16:53:30 -0400426 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400427 free_check = div_factor(cache->key.offset, factor);
Chris Mason8790d502008-04-03 16:29:03 -0400428 if (used + cache->pinned < free_check) {
429 found_group = cache;
Chris Masonc286ac42008-07-22 23:06:41 -0400430 spin_unlock(&cache->lock);
Chris Mason8790d502008-04-03 16:29:03 -0400431 goto found;
432 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400433 }
Chris Masonc286ac42008-07-22 23:06:41 -0400434 spin_unlock(&cache->lock);
Chris Masonde428b62007-05-18 13:28:27 -0400435 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400436 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400437 if (!wrapped) {
438 last = search_start;
439 wrapped = 1;
440 goto again;
441 }
442 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400443 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400444 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400445 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400446 goto again;
447 }
Chris Masonbe744172007-05-06 10:15:01 -0400448found:
Chris Mason31f3c992007-04-30 15:25:45 -0400449 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400450}
451
Chris Mason925baed2008-06-25 16:01:30 -0400452struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
453 struct btrfs_block_group_cache
454 *hint, u64 search_start,
455 int data, int owner)
456{
457
458 struct btrfs_block_group_cache *ret;
Chris Mason925baed2008-06-25 16:01:30 -0400459 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
Chris Mason925baed2008-06-25 16:01:30 -0400460 return ret;
461}
Chris Mason7bb86312007-12-11 09:25:06 -0500462static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500463 u64 owner, u64 owner_offset)
464{
465 u32 high_crc = ~(u32)0;
466 u32 low_crc = ~(u32)0;
467 __le64 lenum;
Chris Mason74493f72007-12-11 09:25:06 -0500468 lenum = cpu_to_le64(root_objectid);
Miguela5eb62e2008-04-11 15:45:51 -0400469 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500470 lenum = cpu_to_le64(ref_generation);
Miguela5eb62e2008-04-11 15:45:51 -0400471 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500472 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
473 lenum = cpu_to_le64(owner);
Miguela5eb62e2008-04-11 15:45:51 -0400474 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500475 lenum = cpu_to_le64(owner_offset);
Miguela5eb62e2008-04-11 15:45:51 -0400476 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500477 }
Chris Mason74493f72007-12-11 09:25:06 -0500478 return ((u64)high_crc << 32) | (u64)low_crc;
479}
480
Chris Mason7bb86312007-12-11 09:25:06 -0500481static int match_extent_ref(struct extent_buffer *leaf,
482 struct btrfs_extent_ref *disk_ref,
483 struct btrfs_extent_ref *cpu_ref)
484{
485 int ret;
486 int len;
487
488 if (cpu_ref->objectid)
489 len = sizeof(*cpu_ref);
490 else
491 len = 2 * sizeof(u64);
492 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
493 len);
494 return ret == 0;
495}
496
Chris Mason98ed5172008-01-03 10:01:48 -0500497static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
498 struct btrfs_root *root,
499 struct btrfs_path *path, u64 bytenr,
500 u64 root_objectid,
501 u64 ref_generation, u64 owner,
502 u64 owner_offset, int del)
Chris Mason7bb86312007-12-11 09:25:06 -0500503{
504 u64 hash;
505 struct btrfs_key key;
506 struct btrfs_key found_key;
507 struct btrfs_extent_ref ref;
508 struct extent_buffer *leaf;
509 struct btrfs_extent_ref *disk_ref;
510 int ret;
511 int ret2;
512
513 btrfs_set_stack_ref_root(&ref, root_objectid);
514 btrfs_set_stack_ref_generation(&ref, ref_generation);
515 btrfs_set_stack_ref_objectid(&ref, owner);
516 btrfs_set_stack_ref_offset(&ref, owner_offset);
517
518 hash = hash_extent_ref(root_objectid, ref_generation, owner,
519 owner_offset);
520 key.offset = hash;
521 key.objectid = bytenr;
522 key.type = BTRFS_EXTENT_REF_KEY;
523
524 while (1) {
525 ret = btrfs_search_slot(trans, root, &key, path,
526 del ? -1 : 0, del);
527 if (ret < 0)
528 goto out;
529 leaf = path->nodes[0];
530 if (ret != 0) {
531 u32 nritems = btrfs_header_nritems(leaf);
532 if (path->slots[0] >= nritems) {
533 ret2 = btrfs_next_leaf(root, path);
534 if (ret2)
535 goto out;
536 leaf = path->nodes[0];
537 }
538 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
539 if (found_key.objectid != bytenr ||
540 found_key.type != BTRFS_EXTENT_REF_KEY)
541 goto out;
542 key.offset = found_key.offset;
543 if (del) {
544 btrfs_release_path(root, path);
545 continue;
546 }
547 }
548 disk_ref = btrfs_item_ptr(path->nodes[0],
549 path->slots[0],
550 struct btrfs_extent_ref);
551 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
552 ret = 0;
553 goto out;
554 }
555 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
556 key.offset = found_key.offset + 1;
557 btrfs_release_path(root, path);
558 }
559out:
560 return ret;
561}
562
Chris Masond8d5f3e2007-12-11 12:42:00 -0500563/*
564 * Back reference rules. Back refs have three main goals:
565 *
566 * 1) differentiate between all holders of references to an extent so that
567 * when a reference is dropped we can make sure it was a valid reference
568 * before freeing the extent.
569 *
570 * 2) Provide enough information to quickly find the holders of an extent
571 * if we notice a given block is corrupted or bad.
572 *
573 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
574 * maintenance. This is actually the same as #2, but with a slightly
575 * different use case.
576 *
577 * File extents can be referenced by:
578 *
579 * - multiple snapshots, subvolumes, or different generations in one subvol
580 * - different files inside a single subvolume (in theory, not implemented yet)
581 * - different offsets inside a file (bookend extents in file.c)
582 *
583 * The extent ref structure has fields for:
584 *
585 * - Objectid of the subvolume root
586 * - Generation number of the tree holding the reference
587 * - objectid of the file holding the reference
588 * - offset in the file corresponding to the key holding the reference
589 *
590 * When a file extent is allocated the fields are filled in:
591 * (root_key.objectid, trans->transid, inode objectid, offset in file)
592 *
593 * When a leaf is cow'd new references are added for every file extent found
594 * in the leaf. It looks the same as the create case, but trans->transid
595 * will be different when the block is cow'd.
596 *
597 * (root_key.objectid, trans->transid, inode objectid, offset in file)
598 *
599 * When a file extent is removed either during snapshot deletion or file
600 * truncation, the corresponding back reference is found
601 * by searching for:
602 *
603 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
604 * inode objectid, offset in file)
605 *
606 * Btree extents can be referenced by:
607 *
608 * - Different subvolumes
609 * - Different generations of the same subvolume
610 *
611 * Storing sufficient information for a full reverse mapping of a btree
612 * block would require storing the lowest key of the block in the backref,
613 * and it would require updating that lowest key either before write out or
614 * every time it changed. Instead, the objectid of the lowest key is stored
615 * along with the level of the tree block. This provides a hint
616 * about where in the btree the block can be found. Searches through the
617 * btree only need to look for a pointer to that block, so they stop one
618 * level higher than the level recorded in the backref.
619 *
620 * Some btrees do not do reference counting on their extents. These
621 * include the extent tree and the tree of tree roots. Backrefs for these
622 * trees always have a generation of zero.
623 *
624 * When a tree block is created, back references are inserted:
625 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500626 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500627 *
628 * When a tree block is cow'd in a reference counted root,
629 * new back references are added for all the blocks it points to.
630 * These are of the form (trans->transid will have increased since creation):
631 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500632 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500633 *
634 * Because the lowest_key_objectid and the level are just hints
635 * they are not used when backrefs are deleted. When a backref is deleted:
636 *
637 * if backref was for a tree root:
638 * root_objectid = root->root_key.objectid
639 * else
640 * root_objectid = btrfs_header_owner(parent)
641 *
642 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
643 *
644 * Back Reference Key hashing:
645 *
646 * Back references have four fields, each 64 bits long. Unfortunately,
647 * This is hashed into a single 64 bit number and placed into the key offset.
648 * The key objectid corresponds to the first byte in the extent, and the
649 * key type is set to BTRFS_EXTENT_REF_KEY
650 */
Chris Mason7bb86312007-12-11 09:25:06 -0500651int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
652 struct btrfs_root *root,
653 struct btrfs_path *path, u64 bytenr,
654 u64 root_objectid, u64 ref_generation,
655 u64 owner, u64 owner_offset)
Chris Mason74493f72007-12-11 09:25:06 -0500656{
657 u64 hash;
658 struct btrfs_key key;
659 struct btrfs_extent_ref ref;
Chris Mason7bb86312007-12-11 09:25:06 -0500660 struct btrfs_extent_ref *disk_ref;
Chris Mason74493f72007-12-11 09:25:06 -0500661 int ret;
662
663 btrfs_set_stack_ref_root(&ref, root_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500664 btrfs_set_stack_ref_generation(&ref, ref_generation);
Chris Mason74493f72007-12-11 09:25:06 -0500665 btrfs_set_stack_ref_objectid(&ref, owner);
666 btrfs_set_stack_ref_offset(&ref, owner_offset);
667
Chris Mason7bb86312007-12-11 09:25:06 -0500668 hash = hash_extent_ref(root_objectid, ref_generation, owner,
669 owner_offset);
Chris Mason74493f72007-12-11 09:25:06 -0500670 key.offset = hash;
671 key.objectid = bytenr;
672 key.type = BTRFS_EXTENT_REF_KEY;
673
674 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
675 while (ret == -EEXIST) {
Chris Mason7bb86312007-12-11 09:25:06 -0500676 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
677 struct btrfs_extent_ref);
678 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
679 goto out;
680 key.offset++;
681 btrfs_release_path(root, path);
682 ret = btrfs_insert_empty_item(trans, root, path, &key,
683 sizeof(ref));
Chris Mason74493f72007-12-11 09:25:06 -0500684 }
Chris Mason7bb86312007-12-11 09:25:06 -0500685 if (ret)
686 goto out;
687 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
688 struct btrfs_extent_ref);
689 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
690 sizeof(ref));
691 btrfs_mark_buffer_dirty(path->nodes[0]);
692out:
693 btrfs_release_path(root, path);
694 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500695}
696
Chris Mason925baed2008-06-25 16:01:30 -0400697static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Chris Masonb18c6682007-04-17 13:26:50 -0400698 struct btrfs_root *root,
Chris Mason74493f72007-12-11 09:25:06 -0500699 u64 bytenr, u64 num_bytes,
Chris Mason7bb86312007-12-11 09:25:06 -0500700 u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500701 u64 owner, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500702{
Chris Mason5caf2a02007-04-02 11:20:42 -0400703 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500704 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400705 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400706 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400707 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400708 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500709
Chris Masondb945352007-10-15 16:15:53 -0400710 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400711 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400712 if (!path)
713 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400714
Chris Mason3c12ac72008-04-21 12:01:38 -0400715 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400716 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400717 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400718 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400719 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400720 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400721 if (ret < 0)
722 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400723 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500724 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400725 }
Chris Mason02217ed2007-03-02 16:08:05 -0500726 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400727 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400728 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400729 refs = btrfs_extent_refs(l, item);
730 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400731 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500732
Chris Mason5caf2a02007-04-02 11:20:42 -0400733 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500734
Chris Mason3c12ac72008-04-21 12:01:38 -0400735 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -0500736 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
737 path, bytenr, root_objectid,
738 ref_generation, owner, owner_offset);
739 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400740 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400741 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500742
743 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500744 return 0;
745}
746
Chris Mason925baed2008-06-25 16:01:30 -0400747int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
748 struct btrfs_root *root,
749 u64 bytenr, u64 num_bytes,
750 u64 root_objectid, u64 ref_generation,
751 u64 owner, u64 owner_offset)
752{
753 int ret;
754
755 mutex_lock(&root->fs_info->alloc_mutex);
756 ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes,
757 root_objectid, ref_generation,
758 owner, owner_offset);
759 mutex_unlock(&root->fs_info->alloc_mutex);
760 return ret;
761}
762
Chris Masone9d0b132007-08-10 14:06:19 -0400763int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
764 struct btrfs_root *root)
765{
766 finish_current_insert(trans, root->fs_info->extent_root);
767 del_pending_extents(trans, root->fs_info->extent_root);
768 return 0;
769}
770
Chris Masonb18c6682007-04-17 13:26:50 -0400771static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400772 struct btrfs_root *root, u64 bytenr,
773 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500774{
Chris Mason5caf2a02007-04-02 11:20:42 -0400775 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500776 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400777 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400778 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400779 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400780
Chris Masondb945352007-10-15 16:15:53 -0400781 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400782 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400783 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400784 key.objectid = bytenr;
785 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400786 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400787 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400788 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400789 if (ret < 0)
790 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400791 if (ret != 0) {
792 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400793 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500794 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400795 }
796 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400797 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400798 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400799out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400800 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500801 return 0;
802}
803
Chris Masonbe20aa92007-12-17 20:14:01 -0500804u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
805 struct btrfs_path *count_path,
Chris Masona68d5932008-05-08 14:11:56 -0400806 u64 expected_owner,
Chris Masonbe20aa92007-12-17 20:14:01 -0500807 u64 first_extent)
808{
809 struct btrfs_root *extent_root = root->fs_info->extent_root;
810 struct btrfs_path *path;
811 u64 bytenr;
812 u64 found_objectid;
Chris Masona68d5932008-05-08 14:11:56 -0400813 u64 found_owner;
Chris Mason56b453c2008-01-03 09:08:27 -0500814 u64 root_objectid = root->root_key.objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500815 u32 total_count = 0;
Chris Masonbbaf5492008-05-08 16:31:21 -0400816 u32 extent_refs;
Chris Masonbe20aa92007-12-17 20:14:01 -0500817 u32 cur_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500818 u32 nritems;
819 int ret;
820 struct btrfs_key key;
821 struct btrfs_key found_key;
822 struct extent_buffer *l;
823 struct btrfs_extent_item *item;
824 struct btrfs_extent_ref *ref_item;
825 int level = -1;
826
Chris Mason925baed2008-06-25 16:01:30 -0400827 /* FIXME, needs locking */
828 BUG();
829
830 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonbe20aa92007-12-17 20:14:01 -0500831 path = btrfs_alloc_path();
832again:
833 if (level == -1)
834 bytenr = first_extent;
835 else
836 bytenr = count_path->nodes[level]->start;
837
838 cur_count = 0;
839 key.objectid = bytenr;
840 key.offset = 0;
841
842 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
843 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
844 if (ret < 0)
845 goto out;
846 BUG_ON(ret == 0);
847
848 l = path->nodes[0];
849 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
850
851 if (found_key.objectid != bytenr ||
852 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
853 goto out;
854 }
855
856 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masonbbaf5492008-05-08 16:31:21 -0400857 extent_refs = btrfs_extent_refs(l, item);
Chris Masonbe20aa92007-12-17 20:14:01 -0500858 while (1) {
Chris Masonbd098352008-01-03 13:23:19 -0500859 l = path->nodes[0];
Chris Masonbe20aa92007-12-17 20:14:01 -0500860 nritems = btrfs_header_nritems(l);
861 if (path->slots[0] >= nritems) {
862 ret = btrfs_next_leaf(extent_root, path);
863 if (ret == 0)
864 continue;
865 break;
866 }
867 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
868 if (found_key.objectid != bytenr)
869 break;
Chris Masonbd098352008-01-03 13:23:19 -0500870
Chris Masonbe20aa92007-12-17 20:14:01 -0500871 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
872 path->slots[0]++;
873 continue;
874 }
875
876 cur_count++;
877 ref_item = btrfs_item_ptr(l, path->slots[0],
878 struct btrfs_extent_ref);
879 found_objectid = btrfs_ref_root(l, ref_item);
880
Chris Mason56b453c2008-01-03 09:08:27 -0500881 if (found_objectid != root_objectid) {
882 total_count = 2;
Chris Mason4313b392008-01-03 09:08:48 -0500883 goto out;
Chris Mason56b453c2008-01-03 09:08:27 -0500884 }
Chris Masona68d5932008-05-08 14:11:56 -0400885 if (level == -1) {
886 found_owner = btrfs_ref_objectid(l, ref_item);
887 if (found_owner != expected_owner) {
888 total_count = 2;
889 goto out;
890 }
Chris Masonbbaf5492008-05-08 16:31:21 -0400891 /*
892 * nasty. we don't count a reference held by
893 * the running transaction. This allows nodatacow
894 * to avoid cow most of the time
895 */
896 if (found_owner >= BTRFS_FIRST_FREE_OBJECTID &&
897 btrfs_ref_generation(l, ref_item) ==
898 root->fs_info->generation) {
899 extent_refs--;
900 }
Chris Masona68d5932008-05-08 14:11:56 -0400901 }
Chris Mason56b453c2008-01-03 09:08:27 -0500902 total_count = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500903 path->slots[0]++;
904 }
Chris Masonbbaf5492008-05-08 16:31:21 -0400905 /*
906 * if there is more than one reference against a data extent,
907 * we have to assume the other ref is another snapshot
908 */
909 if (level == -1 && extent_refs > 1) {
910 total_count = 2;
911 goto out;
912 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500913 if (cur_count == 0) {
914 total_count = 0;
915 goto out;
916 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500917 if (level >= 0 && root->node == count_path->nodes[level])
918 goto out;
919 level++;
920 btrfs_release_path(root, path);
921 goto again;
922
923out:
924 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -0400925 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonbe20aa92007-12-17 20:14:01 -0500926 return total_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500927}
Chris Masonc5739bb2007-04-10 09:27:04 -0400928
Chris Masone089f052007-03-16 16:20:31 -0400929int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400930 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500931{
Chris Masondb945352007-10-15 16:15:53 -0400932 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400933 u32 nritems;
934 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400935 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500936 int i;
Chris Masondb945352007-10-15 16:15:53 -0400937 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400938 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400939 int faili;
Chris Masona28ec192007-03-06 20:08:01 -0500940
Chris Mason3768f362007-03-13 16:47:54 -0400941 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500942 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400943
Chris Masondb945352007-10-15 16:15:53 -0400944 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400945 nritems = btrfs_header_nritems(buf);
946 for (i = 0; i < nritems; i++) {
Chris Masone34a5b42008-07-22 12:08:37 -0400947 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -0400948 if (level == 0) {
949 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400950 btrfs_item_key_to_cpu(buf, &key, i);
951 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400952 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400953 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400954 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400955 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454d2007-04-19 13:37:44 -0400956 BTRFS_FILE_EXTENT_INLINE)
957 continue;
Chris Masondb945352007-10-15 16:15:53 -0400958 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
959 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400960 continue;
Chris Mason4a096752008-07-21 10:29:44 -0400961
962 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -0400963 ret = __btrfs_inc_extent_ref(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500964 btrfs_file_extent_disk_num_bytes(buf, fi),
965 root->root_key.objectid, trans->transid,
966 key.objectid, key.offset);
Chris Mason4a096752008-07-21 10:29:44 -0400967 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason54aa1f42007-06-22 14:16:25 -0400968 if (ret) {
969 faili = i;
Chris Mason4a096752008-07-21 10:29:44 -0400970 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400971 goto fail;
972 }
Chris Mason6407bf62007-03-27 06:33:00 -0400973 } else {
Chris Masondb945352007-10-15 16:15:53 -0400974 bytenr = btrfs_node_blockptr(buf, i);
Chris Mason6caab482007-12-13 09:48:07 -0500975 btrfs_node_key_to_cpu(buf, &key, i);
Chris Mason4a096752008-07-21 10:29:44 -0400976
977 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -0400978 ret = __btrfs_inc_extent_ref(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500979 btrfs_level_size(root, level - 1),
980 root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500981 trans->transid,
982 level - 1, key.objectid);
Chris Mason4a096752008-07-21 10:29:44 -0400983 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason54aa1f42007-06-22 14:16:25 -0400984 if (ret) {
985 faili = i;
Chris Mason4a096752008-07-21 10:29:44 -0400986 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400987 goto fail;
988 }
Chris Mason6407bf62007-03-27 06:33:00 -0400989 }
Chris Mason02217ed2007-03-02 16:08:05 -0500990 }
991 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400992fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400993 WARN_ON(1);
Chris Mason7bb86312007-12-11 09:25:06 -0500994#if 0
Chris Mason54aa1f42007-06-22 14:16:25 -0400995 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400996 if (level == 0) {
997 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400998 btrfs_item_key_to_cpu(buf, &key, i);
999 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001000 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001001 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001002 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001003 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001004 BTRFS_FILE_EXTENT_INLINE)
1005 continue;
Chris Masondb945352007-10-15 16:15:53 -04001006 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1007 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001008 continue;
Chris Masondb945352007-10-15 16:15:53 -04001009 err = btrfs_free_extent(trans, root, disk_bytenr,
1010 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -04001011 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001012 BUG_ON(err);
1013 } else {
Chris Masondb945352007-10-15 16:15:53 -04001014 bytenr = btrfs_node_blockptr(buf, i);
1015 err = btrfs_free_extent(trans, root, bytenr,
1016 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001017 BUG_ON(err);
1018 }
1019 }
Chris Mason7bb86312007-12-11 09:25:06 -05001020#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001021 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001022}
1023
Chris Mason9078a3e2007-04-26 16:46:15 -04001024static int write_one_cache_group(struct btrfs_trans_handle *trans,
1025 struct btrfs_root *root,
1026 struct btrfs_path *path,
1027 struct btrfs_block_group_cache *cache)
1028{
1029 int ret;
1030 int pending_ret;
1031 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001032 unsigned long bi;
1033 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001034
Chris Mason9078a3e2007-04-26 16:46:15 -04001035 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001036 if (ret < 0)
1037 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001038 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001039
1040 leaf = path->nodes[0];
1041 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1042 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1043 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001044 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001045fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001046 finish_current_insert(trans, extent_root);
1047 pending_ret = del_pending_extents(trans, extent_root);
1048 if (ret)
1049 return ret;
1050 if (pending_ret)
1051 return pending_ret;
1052 return 0;
1053
1054}
1055
Chris Mason96b51792007-10-15 16:15:19 -04001056int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1057 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001058{
Chris Masond1310b22008-01-24 16:13:08 -05001059 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -04001060 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001061 int ret;
1062 int err = 0;
1063 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001064 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001065 u64 last = 0;
1066 u64 start;
1067 u64 end;
1068 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -04001069
Chris Mason96b51792007-10-15 16:15:19 -04001070 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001071 path = btrfs_alloc_path();
1072 if (!path)
1073 return -ENOMEM;
1074
Chris Mason925baed2008-06-25 16:01:30 -04001075 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001076 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -04001077 ret = find_first_extent_bit(block_group_cache, last,
1078 &start, &end, BLOCK_GROUP_DIRTY);
1079 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -04001080 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001081
Chris Mason96b51792007-10-15 16:15:19 -04001082 last = end + 1;
1083 ret = get_state_private(block_group_cache, start, &ptr);
1084 if (ret)
1085 break;
Jens Axboeae2f5412007-10-19 09:22:59 -04001086 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -04001087 err = write_one_cache_group(trans, root,
1088 path, cache);
1089 /*
1090 * if we fail to write the cache group, we want
1091 * to keep it marked dirty in hopes that a later
1092 * write will work
1093 */
1094 if (err) {
1095 werr = err;
1096 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001097 }
Chris Mason96b51792007-10-15 16:15:19 -04001098 clear_extent_bits(block_group_cache, start, end,
1099 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001100 }
1101 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04001102 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001103 return werr;
1104}
1105
Chris Mason6324fbf2008-03-24 15:01:59 -04001106static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1107 u64 flags)
1108{
1109 struct list_head *head = &info->space_info;
1110 struct list_head *cur;
1111 struct btrfs_space_info *found;
1112 list_for_each(cur, head) {
1113 found = list_entry(cur, struct btrfs_space_info, list);
1114 if (found->flags == flags)
1115 return found;
1116 }
1117 return NULL;
1118
1119}
1120
Chris Mason593060d2008-03-25 16:50:33 -04001121static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1122 u64 total_bytes, u64 bytes_used,
1123 struct btrfs_space_info **space_info)
1124{
1125 struct btrfs_space_info *found;
1126
1127 found = __find_space_info(info, flags);
1128 if (found) {
1129 found->total_bytes += total_bytes;
1130 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001131 found->full = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001132 WARN_ON(found->total_bytes < found->bytes_used);
1133 *space_info = found;
1134 return 0;
1135 }
1136 found = kmalloc(sizeof(*found), GFP_NOFS);
1137 if (!found)
1138 return -ENOMEM;
1139
1140 list_add(&found->list, &info->space_info);
1141 found->flags = flags;
1142 found->total_bytes = total_bytes;
1143 found->bytes_used = bytes_used;
1144 found->bytes_pinned = 0;
1145 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001146 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001147 *space_info = found;
1148 return 0;
1149}
1150
Chris Mason8790d502008-04-03 16:29:03 -04001151static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1152{
1153 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001154 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001155 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001156 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001157 if (extra_flags) {
1158 if (flags & BTRFS_BLOCK_GROUP_DATA)
1159 fs_info->avail_data_alloc_bits |= extra_flags;
1160 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1161 fs_info->avail_metadata_alloc_bits |= extra_flags;
1162 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1163 fs_info->avail_system_alloc_bits |= extra_flags;
1164 }
1165}
Chris Mason593060d2008-03-25 16:50:33 -04001166
Chris Masona061fc82008-05-07 11:43:44 -04001167static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001168{
Chris Masona061fc82008-05-07 11:43:44 -04001169 u64 num_devices = root->fs_info->fs_devices->num_devices;
1170
1171 if (num_devices == 1)
1172 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1173 if (num_devices < 4)
1174 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1175
Chris Masonec44a352008-04-28 15:29:52 -04001176 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1177 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001178 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001179 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001180 }
Chris Masonec44a352008-04-28 15:29:52 -04001181
1182 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001183 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001184 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001185 }
Chris Masonec44a352008-04-28 15:29:52 -04001186
1187 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1188 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1189 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1190 (flags & BTRFS_BLOCK_GROUP_DUP)))
1191 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1192 return flags;
1193}
1194
Chris Mason6324fbf2008-03-24 15:01:59 -04001195static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1196 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001197 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001198{
1199 struct btrfs_space_info *space_info;
1200 u64 thresh;
1201 u64 start;
1202 u64 num_bytes;
1203 int ret;
1204
Chris Masona061fc82008-05-07 11:43:44 -04001205 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001206
Chris Mason6324fbf2008-03-24 15:01:59 -04001207 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001208 if (!space_info) {
1209 ret = update_space_info(extent_root->fs_info, flags,
1210 0, 0, &space_info);
1211 BUG_ON(ret);
1212 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001213 BUG_ON(!space_info);
1214
Chris Mason0ef3e662008-05-24 14:04:53 -04001215 if (space_info->force_alloc) {
1216 force = 1;
1217 space_info->force_alloc = 0;
1218 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001219 if (space_info->full)
Chris Mason925baed2008-06-25 16:01:30 -04001220 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001221
Chris Mason8790d502008-04-03 16:29:03 -04001222 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001223 if (!force &&
1224 (space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
Chris Mason6324fbf2008-03-24 15:01:59 -04001225 thresh)
Chris Mason925baed2008-06-25 16:01:30 -04001226 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001227
Chris Mason925baed2008-06-25 16:01:30 -04001228 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001229 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1230 if (ret == -ENOSPC) {
1231printk("space info full %Lu\n", flags);
1232 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001233 goto out_unlock;
Chris Mason6324fbf2008-03-24 15:01:59 -04001234 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001235 BUG_ON(ret);
1236
1237 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001238 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001239 BUG_ON(ret);
Chris Masona74a4b92008-06-25 16:01:31 -04001240out_unlock:
Chris Mason333db942008-06-25 16:01:30 -04001241 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001242out:
Chris Mason6324fbf2008-03-24 15:01:59 -04001243 return 0;
1244}
1245
Chris Mason9078a3e2007-04-26 16:46:15 -04001246static int update_block_group(struct btrfs_trans_handle *trans,
1247 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001248 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001249 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001250{
1251 struct btrfs_block_group_cache *cache;
1252 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001253 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001254 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001255 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -04001256 u64 start;
1257 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -04001258
Chris Mason7d9eb122008-07-08 14:19:17 -04001259 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason9078a3e2007-04-26 16:46:15 -04001260 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001261 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001262 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001263 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001264 }
Chris Masondb945352007-10-15 16:15:53 -04001265 byte_in_group = bytenr - cache->key.objectid;
1266 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -04001267 start = cache->key.objectid;
1268 end = start + cache->key.offset - 1;
1269 set_extent_bits(&info->block_group_cache, start, end,
1270 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001271
Chris Masonc286ac42008-07-22 23:06:41 -04001272 spin_lock(&cache->lock);
Chris Mason9078a3e2007-04-26 16:46:15 -04001273 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001274 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001275 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001276 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001277 cache->space_info->bytes_used += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001278 btrfs_set_block_group_used(&cache->item, old_val);
1279 spin_unlock(&cache->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001280 } else {
Chris Masondb945352007-10-15 16:15:53 -04001281 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001282 cache->space_info->bytes_used -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001283 btrfs_set_block_group_used(&cache->item, old_val);
1284 spin_unlock(&cache->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001285 if (mark_free) {
1286 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -04001287 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -04001288 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001289 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001290 }
Chris Masondb945352007-10-15 16:15:53 -04001291 total -= num_bytes;
1292 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001293 }
1294 return 0;
1295}
Chris Mason6324fbf2008-03-24 15:01:59 -04001296
Chris Masona061fc82008-05-07 11:43:44 -04001297static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1298{
1299 u64 start;
1300 u64 end;
1301 int ret;
1302 ret = find_first_extent_bit(&root->fs_info->block_group_cache,
1303 search_start, &start, &end,
1304 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
1305 BLOCK_GROUP_SYSTEM);
1306 if (ret)
1307 return 0;
1308 return start;
1309}
1310
1311
Yan324ae4d2007-11-16 14:57:08 -05001312static int update_pinned_extents(struct btrfs_root *root,
1313 u64 bytenr, u64 num, int pin)
1314{
1315 u64 len;
1316 struct btrfs_block_group_cache *cache;
1317 struct btrfs_fs_info *fs_info = root->fs_info;
1318
Chris Mason7d9eb122008-07-08 14:19:17 -04001319 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Yan324ae4d2007-11-16 14:57:08 -05001320 if (pin) {
1321 set_extent_dirty(&fs_info->pinned_extents,
1322 bytenr, bytenr + num - 1, GFP_NOFS);
1323 } else {
1324 clear_extent_dirty(&fs_info->pinned_extents,
1325 bytenr, bytenr + num - 1, GFP_NOFS);
1326 }
1327 while (num > 0) {
1328 cache = btrfs_lookup_block_group(fs_info, bytenr);
Chris Masona061fc82008-05-07 11:43:44 -04001329 if (!cache) {
1330 u64 first = first_logical_byte(root, bytenr);
1331 WARN_ON(first < bytenr);
1332 len = min(first - bytenr, num);
1333 } else {
1334 len = min(num, cache->key.offset -
1335 (bytenr - cache->key.objectid));
1336 }
Yan324ae4d2007-11-16 14:57:08 -05001337 if (pin) {
Chris Masona061fc82008-05-07 11:43:44 -04001338 if (cache) {
Chris Masonc286ac42008-07-22 23:06:41 -04001339 spin_lock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001340 cache->pinned += len;
1341 cache->space_info->bytes_pinned += len;
Chris Masonc286ac42008-07-22 23:06:41 -04001342 spin_unlock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001343 }
Yan324ae4d2007-11-16 14:57:08 -05001344 fs_info->total_pinned += len;
1345 } else {
Chris Masona061fc82008-05-07 11:43:44 -04001346 if (cache) {
Chris Masonc286ac42008-07-22 23:06:41 -04001347 spin_lock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001348 cache->pinned -= len;
1349 cache->space_info->bytes_pinned -= len;
Chris Masonc286ac42008-07-22 23:06:41 -04001350 spin_unlock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001351 }
Yan324ae4d2007-11-16 14:57:08 -05001352 fs_info->total_pinned -= len;
1353 }
1354 bytenr += len;
1355 num -= len;
1356 }
1357 return 0;
1358}
Chris Mason9078a3e2007-04-26 16:46:15 -04001359
Chris Masond1310b22008-01-24 16:13:08 -05001360int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001361{
Chris Masonccd467d2007-06-28 15:57:36 -04001362 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001363 u64 start;
1364 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001365 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001366 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001367
1368 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001369 ret = find_first_extent_bit(pinned_extents, last,
1370 &start, &end, EXTENT_DIRTY);
1371 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001372 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001373 set_extent_dirty(copy, start, end, GFP_NOFS);
1374 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001375 }
1376 return 0;
1377}
1378
1379int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1380 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001381 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001382{
Chris Mason1a5bc162007-10-15 16:15:26 -04001383 u64 start;
1384 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001385 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001386 struct extent_io_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -04001387 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -05001388
Chris Mason925baed2008-06-25 16:01:30 -04001389 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001390 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001391 ret = find_first_extent_bit(unpin, 0, &start, &end,
1392 EXTENT_DIRTY);
1393 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001394 break;
Yan324ae4d2007-11-16 14:57:08 -05001395 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001396 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1397 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04001398 if (need_resched()) {
1399 mutex_unlock(&root->fs_info->alloc_mutex);
1400 cond_resched();
1401 mutex_lock(&root->fs_info->alloc_mutex);
1402 }
Chris Masona28ec192007-03-06 20:08:01 -05001403 }
Chris Mason925baed2008-06-25 16:01:30 -04001404 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001405 return 0;
1406}
1407
Chris Mason98ed5172008-01-03 10:01:48 -05001408static int finish_current_insert(struct btrfs_trans_handle *trans,
1409 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001410{
Chris Mason7bb86312007-12-11 09:25:06 -05001411 u64 start;
1412 u64 end;
1413 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001414 struct extent_buffer *eb;
Chris Mason7bb86312007-12-11 09:25:06 -05001415 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001416 struct btrfs_key ins;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001417 struct btrfs_disk_key first;
Chris Mason234b63a2007-03-13 10:46:10 -04001418 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001419 int ret;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001420 int level;
Chris Mason1a5bc162007-10-15 16:15:26 -04001421 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001422
Chris Mason7d9eb122008-07-08 14:19:17 -04001423 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Chris Mason5f39d392007-10-15 16:14:19 -04001424 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -04001425 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason7bb86312007-12-11 09:25:06 -05001426 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001427
Chris Mason26b80032007-08-08 20:17:12 -04001428 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001429 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1430 &end, EXTENT_LOCKED);
1431 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001432 break;
1433
Chris Mason1a5bc162007-10-15 16:15:26 -04001434 ins.objectid = start;
1435 ins.offset = end + 1 - start;
1436 err = btrfs_insert_item(trans, extent_root, &ins,
1437 &extent_item, sizeof(extent_item));
1438 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1439 GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04001440
1441 eb = btrfs_find_tree_block(extent_root, ins.objectid,
1442 ins.offset);
1443
1444 if (!btrfs_buffer_uptodate(eb, trans->transid)) {
1445 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1446 btrfs_read_buffer(eb, trans->transid);
1447 mutex_lock(&extent_root->fs_info->alloc_mutex);
1448 }
1449
Chris Mason925baed2008-06-25 16:01:30 -04001450 btrfs_tree_lock(eb);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001451 level = btrfs_header_level(eb);
1452 if (level == 0) {
1453 btrfs_item_key(eb, &first, 0);
1454 } else {
1455 btrfs_node_key(eb, &first, 0);
1456 }
Chris Mason925baed2008-06-25 16:01:30 -04001457 btrfs_tree_unlock(eb);
1458 free_extent_buffer(eb);
1459 /*
1460 * the first key is just a hint, so the race we've created
1461 * against reading it is fine
1462 */
Chris Mason7bb86312007-12-11 09:25:06 -05001463 err = btrfs_insert_extent_backref(trans, extent_root, path,
1464 start, extent_root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -05001465 0, level,
1466 btrfs_disk_key_objectid(&first));
Chris Mason7bb86312007-12-11 09:25:06 -05001467 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001468 if (need_resched()) {
1469 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1470 cond_resched();
1471 mutex_lock(&extent_root->fs_info->alloc_mutex);
1472 }
Chris Mason037e6392007-03-07 11:50:24 -05001473 }
Chris Mason7bb86312007-12-11 09:25:06 -05001474 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001475 return 0;
1476}
1477
Chris Masondb945352007-10-15 16:15:53 -04001478static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1479 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -04001480{
Chris Mason1a5bc162007-10-15 16:15:26 -04001481 int err = 0;
Chris Mason78fae272007-03-25 11:35:08 -04001482
Chris Mason7d9eb122008-07-08 14:19:17 -04001483 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Masonf4b9aa82007-03-27 11:05:53 -04001484 if (!pending) {
Chris Mason925baed2008-06-25 16:01:30 -04001485 struct extent_buffer *buf;
Chris Masondb945352007-10-15 16:15:53 -04001486 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -04001487 if (buf) {
Chris Masona61e6f22008-07-22 11:18:08 -04001488 if (btrfs_try_tree_lock(buf) &&
Chris Mason925baed2008-06-25 16:01:30 -04001489 btrfs_buffer_uptodate(buf, 0)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001490 u64 transid =
1491 root->fs_info->running_transaction->transid;
Chris Masondc17ff82008-01-08 15:46:30 -05001492 u64 header_transid =
1493 btrfs_header_generation(buf);
Chris Mason6bc34672008-04-04 15:40:00 -04001494 if (header_transid == transid &&
1495 !btrfs_header_flag(buf,
1496 BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason55c69072008-01-09 15:55:33 -05001497 clean_tree_block(NULL, root, buf);
Chris Mason925baed2008-06-25 16:01:30 -04001498 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001499 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -05001500 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001501 }
Chris Mason925baed2008-06-25 16:01:30 -04001502 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001503 }
Chris Mason5f39d392007-10-15 16:14:19 -04001504 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -04001505 }
Yan324ae4d2007-11-16 14:57:08 -05001506 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001507 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -04001508 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -04001509 bytenr, bytenr + num_bytes - 1,
1510 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001511 }
Chris Masonbe744172007-05-06 10:15:01 -04001512 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001513 return 0;
1514}
1515
Chris Masona28ec192007-03-06 20:08:01 -05001516/*
1517 * remove an extent from the root, returns 0 on success
1518 */
Chris Masone089f052007-03-16 16:20:31 -04001519static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001520 *root, u64 bytenr, u64 num_bytes,
1521 u64 root_objectid, u64 ref_generation,
1522 u64 owner_objectid, u64 owner_offset, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -04001523 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001524{
Chris Mason5caf2a02007-04-02 11:20:42 -04001525 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001526 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001527 struct btrfs_fs_info *info = root->fs_info;
1528 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001529 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001530 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001531 int extent_slot = 0;
1532 int found_extent = 0;
1533 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001534 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001535 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001536
Chris Mason7d9eb122008-07-08 14:19:17 -04001537 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Masondb945352007-10-15 16:15:53 -04001538 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001539 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001540 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001541 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001542 if (!path)
1543 return -ENOMEM;
1544
Chris Mason3c12ac72008-04-21 12:01:38 -04001545 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05001546 ret = lookup_extent_backref(trans, extent_root, path,
1547 bytenr, root_objectid,
1548 ref_generation,
1549 owner_objectid, owner_offset, 1);
1550 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001551 struct btrfs_key found_key;
1552 extent_slot = path->slots[0];
1553 while(extent_slot > 0) {
1554 extent_slot--;
1555 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1556 extent_slot);
1557 if (found_key.objectid != bytenr)
1558 break;
1559 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1560 found_key.offset == num_bytes) {
1561 found_extent = 1;
1562 break;
1563 }
1564 if (path->slots[0] - extent_slot > 5)
1565 break;
1566 }
1567 if (!found_extent)
1568 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001569 } else {
1570 btrfs_print_leaf(extent_root, path->nodes[0]);
1571 WARN_ON(1);
1572 printk("Unable to find ref byte nr %Lu root %Lu "
1573 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1574 root_objectid, ref_generation, owner_objectid,
1575 owner_offset);
1576 }
Chris Mason952fcca2008-02-18 16:33:44 -05001577 if (!found_extent) {
1578 btrfs_release_path(extent_root, path);
1579 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1580 if (ret < 0)
1581 return ret;
1582 BUG_ON(ret);
1583 extent_slot = path->slots[0];
1584 }
Chris Mason5f39d392007-10-15 16:14:19 -04001585
1586 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001587 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001588 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001589 refs = btrfs_extent_refs(leaf, ei);
1590 BUG_ON(refs == 0);
1591 refs -= 1;
1592 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001593
Chris Mason5f39d392007-10-15 16:14:19 -04001594 btrfs_mark_buffer_dirty(leaf);
1595
Chris Mason952fcca2008-02-18 16:33:44 -05001596 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1597 /* if the back ref and the extent are next to each other
1598 * they get deleted below in one shot
1599 */
1600 path->slots[0] = extent_slot;
1601 num_to_del = 2;
1602 } else if (found_extent) {
1603 /* otherwise delete the extent back ref */
1604 ret = btrfs_del_item(trans, extent_root, path);
1605 BUG_ON(ret);
1606 /* if refs are 0, we need to setup the path for deletion */
1607 if (refs == 0) {
1608 btrfs_release_path(extent_root, path);
1609 ret = btrfs_search_slot(trans, extent_root, &key, path,
1610 -1, 1);
1611 if (ret < 0)
1612 return ret;
1613 BUG_ON(ret);
1614 }
1615 }
1616
Chris Masoncf27e1e2007-03-13 09:49:06 -04001617 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001618 u64 super_used;
1619 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04001620
1621 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -04001622 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -05001623 if (ret > 0)
1624 mark_free = 1;
1625 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001626 }
1627
Josef Bacik58176a92007-08-29 15:47:34 -04001628 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04001629 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04001630 super_used = btrfs_super_bytes_used(&info->super_copy);
1631 btrfs_set_super_bytes_used(&info->super_copy,
1632 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04001633 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04001634
1635 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001636 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001637 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001638 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001639 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1640 num_to_del);
Chris Mason54aa1f42007-06-22 14:16:25 -04001641 if (ret) {
1642 return ret;
1643 }
Chris Masondb945352007-10-15 16:15:53 -04001644 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001645 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001646 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05001647 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001648 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001649 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05001650 return ret;
1651}
1652
1653/*
Chris Masonfec577f2007-02-26 10:40:21 -05001654 * find all the blocks marked as pending in the radix tree and remove
1655 * them from the extent map
1656 */
Chris Masone089f052007-03-16 16:20:31 -04001657static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1658 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05001659{
1660 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001661 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001662 u64 start;
1663 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001664 struct extent_io_tree *pending_del;
1665 struct extent_io_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -04001666
Chris Mason7d9eb122008-07-08 14:19:17 -04001667 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Chris Mason1a5bc162007-10-15 16:15:26 -04001668 pending_del = &extent_root->fs_info->pending_del;
1669 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -05001670
1671 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001672 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1673 EXTENT_LOCKED);
1674 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05001675 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001676 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1677 GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04001678 if (!test_range_bit(&extent_root->fs_info->extent_ins,
1679 start, end, EXTENT_LOCKED, 0)) {
1680 update_pinned_extents(extent_root, start,
1681 end + 1 - start, 1);
1682 ret = __free_extent(trans, extent_root,
1683 start, end + 1 - start,
1684 extent_root->root_key.objectid,
1685 0, 0, 0, 0, 0);
1686 } else {
1687 clear_extent_bits(&extent_root->fs_info->extent_ins,
1688 start, end, EXTENT_LOCKED, GFP_NOFS);
1689 }
Chris Mason1a5bc162007-10-15 16:15:26 -04001690 if (ret)
1691 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04001692
1693 if (need_resched()) {
1694 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1695 cond_resched();
1696 mutex_lock(&extent_root->fs_info->alloc_mutex);
1697 }
Chris Masonfec577f2007-02-26 10:40:21 -05001698 }
Chris Masone20d96d2007-03-22 12:13:20 -04001699 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05001700}
1701
1702/*
1703 * remove an extent from the root, returns 0 on success
1704 */
Chris Mason925baed2008-06-25 16:01:30 -04001705static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
1706 struct btrfs_root *root, u64 bytenr,
1707 u64 num_bytes, u64 root_objectid,
1708 u64 ref_generation, u64 owner_objectid,
1709 u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05001710{
Chris Mason9f5fae22007-03-20 14:38:32 -04001711 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05001712 int pending_ret;
1713 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05001714
Chris Masondb945352007-10-15 16:15:53 -04001715 WARN_ON(num_bytes < root->sectorsize);
Chris Mason7bb86312007-12-11 09:25:06 -05001716 if (!root->ref_cows)
1717 ref_generation = 0;
1718
Chris Masona28ec192007-03-06 20:08:01 -05001719 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -04001720 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -05001721 return 0;
1722 }
Chris Mason7bb86312007-12-11 09:25:06 -05001723 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1724 ref_generation, owner_objectid, owner_offset,
1725 pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04001726
1727 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001728 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05001729 return ret ? ret : pending_ret;
1730}
1731
Chris Mason925baed2008-06-25 16:01:30 -04001732int btrfs_free_extent(struct btrfs_trans_handle *trans,
1733 struct btrfs_root *root, u64 bytenr,
1734 u64 num_bytes, u64 root_objectid,
1735 u64 ref_generation, u64 owner_objectid,
1736 u64 owner_offset, int pin)
1737{
1738 int ret;
1739
1740 maybe_lock_mutex(root);
1741 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes,
1742 root_objectid, ref_generation,
1743 owner_objectid, owner_offset, pin);
1744 maybe_unlock_mutex(root);
1745 return ret;
1746}
1747
Chris Mason87ee04e2007-11-30 11:30:34 -05001748static u64 stripe_align(struct btrfs_root *root, u64 val)
1749{
1750 u64 mask = ((u64)root->stripesize - 1);
1751 u64 ret = (val + mask) & ~mask;
1752 return ret;
1753}
1754
Chris Masonfec577f2007-02-26 10:40:21 -05001755/*
1756 * walks the btree of allocated extents and find a hole of a given size.
1757 * The key ins is changed to record the hole:
1758 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04001759 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001760 * ins->offset == number of blocks
1761 * Any available blocks before search_start are skipped.
1762 */
Chris Mason98ed5172008-01-03 10:01:48 -05001763static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1764 struct btrfs_root *orig_root,
1765 u64 num_bytes, u64 empty_size,
1766 u64 search_start, u64 search_end,
1767 u64 hint_byte, struct btrfs_key *ins,
1768 u64 exclude_start, u64 exclude_nr,
1769 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001770{
Chris Mason87ee04e2007-11-30 11:30:34 -05001771 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04001772 u64 orig_search_start;
Chris Mason9f5fae22007-03-20 14:38:32 -04001773 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001774 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001775 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001776 u64 *last_ptr = NULL;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001777 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001778 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001779 int wrapped = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001780 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001781 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04001782 int allowed_chunk_alloc = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001783
Chris Masondb945352007-10-15 16:15:53 -04001784 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001785 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1786
Chris Mason0ef3e662008-05-24 14:04:53 -04001787 if (orig_root->ref_cows || empty_size)
1788 allowed_chunk_alloc = 1;
1789
Chris Mason239b14b2008-03-24 15:02:07 -04001790 if (data & BTRFS_BLOCK_GROUP_METADATA) {
1791 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04001792 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04001793 }
1794
1795 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
1796 last_ptr = &root->fs_info->last_data_alloc;
1797 }
1798
1799 if (last_ptr) {
1800 if (*last_ptr)
1801 hint_byte = *last_ptr;
1802 else {
1803 empty_size += empty_cluster;
1804 }
1805 }
1806
Chris Masona061fc82008-05-07 11:43:44 -04001807 search_start = max(search_start, first_logical_byte(root, 0));
1808 orig_search_start = search_start;
1809
Chris Mason7f93bf82008-03-24 15:01:28 -04001810 if (search_end == (u64)-1)
1811 search_end = btrfs_super_total_bytes(&info->super_copy);
Chris Mason0b86a832008-03-24 15:01:56 -04001812
Chris Masondb945352007-10-15 16:15:53 -04001813 if (hint_byte) {
Chris Mason0ef3e662008-05-24 14:04:53 -04001814 block_group = btrfs_lookup_first_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001815 if (!block_group)
1816 hint_byte = search_start;
Chris Masonc286ac42008-07-22 23:06:41 -04001817 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -04001818 hint_byte, data, 1);
Chris Mason239b14b2008-03-24 15:02:07 -04001819 if (last_ptr && *last_ptr == 0 && block_group)
1820 hint_byte = block_group->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001821 } else {
Chris Masonc286ac42008-07-22 23:06:41 -04001822 block_group = btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001823 trans->block_group,
1824 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001825 }
Chris Mason239b14b2008-03-24 15:02:07 -04001826 search_start = max(search_start, hint_byte);
Chris Masonbe744172007-05-06 10:15:01 -04001827
Chris Mason6702ed42007-08-07 16:15:09 -04001828 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001829
Chris Masonbe744172007-05-06 10:15:01 -04001830check_failed:
Chris Mason70b043f2007-12-13 09:02:46 -05001831 if (!block_group) {
Chris Mason0ef3e662008-05-24 14:04:53 -04001832 block_group = btrfs_lookup_first_block_group(info,
1833 search_start);
Chris Mason70b043f2007-12-13 09:02:46 -05001834 if (!block_group)
Chris Mason0ef3e662008-05-24 14:04:53 -04001835 block_group = btrfs_lookup_first_block_group(info,
Chris Mason70b043f2007-12-13 09:02:46 -05001836 orig_search_start);
1837 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001838 if (full_scan && !chunk_alloc_done) {
1839 if (allowed_chunk_alloc) {
1840 do_chunk_alloc(trans, root,
1841 num_bytes + 2 * 1024 * 1024, data, 1);
1842 allowed_chunk_alloc = 0;
1843 } else if (block_group && block_group_bits(block_group, data)) {
1844 block_group->space_info->force_alloc = 1;
1845 }
1846 chunk_alloc_done = 1;
1847 }
Chris Mason0b86a832008-03-24 15:01:56 -04001848 ret = find_search_start(root, &block_group, &search_start,
1849 total_needed, data);
Chris Mason239b14b2008-03-24 15:02:07 -04001850 if (ret == -ENOSPC && last_ptr && *last_ptr) {
1851 *last_ptr = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001852 block_group = btrfs_lookup_first_block_group(info,
1853 orig_search_start);
Chris Mason239b14b2008-03-24 15:02:07 -04001854 search_start = orig_search_start;
1855 ret = find_search_start(root, &block_group, &search_start,
1856 total_needed, data);
1857 }
1858 if (ret == -ENOSPC)
1859 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04001860 if (ret)
1861 goto error;
1862
Chris Mason239b14b2008-03-24 15:02:07 -04001863 if (last_ptr && *last_ptr && search_start != *last_ptr) {
1864 *last_ptr = 0;
1865 if (!empty_size) {
1866 empty_size += empty_cluster;
1867 total_needed += empty_size;
1868 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001869 block_group = btrfs_lookup_first_block_group(info,
Chris Mason239b14b2008-03-24 15:02:07 -04001870 orig_search_start);
1871 search_start = orig_search_start;
1872 ret = find_search_start(root, &block_group,
1873 &search_start, total_needed, data);
1874 if (ret == -ENOSPC)
1875 goto enospc;
1876 if (ret)
1877 goto error;
1878 }
1879
Chris Mason87ee04e2007-11-30 11:30:34 -05001880 search_start = stripe_align(root, search_start);
Chris Masonfec577f2007-02-26 10:40:21 -05001881 ins->objectid = search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001882 ins->offset = num_bytes;
Chris Masone37c9e62007-05-09 20:13:14 -04001883
Chris Masondb945352007-10-15 16:15:53 -04001884 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001885 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04001886
1887 if (ins->objectid + num_bytes >
1888 block_group->key.objectid + block_group->key.offset) {
Chris Masone19caa52007-10-15 16:17:44 -04001889 search_start = block_group->key.objectid +
1890 block_group->key.offset;
1891 goto new_group;
1892 }
Chris Mason0b86a832008-03-24 15:01:56 -04001893
Chris Mason1a5bc162007-10-15 16:15:26 -04001894 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001895 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1896 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001897 goto new_group;
1898 }
Chris Mason0b86a832008-03-24 15:01:56 -04001899
Chris Mason1a5bc162007-10-15 16:15:26 -04001900 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001901 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1902 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001903 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001904 }
Chris Mason0b86a832008-03-24 15:01:56 -04001905
Chris Masondb945352007-10-15 16:15:53 -04001906 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001907 ins->objectid < exclude_start + exclude_nr)) {
1908 search_start = exclude_start + exclude_nr;
1909 goto new_group;
1910 }
Chris Mason0b86a832008-03-24 15:01:56 -04001911
Chris Mason6324fbf2008-03-24 15:01:59 -04001912 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
Chris Mason5276aed2007-06-11 21:33:38 -04001913 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001914 if (block_group)
1915 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001916 }
Chris Masondb945352007-10-15 16:15:53 -04001917 ins->offset = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001918 if (last_ptr) {
1919 *last_ptr = ins->objectid + ins->offset;
1920 if (*last_ptr ==
1921 btrfs_super_total_bytes(&root->fs_info->super_copy)) {
1922 *last_ptr = 0;
1923 }
1924 }
Chris Masonfec577f2007-02-26 10:40:21 -05001925 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001926
1927new_group:
Chris Masondb945352007-10-15 16:15:53 -04001928 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001929enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001930 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001931 if (full_scan) {
1932 ret = -ENOSPC;
1933 goto error;
1934 }
Chris Mason6702ed42007-08-07 16:15:09 -04001935 if (wrapped) {
1936 if (!full_scan)
1937 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001938 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001939 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001940 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001941 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001942 block_group = btrfs_lookup_first_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001943 cond_resched();
Chris Masonc286ac42008-07-22 23:06:41 -04001944 block_group = btrfs_find_block_group(root, block_group,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001945 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001946 goto check_failed;
1947
Chris Mason0f70abe2007-02-28 16:46:22 -05001948error:
Chris Mason0f70abe2007-02-28 16:46:22 -05001949 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001950}
Chris Masonec44a352008-04-28 15:29:52 -04001951
Chris Masone6dcd2d2008-07-17 12:53:50 -04001952static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
1953 struct btrfs_root *root,
1954 u64 num_bytes, u64 min_alloc_size,
1955 u64 empty_size, u64 hint_byte,
1956 u64 search_end, struct btrfs_key *ins,
1957 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05001958{
1959 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04001960 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04001961 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04001962 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04001963
Chris Mason6324fbf2008-03-24 15:01:59 -04001964 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04001965 alloc_profile = info->avail_data_alloc_bits &
1966 info->data_alloc_profile;
1967 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001968 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04001969 alloc_profile = info->avail_system_alloc_bits &
1970 info->system_alloc_profile;
1971 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001972 } else {
Chris Mason8790d502008-04-03 16:29:03 -04001973 alloc_profile = info->avail_metadata_alloc_bits &
1974 info->metadata_alloc_profile;
1975 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001976 }
Chris Mason98d20f62008-04-14 09:46:10 -04001977again:
Chris Masona061fc82008-05-07 11:43:44 -04001978 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04001979 /*
1980 * the only place that sets empty_size is btrfs_realloc_node, which
1981 * is not called recursively on allocations
1982 */
1983 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04001984 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001985 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04001986 2 * 1024 * 1024,
1987 BTRFS_BLOCK_GROUP_METADATA |
1988 (info->metadata_alloc_profile &
1989 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04001990 BUG_ON(ret);
1991 }
1992 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04001993 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04001994 BUG_ON(ret);
1995 }
Chris Mason0b86a832008-03-24 15:01:56 -04001996
Chris Masondb945352007-10-15 16:15:53 -04001997 WARN_ON(num_bytes < root->sectorsize);
1998 ret = find_free_extent(trans, root, num_bytes, empty_size,
1999 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002000 trans->alloc_exclude_start,
2001 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002002
Chris Mason98d20f62008-04-14 09:46:10 -04002003 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2004 num_bytes = num_bytes >> 1;
2005 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002006 do_chunk_alloc(trans, root->fs_info->extent_root,
2007 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002008 goto again;
2009 }
Chris Masonec44a352008-04-28 15:29:52 -04002010 if (ret) {
2011 printk("allocation failed flags %Lu\n", data);
Chris Mason925baed2008-06-25 16:01:30 -04002012 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002013 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002014 clear_extent_dirty(&root->fs_info->free_space_cache,
2015 ins->objectid, ins->objectid + ins->offset - 1,
2016 GFP_NOFS);
2017 return 0;
2018}
2019
2020int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2021 struct btrfs_root *root,
2022 u64 num_bytes, u64 min_alloc_size,
2023 u64 empty_size, u64 hint_byte,
2024 u64 search_end, struct btrfs_key *ins,
2025 u64 data)
2026{
2027 int ret;
2028 maybe_lock_mutex(root);
2029 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2030 empty_size, hint_byte, search_end, ins,
2031 data);
2032 maybe_unlock_mutex(root);
2033 return ret;
2034}
2035
2036static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2037 struct btrfs_root *root,
2038 u64 root_objectid, u64 ref_generation,
2039 u64 owner, u64 owner_offset,
2040 struct btrfs_key *ins)
2041{
2042 int ret;
2043 int pending_ret;
2044 u64 super_used;
2045 u64 root_used;
2046 u64 num_bytes = ins->offset;
2047 u32 sizes[2];
2048 struct btrfs_fs_info *info = root->fs_info;
2049 struct btrfs_root *extent_root = info->extent_root;
2050 struct btrfs_extent_item *extent_item;
2051 struct btrfs_extent_ref *ref;
2052 struct btrfs_path *path;
2053 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002054
Josef Bacik58176a92007-08-29 15:47:34 -04002055 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002056 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002057 super_used = btrfs_super_bytes_used(&info->super_copy);
2058 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002059 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04002060
Josef Bacik58176a92007-08-29 15:47:34 -04002061 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002062 root_used = btrfs_root_used(&root->root_item);
2063 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002064
Chris Mason26b80032007-08-08 20:17:12 -04002065 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002066 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2067 ins->objectid + ins->offset - 1,
2068 EXTENT_LOCKED, GFP_NOFS);
Chris Mason26b80032007-08-08 20:17:12 -04002069 goto update_block;
2070 }
2071
Chris Mason47e4bb92008-02-01 14:51:59 -05002072 memcpy(&keys[0], ins, sizeof(*ins));
2073 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
2074 owner, owner_offset);
2075 keys[1].objectid = ins->objectid;
2076 keys[1].type = BTRFS_EXTENT_REF_KEY;
2077 sizes[0] = sizeof(*extent_item);
2078 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002079
2080 path = btrfs_alloc_path();
2081 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002082
2083 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2084 sizes, 2);
Chris Mason26b80032007-08-08 20:17:12 -04002085
Chris Masonccd467d2007-06-28 15:57:36 -04002086 BUG_ON(ret);
Chris Mason47e4bb92008-02-01 14:51:59 -05002087 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2088 struct btrfs_extent_item);
2089 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2090 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2091 struct btrfs_extent_ref);
2092
2093 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2094 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2095 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2096 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
2097
2098 btrfs_mark_buffer_dirty(path->nodes[0]);
2099
2100 trans->alloc_exclude_start = 0;
2101 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002102 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002103 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002104 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04002105
Chris Mason925baed2008-06-25 16:01:30 -04002106 if (ret)
2107 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002108 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04002109 ret = pending_ret;
2110 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002111 }
Chris Mason26b80032007-08-08 20:17:12 -04002112
2113update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04002114 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002115 if (ret) {
2116 printk("update block group failed for %Lu %Lu\n",
2117 ins->objectid, ins->offset);
2118 BUG();
2119 }
Chris Mason925baed2008-06-25 16:01:30 -04002120out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04002121 return ret;
2122}
2123
2124int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2125 struct btrfs_root *root,
2126 u64 root_objectid, u64 ref_generation,
2127 u64 owner, u64 owner_offset,
2128 struct btrfs_key *ins)
2129{
2130 int ret;
2131 maybe_lock_mutex(root);
2132 ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
2133 ref_generation, owner,
2134 owner_offset, ins);
2135 maybe_unlock_mutex(root);
2136 return ret;
2137}
2138/*
2139 * finds a free extent and does all the dirty work required for allocation
2140 * returns the key for the extent through ins, and a tree buffer for
2141 * the first block of the extent through buf.
2142 *
2143 * returns 0 if everything worked, non-zero otherwise.
2144 */
2145int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2146 struct btrfs_root *root,
2147 u64 num_bytes, u64 min_alloc_size,
2148 u64 root_objectid, u64 ref_generation,
2149 u64 owner, u64 owner_offset,
2150 u64 empty_size, u64 hint_byte,
2151 u64 search_end, struct btrfs_key *ins, u64 data)
2152{
2153 int ret;
2154
2155 maybe_lock_mutex(root);
2156
2157 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2158 min_alloc_size, empty_size, hint_byte,
2159 search_end, ins, data);
2160 BUG_ON(ret);
2161 ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
2162 ref_generation, owner,
2163 owner_offset, ins);
2164 BUG_ON(ret);
2165
Chris Mason925baed2008-06-25 16:01:30 -04002166 maybe_unlock_mutex(root);
2167 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002168}
Chris Masonfec577f2007-02-26 10:40:21 -05002169/*
2170 * helper function to allocate a block for a given tree
2171 * returns the tree buffer or NULL.
2172 */
Chris Mason5f39d392007-10-15 16:14:19 -04002173struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04002174 struct btrfs_root *root,
Chris Mason7bb86312007-12-11 09:25:06 -05002175 u32 blocksize,
Chris Mason7bb86312007-12-11 09:25:06 -05002176 u64 root_objectid,
2177 u64 ref_generation,
2178 u64 first_objectid,
2179 int level,
2180 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04002181 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05002182{
Chris Masone2fa7222007-03-12 16:22:34 -04002183 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05002184 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002185 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05002186
Chris Mason98d20f62008-04-14 09:46:10 -04002187 ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
Chris Mason7bb86312007-12-11 09:25:06 -05002188 root_objectid, ref_generation,
Chris Masonf6dbff52007-12-13 11:13:32 -05002189 level, first_objectid, empty_size, hint,
Chris Masondb945352007-10-15 16:15:53 -04002190 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002191 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04002192 BUG_ON(ret > 0);
2193 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05002194 }
Chris Masondb945352007-10-15 16:15:53 -04002195 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002196 if (!buf) {
Chris Mason7bb86312007-12-11 09:25:06 -05002197 btrfs_free_extent(trans, root, ins.objectid, blocksize,
2198 root->root_key.objectid, ref_generation,
2199 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002200 return ERR_PTR(-ENOMEM);
2201 }
Chris Mason55c69072008-01-09 15:55:33 -05002202 btrfs_set_header_generation(buf, trans->transid);
Chris Mason925baed2008-06-25 16:01:30 -04002203 btrfs_tree_lock(buf);
Chris Mason55c69072008-01-09 15:55:33 -05002204 clean_tree_block(trans, root, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002205 btrfs_set_buffer_uptodate(buf);
Chris Mason55c69072008-01-09 15:55:33 -05002206
2207 if (PageDirty(buf->first_page)) {
2208 printk("page %lu dirty\n", buf->first_page->index);
2209 WARN_ON(1);
2210 }
2211
Chris Mason5f39d392007-10-15 16:14:19 -04002212 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2213 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002214 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05002215 return buf;
2216}
Chris Masona28ec192007-03-06 20:08:01 -05002217
Chris Mason98ed5172008-01-03 10:01:48 -05002218static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2219 struct btrfs_root *root,
2220 struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04002221{
Chris Mason7bb86312007-12-11 09:25:06 -05002222 u64 leaf_owner;
2223 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04002224 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002225 struct btrfs_file_extent_item *fi;
2226 int i;
2227 int nritems;
2228 int ret;
2229
Chris Mason5f39d392007-10-15 16:14:19 -04002230 BUG_ON(!btrfs_is_leaf(leaf));
2231 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05002232 leaf_owner = btrfs_header_owner(leaf);
2233 leaf_generation = btrfs_header_generation(leaf);
2234
Chris Mason4a096752008-07-21 10:29:44 -04002235 mutex_unlock(&root->fs_info->alloc_mutex);
2236
Chris Mason6407bf62007-03-27 06:33:00 -04002237 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002238 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04002239 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04002240
2241 btrfs_item_key_to_cpu(leaf, &key, i);
2242 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04002243 continue;
2244 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002245 if (btrfs_file_extent_type(leaf, fi) ==
2246 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04002247 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04002248 /*
2249 * FIXME make sure to insert a trans record that
2250 * repeats the snapshot del on crash
2251 */
Chris Masondb945352007-10-15 16:15:53 -04002252 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2253 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04002254 continue;
Chris Mason4a096752008-07-21 10:29:44 -04002255
2256 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002257 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002258 btrfs_file_extent_disk_num_bytes(leaf, fi),
2259 leaf_owner, leaf_generation,
2260 key.objectid, key.offset, 0);
Chris Mason4a096752008-07-21 10:29:44 -04002261 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason6407bf62007-03-27 06:33:00 -04002262 BUG_ON(ret);
2263 }
Chris Mason4a096752008-07-21 10:29:44 -04002264
2265 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason6407bf62007-03-27 06:33:00 -04002266 return 0;
2267}
2268
Chris Mason98ed5172008-01-03 10:01:48 -05002269static void noinline reada_walk_down(struct btrfs_root *root,
Chris Masonbea495e2008-01-24 16:13:14 -05002270 struct extent_buffer *node,
2271 int slot)
Chris Masone0115992007-06-19 16:23:05 -04002272{
Chris Masondb945352007-10-15 16:15:53 -04002273 u64 bytenr;
Chris Masonbea495e2008-01-24 16:13:14 -05002274 u64 last = 0;
2275 u32 nritems;
Chris Masone0115992007-06-19 16:23:05 -04002276 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04002277 u32 blocksize;
Chris Masonbea495e2008-01-24 16:13:14 -05002278 int ret;
2279 int i;
2280 int level;
2281 int skipped = 0;
Chris Masone0115992007-06-19 16:23:05 -04002282
Chris Mason5f39d392007-10-15 16:14:19 -04002283 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04002284 level = btrfs_header_level(node);
Chris Masonbea495e2008-01-24 16:13:14 -05002285 if (level)
2286 return;
2287
2288 for (i = slot; i < nritems && skipped < 32; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002289 bytenr = btrfs_node_blockptr(node, i);
Chris Masonbea495e2008-01-24 16:13:14 -05002290 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2291 (last > bytenr && last - bytenr > 32 * 1024))) {
2292 skipped++;
Chris Masone0115992007-06-19 16:23:05 -04002293 continue;
Chris Masonbea495e2008-01-24 16:13:14 -05002294 }
2295 blocksize = btrfs_level_size(root, level - 1);
2296 if (i != slot) {
2297 ret = lookup_extent_ref(NULL, root, bytenr,
2298 blocksize, &refs);
2299 BUG_ON(ret);
2300 if (refs != 1) {
2301 skipped++;
2302 continue;
2303 }
2304 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002305 ret = readahead_tree_block(root, bytenr, blocksize,
2306 btrfs_node_ptr_generation(node, i));
Chris Masonbea495e2008-01-24 16:13:14 -05002307 last = bytenr + blocksize;
Chris Mason409eb952007-08-08 20:17:12 -04002308 cond_resched();
Chris Masone0115992007-06-19 16:23:05 -04002309 if (ret)
2310 break;
2311 }
2312}
2313
Chris Mason9aca1d52007-03-13 11:09:37 -04002314/*
Chris Mason333db942008-06-25 16:01:30 -04002315 * we want to avoid as much random IO as we can with the alloc mutex
2316 * held, so drop the lock and do the lookup, then do it again with the
2317 * lock held.
2318 */
2319int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2320 u32 *refs)
2321{
2322 mutex_unlock(&root->fs_info->alloc_mutex);
2323 lookup_extent_ref(NULL, root, start, len, refs);
Chris Masone7a84562008-06-25 16:01:31 -04002324 cond_resched();
Chris Mason333db942008-06-25 16:01:30 -04002325 mutex_lock(&root->fs_info->alloc_mutex);
2326 return lookup_extent_ref(NULL, root, start, len, refs);
2327}
2328
2329/*
Chris Mason9aca1d52007-03-13 11:09:37 -04002330 * helper function for drop_snapshot, this walks down the tree dropping ref
2331 * counts as it goes.
2332 */
Chris Mason98ed5172008-01-03 10:01:48 -05002333static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2334 struct btrfs_root *root,
2335 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002336{
Chris Mason7bb86312007-12-11 09:25:06 -05002337 u64 root_owner;
2338 u64 root_gen;
2339 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04002340 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002341 struct extent_buffer *next;
2342 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002343 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04002344 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002345 int ret;
2346 u32 refs;
2347
Chris Mason925baed2008-06-25 16:01:30 -04002348 mutex_lock(&root->fs_info->alloc_mutex);
2349
Chris Mason5caf2a02007-04-02 11:20:42 -04002350 WARN_ON(*level < 0);
2351 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04002352 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04002353 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002354 BUG_ON(ret);
2355 if (refs > 1)
2356 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002357
Chris Mason9aca1d52007-03-13 11:09:37 -04002358 /*
2359 * walk down to the last node level and free all the leaves
2360 */
Chris Mason6407bf62007-03-27 06:33:00 -04002361 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002362 WARN_ON(*level < 0);
2363 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002364 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002365
Chris Mason5f39d392007-10-15 16:14:19 -04002366 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002367 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002368
Chris Mason7518a232007-03-12 12:01:18 -04002369 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002370 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002371 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002372 if (*level == 0) {
2373 ret = drop_leaf_ref(trans, root, cur);
2374 BUG_ON(ret);
2375 break;
2376 }
Chris Masondb945352007-10-15 16:15:53 -04002377 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04002378 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04002379 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002380
Chris Mason333db942008-06-25 16:01:30 -04002381 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002382 BUG_ON(ret);
2383 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002384 parent = path->nodes[*level];
2385 root_owner = btrfs_header_owner(parent);
2386 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002387 path->slots[*level]++;
Chris Mason925baed2008-06-25 16:01:30 -04002388 ret = __btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002389 blocksize, root_owner,
2390 root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002391 BUG_ON(ret);
2392 continue;
2393 }
Chris Masondb945352007-10-15 16:15:53 -04002394 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04002395 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002396 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04002397 mutex_unlock(&root->fs_info->alloc_mutex);
2398
Chris Masona74a4b92008-06-25 16:01:31 -04002399 if (path->slots[*level] == 0)
2400 reada_walk_down(root, cur, path->slots[*level]);
Chris Masone9d0b132007-08-10 14:06:19 -04002401
Chris Masonca7a79a2008-05-12 12:59:19 -04002402 next = read_tree_block(root, bytenr, blocksize,
2403 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04002404 cond_resched();
Chris Mason925baed2008-06-25 16:01:30 -04002405 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8790d502008-04-03 16:29:03 -04002406
2407 /* we've dropped the lock, double check */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002408 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2409 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002410 BUG_ON(ret);
2411 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002412 parent = path->nodes[*level];
2413 root_owner = btrfs_header_owner(parent);
2414 root_gen = btrfs_header_generation(parent);
2415
Chris Masone9d0b132007-08-10 14:06:19 -04002416 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04002417 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04002418 ret = __btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002419 blocksize,
2420 root_owner,
2421 root_gen, 0, 0, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04002422 BUG_ON(ret);
2423 continue;
2424 }
2425 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002426 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002427 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002428 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002429 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002430 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002431 path->slots[*level] = 0;
2432 }
2433out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002434 WARN_ON(*level < 0);
2435 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002436
2437 if (path->nodes[*level] == root->node) {
2438 root_owner = root->root_key.objectid;
2439 parent = path->nodes[*level];
2440 } else {
2441 parent = path->nodes[*level + 1];
2442 root_owner = btrfs_header_owner(parent);
2443 }
2444
2445 root_gen = btrfs_header_generation(parent);
Chris Mason925baed2008-06-25 16:01:30 -04002446 ret = __btrfs_free_extent(trans, root, path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002447 path->nodes[*level]->len,
2448 root_owner, root_gen, 0, 0, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002449 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05002450 path->nodes[*level] = NULL;
2451 *level += 1;
2452 BUG_ON(ret);
Chris Mason925baed2008-06-25 16:01:30 -04002453 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masone7a84562008-06-25 16:01:31 -04002454 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05002455 return 0;
2456}
2457
Chris Mason9aca1d52007-03-13 11:09:37 -04002458/*
2459 * helper for dropping snapshots. This walks back up the tree in the path
2460 * to find the first node higher up where we haven't yet gone through
2461 * all the slots
2462 */
Chris Mason98ed5172008-01-03 10:01:48 -05002463static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2464 struct btrfs_root *root,
2465 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002466{
Chris Mason7bb86312007-12-11 09:25:06 -05002467 u64 root_owner;
2468 u64 root_gen;
2469 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002470 int i;
2471 int slot;
2472 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04002473
Chris Mason234b63a2007-03-13 10:46:10 -04002474 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05002475 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04002476 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2477 struct extent_buffer *node;
2478 struct btrfs_disk_key disk_key;
2479 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05002480 path->slots[i]++;
2481 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04002482 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002483 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04002484 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04002485 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04002486 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05002487 return 0;
2488 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002489 if (path->nodes[*level] == root->node) {
2490 root_owner = root->root_key.objectid;
2491 root_gen =
2492 btrfs_header_generation(path->nodes[*level]);
2493 } else {
2494 struct extent_buffer *node;
2495 node = path->nodes[*level + 1];
2496 root_owner = btrfs_header_owner(node);
2497 root_gen = btrfs_header_generation(node);
2498 }
Chris Masone089f052007-03-16 16:20:31 -04002499 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002500 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002501 path->nodes[*level]->len,
2502 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04002503 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002504 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04002505 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05002506 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05002507 }
2508 }
2509 return 1;
2510}
2511
Chris Mason9aca1d52007-03-13 11:09:37 -04002512/*
2513 * drop the reference count on the tree rooted at 'snap'. This traverses
2514 * the tree freeing any blocks that have a ref count of zero after being
2515 * decremented.
2516 */
Chris Masone089f052007-03-16 16:20:31 -04002517int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04002518 *root)
Chris Mason20524f02007-03-10 06:35:47 -05002519{
Chris Mason3768f362007-03-13 16:47:54 -04002520 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04002521 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05002522 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04002523 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05002524 int i;
2525 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002526 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002527
Chris Masona2135012008-06-25 16:01:30 -04002528 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04002529 path = btrfs_alloc_path();
2530 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05002531
Chris Mason5f39d392007-10-15 16:14:19 -04002532 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05002533 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002534 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2535 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04002536 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04002537 path->slots[level] = 0;
2538 } else {
2539 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002540 struct btrfs_disk_key found_key;
2541 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04002542
Chris Mason9f3a7422007-08-07 15:52:19 -04002543 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04002544 level = root_item->drop_level;
2545 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002546 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04002547 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04002548 ret = wret;
2549 goto out;
2550 }
Chris Mason5f39d392007-10-15 16:14:19 -04002551 node = path->nodes[level];
2552 btrfs_node_key(node, &found_key, path->slots[level]);
2553 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2554 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04002555 /*
2556 * unlock our path, this is safe because only this
2557 * function is allowed to delete this snapshot
2558 */
Chris Mason925baed2008-06-25 16:01:30 -04002559 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
2560 if (path->nodes[i] && path->locks[i]) {
2561 path->locks[i] = 0;
2562 btrfs_tree_unlock(path->nodes[i]);
2563 }
2564 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002565 }
Chris Mason20524f02007-03-10 06:35:47 -05002566 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002567 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002568 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002569 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002570 if (wret < 0)
2571 ret = wret;
2572
Chris Mason5caf2a02007-04-02 11:20:42 -04002573 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002574 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002575 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002576 if (wret < 0)
2577 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04002578 if (trans->transaction->in_commit) {
2579 ret = -EAGAIN;
2580 break;
2581 }
Chris Mason20524f02007-03-10 06:35:47 -05002582 }
Chris Mason83e15a22007-03-12 09:03:27 -04002583 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002584 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04002585 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04002586 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04002587 }
Chris Mason20524f02007-03-10 06:35:47 -05002588 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002589out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002590 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04002591 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05002592}
Chris Mason9078a3e2007-04-26 16:46:15 -04002593
Chris Masonbe744172007-05-06 10:15:01 -04002594int btrfs_free_block_groups(struct btrfs_fs_info *info)
2595{
Chris Masonf510cfe2007-10-15 16:14:48 -04002596 u64 start;
2597 u64 end;
Yanb97f9202007-11-01 11:28:41 -04002598 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04002599 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04002600
2601 mutex_lock(&info->alloc_mutex);
Chris Mason96b51792007-10-15 16:15:19 -04002602 while(1) {
2603 ret = find_first_extent_bit(&info->block_group_cache, 0,
2604 &start, &end, (unsigned int)-1);
2605 if (ret)
2606 break;
Yanb97f9202007-11-01 11:28:41 -04002607 ret = get_state_private(&info->block_group_cache, start, &ptr);
2608 if (!ret)
2609 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04002610 clear_extent_bits(&info->block_group_cache, start,
2611 end, (unsigned int)-1, GFP_NOFS);
2612 }
Chris Masone37c9e62007-05-09 20:13:14 -04002613 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04002614 ret = find_first_extent_bit(&info->free_space_cache, 0,
2615 &start, &end, EXTENT_DIRTY);
2616 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04002617 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04002618 clear_extent_dirty(&info->free_space_cache, start,
2619 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04002620 }
Chris Mason925baed2008-06-25 16:01:30 -04002621 mutex_unlock(&info->alloc_mutex);
Chris Masonbe744172007-05-06 10:15:01 -04002622 return 0;
2623}
2624
Chris Mason8e7bf942008-04-28 09:02:36 -04002625static unsigned long calc_ra(unsigned long start, unsigned long last,
2626 unsigned long nr)
2627{
2628 return min(last, start + nr - 1);
2629}
2630
Chris Mason98ed5172008-01-03 10:01:48 -05002631static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2632 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05002633{
2634 u64 page_start;
2635 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002636 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05002637 unsigned long i;
2638 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05002639 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05002640 struct file_ra_state *ra;
Chris Mason8e7bf942008-04-28 09:02:36 -04002641 unsigned long total_read = 0;
2642 unsigned long ra_pages;
Chris Mason3eaa2882008-07-24 11:57:52 -04002643 struct btrfs_ordered_extent *ordered;
Chris Masona061fc82008-05-07 11:43:44 -04002644 struct btrfs_trans_handle *trans;
Chris Mason4313b392008-01-03 09:08:48 -05002645
2646 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002647
2648 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05002649 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05002650 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2651
Chris Mason8e7bf942008-04-28 09:02:36 -04002652 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
2653
Chris Mason4313b392008-01-03 09:08:48 -05002654 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05002655
Chris Mason4313b392008-01-03 09:08:48 -05002656 for (; i <= last_index; i++) {
Chris Mason8e7bf942008-04-28 09:02:36 -04002657 if (total_read % ra_pages == 0) {
2658 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
2659 calc_ra(i, last_index, ra_pages));
2660 }
2661 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04002662again:
2663 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Chris Masona061fc82008-05-07 11:43:44 -04002664 goto truncate_racing;
Chris Masonedbd8d42007-12-21 16:27:24 -05002665 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04002666 if (!page) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002667 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04002668 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002669 if (!PageUptodate(page)) {
2670 btrfs_readpage(NULL, page);
2671 lock_page(page);
2672 if (!PageUptodate(page)) {
2673 unlock_page(page);
2674 page_cache_release(page);
2675 goto out_unlock;
2676 }
2677 }
Chris Masonec44a352008-04-28 15:29:52 -04002678 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04002679
Chris Masonedbd8d42007-12-21 16:27:24 -05002680 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2681 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002682 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002683
Chris Mason3eaa2882008-07-24 11:57:52 -04002684 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2685 if (ordered) {
2686 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2687 unlock_page(page);
2688 page_cache_release(page);
2689 btrfs_start_ordered_extent(inode, ordered, 1);
2690 btrfs_put_ordered_extent(ordered);
2691 goto again;
2692 }
2693 set_page_extent_mapped(page);
2694
2695
Chris Masond1310b22008-01-24 16:13:08 -05002696 set_extent_delalloc(io_tree, page_start,
Chris Masonedbd8d42007-12-21 16:27:24 -05002697 page_end, GFP_NOFS);
Chris Masona061fc82008-05-07 11:43:44 -04002698 set_page_dirty(page);
Chris Masonedbd8d42007-12-21 16:27:24 -05002699
Chris Masond1310b22008-01-24 16:13:08 -05002700 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002701 unlock_page(page);
2702 page_cache_release(page);
2703 }
2704
2705out_unlock:
Chris Mason3eaa2882008-07-24 11:57:52 -04002706 /* we have to start the IO in order to get the ordered extents
2707 * instantiated. This allows the relocation to code to wait
2708 * for all the ordered extents to hit the disk.
2709 *
2710 * Otherwise, it would constantly loop over the same extents
2711 * because the old ones don't get deleted until the IO is
2712 * started
2713 */
2714 btrfs_fdatawrite_range(inode->i_mapping, start, start + len - 1,
2715 WB_SYNC_NONE);
Chris Masonec44a352008-04-28 15:29:52 -04002716 kfree(ra);
Chris Masona061fc82008-05-07 11:43:44 -04002717 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
2718 if (trans) {
Chris Masona061fc82008-05-07 11:43:44 -04002719 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
2720 mark_inode_dirty(inode);
2721 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002722 mutex_unlock(&inode->i_mutex);
2723 return 0;
Chris Masona061fc82008-05-07 11:43:44 -04002724
2725truncate_racing:
2726 vmtruncate(inode, inode->i_size);
2727 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2728 total_read);
2729 goto out_unlock;
Chris Masonedbd8d42007-12-21 16:27:24 -05002730}
2731
Chris Mason4313b392008-01-03 09:08:48 -05002732/*
Chris Masonbf4ef672008-05-08 13:26:18 -04002733 * The back references tell us which tree holds a ref on a block,
2734 * but it is possible for the tree root field in the reference to
2735 * reflect the original root before a snapshot was made. In this
2736 * case we should search through all the children of a given root
2737 * to find potential holders of references on a block.
2738 *
2739 * Instead, we do something a little less fancy and just search
2740 * all the roots for a given key/block combination.
2741 */
2742static int find_root_for_ref(struct btrfs_root *root,
2743 struct btrfs_path *path,
2744 struct btrfs_key *key0,
2745 int level,
2746 int file_key,
2747 struct btrfs_root **found_root,
2748 u64 bytenr)
2749{
2750 struct btrfs_key root_location;
2751 struct btrfs_root *cur_root = *found_root;
2752 struct btrfs_file_extent_item *file_extent;
2753 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
2754 u64 found_bytenr;
2755 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04002756
2757 root_location.offset = (u64)-1;
2758 root_location.type = BTRFS_ROOT_ITEM_KEY;
2759 path->lowest_level = level;
2760 path->reada = 0;
2761 while(1) {
2762 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
2763 found_bytenr = 0;
2764 if (ret == 0 && file_key) {
2765 struct extent_buffer *leaf = path->nodes[0];
2766 file_extent = btrfs_item_ptr(leaf, path->slots[0],
2767 struct btrfs_file_extent_item);
2768 if (btrfs_file_extent_type(leaf, file_extent) ==
2769 BTRFS_FILE_EXTENT_REG) {
2770 found_bytenr =
2771 btrfs_file_extent_disk_bytenr(leaf,
2772 file_extent);
2773 }
Chris Mason323da792008-05-09 11:46:48 -04002774 } else if (!file_key) {
Chris Masonbf4ef672008-05-08 13:26:18 -04002775 if (path->nodes[level])
2776 found_bytenr = path->nodes[level]->start;
2777 }
2778
Chris Masonbf4ef672008-05-08 13:26:18 -04002779 btrfs_release_path(cur_root, path);
2780
2781 if (found_bytenr == bytenr) {
2782 *found_root = cur_root;
2783 ret = 0;
2784 goto out;
2785 }
2786 ret = btrfs_search_root(root->fs_info->tree_root,
2787 root_search_start, &root_search_start);
2788 if (ret)
2789 break;
2790
2791 root_location.objectid = root_search_start;
2792 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
2793 &root_location);
2794 if (!cur_root) {
2795 ret = 1;
2796 break;
2797 }
2798 }
2799out:
2800 path->lowest_level = 0;
2801 return ret;
2802}
2803
2804/*
Chris Mason4313b392008-01-03 09:08:48 -05002805 * note, this releases the path
2806 */
Chris Mason98ed5172008-01-03 10:01:48 -05002807static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05002808 struct btrfs_path *path,
Chris Mason0ef3e662008-05-24 14:04:53 -04002809 struct btrfs_key *extent_key,
2810 u64 *last_file_objectid,
2811 u64 *last_file_offset,
2812 u64 *last_file_root,
2813 u64 last_extent)
Chris Masonedbd8d42007-12-21 16:27:24 -05002814{
2815 struct inode *inode;
2816 struct btrfs_root *found_root;
Chris Masonbf4ef672008-05-08 13:26:18 -04002817 struct btrfs_key root_location;
2818 struct btrfs_key found_key;
Chris Mason4313b392008-01-03 09:08:48 -05002819 struct btrfs_extent_ref *ref;
2820 u64 ref_root;
2821 u64 ref_gen;
2822 u64 ref_objectid;
2823 u64 ref_offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05002824 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04002825 int level;
Chris Masonedbd8d42007-12-21 16:27:24 -05002826
Chris Mason7d9eb122008-07-08 14:19:17 -04002827 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
2828
Chris Mason4313b392008-01-03 09:08:48 -05002829 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2830 struct btrfs_extent_ref);
2831 ref_root = btrfs_ref_root(path->nodes[0], ref);
2832 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2833 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2834 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2835 btrfs_release_path(extent_root, path);
2836
Chris Masonbf4ef672008-05-08 13:26:18 -04002837 root_location.objectid = ref_root;
Chris Masonedbd8d42007-12-21 16:27:24 -05002838 if (ref_gen == 0)
Chris Masonbf4ef672008-05-08 13:26:18 -04002839 root_location.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002840 else
Chris Masonbf4ef672008-05-08 13:26:18 -04002841 root_location.offset = (u64)-1;
2842 root_location.type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05002843
2844 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Masonbf4ef672008-05-08 13:26:18 -04002845 &root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002846 BUG_ON(!found_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04002847 mutex_unlock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05002848
2849 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Chris Masonbf4ef672008-05-08 13:26:18 -04002850 found_key.objectid = ref_objectid;
2851 found_key.type = BTRFS_EXTENT_DATA_KEY;
2852 found_key.offset = ref_offset;
2853 level = 0;
2854
Chris Mason0ef3e662008-05-24 14:04:53 -04002855 if (last_extent == extent_key->objectid &&
2856 *last_file_objectid == ref_objectid &&
2857 *last_file_offset == ref_offset &&
2858 *last_file_root == ref_root)
2859 goto out;
2860
Chris Masonbf4ef672008-05-08 13:26:18 -04002861 ret = find_root_for_ref(extent_root, path, &found_key,
2862 level, 1, &found_root,
2863 extent_key->objectid);
2864
2865 if (ret)
2866 goto out;
2867
Chris Mason0ef3e662008-05-24 14:04:53 -04002868 if (last_extent == extent_key->objectid &&
2869 *last_file_objectid == ref_objectid &&
2870 *last_file_offset == ref_offset &&
2871 *last_file_root == ref_root)
2872 goto out;
2873
Chris Masonedbd8d42007-12-21 16:27:24 -05002874 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2875 ref_objectid, found_root);
2876 if (inode->i_state & I_NEW) {
2877 /* the inode and parent dir are two different roots */
2878 BTRFS_I(inode)->root = found_root;
2879 BTRFS_I(inode)->location.objectid = ref_objectid;
2880 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2881 BTRFS_I(inode)->location.offset = 0;
2882 btrfs_read_locked_inode(inode);
2883 unlock_new_inode(inode);
2884
2885 }
2886 /* this can happen if the reference is not against
2887 * the latest version of the tree root
2888 */
Chris Mason7d9eb122008-07-08 14:19:17 -04002889 if (is_bad_inode(inode))
Chris Masonedbd8d42007-12-21 16:27:24 -05002890 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04002891
Chris Mason0ef3e662008-05-24 14:04:53 -04002892 *last_file_objectid = inode->i_ino;
2893 *last_file_root = found_root->root_key.objectid;
2894 *last_file_offset = ref_offset;
2895
Chris Masonedbd8d42007-12-21 16:27:24 -05002896 relocate_inode_pages(inode, ref_offset, extent_key->offset);
Chris Masonedbd8d42007-12-21 16:27:24 -05002897 iput(inode);
Chris Masonedbd8d42007-12-21 16:27:24 -05002898 } else {
2899 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05002900 struct extent_buffer *eb;
Chris Mason7d9eb122008-07-08 14:19:17 -04002901 int needs_lock = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002902
Chris Masonedbd8d42007-12-21 16:27:24 -05002903 eb = read_tree_block(found_root, extent_key->objectid,
Chris Masonca7a79a2008-05-12 12:59:19 -04002904 extent_key->offset, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002905 btrfs_tree_lock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05002906 level = btrfs_header_level(eb);
2907
2908 if (level == 0)
2909 btrfs_item_key_to_cpu(eb, &found_key, 0);
2910 else
2911 btrfs_node_key_to_cpu(eb, &found_key, 0);
2912
Chris Mason925baed2008-06-25 16:01:30 -04002913 btrfs_tree_unlock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05002914 free_extent_buffer(eb);
2915
Chris Masonbf4ef672008-05-08 13:26:18 -04002916 ret = find_root_for_ref(extent_root, path, &found_key,
2917 level, 0, &found_root,
2918 extent_key->objectid);
2919
2920 if (ret)
2921 goto out;
2922
Chris Mason7d9eb122008-07-08 14:19:17 -04002923 /*
2924 * right here almost anything could happen to our key,
2925 * but that's ok. The cow below will either relocate it
2926 * or someone else will have relocated it. Either way,
2927 * it is in a different spot than it was before and
2928 * we're happy.
2929 */
2930
Chris Masonbf4ef672008-05-08 13:26:18 -04002931 trans = btrfs_start_transaction(found_root, 1);
2932
Chris Mason7d9eb122008-07-08 14:19:17 -04002933 if (found_root == extent_root->fs_info->extent_root ||
2934 found_root == extent_root->fs_info->chunk_root ||
2935 found_root == extent_root->fs_info->dev_root) {
2936 needs_lock = 1;
2937 mutex_lock(&extent_root->fs_info->alloc_mutex);
2938 }
2939
Chris Masonedbd8d42007-12-21 16:27:24 -05002940 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05002941 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002942 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2943 0, 1);
2944 path->lowest_level = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002945 btrfs_release_path(found_root, path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002946
Chris Mason0ef3e662008-05-24 14:04:53 -04002947 if (found_root == found_root->fs_info->extent_root)
2948 btrfs_extent_post_op(trans, found_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04002949 if (needs_lock)
2950 mutex_unlock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05002951
Chris Mason7d9eb122008-07-08 14:19:17 -04002952 btrfs_end_transaction(trans, found_root);
2953
2954 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002955out:
Chris Mason7d9eb122008-07-08 14:19:17 -04002956 mutex_lock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05002957 return 0;
2958}
2959
Chris Masona061fc82008-05-07 11:43:44 -04002960static int noinline del_extent_zero(struct btrfs_root *extent_root,
2961 struct btrfs_path *path,
2962 struct btrfs_key *extent_key)
2963{
2964 int ret;
2965 struct btrfs_trans_handle *trans;
2966
2967 trans = btrfs_start_transaction(extent_root, 1);
2968 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
2969 if (ret > 0) {
2970 ret = -EIO;
2971 goto out;
2972 }
2973 if (ret < 0)
2974 goto out;
2975 ret = btrfs_del_item(trans, extent_root, path);
2976out:
2977 btrfs_end_transaction(trans, extent_root);
2978 return ret;
2979}
2980
Chris Mason98ed5172008-01-03 10:01:48 -05002981static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2982 struct btrfs_path *path,
2983 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002984{
2985 struct btrfs_key key;
2986 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002987 struct extent_buffer *leaf;
Chris Mason0ef3e662008-05-24 14:04:53 -04002988 u64 last_file_objectid = 0;
2989 u64 last_file_root = 0;
2990 u64 last_file_offset = (u64)-1;
2991 u64 last_extent = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002992 u32 nritems;
2993 u32 item_size;
2994 int ret = 0;
2995
Chris Masona061fc82008-05-07 11:43:44 -04002996 if (extent_key->objectid == 0) {
2997 ret = del_extent_zero(extent_root, path, extent_key);
2998 goto out;
2999 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003000 key.objectid = extent_key->objectid;
3001 key.type = BTRFS_EXTENT_REF_KEY;
3002 key.offset = 0;
3003
3004 while(1) {
3005 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3006
Chris Masonedbd8d42007-12-21 16:27:24 -05003007 if (ret < 0)
3008 goto out;
3009
3010 ret = 0;
3011 leaf = path->nodes[0];
3012 nritems = btrfs_header_nritems(leaf);
Chris Masona061fc82008-05-07 11:43:44 -04003013 if (path->slots[0] == nritems) {
3014 ret = btrfs_next_leaf(extent_root, path);
3015 if (ret > 0) {
3016 ret = 0;
3017 goto out;
3018 }
3019 if (ret < 0)
3020 goto out;
Chris Masonbf4ef672008-05-08 13:26:18 -04003021 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04003022 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003023
3024 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masona061fc82008-05-07 11:43:44 -04003025 if (found_key.objectid != extent_key->objectid) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003026 break;
Chris Masona061fc82008-05-07 11:43:44 -04003027 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003028
Chris Masona061fc82008-05-07 11:43:44 -04003029 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003030 break;
Chris Masona061fc82008-05-07 11:43:44 -04003031 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003032
3033 key.offset = found_key.offset + 1;
3034 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3035
Chris Mason0ef3e662008-05-24 14:04:53 -04003036 ret = relocate_one_reference(extent_root, path, extent_key,
3037 &last_file_objectid,
3038 &last_file_offset,
3039 &last_file_root, last_extent);
Chris Masonedbd8d42007-12-21 16:27:24 -05003040 if (ret)
3041 goto out;
Chris Mason0ef3e662008-05-24 14:04:53 -04003042 last_extent = extent_key->objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05003043 }
3044 ret = 0;
3045out:
3046 btrfs_release_path(extent_root, path);
3047 return ret;
3048}
3049
Chris Masonec44a352008-04-28 15:29:52 -04003050static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
3051{
3052 u64 num_devices;
3053 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
3054 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
3055
Chris Masona061fc82008-05-07 11:43:44 -04003056 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04003057 if (num_devices == 1) {
3058 stripped |= BTRFS_BLOCK_GROUP_DUP;
3059 stripped = flags & ~stripped;
3060
3061 /* turn raid0 into single device chunks */
3062 if (flags & BTRFS_BLOCK_GROUP_RAID0)
3063 return stripped;
3064
3065 /* turn mirroring into duplication */
3066 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
3067 BTRFS_BLOCK_GROUP_RAID10))
3068 return stripped | BTRFS_BLOCK_GROUP_DUP;
3069 return flags;
3070 } else {
3071 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04003072 if (flags & stripped)
3073 return flags;
3074
3075 stripped |= BTRFS_BLOCK_GROUP_DUP;
3076 stripped = flags & ~stripped;
3077
3078 /* switch duplicated blocks with raid1 */
3079 if (flags & BTRFS_BLOCK_GROUP_DUP)
3080 return stripped | BTRFS_BLOCK_GROUP_RAID1;
3081
3082 /* turn single device chunks into raid0 */
3083 return stripped | BTRFS_BLOCK_GROUP_RAID0;
3084 }
3085 return flags;
3086}
3087
Chris Mason0ef3e662008-05-24 14:04:53 -04003088int __alloc_chunk_for_shrink(struct btrfs_root *root,
3089 struct btrfs_block_group_cache *shrink_block_group,
3090 int force)
3091{
3092 struct btrfs_trans_handle *trans;
3093 u64 new_alloc_flags;
3094 u64 calc;
3095
Chris Masonc286ac42008-07-22 23:06:41 -04003096 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04003097 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04003098 spin_unlock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04003099 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003100
Chris Mason0ef3e662008-05-24 14:04:53 -04003101 trans = btrfs_start_transaction(root, 1);
Chris Mason7d9eb122008-07-08 14:19:17 -04003102 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003103 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04003104
Chris Mason0ef3e662008-05-24 14:04:53 -04003105 new_alloc_flags = update_block_group_flags(root,
3106 shrink_block_group->flags);
3107 if (new_alloc_flags != shrink_block_group->flags) {
3108 calc =
3109 btrfs_block_group_used(&shrink_block_group->item);
3110 } else {
3111 calc = shrink_block_group->key.offset;
3112 }
Chris Masonc286ac42008-07-22 23:06:41 -04003113 spin_unlock(&shrink_block_group->lock);
3114
Chris Mason0ef3e662008-05-24 14:04:53 -04003115 do_chunk_alloc(trans, root->fs_info->extent_root,
3116 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04003117
3118 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0ef3e662008-05-24 14:04:53 -04003119 btrfs_end_transaction(trans, root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003120 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003121 } else
3122 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04003123 return 0;
3124}
3125
Chris Mason8f18cf12008-04-25 16:53:30 -04003126int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05003127{
3128 struct btrfs_trans_handle *trans;
3129 struct btrfs_root *tree_root = root->fs_info->tree_root;
3130 struct btrfs_path *path;
3131 u64 cur_byte;
3132 u64 total_found;
Chris Mason8f18cf12008-04-25 16:53:30 -04003133 u64 shrink_last_byte;
3134 struct btrfs_block_group_cache *shrink_block_group;
Chris Masonedbd8d42007-12-21 16:27:24 -05003135 struct btrfs_fs_info *info = root->fs_info;
Chris Masonedbd8d42007-12-21 16:27:24 -05003136 struct btrfs_key key;
Yan73e48b22008-01-03 14:14:39 -05003137 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05003138 struct extent_buffer *leaf;
3139 u32 nritems;
3140 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04003141 int progress;
Chris Masonedbd8d42007-12-21 16:27:24 -05003142
Chris Mason925baed2008-06-25 16:01:30 -04003143 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04003144 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
3145 shrink_start);
3146 BUG_ON(!shrink_block_group);
3147
Chris Mason0ef3e662008-05-24 14:04:53 -04003148 shrink_last_byte = shrink_block_group->key.objectid +
3149 shrink_block_group->key.offset;
Chris Mason8f18cf12008-04-25 16:53:30 -04003150
3151 shrink_block_group->space_info->total_bytes -=
3152 shrink_block_group->key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05003153 path = btrfs_alloc_path();
3154 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05003155 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05003156
Chris Mason323da792008-05-09 11:46:48 -04003157 printk("btrfs relocating block group %llu flags %llu\n",
3158 (unsigned long long)shrink_start,
3159 (unsigned long long)shrink_block_group->flags);
3160
Chris Mason0ef3e662008-05-24 14:04:53 -04003161 __alloc_chunk_for_shrink(root, shrink_block_group, 1);
Chris Mason323da792008-05-09 11:46:48 -04003162
Chris Mason0ef3e662008-05-24 14:04:53 -04003163again:
3164
Chris Mason8f18cf12008-04-25 16:53:30 -04003165 shrink_block_group->ro = 1;
3166
Chris Masonedbd8d42007-12-21 16:27:24 -05003167 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04003168 progress = 0;
Chris Mason8f18cf12008-04-25 16:53:30 -04003169 key.objectid = shrink_start;
Chris Masonedbd8d42007-12-21 16:27:24 -05003170 key.offset = 0;
3171 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05003172 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05003173
Yan73e48b22008-01-03 14:14:39 -05003174 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3175 if (ret < 0)
3176 goto out;
3177
Chris Mason0b86a832008-03-24 15:01:56 -04003178 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yan73e48b22008-01-03 14:14:39 -05003179 if (ret < 0)
3180 goto out;
Chris Mason8f18cf12008-04-25 16:53:30 -04003181
Yan73e48b22008-01-03 14:14:39 -05003182 if (ret == 0) {
3183 leaf = path->nodes[0];
3184 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003185 if (found_key.objectid + found_key.offset > shrink_start &&
3186 found_key.objectid < shrink_last_byte) {
Yan73e48b22008-01-03 14:14:39 -05003187 cur_byte = found_key.objectid;
3188 key.objectid = cur_byte;
3189 }
3190 }
3191 btrfs_release_path(root, path);
3192
3193 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003194 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3195 if (ret < 0)
3196 goto out;
Yan73e48b22008-01-03 14:14:39 -05003197
Chris Mason7d9eb122008-07-08 14:19:17 -04003198next:
Chris Masonedbd8d42007-12-21 16:27:24 -05003199 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05003200 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05003201 if (path->slots[0] >= nritems) {
3202 ret = btrfs_next_leaf(root, path);
3203 if (ret < 0)
3204 goto out;
3205 if (ret == 1) {
3206 ret = 0;
3207 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05003208 }
Yan73e48b22008-01-03 14:14:39 -05003209 leaf = path->nodes[0];
3210 nritems = btrfs_header_nritems(leaf);
3211 }
3212
3213 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05003214
Chris Mason8f18cf12008-04-25 16:53:30 -04003215 if (found_key.objectid >= shrink_last_byte)
3216 break;
3217
Chris Mason725c8462008-01-04 16:47:16 -05003218 if (progress && need_resched()) {
3219 memcpy(&key, &found_key, sizeof(key));
Chris Mason725c8462008-01-04 16:47:16 -05003220 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05003221 btrfs_release_path(root, path);
3222 btrfs_search_slot(NULL, root, &key, path, 0, 0);
3223 progress = 0;
3224 goto next;
3225 }
3226 progress = 1;
3227
Yan73e48b22008-01-03 14:14:39 -05003228 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
3229 found_key.objectid + found_key.offset <= cur_byte) {
Chris Mason0ef3e662008-05-24 14:04:53 -04003230 memcpy(&key, &found_key, sizeof(key));
3231 key.offset++;
Yan73e48b22008-01-03 14:14:39 -05003232 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003233 goto next;
3234 }
Yan73e48b22008-01-03 14:14:39 -05003235
Chris Masonedbd8d42007-12-21 16:27:24 -05003236 total_found++;
3237 cur_byte = found_key.objectid + found_key.offset;
3238 key.objectid = cur_byte;
3239 btrfs_release_path(root, path);
3240 ret = relocate_one_extent(root, path, &found_key);
Chris Mason0ef3e662008-05-24 14:04:53 -04003241 __alloc_chunk_for_shrink(root, shrink_block_group, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003242 }
3243
3244 btrfs_release_path(root, path);
3245
3246 if (total_found > 0) {
Chris Mason323da792008-05-09 11:46:48 -04003247 printk("btrfs relocate found %llu last extent was %llu\n",
3248 (unsigned long long)total_found,
3249 (unsigned long long)found_key.objectid);
Chris Mason7d9eb122008-07-08 14:19:17 -04003250 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003251 trans = btrfs_start_transaction(tree_root, 1);
3252 btrfs_commit_transaction(trans, tree_root);
3253
Chris Masonedbd8d42007-12-21 16:27:24 -05003254 btrfs_clean_old_snapshots(tree_root);
Chris Masonedbd8d42007-12-21 16:27:24 -05003255
Chris Mason3eaa2882008-07-24 11:57:52 -04003256 btrfs_wait_ordered_extents(tree_root);
3257
Chris Masonedbd8d42007-12-21 16:27:24 -05003258 trans = btrfs_start_transaction(tree_root, 1);
3259 btrfs_commit_transaction(trans, tree_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003260 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003261 goto again;
3262 }
3263
Chris Mason8f18cf12008-04-25 16:53:30 -04003264 /*
3265 * we've freed all the extents, now remove the block
3266 * group item from the tree
3267 */
Chris Mason7d9eb122008-07-08 14:19:17 -04003268 mutex_unlock(&root->fs_info->alloc_mutex);
3269
Chris Masonedbd8d42007-12-21 16:27:24 -05003270 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04003271
Chris Mason7d9eb122008-07-08 14:19:17 -04003272 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04003273 memcpy(&key, &shrink_block_group->key, sizeof(key));
Chris Mason4313b392008-01-03 09:08:48 -05003274
Chris Mason8f18cf12008-04-25 16:53:30 -04003275 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3276 if (ret > 0)
3277 ret = -EIO;
Josef Bacik8e8a1e32008-07-24 12:17:14 -04003278 if (ret < 0) {
3279 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003280 goto out;
Josef Bacik8e8a1e32008-07-24 12:17:14 -04003281 }
Yan73e48b22008-01-03 14:14:39 -05003282
Chris Mason0ef3e662008-05-24 14:04:53 -04003283 clear_extent_bits(&info->block_group_cache, key.objectid,
3284 key.objectid + key.offset - 1,
Chris Mason8f18cf12008-04-25 16:53:30 -04003285 (unsigned int)-1, GFP_NOFS);
Yan73e48b22008-01-03 14:14:39 -05003286
Chris Mason0ef3e662008-05-24 14:04:53 -04003287
3288 clear_extent_bits(&info->free_space_cache,
3289 key.objectid, key.objectid + key.offset - 1,
3290 (unsigned int)-1, GFP_NOFS);
3291
3292 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
3293 kfree(shrink_block_group);
3294
Chris Mason8f18cf12008-04-25 16:53:30 -04003295 btrfs_del_item(trans, root, path);
Chris Mason7d9eb122008-07-08 14:19:17 -04003296 btrfs_release_path(root, path);
3297 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003298 btrfs_commit_transaction(trans, root);
Chris Mason0ef3e662008-05-24 14:04:53 -04003299
Chris Mason7d9eb122008-07-08 14:19:17 -04003300 mutex_lock(&root->fs_info->alloc_mutex);
3301
Chris Mason0ef3e662008-05-24 14:04:53 -04003302 /* the code to unpin extents might set a few bits in the free
3303 * space cache for this range again
3304 */
3305 clear_extent_bits(&info->free_space_cache,
3306 key.objectid, key.objectid + key.offset - 1,
3307 (unsigned int)-1, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003308out:
3309 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003310 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003311 return ret;
3312}
3313
Chris Mason0b86a832008-03-24 15:01:56 -04003314int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3315 struct btrfs_key *key)
3316{
Chris Mason925baed2008-06-25 16:01:30 -04003317 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003318 struct btrfs_key found_key;
3319 struct extent_buffer *leaf;
3320 int slot;
3321
3322 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3323 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003324 goto out;
3325
Chris Mason0b86a832008-03-24 15:01:56 -04003326 while(1) {
3327 slot = path->slots[0];
3328 leaf = path->nodes[0];
3329 if (slot >= btrfs_header_nritems(leaf)) {
3330 ret = btrfs_next_leaf(root, path);
3331 if (ret == 0)
3332 continue;
3333 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003334 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04003335 break;
3336 }
3337 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3338
3339 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04003340 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3341 ret = 0;
3342 goto out;
3343 }
Chris Mason0b86a832008-03-24 15:01:56 -04003344 path->slots[0]++;
3345 }
3346 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04003347out:
Chris Mason0b86a832008-03-24 15:01:56 -04003348 return ret;
3349}
3350
Chris Mason9078a3e2007-04-26 16:46:15 -04003351int btrfs_read_block_groups(struct btrfs_root *root)
3352{
3353 struct btrfs_path *path;
3354 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04003355 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04003356 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04003357 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003358 struct btrfs_space_info *space_info;
Chris Masond1310b22008-01-24 16:13:08 -05003359 struct extent_io_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003360 struct btrfs_key key;
3361 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04003362 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04003363
3364 block_group_cache = &info->block_group_cache;
Chris Masonbe744172007-05-06 10:15:01 -04003365 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04003366 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003367 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003368 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04003369 path = btrfs_alloc_path();
3370 if (!path)
3371 return -ENOMEM;
3372
Chris Mason925baed2008-06-25 16:01:30 -04003373 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04003374 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003375 ret = find_first_block_group(root, path, &key);
3376 if (ret > 0) {
3377 ret = 0;
3378 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04003379 }
Chris Mason0b86a832008-03-24 15:01:56 -04003380 if (ret != 0)
3381 goto error;
3382
Chris Mason5f39d392007-10-15 16:14:19 -04003383 leaf = path->nodes[0];
3384 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003385 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04003386 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04003387 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04003388 break;
3389 }
Chris Mason3e1ad542007-05-07 20:03:49 -04003390
Chris Masonc286ac42008-07-22 23:06:41 -04003391 spin_lock_init(&cache->lock);
Chris Mason5f39d392007-10-15 16:14:19 -04003392 read_extent_buffer(leaf, &cache->item,
3393 btrfs_item_ptr_offset(leaf, path->slots[0]),
3394 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04003395 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04003396
Chris Mason9078a3e2007-04-26 16:46:15 -04003397 key.objectid = found_key.objectid + found_key.offset;
3398 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04003399 cache->flags = btrfs_block_group_flags(&cache->item);
3400 bit = 0;
3401 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04003402 bit = BLOCK_GROUP_DATA;
Chris Mason0b86a832008-03-24 15:01:56 -04003403 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3404 bit = BLOCK_GROUP_SYSTEM;
3405 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason96b51792007-10-15 16:15:19 -04003406 bit = BLOCK_GROUP_METADATA;
Chris Mason31f3c992007-04-30 15:25:45 -04003407 }
Chris Mason8790d502008-04-03 16:29:03 -04003408 set_avail_alloc_bits(info, cache->flags);
Chris Mason96b51792007-10-15 16:15:19 -04003409
Chris Mason6324fbf2008-03-24 15:01:59 -04003410 ret = update_space_info(info, cache->flags, found_key.offset,
3411 btrfs_block_group_used(&cache->item),
3412 &space_info);
3413 BUG_ON(ret);
3414 cache->space_info = space_info;
3415
Chris Mason96b51792007-10-15 16:15:19 -04003416 /* use EXTENT_LOCKED to prevent merging */
3417 set_extent_bits(block_group_cache, found_key.objectid,
3418 found_key.objectid + found_key.offset - 1,
Chris Masonc286ac42008-07-22 23:06:41 -04003419 EXTENT_LOCKED, GFP_NOFS);
Chris Mason96b51792007-10-15 16:15:19 -04003420 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04003421 (unsigned long)cache);
Chris Masonc286ac42008-07-22 23:06:41 -04003422 set_extent_bits(block_group_cache, found_key.objectid,
3423 found_key.objectid + found_key.offset - 1,
3424 bit | EXTENT_LOCKED, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04003425 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04003426 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04003427 break;
3428 }
Chris Mason0b86a832008-03-24 15:01:56 -04003429 ret = 0;
3430error:
Chris Mason9078a3e2007-04-26 16:46:15 -04003431 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003432 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04003433 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003434}
Chris Mason6324fbf2008-03-24 15:01:59 -04003435
3436int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3437 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04003438 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04003439 u64 size)
3440{
3441 int ret;
3442 int bit = 0;
3443 struct btrfs_root *extent_root;
3444 struct btrfs_block_group_cache *cache;
3445 struct extent_io_tree *block_group_cache;
3446
Chris Mason7d9eb122008-07-08 14:19:17 -04003447 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason6324fbf2008-03-24 15:01:59 -04003448 extent_root = root->fs_info->extent_root;
3449 block_group_cache = &root->fs_info->block_group_cache;
3450
Chris Mason8f18cf12008-04-25 16:53:30 -04003451 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04003452 BUG_ON(!cache);
Chris Masone17cade2008-04-15 15:41:47 -04003453 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04003454 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04003455 spin_lock_init(&cache->lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04003456 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04003457
Chris Mason6324fbf2008-03-24 15:01:59 -04003458 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04003459 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3460 cache->flags = type;
3461 btrfs_set_block_group_flags(&cache->item, type);
3462
3463 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
3464 &cache->space_info);
3465 BUG_ON(ret);
3466
Chris Masond18a2c42008-04-04 15:40:00 -04003467 bit = block_group_state_bits(type);
Chris Masone17cade2008-04-15 15:41:47 -04003468 set_extent_bits(block_group_cache, chunk_offset,
3469 chunk_offset + size - 1,
Chris Masonc286ac42008-07-22 23:06:41 -04003470 EXTENT_LOCKED, GFP_NOFS);
Chris Masone17cade2008-04-15 15:41:47 -04003471 set_state_private(block_group_cache, chunk_offset,
3472 (unsigned long)cache);
Chris Masonc286ac42008-07-22 23:06:41 -04003473 set_extent_bits(block_group_cache, chunk_offset,
3474 chunk_offset + size - 1,
3475 bit | EXTENT_LOCKED, GFP_NOFS);
3476
Chris Mason6324fbf2008-03-24 15:01:59 -04003477 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3478 sizeof(cache->item));
3479 BUG_ON(ret);
3480
3481 finish_current_insert(trans, extent_root);
3482 ret = del_pending_extents(trans, extent_root);
3483 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04003484 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04003485
Chris Mason6324fbf2008-03-24 15:01:59 -04003486 return 0;
3487}