blob: 9ab099bc01a41f0066a28616dc662ffdc0b47043 [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 Mason0b86a832008-03-24 15:01:56 -0400328 u64 search_start = *start_ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400329
Chris Mason7d9eb122008-07-08 14:19:17 -0400330 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason0ef3e662008-05-24 14:04:53 -0400331 if (!cache)
Chris Mason54aa1f42007-06-22 14:16:25 -0400332 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500333
Josef Bacik0f9dd462008-09-23 13:14:11 -0400334 last = max(search_start, cache->key.objectid);
335
Chris Mason0ef3e662008-05-24 14:04:53 -0400336again:
337 ret = cache_block_group(root, cache);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400338 if (ret)
Chris Mason0ef3e662008-05-24 14:04:53 -0400339 goto out;
Chris Masone19caa52007-10-15 16:17:44 -0400340
Josef Bacik0f9dd462008-09-23 13:14:11 -0400341 if (cache->ro || !block_group_bits(cache, data))
Chris Mason0ef3e662008-05-24 14:04:53 -0400342 goto new_group;
343
Josef Bacik0f9dd462008-09-23 13:14:11 -0400344 info = btrfs_find_free_space(cache, last, num);
345 if (info) {
346 *start_ret = info->offset;
Chris Mason0b86a832008-03-24 15:01:56 -0400347 return 0;
Chris Mason8790d502008-04-03 16:29:03 -0400348 }
Chris Masone37c9e62007-05-09 20:13:14 -0400349
350new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400351 last = cache->key.objectid + cache->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400352
Chris Mason0ef3e662008-05-24 14:04:53 -0400353 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Zheng Yane8569812008-09-26 10:05:48 -0400354 if (!cache)
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500355 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400356
Chris Masone37c9e62007-05-09 20:13:14 -0400357 *cache_ret = cache;
358 goto again;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400359
360out:
361 return -ENOSPC;
Chris Masone37c9e62007-05-09 20:13:14 -0400362}
363
Chris Mason84f54cf2007-06-12 07:43:08 -0400364static u64 div_factor(u64 num, int factor)
365{
Chris Mason257d0ce2007-11-07 21:08:16 -0500366 if (factor == 10)
367 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400368 num *= factor;
369 do_div(num, 10);
370 return num;
371}
372
Josef Bacik0f9dd462008-09-23 13:14:11 -0400373static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
374 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400375{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400376 struct list_head *head = &info->space_info;
377 struct list_head *cur;
378 struct btrfs_space_info *found;
379 list_for_each(cur, head) {
380 found = list_entry(cur, struct btrfs_space_info, list);
381 if (found->flags == flags)
382 return found;
383 }
384 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400385}
386
Chris Mason925baed2008-06-25 16:01:30 -0400387static struct btrfs_block_group_cache *
388__btrfs_find_block_group(struct btrfs_root *root,
389 struct btrfs_block_group_cache *hint,
390 u64 search_start, int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400391{
Chris Mason96b51792007-10-15 16:15:19 -0400392 struct btrfs_block_group_cache *cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400393 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400394 struct btrfs_fs_info *info = root->fs_info;
395 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400396 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400397 u64 free_check;
Chris Mason31f3c992007-04-30 15:25:45 -0400398 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400399 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400400 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400401
Chris Masona236aed2008-04-29 09:38:00 -0400402 if (data & BTRFS_BLOCK_GROUP_METADATA)
403 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400404
Chris Mason0ef3e662008-05-24 14:04:53 -0400405 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400406 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400407 shint = btrfs_lookup_first_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400408 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonc286ac42008-07-22 23:06:41 -0400409 spin_lock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400410 used = btrfs_block_group_used(&shint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400411 if (used + shint->pinned + shint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500412 div_factor(shint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400413 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400414 return shint;
415 }
Chris Masonc286ac42008-07-22 23:06:41 -0400416 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400417 }
418 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400419 if (hint && !hint->ro && block_group_bits(hint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400420 spin_lock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400421 used = btrfs_block_group_used(&hint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400422 if (used + hint->pinned + hint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500423 div_factor(hint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400424 spin_unlock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400425 return hint;
426 }
Chris Masonc286ac42008-07-22 23:06:41 -0400427 spin_unlock(&hint->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400428 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400429 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400430 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400431 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400432 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400433 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400434 }
Chris Mason31f3c992007-04-30 15:25:45 -0400435again:
Zheng Yane8569812008-09-26 10:05:48 -0400436 while (1) {
437 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400438 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400439 break;
Chris Mason96b51792007-10-15 16:15:19 -0400440
Chris Masonc286ac42008-07-22 23:06:41 -0400441 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400442 last = cache->key.objectid + cache->key.offset;
443 used = btrfs_block_group_used(&cache->item);
444
Chris Mason8f18cf12008-04-25 16:53:30 -0400445 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400446 free_check = div_factor(cache->key.offset, factor);
Zheng Yane8569812008-09-26 10:05:48 -0400447 if (used + cache->pinned + cache->reserved <
448 free_check) {
Chris Mason8790d502008-04-03 16:29:03 -0400449 found_group = cache;
Chris Masonc286ac42008-07-22 23:06:41 -0400450 spin_unlock(&cache->lock);
Chris Mason8790d502008-04-03 16:29:03 -0400451 goto found;
452 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400453 }
Chris Masonc286ac42008-07-22 23:06:41 -0400454 spin_unlock(&cache->lock);
Chris Masonde428b62007-05-18 13:28:27 -0400455 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400456 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400457 if (!wrapped) {
458 last = search_start;
459 wrapped = 1;
460 goto again;
461 }
462 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400463 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400464 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400465 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400466 goto again;
467 }
Chris Masonbe744172007-05-06 10:15:01 -0400468found:
Chris Mason31f3c992007-04-30 15:25:45 -0400469 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400470}
471
Chris Mason925baed2008-06-25 16:01:30 -0400472struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
473 struct btrfs_block_group_cache
474 *hint, u64 search_start,
475 int data, int owner)
476{
477
478 struct btrfs_block_group_cache *ret;
Chris Mason925baed2008-06-25 16:01:30 -0400479 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
Chris Mason925baed2008-06-25 16:01:30 -0400480 return ret;
481}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400482
Chris Masone02119d2008-09-05 16:13:11 -0400483/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400484int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400485{
486 int ret;
487 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400488 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400489
Zheng Yan31840ae2008-09-23 13:14:14 -0400490 path = btrfs_alloc_path();
491 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400492 maybe_lock_mutex(root);
493 key.objectid = start;
494 key.offset = len;
495 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
496 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
497 0, 0);
498 maybe_unlock_mutex(root);
Zheng Yan31840ae2008-09-23 13:14:14 -0400499 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500500 return ret;
501}
502
Chris Masond8d5f3e2007-12-11 12:42:00 -0500503/*
504 * Back reference rules. Back refs have three main goals:
505 *
506 * 1) differentiate between all holders of references to an extent so that
507 * when a reference is dropped we can make sure it was a valid reference
508 * before freeing the extent.
509 *
510 * 2) Provide enough information to quickly find the holders of an extent
511 * if we notice a given block is corrupted or bad.
512 *
513 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
514 * maintenance. This is actually the same as #2, but with a slightly
515 * different use case.
516 *
517 * File extents can be referenced by:
518 *
519 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400520 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500521 * - different offsets inside a file (bookend extents in file.c)
522 *
523 * The extent ref structure has fields for:
524 *
525 * - Objectid of the subvolume root
526 * - Generation number of the tree holding the reference
527 * - objectid of the file holding the reference
528 * - offset in the file corresponding to the key holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400529 * - number of references holding by parent node (alway 1 for tree blocks)
530 *
531 * Btree leaf may hold multiple references to a file extent. In most cases,
532 * these references are from same file and the corresponding offsets inside
533 * the file are close together. So inode objectid and offset in file are
534 * just hints, they provide hints about where in the btree the references
535 * can be found and when we can stop searching.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500536 *
537 * When a file extent is allocated the fields are filled in:
Zheng Yan31840ae2008-09-23 13:14:14 -0400538 * (root_key.objectid, trans->transid, inode objectid, offset in file, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500539 *
540 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400541 * in the leaf. It looks similar to the create case, but trans->transid will
542 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500543 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400544 * (root_key.objectid, trans->transid, inode objectid, offset in file,
545 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500546 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400547 * Because inode objectid and offset in file are just hints, they are not
548 * used when backrefs are deleted. When a file extent is removed either
549 * during snapshot deletion or file truncation, we find the corresponding
550 * back back reference and check the following fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500551 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400552 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf))
Chris Masond8d5f3e2007-12-11 12:42:00 -0500553 *
554 * Btree extents can be referenced by:
555 *
556 * - Different subvolumes
557 * - Different generations of the same subvolume
558 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500559 * When a tree block is created, back references are inserted:
560 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400561 * (root->root_key.objectid, trans->transid, level, 0, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500562 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400563 * When a tree block is cow'd, new back references are added for all the
564 * blocks it points to. If the tree block isn't in reference counted root,
565 * the old back references are removed. These new back references are of
566 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500567 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400568 * (root->root_key.objectid, trans->transid, level, 0, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500569 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400570 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500571 *
572 * if backref was for a tree root:
Zheng Yan31840ae2008-09-23 13:14:14 -0400573 * (btrfs_header_owner(itself), btrfs_header_generation(itself))
Chris Masond8d5f3e2007-12-11 12:42:00 -0500574 * else
Zheng Yan31840ae2008-09-23 13:14:14 -0400575 * (btrfs_header_owner(parent), btrfs_header_generation(parent))
Chris Masond8d5f3e2007-12-11 12:42:00 -0500576 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400577 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500578 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400579 * The key objectid corresponds to the first byte in the extent, the key
580 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
581 * byte of parent extent. If a extent is tree root, the key offset is set
582 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500583 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400584
585static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
586 struct btrfs_root *root,
587 struct btrfs_path *path, u64 bytenr,
588 u64 parent, u64 ref_root,
589 u64 ref_generation, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500590{
Chris Mason74493f72007-12-11 09:25:06 -0500591 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400592 struct btrfs_extent_ref *ref;
593 struct extent_buffer *leaf;
Chris Mason74493f72007-12-11 09:25:06 -0500594 int ret;
595
Chris Mason74493f72007-12-11 09:25:06 -0500596 key.objectid = bytenr;
597 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400598 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500599
Zheng Yan31840ae2008-09-23 13:14:14 -0400600 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
601 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500602 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400603 if (ret > 0) {
604 ret = -ENOENT;
605 goto out;
606 }
607
608 leaf = path->nodes[0];
609 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
610 if (btrfs_ref_root(leaf, ref) != ref_root ||
611 btrfs_ref_generation(leaf, ref) != ref_generation) {
612 ret = -EIO;
613 WARN_ON(1);
614 goto out;
615 }
616 ret = 0;
617out:
618 return ret;
619}
620
621static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
622 struct btrfs_root *root,
623 struct btrfs_path *path,
624 u64 bytenr, u64 parent,
625 u64 ref_root, u64 ref_generation,
626 u64 owner_objectid, u64 owner_offset)
627{
628 struct btrfs_key key;
629 struct extent_buffer *leaf;
630 struct btrfs_extent_ref *ref;
631 u32 num_refs;
632 int ret;
633
634 key.objectid = bytenr;
635 key.type = BTRFS_EXTENT_REF_KEY;
636 key.offset = parent;
637
638 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
639 if (ret == 0) {
640 leaf = path->nodes[0];
641 ref = btrfs_item_ptr(leaf, path->slots[0],
642 struct btrfs_extent_ref);
643 btrfs_set_ref_root(leaf, ref, ref_root);
644 btrfs_set_ref_generation(leaf, ref, ref_generation);
645 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
646 btrfs_set_ref_offset(leaf, ref, owner_offset);
647 btrfs_set_ref_num_refs(leaf, ref, 1);
648 } else if (ret == -EEXIST) {
649 u64 existing_owner;
650 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
651 leaf = path->nodes[0];
652 ref = btrfs_item_ptr(leaf, path->slots[0],
653 struct btrfs_extent_ref);
654 if (btrfs_ref_root(leaf, ref) != ref_root ||
655 btrfs_ref_generation(leaf, ref) != ref_generation) {
656 ret = -EIO;
657 WARN_ON(1);
658 goto out;
659 }
660
661 num_refs = btrfs_ref_num_refs(leaf, ref);
662 BUG_ON(num_refs == 0);
663 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
664
665 existing_owner = btrfs_ref_objectid(leaf, ref);
666 if (existing_owner == owner_objectid &&
667 btrfs_ref_offset(leaf, ref) > owner_offset) {
668 btrfs_set_ref_offset(leaf, ref, owner_offset);
669 } else if (existing_owner != owner_objectid &&
670 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
671 btrfs_set_ref_objectid(leaf, ref,
672 BTRFS_MULTIPLE_OBJECTIDS);
673 btrfs_set_ref_offset(leaf, ref, 0);
674 }
675 ret = 0;
676 } else {
677 goto out;
678 }
Chris Mason7bb86312007-12-11 09:25:06 -0500679 btrfs_mark_buffer_dirty(path->nodes[0]);
680out:
681 btrfs_release_path(root, path);
682 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500683}
684
Zheng Yan31840ae2008-09-23 13:14:14 -0400685static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
686 struct btrfs_root *root,
687 struct btrfs_path *path)
688{
689 struct extent_buffer *leaf;
690 struct btrfs_extent_ref *ref;
691 u32 num_refs;
692 int ret = 0;
693
694 leaf = path->nodes[0];
695 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
696 num_refs = btrfs_ref_num_refs(leaf, ref);
697 BUG_ON(num_refs == 0);
698 num_refs -= 1;
699 if (num_refs == 0) {
700 ret = btrfs_del_item(trans, root, path);
701 } else {
702 btrfs_set_ref_num_refs(leaf, ref, num_refs);
703 btrfs_mark_buffer_dirty(leaf);
704 }
705 btrfs_release_path(root, path);
706 return ret;
707}
708
709static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
710 struct btrfs_root *root, u64 bytenr,
711 u64 orig_parent, u64 parent,
712 u64 orig_root, u64 ref_root,
713 u64 orig_generation, u64 ref_generation,
714 u64 owner_objectid, u64 owner_offset)
715{
716 int ret;
717 struct btrfs_root *extent_root = root->fs_info->extent_root;
718 struct btrfs_path *path;
719
720 if (root == root->fs_info->extent_root) {
721 struct pending_extent_op *extent_op;
722 u64 num_bytes;
723
724 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
725 num_bytes = btrfs_level_size(root, (int)owner_objectid);
726 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
727 bytenr + num_bytes - 1, EXTENT_LOCKED, 0)) {
728 u64 priv;
729 ret = get_state_private(&root->fs_info->extent_ins,
730 bytenr, &priv);
731 BUG_ON(ret);
732 extent_op = (struct pending_extent_op *)
733 (unsigned long)priv;
734 BUG_ON(extent_op->parent != orig_parent);
735 BUG_ON(extent_op->generation != orig_generation);
736 extent_op->parent = parent;
737 extent_op->generation = ref_generation;
738 } else {
739 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
740 BUG_ON(!extent_op);
741
742 extent_op->type = PENDING_BACKREF_UPDATE;
743 extent_op->bytenr = bytenr;
744 extent_op->num_bytes = num_bytes;
745 extent_op->parent = parent;
746 extent_op->orig_parent = orig_parent;
747 extent_op->generation = ref_generation;
748 extent_op->orig_generation = orig_generation;
749 extent_op->level = (int)owner_objectid;
750
751 set_extent_bits(&root->fs_info->extent_ins,
752 bytenr, bytenr + num_bytes - 1,
753 EXTENT_LOCKED, GFP_NOFS);
754 set_state_private(&root->fs_info->extent_ins,
755 bytenr, (unsigned long)extent_op);
756 }
757 return 0;
758 }
759
760 path = btrfs_alloc_path();
761 if (!path)
762 return -ENOMEM;
763 ret = lookup_extent_backref(trans, extent_root, path,
764 bytenr, orig_parent, orig_root,
765 orig_generation, 1);
766 if (ret)
767 goto out;
768 ret = remove_extent_backref(trans, extent_root, path);
769 if (ret)
770 goto out;
771 ret = insert_extent_backref(trans, extent_root, path, bytenr,
772 parent, ref_root, ref_generation,
773 owner_objectid, owner_offset);
774 BUG_ON(ret);
775 finish_current_insert(trans, extent_root);
776 del_pending_extents(trans, extent_root);
777out:
778 btrfs_free_path(path);
779 return ret;
780}
781
782int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
783 struct btrfs_root *root, u64 bytenr,
784 u64 orig_parent, u64 parent,
785 u64 ref_root, u64 ref_generation,
786 u64 owner_objectid, u64 owner_offset)
787{
788 int ret;
789 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
790 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
791 return 0;
792 maybe_lock_mutex(root);
793 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
794 parent, ref_root, ref_root,
795 ref_generation, ref_generation,
796 owner_objectid, owner_offset);
797 maybe_unlock_mutex(root);
798 return ret;
799}
800
Chris Mason925baed2008-06-25 16:01:30 -0400801static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400802 struct btrfs_root *root, u64 bytenr,
803 u64 orig_parent, u64 parent,
804 u64 orig_root, u64 ref_root,
805 u64 orig_generation, u64 ref_generation,
806 u64 owner_objectid, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500807{
Chris Mason5caf2a02007-04-02 11:20:42 -0400808 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500809 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400810 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400811 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400812 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400813 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500814
Chris Mason5caf2a02007-04-02 11:20:42 -0400815 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400816 if (!path)
817 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400818
Chris Mason3c12ac72008-04-21 12:01:38 -0400819 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400820 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400821 key.type = BTRFS_EXTENT_ITEM_KEY;
822 key.offset = (u64)-1;
823
Chris Mason5caf2a02007-04-02 11:20:42 -0400824 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400825 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400826 if (ret < 0)
827 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400828 BUG_ON(ret == 0 || path->slots[0] == 0);
829
830 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400831 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -0400832
833 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
834 BUG_ON(key.objectid != bytenr);
835 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
836
Chris Mason5caf2a02007-04-02 11:20:42 -0400837 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400838 refs = btrfs_extent_refs(l, item);
839 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400840 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500841
Chris Mason5caf2a02007-04-02 11:20:42 -0400842 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500843
Chris Mason3c12ac72008-04-21 12:01:38 -0400844 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -0400845 ret = insert_extent_backref(trans, root->fs_info->extent_root,
846 path, bytenr, parent,
847 ref_root, ref_generation,
848 owner_objectid, owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -0500849 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400850 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400851 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500852
853 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500854 return 0;
855}
856
Chris Mason925baed2008-06-25 16:01:30 -0400857int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400858 struct btrfs_root *root,
859 u64 bytenr, u64 num_bytes, u64 parent,
860 u64 ref_root, u64 ref_generation,
861 u64 owner_objectid, u64 owner_offset)
Chris Mason925baed2008-06-25 16:01:30 -0400862{
863 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400864 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
865 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
866 return 0;
867 maybe_lock_mutex(root);
868 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
869 0, ref_root, 0, ref_generation,
870 owner_objectid, owner_offset);
871 maybe_unlock_mutex(root);
Chris Mason925baed2008-06-25 16:01:30 -0400872 return ret;
873}
874
Chris Masone9d0b132007-08-10 14:06:19 -0400875int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
876 struct btrfs_root *root)
877{
878 finish_current_insert(trans, root->fs_info->extent_root);
879 del_pending_extents(trans, root->fs_info->extent_root);
880 return 0;
881}
882
Zheng Yan31840ae2008-09-23 13:14:14 -0400883int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
884 struct btrfs_root *root, u64 bytenr,
885 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500886{
Chris Mason5caf2a02007-04-02 11:20:42 -0400887 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500888 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400889 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400890 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400891 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400892
Chris Masondb945352007-10-15 16:15:53 -0400893 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400894 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400895 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400896 key.objectid = bytenr;
897 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400898 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400899 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400900 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400901 if (ret < 0)
902 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400903 if (ret != 0) {
904 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400905 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500906 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400907 }
908 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400909 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400910 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400911out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400912 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500913 return 0;
914}
915
Yan Zhengf321e492008-07-30 09:26:11 -0400916static int get_reference_status(struct btrfs_root *root, u64 bytenr,
917 u64 parent_gen, u64 ref_objectid,
918 u64 *min_generation, u32 *ref_count)
Chris Masonbe20aa92007-12-17 20:14:01 -0500919{
920 struct btrfs_root *extent_root = root->fs_info->extent_root;
921 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -0400922 struct extent_buffer *leaf;
923 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -0500924 struct btrfs_key key;
925 struct btrfs_key found_key;
Yan Zhengf321e492008-07-30 09:26:11 -0400926 u64 root_objectid = root->root_key.objectid;
927 u64 ref_generation;
928 u32 nritems;
929 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500930
Chris Masonbe20aa92007-12-17 20:14:01 -0500931 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400932 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -0400933 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -0500934
Yan Zhengf321e492008-07-30 09:26:11 -0400935 path = btrfs_alloc_path();
936 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonbe20aa92007-12-17 20:14:01 -0500937 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
938 if (ret < 0)
939 goto out;
940 BUG_ON(ret == 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400941 if (ret < 0 || path->slots[0] == 0)
942 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500943
Zheng Yan31840ae2008-09-23 13:14:14 -0400944 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -0400945 leaf = path->nodes[0];
946 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500947
948 if (found_key.objectid != bytenr ||
949 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
Yan Zhengf321e492008-07-30 09:26:11 -0400950 ret = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500951 goto out;
952 }
953
Yan Zhengf321e492008-07-30 09:26:11 -0400954 *ref_count = 0;
955 *min_generation = (u64)-1;
956
Chris Masonbe20aa92007-12-17 20:14:01 -0500957 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -0400958 leaf = path->nodes[0];
959 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500960 if (path->slots[0] >= nritems) {
961 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -0400962 if (ret < 0)
963 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500964 if (ret == 0)
965 continue;
966 break;
967 }
Yan Zhengf321e492008-07-30 09:26:11 -0400968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500969 if (found_key.objectid != bytenr)
970 break;
Chris Masonbd098352008-01-03 13:23:19 -0500971
Chris Masonbe20aa92007-12-17 20:14:01 -0500972 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
973 path->slots[0]++;
974 continue;
975 }
976
Yan Zhengf321e492008-07-30 09:26:11 -0400977 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -0500978 struct btrfs_extent_ref);
Yan Zhengf321e492008-07-30 09:26:11 -0400979 ref_generation = btrfs_ref_generation(leaf, ref_item);
980 /*
Zheng Yan31840ae2008-09-23 13:14:14 -0400981 * For (parent_gen > 0 && parent_gen > ref_generation):
Yan Zhengf321e492008-07-30 09:26:11 -0400982 *
Yanbcc63ab2008-07-30 16:29:20 -0400983 * we reach here through the oldest root, therefore
984 * all other reference from same snapshot should have
Yan Zhengf321e492008-07-30 09:26:11 -0400985 * a larger generation.
986 */
987 if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
988 (parent_gen > 0 && parent_gen > ref_generation) ||
989 (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
990 ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400991 *ref_count = 2;
Yan Zhengf321e492008-07-30 09:26:11 -0400992 break;
993 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500994
Yan Zhengf321e492008-07-30 09:26:11 -0400995 *ref_count = 1;
996 if (*min_generation > ref_generation)
997 *min_generation = ref_generation;
998
Chris Masonbe20aa92007-12-17 20:14:01 -0500999 path->slots[0]++;
1000 }
Yan Zhengf321e492008-07-30 09:26:11 -04001001 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001002out:
Chris Mason925baed2008-06-25 16:01:30 -04001003 mutex_unlock(&root->fs_info->alloc_mutex);
Yan Zhengf321e492008-07-30 09:26:11 -04001004 btrfs_free_path(path);
1005 return ret;
1006}
1007
Yan Zheng7ea394f2008-08-05 13:05:02 -04001008int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
1009 struct btrfs_root *root,
Yan Zhengf321e492008-07-30 09:26:11 -04001010 struct btrfs_key *key, u64 bytenr)
1011{
Yan Zhengf321e492008-07-30 09:26:11 -04001012 struct btrfs_root *old_root;
1013 struct btrfs_path *path = NULL;
1014 struct extent_buffer *eb;
1015 struct btrfs_file_extent_item *item;
1016 u64 ref_generation;
1017 u64 min_generation;
1018 u64 extent_start;
1019 u32 ref_count;
1020 int level;
1021 int ret;
1022
Yan Zheng7ea394f2008-08-05 13:05:02 -04001023 BUG_ON(trans == NULL);
Yan Zhengf321e492008-07-30 09:26:11 -04001024 BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY);
1025 ret = get_reference_status(root, bytenr, 0, key->objectid,
1026 &min_generation, &ref_count);
1027 if (ret)
1028 return ret;
1029
1030 if (ref_count != 1)
1031 return 1;
1032
Yan Zhengf321e492008-07-30 09:26:11 -04001033 old_root = root->dirty_root->root;
1034 ref_generation = old_root->root_key.offset;
1035
1036 /* all references are created in running transaction */
1037 if (min_generation > ref_generation) {
1038 ret = 0;
1039 goto out;
1040 }
1041
1042 path = btrfs_alloc_path();
1043 if (!path) {
1044 ret = -ENOMEM;
1045 goto out;
1046 }
1047
1048 path->skip_locking = 1;
1049 /* if no item found, the extent is referenced by other snapshot */
1050 ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0);
1051 if (ret)
1052 goto out;
1053
1054 eb = path->nodes[0];
1055 item = btrfs_item_ptr(eb, path->slots[0],
1056 struct btrfs_file_extent_item);
1057 if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG ||
1058 btrfs_file_extent_disk_bytenr(eb, item) != bytenr) {
1059 ret = 1;
1060 goto out;
1061 }
1062
1063 for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) {
1064 if (level >= 0) {
1065 eb = path->nodes[level];
1066 if (!eb)
1067 continue;
1068 extent_start = eb->start;
Yanbcc63ab2008-07-30 16:29:20 -04001069 } else
Yan Zhengf321e492008-07-30 09:26:11 -04001070 extent_start = bytenr;
1071
1072 ret = get_reference_status(root, extent_start, ref_generation,
1073 0, &min_generation, &ref_count);
1074 if (ret)
1075 goto out;
1076
1077 if (ref_count != 1) {
1078 ret = 1;
1079 goto out;
1080 }
1081 if (level >= 0)
1082 ref_generation = btrfs_header_generation(eb);
1083 }
1084 ret = 0;
1085out:
1086 if (path)
1087 btrfs_free_path(path);
Yan Zhengf321e492008-07-30 09:26:11 -04001088 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001089}
Chris Masonc5739bb2007-04-10 09:27:04 -04001090
Zheng Yan31840ae2008-09-23 13:14:14 -04001091int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1092 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001093{
Chris Mason5f39d392007-10-15 16:14:19 -04001094 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001095 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001096 u64 root_gen;
1097 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001098 int i;
Chris Masondb945352007-10-15 16:15:53 -04001099 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001100 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001101 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001102
Chris Mason3768f362007-03-13 16:47:54 -04001103 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001104 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001105
Zheng Yane4657682008-09-26 10:04:53 -04001106 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1107 shared = 0;
1108 root_gen = root->root_key.offset;
1109 } else {
1110 shared = 1;
1111 root_gen = trans->transid - 1;
1112 }
1113
Chris Masondb945352007-10-15 16:15:53 -04001114 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001115 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001116
Zheng Yan31840ae2008-09-23 13:14:14 -04001117 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001118 struct btrfs_leaf_ref *ref;
1119 struct btrfs_extent_info *info;
1120
Zheng Yan31840ae2008-09-23 13:14:14 -04001121 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001122 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001123 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001124 goto out;
1125 }
1126
Zheng Yane4657682008-09-26 10:04:53 -04001127 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001128 ref->bytenr = buf->start;
1129 ref->owner = btrfs_header_owner(buf);
1130 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001131 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001132 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001133
Zheng Yan31840ae2008-09-23 13:14:14 -04001134 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001135 u64 disk_bytenr;
1136 btrfs_item_key_to_cpu(buf, &key, i);
1137 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1138 continue;
1139 fi = btrfs_item_ptr(buf, i,
1140 struct btrfs_file_extent_item);
1141 if (btrfs_file_extent_type(buf, fi) ==
1142 BTRFS_FILE_EXTENT_INLINE)
1143 continue;
1144 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1145 if (disk_bytenr == 0)
1146 continue;
1147
1148 info->bytenr = disk_bytenr;
1149 info->num_bytes =
1150 btrfs_file_extent_disk_num_bytes(buf, fi);
1151 info->objectid = key.objectid;
1152 info->offset = key.offset;
1153 info++;
1154 }
1155
Zheng Yane4657682008-09-26 10:04:53 -04001156 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng31153d82008-07-28 15:32:19 -04001157 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001158 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001159 }
1160out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001161 return ret;
1162}
1163
1164int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1165 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1166 u32 *nr_extents)
1167{
1168 u64 bytenr;
1169 u64 ref_root;
1170 u64 orig_root;
1171 u64 ref_generation;
1172 u64 orig_generation;
1173 u32 nritems;
1174 u32 nr_file_extents = 0;
1175 struct btrfs_key key;
1176 struct btrfs_file_extent_item *fi;
1177 int i;
1178 int level;
1179 int ret = 0;
1180 int faili = 0;
1181 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1182 u64, u64, u64, u64, u64, u64, u64, u64, u64);
1183
1184 ref_root = btrfs_header_owner(buf);
1185 ref_generation = btrfs_header_generation(buf);
1186 orig_root = btrfs_header_owner(orig_buf);
1187 orig_generation = btrfs_header_generation(orig_buf);
1188
1189 nritems = btrfs_header_nritems(buf);
1190 level = btrfs_header_level(buf);
1191
1192 if (root->ref_cows) {
1193 process_func = __btrfs_inc_extent_ref;
1194 } else {
1195 if (level == 0 &&
1196 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1197 goto out;
1198 if (level != 0 &&
1199 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1200 goto out;
1201 process_func = __btrfs_update_extent_ref;
1202 }
1203
1204 for (i = 0; i < nritems; i++) {
1205 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001206 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001207 btrfs_item_key_to_cpu(buf, &key, i);
1208 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001209 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001210 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001211 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001212 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001213 BTRFS_FILE_EXTENT_INLINE)
1214 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001215 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1216 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001217 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001218
1219 nr_file_extents++;
1220
1221 maybe_lock_mutex(root);
1222 ret = process_func(trans, root, bytenr,
1223 orig_buf->start, buf->start,
1224 orig_root, ref_root,
1225 orig_generation, ref_generation,
1226 key.objectid, key.offset);
1227 maybe_unlock_mutex(root);
1228
1229 if (ret) {
1230 faili = i;
1231 WARN_ON(1);
1232 goto fail;
1233 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001234 } else {
Chris Masondb945352007-10-15 16:15:53 -04001235 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001236 maybe_lock_mutex(root);
1237 ret = process_func(trans, root, bytenr,
1238 orig_buf->start, buf->start,
1239 orig_root, ref_root,
1240 orig_generation, ref_generation,
1241 level - 1, 0);
1242 maybe_unlock_mutex(root);
1243 if (ret) {
1244 faili = i;
1245 WARN_ON(1);
1246 goto fail;
1247 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001248 }
1249 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001250out:
1251 if (nr_extents) {
1252 if (level == 0)
1253 *nr_extents = nr_file_extents;
1254 else
1255 *nr_extents = nritems;
1256 }
1257 return 0;
1258fail:
1259 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001260 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001261}
1262
Zheng Yan31840ae2008-09-23 13:14:14 -04001263int btrfs_update_ref(struct btrfs_trans_handle *trans,
1264 struct btrfs_root *root, struct extent_buffer *orig_buf,
1265 struct extent_buffer *buf, int start_slot, int nr)
1266
1267{
1268 u64 bytenr;
1269 u64 ref_root;
1270 u64 orig_root;
1271 u64 ref_generation;
1272 u64 orig_generation;
1273 struct btrfs_key key;
1274 struct btrfs_file_extent_item *fi;
1275 int i;
1276 int ret;
1277 int slot;
1278 int level;
1279
1280 BUG_ON(start_slot < 0);
1281 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1282
1283 ref_root = btrfs_header_owner(buf);
1284 ref_generation = btrfs_header_generation(buf);
1285 orig_root = btrfs_header_owner(orig_buf);
1286 orig_generation = btrfs_header_generation(orig_buf);
1287 level = btrfs_header_level(buf);
1288
1289 if (!root->ref_cows) {
1290 if (level == 0 &&
1291 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1292 return 0;
1293 if (level != 0 &&
1294 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1295 return 0;
1296 }
1297
1298 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1299 cond_resched();
1300 if (level == 0) {
1301 btrfs_item_key_to_cpu(buf, &key, slot);
1302 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1303 continue;
1304 fi = btrfs_item_ptr(buf, slot,
1305 struct btrfs_file_extent_item);
1306 if (btrfs_file_extent_type(buf, fi) ==
1307 BTRFS_FILE_EXTENT_INLINE)
1308 continue;
1309 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1310 if (bytenr == 0)
1311 continue;
1312 maybe_lock_mutex(root);
1313 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1314 orig_buf->start, buf->start,
1315 orig_root, ref_root,
1316 orig_generation, ref_generation,
1317 key.objectid, key.offset);
1318 maybe_unlock_mutex(root);
1319 if (ret)
1320 goto fail;
1321 } else {
1322 bytenr = btrfs_node_blockptr(buf, slot);
1323 maybe_lock_mutex(root);
1324 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1325 orig_buf->start, buf->start,
1326 orig_root, ref_root,
1327 orig_generation, ref_generation,
1328 level - 1, 0);
1329 maybe_unlock_mutex(root);
1330 if (ret)
1331 goto fail;
1332 }
1333 }
1334 return 0;
1335fail:
1336 WARN_ON(1);
1337 return -1;
1338}
1339
Chris Mason9078a3e2007-04-26 16:46:15 -04001340static int write_one_cache_group(struct btrfs_trans_handle *trans,
1341 struct btrfs_root *root,
1342 struct btrfs_path *path,
1343 struct btrfs_block_group_cache *cache)
1344{
1345 int ret;
1346 int pending_ret;
1347 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001348 unsigned long bi;
1349 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001350
Chris Mason9078a3e2007-04-26 16:46:15 -04001351 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001352 if (ret < 0)
1353 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001354 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001355
1356 leaf = path->nodes[0];
1357 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1358 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1359 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001360 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001361fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001362 finish_current_insert(trans, extent_root);
1363 pending_ret = del_pending_extents(trans, extent_root);
1364 if (ret)
1365 return ret;
1366 if (pending_ret)
1367 return pending_ret;
1368 return 0;
1369
1370}
1371
Chris Mason96b51792007-10-15 16:15:19 -04001372int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1373 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001374{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001375 struct btrfs_block_group_cache *cache, *entry;
1376 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001377 int err = 0;
1378 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001379 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001380 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001381
1382 path = btrfs_alloc_path();
1383 if (!path)
1384 return -ENOMEM;
1385
Chris Mason925baed2008-06-25 16:01:30 -04001386 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001387 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001388 cache = NULL;
1389 spin_lock(&root->fs_info->block_group_cache_lock);
1390 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1391 n; n = rb_next(n)) {
1392 entry = rb_entry(n, struct btrfs_block_group_cache,
1393 cache_node);
1394 if (entry->dirty) {
1395 cache = entry;
1396 break;
1397 }
1398 }
1399 spin_unlock(&root->fs_info->block_group_cache_lock);
1400
1401 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001402 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001403
Zheng Yane8569812008-09-26 10:05:48 -04001404 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001405 last += cache->key.offset;
1406
Chris Mason96b51792007-10-15 16:15:19 -04001407 err = write_one_cache_group(trans, root,
1408 path, cache);
1409 /*
1410 * if we fail to write the cache group, we want
1411 * to keep it marked dirty in hopes that a later
1412 * write will work
1413 */
1414 if (err) {
1415 werr = err;
1416 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001417 }
1418 }
1419 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04001420 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001421 return werr;
1422}
1423
Chris Mason593060d2008-03-25 16:50:33 -04001424static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1425 u64 total_bytes, u64 bytes_used,
1426 struct btrfs_space_info **space_info)
1427{
1428 struct btrfs_space_info *found;
1429
1430 found = __find_space_info(info, flags);
1431 if (found) {
1432 found->total_bytes += total_bytes;
1433 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001434 found->full = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001435 *space_info = found;
1436 return 0;
1437 }
1438 found = kmalloc(sizeof(*found), GFP_NOFS);
1439 if (!found)
1440 return -ENOMEM;
1441
1442 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001443 INIT_LIST_HEAD(&found->block_groups);
1444 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001445 found->flags = flags;
1446 found->total_bytes = total_bytes;
1447 found->bytes_used = bytes_used;
1448 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001449 found->bytes_reserved = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001450 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001451 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001452 *space_info = found;
1453 return 0;
1454}
1455
Chris Mason8790d502008-04-03 16:29:03 -04001456static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1457{
1458 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001459 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001460 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001461 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001462 if (extra_flags) {
1463 if (flags & BTRFS_BLOCK_GROUP_DATA)
1464 fs_info->avail_data_alloc_bits |= extra_flags;
1465 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1466 fs_info->avail_metadata_alloc_bits |= extra_flags;
1467 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1468 fs_info->avail_system_alloc_bits |= extra_flags;
1469 }
1470}
Chris Mason593060d2008-03-25 16:50:33 -04001471
Chris Masona061fc82008-05-07 11:43:44 -04001472static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001473{
Chris Masona061fc82008-05-07 11:43:44 -04001474 u64 num_devices = root->fs_info->fs_devices->num_devices;
1475
1476 if (num_devices == 1)
1477 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1478 if (num_devices < 4)
1479 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1480
Chris Masonec44a352008-04-28 15:29:52 -04001481 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1482 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001483 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001484 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001485 }
Chris Masonec44a352008-04-28 15:29:52 -04001486
1487 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001488 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001489 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001490 }
Chris Masonec44a352008-04-28 15:29:52 -04001491
1492 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1493 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1494 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1495 (flags & BTRFS_BLOCK_GROUP_DUP)))
1496 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1497 return flags;
1498}
1499
Chris Mason6324fbf2008-03-24 15:01:59 -04001500static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1501 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001502 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001503{
1504 struct btrfs_space_info *space_info;
1505 u64 thresh;
1506 u64 start;
1507 u64 num_bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001508 int ret = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001509
Chris Masona061fc82008-05-07 11:43:44 -04001510 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001511
Chris Mason6324fbf2008-03-24 15:01:59 -04001512 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001513 if (!space_info) {
1514 ret = update_space_info(extent_root->fs_info, flags,
1515 0, 0, &space_info);
1516 BUG_ON(ret);
1517 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001518 BUG_ON(!space_info);
1519
Chris Mason0ef3e662008-05-24 14:04:53 -04001520 if (space_info->force_alloc) {
1521 force = 1;
1522 space_info->force_alloc = 0;
1523 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001524 if (space_info->full)
Chris Mason925baed2008-06-25 16:01:30 -04001525 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001526
Chris Mason8790d502008-04-03 16:29:03 -04001527 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001528 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001529 (space_info->bytes_used + space_info->bytes_pinned +
1530 space_info->bytes_reserved + alloc_bytes) < thresh)
Chris Mason925baed2008-06-25 16:01:30 -04001531 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001532
Chris Mason925baed2008-06-25 16:01:30 -04001533 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001534 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1535 if (ret == -ENOSPC) {
1536printk("space info full %Lu\n", flags);
1537 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001538 goto out_unlock;
Chris Mason6324fbf2008-03-24 15:01:59 -04001539 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001540 BUG_ON(ret);
1541
1542 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001543 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001544 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001545
Chris Masona74a4b92008-06-25 16:01:31 -04001546out_unlock:
Chris Mason333db942008-06-25 16:01:30 -04001547 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001548out:
Josef Bacik0f9dd462008-09-23 13:14:11 -04001549 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001550}
1551
Chris Mason9078a3e2007-04-26 16:46:15 -04001552static int update_block_group(struct btrfs_trans_handle *trans,
1553 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001554 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001555 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001556{
1557 struct btrfs_block_group_cache *cache;
1558 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001559 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001560 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001561 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001562
Chris Mason7d9eb122008-07-08 14:19:17 -04001563 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason9078a3e2007-04-26 16:46:15 -04001564 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001565 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001566 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001567 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001568 }
Chris Masondb945352007-10-15 16:15:53 -04001569 byte_in_group = bytenr - cache->key.objectid;
1570 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001571
Chris Masonc286ac42008-07-22 23:06:41 -04001572 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001573 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001574 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001575 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001576 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001577 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001578 cache->space_info->bytes_used += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001579 btrfs_set_block_group_used(&cache->item, old_val);
1580 spin_unlock(&cache->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001581 } else {
Chris Masondb945352007-10-15 16:15:53 -04001582 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001583 cache->space_info->bytes_used -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001584 btrfs_set_block_group_used(&cache->item, old_val);
1585 spin_unlock(&cache->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001586 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001587 int ret;
1588 ret = btrfs_add_free_space(cache, bytenr,
1589 num_bytes);
1590 if (ret)
1591 return -1;
Chris Masone37c9e62007-05-09 20:13:14 -04001592 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001593 }
Chris Masondb945352007-10-15 16:15:53 -04001594 total -= num_bytes;
1595 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001596 }
1597 return 0;
1598}
Chris Mason6324fbf2008-03-24 15:01:59 -04001599
Chris Masona061fc82008-05-07 11:43:44 -04001600static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1601{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001602 struct btrfs_block_group_cache *cache;
1603
1604 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1605 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001606 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001607
1608 return cache->key.objectid;
Chris Masona061fc82008-05-07 11:43:44 -04001609}
1610
Chris Masone02119d2008-09-05 16:13:11 -04001611int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05001612 u64 bytenr, u64 num, int pin)
1613{
1614 u64 len;
1615 struct btrfs_block_group_cache *cache;
1616 struct btrfs_fs_info *fs_info = root->fs_info;
1617
Chris Mason7d9eb122008-07-08 14:19:17 -04001618 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Yan324ae4d2007-11-16 14:57:08 -05001619 if (pin) {
1620 set_extent_dirty(&fs_info->pinned_extents,
1621 bytenr, bytenr + num - 1, GFP_NOFS);
1622 } else {
1623 clear_extent_dirty(&fs_info->pinned_extents,
1624 bytenr, bytenr + num - 1, GFP_NOFS);
1625 }
1626 while (num > 0) {
1627 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04001628 BUG_ON(!cache);
1629 len = min(num, cache->key.offset -
1630 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05001631 if (pin) {
Zheng Yane8569812008-09-26 10:05:48 -04001632 spin_lock(&cache->lock);
1633 cache->pinned += len;
1634 cache->space_info->bytes_pinned += len;
1635 spin_unlock(&cache->lock);
Yan324ae4d2007-11-16 14:57:08 -05001636 fs_info->total_pinned += len;
1637 } else {
Zheng Yane8569812008-09-26 10:05:48 -04001638 spin_lock(&cache->lock);
1639 cache->pinned -= len;
1640 cache->space_info->bytes_pinned -= len;
1641 spin_unlock(&cache->lock);
Yan324ae4d2007-11-16 14:57:08 -05001642 fs_info->total_pinned -= len;
1643 }
1644 bytenr += len;
1645 num -= len;
1646 }
1647 return 0;
1648}
Chris Mason9078a3e2007-04-26 16:46:15 -04001649
Zheng Yane8569812008-09-26 10:05:48 -04001650static int update_reserved_extents(struct btrfs_root *root,
1651 u64 bytenr, u64 num, int reserve)
1652{
1653 u64 len;
1654 struct btrfs_block_group_cache *cache;
1655 struct btrfs_fs_info *fs_info = root->fs_info;
1656
1657 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1658 while (num > 0) {
1659 cache = btrfs_lookup_block_group(fs_info, bytenr);
1660 BUG_ON(!cache);
1661 len = min(num, cache->key.offset -
1662 (bytenr - cache->key.objectid));
1663 if (reserve) {
1664 spin_lock(&cache->lock);
1665 cache->reserved += len;
1666 cache->space_info->bytes_reserved += len;
1667 spin_unlock(&cache->lock);
1668 } else {
1669 spin_lock(&cache->lock);
1670 cache->reserved -= len;
1671 cache->space_info->bytes_reserved -= len;
1672 spin_unlock(&cache->lock);
1673 }
1674 bytenr += len;
1675 num -= len;
1676 }
1677 return 0;
1678}
1679
Chris Masond1310b22008-01-24 16:13:08 -05001680int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001681{
Chris Masonccd467d2007-06-28 15:57:36 -04001682 u64 last = 0;
Chris Mason1a5bc1672007-10-15 16:15:26 -04001683 u64 start;
1684 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001685 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001686 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001687
1688 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04001689 ret = find_first_extent_bit(pinned_extents, last,
1690 &start, &end, EXTENT_DIRTY);
1691 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001692 break;
Chris Mason1a5bc1672007-10-15 16:15:26 -04001693 set_extent_dirty(copy, start, end, GFP_NOFS);
1694 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001695 }
1696 return 0;
1697}
1698
1699int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1700 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001701 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001702{
Chris Mason1a5bc1672007-10-15 16:15:26 -04001703 u64 start;
1704 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001705 int ret;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001706 struct btrfs_block_group_cache *cache;
Chris Masona28ec192007-03-06 20:08:01 -05001707
Chris Mason925baed2008-06-25 16:01:30 -04001708 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001709 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04001710 ret = find_first_extent_bit(unpin, 0, &start, &end,
1711 EXTENT_DIRTY);
1712 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001713 break;
Chris Masone02119d2008-09-05 16:13:11 -04001714 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc1672007-10-15 16:15:26 -04001715 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001716 cache = btrfs_lookup_block_group(root->fs_info, start);
1717 if (cache->cached)
1718 btrfs_add_free_space(cache, start, end - start + 1);
Chris Masonc286ac42008-07-22 23:06:41 -04001719 if (need_resched()) {
1720 mutex_unlock(&root->fs_info->alloc_mutex);
1721 cond_resched();
1722 mutex_lock(&root->fs_info->alloc_mutex);
1723 }
Chris Masona28ec192007-03-06 20:08:01 -05001724 }
Chris Mason925baed2008-06-25 16:01:30 -04001725 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001726 return 0;
1727}
1728
Chris Mason98ed5172008-01-03 10:01:48 -05001729static int finish_current_insert(struct btrfs_trans_handle *trans,
1730 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001731{
Chris Mason7bb86312007-12-11 09:25:06 -05001732 u64 start;
1733 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04001734 u64 priv;
Chris Mason7bb86312007-12-11 09:25:06 -05001735 struct btrfs_fs_info *info = extent_root->fs_info;
1736 struct btrfs_path *path;
Zheng Yan31840ae2008-09-23 13:14:14 -04001737 struct btrfs_extent_ref *ref;
1738 struct pending_extent_op *extent_op;
1739 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -04001740 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001741 int ret;
Chris Mason1a5bc1672007-10-15 16:15:26 -04001742 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001743
Chris Mason7d9eb122008-07-08 14:19:17 -04001744 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Chris Mason5f39d392007-10-15 16:14:19 -04001745 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001746 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001747
Chris Mason26b80032007-08-08 20:17:12 -04001748 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04001749 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1750 &end, EXTENT_LOCKED);
1751 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001752 break;
1753
Zheng Yan31840ae2008-09-23 13:14:14 -04001754 ret = get_state_private(&info->extent_ins, start, &priv);
1755 BUG_ON(ret);
1756 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1757
1758 if (extent_op->type == PENDING_EXTENT_INSERT) {
1759 key.objectid = start;
1760 key.offset = end + 1 - start;
1761 key.type = BTRFS_EXTENT_ITEM_KEY;
1762 err = btrfs_insert_item(trans, extent_root, &key,
Chris Mason1a5bc1672007-10-15 16:15:26 -04001763 &extent_item, sizeof(extent_item));
Zheng Yan31840ae2008-09-23 13:14:14 -04001764 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001765
Zheng Yan31840ae2008-09-23 13:14:14 -04001766 clear_extent_bits(&info->extent_ins, start, end,
1767 EXTENT_LOCKED, GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04001768
Zheng Yan31840ae2008-09-23 13:14:14 -04001769 err = insert_extent_backref(trans, extent_root, path,
1770 start, extent_op->parent,
1771 extent_root->root_key.objectid,
1772 extent_op->generation,
1773 extent_op->level, 0);
1774 BUG_ON(err);
1775 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1776 err = lookup_extent_backref(trans, extent_root, path,
1777 start, extent_op->orig_parent,
1778 extent_root->root_key.objectid,
1779 extent_op->orig_generation, 0);
1780 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001781
Zheng Yan31840ae2008-09-23 13:14:14 -04001782 clear_extent_bits(&info->extent_ins, start, end,
1783 EXTENT_LOCKED, GFP_NOFS);
1784
1785 key.objectid = start;
1786 key.offset = extent_op->parent;
1787 key.type = BTRFS_EXTENT_REF_KEY;
1788 err = btrfs_set_item_key_safe(trans, extent_root, path,
1789 &key);
1790 BUG_ON(err);
1791 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1792 struct btrfs_extent_ref);
1793 btrfs_set_ref_generation(path->nodes[0], ref,
1794 extent_op->generation);
1795 btrfs_mark_buffer_dirty(path->nodes[0]);
1796 btrfs_release_path(extent_root, path);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001797 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001798 BUG_ON(1);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001799 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001800 kfree(extent_op);
1801
Chris Masonc286ac42008-07-22 23:06:41 -04001802 if (need_resched()) {
1803 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1804 cond_resched();
1805 mutex_lock(&extent_root->fs_info->alloc_mutex);
1806 }
Chris Mason037e6392007-03-07 11:50:24 -05001807 }
Chris Mason7bb86312007-12-11 09:25:06 -05001808 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001809 return 0;
1810}
1811
Zheng Yan31840ae2008-09-23 13:14:14 -04001812static int pin_down_bytes(struct btrfs_trans_handle *trans,
1813 struct btrfs_root *root,
1814 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04001815{
Chris Mason1a5bc1672007-10-15 16:15:26 -04001816 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001817 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001818
Chris Mason7d9eb122008-07-08 14:19:17 -04001819 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Zheng Yan31840ae2008-09-23 13:14:14 -04001820 if (is_data)
1821 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001822
Zheng Yan31840ae2008-09-23 13:14:14 -04001823 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1824 if (!buf)
1825 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001826
Zheng Yan31840ae2008-09-23 13:14:14 -04001827 /* we can reuse a block if it hasn't been written
1828 * and it is from this transaction. We can't
1829 * reuse anything from the tree log root because
1830 * it has tiny sub-transactions.
1831 */
1832 if (btrfs_buffer_uptodate(buf, 0) &&
1833 btrfs_try_tree_lock(buf)) {
1834 u64 header_owner = btrfs_header_owner(buf);
1835 u64 header_transid = btrfs_header_generation(buf);
1836 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
1837 header_transid == trans->transid &&
1838 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1839 clean_tree_block(NULL, root, buf);
1840 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001841 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001842 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04001843 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001844 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001845 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001846 free_extent_buffer(buf);
1847pinit:
1848 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
1849
Chris Masonbe744172007-05-06 10:15:01 -04001850 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001851 return 0;
1852}
1853
Chris Masona28ec192007-03-06 20:08:01 -05001854/*
1855 * remove an extent from the root, returns 0 on success
1856 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001857static int __free_extent(struct btrfs_trans_handle *trans,
1858 struct btrfs_root *root,
1859 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05001860 u64 root_objectid, u64 ref_generation,
Zheng Yan31840ae2008-09-23 13:14:14 -04001861 u64 owner_objectid, u64 owner_offset,
1862 int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001863{
Chris Mason5caf2a02007-04-02 11:20:42 -04001864 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001865 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001866 struct btrfs_fs_info *info = root->fs_info;
1867 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001868 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001869 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001870 int extent_slot = 0;
1871 int found_extent = 0;
1872 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001873 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001874 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001875
Chris Mason7d9eb122008-07-08 14:19:17 -04001876 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Masondb945352007-10-15 16:15:53 -04001877 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001878 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001879 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001880 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001881 if (!path)
1882 return -ENOMEM;
1883
Chris Mason3c12ac72008-04-21 12:01:38 -04001884 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001885 ret = lookup_extent_backref(trans, extent_root, path, bytenr, parent,
1886 root_objectid, ref_generation, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001887 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001888 struct btrfs_key found_key;
1889 extent_slot = path->slots[0];
1890 while(extent_slot > 0) {
1891 extent_slot--;
1892 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1893 extent_slot);
1894 if (found_key.objectid != bytenr)
1895 break;
1896 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1897 found_key.offset == num_bytes) {
1898 found_extent = 1;
1899 break;
1900 }
1901 if (path->slots[0] - extent_slot > 5)
1902 break;
1903 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001904 if (!found_extent) {
1905 ret = remove_extent_backref(trans, extent_root, path);
1906 BUG_ON(ret);
1907 btrfs_release_path(extent_root, path);
1908 ret = btrfs_search_slot(trans, extent_root,
1909 &key, path, -1, 1);
1910 BUG_ON(ret);
1911 extent_slot = path->slots[0];
1912 }
Chris Mason7bb86312007-12-11 09:25:06 -05001913 } else {
1914 btrfs_print_leaf(extent_root, path->nodes[0]);
1915 WARN_ON(1);
1916 printk("Unable to find ref byte nr %Lu root %Lu "
1917 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1918 root_objectid, ref_generation, owner_objectid,
1919 owner_offset);
1920 }
Chris Mason5f39d392007-10-15 16:14:19 -04001921
1922 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001923 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001924 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001925 refs = btrfs_extent_refs(leaf, ei);
1926 BUG_ON(refs == 0);
1927 refs -= 1;
1928 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001929
Chris Mason5f39d392007-10-15 16:14:19 -04001930 btrfs_mark_buffer_dirty(leaf);
1931
Chris Mason952fcca2008-02-18 16:33:44 -05001932 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001933 struct btrfs_extent_ref *ref;
1934 ref = btrfs_item_ptr(leaf, path->slots[0],
1935 struct btrfs_extent_ref);
1936 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001937 /* if the back ref and the extent are next to each other
1938 * they get deleted below in one shot
1939 */
1940 path->slots[0] = extent_slot;
1941 num_to_del = 2;
1942 } else if (found_extent) {
1943 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001944 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05001945 BUG_ON(ret);
1946 /* if refs are 0, we need to setup the path for deletion */
1947 if (refs == 0) {
1948 btrfs_release_path(extent_root, path);
1949 ret = btrfs_search_slot(trans, extent_root, &key, path,
1950 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001951 BUG_ON(ret);
1952 }
1953 }
1954
Chris Masoncf27e1e2007-03-13 09:49:06 -04001955 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001956 u64 super_used;
1957 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01001958#ifdef BIO_RW_DISCARD
1959 u64 map_length = num_bytes;
1960 struct btrfs_multi_bio *multi = NULL;
1961#endif
Chris Mason78fae272007-03-25 11:35:08 -04001962
1963 if (pin) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001964 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
1965 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Yanc5492282007-11-06 10:25:25 -05001966 if (ret > 0)
1967 mark_free = 1;
1968 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001969 }
1970
Josef Bacik58176a92007-08-29 15:47:34 -04001971 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04001972 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04001973 super_used = btrfs_super_bytes_used(&info->super_copy);
1974 btrfs_set_super_bytes_used(&info->super_copy,
1975 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04001976 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04001977
1978 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001979 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001980 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001981 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001982 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1983 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04001984 BUG_ON(ret);
Chris Masondb945352007-10-15 16:15:53 -04001985 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001986 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001987 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01001988
1989#ifdef BIO_RW_DISCARD
1990 /* Tell the block device(s) that the sectors can be discarded */
1991 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1992 bytenr, &map_length, &multi, 0);
1993 if (!ret) {
1994 struct btrfs_bio_stripe *stripe = multi->stripes;
1995 int i;
1996
1997 if (map_length > num_bytes)
1998 map_length = num_bytes;
1999
2000 for (i = 0; i < multi->num_stripes; i++, stripe++) {
2001 blkdev_issue_discard(stripe->dev->bdev,
2002 stripe->physical >> 9,
2003 map_length >> 9);
2004 }
2005 kfree(multi);
2006 }
2007#endif
Chris Masona28ec192007-03-06 20:08:01 -05002008 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002009 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002010 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05002011 return ret;
2012}
2013
2014/*
Chris Masonfec577f2007-02-26 10:40:21 -05002015 * find all the blocks marked as pending in the radix tree and remove
2016 * them from the extent map
2017 */
Chris Masone089f052007-03-16 16:20:31 -04002018static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2019 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05002020{
2021 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002022 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002023 int mark_free = 0;
Chris Mason1a5bc1672007-10-15 16:15:26 -04002024 u64 start;
2025 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002026 u64 priv;
Chris Masond1310b22008-01-24 16:13:08 -05002027 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002028 struct extent_io_tree *extent_ins;
2029 struct pending_extent_op *extent_op;
Chris Mason8ef97622007-03-26 10:15:30 -04002030
Chris Mason7d9eb122008-07-08 14:19:17 -04002031 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Zheng Yan31840ae2008-09-23 13:14:14 -04002032 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc1672007-10-15 16:15:26 -04002033 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002034
2035 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04002036 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2037 EXTENT_LOCKED);
2038 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05002039 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002040
2041 ret = get_state_private(pending_del, start, &priv);
2042 BUG_ON(ret);
2043 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2044
Chris Mason1a5bc1672007-10-15 16:15:26 -04002045 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2046 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002047
2048 ret = pin_down_bytes(trans, extent_root, start,
2049 end + 1 - start, 0);
2050 mark_free = ret > 0;
2051 if (!test_range_bit(extent_ins, start, end,
2052 EXTENT_LOCKED, 0)) {
2053free_extent:
Chris Masonc286ac42008-07-22 23:06:41 -04002054 ret = __free_extent(trans, extent_root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002055 start, end + 1 - start,
2056 extent_op->orig_parent,
2057 extent_root->root_key.objectid,
2058 extent_op->orig_generation,
2059 extent_op->level, 0, 0, mark_free);
2060 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002061 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002062 kfree(extent_op);
2063 ret = get_state_private(extent_ins, start, &priv);
2064 BUG_ON(ret);
2065 extent_op = (struct pending_extent_op *)
2066 (unsigned long)priv;
2067
2068 clear_extent_bits(extent_ins, start, end,
2069 EXTENT_LOCKED, GFP_NOFS);
2070
2071 if (extent_op->type == PENDING_BACKREF_UPDATE)
2072 goto free_extent;
2073
2074 ret = update_block_group(trans, extent_root, start,
2075 end + 1 - start, 0, mark_free);
2076 BUG_ON(ret);
2077 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002078 }
Chris Mason1a5bc1672007-10-15 16:15:26 -04002079 if (ret)
2080 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04002081
2082 if (need_resched()) {
2083 mutex_unlock(&extent_root->fs_info->alloc_mutex);
2084 cond_resched();
2085 mutex_lock(&extent_root->fs_info->alloc_mutex);
2086 }
Chris Masonfec577f2007-02-26 10:40:21 -05002087 }
Chris Masone20d96d2007-03-22 12:13:20 -04002088 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002089}
2090
2091/*
2092 * remove an extent from the root, returns 0 on success
2093 */
Chris Mason925baed2008-06-25 16:01:30 -04002094static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002095 struct btrfs_root *root,
2096 u64 bytenr, u64 num_bytes, u64 parent,
2097 u64 root_objectid, u64 ref_generation,
2098 u64 owner_objectid, u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002099{
Chris Mason9f5fae22007-03-20 14:38:32 -04002100 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002101 int pending_ret;
2102 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002103
Chris Masondb945352007-10-15 16:15:53 -04002104 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002105 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002106 struct pending_extent_op *extent_op;
2107
2108 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2109 BUG_ON(!extent_op);
2110
2111 extent_op->type = PENDING_EXTENT_DELETE;
2112 extent_op->bytenr = bytenr;
2113 extent_op->num_bytes = num_bytes;
2114 extent_op->parent = parent;
2115 extent_op->orig_parent = parent;
2116 extent_op->generation = ref_generation;
2117 extent_op->orig_generation = ref_generation;
2118 extent_op->level = (int)owner_objectid;
2119
2120 set_extent_bits(&root->fs_info->pending_del,
2121 bytenr, bytenr + num_bytes - 1,
2122 EXTENT_LOCKED, GFP_NOFS);
2123 set_state_private(&root->fs_info->pending_del,
2124 bytenr, (unsigned long)extent_op);
Chris Masona28ec192007-03-06 20:08:01 -05002125 return 0;
2126 }
Chris Mason4bef0842008-09-08 11:18:08 -04002127 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002128 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2129 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002130 struct btrfs_block_group_cache *cache;
2131
Chris Masond00aff02008-09-11 15:54:42 -04002132 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002133 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2134 BUG_ON(!cache);
2135 btrfs_add_free_space(cache, bytenr, num_bytes);
Zheng Yane8569812008-09-26 10:05:48 -04002136 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002137 return 0;
2138 }
Chris Mason4bef0842008-09-08 11:18:08 -04002139 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002140 }
Chris Mason4bef0842008-09-08 11:18:08 -04002141
2142 /* if data pin when any transaction has committed this */
2143 if (ref_generation != trans->transid)
2144 pin = 1;
2145
Zheng Yan31840ae2008-09-23 13:14:14 -04002146 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2147 root_objectid, ref_generation, owner_objectid,
2148 owner_offset, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002149
2150 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002151 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05002152 return ret ? ret : pending_ret;
2153}
2154
Chris Mason925baed2008-06-25 16:01:30 -04002155int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002156 struct btrfs_root *root,
2157 u64 bytenr, u64 num_bytes, u64 parent,
2158 u64 root_objectid, u64 ref_generation,
2159 u64 owner_objectid, u64 owner_offset, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002160{
2161 int ret;
2162
2163 maybe_lock_mutex(root);
Zheng Yan31840ae2008-09-23 13:14:14 -04002164 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002165 root_objectid, ref_generation,
2166 owner_objectid, owner_offset, pin);
2167 maybe_unlock_mutex(root);
2168 return ret;
2169}
2170
Chris Mason87ee04e2007-11-30 11:30:34 -05002171static u64 stripe_align(struct btrfs_root *root, u64 val)
2172{
2173 u64 mask = ((u64)root->stripesize - 1);
2174 u64 ret = (val + mask) & ~mask;
2175 return ret;
2176}
2177
Chris Masonfec577f2007-02-26 10:40:21 -05002178/*
2179 * walks the btree of allocated extents and find a hole of a given size.
2180 * The key ins is changed to record the hole:
2181 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002182 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002183 * ins->offset == number of blocks
2184 * Any available blocks before search_start are skipped.
2185 */
Chris Mason98ed5172008-01-03 10:01:48 -05002186static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2187 struct btrfs_root *orig_root,
2188 u64 num_bytes, u64 empty_size,
2189 u64 search_start, u64 search_end,
2190 u64 hint_byte, struct btrfs_key *ins,
2191 u64 exclude_start, u64 exclude_nr,
2192 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002193{
Chris Mason87ee04e2007-11-30 11:30:34 -05002194 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04002195 u64 orig_search_start;
Chris Mason9f5fae22007-03-20 14:38:32 -04002196 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04002197 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04002198 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002199 u64 *last_ptr = NULL;
Chris Masonbe08c1b2007-05-03 09:06:49 -04002200 struct btrfs_block_group_cache *block_group;
Chris Mason0ef3e662008-05-24 14:04:53 -04002201 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002202 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002203 int allowed_chunk_alloc = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05002204
Chris Masondb945352007-10-15 16:15:53 -04002205 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002206 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2207
Chris Mason0ef3e662008-05-24 14:04:53 -04002208 if (orig_root->ref_cows || empty_size)
2209 allowed_chunk_alloc = 1;
2210
Chris Mason239b14b2008-03-24 15:02:07 -04002211 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2212 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04002213 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002214 }
2215
Josef Bacik0f9dd462008-09-23 13:14:11 -04002216 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002217 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002218
Chris Masone02119d2008-09-05 16:13:11 -04002219 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2220 last_ptr = &root->fs_info->last_log_alloc;
2221 if (!last_ptr == 0 && root->fs_info->last_alloc) {
2222 *last_ptr = root->fs_info->last_alloc + empty_cluster;
2223 }
2224 }
Chris Mason239b14b2008-03-24 15:02:07 -04002225
2226 if (last_ptr) {
2227 if (*last_ptr)
2228 hint_byte = *last_ptr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002229 else
Chris Mason239b14b2008-03-24 15:02:07 -04002230 empty_size += empty_cluster;
Chris Mason239b14b2008-03-24 15:02:07 -04002231 }
2232
Chris Masona061fc82008-05-07 11:43:44 -04002233 search_start = max(search_start, first_logical_byte(root, 0));
2234 orig_search_start = search_start;
2235
Chris Mason239b14b2008-03-24 15:02:07 -04002236 search_start = max(search_start, hint_byte);
Chris Mason6702ed42007-08-07 16:15:09 -04002237 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002238
Josef Bacik0f9dd462008-09-23 13:14:11 -04002239new_group:
Zheng Yane8569812008-09-26 10:05:48 -04002240 block_group = btrfs_lookup_first_block_group(info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002241
2242 /*
2243 * Ok this looks a little tricky, buts its really simple. First if we
2244 * didn't find a block group obviously we want to start over.
2245 * Secondly, if the block group we found does not match the type we
2246 * need, and we have a last_ptr and its not 0, chances are the last
2247 * allocation we made was at the end of the block group, so lets go
2248 * ahead and skip the looking through the rest of the block groups and
2249 * start at the beginning. This helps with metadata allocations,
2250 * since you are likely to have a bunch of data block groups to search
2251 * through first before you realize that you need to start over, so go
2252 * ahead and start over and save the time.
2253 */
2254 if (!block_group || (!block_group_bits(block_group, data) &&
2255 last_ptr && *last_ptr)) {
2256 if (search_start != orig_search_start) {
2257 if (last_ptr && *last_ptr)
2258 *last_ptr = 0;
2259 search_start = orig_search_start;
2260 goto new_group;
2261 } else if (!chunk_alloc_done && allowed_chunk_alloc) {
2262 ret = do_chunk_alloc(trans, root,
2263 num_bytes + 2 * 1024 * 1024,
2264 data, 1);
Zheng Yane8569812008-09-26 10:05:48 -04002265 if (ret < 0)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002266 goto error;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002267 BUG_ON(ret);
2268 chunk_alloc_done = 1;
2269 search_start = orig_search_start;
2270 goto new_group;
2271 } else {
2272 ret = -ENOSPC;
2273 goto error;
Chris Mason0ef3e662008-05-24 14:04:53 -04002274 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002275 }
2276
2277 /*
2278 * this is going to seach through all of the existing block groups it
2279 * can find, so if we don't find something we need to see if we can
2280 * allocate what we need.
2281 */
2282 ret = find_free_space(root, &block_group, &search_start,
2283 total_needed, data);
2284 if (ret == -ENOSPC) {
2285 /*
2286 * instead of allocating, start at the original search start
2287 * and see if there is something to be found, if not then we
2288 * allocate
2289 */
2290 if (search_start != orig_search_start) {
2291 if (last_ptr && *last_ptr) {
2292 *last_ptr = 0;
2293 total_needed += empty_cluster;
2294 }
2295 search_start = orig_search_start;
2296 goto new_group;
2297 }
2298
2299 /*
2300 * we've already allocated, we're pretty screwed
2301 */
2302 if (chunk_alloc_done) {
2303 goto error;
2304 } else if (!allowed_chunk_alloc && block_group &&
2305 block_group_bits(block_group, data)) {
2306 block_group->space_info->force_alloc = 1;
2307 goto error;
2308 } else if (!allowed_chunk_alloc) {
2309 goto error;
2310 }
2311
2312 ret = do_chunk_alloc(trans, root, num_bytes + 2 * 1024 * 1024,
2313 data, 1);
2314 if (ret < 0)
2315 goto error;
2316
2317 BUG_ON(ret);
Chris Mason0ef3e662008-05-24 14:04:53 -04002318 chunk_alloc_done = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002319 if (block_group)
2320 search_start = block_group->key.objectid +
2321 block_group->key.offset;
2322 else
2323 search_start = orig_search_start;
2324 goto new_group;
Chris Mason0ef3e662008-05-24 14:04:53 -04002325 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002326
Chris Mason0b86a832008-03-24 15:01:56 -04002327 if (ret)
2328 goto error;
2329
Chris Mason87ee04e2007-11-30 11:30:34 -05002330 search_start = stripe_align(root, search_start);
Chris Masonfec577f2007-02-26 10:40:21 -05002331 ins->objectid = search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04002332 ins->offset = num_bytes;
Chris Masone37c9e62007-05-09 20:13:14 -04002333
Josef Bacik0f9dd462008-09-23 13:14:11 -04002334 if (ins->objectid + num_bytes >= search_end) {
2335 search_start = orig_search_start;
2336 if (chunk_alloc_done) {
2337 ret = -ENOSPC;
2338 goto error;
2339 }
2340 goto new_group;
2341 }
Chris Mason0b86a832008-03-24 15:01:56 -04002342
2343 if (ins->objectid + num_bytes >
2344 block_group->key.objectid + block_group->key.offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002345 if (search_start == orig_search_start && chunk_alloc_done) {
2346 ret = -ENOSPC;
2347 goto error;
2348 }
Chris Masone19caa52007-10-15 16:17:44 -04002349 search_start = block_group->key.objectid +
2350 block_group->key.offset;
2351 goto new_group;
2352 }
Chris Mason0b86a832008-03-24 15:01:56 -04002353
Chris Masondb945352007-10-15 16:15:53 -04002354 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04002355 ins->objectid < exclude_start + exclude_nr)) {
2356 search_start = exclude_start + exclude_nr;
2357 goto new_group;
2358 }
Chris Mason0b86a832008-03-24 15:01:56 -04002359
Josef Bacik0f9dd462008-09-23 13:14:11 -04002360 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2361 trans->block_group = block_group;
2362
Chris Masondb945352007-10-15 16:15:53 -04002363 ins->offset = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002364 if (last_ptr) {
2365 *last_ptr = ins->objectid + ins->offset;
2366 if (*last_ptr ==
Josef Bacik0f9dd462008-09-23 13:14:11 -04002367 btrfs_super_total_bytes(&root->fs_info->super_copy))
Chris Mason239b14b2008-03-24 15:02:07 -04002368 *last_ptr = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002369 }
Chris Masonbe744172007-05-06 10:15:01 -04002370
Josef Bacik0f9dd462008-09-23 13:14:11 -04002371 ret = 0;
Chris Mason0f70abe2007-02-28 16:46:22 -05002372error:
Chris Mason0f70abe2007-02-28 16:46:22 -05002373 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002374}
Chris Masonec44a352008-04-28 15:29:52 -04002375
Josef Bacik0f9dd462008-09-23 13:14:11 -04002376static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2377{
2378 struct btrfs_block_group_cache *cache;
2379 struct list_head *l;
2380
2381 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
Zheng Yane8569812008-09-26 10:05:48 -04002382 info->total_bytes - info->bytes_used - info->bytes_pinned -
2383 info->bytes_reserved, (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002384
2385 spin_lock(&info->lock);
2386 list_for_each(l, &info->block_groups) {
2387 cache = list_entry(l, struct btrfs_block_group_cache, list);
2388 spin_lock(&cache->lock);
2389 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
Zheng Yane8569812008-09-26 10:05:48 -04002390 "%Lu pinned %Lu reserved\n",
Josef Bacik0f9dd462008-09-23 13:14:11 -04002391 cache->key.objectid, cache->key.offset,
Zheng Yane8569812008-09-26 10:05:48 -04002392 btrfs_block_group_used(&cache->item),
2393 cache->pinned, cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002394 btrfs_dump_free_space(cache, bytes);
2395 spin_unlock(&cache->lock);
2396 }
2397 spin_unlock(&info->lock);
2398}
Zheng Yane8569812008-09-26 10:05:48 -04002399
Chris Masone6dcd2d2008-07-17 12:53:50 -04002400static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2401 struct btrfs_root *root,
2402 u64 num_bytes, u64 min_alloc_size,
2403 u64 empty_size, u64 hint_byte,
2404 u64 search_end, struct btrfs_key *ins,
2405 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05002406{
2407 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04002408 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002409 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04002410 struct btrfs_fs_info *info = root->fs_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002411 struct btrfs_block_group_cache *cache;
Chris Mason925baed2008-06-25 16:01:30 -04002412
Chris Mason6324fbf2008-03-24 15:01:59 -04002413 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04002414 alloc_profile = info->avail_data_alloc_bits &
2415 info->data_alloc_profile;
2416 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002417 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04002418 alloc_profile = info->avail_system_alloc_bits &
2419 info->system_alloc_profile;
2420 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002421 } else {
Chris Mason8790d502008-04-03 16:29:03 -04002422 alloc_profile = info->avail_metadata_alloc_bits &
2423 info->metadata_alloc_profile;
2424 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002425 }
Chris Mason98d20f62008-04-14 09:46:10 -04002426again:
Chris Masona061fc82008-05-07 11:43:44 -04002427 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04002428 /*
2429 * the only place that sets empty_size is btrfs_realloc_node, which
2430 * is not called recursively on allocations
2431 */
2432 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04002433 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04002434 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002435 2 * 1024 * 1024,
2436 BTRFS_BLOCK_GROUP_METADATA |
2437 (info->metadata_alloc_profile &
2438 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002439 }
2440 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002441 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002442 }
Chris Mason0b86a832008-03-24 15:01:56 -04002443
Chris Masondb945352007-10-15 16:15:53 -04002444 WARN_ON(num_bytes < root->sectorsize);
2445 ret = find_free_extent(trans, root, num_bytes, empty_size,
2446 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002447 trans->alloc_exclude_start,
2448 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002449
Chris Mason98d20f62008-04-14 09:46:10 -04002450 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2451 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002452 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002453 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002454 do_chunk_alloc(trans, root->fs_info->extent_root,
2455 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002456 goto again;
2457 }
Chris Masonec44a352008-04-28 15:29:52 -04002458 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002459 struct btrfs_space_info *sinfo;
2460
2461 sinfo = __find_space_info(root->fs_info, data);
2462 printk("allocation failed flags %Lu, wanted %Lu\n",
2463 data, num_bytes);
2464 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04002465 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002466 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002467 cache = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2468 if (!cache) {
2469 printk(KERN_ERR "Unable to find block group for %Lu\n", ins->objectid);
2470 return -ENOSPC;
2471 }
2472
2473 ret = btrfs_remove_free_space(cache, ins->objectid, ins->offset);
2474
2475 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002476}
2477
Chris Mason65b51a02008-08-01 15:11:20 -04002478int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2479{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002480 struct btrfs_block_group_cache *cache;
2481
Chris Mason65b51a02008-08-01 15:11:20 -04002482 maybe_lock_mutex(root);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002483 cache = btrfs_lookup_block_group(root->fs_info, start);
2484 if (!cache) {
2485 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
2486 maybe_unlock_mutex(root);
2487 return -ENOSPC;
2488 }
2489 btrfs_add_free_space(cache, start, len);
Chris Mason65b51a02008-08-01 15:11:20 -04002490 maybe_unlock_mutex(root);
2491 return 0;
2492}
2493
Chris Masone6dcd2d2008-07-17 12:53:50 -04002494int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2495 struct btrfs_root *root,
2496 u64 num_bytes, u64 min_alloc_size,
2497 u64 empty_size, u64 hint_byte,
2498 u64 search_end, struct btrfs_key *ins,
2499 u64 data)
2500{
2501 int ret;
2502 maybe_lock_mutex(root);
2503 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2504 empty_size, hint_byte, search_end, ins,
2505 data);
Zheng Yane8569812008-09-26 10:05:48 -04002506 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002507 maybe_unlock_mutex(root);
2508 return ret;
2509}
2510
2511static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002512 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002513 u64 root_objectid, u64 ref_generation,
2514 u64 owner, u64 owner_offset,
2515 struct btrfs_key *ins)
2516{
2517 int ret;
2518 int pending_ret;
2519 u64 super_used;
2520 u64 root_used;
2521 u64 num_bytes = ins->offset;
2522 u32 sizes[2];
2523 struct btrfs_fs_info *info = root->fs_info;
2524 struct btrfs_root *extent_root = info->extent_root;
2525 struct btrfs_extent_item *extent_item;
2526 struct btrfs_extent_ref *ref;
2527 struct btrfs_path *path;
2528 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002529
Zheng Yan31840ae2008-09-23 13:14:14 -04002530 if (parent == 0)
2531 parent = ins->objectid;
2532
Josef Bacik58176a92007-08-29 15:47:34 -04002533 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002534 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002535 super_used = btrfs_super_bytes_used(&info->super_copy);
2536 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002537 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04002538
Josef Bacik58176a92007-08-29 15:47:34 -04002539 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002540 root_used = btrfs_root_used(&root->root_item);
2541 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002542
Chris Mason26b80032007-08-08 20:17:12 -04002543 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002544 struct pending_extent_op *extent_op;
2545
2546 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2547 BUG_ON(!extent_op);
2548
2549 extent_op->type = PENDING_EXTENT_INSERT;
2550 extent_op->bytenr = ins->objectid;
2551 extent_op->num_bytes = ins->offset;
2552 extent_op->parent = parent;
2553 extent_op->orig_parent = 0;
2554 extent_op->generation = ref_generation;
2555 extent_op->orig_generation = 0;
2556 extent_op->level = (int)owner;
2557
Chris Mason1a5bc1672007-10-15 16:15:26 -04002558 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2559 ins->objectid + ins->offset - 1,
2560 EXTENT_LOCKED, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002561 set_state_private(&root->fs_info->extent_ins,
2562 ins->objectid, (unsigned long)extent_op);
Chris Mason26b80032007-08-08 20:17:12 -04002563 goto update_block;
2564 }
2565
Chris Mason47e4bb92008-02-01 14:51:59 -05002566 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05002567 keys[1].objectid = ins->objectid;
2568 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04002569 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05002570 sizes[0] = sizeof(*extent_item);
2571 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002572
2573 path = btrfs_alloc_path();
2574 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002575
2576 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2577 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04002578 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002579
Chris Mason47e4bb92008-02-01 14:51:59 -05002580 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2581 struct btrfs_extent_item);
2582 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2583 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2584 struct btrfs_extent_ref);
2585
2586 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2587 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2588 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2589 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
Zheng Yan31840ae2008-09-23 13:14:14 -04002590 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05002591
2592 btrfs_mark_buffer_dirty(path->nodes[0]);
2593
2594 trans->alloc_exclude_start = 0;
2595 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002596 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002597 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002598 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04002599
Chris Mason925baed2008-06-25 16:01:30 -04002600 if (ret)
2601 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002602 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04002603 ret = pending_ret;
2604 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002605 }
Chris Mason26b80032007-08-08 20:17:12 -04002606
2607update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04002608 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002609 if (ret) {
2610 printk("update block group failed for %Lu %Lu\n",
2611 ins->objectid, ins->offset);
2612 BUG();
2613 }
Chris Mason925baed2008-06-25 16:01:30 -04002614out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04002615 return ret;
2616}
2617
2618int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002619 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002620 u64 root_objectid, u64 ref_generation,
2621 u64 owner, u64 owner_offset,
2622 struct btrfs_key *ins)
2623{
2624 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04002625
2626 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2627 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002628 maybe_lock_mutex(root);
Zheng Yan31840ae2008-09-23 13:14:14 -04002629 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2630 root_objectid, ref_generation,
2631 owner, owner_offset, ins);
Zheng Yane8569812008-09-26 10:05:48 -04002632 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002633 maybe_unlock_mutex(root);
2634 return ret;
2635}
Chris Masone02119d2008-09-05 16:13:11 -04002636
2637/*
2638 * this is used by the tree logging recovery code. It records that
2639 * an extent has been allocated and makes sure to clear the free
2640 * space cache bits as well
2641 */
2642int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002643 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04002644 u64 root_objectid, u64 ref_generation,
2645 u64 owner, u64 owner_offset,
2646 struct btrfs_key *ins)
2647{
2648 int ret;
2649 struct btrfs_block_group_cache *block_group;
2650
2651 maybe_lock_mutex(root);
2652 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2653 cache_block_group(root, block_group);
2654
Josef Bacik0f9dd462008-09-23 13:14:11 -04002655 ret = btrfs_remove_free_space(block_group, ins->objectid, ins->offset);
2656 BUG_ON(ret);
Zheng Yan31840ae2008-09-23 13:14:14 -04002657 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2658 root_objectid, ref_generation,
2659 owner, owner_offset, ins);
Chris Masone02119d2008-09-05 16:13:11 -04002660 maybe_unlock_mutex(root);
2661 return ret;
2662}
2663
Chris Masone6dcd2d2008-07-17 12:53:50 -04002664/*
2665 * finds a free extent and does all the dirty work required for allocation
2666 * returns the key for the extent through ins, and a tree buffer for
2667 * the first block of the extent through buf.
2668 *
2669 * returns 0 if everything worked, non-zero otherwise.
2670 */
2671int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2672 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002673 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002674 u64 root_objectid, u64 ref_generation,
Zheng Yan31840ae2008-09-23 13:14:14 -04002675 u64 owner_objectid, u64 owner_offset,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002676 u64 empty_size, u64 hint_byte,
2677 u64 search_end, struct btrfs_key *ins, u64 data)
2678{
2679 int ret;
2680
2681 maybe_lock_mutex(root);
2682
2683 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2684 min_alloc_size, empty_size, hint_byte,
2685 search_end, ins, data);
2686 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04002687 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002688 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2689 root_objectid, ref_generation,
2690 owner_objectid, owner_offset, ins);
Chris Masond00aff02008-09-11 15:54:42 -04002691 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002692
Zheng Yane8569812008-09-26 10:05:48 -04002693 } else {
2694 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04002695 }
Chris Mason925baed2008-06-25 16:01:30 -04002696 maybe_unlock_mutex(root);
2697 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002698}
Chris Mason65b51a02008-08-01 15:11:20 -04002699
2700struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2701 struct btrfs_root *root,
2702 u64 bytenr, u32 blocksize)
2703{
2704 struct extent_buffer *buf;
2705
2706 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2707 if (!buf)
2708 return ERR_PTR(-ENOMEM);
2709 btrfs_set_header_generation(buf, trans->transid);
2710 btrfs_tree_lock(buf);
2711 clean_tree_block(trans, root, buf);
2712 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04002713 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2714 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04002715 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002716 } else {
2717 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2718 buf->start + buf->len - 1, GFP_NOFS);
2719 }
Chris Mason65b51a02008-08-01 15:11:20 -04002720 trans->blocks_used++;
2721 return buf;
2722}
2723
Chris Masonfec577f2007-02-26 10:40:21 -05002724/*
2725 * helper function to allocate a block for a given tree
2726 * returns the tree buffer or NULL.
2727 */
Chris Mason5f39d392007-10-15 16:14:19 -04002728struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04002729 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002730 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002731 u64 root_objectid,
2732 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05002733 int level,
2734 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04002735 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05002736{
Chris Masone2fa7222007-03-12 16:22:34 -04002737 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05002738 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002739 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05002740
Zheng Yan31840ae2008-09-23 13:14:14 -04002741 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
2742 root_objectid, ref_generation, level, 0,
2743 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002744 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04002745 BUG_ON(ret > 0);
2746 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05002747 }
Chris Mason55c69072008-01-09 15:55:33 -05002748
Chris Mason65b51a02008-08-01 15:11:20 -04002749 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05002750 return buf;
2751}
Chris Masona28ec192007-03-06 20:08:01 -05002752
Chris Masone02119d2008-09-05 16:13:11 -04002753int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
2754 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04002755{
Chris Mason7bb86312007-12-11 09:25:06 -05002756 u64 leaf_owner;
2757 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04002758 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002759 struct btrfs_file_extent_item *fi;
2760 int i;
2761 int nritems;
2762 int ret;
2763
Chris Mason5f39d392007-10-15 16:14:19 -04002764 BUG_ON(!btrfs_is_leaf(leaf));
2765 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05002766 leaf_owner = btrfs_header_owner(leaf);
2767 leaf_generation = btrfs_header_generation(leaf);
2768
Chris Mason6407bf62007-03-27 06:33:00 -04002769 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002770 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04002771 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04002772
2773 btrfs_item_key_to_cpu(leaf, &key, i);
2774 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04002775 continue;
2776 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002777 if (btrfs_file_extent_type(leaf, fi) ==
2778 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04002779 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04002780 /*
2781 * FIXME make sure to insert a trans record that
2782 * repeats the snapshot del on crash
2783 */
Chris Masondb945352007-10-15 16:15:53 -04002784 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2785 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04002786 continue;
Chris Mason4a096752008-07-21 10:29:44 -04002787
2788 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002789 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002790 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04002791 leaf->start, leaf_owner, leaf_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05002792 key.objectid, key.offset, 0);
Chris Mason4a096752008-07-21 10:29:44 -04002793 mutex_unlock(&root->fs_info->alloc_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002794 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04002795
2796 atomic_inc(&root->fs_info->throttle_gen);
2797 wake_up(&root->fs_info->transaction_throttle);
2798 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04002799 }
2800 return 0;
2801}
2802
Chris Masone02119d2008-09-05 16:13:11 -04002803static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2804 struct btrfs_root *root,
2805 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04002806{
2807 int i;
2808 int ret;
2809 struct btrfs_extent_info *info = ref->extents;
2810
Yan Zheng31153d82008-07-28 15:32:19 -04002811 for (i = 0; i < ref->nritems; i++) {
2812 mutex_lock(&root->fs_info->alloc_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002813 ret = __btrfs_free_extent(trans, root, info->bytenr,
2814 info->num_bytes, ref->bytenr,
2815 ref->owner, ref->generation,
2816 info->objectid, info->offset, 0);
Yan Zheng31153d82008-07-28 15:32:19 -04002817 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason2dd3e672008-08-04 08:20:15 -04002818
2819 atomic_inc(&root->fs_info->throttle_gen);
2820 wake_up(&root->fs_info->transaction_throttle);
2821 cond_resched();
2822
Yan Zheng31153d82008-07-28 15:32:19 -04002823 BUG_ON(ret);
2824 info++;
2825 }
Yan Zheng31153d82008-07-28 15:32:19 -04002826
2827 return 0;
2828}
2829
Chris Mason333db942008-06-25 16:01:30 -04002830int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2831 u32 *refs)
2832{
Chris Mason017e5362008-07-28 15:32:51 -04002833 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04002834
Zheng Yan31840ae2008-09-23 13:14:14 -04002835 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04002836 BUG_ON(ret);
2837
2838#if 0 // some debugging code in case we see problems here
2839 /* if the refs count is one, it won't get increased again. But
2840 * if the ref count is > 1, someone may be decreasing it at
2841 * the same time we are.
2842 */
2843 if (*refs != 1) {
2844 struct extent_buffer *eb = NULL;
2845 eb = btrfs_find_create_tree_block(root, start, len);
2846 if (eb)
2847 btrfs_tree_lock(eb);
2848
2849 mutex_lock(&root->fs_info->alloc_mutex);
2850 ret = lookup_extent_ref(NULL, root, start, len, refs);
2851 BUG_ON(ret);
2852 mutex_unlock(&root->fs_info->alloc_mutex);
2853
2854 if (eb) {
2855 btrfs_tree_unlock(eb);
2856 free_extent_buffer(eb);
2857 }
2858 if (*refs == 1) {
2859 printk("block %llu went down to one during drop_snap\n",
2860 (unsigned long long)start);
2861 }
2862
2863 }
2864#endif
2865
Chris Masone7a84562008-06-25 16:01:31 -04002866 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04002867 return ret;
Chris Mason333db942008-06-25 16:01:30 -04002868}
2869
2870/*
Chris Mason9aca1d52007-03-13 11:09:37 -04002871 * helper function for drop_snapshot, this walks down the tree dropping ref
2872 * counts as it goes.
2873 */
Chris Mason98ed5172008-01-03 10:01:48 -05002874static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2875 struct btrfs_root *root,
2876 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002877{
Chris Mason7bb86312007-12-11 09:25:06 -05002878 u64 root_owner;
2879 u64 root_gen;
2880 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04002881 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002882 struct extent_buffer *next;
2883 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002884 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04002885 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04002886 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002887 int ret;
2888 u32 refs;
2889
Chris Mason5caf2a02007-04-02 11:20:42 -04002890 WARN_ON(*level < 0);
2891 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04002892 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04002893 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002894 BUG_ON(ret);
2895 if (refs > 1)
2896 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002897
Chris Mason9aca1d52007-03-13 11:09:37 -04002898 /*
2899 * walk down to the last node level and free all the leaves
2900 */
Chris Mason6407bf62007-03-27 06:33:00 -04002901 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002902 WARN_ON(*level < 0);
2903 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002904 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002905
Chris Mason5f39d392007-10-15 16:14:19 -04002906 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002907 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002908
Chris Mason7518a232007-03-12 12:01:18 -04002909 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002910 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002911 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002912 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002913 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04002914 BUG_ON(ret);
2915 break;
2916 }
Chris Masondb945352007-10-15 16:15:53 -04002917 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04002918 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04002919 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002920
Chris Mason333db942008-06-25 16:01:30 -04002921 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002922 BUG_ON(ret);
2923 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002924 parent = path->nodes[*level];
2925 root_owner = btrfs_header_owner(parent);
2926 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002927 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04002928
2929 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002930 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04002931 blocksize, parent->start,
2932 root_owner, root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002933 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002934 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason18e35e0a2008-08-01 13:11:41 -04002935
2936 atomic_inc(&root->fs_info->throttle_gen);
2937 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04002938 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04002939
Chris Mason20524f02007-03-10 06:35:47 -05002940 continue;
2941 }
Chris Masonf87f0572008-08-01 11:27:23 -04002942 /*
2943 * at this point, we have a single ref, and since the
2944 * only place referencing this extent is a dead root
2945 * the reference count should never go higher.
2946 * So, we don't need to check it again
2947 */
Yan Zheng31153d82008-07-28 15:32:19 -04002948 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04002949 ref = btrfs_lookup_leaf_ref(root, bytenr);
Yan Zheng31153d82008-07-28 15:32:19 -04002950 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04002951 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002952 BUG_ON(ret);
2953 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04002954 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002955 *level = 0;
2956 break;
2957 }
Chris Mason37d1aee2008-07-31 10:48:37 -04002958 if (printk_ratelimit())
2959 printk("leaf ref miss for bytenr %llu\n",
2960 (unsigned long long)bytenr);
Yan Zheng31153d82008-07-28 15:32:19 -04002961 }
Chris Masondb945352007-10-15 16:15:53 -04002962 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04002963 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002964 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04002965
Chris Masonca7a79a2008-05-12 12:59:19 -04002966 next = read_tree_block(root, bytenr, blocksize,
2967 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04002968 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04002969#if 0
2970 /*
2971 * this is a debugging check and can go away
2972 * the ref should never go all the way down to 1
2973 * at this point
2974 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002975 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2976 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002977 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002978 WARN_ON(refs != 1);
2979#endif
Chris Masone9d0b132007-08-10 14:06:19 -04002980 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002981 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002982 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002983 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002984 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002985 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002986 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04002987 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05002988 }
2989out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002990 WARN_ON(*level < 0);
2991 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002992
2993 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05002994 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04002995 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05002996 } else {
2997 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04002998 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05002999 }
3000
Yan Zheng31153d82008-07-28 15:32:19 -04003001 blocksize = btrfs_level_size(root, *level);
3002 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003003 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003004
Chris Masonf87f0572008-08-01 11:27:23 -04003005 mutex_lock(&root->fs_info->alloc_mutex);
Yan Zheng31153d82008-07-28 15:32:19 -04003006 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003007 parent->start, root_owner, root_gen,
3008 0, 0, 1);
3009 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04003010 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003011 path->nodes[*level] = NULL;
3012 *level += 1;
3013 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003014
Chris Masone7a84562008-06-25 16:01:31 -04003015 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003016 return 0;
3017}
3018
Chris Mason9aca1d52007-03-13 11:09:37 -04003019/*
3020 * helper for dropping snapshots. This walks back up the tree in the path
3021 * to find the first node higher up where we haven't yet gone through
3022 * all the slots
3023 */
Chris Mason98ed5172008-01-03 10:01:48 -05003024static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3025 struct btrfs_root *root,
3026 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003027{
Chris Mason7bb86312007-12-11 09:25:06 -05003028 u64 root_owner;
3029 u64 root_gen;
3030 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003031 int i;
3032 int slot;
3033 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003034
Chris Mason234b63a2007-03-13 10:46:10 -04003035 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003036 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003037 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3038 struct extent_buffer *node;
3039 struct btrfs_disk_key disk_key;
3040 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003041 path->slots[i]++;
3042 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003043 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003044 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003045 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003046 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003047 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003048 return 0;
3049 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003050 struct extent_buffer *parent;
3051 if (path->nodes[*level] == root->node)
3052 parent = path->nodes[*level];
3053 else
3054 parent = path->nodes[*level + 1];
3055
3056 root_owner = btrfs_header_owner(parent);
3057 root_gen = btrfs_header_generation(parent);
Chris Masone089f052007-03-16 16:20:31 -04003058 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003059 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003060 path->nodes[*level]->len,
Zheng Yan31840ae2008-09-23 13:14:14 -04003061 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003062 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003063 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003064 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003065 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003066 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003067 }
3068 }
3069 return 1;
3070}
3071
Chris Mason9aca1d52007-03-13 11:09:37 -04003072/*
3073 * drop the reference count on the tree rooted at 'snap'. This traverses
3074 * the tree freeing any blocks that have a ref count of zero after being
3075 * decremented.
3076 */
Chris Masone089f052007-03-16 16:20:31 -04003077int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003078 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003079{
Chris Mason3768f362007-03-13 16:47:54 -04003080 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003081 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003082 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003083 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003084 int i;
3085 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003086 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003087
Chris Masona2135012008-06-25 16:01:30 -04003088 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003089 path = btrfs_alloc_path();
3090 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003091
Chris Mason5f39d392007-10-15 16:14:19 -04003092 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003093 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003094 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3095 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003096 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003097 path->slots[level] = 0;
3098 } else {
3099 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003100 struct btrfs_disk_key found_key;
3101 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003102
Chris Mason9f3a7422007-08-07 15:52:19 -04003103 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003104 level = root_item->drop_level;
3105 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003106 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003107 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003108 ret = wret;
3109 goto out;
3110 }
Chris Mason5f39d392007-10-15 16:14:19 -04003111 node = path->nodes[level];
3112 btrfs_node_key(node, &found_key, path->slots[level]);
3113 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3114 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003115 /*
3116 * unlock our path, this is safe because only this
3117 * function is allowed to delete this snapshot
3118 */
Chris Mason925baed2008-06-25 16:01:30 -04003119 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3120 if (path->nodes[i] && path->locks[i]) {
3121 path->locks[i] = 0;
3122 btrfs_tree_unlock(path->nodes[i]);
3123 }
3124 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003125 }
Chris Mason20524f02007-03-10 06:35:47 -05003126 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003127 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003128 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003129 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003130 if (wret < 0)
3131 ret = wret;
3132
Chris Mason5caf2a02007-04-02 11:20:42 -04003133 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003134 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003135 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003136 if (wret < 0)
3137 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003138 if (trans->transaction->in_commit) {
3139 ret = -EAGAIN;
3140 break;
3141 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003142 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003143 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003144 }
Chris Mason83e15a22007-03-12 09:03:27 -04003145 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003146 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003147 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003148 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003149 }
Chris Mason20524f02007-03-10 06:35:47 -05003150 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003151out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003152 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003153 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003154}
Chris Mason9078a3e2007-04-26 16:46:15 -04003155
Chris Masonbe744172007-05-06 10:15:01 -04003156int btrfs_free_block_groups(struct btrfs_fs_info *info)
3157{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003158 struct btrfs_block_group_cache *block_group;
3159 struct rb_node *n;
Chris Mason925baed2008-06-25 16:01:30 -04003160
3161 mutex_lock(&info->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003162 spin_lock(&info->block_group_cache_lock);
3163 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
3164 block_group = rb_entry(n, struct btrfs_block_group_cache,
3165 cache_node);
3166
Chris Mason4434c332008-09-24 12:50:10 -04003167 spin_unlock(&info->block_group_cache_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003168 btrfs_remove_free_space_cache(block_group);
Chris Mason4434c332008-09-24 12:50:10 -04003169 spin_lock(&info->block_group_cache_lock);
3170
Josef Bacik0f9dd462008-09-23 13:14:11 -04003171 rb_erase(&block_group->cache_node,
3172 &info->block_group_cache_tree);
Chris Mason4434c332008-09-24 12:50:10 -04003173
Josef Bacik0f9dd462008-09-23 13:14:11 -04003174 spin_lock(&block_group->space_info->lock);
3175 list_del(&block_group->list);
3176 spin_unlock(&block_group->space_info->lock);
3177 kfree(block_group);
Chris Mason96b51792007-10-15 16:15:19 -04003178 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003179 spin_unlock(&info->block_group_cache_lock);
Chris Mason925baed2008-06-25 16:01:30 -04003180 mutex_unlock(&info->alloc_mutex);
Chris Masonbe744172007-05-06 10:15:01 -04003181 return 0;
3182}
3183
Chris Mason8e7bf942008-04-28 09:02:36 -04003184static unsigned long calc_ra(unsigned long start, unsigned long last,
3185 unsigned long nr)
3186{
3187 return min(last, start + nr - 1);
3188}
3189
Chris Mason98ed5172008-01-03 10:01:48 -05003190static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3191 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003192{
3193 u64 page_start;
3194 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05003195 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003196 unsigned long i;
3197 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003198 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003199 struct file_ra_state *ra;
Chris Mason8e7bf942008-04-28 09:02:36 -04003200 unsigned long total_read = 0;
3201 unsigned long ra_pages;
Chris Mason3eaa2882008-07-24 11:57:52 -04003202 struct btrfs_ordered_extent *ordered;
Chris Masona061fc82008-05-07 11:43:44 -04003203 struct btrfs_trans_handle *trans;
Chris Mason4313b392008-01-03 09:08:48 -05003204
3205 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003206
3207 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05003208 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003209 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3210
Chris Mason8e7bf942008-04-28 09:02:36 -04003211 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
3212
Chris Mason4313b392008-01-03 09:08:48 -05003213 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003214
Chris Mason4313b392008-01-03 09:08:48 -05003215 for (; i <= last_index; i++) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003216 if (total_read % ra_pages == 0) {
3217 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3218 calc_ra(i, last_index, ra_pages));
3219 }
3220 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003221again:
3222 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Chris Masona061fc82008-05-07 11:43:44 -04003223 goto truncate_racing;
Chris Masonedbd8d42007-12-21 16:27:24 -05003224 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003225 if (!page) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003226 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003227 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003228 if (!PageUptodate(page)) {
3229 btrfs_readpage(NULL, page);
3230 lock_page(page);
3231 if (!PageUptodate(page)) {
3232 unlock_page(page);
3233 page_cache_release(page);
3234 goto out_unlock;
3235 }
3236 }
Chris Masonec44a352008-04-28 15:29:52 -04003237 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003238
Chris Masonedbd8d42007-12-21 16:27:24 -05003239 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3240 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003241 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003242
Chris Mason3eaa2882008-07-24 11:57:52 -04003243 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3244 if (ordered) {
3245 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3246 unlock_page(page);
3247 page_cache_release(page);
3248 btrfs_start_ordered_extent(inode, ordered, 1);
3249 btrfs_put_ordered_extent(ordered);
3250 goto again;
3251 }
3252 set_page_extent_mapped(page);
3253
Chris Masonf87f0572008-08-01 11:27:23 -04003254 /*
3255 * make sure page_mkwrite is called for this page if userland
3256 * wants to change it from mmap
3257 */
3258 clear_page_dirty_for_io(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003259
Chris Masonea8c2812008-08-04 23:17:27 -04003260 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masona061fc82008-05-07 11:43:44 -04003261 set_page_dirty(page);
Chris Masonedbd8d42007-12-21 16:27:24 -05003262
Chris Masond1310b22008-01-24 16:13:08 -05003263 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003264 unlock_page(page);
3265 page_cache_release(page);
3266 }
3267
3268out_unlock:
Chris Mason3eaa2882008-07-24 11:57:52 -04003269 /* we have to start the IO in order to get the ordered extents
3270 * instantiated. This allows the relocation to code to wait
3271 * for all the ordered extents to hit the disk.
3272 *
3273 * Otherwise, it would constantly loop over the same extents
3274 * because the old ones don't get deleted until the IO is
3275 * started
3276 */
3277 btrfs_fdatawrite_range(inode->i_mapping, start, start + len - 1,
3278 WB_SYNC_NONE);
Chris Masonec44a352008-04-28 15:29:52 -04003279 kfree(ra);
Chris Masona061fc82008-05-07 11:43:44 -04003280 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
3281 if (trans) {
Chris Masona061fc82008-05-07 11:43:44 -04003282 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
3283 mark_inode_dirty(inode);
3284 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003285 mutex_unlock(&inode->i_mutex);
3286 return 0;
Chris Masona061fc82008-05-07 11:43:44 -04003287
3288truncate_racing:
3289 vmtruncate(inode, inode->i_size);
3290 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
3291 total_read);
3292 goto out_unlock;
Chris Masonedbd8d42007-12-21 16:27:24 -05003293}
3294
Chris Mason4313b392008-01-03 09:08:48 -05003295/*
Chris Masonbf4ef672008-05-08 13:26:18 -04003296 * The back references tell us which tree holds a ref on a block,
3297 * but it is possible for the tree root field in the reference to
3298 * reflect the original root before a snapshot was made. In this
3299 * case we should search through all the children of a given root
3300 * to find potential holders of references on a block.
3301 *
3302 * Instead, we do something a little less fancy and just search
3303 * all the roots for a given key/block combination.
3304 */
3305static int find_root_for_ref(struct btrfs_root *root,
3306 struct btrfs_path *path,
3307 struct btrfs_key *key0,
3308 int level,
3309 int file_key,
3310 struct btrfs_root **found_root,
3311 u64 bytenr)
3312{
3313 struct btrfs_key root_location;
3314 struct btrfs_root *cur_root = *found_root;
3315 struct btrfs_file_extent_item *file_extent;
3316 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
3317 u64 found_bytenr;
3318 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04003319
3320 root_location.offset = (u64)-1;
3321 root_location.type = BTRFS_ROOT_ITEM_KEY;
3322 path->lowest_level = level;
3323 path->reada = 0;
3324 while(1) {
3325 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
3326 found_bytenr = 0;
3327 if (ret == 0 && file_key) {
3328 struct extent_buffer *leaf = path->nodes[0];
3329 file_extent = btrfs_item_ptr(leaf, path->slots[0],
3330 struct btrfs_file_extent_item);
3331 if (btrfs_file_extent_type(leaf, file_extent) ==
3332 BTRFS_FILE_EXTENT_REG) {
3333 found_bytenr =
3334 btrfs_file_extent_disk_bytenr(leaf,
3335 file_extent);
3336 }
Chris Mason323da792008-05-09 11:46:48 -04003337 } else if (!file_key) {
Chris Masonbf4ef672008-05-08 13:26:18 -04003338 if (path->nodes[level])
3339 found_bytenr = path->nodes[level]->start;
3340 }
3341
Chris Masonbf4ef672008-05-08 13:26:18 -04003342 btrfs_release_path(cur_root, path);
3343
3344 if (found_bytenr == bytenr) {
3345 *found_root = cur_root;
3346 ret = 0;
3347 goto out;
3348 }
3349 ret = btrfs_search_root(root->fs_info->tree_root,
3350 root_search_start, &root_search_start);
3351 if (ret)
3352 break;
3353
3354 root_location.objectid = root_search_start;
3355 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
3356 &root_location);
3357 if (!cur_root) {
3358 ret = 1;
3359 break;
3360 }
3361 }
3362out:
3363 path->lowest_level = 0;
3364 return ret;
3365}
3366
3367/*
Chris Mason4313b392008-01-03 09:08:48 -05003368 * note, this releases the path
3369 */
Chris Mason98ed5172008-01-03 10:01:48 -05003370static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05003371 struct btrfs_path *path,
Chris Mason0ef3e662008-05-24 14:04:53 -04003372 struct btrfs_key *extent_key,
3373 u64 *last_file_objectid,
3374 u64 *last_file_offset,
3375 u64 *last_file_root,
3376 u64 last_extent)
Chris Masonedbd8d42007-12-21 16:27:24 -05003377{
3378 struct inode *inode;
3379 struct btrfs_root *found_root;
Chris Masonbf4ef672008-05-08 13:26:18 -04003380 struct btrfs_key root_location;
3381 struct btrfs_key found_key;
Chris Mason4313b392008-01-03 09:08:48 -05003382 struct btrfs_extent_ref *ref;
3383 u64 ref_root;
3384 u64 ref_gen;
3385 u64 ref_objectid;
3386 u64 ref_offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05003387 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04003388 int level;
Chris Masonedbd8d42007-12-21 16:27:24 -05003389
Chris Mason7d9eb122008-07-08 14:19:17 -04003390 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
3391
Chris Mason4313b392008-01-03 09:08:48 -05003392 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
3393 struct btrfs_extent_ref);
3394 ref_root = btrfs_ref_root(path->nodes[0], ref);
3395 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
3396 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
3397 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
3398 btrfs_release_path(extent_root, path);
3399
Chris Masonbf4ef672008-05-08 13:26:18 -04003400 root_location.objectid = ref_root;
Chris Masonedbd8d42007-12-21 16:27:24 -05003401 if (ref_gen == 0)
Chris Masonbf4ef672008-05-08 13:26:18 -04003402 root_location.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003403 else
Chris Masonbf4ef672008-05-08 13:26:18 -04003404 root_location.offset = (u64)-1;
3405 root_location.type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05003406
3407 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Masonbf4ef672008-05-08 13:26:18 -04003408 &root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05003409 BUG_ON(!found_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003410 mutex_unlock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003411
3412 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Chris Masonbf4ef672008-05-08 13:26:18 -04003413 found_key.objectid = ref_objectid;
3414 found_key.type = BTRFS_EXTENT_DATA_KEY;
3415 found_key.offset = ref_offset;
3416 level = 0;
3417
Chris Mason0ef3e662008-05-24 14:04:53 -04003418 if (last_extent == extent_key->objectid &&
3419 *last_file_objectid == ref_objectid &&
3420 *last_file_offset == ref_offset &&
3421 *last_file_root == ref_root)
3422 goto out;
3423
Chris Masonbf4ef672008-05-08 13:26:18 -04003424 ret = find_root_for_ref(extent_root, path, &found_key,
3425 level, 1, &found_root,
3426 extent_key->objectid);
3427
3428 if (ret)
3429 goto out;
3430
Chris Mason0ef3e662008-05-24 14:04:53 -04003431 if (last_extent == extent_key->objectid &&
3432 *last_file_objectid == ref_objectid &&
3433 *last_file_offset == ref_offset &&
3434 *last_file_root == ref_root)
3435 goto out;
3436
Chris Masonedbd8d42007-12-21 16:27:24 -05003437 inode = btrfs_iget_locked(extent_root->fs_info->sb,
3438 ref_objectid, found_root);
3439 if (inode->i_state & I_NEW) {
3440 /* the inode and parent dir are two different roots */
3441 BTRFS_I(inode)->root = found_root;
3442 BTRFS_I(inode)->location.objectid = ref_objectid;
3443 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
3444 BTRFS_I(inode)->location.offset = 0;
3445 btrfs_read_locked_inode(inode);
3446 unlock_new_inode(inode);
3447
3448 }
3449 /* this can happen if the reference is not against
3450 * the latest version of the tree root
3451 */
Chris Mason7d9eb122008-07-08 14:19:17 -04003452 if (is_bad_inode(inode))
Chris Masonedbd8d42007-12-21 16:27:24 -05003453 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04003454
Chris Mason0ef3e662008-05-24 14:04:53 -04003455 *last_file_objectid = inode->i_ino;
3456 *last_file_root = found_root->root_key.objectid;
3457 *last_file_offset = ref_offset;
3458
Chris Masonedbd8d42007-12-21 16:27:24 -05003459 relocate_inode_pages(inode, ref_offset, extent_key->offset);
Chris Masonedbd8d42007-12-21 16:27:24 -05003460 iput(inode);
Chris Masonedbd8d42007-12-21 16:27:24 -05003461 } else {
3462 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05003463 struct extent_buffer *eb;
Chris Mason7d9eb122008-07-08 14:19:17 -04003464 int needs_lock = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003465
Chris Masonedbd8d42007-12-21 16:27:24 -05003466 eb = read_tree_block(found_root, extent_key->objectid,
Chris Masonca7a79a2008-05-12 12:59:19 -04003467 extent_key->offset, 0);
Chris Mason925baed2008-06-25 16:01:30 -04003468 btrfs_tree_lock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05003469 level = btrfs_header_level(eb);
3470
3471 if (level == 0)
3472 btrfs_item_key_to_cpu(eb, &found_key, 0);
3473 else
3474 btrfs_node_key_to_cpu(eb, &found_key, 0);
3475
Chris Mason925baed2008-06-25 16:01:30 -04003476 btrfs_tree_unlock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05003477 free_extent_buffer(eb);
3478
Chris Masonbf4ef672008-05-08 13:26:18 -04003479 ret = find_root_for_ref(extent_root, path, &found_key,
3480 level, 0, &found_root,
3481 extent_key->objectid);
3482
3483 if (ret)
3484 goto out;
3485
Chris Mason7d9eb122008-07-08 14:19:17 -04003486 /*
3487 * right here almost anything could happen to our key,
3488 * but that's ok. The cow below will either relocate it
3489 * or someone else will have relocated it. Either way,
3490 * it is in a different spot than it was before and
3491 * we're happy.
3492 */
3493
Chris Masonbf4ef672008-05-08 13:26:18 -04003494 trans = btrfs_start_transaction(found_root, 1);
3495
Chris Mason7d9eb122008-07-08 14:19:17 -04003496 if (found_root == extent_root->fs_info->extent_root ||
3497 found_root == extent_root->fs_info->chunk_root ||
3498 found_root == extent_root->fs_info->dev_root) {
3499 needs_lock = 1;
3500 mutex_lock(&extent_root->fs_info->alloc_mutex);
3501 }
3502
Chris Masonedbd8d42007-12-21 16:27:24 -05003503 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05003504 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05003505 ret = btrfs_search_slot(trans, found_root, &found_key, path,
3506 0, 1);
3507 path->lowest_level = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003508 btrfs_release_path(found_root, path);
Chris Mason7d9eb122008-07-08 14:19:17 -04003509
Chris Mason0ef3e662008-05-24 14:04:53 -04003510 if (found_root == found_root->fs_info->extent_root)
3511 btrfs_extent_post_op(trans, found_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003512 if (needs_lock)
3513 mutex_unlock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003514
Chris Mason7d9eb122008-07-08 14:19:17 -04003515 btrfs_end_transaction(trans, found_root);
3516
3517 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003518out:
Chris Mason7d9eb122008-07-08 14:19:17 -04003519 mutex_lock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003520 return 0;
3521}
3522
Chris Masona061fc82008-05-07 11:43:44 -04003523static int noinline del_extent_zero(struct btrfs_root *extent_root,
3524 struct btrfs_path *path,
3525 struct btrfs_key *extent_key)
3526{
3527 int ret;
3528 struct btrfs_trans_handle *trans;
3529
3530 trans = btrfs_start_transaction(extent_root, 1);
3531 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
3532 if (ret > 0) {
3533 ret = -EIO;
3534 goto out;
3535 }
3536 if (ret < 0)
3537 goto out;
3538 ret = btrfs_del_item(trans, extent_root, path);
3539out:
3540 btrfs_end_transaction(trans, extent_root);
3541 return ret;
3542}
3543
Chris Mason98ed5172008-01-03 10:01:48 -05003544static int noinline relocate_one_extent(struct btrfs_root *extent_root,
3545 struct btrfs_path *path,
3546 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05003547{
3548 struct btrfs_key key;
3549 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05003550 struct extent_buffer *leaf;
Chris Mason0ef3e662008-05-24 14:04:53 -04003551 u64 last_file_objectid = 0;
3552 u64 last_file_root = 0;
3553 u64 last_file_offset = (u64)-1;
3554 u64 last_extent = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003555 u32 nritems;
3556 u32 item_size;
3557 int ret = 0;
3558
Chris Masona061fc82008-05-07 11:43:44 -04003559 if (extent_key->objectid == 0) {
3560 ret = del_extent_zero(extent_root, path, extent_key);
3561 goto out;
3562 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003563 key.objectid = extent_key->objectid;
3564 key.type = BTRFS_EXTENT_REF_KEY;
3565 key.offset = 0;
3566
3567 while(1) {
3568 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3569
Chris Masonedbd8d42007-12-21 16:27:24 -05003570 if (ret < 0)
3571 goto out;
3572
3573 ret = 0;
3574 leaf = path->nodes[0];
3575 nritems = btrfs_header_nritems(leaf);
Chris Masona061fc82008-05-07 11:43:44 -04003576 if (path->slots[0] == nritems) {
3577 ret = btrfs_next_leaf(extent_root, path);
3578 if (ret > 0) {
3579 ret = 0;
3580 goto out;
3581 }
3582 if (ret < 0)
3583 goto out;
Chris Masonbf4ef672008-05-08 13:26:18 -04003584 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04003585 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003586
3587 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masona061fc82008-05-07 11:43:44 -04003588 if (found_key.objectid != extent_key->objectid) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003589 break;
Chris Masona061fc82008-05-07 11:43:44 -04003590 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003591
Chris Masona061fc82008-05-07 11:43:44 -04003592 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003593 break;
Chris Masona061fc82008-05-07 11:43:44 -04003594 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003595
3596 key.offset = found_key.offset + 1;
3597 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3598
Chris Mason0ef3e662008-05-24 14:04:53 -04003599 ret = relocate_one_reference(extent_root, path, extent_key,
3600 &last_file_objectid,
3601 &last_file_offset,
3602 &last_file_root, last_extent);
Chris Masonedbd8d42007-12-21 16:27:24 -05003603 if (ret)
3604 goto out;
Chris Mason0ef3e662008-05-24 14:04:53 -04003605 last_extent = extent_key->objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05003606 }
3607 ret = 0;
3608out:
3609 btrfs_release_path(extent_root, path);
3610 return ret;
3611}
3612
Chris Masonec44a352008-04-28 15:29:52 -04003613static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
3614{
3615 u64 num_devices;
3616 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
3617 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
3618
Chris Masona061fc82008-05-07 11:43:44 -04003619 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04003620 if (num_devices == 1) {
3621 stripped |= BTRFS_BLOCK_GROUP_DUP;
3622 stripped = flags & ~stripped;
3623
3624 /* turn raid0 into single device chunks */
3625 if (flags & BTRFS_BLOCK_GROUP_RAID0)
3626 return stripped;
3627
3628 /* turn mirroring into duplication */
3629 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
3630 BTRFS_BLOCK_GROUP_RAID10))
3631 return stripped | BTRFS_BLOCK_GROUP_DUP;
3632 return flags;
3633 } else {
3634 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04003635 if (flags & stripped)
3636 return flags;
3637
3638 stripped |= BTRFS_BLOCK_GROUP_DUP;
3639 stripped = flags & ~stripped;
3640
3641 /* switch duplicated blocks with raid1 */
3642 if (flags & BTRFS_BLOCK_GROUP_DUP)
3643 return stripped | BTRFS_BLOCK_GROUP_RAID1;
3644
3645 /* turn single device chunks into raid0 */
3646 return stripped | BTRFS_BLOCK_GROUP_RAID0;
3647 }
3648 return flags;
3649}
3650
Chris Mason0ef3e662008-05-24 14:04:53 -04003651int __alloc_chunk_for_shrink(struct btrfs_root *root,
3652 struct btrfs_block_group_cache *shrink_block_group,
3653 int force)
3654{
3655 struct btrfs_trans_handle *trans;
3656 u64 new_alloc_flags;
3657 u64 calc;
3658
Chris Masonc286ac42008-07-22 23:06:41 -04003659 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04003660 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04003661 spin_unlock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04003662 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003663
Chris Mason0ef3e662008-05-24 14:04:53 -04003664 trans = btrfs_start_transaction(root, 1);
Chris Mason7d9eb122008-07-08 14:19:17 -04003665 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003666 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04003667
Chris Mason0ef3e662008-05-24 14:04:53 -04003668 new_alloc_flags = update_block_group_flags(root,
3669 shrink_block_group->flags);
3670 if (new_alloc_flags != shrink_block_group->flags) {
3671 calc =
3672 btrfs_block_group_used(&shrink_block_group->item);
3673 } else {
3674 calc = shrink_block_group->key.offset;
3675 }
Chris Masonc286ac42008-07-22 23:06:41 -04003676 spin_unlock(&shrink_block_group->lock);
3677
Chris Mason0ef3e662008-05-24 14:04:53 -04003678 do_chunk_alloc(trans, root->fs_info->extent_root,
3679 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04003680
3681 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0ef3e662008-05-24 14:04:53 -04003682 btrfs_end_transaction(trans, root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003683 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003684 } else
3685 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04003686 return 0;
3687}
3688
Chris Mason8f18cf12008-04-25 16:53:30 -04003689int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05003690{
3691 struct btrfs_trans_handle *trans;
3692 struct btrfs_root *tree_root = root->fs_info->tree_root;
3693 struct btrfs_path *path;
3694 u64 cur_byte;
3695 u64 total_found;
Chris Mason8f18cf12008-04-25 16:53:30 -04003696 u64 shrink_last_byte;
3697 struct btrfs_block_group_cache *shrink_block_group;
Chris Masonedbd8d42007-12-21 16:27:24 -05003698 struct btrfs_key key;
Yan73e48b22008-01-03 14:14:39 -05003699 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05003700 struct extent_buffer *leaf;
3701 u32 nritems;
3702 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04003703 int progress;
Chris Masonedbd8d42007-12-21 16:27:24 -05003704
Chris Mason925baed2008-06-25 16:01:30 -04003705 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04003706 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
3707 shrink_start);
3708 BUG_ON(!shrink_block_group);
3709
Chris Mason0ef3e662008-05-24 14:04:53 -04003710 shrink_last_byte = shrink_block_group->key.objectid +
3711 shrink_block_group->key.offset;
Chris Mason8f18cf12008-04-25 16:53:30 -04003712
3713 shrink_block_group->space_info->total_bytes -=
3714 shrink_block_group->key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05003715 path = btrfs_alloc_path();
3716 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05003717 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05003718
Chris Mason323da792008-05-09 11:46:48 -04003719 printk("btrfs relocating block group %llu flags %llu\n",
3720 (unsigned long long)shrink_start,
3721 (unsigned long long)shrink_block_group->flags);
3722
Chris Mason0ef3e662008-05-24 14:04:53 -04003723 __alloc_chunk_for_shrink(root, shrink_block_group, 1);
Chris Mason323da792008-05-09 11:46:48 -04003724
Chris Mason0ef3e662008-05-24 14:04:53 -04003725again:
3726
Chris Mason8f18cf12008-04-25 16:53:30 -04003727 shrink_block_group->ro = 1;
3728
Chris Masonedbd8d42007-12-21 16:27:24 -05003729 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04003730 progress = 0;
Chris Mason8f18cf12008-04-25 16:53:30 -04003731 key.objectid = shrink_start;
Chris Masonedbd8d42007-12-21 16:27:24 -05003732 key.offset = 0;
3733 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05003734 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05003735
Chris Masonea8c2812008-08-04 23:17:27 -04003736 mutex_unlock(&root->fs_info->alloc_mutex);
3737
3738 btrfs_start_delalloc_inodes(root);
Yan Zheng7ea394f2008-08-05 13:05:02 -04003739 btrfs_wait_ordered_extents(tree_root, 0);
Chris Masonea8c2812008-08-04 23:17:27 -04003740
3741 mutex_lock(&root->fs_info->alloc_mutex);
3742
Yan73e48b22008-01-03 14:14:39 -05003743 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3744 if (ret < 0)
3745 goto out;
3746
Chris Mason0b86a832008-03-24 15:01:56 -04003747 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yan73e48b22008-01-03 14:14:39 -05003748 if (ret < 0)
3749 goto out;
Chris Mason8f18cf12008-04-25 16:53:30 -04003750
Yan73e48b22008-01-03 14:14:39 -05003751 if (ret == 0) {
3752 leaf = path->nodes[0];
3753 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003754 if (found_key.objectid + found_key.offset > shrink_start &&
3755 found_key.objectid < shrink_last_byte) {
Yan73e48b22008-01-03 14:14:39 -05003756 cur_byte = found_key.objectid;
3757 key.objectid = cur_byte;
3758 }
3759 }
3760 btrfs_release_path(root, path);
3761
3762 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003763 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3764 if (ret < 0)
3765 goto out;
Yan73e48b22008-01-03 14:14:39 -05003766
Chris Mason7d9eb122008-07-08 14:19:17 -04003767next:
Chris Masonedbd8d42007-12-21 16:27:24 -05003768 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05003769 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05003770 if (path->slots[0] >= nritems) {
3771 ret = btrfs_next_leaf(root, path);
3772 if (ret < 0)
3773 goto out;
3774 if (ret == 1) {
3775 ret = 0;
3776 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05003777 }
Yan73e48b22008-01-03 14:14:39 -05003778 leaf = path->nodes[0];
3779 nritems = btrfs_header_nritems(leaf);
3780 }
3781
3782 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05003783
Chris Mason8f18cf12008-04-25 16:53:30 -04003784 if (found_key.objectid >= shrink_last_byte)
3785 break;
3786
Chris Mason725c8462008-01-04 16:47:16 -05003787 if (progress && need_resched()) {
3788 memcpy(&key, &found_key, sizeof(key));
Chris Mason725c8462008-01-04 16:47:16 -05003789 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05003790 btrfs_release_path(root, path);
3791 btrfs_search_slot(NULL, root, &key, path, 0, 0);
3792 progress = 0;
3793 goto next;
3794 }
3795 progress = 1;
3796
Yan73e48b22008-01-03 14:14:39 -05003797 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
3798 found_key.objectid + found_key.offset <= cur_byte) {
Chris Mason0ef3e662008-05-24 14:04:53 -04003799 memcpy(&key, &found_key, sizeof(key));
3800 key.offset++;
Yan73e48b22008-01-03 14:14:39 -05003801 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003802 goto next;
3803 }
Yan73e48b22008-01-03 14:14:39 -05003804
Chris Masonedbd8d42007-12-21 16:27:24 -05003805 total_found++;
3806 cur_byte = found_key.objectid + found_key.offset;
3807 key.objectid = cur_byte;
3808 btrfs_release_path(root, path);
3809 ret = relocate_one_extent(root, path, &found_key);
Chris Mason0ef3e662008-05-24 14:04:53 -04003810 __alloc_chunk_for_shrink(root, shrink_block_group, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003811 }
3812
3813 btrfs_release_path(root, path);
3814
3815 if (total_found > 0) {
Chris Mason323da792008-05-09 11:46:48 -04003816 printk("btrfs relocate found %llu last extent was %llu\n",
3817 (unsigned long long)total_found,
3818 (unsigned long long)found_key.objectid);
Chris Mason7d9eb122008-07-08 14:19:17 -04003819 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003820 trans = btrfs_start_transaction(tree_root, 1);
3821 btrfs_commit_transaction(trans, tree_root);
3822
Chris Masonedbd8d42007-12-21 16:27:24 -05003823 btrfs_clean_old_snapshots(tree_root);
Chris Masonedbd8d42007-12-21 16:27:24 -05003824
Chris Masonea8c2812008-08-04 23:17:27 -04003825 btrfs_start_delalloc_inodes(root);
Yan Zheng7ea394f2008-08-05 13:05:02 -04003826 btrfs_wait_ordered_extents(tree_root, 0);
Chris Mason3eaa2882008-07-24 11:57:52 -04003827
Chris Masonedbd8d42007-12-21 16:27:24 -05003828 trans = btrfs_start_transaction(tree_root, 1);
3829 btrfs_commit_transaction(trans, tree_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003830 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003831 goto again;
3832 }
3833
Chris Mason8f18cf12008-04-25 16:53:30 -04003834 /*
3835 * we've freed all the extents, now remove the block
3836 * group item from the tree
3837 */
Chris Mason7d9eb122008-07-08 14:19:17 -04003838 mutex_unlock(&root->fs_info->alloc_mutex);
3839
Chris Masonedbd8d42007-12-21 16:27:24 -05003840 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04003841
Chris Mason7d9eb122008-07-08 14:19:17 -04003842 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04003843 memcpy(&key, &shrink_block_group->key, sizeof(key));
Chris Mason4313b392008-01-03 09:08:48 -05003844
Chris Mason8f18cf12008-04-25 16:53:30 -04003845 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3846 if (ret > 0)
3847 ret = -EIO;
Josef Bacik8e8a1e32008-07-24 12:17:14 -04003848 if (ret < 0) {
3849 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003850 goto out;
Josef Bacik8e8a1e32008-07-24 12:17:14 -04003851 }
Yan73e48b22008-01-03 14:14:39 -05003852
Josef Bacik0f9dd462008-09-23 13:14:11 -04003853 spin_lock(&root->fs_info->block_group_cache_lock);
3854 rb_erase(&shrink_block_group->cache_node,
3855 &root->fs_info->block_group_cache_tree);
3856 spin_unlock(&root->fs_info->block_group_cache_lock);
Yan73e48b22008-01-03 14:14:39 -05003857
Josef Bacik0f9dd462008-09-23 13:14:11 -04003858 ret = btrfs_remove_free_space(shrink_block_group, key.objectid,
3859 key.offset);
3860 if (ret) {
3861 btrfs_end_transaction(trans, root);
3862 goto out;
3863 }
Chris Masond7a029a2008-08-04 23:17:26 -04003864 /*
Chris Mason0ef3e662008-05-24 14:04:53 -04003865 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
3866 kfree(shrink_block_group);
Chris Masond7a029a2008-08-04 23:17:26 -04003867 */
Chris Mason0ef3e662008-05-24 14:04:53 -04003868
Chris Mason8f18cf12008-04-25 16:53:30 -04003869 btrfs_del_item(trans, root, path);
Chris Mason7d9eb122008-07-08 14:19:17 -04003870 btrfs_release_path(root, path);
3871 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003872 btrfs_commit_transaction(trans, root);
Chris Mason0ef3e662008-05-24 14:04:53 -04003873
Chris Mason7d9eb122008-07-08 14:19:17 -04003874 mutex_lock(&root->fs_info->alloc_mutex);
3875
Chris Mason0ef3e662008-05-24 14:04:53 -04003876 /* the code to unpin extents might set a few bits in the free
3877 * space cache for this range again
3878 */
Josef Bacik0f9dd462008-09-23 13:14:11 -04003879 /* XXX? */
3880 ret = btrfs_remove_free_space(shrink_block_group, key.objectid,
3881 key.offset);
Chris Masonedbd8d42007-12-21 16:27:24 -05003882out:
3883 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003884 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003885 return ret;
3886}
3887
Chris Mason0b86a832008-03-24 15:01:56 -04003888int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3889 struct btrfs_key *key)
3890{
Chris Mason925baed2008-06-25 16:01:30 -04003891 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003892 struct btrfs_key found_key;
3893 struct extent_buffer *leaf;
3894 int slot;
3895
3896 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3897 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003898 goto out;
3899
Chris Mason0b86a832008-03-24 15:01:56 -04003900 while(1) {
3901 slot = path->slots[0];
3902 leaf = path->nodes[0];
3903 if (slot >= btrfs_header_nritems(leaf)) {
3904 ret = btrfs_next_leaf(root, path);
3905 if (ret == 0)
3906 continue;
3907 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003908 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04003909 break;
3910 }
3911 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3912
3913 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04003914 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3915 ret = 0;
3916 goto out;
3917 }
Chris Mason0b86a832008-03-24 15:01:56 -04003918 path->slots[0]++;
3919 }
3920 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04003921out:
Chris Mason0b86a832008-03-24 15:01:56 -04003922 return ret;
3923}
3924
Chris Mason9078a3e2007-04-26 16:46:15 -04003925int btrfs_read_block_groups(struct btrfs_root *root)
3926{
3927 struct btrfs_path *path;
3928 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003929 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04003930 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003931 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04003932 struct btrfs_key key;
3933 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04003934 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04003935
Chris Masonbe744172007-05-06 10:15:01 -04003936 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04003937 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003938 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003939 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04003940 path = btrfs_alloc_path();
3941 if (!path)
3942 return -ENOMEM;
3943
Chris Mason925baed2008-06-25 16:01:30 -04003944 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04003945 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003946 ret = find_first_block_group(root, path, &key);
3947 if (ret > 0) {
3948 ret = 0;
3949 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04003950 }
Chris Mason0b86a832008-03-24 15:01:56 -04003951 if (ret != 0)
3952 goto error;
3953
Chris Mason5f39d392007-10-15 16:14:19 -04003954 leaf = path->nodes[0];
3955 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003956 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04003957 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04003958 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04003959 break;
3960 }
Chris Mason3e1ad542007-05-07 20:03:49 -04003961
Chris Masonc286ac42008-07-22 23:06:41 -04003962 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003963 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04003964 read_extent_buffer(leaf, &cache->item,
3965 btrfs_item_ptr_offset(leaf, path->slots[0]),
3966 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04003967 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04003968
Chris Mason9078a3e2007-04-26 16:46:15 -04003969 key.objectid = found_key.objectid + found_key.offset;
3970 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04003971 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04003972
Chris Mason6324fbf2008-03-24 15:01:59 -04003973 ret = update_space_info(info, cache->flags, found_key.offset,
3974 btrfs_block_group_used(&cache->item),
3975 &space_info);
3976 BUG_ON(ret);
3977 cache->space_info = space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003978 spin_lock(&space_info->lock);
3979 list_add(&cache->list, &space_info->block_groups);
3980 spin_unlock(&space_info->lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04003981
Josef Bacik0f9dd462008-09-23 13:14:11 -04003982 ret = btrfs_add_block_group_cache(root->fs_info, cache);
3983 BUG_ON(ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04003984 }
Chris Mason0b86a832008-03-24 15:01:56 -04003985 ret = 0;
3986error:
Chris Mason9078a3e2007-04-26 16:46:15 -04003987 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003988 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04003989 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003990}
Chris Mason6324fbf2008-03-24 15:01:59 -04003991
3992int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3993 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04003994 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04003995 u64 size)
3996{
3997 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003998 struct btrfs_root *extent_root;
3999 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04004000
Chris Mason7d9eb122008-07-08 14:19:17 -04004001 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason6324fbf2008-03-24 15:01:59 -04004002 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04004003
Chris Masone02119d2008-09-05 16:13:11 -04004004 root->fs_info->last_trans_new_blockgroup = trans->transid;
4005
Chris Mason8f18cf12008-04-25 16:53:30 -04004006 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004007 if (!cache)
4008 return -ENOMEM;
4009
Chris Masone17cade2008-04-15 15:41:47 -04004010 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04004011 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04004012 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004013 INIT_LIST_HEAD(&cache->list);
Chris Mason6324fbf2008-03-24 15:01:59 -04004014 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04004015
Chris Mason6324fbf2008-03-24 15:01:59 -04004016 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04004017 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
4018 cache->flags = type;
4019 btrfs_set_block_group_flags(&cache->item, type);
4020
4021 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
4022 &cache->space_info);
4023 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004024 spin_lock(&cache->space_info->lock);
4025 list_add(&cache->list, &cache->space_info->block_groups);
4026 spin_unlock(&cache->space_info->lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04004027
Josef Bacik0f9dd462008-09-23 13:14:11 -04004028 ret = btrfs_add_block_group_cache(root->fs_info, cache);
4029 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04004030
Chris Mason6324fbf2008-03-24 15:01:59 -04004031 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
4032 sizeof(cache->item));
4033 BUG_ON(ret);
4034
4035 finish_current_insert(trans, extent_root);
4036 ret = del_pending_extents(trans, extent_root);
4037 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04004038 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04004039
Chris Mason6324fbf2008-03-24 15:01:59 -04004040 return 0;
4041}