blob: e785f0a0632b79a1472b674f27ea3e7a74d1cb35 [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);
Chris Mason8f18cf12008-04-25 16:53:30 -0400358 if (shint && block_group_bits(shint, data) && !shint->ro) {
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 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400369 if (hint && !hint->ro && 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
Chris Mason8f18cf12008-04-25 16:53:30 -0400395 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400396 free_check = div_factor(cache->key.offset, factor);
Zheng Yane8569812008-09-26 10:05:48 -0400397 if (used + cache->pinned + cache->reserved <
398 free_check) {
Chris Mason8790d502008-04-03 16:29:03 -0400399 found_group = cache;
Chris Masonc286ac42008-07-22 23:06:41 -0400400 spin_unlock(&cache->lock);
Chris Mason8790d502008-04-03 16:29:03 -0400401 goto found;
402 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400403 }
Chris Masonc286ac42008-07-22 23:06:41 -0400404 spin_unlock(&cache->lock);
Chris Masonde428b62007-05-18 13:28:27 -0400405 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400406 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400407 if (!wrapped) {
408 last = search_start;
409 wrapped = 1;
410 goto again;
411 }
412 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400413 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400414 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400415 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400416 goto again;
417 }
Chris Masonbe744172007-05-06 10:15:01 -0400418found:
Chris Mason31f3c992007-04-30 15:25:45 -0400419 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400420}
421
Chris Mason925baed2008-06-25 16:01:30 -0400422struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
423 struct btrfs_block_group_cache
424 *hint, u64 search_start,
425 int data, int owner)
426{
427
428 struct btrfs_block_group_cache *ret;
Chris Mason925baed2008-06-25 16:01:30 -0400429 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
Chris Mason925baed2008-06-25 16:01:30 -0400430 return ret;
431}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400432
Chris Masone02119d2008-09-05 16:13:11 -0400433/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400434int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400435{
436 int ret;
437 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400438 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400439
Zheng Yan31840ae2008-09-23 13:14:14 -0400440 path = btrfs_alloc_path();
441 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400442 key.objectid = start;
443 key.offset = len;
444 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
445 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
446 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400447 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500448 return ret;
449}
450
Chris Masond8d5f3e2007-12-11 12:42:00 -0500451/*
452 * Back reference rules. Back refs have three main goals:
453 *
454 * 1) differentiate between all holders of references to an extent so that
455 * when a reference is dropped we can make sure it was a valid reference
456 * before freeing the extent.
457 *
458 * 2) Provide enough information to quickly find the holders of an extent
459 * if we notice a given block is corrupted or bad.
460 *
461 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
462 * maintenance. This is actually the same as #2, but with a slightly
463 * different use case.
464 *
465 * File extents can be referenced by:
466 *
467 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400468 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500469 * - different offsets inside a file (bookend extents in file.c)
470 *
471 * The extent ref structure has fields for:
472 *
473 * - Objectid of the subvolume root
474 * - Generation number of the tree holding the reference
475 * - objectid of the file holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400476 * - number of references holding by parent node (alway 1 for tree blocks)
477 *
478 * Btree leaf may hold multiple references to a file extent. In most cases,
479 * these references are from same file and the corresponding offsets inside
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400480 * the file are close together.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500481 *
482 * When a file extent is allocated the fields are filled in:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400483 * (root_key.objectid, trans->transid, inode objectid, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500484 *
485 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400486 * in the leaf. It looks similar to the create case, but trans->transid will
487 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500488 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400489 * (root_key.objectid, trans->transid, inode objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400490 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500491 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400492 * When a file extent is removed either during snapshot deletion or
493 * file truncation, we find the corresponding back reference and check
494 * the following fields:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500495 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400496 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
497 * inode objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500498 *
499 * Btree extents can be referenced by:
500 *
501 * - Different subvolumes
502 * - Different generations of the same subvolume
503 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500504 * When a tree block is created, back references are inserted:
505 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400506 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500507 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400508 * When a tree block is cow'd, new back references are added for all the
509 * blocks it points to. If the tree block isn't in reference counted root,
510 * the old back references are removed. These new back references are of
511 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500512 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400513 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500514 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400515 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500516 *
517 * if backref was for a tree root:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400518 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500519 * else
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400520 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500521 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400522 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500523 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400524 * The key objectid corresponds to the first byte in the extent, the key
525 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
526 * byte of parent extent. If a extent is tree root, the key offset is set
527 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500528 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400529
530static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
531 struct btrfs_root *root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400532 struct btrfs_path *path,
533 u64 bytenr, u64 parent,
534 u64 ref_root, u64 ref_generation,
535 u64 owner_objectid, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500536{
Chris Mason74493f72007-12-11 09:25:06 -0500537 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400538 struct btrfs_extent_ref *ref;
539 struct extent_buffer *leaf;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400540 u64 ref_objectid;
Chris Mason74493f72007-12-11 09:25:06 -0500541 int ret;
542
Chris Mason74493f72007-12-11 09:25:06 -0500543 key.objectid = bytenr;
544 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400545 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500546
Zheng Yan31840ae2008-09-23 13:14:14 -0400547 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
548 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500549 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400550 if (ret > 0) {
551 ret = -ENOENT;
552 goto out;
553 }
554
555 leaf = path->nodes[0];
556 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400557 ref_objectid = btrfs_ref_objectid(leaf, ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400558 if (btrfs_ref_root(leaf, ref) != ref_root ||
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400559 btrfs_ref_generation(leaf, ref) != ref_generation ||
560 (ref_objectid != owner_objectid &&
561 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400562 ret = -EIO;
563 WARN_ON(1);
564 goto out;
565 }
566 ret = 0;
567out:
568 return ret;
569}
570
Josef Bacikf3465ca2008-11-12 14:19:50 -0500571/*
572 * updates all the backrefs that are pending on update_list for the
573 * extent_root
574 */
575static int noinline update_backrefs(struct btrfs_trans_handle *trans,
576 struct btrfs_root *extent_root,
577 struct btrfs_path *path,
578 struct list_head *update_list)
579{
580 struct btrfs_key key;
581 struct btrfs_extent_ref *ref;
582 struct btrfs_fs_info *info = extent_root->fs_info;
583 struct pending_extent_op *op;
584 struct extent_buffer *leaf;
585 int ret = 0;
586 struct list_head *cur = update_list->next;
587 u64 ref_objectid;
588 u64 ref_root = extent_root->root_key.objectid;
589
590 op = list_entry(cur, struct pending_extent_op, list);
591
592search:
593 key.objectid = op->bytenr;
594 key.type = BTRFS_EXTENT_REF_KEY;
595 key.offset = op->orig_parent;
596
597 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
598 BUG_ON(ret);
599
600 leaf = path->nodes[0];
601
602loop:
603 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
604
605 ref_objectid = btrfs_ref_objectid(leaf, ref);
606
607 if (btrfs_ref_root(leaf, ref) != ref_root ||
608 btrfs_ref_generation(leaf, ref) != op->orig_generation ||
609 (ref_objectid != op->level &&
610 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
611 printk(KERN_ERR "couldn't find %Lu, parent %Lu, root %Lu, "
612 "owner %u\n", op->bytenr, op->orig_parent,
613 ref_root, op->level);
614 btrfs_print_leaf(extent_root, leaf);
615 BUG();
616 }
617
618 key.objectid = op->bytenr;
619 key.offset = op->parent;
620 key.type = BTRFS_EXTENT_REF_KEY;
621 ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
622 BUG_ON(ret);
623 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
624 btrfs_set_ref_generation(leaf, ref, op->generation);
625
626 cur = cur->next;
627
628 list_del_init(&op->list);
629 unlock_extent(&info->extent_ins, op->bytenr,
630 op->bytenr + op->num_bytes - 1, GFP_NOFS);
631 kfree(op);
632
633 if (cur == update_list) {
634 btrfs_mark_buffer_dirty(path->nodes[0]);
635 btrfs_release_path(extent_root, path);
636 goto out;
637 }
638
639 op = list_entry(cur, struct pending_extent_op, list);
640
641 path->slots[0]++;
642 while (path->slots[0] < btrfs_header_nritems(leaf)) {
643 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
644 if (key.objectid == op->bytenr &&
645 key.type == BTRFS_EXTENT_REF_KEY)
646 goto loop;
647 path->slots[0]++;
648 }
649
650 btrfs_mark_buffer_dirty(path->nodes[0]);
651 btrfs_release_path(extent_root, path);
652 goto search;
653
654out:
655 return 0;
656}
657
658static int noinline insert_extents(struct btrfs_trans_handle *trans,
659 struct btrfs_root *extent_root,
660 struct btrfs_path *path,
661 struct list_head *insert_list, int nr)
662{
663 struct btrfs_key *keys;
664 u32 *data_size;
665 struct pending_extent_op *op;
666 struct extent_buffer *leaf;
667 struct list_head *cur = insert_list->next;
668 struct btrfs_fs_info *info = extent_root->fs_info;
669 u64 ref_root = extent_root->root_key.objectid;
670 int i = 0, last = 0, ret;
671 int total = nr * 2;
672
673 if (!nr)
674 return 0;
675
676 keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
677 if (!keys)
678 return -ENOMEM;
679
680 data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
681 if (!data_size) {
682 kfree(keys);
683 return -ENOMEM;
684 }
685
686 list_for_each_entry(op, insert_list, list) {
687 keys[i].objectid = op->bytenr;
688 keys[i].offset = op->num_bytes;
689 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
690 data_size[i] = sizeof(struct btrfs_extent_item);
691 i++;
692
693 keys[i].objectid = op->bytenr;
694 keys[i].offset = op->parent;
695 keys[i].type = BTRFS_EXTENT_REF_KEY;
696 data_size[i] = sizeof(struct btrfs_extent_ref);
697 i++;
698 }
699
700 op = list_entry(cur, struct pending_extent_op, list);
701 i = 0;
702 while (i < total) {
703 int c;
704 ret = btrfs_insert_some_items(trans, extent_root, path,
705 keys+i, data_size+i, total-i);
706 BUG_ON(ret < 0);
707
708 if (last && ret > 1)
709 BUG();
710
711 leaf = path->nodes[0];
712 for (c = 0; c < ret; c++) {
713 int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
714
715 /*
716 * if the first item we inserted was a backref, then
717 * the EXTENT_ITEM will be the odd c's, else it will
718 * be the even c's
719 */
720 if ((ref_first && (c % 2)) ||
721 (!ref_first && !(c % 2))) {
722 struct btrfs_extent_item *itm;
723
724 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
725 struct btrfs_extent_item);
726 btrfs_set_extent_refs(path->nodes[0], itm, 1);
727 op->del++;
728 } else {
729 struct btrfs_extent_ref *ref;
730
731 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
732 struct btrfs_extent_ref);
733 btrfs_set_ref_root(leaf, ref, ref_root);
734 btrfs_set_ref_generation(leaf, ref,
735 op->generation);
736 btrfs_set_ref_objectid(leaf, ref, op->level);
737 btrfs_set_ref_num_refs(leaf, ref, 1);
738 op->del++;
739 }
740
741 /*
742 * using del to see when its ok to free up the
743 * pending_extent_op. In the case where we insert the
744 * last item on the list in order to help do batching
745 * we need to not free the extent op until we actually
746 * insert the extent_item
747 */
748 if (op->del == 2) {
749 unlock_extent(&info->extent_ins, op->bytenr,
750 op->bytenr + op->num_bytes - 1,
751 GFP_NOFS);
752 cur = cur->next;
753 list_del_init(&op->list);
754 kfree(op);
755 if (cur != insert_list)
756 op = list_entry(cur,
757 struct pending_extent_op,
758 list);
759 }
760 }
761 btrfs_mark_buffer_dirty(leaf);
762 btrfs_release_path(extent_root, path);
763
764 /*
765 * Ok backref's and items usually go right next to eachother,
766 * but if we could only insert 1 item that means that we
767 * inserted on the end of a leaf, and we have no idea what may
768 * be on the next leaf so we just play it safe. In order to
769 * try and help this case we insert the last thing on our
770 * insert list so hopefully it will end up being the last
771 * thing on the leaf and everything else will be before it,
772 * which will let us insert a whole bunch of items at the same
773 * time.
774 */
775 if (ret == 1 && !last && (i + ret < total)) {
776 /*
777 * last: where we will pick up the next time around
778 * i: our current key to insert, will be total - 1
779 * cur: the current op we are screwing with
780 * op: duh
781 */
782 last = i + ret;
783 i = total - 1;
784 cur = insert_list->prev;
785 op = list_entry(cur, struct pending_extent_op, list);
786 } else if (last) {
787 /*
788 * ok we successfully inserted the last item on the
789 * list, lets reset everything
790 *
791 * i: our current key to insert, so where we left off
792 * last time
793 * last: done with this
794 * cur: the op we are messing with
795 * op: duh
796 * total: since we inserted the last key, we need to
797 * decrement total so we dont overflow
798 */
799 i = last;
800 last = 0;
801 cur = insert_list->next;
802 op = list_entry(cur, struct pending_extent_op, list);
803 total--;
804 } else {
805 i += ret;
806 }
807
808 cond_resched();
809 }
810 ret = 0;
811 kfree(keys);
812 kfree(data_size);
813 return ret;
814}
815
Zheng Yan31840ae2008-09-23 13:14:14 -0400816static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
817 struct btrfs_root *root,
818 struct btrfs_path *path,
819 u64 bytenr, u64 parent,
820 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400821 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400822{
823 struct btrfs_key key;
824 struct extent_buffer *leaf;
825 struct btrfs_extent_ref *ref;
826 u32 num_refs;
827 int ret;
828
829 key.objectid = bytenr;
830 key.type = BTRFS_EXTENT_REF_KEY;
831 key.offset = parent;
832
833 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
834 if (ret == 0) {
835 leaf = path->nodes[0];
836 ref = btrfs_item_ptr(leaf, path->slots[0],
837 struct btrfs_extent_ref);
838 btrfs_set_ref_root(leaf, ref, ref_root);
839 btrfs_set_ref_generation(leaf, ref, ref_generation);
840 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400841 btrfs_set_ref_num_refs(leaf, ref, 1);
842 } else if (ret == -EEXIST) {
843 u64 existing_owner;
844 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
845 leaf = path->nodes[0];
846 ref = btrfs_item_ptr(leaf, path->slots[0],
847 struct btrfs_extent_ref);
848 if (btrfs_ref_root(leaf, ref) != ref_root ||
849 btrfs_ref_generation(leaf, ref) != ref_generation) {
850 ret = -EIO;
851 WARN_ON(1);
852 goto out;
853 }
854
855 num_refs = btrfs_ref_num_refs(leaf, ref);
856 BUG_ON(num_refs == 0);
857 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
858
859 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400860 if (existing_owner != owner_objectid &&
861 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400862 btrfs_set_ref_objectid(leaf, ref,
863 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400864 }
865 ret = 0;
866 } else {
867 goto out;
868 }
Chris Mason7bb86312007-12-11 09:25:06 -0500869 btrfs_mark_buffer_dirty(path->nodes[0]);
870out:
871 btrfs_release_path(root, path);
872 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500873}
874
Zheng Yan31840ae2008-09-23 13:14:14 -0400875static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
876 struct btrfs_root *root,
877 struct btrfs_path *path)
878{
879 struct extent_buffer *leaf;
880 struct btrfs_extent_ref *ref;
881 u32 num_refs;
882 int ret = 0;
883
884 leaf = path->nodes[0];
885 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
886 num_refs = btrfs_ref_num_refs(leaf, ref);
887 BUG_ON(num_refs == 0);
888 num_refs -= 1;
889 if (num_refs == 0) {
890 ret = btrfs_del_item(trans, root, path);
891 } else {
892 btrfs_set_ref_num_refs(leaf, ref, num_refs);
893 btrfs_mark_buffer_dirty(leaf);
894 }
895 btrfs_release_path(root, path);
896 return ret;
897}
898
Josef Bacikf3465ca2008-11-12 14:19:50 -0500899static int noinline free_extents(struct btrfs_trans_handle *trans,
900 struct btrfs_root *extent_root,
901 struct list_head *del_list)
902{
903 struct btrfs_fs_info *info = extent_root->fs_info;
904 struct btrfs_path *path;
905 struct btrfs_key key, found_key;
906 struct extent_buffer *leaf;
907 struct list_head *cur;
908 struct pending_extent_op *op;
909 struct btrfs_extent_item *ei;
910 int ret, num_to_del, extent_slot = 0, found_extent = 0;
911 u32 refs;
912 u64 bytes_freed = 0;
913
914 path = btrfs_alloc_path();
915 if (!path)
916 return -ENOMEM;
917 path->reada = 1;
918
919search:
920 /* search for the backref for the current ref we want to delete */
921 cur = del_list->next;
922 op = list_entry(cur, struct pending_extent_op, list);
923 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
924 op->orig_parent,
925 extent_root->root_key.objectid,
926 op->orig_generation, op->level, 1);
927 if (ret) {
928 printk("Unable to find backref byte nr %Lu root %Lu gen %Lu "
929 "owner %u\n", op->bytenr,
930 extent_root->root_key.objectid, op->orig_generation,
931 op->level);
932 btrfs_print_leaf(extent_root, path->nodes[0]);
933 WARN_ON(1);
934 goto out;
935 }
936
937 extent_slot = path->slots[0];
938 num_to_del = 1;
939 found_extent = 0;
940
941 /*
942 * if we aren't the first item on the leaf we can move back one and see
943 * if our ref is right next to our extent item
944 */
945 if (likely(extent_slot)) {
946 extent_slot--;
947 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
948 extent_slot);
949 if (found_key.objectid == op->bytenr &&
950 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
951 found_key.offset == op->num_bytes) {
952 num_to_del++;
953 found_extent = 1;
954 }
955 }
956
957 /*
958 * if we didn't find the extent we need to delete the backref and then
959 * search for the extent item key so we can update its ref count
960 */
961 if (!found_extent) {
962 key.objectid = op->bytenr;
963 key.type = BTRFS_EXTENT_ITEM_KEY;
964 key.offset = op->num_bytes;
965
966 ret = remove_extent_backref(trans, extent_root, path);
967 BUG_ON(ret);
968 btrfs_release_path(extent_root, path);
969 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
970 BUG_ON(ret);
971 extent_slot = path->slots[0];
972 }
973
974 /* this is where we update the ref count for the extent */
975 leaf = path->nodes[0];
976 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
977 refs = btrfs_extent_refs(leaf, ei);
978 BUG_ON(refs == 0);
979 refs--;
980 btrfs_set_extent_refs(leaf, ei, refs);
981
982 btrfs_mark_buffer_dirty(leaf);
983
984 /*
985 * This extent needs deleting. The reason cur_slot is extent_slot +
986 * num_to_del is because extent_slot points to the slot where the extent
987 * is, and if the backref was not right next to the extent we will be
988 * deleting at least 1 item, and will want to start searching at the
989 * slot directly next to extent_slot. However if we did find the
990 * backref next to the extent item them we will be deleting at least 2
991 * items and will want to start searching directly after the ref slot
992 */
993 if (!refs) {
994 struct list_head *pos, *n, *end;
995 int cur_slot = extent_slot+num_to_del;
996 u64 super_used;
997 u64 root_used;
998
999 path->slots[0] = extent_slot;
1000 bytes_freed = op->num_bytes;
1001
1002 /*
1003 * we need to see if we can delete multiple things at once, so
1004 * start looping through the list of extents we are wanting to
1005 * delete and see if their extent/backref's are right next to
1006 * eachother and the extents only have 1 ref
1007 */
1008 for (pos = cur->next; pos != del_list; pos = pos->next) {
1009 struct pending_extent_op *tmp;
1010
1011 tmp = list_entry(pos, struct pending_extent_op, list);
1012
1013 /* we only want to delete extent+ref at this stage */
1014 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1015 break;
1016
1017 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1018 if (found_key.objectid != tmp->bytenr ||
1019 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1020 found_key.offset != tmp->num_bytes)
1021 break;
1022
1023 /* check to make sure this extent only has one ref */
1024 ei = btrfs_item_ptr(leaf, cur_slot,
1025 struct btrfs_extent_item);
1026 if (btrfs_extent_refs(leaf, ei) != 1)
1027 break;
1028
1029 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1030 if (found_key.objectid != tmp->bytenr ||
1031 found_key.type != BTRFS_EXTENT_REF_KEY ||
1032 found_key.offset != tmp->orig_parent)
1033 break;
1034
1035 /*
1036 * the ref is right next to the extent, we can set the
1037 * ref count to 0 since we will delete them both now
1038 */
1039 btrfs_set_extent_refs(leaf, ei, 0);
1040
1041 /* pin down the bytes for this extent */
1042 mutex_lock(&info->pinned_mutex);
1043 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1044 tmp->num_bytes, tmp->level >=
1045 BTRFS_FIRST_FREE_OBJECTID);
1046 mutex_unlock(&info->pinned_mutex);
1047 BUG_ON(ret < 0);
1048
1049 /*
1050 * use the del field to tell if we need to go ahead and
1051 * free up the extent when we delete the item or not.
1052 */
1053 tmp->del = ret;
1054 bytes_freed += tmp->num_bytes;
1055
1056 num_to_del += 2;
1057 cur_slot += 2;
1058 }
1059 end = pos;
1060
1061 /* update the free space counters */
1062 spin_lock_irq(&info->delalloc_lock);
1063 super_used = btrfs_super_bytes_used(&info->super_copy);
1064 btrfs_set_super_bytes_used(&info->super_copy,
1065 super_used - bytes_freed);
1066 spin_unlock_irq(&info->delalloc_lock);
1067
1068 root_used = btrfs_root_used(&extent_root->root_item);
1069 btrfs_set_root_used(&extent_root->root_item,
1070 root_used - bytes_freed);
1071
1072 /* delete the items */
1073 ret = btrfs_del_items(trans, extent_root, path,
1074 path->slots[0], num_to_del);
1075 BUG_ON(ret);
1076
1077 /*
1078 * loop through the extents we deleted and do the cleanup work
1079 * on them
1080 */
1081 for (pos = cur, n = pos->next; pos != end;
1082 pos = n, n = pos->next) {
1083 struct pending_extent_op *tmp;
1084#ifdef BIO_RW_DISCARD
1085 u64 map_length;
1086 struct btrfs_multi_bio *multi = NULL;
1087#endif
1088 tmp = list_entry(pos, struct pending_extent_op, list);
1089
1090 /*
1091 * remember tmp->del tells us wether or not we pinned
1092 * down the extent
1093 */
1094 ret = update_block_group(trans, extent_root,
1095 tmp->bytenr, tmp->num_bytes, 0,
1096 tmp->del);
1097 BUG_ON(ret);
1098
1099#ifdef BIO_RW_DISCARD
1100 ret = btrfs_map_block(&info->mapping_tree, READ,
1101 tmp->bytenr, &map_length, &multi,
1102 0);
1103 if (!ret) {
1104 struct btrfs_bio_stripe *stripe;
1105 int i;
1106
1107 stripe = multi->stripe;
1108
1109 if (map_length > tmp->num_bytes)
1110 map_length = tmp->num_bytes;
1111
1112 for (i = 0; i < multi->num_stripes;
1113 i++, stripe++)
1114 blkdev_issue_discard(stripe->dev->bdev,
1115 stripe->physical >> 9,
1116 map_length >> 9);
1117 kfree(multi);
1118 }
1119#endif
1120 list_del_init(&tmp->list);
1121 unlock_extent(&info->extent_ins, tmp->bytenr,
1122 tmp->bytenr + tmp->num_bytes - 1,
1123 GFP_NOFS);
1124 kfree(tmp);
1125 }
1126 } else if (refs && found_extent) {
1127 /*
1128 * the ref and extent were right next to eachother, but the
1129 * extent still has a ref, so just free the backref and keep
1130 * going
1131 */
1132 ret = remove_extent_backref(trans, extent_root, path);
1133 BUG_ON(ret);
1134
1135 list_del_init(&op->list);
1136 unlock_extent(&info->extent_ins, op->bytenr,
1137 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1138 kfree(op);
1139 } else {
1140 /*
1141 * the extent has multiple refs and the backref we were looking
1142 * for was not right next to it, so just unlock and go next,
1143 * we're good to go
1144 */
1145 list_del_init(&op->list);
1146 unlock_extent(&info->extent_ins, op->bytenr,
1147 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1148 kfree(op);
1149 }
1150
1151 btrfs_release_path(extent_root, path);
1152 if (!list_empty(del_list))
1153 goto search;
1154
1155out:
1156 btrfs_free_path(path);
1157 return ret;
1158}
1159
Zheng Yan31840ae2008-09-23 13:14:14 -04001160static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1161 struct btrfs_root *root, u64 bytenr,
1162 u64 orig_parent, u64 parent,
1163 u64 orig_root, u64 ref_root,
1164 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001165 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001166{
1167 int ret;
1168 struct btrfs_root *extent_root = root->fs_info->extent_root;
1169 struct btrfs_path *path;
1170
1171 if (root == root->fs_info->extent_root) {
1172 struct pending_extent_op *extent_op;
1173 u64 num_bytes;
1174
1175 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1176 num_bytes = btrfs_level_size(root, (int)owner_objectid);
Josef Bacik25179202008-10-29 14:49:05 -04001177 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001178 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
Josef Bacik25179202008-10-29 14:49:05 -04001179 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001180 u64 priv;
1181 ret = get_state_private(&root->fs_info->extent_ins,
1182 bytenr, &priv);
1183 BUG_ON(ret);
1184 extent_op = (struct pending_extent_op *)
1185 (unsigned long)priv;
1186 BUG_ON(extent_op->parent != orig_parent);
1187 BUG_ON(extent_op->generation != orig_generation);
Josef Bacik25179202008-10-29 14:49:05 -04001188
Zheng Yan31840ae2008-09-23 13:14:14 -04001189 extent_op->parent = parent;
1190 extent_op->generation = ref_generation;
1191 } else {
1192 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1193 BUG_ON(!extent_op);
1194
1195 extent_op->type = PENDING_BACKREF_UPDATE;
1196 extent_op->bytenr = bytenr;
1197 extent_op->num_bytes = num_bytes;
1198 extent_op->parent = parent;
1199 extent_op->orig_parent = orig_parent;
1200 extent_op->generation = ref_generation;
1201 extent_op->orig_generation = orig_generation;
1202 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001203 INIT_LIST_HEAD(&extent_op->list);
1204 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001205
1206 set_extent_bits(&root->fs_info->extent_ins,
1207 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04001208 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04001209 set_state_private(&root->fs_info->extent_ins,
1210 bytenr, (unsigned long)extent_op);
1211 }
Josef Bacik25179202008-10-29 14:49:05 -04001212 mutex_unlock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001213 return 0;
1214 }
1215
1216 path = btrfs_alloc_path();
1217 if (!path)
1218 return -ENOMEM;
1219 ret = lookup_extent_backref(trans, extent_root, path,
1220 bytenr, orig_parent, orig_root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001221 orig_generation, owner_objectid, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001222 if (ret)
1223 goto out;
1224 ret = remove_extent_backref(trans, extent_root, path);
1225 if (ret)
1226 goto out;
1227 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1228 parent, ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001229 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001230 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001231 finish_current_insert(trans, extent_root, 0);
1232 del_pending_extents(trans, extent_root, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001233out:
1234 btrfs_free_path(path);
1235 return ret;
1236}
1237
1238int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1239 struct btrfs_root *root, u64 bytenr,
1240 u64 orig_parent, u64 parent,
1241 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001242 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001243{
1244 int ret;
1245 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1246 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1247 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001248 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1249 parent, ref_root, ref_root,
1250 ref_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001251 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001252 return ret;
1253}
1254
Chris Mason925baed2008-06-25 16:01:30 -04001255static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001256 struct btrfs_root *root, u64 bytenr,
1257 u64 orig_parent, u64 parent,
1258 u64 orig_root, u64 ref_root,
1259 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001260 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -05001261{
Chris Mason5caf2a02007-04-02 11:20:42 -04001262 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -05001263 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001264 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001265 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001266 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001267 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001268
Chris Mason5caf2a02007-04-02 11:20:42 -04001269 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001270 if (!path)
1271 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001272
Chris Mason3c12ac72008-04-21 12:01:38 -04001273 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001274 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001275 key.type = BTRFS_EXTENT_ITEM_KEY;
1276 key.offset = (u64)-1;
1277
Chris Mason5caf2a02007-04-02 11:20:42 -04001278 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001279 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001280 if (ret < 0)
1281 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001282 BUG_ON(ret == 0 || path->slots[0] == 0);
1283
1284 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -04001285 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -04001286
1287 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Mason771ed682008-11-06 22:02:51 -05001288 if (key.objectid != bytenr) {
1289 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
1290 printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
1291 BUG();
1292 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001293 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1294
Chris Mason5caf2a02007-04-02 11:20:42 -04001295 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001296 refs = btrfs_extent_refs(l, item);
1297 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001298 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -05001299
Chris Mason5caf2a02007-04-02 11:20:42 -04001300 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001301
Chris Mason3c12ac72008-04-21 12:01:38 -04001302 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001303 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1304 path, bytenr, parent,
1305 ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001306 owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05001307 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001308 finish_current_insert(trans, root->fs_info->extent_root, 0);
1309 del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Mason74493f72007-12-11 09:25:06 -05001310
1311 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -05001312 return 0;
1313}
1314
Chris Mason925baed2008-06-25 16:01:30 -04001315int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001316 struct btrfs_root *root,
1317 u64 bytenr, u64 num_bytes, u64 parent,
1318 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001319 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -04001320{
1321 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001322 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1323 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1324 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001325 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1326 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001327 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -04001328 return ret;
1329}
1330
Chris Masone9d0b132007-08-10 14:06:19 -04001331int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1332 struct btrfs_root *root)
1333{
Chris Mason87ef2bb2008-10-30 11:23:27 -04001334 finish_current_insert(trans, root->fs_info->extent_root, 1);
1335 del_pending_extents(trans, root->fs_info->extent_root, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001336 return 0;
1337}
1338
Zheng Yan31840ae2008-09-23 13:14:14 -04001339int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1340 struct btrfs_root *root, u64 bytenr,
1341 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -05001342{
Chris Mason5caf2a02007-04-02 11:20:42 -04001343 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -05001344 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001345 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001346 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001347 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -04001348
Chris Masondb945352007-10-15 16:15:53 -04001349 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -04001350 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -04001351 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001352 key.objectid = bytenr;
1353 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -04001354 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -04001355 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001356 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001357 if (ret < 0)
1358 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -04001359 if (ret != 0) {
1360 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -04001361 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -05001362 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04001363 }
1364 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001365 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001366 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001367out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001368 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05001369 return 0;
1370}
1371
Yan Zheng80ff3852008-10-30 14:20:02 -04001372int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1373 struct btrfs_root *root, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05001374{
1375 struct btrfs_root *extent_root = root->fs_info->extent_root;
1376 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -04001377 struct extent_buffer *leaf;
1378 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -05001379 struct btrfs_key key;
1380 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 u64 ref_root;
1382 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -04001383 u32 nritems;
1384 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001385
Chris Masonbe20aa92007-12-17 20:14:01 -05001386 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001387 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04001388 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05001389
Yan Zhengf321e492008-07-30 09:26:11 -04001390 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -05001391 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1392 if (ret < 0)
1393 goto out;
1394 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001395
1396 ret = -ENOENT;
1397 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04001398 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001399
Zheng Yan31840ae2008-09-23 13:14:14 -04001400 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04001401 leaf = path->nodes[0];
1402 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001403
1404 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001405 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05001406 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001407
Yan Zheng80ff3852008-10-30 14:20:02 -04001408 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05001409 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -04001410 leaf = path->nodes[0];
1411 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -05001412 if (path->slots[0] >= nritems) {
1413 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -04001414 if (ret < 0)
1415 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001416 if (ret == 0)
1417 continue;
1418 break;
1419 }
Yan Zhengf321e492008-07-30 09:26:11 -04001420 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001421 if (found_key.objectid != bytenr)
1422 break;
Chris Masonbd098352008-01-03 13:23:19 -05001423
Chris Masonbe20aa92007-12-17 20:14:01 -05001424 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1425 path->slots[0]++;
1426 continue;
1427 }
1428
Yan Zhengf321e492008-07-30 09:26:11 -04001429 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001430 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -04001431 ref_root = btrfs_ref_root(leaf, ref_item);
1432 if (ref_root != root->root_key.objectid &&
1433 ref_root != BTRFS_TREE_LOG_OBJECTID) {
1434 ret = 1;
1435 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -04001436 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1438 ret = 1;
1439 goto out;
1440 }
Yan Zhengf321e492008-07-30 09:26:11 -04001441
Chris Masonbe20aa92007-12-17 20:14:01 -05001442 path->slots[0]++;
1443 }
Yan Zhengf321e492008-07-30 09:26:11 -04001444 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001445out:
Yan Zhengf321e492008-07-30 09:26:11 -04001446 btrfs_free_path(path);
1447 return ret;
1448}
1449
Zheng Yan31840ae2008-09-23 13:14:14 -04001450int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1451 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001452{
Chris Mason5f39d392007-10-15 16:14:19 -04001453 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001454 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001455 u64 root_gen;
1456 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001457 int i;
Chris Masondb945352007-10-15 16:15:53 -04001458 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001459 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001460 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001461
Chris Mason3768f362007-03-13 16:47:54 -04001462 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001463 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001464
Zheng Yane4657682008-09-26 10:04:53 -04001465 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1466 shared = 0;
1467 root_gen = root->root_key.offset;
1468 } else {
1469 shared = 1;
1470 root_gen = trans->transid - 1;
1471 }
1472
Chris Masondb945352007-10-15 16:15:53 -04001473 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001474 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001475
Zheng Yan31840ae2008-09-23 13:14:14 -04001476 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001477 struct btrfs_leaf_ref *ref;
1478 struct btrfs_extent_info *info;
1479
Zheng Yan31840ae2008-09-23 13:14:14 -04001480 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001481 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001482 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001483 goto out;
1484 }
1485
Zheng Yane4657682008-09-26 10:04:53 -04001486 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001487 ref->bytenr = buf->start;
1488 ref->owner = btrfs_header_owner(buf);
1489 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001490 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001491 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001492
Zheng Yan31840ae2008-09-23 13:14:14 -04001493 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001494 u64 disk_bytenr;
1495 btrfs_item_key_to_cpu(buf, &key, i);
1496 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1497 continue;
1498 fi = btrfs_item_ptr(buf, i,
1499 struct btrfs_file_extent_item);
1500 if (btrfs_file_extent_type(buf, fi) ==
1501 BTRFS_FILE_EXTENT_INLINE)
1502 continue;
1503 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1504 if (disk_bytenr == 0)
1505 continue;
1506
1507 info->bytenr = disk_bytenr;
1508 info->num_bytes =
1509 btrfs_file_extent_disk_num_bytes(buf, fi);
1510 info->objectid = key.objectid;
1511 info->offset = key.offset;
1512 info++;
1513 }
1514
Zheng Yane4657682008-09-26 10:04:53 -04001515 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001516 if (ret == -EEXIST && shared) {
1517 struct btrfs_leaf_ref *old;
1518 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1519 BUG_ON(!old);
1520 btrfs_remove_leaf_ref(root, old);
1521 btrfs_free_leaf_ref(root, old);
1522 ret = btrfs_add_leaf_ref(root, ref, shared);
1523 }
Yan Zheng31153d82008-07-28 15:32:19 -04001524 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001525 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001526 }
1527out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001528 return ret;
1529}
1530
1531int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1532 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1533 u32 *nr_extents)
1534{
1535 u64 bytenr;
1536 u64 ref_root;
1537 u64 orig_root;
1538 u64 ref_generation;
1539 u64 orig_generation;
1540 u32 nritems;
1541 u32 nr_file_extents = 0;
1542 struct btrfs_key key;
1543 struct btrfs_file_extent_item *fi;
1544 int i;
1545 int level;
1546 int ret = 0;
1547 int faili = 0;
1548 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001549 u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001550
1551 ref_root = btrfs_header_owner(buf);
1552 ref_generation = btrfs_header_generation(buf);
1553 orig_root = btrfs_header_owner(orig_buf);
1554 orig_generation = btrfs_header_generation(orig_buf);
1555
1556 nritems = btrfs_header_nritems(buf);
1557 level = btrfs_header_level(buf);
1558
1559 if (root->ref_cows) {
1560 process_func = __btrfs_inc_extent_ref;
1561 } else {
1562 if (level == 0 &&
1563 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1564 goto out;
1565 if (level != 0 &&
1566 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1567 goto out;
1568 process_func = __btrfs_update_extent_ref;
1569 }
1570
1571 for (i = 0; i < nritems; i++) {
1572 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001573 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001574 btrfs_item_key_to_cpu(buf, &key, i);
1575 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001576 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001577 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001578 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001579 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001580 BTRFS_FILE_EXTENT_INLINE)
1581 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001582 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1583 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001584 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001585
1586 nr_file_extents++;
1587
Zheng Yan31840ae2008-09-23 13:14:14 -04001588 ret = process_func(trans, root, bytenr,
1589 orig_buf->start, buf->start,
1590 orig_root, ref_root,
1591 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001592 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001593
1594 if (ret) {
1595 faili = i;
1596 WARN_ON(1);
1597 goto fail;
1598 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001599 } else {
Chris Masondb945352007-10-15 16:15:53 -04001600 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001601 ret = process_func(trans, root, bytenr,
1602 orig_buf->start, buf->start,
1603 orig_root, ref_root,
1604 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001605 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001606 if (ret) {
1607 faili = i;
1608 WARN_ON(1);
1609 goto fail;
1610 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001611 }
1612 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001613out:
1614 if (nr_extents) {
1615 if (level == 0)
1616 *nr_extents = nr_file_extents;
1617 else
1618 *nr_extents = nritems;
1619 }
1620 return 0;
1621fail:
1622 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001623 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001624}
1625
Zheng Yan31840ae2008-09-23 13:14:14 -04001626int btrfs_update_ref(struct btrfs_trans_handle *trans,
1627 struct btrfs_root *root, struct extent_buffer *orig_buf,
1628 struct extent_buffer *buf, int start_slot, int nr)
1629
1630{
1631 u64 bytenr;
1632 u64 ref_root;
1633 u64 orig_root;
1634 u64 ref_generation;
1635 u64 orig_generation;
1636 struct btrfs_key key;
1637 struct btrfs_file_extent_item *fi;
1638 int i;
1639 int ret;
1640 int slot;
1641 int level;
1642
1643 BUG_ON(start_slot < 0);
1644 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1645
1646 ref_root = btrfs_header_owner(buf);
1647 ref_generation = btrfs_header_generation(buf);
1648 orig_root = btrfs_header_owner(orig_buf);
1649 orig_generation = btrfs_header_generation(orig_buf);
1650 level = btrfs_header_level(buf);
1651
1652 if (!root->ref_cows) {
1653 if (level == 0 &&
1654 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1655 return 0;
1656 if (level != 0 &&
1657 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1658 return 0;
1659 }
1660
1661 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1662 cond_resched();
1663 if (level == 0) {
1664 btrfs_item_key_to_cpu(buf, &key, slot);
1665 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1666 continue;
1667 fi = btrfs_item_ptr(buf, slot,
1668 struct btrfs_file_extent_item);
1669 if (btrfs_file_extent_type(buf, fi) ==
1670 BTRFS_FILE_EXTENT_INLINE)
1671 continue;
1672 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1673 if (bytenr == 0)
1674 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001675 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1676 orig_buf->start, buf->start,
1677 orig_root, ref_root,
1678 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001679 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001680 if (ret)
1681 goto fail;
1682 } else {
1683 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001684 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1685 orig_buf->start, buf->start,
1686 orig_root, ref_root,
1687 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001688 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001689 if (ret)
1690 goto fail;
1691 }
1692 }
1693 return 0;
1694fail:
1695 WARN_ON(1);
1696 return -1;
1697}
1698
Chris Mason9078a3e2007-04-26 16:46:15 -04001699static int write_one_cache_group(struct btrfs_trans_handle *trans,
1700 struct btrfs_root *root,
1701 struct btrfs_path *path,
1702 struct btrfs_block_group_cache *cache)
1703{
1704 int ret;
1705 int pending_ret;
1706 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001707 unsigned long bi;
1708 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001709
Chris Mason9078a3e2007-04-26 16:46:15 -04001710 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001711 if (ret < 0)
1712 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001713 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001714
1715 leaf = path->nodes[0];
1716 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1717 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1718 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001719 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001720fail:
Chris Mason87ef2bb2008-10-30 11:23:27 -04001721 finish_current_insert(trans, extent_root, 0);
1722 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001723 if (ret)
1724 return ret;
1725 if (pending_ret)
1726 return pending_ret;
1727 return 0;
1728
1729}
1730
Chris Mason96b51792007-10-15 16:15:19 -04001731int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1732 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001733{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001734 struct btrfs_block_group_cache *cache, *entry;
1735 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001736 int err = 0;
1737 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001738 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001739 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001740
1741 path = btrfs_alloc_path();
1742 if (!path)
1743 return -ENOMEM;
1744
1745 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001746 cache = NULL;
1747 spin_lock(&root->fs_info->block_group_cache_lock);
1748 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1749 n; n = rb_next(n)) {
1750 entry = rb_entry(n, struct btrfs_block_group_cache,
1751 cache_node);
1752 if (entry->dirty) {
1753 cache = entry;
1754 break;
1755 }
1756 }
1757 spin_unlock(&root->fs_info->block_group_cache_lock);
1758
1759 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001760 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001761
Zheng Yane8569812008-09-26 10:05:48 -04001762 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001763 last += cache->key.offset;
1764
Chris Mason96b51792007-10-15 16:15:19 -04001765 err = write_one_cache_group(trans, root,
1766 path, cache);
1767 /*
1768 * if we fail to write the cache group, we want
1769 * to keep it marked dirty in hopes that a later
1770 * write will work
1771 */
1772 if (err) {
1773 werr = err;
1774 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001775 }
1776 }
1777 btrfs_free_path(path);
1778 return werr;
1779}
1780
Chris Mason593060d2008-03-25 16:50:33 -04001781static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1782 u64 total_bytes, u64 bytes_used,
1783 struct btrfs_space_info **space_info)
1784{
1785 struct btrfs_space_info *found;
1786
1787 found = __find_space_info(info, flags);
1788 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001789 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001790 found->total_bytes += total_bytes;
1791 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001792 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001793 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001794 *space_info = found;
1795 return 0;
1796 }
1797 found = kmalloc(sizeof(*found), GFP_NOFS);
1798 if (!found)
1799 return -ENOMEM;
1800
1801 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001802 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001803 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001804 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001805 found->flags = flags;
1806 found->total_bytes = total_bytes;
1807 found->bytes_used = bytes_used;
1808 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001809 found->bytes_reserved = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001810 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001811 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001812 *space_info = found;
1813 return 0;
1814}
1815
Chris Mason8790d502008-04-03 16:29:03 -04001816static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1817{
1818 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001819 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001820 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001821 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001822 if (extra_flags) {
1823 if (flags & BTRFS_BLOCK_GROUP_DATA)
1824 fs_info->avail_data_alloc_bits |= extra_flags;
1825 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1826 fs_info->avail_metadata_alloc_bits |= extra_flags;
1827 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1828 fs_info->avail_system_alloc_bits |= extra_flags;
1829 }
1830}
Chris Mason593060d2008-03-25 16:50:33 -04001831
Chris Masona061fc82008-05-07 11:43:44 -04001832static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001833{
Chris Masona061fc82008-05-07 11:43:44 -04001834 u64 num_devices = root->fs_info->fs_devices->num_devices;
1835
1836 if (num_devices == 1)
1837 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1838 if (num_devices < 4)
1839 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1840
Chris Masonec44a352008-04-28 15:29:52 -04001841 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1842 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001843 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001844 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001845 }
Chris Masonec44a352008-04-28 15:29:52 -04001846
1847 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001848 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001849 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001850 }
Chris Masonec44a352008-04-28 15:29:52 -04001851
1852 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1853 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1854 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1855 (flags & BTRFS_BLOCK_GROUP_DUP)))
1856 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1857 return flags;
1858}
1859
Chris Mason6324fbf2008-03-24 15:01:59 -04001860static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1861 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001862 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001863{
1864 struct btrfs_space_info *space_info;
1865 u64 thresh;
1866 u64 start;
1867 u64 num_bytes;
Josef Bacikcf749822008-10-01 19:11:18 -04001868 int ret = 0, waited = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001869
Chris Masona061fc82008-05-07 11:43:44 -04001870 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001871
Chris Mason6324fbf2008-03-24 15:01:59 -04001872 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001873 if (!space_info) {
1874 ret = update_space_info(extent_root->fs_info, flags,
1875 0, 0, &space_info);
1876 BUG_ON(ret);
1877 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001878 BUG_ON(!space_info);
1879
Josef Bacik25179202008-10-29 14:49:05 -04001880 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001881 if (space_info->force_alloc) {
1882 force = 1;
1883 space_info->force_alloc = 0;
1884 }
Josef Bacik25179202008-10-29 14:49:05 -04001885 if (space_info->full) {
1886 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001887 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001888 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001889
Chris Mason8790d502008-04-03 16:29:03 -04001890 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001891 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001892 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001893 space_info->bytes_reserved + alloc_bytes) < thresh) {
1894 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001895 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001896 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001897
Josef Bacik25179202008-10-29 14:49:05 -04001898 spin_unlock(&space_info->lock);
1899
1900 ret = mutex_trylock(&extent_root->fs_info->chunk_mutex);
1901 if (!ret && !force) {
1902 goto out;
1903 } else if (!ret) {
1904 mutex_lock(&extent_root->fs_info->chunk_mutex);
Josef Bacikcf749822008-10-01 19:11:18 -04001905 waited = 1;
1906 }
1907
Josef Bacik25179202008-10-29 14:49:05 -04001908 if (waited) {
1909 spin_lock(&space_info->lock);
1910 if (space_info->full) {
1911 spin_unlock(&space_info->lock);
1912 goto out_unlock;
1913 }
1914 spin_unlock(&space_info->lock);
1915 }
Josef Bacikcf749822008-10-01 19:11:18 -04001916
Chris Mason6324fbf2008-03-24 15:01:59 -04001917 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
Josef Bacik25179202008-10-29 14:49:05 -04001918 if (ret) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001919printk("space info full %Lu\n", flags);
1920 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001921 goto out_unlock;
Chris Mason6324fbf2008-03-24 15:01:59 -04001922 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001923
1924 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001925 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001926 BUG_ON(ret);
Chris Masona74a4b92008-06-25 16:01:31 -04001927out_unlock:
Chris Mason333db942008-06-25 16:01:30 -04001928 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001929out:
Josef Bacik0f9dd462008-09-23 13:14:11 -04001930 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001931}
1932
Chris Mason9078a3e2007-04-26 16:46:15 -04001933static int update_block_group(struct btrfs_trans_handle *trans,
1934 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001935 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001936 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001937{
1938 struct btrfs_block_group_cache *cache;
1939 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001940 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001941 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001942 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001943
Chris Mason9078a3e2007-04-26 16:46:15 -04001944 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001945 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001946 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001947 return -1;
Chris Masondb945352007-10-15 16:15:53 -04001948 byte_in_group = bytenr - cache->key.objectid;
1949 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001950
Josef Bacik25179202008-10-29 14:49:05 -04001951 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001952 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001953 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001954 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001955 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001956 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001957 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001958 cache->space_info->bytes_used += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001959 btrfs_set_block_group_used(&cache->item, old_val);
1960 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001961 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001962 } else {
Chris Masondb945352007-10-15 16:15:53 -04001963 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001964 cache->space_info->bytes_used -= num_bytes;
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 Masonf510cfe2007-10-15 16:14:48 -04001968 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001969 int ret;
1970 ret = btrfs_add_free_space(cache, bytenr,
1971 num_bytes);
1972 if (ret)
1973 return -1;
Chris Masone37c9e62007-05-09 20:13:14 -04001974 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001975 }
Chris Masondb945352007-10-15 16:15:53 -04001976 total -= num_bytes;
1977 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001978 }
1979 return 0;
1980}
Chris Mason6324fbf2008-03-24 15:01:59 -04001981
Chris Masona061fc82008-05-07 11:43:44 -04001982static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1983{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001984 struct btrfs_block_group_cache *cache;
1985
1986 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1987 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001988 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001989
1990 return cache->key.objectid;
Chris Masona061fc82008-05-07 11:43:44 -04001991}
1992
Chris Masone02119d2008-09-05 16:13:11 -04001993int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05001994 u64 bytenr, u64 num, int pin)
1995{
1996 u64 len;
1997 struct btrfs_block_group_cache *cache;
1998 struct btrfs_fs_info *fs_info = root->fs_info;
1999
Josef Bacik25179202008-10-29 14:49:05 -04002000 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05002001 if (pin) {
2002 set_extent_dirty(&fs_info->pinned_extents,
2003 bytenr, bytenr + num - 1, GFP_NOFS);
2004 } else {
2005 clear_extent_dirty(&fs_info->pinned_extents,
2006 bytenr, bytenr + num - 1, GFP_NOFS);
2007 }
2008 while (num > 0) {
2009 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002010 BUG_ON(!cache);
2011 len = min(num, cache->key.offset -
2012 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002013 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002014 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002015 spin_lock(&cache->lock);
2016 cache->pinned += len;
2017 cache->space_info->bytes_pinned += len;
2018 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002019 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002020 fs_info->total_pinned += len;
2021 } else {
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 }
2030 bytenr += len;
2031 num -= len;
2032 }
2033 return 0;
2034}
Chris Mason9078a3e2007-04-26 16:46:15 -04002035
Zheng Yane8569812008-09-26 10:05:48 -04002036static int update_reserved_extents(struct btrfs_root *root,
2037 u64 bytenr, u64 num, int reserve)
2038{
2039 u64 len;
2040 struct btrfs_block_group_cache *cache;
2041 struct btrfs_fs_info *fs_info = root->fs_info;
2042
Zheng Yane8569812008-09-26 10:05:48 -04002043 while (num > 0) {
2044 cache = btrfs_lookup_block_group(fs_info, bytenr);
2045 BUG_ON(!cache);
2046 len = min(num, cache->key.offset -
2047 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04002048
2049 spin_lock(&cache->space_info->lock);
2050 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002051 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04002052 cache->reserved += len;
2053 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04002054 } else {
Zheng Yane8569812008-09-26 10:05:48 -04002055 cache->reserved -= len;
2056 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04002057 }
Josef Bacik25179202008-10-29 14:49:05 -04002058 spin_unlock(&cache->lock);
2059 spin_unlock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002060 bytenr += len;
2061 num -= len;
2062 }
2063 return 0;
2064}
2065
Chris Masond1310b22008-01-24 16:13:08 -05002066int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04002067{
Chris Masonccd467d2007-06-28 15:57:36 -04002068 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002069 u64 start;
2070 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002071 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04002072 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04002073
Josef Bacik25179202008-10-29 14:49:05 -04002074 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002075 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002076 ret = find_first_extent_bit(pinned_extents, last,
2077 &start, &end, EXTENT_DIRTY);
2078 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04002079 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04002080 set_extent_dirty(copy, start, end, GFP_NOFS);
2081 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04002082 }
Josef Bacik25179202008-10-29 14:49:05 -04002083 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002084 return 0;
2085}
2086
2087int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2088 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05002089 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05002090{
Chris Mason1a5bc162007-10-15 16:15:26 -04002091 u64 start;
2092 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002093 int ret;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002094 struct btrfs_block_group_cache *cache;
Chris Masona28ec192007-03-06 20:08:01 -05002095
Josef Bacik25179202008-10-29 14:49:05 -04002096 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002097 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002098 ret = find_first_extent_bit(unpin, 0, &start, &end,
2099 EXTENT_DIRTY);
2100 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05002101 break;
Chris Masone02119d2008-09-05 16:13:11 -04002102 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04002103 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002104 cache = btrfs_lookup_block_group(root->fs_info, start);
2105 if (cache->cached)
2106 btrfs_add_free_space(cache, start, end - start + 1);
Chris Masonc286ac42008-07-22 23:06:41 -04002107 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04002108 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002109 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04002110 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002111 }
Chris Masona28ec192007-03-06 20:08:01 -05002112 }
Josef Bacik25179202008-10-29 14:49:05 -04002113 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002114 return 0;
2115}
2116
Chris Mason98ed5172008-01-03 10:01:48 -05002117static int finish_current_insert(struct btrfs_trans_handle *trans,
Chris Mason87ef2bb2008-10-30 11:23:27 -04002118 struct btrfs_root *extent_root, int all)
Chris Mason037e6392007-03-07 11:50:24 -05002119{
Chris Mason7bb86312007-12-11 09:25:06 -05002120 u64 start;
2121 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002122 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002123 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002124 u64 skipped = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002125 struct btrfs_fs_info *info = extent_root->fs_info;
2126 struct btrfs_path *path;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002127 struct pending_extent_op *extent_op, *tmp;
2128 struct list_head insert_list, update_list;
Chris Mason037e6392007-03-07 11:50:24 -05002129 int ret;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002130 int num_inserts = 0, max_inserts;
Chris Mason037e6392007-03-07 11:50:24 -05002131
Chris Mason7bb86312007-12-11 09:25:06 -05002132 path = btrfs_alloc_path();
Josef Bacikf3465ca2008-11-12 14:19:50 -05002133 INIT_LIST_HEAD(&insert_list);
2134 INIT_LIST_HEAD(&update_list);
Chris Mason037e6392007-03-07 11:50:24 -05002135
Josef Bacikf3465ca2008-11-12 14:19:50 -05002136 max_inserts = extent_root->leafsize /
2137 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2138 sizeof(struct btrfs_extent_ref) +
2139 sizeof(struct btrfs_extent_item));
2140again:
2141 mutex_lock(&info->extent_ins_mutex);
2142 while (1) {
Josef Bacik25179202008-10-29 14:49:05 -04002143 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2144 &end, EXTENT_WRITEBACK);
2145 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002146 if (skipped && all && !num_inserts) {
2147 skipped = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002148 continue;
2149 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002150 mutex_unlock(&info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04002151 break;
Josef Bacik25179202008-10-29 14:49:05 -04002152 }
2153
2154 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2155 if (!ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002156 skipped = 1;
Chris Mason87ef2bb2008-10-30 11:23:27 -04002157 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002158 if (need_resched()) {
2159 mutex_unlock(&info->extent_ins_mutex);
2160 cond_resched();
2161 mutex_lock(&info->extent_ins_mutex);
2162 }
Josef Bacik25179202008-10-29 14:49:05 -04002163 continue;
2164 }
Chris Mason26b80032007-08-08 20:17:12 -04002165
Zheng Yan31840ae2008-09-23 13:14:14 -04002166 ret = get_state_private(&info->extent_ins, start, &priv);
2167 BUG_ON(ret);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002168 extent_op = (struct pending_extent_op *)(unsigned long) priv;
Josef Bacik25179202008-10-29 14:49:05 -04002169
Zheng Yan31840ae2008-09-23 13:14:14 -04002170 if (extent_op->type == PENDING_EXTENT_INSERT) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002171 num_inserts++;
2172 list_add_tail(&extent_op->list, &insert_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002173 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002174 if (num_inserts == max_inserts) {
2175 mutex_unlock(&info->extent_ins_mutex);
2176 break;
2177 }
2178 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2179 list_add_tail(&extent_op->list, &update_list);
2180 search = end + 1;
2181 } else {
2182 BUG();
2183 }
Chris Mason037e6392007-03-07 11:50:24 -05002184 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002185
2186 /*
2187 * process teh update list, clear the writeback bit for it, and if
2188 * somebody marked this thing for deletion then just unlock it and be
2189 * done, the free_extents will handle it
2190 */
2191 mutex_lock(&info->extent_ins_mutex);
2192 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2193 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2194 extent_op->bytenr + extent_op->num_bytes - 1,
2195 EXTENT_WRITEBACK, GFP_NOFS);
2196 if (extent_op->del) {
2197 list_del_init(&extent_op->list);
2198 unlock_extent(&info->extent_ins, extent_op->bytenr,
2199 extent_op->bytenr + extent_op->num_bytes
2200 - 1, GFP_NOFS);
2201 kfree(extent_op);
2202 }
2203 }
2204 mutex_unlock(&info->extent_ins_mutex);
2205
2206 /*
2207 * still have things left on the update list, go ahead an update
2208 * everything
2209 */
2210 if (!list_empty(&update_list)) {
2211 ret = update_backrefs(trans, extent_root, path, &update_list);
2212 BUG_ON(ret);
2213 }
2214
2215 /*
2216 * if no inserts need to be done, but we skipped some extents and we
2217 * need to make sure everything is cleaned then reset everything and
2218 * go back to the beginning
2219 */
2220 if (!num_inserts && all && skipped) {
2221 search = 0;
2222 skipped = 0;
2223 INIT_LIST_HEAD(&update_list);
2224 INIT_LIST_HEAD(&insert_list);
2225 goto again;
2226 } else if (!num_inserts) {
2227 goto out;
2228 }
2229
2230 /*
2231 * process the insert extents list. Again if we are deleting this
2232 * extent, then just unlock it, pin down the bytes if need be, and be
2233 * done with it. Saves us from having to actually insert the extent
2234 * into the tree and then subsequently come along and delete it
2235 */
2236 mutex_lock(&info->extent_ins_mutex);
2237 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2238 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2239 extent_op->bytenr + extent_op->num_bytes - 1,
2240 EXTENT_WRITEBACK, GFP_NOFS);
2241 if (extent_op->del) {
2242 list_del_init(&extent_op->list);
2243 unlock_extent(&info->extent_ins, extent_op->bytenr,
2244 extent_op->bytenr + extent_op->num_bytes
2245 - 1, GFP_NOFS);
2246
2247 mutex_lock(&extent_root->fs_info->pinned_mutex);
2248 ret = pin_down_bytes(trans, extent_root,
2249 extent_op->bytenr,
2250 extent_op->num_bytes, 0);
2251 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2252
2253 ret = update_block_group(trans, extent_root,
2254 extent_op->bytenr,
2255 extent_op->num_bytes,
2256 0, ret > 0);
2257 BUG_ON(ret);
2258 kfree(extent_op);
2259 num_inserts--;
2260 }
2261 }
2262 mutex_unlock(&info->extent_ins_mutex);
2263
2264 ret = insert_extents(trans, extent_root, path, &insert_list,
2265 num_inserts);
2266 BUG_ON(ret);
2267
2268 /*
2269 * if we broke out of the loop in order to insert stuff because we hit
2270 * the maximum number of inserts at a time we can handle, then loop
2271 * back and pick up where we left off
2272 */
2273 if (num_inserts == max_inserts) {
2274 INIT_LIST_HEAD(&insert_list);
2275 INIT_LIST_HEAD(&update_list);
2276 num_inserts = 0;
2277 goto again;
2278 }
2279
2280 /*
2281 * again, if we need to make absolutely sure there are no more pending
2282 * extent operations left and we know that we skipped some, go back to
2283 * the beginning and do it all again
2284 */
2285 if (all && skipped) {
2286 INIT_LIST_HEAD(&insert_list);
2287 INIT_LIST_HEAD(&update_list);
2288 search = 0;
2289 skipped = 0;
2290 num_inserts = 0;
2291 goto again;
2292 }
2293out:
Chris Mason7bb86312007-12-11 09:25:06 -05002294 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05002295 return 0;
2296}
2297
Zheng Yan31840ae2008-09-23 13:14:14 -04002298static int pin_down_bytes(struct btrfs_trans_handle *trans,
2299 struct btrfs_root *root,
2300 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04002301{
Chris Mason1a5bc162007-10-15 16:15:26 -04002302 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002303 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04002304
Zheng Yan31840ae2008-09-23 13:14:14 -04002305 if (is_data)
2306 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002307
Zheng Yan31840ae2008-09-23 13:14:14 -04002308 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2309 if (!buf)
2310 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002311
Zheng Yan31840ae2008-09-23 13:14:14 -04002312 /* we can reuse a block if it hasn't been written
2313 * and it is from this transaction. We can't
2314 * reuse anything from the tree log root because
2315 * it has tiny sub-transactions.
2316 */
2317 if (btrfs_buffer_uptodate(buf, 0) &&
2318 btrfs_try_tree_lock(buf)) {
2319 u64 header_owner = btrfs_header_owner(buf);
2320 u64 header_transid = btrfs_header_generation(buf);
2321 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04002322 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04002323 header_transid == trans->transid &&
2324 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2325 clean_tree_block(NULL, root, buf);
2326 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002327 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002328 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04002329 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002330 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04002331 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002332 free_extent_buffer(buf);
2333pinit:
2334 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2335
Chris Masonbe744172007-05-06 10:15:01 -04002336 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002337 return 0;
2338}
2339
Chris Masona28ec192007-03-06 20:08:01 -05002340/*
2341 * remove an extent from the root, returns 0 on success
2342 */
Zheng Yan31840ae2008-09-23 13:14:14 -04002343static int __free_extent(struct btrfs_trans_handle *trans,
2344 struct btrfs_root *root,
2345 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002346 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002347 u64 owner_objectid, int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05002348{
Chris Mason5caf2a02007-04-02 11:20:42 -04002349 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04002350 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04002351 struct btrfs_fs_info *info = root->fs_info;
2352 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002353 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05002354 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05002355 int extent_slot = 0;
2356 int found_extent = 0;
2357 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04002358 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04002359 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05002360
Chris Masondb945352007-10-15 16:15:53 -04002361 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04002362 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04002363 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04002364 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002365 if (!path)
2366 return -ENOMEM;
2367
Chris Mason3c12ac72008-04-21 12:01:38 -04002368 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002369 ret = lookup_extent_backref(trans, extent_root, path,
2370 bytenr, parent, root_objectid,
2371 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05002372 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002373 struct btrfs_key found_key;
2374 extent_slot = path->slots[0];
2375 while(extent_slot > 0) {
2376 extent_slot--;
2377 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2378 extent_slot);
2379 if (found_key.objectid != bytenr)
2380 break;
2381 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2382 found_key.offset == num_bytes) {
2383 found_extent = 1;
2384 break;
2385 }
2386 if (path->slots[0] - extent_slot > 5)
2387 break;
2388 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002389 if (!found_extent) {
2390 ret = remove_extent_backref(trans, extent_root, path);
2391 BUG_ON(ret);
2392 btrfs_release_path(extent_root, path);
2393 ret = btrfs_search_slot(trans, extent_root,
2394 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002395 if (ret) {
2396 printk(KERN_ERR "umm, got %d back from search"
2397 ", was looking for %Lu\n", ret,
2398 bytenr);
2399 btrfs_print_leaf(extent_root, path->nodes[0]);
2400 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002401 BUG_ON(ret);
2402 extent_slot = path->slots[0];
2403 }
Chris Mason7bb86312007-12-11 09:25:06 -05002404 } else {
2405 btrfs_print_leaf(extent_root, path->nodes[0]);
2406 WARN_ON(1);
2407 printk("Unable to find ref byte nr %Lu root %Lu "
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002408 "gen %Lu owner %Lu\n", bytenr,
2409 root_objectid, ref_generation, owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05002410 }
Chris Mason5f39d392007-10-15 16:14:19 -04002411
2412 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05002413 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04002414 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002415 refs = btrfs_extent_refs(leaf, ei);
2416 BUG_ON(refs == 0);
2417 refs -= 1;
2418 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05002419
Chris Mason5f39d392007-10-15 16:14:19 -04002420 btrfs_mark_buffer_dirty(leaf);
2421
Chris Mason952fcca2008-02-18 16:33:44 -05002422 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002423 struct btrfs_extent_ref *ref;
2424 ref = btrfs_item_ptr(leaf, path->slots[0],
2425 struct btrfs_extent_ref);
2426 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002427 /* if the back ref and the extent are next to each other
2428 * they get deleted below in one shot
2429 */
2430 path->slots[0] = extent_slot;
2431 num_to_del = 2;
2432 } else if (found_extent) {
2433 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002434 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05002435 BUG_ON(ret);
2436 /* if refs are 0, we need to setup the path for deletion */
2437 if (refs == 0) {
2438 btrfs_release_path(extent_root, path);
2439 ret = btrfs_search_slot(trans, extent_root, &key, path,
2440 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002441 BUG_ON(ret);
2442 }
2443 }
2444
Chris Masoncf27e1e2007-03-13 09:49:06 -04002445 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002446 u64 super_used;
2447 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01002448#ifdef BIO_RW_DISCARD
2449 u64 map_length = num_bytes;
2450 struct btrfs_multi_bio *multi = NULL;
2451#endif
Chris Mason78fae272007-03-25 11:35:08 -04002452
2453 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002454 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002455 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2456 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04002457 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05002458 if (ret > 0)
2459 mark_free = 1;
2460 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04002461 }
2462
Josef Bacik58176a92007-08-29 15:47:34 -04002463 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002464 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002465 super_used = btrfs_super_bytes_used(&info->super_copy);
2466 btrfs_set_super_bytes_used(&info->super_copy,
2467 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002468 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04002469
2470 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002471 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002472 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04002473 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05002474 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2475 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04002476 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04002477 btrfs_release_path(extent_root, path);
Chris Masondb945352007-10-15 16:15:53 -04002478 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002479 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04002480 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01002481
2482#ifdef BIO_RW_DISCARD
2483 /* Tell the block device(s) that the sectors can be discarded */
2484 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2485 bytenr, &map_length, &multi, 0);
2486 if (!ret) {
2487 struct btrfs_bio_stripe *stripe = multi->stripes;
2488 int i;
2489
2490 if (map_length > num_bytes)
2491 map_length = num_bytes;
2492
2493 for (i = 0; i < multi->num_stripes; i++, stripe++) {
2494 blkdev_issue_discard(stripe->dev->bdev,
2495 stripe->physical >> 9,
2496 map_length >> 9);
2497 }
2498 kfree(multi);
2499 }
2500#endif
Chris Masona28ec192007-03-06 20:08:01 -05002501 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002502 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002503 finish_current_insert(trans, extent_root, 0);
Chris Masona28ec192007-03-06 20:08:01 -05002504 return ret;
2505}
2506
2507/*
Chris Masonfec577f2007-02-26 10:40:21 -05002508 * find all the blocks marked as pending in the radix tree and remove
2509 * them from the extent map
2510 */
Chris Masone089f052007-03-16 16:20:31 -04002511static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -04002512 btrfs_root *extent_root, int all)
Chris Masonfec577f2007-02-26 10:40:21 -05002513{
2514 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002515 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002516 u64 start;
2517 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002518 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002519 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002520 int nr = 0, skipped = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002521 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002522 struct extent_io_tree *extent_ins;
2523 struct pending_extent_op *extent_op;
Josef Bacik25179202008-10-29 14:49:05 -04002524 struct btrfs_fs_info *info = extent_root->fs_info;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002525 struct list_head delete_list;
Chris Mason8ef97622007-03-26 10:15:30 -04002526
Josef Bacikf3465ca2008-11-12 14:19:50 -05002527 INIT_LIST_HEAD(&delete_list);
Zheng Yan31840ae2008-09-23 13:14:14 -04002528 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc162007-10-15 16:15:26 -04002529 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002530
Josef Bacikf3465ca2008-11-12 14:19:50 -05002531again:
2532 mutex_lock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002533 while(1) {
Josef Bacik25179202008-10-29 14:49:05 -04002534 ret = find_first_extent_bit(pending_del, search, &start, &end,
2535 EXTENT_WRITEBACK);
2536 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002537 if (all && skipped && !nr) {
Josef Bacik25179202008-10-29 14:49:05 -04002538 search = 0;
2539 continue;
2540 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002541 mutex_unlock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002542 break;
Josef Bacik25179202008-10-29 14:49:05 -04002543 }
2544
2545 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2546 if (!ret) {
2547 search = end+1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002548 skipped = 1;
2549
2550 if (need_resched()) {
2551 mutex_unlock(&info->extent_ins_mutex);
2552 cond_resched();
2553 mutex_lock(&info->extent_ins_mutex);
2554 }
2555
Josef Bacik25179202008-10-29 14:49:05 -04002556 continue;
2557 }
2558 BUG_ON(ret < 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002559
2560 ret = get_state_private(pending_del, start, &priv);
2561 BUG_ON(ret);
2562 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2563
Josef Bacik25179202008-10-29 14:49:05 -04002564 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
Chris Mason1a5bc162007-10-15 16:15:26 -04002565 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002566 if (!test_range_bit(extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04002567 EXTENT_WRITEBACK, 0)) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002568 list_add_tail(&extent_op->list, &delete_list);
2569 nr++;
Chris Masonc286ac42008-07-22 23:06:41 -04002570 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002571 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002572
2573 ret = get_state_private(&info->extent_ins, start,
2574 &priv);
Zheng Yan31840ae2008-09-23 13:14:14 -04002575 BUG_ON(ret);
2576 extent_op = (struct pending_extent_op *)
Josef Bacik25179202008-10-29 14:49:05 -04002577 (unsigned long)priv;
Zheng Yan31840ae2008-09-23 13:14:14 -04002578
Josef Bacik25179202008-10-29 14:49:05 -04002579 clear_extent_bits(&info->extent_ins, start, end,
2580 EXTENT_WRITEBACK, GFP_NOFS);
2581
Josef Bacikf3465ca2008-11-12 14:19:50 -05002582 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2583 list_add_tail(&extent_op->list, &delete_list);
2584 search = end + 1;
2585 nr++;
2586 continue;
2587 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002588
Josef Bacik25179202008-10-29 14:49:05 -04002589 mutex_lock(&extent_root->fs_info->pinned_mutex);
2590 ret = pin_down_bytes(trans, extent_root, start,
2591 end + 1 - start, 0);
2592 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2593
Zheng Yan31840ae2008-09-23 13:14:14 -04002594 ret = update_block_group(trans, extent_root, start,
Josef Bacik25179202008-10-29 14:49:05 -04002595 end + 1 - start, 0, ret > 0);
2596
Josef Bacikf3465ca2008-11-12 14:19:50 -05002597 unlock_extent(extent_ins, start, end, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002598 BUG_ON(ret);
2599 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002600 }
Chris Mason1a5bc162007-10-15 16:15:26 -04002601 if (ret)
2602 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04002603
Josef Bacikf3465ca2008-11-12 14:19:50 -05002604 search = end + 1;
2605
2606 if (need_resched()) {
2607 mutex_unlock(&info->extent_ins_mutex);
2608 cond_resched();
2609 mutex_lock(&info->extent_ins_mutex);
2610 }
Chris Masonfec577f2007-02-26 10:40:21 -05002611 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002612
2613 if (nr) {
2614 ret = free_extents(trans, extent_root, &delete_list);
2615 BUG_ON(ret);
2616 }
2617
2618 if (all && skipped) {
2619 INIT_LIST_HEAD(&delete_list);
2620 search = 0;
2621 nr = 0;
2622 goto again;
2623 }
2624
Chris Masone20d96d2007-03-22 12:13:20 -04002625 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002626}
2627
2628/*
2629 * remove an extent from the root, returns 0 on success
2630 */
Chris Mason925baed2008-06-25 16:01:30 -04002631static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002632 struct btrfs_root *root,
2633 u64 bytenr, u64 num_bytes, u64 parent,
2634 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002635 u64 owner_objectid, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002636{
Chris Mason9f5fae22007-03-20 14:38:32 -04002637 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002638 int pending_ret;
2639 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002640
Chris Masondb945352007-10-15 16:15:53 -04002641 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002642 if (root == extent_root) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002643 struct pending_extent_op *extent_op = NULL;
2644
2645 mutex_lock(&root->fs_info->extent_ins_mutex);
2646 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2647 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2648 u64 priv;
2649 ret = get_state_private(&root->fs_info->extent_ins,
2650 bytenr, &priv);
2651 BUG_ON(ret);
2652 extent_op = (struct pending_extent_op *)
2653 (unsigned long)priv;
2654
2655 extent_op->del = 1;
2656 if (extent_op->type == PENDING_EXTENT_INSERT) {
2657 mutex_unlock(&root->fs_info->extent_ins_mutex);
2658 return 0;
2659 }
2660 }
2661
2662 if (extent_op) {
2663 ref_generation = extent_op->orig_generation;
2664 parent = extent_op->orig_parent;
2665 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002666
2667 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2668 BUG_ON(!extent_op);
2669
2670 extent_op->type = PENDING_EXTENT_DELETE;
2671 extent_op->bytenr = bytenr;
2672 extent_op->num_bytes = num_bytes;
2673 extent_op->parent = parent;
2674 extent_op->orig_parent = parent;
2675 extent_op->generation = ref_generation;
2676 extent_op->orig_generation = ref_generation;
2677 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002678 INIT_LIST_HEAD(&extent_op->list);
2679 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002680
2681 set_extent_bits(&root->fs_info->pending_del,
2682 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04002683 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002684 set_state_private(&root->fs_info->pending_del,
2685 bytenr, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002686 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002687 return 0;
2688 }
Chris Mason4bef0842008-09-08 11:18:08 -04002689 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002690 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2691 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002692 struct btrfs_block_group_cache *cache;
2693
Chris Masond00aff02008-09-11 15:54:42 -04002694 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002695 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2696 BUG_ON(!cache);
2697 btrfs_add_free_space(cache, bytenr, num_bytes);
Zheng Yane8569812008-09-26 10:05:48 -04002698 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002699 return 0;
2700 }
Chris Mason4bef0842008-09-08 11:18:08 -04002701 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002702 }
Chris Mason4bef0842008-09-08 11:18:08 -04002703
2704 /* if data pin when any transaction has committed this */
2705 if (ref_generation != trans->transid)
2706 pin = 1;
2707
Zheng Yan31840ae2008-09-23 13:14:14 -04002708 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002709 root_objectid, ref_generation,
2710 owner_objectid, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002711
Chris Mason87ef2bb2008-10-30 11:23:27 -04002712 finish_current_insert(trans, root->fs_info->extent_root, 0);
2713 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002714 return ret ? ret : pending_ret;
2715}
2716
Chris Mason925baed2008-06-25 16:01:30 -04002717int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002718 struct btrfs_root *root,
2719 u64 bytenr, u64 num_bytes, u64 parent,
2720 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002721 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002722{
2723 int ret;
2724
Zheng Yan31840ae2008-09-23 13:14:14 -04002725 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002726 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002727 owner_objectid, pin);
Chris Mason925baed2008-06-25 16:01:30 -04002728 return ret;
2729}
2730
Chris Mason87ee04e2007-11-30 11:30:34 -05002731static u64 stripe_align(struct btrfs_root *root, u64 val)
2732{
2733 u64 mask = ((u64)root->stripesize - 1);
2734 u64 ret = (val + mask) & ~mask;
2735 return ret;
2736}
2737
Chris Masonfec577f2007-02-26 10:40:21 -05002738/*
2739 * walks the btree of allocated extents and find a hole of a given size.
2740 * The key ins is changed to record the hole:
2741 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002742 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002743 * ins->offset == number of blocks
2744 * Any available blocks before search_start are skipped.
2745 */
Chris Mason98ed5172008-01-03 10:01:48 -05002746static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2747 struct btrfs_root *orig_root,
2748 u64 num_bytes, u64 empty_size,
2749 u64 search_start, u64 search_end,
2750 u64 hint_byte, struct btrfs_key *ins,
2751 u64 exclude_start, u64 exclude_nr,
2752 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002753{
Josef Bacik80eb2342008-10-29 14:49:05 -04002754 int ret = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -04002755 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002756 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002757 u64 *last_ptr = NULL;
Chris Mason43662112008-11-07 09:06:11 -05002758 u64 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002759 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002760 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002761 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002762 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002763 struct list_head *head = NULL, *cur = NULL;
2764 int loop = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002765 int extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002766 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002767
Chris Masondb945352007-10-15 16:15:53 -04002768 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002769 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002770 ins->objectid = 0;
2771 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002772
Chris Mason0ef3e662008-05-24 14:04:53 -04002773 if (orig_root->ref_cows || empty_size)
2774 allowed_chunk_alloc = 1;
2775
Chris Mason239b14b2008-03-24 15:02:07 -04002776 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2777 last_ptr = &root->fs_info->last_alloc;
Chris Mason43662112008-11-07 09:06:11 -05002778 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002779 }
2780
Josef Bacik0f9dd462008-09-23 13:14:11 -04002781 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002782 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002783
Chris Mason239b14b2008-03-24 15:02:07 -04002784 if (last_ptr) {
Chris Mason43662112008-11-07 09:06:11 -05002785 if (*last_ptr) {
Chris Mason239b14b2008-03-24 15:02:07 -04002786 hint_byte = *last_ptr;
Chris Mason43662112008-11-07 09:06:11 -05002787 last_wanted = *last_ptr;
2788 } else
Chris Mason239b14b2008-03-24 15:02:07 -04002789 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002790 } else {
2791 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002792 }
Chris Masona061fc82008-05-07 11:43:44 -04002793 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002794 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04002795
Chris Mason42e70e72008-11-07 18:17:11 -05002796 if (last_wanted && search_start != last_wanted) {
Chris Mason43662112008-11-07 09:06:11 -05002797 last_wanted = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002798 empty_size += empty_cluster;
2799 }
Chris Mason43662112008-11-07 09:06:11 -05002800
Chris Mason42e70e72008-11-07 18:17:11 -05002801 total_needed += empty_size;
Josef Bacik80eb2342008-10-29 14:49:05 -04002802 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
Yan Zhengd899e052008-10-30 14:25:28 -04002803 if (!block_group)
2804 block_group = btrfs_lookup_first_block_group(root->fs_info,
2805 search_start);
Josef Bacik80eb2342008-10-29 14:49:05 -04002806 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002807
Josef Bacik80eb2342008-10-29 14:49:05 -04002808 down_read(&space_info->groups_sem);
2809 while (1) {
2810 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002811 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002812 * the only way this happens if our hint points to a block
2813 * group thats not of the proper type, while looping this
2814 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002815 */
Chris Mason8a1413a2008-11-10 16:13:54 -05002816 if (empty_size)
2817 extra_loop = 1;
2818
Chris Mason42e70e72008-11-07 18:17:11 -05002819 if (!block_group)
2820 goto new_group_no_lock;
2821
Josef Bacik25179202008-10-29 14:49:05 -04002822 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002823 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002824 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002825
Josef Bacik80eb2342008-10-29 14:49:05 -04002826 ret = cache_block_group(root, block_group);
Josef Bacik25179202008-10-29 14:49:05 -04002827 if (ret) {
2828 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002829 break;
Josef Bacik25179202008-10-29 14:49:05 -04002830 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002831
Josef Bacik80eb2342008-10-29 14:49:05 -04002832 if (block_group->ro)
2833 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002834
Josef Bacik80eb2342008-10-29 14:49:05 -04002835 free_space = btrfs_find_free_space(block_group, search_start,
2836 total_needed);
2837 if (free_space) {
2838 u64 start = block_group->key.objectid;
2839 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002840 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002841
Josef Bacik80eb2342008-10-29 14:49:05 -04002842 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002843
Josef Bacik80eb2342008-10-29 14:49:05 -04002844 /* move on to the next group */
2845 if (search_start + num_bytes >= search_end)
2846 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002847
Josef Bacik80eb2342008-10-29 14:49:05 -04002848 /* move on to the next group */
2849 if (search_start + num_bytes > end)
2850 goto new_group;
2851
Chris Mason43662112008-11-07 09:06:11 -05002852 if (last_wanted && search_start != last_wanted) {
Chris Mason3b7885b2008-11-06 21:48:27 -05002853 total_needed += empty_cluster;
Chris Mason8a1413a2008-11-10 16:13:54 -05002854 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002855 last_wanted = 0;
Chris Mason3b7885b2008-11-06 21:48:27 -05002856 /*
2857 * if search_start is still in this block group
2858 * then we just re-search this block group
2859 */
2860 if (search_start >= start &&
2861 search_start < end) {
2862 mutex_unlock(&block_group->alloc_mutex);
2863 continue;
2864 }
2865
2866 /* else we go to the next block group */
2867 goto new_group;
2868 }
2869
Josef Bacik80eb2342008-10-29 14:49:05 -04002870 if (exclude_nr > 0 &&
2871 (search_start + num_bytes > exclude_start &&
2872 search_start < exclude_start + exclude_nr)) {
2873 search_start = exclude_start + exclude_nr;
2874 /*
2875 * if search_start is still in this block group
2876 * then we just re-search this block group
2877 */
2878 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002879 search_start < end) {
2880 mutex_unlock(&block_group->alloc_mutex);
Chris Mason43662112008-11-07 09:06:11 -05002881 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002882 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002883 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002884
2885 /* else we go to the next block group */
2886 goto new_group;
2887 }
2888
2889 ins->objectid = search_start;
2890 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002891
2892 btrfs_remove_free_space_lock(block_group, search_start,
2893 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002894 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002895 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002896 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002897 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002898new_group:
Chris Mason42e70e72008-11-07 18:17:11 -05002899 mutex_unlock(&block_group->alloc_mutex);
2900new_group_no_lock:
Chris Masonf5a31e12008-11-10 11:47:09 -05002901 /* don't try to compare new allocations against the
2902 * last allocation any more
2903 */
Chris Mason43662112008-11-07 09:06:11 -05002904 last_wanted = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002905
Josef Bacik80eb2342008-10-29 14:49:05 -04002906 /*
2907 * Here's how this works.
2908 * loop == 0: we were searching a block group via a hint
2909 * and didn't find anything, so we start at
2910 * the head of the block groups and keep searching
2911 * loop == 1: we're searching through all of the block groups
2912 * if we hit the head again we have searched
2913 * all of the block groups for this space and we
2914 * need to try and allocate, if we cant error out.
2915 * loop == 2: we allocated more space and are looping through
2916 * all of the block groups again.
2917 */
2918 if (loop == 0) {
2919 head = &space_info->block_groups;
2920 cur = head->next;
Josef Bacik80eb2342008-10-29 14:49:05 -04002921 loop++;
2922 } else if (loop == 1 && cur == head) {
Chris Masonf5a31e12008-11-10 11:47:09 -05002923 int keep_going;
Chris Mason42e70e72008-11-07 18:17:11 -05002924
Chris Masonf5a31e12008-11-10 11:47:09 -05002925 /* at this point we give up on the empty_size
2926 * allocations and just try to allocate the min
2927 * space.
2928 *
2929 * The extra_loop field was set if an empty_size
2930 * allocation was attempted above, and if this
2931 * is try we need to try the loop again without
2932 * the additional empty_size.
2933 */
Chris Mason5b7c3fc2008-11-10 07:26:33 -05002934 total_needed -= empty_size;
2935 empty_size = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002936 keep_going = extra_loop;
2937 loop++;
Chris Mason42e70e72008-11-07 18:17:11 -05002938
Josef Bacik80eb2342008-10-29 14:49:05 -04002939 if (allowed_chunk_alloc && !chunk_alloc_done) {
2940 up_read(&space_info->groups_sem);
2941 ret = do_chunk_alloc(trans, root, num_bytes +
2942 2 * 1024 * 1024, data, 1);
Josef Bacik80eb2342008-10-29 14:49:05 -04002943 down_read(&space_info->groups_sem);
Chris Mason2ed6d662008-11-13 09:59:33 -05002944 if (ret < 0)
2945 goto loop_check;
Josef Bacik80eb2342008-10-29 14:49:05 -04002946 head = &space_info->block_groups;
Chris Masonf5a31e12008-11-10 11:47:09 -05002947 /*
2948 * we've allocated a new chunk, keep
2949 * trying
2950 */
2951 keep_going = 1;
Josef Bacik80eb2342008-10-29 14:49:05 -04002952 chunk_alloc_done = 1;
2953 } else if (!allowed_chunk_alloc) {
2954 space_info->force_alloc = 1;
Chris Masonf5a31e12008-11-10 11:47:09 -05002955 }
Chris Mason2ed6d662008-11-13 09:59:33 -05002956loop_check:
Chris Masonf5a31e12008-11-10 11:47:09 -05002957 if (keep_going) {
2958 cur = head->next;
2959 extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002960 } else {
2961 break;
2962 }
2963 } else if (cur == head) {
2964 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002965 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002966
2967 block_group = list_entry(cur, struct btrfs_block_group_cache,
2968 list);
2969 search_start = block_group->key.objectid;
2970 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002971 }
Chris Mason0b86a832008-03-24 15:01:56 -04002972
Josef Bacik80eb2342008-10-29 14:49:05 -04002973 /* we found what we needed */
2974 if (ins->objectid) {
2975 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2976 trans->block_group = block_group;
2977
2978 if (last_ptr)
2979 *last_ptr = ins->objectid + ins->offset;
2980 ret = 0;
2981 } else if (!ret) {
2982 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04002983 }
Chris Mason0b86a832008-03-24 15:01:56 -04002984
Josef Bacik80eb2342008-10-29 14:49:05 -04002985 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05002986 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002987}
Chris Masonec44a352008-04-28 15:29:52 -04002988
Josef Bacik0f9dd462008-09-23 13:14:11 -04002989static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2990{
2991 struct btrfs_block_group_cache *cache;
2992 struct list_head *l;
2993
2994 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
Zheng Yane8569812008-09-26 10:05:48 -04002995 info->total_bytes - info->bytes_used - info->bytes_pinned -
2996 info->bytes_reserved, (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002997
Josef Bacik80eb2342008-10-29 14:49:05 -04002998 down_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002999 list_for_each(l, &info->block_groups) {
3000 cache = list_entry(l, struct btrfs_block_group_cache, list);
3001 spin_lock(&cache->lock);
3002 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
Zheng Yane8569812008-09-26 10:05:48 -04003003 "%Lu pinned %Lu reserved\n",
Josef Bacik0f9dd462008-09-23 13:14:11 -04003004 cache->key.objectid, cache->key.offset,
Zheng Yane8569812008-09-26 10:05:48 -04003005 btrfs_block_group_used(&cache->item),
3006 cache->pinned, cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003007 btrfs_dump_free_space(cache, bytes);
3008 spin_unlock(&cache->lock);
3009 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003010 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003011}
Zheng Yane8569812008-09-26 10:05:48 -04003012
Chris Masone6dcd2d2008-07-17 12:53:50 -04003013static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3014 struct btrfs_root *root,
3015 u64 num_bytes, u64 min_alloc_size,
3016 u64 empty_size, u64 hint_byte,
3017 u64 search_end, struct btrfs_key *ins,
3018 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05003019{
3020 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04003021 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003022 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04003023 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003024
Chris Mason6324fbf2008-03-24 15:01:59 -04003025 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04003026 alloc_profile = info->avail_data_alloc_bits &
3027 info->data_alloc_profile;
3028 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003029 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04003030 alloc_profile = info->avail_system_alloc_bits &
3031 info->system_alloc_profile;
3032 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003033 } else {
Chris Mason8790d502008-04-03 16:29:03 -04003034 alloc_profile = info->avail_metadata_alloc_bits &
3035 info->metadata_alloc_profile;
3036 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003037 }
Chris Mason98d20f62008-04-14 09:46:10 -04003038again:
Chris Masona061fc82008-05-07 11:43:44 -04003039 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04003040 /*
3041 * the only place that sets empty_size is btrfs_realloc_node, which
3042 * is not called recursively on allocations
3043 */
3044 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04003045 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04003046 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003047 2 * 1024 * 1024,
3048 BTRFS_BLOCK_GROUP_METADATA |
3049 (info->metadata_alloc_profile &
3050 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003051 }
3052 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003053 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003054 }
Chris Mason0b86a832008-03-24 15:01:56 -04003055
Chris Masondb945352007-10-15 16:15:53 -04003056 WARN_ON(num_bytes < root->sectorsize);
3057 ret = find_free_extent(trans, root, num_bytes, empty_size,
3058 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04003059 trans->alloc_exclude_start,
3060 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04003061
Chris Mason98d20f62008-04-14 09:46:10 -04003062 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3063 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003064 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003065 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04003066 do_chunk_alloc(trans, root->fs_info->extent_root,
3067 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003068 goto again;
3069 }
Chris Masonec44a352008-04-28 15:29:52 -04003070 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003071 struct btrfs_space_info *sinfo;
3072
3073 sinfo = __find_space_info(root->fs_info, data);
3074 printk("allocation failed flags %Lu, wanted %Lu\n",
3075 data, num_bytes);
3076 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04003077 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04003078 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003079
3080 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003081}
3082
Chris Mason65b51a02008-08-01 15:11:20 -04003083int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3084{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003085 struct btrfs_block_group_cache *cache;
3086
Josef Bacik0f9dd462008-09-23 13:14:11 -04003087 cache = btrfs_lookup_block_group(root->fs_info, start);
3088 if (!cache) {
3089 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003090 return -ENOSPC;
3091 }
3092 btrfs_add_free_space(cache, start, len);
Zheng Yan1a40e232008-09-26 10:09:34 -04003093 update_reserved_extents(root, start, len, 0);
Chris Mason65b51a02008-08-01 15:11:20 -04003094 return 0;
3095}
3096
Chris Masone6dcd2d2008-07-17 12:53:50 -04003097int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3098 struct btrfs_root *root,
3099 u64 num_bytes, u64 min_alloc_size,
3100 u64 empty_size, u64 hint_byte,
3101 u64 search_end, struct btrfs_key *ins,
3102 u64 data)
3103{
3104 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003105 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3106 empty_size, hint_byte, search_end, ins,
3107 data);
Zheng Yane8569812008-09-26 10:05:48 -04003108 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003109 return ret;
3110}
3111
3112static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003113 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003114 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003115 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003116{
3117 int ret;
3118 int pending_ret;
3119 u64 super_used;
3120 u64 root_used;
3121 u64 num_bytes = ins->offset;
3122 u32 sizes[2];
3123 struct btrfs_fs_info *info = root->fs_info;
3124 struct btrfs_root *extent_root = info->extent_root;
3125 struct btrfs_extent_item *extent_item;
3126 struct btrfs_extent_ref *ref;
3127 struct btrfs_path *path;
3128 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04003129
Zheng Yan31840ae2008-09-23 13:14:14 -04003130 if (parent == 0)
3131 parent = ins->objectid;
3132
Josef Bacik58176a92007-08-29 15:47:34 -04003133 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04003134 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04003135 super_used = btrfs_super_bytes_used(&info->super_copy);
3136 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04003137 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04003138
Josef Bacik58176a92007-08-29 15:47:34 -04003139 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04003140 root_used = btrfs_root_used(&root->root_item);
3141 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04003142
Chris Mason26b80032007-08-08 20:17:12 -04003143 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003144 struct pending_extent_op *extent_op;
3145
3146 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3147 BUG_ON(!extent_op);
3148
3149 extent_op->type = PENDING_EXTENT_INSERT;
3150 extent_op->bytenr = ins->objectid;
3151 extent_op->num_bytes = ins->offset;
3152 extent_op->parent = parent;
3153 extent_op->orig_parent = 0;
3154 extent_op->generation = ref_generation;
3155 extent_op->orig_generation = 0;
3156 extent_op->level = (int)owner;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003157 INIT_LIST_HEAD(&extent_op->list);
3158 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003159
Josef Bacik25179202008-10-29 14:49:05 -04003160 mutex_lock(&root->fs_info->extent_ins_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04003161 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3162 ins->objectid + ins->offset - 1,
Josef Bacik25179202008-10-29 14:49:05 -04003163 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04003164 set_state_private(&root->fs_info->extent_ins,
3165 ins->objectid, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04003166 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04003167 goto update_block;
3168 }
3169
Chris Mason47e4bb92008-02-01 14:51:59 -05003170 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05003171 keys[1].objectid = ins->objectid;
3172 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04003173 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05003174 sizes[0] = sizeof(*extent_item);
3175 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05003176
3177 path = btrfs_alloc_path();
3178 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05003179
3180 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3181 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04003182 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003183
Chris Mason47e4bb92008-02-01 14:51:59 -05003184 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3185 struct btrfs_extent_item);
3186 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3187 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3188 struct btrfs_extent_ref);
3189
3190 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3191 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3192 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Zheng Yan31840ae2008-09-23 13:14:14 -04003193 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05003194
3195 btrfs_mark_buffer_dirty(path->nodes[0]);
3196
3197 trans->alloc_exclude_start = 0;
3198 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05003199 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04003200 finish_current_insert(trans, extent_root, 0);
3201 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -04003202
Chris Mason925baed2008-06-25 16:01:30 -04003203 if (ret)
3204 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003205 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04003206 ret = pending_ret;
3207 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003208 }
Chris Mason26b80032007-08-08 20:17:12 -04003209
3210update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04003211 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05003212 if (ret) {
3213 printk("update block group failed for %Lu %Lu\n",
3214 ins->objectid, ins->offset);
3215 BUG();
3216 }
Chris Mason925baed2008-06-25 16:01:30 -04003217out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04003218 return ret;
3219}
3220
3221int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003222 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003223 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003224 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003225{
3226 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04003227
3228 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3229 return 0;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003230 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3231 ref_generation, owner, ins);
Zheng Yane8569812008-09-26 10:05:48 -04003232 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003233 return ret;
3234}
Chris Masone02119d2008-09-05 16:13:11 -04003235
3236/*
3237 * this is used by the tree logging recovery code. It records that
3238 * an extent has been allocated and makes sure to clear the free
3239 * space cache bits as well
3240 */
3241int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003242 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04003243 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003244 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04003245{
3246 int ret;
3247 struct btrfs_block_group_cache *block_group;
3248
Chris Masone02119d2008-09-05 16:13:11 -04003249 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik25179202008-10-29 14:49:05 -04003250 mutex_lock(&block_group->alloc_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003251 cache_block_group(root, block_group);
3252
Josef Bacik25179202008-10-29 14:49:05 -04003253 ret = btrfs_remove_free_space_lock(block_group, ins->objectid,
3254 ins->offset);
3255 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003256 BUG_ON(ret);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003257 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3258 ref_generation, owner, ins);
Chris Masone02119d2008-09-05 16:13:11 -04003259 return ret;
3260}
3261
Chris Masone6dcd2d2008-07-17 12:53:50 -04003262/*
3263 * finds a free extent and does all the dirty work required for allocation
3264 * returns the key for the extent through ins, and a tree buffer for
3265 * the first block of the extent through buf.
3266 *
3267 * returns 0 if everything worked, non-zero otherwise.
3268 */
3269int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3270 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003271 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003272 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003273 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003274 u64 search_end, struct btrfs_key *ins, u64 data)
3275{
3276 int ret;
3277
Chris Masone6dcd2d2008-07-17 12:53:50 -04003278 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3279 min_alloc_size, empty_size, hint_byte,
3280 search_end, ins, data);
3281 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04003282 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003283 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3284 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003285 owner_objectid, ins);
Chris Masond00aff02008-09-11 15:54:42 -04003286 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003287
Zheng Yane8569812008-09-26 10:05:48 -04003288 } else {
3289 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04003290 }
Chris Mason925baed2008-06-25 16:01:30 -04003291 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003292}
Chris Mason65b51a02008-08-01 15:11:20 -04003293
3294struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3295 struct btrfs_root *root,
3296 u64 bytenr, u32 blocksize)
3297{
3298 struct extent_buffer *buf;
3299
3300 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3301 if (!buf)
3302 return ERR_PTR(-ENOMEM);
3303 btrfs_set_header_generation(buf, trans->transid);
3304 btrfs_tree_lock(buf);
3305 clean_tree_block(trans, root, buf);
3306 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04003307 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3308 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04003309 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04003310 } else {
3311 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3312 buf->start + buf->len - 1, GFP_NOFS);
3313 }
Chris Mason65b51a02008-08-01 15:11:20 -04003314 trans->blocks_used++;
3315 return buf;
3316}
3317
Chris Masonfec577f2007-02-26 10:40:21 -05003318/*
3319 * helper function to allocate a block for a given tree
3320 * returns the tree buffer or NULL.
3321 */
Chris Mason5f39d392007-10-15 16:14:19 -04003322struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04003323 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003324 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05003325 u64 root_objectid,
3326 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05003327 int level,
3328 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04003329 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05003330{
Chris Masone2fa7222007-03-12 16:22:34 -04003331 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05003332 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003333 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05003334
Zheng Yan31840ae2008-09-23 13:14:14 -04003335 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003336 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04003337 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05003338 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04003339 BUG_ON(ret > 0);
3340 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05003341 }
Chris Mason55c69072008-01-09 15:55:33 -05003342
Chris Mason65b51a02008-08-01 15:11:20 -04003343 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05003344 return buf;
3345}
Chris Masona28ec192007-03-06 20:08:01 -05003346
Chris Masone02119d2008-09-05 16:13:11 -04003347int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3348 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04003349{
Chris Mason7bb86312007-12-11 09:25:06 -05003350 u64 leaf_owner;
3351 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04003352 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04003353 struct btrfs_file_extent_item *fi;
3354 int i;
3355 int nritems;
3356 int ret;
3357
Chris Mason5f39d392007-10-15 16:14:19 -04003358 BUG_ON(!btrfs_is_leaf(leaf));
3359 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05003360 leaf_owner = btrfs_header_owner(leaf);
3361 leaf_generation = btrfs_header_generation(leaf);
3362
Chris Mason6407bf62007-03-27 06:33:00 -04003363 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003364 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04003365 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04003366
3367 btrfs_item_key_to_cpu(leaf, &key, i);
3368 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04003369 continue;
3370 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003371 if (btrfs_file_extent_type(leaf, fi) ==
3372 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04003373 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04003374 /*
3375 * FIXME make sure to insert a trans record that
3376 * repeats the snapshot del on crash
3377 */
Chris Masondb945352007-10-15 16:15:53 -04003378 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3379 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04003380 continue;
Chris Mason4a096752008-07-21 10:29:44 -04003381
Chris Mason925baed2008-06-25 16:01:30 -04003382 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003383 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04003384 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003385 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003386 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04003387
3388 atomic_inc(&root->fs_info->throttle_gen);
3389 wake_up(&root->fs_info->transaction_throttle);
3390 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04003391 }
3392 return 0;
3393}
3394
Chris Masone02119d2008-09-05 16:13:11 -04003395static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3396 struct btrfs_root *root,
3397 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04003398{
3399 int i;
3400 int ret;
3401 struct btrfs_extent_info *info = ref->extents;
3402
Yan Zheng31153d82008-07-28 15:32:19 -04003403 for (i = 0; i < ref->nritems; i++) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003404 ret = __btrfs_free_extent(trans, root, info->bytenr,
3405 info->num_bytes, ref->bytenr,
3406 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003407 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04003408
3409 atomic_inc(&root->fs_info->throttle_gen);
3410 wake_up(&root->fs_info->transaction_throttle);
3411 cond_resched();
3412
Yan Zheng31153d82008-07-28 15:32:19 -04003413 BUG_ON(ret);
3414 info++;
3415 }
Yan Zheng31153d82008-07-28 15:32:19 -04003416
3417 return 0;
3418}
3419
Chris Mason333db942008-06-25 16:01:30 -04003420int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
3421 u32 *refs)
3422{
Chris Mason017e5362008-07-28 15:32:51 -04003423 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04003424
Zheng Yan31840ae2008-09-23 13:14:14 -04003425 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04003426 BUG_ON(ret);
3427
3428#if 0 // some debugging code in case we see problems here
3429 /* if the refs count is one, it won't get increased again. But
3430 * if the ref count is > 1, someone may be decreasing it at
3431 * the same time we are.
3432 */
3433 if (*refs != 1) {
3434 struct extent_buffer *eb = NULL;
3435 eb = btrfs_find_create_tree_block(root, start, len);
3436 if (eb)
3437 btrfs_tree_lock(eb);
3438
3439 mutex_lock(&root->fs_info->alloc_mutex);
3440 ret = lookup_extent_ref(NULL, root, start, len, refs);
3441 BUG_ON(ret);
3442 mutex_unlock(&root->fs_info->alloc_mutex);
3443
3444 if (eb) {
3445 btrfs_tree_unlock(eb);
3446 free_extent_buffer(eb);
3447 }
3448 if (*refs == 1) {
3449 printk("block %llu went down to one during drop_snap\n",
3450 (unsigned long long)start);
3451 }
3452
3453 }
3454#endif
3455
Chris Masone7a84562008-06-25 16:01:31 -04003456 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04003457 return ret;
Chris Mason333db942008-06-25 16:01:30 -04003458}
3459
3460/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003461 * helper function for drop_snapshot, this walks down the tree dropping ref
3462 * counts as it goes.
3463 */
Chris Mason98ed5172008-01-03 10:01:48 -05003464static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
3465 struct btrfs_root *root,
3466 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003467{
Chris Mason7bb86312007-12-11 09:25:06 -05003468 u64 root_owner;
3469 u64 root_gen;
3470 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04003471 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003472 struct extent_buffer *next;
3473 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05003474 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04003475 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04003476 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05003477 int ret;
3478 u32 refs;
3479
Chris Mason5caf2a02007-04-02 11:20:42 -04003480 WARN_ON(*level < 0);
3481 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04003482 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04003483 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05003484 BUG_ON(ret);
3485 if (refs > 1)
3486 goto out;
Chris Masone0115992007-06-19 16:23:05 -04003487
Chris Mason9aca1d52007-03-13 11:09:37 -04003488 /*
3489 * walk down to the last node level and free all the leaves
3490 */
Chris Mason6407bf62007-03-27 06:33:00 -04003491 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003492 WARN_ON(*level < 0);
3493 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05003494 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04003495
Chris Mason5f39d392007-10-15 16:14:19 -04003496 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04003497 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04003498
Chris Mason7518a232007-03-12 12:01:18 -04003499 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04003500 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05003501 break;
Chris Mason6407bf62007-03-27 06:33:00 -04003502 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003503 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04003504 BUG_ON(ret);
3505 break;
3506 }
Chris Masondb945352007-10-15 16:15:53 -04003507 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04003508 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04003509 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04003510
Chris Mason333db942008-06-25 16:01:30 -04003511 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04003512 BUG_ON(ret);
3513 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05003514 parent = path->nodes[*level];
3515 root_owner = btrfs_header_owner(parent);
3516 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05003517 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04003518
Chris Mason925baed2008-06-25 16:01:30 -04003519 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003520 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003521 root_owner, root_gen,
3522 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05003523 BUG_ON(ret);
Chris Mason18e35e02008-08-01 13:11:41 -04003524
3525 atomic_inc(&root->fs_info->throttle_gen);
3526 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04003527 cond_resched();
Chris Mason18e35e02008-08-01 13:11:41 -04003528
Chris Mason20524f02007-03-10 06:35:47 -05003529 continue;
3530 }
Chris Masonf87f0572008-08-01 11:27:23 -04003531 /*
3532 * at this point, we have a single ref, and since the
3533 * only place referencing this extent is a dead root
3534 * the reference count should never go higher.
3535 * So, we don't need to check it again
3536 */
Yan Zheng31153d82008-07-28 15:32:19 -04003537 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04003538 ref = btrfs_lookup_leaf_ref(root, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04003539 if (ref && ref->generation != ptr_gen) {
3540 btrfs_free_leaf_ref(root, ref);
3541 ref = NULL;
3542 }
Yan Zheng31153d82008-07-28 15:32:19 -04003543 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04003544 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003545 BUG_ON(ret);
3546 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04003547 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003548 *level = 0;
3549 break;
3550 }
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003551 if (printk_ratelimit()) {
Chris Mason37d1aee2008-07-31 10:48:37 -04003552 printk("leaf ref miss for bytenr %llu\n",
3553 (unsigned long long)bytenr);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003554 }
Yan Zheng31153d82008-07-28 15:32:19 -04003555 }
Chris Masondb945352007-10-15 16:15:53 -04003556 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04003557 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003558 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04003559
Chris Masonca7a79a2008-05-12 12:59:19 -04003560 next = read_tree_block(root, bytenr, blocksize,
3561 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04003562 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04003563#if 0
3564 /*
3565 * this is a debugging check and can go away
3566 * the ref should never go all the way down to 1
3567 * at this point
3568 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003569 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
3570 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04003571 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003572 WARN_ON(refs != 1);
3573#endif
Chris Masone9d0b132007-08-10 14:06:19 -04003574 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003575 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04003576 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04003577 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05003578 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04003579 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05003580 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04003581 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003582 }
3583out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003584 WARN_ON(*level < 0);
3585 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05003586
3587 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003588 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003589 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003590 } else {
3591 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003592 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003593 }
3594
Yan Zheng31153d82008-07-28 15:32:19 -04003595 blocksize = btrfs_level_size(root, *level);
3596 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003597 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003598
3599 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003600 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003601 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003602 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003603 path->nodes[*level] = NULL;
3604 *level += 1;
3605 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003606
Chris Masone7a84562008-06-25 16:01:31 -04003607 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003608 return 0;
3609}
3610
Chris Mason9aca1d52007-03-13 11:09:37 -04003611/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04003612 * helper function for drop_subtree, this function is similar to
3613 * walk_down_tree. The main difference is that it checks reference
3614 * counts while tree blocks are locked.
3615 */
3616static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
3617 struct btrfs_root *root,
3618 struct btrfs_path *path, int *level)
3619{
3620 struct extent_buffer *next;
3621 struct extent_buffer *cur;
3622 struct extent_buffer *parent;
3623 u64 bytenr;
3624 u64 ptr_gen;
3625 u32 blocksize;
3626 u32 refs;
3627 int ret;
3628
3629 cur = path->nodes[*level];
3630 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3631 &refs);
3632 BUG_ON(ret);
3633 if (refs > 1)
3634 goto out;
3635
3636 while (*level >= 0) {
3637 cur = path->nodes[*level];
3638 if (*level == 0) {
3639 ret = btrfs_drop_leaf_ref(trans, root, cur);
3640 BUG_ON(ret);
3641 clean_tree_block(trans, root, cur);
3642 break;
3643 }
3644 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3645 clean_tree_block(trans, root, cur);
3646 break;
3647 }
3648
3649 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3650 blocksize = btrfs_level_size(root, *level - 1);
3651 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3652
3653 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3654 btrfs_tree_lock(next);
3655
3656 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3657 &refs);
3658 BUG_ON(ret);
3659 if (refs > 1) {
3660 parent = path->nodes[*level];
3661 ret = btrfs_free_extent(trans, root, bytenr,
3662 blocksize, parent->start,
3663 btrfs_header_owner(parent),
3664 btrfs_header_generation(parent),
3665 *level - 1, 1);
3666 BUG_ON(ret);
3667 path->slots[*level]++;
3668 btrfs_tree_unlock(next);
3669 free_extent_buffer(next);
3670 continue;
3671 }
3672
3673 *level = btrfs_header_level(next);
3674 path->nodes[*level] = next;
3675 path->slots[*level] = 0;
3676 path->locks[*level] = 1;
3677 cond_resched();
3678 }
3679out:
3680 parent = path->nodes[*level + 1];
3681 bytenr = path->nodes[*level]->start;
3682 blocksize = path->nodes[*level]->len;
3683
3684 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3685 parent->start, btrfs_header_owner(parent),
3686 btrfs_header_generation(parent), *level, 1);
3687 BUG_ON(ret);
3688
3689 if (path->locks[*level]) {
3690 btrfs_tree_unlock(path->nodes[*level]);
3691 path->locks[*level] = 0;
3692 }
3693 free_extent_buffer(path->nodes[*level]);
3694 path->nodes[*level] = NULL;
3695 *level += 1;
3696 cond_resched();
3697 return 0;
3698}
3699
3700/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003701 * helper for dropping snapshots. This walks back up the tree in the path
3702 * to find the first node higher up where we haven't yet gone through
3703 * all the slots
3704 */
Chris Mason98ed5172008-01-03 10:01:48 -05003705static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3706 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003707 struct btrfs_path *path,
3708 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003709{
Chris Mason7bb86312007-12-11 09:25:06 -05003710 u64 root_owner;
3711 u64 root_gen;
3712 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003713 int i;
3714 int slot;
3715 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003716
Yan Zhengf82d02d2008-10-29 14:49:05 -04003717 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003718 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003719 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3720 struct extent_buffer *node;
3721 struct btrfs_disk_key disk_key;
3722 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003723 path->slots[i]++;
3724 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003725 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003726 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003727 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003728 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003729 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003730 return 0;
3731 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003732 struct extent_buffer *parent;
3733 if (path->nodes[*level] == root->node)
3734 parent = path->nodes[*level];
3735 else
3736 parent = path->nodes[*level + 1];
3737
3738 root_owner = btrfs_header_owner(parent);
3739 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003740
3741 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003742 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003743 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003744 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003745 parent->start, root_owner,
3746 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003747 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003748 if (path->locks[*level]) {
3749 btrfs_tree_unlock(path->nodes[*level]);
3750 path->locks[*level] = 0;
3751 }
Chris Mason5f39d392007-10-15 16:14:19 -04003752 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003753 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003754 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003755 }
3756 }
3757 return 1;
3758}
3759
Chris Mason9aca1d52007-03-13 11:09:37 -04003760/*
3761 * drop the reference count on the tree rooted at 'snap'. This traverses
3762 * the tree freeing any blocks that have a ref count of zero after being
3763 * decremented.
3764 */
Chris Masone089f052007-03-16 16:20:31 -04003765int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003766 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003767{
Chris Mason3768f362007-03-13 16:47:54 -04003768 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003769 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003770 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003771 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003772 int i;
3773 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003774 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003775
Chris Masona2135012008-06-25 16:01:30 -04003776 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003777 path = btrfs_alloc_path();
3778 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003779
Chris Mason5f39d392007-10-15 16:14:19 -04003780 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003781 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003782 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3783 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003784 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003785 path->slots[level] = 0;
3786 } else {
3787 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003788 struct btrfs_disk_key found_key;
3789 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003790
Chris Mason9f3a7422007-08-07 15:52:19 -04003791 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003792 level = root_item->drop_level;
3793 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003794 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003795 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003796 ret = wret;
3797 goto out;
3798 }
Chris Mason5f39d392007-10-15 16:14:19 -04003799 node = path->nodes[level];
3800 btrfs_node_key(node, &found_key, path->slots[level]);
3801 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3802 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003803 /*
3804 * unlock our path, this is safe because only this
3805 * function is allowed to delete this snapshot
3806 */
Chris Mason925baed2008-06-25 16:01:30 -04003807 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3808 if (path->nodes[i] && path->locks[i]) {
3809 path->locks[i] = 0;
3810 btrfs_tree_unlock(path->nodes[i]);
3811 }
3812 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003813 }
Chris Mason20524f02007-03-10 06:35:47 -05003814 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003815 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003816 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003817 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003818 if (wret < 0)
3819 ret = wret;
3820
Yan Zhengf82d02d2008-10-29 14:49:05 -04003821 wret = walk_up_tree(trans, root, path, &level,
3822 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003823 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003824 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003825 if (wret < 0)
3826 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003827 if (trans->transaction->in_commit) {
3828 ret = -EAGAIN;
3829 break;
3830 }
Chris Mason18e35e02008-08-01 13:11:41 -04003831 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003832 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003833 }
Chris Mason83e15a22007-03-12 09:03:27 -04003834 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003835 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003836 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003837 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003838 }
Chris Mason20524f02007-03-10 06:35:47 -05003839 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003840out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003841 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003842 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003843}
Chris Mason9078a3e2007-04-26 16:46:15 -04003844
Yan Zhengf82d02d2008-10-29 14:49:05 -04003845int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3846 struct btrfs_root *root,
3847 struct extent_buffer *node,
3848 struct extent_buffer *parent)
3849{
3850 struct btrfs_path *path;
3851 int level;
3852 int parent_level;
3853 int ret = 0;
3854 int wret;
3855
3856 path = btrfs_alloc_path();
3857 BUG_ON(!path);
3858
3859 BUG_ON(!btrfs_tree_locked(parent));
3860 parent_level = btrfs_header_level(parent);
3861 extent_buffer_get(parent);
3862 path->nodes[parent_level] = parent;
3863 path->slots[parent_level] = btrfs_header_nritems(parent);
3864
3865 BUG_ON(!btrfs_tree_locked(node));
3866 level = btrfs_header_level(node);
3867 extent_buffer_get(node);
3868 path->nodes[level] = node;
3869 path->slots[level] = 0;
3870
3871 while (1) {
3872 wret = walk_down_subtree(trans, root, path, &level);
3873 if (wret < 0)
3874 ret = wret;
3875 if (wret != 0)
3876 break;
3877
3878 wret = walk_up_tree(trans, root, path, &level, parent_level);
3879 if (wret < 0)
3880 ret = wret;
3881 if (wret != 0)
3882 break;
3883 }
3884
3885 btrfs_free_path(path);
3886 return ret;
3887}
3888
Chris Mason8e7bf942008-04-28 09:02:36 -04003889static unsigned long calc_ra(unsigned long start, unsigned long last,
3890 unsigned long nr)
3891{
3892 return min(last, start + nr - 1);
3893}
3894
Chris Mason98ed5172008-01-03 10:01:48 -05003895static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3896 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003897{
3898 u64 page_start;
3899 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003900 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003901 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003902 unsigned long i;
3903 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003904 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003905 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003906 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003907 unsigned int total_read = 0;
3908 unsigned int total_dirty = 0;
3909 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003910
3911 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003912
3913 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003914 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003915 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3916
Zheng Yan1a40e232008-09-26 10:09:34 -04003917 /* make sure the dirty trick played by the caller work */
3918 ret = invalidate_inode_pages2_range(inode->i_mapping,
3919 first_index, last_index);
3920 if (ret)
3921 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003922
Chris Mason4313b392008-01-03 09:08:48 -05003923 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003924
Zheng Yan1a40e232008-09-26 10:09:34 -04003925 for (i = first_index ; i <= last_index; i++) {
3926 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003927 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003928 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003929 }
3930 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003931again:
3932 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003933 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003934 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003935 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003936 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003937 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003938 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003939 if (!PageUptodate(page)) {
3940 btrfs_readpage(NULL, page);
3941 lock_page(page);
3942 if (!PageUptodate(page)) {
3943 unlock_page(page);
3944 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003945 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003946 goto out_unlock;
3947 }
3948 }
Chris Masonec44a352008-04-28 15:29:52 -04003949 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003950
Chris Masonedbd8d42007-12-21 16:27:24 -05003951 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3952 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003953 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003954
Chris Mason3eaa2882008-07-24 11:57:52 -04003955 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3956 if (ordered) {
3957 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3958 unlock_page(page);
3959 page_cache_release(page);
3960 btrfs_start_ordered_extent(inode, ordered, 1);
3961 btrfs_put_ordered_extent(ordered);
3962 goto again;
3963 }
3964 set_page_extent_mapped(page);
3965
Chris Masonea8c2812008-08-04 23:17:27 -04003966 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003967 if (i == first_index)
3968 set_extent_bits(io_tree, page_start, page_end,
3969 EXTENT_BOUNDARY, GFP_NOFS);
3970
Chris Masona061fc82008-05-07 11:43:44 -04003971 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003972 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003973
Chris Masond1310b22008-01-24 16:13:08 -05003974 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003975 unlock_page(page);
3976 page_cache_release(page);
3977 }
3978
3979out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04003980 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05003981 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003982 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04003983 return ret;
3984}
3985
Zheng Yan1a40e232008-09-26 10:09:34 -04003986static int noinline relocate_data_extent(struct inode *reloc_inode,
3987 struct btrfs_key *extent_key,
3988 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05003989{
Zheng Yan1a40e232008-09-26 10:09:34 -04003990 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3991 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
3992 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04003993 u64 start = extent_key->objectid - offset;
3994 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04003995
3996 em = alloc_extent_map(GFP_NOFS);
3997 BUG_ON(!em || IS_ERR(em));
3998
Yan Zheng66435582008-10-30 14:19:50 -04003999 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004000 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004001 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004002 em->block_start = extent_key->objectid;
4003 em->bdev = root->fs_info->fs_devices->latest_bdev;
4004 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4005
4006 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04004007 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004008 while (1) {
4009 int ret;
4010 spin_lock(&em_tree->lock);
4011 ret = add_extent_mapping(em_tree, em);
4012 spin_unlock(&em_tree->lock);
4013 if (ret != -EEXIST) {
4014 free_extent_map(em);
4015 break;
4016 }
Yan Zheng66435582008-10-30 14:19:50 -04004017 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004018 }
Yan Zheng66435582008-10-30 14:19:50 -04004019 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004020
Yan Zheng66435582008-10-30 14:19:50 -04004021 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04004022}
4023
4024struct btrfs_ref_path {
4025 u64 extent_start;
4026 u64 nodes[BTRFS_MAX_LEVEL];
4027 u64 root_objectid;
4028 u64 root_generation;
4029 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004030 u32 num_refs;
4031 int lowest_level;
4032 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004033 int shared_level;
4034
4035 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4036 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04004037};
4038
4039struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04004040 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04004041 u64 disk_bytenr;
4042 u64 disk_num_bytes;
4043 u64 offset;
4044 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04004045 u8 compression;
4046 u8 encryption;
4047 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04004048};
4049
4050static int is_cowonly_root(u64 root_objectid)
4051{
4052 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4053 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4054 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4055 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
4056 root_objectid == BTRFS_TREE_LOG_OBJECTID)
4057 return 1;
4058 return 0;
4059}
4060
4061static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
4062 struct btrfs_root *extent_root,
4063 struct btrfs_ref_path *ref_path,
4064 int first_time)
4065{
4066 struct extent_buffer *leaf;
4067 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05004068 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05004069 struct btrfs_key key;
4070 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04004071 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05004072 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04004073 int level;
4074 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05004075
Zheng Yan1a40e232008-09-26 10:09:34 -04004076 path = btrfs_alloc_path();
4077 if (!path)
4078 return -ENOMEM;
4079
Zheng Yan1a40e232008-09-26 10:09:34 -04004080 if (first_time) {
4081 ref_path->lowest_level = -1;
4082 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004083 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004084 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04004085 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004086walk_down:
4087 level = ref_path->current_level - 1;
4088 while (level >= -1) {
4089 u64 parent;
4090 if (level < ref_path->lowest_level)
4091 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004092
Zheng Yan1a40e232008-09-26 10:09:34 -04004093 if (level >= 0) {
4094 bytenr = ref_path->nodes[level];
4095 } else {
4096 bytenr = ref_path->extent_start;
4097 }
4098 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004099
Zheng Yan1a40e232008-09-26 10:09:34 -04004100 parent = ref_path->nodes[level + 1];
4101 ref_path->nodes[level + 1] = 0;
4102 ref_path->current_level = level;
4103 BUG_ON(parent == 0);
4104
4105 key.objectid = bytenr;
4106 key.offset = parent + 1;
4107 key.type = BTRFS_EXTENT_REF_KEY;
4108
4109 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004110 if (ret < 0)
4111 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004112 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004113
Chris Masonedbd8d42007-12-21 16:27:24 -05004114 leaf = path->nodes[0];
4115 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04004116 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04004117 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04004118 if (ret < 0)
4119 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004120 if (ret > 0)
4121 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04004122 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04004123 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004124
4125 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04004126 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04004127 found_key.type == BTRFS_EXTENT_REF_KEY) {
4128 if (level < ref_path->shared_level)
4129 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004130 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004131 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004132next:
4133 level--;
4134 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004135 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004136 }
4137 /* reached lowest level */
4138 ret = 1;
4139 goto out;
4140walk_up:
4141 level = ref_path->current_level;
4142 while (level < BTRFS_MAX_LEVEL - 1) {
4143 u64 ref_objectid;
4144 if (level >= 0) {
4145 bytenr = ref_path->nodes[level];
4146 } else {
4147 bytenr = ref_path->extent_start;
Chris Masona061fc82008-05-07 11:43:44 -04004148 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004149 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004150
Zheng Yan1a40e232008-09-26 10:09:34 -04004151 key.objectid = bytenr;
4152 key.offset = 0;
4153 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05004154
Zheng Yan1a40e232008-09-26 10:09:34 -04004155 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4156 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05004157 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004158
4159 leaf = path->nodes[0];
4160 nritems = btrfs_header_nritems(leaf);
4161 if (path->slots[0] >= nritems) {
4162 ret = btrfs_next_leaf(extent_root, path);
4163 if (ret < 0)
4164 goto out;
4165 if (ret > 0) {
4166 /* the extent was freed by someone */
4167 if (ref_path->lowest_level == level)
4168 goto out;
4169 btrfs_release_path(extent_root, path);
4170 goto walk_down;
4171 }
4172 leaf = path->nodes[0];
4173 }
4174
4175 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4176 if (found_key.objectid != bytenr ||
4177 found_key.type != BTRFS_EXTENT_REF_KEY) {
4178 /* the extent was freed by someone */
4179 if (ref_path->lowest_level == level) {
4180 ret = 1;
4181 goto out;
4182 }
4183 btrfs_release_path(extent_root, path);
4184 goto walk_down;
4185 }
4186found:
4187 ref = btrfs_item_ptr(leaf, path->slots[0],
4188 struct btrfs_extent_ref);
4189 ref_objectid = btrfs_ref_objectid(leaf, ref);
4190 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4191 if (first_time) {
4192 level = (int)ref_objectid;
4193 BUG_ON(level >= BTRFS_MAX_LEVEL);
4194 ref_path->lowest_level = level;
4195 ref_path->current_level = level;
4196 ref_path->nodes[level] = bytenr;
4197 } else {
4198 WARN_ON(ref_objectid != level);
4199 }
4200 } else {
4201 WARN_ON(level != -1);
4202 }
4203 first_time = 0;
4204
4205 if (ref_path->lowest_level == level) {
4206 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004207 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4208 }
4209
4210 /*
4211 * the block is tree root or the block isn't in reference
4212 * counted tree.
4213 */
4214 if (found_key.objectid == found_key.offset ||
4215 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4216 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4217 ref_path->root_generation =
4218 btrfs_ref_generation(leaf, ref);
4219 if (level < 0) {
4220 /* special reference from the tree log */
4221 ref_path->nodes[0] = found_key.offset;
4222 ref_path->current_level = 0;
4223 }
4224 ret = 0;
4225 goto out;
4226 }
4227
4228 level++;
4229 BUG_ON(ref_path->nodes[level] != 0);
4230 ref_path->nodes[level] = found_key.offset;
4231 ref_path->current_level = level;
4232
4233 /*
4234 * the reference was created in the running transaction,
4235 * no need to continue walking up.
4236 */
4237 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4238 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4239 ref_path->root_generation =
4240 btrfs_ref_generation(leaf, ref);
4241 ret = 0;
4242 goto out;
4243 }
4244
4245 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004246 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004247 }
4248 /* reached max tree level, but no tree root found. */
4249 BUG();
4250out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004251 btrfs_free_path(path);
4252 return ret;
4253}
4254
4255static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4256 struct btrfs_root *extent_root,
4257 struct btrfs_ref_path *ref_path,
4258 u64 extent_start)
4259{
4260 memset(ref_path, 0, sizeof(*ref_path));
4261 ref_path->extent_start = extent_start;
4262
4263 return __next_ref_path(trans, extent_root, ref_path, 1);
4264}
4265
4266static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4267 struct btrfs_root *extent_root,
4268 struct btrfs_ref_path *ref_path)
4269{
4270 return __next_ref_path(trans, extent_root, ref_path, 0);
4271}
4272
4273static int noinline get_new_locations(struct inode *reloc_inode,
4274 struct btrfs_key *extent_key,
4275 u64 offset, int no_fragment,
4276 struct disk_extent **extents,
4277 int *nr_extents)
4278{
4279 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4280 struct btrfs_path *path;
4281 struct btrfs_file_extent_item *fi;
4282 struct extent_buffer *leaf;
4283 struct disk_extent *exts = *extents;
4284 struct btrfs_key found_key;
4285 u64 cur_pos;
4286 u64 last_byte;
4287 u32 nritems;
4288 int nr = 0;
4289 int max = *nr_extents;
4290 int ret;
4291
4292 WARN_ON(!no_fragment && *extents);
4293 if (!exts) {
4294 max = 1;
4295 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4296 if (!exts)
4297 return -ENOMEM;
4298 }
4299
4300 path = btrfs_alloc_path();
4301 BUG_ON(!path);
4302
4303 cur_pos = extent_key->objectid - offset;
4304 last_byte = extent_key->objectid + extent_key->offset;
4305 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4306 cur_pos, 0);
4307 if (ret < 0)
4308 goto out;
4309 if (ret > 0) {
4310 ret = -ENOENT;
4311 goto out;
4312 }
4313
4314 while (1) {
4315 leaf = path->nodes[0];
4316 nritems = btrfs_header_nritems(leaf);
4317 if (path->slots[0] >= nritems) {
4318 ret = btrfs_next_leaf(root, path);
4319 if (ret < 0)
4320 goto out;
4321 if (ret > 0)
4322 break;
4323 leaf = path->nodes[0];
4324 }
4325
4326 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4327 if (found_key.offset != cur_pos ||
4328 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4329 found_key.objectid != reloc_inode->i_ino)
4330 break;
4331
4332 fi = btrfs_item_ptr(leaf, path->slots[0],
4333 struct btrfs_file_extent_item);
4334 if (btrfs_file_extent_type(leaf, fi) !=
4335 BTRFS_FILE_EXTENT_REG ||
4336 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4337 break;
4338
4339 if (nr == max) {
4340 struct disk_extent *old = exts;
4341 max *= 2;
4342 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4343 memcpy(exts, old, sizeof(*exts) * nr);
4344 if (old != *extents)
4345 kfree(old);
4346 }
4347
4348 exts[nr].disk_bytenr =
4349 btrfs_file_extent_disk_bytenr(leaf, fi);
4350 exts[nr].disk_num_bytes =
4351 btrfs_file_extent_disk_num_bytes(leaf, fi);
4352 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4353 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04004354 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4355 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4356 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4357 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4358 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04004359 BUG_ON(exts[nr].offset > 0);
4360 BUG_ON(exts[nr].compression || exts[nr].encryption);
4361 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004362
4363 cur_pos += exts[nr].num_bytes;
4364 nr++;
4365
4366 if (cur_pos + offset >= last_byte)
4367 break;
4368
4369 if (no_fragment) {
4370 ret = 1;
4371 goto out;
4372 }
4373 path->slots[0]++;
4374 }
4375
4376 WARN_ON(cur_pos + offset > last_byte);
4377 if (cur_pos + offset < last_byte) {
4378 ret = -ENOENT;
4379 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05004380 }
4381 ret = 0;
4382out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004383 btrfs_free_path(path);
4384 if (ret) {
4385 if (exts != *extents)
4386 kfree(exts);
4387 } else {
4388 *extents = exts;
4389 *nr_extents = nr;
4390 }
4391 return ret;
4392}
4393
4394static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
4395 struct btrfs_root *root,
4396 struct btrfs_path *path,
4397 struct btrfs_key *extent_key,
4398 struct btrfs_key *leaf_key,
4399 struct btrfs_ref_path *ref_path,
4400 struct disk_extent *new_extents,
4401 int nr_extents)
4402{
4403 struct extent_buffer *leaf;
4404 struct btrfs_file_extent_item *fi;
4405 struct inode *inode = NULL;
4406 struct btrfs_key key;
4407 u64 lock_start = 0;
4408 u64 lock_end = 0;
4409 u64 num_bytes;
4410 u64 ext_offset;
4411 u64 first_pos;
4412 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004413 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004414 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04004415 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04004416 int ret;
4417
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004418 memcpy(&key, leaf_key, sizeof(key));
4419 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004420 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004421 if (key.objectid < ref_path->owner_objectid ||
4422 (key.objectid == ref_path->owner_objectid &&
4423 key.type < BTRFS_EXTENT_DATA_KEY)) {
4424 key.objectid = ref_path->owner_objectid;
4425 key.type = BTRFS_EXTENT_DATA_KEY;
4426 key.offset = 0;
4427 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004428 }
4429
4430 while (1) {
4431 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4432 if (ret < 0)
4433 goto out;
4434
4435 leaf = path->nodes[0];
4436 nritems = btrfs_header_nritems(leaf);
4437next:
4438 if (extent_locked && ret > 0) {
4439 /*
4440 * the file extent item was modified by someone
4441 * before the extent got locked.
4442 */
Zheng Yan1a40e232008-09-26 10:09:34 -04004443 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4444 lock_end, GFP_NOFS);
4445 extent_locked = 0;
4446 }
4447
4448 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004449 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04004450 break;
4451
4452 BUG_ON(extent_locked);
4453 ret = btrfs_next_leaf(root, path);
4454 if (ret < 0)
4455 goto out;
4456 if (ret > 0)
4457 break;
4458 leaf = path->nodes[0];
4459 nritems = btrfs_header_nritems(leaf);
4460 }
4461
4462 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4463
4464 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4465 if ((key.objectid > ref_path->owner_objectid) ||
4466 (key.objectid == ref_path->owner_objectid &&
4467 key.type > BTRFS_EXTENT_DATA_KEY) ||
4468 (key.offset >= first_pos + extent_key->offset))
4469 break;
4470 }
4471
4472 if (inode && key.objectid != inode->i_ino) {
4473 BUG_ON(extent_locked);
4474 btrfs_release_path(root, path);
4475 mutex_unlock(&inode->i_mutex);
4476 iput(inode);
4477 inode = NULL;
4478 continue;
4479 }
4480
4481 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4482 path->slots[0]++;
4483 ret = 1;
4484 goto next;
4485 }
4486 fi = btrfs_item_ptr(leaf, path->slots[0],
4487 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04004488 extent_type = btrfs_file_extent_type(leaf, fi);
4489 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4490 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04004491 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4492 extent_key->objectid)) {
4493 path->slots[0]++;
4494 ret = 1;
4495 goto next;
4496 }
4497
4498 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4499 ext_offset = btrfs_file_extent_offset(leaf, fi);
4500
4501 if (first_pos > key.offset - ext_offset)
4502 first_pos = key.offset - ext_offset;
4503
4504 if (!extent_locked) {
4505 lock_start = key.offset;
4506 lock_end = lock_start + num_bytes - 1;
4507 } else {
Yan Zheng66435582008-10-30 14:19:50 -04004508 if (lock_start > key.offset ||
4509 lock_end + 1 < key.offset + num_bytes) {
4510 unlock_extent(&BTRFS_I(inode)->io_tree,
4511 lock_start, lock_end, GFP_NOFS);
4512 extent_locked = 0;
4513 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004514 }
4515
4516 if (!inode) {
4517 btrfs_release_path(root, path);
4518
4519 inode = btrfs_iget_locked(root->fs_info->sb,
4520 key.objectid, root);
4521 if (inode->i_state & I_NEW) {
4522 BTRFS_I(inode)->root = root;
4523 BTRFS_I(inode)->location.objectid =
4524 key.objectid;
4525 BTRFS_I(inode)->location.type =
4526 BTRFS_INODE_ITEM_KEY;
4527 BTRFS_I(inode)->location.offset = 0;
4528 btrfs_read_locked_inode(inode);
4529 unlock_new_inode(inode);
4530 }
4531 /*
4532 * some code call btrfs_commit_transaction while
4533 * holding the i_mutex, so we can't use mutex_lock
4534 * here.
4535 */
4536 if (is_bad_inode(inode) ||
4537 !mutex_trylock(&inode->i_mutex)) {
4538 iput(inode);
4539 inode = NULL;
4540 key.offset = (u64)-1;
4541 goto skip;
4542 }
4543 }
4544
4545 if (!extent_locked) {
4546 struct btrfs_ordered_extent *ordered;
4547
4548 btrfs_release_path(root, path);
4549
4550 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4551 lock_end, GFP_NOFS);
4552 ordered = btrfs_lookup_first_ordered_extent(inode,
4553 lock_end);
4554 if (ordered &&
4555 ordered->file_offset <= lock_end &&
4556 ordered->file_offset + ordered->len > lock_start) {
4557 unlock_extent(&BTRFS_I(inode)->io_tree,
4558 lock_start, lock_end, GFP_NOFS);
4559 btrfs_start_ordered_extent(inode, ordered, 1);
4560 btrfs_put_ordered_extent(ordered);
4561 key.offset += num_bytes;
4562 goto skip;
4563 }
4564 if (ordered)
4565 btrfs_put_ordered_extent(ordered);
4566
Zheng Yan1a40e232008-09-26 10:09:34 -04004567 extent_locked = 1;
4568 continue;
4569 }
4570
4571 if (nr_extents == 1) {
4572 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04004573 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4574 new_extents[0].disk_bytenr);
4575 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4576 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004577 btrfs_mark_buffer_dirty(leaf);
4578
4579 btrfs_drop_extent_cache(inode, key.offset,
4580 key.offset + num_bytes - 1, 0);
4581
4582 ret = btrfs_inc_extent_ref(trans, root,
4583 new_extents[0].disk_bytenr,
4584 new_extents[0].disk_num_bytes,
4585 leaf->start,
4586 root->root_key.objectid,
4587 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004588 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004589 BUG_ON(ret);
4590
4591 ret = btrfs_free_extent(trans, root,
4592 extent_key->objectid,
4593 extent_key->offset,
4594 leaf->start,
4595 btrfs_header_owner(leaf),
4596 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004597 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004598 BUG_ON(ret);
4599
4600 btrfs_release_path(root, path);
4601 key.offset += num_bytes;
4602 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04004603 BUG_ON(1);
4604#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04004605 u64 alloc_hint;
4606 u64 extent_len;
4607 int i;
4608 /*
4609 * drop old extent pointer at first, then insert the
4610 * new pointers one bye one
4611 */
4612 btrfs_release_path(root, path);
4613 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4614 key.offset + num_bytes,
4615 key.offset, &alloc_hint);
4616 BUG_ON(ret);
4617
4618 for (i = 0; i < nr_extents; i++) {
4619 if (ext_offset >= new_extents[i].num_bytes) {
4620 ext_offset -= new_extents[i].num_bytes;
4621 continue;
4622 }
4623 extent_len = min(new_extents[i].num_bytes -
4624 ext_offset, num_bytes);
4625
4626 ret = btrfs_insert_empty_item(trans, root,
4627 path, &key,
4628 sizeof(*fi));
4629 BUG_ON(ret);
4630
4631 leaf = path->nodes[0];
4632 fi = btrfs_item_ptr(leaf, path->slots[0],
4633 struct btrfs_file_extent_item);
4634 btrfs_set_file_extent_generation(leaf, fi,
4635 trans->transid);
4636 btrfs_set_file_extent_type(leaf, fi,
4637 BTRFS_FILE_EXTENT_REG);
4638 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4639 new_extents[i].disk_bytenr);
4640 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4641 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004642 btrfs_set_file_extent_ram_bytes(leaf, fi,
4643 new_extents[i].ram_bytes);
4644
4645 btrfs_set_file_extent_compression(leaf, fi,
4646 new_extents[i].compression);
4647 btrfs_set_file_extent_encryption(leaf, fi,
4648 new_extents[i].encryption);
4649 btrfs_set_file_extent_other_encoding(leaf, fi,
4650 new_extents[i].other_encoding);
4651
Zheng Yan1a40e232008-09-26 10:09:34 -04004652 btrfs_set_file_extent_num_bytes(leaf, fi,
4653 extent_len);
4654 ext_offset += new_extents[i].offset;
4655 btrfs_set_file_extent_offset(leaf, fi,
4656 ext_offset);
4657 btrfs_mark_buffer_dirty(leaf);
4658
4659 btrfs_drop_extent_cache(inode, key.offset,
4660 key.offset + extent_len - 1, 0);
4661
4662 ret = btrfs_inc_extent_ref(trans, root,
4663 new_extents[i].disk_bytenr,
4664 new_extents[i].disk_num_bytes,
4665 leaf->start,
4666 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004667 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004668 BUG_ON(ret);
4669 btrfs_release_path(root, path);
4670
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004671 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004672
4673 ext_offset = 0;
4674 num_bytes -= extent_len;
4675 key.offset += extent_len;
4676
4677 if (num_bytes == 0)
4678 break;
4679 }
4680 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004681#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04004682 }
4683
4684 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004685 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4686 lock_end, GFP_NOFS);
4687 extent_locked = 0;
4688 }
4689skip:
4690 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4691 key.offset >= first_pos + extent_key->offset)
4692 break;
4693
4694 cond_resched();
4695 }
4696 ret = 0;
4697out:
4698 btrfs_release_path(root, path);
4699 if (inode) {
4700 mutex_unlock(&inode->i_mutex);
4701 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004702 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4703 lock_end, GFP_NOFS);
4704 }
4705 iput(inode);
4706 }
4707 return ret;
4708}
4709
Zheng Yan1a40e232008-09-26 10:09:34 -04004710int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4711 struct btrfs_root *root,
4712 struct extent_buffer *buf, u64 orig_start)
4713{
4714 int level;
4715 int ret;
4716
4717 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4718 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4719
4720 level = btrfs_header_level(buf);
4721 if (level == 0) {
4722 struct btrfs_leaf_ref *ref;
4723 struct btrfs_leaf_ref *orig_ref;
4724
4725 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4726 if (!orig_ref)
4727 return -ENOENT;
4728
4729 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4730 if (!ref) {
4731 btrfs_free_leaf_ref(root, orig_ref);
4732 return -ENOMEM;
4733 }
4734
4735 ref->nritems = orig_ref->nritems;
4736 memcpy(ref->extents, orig_ref->extents,
4737 sizeof(ref->extents[0]) * ref->nritems);
4738
4739 btrfs_free_leaf_ref(root, orig_ref);
4740
4741 ref->root_gen = trans->transid;
4742 ref->bytenr = buf->start;
4743 ref->owner = btrfs_header_owner(buf);
4744 ref->generation = btrfs_header_generation(buf);
4745 ret = btrfs_add_leaf_ref(root, ref, 0);
4746 WARN_ON(ret);
4747 btrfs_free_leaf_ref(root, ref);
4748 }
4749 return 0;
4750}
4751
4752static int noinline invalidate_extent_cache(struct btrfs_root *root,
4753 struct extent_buffer *leaf,
4754 struct btrfs_block_group_cache *group,
4755 struct btrfs_root *target_root)
4756{
4757 struct btrfs_key key;
4758 struct inode *inode = NULL;
4759 struct btrfs_file_extent_item *fi;
4760 u64 num_bytes;
4761 u64 skip_objectid = 0;
4762 u32 nritems;
4763 u32 i;
4764
4765 nritems = btrfs_header_nritems(leaf);
4766 for (i = 0; i < nritems; i++) {
4767 btrfs_item_key_to_cpu(leaf, &key, i);
4768 if (key.objectid == skip_objectid ||
4769 key.type != BTRFS_EXTENT_DATA_KEY)
4770 continue;
4771 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4772 if (btrfs_file_extent_type(leaf, fi) ==
4773 BTRFS_FILE_EXTENT_INLINE)
4774 continue;
4775 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4776 continue;
4777 if (!inode || inode->i_ino != key.objectid) {
4778 iput(inode);
4779 inode = btrfs_ilookup(target_root->fs_info->sb,
4780 key.objectid, target_root, 1);
4781 }
4782 if (!inode) {
4783 skip_objectid = key.objectid;
4784 continue;
4785 }
4786 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4787
4788 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4789 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004790 btrfs_drop_extent_cache(inode, key.offset,
4791 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004792 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4793 key.offset + num_bytes - 1, GFP_NOFS);
4794 cond_resched();
4795 }
4796 iput(inode);
4797 return 0;
4798}
4799
4800static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4801 struct btrfs_root *root,
4802 struct extent_buffer *leaf,
4803 struct btrfs_block_group_cache *group,
4804 struct inode *reloc_inode)
4805{
4806 struct btrfs_key key;
4807 struct btrfs_key extent_key;
4808 struct btrfs_file_extent_item *fi;
4809 struct btrfs_leaf_ref *ref;
4810 struct disk_extent *new_extent;
4811 u64 bytenr;
4812 u64 num_bytes;
4813 u32 nritems;
4814 u32 i;
4815 int ext_index;
4816 int nr_extent;
4817 int ret;
4818
4819 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4820 BUG_ON(!new_extent);
4821
4822 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4823 BUG_ON(!ref);
4824
4825 ext_index = -1;
4826 nritems = btrfs_header_nritems(leaf);
4827 for (i = 0; i < nritems; i++) {
4828 btrfs_item_key_to_cpu(leaf, &key, i);
4829 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4830 continue;
4831 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4832 if (btrfs_file_extent_type(leaf, fi) ==
4833 BTRFS_FILE_EXTENT_INLINE)
4834 continue;
4835 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4836 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4837 if (bytenr == 0)
4838 continue;
4839
4840 ext_index++;
4841 if (bytenr >= group->key.objectid + group->key.offset ||
4842 bytenr + num_bytes <= group->key.objectid)
4843 continue;
4844
4845 extent_key.objectid = bytenr;
4846 extent_key.offset = num_bytes;
4847 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4848 nr_extent = 1;
4849 ret = get_new_locations(reloc_inode, &extent_key,
4850 group->key.objectid, 1,
4851 &new_extent, &nr_extent);
4852 if (ret > 0)
4853 continue;
4854 BUG_ON(ret < 0);
4855
4856 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4857 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4858 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4859 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4860
Zheng Yan1a40e232008-09-26 10:09:34 -04004861 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4862 new_extent->disk_bytenr);
4863 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4864 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004865 btrfs_mark_buffer_dirty(leaf);
4866
4867 ret = btrfs_inc_extent_ref(trans, root,
4868 new_extent->disk_bytenr,
4869 new_extent->disk_num_bytes,
4870 leaf->start,
4871 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004872 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004873 BUG_ON(ret);
4874 ret = btrfs_free_extent(trans, root,
4875 bytenr, num_bytes, leaf->start,
4876 btrfs_header_owner(leaf),
4877 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004878 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004879 BUG_ON(ret);
4880 cond_resched();
4881 }
4882 kfree(new_extent);
4883 BUG_ON(ext_index + 1 != ref->nritems);
4884 btrfs_free_leaf_ref(root, ref);
4885 return 0;
4886}
4887
Yan Zhengf82d02d2008-10-29 14:49:05 -04004888int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4889 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004890{
4891 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004892 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004893
4894 if (root->reloc_root) {
4895 reloc_root = root->reloc_root;
4896 root->reloc_root = NULL;
4897 list_add(&reloc_root->dead_list,
4898 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004899
4900 btrfs_set_root_bytenr(&reloc_root->root_item,
4901 reloc_root->node->start);
4902 btrfs_set_root_level(&root->root_item,
4903 btrfs_header_level(reloc_root->node));
4904 memset(&reloc_root->root_item.drop_progress, 0,
4905 sizeof(struct btrfs_disk_key));
4906 reloc_root->root_item.drop_level = 0;
4907
4908 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4909 &reloc_root->root_key,
4910 &reloc_root->root_item);
4911 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004912 }
4913 return 0;
4914}
4915
4916int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4917{
4918 struct btrfs_trans_handle *trans;
4919 struct btrfs_root *reloc_root;
4920 struct btrfs_root *prev_root = NULL;
4921 struct list_head dead_roots;
4922 int ret;
4923 unsigned long nr;
4924
4925 INIT_LIST_HEAD(&dead_roots);
4926 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4927
4928 while (!list_empty(&dead_roots)) {
4929 reloc_root = list_entry(dead_roots.prev,
4930 struct btrfs_root, dead_list);
4931 list_del_init(&reloc_root->dead_list);
4932
4933 BUG_ON(reloc_root->commit_root != NULL);
4934 while (1) {
4935 trans = btrfs_join_transaction(root, 1);
4936 BUG_ON(!trans);
4937
4938 mutex_lock(&root->fs_info->drop_mutex);
4939 ret = btrfs_drop_snapshot(trans, reloc_root);
4940 if (ret != -EAGAIN)
4941 break;
4942 mutex_unlock(&root->fs_info->drop_mutex);
4943
4944 nr = trans->blocks_used;
4945 ret = btrfs_end_transaction(trans, root);
4946 BUG_ON(ret);
4947 btrfs_btree_balance_dirty(root, nr);
4948 }
4949
4950 free_extent_buffer(reloc_root->node);
4951
4952 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4953 &reloc_root->root_key);
4954 BUG_ON(ret);
4955 mutex_unlock(&root->fs_info->drop_mutex);
4956
4957 nr = trans->blocks_used;
4958 ret = btrfs_end_transaction(trans, root);
4959 BUG_ON(ret);
4960 btrfs_btree_balance_dirty(root, nr);
4961
4962 kfree(prev_root);
4963 prev_root = reloc_root;
4964 }
4965 if (prev_root) {
4966 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4967 kfree(prev_root);
4968 }
4969 return 0;
4970}
4971
4972int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4973{
4974 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4975 return 0;
4976}
4977
4978int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4979{
4980 struct btrfs_root *reloc_root;
4981 struct btrfs_trans_handle *trans;
4982 struct btrfs_key location;
4983 int found;
4984 int ret;
4985
4986 mutex_lock(&root->fs_info->tree_reloc_mutex);
4987 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4988 BUG_ON(ret);
4989 found = !list_empty(&root->fs_info->dead_reloc_roots);
4990 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4991
4992 if (found) {
4993 trans = btrfs_start_transaction(root, 1);
4994 BUG_ON(!trans);
4995 ret = btrfs_commit_transaction(trans, root);
4996 BUG_ON(ret);
4997 }
4998
4999 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5000 location.offset = (u64)-1;
5001 location.type = BTRFS_ROOT_ITEM_KEY;
5002
5003 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5004 BUG_ON(!reloc_root);
5005 btrfs_orphan_cleanup(reloc_root);
5006 return 0;
5007}
5008
5009static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
5010 struct btrfs_root *root)
5011{
5012 struct btrfs_root *reloc_root;
5013 struct extent_buffer *eb;
5014 struct btrfs_root_item *root_item;
5015 struct btrfs_key root_key;
5016 int ret;
5017
5018 BUG_ON(!root->ref_cows);
5019 if (root->reloc_root)
5020 return 0;
5021
5022 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5023 BUG_ON(!root_item);
5024
5025 ret = btrfs_copy_root(trans, root, root->commit_root,
5026 &eb, BTRFS_TREE_RELOC_OBJECTID);
5027 BUG_ON(ret);
5028
5029 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5030 root_key.offset = root->root_key.objectid;
5031 root_key.type = BTRFS_ROOT_ITEM_KEY;
5032
5033 memcpy(root_item, &root->root_item, sizeof(root_item));
5034 btrfs_set_root_refs(root_item, 0);
5035 btrfs_set_root_bytenr(root_item, eb->start);
5036 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04005037 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005038
5039 btrfs_tree_unlock(eb);
5040 free_extent_buffer(eb);
5041
5042 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5043 &root_key, root_item);
5044 BUG_ON(ret);
5045 kfree(root_item);
5046
5047 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5048 &root_key);
5049 BUG_ON(!reloc_root);
5050 reloc_root->last_trans = trans->transid;
5051 reloc_root->commit_root = NULL;
5052 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5053
5054 root->reloc_root = reloc_root;
5055 return 0;
5056}
5057
5058/*
5059 * Core function of space balance.
5060 *
5061 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04005062 * counted roots. There is one reloc tree for each subvol, and all
5063 * reloc trees share same root key objectid. Reloc trees are snapshots
5064 * of the latest committed roots of subvols (root->commit_root).
5065 *
5066 * To relocate a tree block referenced by a subvol, there are two steps.
5067 * COW the block through subvol's reloc tree, then update block pointer
5068 * in the subvol to point to the new block. Since all reloc trees share
5069 * same root key objectid, doing special handing for tree blocks owned
5070 * by them is easy. Once a tree block has been COWed in one reloc tree,
5071 * we can use the resulting new block directly when the same block is
5072 * required to COW again through other reloc trees. By this way, relocated
5073 * tree blocks are shared between reloc trees, so they are also shared
5074 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04005075 */
5076static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
5077 struct btrfs_root *root,
5078 struct btrfs_path *path,
5079 struct btrfs_key *first_key,
5080 struct btrfs_ref_path *ref_path,
5081 struct btrfs_block_group_cache *group,
5082 struct inode *reloc_inode)
5083{
5084 struct btrfs_root *reloc_root;
5085 struct extent_buffer *eb = NULL;
5086 struct btrfs_key *keys;
5087 u64 *nodes;
5088 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005089 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005090 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005091 int ret;
5092
5093 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5094 lowest_level = ref_path->owner_objectid;
5095
Yan Zhengf82d02d2008-10-29 14:49:05 -04005096 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005097 path->lowest_level = lowest_level;
5098 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5099 BUG_ON(ret < 0);
5100 path->lowest_level = 0;
5101 btrfs_release_path(root, path);
5102 return 0;
5103 }
5104
Zheng Yan1a40e232008-09-26 10:09:34 -04005105 mutex_lock(&root->fs_info->tree_reloc_mutex);
5106 ret = init_reloc_tree(trans, root);
5107 BUG_ON(ret);
5108 reloc_root = root->reloc_root;
5109
Yan Zhengf82d02d2008-10-29 14:49:05 -04005110 shared_level = ref_path->shared_level;
5111 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005112
Yan Zhengf82d02d2008-10-29 14:49:05 -04005113 keys = ref_path->node_keys;
5114 nodes = ref_path->new_nodes;
5115 memset(&keys[shared_level + 1], 0,
5116 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5117 memset(&nodes[shared_level + 1], 0,
5118 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04005119
Yan Zhengf82d02d2008-10-29 14:49:05 -04005120 if (nodes[lowest_level] == 0) {
5121 path->lowest_level = lowest_level;
5122 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5123 0, 1);
5124 BUG_ON(ret);
5125 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5126 eb = path->nodes[level];
5127 if (!eb || eb == reloc_root->node)
5128 break;
5129 nodes[level] = eb->start;
5130 if (level == 0)
5131 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5132 else
5133 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5134 }
5135 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5136 eb = path->nodes[0];
5137 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5138 group, reloc_inode);
5139 BUG_ON(ret);
5140 }
5141 btrfs_release_path(reloc_root, path);
5142 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005143 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005144 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04005145 BUG_ON(ret);
5146 }
5147
Zheng Yan1a40e232008-09-26 10:09:34 -04005148 /*
5149 * replace tree blocks in the fs tree with tree blocks in
5150 * the reloc tree.
5151 */
5152 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5153 BUG_ON(ret < 0);
5154
5155 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005156 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5157 0, 0);
5158 BUG_ON(ret);
5159 extent_buffer_get(path->nodes[0]);
5160 eb = path->nodes[0];
5161 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005162 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5163 BUG_ON(ret);
5164 free_extent_buffer(eb);
5165 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005166
Yan Zhengf82d02d2008-10-29 14:49:05 -04005167 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005168 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005169 return 0;
5170}
5171
5172static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
5173 struct btrfs_root *root,
5174 struct btrfs_path *path,
5175 struct btrfs_key *first_key,
5176 struct btrfs_ref_path *ref_path)
5177{
5178 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005179
5180 ret = relocate_one_path(trans, root, path, first_key,
5181 ref_path, NULL, NULL);
5182 BUG_ON(ret);
5183
5184 if (root == root->fs_info->extent_root)
5185 btrfs_extent_post_op(trans, root);
Zheng Yan1a40e232008-09-26 10:09:34 -04005186
5187 return 0;
5188}
5189
5190static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
5191 struct btrfs_root *extent_root,
5192 struct btrfs_path *path,
5193 struct btrfs_key *extent_key)
5194{
5195 int ret;
5196
Zheng Yan1a40e232008-09-26 10:09:34 -04005197 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5198 if (ret)
5199 goto out;
5200 ret = btrfs_del_item(trans, extent_root, path);
5201out:
Chris Masonedbd8d42007-12-21 16:27:24 -05005202 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005203 return ret;
5204}
5205
5206static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
5207 struct btrfs_ref_path *ref_path)
5208{
5209 struct btrfs_key root_key;
5210
5211 root_key.objectid = ref_path->root_objectid;
5212 root_key.type = BTRFS_ROOT_ITEM_KEY;
5213 if (is_cowonly_root(ref_path->root_objectid))
5214 root_key.offset = 0;
5215 else
5216 root_key.offset = (u64)-1;
5217
5218 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5219}
5220
5221static int noinline relocate_one_extent(struct btrfs_root *extent_root,
5222 struct btrfs_path *path,
5223 struct btrfs_key *extent_key,
5224 struct btrfs_block_group_cache *group,
5225 struct inode *reloc_inode, int pass)
5226{
5227 struct btrfs_trans_handle *trans;
5228 struct btrfs_root *found_root;
5229 struct btrfs_ref_path *ref_path = NULL;
5230 struct disk_extent *new_extents = NULL;
5231 int nr_extents = 0;
5232 int loops;
5233 int ret;
5234 int level;
5235 struct btrfs_key first_key;
5236 u64 prev_block = 0;
5237
Zheng Yan1a40e232008-09-26 10:09:34 -04005238
5239 trans = btrfs_start_transaction(extent_root, 1);
5240 BUG_ON(!trans);
5241
5242 if (extent_key->objectid == 0) {
5243 ret = del_extent_zero(trans, extent_root, path, extent_key);
5244 goto out;
5245 }
5246
5247 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5248 if (!ref_path) {
5249 ret = -ENOMEM;
5250 goto out;
5251 }
5252
5253 for (loops = 0; ; loops++) {
5254 if (loops == 0) {
5255 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5256 extent_key->objectid);
5257 } else {
5258 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5259 }
5260 if (ret < 0)
5261 goto out;
5262 if (ret > 0)
5263 break;
5264
5265 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5266 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5267 continue;
5268
5269 found_root = read_ref_root(extent_root->fs_info, ref_path);
5270 BUG_ON(!found_root);
5271 /*
5272 * for reference counted tree, only process reference paths
5273 * rooted at the latest committed root.
5274 */
5275 if (found_root->ref_cows &&
5276 ref_path->root_generation != found_root->root_key.offset)
5277 continue;
5278
5279 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5280 if (pass == 0) {
5281 /*
5282 * copy data extents to new locations
5283 */
5284 u64 group_start = group->key.objectid;
5285 ret = relocate_data_extent(reloc_inode,
5286 extent_key,
5287 group_start);
5288 if (ret < 0)
5289 goto out;
5290 break;
5291 }
5292 level = 0;
5293 } else {
5294 level = ref_path->owner_objectid;
5295 }
5296
5297 if (prev_block != ref_path->nodes[level]) {
5298 struct extent_buffer *eb;
5299 u64 block_start = ref_path->nodes[level];
5300 u64 block_size = btrfs_level_size(found_root, level);
5301
5302 eb = read_tree_block(found_root, block_start,
5303 block_size, 0);
5304 btrfs_tree_lock(eb);
5305 BUG_ON(level != btrfs_header_level(eb));
5306
5307 if (level == 0)
5308 btrfs_item_key_to_cpu(eb, &first_key, 0);
5309 else
5310 btrfs_node_key_to_cpu(eb, &first_key, 0);
5311
5312 btrfs_tree_unlock(eb);
5313 free_extent_buffer(eb);
5314 prev_block = block_start;
5315 }
5316
5317 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
5318 pass >= 2) {
5319 /*
5320 * use fallback method to process the remaining
5321 * references.
5322 */
5323 if (!new_extents) {
5324 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04005325 new_extents = kmalloc(sizeof(*new_extents),
5326 GFP_NOFS);
5327 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005328 ret = get_new_locations(reloc_inode,
5329 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04005330 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04005331 &new_extents,
5332 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005333 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04005334 goto out;
5335 }
5336 btrfs_record_root_in_trans(found_root);
5337 ret = replace_one_extent(trans, found_root,
5338 path, extent_key,
5339 &first_key, ref_path,
5340 new_extents, nr_extents);
5341 if (ret < 0)
5342 goto out;
5343 continue;
5344 }
5345
5346 btrfs_record_root_in_trans(found_root);
5347 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5348 ret = relocate_tree_block(trans, found_root, path,
5349 &first_key, ref_path);
5350 } else {
5351 /*
5352 * try to update data extent references while
5353 * keeping metadata shared between snapshots.
5354 */
5355 ret = relocate_one_path(trans, found_root, path,
5356 &first_key, ref_path,
5357 group, reloc_inode);
5358 }
5359 if (ret < 0)
5360 goto out;
5361 }
5362 ret = 0;
5363out:
5364 btrfs_end_transaction(trans, extent_root);
5365 kfree(new_extents);
5366 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005367 return ret;
5368}
5369
Chris Masonec44a352008-04-28 15:29:52 -04005370static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5371{
5372 u64 num_devices;
5373 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5374 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5375
Chris Masona061fc82008-05-07 11:43:44 -04005376 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04005377 if (num_devices == 1) {
5378 stripped |= BTRFS_BLOCK_GROUP_DUP;
5379 stripped = flags & ~stripped;
5380
5381 /* turn raid0 into single device chunks */
5382 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5383 return stripped;
5384
5385 /* turn mirroring into duplication */
5386 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5387 BTRFS_BLOCK_GROUP_RAID10))
5388 return stripped | BTRFS_BLOCK_GROUP_DUP;
5389 return flags;
5390 } else {
5391 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04005392 if (flags & stripped)
5393 return flags;
5394
5395 stripped |= BTRFS_BLOCK_GROUP_DUP;
5396 stripped = flags & ~stripped;
5397
5398 /* switch duplicated blocks with raid1 */
5399 if (flags & BTRFS_BLOCK_GROUP_DUP)
5400 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5401
5402 /* turn single device chunks into raid0 */
5403 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5404 }
5405 return flags;
5406}
5407
Chris Mason0ef3e662008-05-24 14:04:53 -04005408int __alloc_chunk_for_shrink(struct btrfs_root *root,
5409 struct btrfs_block_group_cache *shrink_block_group,
5410 int force)
5411{
5412 struct btrfs_trans_handle *trans;
5413 u64 new_alloc_flags;
5414 u64 calc;
5415
Chris Masonc286ac42008-07-22 23:06:41 -04005416 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005417 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04005418 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005419
Chris Mason0ef3e662008-05-24 14:04:53 -04005420 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005421 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04005422
Chris Mason0ef3e662008-05-24 14:04:53 -04005423 new_alloc_flags = update_block_group_flags(root,
5424 shrink_block_group->flags);
5425 if (new_alloc_flags != shrink_block_group->flags) {
5426 calc =
5427 btrfs_block_group_used(&shrink_block_group->item);
5428 } else {
5429 calc = shrink_block_group->key.offset;
5430 }
Chris Masonc286ac42008-07-22 23:06:41 -04005431 spin_unlock(&shrink_block_group->lock);
5432
Chris Mason0ef3e662008-05-24 14:04:53 -04005433 do_chunk_alloc(trans, root->fs_info->extent_root,
5434 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04005435
Chris Mason0ef3e662008-05-24 14:04:53 -04005436 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04005437 } else
5438 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005439 return 0;
5440}
5441
Zheng Yan1a40e232008-09-26 10:09:34 -04005442static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5443 struct btrfs_root *root,
5444 u64 objectid, u64 size)
5445{
5446 struct btrfs_path *path;
5447 struct btrfs_inode_item *item;
5448 struct extent_buffer *leaf;
5449 int ret;
5450
5451 path = btrfs_alloc_path();
5452 if (!path)
5453 return -ENOMEM;
5454
5455 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5456 if (ret)
5457 goto out;
5458
5459 leaf = path->nodes[0];
5460 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5461 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5462 btrfs_set_inode_generation(leaf, item, 1);
5463 btrfs_set_inode_size(leaf, item, size);
5464 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zhengd899e052008-10-30 14:25:28 -04005465 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM |
5466 BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005467 btrfs_mark_buffer_dirty(leaf);
5468 btrfs_release_path(root, path);
5469out:
5470 btrfs_free_path(path);
5471 return ret;
5472}
5473
5474static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
5475 struct btrfs_block_group_cache *group)
5476{
5477 struct inode *inode = NULL;
5478 struct btrfs_trans_handle *trans;
5479 struct btrfs_root *root;
5480 struct btrfs_key root_key;
5481 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5482 int err = 0;
5483
5484 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5485 root_key.type = BTRFS_ROOT_ITEM_KEY;
5486 root_key.offset = (u64)-1;
5487 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5488 if (IS_ERR(root))
5489 return ERR_CAST(root);
5490
5491 trans = btrfs_start_transaction(root, 1);
5492 BUG_ON(!trans);
5493
5494 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5495 if (err)
5496 goto out;
5497
5498 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5499 BUG_ON(err);
5500
5501 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04005502 group->key.offset, 0, group->key.offset,
5503 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005504 BUG_ON(err);
5505
5506 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5507 if (inode->i_state & I_NEW) {
5508 BTRFS_I(inode)->root = root;
5509 BTRFS_I(inode)->location.objectid = objectid;
5510 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5511 BTRFS_I(inode)->location.offset = 0;
5512 btrfs_read_locked_inode(inode);
5513 unlock_new_inode(inode);
5514 BUG_ON(is_bad_inode(inode));
5515 } else {
5516 BUG_ON(1);
5517 }
5518
5519 err = btrfs_orphan_add(trans, inode);
5520out:
5521 btrfs_end_transaction(trans, root);
5522 if (err) {
5523 if (inode)
5524 iput(inode);
5525 inode = ERR_PTR(err);
5526 }
5527 return inode;
5528}
5529
5530int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05005531{
5532 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05005533 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04005534 struct btrfs_fs_info *info = root->fs_info;
5535 struct extent_buffer *leaf;
5536 struct inode *reloc_inode;
5537 struct btrfs_block_group_cache *block_group;
5538 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04005539 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05005540 u64 cur_byte;
5541 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05005542 u32 nritems;
5543 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04005544 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04005545 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005546
Chris Masonedbd8d42007-12-21 16:27:24 -05005547 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04005548
5549 block_group = btrfs_lookup_block_group(info, group_start);
5550 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05005551
Chris Mason323da792008-05-09 11:46:48 -04005552 printk("btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005553 (unsigned long long)block_group->key.objectid,
5554 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04005555
Zheng Yan1a40e232008-09-26 10:09:34 -04005556 path = btrfs_alloc_path();
5557 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04005558
Zheng Yan1a40e232008-09-26 10:09:34 -04005559 reloc_inode = create_reloc_inode(info, block_group);
5560 BUG_ON(IS_ERR(reloc_inode));
5561
Zheng Yan1a40e232008-09-26 10:09:34 -04005562 __alloc_chunk_for_shrink(root, block_group, 1);
5563 block_group->ro = 1;
5564 block_group->space_info->total_bytes -= block_group->key.offset;
5565
Zheng Yan1a40e232008-09-26 10:09:34 -04005566 btrfs_start_delalloc_inodes(info->tree_root);
5567 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04005568again:
Yan Zhengd899e052008-10-30 14:25:28 -04005569 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005570 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04005571 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005572 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05005573 key.offset = 0;
5574 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05005575 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05005576
Zheng Yan1a40e232008-09-26 10:09:34 -04005577 trans = btrfs_start_transaction(info->tree_root, 1);
5578 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04005579
Zheng Yan1a40e232008-09-26 10:09:34 -04005580 mutex_lock(&root->fs_info->cleaner_mutex);
5581 btrfs_clean_old_snapshots(info->tree_root);
5582 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5583 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04005584
Yan73e48b22008-01-03 14:14:39 -05005585 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05005586 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5587 if (ret < 0)
5588 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04005589next:
Chris Masonedbd8d42007-12-21 16:27:24 -05005590 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05005591 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05005592 if (path->slots[0] >= nritems) {
5593 ret = btrfs_next_leaf(root, path);
5594 if (ret < 0)
5595 goto out;
5596 if (ret == 1) {
5597 ret = 0;
5598 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005599 }
Yan73e48b22008-01-03 14:14:39 -05005600 leaf = path->nodes[0];
5601 nritems = btrfs_header_nritems(leaf);
5602 }
5603
Zheng Yan1a40e232008-09-26 10:09:34 -04005604 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05005605
Zheng Yan1a40e232008-09-26 10:09:34 -04005606 if (key.objectid >= block_group->key.objectid +
5607 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04005608 break;
5609
Chris Mason725c8462008-01-04 16:47:16 -05005610 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05005611 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005612 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05005613 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005614 continue;
Chris Mason725c8462008-01-04 16:47:16 -05005615 }
5616 progress = 1;
5617
Zheng Yan1a40e232008-09-26 10:09:34 -04005618 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5619 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05005620 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005621 goto next;
5622 }
Yan73e48b22008-01-03 14:14:39 -05005623
Chris Masonedbd8d42007-12-21 16:27:24 -05005624 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005625 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05005626 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005627
5628 __alloc_chunk_for_shrink(root, block_group, 0);
5629 ret = relocate_one_extent(root, path, &key, block_group,
5630 reloc_inode, pass);
5631 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005632 if (ret > 0)
5633 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005634
5635 key.objectid = cur_byte;
5636 key.type = 0;
5637 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005638 }
5639
5640 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005641
5642 if (pass == 0) {
5643 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5644 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5645 WARN_ON(reloc_inode->i_mapping->nrpages);
5646 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005647
5648 if (total_found > 0) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005649 printk("btrfs found %llu extents in pass %d\n",
5650 (unsigned long long)total_found, pass);
5651 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04005652 if (total_found == skipped && pass > 2) {
5653 iput(reloc_inode);
5654 reloc_inode = create_reloc_inode(info, block_group);
5655 pass = 0;
5656 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005657 goto again;
5658 }
5659
Zheng Yan1a40e232008-09-26 10:09:34 -04005660 /* delete reloc_inode */
5661 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005662
Zheng Yan1a40e232008-09-26 10:09:34 -04005663 /* unpin extents in this range */
5664 trans = btrfs_start_transaction(info->tree_root, 1);
5665 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005666
Zheng Yan1a40e232008-09-26 10:09:34 -04005667 spin_lock(&block_group->lock);
5668 WARN_ON(block_group->pinned > 0);
5669 WARN_ON(block_group->reserved > 0);
5670 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5671 spin_unlock(&block_group->lock);
5672 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005673out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005674 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005675 return ret;
5676}
5677
Chris Mason0b86a832008-03-24 15:01:56 -04005678int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
5679 struct btrfs_key *key)
5680{
Chris Mason925baed2008-06-25 16:01:30 -04005681 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005682 struct btrfs_key found_key;
5683 struct extent_buffer *leaf;
5684 int slot;
5685
5686 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5687 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005688 goto out;
5689
Chris Mason0b86a832008-03-24 15:01:56 -04005690 while(1) {
5691 slot = path->slots[0];
5692 leaf = path->nodes[0];
5693 if (slot >= btrfs_header_nritems(leaf)) {
5694 ret = btrfs_next_leaf(root, path);
5695 if (ret == 0)
5696 continue;
5697 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005698 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005699 break;
5700 }
5701 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5702
5703 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005704 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5705 ret = 0;
5706 goto out;
5707 }
Chris Mason0b86a832008-03-24 15:01:56 -04005708 path->slots[0]++;
5709 }
5710 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005711out:
Chris Mason0b86a832008-03-24 15:01:56 -04005712 return ret;
5713}
5714
Zheng Yan1a40e232008-09-26 10:09:34 -04005715int btrfs_free_block_groups(struct btrfs_fs_info *info)
5716{
5717 struct btrfs_block_group_cache *block_group;
5718 struct rb_node *n;
5719
Zheng Yan1a40e232008-09-26 10:09:34 -04005720 spin_lock(&info->block_group_cache_lock);
5721 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5722 block_group = rb_entry(n, struct btrfs_block_group_cache,
5723 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04005724 rb_erase(&block_group->cache_node,
5725 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04005726 spin_unlock(&info->block_group_cache_lock);
5727
5728 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005729 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005730 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005731 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005732 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04005733
5734 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005735 }
5736 spin_unlock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005737 return 0;
5738}
5739
Chris Mason9078a3e2007-04-26 16:46:15 -04005740int btrfs_read_block_groups(struct btrfs_root *root)
5741{
5742 struct btrfs_path *path;
5743 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005744 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005745 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005746 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005747 struct btrfs_key key;
5748 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005749 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005750
Chris Masonbe744172007-05-06 10:15:01 -04005751 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005752 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005753 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005754 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005755 path = btrfs_alloc_path();
5756 if (!path)
5757 return -ENOMEM;
5758
5759 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005760 ret = find_first_block_group(root, path, &key);
5761 if (ret > 0) {
5762 ret = 0;
5763 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005764 }
Chris Mason0b86a832008-03-24 15:01:56 -04005765 if (ret != 0)
5766 goto error;
5767
Chris Mason5f39d392007-10-15 16:14:19 -04005768 leaf = path->nodes[0];
5769 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005770 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005771 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005772 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005773 break;
5774 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005775
Chris Masonc286ac42008-07-22 23:06:41 -04005776 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005777 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005778 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005779 read_extent_buffer(leaf, &cache->item,
5780 btrfs_item_ptr_offset(leaf, path->slots[0]),
5781 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005782 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005783
Chris Mason9078a3e2007-04-26 16:46:15 -04005784 key.objectid = found_key.objectid + found_key.offset;
5785 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005786 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005787
Chris Mason6324fbf2008-03-24 15:01:59 -04005788 ret = update_space_info(info, cache->flags, found_key.offset,
5789 btrfs_block_group_used(&cache->item),
5790 &space_info);
5791 BUG_ON(ret);
5792 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005793 down_write(&space_info->groups_sem);
5794 list_add_tail(&cache->list, &space_info->block_groups);
5795 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005796
Josef Bacik0f9dd462008-09-23 13:14:11 -04005797 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5798 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005799
5800 set_avail_alloc_bits(root->fs_info, cache->flags);
Chris Mason9078a3e2007-04-26 16:46:15 -04005801 }
Chris Mason0b86a832008-03-24 15:01:56 -04005802 ret = 0;
5803error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005804 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005805 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005806}
Chris Mason6324fbf2008-03-24 15:01:59 -04005807
5808int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5809 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005810 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005811 u64 size)
5812{
5813 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005814 struct btrfs_root *extent_root;
5815 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005816
5817 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005818
Chris Masone02119d2008-09-05 16:13:11 -04005819 root->fs_info->last_trans_new_blockgroup = trans->transid;
5820
Chris Mason8f18cf12008-04-25 16:53:30 -04005821 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005822 if (!cache)
5823 return -ENOMEM;
5824
Chris Masone17cade2008-04-15 15:41:47 -04005825 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005826 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04005827 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005828 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005829 INIT_LIST_HEAD(&cache->list);
Chris Mason6324fbf2008-03-24 15:01:59 -04005830 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04005831
Chris Mason6324fbf2008-03-24 15:01:59 -04005832 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005833 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5834 cache->flags = type;
5835 btrfs_set_block_group_flags(&cache->item, type);
5836
5837 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5838 &cache->space_info);
5839 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005840 down_write(&cache->space_info->groups_sem);
5841 list_add_tail(&cache->list, &cache->space_info->block_groups);
5842 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005843
Josef Bacik0f9dd462008-09-23 13:14:11 -04005844 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5845 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005846
Chris Mason6324fbf2008-03-24 15:01:59 -04005847 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5848 sizeof(cache->item));
5849 BUG_ON(ret);
5850
Chris Mason87ef2bb2008-10-30 11:23:27 -04005851 finish_current_insert(trans, extent_root, 0);
5852 ret = del_pending_extents(trans, extent_root, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04005853 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04005854 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005855
Chris Mason6324fbf2008-03-24 15:01:59 -04005856 return 0;
5857}
Zheng Yan1a40e232008-09-26 10:09:34 -04005858
5859int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5860 struct btrfs_root *root, u64 group_start)
5861{
5862 struct btrfs_path *path;
5863 struct btrfs_block_group_cache *block_group;
5864 struct btrfs_key key;
5865 int ret;
5866
Zheng Yan1a40e232008-09-26 10:09:34 -04005867 root = root->fs_info->extent_root;
5868
5869 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5870 BUG_ON(!block_group);
5871
5872 memcpy(&key, &block_group->key, sizeof(key));
5873
5874 path = btrfs_alloc_path();
5875 BUG_ON(!path);
5876
5877 btrfs_remove_free_space_cache(block_group);
5878 rb_erase(&block_group->cache_node,
5879 &root->fs_info->block_group_cache_tree);
Josef Bacik80eb2342008-10-29 14:49:05 -04005880 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005881 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005882 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005883
5884 /*
5885 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5886 kfree(shrink_block_group);
5887 */
5888
5889 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5890 if (ret > 0)
5891 ret = -EIO;
5892 if (ret < 0)
5893 goto out;
5894
5895 ret = btrfs_del_item(trans, root, path);
5896out:
5897 btrfs_free_path(path);
5898 return ret;
5899}