blob: 1eb69a91b727882cca155bc37ec2eebb435ccabd [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Mason74493f72007-12-11 09:25:06 -050022#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040023#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050024#include "ctree.h"
25#include "disk-io.h"
26#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040027#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040028#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040029#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040030#include "ref-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050031
Zheng Yan31840ae2008-09-23 13:14:14 -040032#define PENDING_EXTENT_INSERT 0
33#define PENDING_EXTENT_DELETE 1
34#define PENDING_BACKREF_UPDATE 2
35
36struct pending_extent_op {
37 int type;
38 u64 bytenr;
39 u64 num_bytes;
40 u64 parent;
41 u64 orig_parent;
42 u64 generation;
43 u64 orig_generation;
44 int level;
45};
46
Chris Masone089f052007-03-16 16:20:31 -040047static int finish_current_insert(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -040048 btrfs_root *extent_root, int all);
Chris Masone20d96d2007-03-22 12:13:20 -040049static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -040050 btrfs_root *extent_root, int all);
Chris Mason925baed2008-06-25 16:01:30 -040051static struct btrfs_block_group_cache *
52__btrfs_find_block_group(struct btrfs_root *root,
53 struct btrfs_block_group_cache *hint,
54 u64 search_start, int data, int owner);
Yand548ee52008-01-03 13:56:30 -050055
Josef Bacik0f9dd462008-09-23 13:14:11 -040056static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
57{
58 return (cache->flags & bits) == bits;
59}
60
61/*
62 * this adds the block group to the fs_info rb tree for the block group
63 * cache
64 */
65int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
66 struct btrfs_block_group_cache *block_group)
67{
68 struct rb_node **p;
69 struct rb_node *parent = NULL;
70 struct btrfs_block_group_cache *cache;
71
72 spin_lock(&info->block_group_cache_lock);
73 p = &info->block_group_cache_tree.rb_node;
74
75 while (*p) {
76 parent = *p;
77 cache = rb_entry(parent, struct btrfs_block_group_cache,
78 cache_node);
79 if (block_group->key.objectid < cache->key.objectid) {
80 p = &(*p)->rb_left;
81 } else if (block_group->key.objectid > cache->key.objectid) {
82 p = &(*p)->rb_right;
83 } else {
84 spin_unlock(&info->block_group_cache_lock);
85 return -EEXIST;
86 }
87 }
88
89 rb_link_node(&block_group->cache_node, parent, p);
90 rb_insert_color(&block_group->cache_node,
91 &info->block_group_cache_tree);
92 spin_unlock(&info->block_group_cache_lock);
93
94 return 0;
95}
96
97/*
98 * This will return the block group at or after bytenr if contains is 0, else
99 * it will return the block group that contains the bytenr
100 */
101static struct btrfs_block_group_cache *
102block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
103 int contains)
104{
105 struct btrfs_block_group_cache *cache, *ret = NULL;
106 struct rb_node *n;
107 u64 end, start;
108
109 spin_lock(&info->block_group_cache_lock);
110 n = info->block_group_cache_tree.rb_node;
111
112 while (n) {
113 cache = rb_entry(n, struct btrfs_block_group_cache,
114 cache_node);
115 end = cache->key.objectid + cache->key.offset - 1;
116 start = cache->key.objectid;
117
118 if (bytenr < start) {
119 if (!contains && (!ret || start < ret->key.objectid))
120 ret = cache;
121 n = n->rb_left;
122 } else if (bytenr > start) {
123 if (contains && bytenr <= end) {
124 ret = cache;
125 break;
126 }
127 n = n->rb_right;
128 } else {
129 ret = cache;
130 break;
131 }
132 }
133 spin_unlock(&info->block_group_cache_lock);
134
135 return ret;
136}
137
138/*
139 * this is only called by cache_block_group, since we could have freed extents
140 * we need to check the pinned_extents for any extents that can't be used yet
141 * since their free space will be released as soon as the transaction commits.
142 */
143static int add_new_free_space(struct btrfs_block_group_cache *block_group,
144 struct btrfs_fs_info *info, u64 start, u64 end)
145{
146 u64 extent_start, extent_end, size;
147 int ret;
148
Josef Bacik25179202008-10-29 14:49:05 -0400149 mutex_lock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400150 while (start < end) {
151 ret = find_first_extent_bit(&info->pinned_extents, start,
152 &extent_start, &extent_end,
153 EXTENT_DIRTY);
154 if (ret)
155 break;
156
157 if (extent_start == start) {
158 start = extent_end + 1;
159 } else if (extent_start > start && extent_start < end) {
160 size = extent_start - start;
Josef Bacik25179202008-10-29 14:49:05 -0400161 ret = btrfs_add_free_space_lock(block_group, start,
162 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400163 BUG_ON(ret);
164 start = extent_end + 1;
165 } else {
166 break;
167 }
168 }
169
170 if (start < end) {
171 size = end - start;
Josef Bacik25179202008-10-29 14:49:05 -0400172 ret = btrfs_add_free_space_lock(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400173 BUG_ON(ret);
174 }
Josef Bacik25179202008-10-29 14:49:05 -0400175 mutex_unlock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400176
177 return 0;
178}
179
Chris Masone37c9e62007-05-09 20:13:14 -0400180static int cache_block_group(struct btrfs_root *root,
181 struct btrfs_block_group_cache *block_group)
182{
183 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400184 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400185 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400186 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400187 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -0400188 u64 last = 0;
Yan7d7d6062007-09-14 16:15:28 -0400189 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -0400190 int found = 0;
191
Chris Mason00f5c792007-11-30 10:09:33 -0500192 if (!block_group)
193 return 0;
194
Chris Masone37c9e62007-05-09 20:13:14 -0400195 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400196
197 if (block_group->cached)
198 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400199
Chris Masone37c9e62007-05-09 20:13:14 -0400200 path = btrfs_alloc_path();
201 if (!path)
202 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400203
Chris Mason2cc58cf2007-08-27 16:49:44 -0400204 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400205 /*
206 * we get into deadlocks with paths held by callers of this function.
207 * since the alloc_mutex is protecting things right now, just
208 * skip the locking here
209 */
210 path->skip_locking = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400211 first_free = max_t(u64, block_group->key.objectid,
212 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Masone37c9e62007-05-09 20:13:14 -0400213 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -0400214 key.offset = 0;
215 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
216 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
217 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400218 goto err;
Chris Mason0b86a832008-03-24 15:01:56 -0400219 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -0500220 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400221 goto err;
Yand548ee52008-01-03 13:56:30 -0500222 if (ret == 0) {
223 leaf = path->nodes[0];
224 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
225 if (key.objectid + key.offset > first_free)
226 first_free = key.objectid + key.offset;
227 }
Chris Masone37c9e62007-05-09 20:13:14 -0400228 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400229 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400230 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400231 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400232 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400233 if (ret < 0)
234 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400235 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400236 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400237 else
Chris Masone37c9e62007-05-09 20:13:14 -0400238 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400239 }
Chris Mason5f39d392007-10-15 16:14:19 -0400240 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400241 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400242 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400243
Chris Masone37c9e62007-05-09 20:13:14 -0400244 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400245 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400246 break;
Yan7d7d6062007-09-14 16:15:28 -0400247
248 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
249 if (!found) {
250 last = first_free;
251 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400252 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400253
254 add_new_free_space(block_group, root->fs_info, last,
255 key.objectid);
256
Yan7d7d6062007-09-14 16:15:28 -0400257 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400258 }
Yan7d7d6062007-09-14 16:15:28 -0400259next:
Chris Masone37c9e62007-05-09 20:13:14 -0400260 path->slots[0]++;
261 }
262
Yan7d7d6062007-09-14 16:15:28 -0400263 if (!found)
264 last = first_free;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400265
266 add_new_free_space(block_group, root->fs_info, last,
267 block_group->key.objectid +
268 block_group->key.offset);
269
Chris Masone37c9e62007-05-09 20:13:14 -0400270 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400271 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400272err:
Chris Masone37c9e62007-05-09 20:13:14 -0400273 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400274 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400275}
276
Josef Bacik0f9dd462008-09-23 13:14:11 -0400277/*
278 * return the block group that starts at or after bytenr
279 */
Chris Mason0ef3e662008-05-24 14:04:53 -0400280struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
281 btrfs_fs_info *info,
282 u64 bytenr)
283{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400284 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400285
Josef Bacik0f9dd462008-09-23 13:14:11 -0400286 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400287
Josef Bacik0f9dd462008-09-23 13:14:11 -0400288 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400289}
290
Josef Bacik0f9dd462008-09-23 13:14:11 -0400291/*
292 * return the block group that contains teh given bytenr
293 */
Chris Mason5276aed2007-06-11 21:33:38 -0400294struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
295 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400296 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400297{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400298 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400299
Josef Bacik0f9dd462008-09-23 13:14:11 -0400300 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400301
Josef Bacik0f9dd462008-09-23 13:14:11 -0400302 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400303}
Chris Mason0b86a832008-03-24 15:01:56 -0400304
Josef Bacik0f9dd462008-09-23 13:14:11 -0400305static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
306 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400307{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400308 struct list_head *head = &info->space_info;
309 struct list_head *cur;
310 struct btrfs_space_info *found;
311 list_for_each(cur, head) {
312 found = list_entry(cur, struct btrfs_space_info, list);
313 if (found->flags == flags)
314 return found;
315 }
316 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400317}
318
Josef Bacik80eb2342008-10-29 14:49:05 -0400319static u64 div_factor(u64 num, int factor)
320{
321 if (factor == 10)
322 return num;
323 num *= factor;
324 do_div(num, 10);
325 return num;
326}
327
Chris Mason925baed2008-06-25 16:01:30 -0400328static struct btrfs_block_group_cache *
329__btrfs_find_block_group(struct btrfs_root *root,
330 struct btrfs_block_group_cache *hint,
331 u64 search_start, int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400332{
Chris Mason96b51792007-10-15 16:15:19 -0400333 struct btrfs_block_group_cache *cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400334 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400335 struct btrfs_fs_info *info = root->fs_info;
336 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400337 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400338 u64 free_check;
Chris Mason31f3c992007-04-30 15:25:45 -0400339 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400340 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400341 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400342
Chris Masona236aed2008-04-29 09:38:00 -0400343 if (data & BTRFS_BLOCK_GROUP_METADATA)
344 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400345
Chris Mason0ef3e662008-05-24 14:04:53 -0400346 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400347 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400348 shint = btrfs_lookup_first_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400349 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonc286ac42008-07-22 23:06:41 -0400350 spin_lock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400351 used = btrfs_block_group_used(&shint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400352 if (used + shint->pinned + shint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500353 div_factor(shint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400354 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400355 return shint;
356 }
Chris Masonc286ac42008-07-22 23:06:41 -0400357 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400358 }
359 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400360 if (hint && !hint->ro && block_group_bits(hint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400361 spin_lock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400362 used = btrfs_block_group_used(&hint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400363 if (used + hint->pinned + hint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500364 div_factor(hint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400365 spin_unlock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400366 return hint;
367 }
Chris Masonc286ac42008-07-22 23:06:41 -0400368 spin_unlock(&hint->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400369 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400370 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400371 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400372 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400373 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400374 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400375 }
Chris Mason31f3c992007-04-30 15:25:45 -0400376again:
Zheng Yane8569812008-09-26 10:05:48 -0400377 while (1) {
378 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400379 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400380 break;
Chris Mason96b51792007-10-15 16:15:19 -0400381
Chris Masonc286ac42008-07-22 23:06:41 -0400382 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400383 last = cache->key.objectid + cache->key.offset;
384 used = btrfs_block_group_used(&cache->item);
385
Chris Mason8f18cf12008-04-25 16:53:30 -0400386 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400387 free_check = div_factor(cache->key.offset, factor);
Zheng Yane8569812008-09-26 10:05:48 -0400388 if (used + cache->pinned + cache->reserved <
389 free_check) {
Chris Mason8790d502008-04-03 16:29:03 -0400390 found_group = cache;
Chris Masonc286ac42008-07-22 23:06:41 -0400391 spin_unlock(&cache->lock);
Chris Mason8790d502008-04-03 16:29:03 -0400392 goto found;
393 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400394 }
Chris Masonc286ac42008-07-22 23:06:41 -0400395 spin_unlock(&cache->lock);
Chris Masonde428b62007-05-18 13:28:27 -0400396 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400397 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400398 if (!wrapped) {
399 last = search_start;
400 wrapped = 1;
401 goto again;
402 }
403 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400404 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400405 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400406 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400407 goto again;
408 }
Chris Masonbe744172007-05-06 10:15:01 -0400409found:
Chris Mason31f3c992007-04-30 15:25:45 -0400410 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400411}
412
Chris Mason925baed2008-06-25 16:01:30 -0400413struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
414 struct btrfs_block_group_cache
415 *hint, u64 search_start,
416 int data, int owner)
417{
418
419 struct btrfs_block_group_cache *ret;
Chris Mason925baed2008-06-25 16:01:30 -0400420 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
Chris Mason925baed2008-06-25 16:01:30 -0400421 return ret;
422}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400423
Chris Masone02119d2008-09-05 16:13:11 -0400424/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400425int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400426{
427 int ret;
428 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400429 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400430
Zheng Yan31840ae2008-09-23 13:14:14 -0400431 path = btrfs_alloc_path();
432 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400433 key.objectid = start;
434 key.offset = len;
435 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
436 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
437 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400438 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500439 return ret;
440}
441
Chris Masond8d5f3e2007-12-11 12:42:00 -0500442/*
443 * Back reference rules. Back refs have three main goals:
444 *
445 * 1) differentiate between all holders of references to an extent so that
446 * when a reference is dropped we can make sure it was a valid reference
447 * before freeing the extent.
448 *
449 * 2) Provide enough information to quickly find the holders of an extent
450 * if we notice a given block is corrupted or bad.
451 *
452 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
453 * maintenance. This is actually the same as #2, but with a slightly
454 * different use case.
455 *
456 * File extents can be referenced by:
457 *
458 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400459 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500460 * - different offsets inside a file (bookend extents in file.c)
461 *
462 * The extent ref structure has fields for:
463 *
464 * - Objectid of the subvolume root
465 * - Generation number of the tree holding the reference
466 * - objectid of the file holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400467 * - number of references holding by parent node (alway 1 for tree blocks)
468 *
469 * Btree leaf may hold multiple references to a file extent. In most cases,
470 * these references are from same file and the corresponding offsets inside
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400471 * the file are close together.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500472 *
473 * When a file extent is allocated the fields are filled in:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400474 * (root_key.objectid, trans->transid, inode objectid, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500475 *
476 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400477 * in the leaf. It looks similar to the create case, but trans->transid will
478 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500479 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400480 * (root_key.objectid, trans->transid, inode objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400481 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500482 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400483 * When a file extent is removed either during snapshot deletion or
484 * file truncation, we find the corresponding back reference and check
485 * the following fields:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500486 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400487 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
488 * inode objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500489 *
490 * Btree extents can be referenced by:
491 *
492 * - Different subvolumes
493 * - Different generations of the same subvolume
494 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500495 * When a tree block is created, back references are inserted:
496 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400497 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500498 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400499 * When a tree block is cow'd, new back references are added for all the
500 * blocks it points to. If the tree block isn't in reference counted root,
501 * the old back references are removed. These new back references are of
502 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500503 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400504 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500505 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400506 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500507 *
508 * if backref was for a tree root:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400509 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500510 * else
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400511 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500512 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400513 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500514 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400515 * The key objectid corresponds to the first byte in the extent, the key
516 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
517 * byte of parent extent. If a extent is tree root, the key offset is set
518 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500519 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400520
521static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
522 struct btrfs_root *root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400523 struct btrfs_path *path,
524 u64 bytenr, u64 parent,
525 u64 ref_root, u64 ref_generation,
526 u64 owner_objectid, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500527{
Chris Mason74493f72007-12-11 09:25:06 -0500528 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400529 struct btrfs_extent_ref *ref;
530 struct extent_buffer *leaf;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400531 u64 ref_objectid;
Chris Mason74493f72007-12-11 09:25:06 -0500532 int ret;
533
Chris Mason74493f72007-12-11 09:25:06 -0500534 key.objectid = bytenr;
535 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400536 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500537
Zheng Yan31840ae2008-09-23 13:14:14 -0400538 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
539 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500540 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400541 if (ret > 0) {
542 ret = -ENOENT;
543 goto out;
544 }
545
546 leaf = path->nodes[0];
547 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400548 ref_objectid = btrfs_ref_objectid(leaf, ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400549 if (btrfs_ref_root(leaf, ref) != ref_root ||
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400550 btrfs_ref_generation(leaf, ref) != ref_generation ||
551 (ref_objectid != owner_objectid &&
552 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400553 ret = -EIO;
554 WARN_ON(1);
555 goto out;
556 }
557 ret = 0;
558out:
559 return ret;
560}
561
562static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
563 struct btrfs_root *root,
564 struct btrfs_path *path,
565 u64 bytenr, u64 parent,
566 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400567 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400568{
569 struct btrfs_key key;
570 struct extent_buffer *leaf;
571 struct btrfs_extent_ref *ref;
572 u32 num_refs;
573 int ret;
574
575 key.objectid = bytenr;
576 key.type = BTRFS_EXTENT_REF_KEY;
577 key.offset = parent;
578
579 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
580 if (ret == 0) {
581 leaf = path->nodes[0];
582 ref = btrfs_item_ptr(leaf, path->slots[0],
583 struct btrfs_extent_ref);
584 btrfs_set_ref_root(leaf, ref, ref_root);
585 btrfs_set_ref_generation(leaf, ref, ref_generation);
586 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400587 btrfs_set_ref_num_refs(leaf, ref, 1);
588 } else if (ret == -EEXIST) {
589 u64 existing_owner;
590 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
591 leaf = path->nodes[0];
592 ref = btrfs_item_ptr(leaf, path->slots[0],
593 struct btrfs_extent_ref);
594 if (btrfs_ref_root(leaf, ref) != ref_root ||
595 btrfs_ref_generation(leaf, ref) != ref_generation) {
596 ret = -EIO;
597 WARN_ON(1);
598 goto out;
599 }
600
601 num_refs = btrfs_ref_num_refs(leaf, ref);
602 BUG_ON(num_refs == 0);
603 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
604
605 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400606 if (existing_owner != owner_objectid &&
607 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400608 btrfs_set_ref_objectid(leaf, ref,
609 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400610 }
611 ret = 0;
612 } else {
613 goto out;
614 }
Chris Mason7bb86312007-12-11 09:25:06 -0500615 btrfs_mark_buffer_dirty(path->nodes[0]);
616out:
617 btrfs_release_path(root, path);
618 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500619}
620
Zheng Yan31840ae2008-09-23 13:14:14 -0400621static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
622 struct btrfs_root *root,
623 struct btrfs_path *path)
624{
625 struct extent_buffer *leaf;
626 struct btrfs_extent_ref *ref;
627 u32 num_refs;
628 int ret = 0;
629
630 leaf = path->nodes[0];
631 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
632 num_refs = btrfs_ref_num_refs(leaf, ref);
633 BUG_ON(num_refs == 0);
634 num_refs -= 1;
635 if (num_refs == 0) {
636 ret = btrfs_del_item(trans, root, path);
637 } else {
638 btrfs_set_ref_num_refs(leaf, ref, num_refs);
639 btrfs_mark_buffer_dirty(leaf);
640 }
641 btrfs_release_path(root, path);
642 return ret;
643}
644
645static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
646 struct btrfs_root *root, u64 bytenr,
647 u64 orig_parent, u64 parent,
648 u64 orig_root, u64 ref_root,
649 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400650 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400651{
652 int ret;
653 struct btrfs_root *extent_root = root->fs_info->extent_root;
654 struct btrfs_path *path;
655
656 if (root == root->fs_info->extent_root) {
657 struct pending_extent_op *extent_op;
658 u64 num_bytes;
659
660 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
661 num_bytes = btrfs_level_size(root, (int)owner_objectid);
Josef Bacik25179202008-10-29 14:49:05 -0400662 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -0400663 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
Josef Bacik25179202008-10-29 14:49:05 -0400664 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400665 u64 priv;
666 ret = get_state_private(&root->fs_info->extent_ins,
667 bytenr, &priv);
668 BUG_ON(ret);
669 extent_op = (struct pending_extent_op *)
670 (unsigned long)priv;
671 BUG_ON(extent_op->parent != orig_parent);
672 BUG_ON(extent_op->generation != orig_generation);
Josef Bacik25179202008-10-29 14:49:05 -0400673
Zheng Yan31840ae2008-09-23 13:14:14 -0400674 extent_op->parent = parent;
675 extent_op->generation = ref_generation;
676 } else {
677 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
678 BUG_ON(!extent_op);
679
680 extent_op->type = PENDING_BACKREF_UPDATE;
681 extent_op->bytenr = bytenr;
682 extent_op->num_bytes = num_bytes;
683 extent_op->parent = parent;
684 extent_op->orig_parent = orig_parent;
685 extent_op->generation = ref_generation;
686 extent_op->orig_generation = orig_generation;
687 extent_op->level = (int)owner_objectid;
688
689 set_extent_bits(&root->fs_info->extent_ins,
690 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -0400691 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400692 set_state_private(&root->fs_info->extent_ins,
693 bytenr, (unsigned long)extent_op);
694 }
Josef Bacik25179202008-10-29 14:49:05 -0400695 mutex_unlock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -0400696 return 0;
697 }
698
699 path = btrfs_alloc_path();
700 if (!path)
701 return -ENOMEM;
702 ret = lookup_extent_backref(trans, extent_root, path,
703 bytenr, orig_parent, orig_root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400704 orig_generation, owner_objectid, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400705 if (ret)
706 goto out;
707 ret = remove_extent_backref(trans, extent_root, path);
708 if (ret)
709 goto out;
710 ret = insert_extent_backref(trans, extent_root, path, bytenr,
711 parent, ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400712 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400713 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400714 finish_current_insert(trans, extent_root, 0);
715 del_pending_extents(trans, extent_root, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400716out:
717 btrfs_free_path(path);
718 return ret;
719}
720
721int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
722 struct btrfs_root *root, u64 bytenr,
723 u64 orig_parent, u64 parent,
724 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400725 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400726{
727 int ret;
728 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
729 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
730 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400731 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
732 parent, ref_root, ref_root,
733 ref_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400734 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400735 return ret;
736}
737
Chris Mason925baed2008-06-25 16:01:30 -0400738static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400739 struct btrfs_root *root, u64 bytenr,
740 u64 orig_parent, u64 parent,
741 u64 orig_root, u64 ref_root,
742 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400743 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -0500744{
Chris Mason5caf2a02007-04-02 11:20:42 -0400745 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500746 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400747 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400748 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400749 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400750 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500751
Chris Mason5caf2a02007-04-02 11:20:42 -0400752 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400753 if (!path)
754 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400755
Chris Mason3c12ac72008-04-21 12:01:38 -0400756 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400757 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400758 key.type = BTRFS_EXTENT_ITEM_KEY;
759 key.offset = (u64)-1;
760
Chris Mason5caf2a02007-04-02 11:20:42 -0400761 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400762 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400763 if (ret < 0)
764 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400765 BUG_ON(ret == 0 || path->slots[0] == 0);
766
767 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400768 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -0400769
770 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
771 BUG_ON(key.objectid != bytenr);
772 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
773
Chris Mason5caf2a02007-04-02 11:20:42 -0400774 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400775 refs = btrfs_extent_refs(l, item);
776 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400777 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500778
Chris Mason5caf2a02007-04-02 11:20:42 -0400779 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500780
Chris Mason3c12ac72008-04-21 12:01:38 -0400781 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -0400782 ret = insert_extent_backref(trans, root->fs_info->extent_root,
783 path, bytenr, parent,
784 ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400785 owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500786 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400787 finish_current_insert(trans, root->fs_info->extent_root, 0);
788 del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Mason74493f72007-12-11 09:25:06 -0500789
790 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500791 return 0;
792}
793
Chris Mason925baed2008-06-25 16:01:30 -0400794int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400795 struct btrfs_root *root,
796 u64 bytenr, u64 num_bytes, u64 parent,
797 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400798 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -0400799{
800 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400801 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
802 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
803 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400804 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
805 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400806 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -0400807 return ret;
808}
809
Chris Masone9d0b132007-08-10 14:06:19 -0400810int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
811 struct btrfs_root *root)
812{
Chris Mason87ef2bb2008-10-30 11:23:27 -0400813 finish_current_insert(trans, root->fs_info->extent_root, 1);
814 del_pending_extents(trans, root->fs_info->extent_root, 1);
Chris Masone9d0b132007-08-10 14:06:19 -0400815 return 0;
816}
817
Zheng Yan31840ae2008-09-23 13:14:14 -0400818int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
819 struct btrfs_root *root, u64 bytenr,
820 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500821{
Chris Mason5caf2a02007-04-02 11:20:42 -0400822 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500823 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400824 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400825 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400826 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400827
Chris Masondb945352007-10-15 16:15:53 -0400828 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400829 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400830 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400831 key.objectid = bytenr;
832 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400833 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400834 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400835 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400836 if (ret < 0)
837 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400838 if (ret != 0) {
839 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400840 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500841 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400842 }
843 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400844 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400845 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400846out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400847 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500848 return 0;
849}
850
Yan Zheng80ff3852008-10-30 14:20:02 -0400851int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
852 struct btrfs_root *root, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -0500853{
854 struct btrfs_root *extent_root = root->fs_info->extent_root;
855 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -0400856 struct extent_buffer *leaf;
857 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -0500858 struct btrfs_key key;
859 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -0400860 u64 ref_root;
861 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -0400862 u32 nritems;
863 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500864
Chris Masonbe20aa92007-12-17 20:14:01 -0500865 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400866 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -0400867 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -0500868
Yan Zhengf321e492008-07-30 09:26:11 -0400869 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -0500870 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
871 if (ret < 0)
872 goto out;
873 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -0400874
875 ret = -ENOENT;
876 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -0400877 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500878
Zheng Yan31840ae2008-09-23 13:14:14 -0400879 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -0400880 leaf = path->nodes[0];
881 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500882
883 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -0400884 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500885 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500886
Yan Zheng80ff3852008-10-30 14:20:02 -0400887 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -0500888 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -0400889 leaf = path->nodes[0];
890 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500891 if (path->slots[0] >= nritems) {
892 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -0400893 if (ret < 0)
894 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500895 if (ret == 0)
896 continue;
897 break;
898 }
Yan Zhengf321e492008-07-30 09:26:11 -0400899 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500900 if (found_key.objectid != bytenr)
901 break;
Chris Masonbd098352008-01-03 13:23:19 -0500902
Chris Masonbe20aa92007-12-17 20:14:01 -0500903 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
904 path->slots[0]++;
905 continue;
906 }
907
Yan Zhengf321e492008-07-30 09:26:11 -0400908 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -0500909 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -0400910 ref_root = btrfs_ref_root(leaf, ref_item);
911 if (ref_root != root->root_key.objectid &&
912 ref_root != BTRFS_TREE_LOG_OBJECTID) {
913 ret = 1;
914 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -0400915 }
Yan Zheng80ff3852008-10-30 14:20:02 -0400916 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
917 ret = 1;
918 goto out;
919 }
Yan Zhengf321e492008-07-30 09:26:11 -0400920
Chris Masonbe20aa92007-12-17 20:14:01 -0500921 path->slots[0]++;
922 }
Yan Zhengf321e492008-07-30 09:26:11 -0400923 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500924out:
Yan Zhengf321e492008-07-30 09:26:11 -0400925 btrfs_free_path(path);
926 return ret;
927}
928
Zheng Yan31840ae2008-09-23 13:14:14 -0400929int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
930 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -0500931{
Chris Mason5f39d392007-10-15 16:14:19 -0400932 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400933 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -0400934 u64 root_gen;
935 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -0500936 int i;
Chris Masondb945352007-10-15 16:15:53 -0400937 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -0400938 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -0400939 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500940
Chris Mason3768f362007-03-13 16:47:54 -0400941 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500942 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400943
Zheng Yane4657682008-09-26 10:04:53 -0400944 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
945 shared = 0;
946 root_gen = root->root_key.offset;
947 } else {
948 shared = 1;
949 root_gen = trans->transid - 1;
950 }
951
Chris Masondb945352007-10-15 16:15:53 -0400952 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400953 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -0400954
Zheng Yan31840ae2008-09-23 13:14:14 -0400955 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -0400956 struct btrfs_leaf_ref *ref;
957 struct btrfs_extent_info *info;
958
Zheng Yan31840ae2008-09-23 13:14:14 -0400959 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -0400960 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400961 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -0400962 goto out;
963 }
964
Zheng Yane4657682008-09-26 10:04:53 -0400965 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -0400966 ref->bytenr = buf->start;
967 ref->owner = btrfs_header_owner(buf);
968 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400969 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -0400970 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -0400971
Zheng Yan31840ae2008-09-23 13:14:14 -0400972 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -0400973 u64 disk_bytenr;
974 btrfs_item_key_to_cpu(buf, &key, i);
975 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
976 continue;
977 fi = btrfs_item_ptr(buf, i,
978 struct btrfs_file_extent_item);
979 if (btrfs_file_extent_type(buf, fi) ==
980 BTRFS_FILE_EXTENT_INLINE)
981 continue;
982 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
983 if (disk_bytenr == 0)
984 continue;
985
986 info->bytenr = disk_bytenr;
987 info->num_bytes =
988 btrfs_file_extent_disk_num_bytes(buf, fi);
989 info->objectid = key.objectid;
990 info->offset = key.offset;
991 info++;
992 }
993
Zheng Yane4657682008-09-26 10:04:53 -0400994 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -0400995 if (ret == -EEXIST && shared) {
996 struct btrfs_leaf_ref *old;
997 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
998 BUG_ON(!old);
999 btrfs_remove_leaf_ref(root, old);
1000 btrfs_free_leaf_ref(root, old);
1001 ret = btrfs_add_leaf_ref(root, ref, shared);
1002 }
Yan Zheng31153d82008-07-28 15:32:19 -04001003 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001004 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001005 }
1006out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001007 return ret;
1008}
1009
1010int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1011 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1012 u32 *nr_extents)
1013{
1014 u64 bytenr;
1015 u64 ref_root;
1016 u64 orig_root;
1017 u64 ref_generation;
1018 u64 orig_generation;
1019 u32 nritems;
1020 u32 nr_file_extents = 0;
1021 struct btrfs_key key;
1022 struct btrfs_file_extent_item *fi;
1023 int i;
1024 int level;
1025 int ret = 0;
1026 int faili = 0;
1027 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001028 u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001029
1030 ref_root = btrfs_header_owner(buf);
1031 ref_generation = btrfs_header_generation(buf);
1032 orig_root = btrfs_header_owner(orig_buf);
1033 orig_generation = btrfs_header_generation(orig_buf);
1034
1035 nritems = btrfs_header_nritems(buf);
1036 level = btrfs_header_level(buf);
1037
1038 if (root->ref_cows) {
1039 process_func = __btrfs_inc_extent_ref;
1040 } else {
1041 if (level == 0 &&
1042 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1043 goto out;
1044 if (level != 0 &&
1045 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1046 goto out;
1047 process_func = __btrfs_update_extent_ref;
1048 }
1049
1050 for (i = 0; i < nritems; i++) {
1051 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001052 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001053 btrfs_item_key_to_cpu(buf, &key, i);
1054 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001055 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001056 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001057 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001058 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001059 BTRFS_FILE_EXTENT_INLINE)
1060 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001061 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1062 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001063 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001064
1065 nr_file_extents++;
1066
Zheng Yan31840ae2008-09-23 13:14:14 -04001067 ret = process_func(trans, root, bytenr,
1068 orig_buf->start, buf->start,
1069 orig_root, ref_root,
1070 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001071 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001072
1073 if (ret) {
1074 faili = i;
1075 WARN_ON(1);
1076 goto fail;
1077 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001078 } else {
Chris Masondb945352007-10-15 16:15:53 -04001079 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001080 ret = process_func(trans, root, bytenr,
1081 orig_buf->start, buf->start,
1082 orig_root, ref_root,
1083 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001084 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001085 if (ret) {
1086 faili = i;
1087 WARN_ON(1);
1088 goto fail;
1089 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001090 }
1091 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001092out:
1093 if (nr_extents) {
1094 if (level == 0)
1095 *nr_extents = nr_file_extents;
1096 else
1097 *nr_extents = nritems;
1098 }
1099 return 0;
1100fail:
1101 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001102 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001103}
1104
Zheng Yan31840ae2008-09-23 13:14:14 -04001105int btrfs_update_ref(struct btrfs_trans_handle *trans,
1106 struct btrfs_root *root, struct extent_buffer *orig_buf,
1107 struct extent_buffer *buf, int start_slot, int nr)
1108
1109{
1110 u64 bytenr;
1111 u64 ref_root;
1112 u64 orig_root;
1113 u64 ref_generation;
1114 u64 orig_generation;
1115 struct btrfs_key key;
1116 struct btrfs_file_extent_item *fi;
1117 int i;
1118 int ret;
1119 int slot;
1120 int level;
1121
1122 BUG_ON(start_slot < 0);
1123 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1124
1125 ref_root = btrfs_header_owner(buf);
1126 ref_generation = btrfs_header_generation(buf);
1127 orig_root = btrfs_header_owner(orig_buf);
1128 orig_generation = btrfs_header_generation(orig_buf);
1129 level = btrfs_header_level(buf);
1130
1131 if (!root->ref_cows) {
1132 if (level == 0 &&
1133 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1134 return 0;
1135 if (level != 0 &&
1136 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1137 return 0;
1138 }
1139
1140 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1141 cond_resched();
1142 if (level == 0) {
1143 btrfs_item_key_to_cpu(buf, &key, slot);
1144 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1145 continue;
1146 fi = btrfs_item_ptr(buf, slot,
1147 struct btrfs_file_extent_item);
1148 if (btrfs_file_extent_type(buf, fi) ==
1149 BTRFS_FILE_EXTENT_INLINE)
1150 continue;
1151 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1152 if (bytenr == 0)
1153 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001154 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1155 orig_buf->start, buf->start,
1156 orig_root, ref_root,
1157 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001158 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001159 if (ret)
1160 goto fail;
1161 } else {
1162 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001163 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1164 orig_buf->start, buf->start,
1165 orig_root, ref_root,
1166 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001167 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001168 if (ret)
1169 goto fail;
1170 }
1171 }
1172 return 0;
1173fail:
1174 WARN_ON(1);
1175 return -1;
1176}
1177
Chris Mason9078a3e2007-04-26 16:46:15 -04001178static int write_one_cache_group(struct btrfs_trans_handle *trans,
1179 struct btrfs_root *root,
1180 struct btrfs_path *path,
1181 struct btrfs_block_group_cache *cache)
1182{
1183 int ret;
1184 int pending_ret;
1185 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001186 unsigned long bi;
1187 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001188
Chris Mason9078a3e2007-04-26 16:46:15 -04001189 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001190 if (ret < 0)
1191 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001192 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001193
1194 leaf = path->nodes[0];
1195 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1196 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1197 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001198 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001199fail:
Chris Mason87ef2bb2008-10-30 11:23:27 -04001200 finish_current_insert(trans, extent_root, 0);
1201 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001202 if (ret)
1203 return ret;
1204 if (pending_ret)
1205 return pending_ret;
1206 return 0;
1207
1208}
1209
Chris Mason96b51792007-10-15 16:15:19 -04001210int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1211 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001212{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001213 struct btrfs_block_group_cache *cache, *entry;
1214 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001215 int err = 0;
1216 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001217 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001218 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001219
1220 path = btrfs_alloc_path();
1221 if (!path)
1222 return -ENOMEM;
1223
1224 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001225 cache = NULL;
1226 spin_lock(&root->fs_info->block_group_cache_lock);
1227 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1228 n; n = rb_next(n)) {
1229 entry = rb_entry(n, struct btrfs_block_group_cache,
1230 cache_node);
1231 if (entry->dirty) {
1232 cache = entry;
1233 break;
1234 }
1235 }
1236 spin_unlock(&root->fs_info->block_group_cache_lock);
1237
1238 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001239 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001240
Zheng Yane8569812008-09-26 10:05:48 -04001241 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001242 last += cache->key.offset;
1243
Chris Mason96b51792007-10-15 16:15:19 -04001244 err = write_one_cache_group(trans, root,
1245 path, cache);
1246 /*
1247 * if we fail to write the cache group, we want
1248 * to keep it marked dirty in hopes that a later
1249 * write will work
1250 */
1251 if (err) {
1252 werr = err;
1253 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001254 }
1255 }
1256 btrfs_free_path(path);
1257 return werr;
1258}
1259
Chris Mason593060d2008-03-25 16:50:33 -04001260static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1261 u64 total_bytes, u64 bytes_used,
1262 struct btrfs_space_info **space_info)
1263{
1264 struct btrfs_space_info *found;
1265
1266 found = __find_space_info(info, flags);
1267 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001268 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001269 found->total_bytes += total_bytes;
1270 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001271 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001272 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001273 *space_info = found;
1274 return 0;
1275 }
1276 found = kmalloc(sizeof(*found), GFP_NOFS);
1277 if (!found)
1278 return -ENOMEM;
1279
1280 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001281 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001282 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001283 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001284 found->flags = flags;
1285 found->total_bytes = total_bytes;
1286 found->bytes_used = bytes_used;
1287 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001288 found->bytes_reserved = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001289 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001290 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001291 *space_info = found;
1292 return 0;
1293}
1294
Chris Mason8790d502008-04-03 16:29:03 -04001295static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1296{
1297 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001298 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001299 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001300 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001301 if (extra_flags) {
1302 if (flags & BTRFS_BLOCK_GROUP_DATA)
1303 fs_info->avail_data_alloc_bits |= extra_flags;
1304 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1305 fs_info->avail_metadata_alloc_bits |= extra_flags;
1306 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1307 fs_info->avail_system_alloc_bits |= extra_flags;
1308 }
1309}
Chris Mason593060d2008-03-25 16:50:33 -04001310
Chris Masona061fc82008-05-07 11:43:44 -04001311static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001312{
Chris Masona061fc82008-05-07 11:43:44 -04001313 u64 num_devices = root->fs_info->fs_devices->num_devices;
1314
1315 if (num_devices == 1)
1316 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1317 if (num_devices < 4)
1318 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1319
Chris Masonec44a352008-04-28 15:29:52 -04001320 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1321 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001322 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001323 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001324 }
Chris Masonec44a352008-04-28 15:29:52 -04001325
1326 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001327 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001328 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001329 }
Chris Masonec44a352008-04-28 15:29:52 -04001330
1331 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1332 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1333 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1334 (flags & BTRFS_BLOCK_GROUP_DUP)))
1335 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1336 return flags;
1337}
1338
Chris Mason6324fbf2008-03-24 15:01:59 -04001339static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1340 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001341 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001342{
1343 struct btrfs_space_info *space_info;
1344 u64 thresh;
1345 u64 start;
1346 u64 num_bytes;
Josef Bacikcf749822008-10-01 19:11:18 -04001347 int ret = 0, waited = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001348
Chris Masona061fc82008-05-07 11:43:44 -04001349 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001350
Chris Mason6324fbf2008-03-24 15:01:59 -04001351 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001352 if (!space_info) {
1353 ret = update_space_info(extent_root->fs_info, flags,
1354 0, 0, &space_info);
1355 BUG_ON(ret);
1356 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001357 BUG_ON(!space_info);
1358
Josef Bacik25179202008-10-29 14:49:05 -04001359 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001360 if (space_info->force_alloc) {
1361 force = 1;
1362 space_info->force_alloc = 0;
1363 }
Josef Bacik25179202008-10-29 14:49:05 -04001364 if (space_info->full) {
1365 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001366 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001367 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001368
Chris Mason8790d502008-04-03 16:29:03 -04001369 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001370 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001371 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001372 space_info->bytes_reserved + alloc_bytes) < thresh) {
1373 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001374 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001375 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001376
Josef Bacik25179202008-10-29 14:49:05 -04001377 spin_unlock(&space_info->lock);
1378
1379 ret = mutex_trylock(&extent_root->fs_info->chunk_mutex);
1380 if (!ret && !force) {
1381 goto out;
1382 } else if (!ret) {
1383 mutex_lock(&extent_root->fs_info->chunk_mutex);
Josef Bacikcf749822008-10-01 19:11:18 -04001384 waited = 1;
1385 }
1386
Josef Bacik25179202008-10-29 14:49:05 -04001387 if (waited) {
1388 spin_lock(&space_info->lock);
1389 if (space_info->full) {
1390 spin_unlock(&space_info->lock);
1391 goto out_unlock;
1392 }
1393 spin_unlock(&space_info->lock);
1394 }
Josef Bacikcf749822008-10-01 19:11:18 -04001395
Chris Mason6324fbf2008-03-24 15:01:59 -04001396 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
Josef Bacik25179202008-10-29 14:49:05 -04001397 if (ret) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001398printk("space info full %Lu\n", flags);
1399 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001400 goto out_unlock;
Chris Mason6324fbf2008-03-24 15:01:59 -04001401 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001402
1403 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001404 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001405 BUG_ON(ret);
Chris Masona74a4b92008-06-25 16:01:31 -04001406out_unlock:
Chris Mason333db942008-06-25 16:01:30 -04001407 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001408out:
Josef Bacik0f9dd462008-09-23 13:14:11 -04001409 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001410}
1411
Chris Mason9078a3e2007-04-26 16:46:15 -04001412static int update_block_group(struct btrfs_trans_handle *trans,
1413 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001414 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001415 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001416{
1417 struct btrfs_block_group_cache *cache;
1418 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001419 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001420 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001421 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001422
Chris Mason9078a3e2007-04-26 16:46:15 -04001423 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001424 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001425 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001426 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001427 }
Chris Masondb945352007-10-15 16:15:53 -04001428 byte_in_group = bytenr - cache->key.objectid;
1429 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001430
Josef Bacik25179202008-10-29 14:49:05 -04001431 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001432 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001433 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001434 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001435 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001436 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001437 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001438 cache->space_info->bytes_used += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001439 btrfs_set_block_group_used(&cache->item, old_val);
1440 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001441 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001442 } else {
Chris Masondb945352007-10-15 16:15:53 -04001443 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001444 cache->space_info->bytes_used -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001445 btrfs_set_block_group_used(&cache->item, old_val);
1446 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001447 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001448 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001449 int ret;
1450 ret = btrfs_add_free_space(cache, bytenr,
1451 num_bytes);
1452 if (ret)
1453 return -1;
Chris Masone37c9e62007-05-09 20:13:14 -04001454 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001455 }
Chris Masondb945352007-10-15 16:15:53 -04001456 total -= num_bytes;
1457 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001458 }
1459 return 0;
1460}
Chris Mason6324fbf2008-03-24 15:01:59 -04001461
Chris Masona061fc82008-05-07 11:43:44 -04001462static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1463{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001464 struct btrfs_block_group_cache *cache;
1465
1466 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1467 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001468 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001469
1470 return cache->key.objectid;
Chris Masona061fc82008-05-07 11:43:44 -04001471}
1472
Chris Masone02119d2008-09-05 16:13:11 -04001473int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05001474 u64 bytenr, u64 num, int pin)
1475{
1476 u64 len;
1477 struct btrfs_block_group_cache *cache;
1478 struct btrfs_fs_info *fs_info = root->fs_info;
1479
Josef Bacik25179202008-10-29 14:49:05 -04001480 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05001481 if (pin) {
1482 set_extent_dirty(&fs_info->pinned_extents,
1483 bytenr, bytenr + num - 1, GFP_NOFS);
1484 } else {
1485 clear_extent_dirty(&fs_info->pinned_extents,
1486 bytenr, bytenr + num - 1, GFP_NOFS);
1487 }
1488 while (num > 0) {
1489 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04001490 BUG_ON(!cache);
1491 len = min(num, cache->key.offset -
1492 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05001493 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04001494 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04001495 spin_lock(&cache->lock);
1496 cache->pinned += len;
1497 cache->space_info->bytes_pinned += len;
1498 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001499 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05001500 fs_info->total_pinned += len;
1501 } else {
Josef Bacik25179202008-10-29 14:49:05 -04001502 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04001503 spin_lock(&cache->lock);
1504 cache->pinned -= len;
1505 cache->space_info->bytes_pinned -= len;
1506 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001507 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05001508 fs_info->total_pinned -= len;
1509 }
1510 bytenr += len;
1511 num -= len;
1512 }
1513 return 0;
1514}
Chris Mason9078a3e2007-04-26 16:46:15 -04001515
Zheng Yane8569812008-09-26 10:05:48 -04001516static int update_reserved_extents(struct btrfs_root *root,
1517 u64 bytenr, u64 num, int reserve)
1518{
1519 u64 len;
1520 struct btrfs_block_group_cache *cache;
1521 struct btrfs_fs_info *fs_info = root->fs_info;
1522
Zheng Yane8569812008-09-26 10:05:48 -04001523 while (num > 0) {
1524 cache = btrfs_lookup_block_group(fs_info, bytenr);
1525 BUG_ON(!cache);
1526 len = min(num, cache->key.offset -
1527 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04001528
1529 spin_lock(&cache->space_info->lock);
1530 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04001531 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04001532 cache->reserved += len;
1533 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04001534 } else {
Zheng Yane8569812008-09-26 10:05:48 -04001535 cache->reserved -= len;
1536 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04001537 }
Josef Bacik25179202008-10-29 14:49:05 -04001538 spin_unlock(&cache->lock);
1539 spin_unlock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04001540 bytenr += len;
1541 num -= len;
1542 }
1543 return 0;
1544}
1545
Chris Masond1310b22008-01-24 16:13:08 -05001546int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001547{
Chris Masonccd467d2007-06-28 15:57:36 -04001548 u64 last = 0;
Chris Mason1a5bc1672007-10-15 16:15:26 -04001549 u64 start;
1550 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001551 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001552 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001553
Josef Bacik25179202008-10-29 14:49:05 -04001554 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04001555 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04001556 ret = find_first_extent_bit(pinned_extents, last,
1557 &start, &end, EXTENT_DIRTY);
1558 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001559 break;
Chris Mason1a5bc1672007-10-15 16:15:26 -04001560 set_extent_dirty(copy, start, end, GFP_NOFS);
1561 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001562 }
Josef Bacik25179202008-10-29 14:49:05 -04001563 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04001564 return 0;
1565}
1566
1567int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1568 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001569 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001570{
Chris Mason1a5bc1672007-10-15 16:15:26 -04001571 u64 start;
1572 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001573 int ret;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001574 struct btrfs_block_group_cache *cache;
Chris Masona28ec192007-03-06 20:08:01 -05001575
Josef Bacik25179202008-10-29 14:49:05 -04001576 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001577 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04001578 ret = find_first_extent_bit(unpin, 0, &start, &end,
1579 EXTENT_DIRTY);
1580 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001581 break;
Chris Masone02119d2008-09-05 16:13:11 -04001582 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc1672007-10-15 16:15:26 -04001583 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001584 cache = btrfs_lookup_block_group(root->fs_info, start);
1585 if (cache->cached)
1586 btrfs_add_free_space(cache, start, end - start + 1);
Chris Masonc286ac42008-07-22 23:06:41 -04001587 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04001588 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04001589 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04001590 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04001591 }
Chris Masona28ec192007-03-06 20:08:01 -05001592 }
Josef Bacik25179202008-10-29 14:49:05 -04001593 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001594 return 0;
1595}
1596
Chris Mason98ed5172008-01-03 10:01:48 -05001597static int finish_current_insert(struct btrfs_trans_handle *trans,
Chris Mason87ef2bb2008-10-30 11:23:27 -04001598 struct btrfs_root *extent_root, int all)
Chris Mason037e6392007-03-07 11:50:24 -05001599{
Chris Mason7bb86312007-12-11 09:25:06 -05001600 u64 start;
1601 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04001602 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04001603 u64 search = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001604 struct btrfs_fs_info *info = extent_root->fs_info;
1605 struct btrfs_path *path;
Zheng Yan31840ae2008-09-23 13:14:14 -04001606 struct btrfs_extent_ref *ref;
1607 struct pending_extent_op *extent_op;
1608 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -04001609 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001610 int ret;
Chris Mason1a5bc1672007-10-15 16:15:26 -04001611 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001612
Chris Mason5f39d392007-10-15 16:14:19 -04001613 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001614 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001615
Chris Mason26b80032007-08-08 20:17:12 -04001616 while(1) {
Josef Bacik25179202008-10-29 14:49:05 -04001617 mutex_lock(&info->extent_ins_mutex);
1618 ret = find_first_extent_bit(&info->extent_ins, search, &start,
1619 &end, EXTENT_WRITEBACK);
1620 if (ret) {
1621 mutex_unlock(&info->extent_ins_mutex);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001622 if (search && all) {
Josef Bacik25179202008-10-29 14:49:05 -04001623 search = 0;
1624 continue;
1625 }
Chris Mason26b80032007-08-08 20:17:12 -04001626 break;
Josef Bacik25179202008-10-29 14:49:05 -04001627 }
1628
1629 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
1630 if (!ret) {
Chris Mason87ef2bb2008-10-30 11:23:27 -04001631 search = end + 1;
Josef Bacik25179202008-10-29 14:49:05 -04001632 mutex_unlock(&info->extent_ins_mutex);
1633 cond_resched();
1634 continue;
1635 }
1636 BUG_ON(ret < 0);
Chris Mason26b80032007-08-08 20:17:12 -04001637
Zheng Yan31840ae2008-09-23 13:14:14 -04001638 ret = get_state_private(&info->extent_ins, start, &priv);
1639 BUG_ON(ret);
1640 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1641
Josef Bacik25179202008-10-29 14:49:05 -04001642 mutex_unlock(&info->extent_ins_mutex);
1643
Zheng Yan31840ae2008-09-23 13:14:14 -04001644 if (extent_op->type == PENDING_EXTENT_INSERT) {
1645 key.objectid = start;
1646 key.offset = end + 1 - start;
1647 key.type = BTRFS_EXTENT_ITEM_KEY;
1648 err = btrfs_insert_item(trans, extent_root, &key,
Chris Mason1a5bc1672007-10-15 16:15:26 -04001649 &extent_item, sizeof(extent_item));
Zheng Yan31840ae2008-09-23 13:14:14 -04001650 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001651
Josef Bacik25179202008-10-29 14:49:05 -04001652 mutex_lock(&info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001653 clear_extent_bits(&info->extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04001654 EXTENT_WRITEBACK, GFP_NOFS);
1655 mutex_unlock(&info->extent_ins_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04001656
Zheng Yan31840ae2008-09-23 13:14:14 -04001657 err = insert_extent_backref(trans, extent_root, path,
1658 start, extent_op->parent,
1659 extent_root->root_key.objectid,
1660 extent_op->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001661 extent_op->level);
Zheng Yan31840ae2008-09-23 13:14:14 -04001662 BUG_ON(err);
1663 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1664 err = lookup_extent_backref(trans, extent_root, path,
1665 start, extent_op->orig_parent,
1666 extent_root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001667 extent_op->orig_generation,
1668 extent_op->level, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001669 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001670
Josef Bacik25179202008-10-29 14:49:05 -04001671 mutex_lock(&info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001672 clear_extent_bits(&info->extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04001673 EXTENT_WRITEBACK, GFP_NOFS);
1674 mutex_unlock(&info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001675
1676 key.objectid = start;
1677 key.offset = extent_op->parent;
1678 key.type = BTRFS_EXTENT_REF_KEY;
1679 err = btrfs_set_item_key_safe(trans, extent_root, path,
1680 &key);
1681 BUG_ON(err);
1682 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1683 struct btrfs_extent_ref);
1684 btrfs_set_ref_generation(path->nodes[0], ref,
1685 extent_op->generation);
1686 btrfs_mark_buffer_dirty(path->nodes[0]);
1687 btrfs_release_path(extent_root, path);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001688 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001689 BUG_ON(1);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001690 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001691 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04001692 unlock_extent(&info->extent_ins, start, end, GFP_NOFS);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001693 if (all)
1694 search = 0;
1695 else
1696 search = end + 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001697
Josef Bacik25179202008-10-29 14:49:05 -04001698 cond_resched();
Chris Mason037e6392007-03-07 11:50:24 -05001699 }
Chris Mason7bb86312007-12-11 09:25:06 -05001700 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001701 return 0;
1702}
1703
Zheng Yan31840ae2008-09-23 13:14:14 -04001704static int pin_down_bytes(struct btrfs_trans_handle *trans,
1705 struct btrfs_root *root,
1706 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04001707{
Chris Mason1a5bc1672007-10-15 16:15:26 -04001708 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001709 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001710
Zheng Yan31840ae2008-09-23 13:14:14 -04001711 if (is_data)
1712 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001713
Zheng Yan31840ae2008-09-23 13:14:14 -04001714 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1715 if (!buf)
1716 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001717
Zheng Yan31840ae2008-09-23 13:14:14 -04001718 /* we can reuse a block if it hasn't been written
1719 * and it is from this transaction. We can't
1720 * reuse anything from the tree log root because
1721 * it has tiny sub-transactions.
1722 */
1723 if (btrfs_buffer_uptodate(buf, 0) &&
1724 btrfs_try_tree_lock(buf)) {
1725 u64 header_owner = btrfs_header_owner(buf);
1726 u64 header_transid = btrfs_header_generation(buf);
1727 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04001728 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04001729 header_transid == trans->transid &&
1730 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1731 clean_tree_block(NULL, root, buf);
1732 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001733 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001734 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04001735 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001736 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001737 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001738 free_extent_buffer(buf);
1739pinit:
1740 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
1741
Chris Masonbe744172007-05-06 10:15:01 -04001742 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001743 return 0;
1744}
1745
Chris Masona28ec192007-03-06 20:08:01 -05001746/*
1747 * remove an extent from the root, returns 0 on success
1748 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001749static int __free_extent(struct btrfs_trans_handle *trans,
1750 struct btrfs_root *root,
1751 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05001752 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001753 u64 owner_objectid, int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001754{
Chris Mason5caf2a02007-04-02 11:20:42 -04001755 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001756 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001757 struct btrfs_fs_info *info = root->fs_info;
1758 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001759 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001760 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001761 int extent_slot = 0;
1762 int found_extent = 0;
1763 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001764 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001765 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001766
Chris Masondb945352007-10-15 16:15:53 -04001767 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001768 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001769 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001770 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001771 if (!path)
1772 return -ENOMEM;
1773
Chris Mason3c12ac72008-04-21 12:01:38 -04001774 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001775 ret = lookup_extent_backref(trans, extent_root, path,
1776 bytenr, parent, root_objectid,
1777 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001778 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001779 struct btrfs_key found_key;
1780 extent_slot = path->slots[0];
1781 while(extent_slot > 0) {
1782 extent_slot--;
1783 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1784 extent_slot);
1785 if (found_key.objectid != bytenr)
1786 break;
1787 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1788 found_key.offset == num_bytes) {
1789 found_extent = 1;
1790 break;
1791 }
1792 if (path->slots[0] - extent_slot > 5)
1793 break;
1794 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001795 if (!found_extent) {
1796 ret = remove_extent_backref(trans, extent_root, path);
1797 BUG_ON(ret);
1798 btrfs_release_path(extent_root, path);
1799 ret = btrfs_search_slot(trans, extent_root,
1800 &key, path, -1, 1);
1801 BUG_ON(ret);
1802 extent_slot = path->slots[0];
1803 }
Chris Mason7bb86312007-12-11 09:25:06 -05001804 } else {
1805 btrfs_print_leaf(extent_root, path->nodes[0]);
1806 WARN_ON(1);
1807 printk("Unable to find ref byte nr %Lu root %Lu "
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001808 "gen %Lu owner %Lu\n", bytenr,
1809 root_objectid, ref_generation, owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05001810 }
Chris Mason5f39d392007-10-15 16:14:19 -04001811
1812 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001813 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001814 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001815 refs = btrfs_extent_refs(leaf, ei);
1816 BUG_ON(refs == 0);
1817 refs -= 1;
1818 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001819
Chris Mason5f39d392007-10-15 16:14:19 -04001820 btrfs_mark_buffer_dirty(leaf);
1821
Chris Mason952fcca2008-02-18 16:33:44 -05001822 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001823 struct btrfs_extent_ref *ref;
1824 ref = btrfs_item_ptr(leaf, path->slots[0],
1825 struct btrfs_extent_ref);
1826 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001827 /* if the back ref and the extent are next to each other
1828 * they get deleted below in one shot
1829 */
1830 path->slots[0] = extent_slot;
1831 num_to_del = 2;
1832 } else if (found_extent) {
1833 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001834 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05001835 BUG_ON(ret);
1836 /* if refs are 0, we need to setup the path for deletion */
1837 if (refs == 0) {
1838 btrfs_release_path(extent_root, path);
1839 ret = btrfs_search_slot(trans, extent_root, &key, path,
1840 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001841 BUG_ON(ret);
1842 }
1843 }
1844
Chris Masoncf27e1e2007-03-13 09:49:06 -04001845 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001846 u64 super_used;
1847 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01001848#ifdef BIO_RW_DISCARD
1849 u64 map_length = num_bytes;
1850 struct btrfs_multi_bio *multi = NULL;
1851#endif
Chris Mason78fae272007-03-25 11:35:08 -04001852
1853 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04001854 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001855 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
1856 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04001857 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05001858 if (ret > 0)
1859 mark_free = 1;
1860 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001861 }
1862
Josef Bacik58176a92007-08-29 15:47:34 -04001863 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04001864 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04001865 super_used = btrfs_super_bytes_used(&info->super_copy);
1866 btrfs_set_super_bytes_used(&info->super_copy,
1867 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04001868 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04001869
1870 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001871 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001872 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001873 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001874 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1875 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04001876 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04001877 btrfs_release_path(extent_root, path);
Chris Masondb945352007-10-15 16:15:53 -04001878 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001879 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001880 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01001881
1882#ifdef BIO_RW_DISCARD
1883 /* Tell the block device(s) that the sectors can be discarded */
1884 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1885 bytenr, &map_length, &multi, 0);
1886 if (!ret) {
1887 struct btrfs_bio_stripe *stripe = multi->stripes;
1888 int i;
1889
1890 if (map_length > num_bytes)
1891 map_length = num_bytes;
1892
1893 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1894 blkdev_issue_discard(stripe->dev->bdev,
1895 stripe->physical >> 9,
1896 map_length >> 9);
1897 }
1898 kfree(multi);
1899 }
1900#endif
Chris Masona28ec192007-03-06 20:08:01 -05001901 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001902 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001903 finish_current_insert(trans, extent_root, 0);
Chris Masona28ec192007-03-06 20:08:01 -05001904 return ret;
1905}
1906
1907/*
Chris Masonfec577f2007-02-26 10:40:21 -05001908 * find all the blocks marked as pending in the radix tree and remove
1909 * them from the extent map
1910 */
Chris Masone089f052007-03-16 16:20:31 -04001911static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -04001912 btrfs_root *extent_root, int all)
Chris Masonfec577f2007-02-26 10:40:21 -05001913{
1914 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001915 int err = 0;
Chris Mason1a5bc1672007-10-15 16:15:26 -04001916 u64 start;
1917 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04001918 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04001919 u64 search = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001920 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04001921 struct extent_io_tree *extent_ins;
1922 struct pending_extent_op *extent_op;
Josef Bacik25179202008-10-29 14:49:05 -04001923 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason8ef97622007-03-26 10:15:30 -04001924
Zheng Yan31840ae2008-09-23 13:14:14 -04001925 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc1672007-10-15 16:15:26 -04001926 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05001927
1928 while(1) {
Josef Bacik25179202008-10-29 14:49:05 -04001929 mutex_lock(&info->extent_ins_mutex);
1930 ret = find_first_extent_bit(pending_del, search, &start, &end,
1931 EXTENT_WRITEBACK);
1932 if (ret) {
1933 mutex_unlock(&info->extent_ins_mutex);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001934 if (all && search) {
Josef Bacik25179202008-10-29 14:49:05 -04001935 search = 0;
1936 continue;
1937 }
Chris Masonfec577f2007-02-26 10:40:21 -05001938 break;
Josef Bacik25179202008-10-29 14:49:05 -04001939 }
1940
1941 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
1942 if (!ret) {
1943 search = end+1;
1944 mutex_unlock(&info->extent_ins_mutex);
1945 cond_resched();
1946 continue;
1947 }
1948 BUG_ON(ret < 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001949
1950 ret = get_state_private(pending_del, start, &priv);
1951 BUG_ON(ret);
1952 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1953
Josef Bacik25179202008-10-29 14:49:05 -04001954 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
Chris Mason1a5bc1672007-10-15 16:15:26 -04001955 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04001956 if (!test_range_bit(extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04001957 EXTENT_WRITEBACK, 0)) {
1958 mutex_unlock(&info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001959free_extent:
Chris Masonc286ac42008-07-22 23:06:41 -04001960 ret = __free_extent(trans, extent_root,
Zheng Yan31840ae2008-09-23 13:14:14 -04001961 start, end + 1 - start,
1962 extent_op->orig_parent,
1963 extent_root->root_key.objectid,
1964 extent_op->orig_generation,
Josef Bacik25179202008-10-29 14:49:05 -04001965 extent_op->level, 1, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001966 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04001967 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001968 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04001969
1970 ret = get_state_private(&info->extent_ins, start,
1971 &priv);
Zheng Yan31840ae2008-09-23 13:14:14 -04001972 BUG_ON(ret);
1973 extent_op = (struct pending_extent_op *)
Josef Bacik25179202008-10-29 14:49:05 -04001974 (unsigned long)priv;
Zheng Yan31840ae2008-09-23 13:14:14 -04001975
Josef Bacik25179202008-10-29 14:49:05 -04001976 clear_extent_bits(&info->extent_ins, start, end,
1977 EXTENT_WRITEBACK, GFP_NOFS);
1978
1979 mutex_unlock(&info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001980
1981 if (extent_op->type == PENDING_BACKREF_UPDATE)
1982 goto free_extent;
1983
Josef Bacik25179202008-10-29 14:49:05 -04001984 mutex_lock(&extent_root->fs_info->pinned_mutex);
1985 ret = pin_down_bytes(trans, extent_root, start,
1986 end + 1 - start, 0);
1987 mutex_unlock(&extent_root->fs_info->pinned_mutex);
1988
Zheng Yan31840ae2008-09-23 13:14:14 -04001989 ret = update_block_group(trans, extent_root, start,
Josef Bacik25179202008-10-29 14:49:05 -04001990 end + 1 - start, 0, ret > 0);
1991
Zheng Yan31840ae2008-09-23 13:14:14 -04001992 BUG_ON(ret);
1993 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04001994 }
Chris Mason1a5bc1672007-10-15 16:15:26 -04001995 if (ret)
1996 err = ret;
Josef Bacik25179202008-10-29 14:49:05 -04001997 unlock_extent(extent_ins, start, end, GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04001998
Chris Mason87ef2bb2008-10-30 11:23:27 -04001999 if (all)
2000 search = 0;
2001 else
2002 search = end + 1;
Josef Bacik25179202008-10-29 14:49:05 -04002003 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05002004 }
Chris Masone20d96d2007-03-22 12:13:20 -04002005 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002006}
2007
2008/*
2009 * remove an extent from the root, returns 0 on success
2010 */
Chris Mason925baed2008-06-25 16:01:30 -04002011static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002012 struct btrfs_root *root,
2013 u64 bytenr, u64 num_bytes, u64 parent,
2014 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002015 u64 owner_objectid, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002016{
Chris Mason9f5fae22007-03-20 14:38:32 -04002017 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002018 int pending_ret;
2019 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002020
Chris Masondb945352007-10-15 16:15:53 -04002021 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002022 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002023 struct pending_extent_op *extent_op;
2024
2025 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2026 BUG_ON(!extent_op);
2027
2028 extent_op->type = PENDING_EXTENT_DELETE;
2029 extent_op->bytenr = bytenr;
2030 extent_op->num_bytes = num_bytes;
2031 extent_op->parent = parent;
2032 extent_op->orig_parent = parent;
2033 extent_op->generation = ref_generation;
2034 extent_op->orig_generation = ref_generation;
2035 extent_op->level = (int)owner_objectid;
2036
Josef Bacik25179202008-10-29 14:49:05 -04002037 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002038 set_extent_bits(&root->fs_info->pending_del,
2039 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04002040 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002041 set_state_private(&root->fs_info->pending_del,
2042 bytenr, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002043 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002044 return 0;
2045 }
Chris Mason4bef0842008-09-08 11:18:08 -04002046 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002047 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2048 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002049 struct btrfs_block_group_cache *cache;
2050
Chris Masond00aff02008-09-11 15:54:42 -04002051 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002052 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2053 BUG_ON(!cache);
2054 btrfs_add_free_space(cache, bytenr, num_bytes);
Zheng Yane8569812008-09-26 10:05:48 -04002055 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002056 return 0;
2057 }
Chris Mason4bef0842008-09-08 11:18:08 -04002058 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002059 }
Chris Mason4bef0842008-09-08 11:18:08 -04002060
2061 /* if data pin when any transaction has committed this */
2062 if (ref_generation != trans->transid)
2063 pin = 1;
2064
Zheng Yan31840ae2008-09-23 13:14:14 -04002065 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002066 root_objectid, ref_generation,
2067 owner_objectid, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002068
Chris Mason87ef2bb2008-10-30 11:23:27 -04002069 finish_current_insert(trans, root->fs_info->extent_root, 0);
2070 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002071 return ret ? ret : pending_ret;
2072}
2073
Chris Mason925baed2008-06-25 16:01:30 -04002074int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002075 struct btrfs_root *root,
2076 u64 bytenr, u64 num_bytes, u64 parent,
2077 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002078 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002079{
2080 int ret;
2081
Zheng Yan31840ae2008-09-23 13:14:14 -04002082 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002083 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002084 owner_objectid, pin);
Chris Mason925baed2008-06-25 16:01:30 -04002085 return ret;
2086}
2087
Chris Mason87ee04e2007-11-30 11:30:34 -05002088static u64 stripe_align(struct btrfs_root *root, u64 val)
2089{
2090 u64 mask = ((u64)root->stripesize - 1);
2091 u64 ret = (val + mask) & ~mask;
2092 return ret;
2093}
2094
Chris Masonfec577f2007-02-26 10:40:21 -05002095/*
2096 * walks the btree of allocated extents and find a hole of a given size.
2097 * The key ins is changed to record the hole:
2098 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002099 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002100 * ins->offset == number of blocks
2101 * Any available blocks before search_start are skipped.
2102 */
Chris Mason98ed5172008-01-03 10:01:48 -05002103static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2104 struct btrfs_root *orig_root,
2105 u64 num_bytes, u64 empty_size,
2106 u64 search_start, u64 search_end,
2107 u64 hint_byte, struct btrfs_key *ins,
2108 u64 exclude_start, u64 exclude_nr,
2109 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002110{
Josef Bacik80eb2342008-10-29 14:49:05 -04002111 int ret = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -04002112 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002113 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002114 u64 *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04002115 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002116 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002117 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002118 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002119 struct list_head *head = NULL, *cur = NULL;
2120 int loop = 0;
2121 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002122
Chris Masondb945352007-10-15 16:15:53 -04002123 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002124 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002125 ins->objectid = 0;
2126 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002127
Chris Mason0ef3e662008-05-24 14:04:53 -04002128 if (orig_root->ref_cows || empty_size)
2129 allowed_chunk_alloc = 1;
2130
Chris Mason239b14b2008-03-24 15:02:07 -04002131 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2132 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04002133 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002134 }
2135
Josef Bacik0f9dd462008-09-23 13:14:11 -04002136 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002137 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002138
Chris Mason239b14b2008-03-24 15:02:07 -04002139 if (last_ptr) {
2140 if (*last_ptr)
2141 hint_byte = *last_ptr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002142 else
Chris Mason239b14b2008-03-24 15:02:07 -04002143 empty_size += empty_cluster;
Chris Mason239b14b2008-03-24 15:02:07 -04002144 }
Chris Masona061fc82008-05-07 11:43:44 -04002145 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002146 search_start = max(search_start, hint_byte);
Chris Mason6702ed42007-08-07 16:15:09 -04002147 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002148
Josef Bacik80eb2342008-10-29 14:49:05 -04002149 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
2150 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002151
Josef Bacik80eb2342008-10-29 14:49:05 -04002152 down_read(&space_info->groups_sem);
2153 while (1) {
2154 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002155 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002156 * the only way this happens if our hint points to a block
2157 * group thats not of the proper type, while looping this
2158 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002159 */
Josef Bacik25179202008-10-29 14:49:05 -04002160 WARN_ON(!block_group);
2161 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002162 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002163 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002164
Josef Bacik80eb2342008-10-29 14:49:05 -04002165 ret = cache_block_group(root, block_group);
Josef Bacik25179202008-10-29 14:49:05 -04002166 if (ret) {
2167 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002168 break;
Josef Bacik25179202008-10-29 14:49:05 -04002169 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002170
Josef Bacik80eb2342008-10-29 14:49:05 -04002171 if (block_group->ro)
2172 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002173
Josef Bacik80eb2342008-10-29 14:49:05 -04002174 free_space = btrfs_find_free_space(block_group, search_start,
2175 total_needed);
2176 if (free_space) {
2177 u64 start = block_group->key.objectid;
2178 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002179 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002180
Josef Bacik80eb2342008-10-29 14:49:05 -04002181 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002182
Josef Bacik80eb2342008-10-29 14:49:05 -04002183 /* move on to the next group */
2184 if (search_start + num_bytes >= search_end)
2185 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002186
Josef Bacik80eb2342008-10-29 14:49:05 -04002187 /* move on to the next group */
2188 if (search_start + num_bytes > end)
2189 goto new_group;
2190
2191 if (exclude_nr > 0 &&
2192 (search_start + num_bytes > exclude_start &&
2193 search_start < exclude_start + exclude_nr)) {
2194 search_start = exclude_start + exclude_nr;
2195 /*
2196 * if search_start is still in this block group
2197 * then we just re-search this block group
2198 */
2199 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002200 search_start < end) {
2201 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002202 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002203 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002204
2205 /* else we go to the next block group */
2206 goto new_group;
2207 }
2208
2209 ins->objectid = search_start;
2210 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002211
2212 btrfs_remove_free_space_lock(block_group, search_start,
2213 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002214 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002215 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002216 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002217 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002218new_group:
Josef Bacik25179202008-10-29 14:49:05 -04002219 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002220 /*
2221 * Here's how this works.
2222 * loop == 0: we were searching a block group via a hint
2223 * and didn't find anything, so we start at
2224 * the head of the block groups and keep searching
2225 * loop == 1: we're searching through all of the block groups
2226 * if we hit the head again we have searched
2227 * all of the block groups for this space and we
2228 * need to try and allocate, if we cant error out.
2229 * loop == 2: we allocated more space and are looping through
2230 * all of the block groups again.
2231 */
2232 if (loop == 0) {
2233 head = &space_info->block_groups;
2234 cur = head->next;
Chris Mason0b86a832008-03-24 15:01:56 -04002235
Josef Bacik80eb2342008-10-29 14:49:05 -04002236 if (last_ptr && *last_ptr) {
2237 total_needed += empty_cluster;
2238 *last_ptr = 0;
2239 }
2240 loop++;
2241 } else if (loop == 1 && cur == head) {
2242 if (allowed_chunk_alloc && !chunk_alloc_done) {
2243 up_read(&space_info->groups_sem);
2244 ret = do_chunk_alloc(trans, root, num_bytes +
2245 2 * 1024 * 1024, data, 1);
2246 if (ret < 0)
2247 break;
2248 down_read(&space_info->groups_sem);
2249 loop++;
2250 head = &space_info->block_groups;
2251 cur = head->next;
2252 chunk_alloc_done = 1;
2253 } else if (!allowed_chunk_alloc) {
2254 space_info->force_alloc = 1;
2255 break;
2256 } else {
2257 break;
2258 }
2259 } else if (cur == head) {
2260 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002261 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002262
2263 block_group = list_entry(cur, struct btrfs_block_group_cache,
2264 list);
2265 search_start = block_group->key.objectid;
2266 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002267 }
Chris Mason0b86a832008-03-24 15:01:56 -04002268
Josef Bacik80eb2342008-10-29 14:49:05 -04002269 /* we found what we needed */
2270 if (ins->objectid) {
2271 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2272 trans->block_group = block_group;
2273
2274 if (last_ptr)
2275 *last_ptr = ins->objectid + ins->offset;
2276 ret = 0;
2277 } else if (!ret) {
2278 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04002279 }
Chris Mason0b86a832008-03-24 15:01:56 -04002280
Josef Bacik80eb2342008-10-29 14:49:05 -04002281 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05002282 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002283}
Chris Masonec44a352008-04-28 15:29:52 -04002284
Josef Bacik0f9dd462008-09-23 13:14:11 -04002285static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2286{
2287 struct btrfs_block_group_cache *cache;
2288 struct list_head *l;
2289
2290 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
Zheng Yane8569812008-09-26 10:05:48 -04002291 info->total_bytes - info->bytes_used - info->bytes_pinned -
2292 info->bytes_reserved, (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002293
Josef Bacik80eb2342008-10-29 14:49:05 -04002294 down_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002295 list_for_each(l, &info->block_groups) {
2296 cache = list_entry(l, struct btrfs_block_group_cache, list);
2297 spin_lock(&cache->lock);
2298 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
Zheng Yane8569812008-09-26 10:05:48 -04002299 "%Lu pinned %Lu reserved\n",
Josef Bacik0f9dd462008-09-23 13:14:11 -04002300 cache->key.objectid, cache->key.offset,
Zheng Yane8569812008-09-26 10:05:48 -04002301 btrfs_block_group_used(&cache->item),
2302 cache->pinned, cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002303 btrfs_dump_free_space(cache, bytes);
2304 spin_unlock(&cache->lock);
2305 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002306 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002307}
Zheng Yane8569812008-09-26 10:05:48 -04002308
Chris Masone6dcd2d2008-07-17 12:53:50 -04002309static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2310 struct btrfs_root *root,
2311 u64 num_bytes, u64 min_alloc_size,
2312 u64 empty_size, u64 hint_byte,
2313 u64 search_end, struct btrfs_key *ins,
2314 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05002315{
2316 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04002317 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002318 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04002319 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04002320
Chris Mason6324fbf2008-03-24 15:01:59 -04002321 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04002322 alloc_profile = info->avail_data_alloc_bits &
2323 info->data_alloc_profile;
2324 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002325 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04002326 alloc_profile = info->avail_system_alloc_bits &
2327 info->system_alloc_profile;
2328 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002329 } else {
Chris Mason8790d502008-04-03 16:29:03 -04002330 alloc_profile = info->avail_metadata_alloc_bits &
2331 info->metadata_alloc_profile;
2332 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002333 }
Chris Mason98d20f62008-04-14 09:46:10 -04002334again:
Chris Masona061fc82008-05-07 11:43:44 -04002335 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04002336 /*
2337 * the only place that sets empty_size is btrfs_realloc_node, which
2338 * is not called recursively on allocations
2339 */
2340 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04002341 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04002342 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002343 2 * 1024 * 1024,
2344 BTRFS_BLOCK_GROUP_METADATA |
2345 (info->metadata_alloc_profile &
2346 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002347 }
2348 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002349 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002350 }
Chris Mason0b86a832008-03-24 15:01:56 -04002351
Chris Masondb945352007-10-15 16:15:53 -04002352 WARN_ON(num_bytes < root->sectorsize);
2353 ret = find_free_extent(trans, root, num_bytes, empty_size,
2354 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002355 trans->alloc_exclude_start,
2356 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002357
Chris Mason98d20f62008-04-14 09:46:10 -04002358 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2359 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002360 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002361 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002362 do_chunk_alloc(trans, root->fs_info->extent_root,
2363 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002364 goto again;
2365 }
Chris Masonec44a352008-04-28 15:29:52 -04002366 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002367 struct btrfs_space_info *sinfo;
2368
2369 sinfo = __find_space_info(root->fs_info, data);
2370 printk("allocation failed flags %Lu, wanted %Lu\n",
2371 data, num_bytes);
2372 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04002373 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002374 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002375
2376 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002377}
2378
Chris Mason65b51a02008-08-01 15:11:20 -04002379int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2380{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002381 struct btrfs_block_group_cache *cache;
2382
Josef Bacik0f9dd462008-09-23 13:14:11 -04002383 cache = btrfs_lookup_block_group(root->fs_info, start);
2384 if (!cache) {
2385 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002386 return -ENOSPC;
2387 }
2388 btrfs_add_free_space(cache, start, len);
Zheng Yan1a40e232008-09-26 10:09:34 -04002389 update_reserved_extents(root, start, len, 0);
Chris Mason65b51a02008-08-01 15:11:20 -04002390 return 0;
2391}
2392
Chris Masone6dcd2d2008-07-17 12:53:50 -04002393int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2394 struct btrfs_root *root,
2395 u64 num_bytes, u64 min_alloc_size,
2396 u64 empty_size, u64 hint_byte,
2397 u64 search_end, struct btrfs_key *ins,
2398 u64 data)
2399{
2400 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002401 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2402 empty_size, hint_byte, search_end, ins,
2403 data);
Zheng Yane8569812008-09-26 10:05:48 -04002404 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002405 return ret;
2406}
2407
2408static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002409 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002410 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002411 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002412{
2413 int ret;
2414 int pending_ret;
2415 u64 super_used;
2416 u64 root_used;
2417 u64 num_bytes = ins->offset;
2418 u32 sizes[2];
2419 struct btrfs_fs_info *info = root->fs_info;
2420 struct btrfs_root *extent_root = info->extent_root;
2421 struct btrfs_extent_item *extent_item;
2422 struct btrfs_extent_ref *ref;
2423 struct btrfs_path *path;
2424 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002425
Zheng Yan31840ae2008-09-23 13:14:14 -04002426 if (parent == 0)
2427 parent = ins->objectid;
2428
Josef Bacik58176a92007-08-29 15:47:34 -04002429 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002430 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002431 super_used = btrfs_super_bytes_used(&info->super_copy);
2432 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002433 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04002434
Josef Bacik58176a92007-08-29 15:47:34 -04002435 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002436 root_used = btrfs_root_used(&root->root_item);
2437 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002438
Chris Mason26b80032007-08-08 20:17:12 -04002439 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002440 struct pending_extent_op *extent_op;
2441
2442 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2443 BUG_ON(!extent_op);
2444
2445 extent_op->type = PENDING_EXTENT_INSERT;
2446 extent_op->bytenr = ins->objectid;
2447 extent_op->num_bytes = ins->offset;
2448 extent_op->parent = parent;
2449 extent_op->orig_parent = 0;
2450 extent_op->generation = ref_generation;
2451 extent_op->orig_generation = 0;
2452 extent_op->level = (int)owner;
2453
Josef Bacik25179202008-10-29 14:49:05 -04002454 mutex_lock(&root->fs_info->extent_ins_mutex);
Chris Mason1a5bc1672007-10-15 16:15:26 -04002455 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2456 ins->objectid + ins->offset - 1,
Josef Bacik25179202008-10-29 14:49:05 -04002457 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002458 set_state_private(&root->fs_info->extent_ins,
2459 ins->objectid, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002460 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04002461 goto update_block;
2462 }
2463
Chris Mason47e4bb92008-02-01 14:51:59 -05002464 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05002465 keys[1].objectid = ins->objectid;
2466 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04002467 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05002468 sizes[0] = sizeof(*extent_item);
2469 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002470
2471 path = btrfs_alloc_path();
2472 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002473
2474 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2475 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04002476 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002477
Chris Mason47e4bb92008-02-01 14:51:59 -05002478 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2479 struct btrfs_extent_item);
2480 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2481 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2482 struct btrfs_extent_ref);
2483
2484 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2485 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2486 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Zheng Yan31840ae2008-09-23 13:14:14 -04002487 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05002488
2489 btrfs_mark_buffer_dirty(path->nodes[0]);
2490
2491 trans->alloc_exclude_start = 0;
2492 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002493 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002494 finish_current_insert(trans, extent_root, 0);
2495 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -04002496
Chris Mason925baed2008-06-25 16:01:30 -04002497 if (ret)
2498 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002499 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04002500 ret = pending_ret;
2501 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002502 }
Chris Mason26b80032007-08-08 20:17:12 -04002503
2504update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04002505 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002506 if (ret) {
2507 printk("update block group failed for %Lu %Lu\n",
2508 ins->objectid, ins->offset);
2509 BUG();
2510 }
Chris Mason925baed2008-06-25 16:01:30 -04002511out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04002512 return ret;
2513}
2514
2515int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002516 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002517 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002518 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002519{
2520 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04002521
2522 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2523 return 0;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002524 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
2525 ref_generation, owner, ins);
Zheng Yane8569812008-09-26 10:05:48 -04002526 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002527 return ret;
2528}
Chris Masone02119d2008-09-05 16:13:11 -04002529
2530/*
2531 * this is used by the tree logging recovery code. It records that
2532 * an extent has been allocated and makes sure to clear the free
2533 * space cache bits as well
2534 */
2535int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002536 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04002537 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002538 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04002539{
2540 int ret;
2541 struct btrfs_block_group_cache *block_group;
2542
Chris Masone02119d2008-09-05 16:13:11 -04002543 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik25179202008-10-29 14:49:05 -04002544 mutex_lock(&block_group->alloc_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002545 cache_block_group(root, block_group);
2546
Josef Bacik25179202008-10-29 14:49:05 -04002547 ret = btrfs_remove_free_space_lock(block_group, ins->objectid,
2548 ins->offset);
2549 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002550 BUG_ON(ret);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002551 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
2552 ref_generation, owner, ins);
Chris Masone02119d2008-09-05 16:13:11 -04002553 return ret;
2554}
2555
Chris Masone6dcd2d2008-07-17 12:53:50 -04002556/*
2557 * finds a free extent and does all the dirty work required for allocation
2558 * returns the key for the extent through ins, and a tree buffer for
2559 * the first block of the extent through buf.
2560 *
2561 * returns 0 if everything worked, non-zero otherwise.
2562 */
2563int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2564 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002565 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002566 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002567 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002568 u64 search_end, struct btrfs_key *ins, u64 data)
2569{
2570 int ret;
2571
Chris Masone6dcd2d2008-07-17 12:53:50 -04002572 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2573 min_alloc_size, empty_size, hint_byte,
2574 search_end, ins, data);
2575 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04002576 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002577 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2578 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002579 owner_objectid, ins);
Chris Masond00aff02008-09-11 15:54:42 -04002580 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002581
Zheng Yane8569812008-09-26 10:05:48 -04002582 } else {
2583 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04002584 }
Chris Mason925baed2008-06-25 16:01:30 -04002585 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002586}
Chris Mason65b51a02008-08-01 15:11:20 -04002587
2588struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2589 struct btrfs_root *root,
2590 u64 bytenr, u32 blocksize)
2591{
2592 struct extent_buffer *buf;
2593
2594 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2595 if (!buf)
2596 return ERR_PTR(-ENOMEM);
2597 btrfs_set_header_generation(buf, trans->transid);
2598 btrfs_tree_lock(buf);
2599 clean_tree_block(trans, root, buf);
2600 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04002601 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2602 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04002603 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002604 } else {
2605 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2606 buf->start + buf->len - 1, GFP_NOFS);
2607 }
Chris Mason65b51a02008-08-01 15:11:20 -04002608 trans->blocks_used++;
2609 return buf;
2610}
2611
Chris Masonfec577f2007-02-26 10:40:21 -05002612/*
2613 * helper function to allocate a block for a given tree
2614 * returns the tree buffer or NULL.
2615 */
Chris Mason5f39d392007-10-15 16:14:19 -04002616struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04002617 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002618 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002619 u64 root_objectid,
2620 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05002621 int level,
2622 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04002623 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05002624{
Chris Masone2fa7222007-03-12 16:22:34 -04002625 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05002626 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002627 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05002628
Zheng Yan31840ae2008-09-23 13:14:14 -04002629 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002630 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04002631 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002632 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04002633 BUG_ON(ret > 0);
2634 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05002635 }
Chris Mason55c69072008-01-09 15:55:33 -05002636
Chris Mason65b51a02008-08-01 15:11:20 -04002637 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05002638 return buf;
2639}
Chris Masona28ec192007-03-06 20:08:01 -05002640
Chris Masone02119d2008-09-05 16:13:11 -04002641int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
2642 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04002643{
Chris Mason7bb86312007-12-11 09:25:06 -05002644 u64 leaf_owner;
2645 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04002646 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002647 struct btrfs_file_extent_item *fi;
2648 int i;
2649 int nritems;
2650 int ret;
2651
Chris Mason5f39d392007-10-15 16:14:19 -04002652 BUG_ON(!btrfs_is_leaf(leaf));
2653 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05002654 leaf_owner = btrfs_header_owner(leaf);
2655 leaf_generation = btrfs_header_generation(leaf);
2656
Chris Mason6407bf62007-03-27 06:33:00 -04002657 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002658 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04002659 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04002660
2661 btrfs_item_key_to_cpu(leaf, &key, i);
2662 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04002663 continue;
2664 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002665 if (btrfs_file_extent_type(leaf, fi) ==
2666 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04002667 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04002668 /*
2669 * FIXME make sure to insert a trans record that
2670 * repeats the snapshot del on crash
2671 */
Chris Masondb945352007-10-15 16:15:53 -04002672 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2673 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04002674 continue;
Chris Mason4a096752008-07-21 10:29:44 -04002675
Chris Mason925baed2008-06-25 16:01:30 -04002676 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002677 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04002678 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002679 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002680 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04002681
2682 atomic_inc(&root->fs_info->throttle_gen);
2683 wake_up(&root->fs_info->transaction_throttle);
2684 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04002685 }
2686 return 0;
2687}
2688
Chris Masone02119d2008-09-05 16:13:11 -04002689static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2690 struct btrfs_root *root,
2691 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04002692{
2693 int i;
2694 int ret;
2695 struct btrfs_extent_info *info = ref->extents;
2696
Yan Zheng31153d82008-07-28 15:32:19 -04002697 for (i = 0; i < ref->nritems; i++) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002698 ret = __btrfs_free_extent(trans, root, info->bytenr,
2699 info->num_bytes, ref->bytenr,
2700 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002701 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04002702
2703 atomic_inc(&root->fs_info->throttle_gen);
2704 wake_up(&root->fs_info->transaction_throttle);
2705 cond_resched();
2706
Yan Zheng31153d82008-07-28 15:32:19 -04002707 BUG_ON(ret);
2708 info++;
2709 }
Yan Zheng31153d82008-07-28 15:32:19 -04002710
2711 return 0;
2712}
2713
Chris Mason333db942008-06-25 16:01:30 -04002714int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2715 u32 *refs)
2716{
Chris Mason017e5362008-07-28 15:32:51 -04002717 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04002718
Zheng Yan31840ae2008-09-23 13:14:14 -04002719 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04002720 BUG_ON(ret);
2721
2722#if 0 // some debugging code in case we see problems here
2723 /* if the refs count is one, it won't get increased again. But
2724 * if the ref count is > 1, someone may be decreasing it at
2725 * the same time we are.
2726 */
2727 if (*refs != 1) {
2728 struct extent_buffer *eb = NULL;
2729 eb = btrfs_find_create_tree_block(root, start, len);
2730 if (eb)
2731 btrfs_tree_lock(eb);
2732
2733 mutex_lock(&root->fs_info->alloc_mutex);
2734 ret = lookup_extent_ref(NULL, root, start, len, refs);
2735 BUG_ON(ret);
2736 mutex_unlock(&root->fs_info->alloc_mutex);
2737
2738 if (eb) {
2739 btrfs_tree_unlock(eb);
2740 free_extent_buffer(eb);
2741 }
2742 if (*refs == 1) {
2743 printk("block %llu went down to one during drop_snap\n",
2744 (unsigned long long)start);
2745 }
2746
2747 }
2748#endif
2749
Chris Masone7a84562008-06-25 16:01:31 -04002750 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04002751 return ret;
Chris Mason333db942008-06-25 16:01:30 -04002752}
2753
2754/*
Chris Mason9aca1d52007-03-13 11:09:37 -04002755 * helper function for drop_snapshot, this walks down the tree dropping ref
2756 * counts as it goes.
2757 */
Chris Mason98ed5172008-01-03 10:01:48 -05002758static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2759 struct btrfs_root *root,
2760 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002761{
Chris Mason7bb86312007-12-11 09:25:06 -05002762 u64 root_owner;
2763 u64 root_gen;
2764 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04002765 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002766 struct extent_buffer *next;
2767 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002768 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04002769 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04002770 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002771 int ret;
2772 u32 refs;
2773
Chris Mason5caf2a02007-04-02 11:20:42 -04002774 WARN_ON(*level < 0);
2775 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04002776 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04002777 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002778 BUG_ON(ret);
2779 if (refs > 1)
2780 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002781
Chris Mason9aca1d52007-03-13 11:09:37 -04002782 /*
2783 * walk down to the last node level and free all the leaves
2784 */
Chris Mason6407bf62007-03-27 06:33:00 -04002785 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002786 WARN_ON(*level < 0);
2787 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002788 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002789
Chris Mason5f39d392007-10-15 16:14:19 -04002790 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002791 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002792
Chris Mason7518a232007-03-12 12:01:18 -04002793 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002794 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002795 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002796 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002797 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04002798 BUG_ON(ret);
2799 break;
2800 }
Chris Masondb945352007-10-15 16:15:53 -04002801 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04002802 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04002803 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002804
Chris Mason333db942008-06-25 16:01:30 -04002805 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002806 BUG_ON(ret);
2807 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002808 parent = path->nodes[*level];
2809 root_owner = btrfs_header_owner(parent);
2810 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002811 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04002812
Chris Mason925baed2008-06-25 16:01:30 -04002813 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04002814 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002815 root_owner, root_gen,
2816 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002817 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04002818
2819 atomic_inc(&root->fs_info->throttle_gen);
2820 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04002821 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04002822
Chris Mason20524f02007-03-10 06:35:47 -05002823 continue;
2824 }
Chris Masonf87f0572008-08-01 11:27:23 -04002825 /*
2826 * at this point, we have a single ref, and since the
2827 * only place referencing this extent is a dead root
2828 * the reference count should never go higher.
2829 * So, we don't need to check it again
2830 */
Yan Zheng31153d82008-07-28 15:32:19 -04002831 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04002832 ref = btrfs_lookup_leaf_ref(root, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04002833 if (ref && ref->generation != ptr_gen) {
2834 btrfs_free_leaf_ref(root, ref);
2835 ref = NULL;
2836 }
Yan Zheng31153d82008-07-28 15:32:19 -04002837 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04002838 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002839 BUG_ON(ret);
2840 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04002841 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002842 *level = 0;
2843 break;
2844 }
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002845 if (printk_ratelimit()) {
Chris Mason37d1aee2008-07-31 10:48:37 -04002846 printk("leaf ref miss for bytenr %llu\n",
2847 (unsigned long long)bytenr);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002848 }
Yan Zheng31153d82008-07-28 15:32:19 -04002849 }
Chris Masondb945352007-10-15 16:15:53 -04002850 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04002851 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002852 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04002853
Chris Masonca7a79a2008-05-12 12:59:19 -04002854 next = read_tree_block(root, bytenr, blocksize,
2855 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04002856 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04002857#if 0
2858 /*
2859 * this is a debugging check and can go away
2860 * the ref should never go all the way down to 1
2861 * at this point
2862 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002863 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2864 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002865 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002866 WARN_ON(refs != 1);
2867#endif
Chris Masone9d0b132007-08-10 14:06:19 -04002868 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002869 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002870 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002871 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002872 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002873 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002874 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04002875 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05002876 }
2877out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002878 WARN_ON(*level < 0);
2879 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002880
2881 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05002882 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04002883 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05002884 } else {
2885 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04002886 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05002887 }
2888
Yan Zheng31153d82008-07-28 15:32:19 -04002889 blocksize = btrfs_level_size(root, *level);
2890 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05002891 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04002892
2893 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002894 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002895 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002896 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05002897 path->nodes[*level] = NULL;
2898 *level += 1;
2899 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002900
Chris Masone7a84562008-06-25 16:01:31 -04002901 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05002902 return 0;
2903}
2904
Chris Mason9aca1d52007-03-13 11:09:37 -04002905/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04002906 * helper function for drop_subtree, this function is similar to
2907 * walk_down_tree. The main difference is that it checks reference
2908 * counts while tree blocks are locked.
2909 */
2910static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
2911 struct btrfs_root *root,
2912 struct btrfs_path *path, int *level)
2913{
2914 struct extent_buffer *next;
2915 struct extent_buffer *cur;
2916 struct extent_buffer *parent;
2917 u64 bytenr;
2918 u64 ptr_gen;
2919 u32 blocksize;
2920 u32 refs;
2921 int ret;
2922
2923 cur = path->nodes[*level];
2924 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
2925 &refs);
2926 BUG_ON(ret);
2927 if (refs > 1)
2928 goto out;
2929
2930 while (*level >= 0) {
2931 cur = path->nodes[*level];
2932 if (*level == 0) {
2933 ret = btrfs_drop_leaf_ref(trans, root, cur);
2934 BUG_ON(ret);
2935 clean_tree_block(trans, root, cur);
2936 break;
2937 }
2938 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
2939 clean_tree_block(trans, root, cur);
2940 break;
2941 }
2942
2943 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2944 blocksize = btrfs_level_size(root, *level - 1);
2945 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2946
2947 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
2948 btrfs_tree_lock(next);
2949
2950 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
2951 &refs);
2952 BUG_ON(ret);
2953 if (refs > 1) {
2954 parent = path->nodes[*level];
2955 ret = btrfs_free_extent(trans, root, bytenr,
2956 blocksize, parent->start,
2957 btrfs_header_owner(parent),
2958 btrfs_header_generation(parent),
2959 *level - 1, 1);
2960 BUG_ON(ret);
2961 path->slots[*level]++;
2962 btrfs_tree_unlock(next);
2963 free_extent_buffer(next);
2964 continue;
2965 }
2966
2967 *level = btrfs_header_level(next);
2968 path->nodes[*level] = next;
2969 path->slots[*level] = 0;
2970 path->locks[*level] = 1;
2971 cond_resched();
2972 }
2973out:
2974 parent = path->nodes[*level + 1];
2975 bytenr = path->nodes[*level]->start;
2976 blocksize = path->nodes[*level]->len;
2977
2978 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
2979 parent->start, btrfs_header_owner(parent),
2980 btrfs_header_generation(parent), *level, 1);
2981 BUG_ON(ret);
2982
2983 if (path->locks[*level]) {
2984 btrfs_tree_unlock(path->nodes[*level]);
2985 path->locks[*level] = 0;
2986 }
2987 free_extent_buffer(path->nodes[*level]);
2988 path->nodes[*level] = NULL;
2989 *level += 1;
2990 cond_resched();
2991 return 0;
2992}
2993
2994/*
Chris Mason9aca1d52007-03-13 11:09:37 -04002995 * helper for dropping snapshots. This walks back up the tree in the path
2996 * to find the first node higher up where we haven't yet gone through
2997 * all the slots
2998 */
Chris Mason98ed5172008-01-03 10:01:48 -05002999static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3000 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003001 struct btrfs_path *path,
3002 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003003{
Chris Mason7bb86312007-12-11 09:25:06 -05003004 u64 root_owner;
3005 u64 root_gen;
3006 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003007 int i;
3008 int slot;
3009 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003010
Yan Zhengf82d02d2008-10-29 14:49:05 -04003011 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003012 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003013 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3014 struct extent_buffer *node;
3015 struct btrfs_disk_key disk_key;
3016 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003017 path->slots[i]++;
3018 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003019 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003020 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003021 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003022 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003023 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003024 return 0;
3025 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003026 struct extent_buffer *parent;
3027 if (path->nodes[*level] == root->node)
3028 parent = path->nodes[*level];
3029 else
3030 parent = path->nodes[*level + 1];
3031
3032 root_owner = btrfs_header_owner(parent);
3033 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003034
3035 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003036 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003037 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003038 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003039 parent->start, root_owner,
3040 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003041 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003042 if (path->locks[*level]) {
3043 btrfs_tree_unlock(path->nodes[*level]);
3044 path->locks[*level] = 0;
3045 }
Chris Mason5f39d392007-10-15 16:14:19 -04003046 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003047 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003048 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003049 }
3050 }
3051 return 1;
3052}
3053
Chris Mason9aca1d52007-03-13 11:09:37 -04003054/*
3055 * drop the reference count on the tree rooted at 'snap'. This traverses
3056 * the tree freeing any blocks that have a ref count of zero after being
3057 * decremented.
3058 */
Chris Masone089f052007-03-16 16:20:31 -04003059int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003060 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003061{
Chris Mason3768f362007-03-13 16:47:54 -04003062 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003063 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003064 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003065 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003066 int i;
3067 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003068 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003069
Chris Masona2135012008-06-25 16:01:30 -04003070 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003071 path = btrfs_alloc_path();
3072 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003073
Chris Mason5f39d392007-10-15 16:14:19 -04003074 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003075 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003076 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3077 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003078 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003079 path->slots[level] = 0;
3080 } else {
3081 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003082 struct btrfs_disk_key found_key;
3083 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003084
Chris Mason9f3a7422007-08-07 15:52:19 -04003085 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003086 level = root_item->drop_level;
3087 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003088 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003089 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003090 ret = wret;
3091 goto out;
3092 }
Chris Mason5f39d392007-10-15 16:14:19 -04003093 node = path->nodes[level];
3094 btrfs_node_key(node, &found_key, path->slots[level]);
3095 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3096 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003097 /*
3098 * unlock our path, this is safe because only this
3099 * function is allowed to delete this snapshot
3100 */
Chris Mason925baed2008-06-25 16:01:30 -04003101 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3102 if (path->nodes[i] && path->locks[i]) {
3103 path->locks[i] = 0;
3104 btrfs_tree_unlock(path->nodes[i]);
3105 }
3106 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003107 }
Chris Mason20524f02007-03-10 06:35:47 -05003108 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003109 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003110 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003111 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003112 if (wret < 0)
3113 ret = wret;
3114
Yan Zhengf82d02d2008-10-29 14:49:05 -04003115 wret = walk_up_tree(trans, root, path, &level,
3116 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003117 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003118 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003119 if (wret < 0)
3120 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003121 if (trans->transaction->in_commit) {
3122 ret = -EAGAIN;
3123 break;
3124 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003125 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003126 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003127 }
Chris Mason83e15a22007-03-12 09:03:27 -04003128 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003129 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003130 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003131 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003132 }
Chris Mason20524f02007-03-10 06:35:47 -05003133 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003134out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003135 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003136 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003137}
Chris Mason9078a3e2007-04-26 16:46:15 -04003138
Yan Zhengf82d02d2008-10-29 14:49:05 -04003139int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3140 struct btrfs_root *root,
3141 struct extent_buffer *node,
3142 struct extent_buffer *parent)
3143{
3144 struct btrfs_path *path;
3145 int level;
3146 int parent_level;
3147 int ret = 0;
3148 int wret;
3149
3150 path = btrfs_alloc_path();
3151 BUG_ON(!path);
3152
3153 BUG_ON(!btrfs_tree_locked(parent));
3154 parent_level = btrfs_header_level(parent);
3155 extent_buffer_get(parent);
3156 path->nodes[parent_level] = parent;
3157 path->slots[parent_level] = btrfs_header_nritems(parent);
3158
3159 BUG_ON(!btrfs_tree_locked(node));
3160 level = btrfs_header_level(node);
3161 extent_buffer_get(node);
3162 path->nodes[level] = node;
3163 path->slots[level] = 0;
3164
3165 while (1) {
3166 wret = walk_down_subtree(trans, root, path, &level);
3167 if (wret < 0)
3168 ret = wret;
3169 if (wret != 0)
3170 break;
3171
3172 wret = walk_up_tree(trans, root, path, &level, parent_level);
3173 if (wret < 0)
3174 ret = wret;
3175 if (wret != 0)
3176 break;
3177 }
3178
3179 btrfs_free_path(path);
3180 return ret;
3181}
3182
Chris Mason8e7bf942008-04-28 09:02:36 -04003183static unsigned long calc_ra(unsigned long start, unsigned long last,
3184 unsigned long nr)
3185{
3186 return min(last, start + nr - 1);
3187}
3188
Chris Mason98ed5172008-01-03 10:01:48 -05003189static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3190 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003191{
3192 u64 page_start;
3193 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003194 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003195 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003196 unsigned long i;
3197 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003198 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003199 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003200 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003201 unsigned int total_read = 0;
3202 unsigned int total_dirty = 0;
3203 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003204
3205 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003206
3207 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003208 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003209 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3210
Zheng Yan1a40e232008-09-26 10:09:34 -04003211 /* make sure the dirty trick played by the caller work */
3212 ret = invalidate_inode_pages2_range(inode->i_mapping,
3213 first_index, last_index);
3214 if (ret)
3215 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003216
Chris Mason4313b392008-01-03 09:08:48 -05003217 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003218
Zheng Yan1a40e232008-09-26 10:09:34 -04003219 for (i = first_index ; i <= last_index; i++) {
3220 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003221 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003222 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003223 }
3224 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003225again:
3226 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003227 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003228 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003229 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003230 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003231 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003232 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003233 if (!PageUptodate(page)) {
3234 btrfs_readpage(NULL, page);
3235 lock_page(page);
3236 if (!PageUptodate(page)) {
3237 unlock_page(page);
3238 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003239 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003240 goto out_unlock;
3241 }
3242 }
Chris Masonec44a352008-04-28 15:29:52 -04003243 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003244
Chris Masonedbd8d42007-12-21 16:27:24 -05003245 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3246 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003247 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003248
Chris Mason3eaa2882008-07-24 11:57:52 -04003249 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3250 if (ordered) {
3251 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3252 unlock_page(page);
3253 page_cache_release(page);
3254 btrfs_start_ordered_extent(inode, ordered, 1);
3255 btrfs_put_ordered_extent(ordered);
3256 goto again;
3257 }
3258 set_page_extent_mapped(page);
3259
Chris Masonea8c2812008-08-04 23:17:27 -04003260 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003261 if (i == first_index)
3262 set_extent_bits(io_tree, page_start, page_end,
3263 EXTENT_BOUNDARY, GFP_NOFS);
3264
Chris Masona061fc82008-05-07 11:43:44 -04003265 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003266 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003267
Chris Masond1310b22008-01-24 16:13:08 -05003268 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003269 unlock_page(page);
3270 page_cache_release(page);
3271 }
3272
3273out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04003274 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05003275 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003276 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04003277 return ret;
3278}
3279
Zheng Yan1a40e232008-09-26 10:09:34 -04003280static int noinline relocate_data_extent(struct inode *reloc_inode,
3281 struct btrfs_key *extent_key,
3282 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05003283{
Zheng Yan1a40e232008-09-26 10:09:34 -04003284 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3285 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
3286 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04003287 u64 start = extent_key->objectid - offset;
3288 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04003289
3290 em = alloc_extent_map(GFP_NOFS);
3291 BUG_ON(!em || IS_ERR(em));
3292
Yan Zheng66435582008-10-30 14:19:50 -04003293 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04003294 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003295 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04003296 em->block_start = extent_key->objectid;
3297 em->bdev = root->fs_info->fs_devices->latest_bdev;
3298 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3299
3300 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04003301 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04003302 while (1) {
3303 int ret;
3304 spin_lock(&em_tree->lock);
3305 ret = add_extent_mapping(em_tree, em);
3306 spin_unlock(&em_tree->lock);
3307 if (ret != -EEXIST) {
3308 free_extent_map(em);
3309 break;
3310 }
Yan Zheng66435582008-10-30 14:19:50 -04003311 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04003312 }
Yan Zheng66435582008-10-30 14:19:50 -04003313 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04003314
Yan Zheng66435582008-10-30 14:19:50 -04003315 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04003316}
3317
3318struct btrfs_ref_path {
3319 u64 extent_start;
3320 u64 nodes[BTRFS_MAX_LEVEL];
3321 u64 root_objectid;
3322 u64 root_generation;
3323 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04003324 u32 num_refs;
3325 int lowest_level;
3326 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04003327 int shared_level;
3328
3329 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
3330 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04003331};
3332
3333struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04003334 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04003335 u64 disk_bytenr;
3336 u64 disk_num_bytes;
3337 u64 offset;
3338 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04003339 u8 compression;
3340 u8 encryption;
3341 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04003342};
3343
3344static int is_cowonly_root(u64 root_objectid)
3345{
3346 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
3347 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
3348 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
3349 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
3350 root_objectid == BTRFS_TREE_LOG_OBJECTID)
3351 return 1;
3352 return 0;
3353}
3354
3355static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
3356 struct btrfs_root *extent_root,
3357 struct btrfs_ref_path *ref_path,
3358 int first_time)
3359{
3360 struct extent_buffer *leaf;
3361 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05003362 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05003363 struct btrfs_key key;
3364 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04003365 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05003366 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04003367 int level;
3368 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05003369
Zheng Yan1a40e232008-09-26 10:09:34 -04003370 path = btrfs_alloc_path();
3371 if (!path)
3372 return -ENOMEM;
3373
Zheng Yan1a40e232008-09-26 10:09:34 -04003374 if (first_time) {
3375 ref_path->lowest_level = -1;
3376 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04003377 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04003378 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04003379 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003380walk_down:
3381 level = ref_path->current_level - 1;
3382 while (level >= -1) {
3383 u64 parent;
3384 if (level < ref_path->lowest_level)
3385 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05003386
Zheng Yan1a40e232008-09-26 10:09:34 -04003387 if (level >= 0) {
3388 bytenr = ref_path->nodes[level];
3389 } else {
3390 bytenr = ref_path->extent_start;
3391 }
3392 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003393
Zheng Yan1a40e232008-09-26 10:09:34 -04003394 parent = ref_path->nodes[level + 1];
3395 ref_path->nodes[level + 1] = 0;
3396 ref_path->current_level = level;
3397 BUG_ON(parent == 0);
3398
3399 key.objectid = bytenr;
3400 key.offset = parent + 1;
3401 key.type = BTRFS_EXTENT_REF_KEY;
3402
3403 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003404 if (ret < 0)
3405 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003406 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003407
Chris Masonedbd8d42007-12-21 16:27:24 -05003408 leaf = path->nodes[0];
3409 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04003410 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04003411 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04003412 if (ret < 0)
3413 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003414 if (ret > 0)
3415 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04003416 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04003417 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003418
3419 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04003420 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04003421 found_key.type == BTRFS_EXTENT_REF_KEY) {
3422 if (level < ref_path->shared_level)
3423 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04003424 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04003425 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003426next:
3427 level--;
3428 btrfs_release_path(extent_root, path);
3429 if (need_resched()) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003430 cond_resched();
Chris Masona061fc82008-05-07 11:43:44 -04003431 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003432 }
3433 /* reached lowest level */
3434 ret = 1;
3435 goto out;
3436walk_up:
3437 level = ref_path->current_level;
3438 while (level < BTRFS_MAX_LEVEL - 1) {
3439 u64 ref_objectid;
3440 if (level >= 0) {
3441 bytenr = ref_path->nodes[level];
3442 } else {
3443 bytenr = ref_path->extent_start;
Chris Masona061fc82008-05-07 11:43:44 -04003444 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003445 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003446
Zheng Yan1a40e232008-09-26 10:09:34 -04003447 key.objectid = bytenr;
3448 key.offset = 0;
3449 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05003450
Zheng Yan1a40e232008-09-26 10:09:34 -04003451 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
3452 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05003453 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003454
3455 leaf = path->nodes[0];
3456 nritems = btrfs_header_nritems(leaf);
3457 if (path->slots[0] >= nritems) {
3458 ret = btrfs_next_leaf(extent_root, path);
3459 if (ret < 0)
3460 goto out;
3461 if (ret > 0) {
3462 /* the extent was freed by someone */
3463 if (ref_path->lowest_level == level)
3464 goto out;
3465 btrfs_release_path(extent_root, path);
3466 goto walk_down;
3467 }
3468 leaf = path->nodes[0];
3469 }
3470
3471 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3472 if (found_key.objectid != bytenr ||
3473 found_key.type != BTRFS_EXTENT_REF_KEY) {
3474 /* the extent was freed by someone */
3475 if (ref_path->lowest_level == level) {
3476 ret = 1;
3477 goto out;
3478 }
3479 btrfs_release_path(extent_root, path);
3480 goto walk_down;
3481 }
3482found:
3483 ref = btrfs_item_ptr(leaf, path->slots[0],
3484 struct btrfs_extent_ref);
3485 ref_objectid = btrfs_ref_objectid(leaf, ref);
3486 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3487 if (first_time) {
3488 level = (int)ref_objectid;
3489 BUG_ON(level >= BTRFS_MAX_LEVEL);
3490 ref_path->lowest_level = level;
3491 ref_path->current_level = level;
3492 ref_path->nodes[level] = bytenr;
3493 } else {
3494 WARN_ON(ref_objectid != level);
3495 }
3496 } else {
3497 WARN_ON(level != -1);
3498 }
3499 first_time = 0;
3500
3501 if (ref_path->lowest_level == level) {
3502 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04003503 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
3504 }
3505
3506 /*
3507 * the block is tree root or the block isn't in reference
3508 * counted tree.
3509 */
3510 if (found_key.objectid == found_key.offset ||
3511 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
3512 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
3513 ref_path->root_generation =
3514 btrfs_ref_generation(leaf, ref);
3515 if (level < 0) {
3516 /* special reference from the tree log */
3517 ref_path->nodes[0] = found_key.offset;
3518 ref_path->current_level = 0;
3519 }
3520 ret = 0;
3521 goto out;
3522 }
3523
3524 level++;
3525 BUG_ON(ref_path->nodes[level] != 0);
3526 ref_path->nodes[level] = found_key.offset;
3527 ref_path->current_level = level;
3528
3529 /*
3530 * the reference was created in the running transaction,
3531 * no need to continue walking up.
3532 */
3533 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
3534 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
3535 ref_path->root_generation =
3536 btrfs_ref_generation(leaf, ref);
3537 ret = 0;
3538 goto out;
3539 }
3540
3541 btrfs_release_path(extent_root, path);
3542 if (need_resched()) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003543 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04003544 }
3545 }
3546 /* reached max tree level, but no tree root found. */
3547 BUG();
3548out:
Zheng Yan1a40e232008-09-26 10:09:34 -04003549 btrfs_free_path(path);
3550 return ret;
3551}
3552
3553static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
3554 struct btrfs_root *extent_root,
3555 struct btrfs_ref_path *ref_path,
3556 u64 extent_start)
3557{
3558 memset(ref_path, 0, sizeof(*ref_path));
3559 ref_path->extent_start = extent_start;
3560
3561 return __next_ref_path(trans, extent_root, ref_path, 1);
3562}
3563
3564static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
3565 struct btrfs_root *extent_root,
3566 struct btrfs_ref_path *ref_path)
3567{
3568 return __next_ref_path(trans, extent_root, ref_path, 0);
3569}
3570
3571static int noinline get_new_locations(struct inode *reloc_inode,
3572 struct btrfs_key *extent_key,
3573 u64 offset, int no_fragment,
3574 struct disk_extent **extents,
3575 int *nr_extents)
3576{
3577 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3578 struct btrfs_path *path;
3579 struct btrfs_file_extent_item *fi;
3580 struct extent_buffer *leaf;
3581 struct disk_extent *exts = *extents;
3582 struct btrfs_key found_key;
3583 u64 cur_pos;
3584 u64 last_byte;
3585 u32 nritems;
3586 int nr = 0;
3587 int max = *nr_extents;
3588 int ret;
3589
3590 WARN_ON(!no_fragment && *extents);
3591 if (!exts) {
3592 max = 1;
3593 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
3594 if (!exts)
3595 return -ENOMEM;
3596 }
3597
3598 path = btrfs_alloc_path();
3599 BUG_ON(!path);
3600
3601 cur_pos = extent_key->objectid - offset;
3602 last_byte = extent_key->objectid + extent_key->offset;
3603 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
3604 cur_pos, 0);
3605 if (ret < 0)
3606 goto out;
3607 if (ret > 0) {
3608 ret = -ENOENT;
3609 goto out;
3610 }
3611
3612 while (1) {
3613 leaf = path->nodes[0];
3614 nritems = btrfs_header_nritems(leaf);
3615 if (path->slots[0] >= nritems) {
3616 ret = btrfs_next_leaf(root, path);
3617 if (ret < 0)
3618 goto out;
3619 if (ret > 0)
3620 break;
3621 leaf = path->nodes[0];
3622 }
3623
3624 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3625 if (found_key.offset != cur_pos ||
3626 found_key.type != BTRFS_EXTENT_DATA_KEY ||
3627 found_key.objectid != reloc_inode->i_ino)
3628 break;
3629
3630 fi = btrfs_item_ptr(leaf, path->slots[0],
3631 struct btrfs_file_extent_item);
3632 if (btrfs_file_extent_type(leaf, fi) !=
3633 BTRFS_FILE_EXTENT_REG ||
3634 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
3635 break;
3636
3637 if (nr == max) {
3638 struct disk_extent *old = exts;
3639 max *= 2;
3640 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
3641 memcpy(exts, old, sizeof(*exts) * nr);
3642 if (old != *extents)
3643 kfree(old);
3644 }
3645
3646 exts[nr].disk_bytenr =
3647 btrfs_file_extent_disk_bytenr(leaf, fi);
3648 exts[nr].disk_num_bytes =
3649 btrfs_file_extent_disk_num_bytes(leaf, fi);
3650 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
3651 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04003652 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
3653 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
3654 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
3655 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
3656 fi);
Zheng Yan1a40e232008-09-26 10:09:34 -04003657 WARN_ON(exts[nr].offset > 0);
3658 WARN_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
3659
3660 cur_pos += exts[nr].num_bytes;
3661 nr++;
3662
3663 if (cur_pos + offset >= last_byte)
3664 break;
3665
3666 if (no_fragment) {
3667 ret = 1;
3668 goto out;
3669 }
3670 path->slots[0]++;
3671 }
3672
3673 WARN_ON(cur_pos + offset > last_byte);
3674 if (cur_pos + offset < last_byte) {
3675 ret = -ENOENT;
3676 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05003677 }
3678 ret = 0;
3679out:
Zheng Yan1a40e232008-09-26 10:09:34 -04003680 btrfs_free_path(path);
3681 if (ret) {
3682 if (exts != *extents)
3683 kfree(exts);
3684 } else {
3685 *extents = exts;
3686 *nr_extents = nr;
3687 }
3688 return ret;
3689}
3690
3691static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
3692 struct btrfs_root *root,
3693 struct btrfs_path *path,
3694 struct btrfs_key *extent_key,
3695 struct btrfs_key *leaf_key,
3696 struct btrfs_ref_path *ref_path,
3697 struct disk_extent *new_extents,
3698 int nr_extents)
3699{
3700 struct extent_buffer *leaf;
3701 struct btrfs_file_extent_item *fi;
3702 struct inode *inode = NULL;
3703 struct btrfs_key key;
3704 u64 lock_start = 0;
3705 u64 lock_end = 0;
3706 u64 num_bytes;
3707 u64 ext_offset;
3708 u64 first_pos;
3709 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003710 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04003711 int extent_locked = 0;
3712 int ret;
3713
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003714 memcpy(&key, leaf_key, sizeof(key));
3715 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04003716 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003717 if (key.objectid < ref_path->owner_objectid ||
3718 (key.objectid == ref_path->owner_objectid &&
3719 key.type < BTRFS_EXTENT_DATA_KEY)) {
3720 key.objectid = ref_path->owner_objectid;
3721 key.type = BTRFS_EXTENT_DATA_KEY;
3722 key.offset = 0;
3723 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003724 }
3725
3726 while (1) {
3727 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3728 if (ret < 0)
3729 goto out;
3730
3731 leaf = path->nodes[0];
3732 nritems = btrfs_header_nritems(leaf);
3733next:
3734 if (extent_locked && ret > 0) {
3735 /*
3736 * the file extent item was modified by someone
3737 * before the extent got locked.
3738 */
Zheng Yan1a40e232008-09-26 10:09:34 -04003739 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3740 lock_end, GFP_NOFS);
3741 extent_locked = 0;
3742 }
3743
3744 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003745 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04003746 break;
3747
3748 BUG_ON(extent_locked);
3749 ret = btrfs_next_leaf(root, path);
3750 if (ret < 0)
3751 goto out;
3752 if (ret > 0)
3753 break;
3754 leaf = path->nodes[0];
3755 nritems = btrfs_header_nritems(leaf);
3756 }
3757
3758 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3759
3760 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
3761 if ((key.objectid > ref_path->owner_objectid) ||
3762 (key.objectid == ref_path->owner_objectid &&
3763 key.type > BTRFS_EXTENT_DATA_KEY) ||
3764 (key.offset >= first_pos + extent_key->offset))
3765 break;
3766 }
3767
3768 if (inode && key.objectid != inode->i_ino) {
3769 BUG_ON(extent_locked);
3770 btrfs_release_path(root, path);
3771 mutex_unlock(&inode->i_mutex);
3772 iput(inode);
3773 inode = NULL;
3774 continue;
3775 }
3776
3777 if (key.type != BTRFS_EXTENT_DATA_KEY) {
3778 path->slots[0]++;
3779 ret = 1;
3780 goto next;
3781 }
3782 fi = btrfs_item_ptr(leaf, path->slots[0],
3783 struct btrfs_file_extent_item);
3784 if ((btrfs_file_extent_type(leaf, fi) !=
3785 BTRFS_FILE_EXTENT_REG) ||
3786 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
3787 extent_key->objectid)) {
3788 path->slots[0]++;
3789 ret = 1;
3790 goto next;
3791 }
3792
3793 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
3794 ext_offset = btrfs_file_extent_offset(leaf, fi);
3795
3796 if (first_pos > key.offset - ext_offset)
3797 first_pos = key.offset - ext_offset;
3798
3799 if (!extent_locked) {
3800 lock_start = key.offset;
3801 lock_end = lock_start + num_bytes - 1;
3802 } else {
Yan Zheng66435582008-10-30 14:19:50 -04003803 if (lock_start > key.offset ||
3804 lock_end + 1 < key.offset + num_bytes) {
3805 unlock_extent(&BTRFS_I(inode)->io_tree,
3806 lock_start, lock_end, GFP_NOFS);
3807 extent_locked = 0;
3808 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003809 }
3810
3811 if (!inode) {
3812 btrfs_release_path(root, path);
3813
3814 inode = btrfs_iget_locked(root->fs_info->sb,
3815 key.objectid, root);
3816 if (inode->i_state & I_NEW) {
3817 BTRFS_I(inode)->root = root;
3818 BTRFS_I(inode)->location.objectid =
3819 key.objectid;
3820 BTRFS_I(inode)->location.type =
3821 BTRFS_INODE_ITEM_KEY;
3822 BTRFS_I(inode)->location.offset = 0;
3823 btrfs_read_locked_inode(inode);
3824 unlock_new_inode(inode);
3825 }
3826 /*
3827 * some code call btrfs_commit_transaction while
3828 * holding the i_mutex, so we can't use mutex_lock
3829 * here.
3830 */
3831 if (is_bad_inode(inode) ||
3832 !mutex_trylock(&inode->i_mutex)) {
3833 iput(inode);
3834 inode = NULL;
3835 key.offset = (u64)-1;
3836 goto skip;
3837 }
3838 }
3839
3840 if (!extent_locked) {
3841 struct btrfs_ordered_extent *ordered;
3842
3843 btrfs_release_path(root, path);
3844
3845 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3846 lock_end, GFP_NOFS);
3847 ordered = btrfs_lookup_first_ordered_extent(inode,
3848 lock_end);
3849 if (ordered &&
3850 ordered->file_offset <= lock_end &&
3851 ordered->file_offset + ordered->len > lock_start) {
3852 unlock_extent(&BTRFS_I(inode)->io_tree,
3853 lock_start, lock_end, GFP_NOFS);
3854 btrfs_start_ordered_extent(inode, ordered, 1);
3855 btrfs_put_ordered_extent(ordered);
3856 key.offset += num_bytes;
3857 goto skip;
3858 }
3859 if (ordered)
3860 btrfs_put_ordered_extent(ordered);
3861
Zheng Yan1a40e232008-09-26 10:09:34 -04003862 extent_locked = 1;
3863 continue;
3864 }
3865
3866 if (nr_extents == 1) {
3867 /* update extent pointer in place */
3868 btrfs_set_file_extent_generation(leaf, fi,
3869 trans->transid);
3870 btrfs_set_file_extent_disk_bytenr(leaf, fi,
3871 new_extents[0].disk_bytenr);
3872 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
3873 new_extents[0].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04003874 btrfs_set_file_extent_ram_bytes(leaf, fi,
3875 new_extents[0].ram_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04003876 ext_offset += new_extents[0].offset;
3877 btrfs_set_file_extent_offset(leaf, fi, ext_offset);
3878 btrfs_mark_buffer_dirty(leaf);
3879
3880 btrfs_drop_extent_cache(inode, key.offset,
3881 key.offset + num_bytes - 1, 0);
3882
3883 ret = btrfs_inc_extent_ref(trans, root,
3884 new_extents[0].disk_bytenr,
3885 new_extents[0].disk_num_bytes,
3886 leaf->start,
3887 root->root_key.objectid,
3888 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003889 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04003890 BUG_ON(ret);
3891
3892 ret = btrfs_free_extent(trans, root,
3893 extent_key->objectid,
3894 extent_key->offset,
3895 leaf->start,
3896 btrfs_header_owner(leaf),
3897 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003898 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04003899 BUG_ON(ret);
3900
3901 btrfs_release_path(root, path);
3902 key.offset += num_bytes;
3903 } else {
3904 u64 alloc_hint;
3905 u64 extent_len;
3906 int i;
3907 /*
3908 * drop old extent pointer at first, then insert the
3909 * new pointers one bye one
3910 */
3911 btrfs_release_path(root, path);
3912 ret = btrfs_drop_extents(trans, root, inode, key.offset,
3913 key.offset + num_bytes,
3914 key.offset, &alloc_hint);
3915 BUG_ON(ret);
3916
3917 for (i = 0; i < nr_extents; i++) {
3918 if (ext_offset >= new_extents[i].num_bytes) {
3919 ext_offset -= new_extents[i].num_bytes;
3920 continue;
3921 }
3922 extent_len = min(new_extents[i].num_bytes -
3923 ext_offset, num_bytes);
3924
3925 ret = btrfs_insert_empty_item(trans, root,
3926 path, &key,
3927 sizeof(*fi));
3928 BUG_ON(ret);
3929
3930 leaf = path->nodes[0];
3931 fi = btrfs_item_ptr(leaf, path->slots[0],
3932 struct btrfs_file_extent_item);
3933 btrfs_set_file_extent_generation(leaf, fi,
3934 trans->transid);
3935 btrfs_set_file_extent_type(leaf, fi,
3936 BTRFS_FILE_EXTENT_REG);
3937 btrfs_set_file_extent_disk_bytenr(leaf, fi,
3938 new_extents[i].disk_bytenr);
3939 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
3940 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04003941 btrfs_set_file_extent_ram_bytes(leaf, fi,
3942 new_extents[i].ram_bytes);
3943
3944 btrfs_set_file_extent_compression(leaf, fi,
3945 new_extents[i].compression);
3946 btrfs_set_file_extent_encryption(leaf, fi,
3947 new_extents[i].encryption);
3948 btrfs_set_file_extent_other_encoding(leaf, fi,
3949 new_extents[i].other_encoding);
3950
Zheng Yan1a40e232008-09-26 10:09:34 -04003951 btrfs_set_file_extent_num_bytes(leaf, fi,
3952 extent_len);
3953 ext_offset += new_extents[i].offset;
3954 btrfs_set_file_extent_offset(leaf, fi,
3955 ext_offset);
3956 btrfs_mark_buffer_dirty(leaf);
3957
3958 btrfs_drop_extent_cache(inode, key.offset,
3959 key.offset + extent_len - 1, 0);
3960
3961 ret = btrfs_inc_extent_ref(trans, root,
3962 new_extents[i].disk_bytenr,
3963 new_extents[i].disk_num_bytes,
3964 leaf->start,
3965 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003966 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04003967 BUG_ON(ret);
3968 btrfs_release_path(root, path);
3969
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003970 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04003971
3972 ext_offset = 0;
3973 num_bytes -= extent_len;
3974 key.offset += extent_len;
3975
3976 if (num_bytes == 0)
3977 break;
3978 }
3979 BUG_ON(i >= nr_extents);
3980 }
3981
3982 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003983 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3984 lock_end, GFP_NOFS);
3985 extent_locked = 0;
3986 }
3987skip:
3988 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
3989 key.offset >= first_pos + extent_key->offset)
3990 break;
3991
3992 cond_resched();
3993 }
3994 ret = 0;
3995out:
3996 btrfs_release_path(root, path);
3997 if (inode) {
3998 mutex_unlock(&inode->i_mutex);
3999 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004000 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4001 lock_end, GFP_NOFS);
4002 }
4003 iput(inode);
4004 }
4005 return ret;
4006}
4007
Zheng Yan1a40e232008-09-26 10:09:34 -04004008int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4009 struct btrfs_root *root,
4010 struct extent_buffer *buf, u64 orig_start)
4011{
4012 int level;
4013 int ret;
4014
4015 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4016 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4017
4018 level = btrfs_header_level(buf);
4019 if (level == 0) {
4020 struct btrfs_leaf_ref *ref;
4021 struct btrfs_leaf_ref *orig_ref;
4022
4023 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4024 if (!orig_ref)
4025 return -ENOENT;
4026
4027 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4028 if (!ref) {
4029 btrfs_free_leaf_ref(root, orig_ref);
4030 return -ENOMEM;
4031 }
4032
4033 ref->nritems = orig_ref->nritems;
4034 memcpy(ref->extents, orig_ref->extents,
4035 sizeof(ref->extents[0]) * ref->nritems);
4036
4037 btrfs_free_leaf_ref(root, orig_ref);
4038
4039 ref->root_gen = trans->transid;
4040 ref->bytenr = buf->start;
4041 ref->owner = btrfs_header_owner(buf);
4042 ref->generation = btrfs_header_generation(buf);
4043 ret = btrfs_add_leaf_ref(root, ref, 0);
4044 WARN_ON(ret);
4045 btrfs_free_leaf_ref(root, ref);
4046 }
4047 return 0;
4048}
4049
4050static int noinline invalidate_extent_cache(struct btrfs_root *root,
4051 struct extent_buffer *leaf,
4052 struct btrfs_block_group_cache *group,
4053 struct btrfs_root *target_root)
4054{
4055 struct btrfs_key key;
4056 struct inode *inode = NULL;
4057 struct btrfs_file_extent_item *fi;
4058 u64 num_bytes;
4059 u64 skip_objectid = 0;
4060 u32 nritems;
4061 u32 i;
4062
4063 nritems = btrfs_header_nritems(leaf);
4064 for (i = 0; i < nritems; i++) {
4065 btrfs_item_key_to_cpu(leaf, &key, i);
4066 if (key.objectid == skip_objectid ||
4067 key.type != BTRFS_EXTENT_DATA_KEY)
4068 continue;
4069 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4070 if (btrfs_file_extent_type(leaf, fi) ==
4071 BTRFS_FILE_EXTENT_INLINE)
4072 continue;
4073 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4074 continue;
4075 if (!inode || inode->i_ino != key.objectid) {
4076 iput(inode);
4077 inode = btrfs_ilookup(target_root->fs_info->sb,
4078 key.objectid, target_root, 1);
4079 }
4080 if (!inode) {
4081 skip_objectid = key.objectid;
4082 continue;
4083 }
4084 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4085
4086 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4087 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004088 btrfs_drop_extent_cache(inode, key.offset,
4089 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004090 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4091 key.offset + num_bytes - 1, GFP_NOFS);
4092 cond_resched();
4093 }
4094 iput(inode);
4095 return 0;
4096}
4097
4098static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4099 struct btrfs_root *root,
4100 struct extent_buffer *leaf,
4101 struct btrfs_block_group_cache *group,
4102 struct inode *reloc_inode)
4103{
4104 struct btrfs_key key;
4105 struct btrfs_key extent_key;
4106 struct btrfs_file_extent_item *fi;
4107 struct btrfs_leaf_ref *ref;
4108 struct disk_extent *new_extent;
4109 u64 bytenr;
4110 u64 num_bytes;
4111 u32 nritems;
4112 u32 i;
4113 int ext_index;
4114 int nr_extent;
4115 int ret;
4116
4117 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4118 BUG_ON(!new_extent);
4119
4120 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4121 BUG_ON(!ref);
4122
4123 ext_index = -1;
4124 nritems = btrfs_header_nritems(leaf);
4125 for (i = 0; i < nritems; i++) {
4126 btrfs_item_key_to_cpu(leaf, &key, i);
4127 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4128 continue;
4129 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4130 if (btrfs_file_extent_type(leaf, fi) ==
4131 BTRFS_FILE_EXTENT_INLINE)
4132 continue;
4133 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4134 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4135 if (bytenr == 0)
4136 continue;
4137
4138 ext_index++;
4139 if (bytenr >= group->key.objectid + group->key.offset ||
4140 bytenr + num_bytes <= group->key.objectid)
4141 continue;
4142
4143 extent_key.objectid = bytenr;
4144 extent_key.offset = num_bytes;
4145 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4146 nr_extent = 1;
4147 ret = get_new_locations(reloc_inode, &extent_key,
4148 group->key.objectid, 1,
4149 &new_extent, &nr_extent);
4150 if (ret > 0)
4151 continue;
4152 BUG_ON(ret < 0);
4153
4154 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4155 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4156 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4157 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4158
4159 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Chris Masonc8b97812008-10-29 14:49:59 -04004160 btrfs_set_file_extent_ram_bytes(leaf, fi,
4161 new_extent->ram_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004162 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4163 new_extent->disk_bytenr);
4164 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4165 new_extent->disk_num_bytes);
4166 new_extent->offset += btrfs_file_extent_offset(leaf, fi);
4167 btrfs_set_file_extent_offset(leaf, fi, new_extent->offset);
4168 btrfs_mark_buffer_dirty(leaf);
4169
4170 ret = btrfs_inc_extent_ref(trans, root,
4171 new_extent->disk_bytenr,
4172 new_extent->disk_num_bytes,
4173 leaf->start,
4174 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004175 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004176 BUG_ON(ret);
4177 ret = btrfs_free_extent(trans, root,
4178 bytenr, num_bytes, leaf->start,
4179 btrfs_header_owner(leaf),
4180 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004181 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004182 BUG_ON(ret);
4183 cond_resched();
4184 }
4185 kfree(new_extent);
4186 BUG_ON(ext_index + 1 != ref->nritems);
4187 btrfs_free_leaf_ref(root, ref);
4188 return 0;
4189}
4190
Yan Zhengf82d02d2008-10-29 14:49:05 -04004191int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4192 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004193{
4194 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004195 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004196
4197 if (root->reloc_root) {
4198 reloc_root = root->reloc_root;
4199 root->reloc_root = NULL;
4200 list_add(&reloc_root->dead_list,
4201 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004202
4203 btrfs_set_root_bytenr(&reloc_root->root_item,
4204 reloc_root->node->start);
4205 btrfs_set_root_level(&root->root_item,
4206 btrfs_header_level(reloc_root->node));
4207 memset(&reloc_root->root_item.drop_progress, 0,
4208 sizeof(struct btrfs_disk_key));
4209 reloc_root->root_item.drop_level = 0;
4210
4211 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4212 &reloc_root->root_key,
4213 &reloc_root->root_item);
4214 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004215 }
4216 return 0;
4217}
4218
4219int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4220{
4221 struct btrfs_trans_handle *trans;
4222 struct btrfs_root *reloc_root;
4223 struct btrfs_root *prev_root = NULL;
4224 struct list_head dead_roots;
4225 int ret;
4226 unsigned long nr;
4227
4228 INIT_LIST_HEAD(&dead_roots);
4229 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4230
4231 while (!list_empty(&dead_roots)) {
4232 reloc_root = list_entry(dead_roots.prev,
4233 struct btrfs_root, dead_list);
4234 list_del_init(&reloc_root->dead_list);
4235
4236 BUG_ON(reloc_root->commit_root != NULL);
4237 while (1) {
4238 trans = btrfs_join_transaction(root, 1);
4239 BUG_ON(!trans);
4240
4241 mutex_lock(&root->fs_info->drop_mutex);
4242 ret = btrfs_drop_snapshot(trans, reloc_root);
4243 if (ret != -EAGAIN)
4244 break;
4245 mutex_unlock(&root->fs_info->drop_mutex);
4246
4247 nr = trans->blocks_used;
4248 ret = btrfs_end_transaction(trans, root);
4249 BUG_ON(ret);
4250 btrfs_btree_balance_dirty(root, nr);
4251 }
4252
4253 free_extent_buffer(reloc_root->node);
4254
4255 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4256 &reloc_root->root_key);
4257 BUG_ON(ret);
4258 mutex_unlock(&root->fs_info->drop_mutex);
4259
4260 nr = trans->blocks_used;
4261 ret = btrfs_end_transaction(trans, root);
4262 BUG_ON(ret);
4263 btrfs_btree_balance_dirty(root, nr);
4264
4265 kfree(prev_root);
4266 prev_root = reloc_root;
4267 }
4268 if (prev_root) {
4269 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4270 kfree(prev_root);
4271 }
4272 return 0;
4273}
4274
4275int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4276{
4277 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4278 return 0;
4279}
4280
4281int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4282{
4283 struct btrfs_root *reloc_root;
4284 struct btrfs_trans_handle *trans;
4285 struct btrfs_key location;
4286 int found;
4287 int ret;
4288
4289 mutex_lock(&root->fs_info->tree_reloc_mutex);
4290 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4291 BUG_ON(ret);
4292 found = !list_empty(&root->fs_info->dead_reloc_roots);
4293 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4294
4295 if (found) {
4296 trans = btrfs_start_transaction(root, 1);
4297 BUG_ON(!trans);
4298 ret = btrfs_commit_transaction(trans, root);
4299 BUG_ON(ret);
4300 }
4301
4302 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4303 location.offset = (u64)-1;
4304 location.type = BTRFS_ROOT_ITEM_KEY;
4305
4306 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
4307 BUG_ON(!reloc_root);
4308 btrfs_orphan_cleanup(reloc_root);
4309 return 0;
4310}
4311
4312static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
4313 struct btrfs_root *root)
4314{
4315 struct btrfs_root *reloc_root;
4316 struct extent_buffer *eb;
4317 struct btrfs_root_item *root_item;
4318 struct btrfs_key root_key;
4319 int ret;
4320
4321 BUG_ON(!root->ref_cows);
4322 if (root->reloc_root)
4323 return 0;
4324
4325 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
4326 BUG_ON(!root_item);
4327
4328 ret = btrfs_copy_root(trans, root, root->commit_root,
4329 &eb, BTRFS_TREE_RELOC_OBJECTID);
4330 BUG_ON(ret);
4331
4332 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4333 root_key.offset = root->root_key.objectid;
4334 root_key.type = BTRFS_ROOT_ITEM_KEY;
4335
4336 memcpy(root_item, &root->root_item, sizeof(root_item));
4337 btrfs_set_root_refs(root_item, 0);
4338 btrfs_set_root_bytenr(root_item, eb->start);
4339 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04004340 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004341
4342 btrfs_tree_unlock(eb);
4343 free_extent_buffer(eb);
4344
4345 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
4346 &root_key, root_item);
4347 BUG_ON(ret);
4348 kfree(root_item);
4349
4350 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
4351 &root_key);
4352 BUG_ON(!reloc_root);
4353 reloc_root->last_trans = trans->transid;
4354 reloc_root->commit_root = NULL;
4355 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
4356
4357 root->reloc_root = reloc_root;
4358 return 0;
4359}
4360
4361/*
4362 * Core function of space balance.
4363 *
4364 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04004365 * counted roots. There is one reloc tree for each subvol, and all
4366 * reloc trees share same root key objectid. Reloc trees are snapshots
4367 * of the latest committed roots of subvols (root->commit_root).
4368 *
4369 * To relocate a tree block referenced by a subvol, there are two steps.
4370 * COW the block through subvol's reloc tree, then update block pointer
4371 * in the subvol to point to the new block. Since all reloc trees share
4372 * same root key objectid, doing special handing for tree blocks owned
4373 * by them is easy. Once a tree block has been COWed in one reloc tree,
4374 * we can use the resulting new block directly when the same block is
4375 * required to COW again through other reloc trees. By this way, relocated
4376 * tree blocks are shared between reloc trees, so they are also shared
4377 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04004378 */
4379static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
4380 struct btrfs_root *root,
4381 struct btrfs_path *path,
4382 struct btrfs_key *first_key,
4383 struct btrfs_ref_path *ref_path,
4384 struct btrfs_block_group_cache *group,
4385 struct inode *reloc_inode)
4386{
4387 struct btrfs_root *reloc_root;
4388 struct extent_buffer *eb = NULL;
4389 struct btrfs_key *keys;
4390 u64 *nodes;
4391 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004392 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004393 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004394 int ret;
4395
4396 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
4397 lowest_level = ref_path->owner_objectid;
4398
Yan Zhengf82d02d2008-10-29 14:49:05 -04004399 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004400 path->lowest_level = lowest_level;
4401 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
4402 BUG_ON(ret < 0);
4403 path->lowest_level = 0;
4404 btrfs_release_path(root, path);
4405 return 0;
4406 }
4407
Zheng Yan1a40e232008-09-26 10:09:34 -04004408 mutex_lock(&root->fs_info->tree_reloc_mutex);
4409 ret = init_reloc_tree(trans, root);
4410 BUG_ON(ret);
4411 reloc_root = root->reloc_root;
4412
Yan Zhengf82d02d2008-10-29 14:49:05 -04004413 shared_level = ref_path->shared_level;
4414 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004415
Yan Zhengf82d02d2008-10-29 14:49:05 -04004416 keys = ref_path->node_keys;
4417 nodes = ref_path->new_nodes;
4418 memset(&keys[shared_level + 1], 0,
4419 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
4420 memset(&nodes[shared_level + 1], 0,
4421 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04004422
Yan Zhengf82d02d2008-10-29 14:49:05 -04004423 if (nodes[lowest_level] == 0) {
4424 path->lowest_level = lowest_level;
4425 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
4426 0, 1);
4427 BUG_ON(ret);
4428 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
4429 eb = path->nodes[level];
4430 if (!eb || eb == reloc_root->node)
4431 break;
4432 nodes[level] = eb->start;
4433 if (level == 0)
4434 btrfs_item_key_to_cpu(eb, &keys[level], 0);
4435 else
4436 btrfs_node_key_to_cpu(eb, &keys[level], 0);
4437 }
4438 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4439 eb = path->nodes[0];
4440 ret = replace_extents_in_leaf(trans, reloc_root, eb,
4441 group, reloc_inode);
4442 BUG_ON(ret);
4443 }
4444 btrfs_release_path(reloc_root, path);
4445 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04004446 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04004447 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04004448 BUG_ON(ret);
4449 }
4450
Zheng Yan1a40e232008-09-26 10:09:34 -04004451 /*
4452 * replace tree blocks in the fs tree with tree blocks in
4453 * the reloc tree.
4454 */
4455 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
4456 BUG_ON(ret < 0);
4457
4458 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04004459 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
4460 0, 0);
4461 BUG_ON(ret);
4462 extent_buffer_get(path->nodes[0]);
4463 eb = path->nodes[0];
4464 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004465 ret = invalidate_extent_cache(reloc_root, eb, group, root);
4466 BUG_ON(ret);
4467 free_extent_buffer(eb);
4468 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004469
Yan Zhengf82d02d2008-10-29 14:49:05 -04004470 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04004471 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004472 return 0;
4473}
4474
4475static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
4476 struct btrfs_root *root,
4477 struct btrfs_path *path,
4478 struct btrfs_key *first_key,
4479 struct btrfs_ref_path *ref_path)
4480{
4481 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004482
4483 ret = relocate_one_path(trans, root, path, first_key,
4484 ref_path, NULL, NULL);
4485 BUG_ON(ret);
4486
4487 if (root == root->fs_info->extent_root)
4488 btrfs_extent_post_op(trans, root);
Zheng Yan1a40e232008-09-26 10:09:34 -04004489
4490 return 0;
4491}
4492
4493static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
4494 struct btrfs_root *extent_root,
4495 struct btrfs_path *path,
4496 struct btrfs_key *extent_key)
4497{
4498 int ret;
4499
Zheng Yan1a40e232008-09-26 10:09:34 -04004500 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
4501 if (ret)
4502 goto out;
4503 ret = btrfs_del_item(trans, extent_root, path);
4504out:
Chris Masonedbd8d42007-12-21 16:27:24 -05004505 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004506 return ret;
4507}
4508
4509static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
4510 struct btrfs_ref_path *ref_path)
4511{
4512 struct btrfs_key root_key;
4513
4514 root_key.objectid = ref_path->root_objectid;
4515 root_key.type = BTRFS_ROOT_ITEM_KEY;
4516 if (is_cowonly_root(ref_path->root_objectid))
4517 root_key.offset = 0;
4518 else
4519 root_key.offset = (u64)-1;
4520
4521 return btrfs_read_fs_root_no_name(fs_info, &root_key);
4522}
4523
4524static int noinline relocate_one_extent(struct btrfs_root *extent_root,
4525 struct btrfs_path *path,
4526 struct btrfs_key *extent_key,
4527 struct btrfs_block_group_cache *group,
4528 struct inode *reloc_inode, int pass)
4529{
4530 struct btrfs_trans_handle *trans;
4531 struct btrfs_root *found_root;
4532 struct btrfs_ref_path *ref_path = NULL;
4533 struct disk_extent *new_extents = NULL;
4534 int nr_extents = 0;
4535 int loops;
4536 int ret;
4537 int level;
4538 struct btrfs_key first_key;
4539 u64 prev_block = 0;
4540
Zheng Yan1a40e232008-09-26 10:09:34 -04004541
4542 trans = btrfs_start_transaction(extent_root, 1);
4543 BUG_ON(!trans);
4544
4545 if (extent_key->objectid == 0) {
4546 ret = del_extent_zero(trans, extent_root, path, extent_key);
4547 goto out;
4548 }
4549
4550 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
4551 if (!ref_path) {
4552 ret = -ENOMEM;
4553 goto out;
4554 }
4555
4556 for (loops = 0; ; loops++) {
4557 if (loops == 0) {
4558 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
4559 extent_key->objectid);
4560 } else {
4561 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
4562 }
4563 if (ret < 0)
4564 goto out;
4565 if (ret > 0)
4566 break;
4567
4568 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4569 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
4570 continue;
4571
4572 found_root = read_ref_root(extent_root->fs_info, ref_path);
4573 BUG_ON(!found_root);
4574 /*
4575 * for reference counted tree, only process reference paths
4576 * rooted at the latest committed root.
4577 */
4578 if (found_root->ref_cows &&
4579 ref_path->root_generation != found_root->root_key.offset)
4580 continue;
4581
4582 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4583 if (pass == 0) {
4584 /*
4585 * copy data extents to new locations
4586 */
4587 u64 group_start = group->key.objectid;
4588 ret = relocate_data_extent(reloc_inode,
4589 extent_key,
4590 group_start);
4591 if (ret < 0)
4592 goto out;
4593 break;
4594 }
4595 level = 0;
4596 } else {
4597 level = ref_path->owner_objectid;
4598 }
4599
4600 if (prev_block != ref_path->nodes[level]) {
4601 struct extent_buffer *eb;
4602 u64 block_start = ref_path->nodes[level];
4603 u64 block_size = btrfs_level_size(found_root, level);
4604
4605 eb = read_tree_block(found_root, block_start,
4606 block_size, 0);
4607 btrfs_tree_lock(eb);
4608 BUG_ON(level != btrfs_header_level(eb));
4609
4610 if (level == 0)
4611 btrfs_item_key_to_cpu(eb, &first_key, 0);
4612 else
4613 btrfs_node_key_to_cpu(eb, &first_key, 0);
4614
4615 btrfs_tree_unlock(eb);
4616 free_extent_buffer(eb);
4617 prev_block = block_start;
4618 }
4619
4620 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
4621 pass >= 2) {
4622 /*
4623 * use fallback method to process the remaining
4624 * references.
4625 */
4626 if (!new_extents) {
4627 u64 group_start = group->key.objectid;
4628 ret = get_new_locations(reloc_inode,
4629 extent_key,
4630 group_start, 0,
4631 &new_extents,
4632 &nr_extents);
4633 if (ret < 0)
4634 goto out;
4635 }
4636 btrfs_record_root_in_trans(found_root);
4637 ret = replace_one_extent(trans, found_root,
4638 path, extent_key,
4639 &first_key, ref_path,
4640 new_extents, nr_extents);
4641 if (ret < 0)
4642 goto out;
4643 continue;
4644 }
4645
4646 btrfs_record_root_in_trans(found_root);
4647 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4648 ret = relocate_tree_block(trans, found_root, path,
4649 &first_key, ref_path);
4650 } else {
4651 /*
4652 * try to update data extent references while
4653 * keeping metadata shared between snapshots.
4654 */
4655 ret = relocate_one_path(trans, found_root, path,
4656 &first_key, ref_path,
4657 group, reloc_inode);
4658 }
4659 if (ret < 0)
4660 goto out;
4661 }
4662 ret = 0;
4663out:
4664 btrfs_end_transaction(trans, extent_root);
4665 kfree(new_extents);
4666 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05004667 return ret;
4668}
4669
Chris Masonec44a352008-04-28 15:29:52 -04004670static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
4671{
4672 u64 num_devices;
4673 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
4674 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
4675
Chris Masona061fc82008-05-07 11:43:44 -04004676 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04004677 if (num_devices == 1) {
4678 stripped |= BTRFS_BLOCK_GROUP_DUP;
4679 stripped = flags & ~stripped;
4680
4681 /* turn raid0 into single device chunks */
4682 if (flags & BTRFS_BLOCK_GROUP_RAID0)
4683 return stripped;
4684
4685 /* turn mirroring into duplication */
4686 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
4687 BTRFS_BLOCK_GROUP_RAID10))
4688 return stripped | BTRFS_BLOCK_GROUP_DUP;
4689 return flags;
4690 } else {
4691 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04004692 if (flags & stripped)
4693 return flags;
4694
4695 stripped |= BTRFS_BLOCK_GROUP_DUP;
4696 stripped = flags & ~stripped;
4697
4698 /* switch duplicated blocks with raid1 */
4699 if (flags & BTRFS_BLOCK_GROUP_DUP)
4700 return stripped | BTRFS_BLOCK_GROUP_RAID1;
4701
4702 /* turn single device chunks into raid0 */
4703 return stripped | BTRFS_BLOCK_GROUP_RAID0;
4704 }
4705 return flags;
4706}
4707
Chris Mason0ef3e662008-05-24 14:04:53 -04004708int __alloc_chunk_for_shrink(struct btrfs_root *root,
4709 struct btrfs_block_group_cache *shrink_block_group,
4710 int force)
4711{
4712 struct btrfs_trans_handle *trans;
4713 u64 new_alloc_flags;
4714 u64 calc;
4715
Chris Masonc286ac42008-07-22 23:06:41 -04004716 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04004717 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04004718 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004719
Chris Mason0ef3e662008-05-24 14:04:53 -04004720 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04004721 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04004722
Chris Mason0ef3e662008-05-24 14:04:53 -04004723 new_alloc_flags = update_block_group_flags(root,
4724 shrink_block_group->flags);
4725 if (new_alloc_flags != shrink_block_group->flags) {
4726 calc =
4727 btrfs_block_group_used(&shrink_block_group->item);
4728 } else {
4729 calc = shrink_block_group->key.offset;
4730 }
Chris Masonc286ac42008-07-22 23:06:41 -04004731 spin_unlock(&shrink_block_group->lock);
4732
Chris Mason0ef3e662008-05-24 14:04:53 -04004733 do_chunk_alloc(trans, root->fs_info->extent_root,
4734 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04004735
Chris Mason0ef3e662008-05-24 14:04:53 -04004736 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04004737 } else
4738 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04004739 return 0;
4740}
4741
Zheng Yan1a40e232008-09-26 10:09:34 -04004742static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
4743 struct btrfs_root *root,
4744 u64 objectid, u64 size)
4745{
4746 struct btrfs_path *path;
4747 struct btrfs_inode_item *item;
4748 struct extent_buffer *leaf;
4749 int ret;
4750
4751 path = btrfs_alloc_path();
4752 if (!path)
4753 return -ENOMEM;
4754
4755 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
4756 if (ret)
4757 goto out;
4758
4759 leaf = path->nodes[0];
4760 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
4761 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
4762 btrfs_set_inode_generation(leaf, item, 1);
4763 btrfs_set_inode_size(leaf, item, size);
4764 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
4765 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM);
4766 btrfs_mark_buffer_dirty(leaf);
4767 btrfs_release_path(root, path);
4768out:
4769 btrfs_free_path(path);
4770 return ret;
4771}
4772
4773static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
4774 struct btrfs_block_group_cache *group)
4775{
4776 struct inode *inode = NULL;
4777 struct btrfs_trans_handle *trans;
4778 struct btrfs_root *root;
4779 struct btrfs_key root_key;
4780 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
4781 int err = 0;
4782
4783 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4784 root_key.type = BTRFS_ROOT_ITEM_KEY;
4785 root_key.offset = (u64)-1;
4786 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
4787 if (IS_ERR(root))
4788 return ERR_CAST(root);
4789
4790 trans = btrfs_start_transaction(root, 1);
4791 BUG_ON(!trans);
4792
4793 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
4794 if (err)
4795 goto out;
4796
4797 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
4798 BUG_ON(err);
4799
4800 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04004801 group->key.offset, 0, group->key.offset,
4802 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004803 BUG_ON(err);
4804
4805 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
4806 if (inode->i_state & I_NEW) {
4807 BTRFS_I(inode)->root = root;
4808 BTRFS_I(inode)->location.objectid = objectid;
4809 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
4810 BTRFS_I(inode)->location.offset = 0;
4811 btrfs_read_locked_inode(inode);
4812 unlock_new_inode(inode);
4813 BUG_ON(is_bad_inode(inode));
4814 } else {
4815 BUG_ON(1);
4816 }
4817
4818 err = btrfs_orphan_add(trans, inode);
4819out:
4820 btrfs_end_transaction(trans, root);
4821 if (err) {
4822 if (inode)
4823 iput(inode);
4824 inode = ERR_PTR(err);
4825 }
4826 return inode;
4827}
4828
4829int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05004830{
4831 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05004832 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04004833 struct btrfs_fs_info *info = root->fs_info;
4834 struct extent_buffer *leaf;
4835 struct inode *reloc_inode;
4836 struct btrfs_block_group_cache *block_group;
4837 struct btrfs_key key;
Chris Masonedbd8d42007-12-21 16:27:24 -05004838 u64 cur_byte;
4839 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05004840 u32 nritems;
4841 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04004842 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04004843 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05004844
Chris Masonedbd8d42007-12-21 16:27:24 -05004845 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04004846
4847 block_group = btrfs_lookup_block_group(info, group_start);
4848 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05004849
Chris Mason323da792008-05-09 11:46:48 -04004850 printk("btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04004851 (unsigned long long)block_group->key.objectid,
4852 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04004853
Zheng Yan1a40e232008-09-26 10:09:34 -04004854 path = btrfs_alloc_path();
4855 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04004856
Zheng Yan1a40e232008-09-26 10:09:34 -04004857 reloc_inode = create_reloc_inode(info, block_group);
4858 BUG_ON(IS_ERR(reloc_inode));
4859
Zheng Yan1a40e232008-09-26 10:09:34 -04004860 __alloc_chunk_for_shrink(root, block_group, 1);
4861 block_group->ro = 1;
4862 block_group->space_info->total_bytes -= block_group->key.offset;
4863
Zheng Yan1a40e232008-09-26 10:09:34 -04004864 btrfs_start_delalloc_inodes(info->tree_root);
4865 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04004866again:
Chris Masonedbd8d42007-12-21 16:27:24 -05004867 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04004868 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004869 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05004870 key.offset = 0;
4871 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05004872 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05004873
Zheng Yan1a40e232008-09-26 10:09:34 -04004874 trans = btrfs_start_transaction(info->tree_root, 1);
4875 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04004876
Zheng Yan1a40e232008-09-26 10:09:34 -04004877 mutex_lock(&root->fs_info->cleaner_mutex);
4878 btrfs_clean_old_snapshots(info->tree_root);
4879 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
4880 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04004881
Yan73e48b22008-01-03 14:14:39 -05004882 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05004883 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4884 if (ret < 0)
4885 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04004886next:
Chris Masonedbd8d42007-12-21 16:27:24 -05004887 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05004888 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05004889 if (path->slots[0] >= nritems) {
4890 ret = btrfs_next_leaf(root, path);
4891 if (ret < 0)
4892 goto out;
4893 if (ret == 1) {
4894 ret = 0;
4895 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004896 }
Yan73e48b22008-01-03 14:14:39 -05004897 leaf = path->nodes[0];
4898 nritems = btrfs_header_nritems(leaf);
4899 }
4900
Zheng Yan1a40e232008-09-26 10:09:34 -04004901 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05004902
Zheng Yan1a40e232008-09-26 10:09:34 -04004903 if (key.objectid >= block_group->key.objectid +
4904 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04004905 break;
4906
Chris Mason725c8462008-01-04 16:47:16 -05004907 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05004908 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004909 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05004910 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004911 continue;
Chris Mason725c8462008-01-04 16:47:16 -05004912 }
4913 progress = 1;
4914
Zheng Yan1a40e232008-09-26 10:09:34 -04004915 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
4916 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05004917 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05004918 goto next;
4919 }
Yan73e48b22008-01-03 14:14:39 -05004920
Chris Masonedbd8d42007-12-21 16:27:24 -05004921 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04004922 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05004923 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004924
4925 __alloc_chunk_for_shrink(root, block_group, 0);
4926 ret = relocate_one_extent(root, path, &key, block_group,
4927 reloc_inode, pass);
4928 BUG_ON(ret < 0);
4929
4930 key.objectid = cur_byte;
4931 key.type = 0;
4932 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05004933 }
4934
4935 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004936
4937 if (pass == 0) {
4938 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
4939 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
4940 WARN_ON(reloc_inode->i_mapping->nrpages);
4941 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004942
4943 if (total_found > 0) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004944 printk("btrfs found %llu extents in pass %d\n",
4945 (unsigned long long)total_found, pass);
4946 pass++;
Chris Masonedbd8d42007-12-21 16:27:24 -05004947 goto again;
4948 }
4949
Zheng Yan1a40e232008-09-26 10:09:34 -04004950 /* delete reloc_inode */
4951 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04004952
Zheng Yan1a40e232008-09-26 10:09:34 -04004953 /* unpin extents in this range */
4954 trans = btrfs_start_transaction(info->tree_root, 1);
4955 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04004956
Zheng Yan1a40e232008-09-26 10:09:34 -04004957 spin_lock(&block_group->lock);
4958 WARN_ON(block_group->pinned > 0);
4959 WARN_ON(block_group->reserved > 0);
4960 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
4961 spin_unlock(&block_group->lock);
4962 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05004963out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004964 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05004965 return ret;
4966}
4967
Chris Mason0b86a832008-03-24 15:01:56 -04004968int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
4969 struct btrfs_key *key)
4970{
Chris Mason925baed2008-06-25 16:01:30 -04004971 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004972 struct btrfs_key found_key;
4973 struct extent_buffer *leaf;
4974 int slot;
4975
4976 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
4977 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04004978 goto out;
4979
Chris Mason0b86a832008-03-24 15:01:56 -04004980 while(1) {
4981 slot = path->slots[0];
4982 leaf = path->nodes[0];
4983 if (slot >= btrfs_header_nritems(leaf)) {
4984 ret = btrfs_next_leaf(root, path);
4985 if (ret == 0)
4986 continue;
4987 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04004988 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04004989 break;
4990 }
4991 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4992
4993 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04004994 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
4995 ret = 0;
4996 goto out;
4997 }
Chris Mason0b86a832008-03-24 15:01:56 -04004998 path->slots[0]++;
4999 }
5000 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005001out:
Chris Mason0b86a832008-03-24 15:01:56 -04005002 return ret;
5003}
5004
Zheng Yan1a40e232008-09-26 10:09:34 -04005005int btrfs_free_block_groups(struct btrfs_fs_info *info)
5006{
5007 struct btrfs_block_group_cache *block_group;
5008 struct rb_node *n;
5009
Zheng Yan1a40e232008-09-26 10:09:34 -04005010 spin_lock(&info->block_group_cache_lock);
5011 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5012 block_group = rb_entry(n, struct btrfs_block_group_cache,
5013 cache_node);
5014
5015 spin_unlock(&info->block_group_cache_lock);
5016 btrfs_remove_free_space_cache(block_group);
5017 spin_lock(&info->block_group_cache_lock);
5018
5019 rb_erase(&block_group->cache_node,
5020 &info->block_group_cache_tree);
Josef Bacik80eb2342008-10-29 14:49:05 -04005021 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005022 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005023 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005024 kfree(block_group);
5025 }
5026 spin_unlock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005027 return 0;
5028}
5029
Chris Mason9078a3e2007-04-26 16:46:15 -04005030int btrfs_read_block_groups(struct btrfs_root *root)
5031{
5032 struct btrfs_path *path;
5033 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005034 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005035 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005036 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005037 struct btrfs_key key;
5038 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005039 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005040
Chris Masonbe744172007-05-06 10:15:01 -04005041 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005042 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005043 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005044 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005045 path = btrfs_alloc_path();
5046 if (!path)
5047 return -ENOMEM;
5048
5049 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005050 ret = find_first_block_group(root, path, &key);
5051 if (ret > 0) {
5052 ret = 0;
5053 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005054 }
Chris Mason0b86a832008-03-24 15:01:56 -04005055 if (ret != 0)
5056 goto error;
5057
Chris Mason5f39d392007-10-15 16:14:19 -04005058 leaf = path->nodes[0];
5059 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005060 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005061 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005062 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005063 break;
5064 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005065
Chris Masonc286ac42008-07-22 23:06:41 -04005066 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005067 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005068 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005069 read_extent_buffer(leaf, &cache->item,
5070 btrfs_item_ptr_offset(leaf, path->slots[0]),
5071 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005072 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005073
Chris Mason9078a3e2007-04-26 16:46:15 -04005074 key.objectid = found_key.objectid + found_key.offset;
5075 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005076 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005077
Chris Mason6324fbf2008-03-24 15:01:59 -04005078 ret = update_space_info(info, cache->flags, found_key.offset,
5079 btrfs_block_group_used(&cache->item),
5080 &space_info);
5081 BUG_ON(ret);
5082 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005083 down_write(&space_info->groups_sem);
5084 list_add_tail(&cache->list, &space_info->block_groups);
5085 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005086
Josef Bacik0f9dd462008-09-23 13:14:11 -04005087 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5088 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005089
5090 set_avail_alloc_bits(root->fs_info, cache->flags);
Chris Mason9078a3e2007-04-26 16:46:15 -04005091 }
Chris Mason0b86a832008-03-24 15:01:56 -04005092 ret = 0;
5093error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005094 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005095 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005096}
Chris Mason6324fbf2008-03-24 15:01:59 -04005097
5098int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5099 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005100 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005101 u64 size)
5102{
5103 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005104 struct btrfs_root *extent_root;
5105 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005106
5107 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005108
Chris Masone02119d2008-09-05 16:13:11 -04005109 root->fs_info->last_trans_new_blockgroup = trans->transid;
5110
Chris Mason8f18cf12008-04-25 16:53:30 -04005111 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005112 if (!cache)
5113 return -ENOMEM;
5114
Chris Masone17cade2008-04-15 15:41:47 -04005115 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005116 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04005117 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005118 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005119 INIT_LIST_HEAD(&cache->list);
Chris Mason6324fbf2008-03-24 15:01:59 -04005120 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04005121
Chris Mason6324fbf2008-03-24 15:01:59 -04005122 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005123 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5124 cache->flags = type;
5125 btrfs_set_block_group_flags(&cache->item, type);
5126
5127 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5128 &cache->space_info);
5129 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005130 down_write(&cache->space_info->groups_sem);
5131 list_add_tail(&cache->list, &cache->space_info->block_groups);
5132 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005133
Josef Bacik0f9dd462008-09-23 13:14:11 -04005134 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5135 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005136
Chris Mason6324fbf2008-03-24 15:01:59 -04005137 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5138 sizeof(cache->item));
5139 BUG_ON(ret);
5140
Chris Mason87ef2bb2008-10-30 11:23:27 -04005141 finish_current_insert(trans, extent_root, 0);
5142 ret = del_pending_extents(trans, extent_root, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04005143 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04005144 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005145
Chris Mason6324fbf2008-03-24 15:01:59 -04005146 return 0;
5147}
Zheng Yan1a40e232008-09-26 10:09:34 -04005148
5149int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5150 struct btrfs_root *root, u64 group_start)
5151{
5152 struct btrfs_path *path;
5153 struct btrfs_block_group_cache *block_group;
5154 struct btrfs_key key;
5155 int ret;
5156
Zheng Yan1a40e232008-09-26 10:09:34 -04005157 root = root->fs_info->extent_root;
5158
5159 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5160 BUG_ON(!block_group);
5161
5162 memcpy(&key, &block_group->key, sizeof(key));
5163
5164 path = btrfs_alloc_path();
5165 BUG_ON(!path);
5166
5167 btrfs_remove_free_space_cache(block_group);
5168 rb_erase(&block_group->cache_node,
5169 &root->fs_info->block_group_cache_tree);
Josef Bacik80eb2342008-10-29 14:49:05 -04005170 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005171 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005172 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005173
5174 /*
5175 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5176 kfree(shrink_block_group);
5177 */
5178
5179 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5180 if (ret > 0)
5181 ret = -EIO;
5182 if (ret < 0)
5183 goto out;
5184
5185 ret = btrfs_del_item(trans, root, path);
5186out:
5187 btrfs_free_path(path);
5188 return ret;
5189}