blob: b0f2241274fdc02c59b47810e59b63c9f05fed97 [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;
Josef Bacikf3465ca2008-11-12 14:19:50 -050045 struct list_head list;
46 int del;
Zheng Yan31840ae2008-09-23 13:14:14 -040047};
48
Chris Masone089f052007-03-16 16:20:31 -040049static int finish_current_insert(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -040050 btrfs_root *extent_root, int all);
Chris Masone20d96d2007-03-22 12:13:20 -040051static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -040052 btrfs_root *extent_root, int all);
Chris Mason925baed2008-06-25 16:01:30 -040053static struct btrfs_block_group_cache *
54__btrfs_find_block_group(struct btrfs_root *root,
55 struct btrfs_block_group_cache *hint,
56 u64 search_start, int data, int owner);
Josef Bacikf3465ca2008-11-12 14:19:50 -050057static int pin_down_bytes(struct btrfs_trans_handle *trans,
58 struct btrfs_root *root,
59 u64 bytenr, u64 num_bytes, int is_data);
60static int update_block_group(struct btrfs_trans_handle *trans,
61 struct btrfs_root *root,
62 u64 bytenr, u64 num_bytes, int alloc,
63 int mark_free);
Yand548ee52008-01-03 13:56:30 -050064
Josef Bacik0f9dd462008-09-23 13:14:11 -040065static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
66{
67 return (cache->flags & bits) == bits;
68}
69
70/*
71 * this adds the block group to the fs_info rb tree for the block group
72 * cache
73 */
74int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
75 struct btrfs_block_group_cache *block_group)
76{
77 struct rb_node **p;
78 struct rb_node *parent = NULL;
79 struct btrfs_block_group_cache *cache;
80
81 spin_lock(&info->block_group_cache_lock);
82 p = &info->block_group_cache_tree.rb_node;
83
84 while (*p) {
85 parent = *p;
86 cache = rb_entry(parent, struct btrfs_block_group_cache,
87 cache_node);
88 if (block_group->key.objectid < cache->key.objectid) {
89 p = &(*p)->rb_left;
90 } else if (block_group->key.objectid > cache->key.objectid) {
91 p = &(*p)->rb_right;
92 } else {
93 spin_unlock(&info->block_group_cache_lock);
94 return -EEXIST;
95 }
96 }
97
98 rb_link_node(&block_group->cache_node, parent, p);
99 rb_insert_color(&block_group->cache_node,
100 &info->block_group_cache_tree);
101 spin_unlock(&info->block_group_cache_lock);
102
103 return 0;
104}
105
106/*
107 * This will return the block group at or after bytenr if contains is 0, else
108 * it will return the block group that contains the bytenr
109 */
110static struct btrfs_block_group_cache *
111block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
112 int contains)
113{
114 struct btrfs_block_group_cache *cache, *ret = NULL;
115 struct rb_node *n;
116 u64 end, start;
117
118 spin_lock(&info->block_group_cache_lock);
119 n = info->block_group_cache_tree.rb_node;
120
121 while (n) {
122 cache = rb_entry(n, struct btrfs_block_group_cache,
123 cache_node);
124 end = cache->key.objectid + cache->key.offset - 1;
125 start = cache->key.objectid;
126
127 if (bytenr < start) {
128 if (!contains && (!ret || start < ret->key.objectid))
129 ret = cache;
130 n = n->rb_left;
131 } else if (bytenr > start) {
132 if (contains && bytenr <= end) {
133 ret = cache;
134 break;
135 }
136 n = n->rb_right;
137 } else {
138 ret = cache;
139 break;
140 }
141 }
142 spin_unlock(&info->block_group_cache_lock);
143
144 return ret;
145}
146
147/*
148 * this is only called by cache_block_group, since we could have freed extents
149 * we need to check the pinned_extents for any extents that can't be used yet
150 * since their free space will be released as soon as the transaction commits.
151 */
152static int add_new_free_space(struct btrfs_block_group_cache *block_group,
153 struct btrfs_fs_info *info, u64 start, u64 end)
154{
155 u64 extent_start, extent_end, size;
156 int ret;
157
Josef Bacik25179202008-10-29 14:49:05 -0400158 mutex_lock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400159 while (start < end) {
160 ret = find_first_extent_bit(&info->pinned_extents, start,
161 &extent_start, &extent_end,
162 EXTENT_DIRTY);
163 if (ret)
164 break;
165
166 if (extent_start == start) {
167 start = extent_end + 1;
168 } else if (extent_start > start && extent_start < end) {
169 size = extent_start - start;
Josef Bacik25179202008-10-29 14:49:05 -0400170 ret = btrfs_add_free_space_lock(block_group, start,
171 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400172 BUG_ON(ret);
173 start = extent_end + 1;
174 } else {
175 break;
176 }
177 }
178
179 if (start < end) {
180 size = end - start;
Josef Bacik25179202008-10-29 14:49:05 -0400181 ret = btrfs_add_free_space_lock(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400182 BUG_ON(ret);
183 }
Josef Bacik25179202008-10-29 14:49:05 -0400184 mutex_unlock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400185
186 return 0;
187}
188
Chris Masone37c9e62007-05-09 20:13:14 -0400189static int cache_block_group(struct btrfs_root *root,
190 struct btrfs_block_group_cache *block_group)
191{
192 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400193 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400194 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400195 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400196 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -0400197 u64 last = 0;
Yan7d7d6062007-09-14 16:15:28 -0400198 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -0400199 int found = 0;
200
Chris Mason00f5c792007-11-30 10:09:33 -0500201 if (!block_group)
202 return 0;
203
Chris Masone37c9e62007-05-09 20:13:14 -0400204 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400205
206 if (block_group->cached)
207 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400208
Chris Masone37c9e62007-05-09 20:13:14 -0400209 path = btrfs_alloc_path();
210 if (!path)
211 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400212
Chris Mason2cc58cf2007-08-27 16:49:44 -0400213 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400214 /*
215 * we get into deadlocks with paths held by callers of this function.
216 * since the alloc_mutex is protecting things right now, just
217 * skip the locking here
218 */
219 path->skip_locking = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400220 first_free = max_t(u64, block_group->key.objectid,
221 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Masone37c9e62007-05-09 20:13:14 -0400222 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -0400223 key.offset = 0;
224 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
225 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
226 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400227 goto err;
Chris Mason0b86a832008-03-24 15:01:56 -0400228 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -0500229 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400230 goto err;
Yand548ee52008-01-03 13:56:30 -0500231 if (ret == 0) {
232 leaf = path->nodes[0];
233 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
234 if (key.objectid + key.offset > first_free)
235 first_free = key.objectid + key.offset;
236 }
Chris Masone37c9e62007-05-09 20:13:14 -0400237 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400238 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400239 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400240 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400241 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400242 if (ret < 0)
243 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400244 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400245 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400246 else
Chris Masone37c9e62007-05-09 20:13:14 -0400247 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400248 }
Chris Mason5f39d392007-10-15 16:14:19 -0400249 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400250 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400251 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400252
Chris Masone37c9e62007-05-09 20:13:14 -0400253 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400254 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400255 break;
Yan7d7d6062007-09-14 16:15:28 -0400256
257 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
258 if (!found) {
259 last = first_free;
260 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400261 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400262
263 add_new_free_space(block_group, root->fs_info, last,
264 key.objectid);
265
Yan7d7d6062007-09-14 16:15:28 -0400266 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400267 }
Yan7d7d6062007-09-14 16:15:28 -0400268next:
Chris Masone37c9e62007-05-09 20:13:14 -0400269 path->slots[0]++;
270 }
271
Yan7d7d6062007-09-14 16:15:28 -0400272 if (!found)
273 last = first_free;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400274
275 add_new_free_space(block_group, root->fs_info, last,
276 block_group->key.objectid +
277 block_group->key.offset);
278
Chris Masone37c9e62007-05-09 20:13:14 -0400279 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400280 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400281err:
Chris Masone37c9e62007-05-09 20:13:14 -0400282 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400283 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400284}
285
Josef Bacik0f9dd462008-09-23 13:14:11 -0400286/*
287 * return the block group that starts at or after bytenr
288 */
Chris Mason0ef3e662008-05-24 14:04:53 -0400289struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
290 btrfs_fs_info *info,
291 u64 bytenr)
292{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400293 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400294
Josef Bacik0f9dd462008-09-23 13:14:11 -0400295 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400296
Josef Bacik0f9dd462008-09-23 13:14:11 -0400297 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400298}
299
Josef Bacik0f9dd462008-09-23 13:14:11 -0400300/*
301 * return the block group that contains teh given bytenr
302 */
Chris Mason5276aed2007-06-11 21:33:38 -0400303struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
304 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400305 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400306{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400307 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400308
Josef Bacik0f9dd462008-09-23 13:14:11 -0400309 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400310
Josef Bacik0f9dd462008-09-23 13:14:11 -0400311 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400312}
Chris Mason0b86a832008-03-24 15:01:56 -0400313
Josef Bacik0f9dd462008-09-23 13:14:11 -0400314static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
315 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400316{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400317 struct list_head *head = &info->space_info;
318 struct list_head *cur;
319 struct btrfs_space_info *found;
320 list_for_each(cur, head) {
321 found = list_entry(cur, struct btrfs_space_info, list);
322 if (found->flags == flags)
323 return found;
324 }
325 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400326}
327
Josef Bacik80eb2342008-10-29 14:49:05 -0400328static u64 div_factor(u64 num, int factor)
329{
330 if (factor == 10)
331 return num;
332 num *= factor;
333 do_div(num, 10);
334 return num;
335}
336
Chris Mason925baed2008-06-25 16:01:30 -0400337static struct btrfs_block_group_cache *
338__btrfs_find_block_group(struct btrfs_root *root,
339 struct btrfs_block_group_cache *hint,
340 u64 search_start, int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400341{
Chris Mason96b51792007-10-15 16:15:19 -0400342 struct btrfs_block_group_cache *cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400343 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400344 struct btrfs_fs_info *info = root->fs_info;
345 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400346 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400347 u64 free_check;
Chris Mason31f3c992007-04-30 15:25:45 -0400348 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400349 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400350 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400351
Chris Masona236aed2008-04-29 09:38:00 -0400352 if (data & BTRFS_BLOCK_GROUP_METADATA)
353 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400354
Chris Mason0ef3e662008-05-24 14:04:53 -0400355 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400356 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400357 shint = btrfs_lookup_first_block_group(info, search_start);
Yan Zheng2b820322008-11-17 21:11:30 -0500358 if (shint && block_group_bits(shint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400359 spin_lock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400360 used = btrfs_block_group_used(&shint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400361 if (used + shint->pinned + shint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500362 div_factor(shint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400363 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400364 return shint;
365 }
Chris Masonc286ac42008-07-22 23:06:41 -0400366 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400367 }
368 }
Yan Zheng2b820322008-11-17 21:11:30 -0500369 if (hint && block_group_bits(hint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400370 spin_lock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400371 used = btrfs_block_group_used(&hint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400372 if (used + hint->pinned + hint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500373 div_factor(hint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400374 spin_unlock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400375 return hint;
376 }
Chris Masonc286ac42008-07-22 23:06:41 -0400377 spin_unlock(&hint->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400378 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400379 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400380 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400381 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400382 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400383 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400384 }
Chris Mason31f3c992007-04-30 15:25:45 -0400385again:
Zheng Yane8569812008-09-26 10:05:48 -0400386 while (1) {
387 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400388 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400389 break;
Chris Mason96b51792007-10-15 16:15:19 -0400390
Chris Masonc286ac42008-07-22 23:06:41 -0400391 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400392 last = cache->key.objectid + cache->key.offset;
393 used = btrfs_block_group_used(&cache->item);
394
Yan Zheng2b820322008-11-17 21:11:30 -0500395 if (block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400396 free_check = div_factor(cache->key.offset, factor);
Zheng Yane8569812008-09-26 10:05:48 -0400397 if (used + cache->pinned + cache->reserved <
398 free_check) {
Chris Mason8790d502008-04-03 16:29:03 -0400399 found_group = cache;
Chris Masonc286ac42008-07-22 23:06:41 -0400400 spin_unlock(&cache->lock);
Chris Mason8790d502008-04-03 16:29:03 -0400401 goto found;
402 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400403 }
Chris Masonc286ac42008-07-22 23:06:41 -0400404 spin_unlock(&cache->lock);
Chris Masonde428b62007-05-18 13:28:27 -0400405 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400406 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400407 if (!wrapped) {
408 last = search_start;
409 wrapped = 1;
410 goto again;
411 }
412 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400413 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400414 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400415 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400416 goto again;
417 }
Chris Masonbe744172007-05-06 10:15:01 -0400418found:
Chris Mason31f3c992007-04-30 15:25:45 -0400419 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400420}
421
Chris Mason925baed2008-06-25 16:01:30 -0400422struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
423 struct btrfs_block_group_cache
424 *hint, u64 search_start,
425 int data, int owner)
426{
427
428 struct btrfs_block_group_cache *ret;
Chris Mason925baed2008-06-25 16:01:30 -0400429 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
Chris Mason925baed2008-06-25 16:01:30 -0400430 return ret;
431}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400432
Chris Masone02119d2008-09-05 16:13:11 -0400433/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400434int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400435{
436 int ret;
437 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400438 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400439
Zheng Yan31840ae2008-09-23 13:14:14 -0400440 path = btrfs_alloc_path();
441 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400442 key.objectid = start;
443 key.offset = len;
444 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
445 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
446 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400447 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500448 return ret;
449}
450
Chris Masond8d5f3e2007-12-11 12:42:00 -0500451/*
452 * Back reference rules. Back refs have three main goals:
453 *
454 * 1) differentiate between all holders of references to an extent so that
455 * when a reference is dropped we can make sure it was a valid reference
456 * before freeing the extent.
457 *
458 * 2) Provide enough information to quickly find the holders of an extent
459 * if we notice a given block is corrupted or bad.
460 *
461 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
462 * maintenance. This is actually the same as #2, but with a slightly
463 * different use case.
464 *
465 * File extents can be referenced by:
466 *
467 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400468 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500469 * - different offsets inside a file (bookend extents in file.c)
470 *
471 * The extent ref structure has fields for:
472 *
473 * - Objectid of the subvolume root
474 * - Generation number of the tree holding the reference
475 * - objectid of the file holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400476 * - number of references holding by parent node (alway 1 for tree blocks)
477 *
478 * Btree leaf may hold multiple references to a file extent. In most cases,
479 * these references are from same file and the corresponding offsets inside
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400480 * the file are close together.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500481 *
482 * When a file extent is allocated the fields are filled in:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400483 * (root_key.objectid, trans->transid, inode objectid, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500484 *
485 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400486 * in the leaf. It looks similar to the create case, but trans->transid will
487 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500488 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400489 * (root_key.objectid, trans->transid, inode objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400490 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500491 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400492 * When a file extent is removed either during snapshot deletion or
493 * file truncation, we find the corresponding back reference and check
494 * the following fields:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500495 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400496 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
497 * inode objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500498 *
499 * Btree extents can be referenced by:
500 *
501 * - Different subvolumes
502 * - Different generations of the same subvolume
503 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500504 * When a tree block is created, back references are inserted:
505 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400506 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500507 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400508 * When a tree block is cow'd, new back references are added for all the
509 * blocks it points to. If the tree block isn't in reference counted root,
510 * the old back references are removed. These new back references are of
511 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500512 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400513 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500514 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400515 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500516 *
517 * if backref was for a tree root:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400518 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500519 * else
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400520 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500521 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400522 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500523 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400524 * The key objectid corresponds to the first byte in the extent, the key
525 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
526 * byte of parent extent. If a extent is tree root, the key offset is set
527 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500528 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400529
530static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
531 struct btrfs_root *root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400532 struct btrfs_path *path,
533 u64 bytenr, u64 parent,
534 u64 ref_root, u64 ref_generation,
535 u64 owner_objectid, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500536{
Chris Mason74493f72007-12-11 09:25:06 -0500537 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400538 struct btrfs_extent_ref *ref;
539 struct extent_buffer *leaf;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400540 u64 ref_objectid;
Chris Mason74493f72007-12-11 09:25:06 -0500541 int ret;
542
Chris Mason74493f72007-12-11 09:25:06 -0500543 key.objectid = bytenr;
544 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400545 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500546
Zheng Yan31840ae2008-09-23 13:14:14 -0400547 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
548 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500549 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400550 if (ret > 0) {
551 ret = -ENOENT;
552 goto out;
553 }
554
555 leaf = path->nodes[0];
556 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400557 ref_objectid = btrfs_ref_objectid(leaf, ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400558 if (btrfs_ref_root(leaf, ref) != ref_root ||
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400559 btrfs_ref_generation(leaf, ref) != ref_generation ||
560 (ref_objectid != owner_objectid &&
561 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400562 ret = -EIO;
563 WARN_ON(1);
564 goto out;
565 }
566 ret = 0;
567out:
568 return ret;
569}
570
Josef Bacikf3465ca2008-11-12 14:19:50 -0500571/*
572 * updates all the backrefs that are pending on update_list for the
573 * extent_root
574 */
575static int noinline update_backrefs(struct btrfs_trans_handle *trans,
576 struct btrfs_root *extent_root,
577 struct btrfs_path *path,
578 struct list_head *update_list)
579{
580 struct btrfs_key key;
581 struct btrfs_extent_ref *ref;
582 struct btrfs_fs_info *info = extent_root->fs_info;
583 struct pending_extent_op *op;
584 struct extent_buffer *leaf;
585 int ret = 0;
586 struct list_head *cur = update_list->next;
587 u64 ref_objectid;
588 u64 ref_root = extent_root->root_key.objectid;
589
590 op = list_entry(cur, struct pending_extent_op, list);
591
592search:
593 key.objectid = op->bytenr;
594 key.type = BTRFS_EXTENT_REF_KEY;
595 key.offset = op->orig_parent;
596
597 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
598 BUG_ON(ret);
599
600 leaf = path->nodes[0];
601
602loop:
603 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
604
605 ref_objectid = btrfs_ref_objectid(leaf, ref);
606
607 if (btrfs_ref_root(leaf, ref) != ref_root ||
608 btrfs_ref_generation(leaf, ref) != op->orig_generation ||
609 (ref_objectid != op->level &&
610 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
611 printk(KERN_ERR "couldn't find %Lu, parent %Lu, root %Lu, "
612 "owner %u\n", op->bytenr, op->orig_parent,
613 ref_root, op->level);
614 btrfs_print_leaf(extent_root, leaf);
615 BUG();
616 }
617
618 key.objectid = op->bytenr;
619 key.offset = op->parent;
620 key.type = BTRFS_EXTENT_REF_KEY;
621 ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
622 BUG_ON(ret);
623 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
624 btrfs_set_ref_generation(leaf, ref, op->generation);
625
626 cur = cur->next;
627
628 list_del_init(&op->list);
629 unlock_extent(&info->extent_ins, op->bytenr,
630 op->bytenr + op->num_bytes - 1, GFP_NOFS);
631 kfree(op);
632
633 if (cur == update_list) {
634 btrfs_mark_buffer_dirty(path->nodes[0]);
635 btrfs_release_path(extent_root, path);
636 goto out;
637 }
638
639 op = list_entry(cur, struct pending_extent_op, list);
640
641 path->slots[0]++;
642 while (path->slots[0] < btrfs_header_nritems(leaf)) {
643 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
644 if (key.objectid == op->bytenr &&
645 key.type == BTRFS_EXTENT_REF_KEY)
646 goto loop;
647 path->slots[0]++;
648 }
649
650 btrfs_mark_buffer_dirty(path->nodes[0]);
651 btrfs_release_path(extent_root, path);
652 goto search;
653
654out:
655 return 0;
656}
657
658static int noinline insert_extents(struct btrfs_trans_handle *trans,
659 struct btrfs_root *extent_root,
660 struct btrfs_path *path,
661 struct list_head *insert_list, int nr)
662{
663 struct btrfs_key *keys;
664 u32 *data_size;
665 struct pending_extent_op *op;
666 struct extent_buffer *leaf;
667 struct list_head *cur = insert_list->next;
668 struct btrfs_fs_info *info = extent_root->fs_info;
669 u64 ref_root = extent_root->root_key.objectid;
670 int i = 0, last = 0, ret;
671 int total = nr * 2;
672
673 if (!nr)
674 return 0;
675
676 keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
677 if (!keys)
678 return -ENOMEM;
679
680 data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
681 if (!data_size) {
682 kfree(keys);
683 return -ENOMEM;
684 }
685
686 list_for_each_entry(op, insert_list, list) {
687 keys[i].objectid = op->bytenr;
688 keys[i].offset = op->num_bytes;
689 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
690 data_size[i] = sizeof(struct btrfs_extent_item);
691 i++;
692
693 keys[i].objectid = op->bytenr;
694 keys[i].offset = op->parent;
695 keys[i].type = BTRFS_EXTENT_REF_KEY;
696 data_size[i] = sizeof(struct btrfs_extent_ref);
697 i++;
698 }
699
700 op = list_entry(cur, struct pending_extent_op, list);
701 i = 0;
702 while (i < total) {
703 int c;
704 ret = btrfs_insert_some_items(trans, extent_root, path,
705 keys+i, data_size+i, total-i);
706 BUG_ON(ret < 0);
707
708 if (last && ret > 1)
709 BUG();
710
711 leaf = path->nodes[0];
712 for (c = 0; c < ret; c++) {
713 int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
714
715 /*
716 * if the first item we inserted was a backref, then
717 * the EXTENT_ITEM will be the odd c's, else it will
718 * be the even c's
719 */
720 if ((ref_first && (c % 2)) ||
721 (!ref_first && !(c % 2))) {
722 struct btrfs_extent_item *itm;
723
724 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
725 struct btrfs_extent_item);
726 btrfs_set_extent_refs(path->nodes[0], itm, 1);
727 op->del++;
728 } else {
729 struct btrfs_extent_ref *ref;
730
731 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
732 struct btrfs_extent_ref);
733 btrfs_set_ref_root(leaf, ref, ref_root);
734 btrfs_set_ref_generation(leaf, ref,
735 op->generation);
736 btrfs_set_ref_objectid(leaf, ref, op->level);
737 btrfs_set_ref_num_refs(leaf, ref, 1);
738 op->del++;
739 }
740
741 /*
742 * using del to see when its ok to free up the
743 * pending_extent_op. In the case where we insert the
744 * last item on the list in order to help do batching
745 * we need to not free the extent op until we actually
746 * insert the extent_item
747 */
748 if (op->del == 2) {
749 unlock_extent(&info->extent_ins, op->bytenr,
750 op->bytenr + op->num_bytes - 1,
751 GFP_NOFS);
752 cur = cur->next;
753 list_del_init(&op->list);
754 kfree(op);
755 if (cur != insert_list)
756 op = list_entry(cur,
757 struct pending_extent_op,
758 list);
759 }
760 }
761 btrfs_mark_buffer_dirty(leaf);
762 btrfs_release_path(extent_root, path);
763
764 /*
765 * Ok backref's and items usually go right next to eachother,
766 * but if we could only insert 1 item that means that we
767 * inserted on the end of a leaf, and we have no idea what may
768 * be on the next leaf so we just play it safe. In order to
769 * try and help this case we insert the last thing on our
770 * insert list so hopefully it will end up being the last
771 * thing on the leaf and everything else will be before it,
772 * which will let us insert a whole bunch of items at the same
773 * time.
774 */
775 if (ret == 1 && !last && (i + ret < total)) {
776 /*
777 * last: where we will pick up the next time around
778 * i: our current key to insert, will be total - 1
779 * cur: the current op we are screwing with
780 * op: duh
781 */
782 last = i + ret;
783 i = total - 1;
784 cur = insert_list->prev;
785 op = list_entry(cur, struct pending_extent_op, list);
786 } else if (last) {
787 /*
788 * ok we successfully inserted the last item on the
789 * list, lets reset everything
790 *
791 * i: our current key to insert, so where we left off
792 * last time
793 * last: done with this
794 * cur: the op we are messing with
795 * op: duh
796 * total: since we inserted the last key, we need to
797 * decrement total so we dont overflow
798 */
799 i = last;
800 last = 0;
801 cur = insert_list->next;
802 op = list_entry(cur, struct pending_extent_op, list);
803 total--;
804 } else {
805 i += ret;
806 }
807
808 cond_resched();
809 }
810 ret = 0;
811 kfree(keys);
812 kfree(data_size);
813 return ret;
814}
815
Zheng Yan31840ae2008-09-23 13:14:14 -0400816static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
817 struct btrfs_root *root,
818 struct btrfs_path *path,
819 u64 bytenr, u64 parent,
820 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400821 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400822{
823 struct btrfs_key key;
824 struct extent_buffer *leaf;
825 struct btrfs_extent_ref *ref;
826 u32 num_refs;
827 int ret;
828
829 key.objectid = bytenr;
830 key.type = BTRFS_EXTENT_REF_KEY;
831 key.offset = parent;
832
833 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
834 if (ret == 0) {
835 leaf = path->nodes[0];
836 ref = btrfs_item_ptr(leaf, path->slots[0],
837 struct btrfs_extent_ref);
838 btrfs_set_ref_root(leaf, ref, ref_root);
839 btrfs_set_ref_generation(leaf, ref, ref_generation);
840 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400841 btrfs_set_ref_num_refs(leaf, ref, 1);
842 } else if (ret == -EEXIST) {
843 u64 existing_owner;
844 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
845 leaf = path->nodes[0];
846 ref = btrfs_item_ptr(leaf, path->slots[0],
847 struct btrfs_extent_ref);
848 if (btrfs_ref_root(leaf, ref) != ref_root ||
849 btrfs_ref_generation(leaf, ref) != ref_generation) {
850 ret = -EIO;
851 WARN_ON(1);
852 goto out;
853 }
854
855 num_refs = btrfs_ref_num_refs(leaf, ref);
856 BUG_ON(num_refs == 0);
857 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
858
859 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400860 if (existing_owner != owner_objectid &&
861 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400862 btrfs_set_ref_objectid(leaf, ref,
863 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400864 }
865 ret = 0;
866 } else {
867 goto out;
868 }
Chris Mason7bb86312007-12-11 09:25:06 -0500869 btrfs_mark_buffer_dirty(path->nodes[0]);
870out:
871 btrfs_release_path(root, path);
872 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500873}
874
Zheng Yan31840ae2008-09-23 13:14:14 -0400875static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
876 struct btrfs_root *root,
877 struct btrfs_path *path)
878{
879 struct extent_buffer *leaf;
880 struct btrfs_extent_ref *ref;
881 u32 num_refs;
882 int ret = 0;
883
884 leaf = path->nodes[0];
885 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
886 num_refs = btrfs_ref_num_refs(leaf, ref);
887 BUG_ON(num_refs == 0);
888 num_refs -= 1;
889 if (num_refs == 0) {
890 ret = btrfs_del_item(trans, root, path);
891 } else {
892 btrfs_set_ref_num_refs(leaf, ref, num_refs);
893 btrfs_mark_buffer_dirty(leaf);
894 }
895 btrfs_release_path(root, path);
896 return ret;
897}
898
Josef Bacikf3465ca2008-11-12 14:19:50 -0500899static int noinline free_extents(struct btrfs_trans_handle *trans,
900 struct btrfs_root *extent_root,
901 struct list_head *del_list)
902{
903 struct btrfs_fs_info *info = extent_root->fs_info;
904 struct btrfs_path *path;
905 struct btrfs_key key, found_key;
906 struct extent_buffer *leaf;
907 struct list_head *cur;
908 struct pending_extent_op *op;
909 struct btrfs_extent_item *ei;
910 int ret, num_to_del, extent_slot = 0, found_extent = 0;
911 u32 refs;
912 u64 bytes_freed = 0;
913
914 path = btrfs_alloc_path();
915 if (!path)
916 return -ENOMEM;
917 path->reada = 1;
918
919search:
920 /* search for the backref for the current ref we want to delete */
921 cur = del_list->next;
922 op = list_entry(cur, struct pending_extent_op, list);
923 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
924 op->orig_parent,
925 extent_root->root_key.objectid,
926 op->orig_generation, op->level, 1);
927 if (ret) {
928 printk("Unable to find backref byte nr %Lu root %Lu gen %Lu "
929 "owner %u\n", op->bytenr,
930 extent_root->root_key.objectid, op->orig_generation,
931 op->level);
932 btrfs_print_leaf(extent_root, path->nodes[0]);
933 WARN_ON(1);
934 goto out;
935 }
936
937 extent_slot = path->slots[0];
938 num_to_del = 1;
939 found_extent = 0;
940
941 /*
942 * if we aren't the first item on the leaf we can move back one and see
943 * if our ref is right next to our extent item
944 */
945 if (likely(extent_slot)) {
946 extent_slot--;
947 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
948 extent_slot);
949 if (found_key.objectid == op->bytenr &&
950 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
951 found_key.offset == op->num_bytes) {
952 num_to_del++;
953 found_extent = 1;
954 }
955 }
956
957 /*
958 * if we didn't find the extent we need to delete the backref and then
959 * search for the extent item key so we can update its ref count
960 */
961 if (!found_extent) {
962 key.objectid = op->bytenr;
963 key.type = BTRFS_EXTENT_ITEM_KEY;
964 key.offset = op->num_bytes;
965
966 ret = remove_extent_backref(trans, extent_root, path);
967 BUG_ON(ret);
968 btrfs_release_path(extent_root, path);
969 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
970 BUG_ON(ret);
971 extent_slot = path->slots[0];
972 }
973
974 /* this is where we update the ref count for the extent */
975 leaf = path->nodes[0];
976 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
977 refs = btrfs_extent_refs(leaf, ei);
978 BUG_ON(refs == 0);
979 refs--;
980 btrfs_set_extent_refs(leaf, ei, refs);
981
982 btrfs_mark_buffer_dirty(leaf);
983
984 /*
985 * This extent needs deleting. The reason cur_slot is extent_slot +
986 * num_to_del is because extent_slot points to the slot where the extent
987 * is, and if the backref was not right next to the extent we will be
988 * deleting at least 1 item, and will want to start searching at the
989 * slot directly next to extent_slot. However if we did find the
990 * backref next to the extent item them we will be deleting at least 2
991 * items and will want to start searching directly after the ref slot
992 */
993 if (!refs) {
994 struct list_head *pos, *n, *end;
995 int cur_slot = extent_slot+num_to_del;
996 u64 super_used;
997 u64 root_used;
998
999 path->slots[0] = extent_slot;
1000 bytes_freed = op->num_bytes;
1001
Josef Bacike3e469f2008-11-17 21:11:49 -05001002 mutex_lock(&info->pinned_mutex);
1003 ret = pin_down_bytes(trans, extent_root, op->bytenr,
1004 op->num_bytes, op->level >=
1005 BTRFS_FIRST_FREE_OBJECTID);
1006 mutex_unlock(&info->pinned_mutex);
1007 BUG_ON(ret < 0);
1008 op->del = ret;
1009
Josef Bacikf3465ca2008-11-12 14:19:50 -05001010 /*
1011 * we need to see if we can delete multiple things at once, so
1012 * start looping through the list of extents we are wanting to
1013 * delete and see if their extent/backref's are right next to
1014 * eachother and the extents only have 1 ref
1015 */
1016 for (pos = cur->next; pos != del_list; pos = pos->next) {
1017 struct pending_extent_op *tmp;
1018
1019 tmp = list_entry(pos, struct pending_extent_op, list);
1020
1021 /* we only want to delete extent+ref at this stage */
1022 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1023 break;
1024
1025 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1026 if (found_key.objectid != tmp->bytenr ||
1027 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1028 found_key.offset != tmp->num_bytes)
1029 break;
1030
1031 /* check to make sure this extent only has one ref */
1032 ei = btrfs_item_ptr(leaf, cur_slot,
1033 struct btrfs_extent_item);
1034 if (btrfs_extent_refs(leaf, ei) != 1)
1035 break;
1036
1037 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1038 if (found_key.objectid != tmp->bytenr ||
1039 found_key.type != BTRFS_EXTENT_REF_KEY ||
1040 found_key.offset != tmp->orig_parent)
1041 break;
1042
1043 /*
1044 * the ref is right next to the extent, we can set the
1045 * ref count to 0 since we will delete them both now
1046 */
1047 btrfs_set_extent_refs(leaf, ei, 0);
1048
1049 /* pin down the bytes for this extent */
1050 mutex_lock(&info->pinned_mutex);
1051 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1052 tmp->num_bytes, tmp->level >=
1053 BTRFS_FIRST_FREE_OBJECTID);
1054 mutex_unlock(&info->pinned_mutex);
1055 BUG_ON(ret < 0);
1056
1057 /*
1058 * use the del field to tell if we need to go ahead and
1059 * free up the extent when we delete the item or not.
1060 */
1061 tmp->del = ret;
1062 bytes_freed += tmp->num_bytes;
1063
1064 num_to_del += 2;
1065 cur_slot += 2;
1066 }
1067 end = pos;
1068
1069 /* update the free space counters */
1070 spin_lock_irq(&info->delalloc_lock);
1071 super_used = btrfs_super_bytes_used(&info->super_copy);
1072 btrfs_set_super_bytes_used(&info->super_copy,
1073 super_used - bytes_freed);
1074 spin_unlock_irq(&info->delalloc_lock);
1075
1076 root_used = btrfs_root_used(&extent_root->root_item);
1077 btrfs_set_root_used(&extent_root->root_item,
1078 root_used - bytes_freed);
1079
1080 /* delete the items */
1081 ret = btrfs_del_items(trans, extent_root, path,
1082 path->slots[0], num_to_del);
1083 BUG_ON(ret);
1084
1085 /*
1086 * loop through the extents we deleted and do the cleanup work
1087 * on them
1088 */
1089 for (pos = cur, n = pos->next; pos != end;
1090 pos = n, n = pos->next) {
1091 struct pending_extent_op *tmp;
1092#ifdef BIO_RW_DISCARD
1093 u64 map_length;
1094 struct btrfs_multi_bio *multi = NULL;
1095#endif
1096 tmp = list_entry(pos, struct pending_extent_op, list);
1097
1098 /*
1099 * remember tmp->del tells us wether or not we pinned
1100 * down the extent
1101 */
1102 ret = update_block_group(trans, extent_root,
1103 tmp->bytenr, tmp->num_bytes, 0,
1104 tmp->del);
1105 BUG_ON(ret);
1106
1107#ifdef BIO_RW_DISCARD
1108 ret = btrfs_map_block(&info->mapping_tree, READ,
1109 tmp->bytenr, &map_length, &multi,
1110 0);
1111 if (!ret) {
1112 struct btrfs_bio_stripe *stripe;
1113 int i;
1114
1115 stripe = multi->stripe;
1116
1117 if (map_length > tmp->num_bytes)
1118 map_length = tmp->num_bytes;
1119
1120 for (i = 0; i < multi->num_stripes;
1121 i++, stripe++)
1122 blkdev_issue_discard(stripe->dev->bdev,
1123 stripe->physical >> 9,
1124 map_length >> 9);
1125 kfree(multi);
1126 }
1127#endif
1128 list_del_init(&tmp->list);
1129 unlock_extent(&info->extent_ins, tmp->bytenr,
1130 tmp->bytenr + tmp->num_bytes - 1,
1131 GFP_NOFS);
1132 kfree(tmp);
1133 }
1134 } else if (refs && found_extent) {
1135 /*
1136 * the ref and extent were right next to eachother, but the
1137 * extent still has a ref, so just free the backref and keep
1138 * going
1139 */
1140 ret = remove_extent_backref(trans, extent_root, path);
1141 BUG_ON(ret);
1142
1143 list_del_init(&op->list);
1144 unlock_extent(&info->extent_ins, op->bytenr,
1145 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1146 kfree(op);
1147 } else {
1148 /*
1149 * the extent has multiple refs and the backref we were looking
1150 * for was not right next to it, so just unlock and go next,
1151 * we're good to go
1152 */
1153 list_del_init(&op->list);
1154 unlock_extent(&info->extent_ins, op->bytenr,
1155 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1156 kfree(op);
1157 }
1158
1159 btrfs_release_path(extent_root, path);
1160 if (!list_empty(del_list))
1161 goto search;
1162
1163out:
1164 btrfs_free_path(path);
1165 return ret;
1166}
1167
Zheng Yan31840ae2008-09-23 13:14:14 -04001168static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1169 struct btrfs_root *root, u64 bytenr,
1170 u64 orig_parent, u64 parent,
1171 u64 orig_root, u64 ref_root,
1172 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001173 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001174{
1175 int ret;
1176 struct btrfs_root *extent_root = root->fs_info->extent_root;
1177 struct btrfs_path *path;
1178
1179 if (root == root->fs_info->extent_root) {
1180 struct pending_extent_op *extent_op;
1181 u64 num_bytes;
1182
1183 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1184 num_bytes = btrfs_level_size(root, (int)owner_objectid);
Josef Bacik25179202008-10-29 14:49:05 -04001185 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001186 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
Josef Bacik25179202008-10-29 14:49:05 -04001187 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001188 u64 priv;
1189 ret = get_state_private(&root->fs_info->extent_ins,
1190 bytenr, &priv);
1191 BUG_ON(ret);
1192 extent_op = (struct pending_extent_op *)
1193 (unsigned long)priv;
1194 BUG_ON(extent_op->parent != orig_parent);
1195 BUG_ON(extent_op->generation != orig_generation);
Josef Bacik25179202008-10-29 14:49:05 -04001196
Zheng Yan31840ae2008-09-23 13:14:14 -04001197 extent_op->parent = parent;
1198 extent_op->generation = ref_generation;
1199 } else {
1200 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1201 BUG_ON(!extent_op);
1202
1203 extent_op->type = PENDING_BACKREF_UPDATE;
1204 extent_op->bytenr = bytenr;
1205 extent_op->num_bytes = num_bytes;
1206 extent_op->parent = parent;
1207 extent_op->orig_parent = orig_parent;
1208 extent_op->generation = ref_generation;
1209 extent_op->orig_generation = orig_generation;
1210 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001211 INIT_LIST_HEAD(&extent_op->list);
1212 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001213
1214 set_extent_bits(&root->fs_info->extent_ins,
1215 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04001216 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04001217 set_state_private(&root->fs_info->extent_ins,
1218 bytenr, (unsigned long)extent_op);
1219 }
Josef Bacik25179202008-10-29 14:49:05 -04001220 mutex_unlock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001221 return 0;
1222 }
1223
1224 path = btrfs_alloc_path();
1225 if (!path)
1226 return -ENOMEM;
1227 ret = lookup_extent_backref(trans, extent_root, path,
1228 bytenr, orig_parent, orig_root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001229 orig_generation, owner_objectid, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001230 if (ret)
1231 goto out;
1232 ret = remove_extent_backref(trans, extent_root, path);
1233 if (ret)
1234 goto out;
1235 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1236 parent, ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001237 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001238 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001239 finish_current_insert(trans, extent_root, 0);
1240 del_pending_extents(trans, extent_root, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001241out:
1242 btrfs_free_path(path);
1243 return ret;
1244}
1245
1246int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1247 struct btrfs_root *root, u64 bytenr,
1248 u64 orig_parent, u64 parent,
1249 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001250 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001251{
1252 int ret;
1253 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1254 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1255 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001256 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1257 parent, ref_root, ref_root,
1258 ref_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001259 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001260 return ret;
1261}
1262
Chris Mason925baed2008-06-25 16:01:30 -04001263static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001264 struct btrfs_root *root, u64 bytenr,
1265 u64 orig_parent, u64 parent,
1266 u64 orig_root, u64 ref_root,
1267 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001268 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -05001269{
Chris Mason5caf2a02007-04-02 11:20:42 -04001270 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -05001271 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001272 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001273 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001274 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001275 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001276
Chris Mason5caf2a02007-04-02 11:20:42 -04001277 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001278 if (!path)
1279 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001280
Chris Mason3c12ac72008-04-21 12:01:38 -04001281 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001282 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001283 key.type = BTRFS_EXTENT_ITEM_KEY;
1284 key.offset = (u64)-1;
1285
Chris Mason5caf2a02007-04-02 11:20:42 -04001286 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001287 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001288 if (ret < 0)
1289 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001290 BUG_ON(ret == 0 || path->slots[0] == 0);
1291
1292 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -04001293 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -04001294
1295 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Mason771ed682008-11-06 22:02:51 -05001296 if (key.objectid != bytenr) {
1297 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
1298 printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
1299 BUG();
1300 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001301 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1302
Chris Mason5caf2a02007-04-02 11:20:42 -04001303 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001304 refs = btrfs_extent_refs(l, item);
1305 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001306 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -05001307
Chris Mason5caf2a02007-04-02 11:20:42 -04001308 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001309
Chris Mason3c12ac72008-04-21 12:01:38 -04001310 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001311 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1312 path, bytenr, parent,
1313 ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001314 owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05001315 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001316 finish_current_insert(trans, root->fs_info->extent_root, 0);
1317 del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Mason74493f72007-12-11 09:25:06 -05001318
1319 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -05001320 return 0;
1321}
1322
Chris Mason925baed2008-06-25 16:01:30 -04001323int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001324 struct btrfs_root *root,
1325 u64 bytenr, u64 num_bytes, u64 parent,
1326 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001327 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -04001328{
1329 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001330 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1331 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1332 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001333 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1334 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001335 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -04001336 return ret;
1337}
1338
Chris Masone9d0b132007-08-10 14:06:19 -04001339int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1340 struct btrfs_root *root)
1341{
Chris Mason87ef2bb2008-10-30 11:23:27 -04001342 finish_current_insert(trans, root->fs_info->extent_root, 1);
1343 del_pending_extents(trans, root->fs_info->extent_root, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001344 return 0;
1345}
1346
Zheng Yan31840ae2008-09-23 13:14:14 -04001347int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1348 struct btrfs_root *root, u64 bytenr,
1349 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -05001350{
Chris Mason5caf2a02007-04-02 11:20:42 -04001351 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -05001352 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001353 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001354 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001355 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -04001356
Chris Masondb945352007-10-15 16:15:53 -04001357 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -04001358 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -04001359 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001360 key.objectid = bytenr;
1361 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -04001362 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -04001363 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001364 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001365 if (ret < 0)
1366 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -04001367 if (ret != 0) {
1368 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -04001369 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -05001370 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04001371 }
1372 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001373 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001374 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001375out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001376 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05001377 return 0;
1378}
1379
Yan Zheng80ff3852008-10-30 14:20:02 -04001380int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1381 struct btrfs_root *root, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05001382{
1383 struct btrfs_root *extent_root = root->fs_info->extent_root;
1384 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -04001385 struct extent_buffer *leaf;
1386 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -05001387 struct btrfs_key key;
1388 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001389 u64 ref_root;
1390 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -04001391 u32 nritems;
1392 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001393
Chris Masonbe20aa92007-12-17 20:14:01 -05001394 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001395 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04001396 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05001397
Yan Zhengf321e492008-07-30 09:26:11 -04001398 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -05001399 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1400 if (ret < 0)
1401 goto out;
1402 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001403
1404 ret = -ENOENT;
1405 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04001406 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001407
Zheng Yan31840ae2008-09-23 13:14:14 -04001408 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04001409 leaf = path->nodes[0];
1410 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001411
1412 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001413 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05001414 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001415
Yan Zheng80ff3852008-10-30 14:20:02 -04001416 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05001417 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -04001418 leaf = path->nodes[0];
1419 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -05001420 if (path->slots[0] >= nritems) {
1421 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -04001422 if (ret < 0)
1423 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001424 if (ret == 0)
1425 continue;
1426 break;
1427 }
Yan Zhengf321e492008-07-30 09:26:11 -04001428 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001429 if (found_key.objectid != bytenr)
1430 break;
Chris Masonbd098352008-01-03 13:23:19 -05001431
Chris Masonbe20aa92007-12-17 20:14:01 -05001432 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1433 path->slots[0]++;
1434 continue;
1435 }
1436
Yan Zhengf321e492008-07-30 09:26:11 -04001437 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001438 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -04001439 ref_root = btrfs_ref_root(leaf, ref_item);
1440 if (ref_root != root->root_key.objectid &&
1441 ref_root != BTRFS_TREE_LOG_OBJECTID) {
1442 ret = 1;
1443 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -04001444 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001445 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1446 ret = 1;
1447 goto out;
1448 }
Yan Zhengf321e492008-07-30 09:26:11 -04001449
Chris Masonbe20aa92007-12-17 20:14:01 -05001450 path->slots[0]++;
1451 }
Yan Zhengf321e492008-07-30 09:26:11 -04001452 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001453out:
Yan Zhengf321e492008-07-30 09:26:11 -04001454 btrfs_free_path(path);
1455 return ret;
1456}
1457
Zheng Yan31840ae2008-09-23 13:14:14 -04001458int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1459 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001460{
Chris Mason5f39d392007-10-15 16:14:19 -04001461 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001462 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001463 u64 root_gen;
1464 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001465 int i;
Chris Masondb945352007-10-15 16:15:53 -04001466 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001467 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001468 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001469
Chris Mason3768f362007-03-13 16:47:54 -04001470 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001471 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001472
Zheng Yane4657682008-09-26 10:04:53 -04001473 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1474 shared = 0;
1475 root_gen = root->root_key.offset;
1476 } else {
1477 shared = 1;
1478 root_gen = trans->transid - 1;
1479 }
1480
Chris Masondb945352007-10-15 16:15:53 -04001481 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001482 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001483
Zheng Yan31840ae2008-09-23 13:14:14 -04001484 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001485 struct btrfs_leaf_ref *ref;
1486 struct btrfs_extent_info *info;
1487
Zheng Yan31840ae2008-09-23 13:14:14 -04001488 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001489 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001490 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001491 goto out;
1492 }
1493
Zheng Yane4657682008-09-26 10:04:53 -04001494 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001495 ref->bytenr = buf->start;
1496 ref->owner = btrfs_header_owner(buf);
1497 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001498 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001499 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001500
Zheng Yan31840ae2008-09-23 13:14:14 -04001501 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001502 u64 disk_bytenr;
1503 btrfs_item_key_to_cpu(buf, &key, i);
1504 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1505 continue;
1506 fi = btrfs_item_ptr(buf, i,
1507 struct btrfs_file_extent_item);
1508 if (btrfs_file_extent_type(buf, fi) ==
1509 BTRFS_FILE_EXTENT_INLINE)
1510 continue;
1511 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1512 if (disk_bytenr == 0)
1513 continue;
1514
1515 info->bytenr = disk_bytenr;
1516 info->num_bytes =
1517 btrfs_file_extent_disk_num_bytes(buf, fi);
1518 info->objectid = key.objectid;
1519 info->offset = key.offset;
1520 info++;
1521 }
1522
Zheng Yane4657682008-09-26 10:04:53 -04001523 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001524 if (ret == -EEXIST && shared) {
1525 struct btrfs_leaf_ref *old;
1526 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1527 BUG_ON(!old);
1528 btrfs_remove_leaf_ref(root, old);
1529 btrfs_free_leaf_ref(root, old);
1530 ret = btrfs_add_leaf_ref(root, ref, shared);
1531 }
Yan Zheng31153d82008-07-28 15:32:19 -04001532 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001533 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001534 }
1535out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001536 return ret;
1537}
1538
1539int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1540 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1541 u32 *nr_extents)
1542{
1543 u64 bytenr;
1544 u64 ref_root;
1545 u64 orig_root;
1546 u64 ref_generation;
1547 u64 orig_generation;
1548 u32 nritems;
1549 u32 nr_file_extents = 0;
1550 struct btrfs_key key;
1551 struct btrfs_file_extent_item *fi;
1552 int i;
1553 int level;
1554 int ret = 0;
1555 int faili = 0;
1556 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001557 u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001558
1559 ref_root = btrfs_header_owner(buf);
1560 ref_generation = btrfs_header_generation(buf);
1561 orig_root = btrfs_header_owner(orig_buf);
1562 orig_generation = btrfs_header_generation(orig_buf);
1563
1564 nritems = btrfs_header_nritems(buf);
1565 level = btrfs_header_level(buf);
1566
1567 if (root->ref_cows) {
1568 process_func = __btrfs_inc_extent_ref;
1569 } else {
1570 if (level == 0 &&
1571 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1572 goto out;
1573 if (level != 0 &&
1574 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1575 goto out;
1576 process_func = __btrfs_update_extent_ref;
1577 }
1578
1579 for (i = 0; i < nritems; i++) {
1580 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001581 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001582 btrfs_item_key_to_cpu(buf, &key, i);
1583 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001584 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001585 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001586 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001587 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001588 BTRFS_FILE_EXTENT_INLINE)
1589 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001590 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1591 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001592 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001593
1594 nr_file_extents++;
1595
Zheng Yan31840ae2008-09-23 13:14:14 -04001596 ret = process_func(trans, root, bytenr,
1597 orig_buf->start, buf->start,
1598 orig_root, ref_root,
1599 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001600 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001601
1602 if (ret) {
1603 faili = i;
1604 WARN_ON(1);
1605 goto fail;
1606 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001607 } else {
Chris Masondb945352007-10-15 16:15:53 -04001608 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001609 ret = process_func(trans, root, bytenr,
1610 orig_buf->start, buf->start,
1611 orig_root, ref_root,
1612 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001613 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001614 if (ret) {
1615 faili = i;
1616 WARN_ON(1);
1617 goto fail;
1618 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001619 }
1620 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001621out:
1622 if (nr_extents) {
1623 if (level == 0)
1624 *nr_extents = nr_file_extents;
1625 else
1626 *nr_extents = nritems;
1627 }
1628 return 0;
1629fail:
1630 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001631 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001632}
1633
Zheng Yan31840ae2008-09-23 13:14:14 -04001634int btrfs_update_ref(struct btrfs_trans_handle *trans,
1635 struct btrfs_root *root, struct extent_buffer *orig_buf,
1636 struct extent_buffer *buf, int start_slot, int nr)
1637
1638{
1639 u64 bytenr;
1640 u64 ref_root;
1641 u64 orig_root;
1642 u64 ref_generation;
1643 u64 orig_generation;
1644 struct btrfs_key key;
1645 struct btrfs_file_extent_item *fi;
1646 int i;
1647 int ret;
1648 int slot;
1649 int level;
1650
1651 BUG_ON(start_slot < 0);
1652 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1653
1654 ref_root = btrfs_header_owner(buf);
1655 ref_generation = btrfs_header_generation(buf);
1656 orig_root = btrfs_header_owner(orig_buf);
1657 orig_generation = btrfs_header_generation(orig_buf);
1658 level = btrfs_header_level(buf);
1659
1660 if (!root->ref_cows) {
1661 if (level == 0 &&
1662 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1663 return 0;
1664 if (level != 0 &&
1665 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1666 return 0;
1667 }
1668
1669 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1670 cond_resched();
1671 if (level == 0) {
1672 btrfs_item_key_to_cpu(buf, &key, slot);
1673 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1674 continue;
1675 fi = btrfs_item_ptr(buf, slot,
1676 struct btrfs_file_extent_item);
1677 if (btrfs_file_extent_type(buf, fi) ==
1678 BTRFS_FILE_EXTENT_INLINE)
1679 continue;
1680 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1681 if (bytenr == 0)
1682 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001683 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1684 orig_buf->start, buf->start,
1685 orig_root, ref_root,
1686 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001687 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001688 if (ret)
1689 goto fail;
1690 } else {
1691 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001692 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1693 orig_buf->start, buf->start,
1694 orig_root, ref_root,
1695 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001696 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001697 if (ret)
1698 goto fail;
1699 }
1700 }
1701 return 0;
1702fail:
1703 WARN_ON(1);
1704 return -1;
1705}
1706
Chris Mason9078a3e2007-04-26 16:46:15 -04001707static int write_one_cache_group(struct btrfs_trans_handle *trans,
1708 struct btrfs_root *root,
1709 struct btrfs_path *path,
1710 struct btrfs_block_group_cache *cache)
1711{
1712 int ret;
1713 int pending_ret;
1714 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001715 unsigned long bi;
1716 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001717
Chris Mason9078a3e2007-04-26 16:46:15 -04001718 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001719 if (ret < 0)
1720 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001721 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001722
1723 leaf = path->nodes[0];
1724 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1725 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1726 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001727 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001728fail:
Chris Mason87ef2bb2008-10-30 11:23:27 -04001729 finish_current_insert(trans, extent_root, 0);
1730 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001731 if (ret)
1732 return ret;
1733 if (pending_ret)
1734 return pending_ret;
1735 return 0;
1736
1737}
1738
Chris Mason96b51792007-10-15 16:15:19 -04001739int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1740 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001741{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001742 struct btrfs_block_group_cache *cache, *entry;
1743 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001744 int err = 0;
1745 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001746 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001747 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001748
1749 path = btrfs_alloc_path();
1750 if (!path)
1751 return -ENOMEM;
1752
1753 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001754 cache = NULL;
1755 spin_lock(&root->fs_info->block_group_cache_lock);
1756 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1757 n; n = rb_next(n)) {
1758 entry = rb_entry(n, struct btrfs_block_group_cache,
1759 cache_node);
1760 if (entry->dirty) {
1761 cache = entry;
1762 break;
1763 }
1764 }
1765 spin_unlock(&root->fs_info->block_group_cache_lock);
1766
1767 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001768 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001769
Zheng Yane8569812008-09-26 10:05:48 -04001770 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001771 last += cache->key.offset;
1772
Chris Mason96b51792007-10-15 16:15:19 -04001773 err = write_one_cache_group(trans, root,
1774 path, cache);
1775 /*
1776 * if we fail to write the cache group, we want
1777 * to keep it marked dirty in hopes that a later
1778 * write will work
1779 */
1780 if (err) {
1781 werr = err;
1782 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001783 }
1784 }
1785 btrfs_free_path(path);
1786 return werr;
1787}
1788
Chris Mason593060d2008-03-25 16:50:33 -04001789static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1790 u64 total_bytes, u64 bytes_used,
1791 struct btrfs_space_info **space_info)
1792{
1793 struct btrfs_space_info *found;
1794
1795 found = __find_space_info(info, flags);
1796 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001797 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001798 found->total_bytes += total_bytes;
1799 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001800 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001801 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001802 *space_info = found;
1803 return 0;
1804 }
Yan Zhengc146afa2008-11-12 14:34:12 -05001805 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04001806 if (!found)
1807 return -ENOMEM;
1808
1809 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001810 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001811 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001812 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001813 found->flags = flags;
1814 found->total_bytes = total_bytes;
1815 found->bytes_used = bytes_used;
1816 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001817 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05001818 found->bytes_readonly = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001819 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001820 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001821 *space_info = found;
1822 return 0;
1823}
1824
Chris Mason8790d502008-04-03 16:29:03 -04001825static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1826{
1827 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001828 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001829 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001830 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001831 if (extra_flags) {
1832 if (flags & BTRFS_BLOCK_GROUP_DATA)
1833 fs_info->avail_data_alloc_bits |= extra_flags;
1834 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1835 fs_info->avail_metadata_alloc_bits |= extra_flags;
1836 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1837 fs_info->avail_system_alloc_bits |= extra_flags;
1838 }
1839}
Chris Mason593060d2008-03-25 16:50:33 -04001840
Yan Zhengc146afa2008-11-12 14:34:12 -05001841static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1842{
1843 spin_lock(&cache->space_info->lock);
1844 spin_lock(&cache->lock);
1845 if (!cache->ro) {
1846 cache->space_info->bytes_readonly += cache->key.offset -
1847 btrfs_block_group_used(&cache->item);
1848 cache->ro = 1;
1849 }
1850 spin_unlock(&cache->lock);
1851 spin_unlock(&cache->space_info->lock);
1852}
1853
Yan Zheng2b820322008-11-17 21:11:30 -05001854u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001855{
Yan Zheng2b820322008-11-17 21:11:30 -05001856 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001857
1858 if (num_devices == 1)
1859 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1860 if (num_devices < 4)
1861 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1862
Chris Masonec44a352008-04-28 15:29:52 -04001863 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1864 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001865 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001866 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001867 }
Chris Masonec44a352008-04-28 15:29:52 -04001868
1869 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001870 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001871 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001872 }
Chris Masonec44a352008-04-28 15:29:52 -04001873
1874 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1875 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1876 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1877 (flags & BTRFS_BLOCK_GROUP_DUP)))
1878 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1879 return flags;
1880}
1881
Chris Mason6324fbf2008-03-24 15:01:59 -04001882static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1883 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001884 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001885{
1886 struct btrfs_space_info *space_info;
1887 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05001888 int ret = 0;
1889
1890 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001891
Yan Zheng2b820322008-11-17 21:11:30 -05001892 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001893
Chris Mason6324fbf2008-03-24 15:01:59 -04001894 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001895 if (!space_info) {
1896 ret = update_space_info(extent_root->fs_info, flags,
1897 0, 0, &space_info);
1898 BUG_ON(ret);
1899 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001900 BUG_ON(!space_info);
1901
Josef Bacik25179202008-10-29 14:49:05 -04001902 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001903 if (space_info->force_alloc) {
1904 force = 1;
1905 space_info->force_alloc = 0;
1906 }
Josef Bacik25179202008-10-29 14:49:05 -04001907 if (space_info->full) {
1908 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001909 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001910 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001911
Yan Zhengc146afa2008-11-12 14:34:12 -05001912 thresh = space_info->total_bytes - space_info->bytes_readonly;
1913 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001914 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001915 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001916 space_info->bytes_reserved + alloc_bytes) < thresh) {
1917 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001918 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001919 }
Josef Bacik25179202008-10-29 14:49:05 -04001920 spin_unlock(&space_info->lock);
1921
Yan Zheng2b820322008-11-17 21:11:30 -05001922 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik25179202008-10-29 14:49:05 -04001923 if (ret) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001924printk("space info full %Lu\n", flags);
1925 space_info->full = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001926 }
Chris Masona74a4b92008-06-25 16:01:31 -04001927out:
Yan Zhengc146afa2008-11-12 14:34:12 -05001928 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001929 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001930}
1931
Chris Mason9078a3e2007-04-26 16:46:15 -04001932static int update_block_group(struct btrfs_trans_handle *trans,
1933 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001934 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001935 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001936{
1937 struct btrfs_block_group_cache *cache;
1938 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001939 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001940 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001941 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001942
Chris Mason9078a3e2007-04-26 16:46:15 -04001943 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001944 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001945 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001946 return -1;
Chris Masondb945352007-10-15 16:15:53 -04001947 byte_in_group = bytenr - cache->key.objectid;
1948 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001949
Josef Bacik25179202008-10-29 14:49:05 -04001950 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001951 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001952 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001953 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001954 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001955 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001956 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001957 cache->space_info->bytes_used += num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001958 if (cache->ro) {
1959 cache->space_info->bytes_readonly -= num_bytes;
1960 WARN_ON(1);
1961 }
Chris Masonc286ac42008-07-22 23:06:41 -04001962 btrfs_set_block_group_used(&cache->item, old_val);
1963 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001964 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001965 } else {
Chris Masondb945352007-10-15 16:15:53 -04001966 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001967 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001968 if (cache->ro)
1969 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001970 btrfs_set_block_group_used(&cache->item, old_val);
1971 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001972 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001973 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001974 int ret;
1975 ret = btrfs_add_free_space(cache, bytenr,
1976 num_bytes);
1977 if (ret)
1978 return -1;
Chris Masone37c9e62007-05-09 20:13:14 -04001979 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001980 }
Chris Masondb945352007-10-15 16:15:53 -04001981 total -= num_bytes;
1982 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001983 }
1984 return 0;
1985}
Chris Mason6324fbf2008-03-24 15:01:59 -04001986
Chris Masona061fc82008-05-07 11:43:44 -04001987static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1988{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001989 struct btrfs_block_group_cache *cache;
1990
1991 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1992 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001993 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001994
1995 return cache->key.objectid;
Chris Masona061fc82008-05-07 11:43:44 -04001996}
1997
Chris Masone02119d2008-09-05 16:13:11 -04001998int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05001999 u64 bytenr, u64 num, int pin)
2000{
2001 u64 len;
2002 struct btrfs_block_group_cache *cache;
2003 struct btrfs_fs_info *fs_info = root->fs_info;
2004
Josef Bacik25179202008-10-29 14:49:05 -04002005 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05002006 if (pin) {
2007 set_extent_dirty(&fs_info->pinned_extents,
2008 bytenr, bytenr + num - 1, GFP_NOFS);
2009 } else {
2010 clear_extent_dirty(&fs_info->pinned_extents,
2011 bytenr, bytenr + num - 1, GFP_NOFS);
2012 }
2013 while (num > 0) {
2014 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002015 BUG_ON(!cache);
2016 len = min(num, cache->key.offset -
2017 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002018 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002019 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002020 spin_lock(&cache->lock);
2021 cache->pinned += len;
2022 cache->space_info->bytes_pinned += len;
2023 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002024 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002025 fs_info->total_pinned += len;
2026 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002027 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002028 spin_lock(&cache->lock);
2029 cache->pinned -= len;
2030 cache->space_info->bytes_pinned -= len;
2031 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002032 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002033 fs_info->total_pinned -= len;
2034 }
2035 bytenr += len;
2036 num -= len;
2037 }
2038 return 0;
2039}
Chris Mason9078a3e2007-04-26 16:46:15 -04002040
Zheng Yane8569812008-09-26 10:05:48 -04002041static int update_reserved_extents(struct btrfs_root *root,
2042 u64 bytenr, u64 num, int reserve)
2043{
2044 u64 len;
2045 struct btrfs_block_group_cache *cache;
2046 struct btrfs_fs_info *fs_info = root->fs_info;
2047
Zheng Yane8569812008-09-26 10:05:48 -04002048 while (num > 0) {
2049 cache = btrfs_lookup_block_group(fs_info, bytenr);
2050 BUG_ON(!cache);
2051 len = min(num, cache->key.offset -
2052 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04002053
2054 spin_lock(&cache->space_info->lock);
2055 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002056 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04002057 cache->reserved += len;
2058 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04002059 } else {
Zheng Yane8569812008-09-26 10:05:48 -04002060 cache->reserved -= len;
2061 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04002062 }
Josef Bacik25179202008-10-29 14:49:05 -04002063 spin_unlock(&cache->lock);
2064 spin_unlock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002065 bytenr += len;
2066 num -= len;
2067 }
2068 return 0;
2069}
2070
Chris Masond1310b22008-01-24 16:13:08 -05002071int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04002072{
Chris Masonccd467d2007-06-28 15:57:36 -04002073 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002074 u64 start;
2075 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002076 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04002077 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04002078
Josef Bacik25179202008-10-29 14:49:05 -04002079 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002080 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002081 ret = find_first_extent_bit(pinned_extents, last,
2082 &start, &end, EXTENT_DIRTY);
2083 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04002084 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04002085 set_extent_dirty(copy, start, end, GFP_NOFS);
2086 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04002087 }
Josef Bacik25179202008-10-29 14:49:05 -04002088 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002089 return 0;
2090}
2091
2092int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2093 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05002094 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05002095{
Chris Mason1a5bc162007-10-15 16:15:26 -04002096 u64 start;
2097 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002098 int ret;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002099 struct btrfs_block_group_cache *cache;
Chris Masona28ec192007-03-06 20:08:01 -05002100
Josef Bacik25179202008-10-29 14:49:05 -04002101 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002102 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002103 ret = find_first_extent_bit(unpin, 0, &start, &end,
2104 EXTENT_DIRTY);
2105 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05002106 break;
Chris Masone02119d2008-09-05 16:13:11 -04002107 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04002108 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002109 cache = btrfs_lookup_block_group(root->fs_info, start);
2110 if (cache->cached)
2111 btrfs_add_free_space(cache, start, end - start + 1);
Chris Masonc286ac42008-07-22 23:06:41 -04002112 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04002113 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002114 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04002115 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002116 }
Chris Masona28ec192007-03-06 20:08:01 -05002117 }
Josef Bacik25179202008-10-29 14:49:05 -04002118 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002119 return 0;
2120}
2121
Chris Mason98ed5172008-01-03 10:01:48 -05002122static int finish_current_insert(struct btrfs_trans_handle *trans,
Chris Mason87ef2bb2008-10-30 11:23:27 -04002123 struct btrfs_root *extent_root, int all)
Chris Mason037e6392007-03-07 11:50:24 -05002124{
Chris Mason7bb86312007-12-11 09:25:06 -05002125 u64 start;
2126 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002127 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002128 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002129 u64 skipped = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002130 struct btrfs_fs_info *info = extent_root->fs_info;
2131 struct btrfs_path *path;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002132 struct pending_extent_op *extent_op, *tmp;
2133 struct list_head insert_list, update_list;
Chris Mason037e6392007-03-07 11:50:24 -05002134 int ret;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002135 int num_inserts = 0, max_inserts;
Chris Mason037e6392007-03-07 11:50:24 -05002136
Chris Mason7bb86312007-12-11 09:25:06 -05002137 path = btrfs_alloc_path();
Josef Bacikf3465ca2008-11-12 14:19:50 -05002138 INIT_LIST_HEAD(&insert_list);
2139 INIT_LIST_HEAD(&update_list);
Chris Mason037e6392007-03-07 11:50:24 -05002140
Josef Bacikf3465ca2008-11-12 14:19:50 -05002141 max_inserts = extent_root->leafsize /
2142 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2143 sizeof(struct btrfs_extent_ref) +
2144 sizeof(struct btrfs_extent_item));
2145again:
2146 mutex_lock(&info->extent_ins_mutex);
2147 while (1) {
Josef Bacik25179202008-10-29 14:49:05 -04002148 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2149 &end, EXTENT_WRITEBACK);
2150 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002151 if (skipped && all && !num_inserts) {
2152 skipped = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002153 continue;
2154 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002155 mutex_unlock(&info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04002156 break;
Josef Bacik25179202008-10-29 14:49:05 -04002157 }
2158
2159 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2160 if (!ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002161 skipped = 1;
Chris Mason87ef2bb2008-10-30 11:23:27 -04002162 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002163 if (need_resched()) {
2164 mutex_unlock(&info->extent_ins_mutex);
2165 cond_resched();
2166 mutex_lock(&info->extent_ins_mutex);
2167 }
Josef Bacik25179202008-10-29 14:49:05 -04002168 continue;
2169 }
Chris Mason26b80032007-08-08 20:17:12 -04002170
Zheng Yan31840ae2008-09-23 13:14:14 -04002171 ret = get_state_private(&info->extent_ins, start, &priv);
2172 BUG_ON(ret);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002173 extent_op = (struct pending_extent_op *)(unsigned long) priv;
Josef Bacik25179202008-10-29 14:49:05 -04002174
Zheng Yan31840ae2008-09-23 13:14:14 -04002175 if (extent_op->type == PENDING_EXTENT_INSERT) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002176 num_inserts++;
2177 list_add_tail(&extent_op->list, &insert_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002178 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002179 if (num_inserts == max_inserts) {
2180 mutex_unlock(&info->extent_ins_mutex);
2181 break;
2182 }
2183 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2184 list_add_tail(&extent_op->list, &update_list);
2185 search = end + 1;
2186 } else {
2187 BUG();
2188 }
Chris Mason037e6392007-03-07 11:50:24 -05002189 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002190
2191 /*
2192 * process teh update list, clear the writeback bit for it, and if
2193 * somebody marked this thing for deletion then just unlock it and be
2194 * done, the free_extents will handle it
2195 */
2196 mutex_lock(&info->extent_ins_mutex);
2197 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2198 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2199 extent_op->bytenr + extent_op->num_bytes - 1,
2200 EXTENT_WRITEBACK, GFP_NOFS);
2201 if (extent_op->del) {
2202 list_del_init(&extent_op->list);
2203 unlock_extent(&info->extent_ins, extent_op->bytenr,
2204 extent_op->bytenr + extent_op->num_bytes
2205 - 1, GFP_NOFS);
2206 kfree(extent_op);
2207 }
2208 }
2209 mutex_unlock(&info->extent_ins_mutex);
2210
2211 /*
2212 * still have things left on the update list, go ahead an update
2213 * everything
2214 */
2215 if (!list_empty(&update_list)) {
2216 ret = update_backrefs(trans, extent_root, path, &update_list);
2217 BUG_ON(ret);
2218 }
2219
2220 /*
2221 * if no inserts need to be done, but we skipped some extents and we
2222 * need to make sure everything is cleaned then reset everything and
2223 * go back to the beginning
2224 */
2225 if (!num_inserts && all && skipped) {
2226 search = 0;
2227 skipped = 0;
2228 INIT_LIST_HEAD(&update_list);
2229 INIT_LIST_HEAD(&insert_list);
2230 goto again;
2231 } else if (!num_inserts) {
2232 goto out;
2233 }
2234
2235 /*
2236 * process the insert extents list. Again if we are deleting this
2237 * extent, then just unlock it, pin down the bytes if need be, and be
2238 * done with it. Saves us from having to actually insert the extent
2239 * into the tree and then subsequently come along and delete it
2240 */
2241 mutex_lock(&info->extent_ins_mutex);
2242 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2243 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2244 extent_op->bytenr + extent_op->num_bytes - 1,
2245 EXTENT_WRITEBACK, GFP_NOFS);
2246 if (extent_op->del) {
2247 list_del_init(&extent_op->list);
2248 unlock_extent(&info->extent_ins, extent_op->bytenr,
2249 extent_op->bytenr + extent_op->num_bytes
2250 - 1, GFP_NOFS);
2251
2252 mutex_lock(&extent_root->fs_info->pinned_mutex);
2253 ret = pin_down_bytes(trans, extent_root,
2254 extent_op->bytenr,
2255 extent_op->num_bytes, 0);
2256 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2257
2258 ret = update_block_group(trans, extent_root,
2259 extent_op->bytenr,
2260 extent_op->num_bytes,
2261 0, ret > 0);
2262 BUG_ON(ret);
2263 kfree(extent_op);
2264 num_inserts--;
2265 }
2266 }
2267 mutex_unlock(&info->extent_ins_mutex);
2268
2269 ret = insert_extents(trans, extent_root, path, &insert_list,
2270 num_inserts);
2271 BUG_ON(ret);
2272
2273 /*
2274 * if we broke out of the loop in order to insert stuff because we hit
2275 * the maximum number of inserts at a time we can handle, then loop
2276 * back and pick up where we left off
2277 */
2278 if (num_inserts == max_inserts) {
2279 INIT_LIST_HEAD(&insert_list);
2280 INIT_LIST_HEAD(&update_list);
2281 num_inserts = 0;
2282 goto again;
2283 }
2284
2285 /*
2286 * again, if we need to make absolutely sure there are no more pending
2287 * extent operations left and we know that we skipped some, go back to
2288 * the beginning and do it all again
2289 */
2290 if (all && skipped) {
2291 INIT_LIST_HEAD(&insert_list);
2292 INIT_LIST_HEAD(&update_list);
2293 search = 0;
2294 skipped = 0;
2295 num_inserts = 0;
2296 goto again;
2297 }
2298out:
Chris Mason7bb86312007-12-11 09:25:06 -05002299 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05002300 return 0;
2301}
2302
Zheng Yan31840ae2008-09-23 13:14:14 -04002303static int pin_down_bytes(struct btrfs_trans_handle *trans,
2304 struct btrfs_root *root,
2305 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04002306{
Chris Mason1a5bc162007-10-15 16:15:26 -04002307 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002308 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04002309
Zheng Yan31840ae2008-09-23 13:14:14 -04002310 if (is_data)
2311 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002312
Zheng Yan31840ae2008-09-23 13:14:14 -04002313 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2314 if (!buf)
2315 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002316
Zheng Yan31840ae2008-09-23 13:14:14 -04002317 /* we can reuse a block if it hasn't been written
2318 * and it is from this transaction. We can't
2319 * reuse anything from the tree log root because
2320 * it has tiny sub-transactions.
2321 */
2322 if (btrfs_buffer_uptodate(buf, 0) &&
2323 btrfs_try_tree_lock(buf)) {
2324 u64 header_owner = btrfs_header_owner(buf);
2325 u64 header_transid = btrfs_header_generation(buf);
2326 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04002327 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04002328 header_transid == trans->transid &&
2329 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2330 clean_tree_block(NULL, root, buf);
2331 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002332 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002333 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04002334 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002335 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04002336 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002337 free_extent_buffer(buf);
2338pinit:
2339 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2340
Chris Masonbe744172007-05-06 10:15:01 -04002341 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002342 return 0;
2343}
2344
Chris Masona28ec192007-03-06 20:08:01 -05002345/*
2346 * remove an extent from the root, returns 0 on success
2347 */
Zheng Yan31840ae2008-09-23 13:14:14 -04002348static int __free_extent(struct btrfs_trans_handle *trans,
2349 struct btrfs_root *root,
2350 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002351 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002352 u64 owner_objectid, int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05002353{
Chris Mason5caf2a02007-04-02 11:20:42 -04002354 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04002355 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04002356 struct btrfs_fs_info *info = root->fs_info;
2357 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002358 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05002359 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05002360 int extent_slot = 0;
2361 int found_extent = 0;
2362 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04002363 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04002364 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05002365
Chris Masondb945352007-10-15 16:15:53 -04002366 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04002367 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04002368 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04002369 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002370 if (!path)
2371 return -ENOMEM;
2372
Chris Mason3c12ac72008-04-21 12:01:38 -04002373 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002374 ret = lookup_extent_backref(trans, extent_root, path,
2375 bytenr, parent, root_objectid,
2376 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05002377 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002378 struct btrfs_key found_key;
2379 extent_slot = path->slots[0];
2380 while(extent_slot > 0) {
2381 extent_slot--;
2382 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2383 extent_slot);
2384 if (found_key.objectid != bytenr)
2385 break;
2386 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2387 found_key.offset == num_bytes) {
2388 found_extent = 1;
2389 break;
2390 }
2391 if (path->slots[0] - extent_slot > 5)
2392 break;
2393 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002394 if (!found_extent) {
2395 ret = remove_extent_backref(trans, extent_root, path);
2396 BUG_ON(ret);
2397 btrfs_release_path(extent_root, path);
2398 ret = btrfs_search_slot(trans, extent_root,
2399 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002400 if (ret) {
2401 printk(KERN_ERR "umm, got %d back from search"
2402 ", was looking for %Lu\n", ret,
2403 bytenr);
2404 btrfs_print_leaf(extent_root, path->nodes[0]);
2405 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002406 BUG_ON(ret);
2407 extent_slot = path->slots[0];
2408 }
Chris Mason7bb86312007-12-11 09:25:06 -05002409 } else {
2410 btrfs_print_leaf(extent_root, path->nodes[0]);
2411 WARN_ON(1);
2412 printk("Unable to find ref byte nr %Lu root %Lu "
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002413 "gen %Lu owner %Lu\n", bytenr,
2414 root_objectid, ref_generation, owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05002415 }
Chris Mason5f39d392007-10-15 16:14:19 -04002416
2417 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05002418 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04002419 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002420 refs = btrfs_extent_refs(leaf, ei);
2421 BUG_ON(refs == 0);
2422 refs -= 1;
2423 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05002424
Chris Mason5f39d392007-10-15 16:14:19 -04002425 btrfs_mark_buffer_dirty(leaf);
2426
Chris Mason952fcca2008-02-18 16:33:44 -05002427 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002428 struct btrfs_extent_ref *ref;
2429 ref = btrfs_item_ptr(leaf, path->slots[0],
2430 struct btrfs_extent_ref);
2431 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002432 /* if the back ref and the extent are next to each other
2433 * they get deleted below in one shot
2434 */
2435 path->slots[0] = extent_slot;
2436 num_to_del = 2;
2437 } else if (found_extent) {
2438 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002439 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05002440 BUG_ON(ret);
2441 /* if refs are 0, we need to setup the path for deletion */
2442 if (refs == 0) {
2443 btrfs_release_path(extent_root, path);
2444 ret = btrfs_search_slot(trans, extent_root, &key, path,
2445 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002446 BUG_ON(ret);
2447 }
2448 }
2449
Chris Masoncf27e1e2007-03-13 09:49:06 -04002450 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002451 u64 super_used;
2452 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01002453#ifdef BIO_RW_DISCARD
2454 u64 map_length = num_bytes;
2455 struct btrfs_multi_bio *multi = NULL;
2456#endif
Chris Mason78fae272007-03-25 11:35:08 -04002457
2458 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002459 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002460 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2461 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04002462 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05002463 if (ret > 0)
2464 mark_free = 1;
2465 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04002466 }
2467
Josef Bacik58176a92007-08-29 15:47:34 -04002468 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002469 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002470 super_used = btrfs_super_bytes_used(&info->super_copy);
2471 btrfs_set_super_bytes_used(&info->super_copy,
2472 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002473 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04002474
2475 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002476 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002477 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04002478 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05002479 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2480 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04002481 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04002482 btrfs_release_path(extent_root, path);
Chris Masondb945352007-10-15 16:15:53 -04002483 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002484 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04002485 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01002486
2487#ifdef BIO_RW_DISCARD
2488 /* Tell the block device(s) that the sectors can be discarded */
2489 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2490 bytenr, &map_length, &multi, 0);
2491 if (!ret) {
2492 struct btrfs_bio_stripe *stripe = multi->stripes;
2493 int i;
2494
2495 if (map_length > num_bytes)
2496 map_length = num_bytes;
2497
2498 for (i = 0; i < multi->num_stripes; i++, stripe++) {
2499 blkdev_issue_discard(stripe->dev->bdev,
2500 stripe->physical >> 9,
2501 map_length >> 9);
2502 }
2503 kfree(multi);
2504 }
2505#endif
Chris Masona28ec192007-03-06 20:08:01 -05002506 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002507 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002508 finish_current_insert(trans, extent_root, 0);
Chris Masona28ec192007-03-06 20:08:01 -05002509 return ret;
2510}
2511
2512/*
Chris Masonfec577f2007-02-26 10:40:21 -05002513 * find all the blocks marked as pending in the radix tree and remove
2514 * them from the extent map
2515 */
Chris Masone089f052007-03-16 16:20:31 -04002516static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -04002517 btrfs_root *extent_root, int all)
Chris Masonfec577f2007-02-26 10:40:21 -05002518{
2519 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002520 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002521 u64 start;
2522 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002523 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002524 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002525 int nr = 0, skipped = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002526 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002527 struct extent_io_tree *extent_ins;
2528 struct pending_extent_op *extent_op;
Josef Bacik25179202008-10-29 14:49:05 -04002529 struct btrfs_fs_info *info = extent_root->fs_info;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002530 struct list_head delete_list;
Chris Mason8ef97622007-03-26 10:15:30 -04002531
Josef Bacikf3465ca2008-11-12 14:19:50 -05002532 INIT_LIST_HEAD(&delete_list);
Zheng Yan31840ae2008-09-23 13:14:14 -04002533 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc162007-10-15 16:15:26 -04002534 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002535
Josef Bacikf3465ca2008-11-12 14:19:50 -05002536again:
2537 mutex_lock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002538 while(1) {
Josef Bacik25179202008-10-29 14:49:05 -04002539 ret = find_first_extent_bit(pending_del, search, &start, &end,
2540 EXTENT_WRITEBACK);
2541 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002542 if (all && skipped && !nr) {
Josef Bacik25179202008-10-29 14:49:05 -04002543 search = 0;
2544 continue;
2545 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002546 mutex_unlock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002547 break;
Josef Bacik25179202008-10-29 14:49:05 -04002548 }
2549
2550 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2551 if (!ret) {
2552 search = end+1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002553 skipped = 1;
2554
2555 if (need_resched()) {
2556 mutex_unlock(&info->extent_ins_mutex);
2557 cond_resched();
2558 mutex_lock(&info->extent_ins_mutex);
2559 }
2560
Josef Bacik25179202008-10-29 14:49:05 -04002561 continue;
2562 }
2563 BUG_ON(ret < 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002564
2565 ret = get_state_private(pending_del, start, &priv);
2566 BUG_ON(ret);
2567 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2568
Josef Bacik25179202008-10-29 14:49:05 -04002569 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
Chris Mason1a5bc162007-10-15 16:15:26 -04002570 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002571 if (!test_range_bit(extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04002572 EXTENT_WRITEBACK, 0)) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002573 list_add_tail(&extent_op->list, &delete_list);
2574 nr++;
Chris Masonc286ac42008-07-22 23:06:41 -04002575 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002576 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002577
2578 ret = get_state_private(&info->extent_ins, start,
2579 &priv);
Zheng Yan31840ae2008-09-23 13:14:14 -04002580 BUG_ON(ret);
2581 extent_op = (struct pending_extent_op *)
Josef Bacik25179202008-10-29 14:49:05 -04002582 (unsigned long)priv;
Zheng Yan31840ae2008-09-23 13:14:14 -04002583
Josef Bacik25179202008-10-29 14:49:05 -04002584 clear_extent_bits(&info->extent_ins, start, end,
2585 EXTENT_WRITEBACK, GFP_NOFS);
2586
Josef Bacikf3465ca2008-11-12 14:19:50 -05002587 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2588 list_add_tail(&extent_op->list, &delete_list);
2589 search = end + 1;
2590 nr++;
2591 continue;
2592 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002593
Josef Bacik25179202008-10-29 14:49:05 -04002594 mutex_lock(&extent_root->fs_info->pinned_mutex);
2595 ret = pin_down_bytes(trans, extent_root, start,
2596 end + 1 - start, 0);
2597 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2598
Zheng Yan31840ae2008-09-23 13:14:14 -04002599 ret = update_block_group(trans, extent_root, start,
Josef Bacik25179202008-10-29 14:49:05 -04002600 end + 1 - start, 0, ret > 0);
2601
Josef Bacikf3465ca2008-11-12 14:19:50 -05002602 unlock_extent(extent_ins, start, end, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002603 BUG_ON(ret);
2604 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002605 }
Chris Mason1a5bc162007-10-15 16:15:26 -04002606 if (ret)
2607 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04002608
Josef Bacikf3465ca2008-11-12 14:19:50 -05002609 search = end + 1;
2610
2611 if (need_resched()) {
2612 mutex_unlock(&info->extent_ins_mutex);
2613 cond_resched();
2614 mutex_lock(&info->extent_ins_mutex);
2615 }
Chris Masonfec577f2007-02-26 10:40:21 -05002616 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002617
2618 if (nr) {
2619 ret = free_extents(trans, extent_root, &delete_list);
2620 BUG_ON(ret);
2621 }
2622
2623 if (all && skipped) {
2624 INIT_LIST_HEAD(&delete_list);
2625 search = 0;
2626 nr = 0;
2627 goto again;
2628 }
2629
Chris Masone20d96d2007-03-22 12:13:20 -04002630 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002631}
2632
2633/*
2634 * remove an extent from the root, returns 0 on success
2635 */
Chris Mason925baed2008-06-25 16:01:30 -04002636static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002637 struct btrfs_root *root,
2638 u64 bytenr, u64 num_bytes, u64 parent,
2639 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002640 u64 owner_objectid, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002641{
Chris Mason9f5fae22007-03-20 14:38:32 -04002642 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002643 int pending_ret;
2644 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002645
Chris Masondb945352007-10-15 16:15:53 -04002646 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002647 if (root == extent_root) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002648 struct pending_extent_op *extent_op = NULL;
2649
2650 mutex_lock(&root->fs_info->extent_ins_mutex);
2651 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2652 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2653 u64 priv;
2654 ret = get_state_private(&root->fs_info->extent_ins,
2655 bytenr, &priv);
2656 BUG_ON(ret);
2657 extent_op = (struct pending_extent_op *)
2658 (unsigned long)priv;
2659
2660 extent_op->del = 1;
2661 if (extent_op->type == PENDING_EXTENT_INSERT) {
2662 mutex_unlock(&root->fs_info->extent_ins_mutex);
2663 return 0;
2664 }
2665 }
2666
2667 if (extent_op) {
2668 ref_generation = extent_op->orig_generation;
2669 parent = extent_op->orig_parent;
2670 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002671
2672 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2673 BUG_ON(!extent_op);
2674
2675 extent_op->type = PENDING_EXTENT_DELETE;
2676 extent_op->bytenr = bytenr;
2677 extent_op->num_bytes = num_bytes;
2678 extent_op->parent = parent;
2679 extent_op->orig_parent = parent;
2680 extent_op->generation = ref_generation;
2681 extent_op->orig_generation = ref_generation;
2682 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002683 INIT_LIST_HEAD(&extent_op->list);
2684 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002685
2686 set_extent_bits(&root->fs_info->pending_del,
2687 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04002688 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002689 set_state_private(&root->fs_info->pending_del,
2690 bytenr, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002691 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002692 return 0;
2693 }
Chris Mason4bef0842008-09-08 11:18:08 -04002694 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002695 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2696 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002697 struct btrfs_block_group_cache *cache;
2698
Chris Masond00aff02008-09-11 15:54:42 -04002699 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002700 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2701 BUG_ON(!cache);
2702 btrfs_add_free_space(cache, bytenr, num_bytes);
Zheng Yane8569812008-09-26 10:05:48 -04002703 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002704 return 0;
2705 }
Chris Mason4bef0842008-09-08 11:18:08 -04002706 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002707 }
Chris Mason4bef0842008-09-08 11:18:08 -04002708
2709 /* if data pin when any transaction has committed this */
2710 if (ref_generation != trans->transid)
2711 pin = 1;
2712
Zheng Yan31840ae2008-09-23 13:14:14 -04002713 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002714 root_objectid, ref_generation,
2715 owner_objectid, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002716
Chris Mason87ef2bb2008-10-30 11:23:27 -04002717 finish_current_insert(trans, root->fs_info->extent_root, 0);
2718 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002719 return ret ? ret : pending_ret;
2720}
2721
Chris Mason925baed2008-06-25 16:01:30 -04002722int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002723 struct btrfs_root *root,
2724 u64 bytenr, u64 num_bytes, u64 parent,
2725 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002726 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002727{
2728 int ret;
2729
Zheng Yan31840ae2008-09-23 13:14:14 -04002730 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002731 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002732 owner_objectid, pin);
Chris Mason925baed2008-06-25 16:01:30 -04002733 return ret;
2734}
2735
Chris Mason87ee04e2007-11-30 11:30:34 -05002736static u64 stripe_align(struct btrfs_root *root, u64 val)
2737{
2738 u64 mask = ((u64)root->stripesize - 1);
2739 u64 ret = (val + mask) & ~mask;
2740 return ret;
2741}
2742
Chris Masonfec577f2007-02-26 10:40:21 -05002743/*
2744 * walks the btree of allocated extents and find a hole of a given size.
2745 * The key ins is changed to record the hole:
2746 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002747 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002748 * ins->offset == number of blocks
2749 * Any available blocks before search_start are skipped.
2750 */
Chris Mason98ed5172008-01-03 10:01:48 -05002751static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2752 struct btrfs_root *orig_root,
2753 u64 num_bytes, u64 empty_size,
2754 u64 search_start, u64 search_end,
2755 u64 hint_byte, struct btrfs_key *ins,
2756 u64 exclude_start, u64 exclude_nr,
2757 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002758{
Josef Bacik80eb2342008-10-29 14:49:05 -04002759 int ret = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -04002760 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002761 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002762 u64 *last_ptr = NULL;
Chris Mason43662112008-11-07 09:06:11 -05002763 u64 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002764 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002765 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002766 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002767 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002768 struct list_head *head = NULL, *cur = NULL;
2769 int loop = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002770 int extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002771 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002772
Chris Masondb945352007-10-15 16:15:53 -04002773 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002774 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002775 ins->objectid = 0;
2776 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002777
Chris Mason0ef3e662008-05-24 14:04:53 -04002778 if (orig_root->ref_cows || empty_size)
2779 allowed_chunk_alloc = 1;
2780
Chris Mason239b14b2008-03-24 15:02:07 -04002781 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2782 last_ptr = &root->fs_info->last_alloc;
Chris Mason43662112008-11-07 09:06:11 -05002783 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002784 }
2785
Josef Bacik0f9dd462008-09-23 13:14:11 -04002786 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002787 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002788
Chris Mason239b14b2008-03-24 15:02:07 -04002789 if (last_ptr) {
Chris Mason43662112008-11-07 09:06:11 -05002790 if (*last_ptr) {
Chris Mason239b14b2008-03-24 15:02:07 -04002791 hint_byte = *last_ptr;
Chris Mason43662112008-11-07 09:06:11 -05002792 last_wanted = *last_ptr;
2793 } else
Chris Mason239b14b2008-03-24 15:02:07 -04002794 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002795 } else {
2796 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002797 }
Chris Masona061fc82008-05-07 11:43:44 -04002798 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002799 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04002800
Chris Mason42e70e72008-11-07 18:17:11 -05002801 if (last_wanted && search_start != last_wanted) {
Chris Mason43662112008-11-07 09:06:11 -05002802 last_wanted = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002803 empty_size += empty_cluster;
2804 }
Chris Mason43662112008-11-07 09:06:11 -05002805
Chris Mason42e70e72008-11-07 18:17:11 -05002806 total_needed += empty_size;
Josef Bacik80eb2342008-10-29 14:49:05 -04002807 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
Yan Zhengd899e052008-10-30 14:25:28 -04002808 if (!block_group)
2809 block_group = btrfs_lookup_first_block_group(root->fs_info,
2810 search_start);
Josef Bacik80eb2342008-10-29 14:49:05 -04002811 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002812
Josef Bacik80eb2342008-10-29 14:49:05 -04002813 down_read(&space_info->groups_sem);
2814 while (1) {
2815 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002816 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002817 * the only way this happens if our hint points to a block
2818 * group thats not of the proper type, while looping this
2819 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002820 */
Chris Mason8a1413a2008-11-10 16:13:54 -05002821 if (empty_size)
2822 extra_loop = 1;
2823
Chris Mason42e70e72008-11-07 18:17:11 -05002824 if (!block_group)
2825 goto new_group_no_lock;
2826
Josef Bacik25179202008-10-29 14:49:05 -04002827 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002828 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002829 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002830
Josef Bacik80eb2342008-10-29 14:49:05 -04002831 ret = cache_block_group(root, block_group);
Josef Bacik25179202008-10-29 14:49:05 -04002832 if (ret) {
2833 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002834 break;
Josef Bacik25179202008-10-29 14:49:05 -04002835 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002836
Josef Bacik80eb2342008-10-29 14:49:05 -04002837 if (block_group->ro)
2838 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002839
Josef Bacik80eb2342008-10-29 14:49:05 -04002840 free_space = btrfs_find_free_space(block_group, search_start,
2841 total_needed);
2842 if (free_space) {
2843 u64 start = block_group->key.objectid;
2844 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002845 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002846
Josef Bacik80eb2342008-10-29 14:49:05 -04002847 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002848
Josef Bacik80eb2342008-10-29 14:49:05 -04002849 /* move on to the next group */
2850 if (search_start + num_bytes >= search_end)
2851 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002852
Josef Bacik80eb2342008-10-29 14:49:05 -04002853 /* move on to the next group */
2854 if (search_start + num_bytes > end)
2855 goto new_group;
2856
Chris Mason43662112008-11-07 09:06:11 -05002857 if (last_wanted && search_start != last_wanted) {
Chris Mason3b7885b2008-11-06 21:48:27 -05002858 total_needed += empty_cluster;
Chris Mason8a1413a2008-11-10 16:13:54 -05002859 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002860 last_wanted = 0;
Chris Mason3b7885b2008-11-06 21:48:27 -05002861 /*
2862 * if search_start is still in this block group
2863 * then we just re-search this block group
2864 */
2865 if (search_start >= start &&
2866 search_start < end) {
2867 mutex_unlock(&block_group->alloc_mutex);
2868 continue;
2869 }
2870
2871 /* else we go to the next block group */
2872 goto new_group;
2873 }
2874
Josef Bacik80eb2342008-10-29 14:49:05 -04002875 if (exclude_nr > 0 &&
2876 (search_start + num_bytes > exclude_start &&
2877 search_start < exclude_start + exclude_nr)) {
2878 search_start = exclude_start + exclude_nr;
2879 /*
2880 * if search_start is still in this block group
2881 * then we just re-search this block group
2882 */
2883 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002884 search_start < end) {
2885 mutex_unlock(&block_group->alloc_mutex);
Chris Mason43662112008-11-07 09:06:11 -05002886 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002887 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002888 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002889
2890 /* else we go to the next block group */
2891 goto new_group;
2892 }
2893
2894 ins->objectid = search_start;
2895 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002896
2897 btrfs_remove_free_space_lock(block_group, search_start,
2898 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002899 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002900 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002901 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002902 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002903new_group:
Chris Mason42e70e72008-11-07 18:17:11 -05002904 mutex_unlock(&block_group->alloc_mutex);
2905new_group_no_lock:
Chris Masonf5a31e12008-11-10 11:47:09 -05002906 /* don't try to compare new allocations against the
2907 * last allocation any more
2908 */
Chris Mason43662112008-11-07 09:06:11 -05002909 last_wanted = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002910
Josef Bacik80eb2342008-10-29 14:49:05 -04002911 /*
2912 * Here's how this works.
2913 * loop == 0: we were searching a block group via a hint
2914 * and didn't find anything, so we start at
2915 * the head of the block groups and keep searching
2916 * loop == 1: we're searching through all of the block groups
2917 * if we hit the head again we have searched
2918 * all of the block groups for this space and we
2919 * need to try and allocate, if we cant error out.
2920 * loop == 2: we allocated more space and are looping through
2921 * all of the block groups again.
2922 */
2923 if (loop == 0) {
2924 head = &space_info->block_groups;
2925 cur = head->next;
Josef Bacik80eb2342008-10-29 14:49:05 -04002926 loop++;
2927 } else if (loop == 1 && cur == head) {
Chris Masonf5a31e12008-11-10 11:47:09 -05002928 int keep_going;
Chris Mason42e70e72008-11-07 18:17:11 -05002929
Chris Masonf5a31e12008-11-10 11:47:09 -05002930 /* at this point we give up on the empty_size
2931 * allocations and just try to allocate the min
2932 * space.
2933 *
2934 * The extra_loop field was set if an empty_size
2935 * allocation was attempted above, and if this
2936 * is try we need to try the loop again without
2937 * the additional empty_size.
2938 */
Chris Mason5b7c3fc2008-11-10 07:26:33 -05002939 total_needed -= empty_size;
2940 empty_size = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002941 keep_going = extra_loop;
2942 loop++;
Chris Mason42e70e72008-11-07 18:17:11 -05002943
Josef Bacik80eb2342008-10-29 14:49:05 -04002944 if (allowed_chunk_alloc && !chunk_alloc_done) {
2945 up_read(&space_info->groups_sem);
2946 ret = do_chunk_alloc(trans, root, num_bytes +
2947 2 * 1024 * 1024, data, 1);
Josef Bacik80eb2342008-10-29 14:49:05 -04002948 down_read(&space_info->groups_sem);
Chris Mason2ed6d662008-11-13 09:59:33 -05002949 if (ret < 0)
2950 goto loop_check;
Josef Bacik80eb2342008-10-29 14:49:05 -04002951 head = &space_info->block_groups;
Chris Masonf5a31e12008-11-10 11:47:09 -05002952 /*
2953 * we've allocated a new chunk, keep
2954 * trying
2955 */
2956 keep_going = 1;
Josef Bacik80eb2342008-10-29 14:49:05 -04002957 chunk_alloc_done = 1;
2958 } else if (!allowed_chunk_alloc) {
2959 space_info->force_alloc = 1;
Chris Masonf5a31e12008-11-10 11:47:09 -05002960 }
Chris Mason2ed6d662008-11-13 09:59:33 -05002961loop_check:
Chris Masonf5a31e12008-11-10 11:47:09 -05002962 if (keep_going) {
2963 cur = head->next;
2964 extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002965 } else {
2966 break;
2967 }
2968 } else if (cur == head) {
2969 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002970 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002971
2972 block_group = list_entry(cur, struct btrfs_block_group_cache,
2973 list);
2974 search_start = block_group->key.objectid;
2975 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002976 }
Chris Mason0b86a832008-03-24 15:01:56 -04002977
Josef Bacik80eb2342008-10-29 14:49:05 -04002978 /* we found what we needed */
2979 if (ins->objectid) {
2980 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2981 trans->block_group = block_group;
2982
2983 if (last_ptr)
2984 *last_ptr = ins->objectid + ins->offset;
2985 ret = 0;
2986 } else if (!ret) {
Josef Bacik4ce4cb52008-11-17 21:12:00 -05002987 printk(KERN_ERR "we were searching for %Lu bytes, num_bytes %Lu,"
2988 " loop %d, allowed_alloc %d\n", total_needed, num_bytes,
2989 loop, allowed_chunk_alloc);
Josef Bacik80eb2342008-10-29 14:49:05 -04002990 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04002991 }
Chris Mason0b86a832008-03-24 15:01:56 -04002992
Josef Bacik80eb2342008-10-29 14:49:05 -04002993 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05002994 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002995}
Chris Masonec44a352008-04-28 15:29:52 -04002996
Josef Bacik0f9dd462008-09-23 13:14:11 -04002997static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2998{
2999 struct btrfs_block_group_cache *cache;
3000 struct list_head *l;
3001
3002 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
Zheng Yane8569812008-09-26 10:05:48 -04003003 info->total_bytes - info->bytes_used - info->bytes_pinned -
3004 info->bytes_reserved, (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04003005
Josef Bacik80eb2342008-10-29 14:49:05 -04003006 down_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003007 list_for_each(l, &info->block_groups) {
3008 cache = list_entry(l, struct btrfs_block_group_cache, list);
3009 spin_lock(&cache->lock);
3010 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
Zheng Yane8569812008-09-26 10:05:48 -04003011 "%Lu pinned %Lu reserved\n",
Josef Bacik0f9dd462008-09-23 13:14:11 -04003012 cache->key.objectid, cache->key.offset,
Zheng Yane8569812008-09-26 10:05:48 -04003013 btrfs_block_group_used(&cache->item),
3014 cache->pinned, cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003015 btrfs_dump_free_space(cache, bytes);
3016 spin_unlock(&cache->lock);
3017 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003018 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003019}
Zheng Yane8569812008-09-26 10:05:48 -04003020
Chris Masone6dcd2d2008-07-17 12:53:50 -04003021static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3022 struct btrfs_root *root,
3023 u64 num_bytes, u64 min_alloc_size,
3024 u64 empty_size, u64 hint_byte,
3025 u64 search_end, struct btrfs_key *ins,
3026 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05003027{
3028 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04003029 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003030 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04003031 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003032
Chris Mason6324fbf2008-03-24 15:01:59 -04003033 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04003034 alloc_profile = info->avail_data_alloc_bits &
3035 info->data_alloc_profile;
3036 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003037 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04003038 alloc_profile = info->avail_system_alloc_bits &
3039 info->system_alloc_profile;
3040 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003041 } else {
Chris Mason8790d502008-04-03 16:29:03 -04003042 alloc_profile = info->avail_metadata_alloc_bits &
3043 info->metadata_alloc_profile;
3044 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003045 }
Chris Mason98d20f62008-04-14 09:46:10 -04003046again:
Yan Zheng2b820322008-11-17 21:11:30 -05003047 data = btrfs_reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04003048 /*
3049 * the only place that sets empty_size is btrfs_realloc_node, which
3050 * is not called recursively on allocations
3051 */
3052 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04003053 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04003054 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003055 2 * 1024 * 1024,
3056 BTRFS_BLOCK_GROUP_METADATA |
3057 (info->metadata_alloc_profile &
3058 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003059 }
3060 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003061 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003062 }
Chris Mason0b86a832008-03-24 15:01:56 -04003063
Chris Masondb945352007-10-15 16:15:53 -04003064 WARN_ON(num_bytes < root->sectorsize);
3065 ret = find_free_extent(trans, root, num_bytes, empty_size,
3066 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04003067 trans->alloc_exclude_start,
3068 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04003069
Chris Mason98d20f62008-04-14 09:46:10 -04003070 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3071 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003072 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003073 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04003074 do_chunk_alloc(trans, root->fs_info->extent_root,
3075 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003076 goto again;
3077 }
Chris Masonec44a352008-04-28 15:29:52 -04003078 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003079 struct btrfs_space_info *sinfo;
3080
3081 sinfo = __find_space_info(root->fs_info, data);
3082 printk("allocation failed flags %Lu, wanted %Lu\n",
3083 data, num_bytes);
3084 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04003085 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04003086 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003087
3088 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003089}
3090
Chris Mason65b51a02008-08-01 15:11:20 -04003091int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3092{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003093 struct btrfs_block_group_cache *cache;
3094
Josef Bacik0f9dd462008-09-23 13:14:11 -04003095 cache = btrfs_lookup_block_group(root->fs_info, start);
3096 if (!cache) {
3097 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003098 return -ENOSPC;
3099 }
3100 btrfs_add_free_space(cache, start, len);
Zheng Yan1a40e232008-09-26 10:09:34 -04003101 update_reserved_extents(root, start, len, 0);
Chris Mason65b51a02008-08-01 15:11:20 -04003102 return 0;
3103}
3104
Chris Masone6dcd2d2008-07-17 12:53:50 -04003105int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3106 struct btrfs_root *root,
3107 u64 num_bytes, u64 min_alloc_size,
3108 u64 empty_size, u64 hint_byte,
3109 u64 search_end, struct btrfs_key *ins,
3110 u64 data)
3111{
3112 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003113 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3114 empty_size, hint_byte, search_end, ins,
3115 data);
Zheng Yane8569812008-09-26 10:05:48 -04003116 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003117 return ret;
3118}
3119
3120static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003121 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003122 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003123 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003124{
3125 int ret;
3126 int pending_ret;
3127 u64 super_used;
3128 u64 root_used;
3129 u64 num_bytes = ins->offset;
3130 u32 sizes[2];
3131 struct btrfs_fs_info *info = root->fs_info;
3132 struct btrfs_root *extent_root = info->extent_root;
3133 struct btrfs_extent_item *extent_item;
3134 struct btrfs_extent_ref *ref;
3135 struct btrfs_path *path;
3136 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04003137
Zheng Yan31840ae2008-09-23 13:14:14 -04003138 if (parent == 0)
3139 parent = ins->objectid;
3140
Josef Bacik58176a92007-08-29 15:47:34 -04003141 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04003142 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04003143 super_used = btrfs_super_bytes_used(&info->super_copy);
3144 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04003145 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04003146
Josef Bacik58176a92007-08-29 15:47:34 -04003147 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04003148 root_used = btrfs_root_used(&root->root_item);
3149 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04003150
Chris Mason26b80032007-08-08 20:17:12 -04003151 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003152 struct pending_extent_op *extent_op;
3153
3154 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3155 BUG_ON(!extent_op);
3156
3157 extent_op->type = PENDING_EXTENT_INSERT;
3158 extent_op->bytenr = ins->objectid;
3159 extent_op->num_bytes = ins->offset;
3160 extent_op->parent = parent;
3161 extent_op->orig_parent = 0;
3162 extent_op->generation = ref_generation;
3163 extent_op->orig_generation = 0;
3164 extent_op->level = (int)owner;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003165 INIT_LIST_HEAD(&extent_op->list);
3166 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003167
Josef Bacik25179202008-10-29 14:49:05 -04003168 mutex_lock(&root->fs_info->extent_ins_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04003169 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3170 ins->objectid + ins->offset - 1,
Josef Bacik25179202008-10-29 14:49:05 -04003171 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04003172 set_state_private(&root->fs_info->extent_ins,
3173 ins->objectid, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04003174 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04003175 goto update_block;
3176 }
3177
Chris Mason47e4bb92008-02-01 14:51:59 -05003178 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05003179 keys[1].objectid = ins->objectid;
3180 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04003181 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05003182 sizes[0] = sizeof(*extent_item);
3183 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05003184
3185 path = btrfs_alloc_path();
3186 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05003187
3188 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3189 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04003190 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003191
Chris Mason47e4bb92008-02-01 14:51:59 -05003192 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3193 struct btrfs_extent_item);
3194 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3195 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3196 struct btrfs_extent_ref);
3197
3198 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3199 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3200 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Zheng Yan31840ae2008-09-23 13:14:14 -04003201 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05003202
3203 btrfs_mark_buffer_dirty(path->nodes[0]);
3204
3205 trans->alloc_exclude_start = 0;
3206 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05003207 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04003208 finish_current_insert(trans, extent_root, 0);
3209 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -04003210
Chris Mason925baed2008-06-25 16:01:30 -04003211 if (ret)
3212 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003213 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04003214 ret = pending_ret;
3215 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003216 }
Chris Mason26b80032007-08-08 20:17:12 -04003217
3218update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04003219 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05003220 if (ret) {
3221 printk("update block group failed for %Lu %Lu\n",
3222 ins->objectid, ins->offset);
3223 BUG();
3224 }
Chris Mason925baed2008-06-25 16:01:30 -04003225out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04003226 return ret;
3227}
3228
3229int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003230 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003231 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003232 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003233{
3234 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04003235
3236 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3237 return 0;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003238 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3239 ref_generation, owner, ins);
Zheng Yane8569812008-09-26 10:05:48 -04003240 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003241 return ret;
3242}
Chris Masone02119d2008-09-05 16:13:11 -04003243
3244/*
3245 * this is used by the tree logging recovery code. It records that
3246 * an extent has been allocated and makes sure to clear the free
3247 * space cache bits as well
3248 */
3249int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003250 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04003251 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003252 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04003253{
3254 int ret;
3255 struct btrfs_block_group_cache *block_group;
3256
Chris Masone02119d2008-09-05 16:13:11 -04003257 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik25179202008-10-29 14:49:05 -04003258 mutex_lock(&block_group->alloc_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003259 cache_block_group(root, block_group);
3260
Josef Bacik25179202008-10-29 14:49:05 -04003261 ret = btrfs_remove_free_space_lock(block_group, ins->objectid,
3262 ins->offset);
3263 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003264 BUG_ON(ret);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003265 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3266 ref_generation, owner, ins);
Chris Masone02119d2008-09-05 16:13:11 -04003267 return ret;
3268}
3269
Chris Masone6dcd2d2008-07-17 12:53:50 -04003270/*
3271 * finds a free extent and does all the dirty work required for allocation
3272 * returns the key for the extent through ins, and a tree buffer for
3273 * the first block of the extent through buf.
3274 *
3275 * returns 0 if everything worked, non-zero otherwise.
3276 */
3277int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3278 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003279 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003280 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003281 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003282 u64 search_end, struct btrfs_key *ins, u64 data)
3283{
3284 int ret;
3285
Chris Masone6dcd2d2008-07-17 12:53:50 -04003286 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3287 min_alloc_size, empty_size, hint_byte,
3288 search_end, ins, data);
3289 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04003290 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003291 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3292 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003293 owner_objectid, ins);
Chris Masond00aff02008-09-11 15:54:42 -04003294 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003295
Zheng Yane8569812008-09-26 10:05:48 -04003296 } else {
3297 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04003298 }
Chris Mason925baed2008-06-25 16:01:30 -04003299 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003300}
Chris Mason65b51a02008-08-01 15:11:20 -04003301
3302struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3303 struct btrfs_root *root,
3304 u64 bytenr, u32 blocksize)
3305{
3306 struct extent_buffer *buf;
3307
3308 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3309 if (!buf)
3310 return ERR_PTR(-ENOMEM);
3311 btrfs_set_header_generation(buf, trans->transid);
3312 btrfs_tree_lock(buf);
3313 clean_tree_block(trans, root, buf);
3314 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04003315 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3316 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04003317 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04003318 } else {
3319 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3320 buf->start + buf->len - 1, GFP_NOFS);
3321 }
Chris Mason65b51a02008-08-01 15:11:20 -04003322 trans->blocks_used++;
3323 return buf;
3324}
3325
Chris Masonfec577f2007-02-26 10:40:21 -05003326/*
3327 * helper function to allocate a block for a given tree
3328 * returns the tree buffer or NULL.
3329 */
Chris Mason5f39d392007-10-15 16:14:19 -04003330struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04003331 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003332 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05003333 u64 root_objectid,
3334 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05003335 int level,
3336 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04003337 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05003338{
Chris Masone2fa7222007-03-12 16:22:34 -04003339 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05003340 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003341 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05003342
Zheng Yan31840ae2008-09-23 13:14:14 -04003343 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003344 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04003345 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05003346 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04003347 BUG_ON(ret > 0);
3348 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05003349 }
Chris Mason55c69072008-01-09 15:55:33 -05003350
Chris Mason65b51a02008-08-01 15:11:20 -04003351 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05003352 return buf;
3353}
Chris Masona28ec192007-03-06 20:08:01 -05003354
Chris Masone02119d2008-09-05 16:13:11 -04003355int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3356 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04003357{
Chris Mason7bb86312007-12-11 09:25:06 -05003358 u64 leaf_owner;
3359 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04003360 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04003361 struct btrfs_file_extent_item *fi;
3362 int i;
3363 int nritems;
3364 int ret;
3365
Chris Mason5f39d392007-10-15 16:14:19 -04003366 BUG_ON(!btrfs_is_leaf(leaf));
3367 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05003368 leaf_owner = btrfs_header_owner(leaf);
3369 leaf_generation = btrfs_header_generation(leaf);
3370
Chris Mason6407bf62007-03-27 06:33:00 -04003371 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003372 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04003373 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04003374
3375 btrfs_item_key_to_cpu(leaf, &key, i);
3376 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04003377 continue;
3378 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003379 if (btrfs_file_extent_type(leaf, fi) ==
3380 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04003381 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04003382 /*
3383 * FIXME make sure to insert a trans record that
3384 * repeats the snapshot del on crash
3385 */
Chris Masondb945352007-10-15 16:15:53 -04003386 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3387 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04003388 continue;
Chris Mason4a096752008-07-21 10:29:44 -04003389
Chris Mason925baed2008-06-25 16:01:30 -04003390 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003391 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04003392 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003393 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003394 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04003395
3396 atomic_inc(&root->fs_info->throttle_gen);
3397 wake_up(&root->fs_info->transaction_throttle);
3398 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04003399 }
3400 return 0;
3401}
3402
Chris Masone02119d2008-09-05 16:13:11 -04003403static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3404 struct btrfs_root *root,
3405 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04003406{
3407 int i;
3408 int ret;
3409 struct btrfs_extent_info *info = ref->extents;
3410
Yan Zheng31153d82008-07-28 15:32:19 -04003411 for (i = 0; i < ref->nritems; i++) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003412 ret = __btrfs_free_extent(trans, root, info->bytenr,
3413 info->num_bytes, ref->bytenr,
3414 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003415 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04003416
3417 atomic_inc(&root->fs_info->throttle_gen);
3418 wake_up(&root->fs_info->transaction_throttle);
3419 cond_resched();
3420
Yan Zheng31153d82008-07-28 15:32:19 -04003421 BUG_ON(ret);
3422 info++;
3423 }
Yan Zheng31153d82008-07-28 15:32:19 -04003424
3425 return 0;
3426}
3427
Chris Mason333db942008-06-25 16:01:30 -04003428int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
3429 u32 *refs)
3430{
Chris Mason017e5362008-07-28 15:32:51 -04003431 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04003432
Zheng Yan31840ae2008-09-23 13:14:14 -04003433 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04003434 BUG_ON(ret);
3435
3436#if 0 // some debugging code in case we see problems here
3437 /* if the refs count is one, it won't get increased again. But
3438 * if the ref count is > 1, someone may be decreasing it at
3439 * the same time we are.
3440 */
3441 if (*refs != 1) {
3442 struct extent_buffer *eb = NULL;
3443 eb = btrfs_find_create_tree_block(root, start, len);
3444 if (eb)
3445 btrfs_tree_lock(eb);
3446
3447 mutex_lock(&root->fs_info->alloc_mutex);
3448 ret = lookup_extent_ref(NULL, root, start, len, refs);
3449 BUG_ON(ret);
3450 mutex_unlock(&root->fs_info->alloc_mutex);
3451
3452 if (eb) {
3453 btrfs_tree_unlock(eb);
3454 free_extent_buffer(eb);
3455 }
3456 if (*refs == 1) {
3457 printk("block %llu went down to one during drop_snap\n",
3458 (unsigned long long)start);
3459 }
3460
3461 }
3462#endif
3463
Chris Masone7a84562008-06-25 16:01:31 -04003464 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04003465 return ret;
Chris Mason333db942008-06-25 16:01:30 -04003466}
3467
3468/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003469 * helper function for drop_snapshot, this walks down the tree dropping ref
3470 * counts as it goes.
3471 */
Chris Mason98ed5172008-01-03 10:01:48 -05003472static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
3473 struct btrfs_root *root,
3474 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003475{
Chris Mason7bb86312007-12-11 09:25:06 -05003476 u64 root_owner;
3477 u64 root_gen;
3478 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04003479 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003480 struct extent_buffer *next;
3481 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05003482 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04003483 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04003484 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05003485 int ret;
3486 u32 refs;
3487
Chris Mason5caf2a02007-04-02 11:20:42 -04003488 WARN_ON(*level < 0);
3489 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04003490 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04003491 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05003492 BUG_ON(ret);
3493 if (refs > 1)
3494 goto out;
Chris Masone0115992007-06-19 16:23:05 -04003495
Chris Mason9aca1d52007-03-13 11:09:37 -04003496 /*
3497 * walk down to the last node level and free all the leaves
3498 */
Chris Mason6407bf62007-03-27 06:33:00 -04003499 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003500 WARN_ON(*level < 0);
3501 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05003502 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04003503
Chris Mason5f39d392007-10-15 16:14:19 -04003504 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04003505 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04003506
Chris Mason7518a232007-03-12 12:01:18 -04003507 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04003508 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05003509 break;
Chris Mason6407bf62007-03-27 06:33:00 -04003510 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003511 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04003512 BUG_ON(ret);
3513 break;
3514 }
Chris Masondb945352007-10-15 16:15:53 -04003515 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04003516 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04003517 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04003518
Chris Mason333db942008-06-25 16:01:30 -04003519 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04003520 BUG_ON(ret);
3521 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05003522 parent = path->nodes[*level];
3523 root_owner = btrfs_header_owner(parent);
3524 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05003525 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04003526
Chris Mason925baed2008-06-25 16:01:30 -04003527 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003528 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003529 root_owner, root_gen,
3530 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05003531 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04003532
3533 atomic_inc(&root->fs_info->throttle_gen);
3534 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04003535 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04003536
Chris Mason20524f02007-03-10 06:35:47 -05003537 continue;
3538 }
Chris Masonf87f0572008-08-01 11:27:23 -04003539 /*
3540 * at this point, we have a single ref, and since the
3541 * only place referencing this extent is a dead root
3542 * the reference count should never go higher.
3543 * So, we don't need to check it again
3544 */
Yan Zheng31153d82008-07-28 15:32:19 -04003545 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04003546 ref = btrfs_lookup_leaf_ref(root, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04003547 if (ref && ref->generation != ptr_gen) {
3548 btrfs_free_leaf_ref(root, ref);
3549 ref = NULL;
3550 }
Yan Zheng31153d82008-07-28 15:32:19 -04003551 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04003552 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003553 BUG_ON(ret);
3554 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04003555 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003556 *level = 0;
3557 break;
3558 }
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003559 if (printk_ratelimit()) {
Chris Mason37d1aee2008-07-31 10:48:37 -04003560 printk("leaf ref miss for bytenr %llu\n",
3561 (unsigned long long)bytenr);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003562 }
Yan Zheng31153d82008-07-28 15:32:19 -04003563 }
Chris Masondb945352007-10-15 16:15:53 -04003564 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04003565 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003566 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04003567
Chris Masonca7a79a2008-05-12 12:59:19 -04003568 next = read_tree_block(root, bytenr, blocksize,
3569 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04003570 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04003571#if 0
3572 /*
3573 * this is a debugging check and can go away
3574 * the ref should never go all the way down to 1
3575 * at this point
3576 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003577 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
3578 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04003579 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003580 WARN_ON(refs != 1);
3581#endif
Chris Masone9d0b132007-08-10 14:06:19 -04003582 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003583 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04003584 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04003585 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05003586 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04003587 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05003588 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04003589 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003590 }
3591out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003592 WARN_ON(*level < 0);
3593 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05003594
3595 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003596 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003597 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003598 } else {
3599 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003600 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003601 }
3602
Yan Zheng31153d82008-07-28 15:32:19 -04003603 blocksize = btrfs_level_size(root, *level);
3604 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003605 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003606
3607 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003608 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003609 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003610 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003611 path->nodes[*level] = NULL;
3612 *level += 1;
3613 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003614
Chris Masone7a84562008-06-25 16:01:31 -04003615 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003616 return 0;
3617}
3618
Chris Mason9aca1d52007-03-13 11:09:37 -04003619/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04003620 * helper function for drop_subtree, this function is similar to
3621 * walk_down_tree. The main difference is that it checks reference
3622 * counts while tree blocks are locked.
3623 */
3624static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
3625 struct btrfs_root *root,
3626 struct btrfs_path *path, int *level)
3627{
3628 struct extent_buffer *next;
3629 struct extent_buffer *cur;
3630 struct extent_buffer *parent;
3631 u64 bytenr;
3632 u64 ptr_gen;
3633 u32 blocksize;
3634 u32 refs;
3635 int ret;
3636
3637 cur = path->nodes[*level];
3638 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3639 &refs);
3640 BUG_ON(ret);
3641 if (refs > 1)
3642 goto out;
3643
3644 while (*level >= 0) {
3645 cur = path->nodes[*level];
3646 if (*level == 0) {
3647 ret = btrfs_drop_leaf_ref(trans, root, cur);
3648 BUG_ON(ret);
3649 clean_tree_block(trans, root, cur);
3650 break;
3651 }
3652 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3653 clean_tree_block(trans, root, cur);
3654 break;
3655 }
3656
3657 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3658 blocksize = btrfs_level_size(root, *level - 1);
3659 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3660
3661 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3662 btrfs_tree_lock(next);
3663
3664 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3665 &refs);
3666 BUG_ON(ret);
3667 if (refs > 1) {
3668 parent = path->nodes[*level];
3669 ret = btrfs_free_extent(trans, root, bytenr,
3670 blocksize, parent->start,
3671 btrfs_header_owner(parent),
3672 btrfs_header_generation(parent),
3673 *level - 1, 1);
3674 BUG_ON(ret);
3675 path->slots[*level]++;
3676 btrfs_tree_unlock(next);
3677 free_extent_buffer(next);
3678 continue;
3679 }
3680
3681 *level = btrfs_header_level(next);
3682 path->nodes[*level] = next;
3683 path->slots[*level] = 0;
3684 path->locks[*level] = 1;
3685 cond_resched();
3686 }
3687out:
3688 parent = path->nodes[*level + 1];
3689 bytenr = path->nodes[*level]->start;
3690 blocksize = path->nodes[*level]->len;
3691
3692 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3693 parent->start, btrfs_header_owner(parent),
3694 btrfs_header_generation(parent), *level, 1);
3695 BUG_ON(ret);
3696
3697 if (path->locks[*level]) {
3698 btrfs_tree_unlock(path->nodes[*level]);
3699 path->locks[*level] = 0;
3700 }
3701 free_extent_buffer(path->nodes[*level]);
3702 path->nodes[*level] = NULL;
3703 *level += 1;
3704 cond_resched();
3705 return 0;
3706}
3707
3708/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003709 * helper for dropping snapshots. This walks back up the tree in the path
3710 * to find the first node higher up where we haven't yet gone through
3711 * all the slots
3712 */
Chris Mason98ed5172008-01-03 10:01:48 -05003713static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3714 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003715 struct btrfs_path *path,
3716 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003717{
Chris Mason7bb86312007-12-11 09:25:06 -05003718 u64 root_owner;
3719 u64 root_gen;
3720 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003721 int i;
3722 int slot;
3723 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003724
Yan Zhengf82d02d2008-10-29 14:49:05 -04003725 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003726 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003727 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3728 struct extent_buffer *node;
3729 struct btrfs_disk_key disk_key;
3730 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003731 path->slots[i]++;
3732 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003733 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003734 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003735 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003736 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003737 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003738 return 0;
3739 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003740 struct extent_buffer *parent;
3741 if (path->nodes[*level] == root->node)
3742 parent = path->nodes[*level];
3743 else
3744 parent = path->nodes[*level + 1];
3745
3746 root_owner = btrfs_header_owner(parent);
3747 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003748
3749 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003750 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003751 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003752 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003753 parent->start, root_owner,
3754 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003755 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003756 if (path->locks[*level]) {
3757 btrfs_tree_unlock(path->nodes[*level]);
3758 path->locks[*level] = 0;
3759 }
Chris Mason5f39d392007-10-15 16:14:19 -04003760 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003761 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003762 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003763 }
3764 }
3765 return 1;
3766}
3767
Chris Mason9aca1d52007-03-13 11:09:37 -04003768/*
3769 * drop the reference count on the tree rooted at 'snap'. This traverses
3770 * the tree freeing any blocks that have a ref count of zero after being
3771 * decremented.
3772 */
Chris Masone089f052007-03-16 16:20:31 -04003773int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003774 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003775{
Chris Mason3768f362007-03-13 16:47:54 -04003776 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003777 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003778 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003779 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003780 int i;
3781 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003782 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003783
Chris Masona2135012008-06-25 16:01:30 -04003784 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003785 path = btrfs_alloc_path();
3786 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003787
Chris Mason5f39d392007-10-15 16:14:19 -04003788 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003789 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003790 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3791 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003792 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003793 path->slots[level] = 0;
3794 } else {
3795 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003796 struct btrfs_disk_key found_key;
3797 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003798
Chris Mason9f3a7422007-08-07 15:52:19 -04003799 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003800 level = root_item->drop_level;
3801 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003802 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003803 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003804 ret = wret;
3805 goto out;
3806 }
Chris Mason5f39d392007-10-15 16:14:19 -04003807 node = path->nodes[level];
3808 btrfs_node_key(node, &found_key, path->slots[level]);
3809 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3810 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003811 /*
3812 * unlock our path, this is safe because only this
3813 * function is allowed to delete this snapshot
3814 */
Chris Mason925baed2008-06-25 16:01:30 -04003815 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3816 if (path->nodes[i] && path->locks[i]) {
3817 path->locks[i] = 0;
3818 btrfs_tree_unlock(path->nodes[i]);
3819 }
3820 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003821 }
Chris Mason20524f02007-03-10 06:35:47 -05003822 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003823 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003824 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003825 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003826 if (wret < 0)
3827 ret = wret;
3828
Yan Zhengf82d02d2008-10-29 14:49:05 -04003829 wret = walk_up_tree(trans, root, path, &level,
3830 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003831 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003832 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003833 if (wret < 0)
3834 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003835 if (trans->transaction->in_commit) {
3836 ret = -EAGAIN;
3837 break;
3838 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003839 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003840 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003841 }
Chris Mason83e15a22007-03-12 09:03:27 -04003842 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003843 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003844 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003845 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003846 }
Chris Mason20524f02007-03-10 06:35:47 -05003847 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003848out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003849 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003850 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003851}
Chris Mason9078a3e2007-04-26 16:46:15 -04003852
Yan Zhengf82d02d2008-10-29 14:49:05 -04003853int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3854 struct btrfs_root *root,
3855 struct extent_buffer *node,
3856 struct extent_buffer *parent)
3857{
3858 struct btrfs_path *path;
3859 int level;
3860 int parent_level;
3861 int ret = 0;
3862 int wret;
3863
3864 path = btrfs_alloc_path();
3865 BUG_ON(!path);
3866
3867 BUG_ON(!btrfs_tree_locked(parent));
3868 parent_level = btrfs_header_level(parent);
3869 extent_buffer_get(parent);
3870 path->nodes[parent_level] = parent;
3871 path->slots[parent_level] = btrfs_header_nritems(parent);
3872
3873 BUG_ON(!btrfs_tree_locked(node));
3874 level = btrfs_header_level(node);
3875 extent_buffer_get(node);
3876 path->nodes[level] = node;
3877 path->slots[level] = 0;
3878
3879 while (1) {
3880 wret = walk_down_subtree(trans, root, path, &level);
3881 if (wret < 0)
3882 ret = wret;
3883 if (wret != 0)
3884 break;
3885
3886 wret = walk_up_tree(trans, root, path, &level, parent_level);
3887 if (wret < 0)
3888 ret = wret;
3889 if (wret != 0)
3890 break;
3891 }
3892
3893 btrfs_free_path(path);
3894 return ret;
3895}
3896
Chris Mason8e7bf942008-04-28 09:02:36 -04003897static unsigned long calc_ra(unsigned long start, unsigned long last,
3898 unsigned long nr)
3899{
3900 return min(last, start + nr - 1);
3901}
3902
Chris Mason98ed5172008-01-03 10:01:48 -05003903static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3904 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003905{
3906 u64 page_start;
3907 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003908 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003909 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003910 unsigned long i;
3911 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003912 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003913 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003914 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003915 unsigned int total_read = 0;
3916 unsigned int total_dirty = 0;
3917 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003918
3919 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003920
3921 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003922 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003923 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3924
Zheng Yan1a40e232008-09-26 10:09:34 -04003925 /* make sure the dirty trick played by the caller work */
3926 ret = invalidate_inode_pages2_range(inode->i_mapping,
3927 first_index, last_index);
3928 if (ret)
3929 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003930
Chris Mason4313b392008-01-03 09:08:48 -05003931 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003932
Zheng Yan1a40e232008-09-26 10:09:34 -04003933 for (i = first_index ; i <= last_index; i++) {
3934 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003935 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003936 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003937 }
3938 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003939again:
3940 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003941 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003942 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003943 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003944 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003945 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003946 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003947 if (!PageUptodate(page)) {
3948 btrfs_readpage(NULL, page);
3949 lock_page(page);
3950 if (!PageUptodate(page)) {
3951 unlock_page(page);
3952 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003953 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003954 goto out_unlock;
3955 }
3956 }
Chris Masonec44a352008-04-28 15:29:52 -04003957 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003958
Chris Masonedbd8d42007-12-21 16:27:24 -05003959 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3960 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003961 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003962
Chris Mason3eaa2882008-07-24 11:57:52 -04003963 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3964 if (ordered) {
3965 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3966 unlock_page(page);
3967 page_cache_release(page);
3968 btrfs_start_ordered_extent(inode, ordered, 1);
3969 btrfs_put_ordered_extent(ordered);
3970 goto again;
3971 }
3972 set_page_extent_mapped(page);
3973
Chris Masonea8c2812008-08-04 23:17:27 -04003974 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003975 if (i == first_index)
3976 set_extent_bits(io_tree, page_start, page_end,
3977 EXTENT_BOUNDARY, GFP_NOFS);
3978
Chris Masona061fc82008-05-07 11:43:44 -04003979 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003980 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003981
Chris Masond1310b22008-01-24 16:13:08 -05003982 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003983 unlock_page(page);
3984 page_cache_release(page);
3985 }
3986
3987out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04003988 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05003989 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003990 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04003991 return ret;
3992}
3993
Zheng Yan1a40e232008-09-26 10:09:34 -04003994static int noinline relocate_data_extent(struct inode *reloc_inode,
3995 struct btrfs_key *extent_key,
3996 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05003997{
Zheng Yan1a40e232008-09-26 10:09:34 -04003998 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3999 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4000 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04004001 u64 start = extent_key->objectid - offset;
4002 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004003
4004 em = alloc_extent_map(GFP_NOFS);
4005 BUG_ON(!em || IS_ERR(em));
4006
Yan Zheng66435582008-10-30 14:19:50 -04004007 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004008 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004009 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004010 em->block_start = extent_key->objectid;
4011 em->bdev = root->fs_info->fs_devices->latest_bdev;
4012 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4013
4014 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04004015 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004016 while (1) {
4017 int ret;
4018 spin_lock(&em_tree->lock);
4019 ret = add_extent_mapping(em_tree, em);
4020 spin_unlock(&em_tree->lock);
4021 if (ret != -EEXIST) {
4022 free_extent_map(em);
4023 break;
4024 }
Yan Zheng66435582008-10-30 14:19:50 -04004025 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004026 }
Yan Zheng66435582008-10-30 14:19:50 -04004027 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004028
Yan Zheng66435582008-10-30 14:19:50 -04004029 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04004030}
4031
4032struct btrfs_ref_path {
4033 u64 extent_start;
4034 u64 nodes[BTRFS_MAX_LEVEL];
4035 u64 root_objectid;
4036 u64 root_generation;
4037 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004038 u32 num_refs;
4039 int lowest_level;
4040 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004041 int shared_level;
4042
4043 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4044 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04004045};
4046
4047struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04004048 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04004049 u64 disk_bytenr;
4050 u64 disk_num_bytes;
4051 u64 offset;
4052 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04004053 u8 compression;
4054 u8 encryption;
4055 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04004056};
4057
4058static int is_cowonly_root(u64 root_objectid)
4059{
4060 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4061 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4062 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4063 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
4064 root_objectid == BTRFS_TREE_LOG_OBJECTID)
4065 return 1;
4066 return 0;
4067}
4068
4069static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
4070 struct btrfs_root *extent_root,
4071 struct btrfs_ref_path *ref_path,
4072 int first_time)
4073{
4074 struct extent_buffer *leaf;
4075 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05004076 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05004077 struct btrfs_key key;
4078 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04004079 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05004080 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04004081 int level;
4082 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05004083
Zheng Yan1a40e232008-09-26 10:09:34 -04004084 path = btrfs_alloc_path();
4085 if (!path)
4086 return -ENOMEM;
4087
Zheng Yan1a40e232008-09-26 10:09:34 -04004088 if (first_time) {
4089 ref_path->lowest_level = -1;
4090 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004091 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004092 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04004093 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004094walk_down:
4095 level = ref_path->current_level - 1;
4096 while (level >= -1) {
4097 u64 parent;
4098 if (level < ref_path->lowest_level)
4099 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004100
Zheng Yan1a40e232008-09-26 10:09:34 -04004101 if (level >= 0) {
4102 bytenr = ref_path->nodes[level];
4103 } else {
4104 bytenr = ref_path->extent_start;
4105 }
4106 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004107
Zheng Yan1a40e232008-09-26 10:09:34 -04004108 parent = ref_path->nodes[level + 1];
4109 ref_path->nodes[level + 1] = 0;
4110 ref_path->current_level = level;
4111 BUG_ON(parent == 0);
4112
4113 key.objectid = bytenr;
4114 key.offset = parent + 1;
4115 key.type = BTRFS_EXTENT_REF_KEY;
4116
4117 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004118 if (ret < 0)
4119 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004120 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004121
Chris Masonedbd8d42007-12-21 16:27:24 -05004122 leaf = path->nodes[0];
4123 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04004124 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04004125 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04004126 if (ret < 0)
4127 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004128 if (ret > 0)
4129 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04004130 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04004131 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004132
4133 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04004134 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04004135 found_key.type == BTRFS_EXTENT_REF_KEY) {
4136 if (level < ref_path->shared_level)
4137 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004138 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004139 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004140next:
4141 level--;
4142 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004143 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004144 }
4145 /* reached lowest level */
4146 ret = 1;
4147 goto out;
4148walk_up:
4149 level = ref_path->current_level;
4150 while (level < BTRFS_MAX_LEVEL - 1) {
4151 u64 ref_objectid;
4152 if (level >= 0) {
4153 bytenr = ref_path->nodes[level];
4154 } else {
4155 bytenr = ref_path->extent_start;
Chris Masona061fc82008-05-07 11:43:44 -04004156 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004157 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004158
Zheng Yan1a40e232008-09-26 10:09:34 -04004159 key.objectid = bytenr;
4160 key.offset = 0;
4161 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05004162
Zheng Yan1a40e232008-09-26 10:09:34 -04004163 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4164 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05004165 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004166
4167 leaf = path->nodes[0];
4168 nritems = btrfs_header_nritems(leaf);
4169 if (path->slots[0] >= nritems) {
4170 ret = btrfs_next_leaf(extent_root, path);
4171 if (ret < 0)
4172 goto out;
4173 if (ret > 0) {
4174 /* the extent was freed by someone */
4175 if (ref_path->lowest_level == level)
4176 goto out;
4177 btrfs_release_path(extent_root, path);
4178 goto walk_down;
4179 }
4180 leaf = path->nodes[0];
4181 }
4182
4183 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4184 if (found_key.objectid != bytenr ||
4185 found_key.type != BTRFS_EXTENT_REF_KEY) {
4186 /* the extent was freed by someone */
4187 if (ref_path->lowest_level == level) {
4188 ret = 1;
4189 goto out;
4190 }
4191 btrfs_release_path(extent_root, path);
4192 goto walk_down;
4193 }
4194found:
4195 ref = btrfs_item_ptr(leaf, path->slots[0],
4196 struct btrfs_extent_ref);
4197 ref_objectid = btrfs_ref_objectid(leaf, ref);
4198 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4199 if (first_time) {
4200 level = (int)ref_objectid;
4201 BUG_ON(level >= BTRFS_MAX_LEVEL);
4202 ref_path->lowest_level = level;
4203 ref_path->current_level = level;
4204 ref_path->nodes[level] = bytenr;
4205 } else {
4206 WARN_ON(ref_objectid != level);
4207 }
4208 } else {
4209 WARN_ON(level != -1);
4210 }
4211 first_time = 0;
4212
4213 if (ref_path->lowest_level == level) {
4214 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004215 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4216 }
4217
4218 /*
4219 * the block is tree root or the block isn't in reference
4220 * counted tree.
4221 */
4222 if (found_key.objectid == found_key.offset ||
4223 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4224 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4225 ref_path->root_generation =
4226 btrfs_ref_generation(leaf, ref);
4227 if (level < 0) {
4228 /* special reference from the tree log */
4229 ref_path->nodes[0] = found_key.offset;
4230 ref_path->current_level = 0;
4231 }
4232 ret = 0;
4233 goto out;
4234 }
4235
4236 level++;
4237 BUG_ON(ref_path->nodes[level] != 0);
4238 ref_path->nodes[level] = found_key.offset;
4239 ref_path->current_level = level;
4240
4241 /*
4242 * the reference was created in the running transaction,
4243 * no need to continue walking up.
4244 */
4245 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4246 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4247 ref_path->root_generation =
4248 btrfs_ref_generation(leaf, ref);
4249 ret = 0;
4250 goto out;
4251 }
4252
4253 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004254 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004255 }
4256 /* reached max tree level, but no tree root found. */
4257 BUG();
4258out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004259 btrfs_free_path(path);
4260 return ret;
4261}
4262
4263static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4264 struct btrfs_root *extent_root,
4265 struct btrfs_ref_path *ref_path,
4266 u64 extent_start)
4267{
4268 memset(ref_path, 0, sizeof(*ref_path));
4269 ref_path->extent_start = extent_start;
4270
4271 return __next_ref_path(trans, extent_root, ref_path, 1);
4272}
4273
4274static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4275 struct btrfs_root *extent_root,
4276 struct btrfs_ref_path *ref_path)
4277{
4278 return __next_ref_path(trans, extent_root, ref_path, 0);
4279}
4280
4281static int noinline get_new_locations(struct inode *reloc_inode,
4282 struct btrfs_key *extent_key,
4283 u64 offset, int no_fragment,
4284 struct disk_extent **extents,
4285 int *nr_extents)
4286{
4287 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4288 struct btrfs_path *path;
4289 struct btrfs_file_extent_item *fi;
4290 struct extent_buffer *leaf;
4291 struct disk_extent *exts = *extents;
4292 struct btrfs_key found_key;
4293 u64 cur_pos;
4294 u64 last_byte;
4295 u32 nritems;
4296 int nr = 0;
4297 int max = *nr_extents;
4298 int ret;
4299
4300 WARN_ON(!no_fragment && *extents);
4301 if (!exts) {
4302 max = 1;
4303 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4304 if (!exts)
4305 return -ENOMEM;
4306 }
4307
4308 path = btrfs_alloc_path();
4309 BUG_ON(!path);
4310
4311 cur_pos = extent_key->objectid - offset;
4312 last_byte = extent_key->objectid + extent_key->offset;
4313 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4314 cur_pos, 0);
4315 if (ret < 0)
4316 goto out;
4317 if (ret > 0) {
4318 ret = -ENOENT;
4319 goto out;
4320 }
4321
4322 while (1) {
4323 leaf = path->nodes[0];
4324 nritems = btrfs_header_nritems(leaf);
4325 if (path->slots[0] >= nritems) {
4326 ret = btrfs_next_leaf(root, path);
4327 if (ret < 0)
4328 goto out;
4329 if (ret > 0)
4330 break;
4331 leaf = path->nodes[0];
4332 }
4333
4334 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4335 if (found_key.offset != cur_pos ||
4336 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4337 found_key.objectid != reloc_inode->i_ino)
4338 break;
4339
4340 fi = btrfs_item_ptr(leaf, path->slots[0],
4341 struct btrfs_file_extent_item);
4342 if (btrfs_file_extent_type(leaf, fi) !=
4343 BTRFS_FILE_EXTENT_REG ||
4344 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4345 break;
4346
4347 if (nr == max) {
4348 struct disk_extent *old = exts;
4349 max *= 2;
4350 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4351 memcpy(exts, old, sizeof(*exts) * nr);
4352 if (old != *extents)
4353 kfree(old);
4354 }
4355
4356 exts[nr].disk_bytenr =
4357 btrfs_file_extent_disk_bytenr(leaf, fi);
4358 exts[nr].disk_num_bytes =
4359 btrfs_file_extent_disk_num_bytes(leaf, fi);
4360 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4361 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04004362 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4363 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4364 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4365 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4366 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04004367 BUG_ON(exts[nr].offset > 0);
4368 BUG_ON(exts[nr].compression || exts[nr].encryption);
4369 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004370
4371 cur_pos += exts[nr].num_bytes;
4372 nr++;
4373
4374 if (cur_pos + offset >= last_byte)
4375 break;
4376
4377 if (no_fragment) {
4378 ret = 1;
4379 goto out;
4380 }
4381 path->slots[0]++;
4382 }
4383
4384 WARN_ON(cur_pos + offset > last_byte);
4385 if (cur_pos + offset < last_byte) {
4386 ret = -ENOENT;
4387 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05004388 }
4389 ret = 0;
4390out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004391 btrfs_free_path(path);
4392 if (ret) {
4393 if (exts != *extents)
4394 kfree(exts);
4395 } else {
4396 *extents = exts;
4397 *nr_extents = nr;
4398 }
4399 return ret;
4400}
4401
4402static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
4403 struct btrfs_root *root,
4404 struct btrfs_path *path,
4405 struct btrfs_key *extent_key,
4406 struct btrfs_key *leaf_key,
4407 struct btrfs_ref_path *ref_path,
4408 struct disk_extent *new_extents,
4409 int nr_extents)
4410{
4411 struct extent_buffer *leaf;
4412 struct btrfs_file_extent_item *fi;
4413 struct inode *inode = NULL;
4414 struct btrfs_key key;
4415 u64 lock_start = 0;
4416 u64 lock_end = 0;
4417 u64 num_bytes;
4418 u64 ext_offset;
4419 u64 first_pos;
4420 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004421 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004422 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04004423 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04004424 int ret;
4425
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004426 memcpy(&key, leaf_key, sizeof(key));
4427 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004428 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004429 if (key.objectid < ref_path->owner_objectid ||
4430 (key.objectid == ref_path->owner_objectid &&
4431 key.type < BTRFS_EXTENT_DATA_KEY)) {
4432 key.objectid = ref_path->owner_objectid;
4433 key.type = BTRFS_EXTENT_DATA_KEY;
4434 key.offset = 0;
4435 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004436 }
4437
4438 while (1) {
4439 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4440 if (ret < 0)
4441 goto out;
4442
4443 leaf = path->nodes[0];
4444 nritems = btrfs_header_nritems(leaf);
4445next:
4446 if (extent_locked && ret > 0) {
4447 /*
4448 * the file extent item was modified by someone
4449 * before the extent got locked.
4450 */
Zheng Yan1a40e232008-09-26 10:09:34 -04004451 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4452 lock_end, GFP_NOFS);
4453 extent_locked = 0;
4454 }
4455
4456 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004457 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04004458 break;
4459
4460 BUG_ON(extent_locked);
4461 ret = btrfs_next_leaf(root, path);
4462 if (ret < 0)
4463 goto out;
4464 if (ret > 0)
4465 break;
4466 leaf = path->nodes[0];
4467 nritems = btrfs_header_nritems(leaf);
4468 }
4469
4470 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4471
4472 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4473 if ((key.objectid > ref_path->owner_objectid) ||
4474 (key.objectid == ref_path->owner_objectid &&
4475 key.type > BTRFS_EXTENT_DATA_KEY) ||
4476 (key.offset >= first_pos + extent_key->offset))
4477 break;
4478 }
4479
4480 if (inode && key.objectid != inode->i_ino) {
4481 BUG_ON(extent_locked);
4482 btrfs_release_path(root, path);
4483 mutex_unlock(&inode->i_mutex);
4484 iput(inode);
4485 inode = NULL;
4486 continue;
4487 }
4488
4489 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4490 path->slots[0]++;
4491 ret = 1;
4492 goto next;
4493 }
4494 fi = btrfs_item_ptr(leaf, path->slots[0],
4495 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04004496 extent_type = btrfs_file_extent_type(leaf, fi);
4497 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4498 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04004499 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4500 extent_key->objectid)) {
4501 path->slots[0]++;
4502 ret = 1;
4503 goto next;
4504 }
4505
4506 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4507 ext_offset = btrfs_file_extent_offset(leaf, fi);
4508
4509 if (first_pos > key.offset - ext_offset)
4510 first_pos = key.offset - ext_offset;
4511
4512 if (!extent_locked) {
4513 lock_start = key.offset;
4514 lock_end = lock_start + num_bytes - 1;
4515 } else {
Yan Zheng66435582008-10-30 14:19:50 -04004516 if (lock_start > key.offset ||
4517 lock_end + 1 < key.offset + num_bytes) {
4518 unlock_extent(&BTRFS_I(inode)->io_tree,
4519 lock_start, lock_end, GFP_NOFS);
4520 extent_locked = 0;
4521 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004522 }
4523
4524 if (!inode) {
4525 btrfs_release_path(root, path);
4526
4527 inode = btrfs_iget_locked(root->fs_info->sb,
4528 key.objectid, root);
4529 if (inode->i_state & I_NEW) {
4530 BTRFS_I(inode)->root = root;
4531 BTRFS_I(inode)->location.objectid =
4532 key.objectid;
4533 BTRFS_I(inode)->location.type =
4534 BTRFS_INODE_ITEM_KEY;
4535 BTRFS_I(inode)->location.offset = 0;
4536 btrfs_read_locked_inode(inode);
4537 unlock_new_inode(inode);
4538 }
4539 /*
4540 * some code call btrfs_commit_transaction while
4541 * holding the i_mutex, so we can't use mutex_lock
4542 * here.
4543 */
4544 if (is_bad_inode(inode) ||
4545 !mutex_trylock(&inode->i_mutex)) {
4546 iput(inode);
4547 inode = NULL;
4548 key.offset = (u64)-1;
4549 goto skip;
4550 }
4551 }
4552
4553 if (!extent_locked) {
4554 struct btrfs_ordered_extent *ordered;
4555
4556 btrfs_release_path(root, path);
4557
4558 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4559 lock_end, GFP_NOFS);
4560 ordered = btrfs_lookup_first_ordered_extent(inode,
4561 lock_end);
4562 if (ordered &&
4563 ordered->file_offset <= lock_end &&
4564 ordered->file_offset + ordered->len > lock_start) {
4565 unlock_extent(&BTRFS_I(inode)->io_tree,
4566 lock_start, lock_end, GFP_NOFS);
4567 btrfs_start_ordered_extent(inode, ordered, 1);
4568 btrfs_put_ordered_extent(ordered);
4569 key.offset += num_bytes;
4570 goto skip;
4571 }
4572 if (ordered)
4573 btrfs_put_ordered_extent(ordered);
4574
Zheng Yan1a40e232008-09-26 10:09:34 -04004575 extent_locked = 1;
4576 continue;
4577 }
4578
4579 if (nr_extents == 1) {
4580 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04004581 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4582 new_extents[0].disk_bytenr);
4583 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4584 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004585 btrfs_mark_buffer_dirty(leaf);
4586
4587 btrfs_drop_extent_cache(inode, key.offset,
4588 key.offset + num_bytes - 1, 0);
4589
4590 ret = btrfs_inc_extent_ref(trans, root,
4591 new_extents[0].disk_bytenr,
4592 new_extents[0].disk_num_bytes,
4593 leaf->start,
4594 root->root_key.objectid,
4595 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004596 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004597 BUG_ON(ret);
4598
4599 ret = btrfs_free_extent(trans, root,
4600 extent_key->objectid,
4601 extent_key->offset,
4602 leaf->start,
4603 btrfs_header_owner(leaf),
4604 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004605 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004606 BUG_ON(ret);
4607
4608 btrfs_release_path(root, path);
4609 key.offset += num_bytes;
4610 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04004611 BUG_ON(1);
4612#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04004613 u64 alloc_hint;
4614 u64 extent_len;
4615 int i;
4616 /*
4617 * drop old extent pointer at first, then insert the
4618 * new pointers one bye one
4619 */
4620 btrfs_release_path(root, path);
4621 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4622 key.offset + num_bytes,
4623 key.offset, &alloc_hint);
4624 BUG_ON(ret);
4625
4626 for (i = 0; i < nr_extents; i++) {
4627 if (ext_offset >= new_extents[i].num_bytes) {
4628 ext_offset -= new_extents[i].num_bytes;
4629 continue;
4630 }
4631 extent_len = min(new_extents[i].num_bytes -
4632 ext_offset, num_bytes);
4633
4634 ret = btrfs_insert_empty_item(trans, root,
4635 path, &key,
4636 sizeof(*fi));
4637 BUG_ON(ret);
4638
4639 leaf = path->nodes[0];
4640 fi = btrfs_item_ptr(leaf, path->slots[0],
4641 struct btrfs_file_extent_item);
4642 btrfs_set_file_extent_generation(leaf, fi,
4643 trans->transid);
4644 btrfs_set_file_extent_type(leaf, fi,
4645 BTRFS_FILE_EXTENT_REG);
4646 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4647 new_extents[i].disk_bytenr);
4648 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4649 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004650 btrfs_set_file_extent_ram_bytes(leaf, fi,
4651 new_extents[i].ram_bytes);
4652
4653 btrfs_set_file_extent_compression(leaf, fi,
4654 new_extents[i].compression);
4655 btrfs_set_file_extent_encryption(leaf, fi,
4656 new_extents[i].encryption);
4657 btrfs_set_file_extent_other_encoding(leaf, fi,
4658 new_extents[i].other_encoding);
4659
Zheng Yan1a40e232008-09-26 10:09:34 -04004660 btrfs_set_file_extent_num_bytes(leaf, fi,
4661 extent_len);
4662 ext_offset += new_extents[i].offset;
4663 btrfs_set_file_extent_offset(leaf, fi,
4664 ext_offset);
4665 btrfs_mark_buffer_dirty(leaf);
4666
4667 btrfs_drop_extent_cache(inode, key.offset,
4668 key.offset + extent_len - 1, 0);
4669
4670 ret = btrfs_inc_extent_ref(trans, root,
4671 new_extents[i].disk_bytenr,
4672 new_extents[i].disk_num_bytes,
4673 leaf->start,
4674 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004675 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004676 BUG_ON(ret);
4677 btrfs_release_path(root, path);
4678
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004679 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004680
4681 ext_offset = 0;
4682 num_bytes -= extent_len;
4683 key.offset += extent_len;
4684
4685 if (num_bytes == 0)
4686 break;
4687 }
4688 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004689#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04004690 }
4691
4692 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004693 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4694 lock_end, GFP_NOFS);
4695 extent_locked = 0;
4696 }
4697skip:
4698 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4699 key.offset >= first_pos + extent_key->offset)
4700 break;
4701
4702 cond_resched();
4703 }
4704 ret = 0;
4705out:
4706 btrfs_release_path(root, path);
4707 if (inode) {
4708 mutex_unlock(&inode->i_mutex);
4709 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004710 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4711 lock_end, GFP_NOFS);
4712 }
4713 iput(inode);
4714 }
4715 return ret;
4716}
4717
Zheng Yan1a40e232008-09-26 10:09:34 -04004718int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4719 struct btrfs_root *root,
4720 struct extent_buffer *buf, u64 orig_start)
4721{
4722 int level;
4723 int ret;
4724
4725 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4726 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4727
4728 level = btrfs_header_level(buf);
4729 if (level == 0) {
4730 struct btrfs_leaf_ref *ref;
4731 struct btrfs_leaf_ref *orig_ref;
4732
4733 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4734 if (!orig_ref)
4735 return -ENOENT;
4736
4737 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4738 if (!ref) {
4739 btrfs_free_leaf_ref(root, orig_ref);
4740 return -ENOMEM;
4741 }
4742
4743 ref->nritems = orig_ref->nritems;
4744 memcpy(ref->extents, orig_ref->extents,
4745 sizeof(ref->extents[0]) * ref->nritems);
4746
4747 btrfs_free_leaf_ref(root, orig_ref);
4748
4749 ref->root_gen = trans->transid;
4750 ref->bytenr = buf->start;
4751 ref->owner = btrfs_header_owner(buf);
4752 ref->generation = btrfs_header_generation(buf);
4753 ret = btrfs_add_leaf_ref(root, ref, 0);
4754 WARN_ON(ret);
4755 btrfs_free_leaf_ref(root, ref);
4756 }
4757 return 0;
4758}
4759
4760static int noinline invalidate_extent_cache(struct btrfs_root *root,
4761 struct extent_buffer *leaf,
4762 struct btrfs_block_group_cache *group,
4763 struct btrfs_root *target_root)
4764{
4765 struct btrfs_key key;
4766 struct inode *inode = NULL;
4767 struct btrfs_file_extent_item *fi;
4768 u64 num_bytes;
4769 u64 skip_objectid = 0;
4770 u32 nritems;
4771 u32 i;
4772
4773 nritems = btrfs_header_nritems(leaf);
4774 for (i = 0; i < nritems; i++) {
4775 btrfs_item_key_to_cpu(leaf, &key, i);
4776 if (key.objectid == skip_objectid ||
4777 key.type != BTRFS_EXTENT_DATA_KEY)
4778 continue;
4779 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4780 if (btrfs_file_extent_type(leaf, fi) ==
4781 BTRFS_FILE_EXTENT_INLINE)
4782 continue;
4783 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4784 continue;
4785 if (!inode || inode->i_ino != key.objectid) {
4786 iput(inode);
4787 inode = btrfs_ilookup(target_root->fs_info->sb,
4788 key.objectid, target_root, 1);
4789 }
4790 if (!inode) {
4791 skip_objectid = key.objectid;
4792 continue;
4793 }
4794 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4795
4796 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4797 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004798 btrfs_drop_extent_cache(inode, key.offset,
4799 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004800 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4801 key.offset + num_bytes - 1, GFP_NOFS);
4802 cond_resched();
4803 }
4804 iput(inode);
4805 return 0;
4806}
4807
4808static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4809 struct btrfs_root *root,
4810 struct extent_buffer *leaf,
4811 struct btrfs_block_group_cache *group,
4812 struct inode *reloc_inode)
4813{
4814 struct btrfs_key key;
4815 struct btrfs_key extent_key;
4816 struct btrfs_file_extent_item *fi;
4817 struct btrfs_leaf_ref *ref;
4818 struct disk_extent *new_extent;
4819 u64 bytenr;
4820 u64 num_bytes;
4821 u32 nritems;
4822 u32 i;
4823 int ext_index;
4824 int nr_extent;
4825 int ret;
4826
4827 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4828 BUG_ON(!new_extent);
4829
4830 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4831 BUG_ON(!ref);
4832
4833 ext_index = -1;
4834 nritems = btrfs_header_nritems(leaf);
4835 for (i = 0; i < nritems; i++) {
4836 btrfs_item_key_to_cpu(leaf, &key, i);
4837 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4838 continue;
4839 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4840 if (btrfs_file_extent_type(leaf, fi) ==
4841 BTRFS_FILE_EXTENT_INLINE)
4842 continue;
4843 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4844 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4845 if (bytenr == 0)
4846 continue;
4847
4848 ext_index++;
4849 if (bytenr >= group->key.objectid + group->key.offset ||
4850 bytenr + num_bytes <= group->key.objectid)
4851 continue;
4852
4853 extent_key.objectid = bytenr;
4854 extent_key.offset = num_bytes;
4855 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4856 nr_extent = 1;
4857 ret = get_new_locations(reloc_inode, &extent_key,
4858 group->key.objectid, 1,
4859 &new_extent, &nr_extent);
4860 if (ret > 0)
4861 continue;
4862 BUG_ON(ret < 0);
4863
4864 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4865 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4866 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4867 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4868
Zheng Yan1a40e232008-09-26 10:09:34 -04004869 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4870 new_extent->disk_bytenr);
4871 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4872 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004873 btrfs_mark_buffer_dirty(leaf);
4874
4875 ret = btrfs_inc_extent_ref(trans, root,
4876 new_extent->disk_bytenr,
4877 new_extent->disk_num_bytes,
4878 leaf->start,
4879 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004880 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004881 BUG_ON(ret);
4882 ret = btrfs_free_extent(trans, root,
4883 bytenr, num_bytes, leaf->start,
4884 btrfs_header_owner(leaf),
4885 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004886 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004887 BUG_ON(ret);
4888 cond_resched();
4889 }
4890 kfree(new_extent);
4891 BUG_ON(ext_index + 1 != ref->nritems);
4892 btrfs_free_leaf_ref(root, ref);
4893 return 0;
4894}
4895
Yan Zhengf82d02d2008-10-29 14:49:05 -04004896int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4897 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004898{
4899 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004900 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004901
4902 if (root->reloc_root) {
4903 reloc_root = root->reloc_root;
4904 root->reloc_root = NULL;
4905 list_add(&reloc_root->dead_list,
4906 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004907
4908 btrfs_set_root_bytenr(&reloc_root->root_item,
4909 reloc_root->node->start);
4910 btrfs_set_root_level(&root->root_item,
4911 btrfs_header_level(reloc_root->node));
4912 memset(&reloc_root->root_item.drop_progress, 0,
4913 sizeof(struct btrfs_disk_key));
4914 reloc_root->root_item.drop_level = 0;
4915
4916 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4917 &reloc_root->root_key,
4918 &reloc_root->root_item);
4919 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004920 }
4921 return 0;
4922}
4923
4924int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4925{
4926 struct btrfs_trans_handle *trans;
4927 struct btrfs_root *reloc_root;
4928 struct btrfs_root *prev_root = NULL;
4929 struct list_head dead_roots;
4930 int ret;
4931 unsigned long nr;
4932
4933 INIT_LIST_HEAD(&dead_roots);
4934 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4935
4936 while (!list_empty(&dead_roots)) {
4937 reloc_root = list_entry(dead_roots.prev,
4938 struct btrfs_root, dead_list);
4939 list_del_init(&reloc_root->dead_list);
4940
4941 BUG_ON(reloc_root->commit_root != NULL);
4942 while (1) {
4943 trans = btrfs_join_transaction(root, 1);
4944 BUG_ON(!trans);
4945
4946 mutex_lock(&root->fs_info->drop_mutex);
4947 ret = btrfs_drop_snapshot(trans, reloc_root);
4948 if (ret != -EAGAIN)
4949 break;
4950 mutex_unlock(&root->fs_info->drop_mutex);
4951
4952 nr = trans->blocks_used;
4953 ret = btrfs_end_transaction(trans, root);
4954 BUG_ON(ret);
4955 btrfs_btree_balance_dirty(root, nr);
4956 }
4957
4958 free_extent_buffer(reloc_root->node);
4959
4960 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4961 &reloc_root->root_key);
4962 BUG_ON(ret);
4963 mutex_unlock(&root->fs_info->drop_mutex);
4964
4965 nr = trans->blocks_used;
4966 ret = btrfs_end_transaction(trans, root);
4967 BUG_ON(ret);
4968 btrfs_btree_balance_dirty(root, nr);
4969
4970 kfree(prev_root);
4971 prev_root = reloc_root;
4972 }
4973 if (prev_root) {
4974 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4975 kfree(prev_root);
4976 }
4977 return 0;
4978}
4979
4980int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4981{
4982 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4983 return 0;
4984}
4985
4986int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4987{
4988 struct btrfs_root *reloc_root;
4989 struct btrfs_trans_handle *trans;
4990 struct btrfs_key location;
4991 int found;
4992 int ret;
4993
4994 mutex_lock(&root->fs_info->tree_reloc_mutex);
4995 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4996 BUG_ON(ret);
4997 found = !list_empty(&root->fs_info->dead_reloc_roots);
4998 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4999
5000 if (found) {
5001 trans = btrfs_start_transaction(root, 1);
5002 BUG_ON(!trans);
5003 ret = btrfs_commit_transaction(trans, root);
5004 BUG_ON(ret);
5005 }
5006
5007 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5008 location.offset = (u64)-1;
5009 location.type = BTRFS_ROOT_ITEM_KEY;
5010
5011 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5012 BUG_ON(!reloc_root);
5013 btrfs_orphan_cleanup(reloc_root);
5014 return 0;
5015}
5016
5017static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
5018 struct btrfs_root *root)
5019{
5020 struct btrfs_root *reloc_root;
5021 struct extent_buffer *eb;
5022 struct btrfs_root_item *root_item;
5023 struct btrfs_key root_key;
5024 int ret;
5025
5026 BUG_ON(!root->ref_cows);
5027 if (root->reloc_root)
5028 return 0;
5029
5030 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5031 BUG_ON(!root_item);
5032
5033 ret = btrfs_copy_root(trans, root, root->commit_root,
5034 &eb, BTRFS_TREE_RELOC_OBJECTID);
5035 BUG_ON(ret);
5036
5037 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5038 root_key.offset = root->root_key.objectid;
5039 root_key.type = BTRFS_ROOT_ITEM_KEY;
5040
5041 memcpy(root_item, &root->root_item, sizeof(root_item));
5042 btrfs_set_root_refs(root_item, 0);
5043 btrfs_set_root_bytenr(root_item, eb->start);
5044 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04005045 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005046
5047 btrfs_tree_unlock(eb);
5048 free_extent_buffer(eb);
5049
5050 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5051 &root_key, root_item);
5052 BUG_ON(ret);
5053 kfree(root_item);
5054
5055 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5056 &root_key);
5057 BUG_ON(!reloc_root);
5058 reloc_root->last_trans = trans->transid;
5059 reloc_root->commit_root = NULL;
5060 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5061
5062 root->reloc_root = reloc_root;
5063 return 0;
5064}
5065
5066/*
5067 * Core function of space balance.
5068 *
5069 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04005070 * counted roots. There is one reloc tree for each subvol, and all
5071 * reloc trees share same root key objectid. Reloc trees are snapshots
5072 * of the latest committed roots of subvols (root->commit_root).
5073 *
5074 * To relocate a tree block referenced by a subvol, there are two steps.
5075 * COW the block through subvol's reloc tree, then update block pointer
5076 * in the subvol to point to the new block. Since all reloc trees share
5077 * same root key objectid, doing special handing for tree blocks owned
5078 * by them is easy. Once a tree block has been COWed in one reloc tree,
5079 * we can use the resulting new block directly when the same block is
5080 * required to COW again through other reloc trees. By this way, relocated
5081 * tree blocks are shared between reloc trees, so they are also shared
5082 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04005083 */
5084static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
5085 struct btrfs_root *root,
5086 struct btrfs_path *path,
5087 struct btrfs_key *first_key,
5088 struct btrfs_ref_path *ref_path,
5089 struct btrfs_block_group_cache *group,
5090 struct inode *reloc_inode)
5091{
5092 struct btrfs_root *reloc_root;
5093 struct extent_buffer *eb = NULL;
5094 struct btrfs_key *keys;
5095 u64 *nodes;
5096 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005097 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005098 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005099 int ret;
5100
5101 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5102 lowest_level = ref_path->owner_objectid;
5103
Yan Zhengf82d02d2008-10-29 14:49:05 -04005104 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005105 path->lowest_level = lowest_level;
5106 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5107 BUG_ON(ret < 0);
5108 path->lowest_level = 0;
5109 btrfs_release_path(root, path);
5110 return 0;
5111 }
5112
Zheng Yan1a40e232008-09-26 10:09:34 -04005113 mutex_lock(&root->fs_info->tree_reloc_mutex);
5114 ret = init_reloc_tree(trans, root);
5115 BUG_ON(ret);
5116 reloc_root = root->reloc_root;
5117
Yan Zhengf82d02d2008-10-29 14:49:05 -04005118 shared_level = ref_path->shared_level;
5119 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005120
Yan Zhengf82d02d2008-10-29 14:49:05 -04005121 keys = ref_path->node_keys;
5122 nodes = ref_path->new_nodes;
5123 memset(&keys[shared_level + 1], 0,
5124 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5125 memset(&nodes[shared_level + 1], 0,
5126 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04005127
Yan Zhengf82d02d2008-10-29 14:49:05 -04005128 if (nodes[lowest_level] == 0) {
5129 path->lowest_level = lowest_level;
5130 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5131 0, 1);
5132 BUG_ON(ret);
5133 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5134 eb = path->nodes[level];
5135 if (!eb || eb == reloc_root->node)
5136 break;
5137 nodes[level] = eb->start;
5138 if (level == 0)
5139 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5140 else
5141 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5142 }
Yan Zheng2b820322008-11-17 21:11:30 -05005143 if (nodes[0] &&
5144 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005145 eb = path->nodes[0];
5146 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5147 group, reloc_inode);
5148 BUG_ON(ret);
5149 }
5150 btrfs_release_path(reloc_root, path);
5151 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005152 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005153 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04005154 BUG_ON(ret);
5155 }
5156
Zheng Yan1a40e232008-09-26 10:09:34 -04005157 /*
5158 * replace tree blocks in the fs tree with tree blocks in
5159 * the reloc tree.
5160 */
5161 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5162 BUG_ON(ret < 0);
5163
5164 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005165 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5166 0, 0);
5167 BUG_ON(ret);
5168 extent_buffer_get(path->nodes[0]);
5169 eb = path->nodes[0];
5170 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005171 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5172 BUG_ON(ret);
5173 free_extent_buffer(eb);
5174 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005175
Yan Zhengf82d02d2008-10-29 14:49:05 -04005176 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005177 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005178 return 0;
5179}
5180
5181static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
5182 struct btrfs_root *root,
5183 struct btrfs_path *path,
5184 struct btrfs_key *first_key,
5185 struct btrfs_ref_path *ref_path)
5186{
5187 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005188
5189 ret = relocate_one_path(trans, root, path, first_key,
5190 ref_path, NULL, NULL);
5191 BUG_ON(ret);
5192
5193 if (root == root->fs_info->extent_root)
5194 btrfs_extent_post_op(trans, root);
Zheng Yan1a40e232008-09-26 10:09:34 -04005195
5196 return 0;
5197}
5198
5199static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
5200 struct btrfs_root *extent_root,
5201 struct btrfs_path *path,
5202 struct btrfs_key *extent_key)
5203{
5204 int ret;
5205
Zheng Yan1a40e232008-09-26 10:09:34 -04005206 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5207 if (ret)
5208 goto out;
5209 ret = btrfs_del_item(trans, extent_root, path);
5210out:
Chris Masonedbd8d42007-12-21 16:27:24 -05005211 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005212 return ret;
5213}
5214
5215static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
5216 struct btrfs_ref_path *ref_path)
5217{
5218 struct btrfs_key root_key;
5219
5220 root_key.objectid = ref_path->root_objectid;
5221 root_key.type = BTRFS_ROOT_ITEM_KEY;
5222 if (is_cowonly_root(ref_path->root_objectid))
5223 root_key.offset = 0;
5224 else
5225 root_key.offset = (u64)-1;
5226
5227 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5228}
5229
5230static int noinline relocate_one_extent(struct btrfs_root *extent_root,
5231 struct btrfs_path *path,
5232 struct btrfs_key *extent_key,
5233 struct btrfs_block_group_cache *group,
5234 struct inode *reloc_inode, int pass)
5235{
5236 struct btrfs_trans_handle *trans;
5237 struct btrfs_root *found_root;
5238 struct btrfs_ref_path *ref_path = NULL;
5239 struct disk_extent *new_extents = NULL;
5240 int nr_extents = 0;
5241 int loops;
5242 int ret;
5243 int level;
5244 struct btrfs_key first_key;
5245 u64 prev_block = 0;
5246
Zheng Yan1a40e232008-09-26 10:09:34 -04005247
5248 trans = btrfs_start_transaction(extent_root, 1);
5249 BUG_ON(!trans);
5250
5251 if (extent_key->objectid == 0) {
5252 ret = del_extent_zero(trans, extent_root, path, extent_key);
5253 goto out;
5254 }
5255
5256 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5257 if (!ref_path) {
5258 ret = -ENOMEM;
5259 goto out;
5260 }
5261
5262 for (loops = 0; ; loops++) {
5263 if (loops == 0) {
5264 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5265 extent_key->objectid);
5266 } else {
5267 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5268 }
5269 if (ret < 0)
5270 goto out;
5271 if (ret > 0)
5272 break;
5273
5274 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5275 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5276 continue;
5277
5278 found_root = read_ref_root(extent_root->fs_info, ref_path);
5279 BUG_ON(!found_root);
5280 /*
5281 * for reference counted tree, only process reference paths
5282 * rooted at the latest committed root.
5283 */
5284 if (found_root->ref_cows &&
5285 ref_path->root_generation != found_root->root_key.offset)
5286 continue;
5287
5288 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5289 if (pass == 0) {
5290 /*
5291 * copy data extents to new locations
5292 */
5293 u64 group_start = group->key.objectid;
5294 ret = relocate_data_extent(reloc_inode,
5295 extent_key,
5296 group_start);
5297 if (ret < 0)
5298 goto out;
5299 break;
5300 }
5301 level = 0;
5302 } else {
5303 level = ref_path->owner_objectid;
5304 }
5305
5306 if (prev_block != ref_path->nodes[level]) {
5307 struct extent_buffer *eb;
5308 u64 block_start = ref_path->nodes[level];
5309 u64 block_size = btrfs_level_size(found_root, level);
5310
5311 eb = read_tree_block(found_root, block_start,
5312 block_size, 0);
5313 btrfs_tree_lock(eb);
5314 BUG_ON(level != btrfs_header_level(eb));
5315
5316 if (level == 0)
5317 btrfs_item_key_to_cpu(eb, &first_key, 0);
5318 else
5319 btrfs_node_key_to_cpu(eb, &first_key, 0);
5320
5321 btrfs_tree_unlock(eb);
5322 free_extent_buffer(eb);
5323 prev_block = block_start;
5324 }
5325
5326 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
5327 pass >= 2) {
5328 /*
5329 * use fallback method to process the remaining
5330 * references.
5331 */
5332 if (!new_extents) {
5333 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04005334 new_extents = kmalloc(sizeof(*new_extents),
5335 GFP_NOFS);
5336 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005337 ret = get_new_locations(reloc_inode,
5338 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04005339 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04005340 &new_extents,
5341 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005342 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04005343 goto out;
5344 }
5345 btrfs_record_root_in_trans(found_root);
5346 ret = replace_one_extent(trans, found_root,
5347 path, extent_key,
5348 &first_key, ref_path,
5349 new_extents, nr_extents);
5350 if (ret < 0)
5351 goto out;
5352 continue;
5353 }
5354
5355 btrfs_record_root_in_trans(found_root);
5356 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5357 ret = relocate_tree_block(trans, found_root, path,
5358 &first_key, ref_path);
5359 } else {
5360 /*
5361 * try to update data extent references while
5362 * keeping metadata shared between snapshots.
5363 */
5364 ret = relocate_one_path(trans, found_root, path,
5365 &first_key, ref_path,
5366 group, reloc_inode);
5367 }
5368 if (ret < 0)
5369 goto out;
5370 }
5371 ret = 0;
5372out:
5373 btrfs_end_transaction(trans, extent_root);
5374 kfree(new_extents);
5375 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005376 return ret;
5377}
5378
Chris Masonec44a352008-04-28 15:29:52 -04005379static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5380{
5381 u64 num_devices;
5382 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5383 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5384
Yan Zheng2b820322008-11-17 21:11:30 -05005385 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04005386 if (num_devices == 1) {
5387 stripped |= BTRFS_BLOCK_GROUP_DUP;
5388 stripped = flags & ~stripped;
5389
5390 /* turn raid0 into single device chunks */
5391 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5392 return stripped;
5393
5394 /* turn mirroring into duplication */
5395 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5396 BTRFS_BLOCK_GROUP_RAID10))
5397 return stripped | BTRFS_BLOCK_GROUP_DUP;
5398 return flags;
5399 } else {
5400 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04005401 if (flags & stripped)
5402 return flags;
5403
5404 stripped |= BTRFS_BLOCK_GROUP_DUP;
5405 stripped = flags & ~stripped;
5406
5407 /* switch duplicated blocks with raid1 */
5408 if (flags & BTRFS_BLOCK_GROUP_DUP)
5409 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5410
5411 /* turn single device chunks into raid0 */
5412 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5413 }
5414 return flags;
5415}
5416
Chris Mason0ef3e662008-05-24 14:04:53 -04005417int __alloc_chunk_for_shrink(struct btrfs_root *root,
5418 struct btrfs_block_group_cache *shrink_block_group,
5419 int force)
5420{
5421 struct btrfs_trans_handle *trans;
5422 u64 new_alloc_flags;
5423 u64 calc;
5424
Chris Masonc286ac42008-07-22 23:06:41 -04005425 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005426 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04005427 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005428
Chris Mason0ef3e662008-05-24 14:04:53 -04005429 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005430 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04005431
Chris Mason0ef3e662008-05-24 14:04:53 -04005432 new_alloc_flags = update_block_group_flags(root,
5433 shrink_block_group->flags);
5434 if (new_alloc_flags != shrink_block_group->flags) {
5435 calc =
5436 btrfs_block_group_used(&shrink_block_group->item);
5437 } else {
5438 calc = shrink_block_group->key.offset;
5439 }
Chris Masonc286ac42008-07-22 23:06:41 -04005440 spin_unlock(&shrink_block_group->lock);
5441
Chris Mason0ef3e662008-05-24 14:04:53 -04005442 do_chunk_alloc(trans, root->fs_info->extent_root,
5443 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04005444
Chris Mason0ef3e662008-05-24 14:04:53 -04005445 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04005446 } else
5447 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005448 return 0;
5449}
5450
Zheng Yan1a40e232008-09-26 10:09:34 -04005451static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5452 struct btrfs_root *root,
5453 u64 objectid, u64 size)
5454{
5455 struct btrfs_path *path;
5456 struct btrfs_inode_item *item;
5457 struct extent_buffer *leaf;
5458 int ret;
5459
5460 path = btrfs_alloc_path();
5461 if (!path)
5462 return -ENOMEM;
5463
5464 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5465 if (ret)
5466 goto out;
5467
5468 leaf = path->nodes[0];
5469 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5470 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5471 btrfs_set_inode_generation(leaf, item, 1);
5472 btrfs_set_inode_size(leaf, item, size);
5473 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zhengd899e052008-10-30 14:25:28 -04005474 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM |
5475 BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005476 btrfs_mark_buffer_dirty(leaf);
5477 btrfs_release_path(root, path);
5478out:
5479 btrfs_free_path(path);
5480 return ret;
5481}
5482
5483static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
5484 struct btrfs_block_group_cache *group)
5485{
5486 struct inode *inode = NULL;
5487 struct btrfs_trans_handle *trans;
5488 struct btrfs_root *root;
5489 struct btrfs_key root_key;
5490 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5491 int err = 0;
5492
5493 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5494 root_key.type = BTRFS_ROOT_ITEM_KEY;
5495 root_key.offset = (u64)-1;
5496 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5497 if (IS_ERR(root))
5498 return ERR_CAST(root);
5499
5500 trans = btrfs_start_transaction(root, 1);
5501 BUG_ON(!trans);
5502
5503 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5504 if (err)
5505 goto out;
5506
5507 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5508 BUG_ON(err);
5509
5510 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04005511 group->key.offset, 0, group->key.offset,
5512 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005513 BUG_ON(err);
5514
5515 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5516 if (inode->i_state & I_NEW) {
5517 BTRFS_I(inode)->root = root;
5518 BTRFS_I(inode)->location.objectid = objectid;
5519 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5520 BTRFS_I(inode)->location.offset = 0;
5521 btrfs_read_locked_inode(inode);
5522 unlock_new_inode(inode);
5523 BUG_ON(is_bad_inode(inode));
5524 } else {
5525 BUG_ON(1);
5526 }
5527
5528 err = btrfs_orphan_add(trans, inode);
5529out:
5530 btrfs_end_transaction(trans, root);
5531 if (err) {
5532 if (inode)
5533 iput(inode);
5534 inode = ERR_PTR(err);
5535 }
5536 return inode;
5537}
5538
5539int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05005540{
5541 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05005542 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04005543 struct btrfs_fs_info *info = root->fs_info;
5544 struct extent_buffer *leaf;
5545 struct inode *reloc_inode;
5546 struct btrfs_block_group_cache *block_group;
5547 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04005548 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05005549 u64 cur_byte;
5550 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05005551 u32 nritems;
5552 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04005553 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04005554 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005555
Chris Masonedbd8d42007-12-21 16:27:24 -05005556 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04005557
5558 block_group = btrfs_lookup_block_group(info, group_start);
5559 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05005560
Chris Mason323da792008-05-09 11:46:48 -04005561 printk("btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005562 (unsigned long long)block_group->key.objectid,
5563 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04005564
Zheng Yan1a40e232008-09-26 10:09:34 -04005565 path = btrfs_alloc_path();
5566 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04005567
Zheng Yan1a40e232008-09-26 10:09:34 -04005568 reloc_inode = create_reloc_inode(info, block_group);
5569 BUG_ON(IS_ERR(reloc_inode));
5570
Zheng Yan1a40e232008-09-26 10:09:34 -04005571 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05005572 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005573
Zheng Yan1a40e232008-09-26 10:09:34 -04005574 btrfs_start_delalloc_inodes(info->tree_root);
5575 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04005576again:
Yan Zhengd899e052008-10-30 14:25:28 -04005577 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005578 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04005579 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005580 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05005581 key.offset = 0;
5582 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05005583 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05005584
Zheng Yan1a40e232008-09-26 10:09:34 -04005585 trans = btrfs_start_transaction(info->tree_root, 1);
5586 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04005587
Zheng Yan1a40e232008-09-26 10:09:34 -04005588 mutex_lock(&root->fs_info->cleaner_mutex);
5589 btrfs_clean_old_snapshots(info->tree_root);
5590 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5591 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04005592
Yan73e48b22008-01-03 14:14:39 -05005593 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05005594 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5595 if (ret < 0)
5596 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04005597next:
Chris Masonedbd8d42007-12-21 16:27:24 -05005598 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05005599 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05005600 if (path->slots[0] >= nritems) {
5601 ret = btrfs_next_leaf(root, path);
5602 if (ret < 0)
5603 goto out;
5604 if (ret == 1) {
5605 ret = 0;
5606 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005607 }
Yan73e48b22008-01-03 14:14:39 -05005608 leaf = path->nodes[0];
5609 nritems = btrfs_header_nritems(leaf);
5610 }
5611
Zheng Yan1a40e232008-09-26 10:09:34 -04005612 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05005613
Zheng Yan1a40e232008-09-26 10:09:34 -04005614 if (key.objectid >= block_group->key.objectid +
5615 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04005616 break;
5617
Chris Mason725c8462008-01-04 16:47:16 -05005618 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05005619 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005620 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05005621 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005622 continue;
Chris Mason725c8462008-01-04 16:47:16 -05005623 }
5624 progress = 1;
5625
Zheng Yan1a40e232008-09-26 10:09:34 -04005626 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5627 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05005628 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005629 goto next;
5630 }
Yan73e48b22008-01-03 14:14:39 -05005631
Chris Masonedbd8d42007-12-21 16:27:24 -05005632 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005633 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05005634 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005635
5636 __alloc_chunk_for_shrink(root, block_group, 0);
5637 ret = relocate_one_extent(root, path, &key, block_group,
5638 reloc_inode, pass);
5639 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005640 if (ret > 0)
5641 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005642
5643 key.objectid = cur_byte;
5644 key.type = 0;
5645 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005646 }
5647
5648 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005649
5650 if (pass == 0) {
5651 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5652 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5653 WARN_ON(reloc_inode->i_mapping->nrpages);
5654 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005655
5656 if (total_found > 0) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005657 printk("btrfs found %llu extents in pass %d\n",
5658 (unsigned long long)total_found, pass);
5659 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04005660 if (total_found == skipped && pass > 2) {
5661 iput(reloc_inode);
5662 reloc_inode = create_reloc_inode(info, block_group);
5663 pass = 0;
5664 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005665 goto again;
5666 }
5667
Zheng Yan1a40e232008-09-26 10:09:34 -04005668 /* delete reloc_inode */
5669 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005670
Zheng Yan1a40e232008-09-26 10:09:34 -04005671 /* unpin extents in this range */
5672 trans = btrfs_start_transaction(info->tree_root, 1);
5673 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005674
Zheng Yan1a40e232008-09-26 10:09:34 -04005675 spin_lock(&block_group->lock);
5676 WARN_ON(block_group->pinned > 0);
5677 WARN_ON(block_group->reserved > 0);
5678 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5679 spin_unlock(&block_group->lock);
5680 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005681out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005682 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005683 return ret;
5684}
5685
Chris Mason0b86a832008-03-24 15:01:56 -04005686int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
5687 struct btrfs_key *key)
5688{
Chris Mason925baed2008-06-25 16:01:30 -04005689 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005690 struct btrfs_key found_key;
5691 struct extent_buffer *leaf;
5692 int slot;
5693
5694 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5695 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005696 goto out;
5697
Chris Mason0b86a832008-03-24 15:01:56 -04005698 while(1) {
5699 slot = path->slots[0];
5700 leaf = path->nodes[0];
5701 if (slot >= btrfs_header_nritems(leaf)) {
5702 ret = btrfs_next_leaf(root, path);
5703 if (ret == 0)
5704 continue;
5705 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005706 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005707 break;
5708 }
5709 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5710
5711 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005712 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5713 ret = 0;
5714 goto out;
5715 }
Chris Mason0b86a832008-03-24 15:01:56 -04005716 path->slots[0]++;
5717 }
5718 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005719out:
Chris Mason0b86a832008-03-24 15:01:56 -04005720 return ret;
5721}
5722
Zheng Yan1a40e232008-09-26 10:09:34 -04005723int btrfs_free_block_groups(struct btrfs_fs_info *info)
5724{
5725 struct btrfs_block_group_cache *block_group;
5726 struct rb_node *n;
5727
Zheng Yan1a40e232008-09-26 10:09:34 -04005728 spin_lock(&info->block_group_cache_lock);
5729 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5730 block_group = rb_entry(n, struct btrfs_block_group_cache,
5731 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04005732 rb_erase(&block_group->cache_node,
5733 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04005734 spin_unlock(&info->block_group_cache_lock);
5735
5736 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005737 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005738 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005739 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005740 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04005741
5742 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005743 }
5744 spin_unlock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005745 return 0;
5746}
5747
Chris Mason9078a3e2007-04-26 16:46:15 -04005748int btrfs_read_block_groups(struct btrfs_root *root)
5749{
5750 struct btrfs_path *path;
5751 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005752 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005753 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005754 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005755 struct btrfs_key key;
5756 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005757 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005758
Chris Masonbe744172007-05-06 10:15:01 -04005759 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005760 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005761 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005762 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005763 path = btrfs_alloc_path();
5764 if (!path)
5765 return -ENOMEM;
5766
5767 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005768 ret = find_first_block_group(root, path, &key);
5769 if (ret > 0) {
5770 ret = 0;
5771 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005772 }
Chris Mason0b86a832008-03-24 15:01:56 -04005773 if (ret != 0)
5774 goto error;
5775
Chris Mason5f39d392007-10-15 16:14:19 -04005776 leaf = path->nodes[0];
5777 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005778 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005779 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005780 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005781 break;
5782 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005783
Chris Masonc286ac42008-07-22 23:06:41 -04005784 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005785 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005786 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005787 read_extent_buffer(leaf, &cache->item,
5788 btrfs_item_ptr_offset(leaf, path->slots[0]),
5789 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005790 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005791
Chris Mason9078a3e2007-04-26 16:46:15 -04005792 key.objectid = found_key.objectid + found_key.offset;
5793 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005794 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005795
Chris Mason6324fbf2008-03-24 15:01:59 -04005796 ret = update_space_info(info, cache->flags, found_key.offset,
5797 btrfs_block_group_used(&cache->item),
5798 &space_info);
5799 BUG_ON(ret);
5800 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005801 down_write(&space_info->groups_sem);
5802 list_add_tail(&cache->list, &space_info->block_groups);
5803 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005804
Josef Bacik0f9dd462008-09-23 13:14:11 -04005805 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5806 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005807
5808 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05005809 if (btrfs_chunk_readonly(root, cache->key.objectid))
5810 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04005811 }
Chris Mason0b86a832008-03-24 15:01:56 -04005812 ret = 0;
5813error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005814 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005815 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005816}
Chris Mason6324fbf2008-03-24 15:01:59 -04005817
5818int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5819 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005820 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005821 u64 size)
5822{
5823 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005824 struct btrfs_root *extent_root;
5825 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005826
5827 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005828
Chris Masone02119d2008-09-05 16:13:11 -04005829 root->fs_info->last_trans_new_blockgroup = trans->transid;
5830
Chris Mason8f18cf12008-04-25 16:53:30 -04005831 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005832 if (!cache)
5833 return -ENOMEM;
5834
Chris Masone17cade2008-04-15 15:41:47 -04005835 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005836 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04005837 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005838 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005839 INIT_LIST_HEAD(&cache->list);
Chris Mason6324fbf2008-03-24 15:01:59 -04005840 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04005841
Chris Mason6324fbf2008-03-24 15:01:59 -04005842 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005843 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5844 cache->flags = type;
5845 btrfs_set_block_group_flags(&cache->item, type);
5846
5847 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5848 &cache->space_info);
5849 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005850 down_write(&cache->space_info->groups_sem);
5851 list_add_tail(&cache->list, &cache->space_info->block_groups);
5852 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005853
Josef Bacik0f9dd462008-09-23 13:14:11 -04005854 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5855 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005856
Chris Mason6324fbf2008-03-24 15:01:59 -04005857 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5858 sizeof(cache->item));
5859 BUG_ON(ret);
5860
Chris Mason87ef2bb2008-10-30 11:23:27 -04005861 finish_current_insert(trans, extent_root, 0);
5862 ret = del_pending_extents(trans, extent_root, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04005863 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04005864 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005865
Chris Mason6324fbf2008-03-24 15:01:59 -04005866 return 0;
5867}
Zheng Yan1a40e232008-09-26 10:09:34 -04005868
5869int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5870 struct btrfs_root *root, u64 group_start)
5871{
5872 struct btrfs_path *path;
5873 struct btrfs_block_group_cache *block_group;
5874 struct btrfs_key key;
5875 int ret;
5876
Zheng Yan1a40e232008-09-26 10:09:34 -04005877 root = root->fs_info->extent_root;
5878
5879 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5880 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05005881 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04005882
5883 memcpy(&key, &block_group->key, sizeof(key));
5884
5885 path = btrfs_alloc_path();
5886 BUG_ON(!path);
5887
5888 btrfs_remove_free_space_cache(block_group);
5889 rb_erase(&block_group->cache_node,
5890 &root->fs_info->block_group_cache_tree);
Josef Bacik80eb2342008-10-29 14:49:05 -04005891 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005892 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005893 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005894
Yan Zhengc146afa2008-11-12 14:34:12 -05005895 spin_lock(&block_group->space_info->lock);
5896 block_group->space_info->total_bytes -= block_group->key.offset;
5897 block_group->space_info->bytes_readonly -= block_group->key.offset;
5898 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05005899 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05005900
Zheng Yan1a40e232008-09-26 10:09:34 -04005901 /*
5902 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5903 kfree(shrink_block_group);
5904 */
5905
5906 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5907 if (ret > 0)
5908 ret = -EIO;
5909 if (ret < 0)
5910 goto out;
5911
5912 ret = btrfs_del_item(trans, root, path);
5913out:
5914 btrfs_free_path(path);
5915 return ret;
5916}