blob: ec43fa526d777713f102b4f5529607024404f7de [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050022#include <linux/version.h>
23#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050024#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040025#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050026#include "ctree.h"
27#include "disk-io.h"
28#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040029#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040030#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040031#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040032#include "ref-cache.h"
Chris Mason15916de2008-11-19 21:17:22 -050033#include "compat.h"
Chris Masonfec577f2007-02-26 10:40:21 -050034
Zheng Yan31840ae2008-09-23 13:14:14 -040035#define PENDING_EXTENT_INSERT 0
36#define PENDING_EXTENT_DELETE 1
37#define PENDING_BACKREF_UPDATE 2
38
39struct pending_extent_op {
40 int type;
41 u64 bytenr;
42 u64 num_bytes;
43 u64 parent;
44 u64 orig_parent;
45 u64 generation;
46 u64 orig_generation;
47 int level;
Josef Bacikf3465ca2008-11-12 14:19:50 -050048 struct list_head list;
49 int del;
Zheng Yan31840ae2008-09-23 13:14:14 -040050};
51
Chris Masond3977122009-01-05 21:25:51 -050052static int finish_current_insert(struct btrfs_trans_handle *trans,
53 struct btrfs_root *extent_root, int all);
54static int del_pending_extents(struct btrfs_trans_handle *trans,
55 struct btrfs_root *extent_root, int all);
Josef Bacikf3465ca2008-11-12 14:19:50 -050056static int pin_down_bytes(struct btrfs_trans_handle *trans,
57 struct btrfs_root *root,
58 u64 bytenr, u64 num_bytes, int is_data);
59static int update_block_group(struct btrfs_trans_handle *trans,
60 struct btrfs_root *root,
61 u64 bytenr, u64 num_bytes, int alloc,
62 int mark_free);
Yand548ee52008-01-03 13:56:30 -050063
Josef Bacik0f9dd462008-09-23 13:14:11 -040064static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
65{
66 return (cache->flags & bits) == bits;
67}
68
69/*
70 * this adds the block group to the fs_info rb tree for the block group
71 * cache
72 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050073static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -040074 struct btrfs_block_group_cache *block_group)
75{
76 struct rb_node **p;
77 struct rb_node *parent = NULL;
78 struct btrfs_block_group_cache *cache;
79
80 spin_lock(&info->block_group_cache_lock);
81 p = &info->block_group_cache_tree.rb_node;
82
83 while (*p) {
84 parent = *p;
85 cache = rb_entry(parent, struct btrfs_block_group_cache,
86 cache_node);
87 if (block_group->key.objectid < cache->key.objectid) {
88 p = &(*p)->rb_left;
89 } else if (block_group->key.objectid > cache->key.objectid) {
90 p = &(*p)->rb_right;
91 } else {
92 spin_unlock(&info->block_group_cache_lock);
93 return -EEXIST;
94 }
95 }
96
97 rb_link_node(&block_group->cache_node, parent, p);
98 rb_insert_color(&block_group->cache_node,
99 &info->block_group_cache_tree);
100 spin_unlock(&info->block_group_cache_lock);
101
102 return 0;
103}
104
105/*
106 * This will return the block group at or after bytenr if contains is 0, else
107 * it will return the block group that contains the bytenr
108 */
109static struct btrfs_block_group_cache *
110block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
111 int contains)
112{
113 struct btrfs_block_group_cache *cache, *ret = NULL;
114 struct rb_node *n;
115 u64 end, start;
116
117 spin_lock(&info->block_group_cache_lock);
118 n = info->block_group_cache_tree.rb_node;
119
120 while (n) {
121 cache = rb_entry(n, struct btrfs_block_group_cache,
122 cache_node);
123 end = cache->key.objectid + cache->key.offset - 1;
124 start = cache->key.objectid;
125
126 if (bytenr < start) {
127 if (!contains && (!ret || start < ret->key.objectid))
128 ret = cache;
129 n = n->rb_left;
130 } else if (bytenr > start) {
131 if (contains && bytenr <= end) {
132 ret = cache;
133 break;
134 }
135 n = n->rb_right;
136 } else {
137 ret = cache;
138 break;
139 }
140 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500141 if (ret)
142 atomic_inc(&ret->count);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400143 spin_unlock(&info->block_group_cache_lock);
144
145 return ret;
146}
147
148/*
149 * this is only called by cache_block_group, since we could have freed extents
150 * we need to check the pinned_extents for any extents that can't be used yet
151 * since their free space will be released as soon as the transaction commits.
152 */
153static int add_new_free_space(struct btrfs_block_group_cache *block_group,
154 struct btrfs_fs_info *info, u64 start, u64 end)
155{
156 u64 extent_start, extent_end, size;
157 int ret;
158
Josef Bacik25179202008-10-29 14:49:05 -0400159 mutex_lock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400160 while (start < end) {
161 ret = find_first_extent_bit(&info->pinned_extents, start,
162 &extent_start, &extent_end,
163 EXTENT_DIRTY);
164 if (ret)
165 break;
166
167 if (extent_start == start) {
168 start = extent_end + 1;
169 } else if (extent_start > start && extent_start < end) {
170 size = extent_start - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500171 ret = btrfs_add_free_space(block_group, start,
172 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400173 BUG_ON(ret);
174 start = extent_end + 1;
175 } else {
176 break;
177 }
178 }
179
180 if (start < end) {
181 size = end - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500182 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400183 BUG_ON(ret);
184 }
Josef Bacik25179202008-10-29 14:49:05 -0400185 mutex_unlock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400186
187 return 0;
188}
189
Yan Zhenga512bbf2008-12-08 16:46:26 -0500190static int remove_sb_from_cache(struct btrfs_root *root,
191 struct btrfs_block_group_cache *cache)
192{
193 u64 bytenr;
194 u64 *logical;
195 int stripe_len;
196 int i, nr, ret;
197
198 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
199 bytenr = btrfs_sb_offset(i);
200 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
201 cache->key.objectid, bytenr, 0,
202 &logical, &nr, &stripe_len);
203 BUG_ON(ret);
204 while (nr--) {
205 btrfs_remove_free_space(cache, logical[nr],
206 stripe_len);
207 }
208 kfree(logical);
209 }
210 return 0;
211}
212
Chris Masone37c9e62007-05-09 20:13:14 -0400213static int cache_block_group(struct btrfs_root *root,
214 struct btrfs_block_group_cache *block_group)
215{
216 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400217 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400218 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400219 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400220 int slot;
Yan Zhenge4404d62008-12-12 10:03:26 -0500221 u64 last;
Chris Masone37c9e62007-05-09 20:13:14 -0400222
Chris Mason00f5c792007-11-30 10:09:33 -0500223 if (!block_group)
224 return 0;
225
Chris Masone37c9e62007-05-09 20:13:14 -0400226 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400227
228 if (block_group->cached)
229 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400230
Chris Masone37c9e62007-05-09 20:13:14 -0400231 path = btrfs_alloc_path();
232 if (!path)
233 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400234
Chris Mason2cc58cf2007-08-27 16:49:44 -0400235 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400236 /*
237 * we get into deadlocks with paths held by callers of this function.
238 * since the alloc_mutex is protecting things right now, just
239 * skip the locking here
240 */
241 path->skip_locking = 1;
Yan Zhenge4404d62008-12-12 10:03:26 -0500242 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
243 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400244 key.offset = 0;
245 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
246 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
247 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400248 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500249
Chris Masond3977122009-01-05 21:25:51 -0500250 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400251 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400252 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400253 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400254 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400255 if (ret < 0)
256 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400257 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400258 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400259 else
Chris Masone37c9e62007-05-09 20:13:14 -0400260 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400261 }
Chris Mason5f39d392007-10-15 16:14:19 -0400262 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400263 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400264 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400265
Chris Masone37c9e62007-05-09 20:13:14 -0400266 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400267 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400268 break;
Yan7d7d6062007-09-14 16:15:28 -0400269
270 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400271 add_new_free_space(block_group, root->fs_info, last,
272 key.objectid);
273
Yan7d7d6062007-09-14 16:15:28 -0400274 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400275 }
Yan7d7d6062007-09-14 16:15:28 -0400276next:
Chris Masone37c9e62007-05-09 20:13:14 -0400277 path->slots[0]++;
278 }
279
Josef Bacik0f9dd462008-09-23 13:14:11 -0400280 add_new_free_space(block_group, root->fs_info, last,
281 block_group->key.objectid +
282 block_group->key.offset);
283
Yan Zhenga512bbf2008-12-08 16:46:26 -0500284 remove_sb_from_cache(root, block_group);
Chris Masone37c9e62007-05-09 20:13:14 -0400285 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400286 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400287err:
Chris Masone37c9e62007-05-09 20:13:14 -0400288 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400289 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400290}
291
Josef Bacik0f9dd462008-09-23 13:14:11 -0400292/*
293 * return the block group that starts at or after bytenr
294 */
Chris Masond3977122009-01-05 21:25:51 -0500295static struct btrfs_block_group_cache *
296btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400297{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400298 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400299
Josef Bacik0f9dd462008-09-23 13:14:11 -0400300 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400301
Josef Bacik0f9dd462008-09-23 13:14:11 -0400302 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400303}
304
Josef Bacik0f9dd462008-09-23 13:14:11 -0400305/*
306 * return the block group that contains teh given bytenr
307 */
Chris Masond3977122009-01-05 21:25:51 -0500308struct btrfs_block_group_cache *btrfs_lookup_block_group(
309 struct btrfs_fs_info *info,
310 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400311{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400312 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400313
Josef Bacik0f9dd462008-09-23 13:14:11 -0400314 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400315
Josef Bacik0f9dd462008-09-23 13:14:11 -0400316 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400317}
Chris Mason0b86a832008-03-24 15:01:56 -0400318
Yan Zhengd2fb3432008-12-11 16:30:39 -0500319static inline void put_block_group(struct btrfs_block_group_cache *cache)
320{
321 if (atomic_dec_and_test(&cache->count))
322 kfree(cache);
323}
324
Josef Bacik0f9dd462008-09-23 13:14:11 -0400325static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
326 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400327{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400328 struct list_head *head = &info->space_info;
329 struct list_head *cur;
330 struct btrfs_space_info *found;
331 list_for_each(cur, head) {
332 found = list_entry(cur, struct btrfs_space_info, list);
333 if (found->flags == flags)
334 return found;
335 }
336 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400337}
338
Josef Bacik80eb2342008-10-29 14:49:05 -0400339static u64 div_factor(u64 num, int factor)
340{
341 if (factor == 10)
342 return num;
343 num *= factor;
344 do_div(num, 10);
345 return num;
346}
347
Yan Zhengd2fb3432008-12-11 16:30:39 -0500348u64 btrfs_find_block_group(struct btrfs_root *root,
349 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400350{
Chris Mason96b51792007-10-15 16:15:19 -0400351 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400352 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500353 u64 last = max(search_hint, search_start);
354 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400355 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500356 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400357 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400358again:
Zheng Yane8569812008-09-26 10:05:48 -0400359 while (1) {
360 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400361 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400362 break;
Chris Mason96b51792007-10-15 16:15:19 -0400363
Chris Masonc286ac42008-07-22 23:06:41 -0400364 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400365 last = cache->key.objectid + cache->key.offset;
366 used = btrfs_block_group_used(&cache->item);
367
Yan Zhengd2fb3432008-12-11 16:30:39 -0500368 if ((full_search || !cache->ro) &&
369 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400370 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500371 div_factor(cache->key.offset, factor)) {
372 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400373 spin_unlock(&cache->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -0500374 put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400375 goto found;
376 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400377 }
Chris Masonc286ac42008-07-22 23:06:41 -0400378 spin_unlock(&cache->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -0500379 put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400380 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400381 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400382 if (!wrapped) {
383 last = search_start;
384 wrapped = 1;
385 goto again;
386 }
387 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400388 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400389 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400390 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400391 goto again;
392 }
Chris Masonbe744172007-05-06 10:15:01 -0400393found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500394 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400395}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400396
Chris Masone02119d2008-09-05 16:13:11 -0400397/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400398int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400399{
400 int ret;
401 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400402 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400403
Zheng Yan31840ae2008-09-23 13:14:14 -0400404 path = btrfs_alloc_path();
405 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400406 key.objectid = start;
407 key.offset = len;
408 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
409 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
410 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400411 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500412 return ret;
413}
414
Chris Masond8d5f3e2007-12-11 12:42:00 -0500415/*
416 * Back reference rules. Back refs have three main goals:
417 *
418 * 1) differentiate between all holders of references to an extent so that
419 * when a reference is dropped we can make sure it was a valid reference
420 * before freeing the extent.
421 *
422 * 2) Provide enough information to quickly find the holders of an extent
423 * if we notice a given block is corrupted or bad.
424 *
425 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
426 * maintenance. This is actually the same as #2, but with a slightly
427 * different use case.
428 *
429 * File extents can be referenced by:
430 *
431 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400432 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500433 * - different offsets inside a file (bookend extents in file.c)
434 *
435 * The extent ref structure has fields for:
436 *
437 * - Objectid of the subvolume root
438 * - Generation number of the tree holding the reference
439 * - objectid of the file holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400440 * - number of references holding by parent node (alway 1 for tree blocks)
441 *
442 * Btree leaf may hold multiple references to a file extent. In most cases,
443 * these references are from same file and the corresponding offsets inside
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400444 * the file are close together.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500445 *
446 * When a file extent is allocated the fields are filled in:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400447 * (root_key.objectid, trans->transid, inode objectid, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500448 *
449 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400450 * in the leaf. It looks similar to the create case, but trans->transid will
451 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500452 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400453 * (root_key.objectid, trans->transid, inode objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400454 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500455 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400456 * When a file extent is removed either during snapshot deletion or
457 * file truncation, we find the corresponding back reference and check
458 * the following fields:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500459 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400460 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
461 * inode objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500462 *
463 * Btree extents can be referenced by:
464 *
465 * - Different subvolumes
466 * - Different generations of the same subvolume
467 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500468 * When a tree block is created, back references are inserted:
469 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400470 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500471 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400472 * When a tree block is cow'd, new back references are added for all the
473 * blocks it points to. If the tree block isn't in reference counted root,
474 * the old back references are removed. These new back references are of
475 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500476 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400477 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500478 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400479 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500480 *
481 * if backref was for a tree root:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400482 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500483 * else
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400484 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500485 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400486 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500487 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400488 * The key objectid corresponds to the first byte in the extent, the key
489 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
490 * byte of parent extent. If a extent is tree root, the key offset is set
491 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500492 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400493
Chris Masond3977122009-01-05 21:25:51 -0500494static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400495 struct btrfs_root *root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400496 struct btrfs_path *path,
497 u64 bytenr, u64 parent,
498 u64 ref_root, u64 ref_generation,
499 u64 owner_objectid, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500500{
Chris Mason74493f72007-12-11 09:25:06 -0500501 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400502 struct btrfs_extent_ref *ref;
503 struct extent_buffer *leaf;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400504 u64 ref_objectid;
Chris Mason74493f72007-12-11 09:25:06 -0500505 int ret;
506
Chris Mason74493f72007-12-11 09:25:06 -0500507 key.objectid = bytenr;
508 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400509 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500510
Zheng Yan31840ae2008-09-23 13:14:14 -0400511 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
512 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500513 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400514 if (ret > 0) {
515 ret = -ENOENT;
516 goto out;
517 }
518
519 leaf = path->nodes[0];
520 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400521 ref_objectid = btrfs_ref_objectid(leaf, ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400522 if (btrfs_ref_root(leaf, ref) != ref_root ||
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400523 btrfs_ref_generation(leaf, ref) != ref_generation ||
524 (ref_objectid != owner_objectid &&
525 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400526 ret = -EIO;
527 WARN_ON(1);
528 goto out;
529 }
530 ret = 0;
531out:
532 return ret;
533}
534
Josef Bacikf3465ca2008-11-12 14:19:50 -0500535/*
536 * updates all the backrefs that are pending on update_list for the
537 * extent_root
538 */
Chris Masond3977122009-01-05 21:25:51 -0500539static noinline int update_backrefs(struct btrfs_trans_handle *trans,
Josef Bacikf3465ca2008-11-12 14:19:50 -0500540 struct btrfs_root *extent_root,
541 struct btrfs_path *path,
542 struct list_head *update_list)
543{
544 struct btrfs_key key;
545 struct btrfs_extent_ref *ref;
546 struct btrfs_fs_info *info = extent_root->fs_info;
547 struct pending_extent_op *op;
548 struct extent_buffer *leaf;
549 int ret = 0;
550 struct list_head *cur = update_list->next;
551 u64 ref_objectid;
552 u64 ref_root = extent_root->root_key.objectid;
553
554 op = list_entry(cur, struct pending_extent_op, list);
555
556search:
557 key.objectid = op->bytenr;
558 key.type = BTRFS_EXTENT_REF_KEY;
559 key.offset = op->orig_parent;
560
561 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
562 BUG_ON(ret);
563
564 leaf = path->nodes[0];
565
566loop:
567 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
568
569 ref_objectid = btrfs_ref_objectid(leaf, ref);
570
571 if (btrfs_ref_root(leaf, ref) != ref_root ||
572 btrfs_ref_generation(leaf, ref) != op->orig_generation ||
573 (ref_objectid != op->level &&
574 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Chris Masond3977122009-01-05 21:25:51 -0500575 printk(KERN_ERR "btrfs couldn't find %llu, parent %llu, "
576 "root %llu, owner %u\n",
577 (unsigned long long)op->bytenr,
578 (unsigned long long)op->orig_parent,
579 (unsigned long long)ref_root, op->level);
Josef Bacikf3465ca2008-11-12 14:19:50 -0500580 btrfs_print_leaf(extent_root, leaf);
581 BUG();
582 }
583
584 key.objectid = op->bytenr;
585 key.offset = op->parent;
586 key.type = BTRFS_EXTENT_REF_KEY;
587 ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
588 BUG_ON(ret);
589 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
590 btrfs_set_ref_generation(leaf, ref, op->generation);
591
592 cur = cur->next;
593
594 list_del_init(&op->list);
595 unlock_extent(&info->extent_ins, op->bytenr,
596 op->bytenr + op->num_bytes - 1, GFP_NOFS);
597 kfree(op);
598
599 if (cur == update_list) {
600 btrfs_mark_buffer_dirty(path->nodes[0]);
601 btrfs_release_path(extent_root, path);
602 goto out;
603 }
604
605 op = list_entry(cur, struct pending_extent_op, list);
606
607 path->slots[0]++;
608 while (path->slots[0] < btrfs_header_nritems(leaf)) {
609 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
610 if (key.objectid == op->bytenr &&
611 key.type == BTRFS_EXTENT_REF_KEY)
612 goto loop;
613 path->slots[0]++;
614 }
615
616 btrfs_mark_buffer_dirty(path->nodes[0]);
617 btrfs_release_path(extent_root, path);
618 goto search;
619
620out:
621 return 0;
622}
623
Chris Masond3977122009-01-05 21:25:51 -0500624static noinline int insert_extents(struct btrfs_trans_handle *trans,
Josef Bacikf3465ca2008-11-12 14:19:50 -0500625 struct btrfs_root *extent_root,
626 struct btrfs_path *path,
627 struct list_head *insert_list, int nr)
628{
629 struct btrfs_key *keys;
630 u32 *data_size;
631 struct pending_extent_op *op;
632 struct extent_buffer *leaf;
633 struct list_head *cur = insert_list->next;
634 struct btrfs_fs_info *info = extent_root->fs_info;
635 u64 ref_root = extent_root->root_key.objectid;
636 int i = 0, last = 0, ret;
637 int total = nr * 2;
638
639 if (!nr)
640 return 0;
641
642 keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
643 if (!keys)
644 return -ENOMEM;
645
646 data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
647 if (!data_size) {
648 kfree(keys);
649 return -ENOMEM;
650 }
651
652 list_for_each_entry(op, insert_list, list) {
653 keys[i].objectid = op->bytenr;
654 keys[i].offset = op->num_bytes;
655 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
656 data_size[i] = sizeof(struct btrfs_extent_item);
657 i++;
658
659 keys[i].objectid = op->bytenr;
660 keys[i].offset = op->parent;
661 keys[i].type = BTRFS_EXTENT_REF_KEY;
662 data_size[i] = sizeof(struct btrfs_extent_ref);
663 i++;
664 }
665
666 op = list_entry(cur, struct pending_extent_op, list);
667 i = 0;
668 while (i < total) {
669 int c;
670 ret = btrfs_insert_some_items(trans, extent_root, path,
671 keys+i, data_size+i, total-i);
672 BUG_ON(ret < 0);
673
674 if (last && ret > 1)
675 BUG();
676
677 leaf = path->nodes[0];
678 for (c = 0; c < ret; c++) {
679 int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
680
681 /*
682 * if the first item we inserted was a backref, then
683 * the EXTENT_ITEM will be the odd c's, else it will
684 * be the even c's
685 */
686 if ((ref_first && (c % 2)) ||
687 (!ref_first && !(c % 2))) {
688 struct btrfs_extent_item *itm;
689
690 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
691 struct btrfs_extent_item);
692 btrfs_set_extent_refs(path->nodes[0], itm, 1);
693 op->del++;
694 } else {
695 struct btrfs_extent_ref *ref;
696
697 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
698 struct btrfs_extent_ref);
699 btrfs_set_ref_root(leaf, ref, ref_root);
700 btrfs_set_ref_generation(leaf, ref,
701 op->generation);
702 btrfs_set_ref_objectid(leaf, ref, op->level);
703 btrfs_set_ref_num_refs(leaf, ref, 1);
704 op->del++;
705 }
706
707 /*
708 * using del to see when its ok to free up the
709 * pending_extent_op. In the case where we insert the
710 * last item on the list in order to help do batching
711 * we need to not free the extent op until we actually
712 * insert the extent_item
713 */
714 if (op->del == 2) {
715 unlock_extent(&info->extent_ins, op->bytenr,
716 op->bytenr + op->num_bytes - 1,
717 GFP_NOFS);
718 cur = cur->next;
719 list_del_init(&op->list);
720 kfree(op);
721 if (cur != insert_list)
722 op = list_entry(cur,
723 struct pending_extent_op,
724 list);
725 }
726 }
727 btrfs_mark_buffer_dirty(leaf);
728 btrfs_release_path(extent_root, path);
729
730 /*
731 * Ok backref's and items usually go right next to eachother,
732 * but if we could only insert 1 item that means that we
733 * inserted on the end of a leaf, and we have no idea what may
734 * be on the next leaf so we just play it safe. In order to
735 * try and help this case we insert the last thing on our
736 * insert list so hopefully it will end up being the last
737 * thing on the leaf and everything else will be before it,
738 * which will let us insert a whole bunch of items at the same
739 * time.
740 */
741 if (ret == 1 && !last && (i + ret < total)) {
742 /*
743 * last: where we will pick up the next time around
744 * i: our current key to insert, will be total - 1
745 * cur: the current op we are screwing with
746 * op: duh
747 */
748 last = i + ret;
749 i = total - 1;
750 cur = insert_list->prev;
751 op = list_entry(cur, struct pending_extent_op, list);
752 } else if (last) {
753 /*
754 * ok we successfully inserted the last item on the
755 * list, lets reset everything
756 *
757 * i: our current key to insert, so where we left off
758 * last time
759 * last: done with this
760 * cur: the op we are messing with
761 * op: duh
762 * total: since we inserted the last key, we need to
763 * decrement total so we dont overflow
764 */
765 i = last;
766 last = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -0500767 total--;
Liu Huib4eec2c2008-11-18 11:30:10 -0500768 if (i < total) {
769 cur = insert_list->next;
770 op = list_entry(cur, struct pending_extent_op,
771 list);
772 }
Josef Bacikf3465ca2008-11-12 14:19:50 -0500773 } else {
774 i += ret;
775 }
776
777 cond_resched();
778 }
779 ret = 0;
780 kfree(keys);
781 kfree(data_size);
782 return ret;
783}
784
Chris Masond3977122009-01-05 21:25:51 -0500785static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400786 struct btrfs_root *root,
787 struct btrfs_path *path,
788 u64 bytenr, u64 parent,
789 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400790 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400791{
792 struct btrfs_key key;
793 struct extent_buffer *leaf;
794 struct btrfs_extent_ref *ref;
795 u32 num_refs;
796 int ret;
797
798 key.objectid = bytenr;
799 key.type = BTRFS_EXTENT_REF_KEY;
800 key.offset = parent;
801
802 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
803 if (ret == 0) {
804 leaf = path->nodes[0];
805 ref = btrfs_item_ptr(leaf, path->slots[0],
806 struct btrfs_extent_ref);
807 btrfs_set_ref_root(leaf, ref, ref_root);
808 btrfs_set_ref_generation(leaf, ref, ref_generation);
809 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400810 btrfs_set_ref_num_refs(leaf, ref, 1);
811 } else if (ret == -EEXIST) {
812 u64 existing_owner;
813 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
814 leaf = path->nodes[0];
815 ref = btrfs_item_ptr(leaf, path->slots[0],
816 struct btrfs_extent_ref);
817 if (btrfs_ref_root(leaf, ref) != ref_root ||
818 btrfs_ref_generation(leaf, ref) != ref_generation) {
819 ret = -EIO;
820 WARN_ON(1);
821 goto out;
822 }
823
824 num_refs = btrfs_ref_num_refs(leaf, ref);
825 BUG_ON(num_refs == 0);
826 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
827
828 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400829 if (existing_owner != owner_objectid &&
830 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400831 btrfs_set_ref_objectid(leaf, ref,
832 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400833 }
834 ret = 0;
835 } else {
836 goto out;
837 }
Chris Mason7bb86312007-12-11 09:25:06 -0500838 btrfs_mark_buffer_dirty(path->nodes[0]);
839out:
840 btrfs_release_path(root, path);
841 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500842}
843
Chris Masond3977122009-01-05 21:25:51 -0500844static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400845 struct btrfs_root *root,
846 struct btrfs_path *path)
847{
848 struct extent_buffer *leaf;
849 struct btrfs_extent_ref *ref;
850 u32 num_refs;
851 int ret = 0;
852
853 leaf = path->nodes[0];
854 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
855 num_refs = btrfs_ref_num_refs(leaf, ref);
856 BUG_ON(num_refs == 0);
857 num_refs -= 1;
858 if (num_refs == 0) {
859 ret = btrfs_del_item(trans, root, path);
860 } else {
861 btrfs_set_ref_num_refs(leaf, ref, num_refs);
862 btrfs_mark_buffer_dirty(leaf);
863 }
864 btrfs_release_path(root, path);
865 return ret;
866}
867
Chris Mason4b4e25f2008-11-20 10:22:27 -0500868#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -0500869static void btrfs_issue_discard(struct block_device *bdev,
870 u64 start, u64 len)
871{
Chris Masond3977122009-01-05 21:25:51 -0500872#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
Chris Mason15916de2008-11-19 21:17:22 -0500873 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
874#else
875 blkdev_issue_discard(bdev, start >> 9, len >> 9);
876#endif
877}
Chris Mason4b4e25f2008-11-20 10:22:27 -0500878#endif
Chris Mason15916de2008-11-19 21:17:22 -0500879
Liu Hui1f3c79a2009-01-05 15:57:51 -0500880static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
881 u64 num_bytes)
882{
883#ifdef BIO_RW_DISCARD
884 int ret;
885 u64 map_length = num_bytes;
886 struct btrfs_multi_bio *multi = NULL;
887
888 /* Tell the block device(s) that the sectors can be discarded */
889 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
890 bytenr, &map_length, &multi, 0);
891 if (!ret) {
892 struct btrfs_bio_stripe *stripe = multi->stripes;
893 int i;
894
895 if (map_length > num_bytes)
896 map_length = num_bytes;
897
898 for (i = 0; i < multi->num_stripes; i++, stripe++) {
899 btrfs_issue_discard(stripe->dev->bdev,
900 stripe->physical,
901 map_length);
902 }
903 kfree(multi);
904 }
905
906 return ret;
907#else
908 return 0;
909#endif
910}
911
Chris Masond3977122009-01-05 21:25:51 -0500912static noinline int free_extents(struct btrfs_trans_handle *trans,
Josef Bacikf3465ca2008-11-12 14:19:50 -0500913 struct btrfs_root *extent_root,
914 struct list_head *del_list)
915{
916 struct btrfs_fs_info *info = extent_root->fs_info;
917 struct btrfs_path *path;
918 struct btrfs_key key, found_key;
919 struct extent_buffer *leaf;
920 struct list_head *cur;
921 struct pending_extent_op *op;
922 struct btrfs_extent_item *ei;
923 int ret, num_to_del, extent_slot = 0, found_extent = 0;
924 u32 refs;
925 u64 bytes_freed = 0;
926
927 path = btrfs_alloc_path();
928 if (!path)
929 return -ENOMEM;
930 path->reada = 1;
931
932search:
933 /* search for the backref for the current ref we want to delete */
934 cur = del_list->next;
935 op = list_entry(cur, struct pending_extent_op, list);
936 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
937 op->orig_parent,
938 extent_root->root_key.objectid,
939 op->orig_generation, op->level, 1);
940 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -0500941 printk(KERN_ERR "btrfs unable to find backref byte nr %llu "
942 "root %llu gen %llu owner %u\n",
943 (unsigned long long)op->bytenr,
944 (unsigned long long)extent_root->root_key.objectid,
945 (unsigned long long)op->orig_generation, op->level);
Josef Bacikf3465ca2008-11-12 14:19:50 -0500946 btrfs_print_leaf(extent_root, path->nodes[0]);
947 WARN_ON(1);
948 goto out;
949 }
950
951 extent_slot = path->slots[0];
952 num_to_del = 1;
953 found_extent = 0;
954
955 /*
956 * if we aren't the first item on the leaf we can move back one and see
957 * if our ref is right next to our extent item
958 */
959 if (likely(extent_slot)) {
960 extent_slot--;
961 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
962 extent_slot);
963 if (found_key.objectid == op->bytenr &&
964 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
965 found_key.offset == op->num_bytes) {
966 num_to_del++;
967 found_extent = 1;
968 }
969 }
970
971 /*
972 * if we didn't find the extent we need to delete the backref and then
973 * search for the extent item key so we can update its ref count
974 */
975 if (!found_extent) {
976 key.objectid = op->bytenr;
977 key.type = BTRFS_EXTENT_ITEM_KEY;
978 key.offset = op->num_bytes;
979
980 ret = remove_extent_backref(trans, extent_root, path);
981 BUG_ON(ret);
982 btrfs_release_path(extent_root, path);
983 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
984 BUG_ON(ret);
985 extent_slot = path->slots[0];
986 }
987
988 /* this is where we update the ref count for the extent */
989 leaf = path->nodes[0];
990 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
991 refs = btrfs_extent_refs(leaf, ei);
992 BUG_ON(refs == 0);
993 refs--;
994 btrfs_set_extent_refs(leaf, ei, refs);
995
996 btrfs_mark_buffer_dirty(leaf);
997
998 /*
999 * This extent needs deleting. The reason cur_slot is extent_slot +
1000 * num_to_del is because extent_slot points to the slot where the extent
1001 * is, and if the backref was not right next to the extent we will be
1002 * deleting at least 1 item, and will want to start searching at the
1003 * slot directly next to extent_slot. However if we did find the
1004 * backref next to the extent item them we will be deleting at least 2
1005 * items and will want to start searching directly after the ref slot
1006 */
1007 if (!refs) {
1008 struct list_head *pos, *n, *end;
1009 int cur_slot = extent_slot+num_to_del;
1010 u64 super_used;
1011 u64 root_used;
1012
1013 path->slots[0] = extent_slot;
1014 bytes_freed = op->num_bytes;
1015
Josef Bacike3e469f2008-11-17 21:11:49 -05001016 mutex_lock(&info->pinned_mutex);
1017 ret = pin_down_bytes(trans, extent_root, op->bytenr,
1018 op->num_bytes, op->level >=
1019 BTRFS_FIRST_FREE_OBJECTID);
1020 mutex_unlock(&info->pinned_mutex);
1021 BUG_ON(ret < 0);
1022 op->del = ret;
1023
Josef Bacikf3465ca2008-11-12 14:19:50 -05001024 /*
1025 * we need to see if we can delete multiple things at once, so
1026 * start looping through the list of extents we are wanting to
1027 * delete and see if their extent/backref's are right next to
1028 * eachother and the extents only have 1 ref
1029 */
1030 for (pos = cur->next; pos != del_list; pos = pos->next) {
1031 struct pending_extent_op *tmp;
1032
1033 tmp = list_entry(pos, struct pending_extent_op, list);
1034
1035 /* we only want to delete extent+ref at this stage */
1036 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1037 break;
1038
1039 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1040 if (found_key.objectid != tmp->bytenr ||
1041 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1042 found_key.offset != tmp->num_bytes)
1043 break;
1044
1045 /* check to make sure this extent only has one ref */
1046 ei = btrfs_item_ptr(leaf, cur_slot,
1047 struct btrfs_extent_item);
1048 if (btrfs_extent_refs(leaf, ei) != 1)
1049 break;
1050
1051 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1052 if (found_key.objectid != tmp->bytenr ||
1053 found_key.type != BTRFS_EXTENT_REF_KEY ||
1054 found_key.offset != tmp->orig_parent)
1055 break;
1056
1057 /*
1058 * the ref is right next to the extent, we can set the
1059 * ref count to 0 since we will delete them both now
1060 */
1061 btrfs_set_extent_refs(leaf, ei, 0);
1062
1063 /* pin down the bytes for this extent */
1064 mutex_lock(&info->pinned_mutex);
1065 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1066 tmp->num_bytes, tmp->level >=
1067 BTRFS_FIRST_FREE_OBJECTID);
1068 mutex_unlock(&info->pinned_mutex);
1069 BUG_ON(ret < 0);
1070
1071 /*
1072 * use the del field to tell if we need to go ahead and
1073 * free up the extent when we delete the item or not.
1074 */
1075 tmp->del = ret;
1076 bytes_freed += tmp->num_bytes;
1077
1078 num_to_del += 2;
1079 cur_slot += 2;
1080 }
1081 end = pos;
1082
1083 /* update the free space counters */
Chris Mason75eff682008-12-15 15:54:40 -05001084 spin_lock(&info->delalloc_lock);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001085 super_used = btrfs_super_bytes_used(&info->super_copy);
1086 btrfs_set_super_bytes_used(&info->super_copy,
1087 super_used - bytes_freed);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001088
1089 root_used = btrfs_root_used(&extent_root->root_item);
1090 btrfs_set_root_used(&extent_root->root_item,
1091 root_used - bytes_freed);
Yan Zheng34bf63c2008-12-19 10:58:46 -05001092 spin_unlock(&info->delalloc_lock);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001093
1094 /* delete the items */
1095 ret = btrfs_del_items(trans, extent_root, path,
1096 path->slots[0], num_to_del);
1097 BUG_ON(ret);
1098
1099 /*
1100 * loop through the extents we deleted and do the cleanup work
1101 * on them
1102 */
1103 for (pos = cur, n = pos->next; pos != end;
1104 pos = n, n = pos->next) {
1105 struct pending_extent_op *tmp;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001106 tmp = list_entry(pos, struct pending_extent_op, list);
1107
1108 /*
1109 * remember tmp->del tells us wether or not we pinned
1110 * down the extent
1111 */
1112 ret = update_block_group(trans, extent_root,
1113 tmp->bytenr, tmp->num_bytes, 0,
1114 tmp->del);
1115 BUG_ON(ret);
1116
Josef Bacikf3465ca2008-11-12 14:19:50 -05001117 list_del_init(&tmp->list);
1118 unlock_extent(&info->extent_ins, tmp->bytenr,
1119 tmp->bytenr + tmp->num_bytes - 1,
1120 GFP_NOFS);
1121 kfree(tmp);
1122 }
1123 } else if (refs && found_extent) {
1124 /*
1125 * the ref and extent were right next to eachother, but the
1126 * extent still has a ref, so just free the backref and keep
1127 * going
1128 */
1129 ret = remove_extent_backref(trans, extent_root, path);
1130 BUG_ON(ret);
1131
1132 list_del_init(&op->list);
1133 unlock_extent(&info->extent_ins, op->bytenr,
1134 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1135 kfree(op);
1136 } else {
1137 /*
1138 * the extent has multiple refs and the backref we were looking
1139 * for was not right next to it, so just unlock and go next,
1140 * we're good to go
1141 */
1142 list_del_init(&op->list);
1143 unlock_extent(&info->extent_ins, op->bytenr,
1144 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1145 kfree(op);
1146 }
1147
1148 btrfs_release_path(extent_root, path);
1149 if (!list_empty(del_list))
1150 goto search;
1151
1152out:
1153 btrfs_free_path(path);
1154 return ret;
1155}
1156
Zheng Yan31840ae2008-09-23 13:14:14 -04001157static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1158 struct btrfs_root *root, u64 bytenr,
1159 u64 orig_parent, u64 parent,
1160 u64 orig_root, u64 ref_root,
1161 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001162 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001163{
1164 int ret;
1165 struct btrfs_root *extent_root = root->fs_info->extent_root;
1166 struct btrfs_path *path;
1167
1168 if (root == root->fs_info->extent_root) {
1169 struct pending_extent_op *extent_op;
1170 u64 num_bytes;
1171
1172 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1173 num_bytes = btrfs_level_size(root, (int)owner_objectid);
Josef Bacik25179202008-10-29 14:49:05 -04001174 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001175 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
Josef Bacik25179202008-10-29 14:49:05 -04001176 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001177 u64 priv;
1178 ret = get_state_private(&root->fs_info->extent_ins,
1179 bytenr, &priv);
1180 BUG_ON(ret);
1181 extent_op = (struct pending_extent_op *)
1182 (unsigned long)priv;
1183 BUG_ON(extent_op->parent != orig_parent);
1184 BUG_ON(extent_op->generation != orig_generation);
Josef Bacik25179202008-10-29 14:49:05 -04001185
Zheng Yan31840ae2008-09-23 13:14:14 -04001186 extent_op->parent = parent;
1187 extent_op->generation = ref_generation;
1188 } else {
1189 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1190 BUG_ON(!extent_op);
1191
1192 extent_op->type = PENDING_BACKREF_UPDATE;
1193 extent_op->bytenr = bytenr;
1194 extent_op->num_bytes = num_bytes;
1195 extent_op->parent = parent;
1196 extent_op->orig_parent = orig_parent;
1197 extent_op->generation = ref_generation;
1198 extent_op->orig_generation = orig_generation;
1199 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001200 INIT_LIST_HEAD(&extent_op->list);
1201 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001202
1203 set_extent_bits(&root->fs_info->extent_ins,
1204 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04001205 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04001206 set_state_private(&root->fs_info->extent_ins,
1207 bytenr, (unsigned long)extent_op);
1208 }
Josef Bacik25179202008-10-29 14:49:05 -04001209 mutex_unlock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001210 return 0;
1211 }
1212
1213 path = btrfs_alloc_path();
1214 if (!path)
1215 return -ENOMEM;
1216 ret = lookup_extent_backref(trans, extent_root, path,
1217 bytenr, orig_parent, orig_root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001218 orig_generation, owner_objectid, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001219 if (ret)
1220 goto out;
1221 ret = remove_extent_backref(trans, extent_root, path);
1222 if (ret)
1223 goto out;
1224 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1225 parent, ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001226 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001227 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001228 finish_current_insert(trans, extent_root, 0);
1229 del_pending_extents(trans, extent_root, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001230out:
1231 btrfs_free_path(path);
1232 return ret;
1233}
1234
1235int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1236 struct btrfs_root *root, u64 bytenr,
1237 u64 orig_parent, u64 parent,
1238 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001239 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001240{
1241 int ret;
1242 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1243 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1244 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001245 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1246 parent, ref_root, ref_root,
1247 ref_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001248 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001249 return ret;
1250}
1251
Chris Mason925baed2008-06-25 16:01:30 -04001252static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001253 struct btrfs_root *root, u64 bytenr,
1254 u64 orig_parent, u64 parent,
1255 u64 orig_root, u64 ref_root,
1256 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001257 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -05001258{
Chris Mason5caf2a02007-04-02 11:20:42 -04001259 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -05001260 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001261 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001262 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001263 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001264 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001265
Chris Mason5caf2a02007-04-02 11:20:42 -04001266 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001267 if (!path)
1268 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001269
Chris Mason3c12ac72008-04-21 12:01:38 -04001270 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001271 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001272 key.type = BTRFS_EXTENT_ITEM_KEY;
1273 key.offset = (u64)-1;
1274
Chris Mason5caf2a02007-04-02 11:20:42 -04001275 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001276 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001277 if (ret < 0)
1278 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001279 BUG_ON(ret == 0 || path->slots[0] == 0);
1280
1281 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -04001282 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -04001283
1284 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Mason771ed682008-11-06 22:02:51 -05001285 if (key.objectid != bytenr) {
1286 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05001287 printk(KERN_ERR "btrfs wanted %llu found %llu\n",
1288 (unsigned long long)bytenr,
1289 (unsigned long long)key.objectid);
Chris Mason771ed682008-11-06 22:02:51 -05001290 BUG();
1291 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001292 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1293
Chris Mason5caf2a02007-04-02 11:20:42 -04001294 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001295 refs = btrfs_extent_refs(l, item);
1296 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001297 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -05001298
Chris Mason5caf2a02007-04-02 11:20:42 -04001299 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001300
Chris Mason3c12ac72008-04-21 12:01:38 -04001301 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001302 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1303 path, bytenr, parent,
1304 ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001305 owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05001306 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001307 finish_current_insert(trans, root->fs_info->extent_root, 0);
1308 del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Mason74493f72007-12-11 09:25:06 -05001309
1310 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -05001311 return 0;
1312}
1313
Chris Mason925baed2008-06-25 16:01:30 -04001314int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001315 struct btrfs_root *root,
1316 u64 bytenr, u64 num_bytes, u64 parent,
1317 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001318 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -04001319{
1320 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001321 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1322 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1323 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001324 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1325 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001326 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -04001327 return ret;
1328}
1329
Chris Masone9d0b132007-08-10 14:06:19 -04001330int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1331 struct btrfs_root *root)
1332{
Chris Mason87ef2bb2008-10-30 11:23:27 -04001333 finish_current_insert(trans, root->fs_info->extent_root, 1);
1334 del_pending_extents(trans, root->fs_info->extent_root, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001335 return 0;
1336}
1337
Zheng Yan31840ae2008-09-23 13:14:14 -04001338int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1339 struct btrfs_root *root, u64 bytenr,
1340 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -05001341{
Chris Mason5caf2a02007-04-02 11:20:42 -04001342 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -05001343 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001344 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001345 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001346 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -04001347
Chris Masondb945352007-10-15 16:15:53 -04001348 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -04001349 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -04001350 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001351 key.objectid = bytenr;
1352 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -04001353 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -04001354 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001355 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001356 if (ret < 0)
1357 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -04001358 if (ret != 0) {
1359 btrfs_print_leaf(root, path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05001360 printk(KERN_INFO "btrfs failed to find block number %llu\n",
1361 (unsigned long long)bytenr);
Chris Masona28ec192007-03-06 20:08:01 -05001362 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04001363 }
1364 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001365 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001366 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001367out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001368 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05001369 return 0;
1370}
1371
Yan Zheng80ff3852008-10-30 14:20:02 -04001372int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
Yan Zheng17d217f2008-12-12 10:03:38 -05001373 struct btrfs_root *root, u64 objectid, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05001374{
1375 struct btrfs_root *extent_root = root->fs_info->extent_root;
1376 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -04001377 struct extent_buffer *leaf;
1378 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -05001379 struct btrfs_key key;
1380 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 u64 ref_root;
1382 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -04001383 u32 nritems;
1384 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001385
Chris Masonbe20aa92007-12-17 20:14:01 -05001386 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001387 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04001388 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05001389
Yan Zhengf321e492008-07-30 09:26:11 -04001390 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -05001391 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1392 if (ret < 0)
1393 goto out;
1394 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001395
1396 ret = -ENOENT;
1397 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04001398 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001399
Zheng Yan31840ae2008-09-23 13:14:14 -04001400 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04001401 leaf = path->nodes[0];
1402 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001403
1404 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001405 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05001406 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001407
Yan Zheng80ff3852008-10-30 14:20:02 -04001408 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05001409 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -04001410 leaf = path->nodes[0];
1411 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -05001412 if (path->slots[0] >= nritems) {
1413 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -04001414 if (ret < 0)
1415 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001416 if (ret == 0)
1417 continue;
1418 break;
1419 }
Yan Zhengf321e492008-07-30 09:26:11 -04001420 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001421 if (found_key.objectid != bytenr)
1422 break;
Chris Masonbd098352008-01-03 13:23:19 -05001423
Chris Masonbe20aa92007-12-17 20:14:01 -05001424 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1425 path->slots[0]++;
1426 continue;
1427 }
1428
Yan Zhengf321e492008-07-30 09:26:11 -04001429 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001430 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -04001431 ref_root = btrfs_ref_root(leaf, ref_item);
Yan Zheng17d217f2008-12-12 10:03:38 -05001432 if ((ref_root != root->root_key.objectid &&
1433 ref_root != BTRFS_TREE_LOG_OBJECTID) ||
1434 objectid != btrfs_ref_objectid(leaf, ref_item)) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001435 ret = 1;
1436 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -04001437 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001438 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1439 ret = 1;
1440 goto out;
1441 }
Yan Zhengf321e492008-07-30 09:26:11 -04001442
Chris Masonbe20aa92007-12-17 20:14:01 -05001443 path->slots[0]++;
1444 }
Yan Zhengf321e492008-07-30 09:26:11 -04001445 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001446out:
Yan Zhengf321e492008-07-30 09:26:11 -04001447 btrfs_free_path(path);
1448 return ret;
1449}
1450
Zheng Yan31840ae2008-09-23 13:14:14 -04001451int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1452 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001453{
Chris Mason5f39d392007-10-15 16:14:19 -04001454 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001455 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001456 u64 root_gen;
1457 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001458 int i;
Chris Masondb945352007-10-15 16:15:53 -04001459 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001460 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001461 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001462
Chris Mason3768f362007-03-13 16:47:54 -04001463 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001464 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001465
Zheng Yane4657682008-09-26 10:04:53 -04001466 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1467 shared = 0;
1468 root_gen = root->root_key.offset;
1469 } else {
1470 shared = 1;
1471 root_gen = trans->transid - 1;
1472 }
1473
Chris Masondb945352007-10-15 16:15:53 -04001474 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001475 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001476
Zheng Yan31840ae2008-09-23 13:14:14 -04001477 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001478 struct btrfs_leaf_ref *ref;
1479 struct btrfs_extent_info *info;
1480
Zheng Yan31840ae2008-09-23 13:14:14 -04001481 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001482 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001483 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001484 goto out;
1485 }
1486
Zheng Yane4657682008-09-26 10:04:53 -04001487 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001488 ref->bytenr = buf->start;
1489 ref->owner = btrfs_header_owner(buf);
1490 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001491 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001492 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001493
Zheng Yan31840ae2008-09-23 13:14:14 -04001494 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001495 u64 disk_bytenr;
1496 btrfs_item_key_to_cpu(buf, &key, i);
1497 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1498 continue;
1499 fi = btrfs_item_ptr(buf, i,
1500 struct btrfs_file_extent_item);
1501 if (btrfs_file_extent_type(buf, fi) ==
1502 BTRFS_FILE_EXTENT_INLINE)
1503 continue;
1504 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1505 if (disk_bytenr == 0)
1506 continue;
1507
1508 info->bytenr = disk_bytenr;
1509 info->num_bytes =
1510 btrfs_file_extent_disk_num_bytes(buf, fi);
1511 info->objectid = key.objectid;
1512 info->offset = key.offset;
1513 info++;
1514 }
1515
Zheng Yane4657682008-09-26 10:04:53 -04001516 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001517 if (ret == -EEXIST && shared) {
1518 struct btrfs_leaf_ref *old;
1519 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1520 BUG_ON(!old);
1521 btrfs_remove_leaf_ref(root, old);
1522 btrfs_free_leaf_ref(root, old);
1523 ret = btrfs_add_leaf_ref(root, ref, shared);
1524 }
Yan Zheng31153d82008-07-28 15:32:19 -04001525 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001526 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001527 }
1528out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001529 return ret;
1530}
1531
1532int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1533 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1534 u32 *nr_extents)
1535{
1536 u64 bytenr;
1537 u64 ref_root;
1538 u64 orig_root;
1539 u64 ref_generation;
1540 u64 orig_generation;
1541 u32 nritems;
1542 u32 nr_file_extents = 0;
1543 struct btrfs_key key;
1544 struct btrfs_file_extent_item *fi;
1545 int i;
1546 int level;
1547 int ret = 0;
1548 int faili = 0;
1549 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001550 u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001551
1552 ref_root = btrfs_header_owner(buf);
1553 ref_generation = btrfs_header_generation(buf);
1554 orig_root = btrfs_header_owner(orig_buf);
1555 orig_generation = btrfs_header_generation(orig_buf);
1556
1557 nritems = btrfs_header_nritems(buf);
1558 level = btrfs_header_level(buf);
1559
1560 if (root->ref_cows) {
1561 process_func = __btrfs_inc_extent_ref;
1562 } else {
1563 if (level == 0 &&
1564 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1565 goto out;
1566 if (level != 0 &&
1567 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1568 goto out;
1569 process_func = __btrfs_update_extent_ref;
1570 }
1571
1572 for (i = 0; i < nritems; i++) {
1573 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001574 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001575 btrfs_item_key_to_cpu(buf, &key, i);
1576 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001577 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001578 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001579 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001580 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001581 BTRFS_FILE_EXTENT_INLINE)
1582 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001583 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1584 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001585 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001586
1587 nr_file_extents++;
1588
Zheng Yan31840ae2008-09-23 13:14:14 -04001589 ret = process_func(trans, root, bytenr,
1590 orig_buf->start, buf->start,
1591 orig_root, ref_root,
1592 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001593 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001594
1595 if (ret) {
1596 faili = i;
1597 WARN_ON(1);
1598 goto fail;
1599 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001600 } else {
Chris Masondb945352007-10-15 16:15:53 -04001601 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001602 ret = process_func(trans, root, bytenr,
1603 orig_buf->start, buf->start,
1604 orig_root, ref_root,
1605 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001606 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001607 if (ret) {
1608 faili = i;
1609 WARN_ON(1);
1610 goto fail;
1611 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001612 }
1613 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001614out:
1615 if (nr_extents) {
1616 if (level == 0)
1617 *nr_extents = nr_file_extents;
1618 else
1619 *nr_extents = nritems;
1620 }
1621 return 0;
1622fail:
1623 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001624 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001625}
1626
Zheng Yan31840ae2008-09-23 13:14:14 -04001627int btrfs_update_ref(struct btrfs_trans_handle *trans,
1628 struct btrfs_root *root, struct extent_buffer *orig_buf,
1629 struct extent_buffer *buf, int start_slot, int nr)
1630
1631{
1632 u64 bytenr;
1633 u64 ref_root;
1634 u64 orig_root;
1635 u64 ref_generation;
1636 u64 orig_generation;
1637 struct btrfs_key key;
1638 struct btrfs_file_extent_item *fi;
1639 int i;
1640 int ret;
1641 int slot;
1642 int level;
1643
1644 BUG_ON(start_slot < 0);
1645 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1646
1647 ref_root = btrfs_header_owner(buf);
1648 ref_generation = btrfs_header_generation(buf);
1649 orig_root = btrfs_header_owner(orig_buf);
1650 orig_generation = btrfs_header_generation(orig_buf);
1651 level = btrfs_header_level(buf);
1652
1653 if (!root->ref_cows) {
1654 if (level == 0 &&
1655 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1656 return 0;
1657 if (level != 0 &&
1658 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1659 return 0;
1660 }
1661
1662 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1663 cond_resched();
1664 if (level == 0) {
1665 btrfs_item_key_to_cpu(buf, &key, slot);
1666 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1667 continue;
1668 fi = btrfs_item_ptr(buf, slot,
1669 struct btrfs_file_extent_item);
1670 if (btrfs_file_extent_type(buf, fi) ==
1671 BTRFS_FILE_EXTENT_INLINE)
1672 continue;
1673 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1674 if (bytenr == 0)
1675 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001676 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1677 orig_buf->start, buf->start,
1678 orig_root, ref_root,
1679 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001680 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001681 if (ret)
1682 goto fail;
1683 } else {
1684 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001685 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1686 orig_buf->start, buf->start,
1687 orig_root, ref_root,
1688 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001689 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001690 if (ret)
1691 goto fail;
1692 }
1693 }
1694 return 0;
1695fail:
1696 WARN_ON(1);
1697 return -1;
1698}
1699
Chris Mason9078a3e2007-04-26 16:46:15 -04001700static int write_one_cache_group(struct btrfs_trans_handle *trans,
1701 struct btrfs_root *root,
1702 struct btrfs_path *path,
1703 struct btrfs_block_group_cache *cache)
1704{
1705 int ret;
1706 int pending_ret;
1707 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001708 unsigned long bi;
1709 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001710
Chris Mason9078a3e2007-04-26 16:46:15 -04001711 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001712 if (ret < 0)
1713 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001714 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001715
1716 leaf = path->nodes[0];
1717 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1718 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1719 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001720 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001721fail:
Chris Mason87ef2bb2008-10-30 11:23:27 -04001722 finish_current_insert(trans, extent_root, 0);
1723 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001724 if (ret)
1725 return ret;
1726 if (pending_ret)
1727 return pending_ret;
1728 return 0;
1729
1730}
1731
Chris Mason96b51792007-10-15 16:15:19 -04001732int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1733 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001734{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001735 struct btrfs_block_group_cache *cache, *entry;
1736 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001737 int err = 0;
1738 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001739 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001740 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001741
1742 path = btrfs_alloc_path();
1743 if (!path)
1744 return -ENOMEM;
1745
Chris Masond3977122009-01-05 21:25:51 -05001746 while (1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001747 cache = NULL;
1748 spin_lock(&root->fs_info->block_group_cache_lock);
1749 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1750 n; n = rb_next(n)) {
1751 entry = rb_entry(n, struct btrfs_block_group_cache,
1752 cache_node);
1753 if (entry->dirty) {
1754 cache = entry;
1755 break;
1756 }
1757 }
1758 spin_unlock(&root->fs_info->block_group_cache_lock);
1759
1760 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001761 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001762
Zheng Yane8569812008-09-26 10:05:48 -04001763 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001764 last += cache->key.offset;
1765
Chris Mason96b51792007-10-15 16:15:19 -04001766 err = write_one_cache_group(trans, root,
1767 path, cache);
1768 /*
1769 * if we fail to write the cache group, we want
1770 * to keep it marked dirty in hopes that a later
1771 * write will work
1772 */
1773 if (err) {
1774 werr = err;
1775 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001776 }
1777 }
1778 btrfs_free_path(path);
1779 return werr;
1780}
1781
Yan Zhengd2fb3432008-12-11 16:30:39 -05001782int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1783{
1784 struct btrfs_block_group_cache *block_group;
1785 int readonly = 0;
1786
1787 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1788 if (!block_group || block_group->ro)
1789 readonly = 1;
1790 if (block_group)
1791 put_block_group(block_group);
1792 return readonly;
1793}
1794
Chris Mason593060d2008-03-25 16:50:33 -04001795static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1796 u64 total_bytes, u64 bytes_used,
1797 struct btrfs_space_info **space_info)
1798{
1799 struct btrfs_space_info *found;
1800
1801 found = __find_space_info(info, flags);
1802 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001803 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001804 found->total_bytes += total_bytes;
1805 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001806 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001807 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001808 *space_info = found;
1809 return 0;
1810 }
Yan Zhengc146afa2008-11-12 14:34:12 -05001811 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04001812 if (!found)
1813 return -ENOMEM;
1814
1815 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001816 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001817 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001818 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001819 found->flags = flags;
1820 found->total_bytes = total_bytes;
1821 found->bytes_used = bytes_used;
1822 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001823 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05001824 found->bytes_readonly = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001825 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001826 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001827 *space_info = found;
1828 return 0;
1829}
1830
Chris Mason8790d502008-04-03 16:29:03 -04001831static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1832{
1833 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001834 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001835 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001836 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001837 if (extra_flags) {
1838 if (flags & BTRFS_BLOCK_GROUP_DATA)
1839 fs_info->avail_data_alloc_bits |= extra_flags;
1840 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1841 fs_info->avail_metadata_alloc_bits |= extra_flags;
1842 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1843 fs_info->avail_system_alloc_bits |= extra_flags;
1844 }
1845}
Chris Mason593060d2008-03-25 16:50:33 -04001846
Yan Zhengc146afa2008-11-12 14:34:12 -05001847static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1848{
1849 spin_lock(&cache->space_info->lock);
1850 spin_lock(&cache->lock);
1851 if (!cache->ro) {
1852 cache->space_info->bytes_readonly += cache->key.offset -
1853 btrfs_block_group_used(&cache->item);
1854 cache->ro = 1;
1855 }
1856 spin_unlock(&cache->lock);
1857 spin_unlock(&cache->space_info->lock);
1858}
1859
Yan Zheng2b820322008-11-17 21:11:30 -05001860u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001861{
Yan Zheng2b820322008-11-17 21:11:30 -05001862 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001863
1864 if (num_devices == 1)
1865 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1866 if (num_devices < 4)
1867 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1868
Chris Masonec44a352008-04-28 15:29:52 -04001869 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1870 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001871 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001872 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001873 }
Chris Masonec44a352008-04-28 15:29:52 -04001874
1875 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001876 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001877 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001878 }
Chris Masonec44a352008-04-28 15:29:52 -04001879
1880 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1881 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1882 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1883 (flags & BTRFS_BLOCK_GROUP_DUP)))
1884 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1885 return flags;
1886}
1887
Chris Mason6324fbf2008-03-24 15:01:59 -04001888static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1889 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001890 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001891{
1892 struct btrfs_space_info *space_info;
1893 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05001894 int ret = 0;
1895
1896 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001897
Yan Zheng2b820322008-11-17 21:11:30 -05001898 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001899
Chris Mason6324fbf2008-03-24 15:01:59 -04001900 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001901 if (!space_info) {
1902 ret = update_space_info(extent_root->fs_info, flags,
1903 0, 0, &space_info);
1904 BUG_ON(ret);
1905 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001906 BUG_ON(!space_info);
1907
Josef Bacik25179202008-10-29 14:49:05 -04001908 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001909 if (space_info->force_alloc) {
1910 force = 1;
1911 space_info->force_alloc = 0;
1912 }
Josef Bacik25179202008-10-29 14:49:05 -04001913 if (space_info->full) {
1914 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001915 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001916 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001917
Yan Zhengc146afa2008-11-12 14:34:12 -05001918 thresh = space_info->total_bytes - space_info->bytes_readonly;
1919 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001920 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001921 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001922 space_info->bytes_reserved + alloc_bytes) < thresh) {
1923 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001924 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001925 }
Josef Bacik25179202008-10-29 14:49:05 -04001926 spin_unlock(&space_info->lock);
1927
Yan Zheng2b820322008-11-17 21:11:30 -05001928 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Chris Masond3977122009-01-05 21:25:51 -05001929 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04001930 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001931out:
Yan Zhengc146afa2008-11-12 14:34:12 -05001932 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001933 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001934}
1935
Chris Mason9078a3e2007-04-26 16:46:15 -04001936static int update_block_group(struct btrfs_trans_handle *trans,
1937 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001938 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001939 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001940{
1941 struct btrfs_block_group_cache *cache;
1942 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001943 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001944 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001945 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001946
Chris Masond3977122009-01-05 21:25:51 -05001947 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04001948 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001949 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001950 return -1;
Chris Masondb945352007-10-15 16:15:53 -04001951 byte_in_group = bytenr - cache->key.objectid;
1952 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001953
Josef Bacik25179202008-10-29 14:49:05 -04001954 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001955 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001956 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001957 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001958 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001959 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001960 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001961 cache->space_info->bytes_used += num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05001962 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05001963 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001964 btrfs_set_block_group_used(&cache->item, old_val);
1965 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001966 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001967 } else {
Chris Masondb945352007-10-15 16:15:53 -04001968 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001969 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001970 if (cache->ro)
1971 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001972 btrfs_set_block_group_used(&cache->item, old_val);
1973 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001974 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001975 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001976 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001977
1978 ret = btrfs_discard_extent(root, bytenr,
1979 num_bytes);
1980 WARN_ON(ret);
1981
Josef Bacik0f9dd462008-09-23 13:14:11 -04001982 ret = btrfs_add_free_space(cache, bytenr,
1983 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05001984 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04001985 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001986 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05001987 put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04001988 total -= num_bytes;
1989 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001990 }
1991 return 0;
1992}
Chris Mason6324fbf2008-03-24 15:01:59 -04001993
Chris Masona061fc82008-05-07 11:43:44 -04001994static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1995{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001996 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001997 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001998
1999 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2000 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002001 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002002
Yan Zhengd2fb3432008-12-11 16:30:39 -05002003 bytenr = cache->key.objectid;
2004 put_block_group(cache);
2005
2006 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002007}
2008
Chris Masone02119d2008-09-05 16:13:11 -04002009int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05002010 u64 bytenr, u64 num, int pin)
2011{
2012 u64 len;
2013 struct btrfs_block_group_cache *cache;
2014 struct btrfs_fs_info *fs_info = root->fs_info;
2015
Josef Bacik25179202008-10-29 14:49:05 -04002016 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05002017 if (pin) {
2018 set_extent_dirty(&fs_info->pinned_extents,
2019 bytenr, bytenr + num - 1, GFP_NOFS);
2020 } else {
2021 clear_extent_dirty(&fs_info->pinned_extents,
2022 bytenr, bytenr + num - 1, GFP_NOFS);
2023 }
2024 while (num > 0) {
2025 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002026 BUG_ON(!cache);
2027 len = min(num, cache->key.offset -
2028 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002029 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002030 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002031 spin_lock(&cache->lock);
2032 cache->pinned += len;
2033 cache->space_info->bytes_pinned += len;
2034 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002035 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002036 fs_info->total_pinned += len;
2037 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002038 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002039 spin_lock(&cache->lock);
2040 cache->pinned -= len;
2041 cache->space_info->bytes_pinned -= len;
2042 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002043 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002044 fs_info->total_pinned -= len;
Josef Bacik07103a32008-11-19 15:17:55 -05002045 if (cache->cached)
2046 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05002047 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002048 put_block_group(cache);
Yan324ae4d2007-11-16 14:57:08 -05002049 bytenr += len;
2050 num -= len;
2051 }
2052 return 0;
2053}
Chris Mason9078a3e2007-04-26 16:46:15 -04002054
Zheng Yane8569812008-09-26 10:05:48 -04002055static int update_reserved_extents(struct btrfs_root *root,
2056 u64 bytenr, u64 num, int reserve)
2057{
2058 u64 len;
2059 struct btrfs_block_group_cache *cache;
2060 struct btrfs_fs_info *fs_info = root->fs_info;
2061
Zheng Yane8569812008-09-26 10:05:48 -04002062 while (num > 0) {
2063 cache = btrfs_lookup_block_group(fs_info, bytenr);
2064 BUG_ON(!cache);
2065 len = min(num, cache->key.offset -
2066 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04002067
2068 spin_lock(&cache->space_info->lock);
2069 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002070 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04002071 cache->reserved += len;
2072 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04002073 } else {
Zheng Yane8569812008-09-26 10:05:48 -04002074 cache->reserved -= len;
2075 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04002076 }
Josef Bacik25179202008-10-29 14:49:05 -04002077 spin_unlock(&cache->lock);
2078 spin_unlock(&cache->space_info->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002079 put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04002080 bytenr += len;
2081 num -= len;
2082 }
2083 return 0;
2084}
2085
Chris Masond1310b22008-01-24 16:13:08 -05002086int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04002087{
Chris Masonccd467d2007-06-28 15:57:36 -04002088 u64 last = 0;
Chris Mason1a5bc1672007-10-15 16:15:26 -04002089 u64 start;
2090 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002091 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04002092 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04002093
Josef Bacik25179202008-10-29 14:49:05 -04002094 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002095 while (1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04002096 ret = find_first_extent_bit(pinned_extents, last,
2097 &start, &end, EXTENT_DIRTY);
2098 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04002099 break;
Chris Mason1a5bc1672007-10-15 16:15:26 -04002100 set_extent_dirty(copy, start, end, GFP_NOFS);
2101 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04002102 }
Josef Bacik25179202008-10-29 14:49:05 -04002103 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002104 return 0;
2105}
2106
2107int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2108 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05002109 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05002110{
Chris Mason1a5bc1672007-10-15 16:15:26 -04002111 u64 start;
2112 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002113 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002114
Josef Bacik25179202008-10-29 14:49:05 -04002115 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002116 while (1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04002117 ret = find_first_extent_bit(unpin, 0, &start, &end,
2118 EXTENT_DIRTY);
2119 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05002120 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002121
2122 ret = btrfs_discard_extent(root, start, end + 1 - start);
2123
Chris Masone02119d2008-09-05 16:13:11 -04002124 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc1672007-10-15 16:15:26 -04002125 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002126
Chris Masonc286ac42008-07-22 23:06:41 -04002127 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04002128 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002129 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04002130 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002131 }
Chris Masona28ec192007-03-06 20:08:01 -05002132 }
Josef Bacik25179202008-10-29 14:49:05 -04002133 mutex_unlock(&root->fs_info->pinned_mutex);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002134 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05002135}
2136
Chris Mason98ed5172008-01-03 10:01:48 -05002137static int finish_current_insert(struct btrfs_trans_handle *trans,
Chris Mason87ef2bb2008-10-30 11:23:27 -04002138 struct btrfs_root *extent_root, int all)
Chris Mason037e6392007-03-07 11:50:24 -05002139{
Chris Mason7bb86312007-12-11 09:25:06 -05002140 u64 start;
2141 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002142 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002143 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002144 u64 skipped = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002145 struct btrfs_fs_info *info = extent_root->fs_info;
2146 struct btrfs_path *path;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002147 struct pending_extent_op *extent_op, *tmp;
2148 struct list_head insert_list, update_list;
Chris Mason037e6392007-03-07 11:50:24 -05002149 int ret;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002150 int num_inserts = 0, max_inserts;
Chris Mason037e6392007-03-07 11:50:24 -05002151
Chris Mason7bb86312007-12-11 09:25:06 -05002152 path = btrfs_alloc_path();
Josef Bacikf3465ca2008-11-12 14:19:50 -05002153 INIT_LIST_HEAD(&insert_list);
2154 INIT_LIST_HEAD(&update_list);
Chris Mason037e6392007-03-07 11:50:24 -05002155
Josef Bacikf3465ca2008-11-12 14:19:50 -05002156 max_inserts = extent_root->leafsize /
2157 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2158 sizeof(struct btrfs_extent_ref) +
2159 sizeof(struct btrfs_extent_item));
2160again:
2161 mutex_lock(&info->extent_ins_mutex);
2162 while (1) {
Josef Bacik25179202008-10-29 14:49:05 -04002163 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2164 &end, EXTENT_WRITEBACK);
2165 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002166 if (skipped && all && !num_inserts) {
2167 skipped = 0;
Liu Huib4eec2c2008-11-18 11:30:10 -05002168 search = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002169 continue;
2170 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002171 mutex_unlock(&info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04002172 break;
Josef Bacik25179202008-10-29 14:49:05 -04002173 }
2174
2175 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2176 if (!ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002177 skipped = 1;
Chris Mason87ef2bb2008-10-30 11:23:27 -04002178 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002179 if (need_resched()) {
2180 mutex_unlock(&info->extent_ins_mutex);
2181 cond_resched();
2182 mutex_lock(&info->extent_ins_mutex);
2183 }
Josef Bacik25179202008-10-29 14:49:05 -04002184 continue;
2185 }
Chris Mason26b80032007-08-08 20:17:12 -04002186
Zheng Yan31840ae2008-09-23 13:14:14 -04002187 ret = get_state_private(&info->extent_ins, start, &priv);
2188 BUG_ON(ret);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002189 extent_op = (struct pending_extent_op *)(unsigned long) priv;
Josef Bacik25179202008-10-29 14:49:05 -04002190
Zheng Yan31840ae2008-09-23 13:14:14 -04002191 if (extent_op->type == PENDING_EXTENT_INSERT) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002192 num_inserts++;
2193 list_add_tail(&extent_op->list, &insert_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002194 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002195 if (num_inserts == max_inserts) {
2196 mutex_unlock(&info->extent_ins_mutex);
2197 break;
2198 }
2199 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2200 list_add_tail(&extent_op->list, &update_list);
2201 search = end + 1;
2202 } else {
2203 BUG();
2204 }
Chris Mason037e6392007-03-07 11:50:24 -05002205 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002206
2207 /*
Liu Huib4eec2c2008-11-18 11:30:10 -05002208 * process the update list, clear the writeback bit for it, and if
Josef Bacikf3465ca2008-11-12 14:19:50 -05002209 * somebody marked this thing for deletion then just unlock it and be
2210 * done, the free_extents will handle it
2211 */
2212 mutex_lock(&info->extent_ins_mutex);
2213 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2214 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2215 extent_op->bytenr + extent_op->num_bytes - 1,
2216 EXTENT_WRITEBACK, GFP_NOFS);
2217 if (extent_op->del) {
2218 list_del_init(&extent_op->list);
2219 unlock_extent(&info->extent_ins, extent_op->bytenr,
2220 extent_op->bytenr + extent_op->num_bytes
2221 - 1, GFP_NOFS);
2222 kfree(extent_op);
2223 }
2224 }
2225 mutex_unlock(&info->extent_ins_mutex);
2226
2227 /*
2228 * still have things left on the update list, go ahead an update
2229 * everything
2230 */
2231 if (!list_empty(&update_list)) {
2232 ret = update_backrefs(trans, extent_root, path, &update_list);
2233 BUG_ON(ret);
2234 }
2235
2236 /*
2237 * if no inserts need to be done, but we skipped some extents and we
2238 * need to make sure everything is cleaned then reset everything and
2239 * go back to the beginning
2240 */
2241 if (!num_inserts && all && skipped) {
2242 search = 0;
2243 skipped = 0;
2244 INIT_LIST_HEAD(&update_list);
2245 INIT_LIST_HEAD(&insert_list);
2246 goto again;
2247 } else if (!num_inserts) {
2248 goto out;
2249 }
2250
2251 /*
2252 * process the insert extents list. Again if we are deleting this
2253 * extent, then just unlock it, pin down the bytes if need be, and be
2254 * done with it. Saves us from having to actually insert the extent
2255 * into the tree and then subsequently come along and delete it
2256 */
2257 mutex_lock(&info->extent_ins_mutex);
2258 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2259 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2260 extent_op->bytenr + extent_op->num_bytes - 1,
2261 EXTENT_WRITEBACK, GFP_NOFS);
2262 if (extent_op->del) {
Yan Zheng34bf63c2008-12-19 10:58:46 -05002263 u64 used;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002264 list_del_init(&extent_op->list);
2265 unlock_extent(&info->extent_ins, extent_op->bytenr,
2266 extent_op->bytenr + extent_op->num_bytes
2267 - 1, GFP_NOFS);
2268
2269 mutex_lock(&extent_root->fs_info->pinned_mutex);
2270 ret = pin_down_bytes(trans, extent_root,
2271 extent_op->bytenr,
2272 extent_op->num_bytes, 0);
2273 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2274
Yan Zheng34bf63c2008-12-19 10:58:46 -05002275 spin_lock(&info->delalloc_lock);
2276 used = btrfs_super_bytes_used(&info->super_copy);
2277 btrfs_set_super_bytes_used(&info->super_copy,
2278 used - extent_op->num_bytes);
2279 used = btrfs_root_used(&extent_root->root_item);
2280 btrfs_set_root_used(&extent_root->root_item,
2281 used - extent_op->num_bytes);
2282 spin_unlock(&info->delalloc_lock);
2283
Josef Bacikf3465ca2008-11-12 14:19:50 -05002284 ret = update_block_group(trans, extent_root,
2285 extent_op->bytenr,
2286 extent_op->num_bytes,
2287 0, ret > 0);
2288 BUG_ON(ret);
2289 kfree(extent_op);
2290 num_inserts--;
2291 }
2292 }
2293 mutex_unlock(&info->extent_ins_mutex);
2294
2295 ret = insert_extents(trans, extent_root, path, &insert_list,
2296 num_inserts);
2297 BUG_ON(ret);
2298
2299 /*
2300 * if we broke out of the loop in order to insert stuff because we hit
2301 * the maximum number of inserts at a time we can handle, then loop
2302 * back and pick up where we left off
2303 */
2304 if (num_inserts == max_inserts) {
2305 INIT_LIST_HEAD(&insert_list);
2306 INIT_LIST_HEAD(&update_list);
2307 num_inserts = 0;
2308 goto again;
2309 }
2310
2311 /*
2312 * again, if we need to make absolutely sure there are no more pending
2313 * extent operations left and we know that we skipped some, go back to
2314 * the beginning and do it all again
2315 */
2316 if (all && skipped) {
2317 INIT_LIST_HEAD(&insert_list);
2318 INIT_LIST_HEAD(&update_list);
2319 search = 0;
2320 skipped = 0;
2321 num_inserts = 0;
2322 goto again;
2323 }
2324out:
Chris Mason7bb86312007-12-11 09:25:06 -05002325 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05002326 return 0;
2327}
2328
Zheng Yan31840ae2008-09-23 13:14:14 -04002329static int pin_down_bytes(struct btrfs_trans_handle *trans,
2330 struct btrfs_root *root,
2331 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04002332{
Chris Mason1a5bc1672007-10-15 16:15:26 -04002333 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002334 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04002335
Zheng Yan31840ae2008-09-23 13:14:14 -04002336 if (is_data)
2337 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002338
Zheng Yan31840ae2008-09-23 13:14:14 -04002339 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2340 if (!buf)
2341 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002342
Zheng Yan31840ae2008-09-23 13:14:14 -04002343 /* we can reuse a block if it hasn't been written
2344 * and it is from this transaction. We can't
2345 * reuse anything from the tree log root because
2346 * it has tiny sub-transactions.
2347 */
2348 if (btrfs_buffer_uptodate(buf, 0) &&
2349 btrfs_try_tree_lock(buf)) {
2350 u64 header_owner = btrfs_header_owner(buf);
2351 u64 header_transid = btrfs_header_generation(buf);
2352 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04002353 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04002354 header_transid == trans->transid &&
2355 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2356 clean_tree_block(NULL, root, buf);
2357 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002358 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002359 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04002360 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002361 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04002362 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002363 free_extent_buffer(buf);
2364pinit:
2365 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2366
Chris Masonbe744172007-05-06 10:15:01 -04002367 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002368 return 0;
2369}
2370
Chris Masona28ec192007-03-06 20:08:01 -05002371/*
2372 * remove an extent from the root, returns 0 on success
2373 */
Zheng Yan31840ae2008-09-23 13:14:14 -04002374static int __free_extent(struct btrfs_trans_handle *trans,
2375 struct btrfs_root *root,
2376 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002377 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002378 u64 owner_objectid, int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05002379{
Chris Mason5caf2a02007-04-02 11:20:42 -04002380 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04002381 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04002382 struct btrfs_fs_info *info = root->fs_info;
2383 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002384 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05002385 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05002386 int extent_slot = 0;
2387 int found_extent = 0;
2388 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04002389 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04002390 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05002391
Chris Masondb945352007-10-15 16:15:53 -04002392 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04002393 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04002394 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04002395 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002396 if (!path)
2397 return -ENOMEM;
2398
Chris Mason3c12ac72008-04-21 12:01:38 -04002399 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002400 ret = lookup_extent_backref(trans, extent_root, path,
2401 bytenr, parent, root_objectid,
2402 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05002403 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002404 struct btrfs_key found_key;
2405 extent_slot = path->slots[0];
Chris Masond3977122009-01-05 21:25:51 -05002406 while (extent_slot > 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002407 extent_slot--;
2408 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2409 extent_slot);
2410 if (found_key.objectid != bytenr)
2411 break;
2412 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2413 found_key.offset == num_bytes) {
2414 found_extent = 1;
2415 break;
2416 }
2417 if (path->slots[0] - extent_slot > 5)
2418 break;
2419 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002420 if (!found_extent) {
2421 ret = remove_extent_backref(trans, extent_root, path);
2422 BUG_ON(ret);
2423 btrfs_release_path(extent_root, path);
2424 ret = btrfs_search_slot(trans, extent_root,
2425 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002426 if (ret) {
2427 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05002428 ", was looking for %llu\n", ret,
2429 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002430 btrfs_print_leaf(extent_root, path->nodes[0]);
2431 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002432 BUG_ON(ret);
2433 extent_slot = path->slots[0];
2434 }
Chris Mason7bb86312007-12-11 09:25:06 -05002435 } else {
2436 btrfs_print_leaf(extent_root, path->nodes[0]);
2437 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05002438 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2439 "root %llu gen %llu owner %llu\n",
2440 (unsigned long long)bytenr,
2441 (unsigned long long)root_objectid,
2442 (unsigned long long)ref_generation,
2443 (unsigned long long)owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05002444 }
Chris Mason5f39d392007-10-15 16:14:19 -04002445
2446 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05002447 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04002448 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002449 refs = btrfs_extent_refs(leaf, ei);
2450 BUG_ON(refs == 0);
2451 refs -= 1;
2452 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05002453
Chris Mason5f39d392007-10-15 16:14:19 -04002454 btrfs_mark_buffer_dirty(leaf);
2455
Chris Mason952fcca2008-02-18 16:33:44 -05002456 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002457 struct btrfs_extent_ref *ref;
2458 ref = btrfs_item_ptr(leaf, path->slots[0],
2459 struct btrfs_extent_ref);
2460 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002461 /* if the back ref and the extent are next to each other
2462 * they get deleted below in one shot
2463 */
2464 path->slots[0] = extent_slot;
2465 num_to_del = 2;
2466 } else if (found_extent) {
2467 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002468 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05002469 BUG_ON(ret);
2470 /* if refs are 0, we need to setup the path for deletion */
2471 if (refs == 0) {
2472 btrfs_release_path(extent_root, path);
2473 ret = btrfs_search_slot(trans, extent_root, &key, path,
2474 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002475 BUG_ON(ret);
2476 }
2477 }
2478
Chris Masoncf27e1e2007-03-13 09:49:06 -04002479 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002480 u64 super_used;
2481 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04002482
2483 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002484 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002485 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2486 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04002487 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05002488 if (ret > 0)
2489 mark_free = 1;
2490 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04002491 }
Josef Bacik58176a92007-08-29 15:47:34 -04002492 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05002493 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002494 super_used = btrfs_super_bytes_used(&info->super_copy);
2495 btrfs_set_super_bytes_used(&info->super_copy,
2496 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002497
2498 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002499 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002500 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04002501 root_used - num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05002502 spin_unlock(&info->delalloc_lock);
Chris Mason952fcca2008-02-18 16:33:44 -05002503 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2504 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04002505 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04002506 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01002507
Chris Mason459931e2008-12-10 09:10:46 -05002508 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2509 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2510 BUG_ON(ret);
2511 }
2512
Chris Masondcbdd4d2008-12-16 13:51:01 -05002513 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2514 mark_free);
2515 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05002516 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002517 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002518 finish_current_insert(trans, extent_root, 0);
Chris Masona28ec192007-03-06 20:08:01 -05002519 return ret;
2520}
2521
2522/*
Chris Masonfec577f2007-02-26 10:40:21 -05002523 * find all the blocks marked as pending in the radix tree and remove
2524 * them from the extent map
2525 */
Chris Masond3977122009-01-05 21:25:51 -05002526static int del_pending_extents(struct btrfs_trans_handle *trans,
2527 struct btrfs_root *extent_root, int all)
Chris Masonfec577f2007-02-26 10:40:21 -05002528{
2529 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002530 int err = 0;
Chris Mason1a5bc1672007-10-15 16:15:26 -04002531 u64 start;
2532 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002533 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002534 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002535 int nr = 0, skipped = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002536 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002537 struct extent_io_tree *extent_ins;
2538 struct pending_extent_op *extent_op;
Josef Bacik25179202008-10-29 14:49:05 -04002539 struct btrfs_fs_info *info = extent_root->fs_info;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002540 struct list_head delete_list;
Chris Mason8ef97622007-03-26 10:15:30 -04002541
Josef Bacikf3465ca2008-11-12 14:19:50 -05002542 INIT_LIST_HEAD(&delete_list);
Zheng Yan31840ae2008-09-23 13:14:14 -04002543 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc1672007-10-15 16:15:26 -04002544 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002545
Josef Bacikf3465ca2008-11-12 14:19:50 -05002546again:
2547 mutex_lock(&info->extent_ins_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002548 while (1) {
Josef Bacik25179202008-10-29 14:49:05 -04002549 ret = find_first_extent_bit(pending_del, search, &start, &end,
2550 EXTENT_WRITEBACK);
2551 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002552 if (all && skipped && !nr) {
Josef Bacik25179202008-10-29 14:49:05 -04002553 search = 0;
2554 continue;
2555 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002556 mutex_unlock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002557 break;
Josef Bacik25179202008-10-29 14:49:05 -04002558 }
2559
2560 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2561 if (!ret) {
2562 search = end+1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002563 skipped = 1;
2564
2565 if (need_resched()) {
2566 mutex_unlock(&info->extent_ins_mutex);
2567 cond_resched();
2568 mutex_lock(&info->extent_ins_mutex);
2569 }
2570
Josef Bacik25179202008-10-29 14:49:05 -04002571 continue;
2572 }
2573 BUG_ON(ret < 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002574
2575 ret = get_state_private(pending_del, start, &priv);
2576 BUG_ON(ret);
2577 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2578
Josef Bacik25179202008-10-29 14:49:05 -04002579 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
Chris Mason1a5bc1672007-10-15 16:15:26 -04002580 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002581 if (!test_range_bit(extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04002582 EXTENT_WRITEBACK, 0)) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002583 list_add_tail(&extent_op->list, &delete_list);
2584 nr++;
Chris Masonc286ac42008-07-22 23:06:41 -04002585 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002586 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002587
2588 ret = get_state_private(&info->extent_ins, start,
2589 &priv);
Zheng Yan31840ae2008-09-23 13:14:14 -04002590 BUG_ON(ret);
2591 extent_op = (struct pending_extent_op *)
Josef Bacik25179202008-10-29 14:49:05 -04002592 (unsigned long)priv;
Zheng Yan31840ae2008-09-23 13:14:14 -04002593
Josef Bacik25179202008-10-29 14:49:05 -04002594 clear_extent_bits(&info->extent_ins, start, end,
2595 EXTENT_WRITEBACK, GFP_NOFS);
2596
Josef Bacikf3465ca2008-11-12 14:19:50 -05002597 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2598 list_add_tail(&extent_op->list, &delete_list);
2599 search = end + 1;
2600 nr++;
2601 continue;
2602 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002603
Josef Bacik25179202008-10-29 14:49:05 -04002604 mutex_lock(&extent_root->fs_info->pinned_mutex);
2605 ret = pin_down_bytes(trans, extent_root, start,
2606 end + 1 - start, 0);
2607 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2608
Zheng Yan31840ae2008-09-23 13:14:14 -04002609 ret = update_block_group(trans, extent_root, start,
Josef Bacik25179202008-10-29 14:49:05 -04002610 end + 1 - start, 0, ret > 0);
2611
Josef Bacikf3465ca2008-11-12 14:19:50 -05002612 unlock_extent(extent_ins, start, end, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002613 BUG_ON(ret);
2614 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002615 }
Chris Mason1a5bc1672007-10-15 16:15:26 -04002616 if (ret)
2617 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04002618
Josef Bacikf3465ca2008-11-12 14:19:50 -05002619 search = end + 1;
2620
2621 if (need_resched()) {
2622 mutex_unlock(&info->extent_ins_mutex);
2623 cond_resched();
2624 mutex_lock(&info->extent_ins_mutex);
2625 }
Chris Masonfec577f2007-02-26 10:40:21 -05002626 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002627
2628 if (nr) {
2629 ret = free_extents(trans, extent_root, &delete_list);
2630 BUG_ON(ret);
2631 }
2632
2633 if (all && skipped) {
2634 INIT_LIST_HEAD(&delete_list);
2635 search = 0;
2636 nr = 0;
2637 goto again;
2638 }
2639
Chris Masone20d96d2007-03-22 12:13:20 -04002640 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002641}
2642
2643/*
2644 * remove an extent from the root, returns 0 on success
2645 */
Chris Mason925baed2008-06-25 16:01:30 -04002646static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002647 struct btrfs_root *root,
2648 u64 bytenr, u64 num_bytes, u64 parent,
2649 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002650 u64 owner_objectid, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002651{
Chris Mason9f5fae22007-03-20 14:38:32 -04002652 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002653 int pending_ret;
2654 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002655
Chris Masondb945352007-10-15 16:15:53 -04002656 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002657 if (root == extent_root) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002658 struct pending_extent_op *extent_op = NULL;
2659
2660 mutex_lock(&root->fs_info->extent_ins_mutex);
2661 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2662 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2663 u64 priv;
2664 ret = get_state_private(&root->fs_info->extent_ins,
2665 bytenr, &priv);
2666 BUG_ON(ret);
2667 extent_op = (struct pending_extent_op *)
2668 (unsigned long)priv;
2669
2670 extent_op->del = 1;
2671 if (extent_op->type == PENDING_EXTENT_INSERT) {
2672 mutex_unlock(&root->fs_info->extent_ins_mutex);
2673 return 0;
2674 }
2675 }
2676
2677 if (extent_op) {
2678 ref_generation = extent_op->orig_generation;
2679 parent = extent_op->orig_parent;
2680 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002681
2682 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2683 BUG_ON(!extent_op);
2684
2685 extent_op->type = PENDING_EXTENT_DELETE;
2686 extent_op->bytenr = bytenr;
2687 extent_op->num_bytes = num_bytes;
2688 extent_op->parent = parent;
2689 extent_op->orig_parent = parent;
2690 extent_op->generation = ref_generation;
2691 extent_op->orig_generation = ref_generation;
2692 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002693 INIT_LIST_HEAD(&extent_op->list);
2694 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002695
2696 set_extent_bits(&root->fs_info->pending_del,
2697 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04002698 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002699 set_state_private(&root->fs_info->pending_del,
2700 bytenr, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002701 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002702 return 0;
2703 }
Chris Mason4bef0842008-09-08 11:18:08 -04002704 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002705 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2706 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002707 struct btrfs_block_group_cache *cache;
2708
Chris Masond00aff02008-09-11 15:54:42 -04002709 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002710 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2711 BUG_ON(!cache);
2712 btrfs_add_free_space(cache, bytenr, num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002713 put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04002714 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002715 return 0;
2716 }
Chris Mason4bef0842008-09-08 11:18:08 -04002717 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002718 }
Chris Mason4bef0842008-09-08 11:18:08 -04002719
2720 /* if data pin when any transaction has committed this */
2721 if (ref_generation != trans->transid)
2722 pin = 1;
2723
Zheng Yan31840ae2008-09-23 13:14:14 -04002724 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002725 root_objectid, ref_generation,
2726 owner_objectid, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002727
Chris Mason87ef2bb2008-10-30 11:23:27 -04002728 finish_current_insert(trans, root->fs_info->extent_root, 0);
2729 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002730 return ret ? ret : pending_ret;
2731}
2732
Chris Mason925baed2008-06-25 16:01:30 -04002733int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002734 struct btrfs_root *root,
2735 u64 bytenr, u64 num_bytes, u64 parent,
2736 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002737 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002738{
2739 int ret;
2740
Zheng Yan31840ae2008-09-23 13:14:14 -04002741 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002742 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002743 owner_objectid, pin);
Chris Mason925baed2008-06-25 16:01:30 -04002744 return ret;
2745}
2746
Chris Mason87ee04e2007-11-30 11:30:34 -05002747static u64 stripe_align(struct btrfs_root *root, u64 val)
2748{
2749 u64 mask = ((u64)root->stripesize - 1);
2750 u64 ret = (val + mask) & ~mask;
2751 return ret;
2752}
2753
Chris Masonfec577f2007-02-26 10:40:21 -05002754/*
2755 * walks the btree of allocated extents and find a hole of a given size.
2756 * The key ins is changed to record the hole:
2757 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002758 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002759 * ins->offset == number of blocks
2760 * Any available blocks before search_start are skipped.
2761 */
Chris Masond3977122009-01-05 21:25:51 -05002762static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002763 struct btrfs_root *orig_root,
2764 u64 num_bytes, u64 empty_size,
2765 u64 search_start, u64 search_end,
2766 u64 hint_byte, struct btrfs_key *ins,
2767 u64 exclude_start, u64 exclude_nr,
2768 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002769{
Josef Bacik80eb2342008-10-29 14:49:05 -04002770 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05002771 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002772 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002773 u64 *last_ptr = NULL;
Chris Mason43662112008-11-07 09:06:11 -05002774 u64 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002775 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002776 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002777 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002778 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002779 struct list_head *head = NULL, *cur = NULL;
2780 int loop = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002781 int extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002782 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002783
Chris Masondb945352007-10-15 16:15:53 -04002784 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002785 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002786 ins->objectid = 0;
2787 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002788
Chris Mason0ef3e662008-05-24 14:04:53 -04002789 if (orig_root->ref_cows || empty_size)
2790 allowed_chunk_alloc = 1;
2791
Chris Mason239b14b2008-03-24 15:02:07 -04002792 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2793 last_ptr = &root->fs_info->last_alloc;
Chris Mason43662112008-11-07 09:06:11 -05002794 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002795 }
2796
Josef Bacik0f9dd462008-09-23 13:14:11 -04002797 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002798 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002799
Chris Mason239b14b2008-03-24 15:02:07 -04002800 if (last_ptr) {
Chris Mason43662112008-11-07 09:06:11 -05002801 if (*last_ptr) {
Chris Mason239b14b2008-03-24 15:02:07 -04002802 hint_byte = *last_ptr;
Chris Mason43662112008-11-07 09:06:11 -05002803 last_wanted = *last_ptr;
2804 } else
Chris Mason239b14b2008-03-24 15:02:07 -04002805 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002806 } else {
2807 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002808 }
Chris Masona061fc82008-05-07 11:43:44 -04002809 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002810 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04002811
Chris Mason42e70e72008-11-07 18:17:11 -05002812 if (last_wanted && search_start != last_wanted) {
Chris Mason43662112008-11-07 09:06:11 -05002813 last_wanted = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002814 empty_size += empty_cluster;
2815 }
Chris Mason43662112008-11-07 09:06:11 -05002816
Chris Mason42e70e72008-11-07 18:17:11 -05002817 total_needed += empty_size;
Josef Bacik80eb2342008-10-29 14:49:05 -04002818 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
Yan Zhengd899e052008-10-30 14:25:28 -04002819 if (!block_group)
2820 block_group = btrfs_lookup_first_block_group(root->fs_info,
2821 search_start);
Josef Bacik80eb2342008-10-29 14:49:05 -04002822 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002823
Josef Bacik80eb2342008-10-29 14:49:05 -04002824 down_read(&space_info->groups_sem);
2825 while (1) {
2826 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002827 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002828 * the only way this happens if our hint points to a block
2829 * group thats not of the proper type, while looping this
2830 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002831 */
Chris Mason8a1413a2008-11-10 16:13:54 -05002832 if (empty_size)
2833 extra_loop = 1;
2834
Chris Mason42e70e72008-11-07 18:17:11 -05002835 if (!block_group)
2836 goto new_group_no_lock;
2837
Josef Bacikea6a4782008-11-20 12:16:16 -05002838 if (unlikely(!block_group->cached)) {
2839 mutex_lock(&block_group->cache_mutex);
2840 ret = cache_block_group(root, block_group);
2841 mutex_unlock(&block_group->cache_mutex);
2842 if (ret)
2843 break;
2844 }
2845
Josef Bacik25179202008-10-29 14:49:05 -04002846 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002847 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002848 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002849
Josef Bacikea6a4782008-11-20 12:16:16 -05002850 if (unlikely(block_group->ro))
Josef Bacik80eb2342008-10-29 14:49:05 -04002851 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002852
Josef Bacik80eb2342008-10-29 14:49:05 -04002853 free_space = btrfs_find_free_space(block_group, search_start,
2854 total_needed);
2855 if (free_space) {
2856 u64 start = block_group->key.objectid;
2857 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002858 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002859
Josef Bacik80eb2342008-10-29 14:49:05 -04002860 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002861
Josef Bacik80eb2342008-10-29 14:49:05 -04002862 /* move on to the next group */
2863 if (search_start + num_bytes >= search_end)
2864 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002865
Josef Bacik80eb2342008-10-29 14:49:05 -04002866 /* move on to the next group */
2867 if (search_start + num_bytes > end)
2868 goto new_group;
2869
Chris Mason43662112008-11-07 09:06:11 -05002870 if (last_wanted && search_start != last_wanted) {
Chris Mason3b7885b2008-11-06 21:48:27 -05002871 total_needed += empty_cluster;
Chris Mason8a1413a2008-11-10 16:13:54 -05002872 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002873 last_wanted = 0;
Chris Mason3b7885b2008-11-06 21:48:27 -05002874 /*
2875 * if search_start is still in this block group
2876 * then we just re-search this block group
2877 */
2878 if (search_start >= start &&
2879 search_start < end) {
2880 mutex_unlock(&block_group->alloc_mutex);
2881 continue;
2882 }
2883
2884 /* else we go to the next block group */
2885 goto new_group;
2886 }
2887
Josef Bacik80eb2342008-10-29 14:49:05 -04002888 if (exclude_nr > 0 &&
2889 (search_start + num_bytes > exclude_start &&
2890 search_start < exclude_start + exclude_nr)) {
2891 search_start = exclude_start + exclude_nr;
2892 /*
2893 * if search_start is still in this block group
2894 * then we just re-search this block group
2895 */
2896 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002897 search_start < end) {
2898 mutex_unlock(&block_group->alloc_mutex);
Chris Mason43662112008-11-07 09:06:11 -05002899 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002900 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002901 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002902
2903 /* else we go to the next block group */
2904 goto new_group;
2905 }
2906
2907 ins->objectid = search_start;
2908 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002909
2910 btrfs_remove_free_space_lock(block_group, search_start,
2911 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002912 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002913 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002914 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002915 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002916new_group:
Chris Mason42e70e72008-11-07 18:17:11 -05002917 mutex_unlock(&block_group->alloc_mutex);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002918 put_block_group(block_group);
2919 block_group = NULL;
Chris Mason42e70e72008-11-07 18:17:11 -05002920new_group_no_lock:
Chris Masonf5a31e12008-11-10 11:47:09 -05002921 /* don't try to compare new allocations against the
2922 * last allocation any more
2923 */
Chris Mason43662112008-11-07 09:06:11 -05002924 last_wanted = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002925
Josef Bacik80eb2342008-10-29 14:49:05 -04002926 /*
2927 * Here's how this works.
2928 * loop == 0: we were searching a block group via a hint
2929 * and didn't find anything, so we start at
2930 * the head of the block groups and keep searching
2931 * loop == 1: we're searching through all of the block groups
2932 * if we hit the head again we have searched
2933 * all of the block groups for this space and we
2934 * need to try and allocate, if we cant error out.
2935 * loop == 2: we allocated more space and are looping through
2936 * all of the block groups again.
2937 */
2938 if (loop == 0) {
2939 head = &space_info->block_groups;
2940 cur = head->next;
Josef Bacik80eb2342008-10-29 14:49:05 -04002941 loop++;
2942 } else if (loop == 1 && cur == head) {
Chris Masonf5a31e12008-11-10 11:47:09 -05002943 int keep_going;
Chris Mason42e70e72008-11-07 18:17:11 -05002944
Chris Masonf5a31e12008-11-10 11:47:09 -05002945 /* at this point we give up on the empty_size
2946 * allocations and just try to allocate the min
2947 * space.
2948 *
2949 * The extra_loop field was set if an empty_size
2950 * allocation was attempted above, and if this
2951 * is try we need to try the loop again without
2952 * the additional empty_size.
2953 */
Chris Mason5b7c3fc2008-11-10 07:26:33 -05002954 total_needed -= empty_size;
2955 empty_size = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002956 keep_going = extra_loop;
2957 loop++;
Chris Mason42e70e72008-11-07 18:17:11 -05002958
Josef Bacik80eb2342008-10-29 14:49:05 -04002959 if (allowed_chunk_alloc && !chunk_alloc_done) {
2960 up_read(&space_info->groups_sem);
2961 ret = do_chunk_alloc(trans, root, num_bytes +
2962 2 * 1024 * 1024, data, 1);
Josef Bacik80eb2342008-10-29 14:49:05 -04002963 down_read(&space_info->groups_sem);
Chris Mason2ed6d662008-11-13 09:59:33 -05002964 if (ret < 0)
2965 goto loop_check;
Josef Bacik80eb2342008-10-29 14:49:05 -04002966 head = &space_info->block_groups;
Chris Masonf5a31e12008-11-10 11:47:09 -05002967 /*
2968 * we've allocated a new chunk, keep
2969 * trying
2970 */
2971 keep_going = 1;
Josef Bacik80eb2342008-10-29 14:49:05 -04002972 chunk_alloc_done = 1;
2973 } else if (!allowed_chunk_alloc) {
2974 space_info->force_alloc = 1;
Chris Masonf5a31e12008-11-10 11:47:09 -05002975 }
Chris Mason2ed6d662008-11-13 09:59:33 -05002976loop_check:
Chris Masonf5a31e12008-11-10 11:47:09 -05002977 if (keep_going) {
2978 cur = head->next;
2979 extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002980 } else {
2981 break;
2982 }
2983 } else if (cur == head) {
2984 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002985 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002986
2987 block_group = list_entry(cur, struct btrfs_block_group_cache,
2988 list);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002989 atomic_inc(&block_group->count);
2990
Josef Bacik80eb2342008-10-29 14:49:05 -04002991 search_start = block_group->key.objectid;
2992 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002993 }
Chris Mason0b86a832008-03-24 15:01:56 -04002994
Josef Bacik80eb2342008-10-29 14:49:05 -04002995 /* we found what we needed */
2996 if (ins->objectid) {
2997 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05002998 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04002999
3000 if (last_ptr)
3001 *last_ptr = ins->objectid + ins->offset;
3002 ret = 0;
3003 } else if (!ret) {
Chris Masond3977122009-01-05 21:25:51 -05003004 printk(KERN_ERR "btrfs searching for %llu bytes, "
3005 "num_bytes %llu, loop %d, allowed_alloc %d\n",
3006 (unsigned long long)total_needed,
3007 (unsigned long long)num_bytes,
Josef Bacik4ce4cb52008-11-17 21:12:00 -05003008 loop, allowed_chunk_alloc);
Josef Bacik80eb2342008-10-29 14:49:05 -04003009 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04003010 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05003011 if (block_group)
3012 put_block_group(block_group);
Chris Mason0b86a832008-03-24 15:01:56 -04003013
Josef Bacik80eb2342008-10-29 14:49:05 -04003014 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05003015 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003016}
Chris Masonec44a352008-04-28 15:29:52 -04003017
Josef Bacik0f9dd462008-09-23 13:14:11 -04003018static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3019{
3020 struct btrfs_block_group_cache *cache;
3021 struct list_head *l;
3022
Chris Masond3977122009-01-05 21:25:51 -05003023 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
3024 (unsigned long long)(info->total_bytes - info->bytes_used -
3025 info->bytes_pinned - info->bytes_reserved),
3026 (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04003027
Josef Bacik80eb2342008-10-29 14:49:05 -04003028 down_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003029 list_for_each(l, &info->block_groups) {
3030 cache = list_entry(l, struct btrfs_block_group_cache, list);
3031 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05003032 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
3033 "%llu pinned %llu reserved\n",
3034 (unsigned long long)cache->key.objectid,
3035 (unsigned long long)cache->key.offset,
3036 (unsigned long long)btrfs_block_group_used(&cache->item),
3037 (unsigned long long)cache->pinned,
3038 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003039 btrfs_dump_free_space(cache, bytes);
3040 spin_unlock(&cache->lock);
3041 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003042 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003043}
Zheng Yane8569812008-09-26 10:05:48 -04003044
Chris Masone6dcd2d2008-07-17 12:53:50 -04003045static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3046 struct btrfs_root *root,
3047 u64 num_bytes, u64 min_alloc_size,
3048 u64 empty_size, u64 hint_byte,
3049 u64 search_end, struct btrfs_key *ins,
3050 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05003051{
3052 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04003053 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003054 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04003055 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003056
Chris Mason6324fbf2008-03-24 15:01:59 -04003057 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04003058 alloc_profile = info->avail_data_alloc_bits &
Chris Masond3977122009-01-05 21:25:51 -05003059 info->data_alloc_profile;
Chris Mason8790d502008-04-03 16:29:03 -04003060 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003061 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04003062 alloc_profile = info->avail_system_alloc_bits &
Chris Masond3977122009-01-05 21:25:51 -05003063 info->system_alloc_profile;
Chris Mason8790d502008-04-03 16:29:03 -04003064 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003065 } else {
Chris Mason8790d502008-04-03 16:29:03 -04003066 alloc_profile = info->avail_metadata_alloc_bits &
Chris Masond3977122009-01-05 21:25:51 -05003067 info->metadata_alloc_profile;
Chris Mason8790d502008-04-03 16:29:03 -04003068 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003069 }
Chris Mason98d20f62008-04-14 09:46:10 -04003070again:
Yan Zheng2b820322008-11-17 21:11:30 -05003071 data = btrfs_reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04003072 /*
3073 * the only place that sets empty_size is btrfs_realloc_node, which
3074 * is not called recursively on allocations
3075 */
3076 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04003077 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04003078 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003079 2 * 1024 * 1024,
3080 BTRFS_BLOCK_GROUP_METADATA |
3081 (info->metadata_alloc_profile &
3082 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003083 }
3084 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003085 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003086 }
Chris Mason0b86a832008-03-24 15:01:56 -04003087
Chris Masondb945352007-10-15 16:15:53 -04003088 WARN_ON(num_bytes < root->sectorsize);
3089 ret = find_free_extent(trans, root, num_bytes, empty_size,
3090 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04003091 trans->alloc_exclude_start,
3092 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04003093
Chris Mason98d20f62008-04-14 09:46:10 -04003094 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3095 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003096 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003097 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04003098 do_chunk_alloc(trans, root->fs_info->extent_root,
3099 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003100 goto again;
3101 }
Chris Masonec44a352008-04-28 15:29:52 -04003102 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003103 struct btrfs_space_info *sinfo;
3104
3105 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05003106 printk(KERN_ERR "btrfs allocation failed flags %llu, "
3107 "wanted %llu\n", (unsigned long long)data,
3108 (unsigned long long)num_bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003109 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04003110 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04003111 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003112
3113 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003114}
3115
Chris Mason65b51a02008-08-01 15:11:20 -04003116int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3117{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003118 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003119 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003120
Josef Bacik0f9dd462008-09-23 13:14:11 -04003121 cache = btrfs_lookup_block_group(root->fs_info, start);
3122 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05003123 printk(KERN_ERR "Unable to find block group for %llu\n",
3124 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003125 return -ENOSPC;
3126 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05003127
3128 ret = btrfs_discard_extent(root, start, len);
3129
Josef Bacik0f9dd462008-09-23 13:14:11 -04003130 btrfs_add_free_space(cache, start, len);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003131 put_block_group(cache);
Zheng Yan1a40e232008-09-26 10:09:34 -04003132 update_reserved_extents(root, start, len, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05003133
3134 return ret;
Chris Mason65b51a02008-08-01 15:11:20 -04003135}
3136
Chris Masone6dcd2d2008-07-17 12:53:50 -04003137int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3138 struct btrfs_root *root,
3139 u64 num_bytes, u64 min_alloc_size,
3140 u64 empty_size, u64 hint_byte,
3141 u64 search_end, struct btrfs_key *ins,
3142 u64 data)
3143{
3144 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003145 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3146 empty_size, hint_byte, search_end, ins,
3147 data);
Zheng Yane8569812008-09-26 10:05:48 -04003148 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003149 return ret;
3150}
3151
3152static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003153 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003154 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003155 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003156{
3157 int ret;
3158 int pending_ret;
3159 u64 super_used;
3160 u64 root_used;
3161 u64 num_bytes = ins->offset;
3162 u32 sizes[2];
3163 struct btrfs_fs_info *info = root->fs_info;
3164 struct btrfs_root *extent_root = info->extent_root;
3165 struct btrfs_extent_item *extent_item;
3166 struct btrfs_extent_ref *ref;
3167 struct btrfs_path *path;
3168 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04003169
Zheng Yan31840ae2008-09-23 13:14:14 -04003170 if (parent == 0)
3171 parent = ins->objectid;
3172
Josef Bacik58176a92007-08-29 15:47:34 -04003173 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05003174 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04003175 super_used = btrfs_super_bytes_used(&info->super_copy);
3176 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04003177
Josef Bacik58176a92007-08-29 15:47:34 -04003178 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04003179 root_used = btrfs_root_used(&root->root_item);
3180 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05003181 spin_unlock(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04003182
Chris Mason26b80032007-08-08 20:17:12 -04003183 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003184 struct pending_extent_op *extent_op;
3185
3186 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3187 BUG_ON(!extent_op);
3188
3189 extent_op->type = PENDING_EXTENT_INSERT;
3190 extent_op->bytenr = ins->objectid;
3191 extent_op->num_bytes = ins->offset;
3192 extent_op->parent = parent;
3193 extent_op->orig_parent = 0;
3194 extent_op->generation = ref_generation;
3195 extent_op->orig_generation = 0;
3196 extent_op->level = (int)owner;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003197 INIT_LIST_HEAD(&extent_op->list);
3198 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003199
Josef Bacik25179202008-10-29 14:49:05 -04003200 mutex_lock(&root->fs_info->extent_ins_mutex);
Chris Mason1a5bc1672007-10-15 16:15:26 -04003201 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3202 ins->objectid + ins->offset - 1,
Josef Bacik25179202008-10-29 14:49:05 -04003203 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04003204 set_state_private(&root->fs_info->extent_ins,
3205 ins->objectid, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04003206 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04003207 goto update_block;
3208 }
3209
Chris Mason47e4bb92008-02-01 14:51:59 -05003210 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05003211 keys[1].objectid = ins->objectid;
3212 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04003213 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05003214 sizes[0] = sizeof(*extent_item);
3215 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05003216
3217 path = btrfs_alloc_path();
3218 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05003219
3220 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3221 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04003222 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003223
Chris Mason47e4bb92008-02-01 14:51:59 -05003224 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3225 struct btrfs_extent_item);
3226 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3227 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3228 struct btrfs_extent_ref);
3229
3230 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3231 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3232 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Zheng Yan31840ae2008-09-23 13:14:14 -04003233 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05003234
3235 btrfs_mark_buffer_dirty(path->nodes[0]);
3236
3237 trans->alloc_exclude_start = 0;
3238 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05003239 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04003240 finish_current_insert(trans, extent_root, 0);
3241 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -04003242
Chris Mason925baed2008-06-25 16:01:30 -04003243 if (ret)
3244 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003245 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04003246 ret = pending_ret;
3247 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003248 }
Chris Mason26b80032007-08-08 20:17:12 -04003249
3250update_block:
Chris Masond3977122009-01-05 21:25:51 -05003251 ret = update_block_group(trans, root, ins->objectid,
3252 ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05003253 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05003254 printk(KERN_ERR "btrfs update block group failed for %llu "
3255 "%llu\n", (unsigned long long)ins->objectid,
3256 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05003257 BUG();
3258 }
Chris Mason925baed2008-06-25 16:01:30 -04003259out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04003260 return ret;
3261}
3262
3263int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003264 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003265 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003266 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003267{
3268 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04003269
3270 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3271 return 0;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003272 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3273 ref_generation, owner, ins);
Zheng Yane8569812008-09-26 10:05:48 -04003274 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003275 return ret;
3276}
Chris Masone02119d2008-09-05 16:13:11 -04003277
3278/*
3279 * this is used by the tree logging recovery code. It records that
3280 * an extent has been allocated and makes sure to clear the free
3281 * space cache bits as well
3282 */
3283int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003284 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04003285 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003286 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04003287{
3288 int ret;
3289 struct btrfs_block_group_cache *block_group;
3290
Chris Masone02119d2008-09-05 16:13:11 -04003291 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikea6a4782008-11-20 12:16:16 -05003292 mutex_lock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003293 cache_block_group(root, block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05003294 mutex_unlock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003295
Josef Bacikea6a4782008-11-20 12:16:16 -05003296 ret = btrfs_remove_free_space(block_group, ins->objectid,
3297 ins->offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003298 BUG_ON(ret);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003299 put_block_group(block_group);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003300 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3301 ref_generation, owner, ins);
Chris Masone02119d2008-09-05 16:13:11 -04003302 return ret;
3303}
3304
Chris Masone6dcd2d2008-07-17 12:53:50 -04003305/*
3306 * finds a free extent and does all the dirty work required for allocation
3307 * returns the key for the extent through ins, and a tree buffer for
3308 * the first block of the extent through buf.
3309 *
3310 * returns 0 if everything worked, non-zero otherwise.
3311 */
3312int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3313 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003314 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003315 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003316 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003317 u64 search_end, struct btrfs_key *ins, u64 data)
3318{
3319 int ret;
3320
Chris Masone6dcd2d2008-07-17 12:53:50 -04003321 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3322 min_alloc_size, empty_size, hint_byte,
3323 search_end, ins, data);
3324 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04003325 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003326 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3327 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003328 owner_objectid, ins);
Chris Masond00aff02008-09-11 15:54:42 -04003329 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003330
Zheng Yane8569812008-09-26 10:05:48 -04003331 } else {
3332 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04003333 }
Chris Mason925baed2008-06-25 16:01:30 -04003334 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003335}
Chris Mason65b51a02008-08-01 15:11:20 -04003336
3337struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3338 struct btrfs_root *root,
3339 u64 bytenr, u32 blocksize)
3340{
3341 struct extent_buffer *buf;
3342
3343 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3344 if (!buf)
3345 return ERR_PTR(-ENOMEM);
3346 btrfs_set_header_generation(buf, trans->transid);
3347 btrfs_tree_lock(buf);
3348 clean_tree_block(trans, root, buf);
3349 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04003350 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3351 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04003352 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04003353 } else {
3354 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3355 buf->start + buf->len - 1, GFP_NOFS);
3356 }
Chris Mason65b51a02008-08-01 15:11:20 -04003357 trans->blocks_used++;
3358 return buf;
3359}
3360
Chris Masonfec577f2007-02-26 10:40:21 -05003361/*
3362 * helper function to allocate a block for a given tree
3363 * returns the tree buffer or NULL.
3364 */
Chris Mason5f39d392007-10-15 16:14:19 -04003365struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04003366 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003367 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05003368 u64 root_objectid,
3369 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05003370 int level,
3371 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04003372 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05003373{
Chris Masone2fa7222007-03-12 16:22:34 -04003374 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05003375 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003376 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05003377
Zheng Yan31840ae2008-09-23 13:14:14 -04003378 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003379 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04003380 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05003381 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04003382 BUG_ON(ret > 0);
3383 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05003384 }
Chris Mason55c69072008-01-09 15:55:33 -05003385
Chris Mason65b51a02008-08-01 15:11:20 -04003386 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05003387 return buf;
3388}
Chris Masona28ec192007-03-06 20:08:01 -05003389
Chris Masone02119d2008-09-05 16:13:11 -04003390int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3391 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04003392{
Chris Mason7bb86312007-12-11 09:25:06 -05003393 u64 leaf_owner;
3394 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04003395 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04003396 struct btrfs_file_extent_item *fi;
3397 int i;
3398 int nritems;
3399 int ret;
3400
Chris Mason5f39d392007-10-15 16:14:19 -04003401 BUG_ON(!btrfs_is_leaf(leaf));
3402 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05003403 leaf_owner = btrfs_header_owner(leaf);
3404 leaf_generation = btrfs_header_generation(leaf);
3405
Chris Mason6407bf62007-03-27 06:33:00 -04003406 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003407 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04003408 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04003409
3410 btrfs_item_key_to_cpu(leaf, &key, i);
3411 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04003412 continue;
3413 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003414 if (btrfs_file_extent_type(leaf, fi) ==
3415 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04003416 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04003417 /*
3418 * FIXME make sure to insert a trans record that
3419 * repeats the snapshot del on crash
3420 */
Chris Masondb945352007-10-15 16:15:53 -04003421 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3422 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04003423 continue;
Chris Mason4a096752008-07-21 10:29:44 -04003424
Chris Mason925baed2008-06-25 16:01:30 -04003425 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003426 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04003427 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003428 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003429 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04003430
3431 atomic_inc(&root->fs_info->throttle_gen);
3432 wake_up(&root->fs_info->transaction_throttle);
3433 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04003434 }
3435 return 0;
3436}
3437
Chris Masond3977122009-01-05 21:25:51 -05003438static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003439 struct btrfs_root *root,
3440 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04003441{
3442 int i;
3443 int ret;
3444 struct btrfs_extent_info *info = ref->extents;
3445
Yan Zheng31153d82008-07-28 15:32:19 -04003446 for (i = 0; i < ref->nritems; i++) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003447 ret = __btrfs_free_extent(trans, root, info->bytenr,
3448 info->num_bytes, ref->bytenr,
3449 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003450 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04003451
3452 atomic_inc(&root->fs_info->throttle_gen);
3453 wake_up(&root->fs_info->transaction_throttle);
3454 cond_resched();
3455
Yan Zheng31153d82008-07-28 15:32:19 -04003456 BUG_ON(ret);
3457 info++;
3458 }
Yan Zheng31153d82008-07-28 15:32:19 -04003459
3460 return 0;
3461}
3462
Chris Masond3977122009-01-05 21:25:51 -05003463static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start,
3464 u64 len, u32 *refs)
Chris Mason333db942008-06-25 16:01:30 -04003465{
Chris Mason017e5362008-07-28 15:32:51 -04003466 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04003467
Zheng Yan31840ae2008-09-23 13:14:14 -04003468 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04003469 BUG_ON(ret);
3470
Chris Masond3977122009-01-05 21:25:51 -05003471#if 0 /* some debugging code in case we see problems here */
Chris Masonf87f0572008-08-01 11:27:23 -04003472 /* if the refs count is one, it won't get increased again. But
3473 * if the ref count is > 1, someone may be decreasing it at
3474 * the same time we are.
3475 */
3476 if (*refs != 1) {
3477 struct extent_buffer *eb = NULL;
3478 eb = btrfs_find_create_tree_block(root, start, len);
3479 if (eb)
3480 btrfs_tree_lock(eb);
3481
3482 mutex_lock(&root->fs_info->alloc_mutex);
3483 ret = lookup_extent_ref(NULL, root, start, len, refs);
3484 BUG_ON(ret);
3485 mutex_unlock(&root->fs_info->alloc_mutex);
3486
3487 if (eb) {
3488 btrfs_tree_unlock(eb);
3489 free_extent_buffer(eb);
3490 }
3491 if (*refs == 1) {
Chris Masond3977122009-01-05 21:25:51 -05003492 printk(KERN_ERR "btrfs block %llu went down to one "
3493 "during drop_snap\n", (unsigned long long)start);
Chris Masonf87f0572008-08-01 11:27:23 -04003494 }
3495
3496 }
3497#endif
3498
Chris Masone7a84562008-06-25 16:01:31 -04003499 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04003500 return ret;
Chris Mason333db942008-06-25 16:01:30 -04003501}
3502
3503/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003504 * helper function for drop_snapshot, this walks down the tree dropping ref
3505 * counts as it goes.
3506 */
Chris Masond3977122009-01-05 21:25:51 -05003507static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003508 struct btrfs_root *root,
3509 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003510{
Chris Mason7bb86312007-12-11 09:25:06 -05003511 u64 root_owner;
3512 u64 root_gen;
3513 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04003514 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003515 struct extent_buffer *next;
3516 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05003517 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04003518 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04003519 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05003520 int ret;
3521 u32 refs;
3522
Chris Mason5caf2a02007-04-02 11:20:42 -04003523 WARN_ON(*level < 0);
3524 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04003525 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04003526 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05003527 BUG_ON(ret);
3528 if (refs > 1)
3529 goto out;
Chris Masone0115992007-06-19 16:23:05 -04003530
Chris Mason9aca1d52007-03-13 11:09:37 -04003531 /*
3532 * walk down to the last node level and free all the leaves
3533 */
Chris Masond3977122009-01-05 21:25:51 -05003534 while (*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003535 WARN_ON(*level < 0);
3536 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05003537 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04003538
Chris Mason5f39d392007-10-15 16:14:19 -04003539 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04003540 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04003541
Chris Mason7518a232007-03-12 12:01:18 -04003542 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04003543 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05003544 break;
Chris Mason6407bf62007-03-27 06:33:00 -04003545 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003546 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04003547 BUG_ON(ret);
3548 break;
3549 }
Chris Masondb945352007-10-15 16:15:53 -04003550 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04003551 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04003552 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04003553
Chris Mason333db942008-06-25 16:01:30 -04003554 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04003555 BUG_ON(ret);
3556 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05003557 parent = path->nodes[*level];
3558 root_owner = btrfs_header_owner(parent);
3559 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05003560 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04003561
Chris Mason925baed2008-06-25 16:01:30 -04003562 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003563 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003564 root_owner, root_gen,
3565 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05003566 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04003567
3568 atomic_inc(&root->fs_info->throttle_gen);
3569 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04003570 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04003571
Chris Mason20524f02007-03-10 06:35:47 -05003572 continue;
3573 }
Chris Masonf87f0572008-08-01 11:27:23 -04003574 /*
3575 * at this point, we have a single ref, and since the
3576 * only place referencing this extent is a dead root
3577 * the reference count should never go higher.
3578 * So, we don't need to check it again
3579 */
Yan Zheng31153d82008-07-28 15:32:19 -04003580 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04003581 ref = btrfs_lookup_leaf_ref(root, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04003582 if (ref && ref->generation != ptr_gen) {
3583 btrfs_free_leaf_ref(root, ref);
3584 ref = NULL;
3585 }
Yan Zheng31153d82008-07-28 15:32:19 -04003586 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04003587 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003588 BUG_ON(ret);
3589 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04003590 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003591 *level = 0;
3592 break;
3593 }
3594 }
Chris Masondb945352007-10-15 16:15:53 -04003595 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04003596 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003597 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04003598
Chris Masonca7a79a2008-05-12 12:59:19 -04003599 next = read_tree_block(root, bytenr, blocksize,
3600 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04003601 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04003602#if 0
3603 /*
3604 * this is a debugging check and can go away
3605 * the ref should never go all the way down to 1
3606 * at this point
3607 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003608 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
3609 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04003610 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003611 WARN_ON(refs != 1);
3612#endif
Chris Masone9d0b132007-08-10 14:06:19 -04003613 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003614 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04003615 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04003616 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05003617 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04003618 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05003619 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04003620 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003621 }
3622out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003623 WARN_ON(*level < 0);
3624 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05003625
3626 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003627 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003628 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003629 } else {
3630 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003631 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003632 }
3633
Yan Zheng31153d82008-07-28 15:32:19 -04003634 blocksize = btrfs_level_size(root, *level);
3635 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003636 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003637
3638 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003639 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003640 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003641 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003642 path->nodes[*level] = NULL;
3643 *level += 1;
3644 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003645
Chris Masone7a84562008-06-25 16:01:31 -04003646 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003647 return 0;
3648}
3649
Chris Mason9aca1d52007-03-13 11:09:37 -04003650/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04003651 * helper function for drop_subtree, this function is similar to
3652 * walk_down_tree. The main difference is that it checks reference
3653 * counts while tree blocks are locked.
3654 */
Chris Masond3977122009-01-05 21:25:51 -05003655static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003656 struct btrfs_root *root,
3657 struct btrfs_path *path, int *level)
3658{
3659 struct extent_buffer *next;
3660 struct extent_buffer *cur;
3661 struct extent_buffer *parent;
3662 u64 bytenr;
3663 u64 ptr_gen;
3664 u32 blocksize;
3665 u32 refs;
3666 int ret;
3667
3668 cur = path->nodes[*level];
3669 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3670 &refs);
3671 BUG_ON(ret);
3672 if (refs > 1)
3673 goto out;
3674
3675 while (*level >= 0) {
3676 cur = path->nodes[*level];
3677 if (*level == 0) {
3678 ret = btrfs_drop_leaf_ref(trans, root, cur);
3679 BUG_ON(ret);
3680 clean_tree_block(trans, root, cur);
3681 break;
3682 }
3683 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3684 clean_tree_block(trans, root, cur);
3685 break;
3686 }
3687
3688 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3689 blocksize = btrfs_level_size(root, *level - 1);
3690 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3691
3692 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3693 btrfs_tree_lock(next);
3694
3695 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3696 &refs);
3697 BUG_ON(ret);
3698 if (refs > 1) {
3699 parent = path->nodes[*level];
3700 ret = btrfs_free_extent(trans, root, bytenr,
3701 blocksize, parent->start,
3702 btrfs_header_owner(parent),
3703 btrfs_header_generation(parent),
3704 *level - 1, 1);
3705 BUG_ON(ret);
3706 path->slots[*level]++;
3707 btrfs_tree_unlock(next);
3708 free_extent_buffer(next);
3709 continue;
3710 }
3711
3712 *level = btrfs_header_level(next);
3713 path->nodes[*level] = next;
3714 path->slots[*level] = 0;
3715 path->locks[*level] = 1;
3716 cond_resched();
3717 }
3718out:
3719 parent = path->nodes[*level + 1];
3720 bytenr = path->nodes[*level]->start;
3721 blocksize = path->nodes[*level]->len;
3722
3723 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3724 parent->start, btrfs_header_owner(parent),
3725 btrfs_header_generation(parent), *level, 1);
3726 BUG_ON(ret);
3727
3728 if (path->locks[*level]) {
3729 btrfs_tree_unlock(path->nodes[*level]);
3730 path->locks[*level] = 0;
3731 }
3732 free_extent_buffer(path->nodes[*level]);
3733 path->nodes[*level] = NULL;
3734 *level += 1;
3735 cond_resched();
3736 return 0;
3737}
3738
3739/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003740 * helper for dropping snapshots. This walks back up the tree in the path
3741 * to find the first node higher up where we haven't yet gone through
3742 * all the slots
3743 */
Chris Masond3977122009-01-05 21:25:51 -05003744static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003745 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003746 struct btrfs_path *path,
3747 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003748{
Chris Mason7bb86312007-12-11 09:25:06 -05003749 u64 root_owner;
3750 u64 root_gen;
3751 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003752 int i;
3753 int slot;
3754 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003755
Yan Zhengf82d02d2008-10-29 14:49:05 -04003756 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003757 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003758 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3759 struct extent_buffer *node;
3760 struct btrfs_disk_key disk_key;
3761 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003762 path->slots[i]++;
3763 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003764 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003765 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003766 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003767 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003768 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003769 return 0;
3770 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003771 struct extent_buffer *parent;
3772 if (path->nodes[*level] == root->node)
3773 parent = path->nodes[*level];
3774 else
3775 parent = path->nodes[*level + 1];
3776
3777 root_owner = btrfs_header_owner(parent);
3778 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003779
3780 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003781 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003782 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003783 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003784 parent->start, root_owner,
3785 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003786 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003787 if (path->locks[*level]) {
3788 btrfs_tree_unlock(path->nodes[*level]);
3789 path->locks[*level] = 0;
3790 }
Chris Mason5f39d392007-10-15 16:14:19 -04003791 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003792 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003793 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003794 }
3795 }
3796 return 1;
3797}
3798
Chris Mason9aca1d52007-03-13 11:09:37 -04003799/*
3800 * drop the reference count on the tree rooted at 'snap'. This traverses
3801 * the tree freeing any blocks that have a ref count of zero after being
3802 * decremented.
3803 */
Chris Masone089f052007-03-16 16:20:31 -04003804int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003805 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003806{
Chris Mason3768f362007-03-13 16:47:54 -04003807 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003808 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003809 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003810 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003811 int i;
3812 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003813 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003814
Chris Masona2135012008-06-25 16:01:30 -04003815 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003816 path = btrfs_alloc_path();
3817 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003818
Chris Mason5f39d392007-10-15 16:14:19 -04003819 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003820 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003821 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3822 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003823 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003824 path->slots[level] = 0;
3825 } else {
3826 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003827 struct btrfs_disk_key found_key;
3828 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003829
Chris Mason9f3a7422007-08-07 15:52:19 -04003830 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003831 level = root_item->drop_level;
3832 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003833 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003834 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003835 ret = wret;
3836 goto out;
3837 }
Chris Mason5f39d392007-10-15 16:14:19 -04003838 node = path->nodes[level];
3839 btrfs_node_key(node, &found_key, path->slots[level]);
3840 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3841 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003842 /*
3843 * unlock our path, this is safe because only this
3844 * function is allowed to delete this snapshot
3845 */
Chris Mason925baed2008-06-25 16:01:30 -04003846 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3847 if (path->nodes[i] && path->locks[i]) {
3848 path->locks[i] = 0;
3849 btrfs_tree_unlock(path->nodes[i]);
3850 }
3851 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003852 }
Chris Masond3977122009-01-05 21:25:51 -05003853 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003854 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003855 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003856 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003857 if (wret < 0)
3858 ret = wret;
3859
Yan Zhengf82d02d2008-10-29 14:49:05 -04003860 wret = walk_up_tree(trans, root, path, &level,
3861 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003862 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003863 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003864 if (wret < 0)
3865 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003866 if (trans->transaction->in_commit) {
3867 ret = -EAGAIN;
3868 break;
3869 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003870 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003871 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003872 }
Chris Mason83e15a22007-03-12 09:03:27 -04003873 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003874 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003875 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003876 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003877 }
Chris Mason20524f02007-03-10 06:35:47 -05003878 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003879out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003880 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003881 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003882}
Chris Mason9078a3e2007-04-26 16:46:15 -04003883
Yan Zhengf82d02d2008-10-29 14:49:05 -04003884int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3885 struct btrfs_root *root,
3886 struct extent_buffer *node,
3887 struct extent_buffer *parent)
3888{
3889 struct btrfs_path *path;
3890 int level;
3891 int parent_level;
3892 int ret = 0;
3893 int wret;
3894
3895 path = btrfs_alloc_path();
3896 BUG_ON(!path);
3897
3898 BUG_ON(!btrfs_tree_locked(parent));
3899 parent_level = btrfs_header_level(parent);
3900 extent_buffer_get(parent);
3901 path->nodes[parent_level] = parent;
3902 path->slots[parent_level] = btrfs_header_nritems(parent);
3903
3904 BUG_ON(!btrfs_tree_locked(node));
3905 level = btrfs_header_level(node);
3906 extent_buffer_get(node);
3907 path->nodes[level] = node;
3908 path->slots[level] = 0;
3909
3910 while (1) {
3911 wret = walk_down_subtree(trans, root, path, &level);
3912 if (wret < 0)
3913 ret = wret;
3914 if (wret != 0)
3915 break;
3916
3917 wret = walk_up_tree(trans, root, path, &level, parent_level);
3918 if (wret < 0)
3919 ret = wret;
3920 if (wret != 0)
3921 break;
3922 }
3923
3924 btrfs_free_path(path);
3925 return ret;
3926}
3927
Chris Mason8e7bf942008-04-28 09:02:36 -04003928static unsigned long calc_ra(unsigned long start, unsigned long last,
3929 unsigned long nr)
3930{
3931 return min(last, start + nr - 1);
3932}
3933
Chris Masond3977122009-01-05 21:25:51 -05003934static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05003935 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003936{
3937 u64 page_start;
3938 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003939 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003940 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003941 unsigned long i;
3942 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003943 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003944 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003945 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003946 unsigned int total_read = 0;
3947 unsigned int total_dirty = 0;
3948 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003949
3950 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003951
3952 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003953 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003954 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3955
Zheng Yan1a40e232008-09-26 10:09:34 -04003956 /* make sure the dirty trick played by the caller work */
3957 ret = invalidate_inode_pages2_range(inode->i_mapping,
3958 first_index, last_index);
3959 if (ret)
3960 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003961
Chris Mason4313b392008-01-03 09:08:48 -05003962 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003963
Zheng Yan1a40e232008-09-26 10:09:34 -04003964 for (i = first_index ; i <= last_index; i++) {
3965 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003966 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003967 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003968 }
3969 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003970again:
3971 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003972 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003973 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003974 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003975 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003976 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003977 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003978 if (!PageUptodate(page)) {
3979 btrfs_readpage(NULL, page);
3980 lock_page(page);
3981 if (!PageUptodate(page)) {
3982 unlock_page(page);
3983 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003984 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003985 goto out_unlock;
3986 }
3987 }
Chris Masonec44a352008-04-28 15:29:52 -04003988 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003989
Chris Masonedbd8d42007-12-21 16:27:24 -05003990 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3991 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003992 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003993
Chris Mason3eaa2882008-07-24 11:57:52 -04003994 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3995 if (ordered) {
3996 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3997 unlock_page(page);
3998 page_cache_release(page);
3999 btrfs_start_ordered_extent(inode, ordered, 1);
4000 btrfs_put_ordered_extent(ordered);
4001 goto again;
4002 }
4003 set_page_extent_mapped(page);
4004
Zheng Yan1a40e232008-09-26 10:09:34 -04004005 if (i == first_index)
4006 set_extent_bits(io_tree, page_start, page_end,
4007 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05004008 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04004009
Chris Masona061fc82008-05-07 11:43:44 -04004010 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04004011 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05004012
Chris Masond1310b22008-01-24 16:13:08 -05004013 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05004014 unlock_page(page);
4015 page_cache_release(page);
4016 }
4017
4018out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04004019 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05004020 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04004021 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04004022 return ret;
4023}
4024
Chris Masond3977122009-01-05 21:25:51 -05004025static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04004026 struct btrfs_key *extent_key,
4027 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05004028{
Zheng Yan1a40e232008-09-26 10:09:34 -04004029 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4030 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4031 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04004032 u64 start = extent_key->objectid - offset;
4033 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004034
4035 em = alloc_extent_map(GFP_NOFS);
4036 BUG_ON(!em || IS_ERR(em));
4037
Yan Zheng66435582008-10-30 14:19:50 -04004038 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004039 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004040 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004041 em->block_start = extent_key->objectid;
4042 em->bdev = root->fs_info->fs_devices->latest_bdev;
4043 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4044
4045 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04004046 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004047 while (1) {
4048 int ret;
4049 spin_lock(&em_tree->lock);
4050 ret = add_extent_mapping(em_tree, em);
4051 spin_unlock(&em_tree->lock);
4052 if (ret != -EEXIST) {
4053 free_extent_map(em);
4054 break;
4055 }
Yan Zheng66435582008-10-30 14:19:50 -04004056 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004057 }
Yan Zheng66435582008-10-30 14:19:50 -04004058 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004059
Yan Zheng66435582008-10-30 14:19:50 -04004060 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04004061}
4062
4063struct btrfs_ref_path {
4064 u64 extent_start;
4065 u64 nodes[BTRFS_MAX_LEVEL];
4066 u64 root_objectid;
4067 u64 root_generation;
4068 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004069 u32 num_refs;
4070 int lowest_level;
4071 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004072 int shared_level;
4073
4074 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4075 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04004076};
4077
4078struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04004079 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04004080 u64 disk_bytenr;
4081 u64 disk_num_bytes;
4082 u64 offset;
4083 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04004084 u8 compression;
4085 u8 encryption;
4086 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04004087};
4088
4089static int is_cowonly_root(u64 root_objectid)
4090{
4091 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4092 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4093 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4094 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05004095 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4096 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04004097 return 1;
4098 return 0;
4099}
4100
Chris Masond3977122009-01-05 21:25:51 -05004101static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004102 struct btrfs_root *extent_root,
4103 struct btrfs_ref_path *ref_path,
4104 int first_time)
4105{
4106 struct extent_buffer *leaf;
4107 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05004108 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05004109 struct btrfs_key key;
4110 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04004111 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05004112 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04004113 int level;
4114 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05004115
Zheng Yan1a40e232008-09-26 10:09:34 -04004116 path = btrfs_alloc_path();
4117 if (!path)
4118 return -ENOMEM;
4119
Zheng Yan1a40e232008-09-26 10:09:34 -04004120 if (first_time) {
4121 ref_path->lowest_level = -1;
4122 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004123 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004124 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04004125 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004126walk_down:
4127 level = ref_path->current_level - 1;
4128 while (level >= -1) {
4129 u64 parent;
4130 if (level < ref_path->lowest_level)
4131 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004132
Chris Masond3977122009-01-05 21:25:51 -05004133 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04004134 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05004135 else
Zheng Yan1a40e232008-09-26 10:09:34 -04004136 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004137 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004138
Zheng Yan1a40e232008-09-26 10:09:34 -04004139 parent = ref_path->nodes[level + 1];
4140 ref_path->nodes[level + 1] = 0;
4141 ref_path->current_level = level;
4142 BUG_ON(parent == 0);
4143
4144 key.objectid = bytenr;
4145 key.offset = parent + 1;
4146 key.type = BTRFS_EXTENT_REF_KEY;
4147
4148 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004149 if (ret < 0)
4150 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004151 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004152
Chris Masonedbd8d42007-12-21 16:27:24 -05004153 leaf = path->nodes[0];
4154 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04004155 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04004156 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04004157 if (ret < 0)
4158 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004159 if (ret > 0)
4160 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04004161 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04004162 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004163
4164 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04004165 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04004166 found_key.type == BTRFS_EXTENT_REF_KEY) {
4167 if (level < ref_path->shared_level)
4168 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004169 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004170 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004171next:
4172 level--;
4173 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004174 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004175 }
4176 /* reached lowest level */
4177 ret = 1;
4178 goto out;
4179walk_up:
4180 level = ref_path->current_level;
4181 while (level < BTRFS_MAX_LEVEL - 1) {
4182 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05004183
4184 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04004185 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05004186 else
Zheng Yan1a40e232008-09-26 10:09:34 -04004187 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05004188
Zheng Yan1a40e232008-09-26 10:09:34 -04004189 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004190
Zheng Yan1a40e232008-09-26 10:09:34 -04004191 key.objectid = bytenr;
4192 key.offset = 0;
4193 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05004194
Zheng Yan1a40e232008-09-26 10:09:34 -04004195 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4196 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05004197 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004198
4199 leaf = path->nodes[0];
4200 nritems = btrfs_header_nritems(leaf);
4201 if (path->slots[0] >= nritems) {
4202 ret = btrfs_next_leaf(extent_root, path);
4203 if (ret < 0)
4204 goto out;
4205 if (ret > 0) {
4206 /* the extent was freed by someone */
4207 if (ref_path->lowest_level == level)
4208 goto out;
4209 btrfs_release_path(extent_root, path);
4210 goto walk_down;
4211 }
4212 leaf = path->nodes[0];
4213 }
4214
4215 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4216 if (found_key.objectid != bytenr ||
4217 found_key.type != BTRFS_EXTENT_REF_KEY) {
4218 /* the extent was freed by someone */
4219 if (ref_path->lowest_level == level) {
4220 ret = 1;
4221 goto out;
4222 }
4223 btrfs_release_path(extent_root, path);
4224 goto walk_down;
4225 }
4226found:
4227 ref = btrfs_item_ptr(leaf, path->slots[0],
4228 struct btrfs_extent_ref);
4229 ref_objectid = btrfs_ref_objectid(leaf, ref);
4230 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4231 if (first_time) {
4232 level = (int)ref_objectid;
4233 BUG_ON(level >= BTRFS_MAX_LEVEL);
4234 ref_path->lowest_level = level;
4235 ref_path->current_level = level;
4236 ref_path->nodes[level] = bytenr;
4237 } else {
4238 WARN_ON(ref_objectid != level);
4239 }
4240 } else {
4241 WARN_ON(level != -1);
4242 }
4243 first_time = 0;
4244
4245 if (ref_path->lowest_level == level) {
4246 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004247 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4248 }
4249
4250 /*
4251 * the block is tree root or the block isn't in reference
4252 * counted tree.
4253 */
4254 if (found_key.objectid == found_key.offset ||
4255 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4256 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4257 ref_path->root_generation =
4258 btrfs_ref_generation(leaf, ref);
4259 if (level < 0) {
4260 /* special reference from the tree log */
4261 ref_path->nodes[0] = found_key.offset;
4262 ref_path->current_level = 0;
4263 }
4264 ret = 0;
4265 goto out;
4266 }
4267
4268 level++;
4269 BUG_ON(ref_path->nodes[level] != 0);
4270 ref_path->nodes[level] = found_key.offset;
4271 ref_path->current_level = level;
4272
4273 /*
4274 * the reference was created in the running transaction,
4275 * no need to continue walking up.
4276 */
4277 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4278 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4279 ref_path->root_generation =
4280 btrfs_ref_generation(leaf, ref);
4281 ret = 0;
4282 goto out;
4283 }
4284
4285 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004286 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004287 }
4288 /* reached max tree level, but no tree root found. */
4289 BUG();
4290out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004291 btrfs_free_path(path);
4292 return ret;
4293}
4294
4295static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4296 struct btrfs_root *extent_root,
4297 struct btrfs_ref_path *ref_path,
4298 u64 extent_start)
4299{
4300 memset(ref_path, 0, sizeof(*ref_path));
4301 ref_path->extent_start = extent_start;
4302
4303 return __next_ref_path(trans, extent_root, ref_path, 1);
4304}
4305
4306static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4307 struct btrfs_root *extent_root,
4308 struct btrfs_ref_path *ref_path)
4309{
4310 return __next_ref_path(trans, extent_root, ref_path, 0);
4311}
4312
Chris Masond3977122009-01-05 21:25:51 -05004313static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04004314 struct btrfs_key *extent_key,
4315 u64 offset, int no_fragment,
4316 struct disk_extent **extents,
4317 int *nr_extents)
4318{
4319 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4320 struct btrfs_path *path;
4321 struct btrfs_file_extent_item *fi;
4322 struct extent_buffer *leaf;
4323 struct disk_extent *exts = *extents;
4324 struct btrfs_key found_key;
4325 u64 cur_pos;
4326 u64 last_byte;
4327 u32 nritems;
4328 int nr = 0;
4329 int max = *nr_extents;
4330 int ret;
4331
4332 WARN_ON(!no_fragment && *extents);
4333 if (!exts) {
4334 max = 1;
4335 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4336 if (!exts)
4337 return -ENOMEM;
4338 }
4339
4340 path = btrfs_alloc_path();
4341 BUG_ON(!path);
4342
4343 cur_pos = extent_key->objectid - offset;
4344 last_byte = extent_key->objectid + extent_key->offset;
4345 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4346 cur_pos, 0);
4347 if (ret < 0)
4348 goto out;
4349 if (ret > 0) {
4350 ret = -ENOENT;
4351 goto out;
4352 }
4353
4354 while (1) {
4355 leaf = path->nodes[0];
4356 nritems = btrfs_header_nritems(leaf);
4357 if (path->slots[0] >= nritems) {
4358 ret = btrfs_next_leaf(root, path);
4359 if (ret < 0)
4360 goto out;
4361 if (ret > 0)
4362 break;
4363 leaf = path->nodes[0];
4364 }
4365
4366 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4367 if (found_key.offset != cur_pos ||
4368 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4369 found_key.objectid != reloc_inode->i_ino)
4370 break;
4371
4372 fi = btrfs_item_ptr(leaf, path->slots[0],
4373 struct btrfs_file_extent_item);
4374 if (btrfs_file_extent_type(leaf, fi) !=
4375 BTRFS_FILE_EXTENT_REG ||
4376 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4377 break;
4378
4379 if (nr == max) {
4380 struct disk_extent *old = exts;
4381 max *= 2;
4382 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4383 memcpy(exts, old, sizeof(*exts) * nr);
4384 if (old != *extents)
4385 kfree(old);
4386 }
4387
4388 exts[nr].disk_bytenr =
4389 btrfs_file_extent_disk_bytenr(leaf, fi);
4390 exts[nr].disk_num_bytes =
4391 btrfs_file_extent_disk_num_bytes(leaf, fi);
4392 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4393 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04004394 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4395 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4396 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4397 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4398 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04004399 BUG_ON(exts[nr].offset > 0);
4400 BUG_ON(exts[nr].compression || exts[nr].encryption);
4401 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004402
4403 cur_pos += exts[nr].num_bytes;
4404 nr++;
4405
4406 if (cur_pos + offset >= last_byte)
4407 break;
4408
4409 if (no_fragment) {
4410 ret = 1;
4411 goto out;
4412 }
4413 path->slots[0]++;
4414 }
4415
Yan Zheng1f80e4d2008-12-19 10:59:04 -05004416 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04004417 if (cur_pos + offset < last_byte) {
4418 ret = -ENOENT;
4419 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05004420 }
4421 ret = 0;
4422out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004423 btrfs_free_path(path);
4424 if (ret) {
4425 if (exts != *extents)
4426 kfree(exts);
4427 } else {
4428 *extents = exts;
4429 *nr_extents = nr;
4430 }
4431 return ret;
4432}
4433
Chris Masond3977122009-01-05 21:25:51 -05004434static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004435 struct btrfs_root *root,
4436 struct btrfs_path *path,
4437 struct btrfs_key *extent_key,
4438 struct btrfs_key *leaf_key,
4439 struct btrfs_ref_path *ref_path,
4440 struct disk_extent *new_extents,
4441 int nr_extents)
4442{
4443 struct extent_buffer *leaf;
4444 struct btrfs_file_extent_item *fi;
4445 struct inode *inode = NULL;
4446 struct btrfs_key key;
4447 u64 lock_start = 0;
4448 u64 lock_end = 0;
4449 u64 num_bytes;
4450 u64 ext_offset;
4451 u64 first_pos;
4452 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004453 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004454 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04004455 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04004456 int ret;
4457
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004458 memcpy(&key, leaf_key, sizeof(key));
4459 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004460 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004461 if (key.objectid < ref_path->owner_objectid ||
4462 (key.objectid == ref_path->owner_objectid &&
4463 key.type < BTRFS_EXTENT_DATA_KEY)) {
4464 key.objectid = ref_path->owner_objectid;
4465 key.type = BTRFS_EXTENT_DATA_KEY;
4466 key.offset = 0;
4467 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004468 }
4469
4470 while (1) {
4471 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4472 if (ret < 0)
4473 goto out;
4474
4475 leaf = path->nodes[0];
4476 nritems = btrfs_header_nritems(leaf);
4477next:
4478 if (extent_locked && ret > 0) {
4479 /*
4480 * the file extent item was modified by someone
4481 * before the extent got locked.
4482 */
Zheng Yan1a40e232008-09-26 10:09:34 -04004483 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4484 lock_end, GFP_NOFS);
4485 extent_locked = 0;
4486 }
4487
4488 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004489 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04004490 break;
4491
4492 BUG_ON(extent_locked);
4493 ret = btrfs_next_leaf(root, path);
4494 if (ret < 0)
4495 goto out;
4496 if (ret > 0)
4497 break;
4498 leaf = path->nodes[0];
4499 nritems = btrfs_header_nritems(leaf);
4500 }
4501
4502 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4503
4504 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4505 if ((key.objectid > ref_path->owner_objectid) ||
4506 (key.objectid == ref_path->owner_objectid &&
4507 key.type > BTRFS_EXTENT_DATA_KEY) ||
4508 (key.offset >= first_pos + extent_key->offset))
4509 break;
4510 }
4511
4512 if (inode && key.objectid != inode->i_ino) {
4513 BUG_ON(extent_locked);
4514 btrfs_release_path(root, path);
4515 mutex_unlock(&inode->i_mutex);
4516 iput(inode);
4517 inode = NULL;
4518 continue;
4519 }
4520
4521 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4522 path->slots[0]++;
4523 ret = 1;
4524 goto next;
4525 }
4526 fi = btrfs_item_ptr(leaf, path->slots[0],
4527 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04004528 extent_type = btrfs_file_extent_type(leaf, fi);
4529 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4530 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04004531 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4532 extent_key->objectid)) {
4533 path->slots[0]++;
4534 ret = 1;
4535 goto next;
4536 }
4537
4538 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4539 ext_offset = btrfs_file_extent_offset(leaf, fi);
4540
4541 if (first_pos > key.offset - ext_offset)
4542 first_pos = key.offset - ext_offset;
4543
4544 if (!extent_locked) {
4545 lock_start = key.offset;
4546 lock_end = lock_start + num_bytes - 1;
4547 } else {
Yan Zheng66435582008-10-30 14:19:50 -04004548 if (lock_start > key.offset ||
4549 lock_end + 1 < key.offset + num_bytes) {
4550 unlock_extent(&BTRFS_I(inode)->io_tree,
4551 lock_start, lock_end, GFP_NOFS);
4552 extent_locked = 0;
4553 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004554 }
4555
4556 if (!inode) {
4557 btrfs_release_path(root, path);
4558
4559 inode = btrfs_iget_locked(root->fs_info->sb,
4560 key.objectid, root);
4561 if (inode->i_state & I_NEW) {
4562 BTRFS_I(inode)->root = root;
4563 BTRFS_I(inode)->location.objectid =
4564 key.objectid;
4565 BTRFS_I(inode)->location.type =
4566 BTRFS_INODE_ITEM_KEY;
4567 BTRFS_I(inode)->location.offset = 0;
4568 btrfs_read_locked_inode(inode);
4569 unlock_new_inode(inode);
4570 }
4571 /*
4572 * some code call btrfs_commit_transaction while
4573 * holding the i_mutex, so we can't use mutex_lock
4574 * here.
4575 */
4576 if (is_bad_inode(inode) ||
4577 !mutex_trylock(&inode->i_mutex)) {
4578 iput(inode);
4579 inode = NULL;
4580 key.offset = (u64)-1;
4581 goto skip;
4582 }
4583 }
4584
4585 if (!extent_locked) {
4586 struct btrfs_ordered_extent *ordered;
4587
4588 btrfs_release_path(root, path);
4589
4590 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4591 lock_end, GFP_NOFS);
4592 ordered = btrfs_lookup_first_ordered_extent(inode,
4593 lock_end);
4594 if (ordered &&
4595 ordered->file_offset <= lock_end &&
4596 ordered->file_offset + ordered->len > lock_start) {
4597 unlock_extent(&BTRFS_I(inode)->io_tree,
4598 lock_start, lock_end, GFP_NOFS);
4599 btrfs_start_ordered_extent(inode, ordered, 1);
4600 btrfs_put_ordered_extent(ordered);
4601 key.offset += num_bytes;
4602 goto skip;
4603 }
4604 if (ordered)
4605 btrfs_put_ordered_extent(ordered);
4606
Zheng Yan1a40e232008-09-26 10:09:34 -04004607 extent_locked = 1;
4608 continue;
4609 }
4610
4611 if (nr_extents == 1) {
4612 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04004613 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4614 new_extents[0].disk_bytenr);
4615 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4616 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004617 btrfs_mark_buffer_dirty(leaf);
4618
4619 btrfs_drop_extent_cache(inode, key.offset,
4620 key.offset + num_bytes - 1, 0);
4621
4622 ret = btrfs_inc_extent_ref(trans, root,
4623 new_extents[0].disk_bytenr,
4624 new_extents[0].disk_num_bytes,
4625 leaf->start,
4626 root->root_key.objectid,
4627 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004628 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004629 BUG_ON(ret);
4630
4631 ret = btrfs_free_extent(trans, root,
4632 extent_key->objectid,
4633 extent_key->offset,
4634 leaf->start,
4635 btrfs_header_owner(leaf),
4636 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004637 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004638 BUG_ON(ret);
4639
4640 btrfs_release_path(root, path);
4641 key.offset += num_bytes;
4642 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04004643 BUG_ON(1);
4644#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04004645 u64 alloc_hint;
4646 u64 extent_len;
4647 int i;
4648 /*
4649 * drop old extent pointer at first, then insert the
4650 * new pointers one bye one
4651 */
4652 btrfs_release_path(root, path);
4653 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4654 key.offset + num_bytes,
4655 key.offset, &alloc_hint);
4656 BUG_ON(ret);
4657
4658 for (i = 0; i < nr_extents; i++) {
4659 if (ext_offset >= new_extents[i].num_bytes) {
4660 ext_offset -= new_extents[i].num_bytes;
4661 continue;
4662 }
4663 extent_len = min(new_extents[i].num_bytes -
4664 ext_offset, num_bytes);
4665
4666 ret = btrfs_insert_empty_item(trans, root,
4667 path, &key,
4668 sizeof(*fi));
4669 BUG_ON(ret);
4670
4671 leaf = path->nodes[0];
4672 fi = btrfs_item_ptr(leaf, path->slots[0],
4673 struct btrfs_file_extent_item);
4674 btrfs_set_file_extent_generation(leaf, fi,
4675 trans->transid);
4676 btrfs_set_file_extent_type(leaf, fi,
4677 BTRFS_FILE_EXTENT_REG);
4678 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4679 new_extents[i].disk_bytenr);
4680 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4681 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004682 btrfs_set_file_extent_ram_bytes(leaf, fi,
4683 new_extents[i].ram_bytes);
4684
4685 btrfs_set_file_extent_compression(leaf, fi,
4686 new_extents[i].compression);
4687 btrfs_set_file_extent_encryption(leaf, fi,
4688 new_extents[i].encryption);
4689 btrfs_set_file_extent_other_encoding(leaf, fi,
4690 new_extents[i].other_encoding);
4691
Zheng Yan1a40e232008-09-26 10:09:34 -04004692 btrfs_set_file_extent_num_bytes(leaf, fi,
4693 extent_len);
4694 ext_offset += new_extents[i].offset;
4695 btrfs_set_file_extent_offset(leaf, fi,
4696 ext_offset);
4697 btrfs_mark_buffer_dirty(leaf);
4698
4699 btrfs_drop_extent_cache(inode, key.offset,
4700 key.offset + extent_len - 1, 0);
4701
4702 ret = btrfs_inc_extent_ref(trans, root,
4703 new_extents[i].disk_bytenr,
4704 new_extents[i].disk_num_bytes,
4705 leaf->start,
4706 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004707 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004708 BUG_ON(ret);
4709 btrfs_release_path(root, path);
4710
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004711 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004712
4713 ext_offset = 0;
4714 num_bytes -= extent_len;
4715 key.offset += extent_len;
4716
4717 if (num_bytes == 0)
4718 break;
4719 }
4720 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004721#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04004722 }
4723
4724 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004725 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4726 lock_end, GFP_NOFS);
4727 extent_locked = 0;
4728 }
4729skip:
4730 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4731 key.offset >= first_pos + extent_key->offset)
4732 break;
4733
4734 cond_resched();
4735 }
4736 ret = 0;
4737out:
4738 btrfs_release_path(root, path);
4739 if (inode) {
4740 mutex_unlock(&inode->i_mutex);
4741 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004742 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4743 lock_end, GFP_NOFS);
4744 }
4745 iput(inode);
4746 }
4747 return ret;
4748}
4749
Zheng Yan1a40e232008-09-26 10:09:34 -04004750int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4751 struct btrfs_root *root,
4752 struct extent_buffer *buf, u64 orig_start)
4753{
4754 int level;
4755 int ret;
4756
4757 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4758 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4759
4760 level = btrfs_header_level(buf);
4761 if (level == 0) {
4762 struct btrfs_leaf_ref *ref;
4763 struct btrfs_leaf_ref *orig_ref;
4764
4765 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4766 if (!orig_ref)
4767 return -ENOENT;
4768
4769 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4770 if (!ref) {
4771 btrfs_free_leaf_ref(root, orig_ref);
4772 return -ENOMEM;
4773 }
4774
4775 ref->nritems = orig_ref->nritems;
4776 memcpy(ref->extents, orig_ref->extents,
4777 sizeof(ref->extents[0]) * ref->nritems);
4778
4779 btrfs_free_leaf_ref(root, orig_ref);
4780
4781 ref->root_gen = trans->transid;
4782 ref->bytenr = buf->start;
4783 ref->owner = btrfs_header_owner(buf);
4784 ref->generation = btrfs_header_generation(buf);
4785 ret = btrfs_add_leaf_ref(root, ref, 0);
4786 WARN_ON(ret);
4787 btrfs_free_leaf_ref(root, ref);
4788 }
4789 return 0;
4790}
4791
Chris Masond3977122009-01-05 21:25:51 -05004792static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04004793 struct extent_buffer *leaf,
4794 struct btrfs_block_group_cache *group,
4795 struct btrfs_root *target_root)
4796{
4797 struct btrfs_key key;
4798 struct inode *inode = NULL;
4799 struct btrfs_file_extent_item *fi;
4800 u64 num_bytes;
4801 u64 skip_objectid = 0;
4802 u32 nritems;
4803 u32 i;
4804
4805 nritems = btrfs_header_nritems(leaf);
4806 for (i = 0; i < nritems; i++) {
4807 btrfs_item_key_to_cpu(leaf, &key, i);
4808 if (key.objectid == skip_objectid ||
4809 key.type != BTRFS_EXTENT_DATA_KEY)
4810 continue;
4811 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4812 if (btrfs_file_extent_type(leaf, fi) ==
4813 BTRFS_FILE_EXTENT_INLINE)
4814 continue;
4815 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4816 continue;
4817 if (!inode || inode->i_ino != key.objectid) {
4818 iput(inode);
4819 inode = btrfs_ilookup(target_root->fs_info->sb,
4820 key.objectid, target_root, 1);
4821 }
4822 if (!inode) {
4823 skip_objectid = key.objectid;
4824 continue;
4825 }
4826 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4827
4828 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4829 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004830 btrfs_drop_extent_cache(inode, key.offset,
4831 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004832 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4833 key.offset + num_bytes - 1, GFP_NOFS);
4834 cond_resched();
4835 }
4836 iput(inode);
4837 return 0;
4838}
4839
Chris Masond3977122009-01-05 21:25:51 -05004840static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004841 struct btrfs_root *root,
4842 struct extent_buffer *leaf,
4843 struct btrfs_block_group_cache *group,
4844 struct inode *reloc_inode)
4845{
4846 struct btrfs_key key;
4847 struct btrfs_key extent_key;
4848 struct btrfs_file_extent_item *fi;
4849 struct btrfs_leaf_ref *ref;
4850 struct disk_extent *new_extent;
4851 u64 bytenr;
4852 u64 num_bytes;
4853 u32 nritems;
4854 u32 i;
4855 int ext_index;
4856 int nr_extent;
4857 int ret;
4858
4859 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4860 BUG_ON(!new_extent);
4861
4862 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4863 BUG_ON(!ref);
4864
4865 ext_index = -1;
4866 nritems = btrfs_header_nritems(leaf);
4867 for (i = 0; i < nritems; i++) {
4868 btrfs_item_key_to_cpu(leaf, &key, i);
4869 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4870 continue;
4871 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4872 if (btrfs_file_extent_type(leaf, fi) ==
4873 BTRFS_FILE_EXTENT_INLINE)
4874 continue;
4875 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4876 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4877 if (bytenr == 0)
4878 continue;
4879
4880 ext_index++;
4881 if (bytenr >= group->key.objectid + group->key.offset ||
4882 bytenr + num_bytes <= group->key.objectid)
4883 continue;
4884
4885 extent_key.objectid = bytenr;
4886 extent_key.offset = num_bytes;
4887 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4888 nr_extent = 1;
4889 ret = get_new_locations(reloc_inode, &extent_key,
4890 group->key.objectid, 1,
4891 &new_extent, &nr_extent);
4892 if (ret > 0)
4893 continue;
4894 BUG_ON(ret < 0);
4895
4896 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4897 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4898 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4899 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4900
Zheng Yan1a40e232008-09-26 10:09:34 -04004901 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4902 new_extent->disk_bytenr);
4903 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4904 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004905 btrfs_mark_buffer_dirty(leaf);
4906
4907 ret = btrfs_inc_extent_ref(trans, root,
4908 new_extent->disk_bytenr,
4909 new_extent->disk_num_bytes,
4910 leaf->start,
4911 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004912 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004913 BUG_ON(ret);
4914 ret = btrfs_free_extent(trans, root,
4915 bytenr, num_bytes, leaf->start,
4916 btrfs_header_owner(leaf),
4917 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004918 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004919 BUG_ON(ret);
4920 cond_resched();
4921 }
4922 kfree(new_extent);
4923 BUG_ON(ext_index + 1 != ref->nritems);
4924 btrfs_free_leaf_ref(root, ref);
4925 return 0;
4926}
4927
Yan Zhengf82d02d2008-10-29 14:49:05 -04004928int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4929 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004930{
4931 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004932 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004933
4934 if (root->reloc_root) {
4935 reloc_root = root->reloc_root;
4936 root->reloc_root = NULL;
4937 list_add(&reloc_root->dead_list,
4938 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004939
4940 btrfs_set_root_bytenr(&reloc_root->root_item,
4941 reloc_root->node->start);
4942 btrfs_set_root_level(&root->root_item,
4943 btrfs_header_level(reloc_root->node));
4944 memset(&reloc_root->root_item.drop_progress, 0,
4945 sizeof(struct btrfs_disk_key));
4946 reloc_root->root_item.drop_level = 0;
4947
4948 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4949 &reloc_root->root_key,
4950 &reloc_root->root_item);
4951 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004952 }
4953 return 0;
4954}
4955
4956int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4957{
4958 struct btrfs_trans_handle *trans;
4959 struct btrfs_root *reloc_root;
4960 struct btrfs_root *prev_root = NULL;
4961 struct list_head dead_roots;
4962 int ret;
4963 unsigned long nr;
4964
4965 INIT_LIST_HEAD(&dead_roots);
4966 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4967
4968 while (!list_empty(&dead_roots)) {
4969 reloc_root = list_entry(dead_roots.prev,
4970 struct btrfs_root, dead_list);
4971 list_del_init(&reloc_root->dead_list);
4972
4973 BUG_ON(reloc_root->commit_root != NULL);
4974 while (1) {
4975 trans = btrfs_join_transaction(root, 1);
4976 BUG_ON(!trans);
4977
4978 mutex_lock(&root->fs_info->drop_mutex);
4979 ret = btrfs_drop_snapshot(trans, reloc_root);
4980 if (ret != -EAGAIN)
4981 break;
4982 mutex_unlock(&root->fs_info->drop_mutex);
4983
4984 nr = trans->blocks_used;
4985 ret = btrfs_end_transaction(trans, root);
4986 BUG_ON(ret);
4987 btrfs_btree_balance_dirty(root, nr);
4988 }
4989
4990 free_extent_buffer(reloc_root->node);
4991
4992 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4993 &reloc_root->root_key);
4994 BUG_ON(ret);
4995 mutex_unlock(&root->fs_info->drop_mutex);
4996
4997 nr = trans->blocks_used;
4998 ret = btrfs_end_transaction(trans, root);
4999 BUG_ON(ret);
5000 btrfs_btree_balance_dirty(root, nr);
5001
5002 kfree(prev_root);
5003 prev_root = reloc_root;
5004 }
5005 if (prev_root) {
5006 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
5007 kfree(prev_root);
5008 }
5009 return 0;
5010}
5011
5012int btrfs_add_dead_reloc_root(struct btrfs_root *root)
5013{
5014 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
5015 return 0;
5016}
5017
5018int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
5019{
5020 struct btrfs_root *reloc_root;
5021 struct btrfs_trans_handle *trans;
5022 struct btrfs_key location;
5023 int found;
5024 int ret;
5025
5026 mutex_lock(&root->fs_info->tree_reloc_mutex);
5027 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
5028 BUG_ON(ret);
5029 found = !list_empty(&root->fs_info->dead_reloc_roots);
5030 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5031
5032 if (found) {
5033 trans = btrfs_start_transaction(root, 1);
5034 BUG_ON(!trans);
5035 ret = btrfs_commit_transaction(trans, root);
5036 BUG_ON(ret);
5037 }
5038
5039 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5040 location.offset = (u64)-1;
5041 location.type = BTRFS_ROOT_ITEM_KEY;
5042
5043 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5044 BUG_ON(!reloc_root);
5045 btrfs_orphan_cleanup(reloc_root);
5046 return 0;
5047}
5048
Chris Masond3977122009-01-05 21:25:51 -05005049static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005050 struct btrfs_root *root)
5051{
5052 struct btrfs_root *reloc_root;
5053 struct extent_buffer *eb;
5054 struct btrfs_root_item *root_item;
5055 struct btrfs_key root_key;
5056 int ret;
5057
5058 BUG_ON(!root->ref_cows);
5059 if (root->reloc_root)
5060 return 0;
5061
5062 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5063 BUG_ON(!root_item);
5064
5065 ret = btrfs_copy_root(trans, root, root->commit_root,
5066 &eb, BTRFS_TREE_RELOC_OBJECTID);
5067 BUG_ON(ret);
5068
5069 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5070 root_key.offset = root->root_key.objectid;
5071 root_key.type = BTRFS_ROOT_ITEM_KEY;
5072
5073 memcpy(root_item, &root->root_item, sizeof(root_item));
5074 btrfs_set_root_refs(root_item, 0);
5075 btrfs_set_root_bytenr(root_item, eb->start);
5076 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04005077 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005078
5079 btrfs_tree_unlock(eb);
5080 free_extent_buffer(eb);
5081
5082 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5083 &root_key, root_item);
5084 BUG_ON(ret);
5085 kfree(root_item);
5086
5087 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5088 &root_key);
5089 BUG_ON(!reloc_root);
5090 reloc_root->last_trans = trans->transid;
5091 reloc_root->commit_root = NULL;
5092 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5093
5094 root->reloc_root = reloc_root;
5095 return 0;
5096}
5097
5098/*
5099 * Core function of space balance.
5100 *
5101 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04005102 * counted roots. There is one reloc tree for each subvol, and all
5103 * reloc trees share same root key objectid. Reloc trees are snapshots
5104 * of the latest committed roots of subvols (root->commit_root).
5105 *
5106 * To relocate a tree block referenced by a subvol, there are two steps.
5107 * COW the block through subvol's reloc tree, then update block pointer
5108 * in the subvol to point to the new block. Since all reloc trees share
5109 * same root key objectid, doing special handing for tree blocks owned
5110 * by them is easy. Once a tree block has been COWed in one reloc tree,
5111 * we can use the resulting new block directly when the same block is
5112 * required to COW again through other reloc trees. By this way, relocated
5113 * tree blocks are shared between reloc trees, so they are also shared
5114 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04005115 */
Chris Masond3977122009-01-05 21:25:51 -05005116static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005117 struct btrfs_root *root,
5118 struct btrfs_path *path,
5119 struct btrfs_key *first_key,
5120 struct btrfs_ref_path *ref_path,
5121 struct btrfs_block_group_cache *group,
5122 struct inode *reloc_inode)
5123{
5124 struct btrfs_root *reloc_root;
5125 struct extent_buffer *eb = NULL;
5126 struct btrfs_key *keys;
5127 u64 *nodes;
5128 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005129 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005130 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005131 int ret;
5132
5133 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5134 lowest_level = ref_path->owner_objectid;
5135
Yan Zhengf82d02d2008-10-29 14:49:05 -04005136 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005137 path->lowest_level = lowest_level;
5138 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5139 BUG_ON(ret < 0);
5140 path->lowest_level = 0;
5141 btrfs_release_path(root, path);
5142 return 0;
5143 }
5144
Zheng Yan1a40e232008-09-26 10:09:34 -04005145 mutex_lock(&root->fs_info->tree_reloc_mutex);
5146 ret = init_reloc_tree(trans, root);
5147 BUG_ON(ret);
5148 reloc_root = root->reloc_root;
5149
Yan Zhengf82d02d2008-10-29 14:49:05 -04005150 shared_level = ref_path->shared_level;
5151 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005152
Yan Zhengf82d02d2008-10-29 14:49:05 -04005153 keys = ref_path->node_keys;
5154 nodes = ref_path->new_nodes;
5155 memset(&keys[shared_level + 1], 0,
5156 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5157 memset(&nodes[shared_level + 1], 0,
5158 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04005159
Yan Zhengf82d02d2008-10-29 14:49:05 -04005160 if (nodes[lowest_level] == 0) {
5161 path->lowest_level = lowest_level;
5162 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5163 0, 1);
5164 BUG_ON(ret);
5165 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5166 eb = path->nodes[level];
5167 if (!eb || eb == reloc_root->node)
5168 break;
5169 nodes[level] = eb->start;
5170 if (level == 0)
5171 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5172 else
5173 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5174 }
Yan Zheng2b820322008-11-17 21:11:30 -05005175 if (nodes[0] &&
5176 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005177 eb = path->nodes[0];
5178 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5179 group, reloc_inode);
5180 BUG_ON(ret);
5181 }
5182 btrfs_release_path(reloc_root, path);
5183 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005184 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005185 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04005186 BUG_ON(ret);
5187 }
5188
Zheng Yan1a40e232008-09-26 10:09:34 -04005189 /*
5190 * replace tree blocks in the fs tree with tree blocks in
5191 * the reloc tree.
5192 */
5193 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5194 BUG_ON(ret < 0);
5195
5196 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005197 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5198 0, 0);
5199 BUG_ON(ret);
5200 extent_buffer_get(path->nodes[0]);
5201 eb = path->nodes[0];
5202 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005203 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5204 BUG_ON(ret);
5205 free_extent_buffer(eb);
5206 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005207
Yan Zhengf82d02d2008-10-29 14:49:05 -04005208 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005209 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005210 return 0;
5211}
5212
Chris Masond3977122009-01-05 21:25:51 -05005213static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005214 struct btrfs_root *root,
5215 struct btrfs_path *path,
5216 struct btrfs_key *first_key,
5217 struct btrfs_ref_path *ref_path)
5218{
5219 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005220
5221 ret = relocate_one_path(trans, root, path, first_key,
5222 ref_path, NULL, NULL);
5223 BUG_ON(ret);
5224
5225 if (root == root->fs_info->extent_root)
5226 btrfs_extent_post_op(trans, root);
Zheng Yan1a40e232008-09-26 10:09:34 -04005227
5228 return 0;
5229}
5230
Chris Masond3977122009-01-05 21:25:51 -05005231static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005232 struct btrfs_root *extent_root,
5233 struct btrfs_path *path,
5234 struct btrfs_key *extent_key)
5235{
5236 int ret;
5237
Zheng Yan1a40e232008-09-26 10:09:34 -04005238 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5239 if (ret)
5240 goto out;
5241 ret = btrfs_del_item(trans, extent_root, path);
5242out:
Chris Masonedbd8d42007-12-21 16:27:24 -05005243 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005244 return ret;
5245}
5246
Chris Masond3977122009-01-05 21:25:51 -05005247static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04005248 struct btrfs_ref_path *ref_path)
5249{
5250 struct btrfs_key root_key;
5251
5252 root_key.objectid = ref_path->root_objectid;
5253 root_key.type = BTRFS_ROOT_ITEM_KEY;
5254 if (is_cowonly_root(ref_path->root_objectid))
5255 root_key.offset = 0;
5256 else
5257 root_key.offset = (u64)-1;
5258
5259 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5260}
5261
Chris Masond3977122009-01-05 21:25:51 -05005262static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04005263 struct btrfs_path *path,
5264 struct btrfs_key *extent_key,
5265 struct btrfs_block_group_cache *group,
5266 struct inode *reloc_inode, int pass)
5267{
5268 struct btrfs_trans_handle *trans;
5269 struct btrfs_root *found_root;
5270 struct btrfs_ref_path *ref_path = NULL;
5271 struct disk_extent *new_extents = NULL;
5272 int nr_extents = 0;
5273 int loops;
5274 int ret;
5275 int level;
5276 struct btrfs_key first_key;
5277 u64 prev_block = 0;
5278
Zheng Yan1a40e232008-09-26 10:09:34 -04005279
5280 trans = btrfs_start_transaction(extent_root, 1);
5281 BUG_ON(!trans);
5282
5283 if (extent_key->objectid == 0) {
5284 ret = del_extent_zero(trans, extent_root, path, extent_key);
5285 goto out;
5286 }
5287
5288 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5289 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05005290 ret = -ENOMEM;
5291 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005292 }
5293
5294 for (loops = 0; ; loops++) {
5295 if (loops == 0) {
5296 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5297 extent_key->objectid);
5298 } else {
5299 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5300 }
5301 if (ret < 0)
5302 goto out;
5303 if (ret > 0)
5304 break;
5305
5306 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5307 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5308 continue;
5309
5310 found_root = read_ref_root(extent_root->fs_info, ref_path);
5311 BUG_ON(!found_root);
5312 /*
5313 * for reference counted tree, only process reference paths
5314 * rooted at the latest committed root.
5315 */
5316 if (found_root->ref_cows &&
5317 ref_path->root_generation != found_root->root_key.offset)
5318 continue;
5319
5320 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5321 if (pass == 0) {
5322 /*
5323 * copy data extents to new locations
5324 */
5325 u64 group_start = group->key.objectid;
5326 ret = relocate_data_extent(reloc_inode,
5327 extent_key,
5328 group_start);
5329 if (ret < 0)
5330 goto out;
5331 break;
5332 }
5333 level = 0;
5334 } else {
5335 level = ref_path->owner_objectid;
5336 }
5337
5338 if (prev_block != ref_path->nodes[level]) {
5339 struct extent_buffer *eb;
5340 u64 block_start = ref_path->nodes[level];
5341 u64 block_size = btrfs_level_size(found_root, level);
5342
5343 eb = read_tree_block(found_root, block_start,
5344 block_size, 0);
5345 btrfs_tree_lock(eb);
5346 BUG_ON(level != btrfs_header_level(eb));
5347
5348 if (level == 0)
5349 btrfs_item_key_to_cpu(eb, &first_key, 0);
5350 else
5351 btrfs_node_key_to_cpu(eb, &first_key, 0);
5352
5353 btrfs_tree_unlock(eb);
5354 free_extent_buffer(eb);
5355 prev_block = block_start;
5356 }
5357
Yan Zhenge4404d62008-12-12 10:03:26 -05005358 btrfs_record_root_in_trans(found_root);
5359 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5360 /*
5361 * try to update data extent references while
5362 * keeping metadata shared between snapshots.
5363 */
5364 if (pass == 1) {
5365 ret = relocate_one_path(trans, found_root,
5366 path, &first_key, ref_path,
5367 group, reloc_inode);
5368 if (ret < 0)
5369 goto out;
5370 continue;
5371 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005372 /*
5373 * use fallback method to process the remaining
5374 * references.
5375 */
5376 if (!new_extents) {
5377 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04005378 new_extents = kmalloc(sizeof(*new_extents),
5379 GFP_NOFS);
5380 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005381 ret = get_new_locations(reloc_inode,
5382 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04005383 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04005384 &new_extents,
5385 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005386 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04005387 goto out;
5388 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005389 ret = replace_one_extent(trans, found_root,
5390 path, extent_key,
5391 &first_key, ref_path,
5392 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05005393 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005394 ret = relocate_tree_block(trans, found_root, path,
5395 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005396 }
5397 if (ret < 0)
5398 goto out;
5399 }
5400 ret = 0;
5401out:
5402 btrfs_end_transaction(trans, extent_root);
5403 kfree(new_extents);
5404 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005405 return ret;
5406}
5407
Chris Masonec44a352008-04-28 15:29:52 -04005408static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5409{
5410 u64 num_devices;
5411 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5412 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5413
Yan Zheng2b820322008-11-17 21:11:30 -05005414 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04005415 if (num_devices == 1) {
5416 stripped |= BTRFS_BLOCK_GROUP_DUP;
5417 stripped = flags & ~stripped;
5418
5419 /* turn raid0 into single device chunks */
5420 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5421 return stripped;
5422
5423 /* turn mirroring into duplication */
5424 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5425 BTRFS_BLOCK_GROUP_RAID10))
5426 return stripped | BTRFS_BLOCK_GROUP_DUP;
5427 return flags;
5428 } else {
5429 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04005430 if (flags & stripped)
5431 return flags;
5432
5433 stripped |= BTRFS_BLOCK_GROUP_DUP;
5434 stripped = flags & ~stripped;
5435
5436 /* switch duplicated blocks with raid1 */
5437 if (flags & BTRFS_BLOCK_GROUP_DUP)
5438 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5439
5440 /* turn single device chunks into raid0 */
5441 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5442 }
5443 return flags;
5444}
5445
Christoph Hellwigb2950862008-12-02 09:54:17 -05005446static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04005447 struct btrfs_block_group_cache *shrink_block_group,
5448 int force)
5449{
5450 struct btrfs_trans_handle *trans;
5451 u64 new_alloc_flags;
5452 u64 calc;
5453
Chris Masonc286ac42008-07-22 23:06:41 -04005454 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005455 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04005456 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005457
Chris Mason0ef3e662008-05-24 14:04:53 -04005458 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005459 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04005460
Chris Mason0ef3e662008-05-24 14:04:53 -04005461 new_alloc_flags = update_block_group_flags(root,
5462 shrink_block_group->flags);
5463 if (new_alloc_flags != shrink_block_group->flags) {
5464 calc =
5465 btrfs_block_group_used(&shrink_block_group->item);
5466 } else {
5467 calc = shrink_block_group->key.offset;
5468 }
Chris Masonc286ac42008-07-22 23:06:41 -04005469 spin_unlock(&shrink_block_group->lock);
5470
Chris Mason0ef3e662008-05-24 14:04:53 -04005471 do_chunk_alloc(trans, root->fs_info->extent_root,
5472 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04005473
Chris Mason0ef3e662008-05-24 14:04:53 -04005474 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04005475 } else
5476 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005477 return 0;
5478}
5479
Zheng Yan1a40e232008-09-26 10:09:34 -04005480static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5481 struct btrfs_root *root,
5482 u64 objectid, u64 size)
5483{
5484 struct btrfs_path *path;
5485 struct btrfs_inode_item *item;
5486 struct extent_buffer *leaf;
5487 int ret;
5488
5489 path = btrfs_alloc_path();
5490 if (!path)
5491 return -ENOMEM;
5492
5493 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5494 if (ret)
5495 goto out;
5496
5497 leaf = path->nodes[0];
5498 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5499 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5500 btrfs_set_inode_generation(leaf, item, 1);
5501 btrfs_set_inode_size(leaf, item, size);
5502 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zheng0403e472008-12-10 20:32:51 -05005503 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005504 btrfs_mark_buffer_dirty(leaf);
5505 btrfs_release_path(root, path);
5506out:
5507 btrfs_free_path(path);
5508 return ret;
5509}
5510
Chris Masond3977122009-01-05 21:25:51 -05005511static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04005512 struct btrfs_block_group_cache *group)
5513{
5514 struct inode *inode = NULL;
5515 struct btrfs_trans_handle *trans;
5516 struct btrfs_root *root;
5517 struct btrfs_key root_key;
5518 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5519 int err = 0;
5520
5521 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5522 root_key.type = BTRFS_ROOT_ITEM_KEY;
5523 root_key.offset = (u64)-1;
5524 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5525 if (IS_ERR(root))
5526 return ERR_CAST(root);
5527
5528 trans = btrfs_start_transaction(root, 1);
5529 BUG_ON(!trans);
5530
5531 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5532 if (err)
5533 goto out;
5534
5535 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5536 BUG_ON(err);
5537
5538 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04005539 group->key.offset, 0, group->key.offset,
5540 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005541 BUG_ON(err);
5542
5543 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5544 if (inode->i_state & I_NEW) {
5545 BTRFS_I(inode)->root = root;
5546 BTRFS_I(inode)->location.objectid = objectid;
5547 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5548 BTRFS_I(inode)->location.offset = 0;
5549 btrfs_read_locked_inode(inode);
5550 unlock_new_inode(inode);
5551 BUG_ON(is_bad_inode(inode));
5552 } else {
5553 BUG_ON(1);
5554 }
Yan Zheng17d217f2008-12-12 10:03:38 -05005555 BTRFS_I(inode)->index_cnt = group->key.objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005556
5557 err = btrfs_orphan_add(trans, inode);
5558out:
5559 btrfs_end_transaction(trans, root);
5560 if (err) {
5561 if (inode)
5562 iput(inode);
5563 inode = ERR_PTR(err);
5564 }
5565 return inode;
5566}
5567
Yan Zheng17d217f2008-12-12 10:03:38 -05005568int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
5569{
5570
5571 struct btrfs_ordered_sum *sums;
5572 struct btrfs_sector_sum *sector_sum;
5573 struct btrfs_ordered_extent *ordered;
5574 struct btrfs_root *root = BTRFS_I(inode)->root;
5575 struct list_head list;
5576 size_t offset;
5577 int ret;
5578 u64 disk_bytenr;
5579
5580 INIT_LIST_HEAD(&list);
5581
5582 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
5583 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
5584
5585 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
5586 ret = btrfs_lookup_csums_range(root, disk_bytenr,
5587 disk_bytenr + len - 1, &list);
5588
5589 while (!list_empty(&list)) {
5590 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
5591 list_del_init(&sums->list);
5592
5593 sector_sum = sums->sums;
5594 sums->bytenr = ordered->start;
5595
5596 offset = 0;
5597 while (offset < sums->len) {
5598 sector_sum->bytenr += ordered->start - disk_bytenr;
5599 sector_sum++;
5600 offset += root->sectorsize;
5601 }
5602
5603 btrfs_add_ordered_sum(inode, ordered, sums);
5604 }
5605 btrfs_put_ordered_extent(ordered);
5606 return 0;
5607}
5608
Zheng Yan1a40e232008-09-26 10:09:34 -04005609int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05005610{
5611 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05005612 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04005613 struct btrfs_fs_info *info = root->fs_info;
5614 struct extent_buffer *leaf;
5615 struct inode *reloc_inode;
5616 struct btrfs_block_group_cache *block_group;
5617 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04005618 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05005619 u64 cur_byte;
5620 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05005621 u32 nritems;
5622 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04005623 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04005624 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005625
Chris Masonedbd8d42007-12-21 16:27:24 -05005626 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04005627
5628 block_group = btrfs_lookup_block_group(info, group_start);
5629 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05005630
Chris Masond3977122009-01-05 21:25:51 -05005631 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005632 (unsigned long long)block_group->key.objectid,
5633 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04005634
Zheng Yan1a40e232008-09-26 10:09:34 -04005635 path = btrfs_alloc_path();
5636 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04005637
Zheng Yan1a40e232008-09-26 10:09:34 -04005638 reloc_inode = create_reloc_inode(info, block_group);
5639 BUG_ON(IS_ERR(reloc_inode));
5640
Zheng Yan1a40e232008-09-26 10:09:34 -04005641 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05005642 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005643
Zheng Yan1a40e232008-09-26 10:09:34 -04005644 btrfs_start_delalloc_inodes(info->tree_root);
5645 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04005646again:
Yan Zhengd899e052008-10-30 14:25:28 -04005647 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005648 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04005649 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005650 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05005651 key.offset = 0;
5652 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05005653 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05005654
Zheng Yan1a40e232008-09-26 10:09:34 -04005655 trans = btrfs_start_transaction(info->tree_root, 1);
5656 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04005657
Zheng Yan1a40e232008-09-26 10:09:34 -04005658 mutex_lock(&root->fs_info->cleaner_mutex);
5659 btrfs_clean_old_snapshots(info->tree_root);
5660 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5661 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04005662
Chris Masond3977122009-01-05 21:25:51 -05005663 while (1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05005664 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5665 if (ret < 0)
5666 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04005667next:
Chris Masonedbd8d42007-12-21 16:27:24 -05005668 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05005669 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05005670 if (path->slots[0] >= nritems) {
5671 ret = btrfs_next_leaf(root, path);
5672 if (ret < 0)
5673 goto out;
5674 if (ret == 1) {
5675 ret = 0;
5676 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005677 }
Yan73e48b22008-01-03 14:14:39 -05005678 leaf = path->nodes[0];
5679 nritems = btrfs_header_nritems(leaf);
5680 }
5681
Zheng Yan1a40e232008-09-26 10:09:34 -04005682 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05005683
Zheng Yan1a40e232008-09-26 10:09:34 -04005684 if (key.objectid >= block_group->key.objectid +
5685 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04005686 break;
5687
Chris Mason725c8462008-01-04 16:47:16 -05005688 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05005689 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005690 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05005691 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005692 continue;
Chris Mason725c8462008-01-04 16:47:16 -05005693 }
5694 progress = 1;
5695
Zheng Yan1a40e232008-09-26 10:09:34 -04005696 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5697 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05005698 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005699 goto next;
5700 }
Yan73e48b22008-01-03 14:14:39 -05005701
Chris Masonedbd8d42007-12-21 16:27:24 -05005702 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005703 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05005704 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005705
5706 __alloc_chunk_for_shrink(root, block_group, 0);
5707 ret = relocate_one_extent(root, path, &key, block_group,
5708 reloc_inode, pass);
5709 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005710 if (ret > 0)
5711 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005712
5713 key.objectid = cur_byte;
5714 key.type = 0;
5715 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005716 }
5717
5718 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005719
5720 if (pass == 0) {
5721 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5722 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005723 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005724
5725 if (total_found > 0) {
Chris Masond3977122009-01-05 21:25:51 -05005726 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005727 (unsigned long long)total_found, pass);
5728 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04005729 if (total_found == skipped && pass > 2) {
5730 iput(reloc_inode);
5731 reloc_inode = create_reloc_inode(info, block_group);
5732 pass = 0;
5733 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005734 goto again;
5735 }
5736
Zheng Yan1a40e232008-09-26 10:09:34 -04005737 /* delete reloc_inode */
5738 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005739
Zheng Yan1a40e232008-09-26 10:09:34 -04005740 /* unpin extents in this range */
5741 trans = btrfs_start_transaction(info->tree_root, 1);
5742 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005743
Zheng Yan1a40e232008-09-26 10:09:34 -04005744 spin_lock(&block_group->lock);
5745 WARN_ON(block_group->pinned > 0);
5746 WARN_ON(block_group->reserved > 0);
5747 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5748 spin_unlock(&block_group->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005749 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005750 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005751out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005752 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005753 return ret;
5754}
5755
Christoph Hellwigb2950862008-12-02 09:54:17 -05005756static int find_first_block_group(struct btrfs_root *root,
5757 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04005758{
Chris Mason925baed2008-06-25 16:01:30 -04005759 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005760 struct btrfs_key found_key;
5761 struct extent_buffer *leaf;
5762 int slot;
5763
5764 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5765 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005766 goto out;
5767
Chris Masond3977122009-01-05 21:25:51 -05005768 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005769 slot = path->slots[0];
5770 leaf = path->nodes[0];
5771 if (slot >= btrfs_header_nritems(leaf)) {
5772 ret = btrfs_next_leaf(root, path);
5773 if (ret == 0)
5774 continue;
5775 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005776 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005777 break;
5778 }
5779 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5780
5781 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005782 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5783 ret = 0;
5784 goto out;
5785 }
Chris Mason0b86a832008-03-24 15:01:56 -04005786 path->slots[0]++;
5787 }
5788 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005789out:
Chris Mason0b86a832008-03-24 15:01:56 -04005790 return ret;
5791}
5792
Zheng Yan1a40e232008-09-26 10:09:34 -04005793int btrfs_free_block_groups(struct btrfs_fs_info *info)
5794{
5795 struct btrfs_block_group_cache *block_group;
5796 struct rb_node *n;
5797
Zheng Yan1a40e232008-09-26 10:09:34 -04005798 spin_lock(&info->block_group_cache_lock);
5799 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5800 block_group = rb_entry(n, struct btrfs_block_group_cache,
5801 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04005802 rb_erase(&block_group->cache_node,
5803 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04005804 spin_unlock(&info->block_group_cache_lock);
5805
5806 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005807 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005808 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005809 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005810
5811 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005812 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04005813
5814 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005815 }
5816 spin_unlock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005817 return 0;
5818}
5819
Chris Mason9078a3e2007-04-26 16:46:15 -04005820int btrfs_read_block_groups(struct btrfs_root *root)
5821{
5822 struct btrfs_path *path;
5823 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005824 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005825 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005826 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005827 struct btrfs_key key;
5828 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005829 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005830
Chris Masonbe744172007-05-06 10:15:01 -04005831 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005832 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005833 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005834 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005835 path = btrfs_alloc_path();
5836 if (!path)
5837 return -ENOMEM;
5838
Chris Masond3977122009-01-05 21:25:51 -05005839 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005840 ret = find_first_block_group(root, path, &key);
5841 if (ret > 0) {
5842 ret = 0;
5843 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005844 }
Chris Mason0b86a832008-03-24 15:01:56 -04005845 if (ret != 0)
5846 goto error;
5847
Chris Mason5f39d392007-10-15 16:14:19 -04005848 leaf = path->nodes[0];
5849 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005850 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005851 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005852 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005853 break;
5854 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005855
Yan Zhengd2fb3432008-12-11 16:30:39 -05005856 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005857 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005858 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05005859 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005860 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005861 read_extent_buffer(leaf, &cache->item,
5862 btrfs_item_ptr_offset(leaf, path->slots[0]),
5863 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005864 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005865
Chris Mason9078a3e2007-04-26 16:46:15 -04005866 key.objectid = found_key.objectid + found_key.offset;
5867 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005868 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005869
Chris Mason6324fbf2008-03-24 15:01:59 -04005870 ret = update_space_info(info, cache->flags, found_key.offset,
5871 btrfs_block_group_used(&cache->item),
5872 &space_info);
5873 BUG_ON(ret);
5874 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005875 down_write(&space_info->groups_sem);
5876 list_add_tail(&cache->list, &space_info->block_groups);
5877 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005878
Josef Bacik0f9dd462008-09-23 13:14:11 -04005879 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5880 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005881
5882 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05005883 if (btrfs_chunk_readonly(root, cache->key.objectid))
5884 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04005885 }
Chris Mason0b86a832008-03-24 15:01:56 -04005886 ret = 0;
5887error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005888 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005889 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005890}
Chris Mason6324fbf2008-03-24 15:01:59 -04005891
5892int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5893 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005894 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005895 u64 size)
5896{
5897 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005898 struct btrfs_root *extent_root;
5899 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005900
5901 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005902
Chris Masone02119d2008-09-05 16:13:11 -04005903 root->fs_info->last_trans_new_blockgroup = trans->transid;
5904
Chris Mason8f18cf12008-04-25 16:53:30 -04005905 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005906 if (!cache)
5907 return -ENOMEM;
5908
Chris Masone17cade2008-04-15 15:41:47 -04005909 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005910 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005911 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
5912 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005913 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005914 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05005915 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005916 INIT_LIST_HEAD(&cache->list);
Chris Mason0ef3e662008-05-24 14:04:53 -04005917
Chris Mason6324fbf2008-03-24 15:01:59 -04005918 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005919 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5920 cache->flags = type;
5921 btrfs_set_block_group_flags(&cache->item, type);
5922
5923 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5924 &cache->space_info);
5925 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005926 down_write(&cache->space_info->groups_sem);
5927 list_add_tail(&cache->list, &cache->space_info->block_groups);
5928 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005929
Josef Bacik0f9dd462008-09-23 13:14:11 -04005930 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5931 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005932
Chris Mason6324fbf2008-03-24 15:01:59 -04005933 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5934 sizeof(cache->item));
5935 BUG_ON(ret);
5936
Chris Mason87ef2bb2008-10-30 11:23:27 -04005937 finish_current_insert(trans, extent_root, 0);
5938 ret = del_pending_extents(trans, extent_root, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04005939 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04005940 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005941
Chris Mason6324fbf2008-03-24 15:01:59 -04005942 return 0;
5943}
Zheng Yan1a40e232008-09-26 10:09:34 -04005944
5945int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5946 struct btrfs_root *root, u64 group_start)
5947{
5948 struct btrfs_path *path;
5949 struct btrfs_block_group_cache *block_group;
5950 struct btrfs_key key;
5951 int ret;
5952
Zheng Yan1a40e232008-09-26 10:09:34 -04005953 root = root->fs_info->extent_root;
5954
5955 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5956 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05005957 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04005958
5959 memcpy(&key, &block_group->key, sizeof(key));
5960
5961 path = btrfs_alloc_path();
5962 BUG_ON(!path);
5963
5964 btrfs_remove_free_space_cache(block_group);
5965 rb_erase(&block_group->cache_node,
5966 &root->fs_info->block_group_cache_tree);
Josef Bacik80eb2342008-10-29 14:49:05 -04005967 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005968 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005969 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005970
Yan Zhengc146afa2008-11-12 14:34:12 -05005971 spin_lock(&block_group->space_info->lock);
5972 block_group->space_info->total_bytes -= block_group->key.offset;
5973 block_group->space_info->bytes_readonly -= block_group->key.offset;
5974 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05005975 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05005976
Yan Zhengd2fb3432008-12-11 16:30:39 -05005977 put_block_group(block_group);
5978 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005979
5980 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5981 if (ret > 0)
5982 ret = -EIO;
5983 if (ret < 0)
5984 goto out;
5985
5986 ret = btrfs_del_item(trans, root, path);
5987out:
5988 btrfs_free_path(path);
5989 return ret;
5990}