blob: 2cd957d6e8d8acb8243a289a7469730911bb1f9a [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 */
18
Zach Brownec6b9102007-07-11 10:00:37 -040019#include <linux/sched.h>
Chris Mason7bb86312007-12-11 09:25:06 -050020#include <linux/crc32c.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050021#include <linux/pagemap.h>
Chris Mason74493f72007-12-11 09:25:06 -050022#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050023#include "ctree.h"
24#include "disk-io.h"
25#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040026#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040027#include "volumes.h"
Chris Masonfec577f2007-02-26 10:40:21 -050028
Chris Mason0b86a832008-03-24 15:01:56 -040029#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
Chris Mason96b51792007-10-15 16:15:19 -040030#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
Chris Mason0b86a832008-03-24 15:01:56 -040031#define BLOCK_GROUP_SYSTEM EXTENT_NEW
32
Chris Mason96b51792007-10-15 16:15:19 -040033#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
34
Chris Masone089f052007-03-16 16:20:31 -040035static int finish_current_insert(struct btrfs_trans_handle *trans, struct
36 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040037static int del_pending_extents(struct btrfs_trans_handle *trans, struct
38 btrfs_root *extent_root);
Yand548ee52008-01-03 13:56:30 -050039
Chris Masonfec577f2007-02-26 10:40:21 -050040
Chris Masone37c9e62007-05-09 20:13:14 -040041static int cache_block_group(struct btrfs_root *root,
42 struct btrfs_block_group_cache *block_group)
43{
44 struct btrfs_path *path;
45 int ret;
46 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -040047 struct extent_buffer *leaf;
Chris Masond1310b22008-01-24 16:13:08 -050048 struct extent_io_tree *free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040049 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -040050 u64 last = 0;
51 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040052 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040053 int found = 0;
54
Chris Mason00f5c792007-11-30 10:09:33 -050055 if (!block_group)
56 return 0;
57
Chris Masone37c9e62007-05-09 20:13:14 -040058 root = root->fs_info->extent_root;
Chris Masonf510cfe2007-10-15 16:14:48 -040059 free_space_cache = &root->fs_info->free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040060
61 if (block_group->cached)
62 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -040063
Chris Masone37c9e62007-05-09 20:13:14 -040064 path = btrfs_alloc_path();
65 if (!path)
66 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040067
Chris Mason2cc58cf2007-08-27 16:49:44 -040068 path->reada = 2;
Yan7d7d6062007-09-14 16:15:28 -040069 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040070 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040071 key.offset = 0;
72 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
73 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
74 if (ret < 0)
75 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -040076 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -050077 if (ret < 0)
78 return ret;
79 if (ret == 0) {
80 leaf = path->nodes[0];
81 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
82 if (key.objectid + key.offset > first_free)
83 first_free = key.objectid + key.offset;
84 }
Chris Masone37c9e62007-05-09 20:13:14 -040085 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -040086 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -040087 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040088 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -040089 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040090 if (ret < 0)
91 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040092 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040093 continue;
Chris Masonde428b62007-05-18 13:28:27 -040094 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040095 break;
96 }
97 }
Chris Mason5f39d392007-10-15 16:14:19 -040098 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -040099 if (key.objectid < block_group->key.objectid) {
Yan7d7d6062007-09-14 16:15:28 -0400100 goto next;
101 }
Chris Masone37c9e62007-05-09 20:13:14 -0400102 if (key.objectid >= block_group->key.objectid +
103 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -0400104 break;
105 }
106
107 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
108 if (!found) {
109 last = first_free;
110 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400111 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400112 if (key.objectid > last) {
113 hole_size = key.objectid - last;
114 set_extent_dirty(free_space_cache, last,
115 last + hole_size - 1,
116 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400117 }
Yan7d7d6062007-09-14 16:15:28 -0400118 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400119 }
Yan7d7d6062007-09-14 16:15:28 -0400120next:
Chris Masone37c9e62007-05-09 20:13:14 -0400121 path->slots[0]++;
122 }
123
Yan7d7d6062007-09-14 16:15:28 -0400124 if (!found)
125 last = first_free;
126 if (block_group->key.objectid +
127 block_group->key.offset > last) {
128 hole_size = block_group->key.objectid +
129 block_group->key.offset - last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400130 set_extent_dirty(free_space_cache, last,
131 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400132 }
Chris Masone37c9e62007-05-09 20:13:14 -0400133 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400134err:
Chris Masone37c9e62007-05-09 20:13:14 -0400135 btrfs_free_path(path);
136 return 0;
137}
138
Chris Mason5276aed2007-06-11 21:33:38 -0400139struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
140 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400141 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400142{
Chris Masond1310b22008-01-24 16:13:08 -0500143 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -0400144 struct btrfs_block_group_cache *block_group = NULL;
145 u64 ptr;
146 u64 start;
147 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400148 int ret;
149
Chris Mason96b51792007-10-15 16:15:19 -0400150 block_group_cache = &info->block_group_cache;
151 ret = find_first_extent_bit(block_group_cache,
Chris Masondb945352007-10-15 16:15:53 -0400152 bytenr, &start, &end,
Chris Mason0b86a832008-03-24 15:01:56 -0400153 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
154 BLOCK_GROUP_SYSTEM);
Chris Masonbe744172007-05-06 10:15:01 -0400155 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400156 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400157 }
Chris Mason96b51792007-10-15 16:15:19 -0400158 ret = get_state_private(block_group_cache, start, &ptr);
159 if (ret)
160 return NULL;
161
Jens Axboeae2f5412007-10-19 09:22:59 -0400162 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Yan5cf66422007-11-16 14:57:09 -0500163 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400164 block_group->key.objectid + block_group->key.offset)
165 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400166 return NULL;
167}
Chris Mason0b86a832008-03-24 15:01:56 -0400168
169static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
170{
171 if ((bits & BLOCK_GROUP_DATA) &&
172 (cache->flags & BTRFS_BLOCK_GROUP_DATA))
173 return 1;
174 if ((bits & BLOCK_GROUP_METADATA) &&
175 (cache->flags & BTRFS_BLOCK_GROUP_METADATA))
176 return 1;
177 if ((bits & BLOCK_GROUP_SYSTEM) &&
178 (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM))
179 return 1;
180 return 0;
181}
182
183static int noinline find_search_start(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500184 struct btrfs_block_group_cache **cache_ret,
Chris Mason0b86a832008-03-24 15:01:56 -0400185 u64 *start_ret, int num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400186{
Chris Masone37c9e62007-05-09 20:13:14 -0400187 int ret;
188 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masond7fc6402008-02-18 12:12:38 -0500189 struct extent_io_tree *free_space_cache;
Chris Masone19caa52007-10-15 16:17:44 -0400190 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400191 u64 start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400192 u64 end = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500193 u64 cache_miss = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500194 u64 total_fs_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -0400195 u64 search_start = *start_ret;
Chris Masonf84a8b32007-11-06 10:26:29 -0500196 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400197
Chris Mason0b86a832008-03-24 15:01:56 -0400198 if (!cache)
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500199 goto out;
Chris Masonc31f8832008-01-08 15:46:31 -0500200 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masond7fc6402008-02-18 12:12:38 -0500201 free_space_cache = &root->fs_info->free_space_cache;
202
Chris Masone37c9e62007-05-09 20:13:14 -0400203again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400204 ret = cache_block_group(root, cache);
205 if (ret)
206 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500207
Chris Masone19caa52007-10-15 16:17:44 -0400208 last = max(search_start, cache->key.objectid);
Chris Mason0b86a832008-03-24 15:01:56 -0400209 if (!block_group_bits(cache, data)) {
210 goto new_group;
211 }
Chris Masone19caa52007-10-15 16:17:44 -0400212
Chris Masone37c9e62007-05-09 20:13:14 -0400213 while(1) {
Chris Mason7f93bf82008-03-24 15:01:28 -0400214 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
215 last, &start, &end, EXTENT_DIRTY);
216 if (ret) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500217 if (!cache_miss)
218 cache_miss = last;
Chris Masone19caa52007-10-15 16:17:44 -0400219 goto new_group;
220 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400221
Chris Mason7f93bf82008-03-24 15:01:28 -0400222 start = max(last, start);
223 last = end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500224 if (last - start < num) {
225 if (last == cache->key.objectid + cache->key.offset)
226 cache_miss = start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400227 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500228 }
Chris Mason0b86a832008-03-24 15:01:56 -0400229 if (start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400230 goto new_group;
Chris Masonc31f8832008-01-08 15:46:31 -0500231 if (start + num > total_fs_bytes)
232 goto new_group;
Chris Mason0b86a832008-03-24 15:01:56 -0400233 *start_ret = start;
234 return 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400235 }
236out:
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500237 cache = btrfs_lookup_block_group(root->fs_info, search_start);
238 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -0400239 printk("Unable to find block group for %Lu\n", search_start);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500240 WARN_ON(1);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500241 }
Chris Mason0b86a832008-03-24 15:01:56 -0400242 return -ENOSPC;
Chris Masone37c9e62007-05-09 20:13:14 -0400243
244new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400245 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500246wrapped:
Chris Masone19caa52007-10-15 16:17:44 -0400247 cache = btrfs_lookup_block_group(root->fs_info, last);
Chris Masonc31f8832008-01-08 15:46:31 -0500248 if (!cache || cache->key.objectid >= total_fs_bytes) {
Chris Mason0e4de582007-11-26 10:55:49 -0500249no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500250 if (!wrapped) {
251 wrapped = 1;
252 last = search_start;
Chris Masonf84a8b32007-11-06 10:26:29 -0500253 goto wrapped;
254 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500255 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400256 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500257 if (cache_miss && !cache->cached) {
258 cache_block_group(root, cache);
259 last = cache_miss;
Chris Mason257d0ce2007-11-07 21:08:16 -0500260 cache = btrfs_lookup_block_group(root->fs_info, last);
261 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500262 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500263 if (!cache)
264 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400265 *cache_ret = cache;
Chris Mason257d0ce2007-11-07 21:08:16 -0500266 cache_miss = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400267 goto again;
268}
269
Chris Mason84f54cf2007-06-12 07:43:08 -0400270static u64 div_factor(u64 num, int factor)
271{
Chris Mason257d0ce2007-11-07 21:08:16 -0500272 if (factor == 10)
273 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400274 num *= factor;
275 do_div(num, 10);
276 return num;
277}
278
Chris Mason31f3c992007-04-30 15:25:45 -0400279struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
280 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400281 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400282 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400283{
Chris Mason96b51792007-10-15 16:15:19 -0400284 struct btrfs_block_group_cache *cache;
Chris Masond1310b22008-01-24 16:13:08 -0500285 struct extent_io_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400286 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400287 struct btrfs_fs_info *info = root->fs_info;
288 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400289 u64 last = 0;
290 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400291 u64 start;
292 u64 end;
293 u64 free_check;
294 u64 ptr;
Chris Masonc31f8832008-01-08 15:46:31 -0500295 u64 total_fs_bytes;
Chris Mason96b51792007-10-15 16:15:19 -0400296 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400297 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400298 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400299 int factor = 8;
300
Chris Mason96b51792007-10-15 16:15:19 -0400301 block_group_cache = &info->block_group_cache;
Chris Masonc31f8832008-01-08 15:46:31 -0500302 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Mason96b51792007-10-15 16:15:19 -0400303
Chris Masonde428b62007-05-18 13:28:27 -0400304 if (!owner)
Chris Masonf84a8b32007-11-06 10:26:29 -0500305 factor = 8;
Chris Masonbe744172007-05-06 10:15:01 -0400306
Chris Mason0b86a832008-03-24 15:01:56 -0400307 bit = data;
Chris Masonbe744172007-05-06 10:15:01 -0400308
Chris Masonc31f8832008-01-08 15:46:31 -0500309 if (search_start && search_start < total_fs_bytes) {
Chris Masonbe744172007-05-06 10:15:01 -0400310 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400311 shint = btrfs_lookup_block_group(info, search_start);
Chris Mason0b86a832008-03-24 15:01:56 -0400312 if (shint && block_group_bits(shint, data)) {
Chris Masonbe744172007-05-06 10:15:01 -0400313 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500314 if (used + shint->pinned <
315 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400316 return shint;
317 }
318 }
319 }
Chris Mason0b86a832008-03-24 15:01:56 -0400320 if (hint && block_group_bits(hint, data) &&
321 hint->key.objectid < total_fs_bytes) {
Chris Mason31f3c992007-04-30 15:25:45 -0400322 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500323 if (used + hint->pinned <
324 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400325 return hint;
326 }
Chris Masone19caa52007-10-15 16:17:44 -0400327 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400328 hint_last = last;
329 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400330 if (hint)
331 hint_last = max(hint->key.objectid, search_start);
332 else
333 hint_last = search_start;
334
Chris Masonc31f8832008-01-08 15:46:31 -0500335 if (hint_last >= total_fs_bytes)
336 hint_last = search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400337 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400338 }
Chris Mason31f3c992007-04-30 15:25:45 -0400339again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400340 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400341 ret = find_first_extent_bit(block_group_cache, last,
342 &start, &end, bit);
343 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400344 break;
Chris Mason96b51792007-10-15 16:15:19 -0400345
346 ret = get_state_private(block_group_cache, start, &ptr);
347 if (ret)
348 break;
349
Jens Axboeae2f5412007-10-19 09:22:59 -0400350 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400351 last = cache->key.objectid + cache->key.offset;
352 used = btrfs_block_group_used(&cache->item);
353
Chris Masonc31f8832008-01-08 15:46:31 -0500354 if (cache->key.objectid > total_fs_bytes)
355 break;
356
Chris Mason96b51792007-10-15 16:15:19 -0400357 if (full_search)
358 free_check = cache->key.offset;
359 else
360 free_check = div_factor(cache->key.offset, factor);
Yan324ae4d2007-11-16 14:57:08 -0500361 if (used + cache->pinned < free_check) {
Chris Mason96b51792007-10-15 16:15:19 -0400362 found_group = cache;
363 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400364 }
Chris Masonde428b62007-05-18 13:28:27 -0400365 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400366 }
Chris Mason31f3c992007-04-30 15:25:45 -0400367 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400368 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400369 full_search = 1;
370 goto again;
371 }
Chris Masonbe744172007-05-06 10:15:01 -0400372found:
Chris Mason31f3c992007-04-30 15:25:45 -0400373 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400374}
375
Chris Mason7bb86312007-12-11 09:25:06 -0500376static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500377 u64 owner, u64 owner_offset)
378{
379 u32 high_crc = ~(u32)0;
380 u32 low_crc = ~(u32)0;
381 __le64 lenum;
382
383 lenum = cpu_to_le64(root_objectid);
384 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500385 lenum = cpu_to_le64(ref_generation);
386 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500387 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
388 lenum = cpu_to_le64(owner);
389 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
390 lenum = cpu_to_le64(owner_offset);
391 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
392 }
Chris Mason74493f72007-12-11 09:25:06 -0500393 return ((u64)high_crc << 32) | (u64)low_crc;
394}
395
Chris Mason7bb86312007-12-11 09:25:06 -0500396static int match_extent_ref(struct extent_buffer *leaf,
397 struct btrfs_extent_ref *disk_ref,
398 struct btrfs_extent_ref *cpu_ref)
399{
400 int ret;
401 int len;
402
403 if (cpu_ref->objectid)
404 len = sizeof(*cpu_ref);
405 else
406 len = 2 * sizeof(u64);
407 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
408 len);
409 return ret == 0;
410}
411
Chris Mason98ed5172008-01-03 10:01:48 -0500412static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
413 struct btrfs_root *root,
414 struct btrfs_path *path, u64 bytenr,
415 u64 root_objectid,
416 u64 ref_generation, u64 owner,
417 u64 owner_offset, int del)
Chris Mason7bb86312007-12-11 09:25:06 -0500418{
419 u64 hash;
420 struct btrfs_key key;
421 struct btrfs_key found_key;
422 struct btrfs_extent_ref ref;
423 struct extent_buffer *leaf;
424 struct btrfs_extent_ref *disk_ref;
425 int ret;
426 int ret2;
427
428 btrfs_set_stack_ref_root(&ref, root_objectid);
429 btrfs_set_stack_ref_generation(&ref, ref_generation);
430 btrfs_set_stack_ref_objectid(&ref, owner);
431 btrfs_set_stack_ref_offset(&ref, owner_offset);
432
433 hash = hash_extent_ref(root_objectid, ref_generation, owner,
434 owner_offset);
435 key.offset = hash;
436 key.objectid = bytenr;
437 key.type = BTRFS_EXTENT_REF_KEY;
438
439 while (1) {
440 ret = btrfs_search_slot(trans, root, &key, path,
441 del ? -1 : 0, del);
442 if (ret < 0)
443 goto out;
444 leaf = path->nodes[0];
445 if (ret != 0) {
446 u32 nritems = btrfs_header_nritems(leaf);
447 if (path->slots[0] >= nritems) {
448 ret2 = btrfs_next_leaf(root, path);
449 if (ret2)
450 goto out;
451 leaf = path->nodes[0];
452 }
453 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
454 if (found_key.objectid != bytenr ||
455 found_key.type != BTRFS_EXTENT_REF_KEY)
456 goto out;
457 key.offset = found_key.offset;
458 if (del) {
459 btrfs_release_path(root, path);
460 continue;
461 }
462 }
463 disk_ref = btrfs_item_ptr(path->nodes[0],
464 path->slots[0],
465 struct btrfs_extent_ref);
466 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
467 ret = 0;
468 goto out;
469 }
470 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
471 key.offset = found_key.offset + 1;
472 btrfs_release_path(root, path);
473 }
474out:
475 return ret;
476}
477
Chris Masond8d5f3e2007-12-11 12:42:00 -0500478/*
479 * Back reference rules. Back refs have three main goals:
480 *
481 * 1) differentiate between all holders of references to an extent so that
482 * when a reference is dropped we can make sure it was a valid reference
483 * before freeing the extent.
484 *
485 * 2) Provide enough information to quickly find the holders of an extent
486 * if we notice a given block is corrupted or bad.
487 *
488 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
489 * maintenance. This is actually the same as #2, but with a slightly
490 * different use case.
491 *
492 * File extents can be referenced by:
493 *
494 * - multiple snapshots, subvolumes, or different generations in one subvol
495 * - different files inside a single subvolume (in theory, not implemented yet)
496 * - different offsets inside a file (bookend extents in file.c)
497 *
498 * The extent ref structure has fields for:
499 *
500 * - Objectid of the subvolume root
501 * - Generation number of the tree holding the reference
502 * - objectid of the file holding the reference
503 * - offset in the file corresponding to the key holding the reference
504 *
505 * When a file extent is allocated the fields are filled in:
506 * (root_key.objectid, trans->transid, inode objectid, offset in file)
507 *
508 * When a leaf is cow'd new references are added for every file extent found
509 * in the leaf. It looks the same as the create case, but trans->transid
510 * will be different when the block is cow'd.
511 *
512 * (root_key.objectid, trans->transid, inode objectid, offset in file)
513 *
514 * When a file extent is removed either during snapshot deletion or file
515 * truncation, the corresponding back reference is found
516 * by searching for:
517 *
518 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
519 * inode objectid, offset in file)
520 *
521 * Btree extents can be referenced by:
522 *
523 * - Different subvolumes
524 * - Different generations of the same subvolume
525 *
526 * Storing sufficient information for a full reverse mapping of a btree
527 * block would require storing the lowest key of the block in the backref,
528 * and it would require updating that lowest key either before write out or
529 * every time it changed. Instead, the objectid of the lowest key is stored
530 * along with the level of the tree block. This provides a hint
531 * about where in the btree the block can be found. Searches through the
532 * btree only need to look for a pointer to that block, so they stop one
533 * level higher than the level recorded in the backref.
534 *
535 * Some btrees do not do reference counting on their extents. These
536 * include the extent tree and the tree of tree roots. Backrefs for these
537 * trees always have a generation of zero.
538 *
539 * When a tree block is created, back references are inserted:
540 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500541 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500542 *
543 * When a tree block is cow'd in a reference counted root,
544 * new back references are added for all the blocks it points to.
545 * These are of the form (trans->transid will have increased since creation):
546 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500547 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500548 *
549 * Because the lowest_key_objectid and the level are just hints
550 * they are not used when backrefs are deleted. When a backref is deleted:
551 *
552 * if backref was for a tree root:
553 * root_objectid = root->root_key.objectid
554 * else
555 * root_objectid = btrfs_header_owner(parent)
556 *
557 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
558 *
559 * Back Reference Key hashing:
560 *
561 * Back references have four fields, each 64 bits long. Unfortunately,
562 * This is hashed into a single 64 bit number and placed into the key offset.
563 * The key objectid corresponds to the first byte in the extent, and the
564 * key type is set to BTRFS_EXTENT_REF_KEY
565 */
Chris Mason7bb86312007-12-11 09:25:06 -0500566int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
567 struct btrfs_root *root,
568 struct btrfs_path *path, u64 bytenr,
569 u64 root_objectid, u64 ref_generation,
570 u64 owner, u64 owner_offset)
Chris Mason74493f72007-12-11 09:25:06 -0500571{
572 u64 hash;
573 struct btrfs_key key;
574 struct btrfs_extent_ref ref;
Chris Mason7bb86312007-12-11 09:25:06 -0500575 struct btrfs_extent_ref *disk_ref;
Chris Mason74493f72007-12-11 09:25:06 -0500576 int ret;
577
578 btrfs_set_stack_ref_root(&ref, root_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500579 btrfs_set_stack_ref_generation(&ref, ref_generation);
Chris Mason74493f72007-12-11 09:25:06 -0500580 btrfs_set_stack_ref_objectid(&ref, owner);
581 btrfs_set_stack_ref_offset(&ref, owner_offset);
582
Chris Mason7bb86312007-12-11 09:25:06 -0500583 hash = hash_extent_ref(root_objectid, ref_generation, owner,
584 owner_offset);
Chris Mason74493f72007-12-11 09:25:06 -0500585 key.offset = hash;
586 key.objectid = bytenr;
587 key.type = BTRFS_EXTENT_REF_KEY;
588
589 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
590 while (ret == -EEXIST) {
Chris Mason7bb86312007-12-11 09:25:06 -0500591 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
592 struct btrfs_extent_ref);
593 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
594 goto out;
595 key.offset++;
596 btrfs_release_path(root, path);
597 ret = btrfs_insert_empty_item(trans, root, path, &key,
598 sizeof(ref));
Chris Mason74493f72007-12-11 09:25:06 -0500599 }
Chris Mason7bb86312007-12-11 09:25:06 -0500600 if (ret)
601 goto out;
602 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
603 struct btrfs_extent_ref);
604 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
605 sizeof(ref));
606 btrfs_mark_buffer_dirty(path->nodes[0]);
607out:
608 btrfs_release_path(root, path);
609 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500610}
611
Chris Masonb18c6682007-04-17 13:26:50 -0400612int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
613 struct btrfs_root *root,
Chris Mason74493f72007-12-11 09:25:06 -0500614 u64 bytenr, u64 num_bytes,
Chris Mason7bb86312007-12-11 09:25:06 -0500615 u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500616 u64 owner, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500617{
Chris Mason5caf2a02007-04-02 11:20:42 -0400618 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500619 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400620 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400621 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400622 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400623 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500624
Chris Masondb945352007-10-15 16:15:53 -0400625 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400626 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400627 if (!path)
628 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400629
Chris Masonb0331a42008-01-08 15:46:31 -0500630 path->reada = 0;
Chris Masondb945352007-10-15 16:15:53 -0400631 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400632 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400633 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400634 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400635 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400636 if (ret < 0)
637 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400638 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500639 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400640 }
Chris Mason02217ed2007-03-02 16:08:05 -0500641 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400642 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400643 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400644 refs = btrfs_extent_refs(l, item);
645 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400646 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500647
Chris Mason5caf2a02007-04-02 11:20:42 -0400648 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500649
Chris Masonb0331a42008-01-08 15:46:31 -0500650 path->reada = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500651 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
652 path, bytenr, root_objectid,
653 ref_generation, owner, owner_offset);
654 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400655 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400656 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500657
658 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500659 return 0;
660}
661
Chris Masone9d0b132007-08-10 14:06:19 -0400662int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
663 struct btrfs_root *root)
664{
665 finish_current_insert(trans, root->fs_info->extent_root);
666 del_pending_extents(trans, root->fs_info->extent_root);
667 return 0;
668}
669
Chris Masonb18c6682007-04-17 13:26:50 -0400670static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400671 struct btrfs_root *root, u64 bytenr,
672 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500673{
Chris Mason5caf2a02007-04-02 11:20:42 -0400674 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500675 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400676 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400677 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400678 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400679
Chris Masondb945352007-10-15 16:15:53 -0400680 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400681 path = btrfs_alloc_path();
Chris Masonb0331a42008-01-08 15:46:31 -0500682 path->reada = 0;
Chris Masondb945352007-10-15 16:15:53 -0400683 key.objectid = bytenr;
684 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400685 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400686 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400687 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400688 if (ret < 0)
689 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400690 if (ret != 0) {
691 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400692 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500693 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400694 }
695 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400696 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400697 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400698out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400699 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500700 return 0;
701}
702
Chris Masonbe20aa92007-12-17 20:14:01 -0500703u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
704 struct btrfs_path *count_path,
705 u64 first_extent)
706{
707 struct btrfs_root *extent_root = root->fs_info->extent_root;
708 struct btrfs_path *path;
709 u64 bytenr;
710 u64 found_objectid;
Chris Mason56b453c2008-01-03 09:08:27 -0500711 u64 root_objectid = root->root_key.objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500712 u32 total_count = 0;
713 u32 cur_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500714 u32 nritems;
715 int ret;
716 struct btrfs_key key;
717 struct btrfs_key found_key;
718 struct extent_buffer *l;
719 struct btrfs_extent_item *item;
720 struct btrfs_extent_ref *ref_item;
721 int level = -1;
722
723 path = btrfs_alloc_path();
724again:
725 if (level == -1)
726 bytenr = first_extent;
727 else
728 bytenr = count_path->nodes[level]->start;
729
730 cur_count = 0;
731 key.objectid = bytenr;
732 key.offset = 0;
733
734 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
735 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
736 if (ret < 0)
737 goto out;
738 BUG_ON(ret == 0);
739
740 l = path->nodes[0];
741 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
742
743 if (found_key.objectid != bytenr ||
744 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
745 goto out;
746 }
747
748 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masonbe20aa92007-12-17 20:14:01 -0500749 while (1) {
Chris Masonbd098352008-01-03 13:23:19 -0500750 l = path->nodes[0];
Chris Masonbe20aa92007-12-17 20:14:01 -0500751 nritems = btrfs_header_nritems(l);
752 if (path->slots[0] >= nritems) {
753 ret = btrfs_next_leaf(extent_root, path);
754 if (ret == 0)
755 continue;
756 break;
757 }
758 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
759 if (found_key.objectid != bytenr)
760 break;
Chris Masonbd098352008-01-03 13:23:19 -0500761
Chris Masonbe20aa92007-12-17 20:14:01 -0500762 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
763 path->slots[0]++;
764 continue;
765 }
766
767 cur_count++;
768 ref_item = btrfs_item_ptr(l, path->slots[0],
769 struct btrfs_extent_ref);
770 found_objectid = btrfs_ref_root(l, ref_item);
771
Chris Mason56b453c2008-01-03 09:08:27 -0500772 if (found_objectid != root_objectid) {
773 total_count = 2;
Chris Mason4313b392008-01-03 09:08:48 -0500774 goto out;
Chris Mason56b453c2008-01-03 09:08:27 -0500775 }
776 total_count = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500777 path->slots[0]++;
778 }
779 if (cur_count == 0) {
780 total_count = 0;
781 goto out;
782 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500783 if (level >= 0 && root->node == count_path->nodes[level])
784 goto out;
785 level++;
786 btrfs_release_path(root, path);
787 goto again;
788
789out:
790 btrfs_free_path(path);
791 return total_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500792}
Chris Masonc5739bb2007-04-10 09:27:04 -0400793int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -0500794 struct btrfs_root *root, u64 owner_objectid)
Chris Masonc5739bb2007-04-10 09:27:04 -0400795{
Chris Mason7bb86312007-12-11 09:25:06 -0500796 u64 generation;
797 u64 key_objectid;
798 u64 level;
799 u32 nritems;
800 struct btrfs_disk_key disk_key;
801
802 level = btrfs_header_level(root->node);
803 generation = trans->transid;
804 nritems = btrfs_header_nritems(root->node);
805 if (nritems > 0) {
806 if (level == 0)
807 btrfs_item_key(root->node, &disk_key, 0);
808 else
809 btrfs_node_key(root->node, &disk_key, 0);
810 key_objectid = btrfs_disk_key_objectid(&disk_key);
811 } else {
812 key_objectid = 0;
813 }
Chris Masondb945352007-10-15 16:15:53 -0400814 return btrfs_inc_extent_ref(trans, root, root->node->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500815 root->node->len, owner_objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500816 generation, level, key_objectid);
Chris Masonc5739bb2007-04-10 09:27:04 -0400817}
818
Chris Masone089f052007-03-16 16:20:31 -0400819int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400820 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500821{
Chris Masondb945352007-10-15 16:15:53 -0400822 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400823 u32 nritems;
824 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400825 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500826 int i;
Chris Masondb945352007-10-15 16:15:53 -0400827 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400828 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400829 int faili;
Chris Masona28ec192007-03-06 20:08:01 -0500830
Chris Mason3768f362007-03-13 16:47:54 -0400831 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500832 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400833
Chris Masondb945352007-10-15 16:15:53 -0400834 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400835 nritems = btrfs_header_nritems(buf);
836 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400837 if (level == 0) {
838 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400839 btrfs_item_key_to_cpu(buf, &key, i);
840 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400841 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400842 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400843 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400844 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454df2007-04-19 13:37:44 -0400845 BTRFS_FILE_EXTENT_INLINE)
846 continue;
Chris Masondb945352007-10-15 16:15:53 -0400847 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
848 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400849 continue;
Chris Masondb945352007-10-15 16:15:53 -0400850 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500851 btrfs_file_extent_disk_num_bytes(buf, fi),
852 root->root_key.objectid, trans->transid,
853 key.objectid, key.offset);
Chris Mason54aa1f42007-06-22 14:16:25 -0400854 if (ret) {
855 faili = i;
856 goto fail;
857 }
Chris Mason6407bf62007-03-27 06:33:00 -0400858 } else {
Chris Masondb945352007-10-15 16:15:53 -0400859 bytenr = btrfs_node_blockptr(buf, i);
Chris Mason6caab482007-12-13 09:48:07 -0500860 btrfs_node_key_to_cpu(buf, &key, i);
Chris Masondb945352007-10-15 16:15:53 -0400861 ret = btrfs_inc_extent_ref(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500862 btrfs_level_size(root, level - 1),
863 root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500864 trans->transid,
865 level - 1, key.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400866 if (ret) {
867 faili = i;
868 goto fail;
869 }
Chris Mason6407bf62007-03-27 06:33:00 -0400870 }
Chris Mason02217ed2007-03-02 16:08:05 -0500871 }
872 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400873fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400874 WARN_ON(1);
Chris Mason7bb86312007-12-11 09:25:06 -0500875#if 0
Chris Mason54aa1f42007-06-22 14:16:25 -0400876 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400877 if (level == 0) {
878 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400879 btrfs_item_key_to_cpu(buf, &key, i);
880 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400881 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400882 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400883 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400884 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400885 BTRFS_FILE_EXTENT_INLINE)
886 continue;
Chris Masondb945352007-10-15 16:15:53 -0400887 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
888 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400889 continue;
Chris Masondb945352007-10-15 16:15:53 -0400890 err = btrfs_free_extent(trans, root, disk_bytenr,
891 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400892 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400893 BUG_ON(err);
894 } else {
Chris Masondb945352007-10-15 16:15:53 -0400895 bytenr = btrfs_node_blockptr(buf, i);
896 err = btrfs_free_extent(trans, root, bytenr,
897 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400898 BUG_ON(err);
899 }
900 }
Chris Mason7bb86312007-12-11 09:25:06 -0500901#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400902 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500903}
904
Chris Mason9078a3e2007-04-26 16:46:15 -0400905static int write_one_cache_group(struct btrfs_trans_handle *trans,
906 struct btrfs_root *root,
907 struct btrfs_path *path,
908 struct btrfs_block_group_cache *cache)
909{
910 int ret;
911 int pending_ret;
912 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400913 unsigned long bi;
914 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400915
Chris Mason9078a3e2007-04-26 16:46:15 -0400916 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400917 if (ret < 0)
918 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400919 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400920
921 leaf = path->nodes[0];
922 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
923 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
924 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400925 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400926fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400927 finish_current_insert(trans, extent_root);
928 pending_ret = del_pending_extents(trans, extent_root);
929 if (ret)
930 return ret;
931 if (pending_ret)
932 return pending_ret;
933 return 0;
934
935}
936
Chris Mason96b51792007-10-15 16:15:19 -0400937int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
938 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400939{
Chris Masond1310b22008-01-24 16:13:08 -0500940 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -0400941 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400942 int ret;
943 int err = 0;
944 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400945 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400946 u64 last = 0;
947 u64 start;
948 u64 end;
949 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400950
Chris Mason96b51792007-10-15 16:15:19 -0400951 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400952 path = btrfs_alloc_path();
953 if (!path)
954 return -ENOMEM;
955
956 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400957 ret = find_first_extent_bit(block_group_cache, last,
958 &start, &end, BLOCK_GROUP_DIRTY);
959 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400960 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400961
Chris Mason96b51792007-10-15 16:15:19 -0400962 last = end + 1;
963 ret = get_state_private(block_group_cache, start, &ptr);
964 if (ret)
965 break;
966
Jens Axboeae2f5412007-10-19 09:22:59 -0400967 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400968 err = write_one_cache_group(trans, root,
969 path, cache);
970 /*
971 * if we fail to write the cache group, we want
972 * to keep it marked dirty in hopes that a later
973 * write will work
974 */
975 if (err) {
976 werr = err;
977 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -0400978 }
Chris Mason96b51792007-10-15 16:15:19 -0400979 clear_extent_bits(block_group_cache, start, end,
980 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400981 }
982 btrfs_free_path(path);
983 return werr;
984}
985
986static int update_block_group(struct btrfs_trans_handle *trans,
987 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400988 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -0400989 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -0400990{
991 struct btrfs_block_group_cache *cache;
992 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400993 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400994 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -0400995 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -0400996 u64 start;
997 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -0400998
Chris Mason9078a3e2007-04-26 16:46:15 -0400999 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001000 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001001 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001002 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001003 }
Chris Masondb945352007-10-15 16:15:53 -04001004 byte_in_group = bytenr - cache->key.objectid;
1005 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -04001006 start = cache->key.objectid;
1007 end = start + cache->key.offset - 1;
1008 set_extent_bits(&info->block_group_cache, start, end,
1009 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001010
1011 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001012 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001013 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001014 old_val += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -04001015 } else {
Chris Masondb945352007-10-15 16:15:53 -04001016 old_val -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -04001017 if (mark_free) {
1018 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -04001019 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -04001020 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001021 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001022 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001023 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -04001024 total -= num_bytes;
1025 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001026 }
1027 return 0;
1028}
Yan324ae4d2007-11-16 14:57:08 -05001029static int update_pinned_extents(struct btrfs_root *root,
1030 u64 bytenr, u64 num, int pin)
1031{
1032 u64 len;
1033 struct btrfs_block_group_cache *cache;
1034 struct btrfs_fs_info *fs_info = root->fs_info;
1035
1036 if (pin) {
1037 set_extent_dirty(&fs_info->pinned_extents,
1038 bytenr, bytenr + num - 1, GFP_NOFS);
1039 } else {
1040 clear_extent_dirty(&fs_info->pinned_extents,
1041 bytenr, bytenr + num - 1, GFP_NOFS);
1042 }
1043 while (num > 0) {
1044 cache = btrfs_lookup_block_group(fs_info, bytenr);
1045 WARN_ON(!cache);
1046 len = min(num, cache->key.offset -
1047 (bytenr - cache->key.objectid));
1048 if (pin) {
1049 cache->pinned += len;
1050 fs_info->total_pinned += len;
1051 } else {
1052 cache->pinned -= len;
1053 fs_info->total_pinned -= len;
1054 }
1055 bytenr += len;
1056 num -= len;
1057 }
1058 return 0;
1059}
Chris Mason9078a3e2007-04-26 16:46:15 -04001060
Chris Masond1310b22008-01-24 16:13:08 -05001061int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001062{
Chris Masonccd467d2007-06-28 15:57:36 -04001063 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001064 u64 start;
1065 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001066 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001067 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001068
1069 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001070 ret = find_first_extent_bit(pinned_extents, last,
1071 &start, &end, EXTENT_DIRTY);
1072 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001073 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001074 set_extent_dirty(copy, start, end, GFP_NOFS);
1075 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001076 }
1077 return 0;
1078}
1079
1080int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1081 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001082 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001083{
Chris Mason1a5bc162007-10-15 16:15:26 -04001084 u64 start;
1085 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001086 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001087 struct extent_io_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -04001088 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -05001089
1090 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001091 ret = find_first_extent_bit(unpin, 0, &start, &end,
1092 EXTENT_DIRTY);
1093 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001094 break;
Yan324ae4d2007-11-16 14:57:08 -05001095 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001096 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1097 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -05001098 }
1099 return 0;
1100}
1101
Chris Mason98ed5172008-01-03 10:01:48 -05001102static int finish_current_insert(struct btrfs_trans_handle *trans,
1103 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001104{
Chris Mason7bb86312007-12-11 09:25:06 -05001105 u64 start;
1106 u64 end;
1107 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001108 struct extent_buffer *eb;
Chris Mason7bb86312007-12-11 09:25:06 -05001109 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001110 struct btrfs_key ins;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001111 struct btrfs_disk_key first;
Chris Mason234b63a2007-03-13 10:46:10 -04001112 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001113 int ret;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001114 int level;
Chris Mason1a5bc162007-10-15 16:15:26 -04001115 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001116
Chris Mason5f39d392007-10-15 16:14:19 -04001117 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -04001118 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason7bb86312007-12-11 09:25:06 -05001119 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001120
Chris Mason26b80032007-08-08 20:17:12 -04001121 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001122 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1123 &end, EXTENT_LOCKED);
1124 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001125 break;
1126
Chris Mason1a5bc162007-10-15 16:15:26 -04001127 ins.objectid = start;
1128 ins.offset = end + 1 - start;
1129 err = btrfs_insert_item(trans, extent_root, &ins,
1130 &extent_item, sizeof(extent_item));
1131 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1132 GFP_NOFS);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001133 eb = read_tree_block(extent_root, ins.objectid, ins.offset);
1134 level = btrfs_header_level(eb);
1135 if (level == 0) {
1136 btrfs_item_key(eb, &first, 0);
1137 } else {
1138 btrfs_node_key(eb, &first, 0);
1139 }
Chris Mason7bb86312007-12-11 09:25:06 -05001140 err = btrfs_insert_extent_backref(trans, extent_root, path,
1141 start, extent_root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -05001142 0, level,
1143 btrfs_disk_key_objectid(&first));
Chris Mason7bb86312007-12-11 09:25:06 -05001144 BUG_ON(err);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001145 free_extent_buffer(eb);
Chris Mason037e6392007-03-07 11:50:24 -05001146 }
Chris Mason7bb86312007-12-11 09:25:06 -05001147 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001148 return 0;
1149}
1150
Chris Masondb945352007-10-15 16:15:53 -04001151static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1152 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -04001153{
Chris Mason1a5bc162007-10-15 16:15:26 -04001154 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001155 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001156
Chris Masonf4b9aa82007-03-27 11:05:53 -04001157 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -04001158 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -04001159 if (buf) {
1160 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001161 u64 transid =
1162 root->fs_info->running_transaction->transid;
Chris Masondc17ff82008-01-08 15:46:30 -05001163 u64 header_transid =
1164 btrfs_header_generation(buf);
1165 if (header_transid == transid) {
Chris Mason55c69072008-01-09 15:55:33 -05001166 clean_tree_block(NULL, root, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001167 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -05001168 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001169 }
Chris Masonf4b9aa82007-03-27 11:05:53 -04001170 }
Chris Mason5f39d392007-10-15 16:14:19 -04001171 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -04001172 }
Yan324ae4d2007-11-16 14:57:08 -05001173 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001174 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -04001175 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -04001176 bytenr, bytenr + num_bytes - 1,
1177 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001178 }
Chris Masonbe744172007-05-06 10:15:01 -04001179 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001180 return 0;
1181}
1182
Chris Masona28ec192007-03-06 20:08:01 -05001183/*
1184 * remove an extent from the root, returns 0 on success
1185 */
Chris Masone089f052007-03-16 16:20:31 -04001186static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001187 *root, u64 bytenr, u64 num_bytes,
1188 u64 root_objectid, u64 ref_generation,
1189 u64 owner_objectid, u64 owner_offset, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -04001190 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001191{
Chris Mason5caf2a02007-04-02 11:20:42 -04001192 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001193 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001194 struct btrfs_fs_info *info = root->fs_info;
1195 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001196 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001197 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001198 int extent_slot = 0;
1199 int found_extent = 0;
1200 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001201 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001202 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001203
Chris Masondb945352007-10-15 16:15:53 -04001204 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001205 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001206 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001207 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001208 if (!path)
1209 return -ENOMEM;
1210
Chris Masonb0331a42008-01-08 15:46:31 -05001211 path->reada = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001212 ret = lookup_extent_backref(trans, extent_root, path,
1213 bytenr, root_objectid,
1214 ref_generation,
1215 owner_objectid, owner_offset, 1);
1216 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001217 struct btrfs_key found_key;
1218 extent_slot = path->slots[0];
1219 while(extent_slot > 0) {
1220 extent_slot--;
1221 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1222 extent_slot);
1223 if (found_key.objectid != bytenr)
1224 break;
1225 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1226 found_key.offset == num_bytes) {
1227 found_extent = 1;
1228 break;
1229 }
1230 if (path->slots[0] - extent_slot > 5)
1231 break;
1232 }
1233 if (!found_extent)
1234 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001235 } else {
1236 btrfs_print_leaf(extent_root, path->nodes[0]);
1237 WARN_ON(1);
1238 printk("Unable to find ref byte nr %Lu root %Lu "
1239 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1240 root_objectid, ref_generation, owner_objectid,
1241 owner_offset);
1242 }
Chris Mason952fcca2008-02-18 16:33:44 -05001243 if (!found_extent) {
1244 btrfs_release_path(extent_root, path);
1245 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1246 if (ret < 0)
1247 return ret;
1248 BUG_ON(ret);
1249 extent_slot = path->slots[0];
1250 }
Chris Mason5f39d392007-10-15 16:14:19 -04001251
1252 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001253 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001254 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001255 refs = btrfs_extent_refs(leaf, ei);
1256 BUG_ON(refs == 0);
1257 refs -= 1;
1258 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001259
Chris Mason5f39d392007-10-15 16:14:19 -04001260 btrfs_mark_buffer_dirty(leaf);
1261
Chris Mason952fcca2008-02-18 16:33:44 -05001262 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1263 /* if the back ref and the extent are next to each other
1264 * they get deleted below in one shot
1265 */
1266 path->slots[0] = extent_slot;
1267 num_to_del = 2;
1268 } else if (found_extent) {
1269 /* otherwise delete the extent back ref */
1270 ret = btrfs_del_item(trans, extent_root, path);
1271 BUG_ON(ret);
1272 /* if refs are 0, we need to setup the path for deletion */
1273 if (refs == 0) {
1274 btrfs_release_path(extent_root, path);
1275 ret = btrfs_search_slot(trans, extent_root, &key, path,
1276 -1, 1);
1277 if (ret < 0)
1278 return ret;
1279 BUG_ON(ret);
1280 }
1281 }
1282
Chris Masoncf27e1e2007-03-13 09:49:06 -04001283 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001284 u64 super_used;
1285 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04001286
1287 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -04001288 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -05001289 if (ret > 0)
1290 mark_free = 1;
1291 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001292 }
1293
Josef Bacik58176a92007-08-29 15:47:34 -04001294 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001295 super_used = btrfs_super_bytes_used(&info->super_copy);
1296 btrfs_set_super_bytes_used(&info->super_copy,
1297 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001298
1299 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001300 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001301 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001302 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001303 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1304 num_to_del);
Chris Mason54aa1f42007-06-22 14:16:25 -04001305 if (ret) {
1306 return ret;
1307 }
Chris Masondb945352007-10-15 16:15:53 -04001308 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001309 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001310 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05001311 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001312 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001313 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05001314 return ret;
1315}
1316
1317/*
Chris Masonfec577f2007-02-26 10:40:21 -05001318 * find all the blocks marked as pending in the radix tree and remove
1319 * them from the extent map
1320 */
Chris Masone089f052007-03-16 16:20:31 -04001321static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1322 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05001323{
1324 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001325 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001326 u64 start;
1327 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001328 struct extent_io_tree *pending_del;
1329 struct extent_io_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -04001330
Chris Mason1a5bc162007-10-15 16:15:26 -04001331 pending_del = &extent_root->fs_info->pending_del;
1332 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -05001333
1334 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001335 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1336 EXTENT_LOCKED);
1337 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05001338 break;
Yan324ae4d2007-11-16 14:57:08 -05001339 update_pinned_extents(extent_root, start, end + 1 - start, 1);
Chris Mason1a5bc162007-10-15 16:15:26 -04001340 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1341 GFP_NOFS);
1342 ret = __free_extent(trans, extent_root,
Chris Mason7bb86312007-12-11 09:25:06 -05001343 start, end + 1 - start,
1344 extent_root->root_key.objectid,
1345 0, 0, 0, 0, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001346 if (ret)
1347 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001348 }
Chris Masone20d96d2007-03-22 12:13:20 -04001349 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05001350}
1351
1352/*
1353 * remove an extent from the root, returns 0 on success
1354 */
Chris Masone089f052007-03-16 16:20:31 -04001355int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001356 *root, u64 bytenr, u64 num_bytes,
1357 u64 root_objectid, u64 ref_generation,
1358 u64 owner_objectid, u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05001359{
Chris Mason9f5fae22007-03-20 14:38:32 -04001360 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05001361 int pending_ret;
1362 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05001363
Chris Masondb945352007-10-15 16:15:53 -04001364 WARN_ON(num_bytes < root->sectorsize);
Chris Mason7bb86312007-12-11 09:25:06 -05001365 if (!root->ref_cows)
1366 ref_generation = 0;
1367
Chris Masona28ec192007-03-06 20:08:01 -05001368 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -04001369 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -05001370 return 0;
1371 }
Chris Mason7bb86312007-12-11 09:25:06 -05001372 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1373 ref_generation, owner_objectid, owner_offset,
1374 pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001375 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05001376 return ret ? ret : pending_ret;
1377}
1378
Chris Mason87ee04e2007-11-30 11:30:34 -05001379static u64 stripe_align(struct btrfs_root *root, u64 val)
1380{
1381 u64 mask = ((u64)root->stripesize - 1);
1382 u64 ret = (val + mask) & ~mask;
1383 return ret;
1384}
1385
Chris Masonfec577f2007-02-26 10:40:21 -05001386/*
1387 * walks the btree of allocated extents and find a hole of a given size.
1388 * The key ins is changed to record the hole:
1389 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04001390 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001391 * ins->offset == number of blocks
1392 * Any available blocks before search_start are skipped.
1393 */
Chris Mason98ed5172008-01-03 10:01:48 -05001394static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1395 struct btrfs_root *orig_root,
1396 u64 num_bytes, u64 empty_size,
1397 u64 search_start, u64 search_end,
1398 u64 hint_byte, struct btrfs_key *ins,
1399 u64 exclude_start, u64 exclude_nr,
1400 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001401{
Chris Mason87ee04e2007-11-30 11:30:34 -05001402 int ret;
Chris Masonbe744172007-05-06 10:15:01 -04001403 u64 orig_search_start = search_start;
Chris Mason9f5fae22007-03-20 14:38:32 -04001404 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001405 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001406 u64 total_needed = num_bytes;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001407 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001408 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001409 int wrapped = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001410
Chris Masondb945352007-10-15 16:15:53 -04001411 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001412 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1413
Chris Mason7f93bf82008-03-24 15:01:28 -04001414 if (search_end == (u64)-1)
1415 search_end = btrfs_super_total_bytes(&info->super_copy);
Chris Mason0b86a832008-03-24 15:01:56 -04001416
Chris Masondb945352007-10-15 16:15:53 -04001417 if (hint_byte) {
1418 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001419 if (!block_group)
1420 hint_byte = search_start;
Chris Masonbe744172007-05-06 10:15:01 -04001421 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -04001422 hint_byte, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001423 } else {
1424 block_group = btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001425 trans->block_group,
1426 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001427 }
1428
Chris Mason6702ed42007-08-07 16:15:09 -04001429 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001430
Chris Masonbe744172007-05-06 10:15:01 -04001431check_failed:
Chris Mason70b043f2007-12-13 09:02:46 -05001432 if (!block_group) {
1433 block_group = btrfs_lookup_block_group(info, search_start);
1434 if (!block_group)
1435 block_group = btrfs_lookup_block_group(info,
1436 orig_search_start);
1437 }
Chris Mason0b86a832008-03-24 15:01:56 -04001438 ret = find_search_start(root, &block_group, &search_start,
1439 total_needed, data);
1440 if (ret)
1441 goto error;
1442
Chris Mason87ee04e2007-11-30 11:30:34 -05001443 search_start = stripe_align(root, search_start);
Chris Masonfec577f2007-02-26 10:40:21 -05001444 ins->objectid = search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001445 ins->offset = num_bytes;
Chris Masone37c9e62007-05-09 20:13:14 -04001446
Chris Masondb945352007-10-15 16:15:53 -04001447 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001448 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04001449
1450 if (ins->objectid + num_bytes >
1451 block_group->key.objectid + block_group->key.offset) {
Chris Masone19caa52007-10-15 16:17:44 -04001452 search_start = block_group->key.objectid +
1453 block_group->key.offset;
1454 goto new_group;
1455 }
Chris Mason0b86a832008-03-24 15:01:56 -04001456
Chris Mason1a5bc162007-10-15 16:15:26 -04001457 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001458 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1459 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001460 goto new_group;
1461 }
Chris Mason0b86a832008-03-24 15:01:56 -04001462
Chris Mason1a5bc162007-10-15 16:15:26 -04001463 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001464 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1465 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001466 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001467 }
Chris Mason0b86a832008-03-24 15:01:56 -04001468
Chris Masondb945352007-10-15 16:15:53 -04001469 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001470 ins->objectid < exclude_start + exclude_nr)) {
1471 search_start = exclude_start + exclude_nr;
1472 goto new_group;
1473 }
Chris Mason0b86a832008-03-24 15:01:56 -04001474
1475 if (!(data & BLOCK_GROUP_DATA)) {
Chris Mason5276aed2007-06-11 21:33:38 -04001476 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001477 if (block_group)
1478 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001479 }
Chris Masondb945352007-10-15 16:15:53 -04001480 ins->offset = num_bytes;
Chris Masonfec577f2007-02-26 10:40:21 -05001481 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001482
1483new_group:
Chris Masondb945352007-10-15 16:15:53 -04001484 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001485enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001486 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001487 if (full_scan) {
1488 ret = -ENOSPC;
1489 goto error;
1490 }
Chris Mason6702ed42007-08-07 16:15:09 -04001491 if (wrapped) {
1492 if (!full_scan)
1493 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001494 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001495 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001496 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001497 }
Chris Mason5276aed2007-06-11 21:33:38 -04001498 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001499 cond_resched();
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001500 block_group = btrfs_find_block_group(root, block_group,
1501 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001502 goto check_failed;
1503
Chris Mason0f70abe2007-02-28 16:46:22 -05001504error:
Chris Mason0f70abe2007-02-28 16:46:22 -05001505 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001506}
Chris Masonfec577f2007-02-26 10:40:21 -05001507/*
Chris Masonfec577f2007-02-26 10:40:21 -05001508 * finds a free extent and does all the dirty work required for allocation
1509 * returns the key for the extent through ins, and a tree buffer for
1510 * the first block of the extent through buf.
1511 *
1512 * returns 0 if everything worked, non-zero otherwise.
1513 */
Chris Mason4d775672007-04-20 20:23:12 -04001514int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -05001515 struct btrfs_root *root,
1516 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1517 u64 owner, u64 owner_offset,
1518 u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001519 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001520{
1521 int ret;
1522 int pending_ret;
Chris Masonc31f8832008-01-08 15:46:31 -05001523 u64 super_used;
1524 u64 root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001525 u64 search_start = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05001526 u64 new_hint;
Chris Mason47e4bb92008-02-01 14:51:59 -05001527 u32 sizes[2];
Chris Mason1261ec42007-03-20 20:35:03 -04001528 struct btrfs_fs_info *info = root->fs_info;
1529 struct btrfs_root *extent_root = info->extent_root;
Chris Mason47e4bb92008-02-01 14:51:59 -05001530 struct btrfs_extent_item *extent_item;
1531 struct btrfs_extent_ref *ref;
Chris Mason7bb86312007-12-11 09:25:06 -05001532 struct btrfs_path *path;
Chris Mason47e4bb92008-02-01 14:51:59 -05001533 struct btrfs_key keys[2];
Chris Mason8f662a72008-01-02 10:01:11 -05001534
Chris Mason0b86a832008-03-24 15:01:56 -04001535 if (data)
1536 data = BLOCK_GROUP_DATA;
1537 else if (root == root->fs_info->chunk_root)
1538 data = BLOCK_GROUP_SYSTEM;
1539 else
1540 data = BLOCK_GROUP_METADATA;
1541
Chris Mason8f662a72008-01-02 10:01:11 -05001542 new_hint = max(hint_byte, root->fs_info->alloc_start);
Chris Masonedbd8d42007-12-21 16:27:24 -05001543 if (new_hint < btrfs_super_total_bytes(&info->super_copy))
1544 hint_byte = new_hint;
Chris Mason8f662a72008-01-02 10:01:11 -05001545
Chris Masondb945352007-10-15 16:15:53 -04001546 WARN_ON(num_bytes < root->sectorsize);
1547 ret = find_free_extent(trans, root, num_bytes, empty_size,
1548 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001549 trans->alloc_exclude_start,
1550 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001551 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001552 if (ret)
1553 return ret;
1554
Josef Bacik58176a92007-08-29 15:47:34 -04001555 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001556 super_used = btrfs_super_bytes_used(&info->super_copy);
1557 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001558
Josef Bacik58176a92007-08-29 15:47:34 -04001559 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001560 root_used = btrfs_root_used(&root->root_item);
1561 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001562
Chris Masonf510cfe2007-10-15 16:14:48 -04001563 clear_extent_dirty(&root->fs_info->free_space_cache,
1564 ins->objectid, ins->objectid + ins->offset - 1,
1565 GFP_NOFS);
1566
Chris Mason26b80032007-08-08 20:17:12 -04001567 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001568 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1569 ins->objectid + ins->offset - 1,
1570 EXTENT_LOCKED, GFP_NOFS);
Chris Mason26b80032007-08-08 20:17:12 -04001571 goto update_block;
1572 }
1573
1574 WARN_ON(trans->alloc_exclude_nr);
1575 trans->alloc_exclude_start = ins->objectid;
1576 trans->alloc_exclude_nr = ins->offset;
Chris Mason037e6392007-03-07 11:50:24 -05001577
Chris Mason47e4bb92008-02-01 14:51:59 -05001578 memcpy(&keys[0], ins, sizeof(*ins));
1579 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
1580 owner, owner_offset);
1581 keys[1].objectid = ins->objectid;
1582 keys[1].type = BTRFS_EXTENT_REF_KEY;
1583 sizes[0] = sizeof(*extent_item);
1584 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05001585
1586 path = btrfs_alloc_path();
1587 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05001588
1589 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
1590 sizes, 2);
Chris Mason26b80032007-08-08 20:17:12 -04001591
Chris Masonccd467d2007-06-28 15:57:36 -04001592 BUG_ON(ret);
Chris Mason47e4bb92008-02-01 14:51:59 -05001593 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1594 struct btrfs_extent_item);
1595 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
1596 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1597 struct btrfs_extent_ref);
1598
1599 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
1600 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
1601 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
1602 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
1603
1604 btrfs_mark_buffer_dirty(path->nodes[0]);
1605
1606 trans->alloc_exclude_start = 0;
1607 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001608 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001609 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001610 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001611
Chris Masone37c9e62007-05-09 20:13:14 -04001612 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001613 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001614 }
1615 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001616 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001617 }
Chris Mason26b80032007-08-08 20:17:12 -04001618
1619update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04001620 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05001621 if (ret) {
1622 printk("update block group failed for %Lu %Lu\n",
1623 ins->objectid, ins->offset);
1624 BUG();
1625 }
Chris Mason037e6392007-03-07 11:50:24 -05001626 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001627}
1628
1629/*
1630 * helper function to allocate a block for a given tree
1631 * returns the tree buffer or NULL.
1632 */
Chris Mason5f39d392007-10-15 16:14:19 -04001633struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001634 struct btrfs_root *root,
Chris Mason7bb86312007-12-11 09:25:06 -05001635 u32 blocksize,
1636 u64 root_objectid, u64 hint,
1637 u64 empty_size)
1638{
1639 u64 ref_generation;
1640
1641 if (root->ref_cows)
1642 ref_generation = trans->transid;
1643 else
1644 ref_generation = 0;
1645
1646
1647 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1648 ref_generation, 0, 0, hint, empty_size);
1649}
1650
1651/*
1652 * helper function to allocate a block for a given tree
1653 * returns the tree buffer or NULL.
1654 */
1655struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1656 struct btrfs_root *root,
1657 u32 blocksize,
1658 u64 root_objectid,
1659 u64 ref_generation,
1660 u64 first_objectid,
1661 int level,
1662 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001663 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001664{
Chris Masone2fa7222007-03-12 16:22:34 -04001665 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001666 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001667 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001668
Chris Mason7bb86312007-12-11 09:25:06 -05001669 ret = btrfs_alloc_extent(trans, root, blocksize,
1670 root_objectid, ref_generation,
Chris Masonf6dbff52007-12-13 11:13:32 -05001671 level, first_objectid, empty_size, hint,
Chris Masondb945352007-10-15 16:15:53 -04001672 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001673 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001674 BUG_ON(ret > 0);
1675 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001676 }
Chris Masondb945352007-10-15 16:15:53 -04001677 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001678 if (!buf) {
Chris Mason7bb86312007-12-11 09:25:06 -05001679 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1680 root->root_key.objectid, ref_generation,
1681 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001682 return ERR_PTR(-ENOMEM);
1683 }
Chris Mason55c69072008-01-09 15:55:33 -05001684 btrfs_set_header_generation(buf, trans->transid);
1685 clean_tree_block(trans, root, buf);
1686 wait_on_tree_block_writeback(root, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001687 btrfs_set_buffer_uptodate(buf);
Chris Mason55c69072008-01-09 15:55:33 -05001688
1689 if (PageDirty(buf->first_page)) {
1690 printk("page %lu dirty\n", buf->first_page->index);
1691 WARN_ON(1);
1692 }
1693
Chris Mason5f39d392007-10-15 16:14:19 -04001694 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1695 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001696 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->io_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -04001697 buf->start, buf->start + buf->len - 1,
1698 EXTENT_CSUM, GFP_NOFS);
1699 buf->flags |= EXTENT_CSUM;
Chris Mason9afbb0b2008-02-15 13:19:35 -05001700 if (!btrfs_test_opt(root, SSD))
1701 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001702 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001703 return buf;
1704}
Chris Masona28ec192007-03-06 20:08:01 -05001705
Chris Mason98ed5172008-01-03 10:01:48 -05001706static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
1707 struct btrfs_root *root,
1708 struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001709{
Chris Mason7bb86312007-12-11 09:25:06 -05001710 u64 leaf_owner;
1711 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04001712 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001713 struct btrfs_file_extent_item *fi;
1714 int i;
1715 int nritems;
1716 int ret;
1717
Chris Mason5f39d392007-10-15 16:14:19 -04001718 BUG_ON(!btrfs_is_leaf(leaf));
1719 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05001720 leaf_owner = btrfs_header_owner(leaf);
1721 leaf_generation = btrfs_header_generation(leaf);
1722
Chris Mason6407bf62007-03-27 06:33:00 -04001723 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001724 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001725
1726 btrfs_item_key_to_cpu(leaf, &key, i);
1727 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001728 continue;
1729 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001730 if (btrfs_file_extent_type(leaf, fi) ==
1731 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04001732 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001733 /*
1734 * FIXME make sure to insert a trans record that
1735 * repeats the snapshot del on crash
1736 */
Chris Masondb945352007-10-15 16:15:53 -04001737 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1738 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001739 continue;
Chris Masondb945352007-10-15 16:15:53 -04001740 ret = btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001741 btrfs_file_extent_disk_num_bytes(leaf, fi),
1742 leaf_owner, leaf_generation,
1743 key.objectid, key.offset, 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001744 BUG_ON(ret);
1745 }
1746 return 0;
1747}
1748
Chris Mason98ed5172008-01-03 10:01:48 -05001749static void noinline reada_walk_down(struct btrfs_root *root,
Chris Masonbea495e2008-01-24 16:13:14 -05001750 struct extent_buffer *node,
1751 int slot)
Chris Masone0115992007-06-19 16:23:05 -04001752{
Chris Masondb945352007-10-15 16:15:53 -04001753 u64 bytenr;
Chris Masonbea495e2008-01-24 16:13:14 -05001754 u64 last = 0;
1755 u32 nritems;
Chris Masone0115992007-06-19 16:23:05 -04001756 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001757 u32 blocksize;
Chris Masonbea495e2008-01-24 16:13:14 -05001758 int ret;
1759 int i;
1760 int level;
1761 int skipped = 0;
Chris Masone0115992007-06-19 16:23:05 -04001762
Chris Mason5f39d392007-10-15 16:14:19 -04001763 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001764 level = btrfs_header_level(node);
Chris Masonbea495e2008-01-24 16:13:14 -05001765 if (level)
1766 return;
1767
1768 for (i = slot; i < nritems && skipped < 32; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001769 bytenr = btrfs_node_blockptr(node, i);
Chris Masonbea495e2008-01-24 16:13:14 -05001770 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
1771 (last > bytenr && last - bytenr > 32 * 1024))) {
1772 skipped++;
Chris Masone0115992007-06-19 16:23:05 -04001773 continue;
Chris Masonbea495e2008-01-24 16:13:14 -05001774 }
1775 blocksize = btrfs_level_size(root, level - 1);
1776 if (i != slot) {
1777 ret = lookup_extent_ref(NULL, root, bytenr,
1778 blocksize, &refs);
1779 BUG_ON(ret);
1780 if (refs != 1) {
1781 skipped++;
1782 continue;
1783 }
1784 }
Chris Mason409eb952007-08-08 20:17:12 -04001785 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001786 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Masonbea495e2008-01-24 16:13:14 -05001787 last = bytenr + blocksize;
Chris Mason409eb952007-08-08 20:17:12 -04001788 cond_resched();
1789 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001790 if (ret)
1791 break;
1792 }
1793}
1794
Chris Mason9aca1d52007-03-13 11:09:37 -04001795/*
1796 * helper function for drop_snapshot, this walks down the tree dropping ref
1797 * counts as it goes.
1798 */
Chris Mason98ed5172008-01-03 10:01:48 -05001799static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
1800 struct btrfs_root *root,
1801 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001802{
Chris Mason7bb86312007-12-11 09:25:06 -05001803 u64 root_owner;
1804 u64 root_gen;
1805 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001806 struct extent_buffer *next;
1807 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05001808 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04001809 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05001810 int ret;
1811 u32 refs;
1812
Chris Mason5caf2a02007-04-02 11:20:42 -04001813 WARN_ON(*level < 0);
1814 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001815 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001816 path->nodes[*level]->start,
1817 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001818 BUG_ON(ret);
1819 if (refs > 1)
1820 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001821
Chris Mason9aca1d52007-03-13 11:09:37 -04001822 /*
1823 * walk down to the last node level and free all the leaves
1824 */
Chris Mason6407bf62007-03-27 06:33:00 -04001825 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001826 WARN_ON(*level < 0);
1827 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001828 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001829
Chris Mason5f39d392007-10-15 16:14:19 -04001830 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001831 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001832
Chris Mason7518a232007-03-12 12:01:18 -04001833 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001834 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001835 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001836 if (*level == 0) {
1837 ret = drop_leaf_ref(trans, root, cur);
1838 BUG_ON(ret);
1839 break;
1840 }
Chris Masondb945352007-10-15 16:15:53 -04001841 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1842 blocksize = btrfs_level_size(root, *level - 1);
1843 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001844 BUG_ON(ret);
1845 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05001846 parent = path->nodes[*level];
1847 root_owner = btrfs_header_owner(parent);
1848 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05001849 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04001850 ret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001851 blocksize, root_owner,
1852 root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001853 BUG_ON(ret);
1854 continue;
1855 }
Chris Masondb945352007-10-15 16:15:53 -04001856 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001857 if (!next || !btrfs_buffer_uptodate(next)) {
1858 free_extent_buffer(next);
Chris Masonbea495e2008-01-24 16:13:14 -05001859 reada_walk_down(root, cur, path->slots[*level]);
Chris Masone9d0b132007-08-10 14:06:19 -04001860 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001861 next = read_tree_block(root, bytenr, blocksize);
Chris Masone9d0b132007-08-10 14:06:19 -04001862 mutex_lock(&root->fs_info->fs_mutex);
1863
1864 /* we dropped the lock, check one more time */
Chris Masondb945352007-10-15 16:15:53 -04001865 ret = lookup_extent_ref(trans, root, bytenr,
1866 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04001867 BUG_ON(ret);
1868 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05001869 parent = path->nodes[*level];
1870 root_owner = btrfs_header_owner(parent);
1871 root_gen = btrfs_header_generation(parent);
1872
Chris Masone9d0b132007-08-10 14:06:19 -04001873 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001874 free_extent_buffer(next);
Chris Mason7bb86312007-12-11 09:25:06 -05001875 ret = btrfs_free_extent(trans, root, bytenr,
1876 blocksize,
1877 root_owner,
1878 root_gen, 0, 0, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001879 BUG_ON(ret);
1880 continue;
1881 }
1882 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001883 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001884 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001885 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001886 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001887 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001888 path->slots[*level] = 0;
1889 }
1890out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001891 WARN_ON(*level < 0);
1892 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05001893
1894 if (path->nodes[*level] == root->node) {
1895 root_owner = root->root_key.objectid;
1896 parent = path->nodes[*level];
1897 } else {
1898 parent = path->nodes[*level + 1];
1899 root_owner = btrfs_header_owner(parent);
1900 }
1901
1902 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001903 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001904 path->nodes[*level]->len,
1905 root_owner, root_gen, 0, 0, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001906 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001907 path->nodes[*level] = NULL;
1908 *level += 1;
1909 BUG_ON(ret);
1910 return 0;
1911}
1912
Chris Mason9aca1d52007-03-13 11:09:37 -04001913/*
1914 * helper for dropping snapshots. This walks back up the tree in the path
1915 * to find the first node higher up where we haven't yet gone through
1916 * all the slots
1917 */
Chris Mason98ed5172008-01-03 10:01:48 -05001918static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
1919 struct btrfs_root *root,
1920 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001921{
Chris Mason7bb86312007-12-11 09:25:06 -05001922 u64 root_owner;
1923 u64 root_gen;
1924 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001925 int i;
1926 int slot;
1927 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001928
Chris Mason234b63a2007-03-13 10:46:10 -04001929 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001930 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001931 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1932 struct extent_buffer *node;
1933 struct btrfs_disk_key disk_key;
1934 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001935 path->slots[i]++;
1936 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001937 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001938 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001939 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001940 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001941 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001942 return 0;
1943 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05001944 if (path->nodes[*level] == root->node) {
1945 root_owner = root->root_key.objectid;
1946 root_gen =
1947 btrfs_header_generation(path->nodes[*level]);
1948 } else {
1949 struct extent_buffer *node;
1950 node = path->nodes[*level + 1];
1951 root_owner = btrfs_header_owner(node);
1952 root_gen = btrfs_header_generation(node);
1953 }
Chris Masone089f052007-03-16 16:20:31 -04001954 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001955 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001956 path->nodes[*level]->len,
1957 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001958 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001959 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001960 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001961 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001962 }
1963 }
1964 return 1;
1965}
1966
Chris Mason9aca1d52007-03-13 11:09:37 -04001967/*
1968 * drop the reference count on the tree rooted at 'snap'. This traverses
1969 * the tree freeing any blocks that have a ref count of zero after being
1970 * decremented.
1971 */
Chris Masone089f052007-03-16 16:20:31 -04001972int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001973 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001974{
Chris Mason3768f362007-03-13 16:47:54 -04001975 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001976 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001977 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001978 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001979 int i;
1980 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001981 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001982
Chris Mason5caf2a02007-04-02 11:20:42 -04001983 path = btrfs_alloc_path();
1984 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001985
Chris Mason5f39d392007-10-15 16:14:19 -04001986 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05001987 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001988 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1989 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04001990 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04001991 path->slots[level] = 0;
1992 } else {
1993 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001994 struct btrfs_disk_key found_key;
1995 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001996
Chris Mason9f3a7422007-08-07 15:52:19 -04001997 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001998 level = root_item->drop_level;
1999 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002000 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04002001 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04002002 ret = wret;
2003 goto out;
2004 }
Chris Mason5f39d392007-10-15 16:14:19 -04002005 node = path->nodes[level];
2006 btrfs_node_key(node, &found_key, path->slots[level]);
2007 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2008 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04002009 }
Chris Mason20524f02007-03-10 06:35:47 -05002010 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002011 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002012 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002013 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002014 if (wret < 0)
2015 ret = wret;
2016
Chris Mason5caf2a02007-04-02 11:20:42 -04002017 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002018 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002019 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002020 if (wret < 0)
2021 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04002022 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04002023 break;
Chris Mason20524f02007-03-10 06:35:47 -05002024 }
Chris Mason83e15a22007-03-12 09:03:27 -04002025 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002026 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04002027 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04002028 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04002029 }
Chris Mason20524f02007-03-10 06:35:47 -05002030 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002031out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002032 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04002033 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05002034}
Chris Mason9078a3e2007-04-26 16:46:15 -04002035
Chris Masonbe744172007-05-06 10:15:01 -04002036int btrfs_free_block_groups(struct btrfs_fs_info *info)
2037{
Chris Masonf510cfe2007-10-15 16:14:48 -04002038 u64 start;
2039 u64 end;
Yanb97f9202007-11-01 11:28:41 -04002040 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04002041 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04002042 while(1) {
2043 ret = find_first_extent_bit(&info->block_group_cache, 0,
2044 &start, &end, (unsigned int)-1);
2045 if (ret)
2046 break;
Yanb97f9202007-11-01 11:28:41 -04002047 ret = get_state_private(&info->block_group_cache, start, &ptr);
2048 if (!ret)
2049 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04002050 clear_extent_bits(&info->block_group_cache, start,
2051 end, (unsigned int)-1, GFP_NOFS);
2052 }
Chris Masone37c9e62007-05-09 20:13:14 -04002053 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04002054 ret = find_first_extent_bit(&info->free_space_cache, 0,
2055 &start, &end, EXTENT_DIRTY);
2056 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04002057 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04002058 clear_extent_dirty(&info->free_space_cache, start,
2059 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04002060 }
Chris Masonbe744172007-05-06 10:15:01 -04002061 return 0;
2062}
2063
Chris Mason98ed5172008-01-03 10:01:48 -05002064static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2065 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05002066{
2067 u64 page_start;
2068 u64 page_end;
2069 u64 delalloc_start;
2070 u64 existing_delalloc;
2071 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05002072 unsigned long i;
2073 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05002074 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05002075 struct file_ra_state *ra;
2076
2077 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002078
2079 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05002080 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05002081 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2082
Chris Mason4313b392008-01-03 09:08:48 -05002083 file_ra_state_init(ra, inode->i_mapping);
2084 btrfs_force_ra(inode->i_mapping, ra, NULL, i, last_index);
2085 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05002086
Chris Mason4313b392008-01-03 09:08:48 -05002087 for (; i <= last_index; i++) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002088 page = grab_cache_page(inode->i_mapping, i);
2089 if (!page)
2090 goto out_unlock;
2091 if (!PageUptodate(page)) {
2092 btrfs_readpage(NULL, page);
2093 lock_page(page);
2094 if (!PageUptodate(page)) {
2095 unlock_page(page);
2096 page_cache_release(page);
2097 goto out_unlock;
2098 }
2099 }
2100 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2101 page_end = page_start + PAGE_CACHE_SIZE - 1;
2102
Chris Masond1310b22008-01-24 16:13:08 -05002103 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002104
2105 delalloc_start = page_start;
Chris Masond1310b22008-01-24 16:13:08 -05002106 existing_delalloc = count_range_bits(io_tree,
2107 &delalloc_start, page_end,
2108 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Masonedbd8d42007-12-21 16:27:24 -05002109
Chris Masond1310b22008-01-24 16:13:08 -05002110 set_extent_delalloc(io_tree, page_start,
Chris Masonedbd8d42007-12-21 16:27:24 -05002111 page_end, GFP_NOFS);
2112
Chris Masond1310b22008-01-24 16:13:08 -05002113 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002114 set_page_dirty(page);
2115 unlock_page(page);
2116 page_cache_release(page);
2117 }
2118
2119out_unlock:
2120 mutex_unlock(&inode->i_mutex);
2121 return 0;
2122}
2123
Chris Mason4313b392008-01-03 09:08:48 -05002124/*
2125 * note, this releases the path
2126 */
Chris Mason98ed5172008-01-03 10:01:48 -05002127static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05002128 struct btrfs_path *path,
Chris Mason4313b392008-01-03 09:08:48 -05002129 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002130{
2131 struct inode *inode;
2132 struct btrfs_root *found_root;
Chris Mason4313b392008-01-03 09:08:48 -05002133 struct btrfs_key *root_location;
2134 struct btrfs_extent_ref *ref;
2135 u64 ref_root;
2136 u64 ref_gen;
2137 u64 ref_objectid;
2138 u64 ref_offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05002139 int ret;
2140
Chris Mason4313b392008-01-03 09:08:48 -05002141 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2142 struct btrfs_extent_ref);
2143 ref_root = btrfs_ref_root(path->nodes[0], ref);
2144 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2145 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2146 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2147 btrfs_release_path(extent_root, path);
2148
2149 root_location = kmalloc(sizeof(*root_location), GFP_NOFS);
2150 root_location->objectid = ref_root;
Chris Masonedbd8d42007-12-21 16:27:24 -05002151 if (ref_gen == 0)
Chris Mason4313b392008-01-03 09:08:48 -05002152 root_location->offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002153 else
Chris Mason4313b392008-01-03 09:08:48 -05002154 root_location->offset = (u64)-1;
2155 root_location->type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05002156
2157 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Mason4313b392008-01-03 09:08:48 -05002158 root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002159 BUG_ON(!found_root);
Chris Mason4313b392008-01-03 09:08:48 -05002160 kfree(root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002161
2162 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2163 mutex_unlock(&extent_root->fs_info->fs_mutex);
2164 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2165 ref_objectid, found_root);
2166 if (inode->i_state & I_NEW) {
2167 /* the inode and parent dir are two different roots */
2168 BTRFS_I(inode)->root = found_root;
2169 BTRFS_I(inode)->location.objectid = ref_objectid;
2170 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2171 BTRFS_I(inode)->location.offset = 0;
2172 btrfs_read_locked_inode(inode);
2173 unlock_new_inode(inode);
2174
2175 }
2176 /* this can happen if the reference is not against
2177 * the latest version of the tree root
2178 */
2179 if (is_bad_inode(inode)) {
2180 mutex_lock(&extent_root->fs_info->fs_mutex);
2181 goto out;
2182 }
2183 relocate_inode_pages(inode, ref_offset, extent_key->offset);
2184 /* FIXME, data=ordered will help get rid of this */
2185 filemap_fdatawrite(inode->i_mapping);
2186 iput(inode);
2187 mutex_lock(&extent_root->fs_info->fs_mutex);
2188 } else {
2189 struct btrfs_trans_handle *trans;
2190 struct btrfs_key found_key;
2191 struct extent_buffer *eb;
2192 int level;
2193 int i;
2194
2195 trans = btrfs_start_transaction(found_root, 1);
2196 eb = read_tree_block(found_root, extent_key->objectid,
2197 extent_key->offset);
2198 level = btrfs_header_level(eb);
2199
2200 if (level == 0)
2201 btrfs_item_key_to_cpu(eb, &found_key, 0);
2202 else
2203 btrfs_node_key_to_cpu(eb, &found_key, 0);
2204
2205 free_extent_buffer(eb);
2206
2207 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05002208 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002209 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2210 0, 1);
2211 path->lowest_level = 0;
2212 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2213 if (!path->nodes[i])
2214 break;
2215 free_extent_buffer(path->nodes[i]);
2216 path->nodes[i] = NULL;
2217 }
2218 btrfs_release_path(found_root, path);
2219 btrfs_end_transaction(trans, found_root);
2220 }
2221
2222out:
2223 return 0;
2224}
2225
Chris Mason98ed5172008-01-03 10:01:48 -05002226static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2227 struct btrfs_path *path,
2228 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002229{
2230 struct btrfs_key key;
2231 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002232 struct extent_buffer *leaf;
Chris Masonedbd8d42007-12-21 16:27:24 -05002233 u32 nritems;
2234 u32 item_size;
2235 int ret = 0;
2236
2237 key.objectid = extent_key->objectid;
2238 key.type = BTRFS_EXTENT_REF_KEY;
2239 key.offset = 0;
2240
2241 while(1) {
2242 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2243
Chris Masonedbd8d42007-12-21 16:27:24 -05002244 if (ret < 0)
2245 goto out;
2246
2247 ret = 0;
2248 leaf = path->nodes[0];
2249 nritems = btrfs_header_nritems(leaf);
2250 if (path->slots[0] == nritems)
2251 goto out;
2252
2253 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2254 if (found_key.objectid != extent_key->objectid)
2255 break;
2256
2257 if (found_key.type != BTRFS_EXTENT_REF_KEY)
2258 break;
2259
2260 key.offset = found_key.offset + 1;
2261 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2262
Chris Mason4313b392008-01-03 09:08:48 -05002263 ret = relocate_one_reference(extent_root, path, extent_key);
Chris Masonedbd8d42007-12-21 16:27:24 -05002264 if (ret)
2265 goto out;
2266 }
2267 ret = 0;
2268out:
2269 btrfs_release_path(extent_root, path);
2270 return ret;
2271}
2272
Chris Masonedbd8d42007-12-21 16:27:24 -05002273int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
2274{
2275 struct btrfs_trans_handle *trans;
2276 struct btrfs_root *tree_root = root->fs_info->tree_root;
2277 struct btrfs_path *path;
2278 u64 cur_byte;
2279 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05002280 struct btrfs_fs_info *info = root->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05002281 struct extent_io_tree *block_group_cache;
Chris Masonedbd8d42007-12-21 16:27:24 -05002282 struct btrfs_key key;
Yan73e48b22008-01-03 14:14:39 -05002283 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002284 struct extent_buffer *leaf;
2285 u32 nritems;
2286 int ret;
Chris Mason725c8462008-01-04 16:47:16 -05002287 int progress = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002288
2289 btrfs_set_super_total_bytes(&info->super_copy, new_size);
Chris Masonc31f8832008-01-08 15:46:31 -05002290 clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
2291 GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002292 block_group_cache = &info->block_group_cache;
2293 path = btrfs_alloc_path();
2294 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05002295 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002296
2297again:
2298 total_found = 0;
2299 key.objectid = new_size;
Chris Masonedbd8d42007-12-21 16:27:24 -05002300 key.offset = 0;
2301 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05002302 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05002303
Yan73e48b22008-01-03 14:14:39 -05002304 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2305 if (ret < 0)
2306 goto out;
2307
Chris Mason0b86a832008-03-24 15:01:56 -04002308 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yan73e48b22008-01-03 14:14:39 -05002309 if (ret < 0)
2310 goto out;
2311 if (ret == 0) {
2312 leaf = path->nodes[0];
2313 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2314 if (found_key.objectid + found_key.offset > new_size) {
2315 cur_byte = found_key.objectid;
2316 key.objectid = cur_byte;
2317 }
2318 }
2319 btrfs_release_path(root, path);
2320
2321 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002322 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2323 if (ret < 0)
2324 goto out;
Yan73e48b22008-01-03 14:14:39 -05002325
Chris Masonedbd8d42007-12-21 16:27:24 -05002326 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05002327 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05002328next:
2329 if (path->slots[0] >= nritems) {
2330 ret = btrfs_next_leaf(root, path);
2331 if (ret < 0)
2332 goto out;
2333 if (ret == 1) {
2334 ret = 0;
2335 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05002336 }
Yan73e48b22008-01-03 14:14:39 -05002337 leaf = path->nodes[0];
2338 nritems = btrfs_header_nritems(leaf);
2339 }
2340
2341 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05002342
2343 if (progress && need_resched()) {
2344 memcpy(&key, &found_key, sizeof(key));
2345 mutex_unlock(&root->fs_info->fs_mutex);
2346 cond_resched();
2347 mutex_lock(&root->fs_info->fs_mutex);
2348 btrfs_release_path(root, path);
2349 btrfs_search_slot(NULL, root, &key, path, 0, 0);
2350 progress = 0;
2351 goto next;
2352 }
2353 progress = 1;
2354
Yan73e48b22008-01-03 14:14:39 -05002355 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
2356 found_key.objectid + found_key.offset <= cur_byte) {
2357 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05002358 goto next;
2359 }
Yan73e48b22008-01-03 14:14:39 -05002360
Chris Masonedbd8d42007-12-21 16:27:24 -05002361 total_found++;
2362 cur_byte = found_key.objectid + found_key.offset;
2363 key.objectid = cur_byte;
2364 btrfs_release_path(root, path);
2365 ret = relocate_one_extent(root, path, &found_key);
2366 }
2367
2368 btrfs_release_path(root, path);
2369
2370 if (total_found > 0) {
2371 trans = btrfs_start_transaction(tree_root, 1);
2372 btrfs_commit_transaction(trans, tree_root);
2373
2374 mutex_unlock(&root->fs_info->fs_mutex);
2375 btrfs_clean_old_snapshots(tree_root);
2376 mutex_lock(&root->fs_info->fs_mutex);
2377
2378 trans = btrfs_start_transaction(tree_root, 1);
2379 btrfs_commit_transaction(trans, tree_root);
2380 goto again;
2381 }
2382
2383 trans = btrfs_start_transaction(root, 1);
2384 key.objectid = new_size;
2385 key.offset = 0;
2386 key.type = 0;
2387 while(1) {
Chris Mason4313b392008-01-03 09:08:48 -05002388 u64 ptr;
2389
Chris Masonedbd8d42007-12-21 16:27:24 -05002390 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2391 if (ret < 0)
2392 goto out;
Yan73e48b22008-01-03 14:14:39 -05002393
Chris Masonedbd8d42007-12-21 16:27:24 -05002394 leaf = path->nodes[0];
2395 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05002396bg_next:
2397 if (path->slots[0] >= nritems) {
2398 ret = btrfs_next_leaf(root, path);
2399 if (ret < 0)
2400 break;
2401 if (ret == 1) {
2402 ret = 0;
2403 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05002404 }
Yan73e48b22008-01-03 14:14:39 -05002405 leaf = path->nodes[0];
Chris Mason1372f8e2008-01-04 09:34:54 -05002406 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2407
2408 /*
2409 * btrfs_next_leaf doesn't cow buffers, we have to
2410 * do the search again
2411 */
2412 memcpy(&key, &found_key, sizeof(key));
2413 btrfs_release_path(root, path);
Chris Mason725c8462008-01-04 16:47:16 -05002414 goto resched_check;
Yan73e48b22008-01-03 14:14:39 -05002415 }
2416
2417 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2418 if (btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
2419 printk("shrinker found key %Lu %u %Lu\n",
2420 found_key.objectid, found_key.type,
2421 found_key.offset);
2422 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05002423 goto bg_next;
2424 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002425 ret = get_state_private(&info->block_group_cache,
2426 found_key.objectid, &ptr);
2427 if (!ret)
2428 kfree((void *)(unsigned long)ptr);
2429
2430 clear_extent_bits(&info->block_group_cache, found_key.objectid,
2431 found_key.objectid + found_key.offset - 1,
2432 (unsigned int)-1, GFP_NOFS);
2433
2434 key.objectid = found_key.objectid + 1;
2435 btrfs_del_item(trans, root, path);
2436 btrfs_release_path(root, path);
Chris Mason725c8462008-01-04 16:47:16 -05002437resched_check:
2438 if (need_resched()) {
2439 mutex_unlock(&root->fs_info->fs_mutex);
2440 cond_resched();
2441 mutex_lock(&root->fs_info->fs_mutex);
2442 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002443 }
2444 clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
2445 GFP_NOFS);
2446 btrfs_commit_transaction(trans, root);
2447out:
2448 btrfs_free_path(path);
2449 return ret;
2450}
2451
2452int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
2453 struct btrfs_root *root, u64 new_size)
2454{
Chris Mason0b86a832008-03-24 15:01:56 -04002455 btrfs_set_super_total_bytes(&root->fs_info->super_copy, new_size);
Chris Masonedbd8d42007-12-21 16:27:24 -05002456 return 0;
2457}
2458
Chris Mason0b86a832008-03-24 15:01:56 -04002459int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
2460 struct btrfs_key *key)
2461{
2462 int ret;
2463 struct btrfs_key found_key;
2464 struct extent_buffer *leaf;
2465 int slot;
2466
2467 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
2468 if (ret < 0)
2469 return ret;
2470 while(1) {
2471 slot = path->slots[0];
2472 leaf = path->nodes[0];
2473 if (slot >= btrfs_header_nritems(leaf)) {
2474 ret = btrfs_next_leaf(root, path);
2475 if (ret == 0)
2476 continue;
2477 if (ret < 0)
2478 goto error;
2479 break;
2480 }
2481 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2482
2483 if (found_key.objectid >= key->objectid &&
2484 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
2485 return 0;
2486 path->slots[0]++;
2487 }
2488 ret = -ENOENT;
2489error:
2490 return ret;
2491}
2492
Chris Mason9078a3e2007-04-26 16:46:15 -04002493int btrfs_read_block_groups(struct btrfs_root *root)
2494{
2495 struct btrfs_path *path;
2496 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04002497 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04002498 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04002499 struct btrfs_fs_info *info = root->fs_info;
Chris Masond1310b22008-01-24 16:13:08 -05002500 struct extent_io_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002501 struct btrfs_key key;
2502 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04002503 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04002504
2505 block_group_cache = &info->block_group_cache;
Chris Masonbe744172007-05-06 10:15:01 -04002506 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04002507 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04002508 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002509 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04002510 path = btrfs_alloc_path();
2511 if (!path)
2512 return -ENOMEM;
2513
2514 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04002515 ret = find_first_block_group(root, path, &key);
2516 if (ret > 0) {
2517 ret = 0;
2518 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04002519 }
Chris Mason0b86a832008-03-24 15:01:56 -04002520 if (ret != 0)
2521 goto error;
2522
Chris Mason5f39d392007-10-15 16:14:19 -04002523 leaf = path->nodes[0];
2524 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04002525 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2526 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04002527 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04002528 break;
2529 }
Chris Mason3e1ad542007-05-07 20:03:49 -04002530
Chris Mason5f39d392007-10-15 16:14:19 -04002531 read_extent_buffer(leaf, &cache->item,
2532 btrfs_item_ptr_offset(leaf, path->slots[0]),
2533 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04002534 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04002535 cache->cached = 0;
Yan324ae4d2007-11-16 14:57:08 -05002536 cache->pinned = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04002537
Chris Mason9078a3e2007-04-26 16:46:15 -04002538 key.objectid = found_key.objectid + found_key.offset;
2539 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04002540 cache->flags = btrfs_block_group_flags(&cache->item);
2541 bit = 0;
2542 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04002543 bit = BLOCK_GROUP_DATA;
Chris Mason0b86a832008-03-24 15:01:56 -04002544 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2545 bit = BLOCK_GROUP_SYSTEM;
2546 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason96b51792007-10-15 16:15:19 -04002547 bit = BLOCK_GROUP_METADATA;
Chris Mason31f3c992007-04-30 15:25:45 -04002548 }
Chris Mason96b51792007-10-15 16:15:19 -04002549
2550 /* use EXTENT_LOCKED to prevent merging */
2551 set_extent_bits(block_group_cache, found_key.objectid,
2552 found_key.objectid + found_key.offset - 1,
2553 bit | EXTENT_LOCKED, GFP_NOFS);
2554 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04002555 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04002556
Chris Mason9078a3e2007-04-26 16:46:15 -04002557 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04002558 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04002559 break;
2560 }
Chris Mason0b86a832008-03-24 15:01:56 -04002561 ret = 0;
2562error:
Chris Mason9078a3e2007-04-26 16:46:15 -04002563 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04002564 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002565}