blob: 564260872c7eb35af4670c3a71435b84d82fbd1b [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Mason74493f72007-12-11 09:25:06 -050022#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040023#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050024#include "ctree.h"
25#include "disk-io.h"
26#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040027#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040028#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040029#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040030#include "ref-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050031
Zheng Yan31840ae2008-09-23 13:14:14 -040032#define PENDING_EXTENT_INSERT 0
33#define PENDING_EXTENT_DELETE 1
34#define PENDING_BACKREF_UPDATE 2
35
36struct pending_extent_op {
37 int type;
38 u64 bytenr;
39 u64 num_bytes;
40 u64 parent;
41 u64 orig_parent;
42 u64 generation;
43 u64 orig_generation;
44 int level;
45};
46
Chris Masone089f052007-03-16 16:20:31 -040047static int finish_current_insert(struct btrfs_trans_handle *trans, struct
48 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040049static int del_pending_extents(struct btrfs_trans_handle *trans, struct
50 btrfs_root *extent_root);
Chris Mason925baed2008-06-25 16:01:30 -040051static struct btrfs_block_group_cache *
52__btrfs_find_block_group(struct btrfs_root *root,
53 struct btrfs_block_group_cache *hint,
54 u64 search_start, int data, int owner);
Yand548ee52008-01-03 13:56:30 -050055
Josef Bacik0f9dd462008-09-23 13:14:11 -040056static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
57{
58 return (cache->flags & bits) == bits;
59}
60
61/*
62 * this adds the block group to the fs_info rb tree for the block group
63 * cache
64 */
65int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
66 struct btrfs_block_group_cache *block_group)
67{
68 struct rb_node **p;
69 struct rb_node *parent = NULL;
70 struct btrfs_block_group_cache *cache;
71
72 spin_lock(&info->block_group_cache_lock);
73 p = &info->block_group_cache_tree.rb_node;
74
75 while (*p) {
76 parent = *p;
77 cache = rb_entry(parent, struct btrfs_block_group_cache,
78 cache_node);
79 if (block_group->key.objectid < cache->key.objectid) {
80 p = &(*p)->rb_left;
81 } else if (block_group->key.objectid > cache->key.objectid) {
82 p = &(*p)->rb_right;
83 } else {
84 spin_unlock(&info->block_group_cache_lock);
85 return -EEXIST;
86 }
87 }
88
89 rb_link_node(&block_group->cache_node, parent, p);
90 rb_insert_color(&block_group->cache_node,
91 &info->block_group_cache_tree);
92 spin_unlock(&info->block_group_cache_lock);
93
94 return 0;
95}
96
97/*
98 * This will return the block group at or after bytenr if contains is 0, else
99 * it will return the block group that contains the bytenr
100 */
101static struct btrfs_block_group_cache *
102block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
103 int contains)
104{
105 struct btrfs_block_group_cache *cache, *ret = NULL;
106 struct rb_node *n;
107 u64 end, start;
108
109 spin_lock(&info->block_group_cache_lock);
110 n = info->block_group_cache_tree.rb_node;
111
112 while (n) {
113 cache = rb_entry(n, struct btrfs_block_group_cache,
114 cache_node);
115 end = cache->key.objectid + cache->key.offset - 1;
116 start = cache->key.objectid;
117
118 if (bytenr < start) {
119 if (!contains && (!ret || start < ret->key.objectid))
120 ret = cache;
121 n = n->rb_left;
122 } else if (bytenr > start) {
123 if (contains && bytenr <= end) {
124 ret = cache;
125 break;
126 }
127 n = n->rb_right;
128 } else {
129 ret = cache;
130 break;
131 }
132 }
133 spin_unlock(&info->block_group_cache_lock);
134
135 return ret;
136}
137
138/*
139 * this is only called by cache_block_group, since we could have freed extents
140 * we need to check the pinned_extents for any extents that can't be used yet
141 * since their free space will be released as soon as the transaction commits.
142 */
143static int add_new_free_space(struct btrfs_block_group_cache *block_group,
144 struct btrfs_fs_info *info, u64 start, u64 end)
145{
146 u64 extent_start, extent_end, size;
147 int ret;
148
Josef Bacik25179202008-10-29 14:49:05 -0400149 mutex_lock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400150 while (start < end) {
151 ret = find_first_extent_bit(&info->pinned_extents, start,
152 &extent_start, &extent_end,
153 EXTENT_DIRTY);
154 if (ret)
155 break;
156
157 if (extent_start == start) {
158 start = extent_end + 1;
159 } else if (extent_start > start && extent_start < end) {
160 size = extent_start - start;
Josef Bacik25179202008-10-29 14:49:05 -0400161 ret = btrfs_add_free_space_lock(block_group, start,
162 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400163 BUG_ON(ret);
164 start = extent_end + 1;
165 } else {
166 break;
167 }
168 }
169
170 if (start < end) {
171 size = end - start;
Josef Bacik25179202008-10-29 14:49:05 -0400172 ret = btrfs_add_free_space_lock(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400173 BUG_ON(ret);
174 }
Josef Bacik25179202008-10-29 14:49:05 -0400175 mutex_unlock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400176
177 return 0;
178}
179
Chris Masone37c9e62007-05-09 20:13:14 -0400180static int cache_block_group(struct btrfs_root *root,
181 struct btrfs_block_group_cache *block_group)
182{
183 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400184 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400185 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400186 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400187 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -0400188 u64 last = 0;
Yan7d7d6062007-09-14 16:15:28 -0400189 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -0400190 int found = 0;
191
Chris Mason00f5c792007-11-30 10:09:33 -0500192 if (!block_group)
193 return 0;
194
Chris Masone37c9e62007-05-09 20:13:14 -0400195 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400196
197 if (block_group->cached)
198 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400199
Chris Masone37c9e62007-05-09 20:13:14 -0400200 path = btrfs_alloc_path();
201 if (!path)
202 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400203
Chris Mason2cc58cf2007-08-27 16:49:44 -0400204 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400205 /*
206 * we get into deadlocks with paths held by callers of this function.
207 * since the alloc_mutex is protecting things right now, just
208 * skip the locking here
209 */
210 path->skip_locking = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400211 first_free = max_t(u64, block_group->key.objectid,
212 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Masone37c9e62007-05-09 20:13:14 -0400213 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -0400214 key.offset = 0;
215 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
216 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
217 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400218 goto err;
Chris Mason0b86a832008-03-24 15:01:56 -0400219 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -0500220 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400221 goto err;
Yand548ee52008-01-03 13:56:30 -0500222 if (ret == 0) {
223 leaf = path->nodes[0];
224 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
225 if (key.objectid + key.offset > first_free)
226 first_free = key.objectid + key.offset;
227 }
Chris Masone37c9e62007-05-09 20:13:14 -0400228 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400229 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400230 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400231 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400232 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400233 if (ret < 0)
234 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400235 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400236 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400237 else
Chris Masone37c9e62007-05-09 20:13:14 -0400238 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400239 }
Chris Mason5f39d392007-10-15 16:14:19 -0400240 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400241 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400242 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400243
Chris Masone37c9e62007-05-09 20:13:14 -0400244 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400245 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400246 break;
Yan7d7d6062007-09-14 16:15:28 -0400247
248 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
249 if (!found) {
250 last = first_free;
251 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400252 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400253
254 add_new_free_space(block_group, root->fs_info, last,
255 key.objectid);
256
Yan7d7d6062007-09-14 16:15:28 -0400257 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400258 }
Yan7d7d6062007-09-14 16:15:28 -0400259next:
Chris Masone37c9e62007-05-09 20:13:14 -0400260 path->slots[0]++;
261 }
262
Yan7d7d6062007-09-14 16:15:28 -0400263 if (!found)
264 last = first_free;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400265
266 add_new_free_space(block_group, root->fs_info, last,
267 block_group->key.objectid +
268 block_group->key.offset);
269
Chris Masone37c9e62007-05-09 20:13:14 -0400270 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400271 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400272err:
Chris Masone37c9e62007-05-09 20:13:14 -0400273 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400274 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400275}
276
Josef Bacik0f9dd462008-09-23 13:14:11 -0400277/*
278 * return the block group that starts at or after bytenr
279 */
Chris Mason0ef3e662008-05-24 14:04:53 -0400280struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
281 btrfs_fs_info *info,
282 u64 bytenr)
283{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400284 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400285
Josef Bacik0f9dd462008-09-23 13:14:11 -0400286 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400287
Josef Bacik0f9dd462008-09-23 13:14:11 -0400288 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400289}
290
Josef Bacik0f9dd462008-09-23 13:14:11 -0400291/*
292 * return the block group that contains teh given bytenr
293 */
Chris Mason5276aed2007-06-11 21:33:38 -0400294struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
295 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400296 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400297{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400298 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400299
Josef Bacik0f9dd462008-09-23 13:14:11 -0400300 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400301
Josef Bacik0f9dd462008-09-23 13:14:11 -0400302 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400303}
Chris Mason0b86a832008-03-24 15:01:56 -0400304
Josef Bacik0f9dd462008-09-23 13:14:11 -0400305static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
306 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400307{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400308 struct list_head *head = &info->space_info;
309 struct list_head *cur;
310 struct btrfs_space_info *found;
311 list_for_each(cur, head) {
312 found = list_entry(cur, struct btrfs_space_info, list);
313 if (found->flags == flags)
314 return found;
315 }
316 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400317}
318
Josef Bacik80eb2342008-10-29 14:49:05 -0400319static u64 div_factor(u64 num, int factor)
320{
321 if (factor == 10)
322 return num;
323 num *= factor;
324 do_div(num, 10);
325 return num;
326}
327
Chris Mason925baed2008-06-25 16:01:30 -0400328static struct btrfs_block_group_cache *
329__btrfs_find_block_group(struct btrfs_root *root,
330 struct btrfs_block_group_cache *hint,
331 u64 search_start, int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400332{
Chris Mason96b51792007-10-15 16:15:19 -0400333 struct btrfs_block_group_cache *cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400334 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400335 struct btrfs_fs_info *info = root->fs_info;
336 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400337 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400338 u64 free_check;
Chris Mason31f3c992007-04-30 15:25:45 -0400339 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400340 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400341 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400342
Chris Masona236aed2008-04-29 09:38:00 -0400343 if (data & BTRFS_BLOCK_GROUP_METADATA)
344 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400345
Chris Mason0ef3e662008-05-24 14:04:53 -0400346 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400347 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400348 shint = btrfs_lookup_first_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400349 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonc286ac42008-07-22 23:06:41 -0400350 spin_lock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400351 used = btrfs_block_group_used(&shint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400352 if (used + shint->pinned + shint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500353 div_factor(shint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400354 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400355 return shint;
356 }
Chris Masonc286ac42008-07-22 23:06:41 -0400357 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400358 }
359 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400360 if (hint && !hint->ro && block_group_bits(hint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400361 spin_lock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400362 used = btrfs_block_group_used(&hint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400363 if (used + hint->pinned + hint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500364 div_factor(hint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400365 spin_unlock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400366 return hint;
367 }
Chris Masonc286ac42008-07-22 23:06:41 -0400368 spin_unlock(&hint->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400369 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400370 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400371 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400372 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400373 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400374 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400375 }
Chris Mason31f3c992007-04-30 15:25:45 -0400376again:
Zheng Yane8569812008-09-26 10:05:48 -0400377 while (1) {
378 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400379 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400380 break;
Chris Mason96b51792007-10-15 16:15:19 -0400381
Chris Masonc286ac42008-07-22 23:06:41 -0400382 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400383 last = cache->key.objectid + cache->key.offset;
384 used = btrfs_block_group_used(&cache->item);
385
Chris Mason8f18cf12008-04-25 16:53:30 -0400386 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400387 free_check = div_factor(cache->key.offset, factor);
Zheng Yane8569812008-09-26 10:05:48 -0400388 if (used + cache->pinned + cache->reserved <
389 free_check) {
Chris Mason8790d502008-04-03 16:29:03 -0400390 found_group = cache;
Chris Masonc286ac42008-07-22 23:06:41 -0400391 spin_unlock(&cache->lock);
Chris Mason8790d502008-04-03 16:29:03 -0400392 goto found;
393 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400394 }
Chris Masonc286ac42008-07-22 23:06:41 -0400395 spin_unlock(&cache->lock);
Chris Masonde428b62007-05-18 13:28:27 -0400396 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400397 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400398 if (!wrapped) {
399 last = search_start;
400 wrapped = 1;
401 goto again;
402 }
403 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400404 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400405 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400406 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400407 goto again;
408 }
Chris Masonbe744172007-05-06 10:15:01 -0400409found:
Chris Mason31f3c992007-04-30 15:25:45 -0400410 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400411}
412
Chris Mason925baed2008-06-25 16:01:30 -0400413struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
414 struct btrfs_block_group_cache
415 *hint, u64 search_start,
416 int data, int owner)
417{
418
419 struct btrfs_block_group_cache *ret;
Chris Mason925baed2008-06-25 16:01:30 -0400420 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
Chris Mason925baed2008-06-25 16:01:30 -0400421 return ret;
422}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400423
Chris Masone02119d2008-09-05 16:13:11 -0400424/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400425int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400426{
427 int ret;
428 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400429 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400430
Zheng Yan31840ae2008-09-23 13:14:14 -0400431 path = btrfs_alloc_path();
432 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400433 key.objectid = start;
434 key.offset = len;
435 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
436 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
437 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400438 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500439 return ret;
440}
441
Chris Masond8d5f3e2007-12-11 12:42:00 -0500442/*
443 * Back reference rules. Back refs have three main goals:
444 *
445 * 1) differentiate between all holders of references to an extent so that
446 * when a reference is dropped we can make sure it was a valid reference
447 * before freeing the extent.
448 *
449 * 2) Provide enough information to quickly find the holders of an extent
450 * if we notice a given block is corrupted or bad.
451 *
452 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
453 * maintenance. This is actually the same as #2, but with a slightly
454 * different use case.
455 *
456 * File extents can be referenced by:
457 *
458 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400459 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500460 * - different offsets inside a file (bookend extents in file.c)
461 *
462 * The extent ref structure has fields for:
463 *
464 * - Objectid of the subvolume root
465 * - Generation number of the tree holding the reference
466 * - objectid of the file holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400467 * - number of references holding by parent node (alway 1 for tree blocks)
468 *
469 * Btree leaf may hold multiple references to a file extent. In most cases,
470 * these references are from same file and the corresponding offsets inside
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400471 * the file are close together.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500472 *
473 * When a file extent is allocated the fields are filled in:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400474 * (root_key.objectid, trans->transid, inode objectid, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500475 *
476 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400477 * in the leaf. It looks similar to the create case, but trans->transid will
478 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500479 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400480 * (root_key.objectid, trans->transid, inode objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400481 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500482 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400483 * When a file extent is removed either during snapshot deletion or
484 * file truncation, we find the corresponding back reference and check
485 * the following fields:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500486 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400487 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
488 * inode objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500489 *
490 * Btree extents can be referenced by:
491 *
492 * - Different subvolumes
493 * - Different generations of the same subvolume
494 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500495 * When a tree block is created, back references are inserted:
496 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400497 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500498 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400499 * When a tree block is cow'd, new back references are added for all the
500 * blocks it points to. If the tree block isn't in reference counted root,
501 * the old back references are removed. These new back references are of
502 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500503 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400504 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500505 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400506 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500507 *
508 * if backref was for a tree root:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400509 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500510 * else
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400511 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500512 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400513 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500514 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400515 * The key objectid corresponds to the first byte in the extent, the key
516 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
517 * byte of parent extent. If a extent is tree root, the key offset is set
518 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500519 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400520
521static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
522 struct btrfs_root *root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400523 struct btrfs_path *path,
524 u64 bytenr, u64 parent,
525 u64 ref_root, u64 ref_generation,
526 u64 owner_objectid, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500527{
Chris Mason74493f72007-12-11 09:25:06 -0500528 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400529 struct btrfs_extent_ref *ref;
530 struct extent_buffer *leaf;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400531 u64 ref_objectid;
Chris Mason74493f72007-12-11 09:25:06 -0500532 int ret;
533
Chris Mason74493f72007-12-11 09:25:06 -0500534 key.objectid = bytenr;
535 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400536 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500537
Zheng Yan31840ae2008-09-23 13:14:14 -0400538 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
539 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500540 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400541 if (ret > 0) {
542 ret = -ENOENT;
543 goto out;
544 }
545
546 leaf = path->nodes[0];
547 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400548 ref_objectid = btrfs_ref_objectid(leaf, ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400549 if (btrfs_ref_root(leaf, ref) != ref_root ||
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400550 btrfs_ref_generation(leaf, ref) != ref_generation ||
551 (ref_objectid != owner_objectid &&
552 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400553 ret = -EIO;
554 WARN_ON(1);
555 goto out;
556 }
557 ret = 0;
558out:
559 return ret;
560}
561
562static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
563 struct btrfs_root *root,
564 struct btrfs_path *path,
565 u64 bytenr, u64 parent,
566 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400567 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400568{
569 struct btrfs_key key;
570 struct extent_buffer *leaf;
571 struct btrfs_extent_ref *ref;
572 u32 num_refs;
573 int ret;
574
575 key.objectid = bytenr;
576 key.type = BTRFS_EXTENT_REF_KEY;
577 key.offset = parent;
578
579 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
580 if (ret == 0) {
581 leaf = path->nodes[0];
582 ref = btrfs_item_ptr(leaf, path->slots[0],
583 struct btrfs_extent_ref);
584 btrfs_set_ref_root(leaf, ref, ref_root);
585 btrfs_set_ref_generation(leaf, ref, ref_generation);
586 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400587 btrfs_set_ref_num_refs(leaf, ref, 1);
588 } else if (ret == -EEXIST) {
589 u64 existing_owner;
590 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
591 leaf = path->nodes[0];
592 ref = btrfs_item_ptr(leaf, path->slots[0],
593 struct btrfs_extent_ref);
594 if (btrfs_ref_root(leaf, ref) != ref_root ||
595 btrfs_ref_generation(leaf, ref) != ref_generation) {
596 ret = -EIO;
597 WARN_ON(1);
598 goto out;
599 }
600
601 num_refs = btrfs_ref_num_refs(leaf, ref);
602 BUG_ON(num_refs == 0);
603 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
604
605 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400606 if (existing_owner != owner_objectid &&
607 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400608 btrfs_set_ref_objectid(leaf, ref,
609 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400610 }
611 ret = 0;
612 } else {
613 goto out;
614 }
Chris Mason7bb86312007-12-11 09:25:06 -0500615 btrfs_mark_buffer_dirty(path->nodes[0]);
616out:
617 btrfs_release_path(root, path);
618 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500619}
620
Zheng Yan31840ae2008-09-23 13:14:14 -0400621static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
622 struct btrfs_root *root,
623 struct btrfs_path *path)
624{
625 struct extent_buffer *leaf;
626 struct btrfs_extent_ref *ref;
627 u32 num_refs;
628 int ret = 0;
629
630 leaf = path->nodes[0];
631 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
632 num_refs = btrfs_ref_num_refs(leaf, ref);
633 BUG_ON(num_refs == 0);
634 num_refs -= 1;
635 if (num_refs == 0) {
636 ret = btrfs_del_item(trans, root, path);
637 } else {
638 btrfs_set_ref_num_refs(leaf, ref, num_refs);
639 btrfs_mark_buffer_dirty(leaf);
640 }
641 btrfs_release_path(root, path);
642 return ret;
643}
644
645static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
646 struct btrfs_root *root, u64 bytenr,
647 u64 orig_parent, u64 parent,
648 u64 orig_root, u64 ref_root,
649 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400650 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400651{
652 int ret;
653 struct btrfs_root *extent_root = root->fs_info->extent_root;
654 struct btrfs_path *path;
655
656 if (root == root->fs_info->extent_root) {
657 struct pending_extent_op *extent_op;
658 u64 num_bytes;
659
660 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
661 num_bytes = btrfs_level_size(root, (int)owner_objectid);
Josef Bacik25179202008-10-29 14:49:05 -0400662 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -0400663 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
Josef Bacik25179202008-10-29 14:49:05 -0400664 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400665 u64 priv;
666 ret = get_state_private(&root->fs_info->extent_ins,
667 bytenr, &priv);
668 BUG_ON(ret);
669 extent_op = (struct pending_extent_op *)
670 (unsigned long)priv;
671 BUG_ON(extent_op->parent != orig_parent);
672 BUG_ON(extent_op->generation != orig_generation);
Josef Bacik25179202008-10-29 14:49:05 -0400673
Zheng Yan31840ae2008-09-23 13:14:14 -0400674 extent_op->parent = parent;
675 extent_op->generation = ref_generation;
676 } else {
677 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
678 BUG_ON(!extent_op);
679
680 extent_op->type = PENDING_BACKREF_UPDATE;
681 extent_op->bytenr = bytenr;
682 extent_op->num_bytes = num_bytes;
683 extent_op->parent = parent;
684 extent_op->orig_parent = orig_parent;
685 extent_op->generation = ref_generation;
686 extent_op->orig_generation = orig_generation;
687 extent_op->level = (int)owner_objectid;
688
689 set_extent_bits(&root->fs_info->extent_ins,
690 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -0400691 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400692 set_state_private(&root->fs_info->extent_ins,
693 bytenr, (unsigned long)extent_op);
694 }
Josef Bacik25179202008-10-29 14:49:05 -0400695 mutex_unlock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -0400696 return 0;
697 }
698
699 path = btrfs_alloc_path();
700 if (!path)
701 return -ENOMEM;
702 ret = lookup_extent_backref(trans, extent_root, path,
703 bytenr, orig_parent, orig_root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400704 orig_generation, owner_objectid, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400705 if (ret)
706 goto out;
707 ret = remove_extent_backref(trans, extent_root, path);
708 if (ret)
709 goto out;
710 ret = insert_extent_backref(trans, extent_root, path, bytenr,
711 parent, ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400712 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400713 BUG_ON(ret);
714 finish_current_insert(trans, extent_root);
715 del_pending_extents(trans, extent_root);
716out:
717 btrfs_free_path(path);
718 return ret;
719}
720
721int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
722 struct btrfs_root *root, u64 bytenr,
723 u64 orig_parent, u64 parent,
724 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400725 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400726{
727 int ret;
728 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
729 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
730 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400731 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
732 parent, ref_root, ref_root,
733 ref_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400734 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400735 return ret;
736}
737
Chris Mason925baed2008-06-25 16:01:30 -0400738static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400739 struct btrfs_root *root, u64 bytenr,
740 u64 orig_parent, u64 parent,
741 u64 orig_root, u64 ref_root,
742 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400743 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -0500744{
Chris Mason5caf2a02007-04-02 11:20:42 -0400745 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500746 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400747 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400748 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400749 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400750 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500751
Chris Mason5caf2a02007-04-02 11:20:42 -0400752 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400753 if (!path)
754 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400755
Chris Mason3c12ac72008-04-21 12:01:38 -0400756 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400757 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400758 key.type = BTRFS_EXTENT_ITEM_KEY;
759 key.offset = (u64)-1;
760
Chris Mason5caf2a02007-04-02 11:20:42 -0400761 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400762 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400763 if (ret < 0)
764 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400765 BUG_ON(ret == 0 || path->slots[0] == 0);
766
767 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400768 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -0400769
770 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
771 BUG_ON(key.objectid != bytenr);
772 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
773
Chris Mason5caf2a02007-04-02 11:20:42 -0400774 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400775 refs = btrfs_extent_refs(l, item);
776 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400777 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500778
Chris Mason5caf2a02007-04-02 11:20:42 -0400779 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500780
Chris Mason3c12ac72008-04-21 12:01:38 -0400781 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -0400782 ret = insert_extent_backref(trans, root->fs_info->extent_root,
783 path, bytenr, parent,
784 ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400785 owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500786 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400787 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400788 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500789
790 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500791 return 0;
792}
793
Chris Mason925baed2008-06-25 16:01:30 -0400794int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400795 struct btrfs_root *root,
796 u64 bytenr, u64 num_bytes, u64 parent,
797 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400798 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -0400799{
800 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400801 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
802 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
803 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400804 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
805 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400806 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -0400807 return ret;
808}
809
Chris Masone9d0b132007-08-10 14:06:19 -0400810int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
811 struct btrfs_root *root)
812{
813 finish_current_insert(trans, root->fs_info->extent_root);
814 del_pending_extents(trans, root->fs_info->extent_root);
815 return 0;
816}
817
Zheng Yan31840ae2008-09-23 13:14:14 -0400818int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
819 struct btrfs_root *root, u64 bytenr,
820 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500821{
Chris Mason5caf2a02007-04-02 11:20:42 -0400822 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500823 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400824 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400825 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400826 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400827
Chris Masondb945352007-10-15 16:15:53 -0400828 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400829 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400830 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400831 key.objectid = bytenr;
832 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400833 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400834 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400835 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400836 if (ret < 0)
837 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400838 if (ret != 0) {
839 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400840 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500841 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400842 }
843 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400844 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400845 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400846out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400847 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500848 return 0;
849}
850
Yan Zhengf321e492008-07-30 09:26:11 -0400851static int get_reference_status(struct btrfs_root *root, u64 bytenr,
852 u64 parent_gen, u64 ref_objectid,
853 u64 *min_generation, u32 *ref_count)
Chris Masonbe20aa92007-12-17 20:14:01 -0500854{
855 struct btrfs_root *extent_root = root->fs_info->extent_root;
856 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -0400857 struct extent_buffer *leaf;
858 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -0500859 struct btrfs_key key;
860 struct btrfs_key found_key;
Yan Zhengf321e492008-07-30 09:26:11 -0400861 u64 root_objectid = root->root_key.objectid;
862 u64 ref_generation;
863 u32 nritems;
864 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500865
Chris Masonbe20aa92007-12-17 20:14:01 -0500866 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400867 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -0400868 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -0500869
Yan Zhengf321e492008-07-30 09:26:11 -0400870 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -0500871 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
872 if (ret < 0)
873 goto out;
874 BUG_ON(ret == 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400875 if (ret < 0 || path->slots[0] == 0)
876 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500877
Zheng Yan31840ae2008-09-23 13:14:14 -0400878 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -0400879 leaf = path->nodes[0];
880 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500881
882 if (found_key.objectid != bytenr ||
883 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
Yan Zhengf321e492008-07-30 09:26:11 -0400884 ret = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500885 goto out;
886 }
887
Yan Zhengf321e492008-07-30 09:26:11 -0400888 *ref_count = 0;
889 *min_generation = (u64)-1;
890
Chris Masonbe20aa92007-12-17 20:14:01 -0500891 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -0400892 leaf = path->nodes[0];
893 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500894 if (path->slots[0] >= nritems) {
895 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -0400896 if (ret < 0)
897 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500898 if (ret == 0)
899 continue;
900 break;
901 }
Yan Zhengf321e492008-07-30 09:26:11 -0400902 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500903 if (found_key.objectid != bytenr)
904 break;
Chris Masonbd098352008-01-03 13:23:19 -0500905
Chris Masonbe20aa92007-12-17 20:14:01 -0500906 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
907 path->slots[0]++;
908 continue;
909 }
910
Yan Zhengf321e492008-07-30 09:26:11 -0400911 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -0500912 struct btrfs_extent_ref);
Yan Zhengf321e492008-07-30 09:26:11 -0400913 ref_generation = btrfs_ref_generation(leaf, ref_item);
914 /*
Zheng Yan31840ae2008-09-23 13:14:14 -0400915 * For (parent_gen > 0 && parent_gen > ref_generation):
Yan Zhengf321e492008-07-30 09:26:11 -0400916 *
Yanbcc63ab2008-07-30 16:29:20 -0400917 * we reach here through the oldest root, therefore
918 * all other reference from same snapshot should have
Yan Zhengf321e492008-07-30 09:26:11 -0400919 * a larger generation.
920 */
921 if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
922 (parent_gen > 0 && parent_gen > ref_generation) ||
923 (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
924 ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400925 *ref_count = 2;
Yan Zhengf321e492008-07-30 09:26:11 -0400926 break;
927 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500928
Yan Zhengf321e492008-07-30 09:26:11 -0400929 *ref_count = 1;
930 if (*min_generation > ref_generation)
931 *min_generation = ref_generation;
932
Chris Masonbe20aa92007-12-17 20:14:01 -0500933 path->slots[0]++;
934 }
Yan Zhengf321e492008-07-30 09:26:11 -0400935 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500936out:
Yan Zhengf321e492008-07-30 09:26:11 -0400937 btrfs_free_path(path);
938 return ret;
939}
940
Yan Zheng7ea394f2008-08-05 13:05:02 -0400941int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
942 struct btrfs_root *root,
Yan Zhengf321e492008-07-30 09:26:11 -0400943 struct btrfs_key *key, u64 bytenr)
944{
Yan Zhengf321e492008-07-30 09:26:11 -0400945 struct btrfs_root *old_root;
946 struct btrfs_path *path = NULL;
947 struct extent_buffer *eb;
948 struct btrfs_file_extent_item *item;
949 u64 ref_generation;
950 u64 min_generation;
951 u64 extent_start;
952 u32 ref_count;
953 int level;
954 int ret;
955
Yan Zheng7ea394f2008-08-05 13:05:02 -0400956 BUG_ON(trans == NULL);
Yan Zhengf321e492008-07-30 09:26:11 -0400957 BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY);
958 ret = get_reference_status(root, bytenr, 0, key->objectid,
959 &min_generation, &ref_count);
960 if (ret)
961 return ret;
962
963 if (ref_count != 1)
964 return 1;
965
Yan Zhengf321e492008-07-30 09:26:11 -0400966 old_root = root->dirty_root->root;
967 ref_generation = old_root->root_key.offset;
968
969 /* all references are created in running transaction */
970 if (min_generation > ref_generation) {
971 ret = 0;
972 goto out;
973 }
974
975 path = btrfs_alloc_path();
976 if (!path) {
977 ret = -ENOMEM;
978 goto out;
979 }
980
981 path->skip_locking = 1;
982 /* if no item found, the extent is referenced by other snapshot */
983 ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0);
984 if (ret)
985 goto out;
986
987 eb = path->nodes[0];
988 item = btrfs_item_ptr(eb, path->slots[0],
989 struct btrfs_file_extent_item);
990 if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG ||
991 btrfs_file_extent_disk_bytenr(eb, item) != bytenr) {
992 ret = 1;
993 goto out;
994 }
995
996 for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) {
997 if (level >= 0) {
998 eb = path->nodes[level];
999 if (!eb)
1000 continue;
1001 extent_start = eb->start;
Yanbcc63ab2008-07-30 16:29:20 -04001002 } else
Yan Zhengf321e492008-07-30 09:26:11 -04001003 extent_start = bytenr;
1004
1005 ret = get_reference_status(root, extent_start, ref_generation,
1006 0, &min_generation, &ref_count);
1007 if (ret)
1008 goto out;
1009
1010 if (ref_count != 1) {
1011 ret = 1;
1012 goto out;
1013 }
1014 if (level >= 0)
1015 ref_generation = btrfs_header_generation(eb);
1016 }
1017 ret = 0;
1018out:
1019 if (path)
1020 btrfs_free_path(path);
Yan Zhengf321e492008-07-30 09:26:11 -04001021 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001022}
Chris Masonc5739bb2007-04-10 09:27:04 -04001023
Zheng Yan31840ae2008-09-23 13:14:14 -04001024int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1025 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001026{
Chris Mason5f39d392007-10-15 16:14:19 -04001027 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001028 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001029 u64 root_gen;
1030 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001031 int i;
Chris Masondb945352007-10-15 16:15:53 -04001032 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001033 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001034 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001035
Chris Mason3768f362007-03-13 16:47:54 -04001036 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001037 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001038
Zheng Yane4657682008-09-26 10:04:53 -04001039 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1040 shared = 0;
1041 root_gen = root->root_key.offset;
1042 } else {
1043 shared = 1;
1044 root_gen = trans->transid - 1;
1045 }
1046
Chris Masondb945352007-10-15 16:15:53 -04001047 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001048 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001049
Zheng Yan31840ae2008-09-23 13:14:14 -04001050 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001051 struct btrfs_leaf_ref *ref;
1052 struct btrfs_extent_info *info;
1053
Zheng Yan31840ae2008-09-23 13:14:14 -04001054 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001055 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001056 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001057 goto out;
1058 }
1059
Zheng Yane4657682008-09-26 10:04:53 -04001060 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001061 ref->bytenr = buf->start;
1062 ref->owner = btrfs_header_owner(buf);
1063 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001064 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001065 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001066
Zheng Yan31840ae2008-09-23 13:14:14 -04001067 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001068 u64 disk_bytenr;
1069 btrfs_item_key_to_cpu(buf, &key, i);
1070 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1071 continue;
1072 fi = btrfs_item_ptr(buf, i,
1073 struct btrfs_file_extent_item);
1074 if (btrfs_file_extent_type(buf, fi) ==
1075 BTRFS_FILE_EXTENT_INLINE)
1076 continue;
1077 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1078 if (disk_bytenr == 0)
1079 continue;
1080
1081 info->bytenr = disk_bytenr;
1082 info->num_bytes =
1083 btrfs_file_extent_disk_num_bytes(buf, fi);
1084 info->objectid = key.objectid;
1085 info->offset = key.offset;
1086 info++;
1087 }
1088
Zheng Yane4657682008-09-26 10:04:53 -04001089 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001090 if (ret == -EEXIST && shared) {
1091 struct btrfs_leaf_ref *old;
1092 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1093 BUG_ON(!old);
1094 btrfs_remove_leaf_ref(root, old);
1095 btrfs_free_leaf_ref(root, old);
1096 ret = btrfs_add_leaf_ref(root, ref, shared);
1097 }
Yan Zheng31153d82008-07-28 15:32:19 -04001098 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001099 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001100 }
1101out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001102 return ret;
1103}
1104
1105int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1106 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1107 u32 *nr_extents)
1108{
1109 u64 bytenr;
1110 u64 ref_root;
1111 u64 orig_root;
1112 u64 ref_generation;
1113 u64 orig_generation;
1114 u32 nritems;
1115 u32 nr_file_extents = 0;
1116 struct btrfs_key key;
1117 struct btrfs_file_extent_item *fi;
1118 int i;
1119 int level;
1120 int ret = 0;
1121 int faili = 0;
1122 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001123 u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001124
1125 ref_root = btrfs_header_owner(buf);
1126 ref_generation = btrfs_header_generation(buf);
1127 orig_root = btrfs_header_owner(orig_buf);
1128 orig_generation = btrfs_header_generation(orig_buf);
1129
1130 nritems = btrfs_header_nritems(buf);
1131 level = btrfs_header_level(buf);
1132
1133 if (root->ref_cows) {
1134 process_func = __btrfs_inc_extent_ref;
1135 } else {
1136 if (level == 0 &&
1137 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1138 goto out;
1139 if (level != 0 &&
1140 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1141 goto out;
1142 process_func = __btrfs_update_extent_ref;
1143 }
1144
1145 for (i = 0; i < nritems; i++) {
1146 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001147 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001148 btrfs_item_key_to_cpu(buf, &key, i);
1149 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001150 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001151 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001152 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001153 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001154 BTRFS_FILE_EXTENT_INLINE)
1155 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001156 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1157 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001158 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001159
1160 nr_file_extents++;
1161
Zheng Yan31840ae2008-09-23 13:14:14 -04001162 ret = process_func(trans, root, bytenr,
1163 orig_buf->start, buf->start,
1164 orig_root, ref_root,
1165 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001166 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001167
1168 if (ret) {
1169 faili = i;
1170 WARN_ON(1);
1171 goto fail;
1172 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001173 } else {
Chris Masondb945352007-10-15 16:15:53 -04001174 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001175 ret = process_func(trans, root, bytenr,
1176 orig_buf->start, buf->start,
1177 orig_root, ref_root,
1178 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001179 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001180 if (ret) {
1181 faili = i;
1182 WARN_ON(1);
1183 goto fail;
1184 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001185 }
1186 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001187out:
1188 if (nr_extents) {
1189 if (level == 0)
1190 *nr_extents = nr_file_extents;
1191 else
1192 *nr_extents = nritems;
1193 }
1194 return 0;
1195fail:
1196 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001197 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001198}
1199
Zheng Yan31840ae2008-09-23 13:14:14 -04001200int btrfs_update_ref(struct btrfs_trans_handle *trans,
1201 struct btrfs_root *root, struct extent_buffer *orig_buf,
1202 struct extent_buffer *buf, int start_slot, int nr)
1203
1204{
1205 u64 bytenr;
1206 u64 ref_root;
1207 u64 orig_root;
1208 u64 ref_generation;
1209 u64 orig_generation;
1210 struct btrfs_key key;
1211 struct btrfs_file_extent_item *fi;
1212 int i;
1213 int ret;
1214 int slot;
1215 int level;
1216
1217 BUG_ON(start_slot < 0);
1218 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1219
1220 ref_root = btrfs_header_owner(buf);
1221 ref_generation = btrfs_header_generation(buf);
1222 orig_root = btrfs_header_owner(orig_buf);
1223 orig_generation = btrfs_header_generation(orig_buf);
1224 level = btrfs_header_level(buf);
1225
1226 if (!root->ref_cows) {
1227 if (level == 0 &&
1228 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1229 return 0;
1230 if (level != 0 &&
1231 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1232 return 0;
1233 }
1234
1235 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1236 cond_resched();
1237 if (level == 0) {
1238 btrfs_item_key_to_cpu(buf, &key, slot);
1239 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1240 continue;
1241 fi = btrfs_item_ptr(buf, slot,
1242 struct btrfs_file_extent_item);
1243 if (btrfs_file_extent_type(buf, fi) ==
1244 BTRFS_FILE_EXTENT_INLINE)
1245 continue;
1246 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1247 if (bytenr == 0)
1248 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001249 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1250 orig_buf->start, buf->start,
1251 orig_root, ref_root,
1252 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001253 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001254 if (ret)
1255 goto fail;
1256 } else {
1257 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001258 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1259 orig_buf->start, buf->start,
1260 orig_root, ref_root,
1261 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001262 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001263 if (ret)
1264 goto fail;
1265 }
1266 }
1267 return 0;
1268fail:
1269 WARN_ON(1);
1270 return -1;
1271}
1272
Chris Mason9078a3e2007-04-26 16:46:15 -04001273static int write_one_cache_group(struct btrfs_trans_handle *trans,
1274 struct btrfs_root *root,
1275 struct btrfs_path *path,
1276 struct btrfs_block_group_cache *cache)
1277{
1278 int ret;
1279 int pending_ret;
1280 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001281 unsigned long bi;
1282 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001283
Chris Mason9078a3e2007-04-26 16:46:15 -04001284 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001285 if (ret < 0)
1286 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001287 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001288
1289 leaf = path->nodes[0];
1290 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1291 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1292 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001293 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001294fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001295 finish_current_insert(trans, extent_root);
1296 pending_ret = del_pending_extents(trans, extent_root);
1297 if (ret)
1298 return ret;
1299 if (pending_ret)
1300 return pending_ret;
1301 return 0;
1302
1303}
1304
Chris Mason96b51792007-10-15 16:15:19 -04001305int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1306 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001307{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001308 struct btrfs_block_group_cache *cache, *entry;
1309 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001310 int err = 0;
1311 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001312 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001313 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001314
1315 path = btrfs_alloc_path();
1316 if (!path)
1317 return -ENOMEM;
1318
1319 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001320 cache = NULL;
1321 spin_lock(&root->fs_info->block_group_cache_lock);
1322 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1323 n; n = rb_next(n)) {
1324 entry = rb_entry(n, struct btrfs_block_group_cache,
1325 cache_node);
1326 if (entry->dirty) {
1327 cache = entry;
1328 break;
1329 }
1330 }
1331 spin_unlock(&root->fs_info->block_group_cache_lock);
1332
1333 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001334 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001335
Zheng Yane8569812008-09-26 10:05:48 -04001336 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001337 last += cache->key.offset;
1338
Chris Mason96b51792007-10-15 16:15:19 -04001339 err = write_one_cache_group(trans, root,
1340 path, cache);
1341 /*
1342 * if we fail to write the cache group, we want
1343 * to keep it marked dirty in hopes that a later
1344 * write will work
1345 */
1346 if (err) {
1347 werr = err;
1348 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001349 }
1350 }
1351 btrfs_free_path(path);
1352 return werr;
1353}
1354
Chris Mason593060d2008-03-25 16:50:33 -04001355static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1356 u64 total_bytes, u64 bytes_used,
1357 struct btrfs_space_info **space_info)
1358{
1359 struct btrfs_space_info *found;
1360
1361 found = __find_space_info(info, flags);
1362 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001363 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001364 found->total_bytes += total_bytes;
1365 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001366 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001367 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001368 *space_info = found;
1369 return 0;
1370 }
1371 found = kmalloc(sizeof(*found), GFP_NOFS);
1372 if (!found)
1373 return -ENOMEM;
1374
1375 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001376 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001377 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001378 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001379 found->flags = flags;
1380 found->total_bytes = total_bytes;
1381 found->bytes_used = bytes_used;
1382 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001383 found->bytes_reserved = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001384 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001385 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001386 *space_info = found;
1387 return 0;
1388}
1389
Chris Mason8790d502008-04-03 16:29:03 -04001390static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1391{
1392 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001393 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001394 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001395 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001396 if (extra_flags) {
1397 if (flags & BTRFS_BLOCK_GROUP_DATA)
1398 fs_info->avail_data_alloc_bits |= extra_flags;
1399 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1400 fs_info->avail_metadata_alloc_bits |= extra_flags;
1401 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1402 fs_info->avail_system_alloc_bits |= extra_flags;
1403 }
1404}
Chris Mason593060d2008-03-25 16:50:33 -04001405
Chris Masona061fc82008-05-07 11:43:44 -04001406static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001407{
Chris Masona061fc82008-05-07 11:43:44 -04001408 u64 num_devices = root->fs_info->fs_devices->num_devices;
1409
1410 if (num_devices == 1)
1411 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1412 if (num_devices < 4)
1413 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1414
Chris Masonec44a352008-04-28 15:29:52 -04001415 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1416 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001417 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001418 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001419 }
Chris Masonec44a352008-04-28 15:29:52 -04001420
1421 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001422 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001423 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001424 }
Chris Masonec44a352008-04-28 15:29:52 -04001425
1426 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1427 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1428 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1429 (flags & BTRFS_BLOCK_GROUP_DUP)))
1430 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1431 return flags;
1432}
1433
Chris Mason6324fbf2008-03-24 15:01:59 -04001434static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1435 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001436 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001437{
1438 struct btrfs_space_info *space_info;
1439 u64 thresh;
1440 u64 start;
1441 u64 num_bytes;
Josef Bacikcf749822008-10-01 19:11:18 -04001442 int ret = 0, waited = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001443
Chris Masona061fc82008-05-07 11:43:44 -04001444 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001445
Chris Mason6324fbf2008-03-24 15:01:59 -04001446 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001447 if (!space_info) {
1448 ret = update_space_info(extent_root->fs_info, flags,
1449 0, 0, &space_info);
1450 BUG_ON(ret);
1451 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001452 BUG_ON(!space_info);
1453
Josef Bacik25179202008-10-29 14:49:05 -04001454 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001455 if (space_info->force_alloc) {
1456 force = 1;
1457 space_info->force_alloc = 0;
1458 }
Josef Bacik25179202008-10-29 14:49:05 -04001459 if (space_info->full) {
1460 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001461 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001462 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001463
Chris Mason8790d502008-04-03 16:29:03 -04001464 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001465 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001466 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001467 space_info->bytes_reserved + alloc_bytes) < thresh) {
1468 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001469 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001470 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001471
Josef Bacik25179202008-10-29 14:49:05 -04001472 spin_unlock(&space_info->lock);
1473
1474 ret = mutex_trylock(&extent_root->fs_info->chunk_mutex);
1475 if (!ret && !force) {
1476 goto out;
1477 } else if (!ret) {
1478 mutex_lock(&extent_root->fs_info->chunk_mutex);
Josef Bacikcf749822008-10-01 19:11:18 -04001479 waited = 1;
1480 }
1481
Josef Bacik25179202008-10-29 14:49:05 -04001482 if (waited) {
1483 spin_lock(&space_info->lock);
1484 if (space_info->full) {
1485 spin_unlock(&space_info->lock);
1486 goto out_unlock;
1487 }
1488 spin_unlock(&space_info->lock);
1489 }
Josef Bacikcf749822008-10-01 19:11:18 -04001490
Chris Mason6324fbf2008-03-24 15:01:59 -04001491 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
Josef Bacik25179202008-10-29 14:49:05 -04001492 if (ret) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001493printk("space info full %Lu\n", flags);
1494 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001495 goto out_unlock;
Chris Mason6324fbf2008-03-24 15:01:59 -04001496 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001497
1498 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001499 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001500 BUG_ON(ret);
Chris Masona74a4b92008-06-25 16:01:31 -04001501out_unlock:
Chris Mason333db942008-06-25 16:01:30 -04001502 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001503out:
Josef Bacik0f9dd462008-09-23 13:14:11 -04001504 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001505}
1506
Chris Mason9078a3e2007-04-26 16:46:15 -04001507static int update_block_group(struct btrfs_trans_handle *trans,
1508 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001509 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001510 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001511{
1512 struct btrfs_block_group_cache *cache;
1513 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001514 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001515 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001516 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001517
Chris Mason9078a3e2007-04-26 16:46:15 -04001518 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001519 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001520 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001521 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001522 }
Chris Masondb945352007-10-15 16:15:53 -04001523 byte_in_group = bytenr - cache->key.objectid;
1524 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001525
Josef Bacik25179202008-10-29 14:49:05 -04001526 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001527 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001528 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001529 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001530 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001531 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001532 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001533 cache->space_info->bytes_used += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001534 btrfs_set_block_group_used(&cache->item, old_val);
1535 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001536 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001537 } else {
Chris Masondb945352007-10-15 16:15:53 -04001538 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001539 cache->space_info->bytes_used -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001540 btrfs_set_block_group_used(&cache->item, old_val);
1541 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001542 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001543 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001544 int ret;
1545 ret = btrfs_add_free_space(cache, bytenr,
1546 num_bytes);
1547 if (ret)
1548 return -1;
Chris Masone37c9e62007-05-09 20:13:14 -04001549 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001550 }
Chris Masondb945352007-10-15 16:15:53 -04001551 total -= num_bytes;
1552 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001553 }
1554 return 0;
1555}
Chris Mason6324fbf2008-03-24 15:01:59 -04001556
Chris Masona061fc82008-05-07 11:43:44 -04001557static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1558{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001559 struct btrfs_block_group_cache *cache;
1560
1561 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1562 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001563 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001564
1565 return cache->key.objectid;
Chris Masona061fc82008-05-07 11:43:44 -04001566}
1567
Chris Masone02119d2008-09-05 16:13:11 -04001568int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05001569 u64 bytenr, u64 num, int pin)
1570{
1571 u64 len;
1572 struct btrfs_block_group_cache *cache;
1573 struct btrfs_fs_info *fs_info = root->fs_info;
1574
Josef Bacik25179202008-10-29 14:49:05 -04001575 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05001576 if (pin) {
1577 set_extent_dirty(&fs_info->pinned_extents,
1578 bytenr, bytenr + num - 1, GFP_NOFS);
1579 } else {
1580 clear_extent_dirty(&fs_info->pinned_extents,
1581 bytenr, bytenr + num - 1, GFP_NOFS);
1582 }
1583 while (num > 0) {
1584 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04001585 BUG_ON(!cache);
1586 len = min(num, cache->key.offset -
1587 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05001588 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04001589 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04001590 spin_lock(&cache->lock);
1591 cache->pinned += len;
1592 cache->space_info->bytes_pinned += len;
1593 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001594 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05001595 fs_info->total_pinned += len;
1596 } else {
Josef Bacik25179202008-10-29 14:49:05 -04001597 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04001598 spin_lock(&cache->lock);
1599 cache->pinned -= len;
1600 cache->space_info->bytes_pinned -= len;
1601 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001602 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05001603 fs_info->total_pinned -= len;
1604 }
1605 bytenr += len;
1606 num -= len;
1607 }
1608 return 0;
1609}
Chris Mason9078a3e2007-04-26 16:46:15 -04001610
Zheng Yane8569812008-09-26 10:05:48 -04001611static int update_reserved_extents(struct btrfs_root *root,
1612 u64 bytenr, u64 num, int reserve)
1613{
1614 u64 len;
1615 struct btrfs_block_group_cache *cache;
1616 struct btrfs_fs_info *fs_info = root->fs_info;
1617
Zheng Yane8569812008-09-26 10:05:48 -04001618 while (num > 0) {
1619 cache = btrfs_lookup_block_group(fs_info, bytenr);
1620 BUG_ON(!cache);
1621 len = min(num, cache->key.offset -
1622 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04001623
1624 spin_lock(&cache->space_info->lock);
1625 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04001626 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04001627 cache->reserved += len;
1628 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04001629 } else {
Zheng Yane8569812008-09-26 10:05:48 -04001630 cache->reserved -= len;
1631 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04001632 }
Josef Bacik25179202008-10-29 14:49:05 -04001633 spin_unlock(&cache->lock);
1634 spin_unlock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04001635 bytenr += len;
1636 num -= len;
1637 }
1638 return 0;
1639}
1640
Chris Masond1310b22008-01-24 16:13:08 -05001641int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001642{
Chris Masonccd467d2007-06-28 15:57:36 -04001643 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001644 u64 start;
1645 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001646 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001647 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001648
Josef Bacik25179202008-10-29 14:49:05 -04001649 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04001650 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001651 ret = find_first_extent_bit(pinned_extents, last,
1652 &start, &end, EXTENT_DIRTY);
1653 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001654 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001655 set_extent_dirty(copy, start, end, GFP_NOFS);
1656 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001657 }
Josef Bacik25179202008-10-29 14:49:05 -04001658 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04001659 return 0;
1660}
1661
1662int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1663 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001664 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001665{
Chris Mason1a5bc162007-10-15 16:15:26 -04001666 u64 start;
1667 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001668 int ret;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001669 struct btrfs_block_group_cache *cache;
Chris Masona28ec192007-03-06 20:08:01 -05001670
Josef Bacik25179202008-10-29 14:49:05 -04001671 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001672 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001673 ret = find_first_extent_bit(unpin, 0, &start, &end,
1674 EXTENT_DIRTY);
1675 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001676 break;
Chris Masone02119d2008-09-05 16:13:11 -04001677 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001678 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001679 cache = btrfs_lookup_block_group(root->fs_info, start);
1680 if (cache->cached)
1681 btrfs_add_free_space(cache, start, end - start + 1);
Chris Masonc286ac42008-07-22 23:06:41 -04001682 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04001683 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04001684 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04001685 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04001686 }
Chris Masona28ec192007-03-06 20:08:01 -05001687 }
Josef Bacik25179202008-10-29 14:49:05 -04001688 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001689 return 0;
1690}
1691
Chris Mason98ed5172008-01-03 10:01:48 -05001692static int finish_current_insert(struct btrfs_trans_handle *trans,
1693 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001694{
Chris Mason7bb86312007-12-11 09:25:06 -05001695 u64 start;
1696 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04001697 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04001698 u64 search = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001699 struct btrfs_fs_info *info = extent_root->fs_info;
1700 struct btrfs_path *path;
Zheng Yan31840ae2008-09-23 13:14:14 -04001701 struct btrfs_extent_ref *ref;
1702 struct pending_extent_op *extent_op;
1703 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -04001704 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001705 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -04001706 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001707
Chris Mason5f39d392007-10-15 16:14:19 -04001708 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001709 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001710
Chris Mason26b80032007-08-08 20:17:12 -04001711 while(1) {
Josef Bacik25179202008-10-29 14:49:05 -04001712 mutex_lock(&info->extent_ins_mutex);
1713 ret = find_first_extent_bit(&info->extent_ins, search, &start,
1714 &end, EXTENT_WRITEBACK);
1715 if (ret) {
1716 mutex_unlock(&info->extent_ins_mutex);
1717 if (search) {
1718 search = 0;
1719 continue;
1720 }
Chris Mason26b80032007-08-08 20:17:12 -04001721 break;
Josef Bacik25179202008-10-29 14:49:05 -04001722 }
1723
1724 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
1725 if (!ret) {
1726 search = end+1;
1727 mutex_unlock(&info->extent_ins_mutex);
1728 cond_resched();
1729 continue;
1730 }
1731 BUG_ON(ret < 0);
Chris Mason26b80032007-08-08 20:17:12 -04001732
Zheng Yan31840ae2008-09-23 13:14:14 -04001733 ret = get_state_private(&info->extent_ins, start, &priv);
1734 BUG_ON(ret);
1735 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1736
Josef Bacik25179202008-10-29 14:49:05 -04001737 mutex_unlock(&info->extent_ins_mutex);
1738
Zheng Yan31840ae2008-09-23 13:14:14 -04001739 if (extent_op->type == PENDING_EXTENT_INSERT) {
1740 key.objectid = start;
1741 key.offset = end + 1 - start;
1742 key.type = BTRFS_EXTENT_ITEM_KEY;
1743 err = btrfs_insert_item(trans, extent_root, &key,
Chris Mason1a5bc162007-10-15 16:15:26 -04001744 &extent_item, sizeof(extent_item));
Zheng Yan31840ae2008-09-23 13:14:14 -04001745 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001746
Josef Bacik25179202008-10-29 14:49:05 -04001747 mutex_lock(&info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001748 clear_extent_bits(&info->extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04001749 EXTENT_WRITEBACK, GFP_NOFS);
1750 mutex_unlock(&info->extent_ins_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04001751
Zheng Yan31840ae2008-09-23 13:14:14 -04001752 err = insert_extent_backref(trans, extent_root, path,
1753 start, extent_op->parent,
1754 extent_root->root_key.objectid,
1755 extent_op->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001756 extent_op->level);
Zheng Yan31840ae2008-09-23 13:14:14 -04001757 BUG_ON(err);
1758 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1759 err = lookup_extent_backref(trans, extent_root, path,
1760 start, extent_op->orig_parent,
1761 extent_root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001762 extent_op->orig_generation,
1763 extent_op->level, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001764 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001765
Josef Bacik25179202008-10-29 14:49:05 -04001766 mutex_lock(&info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001767 clear_extent_bits(&info->extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04001768 EXTENT_WRITEBACK, GFP_NOFS);
1769 mutex_unlock(&info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001770
1771 key.objectid = start;
1772 key.offset = extent_op->parent;
1773 key.type = BTRFS_EXTENT_REF_KEY;
1774 err = btrfs_set_item_key_safe(trans, extent_root, path,
1775 &key);
1776 BUG_ON(err);
1777 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1778 struct btrfs_extent_ref);
1779 btrfs_set_ref_generation(path->nodes[0], ref,
1780 extent_op->generation);
1781 btrfs_mark_buffer_dirty(path->nodes[0]);
1782 btrfs_release_path(extent_root, path);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001783 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001784 BUG_ON(1);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001785 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001786 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04001787 unlock_extent(&info->extent_ins, start, end, GFP_NOFS);
1788 search = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001789
Josef Bacik25179202008-10-29 14:49:05 -04001790 cond_resched();
Chris Mason037e6392007-03-07 11:50:24 -05001791 }
Chris Mason7bb86312007-12-11 09:25:06 -05001792 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001793 return 0;
1794}
1795
Zheng Yan31840ae2008-09-23 13:14:14 -04001796static int pin_down_bytes(struct btrfs_trans_handle *trans,
1797 struct btrfs_root *root,
1798 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04001799{
Chris Mason1a5bc162007-10-15 16:15:26 -04001800 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001801 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001802
Zheng Yan31840ae2008-09-23 13:14:14 -04001803 if (is_data)
1804 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001805
Zheng Yan31840ae2008-09-23 13:14:14 -04001806 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1807 if (!buf)
1808 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001809
Zheng Yan31840ae2008-09-23 13:14:14 -04001810 /* we can reuse a block if it hasn't been written
1811 * and it is from this transaction. We can't
1812 * reuse anything from the tree log root because
1813 * it has tiny sub-transactions.
1814 */
1815 if (btrfs_buffer_uptodate(buf, 0) &&
1816 btrfs_try_tree_lock(buf)) {
1817 u64 header_owner = btrfs_header_owner(buf);
1818 u64 header_transid = btrfs_header_generation(buf);
1819 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04001820 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04001821 header_transid == trans->transid &&
1822 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1823 clean_tree_block(NULL, root, buf);
1824 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001825 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001826 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04001827 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001828 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001829 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001830 free_extent_buffer(buf);
1831pinit:
1832 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
1833
Chris Masonbe744172007-05-06 10:15:01 -04001834 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001835 return 0;
1836}
1837
Chris Masona28ec192007-03-06 20:08:01 -05001838/*
1839 * remove an extent from the root, returns 0 on success
1840 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001841static int __free_extent(struct btrfs_trans_handle *trans,
1842 struct btrfs_root *root,
1843 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05001844 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001845 u64 owner_objectid, int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001846{
Chris Mason5caf2a02007-04-02 11:20:42 -04001847 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001848 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001849 struct btrfs_fs_info *info = root->fs_info;
1850 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001851 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001852 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001853 int extent_slot = 0;
1854 int found_extent = 0;
1855 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001856 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001857 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001858
Chris Masondb945352007-10-15 16:15:53 -04001859 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001860 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001861 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001862 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001863 if (!path)
1864 return -ENOMEM;
1865
Chris Mason3c12ac72008-04-21 12:01:38 -04001866 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001867 ret = lookup_extent_backref(trans, extent_root, path,
1868 bytenr, parent, root_objectid,
1869 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001870 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001871 struct btrfs_key found_key;
1872 extent_slot = path->slots[0];
1873 while(extent_slot > 0) {
1874 extent_slot--;
1875 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1876 extent_slot);
1877 if (found_key.objectid != bytenr)
1878 break;
1879 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1880 found_key.offset == num_bytes) {
1881 found_extent = 1;
1882 break;
1883 }
1884 if (path->slots[0] - extent_slot > 5)
1885 break;
1886 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001887 if (!found_extent) {
1888 ret = remove_extent_backref(trans, extent_root, path);
1889 BUG_ON(ret);
1890 btrfs_release_path(extent_root, path);
1891 ret = btrfs_search_slot(trans, extent_root,
1892 &key, path, -1, 1);
1893 BUG_ON(ret);
1894 extent_slot = path->slots[0];
1895 }
Chris Mason7bb86312007-12-11 09:25:06 -05001896 } else {
1897 btrfs_print_leaf(extent_root, path->nodes[0]);
1898 WARN_ON(1);
1899 printk("Unable to find ref byte nr %Lu root %Lu "
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001900 "gen %Lu owner %Lu\n", bytenr,
1901 root_objectid, ref_generation, owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05001902 }
Chris Mason5f39d392007-10-15 16:14:19 -04001903
1904 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001905 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001906 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001907 refs = btrfs_extent_refs(leaf, ei);
1908 BUG_ON(refs == 0);
1909 refs -= 1;
1910 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001911
Chris Mason5f39d392007-10-15 16:14:19 -04001912 btrfs_mark_buffer_dirty(leaf);
1913
Chris Mason952fcca2008-02-18 16:33:44 -05001914 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001915 struct btrfs_extent_ref *ref;
1916 ref = btrfs_item_ptr(leaf, path->slots[0],
1917 struct btrfs_extent_ref);
1918 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001919 /* if the back ref and the extent are next to each other
1920 * they get deleted below in one shot
1921 */
1922 path->slots[0] = extent_slot;
1923 num_to_del = 2;
1924 } else if (found_extent) {
1925 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001926 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05001927 BUG_ON(ret);
1928 /* if refs are 0, we need to setup the path for deletion */
1929 if (refs == 0) {
1930 btrfs_release_path(extent_root, path);
1931 ret = btrfs_search_slot(trans, extent_root, &key, path,
1932 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001933 BUG_ON(ret);
1934 }
1935 }
1936
Chris Masoncf27e1e2007-03-13 09:49:06 -04001937 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001938 u64 super_used;
1939 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01001940#ifdef BIO_RW_DISCARD
1941 u64 map_length = num_bytes;
1942 struct btrfs_multi_bio *multi = NULL;
1943#endif
Chris Mason78fae272007-03-25 11:35:08 -04001944
1945 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04001946 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001947 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
1948 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04001949 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05001950 if (ret > 0)
1951 mark_free = 1;
1952 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001953 }
1954
Josef Bacik58176a92007-08-29 15:47:34 -04001955 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04001956 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04001957 super_used = btrfs_super_bytes_used(&info->super_copy);
1958 btrfs_set_super_bytes_used(&info->super_copy,
1959 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04001960 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04001961
1962 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001963 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001964 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001965 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001966 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1967 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04001968 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04001969 btrfs_release_path(extent_root, path);
Chris Masondb945352007-10-15 16:15:53 -04001970 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001971 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001972 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01001973
1974#ifdef BIO_RW_DISCARD
1975 /* Tell the block device(s) that the sectors can be discarded */
1976 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1977 bytenr, &map_length, &multi, 0);
1978 if (!ret) {
1979 struct btrfs_bio_stripe *stripe = multi->stripes;
1980 int i;
1981
1982 if (map_length > num_bytes)
1983 map_length = num_bytes;
1984
1985 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1986 blkdev_issue_discard(stripe->dev->bdev,
1987 stripe->physical >> 9,
1988 map_length >> 9);
1989 }
1990 kfree(multi);
1991 }
1992#endif
Chris Masona28ec192007-03-06 20:08:01 -05001993 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001994 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001995 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05001996 return ret;
1997}
1998
1999/*
Chris Masonfec577f2007-02-26 10:40:21 -05002000 * find all the blocks marked as pending in the radix tree and remove
2001 * them from the extent map
2002 */
Chris Masone089f052007-03-16 16:20:31 -04002003static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2004 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05002005{
2006 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002007 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002008 u64 start;
2009 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002010 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002011 u64 search = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002012 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002013 struct extent_io_tree *extent_ins;
2014 struct pending_extent_op *extent_op;
Josef Bacik25179202008-10-29 14:49:05 -04002015 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason8ef97622007-03-26 10:15:30 -04002016
Zheng Yan31840ae2008-09-23 13:14:14 -04002017 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc162007-10-15 16:15:26 -04002018 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002019
2020 while(1) {
Josef Bacik25179202008-10-29 14:49:05 -04002021 mutex_lock(&info->extent_ins_mutex);
2022 ret = find_first_extent_bit(pending_del, search, &start, &end,
2023 EXTENT_WRITEBACK);
2024 if (ret) {
2025 mutex_unlock(&info->extent_ins_mutex);
2026 if (search) {
2027 search = 0;
2028 continue;
2029 }
Chris Masonfec577f2007-02-26 10:40:21 -05002030 break;
Josef Bacik25179202008-10-29 14:49:05 -04002031 }
2032
2033 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2034 if (!ret) {
2035 search = end+1;
2036 mutex_unlock(&info->extent_ins_mutex);
2037 cond_resched();
2038 continue;
2039 }
2040 BUG_ON(ret < 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002041
2042 ret = get_state_private(pending_del, start, &priv);
2043 BUG_ON(ret);
2044 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2045
Josef Bacik25179202008-10-29 14:49:05 -04002046 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
Chris Mason1a5bc162007-10-15 16:15:26 -04002047 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002048 if (!test_range_bit(extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04002049 EXTENT_WRITEBACK, 0)) {
2050 mutex_unlock(&info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002051free_extent:
Chris Masonc286ac42008-07-22 23:06:41 -04002052 ret = __free_extent(trans, extent_root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002053 start, end + 1 - start,
2054 extent_op->orig_parent,
2055 extent_root->root_key.objectid,
2056 extent_op->orig_generation,
Josef Bacik25179202008-10-29 14:49:05 -04002057 extent_op->level, 1, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002058 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002059 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002060 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002061
2062 ret = get_state_private(&info->extent_ins, start,
2063 &priv);
Zheng Yan31840ae2008-09-23 13:14:14 -04002064 BUG_ON(ret);
2065 extent_op = (struct pending_extent_op *)
Josef Bacik25179202008-10-29 14:49:05 -04002066 (unsigned long)priv;
Zheng Yan31840ae2008-09-23 13:14:14 -04002067
Josef Bacik25179202008-10-29 14:49:05 -04002068 clear_extent_bits(&info->extent_ins, start, end,
2069 EXTENT_WRITEBACK, GFP_NOFS);
2070
2071 mutex_unlock(&info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002072
2073 if (extent_op->type == PENDING_BACKREF_UPDATE)
2074 goto free_extent;
2075
Josef Bacik25179202008-10-29 14:49:05 -04002076 mutex_lock(&extent_root->fs_info->pinned_mutex);
2077 ret = pin_down_bytes(trans, extent_root, start,
2078 end + 1 - start, 0);
2079 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2080
Zheng Yan31840ae2008-09-23 13:14:14 -04002081 ret = update_block_group(trans, extent_root, start,
Josef Bacik25179202008-10-29 14:49:05 -04002082 end + 1 - start, 0, ret > 0);
2083
Zheng Yan31840ae2008-09-23 13:14:14 -04002084 BUG_ON(ret);
2085 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002086 }
Chris Mason1a5bc162007-10-15 16:15:26 -04002087 if (ret)
2088 err = ret;
Josef Bacik25179202008-10-29 14:49:05 -04002089 unlock_extent(extent_ins, start, end, GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04002090
Josef Bacik25179202008-10-29 14:49:05 -04002091 search = 0;
2092 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05002093 }
Chris Masone20d96d2007-03-22 12:13:20 -04002094 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002095}
2096
2097/*
2098 * remove an extent from the root, returns 0 on success
2099 */
Chris Mason925baed2008-06-25 16:01:30 -04002100static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002101 struct btrfs_root *root,
2102 u64 bytenr, u64 num_bytes, u64 parent,
2103 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002104 u64 owner_objectid, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002105{
Chris Mason9f5fae22007-03-20 14:38:32 -04002106 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002107 int pending_ret;
2108 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002109
Chris Masondb945352007-10-15 16:15:53 -04002110 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002111 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002112 struct pending_extent_op *extent_op;
2113
2114 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2115 BUG_ON(!extent_op);
2116
2117 extent_op->type = PENDING_EXTENT_DELETE;
2118 extent_op->bytenr = bytenr;
2119 extent_op->num_bytes = num_bytes;
2120 extent_op->parent = parent;
2121 extent_op->orig_parent = parent;
2122 extent_op->generation = ref_generation;
2123 extent_op->orig_generation = ref_generation;
2124 extent_op->level = (int)owner_objectid;
2125
Josef Bacik25179202008-10-29 14:49:05 -04002126 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002127 set_extent_bits(&root->fs_info->pending_del,
2128 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04002129 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002130 set_state_private(&root->fs_info->pending_del,
2131 bytenr, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002132 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002133 return 0;
2134 }
Chris Mason4bef0842008-09-08 11:18:08 -04002135 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002136 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2137 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002138 struct btrfs_block_group_cache *cache;
2139
Chris Masond00aff02008-09-11 15:54:42 -04002140 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002141 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2142 BUG_ON(!cache);
2143 btrfs_add_free_space(cache, bytenr, num_bytes);
Zheng Yane8569812008-09-26 10:05:48 -04002144 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002145 return 0;
2146 }
Chris Mason4bef0842008-09-08 11:18:08 -04002147 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002148 }
Chris Mason4bef0842008-09-08 11:18:08 -04002149
2150 /* if data pin when any transaction has committed this */
2151 if (ref_generation != trans->transid)
2152 pin = 1;
2153
Zheng Yan31840ae2008-09-23 13:14:14 -04002154 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002155 root_objectid, ref_generation,
2156 owner_objectid, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002157
2158 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002159 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05002160 return ret ? ret : pending_ret;
2161}
2162
Chris Mason925baed2008-06-25 16:01:30 -04002163int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002164 struct btrfs_root *root,
2165 u64 bytenr, u64 num_bytes, u64 parent,
2166 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002167 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002168{
2169 int ret;
2170
Zheng Yan31840ae2008-09-23 13:14:14 -04002171 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002172 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002173 owner_objectid, pin);
Chris Mason925baed2008-06-25 16:01:30 -04002174 return ret;
2175}
2176
Chris Mason87ee04e2007-11-30 11:30:34 -05002177static u64 stripe_align(struct btrfs_root *root, u64 val)
2178{
2179 u64 mask = ((u64)root->stripesize - 1);
2180 u64 ret = (val + mask) & ~mask;
2181 return ret;
2182}
2183
Chris Masonfec577f2007-02-26 10:40:21 -05002184/*
2185 * walks the btree of allocated extents and find a hole of a given size.
2186 * The key ins is changed to record the hole:
2187 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002188 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002189 * ins->offset == number of blocks
2190 * Any available blocks before search_start are skipped.
2191 */
Chris Mason98ed5172008-01-03 10:01:48 -05002192static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2193 struct btrfs_root *orig_root,
2194 u64 num_bytes, u64 empty_size,
2195 u64 search_start, u64 search_end,
2196 u64 hint_byte, struct btrfs_key *ins,
2197 u64 exclude_start, u64 exclude_nr,
2198 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002199{
Josef Bacik80eb2342008-10-29 14:49:05 -04002200 int ret = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -04002201 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002202 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002203 u64 *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04002204 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002205 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002206 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002207 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002208 struct list_head *head = NULL, *cur = NULL;
2209 int loop = 0;
2210 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002211
Chris Masondb945352007-10-15 16:15:53 -04002212 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002213 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002214 ins->objectid = 0;
2215 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002216
Chris Mason0ef3e662008-05-24 14:04:53 -04002217 if (orig_root->ref_cows || empty_size)
2218 allowed_chunk_alloc = 1;
2219
Chris Mason239b14b2008-03-24 15:02:07 -04002220 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2221 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04002222 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002223 }
2224
Josef Bacik0f9dd462008-09-23 13:14:11 -04002225 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002226 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002227
Chris Mason239b14b2008-03-24 15:02:07 -04002228 if (last_ptr) {
2229 if (*last_ptr)
2230 hint_byte = *last_ptr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002231 else
Chris Mason239b14b2008-03-24 15:02:07 -04002232 empty_size += empty_cluster;
Chris Mason239b14b2008-03-24 15:02:07 -04002233 }
Chris Masona061fc82008-05-07 11:43:44 -04002234 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002235 search_start = max(search_start, hint_byte);
Chris Mason6702ed42007-08-07 16:15:09 -04002236 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002237
Josef Bacik80eb2342008-10-29 14:49:05 -04002238 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
2239 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002240
Josef Bacik80eb2342008-10-29 14:49:05 -04002241 down_read(&space_info->groups_sem);
2242 while (1) {
2243 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002244 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002245 * the only way this happens if our hint points to a block
2246 * group thats not of the proper type, while looping this
2247 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002248 */
Josef Bacik25179202008-10-29 14:49:05 -04002249 WARN_ON(!block_group);
2250 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002251 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002252 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002253
Josef Bacik80eb2342008-10-29 14:49:05 -04002254 ret = cache_block_group(root, block_group);
Josef Bacik25179202008-10-29 14:49:05 -04002255 if (ret) {
2256 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002257 break;
Josef Bacik25179202008-10-29 14:49:05 -04002258 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002259
Josef Bacik80eb2342008-10-29 14:49:05 -04002260 if (block_group->ro)
2261 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002262
Josef Bacik80eb2342008-10-29 14:49:05 -04002263 free_space = btrfs_find_free_space(block_group, search_start,
2264 total_needed);
2265 if (free_space) {
2266 u64 start = block_group->key.objectid;
2267 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002268 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002269
Josef Bacik80eb2342008-10-29 14:49:05 -04002270 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002271
Josef Bacik80eb2342008-10-29 14:49:05 -04002272 /* move on to the next group */
2273 if (search_start + num_bytes >= search_end)
2274 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002275
Josef Bacik80eb2342008-10-29 14:49:05 -04002276 /* move on to the next group */
2277 if (search_start + num_bytes > end)
2278 goto new_group;
2279
2280 if (exclude_nr > 0 &&
2281 (search_start + num_bytes > exclude_start &&
2282 search_start < exclude_start + exclude_nr)) {
2283 search_start = exclude_start + exclude_nr;
2284 /*
2285 * if search_start is still in this block group
2286 * then we just re-search this block group
2287 */
2288 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002289 search_start < end) {
2290 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002291 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002292 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002293
2294 /* else we go to the next block group */
2295 goto new_group;
2296 }
2297
2298 ins->objectid = search_start;
2299 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002300
2301 btrfs_remove_free_space_lock(block_group, search_start,
2302 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002303 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002304 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002305 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002306 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002307new_group:
Josef Bacik25179202008-10-29 14:49:05 -04002308 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002309 /*
2310 * Here's how this works.
2311 * loop == 0: we were searching a block group via a hint
2312 * and didn't find anything, so we start at
2313 * the head of the block groups and keep searching
2314 * loop == 1: we're searching through all of the block groups
2315 * if we hit the head again we have searched
2316 * all of the block groups for this space and we
2317 * need to try and allocate, if we cant error out.
2318 * loop == 2: we allocated more space and are looping through
2319 * all of the block groups again.
2320 */
2321 if (loop == 0) {
2322 head = &space_info->block_groups;
2323 cur = head->next;
Chris Mason0b86a832008-03-24 15:01:56 -04002324
Josef Bacik80eb2342008-10-29 14:49:05 -04002325 if (last_ptr && *last_ptr) {
2326 total_needed += empty_cluster;
2327 *last_ptr = 0;
2328 }
2329 loop++;
2330 } else if (loop == 1 && cur == head) {
2331 if (allowed_chunk_alloc && !chunk_alloc_done) {
2332 up_read(&space_info->groups_sem);
2333 ret = do_chunk_alloc(trans, root, num_bytes +
2334 2 * 1024 * 1024, data, 1);
2335 if (ret < 0)
2336 break;
2337 down_read(&space_info->groups_sem);
2338 loop++;
2339 head = &space_info->block_groups;
2340 cur = head->next;
2341 chunk_alloc_done = 1;
2342 } else if (!allowed_chunk_alloc) {
2343 space_info->force_alloc = 1;
2344 break;
2345 } else {
2346 break;
2347 }
2348 } else if (cur == head) {
2349 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002350 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002351
2352 block_group = list_entry(cur, struct btrfs_block_group_cache,
2353 list);
2354 search_start = block_group->key.objectid;
2355 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002356 }
Chris Mason0b86a832008-03-24 15:01:56 -04002357
Josef Bacik80eb2342008-10-29 14:49:05 -04002358 /* we found what we needed */
2359 if (ins->objectid) {
2360 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2361 trans->block_group = block_group;
2362
2363 if (last_ptr)
2364 *last_ptr = ins->objectid + ins->offset;
2365 ret = 0;
2366 } else if (!ret) {
2367 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04002368 }
Chris Mason0b86a832008-03-24 15:01:56 -04002369
Josef Bacik80eb2342008-10-29 14:49:05 -04002370 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05002371 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002372}
Chris Masonec44a352008-04-28 15:29:52 -04002373
Josef Bacik0f9dd462008-09-23 13:14:11 -04002374static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2375{
2376 struct btrfs_block_group_cache *cache;
2377 struct list_head *l;
2378
2379 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
Zheng Yane8569812008-09-26 10:05:48 -04002380 info->total_bytes - info->bytes_used - info->bytes_pinned -
2381 info->bytes_reserved, (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002382
Josef Bacik80eb2342008-10-29 14:49:05 -04002383 down_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002384 list_for_each(l, &info->block_groups) {
2385 cache = list_entry(l, struct btrfs_block_group_cache, list);
2386 spin_lock(&cache->lock);
2387 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
Zheng Yane8569812008-09-26 10:05:48 -04002388 "%Lu pinned %Lu reserved\n",
Josef Bacik0f9dd462008-09-23 13:14:11 -04002389 cache->key.objectid, cache->key.offset,
Zheng Yane8569812008-09-26 10:05:48 -04002390 btrfs_block_group_used(&cache->item),
2391 cache->pinned, cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002392 btrfs_dump_free_space(cache, bytes);
2393 spin_unlock(&cache->lock);
2394 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002395 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002396}
Zheng Yane8569812008-09-26 10:05:48 -04002397
Chris Masone6dcd2d2008-07-17 12:53:50 -04002398static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2399 struct btrfs_root *root,
2400 u64 num_bytes, u64 min_alloc_size,
2401 u64 empty_size, u64 hint_byte,
2402 u64 search_end, struct btrfs_key *ins,
2403 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05002404{
2405 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04002406 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002407 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04002408 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04002409
Chris Mason6324fbf2008-03-24 15:01:59 -04002410 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04002411 alloc_profile = info->avail_data_alloc_bits &
2412 info->data_alloc_profile;
2413 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002414 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04002415 alloc_profile = info->avail_system_alloc_bits &
2416 info->system_alloc_profile;
2417 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002418 } else {
Chris Mason8790d502008-04-03 16:29:03 -04002419 alloc_profile = info->avail_metadata_alloc_bits &
2420 info->metadata_alloc_profile;
2421 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002422 }
Chris Mason98d20f62008-04-14 09:46:10 -04002423again:
Chris Masona061fc82008-05-07 11:43:44 -04002424 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04002425 /*
2426 * the only place that sets empty_size is btrfs_realloc_node, which
2427 * is not called recursively on allocations
2428 */
2429 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04002430 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04002431 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002432 2 * 1024 * 1024,
2433 BTRFS_BLOCK_GROUP_METADATA |
2434 (info->metadata_alloc_profile &
2435 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002436 }
2437 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002438 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002439 }
Chris Mason0b86a832008-03-24 15:01:56 -04002440
Chris Masondb945352007-10-15 16:15:53 -04002441 WARN_ON(num_bytes < root->sectorsize);
2442 ret = find_free_extent(trans, root, num_bytes, empty_size,
2443 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002444 trans->alloc_exclude_start,
2445 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002446
Chris Mason98d20f62008-04-14 09:46:10 -04002447 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2448 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002449 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002450 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002451 do_chunk_alloc(trans, root->fs_info->extent_root,
2452 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002453 goto again;
2454 }
Chris Masonec44a352008-04-28 15:29:52 -04002455 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002456 struct btrfs_space_info *sinfo;
2457
2458 sinfo = __find_space_info(root->fs_info, data);
2459 printk("allocation failed flags %Lu, wanted %Lu\n",
2460 data, num_bytes);
2461 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04002462 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002463 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002464
2465 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002466}
2467
Chris Mason65b51a02008-08-01 15:11:20 -04002468int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2469{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002470 struct btrfs_block_group_cache *cache;
2471
Josef Bacik0f9dd462008-09-23 13:14:11 -04002472 cache = btrfs_lookup_block_group(root->fs_info, start);
2473 if (!cache) {
2474 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002475 return -ENOSPC;
2476 }
2477 btrfs_add_free_space(cache, start, len);
Zheng Yan1a40e232008-09-26 10:09:34 -04002478 update_reserved_extents(root, start, len, 0);
Chris Mason65b51a02008-08-01 15:11:20 -04002479 return 0;
2480}
2481
Chris Masone6dcd2d2008-07-17 12:53:50 -04002482int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2483 struct btrfs_root *root,
2484 u64 num_bytes, u64 min_alloc_size,
2485 u64 empty_size, u64 hint_byte,
2486 u64 search_end, struct btrfs_key *ins,
2487 u64 data)
2488{
2489 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002490 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2491 empty_size, hint_byte, search_end, ins,
2492 data);
Zheng Yane8569812008-09-26 10:05:48 -04002493 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002494 return ret;
2495}
2496
2497static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002498 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002499 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002500 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002501{
2502 int ret;
2503 int pending_ret;
2504 u64 super_used;
2505 u64 root_used;
2506 u64 num_bytes = ins->offset;
2507 u32 sizes[2];
2508 struct btrfs_fs_info *info = root->fs_info;
2509 struct btrfs_root *extent_root = info->extent_root;
2510 struct btrfs_extent_item *extent_item;
2511 struct btrfs_extent_ref *ref;
2512 struct btrfs_path *path;
2513 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002514
Zheng Yan31840ae2008-09-23 13:14:14 -04002515 if (parent == 0)
2516 parent = ins->objectid;
2517
Josef Bacik58176a92007-08-29 15:47:34 -04002518 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002519 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002520 super_used = btrfs_super_bytes_used(&info->super_copy);
2521 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002522 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04002523
Josef Bacik58176a92007-08-29 15:47:34 -04002524 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002525 root_used = btrfs_root_used(&root->root_item);
2526 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002527
Chris Mason26b80032007-08-08 20:17:12 -04002528 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002529 struct pending_extent_op *extent_op;
2530
2531 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2532 BUG_ON(!extent_op);
2533
2534 extent_op->type = PENDING_EXTENT_INSERT;
2535 extent_op->bytenr = ins->objectid;
2536 extent_op->num_bytes = ins->offset;
2537 extent_op->parent = parent;
2538 extent_op->orig_parent = 0;
2539 extent_op->generation = ref_generation;
2540 extent_op->orig_generation = 0;
2541 extent_op->level = (int)owner;
2542
Josef Bacik25179202008-10-29 14:49:05 -04002543 mutex_lock(&root->fs_info->extent_ins_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04002544 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2545 ins->objectid + ins->offset - 1,
Josef Bacik25179202008-10-29 14:49:05 -04002546 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002547 set_state_private(&root->fs_info->extent_ins,
2548 ins->objectid, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002549 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04002550 goto update_block;
2551 }
2552
Chris Mason47e4bb92008-02-01 14:51:59 -05002553 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05002554 keys[1].objectid = ins->objectid;
2555 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04002556 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05002557 sizes[0] = sizeof(*extent_item);
2558 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002559
2560 path = btrfs_alloc_path();
2561 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002562
2563 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2564 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04002565 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002566
Chris Mason47e4bb92008-02-01 14:51:59 -05002567 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2568 struct btrfs_extent_item);
2569 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2570 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2571 struct btrfs_extent_ref);
2572
2573 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2574 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2575 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Zheng Yan31840ae2008-09-23 13:14:14 -04002576 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05002577
2578 btrfs_mark_buffer_dirty(path->nodes[0]);
2579
2580 trans->alloc_exclude_start = 0;
2581 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002582 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002583 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002584 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04002585
Chris Mason925baed2008-06-25 16:01:30 -04002586 if (ret)
2587 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002588 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04002589 ret = pending_ret;
2590 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002591 }
Chris Mason26b80032007-08-08 20:17:12 -04002592
2593update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04002594 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002595 if (ret) {
2596 printk("update block group failed for %Lu %Lu\n",
2597 ins->objectid, ins->offset);
2598 BUG();
2599 }
Chris Mason925baed2008-06-25 16:01:30 -04002600out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04002601 return ret;
2602}
2603
2604int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002605 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002606 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002607 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002608{
2609 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04002610
2611 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2612 return 0;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002613 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
2614 ref_generation, owner, ins);
Zheng Yane8569812008-09-26 10:05:48 -04002615 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002616 return ret;
2617}
Chris Masone02119d2008-09-05 16:13:11 -04002618
2619/*
2620 * this is used by the tree logging recovery code. It records that
2621 * an extent has been allocated and makes sure to clear the free
2622 * space cache bits as well
2623 */
2624int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002625 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04002626 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002627 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04002628{
2629 int ret;
2630 struct btrfs_block_group_cache *block_group;
2631
Chris Masone02119d2008-09-05 16:13:11 -04002632 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik25179202008-10-29 14:49:05 -04002633 mutex_lock(&block_group->alloc_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002634 cache_block_group(root, block_group);
2635
Josef Bacik25179202008-10-29 14:49:05 -04002636 ret = btrfs_remove_free_space_lock(block_group, ins->objectid,
2637 ins->offset);
2638 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002639 BUG_ON(ret);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002640 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
2641 ref_generation, owner, ins);
Chris Masone02119d2008-09-05 16:13:11 -04002642 return ret;
2643}
2644
Chris Masone6dcd2d2008-07-17 12:53:50 -04002645/*
2646 * finds a free extent and does all the dirty work required for allocation
2647 * returns the key for the extent through ins, and a tree buffer for
2648 * the first block of the extent through buf.
2649 *
2650 * returns 0 if everything worked, non-zero otherwise.
2651 */
2652int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2653 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002654 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002655 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002656 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002657 u64 search_end, struct btrfs_key *ins, u64 data)
2658{
2659 int ret;
2660
Chris Masone6dcd2d2008-07-17 12:53:50 -04002661 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2662 min_alloc_size, empty_size, hint_byte,
2663 search_end, ins, data);
2664 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04002665 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002666 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2667 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002668 owner_objectid, ins);
Chris Masond00aff02008-09-11 15:54:42 -04002669 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002670
Zheng Yane8569812008-09-26 10:05:48 -04002671 } else {
2672 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04002673 }
Chris Mason925baed2008-06-25 16:01:30 -04002674 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002675}
Chris Mason65b51a02008-08-01 15:11:20 -04002676
2677struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2678 struct btrfs_root *root,
2679 u64 bytenr, u32 blocksize)
2680{
2681 struct extent_buffer *buf;
2682
2683 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2684 if (!buf)
2685 return ERR_PTR(-ENOMEM);
2686 btrfs_set_header_generation(buf, trans->transid);
2687 btrfs_tree_lock(buf);
2688 clean_tree_block(trans, root, buf);
2689 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04002690 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2691 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04002692 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002693 } else {
2694 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2695 buf->start + buf->len - 1, GFP_NOFS);
2696 }
Chris Mason65b51a02008-08-01 15:11:20 -04002697 trans->blocks_used++;
2698 return buf;
2699}
2700
Chris Masonfec577f2007-02-26 10:40:21 -05002701/*
2702 * helper function to allocate a block for a given tree
2703 * returns the tree buffer or NULL.
2704 */
Chris Mason5f39d392007-10-15 16:14:19 -04002705struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04002706 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002707 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002708 u64 root_objectid,
2709 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05002710 int level,
2711 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04002712 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05002713{
Chris Masone2fa7222007-03-12 16:22:34 -04002714 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05002715 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002716 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05002717
Zheng Yan31840ae2008-09-23 13:14:14 -04002718 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002719 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04002720 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002721 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04002722 BUG_ON(ret > 0);
2723 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05002724 }
Chris Mason55c69072008-01-09 15:55:33 -05002725
Chris Mason65b51a02008-08-01 15:11:20 -04002726 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05002727 return buf;
2728}
Chris Masona28ec192007-03-06 20:08:01 -05002729
Chris Masone02119d2008-09-05 16:13:11 -04002730int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
2731 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04002732{
Chris Mason7bb86312007-12-11 09:25:06 -05002733 u64 leaf_owner;
2734 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04002735 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002736 struct btrfs_file_extent_item *fi;
2737 int i;
2738 int nritems;
2739 int ret;
2740
Chris Mason5f39d392007-10-15 16:14:19 -04002741 BUG_ON(!btrfs_is_leaf(leaf));
2742 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05002743 leaf_owner = btrfs_header_owner(leaf);
2744 leaf_generation = btrfs_header_generation(leaf);
2745
Chris Mason6407bf62007-03-27 06:33:00 -04002746 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002747 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04002748 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04002749
2750 btrfs_item_key_to_cpu(leaf, &key, i);
2751 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04002752 continue;
2753 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002754 if (btrfs_file_extent_type(leaf, fi) ==
2755 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04002756 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04002757 /*
2758 * FIXME make sure to insert a trans record that
2759 * repeats the snapshot del on crash
2760 */
Chris Masondb945352007-10-15 16:15:53 -04002761 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2762 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04002763 continue;
Chris Mason4a096752008-07-21 10:29:44 -04002764
Chris Mason925baed2008-06-25 16:01:30 -04002765 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002766 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04002767 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002768 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002769 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04002770
2771 atomic_inc(&root->fs_info->throttle_gen);
2772 wake_up(&root->fs_info->transaction_throttle);
2773 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04002774 }
2775 return 0;
2776}
2777
Chris Masone02119d2008-09-05 16:13:11 -04002778static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2779 struct btrfs_root *root,
2780 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04002781{
2782 int i;
2783 int ret;
2784 struct btrfs_extent_info *info = ref->extents;
2785
Yan Zheng31153d82008-07-28 15:32:19 -04002786 for (i = 0; i < ref->nritems; i++) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002787 ret = __btrfs_free_extent(trans, root, info->bytenr,
2788 info->num_bytes, ref->bytenr,
2789 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002790 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04002791
2792 atomic_inc(&root->fs_info->throttle_gen);
2793 wake_up(&root->fs_info->transaction_throttle);
2794 cond_resched();
2795
Yan Zheng31153d82008-07-28 15:32:19 -04002796 BUG_ON(ret);
2797 info++;
2798 }
Yan Zheng31153d82008-07-28 15:32:19 -04002799
2800 return 0;
2801}
2802
Chris Mason333db942008-06-25 16:01:30 -04002803int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2804 u32 *refs)
2805{
Chris Mason017e5362008-07-28 15:32:51 -04002806 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04002807
Zheng Yan31840ae2008-09-23 13:14:14 -04002808 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04002809 BUG_ON(ret);
2810
2811#if 0 // some debugging code in case we see problems here
2812 /* if the refs count is one, it won't get increased again. But
2813 * if the ref count is > 1, someone may be decreasing it at
2814 * the same time we are.
2815 */
2816 if (*refs != 1) {
2817 struct extent_buffer *eb = NULL;
2818 eb = btrfs_find_create_tree_block(root, start, len);
2819 if (eb)
2820 btrfs_tree_lock(eb);
2821
2822 mutex_lock(&root->fs_info->alloc_mutex);
2823 ret = lookup_extent_ref(NULL, root, start, len, refs);
2824 BUG_ON(ret);
2825 mutex_unlock(&root->fs_info->alloc_mutex);
2826
2827 if (eb) {
2828 btrfs_tree_unlock(eb);
2829 free_extent_buffer(eb);
2830 }
2831 if (*refs == 1) {
2832 printk("block %llu went down to one during drop_snap\n",
2833 (unsigned long long)start);
2834 }
2835
2836 }
2837#endif
2838
Chris Masone7a84562008-06-25 16:01:31 -04002839 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04002840 return ret;
Chris Mason333db942008-06-25 16:01:30 -04002841}
2842
2843/*
Chris Mason9aca1d52007-03-13 11:09:37 -04002844 * helper function for drop_snapshot, this walks down the tree dropping ref
2845 * counts as it goes.
2846 */
Chris Mason98ed5172008-01-03 10:01:48 -05002847static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2848 struct btrfs_root *root,
2849 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002850{
Chris Mason7bb86312007-12-11 09:25:06 -05002851 u64 root_owner;
2852 u64 root_gen;
2853 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04002854 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002855 struct extent_buffer *next;
2856 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002857 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04002858 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04002859 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002860 int ret;
2861 u32 refs;
2862
Chris Mason5caf2a02007-04-02 11:20:42 -04002863 WARN_ON(*level < 0);
2864 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04002865 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04002866 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002867 BUG_ON(ret);
2868 if (refs > 1)
2869 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002870
Chris Mason9aca1d52007-03-13 11:09:37 -04002871 /*
2872 * walk down to the last node level and free all the leaves
2873 */
Chris Mason6407bf62007-03-27 06:33:00 -04002874 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002875 WARN_ON(*level < 0);
2876 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002877 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002878
Chris Mason5f39d392007-10-15 16:14:19 -04002879 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002880 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002881
Chris Mason7518a232007-03-12 12:01:18 -04002882 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002883 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002884 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002885 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002886 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04002887 BUG_ON(ret);
2888 break;
2889 }
Chris Masondb945352007-10-15 16:15:53 -04002890 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04002891 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04002892 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002893
Chris Mason333db942008-06-25 16:01:30 -04002894 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002895 BUG_ON(ret);
2896 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002897 parent = path->nodes[*level];
2898 root_owner = btrfs_header_owner(parent);
2899 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002900 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04002901
Chris Mason925baed2008-06-25 16:01:30 -04002902 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04002903 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002904 root_owner, root_gen,
2905 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002906 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04002907
2908 atomic_inc(&root->fs_info->throttle_gen);
2909 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04002910 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04002911
Chris Mason20524f02007-03-10 06:35:47 -05002912 continue;
2913 }
Chris Masonf87f0572008-08-01 11:27:23 -04002914 /*
2915 * at this point, we have a single ref, and since the
2916 * only place referencing this extent is a dead root
2917 * the reference count should never go higher.
2918 * So, we don't need to check it again
2919 */
Yan Zheng31153d82008-07-28 15:32:19 -04002920 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04002921 ref = btrfs_lookup_leaf_ref(root, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04002922 if (ref && ref->generation != ptr_gen) {
2923 btrfs_free_leaf_ref(root, ref);
2924 ref = NULL;
2925 }
Yan Zheng31153d82008-07-28 15:32:19 -04002926 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04002927 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002928 BUG_ON(ret);
2929 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04002930 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002931 *level = 0;
2932 break;
2933 }
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002934 if (printk_ratelimit()) {
Chris Mason37d1aee2008-07-31 10:48:37 -04002935 printk("leaf ref miss for bytenr %llu\n",
2936 (unsigned long long)bytenr);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002937 }
Yan Zheng31153d82008-07-28 15:32:19 -04002938 }
Chris Masondb945352007-10-15 16:15:53 -04002939 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04002940 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002941 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04002942
Chris Masonca7a79a2008-05-12 12:59:19 -04002943 next = read_tree_block(root, bytenr, blocksize,
2944 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04002945 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04002946#if 0
2947 /*
2948 * this is a debugging check and can go away
2949 * the ref should never go all the way down to 1
2950 * at this point
2951 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002952 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2953 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002954 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002955 WARN_ON(refs != 1);
2956#endif
Chris Masone9d0b132007-08-10 14:06:19 -04002957 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002958 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002959 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002960 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002961 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002962 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002963 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04002964 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05002965 }
2966out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002967 WARN_ON(*level < 0);
2968 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002969
2970 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05002971 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04002972 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05002973 } else {
2974 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04002975 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05002976 }
2977
Yan Zheng31153d82008-07-28 15:32:19 -04002978 blocksize = btrfs_level_size(root, *level);
2979 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05002980 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04002981
2982 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002983 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002984 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002985 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05002986 path->nodes[*level] = NULL;
2987 *level += 1;
2988 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002989
Chris Masone7a84562008-06-25 16:01:31 -04002990 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05002991 return 0;
2992}
2993
Chris Mason9aca1d52007-03-13 11:09:37 -04002994/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04002995 * helper function for drop_subtree, this function is similar to
2996 * walk_down_tree. The main difference is that it checks reference
2997 * counts while tree blocks are locked.
2998 */
2999static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
3000 struct btrfs_root *root,
3001 struct btrfs_path *path, int *level)
3002{
3003 struct extent_buffer *next;
3004 struct extent_buffer *cur;
3005 struct extent_buffer *parent;
3006 u64 bytenr;
3007 u64 ptr_gen;
3008 u32 blocksize;
3009 u32 refs;
3010 int ret;
3011
3012 cur = path->nodes[*level];
3013 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3014 &refs);
3015 BUG_ON(ret);
3016 if (refs > 1)
3017 goto out;
3018
3019 while (*level >= 0) {
3020 cur = path->nodes[*level];
3021 if (*level == 0) {
3022 ret = btrfs_drop_leaf_ref(trans, root, cur);
3023 BUG_ON(ret);
3024 clean_tree_block(trans, root, cur);
3025 break;
3026 }
3027 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3028 clean_tree_block(trans, root, cur);
3029 break;
3030 }
3031
3032 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3033 blocksize = btrfs_level_size(root, *level - 1);
3034 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3035
3036 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3037 btrfs_tree_lock(next);
3038
3039 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3040 &refs);
3041 BUG_ON(ret);
3042 if (refs > 1) {
3043 parent = path->nodes[*level];
3044 ret = btrfs_free_extent(trans, root, bytenr,
3045 blocksize, parent->start,
3046 btrfs_header_owner(parent),
3047 btrfs_header_generation(parent),
3048 *level - 1, 1);
3049 BUG_ON(ret);
3050 path->slots[*level]++;
3051 btrfs_tree_unlock(next);
3052 free_extent_buffer(next);
3053 continue;
3054 }
3055
3056 *level = btrfs_header_level(next);
3057 path->nodes[*level] = next;
3058 path->slots[*level] = 0;
3059 path->locks[*level] = 1;
3060 cond_resched();
3061 }
3062out:
3063 parent = path->nodes[*level + 1];
3064 bytenr = path->nodes[*level]->start;
3065 blocksize = path->nodes[*level]->len;
3066
3067 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3068 parent->start, btrfs_header_owner(parent),
3069 btrfs_header_generation(parent), *level, 1);
3070 BUG_ON(ret);
3071
3072 if (path->locks[*level]) {
3073 btrfs_tree_unlock(path->nodes[*level]);
3074 path->locks[*level] = 0;
3075 }
3076 free_extent_buffer(path->nodes[*level]);
3077 path->nodes[*level] = NULL;
3078 *level += 1;
3079 cond_resched();
3080 return 0;
3081}
3082
3083/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003084 * helper for dropping snapshots. This walks back up the tree in the path
3085 * to find the first node higher up where we haven't yet gone through
3086 * all the slots
3087 */
Chris Mason98ed5172008-01-03 10:01:48 -05003088static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3089 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003090 struct btrfs_path *path,
3091 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003092{
Chris Mason7bb86312007-12-11 09:25:06 -05003093 u64 root_owner;
3094 u64 root_gen;
3095 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003096 int i;
3097 int slot;
3098 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003099
Yan Zhengf82d02d2008-10-29 14:49:05 -04003100 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003101 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003102 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3103 struct extent_buffer *node;
3104 struct btrfs_disk_key disk_key;
3105 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003106 path->slots[i]++;
3107 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003108 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003109 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003110 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003111 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003112 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003113 return 0;
3114 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003115 struct extent_buffer *parent;
3116 if (path->nodes[*level] == root->node)
3117 parent = path->nodes[*level];
3118 else
3119 parent = path->nodes[*level + 1];
3120
3121 root_owner = btrfs_header_owner(parent);
3122 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003123
3124 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003125 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003126 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003127 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003128 parent->start, root_owner,
3129 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003130 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003131 if (path->locks[*level]) {
3132 btrfs_tree_unlock(path->nodes[*level]);
3133 path->locks[*level] = 0;
3134 }
Chris Mason5f39d392007-10-15 16:14:19 -04003135 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003136 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003137 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003138 }
3139 }
3140 return 1;
3141}
3142
Chris Mason9aca1d52007-03-13 11:09:37 -04003143/*
3144 * drop the reference count on the tree rooted at 'snap'. This traverses
3145 * the tree freeing any blocks that have a ref count of zero after being
3146 * decremented.
3147 */
Chris Masone089f052007-03-16 16:20:31 -04003148int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003149 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003150{
Chris Mason3768f362007-03-13 16:47:54 -04003151 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003152 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003153 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003154 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003155 int i;
3156 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003157 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003158
Chris Masona2135012008-06-25 16:01:30 -04003159 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003160 path = btrfs_alloc_path();
3161 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003162
Chris Mason5f39d392007-10-15 16:14:19 -04003163 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003164 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003165 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3166 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003167 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003168 path->slots[level] = 0;
3169 } else {
3170 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003171 struct btrfs_disk_key found_key;
3172 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003173
Chris Mason9f3a7422007-08-07 15:52:19 -04003174 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003175 level = root_item->drop_level;
3176 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003177 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003178 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003179 ret = wret;
3180 goto out;
3181 }
Chris Mason5f39d392007-10-15 16:14:19 -04003182 node = path->nodes[level];
3183 btrfs_node_key(node, &found_key, path->slots[level]);
3184 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3185 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003186 /*
3187 * unlock our path, this is safe because only this
3188 * function is allowed to delete this snapshot
3189 */
Chris Mason925baed2008-06-25 16:01:30 -04003190 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3191 if (path->nodes[i] && path->locks[i]) {
3192 path->locks[i] = 0;
3193 btrfs_tree_unlock(path->nodes[i]);
3194 }
3195 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003196 }
Chris Mason20524f02007-03-10 06:35:47 -05003197 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003198 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003199 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003200 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003201 if (wret < 0)
3202 ret = wret;
3203
Yan Zhengf82d02d2008-10-29 14:49:05 -04003204 wret = walk_up_tree(trans, root, path, &level,
3205 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003206 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003207 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003208 if (wret < 0)
3209 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003210 if (trans->transaction->in_commit) {
3211 ret = -EAGAIN;
3212 break;
3213 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003214 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003215 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003216 }
Chris Mason83e15a22007-03-12 09:03:27 -04003217 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003218 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003219 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003220 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003221 }
Chris Mason20524f02007-03-10 06:35:47 -05003222 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003223out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003224 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003225 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003226}
Chris Mason9078a3e2007-04-26 16:46:15 -04003227
Yan Zhengf82d02d2008-10-29 14:49:05 -04003228int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3229 struct btrfs_root *root,
3230 struct extent_buffer *node,
3231 struct extent_buffer *parent)
3232{
3233 struct btrfs_path *path;
3234 int level;
3235 int parent_level;
3236 int ret = 0;
3237 int wret;
3238
3239 path = btrfs_alloc_path();
3240 BUG_ON(!path);
3241
3242 BUG_ON(!btrfs_tree_locked(parent));
3243 parent_level = btrfs_header_level(parent);
3244 extent_buffer_get(parent);
3245 path->nodes[parent_level] = parent;
3246 path->slots[parent_level] = btrfs_header_nritems(parent);
3247
3248 BUG_ON(!btrfs_tree_locked(node));
3249 level = btrfs_header_level(node);
3250 extent_buffer_get(node);
3251 path->nodes[level] = node;
3252 path->slots[level] = 0;
3253
3254 while (1) {
3255 wret = walk_down_subtree(trans, root, path, &level);
3256 if (wret < 0)
3257 ret = wret;
3258 if (wret != 0)
3259 break;
3260
3261 wret = walk_up_tree(trans, root, path, &level, parent_level);
3262 if (wret < 0)
3263 ret = wret;
3264 if (wret != 0)
3265 break;
3266 }
3267
3268 btrfs_free_path(path);
3269 return ret;
3270}
3271
Chris Mason8e7bf942008-04-28 09:02:36 -04003272static unsigned long calc_ra(unsigned long start, unsigned long last,
3273 unsigned long nr)
3274{
3275 return min(last, start + nr - 1);
3276}
3277
Chris Mason98ed5172008-01-03 10:01:48 -05003278static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3279 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003280{
3281 u64 page_start;
3282 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003283 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003284 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003285 unsigned long i;
3286 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003287 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003288 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003289 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003290 unsigned int total_read = 0;
3291 unsigned int total_dirty = 0;
3292 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003293
3294 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003295
3296 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003297 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003298 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3299
Zheng Yan1a40e232008-09-26 10:09:34 -04003300 /* make sure the dirty trick played by the caller work */
3301 ret = invalidate_inode_pages2_range(inode->i_mapping,
3302 first_index, last_index);
3303 if (ret)
3304 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003305
Chris Mason4313b392008-01-03 09:08:48 -05003306 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003307
Zheng Yan1a40e232008-09-26 10:09:34 -04003308 for (i = first_index ; i <= last_index; i++) {
3309 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003310 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003311 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003312 }
3313 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003314again:
3315 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003316 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003317 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003318 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003319 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003320 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003321 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003322 if (!PageUptodate(page)) {
3323 btrfs_readpage(NULL, page);
3324 lock_page(page);
3325 if (!PageUptodate(page)) {
3326 unlock_page(page);
3327 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003328 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003329 goto out_unlock;
3330 }
3331 }
Chris Masonec44a352008-04-28 15:29:52 -04003332 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003333
Chris Masonedbd8d42007-12-21 16:27:24 -05003334 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3335 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003336 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003337
Chris Mason3eaa2882008-07-24 11:57:52 -04003338 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3339 if (ordered) {
3340 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3341 unlock_page(page);
3342 page_cache_release(page);
3343 btrfs_start_ordered_extent(inode, ordered, 1);
3344 btrfs_put_ordered_extent(ordered);
3345 goto again;
3346 }
3347 set_page_extent_mapped(page);
3348
Chris Masonea8c2812008-08-04 23:17:27 -04003349 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003350 if (i == first_index)
3351 set_extent_bits(io_tree, page_start, page_end,
3352 EXTENT_BOUNDARY, GFP_NOFS);
3353
Chris Masona061fc82008-05-07 11:43:44 -04003354 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003355 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003356
Chris Masond1310b22008-01-24 16:13:08 -05003357 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003358 unlock_page(page);
3359 page_cache_release(page);
3360 }
3361
3362out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04003363 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05003364 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003365 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04003366 return ret;
3367}
3368
Zheng Yan1a40e232008-09-26 10:09:34 -04003369static int noinline relocate_data_extent(struct inode *reloc_inode,
3370 struct btrfs_key *extent_key,
3371 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05003372{
Zheng Yan1a40e232008-09-26 10:09:34 -04003373 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3374 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
3375 struct extent_map *em;
3376
3377 em = alloc_extent_map(GFP_NOFS);
3378 BUG_ON(!em || IS_ERR(em));
3379
3380 em->start = extent_key->objectid - offset;
3381 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003382 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04003383 em->block_start = extent_key->objectid;
3384 em->bdev = root->fs_info->fs_devices->latest_bdev;
3385 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3386
3387 /* setup extent map to cheat btrfs_readpage */
3388 mutex_lock(&BTRFS_I(reloc_inode)->extent_mutex);
3389 while (1) {
3390 int ret;
3391 spin_lock(&em_tree->lock);
3392 ret = add_extent_mapping(em_tree, em);
3393 spin_unlock(&em_tree->lock);
3394 if (ret != -EEXIST) {
3395 free_extent_map(em);
3396 break;
3397 }
3398 btrfs_drop_extent_cache(reloc_inode, em->start,
3399 em->start + em->len - 1, 0);
3400 }
3401 mutex_unlock(&BTRFS_I(reloc_inode)->extent_mutex);
3402
3403 return relocate_inode_pages(reloc_inode, extent_key->objectid - offset,
3404 extent_key->offset);
3405}
3406
3407struct btrfs_ref_path {
3408 u64 extent_start;
3409 u64 nodes[BTRFS_MAX_LEVEL];
3410 u64 root_objectid;
3411 u64 root_generation;
3412 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04003413 u32 num_refs;
3414 int lowest_level;
3415 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04003416 int shared_level;
3417
3418 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
3419 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04003420};
3421
3422struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04003423 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04003424 u64 disk_bytenr;
3425 u64 disk_num_bytes;
3426 u64 offset;
3427 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04003428 u8 compression;
3429 u8 encryption;
3430 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04003431};
3432
3433static int is_cowonly_root(u64 root_objectid)
3434{
3435 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
3436 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
3437 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
3438 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
3439 root_objectid == BTRFS_TREE_LOG_OBJECTID)
3440 return 1;
3441 return 0;
3442}
3443
3444static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
3445 struct btrfs_root *extent_root,
3446 struct btrfs_ref_path *ref_path,
3447 int first_time)
3448{
3449 struct extent_buffer *leaf;
3450 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05003451 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05003452 struct btrfs_key key;
3453 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04003454 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05003455 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04003456 int level;
3457 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05003458
Zheng Yan1a40e232008-09-26 10:09:34 -04003459 path = btrfs_alloc_path();
3460 if (!path)
3461 return -ENOMEM;
3462
Zheng Yan1a40e232008-09-26 10:09:34 -04003463 if (first_time) {
3464 ref_path->lowest_level = -1;
3465 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04003466 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04003467 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04003468 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003469walk_down:
3470 level = ref_path->current_level - 1;
3471 while (level >= -1) {
3472 u64 parent;
3473 if (level < ref_path->lowest_level)
3474 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05003475
Zheng Yan1a40e232008-09-26 10:09:34 -04003476 if (level >= 0) {
3477 bytenr = ref_path->nodes[level];
3478 } else {
3479 bytenr = ref_path->extent_start;
3480 }
3481 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003482
Zheng Yan1a40e232008-09-26 10:09:34 -04003483 parent = ref_path->nodes[level + 1];
3484 ref_path->nodes[level + 1] = 0;
3485 ref_path->current_level = level;
3486 BUG_ON(parent == 0);
3487
3488 key.objectid = bytenr;
3489 key.offset = parent + 1;
3490 key.type = BTRFS_EXTENT_REF_KEY;
3491
3492 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003493 if (ret < 0)
3494 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003495 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003496
Chris Masonedbd8d42007-12-21 16:27:24 -05003497 leaf = path->nodes[0];
3498 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04003499 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04003500 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04003501 if (ret < 0)
3502 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003503 if (ret > 0)
3504 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04003505 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04003506 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003507
3508 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04003509 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04003510 found_key.type == BTRFS_EXTENT_REF_KEY) {
3511 if (level < ref_path->shared_level)
3512 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04003513 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04003514 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003515next:
3516 level--;
3517 btrfs_release_path(extent_root, path);
3518 if (need_resched()) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003519 cond_resched();
Chris Masona061fc82008-05-07 11:43:44 -04003520 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003521 }
3522 /* reached lowest level */
3523 ret = 1;
3524 goto out;
3525walk_up:
3526 level = ref_path->current_level;
3527 while (level < BTRFS_MAX_LEVEL - 1) {
3528 u64 ref_objectid;
3529 if (level >= 0) {
3530 bytenr = ref_path->nodes[level];
3531 } else {
3532 bytenr = ref_path->extent_start;
Chris Masona061fc82008-05-07 11:43:44 -04003533 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003534 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003535
Zheng Yan1a40e232008-09-26 10:09:34 -04003536 key.objectid = bytenr;
3537 key.offset = 0;
3538 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05003539
Zheng Yan1a40e232008-09-26 10:09:34 -04003540 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
3541 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05003542 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003543
3544 leaf = path->nodes[0];
3545 nritems = btrfs_header_nritems(leaf);
3546 if (path->slots[0] >= nritems) {
3547 ret = btrfs_next_leaf(extent_root, path);
3548 if (ret < 0)
3549 goto out;
3550 if (ret > 0) {
3551 /* the extent was freed by someone */
3552 if (ref_path->lowest_level == level)
3553 goto out;
3554 btrfs_release_path(extent_root, path);
3555 goto walk_down;
3556 }
3557 leaf = path->nodes[0];
3558 }
3559
3560 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3561 if (found_key.objectid != bytenr ||
3562 found_key.type != BTRFS_EXTENT_REF_KEY) {
3563 /* the extent was freed by someone */
3564 if (ref_path->lowest_level == level) {
3565 ret = 1;
3566 goto out;
3567 }
3568 btrfs_release_path(extent_root, path);
3569 goto walk_down;
3570 }
3571found:
3572 ref = btrfs_item_ptr(leaf, path->slots[0],
3573 struct btrfs_extent_ref);
3574 ref_objectid = btrfs_ref_objectid(leaf, ref);
3575 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3576 if (first_time) {
3577 level = (int)ref_objectid;
3578 BUG_ON(level >= BTRFS_MAX_LEVEL);
3579 ref_path->lowest_level = level;
3580 ref_path->current_level = level;
3581 ref_path->nodes[level] = bytenr;
3582 } else {
3583 WARN_ON(ref_objectid != level);
3584 }
3585 } else {
3586 WARN_ON(level != -1);
3587 }
3588 first_time = 0;
3589
3590 if (ref_path->lowest_level == level) {
3591 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04003592 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
3593 }
3594
3595 /*
3596 * the block is tree root or the block isn't in reference
3597 * counted tree.
3598 */
3599 if (found_key.objectid == found_key.offset ||
3600 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
3601 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
3602 ref_path->root_generation =
3603 btrfs_ref_generation(leaf, ref);
3604 if (level < 0) {
3605 /* special reference from the tree log */
3606 ref_path->nodes[0] = found_key.offset;
3607 ref_path->current_level = 0;
3608 }
3609 ret = 0;
3610 goto out;
3611 }
3612
3613 level++;
3614 BUG_ON(ref_path->nodes[level] != 0);
3615 ref_path->nodes[level] = found_key.offset;
3616 ref_path->current_level = level;
3617
3618 /*
3619 * the reference was created in the running transaction,
3620 * no need to continue walking up.
3621 */
3622 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
3623 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
3624 ref_path->root_generation =
3625 btrfs_ref_generation(leaf, ref);
3626 ret = 0;
3627 goto out;
3628 }
3629
3630 btrfs_release_path(extent_root, path);
3631 if (need_resched()) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003632 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04003633 }
3634 }
3635 /* reached max tree level, but no tree root found. */
3636 BUG();
3637out:
Zheng Yan1a40e232008-09-26 10:09:34 -04003638 btrfs_free_path(path);
3639 return ret;
3640}
3641
3642static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
3643 struct btrfs_root *extent_root,
3644 struct btrfs_ref_path *ref_path,
3645 u64 extent_start)
3646{
3647 memset(ref_path, 0, sizeof(*ref_path));
3648 ref_path->extent_start = extent_start;
3649
3650 return __next_ref_path(trans, extent_root, ref_path, 1);
3651}
3652
3653static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
3654 struct btrfs_root *extent_root,
3655 struct btrfs_ref_path *ref_path)
3656{
3657 return __next_ref_path(trans, extent_root, ref_path, 0);
3658}
3659
3660static int noinline get_new_locations(struct inode *reloc_inode,
3661 struct btrfs_key *extent_key,
3662 u64 offset, int no_fragment,
3663 struct disk_extent **extents,
3664 int *nr_extents)
3665{
3666 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3667 struct btrfs_path *path;
3668 struct btrfs_file_extent_item *fi;
3669 struct extent_buffer *leaf;
3670 struct disk_extent *exts = *extents;
3671 struct btrfs_key found_key;
3672 u64 cur_pos;
3673 u64 last_byte;
3674 u32 nritems;
3675 int nr = 0;
3676 int max = *nr_extents;
3677 int ret;
3678
3679 WARN_ON(!no_fragment && *extents);
3680 if (!exts) {
3681 max = 1;
3682 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
3683 if (!exts)
3684 return -ENOMEM;
3685 }
3686
3687 path = btrfs_alloc_path();
3688 BUG_ON(!path);
3689
3690 cur_pos = extent_key->objectid - offset;
3691 last_byte = extent_key->objectid + extent_key->offset;
3692 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
3693 cur_pos, 0);
3694 if (ret < 0)
3695 goto out;
3696 if (ret > 0) {
3697 ret = -ENOENT;
3698 goto out;
3699 }
3700
3701 while (1) {
3702 leaf = path->nodes[0];
3703 nritems = btrfs_header_nritems(leaf);
3704 if (path->slots[0] >= nritems) {
3705 ret = btrfs_next_leaf(root, path);
3706 if (ret < 0)
3707 goto out;
3708 if (ret > 0)
3709 break;
3710 leaf = path->nodes[0];
3711 }
3712
3713 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3714 if (found_key.offset != cur_pos ||
3715 found_key.type != BTRFS_EXTENT_DATA_KEY ||
3716 found_key.objectid != reloc_inode->i_ino)
3717 break;
3718
3719 fi = btrfs_item_ptr(leaf, path->slots[0],
3720 struct btrfs_file_extent_item);
3721 if (btrfs_file_extent_type(leaf, fi) !=
3722 BTRFS_FILE_EXTENT_REG ||
3723 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
3724 break;
3725
3726 if (nr == max) {
3727 struct disk_extent *old = exts;
3728 max *= 2;
3729 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
3730 memcpy(exts, old, sizeof(*exts) * nr);
3731 if (old != *extents)
3732 kfree(old);
3733 }
3734
3735 exts[nr].disk_bytenr =
3736 btrfs_file_extent_disk_bytenr(leaf, fi);
3737 exts[nr].disk_num_bytes =
3738 btrfs_file_extent_disk_num_bytes(leaf, fi);
3739 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
3740 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04003741 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
3742 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
3743 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
3744 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
3745 fi);
Zheng Yan1a40e232008-09-26 10:09:34 -04003746 WARN_ON(exts[nr].offset > 0);
3747 WARN_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
3748
3749 cur_pos += exts[nr].num_bytes;
3750 nr++;
3751
3752 if (cur_pos + offset >= last_byte)
3753 break;
3754
3755 if (no_fragment) {
3756 ret = 1;
3757 goto out;
3758 }
3759 path->slots[0]++;
3760 }
3761
3762 WARN_ON(cur_pos + offset > last_byte);
3763 if (cur_pos + offset < last_byte) {
3764 ret = -ENOENT;
3765 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05003766 }
3767 ret = 0;
3768out:
Zheng Yan1a40e232008-09-26 10:09:34 -04003769 btrfs_free_path(path);
3770 if (ret) {
3771 if (exts != *extents)
3772 kfree(exts);
3773 } else {
3774 *extents = exts;
3775 *nr_extents = nr;
3776 }
3777 return ret;
3778}
3779
3780static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
3781 struct btrfs_root *root,
3782 struct btrfs_path *path,
3783 struct btrfs_key *extent_key,
3784 struct btrfs_key *leaf_key,
3785 struct btrfs_ref_path *ref_path,
3786 struct disk_extent *new_extents,
3787 int nr_extents)
3788{
3789 struct extent_buffer *leaf;
3790 struct btrfs_file_extent_item *fi;
3791 struct inode *inode = NULL;
3792 struct btrfs_key key;
3793 u64 lock_start = 0;
3794 u64 lock_end = 0;
3795 u64 num_bytes;
3796 u64 ext_offset;
3797 u64 first_pos;
3798 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003799 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04003800 int extent_locked = 0;
3801 int ret;
3802
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003803 memcpy(&key, leaf_key, sizeof(key));
3804 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04003805 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003806 if (key.objectid < ref_path->owner_objectid ||
3807 (key.objectid == ref_path->owner_objectid &&
3808 key.type < BTRFS_EXTENT_DATA_KEY)) {
3809 key.objectid = ref_path->owner_objectid;
3810 key.type = BTRFS_EXTENT_DATA_KEY;
3811 key.offset = 0;
3812 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003813 }
3814
3815 while (1) {
3816 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3817 if (ret < 0)
3818 goto out;
3819
3820 leaf = path->nodes[0];
3821 nritems = btrfs_header_nritems(leaf);
3822next:
3823 if (extent_locked && ret > 0) {
3824 /*
3825 * the file extent item was modified by someone
3826 * before the extent got locked.
3827 */
3828 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
3829 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3830 lock_end, GFP_NOFS);
3831 extent_locked = 0;
3832 }
3833
3834 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003835 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04003836 break;
3837
3838 BUG_ON(extent_locked);
3839 ret = btrfs_next_leaf(root, path);
3840 if (ret < 0)
3841 goto out;
3842 if (ret > 0)
3843 break;
3844 leaf = path->nodes[0];
3845 nritems = btrfs_header_nritems(leaf);
3846 }
3847
3848 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3849
3850 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
3851 if ((key.objectid > ref_path->owner_objectid) ||
3852 (key.objectid == ref_path->owner_objectid &&
3853 key.type > BTRFS_EXTENT_DATA_KEY) ||
3854 (key.offset >= first_pos + extent_key->offset))
3855 break;
3856 }
3857
3858 if (inode && key.objectid != inode->i_ino) {
3859 BUG_ON(extent_locked);
3860 btrfs_release_path(root, path);
3861 mutex_unlock(&inode->i_mutex);
3862 iput(inode);
3863 inode = NULL;
3864 continue;
3865 }
3866
3867 if (key.type != BTRFS_EXTENT_DATA_KEY) {
3868 path->slots[0]++;
3869 ret = 1;
3870 goto next;
3871 }
3872 fi = btrfs_item_ptr(leaf, path->slots[0],
3873 struct btrfs_file_extent_item);
3874 if ((btrfs_file_extent_type(leaf, fi) !=
3875 BTRFS_FILE_EXTENT_REG) ||
3876 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
3877 extent_key->objectid)) {
3878 path->slots[0]++;
3879 ret = 1;
3880 goto next;
3881 }
3882
3883 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
3884 ext_offset = btrfs_file_extent_offset(leaf, fi);
3885
3886 if (first_pos > key.offset - ext_offset)
3887 first_pos = key.offset - ext_offset;
3888
3889 if (!extent_locked) {
3890 lock_start = key.offset;
3891 lock_end = lock_start + num_bytes - 1;
3892 } else {
3893 BUG_ON(lock_start != key.offset);
3894 BUG_ON(lock_end - lock_start + 1 < num_bytes);
3895 }
3896
3897 if (!inode) {
3898 btrfs_release_path(root, path);
3899
3900 inode = btrfs_iget_locked(root->fs_info->sb,
3901 key.objectid, root);
3902 if (inode->i_state & I_NEW) {
3903 BTRFS_I(inode)->root = root;
3904 BTRFS_I(inode)->location.objectid =
3905 key.objectid;
3906 BTRFS_I(inode)->location.type =
3907 BTRFS_INODE_ITEM_KEY;
3908 BTRFS_I(inode)->location.offset = 0;
3909 btrfs_read_locked_inode(inode);
3910 unlock_new_inode(inode);
3911 }
3912 /*
3913 * some code call btrfs_commit_transaction while
3914 * holding the i_mutex, so we can't use mutex_lock
3915 * here.
3916 */
3917 if (is_bad_inode(inode) ||
3918 !mutex_trylock(&inode->i_mutex)) {
3919 iput(inode);
3920 inode = NULL;
3921 key.offset = (u64)-1;
3922 goto skip;
3923 }
3924 }
3925
3926 if (!extent_locked) {
3927 struct btrfs_ordered_extent *ordered;
3928
3929 btrfs_release_path(root, path);
3930
3931 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3932 lock_end, GFP_NOFS);
3933 ordered = btrfs_lookup_first_ordered_extent(inode,
3934 lock_end);
3935 if (ordered &&
3936 ordered->file_offset <= lock_end &&
3937 ordered->file_offset + ordered->len > lock_start) {
3938 unlock_extent(&BTRFS_I(inode)->io_tree,
3939 lock_start, lock_end, GFP_NOFS);
3940 btrfs_start_ordered_extent(inode, ordered, 1);
3941 btrfs_put_ordered_extent(ordered);
3942 key.offset += num_bytes;
3943 goto skip;
3944 }
3945 if (ordered)
3946 btrfs_put_ordered_extent(ordered);
3947
3948 mutex_lock(&BTRFS_I(inode)->extent_mutex);
3949 extent_locked = 1;
3950 continue;
3951 }
3952
3953 if (nr_extents == 1) {
3954 /* update extent pointer in place */
3955 btrfs_set_file_extent_generation(leaf, fi,
3956 trans->transid);
3957 btrfs_set_file_extent_disk_bytenr(leaf, fi,
3958 new_extents[0].disk_bytenr);
3959 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
3960 new_extents[0].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04003961 btrfs_set_file_extent_ram_bytes(leaf, fi,
3962 new_extents[0].ram_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04003963 ext_offset += new_extents[0].offset;
3964 btrfs_set_file_extent_offset(leaf, fi, ext_offset);
3965 btrfs_mark_buffer_dirty(leaf);
3966
3967 btrfs_drop_extent_cache(inode, key.offset,
3968 key.offset + num_bytes - 1, 0);
3969
3970 ret = btrfs_inc_extent_ref(trans, root,
3971 new_extents[0].disk_bytenr,
3972 new_extents[0].disk_num_bytes,
3973 leaf->start,
3974 root->root_key.objectid,
3975 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003976 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04003977 BUG_ON(ret);
3978
3979 ret = btrfs_free_extent(trans, root,
3980 extent_key->objectid,
3981 extent_key->offset,
3982 leaf->start,
3983 btrfs_header_owner(leaf),
3984 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003985 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04003986 BUG_ON(ret);
3987
3988 btrfs_release_path(root, path);
3989 key.offset += num_bytes;
3990 } else {
3991 u64 alloc_hint;
3992 u64 extent_len;
3993 int i;
3994 /*
3995 * drop old extent pointer at first, then insert the
3996 * new pointers one bye one
3997 */
3998 btrfs_release_path(root, path);
3999 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4000 key.offset + num_bytes,
4001 key.offset, &alloc_hint);
4002 BUG_ON(ret);
4003
4004 for (i = 0; i < nr_extents; i++) {
4005 if (ext_offset >= new_extents[i].num_bytes) {
4006 ext_offset -= new_extents[i].num_bytes;
4007 continue;
4008 }
4009 extent_len = min(new_extents[i].num_bytes -
4010 ext_offset, num_bytes);
4011
4012 ret = btrfs_insert_empty_item(trans, root,
4013 path, &key,
4014 sizeof(*fi));
4015 BUG_ON(ret);
4016
4017 leaf = path->nodes[0];
4018 fi = btrfs_item_ptr(leaf, path->slots[0],
4019 struct btrfs_file_extent_item);
4020 btrfs_set_file_extent_generation(leaf, fi,
4021 trans->transid);
4022 btrfs_set_file_extent_type(leaf, fi,
4023 BTRFS_FILE_EXTENT_REG);
4024 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4025 new_extents[i].disk_bytenr);
4026 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4027 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004028 btrfs_set_file_extent_ram_bytes(leaf, fi,
4029 new_extents[i].ram_bytes);
4030
4031 btrfs_set_file_extent_compression(leaf, fi,
4032 new_extents[i].compression);
4033 btrfs_set_file_extent_encryption(leaf, fi,
4034 new_extents[i].encryption);
4035 btrfs_set_file_extent_other_encoding(leaf, fi,
4036 new_extents[i].other_encoding);
4037
Zheng Yan1a40e232008-09-26 10:09:34 -04004038 btrfs_set_file_extent_num_bytes(leaf, fi,
4039 extent_len);
4040 ext_offset += new_extents[i].offset;
4041 btrfs_set_file_extent_offset(leaf, fi,
4042 ext_offset);
4043 btrfs_mark_buffer_dirty(leaf);
4044
4045 btrfs_drop_extent_cache(inode, key.offset,
4046 key.offset + extent_len - 1, 0);
4047
4048 ret = btrfs_inc_extent_ref(trans, root,
4049 new_extents[i].disk_bytenr,
4050 new_extents[i].disk_num_bytes,
4051 leaf->start,
4052 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004053 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004054 BUG_ON(ret);
4055 btrfs_release_path(root, path);
4056
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004057 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004058
4059 ext_offset = 0;
4060 num_bytes -= extent_len;
4061 key.offset += extent_len;
4062
4063 if (num_bytes == 0)
4064 break;
4065 }
4066 BUG_ON(i >= nr_extents);
4067 }
4068
4069 if (extent_locked) {
4070 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
4071 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4072 lock_end, GFP_NOFS);
4073 extent_locked = 0;
4074 }
4075skip:
4076 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4077 key.offset >= first_pos + extent_key->offset)
4078 break;
4079
4080 cond_resched();
4081 }
4082 ret = 0;
4083out:
4084 btrfs_release_path(root, path);
4085 if (inode) {
4086 mutex_unlock(&inode->i_mutex);
4087 if (extent_locked) {
4088 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
4089 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4090 lock_end, GFP_NOFS);
4091 }
4092 iput(inode);
4093 }
4094 return ret;
4095}
4096
Zheng Yan1a40e232008-09-26 10:09:34 -04004097int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4098 struct btrfs_root *root,
4099 struct extent_buffer *buf, u64 orig_start)
4100{
4101 int level;
4102 int ret;
4103
4104 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4105 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4106
4107 level = btrfs_header_level(buf);
4108 if (level == 0) {
4109 struct btrfs_leaf_ref *ref;
4110 struct btrfs_leaf_ref *orig_ref;
4111
4112 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4113 if (!orig_ref)
4114 return -ENOENT;
4115
4116 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4117 if (!ref) {
4118 btrfs_free_leaf_ref(root, orig_ref);
4119 return -ENOMEM;
4120 }
4121
4122 ref->nritems = orig_ref->nritems;
4123 memcpy(ref->extents, orig_ref->extents,
4124 sizeof(ref->extents[0]) * ref->nritems);
4125
4126 btrfs_free_leaf_ref(root, orig_ref);
4127
4128 ref->root_gen = trans->transid;
4129 ref->bytenr = buf->start;
4130 ref->owner = btrfs_header_owner(buf);
4131 ref->generation = btrfs_header_generation(buf);
4132 ret = btrfs_add_leaf_ref(root, ref, 0);
4133 WARN_ON(ret);
4134 btrfs_free_leaf_ref(root, ref);
4135 }
4136 return 0;
4137}
4138
4139static int noinline invalidate_extent_cache(struct btrfs_root *root,
4140 struct extent_buffer *leaf,
4141 struct btrfs_block_group_cache *group,
4142 struct btrfs_root *target_root)
4143{
4144 struct btrfs_key key;
4145 struct inode *inode = NULL;
4146 struct btrfs_file_extent_item *fi;
4147 u64 num_bytes;
4148 u64 skip_objectid = 0;
4149 u32 nritems;
4150 u32 i;
4151
4152 nritems = btrfs_header_nritems(leaf);
4153 for (i = 0; i < nritems; i++) {
4154 btrfs_item_key_to_cpu(leaf, &key, i);
4155 if (key.objectid == skip_objectid ||
4156 key.type != BTRFS_EXTENT_DATA_KEY)
4157 continue;
4158 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4159 if (btrfs_file_extent_type(leaf, fi) ==
4160 BTRFS_FILE_EXTENT_INLINE)
4161 continue;
4162 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4163 continue;
4164 if (!inode || inode->i_ino != key.objectid) {
4165 iput(inode);
4166 inode = btrfs_ilookup(target_root->fs_info->sb,
4167 key.objectid, target_root, 1);
4168 }
4169 if (!inode) {
4170 skip_objectid = key.objectid;
4171 continue;
4172 }
4173 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4174
4175 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4176 key.offset + num_bytes - 1, GFP_NOFS);
4177 mutex_lock(&BTRFS_I(inode)->extent_mutex);
4178 btrfs_drop_extent_cache(inode, key.offset,
4179 key.offset + num_bytes - 1, 1);
4180 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
4181 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4182 key.offset + num_bytes - 1, GFP_NOFS);
4183 cond_resched();
4184 }
4185 iput(inode);
4186 return 0;
4187}
4188
4189static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4190 struct btrfs_root *root,
4191 struct extent_buffer *leaf,
4192 struct btrfs_block_group_cache *group,
4193 struct inode *reloc_inode)
4194{
4195 struct btrfs_key key;
4196 struct btrfs_key extent_key;
4197 struct btrfs_file_extent_item *fi;
4198 struct btrfs_leaf_ref *ref;
4199 struct disk_extent *new_extent;
4200 u64 bytenr;
4201 u64 num_bytes;
4202 u32 nritems;
4203 u32 i;
4204 int ext_index;
4205 int nr_extent;
4206 int ret;
4207
4208 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4209 BUG_ON(!new_extent);
4210
4211 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4212 BUG_ON(!ref);
4213
4214 ext_index = -1;
4215 nritems = btrfs_header_nritems(leaf);
4216 for (i = 0; i < nritems; i++) {
4217 btrfs_item_key_to_cpu(leaf, &key, i);
4218 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4219 continue;
4220 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4221 if (btrfs_file_extent_type(leaf, fi) ==
4222 BTRFS_FILE_EXTENT_INLINE)
4223 continue;
4224 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4225 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4226 if (bytenr == 0)
4227 continue;
4228
4229 ext_index++;
4230 if (bytenr >= group->key.objectid + group->key.offset ||
4231 bytenr + num_bytes <= group->key.objectid)
4232 continue;
4233
4234 extent_key.objectid = bytenr;
4235 extent_key.offset = num_bytes;
4236 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4237 nr_extent = 1;
4238 ret = get_new_locations(reloc_inode, &extent_key,
4239 group->key.objectid, 1,
4240 &new_extent, &nr_extent);
4241 if (ret > 0)
4242 continue;
4243 BUG_ON(ret < 0);
4244
4245 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4246 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4247 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4248 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4249
4250 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Chris Masonc8b97812008-10-29 14:49:59 -04004251 btrfs_set_file_extent_ram_bytes(leaf, fi,
4252 new_extent->ram_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004253 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4254 new_extent->disk_bytenr);
4255 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4256 new_extent->disk_num_bytes);
4257 new_extent->offset += btrfs_file_extent_offset(leaf, fi);
4258 btrfs_set_file_extent_offset(leaf, fi, new_extent->offset);
4259 btrfs_mark_buffer_dirty(leaf);
4260
4261 ret = btrfs_inc_extent_ref(trans, root,
4262 new_extent->disk_bytenr,
4263 new_extent->disk_num_bytes,
4264 leaf->start,
4265 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004266 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004267 BUG_ON(ret);
4268 ret = btrfs_free_extent(trans, root,
4269 bytenr, num_bytes, leaf->start,
4270 btrfs_header_owner(leaf),
4271 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004272 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004273 BUG_ON(ret);
4274 cond_resched();
4275 }
4276 kfree(new_extent);
4277 BUG_ON(ext_index + 1 != ref->nritems);
4278 btrfs_free_leaf_ref(root, ref);
4279 return 0;
4280}
4281
Yan Zhengf82d02d2008-10-29 14:49:05 -04004282int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4283 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004284{
4285 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004286 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004287
4288 if (root->reloc_root) {
4289 reloc_root = root->reloc_root;
4290 root->reloc_root = NULL;
4291 list_add(&reloc_root->dead_list,
4292 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004293
4294 btrfs_set_root_bytenr(&reloc_root->root_item,
4295 reloc_root->node->start);
4296 btrfs_set_root_level(&root->root_item,
4297 btrfs_header_level(reloc_root->node));
4298 memset(&reloc_root->root_item.drop_progress, 0,
4299 sizeof(struct btrfs_disk_key));
4300 reloc_root->root_item.drop_level = 0;
4301
4302 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4303 &reloc_root->root_key,
4304 &reloc_root->root_item);
4305 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004306 }
4307 return 0;
4308}
4309
4310int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4311{
4312 struct btrfs_trans_handle *trans;
4313 struct btrfs_root *reloc_root;
4314 struct btrfs_root *prev_root = NULL;
4315 struct list_head dead_roots;
4316 int ret;
4317 unsigned long nr;
4318
4319 INIT_LIST_HEAD(&dead_roots);
4320 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4321
4322 while (!list_empty(&dead_roots)) {
4323 reloc_root = list_entry(dead_roots.prev,
4324 struct btrfs_root, dead_list);
4325 list_del_init(&reloc_root->dead_list);
4326
4327 BUG_ON(reloc_root->commit_root != NULL);
4328 while (1) {
4329 trans = btrfs_join_transaction(root, 1);
4330 BUG_ON(!trans);
4331
4332 mutex_lock(&root->fs_info->drop_mutex);
4333 ret = btrfs_drop_snapshot(trans, reloc_root);
4334 if (ret != -EAGAIN)
4335 break;
4336 mutex_unlock(&root->fs_info->drop_mutex);
4337
4338 nr = trans->blocks_used;
4339 ret = btrfs_end_transaction(trans, root);
4340 BUG_ON(ret);
4341 btrfs_btree_balance_dirty(root, nr);
4342 }
4343
4344 free_extent_buffer(reloc_root->node);
4345
4346 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4347 &reloc_root->root_key);
4348 BUG_ON(ret);
4349 mutex_unlock(&root->fs_info->drop_mutex);
4350
4351 nr = trans->blocks_used;
4352 ret = btrfs_end_transaction(trans, root);
4353 BUG_ON(ret);
4354 btrfs_btree_balance_dirty(root, nr);
4355
4356 kfree(prev_root);
4357 prev_root = reloc_root;
4358 }
4359 if (prev_root) {
4360 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4361 kfree(prev_root);
4362 }
4363 return 0;
4364}
4365
4366int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4367{
4368 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4369 return 0;
4370}
4371
4372int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4373{
4374 struct btrfs_root *reloc_root;
4375 struct btrfs_trans_handle *trans;
4376 struct btrfs_key location;
4377 int found;
4378 int ret;
4379
4380 mutex_lock(&root->fs_info->tree_reloc_mutex);
4381 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4382 BUG_ON(ret);
4383 found = !list_empty(&root->fs_info->dead_reloc_roots);
4384 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4385
4386 if (found) {
4387 trans = btrfs_start_transaction(root, 1);
4388 BUG_ON(!trans);
4389 ret = btrfs_commit_transaction(trans, root);
4390 BUG_ON(ret);
4391 }
4392
4393 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4394 location.offset = (u64)-1;
4395 location.type = BTRFS_ROOT_ITEM_KEY;
4396
4397 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
4398 BUG_ON(!reloc_root);
4399 btrfs_orphan_cleanup(reloc_root);
4400 return 0;
4401}
4402
4403static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
4404 struct btrfs_root *root)
4405{
4406 struct btrfs_root *reloc_root;
4407 struct extent_buffer *eb;
4408 struct btrfs_root_item *root_item;
4409 struct btrfs_key root_key;
4410 int ret;
4411
4412 BUG_ON(!root->ref_cows);
4413 if (root->reloc_root)
4414 return 0;
4415
4416 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
4417 BUG_ON(!root_item);
4418
4419 ret = btrfs_copy_root(trans, root, root->commit_root,
4420 &eb, BTRFS_TREE_RELOC_OBJECTID);
4421 BUG_ON(ret);
4422
4423 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4424 root_key.offset = root->root_key.objectid;
4425 root_key.type = BTRFS_ROOT_ITEM_KEY;
4426
4427 memcpy(root_item, &root->root_item, sizeof(root_item));
4428 btrfs_set_root_refs(root_item, 0);
4429 btrfs_set_root_bytenr(root_item, eb->start);
4430 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Zheng Yan1a40e232008-09-26 10:09:34 -04004431
4432 btrfs_tree_unlock(eb);
4433 free_extent_buffer(eb);
4434
4435 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
4436 &root_key, root_item);
4437 BUG_ON(ret);
4438 kfree(root_item);
4439
4440 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
4441 &root_key);
4442 BUG_ON(!reloc_root);
4443 reloc_root->last_trans = trans->transid;
4444 reloc_root->commit_root = NULL;
4445 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
4446
4447 root->reloc_root = reloc_root;
4448 return 0;
4449}
4450
4451/*
4452 * Core function of space balance.
4453 *
4454 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04004455 * counted roots. There is one reloc tree for each subvol, and all
4456 * reloc trees share same root key objectid. Reloc trees are snapshots
4457 * of the latest committed roots of subvols (root->commit_root).
4458 *
4459 * To relocate a tree block referenced by a subvol, there are two steps.
4460 * COW the block through subvol's reloc tree, then update block pointer
4461 * in the subvol to point to the new block. Since all reloc trees share
4462 * same root key objectid, doing special handing for tree blocks owned
4463 * by them is easy. Once a tree block has been COWed in one reloc tree,
4464 * we can use the resulting new block directly when the same block is
4465 * required to COW again through other reloc trees. By this way, relocated
4466 * tree blocks are shared between reloc trees, so they are also shared
4467 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04004468 */
4469static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
4470 struct btrfs_root *root,
4471 struct btrfs_path *path,
4472 struct btrfs_key *first_key,
4473 struct btrfs_ref_path *ref_path,
4474 struct btrfs_block_group_cache *group,
4475 struct inode *reloc_inode)
4476{
4477 struct btrfs_root *reloc_root;
4478 struct extent_buffer *eb = NULL;
4479 struct btrfs_key *keys;
4480 u64 *nodes;
4481 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004482 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004483 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004484 int ret;
4485
4486 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
4487 lowest_level = ref_path->owner_objectid;
4488
Yan Zhengf82d02d2008-10-29 14:49:05 -04004489 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004490 path->lowest_level = lowest_level;
4491 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
4492 BUG_ON(ret < 0);
4493 path->lowest_level = 0;
4494 btrfs_release_path(root, path);
4495 return 0;
4496 }
4497
Zheng Yan1a40e232008-09-26 10:09:34 -04004498 mutex_lock(&root->fs_info->tree_reloc_mutex);
4499 ret = init_reloc_tree(trans, root);
4500 BUG_ON(ret);
4501 reloc_root = root->reloc_root;
4502
Yan Zhengf82d02d2008-10-29 14:49:05 -04004503 shared_level = ref_path->shared_level;
4504 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004505
Yan Zhengf82d02d2008-10-29 14:49:05 -04004506 keys = ref_path->node_keys;
4507 nodes = ref_path->new_nodes;
4508 memset(&keys[shared_level + 1], 0,
4509 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
4510 memset(&nodes[shared_level + 1], 0,
4511 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04004512
Yan Zhengf82d02d2008-10-29 14:49:05 -04004513 if (nodes[lowest_level] == 0) {
4514 path->lowest_level = lowest_level;
4515 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
4516 0, 1);
4517 BUG_ON(ret);
4518 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
4519 eb = path->nodes[level];
4520 if (!eb || eb == reloc_root->node)
4521 break;
4522 nodes[level] = eb->start;
4523 if (level == 0)
4524 btrfs_item_key_to_cpu(eb, &keys[level], 0);
4525 else
4526 btrfs_node_key_to_cpu(eb, &keys[level], 0);
4527 }
4528 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4529 eb = path->nodes[0];
4530 ret = replace_extents_in_leaf(trans, reloc_root, eb,
4531 group, reloc_inode);
4532 BUG_ON(ret);
4533 }
4534 btrfs_release_path(reloc_root, path);
4535 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04004536 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04004537 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04004538 BUG_ON(ret);
4539 }
4540
Zheng Yan1a40e232008-09-26 10:09:34 -04004541 /*
4542 * replace tree blocks in the fs tree with tree blocks in
4543 * the reloc tree.
4544 */
4545 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
4546 BUG_ON(ret < 0);
4547
4548 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04004549 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
4550 0, 0);
4551 BUG_ON(ret);
4552 extent_buffer_get(path->nodes[0]);
4553 eb = path->nodes[0];
4554 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004555 ret = invalidate_extent_cache(reloc_root, eb, group, root);
4556 BUG_ON(ret);
4557 free_extent_buffer(eb);
4558 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004559
Yan Zhengf82d02d2008-10-29 14:49:05 -04004560 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04004561 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004562 return 0;
4563}
4564
4565static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
4566 struct btrfs_root *root,
4567 struct btrfs_path *path,
4568 struct btrfs_key *first_key,
4569 struct btrfs_ref_path *ref_path)
4570{
4571 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004572
4573 ret = relocate_one_path(trans, root, path, first_key,
4574 ref_path, NULL, NULL);
4575 BUG_ON(ret);
4576
4577 if (root == root->fs_info->extent_root)
4578 btrfs_extent_post_op(trans, root);
Zheng Yan1a40e232008-09-26 10:09:34 -04004579
4580 return 0;
4581}
4582
4583static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
4584 struct btrfs_root *extent_root,
4585 struct btrfs_path *path,
4586 struct btrfs_key *extent_key)
4587{
4588 int ret;
4589
Zheng Yan1a40e232008-09-26 10:09:34 -04004590 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
4591 if (ret)
4592 goto out;
4593 ret = btrfs_del_item(trans, extent_root, path);
4594out:
Chris Masonedbd8d42007-12-21 16:27:24 -05004595 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004596 return ret;
4597}
4598
4599static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
4600 struct btrfs_ref_path *ref_path)
4601{
4602 struct btrfs_key root_key;
4603
4604 root_key.objectid = ref_path->root_objectid;
4605 root_key.type = BTRFS_ROOT_ITEM_KEY;
4606 if (is_cowonly_root(ref_path->root_objectid))
4607 root_key.offset = 0;
4608 else
4609 root_key.offset = (u64)-1;
4610
4611 return btrfs_read_fs_root_no_name(fs_info, &root_key);
4612}
4613
4614static int noinline relocate_one_extent(struct btrfs_root *extent_root,
4615 struct btrfs_path *path,
4616 struct btrfs_key *extent_key,
4617 struct btrfs_block_group_cache *group,
4618 struct inode *reloc_inode, int pass)
4619{
4620 struct btrfs_trans_handle *trans;
4621 struct btrfs_root *found_root;
4622 struct btrfs_ref_path *ref_path = NULL;
4623 struct disk_extent *new_extents = NULL;
4624 int nr_extents = 0;
4625 int loops;
4626 int ret;
4627 int level;
4628 struct btrfs_key first_key;
4629 u64 prev_block = 0;
4630
Zheng Yan1a40e232008-09-26 10:09:34 -04004631
4632 trans = btrfs_start_transaction(extent_root, 1);
4633 BUG_ON(!trans);
4634
4635 if (extent_key->objectid == 0) {
4636 ret = del_extent_zero(trans, extent_root, path, extent_key);
4637 goto out;
4638 }
4639
4640 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
4641 if (!ref_path) {
4642 ret = -ENOMEM;
4643 goto out;
4644 }
4645
4646 for (loops = 0; ; loops++) {
4647 if (loops == 0) {
4648 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
4649 extent_key->objectid);
4650 } else {
4651 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
4652 }
4653 if (ret < 0)
4654 goto out;
4655 if (ret > 0)
4656 break;
4657
4658 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4659 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
4660 continue;
4661
4662 found_root = read_ref_root(extent_root->fs_info, ref_path);
4663 BUG_ON(!found_root);
4664 /*
4665 * for reference counted tree, only process reference paths
4666 * rooted at the latest committed root.
4667 */
4668 if (found_root->ref_cows &&
4669 ref_path->root_generation != found_root->root_key.offset)
4670 continue;
4671
4672 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4673 if (pass == 0) {
4674 /*
4675 * copy data extents to new locations
4676 */
4677 u64 group_start = group->key.objectid;
4678 ret = relocate_data_extent(reloc_inode,
4679 extent_key,
4680 group_start);
4681 if (ret < 0)
4682 goto out;
4683 break;
4684 }
4685 level = 0;
4686 } else {
4687 level = ref_path->owner_objectid;
4688 }
4689
4690 if (prev_block != ref_path->nodes[level]) {
4691 struct extent_buffer *eb;
4692 u64 block_start = ref_path->nodes[level];
4693 u64 block_size = btrfs_level_size(found_root, level);
4694
4695 eb = read_tree_block(found_root, block_start,
4696 block_size, 0);
4697 btrfs_tree_lock(eb);
4698 BUG_ON(level != btrfs_header_level(eb));
4699
4700 if (level == 0)
4701 btrfs_item_key_to_cpu(eb, &first_key, 0);
4702 else
4703 btrfs_node_key_to_cpu(eb, &first_key, 0);
4704
4705 btrfs_tree_unlock(eb);
4706 free_extent_buffer(eb);
4707 prev_block = block_start;
4708 }
4709
4710 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
4711 pass >= 2) {
4712 /*
4713 * use fallback method to process the remaining
4714 * references.
4715 */
4716 if (!new_extents) {
4717 u64 group_start = group->key.objectid;
4718 ret = get_new_locations(reloc_inode,
4719 extent_key,
4720 group_start, 0,
4721 &new_extents,
4722 &nr_extents);
4723 if (ret < 0)
4724 goto out;
4725 }
4726 btrfs_record_root_in_trans(found_root);
4727 ret = replace_one_extent(trans, found_root,
4728 path, extent_key,
4729 &first_key, ref_path,
4730 new_extents, nr_extents);
4731 if (ret < 0)
4732 goto out;
4733 continue;
4734 }
4735
4736 btrfs_record_root_in_trans(found_root);
4737 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4738 ret = relocate_tree_block(trans, found_root, path,
4739 &first_key, ref_path);
4740 } else {
4741 /*
4742 * try to update data extent references while
4743 * keeping metadata shared between snapshots.
4744 */
4745 ret = relocate_one_path(trans, found_root, path,
4746 &first_key, ref_path,
4747 group, reloc_inode);
4748 }
4749 if (ret < 0)
4750 goto out;
4751 }
4752 ret = 0;
4753out:
4754 btrfs_end_transaction(trans, extent_root);
4755 kfree(new_extents);
4756 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05004757 return ret;
4758}
4759
Chris Masonec44a352008-04-28 15:29:52 -04004760static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
4761{
4762 u64 num_devices;
4763 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
4764 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
4765
Chris Masona061fc82008-05-07 11:43:44 -04004766 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04004767 if (num_devices == 1) {
4768 stripped |= BTRFS_BLOCK_GROUP_DUP;
4769 stripped = flags & ~stripped;
4770
4771 /* turn raid0 into single device chunks */
4772 if (flags & BTRFS_BLOCK_GROUP_RAID0)
4773 return stripped;
4774
4775 /* turn mirroring into duplication */
4776 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
4777 BTRFS_BLOCK_GROUP_RAID10))
4778 return stripped | BTRFS_BLOCK_GROUP_DUP;
4779 return flags;
4780 } else {
4781 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04004782 if (flags & stripped)
4783 return flags;
4784
4785 stripped |= BTRFS_BLOCK_GROUP_DUP;
4786 stripped = flags & ~stripped;
4787
4788 /* switch duplicated blocks with raid1 */
4789 if (flags & BTRFS_BLOCK_GROUP_DUP)
4790 return stripped | BTRFS_BLOCK_GROUP_RAID1;
4791
4792 /* turn single device chunks into raid0 */
4793 return stripped | BTRFS_BLOCK_GROUP_RAID0;
4794 }
4795 return flags;
4796}
4797
Chris Mason0ef3e662008-05-24 14:04:53 -04004798int __alloc_chunk_for_shrink(struct btrfs_root *root,
4799 struct btrfs_block_group_cache *shrink_block_group,
4800 int force)
4801{
4802 struct btrfs_trans_handle *trans;
4803 u64 new_alloc_flags;
4804 u64 calc;
4805
Chris Masonc286ac42008-07-22 23:06:41 -04004806 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04004807 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04004808 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004809
Chris Mason0ef3e662008-05-24 14:04:53 -04004810 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04004811 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04004812
Chris Mason0ef3e662008-05-24 14:04:53 -04004813 new_alloc_flags = update_block_group_flags(root,
4814 shrink_block_group->flags);
4815 if (new_alloc_flags != shrink_block_group->flags) {
4816 calc =
4817 btrfs_block_group_used(&shrink_block_group->item);
4818 } else {
4819 calc = shrink_block_group->key.offset;
4820 }
Chris Masonc286ac42008-07-22 23:06:41 -04004821 spin_unlock(&shrink_block_group->lock);
4822
Chris Mason0ef3e662008-05-24 14:04:53 -04004823 do_chunk_alloc(trans, root->fs_info->extent_root,
4824 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04004825
Chris Mason0ef3e662008-05-24 14:04:53 -04004826 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04004827 } else
4828 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04004829 return 0;
4830}
4831
Zheng Yan1a40e232008-09-26 10:09:34 -04004832static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
4833 struct btrfs_root *root,
4834 u64 objectid, u64 size)
4835{
4836 struct btrfs_path *path;
4837 struct btrfs_inode_item *item;
4838 struct extent_buffer *leaf;
4839 int ret;
4840
4841 path = btrfs_alloc_path();
4842 if (!path)
4843 return -ENOMEM;
4844
4845 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
4846 if (ret)
4847 goto out;
4848
4849 leaf = path->nodes[0];
4850 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
4851 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
4852 btrfs_set_inode_generation(leaf, item, 1);
4853 btrfs_set_inode_size(leaf, item, size);
4854 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
4855 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM);
4856 btrfs_mark_buffer_dirty(leaf);
4857 btrfs_release_path(root, path);
4858out:
4859 btrfs_free_path(path);
4860 return ret;
4861}
4862
4863static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
4864 struct btrfs_block_group_cache *group)
4865{
4866 struct inode *inode = NULL;
4867 struct btrfs_trans_handle *trans;
4868 struct btrfs_root *root;
4869 struct btrfs_key root_key;
4870 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
4871 int err = 0;
4872
4873 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4874 root_key.type = BTRFS_ROOT_ITEM_KEY;
4875 root_key.offset = (u64)-1;
4876 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
4877 if (IS_ERR(root))
4878 return ERR_CAST(root);
4879
4880 trans = btrfs_start_transaction(root, 1);
4881 BUG_ON(!trans);
4882
4883 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
4884 if (err)
4885 goto out;
4886
4887 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
4888 BUG_ON(err);
4889
4890 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04004891 group->key.offset, 0, group->key.offset,
4892 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004893 BUG_ON(err);
4894
4895 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
4896 if (inode->i_state & I_NEW) {
4897 BTRFS_I(inode)->root = root;
4898 BTRFS_I(inode)->location.objectid = objectid;
4899 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
4900 BTRFS_I(inode)->location.offset = 0;
4901 btrfs_read_locked_inode(inode);
4902 unlock_new_inode(inode);
4903 BUG_ON(is_bad_inode(inode));
4904 } else {
4905 BUG_ON(1);
4906 }
4907
4908 err = btrfs_orphan_add(trans, inode);
4909out:
4910 btrfs_end_transaction(trans, root);
4911 if (err) {
4912 if (inode)
4913 iput(inode);
4914 inode = ERR_PTR(err);
4915 }
4916 return inode;
4917}
4918
4919int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05004920{
4921 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05004922 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04004923 struct btrfs_fs_info *info = root->fs_info;
4924 struct extent_buffer *leaf;
4925 struct inode *reloc_inode;
4926 struct btrfs_block_group_cache *block_group;
4927 struct btrfs_key key;
Chris Masonedbd8d42007-12-21 16:27:24 -05004928 u64 cur_byte;
4929 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05004930 u32 nritems;
4931 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04004932 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04004933 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05004934
Chris Masonedbd8d42007-12-21 16:27:24 -05004935 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04004936
4937 block_group = btrfs_lookup_block_group(info, group_start);
4938 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05004939
Chris Mason323da792008-05-09 11:46:48 -04004940 printk("btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04004941 (unsigned long long)block_group->key.objectid,
4942 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04004943
Zheng Yan1a40e232008-09-26 10:09:34 -04004944 path = btrfs_alloc_path();
4945 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04004946
Zheng Yan1a40e232008-09-26 10:09:34 -04004947 reloc_inode = create_reloc_inode(info, block_group);
4948 BUG_ON(IS_ERR(reloc_inode));
4949
Zheng Yan1a40e232008-09-26 10:09:34 -04004950 __alloc_chunk_for_shrink(root, block_group, 1);
4951 block_group->ro = 1;
4952 block_group->space_info->total_bytes -= block_group->key.offset;
4953
Zheng Yan1a40e232008-09-26 10:09:34 -04004954 btrfs_start_delalloc_inodes(info->tree_root);
4955 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04004956again:
Chris Masonedbd8d42007-12-21 16:27:24 -05004957 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04004958 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004959 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05004960 key.offset = 0;
4961 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05004962 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05004963
Zheng Yan1a40e232008-09-26 10:09:34 -04004964 trans = btrfs_start_transaction(info->tree_root, 1);
4965 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04004966
Zheng Yan1a40e232008-09-26 10:09:34 -04004967 mutex_lock(&root->fs_info->cleaner_mutex);
4968 btrfs_clean_old_snapshots(info->tree_root);
4969 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
4970 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04004971
Yan73e48b22008-01-03 14:14:39 -05004972 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05004973 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4974 if (ret < 0)
4975 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04004976next:
Chris Masonedbd8d42007-12-21 16:27:24 -05004977 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05004978 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05004979 if (path->slots[0] >= nritems) {
4980 ret = btrfs_next_leaf(root, path);
4981 if (ret < 0)
4982 goto out;
4983 if (ret == 1) {
4984 ret = 0;
4985 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004986 }
Yan73e48b22008-01-03 14:14:39 -05004987 leaf = path->nodes[0];
4988 nritems = btrfs_header_nritems(leaf);
4989 }
4990
Zheng Yan1a40e232008-09-26 10:09:34 -04004991 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05004992
Zheng Yan1a40e232008-09-26 10:09:34 -04004993 if (key.objectid >= block_group->key.objectid +
4994 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04004995 break;
4996
Chris Mason725c8462008-01-04 16:47:16 -05004997 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05004998 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004999 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05005000 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005001 continue;
Chris Mason725c8462008-01-04 16:47:16 -05005002 }
5003 progress = 1;
5004
Zheng Yan1a40e232008-09-26 10:09:34 -04005005 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5006 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05005007 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005008 goto next;
5009 }
Yan73e48b22008-01-03 14:14:39 -05005010
Chris Masonedbd8d42007-12-21 16:27:24 -05005011 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005012 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05005013 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005014
5015 __alloc_chunk_for_shrink(root, block_group, 0);
5016 ret = relocate_one_extent(root, path, &key, block_group,
5017 reloc_inode, pass);
5018 BUG_ON(ret < 0);
5019
5020 key.objectid = cur_byte;
5021 key.type = 0;
5022 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005023 }
5024
5025 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005026
5027 if (pass == 0) {
5028 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5029 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5030 WARN_ON(reloc_inode->i_mapping->nrpages);
5031 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005032
5033 if (total_found > 0) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005034 printk("btrfs found %llu extents in pass %d\n",
5035 (unsigned long long)total_found, pass);
5036 pass++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005037 goto again;
5038 }
5039
Zheng Yan1a40e232008-09-26 10:09:34 -04005040 /* delete reloc_inode */
5041 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005042
Zheng Yan1a40e232008-09-26 10:09:34 -04005043 /* unpin extents in this range */
5044 trans = btrfs_start_transaction(info->tree_root, 1);
5045 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005046
Zheng Yan1a40e232008-09-26 10:09:34 -04005047 spin_lock(&block_group->lock);
5048 WARN_ON(block_group->pinned > 0);
5049 WARN_ON(block_group->reserved > 0);
5050 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5051 spin_unlock(&block_group->lock);
5052 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005053out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005054 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005055 return ret;
5056}
5057
Chris Mason0b86a832008-03-24 15:01:56 -04005058int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
5059 struct btrfs_key *key)
5060{
Chris Mason925baed2008-06-25 16:01:30 -04005061 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005062 struct btrfs_key found_key;
5063 struct extent_buffer *leaf;
5064 int slot;
5065
5066 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5067 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005068 goto out;
5069
Chris Mason0b86a832008-03-24 15:01:56 -04005070 while(1) {
5071 slot = path->slots[0];
5072 leaf = path->nodes[0];
5073 if (slot >= btrfs_header_nritems(leaf)) {
5074 ret = btrfs_next_leaf(root, path);
5075 if (ret == 0)
5076 continue;
5077 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005078 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005079 break;
5080 }
5081 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5082
5083 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005084 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5085 ret = 0;
5086 goto out;
5087 }
Chris Mason0b86a832008-03-24 15:01:56 -04005088 path->slots[0]++;
5089 }
5090 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005091out:
Chris Mason0b86a832008-03-24 15:01:56 -04005092 return ret;
5093}
5094
Zheng Yan1a40e232008-09-26 10:09:34 -04005095int btrfs_free_block_groups(struct btrfs_fs_info *info)
5096{
5097 struct btrfs_block_group_cache *block_group;
5098 struct rb_node *n;
5099
Zheng Yan1a40e232008-09-26 10:09:34 -04005100 spin_lock(&info->block_group_cache_lock);
5101 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5102 block_group = rb_entry(n, struct btrfs_block_group_cache,
5103 cache_node);
5104
5105 spin_unlock(&info->block_group_cache_lock);
5106 btrfs_remove_free_space_cache(block_group);
5107 spin_lock(&info->block_group_cache_lock);
5108
5109 rb_erase(&block_group->cache_node,
5110 &info->block_group_cache_tree);
Josef Bacik80eb2342008-10-29 14:49:05 -04005111 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005112 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005113 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005114 kfree(block_group);
5115 }
5116 spin_unlock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005117 return 0;
5118}
5119
Chris Mason9078a3e2007-04-26 16:46:15 -04005120int btrfs_read_block_groups(struct btrfs_root *root)
5121{
5122 struct btrfs_path *path;
5123 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005124 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005125 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005126 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005127 struct btrfs_key key;
5128 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005129 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005130
Chris Masonbe744172007-05-06 10:15:01 -04005131 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005132 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005133 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005134 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005135 path = btrfs_alloc_path();
5136 if (!path)
5137 return -ENOMEM;
5138
5139 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005140 ret = find_first_block_group(root, path, &key);
5141 if (ret > 0) {
5142 ret = 0;
5143 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005144 }
Chris Mason0b86a832008-03-24 15:01:56 -04005145 if (ret != 0)
5146 goto error;
5147
Chris Mason5f39d392007-10-15 16:14:19 -04005148 leaf = path->nodes[0];
5149 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005150 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005151 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005152 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005153 break;
5154 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005155
Chris Masonc286ac42008-07-22 23:06:41 -04005156 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005157 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005158 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005159 read_extent_buffer(leaf, &cache->item,
5160 btrfs_item_ptr_offset(leaf, path->slots[0]),
5161 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005162 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005163
Chris Mason9078a3e2007-04-26 16:46:15 -04005164 key.objectid = found_key.objectid + found_key.offset;
5165 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005166 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005167
Chris Mason6324fbf2008-03-24 15:01:59 -04005168 ret = update_space_info(info, cache->flags, found_key.offset,
5169 btrfs_block_group_used(&cache->item),
5170 &space_info);
5171 BUG_ON(ret);
5172 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005173 down_write(&space_info->groups_sem);
5174 list_add_tail(&cache->list, &space_info->block_groups);
5175 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005176
Josef Bacik0f9dd462008-09-23 13:14:11 -04005177 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5178 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005179
5180 set_avail_alloc_bits(root->fs_info, cache->flags);
Chris Mason9078a3e2007-04-26 16:46:15 -04005181 }
Chris Mason0b86a832008-03-24 15:01:56 -04005182 ret = 0;
5183error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005184 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005185 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005186}
Chris Mason6324fbf2008-03-24 15:01:59 -04005187
5188int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5189 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005190 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005191 u64 size)
5192{
5193 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005194 struct btrfs_root *extent_root;
5195 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005196
5197 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005198
Chris Masone02119d2008-09-05 16:13:11 -04005199 root->fs_info->last_trans_new_blockgroup = trans->transid;
5200
Chris Mason8f18cf12008-04-25 16:53:30 -04005201 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005202 if (!cache)
5203 return -ENOMEM;
5204
Chris Masone17cade2008-04-15 15:41:47 -04005205 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005206 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04005207 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005208 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005209 INIT_LIST_HEAD(&cache->list);
Chris Mason6324fbf2008-03-24 15:01:59 -04005210 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04005211
Chris Mason6324fbf2008-03-24 15:01:59 -04005212 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005213 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5214 cache->flags = type;
5215 btrfs_set_block_group_flags(&cache->item, type);
5216
5217 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5218 &cache->space_info);
5219 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005220 down_write(&cache->space_info->groups_sem);
5221 list_add_tail(&cache->list, &cache->space_info->block_groups);
5222 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005223
Josef Bacik0f9dd462008-09-23 13:14:11 -04005224 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5225 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005226
Chris Mason6324fbf2008-03-24 15:01:59 -04005227 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5228 sizeof(cache->item));
5229 BUG_ON(ret);
5230
5231 finish_current_insert(trans, extent_root);
5232 ret = del_pending_extents(trans, extent_root);
5233 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04005234 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005235
Chris Mason6324fbf2008-03-24 15:01:59 -04005236 return 0;
5237}
Zheng Yan1a40e232008-09-26 10:09:34 -04005238
5239int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5240 struct btrfs_root *root, u64 group_start)
5241{
5242 struct btrfs_path *path;
5243 struct btrfs_block_group_cache *block_group;
5244 struct btrfs_key key;
5245 int ret;
5246
Zheng Yan1a40e232008-09-26 10:09:34 -04005247 root = root->fs_info->extent_root;
5248
5249 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5250 BUG_ON(!block_group);
5251
5252 memcpy(&key, &block_group->key, sizeof(key));
5253
5254 path = btrfs_alloc_path();
5255 BUG_ON(!path);
5256
5257 btrfs_remove_free_space_cache(block_group);
5258 rb_erase(&block_group->cache_node,
5259 &root->fs_info->block_group_cache_tree);
Josef Bacik80eb2342008-10-29 14:49:05 -04005260 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005261 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005262 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005263
5264 /*
5265 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5266 kfree(shrink_block_group);
5267 */
5268
5269 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5270 if (ret > 0)
5271 ret = -EIO;
5272 if (ret < 0)
5273 goto out;
5274
5275 ret = btrfs_del_item(trans, root, path);
5276out:
5277 btrfs_free_path(path);
5278 return ret;
5279}