blob: 5258923d621f0b3030deba2cf9a5451d38711d3c [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>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Mason74493f72007-12-11 09:25:06 -050022#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040023#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050024#include "ctree.h"
25#include "disk-io.h"
26#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040027#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040028#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040029#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040030#include "ref-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050031
Zheng Yan31840ae2008-09-23 13:14:14 -040032#define PENDING_EXTENT_INSERT 0
33#define PENDING_EXTENT_DELETE 1
34#define PENDING_BACKREF_UPDATE 2
35
36struct pending_extent_op {
37 int type;
38 u64 bytenr;
39 u64 num_bytes;
40 u64 parent;
41 u64 orig_parent;
42 u64 generation;
43 u64 orig_generation;
44 int level;
45};
46
Chris Masone089f052007-03-16 16:20:31 -040047static int finish_current_insert(struct btrfs_trans_handle *trans, struct
48 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040049static int del_pending_extents(struct btrfs_trans_handle *trans, struct
50 btrfs_root *extent_root);
Chris Mason925baed2008-06-25 16:01:30 -040051static struct btrfs_block_group_cache *
52__btrfs_find_block_group(struct btrfs_root *root,
53 struct btrfs_block_group_cache *hint,
54 u64 search_start, int data, int owner);
Yand548ee52008-01-03 13:56:30 -050055
Chris Mason925baed2008-06-25 16:01:30 -040056void maybe_lock_mutex(struct btrfs_root *root)
57{
58 if (root != root->fs_info->extent_root &&
59 root != root->fs_info->chunk_root &&
60 root != root->fs_info->dev_root) {
61 mutex_lock(&root->fs_info->alloc_mutex);
62 }
63}
64
65void maybe_unlock_mutex(struct btrfs_root *root)
66{
67 if (root != root->fs_info->extent_root &&
68 root != root->fs_info->chunk_root &&
69 root != root->fs_info->dev_root) {
70 mutex_unlock(&root->fs_info->alloc_mutex);
71 }
72}
Chris Masonfec577f2007-02-26 10:40:21 -050073
Josef Bacik0f9dd462008-09-23 13:14:11 -040074static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
75{
76 return (cache->flags & bits) == bits;
77}
78
79/*
80 * this adds the block group to the fs_info rb tree for the block group
81 * cache
82 */
83int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
84 struct btrfs_block_group_cache *block_group)
85{
86 struct rb_node **p;
87 struct rb_node *parent = NULL;
88 struct btrfs_block_group_cache *cache;
89
90 spin_lock(&info->block_group_cache_lock);
91 p = &info->block_group_cache_tree.rb_node;
92
93 while (*p) {
94 parent = *p;
95 cache = rb_entry(parent, struct btrfs_block_group_cache,
96 cache_node);
97 if (block_group->key.objectid < cache->key.objectid) {
98 p = &(*p)->rb_left;
99 } else if (block_group->key.objectid > cache->key.objectid) {
100 p = &(*p)->rb_right;
101 } else {
102 spin_unlock(&info->block_group_cache_lock);
103 return -EEXIST;
104 }
105 }
106
107 rb_link_node(&block_group->cache_node, parent, p);
108 rb_insert_color(&block_group->cache_node,
109 &info->block_group_cache_tree);
110 spin_unlock(&info->block_group_cache_lock);
111
112 return 0;
113}
114
115/*
116 * This will return the block group at or after bytenr if contains is 0, else
117 * it will return the block group that contains the bytenr
118 */
119static struct btrfs_block_group_cache *
120block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
121 int contains)
122{
123 struct btrfs_block_group_cache *cache, *ret = NULL;
124 struct rb_node *n;
125 u64 end, start;
126
127 spin_lock(&info->block_group_cache_lock);
128 n = info->block_group_cache_tree.rb_node;
129
130 while (n) {
131 cache = rb_entry(n, struct btrfs_block_group_cache,
132 cache_node);
133 end = cache->key.objectid + cache->key.offset - 1;
134 start = cache->key.objectid;
135
136 if (bytenr < start) {
137 if (!contains && (!ret || start < ret->key.objectid))
138 ret = cache;
139 n = n->rb_left;
140 } else if (bytenr > start) {
141 if (contains && bytenr <= end) {
142 ret = cache;
143 break;
144 }
145 n = n->rb_right;
146 } else {
147 ret = cache;
148 break;
149 }
150 }
151 spin_unlock(&info->block_group_cache_lock);
152
153 return ret;
154}
155
156/*
157 * this is only called by cache_block_group, since we could have freed extents
158 * we need to check the pinned_extents for any extents that can't be used yet
159 * since their free space will be released as soon as the transaction commits.
160 */
161static int add_new_free_space(struct btrfs_block_group_cache *block_group,
162 struct btrfs_fs_info *info, u64 start, u64 end)
163{
164 u64 extent_start, extent_end, size;
165 int ret;
166
167 while (start < end) {
168 ret = find_first_extent_bit(&info->pinned_extents, start,
169 &extent_start, &extent_end,
170 EXTENT_DIRTY);
171 if (ret)
172 break;
173
174 if (extent_start == start) {
175 start = extent_end + 1;
176 } else if (extent_start > start && extent_start < end) {
177 size = extent_start - start;
178 ret = btrfs_add_free_space(block_group, start, size);
179 BUG_ON(ret);
180 start = extent_end + 1;
181 } else {
182 break;
183 }
184 }
185
186 if (start < end) {
187 size = end - start;
188 ret = btrfs_add_free_space(block_group, start, size);
189 BUG_ON(ret);
190 }
191
192 return 0;
193}
194
Chris Masone37c9e62007-05-09 20:13:14 -0400195static int cache_block_group(struct btrfs_root *root,
196 struct btrfs_block_group_cache *block_group)
197{
198 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400199 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400200 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400201 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400202 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -0400203 u64 last = 0;
Yan7d7d6062007-09-14 16:15:28 -0400204 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -0400205 int found = 0;
206
Chris Mason00f5c792007-11-30 10:09:33 -0500207 if (!block_group)
208 return 0;
209
Chris Masone37c9e62007-05-09 20:13:14 -0400210 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400211
212 if (block_group->cached)
213 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400214
Chris Masone37c9e62007-05-09 20:13:14 -0400215 path = btrfs_alloc_path();
216 if (!path)
217 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400218
Chris Mason2cc58cf2007-08-27 16:49:44 -0400219 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400220 /*
221 * we get into deadlocks with paths held by callers of this function.
222 * since the alloc_mutex is protecting things right now, just
223 * skip the locking here
224 */
225 path->skip_locking = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400226 first_free = max_t(u64, block_group->key.objectid,
227 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Masone37c9e62007-05-09 20:13:14 -0400228 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -0400229 key.offset = 0;
230 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
231 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
232 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400233 goto err;
Chris Mason0b86a832008-03-24 15:01:56 -0400234 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -0500235 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400236 goto err;
Yand548ee52008-01-03 13:56:30 -0500237 if (ret == 0) {
238 leaf = path->nodes[0];
239 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
240 if (key.objectid + key.offset > first_free)
241 first_free = key.objectid + key.offset;
242 }
Chris Masone37c9e62007-05-09 20:13:14 -0400243 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400244 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400245 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400246 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400247 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400248 if (ret < 0)
249 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400250 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400251 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400252 else
Chris Masone37c9e62007-05-09 20:13:14 -0400253 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400254 }
Chris Mason5f39d392007-10-15 16:14:19 -0400255 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400256 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400257 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400258
Chris Masone37c9e62007-05-09 20:13:14 -0400259 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400260 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400261 break;
Yan7d7d6062007-09-14 16:15:28 -0400262
263 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
264 if (!found) {
265 last = first_free;
266 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400267 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400268
269 add_new_free_space(block_group, root->fs_info, last,
270 key.objectid);
271
Yan7d7d6062007-09-14 16:15:28 -0400272 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400273 }
Yan7d7d6062007-09-14 16:15:28 -0400274next:
Chris Masone37c9e62007-05-09 20:13:14 -0400275 path->slots[0]++;
276 }
277
Yan7d7d6062007-09-14 16:15:28 -0400278 if (!found)
279 last = first_free;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400280
281 add_new_free_space(block_group, root->fs_info, last,
282 block_group->key.objectid +
283 block_group->key.offset);
284
Chris Masone37c9e62007-05-09 20:13:14 -0400285 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400286 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400287err:
Chris Masone37c9e62007-05-09 20:13:14 -0400288 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400289 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400290}
291
Josef Bacik0f9dd462008-09-23 13:14:11 -0400292/*
293 * return the block group that starts at or after bytenr
294 */
Chris Mason0ef3e662008-05-24 14:04:53 -0400295struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
296 btrfs_fs_info *info,
297 u64 bytenr)
298{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400299 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400300
Josef Bacik0f9dd462008-09-23 13:14:11 -0400301 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400302
Josef Bacik0f9dd462008-09-23 13:14:11 -0400303 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400304}
305
Josef Bacik0f9dd462008-09-23 13:14:11 -0400306/*
307 * return the block group that contains teh given bytenr
308 */
Chris Mason5276aed2007-06-11 21:33:38 -0400309struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
310 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400311 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400312{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400313 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400314
Josef Bacik0f9dd462008-09-23 13:14:11 -0400315 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400316
Josef Bacik0f9dd462008-09-23 13:14:11 -0400317 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400318}
Chris Mason0b86a832008-03-24 15:01:56 -0400319
Josef Bacik0f9dd462008-09-23 13:14:11 -0400320static int noinline find_free_space(struct btrfs_root *root,
321 struct btrfs_block_group_cache **cache_ret,
322 u64 *start_ret, u64 num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400323{
Chris Masone37c9e62007-05-09 20:13:14 -0400324 int ret;
325 struct btrfs_block_group_cache *cache = *cache_ret;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400326 struct btrfs_free_space *info = NULL;
Chris Masone19caa52007-10-15 16:17:44 -0400327 u64 last;
Chris Masonc31f8832008-01-08 15:46:31 -0500328 u64 total_fs_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -0400329 u64 search_start = *start_ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400330
Chris Mason7d9eb122008-07-08 14:19:17 -0400331 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Masonc31f8832008-01-08 15:46:31 -0500332 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masond7fc6402008-02-18 12:12:38 -0500333
Chris Mason0ef3e662008-05-24 14:04:53 -0400334 if (!cache)
Chris Mason54aa1f42007-06-22 14:16:25 -0400335 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500336
Josef Bacik0f9dd462008-09-23 13:14:11 -0400337 last = max(search_start, cache->key.objectid);
338
Chris Mason0ef3e662008-05-24 14:04:53 -0400339again:
340 ret = cache_block_group(root, cache);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400341 if (ret)
Chris Mason0ef3e662008-05-24 14:04:53 -0400342 goto out;
Chris Masone19caa52007-10-15 16:17:44 -0400343
Josef Bacik0f9dd462008-09-23 13:14:11 -0400344 if (cache->ro || !block_group_bits(cache, data))
Chris Mason0ef3e662008-05-24 14:04:53 -0400345 goto new_group;
346
Josef Bacik0f9dd462008-09-23 13:14:11 -0400347 info = btrfs_find_free_space(cache, last, num);
348 if (info) {
349 *start_ret = info->offset;
Chris Mason0b86a832008-03-24 15:01:56 -0400350 return 0;
Chris Mason8790d502008-04-03 16:29:03 -0400351 }
Chris Masone37c9e62007-05-09 20:13:14 -0400352
353new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400354 last = cache->key.objectid + cache->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400355
Chris Mason0ef3e662008-05-24 14:04:53 -0400356 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400357 if (!cache || cache->key.objectid >= total_fs_bytes)
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500358 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400359
Chris Masone37c9e62007-05-09 20:13:14 -0400360 *cache_ret = cache;
361 goto again;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400362
363out:
364 return -ENOSPC;
Chris Masone37c9e62007-05-09 20:13:14 -0400365}
366
Chris Mason84f54cf2007-06-12 07:43:08 -0400367static u64 div_factor(u64 num, int factor)
368{
Chris Mason257d0ce2007-11-07 21:08:16 -0500369 if (factor == 10)
370 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400371 num *= factor;
372 do_div(num, 10);
373 return num;
374}
375
Josef Bacik0f9dd462008-09-23 13:14:11 -0400376static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
377 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400378{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400379 struct list_head *head = &info->space_info;
380 struct list_head *cur;
381 struct btrfs_space_info *found;
382 list_for_each(cur, head) {
383 found = list_entry(cur, struct btrfs_space_info, list);
384 if (found->flags == flags)
385 return found;
386 }
387 return NULL;
388
Chris Mason6324fbf2008-03-24 15:01:59 -0400389}
390
Chris Mason925baed2008-06-25 16:01:30 -0400391static struct btrfs_block_group_cache *
392__btrfs_find_block_group(struct btrfs_root *root,
393 struct btrfs_block_group_cache *hint,
394 u64 search_start, int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400395{
Chris Mason96b51792007-10-15 16:15:19 -0400396 struct btrfs_block_group_cache *cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400397 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400398 struct btrfs_fs_info *info = root->fs_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400399 struct btrfs_space_info *sinfo;
Chris Masoncd1bc462007-04-27 10:08:34 -0400400 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400401 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400402 u64 free_check;
Chris Mason31f3c992007-04-30 15:25:45 -0400403 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400404 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400405 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400406
Chris Masona236aed2008-04-29 09:38:00 -0400407 if (data & BTRFS_BLOCK_GROUP_METADATA)
408 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400409
Chris Mason0ef3e662008-05-24 14:04:53 -0400410 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400411 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400412 shint = btrfs_lookup_first_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400413 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonc286ac42008-07-22 23:06:41 -0400414 spin_lock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400415 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500416 if (used + shint->pinned <
417 div_factor(shint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400418 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400419 return shint;
420 }
Chris Masonc286ac42008-07-22 23:06:41 -0400421 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400422 }
423 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400424 if (hint && !hint->ro && block_group_bits(hint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400425 spin_lock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400426 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500427 if (used + hint->pinned <
428 div_factor(hint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400429 spin_unlock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400430 return hint;
431 }
Chris Masonc286ac42008-07-22 23:06:41 -0400432 spin_unlock(&hint->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400433 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400434 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400435 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400436 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400437 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400438 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400439 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400440 sinfo = __find_space_info(root->fs_info, data);
441 if (!sinfo)
442 goto found;
Chris Mason31f3c992007-04-30 15:25:45 -0400443again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400444 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400445 struct list_head *l;
446
447 cache = NULL;
448
449 spin_lock(&sinfo->lock);
450 list_for_each(l, &sinfo->block_groups) {
451 struct btrfs_block_group_cache *entry;
452 entry = list_entry(l, struct btrfs_block_group_cache,
453 list);
454 if ((entry->key.objectid >= last) &&
455 (!cache || (entry->key.objectid <
456 cache->key.objectid)))
457 cache = entry;
458 }
459 spin_unlock(&sinfo->lock);
460
461 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400462 break;
Chris Mason96b51792007-10-15 16:15:19 -0400463
Chris Masonc286ac42008-07-22 23:06:41 -0400464 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400465 last = cache->key.objectid + cache->key.offset;
466 used = btrfs_block_group_used(&cache->item);
467
Chris Mason8f18cf12008-04-25 16:53:30 -0400468 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400469 free_check = div_factor(cache->key.offset, factor);
Chris Mason8790d502008-04-03 16:29:03 -0400470 if (used + cache->pinned < free_check) {
471 found_group = cache;
Chris Masonc286ac42008-07-22 23:06:41 -0400472 spin_unlock(&cache->lock);
Chris Mason8790d502008-04-03 16:29:03 -0400473 goto found;
474 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400475 }
Chris Masonc286ac42008-07-22 23:06:41 -0400476 spin_unlock(&cache->lock);
Chris Masonde428b62007-05-18 13:28:27 -0400477 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400478 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400479 if (!wrapped) {
480 last = search_start;
481 wrapped = 1;
482 goto again;
483 }
484 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400485 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400486 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400487 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400488 goto again;
489 }
Chris Masonbe744172007-05-06 10:15:01 -0400490found:
Chris Mason31f3c992007-04-30 15:25:45 -0400491 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400492}
493
Chris Mason925baed2008-06-25 16:01:30 -0400494struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
495 struct btrfs_block_group_cache
496 *hint, u64 search_start,
497 int data, int owner)
498{
499
500 struct btrfs_block_group_cache *ret;
Chris Mason925baed2008-06-25 16:01:30 -0400501 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
Chris Mason925baed2008-06-25 16:01:30 -0400502 return ret;
503}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400504
Chris Masone02119d2008-09-05 16:13:11 -0400505/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400506int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400507{
508 int ret;
509 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400510 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400511
Zheng Yan31840ae2008-09-23 13:14:14 -0400512 path = btrfs_alloc_path();
513 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400514 maybe_lock_mutex(root);
515 key.objectid = start;
516 key.offset = len;
517 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
518 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
519 0, 0);
520 maybe_unlock_mutex(root);
Zheng Yan31840ae2008-09-23 13:14:14 -0400521 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500522 return ret;
523}
524
Chris Masond8d5f3e2007-12-11 12:42:00 -0500525/*
526 * Back reference rules. Back refs have three main goals:
527 *
528 * 1) differentiate between all holders of references to an extent so that
529 * when a reference is dropped we can make sure it was a valid reference
530 * before freeing the extent.
531 *
532 * 2) Provide enough information to quickly find the holders of an extent
533 * if we notice a given block is corrupted or bad.
534 *
535 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
536 * maintenance. This is actually the same as #2, but with a slightly
537 * different use case.
538 *
539 * File extents can be referenced by:
540 *
541 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400542 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500543 * - different offsets inside a file (bookend extents in file.c)
544 *
545 * The extent ref structure has fields for:
546 *
547 * - Objectid of the subvolume root
548 * - Generation number of the tree holding the reference
549 * - objectid of the file holding the reference
550 * - offset in the file corresponding to the key holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400551 * - number of references holding by parent node (alway 1 for tree blocks)
552 *
553 * Btree leaf may hold multiple references to a file extent. In most cases,
554 * these references are from same file and the corresponding offsets inside
555 * the file are close together. So inode objectid and offset in file are
556 * just hints, they provide hints about where in the btree the references
557 * can be found and when we can stop searching.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500558 *
559 * When a file extent is allocated the fields are filled in:
Zheng Yan31840ae2008-09-23 13:14:14 -0400560 * (root_key.objectid, trans->transid, inode objectid, offset in file, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500561 *
562 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400563 * in the leaf. It looks similar to the create case, but trans->transid will
564 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500565 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400566 * (root_key.objectid, trans->transid, inode objectid, offset in file,
567 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500568 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400569 * Because inode objectid and offset in file are just hints, they are not
570 * used when backrefs are deleted. When a file extent is removed either
571 * during snapshot deletion or file truncation, we find the corresponding
572 * back back reference and check the following fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500573 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400574 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf))
Chris Masond8d5f3e2007-12-11 12:42:00 -0500575 *
576 * Btree extents can be referenced by:
577 *
578 * - Different subvolumes
579 * - Different generations of the same subvolume
580 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500581 * When a tree block is created, back references are inserted:
582 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400583 * (root->root_key.objectid, trans->transid, level, 0, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500584 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400585 * When a tree block is cow'd, new back references are added for all the
586 * blocks it points to. If the tree block isn't in reference counted root,
587 * the old back references are removed. These new back references are of
588 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500589 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400590 * (root->root_key.objectid, trans->transid, level, 0, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500591 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400592 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500593 *
594 * if backref was for a tree root:
Zheng Yan31840ae2008-09-23 13:14:14 -0400595 * (btrfs_header_owner(itself), btrfs_header_generation(itself))
Chris Masond8d5f3e2007-12-11 12:42:00 -0500596 * else
Zheng Yan31840ae2008-09-23 13:14:14 -0400597 * (btrfs_header_owner(parent), btrfs_header_generation(parent))
Chris Masond8d5f3e2007-12-11 12:42:00 -0500598 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400599 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500600 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400601 * The key objectid corresponds to the first byte in the extent, the key
602 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
603 * byte of parent extent. If a extent is tree root, the key offset is set
604 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500605 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400606
607static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
608 struct btrfs_root *root,
609 struct btrfs_path *path, u64 bytenr,
610 u64 parent, u64 ref_root,
611 u64 ref_generation, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500612{
Chris Mason74493f72007-12-11 09:25:06 -0500613 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400614 struct btrfs_extent_ref *ref;
615 struct extent_buffer *leaf;
Chris Mason74493f72007-12-11 09:25:06 -0500616 int ret;
617
Chris Mason74493f72007-12-11 09:25:06 -0500618 key.objectid = bytenr;
619 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400620 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500621
Zheng Yan31840ae2008-09-23 13:14:14 -0400622 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
623 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500624 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400625 if (ret > 0) {
626 ret = -ENOENT;
627 goto out;
628 }
629
630 leaf = path->nodes[0];
631 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
632 if (btrfs_ref_root(leaf, ref) != ref_root ||
633 btrfs_ref_generation(leaf, ref) != ref_generation) {
634 ret = -EIO;
635 WARN_ON(1);
636 goto out;
637 }
638 ret = 0;
639out:
640 return ret;
641}
642
643static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
644 struct btrfs_root *root,
645 struct btrfs_path *path,
646 u64 bytenr, u64 parent,
647 u64 ref_root, u64 ref_generation,
648 u64 owner_objectid, u64 owner_offset)
649{
650 struct btrfs_key key;
651 struct extent_buffer *leaf;
652 struct btrfs_extent_ref *ref;
653 u32 num_refs;
654 int ret;
655
656 key.objectid = bytenr;
657 key.type = BTRFS_EXTENT_REF_KEY;
658 key.offset = parent;
659
660 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
661 if (ret == 0) {
662 leaf = path->nodes[0];
663 ref = btrfs_item_ptr(leaf, path->slots[0],
664 struct btrfs_extent_ref);
665 btrfs_set_ref_root(leaf, ref, ref_root);
666 btrfs_set_ref_generation(leaf, ref, ref_generation);
667 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
668 btrfs_set_ref_offset(leaf, ref, owner_offset);
669 btrfs_set_ref_num_refs(leaf, ref, 1);
670 } else if (ret == -EEXIST) {
671 u64 existing_owner;
672 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
673 leaf = path->nodes[0];
674 ref = btrfs_item_ptr(leaf, path->slots[0],
675 struct btrfs_extent_ref);
676 if (btrfs_ref_root(leaf, ref) != ref_root ||
677 btrfs_ref_generation(leaf, ref) != ref_generation) {
678 ret = -EIO;
679 WARN_ON(1);
680 goto out;
681 }
682
683 num_refs = btrfs_ref_num_refs(leaf, ref);
684 BUG_ON(num_refs == 0);
685 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
686
687 existing_owner = btrfs_ref_objectid(leaf, ref);
688 if (existing_owner == owner_objectid &&
689 btrfs_ref_offset(leaf, ref) > owner_offset) {
690 btrfs_set_ref_offset(leaf, ref, owner_offset);
691 } else if (existing_owner != owner_objectid &&
692 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
693 btrfs_set_ref_objectid(leaf, ref,
694 BTRFS_MULTIPLE_OBJECTIDS);
695 btrfs_set_ref_offset(leaf, ref, 0);
696 }
697 ret = 0;
698 } else {
699 goto out;
700 }
Chris Mason7bb86312007-12-11 09:25:06 -0500701 btrfs_mark_buffer_dirty(path->nodes[0]);
702out:
703 btrfs_release_path(root, path);
704 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500705}
706
Zheng Yan31840ae2008-09-23 13:14:14 -0400707static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
708 struct btrfs_root *root,
709 struct btrfs_path *path)
710{
711 struct extent_buffer *leaf;
712 struct btrfs_extent_ref *ref;
713 u32 num_refs;
714 int ret = 0;
715
716 leaf = path->nodes[0];
717 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
718 num_refs = btrfs_ref_num_refs(leaf, ref);
719 BUG_ON(num_refs == 0);
720 num_refs -= 1;
721 if (num_refs == 0) {
722 ret = btrfs_del_item(trans, root, path);
723 } else {
724 btrfs_set_ref_num_refs(leaf, ref, num_refs);
725 btrfs_mark_buffer_dirty(leaf);
726 }
727 btrfs_release_path(root, path);
728 return ret;
729}
730
731static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
732 struct btrfs_root *root, u64 bytenr,
733 u64 orig_parent, u64 parent,
734 u64 orig_root, u64 ref_root,
735 u64 orig_generation, u64 ref_generation,
736 u64 owner_objectid, u64 owner_offset)
737{
738 int ret;
739 struct btrfs_root *extent_root = root->fs_info->extent_root;
740 struct btrfs_path *path;
741
742 if (root == root->fs_info->extent_root) {
743 struct pending_extent_op *extent_op;
744 u64 num_bytes;
745
746 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
747 num_bytes = btrfs_level_size(root, (int)owner_objectid);
748 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
749 bytenr + num_bytes - 1, EXTENT_LOCKED, 0)) {
750 u64 priv;
751 ret = get_state_private(&root->fs_info->extent_ins,
752 bytenr, &priv);
753 BUG_ON(ret);
754 extent_op = (struct pending_extent_op *)
755 (unsigned long)priv;
756 BUG_ON(extent_op->parent != orig_parent);
757 BUG_ON(extent_op->generation != orig_generation);
758 extent_op->parent = parent;
759 extent_op->generation = ref_generation;
760 } else {
761 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
762 BUG_ON(!extent_op);
763
764 extent_op->type = PENDING_BACKREF_UPDATE;
765 extent_op->bytenr = bytenr;
766 extent_op->num_bytes = num_bytes;
767 extent_op->parent = parent;
768 extent_op->orig_parent = orig_parent;
769 extent_op->generation = ref_generation;
770 extent_op->orig_generation = orig_generation;
771 extent_op->level = (int)owner_objectid;
772
773 set_extent_bits(&root->fs_info->extent_ins,
774 bytenr, bytenr + num_bytes - 1,
775 EXTENT_LOCKED, GFP_NOFS);
776 set_state_private(&root->fs_info->extent_ins,
777 bytenr, (unsigned long)extent_op);
778 }
779 return 0;
780 }
781
782 path = btrfs_alloc_path();
783 if (!path)
784 return -ENOMEM;
785 ret = lookup_extent_backref(trans, extent_root, path,
786 bytenr, orig_parent, orig_root,
787 orig_generation, 1);
788 if (ret)
789 goto out;
790 ret = remove_extent_backref(trans, extent_root, path);
791 if (ret)
792 goto out;
793 ret = insert_extent_backref(trans, extent_root, path, bytenr,
794 parent, ref_root, ref_generation,
795 owner_objectid, owner_offset);
796 BUG_ON(ret);
797 finish_current_insert(trans, extent_root);
798 del_pending_extents(trans, extent_root);
799out:
800 btrfs_free_path(path);
801 return ret;
802}
803
804int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
805 struct btrfs_root *root, u64 bytenr,
806 u64 orig_parent, u64 parent,
807 u64 ref_root, u64 ref_generation,
808 u64 owner_objectid, u64 owner_offset)
809{
810 int ret;
811 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
812 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
813 return 0;
814 maybe_lock_mutex(root);
815 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
816 parent, ref_root, ref_root,
817 ref_generation, ref_generation,
818 owner_objectid, owner_offset);
819 maybe_unlock_mutex(root);
820 return ret;
821}
822
Chris Mason925baed2008-06-25 16:01:30 -0400823static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400824 struct btrfs_root *root, u64 bytenr,
825 u64 orig_parent, u64 parent,
826 u64 orig_root, u64 ref_root,
827 u64 orig_generation, u64 ref_generation,
828 u64 owner_objectid, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500829{
Chris Mason5caf2a02007-04-02 11:20:42 -0400830 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500831 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400832 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400833 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400834 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400835 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500836
Chris Mason5caf2a02007-04-02 11:20:42 -0400837 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400838 if (!path)
839 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400840
Chris Mason3c12ac72008-04-21 12:01:38 -0400841 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400842 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400843 key.type = BTRFS_EXTENT_ITEM_KEY;
844 key.offset = (u64)-1;
845
Chris Mason5caf2a02007-04-02 11:20:42 -0400846 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400847 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400848 if (ret < 0)
849 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400850 BUG_ON(ret == 0 || path->slots[0] == 0);
851
852 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400853 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -0400854
855 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
856 BUG_ON(key.objectid != bytenr);
857 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
858
Chris Mason5caf2a02007-04-02 11:20:42 -0400859 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400860 refs = btrfs_extent_refs(l, item);
861 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400862 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500863
Chris Mason5caf2a02007-04-02 11:20:42 -0400864 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500865
Chris Mason3c12ac72008-04-21 12:01:38 -0400866 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -0400867 ret = insert_extent_backref(trans, root->fs_info->extent_root,
868 path, bytenr, parent,
869 ref_root, ref_generation,
870 owner_objectid, owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -0500871 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400872 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400873 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500874
875 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500876 return 0;
877}
878
Chris Mason925baed2008-06-25 16:01:30 -0400879int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400880 struct btrfs_root *root,
881 u64 bytenr, u64 num_bytes, u64 parent,
882 u64 ref_root, u64 ref_generation,
883 u64 owner_objectid, u64 owner_offset)
Chris Mason925baed2008-06-25 16:01:30 -0400884{
885 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400886 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
887 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
888 return 0;
889 maybe_lock_mutex(root);
890 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
891 0, ref_root, 0, ref_generation,
892 owner_objectid, owner_offset);
893 maybe_unlock_mutex(root);
Chris Mason925baed2008-06-25 16:01:30 -0400894 return ret;
895}
896
Chris Masone9d0b132007-08-10 14:06:19 -0400897int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
898 struct btrfs_root *root)
899{
900 finish_current_insert(trans, root->fs_info->extent_root);
901 del_pending_extents(trans, root->fs_info->extent_root);
902 return 0;
903}
904
Zheng Yan31840ae2008-09-23 13:14:14 -0400905int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
906 struct btrfs_root *root, u64 bytenr,
907 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500908{
Chris Mason5caf2a02007-04-02 11:20:42 -0400909 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500910 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400911 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400912 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400913 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400914
Chris Masondb945352007-10-15 16:15:53 -0400915 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400916 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400917 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400918 key.objectid = bytenr;
919 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400920 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400921 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400922 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400923 if (ret < 0)
924 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400925 if (ret != 0) {
926 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400927 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500928 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400929 }
930 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400931 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400932 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400933out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400934 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500935 return 0;
936}
937
Yan Zhengf321e492008-07-30 09:26:11 -0400938static int get_reference_status(struct btrfs_root *root, u64 bytenr,
939 u64 parent_gen, u64 ref_objectid,
940 u64 *min_generation, u32 *ref_count)
Chris Masonbe20aa92007-12-17 20:14:01 -0500941{
942 struct btrfs_root *extent_root = root->fs_info->extent_root;
943 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -0400944 struct extent_buffer *leaf;
945 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -0500946 struct btrfs_key key;
947 struct btrfs_key found_key;
Yan Zhengf321e492008-07-30 09:26:11 -0400948 u64 root_objectid = root->root_key.objectid;
949 u64 ref_generation;
950 u32 nritems;
951 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500952
Chris Masonbe20aa92007-12-17 20:14:01 -0500953 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400954 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -0400955 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -0500956
Yan Zhengf321e492008-07-30 09:26:11 -0400957 path = btrfs_alloc_path();
958 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonbe20aa92007-12-17 20:14:01 -0500959 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
960 if (ret < 0)
961 goto out;
962 BUG_ON(ret == 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400963 if (ret < 0 || path->slots[0] == 0)
964 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500965
Zheng Yan31840ae2008-09-23 13:14:14 -0400966 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -0400967 leaf = path->nodes[0];
968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500969
970 if (found_key.objectid != bytenr ||
971 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
Yan Zhengf321e492008-07-30 09:26:11 -0400972 ret = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500973 goto out;
974 }
975
Yan Zhengf321e492008-07-30 09:26:11 -0400976 *ref_count = 0;
977 *min_generation = (u64)-1;
978
Chris Masonbe20aa92007-12-17 20:14:01 -0500979 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -0400980 leaf = path->nodes[0];
981 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500982 if (path->slots[0] >= nritems) {
983 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -0400984 if (ret < 0)
985 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500986 if (ret == 0)
987 continue;
988 break;
989 }
Yan Zhengf321e492008-07-30 09:26:11 -0400990 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500991 if (found_key.objectid != bytenr)
992 break;
Chris Masonbd098352008-01-03 13:23:19 -0500993
Chris Masonbe20aa92007-12-17 20:14:01 -0500994 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
995 path->slots[0]++;
996 continue;
997 }
998
Yan Zhengf321e492008-07-30 09:26:11 -0400999 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001000 struct btrfs_extent_ref);
Yan Zhengf321e492008-07-30 09:26:11 -04001001 ref_generation = btrfs_ref_generation(leaf, ref_item);
1002 /*
Zheng Yan31840ae2008-09-23 13:14:14 -04001003 * For (parent_gen > 0 && parent_gen > ref_generation):
Yan Zhengf321e492008-07-30 09:26:11 -04001004 *
Yanbcc63ab2008-07-30 16:29:20 -04001005 * we reach here through the oldest root, therefore
1006 * all other reference from same snapshot should have
Yan Zhengf321e492008-07-30 09:26:11 -04001007 * a larger generation.
1008 */
1009 if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
1010 (parent_gen > 0 && parent_gen > ref_generation) ||
1011 (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1012 ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001013 *ref_count = 2;
Yan Zhengf321e492008-07-30 09:26:11 -04001014 break;
1015 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001016
Yan Zhengf321e492008-07-30 09:26:11 -04001017 *ref_count = 1;
1018 if (*min_generation > ref_generation)
1019 *min_generation = ref_generation;
1020
Chris Masonbe20aa92007-12-17 20:14:01 -05001021 path->slots[0]++;
1022 }
Yan Zhengf321e492008-07-30 09:26:11 -04001023 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001024out:
Chris Mason925baed2008-06-25 16:01:30 -04001025 mutex_unlock(&root->fs_info->alloc_mutex);
Yan Zhengf321e492008-07-30 09:26:11 -04001026 btrfs_free_path(path);
1027 return ret;
1028}
1029
Yan Zheng7ea394f2008-08-05 13:05:02 -04001030int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
1031 struct btrfs_root *root,
Yan Zhengf321e492008-07-30 09:26:11 -04001032 struct btrfs_key *key, u64 bytenr)
1033{
Yan Zhengf321e492008-07-30 09:26:11 -04001034 struct btrfs_root *old_root;
1035 struct btrfs_path *path = NULL;
1036 struct extent_buffer *eb;
1037 struct btrfs_file_extent_item *item;
1038 u64 ref_generation;
1039 u64 min_generation;
1040 u64 extent_start;
1041 u32 ref_count;
1042 int level;
1043 int ret;
1044
Yan Zheng7ea394f2008-08-05 13:05:02 -04001045 BUG_ON(trans == NULL);
Yan Zhengf321e492008-07-30 09:26:11 -04001046 BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY);
1047 ret = get_reference_status(root, bytenr, 0, key->objectid,
1048 &min_generation, &ref_count);
1049 if (ret)
1050 return ret;
1051
1052 if (ref_count != 1)
1053 return 1;
1054
Yan Zhengf321e492008-07-30 09:26:11 -04001055 old_root = root->dirty_root->root;
1056 ref_generation = old_root->root_key.offset;
1057
1058 /* all references are created in running transaction */
1059 if (min_generation > ref_generation) {
1060 ret = 0;
1061 goto out;
1062 }
1063
1064 path = btrfs_alloc_path();
1065 if (!path) {
1066 ret = -ENOMEM;
1067 goto out;
1068 }
1069
1070 path->skip_locking = 1;
1071 /* if no item found, the extent is referenced by other snapshot */
1072 ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0);
1073 if (ret)
1074 goto out;
1075
1076 eb = path->nodes[0];
1077 item = btrfs_item_ptr(eb, path->slots[0],
1078 struct btrfs_file_extent_item);
1079 if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG ||
1080 btrfs_file_extent_disk_bytenr(eb, item) != bytenr) {
1081 ret = 1;
1082 goto out;
1083 }
1084
1085 for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) {
1086 if (level >= 0) {
1087 eb = path->nodes[level];
1088 if (!eb)
1089 continue;
1090 extent_start = eb->start;
Yanbcc63ab2008-07-30 16:29:20 -04001091 } else
Yan Zhengf321e492008-07-30 09:26:11 -04001092 extent_start = bytenr;
1093
1094 ret = get_reference_status(root, extent_start, ref_generation,
1095 0, &min_generation, &ref_count);
1096 if (ret)
1097 goto out;
1098
1099 if (ref_count != 1) {
1100 ret = 1;
1101 goto out;
1102 }
1103 if (level >= 0)
1104 ref_generation = btrfs_header_generation(eb);
1105 }
1106 ret = 0;
1107out:
1108 if (path)
1109 btrfs_free_path(path);
Yan Zhengf321e492008-07-30 09:26:11 -04001110 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001111}
Chris Masonc5739bb2007-04-10 09:27:04 -04001112
Zheng Yan31840ae2008-09-23 13:14:14 -04001113int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1114 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001115{
Chris Mason5f39d392007-10-15 16:14:19 -04001116 u32 nritems;
1117 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001118 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -05001119 int i;
Chris Masondb945352007-10-15 16:15:53 -04001120 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001121 int ret = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001122
Chris Mason3768f362007-03-13 16:47:54 -04001123 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001124 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001125
Chris Masondb945352007-10-15 16:15:53 -04001126 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001127 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001128
Zheng Yan31840ae2008-09-23 13:14:14 -04001129 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001130 struct btrfs_leaf_ref *ref;
1131 struct btrfs_extent_info *info;
1132
Zheng Yan31840ae2008-09-23 13:14:14 -04001133 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001134 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001135 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001136 goto out;
1137 }
1138
Chris Mason47ac14f2008-07-31 09:46:18 -04001139 ref->root_gen = root->root_key.offset;
Yan Zheng31153d82008-07-28 15:32:19 -04001140 ref->bytenr = buf->start;
1141 ref->owner = btrfs_header_owner(buf);
1142 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001143 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001144 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001145
Zheng Yan31840ae2008-09-23 13:14:14 -04001146 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001147 u64 disk_bytenr;
1148 btrfs_item_key_to_cpu(buf, &key, i);
1149 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1150 continue;
1151 fi = btrfs_item_ptr(buf, i,
1152 struct btrfs_file_extent_item);
1153 if (btrfs_file_extent_type(buf, fi) ==
1154 BTRFS_FILE_EXTENT_INLINE)
1155 continue;
1156 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1157 if (disk_bytenr == 0)
1158 continue;
1159
1160 info->bytenr = disk_bytenr;
1161 info->num_bytes =
1162 btrfs_file_extent_disk_num_bytes(buf, fi);
1163 info->objectid = key.objectid;
1164 info->offset = key.offset;
1165 info++;
1166 }
1167
1168 BUG_ON(!root->ref_tree);
1169 ret = btrfs_add_leaf_ref(root, ref);
1170 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001171 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001172 }
1173out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001174 return ret;
1175}
1176
1177int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1178 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1179 u32 *nr_extents)
1180{
1181 u64 bytenr;
1182 u64 ref_root;
1183 u64 orig_root;
1184 u64 ref_generation;
1185 u64 orig_generation;
1186 u32 nritems;
1187 u32 nr_file_extents = 0;
1188 struct btrfs_key key;
1189 struct btrfs_file_extent_item *fi;
1190 int i;
1191 int level;
1192 int ret = 0;
1193 int faili = 0;
1194 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1195 u64, u64, u64, u64, u64, u64, u64, u64, u64);
1196
1197 ref_root = btrfs_header_owner(buf);
1198 ref_generation = btrfs_header_generation(buf);
1199 orig_root = btrfs_header_owner(orig_buf);
1200 orig_generation = btrfs_header_generation(orig_buf);
1201
1202 nritems = btrfs_header_nritems(buf);
1203 level = btrfs_header_level(buf);
1204
1205 if (root->ref_cows) {
1206 process_func = __btrfs_inc_extent_ref;
1207 } else {
1208 if (level == 0 &&
1209 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1210 goto out;
1211 if (level != 0 &&
1212 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1213 goto out;
1214 process_func = __btrfs_update_extent_ref;
1215 }
1216
1217 for (i = 0; i < nritems; i++) {
1218 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001219 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001220 btrfs_item_key_to_cpu(buf, &key, i);
1221 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001222 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001223 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001224 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001225 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001226 BTRFS_FILE_EXTENT_INLINE)
1227 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001228 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1229 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001230 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001231
1232 nr_file_extents++;
1233
1234 maybe_lock_mutex(root);
1235 ret = process_func(trans, root, bytenr,
1236 orig_buf->start, buf->start,
1237 orig_root, ref_root,
1238 orig_generation, ref_generation,
1239 key.objectid, key.offset);
1240 maybe_unlock_mutex(root);
1241
1242 if (ret) {
1243 faili = i;
1244 WARN_ON(1);
1245 goto fail;
1246 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001247 } else {
Chris Masondb945352007-10-15 16:15:53 -04001248 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001249 maybe_lock_mutex(root);
1250 ret = process_func(trans, root, bytenr,
1251 orig_buf->start, buf->start,
1252 orig_root, ref_root,
1253 orig_generation, ref_generation,
1254 level - 1, 0);
1255 maybe_unlock_mutex(root);
1256 if (ret) {
1257 faili = i;
1258 WARN_ON(1);
1259 goto fail;
1260 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001261 }
1262 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001263out:
1264 if (nr_extents) {
1265 if (level == 0)
1266 *nr_extents = nr_file_extents;
1267 else
1268 *nr_extents = nritems;
1269 }
1270 return 0;
1271fail:
1272 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001273 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001274}
1275
Zheng Yan31840ae2008-09-23 13:14:14 -04001276int btrfs_update_ref(struct btrfs_trans_handle *trans,
1277 struct btrfs_root *root, struct extent_buffer *orig_buf,
1278 struct extent_buffer *buf, int start_slot, int nr)
1279
1280{
1281 u64 bytenr;
1282 u64 ref_root;
1283 u64 orig_root;
1284 u64 ref_generation;
1285 u64 orig_generation;
1286 struct btrfs_key key;
1287 struct btrfs_file_extent_item *fi;
1288 int i;
1289 int ret;
1290 int slot;
1291 int level;
1292
1293 BUG_ON(start_slot < 0);
1294 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1295
1296 ref_root = btrfs_header_owner(buf);
1297 ref_generation = btrfs_header_generation(buf);
1298 orig_root = btrfs_header_owner(orig_buf);
1299 orig_generation = btrfs_header_generation(orig_buf);
1300 level = btrfs_header_level(buf);
1301
1302 if (!root->ref_cows) {
1303 if (level == 0 &&
1304 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1305 return 0;
1306 if (level != 0 &&
1307 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1308 return 0;
1309 }
1310
1311 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1312 cond_resched();
1313 if (level == 0) {
1314 btrfs_item_key_to_cpu(buf, &key, slot);
1315 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1316 continue;
1317 fi = btrfs_item_ptr(buf, slot,
1318 struct btrfs_file_extent_item);
1319 if (btrfs_file_extent_type(buf, fi) ==
1320 BTRFS_FILE_EXTENT_INLINE)
1321 continue;
1322 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1323 if (bytenr == 0)
1324 continue;
1325 maybe_lock_mutex(root);
1326 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1327 orig_buf->start, buf->start,
1328 orig_root, ref_root,
1329 orig_generation, ref_generation,
1330 key.objectid, key.offset);
1331 maybe_unlock_mutex(root);
1332 if (ret)
1333 goto fail;
1334 } else {
1335 bytenr = btrfs_node_blockptr(buf, slot);
1336 maybe_lock_mutex(root);
1337 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1338 orig_buf->start, buf->start,
1339 orig_root, ref_root,
1340 orig_generation, ref_generation,
1341 level - 1, 0);
1342 maybe_unlock_mutex(root);
1343 if (ret)
1344 goto fail;
1345 }
1346 }
1347 return 0;
1348fail:
1349 WARN_ON(1);
1350 return -1;
1351}
1352
Chris Mason9078a3e2007-04-26 16:46:15 -04001353static int write_one_cache_group(struct btrfs_trans_handle *trans,
1354 struct btrfs_root *root,
1355 struct btrfs_path *path,
1356 struct btrfs_block_group_cache *cache)
1357{
1358 int ret;
1359 int pending_ret;
1360 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001361 unsigned long bi;
1362 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001363
Chris Mason9078a3e2007-04-26 16:46:15 -04001364 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001365 if (ret < 0)
1366 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001367 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001368
1369 leaf = path->nodes[0];
1370 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1371 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1372 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001373 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001374fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001375 finish_current_insert(trans, extent_root);
1376 pending_ret = del_pending_extents(trans, extent_root);
1377 if (ret)
1378 return ret;
1379 if (pending_ret)
1380 return pending_ret;
1381 return 0;
1382
1383}
1384
Chris Mason96b51792007-10-15 16:15:19 -04001385int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1386 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001387{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001388 struct btrfs_block_group_cache *cache, *entry;
1389 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001390 int err = 0;
1391 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001392 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001393 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001394
1395 path = btrfs_alloc_path();
1396 if (!path)
1397 return -ENOMEM;
1398
Chris Mason925baed2008-06-25 16:01:30 -04001399 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001400 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001401 cache = NULL;
1402 spin_lock(&root->fs_info->block_group_cache_lock);
1403 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1404 n; n = rb_next(n)) {
1405 entry = rb_entry(n, struct btrfs_block_group_cache,
1406 cache_node);
1407 if (entry->dirty) {
1408 cache = entry;
1409 break;
1410 }
1411 }
1412 spin_unlock(&root->fs_info->block_group_cache_lock);
1413
1414 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001415 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001416
Josef Bacik0f9dd462008-09-23 13:14:11 -04001417 last += cache->key.offset;
1418
Chris Mason96b51792007-10-15 16:15:19 -04001419 err = write_one_cache_group(trans, root,
1420 path, cache);
1421 /*
1422 * if we fail to write the cache group, we want
1423 * to keep it marked dirty in hopes that a later
1424 * write will work
1425 */
1426 if (err) {
1427 werr = err;
1428 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001429 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001430
1431 cache->dirty = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001432 }
1433 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04001434 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001435 return werr;
1436}
1437
Chris Mason593060d2008-03-25 16:50:33 -04001438static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1439 u64 total_bytes, u64 bytes_used,
1440 struct btrfs_space_info **space_info)
1441{
1442 struct btrfs_space_info *found;
1443
1444 found = __find_space_info(info, flags);
1445 if (found) {
1446 found->total_bytes += total_bytes;
1447 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001448 found->full = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001449 *space_info = found;
1450 return 0;
1451 }
1452 found = kmalloc(sizeof(*found), GFP_NOFS);
1453 if (!found)
1454 return -ENOMEM;
1455
1456 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001457 INIT_LIST_HEAD(&found->block_groups);
1458 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001459 found->flags = flags;
1460 found->total_bytes = total_bytes;
1461 found->bytes_used = bytes_used;
1462 found->bytes_pinned = 0;
1463 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001464 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001465 *space_info = found;
1466 return 0;
1467}
1468
Chris Mason8790d502008-04-03 16:29:03 -04001469static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1470{
1471 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001472 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001473 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001474 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001475 if (extra_flags) {
1476 if (flags & BTRFS_BLOCK_GROUP_DATA)
1477 fs_info->avail_data_alloc_bits |= extra_flags;
1478 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1479 fs_info->avail_metadata_alloc_bits |= extra_flags;
1480 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1481 fs_info->avail_system_alloc_bits |= extra_flags;
1482 }
1483}
Chris Mason593060d2008-03-25 16:50:33 -04001484
Chris Masona061fc82008-05-07 11:43:44 -04001485static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001486{
Chris Masona061fc82008-05-07 11:43:44 -04001487 u64 num_devices = root->fs_info->fs_devices->num_devices;
1488
1489 if (num_devices == 1)
1490 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1491 if (num_devices < 4)
1492 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1493
Chris Masonec44a352008-04-28 15:29:52 -04001494 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1495 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001496 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001497 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001498 }
Chris Masonec44a352008-04-28 15:29:52 -04001499
1500 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001501 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001502 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001503 }
Chris Masonec44a352008-04-28 15:29:52 -04001504
1505 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1506 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1507 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1508 (flags & BTRFS_BLOCK_GROUP_DUP)))
1509 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1510 return flags;
1511}
1512
Chris Mason6324fbf2008-03-24 15:01:59 -04001513static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1514 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001515 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001516{
1517 struct btrfs_space_info *space_info;
1518 u64 thresh;
1519 u64 start;
1520 u64 num_bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001521 int ret = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001522
Chris Masona061fc82008-05-07 11:43:44 -04001523 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001524
Chris Mason6324fbf2008-03-24 15:01:59 -04001525 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001526 if (!space_info) {
1527 ret = update_space_info(extent_root->fs_info, flags,
1528 0, 0, &space_info);
1529 BUG_ON(ret);
1530 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001531 BUG_ON(!space_info);
1532
Chris Mason0ef3e662008-05-24 14:04:53 -04001533 if (space_info->force_alloc) {
1534 force = 1;
1535 space_info->force_alloc = 0;
1536 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001537 if (space_info->full)
Chris Mason925baed2008-06-25 16:01:30 -04001538 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001539
Chris Mason8790d502008-04-03 16:29:03 -04001540 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001541 if (!force &&
1542 (space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
Chris Mason6324fbf2008-03-24 15:01:59 -04001543 thresh)
Chris Mason925baed2008-06-25 16:01:30 -04001544 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001545
Chris Mason925baed2008-06-25 16:01:30 -04001546 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001547 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1548 if (ret == -ENOSPC) {
1549printk("space info full %Lu\n", flags);
1550 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001551 goto out_unlock;
Chris Mason6324fbf2008-03-24 15:01:59 -04001552 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001553 BUG_ON(ret);
1554
1555 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001556 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001557 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001558
Chris Masona74a4b92008-06-25 16:01:31 -04001559out_unlock:
Chris Mason333db942008-06-25 16:01:30 -04001560 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001561out:
Josef Bacik0f9dd462008-09-23 13:14:11 -04001562 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001563}
1564
Chris Mason9078a3e2007-04-26 16:46:15 -04001565static int update_block_group(struct btrfs_trans_handle *trans,
1566 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001567 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001568 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001569{
1570 struct btrfs_block_group_cache *cache;
1571 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001572 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001573 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001574 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001575
Chris Mason7d9eb122008-07-08 14:19:17 -04001576 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason9078a3e2007-04-26 16:46:15 -04001577 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001578 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001579 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001580 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001581 }
Chris Masondb945352007-10-15 16:15:53 -04001582 byte_in_group = bytenr - cache->key.objectid;
1583 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001584
Chris Masonc286ac42008-07-22 23:06:41 -04001585 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001586 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001587 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001588 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001589 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001590 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001591 cache->space_info->bytes_used += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001592 btrfs_set_block_group_used(&cache->item, old_val);
1593 spin_unlock(&cache->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001594 } else {
Chris Masondb945352007-10-15 16:15:53 -04001595 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001596 cache->space_info->bytes_used -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001597 btrfs_set_block_group_used(&cache->item, old_val);
1598 spin_unlock(&cache->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001599 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001600 int ret;
1601 ret = btrfs_add_free_space(cache, bytenr,
1602 num_bytes);
1603 if (ret)
1604 return -1;
Chris Masone37c9e62007-05-09 20:13:14 -04001605 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001606 }
Chris Masondb945352007-10-15 16:15:53 -04001607 total -= num_bytes;
1608 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001609 }
1610 return 0;
1611}
Chris Mason6324fbf2008-03-24 15:01:59 -04001612
Chris Masona061fc82008-05-07 11:43:44 -04001613static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1614{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001615 struct btrfs_block_group_cache *cache;
1616
1617 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1618 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001619 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001620
1621 return cache->key.objectid;
Chris Masona061fc82008-05-07 11:43:44 -04001622}
1623
1624
Chris Masone02119d2008-09-05 16:13:11 -04001625int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05001626 u64 bytenr, u64 num, int pin)
1627{
1628 u64 len;
1629 struct btrfs_block_group_cache *cache;
1630 struct btrfs_fs_info *fs_info = root->fs_info;
1631
Chris Mason7d9eb122008-07-08 14:19:17 -04001632 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Yan324ae4d2007-11-16 14:57:08 -05001633 if (pin) {
1634 set_extent_dirty(&fs_info->pinned_extents,
1635 bytenr, bytenr + num - 1, GFP_NOFS);
1636 } else {
1637 clear_extent_dirty(&fs_info->pinned_extents,
1638 bytenr, bytenr + num - 1, GFP_NOFS);
1639 }
1640 while (num > 0) {
1641 cache = btrfs_lookup_block_group(fs_info, bytenr);
Chris Masona061fc82008-05-07 11:43:44 -04001642 if (!cache) {
1643 u64 first = first_logical_byte(root, bytenr);
1644 WARN_ON(first < bytenr);
1645 len = min(first - bytenr, num);
1646 } else {
1647 len = min(num, cache->key.offset -
1648 (bytenr - cache->key.objectid));
1649 }
Yan324ae4d2007-11-16 14:57:08 -05001650 if (pin) {
Chris Masona061fc82008-05-07 11:43:44 -04001651 if (cache) {
Chris Masonc286ac42008-07-22 23:06:41 -04001652 spin_lock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001653 cache->pinned += len;
1654 cache->space_info->bytes_pinned += len;
Chris Masonc286ac42008-07-22 23:06:41 -04001655 spin_unlock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001656 }
Yan324ae4d2007-11-16 14:57:08 -05001657 fs_info->total_pinned += len;
1658 } else {
Chris Masona061fc82008-05-07 11:43:44 -04001659 if (cache) {
Chris Masonc286ac42008-07-22 23:06:41 -04001660 spin_lock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001661 cache->pinned -= len;
1662 cache->space_info->bytes_pinned -= len;
Chris Masonc286ac42008-07-22 23:06:41 -04001663 spin_unlock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001664 }
Yan324ae4d2007-11-16 14:57:08 -05001665 fs_info->total_pinned -= len;
1666 }
1667 bytenr += len;
1668 num -= len;
1669 }
1670 return 0;
1671}
Chris Mason9078a3e2007-04-26 16:46:15 -04001672
Chris Masond1310b22008-01-24 16:13:08 -05001673int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001674{
Chris Masonccd467d2007-06-28 15:57:36 -04001675 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001676 u64 start;
1677 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001678 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001679 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001680
1681 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001682 ret = find_first_extent_bit(pinned_extents, last,
1683 &start, &end, EXTENT_DIRTY);
1684 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001685 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001686 set_extent_dirty(copy, start, end, GFP_NOFS);
1687 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001688 }
1689 return 0;
1690}
1691
1692int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1693 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001694 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001695{
Chris Mason1a5bc162007-10-15 16:15:26 -04001696 u64 start;
1697 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001698 int ret;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001699 struct btrfs_block_group_cache *cache;
Chris Masona28ec192007-03-06 20:08:01 -05001700
Chris Mason925baed2008-06-25 16:01:30 -04001701 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001702 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001703 ret = find_first_extent_bit(unpin, 0, &start, &end,
1704 EXTENT_DIRTY);
1705 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001706 break;
Chris Masone02119d2008-09-05 16:13:11 -04001707 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001708 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001709 cache = btrfs_lookup_block_group(root->fs_info, start);
1710 if (cache->cached)
1711 btrfs_add_free_space(cache, start, end - start + 1);
Chris Masonc286ac42008-07-22 23:06:41 -04001712 if (need_resched()) {
1713 mutex_unlock(&root->fs_info->alloc_mutex);
1714 cond_resched();
1715 mutex_lock(&root->fs_info->alloc_mutex);
1716 }
Chris Masona28ec192007-03-06 20:08:01 -05001717 }
Chris Mason925baed2008-06-25 16:01:30 -04001718 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001719 return 0;
1720}
1721
Chris Mason98ed5172008-01-03 10:01:48 -05001722static int finish_current_insert(struct btrfs_trans_handle *trans,
1723 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001724{
Chris Mason7bb86312007-12-11 09:25:06 -05001725 u64 start;
1726 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04001727 u64 priv;
Chris Mason7bb86312007-12-11 09:25:06 -05001728 struct btrfs_fs_info *info = extent_root->fs_info;
1729 struct btrfs_path *path;
Zheng Yan31840ae2008-09-23 13:14:14 -04001730 struct btrfs_extent_ref *ref;
1731 struct pending_extent_op *extent_op;
1732 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -04001733 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001734 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -04001735 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001736
Chris Mason7d9eb122008-07-08 14:19:17 -04001737 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Chris Mason5f39d392007-10-15 16:14:19 -04001738 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001739 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001740
Chris Mason26b80032007-08-08 20:17:12 -04001741 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001742 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1743 &end, EXTENT_LOCKED);
1744 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001745 break;
1746
Zheng Yan31840ae2008-09-23 13:14:14 -04001747 ret = get_state_private(&info->extent_ins, start, &priv);
1748 BUG_ON(ret);
1749 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1750
1751 if (extent_op->type == PENDING_EXTENT_INSERT) {
1752 key.objectid = start;
1753 key.offset = end + 1 - start;
1754 key.type = BTRFS_EXTENT_ITEM_KEY;
1755 err = btrfs_insert_item(trans, extent_root, &key,
Chris Mason1a5bc162007-10-15 16:15:26 -04001756 &extent_item, sizeof(extent_item));
Zheng Yan31840ae2008-09-23 13:14:14 -04001757 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001758
Zheng Yan31840ae2008-09-23 13:14:14 -04001759 clear_extent_bits(&info->extent_ins, start, end,
1760 EXTENT_LOCKED, GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04001761
Zheng Yan31840ae2008-09-23 13:14:14 -04001762 err = insert_extent_backref(trans, extent_root, path,
1763 start, extent_op->parent,
1764 extent_root->root_key.objectid,
1765 extent_op->generation,
1766 extent_op->level, 0);
1767 BUG_ON(err);
1768 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1769 err = lookup_extent_backref(trans, extent_root, path,
1770 start, extent_op->orig_parent,
1771 extent_root->root_key.objectid,
1772 extent_op->orig_generation, 0);
1773 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001774
Zheng Yan31840ae2008-09-23 13:14:14 -04001775 clear_extent_bits(&info->extent_ins, start, end,
1776 EXTENT_LOCKED, GFP_NOFS);
1777
1778 key.objectid = start;
1779 key.offset = extent_op->parent;
1780 key.type = BTRFS_EXTENT_REF_KEY;
1781 err = btrfs_set_item_key_safe(trans, extent_root, path,
1782 &key);
1783 BUG_ON(err);
1784 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1785 struct btrfs_extent_ref);
1786 btrfs_set_ref_generation(path->nodes[0], ref,
1787 extent_op->generation);
1788 btrfs_mark_buffer_dirty(path->nodes[0]);
1789 btrfs_release_path(extent_root, path);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001790 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001791 BUG_ON(1);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001792 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001793 kfree(extent_op);
1794
Chris Masonc286ac42008-07-22 23:06:41 -04001795 if (need_resched()) {
1796 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1797 cond_resched();
1798 mutex_lock(&extent_root->fs_info->alloc_mutex);
1799 }
Chris Mason037e6392007-03-07 11:50:24 -05001800 }
Chris Mason7bb86312007-12-11 09:25:06 -05001801 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001802 return 0;
1803}
1804
Zheng Yan31840ae2008-09-23 13:14:14 -04001805static int pin_down_bytes(struct btrfs_trans_handle *trans,
1806 struct btrfs_root *root,
1807 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04001808{
Chris Mason1a5bc162007-10-15 16:15:26 -04001809 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001810 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001811
Chris Mason7d9eb122008-07-08 14:19:17 -04001812 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Zheng Yan31840ae2008-09-23 13:14:14 -04001813 if (is_data)
1814 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001815
Zheng Yan31840ae2008-09-23 13:14:14 -04001816 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1817 if (!buf)
1818 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001819
Zheng Yan31840ae2008-09-23 13:14:14 -04001820 /* we can reuse a block if it hasn't been written
1821 * and it is from this transaction. We can't
1822 * reuse anything from the tree log root because
1823 * it has tiny sub-transactions.
1824 */
1825 if (btrfs_buffer_uptodate(buf, 0) &&
1826 btrfs_try_tree_lock(buf)) {
1827 u64 header_owner = btrfs_header_owner(buf);
1828 u64 header_transid = btrfs_header_generation(buf);
1829 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
1830 header_transid == trans->transid &&
1831 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1832 clean_tree_block(NULL, root, buf);
1833 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001834 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001835 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04001836 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001837 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001838 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001839 free_extent_buffer(buf);
1840pinit:
1841 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
1842
Chris Masonbe744172007-05-06 10:15:01 -04001843 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001844 return 0;
1845}
1846
Chris Masona28ec192007-03-06 20:08:01 -05001847/*
1848 * remove an extent from the root, returns 0 on success
1849 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001850static int __free_extent(struct btrfs_trans_handle *trans,
1851 struct btrfs_root *root,
1852 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05001853 u64 root_objectid, u64 ref_generation,
Zheng Yan31840ae2008-09-23 13:14:14 -04001854 u64 owner_objectid, u64 owner_offset,
1855 int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001856{
Chris Mason5caf2a02007-04-02 11:20:42 -04001857 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001858 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001859 struct btrfs_fs_info *info = root->fs_info;
1860 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001861 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001862 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001863 int extent_slot = 0;
1864 int found_extent = 0;
1865 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001866 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001867 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001868
Chris Mason7d9eb122008-07-08 14:19:17 -04001869 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Masondb945352007-10-15 16:15:53 -04001870 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001871 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001872 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001873 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001874 if (!path)
1875 return -ENOMEM;
1876
Chris Mason3c12ac72008-04-21 12:01:38 -04001877 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001878 ret = lookup_extent_backref(trans, extent_root, path, bytenr, parent,
1879 root_objectid, ref_generation, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001880 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001881 struct btrfs_key found_key;
1882 extent_slot = path->slots[0];
1883 while(extent_slot > 0) {
1884 extent_slot--;
1885 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1886 extent_slot);
1887 if (found_key.objectid != bytenr)
1888 break;
1889 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1890 found_key.offset == num_bytes) {
1891 found_extent = 1;
1892 break;
1893 }
1894 if (path->slots[0] - extent_slot > 5)
1895 break;
1896 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001897 if (!found_extent) {
1898 ret = remove_extent_backref(trans, extent_root, path);
1899 BUG_ON(ret);
1900 btrfs_release_path(extent_root, path);
1901 ret = btrfs_search_slot(trans, extent_root,
1902 &key, path, -1, 1);
1903 BUG_ON(ret);
1904 extent_slot = path->slots[0];
1905 }
Chris Mason7bb86312007-12-11 09:25:06 -05001906 } else {
1907 btrfs_print_leaf(extent_root, path->nodes[0]);
1908 WARN_ON(1);
1909 printk("Unable to find ref byte nr %Lu root %Lu "
1910 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1911 root_objectid, ref_generation, owner_objectid,
1912 owner_offset);
1913 }
Chris Mason5f39d392007-10-15 16:14:19 -04001914
1915 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001916 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001917 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001918 refs = btrfs_extent_refs(leaf, ei);
1919 BUG_ON(refs == 0);
1920 refs -= 1;
1921 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001922
Chris Mason5f39d392007-10-15 16:14:19 -04001923 btrfs_mark_buffer_dirty(leaf);
1924
Chris Mason952fcca2008-02-18 16:33:44 -05001925 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001926 struct btrfs_extent_ref *ref;
1927 ref = btrfs_item_ptr(leaf, path->slots[0],
1928 struct btrfs_extent_ref);
1929 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001930 /* if the back ref and the extent are next to each other
1931 * they get deleted below in one shot
1932 */
1933 path->slots[0] = extent_slot;
1934 num_to_del = 2;
1935 } else if (found_extent) {
1936 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001937 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05001938 BUG_ON(ret);
1939 /* if refs are 0, we need to setup the path for deletion */
1940 if (refs == 0) {
1941 btrfs_release_path(extent_root, path);
1942 ret = btrfs_search_slot(trans, extent_root, &key, path,
1943 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001944 BUG_ON(ret);
1945 }
1946 }
1947
Chris Masoncf27e1e2007-03-13 09:49:06 -04001948 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001949 u64 super_used;
1950 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01001951#ifdef BIO_RW_DISCARD
1952 u64 map_length = num_bytes;
1953 struct btrfs_multi_bio *multi = NULL;
1954#endif
Chris Mason78fae272007-03-25 11:35:08 -04001955
1956 if (pin) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001957 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
1958 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Yanc5492282007-11-06 10:25:25 -05001959 if (ret > 0)
1960 mark_free = 1;
1961 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001962 }
1963
Josef Bacik58176a92007-08-29 15:47:34 -04001964 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04001965 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04001966 super_used = btrfs_super_bytes_used(&info->super_copy);
1967 btrfs_set_super_bytes_used(&info->super_copy,
1968 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04001969 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04001970
1971 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001972 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001973 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001974 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001975 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1976 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04001977 BUG_ON(ret);
Chris Masondb945352007-10-15 16:15:53 -04001978 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001979 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001980 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01001981
1982#ifdef BIO_RW_DISCARD
1983 /* Tell the block device(s) that the sectors can be discarded */
1984 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1985 bytenr, &map_length, &multi, 0);
1986 if (!ret) {
1987 struct btrfs_bio_stripe *stripe = multi->stripes;
1988 int i;
1989
1990 if (map_length > num_bytes)
1991 map_length = num_bytes;
1992
1993 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1994 blkdev_issue_discard(stripe->dev->bdev,
1995 stripe->physical >> 9,
1996 map_length >> 9);
1997 }
1998 kfree(multi);
1999 }
2000#endif
Chris Masona28ec192007-03-06 20:08:01 -05002001 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002002 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002003 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05002004 return ret;
2005}
2006
2007/*
Chris Masonfec577f2007-02-26 10:40:21 -05002008 * find all the blocks marked as pending in the radix tree and remove
2009 * them from the extent map
2010 */
Chris Masone089f052007-03-16 16:20:31 -04002011static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2012 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05002013{
2014 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002015 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002016 int mark_free = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002017 u64 start;
2018 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002019 u64 priv;
Chris Masond1310b22008-01-24 16:13:08 -05002020 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002021 struct extent_io_tree *extent_ins;
2022 struct pending_extent_op *extent_op;
Chris Mason8ef97622007-03-26 10:15:30 -04002023
Chris Mason7d9eb122008-07-08 14:19:17 -04002024 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Zheng Yan31840ae2008-09-23 13:14:14 -04002025 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc162007-10-15 16:15:26 -04002026 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002027
2028 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002029 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2030 EXTENT_LOCKED);
2031 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05002032 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002033
2034 ret = get_state_private(pending_del, start, &priv);
2035 BUG_ON(ret);
2036 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2037
Chris Mason1a5bc162007-10-15 16:15:26 -04002038 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2039 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002040
2041 ret = pin_down_bytes(trans, extent_root, start,
2042 end + 1 - start, 0);
2043 mark_free = ret > 0;
2044 if (!test_range_bit(extent_ins, start, end,
2045 EXTENT_LOCKED, 0)) {
2046free_extent:
Chris Masonc286ac42008-07-22 23:06:41 -04002047 ret = __free_extent(trans, extent_root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002048 start, end + 1 - start,
2049 extent_op->orig_parent,
2050 extent_root->root_key.objectid,
2051 extent_op->orig_generation,
2052 extent_op->level, 0, 0, mark_free);
2053 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002054 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002055 kfree(extent_op);
2056 ret = get_state_private(extent_ins, start, &priv);
2057 BUG_ON(ret);
2058 extent_op = (struct pending_extent_op *)
2059 (unsigned long)priv;
2060
2061 clear_extent_bits(extent_ins, start, end,
2062 EXTENT_LOCKED, GFP_NOFS);
2063
2064 if (extent_op->type == PENDING_BACKREF_UPDATE)
2065 goto free_extent;
2066
2067 ret = update_block_group(trans, extent_root, start,
2068 end + 1 - start, 0, mark_free);
2069 BUG_ON(ret);
2070 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002071 }
Chris Mason1a5bc162007-10-15 16:15:26 -04002072 if (ret)
2073 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04002074
2075 if (need_resched()) {
2076 mutex_unlock(&extent_root->fs_info->alloc_mutex);
2077 cond_resched();
2078 mutex_lock(&extent_root->fs_info->alloc_mutex);
2079 }
Chris Masonfec577f2007-02-26 10:40:21 -05002080 }
Chris Masone20d96d2007-03-22 12:13:20 -04002081 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002082}
2083
2084/*
2085 * remove an extent from the root, returns 0 on success
2086 */
Chris Mason925baed2008-06-25 16:01:30 -04002087static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002088 struct btrfs_root *root,
2089 u64 bytenr, u64 num_bytes, u64 parent,
2090 u64 root_objectid, u64 ref_generation,
2091 u64 owner_objectid, u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002092{
Chris Mason9f5fae22007-03-20 14:38:32 -04002093 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002094 int pending_ret;
2095 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002096
Chris Masondb945352007-10-15 16:15:53 -04002097 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002098 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002099 struct pending_extent_op *extent_op;
2100
2101 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2102 BUG_ON(!extent_op);
2103
2104 extent_op->type = PENDING_EXTENT_DELETE;
2105 extent_op->bytenr = bytenr;
2106 extent_op->num_bytes = num_bytes;
2107 extent_op->parent = parent;
2108 extent_op->orig_parent = parent;
2109 extent_op->generation = ref_generation;
2110 extent_op->orig_generation = ref_generation;
2111 extent_op->level = (int)owner_objectid;
2112
2113 set_extent_bits(&root->fs_info->pending_del,
2114 bytenr, bytenr + num_bytes - 1,
2115 EXTENT_LOCKED, GFP_NOFS);
2116 set_state_private(&root->fs_info->pending_del,
2117 bytenr, (unsigned long)extent_op);
Chris Masona28ec192007-03-06 20:08:01 -05002118 return 0;
2119 }
Chris Mason4bef0842008-09-08 11:18:08 -04002120 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002121 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2122 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002123 struct btrfs_block_group_cache *cache;
2124
Chris Masond00aff02008-09-11 15:54:42 -04002125 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002126 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2127 BUG_ON(!cache);
2128 btrfs_add_free_space(cache, bytenr, num_bytes);
Chris Masond00aff02008-09-11 15:54:42 -04002129 return 0;
2130 }
Chris Mason4bef0842008-09-08 11:18:08 -04002131 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002132 }
Chris Mason4bef0842008-09-08 11:18:08 -04002133
2134 /* if data pin when any transaction has committed this */
2135 if (ref_generation != trans->transid)
2136 pin = 1;
2137
Zheng Yan31840ae2008-09-23 13:14:14 -04002138 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2139 root_objectid, ref_generation, owner_objectid,
2140 owner_offset, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002141
2142 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002143 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05002144 return ret ? ret : pending_ret;
2145}
2146
Chris Mason925baed2008-06-25 16:01:30 -04002147int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002148 struct btrfs_root *root,
2149 u64 bytenr, u64 num_bytes, u64 parent,
2150 u64 root_objectid, u64 ref_generation,
2151 u64 owner_objectid, u64 owner_offset, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002152{
2153 int ret;
2154
2155 maybe_lock_mutex(root);
Zheng Yan31840ae2008-09-23 13:14:14 -04002156 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002157 root_objectid, ref_generation,
2158 owner_objectid, owner_offset, pin);
2159 maybe_unlock_mutex(root);
2160 return ret;
2161}
2162
Chris Mason87ee04e2007-11-30 11:30:34 -05002163static u64 stripe_align(struct btrfs_root *root, u64 val)
2164{
2165 u64 mask = ((u64)root->stripesize - 1);
2166 u64 ret = (val + mask) & ~mask;
2167 return ret;
2168}
2169
Chris Masonfec577f2007-02-26 10:40:21 -05002170/*
2171 * walks the btree of allocated extents and find a hole of a given size.
2172 * The key ins is changed to record the hole:
2173 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002174 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002175 * ins->offset == number of blocks
2176 * Any available blocks before search_start are skipped.
2177 */
Chris Mason98ed5172008-01-03 10:01:48 -05002178static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2179 struct btrfs_root *orig_root,
2180 u64 num_bytes, u64 empty_size,
2181 u64 search_start, u64 search_end,
2182 u64 hint_byte, struct btrfs_key *ins,
2183 u64 exclude_start, u64 exclude_nr,
2184 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002185{
Chris Mason87ee04e2007-11-30 11:30:34 -05002186 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04002187 u64 orig_search_start;
Chris Mason9f5fae22007-03-20 14:38:32 -04002188 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04002189 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04002190 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002191 u64 *last_ptr = NULL;
Chris Masonbe08c1b2007-05-03 09:06:49 -04002192 struct btrfs_block_group_cache *block_group;
Chris Mason0ef3e662008-05-24 14:04:53 -04002193 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002194 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002195 int allowed_chunk_alloc = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05002196
Chris Masondb945352007-10-15 16:15:53 -04002197 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002198 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2199
Chris Mason0ef3e662008-05-24 14:04:53 -04002200 if (orig_root->ref_cows || empty_size)
2201 allowed_chunk_alloc = 1;
2202
Chris Mason239b14b2008-03-24 15:02:07 -04002203 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2204 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04002205 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002206 }
2207
Josef Bacik0f9dd462008-09-23 13:14:11 -04002208 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002209 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002210
Chris Masone02119d2008-09-05 16:13:11 -04002211 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2212 last_ptr = &root->fs_info->last_log_alloc;
2213 if (!last_ptr == 0 && root->fs_info->last_alloc) {
2214 *last_ptr = root->fs_info->last_alloc + empty_cluster;
2215 }
2216 }
Chris Mason239b14b2008-03-24 15:02:07 -04002217
2218 if (last_ptr) {
2219 if (*last_ptr)
2220 hint_byte = *last_ptr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002221 else
Chris Mason239b14b2008-03-24 15:02:07 -04002222 empty_size += empty_cluster;
Chris Mason239b14b2008-03-24 15:02:07 -04002223 }
2224
Chris Masona061fc82008-05-07 11:43:44 -04002225 search_start = max(search_start, first_logical_byte(root, 0));
2226 orig_search_start = search_start;
2227
Chris Mason7f93bf82008-03-24 15:01:28 -04002228 if (search_end == (u64)-1)
2229 search_end = btrfs_super_total_bytes(&info->super_copy);
Chris Mason0b86a832008-03-24 15:01:56 -04002230
Chris Mason239b14b2008-03-24 15:02:07 -04002231 search_start = max(search_start, hint_byte);
Chris Mason6702ed42007-08-07 16:15:09 -04002232 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002233
Josef Bacik0f9dd462008-09-23 13:14:11 -04002234new_group:
2235 block_group = btrfs_lookup_block_group(info, search_start);
2236
2237 /*
2238 * Ok this looks a little tricky, buts its really simple. First if we
2239 * didn't find a block group obviously we want to start over.
2240 * Secondly, if the block group we found does not match the type we
2241 * need, and we have a last_ptr and its not 0, chances are the last
2242 * allocation we made was at the end of the block group, so lets go
2243 * ahead and skip the looking through the rest of the block groups and
2244 * start at the beginning. This helps with metadata allocations,
2245 * since you are likely to have a bunch of data block groups to search
2246 * through first before you realize that you need to start over, so go
2247 * ahead and start over and save the time.
2248 */
2249 if (!block_group || (!block_group_bits(block_group, data) &&
2250 last_ptr && *last_ptr)) {
2251 if (search_start != orig_search_start) {
2252 if (last_ptr && *last_ptr)
2253 *last_ptr = 0;
2254 search_start = orig_search_start;
2255 goto new_group;
2256 } else if (!chunk_alloc_done && allowed_chunk_alloc) {
2257 ret = do_chunk_alloc(trans, root,
2258 num_bytes + 2 * 1024 * 1024,
2259 data, 1);
2260 if (ret < 0) {
2261 struct btrfs_space_info *info;
2262
2263 info = __find_space_info(root->fs_info, data);
2264 goto error;
2265 }
2266 BUG_ON(ret);
2267 chunk_alloc_done = 1;
2268 search_start = orig_search_start;
2269 goto new_group;
2270 } else {
2271 ret = -ENOSPC;
2272 goto error;
Chris Mason0ef3e662008-05-24 14:04:53 -04002273 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002274 }
2275
2276 /*
2277 * this is going to seach through all of the existing block groups it
2278 * can find, so if we don't find something we need to see if we can
2279 * allocate what we need.
2280 */
2281 ret = find_free_space(root, &block_group, &search_start,
2282 total_needed, data);
2283 if (ret == -ENOSPC) {
2284 /*
2285 * instead of allocating, start at the original search start
2286 * and see if there is something to be found, if not then we
2287 * allocate
2288 */
2289 if (search_start != orig_search_start) {
2290 if (last_ptr && *last_ptr) {
2291 *last_ptr = 0;
2292 total_needed += empty_cluster;
2293 }
2294 search_start = orig_search_start;
2295 goto new_group;
2296 }
2297
2298 /*
2299 * we've already allocated, we're pretty screwed
2300 */
2301 if (chunk_alloc_done) {
2302 goto error;
2303 } else if (!allowed_chunk_alloc && block_group &&
2304 block_group_bits(block_group, data)) {
2305 block_group->space_info->force_alloc = 1;
2306 goto error;
2307 } else if (!allowed_chunk_alloc) {
2308 goto error;
2309 }
2310
2311 ret = do_chunk_alloc(trans, root, num_bytes + 2 * 1024 * 1024,
2312 data, 1);
2313 if (ret < 0)
2314 goto error;
2315
2316 BUG_ON(ret);
Chris Mason0ef3e662008-05-24 14:04:53 -04002317 chunk_alloc_done = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002318 if (block_group)
2319 search_start = block_group->key.objectid +
2320 block_group->key.offset;
2321 else
2322 search_start = orig_search_start;
2323 goto new_group;
Chris Mason0ef3e662008-05-24 14:04:53 -04002324 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002325
Chris Mason0b86a832008-03-24 15:01:56 -04002326 if (ret)
2327 goto error;
2328
Chris Mason87ee04e2007-11-30 11:30:34 -05002329 search_start = stripe_align(root, search_start);
Chris Masonfec577f2007-02-26 10:40:21 -05002330 ins->objectid = search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04002331 ins->offset = num_bytes;
Chris Masone37c9e62007-05-09 20:13:14 -04002332
Josef Bacik0f9dd462008-09-23 13:14:11 -04002333 if (ins->objectid + num_bytes >= search_end) {
2334 search_start = orig_search_start;
2335 if (chunk_alloc_done) {
2336 ret = -ENOSPC;
2337 goto error;
2338 }
2339 goto new_group;
2340 }
Chris Mason0b86a832008-03-24 15:01:56 -04002341
2342 if (ins->objectid + num_bytes >
2343 block_group->key.objectid + block_group->key.offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002344 if (search_start == orig_search_start && chunk_alloc_done) {
2345 ret = -ENOSPC;
2346 goto error;
2347 }
Chris Masone19caa52007-10-15 16:17:44 -04002348 search_start = block_group->key.objectid +
2349 block_group->key.offset;
2350 goto new_group;
2351 }
Chris Mason0b86a832008-03-24 15:01:56 -04002352
Chris Masondb945352007-10-15 16:15:53 -04002353 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04002354 ins->objectid < exclude_start + exclude_nr)) {
2355 search_start = exclude_start + exclude_nr;
2356 goto new_group;
2357 }
Chris Mason0b86a832008-03-24 15:01:56 -04002358
Josef Bacik0f9dd462008-09-23 13:14:11 -04002359 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2360 trans->block_group = block_group;
2361
Chris Masondb945352007-10-15 16:15:53 -04002362 ins->offset = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002363 if (last_ptr) {
2364 *last_ptr = ins->objectid + ins->offset;
2365 if (*last_ptr ==
Josef Bacik0f9dd462008-09-23 13:14:11 -04002366 btrfs_super_total_bytes(&root->fs_info->super_copy))
Chris Mason239b14b2008-03-24 15:02:07 -04002367 *last_ptr = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002368 }
Chris Masonbe744172007-05-06 10:15:01 -04002369
Josef Bacik0f9dd462008-09-23 13:14:11 -04002370 ret = 0;
Chris Mason0f70abe2007-02-28 16:46:22 -05002371error:
Chris Mason0f70abe2007-02-28 16:46:22 -05002372 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002373}
Chris Masonec44a352008-04-28 15:29:52 -04002374
Josef Bacik0f9dd462008-09-23 13:14:11 -04002375static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2376{
2377 struct btrfs_block_group_cache *cache;
2378 struct list_head *l;
2379
2380 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
2381 info->total_bytes - info->bytes_used - info->bytes_pinned,
2382 (info->full) ? "" : "not ");
2383
2384 spin_lock(&info->lock);
2385 list_for_each(l, &info->block_groups) {
2386 cache = list_entry(l, struct btrfs_block_group_cache, list);
2387 spin_lock(&cache->lock);
2388 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
2389 "%Lu pinned\n",
2390 cache->key.objectid, cache->key.offset,
2391 btrfs_block_group_used(&cache->item), cache->pinned);
2392 btrfs_dump_free_space(cache, bytes);
2393 spin_unlock(&cache->lock);
2394 }
2395 spin_unlock(&info->lock);
2396}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002397static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2398 struct btrfs_root *root,
2399 u64 num_bytes, u64 min_alloc_size,
2400 u64 empty_size, u64 hint_byte,
2401 u64 search_end, struct btrfs_key *ins,
2402 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05002403{
2404 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04002405 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002406 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04002407 struct btrfs_fs_info *info = root->fs_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002408 struct btrfs_block_group_cache *cache;
Chris Mason925baed2008-06-25 16:01:30 -04002409
Chris Mason6324fbf2008-03-24 15:01:59 -04002410 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04002411 alloc_profile = info->avail_data_alloc_bits &
2412 info->data_alloc_profile;
2413 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002414 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04002415 alloc_profile = info->avail_system_alloc_bits &
2416 info->system_alloc_profile;
2417 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002418 } else {
Chris Mason8790d502008-04-03 16:29:03 -04002419 alloc_profile = info->avail_metadata_alloc_bits &
2420 info->metadata_alloc_profile;
2421 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002422 }
Chris Mason98d20f62008-04-14 09:46:10 -04002423again:
Chris Masona061fc82008-05-07 11:43:44 -04002424 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04002425 /*
2426 * the only place that sets empty_size is btrfs_realloc_node, which
2427 * is not called recursively on allocations
2428 */
2429 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04002430 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04002431 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002432 2 * 1024 * 1024,
2433 BTRFS_BLOCK_GROUP_METADATA |
2434 (info->metadata_alloc_profile &
2435 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002436 }
2437 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002438 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002439 }
Chris Mason0b86a832008-03-24 15:01:56 -04002440
Chris Masondb945352007-10-15 16:15:53 -04002441 WARN_ON(num_bytes < root->sectorsize);
2442 ret = find_free_extent(trans, root, num_bytes, empty_size,
2443 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002444 trans->alloc_exclude_start,
2445 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002446
Chris Mason98d20f62008-04-14 09:46:10 -04002447 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2448 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002449 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002450 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002451 do_chunk_alloc(trans, root->fs_info->extent_root,
2452 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002453 goto again;
2454 }
Chris Masonec44a352008-04-28 15:29:52 -04002455 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002456 struct btrfs_space_info *sinfo;
2457
2458 sinfo = __find_space_info(root->fs_info, data);
2459 printk("allocation failed flags %Lu, wanted %Lu\n",
2460 data, num_bytes);
2461 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04002462 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002463 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002464 cache = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2465 if (!cache) {
2466 printk(KERN_ERR "Unable to find block group for %Lu\n", ins->objectid);
2467 return -ENOSPC;
2468 }
2469
2470 ret = btrfs_remove_free_space(cache, ins->objectid, ins->offset);
2471
2472 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002473}
2474
Chris Mason65b51a02008-08-01 15:11:20 -04002475int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2476{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002477 struct btrfs_block_group_cache *cache;
2478
Chris Mason65b51a02008-08-01 15:11:20 -04002479 maybe_lock_mutex(root);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002480 cache = btrfs_lookup_block_group(root->fs_info, start);
2481 if (!cache) {
2482 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
2483 maybe_unlock_mutex(root);
2484 return -ENOSPC;
2485 }
2486 btrfs_add_free_space(cache, start, len);
Chris Mason65b51a02008-08-01 15:11:20 -04002487 maybe_unlock_mutex(root);
2488 return 0;
2489}
2490
Chris Masone6dcd2d2008-07-17 12:53:50 -04002491int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2492 struct btrfs_root *root,
2493 u64 num_bytes, u64 min_alloc_size,
2494 u64 empty_size, u64 hint_byte,
2495 u64 search_end, struct btrfs_key *ins,
2496 u64 data)
2497{
2498 int ret;
2499 maybe_lock_mutex(root);
2500 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2501 empty_size, hint_byte, search_end, ins,
2502 data);
2503 maybe_unlock_mutex(root);
2504 return ret;
2505}
2506
2507static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002508 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002509 u64 root_objectid, u64 ref_generation,
2510 u64 owner, u64 owner_offset,
2511 struct btrfs_key *ins)
2512{
2513 int ret;
2514 int pending_ret;
2515 u64 super_used;
2516 u64 root_used;
2517 u64 num_bytes = ins->offset;
2518 u32 sizes[2];
2519 struct btrfs_fs_info *info = root->fs_info;
2520 struct btrfs_root *extent_root = info->extent_root;
2521 struct btrfs_extent_item *extent_item;
2522 struct btrfs_extent_ref *ref;
2523 struct btrfs_path *path;
2524 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002525
Zheng Yan31840ae2008-09-23 13:14:14 -04002526 if (parent == 0)
2527 parent = ins->objectid;
2528
Josef Bacik58176a92007-08-29 15:47:34 -04002529 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002530 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002531 super_used = btrfs_super_bytes_used(&info->super_copy);
2532 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002533 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04002534
Josef Bacik58176a92007-08-29 15:47:34 -04002535 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002536 root_used = btrfs_root_used(&root->root_item);
2537 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002538
Chris Mason26b80032007-08-08 20:17:12 -04002539 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002540 struct pending_extent_op *extent_op;
2541
2542 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2543 BUG_ON(!extent_op);
2544
2545 extent_op->type = PENDING_EXTENT_INSERT;
2546 extent_op->bytenr = ins->objectid;
2547 extent_op->num_bytes = ins->offset;
2548 extent_op->parent = parent;
2549 extent_op->orig_parent = 0;
2550 extent_op->generation = ref_generation;
2551 extent_op->orig_generation = 0;
2552 extent_op->level = (int)owner;
2553
Chris Mason1a5bc162007-10-15 16:15:26 -04002554 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2555 ins->objectid + ins->offset - 1,
2556 EXTENT_LOCKED, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002557 set_state_private(&root->fs_info->extent_ins,
2558 ins->objectid, (unsigned long)extent_op);
Chris Mason26b80032007-08-08 20:17:12 -04002559 goto update_block;
2560 }
2561
Chris Mason47e4bb92008-02-01 14:51:59 -05002562 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05002563 keys[1].objectid = ins->objectid;
2564 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04002565 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05002566 sizes[0] = sizeof(*extent_item);
2567 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002568
2569 path = btrfs_alloc_path();
2570 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002571
2572 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2573 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04002574 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002575
Chris Mason47e4bb92008-02-01 14:51:59 -05002576 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2577 struct btrfs_extent_item);
2578 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2579 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2580 struct btrfs_extent_ref);
2581
2582 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2583 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2584 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2585 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
Zheng Yan31840ae2008-09-23 13:14:14 -04002586 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05002587
2588 btrfs_mark_buffer_dirty(path->nodes[0]);
2589
2590 trans->alloc_exclude_start = 0;
2591 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002592 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002593 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002594 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04002595
Chris Mason925baed2008-06-25 16:01:30 -04002596 if (ret)
2597 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002598 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04002599 ret = pending_ret;
2600 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002601 }
Chris Mason26b80032007-08-08 20:17:12 -04002602
2603update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04002604 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002605 if (ret) {
2606 printk("update block group failed for %Lu %Lu\n",
2607 ins->objectid, ins->offset);
2608 BUG();
2609 }
Chris Mason925baed2008-06-25 16:01:30 -04002610out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04002611 return ret;
2612}
2613
2614int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002615 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002616 u64 root_objectid, u64 ref_generation,
2617 u64 owner, u64 owner_offset,
2618 struct btrfs_key *ins)
2619{
2620 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04002621
2622 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2623 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002624 maybe_lock_mutex(root);
Zheng Yan31840ae2008-09-23 13:14:14 -04002625 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2626 root_objectid, ref_generation,
2627 owner, owner_offset, ins);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002628 maybe_unlock_mutex(root);
2629 return ret;
2630}
Chris Masone02119d2008-09-05 16:13:11 -04002631
2632/*
2633 * this is used by the tree logging recovery code. It records that
2634 * an extent has been allocated and makes sure to clear the free
2635 * space cache bits as well
2636 */
2637int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002638 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04002639 u64 root_objectid, u64 ref_generation,
2640 u64 owner, u64 owner_offset,
2641 struct btrfs_key *ins)
2642{
2643 int ret;
2644 struct btrfs_block_group_cache *block_group;
2645
2646 maybe_lock_mutex(root);
2647 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2648 cache_block_group(root, block_group);
2649
Josef Bacik0f9dd462008-09-23 13:14:11 -04002650 ret = btrfs_remove_free_space(block_group, ins->objectid, ins->offset);
2651 BUG_ON(ret);
Zheng Yan31840ae2008-09-23 13:14:14 -04002652 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2653 root_objectid, ref_generation,
2654 owner, owner_offset, ins);
Chris Masone02119d2008-09-05 16:13:11 -04002655 maybe_unlock_mutex(root);
2656 return ret;
2657}
2658
Chris Masone6dcd2d2008-07-17 12:53:50 -04002659/*
2660 * finds a free extent and does all the dirty work required for allocation
2661 * returns the key for the extent through ins, and a tree buffer for
2662 * the first block of the extent through buf.
2663 *
2664 * returns 0 if everything worked, non-zero otherwise.
2665 */
2666int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2667 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002668 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002669 u64 root_objectid, u64 ref_generation,
Zheng Yan31840ae2008-09-23 13:14:14 -04002670 u64 owner_objectid, u64 owner_offset,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002671 u64 empty_size, u64 hint_byte,
2672 u64 search_end, struct btrfs_key *ins, u64 data)
2673{
2674 int ret;
2675
2676 maybe_lock_mutex(root);
2677
2678 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2679 min_alloc_size, empty_size, hint_byte,
2680 search_end, ins, data);
2681 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04002682 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002683 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2684 root_objectid, ref_generation,
2685 owner_objectid, owner_offset, ins);
Chris Masond00aff02008-09-11 15:54:42 -04002686 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002687
Chris Masond00aff02008-09-11 15:54:42 -04002688 }
Chris Mason925baed2008-06-25 16:01:30 -04002689 maybe_unlock_mutex(root);
2690 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002691}
Chris Mason65b51a02008-08-01 15:11:20 -04002692
2693struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2694 struct btrfs_root *root,
2695 u64 bytenr, u32 blocksize)
2696{
2697 struct extent_buffer *buf;
2698
2699 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2700 if (!buf)
2701 return ERR_PTR(-ENOMEM);
2702 btrfs_set_header_generation(buf, trans->transid);
2703 btrfs_tree_lock(buf);
2704 clean_tree_block(trans, root, buf);
2705 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04002706 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2707 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04002708 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002709 } else {
2710 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2711 buf->start + buf->len - 1, GFP_NOFS);
2712 }
Chris Mason65b51a02008-08-01 15:11:20 -04002713 trans->blocks_used++;
2714 return buf;
2715}
2716
Chris Masonfec577f2007-02-26 10:40:21 -05002717/*
2718 * helper function to allocate a block for a given tree
2719 * returns the tree buffer or NULL.
2720 */
Chris Mason5f39d392007-10-15 16:14:19 -04002721struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04002722 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002723 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002724 u64 root_objectid,
2725 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05002726 int level,
2727 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04002728 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05002729{
Chris Masone2fa7222007-03-12 16:22:34 -04002730 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05002731 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002732 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05002733
Zheng Yan31840ae2008-09-23 13:14:14 -04002734 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
2735 root_objectid, ref_generation, level, 0,
2736 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002737 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04002738 BUG_ON(ret > 0);
2739 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05002740 }
Chris Mason55c69072008-01-09 15:55:33 -05002741
Chris Mason65b51a02008-08-01 15:11:20 -04002742 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05002743 return buf;
2744}
Chris Masona28ec192007-03-06 20:08:01 -05002745
Chris Masone02119d2008-09-05 16:13:11 -04002746int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
2747 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04002748{
Chris Mason7bb86312007-12-11 09:25:06 -05002749 u64 leaf_owner;
2750 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04002751 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002752 struct btrfs_file_extent_item *fi;
2753 int i;
2754 int nritems;
2755 int ret;
2756
Chris Mason5f39d392007-10-15 16:14:19 -04002757 BUG_ON(!btrfs_is_leaf(leaf));
2758 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05002759 leaf_owner = btrfs_header_owner(leaf);
2760 leaf_generation = btrfs_header_generation(leaf);
2761
Chris Mason6407bf62007-03-27 06:33:00 -04002762 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002763 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04002764 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04002765
2766 btrfs_item_key_to_cpu(leaf, &key, i);
2767 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04002768 continue;
2769 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002770 if (btrfs_file_extent_type(leaf, fi) ==
2771 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04002772 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04002773 /*
2774 * FIXME make sure to insert a trans record that
2775 * repeats the snapshot del on crash
2776 */
Chris Masondb945352007-10-15 16:15:53 -04002777 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2778 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04002779 continue;
Chris Mason4a096752008-07-21 10:29:44 -04002780
2781 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002782 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002783 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04002784 leaf->start, leaf_owner, leaf_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05002785 key.objectid, key.offset, 0);
Chris Mason4a096752008-07-21 10:29:44 -04002786 mutex_unlock(&root->fs_info->alloc_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002787 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04002788
2789 atomic_inc(&root->fs_info->throttle_gen);
2790 wake_up(&root->fs_info->transaction_throttle);
2791 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04002792 }
2793 return 0;
2794}
2795
Chris Masone02119d2008-09-05 16:13:11 -04002796static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2797 struct btrfs_root *root,
2798 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04002799{
2800 int i;
2801 int ret;
2802 struct btrfs_extent_info *info = ref->extents;
2803
Yan Zheng31153d82008-07-28 15:32:19 -04002804 for (i = 0; i < ref->nritems; i++) {
2805 mutex_lock(&root->fs_info->alloc_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002806 ret = __btrfs_free_extent(trans, root, info->bytenr,
2807 info->num_bytes, ref->bytenr,
2808 ref->owner, ref->generation,
2809 info->objectid, info->offset, 0);
Yan Zheng31153d82008-07-28 15:32:19 -04002810 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason2dd3e672008-08-04 08:20:15 -04002811
2812 atomic_inc(&root->fs_info->throttle_gen);
2813 wake_up(&root->fs_info->transaction_throttle);
2814 cond_resched();
2815
Yan Zheng31153d82008-07-28 15:32:19 -04002816 BUG_ON(ret);
2817 info++;
2818 }
Yan Zheng31153d82008-07-28 15:32:19 -04002819
2820 return 0;
2821}
2822
Chris Mason333db942008-06-25 16:01:30 -04002823int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2824 u32 *refs)
2825{
Chris Mason017e5362008-07-28 15:32:51 -04002826 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04002827
Zheng Yan31840ae2008-09-23 13:14:14 -04002828 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04002829 BUG_ON(ret);
2830
2831#if 0 // some debugging code in case we see problems here
2832 /* if the refs count is one, it won't get increased again. But
2833 * if the ref count is > 1, someone may be decreasing it at
2834 * the same time we are.
2835 */
2836 if (*refs != 1) {
2837 struct extent_buffer *eb = NULL;
2838 eb = btrfs_find_create_tree_block(root, start, len);
2839 if (eb)
2840 btrfs_tree_lock(eb);
2841
2842 mutex_lock(&root->fs_info->alloc_mutex);
2843 ret = lookup_extent_ref(NULL, root, start, len, refs);
2844 BUG_ON(ret);
2845 mutex_unlock(&root->fs_info->alloc_mutex);
2846
2847 if (eb) {
2848 btrfs_tree_unlock(eb);
2849 free_extent_buffer(eb);
2850 }
2851 if (*refs == 1) {
2852 printk("block %llu went down to one during drop_snap\n",
2853 (unsigned long long)start);
2854 }
2855
2856 }
2857#endif
2858
Chris Masone7a84562008-06-25 16:01:31 -04002859 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04002860 return ret;
Chris Mason333db942008-06-25 16:01:30 -04002861}
2862
2863/*
Chris Mason9aca1d52007-03-13 11:09:37 -04002864 * helper function for drop_snapshot, this walks down the tree dropping ref
2865 * counts as it goes.
2866 */
Chris Mason98ed5172008-01-03 10:01:48 -05002867static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2868 struct btrfs_root *root,
2869 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002870{
Chris Mason7bb86312007-12-11 09:25:06 -05002871 u64 root_owner;
2872 u64 root_gen;
2873 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04002874 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002875 struct extent_buffer *next;
2876 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002877 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04002878 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04002879 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002880 int ret;
2881 u32 refs;
2882
Chris Mason5caf2a02007-04-02 11:20:42 -04002883 WARN_ON(*level < 0);
2884 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04002885 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04002886 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002887 BUG_ON(ret);
2888 if (refs > 1)
2889 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002890
Chris Mason9aca1d52007-03-13 11:09:37 -04002891 /*
2892 * walk down to the last node level and free all the leaves
2893 */
Chris Mason6407bf62007-03-27 06:33:00 -04002894 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002895 WARN_ON(*level < 0);
2896 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002897 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002898
Chris Mason5f39d392007-10-15 16:14:19 -04002899 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002900 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002901
Chris Mason7518a232007-03-12 12:01:18 -04002902 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002903 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002904 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002905 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002906 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04002907 BUG_ON(ret);
2908 break;
2909 }
Chris Masondb945352007-10-15 16:15:53 -04002910 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04002911 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04002912 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002913
Chris Mason333db942008-06-25 16:01:30 -04002914 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002915 BUG_ON(ret);
2916 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002917 parent = path->nodes[*level];
2918 root_owner = btrfs_header_owner(parent);
2919 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002920 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04002921
2922 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002923 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04002924 blocksize, parent->start,
2925 root_owner, root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002926 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002927 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason18e35e02008-08-01 13:11:41 -04002928
2929 atomic_inc(&root->fs_info->throttle_gen);
2930 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04002931 cond_resched();
Chris Mason18e35e02008-08-01 13:11:41 -04002932
Chris Mason20524f02007-03-10 06:35:47 -05002933 continue;
2934 }
Chris Masonf87f0572008-08-01 11:27:23 -04002935 /*
2936 * at this point, we have a single ref, and since the
2937 * only place referencing this extent is a dead root
2938 * the reference count should never go higher.
2939 * So, we don't need to check it again
2940 */
Yan Zheng31153d82008-07-28 15:32:19 -04002941 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04002942 ref = btrfs_lookup_leaf_ref(root, bytenr);
Yan Zheng31153d82008-07-28 15:32:19 -04002943 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04002944 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002945 BUG_ON(ret);
2946 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04002947 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002948 *level = 0;
2949 break;
2950 }
Chris Mason37d1aee2008-07-31 10:48:37 -04002951 if (printk_ratelimit())
2952 printk("leaf ref miss for bytenr %llu\n",
2953 (unsigned long long)bytenr);
Yan Zheng31153d82008-07-28 15:32:19 -04002954 }
Chris Masondb945352007-10-15 16:15:53 -04002955 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04002956 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002957 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04002958
Chris Masonca7a79a2008-05-12 12:59:19 -04002959 next = read_tree_block(root, bytenr, blocksize,
2960 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04002961 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04002962#if 0
2963 /*
2964 * this is a debugging check and can go away
2965 * the ref should never go all the way down to 1
2966 * at this point
2967 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002968 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2969 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002970 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002971 WARN_ON(refs != 1);
2972#endif
Chris Masone9d0b132007-08-10 14:06:19 -04002973 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002974 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002975 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002976 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002977 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002978 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002979 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04002980 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05002981 }
2982out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002983 WARN_ON(*level < 0);
2984 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002985
2986 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05002987 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04002988 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05002989 } else {
2990 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04002991 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05002992 }
2993
Yan Zheng31153d82008-07-28 15:32:19 -04002994 blocksize = btrfs_level_size(root, *level);
2995 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05002996 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04002997
Chris Masonf87f0572008-08-01 11:27:23 -04002998 mutex_lock(&root->fs_info->alloc_mutex);
Yan Zheng31153d82008-07-28 15:32:19 -04002999 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003000 parent->start, root_owner, root_gen,
3001 0, 0, 1);
3002 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04003003 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003004 path->nodes[*level] = NULL;
3005 *level += 1;
3006 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003007
Chris Masone7a84562008-06-25 16:01:31 -04003008 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003009 return 0;
3010}
3011
Chris Mason9aca1d52007-03-13 11:09:37 -04003012/*
3013 * helper for dropping snapshots. This walks back up the tree in the path
3014 * to find the first node higher up where we haven't yet gone through
3015 * all the slots
3016 */
Chris Mason98ed5172008-01-03 10:01:48 -05003017static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3018 struct btrfs_root *root,
3019 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003020{
Chris Mason7bb86312007-12-11 09:25:06 -05003021 u64 root_owner;
3022 u64 root_gen;
3023 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003024 int i;
3025 int slot;
3026 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003027
Chris Mason234b63a2007-03-13 10:46:10 -04003028 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003029 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003030 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3031 struct extent_buffer *node;
3032 struct btrfs_disk_key disk_key;
3033 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003034 path->slots[i]++;
3035 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003036 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003037 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003038 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003039 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003040 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003041 return 0;
3042 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003043 struct extent_buffer *parent;
3044 if (path->nodes[*level] == root->node)
3045 parent = path->nodes[*level];
3046 else
3047 parent = path->nodes[*level + 1];
3048
3049 root_owner = btrfs_header_owner(parent);
3050 root_gen = btrfs_header_generation(parent);
Chris Masone089f052007-03-16 16:20:31 -04003051 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003052 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003053 path->nodes[*level]->len,
Zheng Yan31840ae2008-09-23 13:14:14 -04003054 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003055 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003056 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003057 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003058 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003059 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003060 }
3061 }
3062 return 1;
3063}
3064
Chris Mason9aca1d52007-03-13 11:09:37 -04003065/*
3066 * drop the reference count on the tree rooted at 'snap'. This traverses
3067 * the tree freeing any blocks that have a ref count of zero after being
3068 * decremented.
3069 */
Chris Masone089f052007-03-16 16:20:31 -04003070int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003071 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003072{
Chris Mason3768f362007-03-13 16:47:54 -04003073 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003074 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003075 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003076 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003077 int i;
3078 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003079 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003080
Chris Masona2135012008-06-25 16:01:30 -04003081 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003082 path = btrfs_alloc_path();
3083 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003084
Chris Mason5f39d392007-10-15 16:14:19 -04003085 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003086 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003087 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3088 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003089 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003090 path->slots[level] = 0;
3091 } else {
3092 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003093 struct btrfs_disk_key found_key;
3094 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003095
Chris Mason9f3a7422007-08-07 15:52:19 -04003096 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003097 level = root_item->drop_level;
3098 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003099 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003100 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003101 ret = wret;
3102 goto out;
3103 }
Chris Mason5f39d392007-10-15 16:14:19 -04003104 node = path->nodes[level];
3105 btrfs_node_key(node, &found_key, path->slots[level]);
3106 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3107 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003108 /*
3109 * unlock our path, this is safe because only this
3110 * function is allowed to delete this snapshot
3111 */
Chris Mason925baed2008-06-25 16:01:30 -04003112 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3113 if (path->nodes[i] && path->locks[i]) {
3114 path->locks[i] = 0;
3115 btrfs_tree_unlock(path->nodes[i]);
3116 }
3117 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003118 }
Chris Mason20524f02007-03-10 06:35:47 -05003119 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003120 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003121 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003122 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003123 if (wret < 0)
3124 ret = wret;
3125
Chris Mason5caf2a02007-04-02 11:20:42 -04003126 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003127 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003128 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003129 if (wret < 0)
3130 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003131 if (trans->transaction->in_commit) {
3132 ret = -EAGAIN;
3133 break;
3134 }
Chris Mason18e35e02008-08-01 13:11:41 -04003135 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003136 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003137 }
Chris Mason83e15a22007-03-12 09:03:27 -04003138 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003139 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003140 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003141 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003142 }
Chris Mason20524f02007-03-10 06:35:47 -05003143 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003144out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003145 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003146 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003147}
Chris Mason9078a3e2007-04-26 16:46:15 -04003148
Chris Masonbe744172007-05-06 10:15:01 -04003149int btrfs_free_block_groups(struct btrfs_fs_info *info)
3150{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003151 struct btrfs_block_group_cache *block_group;
3152 struct rb_node *n;
Chris Mason925baed2008-06-25 16:01:30 -04003153
3154 mutex_lock(&info->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003155 spin_lock(&info->block_group_cache_lock);
3156 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
3157 block_group = rb_entry(n, struct btrfs_block_group_cache,
3158 cache_node);
3159
3160 btrfs_remove_free_space_cache(block_group);
3161 rb_erase(&block_group->cache_node,
3162 &info->block_group_cache_tree);
3163 spin_lock(&block_group->space_info->lock);
3164 list_del(&block_group->list);
3165 spin_unlock(&block_group->space_info->lock);
3166 kfree(block_group);
Chris Mason96b51792007-10-15 16:15:19 -04003167 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003168 spin_unlock(&info->block_group_cache_lock);
Chris Mason925baed2008-06-25 16:01:30 -04003169 mutex_unlock(&info->alloc_mutex);
Chris Masonbe744172007-05-06 10:15:01 -04003170 return 0;
3171}
3172
Chris Mason8e7bf942008-04-28 09:02:36 -04003173static unsigned long calc_ra(unsigned long start, unsigned long last,
3174 unsigned long nr)
3175{
3176 return min(last, start + nr - 1);
3177}
3178
Chris Mason98ed5172008-01-03 10:01:48 -05003179static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3180 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003181{
3182 u64 page_start;
3183 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05003184 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003185 unsigned long i;
3186 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003187 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003188 struct file_ra_state *ra;
Chris Mason8e7bf942008-04-28 09:02:36 -04003189 unsigned long total_read = 0;
3190 unsigned long ra_pages;
Chris Mason3eaa2882008-07-24 11:57:52 -04003191 struct btrfs_ordered_extent *ordered;
Chris Masona061fc82008-05-07 11:43:44 -04003192 struct btrfs_trans_handle *trans;
Chris Mason4313b392008-01-03 09:08:48 -05003193
3194 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003195
3196 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05003197 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003198 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3199
Chris Mason8e7bf942008-04-28 09:02:36 -04003200 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
3201
Chris Mason4313b392008-01-03 09:08:48 -05003202 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003203
Chris Mason4313b392008-01-03 09:08:48 -05003204 for (; i <= last_index; i++) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003205 if (total_read % ra_pages == 0) {
3206 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3207 calc_ra(i, last_index, ra_pages));
3208 }
3209 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003210again:
3211 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Chris Masona061fc82008-05-07 11:43:44 -04003212 goto truncate_racing;
Chris Masonedbd8d42007-12-21 16:27:24 -05003213 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003214 if (!page) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003215 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003216 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003217 if (!PageUptodate(page)) {
3218 btrfs_readpage(NULL, page);
3219 lock_page(page);
3220 if (!PageUptodate(page)) {
3221 unlock_page(page);
3222 page_cache_release(page);
3223 goto out_unlock;
3224 }
3225 }
Chris Masonec44a352008-04-28 15:29:52 -04003226 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003227
Chris Masonedbd8d42007-12-21 16:27:24 -05003228 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3229 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003230 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003231
Chris Mason3eaa2882008-07-24 11:57:52 -04003232 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3233 if (ordered) {
3234 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3235 unlock_page(page);
3236 page_cache_release(page);
3237 btrfs_start_ordered_extent(inode, ordered, 1);
3238 btrfs_put_ordered_extent(ordered);
3239 goto again;
3240 }
3241 set_page_extent_mapped(page);
3242
Chris Masonf87f0572008-08-01 11:27:23 -04003243 /*
3244 * make sure page_mkwrite is called for this page if userland
3245 * wants to change it from mmap
3246 */
3247 clear_page_dirty_for_io(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003248
Chris Masonea8c2812008-08-04 23:17:27 -04003249 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masona061fc82008-05-07 11:43:44 -04003250 set_page_dirty(page);
Chris Masonedbd8d42007-12-21 16:27:24 -05003251
Chris Masond1310b22008-01-24 16:13:08 -05003252 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003253 unlock_page(page);
3254 page_cache_release(page);
3255 }
3256
3257out_unlock:
Chris Mason3eaa2882008-07-24 11:57:52 -04003258 /* we have to start the IO in order to get the ordered extents
3259 * instantiated. This allows the relocation to code to wait
3260 * for all the ordered extents to hit the disk.
3261 *
3262 * Otherwise, it would constantly loop over the same extents
3263 * because the old ones don't get deleted until the IO is
3264 * started
3265 */
3266 btrfs_fdatawrite_range(inode->i_mapping, start, start + len - 1,
3267 WB_SYNC_NONE);
Chris Masonec44a352008-04-28 15:29:52 -04003268 kfree(ra);
Chris Masona061fc82008-05-07 11:43:44 -04003269 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
3270 if (trans) {
Chris Masona061fc82008-05-07 11:43:44 -04003271 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
3272 mark_inode_dirty(inode);
3273 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003274 mutex_unlock(&inode->i_mutex);
3275 return 0;
Chris Masona061fc82008-05-07 11:43:44 -04003276
3277truncate_racing:
3278 vmtruncate(inode, inode->i_size);
3279 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
3280 total_read);
3281 goto out_unlock;
Chris Masonedbd8d42007-12-21 16:27:24 -05003282}
3283
Chris Mason4313b392008-01-03 09:08:48 -05003284/*
Chris Masonbf4ef672008-05-08 13:26:18 -04003285 * The back references tell us which tree holds a ref on a block,
3286 * but it is possible for the tree root field in the reference to
3287 * reflect the original root before a snapshot was made. In this
3288 * case we should search through all the children of a given root
3289 * to find potential holders of references on a block.
3290 *
3291 * Instead, we do something a little less fancy and just search
3292 * all the roots for a given key/block combination.
3293 */
3294static int find_root_for_ref(struct btrfs_root *root,
3295 struct btrfs_path *path,
3296 struct btrfs_key *key0,
3297 int level,
3298 int file_key,
3299 struct btrfs_root **found_root,
3300 u64 bytenr)
3301{
3302 struct btrfs_key root_location;
3303 struct btrfs_root *cur_root = *found_root;
3304 struct btrfs_file_extent_item *file_extent;
3305 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
3306 u64 found_bytenr;
3307 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04003308
3309 root_location.offset = (u64)-1;
3310 root_location.type = BTRFS_ROOT_ITEM_KEY;
3311 path->lowest_level = level;
3312 path->reada = 0;
3313 while(1) {
3314 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
3315 found_bytenr = 0;
3316 if (ret == 0 && file_key) {
3317 struct extent_buffer *leaf = path->nodes[0];
3318 file_extent = btrfs_item_ptr(leaf, path->slots[0],
3319 struct btrfs_file_extent_item);
3320 if (btrfs_file_extent_type(leaf, file_extent) ==
3321 BTRFS_FILE_EXTENT_REG) {
3322 found_bytenr =
3323 btrfs_file_extent_disk_bytenr(leaf,
3324 file_extent);
3325 }
Chris Mason323da792008-05-09 11:46:48 -04003326 } else if (!file_key) {
Chris Masonbf4ef672008-05-08 13:26:18 -04003327 if (path->nodes[level])
3328 found_bytenr = path->nodes[level]->start;
3329 }
3330
Chris Masonbf4ef672008-05-08 13:26:18 -04003331 btrfs_release_path(cur_root, path);
3332
3333 if (found_bytenr == bytenr) {
3334 *found_root = cur_root;
3335 ret = 0;
3336 goto out;
3337 }
3338 ret = btrfs_search_root(root->fs_info->tree_root,
3339 root_search_start, &root_search_start);
3340 if (ret)
3341 break;
3342
3343 root_location.objectid = root_search_start;
3344 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
3345 &root_location);
3346 if (!cur_root) {
3347 ret = 1;
3348 break;
3349 }
3350 }
3351out:
3352 path->lowest_level = 0;
3353 return ret;
3354}
3355
3356/*
Chris Mason4313b392008-01-03 09:08:48 -05003357 * note, this releases the path
3358 */
Chris Mason98ed5172008-01-03 10:01:48 -05003359static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05003360 struct btrfs_path *path,
Chris Mason0ef3e662008-05-24 14:04:53 -04003361 struct btrfs_key *extent_key,
3362 u64 *last_file_objectid,
3363 u64 *last_file_offset,
3364 u64 *last_file_root,
3365 u64 last_extent)
Chris Masonedbd8d42007-12-21 16:27:24 -05003366{
3367 struct inode *inode;
3368 struct btrfs_root *found_root;
Chris Masonbf4ef672008-05-08 13:26:18 -04003369 struct btrfs_key root_location;
3370 struct btrfs_key found_key;
Chris Mason4313b392008-01-03 09:08:48 -05003371 struct btrfs_extent_ref *ref;
3372 u64 ref_root;
3373 u64 ref_gen;
3374 u64 ref_objectid;
3375 u64 ref_offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05003376 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04003377 int level;
Chris Masonedbd8d42007-12-21 16:27:24 -05003378
Chris Mason7d9eb122008-07-08 14:19:17 -04003379 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
3380
Chris Mason4313b392008-01-03 09:08:48 -05003381 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
3382 struct btrfs_extent_ref);
3383 ref_root = btrfs_ref_root(path->nodes[0], ref);
3384 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
3385 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
3386 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
3387 btrfs_release_path(extent_root, path);
3388
Chris Masonbf4ef672008-05-08 13:26:18 -04003389 root_location.objectid = ref_root;
Chris Masonedbd8d42007-12-21 16:27:24 -05003390 if (ref_gen == 0)
Chris Masonbf4ef672008-05-08 13:26:18 -04003391 root_location.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003392 else
Chris Masonbf4ef672008-05-08 13:26:18 -04003393 root_location.offset = (u64)-1;
3394 root_location.type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05003395
3396 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Masonbf4ef672008-05-08 13:26:18 -04003397 &root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05003398 BUG_ON(!found_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003399 mutex_unlock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003400
3401 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Chris Masonbf4ef672008-05-08 13:26:18 -04003402 found_key.objectid = ref_objectid;
3403 found_key.type = BTRFS_EXTENT_DATA_KEY;
3404 found_key.offset = ref_offset;
3405 level = 0;
3406
Chris Mason0ef3e662008-05-24 14:04:53 -04003407 if (last_extent == extent_key->objectid &&
3408 *last_file_objectid == ref_objectid &&
3409 *last_file_offset == ref_offset &&
3410 *last_file_root == ref_root)
3411 goto out;
3412
Chris Masonbf4ef672008-05-08 13:26:18 -04003413 ret = find_root_for_ref(extent_root, path, &found_key,
3414 level, 1, &found_root,
3415 extent_key->objectid);
3416
3417 if (ret)
3418 goto out;
3419
Chris Mason0ef3e662008-05-24 14:04:53 -04003420 if (last_extent == extent_key->objectid &&
3421 *last_file_objectid == ref_objectid &&
3422 *last_file_offset == ref_offset &&
3423 *last_file_root == ref_root)
3424 goto out;
3425
Chris Masonedbd8d42007-12-21 16:27:24 -05003426 inode = btrfs_iget_locked(extent_root->fs_info->sb,
3427 ref_objectid, found_root);
3428 if (inode->i_state & I_NEW) {
3429 /* the inode and parent dir are two different roots */
3430 BTRFS_I(inode)->root = found_root;
3431 BTRFS_I(inode)->location.objectid = ref_objectid;
3432 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
3433 BTRFS_I(inode)->location.offset = 0;
3434 btrfs_read_locked_inode(inode);
3435 unlock_new_inode(inode);
3436
3437 }
3438 /* this can happen if the reference is not against
3439 * the latest version of the tree root
3440 */
Chris Mason7d9eb122008-07-08 14:19:17 -04003441 if (is_bad_inode(inode))
Chris Masonedbd8d42007-12-21 16:27:24 -05003442 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04003443
Chris Mason0ef3e662008-05-24 14:04:53 -04003444 *last_file_objectid = inode->i_ino;
3445 *last_file_root = found_root->root_key.objectid;
3446 *last_file_offset = ref_offset;
3447
Chris Masonedbd8d42007-12-21 16:27:24 -05003448 relocate_inode_pages(inode, ref_offset, extent_key->offset);
Chris Masonedbd8d42007-12-21 16:27:24 -05003449 iput(inode);
Chris Masonedbd8d42007-12-21 16:27:24 -05003450 } else {
3451 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05003452 struct extent_buffer *eb;
Chris Mason7d9eb122008-07-08 14:19:17 -04003453 int needs_lock = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003454
Chris Masonedbd8d42007-12-21 16:27:24 -05003455 eb = read_tree_block(found_root, extent_key->objectid,
Chris Masonca7a79a2008-05-12 12:59:19 -04003456 extent_key->offset, 0);
Chris Mason925baed2008-06-25 16:01:30 -04003457 btrfs_tree_lock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05003458 level = btrfs_header_level(eb);
3459
3460 if (level == 0)
3461 btrfs_item_key_to_cpu(eb, &found_key, 0);
3462 else
3463 btrfs_node_key_to_cpu(eb, &found_key, 0);
3464
Chris Mason925baed2008-06-25 16:01:30 -04003465 btrfs_tree_unlock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05003466 free_extent_buffer(eb);
3467
Chris Masonbf4ef672008-05-08 13:26:18 -04003468 ret = find_root_for_ref(extent_root, path, &found_key,
3469 level, 0, &found_root,
3470 extent_key->objectid);
3471
3472 if (ret)
3473 goto out;
3474
Chris Mason7d9eb122008-07-08 14:19:17 -04003475 /*
3476 * right here almost anything could happen to our key,
3477 * but that's ok. The cow below will either relocate it
3478 * or someone else will have relocated it. Either way,
3479 * it is in a different spot than it was before and
3480 * we're happy.
3481 */
3482
Chris Masonbf4ef672008-05-08 13:26:18 -04003483 trans = btrfs_start_transaction(found_root, 1);
3484
Chris Mason7d9eb122008-07-08 14:19:17 -04003485 if (found_root == extent_root->fs_info->extent_root ||
3486 found_root == extent_root->fs_info->chunk_root ||
3487 found_root == extent_root->fs_info->dev_root) {
3488 needs_lock = 1;
3489 mutex_lock(&extent_root->fs_info->alloc_mutex);
3490 }
3491
Chris Masonedbd8d42007-12-21 16:27:24 -05003492 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05003493 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05003494 ret = btrfs_search_slot(trans, found_root, &found_key, path,
3495 0, 1);
3496 path->lowest_level = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003497 btrfs_release_path(found_root, path);
Chris Mason7d9eb122008-07-08 14:19:17 -04003498
Chris Mason0ef3e662008-05-24 14:04:53 -04003499 if (found_root == found_root->fs_info->extent_root)
3500 btrfs_extent_post_op(trans, found_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003501 if (needs_lock)
3502 mutex_unlock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003503
Chris Mason7d9eb122008-07-08 14:19:17 -04003504 btrfs_end_transaction(trans, found_root);
3505
3506 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003507out:
Chris Mason7d9eb122008-07-08 14:19:17 -04003508 mutex_lock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003509 return 0;
3510}
3511
Chris Masona061fc82008-05-07 11:43:44 -04003512static int noinline del_extent_zero(struct btrfs_root *extent_root,
3513 struct btrfs_path *path,
3514 struct btrfs_key *extent_key)
3515{
3516 int ret;
3517 struct btrfs_trans_handle *trans;
3518
3519 trans = btrfs_start_transaction(extent_root, 1);
3520 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
3521 if (ret > 0) {
3522 ret = -EIO;
3523 goto out;
3524 }
3525 if (ret < 0)
3526 goto out;
3527 ret = btrfs_del_item(trans, extent_root, path);
3528out:
3529 btrfs_end_transaction(trans, extent_root);
3530 return ret;
3531}
3532
Chris Mason98ed5172008-01-03 10:01:48 -05003533static int noinline relocate_one_extent(struct btrfs_root *extent_root,
3534 struct btrfs_path *path,
3535 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05003536{
3537 struct btrfs_key key;
3538 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05003539 struct extent_buffer *leaf;
Chris Mason0ef3e662008-05-24 14:04:53 -04003540 u64 last_file_objectid = 0;
3541 u64 last_file_root = 0;
3542 u64 last_file_offset = (u64)-1;
3543 u64 last_extent = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003544 u32 nritems;
3545 u32 item_size;
3546 int ret = 0;
3547
Chris Masona061fc82008-05-07 11:43:44 -04003548 if (extent_key->objectid == 0) {
3549 ret = del_extent_zero(extent_root, path, extent_key);
3550 goto out;
3551 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003552 key.objectid = extent_key->objectid;
3553 key.type = BTRFS_EXTENT_REF_KEY;
3554 key.offset = 0;
3555
3556 while(1) {
3557 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3558
Chris Masonedbd8d42007-12-21 16:27:24 -05003559 if (ret < 0)
3560 goto out;
3561
3562 ret = 0;
3563 leaf = path->nodes[0];
3564 nritems = btrfs_header_nritems(leaf);
Chris Masona061fc82008-05-07 11:43:44 -04003565 if (path->slots[0] == nritems) {
3566 ret = btrfs_next_leaf(extent_root, path);
3567 if (ret > 0) {
3568 ret = 0;
3569 goto out;
3570 }
3571 if (ret < 0)
3572 goto out;
Chris Masonbf4ef672008-05-08 13:26:18 -04003573 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04003574 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003575
3576 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masona061fc82008-05-07 11:43:44 -04003577 if (found_key.objectid != extent_key->objectid) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003578 break;
Chris Masona061fc82008-05-07 11:43:44 -04003579 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003580
Chris Masona061fc82008-05-07 11:43:44 -04003581 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003582 break;
Chris Masona061fc82008-05-07 11:43:44 -04003583 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003584
3585 key.offset = found_key.offset + 1;
3586 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3587
Chris Mason0ef3e662008-05-24 14:04:53 -04003588 ret = relocate_one_reference(extent_root, path, extent_key,
3589 &last_file_objectid,
3590 &last_file_offset,
3591 &last_file_root, last_extent);
Chris Masonedbd8d42007-12-21 16:27:24 -05003592 if (ret)
3593 goto out;
Chris Mason0ef3e662008-05-24 14:04:53 -04003594 last_extent = extent_key->objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05003595 }
3596 ret = 0;
3597out:
3598 btrfs_release_path(extent_root, path);
3599 return ret;
3600}
3601
Chris Masonec44a352008-04-28 15:29:52 -04003602static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
3603{
3604 u64 num_devices;
3605 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
3606 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
3607
Chris Masona061fc82008-05-07 11:43:44 -04003608 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04003609 if (num_devices == 1) {
3610 stripped |= BTRFS_BLOCK_GROUP_DUP;
3611 stripped = flags & ~stripped;
3612
3613 /* turn raid0 into single device chunks */
3614 if (flags & BTRFS_BLOCK_GROUP_RAID0)
3615 return stripped;
3616
3617 /* turn mirroring into duplication */
3618 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
3619 BTRFS_BLOCK_GROUP_RAID10))
3620 return stripped | BTRFS_BLOCK_GROUP_DUP;
3621 return flags;
3622 } else {
3623 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04003624 if (flags & stripped)
3625 return flags;
3626
3627 stripped |= BTRFS_BLOCK_GROUP_DUP;
3628 stripped = flags & ~stripped;
3629
3630 /* switch duplicated blocks with raid1 */
3631 if (flags & BTRFS_BLOCK_GROUP_DUP)
3632 return stripped | BTRFS_BLOCK_GROUP_RAID1;
3633
3634 /* turn single device chunks into raid0 */
3635 return stripped | BTRFS_BLOCK_GROUP_RAID0;
3636 }
3637 return flags;
3638}
3639
Chris Mason0ef3e662008-05-24 14:04:53 -04003640int __alloc_chunk_for_shrink(struct btrfs_root *root,
3641 struct btrfs_block_group_cache *shrink_block_group,
3642 int force)
3643{
3644 struct btrfs_trans_handle *trans;
3645 u64 new_alloc_flags;
3646 u64 calc;
3647
Chris Masonc286ac42008-07-22 23:06:41 -04003648 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04003649 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04003650 spin_unlock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04003651 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003652
Chris Mason0ef3e662008-05-24 14:04:53 -04003653 trans = btrfs_start_transaction(root, 1);
Chris Mason7d9eb122008-07-08 14:19:17 -04003654 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003655 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04003656
Chris Mason0ef3e662008-05-24 14:04:53 -04003657 new_alloc_flags = update_block_group_flags(root,
3658 shrink_block_group->flags);
3659 if (new_alloc_flags != shrink_block_group->flags) {
3660 calc =
3661 btrfs_block_group_used(&shrink_block_group->item);
3662 } else {
3663 calc = shrink_block_group->key.offset;
3664 }
Chris Masonc286ac42008-07-22 23:06:41 -04003665 spin_unlock(&shrink_block_group->lock);
3666
Chris Mason0ef3e662008-05-24 14:04:53 -04003667 do_chunk_alloc(trans, root->fs_info->extent_root,
3668 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04003669
3670 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0ef3e662008-05-24 14:04:53 -04003671 btrfs_end_transaction(trans, root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003672 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003673 } else
3674 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04003675 return 0;
3676}
3677
Chris Mason8f18cf12008-04-25 16:53:30 -04003678int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05003679{
3680 struct btrfs_trans_handle *trans;
3681 struct btrfs_root *tree_root = root->fs_info->tree_root;
3682 struct btrfs_path *path;
3683 u64 cur_byte;
3684 u64 total_found;
Chris Mason8f18cf12008-04-25 16:53:30 -04003685 u64 shrink_last_byte;
3686 struct btrfs_block_group_cache *shrink_block_group;
Chris Masonedbd8d42007-12-21 16:27:24 -05003687 struct btrfs_key key;
Yan73e48b22008-01-03 14:14:39 -05003688 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05003689 struct extent_buffer *leaf;
3690 u32 nritems;
3691 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04003692 int progress;
Chris Masonedbd8d42007-12-21 16:27:24 -05003693
Chris Mason925baed2008-06-25 16:01:30 -04003694 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04003695 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
3696 shrink_start);
3697 BUG_ON(!shrink_block_group);
3698
Chris Mason0ef3e662008-05-24 14:04:53 -04003699 shrink_last_byte = shrink_block_group->key.objectid +
3700 shrink_block_group->key.offset;
Chris Mason8f18cf12008-04-25 16:53:30 -04003701
3702 shrink_block_group->space_info->total_bytes -=
3703 shrink_block_group->key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05003704 path = btrfs_alloc_path();
3705 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05003706 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05003707
Chris Mason323da792008-05-09 11:46:48 -04003708 printk("btrfs relocating block group %llu flags %llu\n",
3709 (unsigned long long)shrink_start,
3710 (unsigned long long)shrink_block_group->flags);
3711
Chris Mason0ef3e662008-05-24 14:04:53 -04003712 __alloc_chunk_for_shrink(root, shrink_block_group, 1);
Chris Mason323da792008-05-09 11:46:48 -04003713
Chris Mason0ef3e662008-05-24 14:04:53 -04003714again:
3715
Chris Mason8f18cf12008-04-25 16:53:30 -04003716 shrink_block_group->ro = 1;
3717
Chris Masonedbd8d42007-12-21 16:27:24 -05003718 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04003719 progress = 0;
Chris Mason8f18cf12008-04-25 16:53:30 -04003720 key.objectid = shrink_start;
Chris Masonedbd8d42007-12-21 16:27:24 -05003721 key.offset = 0;
3722 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05003723 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05003724
Chris Masonea8c2812008-08-04 23:17:27 -04003725 mutex_unlock(&root->fs_info->alloc_mutex);
3726
3727 btrfs_start_delalloc_inodes(root);
Yan Zheng7ea394f2008-08-05 13:05:02 -04003728 btrfs_wait_ordered_extents(tree_root, 0);
Chris Masonea8c2812008-08-04 23:17:27 -04003729
3730 mutex_lock(&root->fs_info->alloc_mutex);
3731
Yan73e48b22008-01-03 14:14:39 -05003732 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3733 if (ret < 0)
3734 goto out;
3735
Chris Mason0b86a832008-03-24 15:01:56 -04003736 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yan73e48b22008-01-03 14:14:39 -05003737 if (ret < 0)
3738 goto out;
Chris Mason8f18cf12008-04-25 16:53:30 -04003739
Yan73e48b22008-01-03 14:14:39 -05003740 if (ret == 0) {
3741 leaf = path->nodes[0];
3742 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003743 if (found_key.objectid + found_key.offset > shrink_start &&
3744 found_key.objectid < shrink_last_byte) {
Yan73e48b22008-01-03 14:14:39 -05003745 cur_byte = found_key.objectid;
3746 key.objectid = cur_byte;
3747 }
3748 }
3749 btrfs_release_path(root, path);
3750
3751 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003752 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3753 if (ret < 0)
3754 goto out;
Yan73e48b22008-01-03 14:14:39 -05003755
Chris Mason7d9eb122008-07-08 14:19:17 -04003756next:
Chris Masonedbd8d42007-12-21 16:27:24 -05003757 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05003758 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05003759 if (path->slots[0] >= nritems) {
3760 ret = btrfs_next_leaf(root, path);
3761 if (ret < 0)
3762 goto out;
3763 if (ret == 1) {
3764 ret = 0;
3765 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05003766 }
Yan73e48b22008-01-03 14:14:39 -05003767 leaf = path->nodes[0];
3768 nritems = btrfs_header_nritems(leaf);
3769 }
3770
3771 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05003772
Chris Mason8f18cf12008-04-25 16:53:30 -04003773 if (found_key.objectid >= shrink_last_byte)
3774 break;
3775
Chris Mason725c8462008-01-04 16:47:16 -05003776 if (progress && need_resched()) {
3777 memcpy(&key, &found_key, sizeof(key));
Chris Mason725c8462008-01-04 16:47:16 -05003778 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05003779 btrfs_release_path(root, path);
3780 btrfs_search_slot(NULL, root, &key, path, 0, 0);
3781 progress = 0;
3782 goto next;
3783 }
3784 progress = 1;
3785
Yan73e48b22008-01-03 14:14:39 -05003786 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
3787 found_key.objectid + found_key.offset <= cur_byte) {
Chris Mason0ef3e662008-05-24 14:04:53 -04003788 memcpy(&key, &found_key, sizeof(key));
3789 key.offset++;
Yan73e48b22008-01-03 14:14:39 -05003790 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003791 goto next;
3792 }
Yan73e48b22008-01-03 14:14:39 -05003793
Chris Masonedbd8d42007-12-21 16:27:24 -05003794 total_found++;
3795 cur_byte = found_key.objectid + found_key.offset;
3796 key.objectid = cur_byte;
3797 btrfs_release_path(root, path);
3798 ret = relocate_one_extent(root, path, &found_key);
Chris Mason0ef3e662008-05-24 14:04:53 -04003799 __alloc_chunk_for_shrink(root, shrink_block_group, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003800 }
3801
3802 btrfs_release_path(root, path);
3803
3804 if (total_found > 0) {
Chris Mason323da792008-05-09 11:46:48 -04003805 printk("btrfs relocate found %llu last extent was %llu\n",
3806 (unsigned long long)total_found,
3807 (unsigned long long)found_key.objectid);
Chris Mason7d9eb122008-07-08 14:19:17 -04003808 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003809 trans = btrfs_start_transaction(tree_root, 1);
3810 btrfs_commit_transaction(trans, tree_root);
3811
Chris Masonedbd8d42007-12-21 16:27:24 -05003812 btrfs_clean_old_snapshots(tree_root);
Chris Masonedbd8d42007-12-21 16:27:24 -05003813
Chris Masonea8c2812008-08-04 23:17:27 -04003814 btrfs_start_delalloc_inodes(root);
Yan Zheng7ea394f2008-08-05 13:05:02 -04003815 btrfs_wait_ordered_extents(tree_root, 0);
Chris Mason3eaa2882008-07-24 11:57:52 -04003816
Chris Masonedbd8d42007-12-21 16:27:24 -05003817 trans = btrfs_start_transaction(tree_root, 1);
3818 btrfs_commit_transaction(trans, tree_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003819 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003820 goto again;
3821 }
3822
Chris Mason8f18cf12008-04-25 16:53:30 -04003823 /*
3824 * we've freed all the extents, now remove the block
3825 * group item from the tree
3826 */
Chris Mason7d9eb122008-07-08 14:19:17 -04003827 mutex_unlock(&root->fs_info->alloc_mutex);
3828
Chris Masonedbd8d42007-12-21 16:27:24 -05003829 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04003830
Chris Mason7d9eb122008-07-08 14:19:17 -04003831 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04003832 memcpy(&key, &shrink_block_group->key, sizeof(key));
Chris Mason4313b392008-01-03 09:08:48 -05003833
Chris Mason8f18cf12008-04-25 16:53:30 -04003834 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3835 if (ret > 0)
3836 ret = -EIO;
Josef Bacik8e8a1e32008-07-24 12:17:14 -04003837 if (ret < 0) {
3838 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003839 goto out;
Josef Bacik8e8a1e32008-07-24 12:17:14 -04003840 }
Yan73e48b22008-01-03 14:14:39 -05003841
Josef Bacik0f9dd462008-09-23 13:14:11 -04003842 spin_lock(&root->fs_info->block_group_cache_lock);
3843 rb_erase(&shrink_block_group->cache_node,
3844 &root->fs_info->block_group_cache_tree);
3845 spin_unlock(&root->fs_info->block_group_cache_lock);
Yan73e48b22008-01-03 14:14:39 -05003846
Josef Bacik0f9dd462008-09-23 13:14:11 -04003847 ret = btrfs_remove_free_space(shrink_block_group, key.objectid,
3848 key.offset);
3849 if (ret) {
3850 btrfs_end_transaction(trans, root);
3851 goto out;
3852 }
Chris Masond7a029a2008-08-04 23:17:26 -04003853 /*
Chris Mason0ef3e662008-05-24 14:04:53 -04003854 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
3855 kfree(shrink_block_group);
Chris Masond7a029a2008-08-04 23:17:26 -04003856 */
Chris Mason0ef3e662008-05-24 14:04:53 -04003857
Chris Mason8f18cf12008-04-25 16:53:30 -04003858 btrfs_del_item(trans, root, path);
Chris Mason7d9eb122008-07-08 14:19:17 -04003859 btrfs_release_path(root, path);
3860 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003861 btrfs_commit_transaction(trans, root);
Chris Mason0ef3e662008-05-24 14:04:53 -04003862
Chris Mason7d9eb122008-07-08 14:19:17 -04003863 mutex_lock(&root->fs_info->alloc_mutex);
3864
Chris Mason0ef3e662008-05-24 14:04:53 -04003865 /* the code to unpin extents might set a few bits in the free
3866 * space cache for this range again
3867 */
Josef Bacik0f9dd462008-09-23 13:14:11 -04003868 /* XXX? */
3869 ret = btrfs_remove_free_space(shrink_block_group, key.objectid,
3870 key.offset);
Chris Masonedbd8d42007-12-21 16:27:24 -05003871out:
3872 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003873 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003874 return ret;
3875}
3876
Chris Mason0b86a832008-03-24 15:01:56 -04003877int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3878 struct btrfs_key *key)
3879{
Chris Mason925baed2008-06-25 16:01:30 -04003880 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003881 struct btrfs_key found_key;
3882 struct extent_buffer *leaf;
3883 int slot;
3884
3885 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3886 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003887 goto out;
3888
Chris Mason0b86a832008-03-24 15:01:56 -04003889 while(1) {
3890 slot = path->slots[0];
3891 leaf = path->nodes[0];
3892 if (slot >= btrfs_header_nritems(leaf)) {
3893 ret = btrfs_next_leaf(root, path);
3894 if (ret == 0)
3895 continue;
3896 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003897 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04003898 break;
3899 }
3900 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3901
3902 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04003903 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3904 ret = 0;
3905 goto out;
3906 }
Chris Mason0b86a832008-03-24 15:01:56 -04003907 path->slots[0]++;
3908 }
3909 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04003910out:
Chris Mason0b86a832008-03-24 15:01:56 -04003911 return ret;
3912}
3913
Chris Mason9078a3e2007-04-26 16:46:15 -04003914int btrfs_read_block_groups(struct btrfs_root *root)
3915{
3916 struct btrfs_path *path;
3917 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003918 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04003919 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003920 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04003921 struct btrfs_key key;
3922 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04003923 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04003924
Chris Masonbe744172007-05-06 10:15:01 -04003925 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04003926 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003927 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003928 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04003929 path = btrfs_alloc_path();
3930 if (!path)
3931 return -ENOMEM;
3932
Chris Mason925baed2008-06-25 16:01:30 -04003933 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04003934 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003935 ret = find_first_block_group(root, path, &key);
3936 if (ret > 0) {
3937 ret = 0;
3938 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04003939 }
Chris Mason0b86a832008-03-24 15:01:56 -04003940 if (ret != 0)
3941 goto error;
3942
Chris Mason5f39d392007-10-15 16:14:19 -04003943 leaf = path->nodes[0];
3944 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003945 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04003946 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04003947 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04003948 break;
3949 }
Chris Mason3e1ad542007-05-07 20:03:49 -04003950
Chris Masonc286ac42008-07-22 23:06:41 -04003951 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003952 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04003953 read_extent_buffer(leaf, &cache->item,
3954 btrfs_item_ptr_offset(leaf, path->slots[0]),
3955 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04003956 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04003957
Chris Mason9078a3e2007-04-26 16:46:15 -04003958 key.objectid = found_key.objectid + found_key.offset;
3959 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04003960 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04003961
Chris Mason6324fbf2008-03-24 15:01:59 -04003962 ret = update_space_info(info, cache->flags, found_key.offset,
3963 btrfs_block_group_used(&cache->item),
3964 &space_info);
3965 BUG_ON(ret);
3966 cache->space_info = space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003967 spin_lock(&space_info->lock);
3968 list_add(&cache->list, &space_info->block_groups);
3969 spin_unlock(&space_info->lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04003970
Josef Bacik0f9dd462008-09-23 13:14:11 -04003971 ret = btrfs_add_block_group_cache(root->fs_info, cache);
3972 BUG_ON(ret);
3973
Chris Mason9078a3e2007-04-26 16:46:15 -04003974 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04003975 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04003976 break;
3977 }
Chris Mason0b86a832008-03-24 15:01:56 -04003978 ret = 0;
3979error:
Chris Mason9078a3e2007-04-26 16:46:15 -04003980 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003981 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04003982 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003983}
Chris Mason6324fbf2008-03-24 15:01:59 -04003984
3985int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3986 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04003987 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04003988 u64 size)
3989{
3990 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003991 struct btrfs_root *extent_root;
3992 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04003993
Chris Mason7d9eb122008-07-08 14:19:17 -04003994 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason6324fbf2008-03-24 15:01:59 -04003995 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04003996
Chris Masone02119d2008-09-05 16:13:11 -04003997 root->fs_info->last_trans_new_blockgroup = trans->transid;
3998
Chris Mason8f18cf12008-04-25 16:53:30 -04003999 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004000 if (!cache)
4001 return -ENOMEM;
4002
Chris Masone17cade2008-04-15 15:41:47 -04004003 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04004004 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04004005 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004006 INIT_LIST_HEAD(&cache->list);
Chris Mason6324fbf2008-03-24 15:01:59 -04004007 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04004008
Chris Mason6324fbf2008-03-24 15:01:59 -04004009 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04004010 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
4011 cache->flags = type;
4012 btrfs_set_block_group_flags(&cache->item, type);
4013
4014 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
4015 &cache->space_info);
4016 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004017 spin_lock(&cache->space_info->lock);
4018 list_add(&cache->list, &cache->space_info->block_groups);
4019 spin_unlock(&cache->space_info->lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04004020
Josef Bacik0f9dd462008-09-23 13:14:11 -04004021 ret = btrfs_add_block_group_cache(root->fs_info, cache);
4022 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04004023
Chris Mason6324fbf2008-03-24 15:01:59 -04004024 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
4025 sizeof(cache->item));
4026 BUG_ON(ret);
4027
4028 finish_current_insert(trans, extent_root);
4029 ret = del_pending_extents(trans, extent_root);
4030 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04004031 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04004032
Chris Mason6324fbf2008-03-24 15:01:59 -04004033 return 0;
4034}