blob: 62d49705d14047890996a965123fd630ef53735c [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 Mason15916de2008-11-19 21:17:22 -050031#include "compat.h"
Chris Masonfec577f2007-02-26 10:40:21 -050032
Zheng Yan31840ae2008-09-23 13:14:14 -040033#define PENDING_EXTENT_INSERT 0
34#define PENDING_EXTENT_DELETE 1
35#define PENDING_BACKREF_UPDATE 2
36
37struct pending_extent_op {
38 int type;
39 u64 bytenr;
40 u64 num_bytes;
41 u64 parent;
42 u64 orig_parent;
43 u64 generation;
44 u64 orig_generation;
45 int level;
Josef Bacikf3465ca2008-11-12 14:19:50 -050046 struct list_head list;
47 int del;
Zheng Yan31840ae2008-09-23 13:14:14 -040048};
49
Chris Masone089f052007-03-16 16:20:31 -040050static int finish_current_insert(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -040051 btrfs_root *extent_root, int all);
Chris Masone20d96d2007-03-22 12:13:20 -040052static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -040053 btrfs_root *extent_root, int all);
Chris Mason925baed2008-06-25 16:01:30 -040054static struct btrfs_block_group_cache *
55__btrfs_find_block_group(struct btrfs_root *root,
56 struct btrfs_block_group_cache *hint,
57 u64 search_start, int data, int owner);
Josef Bacikf3465ca2008-11-12 14:19:50 -050058static int pin_down_bytes(struct btrfs_trans_handle *trans,
59 struct btrfs_root *root,
60 u64 bytenr, u64 num_bytes, int is_data);
61static int update_block_group(struct btrfs_trans_handle *trans,
62 struct btrfs_root *root,
63 u64 bytenr, u64 num_bytes, int alloc,
64 int mark_free);
Yand548ee52008-01-03 13:56:30 -050065
Josef Bacik0f9dd462008-09-23 13:14:11 -040066static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
67{
68 return (cache->flags & bits) == bits;
69}
70
71/*
72 * this adds the block group to the fs_info rb tree for the block group
73 * cache
74 */
75int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
76 struct btrfs_block_group_cache *block_group)
77{
78 struct rb_node **p;
79 struct rb_node *parent = NULL;
80 struct btrfs_block_group_cache *cache;
81
82 spin_lock(&info->block_group_cache_lock);
83 p = &info->block_group_cache_tree.rb_node;
84
85 while (*p) {
86 parent = *p;
87 cache = rb_entry(parent, struct btrfs_block_group_cache,
88 cache_node);
89 if (block_group->key.objectid < cache->key.objectid) {
90 p = &(*p)->rb_left;
91 } else if (block_group->key.objectid > cache->key.objectid) {
92 p = &(*p)->rb_right;
93 } else {
94 spin_unlock(&info->block_group_cache_lock);
95 return -EEXIST;
96 }
97 }
98
99 rb_link_node(&block_group->cache_node, parent, p);
100 rb_insert_color(&block_group->cache_node,
101 &info->block_group_cache_tree);
102 spin_unlock(&info->block_group_cache_lock);
103
104 return 0;
105}
106
107/*
108 * This will return the block group at or after bytenr if contains is 0, else
109 * it will return the block group that contains the bytenr
110 */
111static struct btrfs_block_group_cache *
112block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
113 int contains)
114{
115 struct btrfs_block_group_cache *cache, *ret = NULL;
116 struct rb_node *n;
117 u64 end, start;
118
119 spin_lock(&info->block_group_cache_lock);
120 n = info->block_group_cache_tree.rb_node;
121
122 while (n) {
123 cache = rb_entry(n, struct btrfs_block_group_cache,
124 cache_node);
125 end = cache->key.objectid + cache->key.offset - 1;
126 start = cache->key.objectid;
127
128 if (bytenr < start) {
129 if (!contains && (!ret || start < ret->key.objectid))
130 ret = cache;
131 n = n->rb_left;
132 } else if (bytenr > start) {
133 if (contains && bytenr <= end) {
134 ret = cache;
135 break;
136 }
137 n = n->rb_right;
138 } else {
139 ret = cache;
140 break;
141 }
142 }
143 spin_unlock(&info->block_group_cache_lock);
144
145 return ret;
146}
147
148/*
149 * this is only called by cache_block_group, since we could have freed extents
150 * we need to check the pinned_extents for any extents that can't be used yet
151 * since their free space will be released as soon as the transaction commits.
152 */
153static int add_new_free_space(struct btrfs_block_group_cache *block_group,
154 struct btrfs_fs_info *info, u64 start, u64 end)
155{
156 u64 extent_start, extent_end, size;
157 int ret;
158
Josef Bacik25179202008-10-29 14:49:05 -0400159 mutex_lock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400160 while (start < end) {
161 ret = find_first_extent_bit(&info->pinned_extents, start,
162 &extent_start, &extent_end,
163 EXTENT_DIRTY);
164 if (ret)
165 break;
166
167 if (extent_start == start) {
168 start = extent_end + 1;
169 } else if (extent_start > start && extent_start < end) {
170 size = extent_start - start;
Josef Bacik25179202008-10-29 14:49:05 -0400171 ret = btrfs_add_free_space_lock(block_group, start,
172 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400173 BUG_ON(ret);
174 start = extent_end + 1;
175 } else {
176 break;
177 }
178 }
179
180 if (start < end) {
181 size = end - start;
Josef Bacik25179202008-10-29 14:49:05 -0400182 ret = btrfs_add_free_space_lock(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400183 BUG_ON(ret);
184 }
Josef Bacik25179202008-10-29 14:49:05 -0400185 mutex_unlock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400186
187 return 0;
188}
189
Chris Masone37c9e62007-05-09 20:13:14 -0400190static int cache_block_group(struct btrfs_root *root,
191 struct btrfs_block_group_cache *block_group)
192{
193 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400194 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400195 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400196 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400197 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -0400198 u64 last = 0;
Yan7d7d6062007-09-14 16:15:28 -0400199 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -0400200 int found = 0;
201
Chris Mason00f5c792007-11-30 10:09:33 -0500202 if (!block_group)
203 return 0;
204
Chris Masone37c9e62007-05-09 20:13:14 -0400205 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400206
207 if (block_group->cached)
208 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400209
Chris Masone37c9e62007-05-09 20:13:14 -0400210 path = btrfs_alloc_path();
211 if (!path)
212 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400213
Chris Mason2cc58cf2007-08-27 16:49:44 -0400214 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400215 /*
216 * we get into deadlocks with paths held by callers of this function.
217 * since the alloc_mutex is protecting things right now, just
218 * skip the locking here
219 */
220 path->skip_locking = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400221 first_free = max_t(u64, block_group->key.objectid,
222 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Masone37c9e62007-05-09 20:13:14 -0400223 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -0400224 key.offset = 0;
225 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
226 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
227 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400228 goto err;
Chris Mason0b86a832008-03-24 15:01:56 -0400229 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -0500230 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400231 goto err;
Yand548ee52008-01-03 13:56:30 -0500232 if (ret == 0) {
233 leaf = path->nodes[0];
234 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
235 if (key.objectid + key.offset > first_free)
236 first_free = key.objectid + key.offset;
237 }
Chris Masone37c9e62007-05-09 20:13:14 -0400238 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400239 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400240 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400241 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400242 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400243 if (ret < 0)
244 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400245 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400246 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400247 else
Chris Masone37c9e62007-05-09 20:13:14 -0400248 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400249 }
Chris Mason5f39d392007-10-15 16:14:19 -0400250 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400251 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400252 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400253
Chris Masone37c9e62007-05-09 20:13:14 -0400254 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400255 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400256 break;
Yan7d7d6062007-09-14 16:15:28 -0400257
258 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
259 if (!found) {
260 last = first_free;
261 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400262 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400263
264 add_new_free_space(block_group, root->fs_info, last,
265 key.objectid);
266
Yan7d7d6062007-09-14 16:15:28 -0400267 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400268 }
Yan7d7d6062007-09-14 16:15:28 -0400269next:
Chris Masone37c9e62007-05-09 20:13:14 -0400270 path->slots[0]++;
271 }
272
Yan7d7d6062007-09-14 16:15:28 -0400273 if (!found)
274 last = first_free;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400275
276 add_new_free_space(block_group, root->fs_info, last,
277 block_group->key.objectid +
278 block_group->key.offset);
279
Chris Masone37c9e62007-05-09 20:13:14 -0400280 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400281 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400282err:
Chris Masone37c9e62007-05-09 20:13:14 -0400283 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400284 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400285}
286
Josef Bacik0f9dd462008-09-23 13:14:11 -0400287/*
288 * return the block group that starts at or after bytenr
289 */
Chris Mason0ef3e662008-05-24 14:04:53 -0400290struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
291 btrfs_fs_info *info,
292 u64 bytenr)
293{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400294 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400295
Josef Bacik0f9dd462008-09-23 13:14:11 -0400296 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400297
Josef Bacik0f9dd462008-09-23 13:14:11 -0400298 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400299}
300
Josef Bacik0f9dd462008-09-23 13:14:11 -0400301/*
302 * return the block group that contains teh given bytenr
303 */
Chris Mason5276aed2007-06-11 21:33:38 -0400304struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
305 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400306 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400307{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400308 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400309
Josef Bacik0f9dd462008-09-23 13:14:11 -0400310 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400311
Josef Bacik0f9dd462008-09-23 13:14:11 -0400312 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400313}
Chris Mason0b86a832008-03-24 15:01:56 -0400314
Josef Bacik0f9dd462008-09-23 13:14:11 -0400315static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
316 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400317{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400318 struct list_head *head = &info->space_info;
319 struct list_head *cur;
320 struct btrfs_space_info *found;
321 list_for_each(cur, head) {
322 found = list_entry(cur, struct btrfs_space_info, list);
323 if (found->flags == flags)
324 return found;
325 }
326 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400327}
328
Josef Bacik80eb2342008-10-29 14:49:05 -0400329static u64 div_factor(u64 num, int factor)
330{
331 if (factor == 10)
332 return num;
333 num *= factor;
334 do_div(num, 10);
335 return num;
336}
337
Chris Mason925baed2008-06-25 16:01:30 -0400338static struct btrfs_block_group_cache *
339__btrfs_find_block_group(struct btrfs_root *root,
340 struct btrfs_block_group_cache *hint,
341 u64 search_start, int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400342{
Chris Mason96b51792007-10-15 16:15:19 -0400343 struct btrfs_block_group_cache *cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400344 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400345 struct btrfs_fs_info *info = root->fs_info;
346 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400347 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400348 u64 free_check;
Chris Mason31f3c992007-04-30 15:25:45 -0400349 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400350 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400351 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400352
Chris Masona236aed2008-04-29 09:38:00 -0400353 if (data & BTRFS_BLOCK_GROUP_METADATA)
354 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400355
Chris Mason0ef3e662008-05-24 14:04:53 -0400356 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400357 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400358 shint = btrfs_lookup_first_block_group(info, search_start);
Yan Zheng2b820322008-11-17 21:11:30 -0500359 if (shint && block_group_bits(shint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400360 spin_lock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400361 used = btrfs_block_group_used(&shint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400362 if (used + shint->pinned + shint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500363 div_factor(shint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400364 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400365 return shint;
366 }
Chris Masonc286ac42008-07-22 23:06:41 -0400367 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400368 }
369 }
Yan Zheng2b820322008-11-17 21:11:30 -0500370 if (hint && block_group_bits(hint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400371 spin_lock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400372 used = btrfs_block_group_used(&hint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400373 if (used + hint->pinned + hint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500374 div_factor(hint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400375 spin_unlock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400376 return hint;
377 }
Chris Masonc286ac42008-07-22 23:06:41 -0400378 spin_unlock(&hint->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400379 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400380 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400381 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400382 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400383 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400384 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400385 }
Chris Mason31f3c992007-04-30 15:25:45 -0400386again:
Zheng Yane8569812008-09-26 10:05:48 -0400387 while (1) {
388 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400389 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400390 break;
Chris Mason96b51792007-10-15 16:15:19 -0400391
Chris Masonc286ac42008-07-22 23:06:41 -0400392 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400393 last = cache->key.objectid + cache->key.offset;
394 used = btrfs_block_group_used(&cache->item);
395
Yan Zheng2b820322008-11-17 21:11:30 -0500396 if (block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400397 free_check = div_factor(cache->key.offset, factor);
Zheng Yane8569812008-09-26 10:05:48 -0400398 if (used + cache->pinned + cache->reserved <
399 free_check) {
Chris Mason8790d502008-04-03 16:29:03 -0400400 found_group = cache;
Chris Masonc286ac42008-07-22 23:06:41 -0400401 spin_unlock(&cache->lock);
Chris Mason8790d502008-04-03 16:29:03 -0400402 goto found;
403 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400404 }
Chris Masonc286ac42008-07-22 23:06:41 -0400405 spin_unlock(&cache->lock);
Chris Masonde428b62007-05-18 13:28:27 -0400406 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400407 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400408 if (!wrapped) {
409 last = search_start;
410 wrapped = 1;
411 goto again;
412 }
413 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400414 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400415 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400416 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400417 goto again;
418 }
Chris Masonbe744172007-05-06 10:15:01 -0400419found:
Chris Mason31f3c992007-04-30 15:25:45 -0400420 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400421}
422
Chris Mason925baed2008-06-25 16:01:30 -0400423struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
424 struct btrfs_block_group_cache
425 *hint, u64 search_start,
426 int data, int owner)
427{
428
429 struct btrfs_block_group_cache *ret;
Chris Mason925baed2008-06-25 16:01:30 -0400430 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
Chris Mason925baed2008-06-25 16:01:30 -0400431 return ret;
432}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400433
Chris Masone02119d2008-09-05 16:13:11 -0400434/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400435int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400436{
437 int ret;
438 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400439 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400440
Zheng Yan31840ae2008-09-23 13:14:14 -0400441 path = btrfs_alloc_path();
442 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400443 key.objectid = start;
444 key.offset = len;
445 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
446 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
447 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400448 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500449 return ret;
450}
451
Chris Masond8d5f3e2007-12-11 12:42:00 -0500452/*
453 * Back reference rules. Back refs have three main goals:
454 *
455 * 1) differentiate between all holders of references to an extent so that
456 * when a reference is dropped we can make sure it was a valid reference
457 * before freeing the extent.
458 *
459 * 2) Provide enough information to quickly find the holders of an extent
460 * if we notice a given block is corrupted or bad.
461 *
462 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
463 * maintenance. This is actually the same as #2, but with a slightly
464 * different use case.
465 *
466 * File extents can be referenced by:
467 *
468 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400469 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500470 * - different offsets inside a file (bookend extents in file.c)
471 *
472 * The extent ref structure has fields for:
473 *
474 * - Objectid of the subvolume root
475 * - Generation number of the tree holding the reference
476 * - objectid of the file holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400477 * - number of references holding by parent node (alway 1 for tree blocks)
478 *
479 * Btree leaf may hold multiple references to a file extent. In most cases,
480 * these references are from same file and the corresponding offsets inside
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400481 * the file are close together.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500482 *
483 * When a file extent is allocated the fields are filled in:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400484 * (root_key.objectid, trans->transid, inode objectid, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500485 *
486 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400487 * in the leaf. It looks similar to the create case, but trans->transid will
488 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500489 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400490 * (root_key.objectid, trans->transid, inode objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400491 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500492 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400493 * When a file extent is removed either during snapshot deletion or
494 * file truncation, we find the corresponding back reference and check
495 * the following fields:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500496 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400497 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
498 * inode objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500499 *
500 * Btree extents can be referenced by:
501 *
502 * - Different subvolumes
503 * - Different generations of the same subvolume
504 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500505 * When a tree block is created, back references are inserted:
506 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400507 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500508 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400509 * When a tree block is cow'd, new back references are added for all the
510 * blocks it points to. If the tree block isn't in reference counted root,
511 * the old back references are removed. These new back references are of
512 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500513 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400514 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500515 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400516 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500517 *
518 * if backref was for a tree root:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400519 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500520 * else
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400521 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500522 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400523 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500524 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400525 * The key objectid corresponds to the first byte in the extent, the key
526 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
527 * byte of parent extent. If a extent is tree root, the key offset is set
528 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500529 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400530
531static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
532 struct btrfs_root *root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400533 struct btrfs_path *path,
534 u64 bytenr, u64 parent,
535 u64 ref_root, u64 ref_generation,
536 u64 owner_objectid, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500537{
Chris Mason74493f72007-12-11 09:25:06 -0500538 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400539 struct btrfs_extent_ref *ref;
540 struct extent_buffer *leaf;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400541 u64 ref_objectid;
Chris Mason74493f72007-12-11 09:25:06 -0500542 int ret;
543
Chris Mason74493f72007-12-11 09:25:06 -0500544 key.objectid = bytenr;
545 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400546 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500547
Zheng Yan31840ae2008-09-23 13:14:14 -0400548 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
549 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500550 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400551 if (ret > 0) {
552 ret = -ENOENT;
553 goto out;
554 }
555
556 leaf = path->nodes[0];
557 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400558 ref_objectid = btrfs_ref_objectid(leaf, ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400559 if (btrfs_ref_root(leaf, ref) != ref_root ||
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400560 btrfs_ref_generation(leaf, ref) != ref_generation ||
561 (ref_objectid != owner_objectid &&
562 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400563 ret = -EIO;
564 WARN_ON(1);
565 goto out;
566 }
567 ret = 0;
568out:
569 return ret;
570}
571
Josef Bacikf3465ca2008-11-12 14:19:50 -0500572/*
573 * updates all the backrefs that are pending on update_list for the
574 * extent_root
575 */
576static int noinline update_backrefs(struct btrfs_trans_handle *trans,
577 struct btrfs_root *extent_root,
578 struct btrfs_path *path,
579 struct list_head *update_list)
580{
581 struct btrfs_key key;
582 struct btrfs_extent_ref *ref;
583 struct btrfs_fs_info *info = extent_root->fs_info;
584 struct pending_extent_op *op;
585 struct extent_buffer *leaf;
586 int ret = 0;
587 struct list_head *cur = update_list->next;
588 u64 ref_objectid;
589 u64 ref_root = extent_root->root_key.objectid;
590
591 op = list_entry(cur, struct pending_extent_op, list);
592
593search:
594 key.objectid = op->bytenr;
595 key.type = BTRFS_EXTENT_REF_KEY;
596 key.offset = op->orig_parent;
597
598 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
599 BUG_ON(ret);
600
601 leaf = path->nodes[0];
602
603loop:
604 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
605
606 ref_objectid = btrfs_ref_objectid(leaf, ref);
607
608 if (btrfs_ref_root(leaf, ref) != ref_root ||
609 btrfs_ref_generation(leaf, ref) != op->orig_generation ||
610 (ref_objectid != op->level &&
611 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
612 printk(KERN_ERR "couldn't find %Lu, parent %Lu, root %Lu, "
613 "owner %u\n", op->bytenr, op->orig_parent,
614 ref_root, op->level);
615 btrfs_print_leaf(extent_root, leaf);
616 BUG();
617 }
618
619 key.objectid = op->bytenr;
620 key.offset = op->parent;
621 key.type = BTRFS_EXTENT_REF_KEY;
622 ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
623 BUG_ON(ret);
624 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
625 btrfs_set_ref_generation(leaf, ref, op->generation);
626
627 cur = cur->next;
628
629 list_del_init(&op->list);
630 unlock_extent(&info->extent_ins, op->bytenr,
631 op->bytenr + op->num_bytes - 1, GFP_NOFS);
632 kfree(op);
633
634 if (cur == update_list) {
635 btrfs_mark_buffer_dirty(path->nodes[0]);
636 btrfs_release_path(extent_root, path);
637 goto out;
638 }
639
640 op = list_entry(cur, struct pending_extent_op, list);
641
642 path->slots[0]++;
643 while (path->slots[0] < btrfs_header_nritems(leaf)) {
644 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
645 if (key.objectid == op->bytenr &&
646 key.type == BTRFS_EXTENT_REF_KEY)
647 goto loop;
648 path->slots[0]++;
649 }
650
651 btrfs_mark_buffer_dirty(path->nodes[0]);
652 btrfs_release_path(extent_root, path);
653 goto search;
654
655out:
656 return 0;
657}
658
659static int noinline insert_extents(struct btrfs_trans_handle *trans,
660 struct btrfs_root *extent_root,
661 struct btrfs_path *path,
662 struct list_head *insert_list, int nr)
663{
664 struct btrfs_key *keys;
665 u32 *data_size;
666 struct pending_extent_op *op;
667 struct extent_buffer *leaf;
668 struct list_head *cur = insert_list->next;
669 struct btrfs_fs_info *info = extent_root->fs_info;
670 u64 ref_root = extent_root->root_key.objectid;
671 int i = 0, last = 0, ret;
672 int total = nr * 2;
673
674 if (!nr)
675 return 0;
676
677 keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
678 if (!keys)
679 return -ENOMEM;
680
681 data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
682 if (!data_size) {
683 kfree(keys);
684 return -ENOMEM;
685 }
686
687 list_for_each_entry(op, insert_list, list) {
688 keys[i].objectid = op->bytenr;
689 keys[i].offset = op->num_bytes;
690 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
691 data_size[i] = sizeof(struct btrfs_extent_item);
692 i++;
693
694 keys[i].objectid = op->bytenr;
695 keys[i].offset = op->parent;
696 keys[i].type = BTRFS_EXTENT_REF_KEY;
697 data_size[i] = sizeof(struct btrfs_extent_ref);
698 i++;
699 }
700
701 op = list_entry(cur, struct pending_extent_op, list);
702 i = 0;
703 while (i < total) {
704 int c;
705 ret = btrfs_insert_some_items(trans, extent_root, path,
706 keys+i, data_size+i, total-i);
707 BUG_ON(ret < 0);
708
709 if (last && ret > 1)
710 BUG();
711
712 leaf = path->nodes[0];
713 for (c = 0; c < ret; c++) {
714 int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
715
716 /*
717 * if the first item we inserted was a backref, then
718 * the EXTENT_ITEM will be the odd c's, else it will
719 * be the even c's
720 */
721 if ((ref_first && (c % 2)) ||
722 (!ref_first && !(c % 2))) {
723 struct btrfs_extent_item *itm;
724
725 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
726 struct btrfs_extent_item);
727 btrfs_set_extent_refs(path->nodes[0], itm, 1);
728 op->del++;
729 } else {
730 struct btrfs_extent_ref *ref;
731
732 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
733 struct btrfs_extent_ref);
734 btrfs_set_ref_root(leaf, ref, ref_root);
735 btrfs_set_ref_generation(leaf, ref,
736 op->generation);
737 btrfs_set_ref_objectid(leaf, ref, op->level);
738 btrfs_set_ref_num_refs(leaf, ref, 1);
739 op->del++;
740 }
741
742 /*
743 * using del to see when its ok to free up the
744 * pending_extent_op. In the case where we insert the
745 * last item on the list in order to help do batching
746 * we need to not free the extent op until we actually
747 * insert the extent_item
748 */
749 if (op->del == 2) {
750 unlock_extent(&info->extent_ins, op->bytenr,
751 op->bytenr + op->num_bytes - 1,
752 GFP_NOFS);
753 cur = cur->next;
754 list_del_init(&op->list);
755 kfree(op);
756 if (cur != insert_list)
757 op = list_entry(cur,
758 struct pending_extent_op,
759 list);
760 }
761 }
762 btrfs_mark_buffer_dirty(leaf);
763 btrfs_release_path(extent_root, path);
764
765 /*
766 * Ok backref's and items usually go right next to eachother,
767 * but if we could only insert 1 item that means that we
768 * inserted on the end of a leaf, and we have no idea what may
769 * be on the next leaf so we just play it safe. In order to
770 * try and help this case we insert the last thing on our
771 * insert list so hopefully it will end up being the last
772 * thing on the leaf and everything else will be before it,
773 * which will let us insert a whole bunch of items at the same
774 * time.
775 */
776 if (ret == 1 && !last && (i + ret < total)) {
777 /*
778 * last: where we will pick up the next time around
779 * i: our current key to insert, will be total - 1
780 * cur: the current op we are screwing with
781 * op: duh
782 */
783 last = i + ret;
784 i = total - 1;
785 cur = insert_list->prev;
786 op = list_entry(cur, struct pending_extent_op, list);
787 } else if (last) {
788 /*
789 * ok we successfully inserted the last item on the
790 * list, lets reset everything
791 *
792 * i: our current key to insert, so where we left off
793 * last time
794 * last: done with this
795 * cur: the op we are messing with
796 * op: duh
797 * total: since we inserted the last key, we need to
798 * decrement total so we dont overflow
799 */
800 i = last;
801 last = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -0500802 total--;
Liu Huib4eec2c2008-11-18 11:30:10 -0500803 if (i < total) {
804 cur = insert_list->next;
805 op = list_entry(cur, struct pending_extent_op,
806 list);
807 }
Josef Bacikf3465ca2008-11-12 14:19:50 -0500808 } else {
809 i += ret;
810 }
811
812 cond_resched();
813 }
814 ret = 0;
815 kfree(keys);
816 kfree(data_size);
817 return ret;
818}
819
Zheng Yan31840ae2008-09-23 13:14:14 -0400820static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
821 struct btrfs_root *root,
822 struct btrfs_path *path,
823 u64 bytenr, u64 parent,
824 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400825 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400826{
827 struct btrfs_key key;
828 struct extent_buffer *leaf;
829 struct btrfs_extent_ref *ref;
830 u32 num_refs;
831 int ret;
832
833 key.objectid = bytenr;
834 key.type = BTRFS_EXTENT_REF_KEY;
835 key.offset = parent;
836
837 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
838 if (ret == 0) {
839 leaf = path->nodes[0];
840 ref = btrfs_item_ptr(leaf, path->slots[0],
841 struct btrfs_extent_ref);
842 btrfs_set_ref_root(leaf, ref, ref_root);
843 btrfs_set_ref_generation(leaf, ref, ref_generation);
844 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400845 btrfs_set_ref_num_refs(leaf, ref, 1);
846 } else if (ret == -EEXIST) {
847 u64 existing_owner;
848 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
849 leaf = path->nodes[0];
850 ref = btrfs_item_ptr(leaf, path->slots[0],
851 struct btrfs_extent_ref);
852 if (btrfs_ref_root(leaf, ref) != ref_root ||
853 btrfs_ref_generation(leaf, ref) != ref_generation) {
854 ret = -EIO;
855 WARN_ON(1);
856 goto out;
857 }
858
859 num_refs = btrfs_ref_num_refs(leaf, ref);
860 BUG_ON(num_refs == 0);
861 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
862
863 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400864 if (existing_owner != owner_objectid &&
865 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400866 btrfs_set_ref_objectid(leaf, ref,
867 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400868 }
869 ret = 0;
870 } else {
871 goto out;
872 }
Chris Mason7bb86312007-12-11 09:25:06 -0500873 btrfs_mark_buffer_dirty(path->nodes[0]);
874out:
875 btrfs_release_path(root, path);
876 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500877}
878
Zheng Yan31840ae2008-09-23 13:14:14 -0400879static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
880 struct btrfs_root *root,
881 struct btrfs_path *path)
882{
883 struct extent_buffer *leaf;
884 struct btrfs_extent_ref *ref;
885 u32 num_refs;
886 int ret = 0;
887
888 leaf = path->nodes[0];
889 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
890 num_refs = btrfs_ref_num_refs(leaf, ref);
891 BUG_ON(num_refs == 0);
892 num_refs -= 1;
893 if (num_refs == 0) {
894 ret = btrfs_del_item(trans, root, path);
895 } else {
896 btrfs_set_ref_num_refs(leaf, ref, num_refs);
897 btrfs_mark_buffer_dirty(leaf);
898 }
899 btrfs_release_path(root, path);
900 return ret;
901}
902
Chris Mason15916de2008-11-19 21:17:22 -0500903static void btrfs_issue_discard(struct block_device *bdev,
904 u64 start, u64 len)
905{
906#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
907 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
908#else
909 blkdev_issue_discard(bdev, start >> 9, len >> 9);
910#endif
911}
912
913
Josef Bacikf3465ca2008-11-12 14:19:50 -0500914static int noinline free_extents(struct btrfs_trans_handle *trans,
915 struct btrfs_root *extent_root,
916 struct list_head *del_list)
917{
918 struct btrfs_fs_info *info = extent_root->fs_info;
919 struct btrfs_path *path;
920 struct btrfs_key key, found_key;
921 struct extent_buffer *leaf;
922 struct list_head *cur;
923 struct pending_extent_op *op;
924 struct btrfs_extent_item *ei;
925 int ret, num_to_del, extent_slot = 0, found_extent = 0;
926 u32 refs;
927 u64 bytes_freed = 0;
928
929 path = btrfs_alloc_path();
930 if (!path)
931 return -ENOMEM;
932 path->reada = 1;
933
934search:
935 /* search for the backref for the current ref we want to delete */
936 cur = del_list->next;
937 op = list_entry(cur, struct pending_extent_op, list);
938 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
939 op->orig_parent,
940 extent_root->root_key.objectid,
941 op->orig_generation, op->level, 1);
942 if (ret) {
943 printk("Unable to find backref byte nr %Lu root %Lu gen %Lu "
944 "owner %u\n", op->bytenr,
945 extent_root->root_key.objectid, op->orig_generation,
946 op->level);
947 btrfs_print_leaf(extent_root, path->nodes[0]);
948 WARN_ON(1);
949 goto out;
950 }
951
952 extent_slot = path->slots[0];
953 num_to_del = 1;
954 found_extent = 0;
955
956 /*
957 * if we aren't the first item on the leaf we can move back one and see
958 * if our ref is right next to our extent item
959 */
960 if (likely(extent_slot)) {
961 extent_slot--;
962 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
963 extent_slot);
964 if (found_key.objectid == op->bytenr &&
965 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
966 found_key.offset == op->num_bytes) {
967 num_to_del++;
968 found_extent = 1;
969 }
970 }
971
972 /*
973 * if we didn't find the extent we need to delete the backref and then
974 * search for the extent item key so we can update its ref count
975 */
976 if (!found_extent) {
977 key.objectid = op->bytenr;
978 key.type = BTRFS_EXTENT_ITEM_KEY;
979 key.offset = op->num_bytes;
980
981 ret = remove_extent_backref(trans, extent_root, path);
982 BUG_ON(ret);
983 btrfs_release_path(extent_root, path);
984 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
985 BUG_ON(ret);
986 extent_slot = path->slots[0];
987 }
988
989 /* this is where we update the ref count for the extent */
990 leaf = path->nodes[0];
991 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
992 refs = btrfs_extent_refs(leaf, ei);
993 BUG_ON(refs == 0);
994 refs--;
995 btrfs_set_extent_refs(leaf, ei, refs);
996
997 btrfs_mark_buffer_dirty(leaf);
998
999 /*
1000 * This extent needs deleting. The reason cur_slot is extent_slot +
1001 * num_to_del is because extent_slot points to the slot where the extent
1002 * is, and if the backref was not right next to the extent we will be
1003 * deleting at least 1 item, and will want to start searching at the
1004 * slot directly next to extent_slot. However if we did find the
1005 * backref next to the extent item them we will be deleting at least 2
1006 * items and will want to start searching directly after the ref slot
1007 */
1008 if (!refs) {
1009 struct list_head *pos, *n, *end;
1010 int cur_slot = extent_slot+num_to_del;
1011 u64 super_used;
1012 u64 root_used;
1013
1014 path->slots[0] = extent_slot;
1015 bytes_freed = op->num_bytes;
1016
Josef Bacike3e469f2008-11-17 21:11:49 -05001017 mutex_lock(&info->pinned_mutex);
1018 ret = pin_down_bytes(trans, extent_root, op->bytenr,
1019 op->num_bytes, op->level >=
1020 BTRFS_FIRST_FREE_OBJECTID);
1021 mutex_unlock(&info->pinned_mutex);
1022 BUG_ON(ret < 0);
1023 op->del = ret;
1024
Josef Bacikf3465ca2008-11-12 14:19:50 -05001025 /*
1026 * we need to see if we can delete multiple things at once, so
1027 * start looping through the list of extents we are wanting to
1028 * delete and see if their extent/backref's are right next to
1029 * eachother and the extents only have 1 ref
1030 */
1031 for (pos = cur->next; pos != del_list; pos = pos->next) {
1032 struct pending_extent_op *tmp;
1033
1034 tmp = list_entry(pos, struct pending_extent_op, list);
1035
1036 /* we only want to delete extent+ref at this stage */
1037 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1038 break;
1039
1040 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1041 if (found_key.objectid != tmp->bytenr ||
1042 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1043 found_key.offset != tmp->num_bytes)
1044 break;
1045
1046 /* check to make sure this extent only has one ref */
1047 ei = btrfs_item_ptr(leaf, cur_slot,
1048 struct btrfs_extent_item);
1049 if (btrfs_extent_refs(leaf, ei) != 1)
1050 break;
1051
1052 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1053 if (found_key.objectid != tmp->bytenr ||
1054 found_key.type != BTRFS_EXTENT_REF_KEY ||
1055 found_key.offset != tmp->orig_parent)
1056 break;
1057
1058 /*
1059 * the ref is right next to the extent, we can set the
1060 * ref count to 0 since we will delete them both now
1061 */
1062 btrfs_set_extent_refs(leaf, ei, 0);
1063
1064 /* pin down the bytes for this extent */
1065 mutex_lock(&info->pinned_mutex);
1066 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1067 tmp->num_bytes, tmp->level >=
1068 BTRFS_FIRST_FREE_OBJECTID);
1069 mutex_unlock(&info->pinned_mutex);
1070 BUG_ON(ret < 0);
1071
1072 /*
1073 * use the del field to tell if we need to go ahead and
1074 * free up the extent when we delete the item or not.
1075 */
1076 tmp->del = ret;
1077 bytes_freed += tmp->num_bytes;
1078
1079 num_to_del += 2;
1080 cur_slot += 2;
1081 }
1082 end = pos;
1083
1084 /* update the free space counters */
1085 spin_lock_irq(&info->delalloc_lock);
1086 super_used = btrfs_super_bytes_used(&info->super_copy);
1087 btrfs_set_super_bytes_used(&info->super_copy,
1088 super_used - bytes_freed);
1089 spin_unlock_irq(&info->delalloc_lock);
1090
1091 root_used = btrfs_root_used(&extent_root->root_item);
1092 btrfs_set_root_used(&extent_root->root_item,
1093 root_used - bytes_freed);
1094
1095 /* delete the items */
1096 ret = btrfs_del_items(trans, extent_root, path,
1097 path->slots[0], num_to_del);
1098 BUG_ON(ret);
1099
1100 /*
1101 * loop through the extents we deleted and do the cleanup work
1102 * on them
1103 */
1104 for (pos = cur, n = pos->next; pos != end;
1105 pos = n, n = pos->next) {
1106 struct pending_extent_op *tmp;
1107#ifdef BIO_RW_DISCARD
1108 u64 map_length;
1109 struct btrfs_multi_bio *multi = NULL;
1110#endif
1111 tmp = list_entry(pos, struct pending_extent_op, list);
1112
1113 /*
1114 * remember tmp->del tells us wether or not we pinned
1115 * down the extent
1116 */
1117 ret = update_block_group(trans, extent_root,
1118 tmp->bytenr, tmp->num_bytes, 0,
1119 tmp->del);
1120 BUG_ON(ret);
1121
1122#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -05001123 map_length = tmp->num_bytes;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001124 ret = btrfs_map_block(&info->mapping_tree, READ,
1125 tmp->bytenr, &map_length, &multi,
1126 0);
1127 if (!ret) {
1128 struct btrfs_bio_stripe *stripe;
1129 int i;
1130
Chris Mason15916de2008-11-19 21:17:22 -05001131 stripe = multi->stripes;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001132
1133 if (map_length > tmp->num_bytes)
1134 map_length = tmp->num_bytes;
1135
1136 for (i = 0; i < multi->num_stripes;
1137 i++, stripe++)
Chris Mason15916de2008-11-19 21:17:22 -05001138 btrfs_issue_discard(stripe->dev->bdev,
1139 stripe->physical,
1140 map_length);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001141 kfree(multi);
1142 }
1143#endif
1144 list_del_init(&tmp->list);
1145 unlock_extent(&info->extent_ins, tmp->bytenr,
1146 tmp->bytenr + tmp->num_bytes - 1,
1147 GFP_NOFS);
1148 kfree(tmp);
1149 }
1150 } else if (refs && found_extent) {
1151 /*
1152 * the ref and extent were right next to eachother, but the
1153 * extent still has a ref, so just free the backref and keep
1154 * going
1155 */
1156 ret = remove_extent_backref(trans, extent_root, path);
1157 BUG_ON(ret);
1158
1159 list_del_init(&op->list);
1160 unlock_extent(&info->extent_ins, op->bytenr,
1161 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1162 kfree(op);
1163 } else {
1164 /*
1165 * the extent has multiple refs and the backref we were looking
1166 * for was not right next to it, so just unlock and go next,
1167 * we're good to go
1168 */
1169 list_del_init(&op->list);
1170 unlock_extent(&info->extent_ins, op->bytenr,
1171 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1172 kfree(op);
1173 }
1174
1175 btrfs_release_path(extent_root, path);
1176 if (!list_empty(del_list))
1177 goto search;
1178
1179out:
1180 btrfs_free_path(path);
1181 return ret;
1182}
1183
Zheng Yan31840ae2008-09-23 13:14:14 -04001184static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1185 struct btrfs_root *root, u64 bytenr,
1186 u64 orig_parent, u64 parent,
1187 u64 orig_root, u64 ref_root,
1188 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001189 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001190{
1191 int ret;
1192 struct btrfs_root *extent_root = root->fs_info->extent_root;
1193 struct btrfs_path *path;
1194
1195 if (root == root->fs_info->extent_root) {
1196 struct pending_extent_op *extent_op;
1197 u64 num_bytes;
1198
1199 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1200 num_bytes = btrfs_level_size(root, (int)owner_objectid);
Josef Bacik25179202008-10-29 14:49:05 -04001201 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001202 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
Josef Bacik25179202008-10-29 14:49:05 -04001203 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001204 u64 priv;
1205 ret = get_state_private(&root->fs_info->extent_ins,
1206 bytenr, &priv);
1207 BUG_ON(ret);
1208 extent_op = (struct pending_extent_op *)
1209 (unsigned long)priv;
1210 BUG_ON(extent_op->parent != orig_parent);
1211 BUG_ON(extent_op->generation != orig_generation);
Josef Bacik25179202008-10-29 14:49:05 -04001212
Zheng Yan31840ae2008-09-23 13:14:14 -04001213 extent_op->parent = parent;
1214 extent_op->generation = ref_generation;
1215 } else {
1216 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1217 BUG_ON(!extent_op);
1218
1219 extent_op->type = PENDING_BACKREF_UPDATE;
1220 extent_op->bytenr = bytenr;
1221 extent_op->num_bytes = num_bytes;
1222 extent_op->parent = parent;
1223 extent_op->orig_parent = orig_parent;
1224 extent_op->generation = ref_generation;
1225 extent_op->orig_generation = orig_generation;
1226 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001227 INIT_LIST_HEAD(&extent_op->list);
1228 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001229
1230 set_extent_bits(&root->fs_info->extent_ins,
1231 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04001232 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04001233 set_state_private(&root->fs_info->extent_ins,
1234 bytenr, (unsigned long)extent_op);
1235 }
Josef Bacik25179202008-10-29 14:49:05 -04001236 mutex_unlock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001237 return 0;
1238 }
1239
1240 path = btrfs_alloc_path();
1241 if (!path)
1242 return -ENOMEM;
1243 ret = lookup_extent_backref(trans, extent_root, path,
1244 bytenr, orig_parent, orig_root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001245 orig_generation, owner_objectid, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001246 if (ret)
1247 goto out;
1248 ret = remove_extent_backref(trans, extent_root, path);
1249 if (ret)
1250 goto out;
1251 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1252 parent, ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001253 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001254 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001255 finish_current_insert(trans, extent_root, 0);
1256 del_pending_extents(trans, extent_root, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001257out:
1258 btrfs_free_path(path);
1259 return ret;
1260}
1261
1262int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1263 struct btrfs_root *root, u64 bytenr,
1264 u64 orig_parent, u64 parent,
1265 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001266 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001267{
1268 int ret;
1269 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1270 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1271 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001272 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1273 parent, ref_root, ref_root,
1274 ref_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001275 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001276 return ret;
1277}
1278
Chris Mason925baed2008-06-25 16:01:30 -04001279static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001280 struct btrfs_root *root, u64 bytenr,
1281 u64 orig_parent, u64 parent,
1282 u64 orig_root, u64 ref_root,
1283 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001284 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -05001285{
Chris Mason5caf2a02007-04-02 11:20:42 -04001286 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -05001287 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001288 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001289 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001290 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001291 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001292
Chris Mason5caf2a02007-04-02 11:20:42 -04001293 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001294 if (!path)
1295 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001296
Chris Mason3c12ac72008-04-21 12:01:38 -04001297 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001298 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001299 key.type = BTRFS_EXTENT_ITEM_KEY;
1300 key.offset = (u64)-1;
1301
Chris Mason5caf2a02007-04-02 11:20:42 -04001302 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001303 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001304 if (ret < 0)
1305 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001306 BUG_ON(ret == 0 || path->slots[0] == 0);
1307
1308 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -04001309 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -04001310
1311 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Mason771ed682008-11-06 22:02:51 -05001312 if (key.objectid != bytenr) {
1313 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
1314 printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
1315 BUG();
1316 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001317 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1318
Chris Mason5caf2a02007-04-02 11:20:42 -04001319 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001320 refs = btrfs_extent_refs(l, item);
1321 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001322 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -05001323
Chris Mason5caf2a02007-04-02 11:20:42 -04001324 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001325
Chris Mason3c12ac72008-04-21 12:01:38 -04001326 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001327 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1328 path, bytenr, parent,
1329 ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001330 owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05001331 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001332 finish_current_insert(trans, root->fs_info->extent_root, 0);
1333 del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Mason74493f72007-12-11 09:25:06 -05001334
1335 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -05001336 return 0;
1337}
1338
Chris Mason925baed2008-06-25 16:01:30 -04001339int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001340 struct btrfs_root *root,
1341 u64 bytenr, u64 num_bytes, u64 parent,
1342 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001343 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -04001344{
1345 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001346 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1347 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1348 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001349 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1350 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001351 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -04001352 return ret;
1353}
1354
Chris Masone9d0b132007-08-10 14:06:19 -04001355int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1356 struct btrfs_root *root)
1357{
Chris Mason87ef2bb2008-10-30 11:23:27 -04001358 finish_current_insert(trans, root->fs_info->extent_root, 1);
1359 del_pending_extents(trans, root->fs_info->extent_root, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001360 return 0;
1361}
1362
Zheng Yan31840ae2008-09-23 13:14:14 -04001363int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1364 struct btrfs_root *root, u64 bytenr,
1365 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -05001366{
Chris Mason5caf2a02007-04-02 11:20:42 -04001367 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -05001368 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001369 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001370 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001371 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -04001372
Chris Masondb945352007-10-15 16:15:53 -04001373 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -04001374 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -04001375 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001376 key.objectid = bytenr;
1377 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -04001378 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -04001379 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001380 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001381 if (ret < 0)
1382 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -04001383 if (ret != 0) {
1384 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -04001385 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -05001386 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04001387 }
1388 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001389 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001390 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001391out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001392 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05001393 return 0;
1394}
1395
Yan Zheng80ff3852008-10-30 14:20:02 -04001396int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1397 struct btrfs_root *root, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05001398{
1399 struct btrfs_root *extent_root = root->fs_info->extent_root;
1400 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -04001401 struct extent_buffer *leaf;
1402 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -05001403 struct btrfs_key key;
1404 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001405 u64 ref_root;
1406 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -04001407 u32 nritems;
1408 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001409
Chris Masonbe20aa92007-12-17 20:14:01 -05001410 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001411 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04001412 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05001413
Yan Zhengf321e492008-07-30 09:26:11 -04001414 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -05001415 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1416 if (ret < 0)
1417 goto out;
1418 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001419
1420 ret = -ENOENT;
1421 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04001422 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001423
Zheng Yan31840ae2008-09-23 13:14:14 -04001424 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04001425 leaf = path->nodes[0];
1426 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001427
1428 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001429 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05001430 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001431
Yan Zheng80ff3852008-10-30 14:20:02 -04001432 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05001433 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -04001434 leaf = path->nodes[0];
1435 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -05001436 if (path->slots[0] >= nritems) {
1437 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -04001438 if (ret < 0)
1439 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001440 if (ret == 0)
1441 continue;
1442 break;
1443 }
Yan Zhengf321e492008-07-30 09:26:11 -04001444 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001445 if (found_key.objectid != bytenr)
1446 break;
Chris Masonbd098352008-01-03 13:23:19 -05001447
Chris Masonbe20aa92007-12-17 20:14:01 -05001448 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1449 path->slots[0]++;
1450 continue;
1451 }
1452
Yan Zhengf321e492008-07-30 09:26:11 -04001453 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001454 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -04001455 ref_root = btrfs_ref_root(leaf, ref_item);
1456 if (ref_root != root->root_key.objectid &&
1457 ref_root != BTRFS_TREE_LOG_OBJECTID) {
1458 ret = 1;
1459 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -04001460 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001461 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1462 ret = 1;
1463 goto out;
1464 }
Yan Zhengf321e492008-07-30 09:26:11 -04001465
Chris Masonbe20aa92007-12-17 20:14:01 -05001466 path->slots[0]++;
1467 }
Yan Zhengf321e492008-07-30 09:26:11 -04001468 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001469out:
Yan Zhengf321e492008-07-30 09:26:11 -04001470 btrfs_free_path(path);
1471 return ret;
1472}
1473
Zheng Yan31840ae2008-09-23 13:14:14 -04001474int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1475 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001476{
Chris Mason5f39d392007-10-15 16:14:19 -04001477 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001478 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001479 u64 root_gen;
1480 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001481 int i;
Chris Masondb945352007-10-15 16:15:53 -04001482 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001483 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001484 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001485
Chris Mason3768f362007-03-13 16:47:54 -04001486 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001487 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001488
Zheng Yane4657682008-09-26 10:04:53 -04001489 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1490 shared = 0;
1491 root_gen = root->root_key.offset;
1492 } else {
1493 shared = 1;
1494 root_gen = trans->transid - 1;
1495 }
1496
Chris Masondb945352007-10-15 16:15:53 -04001497 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001498 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001499
Zheng Yan31840ae2008-09-23 13:14:14 -04001500 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001501 struct btrfs_leaf_ref *ref;
1502 struct btrfs_extent_info *info;
1503
Zheng Yan31840ae2008-09-23 13:14:14 -04001504 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001505 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001506 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001507 goto out;
1508 }
1509
Zheng Yane4657682008-09-26 10:04:53 -04001510 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001511 ref->bytenr = buf->start;
1512 ref->owner = btrfs_header_owner(buf);
1513 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001514 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001515 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001516
Zheng Yan31840ae2008-09-23 13:14:14 -04001517 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001518 u64 disk_bytenr;
1519 btrfs_item_key_to_cpu(buf, &key, i);
1520 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1521 continue;
1522 fi = btrfs_item_ptr(buf, i,
1523 struct btrfs_file_extent_item);
1524 if (btrfs_file_extent_type(buf, fi) ==
1525 BTRFS_FILE_EXTENT_INLINE)
1526 continue;
1527 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1528 if (disk_bytenr == 0)
1529 continue;
1530
1531 info->bytenr = disk_bytenr;
1532 info->num_bytes =
1533 btrfs_file_extent_disk_num_bytes(buf, fi);
1534 info->objectid = key.objectid;
1535 info->offset = key.offset;
1536 info++;
1537 }
1538
Zheng Yane4657682008-09-26 10:04:53 -04001539 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001540 if (ret == -EEXIST && shared) {
1541 struct btrfs_leaf_ref *old;
1542 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1543 BUG_ON(!old);
1544 btrfs_remove_leaf_ref(root, old);
1545 btrfs_free_leaf_ref(root, old);
1546 ret = btrfs_add_leaf_ref(root, ref, shared);
1547 }
Yan Zheng31153d82008-07-28 15:32:19 -04001548 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001549 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001550 }
1551out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001552 return ret;
1553}
1554
1555int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1556 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1557 u32 *nr_extents)
1558{
1559 u64 bytenr;
1560 u64 ref_root;
1561 u64 orig_root;
1562 u64 ref_generation;
1563 u64 orig_generation;
1564 u32 nritems;
1565 u32 nr_file_extents = 0;
1566 struct btrfs_key key;
1567 struct btrfs_file_extent_item *fi;
1568 int i;
1569 int level;
1570 int ret = 0;
1571 int faili = 0;
1572 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001573 u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001574
1575 ref_root = btrfs_header_owner(buf);
1576 ref_generation = btrfs_header_generation(buf);
1577 orig_root = btrfs_header_owner(orig_buf);
1578 orig_generation = btrfs_header_generation(orig_buf);
1579
1580 nritems = btrfs_header_nritems(buf);
1581 level = btrfs_header_level(buf);
1582
1583 if (root->ref_cows) {
1584 process_func = __btrfs_inc_extent_ref;
1585 } else {
1586 if (level == 0 &&
1587 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1588 goto out;
1589 if (level != 0 &&
1590 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1591 goto out;
1592 process_func = __btrfs_update_extent_ref;
1593 }
1594
1595 for (i = 0; i < nritems; i++) {
1596 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001597 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001598 btrfs_item_key_to_cpu(buf, &key, i);
1599 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001600 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001601 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001602 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001603 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001604 BTRFS_FILE_EXTENT_INLINE)
1605 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001606 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1607 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001608 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001609
1610 nr_file_extents++;
1611
Zheng Yan31840ae2008-09-23 13:14:14 -04001612 ret = process_func(trans, root, bytenr,
1613 orig_buf->start, buf->start,
1614 orig_root, ref_root,
1615 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001616 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001617
1618 if (ret) {
1619 faili = i;
1620 WARN_ON(1);
1621 goto fail;
1622 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001623 } else {
Chris Masondb945352007-10-15 16:15:53 -04001624 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001625 ret = process_func(trans, root, bytenr,
1626 orig_buf->start, buf->start,
1627 orig_root, ref_root,
1628 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001629 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001630 if (ret) {
1631 faili = i;
1632 WARN_ON(1);
1633 goto fail;
1634 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001635 }
1636 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001637out:
1638 if (nr_extents) {
1639 if (level == 0)
1640 *nr_extents = nr_file_extents;
1641 else
1642 *nr_extents = nritems;
1643 }
1644 return 0;
1645fail:
1646 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001647 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001648}
1649
Zheng Yan31840ae2008-09-23 13:14:14 -04001650int btrfs_update_ref(struct btrfs_trans_handle *trans,
1651 struct btrfs_root *root, struct extent_buffer *orig_buf,
1652 struct extent_buffer *buf, int start_slot, int nr)
1653
1654{
1655 u64 bytenr;
1656 u64 ref_root;
1657 u64 orig_root;
1658 u64 ref_generation;
1659 u64 orig_generation;
1660 struct btrfs_key key;
1661 struct btrfs_file_extent_item *fi;
1662 int i;
1663 int ret;
1664 int slot;
1665 int level;
1666
1667 BUG_ON(start_slot < 0);
1668 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1669
1670 ref_root = btrfs_header_owner(buf);
1671 ref_generation = btrfs_header_generation(buf);
1672 orig_root = btrfs_header_owner(orig_buf);
1673 orig_generation = btrfs_header_generation(orig_buf);
1674 level = btrfs_header_level(buf);
1675
1676 if (!root->ref_cows) {
1677 if (level == 0 &&
1678 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1679 return 0;
1680 if (level != 0 &&
1681 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1682 return 0;
1683 }
1684
1685 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1686 cond_resched();
1687 if (level == 0) {
1688 btrfs_item_key_to_cpu(buf, &key, slot);
1689 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1690 continue;
1691 fi = btrfs_item_ptr(buf, slot,
1692 struct btrfs_file_extent_item);
1693 if (btrfs_file_extent_type(buf, fi) ==
1694 BTRFS_FILE_EXTENT_INLINE)
1695 continue;
1696 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1697 if (bytenr == 0)
1698 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001699 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1700 orig_buf->start, buf->start,
1701 orig_root, ref_root,
1702 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001703 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001704 if (ret)
1705 goto fail;
1706 } else {
1707 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001708 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1709 orig_buf->start, buf->start,
1710 orig_root, ref_root,
1711 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001712 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001713 if (ret)
1714 goto fail;
1715 }
1716 }
1717 return 0;
1718fail:
1719 WARN_ON(1);
1720 return -1;
1721}
1722
Chris Mason9078a3e2007-04-26 16:46:15 -04001723static int write_one_cache_group(struct btrfs_trans_handle *trans,
1724 struct btrfs_root *root,
1725 struct btrfs_path *path,
1726 struct btrfs_block_group_cache *cache)
1727{
1728 int ret;
1729 int pending_ret;
1730 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001731 unsigned long bi;
1732 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001733
Chris Mason9078a3e2007-04-26 16:46:15 -04001734 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001735 if (ret < 0)
1736 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001737 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001738
1739 leaf = path->nodes[0];
1740 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1741 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1742 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001743 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001744fail:
Chris Mason87ef2bb2008-10-30 11:23:27 -04001745 finish_current_insert(trans, extent_root, 0);
1746 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001747 if (ret)
1748 return ret;
1749 if (pending_ret)
1750 return pending_ret;
1751 return 0;
1752
1753}
1754
Chris Mason96b51792007-10-15 16:15:19 -04001755int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1756 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001757{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001758 struct btrfs_block_group_cache *cache, *entry;
1759 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001760 int err = 0;
1761 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001762 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001763 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001764
1765 path = btrfs_alloc_path();
1766 if (!path)
1767 return -ENOMEM;
1768
1769 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001770 cache = NULL;
1771 spin_lock(&root->fs_info->block_group_cache_lock);
1772 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1773 n; n = rb_next(n)) {
1774 entry = rb_entry(n, struct btrfs_block_group_cache,
1775 cache_node);
1776 if (entry->dirty) {
1777 cache = entry;
1778 break;
1779 }
1780 }
1781 spin_unlock(&root->fs_info->block_group_cache_lock);
1782
1783 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001784 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001785
Zheng Yane8569812008-09-26 10:05:48 -04001786 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001787 last += cache->key.offset;
1788
Chris Mason96b51792007-10-15 16:15:19 -04001789 err = write_one_cache_group(trans, root,
1790 path, cache);
1791 /*
1792 * if we fail to write the cache group, we want
1793 * to keep it marked dirty in hopes that a later
1794 * write will work
1795 */
1796 if (err) {
1797 werr = err;
1798 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001799 }
1800 }
1801 btrfs_free_path(path);
1802 return werr;
1803}
1804
Chris Mason593060d2008-03-25 16:50:33 -04001805static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1806 u64 total_bytes, u64 bytes_used,
1807 struct btrfs_space_info **space_info)
1808{
1809 struct btrfs_space_info *found;
1810
1811 found = __find_space_info(info, flags);
1812 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001813 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001814 found->total_bytes += total_bytes;
1815 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001816 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001817 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001818 *space_info = found;
1819 return 0;
1820 }
Yan Zhengc146afa2008-11-12 14:34:12 -05001821 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04001822 if (!found)
1823 return -ENOMEM;
1824
1825 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001826 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001827 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001828 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001829 found->flags = flags;
1830 found->total_bytes = total_bytes;
1831 found->bytes_used = bytes_used;
1832 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001833 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05001834 found->bytes_readonly = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001835 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001836 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001837 *space_info = found;
1838 return 0;
1839}
1840
Chris Mason8790d502008-04-03 16:29:03 -04001841static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1842{
1843 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001844 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001845 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001846 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001847 if (extra_flags) {
1848 if (flags & BTRFS_BLOCK_GROUP_DATA)
1849 fs_info->avail_data_alloc_bits |= extra_flags;
1850 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1851 fs_info->avail_metadata_alloc_bits |= extra_flags;
1852 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1853 fs_info->avail_system_alloc_bits |= extra_flags;
1854 }
1855}
Chris Mason593060d2008-03-25 16:50:33 -04001856
Yan Zhengc146afa2008-11-12 14:34:12 -05001857static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1858{
1859 spin_lock(&cache->space_info->lock);
1860 spin_lock(&cache->lock);
1861 if (!cache->ro) {
1862 cache->space_info->bytes_readonly += cache->key.offset -
1863 btrfs_block_group_used(&cache->item);
1864 cache->ro = 1;
1865 }
1866 spin_unlock(&cache->lock);
1867 spin_unlock(&cache->space_info->lock);
1868}
1869
Yan Zheng2b820322008-11-17 21:11:30 -05001870u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001871{
Yan Zheng2b820322008-11-17 21:11:30 -05001872 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001873
1874 if (num_devices == 1)
1875 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1876 if (num_devices < 4)
1877 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1878
Chris Masonec44a352008-04-28 15:29:52 -04001879 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1880 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001881 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001882 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001883 }
Chris Masonec44a352008-04-28 15:29:52 -04001884
1885 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001886 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001887 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001888 }
Chris Masonec44a352008-04-28 15:29:52 -04001889
1890 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1891 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1892 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1893 (flags & BTRFS_BLOCK_GROUP_DUP)))
1894 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1895 return flags;
1896}
1897
Chris Mason6324fbf2008-03-24 15:01:59 -04001898static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1899 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001900 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001901{
1902 struct btrfs_space_info *space_info;
1903 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05001904 int ret = 0;
1905
1906 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001907
Yan Zheng2b820322008-11-17 21:11:30 -05001908 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001909
Chris Mason6324fbf2008-03-24 15:01:59 -04001910 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001911 if (!space_info) {
1912 ret = update_space_info(extent_root->fs_info, flags,
1913 0, 0, &space_info);
1914 BUG_ON(ret);
1915 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001916 BUG_ON(!space_info);
1917
Josef Bacik25179202008-10-29 14:49:05 -04001918 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001919 if (space_info->force_alloc) {
1920 force = 1;
1921 space_info->force_alloc = 0;
1922 }
Josef Bacik25179202008-10-29 14:49:05 -04001923 if (space_info->full) {
1924 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001925 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001926 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001927
Yan Zhengc146afa2008-11-12 14:34:12 -05001928 thresh = space_info->total_bytes - space_info->bytes_readonly;
1929 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001930 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001931 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001932 space_info->bytes_reserved + alloc_bytes) < thresh) {
1933 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001934 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001935 }
Josef Bacik25179202008-10-29 14:49:05 -04001936 spin_unlock(&space_info->lock);
1937
Yan Zheng2b820322008-11-17 21:11:30 -05001938 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik25179202008-10-29 14:49:05 -04001939 if (ret) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001940printk("space info full %Lu\n", flags);
1941 space_info->full = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001942 }
Chris Masona74a4b92008-06-25 16:01:31 -04001943out:
Yan Zhengc146afa2008-11-12 14:34:12 -05001944 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001945 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001946}
1947
Chris Mason9078a3e2007-04-26 16:46:15 -04001948static int update_block_group(struct btrfs_trans_handle *trans,
1949 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001950 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001951 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001952{
1953 struct btrfs_block_group_cache *cache;
1954 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001955 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001956 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001957 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001958
Chris Mason9078a3e2007-04-26 16:46:15 -04001959 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001960 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001961 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001962 return -1;
Chris Masondb945352007-10-15 16:15:53 -04001963 byte_in_group = bytenr - cache->key.objectid;
1964 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001965
Josef Bacik25179202008-10-29 14:49:05 -04001966 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001967 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001968 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001969 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001970 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001971 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001972 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001973 cache->space_info->bytes_used += num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001974 if (cache->ro) {
1975 cache->space_info->bytes_readonly -= num_bytes;
1976 WARN_ON(1);
1977 }
Chris Masonc286ac42008-07-22 23:06:41 -04001978 btrfs_set_block_group_used(&cache->item, old_val);
1979 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001980 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001981 } else {
Chris Masondb945352007-10-15 16:15:53 -04001982 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001983 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001984 if (cache->ro)
1985 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001986 btrfs_set_block_group_used(&cache->item, old_val);
1987 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001988 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001989 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001990 int ret;
1991 ret = btrfs_add_free_space(cache, bytenr,
1992 num_bytes);
1993 if (ret)
1994 return -1;
Chris Masone37c9e62007-05-09 20:13:14 -04001995 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001996 }
Chris Masondb945352007-10-15 16:15:53 -04001997 total -= num_bytes;
1998 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001999 }
2000 return 0;
2001}
Chris Mason6324fbf2008-03-24 15:01:59 -04002002
Chris Masona061fc82008-05-07 11:43:44 -04002003static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2004{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002005 struct btrfs_block_group_cache *cache;
2006
2007 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2008 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002009 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002010
2011 return cache->key.objectid;
Chris Masona061fc82008-05-07 11:43:44 -04002012}
2013
Chris Masone02119d2008-09-05 16:13:11 -04002014int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05002015 u64 bytenr, u64 num, int pin)
2016{
2017 u64 len;
2018 struct btrfs_block_group_cache *cache;
2019 struct btrfs_fs_info *fs_info = root->fs_info;
2020
Josef Bacik25179202008-10-29 14:49:05 -04002021 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05002022 if (pin) {
2023 set_extent_dirty(&fs_info->pinned_extents,
2024 bytenr, bytenr + num - 1, GFP_NOFS);
2025 } else {
2026 clear_extent_dirty(&fs_info->pinned_extents,
2027 bytenr, bytenr + num - 1, GFP_NOFS);
2028 }
2029 while (num > 0) {
2030 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002031 BUG_ON(!cache);
2032 len = min(num, cache->key.offset -
2033 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002034 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002035 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002036 spin_lock(&cache->lock);
2037 cache->pinned += len;
2038 cache->space_info->bytes_pinned += len;
2039 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002040 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002041 fs_info->total_pinned += len;
2042 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002043 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002044 spin_lock(&cache->lock);
2045 cache->pinned -= len;
2046 cache->space_info->bytes_pinned -= len;
2047 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002048 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002049 fs_info->total_pinned -= len;
Josef Bacik07103a32008-11-19 15:17:55 -05002050 if (cache->cached)
2051 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05002052 }
2053 bytenr += len;
2054 num -= len;
2055 }
2056 return 0;
2057}
Chris Mason9078a3e2007-04-26 16:46:15 -04002058
Zheng Yane8569812008-09-26 10:05:48 -04002059static int update_reserved_extents(struct btrfs_root *root,
2060 u64 bytenr, u64 num, int reserve)
2061{
2062 u64 len;
2063 struct btrfs_block_group_cache *cache;
2064 struct btrfs_fs_info *fs_info = root->fs_info;
2065
Zheng Yane8569812008-09-26 10:05:48 -04002066 while (num > 0) {
2067 cache = btrfs_lookup_block_group(fs_info, bytenr);
2068 BUG_ON(!cache);
2069 len = min(num, cache->key.offset -
2070 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04002071
2072 spin_lock(&cache->space_info->lock);
2073 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002074 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04002075 cache->reserved += len;
2076 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04002077 } else {
Zheng Yane8569812008-09-26 10:05:48 -04002078 cache->reserved -= len;
2079 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04002080 }
Josef Bacik25179202008-10-29 14:49:05 -04002081 spin_unlock(&cache->lock);
2082 spin_unlock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002083 bytenr += len;
2084 num -= len;
2085 }
2086 return 0;
2087}
2088
Chris Masond1310b22008-01-24 16:13:08 -05002089int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04002090{
Chris Masonccd467d2007-06-28 15:57:36 -04002091 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002092 u64 start;
2093 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002094 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04002095 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04002096
Josef Bacik25179202008-10-29 14:49:05 -04002097 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002098 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002099 ret = find_first_extent_bit(pinned_extents, last,
2100 &start, &end, EXTENT_DIRTY);
2101 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04002102 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04002103 set_extent_dirty(copy, start, end, GFP_NOFS);
2104 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04002105 }
Josef Bacik25179202008-10-29 14:49:05 -04002106 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002107 return 0;
2108}
2109
2110int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2111 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05002112 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05002113{
Chris Mason1a5bc162007-10-15 16:15:26 -04002114 u64 start;
2115 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002116 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002117
Josef Bacik25179202008-10-29 14:49:05 -04002118 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002119 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002120 ret = find_first_extent_bit(unpin, 0, &start, &end,
2121 EXTENT_DIRTY);
2122 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05002123 break;
Chris Masone02119d2008-09-05 16:13:11 -04002124 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04002125 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04002126 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04002127 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002128 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04002129 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002130 }
Chris Masona28ec192007-03-06 20:08:01 -05002131 }
Josef Bacik25179202008-10-29 14:49:05 -04002132 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002133 return 0;
2134}
2135
Chris Mason98ed5172008-01-03 10:01:48 -05002136static int finish_current_insert(struct btrfs_trans_handle *trans,
Chris Mason87ef2bb2008-10-30 11:23:27 -04002137 struct btrfs_root *extent_root, int all)
Chris Mason037e6392007-03-07 11:50:24 -05002138{
Chris Mason7bb86312007-12-11 09:25:06 -05002139 u64 start;
2140 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002141 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002142 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002143 u64 skipped = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002144 struct btrfs_fs_info *info = extent_root->fs_info;
2145 struct btrfs_path *path;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002146 struct pending_extent_op *extent_op, *tmp;
2147 struct list_head insert_list, update_list;
Chris Mason037e6392007-03-07 11:50:24 -05002148 int ret;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002149 int num_inserts = 0, max_inserts;
Chris Mason037e6392007-03-07 11:50:24 -05002150
Chris Mason7bb86312007-12-11 09:25:06 -05002151 path = btrfs_alloc_path();
Josef Bacikf3465ca2008-11-12 14:19:50 -05002152 INIT_LIST_HEAD(&insert_list);
2153 INIT_LIST_HEAD(&update_list);
Chris Mason037e6392007-03-07 11:50:24 -05002154
Josef Bacikf3465ca2008-11-12 14:19:50 -05002155 max_inserts = extent_root->leafsize /
2156 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2157 sizeof(struct btrfs_extent_ref) +
2158 sizeof(struct btrfs_extent_item));
2159again:
2160 mutex_lock(&info->extent_ins_mutex);
2161 while (1) {
Josef Bacik25179202008-10-29 14:49:05 -04002162 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2163 &end, EXTENT_WRITEBACK);
2164 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002165 if (skipped && all && !num_inserts) {
2166 skipped = 0;
Liu Huib4eec2c2008-11-18 11:30:10 -05002167 search = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002168 continue;
2169 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002170 mutex_unlock(&info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04002171 break;
Josef Bacik25179202008-10-29 14:49:05 -04002172 }
2173
2174 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2175 if (!ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002176 skipped = 1;
Chris Mason87ef2bb2008-10-30 11:23:27 -04002177 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002178 if (need_resched()) {
2179 mutex_unlock(&info->extent_ins_mutex);
2180 cond_resched();
2181 mutex_lock(&info->extent_ins_mutex);
2182 }
Josef Bacik25179202008-10-29 14:49:05 -04002183 continue;
2184 }
Chris Mason26b80032007-08-08 20:17:12 -04002185
Zheng Yan31840ae2008-09-23 13:14:14 -04002186 ret = get_state_private(&info->extent_ins, start, &priv);
2187 BUG_ON(ret);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002188 extent_op = (struct pending_extent_op *)(unsigned long) priv;
Josef Bacik25179202008-10-29 14:49:05 -04002189
Zheng Yan31840ae2008-09-23 13:14:14 -04002190 if (extent_op->type == PENDING_EXTENT_INSERT) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002191 num_inserts++;
2192 list_add_tail(&extent_op->list, &insert_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002193 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002194 if (num_inserts == max_inserts) {
2195 mutex_unlock(&info->extent_ins_mutex);
2196 break;
2197 }
2198 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2199 list_add_tail(&extent_op->list, &update_list);
2200 search = end + 1;
2201 } else {
2202 BUG();
2203 }
Chris Mason037e6392007-03-07 11:50:24 -05002204 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002205
2206 /*
Liu Huib4eec2c2008-11-18 11:30:10 -05002207 * process the update list, clear the writeback bit for it, and if
Josef Bacikf3465ca2008-11-12 14:19:50 -05002208 * somebody marked this thing for deletion then just unlock it and be
2209 * done, the free_extents will handle it
2210 */
2211 mutex_lock(&info->extent_ins_mutex);
2212 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2213 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2214 extent_op->bytenr + extent_op->num_bytes - 1,
2215 EXTENT_WRITEBACK, GFP_NOFS);
2216 if (extent_op->del) {
2217 list_del_init(&extent_op->list);
2218 unlock_extent(&info->extent_ins, extent_op->bytenr,
2219 extent_op->bytenr + extent_op->num_bytes
2220 - 1, GFP_NOFS);
2221 kfree(extent_op);
2222 }
2223 }
2224 mutex_unlock(&info->extent_ins_mutex);
2225
2226 /*
2227 * still have things left on the update list, go ahead an update
2228 * everything
2229 */
2230 if (!list_empty(&update_list)) {
2231 ret = update_backrefs(trans, extent_root, path, &update_list);
2232 BUG_ON(ret);
2233 }
2234
2235 /*
2236 * if no inserts need to be done, but we skipped some extents and we
2237 * need to make sure everything is cleaned then reset everything and
2238 * go back to the beginning
2239 */
2240 if (!num_inserts && all && skipped) {
2241 search = 0;
2242 skipped = 0;
2243 INIT_LIST_HEAD(&update_list);
2244 INIT_LIST_HEAD(&insert_list);
2245 goto again;
2246 } else if (!num_inserts) {
2247 goto out;
2248 }
2249
2250 /*
2251 * process the insert extents list. Again if we are deleting this
2252 * extent, then just unlock it, pin down the bytes if need be, and be
2253 * done with it. Saves us from having to actually insert the extent
2254 * into the tree and then subsequently come along and delete it
2255 */
2256 mutex_lock(&info->extent_ins_mutex);
2257 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2258 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2259 extent_op->bytenr + extent_op->num_bytes - 1,
2260 EXTENT_WRITEBACK, GFP_NOFS);
2261 if (extent_op->del) {
2262 list_del_init(&extent_op->list);
2263 unlock_extent(&info->extent_ins, extent_op->bytenr,
2264 extent_op->bytenr + extent_op->num_bytes
2265 - 1, GFP_NOFS);
2266
2267 mutex_lock(&extent_root->fs_info->pinned_mutex);
2268 ret = pin_down_bytes(trans, extent_root,
2269 extent_op->bytenr,
2270 extent_op->num_bytes, 0);
2271 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2272
2273 ret = update_block_group(trans, extent_root,
2274 extent_op->bytenr,
2275 extent_op->num_bytes,
2276 0, ret > 0);
2277 BUG_ON(ret);
2278 kfree(extent_op);
2279 num_inserts--;
2280 }
2281 }
2282 mutex_unlock(&info->extent_ins_mutex);
2283
2284 ret = insert_extents(trans, extent_root, path, &insert_list,
2285 num_inserts);
2286 BUG_ON(ret);
2287
2288 /*
2289 * if we broke out of the loop in order to insert stuff because we hit
2290 * the maximum number of inserts at a time we can handle, then loop
2291 * back and pick up where we left off
2292 */
2293 if (num_inserts == max_inserts) {
2294 INIT_LIST_HEAD(&insert_list);
2295 INIT_LIST_HEAD(&update_list);
2296 num_inserts = 0;
2297 goto again;
2298 }
2299
2300 /*
2301 * again, if we need to make absolutely sure there are no more pending
2302 * extent operations left and we know that we skipped some, go back to
2303 * the beginning and do it all again
2304 */
2305 if (all && skipped) {
2306 INIT_LIST_HEAD(&insert_list);
2307 INIT_LIST_HEAD(&update_list);
2308 search = 0;
2309 skipped = 0;
2310 num_inserts = 0;
2311 goto again;
2312 }
2313out:
Chris Mason7bb86312007-12-11 09:25:06 -05002314 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05002315 return 0;
2316}
2317
Zheng Yan31840ae2008-09-23 13:14:14 -04002318static int pin_down_bytes(struct btrfs_trans_handle *trans,
2319 struct btrfs_root *root,
2320 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04002321{
Chris Mason1a5bc162007-10-15 16:15:26 -04002322 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002323 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04002324
Zheng Yan31840ae2008-09-23 13:14:14 -04002325 if (is_data)
2326 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002327
Zheng Yan31840ae2008-09-23 13:14:14 -04002328 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2329 if (!buf)
2330 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002331
Zheng Yan31840ae2008-09-23 13:14:14 -04002332 /* we can reuse a block if it hasn't been written
2333 * and it is from this transaction. We can't
2334 * reuse anything from the tree log root because
2335 * it has tiny sub-transactions.
2336 */
2337 if (btrfs_buffer_uptodate(buf, 0) &&
2338 btrfs_try_tree_lock(buf)) {
2339 u64 header_owner = btrfs_header_owner(buf);
2340 u64 header_transid = btrfs_header_generation(buf);
2341 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04002342 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04002343 header_transid == trans->transid &&
2344 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2345 clean_tree_block(NULL, root, buf);
2346 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002347 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002348 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04002349 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002350 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04002351 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002352 free_extent_buffer(buf);
2353pinit:
2354 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2355
Chris Masonbe744172007-05-06 10:15:01 -04002356 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002357 return 0;
2358}
2359
Chris Masona28ec192007-03-06 20:08:01 -05002360/*
2361 * remove an extent from the root, returns 0 on success
2362 */
Zheng Yan31840ae2008-09-23 13:14:14 -04002363static int __free_extent(struct btrfs_trans_handle *trans,
2364 struct btrfs_root *root,
2365 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002366 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002367 u64 owner_objectid, int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05002368{
Chris Mason5caf2a02007-04-02 11:20:42 -04002369 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04002370 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04002371 struct btrfs_fs_info *info = root->fs_info;
2372 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002373 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05002374 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05002375 int extent_slot = 0;
2376 int found_extent = 0;
2377 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04002378 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04002379 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05002380
Chris Masondb945352007-10-15 16:15:53 -04002381 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04002382 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04002383 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04002384 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002385 if (!path)
2386 return -ENOMEM;
2387
Chris Mason3c12ac72008-04-21 12:01:38 -04002388 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002389 ret = lookup_extent_backref(trans, extent_root, path,
2390 bytenr, parent, root_objectid,
2391 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05002392 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002393 struct btrfs_key found_key;
2394 extent_slot = path->slots[0];
2395 while(extent_slot > 0) {
2396 extent_slot--;
2397 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2398 extent_slot);
2399 if (found_key.objectid != bytenr)
2400 break;
2401 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2402 found_key.offset == num_bytes) {
2403 found_extent = 1;
2404 break;
2405 }
2406 if (path->slots[0] - extent_slot > 5)
2407 break;
2408 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002409 if (!found_extent) {
2410 ret = remove_extent_backref(trans, extent_root, path);
2411 BUG_ON(ret);
2412 btrfs_release_path(extent_root, path);
2413 ret = btrfs_search_slot(trans, extent_root,
2414 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002415 if (ret) {
2416 printk(KERN_ERR "umm, got %d back from search"
2417 ", was looking for %Lu\n", ret,
2418 bytenr);
2419 btrfs_print_leaf(extent_root, path->nodes[0]);
2420 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002421 BUG_ON(ret);
2422 extent_slot = path->slots[0];
2423 }
Chris Mason7bb86312007-12-11 09:25:06 -05002424 } else {
2425 btrfs_print_leaf(extent_root, path->nodes[0]);
2426 WARN_ON(1);
2427 printk("Unable to find ref byte nr %Lu root %Lu "
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002428 "gen %Lu owner %Lu\n", bytenr,
2429 root_objectid, ref_generation, owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05002430 }
Chris Mason5f39d392007-10-15 16:14:19 -04002431
2432 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05002433 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04002434 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002435 refs = btrfs_extent_refs(leaf, ei);
2436 BUG_ON(refs == 0);
2437 refs -= 1;
2438 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05002439
Chris Mason5f39d392007-10-15 16:14:19 -04002440 btrfs_mark_buffer_dirty(leaf);
2441
Chris Mason952fcca2008-02-18 16:33:44 -05002442 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002443 struct btrfs_extent_ref *ref;
2444 ref = btrfs_item_ptr(leaf, path->slots[0],
2445 struct btrfs_extent_ref);
2446 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002447 /* if the back ref and the extent are next to each other
2448 * they get deleted below in one shot
2449 */
2450 path->slots[0] = extent_slot;
2451 num_to_del = 2;
2452 } else if (found_extent) {
2453 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002454 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05002455 BUG_ON(ret);
2456 /* if refs are 0, we need to setup the path for deletion */
2457 if (refs == 0) {
2458 btrfs_release_path(extent_root, path);
2459 ret = btrfs_search_slot(trans, extent_root, &key, path,
2460 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002461 BUG_ON(ret);
2462 }
2463 }
2464
Chris Masoncf27e1e2007-03-13 09:49:06 -04002465 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002466 u64 super_used;
2467 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01002468#ifdef BIO_RW_DISCARD
2469 u64 map_length = num_bytes;
2470 struct btrfs_multi_bio *multi = NULL;
2471#endif
Chris Mason78fae272007-03-25 11:35:08 -04002472
2473 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002474 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002475 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2476 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04002477 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05002478 if (ret > 0)
2479 mark_free = 1;
2480 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04002481 }
2482
Josef Bacik58176a92007-08-29 15:47:34 -04002483 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002484 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002485 super_used = btrfs_super_bytes_used(&info->super_copy);
2486 btrfs_set_super_bytes_used(&info->super_copy,
2487 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002488 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04002489
2490 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002491 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002492 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04002493 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05002494 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2495 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04002496 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04002497 btrfs_release_path(extent_root, path);
Chris Masondb945352007-10-15 16:15:53 -04002498 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002499 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04002500 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01002501
2502#ifdef BIO_RW_DISCARD
2503 /* Tell the block device(s) that the sectors can be discarded */
2504 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2505 bytenr, &map_length, &multi, 0);
2506 if (!ret) {
2507 struct btrfs_bio_stripe *stripe = multi->stripes;
2508 int i;
2509
2510 if (map_length > num_bytes)
2511 map_length = num_bytes;
2512
2513 for (i = 0; i < multi->num_stripes; i++, stripe++) {
Chris Mason15916de2008-11-19 21:17:22 -05002514 btrfs_issue_discard(stripe->dev->bdev,
2515 stripe->physical,
2516 map_length);
David Woodhouse21af8042008-08-12 14:13:26 +01002517 }
2518 kfree(multi);
2519 }
2520#endif
Chris Masona28ec192007-03-06 20:08:01 -05002521 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002522 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002523 finish_current_insert(trans, extent_root, 0);
Chris Masona28ec192007-03-06 20:08:01 -05002524 return ret;
2525}
2526
2527/*
Chris Masonfec577f2007-02-26 10:40:21 -05002528 * find all the blocks marked as pending in the radix tree and remove
2529 * them from the extent map
2530 */
Chris Masone089f052007-03-16 16:20:31 -04002531static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -04002532 btrfs_root *extent_root, int all)
Chris Masonfec577f2007-02-26 10:40:21 -05002533{
2534 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002535 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002536 u64 start;
2537 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002538 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002539 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002540 int nr = 0, skipped = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002541 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002542 struct extent_io_tree *extent_ins;
2543 struct pending_extent_op *extent_op;
Josef Bacik25179202008-10-29 14:49:05 -04002544 struct btrfs_fs_info *info = extent_root->fs_info;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002545 struct list_head delete_list;
Chris Mason8ef97622007-03-26 10:15:30 -04002546
Josef Bacikf3465ca2008-11-12 14:19:50 -05002547 INIT_LIST_HEAD(&delete_list);
Zheng Yan31840ae2008-09-23 13:14:14 -04002548 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc162007-10-15 16:15:26 -04002549 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002550
Josef Bacikf3465ca2008-11-12 14:19:50 -05002551again:
2552 mutex_lock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002553 while(1) {
Josef Bacik25179202008-10-29 14:49:05 -04002554 ret = find_first_extent_bit(pending_del, search, &start, &end,
2555 EXTENT_WRITEBACK);
2556 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002557 if (all && skipped && !nr) {
Josef Bacik25179202008-10-29 14:49:05 -04002558 search = 0;
2559 continue;
2560 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002561 mutex_unlock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002562 break;
Josef Bacik25179202008-10-29 14:49:05 -04002563 }
2564
2565 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2566 if (!ret) {
2567 search = end+1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002568 skipped = 1;
2569
2570 if (need_resched()) {
2571 mutex_unlock(&info->extent_ins_mutex);
2572 cond_resched();
2573 mutex_lock(&info->extent_ins_mutex);
2574 }
2575
Josef Bacik25179202008-10-29 14:49:05 -04002576 continue;
2577 }
2578 BUG_ON(ret < 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002579
2580 ret = get_state_private(pending_del, start, &priv);
2581 BUG_ON(ret);
2582 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2583
Josef Bacik25179202008-10-29 14:49:05 -04002584 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
Chris Mason1a5bc162007-10-15 16:15:26 -04002585 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002586 if (!test_range_bit(extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04002587 EXTENT_WRITEBACK, 0)) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002588 list_add_tail(&extent_op->list, &delete_list);
2589 nr++;
Chris Masonc286ac42008-07-22 23:06:41 -04002590 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002591 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002592
2593 ret = get_state_private(&info->extent_ins, start,
2594 &priv);
Zheng Yan31840ae2008-09-23 13:14:14 -04002595 BUG_ON(ret);
2596 extent_op = (struct pending_extent_op *)
Josef Bacik25179202008-10-29 14:49:05 -04002597 (unsigned long)priv;
Zheng Yan31840ae2008-09-23 13:14:14 -04002598
Josef Bacik25179202008-10-29 14:49:05 -04002599 clear_extent_bits(&info->extent_ins, start, end,
2600 EXTENT_WRITEBACK, GFP_NOFS);
2601
Josef Bacikf3465ca2008-11-12 14:19:50 -05002602 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2603 list_add_tail(&extent_op->list, &delete_list);
2604 search = end + 1;
2605 nr++;
2606 continue;
2607 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002608
Josef Bacik25179202008-10-29 14:49:05 -04002609 mutex_lock(&extent_root->fs_info->pinned_mutex);
2610 ret = pin_down_bytes(trans, extent_root, start,
2611 end + 1 - start, 0);
2612 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2613
Zheng Yan31840ae2008-09-23 13:14:14 -04002614 ret = update_block_group(trans, extent_root, start,
Josef Bacik25179202008-10-29 14:49:05 -04002615 end + 1 - start, 0, ret > 0);
2616
Josef Bacikf3465ca2008-11-12 14:19:50 -05002617 unlock_extent(extent_ins, start, end, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002618 BUG_ON(ret);
2619 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002620 }
Chris Mason1a5bc162007-10-15 16:15:26 -04002621 if (ret)
2622 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04002623
Josef Bacikf3465ca2008-11-12 14:19:50 -05002624 search = end + 1;
2625
2626 if (need_resched()) {
2627 mutex_unlock(&info->extent_ins_mutex);
2628 cond_resched();
2629 mutex_lock(&info->extent_ins_mutex);
2630 }
Chris Masonfec577f2007-02-26 10:40:21 -05002631 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002632
2633 if (nr) {
2634 ret = free_extents(trans, extent_root, &delete_list);
2635 BUG_ON(ret);
2636 }
2637
2638 if (all && skipped) {
2639 INIT_LIST_HEAD(&delete_list);
2640 search = 0;
2641 nr = 0;
2642 goto again;
2643 }
2644
Chris Masone20d96d2007-03-22 12:13:20 -04002645 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002646}
2647
2648/*
2649 * remove an extent from the root, returns 0 on success
2650 */
Chris Mason925baed2008-06-25 16:01:30 -04002651static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002652 struct btrfs_root *root,
2653 u64 bytenr, u64 num_bytes, u64 parent,
2654 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002655 u64 owner_objectid, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002656{
Chris Mason9f5fae22007-03-20 14:38:32 -04002657 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002658 int pending_ret;
2659 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002660
Chris Masondb945352007-10-15 16:15:53 -04002661 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002662 if (root == extent_root) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002663 struct pending_extent_op *extent_op = NULL;
2664
2665 mutex_lock(&root->fs_info->extent_ins_mutex);
2666 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2667 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2668 u64 priv;
2669 ret = get_state_private(&root->fs_info->extent_ins,
2670 bytenr, &priv);
2671 BUG_ON(ret);
2672 extent_op = (struct pending_extent_op *)
2673 (unsigned long)priv;
2674
2675 extent_op->del = 1;
2676 if (extent_op->type == PENDING_EXTENT_INSERT) {
2677 mutex_unlock(&root->fs_info->extent_ins_mutex);
2678 return 0;
2679 }
2680 }
2681
2682 if (extent_op) {
2683 ref_generation = extent_op->orig_generation;
2684 parent = extent_op->orig_parent;
2685 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002686
2687 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2688 BUG_ON(!extent_op);
2689
2690 extent_op->type = PENDING_EXTENT_DELETE;
2691 extent_op->bytenr = bytenr;
2692 extent_op->num_bytes = num_bytes;
2693 extent_op->parent = parent;
2694 extent_op->orig_parent = parent;
2695 extent_op->generation = ref_generation;
2696 extent_op->orig_generation = ref_generation;
2697 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002698 INIT_LIST_HEAD(&extent_op->list);
2699 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002700
2701 set_extent_bits(&root->fs_info->pending_del,
2702 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04002703 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002704 set_state_private(&root->fs_info->pending_del,
2705 bytenr, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002706 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002707 return 0;
2708 }
Chris Mason4bef0842008-09-08 11:18:08 -04002709 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002710 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2711 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002712 struct btrfs_block_group_cache *cache;
2713
Chris Masond00aff02008-09-11 15:54:42 -04002714 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002715 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2716 BUG_ON(!cache);
2717 btrfs_add_free_space(cache, bytenr, num_bytes);
Zheng Yane8569812008-09-26 10:05:48 -04002718 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002719 return 0;
2720 }
Chris Mason4bef0842008-09-08 11:18:08 -04002721 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002722 }
Chris Mason4bef0842008-09-08 11:18:08 -04002723
2724 /* if data pin when any transaction has committed this */
2725 if (ref_generation != trans->transid)
2726 pin = 1;
2727
Zheng Yan31840ae2008-09-23 13:14:14 -04002728 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002729 root_objectid, ref_generation,
2730 owner_objectid, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002731
Chris Mason87ef2bb2008-10-30 11:23:27 -04002732 finish_current_insert(trans, root->fs_info->extent_root, 0);
2733 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002734 return ret ? ret : pending_ret;
2735}
2736
Chris Mason925baed2008-06-25 16:01:30 -04002737int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002738 struct btrfs_root *root,
2739 u64 bytenr, u64 num_bytes, u64 parent,
2740 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002741 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002742{
2743 int ret;
2744
Zheng Yan31840ae2008-09-23 13:14:14 -04002745 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002746 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002747 owner_objectid, pin);
Chris Mason925baed2008-06-25 16:01:30 -04002748 return ret;
2749}
2750
Chris Mason87ee04e2007-11-30 11:30:34 -05002751static u64 stripe_align(struct btrfs_root *root, u64 val)
2752{
2753 u64 mask = ((u64)root->stripesize - 1);
2754 u64 ret = (val + mask) & ~mask;
2755 return ret;
2756}
2757
Chris Masonfec577f2007-02-26 10:40:21 -05002758/*
2759 * walks the btree of allocated extents and find a hole of a given size.
2760 * The key ins is changed to record the hole:
2761 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002762 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002763 * ins->offset == number of blocks
2764 * Any available blocks before search_start are skipped.
2765 */
Chris Mason98ed5172008-01-03 10:01:48 -05002766static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2767 struct btrfs_root *orig_root,
2768 u64 num_bytes, u64 empty_size,
2769 u64 search_start, u64 search_end,
2770 u64 hint_byte, struct btrfs_key *ins,
2771 u64 exclude_start, u64 exclude_nr,
2772 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002773{
Josef Bacik80eb2342008-10-29 14:49:05 -04002774 int ret = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -04002775 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002776 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002777 u64 *last_ptr = NULL;
Chris Mason43662112008-11-07 09:06:11 -05002778 u64 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002779 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002780 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002781 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002782 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002783 struct list_head *head = NULL, *cur = NULL;
2784 int loop = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002785 int extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002786 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002787
Chris Masondb945352007-10-15 16:15:53 -04002788 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002789 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002790 ins->objectid = 0;
2791 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002792
Chris Mason0ef3e662008-05-24 14:04:53 -04002793 if (orig_root->ref_cows || empty_size)
2794 allowed_chunk_alloc = 1;
2795
Chris Mason239b14b2008-03-24 15:02:07 -04002796 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2797 last_ptr = &root->fs_info->last_alloc;
Chris Mason43662112008-11-07 09:06:11 -05002798 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002799 }
2800
Josef Bacik0f9dd462008-09-23 13:14:11 -04002801 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002802 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002803
Chris Mason239b14b2008-03-24 15:02:07 -04002804 if (last_ptr) {
Chris Mason43662112008-11-07 09:06:11 -05002805 if (*last_ptr) {
Chris Mason239b14b2008-03-24 15:02:07 -04002806 hint_byte = *last_ptr;
Chris Mason43662112008-11-07 09:06:11 -05002807 last_wanted = *last_ptr;
2808 } else
Chris Mason239b14b2008-03-24 15:02:07 -04002809 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002810 } else {
2811 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002812 }
Chris Masona061fc82008-05-07 11:43:44 -04002813 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002814 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04002815
Chris Mason42e70e72008-11-07 18:17:11 -05002816 if (last_wanted && search_start != last_wanted) {
Chris Mason43662112008-11-07 09:06:11 -05002817 last_wanted = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002818 empty_size += empty_cluster;
2819 }
Chris Mason43662112008-11-07 09:06:11 -05002820
Chris Mason42e70e72008-11-07 18:17:11 -05002821 total_needed += empty_size;
Josef Bacik80eb2342008-10-29 14:49:05 -04002822 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
Yan Zhengd899e052008-10-30 14:25:28 -04002823 if (!block_group)
2824 block_group = btrfs_lookup_first_block_group(root->fs_info,
2825 search_start);
Josef Bacik80eb2342008-10-29 14:49:05 -04002826 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002827
Josef Bacik80eb2342008-10-29 14:49:05 -04002828 down_read(&space_info->groups_sem);
2829 while (1) {
2830 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002831 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002832 * the only way this happens if our hint points to a block
2833 * group thats not of the proper type, while looping this
2834 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002835 */
Chris Mason8a1413a22008-11-10 16:13:54 -05002836 if (empty_size)
2837 extra_loop = 1;
2838
Chris Mason42e70e72008-11-07 18:17:11 -05002839 if (!block_group)
2840 goto new_group_no_lock;
2841
Josef Bacik25179202008-10-29 14:49:05 -04002842 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002843 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002844 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002845
Josef Bacik80eb2342008-10-29 14:49:05 -04002846 ret = cache_block_group(root, block_group);
Josef Bacik25179202008-10-29 14:49:05 -04002847 if (ret) {
2848 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002849 break;
Josef Bacik25179202008-10-29 14:49:05 -04002850 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002851
Josef Bacik80eb2342008-10-29 14:49:05 -04002852 if (block_group->ro)
2853 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002854
Josef Bacik80eb2342008-10-29 14:49:05 -04002855 free_space = btrfs_find_free_space(block_group, search_start,
2856 total_needed);
2857 if (free_space) {
2858 u64 start = block_group->key.objectid;
2859 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002860 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002861
Josef Bacik80eb2342008-10-29 14:49:05 -04002862 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002863
Josef Bacik80eb2342008-10-29 14:49:05 -04002864 /* move on to the next group */
2865 if (search_start + num_bytes >= search_end)
2866 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002867
Josef Bacik80eb2342008-10-29 14:49:05 -04002868 /* move on to the next group */
2869 if (search_start + num_bytes > end)
2870 goto new_group;
2871
Chris Mason43662112008-11-07 09:06:11 -05002872 if (last_wanted && search_start != last_wanted) {
Chris Mason3b7885b2008-11-06 21:48:27 -05002873 total_needed += empty_cluster;
Chris Mason8a1413a22008-11-10 16:13:54 -05002874 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002875 last_wanted = 0;
Chris Mason3b7885b2008-11-06 21:48:27 -05002876 /*
2877 * if search_start is still in this block group
2878 * then we just re-search this block group
2879 */
2880 if (search_start >= start &&
2881 search_start < end) {
2882 mutex_unlock(&block_group->alloc_mutex);
2883 continue;
2884 }
2885
2886 /* else we go to the next block group */
2887 goto new_group;
2888 }
2889
Josef Bacik80eb2342008-10-29 14:49:05 -04002890 if (exclude_nr > 0 &&
2891 (search_start + num_bytes > exclude_start &&
2892 search_start < exclude_start + exclude_nr)) {
2893 search_start = exclude_start + exclude_nr;
2894 /*
2895 * if search_start is still in this block group
2896 * then we just re-search this block group
2897 */
2898 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002899 search_start < end) {
2900 mutex_unlock(&block_group->alloc_mutex);
Chris Mason43662112008-11-07 09:06:11 -05002901 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002902 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002903 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002904
2905 /* else we go to the next block group */
2906 goto new_group;
2907 }
2908
2909 ins->objectid = search_start;
2910 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002911
2912 btrfs_remove_free_space_lock(block_group, search_start,
2913 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002914 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002915 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002916 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002917 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002918new_group:
Chris Mason42e70e72008-11-07 18:17:11 -05002919 mutex_unlock(&block_group->alloc_mutex);
2920new_group_no_lock:
Chris Masonf5a31e12008-11-10 11:47:09 -05002921 /* don't try to compare new allocations against the
2922 * last allocation any more
2923 */
Chris Mason43662112008-11-07 09:06:11 -05002924 last_wanted = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002925
Josef Bacik80eb2342008-10-29 14:49:05 -04002926 /*
2927 * Here's how this works.
2928 * loop == 0: we were searching a block group via a hint
2929 * and didn't find anything, so we start at
2930 * the head of the block groups and keep searching
2931 * loop == 1: we're searching through all of the block groups
2932 * if we hit the head again we have searched
2933 * all of the block groups for this space and we
2934 * need to try and allocate, if we cant error out.
2935 * loop == 2: we allocated more space and are looping through
2936 * all of the block groups again.
2937 */
2938 if (loop == 0) {
2939 head = &space_info->block_groups;
2940 cur = head->next;
Josef Bacik80eb2342008-10-29 14:49:05 -04002941 loop++;
2942 } else if (loop == 1 && cur == head) {
Chris Masonf5a31e12008-11-10 11:47:09 -05002943 int keep_going;
Chris Mason42e70e72008-11-07 18:17:11 -05002944
Chris Masonf5a31e12008-11-10 11:47:09 -05002945 /* at this point we give up on the empty_size
2946 * allocations and just try to allocate the min
2947 * space.
2948 *
2949 * The extra_loop field was set if an empty_size
2950 * allocation was attempted above, and if this
2951 * is try we need to try the loop again without
2952 * the additional empty_size.
2953 */
Chris Mason5b7c3fc2008-11-10 07:26:33 -05002954 total_needed -= empty_size;
2955 empty_size = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002956 keep_going = extra_loop;
2957 loop++;
Chris Mason42e70e72008-11-07 18:17:11 -05002958
Josef Bacik80eb2342008-10-29 14:49:05 -04002959 if (allowed_chunk_alloc && !chunk_alloc_done) {
2960 up_read(&space_info->groups_sem);
2961 ret = do_chunk_alloc(trans, root, num_bytes +
2962 2 * 1024 * 1024, data, 1);
Josef Bacik80eb2342008-10-29 14:49:05 -04002963 down_read(&space_info->groups_sem);
Chris Mason2ed6d662008-11-13 09:59:33 -05002964 if (ret < 0)
2965 goto loop_check;
Josef Bacik80eb2342008-10-29 14:49:05 -04002966 head = &space_info->block_groups;
Chris Masonf5a31e12008-11-10 11:47:09 -05002967 /*
2968 * we've allocated a new chunk, keep
2969 * trying
2970 */
2971 keep_going = 1;
Josef Bacik80eb2342008-10-29 14:49:05 -04002972 chunk_alloc_done = 1;
2973 } else if (!allowed_chunk_alloc) {
2974 space_info->force_alloc = 1;
Chris Masonf5a31e12008-11-10 11:47:09 -05002975 }
Chris Mason2ed6d662008-11-13 09:59:33 -05002976loop_check:
Chris Masonf5a31e12008-11-10 11:47:09 -05002977 if (keep_going) {
2978 cur = head->next;
2979 extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002980 } else {
2981 break;
2982 }
2983 } else if (cur == head) {
2984 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002985 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002986
2987 block_group = list_entry(cur, struct btrfs_block_group_cache,
2988 list);
2989 search_start = block_group->key.objectid;
2990 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002991 }
Chris Mason0b86a832008-03-24 15:01:56 -04002992
Josef Bacik80eb2342008-10-29 14:49:05 -04002993 /* we found what we needed */
2994 if (ins->objectid) {
2995 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2996 trans->block_group = block_group;
2997
2998 if (last_ptr)
2999 *last_ptr = ins->objectid + ins->offset;
3000 ret = 0;
3001 } else if (!ret) {
Josef Bacik4ce4cb52008-11-17 21:12:00 -05003002 printk(KERN_ERR "we were searching for %Lu bytes, num_bytes %Lu,"
3003 " loop %d, allowed_alloc %d\n", total_needed, num_bytes,
3004 loop, allowed_chunk_alloc);
Josef Bacik80eb2342008-10-29 14:49:05 -04003005 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04003006 }
Chris Mason0b86a832008-03-24 15:01:56 -04003007
Josef Bacik80eb2342008-10-29 14:49:05 -04003008 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05003009 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003010}
Chris Masonec44a352008-04-28 15:29:52 -04003011
Josef Bacik0f9dd462008-09-23 13:14:11 -04003012static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3013{
3014 struct btrfs_block_group_cache *cache;
3015 struct list_head *l;
3016
3017 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
Zheng Yane8569812008-09-26 10:05:48 -04003018 info->total_bytes - info->bytes_used - info->bytes_pinned -
3019 info->bytes_reserved, (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04003020
Josef Bacik80eb2342008-10-29 14:49:05 -04003021 down_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003022 list_for_each(l, &info->block_groups) {
3023 cache = list_entry(l, struct btrfs_block_group_cache, list);
3024 spin_lock(&cache->lock);
3025 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
Zheng Yane8569812008-09-26 10:05:48 -04003026 "%Lu pinned %Lu reserved\n",
Josef Bacik0f9dd462008-09-23 13:14:11 -04003027 cache->key.objectid, cache->key.offset,
Zheng Yane8569812008-09-26 10:05:48 -04003028 btrfs_block_group_used(&cache->item),
3029 cache->pinned, cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003030 btrfs_dump_free_space(cache, bytes);
3031 spin_unlock(&cache->lock);
3032 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003033 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003034}
Zheng Yane8569812008-09-26 10:05:48 -04003035
Chris Masone6dcd2d2008-07-17 12:53:50 -04003036static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3037 struct btrfs_root *root,
3038 u64 num_bytes, u64 min_alloc_size,
3039 u64 empty_size, u64 hint_byte,
3040 u64 search_end, struct btrfs_key *ins,
3041 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05003042{
3043 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04003044 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003045 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04003046 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003047
Chris Mason6324fbf2008-03-24 15:01:59 -04003048 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04003049 alloc_profile = info->avail_data_alloc_bits &
3050 info->data_alloc_profile;
3051 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003052 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04003053 alloc_profile = info->avail_system_alloc_bits &
3054 info->system_alloc_profile;
3055 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003056 } else {
Chris Mason8790d502008-04-03 16:29:03 -04003057 alloc_profile = info->avail_metadata_alloc_bits &
3058 info->metadata_alloc_profile;
3059 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003060 }
Chris Mason98d20f62008-04-14 09:46:10 -04003061again:
Yan Zheng2b820322008-11-17 21:11:30 -05003062 data = btrfs_reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04003063 /*
3064 * the only place that sets empty_size is btrfs_realloc_node, which
3065 * is not called recursively on allocations
3066 */
3067 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04003068 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04003069 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003070 2 * 1024 * 1024,
3071 BTRFS_BLOCK_GROUP_METADATA |
3072 (info->metadata_alloc_profile &
3073 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003074 }
3075 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003076 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003077 }
Chris Mason0b86a832008-03-24 15:01:56 -04003078
Chris Masondb945352007-10-15 16:15:53 -04003079 WARN_ON(num_bytes < root->sectorsize);
3080 ret = find_free_extent(trans, root, num_bytes, empty_size,
3081 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04003082 trans->alloc_exclude_start,
3083 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04003084
Chris Mason98d20f62008-04-14 09:46:10 -04003085 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3086 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003087 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003088 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04003089 do_chunk_alloc(trans, root->fs_info->extent_root,
3090 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003091 goto again;
3092 }
Chris Masonec44a352008-04-28 15:29:52 -04003093 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003094 struct btrfs_space_info *sinfo;
3095
3096 sinfo = __find_space_info(root->fs_info, data);
3097 printk("allocation failed flags %Lu, wanted %Lu\n",
3098 data, num_bytes);
3099 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04003100 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04003101 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003102
3103 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003104}
3105
Chris Mason65b51a02008-08-01 15:11:20 -04003106int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3107{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003108 struct btrfs_block_group_cache *cache;
3109
Josef Bacik0f9dd462008-09-23 13:14:11 -04003110 cache = btrfs_lookup_block_group(root->fs_info, start);
3111 if (!cache) {
3112 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003113 return -ENOSPC;
3114 }
3115 btrfs_add_free_space(cache, start, len);
Zheng Yan1a40e232008-09-26 10:09:34 -04003116 update_reserved_extents(root, start, len, 0);
Chris Mason65b51a02008-08-01 15:11:20 -04003117 return 0;
3118}
3119
Chris Masone6dcd2d2008-07-17 12:53:50 -04003120int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3121 struct btrfs_root *root,
3122 u64 num_bytes, u64 min_alloc_size,
3123 u64 empty_size, u64 hint_byte,
3124 u64 search_end, struct btrfs_key *ins,
3125 u64 data)
3126{
3127 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003128 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3129 empty_size, hint_byte, search_end, ins,
3130 data);
Zheng Yane8569812008-09-26 10:05:48 -04003131 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003132 return ret;
3133}
3134
3135static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003136 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003137 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003138 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003139{
3140 int ret;
3141 int pending_ret;
3142 u64 super_used;
3143 u64 root_used;
3144 u64 num_bytes = ins->offset;
3145 u32 sizes[2];
3146 struct btrfs_fs_info *info = root->fs_info;
3147 struct btrfs_root *extent_root = info->extent_root;
3148 struct btrfs_extent_item *extent_item;
3149 struct btrfs_extent_ref *ref;
3150 struct btrfs_path *path;
3151 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04003152
Zheng Yan31840ae2008-09-23 13:14:14 -04003153 if (parent == 0)
3154 parent = ins->objectid;
3155
Josef Bacik58176a92007-08-29 15:47:34 -04003156 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04003157 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04003158 super_used = btrfs_super_bytes_used(&info->super_copy);
3159 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04003160 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04003161
Josef Bacik58176a92007-08-29 15:47:34 -04003162 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04003163 root_used = btrfs_root_used(&root->root_item);
3164 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04003165
Chris Mason26b80032007-08-08 20:17:12 -04003166 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003167 struct pending_extent_op *extent_op;
3168
3169 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3170 BUG_ON(!extent_op);
3171
3172 extent_op->type = PENDING_EXTENT_INSERT;
3173 extent_op->bytenr = ins->objectid;
3174 extent_op->num_bytes = ins->offset;
3175 extent_op->parent = parent;
3176 extent_op->orig_parent = 0;
3177 extent_op->generation = ref_generation;
3178 extent_op->orig_generation = 0;
3179 extent_op->level = (int)owner;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003180 INIT_LIST_HEAD(&extent_op->list);
3181 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003182
Josef Bacik25179202008-10-29 14:49:05 -04003183 mutex_lock(&root->fs_info->extent_ins_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04003184 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3185 ins->objectid + ins->offset - 1,
Josef Bacik25179202008-10-29 14:49:05 -04003186 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04003187 set_state_private(&root->fs_info->extent_ins,
3188 ins->objectid, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04003189 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04003190 goto update_block;
3191 }
3192
Chris Mason47e4bb92008-02-01 14:51:59 -05003193 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05003194 keys[1].objectid = ins->objectid;
3195 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04003196 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05003197 sizes[0] = sizeof(*extent_item);
3198 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05003199
3200 path = btrfs_alloc_path();
3201 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05003202
3203 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3204 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04003205 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003206
Chris Mason47e4bb92008-02-01 14:51:59 -05003207 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3208 struct btrfs_extent_item);
3209 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3210 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3211 struct btrfs_extent_ref);
3212
3213 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3214 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3215 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Zheng Yan31840ae2008-09-23 13:14:14 -04003216 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05003217
3218 btrfs_mark_buffer_dirty(path->nodes[0]);
3219
3220 trans->alloc_exclude_start = 0;
3221 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05003222 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04003223 finish_current_insert(trans, extent_root, 0);
3224 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -04003225
Chris Mason925baed2008-06-25 16:01:30 -04003226 if (ret)
3227 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003228 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04003229 ret = pending_ret;
3230 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003231 }
Chris Mason26b80032007-08-08 20:17:12 -04003232
3233update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04003234 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05003235 if (ret) {
3236 printk("update block group failed for %Lu %Lu\n",
3237 ins->objectid, ins->offset);
3238 BUG();
3239 }
Chris Mason925baed2008-06-25 16:01:30 -04003240out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04003241 return ret;
3242}
3243
3244int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003245 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003246 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003247 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003248{
3249 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04003250
3251 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3252 return 0;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003253 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3254 ref_generation, owner, ins);
Zheng Yane8569812008-09-26 10:05:48 -04003255 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003256 return ret;
3257}
Chris Masone02119d2008-09-05 16:13:11 -04003258
3259/*
3260 * this is used by the tree logging recovery code. It records that
3261 * an extent has been allocated and makes sure to clear the free
3262 * space cache bits as well
3263 */
3264int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003265 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04003266 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003267 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04003268{
3269 int ret;
3270 struct btrfs_block_group_cache *block_group;
3271
Chris Masone02119d2008-09-05 16:13:11 -04003272 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik25179202008-10-29 14:49:05 -04003273 mutex_lock(&block_group->alloc_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003274 cache_block_group(root, block_group);
3275
Josef Bacik25179202008-10-29 14:49:05 -04003276 ret = btrfs_remove_free_space_lock(block_group, ins->objectid,
3277 ins->offset);
3278 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003279 BUG_ON(ret);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003280 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3281 ref_generation, owner, ins);
Chris Masone02119d2008-09-05 16:13:11 -04003282 return ret;
3283}
3284
Chris Masone6dcd2d2008-07-17 12:53:50 -04003285/*
3286 * finds a free extent and does all the dirty work required for allocation
3287 * returns the key for the extent through ins, and a tree buffer for
3288 * the first block of the extent through buf.
3289 *
3290 * returns 0 if everything worked, non-zero otherwise.
3291 */
3292int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3293 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003294 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003295 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003296 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003297 u64 search_end, struct btrfs_key *ins, u64 data)
3298{
3299 int ret;
3300
Chris Masone6dcd2d2008-07-17 12:53:50 -04003301 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3302 min_alloc_size, empty_size, hint_byte,
3303 search_end, ins, data);
3304 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04003305 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003306 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3307 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003308 owner_objectid, ins);
Chris Masond00aff02008-09-11 15:54:42 -04003309 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003310
Zheng Yane8569812008-09-26 10:05:48 -04003311 } else {
3312 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04003313 }
Chris Mason925baed2008-06-25 16:01:30 -04003314 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003315}
Chris Mason65b51a02008-08-01 15:11:20 -04003316
3317struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3318 struct btrfs_root *root,
3319 u64 bytenr, u32 blocksize)
3320{
3321 struct extent_buffer *buf;
3322
3323 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3324 if (!buf)
3325 return ERR_PTR(-ENOMEM);
3326 btrfs_set_header_generation(buf, trans->transid);
3327 btrfs_tree_lock(buf);
3328 clean_tree_block(trans, root, buf);
3329 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04003330 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3331 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04003332 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04003333 } else {
3334 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3335 buf->start + buf->len - 1, GFP_NOFS);
3336 }
Chris Mason65b51a02008-08-01 15:11:20 -04003337 trans->blocks_used++;
3338 return buf;
3339}
3340
Chris Masonfec577f2007-02-26 10:40:21 -05003341/*
3342 * helper function to allocate a block for a given tree
3343 * returns the tree buffer or NULL.
3344 */
Chris Mason5f39d392007-10-15 16:14:19 -04003345struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04003346 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003347 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05003348 u64 root_objectid,
3349 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05003350 int level,
3351 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04003352 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05003353{
Chris Masone2fa7222007-03-12 16:22:34 -04003354 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05003355 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003356 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05003357
Zheng Yan31840ae2008-09-23 13:14:14 -04003358 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003359 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04003360 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05003361 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04003362 BUG_ON(ret > 0);
3363 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05003364 }
Chris Mason55c69072008-01-09 15:55:33 -05003365
Chris Mason65b51a02008-08-01 15:11:20 -04003366 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05003367 return buf;
3368}
Chris Masona28ec192007-03-06 20:08:01 -05003369
Chris Masone02119d2008-09-05 16:13:11 -04003370int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3371 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04003372{
Chris Mason7bb86312007-12-11 09:25:06 -05003373 u64 leaf_owner;
3374 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04003375 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04003376 struct btrfs_file_extent_item *fi;
3377 int i;
3378 int nritems;
3379 int ret;
3380
Chris Mason5f39d392007-10-15 16:14:19 -04003381 BUG_ON(!btrfs_is_leaf(leaf));
3382 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05003383 leaf_owner = btrfs_header_owner(leaf);
3384 leaf_generation = btrfs_header_generation(leaf);
3385
Chris Mason6407bf62007-03-27 06:33:00 -04003386 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003387 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04003388 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04003389
3390 btrfs_item_key_to_cpu(leaf, &key, i);
3391 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04003392 continue;
3393 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003394 if (btrfs_file_extent_type(leaf, fi) ==
3395 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04003396 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04003397 /*
3398 * FIXME make sure to insert a trans record that
3399 * repeats the snapshot del on crash
3400 */
Chris Masondb945352007-10-15 16:15:53 -04003401 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3402 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04003403 continue;
Chris Mason4a096752008-07-21 10:29:44 -04003404
Chris Mason925baed2008-06-25 16:01:30 -04003405 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003406 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04003407 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003408 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003409 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04003410
3411 atomic_inc(&root->fs_info->throttle_gen);
3412 wake_up(&root->fs_info->transaction_throttle);
3413 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04003414 }
3415 return 0;
3416}
3417
Chris Masone02119d2008-09-05 16:13:11 -04003418static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3419 struct btrfs_root *root,
3420 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04003421{
3422 int i;
3423 int ret;
3424 struct btrfs_extent_info *info = ref->extents;
3425
Yan Zheng31153d82008-07-28 15:32:19 -04003426 for (i = 0; i < ref->nritems; i++) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003427 ret = __btrfs_free_extent(trans, root, info->bytenr,
3428 info->num_bytes, ref->bytenr,
3429 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003430 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04003431
3432 atomic_inc(&root->fs_info->throttle_gen);
3433 wake_up(&root->fs_info->transaction_throttle);
3434 cond_resched();
3435
Yan Zheng31153d82008-07-28 15:32:19 -04003436 BUG_ON(ret);
3437 info++;
3438 }
Yan Zheng31153d82008-07-28 15:32:19 -04003439
3440 return 0;
3441}
3442
Chris Mason333db942008-06-25 16:01:30 -04003443int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
3444 u32 *refs)
3445{
Chris Mason017e5362008-07-28 15:32:51 -04003446 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04003447
Zheng Yan31840ae2008-09-23 13:14:14 -04003448 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04003449 BUG_ON(ret);
3450
3451#if 0 // some debugging code in case we see problems here
3452 /* if the refs count is one, it won't get increased again. But
3453 * if the ref count is > 1, someone may be decreasing it at
3454 * the same time we are.
3455 */
3456 if (*refs != 1) {
3457 struct extent_buffer *eb = NULL;
3458 eb = btrfs_find_create_tree_block(root, start, len);
3459 if (eb)
3460 btrfs_tree_lock(eb);
3461
3462 mutex_lock(&root->fs_info->alloc_mutex);
3463 ret = lookup_extent_ref(NULL, root, start, len, refs);
3464 BUG_ON(ret);
3465 mutex_unlock(&root->fs_info->alloc_mutex);
3466
3467 if (eb) {
3468 btrfs_tree_unlock(eb);
3469 free_extent_buffer(eb);
3470 }
3471 if (*refs == 1) {
3472 printk("block %llu went down to one during drop_snap\n",
3473 (unsigned long long)start);
3474 }
3475
3476 }
3477#endif
3478
Chris Masone7a84562008-06-25 16:01:31 -04003479 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04003480 return ret;
Chris Mason333db942008-06-25 16:01:30 -04003481}
3482
3483/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003484 * helper function for drop_snapshot, this walks down the tree dropping ref
3485 * counts as it goes.
3486 */
Chris Mason98ed5172008-01-03 10:01:48 -05003487static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
3488 struct btrfs_root *root,
3489 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003490{
Chris Mason7bb86312007-12-11 09:25:06 -05003491 u64 root_owner;
3492 u64 root_gen;
3493 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04003494 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003495 struct extent_buffer *next;
3496 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05003497 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04003498 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04003499 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05003500 int ret;
3501 u32 refs;
3502
Chris Mason5caf2a02007-04-02 11:20:42 -04003503 WARN_ON(*level < 0);
3504 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04003505 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04003506 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05003507 BUG_ON(ret);
3508 if (refs > 1)
3509 goto out;
Chris Masone0115992007-06-19 16:23:05 -04003510
Chris Mason9aca1d52007-03-13 11:09:37 -04003511 /*
3512 * walk down to the last node level and free all the leaves
3513 */
Chris Mason6407bf62007-03-27 06:33:00 -04003514 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003515 WARN_ON(*level < 0);
3516 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05003517 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04003518
Chris Mason5f39d392007-10-15 16:14:19 -04003519 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04003520 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04003521
Chris Mason7518a232007-03-12 12:01:18 -04003522 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04003523 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05003524 break;
Chris Mason6407bf62007-03-27 06:33:00 -04003525 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003526 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04003527 BUG_ON(ret);
3528 break;
3529 }
Chris Masondb945352007-10-15 16:15:53 -04003530 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04003531 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04003532 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04003533
Chris Mason333db942008-06-25 16:01:30 -04003534 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04003535 BUG_ON(ret);
3536 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05003537 parent = path->nodes[*level];
3538 root_owner = btrfs_header_owner(parent);
3539 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05003540 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04003541
Chris Mason925baed2008-06-25 16:01:30 -04003542 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003543 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003544 root_owner, root_gen,
3545 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05003546 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04003547
3548 atomic_inc(&root->fs_info->throttle_gen);
3549 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04003550 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04003551
Chris Mason20524f02007-03-10 06:35:47 -05003552 continue;
3553 }
Chris Masonf87f0572008-08-01 11:27:23 -04003554 /*
3555 * at this point, we have a single ref, and since the
3556 * only place referencing this extent is a dead root
3557 * the reference count should never go higher.
3558 * So, we don't need to check it again
3559 */
Yan Zheng31153d82008-07-28 15:32:19 -04003560 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04003561 ref = btrfs_lookup_leaf_ref(root, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04003562 if (ref && ref->generation != ptr_gen) {
3563 btrfs_free_leaf_ref(root, ref);
3564 ref = NULL;
3565 }
Yan Zheng31153d82008-07-28 15:32:19 -04003566 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04003567 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003568 BUG_ON(ret);
3569 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04003570 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003571 *level = 0;
3572 break;
3573 }
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003574 if (printk_ratelimit()) {
Chris Mason37d1aee2008-07-31 10:48:37 -04003575 printk("leaf ref miss for bytenr %llu\n",
3576 (unsigned long long)bytenr);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003577 }
Yan Zheng31153d82008-07-28 15:32:19 -04003578 }
Chris Masondb945352007-10-15 16:15:53 -04003579 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04003580 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003581 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04003582
Chris Masonca7a79a2008-05-12 12:59:19 -04003583 next = read_tree_block(root, bytenr, blocksize,
3584 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04003585 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04003586#if 0
3587 /*
3588 * this is a debugging check and can go away
3589 * the ref should never go all the way down to 1
3590 * at this point
3591 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003592 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
3593 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04003594 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003595 WARN_ON(refs != 1);
3596#endif
Chris Masone9d0b132007-08-10 14:06:19 -04003597 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003598 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04003599 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04003600 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05003601 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04003602 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05003603 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04003604 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003605 }
3606out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003607 WARN_ON(*level < 0);
3608 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05003609
3610 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003611 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003612 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003613 } else {
3614 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003615 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003616 }
3617
Yan Zheng31153d82008-07-28 15:32:19 -04003618 blocksize = btrfs_level_size(root, *level);
3619 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003620 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003621
3622 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003623 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003624 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003625 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003626 path->nodes[*level] = NULL;
3627 *level += 1;
3628 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003629
Chris Masone7a84562008-06-25 16:01:31 -04003630 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003631 return 0;
3632}
3633
Chris Mason9aca1d52007-03-13 11:09:37 -04003634/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04003635 * helper function for drop_subtree, this function is similar to
3636 * walk_down_tree. The main difference is that it checks reference
3637 * counts while tree blocks are locked.
3638 */
3639static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
3640 struct btrfs_root *root,
3641 struct btrfs_path *path, int *level)
3642{
3643 struct extent_buffer *next;
3644 struct extent_buffer *cur;
3645 struct extent_buffer *parent;
3646 u64 bytenr;
3647 u64 ptr_gen;
3648 u32 blocksize;
3649 u32 refs;
3650 int ret;
3651
3652 cur = path->nodes[*level];
3653 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3654 &refs);
3655 BUG_ON(ret);
3656 if (refs > 1)
3657 goto out;
3658
3659 while (*level >= 0) {
3660 cur = path->nodes[*level];
3661 if (*level == 0) {
3662 ret = btrfs_drop_leaf_ref(trans, root, cur);
3663 BUG_ON(ret);
3664 clean_tree_block(trans, root, cur);
3665 break;
3666 }
3667 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3668 clean_tree_block(trans, root, cur);
3669 break;
3670 }
3671
3672 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3673 blocksize = btrfs_level_size(root, *level - 1);
3674 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3675
3676 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3677 btrfs_tree_lock(next);
3678
3679 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3680 &refs);
3681 BUG_ON(ret);
3682 if (refs > 1) {
3683 parent = path->nodes[*level];
3684 ret = btrfs_free_extent(trans, root, bytenr,
3685 blocksize, parent->start,
3686 btrfs_header_owner(parent),
3687 btrfs_header_generation(parent),
3688 *level - 1, 1);
3689 BUG_ON(ret);
3690 path->slots[*level]++;
3691 btrfs_tree_unlock(next);
3692 free_extent_buffer(next);
3693 continue;
3694 }
3695
3696 *level = btrfs_header_level(next);
3697 path->nodes[*level] = next;
3698 path->slots[*level] = 0;
3699 path->locks[*level] = 1;
3700 cond_resched();
3701 }
3702out:
3703 parent = path->nodes[*level + 1];
3704 bytenr = path->nodes[*level]->start;
3705 blocksize = path->nodes[*level]->len;
3706
3707 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3708 parent->start, btrfs_header_owner(parent),
3709 btrfs_header_generation(parent), *level, 1);
3710 BUG_ON(ret);
3711
3712 if (path->locks[*level]) {
3713 btrfs_tree_unlock(path->nodes[*level]);
3714 path->locks[*level] = 0;
3715 }
3716 free_extent_buffer(path->nodes[*level]);
3717 path->nodes[*level] = NULL;
3718 *level += 1;
3719 cond_resched();
3720 return 0;
3721}
3722
3723/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003724 * helper for dropping snapshots. This walks back up the tree in the path
3725 * to find the first node higher up where we haven't yet gone through
3726 * all the slots
3727 */
Chris Mason98ed5172008-01-03 10:01:48 -05003728static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3729 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003730 struct btrfs_path *path,
3731 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003732{
Chris Mason7bb86312007-12-11 09:25:06 -05003733 u64 root_owner;
3734 u64 root_gen;
3735 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003736 int i;
3737 int slot;
3738 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003739
Yan Zhengf82d02d2008-10-29 14:49:05 -04003740 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003741 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003742 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3743 struct extent_buffer *node;
3744 struct btrfs_disk_key disk_key;
3745 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003746 path->slots[i]++;
3747 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003748 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003749 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003750 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003751 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003752 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003753 return 0;
3754 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003755 struct extent_buffer *parent;
3756 if (path->nodes[*level] == root->node)
3757 parent = path->nodes[*level];
3758 else
3759 parent = path->nodes[*level + 1];
3760
3761 root_owner = btrfs_header_owner(parent);
3762 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003763
3764 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003765 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003766 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003767 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003768 parent->start, root_owner,
3769 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003770 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003771 if (path->locks[*level]) {
3772 btrfs_tree_unlock(path->nodes[*level]);
3773 path->locks[*level] = 0;
3774 }
Chris Mason5f39d392007-10-15 16:14:19 -04003775 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003776 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003777 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003778 }
3779 }
3780 return 1;
3781}
3782
Chris Mason9aca1d52007-03-13 11:09:37 -04003783/*
3784 * drop the reference count on the tree rooted at 'snap'. This traverses
3785 * the tree freeing any blocks that have a ref count of zero after being
3786 * decremented.
3787 */
Chris Masone089f052007-03-16 16:20:31 -04003788int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003789 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003790{
Chris Mason3768f362007-03-13 16:47:54 -04003791 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003792 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003793 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003794 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003795 int i;
3796 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003797 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003798
Chris Masona2135012008-06-25 16:01:30 -04003799 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003800 path = btrfs_alloc_path();
3801 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003802
Chris Mason5f39d392007-10-15 16:14:19 -04003803 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003804 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003805 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3806 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003807 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003808 path->slots[level] = 0;
3809 } else {
3810 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003811 struct btrfs_disk_key found_key;
3812 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003813
Chris Mason9f3a7422007-08-07 15:52:19 -04003814 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003815 level = root_item->drop_level;
3816 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003817 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003818 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003819 ret = wret;
3820 goto out;
3821 }
Chris Mason5f39d392007-10-15 16:14:19 -04003822 node = path->nodes[level];
3823 btrfs_node_key(node, &found_key, path->slots[level]);
3824 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3825 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003826 /*
3827 * unlock our path, this is safe because only this
3828 * function is allowed to delete this snapshot
3829 */
Chris Mason925baed2008-06-25 16:01:30 -04003830 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3831 if (path->nodes[i] && path->locks[i]) {
3832 path->locks[i] = 0;
3833 btrfs_tree_unlock(path->nodes[i]);
3834 }
3835 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003836 }
Chris Mason20524f02007-03-10 06:35:47 -05003837 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003838 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003839 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003840 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003841 if (wret < 0)
3842 ret = wret;
3843
Yan Zhengf82d02d2008-10-29 14:49:05 -04003844 wret = walk_up_tree(trans, root, path, &level,
3845 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003846 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003847 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003848 if (wret < 0)
3849 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003850 if (trans->transaction->in_commit) {
3851 ret = -EAGAIN;
3852 break;
3853 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003854 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003855 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003856 }
Chris Mason83e15a22007-03-12 09:03:27 -04003857 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003858 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003859 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003860 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003861 }
Chris Mason20524f02007-03-10 06:35:47 -05003862 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003863out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003864 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003865 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003866}
Chris Mason9078a3e2007-04-26 16:46:15 -04003867
Yan Zhengf82d02d2008-10-29 14:49:05 -04003868int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3869 struct btrfs_root *root,
3870 struct extent_buffer *node,
3871 struct extent_buffer *parent)
3872{
3873 struct btrfs_path *path;
3874 int level;
3875 int parent_level;
3876 int ret = 0;
3877 int wret;
3878
3879 path = btrfs_alloc_path();
3880 BUG_ON(!path);
3881
3882 BUG_ON(!btrfs_tree_locked(parent));
3883 parent_level = btrfs_header_level(parent);
3884 extent_buffer_get(parent);
3885 path->nodes[parent_level] = parent;
3886 path->slots[parent_level] = btrfs_header_nritems(parent);
3887
3888 BUG_ON(!btrfs_tree_locked(node));
3889 level = btrfs_header_level(node);
3890 extent_buffer_get(node);
3891 path->nodes[level] = node;
3892 path->slots[level] = 0;
3893
3894 while (1) {
3895 wret = walk_down_subtree(trans, root, path, &level);
3896 if (wret < 0)
3897 ret = wret;
3898 if (wret != 0)
3899 break;
3900
3901 wret = walk_up_tree(trans, root, path, &level, parent_level);
3902 if (wret < 0)
3903 ret = wret;
3904 if (wret != 0)
3905 break;
3906 }
3907
3908 btrfs_free_path(path);
3909 return ret;
3910}
3911
Chris Mason8e7bf942008-04-28 09:02:36 -04003912static unsigned long calc_ra(unsigned long start, unsigned long last,
3913 unsigned long nr)
3914{
3915 return min(last, start + nr - 1);
3916}
3917
Chris Mason98ed5172008-01-03 10:01:48 -05003918static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3919 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003920{
3921 u64 page_start;
3922 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003923 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003924 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003925 unsigned long i;
3926 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003927 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003928 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003929 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003930 unsigned int total_read = 0;
3931 unsigned int total_dirty = 0;
3932 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003933
3934 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003935
3936 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003937 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003938 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3939
Zheng Yan1a40e232008-09-26 10:09:34 -04003940 /* make sure the dirty trick played by the caller work */
3941 ret = invalidate_inode_pages2_range(inode->i_mapping,
3942 first_index, last_index);
3943 if (ret)
3944 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003945
Chris Mason4313b392008-01-03 09:08:48 -05003946 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003947
Zheng Yan1a40e232008-09-26 10:09:34 -04003948 for (i = first_index ; i <= last_index; i++) {
3949 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003950 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003951 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003952 }
3953 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003954again:
3955 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003956 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003957 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003958 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003959 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003960 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003961 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003962 if (!PageUptodate(page)) {
3963 btrfs_readpage(NULL, page);
3964 lock_page(page);
3965 if (!PageUptodate(page)) {
3966 unlock_page(page);
3967 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003968 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003969 goto out_unlock;
3970 }
3971 }
Chris Masonec44a352008-04-28 15:29:52 -04003972 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003973
Chris Masonedbd8d42007-12-21 16:27:24 -05003974 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3975 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003976 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003977
Chris Mason3eaa2882008-07-24 11:57:52 -04003978 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3979 if (ordered) {
3980 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3981 unlock_page(page);
3982 page_cache_release(page);
3983 btrfs_start_ordered_extent(inode, ordered, 1);
3984 btrfs_put_ordered_extent(ordered);
3985 goto again;
3986 }
3987 set_page_extent_mapped(page);
3988
Chris Masonea8c2812008-08-04 23:17:27 -04003989 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003990 if (i == first_index)
3991 set_extent_bits(io_tree, page_start, page_end,
3992 EXTENT_BOUNDARY, GFP_NOFS);
3993
Chris Masona061fc82008-05-07 11:43:44 -04003994 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003995 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003996
Chris Masond1310b22008-01-24 16:13:08 -05003997 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003998 unlock_page(page);
3999 page_cache_release(page);
4000 }
4001
4002out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04004003 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05004004 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04004005 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04004006 return ret;
4007}
4008
Zheng Yan1a40e232008-09-26 10:09:34 -04004009static int noinline relocate_data_extent(struct inode *reloc_inode,
4010 struct btrfs_key *extent_key,
4011 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05004012{
Zheng Yan1a40e232008-09-26 10:09:34 -04004013 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4014 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4015 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04004016 u64 start = extent_key->objectid - offset;
4017 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004018
4019 em = alloc_extent_map(GFP_NOFS);
4020 BUG_ON(!em || IS_ERR(em));
4021
Yan Zheng66435582008-10-30 14:19:50 -04004022 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004023 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004024 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004025 em->block_start = extent_key->objectid;
4026 em->bdev = root->fs_info->fs_devices->latest_bdev;
4027 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4028
4029 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04004030 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004031 while (1) {
4032 int ret;
4033 spin_lock(&em_tree->lock);
4034 ret = add_extent_mapping(em_tree, em);
4035 spin_unlock(&em_tree->lock);
4036 if (ret != -EEXIST) {
4037 free_extent_map(em);
4038 break;
4039 }
Yan Zheng66435582008-10-30 14:19:50 -04004040 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004041 }
Yan Zheng66435582008-10-30 14:19:50 -04004042 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004043
Yan Zheng66435582008-10-30 14:19:50 -04004044 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04004045}
4046
4047struct btrfs_ref_path {
4048 u64 extent_start;
4049 u64 nodes[BTRFS_MAX_LEVEL];
4050 u64 root_objectid;
4051 u64 root_generation;
4052 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004053 u32 num_refs;
4054 int lowest_level;
4055 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004056 int shared_level;
4057
4058 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4059 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04004060};
4061
4062struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04004063 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04004064 u64 disk_bytenr;
4065 u64 disk_num_bytes;
4066 u64 offset;
4067 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04004068 u8 compression;
4069 u8 encryption;
4070 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04004071};
4072
4073static int is_cowonly_root(u64 root_objectid)
4074{
4075 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4076 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4077 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4078 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
4079 root_objectid == BTRFS_TREE_LOG_OBJECTID)
4080 return 1;
4081 return 0;
4082}
4083
4084static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
4085 struct btrfs_root *extent_root,
4086 struct btrfs_ref_path *ref_path,
4087 int first_time)
4088{
4089 struct extent_buffer *leaf;
4090 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05004091 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05004092 struct btrfs_key key;
4093 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04004094 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05004095 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04004096 int level;
4097 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05004098
Zheng Yan1a40e232008-09-26 10:09:34 -04004099 path = btrfs_alloc_path();
4100 if (!path)
4101 return -ENOMEM;
4102
Zheng Yan1a40e232008-09-26 10:09:34 -04004103 if (first_time) {
4104 ref_path->lowest_level = -1;
4105 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004106 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004107 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04004108 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004109walk_down:
4110 level = ref_path->current_level - 1;
4111 while (level >= -1) {
4112 u64 parent;
4113 if (level < ref_path->lowest_level)
4114 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004115
Zheng Yan1a40e232008-09-26 10:09:34 -04004116 if (level >= 0) {
4117 bytenr = ref_path->nodes[level];
4118 } else {
4119 bytenr = ref_path->extent_start;
4120 }
4121 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004122
Zheng Yan1a40e232008-09-26 10:09:34 -04004123 parent = ref_path->nodes[level + 1];
4124 ref_path->nodes[level + 1] = 0;
4125 ref_path->current_level = level;
4126 BUG_ON(parent == 0);
4127
4128 key.objectid = bytenr;
4129 key.offset = parent + 1;
4130 key.type = BTRFS_EXTENT_REF_KEY;
4131
4132 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004133 if (ret < 0)
4134 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004135 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004136
Chris Masonedbd8d42007-12-21 16:27:24 -05004137 leaf = path->nodes[0];
4138 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04004139 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04004140 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04004141 if (ret < 0)
4142 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004143 if (ret > 0)
4144 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04004145 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04004146 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004147
4148 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04004149 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04004150 found_key.type == BTRFS_EXTENT_REF_KEY) {
4151 if (level < ref_path->shared_level)
4152 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004153 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004154 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004155next:
4156 level--;
4157 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004158 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004159 }
4160 /* reached lowest level */
4161 ret = 1;
4162 goto out;
4163walk_up:
4164 level = ref_path->current_level;
4165 while (level < BTRFS_MAX_LEVEL - 1) {
4166 u64 ref_objectid;
4167 if (level >= 0) {
4168 bytenr = ref_path->nodes[level];
4169 } else {
4170 bytenr = ref_path->extent_start;
Chris Masona061fc82008-05-07 11:43:44 -04004171 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004172 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004173
Zheng Yan1a40e232008-09-26 10:09:34 -04004174 key.objectid = bytenr;
4175 key.offset = 0;
4176 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05004177
Zheng Yan1a40e232008-09-26 10:09:34 -04004178 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4179 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05004180 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004181
4182 leaf = path->nodes[0];
4183 nritems = btrfs_header_nritems(leaf);
4184 if (path->slots[0] >= nritems) {
4185 ret = btrfs_next_leaf(extent_root, path);
4186 if (ret < 0)
4187 goto out;
4188 if (ret > 0) {
4189 /* the extent was freed by someone */
4190 if (ref_path->lowest_level == level)
4191 goto out;
4192 btrfs_release_path(extent_root, path);
4193 goto walk_down;
4194 }
4195 leaf = path->nodes[0];
4196 }
4197
4198 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4199 if (found_key.objectid != bytenr ||
4200 found_key.type != BTRFS_EXTENT_REF_KEY) {
4201 /* the extent was freed by someone */
4202 if (ref_path->lowest_level == level) {
4203 ret = 1;
4204 goto out;
4205 }
4206 btrfs_release_path(extent_root, path);
4207 goto walk_down;
4208 }
4209found:
4210 ref = btrfs_item_ptr(leaf, path->slots[0],
4211 struct btrfs_extent_ref);
4212 ref_objectid = btrfs_ref_objectid(leaf, ref);
4213 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4214 if (first_time) {
4215 level = (int)ref_objectid;
4216 BUG_ON(level >= BTRFS_MAX_LEVEL);
4217 ref_path->lowest_level = level;
4218 ref_path->current_level = level;
4219 ref_path->nodes[level] = bytenr;
4220 } else {
4221 WARN_ON(ref_objectid != level);
4222 }
4223 } else {
4224 WARN_ON(level != -1);
4225 }
4226 first_time = 0;
4227
4228 if (ref_path->lowest_level == level) {
4229 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004230 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4231 }
4232
4233 /*
4234 * the block is tree root or the block isn't in reference
4235 * counted tree.
4236 */
4237 if (found_key.objectid == found_key.offset ||
4238 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4239 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4240 ref_path->root_generation =
4241 btrfs_ref_generation(leaf, ref);
4242 if (level < 0) {
4243 /* special reference from the tree log */
4244 ref_path->nodes[0] = found_key.offset;
4245 ref_path->current_level = 0;
4246 }
4247 ret = 0;
4248 goto out;
4249 }
4250
4251 level++;
4252 BUG_ON(ref_path->nodes[level] != 0);
4253 ref_path->nodes[level] = found_key.offset;
4254 ref_path->current_level = level;
4255
4256 /*
4257 * the reference was created in the running transaction,
4258 * no need to continue walking up.
4259 */
4260 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4261 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4262 ref_path->root_generation =
4263 btrfs_ref_generation(leaf, ref);
4264 ret = 0;
4265 goto out;
4266 }
4267
4268 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004269 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004270 }
4271 /* reached max tree level, but no tree root found. */
4272 BUG();
4273out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004274 btrfs_free_path(path);
4275 return ret;
4276}
4277
4278static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4279 struct btrfs_root *extent_root,
4280 struct btrfs_ref_path *ref_path,
4281 u64 extent_start)
4282{
4283 memset(ref_path, 0, sizeof(*ref_path));
4284 ref_path->extent_start = extent_start;
4285
4286 return __next_ref_path(trans, extent_root, ref_path, 1);
4287}
4288
4289static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4290 struct btrfs_root *extent_root,
4291 struct btrfs_ref_path *ref_path)
4292{
4293 return __next_ref_path(trans, extent_root, ref_path, 0);
4294}
4295
4296static int noinline get_new_locations(struct inode *reloc_inode,
4297 struct btrfs_key *extent_key,
4298 u64 offset, int no_fragment,
4299 struct disk_extent **extents,
4300 int *nr_extents)
4301{
4302 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4303 struct btrfs_path *path;
4304 struct btrfs_file_extent_item *fi;
4305 struct extent_buffer *leaf;
4306 struct disk_extent *exts = *extents;
4307 struct btrfs_key found_key;
4308 u64 cur_pos;
4309 u64 last_byte;
4310 u32 nritems;
4311 int nr = 0;
4312 int max = *nr_extents;
4313 int ret;
4314
4315 WARN_ON(!no_fragment && *extents);
4316 if (!exts) {
4317 max = 1;
4318 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4319 if (!exts)
4320 return -ENOMEM;
4321 }
4322
4323 path = btrfs_alloc_path();
4324 BUG_ON(!path);
4325
4326 cur_pos = extent_key->objectid - offset;
4327 last_byte = extent_key->objectid + extent_key->offset;
4328 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4329 cur_pos, 0);
4330 if (ret < 0)
4331 goto out;
4332 if (ret > 0) {
4333 ret = -ENOENT;
4334 goto out;
4335 }
4336
4337 while (1) {
4338 leaf = path->nodes[0];
4339 nritems = btrfs_header_nritems(leaf);
4340 if (path->slots[0] >= nritems) {
4341 ret = btrfs_next_leaf(root, path);
4342 if (ret < 0)
4343 goto out;
4344 if (ret > 0)
4345 break;
4346 leaf = path->nodes[0];
4347 }
4348
4349 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4350 if (found_key.offset != cur_pos ||
4351 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4352 found_key.objectid != reloc_inode->i_ino)
4353 break;
4354
4355 fi = btrfs_item_ptr(leaf, path->slots[0],
4356 struct btrfs_file_extent_item);
4357 if (btrfs_file_extent_type(leaf, fi) !=
4358 BTRFS_FILE_EXTENT_REG ||
4359 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4360 break;
4361
4362 if (nr == max) {
4363 struct disk_extent *old = exts;
4364 max *= 2;
4365 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4366 memcpy(exts, old, sizeof(*exts) * nr);
4367 if (old != *extents)
4368 kfree(old);
4369 }
4370
4371 exts[nr].disk_bytenr =
4372 btrfs_file_extent_disk_bytenr(leaf, fi);
4373 exts[nr].disk_num_bytes =
4374 btrfs_file_extent_disk_num_bytes(leaf, fi);
4375 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4376 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04004377 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4378 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4379 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4380 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4381 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04004382 BUG_ON(exts[nr].offset > 0);
4383 BUG_ON(exts[nr].compression || exts[nr].encryption);
4384 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004385
4386 cur_pos += exts[nr].num_bytes;
4387 nr++;
4388
4389 if (cur_pos + offset >= last_byte)
4390 break;
4391
4392 if (no_fragment) {
4393 ret = 1;
4394 goto out;
4395 }
4396 path->slots[0]++;
4397 }
4398
4399 WARN_ON(cur_pos + offset > last_byte);
4400 if (cur_pos + offset < last_byte) {
4401 ret = -ENOENT;
4402 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05004403 }
4404 ret = 0;
4405out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004406 btrfs_free_path(path);
4407 if (ret) {
4408 if (exts != *extents)
4409 kfree(exts);
4410 } else {
4411 *extents = exts;
4412 *nr_extents = nr;
4413 }
4414 return ret;
4415}
4416
4417static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
4418 struct btrfs_root *root,
4419 struct btrfs_path *path,
4420 struct btrfs_key *extent_key,
4421 struct btrfs_key *leaf_key,
4422 struct btrfs_ref_path *ref_path,
4423 struct disk_extent *new_extents,
4424 int nr_extents)
4425{
4426 struct extent_buffer *leaf;
4427 struct btrfs_file_extent_item *fi;
4428 struct inode *inode = NULL;
4429 struct btrfs_key key;
4430 u64 lock_start = 0;
4431 u64 lock_end = 0;
4432 u64 num_bytes;
4433 u64 ext_offset;
4434 u64 first_pos;
4435 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004436 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004437 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04004438 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04004439 int ret;
4440
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004441 memcpy(&key, leaf_key, sizeof(key));
4442 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004443 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004444 if (key.objectid < ref_path->owner_objectid ||
4445 (key.objectid == ref_path->owner_objectid &&
4446 key.type < BTRFS_EXTENT_DATA_KEY)) {
4447 key.objectid = ref_path->owner_objectid;
4448 key.type = BTRFS_EXTENT_DATA_KEY;
4449 key.offset = 0;
4450 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004451 }
4452
4453 while (1) {
4454 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4455 if (ret < 0)
4456 goto out;
4457
4458 leaf = path->nodes[0];
4459 nritems = btrfs_header_nritems(leaf);
4460next:
4461 if (extent_locked && ret > 0) {
4462 /*
4463 * the file extent item was modified by someone
4464 * before the extent got locked.
4465 */
Zheng Yan1a40e232008-09-26 10:09:34 -04004466 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4467 lock_end, GFP_NOFS);
4468 extent_locked = 0;
4469 }
4470
4471 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004472 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04004473 break;
4474
4475 BUG_ON(extent_locked);
4476 ret = btrfs_next_leaf(root, path);
4477 if (ret < 0)
4478 goto out;
4479 if (ret > 0)
4480 break;
4481 leaf = path->nodes[0];
4482 nritems = btrfs_header_nritems(leaf);
4483 }
4484
4485 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4486
4487 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4488 if ((key.objectid > ref_path->owner_objectid) ||
4489 (key.objectid == ref_path->owner_objectid &&
4490 key.type > BTRFS_EXTENT_DATA_KEY) ||
4491 (key.offset >= first_pos + extent_key->offset))
4492 break;
4493 }
4494
4495 if (inode && key.objectid != inode->i_ino) {
4496 BUG_ON(extent_locked);
4497 btrfs_release_path(root, path);
4498 mutex_unlock(&inode->i_mutex);
4499 iput(inode);
4500 inode = NULL;
4501 continue;
4502 }
4503
4504 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4505 path->slots[0]++;
4506 ret = 1;
4507 goto next;
4508 }
4509 fi = btrfs_item_ptr(leaf, path->slots[0],
4510 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04004511 extent_type = btrfs_file_extent_type(leaf, fi);
4512 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4513 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04004514 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4515 extent_key->objectid)) {
4516 path->slots[0]++;
4517 ret = 1;
4518 goto next;
4519 }
4520
4521 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4522 ext_offset = btrfs_file_extent_offset(leaf, fi);
4523
4524 if (first_pos > key.offset - ext_offset)
4525 first_pos = key.offset - ext_offset;
4526
4527 if (!extent_locked) {
4528 lock_start = key.offset;
4529 lock_end = lock_start + num_bytes - 1;
4530 } else {
Yan Zheng66435582008-10-30 14:19:50 -04004531 if (lock_start > key.offset ||
4532 lock_end + 1 < key.offset + num_bytes) {
4533 unlock_extent(&BTRFS_I(inode)->io_tree,
4534 lock_start, lock_end, GFP_NOFS);
4535 extent_locked = 0;
4536 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004537 }
4538
4539 if (!inode) {
4540 btrfs_release_path(root, path);
4541
4542 inode = btrfs_iget_locked(root->fs_info->sb,
4543 key.objectid, root);
4544 if (inode->i_state & I_NEW) {
4545 BTRFS_I(inode)->root = root;
4546 BTRFS_I(inode)->location.objectid =
4547 key.objectid;
4548 BTRFS_I(inode)->location.type =
4549 BTRFS_INODE_ITEM_KEY;
4550 BTRFS_I(inode)->location.offset = 0;
4551 btrfs_read_locked_inode(inode);
4552 unlock_new_inode(inode);
4553 }
4554 /*
4555 * some code call btrfs_commit_transaction while
4556 * holding the i_mutex, so we can't use mutex_lock
4557 * here.
4558 */
4559 if (is_bad_inode(inode) ||
4560 !mutex_trylock(&inode->i_mutex)) {
4561 iput(inode);
4562 inode = NULL;
4563 key.offset = (u64)-1;
4564 goto skip;
4565 }
4566 }
4567
4568 if (!extent_locked) {
4569 struct btrfs_ordered_extent *ordered;
4570
4571 btrfs_release_path(root, path);
4572
4573 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4574 lock_end, GFP_NOFS);
4575 ordered = btrfs_lookup_first_ordered_extent(inode,
4576 lock_end);
4577 if (ordered &&
4578 ordered->file_offset <= lock_end &&
4579 ordered->file_offset + ordered->len > lock_start) {
4580 unlock_extent(&BTRFS_I(inode)->io_tree,
4581 lock_start, lock_end, GFP_NOFS);
4582 btrfs_start_ordered_extent(inode, ordered, 1);
4583 btrfs_put_ordered_extent(ordered);
4584 key.offset += num_bytes;
4585 goto skip;
4586 }
4587 if (ordered)
4588 btrfs_put_ordered_extent(ordered);
4589
Zheng Yan1a40e232008-09-26 10:09:34 -04004590 extent_locked = 1;
4591 continue;
4592 }
4593
4594 if (nr_extents == 1) {
4595 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04004596 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4597 new_extents[0].disk_bytenr);
4598 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4599 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004600 btrfs_mark_buffer_dirty(leaf);
4601
4602 btrfs_drop_extent_cache(inode, key.offset,
4603 key.offset + num_bytes - 1, 0);
4604
4605 ret = btrfs_inc_extent_ref(trans, root,
4606 new_extents[0].disk_bytenr,
4607 new_extents[0].disk_num_bytes,
4608 leaf->start,
4609 root->root_key.objectid,
4610 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004611 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004612 BUG_ON(ret);
4613
4614 ret = btrfs_free_extent(trans, root,
4615 extent_key->objectid,
4616 extent_key->offset,
4617 leaf->start,
4618 btrfs_header_owner(leaf),
4619 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004620 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004621 BUG_ON(ret);
4622
4623 btrfs_release_path(root, path);
4624 key.offset += num_bytes;
4625 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04004626 BUG_ON(1);
4627#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04004628 u64 alloc_hint;
4629 u64 extent_len;
4630 int i;
4631 /*
4632 * drop old extent pointer at first, then insert the
4633 * new pointers one bye one
4634 */
4635 btrfs_release_path(root, path);
4636 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4637 key.offset + num_bytes,
4638 key.offset, &alloc_hint);
4639 BUG_ON(ret);
4640
4641 for (i = 0; i < nr_extents; i++) {
4642 if (ext_offset >= new_extents[i].num_bytes) {
4643 ext_offset -= new_extents[i].num_bytes;
4644 continue;
4645 }
4646 extent_len = min(new_extents[i].num_bytes -
4647 ext_offset, num_bytes);
4648
4649 ret = btrfs_insert_empty_item(trans, root,
4650 path, &key,
4651 sizeof(*fi));
4652 BUG_ON(ret);
4653
4654 leaf = path->nodes[0];
4655 fi = btrfs_item_ptr(leaf, path->slots[0],
4656 struct btrfs_file_extent_item);
4657 btrfs_set_file_extent_generation(leaf, fi,
4658 trans->transid);
4659 btrfs_set_file_extent_type(leaf, fi,
4660 BTRFS_FILE_EXTENT_REG);
4661 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4662 new_extents[i].disk_bytenr);
4663 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4664 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004665 btrfs_set_file_extent_ram_bytes(leaf, fi,
4666 new_extents[i].ram_bytes);
4667
4668 btrfs_set_file_extent_compression(leaf, fi,
4669 new_extents[i].compression);
4670 btrfs_set_file_extent_encryption(leaf, fi,
4671 new_extents[i].encryption);
4672 btrfs_set_file_extent_other_encoding(leaf, fi,
4673 new_extents[i].other_encoding);
4674
Zheng Yan1a40e232008-09-26 10:09:34 -04004675 btrfs_set_file_extent_num_bytes(leaf, fi,
4676 extent_len);
4677 ext_offset += new_extents[i].offset;
4678 btrfs_set_file_extent_offset(leaf, fi,
4679 ext_offset);
4680 btrfs_mark_buffer_dirty(leaf);
4681
4682 btrfs_drop_extent_cache(inode, key.offset,
4683 key.offset + extent_len - 1, 0);
4684
4685 ret = btrfs_inc_extent_ref(trans, root,
4686 new_extents[i].disk_bytenr,
4687 new_extents[i].disk_num_bytes,
4688 leaf->start,
4689 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004690 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004691 BUG_ON(ret);
4692 btrfs_release_path(root, path);
4693
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004694 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004695
4696 ext_offset = 0;
4697 num_bytes -= extent_len;
4698 key.offset += extent_len;
4699
4700 if (num_bytes == 0)
4701 break;
4702 }
4703 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004704#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04004705 }
4706
4707 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004708 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4709 lock_end, GFP_NOFS);
4710 extent_locked = 0;
4711 }
4712skip:
4713 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4714 key.offset >= first_pos + extent_key->offset)
4715 break;
4716
4717 cond_resched();
4718 }
4719 ret = 0;
4720out:
4721 btrfs_release_path(root, path);
4722 if (inode) {
4723 mutex_unlock(&inode->i_mutex);
4724 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004725 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4726 lock_end, GFP_NOFS);
4727 }
4728 iput(inode);
4729 }
4730 return ret;
4731}
4732
Zheng Yan1a40e232008-09-26 10:09:34 -04004733int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4734 struct btrfs_root *root,
4735 struct extent_buffer *buf, u64 orig_start)
4736{
4737 int level;
4738 int ret;
4739
4740 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4741 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4742
4743 level = btrfs_header_level(buf);
4744 if (level == 0) {
4745 struct btrfs_leaf_ref *ref;
4746 struct btrfs_leaf_ref *orig_ref;
4747
4748 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4749 if (!orig_ref)
4750 return -ENOENT;
4751
4752 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4753 if (!ref) {
4754 btrfs_free_leaf_ref(root, orig_ref);
4755 return -ENOMEM;
4756 }
4757
4758 ref->nritems = orig_ref->nritems;
4759 memcpy(ref->extents, orig_ref->extents,
4760 sizeof(ref->extents[0]) * ref->nritems);
4761
4762 btrfs_free_leaf_ref(root, orig_ref);
4763
4764 ref->root_gen = trans->transid;
4765 ref->bytenr = buf->start;
4766 ref->owner = btrfs_header_owner(buf);
4767 ref->generation = btrfs_header_generation(buf);
4768 ret = btrfs_add_leaf_ref(root, ref, 0);
4769 WARN_ON(ret);
4770 btrfs_free_leaf_ref(root, ref);
4771 }
4772 return 0;
4773}
4774
4775static int noinline invalidate_extent_cache(struct btrfs_root *root,
4776 struct extent_buffer *leaf,
4777 struct btrfs_block_group_cache *group,
4778 struct btrfs_root *target_root)
4779{
4780 struct btrfs_key key;
4781 struct inode *inode = NULL;
4782 struct btrfs_file_extent_item *fi;
4783 u64 num_bytes;
4784 u64 skip_objectid = 0;
4785 u32 nritems;
4786 u32 i;
4787
4788 nritems = btrfs_header_nritems(leaf);
4789 for (i = 0; i < nritems; i++) {
4790 btrfs_item_key_to_cpu(leaf, &key, i);
4791 if (key.objectid == skip_objectid ||
4792 key.type != BTRFS_EXTENT_DATA_KEY)
4793 continue;
4794 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4795 if (btrfs_file_extent_type(leaf, fi) ==
4796 BTRFS_FILE_EXTENT_INLINE)
4797 continue;
4798 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4799 continue;
4800 if (!inode || inode->i_ino != key.objectid) {
4801 iput(inode);
4802 inode = btrfs_ilookup(target_root->fs_info->sb,
4803 key.objectid, target_root, 1);
4804 }
4805 if (!inode) {
4806 skip_objectid = key.objectid;
4807 continue;
4808 }
4809 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4810
4811 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4812 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004813 btrfs_drop_extent_cache(inode, key.offset,
4814 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004815 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4816 key.offset + num_bytes - 1, GFP_NOFS);
4817 cond_resched();
4818 }
4819 iput(inode);
4820 return 0;
4821}
4822
4823static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4824 struct btrfs_root *root,
4825 struct extent_buffer *leaf,
4826 struct btrfs_block_group_cache *group,
4827 struct inode *reloc_inode)
4828{
4829 struct btrfs_key key;
4830 struct btrfs_key extent_key;
4831 struct btrfs_file_extent_item *fi;
4832 struct btrfs_leaf_ref *ref;
4833 struct disk_extent *new_extent;
4834 u64 bytenr;
4835 u64 num_bytes;
4836 u32 nritems;
4837 u32 i;
4838 int ext_index;
4839 int nr_extent;
4840 int ret;
4841
4842 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4843 BUG_ON(!new_extent);
4844
4845 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4846 BUG_ON(!ref);
4847
4848 ext_index = -1;
4849 nritems = btrfs_header_nritems(leaf);
4850 for (i = 0; i < nritems; i++) {
4851 btrfs_item_key_to_cpu(leaf, &key, i);
4852 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4853 continue;
4854 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4855 if (btrfs_file_extent_type(leaf, fi) ==
4856 BTRFS_FILE_EXTENT_INLINE)
4857 continue;
4858 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4859 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4860 if (bytenr == 0)
4861 continue;
4862
4863 ext_index++;
4864 if (bytenr >= group->key.objectid + group->key.offset ||
4865 bytenr + num_bytes <= group->key.objectid)
4866 continue;
4867
4868 extent_key.objectid = bytenr;
4869 extent_key.offset = num_bytes;
4870 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4871 nr_extent = 1;
4872 ret = get_new_locations(reloc_inode, &extent_key,
4873 group->key.objectid, 1,
4874 &new_extent, &nr_extent);
4875 if (ret > 0)
4876 continue;
4877 BUG_ON(ret < 0);
4878
4879 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4880 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4881 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4882 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4883
Zheng Yan1a40e232008-09-26 10:09:34 -04004884 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4885 new_extent->disk_bytenr);
4886 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4887 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004888 btrfs_mark_buffer_dirty(leaf);
4889
4890 ret = btrfs_inc_extent_ref(trans, root,
4891 new_extent->disk_bytenr,
4892 new_extent->disk_num_bytes,
4893 leaf->start,
4894 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004895 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004896 BUG_ON(ret);
4897 ret = btrfs_free_extent(trans, root,
4898 bytenr, num_bytes, leaf->start,
4899 btrfs_header_owner(leaf),
4900 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004901 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004902 BUG_ON(ret);
4903 cond_resched();
4904 }
4905 kfree(new_extent);
4906 BUG_ON(ext_index + 1 != ref->nritems);
4907 btrfs_free_leaf_ref(root, ref);
4908 return 0;
4909}
4910
Yan Zhengf82d02d2008-10-29 14:49:05 -04004911int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4912 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004913{
4914 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004915 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004916
4917 if (root->reloc_root) {
4918 reloc_root = root->reloc_root;
4919 root->reloc_root = NULL;
4920 list_add(&reloc_root->dead_list,
4921 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004922
4923 btrfs_set_root_bytenr(&reloc_root->root_item,
4924 reloc_root->node->start);
4925 btrfs_set_root_level(&root->root_item,
4926 btrfs_header_level(reloc_root->node));
4927 memset(&reloc_root->root_item.drop_progress, 0,
4928 sizeof(struct btrfs_disk_key));
4929 reloc_root->root_item.drop_level = 0;
4930
4931 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4932 &reloc_root->root_key,
4933 &reloc_root->root_item);
4934 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004935 }
4936 return 0;
4937}
4938
4939int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4940{
4941 struct btrfs_trans_handle *trans;
4942 struct btrfs_root *reloc_root;
4943 struct btrfs_root *prev_root = NULL;
4944 struct list_head dead_roots;
4945 int ret;
4946 unsigned long nr;
4947
4948 INIT_LIST_HEAD(&dead_roots);
4949 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4950
4951 while (!list_empty(&dead_roots)) {
4952 reloc_root = list_entry(dead_roots.prev,
4953 struct btrfs_root, dead_list);
4954 list_del_init(&reloc_root->dead_list);
4955
4956 BUG_ON(reloc_root->commit_root != NULL);
4957 while (1) {
4958 trans = btrfs_join_transaction(root, 1);
4959 BUG_ON(!trans);
4960
4961 mutex_lock(&root->fs_info->drop_mutex);
4962 ret = btrfs_drop_snapshot(trans, reloc_root);
4963 if (ret != -EAGAIN)
4964 break;
4965 mutex_unlock(&root->fs_info->drop_mutex);
4966
4967 nr = trans->blocks_used;
4968 ret = btrfs_end_transaction(trans, root);
4969 BUG_ON(ret);
4970 btrfs_btree_balance_dirty(root, nr);
4971 }
4972
4973 free_extent_buffer(reloc_root->node);
4974
4975 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4976 &reloc_root->root_key);
4977 BUG_ON(ret);
4978 mutex_unlock(&root->fs_info->drop_mutex);
4979
4980 nr = trans->blocks_used;
4981 ret = btrfs_end_transaction(trans, root);
4982 BUG_ON(ret);
4983 btrfs_btree_balance_dirty(root, nr);
4984
4985 kfree(prev_root);
4986 prev_root = reloc_root;
4987 }
4988 if (prev_root) {
4989 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4990 kfree(prev_root);
4991 }
4992 return 0;
4993}
4994
4995int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4996{
4997 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4998 return 0;
4999}
5000
5001int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
5002{
5003 struct btrfs_root *reloc_root;
5004 struct btrfs_trans_handle *trans;
5005 struct btrfs_key location;
5006 int found;
5007 int ret;
5008
5009 mutex_lock(&root->fs_info->tree_reloc_mutex);
5010 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
5011 BUG_ON(ret);
5012 found = !list_empty(&root->fs_info->dead_reloc_roots);
5013 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5014
5015 if (found) {
5016 trans = btrfs_start_transaction(root, 1);
5017 BUG_ON(!trans);
5018 ret = btrfs_commit_transaction(trans, root);
5019 BUG_ON(ret);
5020 }
5021
5022 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5023 location.offset = (u64)-1;
5024 location.type = BTRFS_ROOT_ITEM_KEY;
5025
5026 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5027 BUG_ON(!reloc_root);
5028 btrfs_orphan_cleanup(reloc_root);
5029 return 0;
5030}
5031
5032static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
5033 struct btrfs_root *root)
5034{
5035 struct btrfs_root *reloc_root;
5036 struct extent_buffer *eb;
5037 struct btrfs_root_item *root_item;
5038 struct btrfs_key root_key;
5039 int ret;
5040
5041 BUG_ON(!root->ref_cows);
5042 if (root->reloc_root)
5043 return 0;
5044
5045 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5046 BUG_ON(!root_item);
5047
5048 ret = btrfs_copy_root(trans, root, root->commit_root,
5049 &eb, BTRFS_TREE_RELOC_OBJECTID);
5050 BUG_ON(ret);
5051
5052 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5053 root_key.offset = root->root_key.objectid;
5054 root_key.type = BTRFS_ROOT_ITEM_KEY;
5055
5056 memcpy(root_item, &root->root_item, sizeof(root_item));
5057 btrfs_set_root_refs(root_item, 0);
5058 btrfs_set_root_bytenr(root_item, eb->start);
5059 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04005060 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005061
5062 btrfs_tree_unlock(eb);
5063 free_extent_buffer(eb);
5064
5065 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5066 &root_key, root_item);
5067 BUG_ON(ret);
5068 kfree(root_item);
5069
5070 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5071 &root_key);
5072 BUG_ON(!reloc_root);
5073 reloc_root->last_trans = trans->transid;
5074 reloc_root->commit_root = NULL;
5075 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5076
5077 root->reloc_root = reloc_root;
5078 return 0;
5079}
5080
5081/*
5082 * Core function of space balance.
5083 *
5084 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04005085 * counted roots. There is one reloc tree for each subvol, and all
5086 * reloc trees share same root key objectid. Reloc trees are snapshots
5087 * of the latest committed roots of subvols (root->commit_root).
5088 *
5089 * To relocate a tree block referenced by a subvol, there are two steps.
5090 * COW the block through subvol's reloc tree, then update block pointer
5091 * in the subvol to point to the new block. Since all reloc trees share
5092 * same root key objectid, doing special handing for tree blocks owned
5093 * by them is easy. Once a tree block has been COWed in one reloc tree,
5094 * we can use the resulting new block directly when the same block is
5095 * required to COW again through other reloc trees. By this way, relocated
5096 * tree blocks are shared between reloc trees, so they are also shared
5097 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04005098 */
5099static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
5100 struct btrfs_root *root,
5101 struct btrfs_path *path,
5102 struct btrfs_key *first_key,
5103 struct btrfs_ref_path *ref_path,
5104 struct btrfs_block_group_cache *group,
5105 struct inode *reloc_inode)
5106{
5107 struct btrfs_root *reloc_root;
5108 struct extent_buffer *eb = NULL;
5109 struct btrfs_key *keys;
5110 u64 *nodes;
5111 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005112 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005113 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005114 int ret;
5115
5116 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5117 lowest_level = ref_path->owner_objectid;
5118
Yan Zhengf82d02d2008-10-29 14:49:05 -04005119 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005120 path->lowest_level = lowest_level;
5121 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5122 BUG_ON(ret < 0);
5123 path->lowest_level = 0;
5124 btrfs_release_path(root, path);
5125 return 0;
5126 }
5127
Zheng Yan1a40e232008-09-26 10:09:34 -04005128 mutex_lock(&root->fs_info->tree_reloc_mutex);
5129 ret = init_reloc_tree(trans, root);
5130 BUG_ON(ret);
5131 reloc_root = root->reloc_root;
5132
Yan Zhengf82d02d2008-10-29 14:49:05 -04005133 shared_level = ref_path->shared_level;
5134 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005135
Yan Zhengf82d02d2008-10-29 14:49:05 -04005136 keys = ref_path->node_keys;
5137 nodes = ref_path->new_nodes;
5138 memset(&keys[shared_level + 1], 0,
5139 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5140 memset(&nodes[shared_level + 1], 0,
5141 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04005142
Yan Zhengf82d02d2008-10-29 14:49:05 -04005143 if (nodes[lowest_level] == 0) {
5144 path->lowest_level = lowest_level;
5145 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5146 0, 1);
5147 BUG_ON(ret);
5148 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5149 eb = path->nodes[level];
5150 if (!eb || eb == reloc_root->node)
5151 break;
5152 nodes[level] = eb->start;
5153 if (level == 0)
5154 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5155 else
5156 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5157 }
Yan Zheng2b820322008-11-17 21:11:30 -05005158 if (nodes[0] &&
5159 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005160 eb = path->nodes[0];
5161 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5162 group, reloc_inode);
5163 BUG_ON(ret);
5164 }
5165 btrfs_release_path(reloc_root, path);
5166 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005167 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005168 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04005169 BUG_ON(ret);
5170 }
5171
Zheng Yan1a40e232008-09-26 10:09:34 -04005172 /*
5173 * replace tree blocks in the fs tree with tree blocks in
5174 * the reloc tree.
5175 */
5176 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5177 BUG_ON(ret < 0);
5178
5179 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005180 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5181 0, 0);
5182 BUG_ON(ret);
5183 extent_buffer_get(path->nodes[0]);
5184 eb = path->nodes[0];
5185 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005186 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5187 BUG_ON(ret);
5188 free_extent_buffer(eb);
5189 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005190
Yan Zhengf82d02d2008-10-29 14:49:05 -04005191 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005192 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005193 return 0;
5194}
5195
5196static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
5197 struct btrfs_root *root,
5198 struct btrfs_path *path,
5199 struct btrfs_key *first_key,
5200 struct btrfs_ref_path *ref_path)
5201{
5202 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005203
5204 ret = relocate_one_path(trans, root, path, first_key,
5205 ref_path, NULL, NULL);
5206 BUG_ON(ret);
5207
5208 if (root == root->fs_info->extent_root)
5209 btrfs_extent_post_op(trans, root);
Zheng Yan1a40e232008-09-26 10:09:34 -04005210
5211 return 0;
5212}
5213
5214static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
5215 struct btrfs_root *extent_root,
5216 struct btrfs_path *path,
5217 struct btrfs_key *extent_key)
5218{
5219 int ret;
5220
Zheng Yan1a40e232008-09-26 10:09:34 -04005221 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5222 if (ret)
5223 goto out;
5224 ret = btrfs_del_item(trans, extent_root, path);
5225out:
Chris Masonedbd8d42007-12-21 16:27:24 -05005226 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005227 return ret;
5228}
5229
5230static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
5231 struct btrfs_ref_path *ref_path)
5232{
5233 struct btrfs_key root_key;
5234
5235 root_key.objectid = ref_path->root_objectid;
5236 root_key.type = BTRFS_ROOT_ITEM_KEY;
5237 if (is_cowonly_root(ref_path->root_objectid))
5238 root_key.offset = 0;
5239 else
5240 root_key.offset = (u64)-1;
5241
5242 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5243}
5244
5245static int noinline relocate_one_extent(struct btrfs_root *extent_root,
5246 struct btrfs_path *path,
5247 struct btrfs_key *extent_key,
5248 struct btrfs_block_group_cache *group,
5249 struct inode *reloc_inode, int pass)
5250{
5251 struct btrfs_trans_handle *trans;
5252 struct btrfs_root *found_root;
5253 struct btrfs_ref_path *ref_path = NULL;
5254 struct disk_extent *new_extents = NULL;
5255 int nr_extents = 0;
5256 int loops;
5257 int ret;
5258 int level;
5259 struct btrfs_key first_key;
5260 u64 prev_block = 0;
5261
Zheng Yan1a40e232008-09-26 10:09:34 -04005262
5263 trans = btrfs_start_transaction(extent_root, 1);
5264 BUG_ON(!trans);
5265
5266 if (extent_key->objectid == 0) {
5267 ret = del_extent_zero(trans, extent_root, path, extent_key);
5268 goto out;
5269 }
5270
5271 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5272 if (!ref_path) {
5273 ret = -ENOMEM;
5274 goto out;
5275 }
5276
5277 for (loops = 0; ; loops++) {
5278 if (loops == 0) {
5279 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5280 extent_key->objectid);
5281 } else {
5282 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5283 }
5284 if (ret < 0)
5285 goto out;
5286 if (ret > 0)
5287 break;
5288
5289 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5290 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5291 continue;
5292
5293 found_root = read_ref_root(extent_root->fs_info, ref_path);
5294 BUG_ON(!found_root);
5295 /*
5296 * for reference counted tree, only process reference paths
5297 * rooted at the latest committed root.
5298 */
5299 if (found_root->ref_cows &&
5300 ref_path->root_generation != found_root->root_key.offset)
5301 continue;
5302
5303 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5304 if (pass == 0) {
5305 /*
5306 * copy data extents to new locations
5307 */
5308 u64 group_start = group->key.objectid;
5309 ret = relocate_data_extent(reloc_inode,
5310 extent_key,
5311 group_start);
5312 if (ret < 0)
5313 goto out;
5314 break;
5315 }
5316 level = 0;
5317 } else {
5318 level = ref_path->owner_objectid;
5319 }
5320
5321 if (prev_block != ref_path->nodes[level]) {
5322 struct extent_buffer *eb;
5323 u64 block_start = ref_path->nodes[level];
5324 u64 block_size = btrfs_level_size(found_root, level);
5325
5326 eb = read_tree_block(found_root, block_start,
5327 block_size, 0);
5328 btrfs_tree_lock(eb);
5329 BUG_ON(level != btrfs_header_level(eb));
5330
5331 if (level == 0)
5332 btrfs_item_key_to_cpu(eb, &first_key, 0);
5333 else
5334 btrfs_node_key_to_cpu(eb, &first_key, 0);
5335
5336 btrfs_tree_unlock(eb);
5337 free_extent_buffer(eb);
5338 prev_block = block_start;
5339 }
5340
5341 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
5342 pass >= 2) {
5343 /*
5344 * use fallback method to process the remaining
5345 * references.
5346 */
5347 if (!new_extents) {
5348 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04005349 new_extents = kmalloc(sizeof(*new_extents),
5350 GFP_NOFS);
5351 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005352 ret = get_new_locations(reloc_inode,
5353 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04005354 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04005355 &new_extents,
5356 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005357 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04005358 goto out;
5359 }
5360 btrfs_record_root_in_trans(found_root);
5361 ret = replace_one_extent(trans, found_root,
5362 path, extent_key,
5363 &first_key, ref_path,
5364 new_extents, nr_extents);
5365 if (ret < 0)
5366 goto out;
5367 continue;
5368 }
5369
5370 btrfs_record_root_in_trans(found_root);
5371 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5372 ret = relocate_tree_block(trans, found_root, path,
5373 &first_key, ref_path);
5374 } else {
5375 /*
5376 * try to update data extent references while
5377 * keeping metadata shared between snapshots.
5378 */
5379 ret = relocate_one_path(trans, found_root, path,
5380 &first_key, ref_path,
5381 group, reloc_inode);
5382 }
5383 if (ret < 0)
5384 goto out;
5385 }
5386 ret = 0;
5387out:
5388 btrfs_end_transaction(trans, extent_root);
5389 kfree(new_extents);
5390 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005391 return ret;
5392}
5393
Chris Masonec44a352008-04-28 15:29:52 -04005394static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5395{
5396 u64 num_devices;
5397 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5398 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5399
Yan Zheng2b820322008-11-17 21:11:30 -05005400 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04005401 if (num_devices == 1) {
5402 stripped |= BTRFS_BLOCK_GROUP_DUP;
5403 stripped = flags & ~stripped;
5404
5405 /* turn raid0 into single device chunks */
5406 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5407 return stripped;
5408
5409 /* turn mirroring into duplication */
5410 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5411 BTRFS_BLOCK_GROUP_RAID10))
5412 return stripped | BTRFS_BLOCK_GROUP_DUP;
5413 return flags;
5414 } else {
5415 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04005416 if (flags & stripped)
5417 return flags;
5418
5419 stripped |= BTRFS_BLOCK_GROUP_DUP;
5420 stripped = flags & ~stripped;
5421
5422 /* switch duplicated blocks with raid1 */
5423 if (flags & BTRFS_BLOCK_GROUP_DUP)
5424 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5425
5426 /* turn single device chunks into raid0 */
5427 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5428 }
5429 return flags;
5430}
5431
Chris Mason0ef3e662008-05-24 14:04:53 -04005432int __alloc_chunk_for_shrink(struct btrfs_root *root,
5433 struct btrfs_block_group_cache *shrink_block_group,
5434 int force)
5435{
5436 struct btrfs_trans_handle *trans;
5437 u64 new_alloc_flags;
5438 u64 calc;
5439
Chris Masonc286ac42008-07-22 23:06:41 -04005440 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005441 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04005442 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005443
Chris Mason0ef3e662008-05-24 14:04:53 -04005444 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005445 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04005446
Chris Mason0ef3e662008-05-24 14:04:53 -04005447 new_alloc_flags = update_block_group_flags(root,
5448 shrink_block_group->flags);
5449 if (new_alloc_flags != shrink_block_group->flags) {
5450 calc =
5451 btrfs_block_group_used(&shrink_block_group->item);
5452 } else {
5453 calc = shrink_block_group->key.offset;
5454 }
Chris Masonc286ac42008-07-22 23:06:41 -04005455 spin_unlock(&shrink_block_group->lock);
5456
Chris Mason0ef3e662008-05-24 14:04:53 -04005457 do_chunk_alloc(trans, root->fs_info->extent_root,
5458 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04005459
Chris Mason0ef3e662008-05-24 14:04:53 -04005460 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04005461 } else
5462 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005463 return 0;
5464}
5465
Zheng Yan1a40e232008-09-26 10:09:34 -04005466static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5467 struct btrfs_root *root,
5468 u64 objectid, u64 size)
5469{
5470 struct btrfs_path *path;
5471 struct btrfs_inode_item *item;
5472 struct extent_buffer *leaf;
5473 int ret;
5474
5475 path = btrfs_alloc_path();
5476 if (!path)
5477 return -ENOMEM;
5478
5479 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5480 if (ret)
5481 goto out;
5482
5483 leaf = path->nodes[0];
5484 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5485 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5486 btrfs_set_inode_generation(leaf, item, 1);
5487 btrfs_set_inode_size(leaf, item, size);
5488 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zhengd899e052008-10-30 14:25:28 -04005489 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM |
5490 BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005491 btrfs_mark_buffer_dirty(leaf);
5492 btrfs_release_path(root, path);
5493out:
5494 btrfs_free_path(path);
5495 return ret;
5496}
5497
5498static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
5499 struct btrfs_block_group_cache *group)
5500{
5501 struct inode *inode = NULL;
5502 struct btrfs_trans_handle *trans;
5503 struct btrfs_root *root;
5504 struct btrfs_key root_key;
5505 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5506 int err = 0;
5507
5508 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5509 root_key.type = BTRFS_ROOT_ITEM_KEY;
5510 root_key.offset = (u64)-1;
5511 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5512 if (IS_ERR(root))
5513 return ERR_CAST(root);
5514
5515 trans = btrfs_start_transaction(root, 1);
5516 BUG_ON(!trans);
5517
5518 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5519 if (err)
5520 goto out;
5521
5522 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5523 BUG_ON(err);
5524
5525 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04005526 group->key.offset, 0, group->key.offset,
5527 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005528 BUG_ON(err);
5529
5530 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5531 if (inode->i_state & I_NEW) {
5532 BTRFS_I(inode)->root = root;
5533 BTRFS_I(inode)->location.objectid = objectid;
5534 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5535 BTRFS_I(inode)->location.offset = 0;
5536 btrfs_read_locked_inode(inode);
5537 unlock_new_inode(inode);
5538 BUG_ON(is_bad_inode(inode));
5539 } else {
5540 BUG_ON(1);
5541 }
5542
5543 err = btrfs_orphan_add(trans, inode);
5544out:
5545 btrfs_end_transaction(trans, root);
5546 if (err) {
5547 if (inode)
5548 iput(inode);
5549 inode = ERR_PTR(err);
5550 }
5551 return inode;
5552}
5553
5554int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05005555{
5556 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05005557 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04005558 struct btrfs_fs_info *info = root->fs_info;
5559 struct extent_buffer *leaf;
5560 struct inode *reloc_inode;
5561 struct btrfs_block_group_cache *block_group;
5562 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04005563 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05005564 u64 cur_byte;
5565 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05005566 u32 nritems;
5567 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04005568 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04005569 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005570
Chris Masonedbd8d42007-12-21 16:27:24 -05005571 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04005572
5573 block_group = btrfs_lookup_block_group(info, group_start);
5574 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05005575
Chris Mason323da792008-05-09 11:46:48 -04005576 printk("btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005577 (unsigned long long)block_group->key.objectid,
5578 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04005579
Zheng Yan1a40e232008-09-26 10:09:34 -04005580 path = btrfs_alloc_path();
5581 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04005582
Zheng Yan1a40e232008-09-26 10:09:34 -04005583 reloc_inode = create_reloc_inode(info, block_group);
5584 BUG_ON(IS_ERR(reloc_inode));
5585
Zheng Yan1a40e232008-09-26 10:09:34 -04005586 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05005587 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005588
Zheng Yan1a40e232008-09-26 10:09:34 -04005589 btrfs_start_delalloc_inodes(info->tree_root);
5590 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04005591again:
Yan Zhengd899e052008-10-30 14:25:28 -04005592 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005593 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04005594 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005595 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05005596 key.offset = 0;
5597 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05005598 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05005599
Zheng Yan1a40e232008-09-26 10:09:34 -04005600 trans = btrfs_start_transaction(info->tree_root, 1);
5601 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04005602
Zheng Yan1a40e232008-09-26 10:09:34 -04005603 mutex_lock(&root->fs_info->cleaner_mutex);
5604 btrfs_clean_old_snapshots(info->tree_root);
5605 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5606 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04005607
Yan73e48b22008-01-03 14:14:39 -05005608 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05005609 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5610 if (ret < 0)
5611 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04005612next:
Chris Masonedbd8d42007-12-21 16:27:24 -05005613 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05005614 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05005615 if (path->slots[0] >= nritems) {
5616 ret = btrfs_next_leaf(root, path);
5617 if (ret < 0)
5618 goto out;
5619 if (ret == 1) {
5620 ret = 0;
5621 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005622 }
Yan73e48b22008-01-03 14:14:39 -05005623 leaf = path->nodes[0];
5624 nritems = btrfs_header_nritems(leaf);
5625 }
5626
Zheng Yan1a40e232008-09-26 10:09:34 -04005627 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05005628
Zheng Yan1a40e232008-09-26 10:09:34 -04005629 if (key.objectid >= block_group->key.objectid +
5630 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04005631 break;
5632
Chris Mason725c8462008-01-04 16:47:16 -05005633 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05005634 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005635 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05005636 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005637 continue;
Chris Mason725c8462008-01-04 16:47:16 -05005638 }
5639 progress = 1;
5640
Zheng Yan1a40e232008-09-26 10:09:34 -04005641 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5642 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05005643 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005644 goto next;
5645 }
Yan73e48b22008-01-03 14:14:39 -05005646
Chris Masonedbd8d42007-12-21 16:27:24 -05005647 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005648 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05005649 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005650
5651 __alloc_chunk_for_shrink(root, block_group, 0);
5652 ret = relocate_one_extent(root, path, &key, block_group,
5653 reloc_inode, pass);
5654 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005655 if (ret > 0)
5656 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005657
5658 key.objectid = cur_byte;
5659 key.type = 0;
5660 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005661 }
5662
5663 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005664
5665 if (pass == 0) {
5666 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5667 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5668 WARN_ON(reloc_inode->i_mapping->nrpages);
5669 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005670
5671 if (total_found > 0) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005672 printk("btrfs found %llu extents in pass %d\n",
5673 (unsigned long long)total_found, pass);
5674 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04005675 if (total_found == skipped && pass > 2) {
5676 iput(reloc_inode);
5677 reloc_inode = create_reloc_inode(info, block_group);
5678 pass = 0;
5679 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005680 goto again;
5681 }
5682
Zheng Yan1a40e232008-09-26 10:09:34 -04005683 /* delete reloc_inode */
5684 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005685
Zheng Yan1a40e232008-09-26 10:09:34 -04005686 /* unpin extents in this range */
5687 trans = btrfs_start_transaction(info->tree_root, 1);
5688 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005689
Zheng Yan1a40e232008-09-26 10:09:34 -04005690 spin_lock(&block_group->lock);
5691 WARN_ON(block_group->pinned > 0);
5692 WARN_ON(block_group->reserved > 0);
5693 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5694 spin_unlock(&block_group->lock);
5695 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005696out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005697 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005698 return ret;
5699}
5700
Chris Mason0b86a832008-03-24 15:01:56 -04005701int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
5702 struct btrfs_key *key)
5703{
Chris Mason925baed2008-06-25 16:01:30 -04005704 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005705 struct btrfs_key found_key;
5706 struct extent_buffer *leaf;
5707 int slot;
5708
5709 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5710 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005711 goto out;
5712
Chris Mason0b86a832008-03-24 15:01:56 -04005713 while(1) {
5714 slot = path->slots[0];
5715 leaf = path->nodes[0];
5716 if (slot >= btrfs_header_nritems(leaf)) {
5717 ret = btrfs_next_leaf(root, path);
5718 if (ret == 0)
5719 continue;
5720 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005721 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005722 break;
5723 }
5724 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5725
5726 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005727 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5728 ret = 0;
5729 goto out;
5730 }
Chris Mason0b86a832008-03-24 15:01:56 -04005731 path->slots[0]++;
5732 }
5733 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005734out:
Chris Mason0b86a832008-03-24 15:01:56 -04005735 return ret;
5736}
5737
Zheng Yan1a40e232008-09-26 10:09:34 -04005738int btrfs_free_block_groups(struct btrfs_fs_info *info)
5739{
5740 struct btrfs_block_group_cache *block_group;
5741 struct rb_node *n;
5742
Zheng Yan1a40e232008-09-26 10:09:34 -04005743 spin_lock(&info->block_group_cache_lock);
5744 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5745 block_group = rb_entry(n, struct btrfs_block_group_cache,
5746 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04005747 rb_erase(&block_group->cache_node,
5748 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04005749 spin_unlock(&info->block_group_cache_lock);
5750
5751 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005752 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005753 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005754 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005755 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04005756
5757 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005758 }
5759 spin_unlock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005760 return 0;
5761}
5762
Chris Mason9078a3e2007-04-26 16:46:15 -04005763int btrfs_read_block_groups(struct btrfs_root *root)
5764{
5765 struct btrfs_path *path;
5766 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005767 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005768 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005769 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005770 struct btrfs_key key;
5771 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005772 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005773
Chris Masonbe744172007-05-06 10:15:01 -04005774 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005775 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005776 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005777 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005778 path = btrfs_alloc_path();
5779 if (!path)
5780 return -ENOMEM;
5781
5782 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005783 ret = find_first_block_group(root, path, &key);
5784 if (ret > 0) {
5785 ret = 0;
5786 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005787 }
Chris Mason0b86a832008-03-24 15:01:56 -04005788 if (ret != 0)
5789 goto error;
5790
Chris Mason5f39d392007-10-15 16:14:19 -04005791 leaf = path->nodes[0];
5792 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005793 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005794 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005795 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005796 break;
5797 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005798
Chris Masonc286ac42008-07-22 23:06:41 -04005799 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005800 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005801 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005802 read_extent_buffer(leaf, &cache->item,
5803 btrfs_item_ptr_offset(leaf, path->slots[0]),
5804 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005805 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005806
Chris Mason9078a3e2007-04-26 16:46:15 -04005807 key.objectid = found_key.objectid + found_key.offset;
5808 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005809 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005810
Chris Mason6324fbf2008-03-24 15:01:59 -04005811 ret = update_space_info(info, cache->flags, found_key.offset,
5812 btrfs_block_group_used(&cache->item),
5813 &space_info);
5814 BUG_ON(ret);
5815 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005816 down_write(&space_info->groups_sem);
5817 list_add_tail(&cache->list, &space_info->block_groups);
5818 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005819
Josef Bacik0f9dd462008-09-23 13:14:11 -04005820 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5821 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005822
5823 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05005824 if (btrfs_chunk_readonly(root, cache->key.objectid))
5825 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04005826 }
Chris Mason0b86a832008-03-24 15:01:56 -04005827 ret = 0;
5828error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005829 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005830 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005831}
Chris Mason6324fbf2008-03-24 15:01:59 -04005832
5833int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5834 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005835 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005836 u64 size)
5837{
5838 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005839 struct btrfs_root *extent_root;
5840 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005841
5842 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005843
Chris Masone02119d2008-09-05 16:13:11 -04005844 root->fs_info->last_trans_new_blockgroup = trans->transid;
5845
Chris Mason8f18cf12008-04-25 16:53:30 -04005846 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005847 if (!cache)
5848 return -ENOMEM;
5849
Chris Masone17cade2008-04-15 15:41:47 -04005850 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005851 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04005852 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005853 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005854 INIT_LIST_HEAD(&cache->list);
Chris Mason6324fbf2008-03-24 15:01:59 -04005855 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04005856
Chris Mason6324fbf2008-03-24 15:01:59 -04005857 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005858 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5859 cache->flags = type;
5860 btrfs_set_block_group_flags(&cache->item, type);
5861
5862 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5863 &cache->space_info);
5864 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005865 down_write(&cache->space_info->groups_sem);
5866 list_add_tail(&cache->list, &cache->space_info->block_groups);
5867 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005868
Josef Bacik0f9dd462008-09-23 13:14:11 -04005869 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5870 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005871
Chris Mason6324fbf2008-03-24 15:01:59 -04005872 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5873 sizeof(cache->item));
5874 BUG_ON(ret);
5875
Chris Mason87ef2bb2008-10-30 11:23:27 -04005876 finish_current_insert(trans, extent_root, 0);
5877 ret = del_pending_extents(trans, extent_root, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04005878 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04005879 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005880
Chris Mason6324fbf2008-03-24 15:01:59 -04005881 return 0;
5882}
Zheng Yan1a40e232008-09-26 10:09:34 -04005883
5884int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5885 struct btrfs_root *root, u64 group_start)
5886{
5887 struct btrfs_path *path;
5888 struct btrfs_block_group_cache *block_group;
5889 struct btrfs_key key;
5890 int ret;
5891
Zheng Yan1a40e232008-09-26 10:09:34 -04005892 root = root->fs_info->extent_root;
5893
5894 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5895 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05005896 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04005897
5898 memcpy(&key, &block_group->key, sizeof(key));
5899
5900 path = btrfs_alloc_path();
5901 BUG_ON(!path);
5902
5903 btrfs_remove_free_space_cache(block_group);
5904 rb_erase(&block_group->cache_node,
5905 &root->fs_info->block_group_cache_tree);
Josef Bacik80eb2342008-10-29 14:49:05 -04005906 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005907 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005908 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005909
Yan Zhengc146afa2008-11-12 14:34:12 -05005910 spin_lock(&block_group->space_info->lock);
5911 block_group->space_info->total_bytes -= block_group->key.offset;
5912 block_group->space_info->bytes_readonly -= block_group->key.offset;
5913 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05005914 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05005915
Zheng Yan1a40e232008-09-26 10:09:34 -04005916 /*
5917 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5918 kfree(shrink_block_group);
5919 */
5920
5921 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5922 if (ret > 0)
5923 ret = -EIO;
5924 if (ret < 0)
5925 goto out;
5926
5927 ret = btrfs_del_item(trans, root, path);
5928out:
5929 btrfs_free_path(path);
5930 return ret;
5931}