blob: b33e0bfb99e1f0be74832a11377e8594c61365e9 [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 Mason4b4e25f2008-11-20 10:22:27 -050022#include <linux/version.h>
23#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050024#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040025#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050026#include "ctree.h"
27#include "disk-io.h"
28#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040029#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040030#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040031#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040032#include "ref-cache.h"
Chris Mason15916de2008-11-19 21:17:22 -050033#include "compat.h"
Chris Masonfec577f2007-02-26 10:40:21 -050034
Zheng Yan31840ae2008-09-23 13:14:14 -040035#define PENDING_EXTENT_INSERT 0
36#define PENDING_EXTENT_DELETE 1
37#define PENDING_BACKREF_UPDATE 2
38
39struct pending_extent_op {
40 int type;
41 u64 bytenr;
42 u64 num_bytes;
43 u64 parent;
44 u64 orig_parent;
45 u64 generation;
46 u64 orig_generation;
47 int level;
Josef Bacikf3465ca2008-11-12 14:19:50 -050048 struct list_head list;
49 int del;
Zheng Yan31840ae2008-09-23 13:14:14 -040050};
51
Chris Masone089f052007-03-16 16:20:31 -040052static int finish_current_insert(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -040053 btrfs_root *extent_root, int all);
Chris Masone20d96d2007-03-22 12:13:20 -040054static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -040055 btrfs_root *extent_root, int all);
Chris Mason925baed2008-06-25 16:01:30 -040056static struct btrfs_block_group_cache *
57__btrfs_find_block_group(struct btrfs_root *root,
58 struct btrfs_block_group_cache *hint,
59 u64 search_start, int data, int owner);
Josef Bacikf3465ca2008-11-12 14:19:50 -050060static int pin_down_bytes(struct btrfs_trans_handle *trans,
61 struct btrfs_root *root,
62 u64 bytenr, u64 num_bytes, int is_data);
63static int update_block_group(struct btrfs_trans_handle *trans,
64 struct btrfs_root *root,
65 u64 bytenr, u64 num_bytes, int alloc,
66 int mark_free);
Yand548ee52008-01-03 13:56:30 -050067
Josef Bacik0f9dd462008-09-23 13:14:11 -040068static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
69{
70 return (cache->flags & bits) == bits;
71}
72
73/*
74 * this adds the block group to the fs_info rb tree for the block group
75 * cache
76 */
77int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
78 struct btrfs_block_group_cache *block_group)
79{
80 struct rb_node **p;
81 struct rb_node *parent = NULL;
82 struct btrfs_block_group_cache *cache;
83
84 spin_lock(&info->block_group_cache_lock);
85 p = &info->block_group_cache_tree.rb_node;
86
87 while (*p) {
88 parent = *p;
89 cache = rb_entry(parent, struct btrfs_block_group_cache,
90 cache_node);
91 if (block_group->key.objectid < cache->key.objectid) {
92 p = &(*p)->rb_left;
93 } else if (block_group->key.objectid > cache->key.objectid) {
94 p = &(*p)->rb_right;
95 } else {
96 spin_unlock(&info->block_group_cache_lock);
97 return -EEXIST;
98 }
99 }
100
101 rb_link_node(&block_group->cache_node, parent, p);
102 rb_insert_color(&block_group->cache_node,
103 &info->block_group_cache_tree);
104 spin_unlock(&info->block_group_cache_lock);
105
106 return 0;
107}
108
109/*
110 * This will return the block group at or after bytenr if contains is 0, else
111 * it will return the block group that contains the bytenr
112 */
113static struct btrfs_block_group_cache *
114block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
115 int contains)
116{
117 struct btrfs_block_group_cache *cache, *ret = NULL;
118 struct rb_node *n;
119 u64 end, start;
120
121 spin_lock(&info->block_group_cache_lock);
122 n = info->block_group_cache_tree.rb_node;
123
124 while (n) {
125 cache = rb_entry(n, struct btrfs_block_group_cache,
126 cache_node);
127 end = cache->key.objectid + cache->key.offset - 1;
128 start = cache->key.objectid;
129
130 if (bytenr < start) {
131 if (!contains && (!ret || start < ret->key.objectid))
132 ret = cache;
133 n = n->rb_left;
134 } else if (bytenr > start) {
135 if (contains && bytenr <= end) {
136 ret = cache;
137 break;
138 }
139 n = n->rb_right;
140 } else {
141 ret = cache;
142 break;
143 }
144 }
145 spin_unlock(&info->block_group_cache_lock);
146
147 return ret;
148}
149
150/*
151 * this is only called by cache_block_group, since we could have freed extents
152 * we need to check the pinned_extents for any extents that can't be used yet
153 * since their free space will be released as soon as the transaction commits.
154 */
155static int add_new_free_space(struct btrfs_block_group_cache *block_group,
156 struct btrfs_fs_info *info, u64 start, u64 end)
157{
158 u64 extent_start, extent_end, size;
159 int ret;
160
Josef Bacik25179202008-10-29 14:49:05 -0400161 mutex_lock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400162 while (start < end) {
163 ret = find_first_extent_bit(&info->pinned_extents, start,
164 &extent_start, &extent_end,
165 EXTENT_DIRTY);
166 if (ret)
167 break;
168
169 if (extent_start == start) {
170 start = extent_end + 1;
171 } else if (extent_start > start && extent_start < end) {
172 size = extent_start - start;
Josef Bacik25179202008-10-29 14:49:05 -0400173 ret = btrfs_add_free_space_lock(block_group, start,
174 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400175 BUG_ON(ret);
176 start = extent_end + 1;
177 } else {
178 break;
179 }
180 }
181
182 if (start < end) {
183 size = end - start;
Josef Bacik25179202008-10-29 14:49:05 -0400184 ret = btrfs_add_free_space_lock(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400185 BUG_ON(ret);
186 }
Josef Bacik25179202008-10-29 14:49:05 -0400187 mutex_unlock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400188
189 return 0;
190}
191
Chris Masone37c9e62007-05-09 20:13:14 -0400192static int cache_block_group(struct btrfs_root *root,
193 struct btrfs_block_group_cache *block_group)
194{
195 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400196 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400197 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400198 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400199 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -0400200 u64 last = 0;
Yan7d7d6062007-09-14 16:15:28 -0400201 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -0400202 int found = 0;
203
Chris Mason00f5c792007-11-30 10:09:33 -0500204 if (!block_group)
205 return 0;
206
Chris Masone37c9e62007-05-09 20:13:14 -0400207 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400208
209 if (block_group->cached)
210 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400211
Chris Masone37c9e62007-05-09 20:13:14 -0400212 path = btrfs_alloc_path();
213 if (!path)
214 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400215
Chris Mason2cc58cf2007-08-27 16:49:44 -0400216 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400217 /*
218 * we get into deadlocks with paths held by callers of this function.
219 * since the alloc_mutex is protecting things right now, just
220 * skip the locking here
221 */
222 path->skip_locking = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400223 first_free = max_t(u64, block_group->key.objectid,
224 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Masone37c9e62007-05-09 20:13:14 -0400225 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -0400226 key.offset = 0;
227 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
228 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
229 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400230 goto err;
Chris Mason0b86a832008-03-24 15:01:56 -0400231 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -0500232 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400233 goto err;
Yand548ee52008-01-03 13:56:30 -0500234 if (ret == 0) {
235 leaf = path->nodes[0];
236 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
237 if (key.objectid + key.offset > first_free)
238 first_free = key.objectid + key.offset;
239 }
Chris Masone37c9e62007-05-09 20:13:14 -0400240 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400241 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400242 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400243 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400244 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400245 if (ret < 0)
246 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400247 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400248 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400249 else
Chris Masone37c9e62007-05-09 20:13:14 -0400250 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400251 }
Chris Mason5f39d392007-10-15 16:14:19 -0400252 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400253 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400254 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400255
Chris Masone37c9e62007-05-09 20:13:14 -0400256 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400257 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400258 break;
Yan7d7d6062007-09-14 16:15:28 -0400259
260 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
261 if (!found) {
262 last = first_free;
263 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400264 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400265
266 add_new_free_space(block_group, root->fs_info, last,
267 key.objectid);
268
Yan7d7d6062007-09-14 16:15:28 -0400269 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400270 }
Yan7d7d6062007-09-14 16:15:28 -0400271next:
Chris Masone37c9e62007-05-09 20:13:14 -0400272 path->slots[0]++;
273 }
274
Yan7d7d6062007-09-14 16:15:28 -0400275 if (!found)
276 last = first_free;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400277
278 add_new_free_space(block_group, root->fs_info, last,
279 block_group->key.objectid +
280 block_group->key.offset);
281
Chris Masone37c9e62007-05-09 20:13:14 -0400282 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400283 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400284err:
Chris Masone37c9e62007-05-09 20:13:14 -0400285 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400286 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400287}
288
Josef Bacik0f9dd462008-09-23 13:14:11 -0400289/*
290 * return the block group that starts at or after bytenr
291 */
Chris Mason0ef3e662008-05-24 14:04:53 -0400292struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
293 btrfs_fs_info *info,
294 u64 bytenr)
295{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400296 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400297
Josef Bacik0f9dd462008-09-23 13:14:11 -0400298 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400299
Josef Bacik0f9dd462008-09-23 13:14:11 -0400300 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400301}
302
Josef Bacik0f9dd462008-09-23 13:14:11 -0400303/*
304 * return the block group that contains teh given bytenr
305 */
Chris Mason5276aed2007-06-11 21:33:38 -0400306struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
307 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400308 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400309{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400310 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400311
Josef Bacik0f9dd462008-09-23 13:14:11 -0400312 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400313
Josef Bacik0f9dd462008-09-23 13:14:11 -0400314 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400315}
Chris Mason0b86a832008-03-24 15:01:56 -0400316
Josef Bacik0f9dd462008-09-23 13:14:11 -0400317static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
318 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400319{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400320 struct list_head *head = &info->space_info;
321 struct list_head *cur;
322 struct btrfs_space_info *found;
323 list_for_each(cur, head) {
324 found = list_entry(cur, struct btrfs_space_info, list);
325 if (found->flags == flags)
326 return found;
327 }
328 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400329}
330
Josef Bacik80eb2342008-10-29 14:49:05 -0400331static u64 div_factor(u64 num, int factor)
332{
333 if (factor == 10)
334 return num;
335 num *= factor;
336 do_div(num, 10);
337 return num;
338}
339
Chris Mason925baed2008-06-25 16:01:30 -0400340static struct btrfs_block_group_cache *
341__btrfs_find_block_group(struct btrfs_root *root,
342 struct btrfs_block_group_cache *hint,
343 u64 search_start, int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400344{
Chris Mason96b51792007-10-15 16:15:19 -0400345 struct btrfs_block_group_cache *cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400346 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400347 struct btrfs_fs_info *info = root->fs_info;
348 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400349 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400350 u64 free_check;
Chris Mason31f3c992007-04-30 15:25:45 -0400351 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400352 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400353 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400354
Chris Masona236aed2008-04-29 09:38:00 -0400355 if (data & BTRFS_BLOCK_GROUP_METADATA)
356 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400357
Chris Mason0ef3e662008-05-24 14:04:53 -0400358 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400359 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400360 shint = btrfs_lookup_first_block_group(info, search_start);
Yan Zheng2b820322008-11-17 21:11:30 -0500361 if (shint && block_group_bits(shint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400362 spin_lock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400363 used = btrfs_block_group_used(&shint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400364 if (used + shint->pinned + shint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500365 div_factor(shint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400366 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400367 return shint;
368 }
Chris Masonc286ac42008-07-22 23:06:41 -0400369 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400370 }
371 }
Yan Zheng2b820322008-11-17 21:11:30 -0500372 if (hint && block_group_bits(hint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400373 spin_lock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400374 used = btrfs_block_group_used(&hint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400375 if (used + hint->pinned + hint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500376 div_factor(hint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400377 spin_unlock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400378 return hint;
379 }
Chris Masonc286ac42008-07-22 23:06:41 -0400380 spin_unlock(&hint->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400381 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400382 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400383 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400384 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400385 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400386 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400387 }
Chris Mason31f3c992007-04-30 15:25:45 -0400388again:
Zheng Yane8569812008-09-26 10:05:48 -0400389 while (1) {
390 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400391 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400392 break;
Chris Mason96b51792007-10-15 16:15:19 -0400393
Chris Masonc286ac42008-07-22 23:06:41 -0400394 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400395 last = cache->key.objectid + cache->key.offset;
396 used = btrfs_block_group_used(&cache->item);
397
Yan Zheng2b820322008-11-17 21:11:30 -0500398 if (block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400399 free_check = div_factor(cache->key.offset, factor);
Zheng Yane8569812008-09-26 10:05:48 -0400400 if (used + cache->pinned + cache->reserved <
401 free_check) {
Chris Mason8790d502008-04-03 16:29:03 -0400402 found_group = cache;
Chris Masonc286ac42008-07-22 23:06:41 -0400403 spin_unlock(&cache->lock);
Chris Mason8790d502008-04-03 16:29:03 -0400404 goto found;
405 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400406 }
Chris Masonc286ac42008-07-22 23:06:41 -0400407 spin_unlock(&cache->lock);
Chris Masonde428b62007-05-18 13:28:27 -0400408 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400409 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400410 if (!wrapped) {
411 last = search_start;
412 wrapped = 1;
413 goto again;
414 }
415 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400416 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400417 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400418 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400419 goto again;
420 }
Chris Masonbe744172007-05-06 10:15:01 -0400421found:
Chris Mason31f3c992007-04-30 15:25:45 -0400422 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400423}
424
Chris Mason925baed2008-06-25 16:01:30 -0400425struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
426 struct btrfs_block_group_cache
427 *hint, u64 search_start,
428 int data, int owner)
429{
430
431 struct btrfs_block_group_cache *ret;
Chris Mason925baed2008-06-25 16:01:30 -0400432 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
Chris Mason925baed2008-06-25 16:01:30 -0400433 return ret;
434}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400435
Chris Masone02119d2008-09-05 16:13:11 -0400436/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400437int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400438{
439 int ret;
440 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400441 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400442
Zheng Yan31840ae2008-09-23 13:14:14 -0400443 path = btrfs_alloc_path();
444 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400445 key.objectid = start;
446 key.offset = len;
447 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
448 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
449 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400450 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500451 return ret;
452}
453
Chris Masond8d5f3e2007-12-11 12:42:00 -0500454/*
455 * Back reference rules. Back refs have three main goals:
456 *
457 * 1) differentiate between all holders of references to an extent so that
458 * when a reference is dropped we can make sure it was a valid reference
459 * before freeing the extent.
460 *
461 * 2) Provide enough information to quickly find the holders of an extent
462 * if we notice a given block is corrupted or bad.
463 *
464 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
465 * maintenance. This is actually the same as #2, but with a slightly
466 * different use case.
467 *
468 * File extents can be referenced by:
469 *
470 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400471 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500472 * - different offsets inside a file (bookend extents in file.c)
473 *
474 * The extent ref structure has fields for:
475 *
476 * - Objectid of the subvolume root
477 * - Generation number of the tree holding the reference
478 * - objectid of the file holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400479 * - number of references holding by parent node (alway 1 for tree blocks)
480 *
481 * Btree leaf may hold multiple references to a file extent. In most cases,
482 * these references are from same file and the corresponding offsets inside
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400483 * the file are close together.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500484 *
485 * When a file extent is allocated the fields are filled in:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400486 * (root_key.objectid, trans->transid, inode objectid, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500487 *
488 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400489 * in the leaf. It looks similar to the create case, but trans->transid will
490 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500491 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400492 * (root_key.objectid, trans->transid, inode objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400493 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500494 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400495 * When a file extent is removed either during snapshot deletion or
496 * file truncation, we find the corresponding back reference and check
497 * the following fields:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500498 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400499 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
500 * inode objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500501 *
502 * Btree extents can be referenced by:
503 *
504 * - Different subvolumes
505 * - Different generations of the same subvolume
506 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500507 * When a tree block is created, back references are inserted:
508 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400509 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500510 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400511 * When a tree block is cow'd, new back references are added for all the
512 * blocks it points to. If the tree block isn't in reference counted root,
513 * the old back references are removed. These new back references are of
514 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500515 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400516 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500517 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400518 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500519 *
520 * if backref was for a tree root:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400521 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500522 * else
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400523 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500524 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400525 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500526 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400527 * The key objectid corresponds to the first byte in the extent, the key
528 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
529 * byte of parent extent. If a extent is tree root, the key offset is set
530 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500531 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400532
533static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
534 struct btrfs_root *root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400535 struct btrfs_path *path,
536 u64 bytenr, u64 parent,
537 u64 ref_root, u64 ref_generation,
538 u64 owner_objectid, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500539{
Chris Mason74493f72007-12-11 09:25:06 -0500540 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400541 struct btrfs_extent_ref *ref;
542 struct extent_buffer *leaf;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400543 u64 ref_objectid;
Chris Mason74493f72007-12-11 09:25:06 -0500544 int ret;
545
Chris Mason74493f72007-12-11 09:25:06 -0500546 key.objectid = bytenr;
547 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400548 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500549
Zheng Yan31840ae2008-09-23 13:14:14 -0400550 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
551 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500552 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400553 if (ret > 0) {
554 ret = -ENOENT;
555 goto out;
556 }
557
558 leaf = path->nodes[0];
559 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400560 ref_objectid = btrfs_ref_objectid(leaf, ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400561 if (btrfs_ref_root(leaf, ref) != ref_root ||
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400562 btrfs_ref_generation(leaf, ref) != ref_generation ||
563 (ref_objectid != owner_objectid &&
564 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400565 ret = -EIO;
566 WARN_ON(1);
567 goto out;
568 }
569 ret = 0;
570out:
571 return ret;
572}
573
Josef Bacikf3465ca2008-11-12 14:19:50 -0500574/*
575 * updates all the backrefs that are pending on update_list for the
576 * extent_root
577 */
578static int noinline update_backrefs(struct btrfs_trans_handle *trans,
579 struct btrfs_root *extent_root,
580 struct btrfs_path *path,
581 struct list_head *update_list)
582{
583 struct btrfs_key key;
584 struct btrfs_extent_ref *ref;
585 struct btrfs_fs_info *info = extent_root->fs_info;
586 struct pending_extent_op *op;
587 struct extent_buffer *leaf;
588 int ret = 0;
589 struct list_head *cur = update_list->next;
590 u64 ref_objectid;
591 u64 ref_root = extent_root->root_key.objectid;
592
593 op = list_entry(cur, struct pending_extent_op, list);
594
595search:
596 key.objectid = op->bytenr;
597 key.type = BTRFS_EXTENT_REF_KEY;
598 key.offset = op->orig_parent;
599
600 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
601 BUG_ON(ret);
602
603 leaf = path->nodes[0];
604
605loop:
606 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
607
608 ref_objectid = btrfs_ref_objectid(leaf, ref);
609
610 if (btrfs_ref_root(leaf, ref) != ref_root ||
611 btrfs_ref_generation(leaf, ref) != op->orig_generation ||
612 (ref_objectid != op->level &&
613 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
614 printk(KERN_ERR "couldn't find %Lu, parent %Lu, root %Lu, "
615 "owner %u\n", op->bytenr, op->orig_parent,
616 ref_root, op->level);
617 btrfs_print_leaf(extent_root, leaf);
618 BUG();
619 }
620
621 key.objectid = op->bytenr;
622 key.offset = op->parent;
623 key.type = BTRFS_EXTENT_REF_KEY;
624 ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
625 BUG_ON(ret);
626 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
627 btrfs_set_ref_generation(leaf, ref, op->generation);
628
629 cur = cur->next;
630
631 list_del_init(&op->list);
632 unlock_extent(&info->extent_ins, op->bytenr,
633 op->bytenr + op->num_bytes - 1, GFP_NOFS);
634 kfree(op);
635
636 if (cur == update_list) {
637 btrfs_mark_buffer_dirty(path->nodes[0]);
638 btrfs_release_path(extent_root, path);
639 goto out;
640 }
641
642 op = list_entry(cur, struct pending_extent_op, list);
643
644 path->slots[0]++;
645 while (path->slots[0] < btrfs_header_nritems(leaf)) {
646 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
647 if (key.objectid == op->bytenr &&
648 key.type == BTRFS_EXTENT_REF_KEY)
649 goto loop;
650 path->slots[0]++;
651 }
652
653 btrfs_mark_buffer_dirty(path->nodes[0]);
654 btrfs_release_path(extent_root, path);
655 goto search;
656
657out:
658 return 0;
659}
660
661static int noinline insert_extents(struct btrfs_trans_handle *trans,
662 struct btrfs_root *extent_root,
663 struct btrfs_path *path,
664 struct list_head *insert_list, int nr)
665{
666 struct btrfs_key *keys;
667 u32 *data_size;
668 struct pending_extent_op *op;
669 struct extent_buffer *leaf;
670 struct list_head *cur = insert_list->next;
671 struct btrfs_fs_info *info = extent_root->fs_info;
672 u64 ref_root = extent_root->root_key.objectid;
673 int i = 0, last = 0, ret;
674 int total = nr * 2;
675
676 if (!nr)
677 return 0;
678
679 keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
680 if (!keys)
681 return -ENOMEM;
682
683 data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
684 if (!data_size) {
685 kfree(keys);
686 return -ENOMEM;
687 }
688
689 list_for_each_entry(op, insert_list, list) {
690 keys[i].objectid = op->bytenr;
691 keys[i].offset = op->num_bytes;
692 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
693 data_size[i] = sizeof(struct btrfs_extent_item);
694 i++;
695
696 keys[i].objectid = op->bytenr;
697 keys[i].offset = op->parent;
698 keys[i].type = BTRFS_EXTENT_REF_KEY;
699 data_size[i] = sizeof(struct btrfs_extent_ref);
700 i++;
701 }
702
703 op = list_entry(cur, struct pending_extent_op, list);
704 i = 0;
705 while (i < total) {
706 int c;
707 ret = btrfs_insert_some_items(trans, extent_root, path,
708 keys+i, data_size+i, total-i);
709 BUG_ON(ret < 0);
710
711 if (last && ret > 1)
712 BUG();
713
714 leaf = path->nodes[0];
715 for (c = 0; c < ret; c++) {
716 int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
717
718 /*
719 * if the first item we inserted was a backref, then
720 * the EXTENT_ITEM will be the odd c's, else it will
721 * be the even c's
722 */
723 if ((ref_first && (c % 2)) ||
724 (!ref_first && !(c % 2))) {
725 struct btrfs_extent_item *itm;
726
727 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
728 struct btrfs_extent_item);
729 btrfs_set_extent_refs(path->nodes[0], itm, 1);
730 op->del++;
731 } else {
732 struct btrfs_extent_ref *ref;
733
734 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
735 struct btrfs_extent_ref);
736 btrfs_set_ref_root(leaf, ref, ref_root);
737 btrfs_set_ref_generation(leaf, ref,
738 op->generation);
739 btrfs_set_ref_objectid(leaf, ref, op->level);
740 btrfs_set_ref_num_refs(leaf, ref, 1);
741 op->del++;
742 }
743
744 /*
745 * using del to see when its ok to free up the
746 * pending_extent_op. In the case where we insert the
747 * last item on the list in order to help do batching
748 * we need to not free the extent op until we actually
749 * insert the extent_item
750 */
751 if (op->del == 2) {
752 unlock_extent(&info->extent_ins, op->bytenr,
753 op->bytenr + op->num_bytes - 1,
754 GFP_NOFS);
755 cur = cur->next;
756 list_del_init(&op->list);
757 kfree(op);
758 if (cur != insert_list)
759 op = list_entry(cur,
760 struct pending_extent_op,
761 list);
762 }
763 }
764 btrfs_mark_buffer_dirty(leaf);
765 btrfs_release_path(extent_root, path);
766
767 /*
768 * Ok backref's and items usually go right next to eachother,
769 * but if we could only insert 1 item that means that we
770 * inserted on the end of a leaf, and we have no idea what may
771 * be on the next leaf so we just play it safe. In order to
772 * try and help this case we insert the last thing on our
773 * insert list so hopefully it will end up being the last
774 * thing on the leaf and everything else will be before it,
775 * which will let us insert a whole bunch of items at the same
776 * time.
777 */
778 if (ret == 1 && !last && (i + ret < total)) {
779 /*
780 * last: where we will pick up the next time around
781 * i: our current key to insert, will be total - 1
782 * cur: the current op we are screwing with
783 * op: duh
784 */
785 last = i + ret;
786 i = total - 1;
787 cur = insert_list->prev;
788 op = list_entry(cur, struct pending_extent_op, list);
789 } else if (last) {
790 /*
791 * ok we successfully inserted the last item on the
792 * list, lets reset everything
793 *
794 * i: our current key to insert, so where we left off
795 * last time
796 * last: done with this
797 * cur: the op we are messing with
798 * op: duh
799 * total: since we inserted the last key, we need to
800 * decrement total so we dont overflow
801 */
802 i = last;
803 last = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -0500804 total--;
Liu Huib4eec2c2008-11-18 11:30:10 -0500805 if (i < total) {
806 cur = insert_list->next;
807 op = list_entry(cur, struct pending_extent_op,
808 list);
809 }
Josef Bacikf3465ca2008-11-12 14:19:50 -0500810 } else {
811 i += ret;
812 }
813
814 cond_resched();
815 }
816 ret = 0;
817 kfree(keys);
818 kfree(data_size);
819 return ret;
820}
821
Zheng Yan31840ae2008-09-23 13:14:14 -0400822static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
823 struct btrfs_root *root,
824 struct btrfs_path *path,
825 u64 bytenr, u64 parent,
826 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400827 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400828{
829 struct btrfs_key key;
830 struct extent_buffer *leaf;
831 struct btrfs_extent_ref *ref;
832 u32 num_refs;
833 int ret;
834
835 key.objectid = bytenr;
836 key.type = BTRFS_EXTENT_REF_KEY;
837 key.offset = parent;
838
839 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
840 if (ret == 0) {
841 leaf = path->nodes[0];
842 ref = btrfs_item_ptr(leaf, path->slots[0],
843 struct btrfs_extent_ref);
844 btrfs_set_ref_root(leaf, ref, ref_root);
845 btrfs_set_ref_generation(leaf, ref, ref_generation);
846 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400847 btrfs_set_ref_num_refs(leaf, ref, 1);
848 } else if (ret == -EEXIST) {
849 u64 existing_owner;
850 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
851 leaf = path->nodes[0];
852 ref = btrfs_item_ptr(leaf, path->slots[0],
853 struct btrfs_extent_ref);
854 if (btrfs_ref_root(leaf, ref) != ref_root ||
855 btrfs_ref_generation(leaf, ref) != ref_generation) {
856 ret = -EIO;
857 WARN_ON(1);
858 goto out;
859 }
860
861 num_refs = btrfs_ref_num_refs(leaf, ref);
862 BUG_ON(num_refs == 0);
863 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
864
865 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400866 if (existing_owner != owner_objectid &&
867 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400868 btrfs_set_ref_objectid(leaf, ref,
869 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400870 }
871 ret = 0;
872 } else {
873 goto out;
874 }
Chris Mason7bb86312007-12-11 09:25:06 -0500875 btrfs_mark_buffer_dirty(path->nodes[0]);
876out:
877 btrfs_release_path(root, path);
878 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500879}
880
Zheng Yan31840ae2008-09-23 13:14:14 -0400881static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
882 struct btrfs_root *root,
883 struct btrfs_path *path)
884{
885 struct extent_buffer *leaf;
886 struct btrfs_extent_ref *ref;
887 u32 num_refs;
888 int ret = 0;
889
890 leaf = path->nodes[0];
891 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
892 num_refs = btrfs_ref_num_refs(leaf, ref);
893 BUG_ON(num_refs == 0);
894 num_refs -= 1;
895 if (num_refs == 0) {
896 ret = btrfs_del_item(trans, root, path);
897 } else {
898 btrfs_set_ref_num_refs(leaf, ref, num_refs);
899 btrfs_mark_buffer_dirty(leaf);
900 }
901 btrfs_release_path(root, path);
902 return ret;
903}
904
Chris Mason4b4e25f2008-11-20 10:22:27 -0500905#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -0500906static void btrfs_issue_discard(struct block_device *bdev,
907 u64 start, u64 len)
908{
909#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
910 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
911#else
912 blkdev_issue_discard(bdev, start >> 9, len >> 9);
913#endif
914}
Chris Mason4b4e25f2008-11-20 10:22:27 -0500915#endif
Chris Mason15916de2008-11-19 21:17:22 -0500916
Josef Bacikf3465ca2008-11-12 14:19:50 -0500917static int noinline free_extents(struct btrfs_trans_handle *trans,
918 struct btrfs_root *extent_root,
919 struct list_head *del_list)
920{
921 struct btrfs_fs_info *info = extent_root->fs_info;
922 struct btrfs_path *path;
923 struct btrfs_key key, found_key;
924 struct extent_buffer *leaf;
925 struct list_head *cur;
926 struct pending_extent_op *op;
927 struct btrfs_extent_item *ei;
928 int ret, num_to_del, extent_slot = 0, found_extent = 0;
929 u32 refs;
930 u64 bytes_freed = 0;
931
932 path = btrfs_alloc_path();
933 if (!path)
934 return -ENOMEM;
935 path->reada = 1;
936
937search:
938 /* search for the backref for the current ref we want to delete */
939 cur = del_list->next;
940 op = list_entry(cur, struct pending_extent_op, list);
941 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
942 op->orig_parent,
943 extent_root->root_key.objectid,
944 op->orig_generation, op->level, 1);
945 if (ret) {
946 printk("Unable to find backref byte nr %Lu root %Lu gen %Lu "
947 "owner %u\n", op->bytenr,
948 extent_root->root_key.objectid, op->orig_generation,
949 op->level);
950 btrfs_print_leaf(extent_root, path->nodes[0]);
951 WARN_ON(1);
952 goto out;
953 }
954
955 extent_slot = path->slots[0];
956 num_to_del = 1;
957 found_extent = 0;
958
959 /*
960 * if we aren't the first item on the leaf we can move back one and see
961 * if our ref is right next to our extent item
962 */
963 if (likely(extent_slot)) {
964 extent_slot--;
965 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
966 extent_slot);
967 if (found_key.objectid == op->bytenr &&
968 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
969 found_key.offset == op->num_bytes) {
970 num_to_del++;
971 found_extent = 1;
972 }
973 }
974
975 /*
976 * if we didn't find the extent we need to delete the backref and then
977 * search for the extent item key so we can update its ref count
978 */
979 if (!found_extent) {
980 key.objectid = op->bytenr;
981 key.type = BTRFS_EXTENT_ITEM_KEY;
982 key.offset = op->num_bytes;
983
984 ret = remove_extent_backref(trans, extent_root, path);
985 BUG_ON(ret);
986 btrfs_release_path(extent_root, path);
987 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
988 BUG_ON(ret);
989 extent_slot = path->slots[0];
990 }
991
992 /* this is where we update the ref count for the extent */
993 leaf = path->nodes[0];
994 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
995 refs = btrfs_extent_refs(leaf, ei);
996 BUG_ON(refs == 0);
997 refs--;
998 btrfs_set_extent_refs(leaf, ei, refs);
999
1000 btrfs_mark_buffer_dirty(leaf);
1001
1002 /*
1003 * This extent needs deleting. The reason cur_slot is extent_slot +
1004 * num_to_del is because extent_slot points to the slot where the extent
1005 * is, and if the backref was not right next to the extent we will be
1006 * deleting at least 1 item, and will want to start searching at the
1007 * slot directly next to extent_slot. However if we did find the
1008 * backref next to the extent item them we will be deleting at least 2
1009 * items and will want to start searching directly after the ref slot
1010 */
1011 if (!refs) {
1012 struct list_head *pos, *n, *end;
1013 int cur_slot = extent_slot+num_to_del;
1014 u64 super_used;
1015 u64 root_used;
1016
1017 path->slots[0] = extent_slot;
1018 bytes_freed = op->num_bytes;
1019
Josef Bacike3e469f2008-11-17 21:11:49 -05001020 mutex_lock(&info->pinned_mutex);
1021 ret = pin_down_bytes(trans, extent_root, op->bytenr,
1022 op->num_bytes, op->level >=
1023 BTRFS_FIRST_FREE_OBJECTID);
1024 mutex_unlock(&info->pinned_mutex);
1025 BUG_ON(ret < 0);
1026 op->del = ret;
1027
Josef Bacikf3465ca2008-11-12 14:19:50 -05001028 /*
1029 * we need to see if we can delete multiple things at once, so
1030 * start looping through the list of extents we are wanting to
1031 * delete and see if their extent/backref's are right next to
1032 * eachother and the extents only have 1 ref
1033 */
1034 for (pos = cur->next; pos != del_list; pos = pos->next) {
1035 struct pending_extent_op *tmp;
1036
1037 tmp = list_entry(pos, struct pending_extent_op, list);
1038
1039 /* we only want to delete extent+ref at this stage */
1040 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1041 break;
1042
1043 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1044 if (found_key.objectid != tmp->bytenr ||
1045 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1046 found_key.offset != tmp->num_bytes)
1047 break;
1048
1049 /* check to make sure this extent only has one ref */
1050 ei = btrfs_item_ptr(leaf, cur_slot,
1051 struct btrfs_extent_item);
1052 if (btrfs_extent_refs(leaf, ei) != 1)
1053 break;
1054
1055 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1056 if (found_key.objectid != tmp->bytenr ||
1057 found_key.type != BTRFS_EXTENT_REF_KEY ||
1058 found_key.offset != tmp->orig_parent)
1059 break;
1060
1061 /*
1062 * the ref is right next to the extent, we can set the
1063 * ref count to 0 since we will delete them both now
1064 */
1065 btrfs_set_extent_refs(leaf, ei, 0);
1066
1067 /* pin down the bytes for this extent */
1068 mutex_lock(&info->pinned_mutex);
1069 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1070 tmp->num_bytes, tmp->level >=
1071 BTRFS_FIRST_FREE_OBJECTID);
1072 mutex_unlock(&info->pinned_mutex);
1073 BUG_ON(ret < 0);
1074
1075 /*
1076 * use the del field to tell if we need to go ahead and
1077 * free up the extent when we delete the item or not.
1078 */
1079 tmp->del = ret;
1080 bytes_freed += tmp->num_bytes;
1081
1082 num_to_del += 2;
1083 cur_slot += 2;
1084 }
1085 end = pos;
1086
1087 /* update the free space counters */
1088 spin_lock_irq(&info->delalloc_lock);
1089 super_used = btrfs_super_bytes_used(&info->super_copy);
1090 btrfs_set_super_bytes_used(&info->super_copy,
1091 super_used - bytes_freed);
1092 spin_unlock_irq(&info->delalloc_lock);
1093
1094 root_used = btrfs_root_used(&extent_root->root_item);
1095 btrfs_set_root_used(&extent_root->root_item,
1096 root_used - bytes_freed);
1097
1098 /* delete the items */
1099 ret = btrfs_del_items(trans, extent_root, path,
1100 path->slots[0], num_to_del);
1101 BUG_ON(ret);
1102
1103 /*
1104 * loop through the extents we deleted and do the cleanup work
1105 * on them
1106 */
1107 for (pos = cur, n = pos->next; pos != end;
1108 pos = n, n = pos->next) {
1109 struct pending_extent_op *tmp;
1110#ifdef BIO_RW_DISCARD
1111 u64 map_length;
1112 struct btrfs_multi_bio *multi = NULL;
1113#endif
1114 tmp = list_entry(pos, struct pending_extent_op, list);
1115
1116 /*
1117 * remember tmp->del tells us wether or not we pinned
1118 * down the extent
1119 */
1120 ret = update_block_group(trans, extent_root,
1121 tmp->bytenr, tmp->num_bytes, 0,
1122 tmp->del);
1123 BUG_ON(ret);
1124
1125#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -05001126 map_length = tmp->num_bytes;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001127 ret = btrfs_map_block(&info->mapping_tree, READ,
1128 tmp->bytenr, &map_length, &multi,
1129 0);
1130 if (!ret) {
1131 struct btrfs_bio_stripe *stripe;
1132 int i;
1133
Chris Mason15916de2008-11-19 21:17:22 -05001134 stripe = multi->stripes;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001135
1136 if (map_length > tmp->num_bytes)
1137 map_length = tmp->num_bytes;
1138
1139 for (i = 0; i < multi->num_stripes;
1140 i++, stripe++)
Chris Mason15916de2008-11-19 21:17:22 -05001141 btrfs_issue_discard(stripe->dev->bdev,
1142 stripe->physical,
1143 map_length);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001144 kfree(multi);
1145 }
1146#endif
1147 list_del_init(&tmp->list);
1148 unlock_extent(&info->extent_ins, tmp->bytenr,
1149 tmp->bytenr + tmp->num_bytes - 1,
1150 GFP_NOFS);
1151 kfree(tmp);
1152 }
1153 } else if (refs && found_extent) {
1154 /*
1155 * the ref and extent were right next to eachother, but the
1156 * extent still has a ref, so just free the backref and keep
1157 * going
1158 */
1159 ret = remove_extent_backref(trans, extent_root, path);
1160 BUG_ON(ret);
1161
1162 list_del_init(&op->list);
1163 unlock_extent(&info->extent_ins, op->bytenr,
1164 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1165 kfree(op);
1166 } else {
1167 /*
1168 * the extent has multiple refs and the backref we were looking
1169 * for was not right next to it, so just unlock and go next,
1170 * we're good to go
1171 */
1172 list_del_init(&op->list);
1173 unlock_extent(&info->extent_ins, op->bytenr,
1174 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1175 kfree(op);
1176 }
1177
1178 btrfs_release_path(extent_root, path);
1179 if (!list_empty(del_list))
1180 goto search;
1181
1182out:
1183 btrfs_free_path(path);
1184 return ret;
1185}
1186
Zheng Yan31840ae2008-09-23 13:14:14 -04001187static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1188 struct btrfs_root *root, u64 bytenr,
1189 u64 orig_parent, u64 parent,
1190 u64 orig_root, u64 ref_root,
1191 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001192 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001193{
1194 int ret;
1195 struct btrfs_root *extent_root = root->fs_info->extent_root;
1196 struct btrfs_path *path;
1197
1198 if (root == root->fs_info->extent_root) {
1199 struct pending_extent_op *extent_op;
1200 u64 num_bytes;
1201
1202 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1203 num_bytes = btrfs_level_size(root, (int)owner_objectid);
Josef Bacik25179202008-10-29 14:49:05 -04001204 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001205 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
Josef Bacik25179202008-10-29 14:49:05 -04001206 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001207 u64 priv;
1208 ret = get_state_private(&root->fs_info->extent_ins,
1209 bytenr, &priv);
1210 BUG_ON(ret);
1211 extent_op = (struct pending_extent_op *)
1212 (unsigned long)priv;
1213 BUG_ON(extent_op->parent != orig_parent);
1214 BUG_ON(extent_op->generation != orig_generation);
Josef Bacik25179202008-10-29 14:49:05 -04001215
Zheng Yan31840ae2008-09-23 13:14:14 -04001216 extent_op->parent = parent;
1217 extent_op->generation = ref_generation;
1218 } else {
1219 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1220 BUG_ON(!extent_op);
1221
1222 extent_op->type = PENDING_BACKREF_UPDATE;
1223 extent_op->bytenr = bytenr;
1224 extent_op->num_bytes = num_bytes;
1225 extent_op->parent = parent;
1226 extent_op->orig_parent = orig_parent;
1227 extent_op->generation = ref_generation;
1228 extent_op->orig_generation = orig_generation;
1229 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001230 INIT_LIST_HEAD(&extent_op->list);
1231 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001232
1233 set_extent_bits(&root->fs_info->extent_ins,
1234 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04001235 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04001236 set_state_private(&root->fs_info->extent_ins,
1237 bytenr, (unsigned long)extent_op);
1238 }
Josef Bacik25179202008-10-29 14:49:05 -04001239 mutex_unlock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001240 return 0;
1241 }
1242
1243 path = btrfs_alloc_path();
1244 if (!path)
1245 return -ENOMEM;
1246 ret = lookup_extent_backref(trans, extent_root, path,
1247 bytenr, orig_parent, orig_root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001248 orig_generation, owner_objectid, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001249 if (ret)
1250 goto out;
1251 ret = remove_extent_backref(trans, extent_root, path);
1252 if (ret)
1253 goto out;
1254 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1255 parent, ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001256 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001257 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001258 finish_current_insert(trans, extent_root, 0);
1259 del_pending_extents(trans, extent_root, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001260out:
1261 btrfs_free_path(path);
1262 return ret;
1263}
1264
1265int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1266 struct btrfs_root *root, u64 bytenr,
1267 u64 orig_parent, u64 parent,
1268 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001269 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001270{
1271 int ret;
1272 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1273 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1274 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001275 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1276 parent, ref_root, ref_root,
1277 ref_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001278 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001279 return ret;
1280}
1281
Chris Mason925baed2008-06-25 16:01:30 -04001282static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001283 struct btrfs_root *root, u64 bytenr,
1284 u64 orig_parent, u64 parent,
1285 u64 orig_root, u64 ref_root,
1286 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001287 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -05001288{
Chris Mason5caf2a02007-04-02 11:20:42 -04001289 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -05001290 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001291 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001292 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001293 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001294 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001295
Chris Mason5caf2a02007-04-02 11:20:42 -04001296 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001297 if (!path)
1298 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001299
Chris Mason3c12ac72008-04-21 12:01:38 -04001300 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001301 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001302 key.type = BTRFS_EXTENT_ITEM_KEY;
1303 key.offset = (u64)-1;
1304
Chris Mason5caf2a02007-04-02 11:20:42 -04001305 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001306 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001307 if (ret < 0)
1308 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001309 BUG_ON(ret == 0 || path->slots[0] == 0);
1310
1311 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -04001312 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -04001313
1314 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Mason771ed682008-11-06 22:02:51 -05001315 if (key.objectid != bytenr) {
1316 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
1317 printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
1318 BUG();
1319 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001320 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1321
Chris Mason5caf2a02007-04-02 11:20:42 -04001322 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001323 refs = btrfs_extent_refs(l, item);
1324 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001325 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -05001326
Chris Mason5caf2a02007-04-02 11:20:42 -04001327 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001328
Chris Mason3c12ac72008-04-21 12:01:38 -04001329 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001330 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1331 path, bytenr, parent,
1332 ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001333 owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05001334 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001335 finish_current_insert(trans, root->fs_info->extent_root, 0);
1336 del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Mason74493f72007-12-11 09:25:06 -05001337
1338 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -05001339 return 0;
1340}
1341
Chris Mason925baed2008-06-25 16:01:30 -04001342int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001343 struct btrfs_root *root,
1344 u64 bytenr, u64 num_bytes, u64 parent,
1345 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001346 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -04001347{
1348 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001349 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1350 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1351 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001352 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1353 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001354 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -04001355 return ret;
1356}
1357
Chris Masone9d0b132007-08-10 14:06:19 -04001358int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1359 struct btrfs_root *root)
1360{
Chris Mason87ef2bb2008-10-30 11:23:27 -04001361 finish_current_insert(trans, root->fs_info->extent_root, 1);
1362 del_pending_extents(trans, root->fs_info->extent_root, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001363 return 0;
1364}
1365
Zheng Yan31840ae2008-09-23 13:14:14 -04001366int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1367 struct btrfs_root *root, u64 bytenr,
1368 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -05001369{
Chris Mason5caf2a02007-04-02 11:20:42 -04001370 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -05001371 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001372 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001373 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001374 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -04001375
Chris Masondb945352007-10-15 16:15:53 -04001376 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -04001377 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -04001378 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001379 key.objectid = bytenr;
1380 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -04001381 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -04001382 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001383 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001384 if (ret < 0)
1385 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -04001386 if (ret != 0) {
1387 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -04001388 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -05001389 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04001390 }
1391 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001392 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001393 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001394out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001395 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05001396 return 0;
1397}
1398
Yan Zheng80ff3852008-10-30 14:20:02 -04001399int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1400 struct btrfs_root *root, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05001401{
1402 struct btrfs_root *extent_root = root->fs_info->extent_root;
1403 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -04001404 struct extent_buffer *leaf;
1405 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -05001406 struct btrfs_key key;
1407 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001408 u64 ref_root;
1409 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -04001410 u32 nritems;
1411 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001412
Chris Masonbe20aa92007-12-17 20:14:01 -05001413 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001414 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04001415 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05001416
Yan Zhengf321e492008-07-30 09:26:11 -04001417 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -05001418 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1419 if (ret < 0)
1420 goto out;
1421 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001422
1423 ret = -ENOENT;
1424 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04001425 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001426
Zheng Yan31840ae2008-09-23 13:14:14 -04001427 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04001428 leaf = path->nodes[0];
1429 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001430
1431 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001432 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05001433 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001434
Yan Zheng80ff3852008-10-30 14:20:02 -04001435 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05001436 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -04001437 leaf = path->nodes[0];
1438 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -05001439 if (path->slots[0] >= nritems) {
1440 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -04001441 if (ret < 0)
1442 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001443 if (ret == 0)
1444 continue;
1445 break;
1446 }
Yan Zhengf321e492008-07-30 09:26:11 -04001447 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001448 if (found_key.objectid != bytenr)
1449 break;
Chris Masonbd098352008-01-03 13:23:19 -05001450
Chris Masonbe20aa92007-12-17 20:14:01 -05001451 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1452 path->slots[0]++;
1453 continue;
1454 }
1455
Yan Zhengf321e492008-07-30 09:26:11 -04001456 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001457 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -04001458 ref_root = btrfs_ref_root(leaf, ref_item);
1459 if (ref_root != root->root_key.objectid &&
1460 ref_root != BTRFS_TREE_LOG_OBJECTID) {
1461 ret = 1;
1462 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -04001463 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001464 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1465 ret = 1;
1466 goto out;
1467 }
Yan Zhengf321e492008-07-30 09:26:11 -04001468
Chris Masonbe20aa92007-12-17 20:14:01 -05001469 path->slots[0]++;
1470 }
Yan Zhengf321e492008-07-30 09:26:11 -04001471 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001472out:
Yan Zhengf321e492008-07-30 09:26:11 -04001473 btrfs_free_path(path);
1474 return ret;
1475}
1476
Zheng Yan31840ae2008-09-23 13:14:14 -04001477int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1478 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001479{
Chris Mason5f39d392007-10-15 16:14:19 -04001480 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001481 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001482 u64 root_gen;
1483 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001484 int i;
Chris Masondb945352007-10-15 16:15:53 -04001485 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001486 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001487 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001488
Chris Mason3768f362007-03-13 16:47:54 -04001489 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001490 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001491
Zheng Yane4657682008-09-26 10:04:53 -04001492 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1493 shared = 0;
1494 root_gen = root->root_key.offset;
1495 } else {
1496 shared = 1;
1497 root_gen = trans->transid - 1;
1498 }
1499
Chris Masondb945352007-10-15 16:15:53 -04001500 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001501 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001502
Zheng Yan31840ae2008-09-23 13:14:14 -04001503 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001504 struct btrfs_leaf_ref *ref;
1505 struct btrfs_extent_info *info;
1506
Zheng Yan31840ae2008-09-23 13:14:14 -04001507 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001508 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001509 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001510 goto out;
1511 }
1512
Zheng Yane4657682008-09-26 10:04:53 -04001513 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001514 ref->bytenr = buf->start;
1515 ref->owner = btrfs_header_owner(buf);
1516 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001517 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001518 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001519
Zheng Yan31840ae2008-09-23 13:14:14 -04001520 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001521 u64 disk_bytenr;
1522 btrfs_item_key_to_cpu(buf, &key, i);
1523 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1524 continue;
1525 fi = btrfs_item_ptr(buf, i,
1526 struct btrfs_file_extent_item);
1527 if (btrfs_file_extent_type(buf, fi) ==
1528 BTRFS_FILE_EXTENT_INLINE)
1529 continue;
1530 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1531 if (disk_bytenr == 0)
1532 continue;
1533
1534 info->bytenr = disk_bytenr;
1535 info->num_bytes =
1536 btrfs_file_extent_disk_num_bytes(buf, fi);
1537 info->objectid = key.objectid;
1538 info->offset = key.offset;
1539 info++;
1540 }
1541
Zheng Yane4657682008-09-26 10:04:53 -04001542 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001543 if (ret == -EEXIST && shared) {
1544 struct btrfs_leaf_ref *old;
1545 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1546 BUG_ON(!old);
1547 btrfs_remove_leaf_ref(root, old);
1548 btrfs_free_leaf_ref(root, old);
1549 ret = btrfs_add_leaf_ref(root, ref, shared);
1550 }
Yan Zheng31153d82008-07-28 15:32:19 -04001551 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001552 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001553 }
1554out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001555 return ret;
1556}
1557
1558int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1559 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1560 u32 *nr_extents)
1561{
1562 u64 bytenr;
1563 u64 ref_root;
1564 u64 orig_root;
1565 u64 ref_generation;
1566 u64 orig_generation;
1567 u32 nritems;
1568 u32 nr_file_extents = 0;
1569 struct btrfs_key key;
1570 struct btrfs_file_extent_item *fi;
1571 int i;
1572 int level;
1573 int ret = 0;
1574 int faili = 0;
1575 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001576 u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001577
1578 ref_root = btrfs_header_owner(buf);
1579 ref_generation = btrfs_header_generation(buf);
1580 orig_root = btrfs_header_owner(orig_buf);
1581 orig_generation = btrfs_header_generation(orig_buf);
1582
1583 nritems = btrfs_header_nritems(buf);
1584 level = btrfs_header_level(buf);
1585
1586 if (root->ref_cows) {
1587 process_func = __btrfs_inc_extent_ref;
1588 } else {
1589 if (level == 0 &&
1590 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1591 goto out;
1592 if (level != 0 &&
1593 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1594 goto out;
1595 process_func = __btrfs_update_extent_ref;
1596 }
1597
1598 for (i = 0; i < nritems; i++) {
1599 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001600 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001601 btrfs_item_key_to_cpu(buf, &key, i);
1602 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001603 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001604 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001605 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001606 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001607 BTRFS_FILE_EXTENT_INLINE)
1608 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001609 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1610 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001611 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001612
1613 nr_file_extents++;
1614
Zheng Yan31840ae2008-09-23 13:14:14 -04001615 ret = process_func(trans, root, bytenr,
1616 orig_buf->start, buf->start,
1617 orig_root, ref_root,
1618 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001619 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001620
1621 if (ret) {
1622 faili = i;
1623 WARN_ON(1);
1624 goto fail;
1625 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001626 } else {
Chris Masondb945352007-10-15 16:15:53 -04001627 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001628 ret = process_func(trans, root, bytenr,
1629 orig_buf->start, buf->start,
1630 orig_root, ref_root,
1631 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001632 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001633 if (ret) {
1634 faili = i;
1635 WARN_ON(1);
1636 goto fail;
1637 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001638 }
1639 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001640out:
1641 if (nr_extents) {
1642 if (level == 0)
1643 *nr_extents = nr_file_extents;
1644 else
1645 *nr_extents = nritems;
1646 }
1647 return 0;
1648fail:
1649 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001650 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001651}
1652
Zheng Yan31840ae2008-09-23 13:14:14 -04001653int btrfs_update_ref(struct btrfs_trans_handle *trans,
1654 struct btrfs_root *root, struct extent_buffer *orig_buf,
1655 struct extent_buffer *buf, int start_slot, int nr)
1656
1657{
1658 u64 bytenr;
1659 u64 ref_root;
1660 u64 orig_root;
1661 u64 ref_generation;
1662 u64 orig_generation;
1663 struct btrfs_key key;
1664 struct btrfs_file_extent_item *fi;
1665 int i;
1666 int ret;
1667 int slot;
1668 int level;
1669
1670 BUG_ON(start_slot < 0);
1671 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1672
1673 ref_root = btrfs_header_owner(buf);
1674 ref_generation = btrfs_header_generation(buf);
1675 orig_root = btrfs_header_owner(orig_buf);
1676 orig_generation = btrfs_header_generation(orig_buf);
1677 level = btrfs_header_level(buf);
1678
1679 if (!root->ref_cows) {
1680 if (level == 0 &&
1681 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1682 return 0;
1683 if (level != 0 &&
1684 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1685 return 0;
1686 }
1687
1688 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1689 cond_resched();
1690 if (level == 0) {
1691 btrfs_item_key_to_cpu(buf, &key, slot);
1692 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1693 continue;
1694 fi = btrfs_item_ptr(buf, slot,
1695 struct btrfs_file_extent_item);
1696 if (btrfs_file_extent_type(buf, fi) ==
1697 BTRFS_FILE_EXTENT_INLINE)
1698 continue;
1699 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1700 if (bytenr == 0)
1701 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001702 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1703 orig_buf->start, buf->start,
1704 orig_root, ref_root,
1705 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001706 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001707 if (ret)
1708 goto fail;
1709 } else {
1710 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001711 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1712 orig_buf->start, buf->start,
1713 orig_root, ref_root,
1714 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001715 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001716 if (ret)
1717 goto fail;
1718 }
1719 }
1720 return 0;
1721fail:
1722 WARN_ON(1);
1723 return -1;
1724}
1725
Chris Mason9078a3e2007-04-26 16:46:15 -04001726static int write_one_cache_group(struct btrfs_trans_handle *trans,
1727 struct btrfs_root *root,
1728 struct btrfs_path *path,
1729 struct btrfs_block_group_cache *cache)
1730{
1731 int ret;
1732 int pending_ret;
1733 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001734 unsigned long bi;
1735 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001736
Chris Mason9078a3e2007-04-26 16:46:15 -04001737 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001738 if (ret < 0)
1739 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001740 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001741
1742 leaf = path->nodes[0];
1743 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1744 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1745 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001746 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001747fail:
Chris Mason87ef2bb2008-10-30 11:23:27 -04001748 finish_current_insert(trans, extent_root, 0);
1749 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001750 if (ret)
1751 return ret;
1752 if (pending_ret)
1753 return pending_ret;
1754 return 0;
1755
1756}
1757
Chris Mason96b51792007-10-15 16:15:19 -04001758int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1759 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001760{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001761 struct btrfs_block_group_cache *cache, *entry;
1762 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001763 int err = 0;
1764 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001765 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001766 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001767
1768 path = btrfs_alloc_path();
1769 if (!path)
1770 return -ENOMEM;
1771
1772 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001773 cache = NULL;
1774 spin_lock(&root->fs_info->block_group_cache_lock);
1775 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1776 n; n = rb_next(n)) {
1777 entry = rb_entry(n, struct btrfs_block_group_cache,
1778 cache_node);
1779 if (entry->dirty) {
1780 cache = entry;
1781 break;
1782 }
1783 }
1784 spin_unlock(&root->fs_info->block_group_cache_lock);
1785
1786 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001787 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001788
Zheng Yane8569812008-09-26 10:05:48 -04001789 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001790 last += cache->key.offset;
1791
Chris Mason96b51792007-10-15 16:15:19 -04001792 err = write_one_cache_group(trans, root,
1793 path, cache);
1794 /*
1795 * if we fail to write the cache group, we want
1796 * to keep it marked dirty in hopes that a later
1797 * write will work
1798 */
1799 if (err) {
1800 werr = err;
1801 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001802 }
1803 }
1804 btrfs_free_path(path);
1805 return werr;
1806}
1807
Chris Mason593060d2008-03-25 16:50:33 -04001808static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1809 u64 total_bytes, u64 bytes_used,
1810 struct btrfs_space_info **space_info)
1811{
1812 struct btrfs_space_info *found;
1813
1814 found = __find_space_info(info, flags);
1815 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001816 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001817 found->total_bytes += total_bytes;
1818 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001819 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001820 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001821 *space_info = found;
1822 return 0;
1823 }
Yan Zhengc146afa2008-11-12 14:34:12 -05001824 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04001825 if (!found)
1826 return -ENOMEM;
1827
1828 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001829 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001830 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001831 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001832 found->flags = flags;
1833 found->total_bytes = total_bytes;
1834 found->bytes_used = bytes_used;
1835 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001836 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05001837 found->bytes_readonly = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001838 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001839 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001840 *space_info = found;
1841 return 0;
1842}
1843
Chris Mason8790d502008-04-03 16:29:03 -04001844static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1845{
1846 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001847 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001848 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001849 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001850 if (extra_flags) {
1851 if (flags & BTRFS_BLOCK_GROUP_DATA)
1852 fs_info->avail_data_alloc_bits |= extra_flags;
1853 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1854 fs_info->avail_metadata_alloc_bits |= extra_flags;
1855 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1856 fs_info->avail_system_alloc_bits |= extra_flags;
1857 }
1858}
Chris Mason593060d2008-03-25 16:50:33 -04001859
Yan Zhengc146afa2008-11-12 14:34:12 -05001860static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1861{
1862 spin_lock(&cache->space_info->lock);
1863 spin_lock(&cache->lock);
1864 if (!cache->ro) {
1865 cache->space_info->bytes_readonly += cache->key.offset -
1866 btrfs_block_group_used(&cache->item);
1867 cache->ro = 1;
1868 }
1869 spin_unlock(&cache->lock);
1870 spin_unlock(&cache->space_info->lock);
1871}
1872
Yan Zheng2b820322008-11-17 21:11:30 -05001873u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001874{
Yan Zheng2b820322008-11-17 21:11:30 -05001875 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001876
1877 if (num_devices == 1)
1878 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1879 if (num_devices < 4)
1880 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1881
Chris Masonec44a352008-04-28 15:29:52 -04001882 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1883 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001884 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001885 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001886 }
Chris Masonec44a352008-04-28 15:29:52 -04001887
1888 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001889 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001890 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001891 }
Chris Masonec44a352008-04-28 15:29:52 -04001892
1893 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1894 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1895 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1896 (flags & BTRFS_BLOCK_GROUP_DUP)))
1897 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1898 return flags;
1899}
1900
Chris Mason6324fbf2008-03-24 15:01:59 -04001901static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1902 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001903 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001904{
1905 struct btrfs_space_info *space_info;
1906 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05001907 int ret = 0;
1908
1909 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001910
Yan Zheng2b820322008-11-17 21:11:30 -05001911 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001912
Chris Mason6324fbf2008-03-24 15:01:59 -04001913 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001914 if (!space_info) {
1915 ret = update_space_info(extent_root->fs_info, flags,
1916 0, 0, &space_info);
1917 BUG_ON(ret);
1918 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001919 BUG_ON(!space_info);
1920
Josef Bacik25179202008-10-29 14:49:05 -04001921 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001922 if (space_info->force_alloc) {
1923 force = 1;
1924 space_info->force_alloc = 0;
1925 }
Josef Bacik25179202008-10-29 14:49:05 -04001926 if (space_info->full) {
1927 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001928 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001929 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001930
Yan Zhengc146afa2008-11-12 14:34:12 -05001931 thresh = space_info->total_bytes - space_info->bytes_readonly;
1932 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001933 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001934 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001935 space_info->bytes_reserved + alloc_bytes) < thresh) {
1936 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001937 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001938 }
Josef Bacik25179202008-10-29 14:49:05 -04001939 spin_unlock(&space_info->lock);
1940
Yan Zheng2b820322008-11-17 21:11:30 -05001941 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik25179202008-10-29 14:49:05 -04001942 if (ret) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001943printk("space info full %Lu\n", flags);
1944 space_info->full = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001945 }
Chris Masona74a4b92008-06-25 16:01:31 -04001946out:
Yan Zhengc146afa2008-11-12 14:34:12 -05001947 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001948 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001949}
1950
Chris Mason9078a3e2007-04-26 16:46:15 -04001951static int update_block_group(struct btrfs_trans_handle *trans,
1952 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001953 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001954 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001955{
1956 struct btrfs_block_group_cache *cache;
1957 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001958 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001959 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001960 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001961
Chris Mason9078a3e2007-04-26 16:46:15 -04001962 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001963 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001964 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001965 return -1;
Chris Masondb945352007-10-15 16:15:53 -04001966 byte_in_group = bytenr - cache->key.objectid;
1967 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001968
Josef Bacik25179202008-10-29 14:49:05 -04001969 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001970 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001971 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001972 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001973 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001974 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001975 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001976 cache->space_info->bytes_used += num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001977 if (cache->ro) {
1978 cache->space_info->bytes_readonly -= num_bytes;
1979 WARN_ON(1);
1980 }
Chris Masonc286ac42008-07-22 23:06:41 -04001981 btrfs_set_block_group_used(&cache->item, old_val);
1982 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001983 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001984 } else {
Chris Masondb945352007-10-15 16:15:53 -04001985 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001986 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001987 if (cache->ro)
1988 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001989 btrfs_set_block_group_used(&cache->item, old_val);
1990 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001991 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001992 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001993 int ret;
1994 ret = btrfs_add_free_space(cache, bytenr,
1995 num_bytes);
1996 if (ret)
1997 return -1;
Chris Masone37c9e62007-05-09 20:13:14 -04001998 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001999 }
Chris Masondb945352007-10-15 16:15:53 -04002000 total -= num_bytes;
2001 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04002002 }
2003 return 0;
2004}
Chris Mason6324fbf2008-03-24 15:01:59 -04002005
Chris Masona061fc82008-05-07 11:43:44 -04002006static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2007{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002008 struct btrfs_block_group_cache *cache;
2009
2010 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2011 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002012 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002013
2014 return cache->key.objectid;
Chris Masona061fc82008-05-07 11:43:44 -04002015}
2016
Chris Masone02119d2008-09-05 16:13:11 -04002017int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05002018 u64 bytenr, u64 num, int pin)
2019{
2020 u64 len;
2021 struct btrfs_block_group_cache *cache;
2022 struct btrfs_fs_info *fs_info = root->fs_info;
2023
Josef Bacik25179202008-10-29 14:49:05 -04002024 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05002025 if (pin) {
2026 set_extent_dirty(&fs_info->pinned_extents,
2027 bytenr, bytenr + num - 1, GFP_NOFS);
2028 } else {
2029 clear_extent_dirty(&fs_info->pinned_extents,
2030 bytenr, bytenr + num - 1, GFP_NOFS);
2031 }
2032 while (num > 0) {
2033 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002034 BUG_ON(!cache);
2035 len = min(num, cache->key.offset -
2036 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002037 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002038 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002039 spin_lock(&cache->lock);
2040 cache->pinned += len;
2041 cache->space_info->bytes_pinned += len;
2042 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002043 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002044 fs_info->total_pinned += len;
2045 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002046 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002047 spin_lock(&cache->lock);
2048 cache->pinned -= len;
2049 cache->space_info->bytes_pinned -= len;
2050 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002051 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002052 fs_info->total_pinned -= len;
Josef Bacik07103a32008-11-19 15:17:55 -05002053 if (cache->cached)
2054 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05002055 }
2056 bytenr += len;
2057 num -= len;
2058 }
2059 return 0;
2060}
Chris Mason9078a3e2007-04-26 16:46:15 -04002061
Zheng Yane8569812008-09-26 10:05:48 -04002062static int update_reserved_extents(struct btrfs_root *root,
2063 u64 bytenr, u64 num, int reserve)
2064{
2065 u64 len;
2066 struct btrfs_block_group_cache *cache;
2067 struct btrfs_fs_info *fs_info = root->fs_info;
2068
Zheng Yane8569812008-09-26 10:05:48 -04002069 while (num > 0) {
2070 cache = btrfs_lookup_block_group(fs_info, bytenr);
2071 BUG_ON(!cache);
2072 len = min(num, cache->key.offset -
2073 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04002074
2075 spin_lock(&cache->space_info->lock);
2076 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002077 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04002078 cache->reserved += len;
2079 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04002080 } else {
Zheng Yane8569812008-09-26 10:05:48 -04002081 cache->reserved -= len;
2082 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04002083 }
Josef Bacik25179202008-10-29 14:49:05 -04002084 spin_unlock(&cache->lock);
2085 spin_unlock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002086 bytenr += len;
2087 num -= len;
2088 }
2089 return 0;
2090}
2091
Chris Masond1310b22008-01-24 16:13:08 -05002092int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04002093{
Chris Masonccd467d2007-06-28 15:57:36 -04002094 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002095 u64 start;
2096 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002097 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04002098 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04002099
Josef Bacik25179202008-10-29 14:49:05 -04002100 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002101 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002102 ret = find_first_extent_bit(pinned_extents, last,
2103 &start, &end, EXTENT_DIRTY);
2104 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04002105 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04002106 set_extent_dirty(copy, start, end, GFP_NOFS);
2107 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04002108 }
Josef Bacik25179202008-10-29 14:49:05 -04002109 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002110 return 0;
2111}
2112
2113int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2114 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05002115 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05002116{
Chris Mason1a5bc162007-10-15 16:15:26 -04002117 u64 start;
2118 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002119 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002120
Josef Bacik25179202008-10-29 14:49:05 -04002121 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002122 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002123 ret = find_first_extent_bit(unpin, 0, &start, &end,
2124 EXTENT_DIRTY);
2125 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05002126 break;
Chris Masone02119d2008-09-05 16:13:11 -04002127 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04002128 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04002129 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04002130 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002131 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04002132 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002133 }
Chris Masona28ec192007-03-06 20:08:01 -05002134 }
Josef Bacik25179202008-10-29 14:49:05 -04002135 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002136 return 0;
2137}
2138
Chris Mason98ed5172008-01-03 10:01:48 -05002139static int finish_current_insert(struct btrfs_trans_handle *trans,
Chris Mason87ef2bb2008-10-30 11:23:27 -04002140 struct btrfs_root *extent_root, int all)
Chris Mason037e6392007-03-07 11:50:24 -05002141{
Chris Mason7bb86312007-12-11 09:25:06 -05002142 u64 start;
2143 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002144 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002145 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002146 u64 skipped = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002147 struct btrfs_fs_info *info = extent_root->fs_info;
2148 struct btrfs_path *path;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002149 struct pending_extent_op *extent_op, *tmp;
2150 struct list_head insert_list, update_list;
Chris Mason037e6392007-03-07 11:50:24 -05002151 int ret;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002152 int num_inserts = 0, max_inserts;
Chris Mason037e6392007-03-07 11:50:24 -05002153
Chris Mason7bb86312007-12-11 09:25:06 -05002154 path = btrfs_alloc_path();
Josef Bacikf3465ca2008-11-12 14:19:50 -05002155 INIT_LIST_HEAD(&insert_list);
2156 INIT_LIST_HEAD(&update_list);
Chris Mason037e6392007-03-07 11:50:24 -05002157
Josef Bacikf3465ca2008-11-12 14:19:50 -05002158 max_inserts = extent_root->leafsize /
2159 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2160 sizeof(struct btrfs_extent_ref) +
2161 sizeof(struct btrfs_extent_item));
2162again:
2163 mutex_lock(&info->extent_ins_mutex);
2164 while (1) {
Josef Bacik25179202008-10-29 14:49:05 -04002165 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2166 &end, EXTENT_WRITEBACK);
2167 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002168 if (skipped && all && !num_inserts) {
2169 skipped = 0;
Liu Huib4eec2c2008-11-18 11:30:10 -05002170 search = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002171 continue;
2172 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002173 mutex_unlock(&info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04002174 break;
Josef Bacik25179202008-10-29 14:49:05 -04002175 }
2176
2177 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2178 if (!ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002179 skipped = 1;
Chris Mason87ef2bb2008-10-30 11:23:27 -04002180 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002181 if (need_resched()) {
2182 mutex_unlock(&info->extent_ins_mutex);
2183 cond_resched();
2184 mutex_lock(&info->extent_ins_mutex);
2185 }
Josef Bacik25179202008-10-29 14:49:05 -04002186 continue;
2187 }
Chris Mason26b80032007-08-08 20:17:12 -04002188
Zheng Yan31840ae2008-09-23 13:14:14 -04002189 ret = get_state_private(&info->extent_ins, start, &priv);
2190 BUG_ON(ret);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002191 extent_op = (struct pending_extent_op *)(unsigned long) priv;
Josef Bacik25179202008-10-29 14:49:05 -04002192
Zheng Yan31840ae2008-09-23 13:14:14 -04002193 if (extent_op->type == PENDING_EXTENT_INSERT) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002194 num_inserts++;
2195 list_add_tail(&extent_op->list, &insert_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002196 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002197 if (num_inserts == max_inserts) {
2198 mutex_unlock(&info->extent_ins_mutex);
2199 break;
2200 }
2201 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2202 list_add_tail(&extent_op->list, &update_list);
2203 search = end + 1;
2204 } else {
2205 BUG();
2206 }
Chris Mason037e6392007-03-07 11:50:24 -05002207 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002208
2209 /*
Liu Huib4eec2c2008-11-18 11:30:10 -05002210 * process the update list, clear the writeback bit for it, and if
Josef Bacikf3465ca2008-11-12 14:19:50 -05002211 * somebody marked this thing for deletion then just unlock it and be
2212 * done, the free_extents will handle it
2213 */
2214 mutex_lock(&info->extent_ins_mutex);
2215 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2216 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2217 extent_op->bytenr + extent_op->num_bytes - 1,
2218 EXTENT_WRITEBACK, GFP_NOFS);
2219 if (extent_op->del) {
2220 list_del_init(&extent_op->list);
2221 unlock_extent(&info->extent_ins, extent_op->bytenr,
2222 extent_op->bytenr + extent_op->num_bytes
2223 - 1, GFP_NOFS);
2224 kfree(extent_op);
2225 }
2226 }
2227 mutex_unlock(&info->extent_ins_mutex);
2228
2229 /*
2230 * still have things left on the update list, go ahead an update
2231 * everything
2232 */
2233 if (!list_empty(&update_list)) {
2234 ret = update_backrefs(trans, extent_root, path, &update_list);
2235 BUG_ON(ret);
2236 }
2237
2238 /*
2239 * if no inserts need to be done, but we skipped some extents and we
2240 * need to make sure everything is cleaned then reset everything and
2241 * go back to the beginning
2242 */
2243 if (!num_inserts && all && skipped) {
2244 search = 0;
2245 skipped = 0;
2246 INIT_LIST_HEAD(&update_list);
2247 INIT_LIST_HEAD(&insert_list);
2248 goto again;
2249 } else if (!num_inserts) {
2250 goto out;
2251 }
2252
2253 /*
2254 * process the insert extents list. Again if we are deleting this
2255 * extent, then just unlock it, pin down the bytes if need be, and be
2256 * done with it. Saves us from having to actually insert the extent
2257 * into the tree and then subsequently come along and delete it
2258 */
2259 mutex_lock(&info->extent_ins_mutex);
2260 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2261 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2262 extent_op->bytenr + extent_op->num_bytes - 1,
2263 EXTENT_WRITEBACK, GFP_NOFS);
2264 if (extent_op->del) {
2265 list_del_init(&extent_op->list);
2266 unlock_extent(&info->extent_ins, extent_op->bytenr,
2267 extent_op->bytenr + extent_op->num_bytes
2268 - 1, GFP_NOFS);
2269
2270 mutex_lock(&extent_root->fs_info->pinned_mutex);
2271 ret = pin_down_bytes(trans, extent_root,
2272 extent_op->bytenr,
2273 extent_op->num_bytes, 0);
2274 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2275
2276 ret = update_block_group(trans, extent_root,
2277 extent_op->bytenr,
2278 extent_op->num_bytes,
2279 0, ret > 0);
2280 BUG_ON(ret);
2281 kfree(extent_op);
2282 num_inserts--;
2283 }
2284 }
2285 mutex_unlock(&info->extent_ins_mutex);
2286
2287 ret = insert_extents(trans, extent_root, path, &insert_list,
2288 num_inserts);
2289 BUG_ON(ret);
2290
2291 /*
2292 * if we broke out of the loop in order to insert stuff because we hit
2293 * the maximum number of inserts at a time we can handle, then loop
2294 * back and pick up where we left off
2295 */
2296 if (num_inserts == max_inserts) {
2297 INIT_LIST_HEAD(&insert_list);
2298 INIT_LIST_HEAD(&update_list);
2299 num_inserts = 0;
2300 goto again;
2301 }
2302
2303 /*
2304 * again, if we need to make absolutely sure there are no more pending
2305 * extent operations left and we know that we skipped some, go back to
2306 * the beginning and do it all again
2307 */
2308 if (all && skipped) {
2309 INIT_LIST_HEAD(&insert_list);
2310 INIT_LIST_HEAD(&update_list);
2311 search = 0;
2312 skipped = 0;
2313 num_inserts = 0;
2314 goto again;
2315 }
2316out:
Chris Mason7bb86312007-12-11 09:25:06 -05002317 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05002318 return 0;
2319}
2320
Zheng Yan31840ae2008-09-23 13:14:14 -04002321static int pin_down_bytes(struct btrfs_trans_handle *trans,
2322 struct btrfs_root *root,
2323 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04002324{
Chris Mason1a5bc162007-10-15 16:15:26 -04002325 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002326 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04002327
Zheng Yan31840ae2008-09-23 13:14:14 -04002328 if (is_data)
2329 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002330
Zheng Yan31840ae2008-09-23 13:14:14 -04002331 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2332 if (!buf)
2333 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002334
Zheng Yan31840ae2008-09-23 13:14:14 -04002335 /* we can reuse a block if it hasn't been written
2336 * and it is from this transaction. We can't
2337 * reuse anything from the tree log root because
2338 * it has tiny sub-transactions.
2339 */
2340 if (btrfs_buffer_uptodate(buf, 0) &&
2341 btrfs_try_tree_lock(buf)) {
2342 u64 header_owner = btrfs_header_owner(buf);
2343 u64 header_transid = btrfs_header_generation(buf);
2344 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04002345 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04002346 header_transid == trans->transid &&
2347 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2348 clean_tree_block(NULL, root, buf);
2349 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002350 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002351 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04002352 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002353 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04002354 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002355 free_extent_buffer(buf);
2356pinit:
2357 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2358
Chris Masonbe744172007-05-06 10:15:01 -04002359 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002360 return 0;
2361}
2362
Chris Masona28ec192007-03-06 20:08:01 -05002363/*
2364 * remove an extent from the root, returns 0 on success
2365 */
Zheng Yan31840ae2008-09-23 13:14:14 -04002366static int __free_extent(struct btrfs_trans_handle *trans,
2367 struct btrfs_root *root,
2368 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002369 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002370 u64 owner_objectid, int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05002371{
Chris Mason5caf2a02007-04-02 11:20:42 -04002372 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04002373 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04002374 struct btrfs_fs_info *info = root->fs_info;
2375 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002376 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05002377 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05002378 int extent_slot = 0;
2379 int found_extent = 0;
2380 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04002381 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04002382 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05002383
Chris Masondb945352007-10-15 16:15:53 -04002384 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04002385 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04002386 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04002387 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002388 if (!path)
2389 return -ENOMEM;
2390
Chris Mason3c12ac72008-04-21 12:01:38 -04002391 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002392 ret = lookup_extent_backref(trans, extent_root, path,
2393 bytenr, parent, root_objectid,
2394 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05002395 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002396 struct btrfs_key found_key;
2397 extent_slot = path->slots[0];
2398 while(extent_slot > 0) {
2399 extent_slot--;
2400 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2401 extent_slot);
2402 if (found_key.objectid != bytenr)
2403 break;
2404 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2405 found_key.offset == num_bytes) {
2406 found_extent = 1;
2407 break;
2408 }
2409 if (path->slots[0] - extent_slot > 5)
2410 break;
2411 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002412 if (!found_extent) {
2413 ret = remove_extent_backref(trans, extent_root, path);
2414 BUG_ON(ret);
2415 btrfs_release_path(extent_root, path);
2416 ret = btrfs_search_slot(trans, extent_root,
2417 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002418 if (ret) {
2419 printk(KERN_ERR "umm, got %d back from search"
2420 ", was looking for %Lu\n", ret,
2421 bytenr);
2422 btrfs_print_leaf(extent_root, path->nodes[0]);
2423 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002424 BUG_ON(ret);
2425 extent_slot = path->slots[0];
2426 }
Chris Mason7bb86312007-12-11 09:25:06 -05002427 } else {
2428 btrfs_print_leaf(extent_root, path->nodes[0]);
2429 WARN_ON(1);
2430 printk("Unable to find ref byte nr %Lu root %Lu "
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002431 "gen %Lu owner %Lu\n", bytenr,
2432 root_objectid, ref_generation, owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05002433 }
Chris Mason5f39d392007-10-15 16:14:19 -04002434
2435 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05002436 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04002437 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002438 refs = btrfs_extent_refs(leaf, ei);
2439 BUG_ON(refs == 0);
2440 refs -= 1;
2441 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05002442
Chris Mason5f39d392007-10-15 16:14:19 -04002443 btrfs_mark_buffer_dirty(leaf);
2444
Chris Mason952fcca2008-02-18 16:33:44 -05002445 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002446 struct btrfs_extent_ref *ref;
2447 ref = btrfs_item_ptr(leaf, path->slots[0],
2448 struct btrfs_extent_ref);
2449 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002450 /* if the back ref and the extent are next to each other
2451 * they get deleted below in one shot
2452 */
2453 path->slots[0] = extent_slot;
2454 num_to_del = 2;
2455 } else if (found_extent) {
2456 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002457 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05002458 BUG_ON(ret);
2459 /* if refs are 0, we need to setup the path for deletion */
2460 if (refs == 0) {
2461 btrfs_release_path(extent_root, path);
2462 ret = btrfs_search_slot(trans, extent_root, &key, path,
2463 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002464 BUG_ON(ret);
2465 }
2466 }
2467
Chris Masoncf27e1e2007-03-13 09:49:06 -04002468 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002469 u64 super_used;
2470 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01002471#ifdef BIO_RW_DISCARD
2472 u64 map_length = num_bytes;
2473 struct btrfs_multi_bio *multi = NULL;
2474#endif
Chris Mason78fae272007-03-25 11:35:08 -04002475
2476 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002477 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002478 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2479 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04002480 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05002481 if (ret > 0)
2482 mark_free = 1;
2483 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04002484 }
2485
Josef Bacik58176a92007-08-29 15:47:34 -04002486 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002487 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002488 super_used = btrfs_super_bytes_used(&info->super_copy);
2489 btrfs_set_super_bytes_used(&info->super_copy,
2490 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002491 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04002492
2493 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002494 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002495 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04002496 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05002497 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2498 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04002499 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04002500 btrfs_release_path(extent_root, path);
Chris Masondb945352007-10-15 16:15:53 -04002501 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002502 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04002503 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01002504
2505#ifdef BIO_RW_DISCARD
2506 /* Tell the block device(s) that the sectors can be discarded */
2507 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2508 bytenr, &map_length, &multi, 0);
2509 if (!ret) {
2510 struct btrfs_bio_stripe *stripe = multi->stripes;
2511 int i;
2512
2513 if (map_length > num_bytes)
2514 map_length = num_bytes;
2515
2516 for (i = 0; i < multi->num_stripes; i++, stripe++) {
Chris Mason15916de2008-11-19 21:17:22 -05002517 btrfs_issue_discard(stripe->dev->bdev,
2518 stripe->physical,
2519 map_length);
David Woodhouse21af8042008-08-12 14:13:26 +01002520 }
2521 kfree(multi);
2522 }
2523#endif
Chris Masona28ec192007-03-06 20:08:01 -05002524 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002525 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002526 finish_current_insert(trans, extent_root, 0);
Chris Masona28ec192007-03-06 20:08:01 -05002527 return ret;
2528}
2529
2530/*
Chris Masonfec577f2007-02-26 10:40:21 -05002531 * find all the blocks marked as pending in the radix tree and remove
2532 * them from the extent map
2533 */
Chris Masone089f052007-03-16 16:20:31 -04002534static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -04002535 btrfs_root *extent_root, int all)
Chris Masonfec577f2007-02-26 10:40:21 -05002536{
2537 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002538 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002539 u64 start;
2540 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002541 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002542 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002543 int nr = 0, skipped = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002544 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002545 struct extent_io_tree *extent_ins;
2546 struct pending_extent_op *extent_op;
Josef Bacik25179202008-10-29 14:49:05 -04002547 struct btrfs_fs_info *info = extent_root->fs_info;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002548 struct list_head delete_list;
Chris Mason8ef97622007-03-26 10:15:30 -04002549
Josef Bacikf3465ca2008-11-12 14:19:50 -05002550 INIT_LIST_HEAD(&delete_list);
Zheng Yan31840ae2008-09-23 13:14:14 -04002551 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc162007-10-15 16:15:26 -04002552 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002553
Josef Bacikf3465ca2008-11-12 14:19:50 -05002554again:
2555 mutex_lock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002556 while(1) {
Josef Bacik25179202008-10-29 14:49:05 -04002557 ret = find_first_extent_bit(pending_del, search, &start, &end,
2558 EXTENT_WRITEBACK);
2559 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002560 if (all && skipped && !nr) {
Josef Bacik25179202008-10-29 14:49:05 -04002561 search = 0;
2562 continue;
2563 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002564 mutex_unlock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002565 break;
Josef Bacik25179202008-10-29 14:49:05 -04002566 }
2567
2568 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2569 if (!ret) {
2570 search = end+1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002571 skipped = 1;
2572
2573 if (need_resched()) {
2574 mutex_unlock(&info->extent_ins_mutex);
2575 cond_resched();
2576 mutex_lock(&info->extent_ins_mutex);
2577 }
2578
Josef Bacik25179202008-10-29 14:49:05 -04002579 continue;
2580 }
2581 BUG_ON(ret < 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002582
2583 ret = get_state_private(pending_del, start, &priv);
2584 BUG_ON(ret);
2585 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2586
Josef Bacik25179202008-10-29 14:49:05 -04002587 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
Chris Mason1a5bc162007-10-15 16:15:26 -04002588 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002589 if (!test_range_bit(extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04002590 EXTENT_WRITEBACK, 0)) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002591 list_add_tail(&extent_op->list, &delete_list);
2592 nr++;
Chris Masonc286ac42008-07-22 23:06:41 -04002593 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002594 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002595
2596 ret = get_state_private(&info->extent_ins, start,
2597 &priv);
Zheng Yan31840ae2008-09-23 13:14:14 -04002598 BUG_ON(ret);
2599 extent_op = (struct pending_extent_op *)
Josef Bacik25179202008-10-29 14:49:05 -04002600 (unsigned long)priv;
Zheng Yan31840ae2008-09-23 13:14:14 -04002601
Josef Bacik25179202008-10-29 14:49:05 -04002602 clear_extent_bits(&info->extent_ins, start, end,
2603 EXTENT_WRITEBACK, GFP_NOFS);
2604
Josef Bacikf3465ca2008-11-12 14:19:50 -05002605 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2606 list_add_tail(&extent_op->list, &delete_list);
2607 search = end + 1;
2608 nr++;
2609 continue;
2610 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002611
Josef Bacik25179202008-10-29 14:49:05 -04002612 mutex_lock(&extent_root->fs_info->pinned_mutex);
2613 ret = pin_down_bytes(trans, extent_root, start,
2614 end + 1 - start, 0);
2615 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2616
Zheng Yan31840ae2008-09-23 13:14:14 -04002617 ret = update_block_group(trans, extent_root, start,
Josef Bacik25179202008-10-29 14:49:05 -04002618 end + 1 - start, 0, ret > 0);
2619
Josef Bacikf3465ca2008-11-12 14:19:50 -05002620 unlock_extent(extent_ins, start, end, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002621 BUG_ON(ret);
2622 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002623 }
Chris Mason1a5bc162007-10-15 16:15:26 -04002624 if (ret)
2625 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04002626
Josef Bacikf3465ca2008-11-12 14:19:50 -05002627 search = end + 1;
2628
2629 if (need_resched()) {
2630 mutex_unlock(&info->extent_ins_mutex);
2631 cond_resched();
2632 mutex_lock(&info->extent_ins_mutex);
2633 }
Chris Masonfec577f2007-02-26 10:40:21 -05002634 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002635
2636 if (nr) {
2637 ret = free_extents(trans, extent_root, &delete_list);
2638 BUG_ON(ret);
2639 }
2640
2641 if (all && skipped) {
2642 INIT_LIST_HEAD(&delete_list);
2643 search = 0;
2644 nr = 0;
2645 goto again;
2646 }
2647
Chris Masone20d96d2007-03-22 12:13:20 -04002648 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002649}
2650
2651/*
2652 * remove an extent from the root, returns 0 on success
2653 */
Chris Mason925baed2008-06-25 16:01:30 -04002654static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002655 struct btrfs_root *root,
2656 u64 bytenr, u64 num_bytes, u64 parent,
2657 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002658 u64 owner_objectid, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002659{
Chris Mason9f5fae22007-03-20 14:38:32 -04002660 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002661 int pending_ret;
2662 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002663
Chris Masondb945352007-10-15 16:15:53 -04002664 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002665 if (root == extent_root) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002666 struct pending_extent_op *extent_op = NULL;
2667
2668 mutex_lock(&root->fs_info->extent_ins_mutex);
2669 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2670 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2671 u64 priv;
2672 ret = get_state_private(&root->fs_info->extent_ins,
2673 bytenr, &priv);
2674 BUG_ON(ret);
2675 extent_op = (struct pending_extent_op *)
2676 (unsigned long)priv;
2677
2678 extent_op->del = 1;
2679 if (extent_op->type == PENDING_EXTENT_INSERT) {
2680 mutex_unlock(&root->fs_info->extent_ins_mutex);
2681 return 0;
2682 }
2683 }
2684
2685 if (extent_op) {
2686 ref_generation = extent_op->orig_generation;
2687 parent = extent_op->orig_parent;
2688 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002689
2690 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2691 BUG_ON(!extent_op);
2692
2693 extent_op->type = PENDING_EXTENT_DELETE;
2694 extent_op->bytenr = bytenr;
2695 extent_op->num_bytes = num_bytes;
2696 extent_op->parent = parent;
2697 extent_op->orig_parent = parent;
2698 extent_op->generation = ref_generation;
2699 extent_op->orig_generation = ref_generation;
2700 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002701 INIT_LIST_HEAD(&extent_op->list);
2702 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002703
2704 set_extent_bits(&root->fs_info->pending_del,
2705 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04002706 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002707 set_state_private(&root->fs_info->pending_del,
2708 bytenr, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002709 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002710 return 0;
2711 }
Chris Mason4bef0842008-09-08 11:18:08 -04002712 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002713 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2714 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002715 struct btrfs_block_group_cache *cache;
2716
Chris Masond00aff02008-09-11 15:54:42 -04002717 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002718 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2719 BUG_ON(!cache);
2720 btrfs_add_free_space(cache, bytenr, num_bytes);
Zheng Yane8569812008-09-26 10:05:48 -04002721 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002722 return 0;
2723 }
Chris Mason4bef0842008-09-08 11:18:08 -04002724 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002725 }
Chris Mason4bef0842008-09-08 11:18:08 -04002726
2727 /* if data pin when any transaction has committed this */
2728 if (ref_generation != trans->transid)
2729 pin = 1;
2730
Zheng Yan31840ae2008-09-23 13:14:14 -04002731 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002732 root_objectid, ref_generation,
2733 owner_objectid, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002734
Chris Mason87ef2bb2008-10-30 11:23:27 -04002735 finish_current_insert(trans, root->fs_info->extent_root, 0);
2736 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002737 return ret ? ret : pending_ret;
2738}
2739
Chris Mason925baed2008-06-25 16:01:30 -04002740int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002741 struct btrfs_root *root,
2742 u64 bytenr, u64 num_bytes, u64 parent,
2743 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002744 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002745{
2746 int ret;
2747
Zheng Yan31840ae2008-09-23 13:14:14 -04002748 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002749 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002750 owner_objectid, pin);
Chris Mason925baed2008-06-25 16:01:30 -04002751 return ret;
2752}
2753
Chris Mason87ee04e2007-11-30 11:30:34 -05002754static u64 stripe_align(struct btrfs_root *root, u64 val)
2755{
2756 u64 mask = ((u64)root->stripesize - 1);
2757 u64 ret = (val + mask) & ~mask;
2758 return ret;
2759}
2760
Chris Masonfec577f2007-02-26 10:40:21 -05002761/*
2762 * walks the btree of allocated extents and find a hole of a given size.
2763 * The key ins is changed to record the hole:
2764 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002765 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002766 * ins->offset == number of blocks
2767 * Any available blocks before search_start are skipped.
2768 */
Chris Mason98ed5172008-01-03 10:01:48 -05002769static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2770 struct btrfs_root *orig_root,
2771 u64 num_bytes, u64 empty_size,
2772 u64 search_start, u64 search_end,
2773 u64 hint_byte, struct btrfs_key *ins,
2774 u64 exclude_start, u64 exclude_nr,
2775 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002776{
Josef Bacik80eb2342008-10-29 14:49:05 -04002777 int ret = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -04002778 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002779 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002780 u64 *last_ptr = NULL;
Chris Mason43662112008-11-07 09:06:11 -05002781 u64 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002782 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002783 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002784 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002785 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002786 struct list_head *head = NULL, *cur = NULL;
2787 int loop = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002788 int extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002789 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002790
Chris Masondb945352007-10-15 16:15:53 -04002791 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002792 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002793 ins->objectid = 0;
2794 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002795
Chris Mason0ef3e662008-05-24 14:04:53 -04002796 if (orig_root->ref_cows || empty_size)
2797 allowed_chunk_alloc = 1;
2798
Chris Mason239b14b2008-03-24 15:02:07 -04002799 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2800 last_ptr = &root->fs_info->last_alloc;
Chris Mason43662112008-11-07 09:06:11 -05002801 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002802 }
2803
Josef Bacik0f9dd462008-09-23 13:14:11 -04002804 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002805 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002806
Chris Mason239b14b2008-03-24 15:02:07 -04002807 if (last_ptr) {
Chris Mason43662112008-11-07 09:06:11 -05002808 if (*last_ptr) {
Chris Mason239b14b2008-03-24 15:02:07 -04002809 hint_byte = *last_ptr;
Chris Mason43662112008-11-07 09:06:11 -05002810 last_wanted = *last_ptr;
2811 } else
Chris Mason239b14b2008-03-24 15:02:07 -04002812 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002813 } else {
2814 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002815 }
Chris Masona061fc82008-05-07 11:43:44 -04002816 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002817 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04002818
Chris Mason42e70e72008-11-07 18:17:11 -05002819 if (last_wanted && search_start != last_wanted) {
Chris Mason43662112008-11-07 09:06:11 -05002820 last_wanted = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002821 empty_size += empty_cluster;
2822 }
Chris Mason43662112008-11-07 09:06:11 -05002823
Chris Mason42e70e72008-11-07 18:17:11 -05002824 total_needed += empty_size;
Josef Bacik80eb2342008-10-29 14:49:05 -04002825 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
Yan Zhengd899e052008-10-30 14:25:28 -04002826 if (!block_group)
2827 block_group = btrfs_lookup_first_block_group(root->fs_info,
2828 search_start);
Josef Bacik80eb2342008-10-29 14:49:05 -04002829 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002830
Josef Bacik80eb2342008-10-29 14:49:05 -04002831 down_read(&space_info->groups_sem);
2832 while (1) {
2833 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002834 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002835 * the only way this happens if our hint points to a block
2836 * group thats not of the proper type, while looping this
2837 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002838 */
Chris Mason8a1413a22008-11-10 16:13:54 -05002839 if (empty_size)
2840 extra_loop = 1;
2841
Chris Mason42e70e72008-11-07 18:17:11 -05002842 if (!block_group)
2843 goto new_group_no_lock;
2844
Josef Bacik25179202008-10-29 14:49:05 -04002845 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002846 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002847 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002848
Josef Bacik80eb2342008-10-29 14:49:05 -04002849 ret = cache_block_group(root, block_group);
Josef Bacik25179202008-10-29 14:49:05 -04002850 if (ret) {
2851 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002852 break;
Josef Bacik25179202008-10-29 14:49:05 -04002853 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002854
Josef Bacik80eb2342008-10-29 14:49:05 -04002855 if (block_group->ro)
2856 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002857
Josef Bacik80eb2342008-10-29 14:49:05 -04002858 free_space = btrfs_find_free_space(block_group, search_start,
2859 total_needed);
2860 if (free_space) {
2861 u64 start = block_group->key.objectid;
2862 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002863 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002864
Josef Bacik80eb2342008-10-29 14:49:05 -04002865 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002866
Josef Bacik80eb2342008-10-29 14:49:05 -04002867 /* move on to the next group */
2868 if (search_start + num_bytes >= search_end)
2869 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002870
Josef Bacik80eb2342008-10-29 14:49:05 -04002871 /* move on to the next group */
2872 if (search_start + num_bytes > end)
2873 goto new_group;
2874
Chris Mason43662112008-11-07 09:06:11 -05002875 if (last_wanted && search_start != last_wanted) {
Chris Mason3b7885b2008-11-06 21:48:27 -05002876 total_needed += empty_cluster;
Chris Mason8a1413a22008-11-10 16:13:54 -05002877 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002878 last_wanted = 0;
Chris Mason3b7885b2008-11-06 21:48:27 -05002879 /*
2880 * if search_start is still in this block group
2881 * then we just re-search this block group
2882 */
2883 if (search_start >= start &&
2884 search_start < end) {
2885 mutex_unlock(&block_group->alloc_mutex);
2886 continue;
2887 }
2888
2889 /* else we go to the next block group */
2890 goto new_group;
2891 }
2892
Josef Bacik80eb2342008-10-29 14:49:05 -04002893 if (exclude_nr > 0 &&
2894 (search_start + num_bytes > exclude_start &&
2895 search_start < exclude_start + exclude_nr)) {
2896 search_start = exclude_start + exclude_nr;
2897 /*
2898 * if search_start is still in this block group
2899 * then we just re-search this block group
2900 */
2901 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002902 search_start < end) {
2903 mutex_unlock(&block_group->alloc_mutex);
Chris Mason43662112008-11-07 09:06:11 -05002904 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002905 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002906 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002907
2908 /* else we go to the next block group */
2909 goto new_group;
2910 }
2911
2912 ins->objectid = search_start;
2913 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002914
2915 btrfs_remove_free_space_lock(block_group, search_start,
2916 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002917 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002918 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002919 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002920 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002921new_group:
Chris Mason42e70e72008-11-07 18:17:11 -05002922 mutex_unlock(&block_group->alloc_mutex);
2923new_group_no_lock:
Chris Masonf5a31e12008-11-10 11:47:09 -05002924 /* don't try to compare new allocations against the
2925 * last allocation any more
2926 */
Chris Mason43662112008-11-07 09:06:11 -05002927 last_wanted = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002928
Josef Bacik80eb2342008-10-29 14:49:05 -04002929 /*
2930 * Here's how this works.
2931 * loop == 0: we were searching a block group via a hint
2932 * and didn't find anything, so we start at
2933 * the head of the block groups and keep searching
2934 * loop == 1: we're searching through all of the block groups
2935 * if we hit the head again we have searched
2936 * all of the block groups for this space and we
2937 * need to try and allocate, if we cant error out.
2938 * loop == 2: we allocated more space and are looping through
2939 * all of the block groups again.
2940 */
2941 if (loop == 0) {
2942 head = &space_info->block_groups;
2943 cur = head->next;
Josef Bacik80eb2342008-10-29 14:49:05 -04002944 loop++;
2945 } else if (loop == 1 && cur == head) {
Chris Masonf5a31e12008-11-10 11:47:09 -05002946 int keep_going;
Chris Mason42e70e72008-11-07 18:17:11 -05002947
Chris Masonf5a31e12008-11-10 11:47:09 -05002948 /* at this point we give up on the empty_size
2949 * allocations and just try to allocate the min
2950 * space.
2951 *
2952 * The extra_loop field was set if an empty_size
2953 * allocation was attempted above, and if this
2954 * is try we need to try the loop again without
2955 * the additional empty_size.
2956 */
Chris Mason5b7c3fc2008-11-10 07:26:33 -05002957 total_needed -= empty_size;
2958 empty_size = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002959 keep_going = extra_loop;
2960 loop++;
Chris Mason42e70e72008-11-07 18:17:11 -05002961
Josef Bacik80eb2342008-10-29 14:49:05 -04002962 if (allowed_chunk_alloc && !chunk_alloc_done) {
2963 up_read(&space_info->groups_sem);
2964 ret = do_chunk_alloc(trans, root, num_bytes +
2965 2 * 1024 * 1024, data, 1);
Josef Bacik80eb2342008-10-29 14:49:05 -04002966 down_read(&space_info->groups_sem);
Chris Mason2ed6d662008-11-13 09:59:33 -05002967 if (ret < 0)
2968 goto loop_check;
Josef Bacik80eb2342008-10-29 14:49:05 -04002969 head = &space_info->block_groups;
Chris Masonf5a31e12008-11-10 11:47:09 -05002970 /*
2971 * we've allocated a new chunk, keep
2972 * trying
2973 */
2974 keep_going = 1;
Josef Bacik80eb2342008-10-29 14:49:05 -04002975 chunk_alloc_done = 1;
2976 } else if (!allowed_chunk_alloc) {
2977 space_info->force_alloc = 1;
Chris Masonf5a31e12008-11-10 11:47:09 -05002978 }
Chris Mason2ed6d662008-11-13 09:59:33 -05002979loop_check:
Chris Masonf5a31e12008-11-10 11:47:09 -05002980 if (keep_going) {
2981 cur = head->next;
2982 extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002983 } else {
2984 break;
2985 }
2986 } else if (cur == head) {
2987 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002988 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002989
2990 block_group = list_entry(cur, struct btrfs_block_group_cache,
2991 list);
2992 search_start = block_group->key.objectid;
2993 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002994 }
Chris Mason0b86a832008-03-24 15:01:56 -04002995
Josef Bacik80eb2342008-10-29 14:49:05 -04002996 /* we found what we needed */
2997 if (ins->objectid) {
2998 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2999 trans->block_group = block_group;
3000
3001 if (last_ptr)
3002 *last_ptr = ins->objectid + ins->offset;
3003 ret = 0;
3004 } else if (!ret) {
Josef Bacik4ce4cb52008-11-17 21:12:00 -05003005 printk(KERN_ERR "we were searching for %Lu bytes, num_bytes %Lu,"
3006 " loop %d, allowed_alloc %d\n", total_needed, num_bytes,
3007 loop, allowed_chunk_alloc);
Josef Bacik80eb2342008-10-29 14:49:05 -04003008 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04003009 }
Chris Mason0b86a832008-03-24 15:01:56 -04003010
Josef Bacik80eb2342008-10-29 14:49:05 -04003011 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05003012 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003013}
Chris Masonec44a352008-04-28 15:29:52 -04003014
Josef Bacik0f9dd462008-09-23 13:14:11 -04003015static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3016{
3017 struct btrfs_block_group_cache *cache;
3018 struct list_head *l;
3019
3020 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
Zheng Yane8569812008-09-26 10:05:48 -04003021 info->total_bytes - info->bytes_used - info->bytes_pinned -
3022 info->bytes_reserved, (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04003023
Josef Bacik80eb2342008-10-29 14:49:05 -04003024 down_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003025 list_for_each(l, &info->block_groups) {
3026 cache = list_entry(l, struct btrfs_block_group_cache, list);
3027 spin_lock(&cache->lock);
3028 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
Zheng Yane8569812008-09-26 10:05:48 -04003029 "%Lu pinned %Lu reserved\n",
Josef Bacik0f9dd462008-09-23 13:14:11 -04003030 cache->key.objectid, cache->key.offset,
Zheng Yane8569812008-09-26 10:05:48 -04003031 btrfs_block_group_used(&cache->item),
3032 cache->pinned, cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003033 btrfs_dump_free_space(cache, bytes);
3034 spin_unlock(&cache->lock);
3035 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003036 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003037}
Zheng Yane8569812008-09-26 10:05:48 -04003038
Chris Masone6dcd2d2008-07-17 12:53:50 -04003039static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3040 struct btrfs_root *root,
3041 u64 num_bytes, u64 min_alloc_size,
3042 u64 empty_size, u64 hint_byte,
3043 u64 search_end, struct btrfs_key *ins,
3044 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05003045{
3046 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04003047 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003048 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04003049 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003050
Chris Mason6324fbf2008-03-24 15:01:59 -04003051 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04003052 alloc_profile = info->avail_data_alloc_bits &
3053 info->data_alloc_profile;
3054 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003055 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04003056 alloc_profile = info->avail_system_alloc_bits &
3057 info->system_alloc_profile;
3058 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003059 } else {
Chris Mason8790d502008-04-03 16:29:03 -04003060 alloc_profile = info->avail_metadata_alloc_bits &
3061 info->metadata_alloc_profile;
3062 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003063 }
Chris Mason98d20f62008-04-14 09:46:10 -04003064again:
Yan Zheng2b820322008-11-17 21:11:30 -05003065 data = btrfs_reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04003066 /*
3067 * the only place that sets empty_size is btrfs_realloc_node, which
3068 * is not called recursively on allocations
3069 */
3070 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04003071 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04003072 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003073 2 * 1024 * 1024,
3074 BTRFS_BLOCK_GROUP_METADATA |
3075 (info->metadata_alloc_profile &
3076 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003077 }
3078 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003079 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003080 }
Chris Mason0b86a832008-03-24 15:01:56 -04003081
Chris Masondb945352007-10-15 16:15:53 -04003082 WARN_ON(num_bytes < root->sectorsize);
3083 ret = find_free_extent(trans, root, num_bytes, empty_size,
3084 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04003085 trans->alloc_exclude_start,
3086 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04003087
Chris Mason98d20f62008-04-14 09:46:10 -04003088 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3089 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003090 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003091 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04003092 do_chunk_alloc(trans, root->fs_info->extent_root,
3093 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003094 goto again;
3095 }
Chris Masonec44a352008-04-28 15:29:52 -04003096 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003097 struct btrfs_space_info *sinfo;
3098
3099 sinfo = __find_space_info(root->fs_info, data);
3100 printk("allocation failed flags %Lu, wanted %Lu\n",
3101 data, num_bytes);
3102 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04003103 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04003104 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003105
3106 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003107}
3108
Chris Mason65b51a02008-08-01 15:11:20 -04003109int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3110{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003111 struct btrfs_block_group_cache *cache;
3112
Josef Bacik0f9dd462008-09-23 13:14:11 -04003113 cache = btrfs_lookup_block_group(root->fs_info, start);
3114 if (!cache) {
3115 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003116 return -ENOSPC;
3117 }
3118 btrfs_add_free_space(cache, start, len);
Zheng Yan1a40e232008-09-26 10:09:34 -04003119 update_reserved_extents(root, start, len, 0);
Chris Mason65b51a02008-08-01 15:11:20 -04003120 return 0;
3121}
3122
Chris Masone6dcd2d2008-07-17 12:53:50 -04003123int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3124 struct btrfs_root *root,
3125 u64 num_bytes, u64 min_alloc_size,
3126 u64 empty_size, u64 hint_byte,
3127 u64 search_end, struct btrfs_key *ins,
3128 u64 data)
3129{
3130 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003131 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3132 empty_size, hint_byte, search_end, ins,
3133 data);
Zheng Yane8569812008-09-26 10:05:48 -04003134 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003135 return ret;
3136}
3137
3138static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003139 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003140 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003141 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003142{
3143 int ret;
3144 int pending_ret;
3145 u64 super_used;
3146 u64 root_used;
3147 u64 num_bytes = ins->offset;
3148 u32 sizes[2];
3149 struct btrfs_fs_info *info = root->fs_info;
3150 struct btrfs_root *extent_root = info->extent_root;
3151 struct btrfs_extent_item *extent_item;
3152 struct btrfs_extent_ref *ref;
3153 struct btrfs_path *path;
3154 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04003155
Zheng Yan31840ae2008-09-23 13:14:14 -04003156 if (parent == 0)
3157 parent = ins->objectid;
3158
Josef Bacik58176a92007-08-29 15:47:34 -04003159 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04003160 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04003161 super_used = btrfs_super_bytes_used(&info->super_copy);
3162 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04003163 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04003164
Josef Bacik58176a92007-08-29 15:47:34 -04003165 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04003166 root_used = btrfs_root_used(&root->root_item);
3167 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04003168
Chris Mason26b80032007-08-08 20:17:12 -04003169 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003170 struct pending_extent_op *extent_op;
3171
3172 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3173 BUG_ON(!extent_op);
3174
3175 extent_op->type = PENDING_EXTENT_INSERT;
3176 extent_op->bytenr = ins->objectid;
3177 extent_op->num_bytes = ins->offset;
3178 extent_op->parent = parent;
3179 extent_op->orig_parent = 0;
3180 extent_op->generation = ref_generation;
3181 extent_op->orig_generation = 0;
3182 extent_op->level = (int)owner;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003183 INIT_LIST_HEAD(&extent_op->list);
3184 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003185
Josef Bacik25179202008-10-29 14:49:05 -04003186 mutex_lock(&root->fs_info->extent_ins_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04003187 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3188 ins->objectid + ins->offset - 1,
Josef Bacik25179202008-10-29 14:49:05 -04003189 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04003190 set_state_private(&root->fs_info->extent_ins,
3191 ins->objectid, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04003192 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04003193 goto update_block;
3194 }
3195
Chris Mason47e4bb92008-02-01 14:51:59 -05003196 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05003197 keys[1].objectid = ins->objectid;
3198 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04003199 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05003200 sizes[0] = sizeof(*extent_item);
3201 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05003202
3203 path = btrfs_alloc_path();
3204 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05003205
3206 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3207 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04003208 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003209
Chris Mason47e4bb92008-02-01 14:51:59 -05003210 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3211 struct btrfs_extent_item);
3212 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3213 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3214 struct btrfs_extent_ref);
3215
3216 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3217 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3218 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Zheng Yan31840ae2008-09-23 13:14:14 -04003219 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05003220
3221 btrfs_mark_buffer_dirty(path->nodes[0]);
3222
3223 trans->alloc_exclude_start = 0;
3224 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05003225 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04003226 finish_current_insert(trans, extent_root, 0);
3227 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -04003228
Chris Mason925baed2008-06-25 16:01:30 -04003229 if (ret)
3230 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003231 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04003232 ret = pending_ret;
3233 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003234 }
Chris Mason26b80032007-08-08 20:17:12 -04003235
3236update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04003237 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05003238 if (ret) {
3239 printk("update block group failed for %Lu %Lu\n",
3240 ins->objectid, ins->offset);
3241 BUG();
3242 }
Chris Mason925baed2008-06-25 16:01:30 -04003243out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04003244 return ret;
3245}
3246
3247int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003248 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003249 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003250 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003251{
3252 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04003253
3254 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3255 return 0;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003256 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3257 ref_generation, owner, ins);
Zheng Yane8569812008-09-26 10:05:48 -04003258 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003259 return ret;
3260}
Chris Masone02119d2008-09-05 16:13:11 -04003261
3262/*
3263 * this is used by the tree logging recovery code. It records that
3264 * an extent has been allocated and makes sure to clear the free
3265 * space cache bits as well
3266 */
3267int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003268 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04003269 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003270 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04003271{
3272 int ret;
3273 struct btrfs_block_group_cache *block_group;
3274
Chris Masone02119d2008-09-05 16:13:11 -04003275 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik25179202008-10-29 14:49:05 -04003276 mutex_lock(&block_group->alloc_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003277 cache_block_group(root, block_group);
3278
Josef Bacik25179202008-10-29 14:49:05 -04003279 ret = btrfs_remove_free_space_lock(block_group, ins->objectid,
3280 ins->offset);
3281 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003282 BUG_ON(ret);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003283 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3284 ref_generation, owner, ins);
Chris Masone02119d2008-09-05 16:13:11 -04003285 return ret;
3286}
3287
Chris Masone6dcd2d2008-07-17 12:53:50 -04003288/*
3289 * finds a free extent and does all the dirty work required for allocation
3290 * returns the key for the extent through ins, and a tree buffer for
3291 * the first block of the extent through buf.
3292 *
3293 * returns 0 if everything worked, non-zero otherwise.
3294 */
3295int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3296 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003297 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003298 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003299 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003300 u64 search_end, struct btrfs_key *ins, u64 data)
3301{
3302 int ret;
3303
Chris Masone6dcd2d2008-07-17 12:53:50 -04003304 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3305 min_alloc_size, empty_size, hint_byte,
3306 search_end, ins, data);
3307 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04003308 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003309 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3310 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003311 owner_objectid, ins);
Chris Masond00aff02008-09-11 15:54:42 -04003312 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003313
Zheng Yane8569812008-09-26 10:05:48 -04003314 } else {
3315 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04003316 }
Chris Mason925baed2008-06-25 16:01:30 -04003317 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003318}
Chris Mason65b51a02008-08-01 15:11:20 -04003319
3320struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3321 struct btrfs_root *root,
3322 u64 bytenr, u32 blocksize)
3323{
3324 struct extent_buffer *buf;
3325
3326 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3327 if (!buf)
3328 return ERR_PTR(-ENOMEM);
3329 btrfs_set_header_generation(buf, trans->transid);
3330 btrfs_tree_lock(buf);
3331 clean_tree_block(trans, root, buf);
3332 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04003333 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3334 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04003335 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04003336 } else {
3337 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3338 buf->start + buf->len - 1, GFP_NOFS);
3339 }
Chris Mason65b51a02008-08-01 15:11:20 -04003340 trans->blocks_used++;
3341 return buf;
3342}
3343
Chris Masonfec577f2007-02-26 10:40:21 -05003344/*
3345 * helper function to allocate a block for a given tree
3346 * returns the tree buffer or NULL.
3347 */
Chris Mason5f39d392007-10-15 16:14:19 -04003348struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04003349 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003350 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05003351 u64 root_objectid,
3352 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05003353 int level,
3354 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04003355 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05003356{
Chris Masone2fa7222007-03-12 16:22:34 -04003357 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05003358 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003359 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05003360
Zheng Yan31840ae2008-09-23 13:14:14 -04003361 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003362 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04003363 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05003364 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04003365 BUG_ON(ret > 0);
3366 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05003367 }
Chris Mason55c69072008-01-09 15:55:33 -05003368
Chris Mason65b51a02008-08-01 15:11:20 -04003369 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05003370 return buf;
3371}
Chris Masona28ec192007-03-06 20:08:01 -05003372
Chris Masone02119d2008-09-05 16:13:11 -04003373int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3374 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04003375{
Chris Mason7bb86312007-12-11 09:25:06 -05003376 u64 leaf_owner;
3377 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04003378 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04003379 struct btrfs_file_extent_item *fi;
3380 int i;
3381 int nritems;
3382 int ret;
3383
Chris Mason5f39d392007-10-15 16:14:19 -04003384 BUG_ON(!btrfs_is_leaf(leaf));
3385 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05003386 leaf_owner = btrfs_header_owner(leaf);
3387 leaf_generation = btrfs_header_generation(leaf);
3388
Chris Mason6407bf62007-03-27 06:33:00 -04003389 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003390 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04003391 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04003392
3393 btrfs_item_key_to_cpu(leaf, &key, i);
3394 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04003395 continue;
3396 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003397 if (btrfs_file_extent_type(leaf, fi) ==
3398 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04003399 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04003400 /*
3401 * FIXME make sure to insert a trans record that
3402 * repeats the snapshot del on crash
3403 */
Chris Masondb945352007-10-15 16:15:53 -04003404 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3405 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04003406 continue;
Chris Mason4a096752008-07-21 10:29:44 -04003407
Chris Mason925baed2008-06-25 16:01:30 -04003408 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003409 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04003410 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003411 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003412 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04003413
3414 atomic_inc(&root->fs_info->throttle_gen);
3415 wake_up(&root->fs_info->transaction_throttle);
3416 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04003417 }
3418 return 0;
3419}
3420
Chris Masone02119d2008-09-05 16:13:11 -04003421static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3422 struct btrfs_root *root,
3423 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04003424{
3425 int i;
3426 int ret;
3427 struct btrfs_extent_info *info = ref->extents;
3428
Yan Zheng31153d82008-07-28 15:32:19 -04003429 for (i = 0; i < ref->nritems; i++) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003430 ret = __btrfs_free_extent(trans, root, info->bytenr,
3431 info->num_bytes, ref->bytenr,
3432 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003433 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04003434
3435 atomic_inc(&root->fs_info->throttle_gen);
3436 wake_up(&root->fs_info->transaction_throttle);
3437 cond_resched();
3438
Yan Zheng31153d82008-07-28 15:32:19 -04003439 BUG_ON(ret);
3440 info++;
3441 }
Yan Zheng31153d82008-07-28 15:32:19 -04003442
3443 return 0;
3444}
3445
Chris Mason333db942008-06-25 16:01:30 -04003446int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
3447 u32 *refs)
3448{
Chris Mason017e5362008-07-28 15:32:51 -04003449 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04003450
Zheng Yan31840ae2008-09-23 13:14:14 -04003451 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04003452 BUG_ON(ret);
3453
3454#if 0 // some debugging code in case we see problems here
3455 /* if the refs count is one, it won't get increased again. But
3456 * if the ref count is > 1, someone may be decreasing it at
3457 * the same time we are.
3458 */
3459 if (*refs != 1) {
3460 struct extent_buffer *eb = NULL;
3461 eb = btrfs_find_create_tree_block(root, start, len);
3462 if (eb)
3463 btrfs_tree_lock(eb);
3464
3465 mutex_lock(&root->fs_info->alloc_mutex);
3466 ret = lookup_extent_ref(NULL, root, start, len, refs);
3467 BUG_ON(ret);
3468 mutex_unlock(&root->fs_info->alloc_mutex);
3469
3470 if (eb) {
3471 btrfs_tree_unlock(eb);
3472 free_extent_buffer(eb);
3473 }
3474 if (*refs == 1) {
3475 printk("block %llu went down to one during drop_snap\n",
3476 (unsigned long long)start);
3477 }
3478
3479 }
3480#endif
3481
Chris Masone7a84562008-06-25 16:01:31 -04003482 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04003483 return ret;
Chris Mason333db942008-06-25 16:01:30 -04003484}
3485
3486/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003487 * helper function for drop_snapshot, this walks down the tree dropping ref
3488 * counts as it goes.
3489 */
Chris Mason98ed5172008-01-03 10:01:48 -05003490static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
3491 struct btrfs_root *root,
3492 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003493{
Chris Mason7bb86312007-12-11 09:25:06 -05003494 u64 root_owner;
3495 u64 root_gen;
3496 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04003497 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003498 struct extent_buffer *next;
3499 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05003500 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04003501 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04003502 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05003503 int ret;
3504 u32 refs;
3505
Chris Mason5caf2a02007-04-02 11:20:42 -04003506 WARN_ON(*level < 0);
3507 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04003508 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04003509 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05003510 BUG_ON(ret);
3511 if (refs > 1)
3512 goto out;
Chris Masone0115992007-06-19 16:23:05 -04003513
Chris Mason9aca1d52007-03-13 11:09:37 -04003514 /*
3515 * walk down to the last node level and free all the leaves
3516 */
Chris Mason6407bf62007-03-27 06:33:00 -04003517 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003518 WARN_ON(*level < 0);
3519 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05003520 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04003521
Chris Mason5f39d392007-10-15 16:14:19 -04003522 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04003523 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04003524
Chris Mason7518a232007-03-12 12:01:18 -04003525 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04003526 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05003527 break;
Chris Mason6407bf62007-03-27 06:33:00 -04003528 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003529 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04003530 BUG_ON(ret);
3531 break;
3532 }
Chris Masondb945352007-10-15 16:15:53 -04003533 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04003534 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04003535 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04003536
Chris Mason333db942008-06-25 16:01:30 -04003537 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04003538 BUG_ON(ret);
3539 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05003540 parent = path->nodes[*level];
3541 root_owner = btrfs_header_owner(parent);
3542 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05003543 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04003544
Chris Mason925baed2008-06-25 16:01:30 -04003545 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003546 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003547 root_owner, root_gen,
3548 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05003549 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04003550
3551 atomic_inc(&root->fs_info->throttle_gen);
3552 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04003553 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04003554
Chris Mason20524f02007-03-10 06:35:47 -05003555 continue;
3556 }
Chris Masonf87f0572008-08-01 11:27:23 -04003557 /*
3558 * at this point, we have a single ref, and since the
3559 * only place referencing this extent is a dead root
3560 * the reference count should never go higher.
3561 * So, we don't need to check it again
3562 */
Yan Zheng31153d82008-07-28 15:32:19 -04003563 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04003564 ref = btrfs_lookup_leaf_ref(root, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04003565 if (ref && ref->generation != ptr_gen) {
3566 btrfs_free_leaf_ref(root, ref);
3567 ref = NULL;
3568 }
Yan Zheng31153d82008-07-28 15:32:19 -04003569 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04003570 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003571 BUG_ON(ret);
3572 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04003573 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003574 *level = 0;
3575 break;
3576 }
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003577 if (printk_ratelimit()) {
Chris Mason37d1aee2008-07-31 10:48:37 -04003578 printk("leaf ref miss for bytenr %llu\n",
3579 (unsigned long long)bytenr);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003580 }
Yan Zheng31153d82008-07-28 15:32:19 -04003581 }
Chris Masondb945352007-10-15 16:15:53 -04003582 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04003583 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003584 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04003585
Chris Masonca7a79a2008-05-12 12:59:19 -04003586 next = read_tree_block(root, bytenr, blocksize,
3587 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04003588 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04003589#if 0
3590 /*
3591 * this is a debugging check and can go away
3592 * the ref should never go all the way down to 1
3593 * at this point
3594 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003595 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
3596 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04003597 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003598 WARN_ON(refs != 1);
3599#endif
Chris Masone9d0b132007-08-10 14:06:19 -04003600 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003601 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04003602 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04003603 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05003604 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04003605 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05003606 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04003607 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003608 }
3609out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003610 WARN_ON(*level < 0);
3611 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05003612
3613 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003614 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003615 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003616 } else {
3617 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003618 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003619 }
3620
Yan Zheng31153d82008-07-28 15:32:19 -04003621 blocksize = btrfs_level_size(root, *level);
3622 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003623 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003624
3625 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003626 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003627 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003628 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003629 path->nodes[*level] = NULL;
3630 *level += 1;
3631 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003632
Chris Masone7a84562008-06-25 16:01:31 -04003633 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003634 return 0;
3635}
3636
Chris Mason9aca1d52007-03-13 11:09:37 -04003637/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04003638 * helper function for drop_subtree, this function is similar to
3639 * walk_down_tree. The main difference is that it checks reference
3640 * counts while tree blocks are locked.
3641 */
3642static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
3643 struct btrfs_root *root,
3644 struct btrfs_path *path, int *level)
3645{
3646 struct extent_buffer *next;
3647 struct extent_buffer *cur;
3648 struct extent_buffer *parent;
3649 u64 bytenr;
3650 u64 ptr_gen;
3651 u32 blocksize;
3652 u32 refs;
3653 int ret;
3654
3655 cur = path->nodes[*level];
3656 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3657 &refs);
3658 BUG_ON(ret);
3659 if (refs > 1)
3660 goto out;
3661
3662 while (*level >= 0) {
3663 cur = path->nodes[*level];
3664 if (*level == 0) {
3665 ret = btrfs_drop_leaf_ref(trans, root, cur);
3666 BUG_ON(ret);
3667 clean_tree_block(trans, root, cur);
3668 break;
3669 }
3670 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3671 clean_tree_block(trans, root, cur);
3672 break;
3673 }
3674
3675 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3676 blocksize = btrfs_level_size(root, *level - 1);
3677 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3678
3679 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3680 btrfs_tree_lock(next);
3681
3682 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3683 &refs);
3684 BUG_ON(ret);
3685 if (refs > 1) {
3686 parent = path->nodes[*level];
3687 ret = btrfs_free_extent(trans, root, bytenr,
3688 blocksize, parent->start,
3689 btrfs_header_owner(parent),
3690 btrfs_header_generation(parent),
3691 *level - 1, 1);
3692 BUG_ON(ret);
3693 path->slots[*level]++;
3694 btrfs_tree_unlock(next);
3695 free_extent_buffer(next);
3696 continue;
3697 }
3698
3699 *level = btrfs_header_level(next);
3700 path->nodes[*level] = next;
3701 path->slots[*level] = 0;
3702 path->locks[*level] = 1;
3703 cond_resched();
3704 }
3705out:
3706 parent = path->nodes[*level + 1];
3707 bytenr = path->nodes[*level]->start;
3708 blocksize = path->nodes[*level]->len;
3709
3710 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3711 parent->start, btrfs_header_owner(parent),
3712 btrfs_header_generation(parent), *level, 1);
3713 BUG_ON(ret);
3714
3715 if (path->locks[*level]) {
3716 btrfs_tree_unlock(path->nodes[*level]);
3717 path->locks[*level] = 0;
3718 }
3719 free_extent_buffer(path->nodes[*level]);
3720 path->nodes[*level] = NULL;
3721 *level += 1;
3722 cond_resched();
3723 return 0;
3724}
3725
3726/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003727 * helper for dropping snapshots. This walks back up the tree in the path
3728 * to find the first node higher up where we haven't yet gone through
3729 * all the slots
3730 */
Chris Mason98ed5172008-01-03 10:01:48 -05003731static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3732 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003733 struct btrfs_path *path,
3734 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003735{
Chris Mason7bb86312007-12-11 09:25:06 -05003736 u64 root_owner;
3737 u64 root_gen;
3738 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003739 int i;
3740 int slot;
3741 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003742
Yan Zhengf82d02d2008-10-29 14:49:05 -04003743 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003744 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003745 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3746 struct extent_buffer *node;
3747 struct btrfs_disk_key disk_key;
3748 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003749 path->slots[i]++;
3750 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003751 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003752 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003753 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003754 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003755 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003756 return 0;
3757 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003758 struct extent_buffer *parent;
3759 if (path->nodes[*level] == root->node)
3760 parent = path->nodes[*level];
3761 else
3762 parent = path->nodes[*level + 1];
3763
3764 root_owner = btrfs_header_owner(parent);
3765 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003766
3767 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003768 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003769 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003770 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003771 parent->start, root_owner,
3772 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003773 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003774 if (path->locks[*level]) {
3775 btrfs_tree_unlock(path->nodes[*level]);
3776 path->locks[*level] = 0;
3777 }
Chris Mason5f39d392007-10-15 16:14:19 -04003778 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003779 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003780 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003781 }
3782 }
3783 return 1;
3784}
3785
Chris Mason9aca1d52007-03-13 11:09:37 -04003786/*
3787 * drop the reference count on the tree rooted at 'snap'. This traverses
3788 * the tree freeing any blocks that have a ref count of zero after being
3789 * decremented.
3790 */
Chris Masone089f052007-03-16 16:20:31 -04003791int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003792 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003793{
Chris Mason3768f362007-03-13 16:47:54 -04003794 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003795 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003796 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003797 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003798 int i;
3799 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003800 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003801
Chris Masona2135012008-06-25 16:01:30 -04003802 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003803 path = btrfs_alloc_path();
3804 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003805
Chris Mason5f39d392007-10-15 16:14:19 -04003806 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003807 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003808 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3809 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003810 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003811 path->slots[level] = 0;
3812 } else {
3813 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003814 struct btrfs_disk_key found_key;
3815 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003816
Chris Mason9f3a7422007-08-07 15:52:19 -04003817 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003818 level = root_item->drop_level;
3819 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003820 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003821 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003822 ret = wret;
3823 goto out;
3824 }
Chris Mason5f39d392007-10-15 16:14:19 -04003825 node = path->nodes[level];
3826 btrfs_node_key(node, &found_key, path->slots[level]);
3827 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3828 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003829 /*
3830 * unlock our path, this is safe because only this
3831 * function is allowed to delete this snapshot
3832 */
Chris Mason925baed2008-06-25 16:01:30 -04003833 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3834 if (path->nodes[i] && path->locks[i]) {
3835 path->locks[i] = 0;
3836 btrfs_tree_unlock(path->nodes[i]);
3837 }
3838 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003839 }
Chris Mason20524f02007-03-10 06:35:47 -05003840 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003841 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003842 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003843 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003844 if (wret < 0)
3845 ret = wret;
3846
Yan Zhengf82d02d2008-10-29 14:49:05 -04003847 wret = walk_up_tree(trans, root, path, &level,
3848 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003849 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003850 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003851 if (wret < 0)
3852 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003853 if (trans->transaction->in_commit) {
3854 ret = -EAGAIN;
3855 break;
3856 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003857 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003858 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003859 }
Chris Mason83e15a22007-03-12 09:03:27 -04003860 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003861 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003862 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003863 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003864 }
Chris Mason20524f02007-03-10 06:35:47 -05003865 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003866out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003867 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003868 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003869}
Chris Mason9078a3e2007-04-26 16:46:15 -04003870
Yan Zhengf82d02d2008-10-29 14:49:05 -04003871int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3872 struct btrfs_root *root,
3873 struct extent_buffer *node,
3874 struct extent_buffer *parent)
3875{
3876 struct btrfs_path *path;
3877 int level;
3878 int parent_level;
3879 int ret = 0;
3880 int wret;
3881
3882 path = btrfs_alloc_path();
3883 BUG_ON(!path);
3884
3885 BUG_ON(!btrfs_tree_locked(parent));
3886 parent_level = btrfs_header_level(parent);
3887 extent_buffer_get(parent);
3888 path->nodes[parent_level] = parent;
3889 path->slots[parent_level] = btrfs_header_nritems(parent);
3890
3891 BUG_ON(!btrfs_tree_locked(node));
3892 level = btrfs_header_level(node);
3893 extent_buffer_get(node);
3894 path->nodes[level] = node;
3895 path->slots[level] = 0;
3896
3897 while (1) {
3898 wret = walk_down_subtree(trans, root, path, &level);
3899 if (wret < 0)
3900 ret = wret;
3901 if (wret != 0)
3902 break;
3903
3904 wret = walk_up_tree(trans, root, path, &level, parent_level);
3905 if (wret < 0)
3906 ret = wret;
3907 if (wret != 0)
3908 break;
3909 }
3910
3911 btrfs_free_path(path);
3912 return ret;
3913}
3914
Chris Mason8e7bf942008-04-28 09:02:36 -04003915static unsigned long calc_ra(unsigned long start, unsigned long last,
3916 unsigned long nr)
3917{
3918 return min(last, start + nr - 1);
3919}
3920
Chris Mason98ed5172008-01-03 10:01:48 -05003921static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3922 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003923{
3924 u64 page_start;
3925 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003926 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003927 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003928 unsigned long i;
3929 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003930 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003931 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003932 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003933 unsigned int total_read = 0;
3934 unsigned int total_dirty = 0;
3935 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003936
3937 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003938
3939 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003940 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003941 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3942
Zheng Yan1a40e232008-09-26 10:09:34 -04003943 /* make sure the dirty trick played by the caller work */
3944 ret = invalidate_inode_pages2_range(inode->i_mapping,
3945 first_index, last_index);
3946 if (ret)
3947 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003948
Chris Mason4313b392008-01-03 09:08:48 -05003949 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003950
Zheng Yan1a40e232008-09-26 10:09:34 -04003951 for (i = first_index ; i <= last_index; i++) {
3952 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003953 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003954 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003955 }
3956 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003957again:
3958 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003959 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003960 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003961 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003962 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003963 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003964 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003965 if (!PageUptodate(page)) {
3966 btrfs_readpage(NULL, page);
3967 lock_page(page);
3968 if (!PageUptodate(page)) {
3969 unlock_page(page);
3970 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003971 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003972 goto out_unlock;
3973 }
3974 }
Chris Masonec44a352008-04-28 15:29:52 -04003975 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003976
Chris Masonedbd8d42007-12-21 16:27:24 -05003977 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3978 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003979 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003980
Chris Mason3eaa2882008-07-24 11:57:52 -04003981 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3982 if (ordered) {
3983 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3984 unlock_page(page);
3985 page_cache_release(page);
3986 btrfs_start_ordered_extent(inode, ordered, 1);
3987 btrfs_put_ordered_extent(ordered);
3988 goto again;
3989 }
3990 set_page_extent_mapped(page);
3991
Chris Masonea8c2812008-08-04 23:17:27 -04003992 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003993 if (i == first_index)
3994 set_extent_bits(io_tree, page_start, page_end,
3995 EXTENT_BOUNDARY, GFP_NOFS);
3996
Chris Masona061fc82008-05-07 11:43:44 -04003997 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003998 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003999
Chris Masond1310b22008-01-24 16:13:08 -05004000 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05004001 unlock_page(page);
4002 page_cache_release(page);
4003 }
4004
4005out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04004006 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05004007 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04004008 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04004009 return ret;
4010}
4011
Zheng Yan1a40e232008-09-26 10:09:34 -04004012static int noinline relocate_data_extent(struct inode *reloc_inode,
4013 struct btrfs_key *extent_key,
4014 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05004015{
Zheng Yan1a40e232008-09-26 10:09:34 -04004016 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4017 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4018 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04004019 u64 start = extent_key->objectid - offset;
4020 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004021
4022 em = alloc_extent_map(GFP_NOFS);
4023 BUG_ON(!em || IS_ERR(em));
4024
Yan Zheng66435582008-10-30 14:19:50 -04004025 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004026 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004027 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004028 em->block_start = extent_key->objectid;
4029 em->bdev = root->fs_info->fs_devices->latest_bdev;
4030 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4031
4032 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04004033 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004034 while (1) {
4035 int ret;
4036 spin_lock(&em_tree->lock);
4037 ret = add_extent_mapping(em_tree, em);
4038 spin_unlock(&em_tree->lock);
4039 if (ret != -EEXIST) {
4040 free_extent_map(em);
4041 break;
4042 }
Yan Zheng66435582008-10-30 14:19:50 -04004043 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004044 }
Yan Zheng66435582008-10-30 14:19:50 -04004045 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004046
Yan Zheng66435582008-10-30 14:19:50 -04004047 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04004048}
4049
4050struct btrfs_ref_path {
4051 u64 extent_start;
4052 u64 nodes[BTRFS_MAX_LEVEL];
4053 u64 root_objectid;
4054 u64 root_generation;
4055 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004056 u32 num_refs;
4057 int lowest_level;
4058 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004059 int shared_level;
4060
4061 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4062 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04004063};
4064
4065struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04004066 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04004067 u64 disk_bytenr;
4068 u64 disk_num_bytes;
4069 u64 offset;
4070 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04004071 u8 compression;
4072 u8 encryption;
4073 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04004074};
4075
4076static int is_cowonly_root(u64 root_objectid)
4077{
4078 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4079 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4080 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4081 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
4082 root_objectid == BTRFS_TREE_LOG_OBJECTID)
4083 return 1;
4084 return 0;
4085}
4086
4087static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
4088 struct btrfs_root *extent_root,
4089 struct btrfs_ref_path *ref_path,
4090 int first_time)
4091{
4092 struct extent_buffer *leaf;
4093 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05004094 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05004095 struct btrfs_key key;
4096 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04004097 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05004098 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04004099 int level;
4100 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05004101
Zheng Yan1a40e232008-09-26 10:09:34 -04004102 path = btrfs_alloc_path();
4103 if (!path)
4104 return -ENOMEM;
4105
Zheng Yan1a40e232008-09-26 10:09:34 -04004106 if (first_time) {
4107 ref_path->lowest_level = -1;
4108 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004109 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004110 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04004111 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004112walk_down:
4113 level = ref_path->current_level - 1;
4114 while (level >= -1) {
4115 u64 parent;
4116 if (level < ref_path->lowest_level)
4117 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004118
Zheng Yan1a40e232008-09-26 10:09:34 -04004119 if (level >= 0) {
4120 bytenr = ref_path->nodes[level];
4121 } else {
4122 bytenr = ref_path->extent_start;
4123 }
4124 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004125
Zheng Yan1a40e232008-09-26 10:09:34 -04004126 parent = ref_path->nodes[level + 1];
4127 ref_path->nodes[level + 1] = 0;
4128 ref_path->current_level = level;
4129 BUG_ON(parent == 0);
4130
4131 key.objectid = bytenr;
4132 key.offset = parent + 1;
4133 key.type = BTRFS_EXTENT_REF_KEY;
4134
4135 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004136 if (ret < 0)
4137 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004138 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004139
Chris Masonedbd8d42007-12-21 16:27:24 -05004140 leaf = path->nodes[0];
4141 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04004142 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04004143 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04004144 if (ret < 0)
4145 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004146 if (ret > 0)
4147 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04004148 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04004149 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004150
4151 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04004152 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04004153 found_key.type == BTRFS_EXTENT_REF_KEY) {
4154 if (level < ref_path->shared_level)
4155 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004156 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004157 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004158next:
4159 level--;
4160 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004161 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004162 }
4163 /* reached lowest level */
4164 ret = 1;
4165 goto out;
4166walk_up:
4167 level = ref_path->current_level;
4168 while (level < BTRFS_MAX_LEVEL - 1) {
4169 u64 ref_objectid;
4170 if (level >= 0) {
4171 bytenr = ref_path->nodes[level];
4172 } else {
4173 bytenr = ref_path->extent_start;
Chris Masona061fc82008-05-07 11:43:44 -04004174 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004175 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004176
Zheng Yan1a40e232008-09-26 10:09:34 -04004177 key.objectid = bytenr;
4178 key.offset = 0;
4179 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05004180
Zheng Yan1a40e232008-09-26 10:09:34 -04004181 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4182 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05004183 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004184
4185 leaf = path->nodes[0];
4186 nritems = btrfs_header_nritems(leaf);
4187 if (path->slots[0] >= nritems) {
4188 ret = btrfs_next_leaf(extent_root, path);
4189 if (ret < 0)
4190 goto out;
4191 if (ret > 0) {
4192 /* the extent was freed by someone */
4193 if (ref_path->lowest_level == level)
4194 goto out;
4195 btrfs_release_path(extent_root, path);
4196 goto walk_down;
4197 }
4198 leaf = path->nodes[0];
4199 }
4200
4201 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4202 if (found_key.objectid != bytenr ||
4203 found_key.type != BTRFS_EXTENT_REF_KEY) {
4204 /* the extent was freed by someone */
4205 if (ref_path->lowest_level == level) {
4206 ret = 1;
4207 goto out;
4208 }
4209 btrfs_release_path(extent_root, path);
4210 goto walk_down;
4211 }
4212found:
4213 ref = btrfs_item_ptr(leaf, path->slots[0],
4214 struct btrfs_extent_ref);
4215 ref_objectid = btrfs_ref_objectid(leaf, ref);
4216 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4217 if (first_time) {
4218 level = (int)ref_objectid;
4219 BUG_ON(level >= BTRFS_MAX_LEVEL);
4220 ref_path->lowest_level = level;
4221 ref_path->current_level = level;
4222 ref_path->nodes[level] = bytenr;
4223 } else {
4224 WARN_ON(ref_objectid != level);
4225 }
4226 } else {
4227 WARN_ON(level != -1);
4228 }
4229 first_time = 0;
4230
4231 if (ref_path->lowest_level == level) {
4232 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004233 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4234 }
4235
4236 /*
4237 * the block is tree root or the block isn't in reference
4238 * counted tree.
4239 */
4240 if (found_key.objectid == found_key.offset ||
4241 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4242 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4243 ref_path->root_generation =
4244 btrfs_ref_generation(leaf, ref);
4245 if (level < 0) {
4246 /* special reference from the tree log */
4247 ref_path->nodes[0] = found_key.offset;
4248 ref_path->current_level = 0;
4249 }
4250 ret = 0;
4251 goto out;
4252 }
4253
4254 level++;
4255 BUG_ON(ref_path->nodes[level] != 0);
4256 ref_path->nodes[level] = found_key.offset;
4257 ref_path->current_level = level;
4258
4259 /*
4260 * the reference was created in the running transaction,
4261 * no need to continue walking up.
4262 */
4263 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4264 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4265 ref_path->root_generation =
4266 btrfs_ref_generation(leaf, ref);
4267 ret = 0;
4268 goto out;
4269 }
4270
4271 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004272 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004273 }
4274 /* reached max tree level, but no tree root found. */
4275 BUG();
4276out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004277 btrfs_free_path(path);
4278 return ret;
4279}
4280
4281static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4282 struct btrfs_root *extent_root,
4283 struct btrfs_ref_path *ref_path,
4284 u64 extent_start)
4285{
4286 memset(ref_path, 0, sizeof(*ref_path));
4287 ref_path->extent_start = extent_start;
4288
4289 return __next_ref_path(trans, extent_root, ref_path, 1);
4290}
4291
4292static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4293 struct btrfs_root *extent_root,
4294 struct btrfs_ref_path *ref_path)
4295{
4296 return __next_ref_path(trans, extent_root, ref_path, 0);
4297}
4298
4299static int noinline get_new_locations(struct inode *reloc_inode,
4300 struct btrfs_key *extent_key,
4301 u64 offset, int no_fragment,
4302 struct disk_extent **extents,
4303 int *nr_extents)
4304{
4305 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4306 struct btrfs_path *path;
4307 struct btrfs_file_extent_item *fi;
4308 struct extent_buffer *leaf;
4309 struct disk_extent *exts = *extents;
4310 struct btrfs_key found_key;
4311 u64 cur_pos;
4312 u64 last_byte;
4313 u32 nritems;
4314 int nr = 0;
4315 int max = *nr_extents;
4316 int ret;
4317
4318 WARN_ON(!no_fragment && *extents);
4319 if (!exts) {
4320 max = 1;
4321 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4322 if (!exts)
4323 return -ENOMEM;
4324 }
4325
4326 path = btrfs_alloc_path();
4327 BUG_ON(!path);
4328
4329 cur_pos = extent_key->objectid - offset;
4330 last_byte = extent_key->objectid + extent_key->offset;
4331 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4332 cur_pos, 0);
4333 if (ret < 0)
4334 goto out;
4335 if (ret > 0) {
4336 ret = -ENOENT;
4337 goto out;
4338 }
4339
4340 while (1) {
4341 leaf = path->nodes[0];
4342 nritems = btrfs_header_nritems(leaf);
4343 if (path->slots[0] >= nritems) {
4344 ret = btrfs_next_leaf(root, path);
4345 if (ret < 0)
4346 goto out;
4347 if (ret > 0)
4348 break;
4349 leaf = path->nodes[0];
4350 }
4351
4352 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4353 if (found_key.offset != cur_pos ||
4354 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4355 found_key.objectid != reloc_inode->i_ino)
4356 break;
4357
4358 fi = btrfs_item_ptr(leaf, path->slots[0],
4359 struct btrfs_file_extent_item);
4360 if (btrfs_file_extent_type(leaf, fi) !=
4361 BTRFS_FILE_EXTENT_REG ||
4362 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4363 break;
4364
4365 if (nr == max) {
4366 struct disk_extent *old = exts;
4367 max *= 2;
4368 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4369 memcpy(exts, old, sizeof(*exts) * nr);
4370 if (old != *extents)
4371 kfree(old);
4372 }
4373
4374 exts[nr].disk_bytenr =
4375 btrfs_file_extent_disk_bytenr(leaf, fi);
4376 exts[nr].disk_num_bytes =
4377 btrfs_file_extent_disk_num_bytes(leaf, fi);
4378 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4379 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04004380 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4381 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4382 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4383 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4384 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04004385 BUG_ON(exts[nr].offset > 0);
4386 BUG_ON(exts[nr].compression || exts[nr].encryption);
4387 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004388
4389 cur_pos += exts[nr].num_bytes;
4390 nr++;
4391
4392 if (cur_pos + offset >= last_byte)
4393 break;
4394
4395 if (no_fragment) {
4396 ret = 1;
4397 goto out;
4398 }
4399 path->slots[0]++;
4400 }
4401
4402 WARN_ON(cur_pos + offset > last_byte);
4403 if (cur_pos + offset < last_byte) {
4404 ret = -ENOENT;
4405 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05004406 }
4407 ret = 0;
4408out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004409 btrfs_free_path(path);
4410 if (ret) {
4411 if (exts != *extents)
4412 kfree(exts);
4413 } else {
4414 *extents = exts;
4415 *nr_extents = nr;
4416 }
4417 return ret;
4418}
4419
4420static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
4421 struct btrfs_root *root,
4422 struct btrfs_path *path,
4423 struct btrfs_key *extent_key,
4424 struct btrfs_key *leaf_key,
4425 struct btrfs_ref_path *ref_path,
4426 struct disk_extent *new_extents,
4427 int nr_extents)
4428{
4429 struct extent_buffer *leaf;
4430 struct btrfs_file_extent_item *fi;
4431 struct inode *inode = NULL;
4432 struct btrfs_key key;
4433 u64 lock_start = 0;
4434 u64 lock_end = 0;
4435 u64 num_bytes;
4436 u64 ext_offset;
4437 u64 first_pos;
4438 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004439 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004440 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04004441 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04004442 int ret;
4443
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004444 memcpy(&key, leaf_key, sizeof(key));
4445 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004446 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004447 if (key.objectid < ref_path->owner_objectid ||
4448 (key.objectid == ref_path->owner_objectid &&
4449 key.type < BTRFS_EXTENT_DATA_KEY)) {
4450 key.objectid = ref_path->owner_objectid;
4451 key.type = BTRFS_EXTENT_DATA_KEY;
4452 key.offset = 0;
4453 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004454 }
4455
4456 while (1) {
4457 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4458 if (ret < 0)
4459 goto out;
4460
4461 leaf = path->nodes[0];
4462 nritems = btrfs_header_nritems(leaf);
4463next:
4464 if (extent_locked && ret > 0) {
4465 /*
4466 * the file extent item was modified by someone
4467 * before the extent got locked.
4468 */
Zheng Yan1a40e232008-09-26 10:09:34 -04004469 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4470 lock_end, GFP_NOFS);
4471 extent_locked = 0;
4472 }
4473
4474 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004475 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04004476 break;
4477
4478 BUG_ON(extent_locked);
4479 ret = btrfs_next_leaf(root, path);
4480 if (ret < 0)
4481 goto out;
4482 if (ret > 0)
4483 break;
4484 leaf = path->nodes[0];
4485 nritems = btrfs_header_nritems(leaf);
4486 }
4487
4488 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4489
4490 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4491 if ((key.objectid > ref_path->owner_objectid) ||
4492 (key.objectid == ref_path->owner_objectid &&
4493 key.type > BTRFS_EXTENT_DATA_KEY) ||
4494 (key.offset >= first_pos + extent_key->offset))
4495 break;
4496 }
4497
4498 if (inode && key.objectid != inode->i_ino) {
4499 BUG_ON(extent_locked);
4500 btrfs_release_path(root, path);
4501 mutex_unlock(&inode->i_mutex);
4502 iput(inode);
4503 inode = NULL;
4504 continue;
4505 }
4506
4507 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4508 path->slots[0]++;
4509 ret = 1;
4510 goto next;
4511 }
4512 fi = btrfs_item_ptr(leaf, path->slots[0],
4513 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04004514 extent_type = btrfs_file_extent_type(leaf, fi);
4515 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4516 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04004517 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4518 extent_key->objectid)) {
4519 path->slots[0]++;
4520 ret = 1;
4521 goto next;
4522 }
4523
4524 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4525 ext_offset = btrfs_file_extent_offset(leaf, fi);
4526
4527 if (first_pos > key.offset - ext_offset)
4528 first_pos = key.offset - ext_offset;
4529
4530 if (!extent_locked) {
4531 lock_start = key.offset;
4532 lock_end = lock_start + num_bytes - 1;
4533 } else {
Yan Zheng66435582008-10-30 14:19:50 -04004534 if (lock_start > key.offset ||
4535 lock_end + 1 < key.offset + num_bytes) {
4536 unlock_extent(&BTRFS_I(inode)->io_tree,
4537 lock_start, lock_end, GFP_NOFS);
4538 extent_locked = 0;
4539 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004540 }
4541
4542 if (!inode) {
4543 btrfs_release_path(root, path);
4544
4545 inode = btrfs_iget_locked(root->fs_info->sb,
4546 key.objectid, root);
4547 if (inode->i_state & I_NEW) {
4548 BTRFS_I(inode)->root = root;
4549 BTRFS_I(inode)->location.objectid =
4550 key.objectid;
4551 BTRFS_I(inode)->location.type =
4552 BTRFS_INODE_ITEM_KEY;
4553 BTRFS_I(inode)->location.offset = 0;
4554 btrfs_read_locked_inode(inode);
4555 unlock_new_inode(inode);
4556 }
4557 /*
4558 * some code call btrfs_commit_transaction while
4559 * holding the i_mutex, so we can't use mutex_lock
4560 * here.
4561 */
4562 if (is_bad_inode(inode) ||
4563 !mutex_trylock(&inode->i_mutex)) {
4564 iput(inode);
4565 inode = NULL;
4566 key.offset = (u64)-1;
4567 goto skip;
4568 }
4569 }
4570
4571 if (!extent_locked) {
4572 struct btrfs_ordered_extent *ordered;
4573
4574 btrfs_release_path(root, path);
4575
4576 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4577 lock_end, GFP_NOFS);
4578 ordered = btrfs_lookup_first_ordered_extent(inode,
4579 lock_end);
4580 if (ordered &&
4581 ordered->file_offset <= lock_end &&
4582 ordered->file_offset + ordered->len > lock_start) {
4583 unlock_extent(&BTRFS_I(inode)->io_tree,
4584 lock_start, lock_end, GFP_NOFS);
4585 btrfs_start_ordered_extent(inode, ordered, 1);
4586 btrfs_put_ordered_extent(ordered);
4587 key.offset += num_bytes;
4588 goto skip;
4589 }
4590 if (ordered)
4591 btrfs_put_ordered_extent(ordered);
4592
Zheng Yan1a40e232008-09-26 10:09:34 -04004593 extent_locked = 1;
4594 continue;
4595 }
4596
4597 if (nr_extents == 1) {
4598 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04004599 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4600 new_extents[0].disk_bytenr);
4601 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4602 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004603 btrfs_mark_buffer_dirty(leaf);
4604
4605 btrfs_drop_extent_cache(inode, key.offset,
4606 key.offset + num_bytes - 1, 0);
4607
4608 ret = btrfs_inc_extent_ref(trans, root,
4609 new_extents[0].disk_bytenr,
4610 new_extents[0].disk_num_bytes,
4611 leaf->start,
4612 root->root_key.objectid,
4613 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004614 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004615 BUG_ON(ret);
4616
4617 ret = btrfs_free_extent(trans, root,
4618 extent_key->objectid,
4619 extent_key->offset,
4620 leaf->start,
4621 btrfs_header_owner(leaf),
4622 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004623 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004624 BUG_ON(ret);
4625
4626 btrfs_release_path(root, path);
4627 key.offset += num_bytes;
4628 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04004629 BUG_ON(1);
4630#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04004631 u64 alloc_hint;
4632 u64 extent_len;
4633 int i;
4634 /*
4635 * drop old extent pointer at first, then insert the
4636 * new pointers one bye one
4637 */
4638 btrfs_release_path(root, path);
4639 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4640 key.offset + num_bytes,
4641 key.offset, &alloc_hint);
4642 BUG_ON(ret);
4643
4644 for (i = 0; i < nr_extents; i++) {
4645 if (ext_offset >= new_extents[i].num_bytes) {
4646 ext_offset -= new_extents[i].num_bytes;
4647 continue;
4648 }
4649 extent_len = min(new_extents[i].num_bytes -
4650 ext_offset, num_bytes);
4651
4652 ret = btrfs_insert_empty_item(trans, root,
4653 path, &key,
4654 sizeof(*fi));
4655 BUG_ON(ret);
4656
4657 leaf = path->nodes[0];
4658 fi = btrfs_item_ptr(leaf, path->slots[0],
4659 struct btrfs_file_extent_item);
4660 btrfs_set_file_extent_generation(leaf, fi,
4661 trans->transid);
4662 btrfs_set_file_extent_type(leaf, fi,
4663 BTRFS_FILE_EXTENT_REG);
4664 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4665 new_extents[i].disk_bytenr);
4666 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4667 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004668 btrfs_set_file_extent_ram_bytes(leaf, fi,
4669 new_extents[i].ram_bytes);
4670
4671 btrfs_set_file_extent_compression(leaf, fi,
4672 new_extents[i].compression);
4673 btrfs_set_file_extent_encryption(leaf, fi,
4674 new_extents[i].encryption);
4675 btrfs_set_file_extent_other_encoding(leaf, fi,
4676 new_extents[i].other_encoding);
4677
Zheng Yan1a40e232008-09-26 10:09:34 -04004678 btrfs_set_file_extent_num_bytes(leaf, fi,
4679 extent_len);
4680 ext_offset += new_extents[i].offset;
4681 btrfs_set_file_extent_offset(leaf, fi,
4682 ext_offset);
4683 btrfs_mark_buffer_dirty(leaf);
4684
4685 btrfs_drop_extent_cache(inode, key.offset,
4686 key.offset + extent_len - 1, 0);
4687
4688 ret = btrfs_inc_extent_ref(trans, root,
4689 new_extents[i].disk_bytenr,
4690 new_extents[i].disk_num_bytes,
4691 leaf->start,
4692 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004693 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004694 BUG_ON(ret);
4695 btrfs_release_path(root, path);
4696
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004697 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004698
4699 ext_offset = 0;
4700 num_bytes -= extent_len;
4701 key.offset += extent_len;
4702
4703 if (num_bytes == 0)
4704 break;
4705 }
4706 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004707#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04004708 }
4709
4710 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004711 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4712 lock_end, GFP_NOFS);
4713 extent_locked = 0;
4714 }
4715skip:
4716 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4717 key.offset >= first_pos + extent_key->offset)
4718 break;
4719
4720 cond_resched();
4721 }
4722 ret = 0;
4723out:
4724 btrfs_release_path(root, path);
4725 if (inode) {
4726 mutex_unlock(&inode->i_mutex);
4727 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004728 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4729 lock_end, GFP_NOFS);
4730 }
4731 iput(inode);
4732 }
4733 return ret;
4734}
4735
Zheng Yan1a40e232008-09-26 10:09:34 -04004736int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4737 struct btrfs_root *root,
4738 struct extent_buffer *buf, u64 orig_start)
4739{
4740 int level;
4741 int ret;
4742
4743 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4744 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4745
4746 level = btrfs_header_level(buf);
4747 if (level == 0) {
4748 struct btrfs_leaf_ref *ref;
4749 struct btrfs_leaf_ref *orig_ref;
4750
4751 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4752 if (!orig_ref)
4753 return -ENOENT;
4754
4755 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4756 if (!ref) {
4757 btrfs_free_leaf_ref(root, orig_ref);
4758 return -ENOMEM;
4759 }
4760
4761 ref->nritems = orig_ref->nritems;
4762 memcpy(ref->extents, orig_ref->extents,
4763 sizeof(ref->extents[0]) * ref->nritems);
4764
4765 btrfs_free_leaf_ref(root, orig_ref);
4766
4767 ref->root_gen = trans->transid;
4768 ref->bytenr = buf->start;
4769 ref->owner = btrfs_header_owner(buf);
4770 ref->generation = btrfs_header_generation(buf);
4771 ret = btrfs_add_leaf_ref(root, ref, 0);
4772 WARN_ON(ret);
4773 btrfs_free_leaf_ref(root, ref);
4774 }
4775 return 0;
4776}
4777
4778static int noinline invalidate_extent_cache(struct btrfs_root *root,
4779 struct extent_buffer *leaf,
4780 struct btrfs_block_group_cache *group,
4781 struct btrfs_root *target_root)
4782{
4783 struct btrfs_key key;
4784 struct inode *inode = NULL;
4785 struct btrfs_file_extent_item *fi;
4786 u64 num_bytes;
4787 u64 skip_objectid = 0;
4788 u32 nritems;
4789 u32 i;
4790
4791 nritems = btrfs_header_nritems(leaf);
4792 for (i = 0; i < nritems; i++) {
4793 btrfs_item_key_to_cpu(leaf, &key, i);
4794 if (key.objectid == skip_objectid ||
4795 key.type != BTRFS_EXTENT_DATA_KEY)
4796 continue;
4797 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4798 if (btrfs_file_extent_type(leaf, fi) ==
4799 BTRFS_FILE_EXTENT_INLINE)
4800 continue;
4801 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4802 continue;
4803 if (!inode || inode->i_ino != key.objectid) {
4804 iput(inode);
4805 inode = btrfs_ilookup(target_root->fs_info->sb,
4806 key.objectid, target_root, 1);
4807 }
4808 if (!inode) {
4809 skip_objectid = key.objectid;
4810 continue;
4811 }
4812 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4813
4814 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4815 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004816 btrfs_drop_extent_cache(inode, key.offset,
4817 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004818 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4819 key.offset + num_bytes - 1, GFP_NOFS);
4820 cond_resched();
4821 }
4822 iput(inode);
4823 return 0;
4824}
4825
4826static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4827 struct btrfs_root *root,
4828 struct extent_buffer *leaf,
4829 struct btrfs_block_group_cache *group,
4830 struct inode *reloc_inode)
4831{
4832 struct btrfs_key key;
4833 struct btrfs_key extent_key;
4834 struct btrfs_file_extent_item *fi;
4835 struct btrfs_leaf_ref *ref;
4836 struct disk_extent *new_extent;
4837 u64 bytenr;
4838 u64 num_bytes;
4839 u32 nritems;
4840 u32 i;
4841 int ext_index;
4842 int nr_extent;
4843 int ret;
4844
4845 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4846 BUG_ON(!new_extent);
4847
4848 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4849 BUG_ON(!ref);
4850
4851 ext_index = -1;
4852 nritems = btrfs_header_nritems(leaf);
4853 for (i = 0; i < nritems; i++) {
4854 btrfs_item_key_to_cpu(leaf, &key, i);
4855 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4856 continue;
4857 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4858 if (btrfs_file_extent_type(leaf, fi) ==
4859 BTRFS_FILE_EXTENT_INLINE)
4860 continue;
4861 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4862 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4863 if (bytenr == 0)
4864 continue;
4865
4866 ext_index++;
4867 if (bytenr >= group->key.objectid + group->key.offset ||
4868 bytenr + num_bytes <= group->key.objectid)
4869 continue;
4870
4871 extent_key.objectid = bytenr;
4872 extent_key.offset = num_bytes;
4873 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4874 nr_extent = 1;
4875 ret = get_new_locations(reloc_inode, &extent_key,
4876 group->key.objectid, 1,
4877 &new_extent, &nr_extent);
4878 if (ret > 0)
4879 continue;
4880 BUG_ON(ret < 0);
4881
4882 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4883 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4884 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4885 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4886
Zheng Yan1a40e232008-09-26 10:09:34 -04004887 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4888 new_extent->disk_bytenr);
4889 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4890 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004891 btrfs_mark_buffer_dirty(leaf);
4892
4893 ret = btrfs_inc_extent_ref(trans, root,
4894 new_extent->disk_bytenr,
4895 new_extent->disk_num_bytes,
4896 leaf->start,
4897 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004898 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004899 BUG_ON(ret);
4900 ret = btrfs_free_extent(trans, root,
4901 bytenr, num_bytes, leaf->start,
4902 btrfs_header_owner(leaf),
4903 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004904 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004905 BUG_ON(ret);
4906 cond_resched();
4907 }
4908 kfree(new_extent);
4909 BUG_ON(ext_index + 1 != ref->nritems);
4910 btrfs_free_leaf_ref(root, ref);
4911 return 0;
4912}
4913
Yan Zhengf82d02d2008-10-29 14:49:05 -04004914int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4915 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004916{
4917 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004918 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004919
4920 if (root->reloc_root) {
4921 reloc_root = root->reloc_root;
4922 root->reloc_root = NULL;
4923 list_add(&reloc_root->dead_list,
4924 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004925
4926 btrfs_set_root_bytenr(&reloc_root->root_item,
4927 reloc_root->node->start);
4928 btrfs_set_root_level(&root->root_item,
4929 btrfs_header_level(reloc_root->node));
4930 memset(&reloc_root->root_item.drop_progress, 0,
4931 sizeof(struct btrfs_disk_key));
4932 reloc_root->root_item.drop_level = 0;
4933
4934 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4935 &reloc_root->root_key,
4936 &reloc_root->root_item);
4937 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004938 }
4939 return 0;
4940}
4941
4942int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4943{
4944 struct btrfs_trans_handle *trans;
4945 struct btrfs_root *reloc_root;
4946 struct btrfs_root *prev_root = NULL;
4947 struct list_head dead_roots;
4948 int ret;
4949 unsigned long nr;
4950
4951 INIT_LIST_HEAD(&dead_roots);
4952 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4953
4954 while (!list_empty(&dead_roots)) {
4955 reloc_root = list_entry(dead_roots.prev,
4956 struct btrfs_root, dead_list);
4957 list_del_init(&reloc_root->dead_list);
4958
4959 BUG_ON(reloc_root->commit_root != NULL);
4960 while (1) {
4961 trans = btrfs_join_transaction(root, 1);
4962 BUG_ON(!trans);
4963
4964 mutex_lock(&root->fs_info->drop_mutex);
4965 ret = btrfs_drop_snapshot(trans, reloc_root);
4966 if (ret != -EAGAIN)
4967 break;
4968 mutex_unlock(&root->fs_info->drop_mutex);
4969
4970 nr = trans->blocks_used;
4971 ret = btrfs_end_transaction(trans, root);
4972 BUG_ON(ret);
4973 btrfs_btree_balance_dirty(root, nr);
4974 }
4975
4976 free_extent_buffer(reloc_root->node);
4977
4978 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4979 &reloc_root->root_key);
4980 BUG_ON(ret);
4981 mutex_unlock(&root->fs_info->drop_mutex);
4982
4983 nr = trans->blocks_used;
4984 ret = btrfs_end_transaction(trans, root);
4985 BUG_ON(ret);
4986 btrfs_btree_balance_dirty(root, nr);
4987
4988 kfree(prev_root);
4989 prev_root = reloc_root;
4990 }
4991 if (prev_root) {
4992 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4993 kfree(prev_root);
4994 }
4995 return 0;
4996}
4997
4998int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4999{
5000 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
5001 return 0;
5002}
5003
5004int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
5005{
5006 struct btrfs_root *reloc_root;
5007 struct btrfs_trans_handle *trans;
5008 struct btrfs_key location;
5009 int found;
5010 int ret;
5011
5012 mutex_lock(&root->fs_info->tree_reloc_mutex);
5013 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
5014 BUG_ON(ret);
5015 found = !list_empty(&root->fs_info->dead_reloc_roots);
5016 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5017
5018 if (found) {
5019 trans = btrfs_start_transaction(root, 1);
5020 BUG_ON(!trans);
5021 ret = btrfs_commit_transaction(trans, root);
5022 BUG_ON(ret);
5023 }
5024
5025 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5026 location.offset = (u64)-1;
5027 location.type = BTRFS_ROOT_ITEM_KEY;
5028
5029 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5030 BUG_ON(!reloc_root);
5031 btrfs_orphan_cleanup(reloc_root);
5032 return 0;
5033}
5034
5035static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
5036 struct btrfs_root *root)
5037{
5038 struct btrfs_root *reloc_root;
5039 struct extent_buffer *eb;
5040 struct btrfs_root_item *root_item;
5041 struct btrfs_key root_key;
5042 int ret;
5043
5044 BUG_ON(!root->ref_cows);
5045 if (root->reloc_root)
5046 return 0;
5047
5048 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5049 BUG_ON(!root_item);
5050
5051 ret = btrfs_copy_root(trans, root, root->commit_root,
5052 &eb, BTRFS_TREE_RELOC_OBJECTID);
5053 BUG_ON(ret);
5054
5055 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5056 root_key.offset = root->root_key.objectid;
5057 root_key.type = BTRFS_ROOT_ITEM_KEY;
5058
5059 memcpy(root_item, &root->root_item, sizeof(root_item));
5060 btrfs_set_root_refs(root_item, 0);
5061 btrfs_set_root_bytenr(root_item, eb->start);
5062 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04005063 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005064
5065 btrfs_tree_unlock(eb);
5066 free_extent_buffer(eb);
5067
5068 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5069 &root_key, root_item);
5070 BUG_ON(ret);
5071 kfree(root_item);
5072
5073 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5074 &root_key);
5075 BUG_ON(!reloc_root);
5076 reloc_root->last_trans = trans->transid;
5077 reloc_root->commit_root = NULL;
5078 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5079
5080 root->reloc_root = reloc_root;
5081 return 0;
5082}
5083
5084/*
5085 * Core function of space balance.
5086 *
5087 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04005088 * counted roots. There is one reloc tree for each subvol, and all
5089 * reloc trees share same root key objectid. Reloc trees are snapshots
5090 * of the latest committed roots of subvols (root->commit_root).
5091 *
5092 * To relocate a tree block referenced by a subvol, there are two steps.
5093 * COW the block through subvol's reloc tree, then update block pointer
5094 * in the subvol to point to the new block. Since all reloc trees share
5095 * same root key objectid, doing special handing for tree blocks owned
5096 * by them is easy. Once a tree block has been COWed in one reloc tree,
5097 * we can use the resulting new block directly when the same block is
5098 * required to COW again through other reloc trees. By this way, relocated
5099 * tree blocks are shared between reloc trees, so they are also shared
5100 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04005101 */
5102static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
5103 struct btrfs_root *root,
5104 struct btrfs_path *path,
5105 struct btrfs_key *first_key,
5106 struct btrfs_ref_path *ref_path,
5107 struct btrfs_block_group_cache *group,
5108 struct inode *reloc_inode)
5109{
5110 struct btrfs_root *reloc_root;
5111 struct extent_buffer *eb = NULL;
5112 struct btrfs_key *keys;
5113 u64 *nodes;
5114 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005115 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005116 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005117 int ret;
5118
5119 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5120 lowest_level = ref_path->owner_objectid;
5121
Yan Zhengf82d02d2008-10-29 14:49:05 -04005122 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005123 path->lowest_level = lowest_level;
5124 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5125 BUG_ON(ret < 0);
5126 path->lowest_level = 0;
5127 btrfs_release_path(root, path);
5128 return 0;
5129 }
5130
Zheng Yan1a40e232008-09-26 10:09:34 -04005131 mutex_lock(&root->fs_info->tree_reloc_mutex);
5132 ret = init_reloc_tree(trans, root);
5133 BUG_ON(ret);
5134 reloc_root = root->reloc_root;
5135
Yan Zhengf82d02d2008-10-29 14:49:05 -04005136 shared_level = ref_path->shared_level;
5137 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005138
Yan Zhengf82d02d2008-10-29 14:49:05 -04005139 keys = ref_path->node_keys;
5140 nodes = ref_path->new_nodes;
5141 memset(&keys[shared_level + 1], 0,
5142 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5143 memset(&nodes[shared_level + 1], 0,
5144 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04005145
Yan Zhengf82d02d2008-10-29 14:49:05 -04005146 if (nodes[lowest_level] == 0) {
5147 path->lowest_level = lowest_level;
5148 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5149 0, 1);
5150 BUG_ON(ret);
5151 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5152 eb = path->nodes[level];
5153 if (!eb || eb == reloc_root->node)
5154 break;
5155 nodes[level] = eb->start;
5156 if (level == 0)
5157 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5158 else
5159 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5160 }
Yan Zheng2b820322008-11-17 21:11:30 -05005161 if (nodes[0] &&
5162 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005163 eb = path->nodes[0];
5164 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5165 group, reloc_inode);
5166 BUG_ON(ret);
5167 }
5168 btrfs_release_path(reloc_root, path);
5169 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005170 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005171 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04005172 BUG_ON(ret);
5173 }
5174
Zheng Yan1a40e232008-09-26 10:09:34 -04005175 /*
5176 * replace tree blocks in the fs tree with tree blocks in
5177 * the reloc tree.
5178 */
5179 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5180 BUG_ON(ret < 0);
5181
5182 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005183 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5184 0, 0);
5185 BUG_ON(ret);
5186 extent_buffer_get(path->nodes[0]);
5187 eb = path->nodes[0];
5188 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005189 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5190 BUG_ON(ret);
5191 free_extent_buffer(eb);
5192 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005193
Yan Zhengf82d02d2008-10-29 14:49:05 -04005194 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005195 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005196 return 0;
5197}
5198
5199static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
5200 struct btrfs_root *root,
5201 struct btrfs_path *path,
5202 struct btrfs_key *first_key,
5203 struct btrfs_ref_path *ref_path)
5204{
5205 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005206
5207 ret = relocate_one_path(trans, root, path, first_key,
5208 ref_path, NULL, NULL);
5209 BUG_ON(ret);
5210
5211 if (root == root->fs_info->extent_root)
5212 btrfs_extent_post_op(trans, root);
Zheng Yan1a40e232008-09-26 10:09:34 -04005213
5214 return 0;
5215}
5216
5217static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
5218 struct btrfs_root *extent_root,
5219 struct btrfs_path *path,
5220 struct btrfs_key *extent_key)
5221{
5222 int ret;
5223
Zheng Yan1a40e232008-09-26 10:09:34 -04005224 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5225 if (ret)
5226 goto out;
5227 ret = btrfs_del_item(trans, extent_root, path);
5228out:
Chris Masonedbd8d42007-12-21 16:27:24 -05005229 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005230 return ret;
5231}
5232
5233static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
5234 struct btrfs_ref_path *ref_path)
5235{
5236 struct btrfs_key root_key;
5237
5238 root_key.objectid = ref_path->root_objectid;
5239 root_key.type = BTRFS_ROOT_ITEM_KEY;
5240 if (is_cowonly_root(ref_path->root_objectid))
5241 root_key.offset = 0;
5242 else
5243 root_key.offset = (u64)-1;
5244
5245 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5246}
5247
5248static int noinline relocate_one_extent(struct btrfs_root *extent_root,
5249 struct btrfs_path *path,
5250 struct btrfs_key *extent_key,
5251 struct btrfs_block_group_cache *group,
5252 struct inode *reloc_inode, int pass)
5253{
5254 struct btrfs_trans_handle *trans;
5255 struct btrfs_root *found_root;
5256 struct btrfs_ref_path *ref_path = NULL;
5257 struct disk_extent *new_extents = NULL;
5258 int nr_extents = 0;
5259 int loops;
5260 int ret;
5261 int level;
5262 struct btrfs_key first_key;
5263 u64 prev_block = 0;
5264
Zheng Yan1a40e232008-09-26 10:09:34 -04005265
5266 trans = btrfs_start_transaction(extent_root, 1);
5267 BUG_ON(!trans);
5268
5269 if (extent_key->objectid == 0) {
5270 ret = del_extent_zero(trans, extent_root, path, extent_key);
5271 goto out;
5272 }
5273
5274 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5275 if (!ref_path) {
5276 ret = -ENOMEM;
5277 goto out;
5278 }
5279
5280 for (loops = 0; ; loops++) {
5281 if (loops == 0) {
5282 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5283 extent_key->objectid);
5284 } else {
5285 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5286 }
5287 if (ret < 0)
5288 goto out;
5289 if (ret > 0)
5290 break;
5291
5292 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5293 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5294 continue;
5295
5296 found_root = read_ref_root(extent_root->fs_info, ref_path);
5297 BUG_ON(!found_root);
5298 /*
5299 * for reference counted tree, only process reference paths
5300 * rooted at the latest committed root.
5301 */
5302 if (found_root->ref_cows &&
5303 ref_path->root_generation != found_root->root_key.offset)
5304 continue;
5305
5306 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5307 if (pass == 0) {
5308 /*
5309 * copy data extents to new locations
5310 */
5311 u64 group_start = group->key.objectid;
5312 ret = relocate_data_extent(reloc_inode,
5313 extent_key,
5314 group_start);
5315 if (ret < 0)
5316 goto out;
5317 break;
5318 }
5319 level = 0;
5320 } else {
5321 level = ref_path->owner_objectid;
5322 }
5323
5324 if (prev_block != ref_path->nodes[level]) {
5325 struct extent_buffer *eb;
5326 u64 block_start = ref_path->nodes[level];
5327 u64 block_size = btrfs_level_size(found_root, level);
5328
5329 eb = read_tree_block(found_root, block_start,
5330 block_size, 0);
5331 btrfs_tree_lock(eb);
5332 BUG_ON(level != btrfs_header_level(eb));
5333
5334 if (level == 0)
5335 btrfs_item_key_to_cpu(eb, &first_key, 0);
5336 else
5337 btrfs_node_key_to_cpu(eb, &first_key, 0);
5338
5339 btrfs_tree_unlock(eb);
5340 free_extent_buffer(eb);
5341 prev_block = block_start;
5342 }
5343
5344 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
5345 pass >= 2) {
5346 /*
5347 * use fallback method to process the remaining
5348 * references.
5349 */
5350 if (!new_extents) {
5351 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04005352 new_extents = kmalloc(sizeof(*new_extents),
5353 GFP_NOFS);
5354 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005355 ret = get_new_locations(reloc_inode,
5356 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04005357 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04005358 &new_extents,
5359 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005360 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04005361 goto out;
5362 }
5363 btrfs_record_root_in_trans(found_root);
5364 ret = replace_one_extent(trans, found_root,
5365 path, extent_key,
5366 &first_key, ref_path,
5367 new_extents, nr_extents);
5368 if (ret < 0)
5369 goto out;
5370 continue;
5371 }
5372
5373 btrfs_record_root_in_trans(found_root);
5374 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5375 ret = relocate_tree_block(trans, found_root, path,
5376 &first_key, ref_path);
5377 } else {
5378 /*
5379 * try to update data extent references while
5380 * keeping metadata shared between snapshots.
5381 */
5382 ret = relocate_one_path(trans, found_root, path,
5383 &first_key, ref_path,
5384 group, reloc_inode);
5385 }
5386 if (ret < 0)
5387 goto out;
5388 }
5389 ret = 0;
5390out:
5391 btrfs_end_transaction(trans, extent_root);
5392 kfree(new_extents);
5393 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005394 return ret;
5395}
5396
Chris Masonec44a352008-04-28 15:29:52 -04005397static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5398{
5399 u64 num_devices;
5400 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5401 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5402
Yan Zheng2b820322008-11-17 21:11:30 -05005403 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04005404 if (num_devices == 1) {
5405 stripped |= BTRFS_BLOCK_GROUP_DUP;
5406 stripped = flags & ~stripped;
5407
5408 /* turn raid0 into single device chunks */
5409 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5410 return stripped;
5411
5412 /* turn mirroring into duplication */
5413 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5414 BTRFS_BLOCK_GROUP_RAID10))
5415 return stripped | BTRFS_BLOCK_GROUP_DUP;
5416 return flags;
5417 } else {
5418 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04005419 if (flags & stripped)
5420 return flags;
5421
5422 stripped |= BTRFS_BLOCK_GROUP_DUP;
5423 stripped = flags & ~stripped;
5424
5425 /* switch duplicated blocks with raid1 */
5426 if (flags & BTRFS_BLOCK_GROUP_DUP)
5427 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5428
5429 /* turn single device chunks into raid0 */
5430 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5431 }
5432 return flags;
5433}
5434
Chris Mason0ef3e662008-05-24 14:04:53 -04005435int __alloc_chunk_for_shrink(struct btrfs_root *root,
5436 struct btrfs_block_group_cache *shrink_block_group,
5437 int force)
5438{
5439 struct btrfs_trans_handle *trans;
5440 u64 new_alloc_flags;
5441 u64 calc;
5442
Chris Masonc286ac42008-07-22 23:06:41 -04005443 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005444 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04005445 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005446
Chris Mason0ef3e662008-05-24 14:04:53 -04005447 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005448 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04005449
Chris Mason0ef3e662008-05-24 14:04:53 -04005450 new_alloc_flags = update_block_group_flags(root,
5451 shrink_block_group->flags);
5452 if (new_alloc_flags != shrink_block_group->flags) {
5453 calc =
5454 btrfs_block_group_used(&shrink_block_group->item);
5455 } else {
5456 calc = shrink_block_group->key.offset;
5457 }
Chris Masonc286ac42008-07-22 23:06:41 -04005458 spin_unlock(&shrink_block_group->lock);
5459
Chris Mason0ef3e662008-05-24 14:04:53 -04005460 do_chunk_alloc(trans, root->fs_info->extent_root,
5461 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04005462
Chris Mason0ef3e662008-05-24 14:04:53 -04005463 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04005464 } else
5465 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005466 return 0;
5467}
5468
Zheng Yan1a40e232008-09-26 10:09:34 -04005469static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5470 struct btrfs_root *root,
5471 u64 objectid, u64 size)
5472{
5473 struct btrfs_path *path;
5474 struct btrfs_inode_item *item;
5475 struct extent_buffer *leaf;
5476 int ret;
5477
5478 path = btrfs_alloc_path();
5479 if (!path)
5480 return -ENOMEM;
5481
5482 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5483 if (ret)
5484 goto out;
5485
5486 leaf = path->nodes[0];
5487 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5488 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5489 btrfs_set_inode_generation(leaf, item, 1);
5490 btrfs_set_inode_size(leaf, item, size);
5491 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zhengd899e052008-10-30 14:25:28 -04005492 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM |
5493 BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005494 btrfs_mark_buffer_dirty(leaf);
5495 btrfs_release_path(root, path);
5496out:
5497 btrfs_free_path(path);
5498 return ret;
5499}
5500
5501static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
5502 struct btrfs_block_group_cache *group)
5503{
5504 struct inode *inode = NULL;
5505 struct btrfs_trans_handle *trans;
5506 struct btrfs_root *root;
5507 struct btrfs_key root_key;
5508 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5509 int err = 0;
5510
5511 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5512 root_key.type = BTRFS_ROOT_ITEM_KEY;
5513 root_key.offset = (u64)-1;
5514 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5515 if (IS_ERR(root))
5516 return ERR_CAST(root);
5517
5518 trans = btrfs_start_transaction(root, 1);
5519 BUG_ON(!trans);
5520
5521 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5522 if (err)
5523 goto out;
5524
5525 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5526 BUG_ON(err);
5527
5528 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04005529 group->key.offset, 0, group->key.offset,
5530 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005531 BUG_ON(err);
5532
5533 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5534 if (inode->i_state & I_NEW) {
5535 BTRFS_I(inode)->root = root;
5536 BTRFS_I(inode)->location.objectid = objectid;
5537 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5538 BTRFS_I(inode)->location.offset = 0;
5539 btrfs_read_locked_inode(inode);
5540 unlock_new_inode(inode);
5541 BUG_ON(is_bad_inode(inode));
5542 } else {
5543 BUG_ON(1);
5544 }
5545
5546 err = btrfs_orphan_add(trans, inode);
5547out:
5548 btrfs_end_transaction(trans, root);
5549 if (err) {
5550 if (inode)
5551 iput(inode);
5552 inode = ERR_PTR(err);
5553 }
5554 return inode;
5555}
5556
5557int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05005558{
5559 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05005560 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04005561 struct btrfs_fs_info *info = root->fs_info;
5562 struct extent_buffer *leaf;
5563 struct inode *reloc_inode;
5564 struct btrfs_block_group_cache *block_group;
5565 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04005566 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05005567 u64 cur_byte;
5568 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05005569 u32 nritems;
5570 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04005571 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04005572 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005573
Chris Masonedbd8d42007-12-21 16:27:24 -05005574 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04005575
5576 block_group = btrfs_lookup_block_group(info, group_start);
5577 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05005578
Chris Mason323da792008-05-09 11:46:48 -04005579 printk("btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005580 (unsigned long long)block_group->key.objectid,
5581 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04005582
Zheng Yan1a40e232008-09-26 10:09:34 -04005583 path = btrfs_alloc_path();
5584 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04005585
Zheng Yan1a40e232008-09-26 10:09:34 -04005586 reloc_inode = create_reloc_inode(info, block_group);
5587 BUG_ON(IS_ERR(reloc_inode));
5588
Zheng Yan1a40e232008-09-26 10:09:34 -04005589 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05005590 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005591
Zheng Yan1a40e232008-09-26 10:09:34 -04005592 btrfs_start_delalloc_inodes(info->tree_root);
5593 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04005594again:
Yan Zhengd899e052008-10-30 14:25:28 -04005595 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005596 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04005597 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005598 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05005599 key.offset = 0;
5600 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05005601 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05005602
Zheng Yan1a40e232008-09-26 10:09:34 -04005603 trans = btrfs_start_transaction(info->tree_root, 1);
5604 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04005605
Zheng Yan1a40e232008-09-26 10:09:34 -04005606 mutex_lock(&root->fs_info->cleaner_mutex);
5607 btrfs_clean_old_snapshots(info->tree_root);
5608 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5609 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04005610
Yan73e48b22008-01-03 14:14:39 -05005611 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05005612 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5613 if (ret < 0)
5614 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04005615next:
Chris Masonedbd8d42007-12-21 16:27:24 -05005616 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05005617 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05005618 if (path->slots[0] >= nritems) {
5619 ret = btrfs_next_leaf(root, path);
5620 if (ret < 0)
5621 goto out;
5622 if (ret == 1) {
5623 ret = 0;
5624 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005625 }
Yan73e48b22008-01-03 14:14:39 -05005626 leaf = path->nodes[0];
5627 nritems = btrfs_header_nritems(leaf);
5628 }
5629
Zheng Yan1a40e232008-09-26 10:09:34 -04005630 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05005631
Zheng Yan1a40e232008-09-26 10:09:34 -04005632 if (key.objectid >= block_group->key.objectid +
5633 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04005634 break;
5635
Chris Mason725c8462008-01-04 16:47:16 -05005636 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05005637 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005638 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05005639 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005640 continue;
Chris Mason725c8462008-01-04 16:47:16 -05005641 }
5642 progress = 1;
5643
Zheng Yan1a40e232008-09-26 10:09:34 -04005644 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5645 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05005646 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005647 goto next;
5648 }
Yan73e48b22008-01-03 14:14:39 -05005649
Chris Masonedbd8d42007-12-21 16:27:24 -05005650 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005651 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05005652 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005653
5654 __alloc_chunk_for_shrink(root, block_group, 0);
5655 ret = relocate_one_extent(root, path, &key, block_group,
5656 reloc_inode, pass);
5657 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005658 if (ret > 0)
5659 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005660
5661 key.objectid = cur_byte;
5662 key.type = 0;
5663 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005664 }
5665
5666 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005667
5668 if (pass == 0) {
5669 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5670 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5671 WARN_ON(reloc_inode->i_mapping->nrpages);
5672 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005673
5674 if (total_found > 0) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005675 printk("btrfs found %llu extents in pass %d\n",
5676 (unsigned long long)total_found, pass);
5677 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04005678 if (total_found == skipped && pass > 2) {
5679 iput(reloc_inode);
5680 reloc_inode = create_reloc_inode(info, block_group);
5681 pass = 0;
5682 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005683 goto again;
5684 }
5685
Zheng Yan1a40e232008-09-26 10:09:34 -04005686 /* delete reloc_inode */
5687 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005688
Zheng Yan1a40e232008-09-26 10:09:34 -04005689 /* unpin extents in this range */
5690 trans = btrfs_start_transaction(info->tree_root, 1);
5691 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005692
Zheng Yan1a40e232008-09-26 10:09:34 -04005693 spin_lock(&block_group->lock);
5694 WARN_ON(block_group->pinned > 0);
5695 WARN_ON(block_group->reserved > 0);
5696 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5697 spin_unlock(&block_group->lock);
5698 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005699out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005700 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005701 return ret;
5702}
5703
Chris Mason0b86a832008-03-24 15:01:56 -04005704int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
5705 struct btrfs_key *key)
5706{
Chris Mason925baed2008-06-25 16:01:30 -04005707 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005708 struct btrfs_key found_key;
5709 struct extent_buffer *leaf;
5710 int slot;
5711
5712 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5713 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005714 goto out;
5715
Chris Mason0b86a832008-03-24 15:01:56 -04005716 while(1) {
5717 slot = path->slots[0];
5718 leaf = path->nodes[0];
5719 if (slot >= btrfs_header_nritems(leaf)) {
5720 ret = btrfs_next_leaf(root, path);
5721 if (ret == 0)
5722 continue;
5723 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005724 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005725 break;
5726 }
5727 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5728
5729 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005730 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5731 ret = 0;
5732 goto out;
5733 }
Chris Mason0b86a832008-03-24 15:01:56 -04005734 path->slots[0]++;
5735 }
5736 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005737out:
Chris Mason0b86a832008-03-24 15:01:56 -04005738 return ret;
5739}
5740
Zheng Yan1a40e232008-09-26 10:09:34 -04005741int btrfs_free_block_groups(struct btrfs_fs_info *info)
5742{
5743 struct btrfs_block_group_cache *block_group;
5744 struct rb_node *n;
5745
Zheng Yan1a40e232008-09-26 10:09:34 -04005746 spin_lock(&info->block_group_cache_lock);
5747 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5748 block_group = rb_entry(n, struct btrfs_block_group_cache,
5749 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04005750 rb_erase(&block_group->cache_node,
5751 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04005752 spin_unlock(&info->block_group_cache_lock);
5753
5754 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005755 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005756 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005757 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005758 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04005759
5760 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005761 }
5762 spin_unlock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005763 return 0;
5764}
5765
Chris Mason9078a3e2007-04-26 16:46:15 -04005766int btrfs_read_block_groups(struct btrfs_root *root)
5767{
5768 struct btrfs_path *path;
5769 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005770 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005771 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005772 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005773 struct btrfs_key key;
5774 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005775 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005776
Chris Masonbe744172007-05-06 10:15:01 -04005777 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005778 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005779 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005780 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005781 path = btrfs_alloc_path();
5782 if (!path)
5783 return -ENOMEM;
5784
5785 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005786 ret = find_first_block_group(root, path, &key);
5787 if (ret > 0) {
5788 ret = 0;
5789 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005790 }
Chris Mason0b86a832008-03-24 15:01:56 -04005791 if (ret != 0)
5792 goto error;
5793
Chris Mason5f39d392007-10-15 16:14:19 -04005794 leaf = path->nodes[0];
5795 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005796 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005797 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005798 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005799 break;
5800 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005801
Chris Masonc286ac42008-07-22 23:06:41 -04005802 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005803 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005804 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005805 read_extent_buffer(leaf, &cache->item,
5806 btrfs_item_ptr_offset(leaf, path->slots[0]),
5807 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005808 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005809
Chris Mason9078a3e2007-04-26 16:46:15 -04005810 key.objectid = found_key.objectid + found_key.offset;
5811 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005812 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005813
Chris Mason6324fbf2008-03-24 15:01:59 -04005814 ret = update_space_info(info, cache->flags, found_key.offset,
5815 btrfs_block_group_used(&cache->item),
5816 &space_info);
5817 BUG_ON(ret);
5818 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005819 down_write(&space_info->groups_sem);
5820 list_add_tail(&cache->list, &space_info->block_groups);
5821 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005822
Josef Bacik0f9dd462008-09-23 13:14:11 -04005823 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5824 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005825
5826 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05005827 if (btrfs_chunk_readonly(root, cache->key.objectid))
5828 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04005829 }
Chris Mason0b86a832008-03-24 15:01:56 -04005830 ret = 0;
5831error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005832 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005833 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005834}
Chris Mason6324fbf2008-03-24 15:01:59 -04005835
5836int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5837 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005838 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005839 u64 size)
5840{
5841 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005842 struct btrfs_root *extent_root;
5843 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005844
5845 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005846
Chris Masone02119d2008-09-05 16:13:11 -04005847 root->fs_info->last_trans_new_blockgroup = trans->transid;
5848
Chris Mason8f18cf12008-04-25 16:53:30 -04005849 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005850 if (!cache)
5851 return -ENOMEM;
5852
Chris Masone17cade2008-04-15 15:41:47 -04005853 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005854 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04005855 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005856 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005857 INIT_LIST_HEAD(&cache->list);
Chris Mason6324fbf2008-03-24 15:01:59 -04005858 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04005859
Chris Mason6324fbf2008-03-24 15:01:59 -04005860 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005861 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5862 cache->flags = type;
5863 btrfs_set_block_group_flags(&cache->item, type);
5864
5865 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5866 &cache->space_info);
5867 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005868 down_write(&cache->space_info->groups_sem);
5869 list_add_tail(&cache->list, &cache->space_info->block_groups);
5870 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005871
Josef Bacik0f9dd462008-09-23 13:14:11 -04005872 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5873 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005874
Chris Mason6324fbf2008-03-24 15:01:59 -04005875 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5876 sizeof(cache->item));
5877 BUG_ON(ret);
5878
Chris Mason87ef2bb2008-10-30 11:23:27 -04005879 finish_current_insert(trans, extent_root, 0);
5880 ret = del_pending_extents(trans, extent_root, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04005881 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04005882 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005883
Chris Mason6324fbf2008-03-24 15:01:59 -04005884 return 0;
5885}
Zheng Yan1a40e232008-09-26 10:09:34 -04005886
5887int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5888 struct btrfs_root *root, u64 group_start)
5889{
5890 struct btrfs_path *path;
5891 struct btrfs_block_group_cache *block_group;
5892 struct btrfs_key key;
5893 int ret;
5894
Zheng Yan1a40e232008-09-26 10:09:34 -04005895 root = root->fs_info->extent_root;
5896
5897 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5898 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05005899 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04005900
5901 memcpy(&key, &block_group->key, sizeof(key));
5902
5903 path = btrfs_alloc_path();
5904 BUG_ON(!path);
5905
5906 btrfs_remove_free_space_cache(block_group);
5907 rb_erase(&block_group->cache_node,
5908 &root->fs_info->block_group_cache_tree);
Josef Bacik80eb2342008-10-29 14:49:05 -04005909 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005910 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005911 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005912
Yan Zhengc146afa2008-11-12 14:34:12 -05005913 spin_lock(&block_group->space_info->lock);
5914 block_group->space_info->total_bytes -= block_group->key.offset;
5915 block_group->space_info->bytes_readonly -= block_group->key.offset;
5916 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05005917 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05005918
Zheng Yan1a40e232008-09-26 10:09:34 -04005919 /*
5920 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5921 kfree(shrink_block_group);
5922 */
5923
5924 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5925 if (ret > 0)
5926 ret = -EIO;
5927 if (ret < 0)
5928 goto out;
5929
5930 ret = btrfs_del_item(trans, root, path);
5931out:
5932 btrfs_free_path(path);
5933 return ret;
5934}