blob: ab36769c356cf0cff25bb43f2827416c3cfab7a8 [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
Zheng Yan31840ae2008-09-23 13:14:14 -0400528 * - number of references holding by parent node (alway 1 for tree blocks)
529 *
530 * Btree leaf may hold multiple references to a file extent. In most cases,
531 * these references are from same file and the corresponding offsets inside
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400532 * the file are close together.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500533 *
534 * When a file extent is allocated the fields are filled in:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400535 * (root_key.objectid, trans->transid, inode objectid, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500536 *
537 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400538 * in the leaf. It looks similar to the create case, but trans->transid will
539 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500540 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400541 * (root_key.objectid, trans->transid, inode objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400542 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500543 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400544 * When a file extent is removed either during snapshot deletion or
545 * file truncation, we find the corresponding back reference and check
546 * the following fields:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500547 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400548 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
549 * inode objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500550 *
551 * Btree extents can be referenced by:
552 *
553 * - Different subvolumes
554 * - Different generations of the same subvolume
555 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500556 * When a tree block is created, back references are inserted:
557 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400558 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500559 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400560 * When a tree block is cow'd, new back references are added for all the
561 * blocks it points to. If the tree block isn't in reference counted root,
562 * the old back references are removed. These new back references are of
563 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500564 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400565 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500566 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400567 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500568 *
569 * if backref was for a tree root:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400570 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500571 * else
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400572 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500573 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400574 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500575 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400576 * The key objectid corresponds to the first byte in the extent, the key
577 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
578 * byte of parent extent. If a extent is tree root, the key offset is set
579 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500580 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400581
582static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
583 struct btrfs_root *root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400584 struct btrfs_path *path,
585 u64 bytenr, u64 parent,
586 u64 ref_root, u64 ref_generation,
587 u64 owner_objectid, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500588{
Chris Mason74493f72007-12-11 09:25:06 -0500589 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400590 struct btrfs_extent_ref *ref;
591 struct extent_buffer *leaf;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400592 u64 ref_objectid;
Chris Mason74493f72007-12-11 09:25:06 -0500593 int ret;
594
Chris Mason74493f72007-12-11 09:25:06 -0500595 key.objectid = bytenr;
596 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400597 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500598
Zheng Yan31840ae2008-09-23 13:14:14 -0400599 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
600 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500601 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400602 if (ret > 0) {
603 ret = -ENOENT;
604 goto out;
605 }
606
607 leaf = path->nodes[0];
608 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400609 ref_objectid = btrfs_ref_objectid(leaf, ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400610 if (btrfs_ref_root(leaf, ref) != ref_root ||
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400611 btrfs_ref_generation(leaf, ref) != ref_generation ||
612 (ref_objectid != owner_objectid &&
613 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400614 ret = -EIO;
615 WARN_ON(1);
616 goto out;
617 }
618 ret = 0;
619out:
620 return ret;
621}
622
623static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
624 struct btrfs_root *root,
625 struct btrfs_path *path,
626 u64 bytenr, u64 parent,
627 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400628 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400629{
630 struct btrfs_key key;
631 struct extent_buffer *leaf;
632 struct btrfs_extent_ref *ref;
633 u32 num_refs;
634 int ret;
635
636 key.objectid = bytenr;
637 key.type = BTRFS_EXTENT_REF_KEY;
638 key.offset = parent;
639
640 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
641 if (ret == 0) {
642 leaf = path->nodes[0];
643 ref = btrfs_item_ptr(leaf, path->slots[0],
644 struct btrfs_extent_ref);
645 btrfs_set_ref_root(leaf, ref, ref_root);
646 btrfs_set_ref_generation(leaf, ref, ref_generation);
647 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400648 btrfs_set_ref_num_refs(leaf, ref, 1);
649 } else if (ret == -EEXIST) {
650 u64 existing_owner;
651 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
652 leaf = path->nodes[0];
653 ref = btrfs_item_ptr(leaf, path->slots[0],
654 struct btrfs_extent_ref);
655 if (btrfs_ref_root(leaf, ref) != ref_root ||
656 btrfs_ref_generation(leaf, ref) != ref_generation) {
657 ret = -EIO;
658 WARN_ON(1);
659 goto out;
660 }
661
662 num_refs = btrfs_ref_num_refs(leaf, ref);
663 BUG_ON(num_refs == 0);
664 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
665
666 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400667 if (existing_owner != owner_objectid &&
668 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400669 btrfs_set_ref_objectid(leaf, ref,
670 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400671 }
672 ret = 0;
673 } else {
674 goto out;
675 }
Chris Mason7bb86312007-12-11 09:25:06 -0500676 btrfs_mark_buffer_dirty(path->nodes[0]);
677out:
678 btrfs_release_path(root, path);
679 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500680}
681
Zheng Yan31840ae2008-09-23 13:14:14 -0400682static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
683 struct btrfs_root *root,
684 struct btrfs_path *path)
685{
686 struct extent_buffer *leaf;
687 struct btrfs_extent_ref *ref;
688 u32 num_refs;
689 int ret = 0;
690
691 leaf = path->nodes[0];
692 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
693 num_refs = btrfs_ref_num_refs(leaf, ref);
694 BUG_ON(num_refs == 0);
695 num_refs -= 1;
696 if (num_refs == 0) {
697 ret = btrfs_del_item(trans, root, path);
698 } else {
699 btrfs_set_ref_num_refs(leaf, ref, num_refs);
700 btrfs_mark_buffer_dirty(leaf);
701 }
702 btrfs_release_path(root, path);
703 return ret;
704}
705
706static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
707 struct btrfs_root *root, u64 bytenr,
708 u64 orig_parent, u64 parent,
709 u64 orig_root, u64 ref_root,
710 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400711 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400712{
713 int ret;
714 struct btrfs_root *extent_root = root->fs_info->extent_root;
715 struct btrfs_path *path;
716
717 if (root == root->fs_info->extent_root) {
718 struct pending_extent_op *extent_op;
719 u64 num_bytes;
720
721 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
722 num_bytes = btrfs_level_size(root, (int)owner_objectid);
723 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
724 bytenr + num_bytes - 1, EXTENT_LOCKED, 0)) {
725 u64 priv;
726 ret = get_state_private(&root->fs_info->extent_ins,
727 bytenr, &priv);
728 BUG_ON(ret);
729 extent_op = (struct pending_extent_op *)
730 (unsigned long)priv;
731 BUG_ON(extent_op->parent != orig_parent);
732 BUG_ON(extent_op->generation != orig_generation);
733 extent_op->parent = parent;
734 extent_op->generation = ref_generation;
735 } else {
736 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
737 BUG_ON(!extent_op);
738
739 extent_op->type = PENDING_BACKREF_UPDATE;
740 extent_op->bytenr = bytenr;
741 extent_op->num_bytes = num_bytes;
742 extent_op->parent = parent;
743 extent_op->orig_parent = orig_parent;
744 extent_op->generation = ref_generation;
745 extent_op->orig_generation = orig_generation;
746 extent_op->level = (int)owner_objectid;
747
748 set_extent_bits(&root->fs_info->extent_ins,
749 bytenr, bytenr + num_bytes - 1,
750 EXTENT_LOCKED, GFP_NOFS);
751 set_state_private(&root->fs_info->extent_ins,
752 bytenr, (unsigned long)extent_op);
753 }
754 return 0;
755 }
756
757 path = btrfs_alloc_path();
758 if (!path)
759 return -ENOMEM;
760 ret = lookup_extent_backref(trans, extent_root, path,
761 bytenr, orig_parent, orig_root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400762 orig_generation, owner_objectid, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400763 if (ret)
764 goto out;
765 ret = remove_extent_backref(trans, extent_root, path);
766 if (ret)
767 goto out;
768 ret = insert_extent_backref(trans, extent_root, path, bytenr,
769 parent, ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400770 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400771 BUG_ON(ret);
772 finish_current_insert(trans, extent_root);
773 del_pending_extents(trans, extent_root);
774out:
775 btrfs_free_path(path);
776 return ret;
777}
778
779int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
780 struct btrfs_root *root, u64 bytenr,
781 u64 orig_parent, u64 parent,
782 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400783 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400784{
785 int ret;
786 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
787 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
788 return 0;
789 maybe_lock_mutex(root);
790 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
791 parent, ref_root, ref_root,
792 ref_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400793 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400794 maybe_unlock_mutex(root);
795 return ret;
796}
797
Chris Mason925baed2008-06-25 16:01:30 -0400798static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400799 struct btrfs_root *root, u64 bytenr,
800 u64 orig_parent, u64 parent,
801 u64 orig_root, u64 ref_root,
802 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400803 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -0500804{
Chris Mason5caf2a02007-04-02 11:20:42 -0400805 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500806 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400807 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400808 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400809 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400810 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500811
Chris Mason5caf2a02007-04-02 11:20:42 -0400812 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400813 if (!path)
814 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400815
Chris Mason3c12ac72008-04-21 12:01:38 -0400816 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400817 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400818 key.type = BTRFS_EXTENT_ITEM_KEY;
819 key.offset = (u64)-1;
820
Chris Mason5caf2a02007-04-02 11:20:42 -0400821 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400822 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400823 if (ret < 0)
824 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400825 BUG_ON(ret == 0 || path->slots[0] == 0);
826
827 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400828 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -0400829
830 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
831 BUG_ON(key.objectid != bytenr);
832 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
833
Chris Mason5caf2a02007-04-02 11:20:42 -0400834 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400835 refs = btrfs_extent_refs(l, item);
836 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400837 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500838
Chris Mason5caf2a02007-04-02 11:20:42 -0400839 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500840
Chris Mason3c12ac72008-04-21 12:01:38 -0400841 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -0400842 ret = insert_extent_backref(trans, root->fs_info->extent_root,
843 path, bytenr, parent,
844 ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400845 owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500846 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400847 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400848 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500849
850 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500851 return 0;
852}
853
Chris Mason925baed2008-06-25 16:01:30 -0400854int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400855 struct btrfs_root *root,
856 u64 bytenr, u64 num_bytes, u64 parent,
857 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400858 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -0400859{
860 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400861 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
862 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
863 return 0;
864 maybe_lock_mutex(root);
865 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
866 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400867 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400868 maybe_unlock_mutex(root);
Chris Mason925baed2008-06-25 16:01:30 -0400869 return ret;
870}
871
Chris Masone9d0b132007-08-10 14:06:19 -0400872int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
873 struct btrfs_root *root)
874{
875 finish_current_insert(trans, root->fs_info->extent_root);
876 del_pending_extents(trans, root->fs_info->extent_root);
877 return 0;
878}
879
Zheng Yan31840ae2008-09-23 13:14:14 -0400880int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
881 struct btrfs_root *root, u64 bytenr,
882 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500883{
Chris Mason5caf2a02007-04-02 11:20:42 -0400884 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500885 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400886 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400887 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400888 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400889
Chris Masondb945352007-10-15 16:15:53 -0400890 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400891 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400892 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400893 key.objectid = bytenr;
894 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400895 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400896 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400897 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400898 if (ret < 0)
899 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400900 if (ret != 0) {
901 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400902 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500903 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400904 }
905 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400906 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400907 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400908out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400909 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500910 return 0;
911}
912
Yan Zhengf321e492008-07-30 09:26:11 -0400913static int get_reference_status(struct btrfs_root *root, u64 bytenr,
914 u64 parent_gen, u64 ref_objectid,
915 u64 *min_generation, u32 *ref_count)
Chris Masonbe20aa92007-12-17 20:14:01 -0500916{
917 struct btrfs_root *extent_root = root->fs_info->extent_root;
918 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -0400919 struct extent_buffer *leaf;
920 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -0500921 struct btrfs_key key;
922 struct btrfs_key found_key;
Yan Zhengf321e492008-07-30 09:26:11 -0400923 u64 root_objectid = root->root_key.objectid;
924 u64 ref_generation;
925 u32 nritems;
926 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500927
Chris Masonbe20aa92007-12-17 20:14:01 -0500928 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400929 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -0400930 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -0500931
Yan Zhengf321e492008-07-30 09:26:11 -0400932 path = btrfs_alloc_path();
933 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonbe20aa92007-12-17 20:14:01 -0500934 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
935 if (ret < 0)
936 goto out;
937 BUG_ON(ret == 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400938 if (ret < 0 || path->slots[0] == 0)
939 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500940
Zheng Yan31840ae2008-09-23 13:14:14 -0400941 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -0400942 leaf = path->nodes[0];
943 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500944
945 if (found_key.objectid != bytenr ||
946 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
Yan Zhengf321e492008-07-30 09:26:11 -0400947 ret = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500948 goto out;
949 }
950
Yan Zhengf321e492008-07-30 09:26:11 -0400951 *ref_count = 0;
952 *min_generation = (u64)-1;
953
Chris Masonbe20aa92007-12-17 20:14:01 -0500954 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -0400955 leaf = path->nodes[0];
956 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500957 if (path->slots[0] >= nritems) {
958 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -0400959 if (ret < 0)
960 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500961 if (ret == 0)
962 continue;
963 break;
964 }
Yan Zhengf321e492008-07-30 09:26:11 -0400965 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500966 if (found_key.objectid != bytenr)
967 break;
Chris Masonbd098352008-01-03 13:23:19 -0500968
Chris Masonbe20aa92007-12-17 20:14:01 -0500969 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
970 path->slots[0]++;
971 continue;
972 }
973
Yan Zhengf321e492008-07-30 09:26:11 -0400974 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -0500975 struct btrfs_extent_ref);
Yan Zhengf321e492008-07-30 09:26:11 -0400976 ref_generation = btrfs_ref_generation(leaf, ref_item);
977 /*
Zheng Yan31840ae2008-09-23 13:14:14 -0400978 * For (parent_gen > 0 && parent_gen > ref_generation):
Yan Zhengf321e492008-07-30 09:26:11 -0400979 *
Yanbcc63ab2008-07-30 16:29:20 -0400980 * we reach here through the oldest root, therefore
981 * all other reference from same snapshot should have
Yan Zhengf321e492008-07-30 09:26:11 -0400982 * a larger generation.
983 */
984 if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
985 (parent_gen > 0 && parent_gen > ref_generation) ||
986 (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
987 ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400988 *ref_count = 2;
Yan Zhengf321e492008-07-30 09:26:11 -0400989 break;
990 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500991
Yan Zhengf321e492008-07-30 09:26:11 -0400992 *ref_count = 1;
993 if (*min_generation > ref_generation)
994 *min_generation = ref_generation;
995
Chris Masonbe20aa92007-12-17 20:14:01 -0500996 path->slots[0]++;
997 }
Yan Zhengf321e492008-07-30 09:26:11 -0400998 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500999out:
Chris Mason925baed2008-06-25 16:01:30 -04001000 mutex_unlock(&root->fs_info->alloc_mutex);
Yan Zhengf321e492008-07-30 09:26:11 -04001001 btrfs_free_path(path);
1002 return ret;
1003}
1004
Yan Zheng7ea394f2008-08-05 13:05:02 -04001005int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
1006 struct btrfs_root *root,
Yan Zhengf321e492008-07-30 09:26:11 -04001007 struct btrfs_key *key, u64 bytenr)
1008{
Yan Zhengf321e492008-07-30 09:26:11 -04001009 struct btrfs_root *old_root;
1010 struct btrfs_path *path = NULL;
1011 struct extent_buffer *eb;
1012 struct btrfs_file_extent_item *item;
1013 u64 ref_generation;
1014 u64 min_generation;
1015 u64 extent_start;
1016 u32 ref_count;
1017 int level;
1018 int ret;
1019
Yan Zheng7ea394f2008-08-05 13:05:02 -04001020 BUG_ON(trans == NULL);
Yan Zhengf321e492008-07-30 09:26:11 -04001021 BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY);
1022 ret = get_reference_status(root, bytenr, 0, key->objectid,
1023 &min_generation, &ref_count);
1024 if (ret)
1025 return ret;
1026
1027 if (ref_count != 1)
1028 return 1;
1029
Yan Zhengf321e492008-07-30 09:26:11 -04001030 old_root = root->dirty_root->root;
1031 ref_generation = old_root->root_key.offset;
1032
1033 /* all references are created in running transaction */
1034 if (min_generation > ref_generation) {
1035 ret = 0;
1036 goto out;
1037 }
1038
1039 path = btrfs_alloc_path();
1040 if (!path) {
1041 ret = -ENOMEM;
1042 goto out;
1043 }
1044
1045 path->skip_locking = 1;
1046 /* if no item found, the extent is referenced by other snapshot */
1047 ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0);
1048 if (ret)
1049 goto out;
1050
1051 eb = path->nodes[0];
1052 item = btrfs_item_ptr(eb, path->slots[0],
1053 struct btrfs_file_extent_item);
1054 if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG ||
1055 btrfs_file_extent_disk_bytenr(eb, item) != bytenr) {
1056 ret = 1;
1057 goto out;
1058 }
1059
1060 for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) {
1061 if (level >= 0) {
1062 eb = path->nodes[level];
1063 if (!eb)
1064 continue;
1065 extent_start = eb->start;
Yanbcc63ab2008-07-30 16:29:20 -04001066 } else
Yan Zhengf321e492008-07-30 09:26:11 -04001067 extent_start = bytenr;
1068
1069 ret = get_reference_status(root, extent_start, ref_generation,
1070 0, &min_generation, &ref_count);
1071 if (ret)
1072 goto out;
1073
1074 if (ref_count != 1) {
1075 ret = 1;
1076 goto out;
1077 }
1078 if (level >= 0)
1079 ref_generation = btrfs_header_generation(eb);
1080 }
1081 ret = 0;
1082out:
1083 if (path)
1084 btrfs_free_path(path);
Yan Zhengf321e492008-07-30 09:26:11 -04001085 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001086}
Chris Masonc5739bb2007-04-10 09:27:04 -04001087
Zheng Yan31840ae2008-09-23 13:14:14 -04001088int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1089 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001090{
Chris Mason5f39d392007-10-15 16:14:19 -04001091 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001092 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001093 u64 root_gen;
1094 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001095 int i;
Chris Masondb945352007-10-15 16:15:53 -04001096 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001097 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001098 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001099
Chris Mason3768f362007-03-13 16:47:54 -04001100 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001101 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001102
Zheng Yane4657682008-09-26 10:04:53 -04001103 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1104 shared = 0;
1105 root_gen = root->root_key.offset;
1106 } else {
1107 shared = 1;
1108 root_gen = trans->transid - 1;
1109 }
1110
Chris Masondb945352007-10-15 16:15:53 -04001111 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001112 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001113
Zheng Yan31840ae2008-09-23 13:14:14 -04001114 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001115 struct btrfs_leaf_ref *ref;
1116 struct btrfs_extent_info *info;
1117
Zheng Yan31840ae2008-09-23 13:14:14 -04001118 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001119 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001120 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001121 goto out;
1122 }
1123
Zheng Yane4657682008-09-26 10:04:53 -04001124 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001125 ref->bytenr = buf->start;
1126 ref->owner = btrfs_header_owner(buf);
1127 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001128 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001129 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001130
Zheng Yan31840ae2008-09-23 13:14:14 -04001131 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001132 u64 disk_bytenr;
1133 btrfs_item_key_to_cpu(buf, &key, i);
1134 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1135 continue;
1136 fi = btrfs_item_ptr(buf, i,
1137 struct btrfs_file_extent_item);
1138 if (btrfs_file_extent_type(buf, fi) ==
1139 BTRFS_FILE_EXTENT_INLINE)
1140 continue;
1141 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1142 if (disk_bytenr == 0)
1143 continue;
1144
1145 info->bytenr = disk_bytenr;
1146 info->num_bytes =
1147 btrfs_file_extent_disk_num_bytes(buf, fi);
1148 info->objectid = key.objectid;
1149 info->offset = key.offset;
1150 info++;
1151 }
1152
Zheng Yane4657682008-09-26 10:04:53 -04001153 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng31153d82008-07-28 15:32:19 -04001154 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001155 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001156 }
1157out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001158 return ret;
1159}
1160
1161int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1162 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1163 u32 *nr_extents)
1164{
1165 u64 bytenr;
1166 u64 ref_root;
1167 u64 orig_root;
1168 u64 ref_generation;
1169 u64 orig_generation;
1170 u32 nritems;
1171 u32 nr_file_extents = 0;
1172 struct btrfs_key key;
1173 struct btrfs_file_extent_item *fi;
1174 int i;
1175 int level;
1176 int ret = 0;
1177 int faili = 0;
1178 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001179 u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001180
1181 ref_root = btrfs_header_owner(buf);
1182 ref_generation = btrfs_header_generation(buf);
1183 orig_root = btrfs_header_owner(orig_buf);
1184 orig_generation = btrfs_header_generation(orig_buf);
1185
1186 nritems = btrfs_header_nritems(buf);
1187 level = btrfs_header_level(buf);
1188
1189 if (root->ref_cows) {
1190 process_func = __btrfs_inc_extent_ref;
1191 } else {
1192 if (level == 0 &&
1193 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1194 goto out;
1195 if (level != 0 &&
1196 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1197 goto out;
1198 process_func = __btrfs_update_extent_ref;
1199 }
1200
1201 for (i = 0; i < nritems; i++) {
1202 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001203 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001204 btrfs_item_key_to_cpu(buf, &key, i);
1205 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001206 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001207 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001208 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001209 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001210 BTRFS_FILE_EXTENT_INLINE)
1211 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001212 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1213 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001214 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001215
1216 nr_file_extents++;
1217
1218 maybe_lock_mutex(root);
1219 ret = process_func(trans, root, bytenr,
1220 orig_buf->start, buf->start,
1221 orig_root, ref_root,
1222 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001223 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001224 maybe_unlock_mutex(root);
1225
1226 if (ret) {
1227 faili = i;
1228 WARN_ON(1);
1229 goto fail;
1230 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001231 } else {
Chris Masondb945352007-10-15 16:15:53 -04001232 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001233 maybe_lock_mutex(root);
1234 ret = process_func(trans, root, bytenr,
1235 orig_buf->start, buf->start,
1236 orig_root, ref_root,
1237 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001238 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001239 maybe_unlock_mutex(root);
1240 if (ret) {
1241 faili = i;
1242 WARN_ON(1);
1243 goto fail;
1244 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001245 }
1246 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001247out:
1248 if (nr_extents) {
1249 if (level == 0)
1250 *nr_extents = nr_file_extents;
1251 else
1252 *nr_extents = nritems;
1253 }
1254 return 0;
1255fail:
1256 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001257 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001258}
1259
Zheng Yan31840ae2008-09-23 13:14:14 -04001260int btrfs_update_ref(struct btrfs_trans_handle *trans,
1261 struct btrfs_root *root, struct extent_buffer *orig_buf,
1262 struct extent_buffer *buf, int start_slot, int nr)
1263
1264{
1265 u64 bytenr;
1266 u64 ref_root;
1267 u64 orig_root;
1268 u64 ref_generation;
1269 u64 orig_generation;
1270 struct btrfs_key key;
1271 struct btrfs_file_extent_item *fi;
1272 int i;
1273 int ret;
1274 int slot;
1275 int level;
1276
1277 BUG_ON(start_slot < 0);
1278 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1279
1280 ref_root = btrfs_header_owner(buf);
1281 ref_generation = btrfs_header_generation(buf);
1282 orig_root = btrfs_header_owner(orig_buf);
1283 orig_generation = btrfs_header_generation(orig_buf);
1284 level = btrfs_header_level(buf);
1285
1286 if (!root->ref_cows) {
1287 if (level == 0 &&
1288 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1289 return 0;
1290 if (level != 0 &&
1291 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1292 return 0;
1293 }
1294
1295 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1296 cond_resched();
1297 if (level == 0) {
1298 btrfs_item_key_to_cpu(buf, &key, slot);
1299 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1300 continue;
1301 fi = btrfs_item_ptr(buf, slot,
1302 struct btrfs_file_extent_item);
1303 if (btrfs_file_extent_type(buf, fi) ==
1304 BTRFS_FILE_EXTENT_INLINE)
1305 continue;
1306 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1307 if (bytenr == 0)
1308 continue;
1309 maybe_lock_mutex(root);
1310 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1311 orig_buf->start, buf->start,
1312 orig_root, ref_root,
1313 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001314 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001315 maybe_unlock_mutex(root);
1316 if (ret)
1317 goto fail;
1318 } else {
1319 bytenr = btrfs_node_blockptr(buf, slot);
1320 maybe_lock_mutex(root);
1321 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1322 orig_buf->start, buf->start,
1323 orig_root, ref_root,
1324 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001325 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001326 maybe_unlock_mutex(root);
1327 if (ret)
1328 goto fail;
1329 }
1330 }
1331 return 0;
1332fail:
1333 WARN_ON(1);
1334 return -1;
1335}
1336
Chris Mason9078a3e2007-04-26 16:46:15 -04001337static int write_one_cache_group(struct btrfs_trans_handle *trans,
1338 struct btrfs_root *root,
1339 struct btrfs_path *path,
1340 struct btrfs_block_group_cache *cache)
1341{
1342 int ret;
1343 int pending_ret;
1344 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001345 unsigned long bi;
1346 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001347
Chris Mason9078a3e2007-04-26 16:46:15 -04001348 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001349 if (ret < 0)
1350 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001351 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001352
1353 leaf = path->nodes[0];
1354 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1355 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1356 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001357 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001358fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001359 finish_current_insert(trans, extent_root);
1360 pending_ret = del_pending_extents(trans, extent_root);
1361 if (ret)
1362 return ret;
1363 if (pending_ret)
1364 return pending_ret;
1365 return 0;
1366
1367}
1368
Chris Mason96b51792007-10-15 16:15:19 -04001369int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1370 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001371{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001372 struct btrfs_block_group_cache *cache, *entry;
1373 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001374 int err = 0;
1375 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001376 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001377 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001378
1379 path = btrfs_alloc_path();
1380 if (!path)
1381 return -ENOMEM;
1382
Chris Mason925baed2008-06-25 16:01:30 -04001383 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001384 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001385 cache = NULL;
1386 spin_lock(&root->fs_info->block_group_cache_lock);
1387 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1388 n; n = rb_next(n)) {
1389 entry = rb_entry(n, struct btrfs_block_group_cache,
1390 cache_node);
1391 if (entry->dirty) {
1392 cache = entry;
1393 break;
1394 }
1395 }
1396 spin_unlock(&root->fs_info->block_group_cache_lock);
1397
1398 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001399 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001400
Zheng Yane8569812008-09-26 10:05:48 -04001401 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001402 last += cache->key.offset;
1403
Chris Mason96b51792007-10-15 16:15:19 -04001404 err = write_one_cache_group(trans, root,
1405 path, cache);
1406 /*
1407 * if we fail to write the cache group, we want
1408 * to keep it marked dirty in hopes that a later
1409 * write will work
1410 */
1411 if (err) {
1412 werr = err;
1413 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001414 }
1415 }
1416 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04001417 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001418 return werr;
1419}
1420
Chris Mason593060d2008-03-25 16:50:33 -04001421static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1422 u64 total_bytes, u64 bytes_used,
1423 struct btrfs_space_info **space_info)
1424{
1425 struct btrfs_space_info *found;
1426
1427 found = __find_space_info(info, flags);
1428 if (found) {
1429 found->total_bytes += total_bytes;
1430 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001431 found->full = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001432 *space_info = found;
1433 return 0;
1434 }
1435 found = kmalloc(sizeof(*found), GFP_NOFS);
1436 if (!found)
1437 return -ENOMEM;
1438
1439 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001440 INIT_LIST_HEAD(&found->block_groups);
1441 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001442 found->flags = flags;
1443 found->total_bytes = total_bytes;
1444 found->bytes_used = bytes_used;
1445 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001446 found->bytes_reserved = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001447 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001448 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001449 *space_info = found;
1450 return 0;
1451}
1452
Chris Mason8790d502008-04-03 16:29:03 -04001453static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1454{
1455 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001456 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001457 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001458 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001459 if (extra_flags) {
1460 if (flags & BTRFS_BLOCK_GROUP_DATA)
1461 fs_info->avail_data_alloc_bits |= extra_flags;
1462 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1463 fs_info->avail_metadata_alloc_bits |= extra_flags;
1464 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1465 fs_info->avail_system_alloc_bits |= extra_flags;
1466 }
1467}
Chris Mason593060d2008-03-25 16:50:33 -04001468
Chris Masona061fc82008-05-07 11:43:44 -04001469static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001470{
Chris Masona061fc82008-05-07 11:43:44 -04001471 u64 num_devices = root->fs_info->fs_devices->num_devices;
1472
1473 if (num_devices == 1)
1474 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1475 if (num_devices < 4)
1476 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1477
Chris Masonec44a352008-04-28 15:29:52 -04001478 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1479 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001480 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001481 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001482 }
Chris Masonec44a352008-04-28 15:29:52 -04001483
1484 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001485 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001486 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001487 }
Chris Masonec44a352008-04-28 15:29:52 -04001488
1489 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1490 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1491 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1492 (flags & BTRFS_BLOCK_GROUP_DUP)))
1493 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1494 return flags;
1495}
1496
Chris Mason6324fbf2008-03-24 15:01:59 -04001497static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1498 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001499 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001500{
1501 struct btrfs_space_info *space_info;
1502 u64 thresh;
1503 u64 start;
1504 u64 num_bytes;
Josef Bacikcf749822008-10-01 19:11:18 -04001505 int ret = 0, waited = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001506
Chris Masona061fc82008-05-07 11:43:44 -04001507 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001508
Chris Mason6324fbf2008-03-24 15:01:59 -04001509 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001510 if (!space_info) {
1511 ret = update_space_info(extent_root->fs_info, flags,
1512 0, 0, &space_info);
1513 BUG_ON(ret);
1514 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001515 BUG_ON(!space_info);
1516
Chris Mason0ef3e662008-05-24 14:04:53 -04001517 if (space_info->force_alloc) {
1518 force = 1;
1519 space_info->force_alloc = 0;
1520 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001521 if (space_info->full)
Chris Mason925baed2008-06-25 16:01:30 -04001522 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001523
Chris Mason8790d502008-04-03 16:29:03 -04001524 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001525 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001526 (space_info->bytes_used + space_info->bytes_pinned +
1527 space_info->bytes_reserved + alloc_bytes) < thresh)
Chris Mason925baed2008-06-25 16:01:30 -04001528 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001529
Josef Bacikcf749822008-10-01 19:11:18 -04001530 while (!mutex_trylock(&extent_root->fs_info->chunk_mutex)) {
1531 if (!force)
1532 goto out;
1533 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1534 cond_resched();
1535 mutex_lock(&extent_root->fs_info->alloc_mutex);
1536 waited = 1;
1537 }
1538
1539 if (waited && space_info->full)
1540 goto out_unlock;
1541
Chris Mason6324fbf2008-03-24 15:01:59 -04001542 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1543 if (ret == -ENOSPC) {
1544printk("space info full %Lu\n", flags);
1545 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001546 goto out_unlock;
Chris Mason6324fbf2008-03-24 15:01:59 -04001547 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001548 BUG_ON(ret);
1549
1550 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001551 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001552 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001553
Chris Masona74a4b92008-06-25 16:01:31 -04001554out_unlock:
Chris Mason333db942008-06-25 16:01:30 -04001555 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001556out:
Josef Bacik0f9dd462008-09-23 13:14:11 -04001557 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001558}
1559
Chris Mason9078a3e2007-04-26 16:46:15 -04001560static int update_block_group(struct btrfs_trans_handle *trans,
1561 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001562 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001563 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001564{
1565 struct btrfs_block_group_cache *cache;
1566 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001567 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001568 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001569 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001570
Chris Mason7d9eb122008-07-08 14:19:17 -04001571 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason9078a3e2007-04-26 16:46:15 -04001572 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001573 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001574 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001575 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001576 }
Chris Masondb945352007-10-15 16:15:53 -04001577 byte_in_group = bytenr - cache->key.objectid;
1578 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001579
Chris Masonc286ac42008-07-22 23:06:41 -04001580 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001581 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001582 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001583 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001584 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001585 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001586 cache->space_info->bytes_used += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001587 btrfs_set_block_group_used(&cache->item, old_val);
1588 spin_unlock(&cache->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001589 } else {
Chris Masondb945352007-10-15 16:15:53 -04001590 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001591 cache->space_info->bytes_used -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001592 btrfs_set_block_group_used(&cache->item, old_val);
1593 spin_unlock(&cache->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001594 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001595 int ret;
1596 ret = btrfs_add_free_space(cache, bytenr,
1597 num_bytes);
1598 if (ret)
1599 return -1;
Chris Masone37c9e62007-05-09 20:13:14 -04001600 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001601 }
Chris Masondb945352007-10-15 16:15:53 -04001602 total -= num_bytes;
1603 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001604 }
1605 return 0;
1606}
Chris Mason6324fbf2008-03-24 15:01:59 -04001607
Chris Masona061fc82008-05-07 11:43:44 -04001608static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1609{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001610 struct btrfs_block_group_cache *cache;
1611
1612 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1613 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001614 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001615
1616 return cache->key.objectid;
Chris Masona061fc82008-05-07 11:43:44 -04001617}
1618
Chris Masone02119d2008-09-05 16:13:11 -04001619int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05001620 u64 bytenr, u64 num, int pin)
1621{
1622 u64 len;
1623 struct btrfs_block_group_cache *cache;
1624 struct btrfs_fs_info *fs_info = root->fs_info;
1625
Chris Mason7d9eb122008-07-08 14:19:17 -04001626 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Yan324ae4d2007-11-16 14:57:08 -05001627 if (pin) {
1628 set_extent_dirty(&fs_info->pinned_extents,
1629 bytenr, bytenr + num - 1, GFP_NOFS);
1630 } else {
1631 clear_extent_dirty(&fs_info->pinned_extents,
1632 bytenr, bytenr + num - 1, GFP_NOFS);
1633 }
1634 while (num > 0) {
1635 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04001636 BUG_ON(!cache);
1637 len = min(num, cache->key.offset -
1638 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05001639 if (pin) {
Zheng Yane8569812008-09-26 10:05:48 -04001640 spin_lock(&cache->lock);
1641 cache->pinned += len;
1642 cache->space_info->bytes_pinned += len;
1643 spin_unlock(&cache->lock);
Yan324ae4d2007-11-16 14:57:08 -05001644 fs_info->total_pinned += len;
1645 } else {
Zheng Yane8569812008-09-26 10:05:48 -04001646 spin_lock(&cache->lock);
1647 cache->pinned -= len;
1648 cache->space_info->bytes_pinned -= len;
1649 spin_unlock(&cache->lock);
Yan324ae4d2007-11-16 14:57:08 -05001650 fs_info->total_pinned -= len;
1651 }
1652 bytenr += len;
1653 num -= len;
1654 }
1655 return 0;
1656}
Chris Mason9078a3e2007-04-26 16:46:15 -04001657
Zheng Yane8569812008-09-26 10:05:48 -04001658static int update_reserved_extents(struct btrfs_root *root,
1659 u64 bytenr, u64 num, int reserve)
1660{
1661 u64 len;
1662 struct btrfs_block_group_cache *cache;
1663 struct btrfs_fs_info *fs_info = root->fs_info;
1664
1665 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1666 while (num > 0) {
1667 cache = btrfs_lookup_block_group(fs_info, bytenr);
1668 BUG_ON(!cache);
1669 len = min(num, cache->key.offset -
1670 (bytenr - cache->key.objectid));
1671 if (reserve) {
1672 spin_lock(&cache->lock);
1673 cache->reserved += len;
1674 cache->space_info->bytes_reserved += len;
1675 spin_unlock(&cache->lock);
1676 } else {
1677 spin_lock(&cache->lock);
1678 cache->reserved -= len;
1679 cache->space_info->bytes_reserved -= len;
1680 spin_unlock(&cache->lock);
1681 }
1682 bytenr += len;
1683 num -= len;
1684 }
1685 return 0;
1686}
1687
Chris Masond1310b22008-01-24 16:13:08 -05001688int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001689{
Chris Masonccd467d2007-06-28 15:57:36 -04001690 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001691 u64 start;
1692 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001693 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001694 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001695
1696 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001697 ret = find_first_extent_bit(pinned_extents, last,
1698 &start, &end, EXTENT_DIRTY);
1699 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001700 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001701 set_extent_dirty(copy, start, end, GFP_NOFS);
1702 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001703 }
1704 return 0;
1705}
1706
1707int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1708 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001709 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001710{
Chris Mason1a5bc162007-10-15 16:15:26 -04001711 u64 start;
1712 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001713 int ret;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001714 struct btrfs_block_group_cache *cache;
Chris Masona28ec192007-03-06 20:08:01 -05001715
Chris Mason925baed2008-06-25 16:01:30 -04001716 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001717 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001718 ret = find_first_extent_bit(unpin, 0, &start, &end,
1719 EXTENT_DIRTY);
1720 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001721 break;
Chris Masone02119d2008-09-05 16:13:11 -04001722 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001723 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001724 cache = btrfs_lookup_block_group(root->fs_info, start);
1725 if (cache->cached)
1726 btrfs_add_free_space(cache, start, end - start + 1);
Chris Masonc286ac42008-07-22 23:06:41 -04001727 if (need_resched()) {
1728 mutex_unlock(&root->fs_info->alloc_mutex);
1729 cond_resched();
1730 mutex_lock(&root->fs_info->alloc_mutex);
1731 }
Chris Masona28ec192007-03-06 20:08:01 -05001732 }
Chris Mason925baed2008-06-25 16:01:30 -04001733 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001734 return 0;
1735}
1736
Chris Mason98ed5172008-01-03 10:01:48 -05001737static int finish_current_insert(struct btrfs_trans_handle *trans,
1738 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001739{
Chris Mason7bb86312007-12-11 09:25:06 -05001740 u64 start;
1741 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04001742 u64 priv;
Chris Mason7bb86312007-12-11 09:25:06 -05001743 struct btrfs_fs_info *info = extent_root->fs_info;
1744 struct btrfs_path *path;
Zheng Yan31840ae2008-09-23 13:14:14 -04001745 struct btrfs_extent_ref *ref;
1746 struct pending_extent_op *extent_op;
1747 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -04001748 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001749 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -04001750 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001751
Chris Mason7d9eb122008-07-08 14:19:17 -04001752 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Chris Mason5f39d392007-10-15 16:14:19 -04001753 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001754 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001755
Chris Mason26b80032007-08-08 20:17:12 -04001756 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001757 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1758 &end, EXTENT_LOCKED);
1759 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001760 break;
1761
Zheng Yan31840ae2008-09-23 13:14:14 -04001762 ret = get_state_private(&info->extent_ins, start, &priv);
1763 BUG_ON(ret);
1764 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1765
1766 if (extent_op->type == PENDING_EXTENT_INSERT) {
1767 key.objectid = start;
1768 key.offset = end + 1 - start;
1769 key.type = BTRFS_EXTENT_ITEM_KEY;
1770 err = btrfs_insert_item(trans, extent_root, &key,
Chris Mason1a5bc162007-10-15 16:15:26 -04001771 &extent_item, sizeof(extent_item));
Zheng Yan31840ae2008-09-23 13:14:14 -04001772 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001773
Zheng Yan31840ae2008-09-23 13:14:14 -04001774 clear_extent_bits(&info->extent_ins, start, end,
1775 EXTENT_LOCKED, GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04001776
Zheng Yan31840ae2008-09-23 13:14:14 -04001777 err = insert_extent_backref(trans, extent_root, path,
1778 start, extent_op->parent,
1779 extent_root->root_key.objectid,
1780 extent_op->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001781 extent_op->level);
Zheng Yan31840ae2008-09-23 13:14:14 -04001782 BUG_ON(err);
1783 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1784 err = lookup_extent_backref(trans, extent_root, path,
1785 start, extent_op->orig_parent,
1786 extent_root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001787 extent_op->orig_generation,
1788 extent_op->level, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001789 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001790
Zheng Yan31840ae2008-09-23 13:14:14 -04001791 clear_extent_bits(&info->extent_ins, start, end,
1792 EXTENT_LOCKED, GFP_NOFS);
1793
1794 key.objectid = start;
1795 key.offset = extent_op->parent;
1796 key.type = BTRFS_EXTENT_REF_KEY;
1797 err = btrfs_set_item_key_safe(trans, extent_root, path,
1798 &key);
1799 BUG_ON(err);
1800 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1801 struct btrfs_extent_ref);
1802 btrfs_set_ref_generation(path->nodes[0], ref,
1803 extent_op->generation);
1804 btrfs_mark_buffer_dirty(path->nodes[0]);
1805 btrfs_release_path(extent_root, path);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001806 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001807 BUG_ON(1);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001808 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001809 kfree(extent_op);
1810
Chris Masonc286ac42008-07-22 23:06:41 -04001811 if (need_resched()) {
1812 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1813 cond_resched();
1814 mutex_lock(&extent_root->fs_info->alloc_mutex);
1815 }
Chris Mason037e6392007-03-07 11:50:24 -05001816 }
Chris Mason7bb86312007-12-11 09:25:06 -05001817 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001818 return 0;
1819}
1820
Zheng Yan31840ae2008-09-23 13:14:14 -04001821static int pin_down_bytes(struct btrfs_trans_handle *trans,
1822 struct btrfs_root *root,
1823 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04001824{
Chris Mason1a5bc162007-10-15 16:15:26 -04001825 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001826 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001827
Chris Mason7d9eb122008-07-08 14:19:17 -04001828 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Zheng Yan31840ae2008-09-23 13:14:14 -04001829 if (is_data)
1830 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001831
Zheng Yan31840ae2008-09-23 13:14:14 -04001832 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1833 if (!buf)
1834 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001835
Zheng Yan31840ae2008-09-23 13:14:14 -04001836 /* we can reuse a block if it hasn't been written
1837 * and it is from this transaction. We can't
1838 * reuse anything from the tree log root because
1839 * it has tiny sub-transactions.
1840 */
1841 if (btrfs_buffer_uptodate(buf, 0) &&
1842 btrfs_try_tree_lock(buf)) {
1843 u64 header_owner = btrfs_header_owner(buf);
1844 u64 header_transid = btrfs_header_generation(buf);
1845 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04001846 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04001847 header_transid == trans->transid &&
1848 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1849 clean_tree_block(NULL, root, buf);
1850 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001851 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001852 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04001853 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001854 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001855 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001856 free_extent_buffer(buf);
1857pinit:
1858 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
1859
Chris Masonbe744172007-05-06 10:15:01 -04001860 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001861 return 0;
1862}
1863
Chris Masona28ec192007-03-06 20:08:01 -05001864/*
1865 * remove an extent from the root, returns 0 on success
1866 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001867static int __free_extent(struct btrfs_trans_handle *trans,
1868 struct btrfs_root *root,
1869 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05001870 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001871 u64 owner_objectid, int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001872{
Chris Mason5caf2a02007-04-02 11:20:42 -04001873 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001874 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001875 struct btrfs_fs_info *info = root->fs_info;
1876 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001877 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001878 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001879 int extent_slot = 0;
1880 int found_extent = 0;
1881 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001882 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001883 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001884
Chris Mason7d9eb122008-07-08 14:19:17 -04001885 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Masondb945352007-10-15 16:15:53 -04001886 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001887 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001888 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001889 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001890 if (!path)
1891 return -ENOMEM;
1892
Chris Mason3c12ac72008-04-21 12:01:38 -04001893 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001894 ret = lookup_extent_backref(trans, extent_root, path,
1895 bytenr, parent, root_objectid,
1896 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001897 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001898 struct btrfs_key found_key;
1899 extent_slot = path->slots[0];
1900 while(extent_slot > 0) {
1901 extent_slot--;
1902 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1903 extent_slot);
1904 if (found_key.objectid != bytenr)
1905 break;
1906 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1907 found_key.offset == num_bytes) {
1908 found_extent = 1;
1909 break;
1910 }
1911 if (path->slots[0] - extent_slot > 5)
1912 break;
1913 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001914 if (!found_extent) {
1915 ret = remove_extent_backref(trans, extent_root, path);
1916 BUG_ON(ret);
1917 btrfs_release_path(extent_root, path);
1918 ret = btrfs_search_slot(trans, extent_root,
1919 &key, path, -1, 1);
1920 BUG_ON(ret);
1921 extent_slot = path->slots[0];
1922 }
Chris Mason7bb86312007-12-11 09:25:06 -05001923 } else {
1924 btrfs_print_leaf(extent_root, path->nodes[0]);
1925 WARN_ON(1);
1926 printk("Unable to find ref byte nr %Lu root %Lu "
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001927 "gen %Lu owner %Lu\n", bytenr,
1928 root_objectid, ref_generation, owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05001929 }
Chris Mason5f39d392007-10-15 16:14:19 -04001930
1931 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001932 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001933 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001934 refs = btrfs_extent_refs(leaf, ei);
1935 BUG_ON(refs == 0);
1936 refs -= 1;
1937 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001938
Chris Mason5f39d392007-10-15 16:14:19 -04001939 btrfs_mark_buffer_dirty(leaf);
1940
Chris Mason952fcca2008-02-18 16:33:44 -05001941 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001942 struct btrfs_extent_ref *ref;
1943 ref = btrfs_item_ptr(leaf, path->slots[0],
1944 struct btrfs_extent_ref);
1945 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001946 /* if the back ref and the extent are next to each other
1947 * they get deleted below in one shot
1948 */
1949 path->slots[0] = extent_slot;
1950 num_to_del = 2;
1951 } else if (found_extent) {
1952 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001953 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05001954 BUG_ON(ret);
1955 /* if refs are 0, we need to setup the path for deletion */
1956 if (refs == 0) {
1957 btrfs_release_path(extent_root, path);
1958 ret = btrfs_search_slot(trans, extent_root, &key, path,
1959 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001960 BUG_ON(ret);
1961 }
1962 }
1963
Chris Masoncf27e1e2007-03-13 09:49:06 -04001964 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001965 u64 super_used;
1966 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01001967#ifdef BIO_RW_DISCARD
1968 u64 map_length = num_bytes;
1969 struct btrfs_multi_bio *multi = NULL;
1970#endif
Chris Mason78fae272007-03-25 11:35:08 -04001971
1972 if (pin) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001973 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
1974 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Yanc5492282007-11-06 10:25:25 -05001975 if (ret > 0)
1976 mark_free = 1;
1977 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001978 }
1979
Josef Bacik58176a92007-08-29 15:47:34 -04001980 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04001981 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04001982 super_used = btrfs_super_bytes_used(&info->super_copy);
1983 btrfs_set_super_bytes_used(&info->super_copy,
1984 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04001985 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04001986
1987 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001988 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001989 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001990 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001991 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1992 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04001993 BUG_ON(ret);
Chris Masondb945352007-10-15 16:15:53 -04001994 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001995 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001996 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01001997
1998#ifdef BIO_RW_DISCARD
1999 /* Tell the block device(s) that the sectors can be discarded */
2000 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2001 bytenr, &map_length, &multi, 0);
2002 if (!ret) {
2003 struct btrfs_bio_stripe *stripe = multi->stripes;
2004 int i;
2005
2006 if (map_length > num_bytes)
2007 map_length = num_bytes;
2008
2009 for (i = 0; i < multi->num_stripes; i++, stripe++) {
2010 blkdev_issue_discard(stripe->dev->bdev,
2011 stripe->physical >> 9,
2012 map_length >> 9);
2013 }
2014 kfree(multi);
2015 }
2016#endif
Chris Masona28ec192007-03-06 20:08:01 -05002017 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002018 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002019 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05002020 return ret;
2021}
2022
2023/*
Chris Masonfec577f2007-02-26 10:40:21 -05002024 * find all the blocks marked as pending in the radix tree and remove
2025 * them from the extent map
2026 */
Chris Masone089f052007-03-16 16:20:31 -04002027static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2028 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05002029{
2030 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002031 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002032 int mark_free = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002033 u64 start;
2034 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002035 u64 priv;
Chris Masond1310b22008-01-24 16:13:08 -05002036 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002037 struct extent_io_tree *extent_ins;
2038 struct pending_extent_op *extent_op;
Chris Mason8ef97622007-03-26 10:15:30 -04002039
Chris Mason7d9eb122008-07-08 14:19:17 -04002040 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Zheng Yan31840ae2008-09-23 13:14:14 -04002041 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc162007-10-15 16:15:26 -04002042 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002043
2044 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002045 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2046 EXTENT_LOCKED);
2047 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05002048 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002049
2050 ret = get_state_private(pending_del, start, &priv);
2051 BUG_ON(ret);
2052 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2053
Chris Mason1a5bc162007-10-15 16:15:26 -04002054 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2055 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002056
2057 ret = pin_down_bytes(trans, extent_root, start,
2058 end + 1 - start, 0);
2059 mark_free = ret > 0;
2060 if (!test_range_bit(extent_ins, start, end,
2061 EXTENT_LOCKED, 0)) {
2062free_extent:
Chris Masonc286ac42008-07-22 23:06:41 -04002063 ret = __free_extent(trans, extent_root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002064 start, end + 1 - start,
2065 extent_op->orig_parent,
2066 extent_root->root_key.objectid,
2067 extent_op->orig_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002068 extent_op->level, 0, mark_free);
Zheng Yan31840ae2008-09-23 13:14:14 -04002069 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002070 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002071 kfree(extent_op);
2072 ret = get_state_private(extent_ins, start, &priv);
2073 BUG_ON(ret);
2074 extent_op = (struct pending_extent_op *)
2075 (unsigned long)priv;
2076
2077 clear_extent_bits(extent_ins, start, end,
2078 EXTENT_LOCKED, GFP_NOFS);
2079
2080 if (extent_op->type == PENDING_BACKREF_UPDATE)
2081 goto free_extent;
2082
2083 ret = update_block_group(trans, extent_root, start,
2084 end + 1 - start, 0, mark_free);
2085 BUG_ON(ret);
2086 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002087 }
Chris Mason1a5bc162007-10-15 16:15:26 -04002088 if (ret)
2089 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04002090
2091 if (need_resched()) {
2092 mutex_unlock(&extent_root->fs_info->alloc_mutex);
2093 cond_resched();
2094 mutex_lock(&extent_root->fs_info->alloc_mutex);
2095 }
Chris Masonfec577f2007-02-26 10:40:21 -05002096 }
Chris Masone20d96d2007-03-22 12:13:20 -04002097 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002098}
2099
2100/*
2101 * remove an extent from the root, returns 0 on success
2102 */
Chris Mason925baed2008-06-25 16:01:30 -04002103static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002104 struct btrfs_root *root,
2105 u64 bytenr, u64 num_bytes, u64 parent,
2106 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002107 u64 owner_objectid, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002108{
Chris Mason9f5fae22007-03-20 14:38:32 -04002109 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002110 int pending_ret;
2111 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002112
Chris Masondb945352007-10-15 16:15:53 -04002113 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002114 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002115 struct pending_extent_op *extent_op;
2116
2117 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2118 BUG_ON(!extent_op);
2119
2120 extent_op->type = PENDING_EXTENT_DELETE;
2121 extent_op->bytenr = bytenr;
2122 extent_op->num_bytes = num_bytes;
2123 extent_op->parent = parent;
2124 extent_op->orig_parent = parent;
2125 extent_op->generation = ref_generation;
2126 extent_op->orig_generation = ref_generation;
2127 extent_op->level = (int)owner_objectid;
2128
2129 set_extent_bits(&root->fs_info->pending_del,
2130 bytenr, bytenr + num_bytes - 1,
2131 EXTENT_LOCKED, GFP_NOFS);
2132 set_state_private(&root->fs_info->pending_del,
2133 bytenr, (unsigned long)extent_op);
Chris Masona28ec192007-03-06 20:08:01 -05002134 return 0;
2135 }
Chris Mason4bef0842008-09-08 11:18:08 -04002136 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002137 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2138 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002139 struct btrfs_block_group_cache *cache;
2140
Chris Masond00aff02008-09-11 15:54:42 -04002141 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002142 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2143 BUG_ON(!cache);
2144 btrfs_add_free_space(cache, bytenr, num_bytes);
Zheng Yane8569812008-09-26 10:05:48 -04002145 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002146 return 0;
2147 }
Chris Mason4bef0842008-09-08 11:18:08 -04002148 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002149 }
Chris Mason4bef0842008-09-08 11:18:08 -04002150
2151 /* if data pin when any transaction has committed this */
2152 if (ref_generation != trans->transid)
2153 pin = 1;
2154
Zheng Yan31840ae2008-09-23 13:14:14 -04002155 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002156 root_objectid, ref_generation,
2157 owner_objectid, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002158
2159 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002160 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05002161 return ret ? ret : pending_ret;
2162}
2163
Chris Mason925baed2008-06-25 16:01:30 -04002164int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002165 struct btrfs_root *root,
2166 u64 bytenr, u64 num_bytes, u64 parent,
2167 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002168 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002169{
2170 int ret;
2171
2172 maybe_lock_mutex(root);
Zheng Yan31840ae2008-09-23 13:14:14 -04002173 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002174 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002175 owner_objectid, pin);
Chris Mason925baed2008-06-25 16:01:30 -04002176 maybe_unlock_mutex(root);
2177 return ret;
2178}
2179
Chris Mason87ee04e2007-11-30 11:30:34 -05002180static u64 stripe_align(struct btrfs_root *root, u64 val)
2181{
2182 u64 mask = ((u64)root->stripesize - 1);
2183 u64 ret = (val + mask) & ~mask;
2184 return ret;
2185}
2186
Chris Masonfec577f2007-02-26 10:40:21 -05002187/*
2188 * walks the btree of allocated extents and find a hole of a given size.
2189 * The key ins is changed to record the hole:
2190 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002191 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002192 * ins->offset == number of blocks
2193 * Any available blocks before search_start are skipped.
2194 */
Chris Mason98ed5172008-01-03 10:01:48 -05002195static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2196 struct btrfs_root *orig_root,
2197 u64 num_bytes, u64 empty_size,
2198 u64 search_start, u64 search_end,
2199 u64 hint_byte, struct btrfs_key *ins,
2200 u64 exclude_start, u64 exclude_nr,
2201 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002202{
Chris Mason87ee04e2007-11-30 11:30:34 -05002203 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04002204 u64 orig_search_start;
Chris Mason9f5fae22007-03-20 14:38:32 -04002205 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04002206 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04002207 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002208 u64 *last_ptr = NULL;
Chris Masonbe08c1b2007-05-03 09:06:49 -04002209 struct btrfs_block_group_cache *block_group;
Chris Mason0ef3e662008-05-24 14:04:53 -04002210 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002211 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002212 int allowed_chunk_alloc = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05002213
Chris Masondb945352007-10-15 16:15:53 -04002214 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002215 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2216
Chris Mason0ef3e662008-05-24 14:04:53 -04002217 if (orig_root->ref_cows || empty_size)
2218 allowed_chunk_alloc = 1;
2219
Chris Mason239b14b2008-03-24 15:02:07 -04002220 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2221 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04002222 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002223 }
2224
Josef Bacik0f9dd462008-09-23 13:14:11 -04002225 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002226 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002227
Chris Mason239b14b2008-03-24 15:02:07 -04002228 if (last_ptr) {
2229 if (*last_ptr)
2230 hint_byte = *last_ptr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002231 else
Chris Mason239b14b2008-03-24 15:02:07 -04002232 empty_size += empty_cluster;
Chris Mason239b14b2008-03-24 15:02:07 -04002233 }
2234
Chris Masona061fc82008-05-07 11:43:44 -04002235 search_start = max(search_start, first_logical_byte(root, 0));
2236 orig_search_start = search_start;
2237
Chris Mason239b14b2008-03-24 15:02:07 -04002238 search_start = max(search_start, hint_byte);
Chris Mason6702ed42007-08-07 16:15:09 -04002239 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002240
Josef Bacik0f9dd462008-09-23 13:14:11 -04002241new_group:
Josef Bacik45b8c9a2008-09-30 14:40:06 -04002242 block_group = btrfs_lookup_block_group(info, search_start);
2243 if (!block_group)
2244 block_group = btrfs_lookup_first_block_group(info,
2245 search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002246
2247 /*
2248 * Ok this looks a little tricky, buts its really simple. First if we
2249 * didn't find a block group obviously we want to start over.
2250 * Secondly, if the block group we found does not match the type we
2251 * need, and we have a last_ptr and its not 0, chances are the last
2252 * allocation we made was at the end of the block group, so lets go
2253 * ahead and skip the looking through the rest of the block groups and
2254 * start at the beginning. This helps with metadata allocations,
2255 * since you are likely to have a bunch of data block groups to search
2256 * through first before you realize that you need to start over, so go
2257 * ahead and start over and save the time.
2258 */
2259 if (!block_group || (!block_group_bits(block_group, data) &&
2260 last_ptr && *last_ptr)) {
2261 if (search_start != orig_search_start) {
Josef Bacik45b8c9a2008-09-30 14:40:06 -04002262 if (last_ptr && *last_ptr) {
2263 total_needed += empty_cluster;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002264 *last_ptr = 0;
Josef Bacik45b8c9a2008-09-30 14:40:06 -04002265 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002266 search_start = orig_search_start;
2267 goto new_group;
2268 } else if (!chunk_alloc_done && allowed_chunk_alloc) {
2269 ret = do_chunk_alloc(trans, root,
2270 num_bytes + 2 * 1024 * 1024,
2271 data, 1);
Zheng Yane8569812008-09-26 10:05:48 -04002272 if (ret < 0)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002273 goto error;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002274 BUG_ON(ret);
2275 chunk_alloc_done = 1;
2276 search_start = orig_search_start;
2277 goto new_group;
2278 } else {
2279 ret = -ENOSPC;
2280 goto error;
Chris Mason0ef3e662008-05-24 14:04:53 -04002281 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002282 }
2283
2284 /*
2285 * this is going to seach through all of the existing block groups it
2286 * can find, so if we don't find something we need to see if we can
2287 * allocate what we need.
2288 */
2289 ret = find_free_space(root, &block_group, &search_start,
2290 total_needed, data);
2291 if (ret == -ENOSPC) {
2292 /*
2293 * instead of allocating, start at the original search start
2294 * and see if there is something to be found, if not then we
2295 * allocate
2296 */
2297 if (search_start != orig_search_start) {
2298 if (last_ptr && *last_ptr) {
2299 *last_ptr = 0;
2300 total_needed += empty_cluster;
2301 }
2302 search_start = orig_search_start;
2303 goto new_group;
2304 }
2305
2306 /*
2307 * we've already allocated, we're pretty screwed
2308 */
2309 if (chunk_alloc_done) {
2310 goto error;
2311 } else if (!allowed_chunk_alloc && block_group &&
2312 block_group_bits(block_group, data)) {
2313 block_group->space_info->force_alloc = 1;
2314 goto error;
2315 } else if (!allowed_chunk_alloc) {
2316 goto error;
2317 }
2318
2319 ret = do_chunk_alloc(trans, root, num_bytes + 2 * 1024 * 1024,
2320 data, 1);
2321 if (ret < 0)
2322 goto error;
2323
2324 BUG_ON(ret);
Chris Mason0ef3e662008-05-24 14:04:53 -04002325 chunk_alloc_done = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002326 if (block_group)
2327 search_start = block_group->key.objectid +
2328 block_group->key.offset;
2329 else
2330 search_start = orig_search_start;
2331 goto new_group;
Chris Mason0ef3e662008-05-24 14:04:53 -04002332 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002333
Chris Mason0b86a832008-03-24 15:01:56 -04002334 if (ret)
2335 goto error;
2336
Chris Mason87ee04e2007-11-30 11:30:34 -05002337 search_start = stripe_align(root, search_start);
Chris Masonfec577f2007-02-26 10:40:21 -05002338 ins->objectid = search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04002339 ins->offset = num_bytes;
Chris Masone37c9e62007-05-09 20:13:14 -04002340
Josef Bacik0f9dd462008-09-23 13:14:11 -04002341 if (ins->objectid + num_bytes >= search_end) {
2342 search_start = orig_search_start;
2343 if (chunk_alloc_done) {
2344 ret = -ENOSPC;
2345 goto error;
2346 }
2347 goto new_group;
2348 }
Chris Mason0b86a832008-03-24 15:01:56 -04002349
2350 if (ins->objectid + num_bytes >
2351 block_group->key.objectid + block_group->key.offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002352 if (search_start == orig_search_start && chunk_alloc_done) {
2353 ret = -ENOSPC;
2354 goto error;
2355 }
Chris Masone19caa52007-10-15 16:17:44 -04002356 search_start = block_group->key.objectid +
2357 block_group->key.offset;
2358 goto new_group;
2359 }
Chris Mason0b86a832008-03-24 15:01:56 -04002360
Chris Masondb945352007-10-15 16:15:53 -04002361 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04002362 ins->objectid < exclude_start + exclude_nr)) {
2363 search_start = exclude_start + exclude_nr;
2364 goto new_group;
2365 }
Chris Mason0b86a832008-03-24 15:01:56 -04002366
Josef Bacik0f9dd462008-09-23 13:14:11 -04002367 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2368 trans->block_group = block_group;
2369
Chris Masondb945352007-10-15 16:15:53 -04002370 ins->offset = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002371 if (last_ptr) {
2372 *last_ptr = ins->objectid + ins->offset;
2373 if (*last_ptr ==
Josef Bacik0f9dd462008-09-23 13:14:11 -04002374 btrfs_super_total_bytes(&root->fs_info->super_copy))
Chris Mason239b14b2008-03-24 15:02:07 -04002375 *last_ptr = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002376 }
Chris Masonbe744172007-05-06 10:15:01 -04002377
Josef Bacik0f9dd462008-09-23 13:14:11 -04002378 ret = 0;
Chris Mason0f70abe2007-02-28 16:46:22 -05002379error:
Chris Mason0f70abe2007-02-28 16:46:22 -05002380 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002381}
Chris Masonec44a352008-04-28 15:29:52 -04002382
Josef Bacik0f9dd462008-09-23 13:14:11 -04002383static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2384{
2385 struct btrfs_block_group_cache *cache;
2386 struct list_head *l;
2387
2388 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
Zheng Yane8569812008-09-26 10:05:48 -04002389 info->total_bytes - info->bytes_used - info->bytes_pinned -
2390 info->bytes_reserved, (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002391
2392 spin_lock(&info->lock);
2393 list_for_each(l, &info->block_groups) {
2394 cache = list_entry(l, struct btrfs_block_group_cache, list);
2395 spin_lock(&cache->lock);
2396 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
Zheng Yane8569812008-09-26 10:05:48 -04002397 "%Lu pinned %Lu reserved\n",
Josef Bacik0f9dd462008-09-23 13:14:11 -04002398 cache->key.objectid, cache->key.offset,
Zheng Yane8569812008-09-26 10:05:48 -04002399 btrfs_block_group_used(&cache->item),
2400 cache->pinned, cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002401 btrfs_dump_free_space(cache, bytes);
2402 spin_unlock(&cache->lock);
2403 }
2404 spin_unlock(&info->lock);
2405}
Zheng Yane8569812008-09-26 10:05:48 -04002406
Chris Masone6dcd2d2008-07-17 12:53:50 -04002407static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2408 struct btrfs_root *root,
2409 u64 num_bytes, u64 min_alloc_size,
2410 u64 empty_size, u64 hint_byte,
2411 u64 search_end, struct btrfs_key *ins,
2412 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05002413{
2414 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04002415 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002416 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04002417 struct btrfs_fs_info *info = root->fs_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002418 struct btrfs_block_group_cache *cache;
Chris Mason925baed2008-06-25 16:01:30 -04002419
Chris Mason6324fbf2008-03-24 15:01:59 -04002420 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04002421 alloc_profile = info->avail_data_alloc_bits &
2422 info->data_alloc_profile;
2423 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002424 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04002425 alloc_profile = info->avail_system_alloc_bits &
2426 info->system_alloc_profile;
2427 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002428 } else {
Chris Mason8790d502008-04-03 16:29:03 -04002429 alloc_profile = info->avail_metadata_alloc_bits &
2430 info->metadata_alloc_profile;
2431 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002432 }
Chris Mason98d20f62008-04-14 09:46:10 -04002433again:
Chris Masona061fc82008-05-07 11:43:44 -04002434 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04002435 /*
2436 * the only place that sets empty_size is btrfs_realloc_node, which
2437 * is not called recursively on allocations
2438 */
2439 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04002440 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04002441 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002442 2 * 1024 * 1024,
2443 BTRFS_BLOCK_GROUP_METADATA |
2444 (info->metadata_alloc_profile &
2445 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002446 }
2447 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002448 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002449 }
Chris Mason0b86a832008-03-24 15:01:56 -04002450
Chris Masondb945352007-10-15 16:15:53 -04002451 WARN_ON(num_bytes < root->sectorsize);
2452 ret = find_free_extent(trans, root, num_bytes, empty_size,
2453 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002454 trans->alloc_exclude_start,
2455 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002456
Chris Mason98d20f62008-04-14 09:46:10 -04002457 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2458 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002459 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002460 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002461 do_chunk_alloc(trans, root->fs_info->extent_root,
2462 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002463 goto again;
2464 }
Chris Masonec44a352008-04-28 15:29:52 -04002465 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002466 struct btrfs_space_info *sinfo;
2467
2468 sinfo = __find_space_info(root->fs_info, data);
2469 printk("allocation failed flags %Lu, wanted %Lu\n",
2470 data, num_bytes);
2471 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04002472 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002473 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002474 cache = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2475 if (!cache) {
2476 printk(KERN_ERR "Unable to find block group for %Lu\n", ins->objectid);
2477 return -ENOSPC;
2478 }
2479
2480 ret = btrfs_remove_free_space(cache, ins->objectid, ins->offset);
2481
2482 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002483}
2484
Chris Mason65b51a02008-08-01 15:11:20 -04002485int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2486{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002487 struct btrfs_block_group_cache *cache;
2488
Chris Mason65b51a02008-08-01 15:11:20 -04002489 maybe_lock_mutex(root);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002490 cache = btrfs_lookup_block_group(root->fs_info, start);
2491 if (!cache) {
2492 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
2493 maybe_unlock_mutex(root);
2494 return -ENOSPC;
2495 }
2496 btrfs_add_free_space(cache, start, len);
Zheng Yan1a40e232008-09-26 10:09:34 -04002497 update_reserved_extents(root, start, len, 0);
Chris Mason65b51a02008-08-01 15:11:20 -04002498 maybe_unlock_mutex(root);
2499 return 0;
2500}
2501
Chris Masone6dcd2d2008-07-17 12:53:50 -04002502int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2503 struct btrfs_root *root,
2504 u64 num_bytes, u64 min_alloc_size,
2505 u64 empty_size, u64 hint_byte,
2506 u64 search_end, struct btrfs_key *ins,
2507 u64 data)
2508{
2509 int ret;
2510 maybe_lock_mutex(root);
2511 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2512 empty_size, hint_byte, search_end, ins,
2513 data);
Zheng Yane8569812008-09-26 10:05:48 -04002514 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002515 maybe_unlock_mutex(root);
2516 return ret;
2517}
2518
2519static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002520 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002521 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002522 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002523{
2524 int ret;
2525 int pending_ret;
2526 u64 super_used;
2527 u64 root_used;
2528 u64 num_bytes = ins->offset;
2529 u32 sizes[2];
2530 struct btrfs_fs_info *info = root->fs_info;
2531 struct btrfs_root *extent_root = info->extent_root;
2532 struct btrfs_extent_item *extent_item;
2533 struct btrfs_extent_ref *ref;
2534 struct btrfs_path *path;
2535 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002536
Zheng Yan31840ae2008-09-23 13:14:14 -04002537 if (parent == 0)
2538 parent = ins->objectid;
2539
Josef Bacik58176a92007-08-29 15:47:34 -04002540 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002541 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002542 super_used = btrfs_super_bytes_used(&info->super_copy);
2543 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002544 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04002545
Josef Bacik58176a92007-08-29 15:47:34 -04002546 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002547 root_used = btrfs_root_used(&root->root_item);
2548 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002549
Chris Mason26b80032007-08-08 20:17:12 -04002550 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002551 struct pending_extent_op *extent_op;
2552
2553 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2554 BUG_ON(!extent_op);
2555
2556 extent_op->type = PENDING_EXTENT_INSERT;
2557 extent_op->bytenr = ins->objectid;
2558 extent_op->num_bytes = ins->offset;
2559 extent_op->parent = parent;
2560 extent_op->orig_parent = 0;
2561 extent_op->generation = ref_generation;
2562 extent_op->orig_generation = 0;
2563 extent_op->level = (int)owner;
2564
Chris Mason1a5bc162007-10-15 16:15:26 -04002565 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2566 ins->objectid + ins->offset - 1,
2567 EXTENT_LOCKED, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002568 set_state_private(&root->fs_info->extent_ins,
2569 ins->objectid, (unsigned long)extent_op);
Chris Mason26b80032007-08-08 20:17:12 -04002570 goto update_block;
2571 }
2572
Chris Mason47e4bb92008-02-01 14:51:59 -05002573 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05002574 keys[1].objectid = ins->objectid;
2575 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04002576 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05002577 sizes[0] = sizeof(*extent_item);
2578 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002579
2580 path = btrfs_alloc_path();
2581 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002582
2583 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2584 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04002585 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002586
Chris Mason47e4bb92008-02-01 14:51:59 -05002587 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2588 struct btrfs_extent_item);
2589 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2590 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2591 struct btrfs_extent_ref);
2592
2593 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2594 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2595 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Zheng Yan31840ae2008-09-23 13:14:14 -04002596 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05002597
2598 btrfs_mark_buffer_dirty(path->nodes[0]);
2599
2600 trans->alloc_exclude_start = 0;
2601 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002602 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002603 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002604 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04002605
Chris Mason925baed2008-06-25 16:01:30 -04002606 if (ret)
2607 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002608 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04002609 ret = pending_ret;
2610 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002611 }
Chris Mason26b80032007-08-08 20:17:12 -04002612
2613update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04002614 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002615 if (ret) {
2616 printk("update block group failed for %Lu %Lu\n",
2617 ins->objectid, ins->offset);
2618 BUG();
2619 }
Chris Mason925baed2008-06-25 16:01:30 -04002620out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04002621 return ret;
2622}
2623
2624int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002625 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002626 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002627 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002628{
2629 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04002630
2631 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2632 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002633 maybe_lock_mutex(root);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002634 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
2635 ref_generation, owner, ins);
Zheng Yane8569812008-09-26 10:05:48 -04002636 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002637 maybe_unlock_mutex(root);
2638 return ret;
2639}
Chris Masone02119d2008-09-05 16:13:11 -04002640
2641/*
2642 * this is used by the tree logging recovery code. It records that
2643 * an extent has been allocated and makes sure to clear the free
2644 * space cache bits as well
2645 */
2646int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002647 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04002648 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002649 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04002650{
2651 int ret;
2652 struct btrfs_block_group_cache *block_group;
2653
2654 maybe_lock_mutex(root);
2655 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2656 cache_block_group(root, block_group);
2657
Josef Bacik0f9dd462008-09-23 13:14:11 -04002658 ret = btrfs_remove_free_space(block_group, ins->objectid, ins->offset);
2659 BUG_ON(ret);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002660 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
2661 ref_generation, owner, ins);
Chris Masone02119d2008-09-05 16:13:11 -04002662 maybe_unlock_mutex(root);
2663 return ret;
2664}
2665
Chris Masone6dcd2d2008-07-17 12:53:50 -04002666/*
2667 * finds a free extent and does all the dirty work required for allocation
2668 * returns the key for the extent through ins, and a tree buffer for
2669 * the first block of the extent through buf.
2670 *
2671 * returns 0 if everything worked, non-zero otherwise.
2672 */
2673int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2674 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002675 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002676 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002677 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002678 u64 search_end, struct btrfs_key *ins, u64 data)
2679{
2680 int ret;
2681
2682 maybe_lock_mutex(root);
2683
2684 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2685 min_alloc_size, empty_size, hint_byte,
2686 search_end, ins, data);
2687 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04002688 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002689 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2690 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002691 owner_objectid, ins);
Chris Masond00aff02008-09-11 15:54:42 -04002692 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002693
Zheng Yane8569812008-09-26 10:05:48 -04002694 } else {
2695 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04002696 }
Chris Mason925baed2008-06-25 16:01:30 -04002697 maybe_unlock_mutex(root);
2698 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002699}
Chris Mason65b51a02008-08-01 15:11:20 -04002700
2701struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2702 struct btrfs_root *root,
2703 u64 bytenr, u32 blocksize)
2704{
2705 struct extent_buffer *buf;
2706
2707 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2708 if (!buf)
2709 return ERR_PTR(-ENOMEM);
2710 btrfs_set_header_generation(buf, trans->transid);
2711 btrfs_tree_lock(buf);
2712 clean_tree_block(trans, root, buf);
2713 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04002714 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2715 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04002716 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002717 } else {
2718 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2719 buf->start + buf->len - 1, GFP_NOFS);
2720 }
Chris Mason65b51a02008-08-01 15:11:20 -04002721 trans->blocks_used++;
2722 return buf;
2723}
2724
Chris Masonfec577f2007-02-26 10:40:21 -05002725/*
2726 * helper function to allocate a block for a given tree
2727 * returns the tree buffer or NULL.
2728 */
Chris Mason5f39d392007-10-15 16:14:19 -04002729struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04002730 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002731 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002732 u64 root_objectid,
2733 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05002734 int level,
2735 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04002736 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05002737{
Chris Masone2fa7222007-03-12 16:22:34 -04002738 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05002739 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002740 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05002741
Zheng Yan31840ae2008-09-23 13:14:14 -04002742 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002743 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04002744 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002745 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04002746 BUG_ON(ret > 0);
2747 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05002748 }
Chris Mason55c69072008-01-09 15:55:33 -05002749
Chris Mason65b51a02008-08-01 15:11:20 -04002750 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05002751 return buf;
2752}
Chris Masona28ec192007-03-06 20:08:01 -05002753
Chris Masone02119d2008-09-05 16:13:11 -04002754int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
2755 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04002756{
Chris Mason7bb86312007-12-11 09:25:06 -05002757 u64 leaf_owner;
2758 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04002759 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002760 struct btrfs_file_extent_item *fi;
2761 int i;
2762 int nritems;
2763 int ret;
2764
Chris Mason5f39d392007-10-15 16:14:19 -04002765 BUG_ON(!btrfs_is_leaf(leaf));
2766 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05002767 leaf_owner = btrfs_header_owner(leaf);
2768 leaf_generation = btrfs_header_generation(leaf);
2769
Chris Mason6407bf62007-03-27 06:33:00 -04002770 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002771 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04002772 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04002773
2774 btrfs_item_key_to_cpu(leaf, &key, i);
2775 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04002776 continue;
2777 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002778 if (btrfs_file_extent_type(leaf, fi) ==
2779 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04002780 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04002781 /*
2782 * FIXME make sure to insert a trans record that
2783 * repeats the snapshot del on crash
2784 */
Chris Masondb945352007-10-15 16:15:53 -04002785 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2786 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04002787 continue;
Chris Mason4a096752008-07-21 10:29:44 -04002788
2789 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002790 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002791 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04002792 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002793 key.objectid, 0);
Chris Mason4a096752008-07-21 10:29:44 -04002794 mutex_unlock(&root->fs_info->alloc_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002795 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04002796
2797 atomic_inc(&root->fs_info->throttle_gen);
2798 wake_up(&root->fs_info->transaction_throttle);
2799 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04002800 }
2801 return 0;
2802}
2803
Chris Masone02119d2008-09-05 16:13:11 -04002804static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2805 struct btrfs_root *root,
2806 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04002807{
2808 int i;
2809 int ret;
2810 struct btrfs_extent_info *info = ref->extents;
2811
Yan Zheng31153d82008-07-28 15:32:19 -04002812 for (i = 0; i < ref->nritems; i++) {
2813 mutex_lock(&root->fs_info->alloc_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002814 ret = __btrfs_free_extent(trans, root, info->bytenr,
2815 info->num_bytes, ref->bytenr,
2816 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002817 info->objectid, 0);
Yan Zheng31153d82008-07-28 15:32:19 -04002818 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason2dd3e672008-08-04 08:20:15 -04002819
2820 atomic_inc(&root->fs_info->throttle_gen);
2821 wake_up(&root->fs_info->transaction_throttle);
2822 cond_resched();
2823
Yan Zheng31153d82008-07-28 15:32:19 -04002824 BUG_ON(ret);
2825 info++;
2826 }
Yan Zheng31153d82008-07-28 15:32:19 -04002827
2828 return 0;
2829}
2830
Chris Mason333db942008-06-25 16:01:30 -04002831int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2832 u32 *refs)
2833{
Chris Mason017e5362008-07-28 15:32:51 -04002834 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04002835
Zheng Yan31840ae2008-09-23 13:14:14 -04002836 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04002837 BUG_ON(ret);
2838
2839#if 0 // some debugging code in case we see problems here
2840 /* if the refs count is one, it won't get increased again. But
2841 * if the ref count is > 1, someone may be decreasing it at
2842 * the same time we are.
2843 */
2844 if (*refs != 1) {
2845 struct extent_buffer *eb = NULL;
2846 eb = btrfs_find_create_tree_block(root, start, len);
2847 if (eb)
2848 btrfs_tree_lock(eb);
2849
2850 mutex_lock(&root->fs_info->alloc_mutex);
2851 ret = lookup_extent_ref(NULL, root, start, len, refs);
2852 BUG_ON(ret);
2853 mutex_unlock(&root->fs_info->alloc_mutex);
2854
2855 if (eb) {
2856 btrfs_tree_unlock(eb);
2857 free_extent_buffer(eb);
2858 }
2859 if (*refs == 1) {
2860 printk("block %llu went down to one during drop_snap\n",
2861 (unsigned long long)start);
2862 }
2863
2864 }
2865#endif
2866
Chris Masone7a84562008-06-25 16:01:31 -04002867 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04002868 return ret;
Chris Mason333db942008-06-25 16:01:30 -04002869}
2870
2871/*
Chris Mason9aca1d52007-03-13 11:09:37 -04002872 * helper function for drop_snapshot, this walks down the tree dropping ref
2873 * counts as it goes.
2874 */
Chris Mason98ed5172008-01-03 10:01:48 -05002875static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2876 struct btrfs_root *root,
2877 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002878{
Chris Mason7bb86312007-12-11 09:25:06 -05002879 u64 root_owner;
2880 u64 root_gen;
2881 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04002882 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002883 struct extent_buffer *next;
2884 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002885 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04002886 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04002887 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002888 int ret;
2889 u32 refs;
2890
Chris Mason5caf2a02007-04-02 11:20:42 -04002891 WARN_ON(*level < 0);
2892 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04002893 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04002894 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002895 BUG_ON(ret);
2896 if (refs > 1)
2897 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002898
Chris Mason9aca1d52007-03-13 11:09:37 -04002899 /*
2900 * walk down to the last node level and free all the leaves
2901 */
Chris Mason6407bf62007-03-27 06:33:00 -04002902 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002903 WARN_ON(*level < 0);
2904 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002905 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002906
Chris Mason5f39d392007-10-15 16:14:19 -04002907 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002908 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002909
Chris Mason7518a232007-03-12 12:01:18 -04002910 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002911 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002912 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002913 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002914 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04002915 BUG_ON(ret);
2916 break;
2917 }
Chris Masondb945352007-10-15 16:15:53 -04002918 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04002919 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04002920 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002921
Chris Mason333db942008-06-25 16:01:30 -04002922 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002923 BUG_ON(ret);
2924 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002925 parent = path->nodes[*level];
2926 root_owner = btrfs_header_owner(parent);
2927 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002928 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04002929
2930 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002931 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04002932 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002933 root_owner, root_gen,
2934 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002935 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002936 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason18e35e02008-08-01 13:11:41 -04002937
2938 atomic_inc(&root->fs_info->throttle_gen);
2939 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04002940 cond_resched();
Chris Mason18e35e02008-08-01 13:11:41 -04002941
Chris Mason20524f02007-03-10 06:35:47 -05002942 continue;
2943 }
Chris Masonf87f0572008-08-01 11:27:23 -04002944 /*
2945 * at this point, we have a single ref, and since the
2946 * only place referencing this extent is a dead root
2947 * the reference count should never go higher.
2948 * So, we don't need to check it again
2949 */
Yan Zheng31153d82008-07-28 15:32:19 -04002950 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04002951 ref = btrfs_lookup_leaf_ref(root, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04002952 if (ref && ref->generation != ptr_gen) {
2953 btrfs_free_leaf_ref(root, ref);
2954 ref = NULL;
2955 }
Yan Zheng31153d82008-07-28 15:32:19 -04002956 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04002957 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002958 BUG_ON(ret);
2959 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04002960 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002961 *level = 0;
2962 break;
2963 }
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002964 if (printk_ratelimit()) {
Chris Mason37d1aee2008-07-31 10:48:37 -04002965 printk("leaf ref miss for bytenr %llu\n",
2966 (unsigned long long)bytenr);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002967 }
Yan Zheng31153d82008-07-28 15:32:19 -04002968 }
Chris Masondb945352007-10-15 16:15:53 -04002969 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04002970 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002971 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04002972
Chris Masonca7a79a2008-05-12 12:59:19 -04002973 next = read_tree_block(root, bytenr, blocksize,
2974 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04002975 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04002976#if 0
2977 /*
2978 * this is a debugging check and can go away
2979 * the ref should never go all the way down to 1
2980 * at this point
2981 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002982 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2983 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002984 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002985 WARN_ON(refs != 1);
2986#endif
Chris Masone9d0b132007-08-10 14:06:19 -04002987 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002988 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002989 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002990 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002991 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002992 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002993 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04002994 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05002995 }
2996out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002997 WARN_ON(*level < 0);
2998 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002999
3000 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003001 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003002 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003003 } else {
3004 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003005 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003006 }
3007
Yan Zheng31153d82008-07-28 15:32:19 -04003008 blocksize = btrfs_level_size(root, *level);
3009 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003010 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003011
Chris Masonf87f0572008-08-01 11:27:23 -04003012 mutex_lock(&root->fs_info->alloc_mutex);
Yan Zheng31153d82008-07-28 15:32:19 -04003013 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003014 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003015 *level, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003016 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04003017 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003018 path->nodes[*level] = NULL;
3019 *level += 1;
3020 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003021
Chris Masone7a84562008-06-25 16:01:31 -04003022 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003023 return 0;
3024}
3025
Chris Mason9aca1d52007-03-13 11:09:37 -04003026/*
3027 * helper for dropping snapshots. This walks back up the tree in the path
3028 * to find the first node higher up where we haven't yet gone through
3029 * all the slots
3030 */
Chris Mason98ed5172008-01-03 10:01:48 -05003031static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3032 struct btrfs_root *root,
3033 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003034{
Chris Mason7bb86312007-12-11 09:25:06 -05003035 u64 root_owner;
3036 u64 root_gen;
3037 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003038 int i;
3039 int slot;
3040 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003041
Chris Mason234b63a2007-03-13 10:46:10 -04003042 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003043 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003044 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3045 struct extent_buffer *node;
3046 struct btrfs_disk_key disk_key;
3047 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003048 path->slots[i]++;
3049 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003050 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003051 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003052 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003053 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003054 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003055 return 0;
3056 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003057 struct extent_buffer *parent;
3058 if (path->nodes[*level] == root->node)
3059 parent = path->nodes[*level];
3060 else
3061 parent = path->nodes[*level + 1];
3062
3063 root_owner = btrfs_header_owner(parent);
3064 root_gen = btrfs_header_generation(parent);
Chris Masone089f052007-03-16 16:20:31 -04003065 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003066 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003067 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003068 parent->start, root_owner,
3069 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003070 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003071 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003072 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003073 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003074 }
3075 }
3076 return 1;
3077}
3078
Chris Mason9aca1d52007-03-13 11:09:37 -04003079/*
3080 * drop the reference count on the tree rooted at 'snap'. This traverses
3081 * the tree freeing any blocks that have a ref count of zero after being
3082 * decremented.
3083 */
Chris Masone089f052007-03-16 16:20:31 -04003084int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003085 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003086{
Chris Mason3768f362007-03-13 16:47:54 -04003087 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003088 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003089 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003090 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003091 int i;
3092 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003093 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003094
Chris Masona2135012008-06-25 16:01:30 -04003095 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003096 path = btrfs_alloc_path();
3097 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003098
Chris Mason5f39d392007-10-15 16:14:19 -04003099 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003100 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003101 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3102 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003103 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003104 path->slots[level] = 0;
3105 } else {
3106 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003107 struct btrfs_disk_key found_key;
3108 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003109
Chris Mason9f3a7422007-08-07 15:52:19 -04003110 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003111 level = root_item->drop_level;
3112 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003113 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003114 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003115 ret = wret;
3116 goto out;
3117 }
Chris Mason5f39d392007-10-15 16:14:19 -04003118 node = path->nodes[level];
3119 btrfs_node_key(node, &found_key, path->slots[level]);
3120 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3121 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003122 /*
3123 * unlock our path, this is safe because only this
3124 * function is allowed to delete this snapshot
3125 */
Chris Mason925baed2008-06-25 16:01:30 -04003126 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3127 if (path->nodes[i] && path->locks[i]) {
3128 path->locks[i] = 0;
3129 btrfs_tree_unlock(path->nodes[i]);
3130 }
3131 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003132 }
Chris Mason20524f02007-03-10 06:35:47 -05003133 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003134 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003135 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003136 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003137 if (wret < 0)
3138 ret = wret;
3139
Chris Mason5caf2a02007-04-02 11:20:42 -04003140 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003141 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003142 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003143 if (wret < 0)
3144 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003145 if (trans->transaction->in_commit) {
3146 ret = -EAGAIN;
3147 break;
3148 }
Chris Mason18e35e02008-08-01 13:11:41 -04003149 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003150 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003151 }
Chris Mason83e15a22007-03-12 09:03:27 -04003152 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003153 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003154 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003155 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003156 }
Chris Mason20524f02007-03-10 06:35:47 -05003157 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003158out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003159 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003160 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003161}
Chris Mason9078a3e2007-04-26 16:46:15 -04003162
Chris Mason8e7bf942008-04-28 09:02:36 -04003163static unsigned long calc_ra(unsigned long start, unsigned long last,
3164 unsigned long nr)
3165{
3166 return min(last, start + nr - 1);
3167}
3168
Chris Mason98ed5172008-01-03 10:01:48 -05003169static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3170 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003171{
3172 u64 page_start;
3173 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003174 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003175 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003176 unsigned long i;
3177 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003178 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003179 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003180 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003181 unsigned int total_read = 0;
3182 unsigned int total_dirty = 0;
3183 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003184
3185 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003186
3187 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003188 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003189 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3190
Zheng Yan1a40e232008-09-26 10:09:34 -04003191 /* make sure the dirty trick played by the caller work */
3192 ret = invalidate_inode_pages2_range(inode->i_mapping,
3193 first_index, last_index);
3194 if (ret)
3195 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003196
Chris Mason4313b392008-01-03 09:08:48 -05003197 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003198
Zheng Yan1a40e232008-09-26 10:09:34 -04003199 for (i = first_index ; i <= last_index; i++) {
3200 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003201 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003202 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003203 }
3204 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003205again:
3206 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003207 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003208 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003209 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003210 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003211 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003212 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003213 if (!PageUptodate(page)) {
3214 btrfs_readpage(NULL, page);
3215 lock_page(page);
3216 if (!PageUptodate(page)) {
3217 unlock_page(page);
3218 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003219 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003220 goto out_unlock;
3221 }
3222 }
Chris Masonec44a352008-04-28 15:29:52 -04003223 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003224
Chris Masonedbd8d42007-12-21 16:27:24 -05003225 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3226 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003227 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003228
Chris Mason3eaa2882008-07-24 11:57:52 -04003229 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3230 if (ordered) {
3231 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3232 unlock_page(page);
3233 page_cache_release(page);
3234 btrfs_start_ordered_extent(inode, ordered, 1);
3235 btrfs_put_ordered_extent(ordered);
3236 goto again;
3237 }
3238 set_page_extent_mapped(page);
3239
Chris Masonea8c2812008-08-04 23:17:27 -04003240 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003241 if (i == first_index)
3242 set_extent_bits(io_tree, page_start, page_end,
3243 EXTENT_BOUNDARY, GFP_NOFS);
3244
Chris Masona061fc82008-05-07 11:43:44 -04003245 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003246 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003247
Chris Masond1310b22008-01-24 16:13:08 -05003248 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003249 unlock_page(page);
3250 page_cache_release(page);
3251 }
3252
3253out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04003254 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05003255 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003256 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04003257 return ret;
3258}
3259
Zheng Yan1a40e232008-09-26 10:09:34 -04003260static int noinline relocate_data_extent(struct inode *reloc_inode,
3261 struct btrfs_key *extent_key,
3262 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05003263{
Zheng Yan1a40e232008-09-26 10:09:34 -04003264 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3265 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
3266 struct extent_map *em;
3267
3268 em = alloc_extent_map(GFP_NOFS);
3269 BUG_ON(!em || IS_ERR(em));
3270
3271 em->start = extent_key->objectid - offset;
3272 em->len = extent_key->offset;
3273 em->block_start = extent_key->objectid;
3274 em->bdev = root->fs_info->fs_devices->latest_bdev;
3275 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3276
3277 /* setup extent map to cheat btrfs_readpage */
3278 mutex_lock(&BTRFS_I(reloc_inode)->extent_mutex);
3279 while (1) {
3280 int ret;
3281 spin_lock(&em_tree->lock);
3282 ret = add_extent_mapping(em_tree, em);
3283 spin_unlock(&em_tree->lock);
3284 if (ret != -EEXIST) {
3285 free_extent_map(em);
3286 break;
3287 }
3288 btrfs_drop_extent_cache(reloc_inode, em->start,
3289 em->start + em->len - 1, 0);
3290 }
3291 mutex_unlock(&BTRFS_I(reloc_inode)->extent_mutex);
3292
3293 return relocate_inode_pages(reloc_inode, extent_key->objectid - offset,
3294 extent_key->offset);
3295}
3296
3297struct btrfs_ref_path {
3298 u64 extent_start;
3299 u64 nodes[BTRFS_MAX_LEVEL];
3300 u64 root_objectid;
3301 u64 root_generation;
3302 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04003303 u32 num_refs;
3304 int lowest_level;
3305 int current_level;
3306};
3307
3308struct disk_extent {
3309 u64 disk_bytenr;
3310 u64 disk_num_bytes;
3311 u64 offset;
3312 u64 num_bytes;
3313};
3314
3315static int is_cowonly_root(u64 root_objectid)
3316{
3317 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
3318 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
3319 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
3320 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
3321 root_objectid == BTRFS_TREE_LOG_OBJECTID)
3322 return 1;
3323 return 0;
3324}
3325
3326static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
3327 struct btrfs_root *extent_root,
3328 struct btrfs_ref_path *ref_path,
3329 int first_time)
3330{
3331 struct extent_buffer *leaf;
3332 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05003333 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05003334 struct btrfs_key key;
3335 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04003336 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05003337 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04003338 int level;
3339 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05003340
Zheng Yan1a40e232008-09-26 10:09:34 -04003341 path = btrfs_alloc_path();
3342 if (!path)
3343 return -ENOMEM;
3344
3345 mutex_lock(&extent_root->fs_info->alloc_mutex);
3346
3347 if (first_time) {
3348 ref_path->lowest_level = -1;
3349 ref_path->current_level = -1;
3350 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04003351 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003352walk_down:
3353 level = ref_path->current_level - 1;
3354 while (level >= -1) {
3355 u64 parent;
3356 if (level < ref_path->lowest_level)
3357 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05003358
Zheng Yan1a40e232008-09-26 10:09:34 -04003359 if (level >= 0) {
3360 bytenr = ref_path->nodes[level];
3361 } else {
3362 bytenr = ref_path->extent_start;
3363 }
3364 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003365
Zheng Yan1a40e232008-09-26 10:09:34 -04003366 parent = ref_path->nodes[level + 1];
3367 ref_path->nodes[level + 1] = 0;
3368 ref_path->current_level = level;
3369 BUG_ON(parent == 0);
3370
3371 key.objectid = bytenr;
3372 key.offset = parent + 1;
3373 key.type = BTRFS_EXTENT_REF_KEY;
3374
3375 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003376 if (ret < 0)
3377 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003378 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003379
Chris Masonedbd8d42007-12-21 16:27:24 -05003380 leaf = path->nodes[0];
3381 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04003382 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04003383 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04003384 if (ret < 0)
3385 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003386 if (ret > 0)
3387 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04003388 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04003389 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003390
3391 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04003392 if (found_key.objectid == bytenr &&
3393 found_key.type == BTRFS_EXTENT_REF_KEY)
3394 goto found;
3395next:
3396 level--;
3397 btrfs_release_path(extent_root, path);
3398 if (need_resched()) {
3399 mutex_unlock(&extent_root->fs_info->alloc_mutex);
3400 cond_resched();
3401 mutex_lock(&extent_root->fs_info->alloc_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04003402 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003403 }
3404 /* reached lowest level */
3405 ret = 1;
3406 goto out;
3407walk_up:
3408 level = ref_path->current_level;
3409 while (level < BTRFS_MAX_LEVEL - 1) {
3410 u64 ref_objectid;
3411 if (level >= 0) {
3412 bytenr = ref_path->nodes[level];
3413 } else {
3414 bytenr = ref_path->extent_start;
Chris Masona061fc82008-05-07 11:43:44 -04003415 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003416 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003417
Zheng Yan1a40e232008-09-26 10:09:34 -04003418 key.objectid = bytenr;
3419 key.offset = 0;
3420 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05003421
Zheng Yan1a40e232008-09-26 10:09:34 -04003422 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
3423 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05003424 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003425
3426 leaf = path->nodes[0];
3427 nritems = btrfs_header_nritems(leaf);
3428 if (path->slots[0] >= nritems) {
3429 ret = btrfs_next_leaf(extent_root, path);
3430 if (ret < 0)
3431 goto out;
3432 if (ret > 0) {
3433 /* the extent was freed by someone */
3434 if (ref_path->lowest_level == level)
3435 goto out;
3436 btrfs_release_path(extent_root, path);
3437 goto walk_down;
3438 }
3439 leaf = path->nodes[0];
3440 }
3441
3442 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3443 if (found_key.objectid != bytenr ||
3444 found_key.type != BTRFS_EXTENT_REF_KEY) {
3445 /* the extent was freed by someone */
3446 if (ref_path->lowest_level == level) {
3447 ret = 1;
3448 goto out;
3449 }
3450 btrfs_release_path(extent_root, path);
3451 goto walk_down;
3452 }
3453found:
3454 ref = btrfs_item_ptr(leaf, path->slots[0],
3455 struct btrfs_extent_ref);
3456 ref_objectid = btrfs_ref_objectid(leaf, ref);
3457 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3458 if (first_time) {
3459 level = (int)ref_objectid;
3460 BUG_ON(level >= BTRFS_MAX_LEVEL);
3461 ref_path->lowest_level = level;
3462 ref_path->current_level = level;
3463 ref_path->nodes[level] = bytenr;
3464 } else {
3465 WARN_ON(ref_objectid != level);
3466 }
3467 } else {
3468 WARN_ON(level != -1);
3469 }
3470 first_time = 0;
3471
3472 if (ref_path->lowest_level == level) {
3473 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04003474 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
3475 }
3476
3477 /*
3478 * the block is tree root or the block isn't in reference
3479 * counted tree.
3480 */
3481 if (found_key.objectid == found_key.offset ||
3482 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
3483 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
3484 ref_path->root_generation =
3485 btrfs_ref_generation(leaf, ref);
3486 if (level < 0) {
3487 /* special reference from the tree log */
3488 ref_path->nodes[0] = found_key.offset;
3489 ref_path->current_level = 0;
3490 }
3491 ret = 0;
3492 goto out;
3493 }
3494
3495 level++;
3496 BUG_ON(ref_path->nodes[level] != 0);
3497 ref_path->nodes[level] = found_key.offset;
3498 ref_path->current_level = level;
3499
3500 /*
3501 * the reference was created in the running transaction,
3502 * no need to continue walking up.
3503 */
3504 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
3505 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
3506 ref_path->root_generation =
3507 btrfs_ref_generation(leaf, ref);
3508 ret = 0;
3509 goto out;
3510 }
3511
3512 btrfs_release_path(extent_root, path);
3513 if (need_resched()) {
3514 mutex_unlock(&extent_root->fs_info->alloc_mutex);
3515 cond_resched();
3516 mutex_lock(&extent_root->fs_info->alloc_mutex);
3517 }
3518 }
3519 /* reached max tree level, but no tree root found. */
3520 BUG();
3521out:
3522 mutex_unlock(&extent_root->fs_info->alloc_mutex);
3523 btrfs_free_path(path);
3524 return ret;
3525}
3526
3527static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
3528 struct btrfs_root *extent_root,
3529 struct btrfs_ref_path *ref_path,
3530 u64 extent_start)
3531{
3532 memset(ref_path, 0, sizeof(*ref_path));
3533 ref_path->extent_start = extent_start;
3534
3535 return __next_ref_path(trans, extent_root, ref_path, 1);
3536}
3537
3538static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
3539 struct btrfs_root *extent_root,
3540 struct btrfs_ref_path *ref_path)
3541{
3542 return __next_ref_path(trans, extent_root, ref_path, 0);
3543}
3544
3545static int noinline get_new_locations(struct inode *reloc_inode,
3546 struct btrfs_key *extent_key,
3547 u64 offset, int no_fragment,
3548 struct disk_extent **extents,
3549 int *nr_extents)
3550{
3551 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3552 struct btrfs_path *path;
3553 struct btrfs_file_extent_item *fi;
3554 struct extent_buffer *leaf;
3555 struct disk_extent *exts = *extents;
3556 struct btrfs_key found_key;
3557 u64 cur_pos;
3558 u64 last_byte;
3559 u32 nritems;
3560 int nr = 0;
3561 int max = *nr_extents;
3562 int ret;
3563
3564 WARN_ON(!no_fragment && *extents);
3565 if (!exts) {
3566 max = 1;
3567 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
3568 if (!exts)
3569 return -ENOMEM;
3570 }
3571
3572 path = btrfs_alloc_path();
3573 BUG_ON(!path);
3574
3575 cur_pos = extent_key->objectid - offset;
3576 last_byte = extent_key->objectid + extent_key->offset;
3577 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
3578 cur_pos, 0);
3579 if (ret < 0)
3580 goto out;
3581 if (ret > 0) {
3582 ret = -ENOENT;
3583 goto out;
3584 }
3585
3586 while (1) {
3587 leaf = path->nodes[0];
3588 nritems = btrfs_header_nritems(leaf);
3589 if (path->slots[0] >= nritems) {
3590 ret = btrfs_next_leaf(root, path);
3591 if (ret < 0)
3592 goto out;
3593 if (ret > 0)
3594 break;
3595 leaf = path->nodes[0];
3596 }
3597
3598 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3599 if (found_key.offset != cur_pos ||
3600 found_key.type != BTRFS_EXTENT_DATA_KEY ||
3601 found_key.objectid != reloc_inode->i_ino)
3602 break;
3603
3604 fi = btrfs_item_ptr(leaf, path->slots[0],
3605 struct btrfs_file_extent_item);
3606 if (btrfs_file_extent_type(leaf, fi) !=
3607 BTRFS_FILE_EXTENT_REG ||
3608 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
3609 break;
3610
3611 if (nr == max) {
3612 struct disk_extent *old = exts;
3613 max *= 2;
3614 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
3615 memcpy(exts, old, sizeof(*exts) * nr);
3616 if (old != *extents)
3617 kfree(old);
3618 }
3619
3620 exts[nr].disk_bytenr =
3621 btrfs_file_extent_disk_bytenr(leaf, fi);
3622 exts[nr].disk_num_bytes =
3623 btrfs_file_extent_disk_num_bytes(leaf, fi);
3624 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
3625 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
3626 WARN_ON(exts[nr].offset > 0);
3627 WARN_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
3628
3629 cur_pos += exts[nr].num_bytes;
3630 nr++;
3631
3632 if (cur_pos + offset >= last_byte)
3633 break;
3634
3635 if (no_fragment) {
3636 ret = 1;
3637 goto out;
3638 }
3639 path->slots[0]++;
3640 }
3641
3642 WARN_ON(cur_pos + offset > last_byte);
3643 if (cur_pos + offset < last_byte) {
3644 ret = -ENOENT;
3645 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05003646 }
3647 ret = 0;
3648out:
Zheng Yan1a40e232008-09-26 10:09:34 -04003649 btrfs_free_path(path);
3650 if (ret) {
3651 if (exts != *extents)
3652 kfree(exts);
3653 } else {
3654 *extents = exts;
3655 *nr_extents = nr;
3656 }
3657 return ret;
3658}
3659
3660static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
3661 struct btrfs_root *root,
3662 struct btrfs_path *path,
3663 struct btrfs_key *extent_key,
3664 struct btrfs_key *leaf_key,
3665 struct btrfs_ref_path *ref_path,
3666 struct disk_extent *new_extents,
3667 int nr_extents)
3668{
3669 struct extent_buffer *leaf;
3670 struct btrfs_file_extent_item *fi;
3671 struct inode *inode = NULL;
3672 struct btrfs_key key;
3673 u64 lock_start = 0;
3674 u64 lock_end = 0;
3675 u64 num_bytes;
3676 u64 ext_offset;
3677 u64 first_pos;
3678 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003679 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04003680 int extent_locked = 0;
3681 int ret;
3682
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003683 memcpy(&key, leaf_key, sizeof(key));
3684 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04003685 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003686 if (key.objectid < ref_path->owner_objectid ||
3687 (key.objectid == ref_path->owner_objectid &&
3688 key.type < BTRFS_EXTENT_DATA_KEY)) {
3689 key.objectid = ref_path->owner_objectid;
3690 key.type = BTRFS_EXTENT_DATA_KEY;
3691 key.offset = 0;
3692 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003693 }
3694
3695 while (1) {
3696 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3697 if (ret < 0)
3698 goto out;
3699
3700 leaf = path->nodes[0];
3701 nritems = btrfs_header_nritems(leaf);
3702next:
3703 if (extent_locked && ret > 0) {
3704 /*
3705 * the file extent item was modified by someone
3706 * before the extent got locked.
3707 */
3708 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
3709 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3710 lock_end, GFP_NOFS);
3711 extent_locked = 0;
3712 }
3713
3714 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003715 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04003716 break;
3717
3718 BUG_ON(extent_locked);
3719 ret = btrfs_next_leaf(root, path);
3720 if (ret < 0)
3721 goto out;
3722 if (ret > 0)
3723 break;
3724 leaf = path->nodes[0];
3725 nritems = btrfs_header_nritems(leaf);
3726 }
3727
3728 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3729
3730 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
3731 if ((key.objectid > ref_path->owner_objectid) ||
3732 (key.objectid == ref_path->owner_objectid &&
3733 key.type > BTRFS_EXTENT_DATA_KEY) ||
3734 (key.offset >= first_pos + extent_key->offset))
3735 break;
3736 }
3737
3738 if (inode && key.objectid != inode->i_ino) {
3739 BUG_ON(extent_locked);
3740 btrfs_release_path(root, path);
3741 mutex_unlock(&inode->i_mutex);
3742 iput(inode);
3743 inode = NULL;
3744 continue;
3745 }
3746
3747 if (key.type != BTRFS_EXTENT_DATA_KEY) {
3748 path->slots[0]++;
3749 ret = 1;
3750 goto next;
3751 }
3752 fi = btrfs_item_ptr(leaf, path->slots[0],
3753 struct btrfs_file_extent_item);
3754 if ((btrfs_file_extent_type(leaf, fi) !=
3755 BTRFS_FILE_EXTENT_REG) ||
3756 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
3757 extent_key->objectid)) {
3758 path->slots[0]++;
3759 ret = 1;
3760 goto next;
3761 }
3762
3763 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
3764 ext_offset = btrfs_file_extent_offset(leaf, fi);
3765
3766 if (first_pos > key.offset - ext_offset)
3767 first_pos = key.offset - ext_offset;
3768
3769 if (!extent_locked) {
3770 lock_start = key.offset;
3771 lock_end = lock_start + num_bytes - 1;
3772 } else {
3773 BUG_ON(lock_start != key.offset);
3774 BUG_ON(lock_end - lock_start + 1 < num_bytes);
3775 }
3776
3777 if (!inode) {
3778 btrfs_release_path(root, path);
3779
3780 inode = btrfs_iget_locked(root->fs_info->sb,
3781 key.objectid, root);
3782 if (inode->i_state & I_NEW) {
3783 BTRFS_I(inode)->root = root;
3784 BTRFS_I(inode)->location.objectid =
3785 key.objectid;
3786 BTRFS_I(inode)->location.type =
3787 BTRFS_INODE_ITEM_KEY;
3788 BTRFS_I(inode)->location.offset = 0;
3789 btrfs_read_locked_inode(inode);
3790 unlock_new_inode(inode);
3791 }
3792 /*
3793 * some code call btrfs_commit_transaction while
3794 * holding the i_mutex, so we can't use mutex_lock
3795 * here.
3796 */
3797 if (is_bad_inode(inode) ||
3798 !mutex_trylock(&inode->i_mutex)) {
3799 iput(inode);
3800 inode = NULL;
3801 key.offset = (u64)-1;
3802 goto skip;
3803 }
3804 }
3805
3806 if (!extent_locked) {
3807 struct btrfs_ordered_extent *ordered;
3808
3809 btrfs_release_path(root, path);
3810
3811 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3812 lock_end, GFP_NOFS);
3813 ordered = btrfs_lookup_first_ordered_extent(inode,
3814 lock_end);
3815 if (ordered &&
3816 ordered->file_offset <= lock_end &&
3817 ordered->file_offset + ordered->len > lock_start) {
3818 unlock_extent(&BTRFS_I(inode)->io_tree,
3819 lock_start, lock_end, GFP_NOFS);
3820 btrfs_start_ordered_extent(inode, ordered, 1);
3821 btrfs_put_ordered_extent(ordered);
3822 key.offset += num_bytes;
3823 goto skip;
3824 }
3825 if (ordered)
3826 btrfs_put_ordered_extent(ordered);
3827
3828 mutex_lock(&BTRFS_I(inode)->extent_mutex);
3829 extent_locked = 1;
3830 continue;
3831 }
3832
3833 if (nr_extents == 1) {
3834 /* update extent pointer in place */
3835 btrfs_set_file_extent_generation(leaf, fi,
3836 trans->transid);
3837 btrfs_set_file_extent_disk_bytenr(leaf, fi,
3838 new_extents[0].disk_bytenr);
3839 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
3840 new_extents[0].disk_num_bytes);
3841 ext_offset += new_extents[0].offset;
3842 btrfs_set_file_extent_offset(leaf, fi, ext_offset);
3843 btrfs_mark_buffer_dirty(leaf);
3844
3845 btrfs_drop_extent_cache(inode, key.offset,
3846 key.offset + num_bytes - 1, 0);
3847
3848 ret = btrfs_inc_extent_ref(trans, root,
3849 new_extents[0].disk_bytenr,
3850 new_extents[0].disk_num_bytes,
3851 leaf->start,
3852 root->root_key.objectid,
3853 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003854 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04003855 BUG_ON(ret);
3856
3857 ret = btrfs_free_extent(trans, root,
3858 extent_key->objectid,
3859 extent_key->offset,
3860 leaf->start,
3861 btrfs_header_owner(leaf),
3862 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003863 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04003864 BUG_ON(ret);
3865
3866 btrfs_release_path(root, path);
3867 key.offset += num_bytes;
3868 } else {
3869 u64 alloc_hint;
3870 u64 extent_len;
3871 int i;
3872 /*
3873 * drop old extent pointer at first, then insert the
3874 * new pointers one bye one
3875 */
3876 btrfs_release_path(root, path);
3877 ret = btrfs_drop_extents(trans, root, inode, key.offset,
3878 key.offset + num_bytes,
3879 key.offset, &alloc_hint);
3880 BUG_ON(ret);
3881
3882 for (i = 0; i < nr_extents; i++) {
3883 if (ext_offset >= new_extents[i].num_bytes) {
3884 ext_offset -= new_extents[i].num_bytes;
3885 continue;
3886 }
3887 extent_len = min(new_extents[i].num_bytes -
3888 ext_offset, num_bytes);
3889
3890 ret = btrfs_insert_empty_item(trans, root,
3891 path, &key,
3892 sizeof(*fi));
3893 BUG_ON(ret);
3894
3895 leaf = path->nodes[0];
3896 fi = btrfs_item_ptr(leaf, path->slots[0],
3897 struct btrfs_file_extent_item);
3898 btrfs_set_file_extent_generation(leaf, fi,
3899 trans->transid);
3900 btrfs_set_file_extent_type(leaf, fi,
3901 BTRFS_FILE_EXTENT_REG);
3902 btrfs_set_file_extent_disk_bytenr(leaf, fi,
3903 new_extents[i].disk_bytenr);
3904 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
3905 new_extents[i].disk_num_bytes);
3906 btrfs_set_file_extent_num_bytes(leaf, fi,
3907 extent_len);
3908 ext_offset += new_extents[i].offset;
3909 btrfs_set_file_extent_offset(leaf, fi,
3910 ext_offset);
3911 btrfs_mark_buffer_dirty(leaf);
3912
3913 btrfs_drop_extent_cache(inode, key.offset,
3914 key.offset + extent_len - 1, 0);
3915
3916 ret = btrfs_inc_extent_ref(trans, root,
3917 new_extents[i].disk_bytenr,
3918 new_extents[i].disk_num_bytes,
3919 leaf->start,
3920 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003921 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04003922 BUG_ON(ret);
3923 btrfs_release_path(root, path);
3924
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003925 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04003926
3927 ext_offset = 0;
3928 num_bytes -= extent_len;
3929 key.offset += extent_len;
3930
3931 if (num_bytes == 0)
3932 break;
3933 }
3934 BUG_ON(i >= nr_extents);
3935 }
3936
3937 if (extent_locked) {
3938 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
3939 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3940 lock_end, GFP_NOFS);
3941 extent_locked = 0;
3942 }
3943skip:
3944 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
3945 key.offset >= first_pos + extent_key->offset)
3946 break;
3947
3948 cond_resched();
3949 }
3950 ret = 0;
3951out:
3952 btrfs_release_path(root, path);
3953 if (inode) {
3954 mutex_unlock(&inode->i_mutex);
3955 if (extent_locked) {
3956 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
3957 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3958 lock_end, GFP_NOFS);
3959 }
3960 iput(inode);
3961 }
3962 return ret;
3963}
3964
3965int btrfs_add_reloc_mapping(struct btrfs_root *root, u64 orig_bytenr,
3966 u64 num_bytes, u64 new_bytenr)
3967{
3968 set_extent_bits(&root->fs_info->reloc_mapping_tree,
3969 orig_bytenr, orig_bytenr + num_bytes - 1,
3970 EXTENT_LOCKED, GFP_NOFS);
3971 set_state_private(&root->fs_info->reloc_mapping_tree,
3972 orig_bytenr, new_bytenr);
3973 return 0;
3974}
3975
3976int btrfs_get_reloc_mapping(struct btrfs_root *root, u64 orig_bytenr,
3977 u64 num_bytes, u64 *new_bytenr)
3978{
3979 u64 bytenr;
3980 u64 cur_bytenr = orig_bytenr;
3981 u64 prev_bytenr = orig_bytenr;
3982 int ret;
3983
3984 while (1) {
3985 ret = get_state_private(&root->fs_info->reloc_mapping_tree,
3986 cur_bytenr, &bytenr);
3987 if (ret)
3988 break;
3989 prev_bytenr = cur_bytenr;
3990 cur_bytenr = bytenr;
3991 }
3992
3993 if (orig_bytenr == cur_bytenr)
3994 return -ENOENT;
3995
3996 if (prev_bytenr != orig_bytenr) {
3997 set_state_private(&root->fs_info->reloc_mapping_tree,
3998 orig_bytenr, cur_bytenr);
3999 }
4000 *new_bytenr = cur_bytenr;
4001 return 0;
4002}
4003
4004void btrfs_free_reloc_mappings(struct btrfs_root *root)
4005{
4006 clear_extent_bits(&root->fs_info->reloc_mapping_tree,
4007 0, (u64)-1, -1, GFP_NOFS);
4008}
4009
4010int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4011 struct btrfs_root *root,
4012 struct extent_buffer *buf, u64 orig_start)
4013{
4014 int level;
4015 int ret;
4016
4017 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4018 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4019
4020 level = btrfs_header_level(buf);
4021 if (level == 0) {
4022 struct btrfs_leaf_ref *ref;
4023 struct btrfs_leaf_ref *orig_ref;
4024
4025 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4026 if (!orig_ref)
4027 return -ENOENT;
4028
4029 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4030 if (!ref) {
4031 btrfs_free_leaf_ref(root, orig_ref);
4032 return -ENOMEM;
4033 }
4034
4035 ref->nritems = orig_ref->nritems;
4036 memcpy(ref->extents, orig_ref->extents,
4037 sizeof(ref->extents[0]) * ref->nritems);
4038
4039 btrfs_free_leaf_ref(root, orig_ref);
4040
4041 ref->root_gen = trans->transid;
4042 ref->bytenr = buf->start;
4043 ref->owner = btrfs_header_owner(buf);
4044 ref->generation = btrfs_header_generation(buf);
4045 ret = btrfs_add_leaf_ref(root, ref, 0);
4046 WARN_ON(ret);
4047 btrfs_free_leaf_ref(root, ref);
4048 }
4049 return 0;
4050}
4051
4052static int noinline invalidate_extent_cache(struct btrfs_root *root,
4053 struct extent_buffer *leaf,
4054 struct btrfs_block_group_cache *group,
4055 struct btrfs_root *target_root)
4056{
4057 struct btrfs_key key;
4058 struct inode *inode = NULL;
4059 struct btrfs_file_extent_item *fi;
4060 u64 num_bytes;
4061 u64 skip_objectid = 0;
4062 u32 nritems;
4063 u32 i;
4064
4065 nritems = btrfs_header_nritems(leaf);
4066 for (i = 0; i < nritems; i++) {
4067 btrfs_item_key_to_cpu(leaf, &key, i);
4068 if (key.objectid == skip_objectid ||
4069 key.type != BTRFS_EXTENT_DATA_KEY)
4070 continue;
4071 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4072 if (btrfs_file_extent_type(leaf, fi) ==
4073 BTRFS_FILE_EXTENT_INLINE)
4074 continue;
4075 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4076 continue;
4077 if (!inode || inode->i_ino != key.objectid) {
4078 iput(inode);
4079 inode = btrfs_ilookup(target_root->fs_info->sb,
4080 key.objectid, target_root, 1);
4081 }
4082 if (!inode) {
4083 skip_objectid = key.objectid;
4084 continue;
4085 }
4086 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4087
4088 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4089 key.offset + num_bytes - 1, GFP_NOFS);
4090 mutex_lock(&BTRFS_I(inode)->extent_mutex);
4091 btrfs_drop_extent_cache(inode, key.offset,
4092 key.offset + num_bytes - 1, 1);
4093 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
4094 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4095 key.offset + num_bytes - 1, GFP_NOFS);
4096 cond_resched();
4097 }
4098 iput(inode);
4099 return 0;
4100}
4101
4102static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4103 struct btrfs_root *root,
4104 struct extent_buffer *leaf,
4105 struct btrfs_block_group_cache *group,
4106 struct inode *reloc_inode)
4107{
4108 struct btrfs_key key;
4109 struct btrfs_key extent_key;
4110 struct btrfs_file_extent_item *fi;
4111 struct btrfs_leaf_ref *ref;
4112 struct disk_extent *new_extent;
4113 u64 bytenr;
4114 u64 num_bytes;
4115 u32 nritems;
4116 u32 i;
4117 int ext_index;
4118 int nr_extent;
4119 int ret;
4120
4121 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4122 BUG_ON(!new_extent);
4123
4124 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4125 BUG_ON(!ref);
4126
4127 ext_index = -1;
4128 nritems = btrfs_header_nritems(leaf);
4129 for (i = 0; i < nritems; i++) {
4130 btrfs_item_key_to_cpu(leaf, &key, i);
4131 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4132 continue;
4133 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4134 if (btrfs_file_extent_type(leaf, fi) ==
4135 BTRFS_FILE_EXTENT_INLINE)
4136 continue;
4137 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4138 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4139 if (bytenr == 0)
4140 continue;
4141
4142 ext_index++;
4143 if (bytenr >= group->key.objectid + group->key.offset ||
4144 bytenr + num_bytes <= group->key.objectid)
4145 continue;
4146
4147 extent_key.objectid = bytenr;
4148 extent_key.offset = num_bytes;
4149 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4150 nr_extent = 1;
4151 ret = get_new_locations(reloc_inode, &extent_key,
4152 group->key.objectid, 1,
4153 &new_extent, &nr_extent);
4154 if (ret > 0)
4155 continue;
4156 BUG_ON(ret < 0);
4157
4158 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4159 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4160 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4161 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4162
4163 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
4164 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4165 new_extent->disk_bytenr);
4166 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4167 new_extent->disk_num_bytes);
4168 new_extent->offset += btrfs_file_extent_offset(leaf, fi);
4169 btrfs_set_file_extent_offset(leaf, fi, new_extent->offset);
4170 btrfs_mark_buffer_dirty(leaf);
4171
4172 ret = btrfs_inc_extent_ref(trans, root,
4173 new_extent->disk_bytenr,
4174 new_extent->disk_num_bytes,
4175 leaf->start,
4176 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004177 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004178 BUG_ON(ret);
4179 ret = btrfs_free_extent(trans, root,
4180 bytenr, num_bytes, leaf->start,
4181 btrfs_header_owner(leaf),
4182 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004183 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004184 BUG_ON(ret);
4185 cond_resched();
4186 }
4187 kfree(new_extent);
4188 BUG_ON(ext_index + 1 != ref->nritems);
4189 btrfs_free_leaf_ref(root, ref);
4190 return 0;
4191}
4192
4193int btrfs_free_reloc_root(struct btrfs_root *root)
4194{
4195 struct btrfs_root *reloc_root;
4196
4197 if (root->reloc_root) {
4198 reloc_root = root->reloc_root;
4199 root->reloc_root = NULL;
4200 list_add(&reloc_root->dead_list,
4201 &root->fs_info->dead_reloc_roots);
4202 }
4203 return 0;
4204}
4205
4206int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4207{
4208 struct btrfs_trans_handle *trans;
4209 struct btrfs_root *reloc_root;
4210 struct btrfs_root *prev_root = NULL;
4211 struct list_head dead_roots;
4212 int ret;
4213 unsigned long nr;
4214
4215 INIT_LIST_HEAD(&dead_roots);
4216 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4217
4218 while (!list_empty(&dead_roots)) {
4219 reloc_root = list_entry(dead_roots.prev,
4220 struct btrfs_root, dead_list);
4221 list_del_init(&reloc_root->dead_list);
4222
4223 BUG_ON(reloc_root->commit_root != NULL);
4224 while (1) {
4225 trans = btrfs_join_transaction(root, 1);
4226 BUG_ON(!trans);
4227
4228 mutex_lock(&root->fs_info->drop_mutex);
4229 ret = btrfs_drop_snapshot(trans, reloc_root);
4230 if (ret != -EAGAIN)
4231 break;
4232 mutex_unlock(&root->fs_info->drop_mutex);
4233
4234 nr = trans->blocks_used;
4235 ret = btrfs_end_transaction(trans, root);
4236 BUG_ON(ret);
4237 btrfs_btree_balance_dirty(root, nr);
4238 }
4239
4240 free_extent_buffer(reloc_root->node);
4241
4242 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4243 &reloc_root->root_key);
4244 BUG_ON(ret);
4245 mutex_unlock(&root->fs_info->drop_mutex);
4246
4247 nr = trans->blocks_used;
4248 ret = btrfs_end_transaction(trans, root);
4249 BUG_ON(ret);
4250 btrfs_btree_balance_dirty(root, nr);
4251
4252 kfree(prev_root);
4253 prev_root = reloc_root;
4254 }
4255 if (prev_root) {
4256 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4257 kfree(prev_root);
4258 }
4259 return 0;
4260}
4261
4262int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4263{
4264 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4265 return 0;
4266}
4267
4268int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4269{
4270 struct btrfs_root *reloc_root;
4271 struct btrfs_trans_handle *trans;
4272 struct btrfs_key location;
4273 int found;
4274 int ret;
4275
4276 mutex_lock(&root->fs_info->tree_reloc_mutex);
4277 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4278 BUG_ON(ret);
4279 found = !list_empty(&root->fs_info->dead_reloc_roots);
4280 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4281
4282 if (found) {
4283 trans = btrfs_start_transaction(root, 1);
4284 BUG_ON(!trans);
4285 ret = btrfs_commit_transaction(trans, root);
4286 BUG_ON(ret);
4287 }
4288
4289 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4290 location.offset = (u64)-1;
4291 location.type = BTRFS_ROOT_ITEM_KEY;
4292
4293 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
4294 BUG_ON(!reloc_root);
4295 btrfs_orphan_cleanup(reloc_root);
4296 return 0;
4297}
4298
4299static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
4300 struct btrfs_root *root)
4301{
4302 struct btrfs_root *reloc_root;
4303 struct extent_buffer *eb;
4304 struct btrfs_root_item *root_item;
4305 struct btrfs_key root_key;
4306 int ret;
4307
4308 BUG_ON(!root->ref_cows);
4309 if (root->reloc_root)
4310 return 0;
4311
4312 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
4313 BUG_ON(!root_item);
4314
4315 ret = btrfs_copy_root(trans, root, root->commit_root,
4316 &eb, BTRFS_TREE_RELOC_OBJECTID);
4317 BUG_ON(ret);
4318
4319 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4320 root_key.offset = root->root_key.objectid;
4321 root_key.type = BTRFS_ROOT_ITEM_KEY;
4322
4323 memcpy(root_item, &root->root_item, sizeof(root_item));
4324 btrfs_set_root_refs(root_item, 0);
4325 btrfs_set_root_bytenr(root_item, eb->start);
4326 btrfs_set_root_level(root_item, btrfs_header_level(eb));
4327 memset(&root_item->drop_progress, 0, sizeof(root_item->drop_progress));
4328 root_item->drop_level = 0;
4329
4330 btrfs_tree_unlock(eb);
4331 free_extent_buffer(eb);
4332
4333 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
4334 &root_key, root_item);
4335 BUG_ON(ret);
4336 kfree(root_item);
4337
4338 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
4339 &root_key);
4340 BUG_ON(!reloc_root);
4341 reloc_root->last_trans = trans->transid;
4342 reloc_root->commit_root = NULL;
4343 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
4344
4345 root->reloc_root = reloc_root;
4346 return 0;
4347}
4348
4349/*
4350 * Core function of space balance.
4351 *
4352 * The idea is using reloc trees to relocate tree blocks in reference
4353 * counted roots. There is one reloc tree for each subvol, all reloc
4354 * trees share same key objectid. Reloc trees are snapshots of the
4355 * latest committed roots (subvol root->commit_root). To relocate a tree
4356 * block referenced by a subvol, the code COW the block through the reloc
4357 * tree, then update pointer in the subvol to point to the new block.
4358 * Since all reloc trees share same key objectid, we can easily do special
4359 * handing to share tree blocks between reloc trees. Once a tree block has
4360 * been COWed in one reloc tree, we can use the result when the same block
4361 * is COWed again through other reloc trees.
4362 */
4363static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
4364 struct btrfs_root *root,
4365 struct btrfs_path *path,
4366 struct btrfs_key *first_key,
4367 struct btrfs_ref_path *ref_path,
4368 struct btrfs_block_group_cache *group,
4369 struct inode *reloc_inode)
4370{
4371 struct btrfs_root *reloc_root;
4372 struct extent_buffer *eb = NULL;
4373 struct btrfs_key *keys;
4374 u64 *nodes;
4375 int level;
4376 int lowest_merge;
4377 int lowest_level = 0;
4378 int update_refs;
4379 int ret;
4380
4381 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
4382 lowest_level = ref_path->owner_objectid;
4383
4384 if (is_cowonly_root(ref_path->root_objectid)) {
4385 path->lowest_level = lowest_level;
4386 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
4387 BUG_ON(ret < 0);
4388 path->lowest_level = 0;
4389 btrfs_release_path(root, path);
4390 return 0;
4391 }
4392
4393 keys = kzalloc(sizeof(*keys) * BTRFS_MAX_LEVEL, GFP_NOFS);
4394 BUG_ON(!keys);
4395 nodes = kzalloc(sizeof(*nodes) * BTRFS_MAX_LEVEL, GFP_NOFS);
4396 BUG_ON(!nodes);
4397
4398 mutex_lock(&root->fs_info->tree_reloc_mutex);
4399 ret = init_reloc_tree(trans, root);
4400 BUG_ON(ret);
4401 reloc_root = root->reloc_root;
4402
4403 path->lowest_level = lowest_level;
4404 ret = btrfs_search_slot(trans, reloc_root, first_key, path, 0, 0);
4405 BUG_ON(ret);
4406 /*
4407 * get relocation mapping for tree blocks in the path
4408 */
4409 lowest_merge = BTRFS_MAX_LEVEL;
4410 for (level = BTRFS_MAX_LEVEL - 1; level >= lowest_level; level--) {
4411 u64 new_bytenr;
4412 eb = path->nodes[level];
4413 if (!eb || eb == reloc_root->node)
4414 continue;
4415 ret = btrfs_get_reloc_mapping(reloc_root, eb->start, eb->len,
4416 &new_bytenr);
4417 if (ret)
4418 continue;
4419 if (level == 0)
4420 btrfs_item_key_to_cpu(eb, &keys[level], 0);
4421 else
4422 btrfs_node_key_to_cpu(eb, &keys[level], 0);
4423 nodes[level] = new_bytenr;
4424 lowest_merge = level;
4425 }
4426
4427 update_refs = 0;
4428 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4429 eb = path->nodes[0];
4430 if (btrfs_header_generation(eb) < trans->transid)
4431 update_refs = 1;
4432 }
4433
4434 btrfs_release_path(reloc_root, path);
4435 /*
4436 * merge tree blocks that already relocated in other reloc trees
4437 */
4438 if (lowest_merge != BTRFS_MAX_LEVEL) {
4439 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
4440 lowest_merge);
4441 BUG_ON(ret < 0);
4442 }
4443 /*
4444 * cow any tree blocks that still haven't been relocated
4445 */
4446 ret = btrfs_search_slot(trans, reloc_root, first_key, path, 0, 1);
4447 BUG_ON(ret);
4448 /*
4449 * if we are relocating data block group, update extent pointers
4450 * in the newly created tree leaf.
4451 */
4452 eb = path->nodes[0];
4453 if (update_refs && nodes[0] != eb->start) {
4454 ret = replace_extents_in_leaf(trans, reloc_root, eb, group,
4455 reloc_inode);
4456 BUG_ON(ret);
4457 }
4458
4459 memset(keys, 0, sizeof(*keys) * BTRFS_MAX_LEVEL);
4460 memset(nodes, 0, sizeof(*nodes) * BTRFS_MAX_LEVEL);
4461 for (level = BTRFS_MAX_LEVEL - 1; level >= lowest_level; level--) {
4462 eb = path->nodes[level];
4463 if (!eb || eb == reloc_root->node)
4464 continue;
4465 BUG_ON(btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID);
4466 nodes[level] = eb->start;
4467 if (level == 0)
4468 btrfs_item_key_to_cpu(eb, &keys[level], 0);
4469 else
4470 btrfs_node_key_to_cpu(eb, &keys[level], 0);
4471 }
4472
4473 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4474 eb = path->nodes[0];
4475 extent_buffer_get(eb);
4476 }
4477 btrfs_release_path(reloc_root, path);
4478 /*
4479 * replace tree blocks in the fs tree with tree blocks in
4480 * the reloc tree.
4481 */
4482 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
4483 BUG_ON(ret < 0);
4484
4485 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4486 ret = invalidate_extent_cache(reloc_root, eb, group, root);
4487 BUG_ON(ret);
4488 free_extent_buffer(eb);
4489 }
4490 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4491
4492 path->lowest_level = 0;
4493 kfree(nodes);
4494 kfree(keys);
4495 return 0;
4496}
4497
4498static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
4499 struct btrfs_root *root,
4500 struct btrfs_path *path,
4501 struct btrfs_key *first_key,
4502 struct btrfs_ref_path *ref_path)
4503{
4504 int ret;
4505 int needs_lock = 0;
4506
4507 if (root == root->fs_info->extent_root ||
4508 root == root->fs_info->chunk_root ||
4509 root == root->fs_info->dev_root) {
4510 needs_lock = 1;
4511 mutex_lock(&root->fs_info->alloc_mutex);
4512 }
4513
4514 ret = relocate_one_path(trans, root, path, first_key,
4515 ref_path, NULL, NULL);
4516 BUG_ON(ret);
4517
4518 if (root == root->fs_info->extent_root)
4519 btrfs_extent_post_op(trans, root);
4520 if (needs_lock)
4521 mutex_unlock(&root->fs_info->alloc_mutex);
4522
4523 return 0;
4524}
4525
4526static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
4527 struct btrfs_root *extent_root,
4528 struct btrfs_path *path,
4529 struct btrfs_key *extent_key)
4530{
4531 int ret;
4532
4533 mutex_lock(&extent_root->fs_info->alloc_mutex);
4534 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
4535 if (ret)
4536 goto out;
4537 ret = btrfs_del_item(trans, extent_root, path);
4538out:
Chris Masonedbd8d42007-12-21 16:27:24 -05004539 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004540 mutex_unlock(&extent_root->fs_info->alloc_mutex);
4541 return ret;
4542}
4543
4544static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
4545 struct btrfs_ref_path *ref_path)
4546{
4547 struct btrfs_key root_key;
4548
4549 root_key.objectid = ref_path->root_objectid;
4550 root_key.type = BTRFS_ROOT_ITEM_KEY;
4551 if (is_cowonly_root(ref_path->root_objectid))
4552 root_key.offset = 0;
4553 else
4554 root_key.offset = (u64)-1;
4555
4556 return btrfs_read_fs_root_no_name(fs_info, &root_key);
4557}
4558
4559static int noinline relocate_one_extent(struct btrfs_root *extent_root,
4560 struct btrfs_path *path,
4561 struct btrfs_key *extent_key,
4562 struct btrfs_block_group_cache *group,
4563 struct inode *reloc_inode, int pass)
4564{
4565 struct btrfs_trans_handle *trans;
4566 struct btrfs_root *found_root;
4567 struct btrfs_ref_path *ref_path = NULL;
4568 struct disk_extent *new_extents = NULL;
4569 int nr_extents = 0;
4570 int loops;
4571 int ret;
4572 int level;
4573 struct btrfs_key first_key;
4574 u64 prev_block = 0;
4575
4576 mutex_unlock(&extent_root->fs_info->alloc_mutex);
4577
4578 trans = btrfs_start_transaction(extent_root, 1);
4579 BUG_ON(!trans);
4580
4581 if (extent_key->objectid == 0) {
4582 ret = del_extent_zero(trans, extent_root, path, extent_key);
4583 goto out;
4584 }
4585
4586 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
4587 if (!ref_path) {
4588 ret = -ENOMEM;
4589 goto out;
4590 }
4591
4592 for (loops = 0; ; loops++) {
4593 if (loops == 0) {
4594 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
4595 extent_key->objectid);
4596 } else {
4597 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
4598 }
4599 if (ret < 0)
4600 goto out;
4601 if (ret > 0)
4602 break;
4603
4604 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4605 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
4606 continue;
4607
4608 found_root = read_ref_root(extent_root->fs_info, ref_path);
4609 BUG_ON(!found_root);
4610 /*
4611 * for reference counted tree, only process reference paths
4612 * rooted at the latest committed root.
4613 */
4614 if (found_root->ref_cows &&
4615 ref_path->root_generation != found_root->root_key.offset)
4616 continue;
4617
4618 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4619 if (pass == 0) {
4620 /*
4621 * copy data extents to new locations
4622 */
4623 u64 group_start = group->key.objectid;
4624 ret = relocate_data_extent(reloc_inode,
4625 extent_key,
4626 group_start);
4627 if (ret < 0)
4628 goto out;
4629 break;
4630 }
4631 level = 0;
4632 } else {
4633 level = ref_path->owner_objectid;
4634 }
4635
4636 if (prev_block != ref_path->nodes[level]) {
4637 struct extent_buffer *eb;
4638 u64 block_start = ref_path->nodes[level];
4639 u64 block_size = btrfs_level_size(found_root, level);
4640
4641 eb = read_tree_block(found_root, block_start,
4642 block_size, 0);
4643 btrfs_tree_lock(eb);
4644 BUG_ON(level != btrfs_header_level(eb));
4645
4646 if (level == 0)
4647 btrfs_item_key_to_cpu(eb, &first_key, 0);
4648 else
4649 btrfs_node_key_to_cpu(eb, &first_key, 0);
4650
4651 btrfs_tree_unlock(eb);
4652 free_extent_buffer(eb);
4653 prev_block = block_start;
4654 }
4655
4656 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
4657 pass >= 2) {
4658 /*
4659 * use fallback method to process the remaining
4660 * references.
4661 */
4662 if (!new_extents) {
4663 u64 group_start = group->key.objectid;
4664 ret = get_new_locations(reloc_inode,
4665 extent_key,
4666 group_start, 0,
4667 &new_extents,
4668 &nr_extents);
4669 if (ret < 0)
4670 goto out;
4671 }
4672 btrfs_record_root_in_trans(found_root);
4673 ret = replace_one_extent(trans, found_root,
4674 path, extent_key,
4675 &first_key, ref_path,
4676 new_extents, nr_extents);
4677 if (ret < 0)
4678 goto out;
4679 continue;
4680 }
4681
4682 btrfs_record_root_in_trans(found_root);
4683 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4684 ret = relocate_tree_block(trans, found_root, path,
4685 &first_key, ref_path);
4686 } else {
4687 /*
4688 * try to update data extent references while
4689 * keeping metadata shared between snapshots.
4690 */
4691 ret = relocate_one_path(trans, found_root, path,
4692 &first_key, ref_path,
4693 group, reloc_inode);
4694 }
4695 if (ret < 0)
4696 goto out;
4697 }
4698 ret = 0;
4699out:
4700 btrfs_end_transaction(trans, extent_root);
4701 kfree(new_extents);
4702 kfree(ref_path);
4703 mutex_lock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05004704 return ret;
4705}
4706
Chris Masonec44a352008-04-28 15:29:52 -04004707static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
4708{
4709 u64 num_devices;
4710 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
4711 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
4712
Chris Masona061fc82008-05-07 11:43:44 -04004713 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04004714 if (num_devices == 1) {
4715 stripped |= BTRFS_BLOCK_GROUP_DUP;
4716 stripped = flags & ~stripped;
4717
4718 /* turn raid0 into single device chunks */
4719 if (flags & BTRFS_BLOCK_GROUP_RAID0)
4720 return stripped;
4721
4722 /* turn mirroring into duplication */
4723 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
4724 BTRFS_BLOCK_GROUP_RAID10))
4725 return stripped | BTRFS_BLOCK_GROUP_DUP;
4726 return flags;
4727 } else {
4728 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04004729 if (flags & stripped)
4730 return flags;
4731
4732 stripped |= BTRFS_BLOCK_GROUP_DUP;
4733 stripped = flags & ~stripped;
4734
4735 /* switch duplicated blocks with raid1 */
4736 if (flags & BTRFS_BLOCK_GROUP_DUP)
4737 return stripped | BTRFS_BLOCK_GROUP_RAID1;
4738
4739 /* turn single device chunks into raid0 */
4740 return stripped | BTRFS_BLOCK_GROUP_RAID0;
4741 }
4742 return flags;
4743}
4744
Chris Mason0ef3e662008-05-24 14:04:53 -04004745int __alloc_chunk_for_shrink(struct btrfs_root *root,
4746 struct btrfs_block_group_cache *shrink_block_group,
4747 int force)
4748{
4749 struct btrfs_trans_handle *trans;
4750 u64 new_alloc_flags;
4751 u64 calc;
4752
Chris Masonc286ac42008-07-22 23:06:41 -04004753 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04004754 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04004755 spin_unlock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04004756 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04004757
Chris Mason0ef3e662008-05-24 14:04:53 -04004758 trans = btrfs_start_transaction(root, 1);
Chris Mason7d9eb122008-07-08 14:19:17 -04004759 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04004760 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04004761
Chris Mason0ef3e662008-05-24 14:04:53 -04004762 new_alloc_flags = update_block_group_flags(root,
4763 shrink_block_group->flags);
4764 if (new_alloc_flags != shrink_block_group->flags) {
4765 calc =
4766 btrfs_block_group_used(&shrink_block_group->item);
4767 } else {
4768 calc = shrink_block_group->key.offset;
4769 }
Chris Masonc286ac42008-07-22 23:06:41 -04004770 spin_unlock(&shrink_block_group->lock);
4771
Chris Mason0ef3e662008-05-24 14:04:53 -04004772 do_chunk_alloc(trans, root->fs_info->extent_root,
4773 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04004774
4775 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0ef3e662008-05-24 14:04:53 -04004776 btrfs_end_transaction(trans, root);
Chris Mason7d9eb122008-07-08 14:19:17 -04004777 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04004778 } else
4779 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04004780 return 0;
4781}
4782
Zheng Yan1a40e232008-09-26 10:09:34 -04004783static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
4784 struct btrfs_root *root,
4785 u64 objectid, u64 size)
4786{
4787 struct btrfs_path *path;
4788 struct btrfs_inode_item *item;
4789 struct extent_buffer *leaf;
4790 int ret;
4791
4792 path = btrfs_alloc_path();
4793 if (!path)
4794 return -ENOMEM;
4795
4796 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
4797 if (ret)
4798 goto out;
4799
4800 leaf = path->nodes[0];
4801 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
4802 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
4803 btrfs_set_inode_generation(leaf, item, 1);
4804 btrfs_set_inode_size(leaf, item, size);
4805 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
4806 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM);
4807 btrfs_mark_buffer_dirty(leaf);
4808 btrfs_release_path(root, path);
4809out:
4810 btrfs_free_path(path);
4811 return ret;
4812}
4813
4814static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
4815 struct btrfs_block_group_cache *group)
4816{
4817 struct inode *inode = NULL;
4818 struct btrfs_trans_handle *trans;
4819 struct btrfs_root *root;
4820 struct btrfs_key root_key;
4821 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
4822 int err = 0;
4823
4824 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4825 root_key.type = BTRFS_ROOT_ITEM_KEY;
4826 root_key.offset = (u64)-1;
4827 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
4828 if (IS_ERR(root))
4829 return ERR_CAST(root);
4830
4831 trans = btrfs_start_transaction(root, 1);
4832 BUG_ON(!trans);
4833
4834 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
4835 if (err)
4836 goto out;
4837
4838 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
4839 BUG_ON(err);
4840
4841 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
4842 group->key.offset, 0);
4843 BUG_ON(err);
4844
4845 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
4846 if (inode->i_state & I_NEW) {
4847 BTRFS_I(inode)->root = root;
4848 BTRFS_I(inode)->location.objectid = objectid;
4849 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
4850 BTRFS_I(inode)->location.offset = 0;
4851 btrfs_read_locked_inode(inode);
4852 unlock_new_inode(inode);
4853 BUG_ON(is_bad_inode(inode));
4854 } else {
4855 BUG_ON(1);
4856 }
4857
4858 err = btrfs_orphan_add(trans, inode);
4859out:
4860 btrfs_end_transaction(trans, root);
4861 if (err) {
4862 if (inode)
4863 iput(inode);
4864 inode = ERR_PTR(err);
4865 }
4866 return inode;
4867}
4868
4869int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05004870{
4871 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05004872 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04004873 struct btrfs_fs_info *info = root->fs_info;
4874 struct extent_buffer *leaf;
4875 struct inode *reloc_inode;
4876 struct btrfs_block_group_cache *block_group;
4877 struct btrfs_key key;
Chris Masonedbd8d42007-12-21 16:27:24 -05004878 u64 cur_byte;
4879 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05004880 u32 nritems;
4881 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04004882 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04004883 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05004884
Chris Masonedbd8d42007-12-21 16:27:24 -05004885 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04004886
4887 block_group = btrfs_lookup_block_group(info, group_start);
4888 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05004889
Chris Mason323da792008-05-09 11:46:48 -04004890 printk("btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04004891 (unsigned long long)block_group->key.objectid,
4892 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04004893
Zheng Yan1a40e232008-09-26 10:09:34 -04004894 path = btrfs_alloc_path();
4895 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04004896
Zheng Yan1a40e232008-09-26 10:09:34 -04004897 reloc_inode = create_reloc_inode(info, block_group);
4898 BUG_ON(IS_ERR(reloc_inode));
4899
4900 mutex_lock(&root->fs_info->alloc_mutex);
4901
4902 __alloc_chunk_for_shrink(root, block_group, 1);
4903 block_group->ro = 1;
4904 block_group->space_info->total_bytes -= block_group->key.offset;
4905
4906 mutex_unlock(&root->fs_info->alloc_mutex);
4907
4908 btrfs_start_delalloc_inodes(info->tree_root);
4909 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04004910again:
Chris Masonedbd8d42007-12-21 16:27:24 -05004911 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04004912 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004913 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05004914 key.offset = 0;
4915 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05004916 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05004917
Zheng Yan1a40e232008-09-26 10:09:34 -04004918 trans = btrfs_start_transaction(info->tree_root, 1);
4919 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04004920
Zheng Yan1a40e232008-09-26 10:09:34 -04004921 mutex_lock(&root->fs_info->cleaner_mutex);
4922 btrfs_clean_old_snapshots(info->tree_root);
4923 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
4924 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04004925
4926 mutex_lock(&root->fs_info->alloc_mutex);
4927
Yan73e48b22008-01-03 14:14:39 -05004928 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05004929 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4930 if (ret < 0)
4931 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04004932next:
Chris Masonedbd8d42007-12-21 16:27:24 -05004933 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05004934 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05004935 if (path->slots[0] >= nritems) {
4936 ret = btrfs_next_leaf(root, path);
4937 if (ret < 0)
4938 goto out;
4939 if (ret == 1) {
4940 ret = 0;
4941 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004942 }
Yan73e48b22008-01-03 14:14:39 -05004943 leaf = path->nodes[0];
4944 nritems = btrfs_header_nritems(leaf);
4945 }
4946
Zheng Yan1a40e232008-09-26 10:09:34 -04004947 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05004948
Zheng Yan1a40e232008-09-26 10:09:34 -04004949 if (key.objectid >= block_group->key.objectid +
4950 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04004951 break;
4952
Chris Mason725c8462008-01-04 16:47:16 -05004953 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05004954 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004955 mutex_unlock(&root->fs_info->alloc_mutex);
4956 cond_resched();
4957 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason725c8462008-01-04 16:47:16 -05004958 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004959 continue;
Chris Mason725c8462008-01-04 16:47:16 -05004960 }
4961 progress = 1;
4962
Zheng Yan1a40e232008-09-26 10:09:34 -04004963 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
4964 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05004965 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05004966 goto next;
4967 }
Yan73e48b22008-01-03 14:14:39 -05004968
Chris Masonedbd8d42007-12-21 16:27:24 -05004969 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04004970 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05004971 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004972
4973 __alloc_chunk_for_shrink(root, block_group, 0);
4974 ret = relocate_one_extent(root, path, &key, block_group,
4975 reloc_inode, pass);
4976 BUG_ON(ret < 0);
4977
4978 key.objectid = cur_byte;
4979 key.type = 0;
4980 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05004981 }
4982
4983 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004984 mutex_unlock(&root->fs_info->alloc_mutex);
4985
4986 if (pass == 0) {
4987 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
4988 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
4989 WARN_ON(reloc_inode->i_mapping->nrpages);
4990 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004991
4992 if (total_found > 0) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004993 printk("btrfs found %llu extents in pass %d\n",
4994 (unsigned long long)total_found, pass);
4995 pass++;
Chris Masonedbd8d42007-12-21 16:27:24 -05004996 goto again;
4997 }
4998
Zheng Yan1a40e232008-09-26 10:09:34 -04004999 /* delete reloc_inode */
5000 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005001
Zheng Yan1a40e232008-09-26 10:09:34 -04005002 /* unpin extents in this range */
5003 trans = btrfs_start_transaction(info->tree_root, 1);
5004 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005005
Chris Mason7d9eb122008-07-08 14:19:17 -04005006 mutex_lock(&root->fs_info->alloc_mutex);
5007
Zheng Yan1a40e232008-09-26 10:09:34 -04005008 spin_lock(&block_group->lock);
5009 WARN_ON(block_group->pinned > 0);
5010 WARN_ON(block_group->reserved > 0);
5011 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5012 spin_unlock(&block_group->lock);
5013 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005014out:
Chris Mason925baed2008-06-25 16:01:30 -04005015 mutex_unlock(&root->fs_info->alloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005016 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005017 return ret;
5018}
5019
Chris Mason0b86a832008-03-24 15:01:56 -04005020int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
5021 struct btrfs_key *key)
5022{
Chris Mason925baed2008-06-25 16:01:30 -04005023 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005024 struct btrfs_key found_key;
5025 struct extent_buffer *leaf;
5026 int slot;
5027
5028 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5029 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005030 goto out;
5031
Chris Mason0b86a832008-03-24 15:01:56 -04005032 while(1) {
5033 slot = path->slots[0];
5034 leaf = path->nodes[0];
5035 if (slot >= btrfs_header_nritems(leaf)) {
5036 ret = btrfs_next_leaf(root, path);
5037 if (ret == 0)
5038 continue;
5039 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005040 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005041 break;
5042 }
5043 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5044
5045 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005046 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5047 ret = 0;
5048 goto out;
5049 }
Chris Mason0b86a832008-03-24 15:01:56 -04005050 path->slots[0]++;
5051 }
5052 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005053out:
Chris Mason0b86a832008-03-24 15:01:56 -04005054 return ret;
5055}
5056
Zheng Yan1a40e232008-09-26 10:09:34 -04005057int btrfs_free_block_groups(struct btrfs_fs_info *info)
5058{
5059 struct btrfs_block_group_cache *block_group;
5060 struct rb_node *n;
5061
5062 mutex_lock(&info->alloc_mutex);
5063 spin_lock(&info->block_group_cache_lock);
5064 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5065 block_group = rb_entry(n, struct btrfs_block_group_cache,
5066 cache_node);
5067
5068 spin_unlock(&info->block_group_cache_lock);
5069 btrfs_remove_free_space_cache(block_group);
5070 spin_lock(&info->block_group_cache_lock);
5071
5072 rb_erase(&block_group->cache_node,
5073 &info->block_group_cache_tree);
5074 spin_lock(&block_group->space_info->lock);
5075 list_del(&block_group->list);
5076 spin_unlock(&block_group->space_info->lock);
5077 kfree(block_group);
5078 }
5079 spin_unlock(&info->block_group_cache_lock);
5080 mutex_unlock(&info->alloc_mutex);
5081 return 0;
5082}
5083
Chris Mason9078a3e2007-04-26 16:46:15 -04005084int btrfs_read_block_groups(struct btrfs_root *root)
5085{
5086 struct btrfs_path *path;
5087 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005088 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005089 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005090 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005091 struct btrfs_key key;
5092 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005093 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005094
Chris Masonbe744172007-05-06 10:15:01 -04005095 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005096 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005097 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005098 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005099 path = btrfs_alloc_path();
5100 if (!path)
5101 return -ENOMEM;
5102
Chris Mason925baed2008-06-25 16:01:30 -04005103 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04005104 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005105 ret = find_first_block_group(root, path, &key);
5106 if (ret > 0) {
5107 ret = 0;
5108 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005109 }
Chris Mason0b86a832008-03-24 15:01:56 -04005110 if (ret != 0)
5111 goto error;
5112
Chris Mason5f39d392007-10-15 16:14:19 -04005113 leaf = path->nodes[0];
5114 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005115 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005116 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005117 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005118 break;
5119 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005120
Chris Masonc286ac42008-07-22 23:06:41 -04005121 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005122 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005123 read_extent_buffer(leaf, &cache->item,
5124 btrfs_item_ptr_offset(leaf, path->slots[0]),
5125 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005126 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005127
Chris Mason9078a3e2007-04-26 16:46:15 -04005128 key.objectid = found_key.objectid + found_key.offset;
5129 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005130 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005131
Chris Mason6324fbf2008-03-24 15:01:59 -04005132 ret = update_space_info(info, cache->flags, found_key.offset,
5133 btrfs_block_group_used(&cache->item),
5134 &space_info);
5135 BUG_ON(ret);
5136 cache->space_info = space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005137 spin_lock(&space_info->lock);
5138 list_add(&cache->list, &space_info->block_groups);
5139 spin_unlock(&space_info->lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04005140
Josef Bacik0f9dd462008-09-23 13:14:11 -04005141 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5142 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005143
5144 set_avail_alloc_bits(root->fs_info, cache->flags);
Chris Mason9078a3e2007-04-26 16:46:15 -04005145 }
Chris Mason0b86a832008-03-24 15:01:56 -04005146 ret = 0;
5147error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005148 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005149 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04005150 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005151}
Chris Mason6324fbf2008-03-24 15:01:59 -04005152
5153int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5154 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005155 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005156 u64 size)
5157{
5158 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005159 struct btrfs_root *extent_root;
5160 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005161
Chris Mason7d9eb122008-07-08 14:19:17 -04005162 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason6324fbf2008-03-24 15:01:59 -04005163 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005164
Chris Masone02119d2008-09-05 16:13:11 -04005165 root->fs_info->last_trans_new_blockgroup = trans->transid;
5166
Chris Mason8f18cf12008-04-25 16:53:30 -04005167 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005168 if (!cache)
5169 return -ENOMEM;
5170
Chris Masone17cade2008-04-15 15:41:47 -04005171 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005172 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04005173 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005174 INIT_LIST_HEAD(&cache->list);
Chris Mason6324fbf2008-03-24 15:01:59 -04005175 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04005176
Chris Mason6324fbf2008-03-24 15:01:59 -04005177 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005178 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5179 cache->flags = type;
5180 btrfs_set_block_group_flags(&cache->item, type);
5181
5182 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5183 &cache->space_info);
5184 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005185 spin_lock(&cache->space_info->lock);
5186 list_add(&cache->list, &cache->space_info->block_groups);
5187 spin_unlock(&cache->space_info->lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04005188
Josef Bacik0f9dd462008-09-23 13:14:11 -04005189 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5190 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005191
Chris Mason6324fbf2008-03-24 15:01:59 -04005192 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5193 sizeof(cache->item));
5194 BUG_ON(ret);
5195
5196 finish_current_insert(trans, extent_root);
5197 ret = del_pending_extents(trans, extent_root);
5198 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04005199 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005200
Chris Mason6324fbf2008-03-24 15:01:59 -04005201 return 0;
5202}
Zheng Yan1a40e232008-09-26 10:09:34 -04005203
5204int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5205 struct btrfs_root *root, u64 group_start)
5206{
5207 struct btrfs_path *path;
5208 struct btrfs_block_group_cache *block_group;
5209 struct btrfs_key key;
5210 int ret;
5211
5212 BUG_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
5213 root = root->fs_info->extent_root;
5214
5215 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5216 BUG_ON(!block_group);
5217
5218 memcpy(&key, &block_group->key, sizeof(key));
5219
5220 path = btrfs_alloc_path();
5221 BUG_ON(!path);
5222
5223 btrfs_remove_free_space_cache(block_group);
5224 rb_erase(&block_group->cache_node,
5225 &root->fs_info->block_group_cache_tree);
5226 spin_lock(&block_group->space_info->lock);
5227 list_del(&block_group->list);
5228 spin_unlock(&block_group->space_info->lock);
5229
5230 /*
5231 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5232 kfree(shrink_block_group);
5233 */
5234
5235 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5236 if (ret > 0)
5237 ret = -EIO;
5238 if (ret < 0)
5239 goto out;
5240
5241 ret = btrfs_del_item(trans, root, path);
5242out:
5243 btrfs_free_path(path);
5244 return ret;
5245}