blob: ee73efe75423e71b47e38e3096081f0b1c644a43 [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;
Josef Bacikf3465ca2008-11-12 14:19:50 -0500801 total--;
Liu Huib4eec2c2008-11-18 11:30:10 -0500802 if (i < total) {
803 cur = insert_list->next;
804 op = list_entry(cur, struct pending_extent_op,
805 list);
806 }
Josef Bacikf3465ca2008-11-12 14:19:50 -0500807 } else {
808 i += ret;
809 }
810
811 cond_resched();
812 }
813 ret = 0;
814 kfree(keys);
815 kfree(data_size);
816 return ret;
817}
818
Zheng Yan31840ae2008-09-23 13:14:14 -0400819static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
820 struct btrfs_root *root,
821 struct btrfs_path *path,
822 u64 bytenr, u64 parent,
823 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400824 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400825{
826 struct btrfs_key key;
827 struct extent_buffer *leaf;
828 struct btrfs_extent_ref *ref;
829 u32 num_refs;
830 int ret;
831
832 key.objectid = bytenr;
833 key.type = BTRFS_EXTENT_REF_KEY;
834 key.offset = parent;
835
836 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
837 if (ret == 0) {
838 leaf = path->nodes[0];
839 ref = btrfs_item_ptr(leaf, path->slots[0],
840 struct btrfs_extent_ref);
841 btrfs_set_ref_root(leaf, ref, ref_root);
842 btrfs_set_ref_generation(leaf, ref, ref_generation);
843 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400844 btrfs_set_ref_num_refs(leaf, ref, 1);
845 } else if (ret == -EEXIST) {
846 u64 existing_owner;
847 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
848 leaf = path->nodes[0];
849 ref = btrfs_item_ptr(leaf, path->slots[0],
850 struct btrfs_extent_ref);
851 if (btrfs_ref_root(leaf, ref) != ref_root ||
852 btrfs_ref_generation(leaf, ref) != ref_generation) {
853 ret = -EIO;
854 WARN_ON(1);
855 goto out;
856 }
857
858 num_refs = btrfs_ref_num_refs(leaf, ref);
859 BUG_ON(num_refs == 0);
860 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
861
862 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400863 if (existing_owner != owner_objectid &&
864 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400865 btrfs_set_ref_objectid(leaf, ref,
866 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400867 }
868 ret = 0;
869 } else {
870 goto out;
871 }
Chris Mason7bb86312007-12-11 09:25:06 -0500872 btrfs_mark_buffer_dirty(path->nodes[0]);
873out:
874 btrfs_release_path(root, path);
875 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500876}
877
Zheng Yan31840ae2008-09-23 13:14:14 -0400878static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
879 struct btrfs_root *root,
880 struct btrfs_path *path)
881{
882 struct extent_buffer *leaf;
883 struct btrfs_extent_ref *ref;
884 u32 num_refs;
885 int ret = 0;
886
887 leaf = path->nodes[0];
888 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
889 num_refs = btrfs_ref_num_refs(leaf, ref);
890 BUG_ON(num_refs == 0);
891 num_refs -= 1;
892 if (num_refs == 0) {
893 ret = btrfs_del_item(trans, root, path);
894 } else {
895 btrfs_set_ref_num_refs(leaf, ref, num_refs);
896 btrfs_mark_buffer_dirty(leaf);
897 }
898 btrfs_release_path(root, path);
899 return ret;
900}
901
Josef Bacikf3465ca2008-11-12 14:19:50 -0500902static int noinline free_extents(struct btrfs_trans_handle *trans,
903 struct btrfs_root *extent_root,
904 struct list_head *del_list)
905{
906 struct btrfs_fs_info *info = extent_root->fs_info;
907 struct btrfs_path *path;
908 struct btrfs_key key, found_key;
909 struct extent_buffer *leaf;
910 struct list_head *cur;
911 struct pending_extent_op *op;
912 struct btrfs_extent_item *ei;
913 int ret, num_to_del, extent_slot = 0, found_extent = 0;
914 u32 refs;
915 u64 bytes_freed = 0;
916
917 path = btrfs_alloc_path();
918 if (!path)
919 return -ENOMEM;
920 path->reada = 1;
921
922search:
923 /* search for the backref for the current ref we want to delete */
924 cur = del_list->next;
925 op = list_entry(cur, struct pending_extent_op, list);
926 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
927 op->orig_parent,
928 extent_root->root_key.objectid,
929 op->orig_generation, op->level, 1);
930 if (ret) {
931 printk("Unable to find backref byte nr %Lu root %Lu gen %Lu "
932 "owner %u\n", op->bytenr,
933 extent_root->root_key.objectid, op->orig_generation,
934 op->level);
935 btrfs_print_leaf(extent_root, path->nodes[0]);
936 WARN_ON(1);
937 goto out;
938 }
939
940 extent_slot = path->slots[0];
941 num_to_del = 1;
942 found_extent = 0;
943
944 /*
945 * if we aren't the first item on the leaf we can move back one and see
946 * if our ref is right next to our extent item
947 */
948 if (likely(extent_slot)) {
949 extent_slot--;
950 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
951 extent_slot);
952 if (found_key.objectid == op->bytenr &&
953 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
954 found_key.offset == op->num_bytes) {
955 num_to_del++;
956 found_extent = 1;
957 }
958 }
959
960 /*
961 * if we didn't find the extent we need to delete the backref and then
962 * search for the extent item key so we can update its ref count
963 */
964 if (!found_extent) {
965 key.objectid = op->bytenr;
966 key.type = BTRFS_EXTENT_ITEM_KEY;
967 key.offset = op->num_bytes;
968
969 ret = remove_extent_backref(trans, extent_root, path);
970 BUG_ON(ret);
971 btrfs_release_path(extent_root, path);
972 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
973 BUG_ON(ret);
974 extent_slot = path->slots[0];
975 }
976
977 /* this is where we update the ref count for the extent */
978 leaf = path->nodes[0];
979 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
980 refs = btrfs_extent_refs(leaf, ei);
981 BUG_ON(refs == 0);
982 refs--;
983 btrfs_set_extent_refs(leaf, ei, refs);
984
985 btrfs_mark_buffer_dirty(leaf);
986
987 /*
988 * This extent needs deleting. The reason cur_slot is extent_slot +
989 * num_to_del is because extent_slot points to the slot where the extent
990 * is, and if the backref was not right next to the extent we will be
991 * deleting at least 1 item, and will want to start searching at the
992 * slot directly next to extent_slot. However if we did find the
993 * backref next to the extent item them we will be deleting at least 2
994 * items and will want to start searching directly after the ref slot
995 */
996 if (!refs) {
997 struct list_head *pos, *n, *end;
998 int cur_slot = extent_slot+num_to_del;
999 u64 super_used;
1000 u64 root_used;
1001
1002 path->slots[0] = extent_slot;
1003 bytes_freed = op->num_bytes;
1004
Josef Bacike3e469f2008-11-17 21:11:49 -05001005 mutex_lock(&info->pinned_mutex);
1006 ret = pin_down_bytes(trans, extent_root, op->bytenr,
1007 op->num_bytes, op->level >=
1008 BTRFS_FIRST_FREE_OBJECTID);
1009 mutex_unlock(&info->pinned_mutex);
1010 BUG_ON(ret < 0);
1011 op->del = ret;
1012
Josef Bacikf3465ca2008-11-12 14:19:50 -05001013 /*
1014 * we need to see if we can delete multiple things at once, so
1015 * start looping through the list of extents we are wanting to
1016 * delete and see if their extent/backref's are right next to
1017 * eachother and the extents only have 1 ref
1018 */
1019 for (pos = cur->next; pos != del_list; pos = pos->next) {
1020 struct pending_extent_op *tmp;
1021
1022 tmp = list_entry(pos, struct pending_extent_op, list);
1023
1024 /* we only want to delete extent+ref at this stage */
1025 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1026 break;
1027
1028 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1029 if (found_key.objectid != tmp->bytenr ||
1030 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1031 found_key.offset != tmp->num_bytes)
1032 break;
1033
1034 /* check to make sure this extent only has one ref */
1035 ei = btrfs_item_ptr(leaf, cur_slot,
1036 struct btrfs_extent_item);
1037 if (btrfs_extent_refs(leaf, ei) != 1)
1038 break;
1039
1040 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1041 if (found_key.objectid != tmp->bytenr ||
1042 found_key.type != BTRFS_EXTENT_REF_KEY ||
1043 found_key.offset != tmp->orig_parent)
1044 break;
1045
1046 /*
1047 * the ref is right next to the extent, we can set the
1048 * ref count to 0 since we will delete them both now
1049 */
1050 btrfs_set_extent_refs(leaf, ei, 0);
1051
1052 /* pin down the bytes for this extent */
1053 mutex_lock(&info->pinned_mutex);
1054 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1055 tmp->num_bytes, tmp->level >=
1056 BTRFS_FIRST_FREE_OBJECTID);
1057 mutex_unlock(&info->pinned_mutex);
1058 BUG_ON(ret < 0);
1059
1060 /*
1061 * use the del field to tell if we need to go ahead and
1062 * free up the extent when we delete the item or not.
1063 */
1064 tmp->del = ret;
1065 bytes_freed += tmp->num_bytes;
1066
1067 num_to_del += 2;
1068 cur_slot += 2;
1069 }
1070 end = pos;
1071
1072 /* update the free space counters */
1073 spin_lock_irq(&info->delalloc_lock);
1074 super_used = btrfs_super_bytes_used(&info->super_copy);
1075 btrfs_set_super_bytes_used(&info->super_copy,
1076 super_used - bytes_freed);
1077 spin_unlock_irq(&info->delalloc_lock);
1078
1079 root_used = btrfs_root_used(&extent_root->root_item);
1080 btrfs_set_root_used(&extent_root->root_item,
1081 root_used - bytes_freed);
1082
1083 /* delete the items */
1084 ret = btrfs_del_items(trans, extent_root, path,
1085 path->slots[0], num_to_del);
1086 BUG_ON(ret);
1087
1088 /*
1089 * loop through the extents we deleted and do the cleanup work
1090 * on them
1091 */
1092 for (pos = cur, n = pos->next; pos != end;
1093 pos = n, n = pos->next) {
1094 struct pending_extent_op *tmp;
1095#ifdef BIO_RW_DISCARD
1096 u64 map_length;
1097 struct btrfs_multi_bio *multi = NULL;
1098#endif
1099 tmp = list_entry(pos, struct pending_extent_op, list);
1100
1101 /*
1102 * remember tmp->del tells us wether or not we pinned
1103 * down the extent
1104 */
1105 ret = update_block_group(trans, extent_root,
1106 tmp->bytenr, tmp->num_bytes, 0,
1107 tmp->del);
1108 BUG_ON(ret);
1109
1110#ifdef BIO_RW_DISCARD
1111 ret = btrfs_map_block(&info->mapping_tree, READ,
1112 tmp->bytenr, &map_length, &multi,
1113 0);
1114 if (!ret) {
1115 struct btrfs_bio_stripe *stripe;
1116 int i;
1117
1118 stripe = multi->stripe;
1119
1120 if (map_length > tmp->num_bytes)
1121 map_length = tmp->num_bytes;
1122
1123 for (i = 0; i < multi->num_stripes;
1124 i++, stripe++)
1125 blkdev_issue_discard(stripe->dev->bdev,
1126 stripe->physical >> 9,
1127 map_length >> 9);
1128 kfree(multi);
1129 }
1130#endif
1131 list_del_init(&tmp->list);
1132 unlock_extent(&info->extent_ins, tmp->bytenr,
1133 tmp->bytenr + tmp->num_bytes - 1,
1134 GFP_NOFS);
1135 kfree(tmp);
1136 }
1137 } else if (refs && found_extent) {
1138 /*
1139 * the ref and extent were right next to eachother, but the
1140 * extent still has a ref, so just free the backref and keep
1141 * going
1142 */
1143 ret = remove_extent_backref(trans, extent_root, path);
1144 BUG_ON(ret);
1145
1146 list_del_init(&op->list);
1147 unlock_extent(&info->extent_ins, op->bytenr,
1148 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1149 kfree(op);
1150 } else {
1151 /*
1152 * the extent has multiple refs and the backref we were looking
1153 * for was not right next to it, so just unlock and go next,
1154 * we're good to go
1155 */
1156 list_del_init(&op->list);
1157 unlock_extent(&info->extent_ins, op->bytenr,
1158 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1159 kfree(op);
1160 }
1161
1162 btrfs_release_path(extent_root, path);
1163 if (!list_empty(del_list))
1164 goto search;
1165
1166out:
1167 btrfs_free_path(path);
1168 return ret;
1169}
1170
Zheng Yan31840ae2008-09-23 13:14:14 -04001171static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1172 struct btrfs_root *root, u64 bytenr,
1173 u64 orig_parent, u64 parent,
1174 u64 orig_root, u64 ref_root,
1175 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001176 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001177{
1178 int ret;
1179 struct btrfs_root *extent_root = root->fs_info->extent_root;
1180 struct btrfs_path *path;
1181
1182 if (root == root->fs_info->extent_root) {
1183 struct pending_extent_op *extent_op;
1184 u64 num_bytes;
1185
1186 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1187 num_bytes = btrfs_level_size(root, (int)owner_objectid);
Josef Bacik25179202008-10-29 14:49:05 -04001188 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001189 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
Josef Bacik25179202008-10-29 14:49:05 -04001190 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001191 u64 priv;
1192 ret = get_state_private(&root->fs_info->extent_ins,
1193 bytenr, &priv);
1194 BUG_ON(ret);
1195 extent_op = (struct pending_extent_op *)
1196 (unsigned long)priv;
1197 BUG_ON(extent_op->parent != orig_parent);
1198 BUG_ON(extent_op->generation != orig_generation);
Josef Bacik25179202008-10-29 14:49:05 -04001199
Zheng Yan31840ae2008-09-23 13:14:14 -04001200 extent_op->parent = parent;
1201 extent_op->generation = ref_generation;
1202 } else {
1203 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1204 BUG_ON(!extent_op);
1205
1206 extent_op->type = PENDING_BACKREF_UPDATE;
1207 extent_op->bytenr = bytenr;
1208 extent_op->num_bytes = num_bytes;
1209 extent_op->parent = parent;
1210 extent_op->orig_parent = orig_parent;
1211 extent_op->generation = ref_generation;
1212 extent_op->orig_generation = orig_generation;
1213 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001214 INIT_LIST_HEAD(&extent_op->list);
1215 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001216
1217 set_extent_bits(&root->fs_info->extent_ins,
1218 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04001219 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04001220 set_state_private(&root->fs_info->extent_ins,
1221 bytenr, (unsigned long)extent_op);
1222 }
Josef Bacik25179202008-10-29 14:49:05 -04001223 mutex_unlock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001224 return 0;
1225 }
1226
1227 path = btrfs_alloc_path();
1228 if (!path)
1229 return -ENOMEM;
1230 ret = lookup_extent_backref(trans, extent_root, path,
1231 bytenr, orig_parent, orig_root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001232 orig_generation, owner_objectid, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001233 if (ret)
1234 goto out;
1235 ret = remove_extent_backref(trans, extent_root, path);
1236 if (ret)
1237 goto out;
1238 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1239 parent, ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001240 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001241 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001242 finish_current_insert(trans, extent_root, 0);
1243 del_pending_extents(trans, extent_root, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001244out:
1245 btrfs_free_path(path);
1246 return ret;
1247}
1248
1249int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1250 struct btrfs_root *root, u64 bytenr,
1251 u64 orig_parent, u64 parent,
1252 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001253 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001254{
1255 int ret;
1256 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1257 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1258 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001259 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1260 parent, ref_root, ref_root,
1261 ref_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001262 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001263 return ret;
1264}
1265
Chris Mason925baed2008-06-25 16:01:30 -04001266static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001267 struct btrfs_root *root, u64 bytenr,
1268 u64 orig_parent, u64 parent,
1269 u64 orig_root, u64 ref_root,
1270 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001271 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -05001272{
Chris Mason5caf2a02007-04-02 11:20:42 -04001273 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -05001274 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001275 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001276 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001277 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001278 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001279
Chris Mason5caf2a02007-04-02 11:20:42 -04001280 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001281 if (!path)
1282 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001283
Chris Mason3c12ac72008-04-21 12:01:38 -04001284 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001285 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001286 key.type = BTRFS_EXTENT_ITEM_KEY;
1287 key.offset = (u64)-1;
1288
Chris Mason5caf2a02007-04-02 11:20:42 -04001289 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001290 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001291 if (ret < 0)
1292 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001293 BUG_ON(ret == 0 || path->slots[0] == 0);
1294
1295 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -04001296 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -04001297
1298 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Mason771ed682008-11-06 22:02:51 -05001299 if (key.objectid != bytenr) {
1300 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
1301 printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
1302 BUG();
1303 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001304 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1305
Chris Mason5caf2a02007-04-02 11:20:42 -04001306 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001307 refs = btrfs_extent_refs(l, item);
1308 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001309 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -05001310
Chris Mason5caf2a02007-04-02 11:20:42 -04001311 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001312
Chris Mason3c12ac72008-04-21 12:01:38 -04001313 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001314 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1315 path, bytenr, parent,
1316 ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001317 owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05001318 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001319 finish_current_insert(trans, root->fs_info->extent_root, 0);
1320 del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Mason74493f72007-12-11 09:25:06 -05001321
1322 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -05001323 return 0;
1324}
1325
Chris Mason925baed2008-06-25 16:01:30 -04001326int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001327 struct btrfs_root *root,
1328 u64 bytenr, u64 num_bytes, u64 parent,
1329 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001330 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -04001331{
1332 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001333 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1334 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1335 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001336 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1337 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001338 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -04001339 return ret;
1340}
1341
Chris Masone9d0b132007-08-10 14:06:19 -04001342int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1343 struct btrfs_root *root)
1344{
Chris Mason87ef2bb2008-10-30 11:23:27 -04001345 finish_current_insert(trans, root->fs_info->extent_root, 1);
1346 del_pending_extents(trans, root->fs_info->extent_root, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001347 return 0;
1348}
1349
Zheng Yan31840ae2008-09-23 13:14:14 -04001350int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1351 struct btrfs_root *root, u64 bytenr,
1352 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -05001353{
Chris Mason5caf2a02007-04-02 11:20:42 -04001354 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -05001355 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001356 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001357 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001358 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -04001359
Chris Masondb945352007-10-15 16:15:53 -04001360 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -04001361 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -04001362 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001363 key.objectid = bytenr;
1364 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -04001365 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -04001366 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001367 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001368 if (ret < 0)
1369 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -04001370 if (ret != 0) {
1371 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -04001372 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -05001373 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04001374 }
1375 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001376 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001377 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001378out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001379 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05001380 return 0;
1381}
1382
Yan Zheng80ff3852008-10-30 14:20:02 -04001383int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1384 struct btrfs_root *root, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05001385{
1386 struct btrfs_root *extent_root = root->fs_info->extent_root;
1387 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -04001388 struct extent_buffer *leaf;
1389 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -05001390 struct btrfs_key key;
1391 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001392 u64 ref_root;
1393 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -04001394 u32 nritems;
1395 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001396
Chris Masonbe20aa92007-12-17 20:14:01 -05001397 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001398 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04001399 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05001400
Yan Zhengf321e492008-07-30 09:26:11 -04001401 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -05001402 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1403 if (ret < 0)
1404 goto out;
1405 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001406
1407 ret = -ENOENT;
1408 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04001409 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001410
Zheng Yan31840ae2008-09-23 13:14:14 -04001411 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04001412 leaf = path->nodes[0];
1413 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001414
1415 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001416 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05001417 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001418
Yan Zheng80ff3852008-10-30 14:20:02 -04001419 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05001420 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -04001421 leaf = path->nodes[0];
1422 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -05001423 if (path->slots[0] >= nritems) {
1424 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -04001425 if (ret < 0)
1426 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001427 if (ret == 0)
1428 continue;
1429 break;
1430 }
Yan Zhengf321e492008-07-30 09:26:11 -04001431 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001432 if (found_key.objectid != bytenr)
1433 break;
Chris Masonbd098352008-01-03 13:23:19 -05001434
Chris Masonbe20aa92007-12-17 20:14:01 -05001435 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1436 path->slots[0]++;
1437 continue;
1438 }
1439
Yan Zhengf321e492008-07-30 09:26:11 -04001440 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001441 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -04001442 ref_root = btrfs_ref_root(leaf, ref_item);
1443 if (ref_root != root->root_key.objectid &&
1444 ref_root != BTRFS_TREE_LOG_OBJECTID) {
1445 ret = 1;
1446 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -04001447 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001448 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1449 ret = 1;
1450 goto out;
1451 }
Yan Zhengf321e492008-07-30 09:26:11 -04001452
Chris Masonbe20aa92007-12-17 20:14:01 -05001453 path->slots[0]++;
1454 }
Yan Zhengf321e492008-07-30 09:26:11 -04001455 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001456out:
Yan Zhengf321e492008-07-30 09:26:11 -04001457 btrfs_free_path(path);
1458 return ret;
1459}
1460
Zheng Yan31840ae2008-09-23 13:14:14 -04001461int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1462 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001463{
Chris Mason5f39d392007-10-15 16:14:19 -04001464 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001465 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001466 u64 root_gen;
1467 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001468 int i;
Chris Masondb945352007-10-15 16:15:53 -04001469 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001470 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001471 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001472
Chris Mason3768f362007-03-13 16:47:54 -04001473 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001474 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001475
Zheng Yane4657682008-09-26 10:04:53 -04001476 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1477 shared = 0;
1478 root_gen = root->root_key.offset;
1479 } else {
1480 shared = 1;
1481 root_gen = trans->transid - 1;
1482 }
1483
Chris Masondb945352007-10-15 16:15:53 -04001484 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001485 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001486
Zheng Yan31840ae2008-09-23 13:14:14 -04001487 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001488 struct btrfs_leaf_ref *ref;
1489 struct btrfs_extent_info *info;
1490
Zheng Yan31840ae2008-09-23 13:14:14 -04001491 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001492 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001493 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001494 goto out;
1495 }
1496
Zheng Yane4657682008-09-26 10:04:53 -04001497 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001498 ref->bytenr = buf->start;
1499 ref->owner = btrfs_header_owner(buf);
1500 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001501 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001502 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001503
Zheng Yan31840ae2008-09-23 13:14:14 -04001504 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001505 u64 disk_bytenr;
1506 btrfs_item_key_to_cpu(buf, &key, i);
1507 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1508 continue;
1509 fi = btrfs_item_ptr(buf, i,
1510 struct btrfs_file_extent_item);
1511 if (btrfs_file_extent_type(buf, fi) ==
1512 BTRFS_FILE_EXTENT_INLINE)
1513 continue;
1514 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1515 if (disk_bytenr == 0)
1516 continue;
1517
1518 info->bytenr = disk_bytenr;
1519 info->num_bytes =
1520 btrfs_file_extent_disk_num_bytes(buf, fi);
1521 info->objectid = key.objectid;
1522 info->offset = key.offset;
1523 info++;
1524 }
1525
Zheng Yane4657682008-09-26 10:04:53 -04001526 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001527 if (ret == -EEXIST && shared) {
1528 struct btrfs_leaf_ref *old;
1529 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1530 BUG_ON(!old);
1531 btrfs_remove_leaf_ref(root, old);
1532 btrfs_free_leaf_ref(root, old);
1533 ret = btrfs_add_leaf_ref(root, ref, shared);
1534 }
Yan Zheng31153d82008-07-28 15:32:19 -04001535 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001536 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001537 }
1538out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001539 return ret;
1540}
1541
1542int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1543 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1544 u32 *nr_extents)
1545{
1546 u64 bytenr;
1547 u64 ref_root;
1548 u64 orig_root;
1549 u64 ref_generation;
1550 u64 orig_generation;
1551 u32 nritems;
1552 u32 nr_file_extents = 0;
1553 struct btrfs_key key;
1554 struct btrfs_file_extent_item *fi;
1555 int i;
1556 int level;
1557 int ret = 0;
1558 int faili = 0;
1559 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001560 u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001561
1562 ref_root = btrfs_header_owner(buf);
1563 ref_generation = btrfs_header_generation(buf);
1564 orig_root = btrfs_header_owner(orig_buf);
1565 orig_generation = btrfs_header_generation(orig_buf);
1566
1567 nritems = btrfs_header_nritems(buf);
1568 level = btrfs_header_level(buf);
1569
1570 if (root->ref_cows) {
1571 process_func = __btrfs_inc_extent_ref;
1572 } else {
1573 if (level == 0 &&
1574 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1575 goto out;
1576 if (level != 0 &&
1577 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1578 goto out;
1579 process_func = __btrfs_update_extent_ref;
1580 }
1581
1582 for (i = 0; i < nritems; i++) {
1583 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001584 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001585 btrfs_item_key_to_cpu(buf, &key, i);
1586 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001587 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001588 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001589 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001590 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001591 BTRFS_FILE_EXTENT_INLINE)
1592 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001593 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1594 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001595 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001596
1597 nr_file_extents++;
1598
Zheng Yan31840ae2008-09-23 13:14:14 -04001599 ret = process_func(trans, root, bytenr,
1600 orig_buf->start, buf->start,
1601 orig_root, ref_root,
1602 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001603 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001604
1605 if (ret) {
1606 faili = i;
1607 WARN_ON(1);
1608 goto fail;
1609 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001610 } else {
Chris Masondb945352007-10-15 16:15:53 -04001611 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001612 ret = process_func(trans, root, bytenr,
1613 orig_buf->start, buf->start,
1614 orig_root, ref_root,
1615 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001616 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001617 if (ret) {
1618 faili = i;
1619 WARN_ON(1);
1620 goto fail;
1621 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001622 }
1623 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001624out:
1625 if (nr_extents) {
1626 if (level == 0)
1627 *nr_extents = nr_file_extents;
1628 else
1629 *nr_extents = nritems;
1630 }
1631 return 0;
1632fail:
1633 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001634 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001635}
1636
Zheng Yan31840ae2008-09-23 13:14:14 -04001637int btrfs_update_ref(struct btrfs_trans_handle *trans,
1638 struct btrfs_root *root, struct extent_buffer *orig_buf,
1639 struct extent_buffer *buf, int start_slot, int nr)
1640
1641{
1642 u64 bytenr;
1643 u64 ref_root;
1644 u64 orig_root;
1645 u64 ref_generation;
1646 u64 orig_generation;
1647 struct btrfs_key key;
1648 struct btrfs_file_extent_item *fi;
1649 int i;
1650 int ret;
1651 int slot;
1652 int level;
1653
1654 BUG_ON(start_slot < 0);
1655 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1656
1657 ref_root = btrfs_header_owner(buf);
1658 ref_generation = btrfs_header_generation(buf);
1659 orig_root = btrfs_header_owner(orig_buf);
1660 orig_generation = btrfs_header_generation(orig_buf);
1661 level = btrfs_header_level(buf);
1662
1663 if (!root->ref_cows) {
1664 if (level == 0 &&
1665 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1666 return 0;
1667 if (level != 0 &&
1668 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1669 return 0;
1670 }
1671
1672 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1673 cond_resched();
1674 if (level == 0) {
1675 btrfs_item_key_to_cpu(buf, &key, slot);
1676 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1677 continue;
1678 fi = btrfs_item_ptr(buf, slot,
1679 struct btrfs_file_extent_item);
1680 if (btrfs_file_extent_type(buf, fi) ==
1681 BTRFS_FILE_EXTENT_INLINE)
1682 continue;
1683 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1684 if (bytenr == 0)
1685 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001686 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1687 orig_buf->start, buf->start,
1688 orig_root, ref_root,
1689 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001690 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001691 if (ret)
1692 goto fail;
1693 } else {
1694 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001695 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1696 orig_buf->start, buf->start,
1697 orig_root, ref_root,
1698 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001699 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001700 if (ret)
1701 goto fail;
1702 }
1703 }
1704 return 0;
1705fail:
1706 WARN_ON(1);
1707 return -1;
1708}
1709
Chris Mason9078a3e2007-04-26 16:46:15 -04001710static int write_one_cache_group(struct btrfs_trans_handle *trans,
1711 struct btrfs_root *root,
1712 struct btrfs_path *path,
1713 struct btrfs_block_group_cache *cache)
1714{
1715 int ret;
1716 int pending_ret;
1717 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001718 unsigned long bi;
1719 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001720
Chris Mason9078a3e2007-04-26 16:46:15 -04001721 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001722 if (ret < 0)
1723 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001724 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001725
1726 leaf = path->nodes[0];
1727 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1728 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1729 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001730 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001731fail:
Chris Mason87ef2bb2008-10-30 11:23:27 -04001732 finish_current_insert(trans, extent_root, 0);
1733 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001734 if (ret)
1735 return ret;
1736 if (pending_ret)
1737 return pending_ret;
1738 return 0;
1739
1740}
1741
Chris Mason96b51792007-10-15 16:15:19 -04001742int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1743 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001744{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001745 struct btrfs_block_group_cache *cache, *entry;
1746 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001747 int err = 0;
1748 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001749 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001750 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001751
1752 path = btrfs_alloc_path();
1753 if (!path)
1754 return -ENOMEM;
1755
1756 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001757 cache = NULL;
1758 spin_lock(&root->fs_info->block_group_cache_lock);
1759 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1760 n; n = rb_next(n)) {
1761 entry = rb_entry(n, struct btrfs_block_group_cache,
1762 cache_node);
1763 if (entry->dirty) {
1764 cache = entry;
1765 break;
1766 }
1767 }
1768 spin_unlock(&root->fs_info->block_group_cache_lock);
1769
1770 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001771 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001772
Zheng Yane8569812008-09-26 10:05:48 -04001773 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001774 last += cache->key.offset;
1775
Chris Mason96b51792007-10-15 16:15:19 -04001776 err = write_one_cache_group(trans, root,
1777 path, cache);
1778 /*
1779 * if we fail to write the cache group, we want
1780 * to keep it marked dirty in hopes that a later
1781 * write will work
1782 */
1783 if (err) {
1784 werr = err;
1785 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001786 }
1787 }
1788 btrfs_free_path(path);
1789 return werr;
1790}
1791
Chris Mason593060d2008-03-25 16:50:33 -04001792static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1793 u64 total_bytes, u64 bytes_used,
1794 struct btrfs_space_info **space_info)
1795{
1796 struct btrfs_space_info *found;
1797
1798 found = __find_space_info(info, flags);
1799 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001800 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001801 found->total_bytes += total_bytes;
1802 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001803 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001804 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001805 *space_info = found;
1806 return 0;
1807 }
Yan Zhengc146afa2008-11-12 14:34:12 -05001808 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04001809 if (!found)
1810 return -ENOMEM;
1811
1812 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001813 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001814 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001815 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001816 found->flags = flags;
1817 found->total_bytes = total_bytes;
1818 found->bytes_used = bytes_used;
1819 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001820 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05001821 found->bytes_readonly = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001822 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001823 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001824 *space_info = found;
1825 return 0;
1826}
1827
Chris Mason8790d502008-04-03 16:29:03 -04001828static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1829{
1830 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001831 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001832 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001833 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001834 if (extra_flags) {
1835 if (flags & BTRFS_BLOCK_GROUP_DATA)
1836 fs_info->avail_data_alloc_bits |= extra_flags;
1837 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1838 fs_info->avail_metadata_alloc_bits |= extra_flags;
1839 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1840 fs_info->avail_system_alloc_bits |= extra_flags;
1841 }
1842}
Chris Mason593060d2008-03-25 16:50:33 -04001843
Yan Zhengc146afa2008-11-12 14:34:12 -05001844static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1845{
1846 spin_lock(&cache->space_info->lock);
1847 spin_lock(&cache->lock);
1848 if (!cache->ro) {
1849 cache->space_info->bytes_readonly += cache->key.offset -
1850 btrfs_block_group_used(&cache->item);
1851 cache->ro = 1;
1852 }
1853 spin_unlock(&cache->lock);
1854 spin_unlock(&cache->space_info->lock);
1855}
1856
Yan Zheng2b820322008-11-17 21:11:30 -05001857u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001858{
Yan Zheng2b820322008-11-17 21:11:30 -05001859 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001860
1861 if (num_devices == 1)
1862 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1863 if (num_devices < 4)
1864 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1865
Chris Masonec44a352008-04-28 15:29:52 -04001866 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1867 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001868 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001869 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001870 }
Chris Masonec44a352008-04-28 15:29:52 -04001871
1872 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001873 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001874 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001875 }
Chris Masonec44a352008-04-28 15:29:52 -04001876
1877 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1878 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1879 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1880 (flags & BTRFS_BLOCK_GROUP_DUP)))
1881 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1882 return flags;
1883}
1884
Chris Mason6324fbf2008-03-24 15:01:59 -04001885static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1886 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001887 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001888{
1889 struct btrfs_space_info *space_info;
1890 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05001891 int ret = 0;
1892
1893 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001894
Yan Zheng2b820322008-11-17 21:11:30 -05001895 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001896
Chris Mason6324fbf2008-03-24 15:01:59 -04001897 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001898 if (!space_info) {
1899 ret = update_space_info(extent_root->fs_info, flags,
1900 0, 0, &space_info);
1901 BUG_ON(ret);
1902 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001903 BUG_ON(!space_info);
1904
Josef Bacik25179202008-10-29 14:49:05 -04001905 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001906 if (space_info->force_alloc) {
1907 force = 1;
1908 space_info->force_alloc = 0;
1909 }
Josef Bacik25179202008-10-29 14:49:05 -04001910 if (space_info->full) {
1911 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001912 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001913 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001914
Yan Zhengc146afa2008-11-12 14:34:12 -05001915 thresh = space_info->total_bytes - space_info->bytes_readonly;
1916 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001917 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001918 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001919 space_info->bytes_reserved + alloc_bytes) < thresh) {
1920 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001921 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001922 }
Josef Bacik25179202008-10-29 14:49:05 -04001923 spin_unlock(&space_info->lock);
1924
Yan Zheng2b820322008-11-17 21:11:30 -05001925 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik25179202008-10-29 14:49:05 -04001926 if (ret) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001927printk("space info full %Lu\n", flags);
1928 space_info->full = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001929 }
Chris Masona74a4b92008-06-25 16:01:31 -04001930out:
Yan Zhengc146afa2008-11-12 14:34:12 -05001931 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001932 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001933}
1934
Chris Mason9078a3e2007-04-26 16:46:15 -04001935static int update_block_group(struct btrfs_trans_handle *trans,
1936 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001937 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001938 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001939{
1940 struct btrfs_block_group_cache *cache;
1941 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001942 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001943 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001944 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001945
Chris Mason9078a3e2007-04-26 16:46:15 -04001946 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001947 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001948 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001949 return -1;
Chris Masondb945352007-10-15 16:15:53 -04001950 byte_in_group = bytenr - cache->key.objectid;
1951 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001952
Josef Bacik25179202008-10-29 14:49:05 -04001953 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001954 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001955 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001956 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001957 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001958 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001959 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001960 cache->space_info->bytes_used += num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001961 if (cache->ro) {
1962 cache->space_info->bytes_readonly -= num_bytes;
1963 WARN_ON(1);
1964 }
Chris Masonc286ac42008-07-22 23:06:41 -04001965 btrfs_set_block_group_used(&cache->item, old_val);
1966 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001967 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001968 } else {
Chris Masondb945352007-10-15 16:15:53 -04001969 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001970 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001971 if (cache->ro)
1972 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001973 btrfs_set_block_group_used(&cache->item, old_val);
1974 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001975 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001976 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001977 int ret;
1978 ret = btrfs_add_free_space(cache, bytenr,
1979 num_bytes);
1980 if (ret)
1981 return -1;
Chris Masone37c9e62007-05-09 20:13:14 -04001982 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001983 }
Chris Masondb945352007-10-15 16:15:53 -04001984 total -= num_bytes;
1985 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001986 }
1987 return 0;
1988}
Chris Mason6324fbf2008-03-24 15:01:59 -04001989
Chris Masona061fc82008-05-07 11:43:44 -04001990static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1991{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001992 struct btrfs_block_group_cache *cache;
1993
1994 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1995 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001996 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001997
1998 return cache->key.objectid;
Chris Masona061fc82008-05-07 11:43:44 -04001999}
2000
Chris Masone02119d2008-09-05 16:13:11 -04002001int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05002002 u64 bytenr, u64 num, int pin)
2003{
2004 u64 len;
2005 struct btrfs_block_group_cache *cache;
2006 struct btrfs_fs_info *fs_info = root->fs_info;
2007
Josef Bacik25179202008-10-29 14:49:05 -04002008 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05002009 if (pin) {
2010 set_extent_dirty(&fs_info->pinned_extents,
2011 bytenr, bytenr + num - 1, GFP_NOFS);
2012 } else {
2013 clear_extent_dirty(&fs_info->pinned_extents,
2014 bytenr, bytenr + num - 1, GFP_NOFS);
2015 }
2016 while (num > 0) {
2017 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002018 BUG_ON(!cache);
2019 len = min(num, cache->key.offset -
2020 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002021 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002022 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002023 spin_lock(&cache->lock);
2024 cache->pinned += len;
2025 cache->space_info->bytes_pinned += len;
2026 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002027 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002028 fs_info->total_pinned += len;
2029 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002030 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002031 spin_lock(&cache->lock);
2032 cache->pinned -= len;
2033 cache->space_info->bytes_pinned -= len;
2034 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002035 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002036 fs_info->total_pinned -= len;
Josef Bacik07103a32008-11-19 15:17:55 -05002037 if (cache->cached)
2038 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05002039 }
2040 bytenr += len;
2041 num -= len;
2042 }
2043 return 0;
2044}
Chris Mason9078a3e2007-04-26 16:46:15 -04002045
Zheng Yane8569812008-09-26 10:05:48 -04002046static int update_reserved_extents(struct btrfs_root *root,
2047 u64 bytenr, u64 num, int reserve)
2048{
2049 u64 len;
2050 struct btrfs_block_group_cache *cache;
2051 struct btrfs_fs_info *fs_info = root->fs_info;
2052
Zheng Yane8569812008-09-26 10:05:48 -04002053 while (num > 0) {
2054 cache = btrfs_lookup_block_group(fs_info, bytenr);
2055 BUG_ON(!cache);
2056 len = min(num, cache->key.offset -
2057 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04002058
2059 spin_lock(&cache->space_info->lock);
2060 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002061 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04002062 cache->reserved += len;
2063 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04002064 } else {
Zheng Yane8569812008-09-26 10:05:48 -04002065 cache->reserved -= len;
2066 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04002067 }
Josef Bacik25179202008-10-29 14:49:05 -04002068 spin_unlock(&cache->lock);
2069 spin_unlock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002070 bytenr += len;
2071 num -= len;
2072 }
2073 return 0;
2074}
2075
Chris Masond1310b22008-01-24 16:13:08 -05002076int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04002077{
Chris Masonccd467d2007-06-28 15:57:36 -04002078 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002079 u64 start;
2080 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002081 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04002082 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04002083
Josef Bacik25179202008-10-29 14:49:05 -04002084 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002085 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002086 ret = find_first_extent_bit(pinned_extents, last,
2087 &start, &end, EXTENT_DIRTY);
2088 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04002089 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04002090 set_extent_dirty(copy, start, end, GFP_NOFS);
2091 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04002092 }
Josef Bacik25179202008-10-29 14:49:05 -04002093 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002094 return 0;
2095}
2096
2097int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2098 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05002099 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05002100{
Chris Mason1a5bc162007-10-15 16:15:26 -04002101 u64 start;
2102 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002103 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002104
Josef Bacik25179202008-10-29 14:49:05 -04002105 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002106 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002107 ret = find_first_extent_bit(unpin, 0, &start, &end,
2108 EXTENT_DIRTY);
2109 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05002110 break;
Chris Masone02119d2008-09-05 16:13:11 -04002111 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04002112 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04002113 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04002114 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002115 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04002116 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002117 }
Chris Masona28ec192007-03-06 20:08:01 -05002118 }
Josef Bacik25179202008-10-29 14:49:05 -04002119 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002120 return 0;
2121}
2122
Chris Mason98ed5172008-01-03 10:01:48 -05002123static int finish_current_insert(struct btrfs_trans_handle *trans,
Chris Mason87ef2bb2008-10-30 11:23:27 -04002124 struct btrfs_root *extent_root, int all)
Chris Mason037e6392007-03-07 11:50:24 -05002125{
Chris Mason7bb86312007-12-11 09:25:06 -05002126 u64 start;
2127 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002128 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002129 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002130 u64 skipped = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002131 struct btrfs_fs_info *info = extent_root->fs_info;
2132 struct btrfs_path *path;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002133 struct pending_extent_op *extent_op, *tmp;
2134 struct list_head insert_list, update_list;
Chris Mason037e6392007-03-07 11:50:24 -05002135 int ret;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002136 int num_inserts = 0, max_inserts;
Chris Mason037e6392007-03-07 11:50:24 -05002137
Chris Mason7bb86312007-12-11 09:25:06 -05002138 path = btrfs_alloc_path();
Josef Bacikf3465ca2008-11-12 14:19:50 -05002139 INIT_LIST_HEAD(&insert_list);
2140 INIT_LIST_HEAD(&update_list);
Chris Mason037e6392007-03-07 11:50:24 -05002141
Josef Bacikf3465ca2008-11-12 14:19:50 -05002142 max_inserts = extent_root->leafsize /
2143 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2144 sizeof(struct btrfs_extent_ref) +
2145 sizeof(struct btrfs_extent_item));
2146again:
2147 mutex_lock(&info->extent_ins_mutex);
2148 while (1) {
Josef Bacik25179202008-10-29 14:49:05 -04002149 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2150 &end, EXTENT_WRITEBACK);
2151 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002152 if (skipped && all && !num_inserts) {
2153 skipped = 0;
Liu Huib4eec2c2008-11-18 11:30:10 -05002154 search = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002155 continue;
2156 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002157 mutex_unlock(&info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04002158 break;
Josef Bacik25179202008-10-29 14:49:05 -04002159 }
2160
2161 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2162 if (!ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002163 skipped = 1;
Chris Mason87ef2bb2008-10-30 11:23:27 -04002164 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002165 if (need_resched()) {
2166 mutex_unlock(&info->extent_ins_mutex);
2167 cond_resched();
2168 mutex_lock(&info->extent_ins_mutex);
2169 }
Josef Bacik25179202008-10-29 14:49:05 -04002170 continue;
2171 }
Chris Mason26b80032007-08-08 20:17:12 -04002172
Zheng Yan31840ae2008-09-23 13:14:14 -04002173 ret = get_state_private(&info->extent_ins, start, &priv);
2174 BUG_ON(ret);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002175 extent_op = (struct pending_extent_op *)(unsigned long) priv;
Josef Bacik25179202008-10-29 14:49:05 -04002176
Zheng Yan31840ae2008-09-23 13:14:14 -04002177 if (extent_op->type == PENDING_EXTENT_INSERT) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002178 num_inserts++;
2179 list_add_tail(&extent_op->list, &insert_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002180 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002181 if (num_inserts == max_inserts) {
2182 mutex_unlock(&info->extent_ins_mutex);
2183 break;
2184 }
2185 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2186 list_add_tail(&extent_op->list, &update_list);
2187 search = end + 1;
2188 } else {
2189 BUG();
2190 }
Chris Mason037e6392007-03-07 11:50:24 -05002191 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002192
2193 /*
Liu Huib4eec2c2008-11-18 11:30:10 -05002194 * process the update list, clear the writeback bit for it, and if
Josef Bacikf3465ca2008-11-12 14:19:50 -05002195 * somebody marked this thing for deletion then just unlock it and be
2196 * done, the free_extents will handle it
2197 */
2198 mutex_lock(&info->extent_ins_mutex);
2199 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2200 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2201 extent_op->bytenr + extent_op->num_bytes - 1,
2202 EXTENT_WRITEBACK, GFP_NOFS);
2203 if (extent_op->del) {
2204 list_del_init(&extent_op->list);
2205 unlock_extent(&info->extent_ins, extent_op->bytenr,
2206 extent_op->bytenr + extent_op->num_bytes
2207 - 1, GFP_NOFS);
2208 kfree(extent_op);
2209 }
2210 }
2211 mutex_unlock(&info->extent_ins_mutex);
2212
2213 /*
2214 * still have things left on the update list, go ahead an update
2215 * everything
2216 */
2217 if (!list_empty(&update_list)) {
2218 ret = update_backrefs(trans, extent_root, path, &update_list);
2219 BUG_ON(ret);
2220 }
2221
2222 /*
2223 * if no inserts need to be done, but we skipped some extents and we
2224 * need to make sure everything is cleaned then reset everything and
2225 * go back to the beginning
2226 */
2227 if (!num_inserts && all && skipped) {
2228 search = 0;
2229 skipped = 0;
2230 INIT_LIST_HEAD(&update_list);
2231 INIT_LIST_HEAD(&insert_list);
2232 goto again;
2233 } else if (!num_inserts) {
2234 goto out;
2235 }
2236
2237 /*
2238 * process the insert extents list. Again if we are deleting this
2239 * extent, then just unlock it, pin down the bytes if need be, and be
2240 * done with it. Saves us from having to actually insert the extent
2241 * into the tree and then subsequently come along and delete it
2242 */
2243 mutex_lock(&info->extent_ins_mutex);
2244 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2245 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2246 extent_op->bytenr + extent_op->num_bytes - 1,
2247 EXTENT_WRITEBACK, GFP_NOFS);
2248 if (extent_op->del) {
2249 list_del_init(&extent_op->list);
2250 unlock_extent(&info->extent_ins, extent_op->bytenr,
2251 extent_op->bytenr + extent_op->num_bytes
2252 - 1, GFP_NOFS);
2253
2254 mutex_lock(&extent_root->fs_info->pinned_mutex);
2255 ret = pin_down_bytes(trans, extent_root,
2256 extent_op->bytenr,
2257 extent_op->num_bytes, 0);
2258 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2259
2260 ret = update_block_group(trans, extent_root,
2261 extent_op->bytenr,
2262 extent_op->num_bytes,
2263 0, ret > 0);
2264 BUG_ON(ret);
2265 kfree(extent_op);
2266 num_inserts--;
2267 }
2268 }
2269 mutex_unlock(&info->extent_ins_mutex);
2270
2271 ret = insert_extents(trans, extent_root, path, &insert_list,
2272 num_inserts);
2273 BUG_ON(ret);
2274
2275 /*
2276 * if we broke out of the loop in order to insert stuff because we hit
2277 * the maximum number of inserts at a time we can handle, then loop
2278 * back and pick up where we left off
2279 */
2280 if (num_inserts == max_inserts) {
2281 INIT_LIST_HEAD(&insert_list);
2282 INIT_LIST_HEAD(&update_list);
2283 num_inserts = 0;
2284 goto again;
2285 }
2286
2287 /*
2288 * again, if we need to make absolutely sure there are no more pending
2289 * extent operations left and we know that we skipped some, go back to
2290 * the beginning and do it all again
2291 */
2292 if (all && skipped) {
2293 INIT_LIST_HEAD(&insert_list);
2294 INIT_LIST_HEAD(&update_list);
2295 search = 0;
2296 skipped = 0;
2297 num_inserts = 0;
2298 goto again;
2299 }
2300out:
Chris Mason7bb86312007-12-11 09:25:06 -05002301 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05002302 return 0;
2303}
2304
Zheng Yan31840ae2008-09-23 13:14:14 -04002305static int pin_down_bytes(struct btrfs_trans_handle *trans,
2306 struct btrfs_root *root,
2307 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04002308{
Chris Mason1a5bc162007-10-15 16:15:26 -04002309 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002310 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04002311
Zheng Yan31840ae2008-09-23 13:14:14 -04002312 if (is_data)
2313 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002314
Zheng Yan31840ae2008-09-23 13:14:14 -04002315 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2316 if (!buf)
2317 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002318
Zheng Yan31840ae2008-09-23 13:14:14 -04002319 /* we can reuse a block if it hasn't been written
2320 * and it is from this transaction. We can't
2321 * reuse anything from the tree log root because
2322 * it has tiny sub-transactions.
2323 */
2324 if (btrfs_buffer_uptodate(buf, 0) &&
2325 btrfs_try_tree_lock(buf)) {
2326 u64 header_owner = btrfs_header_owner(buf);
2327 u64 header_transid = btrfs_header_generation(buf);
2328 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04002329 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04002330 header_transid == trans->transid &&
2331 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2332 clean_tree_block(NULL, root, buf);
2333 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002334 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002335 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04002336 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002337 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04002338 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002339 free_extent_buffer(buf);
2340pinit:
2341 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2342
Chris Masonbe744172007-05-06 10:15:01 -04002343 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002344 return 0;
2345}
2346
Chris Masona28ec192007-03-06 20:08:01 -05002347/*
2348 * remove an extent from the root, returns 0 on success
2349 */
Zheng Yan31840ae2008-09-23 13:14:14 -04002350static int __free_extent(struct btrfs_trans_handle *trans,
2351 struct btrfs_root *root,
2352 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002353 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002354 u64 owner_objectid, int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05002355{
Chris Mason5caf2a02007-04-02 11:20:42 -04002356 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04002357 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04002358 struct btrfs_fs_info *info = root->fs_info;
2359 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002360 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05002361 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05002362 int extent_slot = 0;
2363 int found_extent = 0;
2364 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04002365 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04002366 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05002367
Chris Masondb945352007-10-15 16:15:53 -04002368 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04002369 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04002370 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04002371 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002372 if (!path)
2373 return -ENOMEM;
2374
Chris Mason3c12ac72008-04-21 12:01:38 -04002375 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002376 ret = lookup_extent_backref(trans, extent_root, path,
2377 bytenr, parent, root_objectid,
2378 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05002379 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002380 struct btrfs_key found_key;
2381 extent_slot = path->slots[0];
2382 while(extent_slot > 0) {
2383 extent_slot--;
2384 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2385 extent_slot);
2386 if (found_key.objectid != bytenr)
2387 break;
2388 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2389 found_key.offset == num_bytes) {
2390 found_extent = 1;
2391 break;
2392 }
2393 if (path->slots[0] - extent_slot > 5)
2394 break;
2395 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002396 if (!found_extent) {
2397 ret = remove_extent_backref(trans, extent_root, path);
2398 BUG_ON(ret);
2399 btrfs_release_path(extent_root, path);
2400 ret = btrfs_search_slot(trans, extent_root,
2401 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002402 if (ret) {
2403 printk(KERN_ERR "umm, got %d back from search"
2404 ", was looking for %Lu\n", ret,
2405 bytenr);
2406 btrfs_print_leaf(extent_root, path->nodes[0]);
2407 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002408 BUG_ON(ret);
2409 extent_slot = path->slots[0];
2410 }
Chris Mason7bb86312007-12-11 09:25:06 -05002411 } else {
2412 btrfs_print_leaf(extent_root, path->nodes[0]);
2413 WARN_ON(1);
2414 printk("Unable to find ref byte nr %Lu root %Lu "
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002415 "gen %Lu owner %Lu\n", bytenr,
2416 root_objectid, ref_generation, owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05002417 }
Chris Mason5f39d392007-10-15 16:14:19 -04002418
2419 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05002420 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04002421 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002422 refs = btrfs_extent_refs(leaf, ei);
2423 BUG_ON(refs == 0);
2424 refs -= 1;
2425 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05002426
Chris Mason5f39d392007-10-15 16:14:19 -04002427 btrfs_mark_buffer_dirty(leaf);
2428
Chris Mason952fcca2008-02-18 16:33:44 -05002429 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002430 struct btrfs_extent_ref *ref;
2431 ref = btrfs_item_ptr(leaf, path->slots[0],
2432 struct btrfs_extent_ref);
2433 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002434 /* if the back ref and the extent are next to each other
2435 * they get deleted below in one shot
2436 */
2437 path->slots[0] = extent_slot;
2438 num_to_del = 2;
2439 } else if (found_extent) {
2440 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002441 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05002442 BUG_ON(ret);
2443 /* if refs are 0, we need to setup the path for deletion */
2444 if (refs == 0) {
2445 btrfs_release_path(extent_root, path);
2446 ret = btrfs_search_slot(trans, extent_root, &key, path,
2447 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002448 BUG_ON(ret);
2449 }
2450 }
2451
Chris Masoncf27e1e2007-03-13 09:49:06 -04002452 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002453 u64 super_used;
2454 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01002455#ifdef BIO_RW_DISCARD
2456 u64 map_length = num_bytes;
2457 struct btrfs_multi_bio *multi = NULL;
2458#endif
Chris Mason78fae272007-03-25 11:35:08 -04002459
2460 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002461 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002462 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2463 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04002464 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05002465 if (ret > 0)
2466 mark_free = 1;
2467 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04002468 }
2469
Josef Bacik58176a92007-08-29 15:47:34 -04002470 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002471 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002472 super_used = btrfs_super_bytes_used(&info->super_copy);
2473 btrfs_set_super_bytes_used(&info->super_copy,
2474 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002475 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04002476
2477 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002478 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002479 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04002480 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05002481 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2482 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04002483 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04002484 btrfs_release_path(extent_root, path);
Chris Masondb945352007-10-15 16:15:53 -04002485 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002486 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04002487 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01002488
2489#ifdef BIO_RW_DISCARD
2490 /* Tell the block device(s) that the sectors can be discarded */
2491 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2492 bytenr, &map_length, &multi, 0);
2493 if (!ret) {
2494 struct btrfs_bio_stripe *stripe = multi->stripes;
2495 int i;
2496
2497 if (map_length > num_bytes)
2498 map_length = num_bytes;
2499
2500 for (i = 0; i < multi->num_stripes; i++, stripe++) {
2501 blkdev_issue_discard(stripe->dev->bdev,
2502 stripe->physical >> 9,
2503 map_length >> 9);
2504 }
2505 kfree(multi);
2506 }
2507#endif
Chris Masona28ec192007-03-06 20:08:01 -05002508 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002509 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002510 finish_current_insert(trans, extent_root, 0);
Chris Masona28ec192007-03-06 20:08:01 -05002511 return ret;
2512}
2513
2514/*
Chris Masonfec577f2007-02-26 10:40:21 -05002515 * find all the blocks marked as pending in the radix tree and remove
2516 * them from the extent map
2517 */
Chris Masone089f052007-03-16 16:20:31 -04002518static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -04002519 btrfs_root *extent_root, int all)
Chris Masonfec577f2007-02-26 10:40:21 -05002520{
2521 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002522 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002523 u64 start;
2524 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002525 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002526 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002527 int nr = 0, skipped = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002528 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002529 struct extent_io_tree *extent_ins;
2530 struct pending_extent_op *extent_op;
Josef Bacik25179202008-10-29 14:49:05 -04002531 struct btrfs_fs_info *info = extent_root->fs_info;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002532 struct list_head delete_list;
Chris Mason8ef97622007-03-26 10:15:30 -04002533
Josef Bacikf3465ca2008-11-12 14:19:50 -05002534 INIT_LIST_HEAD(&delete_list);
Zheng Yan31840ae2008-09-23 13:14:14 -04002535 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc162007-10-15 16:15:26 -04002536 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002537
Josef Bacikf3465ca2008-11-12 14:19:50 -05002538again:
2539 mutex_lock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002540 while(1) {
Josef Bacik25179202008-10-29 14:49:05 -04002541 ret = find_first_extent_bit(pending_del, search, &start, &end,
2542 EXTENT_WRITEBACK);
2543 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002544 if (all && skipped && !nr) {
Josef Bacik25179202008-10-29 14:49:05 -04002545 search = 0;
2546 continue;
2547 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002548 mutex_unlock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002549 break;
Josef Bacik25179202008-10-29 14:49:05 -04002550 }
2551
2552 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2553 if (!ret) {
2554 search = end+1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002555 skipped = 1;
2556
2557 if (need_resched()) {
2558 mutex_unlock(&info->extent_ins_mutex);
2559 cond_resched();
2560 mutex_lock(&info->extent_ins_mutex);
2561 }
2562
Josef Bacik25179202008-10-29 14:49:05 -04002563 continue;
2564 }
2565 BUG_ON(ret < 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002566
2567 ret = get_state_private(pending_del, start, &priv);
2568 BUG_ON(ret);
2569 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2570
Josef Bacik25179202008-10-29 14:49:05 -04002571 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
Chris Mason1a5bc162007-10-15 16:15:26 -04002572 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002573 if (!test_range_bit(extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04002574 EXTENT_WRITEBACK, 0)) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002575 list_add_tail(&extent_op->list, &delete_list);
2576 nr++;
Chris Masonc286ac42008-07-22 23:06:41 -04002577 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002578 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002579
2580 ret = get_state_private(&info->extent_ins, start,
2581 &priv);
Zheng Yan31840ae2008-09-23 13:14:14 -04002582 BUG_ON(ret);
2583 extent_op = (struct pending_extent_op *)
Josef Bacik25179202008-10-29 14:49:05 -04002584 (unsigned long)priv;
Zheng Yan31840ae2008-09-23 13:14:14 -04002585
Josef Bacik25179202008-10-29 14:49:05 -04002586 clear_extent_bits(&info->extent_ins, start, end,
2587 EXTENT_WRITEBACK, GFP_NOFS);
2588
Josef Bacikf3465ca2008-11-12 14:19:50 -05002589 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2590 list_add_tail(&extent_op->list, &delete_list);
2591 search = end + 1;
2592 nr++;
2593 continue;
2594 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002595
Josef Bacik25179202008-10-29 14:49:05 -04002596 mutex_lock(&extent_root->fs_info->pinned_mutex);
2597 ret = pin_down_bytes(trans, extent_root, start,
2598 end + 1 - start, 0);
2599 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2600
Zheng Yan31840ae2008-09-23 13:14:14 -04002601 ret = update_block_group(trans, extent_root, start,
Josef Bacik25179202008-10-29 14:49:05 -04002602 end + 1 - start, 0, ret > 0);
2603
Josef Bacikf3465ca2008-11-12 14:19:50 -05002604 unlock_extent(extent_ins, start, end, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002605 BUG_ON(ret);
2606 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002607 }
Chris Mason1a5bc162007-10-15 16:15:26 -04002608 if (ret)
2609 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04002610
Josef Bacikf3465ca2008-11-12 14:19:50 -05002611 search = end + 1;
2612
2613 if (need_resched()) {
2614 mutex_unlock(&info->extent_ins_mutex);
2615 cond_resched();
2616 mutex_lock(&info->extent_ins_mutex);
2617 }
Chris Masonfec577f2007-02-26 10:40:21 -05002618 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002619
2620 if (nr) {
2621 ret = free_extents(trans, extent_root, &delete_list);
2622 BUG_ON(ret);
2623 }
2624
2625 if (all && skipped) {
2626 INIT_LIST_HEAD(&delete_list);
2627 search = 0;
2628 nr = 0;
2629 goto again;
2630 }
2631
Chris Masone20d96d2007-03-22 12:13:20 -04002632 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002633}
2634
2635/*
2636 * remove an extent from the root, returns 0 on success
2637 */
Chris Mason925baed2008-06-25 16:01:30 -04002638static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002639 struct btrfs_root *root,
2640 u64 bytenr, u64 num_bytes, u64 parent,
2641 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002642 u64 owner_objectid, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002643{
Chris Mason9f5fae22007-03-20 14:38:32 -04002644 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002645 int pending_ret;
2646 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002647
Chris Masondb945352007-10-15 16:15:53 -04002648 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002649 if (root == extent_root) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002650 struct pending_extent_op *extent_op = NULL;
2651
2652 mutex_lock(&root->fs_info->extent_ins_mutex);
2653 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2654 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2655 u64 priv;
2656 ret = get_state_private(&root->fs_info->extent_ins,
2657 bytenr, &priv);
2658 BUG_ON(ret);
2659 extent_op = (struct pending_extent_op *)
2660 (unsigned long)priv;
2661
2662 extent_op->del = 1;
2663 if (extent_op->type == PENDING_EXTENT_INSERT) {
2664 mutex_unlock(&root->fs_info->extent_ins_mutex);
2665 return 0;
2666 }
2667 }
2668
2669 if (extent_op) {
2670 ref_generation = extent_op->orig_generation;
2671 parent = extent_op->orig_parent;
2672 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002673
2674 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2675 BUG_ON(!extent_op);
2676
2677 extent_op->type = PENDING_EXTENT_DELETE;
2678 extent_op->bytenr = bytenr;
2679 extent_op->num_bytes = num_bytes;
2680 extent_op->parent = parent;
2681 extent_op->orig_parent = parent;
2682 extent_op->generation = ref_generation;
2683 extent_op->orig_generation = ref_generation;
2684 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002685 INIT_LIST_HEAD(&extent_op->list);
2686 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002687
2688 set_extent_bits(&root->fs_info->pending_del,
2689 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04002690 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002691 set_state_private(&root->fs_info->pending_del,
2692 bytenr, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002693 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002694 return 0;
2695 }
Chris Mason4bef0842008-09-08 11:18:08 -04002696 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002697 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2698 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002699 struct btrfs_block_group_cache *cache;
2700
Chris Masond00aff02008-09-11 15:54:42 -04002701 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002702 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2703 BUG_ON(!cache);
2704 btrfs_add_free_space(cache, bytenr, num_bytes);
Zheng Yane8569812008-09-26 10:05:48 -04002705 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002706 return 0;
2707 }
Chris Mason4bef0842008-09-08 11:18:08 -04002708 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002709 }
Chris Mason4bef0842008-09-08 11:18:08 -04002710
2711 /* if data pin when any transaction has committed this */
2712 if (ref_generation != trans->transid)
2713 pin = 1;
2714
Zheng Yan31840ae2008-09-23 13:14:14 -04002715 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002716 root_objectid, ref_generation,
2717 owner_objectid, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002718
Chris Mason87ef2bb2008-10-30 11:23:27 -04002719 finish_current_insert(trans, root->fs_info->extent_root, 0);
2720 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002721 return ret ? ret : pending_ret;
2722}
2723
Chris Mason925baed2008-06-25 16:01:30 -04002724int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002725 struct btrfs_root *root,
2726 u64 bytenr, u64 num_bytes, u64 parent,
2727 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002728 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002729{
2730 int ret;
2731
Zheng Yan31840ae2008-09-23 13:14:14 -04002732 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002733 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002734 owner_objectid, pin);
Chris Mason925baed2008-06-25 16:01:30 -04002735 return ret;
2736}
2737
Chris Mason87ee04e2007-11-30 11:30:34 -05002738static u64 stripe_align(struct btrfs_root *root, u64 val)
2739{
2740 u64 mask = ((u64)root->stripesize - 1);
2741 u64 ret = (val + mask) & ~mask;
2742 return ret;
2743}
2744
Chris Masonfec577f2007-02-26 10:40:21 -05002745/*
2746 * walks the btree of allocated extents and find a hole of a given size.
2747 * The key ins is changed to record the hole:
2748 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002749 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002750 * ins->offset == number of blocks
2751 * Any available blocks before search_start are skipped.
2752 */
Chris Mason98ed5172008-01-03 10:01:48 -05002753static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2754 struct btrfs_root *orig_root,
2755 u64 num_bytes, u64 empty_size,
2756 u64 search_start, u64 search_end,
2757 u64 hint_byte, struct btrfs_key *ins,
2758 u64 exclude_start, u64 exclude_nr,
2759 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002760{
Josef Bacik80eb2342008-10-29 14:49:05 -04002761 int ret = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -04002762 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002763 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002764 u64 *last_ptr = NULL;
Chris Mason43662112008-11-07 09:06:11 -05002765 u64 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002766 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002767 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002768 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002769 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002770 struct list_head *head = NULL, *cur = NULL;
2771 int loop = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002772 int extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002773 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002774
Chris Masondb945352007-10-15 16:15:53 -04002775 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002776 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002777 ins->objectid = 0;
2778 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002779
Chris Mason0ef3e662008-05-24 14:04:53 -04002780 if (orig_root->ref_cows || empty_size)
2781 allowed_chunk_alloc = 1;
2782
Chris Mason239b14b2008-03-24 15:02:07 -04002783 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2784 last_ptr = &root->fs_info->last_alloc;
Chris Mason43662112008-11-07 09:06:11 -05002785 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002786 }
2787
Josef Bacik0f9dd462008-09-23 13:14:11 -04002788 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002789 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002790
Chris Mason239b14b2008-03-24 15:02:07 -04002791 if (last_ptr) {
Chris Mason43662112008-11-07 09:06:11 -05002792 if (*last_ptr) {
Chris Mason239b14b2008-03-24 15:02:07 -04002793 hint_byte = *last_ptr;
Chris Mason43662112008-11-07 09:06:11 -05002794 last_wanted = *last_ptr;
2795 } else
Chris Mason239b14b2008-03-24 15:02:07 -04002796 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002797 } else {
2798 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002799 }
Chris Masona061fc82008-05-07 11:43:44 -04002800 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002801 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04002802
Chris Mason42e70e72008-11-07 18:17:11 -05002803 if (last_wanted && search_start != last_wanted) {
Chris Mason43662112008-11-07 09:06:11 -05002804 last_wanted = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002805 empty_size += empty_cluster;
2806 }
Chris Mason43662112008-11-07 09:06:11 -05002807
Chris Mason42e70e72008-11-07 18:17:11 -05002808 total_needed += empty_size;
Josef Bacik80eb2342008-10-29 14:49:05 -04002809 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
Yan Zhengd899e052008-10-30 14:25:28 -04002810 if (!block_group)
2811 block_group = btrfs_lookup_first_block_group(root->fs_info,
2812 search_start);
Josef Bacik80eb2342008-10-29 14:49:05 -04002813 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002814
Josef Bacik80eb2342008-10-29 14:49:05 -04002815 down_read(&space_info->groups_sem);
2816 while (1) {
2817 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002818 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002819 * the only way this happens if our hint points to a block
2820 * group thats not of the proper type, while looping this
2821 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002822 */
Chris Mason8a1413a2008-11-10 16:13:54 -05002823 if (empty_size)
2824 extra_loop = 1;
2825
Chris Mason42e70e72008-11-07 18:17:11 -05002826 if (!block_group)
2827 goto new_group_no_lock;
2828
Josef Bacik25179202008-10-29 14:49:05 -04002829 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002830 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002831 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002832
Josef Bacik80eb2342008-10-29 14:49:05 -04002833 ret = cache_block_group(root, block_group);
Josef Bacik25179202008-10-29 14:49:05 -04002834 if (ret) {
2835 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002836 break;
Josef Bacik25179202008-10-29 14:49:05 -04002837 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002838
Josef Bacik80eb2342008-10-29 14:49:05 -04002839 if (block_group->ro)
2840 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002841
Josef Bacik80eb2342008-10-29 14:49:05 -04002842 free_space = btrfs_find_free_space(block_group, search_start,
2843 total_needed);
2844 if (free_space) {
2845 u64 start = block_group->key.objectid;
2846 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002847 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002848
Josef Bacik80eb2342008-10-29 14:49:05 -04002849 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002850
Josef Bacik80eb2342008-10-29 14:49:05 -04002851 /* move on to the next group */
2852 if (search_start + num_bytes >= search_end)
2853 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002854
Josef Bacik80eb2342008-10-29 14:49:05 -04002855 /* move on to the next group */
2856 if (search_start + num_bytes > end)
2857 goto new_group;
2858
Chris Mason43662112008-11-07 09:06:11 -05002859 if (last_wanted && search_start != last_wanted) {
Chris Mason3b7885b2008-11-06 21:48:27 -05002860 total_needed += empty_cluster;
Chris Mason8a1413a2008-11-10 16:13:54 -05002861 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002862 last_wanted = 0;
Chris Mason3b7885b2008-11-06 21:48:27 -05002863 /*
2864 * if search_start is still in this block group
2865 * then we just re-search this block group
2866 */
2867 if (search_start >= start &&
2868 search_start < end) {
2869 mutex_unlock(&block_group->alloc_mutex);
2870 continue;
2871 }
2872
2873 /* else we go to the next block group */
2874 goto new_group;
2875 }
2876
Josef Bacik80eb2342008-10-29 14:49:05 -04002877 if (exclude_nr > 0 &&
2878 (search_start + num_bytes > exclude_start &&
2879 search_start < exclude_start + exclude_nr)) {
2880 search_start = exclude_start + exclude_nr;
2881 /*
2882 * if search_start is still in this block group
2883 * then we just re-search this block group
2884 */
2885 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002886 search_start < end) {
2887 mutex_unlock(&block_group->alloc_mutex);
Chris Mason43662112008-11-07 09:06:11 -05002888 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002889 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002890 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002891
2892 /* else we go to the next block group */
2893 goto new_group;
2894 }
2895
2896 ins->objectid = search_start;
2897 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002898
2899 btrfs_remove_free_space_lock(block_group, search_start,
2900 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002901 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002902 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002903 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002904 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002905new_group:
Chris Mason42e70e72008-11-07 18:17:11 -05002906 mutex_unlock(&block_group->alloc_mutex);
2907new_group_no_lock:
Chris Masonf5a31e12008-11-10 11:47:09 -05002908 /* don't try to compare new allocations against the
2909 * last allocation any more
2910 */
Chris Mason43662112008-11-07 09:06:11 -05002911 last_wanted = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002912
Josef Bacik80eb2342008-10-29 14:49:05 -04002913 /*
2914 * Here's how this works.
2915 * loop == 0: we were searching a block group via a hint
2916 * and didn't find anything, so we start at
2917 * the head of the block groups and keep searching
2918 * loop == 1: we're searching through all of the block groups
2919 * if we hit the head again we have searched
2920 * all of the block groups for this space and we
2921 * need to try and allocate, if we cant error out.
2922 * loop == 2: we allocated more space and are looping through
2923 * all of the block groups again.
2924 */
2925 if (loop == 0) {
2926 head = &space_info->block_groups;
2927 cur = head->next;
Josef Bacik80eb2342008-10-29 14:49:05 -04002928 loop++;
2929 } else if (loop == 1 && cur == head) {
Chris Masonf5a31e12008-11-10 11:47:09 -05002930 int keep_going;
Chris Mason42e70e72008-11-07 18:17:11 -05002931
Chris Masonf5a31e12008-11-10 11:47:09 -05002932 /* at this point we give up on the empty_size
2933 * allocations and just try to allocate the min
2934 * space.
2935 *
2936 * The extra_loop field was set if an empty_size
2937 * allocation was attempted above, and if this
2938 * is try we need to try the loop again without
2939 * the additional empty_size.
2940 */
Chris Mason5b7c3fc2008-11-10 07:26:33 -05002941 total_needed -= empty_size;
2942 empty_size = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002943 keep_going = extra_loop;
2944 loop++;
Chris Mason42e70e72008-11-07 18:17:11 -05002945
Josef Bacik80eb2342008-10-29 14:49:05 -04002946 if (allowed_chunk_alloc && !chunk_alloc_done) {
2947 up_read(&space_info->groups_sem);
2948 ret = do_chunk_alloc(trans, root, num_bytes +
2949 2 * 1024 * 1024, data, 1);
Josef Bacik80eb2342008-10-29 14:49:05 -04002950 down_read(&space_info->groups_sem);
Chris Mason2ed6d662008-11-13 09:59:33 -05002951 if (ret < 0)
2952 goto loop_check;
Josef Bacik80eb2342008-10-29 14:49:05 -04002953 head = &space_info->block_groups;
Chris Masonf5a31e12008-11-10 11:47:09 -05002954 /*
2955 * we've allocated a new chunk, keep
2956 * trying
2957 */
2958 keep_going = 1;
Josef Bacik80eb2342008-10-29 14:49:05 -04002959 chunk_alloc_done = 1;
2960 } else if (!allowed_chunk_alloc) {
2961 space_info->force_alloc = 1;
Chris Masonf5a31e12008-11-10 11:47:09 -05002962 }
Chris Mason2ed6d662008-11-13 09:59:33 -05002963loop_check:
Chris Masonf5a31e12008-11-10 11:47:09 -05002964 if (keep_going) {
2965 cur = head->next;
2966 extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002967 } else {
2968 break;
2969 }
2970 } else if (cur == head) {
2971 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002972 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002973
2974 block_group = list_entry(cur, struct btrfs_block_group_cache,
2975 list);
2976 search_start = block_group->key.objectid;
2977 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002978 }
Chris Mason0b86a832008-03-24 15:01:56 -04002979
Josef Bacik80eb2342008-10-29 14:49:05 -04002980 /* we found what we needed */
2981 if (ins->objectid) {
2982 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2983 trans->block_group = block_group;
2984
2985 if (last_ptr)
2986 *last_ptr = ins->objectid + ins->offset;
2987 ret = 0;
2988 } else if (!ret) {
Josef Bacik4ce4cb52008-11-17 21:12:00 -05002989 printk(KERN_ERR "we were searching for %Lu bytes, num_bytes %Lu,"
2990 " loop %d, allowed_alloc %d\n", total_needed, num_bytes,
2991 loop, allowed_chunk_alloc);
Josef Bacik80eb2342008-10-29 14:49:05 -04002992 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04002993 }
Chris Mason0b86a832008-03-24 15:01:56 -04002994
Josef Bacik80eb2342008-10-29 14:49:05 -04002995 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05002996 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002997}
Chris Masonec44a352008-04-28 15:29:52 -04002998
Josef Bacik0f9dd462008-09-23 13:14:11 -04002999static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3000{
3001 struct btrfs_block_group_cache *cache;
3002 struct list_head *l;
3003
3004 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
Zheng Yane8569812008-09-26 10:05:48 -04003005 info->total_bytes - info->bytes_used - info->bytes_pinned -
3006 info->bytes_reserved, (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04003007
Josef Bacik80eb2342008-10-29 14:49:05 -04003008 down_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003009 list_for_each(l, &info->block_groups) {
3010 cache = list_entry(l, struct btrfs_block_group_cache, list);
3011 spin_lock(&cache->lock);
3012 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
Zheng Yane8569812008-09-26 10:05:48 -04003013 "%Lu pinned %Lu reserved\n",
Josef Bacik0f9dd462008-09-23 13:14:11 -04003014 cache->key.objectid, cache->key.offset,
Zheng Yane8569812008-09-26 10:05:48 -04003015 btrfs_block_group_used(&cache->item),
3016 cache->pinned, cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003017 btrfs_dump_free_space(cache, bytes);
3018 spin_unlock(&cache->lock);
3019 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003020 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003021}
Zheng Yane8569812008-09-26 10:05:48 -04003022
Chris Masone6dcd2d2008-07-17 12:53:50 -04003023static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3024 struct btrfs_root *root,
3025 u64 num_bytes, u64 min_alloc_size,
3026 u64 empty_size, u64 hint_byte,
3027 u64 search_end, struct btrfs_key *ins,
3028 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05003029{
3030 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04003031 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003032 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04003033 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003034
Chris Mason6324fbf2008-03-24 15:01:59 -04003035 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04003036 alloc_profile = info->avail_data_alloc_bits &
3037 info->data_alloc_profile;
3038 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003039 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04003040 alloc_profile = info->avail_system_alloc_bits &
3041 info->system_alloc_profile;
3042 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003043 } else {
Chris Mason8790d502008-04-03 16:29:03 -04003044 alloc_profile = info->avail_metadata_alloc_bits &
3045 info->metadata_alloc_profile;
3046 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003047 }
Chris Mason98d20f62008-04-14 09:46:10 -04003048again:
Yan Zheng2b820322008-11-17 21:11:30 -05003049 data = btrfs_reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04003050 /*
3051 * the only place that sets empty_size is btrfs_realloc_node, which
3052 * is not called recursively on allocations
3053 */
3054 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04003055 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04003056 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003057 2 * 1024 * 1024,
3058 BTRFS_BLOCK_GROUP_METADATA |
3059 (info->metadata_alloc_profile &
3060 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003061 }
3062 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003063 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003064 }
Chris Mason0b86a832008-03-24 15:01:56 -04003065
Chris Masondb945352007-10-15 16:15:53 -04003066 WARN_ON(num_bytes < root->sectorsize);
3067 ret = find_free_extent(trans, root, num_bytes, empty_size,
3068 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04003069 trans->alloc_exclude_start,
3070 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04003071
Chris Mason98d20f62008-04-14 09:46:10 -04003072 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3073 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003074 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003075 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04003076 do_chunk_alloc(trans, root->fs_info->extent_root,
3077 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003078 goto again;
3079 }
Chris Masonec44a352008-04-28 15:29:52 -04003080 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003081 struct btrfs_space_info *sinfo;
3082
3083 sinfo = __find_space_info(root->fs_info, data);
3084 printk("allocation failed flags %Lu, wanted %Lu\n",
3085 data, num_bytes);
3086 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04003087 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04003088 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003089
3090 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003091}
3092
Chris Mason65b51a02008-08-01 15:11:20 -04003093int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3094{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003095 struct btrfs_block_group_cache *cache;
3096
Josef Bacik0f9dd462008-09-23 13:14:11 -04003097 cache = btrfs_lookup_block_group(root->fs_info, start);
3098 if (!cache) {
3099 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003100 return -ENOSPC;
3101 }
3102 btrfs_add_free_space(cache, start, len);
Zheng Yan1a40e232008-09-26 10:09:34 -04003103 update_reserved_extents(root, start, len, 0);
Chris Mason65b51a02008-08-01 15:11:20 -04003104 return 0;
3105}
3106
Chris Masone6dcd2d2008-07-17 12:53:50 -04003107int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3108 struct btrfs_root *root,
3109 u64 num_bytes, u64 min_alloc_size,
3110 u64 empty_size, u64 hint_byte,
3111 u64 search_end, struct btrfs_key *ins,
3112 u64 data)
3113{
3114 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003115 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3116 empty_size, hint_byte, search_end, ins,
3117 data);
Zheng Yane8569812008-09-26 10:05:48 -04003118 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003119 return ret;
3120}
3121
3122static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003123 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003124 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003125 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003126{
3127 int ret;
3128 int pending_ret;
3129 u64 super_used;
3130 u64 root_used;
3131 u64 num_bytes = ins->offset;
3132 u32 sizes[2];
3133 struct btrfs_fs_info *info = root->fs_info;
3134 struct btrfs_root *extent_root = info->extent_root;
3135 struct btrfs_extent_item *extent_item;
3136 struct btrfs_extent_ref *ref;
3137 struct btrfs_path *path;
3138 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04003139
Zheng Yan31840ae2008-09-23 13:14:14 -04003140 if (parent == 0)
3141 parent = ins->objectid;
3142
Josef Bacik58176a92007-08-29 15:47:34 -04003143 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04003144 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04003145 super_used = btrfs_super_bytes_used(&info->super_copy);
3146 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04003147 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04003148
Josef Bacik58176a92007-08-29 15:47:34 -04003149 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04003150 root_used = btrfs_root_used(&root->root_item);
3151 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04003152
Chris Mason26b80032007-08-08 20:17:12 -04003153 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003154 struct pending_extent_op *extent_op;
3155
3156 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3157 BUG_ON(!extent_op);
3158
3159 extent_op->type = PENDING_EXTENT_INSERT;
3160 extent_op->bytenr = ins->objectid;
3161 extent_op->num_bytes = ins->offset;
3162 extent_op->parent = parent;
3163 extent_op->orig_parent = 0;
3164 extent_op->generation = ref_generation;
3165 extent_op->orig_generation = 0;
3166 extent_op->level = (int)owner;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003167 INIT_LIST_HEAD(&extent_op->list);
3168 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003169
Josef Bacik25179202008-10-29 14:49:05 -04003170 mutex_lock(&root->fs_info->extent_ins_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04003171 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3172 ins->objectid + ins->offset - 1,
Josef Bacik25179202008-10-29 14:49:05 -04003173 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04003174 set_state_private(&root->fs_info->extent_ins,
3175 ins->objectid, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04003176 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04003177 goto update_block;
3178 }
3179
Chris Mason47e4bb92008-02-01 14:51:59 -05003180 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05003181 keys[1].objectid = ins->objectid;
3182 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04003183 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05003184 sizes[0] = sizeof(*extent_item);
3185 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05003186
3187 path = btrfs_alloc_path();
3188 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05003189
3190 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3191 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04003192 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003193
Chris Mason47e4bb92008-02-01 14:51:59 -05003194 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3195 struct btrfs_extent_item);
3196 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3197 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3198 struct btrfs_extent_ref);
3199
3200 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3201 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3202 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Zheng Yan31840ae2008-09-23 13:14:14 -04003203 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05003204
3205 btrfs_mark_buffer_dirty(path->nodes[0]);
3206
3207 trans->alloc_exclude_start = 0;
3208 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05003209 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04003210 finish_current_insert(trans, extent_root, 0);
3211 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -04003212
Chris Mason925baed2008-06-25 16:01:30 -04003213 if (ret)
3214 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003215 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04003216 ret = pending_ret;
3217 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003218 }
Chris Mason26b80032007-08-08 20:17:12 -04003219
3220update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04003221 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05003222 if (ret) {
3223 printk("update block group failed for %Lu %Lu\n",
3224 ins->objectid, ins->offset);
3225 BUG();
3226 }
Chris Mason925baed2008-06-25 16:01:30 -04003227out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04003228 return ret;
3229}
3230
3231int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003232 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003233 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003234 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003235{
3236 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04003237
3238 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3239 return 0;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003240 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3241 ref_generation, owner, ins);
Zheng Yane8569812008-09-26 10:05:48 -04003242 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003243 return ret;
3244}
Chris Masone02119d2008-09-05 16:13:11 -04003245
3246/*
3247 * this is used by the tree logging recovery code. It records that
3248 * an extent has been allocated and makes sure to clear the free
3249 * space cache bits as well
3250 */
3251int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003252 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04003253 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003254 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04003255{
3256 int ret;
3257 struct btrfs_block_group_cache *block_group;
3258
Chris Masone02119d2008-09-05 16:13:11 -04003259 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik25179202008-10-29 14:49:05 -04003260 mutex_lock(&block_group->alloc_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003261 cache_block_group(root, block_group);
3262
Josef Bacik25179202008-10-29 14:49:05 -04003263 ret = btrfs_remove_free_space_lock(block_group, ins->objectid,
3264 ins->offset);
3265 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003266 BUG_ON(ret);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003267 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3268 ref_generation, owner, ins);
Chris Masone02119d2008-09-05 16:13:11 -04003269 return ret;
3270}
3271
Chris Masone6dcd2d2008-07-17 12:53:50 -04003272/*
3273 * finds a free extent and does all the dirty work required for allocation
3274 * returns the key for the extent through ins, and a tree buffer for
3275 * the first block of the extent through buf.
3276 *
3277 * returns 0 if everything worked, non-zero otherwise.
3278 */
3279int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3280 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003281 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003282 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003283 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003284 u64 search_end, struct btrfs_key *ins, u64 data)
3285{
3286 int ret;
3287
Chris Masone6dcd2d2008-07-17 12:53:50 -04003288 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3289 min_alloc_size, empty_size, hint_byte,
3290 search_end, ins, data);
3291 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04003292 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003293 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3294 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003295 owner_objectid, ins);
Chris Masond00aff02008-09-11 15:54:42 -04003296 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003297
Zheng Yane8569812008-09-26 10:05:48 -04003298 } else {
3299 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04003300 }
Chris Mason925baed2008-06-25 16:01:30 -04003301 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003302}
Chris Mason65b51a02008-08-01 15:11:20 -04003303
3304struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3305 struct btrfs_root *root,
3306 u64 bytenr, u32 blocksize)
3307{
3308 struct extent_buffer *buf;
3309
3310 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3311 if (!buf)
3312 return ERR_PTR(-ENOMEM);
3313 btrfs_set_header_generation(buf, trans->transid);
3314 btrfs_tree_lock(buf);
3315 clean_tree_block(trans, root, buf);
3316 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04003317 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3318 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04003319 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04003320 } else {
3321 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3322 buf->start + buf->len - 1, GFP_NOFS);
3323 }
Chris Mason65b51a02008-08-01 15:11:20 -04003324 trans->blocks_used++;
3325 return buf;
3326}
3327
Chris Masonfec577f2007-02-26 10:40:21 -05003328/*
3329 * helper function to allocate a block for a given tree
3330 * returns the tree buffer or NULL.
3331 */
Chris Mason5f39d392007-10-15 16:14:19 -04003332struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04003333 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003334 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05003335 u64 root_objectid,
3336 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05003337 int level,
3338 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04003339 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05003340{
Chris Masone2fa7222007-03-12 16:22:34 -04003341 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05003342 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003343 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05003344
Zheng Yan31840ae2008-09-23 13:14:14 -04003345 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003346 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04003347 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05003348 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04003349 BUG_ON(ret > 0);
3350 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05003351 }
Chris Mason55c69072008-01-09 15:55:33 -05003352
Chris Mason65b51a02008-08-01 15:11:20 -04003353 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05003354 return buf;
3355}
Chris Masona28ec192007-03-06 20:08:01 -05003356
Chris Masone02119d2008-09-05 16:13:11 -04003357int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3358 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04003359{
Chris Mason7bb86312007-12-11 09:25:06 -05003360 u64 leaf_owner;
3361 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04003362 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04003363 struct btrfs_file_extent_item *fi;
3364 int i;
3365 int nritems;
3366 int ret;
3367
Chris Mason5f39d392007-10-15 16:14:19 -04003368 BUG_ON(!btrfs_is_leaf(leaf));
3369 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05003370 leaf_owner = btrfs_header_owner(leaf);
3371 leaf_generation = btrfs_header_generation(leaf);
3372
Chris Mason6407bf62007-03-27 06:33:00 -04003373 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003374 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04003375 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04003376
3377 btrfs_item_key_to_cpu(leaf, &key, i);
3378 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04003379 continue;
3380 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003381 if (btrfs_file_extent_type(leaf, fi) ==
3382 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04003383 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04003384 /*
3385 * FIXME make sure to insert a trans record that
3386 * repeats the snapshot del on crash
3387 */
Chris Masondb945352007-10-15 16:15:53 -04003388 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3389 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04003390 continue;
Chris Mason4a096752008-07-21 10:29:44 -04003391
Chris Mason925baed2008-06-25 16:01:30 -04003392 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003393 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04003394 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003395 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003396 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04003397
3398 atomic_inc(&root->fs_info->throttle_gen);
3399 wake_up(&root->fs_info->transaction_throttle);
3400 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04003401 }
3402 return 0;
3403}
3404
Chris Masone02119d2008-09-05 16:13:11 -04003405static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3406 struct btrfs_root *root,
3407 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04003408{
3409 int i;
3410 int ret;
3411 struct btrfs_extent_info *info = ref->extents;
3412
Yan Zheng31153d82008-07-28 15:32:19 -04003413 for (i = 0; i < ref->nritems; i++) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003414 ret = __btrfs_free_extent(trans, root, info->bytenr,
3415 info->num_bytes, ref->bytenr,
3416 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003417 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04003418
3419 atomic_inc(&root->fs_info->throttle_gen);
3420 wake_up(&root->fs_info->transaction_throttle);
3421 cond_resched();
3422
Yan Zheng31153d82008-07-28 15:32:19 -04003423 BUG_ON(ret);
3424 info++;
3425 }
Yan Zheng31153d82008-07-28 15:32:19 -04003426
3427 return 0;
3428}
3429
Chris Mason333db942008-06-25 16:01:30 -04003430int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
3431 u32 *refs)
3432{
Chris Mason017e5362008-07-28 15:32:51 -04003433 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04003434
Zheng Yan31840ae2008-09-23 13:14:14 -04003435 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04003436 BUG_ON(ret);
3437
3438#if 0 // some debugging code in case we see problems here
3439 /* if the refs count is one, it won't get increased again. But
3440 * if the ref count is > 1, someone may be decreasing it at
3441 * the same time we are.
3442 */
3443 if (*refs != 1) {
3444 struct extent_buffer *eb = NULL;
3445 eb = btrfs_find_create_tree_block(root, start, len);
3446 if (eb)
3447 btrfs_tree_lock(eb);
3448
3449 mutex_lock(&root->fs_info->alloc_mutex);
3450 ret = lookup_extent_ref(NULL, root, start, len, refs);
3451 BUG_ON(ret);
3452 mutex_unlock(&root->fs_info->alloc_mutex);
3453
3454 if (eb) {
3455 btrfs_tree_unlock(eb);
3456 free_extent_buffer(eb);
3457 }
3458 if (*refs == 1) {
3459 printk("block %llu went down to one during drop_snap\n",
3460 (unsigned long long)start);
3461 }
3462
3463 }
3464#endif
3465
Chris Masone7a84562008-06-25 16:01:31 -04003466 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04003467 return ret;
Chris Mason333db942008-06-25 16:01:30 -04003468}
3469
3470/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003471 * helper function for drop_snapshot, this walks down the tree dropping ref
3472 * counts as it goes.
3473 */
Chris Mason98ed5172008-01-03 10:01:48 -05003474static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
3475 struct btrfs_root *root,
3476 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003477{
Chris Mason7bb86312007-12-11 09:25:06 -05003478 u64 root_owner;
3479 u64 root_gen;
3480 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04003481 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003482 struct extent_buffer *next;
3483 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05003484 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04003485 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04003486 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05003487 int ret;
3488 u32 refs;
3489
Chris Mason5caf2a02007-04-02 11:20:42 -04003490 WARN_ON(*level < 0);
3491 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04003492 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04003493 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05003494 BUG_ON(ret);
3495 if (refs > 1)
3496 goto out;
Chris Masone0115992007-06-19 16:23:05 -04003497
Chris Mason9aca1d52007-03-13 11:09:37 -04003498 /*
3499 * walk down to the last node level and free all the leaves
3500 */
Chris Mason6407bf62007-03-27 06:33:00 -04003501 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003502 WARN_ON(*level < 0);
3503 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05003504 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04003505
Chris Mason5f39d392007-10-15 16:14:19 -04003506 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04003507 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04003508
Chris Mason7518a232007-03-12 12:01:18 -04003509 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04003510 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05003511 break;
Chris Mason6407bf62007-03-27 06:33:00 -04003512 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003513 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04003514 BUG_ON(ret);
3515 break;
3516 }
Chris Masondb945352007-10-15 16:15:53 -04003517 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04003518 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04003519 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04003520
Chris Mason333db942008-06-25 16:01:30 -04003521 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04003522 BUG_ON(ret);
3523 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05003524 parent = path->nodes[*level];
3525 root_owner = btrfs_header_owner(parent);
3526 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05003527 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04003528
Chris Mason925baed2008-06-25 16:01:30 -04003529 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003530 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003531 root_owner, root_gen,
3532 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05003533 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04003534
3535 atomic_inc(&root->fs_info->throttle_gen);
3536 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04003537 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04003538
Chris Mason20524f02007-03-10 06:35:47 -05003539 continue;
3540 }
Chris Masonf87f0572008-08-01 11:27:23 -04003541 /*
3542 * at this point, we have a single ref, and since the
3543 * only place referencing this extent is a dead root
3544 * the reference count should never go higher.
3545 * So, we don't need to check it again
3546 */
Yan Zheng31153d82008-07-28 15:32:19 -04003547 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04003548 ref = btrfs_lookup_leaf_ref(root, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04003549 if (ref && ref->generation != ptr_gen) {
3550 btrfs_free_leaf_ref(root, ref);
3551 ref = NULL;
3552 }
Yan Zheng31153d82008-07-28 15:32:19 -04003553 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04003554 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003555 BUG_ON(ret);
3556 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04003557 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003558 *level = 0;
3559 break;
3560 }
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003561 if (printk_ratelimit()) {
Chris Mason37d1aee2008-07-31 10:48:37 -04003562 printk("leaf ref miss for bytenr %llu\n",
3563 (unsigned long long)bytenr);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003564 }
Yan Zheng31153d82008-07-28 15:32:19 -04003565 }
Chris Masondb945352007-10-15 16:15:53 -04003566 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04003567 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003568 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04003569
Chris Masonca7a79a2008-05-12 12:59:19 -04003570 next = read_tree_block(root, bytenr, blocksize,
3571 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04003572 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04003573#if 0
3574 /*
3575 * this is a debugging check and can go away
3576 * the ref should never go all the way down to 1
3577 * at this point
3578 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003579 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
3580 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04003581 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003582 WARN_ON(refs != 1);
3583#endif
Chris Masone9d0b132007-08-10 14:06:19 -04003584 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003585 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04003586 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04003587 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05003588 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04003589 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05003590 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04003591 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003592 }
3593out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003594 WARN_ON(*level < 0);
3595 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05003596
3597 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003598 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003599 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003600 } else {
3601 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003602 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003603 }
3604
Yan Zheng31153d82008-07-28 15:32:19 -04003605 blocksize = btrfs_level_size(root, *level);
3606 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003607 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003608
3609 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003610 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003611 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003612 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003613 path->nodes[*level] = NULL;
3614 *level += 1;
3615 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003616
Chris Masone7a84562008-06-25 16:01:31 -04003617 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003618 return 0;
3619}
3620
Chris Mason9aca1d52007-03-13 11:09:37 -04003621/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04003622 * helper function for drop_subtree, this function is similar to
3623 * walk_down_tree. The main difference is that it checks reference
3624 * counts while tree blocks are locked.
3625 */
3626static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
3627 struct btrfs_root *root,
3628 struct btrfs_path *path, int *level)
3629{
3630 struct extent_buffer *next;
3631 struct extent_buffer *cur;
3632 struct extent_buffer *parent;
3633 u64 bytenr;
3634 u64 ptr_gen;
3635 u32 blocksize;
3636 u32 refs;
3637 int ret;
3638
3639 cur = path->nodes[*level];
3640 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3641 &refs);
3642 BUG_ON(ret);
3643 if (refs > 1)
3644 goto out;
3645
3646 while (*level >= 0) {
3647 cur = path->nodes[*level];
3648 if (*level == 0) {
3649 ret = btrfs_drop_leaf_ref(trans, root, cur);
3650 BUG_ON(ret);
3651 clean_tree_block(trans, root, cur);
3652 break;
3653 }
3654 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3655 clean_tree_block(trans, root, cur);
3656 break;
3657 }
3658
3659 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3660 blocksize = btrfs_level_size(root, *level - 1);
3661 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3662
3663 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3664 btrfs_tree_lock(next);
3665
3666 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3667 &refs);
3668 BUG_ON(ret);
3669 if (refs > 1) {
3670 parent = path->nodes[*level];
3671 ret = btrfs_free_extent(trans, root, bytenr,
3672 blocksize, parent->start,
3673 btrfs_header_owner(parent),
3674 btrfs_header_generation(parent),
3675 *level - 1, 1);
3676 BUG_ON(ret);
3677 path->slots[*level]++;
3678 btrfs_tree_unlock(next);
3679 free_extent_buffer(next);
3680 continue;
3681 }
3682
3683 *level = btrfs_header_level(next);
3684 path->nodes[*level] = next;
3685 path->slots[*level] = 0;
3686 path->locks[*level] = 1;
3687 cond_resched();
3688 }
3689out:
3690 parent = path->nodes[*level + 1];
3691 bytenr = path->nodes[*level]->start;
3692 blocksize = path->nodes[*level]->len;
3693
3694 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3695 parent->start, btrfs_header_owner(parent),
3696 btrfs_header_generation(parent), *level, 1);
3697 BUG_ON(ret);
3698
3699 if (path->locks[*level]) {
3700 btrfs_tree_unlock(path->nodes[*level]);
3701 path->locks[*level] = 0;
3702 }
3703 free_extent_buffer(path->nodes[*level]);
3704 path->nodes[*level] = NULL;
3705 *level += 1;
3706 cond_resched();
3707 return 0;
3708}
3709
3710/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003711 * helper for dropping snapshots. This walks back up the tree in the path
3712 * to find the first node higher up where we haven't yet gone through
3713 * all the slots
3714 */
Chris Mason98ed5172008-01-03 10:01:48 -05003715static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3716 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003717 struct btrfs_path *path,
3718 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003719{
Chris Mason7bb86312007-12-11 09:25:06 -05003720 u64 root_owner;
3721 u64 root_gen;
3722 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003723 int i;
3724 int slot;
3725 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003726
Yan Zhengf82d02d2008-10-29 14:49:05 -04003727 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003728 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003729 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3730 struct extent_buffer *node;
3731 struct btrfs_disk_key disk_key;
3732 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003733 path->slots[i]++;
3734 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003735 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003736 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003737 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003738 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003739 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003740 return 0;
3741 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003742 struct extent_buffer *parent;
3743 if (path->nodes[*level] == root->node)
3744 parent = path->nodes[*level];
3745 else
3746 parent = path->nodes[*level + 1];
3747
3748 root_owner = btrfs_header_owner(parent);
3749 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003750
3751 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003752 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003753 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003754 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003755 parent->start, root_owner,
3756 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003757 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003758 if (path->locks[*level]) {
3759 btrfs_tree_unlock(path->nodes[*level]);
3760 path->locks[*level] = 0;
3761 }
Chris Mason5f39d392007-10-15 16:14:19 -04003762 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003763 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003764 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003765 }
3766 }
3767 return 1;
3768}
3769
Chris Mason9aca1d52007-03-13 11:09:37 -04003770/*
3771 * drop the reference count on the tree rooted at 'snap'. This traverses
3772 * the tree freeing any blocks that have a ref count of zero after being
3773 * decremented.
3774 */
Chris Masone089f052007-03-16 16:20:31 -04003775int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003776 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003777{
Chris Mason3768f362007-03-13 16:47:54 -04003778 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003779 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003780 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003781 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003782 int i;
3783 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003784 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003785
Chris Masona2135012008-06-25 16:01:30 -04003786 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003787 path = btrfs_alloc_path();
3788 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003789
Chris Mason5f39d392007-10-15 16:14:19 -04003790 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003791 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003792 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3793 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003794 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003795 path->slots[level] = 0;
3796 } else {
3797 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003798 struct btrfs_disk_key found_key;
3799 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003800
Chris Mason9f3a7422007-08-07 15:52:19 -04003801 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003802 level = root_item->drop_level;
3803 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003804 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003805 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003806 ret = wret;
3807 goto out;
3808 }
Chris Mason5f39d392007-10-15 16:14:19 -04003809 node = path->nodes[level];
3810 btrfs_node_key(node, &found_key, path->slots[level]);
3811 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3812 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003813 /*
3814 * unlock our path, this is safe because only this
3815 * function is allowed to delete this snapshot
3816 */
Chris Mason925baed2008-06-25 16:01:30 -04003817 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3818 if (path->nodes[i] && path->locks[i]) {
3819 path->locks[i] = 0;
3820 btrfs_tree_unlock(path->nodes[i]);
3821 }
3822 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003823 }
Chris Mason20524f02007-03-10 06:35:47 -05003824 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003825 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003826 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003827 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003828 if (wret < 0)
3829 ret = wret;
3830
Yan Zhengf82d02d2008-10-29 14:49:05 -04003831 wret = walk_up_tree(trans, root, path, &level,
3832 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003833 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003834 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003835 if (wret < 0)
3836 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003837 if (trans->transaction->in_commit) {
3838 ret = -EAGAIN;
3839 break;
3840 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003841 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003842 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003843 }
Chris Mason83e15a22007-03-12 09:03:27 -04003844 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003845 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003846 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003847 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003848 }
Chris Mason20524f02007-03-10 06:35:47 -05003849 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003850out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003851 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003852 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003853}
Chris Mason9078a3e2007-04-26 16:46:15 -04003854
Yan Zhengf82d02d2008-10-29 14:49:05 -04003855int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3856 struct btrfs_root *root,
3857 struct extent_buffer *node,
3858 struct extent_buffer *parent)
3859{
3860 struct btrfs_path *path;
3861 int level;
3862 int parent_level;
3863 int ret = 0;
3864 int wret;
3865
3866 path = btrfs_alloc_path();
3867 BUG_ON(!path);
3868
3869 BUG_ON(!btrfs_tree_locked(parent));
3870 parent_level = btrfs_header_level(parent);
3871 extent_buffer_get(parent);
3872 path->nodes[parent_level] = parent;
3873 path->slots[parent_level] = btrfs_header_nritems(parent);
3874
3875 BUG_ON(!btrfs_tree_locked(node));
3876 level = btrfs_header_level(node);
3877 extent_buffer_get(node);
3878 path->nodes[level] = node;
3879 path->slots[level] = 0;
3880
3881 while (1) {
3882 wret = walk_down_subtree(trans, root, path, &level);
3883 if (wret < 0)
3884 ret = wret;
3885 if (wret != 0)
3886 break;
3887
3888 wret = walk_up_tree(trans, root, path, &level, parent_level);
3889 if (wret < 0)
3890 ret = wret;
3891 if (wret != 0)
3892 break;
3893 }
3894
3895 btrfs_free_path(path);
3896 return ret;
3897}
3898
Chris Mason8e7bf942008-04-28 09:02:36 -04003899static unsigned long calc_ra(unsigned long start, unsigned long last,
3900 unsigned long nr)
3901{
3902 return min(last, start + nr - 1);
3903}
3904
Chris Mason98ed5172008-01-03 10:01:48 -05003905static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3906 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003907{
3908 u64 page_start;
3909 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003910 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003911 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003912 unsigned long i;
3913 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003914 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003915 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003916 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003917 unsigned int total_read = 0;
3918 unsigned int total_dirty = 0;
3919 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003920
3921 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003922
3923 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003924 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003925 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3926
Zheng Yan1a40e232008-09-26 10:09:34 -04003927 /* make sure the dirty trick played by the caller work */
3928 ret = invalidate_inode_pages2_range(inode->i_mapping,
3929 first_index, last_index);
3930 if (ret)
3931 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003932
Chris Mason4313b392008-01-03 09:08:48 -05003933 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003934
Zheng Yan1a40e232008-09-26 10:09:34 -04003935 for (i = first_index ; i <= last_index; i++) {
3936 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003937 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003938 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003939 }
3940 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003941again:
3942 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003943 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003944 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003945 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003946 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003947 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003948 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003949 if (!PageUptodate(page)) {
3950 btrfs_readpage(NULL, page);
3951 lock_page(page);
3952 if (!PageUptodate(page)) {
3953 unlock_page(page);
3954 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003955 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003956 goto out_unlock;
3957 }
3958 }
Chris Masonec44a352008-04-28 15:29:52 -04003959 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003960
Chris Masonedbd8d42007-12-21 16:27:24 -05003961 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3962 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003963 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003964
Chris Mason3eaa2882008-07-24 11:57:52 -04003965 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3966 if (ordered) {
3967 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3968 unlock_page(page);
3969 page_cache_release(page);
3970 btrfs_start_ordered_extent(inode, ordered, 1);
3971 btrfs_put_ordered_extent(ordered);
3972 goto again;
3973 }
3974 set_page_extent_mapped(page);
3975
Chris Masonea8c2812008-08-04 23:17:27 -04003976 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003977 if (i == first_index)
3978 set_extent_bits(io_tree, page_start, page_end,
3979 EXTENT_BOUNDARY, GFP_NOFS);
3980
Chris Masona061fc82008-05-07 11:43:44 -04003981 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003982 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003983
Chris Masond1310b22008-01-24 16:13:08 -05003984 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003985 unlock_page(page);
3986 page_cache_release(page);
3987 }
3988
3989out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04003990 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05003991 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003992 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04003993 return ret;
3994}
3995
Zheng Yan1a40e232008-09-26 10:09:34 -04003996static int noinline relocate_data_extent(struct inode *reloc_inode,
3997 struct btrfs_key *extent_key,
3998 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05003999{
Zheng Yan1a40e232008-09-26 10:09:34 -04004000 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4001 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4002 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04004003 u64 start = extent_key->objectid - offset;
4004 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004005
4006 em = alloc_extent_map(GFP_NOFS);
4007 BUG_ON(!em || IS_ERR(em));
4008
Yan Zheng66435582008-10-30 14:19:50 -04004009 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004010 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004011 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004012 em->block_start = extent_key->objectid;
4013 em->bdev = root->fs_info->fs_devices->latest_bdev;
4014 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4015
4016 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04004017 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004018 while (1) {
4019 int ret;
4020 spin_lock(&em_tree->lock);
4021 ret = add_extent_mapping(em_tree, em);
4022 spin_unlock(&em_tree->lock);
4023 if (ret != -EEXIST) {
4024 free_extent_map(em);
4025 break;
4026 }
Yan Zheng66435582008-10-30 14:19:50 -04004027 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004028 }
Yan Zheng66435582008-10-30 14:19:50 -04004029 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004030
Yan Zheng66435582008-10-30 14:19:50 -04004031 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04004032}
4033
4034struct btrfs_ref_path {
4035 u64 extent_start;
4036 u64 nodes[BTRFS_MAX_LEVEL];
4037 u64 root_objectid;
4038 u64 root_generation;
4039 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004040 u32 num_refs;
4041 int lowest_level;
4042 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004043 int shared_level;
4044
4045 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4046 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04004047};
4048
4049struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04004050 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04004051 u64 disk_bytenr;
4052 u64 disk_num_bytes;
4053 u64 offset;
4054 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04004055 u8 compression;
4056 u8 encryption;
4057 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04004058};
4059
4060static int is_cowonly_root(u64 root_objectid)
4061{
4062 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4063 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4064 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4065 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
4066 root_objectid == BTRFS_TREE_LOG_OBJECTID)
4067 return 1;
4068 return 0;
4069}
4070
4071static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
4072 struct btrfs_root *extent_root,
4073 struct btrfs_ref_path *ref_path,
4074 int first_time)
4075{
4076 struct extent_buffer *leaf;
4077 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05004078 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05004079 struct btrfs_key key;
4080 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04004081 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05004082 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04004083 int level;
4084 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05004085
Zheng Yan1a40e232008-09-26 10:09:34 -04004086 path = btrfs_alloc_path();
4087 if (!path)
4088 return -ENOMEM;
4089
Zheng Yan1a40e232008-09-26 10:09:34 -04004090 if (first_time) {
4091 ref_path->lowest_level = -1;
4092 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004093 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004094 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04004095 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004096walk_down:
4097 level = ref_path->current_level - 1;
4098 while (level >= -1) {
4099 u64 parent;
4100 if (level < ref_path->lowest_level)
4101 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004102
Zheng Yan1a40e232008-09-26 10:09:34 -04004103 if (level >= 0) {
4104 bytenr = ref_path->nodes[level];
4105 } else {
4106 bytenr = ref_path->extent_start;
4107 }
4108 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004109
Zheng Yan1a40e232008-09-26 10:09:34 -04004110 parent = ref_path->nodes[level + 1];
4111 ref_path->nodes[level + 1] = 0;
4112 ref_path->current_level = level;
4113 BUG_ON(parent == 0);
4114
4115 key.objectid = bytenr;
4116 key.offset = parent + 1;
4117 key.type = BTRFS_EXTENT_REF_KEY;
4118
4119 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004120 if (ret < 0)
4121 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004122 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004123
Chris Masonedbd8d42007-12-21 16:27:24 -05004124 leaf = path->nodes[0];
4125 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04004126 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04004127 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04004128 if (ret < 0)
4129 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004130 if (ret > 0)
4131 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04004132 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04004133 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004134
4135 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04004136 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04004137 found_key.type == BTRFS_EXTENT_REF_KEY) {
4138 if (level < ref_path->shared_level)
4139 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004140 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004141 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004142next:
4143 level--;
4144 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004145 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004146 }
4147 /* reached lowest level */
4148 ret = 1;
4149 goto out;
4150walk_up:
4151 level = ref_path->current_level;
4152 while (level < BTRFS_MAX_LEVEL - 1) {
4153 u64 ref_objectid;
4154 if (level >= 0) {
4155 bytenr = ref_path->nodes[level];
4156 } else {
4157 bytenr = ref_path->extent_start;
Chris Masona061fc82008-05-07 11:43:44 -04004158 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004159 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004160
Zheng Yan1a40e232008-09-26 10:09:34 -04004161 key.objectid = bytenr;
4162 key.offset = 0;
4163 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05004164
Zheng Yan1a40e232008-09-26 10:09:34 -04004165 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4166 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05004167 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004168
4169 leaf = path->nodes[0];
4170 nritems = btrfs_header_nritems(leaf);
4171 if (path->slots[0] >= nritems) {
4172 ret = btrfs_next_leaf(extent_root, path);
4173 if (ret < 0)
4174 goto out;
4175 if (ret > 0) {
4176 /* the extent was freed by someone */
4177 if (ref_path->lowest_level == level)
4178 goto out;
4179 btrfs_release_path(extent_root, path);
4180 goto walk_down;
4181 }
4182 leaf = path->nodes[0];
4183 }
4184
4185 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4186 if (found_key.objectid != bytenr ||
4187 found_key.type != BTRFS_EXTENT_REF_KEY) {
4188 /* the extent was freed by someone */
4189 if (ref_path->lowest_level == level) {
4190 ret = 1;
4191 goto out;
4192 }
4193 btrfs_release_path(extent_root, path);
4194 goto walk_down;
4195 }
4196found:
4197 ref = btrfs_item_ptr(leaf, path->slots[0],
4198 struct btrfs_extent_ref);
4199 ref_objectid = btrfs_ref_objectid(leaf, ref);
4200 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4201 if (first_time) {
4202 level = (int)ref_objectid;
4203 BUG_ON(level >= BTRFS_MAX_LEVEL);
4204 ref_path->lowest_level = level;
4205 ref_path->current_level = level;
4206 ref_path->nodes[level] = bytenr;
4207 } else {
4208 WARN_ON(ref_objectid != level);
4209 }
4210 } else {
4211 WARN_ON(level != -1);
4212 }
4213 first_time = 0;
4214
4215 if (ref_path->lowest_level == level) {
4216 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004217 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4218 }
4219
4220 /*
4221 * the block is tree root or the block isn't in reference
4222 * counted tree.
4223 */
4224 if (found_key.objectid == found_key.offset ||
4225 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4226 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4227 ref_path->root_generation =
4228 btrfs_ref_generation(leaf, ref);
4229 if (level < 0) {
4230 /* special reference from the tree log */
4231 ref_path->nodes[0] = found_key.offset;
4232 ref_path->current_level = 0;
4233 }
4234 ret = 0;
4235 goto out;
4236 }
4237
4238 level++;
4239 BUG_ON(ref_path->nodes[level] != 0);
4240 ref_path->nodes[level] = found_key.offset;
4241 ref_path->current_level = level;
4242
4243 /*
4244 * the reference was created in the running transaction,
4245 * no need to continue walking up.
4246 */
4247 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4248 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4249 ref_path->root_generation =
4250 btrfs_ref_generation(leaf, ref);
4251 ret = 0;
4252 goto out;
4253 }
4254
4255 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004256 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004257 }
4258 /* reached max tree level, but no tree root found. */
4259 BUG();
4260out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004261 btrfs_free_path(path);
4262 return ret;
4263}
4264
4265static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4266 struct btrfs_root *extent_root,
4267 struct btrfs_ref_path *ref_path,
4268 u64 extent_start)
4269{
4270 memset(ref_path, 0, sizeof(*ref_path));
4271 ref_path->extent_start = extent_start;
4272
4273 return __next_ref_path(trans, extent_root, ref_path, 1);
4274}
4275
4276static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4277 struct btrfs_root *extent_root,
4278 struct btrfs_ref_path *ref_path)
4279{
4280 return __next_ref_path(trans, extent_root, ref_path, 0);
4281}
4282
4283static int noinline get_new_locations(struct inode *reloc_inode,
4284 struct btrfs_key *extent_key,
4285 u64 offset, int no_fragment,
4286 struct disk_extent **extents,
4287 int *nr_extents)
4288{
4289 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4290 struct btrfs_path *path;
4291 struct btrfs_file_extent_item *fi;
4292 struct extent_buffer *leaf;
4293 struct disk_extent *exts = *extents;
4294 struct btrfs_key found_key;
4295 u64 cur_pos;
4296 u64 last_byte;
4297 u32 nritems;
4298 int nr = 0;
4299 int max = *nr_extents;
4300 int ret;
4301
4302 WARN_ON(!no_fragment && *extents);
4303 if (!exts) {
4304 max = 1;
4305 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4306 if (!exts)
4307 return -ENOMEM;
4308 }
4309
4310 path = btrfs_alloc_path();
4311 BUG_ON(!path);
4312
4313 cur_pos = extent_key->objectid - offset;
4314 last_byte = extent_key->objectid + extent_key->offset;
4315 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4316 cur_pos, 0);
4317 if (ret < 0)
4318 goto out;
4319 if (ret > 0) {
4320 ret = -ENOENT;
4321 goto out;
4322 }
4323
4324 while (1) {
4325 leaf = path->nodes[0];
4326 nritems = btrfs_header_nritems(leaf);
4327 if (path->slots[0] >= nritems) {
4328 ret = btrfs_next_leaf(root, path);
4329 if (ret < 0)
4330 goto out;
4331 if (ret > 0)
4332 break;
4333 leaf = path->nodes[0];
4334 }
4335
4336 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4337 if (found_key.offset != cur_pos ||
4338 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4339 found_key.objectid != reloc_inode->i_ino)
4340 break;
4341
4342 fi = btrfs_item_ptr(leaf, path->slots[0],
4343 struct btrfs_file_extent_item);
4344 if (btrfs_file_extent_type(leaf, fi) !=
4345 BTRFS_FILE_EXTENT_REG ||
4346 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4347 break;
4348
4349 if (nr == max) {
4350 struct disk_extent *old = exts;
4351 max *= 2;
4352 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4353 memcpy(exts, old, sizeof(*exts) * nr);
4354 if (old != *extents)
4355 kfree(old);
4356 }
4357
4358 exts[nr].disk_bytenr =
4359 btrfs_file_extent_disk_bytenr(leaf, fi);
4360 exts[nr].disk_num_bytes =
4361 btrfs_file_extent_disk_num_bytes(leaf, fi);
4362 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4363 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04004364 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4365 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4366 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4367 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4368 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04004369 BUG_ON(exts[nr].offset > 0);
4370 BUG_ON(exts[nr].compression || exts[nr].encryption);
4371 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004372
4373 cur_pos += exts[nr].num_bytes;
4374 nr++;
4375
4376 if (cur_pos + offset >= last_byte)
4377 break;
4378
4379 if (no_fragment) {
4380 ret = 1;
4381 goto out;
4382 }
4383 path->slots[0]++;
4384 }
4385
4386 WARN_ON(cur_pos + offset > last_byte);
4387 if (cur_pos + offset < last_byte) {
4388 ret = -ENOENT;
4389 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05004390 }
4391 ret = 0;
4392out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004393 btrfs_free_path(path);
4394 if (ret) {
4395 if (exts != *extents)
4396 kfree(exts);
4397 } else {
4398 *extents = exts;
4399 *nr_extents = nr;
4400 }
4401 return ret;
4402}
4403
4404static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
4405 struct btrfs_root *root,
4406 struct btrfs_path *path,
4407 struct btrfs_key *extent_key,
4408 struct btrfs_key *leaf_key,
4409 struct btrfs_ref_path *ref_path,
4410 struct disk_extent *new_extents,
4411 int nr_extents)
4412{
4413 struct extent_buffer *leaf;
4414 struct btrfs_file_extent_item *fi;
4415 struct inode *inode = NULL;
4416 struct btrfs_key key;
4417 u64 lock_start = 0;
4418 u64 lock_end = 0;
4419 u64 num_bytes;
4420 u64 ext_offset;
4421 u64 first_pos;
4422 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004423 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004424 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04004425 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04004426 int ret;
4427
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004428 memcpy(&key, leaf_key, sizeof(key));
4429 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004430 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004431 if (key.objectid < ref_path->owner_objectid ||
4432 (key.objectid == ref_path->owner_objectid &&
4433 key.type < BTRFS_EXTENT_DATA_KEY)) {
4434 key.objectid = ref_path->owner_objectid;
4435 key.type = BTRFS_EXTENT_DATA_KEY;
4436 key.offset = 0;
4437 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004438 }
4439
4440 while (1) {
4441 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4442 if (ret < 0)
4443 goto out;
4444
4445 leaf = path->nodes[0];
4446 nritems = btrfs_header_nritems(leaf);
4447next:
4448 if (extent_locked && ret > 0) {
4449 /*
4450 * the file extent item was modified by someone
4451 * before the extent got locked.
4452 */
Zheng Yan1a40e232008-09-26 10:09:34 -04004453 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4454 lock_end, GFP_NOFS);
4455 extent_locked = 0;
4456 }
4457
4458 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004459 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04004460 break;
4461
4462 BUG_ON(extent_locked);
4463 ret = btrfs_next_leaf(root, path);
4464 if (ret < 0)
4465 goto out;
4466 if (ret > 0)
4467 break;
4468 leaf = path->nodes[0];
4469 nritems = btrfs_header_nritems(leaf);
4470 }
4471
4472 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4473
4474 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4475 if ((key.objectid > ref_path->owner_objectid) ||
4476 (key.objectid == ref_path->owner_objectid &&
4477 key.type > BTRFS_EXTENT_DATA_KEY) ||
4478 (key.offset >= first_pos + extent_key->offset))
4479 break;
4480 }
4481
4482 if (inode && key.objectid != inode->i_ino) {
4483 BUG_ON(extent_locked);
4484 btrfs_release_path(root, path);
4485 mutex_unlock(&inode->i_mutex);
4486 iput(inode);
4487 inode = NULL;
4488 continue;
4489 }
4490
4491 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4492 path->slots[0]++;
4493 ret = 1;
4494 goto next;
4495 }
4496 fi = btrfs_item_ptr(leaf, path->slots[0],
4497 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04004498 extent_type = btrfs_file_extent_type(leaf, fi);
4499 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4500 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04004501 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4502 extent_key->objectid)) {
4503 path->slots[0]++;
4504 ret = 1;
4505 goto next;
4506 }
4507
4508 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4509 ext_offset = btrfs_file_extent_offset(leaf, fi);
4510
4511 if (first_pos > key.offset - ext_offset)
4512 first_pos = key.offset - ext_offset;
4513
4514 if (!extent_locked) {
4515 lock_start = key.offset;
4516 lock_end = lock_start + num_bytes - 1;
4517 } else {
Yan Zheng66435582008-10-30 14:19:50 -04004518 if (lock_start > key.offset ||
4519 lock_end + 1 < key.offset + num_bytes) {
4520 unlock_extent(&BTRFS_I(inode)->io_tree,
4521 lock_start, lock_end, GFP_NOFS);
4522 extent_locked = 0;
4523 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004524 }
4525
4526 if (!inode) {
4527 btrfs_release_path(root, path);
4528
4529 inode = btrfs_iget_locked(root->fs_info->sb,
4530 key.objectid, root);
4531 if (inode->i_state & I_NEW) {
4532 BTRFS_I(inode)->root = root;
4533 BTRFS_I(inode)->location.objectid =
4534 key.objectid;
4535 BTRFS_I(inode)->location.type =
4536 BTRFS_INODE_ITEM_KEY;
4537 BTRFS_I(inode)->location.offset = 0;
4538 btrfs_read_locked_inode(inode);
4539 unlock_new_inode(inode);
4540 }
4541 /*
4542 * some code call btrfs_commit_transaction while
4543 * holding the i_mutex, so we can't use mutex_lock
4544 * here.
4545 */
4546 if (is_bad_inode(inode) ||
4547 !mutex_trylock(&inode->i_mutex)) {
4548 iput(inode);
4549 inode = NULL;
4550 key.offset = (u64)-1;
4551 goto skip;
4552 }
4553 }
4554
4555 if (!extent_locked) {
4556 struct btrfs_ordered_extent *ordered;
4557
4558 btrfs_release_path(root, path);
4559
4560 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4561 lock_end, GFP_NOFS);
4562 ordered = btrfs_lookup_first_ordered_extent(inode,
4563 lock_end);
4564 if (ordered &&
4565 ordered->file_offset <= lock_end &&
4566 ordered->file_offset + ordered->len > lock_start) {
4567 unlock_extent(&BTRFS_I(inode)->io_tree,
4568 lock_start, lock_end, GFP_NOFS);
4569 btrfs_start_ordered_extent(inode, ordered, 1);
4570 btrfs_put_ordered_extent(ordered);
4571 key.offset += num_bytes;
4572 goto skip;
4573 }
4574 if (ordered)
4575 btrfs_put_ordered_extent(ordered);
4576
Zheng Yan1a40e232008-09-26 10:09:34 -04004577 extent_locked = 1;
4578 continue;
4579 }
4580
4581 if (nr_extents == 1) {
4582 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04004583 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4584 new_extents[0].disk_bytenr);
4585 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4586 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004587 btrfs_mark_buffer_dirty(leaf);
4588
4589 btrfs_drop_extent_cache(inode, key.offset,
4590 key.offset + num_bytes - 1, 0);
4591
4592 ret = btrfs_inc_extent_ref(trans, root,
4593 new_extents[0].disk_bytenr,
4594 new_extents[0].disk_num_bytes,
4595 leaf->start,
4596 root->root_key.objectid,
4597 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004598 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004599 BUG_ON(ret);
4600
4601 ret = btrfs_free_extent(trans, root,
4602 extent_key->objectid,
4603 extent_key->offset,
4604 leaf->start,
4605 btrfs_header_owner(leaf),
4606 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004607 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004608 BUG_ON(ret);
4609
4610 btrfs_release_path(root, path);
4611 key.offset += num_bytes;
4612 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04004613 BUG_ON(1);
4614#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04004615 u64 alloc_hint;
4616 u64 extent_len;
4617 int i;
4618 /*
4619 * drop old extent pointer at first, then insert the
4620 * new pointers one bye one
4621 */
4622 btrfs_release_path(root, path);
4623 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4624 key.offset + num_bytes,
4625 key.offset, &alloc_hint);
4626 BUG_ON(ret);
4627
4628 for (i = 0; i < nr_extents; i++) {
4629 if (ext_offset >= new_extents[i].num_bytes) {
4630 ext_offset -= new_extents[i].num_bytes;
4631 continue;
4632 }
4633 extent_len = min(new_extents[i].num_bytes -
4634 ext_offset, num_bytes);
4635
4636 ret = btrfs_insert_empty_item(trans, root,
4637 path, &key,
4638 sizeof(*fi));
4639 BUG_ON(ret);
4640
4641 leaf = path->nodes[0];
4642 fi = btrfs_item_ptr(leaf, path->slots[0],
4643 struct btrfs_file_extent_item);
4644 btrfs_set_file_extent_generation(leaf, fi,
4645 trans->transid);
4646 btrfs_set_file_extent_type(leaf, fi,
4647 BTRFS_FILE_EXTENT_REG);
4648 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4649 new_extents[i].disk_bytenr);
4650 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4651 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004652 btrfs_set_file_extent_ram_bytes(leaf, fi,
4653 new_extents[i].ram_bytes);
4654
4655 btrfs_set_file_extent_compression(leaf, fi,
4656 new_extents[i].compression);
4657 btrfs_set_file_extent_encryption(leaf, fi,
4658 new_extents[i].encryption);
4659 btrfs_set_file_extent_other_encoding(leaf, fi,
4660 new_extents[i].other_encoding);
4661
Zheng Yan1a40e232008-09-26 10:09:34 -04004662 btrfs_set_file_extent_num_bytes(leaf, fi,
4663 extent_len);
4664 ext_offset += new_extents[i].offset;
4665 btrfs_set_file_extent_offset(leaf, fi,
4666 ext_offset);
4667 btrfs_mark_buffer_dirty(leaf);
4668
4669 btrfs_drop_extent_cache(inode, key.offset,
4670 key.offset + extent_len - 1, 0);
4671
4672 ret = btrfs_inc_extent_ref(trans, root,
4673 new_extents[i].disk_bytenr,
4674 new_extents[i].disk_num_bytes,
4675 leaf->start,
4676 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004677 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004678 BUG_ON(ret);
4679 btrfs_release_path(root, path);
4680
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004681 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004682
4683 ext_offset = 0;
4684 num_bytes -= extent_len;
4685 key.offset += extent_len;
4686
4687 if (num_bytes == 0)
4688 break;
4689 }
4690 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004691#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04004692 }
4693
4694 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004695 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4696 lock_end, GFP_NOFS);
4697 extent_locked = 0;
4698 }
4699skip:
4700 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4701 key.offset >= first_pos + extent_key->offset)
4702 break;
4703
4704 cond_resched();
4705 }
4706 ret = 0;
4707out:
4708 btrfs_release_path(root, path);
4709 if (inode) {
4710 mutex_unlock(&inode->i_mutex);
4711 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004712 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4713 lock_end, GFP_NOFS);
4714 }
4715 iput(inode);
4716 }
4717 return ret;
4718}
4719
Zheng Yan1a40e232008-09-26 10:09:34 -04004720int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4721 struct btrfs_root *root,
4722 struct extent_buffer *buf, u64 orig_start)
4723{
4724 int level;
4725 int ret;
4726
4727 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4728 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4729
4730 level = btrfs_header_level(buf);
4731 if (level == 0) {
4732 struct btrfs_leaf_ref *ref;
4733 struct btrfs_leaf_ref *orig_ref;
4734
4735 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4736 if (!orig_ref)
4737 return -ENOENT;
4738
4739 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4740 if (!ref) {
4741 btrfs_free_leaf_ref(root, orig_ref);
4742 return -ENOMEM;
4743 }
4744
4745 ref->nritems = orig_ref->nritems;
4746 memcpy(ref->extents, orig_ref->extents,
4747 sizeof(ref->extents[0]) * ref->nritems);
4748
4749 btrfs_free_leaf_ref(root, orig_ref);
4750
4751 ref->root_gen = trans->transid;
4752 ref->bytenr = buf->start;
4753 ref->owner = btrfs_header_owner(buf);
4754 ref->generation = btrfs_header_generation(buf);
4755 ret = btrfs_add_leaf_ref(root, ref, 0);
4756 WARN_ON(ret);
4757 btrfs_free_leaf_ref(root, ref);
4758 }
4759 return 0;
4760}
4761
4762static int noinline invalidate_extent_cache(struct btrfs_root *root,
4763 struct extent_buffer *leaf,
4764 struct btrfs_block_group_cache *group,
4765 struct btrfs_root *target_root)
4766{
4767 struct btrfs_key key;
4768 struct inode *inode = NULL;
4769 struct btrfs_file_extent_item *fi;
4770 u64 num_bytes;
4771 u64 skip_objectid = 0;
4772 u32 nritems;
4773 u32 i;
4774
4775 nritems = btrfs_header_nritems(leaf);
4776 for (i = 0; i < nritems; i++) {
4777 btrfs_item_key_to_cpu(leaf, &key, i);
4778 if (key.objectid == skip_objectid ||
4779 key.type != BTRFS_EXTENT_DATA_KEY)
4780 continue;
4781 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4782 if (btrfs_file_extent_type(leaf, fi) ==
4783 BTRFS_FILE_EXTENT_INLINE)
4784 continue;
4785 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4786 continue;
4787 if (!inode || inode->i_ino != key.objectid) {
4788 iput(inode);
4789 inode = btrfs_ilookup(target_root->fs_info->sb,
4790 key.objectid, target_root, 1);
4791 }
4792 if (!inode) {
4793 skip_objectid = key.objectid;
4794 continue;
4795 }
4796 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4797
4798 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4799 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004800 btrfs_drop_extent_cache(inode, key.offset,
4801 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004802 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4803 key.offset + num_bytes - 1, GFP_NOFS);
4804 cond_resched();
4805 }
4806 iput(inode);
4807 return 0;
4808}
4809
4810static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4811 struct btrfs_root *root,
4812 struct extent_buffer *leaf,
4813 struct btrfs_block_group_cache *group,
4814 struct inode *reloc_inode)
4815{
4816 struct btrfs_key key;
4817 struct btrfs_key extent_key;
4818 struct btrfs_file_extent_item *fi;
4819 struct btrfs_leaf_ref *ref;
4820 struct disk_extent *new_extent;
4821 u64 bytenr;
4822 u64 num_bytes;
4823 u32 nritems;
4824 u32 i;
4825 int ext_index;
4826 int nr_extent;
4827 int ret;
4828
4829 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4830 BUG_ON(!new_extent);
4831
4832 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4833 BUG_ON(!ref);
4834
4835 ext_index = -1;
4836 nritems = btrfs_header_nritems(leaf);
4837 for (i = 0; i < nritems; i++) {
4838 btrfs_item_key_to_cpu(leaf, &key, i);
4839 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4840 continue;
4841 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4842 if (btrfs_file_extent_type(leaf, fi) ==
4843 BTRFS_FILE_EXTENT_INLINE)
4844 continue;
4845 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4846 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4847 if (bytenr == 0)
4848 continue;
4849
4850 ext_index++;
4851 if (bytenr >= group->key.objectid + group->key.offset ||
4852 bytenr + num_bytes <= group->key.objectid)
4853 continue;
4854
4855 extent_key.objectid = bytenr;
4856 extent_key.offset = num_bytes;
4857 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4858 nr_extent = 1;
4859 ret = get_new_locations(reloc_inode, &extent_key,
4860 group->key.objectid, 1,
4861 &new_extent, &nr_extent);
4862 if (ret > 0)
4863 continue;
4864 BUG_ON(ret < 0);
4865
4866 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4867 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4868 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4869 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4870
Zheng Yan1a40e232008-09-26 10:09:34 -04004871 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4872 new_extent->disk_bytenr);
4873 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4874 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004875 btrfs_mark_buffer_dirty(leaf);
4876
4877 ret = btrfs_inc_extent_ref(trans, root,
4878 new_extent->disk_bytenr,
4879 new_extent->disk_num_bytes,
4880 leaf->start,
4881 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004882 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004883 BUG_ON(ret);
4884 ret = btrfs_free_extent(trans, root,
4885 bytenr, num_bytes, leaf->start,
4886 btrfs_header_owner(leaf),
4887 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004888 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004889 BUG_ON(ret);
4890 cond_resched();
4891 }
4892 kfree(new_extent);
4893 BUG_ON(ext_index + 1 != ref->nritems);
4894 btrfs_free_leaf_ref(root, ref);
4895 return 0;
4896}
4897
Yan Zhengf82d02d2008-10-29 14:49:05 -04004898int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4899 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004900{
4901 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004902 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004903
4904 if (root->reloc_root) {
4905 reloc_root = root->reloc_root;
4906 root->reloc_root = NULL;
4907 list_add(&reloc_root->dead_list,
4908 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004909
4910 btrfs_set_root_bytenr(&reloc_root->root_item,
4911 reloc_root->node->start);
4912 btrfs_set_root_level(&root->root_item,
4913 btrfs_header_level(reloc_root->node));
4914 memset(&reloc_root->root_item.drop_progress, 0,
4915 sizeof(struct btrfs_disk_key));
4916 reloc_root->root_item.drop_level = 0;
4917
4918 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4919 &reloc_root->root_key,
4920 &reloc_root->root_item);
4921 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004922 }
4923 return 0;
4924}
4925
4926int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4927{
4928 struct btrfs_trans_handle *trans;
4929 struct btrfs_root *reloc_root;
4930 struct btrfs_root *prev_root = NULL;
4931 struct list_head dead_roots;
4932 int ret;
4933 unsigned long nr;
4934
4935 INIT_LIST_HEAD(&dead_roots);
4936 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4937
4938 while (!list_empty(&dead_roots)) {
4939 reloc_root = list_entry(dead_roots.prev,
4940 struct btrfs_root, dead_list);
4941 list_del_init(&reloc_root->dead_list);
4942
4943 BUG_ON(reloc_root->commit_root != NULL);
4944 while (1) {
4945 trans = btrfs_join_transaction(root, 1);
4946 BUG_ON(!trans);
4947
4948 mutex_lock(&root->fs_info->drop_mutex);
4949 ret = btrfs_drop_snapshot(trans, reloc_root);
4950 if (ret != -EAGAIN)
4951 break;
4952 mutex_unlock(&root->fs_info->drop_mutex);
4953
4954 nr = trans->blocks_used;
4955 ret = btrfs_end_transaction(trans, root);
4956 BUG_ON(ret);
4957 btrfs_btree_balance_dirty(root, nr);
4958 }
4959
4960 free_extent_buffer(reloc_root->node);
4961
4962 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4963 &reloc_root->root_key);
4964 BUG_ON(ret);
4965 mutex_unlock(&root->fs_info->drop_mutex);
4966
4967 nr = trans->blocks_used;
4968 ret = btrfs_end_transaction(trans, root);
4969 BUG_ON(ret);
4970 btrfs_btree_balance_dirty(root, nr);
4971
4972 kfree(prev_root);
4973 prev_root = reloc_root;
4974 }
4975 if (prev_root) {
4976 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4977 kfree(prev_root);
4978 }
4979 return 0;
4980}
4981
4982int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4983{
4984 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4985 return 0;
4986}
4987
4988int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4989{
4990 struct btrfs_root *reloc_root;
4991 struct btrfs_trans_handle *trans;
4992 struct btrfs_key location;
4993 int found;
4994 int ret;
4995
4996 mutex_lock(&root->fs_info->tree_reloc_mutex);
4997 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4998 BUG_ON(ret);
4999 found = !list_empty(&root->fs_info->dead_reloc_roots);
5000 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5001
5002 if (found) {
5003 trans = btrfs_start_transaction(root, 1);
5004 BUG_ON(!trans);
5005 ret = btrfs_commit_transaction(trans, root);
5006 BUG_ON(ret);
5007 }
5008
5009 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5010 location.offset = (u64)-1;
5011 location.type = BTRFS_ROOT_ITEM_KEY;
5012
5013 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5014 BUG_ON(!reloc_root);
5015 btrfs_orphan_cleanup(reloc_root);
5016 return 0;
5017}
5018
5019static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
5020 struct btrfs_root *root)
5021{
5022 struct btrfs_root *reloc_root;
5023 struct extent_buffer *eb;
5024 struct btrfs_root_item *root_item;
5025 struct btrfs_key root_key;
5026 int ret;
5027
5028 BUG_ON(!root->ref_cows);
5029 if (root->reloc_root)
5030 return 0;
5031
5032 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5033 BUG_ON(!root_item);
5034
5035 ret = btrfs_copy_root(trans, root, root->commit_root,
5036 &eb, BTRFS_TREE_RELOC_OBJECTID);
5037 BUG_ON(ret);
5038
5039 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5040 root_key.offset = root->root_key.objectid;
5041 root_key.type = BTRFS_ROOT_ITEM_KEY;
5042
5043 memcpy(root_item, &root->root_item, sizeof(root_item));
5044 btrfs_set_root_refs(root_item, 0);
5045 btrfs_set_root_bytenr(root_item, eb->start);
5046 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04005047 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005048
5049 btrfs_tree_unlock(eb);
5050 free_extent_buffer(eb);
5051
5052 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5053 &root_key, root_item);
5054 BUG_ON(ret);
5055 kfree(root_item);
5056
5057 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5058 &root_key);
5059 BUG_ON(!reloc_root);
5060 reloc_root->last_trans = trans->transid;
5061 reloc_root->commit_root = NULL;
5062 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5063
5064 root->reloc_root = reloc_root;
5065 return 0;
5066}
5067
5068/*
5069 * Core function of space balance.
5070 *
5071 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04005072 * counted roots. There is one reloc tree for each subvol, and all
5073 * reloc trees share same root key objectid. Reloc trees are snapshots
5074 * of the latest committed roots of subvols (root->commit_root).
5075 *
5076 * To relocate a tree block referenced by a subvol, there are two steps.
5077 * COW the block through subvol's reloc tree, then update block pointer
5078 * in the subvol to point to the new block. Since all reloc trees share
5079 * same root key objectid, doing special handing for tree blocks owned
5080 * by them is easy. Once a tree block has been COWed in one reloc tree,
5081 * we can use the resulting new block directly when the same block is
5082 * required to COW again through other reloc trees. By this way, relocated
5083 * tree blocks are shared between reloc trees, so they are also shared
5084 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04005085 */
5086static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
5087 struct btrfs_root *root,
5088 struct btrfs_path *path,
5089 struct btrfs_key *first_key,
5090 struct btrfs_ref_path *ref_path,
5091 struct btrfs_block_group_cache *group,
5092 struct inode *reloc_inode)
5093{
5094 struct btrfs_root *reloc_root;
5095 struct extent_buffer *eb = NULL;
5096 struct btrfs_key *keys;
5097 u64 *nodes;
5098 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005099 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005100 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005101 int ret;
5102
5103 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5104 lowest_level = ref_path->owner_objectid;
5105
Yan Zhengf82d02d2008-10-29 14:49:05 -04005106 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005107 path->lowest_level = lowest_level;
5108 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5109 BUG_ON(ret < 0);
5110 path->lowest_level = 0;
5111 btrfs_release_path(root, path);
5112 return 0;
5113 }
5114
Zheng Yan1a40e232008-09-26 10:09:34 -04005115 mutex_lock(&root->fs_info->tree_reloc_mutex);
5116 ret = init_reloc_tree(trans, root);
5117 BUG_ON(ret);
5118 reloc_root = root->reloc_root;
5119
Yan Zhengf82d02d2008-10-29 14:49:05 -04005120 shared_level = ref_path->shared_level;
5121 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005122
Yan Zhengf82d02d2008-10-29 14:49:05 -04005123 keys = ref_path->node_keys;
5124 nodes = ref_path->new_nodes;
5125 memset(&keys[shared_level + 1], 0,
5126 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5127 memset(&nodes[shared_level + 1], 0,
5128 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04005129
Yan Zhengf82d02d2008-10-29 14:49:05 -04005130 if (nodes[lowest_level] == 0) {
5131 path->lowest_level = lowest_level;
5132 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5133 0, 1);
5134 BUG_ON(ret);
5135 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5136 eb = path->nodes[level];
5137 if (!eb || eb == reloc_root->node)
5138 break;
5139 nodes[level] = eb->start;
5140 if (level == 0)
5141 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5142 else
5143 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5144 }
Yan Zheng2b820322008-11-17 21:11:30 -05005145 if (nodes[0] &&
5146 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005147 eb = path->nodes[0];
5148 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5149 group, reloc_inode);
5150 BUG_ON(ret);
5151 }
5152 btrfs_release_path(reloc_root, path);
5153 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005154 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005155 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04005156 BUG_ON(ret);
5157 }
5158
Zheng Yan1a40e232008-09-26 10:09:34 -04005159 /*
5160 * replace tree blocks in the fs tree with tree blocks in
5161 * the reloc tree.
5162 */
5163 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5164 BUG_ON(ret < 0);
5165
5166 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005167 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5168 0, 0);
5169 BUG_ON(ret);
5170 extent_buffer_get(path->nodes[0]);
5171 eb = path->nodes[0];
5172 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005173 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5174 BUG_ON(ret);
5175 free_extent_buffer(eb);
5176 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005177
Yan Zhengf82d02d2008-10-29 14:49:05 -04005178 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005179 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005180 return 0;
5181}
5182
5183static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
5184 struct btrfs_root *root,
5185 struct btrfs_path *path,
5186 struct btrfs_key *first_key,
5187 struct btrfs_ref_path *ref_path)
5188{
5189 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005190
5191 ret = relocate_one_path(trans, root, path, first_key,
5192 ref_path, NULL, NULL);
5193 BUG_ON(ret);
5194
5195 if (root == root->fs_info->extent_root)
5196 btrfs_extent_post_op(trans, root);
Zheng Yan1a40e232008-09-26 10:09:34 -04005197
5198 return 0;
5199}
5200
5201static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
5202 struct btrfs_root *extent_root,
5203 struct btrfs_path *path,
5204 struct btrfs_key *extent_key)
5205{
5206 int ret;
5207
Zheng Yan1a40e232008-09-26 10:09:34 -04005208 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5209 if (ret)
5210 goto out;
5211 ret = btrfs_del_item(trans, extent_root, path);
5212out:
Chris Masonedbd8d42007-12-21 16:27:24 -05005213 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005214 return ret;
5215}
5216
5217static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
5218 struct btrfs_ref_path *ref_path)
5219{
5220 struct btrfs_key root_key;
5221
5222 root_key.objectid = ref_path->root_objectid;
5223 root_key.type = BTRFS_ROOT_ITEM_KEY;
5224 if (is_cowonly_root(ref_path->root_objectid))
5225 root_key.offset = 0;
5226 else
5227 root_key.offset = (u64)-1;
5228
5229 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5230}
5231
5232static int noinline relocate_one_extent(struct btrfs_root *extent_root,
5233 struct btrfs_path *path,
5234 struct btrfs_key *extent_key,
5235 struct btrfs_block_group_cache *group,
5236 struct inode *reloc_inode, int pass)
5237{
5238 struct btrfs_trans_handle *trans;
5239 struct btrfs_root *found_root;
5240 struct btrfs_ref_path *ref_path = NULL;
5241 struct disk_extent *new_extents = NULL;
5242 int nr_extents = 0;
5243 int loops;
5244 int ret;
5245 int level;
5246 struct btrfs_key first_key;
5247 u64 prev_block = 0;
5248
Zheng Yan1a40e232008-09-26 10:09:34 -04005249
5250 trans = btrfs_start_transaction(extent_root, 1);
5251 BUG_ON(!trans);
5252
5253 if (extent_key->objectid == 0) {
5254 ret = del_extent_zero(trans, extent_root, path, extent_key);
5255 goto out;
5256 }
5257
5258 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5259 if (!ref_path) {
5260 ret = -ENOMEM;
5261 goto out;
5262 }
5263
5264 for (loops = 0; ; loops++) {
5265 if (loops == 0) {
5266 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5267 extent_key->objectid);
5268 } else {
5269 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5270 }
5271 if (ret < 0)
5272 goto out;
5273 if (ret > 0)
5274 break;
5275
5276 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5277 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5278 continue;
5279
5280 found_root = read_ref_root(extent_root->fs_info, ref_path);
5281 BUG_ON(!found_root);
5282 /*
5283 * for reference counted tree, only process reference paths
5284 * rooted at the latest committed root.
5285 */
5286 if (found_root->ref_cows &&
5287 ref_path->root_generation != found_root->root_key.offset)
5288 continue;
5289
5290 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5291 if (pass == 0) {
5292 /*
5293 * copy data extents to new locations
5294 */
5295 u64 group_start = group->key.objectid;
5296 ret = relocate_data_extent(reloc_inode,
5297 extent_key,
5298 group_start);
5299 if (ret < 0)
5300 goto out;
5301 break;
5302 }
5303 level = 0;
5304 } else {
5305 level = ref_path->owner_objectid;
5306 }
5307
5308 if (prev_block != ref_path->nodes[level]) {
5309 struct extent_buffer *eb;
5310 u64 block_start = ref_path->nodes[level];
5311 u64 block_size = btrfs_level_size(found_root, level);
5312
5313 eb = read_tree_block(found_root, block_start,
5314 block_size, 0);
5315 btrfs_tree_lock(eb);
5316 BUG_ON(level != btrfs_header_level(eb));
5317
5318 if (level == 0)
5319 btrfs_item_key_to_cpu(eb, &first_key, 0);
5320 else
5321 btrfs_node_key_to_cpu(eb, &first_key, 0);
5322
5323 btrfs_tree_unlock(eb);
5324 free_extent_buffer(eb);
5325 prev_block = block_start;
5326 }
5327
5328 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
5329 pass >= 2) {
5330 /*
5331 * use fallback method to process the remaining
5332 * references.
5333 */
5334 if (!new_extents) {
5335 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04005336 new_extents = kmalloc(sizeof(*new_extents),
5337 GFP_NOFS);
5338 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005339 ret = get_new_locations(reloc_inode,
5340 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04005341 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04005342 &new_extents,
5343 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005344 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04005345 goto out;
5346 }
5347 btrfs_record_root_in_trans(found_root);
5348 ret = replace_one_extent(trans, found_root,
5349 path, extent_key,
5350 &first_key, ref_path,
5351 new_extents, nr_extents);
5352 if (ret < 0)
5353 goto out;
5354 continue;
5355 }
5356
5357 btrfs_record_root_in_trans(found_root);
5358 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5359 ret = relocate_tree_block(trans, found_root, path,
5360 &first_key, ref_path);
5361 } else {
5362 /*
5363 * try to update data extent references while
5364 * keeping metadata shared between snapshots.
5365 */
5366 ret = relocate_one_path(trans, found_root, path,
5367 &first_key, ref_path,
5368 group, reloc_inode);
5369 }
5370 if (ret < 0)
5371 goto out;
5372 }
5373 ret = 0;
5374out:
5375 btrfs_end_transaction(trans, extent_root);
5376 kfree(new_extents);
5377 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005378 return ret;
5379}
5380
Chris Masonec44a352008-04-28 15:29:52 -04005381static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5382{
5383 u64 num_devices;
5384 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5385 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5386
Yan Zheng2b820322008-11-17 21:11:30 -05005387 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04005388 if (num_devices == 1) {
5389 stripped |= BTRFS_BLOCK_GROUP_DUP;
5390 stripped = flags & ~stripped;
5391
5392 /* turn raid0 into single device chunks */
5393 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5394 return stripped;
5395
5396 /* turn mirroring into duplication */
5397 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5398 BTRFS_BLOCK_GROUP_RAID10))
5399 return stripped | BTRFS_BLOCK_GROUP_DUP;
5400 return flags;
5401 } else {
5402 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04005403 if (flags & stripped)
5404 return flags;
5405
5406 stripped |= BTRFS_BLOCK_GROUP_DUP;
5407 stripped = flags & ~stripped;
5408
5409 /* switch duplicated blocks with raid1 */
5410 if (flags & BTRFS_BLOCK_GROUP_DUP)
5411 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5412
5413 /* turn single device chunks into raid0 */
5414 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5415 }
5416 return flags;
5417}
5418
Chris Mason0ef3e662008-05-24 14:04:53 -04005419int __alloc_chunk_for_shrink(struct btrfs_root *root,
5420 struct btrfs_block_group_cache *shrink_block_group,
5421 int force)
5422{
5423 struct btrfs_trans_handle *trans;
5424 u64 new_alloc_flags;
5425 u64 calc;
5426
Chris Masonc286ac42008-07-22 23:06:41 -04005427 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005428 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04005429 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005430
Chris Mason0ef3e662008-05-24 14:04:53 -04005431 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005432 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04005433
Chris Mason0ef3e662008-05-24 14:04:53 -04005434 new_alloc_flags = update_block_group_flags(root,
5435 shrink_block_group->flags);
5436 if (new_alloc_flags != shrink_block_group->flags) {
5437 calc =
5438 btrfs_block_group_used(&shrink_block_group->item);
5439 } else {
5440 calc = shrink_block_group->key.offset;
5441 }
Chris Masonc286ac42008-07-22 23:06:41 -04005442 spin_unlock(&shrink_block_group->lock);
5443
Chris Mason0ef3e662008-05-24 14:04:53 -04005444 do_chunk_alloc(trans, root->fs_info->extent_root,
5445 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04005446
Chris Mason0ef3e662008-05-24 14:04:53 -04005447 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04005448 } else
5449 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005450 return 0;
5451}
5452
Zheng Yan1a40e232008-09-26 10:09:34 -04005453static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5454 struct btrfs_root *root,
5455 u64 objectid, u64 size)
5456{
5457 struct btrfs_path *path;
5458 struct btrfs_inode_item *item;
5459 struct extent_buffer *leaf;
5460 int ret;
5461
5462 path = btrfs_alloc_path();
5463 if (!path)
5464 return -ENOMEM;
5465
5466 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5467 if (ret)
5468 goto out;
5469
5470 leaf = path->nodes[0];
5471 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5472 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5473 btrfs_set_inode_generation(leaf, item, 1);
5474 btrfs_set_inode_size(leaf, item, size);
5475 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zhengd899e052008-10-30 14:25:28 -04005476 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM |
5477 BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005478 btrfs_mark_buffer_dirty(leaf);
5479 btrfs_release_path(root, path);
5480out:
5481 btrfs_free_path(path);
5482 return ret;
5483}
5484
5485static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
5486 struct btrfs_block_group_cache *group)
5487{
5488 struct inode *inode = NULL;
5489 struct btrfs_trans_handle *trans;
5490 struct btrfs_root *root;
5491 struct btrfs_key root_key;
5492 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5493 int err = 0;
5494
5495 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5496 root_key.type = BTRFS_ROOT_ITEM_KEY;
5497 root_key.offset = (u64)-1;
5498 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5499 if (IS_ERR(root))
5500 return ERR_CAST(root);
5501
5502 trans = btrfs_start_transaction(root, 1);
5503 BUG_ON(!trans);
5504
5505 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5506 if (err)
5507 goto out;
5508
5509 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5510 BUG_ON(err);
5511
5512 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04005513 group->key.offset, 0, group->key.offset,
5514 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005515 BUG_ON(err);
5516
5517 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5518 if (inode->i_state & I_NEW) {
5519 BTRFS_I(inode)->root = root;
5520 BTRFS_I(inode)->location.objectid = objectid;
5521 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5522 BTRFS_I(inode)->location.offset = 0;
5523 btrfs_read_locked_inode(inode);
5524 unlock_new_inode(inode);
5525 BUG_ON(is_bad_inode(inode));
5526 } else {
5527 BUG_ON(1);
5528 }
5529
5530 err = btrfs_orphan_add(trans, inode);
5531out:
5532 btrfs_end_transaction(trans, root);
5533 if (err) {
5534 if (inode)
5535 iput(inode);
5536 inode = ERR_PTR(err);
5537 }
5538 return inode;
5539}
5540
5541int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05005542{
5543 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05005544 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04005545 struct btrfs_fs_info *info = root->fs_info;
5546 struct extent_buffer *leaf;
5547 struct inode *reloc_inode;
5548 struct btrfs_block_group_cache *block_group;
5549 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04005550 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05005551 u64 cur_byte;
5552 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05005553 u32 nritems;
5554 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04005555 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04005556 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005557
Chris Masonedbd8d42007-12-21 16:27:24 -05005558 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04005559
5560 block_group = btrfs_lookup_block_group(info, group_start);
5561 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05005562
Chris Mason323da792008-05-09 11:46:48 -04005563 printk("btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005564 (unsigned long long)block_group->key.objectid,
5565 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04005566
Zheng Yan1a40e232008-09-26 10:09:34 -04005567 path = btrfs_alloc_path();
5568 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04005569
Zheng Yan1a40e232008-09-26 10:09:34 -04005570 reloc_inode = create_reloc_inode(info, block_group);
5571 BUG_ON(IS_ERR(reloc_inode));
5572
Zheng Yan1a40e232008-09-26 10:09:34 -04005573 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05005574 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005575
Zheng Yan1a40e232008-09-26 10:09:34 -04005576 btrfs_start_delalloc_inodes(info->tree_root);
5577 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04005578again:
Yan Zhengd899e052008-10-30 14:25:28 -04005579 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005580 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04005581 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005582 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05005583 key.offset = 0;
5584 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05005585 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05005586
Zheng Yan1a40e232008-09-26 10:09:34 -04005587 trans = btrfs_start_transaction(info->tree_root, 1);
5588 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04005589
Zheng Yan1a40e232008-09-26 10:09:34 -04005590 mutex_lock(&root->fs_info->cleaner_mutex);
5591 btrfs_clean_old_snapshots(info->tree_root);
5592 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5593 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04005594
Yan73e48b22008-01-03 14:14:39 -05005595 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05005596 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5597 if (ret < 0)
5598 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04005599next:
Chris Masonedbd8d42007-12-21 16:27:24 -05005600 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05005601 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05005602 if (path->slots[0] >= nritems) {
5603 ret = btrfs_next_leaf(root, path);
5604 if (ret < 0)
5605 goto out;
5606 if (ret == 1) {
5607 ret = 0;
5608 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005609 }
Yan73e48b22008-01-03 14:14:39 -05005610 leaf = path->nodes[0];
5611 nritems = btrfs_header_nritems(leaf);
5612 }
5613
Zheng Yan1a40e232008-09-26 10:09:34 -04005614 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05005615
Zheng Yan1a40e232008-09-26 10:09:34 -04005616 if (key.objectid >= block_group->key.objectid +
5617 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04005618 break;
5619
Chris Mason725c8462008-01-04 16:47:16 -05005620 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05005621 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005622 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05005623 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005624 continue;
Chris Mason725c8462008-01-04 16:47:16 -05005625 }
5626 progress = 1;
5627
Zheng Yan1a40e232008-09-26 10:09:34 -04005628 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5629 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05005630 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005631 goto next;
5632 }
Yan73e48b22008-01-03 14:14:39 -05005633
Chris Masonedbd8d42007-12-21 16:27:24 -05005634 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005635 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05005636 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005637
5638 __alloc_chunk_for_shrink(root, block_group, 0);
5639 ret = relocate_one_extent(root, path, &key, block_group,
5640 reloc_inode, pass);
5641 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005642 if (ret > 0)
5643 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005644
5645 key.objectid = cur_byte;
5646 key.type = 0;
5647 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005648 }
5649
5650 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005651
5652 if (pass == 0) {
5653 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5654 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5655 WARN_ON(reloc_inode->i_mapping->nrpages);
5656 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005657
5658 if (total_found > 0) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005659 printk("btrfs found %llu extents in pass %d\n",
5660 (unsigned long long)total_found, pass);
5661 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04005662 if (total_found == skipped && pass > 2) {
5663 iput(reloc_inode);
5664 reloc_inode = create_reloc_inode(info, block_group);
5665 pass = 0;
5666 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005667 goto again;
5668 }
5669
Zheng Yan1a40e232008-09-26 10:09:34 -04005670 /* delete reloc_inode */
5671 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005672
Zheng Yan1a40e232008-09-26 10:09:34 -04005673 /* unpin extents in this range */
5674 trans = btrfs_start_transaction(info->tree_root, 1);
5675 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005676
Zheng Yan1a40e232008-09-26 10:09:34 -04005677 spin_lock(&block_group->lock);
5678 WARN_ON(block_group->pinned > 0);
5679 WARN_ON(block_group->reserved > 0);
5680 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5681 spin_unlock(&block_group->lock);
5682 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005683out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005684 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005685 return ret;
5686}
5687
Chris Mason0b86a832008-03-24 15:01:56 -04005688int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
5689 struct btrfs_key *key)
5690{
Chris Mason925baed2008-06-25 16:01:30 -04005691 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005692 struct btrfs_key found_key;
5693 struct extent_buffer *leaf;
5694 int slot;
5695
5696 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5697 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005698 goto out;
5699
Chris Mason0b86a832008-03-24 15:01:56 -04005700 while(1) {
5701 slot = path->slots[0];
5702 leaf = path->nodes[0];
5703 if (slot >= btrfs_header_nritems(leaf)) {
5704 ret = btrfs_next_leaf(root, path);
5705 if (ret == 0)
5706 continue;
5707 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005708 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005709 break;
5710 }
5711 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5712
5713 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005714 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5715 ret = 0;
5716 goto out;
5717 }
Chris Mason0b86a832008-03-24 15:01:56 -04005718 path->slots[0]++;
5719 }
5720 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005721out:
Chris Mason0b86a832008-03-24 15:01:56 -04005722 return ret;
5723}
5724
Zheng Yan1a40e232008-09-26 10:09:34 -04005725int btrfs_free_block_groups(struct btrfs_fs_info *info)
5726{
5727 struct btrfs_block_group_cache *block_group;
5728 struct rb_node *n;
5729
Zheng Yan1a40e232008-09-26 10:09:34 -04005730 spin_lock(&info->block_group_cache_lock);
5731 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5732 block_group = rb_entry(n, struct btrfs_block_group_cache,
5733 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04005734 rb_erase(&block_group->cache_node,
5735 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04005736 spin_unlock(&info->block_group_cache_lock);
5737
5738 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005739 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005740 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005741 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005742 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04005743
5744 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005745 }
5746 spin_unlock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005747 return 0;
5748}
5749
Chris Mason9078a3e2007-04-26 16:46:15 -04005750int btrfs_read_block_groups(struct btrfs_root *root)
5751{
5752 struct btrfs_path *path;
5753 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005754 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005755 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005756 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005757 struct btrfs_key key;
5758 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005759 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005760
Chris Masonbe744172007-05-06 10:15:01 -04005761 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005762 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005763 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005764 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005765 path = btrfs_alloc_path();
5766 if (!path)
5767 return -ENOMEM;
5768
5769 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005770 ret = find_first_block_group(root, path, &key);
5771 if (ret > 0) {
5772 ret = 0;
5773 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005774 }
Chris Mason0b86a832008-03-24 15:01:56 -04005775 if (ret != 0)
5776 goto error;
5777
Chris Mason5f39d392007-10-15 16:14:19 -04005778 leaf = path->nodes[0];
5779 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005780 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005781 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005782 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005783 break;
5784 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005785
Chris Masonc286ac42008-07-22 23:06:41 -04005786 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005787 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005788 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005789 read_extent_buffer(leaf, &cache->item,
5790 btrfs_item_ptr_offset(leaf, path->slots[0]),
5791 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005792 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005793
Chris Mason9078a3e2007-04-26 16:46:15 -04005794 key.objectid = found_key.objectid + found_key.offset;
5795 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005796 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005797
Chris Mason6324fbf2008-03-24 15:01:59 -04005798 ret = update_space_info(info, cache->flags, found_key.offset,
5799 btrfs_block_group_used(&cache->item),
5800 &space_info);
5801 BUG_ON(ret);
5802 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005803 down_write(&space_info->groups_sem);
5804 list_add_tail(&cache->list, &space_info->block_groups);
5805 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005806
Josef Bacik0f9dd462008-09-23 13:14:11 -04005807 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5808 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005809
5810 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05005811 if (btrfs_chunk_readonly(root, cache->key.objectid))
5812 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04005813 }
Chris Mason0b86a832008-03-24 15:01:56 -04005814 ret = 0;
5815error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005816 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005817 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005818}
Chris Mason6324fbf2008-03-24 15:01:59 -04005819
5820int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5821 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005822 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005823 u64 size)
5824{
5825 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005826 struct btrfs_root *extent_root;
5827 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005828
5829 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005830
Chris Masone02119d2008-09-05 16:13:11 -04005831 root->fs_info->last_trans_new_blockgroup = trans->transid;
5832
Chris Mason8f18cf12008-04-25 16:53:30 -04005833 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005834 if (!cache)
5835 return -ENOMEM;
5836
Chris Masone17cade2008-04-15 15:41:47 -04005837 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005838 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04005839 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005840 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005841 INIT_LIST_HEAD(&cache->list);
Chris Mason6324fbf2008-03-24 15:01:59 -04005842 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04005843
Chris Mason6324fbf2008-03-24 15:01:59 -04005844 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005845 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5846 cache->flags = type;
5847 btrfs_set_block_group_flags(&cache->item, type);
5848
5849 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5850 &cache->space_info);
5851 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005852 down_write(&cache->space_info->groups_sem);
5853 list_add_tail(&cache->list, &cache->space_info->block_groups);
5854 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005855
Josef Bacik0f9dd462008-09-23 13:14:11 -04005856 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5857 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005858
Chris Mason6324fbf2008-03-24 15:01:59 -04005859 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5860 sizeof(cache->item));
5861 BUG_ON(ret);
5862
Chris Mason87ef2bb2008-10-30 11:23:27 -04005863 finish_current_insert(trans, extent_root, 0);
5864 ret = del_pending_extents(trans, extent_root, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04005865 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04005866 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005867
Chris Mason6324fbf2008-03-24 15:01:59 -04005868 return 0;
5869}
Zheng Yan1a40e232008-09-26 10:09:34 -04005870
5871int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5872 struct btrfs_root *root, u64 group_start)
5873{
5874 struct btrfs_path *path;
5875 struct btrfs_block_group_cache *block_group;
5876 struct btrfs_key key;
5877 int ret;
5878
Zheng Yan1a40e232008-09-26 10:09:34 -04005879 root = root->fs_info->extent_root;
5880
5881 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5882 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05005883 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04005884
5885 memcpy(&key, &block_group->key, sizeof(key));
5886
5887 path = btrfs_alloc_path();
5888 BUG_ON(!path);
5889
5890 btrfs_remove_free_space_cache(block_group);
5891 rb_erase(&block_group->cache_node,
5892 &root->fs_info->block_group_cache_tree);
Josef Bacik80eb2342008-10-29 14:49:05 -04005893 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005894 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005895 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005896
Yan Zhengc146afa2008-11-12 14:34:12 -05005897 spin_lock(&block_group->space_info->lock);
5898 block_group->space_info->total_bytes -= block_group->key.offset;
5899 block_group->space_info->bytes_readonly -= block_group->key.offset;
5900 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05005901 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05005902
Zheng Yan1a40e232008-09-26 10:09:34 -04005903 /*
5904 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5905 kfree(shrink_block_group);
5906 */
5907
5908 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5909 if (ret > 0)
5910 ret = -EIO;
5911 if (ret < 0)
5912 goto out;
5913
5914 ret = btrfs_del_item(trans, root, path);
5915out:
5916 btrfs_free_path(path);
5917 return ret;
5918}