blob: 41a63462d3eb71671d81ffce3707747510f5e512 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
Chris Mason74493f72007-12-11 09:25:06 -050021#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040022#include "crc32c.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 Mason0ef3e662008-05-24 14:04:53 -0400139struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
140 btrfs_fs_info *info,
141 u64 bytenr)
142{
143 struct extent_io_tree *block_group_cache;
144 struct btrfs_block_group_cache *block_group = NULL;
145 u64 ptr;
146 u64 start;
147 u64 end;
148 int ret;
149
150 bytenr = max_t(u64, bytenr,
151 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
152 block_group_cache = &info->block_group_cache;
153 ret = find_first_extent_bit(block_group_cache,
154 bytenr, &start, &end,
155 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
156 BLOCK_GROUP_SYSTEM);
157 if (ret) {
158 return NULL;
159 }
160 ret = get_state_private(block_group_cache, start, &ptr);
161 if (ret)
162 return NULL;
163
164 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
165 return block_group;
166}
167
Chris Mason5276aed2007-06-11 21:33:38 -0400168struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
169 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400170 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400171{
Chris Masond1310b22008-01-24 16:13:08 -0500172 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -0400173 struct btrfs_block_group_cache *block_group = NULL;
174 u64 ptr;
175 u64 start;
176 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400177 int ret;
178
Chris Masona061fc82008-05-07 11:43:44 -0400179 bytenr = max_t(u64, bytenr,
180 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Mason96b51792007-10-15 16:15:19 -0400181 block_group_cache = &info->block_group_cache;
182 ret = find_first_extent_bit(block_group_cache,
Chris Masondb945352007-10-15 16:15:53 -0400183 bytenr, &start, &end,
Chris Mason0b86a832008-03-24 15:01:56 -0400184 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
185 BLOCK_GROUP_SYSTEM);
Chris Masonbe744172007-05-06 10:15:01 -0400186 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400187 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400188 }
Chris Mason96b51792007-10-15 16:15:19 -0400189 ret = get_state_private(block_group_cache, start, &ptr);
190 if (ret)
191 return NULL;
192
Jens Axboeae2f5412007-10-19 09:22:59 -0400193 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Yan5cf66422007-11-16 14:57:09 -0500194 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400195 block_group->key.objectid + block_group->key.offset)
196 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400197 return NULL;
198}
Chris Mason0b86a832008-03-24 15:01:56 -0400199
200static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
201{
Chris Mason593060d2008-03-25 16:50:33 -0400202 return (cache->flags & bits) == bits;
Chris Mason0b86a832008-03-24 15:01:56 -0400203}
204
205static int noinline find_search_start(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500206 struct btrfs_block_group_cache **cache_ret,
Chris Mason0ef3e662008-05-24 14:04:53 -0400207 u64 *start_ret, u64 num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400208{
Chris Masone37c9e62007-05-09 20:13:14 -0400209 int ret;
210 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masond7fc6402008-02-18 12:12:38 -0500211 struct extent_io_tree *free_space_cache;
Chris Mason7d1660d2008-03-24 15:02:03 -0400212 struct extent_state *state;
Chris Masone19caa52007-10-15 16:17:44 -0400213 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400214 u64 start = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500215 u64 cache_miss = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500216 u64 total_fs_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -0400217 u64 search_start = *start_ret;
Chris Masonf84a8b32007-11-06 10:26:29 -0500218 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400219
Chris Masonc31f8832008-01-08 15:46:31 -0500220 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masond7fc6402008-02-18 12:12:38 -0500221 free_space_cache = &root->fs_info->free_space_cache;
222
Chris Mason0ef3e662008-05-24 14:04:53 -0400223 if (!cache)
Chris Mason54aa1f42007-06-22 14:16:25 -0400224 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500225
Chris Mason0ef3e662008-05-24 14:04:53 -0400226again:
227 ret = cache_block_group(root, cache);
228 if (ret) {
229 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400230 }
Chris Masone19caa52007-10-15 16:17:44 -0400231
Chris Mason0ef3e662008-05-24 14:04:53 -0400232 last = max(search_start, cache->key.objectid);
233 if (!block_group_bits(cache, data) || cache->ro)
234 goto new_group;
235
Chris Mason7d1660d2008-03-24 15:02:03 -0400236 spin_lock_irq(&free_space_cache->lock);
237 state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
Chris Masone37c9e62007-05-09 20:13:14 -0400238 while(1) {
Chris Mason7d1660d2008-03-24 15:02:03 -0400239 if (!state) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500240 if (!cache_miss)
241 cache_miss = last;
Chris Mason7d1660d2008-03-24 15:02:03 -0400242 spin_unlock_irq(&free_space_cache->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400243 goto new_group;
244 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400245
Chris Mason7d1660d2008-03-24 15:02:03 -0400246 start = max(last, state->start);
247 last = state->end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500248 if (last - start < num) {
Chris Mason7d1660d2008-03-24 15:02:03 -0400249 do {
250 state = extent_state_next(state);
251 } while(state && !(state->state & EXTENT_DIRTY));
Chris Masonf510cfe2007-10-15 16:14:48 -0400252 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500253 }
Chris Mason7d1660d2008-03-24 15:02:03 -0400254 spin_unlock_irq(&free_space_cache->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -0400255 if (cache->ro) {
Chris Mason8f18cf12008-04-25 16:53:30 -0400256 goto new_group;
Chris Mason0ef3e662008-05-24 14:04:53 -0400257 }
Chris Mason0b86a832008-03-24 15:01:56 -0400258 if (start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400259 goto new_group;
Chris Mason8790d502008-04-03 16:29:03 -0400260 if (!block_group_bits(cache, data)) {
Chris Mason611f0e02008-04-03 16:29:03 -0400261 printk("block group bits don't match %Lu %d\n", cache->flags, data);
Chris Mason8790d502008-04-03 16:29:03 -0400262 }
Chris Mason0b86a832008-03-24 15:01:56 -0400263 *start_ret = start;
264 return 0;
Chris Mason8790d502008-04-03 16:29:03 -0400265 }
266out:
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500267 cache = btrfs_lookup_block_group(root->fs_info, search_start);
268 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -0400269 printk("Unable to find block group for %Lu\n", search_start);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500270 WARN_ON(1);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500271 }
Chris Mason0b86a832008-03-24 15:01:56 -0400272 return -ENOSPC;
Chris Masone37c9e62007-05-09 20:13:14 -0400273
274new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400275 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500276wrapped:
Chris Mason0ef3e662008-05-24 14:04:53 -0400277 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Chris Masonc31f8832008-01-08 15:46:31 -0500278 if (!cache || cache->key.objectid >= total_fs_bytes) {
Chris Mason0e4de582007-11-26 10:55:49 -0500279no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500280 if (!wrapped) {
281 wrapped = 1;
282 last = search_start;
Chris Masonf84a8b32007-11-06 10:26:29 -0500283 goto wrapped;
284 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500285 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400286 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500287 if (cache_miss && !cache->cached) {
288 cache_block_group(root, cache);
289 last = cache_miss;
Chris Mason0ef3e662008-05-24 14:04:53 -0400290 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Chris Mason257d0ce2007-11-07 21:08:16 -0500291 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400292 cache_miss = 0;
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500293 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500294 if (!cache)
295 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400296 *cache_ret = cache;
297 goto again;
298}
299
Chris Mason84f54cf2007-06-12 07:43:08 -0400300static u64 div_factor(u64 num, int factor)
301{
Chris Mason257d0ce2007-11-07 21:08:16 -0500302 if (factor == 10)
303 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400304 num *= factor;
305 do_div(num, 10);
306 return num;
307}
308
Chris Mason6324fbf2008-03-24 15:01:59 -0400309static int block_group_state_bits(u64 flags)
310{
311 int bits = 0;
312 if (flags & BTRFS_BLOCK_GROUP_DATA)
313 bits |= BLOCK_GROUP_DATA;
314 if (flags & BTRFS_BLOCK_GROUP_METADATA)
315 bits |= BLOCK_GROUP_METADATA;
316 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
317 bits |= BLOCK_GROUP_SYSTEM;
318 return bits;
319}
320
Chris Mason31f3c992007-04-30 15:25:45 -0400321struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
322 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400323 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400324 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400325{
Chris Mason96b51792007-10-15 16:15:19 -0400326 struct btrfs_block_group_cache *cache;
Chris Masond1310b22008-01-24 16:13:08 -0500327 struct extent_io_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400328 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400329 struct btrfs_fs_info *info = root->fs_info;
330 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400331 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400332 u64 start;
333 u64 end;
334 u64 free_check;
335 u64 ptr;
336 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400337 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400338 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400339 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400340 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400341
Chris Mason96b51792007-10-15 16:15:19 -0400342 block_group_cache = &info->block_group_cache;
343
Chris Masona236aed2008-04-29 09:38:00 -0400344 if (data & BTRFS_BLOCK_GROUP_METADATA)
345 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400346
Chris Mason6324fbf2008-03-24 15:01:59 -0400347 bit = block_group_state_bits(data);
Chris Masonbe744172007-05-06 10:15:01 -0400348
Chris Mason0ef3e662008-05-24 14:04:53 -0400349 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400350 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400351 shint = btrfs_lookup_first_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400352 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonbe744172007-05-06 10:15:01 -0400353 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500354 if (used + shint->pinned <
355 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400356 return shint;
357 }
358 }
359 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400360 if (hint && !hint->ro && block_group_bits(hint, data)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400361 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500362 if (used + hint->pinned <
363 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400364 return hint;
365 }
Chris Masone19caa52007-10-15 16:17:44 -0400366 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400367 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400368 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400369 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400370 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400371 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400372 }
Chris Mason31f3c992007-04-30 15:25:45 -0400373again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400374 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400375 ret = find_first_extent_bit(block_group_cache, last,
376 &start, &end, bit);
377 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400378 break;
Chris Mason96b51792007-10-15 16:15:19 -0400379
380 ret = get_state_private(block_group_cache, start, &ptr);
Chris Mason0ef3e662008-05-24 14:04:53 -0400381 if (ret) {
382 last = end + 1;
383 continue;
384 }
Chris Mason96b51792007-10-15 16:15:19 -0400385
Jens Axboeae2f5412007-10-19 09:22:59 -0400386 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400387 last = cache->key.objectid + cache->key.offset;
388 used = btrfs_block_group_used(&cache->item);
389
Chris Mason8f18cf12008-04-25 16:53:30 -0400390 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400391 free_check = div_factor(cache->key.offset, factor);
Chris Mason8790d502008-04-03 16:29:03 -0400392 if (used + cache->pinned < free_check) {
393 found_group = cache;
394 goto found;
395 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400396 }
Chris Masonde428b62007-05-18 13:28:27 -0400397 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400398 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400399 if (!wrapped) {
400 last = search_start;
401 wrapped = 1;
402 goto again;
403 }
404 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400405 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400406 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400407 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400408 goto again;
409 }
Chris Masonbe744172007-05-06 10:15:01 -0400410found:
Chris Mason31f3c992007-04-30 15:25:45 -0400411 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400412}
413
Chris Mason7bb86312007-12-11 09:25:06 -0500414static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500415 u64 owner, u64 owner_offset)
416{
417 u32 high_crc = ~(u32)0;
418 u32 low_crc = ~(u32)0;
419 __le64 lenum;
Chris Mason74493f72007-12-11 09:25:06 -0500420 lenum = cpu_to_le64(root_objectid);
Miguela5eb62e2008-04-11 15:45:51 -0400421 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500422 lenum = cpu_to_le64(ref_generation);
Miguela5eb62e2008-04-11 15:45:51 -0400423 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500424 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
425 lenum = cpu_to_le64(owner);
Miguela5eb62e2008-04-11 15:45:51 -0400426 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500427 lenum = cpu_to_le64(owner_offset);
Miguela5eb62e2008-04-11 15:45:51 -0400428 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500429 }
Chris Mason74493f72007-12-11 09:25:06 -0500430 return ((u64)high_crc << 32) | (u64)low_crc;
431}
432
Chris Mason7bb86312007-12-11 09:25:06 -0500433static int match_extent_ref(struct extent_buffer *leaf,
434 struct btrfs_extent_ref *disk_ref,
435 struct btrfs_extent_ref *cpu_ref)
436{
437 int ret;
438 int len;
439
440 if (cpu_ref->objectid)
441 len = sizeof(*cpu_ref);
442 else
443 len = 2 * sizeof(u64);
444 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
445 len);
446 return ret == 0;
447}
448
Chris Mason98ed5172008-01-03 10:01:48 -0500449static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
450 struct btrfs_root *root,
451 struct btrfs_path *path, u64 bytenr,
452 u64 root_objectid,
453 u64 ref_generation, u64 owner,
454 u64 owner_offset, int del)
Chris Mason7bb86312007-12-11 09:25:06 -0500455{
456 u64 hash;
457 struct btrfs_key key;
458 struct btrfs_key found_key;
459 struct btrfs_extent_ref ref;
460 struct extent_buffer *leaf;
461 struct btrfs_extent_ref *disk_ref;
462 int ret;
463 int ret2;
464
465 btrfs_set_stack_ref_root(&ref, root_objectid);
466 btrfs_set_stack_ref_generation(&ref, ref_generation);
467 btrfs_set_stack_ref_objectid(&ref, owner);
468 btrfs_set_stack_ref_offset(&ref, owner_offset);
469
470 hash = hash_extent_ref(root_objectid, ref_generation, owner,
471 owner_offset);
472 key.offset = hash;
473 key.objectid = bytenr;
474 key.type = BTRFS_EXTENT_REF_KEY;
475
476 while (1) {
477 ret = btrfs_search_slot(trans, root, &key, path,
478 del ? -1 : 0, del);
479 if (ret < 0)
480 goto out;
481 leaf = path->nodes[0];
482 if (ret != 0) {
483 u32 nritems = btrfs_header_nritems(leaf);
484 if (path->slots[0] >= nritems) {
485 ret2 = btrfs_next_leaf(root, path);
486 if (ret2)
487 goto out;
488 leaf = path->nodes[0];
489 }
490 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
491 if (found_key.objectid != bytenr ||
492 found_key.type != BTRFS_EXTENT_REF_KEY)
493 goto out;
494 key.offset = found_key.offset;
495 if (del) {
496 btrfs_release_path(root, path);
497 continue;
498 }
499 }
500 disk_ref = btrfs_item_ptr(path->nodes[0],
501 path->slots[0],
502 struct btrfs_extent_ref);
503 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
504 ret = 0;
505 goto out;
506 }
507 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
508 key.offset = found_key.offset + 1;
509 btrfs_release_path(root, path);
510 }
511out:
512 return ret;
513}
514
Chris Masond8d5f3e2007-12-11 12:42:00 -0500515/*
516 * Back reference rules. Back refs have three main goals:
517 *
518 * 1) differentiate between all holders of references to an extent so that
519 * when a reference is dropped we can make sure it was a valid reference
520 * before freeing the extent.
521 *
522 * 2) Provide enough information to quickly find the holders of an extent
523 * if we notice a given block is corrupted or bad.
524 *
525 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
526 * maintenance. This is actually the same as #2, but with a slightly
527 * different use case.
528 *
529 * File extents can be referenced by:
530 *
531 * - multiple snapshots, subvolumes, or different generations in one subvol
532 * - different files inside a single subvolume (in theory, not implemented yet)
533 * - different offsets inside a file (bookend extents in file.c)
534 *
535 * The extent ref structure has fields for:
536 *
537 * - Objectid of the subvolume root
538 * - Generation number of the tree holding the reference
539 * - objectid of the file holding the reference
540 * - offset in the file corresponding to the key holding the reference
541 *
542 * When a file extent is allocated the fields are filled in:
543 * (root_key.objectid, trans->transid, inode objectid, offset in file)
544 *
545 * When a leaf is cow'd new references are added for every file extent found
546 * in the leaf. It looks the same as the create case, but trans->transid
547 * will be different when the block is cow'd.
548 *
549 * (root_key.objectid, trans->transid, inode objectid, offset in file)
550 *
551 * When a file extent is removed either during snapshot deletion or file
552 * truncation, the corresponding back reference is found
553 * by searching for:
554 *
555 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
556 * inode objectid, offset in file)
557 *
558 * Btree extents can be referenced by:
559 *
560 * - Different subvolumes
561 * - Different generations of the same subvolume
562 *
563 * Storing sufficient information for a full reverse mapping of a btree
564 * block would require storing the lowest key of the block in the backref,
565 * and it would require updating that lowest key either before write out or
566 * every time it changed. Instead, the objectid of the lowest key is stored
567 * along with the level of the tree block. This provides a hint
568 * about where in the btree the block can be found. Searches through the
569 * btree only need to look for a pointer to that block, so they stop one
570 * level higher than the level recorded in the backref.
571 *
572 * Some btrees do not do reference counting on their extents. These
573 * include the extent tree and the tree of tree roots. Backrefs for these
574 * trees always have a generation of zero.
575 *
576 * When a tree block is created, back references are inserted:
577 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500578 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500579 *
580 * When a tree block is cow'd in a reference counted root,
581 * new back references are added for all the blocks it points to.
582 * These are of the form (trans->transid will have increased since creation):
583 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500584 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500585 *
586 * Because the lowest_key_objectid and the level are just hints
587 * they are not used when backrefs are deleted. When a backref is deleted:
588 *
589 * if backref was for a tree root:
590 * root_objectid = root->root_key.objectid
591 * else
592 * root_objectid = btrfs_header_owner(parent)
593 *
594 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
595 *
596 * Back Reference Key hashing:
597 *
598 * Back references have four fields, each 64 bits long. Unfortunately,
599 * This is hashed into a single 64 bit number and placed into the key offset.
600 * The key objectid corresponds to the first byte in the extent, and the
601 * key type is set to BTRFS_EXTENT_REF_KEY
602 */
Chris Mason7bb86312007-12-11 09:25:06 -0500603int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
604 struct btrfs_root *root,
605 struct btrfs_path *path, u64 bytenr,
606 u64 root_objectid, u64 ref_generation,
607 u64 owner, u64 owner_offset)
Chris Mason74493f72007-12-11 09:25:06 -0500608{
609 u64 hash;
610 struct btrfs_key key;
611 struct btrfs_extent_ref ref;
Chris Mason7bb86312007-12-11 09:25:06 -0500612 struct btrfs_extent_ref *disk_ref;
Chris Mason74493f72007-12-11 09:25:06 -0500613 int ret;
614
615 btrfs_set_stack_ref_root(&ref, root_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500616 btrfs_set_stack_ref_generation(&ref, ref_generation);
Chris Mason74493f72007-12-11 09:25:06 -0500617 btrfs_set_stack_ref_objectid(&ref, owner);
618 btrfs_set_stack_ref_offset(&ref, owner_offset);
619
Chris Mason7bb86312007-12-11 09:25:06 -0500620 hash = hash_extent_ref(root_objectid, ref_generation, owner,
621 owner_offset);
Chris Mason74493f72007-12-11 09:25:06 -0500622 key.offset = hash;
623 key.objectid = bytenr;
624 key.type = BTRFS_EXTENT_REF_KEY;
625
626 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
627 while (ret == -EEXIST) {
Chris Mason7bb86312007-12-11 09:25:06 -0500628 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
629 struct btrfs_extent_ref);
630 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
631 goto out;
632 key.offset++;
633 btrfs_release_path(root, path);
634 ret = btrfs_insert_empty_item(trans, root, path, &key,
635 sizeof(ref));
Chris Mason74493f72007-12-11 09:25:06 -0500636 }
Chris Mason7bb86312007-12-11 09:25:06 -0500637 if (ret)
638 goto out;
639 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
640 struct btrfs_extent_ref);
641 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
642 sizeof(ref));
643 btrfs_mark_buffer_dirty(path->nodes[0]);
644out:
645 btrfs_release_path(root, path);
646 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500647}
648
Chris Masonb18c6682007-04-17 13:26:50 -0400649int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
650 struct btrfs_root *root,
Chris Mason74493f72007-12-11 09:25:06 -0500651 u64 bytenr, u64 num_bytes,
Chris Mason7bb86312007-12-11 09:25:06 -0500652 u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500653 u64 owner, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500654{
Chris Mason5caf2a02007-04-02 11:20:42 -0400655 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500656 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400657 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400658 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400659 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400660 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500661
Chris Masondb945352007-10-15 16:15:53 -0400662 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400663 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400664 if (!path)
665 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400666
Chris Mason3c12ac72008-04-21 12:01:38 -0400667 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400668 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400669 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400670 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400671 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400672 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400673 if (ret < 0)
674 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400675 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500676 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400677 }
Chris Mason02217ed2007-03-02 16:08:05 -0500678 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400679 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400680 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400681 refs = btrfs_extent_refs(l, item);
682 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400683 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500684
Chris Mason5caf2a02007-04-02 11:20:42 -0400685 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500686
Chris Mason3c12ac72008-04-21 12:01:38 -0400687 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -0500688 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
689 path, bytenr, root_objectid,
690 ref_generation, owner, owner_offset);
691 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400692 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400693 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500694
695 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500696 return 0;
697}
698
Chris Masone9d0b132007-08-10 14:06:19 -0400699int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
700 struct btrfs_root *root)
701{
702 finish_current_insert(trans, root->fs_info->extent_root);
703 del_pending_extents(trans, root->fs_info->extent_root);
704 return 0;
705}
706
Chris Masonb18c6682007-04-17 13:26:50 -0400707static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400708 struct btrfs_root *root, u64 bytenr,
709 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500710{
Chris Mason5caf2a02007-04-02 11:20:42 -0400711 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500712 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400713 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400714 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400715 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400716
Chris Masondb945352007-10-15 16:15:53 -0400717 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400718 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400719 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400720 key.objectid = bytenr;
721 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400722 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400723 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400724 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400725 if (ret < 0)
726 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400727 if (ret != 0) {
728 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400729 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500730 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400731 }
732 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400733 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400734 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400735out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400736 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500737 return 0;
738}
739
Chris Masonbe20aa92007-12-17 20:14:01 -0500740u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
741 struct btrfs_path *count_path,
Chris Masona68d5932008-05-08 14:11:56 -0400742 u64 expected_owner,
Chris Masonbe20aa92007-12-17 20:14:01 -0500743 u64 first_extent)
744{
745 struct btrfs_root *extent_root = root->fs_info->extent_root;
746 struct btrfs_path *path;
747 u64 bytenr;
748 u64 found_objectid;
Chris Masona68d5932008-05-08 14:11:56 -0400749 u64 found_owner;
Chris Mason56b453c2008-01-03 09:08:27 -0500750 u64 root_objectid = root->root_key.objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500751 u32 total_count = 0;
Chris Masonbbaf5492008-05-08 16:31:21 -0400752 u32 extent_refs;
Chris Masonbe20aa92007-12-17 20:14:01 -0500753 u32 cur_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500754 u32 nritems;
755 int ret;
756 struct btrfs_key key;
757 struct btrfs_key found_key;
758 struct extent_buffer *l;
759 struct btrfs_extent_item *item;
760 struct btrfs_extent_ref *ref_item;
761 int level = -1;
762
763 path = btrfs_alloc_path();
764again:
765 if (level == -1)
766 bytenr = first_extent;
767 else
768 bytenr = count_path->nodes[level]->start;
769
770 cur_count = 0;
771 key.objectid = bytenr;
772 key.offset = 0;
773
774 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
775 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
776 if (ret < 0)
777 goto out;
778 BUG_ON(ret == 0);
779
780 l = path->nodes[0];
781 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
782
783 if (found_key.objectid != bytenr ||
784 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
785 goto out;
786 }
787
788 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masonbbaf5492008-05-08 16:31:21 -0400789 extent_refs = btrfs_extent_refs(l, item);
Chris Masonbe20aa92007-12-17 20:14:01 -0500790 while (1) {
Chris Masonbd098352008-01-03 13:23:19 -0500791 l = path->nodes[0];
Chris Masonbe20aa92007-12-17 20:14:01 -0500792 nritems = btrfs_header_nritems(l);
793 if (path->slots[0] >= nritems) {
794 ret = btrfs_next_leaf(extent_root, path);
795 if (ret == 0)
796 continue;
797 break;
798 }
799 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
800 if (found_key.objectid != bytenr)
801 break;
Chris Masonbd098352008-01-03 13:23:19 -0500802
Chris Masonbe20aa92007-12-17 20:14:01 -0500803 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
804 path->slots[0]++;
805 continue;
806 }
807
808 cur_count++;
809 ref_item = btrfs_item_ptr(l, path->slots[0],
810 struct btrfs_extent_ref);
811 found_objectid = btrfs_ref_root(l, ref_item);
812
Chris Mason56b453c2008-01-03 09:08:27 -0500813 if (found_objectid != root_objectid) {
814 total_count = 2;
Chris Mason4313b392008-01-03 09:08:48 -0500815 goto out;
Chris Mason56b453c2008-01-03 09:08:27 -0500816 }
Chris Masona68d5932008-05-08 14:11:56 -0400817 if (level == -1) {
818 found_owner = btrfs_ref_objectid(l, ref_item);
819 if (found_owner != expected_owner) {
820 total_count = 2;
821 goto out;
822 }
Chris Masonbbaf5492008-05-08 16:31:21 -0400823 /*
824 * nasty. we don't count a reference held by
825 * the running transaction. This allows nodatacow
826 * to avoid cow most of the time
827 */
828 if (found_owner >= BTRFS_FIRST_FREE_OBJECTID &&
829 btrfs_ref_generation(l, ref_item) ==
830 root->fs_info->generation) {
831 extent_refs--;
832 }
Chris Masona68d5932008-05-08 14:11:56 -0400833 }
Chris Mason56b453c2008-01-03 09:08:27 -0500834 total_count = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500835 path->slots[0]++;
836 }
Chris Masonbbaf5492008-05-08 16:31:21 -0400837 /*
838 * if there is more than one reference against a data extent,
839 * we have to assume the other ref is another snapshot
840 */
841 if (level == -1 && extent_refs > 1) {
842 total_count = 2;
843 goto out;
844 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500845 if (cur_count == 0) {
846 total_count = 0;
847 goto out;
848 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500849 if (level >= 0 && root->node == count_path->nodes[level])
850 goto out;
851 level++;
852 btrfs_release_path(root, path);
853 goto again;
854
855out:
856 btrfs_free_path(path);
857 return total_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500858}
Chris Masonc5739bb2007-04-10 09:27:04 -0400859int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -0500860 struct btrfs_root *root, u64 owner_objectid)
Chris Masonc5739bb2007-04-10 09:27:04 -0400861{
Chris Mason7bb86312007-12-11 09:25:06 -0500862 u64 generation;
863 u64 key_objectid;
864 u64 level;
865 u32 nritems;
866 struct btrfs_disk_key disk_key;
867
868 level = btrfs_header_level(root->node);
869 generation = trans->transid;
870 nritems = btrfs_header_nritems(root->node);
871 if (nritems > 0) {
872 if (level == 0)
873 btrfs_item_key(root->node, &disk_key, 0);
874 else
875 btrfs_node_key(root->node, &disk_key, 0);
876 key_objectid = btrfs_disk_key_objectid(&disk_key);
877 } else {
878 key_objectid = 0;
879 }
Chris Masondb945352007-10-15 16:15:53 -0400880 return btrfs_inc_extent_ref(trans, root, root->node->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500881 root->node->len, owner_objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500882 generation, level, key_objectid);
Chris Masonc5739bb2007-04-10 09:27:04 -0400883}
884
Chris Masone089f052007-03-16 16:20:31 -0400885int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400886 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500887{
Chris Masondb945352007-10-15 16:15:53 -0400888 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400889 u32 nritems;
890 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400891 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500892 int i;
Chris Masondb945352007-10-15 16:15:53 -0400893 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400894 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400895 int faili;
Chris Masona28ec192007-03-06 20:08:01 -0500896
Chris Mason3768f362007-03-13 16:47:54 -0400897 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500898 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400899
Chris Masondb945352007-10-15 16:15:53 -0400900 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400901 nritems = btrfs_header_nritems(buf);
902 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400903 if (level == 0) {
904 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400905 btrfs_item_key_to_cpu(buf, &key, i);
906 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400907 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400908 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400909 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400910 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454df2007-04-19 13:37:44 -0400911 BTRFS_FILE_EXTENT_INLINE)
912 continue;
Chris Masondb945352007-10-15 16:15:53 -0400913 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
914 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400915 continue;
Chris Masondb945352007-10-15 16:15:53 -0400916 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500917 btrfs_file_extent_disk_num_bytes(buf, fi),
918 root->root_key.objectid, trans->transid,
919 key.objectid, key.offset);
Chris Mason54aa1f42007-06-22 14:16:25 -0400920 if (ret) {
921 faili = i;
922 goto fail;
923 }
Chris Mason6407bf62007-03-27 06:33:00 -0400924 } else {
Chris Masondb945352007-10-15 16:15:53 -0400925 bytenr = btrfs_node_blockptr(buf, i);
Chris Mason6caab482007-12-13 09:48:07 -0500926 btrfs_node_key_to_cpu(buf, &key, i);
Chris Masondb945352007-10-15 16:15:53 -0400927 ret = btrfs_inc_extent_ref(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500928 btrfs_level_size(root, level - 1),
929 root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500930 trans->transid,
931 level - 1, key.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400932 if (ret) {
933 faili = i;
934 goto fail;
935 }
Chris Mason6407bf62007-03-27 06:33:00 -0400936 }
Chris Mason02217ed2007-03-02 16:08:05 -0500937 }
938 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400939fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400940 WARN_ON(1);
Chris Mason7bb86312007-12-11 09:25:06 -0500941#if 0
Chris Mason54aa1f42007-06-22 14:16:25 -0400942 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400943 if (level == 0) {
944 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400945 btrfs_item_key_to_cpu(buf, &key, i);
946 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400947 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400948 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400949 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400950 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400951 BTRFS_FILE_EXTENT_INLINE)
952 continue;
Chris Masondb945352007-10-15 16:15:53 -0400953 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
954 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400955 continue;
Chris Masondb945352007-10-15 16:15:53 -0400956 err = btrfs_free_extent(trans, root, disk_bytenr,
957 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400958 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400959 BUG_ON(err);
960 } else {
Chris Masondb945352007-10-15 16:15:53 -0400961 bytenr = btrfs_node_blockptr(buf, i);
962 err = btrfs_free_extent(trans, root, bytenr,
963 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400964 BUG_ON(err);
965 }
966 }
Chris Mason7bb86312007-12-11 09:25:06 -0500967#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400968 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500969}
970
Chris Mason9078a3e2007-04-26 16:46:15 -0400971static int write_one_cache_group(struct btrfs_trans_handle *trans,
972 struct btrfs_root *root,
973 struct btrfs_path *path,
974 struct btrfs_block_group_cache *cache)
975{
976 int ret;
977 int pending_ret;
978 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400979 unsigned long bi;
980 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400981
Chris Mason9078a3e2007-04-26 16:46:15 -0400982 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400983 if (ret < 0)
984 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400985 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400986
987 leaf = path->nodes[0];
988 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
989 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
990 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400991 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400992fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400993 finish_current_insert(trans, extent_root);
994 pending_ret = del_pending_extents(trans, extent_root);
995 if (ret)
996 return ret;
997 if (pending_ret)
998 return pending_ret;
999 return 0;
1000
1001}
1002
Chris Mason96b51792007-10-15 16:15:19 -04001003int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1004 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001005{
Chris Masond1310b22008-01-24 16:13:08 -05001006 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -04001007 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001008 int ret;
1009 int err = 0;
1010 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001011 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001012 u64 last = 0;
1013 u64 start;
1014 u64 end;
1015 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -04001016
Chris Mason96b51792007-10-15 16:15:19 -04001017 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001018 path = btrfs_alloc_path();
1019 if (!path)
1020 return -ENOMEM;
1021
1022 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -04001023 ret = find_first_extent_bit(block_group_cache, last,
1024 &start, &end, BLOCK_GROUP_DIRTY);
1025 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -04001026 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001027
Chris Mason96b51792007-10-15 16:15:19 -04001028 last = end + 1;
1029 ret = get_state_private(block_group_cache, start, &ptr);
1030 if (ret)
1031 break;
Jens Axboeae2f5412007-10-19 09:22:59 -04001032 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -04001033 err = write_one_cache_group(trans, root,
1034 path, cache);
1035 /*
1036 * if we fail to write the cache group, we want
1037 * to keep it marked dirty in hopes that a later
1038 * write will work
1039 */
1040 if (err) {
1041 werr = err;
1042 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001043 }
Chris Mason96b51792007-10-15 16:15:19 -04001044 clear_extent_bits(block_group_cache, start, end,
1045 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001046 }
1047 btrfs_free_path(path);
1048 return werr;
1049}
1050
Chris Mason6324fbf2008-03-24 15:01:59 -04001051static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1052 u64 flags)
1053{
1054 struct list_head *head = &info->space_info;
1055 struct list_head *cur;
1056 struct btrfs_space_info *found;
1057 list_for_each(cur, head) {
1058 found = list_entry(cur, struct btrfs_space_info, list);
1059 if (found->flags == flags)
1060 return found;
1061 }
1062 return NULL;
1063
1064}
1065
Chris Mason593060d2008-03-25 16:50:33 -04001066static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1067 u64 total_bytes, u64 bytes_used,
1068 struct btrfs_space_info **space_info)
1069{
1070 struct btrfs_space_info *found;
1071
1072 found = __find_space_info(info, flags);
1073 if (found) {
1074 found->total_bytes += total_bytes;
1075 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001076 found->full = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001077 WARN_ON(found->total_bytes < found->bytes_used);
1078 *space_info = found;
1079 return 0;
1080 }
1081 found = kmalloc(sizeof(*found), GFP_NOFS);
1082 if (!found)
1083 return -ENOMEM;
1084
1085 list_add(&found->list, &info->space_info);
1086 found->flags = flags;
1087 found->total_bytes = total_bytes;
1088 found->bytes_used = bytes_used;
1089 found->bytes_pinned = 0;
1090 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001091 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001092 *space_info = found;
1093 return 0;
1094}
1095
Chris Mason8790d502008-04-03 16:29:03 -04001096static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1097{
1098 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001099 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001100 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001101 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001102 if (extra_flags) {
1103 if (flags & BTRFS_BLOCK_GROUP_DATA)
1104 fs_info->avail_data_alloc_bits |= extra_flags;
1105 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1106 fs_info->avail_metadata_alloc_bits |= extra_flags;
1107 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1108 fs_info->avail_system_alloc_bits |= extra_flags;
1109 }
1110}
Chris Mason593060d2008-03-25 16:50:33 -04001111
Chris Masona061fc82008-05-07 11:43:44 -04001112static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001113{
Chris Masona061fc82008-05-07 11:43:44 -04001114 u64 num_devices = root->fs_info->fs_devices->num_devices;
1115
1116 if (num_devices == 1)
1117 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1118 if (num_devices < 4)
1119 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1120
Chris Masonec44a352008-04-28 15:29:52 -04001121 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1122 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001123 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001124 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001125 }
Chris Masonec44a352008-04-28 15:29:52 -04001126
1127 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001128 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001129 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001130 }
Chris Masonec44a352008-04-28 15:29:52 -04001131
1132 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1133 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1134 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1135 (flags & BTRFS_BLOCK_GROUP_DUP)))
1136 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1137 return flags;
1138}
1139
Chris Mason6324fbf2008-03-24 15:01:59 -04001140static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1141 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001142 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001143{
1144 struct btrfs_space_info *space_info;
1145 u64 thresh;
1146 u64 start;
1147 u64 num_bytes;
1148 int ret;
1149
Chris Masona061fc82008-05-07 11:43:44 -04001150 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001151
Chris Mason6324fbf2008-03-24 15:01:59 -04001152 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001153 if (!space_info) {
1154 ret = update_space_info(extent_root->fs_info, flags,
1155 0, 0, &space_info);
1156 BUG_ON(ret);
1157 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001158 BUG_ON(!space_info);
1159
Chris Mason0ef3e662008-05-24 14:04:53 -04001160 if (space_info->force_alloc) {
1161 force = 1;
1162 space_info->force_alloc = 0;
1163 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001164 if (space_info->full)
1165 return 0;
1166
Chris Mason8790d502008-04-03 16:29:03 -04001167 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001168 if (!force &&
1169 (space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
Chris Mason6324fbf2008-03-24 15:01:59 -04001170 thresh)
1171 return 0;
1172
1173 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1174 if (ret == -ENOSPC) {
1175printk("space info full %Lu\n", flags);
1176 space_info->full = 1;
1177 return 0;
1178 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001179 BUG_ON(ret);
1180
1181 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001182 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001183 BUG_ON(ret);
Chris Mason593060d2008-03-25 16:50:33 -04001184
Chris Mason6324fbf2008-03-24 15:01:59 -04001185 return 0;
1186}
1187
Chris Mason9078a3e2007-04-26 16:46:15 -04001188static int update_block_group(struct btrfs_trans_handle *trans,
1189 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001190 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001191 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001192{
1193 struct btrfs_block_group_cache *cache;
1194 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001195 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001196 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001197 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -04001198 u64 start;
1199 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -04001200
Chris Mason9078a3e2007-04-26 16:46:15 -04001201 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001202 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001203 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001204 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001205 }
Chris Masondb945352007-10-15 16:15:53 -04001206 byte_in_group = bytenr - cache->key.objectid;
1207 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -04001208 start = cache->key.objectid;
1209 end = start + cache->key.offset - 1;
1210 set_extent_bits(&info->block_group_cache, start, end,
1211 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001212
1213 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001214 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001215 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001216 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001217 cache->space_info->bytes_used += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -04001218 } else {
Chris Masondb945352007-10-15 16:15:53 -04001219 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001220 cache->space_info->bytes_used -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -04001221 if (mark_free) {
1222 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -04001223 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -04001224 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001225 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001226 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001227 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -04001228 total -= num_bytes;
1229 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001230 }
1231 return 0;
1232}
Chris Mason6324fbf2008-03-24 15:01:59 -04001233
Chris Masona061fc82008-05-07 11:43:44 -04001234static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1235{
1236 u64 start;
1237 u64 end;
1238 int ret;
1239 ret = find_first_extent_bit(&root->fs_info->block_group_cache,
1240 search_start, &start, &end,
1241 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
1242 BLOCK_GROUP_SYSTEM);
1243 if (ret)
1244 return 0;
1245 return start;
1246}
1247
1248
Yan324ae4d2007-11-16 14:57:08 -05001249static int update_pinned_extents(struct btrfs_root *root,
1250 u64 bytenr, u64 num, int pin)
1251{
1252 u64 len;
1253 struct btrfs_block_group_cache *cache;
1254 struct btrfs_fs_info *fs_info = root->fs_info;
1255
1256 if (pin) {
1257 set_extent_dirty(&fs_info->pinned_extents,
1258 bytenr, bytenr + num - 1, GFP_NOFS);
1259 } else {
1260 clear_extent_dirty(&fs_info->pinned_extents,
1261 bytenr, bytenr + num - 1, GFP_NOFS);
1262 }
1263 while (num > 0) {
1264 cache = btrfs_lookup_block_group(fs_info, bytenr);
Chris Masona061fc82008-05-07 11:43:44 -04001265 if (!cache) {
1266 u64 first = first_logical_byte(root, bytenr);
1267 WARN_ON(first < bytenr);
1268 len = min(first - bytenr, num);
1269 } else {
1270 len = min(num, cache->key.offset -
1271 (bytenr - cache->key.objectid));
1272 }
Yan324ae4d2007-11-16 14:57:08 -05001273 if (pin) {
Chris Masona061fc82008-05-07 11:43:44 -04001274 if (cache) {
1275 cache->pinned += len;
1276 cache->space_info->bytes_pinned += len;
1277 }
Yan324ae4d2007-11-16 14:57:08 -05001278 fs_info->total_pinned += len;
1279 } else {
Chris Masona061fc82008-05-07 11:43:44 -04001280 if (cache) {
1281 cache->pinned -= len;
1282 cache->space_info->bytes_pinned -= len;
1283 }
Yan324ae4d2007-11-16 14:57:08 -05001284 fs_info->total_pinned -= len;
1285 }
1286 bytenr += len;
1287 num -= len;
1288 }
1289 return 0;
1290}
Chris Mason9078a3e2007-04-26 16:46:15 -04001291
Chris Masond1310b22008-01-24 16:13:08 -05001292int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001293{
Chris Masonccd467d2007-06-28 15:57:36 -04001294 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001295 u64 start;
1296 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001297 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001298 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001299
1300 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001301 ret = find_first_extent_bit(pinned_extents, last,
1302 &start, &end, EXTENT_DIRTY);
1303 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001304 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001305 set_extent_dirty(copy, start, end, GFP_NOFS);
1306 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001307 }
1308 return 0;
1309}
1310
1311int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1312 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001313 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001314{
Chris Mason1a5bc162007-10-15 16:15:26 -04001315 u64 start;
1316 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001317 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001318 struct extent_io_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -04001319 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -05001320
1321 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001322 ret = find_first_extent_bit(unpin, 0, &start, &end,
1323 EXTENT_DIRTY);
1324 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001325 break;
Yan324ae4d2007-11-16 14:57:08 -05001326 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001327 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1328 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -05001329 }
1330 return 0;
1331}
1332
Chris Mason98ed5172008-01-03 10:01:48 -05001333static int finish_current_insert(struct btrfs_trans_handle *trans,
1334 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001335{
Chris Mason7bb86312007-12-11 09:25:06 -05001336 u64 start;
1337 u64 end;
1338 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001339 struct extent_buffer *eb;
Chris Mason7bb86312007-12-11 09:25:06 -05001340 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001341 struct btrfs_key ins;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001342 struct btrfs_disk_key first;
Chris Mason234b63a2007-03-13 10:46:10 -04001343 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001344 int ret;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001345 int level;
Chris Mason1a5bc162007-10-15 16:15:26 -04001346 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001347
Chris Mason5f39d392007-10-15 16:14:19 -04001348 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -04001349 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason7bb86312007-12-11 09:25:06 -05001350 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001351
Chris Mason26b80032007-08-08 20:17:12 -04001352 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001353 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1354 &end, EXTENT_LOCKED);
1355 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001356 break;
1357
Chris Mason1a5bc162007-10-15 16:15:26 -04001358 ins.objectid = start;
1359 ins.offset = end + 1 - start;
1360 err = btrfs_insert_item(trans, extent_root, &ins,
1361 &extent_item, sizeof(extent_item));
1362 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1363 GFP_NOFS);
Chris Masonca7a79a2008-05-12 12:59:19 -04001364 eb = read_tree_block(extent_root, ins.objectid, ins.offset,
1365 trans->transid);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001366 level = btrfs_header_level(eb);
1367 if (level == 0) {
1368 btrfs_item_key(eb, &first, 0);
1369 } else {
1370 btrfs_node_key(eb, &first, 0);
1371 }
Chris Mason7bb86312007-12-11 09:25:06 -05001372 err = btrfs_insert_extent_backref(trans, extent_root, path,
1373 start, extent_root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -05001374 0, level,
1375 btrfs_disk_key_objectid(&first));
Chris Mason7bb86312007-12-11 09:25:06 -05001376 BUG_ON(err);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001377 free_extent_buffer(eb);
Chris Mason037e6392007-03-07 11:50:24 -05001378 }
Chris Mason7bb86312007-12-11 09:25:06 -05001379 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001380 return 0;
1381}
1382
Chris Masondb945352007-10-15 16:15:53 -04001383static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1384 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -04001385{
Chris Mason1a5bc162007-10-15 16:15:26 -04001386 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001387 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001388
Chris Masonf4b9aa82007-03-27 11:05:53 -04001389 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -04001390 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -04001391 if (buf) {
Chris Mason1259ab72008-05-12 13:39:03 -04001392 if (btrfs_buffer_uptodate(buf, 0)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001393 u64 transid =
1394 root->fs_info->running_transaction->transid;
Chris Masondc17ff82008-01-08 15:46:30 -05001395 u64 header_transid =
1396 btrfs_header_generation(buf);
Chris Mason6bc34672008-04-04 15:40:00 -04001397 if (header_transid == transid &&
1398 !btrfs_header_flag(buf,
1399 BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason55c69072008-01-09 15:55:33 -05001400 clean_tree_block(NULL, root, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001401 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -05001402 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001403 }
Chris Masonf4b9aa82007-03-27 11:05:53 -04001404 }
Chris Mason5f39d392007-10-15 16:14:19 -04001405 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -04001406 }
Yan324ae4d2007-11-16 14:57:08 -05001407 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001408 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -04001409 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -04001410 bytenr, bytenr + num_bytes - 1,
1411 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001412 }
Chris Masonbe744172007-05-06 10:15:01 -04001413 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001414 return 0;
1415}
1416
Chris Masona28ec192007-03-06 20:08:01 -05001417/*
1418 * remove an extent from the root, returns 0 on success
1419 */
Chris Masone089f052007-03-16 16:20:31 -04001420static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001421 *root, u64 bytenr, u64 num_bytes,
1422 u64 root_objectid, u64 ref_generation,
1423 u64 owner_objectid, u64 owner_offset, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -04001424 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001425{
Chris Mason5caf2a02007-04-02 11:20:42 -04001426 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001427 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001428 struct btrfs_fs_info *info = root->fs_info;
1429 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001430 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001431 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001432 int extent_slot = 0;
1433 int found_extent = 0;
1434 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001435 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001436 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001437
Chris Masondb945352007-10-15 16:15:53 -04001438 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001439 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001440 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001441 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001442 if (!path)
1443 return -ENOMEM;
1444
Chris Mason3c12ac72008-04-21 12:01:38 -04001445 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05001446 ret = lookup_extent_backref(trans, extent_root, path,
1447 bytenr, root_objectid,
1448 ref_generation,
1449 owner_objectid, owner_offset, 1);
1450 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001451 struct btrfs_key found_key;
1452 extent_slot = path->slots[0];
1453 while(extent_slot > 0) {
1454 extent_slot--;
1455 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1456 extent_slot);
1457 if (found_key.objectid != bytenr)
1458 break;
1459 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1460 found_key.offset == num_bytes) {
1461 found_extent = 1;
1462 break;
1463 }
1464 if (path->slots[0] - extent_slot > 5)
1465 break;
1466 }
1467 if (!found_extent)
1468 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001469 } else {
1470 btrfs_print_leaf(extent_root, path->nodes[0]);
1471 WARN_ON(1);
1472 printk("Unable to find ref byte nr %Lu root %Lu "
1473 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1474 root_objectid, ref_generation, owner_objectid,
1475 owner_offset);
1476 }
Chris Mason952fcca2008-02-18 16:33:44 -05001477 if (!found_extent) {
1478 btrfs_release_path(extent_root, path);
1479 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1480 if (ret < 0)
1481 return ret;
1482 BUG_ON(ret);
1483 extent_slot = path->slots[0];
1484 }
Chris Mason5f39d392007-10-15 16:14:19 -04001485
1486 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001487 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001488 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001489 refs = btrfs_extent_refs(leaf, ei);
1490 BUG_ON(refs == 0);
1491 refs -= 1;
1492 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001493
Chris Mason5f39d392007-10-15 16:14:19 -04001494 btrfs_mark_buffer_dirty(leaf);
1495
Chris Mason952fcca2008-02-18 16:33:44 -05001496 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1497 /* if the back ref and the extent are next to each other
1498 * they get deleted below in one shot
1499 */
1500 path->slots[0] = extent_slot;
1501 num_to_del = 2;
1502 } else if (found_extent) {
1503 /* otherwise delete the extent back ref */
1504 ret = btrfs_del_item(trans, extent_root, path);
1505 BUG_ON(ret);
1506 /* if refs are 0, we need to setup the path for deletion */
1507 if (refs == 0) {
1508 btrfs_release_path(extent_root, path);
1509 ret = btrfs_search_slot(trans, extent_root, &key, path,
1510 -1, 1);
1511 if (ret < 0)
1512 return ret;
1513 BUG_ON(ret);
1514 }
1515 }
1516
Chris Masoncf27e1e2007-03-13 09:49:06 -04001517 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001518 u64 super_used;
1519 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04001520
1521 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -04001522 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -05001523 if (ret > 0)
1524 mark_free = 1;
1525 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001526 }
1527
Josef Bacik58176a92007-08-29 15:47:34 -04001528 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001529 super_used = btrfs_super_bytes_used(&info->super_copy);
1530 btrfs_set_super_bytes_used(&info->super_copy,
1531 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001532
1533 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001534 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001535 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001536 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001537 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1538 num_to_del);
Chris Mason54aa1f42007-06-22 14:16:25 -04001539 if (ret) {
1540 return ret;
1541 }
Chris Masondb945352007-10-15 16:15:53 -04001542 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001543 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001544 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05001545 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001546 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001547 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05001548 return ret;
1549}
1550
1551/*
Chris Masonfec577f2007-02-26 10:40:21 -05001552 * find all the blocks marked as pending in the radix tree and remove
1553 * them from the extent map
1554 */
Chris Masone089f052007-03-16 16:20:31 -04001555static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1556 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05001557{
1558 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001559 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001560 u64 start;
1561 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001562 struct extent_io_tree *pending_del;
1563 struct extent_io_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -04001564
Chris Mason1a5bc162007-10-15 16:15:26 -04001565 pending_del = &extent_root->fs_info->pending_del;
1566 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -05001567
1568 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001569 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1570 EXTENT_LOCKED);
1571 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05001572 break;
Yan324ae4d2007-11-16 14:57:08 -05001573 update_pinned_extents(extent_root, start, end + 1 - start, 1);
Chris Mason1a5bc162007-10-15 16:15:26 -04001574 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1575 GFP_NOFS);
1576 ret = __free_extent(trans, extent_root,
Chris Mason7bb86312007-12-11 09:25:06 -05001577 start, end + 1 - start,
1578 extent_root->root_key.objectid,
1579 0, 0, 0, 0, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001580 if (ret)
1581 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001582 }
Chris Masone20d96d2007-03-22 12:13:20 -04001583 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05001584}
1585
1586/*
1587 * remove an extent from the root, returns 0 on success
1588 */
Chris Masone089f052007-03-16 16:20:31 -04001589int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001590 *root, u64 bytenr, u64 num_bytes,
1591 u64 root_objectid, u64 ref_generation,
1592 u64 owner_objectid, u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05001593{
Chris Mason9f5fae22007-03-20 14:38:32 -04001594 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05001595 int pending_ret;
1596 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05001597
Chris Masondb945352007-10-15 16:15:53 -04001598 WARN_ON(num_bytes < root->sectorsize);
Chris Mason7bb86312007-12-11 09:25:06 -05001599 if (!root->ref_cows)
1600 ref_generation = 0;
1601
Chris Masona28ec192007-03-06 20:08:01 -05001602 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -04001603 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -05001604 return 0;
1605 }
Chris Mason7bb86312007-12-11 09:25:06 -05001606 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1607 ref_generation, owner_objectid, owner_offset,
1608 pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001609 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05001610 return ret ? ret : pending_ret;
1611}
1612
Chris Mason87ee04e2007-11-30 11:30:34 -05001613static u64 stripe_align(struct btrfs_root *root, u64 val)
1614{
1615 u64 mask = ((u64)root->stripesize - 1);
1616 u64 ret = (val + mask) & ~mask;
1617 return ret;
1618}
1619
Chris Masonfec577f2007-02-26 10:40:21 -05001620/*
1621 * walks the btree of allocated extents and find a hole of a given size.
1622 * The key ins is changed to record the hole:
1623 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04001624 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001625 * ins->offset == number of blocks
1626 * Any available blocks before search_start are skipped.
1627 */
Chris Mason98ed5172008-01-03 10:01:48 -05001628static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1629 struct btrfs_root *orig_root,
1630 u64 num_bytes, u64 empty_size,
1631 u64 search_start, u64 search_end,
1632 u64 hint_byte, struct btrfs_key *ins,
1633 u64 exclude_start, u64 exclude_nr,
1634 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001635{
Chris Mason87ee04e2007-11-30 11:30:34 -05001636 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04001637 u64 orig_search_start;
Chris Mason9f5fae22007-03-20 14:38:32 -04001638 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001639 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001640 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001641 u64 *last_ptr = NULL;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001642 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001643 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001644 int wrapped = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001645 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001646 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04001647 int allowed_chunk_alloc = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001648
Chris Masondb945352007-10-15 16:15:53 -04001649 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001650 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1651
Chris Mason0ef3e662008-05-24 14:04:53 -04001652 if (orig_root->ref_cows || empty_size)
1653 allowed_chunk_alloc = 1;
1654
Chris Mason239b14b2008-03-24 15:02:07 -04001655 if (data & BTRFS_BLOCK_GROUP_METADATA) {
1656 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04001657 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04001658 }
1659
1660 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
1661 last_ptr = &root->fs_info->last_data_alloc;
1662 }
1663
1664 if (last_ptr) {
1665 if (*last_ptr)
1666 hint_byte = *last_ptr;
1667 else {
1668 empty_size += empty_cluster;
1669 }
1670 }
1671
Chris Masona061fc82008-05-07 11:43:44 -04001672 search_start = max(search_start, first_logical_byte(root, 0));
1673 orig_search_start = search_start;
1674
Chris Mason7f93bf82008-03-24 15:01:28 -04001675 if (search_end == (u64)-1)
1676 search_end = btrfs_super_total_bytes(&info->super_copy);
Chris Mason0b86a832008-03-24 15:01:56 -04001677
Chris Masondb945352007-10-15 16:15:53 -04001678 if (hint_byte) {
Chris Mason0ef3e662008-05-24 14:04:53 -04001679 block_group = btrfs_lookup_first_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001680 if (!block_group)
1681 hint_byte = search_start;
Chris Masonbe744172007-05-06 10:15:01 -04001682 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -04001683 hint_byte, data, 1);
Chris Mason239b14b2008-03-24 15:02:07 -04001684 if (last_ptr && *last_ptr == 0 && block_group)
1685 hint_byte = block_group->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001686 } else {
1687 block_group = btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001688 trans->block_group,
1689 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001690 }
Chris Mason239b14b2008-03-24 15:02:07 -04001691 search_start = max(search_start, hint_byte);
Chris Masonbe744172007-05-06 10:15:01 -04001692
Chris Mason6702ed42007-08-07 16:15:09 -04001693 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001694
Chris Masonbe744172007-05-06 10:15:01 -04001695check_failed:
Chris Mason70b043f2007-12-13 09:02:46 -05001696 if (!block_group) {
Chris Mason0ef3e662008-05-24 14:04:53 -04001697 block_group = btrfs_lookup_first_block_group(info,
1698 search_start);
Chris Mason70b043f2007-12-13 09:02:46 -05001699 if (!block_group)
Chris Mason0ef3e662008-05-24 14:04:53 -04001700 block_group = btrfs_lookup_first_block_group(info,
Chris Mason70b043f2007-12-13 09:02:46 -05001701 orig_search_start);
1702 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001703 if (full_scan && !chunk_alloc_done) {
1704 if (allowed_chunk_alloc) {
1705 do_chunk_alloc(trans, root,
1706 num_bytes + 2 * 1024 * 1024, data, 1);
1707 allowed_chunk_alloc = 0;
1708 } else if (block_group && block_group_bits(block_group, data)) {
1709 block_group->space_info->force_alloc = 1;
1710 }
1711 chunk_alloc_done = 1;
1712 }
Chris Mason0b86a832008-03-24 15:01:56 -04001713 ret = find_search_start(root, &block_group, &search_start,
1714 total_needed, data);
Chris Mason239b14b2008-03-24 15:02:07 -04001715 if (ret == -ENOSPC && last_ptr && *last_ptr) {
1716 *last_ptr = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001717 block_group = btrfs_lookup_first_block_group(info,
1718 orig_search_start);
Chris Mason239b14b2008-03-24 15:02:07 -04001719 search_start = orig_search_start;
1720 ret = find_search_start(root, &block_group, &search_start,
1721 total_needed, data);
1722 }
1723 if (ret == -ENOSPC)
1724 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04001725 if (ret)
1726 goto error;
1727
Chris Mason239b14b2008-03-24 15:02:07 -04001728 if (last_ptr && *last_ptr && search_start != *last_ptr) {
1729 *last_ptr = 0;
1730 if (!empty_size) {
1731 empty_size += empty_cluster;
1732 total_needed += empty_size;
1733 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001734 block_group = btrfs_lookup_first_block_group(info,
Chris Mason239b14b2008-03-24 15:02:07 -04001735 orig_search_start);
1736 search_start = orig_search_start;
1737 ret = find_search_start(root, &block_group,
1738 &search_start, total_needed, data);
1739 if (ret == -ENOSPC)
1740 goto enospc;
1741 if (ret)
1742 goto error;
1743 }
1744
Chris Mason87ee04e2007-11-30 11:30:34 -05001745 search_start = stripe_align(root, search_start);
Chris Masonfec577f2007-02-26 10:40:21 -05001746 ins->objectid = search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001747 ins->offset = num_bytes;
Chris Masone37c9e62007-05-09 20:13:14 -04001748
Chris Masondb945352007-10-15 16:15:53 -04001749 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001750 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04001751
1752 if (ins->objectid + num_bytes >
1753 block_group->key.objectid + block_group->key.offset) {
Chris Masone19caa52007-10-15 16:17:44 -04001754 search_start = block_group->key.objectid +
1755 block_group->key.offset;
1756 goto new_group;
1757 }
Chris Mason0b86a832008-03-24 15:01:56 -04001758
Chris Mason1a5bc162007-10-15 16:15:26 -04001759 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001760 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1761 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001762 goto new_group;
1763 }
Chris Mason0b86a832008-03-24 15:01:56 -04001764
Chris Mason1a5bc162007-10-15 16:15:26 -04001765 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001766 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1767 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001768 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001769 }
Chris Mason0b86a832008-03-24 15:01:56 -04001770
Chris Masondb945352007-10-15 16:15:53 -04001771 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001772 ins->objectid < exclude_start + exclude_nr)) {
1773 search_start = exclude_start + exclude_nr;
1774 goto new_group;
1775 }
Chris Mason0b86a832008-03-24 15:01:56 -04001776
Chris Mason6324fbf2008-03-24 15:01:59 -04001777 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
Chris Mason5276aed2007-06-11 21:33:38 -04001778 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001779 if (block_group)
1780 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001781 }
Chris Masondb945352007-10-15 16:15:53 -04001782 ins->offset = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001783 if (last_ptr) {
1784 *last_ptr = ins->objectid + ins->offset;
1785 if (*last_ptr ==
1786 btrfs_super_total_bytes(&root->fs_info->super_copy)) {
1787 *last_ptr = 0;
1788 }
1789 }
Chris Masonfec577f2007-02-26 10:40:21 -05001790 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001791
1792new_group:
Chris Masondb945352007-10-15 16:15:53 -04001793 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001794enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001795 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001796 if (full_scan) {
1797 ret = -ENOSPC;
1798 goto error;
1799 }
Chris Mason6702ed42007-08-07 16:15:09 -04001800 if (wrapped) {
1801 if (!full_scan)
1802 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001803 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001804 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001805 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001806 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001807 block_group = btrfs_lookup_first_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001808 cond_resched();
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001809 block_group = btrfs_find_block_group(root, block_group,
1810 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001811 goto check_failed;
1812
Chris Mason0f70abe2007-02-28 16:46:22 -05001813error:
Chris Mason0f70abe2007-02-28 16:46:22 -05001814 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001815}
Chris Masonec44a352008-04-28 15:29:52 -04001816
Chris Masonfec577f2007-02-26 10:40:21 -05001817/*
Chris Masonfec577f2007-02-26 10:40:21 -05001818 * finds a free extent and does all the dirty work required for allocation
1819 * returns the key for the extent through ins, and a tree buffer for
1820 * the first block of the extent through buf.
1821 *
1822 * returns 0 if everything worked, non-zero otherwise.
1823 */
Chris Mason4d775672007-04-20 20:23:12 -04001824int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -05001825 struct btrfs_root *root,
Chris Mason98d20f62008-04-14 09:46:10 -04001826 u64 num_bytes, u64 min_alloc_size,
1827 u64 root_objectid, u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05001828 u64 owner, u64 owner_offset,
1829 u64 empty_size, u64 hint_byte,
Chris Masonec44a352008-04-28 15:29:52 -04001830 u64 search_end, struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05001831{
1832 int ret;
1833 int pending_ret;
Chris Masonc31f8832008-01-08 15:46:31 -05001834 u64 super_used;
1835 u64 root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001836 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04001837 u64 alloc_profile;
Chris Mason47e4bb92008-02-01 14:51:59 -05001838 u32 sizes[2];
Chris Mason1261ec42007-03-20 20:35:03 -04001839 struct btrfs_fs_info *info = root->fs_info;
1840 struct btrfs_root *extent_root = info->extent_root;
Chris Mason47e4bb92008-02-01 14:51:59 -05001841 struct btrfs_extent_item *extent_item;
1842 struct btrfs_extent_ref *ref;
Chris Mason7bb86312007-12-11 09:25:06 -05001843 struct btrfs_path *path;
Chris Mason47e4bb92008-02-01 14:51:59 -05001844 struct btrfs_key keys[2];
Chris Mason8f662a72008-01-02 10:01:11 -05001845
Chris Mason6324fbf2008-03-24 15:01:59 -04001846 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04001847 alloc_profile = info->avail_data_alloc_bits &
1848 info->data_alloc_profile;
1849 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001850 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04001851 alloc_profile = info->avail_system_alloc_bits &
1852 info->system_alloc_profile;
1853 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001854 } else {
Chris Mason8790d502008-04-03 16:29:03 -04001855 alloc_profile = info->avail_metadata_alloc_bits &
1856 info->metadata_alloc_profile;
1857 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001858 }
Chris Mason98d20f62008-04-14 09:46:10 -04001859again:
Chris Masona061fc82008-05-07 11:43:44 -04001860 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04001861 /*
1862 * the only place that sets empty_size is btrfs_realloc_node, which
1863 * is not called recursively on allocations
1864 */
1865 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04001866 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001867 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04001868 2 * 1024 * 1024,
1869 BTRFS_BLOCK_GROUP_METADATA |
1870 (info->metadata_alloc_profile &
1871 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04001872 BUG_ON(ret);
1873 }
1874 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04001875 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04001876 BUG_ON(ret);
1877 }
Chris Mason0b86a832008-03-24 15:01:56 -04001878
Chris Masondb945352007-10-15 16:15:53 -04001879 WARN_ON(num_bytes < root->sectorsize);
1880 ret = find_free_extent(trans, root, num_bytes, empty_size,
1881 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001882 trans->alloc_exclude_start,
1883 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04001884
Chris Mason98d20f62008-04-14 09:46:10 -04001885 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
1886 num_bytes = num_bytes >> 1;
1887 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04001888 do_chunk_alloc(trans, root->fs_info->extent_root,
1889 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04001890 goto again;
1891 }
Chris Masonec44a352008-04-28 15:29:52 -04001892 if (ret) {
1893 printk("allocation failed flags %Lu\n", data);
1894 }
Chris Masonccd467d2007-06-28 15:57:36 -04001895 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001896 if (ret)
1897 return ret;
1898
Josef Bacik58176a92007-08-29 15:47:34 -04001899 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001900 super_used = btrfs_super_bytes_used(&info->super_copy);
1901 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001902
Josef Bacik58176a92007-08-29 15:47:34 -04001903 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001904 root_used = btrfs_root_used(&root->root_item);
1905 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001906
Chris Masonf510cfe2007-10-15 16:14:48 -04001907 clear_extent_dirty(&root->fs_info->free_space_cache,
1908 ins->objectid, ins->objectid + ins->offset - 1,
1909 GFP_NOFS);
1910
Chris Mason26b80032007-08-08 20:17:12 -04001911 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001912 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1913 ins->objectid + ins->offset - 1,
1914 EXTENT_LOCKED, GFP_NOFS);
Chris Mason26b80032007-08-08 20:17:12 -04001915 goto update_block;
1916 }
1917
1918 WARN_ON(trans->alloc_exclude_nr);
1919 trans->alloc_exclude_start = ins->objectid;
1920 trans->alloc_exclude_nr = ins->offset;
Chris Mason037e6392007-03-07 11:50:24 -05001921
Chris Mason47e4bb92008-02-01 14:51:59 -05001922 memcpy(&keys[0], ins, sizeof(*ins));
1923 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
1924 owner, owner_offset);
1925 keys[1].objectid = ins->objectid;
1926 keys[1].type = BTRFS_EXTENT_REF_KEY;
1927 sizes[0] = sizeof(*extent_item);
1928 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05001929
1930 path = btrfs_alloc_path();
1931 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05001932
1933 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
1934 sizes, 2);
Chris Mason26b80032007-08-08 20:17:12 -04001935
Chris Masonccd467d2007-06-28 15:57:36 -04001936 BUG_ON(ret);
Chris Mason47e4bb92008-02-01 14:51:59 -05001937 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1938 struct btrfs_extent_item);
1939 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
1940 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1941 struct btrfs_extent_ref);
1942
1943 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
1944 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
1945 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
1946 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
1947
1948 btrfs_mark_buffer_dirty(path->nodes[0]);
1949
1950 trans->alloc_exclude_start = 0;
1951 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001952 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001953 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001954 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001955
Chris Masone37c9e62007-05-09 20:13:14 -04001956 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001957 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001958 }
1959 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001960 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001961 }
Chris Mason26b80032007-08-08 20:17:12 -04001962
1963update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04001964 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05001965 if (ret) {
1966 printk("update block group failed for %Lu %Lu\n",
1967 ins->objectid, ins->offset);
1968 BUG();
1969 }
Chris Mason037e6392007-03-07 11:50:24 -05001970 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001971}
1972
1973/*
1974 * helper function to allocate a block for a given tree
1975 * returns the tree buffer or NULL.
1976 */
Chris Mason5f39d392007-10-15 16:14:19 -04001977struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001978 struct btrfs_root *root,
Chris Mason7bb86312007-12-11 09:25:06 -05001979 u32 blocksize,
1980 u64 root_objectid, u64 hint,
1981 u64 empty_size)
1982{
1983 u64 ref_generation;
1984
1985 if (root->ref_cows)
1986 ref_generation = trans->transid;
1987 else
1988 ref_generation = 0;
1989
1990
1991 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1992 ref_generation, 0, 0, hint, empty_size);
1993}
1994
1995/*
1996 * helper function to allocate a block for a given tree
1997 * returns the tree buffer or NULL.
1998 */
1999struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2000 struct btrfs_root *root,
2001 u32 blocksize,
2002 u64 root_objectid,
2003 u64 ref_generation,
2004 u64 first_objectid,
2005 int level,
2006 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04002007 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05002008{
Chris Masone2fa7222007-03-12 16:22:34 -04002009 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05002010 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002011 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05002012
Chris Mason98d20f62008-04-14 09:46:10 -04002013 ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
Chris Mason7bb86312007-12-11 09:25:06 -05002014 root_objectid, ref_generation,
Chris Masonf6dbff52007-12-13 11:13:32 -05002015 level, first_objectid, empty_size, hint,
Chris Masondb945352007-10-15 16:15:53 -04002016 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002017 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04002018 BUG_ON(ret > 0);
2019 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05002020 }
Chris Masondb945352007-10-15 16:15:53 -04002021 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002022 if (!buf) {
Chris Mason7bb86312007-12-11 09:25:06 -05002023 btrfs_free_extent(trans, root, ins.objectid, blocksize,
2024 root->root_key.objectid, ref_generation,
2025 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002026 return ERR_PTR(-ENOMEM);
2027 }
Chris Mason55c69072008-01-09 15:55:33 -05002028 btrfs_set_header_generation(buf, trans->transid);
2029 clean_tree_block(trans, root, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002030 btrfs_set_buffer_uptodate(buf);
Chris Mason55c69072008-01-09 15:55:33 -05002031
2032 if (PageDirty(buf->first_page)) {
2033 printk("page %lu dirty\n", buf->first_page->index);
2034 WARN_ON(1);
2035 }
2036
Chris Mason5f39d392007-10-15 16:14:19 -04002037 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2038 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason9afbb0b2008-02-15 13:19:35 -05002039 if (!btrfs_test_opt(root, SSD))
2040 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002041 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05002042 return buf;
2043}
Chris Masona28ec192007-03-06 20:08:01 -05002044
Chris Mason98ed5172008-01-03 10:01:48 -05002045static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2046 struct btrfs_root *root,
2047 struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04002048{
Chris Mason7bb86312007-12-11 09:25:06 -05002049 u64 leaf_owner;
2050 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04002051 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002052 struct btrfs_file_extent_item *fi;
2053 int i;
2054 int nritems;
2055 int ret;
2056
Chris Mason5f39d392007-10-15 16:14:19 -04002057 BUG_ON(!btrfs_is_leaf(leaf));
2058 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05002059 leaf_owner = btrfs_header_owner(leaf);
2060 leaf_generation = btrfs_header_generation(leaf);
2061
Chris Mason6407bf62007-03-27 06:33:00 -04002062 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002063 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002064
2065 btrfs_item_key_to_cpu(leaf, &key, i);
2066 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04002067 continue;
2068 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002069 if (btrfs_file_extent_type(leaf, fi) ==
2070 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04002071 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04002072 /*
2073 * FIXME make sure to insert a trans record that
2074 * repeats the snapshot del on crash
2075 */
Chris Masondb945352007-10-15 16:15:53 -04002076 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2077 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04002078 continue;
Chris Masondb945352007-10-15 16:15:53 -04002079 ret = btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002080 btrfs_file_extent_disk_num_bytes(leaf, fi),
2081 leaf_owner, leaf_generation,
2082 key.objectid, key.offset, 0);
Chris Mason6407bf62007-03-27 06:33:00 -04002083 BUG_ON(ret);
2084 }
2085 return 0;
2086}
2087
Chris Mason98ed5172008-01-03 10:01:48 -05002088static void noinline reada_walk_down(struct btrfs_root *root,
Chris Masonbea495e2008-01-24 16:13:14 -05002089 struct extent_buffer *node,
2090 int slot)
Chris Masone0115992007-06-19 16:23:05 -04002091{
Chris Masondb945352007-10-15 16:15:53 -04002092 u64 bytenr;
Chris Masonbea495e2008-01-24 16:13:14 -05002093 u64 last = 0;
2094 u32 nritems;
Chris Masone0115992007-06-19 16:23:05 -04002095 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04002096 u32 blocksize;
Chris Masonbea495e2008-01-24 16:13:14 -05002097 int ret;
2098 int i;
2099 int level;
2100 int skipped = 0;
Chris Masone0115992007-06-19 16:23:05 -04002101
Chris Mason5f39d392007-10-15 16:14:19 -04002102 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04002103 level = btrfs_header_level(node);
Chris Masonbea495e2008-01-24 16:13:14 -05002104 if (level)
2105 return;
2106
2107 for (i = slot; i < nritems && skipped < 32; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002108 bytenr = btrfs_node_blockptr(node, i);
Chris Masonbea495e2008-01-24 16:13:14 -05002109 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2110 (last > bytenr && last - bytenr > 32 * 1024))) {
2111 skipped++;
Chris Masone0115992007-06-19 16:23:05 -04002112 continue;
Chris Masonbea495e2008-01-24 16:13:14 -05002113 }
2114 blocksize = btrfs_level_size(root, level - 1);
2115 if (i != slot) {
2116 ret = lookup_extent_ref(NULL, root, bytenr,
2117 blocksize, &refs);
2118 BUG_ON(ret);
2119 if (refs != 1) {
2120 skipped++;
2121 continue;
2122 }
2123 }
Chris Mason409eb952007-08-08 20:17:12 -04002124 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masonca7a79a2008-05-12 12:59:19 -04002125 ret = readahead_tree_block(root, bytenr, blocksize,
2126 btrfs_node_ptr_generation(node, i));
Chris Masonbea495e2008-01-24 16:13:14 -05002127 last = bytenr + blocksize;
Chris Mason409eb952007-08-08 20:17:12 -04002128 cond_resched();
2129 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04002130 if (ret)
2131 break;
2132 }
2133}
2134
Chris Mason9aca1d52007-03-13 11:09:37 -04002135/*
2136 * helper function for drop_snapshot, this walks down the tree dropping ref
2137 * counts as it goes.
2138 */
Chris Mason98ed5172008-01-03 10:01:48 -05002139static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2140 struct btrfs_root *root,
2141 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002142{
Chris Mason7bb86312007-12-11 09:25:06 -05002143 u64 root_owner;
2144 u64 root_gen;
2145 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04002146 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002147 struct extent_buffer *next;
2148 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002149 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04002150 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002151 int ret;
2152 u32 refs;
2153
Chris Mason5caf2a02007-04-02 11:20:42 -04002154 WARN_ON(*level < 0);
2155 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04002156 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002157 path->nodes[*level]->start,
2158 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002159 BUG_ON(ret);
2160 if (refs > 1)
2161 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002162
Chris Mason9aca1d52007-03-13 11:09:37 -04002163 /*
2164 * walk down to the last node level and free all the leaves
2165 */
Chris Mason6407bf62007-03-27 06:33:00 -04002166 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002167 WARN_ON(*level < 0);
2168 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002169 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002170
Chris Mason5f39d392007-10-15 16:14:19 -04002171 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002172 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002173
Chris Mason7518a232007-03-12 12:01:18 -04002174 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002175 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002176 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002177 if (*level == 0) {
2178 ret = drop_leaf_ref(trans, root, cur);
2179 BUG_ON(ret);
2180 break;
2181 }
Chris Masondb945352007-10-15 16:15:53 -04002182 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04002183 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04002184 blocksize = btrfs_level_size(root, *level - 1);
2185 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002186 BUG_ON(ret);
2187 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002188 parent = path->nodes[*level];
2189 root_owner = btrfs_header_owner(parent);
2190 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002191 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04002192 ret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002193 blocksize, root_owner,
2194 root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002195 BUG_ON(ret);
2196 continue;
2197 }
Chris Masondb945352007-10-15 16:15:53 -04002198 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04002199 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002200 free_extent_buffer(next);
Chris Masonbea495e2008-01-24 16:13:14 -05002201 reada_walk_down(root, cur, path->slots[*level]);
Chris Masone9d0b132007-08-10 14:06:19 -04002202
Chris Mason8790d502008-04-03 16:29:03 -04002203 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masonca7a79a2008-05-12 12:59:19 -04002204 next = read_tree_block(root, bytenr, blocksize,
2205 ptr_gen);
Chris Mason8790d502008-04-03 16:29:03 -04002206 mutex_lock(&root->fs_info->fs_mutex);
2207
2208 /* we've dropped the lock, double check */
Chris Masondb945352007-10-15 16:15:53 -04002209 ret = lookup_extent_ref(trans, root, bytenr,
2210 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002211 BUG_ON(ret);
2212 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002213 parent = path->nodes[*level];
2214 root_owner = btrfs_header_owner(parent);
2215 root_gen = btrfs_header_generation(parent);
2216
Chris Masone9d0b132007-08-10 14:06:19 -04002217 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04002218 free_extent_buffer(next);
Chris Mason7bb86312007-12-11 09:25:06 -05002219 ret = btrfs_free_extent(trans, root, bytenr,
2220 blocksize,
2221 root_owner,
2222 root_gen, 0, 0, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04002223 BUG_ON(ret);
2224 continue;
2225 }
2226 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002227 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002228 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002229 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002230 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002231 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002232 path->slots[*level] = 0;
2233 }
2234out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002235 WARN_ON(*level < 0);
2236 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002237
2238 if (path->nodes[*level] == root->node) {
2239 root_owner = root->root_key.objectid;
2240 parent = path->nodes[*level];
2241 } else {
2242 parent = path->nodes[*level + 1];
2243 root_owner = btrfs_header_owner(parent);
2244 }
2245
2246 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04002247 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002248 path->nodes[*level]->len,
2249 root_owner, root_gen, 0, 0, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002250 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05002251 path->nodes[*level] = NULL;
2252 *level += 1;
2253 BUG_ON(ret);
2254 return 0;
2255}
2256
Chris Mason9aca1d52007-03-13 11:09:37 -04002257/*
2258 * helper for dropping snapshots. This walks back up the tree in the path
2259 * to find the first node higher up where we haven't yet gone through
2260 * all the slots
2261 */
Chris Mason98ed5172008-01-03 10:01:48 -05002262static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2263 struct btrfs_root *root,
2264 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002265{
Chris Mason7bb86312007-12-11 09:25:06 -05002266 u64 root_owner;
2267 u64 root_gen;
2268 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002269 int i;
2270 int slot;
2271 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04002272
Chris Mason234b63a2007-03-13 10:46:10 -04002273 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05002274 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04002275 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2276 struct extent_buffer *node;
2277 struct btrfs_disk_key disk_key;
2278 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05002279 path->slots[i]++;
2280 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04002281 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002282 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04002283 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04002284 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04002285 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05002286 return 0;
2287 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002288 if (path->nodes[*level] == root->node) {
2289 root_owner = root->root_key.objectid;
2290 root_gen =
2291 btrfs_header_generation(path->nodes[*level]);
2292 } else {
2293 struct extent_buffer *node;
2294 node = path->nodes[*level + 1];
2295 root_owner = btrfs_header_owner(node);
2296 root_gen = btrfs_header_generation(node);
2297 }
Chris Masone089f052007-03-16 16:20:31 -04002298 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002299 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002300 path->nodes[*level]->len,
2301 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04002302 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002303 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04002304 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05002305 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05002306 }
2307 }
2308 return 1;
2309}
2310
Chris Mason9aca1d52007-03-13 11:09:37 -04002311/*
2312 * drop the reference count on the tree rooted at 'snap'. This traverses
2313 * the tree freeing any blocks that have a ref count of zero after being
2314 * decremented.
2315 */
Chris Masone089f052007-03-16 16:20:31 -04002316int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04002317 *root)
Chris Mason20524f02007-03-10 06:35:47 -05002318{
Chris Mason3768f362007-03-13 16:47:54 -04002319 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04002320 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05002321 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04002322 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05002323 int i;
2324 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002325 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002326
Chris Mason5caf2a02007-04-02 11:20:42 -04002327 path = btrfs_alloc_path();
2328 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05002329
Chris Mason5f39d392007-10-15 16:14:19 -04002330 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05002331 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002332 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2333 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04002334 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04002335 path->slots[level] = 0;
2336 } else {
2337 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002338 struct btrfs_disk_key found_key;
2339 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04002340
Chris Mason9f3a7422007-08-07 15:52:19 -04002341 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04002342 level = root_item->drop_level;
2343 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002344 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04002345 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04002346 ret = wret;
2347 goto out;
2348 }
Chris Mason5f39d392007-10-15 16:14:19 -04002349 node = path->nodes[level];
2350 btrfs_node_key(node, &found_key, path->slots[level]);
2351 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2352 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04002353 }
Chris Mason20524f02007-03-10 06:35:47 -05002354 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002355 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002356 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002357 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002358 if (wret < 0)
2359 ret = wret;
2360
Chris Mason5caf2a02007-04-02 11:20:42 -04002361 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002362 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002363 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002364 if (wret < 0)
2365 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04002366 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04002367 break;
Chris Mason20524f02007-03-10 06:35:47 -05002368 }
Chris Mason83e15a22007-03-12 09:03:27 -04002369 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002370 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04002371 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04002372 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04002373 }
Chris Mason20524f02007-03-10 06:35:47 -05002374 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002375out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002376 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04002377 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05002378}
Chris Mason9078a3e2007-04-26 16:46:15 -04002379
Chris Masonbe744172007-05-06 10:15:01 -04002380int btrfs_free_block_groups(struct btrfs_fs_info *info)
2381{
Chris Masonf510cfe2007-10-15 16:14:48 -04002382 u64 start;
2383 u64 end;
Yanb97f9202007-11-01 11:28:41 -04002384 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04002385 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04002386 while(1) {
2387 ret = find_first_extent_bit(&info->block_group_cache, 0,
2388 &start, &end, (unsigned int)-1);
2389 if (ret)
2390 break;
Yanb97f9202007-11-01 11:28:41 -04002391 ret = get_state_private(&info->block_group_cache, start, &ptr);
2392 if (!ret)
2393 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04002394 clear_extent_bits(&info->block_group_cache, start,
2395 end, (unsigned int)-1, GFP_NOFS);
2396 }
Chris Masone37c9e62007-05-09 20:13:14 -04002397 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04002398 ret = find_first_extent_bit(&info->free_space_cache, 0,
2399 &start, &end, EXTENT_DIRTY);
2400 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04002401 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04002402 clear_extent_dirty(&info->free_space_cache, start,
2403 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04002404 }
Chris Masonbe744172007-05-06 10:15:01 -04002405 return 0;
2406}
2407
Chris Mason8e7bf942008-04-28 09:02:36 -04002408static unsigned long calc_ra(unsigned long start, unsigned long last,
2409 unsigned long nr)
2410{
2411 return min(last, start + nr - 1);
2412}
2413
Chris Mason98ed5172008-01-03 10:01:48 -05002414static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2415 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05002416{
2417 u64 page_start;
2418 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002419 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05002420 unsigned long i;
2421 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05002422 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05002423 struct file_ra_state *ra;
Chris Mason8e7bf942008-04-28 09:02:36 -04002424 unsigned long total_read = 0;
2425 unsigned long ra_pages;
Chris Masona061fc82008-05-07 11:43:44 -04002426 struct btrfs_trans_handle *trans;
Chris Mason4313b392008-01-03 09:08:48 -05002427
2428 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002429
2430 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05002431 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05002432 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2433
Chris Mason8e7bf942008-04-28 09:02:36 -04002434 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
2435
Chris Mason4313b392008-01-03 09:08:48 -05002436 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05002437
Chris Mason4313b392008-01-03 09:08:48 -05002438 for (; i <= last_index; i++) {
Chris Mason8e7bf942008-04-28 09:02:36 -04002439 if (total_read % ra_pages == 0) {
2440 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
2441 calc_ra(i, last_index, ra_pages));
2442 }
2443 total_read++;
Chris Masona061fc82008-05-07 11:43:44 -04002444 if (((u64)i << PAGE_CACHE_SHIFT) > inode->i_size)
2445 goto truncate_racing;
2446
Chris Masonedbd8d42007-12-21 16:27:24 -05002447 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04002448 if (!page) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002449 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04002450 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002451 if (!PageUptodate(page)) {
2452 btrfs_readpage(NULL, page);
2453 lock_page(page);
2454 if (!PageUptodate(page)) {
2455 unlock_page(page);
2456 page_cache_release(page);
2457 goto out_unlock;
2458 }
2459 }
Chris Masonec44a352008-04-28 15:29:52 -04002460#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2461 ClearPageDirty(page);
2462#else
2463 cancel_dirty_page(page, PAGE_CACHE_SIZE);
2464#endif
2465 wait_on_page_writeback(page);
2466 set_page_extent_mapped(page);
Chris Masonedbd8d42007-12-21 16:27:24 -05002467 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2468 page_end = page_start + PAGE_CACHE_SIZE - 1;
2469
Chris Masond1310b22008-01-24 16:13:08 -05002470 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002471
Chris Masond1310b22008-01-24 16:13:08 -05002472 set_extent_delalloc(io_tree, page_start,
Chris Masonedbd8d42007-12-21 16:27:24 -05002473 page_end, GFP_NOFS);
Chris Masona061fc82008-05-07 11:43:44 -04002474 set_page_dirty(page);
Chris Masonedbd8d42007-12-21 16:27:24 -05002475
Chris Masond1310b22008-01-24 16:13:08 -05002476 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002477 unlock_page(page);
2478 page_cache_release(page);
2479 }
Chris Masona061fc82008-05-07 11:43:44 -04002480 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2481 total_read);
Chris Masonedbd8d42007-12-21 16:27:24 -05002482
2483out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04002484 kfree(ra);
Chris Masona061fc82008-05-07 11:43:44 -04002485 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
2486 if (trans) {
2487 btrfs_add_ordered_inode(inode);
2488 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
2489 mark_inode_dirty(inode);
2490 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002491 mutex_unlock(&inode->i_mutex);
2492 return 0;
Chris Masona061fc82008-05-07 11:43:44 -04002493
2494truncate_racing:
2495 vmtruncate(inode, inode->i_size);
2496 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2497 total_read);
2498 goto out_unlock;
Chris Masonedbd8d42007-12-21 16:27:24 -05002499}
2500
Chris Mason4313b392008-01-03 09:08:48 -05002501/*
Chris Masonbf4ef672008-05-08 13:26:18 -04002502 * The back references tell us which tree holds a ref on a block,
2503 * but it is possible for the tree root field in the reference to
2504 * reflect the original root before a snapshot was made. In this
2505 * case we should search through all the children of a given root
2506 * to find potential holders of references on a block.
2507 *
2508 * Instead, we do something a little less fancy and just search
2509 * all the roots for a given key/block combination.
2510 */
2511static int find_root_for_ref(struct btrfs_root *root,
2512 struct btrfs_path *path,
2513 struct btrfs_key *key0,
2514 int level,
2515 int file_key,
2516 struct btrfs_root **found_root,
2517 u64 bytenr)
2518{
2519 struct btrfs_key root_location;
2520 struct btrfs_root *cur_root = *found_root;
2521 struct btrfs_file_extent_item *file_extent;
2522 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
2523 u64 found_bytenr;
2524 int ret;
2525 int i;
2526
2527 root_location.offset = (u64)-1;
2528 root_location.type = BTRFS_ROOT_ITEM_KEY;
2529 path->lowest_level = level;
2530 path->reada = 0;
2531 while(1) {
2532 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
2533 found_bytenr = 0;
2534 if (ret == 0 && file_key) {
2535 struct extent_buffer *leaf = path->nodes[0];
2536 file_extent = btrfs_item_ptr(leaf, path->slots[0],
2537 struct btrfs_file_extent_item);
2538 if (btrfs_file_extent_type(leaf, file_extent) ==
2539 BTRFS_FILE_EXTENT_REG) {
2540 found_bytenr =
2541 btrfs_file_extent_disk_bytenr(leaf,
2542 file_extent);
2543 }
Chris Mason323da792008-05-09 11:46:48 -04002544 } else if (!file_key) {
Chris Masonbf4ef672008-05-08 13:26:18 -04002545 if (path->nodes[level])
2546 found_bytenr = path->nodes[level]->start;
2547 }
2548
2549 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2550 if (!path->nodes[i])
2551 break;
2552 free_extent_buffer(path->nodes[i]);
2553 path->nodes[i] = NULL;
2554 }
2555 btrfs_release_path(cur_root, path);
2556
2557 if (found_bytenr == bytenr) {
2558 *found_root = cur_root;
2559 ret = 0;
2560 goto out;
2561 }
2562 ret = btrfs_search_root(root->fs_info->tree_root,
2563 root_search_start, &root_search_start);
2564 if (ret)
2565 break;
2566
2567 root_location.objectid = root_search_start;
2568 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
2569 &root_location);
2570 if (!cur_root) {
2571 ret = 1;
2572 break;
2573 }
2574 }
2575out:
2576 path->lowest_level = 0;
2577 return ret;
2578}
2579
2580/*
Chris Mason4313b392008-01-03 09:08:48 -05002581 * note, this releases the path
2582 */
Chris Mason98ed5172008-01-03 10:01:48 -05002583static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05002584 struct btrfs_path *path,
Chris Mason0ef3e662008-05-24 14:04:53 -04002585 struct btrfs_key *extent_key,
2586 u64 *last_file_objectid,
2587 u64 *last_file_offset,
2588 u64 *last_file_root,
2589 u64 last_extent)
Chris Masonedbd8d42007-12-21 16:27:24 -05002590{
2591 struct inode *inode;
2592 struct btrfs_root *found_root;
Chris Masonbf4ef672008-05-08 13:26:18 -04002593 struct btrfs_key root_location;
2594 struct btrfs_key found_key;
Chris Mason4313b392008-01-03 09:08:48 -05002595 struct btrfs_extent_ref *ref;
2596 u64 ref_root;
2597 u64 ref_gen;
2598 u64 ref_objectid;
2599 u64 ref_offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05002600 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04002601 int level;
Chris Masonedbd8d42007-12-21 16:27:24 -05002602
Chris Mason4313b392008-01-03 09:08:48 -05002603 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2604 struct btrfs_extent_ref);
2605 ref_root = btrfs_ref_root(path->nodes[0], ref);
2606 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2607 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2608 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2609 btrfs_release_path(extent_root, path);
2610
Chris Masonbf4ef672008-05-08 13:26:18 -04002611 root_location.objectid = ref_root;
Chris Masonedbd8d42007-12-21 16:27:24 -05002612 if (ref_gen == 0)
Chris Masonbf4ef672008-05-08 13:26:18 -04002613 root_location.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002614 else
Chris Masonbf4ef672008-05-08 13:26:18 -04002615 root_location.offset = (u64)-1;
2616 root_location.type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05002617
2618 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Masonbf4ef672008-05-08 13:26:18 -04002619 &root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002620 BUG_ON(!found_root);
2621
2622 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Chris Masonbf4ef672008-05-08 13:26:18 -04002623 found_key.objectid = ref_objectid;
2624 found_key.type = BTRFS_EXTENT_DATA_KEY;
2625 found_key.offset = ref_offset;
2626 level = 0;
2627
Chris Mason0ef3e662008-05-24 14:04:53 -04002628 if (last_extent == extent_key->objectid &&
2629 *last_file_objectid == ref_objectid &&
2630 *last_file_offset == ref_offset &&
2631 *last_file_root == ref_root)
2632 goto out;
2633
Chris Masonbf4ef672008-05-08 13:26:18 -04002634 ret = find_root_for_ref(extent_root, path, &found_key,
2635 level, 1, &found_root,
2636 extent_key->objectid);
2637
2638 if (ret)
2639 goto out;
2640
Chris Mason0ef3e662008-05-24 14:04:53 -04002641 if (last_extent == extent_key->objectid &&
2642 *last_file_objectid == ref_objectid &&
2643 *last_file_offset == ref_offset &&
2644 *last_file_root == ref_root)
2645 goto out;
2646
Chris Masonedbd8d42007-12-21 16:27:24 -05002647 mutex_unlock(&extent_root->fs_info->fs_mutex);
2648 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2649 ref_objectid, found_root);
2650 if (inode->i_state & I_NEW) {
2651 /* the inode and parent dir are two different roots */
2652 BTRFS_I(inode)->root = found_root;
2653 BTRFS_I(inode)->location.objectid = ref_objectid;
2654 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2655 BTRFS_I(inode)->location.offset = 0;
2656 btrfs_read_locked_inode(inode);
2657 unlock_new_inode(inode);
2658
2659 }
2660 /* this can happen if the reference is not against
2661 * the latest version of the tree root
2662 */
2663 if (is_bad_inode(inode)) {
2664 mutex_lock(&extent_root->fs_info->fs_mutex);
2665 goto out;
2666 }
Chris Mason0ef3e662008-05-24 14:04:53 -04002667 *last_file_objectid = inode->i_ino;
2668 *last_file_root = found_root->root_key.objectid;
2669 *last_file_offset = ref_offset;
2670
Chris Masonedbd8d42007-12-21 16:27:24 -05002671 relocate_inode_pages(inode, ref_offset, extent_key->offset);
Chris Masonedbd8d42007-12-21 16:27:24 -05002672 iput(inode);
2673 mutex_lock(&extent_root->fs_info->fs_mutex);
2674 } else {
2675 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05002676 struct extent_buffer *eb;
Chris Masonedbd8d42007-12-21 16:27:24 -05002677 int i;
2678
Chris Masonedbd8d42007-12-21 16:27:24 -05002679 eb = read_tree_block(found_root, extent_key->objectid,
Chris Masonca7a79a2008-05-12 12:59:19 -04002680 extent_key->offset, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05002681 level = btrfs_header_level(eb);
2682
2683 if (level == 0)
2684 btrfs_item_key_to_cpu(eb, &found_key, 0);
2685 else
2686 btrfs_node_key_to_cpu(eb, &found_key, 0);
2687
2688 free_extent_buffer(eb);
2689
Chris Masonbf4ef672008-05-08 13:26:18 -04002690 ret = find_root_for_ref(extent_root, path, &found_key,
2691 level, 0, &found_root,
2692 extent_key->objectid);
2693
2694 if (ret)
2695 goto out;
2696
2697 trans = btrfs_start_transaction(found_root, 1);
2698
Chris Masonedbd8d42007-12-21 16:27:24 -05002699 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05002700 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002701 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2702 0, 1);
2703 path->lowest_level = 0;
2704 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2705 if (!path->nodes[i])
2706 break;
2707 free_extent_buffer(path->nodes[i]);
2708 path->nodes[i] = NULL;
2709 }
2710 btrfs_release_path(found_root, path);
Chris Mason0ef3e662008-05-24 14:04:53 -04002711 if (found_root == found_root->fs_info->extent_root)
2712 btrfs_extent_post_op(trans, found_root);
Chris Masonedbd8d42007-12-21 16:27:24 -05002713 btrfs_end_transaction(trans, found_root);
2714 }
2715
2716out:
2717 return 0;
2718}
2719
Chris Masona061fc82008-05-07 11:43:44 -04002720static int noinline del_extent_zero(struct btrfs_root *extent_root,
2721 struct btrfs_path *path,
2722 struct btrfs_key *extent_key)
2723{
2724 int ret;
2725 struct btrfs_trans_handle *trans;
2726
2727 trans = btrfs_start_transaction(extent_root, 1);
2728 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
2729 if (ret > 0) {
2730 ret = -EIO;
2731 goto out;
2732 }
2733 if (ret < 0)
2734 goto out;
2735 ret = btrfs_del_item(trans, extent_root, path);
2736out:
2737 btrfs_end_transaction(trans, extent_root);
2738 return ret;
2739}
2740
Chris Mason98ed5172008-01-03 10:01:48 -05002741static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2742 struct btrfs_path *path,
2743 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002744{
2745 struct btrfs_key key;
2746 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002747 struct extent_buffer *leaf;
Chris Mason0ef3e662008-05-24 14:04:53 -04002748 u64 last_file_objectid = 0;
2749 u64 last_file_root = 0;
2750 u64 last_file_offset = (u64)-1;
2751 u64 last_extent = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002752 u32 nritems;
2753 u32 item_size;
2754 int ret = 0;
2755
Chris Masona061fc82008-05-07 11:43:44 -04002756 if (extent_key->objectid == 0) {
2757 ret = del_extent_zero(extent_root, path, extent_key);
2758 goto out;
2759 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002760 key.objectid = extent_key->objectid;
2761 key.type = BTRFS_EXTENT_REF_KEY;
2762 key.offset = 0;
2763
2764 while(1) {
2765 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2766
Chris Masonedbd8d42007-12-21 16:27:24 -05002767 if (ret < 0)
2768 goto out;
2769
2770 ret = 0;
2771 leaf = path->nodes[0];
2772 nritems = btrfs_header_nritems(leaf);
Chris Masona061fc82008-05-07 11:43:44 -04002773 if (path->slots[0] == nritems) {
2774 ret = btrfs_next_leaf(extent_root, path);
2775 if (ret > 0) {
2776 ret = 0;
2777 goto out;
2778 }
2779 if (ret < 0)
2780 goto out;
Chris Masonbf4ef672008-05-08 13:26:18 -04002781 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04002782 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002783
2784 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masona061fc82008-05-07 11:43:44 -04002785 if (found_key.objectid != extent_key->objectid) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002786 break;
Chris Masona061fc82008-05-07 11:43:44 -04002787 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002788
Chris Masona061fc82008-05-07 11:43:44 -04002789 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002790 break;
Chris Masona061fc82008-05-07 11:43:44 -04002791 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002792
2793 key.offset = found_key.offset + 1;
2794 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2795
Chris Mason0ef3e662008-05-24 14:04:53 -04002796 ret = relocate_one_reference(extent_root, path, extent_key,
2797 &last_file_objectid,
2798 &last_file_offset,
2799 &last_file_root, last_extent);
Chris Masonedbd8d42007-12-21 16:27:24 -05002800 if (ret)
2801 goto out;
Chris Mason0ef3e662008-05-24 14:04:53 -04002802 last_extent = extent_key->objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05002803 }
2804 ret = 0;
2805out:
2806 btrfs_release_path(extent_root, path);
2807 return ret;
2808}
2809
Chris Masonec44a352008-04-28 15:29:52 -04002810static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
2811{
2812 u64 num_devices;
2813 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
2814 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
2815
Chris Masona061fc82008-05-07 11:43:44 -04002816 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04002817 if (num_devices == 1) {
2818 stripped |= BTRFS_BLOCK_GROUP_DUP;
2819 stripped = flags & ~stripped;
2820
2821 /* turn raid0 into single device chunks */
2822 if (flags & BTRFS_BLOCK_GROUP_RAID0)
2823 return stripped;
2824
2825 /* turn mirroring into duplication */
2826 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2827 BTRFS_BLOCK_GROUP_RAID10))
2828 return stripped | BTRFS_BLOCK_GROUP_DUP;
2829 return flags;
2830 } else {
2831 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04002832 if (flags & stripped)
2833 return flags;
2834
2835 stripped |= BTRFS_BLOCK_GROUP_DUP;
2836 stripped = flags & ~stripped;
2837
2838 /* switch duplicated blocks with raid1 */
2839 if (flags & BTRFS_BLOCK_GROUP_DUP)
2840 return stripped | BTRFS_BLOCK_GROUP_RAID1;
2841
2842 /* turn single device chunks into raid0 */
2843 return stripped | BTRFS_BLOCK_GROUP_RAID0;
2844 }
2845 return flags;
2846}
2847
Chris Mason0ef3e662008-05-24 14:04:53 -04002848int __alloc_chunk_for_shrink(struct btrfs_root *root,
2849 struct btrfs_block_group_cache *shrink_block_group,
2850 int force)
2851{
2852 struct btrfs_trans_handle *trans;
2853 u64 new_alloc_flags;
2854 u64 calc;
2855
2856 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
2857
2858 trans = btrfs_start_transaction(root, 1);
2859 new_alloc_flags = update_block_group_flags(root,
2860 shrink_block_group->flags);
2861 if (new_alloc_flags != shrink_block_group->flags) {
2862 calc =
2863 btrfs_block_group_used(&shrink_block_group->item);
2864 } else {
2865 calc = shrink_block_group->key.offset;
2866 }
2867 do_chunk_alloc(trans, root->fs_info->extent_root,
2868 calc + 2 * 1024 * 1024, new_alloc_flags, force);
2869 btrfs_end_transaction(trans, root);
2870 }
2871 return 0;
2872}
2873
Chris Mason8f18cf12008-04-25 16:53:30 -04002874int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05002875{
2876 struct btrfs_trans_handle *trans;
2877 struct btrfs_root *tree_root = root->fs_info->tree_root;
2878 struct btrfs_path *path;
2879 u64 cur_byte;
2880 u64 total_found;
Chris Mason8f18cf12008-04-25 16:53:30 -04002881 u64 shrink_last_byte;
2882 struct btrfs_block_group_cache *shrink_block_group;
Chris Masonedbd8d42007-12-21 16:27:24 -05002883 struct btrfs_fs_info *info = root->fs_info;
Chris Masonedbd8d42007-12-21 16:27:24 -05002884 struct btrfs_key key;
Yan73e48b22008-01-03 14:14:39 -05002885 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002886 struct extent_buffer *leaf;
2887 u32 nritems;
2888 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04002889 int progress;
Chris Masonedbd8d42007-12-21 16:27:24 -05002890
Chris Mason8f18cf12008-04-25 16:53:30 -04002891 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
2892 shrink_start);
2893 BUG_ON(!shrink_block_group);
2894
Chris Mason0ef3e662008-05-24 14:04:53 -04002895 shrink_last_byte = shrink_block_group->key.objectid +
2896 shrink_block_group->key.offset;
Chris Mason8f18cf12008-04-25 16:53:30 -04002897
2898 shrink_block_group->space_info->total_bytes -=
2899 shrink_block_group->key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05002900 path = btrfs_alloc_path();
2901 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05002902 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002903
Chris Mason323da792008-05-09 11:46:48 -04002904 printk("btrfs relocating block group %llu flags %llu\n",
2905 (unsigned long long)shrink_start,
2906 (unsigned long long)shrink_block_group->flags);
2907
Chris Mason0ef3e662008-05-24 14:04:53 -04002908 __alloc_chunk_for_shrink(root, shrink_block_group, 1);
Chris Mason323da792008-05-09 11:46:48 -04002909
Chris Mason0ef3e662008-05-24 14:04:53 -04002910again:
2911
Chris Mason8f18cf12008-04-25 16:53:30 -04002912 shrink_block_group->ro = 1;
2913
Chris Masonedbd8d42007-12-21 16:27:24 -05002914 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04002915 progress = 0;
Chris Mason8f18cf12008-04-25 16:53:30 -04002916 key.objectid = shrink_start;
Chris Masonedbd8d42007-12-21 16:27:24 -05002917 key.offset = 0;
2918 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05002919 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05002920
Yan73e48b22008-01-03 14:14:39 -05002921 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2922 if (ret < 0)
2923 goto out;
2924
Chris Mason0b86a832008-03-24 15:01:56 -04002925 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yan73e48b22008-01-03 14:14:39 -05002926 if (ret < 0)
2927 goto out;
Chris Mason8f18cf12008-04-25 16:53:30 -04002928
Yan73e48b22008-01-03 14:14:39 -05002929 if (ret == 0) {
2930 leaf = path->nodes[0];
2931 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04002932 if (found_key.objectid + found_key.offset > shrink_start &&
2933 found_key.objectid < shrink_last_byte) {
Yan73e48b22008-01-03 14:14:39 -05002934 cur_byte = found_key.objectid;
2935 key.objectid = cur_byte;
2936 }
2937 }
2938 btrfs_release_path(root, path);
2939
2940 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002941 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2942 if (ret < 0)
2943 goto out;
Yan73e48b22008-01-03 14:14:39 -05002944
Chris Masonedbd8d42007-12-21 16:27:24 -05002945 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05002946 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05002947next:
2948 if (path->slots[0] >= nritems) {
2949 ret = btrfs_next_leaf(root, path);
2950 if (ret < 0)
2951 goto out;
2952 if (ret == 1) {
2953 ret = 0;
2954 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05002955 }
Yan73e48b22008-01-03 14:14:39 -05002956 leaf = path->nodes[0];
2957 nritems = btrfs_header_nritems(leaf);
2958 }
2959
2960 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05002961
Chris Mason8f18cf12008-04-25 16:53:30 -04002962 if (found_key.objectid >= shrink_last_byte)
2963 break;
2964
Chris Mason725c8462008-01-04 16:47:16 -05002965 if (progress && need_resched()) {
2966 memcpy(&key, &found_key, sizeof(key));
2967 mutex_unlock(&root->fs_info->fs_mutex);
2968 cond_resched();
2969 mutex_lock(&root->fs_info->fs_mutex);
2970 btrfs_release_path(root, path);
2971 btrfs_search_slot(NULL, root, &key, path, 0, 0);
2972 progress = 0;
2973 goto next;
2974 }
2975 progress = 1;
2976
Yan73e48b22008-01-03 14:14:39 -05002977 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
2978 found_key.objectid + found_key.offset <= cur_byte) {
Chris Mason0ef3e662008-05-24 14:04:53 -04002979 memcpy(&key, &found_key, sizeof(key));
2980 key.offset++;
Yan73e48b22008-01-03 14:14:39 -05002981 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05002982 goto next;
2983 }
Yan73e48b22008-01-03 14:14:39 -05002984
Chris Masonedbd8d42007-12-21 16:27:24 -05002985 total_found++;
2986 cur_byte = found_key.objectid + found_key.offset;
2987 key.objectid = cur_byte;
2988 btrfs_release_path(root, path);
2989 ret = relocate_one_extent(root, path, &found_key);
Chris Mason0ef3e662008-05-24 14:04:53 -04002990 __alloc_chunk_for_shrink(root, shrink_block_group, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05002991 }
2992
2993 btrfs_release_path(root, path);
2994
2995 if (total_found > 0) {
Chris Mason323da792008-05-09 11:46:48 -04002996 printk("btrfs relocate found %llu last extent was %llu\n",
2997 (unsigned long long)total_found,
2998 (unsigned long long)found_key.objectid);
Chris Masonedbd8d42007-12-21 16:27:24 -05002999 trans = btrfs_start_transaction(tree_root, 1);
3000 btrfs_commit_transaction(trans, tree_root);
3001
3002 mutex_unlock(&root->fs_info->fs_mutex);
3003 btrfs_clean_old_snapshots(tree_root);
3004 mutex_lock(&root->fs_info->fs_mutex);
3005
3006 trans = btrfs_start_transaction(tree_root, 1);
3007 btrfs_commit_transaction(trans, tree_root);
3008 goto again;
3009 }
3010
Chris Mason8f18cf12008-04-25 16:53:30 -04003011 /*
3012 * we've freed all the extents, now remove the block
3013 * group item from the tree
3014 */
Chris Masonedbd8d42007-12-21 16:27:24 -05003015 trans = btrfs_start_transaction(root, 1);
Chris Mason8f18cf12008-04-25 16:53:30 -04003016 memcpy(&key, &shrink_block_group->key, sizeof(key));
Chris Mason4313b392008-01-03 09:08:48 -05003017
Chris Mason8f18cf12008-04-25 16:53:30 -04003018 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3019 if (ret > 0)
3020 ret = -EIO;
3021 if (ret < 0)
3022 goto out;
Yan73e48b22008-01-03 14:14:39 -05003023
Chris Mason0ef3e662008-05-24 14:04:53 -04003024 clear_extent_bits(&info->block_group_cache, key.objectid,
3025 key.objectid + key.offset - 1,
Chris Mason8f18cf12008-04-25 16:53:30 -04003026 (unsigned int)-1, GFP_NOFS);
Yan73e48b22008-01-03 14:14:39 -05003027
Chris Mason0ef3e662008-05-24 14:04:53 -04003028
3029 clear_extent_bits(&info->free_space_cache,
3030 key.objectid, key.objectid + key.offset - 1,
3031 (unsigned int)-1, GFP_NOFS);
3032
3033 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
3034 kfree(shrink_block_group);
3035
Chris Mason8f18cf12008-04-25 16:53:30 -04003036 btrfs_del_item(trans, root, path);
Chris Masonedbd8d42007-12-21 16:27:24 -05003037 btrfs_commit_transaction(trans, root);
Chris Mason0ef3e662008-05-24 14:04:53 -04003038
3039 /* the code to unpin extents might set a few bits in the free
3040 * space cache for this range again
3041 */
3042 clear_extent_bits(&info->free_space_cache,
3043 key.objectid, key.objectid + key.offset - 1,
3044 (unsigned int)-1, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003045out:
3046 btrfs_free_path(path);
3047 return ret;
3048}
3049
Chris Mason0b86a832008-03-24 15:01:56 -04003050int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3051 struct btrfs_key *key)
3052{
3053 int ret;
3054 struct btrfs_key found_key;
3055 struct extent_buffer *leaf;
3056 int slot;
3057
3058 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3059 if (ret < 0)
3060 return ret;
3061 while(1) {
3062 slot = path->slots[0];
3063 leaf = path->nodes[0];
3064 if (slot >= btrfs_header_nritems(leaf)) {
3065 ret = btrfs_next_leaf(root, path);
3066 if (ret == 0)
3067 continue;
3068 if (ret < 0)
3069 goto error;
3070 break;
3071 }
3072 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3073
3074 if (found_key.objectid >= key->objectid &&
3075 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
3076 return 0;
3077 path->slots[0]++;
3078 }
3079 ret = -ENOENT;
3080error:
3081 return ret;
3082}
3083
Chris Mason9078a3e2007-04-26 16:46:15 -04003084int btrfs_read_block_groups(struct btrfs_root *root)
3085{
3086 struct btrfs_path *path;
3087 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04003088 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04003089 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04003090 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003091 struct btrfs_space_info *space_info;
Chris Masond1310b22008-01-24 16:13:08 -05003092 struct extent_io_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003093 struct btrfs_key key;
3094 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04003095 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04003096
3097 block_group_cache = &info->block_group_cache;
Chris Masonbe744172007-05-06 10:15:01 -04003098 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04003099 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003100 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003101 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04003102 path = btrfs_alloc_path();
3103 if (!path)
3104 return -ENOMEM;
3105
3106 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003107 ret = find_first_block_group(root, path, &key);
3108 if (ret > 0) {
3109 ret = 0;
3110 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04003111 }
Chris Mason0b86a832008-03-24 15:01:56 -04003112 if (ret != 0)
3113 goto error;
3114
Chris Mason5f39d392007-10-15 16:14:19 -04003115 leaf = path->nodes[0];
3116 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003117 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04003118 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04003119 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04003120 break;
3121 }
Chris Mason3e1ad542007-05-07 20:03:49 -04003122
Chris Mason5f39d392007-10-15 16:14:19 -04003123 read_extent_buffer(leaf, &cache->item,
3124 btrfs_item_ptr_offset(leaf, path->slots[0]),
3125 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04003126 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04003127
Chris Mason9078a3e2007-04-26 16:46:15 -04003128 key.objectid = found_key.objectid + found_key.offset;
3129 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04003130 cache->flags = btrfs_block_group_flags(&cache->item);
3131 bit = 0;
3132 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04003133 bit = BLOCK_GROUP_DATA;
Chris Mason0b86a832008-03-24 15:01:56 -04003134 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3135 bit = BLOCK_GROUP_SYSTEM;
3136 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason96b51792007-10-15 16:15:19 -04003137 bit = BLOCK_GROUP_METADATA;
Chris Mason31f3c992007-04-30 15:25:45 -04003138 }
Chris Mason8790d502008-04-03 16:29:03 -04003139 set_avail_alloc_bits(info, cache->flags);
Chris Mason96b51792007-10-15 16:15:19 -04003140
Chris Mason6324fbf2008-03-24 15:01:59 -04003141 ret = update_space_info(info, cache->flags, found_key.offset,
3142 btrfs_block_group_used(&cache->item),
3143 &space_info);
3144 BUG_ON(ret);
3145 cache->space_info = space_info;
3146
Chris Mason96b51792007-10-15 16:15:19 -04003147 /* use EXTENT_LOCKED to prevent merging */
3148 set_extent_bits(block_group_cache, found_key.objectid,
3149 found_key.objectid + found_key.offset - 1,
3150 bit | EXTENT_LOCKED, GFP_NOFS);
3151 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04003152 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04003153
Chris Mason9078a3e2007-04-26 16:46:15 -04003154 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04003155 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04003156 break;
3157 }
Chris Mason0b86a832008-03-24 15:01:56 -04003158 ret = 0;
3159error:
Chris Mason9078a3e2007-04-26 16:46:15 -04003160 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003161 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003162}
Chris Mason6324fbf2008-03-24 15:01:59 -04003163
3164int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3165 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04003166 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04003167 u64 size)
3168{
3169 int ret;
3170 int bit = 0;
3171 struct btrfs_root *extent_root;
3172 struct btrfs_block_group_cache *cache;
3173 struct extent_io_tree *block_group_cache;
3174
3175 extent_root = root->fs_info->extent_root;
3176 block_group_cache = &root->fs_info->block_group_cache;
3177
Chris Mason8f18cf12008-04-25 16:53:30 -04003178 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04003179 BUG_ON(!cache);
Chris Masone17cade2008-04-15 15:41:47 -04003180 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04003181 cache->key.offset = size;
Chris Mason6324fbf2008-03-24 15:01:59 -04003182 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04003183
Chris Mason6324fbf2008-03-24 15:01:59 -04003184 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04003185 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3186 cache->flags = type;
3187 btrfs_set_block_group_flags(&cache->item, type);
3188
3189 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
3190 &cache->space_info);
3191 BUG_ON(ret);
3192
Chris Masond18a2c42008-04-04 15:40:00 -04003193 bit = block_group_state_bits(type);
Chris Masone17cade2008-04-15 15:41:47 -04003194 set_extent_bits(block_group_cache, chunk_offset,
3195 chunk_offset + size - 1,
Chris Mason6324fbf2008-03-24 15:01:59 -04003196 bit | EXTENT_LOCKED, GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04003197
Chris Masone17cade2008-04-15 15:41:47 -04003198 set_state_private(block_group_cache, chunk_offset,
3199 (unsigned long)cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04003200 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3201 sizeof(cache->item));
3202 BUG_ON(ret);
3203
3204 finish_current_insert(trans, extent_root);
3205 ret = del_pending_extents(trans, extent_root);
3206 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04003207 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason6324fbf2008-03-24 15:01:59 -04003208 return 0;
3209}