blob: 30a5094fffa71b86e65d3465f52a1250738c6696 [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 Mason5276aed2007-06-11 21:33:38 -0400139struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
140 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400141 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400142{
Chris Masond1310b22008-01-24 16:13:08 -0500143 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -0400144 struct btrfs_block_group_cache *block_group = NULL;
145 u64 ptr;
146 u64 start;
147 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400148 int ret;
149
Chris Masona061fc82008-05-07 11:43:44 -0400150 bytenr = max_t(u64, bytenr,
151 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Mason96b51792007-10-15 16:15:19 -0400152 block_group_cache = &info->block_group_cache;
153 ret = find_first_extent_bit(block_group_cache,
Chris Masondb945352007-10-15 16:15:53 -0400154 bytenr, &start, &end,
Chris Mason0b86a832008-03-24 15:01:56 -0400155 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
156 BLOCK_GROUP_SYSTEM);
Chris Masonbe744172007-05-06 10:15:01 -0400157 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400158 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400159 }
Chris Mason96b51792007-10-15 16:15:19 -0400160 ret = get_state_private(block_group_cache, start, &ptr);
161 if (ret)
162 return NULL;
163
Jens Axboeae2f5412007-10-19 09:22:59 -0400164 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Yan5cf66422007-11-16 14:57:09 -0500165 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400166 block_group->key.objectid + block_group->key.offset)
167 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400168 return NULL;
169}
Chris Mason0b86a832008-03-24 15:01:56 -0400170
171static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
172{
Chris Mason593060d2008-03-25 16:50:33 -0400173 return (cache->flags & bits) == bits;
Chris Mason0b86a832008-03-24 15:01:56 -0400174}
175
176static int noinline find_search_start(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500177 struct btrfs_block_group_cache **cache_ret,
Chris Mason0b86a832008-03-24 15:01:56 -0400178 u64 *start_ret, int num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400179{
Chris Masone37c9e62007-05-09 20:13:14 -0400180 int ret;
181 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masond7fc6402008-02-18 12:12:38 -0500182 struct extent_io_tree *free_space_cache;
Chris Mason7d1660d2008-03-24 15:02:03 -0400183 struct extent_state *state;
Chris Masone19caa52007-10-15 16:17:44 -0400184 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400185 u64 start = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500186 u64 cache_miss = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500187 u64 total_fs_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -0400188 u64 search_start = *start_ret;
Chris Masonf84a8b32007-11-06 10:26:29 -0500189 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400190
Chris Mason0b86a832008-03-24 15:01:56 -0400191 if (!cache)
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500192 goto out;
Chris Mason8f18cf12008-04-25 16:53:30 -0400193
Chris Masonc31f8832008-01-08 15:46:31 -0500194 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masond7fc6402008-02-18 12:12:38 -0500195 free_space_cache = &root->fs_info->free_space_cache;
196
Chris Masone37c9e62007-05-09 20:13:14 -0400197again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400198 ret = cache_block_group(root, cache);
199 if (ret)
200 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500201
Chris Masone19caa52007-10-15 16:17:44 -0400202 last = max(search_start, cache->key.objectid);
Chris Mason8f18cf12008-04-25 16:53:30 -0400203 if (!block_group_bits(cache, data) || cache->ro) {
Chris Mason0b86a832008-03-24 15:01:56 -0400204 goto new_group;
205 }
Chris Masone19caa52007-10-15 16:17:44 -0400206
Chris Mason7d1660d2008-03-24 15:02:03 -0400207 spin_lock_irq(&free_space_cache->lock);
208 state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
Chris Masone37c9e62007-05-09 20:13:14 -0400209 while(1) {
Chris Mason7d1660d2008-03-24 15:02:03 -0400210 if (!state) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500211 if (!cache_miss)
212 cache_miss = last;
Chris Mason7d1660d2008-03-24 15:02:03 -0400213 spin_unlock_irq(&free_space_cache->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400214 goto new_group;
215 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400216
Chris Mason7d1660d2008-03-24 15:02:03 -0400217 start = max(last, state->start);
218 last = state->end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500219 if (last - start < num) {
220 if (last == cache->key.objectid + cache->key.offset)
221 cache_miss = start;
Chris Mason7d1660d2008-03-24 15:02:03 -0400222 do {
223 state = extent_state_next(state);
224 } while(state && !(state->state & EXTENT_DIRTY));
Chris Masonf510cfe2007-10-15 16:14:48 -0400225 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500226 }
Chris Mason7d1660d2008-03-24 15:02:03 -0400227 spin_unlock_irq(&free_space_cache->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -0400228 if (cache->ro)
229 goto new_group;
Chris Mason0b86a832008-03-24 15:01:56 -0400230 if (start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400231 goto new_group;
Chris Masonc31f8832008-01-08 15:46:31 -0500232 if (start + num > total_fs_bytes)
233 goto new_group;
Chris Mason8790d502008-04-03 16:29:03 -0400234 if (!block_group_bits(cache, data)) {
Chris Mason611f0e02008-04-03 16:29:03 -0400235 printk("block group bits don't match %Lu %d\n", cache->flags, data);
Chris Mason8790d502008-04-03 16:29:03 -0400236 }
Chris Mason0b86a832008-03-24 15:01:56 -0400237 *start_ret = start;
238 return 0;
Chris Mason8790d502008-04-03 16:29:03 -0400239 }
240out:
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500241 cache = btrfs_lookup_block_group(root->fs_info, search_start);
242 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -0400243 printk("Unable to find block group for %Lu\n", search_start);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500244 WARN_ON(1);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500245 }
Chris Mason0b86a832008-03-24 15:01:56 -0400246 return -ENOSPC;
Chris Masone37c9e62007-05-09 20:13:14 -0400247
248new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400249 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500250wrapped:
Chris Masone19caa52007-10-15 16:17:44 -0400251 cache = btrfs_lookup_block_group(root->fs_info, last);
Chris Masonc31f8832008-01-08 15:46:31 -0500252 if (!cache || cache->key.objectid >= total_fs_bytes) {
Chris Mason0e4de582007-11-26 10:55:49 -0500253no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500254 if (!wrapped) {
255 wrapped = 1;
256 last = search_start;
Chris Masonf84a8b32007-11-06 10:26:29 -0500257 goto wrapped;
258 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500259 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400260 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500261 if (cache_miss && !cache->cached) {
262 cache_block_group(root, cache);
263 last = cache_miss;
Chris Mason257d0ce2007-11-07 21:08:16 -0500264 cache = btrfs_lookup_block_group(root->fs_info, last);
265 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500266 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500267 if (!cache)
268 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400269 *cache_ret = cache;
Chris Mason257d0ce2007-11-07 21:08:16 -0500270 cache_miss = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400271 goto again;
272}
273
Chris Mason84f54cf2007-06-12 07:43:08 -0400274static u64 div_factor(u64 num, int factor)
275{
Chris Mason257d0ce2007-11-07 21:08:16 -0500276 if (factor == 10)
277 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400278 num *= factor;
279 do_div(num, 10);
280 return num;
281}
282
Chris Mason6324fbf2008-03-24 15:01:59 -0400283static int block_group_state_bits(u64 flags)
284{
285 int bits = 0;
286 if (flags & BTRFS_BLOCK_GROUP_DATA)
287 bits |= BLOCK_GROUP_DATA;
288 if (flags & BTRFS_BLOCK_GROUP_METADATA)
289 bits |= BLOCK_GROUP_METADATA;
290 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
291 bits |= BLOCK_GROUP_SYSTEM;
292 return bits;
293}
294
Chris Mason31f3c992007-04-30 15:25:45 -0400295struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
296 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400297 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400298 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400299{
Chris Mason96b51792007-10-15 16:15:19 -0400300 struct btrfs_block_group_cache *cache;
Chris Masond1310b22008-01-24 16:13:08 -0500301 struct extent_io_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400302 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400303 struct btrfs_fs_info *info = root->fs_info;
304 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400305 u64 last = 0;
306 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400307 u64 start;
308 u64 end;
309 u64 free_check;
310 u64 ptr;
Chris Masonc31f8832008-01-08 15:46:31 -0500311 u64 total_fs_bytes;
Chris Mason96b51792007-10-15 16:15:19 -0400312 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400313 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400314 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400315 int factor = 10;
Chris Masonde428b62007-05-18 13:28:27 -0400316
Chris Mason96b51792007-10-15 16:15:19 -0400317 block_group_cache = &info->block_group_cache;
Chris Masonc31f8832008-01-08 15:46:31 -0500318 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Mason96b51792007-10-15 16:15:19 -0400319
Chris Masona236aed2008-04-29 09:38:00 -0400320 if (data & BTRFS_BLOCK_GROUP_METADATA)
321 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400322
Chris Mason6324fbf2008-03-24 15:01:59 -0400323 bit = block_group_state_bits(data);
Chris Masonbe744172007-05-06 10:15:01 -0400324
Chris Masonc31f8832008-01-08 15:46:31 -0500325 if (search_start && search_start < total_fs_bytes) {
Chris Masonbe744172007-05-06 10:15:01 -0400326 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400327 shint = btrfs_lookup_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400328 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonbe744172007-05-06 10:15:01 -0400329 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500330 if (used + shint->pinned <
331 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400332 return shint;
333 }
334 }
335 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400336 if (hint && !hint->ro && block_group_bits(hint, data) &&
Chris Mason0b86a832008-03-24 15:01:56 -0400337 hint->key.objectid < total_fs_bytes) {
Chris Mason31f3c992007-04-30 15:25:45 -0400338 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500339 if (used + hint->pinned <
340 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400341 return hint;
342 }
Chris Masone19caa52007-10-15 16:17:44 -0400343 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400344 hint_last = last;
345 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400346 if (hint)
347 hint_last = max(hint->key.objectid, search_start);
348 else
349 hint_last = search_start;
350
Chris Masonc31f8832008-01-08 15:46:31 -0500351 if (hint_last >= total_fs_bytes)
352 hint_last = search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400353 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400354 }
Chris Mason31f3c992007-04-30 15:25:45 -0400355again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400356 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400357 ret = find_first_extent_bit(block_group_cache, last,
358 &start, &end, bit);
359 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400360 break;
Chris Mason96b51792007-10-15 16:15:19 -0400361
362 ret = get_state_private(block_group_cache, start, &ptr);
363 if (ret)
364 break;
365
Jens Axboeae2f5412007-10-19 09:22:59 -0400366 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400367 last = cache->key.objectid + cache->key.offset;
368 used = btrfs_block_group_used(&cache->item);
369
Chris Masonc31f8832008-01-08 15:46:31 -0500370 if (cache->key.objectid > total_fs_bytes)
371 break;
372
Chris Mason8f18cf12008-04-25 16:53:30 -0400373 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason8790d502008-04-03 16:29:03 -0400374 if (full_search)
375 free_check = cache->key.offset;
376 else
377 free_check = div_factor(cache->key.offset,
378 factor);
Chris Mason6324fbf2008-03-24 15:01:59 -0400379
Chris Mason8790d502008-04-03 16:29:03 -0400380 if (used + cache->pinned < free_check) {
381 found_group = cache;
382 goto found;
383 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400384 }
Chris Masonde428b62007-05-18 13:28:27 -0400385 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400386 }
Chris Mason31f3c992007-04-30 15:25:45 -0400387 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400388 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400389 full_search = 1;
390 goto again;
391 }
Chris Masonbe744172007-05-06 10:15:01 -0400392found:
Chris Mason31f3c992007-04-30 15:25:45 -0400393 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400394}
395
Chris Mason7bb86312007-12-11 09:25:06 -0500396static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500397 u64 owner, u64 owner_offset)
398{
399 u32 high_crc = ~(u32)0;
400 u32 low_crc = ~(u32)0;
401 __le64 lenum;
Chris Mason74493f72007-12-11 09:25:06 -0500402 lenum = cpu_to_le64(root_objectid);
Miguela5eb62e2008-04-11 15:45:51 -0400403 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500404 lenum = cpu_to_le64(ref_generation);
Miguela5eb62e2008-04-11 15:45:51 -0400405 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500406 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
407 lenum = cpu_to_le64(owner);
Miguela5eb62e2008-04-11 15:45:51 -0400408 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500409 lenum = cpu_to_le64(owner_offset);
Miguela5eb62e2008-04-11 15:45:51 -0400410 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500411 }
Chris Mason74493f72007-12-11 09:25:06 -0500412 return ((u64)high_crc << 32) | (u64)low_crc;
413}
414
Chris Mason7bb86312007-12-11 09:25:06 -0500415static int match_extent_ref(struct extent_buffer *leaf,
416 struct btrfs_extent_ref *disk_ref,
417 struct btrfs_extent_ref *cpu_ref)
418{
419 int ret;
420 int len;
421
422 if (cpu_ref->objectid)
423 len = sizeof(*cpu_ref);
424 else
425 len = 2 * sizeof(u64);
426 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
427 len);
428 return ret == 0;
429}
430
Chris Mason98ed5172008-01-03 10:01:48 -0500431static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
432 struct btrfs_root *root,
433 struct btrfs_path *path, u64 bytenr,
434 u64 root_objectid,
435 u64 ref_generation, u64 owner,
436 u64 owner_offset, int del)
Chris Mason7bb86312007-12-11 09:25:06 -0500437{
438 u64 hash;
439 struct btrfs_key key;
440 struct btrfs_key found_key;
441 struct btrfs_extent_ref ref;
442 struct extent_buffer *leaf;
443 struct btrfs_extent_ref *disk_ref;
444 int ret;
445 int ret2;
446
447 btrfs_set_stack_ref_root(&ref, root_objectid);
448 btrfs_set_stack_ref_generation(&ref, ref_generation);
449 btrfs_set_stack_ref_objectid(&ref, owner);
450 btrfs_set_stack_ref_offset(&ref, owner_offset);
451
452 hash = hash_extent_ref(root_objectid, ref_generation, owner,
453 owner_offset);
454 key.offset = hash;
455 key.objectid = bytenr;
456 key.type = BTRFS_EXTENT_REF_KEY;
457
458 while (1) {
459 ret = btrfs_search_slot(trans, root, &key, path,
460 del ? -1 : 0, del);
461 if (ret < 0)
462 goto out;
463 leaf = path->nodes[0];
464 if (ret != 0) {
465 u32 nritems = btrfs_header_nritems(leaf);
466 if (path->slots[0] >= nritems) {
467 ret2 = btrfs_next_leaf(root, path);
468 if (ret2)
469 goto out;
470 leaf = path->nodes[0];
471 }
472 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
473 if (found_key.objectid != bytenr ||
474 found_key.type != BTRFS_EXTENT_REF_KEY)
475 goto out;
476 key.offset = found_key.offset;
477 if (del) {
478 btrfs_release_path(root, path);
479 continue;
480 }
481 }
482 disk_ref = btrfs_item_ptr(path->nodes[0],
483 path->slots[0],
484 struct btrfs_extent_ref);
485 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
486 ret = 0;
487 goto out;
488 }
489 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
490 key.offset = found_key.offset + 1;
491 btrfs_release_path(root, path);
492 }
493out:
494 return ret;
495}
496
Chris Masond8d5f3e2007-12-11 12:42:00 -0500497/*
498 * Back reference rules. Back refs have three main goals:
499 *
500 * 1) differentiate between all holders of references to an extent so that
501 * when a reference is dropped we can make sure it was a valid reference
502 * before freeing the extent.
503 *
504 * 2) Provide enough information to quickly find the holders of an extent
505 * if we notice a given block is corrupted or bad.
506 *
507 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
508 * maintenance. This is actually the same as #2, but with a slightly
509 * different use case.
510 *
511 * File extents can be referenced by:
512 *
513 * - multiple snapshots, subvolumes, or different generations in one subvol
514 * - different files inside a single subvolume (in theory, not implemented yet)
515 * - different offsets inside a file (bookend extents in file.c)
516 *
517 * The extent ref structure has fields for:
518 *
519 * - Objectid of the subvolume root
520 * - Generation number of the tree holding the reference
521 * - objectid of the file holding the reference
522 * - offset in the file corresponding to the key holding the reference
523 *
524 * When a file extent is allocated the fields are filled in:
525 * (root_key.objectid, trans->transid, inode objectid, offset in file)
526 *
527 * When a leaf is cow'd new references are added for every file extent found
528 * in the leaf. It looks the same as the create case, but trans->transid
529 * will be different when the block is cow'd.
530 *
531 * (root_key.objectid, trans->transid, inode objectid, offset in file)
532 *
533 * When a file extent is removed either during snapshot deletion or file
534 * truncation, the corresponding back reference is found
535 * by searching for:
536 *
537 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
538 * inode objectid, offset in file)
539 *
540 * Btree extents can be referenced by:
541 *
542 * - Different subvolumes
543 * - Different generations of the same subvolume
544 *
545 * Storing sufficient information for a full reverse mapping of a btree
546 * block would require storing the lowest key of the block in the backref,
547 * and it would require updating that lowest key either before write out or
548 * every time it changed. Instead, the objectid of the lowest key is stored
549 * along with the level of the tree block. This provides a hint
550 * about where in the btree the block can be found. Searches through the
551 * btree only need to look for a pointer to that block, so they stop one
552 * level higher than the level recorded in the backref.
553 *
554 * Some btrees do not do reference counting on their extents. These
555 * include the extent tree and the tree of tree roots. Backrefs for these
556 * trees always have a generation of zero.
557 *
558 * When a tree block is created, back references are inserted:
559 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500560 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500561 *
562 * When a tree block is cow'd in a reference counted root,
563 * new back references are added for all the blocks it points to.
564 * These are of the form (trans->transid will have increased since creation):
565 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500566 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500567 *
568 * Because the lowest_key_objectid and the level are just hints
569 * they are not used when backrefs are deleted. When a backref is deleted:
570 *
571 * if backref was for a tree root:
572 * root_objectid = root->root_key.objectid
573 * else
574 * root_objectid = btrfs_header_owner(parent)
575 *
576 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
577 *
578 * Back Reference Key hashing:
579 *
580 * Back references have four fields, each 64 bits long. Unfortunately,
581 * This is hashed into a single 64 bit number and placed into the key offset.
582 * The key objectid corresponds to the first byte in the extent, and the
583 * key type is set to BTRFS_EXTENT_REF_KEY
584 */
Chris Mason7bb86312007-12-11 09:25:06 -0500585int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
586 struct btrfs_root *root,
587 struct btrfs_path *path, u64 bytenr,
588 u64 root_objectid, u64 ref_generation,
589 u64 owner, u64 owner_offset)
Chris Mason74493f72007-12-11 09:25:06 -0500590{
591 u64 hash;
592 struct btrfs_key key;
593 struct btrfs_extent_ref ref;
Chris Mason7bb86312007-12-11 09:25:06 -0500594 struct btrfs_extent_ref *disk_ref;
Chris Mason74493f72007-12-11 09:25:06 -0500595 int ret;
596
597 btrfs_set_stack_ref_root(&ref, root_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500598 btrfs_set_stack_ref_generation(&ref, ref_generation);
Chris Mason74493f72007-12-11 09:25:06 -0500599 btrfs_set_stack_ref_objectid(&ref, owner);
600 btrfs_set_stack_ref_offset(&ref, owner_offset);
601
Chris Mason7bb86312007-12-11 09:25:06 -0500602 hash = hash_extent_ref(root_objectid, ref_generation, owner,
603 owner_offset);
Chris Mason74493f72007-12-11 09:25:06 -0500604 key.offset = hash;
605 key.objectid = bytenr;
606 key.type = BTRFS_EXTENT_REF_KEY;
607
608 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
609 while (ret == -EEXIST) {
Chris Mason7bb86312007-12-11 09:25:06 -0500610 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
611 struct btrfs_extent_ref);
612 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
613 goto out;
614 key.offset++;
615 btrfs_release_path(root, path);
616 ret = btrfs_insert_empty_item(trans, root, path, &key,
617 sizeof(ref));
Chris Mason74493f72007-12-11 09:25:06 -0500618 }
Chris Mason7bb86312007-12-11 09:25:06 -0500619 if (ret)
620 goto out;
621 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
622 struct btrfs_extent_ref);
623 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
624 sizeof(ref));
625 btrfs_mark_buffer_dirty(path->nodes[0]);
626out:
627 btrfs_release_path(root, path);
628 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500629}
630
Chris Masonb18c6682007-04-17 13:26:50 -0400631int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
632 struct btrfs_root *root,
Chris Mason74493f72007-12-11 09:25:06 -0500633 u64 bytenr, u64 num_bytes,
Chris Mason7bb86312007-12-11 09:25:06 -0500634 u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500635 u64 owner, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500636{
Chris Mason5caf2a02007-04-02 11:20:42 -0400637 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500638 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400639 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400640 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400641 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400642 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500643
Chris Masondb945352007-10-15 16:15:53 -0400644 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400645 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400646 if (!path)
647 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400648
Chris Mason3c12ac72008-04-21 12:01:38 -0400649 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400650 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400651 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400652 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400653 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400654 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400655 if (ret < 0)
656 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400657 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500658 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400659 }
Chris Mason02217ed2007-03-02 16:08:05 -0500660 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400661 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400662 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400663 refs = btrfs_extent_refs(l, item);
664 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400665 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500666
Chris Mason5caf2a02007-04-02 11:20:42 -0400667 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500668
Chris Mason3c12ac72008-04-21 12:01:38 -0400669 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -0500670 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
671 path, bytenr, root_objectid,
672 ref_generation, owner, owner_offset);
673 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400674 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400675 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500676
677 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500678 return 0;
679}
680
Chris Masone9d0b132007-08-10 14:06:19 -0400681int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
682 struct btrfs_root *root)
683{
684 finish_current_insert(trans, root->fs_info->extent_root);
685 del_pending_extents(trans, root->fs_info->extent_root);
686 return 0;
687}
688
Chris Masonb18c6682007-04-17 13:26:50 -0400689static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400690 struct btrfs_root *root, u64 bytenr,
691 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500692{
Chris Mason5caf2a02007-04-02 11:20:42 -0400693 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500694 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400695 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400696 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400697 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400698
Chris Masondb945352007-10-15 16:15:53 -0400699 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400700 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400701 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400702 key.objectid = bytenr;
703 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400704 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400705 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400706 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400707 if (ret < 0)
708 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400709 if (ret != 0) {
710 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400711 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500712 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400713 }
714 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400715 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400716 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400717out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400718 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500719 return 0;
720}
721
Chris Masonbe20aa92007-12-17 20:14:01 -0500722u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
723 struct btrfs_path *count_path,
Chris Masona68d5932008-05-08 14:11:56 -0400724 u64 expected_owner,
Chris Masonbe20aa92007-12-17 20:14:01 -0500725 u64 first_extent)
726{
727 struct btrfs_root *extent_root = root->fs_info->extent_root;
728 struct btrfs_path *path;
729 u64 bytenr;
730 u64 found_objectid;
Chris Masona68d5932008-05-08 14:11:56 -0400731 u64 found_owner;
Chris Mason56b453c2008-01-03 09:08:27 -0500732 u64 root_objectid = root->root_key.objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500733 u32 total_count = 0;
734 u32 cur_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500735 u32 nritems;
736 int ret;
737 struct btrfs_key key;
738 struct btrfs_key found_key;
739 struct extent_buffer *l;
740 struct btrfs_extent_item *item;
741 struct btrfs_extent_ref *ref_item;
742 int level = -1;
743
744 path = btrfs_alloc_path();
745again:
746 if (level == -1)
747 bytenr = first_extent;
748 else
749 bytenr = count_path->nodes[level]->start;
750
751 cur_count = 0;
752 key.objectid = bytenr;
753 key.offset = 0;
754
755 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
756 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
757 if (ret < 0)
758 goto out;
759 BUG_ON(ret == 0);
760
761 l = path->nodes[0];
762 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
763
764 if (found_key.objectid != bytenr ||
765 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
766 goto out;
767 }
768
769 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masonbe20aa92007-12-17 20:14:01 -0500770 while (1) {
Chris Masonbd098352008-01-03 13:23:19 -0500771 l = path->nodes[0];
Chris Masonbe20aa92007-12-17 20:14:01 -0500772 nritems = btrfs_header_nritems(l);
773 if (path->slots[0] >= nritems) {
774 ret = btrfs_next_leaf(extent_root, path);
775 if (ret == 0)
776 continue;
777 break;
778 }
779 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
780 if (found_key.objectid != bytenr)
781 break;
Chris Masonbd098352008-01-03 13:23:19 -0500782
Chris Masonbe20aa92007-12-17 20:14:01 -0500783 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
784 path->slots[0]++;
785 continue;
786 }
787
788 cur_count++;
789 ref_item = btrfs_item_ptr(l, path->slots[0],
790 struct btrfs_extent_ref);
791 found_objectid = btrfs_ref_root(l, ref_item);
792
Chris Mason56b453c2008-01-03 09:08:27 -0500793 if (found_objectid != root_objectid) {
794 total_count = 2;
Chris Mason4313b392008-01-03 09:08:48 -0500795 goto out;
Chris Mason56b453c2008-01-03 09:08:27 -0500796 }
Chris Masona68d5932008-05-08 14:11:56 -0400797 if (level == -1) {
798 found_owner = btrfs_ref_objectid(l, ref_item);
799 if (found_owner != expected_owner) {
800 total_count = 2;
801 goto out;
802 }
803 }
Chris Mason56b453c2008-01-03 09:08:27 -0500804 total_count = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500805 path->slots[0]++;
806 }
807 if (cur_count == 0) {
808 total_count = 0;
809 goto out;
810 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500811 if (level >= 0 && root->node == count_path->nodes[level])
812 goto out;
813 level++;
814 btrfs_release_path(root, path);
815 goto again;
816
817out:
818 btrfs_free_path(path);
819 return total_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500820}
Chris Masonc5739bb2007-04-10 09:27:04 -0400821int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -0500822 struct btrfs_root *root, u64 owner_objectid)
Chris Masonc5739bb2007-04-10 09:27:04 -0400823{
Chris Mason7bb86312007-12-11 09:25:06 -0500824 u64 generation;
825 u64 key_objectid;
826 u64 level;
827 u32 nritems;
828 struct btrfs_disk_key disk_key;
829
830 level = btrfs_header_level(root->node);
831 generation = trans->transid;
832 nritems = btrfs_header_nritems(root->node);
833 if (nritems > 0) {
834 if (level == 0)
835 btrfs_item_key(root->node, &disk_key, 0);
836 else
837 btrfs_node_key(root->node, &disk_key, 0);
838 key_objectid = btrfs_disk_key_objectid(&disk_key);
839 } else {
840 key_objectid = 0;
841 }
Chris Masondb945352007-10-15 16:15:53 -0400842 return btrfs_inc_extent_ref(trans, root, root->node->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500843 root->node->len, owner_objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500844 generation, level, key_objectid);
Chris Masonc5739bb2007-04-10 09:27:04 -0400845}
846
Chris Masone089f052007-03-16 16:20:31 -0400847int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400848 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500849{
Chris Masondb945352007-10-15 16:15:53 -0400850 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400851 u32 nritems;
852 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400853 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500854 int i;
Chris Masondb945352007-10-15 16:15:53 -0400855 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400856 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400857 int faili;
Chris Masona28ec192007-03-06 20:08:01 -0500858
Chris Mason3768f362007-03-13 16:47:54 -0400859 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500860 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400861
Chris Masondb945352007-10-15 16:15:53 -0400862 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400863 nritems = btrfs_header_nritems(buf);
864 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400865 if (level == 0) {
866 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400867 btrfs_item_key_to_cpu(buf, &key, i);
868 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400869 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400870 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400871 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400872 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454df2007-04-19 13:37:44 -0400873 BTRFS_FILE_EXTENT_INLINE)
874 continue;
Chris Masondb945352007-10-15 16:15:53 -0400875 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
876 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400877 continue;
Chris Masondb945352007-10-15 16:15:53 -0400878 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500879 btrfs_file_extent_disk_num_bytes(buf, fi),
880 root->root_key.objectid, trans->transid,
881 key.objectid, key.offset);
Chris Mason54aa1f42007-06-22 14:16:25 -0400882 if (ret) {
883 faili = i;
884 goto fail;
885 }
Chris Mason6407bf62007-03-27 06:33:00 -0400886 } else {
Chris Masondb945352007-10-15 16:15:53 -0400887 bytenr = btrfs_node_blockptr(buf, i);
Chris Mason6caab482007-12-13 09:48:07 -0500888 btrfs_node_key_to_cpu(buf, &key, i);
Chris Masondb945352007-10-15 16:15:53 -0400889 ret = btrfs_inc_extent_ref(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500890 btrfs_level_size(root, level - 1),
891 root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500892 trans->transid,
893 level - 1, key.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400894 if (ret) {
895 faili = i;
896 goto fail;
897 }
Chris Mason6407bf62007-03-27 06:33:00 -0400898 }
Chris Mason02217ed2007-03-02 16:08:05 -0500899 }
900 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400901fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400902 WARN_ON(1);
Chris Mason7bb86312007-12-11 09:25:06 -0500903#if 0
Chris Mason54aa1f42007-06-22 14:16:25 -0400904 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400905 if (level == 0) {
906 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400907 btrfs_item_key_to_cpu(buf, &key, i);
908 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400909 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400910 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400911 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400912 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400913 BTRFS_FILE_EXTENT_INLINE)
914 continue;
Chris Masondb945352007-10-15 16:15:53 -0400915 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
916 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400917 continue;
Chris Masondb945352007-10-15 16:15:53 -0400918 err = btrfs_free_extent(trans, root, disk_bytenr,
919 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400920 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400921 BUG_ON(err);
922 } else {
Chris Masondb945352007-10-15 16:15:53 -0400923 bytenr = btrfs_node_blockptr(buf, i);
924 err = btrfs_free_extent(trans, root, bytenr,
925 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400926 BUG_ON(err);
927 }
928 }
Chris Mason7bb86312007-12-11 09:25:06 -0500929#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400930 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500931}
932
Chris Mason9078a3e2007-04-26 16:46:15 -0400933static int write_one_cache_group(struct btrfs_trans_handle *trans,
934 struct btrfs_root *root,
935 struct btrfs_path *path,
936 struct btrfs_block_group_cache *cache)
937{
938 int ret;
939 int pending_ret;
940 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400941 unsigned long bi;
942 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400943
Chris Mason9078a3e2007-04-26 16:46:15 -0400944 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400945 if (ret < 0)
946 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400947 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400948
949 leaf = path->nodes[0];
950 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
951 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
952 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400953 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400954fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400955 finish_current_insert(trans, extent_root);
956 pending_ret = del_pending_extents(trans, extent_root);
957 if (ret)
958 return ret;
959 if (pending_ret)
960 return pending_ret;
961 return 0;
962
963}
964
Chris Mason96b51792007-10-15 16:15:19 -0400965int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
966 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400967{
Chris Masond1310b22008-01-24 16:13:08 -0500968 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -0400969 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400970 int ret;
971 int err = 0;
972 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400973 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400974 u64 last = 0;
975 u64 start;
976 u64 end;
977 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400978
Chris Mason96b51792007-10-15 16:15:19 -0400979 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400980 path = btrfs_alloc_path();
981 if (!path)
982 return -ENOMEM;
983
984 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400985 ret = find_first_extent_bit(block_group_cache, last,
986 &start, &end, BLOCK_GROUP_DIRTY);
987 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400988 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400989
Chris Mason96b51792007-10-15 16:15:19 -0400990 last = end + 1;
991 ret = get_state_private(block_group_cache, start, &ptr);
992 if (ret)
993 break;
Jens Axboeae2f5412007-10-19 09:22:59 -0400994 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400995 err = write_one_cache_group(trans, root,
996 path, cache);
997 /*
998 * if we fail to write the cache group, we want
999 * to keep it marked dirty in hopes that a later
1000 * write will work
1001 */
1002 if (err) {
1003 werr = err;
1004 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001005 }
Chris Mason96b51792007-10-15 16:15:19 -04001006 clear_extent_bits(block_group_cache, start, end,
1007 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001008 }
1009 btrfs_free_path(path);
1010 return werr;
1011}
1012
Chris Mason6324fbf2008-03-24 15:01:59 -04001013static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1014 u64 flags)
1015{
1016 struct list_head *head = &info->space_info;
1017 struct list_head *cur;
1018 struct btrfs_space_info *found;
1019 list_for_each(cur, head) {
1020 found = list_entry(cur, struct btrfs_space_info, list);
1021 if (found->flags == flags)
1022 return found;
1023 }
1024 return NULL;
1025
1026}
1027
Chris Mason593060d2008-03-25 16:50:33 -04001028static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1029 u64 total_bytes, u64 bytes_used,
1030 struct btrfs_space_info **space_info)
1031{
1032 struct btrfs_space_info *found;
1033
1034 found = __find_space_info(info, flags);
1035 if (found) {
1036 found->total_bytes += total_bytes;
1037 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001038 found->full = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001039 WARN_ON(found->total_bytes < found->bytes_used);
1040 *space_info = found;
1041 return 0;
1042 }
1043 found = kmalloc(sizeof(*found), GFP_NOFS);
1044 if (!found)
1045 return -ENOMEM;
1046
1047 list_add(&found->list, &info->space_info);
1048 found->flags = flags;
1049 found->total_bytes = total_bytes;
1050 found->bytes_used = bytes_used;
1051 found->bytes_pinned = 0;
1052 found->full = 0;
1053 *space_info = found;
1054 return 0;
1055}
1056
Chris Mason8790d502008-04-03 16:29:03 -04001057static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1058{
1059 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001060 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001061 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001062 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001063 if (extra_flags) {
1064 if (flags & BTRFS_BLOCK_GROUP_DATA)
1065 fs_info->avail_data_alloc_bits |= extra_flags;
1066 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1067 fs_info->avail_metadata_alloc_bits |= extra_flags;
1068 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1069 fs_info->avail_system_alloc_bits |= extra_flags;
1070 }
1071}
Chris Mason593060d2008-03-25 16:50:33 -04001072
Chris Masona061fc82008-05-07 11:43:44 -04001073static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001074{
Chris Masona061fc82008-05-07 11:43:44 -04001075 u64 num_devices = root->fs_info->fs_devices->num_devices;
1076
1077 if (num_devices == 1)
1078 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1079 if (num_devices < 4)
1080 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1081
Chris Masonec44a352008-04-28 15:29:52 -04001082 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1083 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001084 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001085 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001086 }
Chris Masonec44a352008-04-28 15:29:52 -04001087
1088 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001089 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001090 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001091 }
Chris Masonec44a352008-04-28 15:29:52 -04001092
1093 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1094 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1095 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1096 (flags & BTRFS_BLOCK_GROUP_DUP)))
1097 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1098 return flags;
1099}
1100
Chris Mason6324fbf2008-03-24 15:01:59 -04001101static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1102 struct btrfs_root *extent_root, u64 alloc_bytes,
1103 u64 flags)
1104{
1105 struct btrfs_space_info *space_info;
1106 u64 thresh;
1107 u64 start;
1108 u64 num_bytes;
1109 int ret;
1110
Chris Masona061fc82008-05-07 11:43:44 -04001111 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001112
Chris Mason6324fbf2008-03-24 15:01:59 -04001113 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001114 if (!space_info) {
1115 ret = update_space_info(extent_root->fs_info, flags,
1116 0, 0, &space_info);
1117 BUG_ON(ret);
1118 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001119 BUG_ON(!space_info);
1120
1121 if (space_info->full)
1122 return 0;
1123
Chris Mason8790d502008-04-03 16:29:03 -04001124 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason6324fbf2008-03-24 15:01:59 -04001125 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1126 thresh)
1127 return 0;
1128
1129 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1130 if (ret == -ENOSPC) {
1131printk("space info full %Lu\n", flags);
1132 space_info->full = 1;
1133 return 0;
1134 }
1135
1136 BUG_ON(ret);
1137
1138 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001139 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001140 BUG_ON(ret);
Chris Mason593060d2008-03-25 16:50:33 -04001141
Chris Mason6324fbf2008-03-24 15:01:59 -04001142 return 0;
1143}
1144
Chris Mason9078a3e2007-04-26 16:46:15 -04001145static int update_block_group(struct btrfs_trans_handle *trans,
1146 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001147 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001148 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001149{
1150 struct btrfs_block_group_cache *cache;
1151 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001152 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001153 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001154 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -04001155 u64 start;
1156 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -04001157
Chris Mason9078a3e2007-04-26 16:46:15 -04001158 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001159 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001160 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001161 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001162 }
Chris Masondb945352007-10-15 16:15:53 -04001163 byte_in_group = bytenr - cache->key.objectid;
1164 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -04001165 start = cache->key.objectid;
1166 end = start + cache->key.offset - 1;
1167 set_extent_bits(&info->block_group_cache, start, end,
1168 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001169
1170 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001171 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001172 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001173 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001174 cache->space_info->bytes_used += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -04001175 } else {
Chris Masondb945352007-10-15 16:15:53 -04001176 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001177 cache->space_info->bytes_used -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -04001178 if (mark_free) {
1179 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -04001180 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -04001181 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001182 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001183 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001184 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -04001185 total -= num_bytes;
1186 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001187 }
1188 return 0;
1189}
Chris Mason6324fbf2008-03-24 15:01:59 -04001190
Chris Masona061fc82008-05-07 11:43:44 -04001191static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1192{
1193 u64 start;
1194 u64 end;
1195 int ret;
1196 ret = find_first_extent_bit(&root->fs_info->block_group_cache,
1197 search_start, &start, &end,
1198 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
1199 BLOCK_GROUP_SYSTEM);
1200 if (ret)
1201 return 0;
1202 return start;
1203}
1204
1205
Yan324ae4d2007-11-16 14:57:08 -05001206static int update_pinned_extents(struct btrfs_root *root,
1207 u64 bytenr, u64 num, int pin)
1208{
1209 u64 len;
1210 struct btrfs_block_group_cache *cache;
1211 struct btrfs_fs_info *fs_info = root->fs_info;
1212
1213 if (pin) {
1214 set_extent_dirty(&fs_info->pinned_extents,
1215 bytenr, bytenr + num - 1, GFP_NOFS);
1216 } else {
1217 clear_extent_dirty(&fs_info->pinned_extents,
1218 bytenr, bytenr + num - 1, GFP_NOFS);
1219 }
1220 while (num > 0) {
1221 cache = btrfs_lookup_block_group(fs_info, bytenr);
Chris Masona061fc82008-05-07 11:43:44 -04001222 if (!cache) {
1223 u64 first = first_logical_byte(root, bytenr);
1224 WARN_ON(first < bytenr);
1225 len = min(first - bytenr, num);
1226 } else {
1227 len = min(num, cache->key.offset -
1228 (bytenr - cache->key.objectid));
1229 }
Yan324ae4d2007-11-16 14:57:08 -05001230 if (pin) {
Chris Masona061fc82008-05-07 11:43:44 -04001231 if (cache) {
1232 cache->pinned += len;
1233 cache->space_info->bytes_pinned += len;
1234 }
Yan324ae4d2007-11-16 14:57:08 -05001235 fs_info->total_pinned += len;
1236 } else {
Chris Masona061fc82008-05-07 11:43:44 -04001237 if (cache) {
1238 cache->pinned -= len;
1239 cache->space_info->bytes_pinned -= len;
1240 }
Yan324ae4d2007-11-16 14:57:08 -05001241 fs_info->total_pinned -= len;
1242 }
1243 bytenr += len;
1244 num -= len;
1245 }
1246 return 0;
1247}
Chris Mason9078a3e2007-04-26 16:46:15 -04001248
Chris Masond1310b22008-01-24 16:13:08 -05001249int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001250{
Chris Masonccd467d2007-06-28 15:57:36 -04001251 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001252 u64 start;
1253 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001254 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001255 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001256
1257 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001258 ret = find_first_extent_bit(pinned_extents, last,
1259 &start, &end, EXTENT_DIRTY);
1260 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001261 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001262 set_extent_dirty(copy, start, end, GFP_NOFS);
1263 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001264 }
1265 return 0;
1266}
1267
1268int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1269 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001270 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001271{
Chris Mason1a5bc162007-10-15 16:15:26 -04001272 u64 start;
1273 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001274 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001275 struct extent_io_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -04001276 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -05001277
1278 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001279 ret = find_first_extent_bit(unpin, 0, &start, &end,
1280 EXTENT_DIRTY);
1281 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001282 break;
Yan324ae4d2007-11-16 14:57:08 -05001283 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001284 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1285 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -05001286 }
1287 return 0;
1288}
1289
Chris Mason98ed5172008-01-03 10:01:48 -05001290static int finish_current_insert(struct btrfs_trans_handle *trans,
1291 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001292{
Chris Mason7bb86312007-12-11 09:25:06 -05001293 u64 start;
1294 u64 end;
1295 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001296 struct extent_buffer *eb;
Chris Mason7bb86312007-12-11 09:25:06 -05001297 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001298 struct btrfs_key ins;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001299 struct btrfs_disk_key first;
Chris Mason234b63a2007-03-13 10:46:10 -04001300 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001301 int ret;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001302 int level;
Chris Mason1a5bc162007-10-15 16:15:26 -04001303 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001304
Chris Mason5f39d392007-10-15 16:14:19 -04001305 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -04001306 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason7bb86312007-12-11 09:25:06 -05001307 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001308
Chris Mason26b80032007-08-08 20:17:12 -04001309 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001310 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1311 &end, EXTENT_LOCKED);
1312 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001313 break;
1314
Chris Mason1a5bc162007-10-15 16:15:26 -04001315 ins.objectid = start;
1316 ins.offset = end + 1 - start;
1317 err = btrfs_insert_item(trans, extent_root, &ins,
1318 &extent_item, sizeof(extent_item));
1319 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1320 GFP_NOFS);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001321 eb = read_tree_block(extent_root, ins.objectid, ins.offset);
1322 level = btrfs_header_level(eb);
1323 if (level == 0) {
1324 btrfs_item_key(eb, &first, 0);
1325 } else {
1326 btrfs_node_key(eb, &first, 0);
1327 }
Chris Mason7bb86312007-12-11 09:25:06 -05001328 err = btrfs_insert_extent_backref(trans, extent_root, path,
1329 start, extent_root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -05001330 0, level,
1331 btrfs_disk_key_objectid(&first));
Chris Mason7bb86312007-12-11 09:25:06 -05001332 BUG_ON(err);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001333 free_extent_buffer(eb);
Chris Mason037e6392007-03-07 11:50:24 -05001334 }
Chris Mason7bb86312007-12-11 09:25:06 -05001335 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001336 return 0;
1337}
1338
Chris Masondb945352007-10-15 16:15:53 -04001339static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1340 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -04001341{
Chris Mason1a5bc162007-10-15 16:15:26 -04001342 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001343 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001344
Chris Masonf4b9aa82007-03-27 11:05:53 -04001345 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -04001346 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -04001347 if (buf) {
1348 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001349 u64 transid =
1350 root->fs_info->running_transaction->transid;
Chris Masondc17ff82008-01-08 15:46:30 -05001351 u64 header_transid =
1352 btrfs_header_generation(buf);
Chris Mason6bc34672008-04-04 15:40:00 -04001353 if (header_transid == transid &&
1354 !btrfs_header_flag(buf,
1355 BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason55c69072008-01-09 15:55:33 -05001356 clean_tree_block(NULL, root, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001357 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -05001358 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001359 }
Chris Masonf4b9aa82007-03-27 11:05:53 -04001360 }
Chris Mason5f39d392007-10-15 16:14:19 -04001361 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -04001362 }
Yan324ae4d2007-11-16 14:57:08 -05001363 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001364 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -04001365 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -04001366 bytenr, bytenr + num_bytes - 1,
1367 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001368 }
Chris Masonbe744172007-05-06 10:15:01 -04001369 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001370 return 0;
1371}
1372
Chris Masona28ec192007-03-06 20:08:01 -05001373/*
1374 * remove an extent from the root, returns 0 on success
1375 */
Chris Masone089f052007-03-16 16:20:31 -04001376static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001377 *root, u64 bytenr, u64 num_bytes,
1378 u64 root_objectid, u64 ref_generation,
1379 u64 owner_objectid, u64 owner_offset, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -04001380 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001381{
Chris Mason5caf2a02007-04-02 11:20:42 -04001382 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001383 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001384 struct btrfs_fs_info *info = root->fs_info;
1385 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001386 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001387 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001388 int extent_slot = 0;
1389 int found_extent = 0;
1390 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001391 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001392 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001393
Chris Masondb945352007-10-15 16:15:53 -04001394 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001395 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001396 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001397 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001398 if (!path)
1399 return -ENOMEM;
1400
Chris Mason3c12ac72008-04-21 12:01:38 -04001401 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05001402 ret = lookup_extent_backref(trans, extent_root, path,
1403 bytenr, root_objectid,
1404 ref_generation,
1405 owner_objectid, owner_offset, 1);
1406 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001407 struct btrfs_key found_key;
1408 extent_slot = path->slots[0];
1409 while(extent_slot > 0) {
1410 extent_slot--;
1411 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1412 extent_slot);
1413 if (found_key.objectid != bytenr)
1414 break;
1415 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1416 found_key.offset == num_bytes) {
1417 found_extent = 1;
1418 break;
1419 }
1420 if (path->slots[0] - extent_slot > 5)
1421 break;
1422 }
1423 if (!found_extent)
1424 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001425 } else {
1426 btrfs_print_leaf(extent_root, path->nodes[0]);
1427 WARN_ON(1);
1428 printk("Unable to find ref byte nr %Lu root %Lu "
1429 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1430 root_objectid, ref_generation, owner_objectid,
1431 owner_offset);
1432 }
Chris Mason952fcca2008-02-18 16:33:44 -05001433 if (!found_extent) {
1434 btrfs_release_path(extent_root, path);
1435 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1436 if (ret < 0)
1437 return ret;
1438 BUG_ON(ret);
1439 extent_slot = path->slots[0];
1440 }
Chris Mason5f39d392007-10-15 16:14:19 -04001441
1442 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001443 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001444 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001445 refs = btrfs_extent_refs(leaf, ei);
1446 BUG_ON(refs == 0);
1447 refs -= 1;
1448 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001449
Chris Mason5f39d392007-10-15 16:14:19 -04001450 btrfs_mark_buffer_dirty(leaf);
1451
Chris Mason952fcca2008-02-18 16:33:44 -05001452 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1453 /* if the back ref and the extent are next to each other
1454 * they get deleted below in one shot
1455 */
1456 path->slots[0] = extent_slot;
1457 num_to_del = 2;
1458 } else if (found_extent) {
1459 /* otherwise delete the extent back ref */
1460 ret = btrfs_del_item(trans, extent_root, path);
1461 BUG_ON(ret);
1462 /* if refs are 0, we need to setup the path for deletion */
1463 if (refs == 0) {
1464 btrfs_release_path(extent_root, path);
1465 ret = btrfs_search_slot(trans, extent_root, &key, path,
1466 -1, 1);
1467 if (ret < 0)
1468 return ret;
1469 BUG_ON(ret);
1470 }
1471 }
1472
Chris Masoncf27e1e2007-03-13 09:49:06 -04001473 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001474 u64 super_used;
1475 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04001476
1477 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -04001478 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -05001479 if (ret > 0)
1480 mark_free = 1;
1481 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001482 }
1483
Josef Bacik58176a92007-08-29 15:47:34 -04001484 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001485 super_used = btrfs_super_bytes_used(&info->super_copy);
1486 btrfs_set_super_bytes_used(&info->super_copy,
1487 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001488
1489 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001490 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001491 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001492 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001493 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1494 num_to_del);
Chris Mason54aa1f42007-06-22 14:16:25 -04001495 if (ret) {
1496 return ret;
1497 }
Chris Masondb945352007-10-15 16:15:53 -04001498 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001499 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001500 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05001501 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001502 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001503 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05001504 return ret;
1505}
1506
1507/*
Chris Masonfec577f2007-02-26 10:40:21 -05001508 * find all the blocks marked as pending in the radix tree and remove
1509 * them from the extent map
1510 */
Chris Masone089f052007-03-16 16:20:31 -04001511static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1512 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05001513{
1514 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001515 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001516 u64 start;
1517 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001518 struct extent_io_tree *pending_del;
1519 struct extent_io_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -04001520
Chris Mason1a5bc162007-10-15 16:15:26 -04001521 pending_del = &extent_root->fs_info->pending_del;
1522 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -05001523
1524 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001525 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1526 EXTENT_LOCKED);
1527 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05001528 break;
Yan324ae4d2007-11-16 14:57:08 -05001529 update_pinned_extents(extent_root, start, end + 1 - start, 1);
Chris Mason1a5bc162007-10-15 16:15:26 -04001530 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1531 GFP_NOFS);
1532 ret = __free_extent(trans, extent_root,
Chris Mason7bb86312007-12-11 09:25:06 -05001533 start, end + 1 - start,
1534 extent_root->root_key.objectid,
1535 0, 0, 0, 0, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001536 if (ret)
1537 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001538 }
Chris Masone20d96d2007-03-22 12:13:20 -04001539 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05001540}
1541
1542/*
1543 * remove an extent from the root, returns 0 on success
1544 */
Chris Masone089f052007-03-16 16:20:31 -04001545int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001546 *root, u64 bytenr, u64 num_bytes,
1547 u64 root_objectid, u64 ref_generation,
1548 u64 owner_objectid, u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05001549{
Chris Mason9f5fae22007-03-20 14:38:32 -04001550 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05001551 int pending_ret;
1552 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05001553
Chris Masondb945352007-10-15 16:15:53 -04001554 WARN_ON(num_bytes < root->sectorsize);
Chris Mason7bb86312007-12-11 09:25:06 -05001555 if (!root->ref_cows)
1556 ref_generation = 0;
1557
Chris Masona28ec192007-03-06 20:08:01 -05001558 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -04001559 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -05001560 return 0;
1561 }
Chris Mason7bb86312007-12-11 09:25:06 -05001562 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1563 ref_generation, owner_objectid, owner_offset,
1564 pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001565 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05001566 return ret ? ret : pending_ret;
1567}
1568
Chris Mason87ee04e2007-11-30 11:30:34 -05001569static u64 stripe_align(struct btrfs_root *root, u64 val)
1570{
1571 u64 mask = ((u64)root->stripesize - 1);
1572 u64 ret = (val + mask) & ~mask;
1573 return ret;
1574}
1575
Chris Masonfec577f2007-02-26 10:40:21 -05001576/*
1577 * walks the btree of allocated extents and find a hole of a given size.
1578 * The key ins is changed to record the hole:
1579 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04001580 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001581 * ins->offset == number of blocks
1582 * Any available blocks before search_start are skipped.
1583 */
Chris Mason98ed5172008-01-03 10:01:48 -05001584static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1585 struct btrfs_root *orig_root,
1586 u64 num_bytes, u64 empty_size,
1587 u64 search_start, u64 search_end,
1588 u64 hint_byte, struct btrfs_key *ins,
1589 u64 exclude_start, u64 exclude_nr,
1590 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001591{
Chris Mason87ee04e2007-11-30 11:30:34 -05001592 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04001593 u64 orig_search_start;
Chris Mason9f5fae22007-03-20 14:38:32 -04001594 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001595 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001596 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001597 u64 *last_ptr = NULL;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001598 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001599 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001600 int wrapped = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001601 int empty_cluster = 2 * 1024 * 1024;
Chris Masonfec577f2007-02-26 10:40:21 -05001602
Chris Masondb945352007-10-15 16:15:53 -04001603 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001604 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1605
Chris Mason239b14b2008-03-24 15:02:07 -04001606 if (data & BTRFS_BLOCK_GROUP_METADATA) {
1607 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04001608 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04001609 }
1610
1611 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
1612 last_ptr = &root->fs_info->last_data_alloc;
1613 }
1614
1615 if (last_ptr) {
1616 if (*last_ptr)
1617 hint_byte = *last_ptr;
1618 else {
1619 empty_size += empty_cluster;
1620 }
1621 }
1622
Chris Masona061fc82008-05-07 11:43:44 -04001623 search_start = max(search_start, first_logical_byte(root, 0));
1624 orig_search_start = search_start;
1625
Chris Mason7f93bf82008-03-24 15:01:28 -04001626 if (search_end == (u64)-1)
1627 search_end = btrfs_super_total_bytes(&info->super_copy);
Chris Mason0b86a832008-03-24 15:01:56 -04001628
Chris Masondb945352007-10-15 16:15:53 -04001629 if (hint_byte) {
1630 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001631 if (!block_group)
1632 hint_byte = search_start;
Chris Masonbe744172007-05-06 10:15:01 -04001633 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -04001634 hint_byte, data, 1);
Chris Mason239b14b2008-03-24 15:02:07 -04001635 if (last_ptr && *last_ptr == 0 && block_group)
1636 hint_byte = block_group->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001637 } else {
1638 block_group = btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001639 trans->block_group,
1640 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001641 }
Chris Mason239b14b2008-03-24 15:02:07 -04001642 search_start = max(search_start, hint_byte);
Chris Masonbe744172007-05-06 10:15:01 -04001643
Chris Mason6702ed42007-08-07 16:15:09 -04001644 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001645
Chris Masonbe744172007-05-06 10:15:01 -04001646check_failed:
Chris Mason70b043f2007-12-13 09:02:46 -05001647 if (!block_group) {
1648 block_group = btrfs_lookup_block_group(info, search_start);
1649 if (!block_group)
1650 block_group = btrfs_lookup_block_group(info,
1651 orig_search_start);
1652 }
Chris Mason0b86a832008-03-24 15:01:56 -04001653 ret = find_search_start(root, &block_group, &search_start,
1654 total_needed, data);
Chris Mason239b14b2008-03-24 15:02:07 -04001655 if (ret == -ENOSPC && last_ptr && *last_ptr) {
1656 *last_ptr = 0;
1657 block_group = btrfs_lookup_block_group(info,
1658 orig_search_start);
1659 search_start = orig_search_start;
1660 ret = find_search_start(root, &block_group, &search_start,
1661 total_needed, data);
1662 }
1663 if (ret == -ENOSPC)
1664 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04001665 if (ret)
1666 goto error;
1667
Chris Mason239b14b2008-03-24 15:02:07 -04001668 if (last_ptr && *last_ptr && search_start != *last_ptr) {
1669 *last_ptr = 0;
1670 if (!empty_size) {
1671 empty_size += empty_cluster;
1672 total_needed += empty_size;
1673 }
1674 block_group = btrfs_lookup_block_group(info,
1675 orig_search_start);
1676 search_start = orig_search_start;
1677 ret = find_search_start(root, &block_group,
1678 &search_start, total_needed, data);
1679 if (ret == -ENOSPC)
1680 goto enospc;
1681 if (ret)
1682 goto error;
1683 }
1684
Chris Mason87ee04e2007-11-30 11:30:34 -05001685 search_start = stripe_align(root, search_start);
Chris Masonfec577f2007-02-26 10:40:21 -05001686 ins->objectid = search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001687 ins->offset = num_bytes;
Chris Masone37c9e62007-05-09 20:13:14 -04001688
Chris Masondb945352007-10-15 16:15:53 -04001689 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001690 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04001691
1692 if (ins->objectid + num_bytes >
1693 block_group->key.objectid + block_group->key.offset) {
Chris Masone19caa52007-10-15 16:17:44 -04001694 search_start = block_group->key.objectid +
1695 block_group->key.offset;
1696 goto new_group;
1697 }
Chris Mason0b86a832008-03-24 15:01:56 -04001698
Chris Mason1a5bc162007-10-15 16:15:26 -04001699 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001700 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1701 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001702 goto new_group;
1703 }
Chris Mason0b86a832008-03-24 15:01:56 -04001704
Chris Mason1a5bc162007-10-15 16:15:26 -04001705 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001706 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1707 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001708 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001709 }
Chris Mason0b86a832008-03-24 15:01:56 -04001710
Chris Masondb945352007-10-15 16:15:53 -04001711 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001712 ins->objectid < exclude_start + exclude_nr)) {
1713 search_start = exclude_start + exclude_nr;
1714 goto new_group;
1715 }
Chris Mason0b86a832008-03-24 15:01:56 -04001716
Chris Mason6324fbf2008-03-24 15:01:59 -04001717 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
Chris Mason5276aed2007-06-11 21:33:38 -04001718 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001719 if (block_group)
1720 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001721 }
Chris Masondb945352007-10-15 16:15:53 -04001722 ins->offset = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001723 if (last_ptr) {
1724 *last_ptr = ins->objectid + ins->offset;
1725 if (*last_ptr ==
1726 btrfs_super_total_bytes(&root->fs_info->super_copy)) {
1727 *last_ptr = 0;
1728 }
1729 }
Chris Masonfec577f2007-02-26 10:40:21 -05001730 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001731
1732new_group:
Chris Masondb945352007-10-15 16:15:53 -04001733 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001734enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001735 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001736 if (full_scan) {
1737 ret = -ENOSPC;
1738 goto error;
1739 }
Chris Mason6702ed42007-08-07 16:15:09 -04001740 if (wrapped) {
1741 if (!full_scan)
1742 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001743 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001744 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001745 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001746 }
Chris Mason5276aed2007-06-11 21:33:38 -04001747 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001748 cond_resched();
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001749 block_group = btrfs_find_block_group(root, block_group,
1750 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001751 goto check_failed;
1752
Chris Mason0f70abe2007-02-28 16:46:22 -05001753error:
Chris Mason0f70abe2007-02-28 16:46:22 -05001754 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001755}
Chris Masonec44a352008-04-28 15:29:52 -04001756
Chris Masonfec577f2007-02-26 10:40:21 -05001757/*
Chris Masonfec577f2007-02-26 10:40:21 -05001758 * finds a free extent and does all the dirty work required for allocation
1759 * returns the key for the extent through ins, and a tree buffer for
1760 * the first block of the extent through buf.
1761 *
1762 * returns 0 if everything worked, non-zero otherwise.
1763 */
Chris Mason4d775672007-04-20 20:23:12 -04001764int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -05001765 struct btrfs_root *root,
Chris Mason98d20f62008-04-14 09:46:10 -04001766 u64 num_bytes, u64 min_alloc_size,
1767 u64 root_objectid, u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05001768 u64 owner, u64 owner_offset,
1769 u64 empty_size, u64 hint_byte,
Chris Masonec44a352008-04-28 15:29:52 -04001770 u64 search_end, struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05001771{
1772 int ret;
1773 int pending_ret;
Chris Masonc31f8832008-01-08 15:46:31 -05001774 u64 super_used;
1775 u64 root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001776 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04001777 u64 alloc_profile;
Chris Mason47e4bb92008-02-01 14:51:59 -05001778 u32 sizes[2];
Chris Mason1261ec42007-03-20 20:35:03 -04001779 struct btrfs_fs_info *info = root->fs_info;
1780 struct btrfs_root *extent_root = info->extent_root;
Chris Mason47e4bb92008-02-01 14:51:59 -05001781 struct btrfs_extent_item *extent_item;
1782 struct btrfs_extent_ref *ref;
Chris Mason7bb86312007-12-11 09:25:06 -05001783 struct btrfs_path *path;
Chris Mason47e4bb92008-02-01 14:51:59 -05001784 struct btrfs_key keys[2];
Chris Mason8f662a72008-01-02 10:01:11 -05001785
Chris Mason6324fbf2008-03-24 15:01:59 -04001786 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04001787 alloc_profile = info->avail_data_alloc_bits &
1788 info->data_alloc_profile;
1789 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001790 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04001791 alloc_profile = info->avail_system_alloc_bits &
1792 info->system_alloc_profile;
1793 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001794 } else {
Chris Mason8790d502008-04-03 16:29:03 -04001795 alloc_profile = info->avail_metadata_alloc_bits &
1796 info->metadata_alloc_profile;
1797 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001798 }
Chris Mason98d20f62008-04-14 09:46:10 -04001799again:
Chris Masona061fc82008-05-07 11:43:44 -04001800 data = reduce_alloc_profile(root, data);
Chris Mason3bf3d9e2008-04-26 11:03:32 -04001801 if (root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04001802 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001803 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason239b14b2008-03-24 15:02:07 -04001804 2 * 1024 * 1024,
Chris Mason593060d2008-03-25 16:50:33 -04001805 BTRFS_BLOCK_GROUP_METADATA |
Chris Mason8790d502008-04-03 16:29:03 -04001806 (info->metadata_alloc_profile &
1807 info->avail_metadata_alloc_bits));
Chris Mason6324fbf2008-03-24 15:01:59 -04001808 BUG_ON(ret);
1809 }
1810 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason8790d502008-04-03 16:29:03 -04001811 num_bytes + 2 * 1024 * 1024, data);
Chris Mason6324fbf2008-03-24 15:01:59 -04001812 BUG_ON(ret);
1813 }
Chris Mason0b86a832008-03-24 15:01:56 -04001814
Chris Masondb945352007-10-15 16:15:53 -04001815 WARN_ON(num_bytes < root->sectorsize);
1816 ret = find_free_extent(trans, root, num_bytes, empty_size,
1817 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001818 trans->alloc_exclude_start,
1819 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04001820
Chris Mason98d20f62008-04-14 09:46:10 -04001821 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
1822 num_bytes = num_bytes >> 1;
1823 num_bytes = max(num_bytes, min_alloc_size);
1824 goto again;
1825 }
Chris Masonec44a352008-04-28 15:29:52 -04001826 if (ret) {
1827 printk("allocation failed flags %Lu\n", data);
1828 }
Chris Masonccd467d2007-06-28 15:57:36 -04001829 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001830 if (ret)
1831 return ret;
1832
Josef Bacik58176a92007-08-29 15:47:34 -04001833 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001834 super_used = btrfs_super_bytes_used(&info->super_copy);
1835 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001836
Josef Bacik58176a92007-08-29 15:47:34 -04001837 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001838 root_used = btrfs_root_used(&root->root_item);
1839 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001840
Chris Masonf510cfe2007-10-15 16:14:48 -04001841 clear_extent_dirty(&root->fs_info->free_space_cache,
1842 ins->objectid, ins->objectid + ins->offset - 1,
1843 GFP_NOFS);
1844
Chris Mason26b80032007-08-08 20:17:12 -04001845 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001846 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1847 ins->objectid + ins->offset - 1,
1848 EXTENT_LOCKED, GFP_NOFS);
Chris Mason26b80032007-08-08 20:17:12 -04001849 goto update_block;
1850 }
1851
1852 WARN_ON(trans->alloc_exclude_nr);
1853 trans->alloc_exclude_start = ins->objectid;
1854 trans->alloc_exclude_nr = ins->offset;
Chris Mason037e6392007-03-07 11:50:24 -05001855
Chris Mason47e4bb92008-02-01 14:51:59 -05001856 memcpy(&keys[0], ins, sizeof(*ins));
1857 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
1858 owner, owner_offset);
1859 keys[1].objectid = ins->objectid;
1860 keys[1].type = BTRFS_EXTENT_REF_KEY;
1861 sizes[0] = sizeof(*extent_item);
1862 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05001863
1864 path = btrfs_alloc_path();
1865 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05001866
1867 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
1868 sizes, 2);
Chris Mason26b80032007-08-08 20:17:12 -04001869
Chris Masonccd467d2007-06-28 15:57:36 -04001870 BUG_ON(ret);
Chris Mason47e4bb92008-02-01 14:51:59 -05001871 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1872 struct btrfs_extent_item);
1873 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
1874 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1875 struct btrfs_extent_ref);
1876
1877 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
1878 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
1879 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
1880 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
1881
1882 btrfs_mark_buffer_dirty(path->nodes[0]);
1883
1884 trans->alloc_exclude_start = 0;
1885 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001886 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001887 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001888 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001889
Chris Masone37c9e62007-05-09 20:13:14 -04001890 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001891 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001892 }
1893 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001894 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001895 }
Chris Mason26b80032007-08-08 20:17:12 -04001896
1897update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04001898 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05001899 if (ret) {
1900 printk("update block group failed for %Lu %Lu\n",
1901 ins->objectid, ins->offset);
1902 BUG();
1903 }
Chris Mason037e6392007-03-07 11:50:24 -05001904 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001905}
1906
1907/*
1908 * helper function to allocate a block for a given tree
1909 * returns the tree buffer or NULL.
1910 */
Chris Mason5f39d392007-10-15 16:14:19 -04001911struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001912 struct btrfs_root *root,
Chris Mason7bb86312007-12-11 09:25:06 -05001913 u32 blocksize,
1914 u64 root_objectid, u64 hint,
1915 u64 empty_size)
1916{
1917 u64 ref_generation;
1918
1919 if (root->ref_cows)
1920 ref_generation = trans->transid;
1921 else
1922 ref_generation = 0;
1923
1924
1925 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1926 ref_generation, 0, 0, hint, empty_size);
1927}
1928
1929/*
1930 * helper function to allocate a block for a given tree
1931 * returns the tree buffer or NULL.
1932 */
1933struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1934 struct btrfs_root *root,
1935 u32 blocksize,
1936 u64 root_objectid,
1937 u64 ref_generation,
1938 u64 first_objectid,
1939 int level,
1940 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001941 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001942{
Chris Masone2fa7222007-03-12 16:22:34 -04001943 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001944 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001945 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001946
Chris Mason98d20f62008-04-14 09:46:10 -04001947 ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
Chris Mason7bb86312007-12-11 09:25:06 -05001948 root_objectid, ref_generation,
Chris Masonf6dbff52007-12-13 11:13:32 -05001949 level, first_objectid, empty_size, hint,
Chris Masondb945352007-10-15 16:15:53 -04001950 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001951 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001952 BUG_ON(ret > 0);
1953 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001954 }
Chris Masondb945352007-10-15 16:15:53 -04001955 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001956 if (!buf) {
Chris Mason7bb86312007-12-11 09:25:06 -05001957 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1958 root->root_key.objectid, ref_generation,
1959 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001960 return ERR_PTR(-ENOMEM);
1961 }
Chris Mason55c69072008-01-09 15:55:33 -05001962 btrfs_set_header_generation(buf, trans->transid);
1963 clean_tree_block(trans, root, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001964 btrfs_set_buffer_uptodate(buf);
Chris Mason55c69072008-01-09 15:55:33 -05001965
1966 if (PageDirty(buf->first_page)) {
1967 printk("page %lu dirty\n", buf->first_page->index);
1968 WARN_ON(1);
1969 }
1970
Chris Mason5f39d392007-10-15 16:14:19 -04001971 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1972 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason9afbb0b2008-02-15 13:19:35 -05001973 if (!btrfs_test_opt(root, SSD))
1974 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001975 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001976 return buf;
1977}
Chris Masona28ec192007-03-06 20:08:01 -05001978
Chris Mason98ed5172008-01-03 10:01:48 -05001979static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
1980 struct btrfs_root *root,
1981 struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001982{
Chris Mason7bb86312007-12-11 09:25:06 -05001983 u64 leaf_owner;
1984 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04001985 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001986 struct btrfs_file_extent_item *fi;
1987 int i;
1988 int nritems;
1989 int ret;
1990
Chris Mason5f39d392007-10-15 16:14:19 -04001991 BUG_ON(!btrfs_is_leaf(leaf));
1992 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05001993 leaf_owner = btrfs_header_owner(leaf);
1994 leaf_generation = btrfs_header_generation(leaf);
1995
Chris Mason6407bf62007-03-27 06:33:00 -04001996 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001997 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001998
1999 btrfs_item_key_to_cpu(leaf, &key, i);
2000 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04002001 continue;
2002 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002003 if (btrfs_file_extent_type(leaf, fi) ==
2004 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04002005 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04002006 /*
2007 * FIXME make sure to insert a trans record that
2008 * repeats the snapshot del on crash
2009 */
Chris Masondb945352007-10-15 16:15:53 -04002010 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2011 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04002012 continue;
Chris Masondb945352007-10-15 16:15:53 -04002013 ret = btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002014 btrfs_file_extent_disk_num_bytes(leaf, fi),
2015 leaf_owner, leaf_generation,
2016 key.objectid, key.offset, 0);
Chris Mason6407bf62007-03-27 06:33:00 -04002017 BUG_ON(ret);
2018 }
2019 return 0;
2020}
2021
Chris Mason98ed5172008-01-03 10:01:48 -05002022static void noinline reada_walk_down(struct btrfs_root *root,
Chris Masonbea495e2008-01-24 16:13:14 -05002023 struct extent_buffer *node,
2024 int slot)
Chris Masone0115992007-06-19 16:23:05 -04002025{
Chris Masondb945352007-10-15 16:15:53 -04002026 u64 bytenr;
Chris Masonbea495e2008-01-24 16:13:14 -05002027 u64 last = 0;
2028 u32 nritems;
Chris Masone0115992007-06-19 16:23:05 -04002029 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04002030 u32 blocksize;
Chris Masonbea495e2008-01-24 16:13:14 -05002031 int ret;
2032 int i;
2033 int level;
2034 int skipped = 0;
Chris Masone0115992007-06-19 16:23:05 -04002035
Chris Mason5f39d392007-10-15 16:14:19 -04002036 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04002037 level = btrfs_header_level(node);
Chris Masonbea495e2008-01-24 16:13:14 -05002038 if (level)
2039 return;
2040
2041 for (i = slot; i < nritems && skipped < 32; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002042 bytenr = btrfs_node_blockptr(node, i);
Chris Masonbea495e2008-01-24 16:13:14 -05002043 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2044 (last > bytenr && last - bytenr > 32 * 1024))) {
2045 skipped++;
Chris Masone0115992007-06-19 16:23:05 -04002046 continue;
Chris Masonbea495e2008-01-24 16:13:14 -05002047 }
2048 blocksize = btrfs_level_size(root, level - 1);
2049 if (i != slot) {
2050 ret = lookup_extent_ref(NULL, root, bytenr,
2051 blocksize, &refs);
2052 BUG_ON(ret);
2053 if (refs != 1) {
2054 skipped++;
2055 continue;
2056 }
2057 }
Chris Mason409eb952007-08-08 20:17:12 -04002058 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04002059 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Masonbea495e2008-01-24 16:13:14 -05002060 last = bytenr + blocksize;
Chris Mason409eb952007-08-08 20:17:12 -04002061 cond_resched();
2062 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04002063 if (ret)
2064 break;
2065 }
2066}
2067
Chris Mason9aca1d52007-03-13 11:09:37 -04002068/*
2069 * helper function for drop_snapshot, this walks down the tree dropping ref
2070 * counts as it goes.
2071 */
Chris Mason98ed5172008-01-03 10:01:48 -05002072static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2073 struct btrfs_root *root,
2074 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002075{
Chris Mason7bb86312007-12-11 09:25:06 -05002076 u64 root_owner;
2077 u64 root_gen;
2078 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002079 struct extent_buffer *next;
2080 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002081 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04002082 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002083 int ret;
2084 u32 refs;
2085
Chris Mason5caf2a02007-04-02 11:20:42 -04002086 WARN_ON(*level < 0);
2087 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04002088 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002089 path->nodes[*level]->start,
2090 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002091 BUG_ON(ret);
2092 if (refs > 1)
2093 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002094
Chris Mason9aca1d52007-03-13 11:09:37 -04002095 /*
2096 * walk down to the last node level and free all the leaves
2097 */
Chris Mason6407bf62007-03-27 06:33:00 -04002098 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002099 WARN_ON(*level < 0);
2100 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002101 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002102
Chris Mason5f39d392007-10-15 16:14:19 -04002103 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002104 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002105
Chris Mason7518a232007-03-12 12:01:18 -04002106 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002107 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002108 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002109 if (*level == 0) {
2110 ret = drop_leaf_ref(trans, root, cur);
2111 BUG_ON(ret);
2112 break;
2113 }
Chris Masondb945352007-10-15 16:15:53 -04002114 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2115 blocksize = btrfs_level_size(root, *level - 1);
2116 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002117 BUG_ON(ret);
2118 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002119 parent = path->nodes[*level];
2120 root_owner = btrfs_header_owner(parent);
2121 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002122 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04002123 ret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002124 blocksize, root_owner,
2125 root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002126 BUG_ON(ret);
2127 continue;
2128 }
Chris Masondb945352007-10-15 16:15:53 -04002129 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002130 if (!next || !btrfs_buffer_uptodate(next)) {
2131 free_extent_buffer(next);
Chris Masonbea495e2008-01-24 16:13:14 -05002132 reada_walk_down(root, cur, path->slots[*level]);
Chris Masone9d0b132007-08-10 14:06:19 -04002133
Chris Mason8790d502008-04-03 16:29:03 -04002134 mutex_unlock(&root->fs_info->fs_mutex);
2135 next = read_tree_block(root, bytenr, blocksize);
2136 mutex_lock(&root->fs_info->fs_mutex);
2137
2138 /* we've dropped the lock, double check */
Chris Masondb945352007-10-15 16:15:53 -04002139 ret = lookup_extent_ref(trans, root, bytenr,
2140 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002141 BUG_ON(ret);
2142 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002143 parent = path->nodes[*level];
2144 root_owner = btrfs_header_owner(parent);
2145 root_gen = btrfs_header_generation(parent);
2146
Chris Masone9d0b132007-08-10 14:06:19 -04002147 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04002148 free_extent_buffer(next);
Chris Mason7bb86312007-12-11 09:25:06 -05002149 ret = btrfs_free_extent(trans, root, bytenr,
2150 blocksize,
2151 root_owner,
2152 root_gen, 0, 0, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04002153 BUG_ON(ret);
2154 continue;
2155 }
Chris Mason0999df52008-04-01 13:48:14 -04002156 } else if (next) {
2157 btrfs_verify_block_csum(root, next);
Chris Masone9d0b132007-08-10 14:06:19 -04002158 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002159 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002160 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002161 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002162 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002163 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002164 path->slots[*level] = 0;
2165 }
2166out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002167 WARN_ON(*level < 0);
2168 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002169
2170 if (path->nodes[*level] == root->node) {
2171 root_owner = root->root_key.objectid;
2172 parent = path->nodes[*level];
2173 } else {
2174 parent = path->nodes[*level + 1];
2175 root_owner = btrfs_header_owner(parent);
2176 }
2177
2178 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04002179 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002180 path->nodes[*level]->len,
2181 root_owner, root_gen, 0, 0, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002182 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05002183 path->nodes[*level] = NULL;
2184 *level += 1;
2185 BUG_ON(ret);
2186 return 0;
2187}
2188
Chris Mason9aca1d52007-03-13 11:09:37 -04002189/*
2190 * helper for dropping snapshots. This walks back up the tree in the path
2191 * to find the first node higher up where we haven't yet gone through
2192 * all the slots
2193 */
Chris Mason98ed5172008-01-03 10:01:48 -05002194static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2195 struct btrfs_root *root,
2196 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002197{
Chris Mason7bb86312007-12-11 09:25:06 -05002198 u64 root_owner;
2199 u64 root_gen;
2200 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002201 int i;
2202 int slot;
2203 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04002204
Chris Mason234b63a2007-03-13 10:46:10 -04002205 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05002206 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04002207 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2208 struct extent_buffer *node;
2209 struct btrfs_disk_key disk_key;
2210 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05002211 path->slots[i]++;
2212 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04002213 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002214 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04002215 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04002216 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04002217 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05002218 return 0;
2219 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002220 if (path->nodes[*level] == root->node) {
2221 root_owner = root->root_key.objectid;
2222 root_gen =
2223 btrfs_header_generation(path->nodes[*level]);
2224 } else {
2225 struct extent_buffer *node;
2226 node = path->nodes[*level + 1];
2227 root_owner = btrfs_header_owner(node);
2228 root_gen = btrfs_header_generation(node);
2229 }
Chris Masone089f052007-03-16 16:20:31 -04002230 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002231 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002232 path->nodes[*level]->len,
2233 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04002234 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002235 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04002236 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05002237 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05002238 }
2239 }
2240 return 1;
2241}
2242
Chris Mason9aca1d52007-03-13 11:09:37 -04002243/*
2244 * drop the reference count on the tree rooted at 'snap'. This traverses
2245 * the tree freeing any blocks that have a ref count of zero after being
2246 * decremented.
2247 */
Chris Masone089f052007-03-16 16:20:31 -04002248int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04002249 *root)
Chris Mason20524f02007-03-10 06:35:47 -05002250{
Chris Mason3768f362007-03-13 16:47:54 -04002251 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04002252 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05002253 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04002254 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05002255 int i;
2256 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002257 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002258
Chris Mason5caf2a02007-04-02 11:20:42 -04002259 path = btrfs_alloc_path();
2260 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05002261
Chris Mason5f39d392007-10-15 16:14:19 -04002262 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05002263 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002264 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2265 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04002266 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04002267 path->slots[level] = 0;
2268 } else {
2269 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002270 struct btrfs_disk_key found_key;
2271 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04002272
Chris Mason9f3a7422007-08-07 15:52:19 -04002273 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04002274 level = root_item->drop_level;
2275 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002276 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04002277 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04002278 ret = wret;
2279 goto out;
2280 }
Chris Mason5f39d392007-10-15 16:14:19 -04002281 node = path->nodes[level];
2282 btrfs_node_key(node, &found_key, path->slots[level]);
2283 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2284 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04002285 }
Chris Mason20524f02007-03-10 06:35:47 -05002286 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002287 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002288 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002289 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002290 if (wret < 0)
2291 ret = wret;
2292
Chris Mason5caf2a02007-04-02 11:20:42 -04002293 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002294 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002295 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002296 if (wret < 0)
2297 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04002298 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04002299 break;
Chris Mason20524f02007-03-10 06:35:47 -05002300 }
Chris Mason83e15a22007-03-12 09:03:27 -04002301 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002302 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04002303 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04002304 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04002305 }
Chris Mason20524f02007-03-10 06:35:47 -05002306 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002307out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002308 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04002309 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05002310}
Chris Mason9078a3e2007-04-26 16:46:15 -04002311
Chris Masonbe744172007-05-06 10:15:01 -04002312int btrfs_free_block_groups(struct btrfs_fs_info *info)
2313{
Chris Masonf510cfe2007-10-15 16:14:48 -04002314 u64 start;
2315 u64 end;
Yanb97f9202007-11-01 11:28:41 -04002316 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04002317 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04002318 while(1) {
2319 ret = find_first_extent_bit(&info->block_group_cache, 0,
2320 &start, &end, (unsigned int)-1);
2321 if (ret)
2322 break;
Yanb97f9202007-11-01 11:28:41 -04002323 ret = get_state_private(&info->block_group_cache, start, &ptr);
2324 if (!ret)
2325 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04002326 clear_extent_bits(&info->block_group_cache, start,
2327 end, (unsigned int)-1, GFP_NOFS);
2328 }
Chris Masone37c9e62007-05-09 20:13:14 -04002329 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04002330 ret = find_first_extent_bit(&info->free_space_cache, 0,
2331 &start, &end, EXTENT_DIRTY);
2332 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04002333 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04002334 clear_extent_dirty(&info->free_space_cache, start,
2335 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04002336 }
Chris Masonbe744172007-05-06 10:15:01 -04002337 return 0;
2338}
2339
Chris Mason8e7bf942008-04-28 09:02:36 -04002340static unsigned long calc_ra(unsigned long start, unsigned long last,
2341 unsigned long nr)
2342{
2343 return min(last, start + nr - 1);
2344}
2345
Chris Mason98ed5172008-01-03 10:01:48 -05002346static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2347 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05002348{
2349 u64 page_start;
2350 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002351 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05002352 unsigned long i;
2353 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05002354 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05002355 struct file_ra_state *ra;
Chris Mason8e7bf942008-04-28 09:02:36 -04002356 unsigned long total_read = 0;
2357 unsigned long ra_pages;
Chris Masona061fc82008-05-07 11:43:44 -04002358 struct btrfs_trans_handle *trans;
Chris Mason4313b392008-01-03 09:08:48 -05002359
2360 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002361
2362 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05002363 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05002364 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2365
Chris Mason8e7bf942008-04-28 09:02:36 -04002366 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
2367
Chris Mason4313b392008-01-03 09:08:48 -05002368 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05002369
Chris Mason4313b392008-01-03 09:08:48 -05002370 for (; i <= last_index; i++) {
Chris Mason8e7bf942008-04-28 09:02:36 -04002371 if (total_read % ra_pages == 0) {
2372 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
2373 calc_ra(i, last_index, ra_pages));
2374 }
2375 total_read++;
Chris Masona061fc82008-05-07 11:43:44 -04002376 if (((u64)i << PAGE_CACHE_SHIFT) > inode->i_size)
2377 goto truncate_racing;
2378
Chris Masonedbd8d42007-12-21 16:27:24 -05002379 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04002380 if (!page) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002381 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04002382 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002383 if (!PageUptodate(page)) {
2384 btrfs_readpage(NULL, page);
2385 lock_page(page);
2386 if (!PageUptodate(page)) {
2387 unlock_page(page);
2388 page_cache_release(page);
2389 goto out_unlock;
2390 }
2391 }
Chris Masonec44a352008-04-28 15:29:52 -04002392#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2393 ClearPageDirty(page);
2394#else
2395 cancel_dirty_page(page, PAGE_CACHE_SIZE);
2396#endif
2397 wait_on_page_writeback(page);
2398 set_page_extent_mapped(page);
Chris Masonedbd8d42007-12-21 16:27:24 -05002399 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2400 page_end = page_start + PAGE_CACHE_SIZE - 1;
2401
Chris Masond1310b22008-01-24 16:13:08 -05002402 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002403
Chris Masond1310b22008-01-24 16:13:08 -05002404 set_extent_delalloc(io_tree, page_start,
Chris Masonedbd8d42007-12-21 16:27:24 -05002405 page_end, GFP_NOFS);
Chris Masona061fc82008-05-07 11:43:44 -04002406 set_page_dirty(page);
Chris Masonedbd8d42007-12-21 16:27:24 -05002407
Chris Masond1310b22008-01-24 16:13:08 -05002408 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002409 unlock_page(page);
2410 page_cache_release(page);
2411 }
Chris Masona061fc82008-05-07 11:43:44 -04002412 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2413 total_read);
Chris Masonedbd8d42007-12-21 16:27:24 -05002414
2415out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04002416 kfree(ra);
Chris Masona061fc82008-05-07 11:43:44 -04002417 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
2418 if (trans) {
2419 btrfs_add_ordered_inode(inode);
2420 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
2421 mark_inode_dirty(inode);
2422 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002423 mutex_unlock(&inode->i_mutex);
2424 return 0;
Chris Masona061fc82008-05-07 11:43:44 -04002425
2426truncate_racing:
2427 vmtruncate(inode, inode->i_size);
2428 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2429 total_read);
2430 goto out_unlock;
Chris Masonedbd8d42007-12-21 16:27:24 -05002431}
2432
Chris Mason4313b392008-01-03 09:08:48 -05002433/*
Chris Masonbf4ef672008-05-08 13:26:18 -04002434 * The back references tell us which tree holds a ref on a block,
2435 * but it is possible for the tree root field in the reference to
2436 * reflect the original root before a snapshot was made. In this
2437 * case we should search through all the children of a given root
2438 * to find potential holders of references on a block.
2439 *
2440 * Instead, we do something a little less fancy and just search
2441 * all the roots for a given key/block combination.
2442 */
2443static int find_root_for_ref(struct btrfs_root *root,
2444 struct btrfs_path *path,
2445 struct btrfs_key *key0,
2446 int level,
2447 int file_key,
2448 struct btrfs_root **found_root,
2449 u64 bytenr)
2450{
2451 struct btrfs_key root_location;
2452 struct btrfs_root *cur_root = *found_root;
2453 struct btrfs_file_extent_item *file_extent;
2454 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
2455 u64 found_bytenr;
2456 int ret;
2457 int i;
2458
2459 root_location.offset = (u64)-1;
2460 root_location.type = BTRFS_ROOT_ITEM_KEY;
2461 path->lowest_level = level;
2462 path->reada = 0;
2463 while(1) {
2464 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
2465 found_bytenr = 0;
2466 if (ret == 0 && file_key) {
2467 struct extent_buffer *leaf = path->nodes[0];
2468 file_extent = btrfs_item_ptr(leaf, path->slots[0],
2469 struct btrfs_file_extent_item);
2470 if (btrfs_file_extent_type(leaf, file_extent) ==
2471 BTRFS_FILE_EXTENT_REG) {
2472 found_bytenr =
2473 btrfs_file_extent_disk_bytenr(leaf,
2474 file_extent);
2475 }
2476 } else if (ret == 0) {
2477 if (path->nodes[level])
2478 found_bytenr = path->nodes[level]->start;
2479 }
2480
2481 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2482 if (!path->nodes[i])
2483 break;
2484 free_extent_buffer(path->nodes[i]);
2485 path->nodes[i] = NULL;
2486 }
2487 btrfs_release_path(cur_root, path);
2488
2489 if (found_bytenr == bytenr) {
2490 *found_root = cur_root;
2491 ret = 0;
2492 goto out;
2493 }
2494 ret = btrfs_search_root(root->fs_info->tree_root,
2495 root_search_start, &root_search_start);
2496 if (ret)
2497 break;
2498
2499 root_location.objectid = root_search_start;
2500 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
2501 &root_location);
2502 if (!cur_root) {
2503 ret = 1;
2504 break;
2505 }
2506 }
2507out:
2508 path->lowest_level = 0;
2509 return ret;
2510}
2511
2512/*
Chris Mason4313b392008-01-03 09:08:48 -05002513 * note, this releases the path
2514 */
Chris Mason98ed5172008-01-03 10:01:48 -05002515static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05002516 struct btrfs_path *path,
Chris Mason4313b392008-01-03 09:08:48 -05002517 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002518{
2519 struct inode *inode;
2520 struct btrfs_root *found_root;
Chris Masonbf4ef672008-05-08 13:26:18 -04002521 struct btrfs_key root_location;
2522 struct btrfs_key found_key;
Chris Mason4313b392008-01-03 09:08:48 -05002523 struct btrfs_extent_ref *ref;
2524 u64 ref_root;
2525 u64 ref_gen;
2526 u64 ref_objectid;
2527 u64 ref_offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05002528 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04002529 int level;
Chris Masonedbd8d42007-12-21 16:27:24 -05002530
Chris Mason4313b392008-01-03 09:08:48 -05002531 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2532 struct btrfs_extent_ref);
2533 ref_root = btrfs_ref_root(path->nodes[0], ref);
2534 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2535 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2536 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2537 btrfs_release_path(extent_root, path);
2538
Chris Masonbf4ef672008-05-08 13:26:18 -04002539 root_location.objectid = ref_root;
Chris Masonedbd8d42007-12-21 16:27:24 -05002540 if (ref_gen == 0)
Chris Masonbf4ef672008-05-08 13:26:18 -04002541 root_location.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002542 else
Chris Masonbf4ef672008-05-08 13:26:18 -04002543 root_location.offset = (u64)-1;
2544 root_location.type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05002545
2546 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Masonbf4ef672008-05-08 13:26:18 -04002547 &root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002548 BUG_ON(!found_root);
2549
2550 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Chris Masonbf4ef672008-05-08 13:26:18 -04002551 found_key.objectid = ref_objectid;
2552 found_key.type = BTRFS_EXTENT_DATA_KEY;
2553 found_key.offset = ref_offset;
2554 level = 0;
2555
2556 ret = find_root_for_ref(extent_root, path, &found_key,
2557 level, 1, &found_root,
2558 extent_key->objectid);
2559
2560 if (ret)
2561 goto out;
2562
Chris Masonedbd8d42007-12-21 16:27:24 -05002563 mutex_unlock(&extent_root->fs_info->fs_mutex);
2564 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2565 ref_objectid, found_root);
2566 if (inode->i_state & I_NEW) {
2567 /* the inode and parent dir are two different roots */
2568 BTRFS_I(inode)->root = found_root;
2569 BTRFS_I(inode)->location.objectid = ref_objectid;
2570 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2571 BTRFS_I(inode)->location.offset = 0;
2572 btrfs_read_locked_inode(inode);
2573 unlock_new_inode(inode);
2574
2575 }
2576 /* this can happen if the reference is not against
2577 * the latest version of the tree root
2578 */
2579 if (is_bad_inode(inode)) {
2580 mutex_lock(&extent_root->fs_info->fs_mutex);
2581 goto out;
2582 }
2583 relocate_inode_pages(inode, ref_offset, extent_key->offset);
Chris Masonedbd8d42007-12-21 16:27:24 -05002584 iput(inode);
2585 mutex_lock(&extent_root->fs_info->fs_mutex);
2586 } else {
2587 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05002588 struct extent_buffer *eb;
Chris Masonedbd8d42007-12-21 16:27:24 -05002589 int i;
2590
Chris Masonedbd8d42007-12-21 16:27:24 -05002591 eb = read_tree_block(found_root, extent_key->objectid,
2592 extent_key->offset);
2593 level = btrfs_header_level(eb);
2594
2595 if (level == 0)
2596 btrfs_item_key_to_cpu(eb, &found_key, 0);
2597 else
2598 btrfs_node_key_to_cpu(eb, &found_key, 0);
2599
2600 free_extent_buffer(eb);
2601
Chris Masonbf4ef672008-05-08 13:26:18 -04002602 ret = find_root_for_ref(extent_root, path, &found_key,
2603 level, 0, &found_root,
2604 extent_key->objectid);
2605
2606 if (ret)
2607 goto out;
2608
2609 trans = btrfs_start_transaction(found_root, 1);
2610
Chris Masonedbd8d42007-12-21 16:27:24 -05002611 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05002612 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002613 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2614 0, 1);
2615 path->lowest_level = 0;
2616 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2617 if (!path->nodes[i])
2618 break;
2619 free_extent_buffer(path->nodes[i]);
2620 path->nodes[i] = NULL;
2621 }
2622 btrfs_release_path(found_root, path);
2623 btrfs_end_transaction(trans, found_root);
2624 }
2625
2626out:
2627 return 0;
2628}
2629
Chris Masona061fc82008-05-07 11:43:44 -04002630static int noinline del_extent_zero(struct btrfs_root *extent_root,
2631 struct btrfs_path *path,
2632 struct btrfs_key *extent_key)
2633{
2634 int ret;
2635 struct btrfs_trans_handle *trans;
2636
2637 trans = btrfs_start_transaction(extent_root, 1);
2638 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
2639 if (ret > 0) {
2640 ret = -EIO;
2641 goto out;
2642 }
2643 if (ret < 0)
2644 goto out;
2645 ret = btrfs_del_item(trans, extent_root, path);
2646out:
2647 btrfs_end_transaction(trans, extent_root);
2648 return ret;
2649}
2650
Chris Mason98ed5172008-01-03 10:01:48 -05002651static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2652 struct btrfs_path *path,
2653 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002654{
2655 struct btrfs_key key;
2656 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002657 struct extent_buffer *leaf;
Chris Masonedbd8d42007-12-21 16:27:24 -05002658 u32 nritems;
2659 u32 item_size;
2660 int ret = 0;
2661
Chris Masona061fc82008-05-07 11:43:44 -04002662 if (extent_key->objectid == 0) {
2663 ret = del_extent_zero(extent_root, path, extent_key);
2664 goto out;
2665 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002666 key.objectid = extent_key->objectid;
2667 key.type = BTRFS_EXTENT_REF_KEY;
2668 key.offset = 0;
2669
2670 while(1) {
2671 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2672
Chris Masonedbd8d42007-12-21 16:27:24 -05002673 if (ret < 0)
2674 goto out;
2675
2676 ret = 0;
2677 leaf = path->nodes[0];
2678 nritems = btrfs_header_nritems(leaf);
Chris Masona061fc82008-05-07 11:43:44 -04002679 if (path->slots[0] == nritems) {
2680 ret = btrfs_next_leaf(extent_root, path);
2681 if (ret > 0) {
2682 ret = 0;
2683 goto out;
2684 }
2685 if (ret < 0)
2686 goto out;
Chris Masonbf4ef672008-05-08 13:26:18 -04002687 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04002688 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002689
2690 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masona061fc82008-05-07 11:43:44 -04002691 if (found_key.objectid != extent_key->objectid) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002692 break;
Chris Masona061fc82008-05-07 11:43:44 -04002693 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002694
Chris Masona061fc82008-05-07 11:43:44 -04002695 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002696 break;
Chris Masona061fc82008-05-07 11:43:44 -04002697 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002698
2699 key.offset = found_key.offset + 1;
2700 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2701
Chris Mason4313b392008-01-03 09:08:48 -05002702 ret = relocate_one_reference(extent_root, path, extent_key);
Chris Masonedbd8d42007-12-21 16:27:24 -05002703 if (ret)
2704 goto out;
2705 }
2706 ret = 0;
2707out:
2708 btrfs_release_path(extent_root, path);
2709 return ret;
2710}
2711
Chris Masonec44a352008-04-28 15:29:52 -04002712static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
2713{
2714 u64 num_devices;
2715 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
2716 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
2717
Chris Masona061fc82008-05-07 11:43:44 -04002718 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04002719 if (num_devices == 1) {
2720 stripped |= BTRFS_BLOCK_GROUP_DUP;
2721 stripped = flags & ~stripped;
2722
2723 /* turn raid0 into single device chunks */
2724 if (flags & BTRFS_BLOCK_GROUP_RAID0)
2725 return stripped;
2726
2727 /* turn mirroring into duplication */
2728 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2729 BTRFS_BLOCK_GROUP_RAID10))
2730 return stripped | BTRFS_BLOCK_GROUP_DUP;
2731 return flags;
2732 } else {
2733 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04002734 if (flags & stripped)
2735 return flags;
2736
2737 stripped |= BTRFS_BLOCK_GROUP_DUP;
2738 stripped = flags & ~stripped;
2739
2740 /* switch duplicated blocks with raid1 */
2741 if (flags & BTRFS_BLOCK_GROUP_DUP)
2742 return stripped | BTRFS_BLOCK_GROUP_RAID1;
2743
2744 /* turn single device chunks into raid0 */
2745 return stripped | BTRFS_BLOCK_GROUP_RAID0;
2746 }
2747 return flags;
2748}
2749
Chris Mason8f18cf12008-04-25 16:53:30 -04002750int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05002751{
2752 struct btrfs_trans_handle *trans;
2753 struct btrfs_root *tree_root = root->fs_info->tree_root;
2754 struct btrfs_path *path;
2755 u64 cur_byte;
2756 u64 total_found;
Chris Mason8f18cf12008-04-25 16:53:30 -04002757 u64 shrink_last_byte;
Chris Masonec44a352008-04-28 15:29:52 -04002758 u64 new_alloc_flags;
Chris Mason8f18cf12008-04-25 16:53:30 -04002759 struct btrfs_block_group_cache *shrink_block_group;
Chris Masonedbd8d42007-12-21 16:27:24 -05002760 struct btrfs_fs_info *info = root->fs_info;
Chris Masonedbd8d42007-12-21 16:27:24 -05002761 struct btrfs_key key;
Yan73e48b22008-01-03 14:14:39 -05002762 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002763 struct extent_buffer *leaf;
2764 u32 nritems;
2765 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04002766 int progress;
Chris Masonedbd8d42007-12-21 16:27:24 -05002767
Chris Mason8f18cf12008-04-25 16:53:30 -04002768 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
2769 shrink_start);
2770 BUG_ON(!shrink_block_group);
2771
2772 shrink_last_byte = shrink_start + shrink_block_group->key.offset;
2773
2774 shrink_block_group->space_info->total_bytes -=
2775 shrink_block_group->key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05002776 path = btrfs_alloc_path();
2777 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05002778 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002779
2780again:
Chris Masonec44a352008-04-28 15:29:52 -04002781 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
2782 trans = btrfs_start_transaction(root, 1);
2783 new_alloc_flags = update_block_group_flags(root,
2784 shrink_block_group->flags);
2785 do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002786 btrfs_block_group_used(&shrink_block_group->item) +
Chris Masonec44a352008-04-28 15:29:52 -04002787 2 * 1024 * 1024, new_alloc_flags);
2788 btrfs_end_transaction(trans, root);
2789 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002790 shrink_block_group->ro = 1;
2791
Chris Masonedbd8d42007-12-21 16:27:24 -05002792 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04002793 progress = 0;
Chris Mason8f18cf12008-04-25 16:53:30 -04002794 key.objectid = shrink_start;
Chris Masonedbd8d42007-12-21 16:27:24 -05002795 key.offset = 0;
2796 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05002797 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05002798
Yan73e48b22008-01-03 14:14:39 -05002799 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2800 if (ret < 0)
2801 goto out;
2802
Chris Mason0b86a832008-03-24 15:01:56 -04002803 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yan73e48b22008-01-03 14:14:39 -05002804 if (ret < 0)
2805 goto out;
Chris Mason8f18cf12008-04-25 16:53:30 -04002806
Yan73e48b22008-01-03 14:14:39 -05002807 if (ret == 0) {
2808 leaf = path->nodes[0];
2809 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04002810 if (found_key.objectid + found_key.offset > shrink_start &&
2811 found_key.objectid < shrink_last_byte) {
Yan73e48b22008-01-03 14:14:39 -05002812 cur_byte = found_key.objectid;
2813 key.objectid = cur_byte;
2814 }
2815 }
2816 btrfs_release_path(root, path);
2817
2818 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002819 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2820 if (ret < 0)
2821 goto out;
Yan73e48b22008-01-03 14:14:39 -05002822
Chris Masonedbd8d42007-12-21 16:27:24 -05002823 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05002824 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05002825next:
2826 if (path->slots[0] >= nritems) {
2827 ret = btrfs_next_leaf(root, path);
2828 if (ret < 0)
2829 goto out;
2830 if (ret == 1) {
2831 ret = 0;
2832 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05002833 }
Yan73e48b22008-01-03 14:14:39 -05002834 leaf = path->nodes[0];
2835 nritems = btrfs_header_nritems(leaf);
2836 }
2837
2838 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05002839
Chris Mason8f18cf12008-04-25 16:53:30 -04002840 if (found_key.objectid >= shrink_last_byte)
2841 break;
2842
Chris Mason725c8462008-01-04 16:47:16 -05002843 if (progress && need_resched()) {
2844 memcpy(&key, &found_key, sizeof(key));
2845 mutex_unlock(&root->fs_info->fs_mutex);
2846 cond_resched();
2847 mutex_lock(&root->fs_info->fs_mutex);
2848 btrfs_release_path(root, path);
2849 btrfs_search_slot(NULL, root, &key, path, 0, 0);
2850 progress = 0;
2851 goto next;
2852 }
2853 progress = 1;
2854
Yan73e48b22008-01-03 14:14:39 -05002855 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
2856 found_key.objectid + found_key.offset <= cur_byte) {
2857 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05002858 goto next;
2859 }
Yan73e48b22008-01-03 14:14:39 -05002860
Chris Masonedbd8d42007-12-21 16:27:24 -05002861 total_found++;
2862 cur_byte = found_key.objectid + found_key.offset;
2863 key.objectid = cur_byte;
2864 btrfs_release_path(root, path);
2865 ret = relocate_one_extent(root, path, &found_key);
2866 }
2867
2868 btrfs_release_path(root, path);
2869
2870 if (total_found > 0) {
2871 trans = btrfs_start_transaction(tree_root, 1);
2872 btrfs_commit_transaction(trans, tree_root);
2873
2874 mutex_unlock(&root->fs_info->fs_mutex);
2875 btrfs_clean_old_snapshots(tree_root);
2876 mutex_lock(&root->fs_info->fs_mutex);
2877
2878 trans = btrfs_start_transaction(tree_root, 1);
2879 btrfs_commit_transaction(trans, tree_root);
2880 goto again;
2881 }
2882
Chris Mason8f18cf12008-04-25 16:53:30 -04002883 /*
2884 * we've freed all the extents, now remove the block
2885 * group item from the tree
2886 */
Chris Masonedbd8d42007-12-21 16:27:24 -05002887 trans = btrfs_start_transaction(root, 1);
Chris Mason8f18cf12008-04-25 16:53:30 -04002888 memcpy(&key, &shrink_block_group->key, sizeof(key));
Chris Mason4313b392008-01-03 09:08:48 -05002889
Chris Mason8f18cf12008-04-25 16:53:30 -04002890 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2891 if (ret > 0)
2892 ret = -EIO;
2893 if (ret < 0)
2894 goto out;
Yan73e48b22008-01-03 14:14:39 -05002895
Chris Mason8f18cf12008-04-25 16:53:30 -04002896 leaf = path->nodes[0];
2897 nritems = btrfs_header_nritems(leaf);
2898 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2899 kfree(shrink_block_group);
Chris Mason1372f8e2008-01-04 09:34:54 -05002900
Chris Mason8f18cf12008-04-25 16:53:30 -04002901 clear_extent_bits(&info->block_group_cache, found_key.objectid,
2902 found_key.objectid + found_key.offset - 1,
2903 (unsigned int)-1, GFP_NOFS);
Yan73e48b22008-01-03 14:14:39 -05002904
Chris Mason8f18cf12008-04-25 16:53:30 -04002905 btrfs_del_item(trans, root, path);
2906 clear_extent_dirty(&info->free_space_cache,
2907 shrink_start, shrink_last_byte - 1,
Chris Masonedbd8d42007-12-21 16:27:24 -05002908 GFP_NOFS);
2909 btrfs_commit_transaction(trans, root);
2910out:
2911 btrfs_free_path(path);
2912 return ret;
2913}
2914
Chris Mason0b86a832008-03-24 15:01:56 -04002915int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
2916 struct btrfs_key *key)
2917{
2918 int ret;
2919 struct btrfs_key found_key;
2920 struct extent_buffer *leaf;
2921 int slot;
2922
2923 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
2924 if (ret < 0)
2925 return ret;
2926 while(1) {
2927 slot = path->slots[0];
2928 leaf = path->nodes[0];
2929 if (slot >= btrfs_header_nritems(leaf)) {
2930 ret = btrfs_next_leaf(root, path);
2931 if (ret == 0)
2932 continue;
2933 if (ret < 0)
2934 goto error;
2935 break;
2936 }
2937 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2938
2939 if (found_key.objectid >= key->objectid &&
2940 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
2941 return 0;
2942 path->slots[0]++;
2943 }
2944 ret = -ENOENT;
2945error:
2946 return ret;
2947}
2948
Chris Mason9078a3e2007-04-26 16:46:15 -04002949int btrfs_read_block_groups(struct btrfs_root *root)
2950{
2951 struct btrfs_path *path;
2952 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04002953 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04002954 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04002955 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04002956 struct btrfs_space_info *space_info;
Chris Masond1310b22008-01-24 16:13:08 -05002957 struct extent_io_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002958 struct btrfs_key key;
2959 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04002960 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04002961
2962 block_group_cache = &info->block_group_cache;
Chris Masonbe744172007-05-06 10:15:01 -04002963 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04002964 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04002965 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002966 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04002967 path = btrfs_alloc_path();
2968 if (!path)
2969 return -ENOMEM;
2970
2971 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04002972 ret = find_first_block_group(root, path, &key);
2973 if (ret > 0) {
2974 ret = 0;
2975 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04002976 }
Chris Mason0b86a832008-03-24 15:01:56 -04002977 if (ret != 0)
2978 goto error;
2979
Chris Mason5f39d392007-10-15 16:14:19 -04002980 leaf = path->nodes[0];
2981 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04002982 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04002983 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04002984 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04002985 break;
2986 }
Chris Mason3e1ad542007-05-07 20:03:49 -04002987
Chris Mason5f39d392007-10-15 16:14:19 -04002988 read_extent_buffer(leaf, &cache->item,
2989 btrfs_item_ptr_offset(leaf, path->slots[0]),
2990 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04002991 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04002992
Chris Mason9078a3e2007-04-26 16:46:15 -04002993 key.objectid = found_key.objectid + found_key.offset;
2994 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04002995 cache->flags = btrfs_block_group_flags(&cache->item);
2996 bit = 0;
2997 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04002998 bit = BLOCK_GROUP_DATA;
Chris Mason0b86a832008-03-24 15:01:56 -04002999 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3000 bit = BLOCK_GROUP_SYSTEM;
3001 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason96b51792007-10-15 16:15:19 -04003002 bit = BLOCK_GROUP_METADATA;
Chris Mason31f3c992007-04-30 15:25:45 -04003003 }
Chris Mason8790d502008-04-03 16:29:03 -04003004 set_avail_alloc_bits(info, cache->flags);
Chris Mason96b51792007-10-15 16:15:19 -04003005
Chris Mason6324fbf2008-03-24 15:01:59 -04003006 ret = update_space_info(info, cache->flags, found_key.offset,
3007 btrfs_block_group_used(&cache->item),
3008 &space_info);
3009 BUG_ON(ret);
3010 cache->space_info = space_info;
3011
Chris Mason96b51792007-10-15 16:15:19 -04003012 /* use EXTENT_LOCKED to prevent merging */
3013 set_extent_bits(block_group_cache, found_key.objectid,
3014 found_key.objectid + found_key.offset - 1,
3015 bit | EXTENT_LOCKED, GFP_NOFS);
3016 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04003017 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04003018
Chris Mason9078a3e2007-04-26 16:46:15 -04003019 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04003020 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04003021 break;
3022 }
Chris Mason0b86a832008-03-24 15:01:56 -04003023 ret = 0;
3024error:
Chris Mason9078a3e2007-04-26 16:46:15 -04003025 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003026 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003027}
Chris Mason6324fbf2008-03-24 15:01:59 -04003028
3029int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3030 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04003031 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04003032 u64 size)
3033{
3034 int ret;
3035 int bit = 0;
3036 struct btrfs_root *extent_root;
3037 struct btrfs_block_group_cache *cache;
3038 struct extent_io_tree *block_group_cache;
3039
3040 extent_root = root->fs_info->extent_root;
3041 block_group_cache = &root->fs_info->block_group_cache;
3042
Chris Mason8f18cf12008-04-25 16:53:30 -04003043 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04003044 BUG_ON(!cache);
Chris Masone17cade2008-04-15 15:41:47 -04003045 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04003046 cache->key.offset = size;
Chris Masone17cade2008-04-15 15:41:47 -04003047
Chris Mason6324fbf2008-03-24 15:01:59 -04003048 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3049 memset(&cache->item, 0, sizeof(cache->item));
3050 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04003051 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3052 cache->flags = type;
3053 btrfs_set_block_group_flags(&cache->item, type);
3054
3055 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
3056 &cache->space_info);
3057 BUG_ON(ret);
3058
Chris Masond18a2c42008-04-04 15:40:00 -04003059 bit = block_group_state_bits(type);
Chris Masone17cade2008-04-15 15:41:47 -04003060 set_extent_bits(block_group_cache, chunk_offset,
3061 chunk_offset + size - 1,
Chris Mason6324fbf2008-03-24 15:01:59 -04003062 bit | EXTENT_LOCKED, GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04003063
Chris Masone17cade2008-04-15 15:41:47 -04003064 set_state_private(block_group_cache, chunk_offset,
3065 (unsigned long)cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04003066 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3067 sizeof(cache->item));
3068 BUG_ON(ret);
3069
3070 finish_current_insert(trans, extent_root);
3071 ret = del_pending_extents(trans, extent_root);
3072 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04003073 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason6324fbf2008-03-24 15:01:59 -04003074 return 0;
3075}