blob: af2de30dbeac4ba89f394e62545128f554ec6ed8 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Mason74493f72007-12-11 09:25:06 -050022#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040023#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050024#include "ctree.h"
25#include "disk-io.h"
26#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040027#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040028#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040029#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040030#include "ref-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050031
Zheng Yan31840ae2008-09-23 13:14:14 -040032#define PENDING_EXTENT_INSERT 0
33#define PENDING_EXTENT_DELETE 1
34#define PENDING_BACKREF_UPDATE 2
35
36struct pending_extent_op {
37 int type;
38 u64 bytenr;
39 u64 num_bytes;
40 u64 parent;
41 u64 orig_parent;
42 u64 generation;
43 u64 orig_generation;
44 int level;
Josef Bacikf3465ca2008-11-12 14:19:50 -050045 struct list_head list;
46 int del;
Zheng Yan31840ae2008-09-23 13:14:14 -040047};
48
Chris Masone089f052007-03-16 16:20:31 -040049static int finish_current_insert(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -040050 btrfs_root *extent_root, int all);
Chris Masone20d96d2007-03-22 12:13:20 -040051static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -040052 btrfs_root *extent_root, int all);
Chris Mason925baed2008-06-25 16:01:30 -040053static struct btrfs_block_group_cache *
54__btrfs_find_block_group(struct btrfs_root *root,
55 struct btrfs_block_group_cache *hint,
56 u64 search_start, int data, int owner);
Josef Bacikf3465ca2008-11-12 14:19:50 -050057static int pin_down_bytes(struct btrfs_trans_handle *trans,
58 struct btrfs_root *root,
59 u64 bytenr, u64 num_bytes, int is_data);
60static int update_block_group(struct btrfs_trans_handle *trans,
61 struct btrfs_root *root,
62 u64 bytenr, u64 num_bytes, int alloc,
63 int mark_free);
Yand548ee52008-01-03 13:56:30 -050064
Josef Bacik0f9dd462008-09-23 13:14:11 -040065static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
66{
67 return (cache->flags & bits) == bits;
68}
69
70/*
71 * this adds the block group to the fs_info rb tree for the block group
72 * cache
73 */
74int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
75 struct btrfs_block_group_cache *block_group)
76{
77 struct rb_node **p;
78 struct rb_node *parent = NULL;
79 struct btrfs_block_group_cache *cache;
80
81 spin_lock(&info->block_group_cache_lock);
82 p = &info->block_group_cache_tree.rb_node;
83
84 while (*p) {
85 parent = *p;
86 cache = rb_entry(parent, struct btrfs_block_group_cache,
87 cache_node);
88 if (block_group->key.objectid < cache->key.objectid) {
89 p = &(*p)->rb_left;
90 } else if (block_group->key.objectid > cache->key.objectid) {
91 p = &(*p)->rb_right;
92 } else {
93 spin_unlock(&info->block_group_cache_lock);
94 return -EEXIST;
95 }
96 }
97
98 rb_link_node(&block_group->cache_node, parent, p);
99 rb_insert_color(&block_group->cache_node,
100 &info->block_group_cache_tree);
101 spin_unlock(&info->block_group_cache_lock);
102
103 return 0;
104}
105
106/*
107 * This will return the block group at or after bytenr if contains is 0, else
108 * it will return the block group that contains the bytenr
109 */
110static struct btrfs_block_group_cache *
111block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
112 int contains)
113{
114 struct btrfs_block_group_cache *cache, *ret = NULL;
115 struct rb_node *n;
116 u64 end, start;
117
118 spin_lock(&info->block_group_cache_lock);
119 n = info->block_group_cache_tree.rb_node;
120
121 while (n) {
122 cache = rb_entry(n, struct btrfs_block_group_cache,
123 cache_node);
124 end = cache->key.objectid + cache->key.offset - 1;
125 start = cache->key.objectid;
126
127 if (bytenr < start) {
128 if (!contains && (!ret || start < ret->key.objectid))
129 ret = cache;
130 n = n->rb_left;
131 } else if (bytenr > start) {
132 if (contains && bytenr <= end) {
133 ret = cache;
134 break;
135 }
136 n = n->rb_right;
137 } else {
138 ret = cache;
139 break;
140 }
141 }
142 spin_unlock(&info->block_group_cache_lock);
143
144 return ret;
145}
146
147/*
148 * this is only called by cache_block_group, since we could have freed extents
149 * we need to check the pinned_extents for any extents that can't be used yet
150 * since their free space will be released as soon as the transaction commits.
151 */
152static int add_new_free_space(struct btrfs_block_group_cache *block_group,
153 struct btrfs_fs_info *info, u64 start, u64 end)
154{
155 u64 extent_start, extent_end, size;
156 int ret;
157
Josef Bacik25179202008-10-29 14:49:05 -0400158 mutex_lock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400159 while (start < end) {
160 ret = find_first_extent_bit(&info->pinned_extents, start,
161 &extent_start, &extent_end,
162 EXTENT_DIRTY);
163 if (ret)
164 break;
165
166 if (extent_start == start) {
167 start = extent_end + 1;
168 } else if (extent_start > start && extent_start < end) {
169 size = extent_start - start;
Josef Bacik25179202008-10-29 14:49:05 -0400170 ret = btrfs_add_free_space_lock(block_group, start,
171 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400172 BUG_ON(ret);
173 start = extent_end + 1;
174 } else {
175 break;
176 }
177 }
178
179 if (start < end) {
180 size = end - start;
Josef Bacik25179202008-10-29 14:49:05 -0400181 ret = btrfs_add_free_space_lock(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400182 BUG_ON(ret);
183 }
Josef Bacik25179202008-10-29 14:49:05 -0400184 mutex_unlock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400185
186 return 0;
187}
188
Chris Masone37c9e62007-05-09 20:13:14 -0400189static int cache_block_group(struct btrfs_root *root,
190 struct btrfs_block_group_cache *block_group)
191{
192 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400193 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400194 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400195 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400196 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -0400197 u64 last = 0;
Yan7d7d6062007-09-14 16:15:28 -0400198 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -0400199 int found = 0;
200
Chris Mason00f5c792007-11-30 10:09:33 -0500201 if (!block_group)
202 return 0;
203
Chris Masone37c9e62007-05-09 20:13:14 -0400204 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400205
206 if (block_group->cached)
207 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400208
Chris Masone37c9e62007-05-09 20:13:14 -0400209 path = btrfs_alloc_path();
210 if (!path)
211 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400212
Chris Mason2cc58cf2007-08-27 16:49:44 -0400213 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400214 /*
215 * we get into deadlocks with paths held by callers of this function.
216 * since the alloc_mutex is protecting things right now, just
217 * skip the locking here
218 */
219 path->skip_locking = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400220 first_free = max_t(u64, block_group->key.objectid,
221 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Masone37c9e62007-05-09 20:13:14 -0400222 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -0400223 key.offset = 0;
224 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
225 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
226 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400227 goto err;
Chris Mason0b86a832008-03-24 15:01:56 -0400228 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -0500229 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400230 goto err;
Yand548ee52008-01-03 13:56:30 -0500231 if (ret == 0) {
232 leaf = path->nodes[0];
233 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
234 if (key.objectid + key.offset > first_free)
235 first_free = key.objectid + key.offset;
236 }
Chris Masone37c9e62007-05-09 20:13:14 -0400237 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400238 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400239 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400240 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400241 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400242 if (ret < 0)
243 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400244 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400245 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400246 else
Chris Masone37c9e62007-05-09 20:13:14 -0400247 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400248 }
Chris Mason5f39d392007-10-15 16:14:19 -0400249 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400250 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400251 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400252
Chris Masone37c9e62007-05-09 20:13:14 -0400253 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400254 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400255 break;
Yan7d7d6062007-09-14 16:15:28 -0400256
257 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
258 if (!found) {
259 last = first_free;
260 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400261 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400262
263 add_new_free_space(block_group, root->fs_info, last,
264 key.objectid);
265
Yan7d7d6062007-09-14 16:15:28 -0400266 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400267 }
Yan7d7d6062007-09-14 16:15:28 -0400268next:
Chris Masone37c9e62007-05-09 20:13:14 -0400269 path->slots[0]++;
270 }
271
Yan7d7d6062007-09-14 16:15:28 -0400272 if (!found)
273 last = first_free;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400274
275 add_new_free_space(block_group, root->fs_info, last,
276 block_group->key.objectid +
277 block_group->key.offset);
278
Chris Masone37c9e62007-05-09 20:13:14 -0400279 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400280 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400281err:
Chris Masone37c9e62007-05-09 20:13:14 -0400282 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400283 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400284}
285
Josef Bacik0f9dd462008-09-23 13:14:11 -0400286/*
287 * return the block group that starts at or after bytenr
288 */
Chris Mason0ef3e662008-05-24 14:04:53 -0400289struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
290 btrfs_fs_info *info,
291 u64 bytenr)
292{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400293 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400294
Josef Bacik0f9dd462008-09-23 13:14:11 -0400295 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400296
Josef Bacik0f9dd462008-09-23 13:14:11 -0400297 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400298}
299
Josef Bacik0f9dd462008-09-23 13:14:11 -0400300/*
301 * return the block group that contains teh given bytenr
302 */
Chris Mason5276aed2007-06-11 21:33:38 -0400303struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
304 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400305 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400306{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400307 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400308
Josef Bacik0f9dd462008-09-23 13:14:11 -0400309 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400310
Josef Bacik0f9dd462008-09-23 13:14:11 -0400311 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400312}
Chris Mason0b86a832008-03-24 15:01:56 -0400313
Josef Bacik0f9dd462008-09-23 13:14:11 -0400314static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
315 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400316{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400317 struct list_head *head = &info->space_info;
318 struct list_head *cur;
319 struct btrfs_space_info *found;
320 list_for_each(cur, head) {
321 found = list_entry(cur, struct btrfs_space_info, list);
322 if (found->flags == flags)
323 return found;
324 }
325 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400326}
327
Josef Bacik80eb2342008-10-29 14:49:05 -0400328static u64 div_factor(u64 num, int factor)
329{
330 if (factor == 10)
331 return num;
332 num *= factor;
333 do_div(num, 10);
334 return num;
335}
336
Chris Mason925baed2008-06-25 16:01:30 -0400337static struct btrfs_block_group_cache *
338__btrfs_find_block_group(struct btrfs_root *root,
339 struct btrfs_block_group_cache *hint,
340 u64 search_start, int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400341{
Chris Mason96b51792007-10-15 16:15:19 -0400342 struct btrfs_block_group_cache *cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400343 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400344 struct btrfs_fs_info *info = root->fs_info;
345 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400346 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400347 u64 free_check;
Chris Mason31f3c992007-04-30 15:25:45 -0400348 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400349 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400350 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400351
Chris Masona236aed2008-04-29 09:38:00 -0400352 if (data & BTRFS_BLOCK_GROUP_METADATA)
353 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400354
Chris Mason0ef3e662008-05-24 14:04:53 -0400355 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400356 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400357 shint = btrfs_lookup_first_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400358 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonc286ac42008-07-22 23:06:41 -0400359 spin_lock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400360 used = btrfs_block_group_used(&shint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400361 if (used + shint->pinned + shint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500362 div_factor(shint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400363 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400364 return shint;
365 }
Chris Masonc286ac42008-07-22 23:06:41 -0400366 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400367 }
368 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400369 if (hint && !hint->ro && block_group_bits(hint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400370 spin_lock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400371 used = btrfs_block_group_used(&hint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400372 if (used + hint->pinned + hint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500373 div_factor(hint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400374 spin_unlock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400375 return hint;
376 }
Chris Masonc286ac42008-07-22 23:06:41 -0400377 spin_unlock(&hint->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400378 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400379 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400380 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400381 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400382 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400383 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400384 }
Chris Mason31f3c992007-04-30 15:25:45 -0400385again:
Zheng Yane8569812008-09-26 10:05:48 -0400386 while (1) {
387 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400388 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400389 break;
Chris Mason96b51792007-10-15 16:15:19 -0400390
Chris Masonc286ac42008-07-22 23:06:41 -0400391 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400392 last = cache->key.objectid + cache->key.offset;
393 used = btrfs_block_group_used(&cache->item);
394
Chris Mason8f18cf12008-04-25 16:53:30 -0400395 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400396 free_check = div_factor(cache->key.offset, factor);
Zheng Yane8569812008-09-26 10:05:48 -0400397 if (used + cache->pinned + cache->reserved <
398 free_check) {
Chris Mason8790d502008-04-03 16:29:03 -0400399 found_group = cache;
Chris Masonc286ac42008-07-22 23:06:41 -0400400 spin_unlock(&cache->lock);
Chris Mason8790d502008-04-03 16:29:03 -0400401 goto found;
402 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400403 }
Chris Masonc286ac42008-07-22 23:06:41 -0400404 spin_unlock(&cache->lock);
Chris Masonde428b62007-05-18 13:28:27 -0400405 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400406 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400407 if (!wrapped) {
408 last = search_start;
409 wrapped = 1;
410 goto again;
411 }
412 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400413 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400414 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400415 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400416 goto again;
417 }
Chris Masonbe744172007-05-06 10:15:01 -0400418found:
Chris Mason31f3c992007-04-30 15:25:45 -0400419 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400420}
421
Chris Mason925baed2008-06-25 16:01:30 -0400422struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
423 struct btrfs_block_group_cache
424 *hint, u64 search_start,
425 int data, int owner)
426{
427
428 struct btrfs_block_group_cache *ret;
Chris Mason925baed2008-06-25 16:01:30 -0400429 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
Chris Mason925baed2008-06-25 16:01:30 -0400430 return ret;
431}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400432
Chris Masone02119d2008-09-05 16:13:11 -0400433/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400434int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400435{
436 int ret;
437 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400438 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400439
Zheng Yan31840ae2008-09-23 13:14:14 -0400440 path = btrfs_alloc_path();
441 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400442 key.objectid = start;
443 key.offset = len;
444 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
445 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
446 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400447 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500448 return ret;
449}
450
Chris Masond8d5f3e2007-12-11 12:42:00 -0500451/*
452 * Back reference rules. Back refs have three main goals:
453 *
454 * 1) differentiate between all holders of references to an extent so that
455 * when a reference is dropped we can make sure it was a valid reference
456 * before freeing the extent.
457 *
458 * 2) Provide enough information to quickly find the holders of an extent
459 * if we notice a given block is corrupted or bad.
460 *
461 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
462 * maintenance. This is actually the same as #2, but with a slightly
463 * different use case.
464 *
465 * File extents can be referenced by:
466 *
467 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400468 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500469 * - different offsets inside a file (bookend extents in file.c)
470 *
471 * The extent ref structure has fields for:
472 *
473 * - Objectid of the subvolume root
474 * - Generation number of the tree holding the reference
475 * - objectid of the file holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400476 * - number of references holding by parent node (alway 1 for tree blocks)
477 *
478 * Btree leaf may hold multiple references to a file extent. In most cases,
479 * these references are from same file and the corresponding offsets inside
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400480 * the file are close together.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500481 *
482 * When a file extent is allocated the fields are filled in:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400483 * (root_key.objectid, trans->transid, inode objectid, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500484 *
485 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400486 * in the leaf. It looks similar to the create case, but trans->transid will
487 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500488 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400489 * (root_key.objectid, trans->transid, inode objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400490 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500491 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400492 * When a file extent is removed either during snapshot deletion or
493 * file truncation, we find the corresponding back reference and check
494 * the following fields:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500495 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400496 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
497 * inode objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500498 *
499 * Btree extents can be referenced by:
500 *
501 * - Different subvolumes
502 * - Different generations of the same subvolume
503 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500504 * When a tree block is created, back references are inserted:
505 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400506 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500507 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400508 * When a tree block is cow'd, new back references are added for all the
509 * blocks it points to. If the tree block isn't in reference counted root,
510 * the old back references are removed. These new back references are of
511 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500512 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400513 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500514 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400515 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500516 *
517 * if backref was for a tree root:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400518 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500519 * else
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400520 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500521 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400522 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500523 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400524 * The key objectid corresponds to the first byte in the extent, the key
525 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
526 * byte of parent extent. If a extent is tree root, the key offset is set
527 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500528 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400529
530static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
531 struct btrfs_root *root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400532 struct btrfs_path *path,
533 u64 bytenr, u64 parent,
534 u64 ref_root, u64 ref_generation,
535 u64 owner_objectid, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500536{
Chris Mason74493f72007-12-11 09:25:06 -0500537 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400538 struct btrfs_extent_ref *ref;
539 struct extent_buffer *leaf;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400540 u64 ref_objectid;
Chris Mason74493f72007-12-11 09:25:06 -0500541 int ret;
542
Chris Mason74493f72007-12-11 09:25:06 -0500543 key.objectid = bytenr;
544 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400545 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500546
Zheng Yan31840ae2008-09-23 13:14:14 -0400547 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
548 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500549 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400550 if (ret > 0) {
551 ret = -ENOENT;
552 goto out;
553 }
554
555 leaf = path->nodes[0];
556 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400557 ref_objectid = btrfs_ref_objectid(leaf, ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400558 if (btrfs_ref_root(leaf, ref) != ref_root ||
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400559 btrfs_ref_generation(leaf, ref) != ref_generation ||
560 (ref_objectid != owner_objectid &&
561 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400562 ret = -EIO;
563 WARN_ON(1);
564 goto out;
565 }
566 ret = 0;
567out:
568 return ret;
569}
570
Josef Bacikf3465ca2008-11-12 14:19:50 -0500571/*
572 * updates all the backrefs that are pending on update_list for the
573 * extent_root
574 */
575static int noinline update_backrefs(struct btrfs_trans_handle *trans,
576 struct btrfs_root *extent_root,
577 struct btrfs_path *path,
578 struct list_head *update_list)
579{
580 struct btrfs_key key;
581 struct btrfs_extent_ref *ref;
582 struct btrfs_fs_info *info = extent_root->fs_info;
583 struct pending_extent_op *op;
584 struct extent_buffer *leaf;
585 int ret = 0;
586 struct list_head *cur = update_list->next;
587 u64 ref_objectid;
588 u64 ref_root = extent_root->root_key.objectid;
589
590 op = list_entry(cur, struct pending_extent_op, list);
591
592search:
593 key.objectid = op->bytenr;
594 key.type = BTRFS_EXTENT_REF_KEY;
595 key.offset = op->orig_parent;
596
597 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
598 BUG_ON(ret);
599
600 leaf = path->nodes[0];
601
602loop:
603 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
604
605 ref_objectid = btrfs_ref_objectid(leaf, ref);
606
607 if (btrfs_ref_root(leaf, ref) != ref_root ||
608 btrfs_ref_generation(leaf, ref) != op->orig_generation ||
609 (ref_objectid != op->level &&
610 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
611 printk(KERN_ERR "couldn't find %Lu, parent %Lu, root %Lu, "
612 "owner %u\n", op->bytenr, op->orig_parent,
613 ref_root, op->level);
614 btrfs_print_leaf(extent_root, leaf);
615 BUG();
616 }
617
618 key.objectid = op->bytenr;
619 key.offset = op->parent;
620 key.type = BTRFS_EXTENT_REF_KEY;
621 ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
622 BUG_ON(ret);
623 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
624 btrfs_set_ref_generation(leaf, ref, op->generation);
625
626 cur = cur->next;
627
628 list_del_init(&op->list);
629 unlock_extent(&info->extent_ins, op->bytenr,
630 op->bytenr + op->num_bytes - 1, GFP_NOFS);
631 kfree(op);
632
633 if (cur == update_list) {
634 btrfs_mark_buffer_dirty(path->nodes[0]);
635 btrfs_release_path(extent_root, path);
636 goto out;
637 }
638
639 op = list_entry(cur, struct pending_extent_op, list);
640
641 path->slots[0]++;
642 while (path->slots[0] < btrfs_header_nritems(leaf)) {
643 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
644 if (key.objectid == op->bytenr &&
645 key.type == BTRFS_EXTENT_REF_KEY)
646 goto loop;
647 path->slots[0]++;
648 }
649
650 btrfs_mark_buffer_dirty(path->nodes[0]);
651 btrfs_release_path(extent_root, path);
652 goto search;
653
654out:
655 return 0;
656}
657
658static int noinline insert_extents(struct btrfs_trans_handle *trans,
659 struct btrfs_root *extent_root,
660 struct btrfs_path *path,
661 struct list_head *insert_list, int nr)
662{
663 struct btrfs_key *keys;
664 u32 *data_size;
665 struct pending_extent_op *op;
666 struct extent_buffer *leaf;
667 struct list_head *cur = insert_list->next;
668 struct btrfs_fs_info *info = extent_root->fs_info;
669 u64 ref_root = extent_root->root_key.objectid;
670 int i = 0, last = 0, ret;
671 int total = nr * 2;
672
673 if (!nr)
674 return 0;
675
676 keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
677 if (!keys)
678 return -ENOMEM;
679
680 data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
681 if (!data_size) {
682 kfree(keys);
683 return -ENOMEM;
684 }
685
686 list_for_each_entry(op, insert_list, list) {
687 keys[i].objectid = op->bytenr;
688 keys[i].offset = op->num_bytes;
689 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
690 data_size[i] = sizeof(struct btrfs_extent_item);
691 i++;
692
693 keys[i].objectid = op->bytenr;
694 keys[i].offset = op->parent;
695 keys[i].type = BTRFS_EXTENT_REF_KEY;
696 data_size[i] = sizeof(struct btrfs_extent_ref);
697 i++;
698 }
699
700 op = list_entry(cur, struct pending_extent_op, list);
701 i = 0;
702 while (i < total) {
703 int c;
704 ret = btrfs_insert_some_items(trans, extent_root, path,
705 keys+i, data_size+i, total-i);
706 BUG_ON(ret < 0);
707
708 if (last && ret > 1)
709 BUG();
710
711 leaf = path->nodes[0];
712 for (c = 0; c < ret; c++) {
713 int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
714
715 /*
716 * if the first item we inserted was a backref, then
717 * the EXTENT_ITEM will be the odd c's, else it will
718 * be the even c's
719 */
720 if ((ref_first && (c % 2)) ||
721 (!ref_first && !(c % 2))) {
722 struct btrfs_extent_item *itm;
723
724 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
725 struct btrfs_extent_item);
726 btrfs_set_extent_refs(path->nodes[0], itm, 1);
727 op->del++;
728 } else {
729 struct btrfs_extent_ref *ref;
730
731 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
732 struct btrfs_extent_ref);
733 btrfs_set_ref_root(leaf, ref, ref_root);
734 btrfs_set_ref_generation(leaf, ref,
735 op->generation);
736 btrfs_set_ref_objectid(leaf, ref, op->level);
737 btrfs_set_ref_num_refs(leaf, ref, 1);
738 op->del++;
739 }
740
741 /*
742 * using del to see when its ok to free up the
743 * pending_extent_op. In the case where we insert the
744 * last item on the list in order to help do batching
745 * we need to not free the extent op until we actually
746 * insert the extent_item
747 */
748 if (op->del == 2) {
749 unlock_extent(&info->extent_ins, op->bytenr,
750 op->bytenr + op->num_bytes - 1,
751 GFP_NOFS);
752 cur = cur->next;
753 list_del_init(&op->list);
754 kfree(op);
755 if (cur != insert_list)
756 op = list_entry(cur,
757 struct pending_extent_op,
758 list);
759 }
760 }
761 btrfs_mark_buffer_dirty(leaf);
762 btrfs_release_path(extent_root, path);
763
764 /*
765 * Ok backref's and items usually go right next to eachother,
766 * but if we could only insert 1 item that means that we
767 * inserted on the end of a leaf, and we have no idea what may
768 * be on the next leaf so we just play it safe. In order to
769 * try and help this case we insert the last thing on our
770 * insert list so hopefully it will end up being the last
771 * thing on the leaf and everything else will be before it,
772 * which will let us insert a whole bunch of items at the same
773 * time.
774 */
775 if (ret == 1 && !last && (i + ret < total)) {
776 /*
777 * last: where we will pick up the next time around
778 * i: our current key to insert, will be total - 1
779 * cur: the current op we are screwing with
780 * op: duh
781 */
782 last = i + ret;
783 i = total - 1;
784 cur = insert_list->prev;
785 op = list_entry(cur, struct pending_extent_op, list);
786 } else if (last) {
787 /*
788 * ok we successfully inserted the last item on the
789 * list, lets reset everything
790 *
791 * i: our current key to insert, so where we left off
792 * last time
793 * last: done with this
794 * cur: the op we are messing with
795 * op: duh
796 * total: since we inserted the last key, we need to
797 * decrement total so we dont overflow
798 */
799 i = last;
800 last = 0;
801 cur = insert_list->next;
802 op = list_entry(cur, struct pending_extent_op, list);
803 total--;
804 } else {
805 i += ret;
806 }
807
808 cond_resched();
809 }
810 ret = 0;
811 kfree(keys);
812 kfree(data_size);
813 return ret;
814}
815
Zheng Yan31840ae2008-09-23 13:14:14 -0400816static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
817 struct btrfs_root *root,
818 struct btrfs_path *path,
819 u64 bytenr, u64 parent,
820 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400821 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400822{
823 struct btrfs_key key;
824 struct extent_buffer *leaf;
825 struct btrfs_extent_ref *ref;
826 u32 num_refs;
827 int ret;
828
829 key.objectid = bytenr;
830 key.type = BTRFS_EXTENT_REF_KEY;
831 key.offset = parent;
832
833 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
834 if (ret == 0) {
835 leaf = path->nodes[0];
836 ref = btrfs_item_ptr(leaf, path->slots[0],
837 struct btrfs_extent_ref);
838 btrfs_set_ref_root(leaf, ref, ref_root);
839 btrfs_set_ref_generation(leaf, ref, ref_generation);
840 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400841 btrfs_set_ref_num_refs(leaf, ref, 1);
842 } else if (ret == -EEXIST) {
843 u64 existing_owner;
844 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
845 leaf = path->nodes[0];
846 ref = btrfs_item_ptr(leaf, path->slots[0],
847 struct btrfs_extent_ref);
848 if (btrfs_ref_root(leaf, ref) != ref_root ||
849 btrfs_ref_generation(leaf, ref) != ref_generation) {
850 ret = -EIO;
851 WARN_ON(1);
852 goto out;
853 }
854
855 num_refs = btrfs_ref_num_refs(leaf, ref);
856 BUG_ON(num_refs == 0);
857 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
858
859 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400860 if (existing_owner != owner_objectid &&
861 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400862 btrfs_set_ref_objectid(leaf, ref,
863 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400864 }
865 ret = 0;
866 } else {
867 goto out;
868 }
Chris Mason7bb86312007-12-11 09:25:06 -0500869 btrfs_mark_buffer_dirty(path->nodes[0]);
870out:
871 btrfs_release_path(root, path);
872 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500873}
874
Zheng Yan31840ae2008-09-23 13:14:14 -0400875static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
876 struct btrfs_root *root,
877 struct btrfs_path *path)
878{
879 struct extent_buffer *leaf;
880 struct btrfs_extent_ref *ref;
881 u32 num_refs;
882 int ret = 0;
883
884 leaf = path->nodes[0];
885 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
886 num_refs = btrfs_ref_num_refs(leaf, ref);
887 BUG_ON(num_refs == 0);
888 num_refs -= 1;
889 if (num_refs == 0) {
890 ret = btrfs_del_item(trans, root, path);
891 } else {
892 btrfs_set_ref_num_refs(leaf, ref, num_refs);
893 btrfs_mark_buffer_dirty(leaf);
894 }
895 btrfs_release_path(root, path);
896 return ret;
897}
898
Josef Bacikf3465ca2008-11-12 14:19:50 -0500899static int noinline free_extents(struct btrfs_trans_handle *trans,
900 struct btrfs_root *extent_root,
901 struct list_head *del_list)
902{
903 struct btrfs_fs_info *info = extent_root->fs_info;
904 struct btrfs_path *path;
905 struct btrfs_key key, found_key;
906 struct extent_buffer *leaf;
907 struct list_head *cur;
908 struct pending_extent_op *op;
909 struct btrfs_extent_item *ei;
910 int ret, num_to_del, extent_slot = 0, found_extent = 0;
911 u32 refs;
912 u64 bytes_freed = 0;
913
914 path = btrfs_alloc_path();
915 if (!path)
916 return -ENOMEM;
917 path->reada = 1;
918
919search:
920 /* search for the backref for the current ref we want to delete */
921 cur = del_list->next;
922 op = list_entry(cur, struct pending_extent_op, list);
923 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
924 op->orig_parent,
925 extent_root->root_key.objectid,
926 op->orig_generation, op->level, 1);
927 if (ret) {
928 printk("Unable to find backref byte nr %Lu root %Lu gen %Lu "
929 "owner %u\n", op->bytenr,
930 extent_root->root_key.objectid, op->orig_generation,
931 op->level);
932 btrfs_print_leaf(extent_root, path->nodes[0]);
933 WARN_ON(1);
934 goto out;
935 }
936
937 extent_slot = path->slots[0];
938 num_to_del = 1;
939 found_extent = 0;
940
941 /*
942 * if we aren't the first item on the leaf we can move back one and see
943 * if our ref is right next to our extent item
944 */
945 if (likely(extent_slot)) {
946 extent_slot--;
947 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
948 extent_slot);
949 if (found_key.objectid == op->bytenr &&
950 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
951 found_key.offset == op->num_bytes) {
952 num_to_del++;
953 found_extent = 1;
954 }
955 }
956
957 /*
958 * if we didn't find the extent we need to delete the backref and then
959 * search for the extent item key so we can update its ref count
960 */
961 if (!found_extent) {
962 key.objectid = op->bytenr;
963 key.type = BTRFS_EXTENT_ITEM_KEY;
964 key.offset = op->num_bytes;
965
966 ret = remove_extent_backref(trans, extent_root, path);
967 BUG_ON(ret);
968 btrfs_release_path(extent_root, path);
969 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
970 BUG_ON(ret);
971 extent_slot = path->slots[0];
972 }
973
974 /* this is where we update the ref count for the extent */
975 leaf = path->nodes[0];
976 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
977 refs = btrfs_extent_refs(leaf, ei);
978 BUG_ON(refs == 0);
979 refs--;
980 btrfs_set_extent_refs(leaf, ei, refs);
981
982 btrfs_mark_buffer_dirty(leaf);
983
984 /*
985 * This extent needs deleting. The reason cur_slot is extent_slot +
986 * num_to_del is because extent_slot points to the slot where the extent
987 * is, and if the backref was not right next to the extent we will be
988 * deleting at least 1 item, and will want to start searching at the
989 * slot directly next to extent_slot. However if we did find the
990 * backref next to the extent item them we will be deleting at least 2
991 * items and will want to start searching directly after the ref slot
992 */
993 if (!refs) {
994 struct list_head *pos, *n, *end;
995 int cur_slot = extent_slot+num_to_del;
996 u64 super_used;
997 u64 root_used;
998
999 path->slots[0] = extent_slot;
1000 bytes_freed = op->num_bytes;
1001
1002 /*
1003 * we need to see if we can delete multiple things at once, so
1004 * start looping through the list of extents we are wanting to
1005 * delete and see if their extent/backref's are right next to
1006 * eachother and the extents only have 1 ref
1007 */
1008 for (pos = cur->next; pos != del_list; pos = pos->next) {
1009 struct pending_extent_op *tmp;
1010
1011 tmp = list_entry(pos, struct pending_extent_op, list);
1012
1013 /* we only want to delete extent+ref at this stage */
1014 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1015 break;
1016
1017 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1018 if (found_key.objectid != tmp->bytenr ||
1019 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1020 found_key.offset != tmp->num_bytes)
1021 break;
1022
1023 /* check to make sure this extent only has one ref */
1024 ei = btrfs_item_ptr(leaf, cur_slot,
1025 struct btrfs_extent_item);
1026 if (btrfs_extent_refs(leaf, ei) != 1)
1027 break;
1028
1029 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1030 if (found_key.objectid != tmp->bytenr ||
1031 found_key.type != BTRFS_EXTENT_REF_KEY ||
1032 found_key.offset != tmp->orig_parent)
1033 break;
1034
1035 /*
1036 * the ref is right next to the extent, we can set the
1037 * ref count to 0 since we will delete them both now
1038 */
1039 btrfs_set_extent_refs(leaf, ei, 0);
1040
1041 /* pin down the bytes for this extent */
1042 mutex_lock(&info->pinned_mutex);
1043 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1044 tmp->num_bytes, tmp->level >=
1045 BTRFS_FIRST_FREE_OBJECTID);
1046 mutex_unlock(&info->pinned_mutex);
1047 BUG_ON(ret < 0);
1048
1049 /*
1050 * use the del field to tell if we need to go ahead and
1051 * free up the extent when we delete the item or not.
1052 */
1053 tmp->del = ret;
1054 bytes_freed += tmp->num_bytes;
1055
1056 num_to_del += 2;
1057 cur_slot += 2;
1058 }
1059 end = pos;
1060
1061 /* update the free space counters */
1062 spin_lock_irq(&info->delalloc_lock);
1063 super_used = btrfs_super_bytes_used(&info->super_copy);
1064 btrfs_set_super_bytes_used(&info->super_copy,
1065 super_used - bytes_freed);
1066 spin_unlock_irq(&info->delalloc_lock);
1067
1068 root_used = btrfs_root_used(&extent_root->root_item);
1069 btrfs_set_root_used(&extent_root->root_item,
1070 root_used - bytes_freed);
1071
1072 /* delete the items */
1073 ret = btrfs_del_items(trans, extent_root, path,
1074 path->slots[0], num_to_del);
1075 BUG_ON(ret);
1076
1077 /*
1078 * loop through the extents we deleted and do the cleanup work
1079 * on them
1080 */
1081 for (pos = cur, n = pos->next; pos != end;
1082 pos = n, n = pos->next) {
1083 struct pending_extent_op *tmp;
1084#ifdef BIO_RW_DISCARD
1085 u64 map_length;
1086 struct btrfs_multi_bio *multi = NULL;
1087#endif
1088 tmp = list_entry(pos, struct pending_extent_op, list);
1089
1090 /*
1091 * remember tmp->del tells us wether or not we pinned
1092 * down the extent
1093 */
1094 ret = update_block_group(trans, extent_root,
1095 tmp->bytenr, tmp->num_bytes, 0,
1096 tmp->del);
1097 BUG_ON(ret);
1098
1099#ifdef BIO_RW_DISCARD
1100 ret = btrfs_map_block(&info->mapping_tree, READ,
1101 tmp->bytenr, &map_length, &multi,
1102 0);
1103 if (!ret) {
1104 struct btrfs_bio_stripe *stripe;
1105 int i;
1106
1107 stripe = multi->stripe;
1108
1109 if (map_length > tmp->num_bytes)
1110 map_length = tmp->num_bytes;
1111
1112 for (i = 0; i < multi->num_stripes;
1113 i++, stripe++)
1114 blkdev_issue_discard(stripe->dev->bdev,
1115 stripe->physical >> 9,
1116 map_length >> 9);
1117 kfree(multi);
1118 }
1119#endif
1120 list_del_init(&tmp->list);
1121 unlock_extent(&info->extent_ins, tmp->bytenr,
1122 tmp->bytenr + tmp->num_bytes - 1,
1123 GFP_NOFS);
1124 kfree(tmp);
1125 }
1126 } else if (refs && found_extent) {
1127 /*
1128 * the ref and extent were right next to eachother, but the
1129 * extent still has a ref, so just free the backref and keep
1130 * going
1131 */
1132 ret = remove_extent_backref(trans, extent_root, path);
1133 BUG_ON(ret);
1134
1135 list_del_init(&op->list);
1136 unlock_extent(&info->extent_ins, op->bytenr,
1137 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1138 kfree(op);
1139 } else {
1140 /*
1141 * the extent has multiple refs and the backref we were looking
1142 * for was not right next to it, so just unlock and go next,
1143 * we're good to go
1144 */
1145 list_del_init(&op->list);
1146 unlock_extent(&info->extent_ins, op->bytenr,
1147 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1148 kfree(op);
1149 }
1150
1151 btrfs_release_path(extent_root, path);
1152 if (!list_empty(del_list))
1153 goto search;
1154
1155out:
1156 btrfs_free_path(path);
1157 return ret;
1158}
1159
Zheng Yan31840ae2008-09-23 13:14:14 -04001160static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1161 struct btrfs_root *root, u64 bytenr,
1162 u64 orig_parent, u64 parent,
1163 u64 orig_root, u64 ref_root,
1164 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001165 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001166{
1167 int ret;
1168 struct btrfs_root *extent_root = root->fs_info->extent_root;
1169 struct btrfs_path *path;
1170
1171 if (root == root->fs_info->extent_root) {
1172 struct pending_extent_op *extent_op;
1173 u64 num_bytes;
1174
1175 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1176 num_bytes = btrfs_level_size(root, (int)owner_objectid);
Josef Bacik25179202008-10-29 14:49:05 -04001177 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001178 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
Josef Bacik25179202008-10-29 14:49:05 -04001179 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001180 u64 priv;
1181 ret = get_state_private(&root->fs_info->extent_ins,
1182 bytenr, &priv);
1183 BUG_ON(ret);
1184 extent_op = (struct pending_extent_op *)
1185 (unsigned long)priv;
1186 BUG_ON(extent_op->parent != orig_parent);
1187 BUG_ON(extent_op->generation != orig_generation);
Josef Bacik25179202008-10-29 14:49:05 -04001188
Zheng Yan31840ae2008-09-23 13:14:14 -04001189 extent_op->parent = parent;
1190 extent_op->generation = ref_generation;
1191 } else {
1192 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1193 BUG_ON(!extent_op);
1194
1195 extent_op->type = PENDING_BACKREF_UPDATE;
1196 extent_op->bytenr = bytenr;
1197 extent_op->num_bytes = num_bytes;
1198 extent_op->parent = parent;
1199 extent_op->orig_parent = orig_parent;
1200 extent_op->generation = ref_generation;
1201 extent_op->orig_generation = orig_generation;
1202 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001203 INIT_LIST_HEAD(&extent_op->list);
1204 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001205
1206 set_extent_bits(&root->fs_info->extent_ins,
1207 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04001208 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04001209 set_state_private(&root->fs_info->extent_ins,
1210 bytenr, (unsigned long)extent_op);
1211 }
Josef Bacik25179202008-10-29 14:49:05 -04001212 mutex_unlock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001213 return 0;
1214 }
1215
1216 path = btrfs_alloc_path();
1217 if (!path)
1218 return -ENOMEM;
1219 ret = lookup_extent_backref(trans, extent_root, path,
1220 bytenr, orig_parent, orig_root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001221 orig_generation, owner_objectid, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001222 if (ret)
1223 goto out;
1224 ret = remove_extent_backref(trans, extent_root, path);
1225 if (ret)
1226 goto out;
1227 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1228 parent, ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001229 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001230 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001231 finish_current_insert(trans, extent_root, 0);
1232 del_pending_extents(trans, extent_root, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001233out:
1234 btrfs_free_path(path);
1235 return ret;
1236}
1237
1238int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1239 struct btrfs_root *root, u64 bytenr,
1240 u64 orig_parent, u64 parent,
1241 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001242 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001243{
1244 int ret;
1245 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1246 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1247 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001248 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1249 parent, ref_root, ref_root,
1250 ref_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001251 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001252 return ret;
1253}
1254
Chris Mason925baed2008-06-25 16:01:30 -04001255static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001256 struct btrfs_root *root, u64 bytenr,
1257 u64 orig_parent, u64 parent,
1258 u64 orig_root, u64 ref_root,
1259 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001260 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -05001261{
Chris Mason5caf2a02007-04-02 11:20:42 -04001262 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -05001263 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001264 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001265 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001266 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001267 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001268
Chris Mason5caf2a02007-04-02 11:20:42 -04001269 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001270 if (!path)
1271 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001272
Chris Mason3c12ac72008-04-21 12:01:38 -04001273 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001274 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001275 key.type = BTRFS_EXTENT_ITEM_KEY;
1276 key.offset = (u64)-1;
1277
Chris Mason5caf2a02007-04-02 11:20:42 -04001278 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001279 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001280 if (ret < 0)
1281 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001282 BUG_ON(ret == 0 || path->slots[0] == 0);
1283
1284 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -04001285 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -04001286
1287 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Mason771ed682008-11-06 22:02:51 -05001288 if (key.objectid != bytenr) {
1289 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
1290 printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
1291 BUG();
1292 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001293 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1294
Chris Mason5caf2a02007-04-02 11:20:42 -04001295 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001296 refs = btrfs_extent_refs(l, item);
1297 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001298 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -05001299
Chris Mason5caf2a02007-04-02 11:20:42 -04001300 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001301
Chris Mason3c12ac72008-04-21 12:01:38 -04001302 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001303 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1304 path, bytenr, parent,
1305 ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001306 owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05001307 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001308 finish_current_insert(trans, root->fs_info->extent_root, 0);
1309 del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Mason74493f72007-12-11 09:25:06 -05001310
1311 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -05001312 return 0;
1313}
1314
Chris Mason925baed2008-06-25 16:01:30 -04001315int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001316 struct btrfs_root *root,
1317 u64 bytenr, u64 num_bytes, u64 parent,
1318 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001319 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -04001320{
1321 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001322 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1323 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1324 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001325 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1326 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001327 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -04001328 return ret;
1329}
1330
Chris Masone9d0b132007-08-10 14:06:19 -04001331int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1332 struct btrfs_root *root)
1333{
Chris Mason87ef2bb2008-10-30 11:23:27 -04001334 finish_current_insert(trans, root->fs_info->extent_root, 1);
1335 del_pending_extents(trans, root->fs_info->extent_root, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001336 return 0;
1337}
1338
Zheng Yan31840ae2008-09-23 13:14:14 -04001339int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1340 struct btrfs_root *root, u64 bytenr,
1341 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -05001342{
Chris Mason5caf2a02007-04-02 11:20:42 -04001343 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -05001344 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001345 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001346 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001347 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -04001348
Chris Masondb945352007-10-15 16:15:53 -04001349 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -04001350 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -04001351 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001352 key.objectid = bytenr;
1353 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -04001354 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -04001355 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001356 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001357 if (ret < 0)
1358 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -04001359 if (ret != 0) {
1360 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -04001361 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -05001362 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04001363 }
1364 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001365 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001366 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001367out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001368 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05001369 return 0;
1370}
1371
Yan Zheng80ff3852008-10-30 14:20:02 -04001372int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1373 struct btrfs_root *root, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05001374{
1375 struct btrfs_root *extent_root = root->fs_info->extent_root;
1376 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -04001377 struct extent_buffer *leaf;
1378 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -05001379 struct btrfs_key key;
1380 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 u64 ref_root;
1382 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -04001383 u32 nritems;
1384 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001385
Chris Masonbe20aa92007-12-17 20:14:01 -05001386 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001387 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04001388 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05001389
Yan Zhengf321e492008-07-30 09:26:11 -04001390 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -05001391 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1392 if (ret < 0)
1393 goto out;
1394 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001395
1396 ret = -ENOENT;
1397 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04001398 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001399
Zheng Yan31840ae2008-09-23 13:14:14 -04001400 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04001401 leaf = path->nodes[0];
1402 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001403
1404 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001405 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05001406 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001407
Yan Zheng80ff3852008-10-30 14:20:02 -04001408 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05001409 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -04001410 leaf = path->nodes[0];
1411 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -05001412 if (path->slots[0] >= nritems) {
1413 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -04001414 if (ret < 0)
1415 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001416 if (ret == 0)
1417 continue;
1418 break;
1419 }
Yan Zhengf321e492008-07-30 09:26:11 -04001420 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001421 if (found_key.objectid != bytenr)
1422 break;
Chris Masonbd098352008-01-03 13:23:19 -05001423
Chris Masonbe20aa92007-12-17 20:14:01 -05001424 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1425 path->slots[0]++;
1426 continue;
1427 }
1428
Yan Zhengf321e492008-07-30 09:26:11 -04001429 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001430 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -04001431 ref_root = btrfs_ref_root(leaf, ref_item);
1432 if (ref_root != root->root_key.objectid &&
1433 ref_root != BTRFS_TREE_LOG_OBJECTID) {
1434 ret = 1;
1435 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -04001436 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1438 ret = 1;
1439 goto out;
1440 }
Yan Zhengf321e492008-07-30 09:26:11 -04001441
Chris Masonbe20aa92007-12-17 20:14:01 -05001442 path->slots[0]++;
1443 }
Yan Zhengf321e492008-07-30 09:26:11 -04001444 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001445out:
Yan Zhengf321e492008-07-30 09:26:11 -04001446 btrfs_free_path(path);
1447 return ret;
1448}
1449
Zheng Yan31840ae2008-09-23 13:14:14 -04001450int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1451 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001452{
Chris Mason5f39d392007-10-15 16:14:19 -04001453 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001454 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001455 u64 root_gen;
1456 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001457 int i;
Chris Masondb945352007-10-15 16:15:53 -04001458 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001459 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001460 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001461
Chris Mason3768f362007-03-13 16:47:54 -04001462 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001463 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001464
Zheng Yane4657682008-09-26 10:04:53 -04001465 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1466 shared = 0;
1467 root_gen = root->root_key.offset;
1468 } else {
1469 shared = 1;
1470 root_gen = trans->transid - 1;
1471 }
1472
Chris Masondb945352007-10-15 16:15:53 -04001473 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001474 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001475
Zheng Yan31840ae2008-09-23 13:14:14 -04001476 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001477 struct btrfs_leaf_ref *ref;
1478 struct btrfs_extent_info *info;
1479
Zheng Yan31840ae2008-09-23 13:14:14 -04001480 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001481 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001482 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001483 goto out;
1484 }
1485
Zheng Yane4657682008-09-26 10:04:53 -04001486 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001487 ref->bytenr = buf->start;
1488 ref->owner = btrfs_header_owner(buf);
1489 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001490 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001491 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001492
Zheng Yan31840ae2008-09-23 13:14:14 -04001493 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001494 u64 disk_bytenr;
1495 btrfs_item_key_to_cpu(buf, &key, i);
1496 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1497 continue;
1498 fi = btrfs_item_ptr(buf, i,
1499 struct btrfs_file_extent_item);
1500 if (btrfs_file_extent_type(buf, fi) ==
1501 BTRFS_FILE_EXTENT_INLINE)
1502 continue;
1503 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1504 if (disk_bytenr == 0)
1505 continue;
1506
1507 info->bytenr = disk_bytenr;
1508 info->num_bytes =
1509 btrfs_file_extent_disk_num_bytes(buf, fi);
1510 info->objectid = key.objectid;
1511 info->offset = key.offset;
1512 info++;
1513 }
1514
Zheng Yane4657682008-09-26 10:04:53 -04001515 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001516 if (ret == -EEXIST && shared) {
1517 struct btrfs_leaf_ref *old;
1518 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1519 BUG_ON(!old);
1520 btrfs_remove_leaf_ref(root, old);
1521 btrfs_free_leaf_ref(root, old);
1522 ret = btrfs_add_leaf_ref(root, ref, shared);
1523 }
Yan Zheng31153d82008-07-28 15:32:19 -04001524 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001525 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001526 }
1527out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001528 return ret;
1529}
1530
1531int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1532 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1533 u32 *nr_extents)
1534{
1535 u64 bytenr;
1536 u64 ref_root;
1537 u64 orig_root;
1538 u64 ref_generation;
1539 u64 orig_generation;
1540 u32 nritems;
1541 u32 nr_file_extents = 0;
1542 struct btrfs_key key;
1543 struct btrfs_file_extent_item *fi;
1544 int i;
1545 int level;
1546 int ret = 0;
1547 int faili = 0;
1548 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001549 u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001550
1551 ref_root = btrfs_header_owner(buf);
1552 ref_generation = btrfs_header_generation(buf);
1553 orig_root = btrfs_header_owner(orig_buf);
1554 orig_generation = btrfs_header_generation(orig_buf);
1555
1556 nritems = btrfs_header_nritems(buf);
1557 level = btrfs_header_level(buf);
1558
1559 if (root->ref_cows) {
1560 process_func = __btrfs_inc_extent_ref;
1561 } else {
1562 if (level == 0 &&
1563 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1564 goto out;
1565 if (level != 0 &&
1566 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1567 goto out;
1568 process_func = __btrfs_update_extent_ref;
1569 }
1570
1571 for (i = 0; i < nritems; i++) {
1572 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001573 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001574 btrfs_item_key_to_cpu(buf, &key, i);
1575 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001576 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001577 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001578 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001579 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001580 BTRFS_FILE_EXTENT_INLINE)
1581 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001582 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1583 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001584 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001585
1586 nr_file_extents++;
1587
Zheng Yan31840ae2008-09-23 13:14:14 -04001588 ret = process_func(trans, root, bytenr,
1589 orig_buf->start, buf->start,
1590 orig_root, ref_root,
1591 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001592 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001593
1594 if (ret) {
1595 faili = i;
1596 WARN_ON(1);
1597 goto fail;
1598 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001599 } else {
Chris Masondb945352007-10-15 16:15:53 -04001600 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001601 ret = process_func(trans, root, bytenr,
1602 orig_buf->start, buf->start,
1603 orig_root, ref_root,
1604 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001605 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001606 if (ret) {
1607 faili = i;
1608 WARN_ON(1);
1609 goto fail;
1610 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001611 }
1612 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001613out:
1614 if (nr_extents) {
1615 if (level == 0)
1616 *nr_extents = nr_file_extents;
1617 else
1618 *nr_extents = nritems;
1619 }
1620 return 0;
1621fail:
1622 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001623 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001624}
1625
Zheng Yan31840ae2008-09-23 13:14:14 -04001626int btrfs_update_ref(struct btrfs_trans_handle *trans,
1627 struct btrfs_root *root, struct extent_buffer *orig_buf,
1628 struct extent_buffer *buf, int start_slot, int nr)
1629
1630{
1631 u64 bytenr;
1632 u64 ref_root;
1633 u64 orig_root;
1634 u64 ref_generation;
1635 u64 orig_generation;
1636 struct btrfs_key key;
1637 struct btrfs_file_extent_item *fi;
1638 int i;
1639 int ret;
1640 int slot;
1641 int level;
1642
1643 BUG_ON(start_slot < 0);
1644 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1645
1646 ref_root = btrfs_header_owner(buf);
1647 ref_generation = btrfs_header_generation(buf);
1648 orig_root = btrfs_header_owner(orig_buf);
1649 orig_generation = btrfs_header_generation(orig_buf);
1650 level = btrfs_header_level(buf);
1651
1652 if (!root->ref_cows) {
1653 if (level == 0 &&
1654 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1655 return 0;
1656 if (level != 0 &&
1657 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1658 return 0;
1659 }
1660
1661 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1662 cond_resched();
1663 if (level == 0) {
1664 btrfs_item_key_to_cpu(buf, &key, slot);
1665 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1666 continue;
1667 fi = btrfs_item_ptr(buf, slot,
1668 struct btrfs_file_extent_item);
1669 if (btrfs_file_extent_type(buf, fi) ==
1670 BTRFS_FILE_EXTENT_INLINE)
1671 continue;
1672 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1673 if (bytenr == 0)
1674 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001675 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1676 orig_buf->start, buf->start,
1677 orig_root, ref_root,
1678 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001679 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001680 if (ret)
1681 goto fail;
1682 } else {
1683 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001684 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1685 orig_buf->start, buf->start,
1686 orig_root, ref_root,
1687 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001688 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001689 if (ret)
1690 goto fail;
1691 }
1692 }
1693 return 0;
1694fail:
1695 WARN_ON(1);
1696 return -1;
1697}
1698
Chris Mason9078a3e2007-04-26 16:46:15 -04001699static int write_one_cache_group(struct btrfs_trans_handle *trans,
1700 struct btrfs_root *root,
1701 struct btrfs_path *path,
1702 struct btrfs_block_group_cache *cache)
1703{
1704 int ret;
1705 int pending_ret;
1706 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001707 unsigned long bi;
1708 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001709
Chris Mason9078a3e2007-04-26 16:46:15 -04001710 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001711 if (ret < 0)
1712 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001713 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001714
1715 leaf = path->nodes[0];
1716 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1717 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1718 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001719 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001720fail:
Chris Mason87ef2bb2008-10-30 11:23:27 -04001721 finish_current_insert(trans, extent_root, 0);
1722 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001723 if (ret)
1724 return ret;
1725 if (pending_ret)
1726 return pending_ret;
1727 return 0;
1728
1729}
1730
Chris Mason96b51792007-10-15 16:15:19 -04001731int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1732 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001733{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001734 struct btrfs_block_group_cache *cache, *entry;
1735 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001736 int err = 0;
1737 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001738 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001739 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001740
1741 path = btrfs_alloc_path();
1742 if (!path)
1743 return -ENOMEM;
1744
1745 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001746 cache = NULL;
1747 spin_lock(&root->fs_info->block_group_cache_lock);
1748 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1749 n; n = rb_next(n)) {
1750 entry = rb_entry(n, struct btrfs_block_group_cache,
1751 cache_node);
1752 if (entry->dirty) {
1753 cache = entry;
1754 break;
1755 }
1756 }
1757 spin_unlock(&root->fs_info->block_group_cache_lock);
1758
1759 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001760 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001761
Zheng Yane8569812008-09-26 10:05:48 -04001762 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001763 last += cache->key.offset;
1764
Chris Mason96b51792007-10-15 16:15:19 -04001765 err = write_one_cache_group(trans, root,
1766 path, cache);
1767 /*
1768 * if we fail to write the cache group, we want
1769 * to keep it marked dirty in hopes that a later
1770 * write will work
1771 */
1772 if (err) {
1773 werr = err;
1774 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001775 }
1776 }
1777 btrfs_free_path(path);
1778 return werr;
1779}
1780
Chris Mason593060d2008-03-25 16:50:33 -04001781static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1782 u64 total_bytes, u64 bytes_used,
1783 struct btrfs_space_info **space_info)
1784{
1785 struct btrfs_space_info *found;
1786
1787 found = __find_space_info(info, flags);
1788 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001789 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001790 found->total_bytes += total_bytes;
1791 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001792 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001793 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001794 *space_info = found;
1795 return 0;
1796 }
Yan Zhengc146afa2008-11-12 14:34:12 -05001797 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04001798 if (!found)
1799 return -ENOMEM;
1800
1801 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001802 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001803 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001804 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001805 found->flags = flags;
1806 found->total_bytes = total_bytes;
1807 found->bytes_used = bytes_used;
1808 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001809 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05001810 found->bytes_readonly = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001811 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001812 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001813 *space_info = found;
1814 return 0;
1815}
1816
Chris Mason8790d502008-04-03 16:29:03 -04001817static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1818{
1819 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001820 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001821 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001822 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001823 if (extra_flags) {
1824 if (flags & BTRFS_BLOCK_GROUP_DATA)
1825 fs_info->avail_data_alloc_bits |= extra_flags;
1826 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1827 fs_info->avail_metadata_alloc_bits |= extra_flags;
1828 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1829 fs_info->avail_system_alloc_bits |= extra_flags;
1830 }
1831}
Chris Mason593060d2008-03-25 16:50:33 -04001832
Yan Zhengc146afa2008-11-12 14:34:12 -05001833static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1834{
1835 spin_lock(&cache->space_info->lock);
1836 spin_lock(&cache->lock);
1837 if (!cache->ro) {
1838 cache->space_info->bytes_readonly += cache->key.offset -
1839 btrfs_block_group_used(&cache->item);
1840 cache->ro = 1;
1841 }
1842 spin_unlock(&cache->lock);
1843 spin_unlock(&cache->space_info->lock);
1844}
1845
Chris Masona061fc82008-05-07 11:43:44 -04001846static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001847{
Chris Masona061fc82008-05-07 11:43:44 -04001848 u64 num_devices = root->fs_info->fs_devices->num_devices;
1849
1850 if (num_devices == 1)
1851 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1852 if (num_devices < 4)
1853 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1854
Chris Masonec44a352008-04-28 15:29:52 -04001855 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1856 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001857 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001858 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001859 }
Chris Masonec44a352008-04-28 15:29:52 -04001860
1861 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001862 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001863 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001864 }
Chris Masonec44a352008-04-28 15:29:52 -04001865
1866 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1867 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1868 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1869 (flags & BTRFS_BLOCK_GROUP_DUP)))
1870 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1871 return flags;
1872}
1873
Chris Mason6324fbf2008-03-24 15:01:59 -04001874static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1875 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001876 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001877{
1878 struct btrfs_space_info *space_info;
1879 u64 thresh;
1880 u64 start;
1881 u64 num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001882 int ret = 0;
1883
1884 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001885
Chris Masona061fc82008-05-07 11:43:44 -04001886 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001887
Chris Mason6324fbf2008-03-24 15:01:59 -04001888 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001889 if (!space_info) {
1890 ret = update_space_info(extent_root->fs_info, flags,
1891 0, 0, &space_info);
1892 BUG_ON(ret);
1893 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001894 BUG_ON(!space_info);
1895
Josef Bacik25179202008-10-29 14:49:05 -04001896 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001897 if (space_info->force_alloc) {
1898 force = 1;
1899 space_info->force_alloc = 0;
1900 }
Josef Bacik25179202008-10-29 14:49:05 -04001901 if (space_info->full) {
1902 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001903 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001904 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001905
Yan Zhengc146afa2008-11-12 14:34:12 -05001906 thresh = space_info->total_bytes - space_info->bytes_readonly;
1907 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001908 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001909 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001910 space_info->bytes_reserved + alloc_bytes) < thresh) {
1911 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001912 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001913 }
Josef Bacik25179202008-10-29 14:49:05 -04001914 spin_unlock(&space_info->lock);
1915
Chris Mason6324fbf2008-03-24 15:01:59 -04001916 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
Josef Bacik25179202008-10-29 14:49:05 -04001917 if (ret) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001918printk("space info full %Lu\n", flags);
1919 space_info->full = 1;
Yan Zhengc146afa2008-11-12 14:34:12 -05001920 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001921 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001922
1923 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001924 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001925 BUG_ON(ret);
Chris Masona74a4b92008-06-25 16:01:31 -04001926out:
Yan Zhengc146afa2008-11-12 14:34:12 -05001927 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001928 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001929}
1930
Chris Mason9078a3e2007-04-26 16:46:15 -04001931static int update_block_group(struct btrfs_trans_handle *trans,
1932 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001933 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001934 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001935{
1936 struct btrfs_block_group_cache *cache;
1937 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001938 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001939 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001940 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001941
Chris Mason9078a3e2007-04-26 16:46:15 -04001942 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001943 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001944 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001945 return -1;
Chris Masondb945352007-10-15 16:15:53 -04001946 byte_in_group = bytenr - cache->key.objectid;
1947 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001948
Josef Bacik25179202008-10-29 14:49:05 -04001949 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001950 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001951 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001952 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001953 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001954 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001955 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001956 cache->space_info->bytes_used += num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001957 if (cache->ro) {
1958 cache->space_info->bytes_readonly -= num_bytes;
1959 WARN_ON(1);
1960 }
Chris Masonc286ac42008-07-22 23:06:41 -04001961 btrfs_set_block_group_used(&cache->item, old_val);
1962 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001963 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001964 } else {
Chris Masondb945352007-10-15 16:15:53 -04001965 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001966 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001967 if (cache->ro)
1968 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001969 btrfs_set_block_group_used(&cache->item, old_val);
1970 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001971 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001972 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001973 int ret;
1974 ret = btrfs_add_free_space(cache, bytenr,
1975 num_bytes);
1976 if (ret)
1977 return -1;
Chris Masone37c9e62007-05-09 20:13:14 -04001978 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001979 }
Chris Masondb945352007-10-15 16:15:53 -04001980 total -= num_bytes;
1981 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001982 }
1983 return 0;
1984}
Chris Mason6324fbf2008-03-24 15:01:59 -04001985
Chris Masona061fc82008-05-07 11:43:44 -04001986static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1987{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001988 struct btrfs_block_group_cache *cache;
1989
1990 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1991 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001992 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001993
1994 return cache->key.objectid;
Chris Masona061fc82008-05-07 11:43:44 -04001995}
1996
Chris Masone02119d2008-09-05 16:13:11 -04001997int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05001998 u64 bytenr, u64 num, int pin)
1999{
2000 u64 len;
2001 struct btrfs_block_group_cache *cache;
2002 struct btrfs_fs_info *fs_info = root->fs_info;
2003
Josef Bacik25179202008-10-29 14:49:05 -04002004 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05002005 if (pin) {
2006 set_extent_dirty(&fs_info->pinned_extents,
2007 bytenr, bytenr + num - 1, GFP_NOFS);
2008 } else {
2009 clear_extent_dirty(&fs_info->pinned_extents,
2010 bytenr, bytenr + num - 1, GFP_NOFS);
2011 }
2012 while (num > 0) {
2013 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002014 BUG_ON(!cache);
2015 len = min(num, cache->key.offset -
2016 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002017 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002018 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002019 spin_lock(&cache->lock);
2020 cache->pinned += len;
2021 cache->space_info->bytes_pinned += len;
2022 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002023 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002024 fs_info->total_pinned += len;
2025 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002026 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002027 spin_lock(&cache->lock);
2028 cache->pinned -= len;
2029 cache->space_info->bytes_pinned -= len;
2030 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002031 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002032 fs_info->total_pinned -= len;
2033 }
2034 bytenr += len;
2035 num -= len;
2036 }
2037 return 0;
2038}
Chris Mason9078a3e2007-04-26 16:46:15 -04002039
Zheng Yane8569812008-09-26 10:05:48 -04002040static int update_reserved_extents(struct btrfs_root *root,
2041 u64 bytenr, u64 num, int reserve)
2042{
2043 u64 len;
2044 struct btrfs_block_group_cache *cache;
2045 struct btrfs_fs_info *fs_info = root->fs_info;
2046
Zheng Yane8569812008-09-26 10:05:48 -04002047 while (num > 0) {
2048 cache = btrfs_lookup_block_group(fs_info, bytenr);
2049 BUG_ON(!cache);
2050 len = min(num, cache->key.offset -
2051 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04002052
2053 spin_lock(&cache->space_info->lock);
2054 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002055 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04002056 cache->reserved += len;
2057 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04002058 } else {
Zheng Yane8569812008-09-26 10:05:48 -04002059 cache->reserved -= len;
2060 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04002061 }
Josef Bacik25179202008-10-29 14:49:05 -04002062 spin_unlock(&cache->lock);
2063 spin_unlock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002064 bytenr += len;
2065 num -= len;
2066 }
2067 return 0;
2068}
2069
Chris Masond1310b22008-01-24 16:13:08 -05002070int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04002071{
Chris Masonccd467d2007-06-28 15:57:36 -04002072 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002073 u64 start;
2074 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002075 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04002076 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04002077
Josef Bacik25179202008-10-29 14:49:05 -04002078 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002079 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002080 ret = find_first_extent_bit(pinned_extents, last,
2081 &start, &end, EXTENT_DIRTY);
2082 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04002083 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04002084 set_extent_dirty(copy, start, end, GFP_NOFS);
2085 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04002086 }
Josef Bacik25179202008-10-29 14:49:05 -04002087 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002088 return 0;
2089}
2090
2091int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2092 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05002093 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05002094{
Chris Mason1a5bc162007-10-15 16:15:26 -04002095 u64 start;
2096 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002097 int ret;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002098 struct btrfs_block_group_cache *cache;
Chris Masona28ec192007-03-06 20:08:01 -05002099
Josef Bacik25179202008-10-29 14:49:05 -04002100 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002101 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002102 ret = find_first_extent_bit(unpin, 0, &start, &end,
2103 EXTENT_DIRTY);
2104 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05002105 break;
Chris Masone02119d2008-09-05 16:13:11 -04002106 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04002107 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002108 cache = btrfs_lookup_block_group(root->fs_info, start);
2109 if (cache->cached)
2110 btrfs_add_free_space(cache, start, end - start + 1);
Chris Masonc286ac42008-07-22 23:06:41 -04002111 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04002112 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002113 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04002114 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002115 }
Chris Masona28ec192007-03-06 20:08:01 -05002116 }
Josef Bacik25179202008-10-29 14:49:05 -04002117 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002118 return 0;
2119}
2120
Chris Mason98ed5172008-01-03 10:01:48 -05002121static int finish_current_insert(struct btrfs_trans_handle *trans,
Chris Mason87ef2bb2008-10-30 11:23:27 -04002122 struct btrfs_root *extent_root, int all)
Chris Mason037e6392007-03-07 11:50:24 -05002123{
Chris Mason7bb86312007-12-11 09:25:06 -05002124 u64 start;
2125 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002126 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002127 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002128 u64 skipped = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002129 struct btrfs_fs_info *info = extent_root->fs_info;
2130 struct btrfs_path *path;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002131 struct pending_extent_op *extent_op, *tmp;
2132 struct list_head insert_list, update_list;
Chris Mason037e6392007-03-07 11:50:24 -05002133 int ret;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002134 int num_inserts = 0, max_inserts;
Chris Mason037e6392007-03-07 11:50:24 -05002135
Chris Mason7bb86312007-12-11 09:25:06 -05002136 path = btrfs_alloc_path();
Josef Bacikf3465ca2008-11-12 14:19:50 -05002137 INIT_LIST_HEAD(&insert_list);
2138 INIT_LIST_HEAD(&update_list);
Chris Mason037e6392007-03-07 11:50:24 -05002139
Josef Bacikf3465ca2008-11-12 14:19:50 -05002140 max_inserts = extent_root->leafsize /
2141 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2142 sizeof(struct btrfs_extent_ref) +
2143 sizeof(struct btrfs_extent_item));
2144again:
2145 mutex_lock(&info->extent_ins_mutex);
2146 while (1) {
Josef Bacik25179202008-10-29 14:49:05 -04002147 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2148 &end, EXTENT_WRITEBACK);
2149 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002150 if (skipped && all && !num_inserts) {
2151 skipped = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002152 continue;
2153 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002154 mutex_unlock(&info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04002155 break;
Josef Bacik25179202008-10-29 14:49:05 -04002156 }
2157
2158 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2159 if (!ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002160 skipped = 1;
Chris Mason87ef2bb2008-10-30 11:23:27 -04002161 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002162 if (need_resched()) {
2163 mutex_unlock(&info->extent_ins_mutex);
2164 cond_resched();
2165 mutex_lock(&info->extent_ins_mutex);
2166 }
Josef Bacik25179202008-10-29 14:49:05 -04002167 continue;
2168 }
Chris Mason26b80032007-08-08 20:17:12 -04002169
Zheng Yan31840ae2008-09-23 13:14:14 -04002170 ret = get_state_private(&info->extent_ins, start, &priv);
2171 BUG_ON(ret);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002172 extent_op = (struct pending_extent_op *)(unsigned long) priv;
Josef Bacik25179202008-10-29 14:49:05 -04002173
Zheng Yan31840ae2008-09-23 13:14:14 -04002174 if (extent_op->type == PENDING_EXTENT_INSERT) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002175 num_inserts++;
2176 list_add_tail(&extent_op->list, &insert_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002177 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002178 if (num_inserts == max_inserts) {
2179 mutex_unlock(&info->extent_ins_mutex);
2180 break;
2181 }
2182 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2183 list_add_tail(&extent_op->list, &update_list);
2184 search = end + 1;
2185 } else {
2186 BUG();
2187 }
Chris Mason037e6392007-03-07 11:50:24 -05002188 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002189
2190 /*
2191 * process teh update list, clear the writeback bit for it, and if
2192 * somebody marked this thing for deletion then just unlock it and be
2193 * done, the free_extents will handle it
2194 */
2195 mutex_lock(&info->extent_ins_mutex);
2196 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2197 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2198 extent_op->bytenr + extent_op->num_bytes - 1,
2199 EXTENT_WRITEBACK, GFP_NOFS);
2200 if (extent_op->del) {
2201 list_del_init(&extent_op->list);
2202 unlock_extent(&info->extent_ins, extent_op->bytenr,
2203 extent_op->bytenr + extent_op->num_bytes
2204 - 1, GFP_NOFS);
2205 kfree(extent_op);
2206 }
2207 }
2208 mutex_unlock(&info->extent_ins_mutex);
2209
2210 /*
2211 * still have things left on the update list, go ahead an update
2212 * everything
2213 */
2214 if (!list_empty(&update_list)) {
2215 ret = update_backrefs(trans, extent_root, path, &update_list);
2216 BUG_ON(ret);
2217 }
2218
2219 /*
2220 * if no inserts need to be done, but we skipped some extents and we
2221 * need to make sure everything is cleaned then reset everything and
2222 * go back to the beginning
2223 */
2224 if (!num_inserts && all && skipped) {
2225 search = 0;
2226 skipped = 0;
2227 INIT_LIST_HEAD(&update_list);
2228 INIT_LIST_HEAD(&insert_list);
2229 goto again;
2230 } else if (!num_inserts) {
2231 goto out;
2232 }
2233
2234 /*
2235 * process the insert extents list. Again if we are deleting this
2236 * extent, then just unlock it, pin down the bytes if need be, and be
2237 * done with it. Saves us from having to actually insert the extent
2238 * into the tree and then subsequently come along and delete it
2239 */
2240 mutex_lock(&info->extent_ins_mutex);
2241 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2242 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2243 extent_op->bytenr + extent_op->num_bytes - 1,
2244 EXTENT_WRITEBACK, GFP_NOFS);
2245 if (extent_op->del) {
2246 list_del_init(&extent_op->list);
2247 unlock_extent(&info->extent_ins, extent_op->bytenr,
2248 extent_op->bytenr + extent_op->num_bytes
2249 - 1, GFP_NOFS);
2250
2251 mutex_lock(&extent_root->fs_info->pinned_mutex);
2252 ret = pin_down_bytes(trans, extent_root,
2253 extent_op->bytenr,
2254 extent_op->num_bytes, 0);
2255 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2256
2257 ret = update_block_group(trans, extent_root,
2258 extent_op->bytenr,
2259 extent_op->num_bytes,
2260 0, ret > 0);
2261 BUG_ON(ret);
2262 kfree(extent_op);
2263 num_inserts--;
2264 }
2265 }
2266 mutex_unlock(&info->extent_ins_mutex);
2267
2268 ret = insert_extents(trans, extent_root, path, &insert_list,
2269 num_inserts);
2270 BUG_ON(ret);
2271
2272 /*
2273 * if we broke out of the loop in order to insert stuff because we hit
2274 * the maximum number of inserts at a time we can handle, then loop
2275 * back and pick up where we left off
2276 */
2277 if (num_inserts == max_inserts) {
2278 INIT_LIST_HEAD(&insert_list);
2279 INIT_LIST_HEAD(&update_list);
2280 num_inserts = 0;
2281 goto again;
2282 }
2283
2284 /*
2285 * again, if we need to make absolutely sure there are no more pending
2286 * extent operations left and we know that we skipped some, go back to
2287 * the beginning and do it all again
2288 */
2289 if (all && skipped) {
2290 INIT_LIST_HEAD(&insert_list);
2291 INIT_LIST_HEAD(&update_list);
2292 search = 0;
2293 skipped = 0;
2294 num_inserts = 0;
2295 goto again;
2296 }
2297out:
Chris Mason7bb86312007-12-11 09:25:06 -05002298 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05002299 return 0;
2300}
2301
Zheng Yan31840ae2008-09-23 13:14:14 -04002302static int pin_down_bytes(struct btrfs_trans_handle *trans,
2303 struct btrfs_root *root,
2304 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04002305{
Chris Mason1a5bc162007-10-15 16:15:26 -04002306 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002307 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04002308
Zheng Yan31840ae2008-09-23 13:14:14 -04002309 if (is_data)
2310 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002311
Zheng Yan31840ae2008-09-23 13:14:14 -04002312 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2313 if (!buf)
2314 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002315
Zheng Yan31840ae2008-09-23 13:14:14 -04002316 /* we can reuse a block if it hasn't been written
2317 * and it is from this transaction. We can't
2318 * reuse anything from the tree log root because
2319 * it has tiny sub-transactions.
2320 */
2321 if (btrfs_buffer_uptodate(buf, 0) &&
2322 btrfs_try_tree_lock(buf)) {
2323 u64 header_owner = btrfs_header_owner(buf);
2324 u64 header_transid = btrfs_header_generation(buf);
2325 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04002326 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04002327 header_transid == trans->transid &&
2328 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2329 clean_tree_block(NULL, root, buf);
2330 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002331 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002332 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04002333 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002334 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04002335 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002336 free_extent_buffer(buf);
2337pinit:
2338 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2339
Chris Masonbe744172007-05-06 10:15:01 -04002340 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002341 return 0;
2342}
2343
Chris Masona28ec192007-03-06 20:08:01 -05002344/*
2345 * remove an extent from the root, returns 0 on success
2346 */
Zheng Yan31840ae2008-09-23 13:14:14 -04002347static int __free_extent(struct btrfs_trans_handle *trans,
2348 struct btrfs_root *root,
2349 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002350 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002351 u64 owner_objectid, int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05002352{
Chris Mason5caf2a02007-04-02 11:20:42 -04002353 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04002354 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04002355 struct btrfs_fs_info *info = root->fs_info;
2356 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002357 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05002358 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05002359 int extent_slot = 0;
2360 int found_extent = 0;
2361 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04002362 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04002363 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05002364
Chris Masondb945352007-10-15 16:15:53 -04002365 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04002366 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04002367 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04002368 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002369 if (!path)
2370 return -ENOMEM;
2371
Chris Mason3c12ac72008-04-21 12:01:38 -04002372 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002373 ret = lookup_extent_backref(trans, extent_root, path,
2374 bytenr, parent, root_objectid,
2375 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05002376 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002377 struct btrfs_key found_key;
2378 extent_slot = path->slots[0];
2379 while(extent_slot > 0) {
2380 extent_slot--;
2381 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2382 extent_slot);
2383 if (found_key.objectid != bytenr)
2384 break;
2385 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2386 found_key.offset == num_bytes) {
2387 found_extent = 1;
2388 break;
2389 }
2390 if (path->slots[0] - extent_slot > 5)
2391 break;
2392 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002393 if (!found_extent) {
2394 ret = remove_extent_backref(trans, extent_root, path);
2395 BUG_ON(ret);
2396 btrfs_release_path(extent_root, path);
2397 ret = btrfs_search_slot(trans, extent_root,
2398 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002399 if (ret) {
2400 printk(KERN_ERR "umm, got %d back from search"
2401 ", was looking for %Lu\n", ret,
2402 bytenr);
2403 btrfs_print_leaf(extent_root, path->nodes[0]);
2404 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002405 BUG_ON(ret);
2406 extent_slot = path->slots[0];
2407 }
Chris Mason7bb86312007-12-11 09:25:06 -05002408 } else {
2409 btrfs_print_leaf(extent_root, path->nodes[0]);
2410 WARN_ON(1);
2411 printk("Unable to find ref byte nr %Lu root %Lu "
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002412 "gen %Lu owner %Lu\n", bytenr,
2413 root_objectid, ref_generation, owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05002414 }
Chris Mason5f39d392007-10-15 16:14:19 -04002415
2416 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05002417 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04002418 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002419 refs = btrfs_extent_refs(leaf, ei);
2420 BUG_ON(refs == 0);
2421 refs -= 1;
2422 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05002423
Chris Mason5f39d392007-10-15 16:14:19 -04002424 btrfs_mark_buffer_dirty(leaf);
2425
Chris Mason952fcca2008-02-18 16:33:44 -05002426 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002427 struct btrfs_extent_ref *ref;
2428 ref = btrfs_item_ptr(leaf, path->slots[0],
2429 struct btrfs_extent_ref);
2430 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002431 /* if the back ref and the extent are next to each other
2432 * they get deleted below in one shot
2433 */
2434 path->slots[0] = extent_slot;
2435 num_to_del = 2;
2436 } else if (found_extent) {
2437 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002438 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05002439 BUG_ON(ret);
2440 /* if refs are 0, we need to setup the path for deletion */
2441 if (refs == 0) {
2442 btrfs_release_path(extent_root, path);
2443 ret = btrfs_search_slot(trans, extent_root, &key, path,
2444 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002445 BUG_ON(ret);
2446 }
2447 }
2448
Chris Masoncf27e1e2007-03-13 09:49:06 -04002449 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002450 u64 super_used;
2451 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01002452#ifdef BIO_RW_DISCARD
2453 u64 map_length = num_bytes;
2454 struct btrfs_multi_bio *multi = NULL;
2455#endif
Chris Mason78fae272007-03-25 11:35:08 -04002456
2457 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002458 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002459 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2460 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04002461 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05002462 if (ret > 0)
2463 mark_free = 1;
2464 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04002465 }
2466
Josef Bacik58176a92007-08-29 15:47:34 -04002467 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002468 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002469 super_used = btrfs_super_bytes_used(&info->super_copy);
2470 btrfs_set_super_bytes_used(&info->super_copy,
2471 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002472 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04002473
2474 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002475 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002476 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04002477 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05002478 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2479 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04002480 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04002481 btrfs_release_path(extent_root, path);
Chris Masondb945352007-10-15 16:15:53 -04002482 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002483 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04002484 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01002485
2486#ifdef BIO_RW_DISCARD
2487 /* Tell the block device(s) that the sectors can be discarded */
2488 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2489 bytenr, &map_length, &multi, 0);
2490 if (!ret) {
2491 struct btrfs_bio_stripe *stripe = multi->stripes;
2492 int i;
2493
2494 if (map_length > num_bytes)
2495 map_length = num_bytes;
2496
2497 for (i = 0; i < multi->num_stripes; i++, stripe++) {
2498 blkdev_issue_discard(stripe->dev->bdev,
2499 stripe->physical >> 9,
2500 map_length >> 9);
2501 }
2502 kfree(multi);
2503 }
2504#endif
Chris Masona28ec192007-03-06 20:08:01 -05002505 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002506 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002507 finish_current_insert(trans, extent_root, 0);
Chris Masona28ec192007-03-06 20:08:01 -05002508 return ret;
2509}
2510
2511/*
Chris Masonfec577f2007-02-26 10:40:21 -05002512 * find all the blocks marked as pending in the radix tree and remove
2513 * them from the extent map
2514 */
Chris Masone089f052007-03-16 16:20:31 -04002515static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -04002516 btrfs_root *extent_root, int all)
Chris Masonfec577f2007-02-26 10:40:21 -05002517{
2518 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002519 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002520 u64 start;
2521 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002522 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002523 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002524 int nr = 0, skipped = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002525 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002526 struct extent_io_tree *extent_ins;
2527 struct pending_extent_op *extent_op;
Josef Bacik25179202008-10-29 14:49:05 -04002528 struct btrfs_fs_info *info = extent_root->fs_info;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002529 struct list_head delete_list;
Chris Mason8ef97622007-03-26 10:15:30 -04002530
Josef Bacikf3465ca2008-11-12 14:19:50 -05002531 INIT_LIST_HEAD(&delete_list);
Zheng Yan31840ae2008-09-23 13:14:14 -04002532 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc162007-10-15 16:15:26 -04002533 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002534
Josef Bacikf3465ca2008-11-12 14:19:50 -05002535again:
2536 mutex_lock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002537 while(1) {
Josef Bacik25179202008-10-29 14:49:05 -04002538 ret = find_first_extent_bit(pending_del, search, &start, &end,
2539 EXTENT_WRITEBACK);
2540 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002541 if (all && skipped && !nr) {
Josef Bacik25179202008-10-29 14:49:05 -04002542 search = 0;
2543 continue;
2544 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002545 mutex_unlock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002546 break;
Josef Bacik25179202008-10-29 14:49:05 -04002547 }
2548
2549 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2550 if (!ret) {
2551 search = end+1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002552 skipped = 1;
2553
2554 if (need_resched()) {
2555 mutex_unlock(&info->extent_ins_mutex);
2556 cond_resched();
2557 mutex_lock(&info->extent_ins_mutex);
2558 }
2559
Josef Bacik25179202008-10-29 14:49:05 -04002560 continue;
2561 }
2562 BUG_ON(ret < 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002563
2564 ret = get_state_private(pending_del, start, &priv);
2565 BUG_ON(ret);
2566 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2567
Josef Bacik25179202008-10-29 14:49:05 -04002568 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
Chris Mason1a5bc162007-10-15 16:15:26 -04002569 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002570 if (!test_range_bit(extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04002571 EXTENT_WRITEBACK, 0)) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002572 list_add_tail(&extent_op->list, &delete_list);
2573 nr++;
Chris Masonc286ac42008-07-22 23:06:41 -04002574 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002575 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002576
2577 ret = get_state_private(&info->extent_ins, start,
2578 &priv);
Zheng Yan31840ae2008-09-23 13:14:14 -04002579 BUG_ON(ret);
2580 extent_op = (struct pending_extent_op *)
Josef Bacik25179202008-10-29 14:49:05 -04002581 (unsigned long)priv;
Zheng Yan31840ae2008-09-23 13:14:14 -04002582
Josef Bacik25179202008-10-29 14:49:05 -04002583 clear_extent_bits(&info->extent_ins, start, end,
2584 EXTENT_WRITEBACK, GFP_NOFS);
2585
Josef Bacikf3465ca2008-11-12 14:19:50 -05002586 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2587 list_add_tail(&extent_op->list, &delete_list);
2588 search = end + 1;
2589 nr++;
2590 continue;
2591 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002592
Josef Bacik25179202008-10-29 14:49:05 -04002593 mutex_lock(&extent_root->fs_info->pinned_mutex);
2594 ret = pin_down_bytes(trans, extent_root, start,
2595 end + 1 - start, 0);
2596 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2597
Zheng Yan31840ae2008-09-23 13:14:14 -04002598 ret = update_block_group(trans, extent_root, start,
Josef Bacik25179202008-10-29 14:49:05 -04002599 end + 1 - start, 0, ret > 0);
2600
Josef Bacikf3465ca2008-11-12 14:19:50 -05002601 unlock_extent(extent_ins, start, end, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002602 BUG_ON(ret);
2603 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002604 }
Chris Mason1a5bc162007-10-15 16:15:26 -04002605 if (ret)
2606 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04002607
Josef Bacikf3465ca2008-11-12 14:19:50 -05002608 search = end + 1;
2609
2610 if (need_resched()) {
2611 mutex_unlock(&info->extent_ins_mutex);
2612 cond_resched();
2613 mutex_lock(&info->extent_ins_mutex);
2614 }
Chris Masonfec577f2007-02-26 10:40:21 -05002615 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002616
2617 if (nr) {
2618 ret = free_extents(trans, extent_root, &delete_list);
2619 BUG_ON(ret);
2620 }
2621
2622 if (all && skipped) {
2623 INIT_LIST_HEAD(&delete_list);
2624 search = 0;
2625 nr = 0;
2626 goto again;
2627 }
2628
Chris Masone20d96d2007-03-22 12:13:20 -04002629 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002630}
2631
2632/*
2633 * remove an extent from the root, returns 0 on success
2634 */
Chris Mason925baed2008-06-25 16:01:30 -04002635static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002636 struct btrfs_root *root,
2637 u64 bytenr, u64 num_bytes, u64 parent,
2638 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002639 u64 owner_objectid, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002640{
Chris Mason9f5fae22007-03-20 14:38:32 -04002641 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002642 int pending_ret;
2643 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002644
Chris Masondb945352007-10-15 16:15:53 -04002645 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002646 if (root == extent_root) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002647 struct pending_extent_op *extent_op = NULL;
2648
2649 mutex_lock(&root->fs_info->extent_ins_mutex);
2650 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2651 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2652 u64 priv;
2653 ret = get_state_private(&root->fs_info->extent_ins,
2654 bytenr, &priv);
2655 BUG_ON(ret);
2656 extent_op = (struct pending_extent_op *)
2657 (unsigned long)priv;
2658
2659 extent_op->del = 1;
2660 if (extent_op->type == PENDING_EXTENT_INSERT) {
2661 mutex_unlock(&root->fs_info->extent_ins_mutex);
2662 return 0;
2663 }
2664 }
2665
2666 if (extent_op) {
2667 ref_generation = extent_op->orig_generation;
2668 parent = extent_op->orig_parent;
2669 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002670
2671 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2672 BUG_ON(!extent_op);
2673
2674 extent_op->type = PENDING_EXTENT_DELETE;
2675 extent_op->bytenr = bytenr;
2676 extent_op->num_bytes = num_bytes;
2677 extent_op->parent = parent;
2678 extent_op->orig_parent = parent;
2679 extent_op->generation = ref_generation;
2680 extent_op->orig_generation = ref_generation;
2681 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002682 INIT_LIST_HEAD(&extent_op->list);
2683 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002684
2685 set_extent_bits(&root->fs_info->pending_del,
2686 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04002687 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002688 set_state_private(&root->fs_info->pending_del,
2689 bytenr, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002690 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002691 return 0;
2692 }
Chris Mason4bef0842008-09-08 11:18:08 -04002693 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002694 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2695 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002696 struct btrfs_block_group_cache *cache;
2697
Chris Masond00aff02008-09-11 15:54:42 -04002698 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002699 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2700 BUG_ON(!cache);
2701 btrfs_add_free_space(cache, bytenr, num_bytes);
Zheng Yane8569812008-09-26 10:05:48 -04002702 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002703 return 0;
2704 }
Chris Mason4bef0842008-09-08 11:18:08 -04002705 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002706 }
Chris Mason4bef0842008-09-08 11:18:08 -04002707
2708 /* if data pin when any transaction has committed this */
2709 if (ref_generation != trans->transid)
2710 pin = 1;
2711
Zheng Yan31840ae2008-09-23 13:14:14 -04002712 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002713 root_objectid, ref_generation,
2714 owner_objectid, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002715
Chris Mason87ef2bb2008-10-30 11:23:27 -04002716 finish_current_insert(trans, root->fs_info->extent_root, 0);
2717 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002718 return ret ? ret : pending_ret;
2719}
2720
Chris Mason925baed2008-06-25 16:01:30 -04002721int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002722 struct btrfs_root *root,
2723 u64 bytenr, u64 num_bytes, u64 parent,
2724 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002725 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002726{
2727 int ret;
2728
Zheng Yan31840ae2008-09-23 13:14:14 -04002729 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002730 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002731 owner_objectid, pin);
Chris Mason925baed2008-06-25 16:01:30 -04002732 return ret;
2733}
2734
Chris Mason87ee04e2007-11-30 11:30:34 -05002735static u64 stripe_align(struct btrfs_root *root, u64 val)
2736{
2737 u64 mask = ((u64)root->stripesize - 1);
2738 u64 ret = (val + mask) & ~mask;
2739 return ret;
2740}
2741
Chris Masonfec577f2007-02-26 10:40:21 -05002742/*
2743 * walks the btree of allocated extents and find a hole of a given size.
2744 * The key ins is changed to record the hole:
2745 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002746 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002747 * ins->offset == number of blocks
2748 * Any available blocks before search_start are skipped.
2749 */
Chris Mason98ed5172008-01-03 10:01:48 -05002750static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2751 struct btrfs_root *orig_root,
2752 u64 num_bytes, u64 empty_size,
2753 u64 search_start, u64 search_end,
2754 u64 hint_byte, struct btrfs_key *ins,
2755 u64 exclude_start, u64 exclude_nr,
2756 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002757{
Josef Bacik80eb2342008-10-29 14:49:05 -04002758 int ret = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -04002759 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002760 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002761 u64 *last_ptr = NULL;
Chris Mason43662112008-11-07 09:06:11 -05002762 u64 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002763 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002764 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002765 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002766 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002767 struct list_head *head = NULL, *cur = NULL;
2768 int loop = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002769 int extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002770 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002771
Chris Masondb945352007-10-15 16:15:53 -04002772 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002773 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002774 ins->objectid = 0;
2775 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002776
Chris Mason0ef3e662008-05-24 14:04:53 -04002777 if (orig_root->ref_cows || empty_size)
2778 allowed_chunk_alloc = 1;
2779
Chris Mason239b14b2008-03-24 15:02:07 -04002780 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2781 last_ptr = &root->fs_info->last_alloc;
Chris Mason43662112008-11-07 09:06:11 -05002782 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002783 }
2784
Josef Bacik0f9dd462008-09-23 13:14:11 -04002785 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002786 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002787
Chris Mason239b14b2008-03-24 15:02:07 -04002788 if (last_ptr) {
Chris Mason43662112008-11-07 09:06:11 -05002789 if (*last_ptr) {
Chris Mason239b14b2008-03-24 15:02:07 -04002790 hint_byte = *last_ptr;
Chris Mason43662112008-11-07 09:06:11 -05002791 last_wanted = *last_ptr;
2792 } else
Chris Mason239b14b2008-03-24 15:02:07 -04002793 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002794 } else {
2795 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002796 }
Chris Masona061fc82008-05-07 11:43:44 -04002797 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002798 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04002799
Chris Mason42e70e72008-11-07 18:17:11 -05002800 if (last_wanted && search_start != last_wanted) {
Chris Mason43662112008-11-07 09:06:11 -05002801 last_wanted = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002802 empty_size += empty_cluster;
2803 }
Chris Mason43662112008-11-07 09:06:11 -05002804
Chris Mason42e70e72008-11-07 18:17:11 -05002805 total_needed += empty_size;
Josef Bacik80eb2342008-10-29 14:49:05 -04002806 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
Yan Zhengd899e052008-10-30 14:25:28 -04002807 if (!block_group)
2808 block_group = btrfs_lookup_first_block_group(root->fs_info,
2809 search_start);
Josef Bacik80eb2342008-10-29 14:49:05 -04002810 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002811
Josef Bacik80eb2342008-10-29 14:49:05 -04002812 down_read(&space_info->groups_sem);
2813 while (1) {
2814 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002815 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002816 * the only way this happens if our hint points to a block
2817 * group thats not of the proper type, while looping this
2818 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002819 */
Chris Mason8a1413a22008-11-10 16:13:54 -05002820 if (empty_size)
2821 extra_loop = 1;
2822
Chris Mason42e70e72008-11-07 18:17:11 -05002823 if (!block_group)
2824 goto new_group_no_lock;
2825
Josef Bacik25179202008-10-29 14:49:05 -04002826 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002827 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002828 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002829
Josef Bacik80eb2342008-10-29 14:49:05 -04002830 ret = cache_block_group(root, block_group);
Josef Bacik25179202008-10-29 14:49:05 -04002831 if (ret) {
2832 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002833 break;
Josef Bacik25179202008-10-29 14:49:05 -04002834 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002835
Josef Bacik80eb2342008-10-29 14:49:05 -04002836 if (block_group->ro)
2837 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002838
Josef Bacik80eb2342008-10-29 14:49:05 -04002839 free_space = btrfs_find_free_space(block_group, search_start,
2840 total_needed);
2841 if (free_space) {
2842 u64 start = block_group->key.objectid;
2843 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002844 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002845
Josef Bacik80eb2342008-10-29 14:49:05 -04002846 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002847
Josef Bacik80eb2342008-10-29 14:49:05 -04002848 /* move on to the next group */
2849 if (search_start + num_bytes >= search_end)
2850 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002851
Josef Bacik80eb2342008-10-29 14:49:05 -04002852 /* move on to the next group */
2853 if (search_start + num_bytes > end)
2854 goto new_group;
2855
Chris Mason43662112008-11-07 09:06:11 -05002856 if (last_wanted && search_start != last_wanted) {
Chris Mason3b7885b2008-11-06 21:48:27 -05002857 total_needed += empty_cluster;
Chris Mason8a1413a22008-11-10 16:13:54 -05002858 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002859 last_wanted = 0;
Chris Mason3b7885b2008-11-06 21:48:27 -05002860 /*
2861 * if search_start is still in this block group
2862 * then we just re-search this block group
2863 */
2864 if (search_start >= start &&
2865 search_start < end) {
2866 mutex_unlock(&block_group->alloc_mutex);
2867 continue;
2868 }
2869
2870 /* else we go to the next block group */
2871 goto new_group;
2872 }
2873
Josef Bacik80eb2342008-10-29 14:49:05 -04002874 if (exclude_nr > 0 &&
2875 (search_start + num_bytes > exclude_start &&
2876 search_start < exclude_start + exclude_nr)) {
2877 search_start = exclude_start + exclude_nr;
2878 /*
2879 * if search_start is still in this block group
2880 * then we just re-search this block group
2881 */
2882 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002883 search_start < end) {
2884 mutex_unlock(&block_group->alloc_mutex);
Chris Mason43662112008-11-07 09:06:11 -05002885 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002886 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002887 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002888
2889 /* else we go to the next block group */
2890 goto new_group;
2891 }
2892
2893 ins->objectid = search_start;
2894 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002895
2896 btrfs_remove_free_space_lock(block_group, search_start,
2897 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002898 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002899 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002900 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002901 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002902new_group:
Chris Mason42e70e72008-11-07 18:17:11 -05002903 mutex_unlock(&block_group->alloc_mutex);
2904new_group_no_lock:
Chris Masonf5a31e12008-11-10 11:47:09 -05002905 /* don't try to compare new allocations against the
2906 * last allocation any more
2907 */
Chris Mason43662112008-11-07 09:06:11 -05002908 last_wanted = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002909
Josef Bacik80eb2342008-10-29 14:49:05 -04002910 /*
2911 * Here's how this works.
2912 * loop == 0: we were searching a block group via a hint
2913 * and didn't find anything, so we start at
2914 * the head of the block groups and keep searching
2915 * loop == 1: we're searching through all of the block groups
2916 * if we hit the head again we have searched
2917 * all of the block groups for this space and we
2918 * need to try and allocate, if we cant error out.
2919 * loop == 2: we allocated more space and are looping through
2920 * all of the block groups again.
2921 */
2922 if (loop == 0) {
2923 head = &space_info->block_groups;
2924 cur = head->next;
Josef Bacik80eb2342008-10-29 14:49:05 -04002925 loop++;
2926 } else if (loop == 1 && cur == head) {
Chris Masonf5a31e12008-11-10 11:47:09 -05002927 int keep_going;
Chris Mason42e70e72008-11-07 18:17:11 -05002928
Chris Masonf5a31e12008-11-10 11:47:09 -05002929 /* at this point we give up on the empty_size
2930 * allocations and just try to allocate the min
2931 * space.
2932 *
2933 * The extra_loop field was set if an empty_size
2934 * allocation was attempted above, and if this
2935 * is try we need to try the loop again without
2936 * the additional empty_size.
2937 */
Chris Mason5b7c3fc2008-11-10 07:26:33 -05002938 total_needed -= empty_size;
2939 empty_size = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002940 keep_going = extra_loop;
2941 loop++;
Chris Mason42e70e72008-11-07 18:17:11 -05002942
Josef Bacik80eb2342008-10-29 14:49:05 -04002943 if (allowed_chunk_alloc && !chunk_alloc_done) {
2944 up_read(&space_info->groups_sem);
2945 ret = do_chunk_alloc(trans, root, num_bytes +
2946 2 * 1024 * 1024, data, 1);
Josef Bacik80eb2342008-10-29 14:49:05 -04002947 down_read(&space_info->groups_sem);
Chris Mason2ed6d662008-11-13 09:59:33 -05002948 if (ret < 0)
2949 goto loop_check;
Josef Bacik80eb2342008-10-29 14:49:05 -04002950 head = &space_info->block_groups;
Chris Masonf5a31e12008-11-10 11:47:09 -05002951 /*
2952 * we've allocated a new chunk, keep
2953 * trying
2954 */
2955 keep_going = 1;
Josef Bacik80eb2342008-10-29 14:49:05 -04002956 chunk_alloc_done = 1;
2957 } else if (!allowed_chunk_alloc) {
2958 space_info->force_alloc = 1;
Chris Masonf5a31e12008-11-10 11:47:09 -05002959 }
Chris Mason2ed6d662008-11-13 09:59:33 -05002960loop_check:
Chris Masonf5a31e12008-11-10 11:47:09 -05002961 if (keep_going) {
2962 cur = head->next;
2963 extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002964 } else {
2965 break;
2966 }
2967 } else if (cur == head) {
2968 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002969 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002970
2971 block_group = list_entry(cur, struct btrfs_block_group_cache,
2972 list);
2973 search_start = block_group->key.objectid;
2974 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002975 }
Chris Mason0b86a832008-03-24 15:01:56 -04002976
Josef Bacik80eb2342008-10-29 14:49:05 -04002977 /* we found what we needed */
2978 if (ins->objectid) {
2979 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2980 trans->block_group = block_group;
2981
2982 if (last_ptr)
2983 *last_ptr = ins->objectid + ins->offset;
2984 ret = 0;
2985 } else if (!ret) {
2986 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04002987 }
Chris Mason0b86a832008-03-24 15:01:56 -04002988
Josef Bacik80eb2342008-10-29 14:49:05 -04002989 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05002990 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002991}
Chris Masonec44a352008-04-28 15:29:52 -04002992
Josef Bacik0f9dd462008-09-23 13:14:11 -04002993static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2994{
2995 struct btrfs_block_group_cache *cache;
2996 struct list_head *l;
2997
2998 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
Zheng Yane8569812008-09-26 10:05:48 -04002999 info->total_bytes - info->bytes_used - info->bytes_pinned -
3000 info->bytes_reserved, (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04003001
Josef Bacik80eb2342008-10-29 14:49:05 -04003002 down_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003003 list_for_each(l, &info->block_groups) {
3004 cache = list_entry(l, struct btrfs_block_group_cache, list);
3005 spin_lock(&cache->lock);
3006 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
Zheng Yane8569812008-09-26 10:05:48 -04003007 "%Lu pinned %Lu reserved\n",
Josef Bacik0f9dd462008-09-23 13:14:11 -04003008 cache->key.objectid, cache->key.offset,
Zheng Yane8569812008-09-26 10:05:48 -04003009 btrfs_block_group_used(&cache->item),
3010 cache->pinned, cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003011 btrfs_dump_free_space(cache, bytes);
3012 spin_unlock(&cache->lock);
3013 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003014 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003015}
Zheng Yane8569812008-09-26 10:05:48 -04003016
Chris Masone6dcd2d2008-07-17 12:53:50 -04003017static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3018 struct btrfs_root *root,
3019 u64 num_bytes, u64 min_alloc_size,
3020 u64 empty_size, u64 hint_byte,
3021 u64 search_end, struct btrfs_key *ins,
3022 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05003023{
3024 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04003025 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003026 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04003027 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003028
Chris Mason6324fbf2008-03-24 15:01:59 -04003029 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04003030 alloc_profile = info->avail_data_alloc_bits &
3031 info->data_alloc_profile;
3032 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003033 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04003034 alloc_profile = info->avail_system_alloc_bits &
3035 info->system_alloc_profile;
3036 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003037 } else {
Chris Mason8790d502008-04-03 16:29:03 -04003038 alloc_profile = info->avail_metadata_alloc_bits &
3039 info->metadata_alloc_profile;
3040 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003041 }
Chris Mason98d20f62008-04-14 09:46:10 -04003042again:
Chris Masona061fc82008-05-07 11:43:44 -04003043 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04003044 /*
3045 * the only place that sets empty_size is btrfs_realloc_node, which
3046 * is not called recursively on allocations
3047 */
3048 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04003049 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04003050 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003051 2 * 1024 * 1024,
3052 BTRFS_BLOCK_GROUP_METADATA |
3053 (info->metadata_alloc_profile &
3054 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003055 }
3056 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003057 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003058 }
Chris Mason0b86a832008-03-24 15:01:56 -04003059
Chris Masondb945352007-10-15 16:15:53 -04003060 WARN_ON(num_bytes < root->sectorsize);
3061 ret = find_free_extent(trans, root, num_bytes, empty_size,
3062 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04003063 trans->alloc_exclude_start,
3064 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04003065
Chris Mason98d20f62008-04-14 09:46:10 -04003066 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3067 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003068 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003069 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04003070 do_chunk_alloc(trans, root->fs_info->extent_root,
3071 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003072 goto again;
3073 }
Chris Masonec44a352008-04-28 15:29:52 -04003074 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003075 struct btrfs_space_info *sinfo;
3076
3077 sinfo = __find_space_info(root->fs_info, data);
3078 printk("allocation failed flags %Lu, wanted %Lu\n",
3079 data, num_bytes);
3080 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04003081 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04003082 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003083
3084 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003085}
3086
Chris Mason65b51a02008-08-01 15:11:20 -04003087int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3088{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003089 struct btrfs_block_group_cache *cache;
3090
Josef Bacik0f9dd462008-09-23 13:14:11 -04003091 cache = btrfs_lookup_block_group(root->fs_info, start);
3092 if (!cache) {
3093 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003094 return -ENOSPC;
3095 }
3096 btrfs_add_free_space(cache, start, len);
Zheng Yan1a40e232008-09-26 10:09:34 -04003097 update_reserved_extents(root, start, len, 0);
Chris Mason65b51a02008-08-01 15:11:20 -04003098 return 0;
3099}
3100
Chris Masone6dcd2d2008-07-17 12:53:50 -04003101int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3102 struct btrfs_root *root,
3103 u64 num_bytes, u64 min_alloc_size,
3104 u64 empty_size, u64 hint_byte,
3105 u64 search_end, struct btrfs_key *ins,
3106 u64 data)
3107{
3108 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003109 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3110 empty_size, hint_byte, search_end, ins,
3111 data);
Zheng Yane8569812008-09-26 10:05:48 -04003112 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003113 return ret;
3114}
3115
3116static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003117 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003118 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003119 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003120{
3121 int ret;
3122 int pending_ret;
3123 u64 super_used;
3124 u64 root_used;
3125 u64 num_bytes = ins->offset;
3126 u32 sizes[2];
3127 struct btrfs_fs_info *info = root->fs_info;
3128 struct btrfs_root *extent_root = info->extent_root;
3129 struct btrfs_extent_item *extent_item;
3130 struct btrfs_extent_ref *ref;
3131 struct btrfs_path *path;
3132 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04003133
Zheng Yan31840ae2008-09-23 13:14:14 -04003134 if (parent == 0)
3135 parent = ins->objectid;
3136
Josef Bacik58176a92007-08-29 15:47:34 -04003137 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04003138 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04003139 super_used = btrfs_super_bytes_used(&info->super_copy);
3140 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04003141 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04003142
Josef Bacik58176a92007-08-29 15:47:34 -04003143 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04003144 root_used = btrfs_root_used(&root->root_item);
3145 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04003146
Chris Mason26b80032007-08-08 20:17:12 -04003147 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003148 struct pending_extent_op *extent_op;
3149
3150 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3151 BUG_ON(!extent_op);
3152
3153 extent_op->type = PENDING_EXTENT_INSERT;
3154 extent_op->bytenr = ins->objectid;
3155 extent_op->num_bytes = ins->offset;
3156 extent_op->parent = parent;
3157 extent_op->orig_parent = 0;
3158 extent_op->generation = ref_generation;
3159 extent_op->orig_generation = 0;
3160 extent_op->level = (int)owner;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003161 INIT_LIST_HEAD(&extent_op->list);
3162 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003163
Josef Bacik25179202008-10-29 14:49:05 -04003164 mutex_lock(&root->fs_info->extent_ins_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04003165 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3166 ins->objectid + ins->offset - 1,
Josef Bacik25179202008-10-29 14:49:05 -04003167 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04003168 set_state_private(&root->fs_info->extent_ins,
3169 ins->objectid, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04003170 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04003171 goto update_block;
3172 }
3173
Chris Mason47e4bb92008-02-01 14:51:59 -05003174 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05003175 keys[1].objectid = ins->objectid;
3176 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04003177 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05003178 sizes[0] = sizeof(*extent_item);
3179 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05003180
3181 path = btrfs_alloc_path();
3182 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05003183
3184 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3185 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04003186 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003187
Chris Mason47e4bb92008-02-01 14:51:59 -05003188 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3189 struct btrfs_extent_item);
3190 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3191 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3192 struct btrfs_extent_ref);
3193
3194 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3195 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3196 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Zheng Yan31840ae2008-09-23 13:14:14 -04003197 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05003198
3199 btrfs_mark_buffer_dirty(path->nodes[0]);
3200
3201 trans->alloc_exclude_start = 0;
3202 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05003203 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04003204 finish_current_insert(trans, extent_root, 0);
3205 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -04003206
Chris Mason925baed2008-06-25 16:01:30 -04003207 if (ret)
3208 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003209 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04003210 ret = pending_ret;
3211 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003212 }
Chris Mason26b80032007-08-08 20:17:12 -04003213
3214update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04003215 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05003216 if (ret) {
3217 printk("update block group failed for %Lu %Lu\n",
3218 ins->objectid, ins->offset);
3219 BUG();
3220 }
Chris Mason925baed2008-06-25 16:01:30 -04003221out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04003222 return ret;
3223}
3224
3225int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003226 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003227 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003228 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003229{
3230 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04003231
3232 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3233 return 0;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003234 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3235 ref_generation, owner, ins);
Zheng Yane8569812008-09-26 10:05:48 -04003236 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003237 return ret;
3238}
Chris Masone02119d2008-09-05 16:13:11 -04003239
3240/*
3241 * this is used by the tree logging recovery code. It records that
3242 * an extent has been allocated and makes sure to clear the free
3243 * space cache bits as well
3244 */
3245int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003246 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04003247 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003248 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04003249{
3250 int ret;
3251 struct btrfs_block_group_cache *block_group;
3252
Chris Masone02119d2008-09-05 16:13:11 -04003253 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik25179202008-10-29 14:49:05 -04003254 mutex_lock(&block_group->alloc_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003255 cache_block_group(root, block_group);
3256
Josef Bacik25179202008-10-29 14:49:05 -04003257 ret = btrfs_remove_free_space_lock(block_group, ins->objectid,
3258 ins->offset);
3259 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003260 BUG_ON(ret);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003261 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3262 ref_generation, owner, ins);
Chris Masone02119d2008-09-05 16:13:11 -04003263 return ret;
3264}
3265
Chris Masone6dcd2d2008-07-17 12:53:50 -04003266/*
3267 * finds a free extent and does all the dirty work required for allocation
3268 * returns the key for the extent through ins, and a tree buffer for
3269 * the first block of the extent through buf.
3270 *
3271 * returns 0 if everything worked, non-zero otherwise.
3272 */
3273int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3274 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003275 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003276 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003277 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003278 u64 search_end, struct btrfs_key *ins, u64 data)
3279{
3280 int ret;
3281
Chris Masone6dcd2d2008-07-17 12:53:50 -04003282 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3283 min_alloc_size, empty_size, hint_byte,
3284 search_end, ins, data);
3285 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04003286 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003287 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3288 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003289 owner_objectid, ins);
Chris Masond00aff02008-09-11 15:54:42 -04003290 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003291
Zheng Yane8569812008-09-26 10:05:48 -04003292 } else {
3293 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04003294 }
Chris Mason925baed2008-06-25 16:01:30 -04003295 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003296}
Chris Mason65b51a02008-08-01 15:11:20 -04003297
3298struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3299 struct btrfs_root *root,
3300 u64 bytenr, u32 blocksize)
3301{
3302 struct extent_buffer *buf;
3303
3304 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3305 if (!buf)
3306 return ERR_PTR(-ENOMEM);
3307 btrfs_set_header_generation(buf, trans->transid);
3308 btrfs_tree_lock(buf);
3309 clean_tree_block(trans, root, buf);
3310 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04003311 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3312 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04003313 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04003314 } else {
3315 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3316 buf->start + buf->len - 1, GFP_NOFS);
3317 }
Chris Mason65b51a02008-08-01 15:11:20 -04003318 trans->blocks_used++;
3319 return buf;
3320}
3321
Chris Masonfec577f2007-02-26 10:40:21 -05003322/*
3323 * helper function to allocate a block for a given tree
3324 * returns the tree buffer or NULL.
3325 */
Chris Mason5f39d392007-10-15 16:14:19 -04003326struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04003327 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003328 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05003329 u64 root_objectid,
3330 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05003331 int level,
3332 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04003333 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05003334{
Chris Masone2fa7222007-03-12 16:22:34 -04003335 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05003336 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003337 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05003338
Zheng Yan31840ae2008-09-23 13:14:14 -04003339 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003340 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04003341 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05003342 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04003343 BUG_ON(ret > 0);
3344 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05003345 }
Chris Mason55c69072008-01-09 15:55:33 -05003346
Chris Mason65b51a02008-08-01 15:11:20 -04003347 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05003348 return buf;
3349}
Chris Masona28ec192007-03-06 20:08:01 -05003350
Chris Masone02119d2008-09-05 16:13:11 -04003351int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3352 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04003353{
Chris Mason7bb86312007-12-11 09:25:06 -05003354 u64 leaf_owner;
3355 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04003356 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04003357 struct btrfs_file_extent_item *fi;
3358 int i;
3359 int nritems;
3360 int ret;
3361
Chris Mason5f39d392007-10-15 16:14:19 -04003362 BUG_ON(!btrfs_is_leaf(leaf));
3363 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05003364 leaf_owner = btrfs_header_owner(leaf);
3365 leaf_generation = btrfs_header_generation(leaf);
3366
Chris Mason6407bf62007-03-27 06:33:00 -04003367 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003368 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04003369 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04003370
3371 btrfs_item_key_to_cpu(leaf, &key, i);
3372 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04003373 continue;
3374 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003375 if (btrfs_file_extent_type(leaf, fi) ==
3376 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04003377 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04003378 /*
3379 * FIXME make sure to insert a trans record that
3380 * repeats the snapshot del on crash
3381 */
Chris Masondb945352007-10-15 16:15:53 -04003382 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3383 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04003384 continue;
Chris Mason4a096752008-07-21 10:29:44 -04003385
Chris Mason925baed2008-06-25 16:01:30 -04003386 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003387 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04003388 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003389 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003390 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04003391
3392 atomic_inc(&root->fs_info->throttle_gen);
3393 wake_up(&root->fs_info->transaction_throttle);
3394 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04003395 }
3396 return 0;
3397}
3398
Chris Masone02119d2008-09-05 16:13:11 -04003399static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3400 struct btrfs_root *root,
3401 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04003402{
3403 int i;
3404 int ret;
3405 struct btrfs_extent_info *info = ref->extents;
3406
Yan Zheng31153d82008-07-28 15:32:19 -04003407 for (i = 0; i < ref->nritems; i++) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003408 ret = __btrfs_free_extent(trans, root, info->bytenr,
3409 info->num_bytes, ref->bytenr,
3410 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003411 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04003412
3413 atomic_inc(&root->fs_info->throttle_gen);
3414 wake_up(&root->fs_info->transaction_throttle);
3415 cond_resched();
3416
Yan Zheng31153d82008-07-28 15:32:19 -04003417 BUG_ON(ret);
3418 info++;
3419 }
Yan Zheng31153d82008-07-28 15:32:19 -04003420
3421 return 0;
3422}
3423
Chris Mason333db942008-06-25 16:01:30 -04003424int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
3425 u32 *refs)
3426{
Chris Mason017e5362008-07-28 15:32:51 -04003427 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04003428
Zheng Yan31840ae2008-09-23 13:14:14 -04003429 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04003430 BUG_ON(ret);
3431
3432#if 0 // some debugging code in case we see problems here
3433 /* if the refs count is one, it won't get increased again. But
3434 * if the ref count is > 1, someone may be decreasing it at
3435 * the same time we are.
3436 */
3437 if (*refs != 1) {
3438 struct extent_buffer *eb = NULL;
3439 eb = btrfs_find_create_tree_block(root, start, len);
3440 if (eb)
3441 btrfs_tree_lock(eb);
3442
3443 mutex_lock(&root->fs_info->alloc_mutex);
3444 ret = lookup_extent_ref(NULL, root, start, len, refs);
3445 BUG_ON(ret);
3446 mutex_unlock(&root->fs_info->alloc_mutex);
3447
3448 if (eb) {
3449 btrfs_tree_unlock(eb);
3450 free_extent_buffer(eb);
3451 }
3452 if (*refs == 1) {
3453 printk("block %llu went down to one during drop_snap\n",
3454 (unsigned long long)start);
3455 }
3456
3457 }
3458#endif
3459
Chris Masone7a84562008-06-25 16:01:31 -04003460 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04003461 return ret;
Chris Mason333db942008-06-25 16:01:30 -04003462}
3463
3464/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003465 * helper function for drop_snapshot, this walks down the tree dropping ref
3466 * counts as it goes.
3467 */
Chris Mason98ed5172008-01-03 10:01:48 -05003468static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
3469 struct btrfs_root *root,
3470 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003471{
Chris Mason7bb86312007-12-11 09:25:06 -05003472 u64 root_owner;
3473 u64 root_gen;
3474 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04003475 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003476 struct extent_buffer *next;
3477 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05003478 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04003479 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04003480 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05003481 int ret;
3482 u32 refs;
3483
Chris Mason5caf2a02007-04-02 11:20:42 -04003484 WARN_ON(*level < 0);
3485 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04003486 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04003487 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05003488 BUG_ON(ret);
3489 if (refs > 1)
3490 goto out;
Chris Masone0115992007-06-19 16:23:05 -04003491
Chris Mason9aca1d52007-03-13 11:09:37 -04003492 /*
3493 * walk down to the last node level and free all the leaves
3494 */
Chris Mason6407bf62007-03-27 06:33:00 -04003495 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003496 WARN_ON(*level < 0);
3497 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05003498 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04003499
Chris Mason5f39d392007-10-15 16:14:19 -04003500 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04003501 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04003502
Chris Mason7518a232007-03-12 12:01:18 -04003503 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04003504 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05003505 break;
Chris Mason6407bf62007-03-27 06:33:00 -04003506 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003507 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04003508 BUG_ON(ret);
3509 break;
3510 }
Chris Masondb945352007-10-15 16:15:53 -04003511 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04003512 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04003513 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04003514
Chris Mason333db942008-06-25 16:01:30 -04003515 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04003516 BUG_ON(ret);
3517 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05003518 parent = path->nodes[*level];
3519 root_owner = btrfs_header_owner(parent);
3520 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05003521 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04003522
Chris Mason925baed2008-06-25 16:01:30 -04003523 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003524 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003525 root_owner, root_gen,
3526 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05003527 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04003528
3529 atomic_inc(&root->fs_info->throttle_gen);
3530 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04003531 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04003532
Chris Mason20524f02007-03-10 06:35:47 -05003533 continue;
3534 }
Chris Masonf87f0572008-08-01 11:27:23 -04003535 /*
3536 * at this point, we have a single ref, and since the
3537 * only place referencing this extent is a dead root
3538 * the reference count should never go higher.
3539 * So, we don't need to check it again
3540 */
Yan Zheng31153d82008-07-28 15:32:19 -04003541 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04003542 ref = btrfs_lookup_leaf_ref(root, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04003543 if (ref && ref->generation != ptr_gen) {
3544 btrfs_free_leaf_ref(root, ref);
3545 ref = NULL;
3546 }
Yan Zheng31153d82008-07-28 15:32:19 -04003547 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04003548 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003549 BUG_ON(ret);
3550 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04003551 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003552 *level = 0;
3553 break;
3554 }
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003555 if (printk_ratelimit()) {
Chris Mason37d1aee2008-07-31 10:48:37 -04003556 printk("leaf ref miss for bytenr %llu\n",
3557 (unsigned long long)bytenr);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003558 }
Yan Zheng31153d82008-07-28 15:32:19 -04003559 }
Chris Masondb945352007-10-15 16:15:53 -04003560 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04003561 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003562 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04003563
Chris Masonca7a79a2008-05-12 12:59:19 -04003564 next = read_tree_block(root, bytenr, blocksize,
3565 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04003566 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04003567#if 0
3568 /*
3569 * this is a debugging check and can go away
3570 * the ref should never go all the way down to 1
3571 * at this point
3572 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003573 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
3574 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04003575 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003576 WARN_ON(refs != 1);
3577#endif
Chris Masone9d0b132007-08-10 14:06:19 -04003578 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003579 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04003580 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04003581 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05003582 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04003583 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05003584 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04003585 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003586 }
3587out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003588 WARN_ON(*level < 0);
3589 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05003590
3591 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003592 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003593 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003594 } else {
3595 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003596 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003597 }
3598
Yan Zheng31153d82008-07-28 15:32:19 -04003599 blocksize = btrfs_level_size(root, *level);
3600 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003601 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003602
3603 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003604 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003605 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003606 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003607 path->nodes[*level] = NULL;
3608 *level += 1;
3609 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003610
Chris Masone7a84562008-06-25 16:01:31 -04003611 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003612 return 0;
3613}
3614
Chris Mason9aca1d52007-03-13 11:09:37 -04003615/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04003616 * helper function for drop_subtree, this function is similar to
3617 * walk_down_tree. The main difference is that it checks reference
3618 * counts while tree blocks are locked.
3619 */
3620static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
3621 struct btrfs_root *root,
3622 struct btrfs_path *path, int *level)
3623{
3624 struct extent_buffer *next;
3625 struct extent_buffer *cur;
3626 struct extent_buffer *parent;
3627 u64 bytenr;
3628 u64 ptr_gen;
3629 u32 blocksize;
3630 u32 refs;
3631 int ret;
3632
3633 cur = path->nodes[*level];
3634 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3635 &refs);
3636 BUG_ON(ret);
3637 if (refs > 1)
3638 goto out;
3639
3640 while (*level >= 0) {
3641 cur = path->nodes[*level];
3642 if (*level == 0) {
3643 ret = btrfs_drop_leaf_ref(trans, root, cur);
3644 BUG_ON(ret);
3645 clean_tree_block(trans, root, cur);
3646 break;
3647 }
3648 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3649 clean_tree_block(trans, root, cur);
3650 break;
3651 }
3652
3653 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3654 blocksize = btrfs_level_size(root, *level - 1);
3655 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3656
3657 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3658 btrfs_tree_lock(next);
3659
3660 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3661 &refs);
3662 BUG_ON(ret);
3663 if (refs > 1) {
3664 parent = path->nodes[*level];
3665 ret = btrfs_free_extent(trans, root, bytenr,
3666 blocksize, parent->start,
3667 btrfs_header_owner(parent),
3668 btrfs_header_generation(parent),
3669 *level - 1, 1);
3670 BUG_ON(ret);
3671 path->slots[*level]++;
3672 btrfs_tree_unlock(next);
3673 free_extent_buffer(next);
3674 continue;
3675 }
3676
3677 *level = btrfs_header_level(next);
3678 path->nodes[*level] = next;
3679 path->slots[*level] = 0;
3680 path->locks[*level] = 1;
3681 cond_resched();
3682 }
3683out:
3684 parent = path->nodes[*level + 1];
3685 bytenr = path->nodes[*level]->start;
3686 blocksize = path->nodes[*level]->len;
3687
3688 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3689 parent->start, btrfs_header_owner(parent),
3690 btrfs_header_generation(parent), *level, 1);
3691 BUG_ON(ret);
3692
3693 if (path->locks[*level]) {
3694 btrfs_tree_unlock(path->nodes[*level]);
3695 path->locks[*level] = 0;
3696 }
3697 free_extent_buffer(path->nodes[*level]);
3698 path->nodes[*level] = NULL;
3699 *level += 1;
3700 cond_resched();
3701 return 0;
3702}
3703
3704/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003705 * helper for dropping snapshots. This walks back up the tree in the path
3706 * to find the first node higher up where we haven't yet gone through
3707 * all the slots
3708 */
Chris Mason98ed5172008-01-03 10:01:48 -05003709static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3710 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003711 struct btrfs_path *path,
3712 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003713{
Chris Mason7bb86312007-12-11 09:25:06 -05003714 u64 root_owner;
3715 u64 root_gen;
3716 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003717 int i;
3718 int slot;
3719 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003720
Yan Zhengf82d02d2008-10-29 14:49:05 -04003721 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003722 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003723 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3724 struct extent_buffer *node;
3725 struct btrfs_disk_key disk_key;
3726 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003727 path->slots[i]++;
3728 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003729 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003730 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003731 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003732 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003733 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003734 return 0;
3735 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003736 struct extent_buffer *parent;
3737 if (path->nodes[*level] == root->node)
3738 parent = path->nodes[*level];
3739 else
3740 parent = path->nodes[*level + 1];
3741
3742 root_owner = btrfs_header_owner(parent);
3743 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003744
3745 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003746 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003747 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003748 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003749 parent->start, root_owner,
3750 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003751 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003752 if (path->locks[*level]) {
3753 btrfs_tree_unlock(path->nodes[*level]);
3754 path->locks[*level] = 0;
3755 }
Chris Mason5f39d392007-10-15 16:14:19 -04003756 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003757 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003758 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003759 }
3760 }
3761 return 1;
3762}
3763
Chris Mason9aca1d52007-03-13 11:09:37 -04003764/*
3765 * drop the reference count on the tree rooted at 'snap'. This traverses
3766 * the tree freeing any blocks that have a ref count of zero after being
3767 * decremented.
3768 */
Chris Masone089f052007-03-16 16:20:31 -04003769int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003770 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003771{
Chris Mason3768f362007-03-13 16:47:54 -04003772 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003773 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003774 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003775 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003776 int i;
3777 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003778 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003779
Chris Masona2135012008-06-25 16:01:30 -04003780 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003781 path = btrfs_alloc_path();
3782 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003783
Chris Mason5f39d392007-10-15 16:14:19 -04003784 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003785 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003786 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3787 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003788 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003789 path->slots[level] = 0;
3790 } else {
3791 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003792 struct btrfs_disk_key found_key;
3793 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003794
Chris Mason9f3a7422007-08-07 15:52:19 -04003795 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003796 level = root_item->drop_level;
3797 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003798 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003799 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003800 ret = wret;
3801 goto out;
3802 }
Chris Mason5f39d392007-10-15 16:14:19 -04003803 node = path->nodes[level];
3804 btrfs_node_key(node, &found_key, path->slots[level]);
3805 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3806 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003807 /*
3808 * unlock our path, this is safe because only this
3809 * function is allowed to delete this snapshot
3810 */
Chris Mason925baed2008-06-25 16:01:30 -04003811 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3812 if (path->nodes[i] && path->locks[i]) {
3813 path->locks[i] = 0;
3814 btrfs_tree_unlock(path->nodes[i]);
3815 }
3816 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003817 }
Chris Mason20524f02007-03-10 06:35:47 -05003818 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003819 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003820 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003821 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003822 if (wret < 0)
3823 ret = wret;
3824
Yan Zhengf82d02d2008-10-29 14:49:05 -04003825 wret = walk_up_tree(trans, root, path, &level,
3826 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003827 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003828 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003829 if (wret < 0)
3830 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003831 if (trans->transaction->in_commit) {
3832 ret = -EAGAIN;
3833 break;
3834 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003835 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003836 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003837 }
Chris Mason83e15a22007-03-12 09:03:27 -04003838 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003839 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003840 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003841 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003842 }
Chris Mason20524f02007-03-10 06:35:47 -05003843 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003844out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003845 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003846 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003847}
Chris Mason9078a3e2007-04-26 16:46:15 -04003848
Yan Zhengf82d02d2008-10-29 14:49:05 -04003849int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3850 struct btrfs_root *root,
3851 struct extent_buffer *node,
3852 struct extent_buffer *parent)
3853{
3854 struct btrfs_path *path;
3855 int level;
3856 int parent_level;
3857 int ret = 0;
3858 int wret;
3859
3860 path = btrfs_alloc_path();
3861 BUG_ON(!path);
3862
3863 BUG_ON(!btrfs_tree_locked(parent));
3864 parent_level = btrfs_header_level(parent);
3865 extent_buffer_get(parent);
3866 path->nodes[parent_level] = parent;
3867 path->slots[parent_level] = btrfs_header_nritems(parent);
3868
3869 BUG_ON(!btrfs_tree_locked(node));
3870 level = btrfs_header_level(node);
3871 extent_buffer_get(node);
3872 path->nodes[level] = node;
3873 path->slots[level] = 0;
3874
3875 while (1) {
3876 wret = walk_down_subtree(trans, root, path, &level);
3877 if (wret < 0)
3878 ret = wret;
3879 if (wret != 0)
3880 break;
3881
3882 wret = walk_up_tree(trans, root, path, &level, parent_level);
3883 if (wret < 0)
3884 ret = wret;
3885 if (wret != 0)
3886 break;
3887 }
3888
3889 btrfs_free_path(path);
3890 return ret;
3891}
3892
Chris Mason8e7bf942008-04-28 09:02:36 -04003893static unsigned long calc_ra(unsigned long start, unsigned long last,
3894 unsigned long nr)
3895{
3896 return min(last, start + nr - 1);
3897}
3898
Chris Mason98ed5172008-01-03 10:01:48 -05003899static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3900 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003901{
3902 u64 page_start;
3903 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003904 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003905 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003906 unsigned long i;
3907 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003908 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003909 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003910 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003911 unsigned int total_read = 0;
3912 unsigned int total_dirty = 0;
3913 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003914
3915 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003916
3917 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003918 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003919 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3920
Zheng Yan1a40e232008-09-26 10:09:34 -04003921 /* make sure the dirty trick played by the caller work */
3922 ret = invalidate_inode_pages2_range(inode->i_mapping,
3923 first_index, last_index);
3924 if (ret)
3925 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003926
Chris Mason4313b392008-01-03 09:08:48 -05003927 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003928
Zheng Yan1a40e232008-09-26 10:09:34 -04003929 for (i = first_index ; i <= last_index; i++) {
3930 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003931 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003932 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003933 }
3934 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003935again:
3936 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003937 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003938 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003939 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003940 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003941 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003942 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003943 if (!PageUptodate(page)) {
3944 btrfs_readpage(NULL, page);
3945 lock_page(page);
3946 if (!PageUptodate(page)) {
3947 unlock_page(page);
3948 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003949 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003950 goto out_unlock;
3951 }
3952 }
Chris Masonec44a352008-04-28 15:29:52 -04003953 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003954
Chris Masonedbd8d42007-12-21 16:27:24 -05003955 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3956 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003957 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003958
Chris Mason3eaa2882008-07-24 11:57:52 -04003959 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3960 if (ordered) {
3961 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3962 unlock_page(page);
3963 page_cache_release(page);
3964 btrfs_start_ordered_extent(inode, ordered, 1);
3965 btrfs_put_ordered_extent(ordered);
3966 goto again;
3967 }
3968 set_page_extent_mapped(page);
3969
Chris Masonea8c2812008-08-04 23:17:27 -04003970 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003971 if (i == first_index)
3972 set_extent_bits(io_tree, page_start, page_end,
3973 EXTENT_BOUNDARY, GFP_NOFS);
3974
Chris Masona061fc82008-05-07 11:43:44 -04003975 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003976 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003977
Chris Masond1310b22008-01-24 16:13:08 -05003978 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003979 unlock_page(page);
3980 page_cache_release(page);
3981 }
3982
3983out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04003984 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05003985 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003986 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04003987 return ret;
3988}
3989
Zheng Yan1a40e232008-09-26 10:09:34 -04003990static int noinline relocate_data_extent(struct inode *reloc_inode,
3991 struct btrfs_key *extent_key,
3992 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05003993{
Zheng Yan1a40e232008-09-26 10:09:34 -04003994 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3995 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
3996 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04003997 u64 start = extent_key->objectid - offset;
3998 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04003999
4000 em = alloc_extent_map(GFP_NOFS);
4001 BUG_ON(!em || IS_ERR(em));
4002
Yan Zheng66435582008-10-30 14:19:50 -04004003 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004004 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004005 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004006 em->block_start = extent_key->objectid;
4007 em->bdev = root->fs_info->fs_devices->latest_bdev;
4008 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4009
4010 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04004011 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004012 while (1) {
4013 int ret;
4014 spin_lock(&em_tree->lock);
4015 ret = add_extent_mapping(em_tree, em);
4016 spin_unlock(&em_tree->lock);
4017 if (ret != -EEXIST) {
4018 free_extent_map(em);
4019 break;
4020 }
Yan Zheng66435582008-10-30 14:19:50 -04004021 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004022 }
Yan Zheng66435582008-10-30 14:19:50 -04004023 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004024
Yan Zheng66435582008-10-30 14:19:50 -04004025 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04004026}
4027
4028struct btrfs_ref_path {
4029 u64 extent_start;
4030 u64 nodes[BTRFS_MAX_LEVEL];
4031 u64 root_objectid;
4032 u64 root_generation;
4033 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004034 u32 num_refs;
4035 int lowest_level;
4036 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004037 int shared_level;
4038
4039 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4040 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04004041};
4042
4043struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04004044 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04004045 u64 disk_bytenr;
4046 u64 disk_num_bytes;
4047 u64 offset;
4048 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04004049 u8 compression;
4050 u8 encryption;
4051 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04004052};
4053
4054static int is_cowonly_root(u64 root_objectid)
4055{
4056 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4057 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4058 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4059 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
4060 root_objectid == BTRFS_TREE_LOG_OBJECTID)
4061 return 1;
4062 return 0;
4063}
4064
4065static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
4066 struct btrfs_root *extent_root,
4067 struct btrfs_ref_path *ref_path,
4068 int first_time)
4069{
4070 struct extent_buffer *leaf;
4071 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05004072 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05004073 struct btrfs_key key;
4074 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04004075 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05004076 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04004077 int level;
4078 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05004079
Zheng Yan1a40e232008-09-26 10:09:34 -04004080 path = btrfs_alloc_path();
4081 if (!path)
4082 return -ENOMEM;
4083
Zheng Yan1a40e232008-09-26 10:09:34 -04004084 if (first_time) {
4085 ref_path->lowest_level = -1;
4086 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004087 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004088 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04004089 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004090walk_down:
4091 level = ref_path->current_level - 1;
4092 while (level >= -1) {
4093 u64 parent;
4094 if (level < ref_path->lowest_level)
4095 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004096
Zheng Yan1a40e232008-09-26 10:09:34 -04004097 if (level >= 0) {
4098 bytenr = ref_path->nodes[level];
4099 } else {
4100 bytenr = ref_path->extent_start;
4101 }
4102 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004103
Zheng Yan1a40e232008-09-26 10:09:34 -04004104 parent = ref_path->nodes[level + 1];
4105 ref_path->nodes[level + 1] = 0;
4106 ref_path->current_level = level;
4107 BUG_ON(parent == 0);
4108
4109 key.objectid = bytenr;
4110 key.offset = parent + 1;
4111 key.type = BTRFS_EXTENT_REF_KEY;
4112
4113 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004114 if (ret < 0)
4115 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004116 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004117
Chris Masonedbd8d42007-12-21 16:27:24 -05004118 leaf = path->nodes[0];
4119 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04004120 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04004121 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04004122 if (ret < 0)
4123 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004124 if (ret > 0)
4125 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04004126 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04004127 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004128
4129 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04004130 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04004131 found_key.type == BTRFS_EXTENT_REF_KEY) {
4132 if (level < ref_path->shared_level)
4133 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004134 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004135 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004136next:
4137 level--;
4138 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004139 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004140 }
4141 /* reached lowest level */
4142 ret = 1;
4143 goto out;
4144walk_up:
4145 level = ref_path->current_level;
4146 while (level < BTRFS_MAX_LEVEL - 1) {
4147 u64 ref_objectid;
4148 if (level >= 0) {
4149 bytenr = ref_path->nodes[level];
4150 } else {
4151 bytenr = ref_path->extent_start;
Chris Masona061fc82008-05-07 11:43:44 -04004152 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004153 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004154
Zheng Yan1a40e232008-09-26 10:09:34 -04004155 key.objectid = bytenr;
4156 key.offset = 0;
4157 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05004158
Zheng Yan1a40e232008-09-26 10:09:34 -04004159 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4160 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05004161 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004162
4163 leaf = path->nodes[0];
4164 nritems = btrfs_header_nritems(leaf);
4165 if (path->slots[0] >= nritems) {
4166 ret = btrfs_next_leaf(extent_root, path);
4167 if (ret < 0)
4168 goto out;
4169 if (ret > 0) {
4170 /* the extent was freed by someone */
4171 if (ref_path->lowest_level == level)
4172 goto out;
4173 btrfs_release_path(extent_root, path);
4174 goto walk_down;
4175 }
4176 leaf = path->nodes[0];
4177 }
4178
4179 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4180 if (found_key.objectid != bytenr ||
4181 found_key.type != BTRFS_EXTENT_REF_KEY) {
4182 /* the extent was freed by someone */
4183 if (ref_path->lowest_level == level) {
4184 ret = 1;
4185 goto out;
4186 }
4187 btrfs_release_path(extent_root, path);
4188 goto walk_down;
4189 }
4190found:
4191 ref = btrfs_item_ptr(leaf, path->slots[0],
4192 struct btrfs_extent_ref);
4193 ref_objectid = btrfs_ref_objectid(leaf, ref);
4194 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4195 if (first_time) {
4196 level = (int)ref_objectid;
4197 BUG_ON(level >= BTRFS_MAX_LEVEL);
4198 ref_path->lowest_level = level;
4199 ref_path->current_level = level;
4200 ref_path->nodes[level] = bytenr;
4201 } else {
4202 WARN_ON(ref_objectid != level);
4203 }
4204 } else {
4205 WARN_ON(level != -1);
4206 }
4207 first_time = 0;
4208
4209 if (ref_path->lowest_level == level) {
4210 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004211 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4212 }
4213
4214 /*
4215 * the block is tree root or the block isn't in reference
4216 * counted tree.
4217 */
4218 if (found_key.objectid == found_key.offset ||
4219 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4220 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4221 ref_path->root_generation =
4222 btrfs_ref_generation(leaf, ref);
4223 if (level < 0) {
4224 /* special reference from the tree log */
4225 ref_path->nodes[0] = found_key.offset;
4226 ref_path->current_level = 0;
4227 }
4228 ret = 0;
4229 goto out;
4230 }
4231
4232 level++;
4233 BUG_ON(ref_path->nodes[level] != 0);
4234 ref_path->nodes[level] = found_key.offset;
4235 ref_path->current_level = level;
4236
4237 /*
4238 * the reference was created in the running transaction,
4239 * no need to continue walking up.
4240 */
4241 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4242 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4243 ref_path->root_generation =
4244 btrfs_ref_generation(leaf, ref);
4245 ret = 0;
4246 goto out;
4247 }
4248
4249 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004250 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004251 }
4252 /* reached max tree level, but no tree root found. */
4253 BUG();
4254out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004255 btrfs_free_path(path);
4256 return ret;
4257}
4258
4259static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4260 struct btrfs_root *extent_root,
4261 struct btrfs_ref_path *ref_path,
4262 u64 extent_start)
4263{
4264 memset(ref_path, 0, sizeof(*ref_path));
4265 ref_path->extent_start = extent_start;
4266
4267 return __next_ref_path(trans, extent_root, ref_path, 1);
4268}
4269
4270static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4271 struct btrfs_root *extent_root,
4272 struct btrfs_ref_path *ref_path)
4273{
4274 return __next_ref_path(trans, extent_root, ref_path, 0);
4275}
4276
4277static int noinline get_new_locations(struct inode *reloc_inode,
4278 struct btrfs_key *extent_key,
4279 u64 offset, int no_fragment,
4280 struct disk_extent **extents,
4281 int *nr_extents)
4282{
4283 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4284 struct btrfs_path *path;
4285 struct btrfs_file_extent_item *fi;
4286 struct extent_buffer *leaf;
4287 struct disk_extent *exts = *extents;
4288 struct btrfs_key found_key;
4289 u64 cur_pos;
4290 u64 last_byte;
4291 u32 nritems;
4292 int nr = 0;
4293 int max = *nr_extents;
4294 int ret;
4295
4296 WARN_ON(!no_fragment && *extents);
4297 if (!exts) {
4298 max = 1;
4299 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4300 if (!exts)
4301 return -ENOMEM;
4302 }
4303
4304 path = btrfs_alloc_path();
4305 BUG_ON(!path);
4306
4307 cur_pos = extent_key->objectid - offset;
4308 last_byte = extent_key->objectid + extent_key->offset;
4309 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4310 cur_pos, 0);
4311 if (ret < 0)
4312 goto out;
4313 if (ret > 0) {
4314 ret = -ENOENT;
4315 goto out;
4316 }
4317
4318 while (1) {
4319 leaf = path->nodes[0];
4320 nritems = btrfs_header_nritems(leaf);
4321 if (path->slots[0] >= nritems) {
4322 ret = btrfs_next_leaf(root, path);
4323 if (ret < 0)
4324 goto out;
4325 if (ret > 0)
4326 break;
4327 leaf = path->nodes[0];
4328 }
4329
4330 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4331 if (found_key.offset != cur_pos ||
4332 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4333 found_key.objectid != reloc_inode->i_ino)
4334 break;
4335
4336 fi = btrfs_item_ptr(leaf, path->slots[0],
4337 struct btrfs_file_extent_item);
4338 if (btrfs_file_extent_type(leaf, fi) !=
4339 BTRFS_FILE_EXTENT_REG ||
4340 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4341 break;
4342
4343 if (nr == max) {
4344 struct disk_extent *old = exts;
4345 max *= 2;
4346 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4347 memcpy(exts, old, sizeof(*exts) * nr);
4348 if (old != *extents)
4349 kfree(old);
4350 }
4351
4352 exts[nr].disk_bytenr =
4353 btrfs_file_extent_disk_bytenr(leaf, fi);
4354 exts[nr].disk_num_bytes =
4355 btrfs_file_extent_disk_num_bytes(leaf, fi);
4356 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4357 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04004358 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4359 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4360 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4361 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4362 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04004363 BUG_ON(exts[nr].offset > 0);
4364 BUG_ON(exts[nr].compression || exts[nr].encryption);
4365 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004366
4367 cur_pos += exts[nr].num_bytes;
4368 nr++;
4369
4370 if (cur_pos + offset >= last_byte)
4371 break;
4372
4373 if (no_fragment) {
4374 ret = 1;
4375 goto out;
4376 }
4377 path->slots[0]++;
4378 }
4379
4380 WARN_ON(cur_pos + offset > last_byte);
4381 if (cur_pos + offset < last_byte) {
4382 ret = -ENOENT;
4383 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05004384 }
4385 ret = 0;
4386out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004387 btrfs_free_path(path);
4388 if (ret) {
4389 if (exts != *extents)
4390 kfree(exts);
4391 } else {
4392 *extents = exts;
4393 *nr_extents = nr;
4394 }
4395 return ret;
4396}
4397
4398static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
4399 struct btrfs_root *root,
4400 struct btrfs_path *path,
4401 struct btrfs_key *extent_key,
4402 struct btrfs_key *leaf_key,
4403 struct btrfs_ref_path *ref_path,
4404 struct disk_extent *new_extents,
4405 int nr_extents)
4406{
4407 struct extent_buffer *leaf;
4408 struct btrfs_file_extent_item *fi;
4409 struct inode *inode = NULL;
4410 struct btrfs_key key;
4411 u64 lock_start = 0;
4412 u64 lock_end = 0;
4413 u64 num_bytes;
4414 u64 ext_offset;
4415 u64 first_pos;
4416 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004417 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004418 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04004419 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04004420 int ret;
4421
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004422 memcpy(&key, leaf_key, sizeof(key));
4423 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004424 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004425 if (key.objectid < ref_path->owner_objectid ||
4426 (key.objectid == ref_path->owner_objectid &&
4427 key.type < BTRFS_EXTENT_DATA_KEY)) {
4428 key.objectid = ref_path->owner_objectid;
4429 key.type = BTRFS_EXTENT_DATA_KEY;
4430 key.offset = 0;
4431 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004432 }
4433
4434 while (1) {
4435 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4436 if (ret < 0)
4437 goto out;
4438
4439 leaf = path->nodes[0];
4440 nritems = btrfs_header_nritems(leaf);
4441next:
4442 if (extent_locked && ret > 0) {
4443 /*
4444 * the file extent item was modified by someone
4445 * before the extent got locked.
4446 */
Zheng Yan1a40e232008-09-26 10:09:34 -04004447 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4448 lock_end, GFP_NOFS);
4449 extent_locked = 0;
4450 }
4451
4452 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004453 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04004454 break;
4455
4456 BUG_ON(extent_locked);
4457 ret = btrfs_next_leaf(root, path);
4458 if (ret < 0)
4459 goto out;
4460 if (ret > 0)
4461 break;
4462 leaf = path->nodes[0];
4463 nritems = btrfs_header_nritems(leaf);
4464 }
4465
4466 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4467
4468 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4469 if ((key.objectid > ref_path->owner_objectid) ||
4470 (key.objectid == ref_path->owner_objectid &&
4471 key.type > BTRFS_EXTENT_DATA_KEY) ||
4472 (key.offset >= first_pos + extent_key->offset))
4473 break;
4474 }
4475
4476 if (inode && key.objectid != inode->i_ino) {
4477 BUG_ON(extent_locked);
4478 btrfs_release_path(root, path);
4479 mutex_unlock(&inode->i_mutex);
4480 iput(inode);
4481 inode = NULL;
4482 continue;
4483 }
4484
4485 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4486 path->slots[0]++;
4487 ret = 1;
4488 goto next;
4489 }
4490 fi = btrfs_item_ptr(leaf, path->slots[0],
4491 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04004492 extent_type = btrfs_file_extent_type(leaf, fi);
4493 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4494 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04004495 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4496 extent_key->objectid)) {
4497 path->slots[0]++;
4498 ret = 1;
4499 goto next;
4500 }
4501
4502 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4503 ext_offset = btrfs_file_extent_offset(leaf, fi);
4504
4505 if (first_pos > key.offset - ext_offset)
4506 first_pos = key.offset - ext_offset;
4507
4508 if (!extent_locked) {
4509 lock_start = key.offset;
4510 lock_end = lock_start + num_bytes - 1;
4511 } else {
Yan Zheng66435582008-10-30 14:19:50 -04004512 if (lock_start > key.offset ||
4513 lock_end + 1 < key.offset + num_bytes) {
4514 unlock_extent(&BTRFS_I(inode)->io_tree,
4515 lock_start, lock_end, GFP_NOFS);
4516 extent_locked = 0;
4517 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004518 }
4519
4520 if (!inode) {
4521 btrfs_release_path(root, path);
4522
4523 inode = btrfs_iget_locked(root->fs_info->sb,
4524 key.objectid, root);
4525 if (inode->i_state & I_NEW) {
4526 BTRFS_I(inode)->root = root;
4527 BTRFS_I(inode)->location.objectid =
4528 key.objectid;
4529 BTRFS_I(inode)->location.type =
4530 BTRFS_INODE_ITEM_KEY;
4531 BTRFS_I(inode)->location.offset = 0;
4532 btrfs_read_locked_inode(inode);
4533 unlock_new_inode(inode);
4534 }
4535 /*
4536 * some code call btrfs_commit_transaction while
4537 * holding the i_mutex, so we can't use mutex_lock
4538 * here.
4539 */
4540 if (is_bad_inode(inode) ||
4541 !mutex_trylock(&inode->i_mutex)) {
4542 iput(inode);
4543 inode = NULL;
4544 key.offset = (u64)-1;
4545 goto skip;
4546 }
4547 }
4548
4549 if (!extent_locked) {
4550 struct btrfs_ordered_extent *ordered;
4551
4552 btrfs_release_path(root, path);
4553
4554 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4555 lock_end, GFP_NOFS);
4556 ordered = btrfs_lookup_first_ordered_extent(inode,
4557 lock_end);
4558 if (ordered &&
4559 ordered->file_offset <= lock_end &&
4560 ordered->file_offset + ordered->len > lock_start) {
4561 unlock_extent(&BTRFS_I(inode)->io_tree,
4562 lock_start, lock_end, GFP_NOFS);
4563 btrfs_start_ordered_extent(inode, ordered, 1);
4564 btrfs_put_ordered_extent(ordered);
4565 key.offset += num_bytes;
4566 goto skip;
4567 }
4568 if (ordered)
4569 btrfs_put_ordered_extent(ordered);
4570
Zheng Yan1a40e232008-09-26 10:09:34 -04004571 extent_locked = 1;
4572 continue;
4573 }
4574
4575 if (nr_extents == 1) {
4576 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04004577 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4578 new_extents[0].disk_bytenr);
4579 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4580 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004581 btrfs_mark_buffer_dirty(leaf);
4582
4583 btrfs_drop_extent_cache(inode, key.offset,
4584 key.offset + num_bytes - 1, 0);
4585
4586 ret = btrfs_inc_extent_ref(trans, root,
4587 new_extents[0].disk_bytenr,
4588 new_extents[0].disk_num_bytes,
4589 leaf->start,
4590 root->root_key.objectid,
4591 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004592 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004593 BUG_ON(ret);
4594
4595 ret = btrfs_free_extent(trans, root,
4596 extent_key->objectid,
4597 extent_key->offset,
4598 leaf->start,
4599 btrfs_header_owner(leaf),
4600 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004601 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004602 BUG_ON(ret);
4603
4604 btrfs_release_path(root, path);
4605 key.offset += num_bytes;
4606 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04004607 BUG_ON(1);
4608#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04004609 u64 alloc_hint;
4610 u64 extent_len;
4611 int i;
4612 /*
4613 * drop old extent pointer at first, then insert the
4614 * new pointers one bye one
4615 */
4616 btrfs_release_path(root, path);
4617 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4618 key.offset + num_bytes,
4619 key.offset, &alloc_hint);
4620 BUG_ON(ret);
4621
4622 for (i = 0; i < nr_extents; i++) {
4623 if (ext_offset >= new_extents[i].num_bytes) {
4624 ext_offset -= new_extents[i].num_bytes;
4625 continue;
4626 }
4627 extent_len = min(new_extents[i].num_bytes -
4628 ext_offset, num_bytes);
4629
4630 ret = btrfs_insert_empty_item(trans, root,
4631 path, &key,
4632 sizeof(*fi));
4633 BUG_ON(ret);
4634
4635 leaf = path->nodes[0];
4636 fi = btrfs_item_ptr(leaf, path->slots[0],
4637 struct btrfs_file_extent_item);
4638 btrfs_set_file_extent_generation(leaf, fi,
4639 trans->transid);
4640 btrfs_set_file_extent_type(leaf, fi,
4641 BTRFS_FILE_EXTENT_REG);
4642 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4643 new_extents[i].disk_bytenr);
4644 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4645 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004646 btrfs_set_file_extent_ram_bytes(leaf, fi,
4647 new_extents[i].ram_bytes);
4648
4649 btrfs_set_file_extent_compression(leaf, fi,
4650 new_extents[i].compression);
4651 btrfs_set_file_extent_encryption(leaf, fi,
4652 new_extents[i].encryption);
4653 btrfs_set_file_extent_other_encoding(leaf, fi,
4654 new_extents[i].other_encoding);
4655
Zheng Yan1a40e232008-09-26 10:09:34 -04004656 btrfs_set_file_extent_num_bytes(leaf, fi,
4657 extent_len);
4658 ext_offset += new_extents[i].offset;
4659 btrfs_set_file_extent_offset(leaf, fi,
4660 ext_offset);
4661 btrfs_mark_buffer_dirty(leaf);
4662
4663 btrfs_drop_extent_cache(inode, key.offset,
4664 key.offset + extent_len - 1, 0);
4665
4666 ret = btrfs_inc_extent_ref(trans, root,
4667 new_extents[i].disk_bytenr,
4668 new_extents[i].disk_num_bytes,
4669 leaf->start,
4670 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004671 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004672 BUG_ON(ret);
4673 btrfs_release_path(root, path);
4674
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004675 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004676
4677 ext_offset = 0;
4678 num_bytes -= extent_len;
4679 key.offset += extent_len;
4680
4681 if (num_bytes == 0)
4682 break;
4683 }
4684 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004685#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04004686 }
4687
4688 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004689 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4690 lock_end, GFP_NOFS);
4691 extent_locked = 0;
4692 }
4693skip:
4694 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4695 key.offset >= first_pos + extent_key->offset)
4696 break;
4697
4698 cond_resched();
4699 }
4700 ret = 0;
4701out:
4702 btrfs_release_path(root, path);
4703 if (inode) {
4704 mutex_unlock(&inode->i_mutex);
4705 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004706 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4707 lock_end, GFP_NOFS);
4708 }
4709 iput(inode);
4710 }
4711 return ret;
4712}
4713
Zheng Yan1a40e232008-09-26 10:09:34 -04004714int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4715 struct btrfs_root *root,
4716 struct extent_buffer *buf, u64 orig_start)
4717{
4718 int level;
4719 int ret;
4720
4721 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4722 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4723
4724 level = btrfs_header_level(buf);
4725 if (level == 0) {
4726 struct btrfs_leaf_ref *ref;
4727 struct btrfs_leaf_ref *orig_ref;
4728
4729 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4730 if (!orig_ref)
4731 return -ENOENT;
4732
4733 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4734 if (!ref) {
4735 btrfs_free_leaf_ref(root, orig_ref);
4736 return -ENOMEM;
4737 }
4738
4739 ref->nritems = orig_ref->nritems;
4740 memcpy(ref->extents, orig_ref->extents,
4741 sizeof(ref->extents[0]) * ref->nritems);
4742
4743 btrfs_free_leaf_ref(root, orig_ref);
4744
4745 ref->root_gen = trans->transid;
4746 ref->bytenr = buf->start;
4747 ref->owner = btrfs_header_owner(buf);
4748 ref->generation = btrfs_header_generation(buf);
4749 ret = btrfs_add_leaf_ref(root, ref, 0);
4750 WARN_ON(ret);
4751 btrfs_free_leaf_ref(root, ref);
4752 }
4753 return 0;
4754}
4755
4756static int noinline invalidate_extent_cache(struct btrfs_root *root,
4757 struct extent_buffer *leaf,
4758 struct btrfs_block_group_cache *group,
4759 struct btrfs_root *target_root)
4760{
4761 struct btrfs_key key;
4762 struct inode *inode = NULL;
4763 struct btrfs_file_extent_item *fi;
4764 u64 num_bytes;
4765 u64 skip_objectid = 0;
4766 u32 nritems;
4767 u32 i;
4768
4769 nritems = btrfs_header_nritems(leaf);
4770 for (i = 0; i < nritems; i++) {
4771 btrfs_item_key_to_cpu(leaf, &key, i);
4772 if (key.objectid == skip_objectid ||
4773 key.type != BTRFS_EXTENT_DATA_KEY)
4774 continue;
4775 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4776 if (btrfs_file_extent_type(leaf, fi) ==
4777 BTRFS_FILE_EXTENT_INLINE)
4778 continue;
4779 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4780 continue;
4781 if (!inode || inode->i_ino != key.objectid) {
4782 iput(inode);
4783 inode = btrfs_ilookup(target_root->fs_info->sb,
4784 key.objectid, target_root, 1);
4785 }
4786 if (!inode) {
4787 skip_objectid = key.objectid;
4788 continue;
4789 }
4790 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4791
4792 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4793 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004794 btrfs_drop_extent_cache(inode, key.offset,
4795 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004796 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4797 key.offset + num_bytes - 1, GFP_NOFS);
4798 cond_resched();
4799 }
4800 iput(inode);
4801 return 0;
4802}
4803
4804static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4805 struct btrfs_root *root,
4806 struct extent_buffer *leaf,
4807 struct btrfs_block_group_cache *group,
4808 struct inode *reloc_inode)
4809{
4810 struct btrfs_key key;
4811 struct btrfs_key extent_key;
4812 struct btrfs_file_extent_item *fi;
4813 struct btrfs_leaf_ref *ref;
4814 struct disk_extent *new_extent;
4815 u64 bytenr;
4816 u64 num_bytes;
4817 u32 nritems;
4818 u32 i;
4819 int ext_index;
4820 int nr_extent;
4821 int ret;
4822
4823 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4824 BUG_ON(!new_extent);
4825
4826 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4827 BUG_ON(!ref);
4828
4829 ext_index = -1;
4830 nritems = btrfs_header_nritems(leaf);
4831 for (i = 0; i < nritems; i++) {
4832 btrfs_item_key_to_cpu(leaf, &key, i);
4833 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4834 continue;
4835 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4836 if (btrfs_file_extent_type(leaf, fi) ==
4837 BTRFS_FILE_EXTENT_INLINE)
4838 continue;
4839 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4840 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4841 if (bytenr == 0)
4842 continue;
4843
4844 ext_index++;
4845 if (bytenr >= group->key.objectid + group->key.offset ||
4846 bytenr + num_bytes <= group->key.objectid)
4847 continue;
4848
4849 extent_key.objectid = bytenr;
4850 extent_key.offset = num_bytes;
4851 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4852 nr_extent = 1;
4853 ret = get_new_locations(reloc_inode, &extent_key,
4854 group->key.objectid, 1,
4855 &new_extent, &nr_extent);
4856 if (ret > 0)
4857 continue;
4858 BUG_ON(ret < 0);
4859
4860 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4861 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4862 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4863 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4864
Zheng Yan1a40e232008-09-26 10:09:34 -04004865 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4866 new_extent->disk_bytenr);
4867 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4868 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004869 btrfs_mark_buffer_dirty(leaf);
4870
4871 ret = btrfs_inc_extent_ref(trans, root,
4872 new_extent->disk_bytenr,
4873 new_extent->disk_num_bytes,
4874 leaf->start,
4875 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004876 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004877 BUG_ON(ret);
4878 ret = btrfs_free_extent(trans, root,
4879 bytenr, num_bytes, leaf->start,
4880 btrfs_header_owner(leaf),
4881 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004882 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004883 BUG_ON(ret);
4884 cond_resched();
4885 }
4886 kfree(new_extent);
4887 BUG_ON(ext_index + 1 != ref->nritems);
4888 btrfs_free_leaf_ref(root, ref);
4889 return 0;
4890}
4891
Yan Zhengf82d02d2008-10-29 14:49:05 -04004892int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4893 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004894{
4895 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004896 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004897
4898 if (root->reloc_root) {
4899 reloc_root = root->reloc_root;
4900 root->reloc_root = NULL;
4901 list_add(&reloc_root->dead_list,
4902 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004903
4904 btrfs_set_root_bytenr(&reloc_root->root_item,
4905 reloc_root->node->start);
4906 btrfs_set_root_level(&root->root_item,
4907 btrfs_header_level(reloc_root->node));
4908 memset(&reloc_root->root_item.drop_progress, 0,
4909 sizeof(struct btrfs_disk_key));
4910 reloc_root->root_item.drop_level = 0;
4911
4912 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4913 &reloc_root->root_key,
4914 &reloc_root->root_item);
4915 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004916 }
4917 return 0;
4918}
4919
4920int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4921{
4922 struct btrfs_trans_handle *trans;
4923 struct btrfs_root *reloc_root;
4924 struct btrfs_root *prev_root = NULL;
4925 struct list_head dead_roots;
4926 int ret;
4927 unsigned long nr;
4928
4929 INIT_LIST_HEAD(&dead_roots);
4930 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4931
4932 while (!list_empty(&dead_roots)) {
4933 reloc_root = list_entry(dead_roots.prev,
4934 struct btrfs_root, dead_list);
4935 list_del_init(&reloc_root->dead_list);
4936
4937 BUG_ON(reloc_root->commit_root != NULL);
4938 while (1) {
4939 trans = btrfs_join_transaction(root, 1);
4940 BUG_ON(!trans);
4941
4942 mutex_lock(&root->fs_info->drop_mutex);
4943 ret = btrfs_drop_snapshot(trans, reloc_root);
4944 if (ret != -EAGAIN)
4945 break;
4946 mutex_unlock(&root->fs_info->drop_mutex);
4947
4948 nr = trans->blocks_used;
4949 ret = btrfs_end_transaction(trans, root);
4950 BUG_ON(ret);
4951 btrfs_btree_balance_dirty(root, nr);
4952 }
4953
4954 free_extent_buffer(reloc_root->node);
4955
4956 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4957 &reloc_root->root_key);
4958 BUG_ON(ret);
4959 mutex_unlock(&root->fs_info->drop_mutex);
4960
4961 nr = trans->blocks_used;
4962 ret = btrfs_end_transaction(trans, root);
4963 BUG_ON(ret);
4964 btrfs_btree_balance_dirty(root, nr);
4965
4966 kfree(prev_root);
4967 prev_root = reloc_root;
4968 }
4969 if (prev_root) {
4970 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4971 kfree(prev_root);
4972 }
4973 return 0;
4974}
4975
4976int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4977{
4978 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4979 return 0;
4980}
4981
4982int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4983{
4984 struct btrfs_root *reloc_root;
4985 struct btrfs_trans_handle *trans;
4986 struct btrfs_key location;
4987 int found;
4988 int ret;
4989
4990 mutex_lock(&root->fs_info->tree_reloc_mutex);
4991 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4992 BUG_ON(ret);
4993 found = !list_empty(&root->fs_info->dead_reloc_roots);
4994 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4995
4996 if (found) {
4997 trans = btrfs_start_transaction(root, 1);
4998 BUG_ON(!trans);
4999 ret = btrfs_commit_transaction(trans, root);
5000 BUG_ON(ret);
5001 }
5002
5003 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5004 location.offset = (u64)-1;
5005 location.type = BTRFS_ROOT_ITEM_KEY;
5006
5007 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5008 BUG_ON(!reloc_root);
5009 btrfs_orphan_cleanup(reloc_root);
5010 return 0;
5011}
5012
5013static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
5014 struct btrfs_root *root)
5015{
5016 struct btrfs_root *reloc_root;
5017 struct extent_buffer *eb;
5018 struct btrfs_root_item *root_item;
5019 struct btrfs_key root_key;
5020 int ret;
5021
5022 BUG_ON(!root->ref_cows);
5023 if (root->reloc_root)
5024 return 0;
5025
5026 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5027 BUG_ON(!root_item);
5028
5029 ret = btrfs_copy_root(trans, root, root->commit_root,
5030 &eb, BTRFS_TREE_RELOC_OBJECTID);
5031 BUG_ON(ret);
5032
5033 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5034 root_key.offset = root->root_key.objectid;
5035 root_key.type = BTRFS_ROOT_ITEM_KEY;
5036
5037 memcpy(root_item, &root->root_item, sizeof(root_item));
5038 btrfs_set_root_refs(root_item, 0);
5039 btrfs_set_root_bytenr(root_item, eb->start);
5040 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04005041 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005042
5043 btrfs_tree_unlock(eb);
5044 free_extent_buffer(eb);
5045
5046 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5047 &root_key, root_item);
5048 BUG_ON(ret);
5049 kfree(root_item);
5050
5051 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5052 &root_key);
5053 BUG_ON(!reloc_root);
5054 reloc_root->last_trans = trans->transid;
5055 reloc_root->commit_root = NULL;
5056 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5057
5058 root->reloc_root = reloc_root;
5059 return 0;
5060}
5061
5062/*
5063 * Core function of space balance.
5064 *
5065 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04005066 * counted roots. There is one reloc tree for each subvol, and all
5067 * reloc trees share same root key objectid. Reloc trees are snapshots
5068 * of the latest committed roots of subvols (root->commit_root).
5069 *
5070 * To relocate a tree block referenced by a subvol, there are two steps.
5071 * COW the block through subvol's reloc tree, then update block pointer
5072 * in the subvol to point to the new block. Since all reloc trees share
5073 * same root key objectid, doing special handing for tree blocks owned
5074 * by them is easy. Once a tree block has been COWed in one reloc tree,
5075 * we can use the resulting new block directly when the same block is
5076 * required to COW again through other reloc trees. By this way, relocated
5077 * tree blocks are shared between reloc trees, so they are also shared
5078 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04005079 */
5080static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
5081 struct btrfs_root *root,
5082 struct btrfs_path *path,
5083 struct btrfs_key *first_key,
5084 struct btrfs_ref_path *ref_path,
5085 struct btrfs_block_group_cache *group,
5086 struct inode *reloc_inode)
5087{
5088 struct btrfs_root *reloc_root;
5089 struct extent_buffer *eb = NULL;
5090 struct btrfs_key *keys;
5091 u64 *nodes;
5092 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005093 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005094 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005095 int ret;
5096
5097 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5098 lowest_level = ref_path->owner_objectid;
5099
Yan Zhengf82d02d2008-10-29 14:49:05 -04005100 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005101 path->lowest_level = lowest_level;
5102 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5103 BUG_ON(ret < 0);
5104 path->lowest_level = 0;
5105 btrfs_release_path(root, path);
5106 return 0;
5107 }
5108
Zheng Yan1a40e232008-09-26 10:09:34 -04005109 mutex_lock(&root->fs_info->tree_reloc_mutex);
5110 ret = init_reloc_tree(trans, root);
5111 BUG_ON(ret);
5112 reloc_root = root->reloc_root;
5113
Yan Zhengf82d02d2008-10-29 14:49:05 -04005114 shared_level = ref_path->shared_level;
5115 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005116
Yan Zhengf82d02d2008-10-29 14:49:05 -04005117 keys = ref_path->node_keys;
5118 nodes = ref_path->new_nodes;
5119 memset(&keys[shared_level + 1], 0,
5120 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5121 memset(&nodes[shared_level + 1], 0,
5122 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04005123
Yan Zhengf82d02d2008-10-29 14:49:05 -04005124 if (nodes[lowest_level] == 0) {
5125 path->lowest_level = lowest_level;
5126 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5127 0, 1);
5128 BUG_ON(ret);
5129 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5130 eb = path->nodes[level];
5131 if (!eb || eb == reloc_root->node)
5132 break;
5133 nodes[level] = eb->start;
5134 if (level == 0)
5135 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5136 else
5137 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5138 }
5139 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5140 eb = path->nodes[0];
5141 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5142 group, reloc_inode);
5143 BUG_ON(ret);
5144 }
5145 btrfs_release_path(reloc_root, path);
5146 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005147 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005148 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04005149 BUG_ON(ret);
5150 }
5151
Zheng Yan1a40e232008-09-26 10:09:34 -04005152 /*
5153 * replace tree blocks in the fs tree with tree blocks in
5154 * the reloc tree.
5155 */
5156 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5157 BUG_ON(ret < 0);
5158
5159 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005160 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5161 0, 0);
5162 BUG_ON(ret);
5163 extent_buffer_get(path->nodes[0]);
5164 eb = path->nodes[0];
5165 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005166 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5167 BUG_ON(ret);
5168 free_extent_buffer(eb);
5169 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005170
Yan Zhengf82d02d2008-10-29 14:49:05 -04005171 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005172 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005173 return 0;
5174}
5175
5176static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
5177 struct btrfs_root *root,
5178 struct btrfs_path *path,
5179 struct btrfs_key *first_key,
5180 struct btrfs_ref_path *ref_path)
5181{
5182 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005183
5184 ret = relocate_one_path(trans, root, path, first_key,
5185 ref_path, NULL, NULL);
5186 BUG_ON(ret);
5187
5188 if (root == root->fs_info->extent_root)
5189 btrfs_extent_post_op(trans, root);
Zheng Yan1a40e232008-09-26 10:09:34 -04005190
5191 return 0;
5192}
5193
5194static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
5195 struct btrfs_root *extent_root,
5196 struct btrfs_path *path,
5197 struct btrfs_key *extent_key)
5198{
5199 int ret;
5200
Zheng Yan1a40e232008-09-26 10:09:34 -04005201 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5202 if (ret)
5203 goto out;
5204 ret = btrfs_del_item(trans, extent_root, path);
5205out:
Chris Masonedbd8d42007-12-21 16:27:24 -05005206 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005207 return ret;
5208}
5209
5210static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
5211 struct btrfs_ref_path *ref_path)
5212{
5213 struct btrfs_key root_key;
5214
5215 root_key.objectid = ref_path->root_objectid;
5216 root_key.type = BTRFS_ROOT_ITEM_KEY;
5217 if (is_cowonly_root(ref_path->root_objectid))
5218 root_key.offset = 0;
5219 else
5220 root_key.offset = (u64)-1;
5221
5222 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5223}
5224
5225static int noinline relocate_one_extent(struct btrfs_root *extent_root,
5226 struct btrfs_path *path,
5227 struct btrfs_key *extent_key,
5228 struct btrfs_block_group_cache *group,
5229 struct inode *reloc_inode, int pass)
5230{
5231 struct btrfs_trans_handle *trans;
5232 struct btrfs_root *found_root;
5233 struct btrfs_ref_path *ref_path = NULL;
5234 struct disk_extent *new_extents = NULL;
5235 int nr_extents = 0;
5236 int loops;
5237 int ret;
5238 int level;
5239 struct btrfs_key first_key;
5240 u64 prev_block = 0;
5241
Zheng Yan1a40e232008-09-26 10:09:34 -04005242
5243 trans = btrfs_start_transaction(extent_root, 1);
5244 BUG_ON(!trans);
5245
5246 if (extent_key->objectid == 0) {
5247 ret = del_extent_zero(trans, extent_root, path, extent_key);
5248 goto out;
5249 }
5250
5251 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5252 if (!ref_path) {
5253 ret = -ENOMEM;
5254 goto out;
5255 }
5256
5257 for (loops = 0; ; loops++) {
5258 if (loops == 0) {
5259 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5260 extent_key->objectid);
5261 } else {
5262 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5263 }
5264 if (ret < 0)
5265 goto out;
5266 if (ret > 0)
5267 break;
5268
5269 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5270 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5271 continue;
5272
5273 found_root = read_ref_root(extent_root->fs_info, ref_path);
5274 BUG_ON(!found_root);
5275 /*
5276 * for reference counted tree, only process reference paths
5277 * rooted at the latest committed root.
5278 */
5279 if (found_root->ref_cows &&
5280 ref_path->root_generation != found_root->root_key.offset)
5281 continue;
5282
5283 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5284 if (pass == 0) {
5285 /*
5286 * copy data extents to new locations
5287 */
5288 u64 group_start = group->key.objectid;
5289 ret = relocate_data_extent(reloc_inode,
5290 extent_key,
5291 group_start);
5292 if (ret < 0)
5293 goto out;
5294 break;
5295 }
5296 level = 0;
5297 } else {
5298 level = ref_path->owner_objectid;
5299 }
5300
5301 if (prev_block != ref_path->nodes[level]) {
5302 struct extent_buffer *eb;
5303 u64 block_start = ref_path->nodes[level];
5304 u64 block_size = btrfs_level_size(found_root, level);
5305
5306 eb = read_tree_block(found_root, block_start,
5307 block_size, 0);
5308 btrfs_tree_lock(eb);
5309 BUG_ON(level != btrfs_header_level(eb));
5310
5311 if (level == 0)
5312 btrfs_item_key_to_cpu(eb, &first_key, 0);
5313 else
5314 btrfs_node_key_to_cpu(eb, &first_key, 0);
5315
5316 btrfs_tree_unlock(eb);
5317 free_extent_buffer(eb);
5318 prev_block = block_start;
5319 }
5320
5321 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
5322 pass >= 2) {
5323 /*
5324 * use fallback method to process the remaining
5325 * references.
5326 */
5327 if (!new_extents) {
5328 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04005329 new_extents = kmalloc(sizeof(*new_extents),
5330 GFP_NOFS);
5331 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005332 ret = get_new_locations(reloc_inode,
5333 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04005334 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04005335 &new_extents,
5336 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005337 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04005338 goto out;
5339 }
5340 btrfs_record_root_in_trans(found_root);
5341 ret = replace_one_extent(trans, found_root,
5342 path, extent_key,
5343 &first_key, ref_path,
5344 new_extents, nr_extents);
5345 if (ret < 0)
5346 goto out;
5347 continue;
5348 }
5349
5350 btrfs_record_root_in_trans(found_root);
5351 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5352 ret = relocate_tree_block(trans, found_root, path,
5353 &first_key, ref_path);
5354 } else {
5355 /*
5356 * try to update data extent references while
5357 * keeping metadata shared between snapshots.
5358 */
5359 ret = relocate_one_path(trans, found_root, path,
5360 &first_key, ref_path,
5361 group, reloc_inode);
5362 }
5363 if (ret < 0)
5364 goto out;
5365 }
5366 ret = 0;
5367out:
5368 btrfs_end_transaction(trans, extent_root);
5369 kfree(new_extents);
5370 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005371 return ret;
5372}
5373
Chris Masonec44a352008-04-28 15:29:52 -04005374static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5375{
5376 u64 num_devices;
5377 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5378 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5379
Chris Masona061fc82008-05-07 11:43:44 -04005380 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04005381 if (num_devices == 1) {
5382 stripped |= BTRFS_BLOCK_GROUP_DUP;
5383 stripped = flags & ~stripped;
5384
5385 /* turn raid0 into single device chunks */
5386 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5387 return stripped;
5388
5389 /* turn mirroring into duplication */
5390 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5391 BTRFS_BLOCK_GROUP_RAID10))
5392 return stripped | BTRFS_BLOCK_GROUP_DUP;
5393 return flags;
5394 } else {
5395 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04005396 if (flags & stripped)
5397 return flags;
5398
5399 stripped |= BTRFS_BLOCK_GROUP_DUP;
5400 stripped = flags & ~stripped;
5401
5402 /* switch duplicated blocks with raid1 */
5403 if (flags & BTRFS_BLOCK_GROUP_DUP)
5404 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5405
5406 /* turn single device chunks into raid0 */
5407 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5408 }
5409 return flags;
5410}
5411
Chris Mason0ef3e662008-05-24 14:04:53 -04005412int __alloc_chunk_for_shrink(struct btrfs_root *root,
5413 struct btrfs_block_group_cache *shrink_block_group,
5414 int force)
5415{
5416 struct btrfs_trans_handle *trans;
5417 u64 new_alloc_flags;
5418 u64 calc;
5419
Chris Masonc286ac42008-07-22 23:06:41 -04005420 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005421 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04005422 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005423
Chris Mason0ef3e662008-05-24 14:04:53 -04005424 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005425 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04005426
Chris Mason0ef3e662008-05-24 14:04:53 -04005427 new_alloc_flags = update_block_group_flags(root,
5428 shrink_block_group->flags);
5429 if (new_alloc_flags != shrink_block_group->flags) {
5430 calc =
5431 btrfs_block_group_used(&shrink_block_group->item);
5432 } else {
5433 calc = shrink_block_group->key.offset;
5434 }
Chris Masonc286ac42008-07-22 23:06:41 -04005435 spin_unlock(&shrink_block_group->lock);
5436
Chris Mason0ef3e662008-05-24 14:04:53 -04005437 do_chunk_alloc(trans, root->fs_info->extent_root,
5438 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04005439
Chris Mason0ef3e662008-05-24 14:04:53 -04005440 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04005441 } else
5442 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005443 return 0;
5444}
5445
Zheng Yan1a40e232008-09-26 10:09:34 -04005446static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5447 struct btrfs_root *root,
5448 u64 objectid, u64 size)
5449{
5450 struct btrfs_path *path;
5451 struct btrfs_inode_item *item;
5452 struct extent_buffer *leaf;
5453 int ret;
5454
5455 path = btrfs_alloc_path();
5456 if (!path)
5457 return -ENOMEM;
5458
5459 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5460 if (ret)
5461 goto out;
5462
5463 leaf = path->nodes[0];
5464 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5465 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5466 btrfs_set_inode_generation(leaf, item, 1);
5467 btrfs_set_inode_size(leaf, item, size);
5468 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zhengd899e052008-10-30 14:25:28 -04005469 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM |
5470 BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005471 btrfs_mark_buffer_dirty(leaf);
5472 btrfs_release_path(root, path);
5473out:
5474 btrfs_free_path(path);
5475 return ret;
5476}
5477
5478static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
5479 struct btrfs_block_group_cache *group)
5480{
5481 struct inode *inode = NULL;
5482 struct btrfs_trans_handle *trans;
5483 struct btrfs_root *root;
5484 struct btrfs_key root_key;
5485 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5486 int err = 0;
5487
5488 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5489 root_key.type = BTRFS_ROOT_ITEM_KEY;
5490 root_key.offset = (u64)-1;
5491 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5492 if (IS_ERR(root))
5493 return ERR_CAST(root);
5494
5495 trans = btrfs_start_transaction(root, 1);
5496 BUG_ON(!trans);
5497
5498 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5499 if (err)
5500 goto out;
5501
5502 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5503 BUG_ON(err);
5504
5505 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04005506 group->key.offset, 0, group->key.offset,
5507 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005508 BUG_ON(err);
5509
5510 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5511 if (inode->i_state & I_NEW) {
5512 BTRFS_I(inode)->root = root;
5513 BTRFS_I(inode)->location.objectid = objectid;
5514 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5515 BTRFS_I(inode)->location.offset = 0;
5516 btrfs_read_locked_inode(inode);
5517 unlock_new_inode(inode);
5518 BUG_ON(is_bad_inode(inode));
5519 } else {
5520 BUG_ON(1);
5521 }
5522
5523 err = btrfs_orphan_add(trans, inode);
5524out:
5525 btrfs_end_transaction(trans, root);
5526 if (err) {
5527 if (inode)
5528 iput(inode);
5529 inode = ERR_PTR(err);
5530 }
5531 return inode;
5532}
5533
5534int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05005535{
5536 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05005537 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04005538 struct btrfs_fs_info *info = root->fs_info;
5539 struct extent_buffer *leaf;
5540 struct inode *reloc_inode;
5541 struct btrfs_block_group_cache *block_group;
5542 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04005543 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05005544 u64 cur_byte;
5545 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05005546 u32 nritems;
5547 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04005548 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04005549 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005550
Chris Masonedbd8d42007-12-21 16:27:24 -05005551 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04005552
5553 block_group = btrfs_lookup_block_group(info, group_start);
5554 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05005555
Chris Mason323da792008-05-09 11:46:48 -04005556 printk("btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005557 (unsigned long long)block_group->key.objectid,
5558 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04005559
Zheng Yan1a40e232008-09-26 10:09:34 -04005560 path = btrfs_alloc_path();
5561 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04005562
Zheng Yan1a40e232008-09-26 10:09:34 -04005563 reloc_inode = create_reloc_inode(info, block_group);
5564 BUG_ON(IS_ERR(reloc_inode));
5565
Zheng Yan1a40e232008-09-26 10:09:34 -04005566 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05005567 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005568
Zheng Yan1a40e232008-09-26 10:09:34 -04005569 btrfs_start_delalloc_inodes(info->tree_root);
5570 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04005571again:
Yan Zhengd899e052008-10-30 14:25:28 -04005572 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005573 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04005574 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005575 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05005576 key.offset = 0;
5577 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05005578 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05005579
Zheng Yan1a40e232008-09-26 10:09:34 -04005580 trans = btrfs_start_transaction(info->tree_root, 1);
5581 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04005582
Zheng Yan1a40e232008-09-26 10:09:34 -04005583 mutex_lock(&root->fs_info->cleaner_mutex);
5584 btrfs_clean_old_snapshots(info->tree_root);
5585 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5586 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04005587
Yan73e48b22008-01-03 14:14:39 -05005588 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05005589 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5590 if (ret < 0)
5591 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04005592next:
Chris Masonedbd8d42007-12-21 16:27:24 -05005593 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05005594 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05005595 if (path->slots[0] >= nritems) {
5596 ret = btrfs_next_leaf(root, path);
5597 if (ret < 0)
5598 goto out;
5599 if (ret == 1) {
5600 ret = 0;
5601 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005602 }
Yan73e48b22008-01-03 14:14:39 -05005603 leaf = path->nodes[0];
5604 nritems = btrfs_header_nritems(leaf);
5605 }
5606
Zheng Yan1a40e232008-09-26 10:09:34 -04005607 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05005608
Zheng Yan1a40e232008-09-26 10:09:34 -04005609 if (key.objectid >= block_group->key.objectid +
5610 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04005611 break;
5612
Chris Mason725c8462008-01-04 16:47:16 -05005613 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05005614 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005615 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05005616 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005617 continue;
Chris Mason725c8462008-01-04 16:47:16 -05005618 }
5619 progress = 1;
5620
Zheng Yan1a40e232008-09-26 10:09:34 -04005621 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5622 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05005623 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005624 goto next;
5625 }
Yan73e48b22008-01-03 14:14:39 -05005626
Chris Masonedbd8d42007-12-21 16:27:24 -05005627 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005628 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05005629 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005630
5631 __alloc_chunk_for_shrink(root, block_group, 0);
5632 ret = relocate_one_extent(root, path, &key, block_group,
5633 reloc_inode, pass);
5634 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005635 if (ret > 0)
5636 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005637
5638 key.objectid = cur_byte;
5639 key.type = 0;
5640 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005641 }
5642
5643 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005644
5645 if (pass == 0) {
5646 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5647 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5648 WARN_ON(reloc_inode->i_mapping->nrpages);
5649 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005650
5651 if (total_found > 0) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005652 printk("btrfs found %llu extents in pass %d\n",
5653 (unsigned long long)total_found, pass);
5654 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04005655 if (total_found == skipped && pass > 2) {
5656 iput(reloc_inode);
5657 reloc_inode = create_reloc_inode(info, block_group);
5658 pass = 0;
5659 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005660 goto again;
5661 }
5662
Zheng Yan1a40e232008-09-26 10:09:34 -04005663 /* delete reloc_inode */
5664 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005665
Zheng Yan1a40e232008-09-26 10:09:34 -04005666 /* unpin extents in this range */
5667 trans = btrfs_start_transaction(info->tree_root, 1);
5668 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005669
Zheng Yan1a40e232008-09-26 10:09:34 -04005670 spin_lock(&block_group->lock);
5671 WARN_ON(block_group->pinned > 0);
5672 WARN_ON(block_group->reserved > 0);
5673 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5674 spin_unlock(&block_group->lock);
5675 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005676out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005677 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005678 return ret;
5679}
5680
Chris Mason0b86a832008-03-24 15:01:56 -04005681int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
5682 struct btrfs_key *key)
5683{
Chris Mason925baed2008-06-25 16:01:30 -04005684 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005685 struct btrfs_key found_key;
5686 struct extent_buffer *leaf;
5687 int slot;
5688
5689 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5690 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005691 goto out;
5692
Chris Mason0b86a832008-03-24 15:01:56 -04005693 while(1) {
5694 slot = path->slots[0];
5695 leaf = path->nodes[0];
5696 if (slot >= btrfs_header_nritems(leaf)) {
5697 ret = btrfs_next_leaf(root, path);
5698 if (ret == 0)
5699 continue;
5700 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005701 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005702 break;
5703 }
5704 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5705
5706 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005707 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5708 ret = 0;
5709 goto out;
5710 }
Chris Mason0b86a832008-03-24 15:01:56 -04005711 path->slots[0]++;
5712 }
5713 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005714out:
Chris Mason0b86a832008-03-24 15:01:56 -04005715 return ret;
5716}
5717
Zheng Yan1a40e232008-09-26 10:09:34 -04005718int btrfs_free_block_groups(struct btrfs_fs_info *info)
5719{
5720 struct btrfs_block_group_cache *block_group;
5721 struct rb_node *n;
5722
Zheng Yan1a40e232008-09-26 10:09:34 -04005723 spin_lock(&info->block_group_cache_lock);
5724 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5725 block_group = rb_entry(n, struct btrfs_block_group_cache,
5726 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04005727 rb_erase(&block_group->cache_node,
5728 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04005729 spin_unlock(&info->block_group_cache_lock);
5730
5731 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005732 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005733 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005734 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005735 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04005736
5737 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005738 }
5739 spin_unlock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005740 return 0;
5741}
5742
Chris Mason9078a3e2007-04-26 16:46:15 -04005743int btrfs_read_block_groups(struct btrfs_root *root)
5744{
5745 struct btrfs_path *path;
5746 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005747 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005748 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005749 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005750 struct btrfs_key key;
5751 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005752 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005753
Chris Masonbe744172007-05-06 10:15:01 -04005754 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005755 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005756 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005757 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005758 path = btrfs_alloc_path();
5759 if (!path)
5760 return -ENOMEM;
5761
5762 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005763 ret = find_first_block_group(root, path, &key);
5764 if (ret > 0) {
5765 ret = 0;
5766 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005767 }
Chris Mason0b86a832008-03-24 15:01:56 -04005768 if (ret != 0)
5769 goto error;
5770
Chris Mason5f39d392007-10-15 16:14:19 -04005771 leaf = path->nodes[0];
5772 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005773 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005774 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005775 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005776 break;
5777 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005778
Chris Masonc286ac42008-07-22 23:06:41 -04005779 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005780 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005781 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005782 read_extent_buffer(leaf, &cache->item,
5783 btrfs_item_ptr_offset(leaf, path->slots[0]),
5784 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005785 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005786
Chris Mason9078a3e2007-04-26 16:46:15 -04005787 key.objectid = found_key.objectid + found_key.offset;
5788 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005789 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005790
Chris Mason6324fbf2008-03-24 15:01:59 -04005791 ret = update_space_info(info, cache->flags, found_key.offset,
5792 btrfs_block_group_used(&cache->item),
5793 &space_info);
5794 BUG_ON(ret);
5795 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005796 down_write(&space_info->groups_sem);
5797 list_add_tail(&cache->list, &space_info->block_groups);
5798 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005799
Josef Bacik0f9dd462008-09-23 13:14:11 -04005800 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5801 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005802
5803 set_avail_alloc_bits(root->fs_info, cache->flags);
Chris Mason9078a3e2007-04-26 16:46:15 -04005804 }
Chris Mason0b86a832008-03-24 15:01:56 -04005805 ret = 0;
5806error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005807 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005808 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005809}
Chris Mason6324fbf2008-03-24 15:01:59 -04005810
5811int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5812 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005813 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005814 u64 size)
5815{
5816 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005817 struct btrfs_root *extent_root;
5818 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005819
5820 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005821
Chris Masone02119d2008-09-05 16:13:11 -04005822 root->fs_info->last_trans_new_blockgroup = trans->transid;
5823
Chris Mason8f18cf12008-04-25 16:53:30 -04005824 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005825 if (!cache)
5826 return -ENOMEM;
5827
Chris Masone17cade2008-04-15 15:41:47 -04005828 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005829 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04005830 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005831 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005832 INIT_LIST_HEAD(&cache->list);
Chris Mason6324fbf2008-03-24 15:01:59 -04005833 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04005834
Chris Mason6324fbf2008-03-24 15:01:59 -04005835 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005836 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5837 cache->flags = type;
5838 btrfs_set_block_group_flags(&cache->item, type);
5839
5840 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5841 &cache->space_info);
5842 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005843 down_write(&cache->space_info->groups_sem);
5844 list_add_tail(&cache->list, &cache->space_info->block_groups);
5845 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005846
Josef Bacik0f9dd462008-09-23 13:14:11 -04005847 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5848 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005849
Chris Mason6324fbf2008-03-24 15:01:59 -04005850 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5851 sizeof(cache->item));
5852 BUG_ON(ret);
5853
Chris Mason87ef2bb2008-10-30 11:23:27 -04005854 finish_current_insert(trans, extent_root, 0);
5855 ret = del_pending_extents(trans, extent_root, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04005856 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04005857 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005858
Chris Mason6324fbf2008-03-24 15:01:59 -04005859 return 0;
5860}
Zheng Yan1a40e232008-09-26 10:09:34 -04005861
5862int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5863 struct btrfs_root *root, u64 group_start)
5864{
5865 struct btrfs_path *path;
5866 struct btrfs_block_group_cache *block_group;
5867 struct btrfs_key key;
5868 int ret;
5869
Zheng Yan1a40e232008-09-26 10:09:34 -04005870 root = root->fs_info->extent_root;
5871
5872 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5873 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05005874 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04005875
5876 memcpy(&key, &block_group->key, sizeof(key));
5877
5878 path = btrfs_alloc_path();
5879 BUG_ON(!path);
5880
5881 btrfs_remove_free_space_cache(block_group);
5882 rb_erase(&block_group->cache_node,
5883 &root->fs_info->block_group_cache_tree);
Josef Bacik80eb2342008-10-29 14:49:05 -04005884 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005885 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005886 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005887
Yan Zhengc146afa2008-11-12 14:34:12 -05005888 spin_lock(&block_group->space_info->lock);
5889 block_group->space_info->total_bytes -= block_group->key.offset;
5890 block_group->space_info->bytes_readonly -= block_group->key.offset;
5891 spin_unlock(&block_group->space_info->lock);
5892
Zheng Yan1a40e232008-09-26 10:09:34 -04005893 /*
5894 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5895 kfree(shrink_block_group);
5896 */
5897
5898 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5899 if (ret > 0)
5900 ret = -EIO;
5901 if (ret < 0)
5902 goto out;
5903
5904 ret = btrfs_del_item(trans, root, path);
5905out:
5906 btrfs_free_path(path);
5907 return ret;
5908}