Commit ca7a79ad8dbe24669a8ced01f9fc0126b872745d

Authored by Chris Mason
1 parent 188de649c5

Btrfs: Pass down the expected generation number when reading tree blocks

Signed-off-by: Chris Mason <chris.mason@oracle.com>

Showing 6 changed files with 55 additions and 64 deletions Inline Diff

1 /* 1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved. 2 * Copyright (C) 2007 Oracle. All rights reserved.
3 * 3 *
4 * This program is free software; you can redistribute it and/or 4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public 5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation. 6 * License v2 as published by the Free Software Foundation.
7 * 7 *
8 * This program is distributed in the hope that it will be useful, 8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details. 11 * General Public License for more details.
12 * 12 *
13 * You should have received a copy of the GNU General Public 13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the 14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA. 16 * Boston, MA 021110-1307, USA.
17 */ 17 */
18 18
19 #include <linux/sched.h> 19 #include <linux/sched.h>
20 #include "ctree.h" 20 #include "ctree.h"
21 #include "disk-io.h" 21 #include "disk-io.h"
22 #include "transaction.h" 22 #include "transaction.h"
23 #include "print-tree.h" 23 #include "print-tree.h"
24 24
25 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root 25 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
26 *root, struct btrfs_path *path, int level); 26 *root, struct btrfs_path *path, int level);
27 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root 27 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
28 *root, struct btrfs_key *ins_key, 28 *root, struct btrfs_key *ins_key,
29 struct btrfs_path *path, int data_size, int extend); 29 struct btrfs_path *path, int data_size, int extend);
30 static int push_node_left(struct btrfs_trans_handle *trans, 30 static int push_node_left(struct btrfs_trans_handle *trans,
31 struct btrfs_root *root, struct extent_buffer *dst, 31 struct btrfs_root *root, struct extent_buffer *dst,
32 struct extent_buffer *src, int empty); 32 struct extent_buffer *src, int empty);
33 static int balance_node_right(struct btrfs_trans_handle *trans, 33 static int balance_node_right(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root, 34 struct btrfs_root *root,
35 struct extent_buffer *dst_buf, 35 struct extent_buffer *dst_buf,
36 struct extent_buffer *src_buf); 36 struct extent_buffer *src_buf);
37 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, 37 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
38 struct btrfs_path *path, int level, int slot); 38 struct btrfs_path *path, int level, int slot);
39 39
40 inline void btrfs_init_path(struct btrfs_path *p) 40 inline void btrfs_init_path(struct btrfs_path *p)
41 { 41 {
42 memset(p, 0, sizeof(*p)); 42 memset(p, 0, sizeof(*p));
43 } 43 }
44 44
45 struct btrfs_path *btrfs_alloc_path(void) 45 struct btrfs_path *btrfs_alloc_path(void)
46 { 46 {
47 struct btrfs_path *path; 47 struct btrfs_path *path;
48 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS); 48 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
49 if (path) { 49 if (path) {
50 btrfs_init_path(path); 50 btrfs_init_path(path);
51 path->reada = 1; 51 path->reada = 1;
52 } 52 }
53 return path; 53 return path;
54 } 54 }
55 55
56 void btrfs_free_path(struct btrfs_path *p) 56 void btrfs_free_path(struct btrfs_path *p)
57 { 57 {
58 btrfs_release_path(NULL, p); 58 btrfs_release_path(NULL, p);
59 kmem_cache_free(btrfs_path_cachep, p); 59 kmem_cache_free(btrfs_path_cachep, p);
60 } 60 }
61 61
62 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p) 62 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
63 { 63 {
64 int i; 64 int i;
65 for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 65 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
66 if (!p->nodes[i]) 66 if (!p->nodes[i])
67 break; 67 break;
68 free_extent_buffer(p->nodes[i]); 68 free_extent_buffer(p->nodes[i]);
69 } 69 }
70 memset(p, 0, sizeof(*p)); 70 memset(p, 0, sizeof(*p));
71 } 71 }
72 72
73 static void add_root_to_dirty_list(struct btrfs_root *root) 73 static void add_root_to_dirty_list(struct btrfs_root *root)
74 { 74 {
75 if (root->track_dirty && list_empty(&root->dirty_list)) { 75 if (root->track_dirty && list_empty(&root->dirty_list)) {
76 list_add(&root->dirty_list, 76 list_add(&root->dirty_list,
77 &root->fs_info->dirty_cowonly_roots); 77 &root->fs_info->dirty_cowonly_roots);
78 } 78 }
79 } 79 }
80 80
81 int btrfs_copy_root(struct btrfs_trans_handle *trans, 81 int btrfs_copy_root(struct btrfs_trans_handle *trans,
82 struct btrfs_root *root, 82 struct btrfs_root *root,
83 struct extent_buffer *buf, 83 struct extent_buffer *buf,
84 struct extent_buffer **cow_ret, u64 new_root_objectid) 84 struct extent_buffer **cow_ret, u64 new_root_objectid)
85 { 85 {
86 struct extent_buffer *cow; 86 struct extent_buffer *cow;
87 u32 nritems; 87 u32 nritems;
88 int ret = 0; 88 int ret = 0;
89 int level; 89 int level;
90 struct btrfs_key first_key; 90 struct btrfs_key first_key;
91 struct btrfs_root *new_root; 91 struct btrfs_root *new_root;
92 92
93 new_root = kmalloc(sizeof(*new_root), GFP_NOFS); 93 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
94 if (!new_root) 94 if (!new_root)
95 return -ENOMEM; 95 return -ENOMEM;
96 96
97 memcpy(new_root, root, sizeof(*new_root)); 97 memcpy(new_root, root, sizeof(*new_root));
98 new_root->root_key.objectid = new_root_objectid; 98 new_root->root_key.objectid = new_root_objectid;
99 99
100 WARN_ON(root->ref_cows && trans->transid != 100 WARN_ON(root->ref_cows && trans->transid !=
101 root->fs_info->running_transaction->transid); 101 root->fs_info->running_transaction->transid);
102 WARN_ON(root->ref_cows && trans->transid != root->last_trans); 102 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
103 103
104 level = btrfs_header_level(buf); 104 level = btrfs_header_level(buf);
105 nritems = btrfs_header_nritems(buf); 105 nritems = btrfs_header_nritems(buf);
106 if (nritems) { 106 if (nritems) {
107 if (level == 0) 107 if (level == 0)
108 btrfs_item_key_to_cpu(buf, &first_key, 0); 108 btrfs_item_key_to_cpu(buf, &first_key, 0);
109 else 109 else
110 btrfs_node_key_to_cpu(buf, &first_key, 0); 110 btrfs_node_key_to_cpu(buf, &first_key, 0);
111 } else { 111 } else {
112 first_key.objectid = 0; 112 first_key.objectid = 0;
113 } 113 }
114 cow = __btrfs_alloc_free_block(trans, new_root, buf->len, 114 cow = __btrfs_alloc_free_block(trans, new_root, buf->len,
115 new_root_objectid, 115 new_root_objectid,
116 trans->transid, first_key.objectid, 116 trans->transid, first_key.objectid,
117 level, buf->start, 0); 117 level, buf->start, 0);
118 if (IS_ERR(cow)) { 118 if (IS_ERR(cow)) {
119 kfree(new_root); 119 kfree(new_root);
120 return PTR_ERR(cow); 120 return PTR_ERR(cow);
121 } 121 }
122 122
123 copy_extent_buffer(cow, buf, 0, 0, cow->len); 123 copy_extent_buffer(cow, buf, 0, 0, cow->len);
124 btrfs_set_header_bytenr(cow, cow->start); 124 btrfs_set_header_bytenr(cow, cow->start);
125 btrfs_set_header_generation(cow, trans->transid); 125 btrfs_set_header_generation(cow, trans->transid);
126 btrfs_set_header_owner(cow, new_root_objectid); 126 btrfs_set_header_owner(cow, new_root_objectid);
127 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN); 127 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
128 128
129 WARN_ON(btrfs_header_generation(buf) > trans->transid); 129 WARN_ON(btrfs_header_generation(buf) > trans->transid);
130 ret = btrfs_inc_ref(trans, new_root, buf); 130 ret = btrfs_inc_ref(trans, new_root, buf);
131 kfree(new_root); 131 kfree(new_root);
132 132
133 if (ret) 133 if (ret)
134 return ret; 134 return ret;
135 135
136 btrfs_mark_buffer_dirty(cow); 136 btrfs_mark_buffer_dirty(cow);
137 *cow_ret = cow; 137 *cow_ret = cow;
138 return 0; 138 return 0;
139 } 139 }
140 140
141 int __btrfs_cow_block(struct btrfs_trans_handle *trans, 141 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
142 struct btrfs_root *root, 142 struct btrfs_root *root,
143 struct extent_buffer *buf, 143 struct extent_buffer *buf,
144 struct extent_buffer *parent, int parent_slot, 144 struct extent_buffer *parent, int parent_slot,
145 struct extent_buffer **cow_ret, 145 struct extent_buffer **cow_ret,
146 u64 search_start, u64 empty_size) 146 u64 search_start, u64 empty_size)
147 { 147 {
148 u64 root_gen; 148 u64 root_gen;
149 struct extent_buffer *cow; 149 struct extent_buffer *cow;
150 u32 nritems; 150 u32 nritems;
151 int ret = 0; 151 int ret = 0;
152 int different_trans = 0; 152 int different_trans = 0;
153 int level; 153 int level;
154 struct btrfs_key first_key; 154 struct btrfs_key first_key;
155 155
156 if (root->ref_cows) { 156 if (root->ref_cows) {
157 root_gen = trans->transid; 157 root_gen = trans->transid;
158 } else { 158 } else {
159 root_gen = 0; 159 root_gen = 0;
160 } 160 }
161 WARN_ON(root->ref_cows && trans->transid != 161 WARN_ON(root->ref_cows && trans->transid !=
162 root->fs_info->running_transaction->transid); 162 root->fs_info->running_transaction->transid);
163 WARN_ON(root->ref_cows && trans->transid != root->last_trans); 163 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
164 164
165 level = btrfs_header_level(buf); 165 level = btrfs_header_level(buf);
166 nritems = btrfs_header_nritems(buf); 166 nritems = btrfs_header_nritems(buf);
167 if (nritems) { 167 if (nritems) {
168 if (level == 0) 168 if (level == 0)
169 btrfs_item_key_to_cpu(buf, &first_key, 0); 169 btrfs_item_key_to_cpu(buf, &first_key, 0);
170 else 170 else
171 btrfs_node_key_to_cpu(buf, &first_key, 0); 171 btrfs_node_key_to_cpu(buf, &first_key, 0);
172 } else { 172 } else {
173 first_key.objectid = 0; 173 first_key.objectid = 0;
174 } 174 }
175 cow = __btrfs_alloc_free_block(trans, root, buf->len, 175 cow = __btrfs_alloc_free_block(trans, root, buf->len,
176 root->root_key.objectid, 176 root->root_key.objectid,
177 root_gen, first_key.objectid, level, 177 root_gen, first_key.objectid, level,
178 search_start, empty_size); 178 search_start, empty_size);
179 if (IS_ERR(cow)) 179 if (IS_ERR(cow))
180 return PTR_ERR(cow); 180 return PTR_ERR(cow);
181 181
182 copy_extent_buffer(cow, buf, 0, 0, cow->len); 182 copy_extent_buffer(cow, buf, 0, 0, cow->len);
183 btrfs_set_header_bytenr(cow, cow->start); 183 btrfs_set_header_bytenr(cow, cow->start);
184 btrfs_set_header_generation(cow, trans->transid); 184 btrfs_set_header_generation(cow, trans->transid);
185 btrfs_set_header_owner(cow, root->root_key.objectid); 185 btrfs_set_header_owner(cow, root->root_key.objectid);
186 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN); 186 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
187 187
188 WARN_ON(btrfs_header_generation(buf) > trans->transid); 188 WARN_ON(btrfs_header_generation(buf) > trans->transid);
189 if (btrfs_header_generation(buf) != trans->transid) { 189 if (btrfs_header_generation(buf) != trans->transid) {
190 different_trans = 1; 190 different_trans = 1;
191 ret = btrfs_inc_ref(trans, root, buf); 191 ret = btrfs_inc_ref(trans, root, buf);
192 if (ret) 192 if (ret)
193 return ret; 193 return ret;
194 } else { 194 } else {
195 clean_tree_block(trans, root, buf); 195 clean_tree_block(trans, root, buf);
196 } 196 }
197 197
198 if (buf == root->node) { 198 if (buf == root->node) {
199 root_gen = btrfs_header_generation(buf); 199 root_gen = btrfs_header_generation(buf);
200 root->node = cow; 200 root->node = cow;
201 extent_buffer_get(cow); 201 extent_buffer_get(cow);
202 if (buf != root->commit_root) { 202 if (buf != root->commit_root) {
203 btrfs_free_extent(trans, root, buf->start, 203 btrfs_free_extent(trans, root, buf->start,
204 buf->len, root->root_key.objectid, 204 buf->len, root->root_key.objectid,
205 root_gen, 0, 0, 1); 205 root_gen, 0, 0, 1);
206 } 206 }
207 free_extent_buffer(buf); 207 free_extent_buffer(buf);
208 add_root_to_dirty_list(root); 208 add_root_to_dirty_list(root);
209 } else { 209 } else {
210 root_gen = btrfs_header_generation(parent); 210 root_gen = btrfs_header_generation(parent);
211 btrfs_set_node_blockptr(parent, parent_slot, 211 btrfs_set_node_blockptr(parent, parent_slot,
212 cow->start); 212 cow->start);
213 WARN_ON(trans->transid == 0); 213 WARN_ON(trans->transid == 0);
214 btrfs_set_node_ptr_generation(parent, parent_slot, 214 btrfs_set_node_ptr_generation(parent, parent_slot,
215 trans->transid); 215 trans->transid);
216 btrfs_mark_buffer_dirty(parent); 216 btrfs_mark_buffer_dirty(parent);
217 WARN_ON(btrfs_header_generation(parent) != trans->transid); 217 WARN_ON(btrfs_header_generation(parent) != trans->transid);
218 btrfs_free_extent(trans, root, buf->start, buf->len, 218 btrfs_free_extent(trans, root, buf->start, buf->len,
219 btrfs_header_owner(parent), root_gen, 219 btrfs_header_owner(parent), root_gen,
220 0, 0, 1); 220 0, 0, 1);
221 } 221 }
222 free_extent_buffer(buf); 222 free_extent_buffer(buf);
223 btrfs_mark_buffer_dirty(cow); 223 btrfs_mark_buffer_dirty(cow);
224 *cow_ret = cow; 224 *cow_ret = cow;
225 return 0; 225 return 0;
226 } 226 }
227 227
228 int btrfs_cow_block(struct btrfs_trans_handle *trans, 228 int btrfs_cow_block(struct btrfs_trans_handle *trans,
229 struct btrfs_root *root, struct extent_buffer *buf, 229 struct btrfs_root *root, struct extent_buffer *buf,
230 struct extent_buffer *parent, int parent_slot, 230 struct extent_buffer *parent, int parent_slot,
231 struct extent_buffer **cow_ret) 231 struct extent_buffer **cow_ret)
232 { 232 {
233 u64 search_start; 233 u64 search_start;
234 u64 header_trans; 234 u64 header_trans;
235 int ret; 235 int ret;
236 236
237 if (trans->transaction != root->fs_info->running_transaction) { 237 if (trans->transaction != root->fs_info->running_transaction) {
238 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, 238 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
239 root->fs_info->running_transaction->transid); 239 root->fs_info->running_transaction->transid);
240 WARN_ON(1); 240 WARN_ON(1);
241 } 241 }
242 if (trans->transid != root->fs_info->generation) { 242 if (trans->transid != root->fs_info->generation) {
243 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, 243 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
244 root->fs_info->generation); 244 root->fs_info->generation);
245 WARN_ON(1); 245 WARN_ON(1);
246 } 246 }
247 247
248 header_trans = btrfs_header_generation(buf); 248 header_trans = btrfs_header_generation(buf);
249 spin_lock(&root->fs_info->hash_lock); 249 spin_lock(&root->fs_info->hash_lock);
250 if (header_trans == trans->transid && 250 if (header_trans == trans->transid &&
251 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { 251 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
252 *cow_ret = buf; 252 *cow_ret = buf;
253 spin_unlock(&root->fs_info->hash_lock); 253 spin_unlock(&root->fs_info->hash_lock);
254 return 0; 254 return 0;
255 } 255 }
256 spin_unlock(&root->fs_info->hash_lock); 256 spin_unlock(&root->fs_info->hash_lock);
257 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1); 257 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
258 ret = __btrfs_cow_block(trans, root, buf, parent, 258 ret = __btrfs_cow_block(trans, root, buf, parent,
259 parent_slot, cow_ret, search_start, 0); 259 parent_slot, cow_ret, search_start, 0);
260 return ret; 260 return ret;
261 } 261 }
262 262
263 static int close_blocks(u64 blocknr, u64 other, u32 blocksize) 263 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
264 { 264 {
265 if (blocknr < other && other - (blocknr + blocksize) < 32768) 265 if (blocknr < other && other - (blocknr + blocksize) < 32768)
266 return 1; 266 return 1;
267 if (blocknr > other && blocknr - (other + blocksize) < 32768) 267 if (blocknr > other && blocknr - (other + blocksize) < 32768)
268 return 1; 268 return 1;
269 return 0; 269 return 0;
270 } 270 }
271 271
272 /* 272 /*
273 * compare two keys in a memcmp fashion 273 * compare two keys in a memcmp fashion
274 */ 274 */
275 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) 275 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
276 { 276 {
277 struct btrfs_key k1; 277 struct btrfs_key k1;
278 278
279 btrfs_disk_key_to_cpu(&k1, disk); 279 btrfs_disk_key_to_cpu(&k1, disk);
280 280
281 if (k1.objectid > k2->objectid) 281 if (k1.objectid > k2->objectid)
282 return 1; 282 return 1;
283 if (k1.objectid < k2->objectid) 283 if (k1.objectid < k2->objectid)
284 return -1; 284 return -1;
285 if (k1.type > k2->type) 285 if (k1.type > k2->type)
286 return 1; 286 return 1;
287 if (k1.type < k2->type) 287 if (k1.type < k2->type)
288 return -1; 288 return -1;
289 if (k1.offset > k2->offset) 289 if (k1.offset > k2->offset)
290 return 1; 290 return 1;
291 if (k1.offset < k2->offset) 291 if (k1.offset < k2->offset)
292 return -1; 292 return -1;
293 return 0; 293 return 0;
294 } 294 }
295 295
296 296
297 int btrfs_realloc_node(struct btrfs_trans_handle *trans, 297 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
298 struct btrfs_root *root, struct extent_buffer *parent, 298 struct btrfs_root *root, struct extent_buffer *parent,
299 int start_slot, int cache_only, u64 *last_ret, 299 int start_slot, int cache_only, u64 *last_ret,
300 struct btrfs_key *progress) 300 struct btrfs_key *progress)
301 { 301 {
302 struct extent_buffer *cur; 302 struct extent_buffer *cur;
303 struct extent_buffer *tmp; 303 struct extent_buffer *tmp;
304 u64 blocknr; 304 u64 blocknr;
305 u64 gen;
305 u64 search_start = *last_ret; 306 u64 search_start = *last_ret;
306 u64 last_block = 0; 307 u64 last_block = 0;
307 u64 other; 308 u64 other;
308 u32 parent_nritems; 309 u32 parent_nritems;
309 int end_slot; 310 int end_slot;
310 int i; 311 int i;
311 int err = 0; 312 int err = 0;
312 int parent_level; 313 int parent_level;
313 int uptodate; 314 int uptodate;
314 u32 blocksize; 315 u32 blocksize;
315 int progress_passed = 0; 316 int progress_passed = 0;
316 struct btrfs_disk_key disk_key; 317 struct btrfs_disk_key disk_key;
317 318
318 parent_level = btrfs_header_level(parent); 319 parent_level = btrfs_header_level(parent);
319 if (cache_only && parent_level != 1) 320 if (cache_only && parent_level != 1)
320 return 0; 321 return 0;
321 322
322 if (trans->transaction != root->fs_info->running_transaction) { 323 if (trans->transaction != root->fs_info->running_transaction) {
323 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, 324 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
324 root->fs_info->running_transaction->transid); 325 root->fs_info->running_transaction->transid);
325 WARN_ON(1); 326 WARN_ON(1);
326 } 327 }
327 if (trans->transid != root->fs_info->generation) { 328 if (trans->transid != root->fs_info->generation) {
328 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, 329 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
329 root->fs_info->generation); 330 root->fs_info->generation);
330 WARN_ON(1); 331 WARN_ON(1);
331 } 332 }
332 333
333 parent_nritems = btrfs_header_nritems(parent); 334 parent_nritems = btrfs_header_nritems(parent);
334 blocksize = btrfs_level_size(root, parent_level - 1); 335 blocksize = btrfs_level_size(root, parent_level - 1);
335 end_slot = parent_nritems; 336 end_slot = parent_nritems;
336 337
337 if (parent_nritems == 1) 338 if (parent_nritems == 1)
338 return 0; 339 return 0;
339 340
340 for (i = start_slot; i < end_slot; i++) { 341 for (i = start_slot; i < end_slot; i++) {
341 int close = 1; 342 int close = 1;
342 343
343 if (!parent->map_token) { 344 if (!parent->map_token) {
344 map_extent_buffer(parent, 345 map_extent_buffer(parent,
345 btrfs_node_key_ptr_offset(i), 346 btrfs_node_key_ptr_offset(i),
346 sizeof(struct btrfs_key_ptr), 347 sizeof(struct btrfs_key_ptr),
347 &parent->map_token, &parent->kaddr, 348 &parent->map_token, &parent->kaddr,
348 &parent->map_start, &parent->map_len, 349 &parent->map_start, &parent->map_len,
349 KM_USER1); 350 KM_USER1);
350 } 351 }
351 btrfs_node_key(parent, &disk_key, i); 352 btrfs_node_key(parent, &disk_key, i);
352 if (!progress_passed && comp_keys(&disk_key, progress) < 0) 353 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
353 continue; 354 continue;
354 355
355 progress_passed = 1; 356 progress_passed = 1;
356 blocknr = btrfs_node_blockptr(parent, i); 357 blocknr = btrfs_node_blockptr(parent, i);
358 gen = btrfs_node_ptr_generation(parent, i);
357 if (last_block == 0) 359 if (last_block == 0)
358 last_block = blocknr; 360 last_block = blocknr;
359 361
360 if (i > 0) { 362 if (i > 0) {
361 other = btrfs_node_blockptr(parent, i - 1); 363 other = btrfs_node_blockptr(parent, i - 1);
362 close = close_blocks(blocknr, other, blocksize); 364 close = close_blocks(blocknr, other, blocksize);
363 } 365 }
364 if (close && i < end_slot - 2) { 366 if (close && i < end_slot - 2) {
365 other = btrfs_node_blockptr(parent, i + 1); 367 other = btrfs_node_blockptr(parent, i + 1);
366 close = close_blocks(blocknr, other, blocksize); 368 close = close_blocks(blocknr, other, blocksize);
367 } 369 }
368 if (close) { 370 if (close) {
369 last_block = blocknr; 371 last_block = blocknr;
370 continue; 372 continue;
371 } 373 }
372 if (parent->map_token) { 374 if (parent->map_token) {
373 unmap_extent_buffer(parent, parent->map_token, 375 unmap_extent_buffer(parent, parent->map_token,
374 KM_USER1); 376 KM_USER1);
375 parent->map_token = NULL; 377 parent->map_token = NULL;
376 } 378 }
377 379
378 cur = btrfs_find_tree_block(root, blocknr, blocksize); 380 cur = btrfs_find_tree_block(root, blocknr, blocksize);
379 if (cur) 381 if (cur)
380 uptodate = btrfs_buffer_uptodate(cur); 382 uptodate = btrfs_buffer_uptodate(cur);
381 else 383 else
382 uptodate = 0; 384 uptodate = 0;
383 if (!cur || !uptodate) { 385 if (!cur || !uptodate) {
384 if (cache_only) { 386 if (cache_only) {
385 free_extent_buffer(cur); 387 free_extent_buffer(cur);
386 continue; 388 continue;
387 } 389 }
388 if (!cur) { 390 if (!cur) {
389 cur = read_tree_block(root, blocknr, 391 cur = read_tree_block(root, blocknr,
390 blocksize); 392 blocksize, gen);
391 } else if (!uptodate) { 393 } else if (!uptodate) {
392 btrfs_read_buffer(cur); 394 btrfs_read_buffer(cur, gen);
393 } 395 }
394 } 396 }
395 if (search_start == 0) 397 if (search_start == 0)
396 search_start = last_block; 398 search_start = last_block;
397 399
398 btrfs_verify_block_csum(root, cur);
399 err = __btrfs_cow_block(trans, root, cur, parent, i, 400 err = __btrfs_cow_block(trans, root, cur, parent, i,
400 &tmp, search_start, 401 &tmp, search_start,
401 min(16 * blocksize, 402 min(16 * blocksize,
402 (end_slot - i) * blocksize)); 403 (end_slot - i) * blocksize));
403 if (err) { 404 if (err) {
404 free_extent_buffer(cur); 405 free_extent_buffer(cur);
405 break; 406 break;
406 } 407 }
407 search_start = tmp->start; 408 search_start = tmp->start;
408 last_block = tmp->start; 409 last_block = tmp->start;
409 *last_ret = search_start; 410 *last_ret = search_start;
410 if (parent_level == 1) 411 if (parent_level == 1)
411 btrfs_clear_buffer_defrag(tmp); 412 btrfs_clear_buffer_defrag(tmp);
412 free_extent_buffer(tmp); 413 free_extent_buffer(tmp);
413 } 414 }
414 if (parent->map_token) { 415 if (parent->map_token) {
415 unmap_extent_buffer(parent, parent->map_token, 416 unmap_extent_buffer(parent, parent->map_token,
416 KM_USER1); 417 KM_USER1);
417 parent->map_token = NULL; 418 parent->map_token = NULL;
418 } 419 }
419 return err; 420 return err;
420 } 421 }
421 422
422 /* 423 /*
423 * The leaf data grows from end-to-front in the node. 424 * The leaf data grows from end-to-front in the node.
424 * this returns the address of the start of the last item, 425 * this returns the address of the start of the last item,
425 * which is the stop of the leaf data stack 426 * which is the stop of the leaf data stack
426 */ 427 */
427 static inline unsigned int leaf_data_end(struct btrfs_root *root, 428 static inline unsigned int leaf_data_end(struct btrfs_root *root,
428 struct extent_buffer *leaf) 429 struct extent_buffer *leaf)
429 { 430 {
430 u32 nr = btrfs_header_nritems(leaf); 431 u32 nr = btrfs_header_nritems(leaf);
431 if (nr == 0) 432 if (nr == 0)
432 return BTRFS_LEAF_DATA_SIZE(root); 433 return BTRFS_LEAF_DATA_SIZE(root);
433 return btrfs_item_offset_nr(leaf, nr - 1); 434 return btrfs_item_offset_nr(leaf, nr - 1);
434 } 435 }
435 436
436 static int check_node(struct btrfs_root *root, struct btrfs_path *path, 437 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
437 int level) 438 int level)
438 { 439 {
439 struct extent_buffer *parent = NULL; 440 struct extent_buffer *parent = NULL;
440 struct extent_buffer *node = path->nodes[level]; 441 struct extent_buffer *node = path->nodes[level];
441 struct btrfs_disk_key parent_key; 442 struct btrfs_disk_key parent_key;
442 struct btrfs_disk_key node_key; 443 struct btrfs_disk_key node_key;
443 int parent_slot; 444 int parent_slot;
444 int slot; 445 int slot;
445 struct btrfs_key cpukey; 446 struct btrfs_key cpukey;
446 u32 nritems = btrfs_header_nritems(node); 447 u32 nritems = btrfs_header_nritems(node);
447 448
448 if (path->nodes[level + 1]) 449 if (path->nodes[level + 1])
449 parent = path->nodes[level + 1]; 450 parent = path->nodes[level + 1];
450 451
451 slot = path->slots[level]; 452 slot = path->slots[level];
452 BUG_ON(nritems == 0); 453 BUG_ON(nritems == 0);
453 if (parent) { 454 if (parent) {
454 parent_slot = path->slots[level + 1]; 455 parent_slot = path->slots[level + 1];
455 btrfs_node_key(parent, &parent_key, parent_slot); 456 btrfs_node_key(parent, &parent_key, parent_slot);
456 btrfs_node_key(node, &node_key, 0); 457 btrfs_node_key(node, &node_key, 0);
457 BUG_ON(memcmp(&parent_key, &node_key, 458 BUG_ON(memcmp(&parent_key, &node_key,
458 sizeof(struct btrfs_disk_key))); 459 sizeof(struct btrfs_disk_key)));
459 BUG_ON(btrfs_node_blockptr(parent, parent_slot) != 460 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
460 btrfs_header_bytenr(node)); 461 btrfs_header_bytenr(node));
461 } 462 }
462 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root)); 463 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
463 if (slot != 0) { 464 if (slot != 0) {
464 btrfs_node_key_to_cpu(node, &cpukey, slot - 1); 465 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
465 btrfs_node_key(node, &node_key, slot); 466 btrfs_node_key(node, &node_key, slot);
466 BUG_ON(comp_keys(&node_key, &cpukey) <= 0); 467 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
467 } 468 }
468 if (slot < nritems - 1) { 469 if (slot < nritems - 1) {
469 btrfs_node_key_to_cpu(node, &cpukey, slot + 1); 470 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
470 btrfs_node_key(node, &node_key, slot); 471 btrfs_node_key(node, &node_key, slot);
471 BUG_ON(comp_keys(&node_key, &cpukey) >= 0); 472 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
472 } 473 }
473 return 0; 474 return 0;
474 } 475 }
475 476
476 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path, 477 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
477 int level) 478 int level)
478 { 479 {
479 struct extent_buffer *leaf = path->nodes[level]; 480 struct extent_buffer *leaf = path->nodes[level];
480 struct extent_buffer *parent = NULL; 481 struct extent_buffer *parent = NULL;
481 int parent_slot; 482 int parent_slot;
482 struct btrfs_key cpukey; 483 struct btrfs_key cpukey;
483 struct btrfs_disk_key parent_key; 484 struct btrfs_disk_key parent_key;
484 struct btrfs_disk_key leaf_key; 485 struct btrfs_disk_key leaf_key;
485 int slot = path->slots[0]; 486 int slot = path->slots[0];
486 487
487 u32 nritems = btrfs_header_nritems(leaf); 488 u32 nritems = btrfs_header_nritems(leaf);
488 489
489 if (path->nodes[level + 1]) 490 if (path->nodes[level + 1])
490 parent = path->nodes[level + 1]; 491 parent = path->nodes[level + 1];
491 492
492 if (nritems == 0) 493 if (nritems == 0)
493 return 0; 494 return 0;
494 495
495 if (parent) { 496 if (parent) {
496 parent_slot = path->slots[level + 1]; 497 parent_slot = path->slots[level + 1];
497 btrfs_node_key(parent, &parent_key, parent_slot); 498 btrfs_node_key(parent, &parent_key, parent_slot);
498 btrfs_item_key(leaf, &leaf_key, 0); 499 btrfs_item_key(leaf, &leaf_key, 0);
499 500
500 BUG_ON(memcmp(&parent_key, &leaf_key, 501 BUG_ON(memcmp(&parent_key, &leaf_key,
501 sizeof(struct btrfs_disk_key))); 502 sizeof(struct btrfs_disk_key)));
502 BUG_ON(btrfs_node_blockptr(parent, parent_slot) != 503 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
503 btrfs_header_bytenr(leaf)); 504 btrfs_header_bytenr(leaf));
504 } 505 }
505 #if 0 506 #if 0
506 for (i = 0; nritems > 1 && i < nritems - 2; i++) { 507 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
507 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1); 508 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
508 btrfs_item_key(leaf, &leaf_key, i); 509 btrfs_item_key(leaf, &leaf_key, i);
509 if (comp_keys(&leaf_key, &cpukey) >= 0) { 510 if (comp_keys(&leaf_key, &cpukey) >= 0) {
510 btrfs_print_leaf(root, leaf); 511 btrfs_print_leaf(root, leaf);
511 printk("slot %d offset bad key\n", i); 512 printk("slot %d offset bad key\n", i);
512 BUG_ON(1); 513 BUG_ON(1);
513 } 514 }
514 if (btrfs_item_offset_nr(leaf, i) != 515 if (btrfs_item_offset_nr(leaf, i) !=
515 btrfs_item_end_nr(leaf, i + 1)) { 516 btrfs_item_end_nr(leaf, i + 1)) {
516 btrfs_print_leaf(root, leaf); 517 btrfs_print_leaf(root, leaf);
517 printk("slot %d offset bad\n", i); 518 printk("slot %d offset bad\n", i);
518 BUG_ON(1); 519 BUG_ON(1);
519 } 520 }
520 if (i == 0) { 521 if (i == 0) {
521 if (btrfs_item_offset_nr(leaf, i) + 522 if (btrfs_item_offset_nr(leaf, i) +
522 btrfs_item_size_nr(leaf, i) != 523 btrfs_item_size_nr(leaf, i) !=
523 BTRFS_LEAF_DATA_SIZE(root)) { 524 BTRFS_LEAF_DATA_SIZE(root)) {
524 btrfs_print_leaf(root, leaf); 525 btrfs_print_leaf(root, leaf);
525 printk("slot %d first offset bad\n", i); 526 printk("slot %d first offset bad\n", i);
526 BUG_ON(1); 527 BUG_ON(1);
527 } 528 }
528 } 529 }
529 } 530 }
530 if (nritems > 0) { 531 if (nritems > 0) {
531 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) { 532 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
532 btrfs_print_leaf(root, leaf); 533 btrfs_print_leaf(root, leaf);
533 printk("slot %d bad size \n", nritems - 1); 534 printk("slot %d bad size \n", nritems - 1);
534 BUG_ON(1); 535 BUG_ON(1);
535 } 536 }
536 } 537 }
537 #endif 538 #endif
538 if (slot != 0 && slot < nritems - 1) { 539 if (slot != 0 && slot < nritems - 1) {
539 btrfs_item_key(leaf, &leaf_key, slot); 540 btrfs_item_key(leaf, &leaf_key, slot);
540 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1); 541 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
541 if (comp_keys(&leaf_key, &cpukey) <= 0) { 542 if (comp_keys(&leaf_key, &cpukey) <= 0) {
542 btrfs_print_leaf(root, leaf); 543 btrfs_print_leaf(root, leaf);
543 printk("slot %d offset bad key\n", slot); 544 printk("slot %d offset bad key\n", slot);
544 BUG_ON(1); 545 BUG_ON(1);
545 } 546 }
546 if (btrfs_item_offset_nr(leaf, slot - 1) != 547 if (btrfs_item_offset_nr(leaf, slot - 1) !=
547 btrfs_item_end_nr(leaf, slot)) { 548 btrfs_item_end_nr(leaf, slot)) {
548 btrfs_print_leaf(root, leaf); 549 btrfs_print_leaf(root, leaf);
549 printk("slot %d offset bad\n", slot); 550 printk("slot %d offset bad\n", slot);
550 BUG_ON(1); 551 BUG_ON(1);
551 } 552 }
552 } 553 }
553 if (slot < nritems - 1) { 554 if (slot < nritems - 1) {
554 btrfs_item_key(leaf, &leaf_key, slot); 555 btrfs_item_key(leaf, &leaf_key, slot);
555 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1); 556 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
556 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0); 557 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
557 if (btrfs_item_offset_nr(leaf, slot) != 558 if (btrfs_item_offset_nr(leaf, slot) !=
558 btrfs_item_end_nr(leaf, slot + 1)) { 559 btrfs_item_end_nr(leaf, slot + 1)) {
559 btrfs_print_leaf(root, leaf); 560 btrfs_print_leaf(root, leaf);
560 printk("slot %d offset bad\n", slot); 561 printk("slot %d offset bad\n", slot);
561 BUG_ON(1); 562 BUG_ON(1);
562 } 563 }
563 } 564 }
564 BUG_ON(btrfs_item_offset_nr(leaf, 0) + 565 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
565 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root)); 566 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
566 return 0; 567 return 0;
567 } 568 }
568 569
569 static int noinline check_block(struct btrfs_root *root, 570 static int noinline check_block(struct btrfs_root *root,
570 struct btrfs_path *path, int level) 571 struct btrfs_path *path, int level)
571 { 572 {
572 u64 found_start; 573 u64 found_start;
573 return 0; 574 return 0;
574 if (btrfs_header_level(path->nodes[level]) != level) 575 if (btrfs_header_level(path->nodes[level]) != level)
575 printk("warning: bad level %Lu wanted %d found %d\n", 576 printk("warning: bad level %Lu wanted %d found %d\n",
576 path->nodes[level]->start, level, 577 path->nodes[level]->start, level,
577 btrfs_header_level(path->nodes[level])); 578 btrfs_header_level(path->nodes[level]));
578 found_start = btrfs_header_bytenr(path->nodes[level]); 579 found_start = btrfs_header_bytenr(path->nodes[level]);
579 if (found_start != path->nodes[level]->start) { 580 if (found_start != path->nodes[level]->start) {
580 printk("warning: bad bytentr %Lu found %Lu\n", 581 printk("warning: bad bytentr %Lu found %Lu\n",
581 path->nodes[level]->start, found_start); 582 path->nodes[level]->start, found_start);
582 } 583 }
583 #if 0 584 #if 0
584 struct extent_buffer *buf = path->nodes[level]; 585 struct extent_buffer *buf = path->nodes[level];
585 586
586 if (memcmp_extent_buffer(buf, root->fs_info->fsid, 587 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
587 (unsigned long)btrfs_header_fsid(buf), 588 (unsigned long)btrfs_header_fsid(buf),
588 BTRFS_FSID_SIZE)) { 589 BTRFS_FSID_SIZE)) {
589 printk("warning bad block %Lu\n", buf->start); 590 printk("warning bad block %Lu\n", buf->start);
590 return 1; 591 return 1;
591 } 592 }
592 #endif 593 #endif
593 if (level == 0) 594 if (level == 0)
594 return check_leaf(root, path, level); 595 return check_leaf(root, path, level);
595 return check_node(root, path, level); 596 return check_node(root, path, level);
596 } 597 }
597 598
598 /* 599 /*
599 * search for key in the extent_buffer. The items start at offset p, 600 * search for key in the extent_buffer. The items start at offset p,
600 * and they are item_size apart. There are 'max' items in p. 601 * and they are item_size apart. There are 'max' items in p.
601 * 602 *
602 * the slot in the array is returned via slot, and it points to 603 * the slot in the array is returned via slot, and it points to
603 * the place where you would insert key if it is not found in 604 * the place where you would insert key if it is not found in
604 * the array. 605 * the array.
605 * 606 *
606 * slot may point to max if the key is bigger than all of the keys 607 * slot may point to max if the key is bigger than all of the keys
607 */ 608 */
608 static int generic_bin_search(struct extent_buffer *eb, unsigned long p, 609 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
609 int item_size, struct btrfs_key *key, 610 int item_size, struct btrfs_key *key,
610 int max, int *slot) 611 int max, int *slot)
611 { 612 {
612 int low = 0; 613 int low = 0;
613 int high = max; 614 int high = max;
614 int mid; 615 int mid;
615 int ret; 616 int ret;
616 struct btrfs_disk_key *tmp = NULL; 617 struct btrfs_disk_key *tmp = NULL;
617 struct btrfs_disk_key unaligned; 618 struct btrfs_disk_key unaligned;
618 unsigned long offset; 619 unsigned long offset;
619 char *map_token = NULL; 620 char *map_token = NULL;
620 char *kaddr = NULL; 621 char *kaddr = NULL;
621 unsigned long map_start = 0; 622 unsigned long map_start = 0;
622 unsigned long map_len = 0; 623 unsigned long map_len = 0;
623 int err; 624 int err;
624 625
625 while(low < high) { 626 while(low < high) {
626 mid = (low + high) / 2; 627 mid = (low + high) / 2;
627 offset = p + mid * item_size; 628 offset = p + mid * item_size;
628 629
629 if (!map_token || offset < map_start || 630 if (!map_token || offset < map_start ||
630 (offset + sizeof(struct btrfs_disk_key)) > 631 (offset + sizeof(struct btrfs_disk_key)) >
631 map_start + map_len) { 632 map_start + map_len) {
632 if (map_token) { 633 if (map_token) {
633 unmap_extent_buffer(eb, map_token, KM_USER0); 634 unmap_extent_buffer(eb, map_token, KM_USER0);
634 map_token = NULL; 635 map_token = NULL;
635 } 636 }
636 err = map_extent_buffer(eb, offset, 637 err = map_extent_buffer(eb, offset,
637 sizeof(struct btrfs_disk_key), 638 sizeof(struct btrfs_disk_key),
638 &map_token, &kaddr, 639 &map_token, &kaddr,
639 &map_start, &map_len, KM_USER0); 640 &map_start, &map_len, KM_USER0);
640 641
641 if (!err) { 642 if (!err) {
642 tmp = (struct btrfs_disk_key *)(kaddr + offset - 643 tmp = (struct btrfs_disk_key *)(kaddr + offset -
643 map_start); 644 map_start);
644 } else { 645 } else {
645 read_extent_buffer(eb, &unaligned, 646 read_extent_buffer(eb, &unaligned,
646 offset, sizeof(unaligned)); 647 offset, sizeof(unaligned));
647 tmp = &unaligned; 648 tmp = &unaligned;
648 } 649 }
649 650
650 } else { 651 } else {
651 tmp = (struct btrfs_disk_key *)(kaddr + offset - 652 tmp = (struct btrfs_disk_key *)(kaddr + offset -
652 map_start); 653 map_start);
653 } 654 }
654 ret = comp_keys(tmp, key); 655 ret = comp_keys(tmp, key);
655 656
656 if (ret < 0) 657 if (ret < 0)
657 low = mid + 1; 658 low = mid + 1;
658 else if (ret > 0) 659 else if (ret > 0)
659 high = mid; 660 high = mid;
660 else { 661 else {
661 *slot = mid; 662 *slot = mid;
662 if (map_token) 663 if (map_token)
663 unmap_extent_buffer(eb, map_token, KM_USER0); 664 unmap_extent_buffer(eb, map_token, KM_USER0);
664 return 0; 665 return 0;
665 } 666 }
666 } 667 }
667 *slot = low; 668 *slot = low;
668 if (map_token) 669 if (map_token)
669 unmap_extent_buffer(eb, map_token, KM_USER0); 670 unmap_extent_buffer(eb, map_token, KM_USER0);
670 return 1; 671 return 1;
671 } 672 }
672 673
673 /* 674 /*
674 * simple bin_search frontend that does the right thing for 675 * simple bin_search frontend that does the right thing for
675 * leaves vs nodes 676 * leaves vs nodes
676 */ 677 */
677 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key, 678 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
678 int level, int *slot) 679 int level, int *slot)
679 { 680 {
680 if (level == 0) { 681 if (level == 0) {
681 return generic_bin_search(eb, 682 return generic_bin_search(eb,
682 offsetof(struct btrfs_leaf, items), 683 offsetof(struct btrfs_leaf, items),
683 sizeof(struct btrfs_item), 684 sizeof(struct btrfs_item),
684 key, btrfs_header_nritems(eb), 685 key, btrfs_header_nritems(eb),
685 slot); 686 slot);
686 } else { 687 } else {
687 return generic_bin_search(eb, 688 return generic_bin_search(eb,
688 offsetof(struct btrfs_node, ptrs), 689 offsetof(struct btrfs_node, ptrs),
689 sizeof(struct btrfs_key_ptr), 690 sizeof(struct btrfs_key_ptr),
690 key, btrfs_header_nritems(eb), 691 key, btrfs_header_nritems(eb),
691 slot); 692 slot);
692 } 693 }
693 return -1; 694 return -1;
694 } 695 }
695 696
696 static struct extent_buffer *read_node_slot(struct btrfs_root *root, 697 static struct extent_buffer *read_node_slot(struct btrfs_root *root,
697 struct extent_buffer *parent, int slot) 698 struct extent_buffer *parent, int slot)
698 { 699 {
700 int level = btrfs_header_level(parent);
699 if (slot < 0) 701 if (slot < 0)
700 return NULL; 702 return NULL;
701 if (slot >= btrfs_header_nritems(parent)) 703 if (slot >= btrfs_header_nritems(parent))
702 return NULL; 704 return NULL;
705
706 BUG_ON(level == 0);
707
703 return read_tree_block(root, btrfs_node_blockptr(parent, slot), 708 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
704 btrfs_level_size(root, btrfs_header_level(parent) - 1)); 709 btrfs_level_size(root, level - 1),
710 btrfs_node_ptr_generation(parent, slot));
705 } 711 }
706 712
707 static int balance_level(struct btrfs_trans_handle *trans, 713 static int balance_level(struct btrfs_trans_handle *trans,
708 struct btrfs_root *root, 714 struct btrfs_root *root,
709 struct btrfs_path *path, int level) 715 struct btrfs_path *path, int level)
710 { 716 {
711 struct extent_buffer *right = NULL; 717 struct extent_buffer *right = NULL;
712 struct extent_buffer *mid; 718 struct extent_buffer *mid;
713 struct extent_buffer *left = NULL; 719 struct extent_buffer *left = NULL;
714 struct extent_buffer *parent = NULL; 720 struct extent_buffer *parent = NULL;
715 int ret = 0; 721 int ret = 0;
716 int wret; 722 int wret;
717 int pslot; 723 int pslot;
718 int orig_slot = path->slots[level]; 724 int orig_slot = path->slots[level];
719 int err_on_enospc = 0; 725 int err_on_enospc = 0;
720 u64 orig_ptr; 726 u64 orig_ptr;
721 727
722 if (level == 0) 728 if (level == 0)
723 return 0; 729 return 0;
724 730
725 mid = path->nodes[level]; 731 mid = path->nodes[level];
726 WARN_ON(btrfs_header_generation(mid) != trans->transid); 732 WARN_ON(btrfs_header_generation(mid) != trans->transid);
727 733
728 orig_ptr = btrfs_node_blockptr(mid, orig_slot); 734 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
729 735
730 if (level < BTRFS_MAX_LEVEL - 1) 736 if (level < BTRFS_MAX_LEVEL - 1)
731 parent = path->nodes[level + 1]; 737 parent = path->nodes[level + 1];
732 pslot = path->slots[level + 1]; 738 pslot = path->slots[level + 1];
733 739
734 /* 740 /*
735 * deal with the case where there is only one pointer in the root 741 * deal with the case where there is only one pointer in the root
736 * by promoting the node below to a root 742 * by promoting the node below to a root
737 */ 743 */
738 if (!parent) { 744 if (!parent) {
739 struct extent_buffer *child; 745 struct extent_buffer *child;
740 746
741 if (btrfs_header_nritems(mid) != 1) 747 if (btrfs_header_nritems(mid) != 1)
742 return 0; 748 return 0;
743 749
744 /* promote the child to a root */ 750 /* promote the child to a root */
745 child = read_node_slot(root, mid, 0); 751 child = read_node_slot(root, mid, 0);
746 BUG_ON(!child); 752 BUG_ON(!child);
747 ret = btrfs_cow_block(trans, root, child, mid, 0, &child); 753 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
748 BUG_ON(ret); 754 BUG_ON(ret);
749 755
750 root->node = child; 756 root->node = child;
751 add_root_to_dirty_list(root); 757 add_root_to_dirty_list(root);
752 path->nodes[level] = NULL; 758 path->nodes[level] = NULL;
753 clean_tree_block(trans, root, mid); 759 clean_tree_block(trans, root, mid);
754 /* once for the path */ 760 /* once for the path */
755 free_extent_buffer(mid); 761 free_extent_buffer(mid);
756 ret = btrfs_free_extent(trans, root, mid->start, mid->len, 762 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
757 root->root_key.objectid, 763 root->root_key.objectid,
758 btrfs_header_generation(mid), 0, 0, 1); 764 btrfs_header_generation(mid), 0, 0, 1);
759 /* once for the root ptr */ 765 /* once for the root ptr */
760 free_extent_buffer(mid); 766 free_extent_buffer(mid);
761 return ret; 767 return ret;
762 } 768 }
763 if (btrfs_header_nritems(mid) > 769 if (btrfs_header_nritems(mid) >
764 BTRFS_NODEPTRS_PER_BLOCK(root) / 4) 770 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
765 return 0; 771 return 0;
766 772
767 if (btrfs_header_nritems(mid) < 2) 773 if (btrfs_header_nritems(mid) < 2)
768 err_on_enospc = 1; 774 err_on_enospc = 1;
769 775
770 left = read_node_slot(root, parent, pslot - 1); 776 left = read_node_slot(root, parent, pslot - 1);
771 if (left) { 777 if (left) {
772 wret = btrfs_cow_block(trans, root, left, 778 wret = btrfs_cow_block(trans, root, left,
773 parent, pslot - 1, &left); 779 parent, pslot - 1, &left);
774 if (wret) { 780 if (wret) {
775 ret = wret; 781 ret = wret;
776 goto enospc; 782 goto enospc;
777 } 783 }
778 } 784 }
779 right = read_node_slot(root, parent, pslot + 1); 785 right = read_node_slot(root, parent, pslot + 1);
780 if (right) { 786 if (right) {
781 wret = btrfs_cow_block(trans, root, right, 787 wret = btrfs_cow_block(trans, root, right,
782 parent, pslot + 1, &right); 788 parent, pslot + 1, &right);
783 if (wret) { 789 if (wret) {
784 ret = wret; 790 ret = wret;
785 goto enospc; 791 goto enospc;
786 } 792 }
787 } 793 }
788 794
789 /* first, try to make some room in the middle buffer */ 795 /* first, try to make some room in the middle buffer */
790 if (left) { 796 if (left) {
791 orig_slot += btrfs_header_nritems(left); 797 orig_slot += btrfs_header_nritems(left);
792 wret = push_node_left(trans, root, left, mid, 1); 798 wret = push_node_left(trans, root, left, mid, 1);
793 if (wret < 0) 799 if (wret < 0)
794 ret = wret; 800 ret = wret;
795 if (btrfs_header_nritems(mid) < 2) 801 if (btrfs_header_nritems(mid) < 2)
796 err_on_enospc = 1; 802 err_on_enospc = 1;
797 } 803 }
798 804
799 /* 805 /*
800 * then try to empty the right most buffer into the middle 806 * then try to empty the right most buffer into the middle
801 */ 807 */
802 if (right) { 808 if (right) {
803 wret = push_node_left(trans, root, mid, right, 1); 809 wret = push_node_left(trans, root, mid, right, 1);
804 if (wret < 0 && wret != -ENOSPC) 810 if (wret < 0 && wret != -ENOSPC)
805 ret = wret; 811 ret = wret;
806 if (btrfs_header_nritems(right) == 0) { 812 if (btrfs_header_nritems(right) == 0) {
807 u64 bytenr = right->start; 813 u64 bytenr = right->start;
808 u64 generation = btrfs_header_generation(parent); 814 u64 generation = btrfs_header_generation(parent);
809 u32 blocksize = right->len; 815 u32 blocksize = right->len;
810 816
811 clean_tree_block(trans, root, right); 817 clean_tree_block(trans, root, right);
812 free_extent_buffer(right); 818 free_extent_buffer(right);
813 right = NULL; 819 right = NULL;
814 wret = del_ptr(trans, root, path, level + 1, pslot + 820 wret = del_ptr(trans, root, path, level + 1, pslot +
815 1); 821 1);
816 if (wret) 822 if (wret)
817 ret = wret; 823 ret = wret;
818 wret = btrfs_free_extent(trans, root, bytenr, 824 wret = btrfs_free_extent(trans, root, bytenr,
819 blocksize, 825 blocksize,
820 btrfs_header_owner(parent), 826 btrfs_header_owner(parent),
821 generation, 0, 0, 1); 827 generation, 0, 0, 1);
822 if (wret) 828 if (wret)
823 ret = wret; 829 ret = wret;
824 } else { 830 } else {
825 struct btrfs_disk_key right_key; 831 struct btrfs_disk_key right_key;
826 btrfs_node_key(right, &right_key, 0); 832 btrfs_node_key(right, &right_key, 0);
827 btrfs_set_node_key(parent, &right_key, pslot + 1); 833 btrfs_set_node_key(parent, &right_key, pslot + 1);
828 btrfs_mark_buffer_dirty(parent); 834 btrfs_mark_buffer_dirty(parent);
829 } 835 }
830 } 836 }
831 if (btrfs_header_nritems(mid) == 1) { 837 if (btrfs_header_nritems(mid) == 1) {
832 /* 838 /*
833 * we're not allowed to leave a node with one item in the 839 * we're not allowed to leave a node with one item in the
834 * tree during a delete. A deletion from lower in the tree 840 * tree during a delete. A deletion from lower in the tree
835 * could try to delete the only pointer in this node. 841 * could try to delete the only pointer in this node.
836 * So, pull some keys from the left. 842 * So, pull some keys from the left.
837 * There has to be a left pointer at this point because 843 * There has to be a left pointer at this point because
838 * otherwise we would have pulled some pointers from the 844 * otherwise we would have pulled some pointers from the
839 * right 845 * right
840 */ 846 */
841 BUG_ON(!left); 847 BUG_ON(!left);
842 wret = balance_node_right(trans, root, mid, left); 848 wret = balance_node_right(trans, root, mid, left);
843 if (wret < 0) { 849 if (wret < 0) {
844 ret = wret; 850 ret = wret;
845 goto enospc; 851 goto enospc;
846 } 852 }
847 if (wret == 1) { 853 if (wret == 1) {
848 wret = push_node_left(trans, root, left, mid, 1); 854 wret = push_node_left(trans, root, left, mid, 1);
849 if (wret < 0) 855 if (wret < 0)
850 ret = wret; 856 ret = wret;
851 } 857 }
852 BUG_ON(wret == 1); 858 BUG_ON(wret == 1);
853 } 859 }
854 if (btrfs_header_nritems(mid) == 0) { 860 if (btrfs_header_nritems(mid) == 0) {
855 /* we've managed to empty the middle node, drop it */ 861 /* we've managed to empty the middle node, drop it */
856 u64 root_gen = btrfs_header_generation(parent); 862 u64 root_gen = btrfs_header_generation(parent);
857 u64 bytenr = mid->start; 863 u64 bytenr = mid->start;
858 u32 blocksize = mid->len; 864 u32 blocksize = mid->len;
859 clean_tree_block(trans, root, mid); 865 clean_tree_block(trans, root, mid);
860 free_extent_buffer(mid); 866 free_extent_buffer(mid);
861 mid = NULL; 867 mid = NULL;
862 wret = del_ptr(trans, root, path, level + 1, pslot); 868 wret = del_ptr(trans, root, path, level + 1, pslot);
863 if (wret) 869 if (wret)
864 ret = wret; 870 ret = wret;
865 wret = btrfs_free_extent(trans, root, bytenr, blocksize, 871 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
866 btrfs_header_owner(parent), 872 btrfs_header_owner(parent),
867 root_gen, 0, 0, 1); 873 root_gen, 0, 0, 1);
868 if (wret) 874 if (wret)
869 ret = wret; 875 ret = wret;
870 } else { 876 } else {
871 /* update the parent key to reflect our changes */ 877 /* update the parent key to reflect our changes */
872 struct btrfs_disk_key mid_key; 878 struct btrfs_disk_key mid_key;
873 btrfs_node_key(mid, &mid_key, 0); 879 btrfs_node_key(mid, &mid_key, 0);
874 btrfs_set_node_key(parent, &mid_key, pslot); 880 btrfs_set_node_key(parent, &mid_key, pslot);
875 btrfs_mark_buffer_dirty(parent); 881 btrfs_mark_buffer_dirty(parent);
876 } 882 }
877 883
878 /* update the path */ 884 /* update the path */
879 if (left) { 885 if (left) {
880 if (btrfs_header_nritems(left) > orig_slot) { 886 if (btrfs_header_nritems(left) > orig_slot) {
881 extent_buffer_get(left); 887 extent_buffer_get(left);
882 path->nodes[level] = left; 888 path->nodes[level] = left;
883 path->slots[level + 1] -= 1; 889 path->slots[level + 1] -= 1;
884 path->slots[level] = orig_slot; 890 path->slots[level] = orig_slot;
885 if (mid) 891 if (mid)
886 free_extent_buffer(mid); 892 free_extent_buffer(mid);
887 } else { 893 } else {
888 orig_slot -= btrfs_header_nritems(left); 894 orig_slot -= btrfs_header_nritems(left);
889 path->slots[level] = orig_slot; 895 path->slots[level] = orig_slot;
890 } 896 }
891 } 897 }
892 /* double check we haven't messed things up */ 898 /* double check we haven't messed things up */
893 check_block(root, path, level); 899 check_block(root, path, level);
894 if (orig_ptr != 900 if (orig_ptr !=
895 btrfs_node_blockptr(path->nodes[level], path->slots[level])) 901 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
896 BUG(); 902 BUG();
897 enospc: 903 enospc:
898 if (right) 904 if (right)
899 free_extent_buffer(right); 905 free_extent_buffer(right);
900 if (left) 906 if (left)
901 free_extent_buffer(left); 907 free_extent_buffer(left);
902 return ret; 908 return ret;
903 } 909 }
904 910
905 /* returns zero if the push worked, non-zero otherwise */ 911 /* returns zero if the push worked, non-zero otherwise */
906 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans, 912 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
907 struct btrfs_root *root, 913 struct btrfs_root *root,
908 struct btrfs_path *path, int level) 914 struct btrfs_path *path, int level)
909 { 915 {
910 struct extent_buffer *right = NULL; 916 struct extent_buffer *right = NULL;
911 struct extent_buffer *mid; 917 struct extent_buffer *mid;
912 struct extent_buffer *left = NULL; 918 struct extent_buffer *left = NULL;
913 struct extent_buffer *parent = NULL; 919 struct extent_buffer *parent = NULL;
914 int ret = 0; 920 int ret = 0;
915 int wret; 921 int wret;
916 int pslot; 922 int pslot;
917 int orig_slot = path->slots[level]; 923 int orig_slot = path->slots[level];
918 u64 orig_ptr; 924 u64 orig_ptr;
919 925
920 if (level == 0) 926 if (level == 0)
921 return 1; 927 return 1;
922 928
923 mid = path->nodes[level]; 929 mid = path->nodes[level];
924 WARN_ON(btrfs_header_generation(mid) != trans->transid); 930 WARN_ON(btrfs_header_generation(mid) != trans->transid);
925 orig_ptr = btrfs_node_blockptr(mid, orig_slot); 931 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
926 932
927 if (level < BTRFS_MAX_LEVEL - 1) 933 if (level < BTRFS_MAX_LEVEL - 1)
928 parent = path->nodes[level + 1]; 934 parent = path->nodes[level + 1];
929 pslot = path->slots[level + 1]; 935 pslot = path->slots[level + 1];
930 936
931 if (!parent) 937 if (!parent)
932 return 1; 938 return 1;
933 939
934 left = read_node_slot(root, parent, pslot - 1); 940 left = read_node_slot(root, parent, pslot - 1);
935 941
936 /* first, try to make some room in the middle buffer */ 942 /* first, try to make some room in the middle buffer */
937 if (left) { 943 if (left) {
938 u32 left_nr; 944 u32 left_nr;
939 left_nr = btrfs_header_nritems(left); 945 left_nr = btrfs_header_nritems(left);
940 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 946 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
941 wret = 1; 947 wret = 1;
942 } else { 948 } else {
943 ret = btrfs_cow_block(trans, root, left, parent, 949 ret = btrfs_cow_block(trans, root, left, parent,
944 pslot - 1, &left); 950 pslot - 1, &left);
945 if (ret) 951 if (ret)
946 wret = 1; 952 wret = 1;
947 else { 953 else {
948 wret = push_node_left(trans, root, 954 wret = push_node_left(trans, root,
949 left, mid, 0); 955 left, mid, 0);
950 } 956 }
951 } 957 }
952 if (wret < 0) 958 if (wret < 0)
953 ret = wret; 959 ret = wret;
954 if (wret == 0) { 960 if (wret == 0) {
955 struct btrfs_disk_key disk_key; 961 struct btrfs_disk_key disk_key;
956 orig_slot += left_nr; 962 orig_slot += left_nr;
957 btrfs_node_key(mid, &disk_key, 0); 963 btrfs_node_key(mid, &disk_key, 0);
958 btrfs_set_node_key(parent, &disk_key, pslot); 964 btrfs_set_node_key(parent, &disk_key, pslot);
959 btrfs_mark_buffer_dirty(parent); 965 btrfs_mark_buffer_dirty(parent);
960 if (btrfs_header_nritems(left) > orig_slot) { 966 if (btrfs_header_nritems(left) > orig_slot) {
961 path->nodes[level] = left; 967 path->nodes[level] = left;
962 path->slots[level + 1] -= 1; 968 path->slots[level + 1] -= 1;
963 path->slots[level] = orig_slot; 969 path->slots[level] = orig_slot;
964 free_extent_buffer(mid); 970 free_extent_buffer(mid);
965 } else { 971 } else {
966 orig_slot -= 972 orig_slot -=
967 btrfs_header_nritems(left); 973 btrfs_header_nritems(left);
968 path->slots[level] = orig_slot; 974 path->slots[level] = orig_slot;
969 free_extent_buffer(left); 975 free_extent_buffer(left);
970 } 976 }
971 return 0; 977 return 0;
972 } 978 }
973 free_extent_buffer(left); 979 free_extent_buffer(left);
974 } 980 }
975 right= read_node_slot(root, parent, pslot + 1); 981 right= read_node_slot(root, parent, pslot + 1);
976 982
977 /* 983 /*
978 * then try to empty the right most buffer into the middle 984 * then try to empty the right most buffer into the middle
979 */ 985 */
980 if (right) { 986 if (right) {
981 u32 right_nr; 987 u32 right_nr;
982 right_nr = btrfs_header_nritems(right); 988 right_nr = btrfs_header_nritems(right);
983 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 989 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
984 wret = 1; 990 wret = 1;
985 } else { 991 } else {
986 ret = btrfs_cow_block(trans, root, right, 992 ret = btrfs_cow_block(trans, root, right,
987 parent, pslot + 1, 993 parent, pslot + 1,
988 &right); 994 &right);
989 if (ret) 995 if (ret)
990 wret = 1; 996 wret = 1;
991 else { 997 else {
992 wret = balance_node_right(trans, root, 998 wret = balance_node_right(trans, root,
993 right, mid); 999 right, mid);
994 } 1000 }
995 } 1001 }
996 if (wret < 0) 1002 if (wret < 0)
997 ret = wret; 1003 ret = wret;
998 if (wret == 0) { 1004 if (wret == 0) {
999 struct btrfs_disk_key disk_key; 1005 struct btrfs_disk_key disk_key;
1000 1006
1001 btrfs_node_key(right, &disk_key, 0); 1007 btrfs_node_key(right, &disk_key, 0);
1002 btrfs_set_node_key(parent, &disk_key, pslot + 1); 1008 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1003 btrfs_mark_buffer_dirty(parent); 1009 btrfs_mark_buffer_dirty(parent);
1004 1010
1005 if (btrfs_header_nritems(mid) <= orig_slot) { 1011 if (btrfs_header_nritems(mid) <= orig_slot) {
1006 path->nodes[level] = right; 1012 path->nodes[level] = right;
1007 path->slots[level + 1] += 1; 1013 path->slots[level + 1] += 1;
1008 path->slots[level] = orig_slot - 1014 path->slots[level] = orig_slot -
1009 btrfs_header_nritems(mid); 1015 btrfs_header_nritems(mid);
1010 free_extent_buffer(mid); 1016 free_extent_buffer(mid);
1011 } else { 1017 } else {
1012 free_extent_buffer(right); 1018 free_extent_buffer(right);
1013 } 1019 }
1014 return 0; 1020 return 0;
1015 } 1021 }
1016 free_extent_buffer(right); 1022 free_extent_buffer(right);
1017 } 1023 }
1018 return 1; 1024 return 1;
1019 } 1025 }
1020 1026
1021 /* 1027 /*
1022 * readahead one full node of leaves 1028 * readahead one full node of leaves
1023 */ 1029 */
1024 static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path, 1030 static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
1025 int level, int slot, u64 objectid) 1031 int level, int slot, u64 objectid)
1026 { 1032 {
1027 struct extent_buffer *node; 1033 struct extent_buffer *node;
1028 struct btrfs_disk_key disk_key; 1034 struct btrfs_disk_key disk_key;
1029 u32 nritems; 1035 u32 nritems;
1030 u64 search; 1036 u64 search;
1031 u64 lowest_read; 1037 u64 lowest_read;
1032 u64 highest_read; 1038 u64 highest_read;
1033 u64 nread = 0; 1039 u64 nread = 0;
1034 int direction = path->reada; 1040 int direction = path->reada;
1035 struct extent_buffer *eb; 1041 struct extent_buffer *eb;
1036 u32 nr; 1042 u32 nr;
1037 u32 blocksize; 1043 u32 blocksize;
1038 u32 nscan = 0; 1044 u32 nscan = 0;
1039 1045
1040 if (level != 1) 1046 if (level != 1)
1041 return; 1047 return;
1042 1048
1043 if (!path->nodes[level]) 1049 if (!path->nodes[level])
1044 return; 1050 return;
1045 1051
1046 node = path->nodes[level]; 1052 node = path->nodes[level];
1047 search = btrfs_node_blockptr(node, slot); 1053 search = btrfs_node_blockptr(node, slot);
1048 blocksize = btrfs_level_size(root, level - 1); 1054 blocksize = btrfs_level_size(root, level - 1);
1049 eb = btrfs_find_tree_block(root, search, blocksize); 1055 eb = btrfs_find_tree_block(root, search, blocksize);
1050 if (eb) { 1056 if (eb) {
1051 free_extent_buffer(eb); 1057 free_extent_buffer(eb);
1052 return; 1058 return;
1053 } 1059 }
1054 1060
1055 highest_read = search; 1061 highest_read = search;
1056 lowest_read = search; 1062 lowest_read = search;
1057 1063
1058 nritems = btrfs_header_nritems(node); 1064 nritems = btrfs_header_nritems(node);
1059 nr = slot; 1065 nr = slot;
1060 while(1) { 1066 while(1) {
1061 if (direction < 0) { 1067 if (direction < 0) {
1062 if (nr == 0) 1068 if (nr == 0)
1063 break; 1069 break;
1064 nr--; 1070 nr--;
1065 } else if (direction > 0) { 1071 } else if (direction > 0) {
1066 nr++; 1072 nr++;
1067 if (nr >= nritems) 1073 if (nr >= nritems)
1068 break; 1074 break;
1069 } 1075 }
1070 if (path->reada < 0 && objectid) { 1076 if (path->reada < 0 && objectid) {
1071 btrfs_node_key(node, &disk_key, nr); 1077 btrfs_node_key(node, &disk_key, nr);
1072 if (btrfs_disk_key_objectid(&disk_key) != objectid) 1078 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1073 break; 1079 break;
1074 } 1080 }
1075 search = btrfs_node_blockptr(node, nr); 1081 search = btrfs_node_blockptr(node, nr);
1076 if ((search >= lowest_read && search <= highest_read) || 1082 if ((search >= lowest_read && search <= highest_read) ||
1077 (search < lowest_read && lowest_read - search <= 32768) || 1083 (search < lowest_read && lowest_read - search <= 32768) ||
1078 (search > highest_read && search - highest_read <= 32768)) { 1084 (search > highest_read && search - highest_read <= 32768)) {
1079 readahead_tree_block(root, search, blocksize); 1085 readahead_tree_block(root, search, blocksize,
1086 btrfs_node_ptr_generation(node, nr));
1080 nread += blocksize; 1087 nread += blocksize;
1081 } 1088 }
1082 nscan++; 1089 nscan++;
1083 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32)) 1090 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1084 break; 1091 break;
1085 if(nread > (1024 * 1024) || nscan > 128) 1092 if(nread > (1024 * 1024) || nscan > 128)
1086 break; 1093 break;
1087 1094
1088 if (search < lowest_read) 1095 if (search < lowest_read)
1089 lowest_read = search; 1096 lowest_read = search;
1090 if (search > highest_read) 1097 if (search > highest_read)
1091 highest_read = search; 1098 highest_read = search;
1092 } 1099 }
1093 } 1100 }
1094 /* 1101 /*
1095 * look for key in the tree. path is filled in with nodes along the way 1102 * look for key in the tree. path is filled in with nodes along the way
1096 * if key is found, we return zero and you can find the item in the leaf 1103 * if key is found, we return zero and you can find the item in the leaf
1097 * level of the path (level 0) 1104 * level of the path (level 0)
1098 * 1105 *
1099 * If the key isn't found, the path points to the slot where it should 1106 * If the key isn't found, the path points to the slot where it should
1100 * be inserted, and 1 is returned. If there are other errors during the 1107 * be inserted, and 1 is returned. If there are other errors during the
1101 * search a negative error number is returned. 1108 * search a negative error number is returned.
1102 * 1109 *
1103 * if ins_len > 0, nodes and leaves will be split as we walk down the 1110 * if ins_len > 0, nodes and leaves will be split as we walk down the
1104 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if 1111 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1105 * possible) 1112 * possible)
1106 */ 1113 */
1107 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root 1114 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1108 *root, struct btrfs_key *key, struct btrfs_path *p, int 1115 *root, struct btrfs_key *key, struct btrfs_path *p, int
1109 ins_len, int cow) 1116 ins_len, int cow)
1110 { 1117 {
1111 struct extent_buffer *b; 1118 struct extent_buffer *b;
1112 u64 bytenr;
1113 u64 ptr_gen;
1114 int slot; 1119 int slot;
1115 int ret; 1120 int ret;
1116 int level; 1121 int level;
1117 int should_reada = p->reada; 1122 int should_reada = p->reada;
1118 u8 lowest_level = 0; 1123 u8 lowest_level = 0;
1119 1124
1120 lowest_level = p->lowest_level; 1125 lowest_level = p->lowest_level;
1121 WARN_ON(lowest_level && ins_len); 1126 WARN_ON(lowest_level && ins_len);
1122 WARN_ON(p->nodes[0] != NULL); 1127 WARN_ON(p->nodes[0] != NULL);
1123 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex)); 1128 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1124 again: 1129 again:
1125 b = root->node; 1130 b = root->node;
1126 extent_buffer_get(b); 1131 extent_buffer_get(b);
1127 while (b) { 1132 while (b) {
1128 level = btrfs_header_level(b); 1133 level = btrfs_header_level(b);
1129 if (cow) { 1134 if (cow) {
1130 int wret; 1135 int wret;
1131 wret = btrfs_cow_block(trans, root, b, 1136 wret = btrfs_cow_block(trans, root, b,
1132 p->nodes[level + 1], 1137 p->nodes[level + 1],
1133 p->slots[level + 1], 1138 p->slots[level + 1],
1134 &b); 1139 &b);
1135 if (wret) { 1140 if (wret) {
1136 free_extent_buffer(b); 1141 free_extent_buffer(b);
1137 return wret; 1142 return wret;
1138 } 1143 }
1139 } 1144 }
1140 BUG_ON(!cow && ins_len); 1145 BUG_ON(!cow && ins_len);
1141 if (level != btrfs_header_level(b)) 1146 if (level != btrfs_header_level(b))
1142 WARN_ON(1); 1147 WARN_ON(1);
1143 level = btrfs_header_level(b); 1148 level = btrfs_header_level(b);
1144 p->nodes[level] = b; 1149 p->nodes[level] = b;
1145 ret = check_block(root, p, level); 1150 ret = check_block(root, p, level);
1146 if (ret) 1151 if (ret)
1147 return -1; 1152 return -1;
1148 ret = bin_search(b, key, level, &slot); 1153 ret = bin_search(b, key, level, &slot);
1149 if (level != 0) { 1154 if (level != 0) {
1150 if (ret && slot > 0) 1155 if (ret && slot > 0)
1151 slot -= 1; 1156 slot -= 1;
1152 p->slots[level] = slot; 1157 p->slots[level] = slot;
1153 if (ins_len > 0 && btrfs_header_nritems(b) >= 1158 if (ins_len > 0 && btrfs_header_nritems(b) >=
1154 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) { 1159 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1155 int sret = split_node(trans, root, p, level); 1160 int sret = split_node(trans, root, p, level);
1156 BUG_ON(sret > 0); 1161 BUG_ON(sret > 0);
1157 if (sret) 1162 if (sret)
1158 return sret; 1163 return sret;
1159 b = p->nodes[level]; 1164 b = p->nodes[level];
1160 slot = p->slots[level]; 1165 slot = p->slots[level];
1161 } else if (ins_len < 0) { 1166 } else if (ins_len < 0) {
1162 int sret = balance_level(trans, root, p, 1167 int sret = balance_level(trans, root, p,
1163 level); 1168 level);
1164 if (sret) 1169 if (sret)
1165 return sret; 1170 return sret;
1166 b = p->nodes[level]; 1171 b = p->nodes[level];
1167 if (!b) { 1172 if (!b) {
1168 btrfs_release_path(NULL, p); 1173 btrfs_release_path(NULL, p);
1169 goto again; 1174 goto again;
1170 } 1175 }
1171 slot = p->slots[level]; 1176 slot = p->slots[level];
1172 BUG_ON(btrfs_header_nritems(b) == 1); 1177 BUG_ON(btrfs_header_nritems(b) == 1);
1173 } 1178 }
1174 /* this is only true while dropping a snapshot */ 1179 /* this is only true while dropping a snapshot */
1175 if (level == lowest_level) 1180 if (level == lowest_level)
1176 break; 1181 break;
1177 bytenr = btrfs_node_blockptr(b, slot); 1182
1178 ptr_gen = btrfs_node_ptr_generation(b, slot);
1179 if (should_reada) 1183 if (should_reada)
1180 reada_for_search(root, p, level, slot, 1184 reada_for_search(root, p, level, slot,
1181 key->objectid); 1185 key->objectid);
1182 b = read_tree_block(root, bytenr, 1186
1183 btrfs_level_size(root, level - 1)); 1187 b = read_node_slot(root, b, slot);
1184 if (ptr_gen != btrfs_header_generation(b)) {
1185 printk("block %llu bad gen wanted %llu "
1186 "found %llu\n",
1187 (unsigned long long)b->start,
1188 (unsigned long long)ptr_gen,
1189 (unsigned long long)btrfs_header_generation(b));
1190 }
1191 } else { 1188 } else {
1192 p->slots[level] = slot; 1189 p->slots[level] = slot;
1193 if (ins_len > 0 && btrfs_leaf_free_space(root, b) < 1190 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
1194 sizeof(struct btrfs_item) + ins_len) { 1191 sizeof(struct btrfs_item) + ins_len) {
1195 int sret = split_leaf(trans, root, key, 1192 int sret = split_leaf(trans, root, key,
1196 p, ins_len, ret == 0); 1193 p, ins_len, ret == 0);
1197 BUG_ON(sret > 0); 1194 BUG_ON(sret > 0);
1198 if (sret) 1195 if (sret)
1199 return sret; 1196 return sret;
1200 } 1197 }
1201 return ret; 1198 return ret;
1202 } 1199 }
1203 } 1200 }
1204 return 1; 1201 return 1;
1205 } 1202 }
1206 1203
1207 /* 1204 /*
1208 * adjust the pointers going up the tree, starting at level 1205 * adjust the pointers going up the tree, starting at level
1209 * making sure the right key of each node is points to 'key'. 1206 * making sure the right key of each node is points to 'key'.
1210 * This is used after shifting pointers to the left, so it stops 1207 * This is used after shifting pointers to the left, so it stops
1211 * fixing up pointers when a given leaf/node is not in slot 0 of the 1208 * fixing up pointers when a given leaf/node is not in slot 0 of the
1212 * higher levels 1209 * higher levels
1213 * 1210 *
1214 * If this fails to write a tree block, it returns -1, but continues 1211 * If this fails to write a tree block, it returns -1, but continues
1215 * fixing up the blocks in ram so the tree is consistent. 1212 * fixing up the blocks in ram so the tree is consistent.
1216 */ 1213 */
1217 static int fixup_low_keys(struct btrfs_trans_handle *trans, 1214 static int fixup_low_keys(struct btrfs_trans_handle *trans,
1218 struct btrfs_root *root, struct btrfs_path *path, 1215 struct btrfs_root *root, struct btrfs_path *path,
1219 struct btrfs_disk_key *key, int level) 1216 struct btrfs_disk_key *key, int level)
1220 { 1217 {
1221 int i; 1218 int i;
1222 int ret = 0; 1219 int ret = 0;
1223 struct extent_buffer *t; 1220 struct extent_buffer *t;
1224 1221
1225 for (i = level; i < BTRFS_MAX_LEVEL; i++) { 1222 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1226 int tslot = path->slots[i]; 1223 int tslot = path->slots[i];
1227 if (!path->nodes[i]) 1224 if (!path->nodes[i])
1228 break; 1225 break;
1229 t = path->nodes[i]; 1226 t = path->nodes[i];
1230 btrfs_set_node_key(t, key, tslot); 1227 btrfs_set_node_key(t, key, tslot);
1231 btrfs_mark_buffer_dirty(path->nodes[i]); 1228 btrfs_mark_buffer_dirty(path->nodes[i]);
1232 if (tslot != 0) 1229 if (tslot != 0)
1233 break; 1230 break;
1234 } 1231 }
1235 return ret; 1232 return ret;
1236 } 1233 }
1237 1234
1238 /* 1235 /*
1239 * try to push data from one node into the next node left in the 1236 * try to push data from one node into the next node left in the
1240 * tree. 1237 * tree.
1241 * 1238 *
1242 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible 1239 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1243 * error, and > 0 if there was no room in the left hand block. 1240 * error, and > 0 if there was no room in the left hand block.
1244 */ 1241 */
1245 static int push_node_left(struct btrfs_trans_handle *trans, 1242 static int push_node_left(struct btrfs_trans_handle *trans,
1246 struct btrfs_root *root, struct extent_buffer *dst, 1243 struct btrfs_root *root, struct extent_buffer *dst,
1247 struct extent_buffer *src, int empty) 1244 struct extent_buffer *src, int empty)
1248 { 1245 {
1249 int push_items = 0; 1246 int push_items = 0;
1250 int src_nritems; 1247 int src_nritems;
1251 int dst_nritems; 1248 int dst_nritems;
1252 int ret = 0; 1249 int ret = 0;
1253 1250
1254 src_nritems = btrfs_header_nritems(src); 1251 src_nritems = btrfs_header_nritems(src);
1255 dst_nritems = btrfs_header_nritems(dst); 1252 dst_nritems = btrfs_header_nritems(dst);
1256 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; 1253 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1257 WARN_ON(btrfs_header_generation(src) != trans->transid); 1254 WARN_ON(btrfs_header_generation(src) != trans->transid);
1258 WARN_ON(btrfs_header_generation(dst) != trans->transid); 1255 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1259 1256
1260 if (!empty && src_nritems <= 8) 1257 if (!empty && src_nritems <= 8)
1261 return 1; 1258 return 1;
1262 1259
1263 if (push_items <= 0) { 1260 if (push_items <= 0) {
1264 return 1; 1261 return 1;
1265 } 1262 }
1266 1263
1267 if (empty) { 1264 if (empty) {
1268 push_items = min(src_nritems, push_items); 1265 push_items = min(src_nritems, push_items);
1269 if (push_items < src_nritems) { 1266 if (push_items < src_nritems) {
1270 /* leave at least 8 pointers in the node if 1267 /* leave at least 8 pointers in the node if
1271 * we aren't going to empty it 1268 * we aren't going to empty it
1272 */ 1269 */
1273 if (src_nritems - push_items < 8) { 1270 if (src_nritems - push_items < 8) {
1274 if (push_items <= 8) 1271 if (push_items <= 8)
1275 return 1; 1272 return 1;
1276 push_items -= 8; 1273 push_items -= 8;
1277 } 1274 }
1278 } 1275 }
1279 } else 1276 } else
1280 push_items = min(src_nritems - 8, push_items); 1277 push_items = min(src_nritems - 8, push_items);
1281 1278
1282 copy_extent_buffer(dst, src, 1279 copy_extent_buffer(dst, src,
1283 btrfs_node_key_ptr_offset(dst_nritems), 1280 btrfs_node_key_ptr_offset(dst_nritems),
1284 btrfs_node_key_ptr_offset(0), 1281 btrfs_node_key_ptr_offset(0),
1285 push_items * sizeof(struct btrfs_key_ptr)); 1282 push_items * sizeof(struct btrfs_key_ptr));
1286 1283
1287 if (push_items < src_nritems) { 1284 if (push_items < src_nritems) {
1288 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), 1285 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1289 btrfs_node_key_ptr_offset(push_items), 1286 btrfs_node_key_ptr_offset(push_items),
1290 (src_nritems - push_items) * 1287 (src_nritems - push_items) *
1291 sizeof(struct btrfs_key_ptr)); 1288 sizeof(struct btrfs_key_ptr));
1292 } 1289 }
1293 btrfs_set_header_nritems(src, src_nritems - push_items); 1290 btrfs_set_header_nritems(src, src_nritems - push_items);
1294 btrfs_set_header_nritems(dst, dst_nritems + push_items); 1291 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1295 btrfs_mark_buffer_dirty(src); 1292 btrfs_mark_buffer_dirty(src);
1296 btrfs_mark_buffer_dirty(dst); 1293 btrfs_mark_buffer_dirty(dst);
1297 return ret; 1294 return ret;
1298 } 1295 }
1299 1296
1300 /* 1297 /*
1301 * try to push data from one node into the next node right in the 1298 * try to push data from one node into the next node right in the
1302 * tree. 1299 * tree.
1303 * 1300 *
1304 * returns 0 if some ptrs were pushed, < 0 if there was some horrible 1301 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1305 * error, and > 0 if there was no room in the right hand block. 1302 * error, and > 0 if there was no room in the right hand block.
1306 * 1303 *
1307 * this will only push up to 1/2 the contents of the left node over 1304 * this will only push up to 1/2 the contents of the left node over
1308 */ 1305 */
1309 static int balance_node_right(struct btrfs_trans_handle *trans, 1306 static int balance_node_right(struct btrfs_trans_handle *trans,
1310 struct btrfs_root *root, 1307 struct btrfs_root *root,
1311 struct extent_buffer *dst, 1308 struct extent_buffer *dst,
1312 struct extent_buffer *src) 1309 struct extent_buffer *src)
1313 { 1310 {
1314 int push_items = 0; 1311 int push_items = 0;
1315 int max_push; 1312 int max_push;
1316 int src_nritems; 1313 int src_nritems;
1317 int dst_nritems; 1314 int dst_nritems;
1318 int ret = 0; 1315 int ret = 0;
1319 1316
1320 WARN_ON(btrfs_header_generation(src) != trans->transid); 1317 WARN_ON(btrfs_header_generation(src) != trans->transid);
1321 WARN_ON(btrfs_header_generation(dst) != trans->transid); 1318 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1322 1319
1323 src_nritems = btrfs_header_nritems(src); 1320 src_nritems = btrfs_header_nritems(src);
1324 dst_nritems = btrfs_header_nritems(dst); 1321 dst_nritems = btrfs_header_nritems(dst);
1325 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; 1322 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1326 if (push_items <= 0) { 1323 if (push_items <= 0) {
1327 return 1; 1324 return 1;
1328 } 1325 }
1329 1326
1330 if (src_nritems < 4) { 1327 if (src_nritems < 4) {
1331 return 1; 1328 return 1;
1332 } 1329 }
1333 1330
1334 max_push = src_nritems / 2 + 1; 1331 max_push = src_nritems / 2 + 1;
1335 /* don't try to empty the node */ 1332 /* don't try to empty the node */
1336 if (max_push >= src_nritems) { 1333 if (max_push >= src_nritems) {
1337 return 1; 1334 return 1;
1338 } 1335 }
1339 1336
1340 if (max_push < push_items) 1337 if (max_push < push_items)
1341 push_items = max_push; 1338 push_items = max_push;
1342 1339
1343 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), 1340 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1344 btrfs_node_key_ptr_offset(0), 1341 btrfs_node_key_ptr_offset(0),
1345 (dst_nritems) * 1342 (dst_nritems) *
1346 sizeof(struct btrfs_key_ptr)); 1343 sizeof(struct btrfs_key_ptr));
1347 1344
1348 copy_extent_buffer(dst, src, 1345 copy_extent_buffer(dst, src,
1349 btrfs_node_key_ptr_offset(0), 1346 btrfs_node_key_ptr_offset(0),
1350 btrfs_node_key_ptr_offset(src_nritems - push_items), 1347 btrfs_node_key_ptr_offset(src_nritems - push_items),
1351 push_items * sizeof(struct btrfs_key_ptr)); 1348 push_items * sizeof(struct btrfs_key_ptr));
1352 1349
1353 btrfs_set_header_nritems(src, src_nritems - push_items); 1350 btrfs_set_header_nritems(src, src_nritems - push_items);
1354 btrfs_set_header_nritems(dst, dst_nritems + push_items); 1351 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1355 1352
1356 btrfs_mark_buffer_dirty(src); 1353 btrfs_mark_buffer_dirty(src);
1357 btrfs_mark_buffer_dirty(dst); 1354 btrfs_mark_buffer_dirty(dst);
1358 return ret; 1355 return ret;
1359 } 1356 }
1360 1357
1361 /* 1358 /*
1362 * helper function to insert a new root level in the tree. 1359 * helper function to insert a new root level in the tree.
1363 * A new node is allocated, and a single item is inserted to 1360 * A new node is allocated, and a single item is inserted to
1364 * point to the existing root 1361 * point to the existing root
1365 * 1362 *
1366 * returns zero on success or < 0 on failure. 1363 * returns zero on success or < 0 on failure.
1367 */ 1364 */
1368 static int noinline insert_new_root(struct btrfs_trans_handle *trans, 1365 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1369 struct btrfs_root *root, 1366 struct btrfs_root *root,
1370 struct btrfs_path *path, int level) 1367 struct btrfs_path *path, int level)
1371 { 1368 {
1372 u64 root_gen; 1369 u64 root_gen;
1373 u64 lower_gen; 1370 u64 lower_gen;
1374 struct extent_buffer *lower; 1371 struct extent_buffer *lower;
1375 struct extent_buffer *c; 1372 struct extent_buffer *c;
1376 struct btrfs_disk_key lower_key; 1373 struct btrfs_disk_key lower_key;
1377 1374
1378 BUG_ON(path->nodes[level]); 1375 BUG_ON(path->nodes[level]);
1379 BUG_ON(path->nodes[level-1] != root->node); 1376 BUG_ON(path->nodes[level-1] != root->node);
1380 1377
1381 if (root->ref_cows) 1378 if (root->ref_cows)
1382 root_gen = trans->transid; 1379 root_gen = trans->transid;
1383 else 1380 else
1384 root_gen = 0; 1381 root_gen = 0;
1385 1382
1386 lower = path->nodes[level-1]; 1383 lower = path->nodes[level-1];
1387 if (level == 1) 1384 if (level == 1)
1388 btrfs_item_key(lower, &lower_key, 0); 1385 btrfs_item_key(lower, &lower_key, 0);
1389 else 1386 else
1390 btrfs_node_key(lower, &lower_key, 0); 1387 btrfs_node_key(lower, &lower_key, 0);
1391 1388
1392 c = __btrfs_alloc_free_block(trans, root, root->nodesize, 1389 c = __btrfs_alloc_free_block(trans, root, root->nodesize,
1393 root->root_key.objectid, 1390 root->root_key.objectid,
1394 root_gen, lower_key.objectid, level, 1391 root_gen, lower_key.objectid, level,
1395 root->node->start, 0); 1392 root->node->start, 0);
1396 if (IS_ERR(c)) 1393 if (IS_ERR(c))
1397 return PTR_ERR(c); 1394 return PTR_ERR(c);
1398 memset_extent_buffer(c, 0, 0, root->nodesize); 1395 memset_extent_buffer(c, 0, 0, root->nodesize);
1399 btrfs_set_header_nritems(c, 1); 1396 btrfs_set_header_nritems(c, 1);
1400 btrfs_set_header_level(c, level); 1397 btrfs_set_header_level(c, level);
1401 btrfs_set_header_bytenr(c, c->start); 1398 btrfs_set_header_bytenr(c, c->start);
1402 btrfs_set_header_generation(c, trans->transid); 1399 btrfs_set_header_generation(c, trans->transid);
1403 btrfs_set_header_owner(c, root->root_key.objectid); 1400 btrfs_set_header_owner(c, root->root_key.objectid);
1404 1401
1405 write_extent_buffer(c, root->fs_info->fsid, 1402 write_extent_buffer(c, root->fs_info->fsid,
1406 (unsigned long)btrfs_header_fsid(c), 1403 (unsigned long)btrfs_header_fsid(c),
1407 BTRFS_FSID_SIZE); 1404 BTRFS_FSID_SIZE);
1408 1405
1409 write_extent_buffer(c, root->fs_info->chunk_tree_uuid, 1406 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1410 (unsigned long)btrfs_header_chunk_tree_uuid(c), 1407 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1411 BTRFS_UUID_SIZE); 1408 BTRFS_UUID_SIZE);
1412 1409
1413 btrfs_set_node_key(c, &lower_key, 0); 1410 btrfs_set_node_key(c, &lower_key, 0);
1414 btrfs_set_node_blockptr(c, 0, lower->start); 1411 btrfs_set_node_blockptr(c, 0, lower->start);
1415 lower_gen = btrfs_header_generation(lower); 1412 lower_gen = btrfs_header_generation(lower);
1416 WARN_ON(lower_gen == 0); 1413 WARN_ON(lower_gen == 0);
1417 1414
1418 btrfs_set_node_ptr_generation(c, 0, lower_gen); 1415 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1419 1416
1420 btrfs_mark_buffer_dirty(c); 1417 btrfs_mark_buffer_dirty(c);
1421 1418
1422 /* the super has an extra ref to root->node */ 1419 /* the super has an extra ref to root->node */
1423 free_extent_buffer(root->node); 1420 free_extent_buffer(root->node);
1424 root->node = c; 1421 root->node = c;
1425 add_root_to_dirty_list(root); 1422 add_root_to_dirty_list(root);
1426 extent_buffer_get(c); 1423 extent_buffer_get(c);
1427 path->nodes[level] = c; 1424 path->nodes[level] = c;
1428 path->slots[level] = 0; 1425 path->slots[level] = 0;
1429 1426
1430 if (root->ref_cows && lower_gen != trans->transid) { 1427 if (root->ref_cows && lower_gen != trans->transid) {
1431 struct btrfs_path *back_path = btrfs_alloc_path(); 1428 struct btrfs_path *back_path = btrfs_alloc_path();
1432 int ret; 1429 int ret;
1433 ret = btrfs_insert_extent_backref(trans, 1430 ret = btrfs_insert_extent_backref(trans,
1434 root->fs_info->extent_root, 1431 root->fs_info->extent_root,
1435 path, lower->start, 1432 path, lower->start,
1436 root->root_key.objectid, 1433 root->root_key.objectid,
1437 trans->transid, 0, 0); 1434 trans->transid, 0, 0);
1438 BUG_ON(ret); 1435 BUG_ON(ret);
1439 btrfs_free_path(back_path); 1436 btrfs_free_path(back_path);
1440 } 1437 }
1441 return 0; 1438 return 0;
1442 } 1439 }
1443 1440
1444 /* 1441 /*
1445 * worker function to insert a single pointer in a node. 1442 * worker function to insert a single pointer in a node.
1446 * the node should have enough room for the pointer already 1443 * the node should have enough room for the pointer already
1447 * 1444 *
1448 * slot and level indicate where you want the key to go, and 1445 * slot and level indicate where you want the key to go, and
1449 * blocknr is the block the key points to. 1446 * blocknr is the block the key points to.
1450 * 1447 *
1451 * returns zero on success and < 0 on any error 1448 * returns zero on success and < 0 on any error
1452 */ 1449 */
1453 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root 1450 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1454 *root, struct btrfs_path *path, struct btrfs_disk_key 1451 *root, struct btrfs_path *path, struct btrfs_disk_key
1455 *key, u64 bytenr, int slot, int level) 1452 *key, u64 bytenr, int slot, int level)
1456 { 1453 {
1457 struct extent_buffer *lower; 1454 struct extent_buffer *lower;
1458 int nritems; 1455 int nritems;
1459 1456
1460 BUG_ON(!path->nodes[level]); 1457 BUG_ON(!path->nodes[level]);
1461 lower = path->nodes[level]; 1458 lower = path->nodes[level];
1462 nritems = btrfs_header_nritems(lower); 1459 nritems = btrfs_header_nritems(lower);
1463 if (slot > nritems) 1460 if (slot > nritems)
1464 BUG(); 1461 BUG();
1465 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root)) 1462 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
1466 BUG(); 1463 BUG();
1467 if (slot != nritems) { 1464 if (slot != nritems) {
1468 memmove_extent_buffer(lower, 1465 memmove_extent_buffer(lower,
1469 btrfs_node_key_ptr_offset(slot + 1), 1466 btrfs_node_key_ptr_offset(slot + 1),
1470 btrfs_node_key_ptr_offset(slot), 1467 btrfs_node_key_ptr_offset(slot),
1471 (nritems - slot) * sizeof(struct btrfs_key_ptr)); 1468 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1472 } 1469 }
1473 btrfs_set_node_key(lower, key, slot); 1470 btrfs_set_node_key(lower, key, slot);
1474 btrfs_set_node_blockptr(lower, slot, bytenr); 1471 btrfs_set_node_blockptr(lower, slot, bytenr);
1475 WARN_ON(trans->transid == 0); 1472 WARN_ON(trans->transid == 0);
1476 btrfs_set_node_ptr_generation(lower, slot, trans->transid); 1473 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1477 btrfs_set_header_nritems(lower, nritems + 1); 1474 btrfs_set_header_nritems(lower, nritems + 1);
1478 btrfs_mark_buffer_dirty(lower); 1475 btrfs_mark_buffer_dirty(lower);
1479 return 0; 1476 return 0;
1480 } 1477 }
1481 1478
1482 /* 1479 /*
1483 * split the node at the specified level in path in two. 1480 * split the node at the specified level in path in two.
1484 * The path is corrected to point to the appropriate node after the split 1481 * The path is corrected to point to the appropriate node after the split
1485 * 1482 *
1486 * Before splitting this tries to make some room in the node by pushing 1483 * Before splitting this tries to make some room in the node by pushing
1487 * left and right, if either one works, it returns right away. 1484 * left and right, if either one works, it returns right away.
1488 * 1485 *
1489 * returns 0 on success and < 0 on failure 1486 * returns 0 on success and < 0 on failure
1490 */ 1487 */
1491 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root 1488 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1492 *root, struct btrfs_path *path, int level) 1489 *root, struct btrfs_path *path, int level)
1493 { 1490 {
1494 u64 root_gen; 1491 u64 root_gen;
1495 struct extent_buffer *c; 1492 struct extent_buffer *c;
1496 struct extent_buffer *split; 1493 struct extent_buffer *split;
1497 struct btrfs_disk_key disk_key; 1494 struct btrfs_disk_key disk_key;
1498 int mid; 1495 int mid;
1499 int ret; 1496 int ret;
1500 int wret; 1497 int wret;
1501 u32 c_nritems; 1498 u32 c_nritems;
1502 1499
1503 c = path->nodes[level]; 1500 c = path->nodes[level];
1504 WARN_ON(btrfs_header_generation(c) != trans->transid); 1501 WARN_ON(btrfs_header_generation(c) != trans->transid);
1505 if (c == root->node) { 1502 if (c == root->node) {
1506 /* trying to split the root, lets make a new one */ 1503 /* trying to split the root, lets make a new one */
1507 ret = insert_new_root(trans, root, path, level + 1); 1504 ret = insert_new_root(trans, root, path, level + 1);
1508 if (ret) 1505 if (ret)
1509 return ret; 1506 return ret;
1510 } else { 1507 } else {
1511 ret = push_nodes_for_insert(trans, root, path, level); 1508 ret = push_nodes_for_insert(trans, root, path, level);
1512 c = path->nodes[level]; 1509 c = path->nodes[level];
1513 if (!ret && btrfs_header_nritems(c) < 1510 if (!ret && btrfs_header_nritems(c) <
1514 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) 1511 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
1515 return 0; 1512 return 0;
1516 if (ret < 0) 1513 if (ret < 0)
1517 return ret; 1514 return ret;
1518 } 1515 }
1519 1516
1520 c_nritems = btrfs_header_nritems(c); 1517 c_nritems = btrfs_header_nritems(c);
1521 if (root->ref_cows) 1518 if (root->ref_cows)
1522 root_gen = trans->transid; 1519 root_gen = trans->transid;
1523 else 1520 else
1524 root_gen = 0; 1521 root_gen = 0;
1525 1522
1526 btrfs_node_key(c, &disk_key, 0); 1523 btrfs_node_key(c, &disk_key, 0);
1527 split = __btrfs_alloc_free_block(trans, root, root->nodesize, 1524 split = __btrfs_alloc_free_block(trans, root, root->nodesize,
1528 root->root_key.objectid, 1525 root->root_key.objectid,
1529 root_gen, 1526 root_gen,
1530 btrfs_disk_key_objectid(&disk_key), 1527 btrfs_disk_key_objectid(&disk_key),
1531 level, c->start, 0); 1528 level, c->start, 0);
1532 if (IS_ERR(split)) 1529 if (IS_ERR(split))
1533 return PTR_ERR(split); 1530 return PTR_ERR(split);
1534 1531
1535 btrfs_set_header_flags(split, btrfs_header_flags(c)); 1532 btrfs_set_header_flags(split, btrfs_header_flags(c));
1536 btrfs_set_header_level(split, btrfs_header_level(c)); 1533 btrfs_set_header_level(split, btrfs_header_level(c));
1537 btrfs_set_header_bytenr(split, split->start); 1534 btrfs_set_header_bytenr(split, split->start);
1538 btrfs_set_header_generation(split, trans->transid); 1535 btrfs_set_header_generation(split, trans->transid);
1539 btrfs_set_header_owner(split, root->root_key.objectid); 1536 btrfs_set_header_owner(split, root->root_key.objectid);
1540 btrfs_set_header_flags(split, 0); 1537 btrfs_set_header_flags(split, 0);
1541 write_extent_buffer(split, root->fs_info->fsid, 1538 write_extent_buffer(split, root->fs_info->fsid,
1542 (unsigned long)btrfs_header_fsid(split), 1539 (unsigned long)btrfs_header_fsid(split),
1543 BTRFS_FSID_SIZE); 1540 BTRFS_FSID_SIZE);
1544 write_extent_buffer(split, root->fs_info->chunk_tree_uuid, 1541 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1545 (unsigned long)btrfs_header_chunk_tree_uuid(split), 1542 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1546 BTRFS_UUID_SIZE); 1543 BTRFS_UUID_SIZE);
1547 1544
1548 mid = (c_nritems + 1) / 2; 1545 mid = (c_nritems + 1) / 2;
1549 1546
1550 copy_extent_buffer(split, c, 1547 copy_extent_buffer(split, c,
1551 btrfs_node_key_ptr_offset(0), 1548 btrfs_node_key_ptr_offset(0),
1552 btrfs_node_key_ptr_offset(mid), 1549 btrfs_node_key_ptr_offset(mid),
1553 (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); 1550 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1554 btrfs_set_header_nritems(split, c_nritems - mid); 1551 btrfs_set_header_nritems(split, c_nritems - mid);
1555 btrfs_set_header_nritems(c, mid); 1552 btrfs_set_header_nritems(c, mid);
1556 ret = 0; 1553 ret = 0;
1557 1554
1558 btrfs_mark_buffer_dirty(c); 1555 btrfs_mark_buffer_dirty(c);
1559 btrfs_mark_buffer_dirty(split); 1556 btrfs_mark_buffer_dirty(split);
1560 1557
1561 btrfs_node_key(split, &disk_key, 0); 1558 btrfs_node_key(split, &disk_key, 0);
1562 wret = insert_ptr(trans, root, path, &disk_key, split->start, 1559 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1563 path->slots[level + 1] + 1, 1560 path->slots[level + 1] + 1,
1564 level + 1); 1561 level + 1);
1565 if (wret) 1562 if (wret)
1566 ret = wret; 1563 ret = wret;
1567 1564
1568 if (path->slots[level] >= mid) { 1565 if (path->slots[level] >= mid) {
1569 path->slots[level] -= mid; 1566 path->slots[level] -= mid;
1570 free_extent_buffer(c); 1567 free_extent_buffer(c);
1571 path->nodes[level] = split; 1568 path->nodes[level] = split;
1572 path->slots[level + 1] += 1; 1569 path->slots[level + 1] += 1;
1573 } else { 1570 } else {
1574 free_extent_buffer(split); 1571 free_extent_buffer(split);
1575 } 1572 }
1576 return ret; 1573 return ret;
1577 } 1574 }
1578 1575
1579 /* 1576 /*
1580 * how many bytes are required to store the items in a leaf. start 1577 * how many bytes are required to store the items in a leaf. start
1581 * and nr indicate which items in the leaf to check. This totals up the 1578 * and nr indicate which items in the leaf to check. This totals up the
1582 * space used both by the item structs and the item data 1579 * space used both by the item structs and the item data
1583 */ 1580 */
1584 static int leaf_space_used(struct extent_buffer *l, int start, int nr) 1581 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1585 { 1582 {
1586 int data_len; 1583 int data_len;
1587 int nritems = btrfs_header_nritems(l); 1584 int nritems = btrfs_header_nritems(l);
1588 int end = min(nritems, start + nr) - 1; 1585 int end = min(nritems, start + nr) - 1;
1589 1586
1590 if (!nr) 1587 if (!nr)
1591 return 0; 1588 return 0;
1592 data_len = btrfs_item_end_nr(l, start); 1589 data_len = btrfs_item_end_nr(l, start);
1593 data_len = data_len - btrfs_item_offset_nr(l, end); 1590 data_len = data_len - btrfs_item_offset_nr(l, end);
1594 data_len += sizeof(struct btrfs_item) * nr; 1591 data_len += sizeof(struct btrfs_item) * nr;
1595 WARN_ON(data_len < 0); 1592 WARN_ON(data_len < 0);
1596 return data_len; 1593 return data_len;
1597 } 1594 }
1598 1595
1599 /* 1596 /*
1600 * The space between the end of the leaf items and 1597 * The space between the end of the leaf items and
1601 * the start of the leaf data. IOW, how much room 1598 * the start of the leaf data. IOW, how much room
1602 * the leaf has left for both items and data 1599 * the leaf has left for both items and data
1603 */ 1600 */
1604 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf) 1601 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
1605 { 1602 {
1606 int nritems = btrfs_header_nritems(leaf); 1603 int nritems = btrfs_header_nritems(leaf);
1607 int ret; 1604 int ret;
1608 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); 1605 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1609 if (ret < 0) { 1606 if (ret < 0) {
1610 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n", 1607 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1611 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root), 1608 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
1612 leaf_space_used(leaf, 0, nritems), nritems); 1609 leaf_space_used(leaf, 0, nritems), nritems);
1613 } 1610 }
1614 return ret; 1611 return ret;
1615 } 1612 }
1616 1613
1617 /* 1614 /*
1618 * push some data in the path leaf to the right, trying to free up at 1615 * push some data in the path leaf to the right, trying to free up at
1619 * least data_size bytes. returns zero if the push worked, nonzero otherwise 1616 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1620 * 1617 *
1621 * returns 1 if the push failed because the other node didn't have enough 1618 * returns 1 if the push failed because the other node didn't have enough
1622 * room, 0 if everything worked out and < 0 if there were major errors. 1619 * room, 0 if everything worked out and < 0 if there were major errors.
1623 */ 1620 */
1624 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root 1621 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1625 *root, struct btrfs_path *path, int data_size, 1622 *root, struct btrfs_path *path, int data_size,
1626 int empty) 1623 int empty)
1627 { 1624 {
1628 struct extent_buffer *left = path->nodes[0]; 1625 struct extent_buffer *left = path->nodes[0];
1629 struct extent_buffer *right; 1626 struct extent_buffer *right;
1630 struct extent_buffer *upper; 1627 struct extent_buffer *upper;
1631 struct btrfs_disk_key disk_key; 1628 struct btrfs_disk_key disk_key;
1632 int slot; 1629 int slot;
1633 u32 i; 1630 u32 i;
1634 int free_space; 1631 int free_space;
1635 int push_space = 0; 1632 int push_space = 0;
1636 int push_items = 0; 1633 int push_items = 0;
1637 struct btrfs_item *item; 1634 struct btrfs_item *item;
1638 u32 left_nritems; 1635 u32 left_nritems;
1639 u32 nr; 1636 u32 nr;
1640 u32 right_nritems; 1637 u32 right_nritems;
1641 u32 data_end; 1638 u32 data_end;
1642 u32 this_item_size; 1639 u32 this_item_size;
1643 int ret; 1640 int ret;
1644 1641
1645 slot = path->slots[1]; 1642 slot = path->slots[1];
1646 if (!path->nodes[1]) { 1643 if (!path->nodes[1]) {
1647 return 1; 1644 return 1;
1648 } 1645 }
1649 upper = path->nodes[1]; 1646 upper = path->nodes[1];
1650 if (slot >= btrfs_header_nritems(upper) - 1) 1647 if (slot >= btrfs_header_nritems(upper) - 1)
1651 return 1; 1648 return 1;
1652 1649
1653 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1), 1650 right = read_node_slot(root, upper, slot + 1);
1654 root->leafsize);
1655 free_space = btrfs_leaf_free_space(root, right); 1651 free_space = btrfs_leaf_free_space(root, right);
1656 if (free_space < data_size + sizeof(struct btrfs_item)) { 1652 if (free_space < data_size + sizeof(struct btrfs_item)) {
1657 free_extent_buffer(right); 1653 free_extent_buffer(right);
1658 return 1; 1654 return 1;
1659 } 1655 }
1660 1656
1661 /* cow and double check */ 1657 /* cow and double check */
1662 ret = btrfs_cow_block(trans, root, right, upper, 1658 ret = btrfs_cow_block(trans, root, right, upper,
1663 slot + 1, &right); 1659 slot + 1, &right);
1664 if (ret) { 1660 if (ret) {
1665 free_extent_buffer(right); 1661 free_extent_buffer(right);
1666 return 1; 1662 return 1;
1667 } 1663 }
1668 free_space = btrfs_leaf_free_space(root, right); 1664 free_space = btrfs_leaf_free_space(root, right);
1669 if (free_space < data_size + sizeof(struct btrfs_item)) { 1665 if (free_space < data_size + sizeof(struct btrfs_item)) {
1670 free_extent_buffer(right); 1666 free_extent_buffer(right);
1671 return 1; 1667 return 1;
1672 } 1668 }
1673 1669
1674 left_nritems = btrfs_header_nritems(left); 1670 left_nritems = btrfs_header_nritems(left);
1675 if (left_nritems == 0) { 1671 if (left_nritems == 0) {
1676 free_extent_buffer(right); 1672 free_extent_buffer(right);
1677 return 1; 1673 return 1;
1678 } 1674 }
1679 1675
1680 if (empty) 1676 if (empty)
1681 nr = 0; 1677 nr = 0;
1682 else 1678 else
1683 nr = 1; 1679 nr = 1;
1684 1680
1685 i = left_nritems - 1; 1681 i = left_nritems - 1;
1686 while (i >= nr) { 1682 while (i >= nr) {
1687 item = btrfs_item_nr(left, i); 1683 item = btrfs_item_nr(left, i);
1688 1684
1689 if (path->slots[0] == i) 1685 if (path->slots[0] == i)
1690 push_space += data_size + sizeof(*item); 1686 push_space += data_size + sizeof(*item);
1691 1687
1692 if (!left->map_token) { 1688 if (!left->map_token) {
1693 map_extent_buffer(left, (unsigned long)item, 1689 map_extent_buffer(left, (unsigned long)item,
1694 sizeof(struct btrfs_item), 1690 sizeof(struct btrfs_item),
1695 &left->map_token, &left->kaddr, 1691 &left->map_token, &left->kaddr,
1696 &left->map_start, &left->map_len, 1692 &left->map_start, &left->map_len,
1697 KM_USER1); 1693 KM_USER1);
1698 } 1694 }
1699 1695
1700 this_item_size = btrfs_item_size(left, item); 1696 this_item_size = btrfs_item_size(left, item);
1701 if (this_item_size + sizeof(*item) + push_space > free_space) 1697 if (this_item_size + sizeof(*item) + push_space > free_space)
1702 break; 1698 break;
1703 push_items++; 1699 push_items++;
1704 push_space += this_item_size + sizeof(*item); 1700 push_space += this_item_size + sizeof(*item);
1705 if (i == 0) 1701 if (i == 0)
1706 break; 1702 break;
1707 i--; 1703 i--;
1708 } 1704 }
1709 if (left->map_token) { 1705 if (left->map_token) {
1710 unmap_extent_buffer(left, left->map_token, KM_USER1); 1706 unmap_extent_buffer(left, left->map_token, KM_USER1);
1711 left->map_token = NULL; 1707 left->map_token = NULL;
1712 } 1708 }
1713 1709
1714 if (push_items == 0) { 1710 if (push_items == 0) {
1715 free_extent_buffer(right); 1711 free_extent_buffer(right);
1716 return 1; 1712 return 1;
1717 } 1713 }
1718 1714
1719 if (!empty && push_items == left_nritems) 1715 if (!empty && push_items == left_nritems)
1720 WARN_ON(1); 1716 WARN_ON(1);
1721 1717
1722 /* push left to right */ 1718 /* push left to right */
1723 right_nritems = btrfs_header_nritems(right); 1719 right_nritems = btrfs_header_nritems(right);
1724 1720
1725 push_space = btrfs_item_end_nr(left, left_nritems - push_items); 1721 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1726 push_space -= leaf_data_end(root, left); 1722 push_space -= leaf_data_end(root, left);
1727 1723
1728 /* make room in the right data area */ 1724 /* make room in the right data area */
1729 data_end = leaf_data_end(root, right); 1725 data_end = leaf_data_end(root, right);
1730 memmove_extent_buffer(right, 1726 memmove_extent_buffer(right,
1731 btrfs_leaf_data(right) + data_end - push_space, 1727 btrfs_leaf_data(right) + data_end - push_space,
1732 btrfs_leaf_data(right) + data_end, 1728 btrfs_leaf_data(right) + data_end,
1733 BTRFS_LEAF_DATA_SIZE(root) - data_end); 1729 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1734 1730
1735 /* copy from the left data area */ 1731 /* copy from the left data area */
1736 copy_extent_buffer(right, left, btrfs_leaf_data(right) + 1732 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1737 BTRFS_LEAF_DATA_SIZE(root) - push_space, 1733 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1738 btrfs_leaf_data(left) + leaf_data_end(root, left), 1734 btrfs_leaf_data(left) + leaf_data_end(root, left),
1739 push_space); 1735 push_space);
1740 1736
1741 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), 1737 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1742 btrfs_item_nr_offset(0), 1738 btrfs_item_nr_offset(0),
1743 right_nritems * sizeof(struct btrfs_item)); 1739 right_nritems * sizeof(struct btrfs_item));
1744 1740
1745 /* copy the items from left to right */ 1741 /* copy the items from left to right */
1746 copy_extent_buffer(right, left, btrfs_item_nr_offset(0), 1742 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1747 btrfs_item_nr_offset(left_nritems - push_items), 1743 btrfs_item_nr_offset(left_nritems - push_items),
1748 push_items * sizeof(struct btrfs_item)); 1744 push_items * sizeof(struct btrfs_item));
1749 1745
1750 /* update the item pointers */ 1746 /* update the item pointers */
1751 right_nritems += push_items; 1747 right_nritems += push_items;
1752 btrfs_set_header_nritems(right, right_nritems); 1748 btrfs_set_header_nritems(right, right_nritems);
1753 push_space = BTRFS_LEAF_DATA_SIZE(root); 1749 push_space = BTRFS_LEAF_DATA_SIZE(root);
1754 for (i = 0; i < right_nritems; i++) { 1750 for (i = 0; i < right_nritems; i++) {
1755 item = btrfs_item_nr(right, i); 1751 item = btrfs_item_nr(right, i);
1756 if (!right->map_token) { 1752 if (!right->map_token) {
1757 map_extent_buffer(right, (unsigned long)item, 1753 map_extent_buffer(right, (unsigned long)item,
1758 sizeof(struct btrfs_item), 1754 sizeof(struct btrfs_item),
1759 &right->map_token, &right->kaddr, 1755 &right->map_token, &right->kaddr,
1760 &right->map_start, &right->map_len, 1756 &right->map_start, &right->map_len,
1761 KM_USER1); 1757 KM_USER1);
1762 } 1758 }
1763 push_space -= btrfs_item_size(right, item); 1759 push_space -= btrfs_item_size(right, item);
1764 btrfs_set_item_offset(right, item, push_space); 1760 btrfs_set_item_offset(right, item, push_space);
1765 } 1761 }
1766 1762
1767 if (right->map_token) { 1763 if (right->map_token) {
1768 unmap_extent_buffer(right, right->map_token, KM_USER1); 1764 unmap_extent_buffer(right, right->map_token, KM_USER1);
1769 right->map_token = NULL; 1765 right->map_token = NULL;
1770 } 1766 }
1771 left_nritems -= push_items; 1767 left_nritems -= push_items;
1772 btrfs_set_header_nritems(left, left_nritems); 1768 btrfs_set_header_nritems(left, left_nritems);
1773 1769
1774 if (left_nritems) 1770 if (left_nritems)
1775 btrfs_mark_buffer_dirty(left); 1771 btrfs_mark_buffer_dirty(left);
1776 btrfs_mark_buffer_dirty(right); 1772 btrfs_mark_buffer_dirty(right);
1777 1773
1778 btrfs_item_key(right, &disk_key, 0); 1774 btrfs_item_key(right, &disk_key, 0);
1779 btrfs_set_node_key(upper, &disk_key, slot + 1); 1775 btrfs_set_node_key(upper, &disk_key, slot + 1);
1780 btrfs_mark_buffer_dirty(upper); 1776 btrfs_mark_buffer_dirty(upper);
1781 1777
1782 /* then fixup the leaf pointer in the path */ 1778 /* then fixup the leaf pointer in the path */
1783 if (path->slots[0] >= left_nritems) { 1779 if (path->slots[0] >= left_nritems) {
1784 path->slots[0] -= left_nritems; 1780 path->slots[0] -= left_nritems;
1785 free_extent_buffer(path->nodes[0]); 1781 free_extent_buffer(path->nodes[0]);
1786 path->nodes[0] = right; 1782 path->nodes[0] = right;
1787 path->slots[1] += 1; 1783 path->slots[1] += 1;
1788 } else { 1784 } else {
1789 free_extent_buffer(right); 1785 free_extent_buffer(right);
1790 } 1786 }
1791 return 0; 1787 return 0;
1792 } 1788 }
1793 /* 1789 /*
1794 * push some data in the path leaf to the left, trying to free up at 1790 * push some data in the path leaf to the left, trying to free up at
1795 * least data_size bytes. returns zero if the push worked, nonzero otherwise 1791 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1796 */ 1792 */
1797 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root 1793 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1798 *root, struct btrfs_path *path, int data_size, 1794 *root, struct btrfs_path *path, int data_size,
1799 int empty) 1795 int empty)
1800 { 1796 {
1801 struct btrfs_disk_key disk_key; 1797 struct btrfs_disk_key disk_key;
1802 struct extent_buffer *right = path->nodes[0]; 1798 struct extent_buffer *right = path->nodes[0];
1803 struct extent_buffer *left; 1799 struct extent_buffer *left;
1804 int slot; 1800 int slot;
1805 int i; 1801 int i;
1806 int free_space; 1802 int free_space;
1807 int push_space = 0; 1803 int push_space = 0;
1808 int push_items = 0; 1804 int push_items = 0;
1809 struct btrfs_item *item; 1805 struct btrfs_item *item;
1810 u32 old_left_nritems; 1806 u32 old_left_nritems;
1811 u32 right_nritems; 1807 u32 right_nritems;
1812 u32 nr; 1808 u32 nr;
1813 int ret = 0; 1809 int ret = 0;
1814 int wret; 1810 int wret;
1815 u32 this_item_size; 1811 u32 this_item_size;
1816 u32 old_left_item_size; 1812 u32 old_left_item_size;
1817 1813
1818 slot = path->slots[1]; 1814 slot = path->slots[1];
1819 if (slot == 0) 1815 if (slot == 0)
1820 return 1; 1816 return 1;
1821 if (!path->nodes[1]) 1817 if (!path->nodes[1])
1822 return 1; 1818 return 1;
1823 1819
1824 right_nritems = btrfs_header_nritems(right); 1820 right_nritems = btrfs_header_nritems(right);
1825 if (right_nritems == 0) { 1821 if (right_nritems == 0) {
1826 return 1; 1822 return 1;
1827 } 1823 }
1828 1824
1829 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1], 1825 left = read_node_slot(root, path->nodes[1], slot - 1);
1830 slot - 1), root->leafsize);
1831 free_space = btrfs_leaf_free_space(root, left); 1826 free_space = btrfs_leaf_free_space(root, left);
1832 if (free_space < data_size + sizeof(struct btrfs_item)) { 1827 if (free_space < data_size + sizeof(struct btrfs_item)) {
1833 free_extent_buffer(left); 1828 free_extent_buffer(left);
1834 return 1; 1829 return 1;
1835 } 1830 }
1836 1831
1837 /* cow and double check */ 1832 /* cow and double check */
1838 ret = btrfs_cow_block(trans, root, left, 1833 ret = btrfs_cow_block(trans, root, left,
1839 path->nodes[1], slot - 1, &left); 1834 path->nodes[1], slot - 1, &left);
1840 if (ret) { 1835 if (ret) {
1841 /* we hit -ENOSPC, but it isn't fatal here */ 1836 /* we hit -ENOSPC, but it isn't fatal here */
1842 free_extent_buffer(left); 1837 free_extent_buffer(left);
1843 return 1; 1838 return 1;
1844 } 1839 }
1845 1840
1846 free_space = btrfs_leaf_free_space(root, left); 1841 free_space = btrfs_leaf_free_space(root, left);
1847 if (free_space < data_size + sizeof(struct btrfs_item)) { 1842 if (free_space < data_size + sizeof(struct btrfs_item)) {
1848 free_extent_buffer(left); 1843 free_extent_buffer(left);
1849 return 1; 1844 return 1;
1850 } 1845 }
1851 1846
1852 if (empty) 1847 if (empty)
1853 nr = right_nritems; 1848 nr = right_nritems;
1854 else 1849 else
1855 nr = right_nritems - 1; 1850 nr = right_nritems - 1;
1856 1851
1857 for (i = 0; i < nr; i++) { 1852 for (i = 0; i < nr; i++) {
1858 item = btrfs_item_nr(right, i); 1853 item = btrfs_item_nr(right, i);
1859 if (!right->map_token) { 1854 if (!right->map_token) {
1860 map_extent_buffer(right, (unsigned long)item, 1855 map_extent_buffer(right, (unsigned long)item,
1861 sizeof(struct btrfs_item), 1856 sizeof(struct btrfs_item),
1862 &right->map_token, &right->kaddr, 1857 &right->map_token, &right->kaddr,
1863 &right->map_start, &right->map_len, 1858 &right->map_start, &right->map_len,
1864 KM_USER1); 1859 KM_USER1);
1865 } 1860 }
1866 1861
1867 if (path->slots[0] == i) 1862 if (path->slots[0] == i)
1868 push_space += data_size + sizeof(*item); 1863 push_space += data_size + sizeof(*item);
1869 1864
1870 this_item_size = btrfs_item_size(right, item); 1865 this_item_size = btrfs_item_size(right, item);
1871 if (this_item_size + sizeof(*item) + push_space > free_space) 1866 if (this_item_size + sizeof(*item) + push_space > free_space)
1872 break; 1867 break;
1873 1868
1874 push_items++; 1869 push_items++;
1875 push_space += this_item_size + sizeof(*item); 1870 push_space += this_item_size + sizeof(*item);
1876 } 1871 }
1877 1872
1878 if (right->map_token) { 1873 if (right->map_token) {
1879 unmap_extent_buffer(right, right->map_token, KM_USER1); 1874 unmap_extent_buffer(right, right->map_token, KM_USER1);
1880 right->map_token = NULL; 1875 right->map_token = NULL;
1881 } 1876 }
1882 1877
1883 if (push_items == 0) { 1878 if (push_items == 0) {
1884 free_extent_buffer(left); 1879 free_extent_buffer(left);
1885 return 1; 1880 return 1;
1886 } 1881 }
1887 if (!empty && push_items == btrfs_header_nritems(right)) 1882 if (!empty && push_items == btrfs_header_nritems(right))
1888 WARN_ON(1); 1883 WARN_ON(1);
1889 1884
1890 /* push data from right to left */ 1885 /* push data from right to left */
1891 copy_extent_buffer(left, right, 1886 copy_extent_buffer(left, right,
1892 btrfs_item_nr_offset(btrfs_header_nritems(left)), 1887 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1893 btrfs_item_nr_offset(0), 1888 btrfs_item_nr_offset(0),
1894 push_items * sizeof(struct btrfs_item)); 1889 push_items * sizeof(struct btrfs_item));
1895 1890
1896 push_space = BTRFS_LEAF_DATA_SIZE(root) - 1891 push_space = BTRFS_LEAF_DATA_SIZE(root) -
1897 btrfs_item_offset_nr(right, push_items -1); 1892 btrfs_item_offset_nr(right, push_items -1);
1898 1893
1899 copy_extent_buffer(left, right, btrfs_leaf_data(left) + 1894 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1900 leaf_data_end(root, left) - push_space, 1895 leaf_data_end(root, left) - push_space,
1901 btrfs_leaf_data(right) + 1896 btrfs_leaf_data(right) +
1902 btrfs_item_offset_nr(right, push_items - 1), 1897 btrfs_item_offset_nr(right, push_items - 1),
1903 push_space); 1898 push_space);
1904 old_left_nritems = btrfs_header_nritems(left); 1899 old_left_nritems = btrfs_header_nritems(left);
1905 BUG_ON(old_left_nritems < 0); 1900 BUG_ON(old_left_nritems < 0);
1906 1901
1907 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1); 1902 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1908 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { 1903 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1909 u32 ioff; 1904 u32 ioff;
1910 1905
1911 item = btrfs_item_nr(left, i); 1906 item = btrfs_item_nr(left, i);
1912 if (!left->map_token) { 1907 if (!left->map_token) {
1913 map_extent_buffer(left, (unsigned long)item, 1908 map_extent_buffer(left, (unsigned long)item,
1914 sizeof(struct btrfs_item), 1909 sizeof(struct btrfs_item),
1915 &left->map_token, &left->kaddr, 1910 &left->map_token, &left->kaddr,
1916 &left->map_start, &left->map_len, 1911 &left->map_start, &left->map_len,
1917 KM_USER1); 1912 KM_USER1);
1918 } 1913 }
1919 1914
1920 ioff = btrfs_item_offset(left, item); 1915 ioff = btrfs_item_offset(left, item);
1921 btrfs_set_item_offset(left, item, 1916 btrfs_set_item_offset(left, item,
1922 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size)); 1917 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
1923 } 1918 }
1924 btrfs_set_header_nritems(left, old_left_nritems + push_items); 1919 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1925 if (left->map_token) { 1920 if (left->map_token) {
1926 unmap_extent_buffer(left, left->map_token, KM_USER1); 1921 unmap_extent_buffer(left, left->map_token, KM_USER1);
1927 left->map_token = NULL; 1922 left->map_token = NULL;
1928 } 1923 }
1929 1924
1930 /* fixup right node */ 1925 /* fixup right node */
1931 if (push_items > right_nritems) { 1926 if (push_items > right_nritems) {
1932 printk("push items %d nr %u\n", push_items, right_nritems); 1927 printk("push items %d nr %u\n", push_items, right_nritems);
1933 WARN_ON(1); 1928 WARN_ON(1);
1934 } 1929 }
1935 1930
1936 if (push_items < right_nritems) { 1931 if (push_items < right_nritems) {
1937 push_space = btrfs_item_offset_nr(right, push_items - 1) - 1932 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1938 leaf_data_end(root, right); 1933 leaf_data_end(root, right);
1939 memmove_extent_buffer(right, btrfs_leaf_data(right) + 1934 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1940 BTRFS_LEAF_DATA_SIZE(root) - push_space, 1935 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1941 btrfs_leaf_data(right) + 1936 btrfs_leaf_data(right) +
1942 leaf_data_end(root, right), push_space); 1937 leaf_data_end(root, right), push_space);
1943 1938
1944 memmove_extent_buffer(right, btrfs_item_nr_offset(0), 1939 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1945 btrfs_item_nr_offset(push_items), 1940 btrfs_item_nr_offset(push_items),
1946 (btrfs_header_nritems(right) - push_items) * 1941 (btrfs_header_nritems(right) - push_items) *
1947 sizeof(struct btrfs_item)); 1942 sizeof(struct btrfs_item));
1948 } 1943 }
1949 right_nritems -= push_items; 1944 right_nritems -= push_items;
1950 btrfs_set_header_nritems(right, right_nritems); 1945 btrfs_set_header_nritems(right, right_nritems);
1951 push_space = BTRFS_LEAF_DATA_SIZE(root); 1946 push_space = BTRFS_LEAF_DATA_SIZE(root);
1952 for (i = 0; i < right_nritems; i++) { 1947 for (i = 0; i < right_nritems; i++) {
1953 item = btrfs_item_nr(right, i); 1948 item = btrfs_item_nr(right, i);
1954 1949
1955 if (!right->map_token) { 1950 if (!right->map_token) {
1956 map_extent_buffer(right, (unsigned long)item, 1951 map_extent_buffer(right, (unsigned long)item,
1957 sizeof(struct btrfs_item), 1952 sizeof(struct btrfs_item),
1958 &right->map_token, &right->kaddr, 1953 &right->map_token, &right->kaddr,
1959 &right->map_start, &right->map_len, 1954 &right->map_start, &right->map_len,
1960 KM_USER1); 1955 KM_USER1);
1961 } 1956 }
1962 1957
1963 push_space = push_space - btrfs_item_size(right, item); 1958 push_space = push_space - btrfs_item_size(right, item);
1964 btrfs_set_item_offset(right, item, push_space); 1959 btrfs_set_item_offset(right, item, push_space);
1965 } 1960 }
1966 if (right->map_token) { 1961 if (right->map_token) {
1967 unmap_extent_buffer(right, right->map_token, KM_USER1); 1962 unmap_extent_buffer(right, right->map_token, KM_USER1);
1968 right->map_token = NULL; 1963 right->map_token = NULL;
1969 } 1964 }
1970 1965
1971 btrfs_mark_buffer_dirty(left); 1966 btrfs_mark_buffer_dirty(left);
1972 if (right_nritems) 1967 if (right_nritems)
1973 btrfs_mark_buffer_dirty(right); 1968 btrfs_mark_buffer_dirty(right);
1974 1969
1975 btrfs_item_key(right, &disk_key, 0); 1970 btrfs_item_key(right, &disk_key, 0);
1976 wret = fixup_low_keys(trans, root, path, &disk_key, 1); 1971 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1977 if (wret) 1972 if (wret)
1978 ret = wret; 1973 ret = wret;
1979 1974
1980 /* then fixup the leaf pointer in the path */ 1975 /* then fixup the leaf pointer in the path */
1981 if (path->slots[0] < push_items) { 1976 if (path->slots[0] < push_items) {
1982 path->slots[0] += old_left_nritems; 1977 path->slots[0] += old_left_nritems;
1983 free_extent_buffer(path->nodes[0]); 1978 free_extent_buffer(path->nodes[0]);
1984 path->nodes[0] = left; 1979 path->nodes[0] = left;
1985 path->slots[1] -= 1; 1980 path->slots[1] -= 1;
1986 } else { 1981 } else {
1987 free_extent_buffer(left); 1982 free_extent_buffer(left);
1988 path->slots[0] -= push_items; 1983 path->slots[0] -= push_items;
1989 } 1984 }
1990 BUG_ON(path->slots[0] < 0); 1985 BUG_ON(path->slots[0] < 0);
1991 return ret; 1986 return ret;
1992 } 1987 }
1993 1988
1994 /* 1989 /*
1995 * split the path's leaf in two, making sure there is at least data_size 1990 * split the path's leaf in two, making sure there is at least data_size
1996 * available for the resulting leaf level of the path. 1991 * available for the resulting leaf level of the path.
1997 * 1992 *
1998 * returns 0 if all went well and < 0 on failure. 1993 * returns 0 if all went well and < 0 on failure.
1999 */ 1994 */
2000 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root 1995 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
2001 *root, struct btrfs_key *ins_key, 1996 *root, struct btrfs_key *ins_key,
2002 struct btrfs_path *path, int data_size, int extend) 1997 struct btrfs_path *path, int data_size, int extend)
2003 { 1998 {
2004 u64 root_gen; 1999 u64 root_gen;
2005 struct extent_buffer *l; 2000 struct extent_buffer *l;
2006 u32 nritems; 2001 u32 nritems;
2007 int mid; 2002 int mid;
2008 int slot; 2003 int slot;
2009 struct extent_buffer *right; 2004 struct extent_buffer *right;
2010 int space_needed = data_size + sizeof(struct btrfs_item); 2005 int space_needed = data_size + sizeof(struct btrfs_item);
2011 int data_copy_size; 2006 int data_copy_size;
2012 int rt_data_off; 2007 int rt_data_off;
2013 int i; 2008 int i;
2014 int ret = 0; 2009 int ret = 0;
2015 int wret; 2010 int wret;
2016 int double_split; 2011 int double_split;
2017 int num_doubles = 0; 2012 int num_doubles = 0;
2018 struct btrfs_disk_key disk_key; 2013 struct btrfs_disk_key disk_key;
2019 2014
2020 if (extend) 2015 if (extend)
2021 space_needed = data_size; 2016 space_needed = data_size;
2022 2017
2023 if (root->ref_cows) 2018 if (root->ref_cows)
2024 root_gen = trans->transid; 2019 root_gen = trans->transid;
2025 else 2020 else
2026 root_gen = 0; 2021 root_gen = 0;
2027 2022
2028 /* first try to make some room by pushing left and right */ 2023 /* first try to make some room by pushing left and right */
2029 if (ins_key->type != BTRFS_DIR_ITEM_KEY) { 2024 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
2030 wret = push_leaf_right(trans, root, path, data_size, 0); 2025 wret = push_leaf_right(trans, root, path, data_size, 0);
2031 if (wret < 0) { 2026 if (wret < 0) {
2032 return wret; 2027 return wret;
2033 } 2028 }
2034 if (wret) { 2029 if (wret) {
2035 wret = push_leaf_left(trans, root, path, data_size, 0); 2030 wret = push_leaf_left(trans, root, path, data_size, 0);
2036 if (wret < 0) 2031 if (wret < 0)
2037 return wret; 2032 return wret;
2038 } 2033 }
2039 l = path->nodes[0]; 2034 l = path->nodes[0];
2040 2035
2041 /* did the pushes work? */ 2036 /* did the pushes work? */
2042 if (btrfs_leaf_free_space(root, l) >= space_needed) 2037 if (btrfs_leaf_free_space(root, l) >= space_needed)
2043 return 0; 2038 return 0;
2044 } 2039 }
2045 2040
2046 if (!path->nodes[1]) { 2041 if (!path->nodes[1]) {
2047 ret = insert_new_root(trans, root, path, 1); 2042 ret = insert_new_root(trans, root, path, 1);
2048 if (ret) 2043 if (ret)
2049 return ret; 2044 return ret;
2050 } 2045 }
2051 again: 2046 again:
2052 double_split = 0; 2047 double_split = 0;
2053 l = path->nodes[0]; 2048 l = path->nodes[0];
2054 slot = path->slots[0]; 2049 slot = path->slots[0];
2055 nritems = btrfs_header_nritems(l); 2050 nritems = btrfs_header_nritems(l);
2056 mid = (nritems + 1)/ 2; 2051 mid = (nritems + 1)/ 2;
2057 2052
2058 btrfs_item_key(l, &disk_key, 0); 2053 btrfs_item_key(l, &disk_key, 0);
2059 2054
2060 right = __btrfs_alloc_free_block(trans, root, root->leafsize, 2055 right = __btrfs_alloc_free_block(trans, root, root->leafsize,
2061 root->root_key.objectid, 2056 root->root_key.objectid,
2062 root_gen, disk_key.objectid, 0, 2057 root_gen, disk_key.objectid, 0,
2063 l->start, 0); 2058 l->start, 0);
2064 if (IS_ERR(right)) { 2059 if (IS_ERR(right)) {
2065 BUG_ON(1); 2060 BUG_ON(1);
2066 return PTR_ERR(right); 2061 return PTR_ERR(right);
2067 } 2062 }
2068 2063
2069 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header)); 2064 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2070 btrfs_set_header_bytenr(right, right->start); 2065 btrfs_set_header_bytenr(right, right->start);
2071 btrfs_set_header_generation(right, trans->transid); 2066 btrfs_set_header_generation(right, trans->transid);
2072 btrfs_set_header_owner(right, root->root_key.objectid); 2067 btrfs_set_header_owner(right, root->root_key.objectid);
2073 btrfs_set_header_level(right, 0); 2068 btrfs_set_header_level(right, 0);
2074 write_extent_buffer(right, root->fs_info->fsid, 2069 write_extent_buffer(right, root->fs_info->fsid,
2075 (unsigned long)btrfs_header_fsid(right), 2070 (unsigned long)btrfs_header_fsid(right),
2076 BTRFS_FSID_SIZE); 2071 BTRFS_FSID_SIZE);
2077 2072
2078 write_extent_buffer(right, root->fs_info->chunk_tree_uuid, 2073 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2079 (unsigned long)btrfs_header_chunk_tree_uuid(right), 2074 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2080 BTRFS_UUID_SIZE); 2075 BTRFS_UUID_SIZE);
2081 if (mid <= slot) { 2076 if (mid <= slot) {
2082 if (nritems == 1 || 2077 if (nritems == 1 ||
2083 leaf_space_used(l, mid, nritems - mid) + space_needed > 2078 leaf_space_used(l, mid, nritems - mid) + space_needed >
2084 BTRFS_LEAF_DATA_SIZE(root)) { 2079 BTRFS_LEAF_DATA_SIZE(root)) {
2085 if (slot >= nritems) { 2080 if (slot >= nritems) {
2086 btrfs_cpu_key_to_disk(&disk_key, ins_key); 2081 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2087 btrfs_set_header_nritems(right, 0); 2082 btrfs_set_header_nritems(right, 0);
2088 wret = insert_ptr(trans, root, path, 2083 wret = insert_ptr(trans, root, path,
2089 &disk_key, right->start, 2084 &disk_key, right->start,
2090 path->slots[1] + 1, 1); 2085 path->slots[1] + 1, 1);
2091 if (wret) 2086 if (wret)
2092 ret = wret; 2087 ret = wret;
2093 free_extent_buffer(path->nodes[0]); 2088 free_extent_buffer(path->nodes[0]);
2094 path->nodes[0] = right; 2089 path->nodes[0] = right;
2095 path->slots[0] = 0; 2090 path->slots[0] = 0;
2096 path->slots[1] += 1; 2091 path->slots[1] += 1;
2097 btrfs_mark_buffer_dirty(right); 2092 btrfs_mark_buffer_dirty(right);
2098 return ret; 2093 return ret;
2099 } 2094 }
2100 mid = slot; 2095 mid = slot;
2101 if (mid != nritems && 2096 if (mid != nritems &&
2102 leaf_space_used(l, mid, nritems - mid) + 2097 leaf_space_used(l, mid, nritems - mid) +
2103 space_needed > BTRFS_LEAF_DATA_SIZE(root)) { 2098 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2104 double_split = 1; 2099 double_split = 1;
2105 } 2100 }
2106 } 2101 }
2107 } else { 2102 } else {
2108 if (leaf_space_used(l, 0, mid + 1) + space_needed > 2103 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2109 BTRFS_LEAF_DATA_SIZE(root)) { 2104 BTRFS_LEAF_DATA_SIZE(root)) {
2110 if (!extend && slot == 0) { 2105 if (!extend && slot == 0) {
2111 btrfs_cpu_key_to_disk(&disk_key, ins_key); 2106 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2112 btrfs_set_header_nritems(right, 0); 2107 btrfs_set_header_nritems(right, 0);
2113 wret = insert_ptr(trans, root, path, 2108 wret = insert_ptr(trans, root, path,
2114 &disk_key, 2109 &disk_key,
2115 right->start, 2110 right->start,
2116 path->slots[1], 1); 2111 path->slots[1], 1);
2117 if (wret) 2112 if (wret)
2118 ret = wret; 2113 ret = wret;
2119 free_extent_buffer(path->nodes[0]); 2114 free_extent_buffer(path->nodes[0]);
2120 path->nodes[0] = right; 2115 path->nodes[0] = right;
2121 path->slots[0] = 0; 2116 path->slots[0] = 0;
2122 if (path->slots[1] == 0) { 2117 if (path->slots[1] == 0) {
2123 wret = fixup_low_keys(trans, root, 2118 wret = fixup_low_keys(trans, root,
2124 path, &disk_key, 1); 2119 path, &disk_key, 1);
2125 if (wret) 2120 if (wret)
2126 ret = wret; 2121 ret = wret;
2127 } 2122 }
2128 btrfs_mark_buffer_dirty(right); 2123 btrfs_mark_buffer_dirty(right);
2129 return ret; 2124 return ret;
2130 } else if (extend && slot == 0) { 2125 } else if (extend && slot == 0) {
2131 mid = 1; 2126 mid = 1;
2132 } else { 2127 } else {
2133 mid = slot; 2128 mid = slot;
2134 if (mid != nritems && 2129 if (mid != nritems &&
2135 leaf_space_used(l, mid, nritems - mid) + 2130 leaf_space_used(l, mid, nritems - mid) +
2136 space_needed > BTRFS_LEAF_DATA_SIZE(root)) { 2131 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2137 double_split = 1; 2132 double_split = 1;
2138 } 2133 }
2139 } 2134 }
2140 } 2135 }
2141 } 2136 }
2142 nritems = nritems - mid; 2137 nritems = nritems - mid;
2143 btrfs_set_header_nritems(right, nritems); 2138 btrfs_set_header_nritems(right, nritems);
2144 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l); 2139 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2145 2140
2146 copy_extent_buffer(right, l, btrfs_item_nr_offset(0), 2141 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2147 btrfs_item_nr_offset(mid), 2142 btrfs_item_nr_offset(mid),
2148 nritems * sizeof(struct btrfs_item)); 2143 nritems * sizeof(struct btrfs_item));
2149 2144
2150 copy_extent_buffer(right, l, 2145 copy_extent_buffer(right, l,
2151 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - 2146 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2152 data_copy_size, btrfs_leaf_data(l) + 2147 data_copy_size, btrfs_leaf_data(l) +
2153 leaf_data_end(root, l), data_copy_size); 2148 leaf_data_end(root, l), data_copy_size);
2154 2149
2155 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - 2150 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2156 btrfs_item_end_nr(l, mid); 2151 btrfs_item_end_nr(l, mid);
2157 2152
2158 for (i = 0; i < nritems; i++) { 2153 for (i = 0; i < nritems; i++) {
2159 struct btrfs_item *item = btrfs_item_nr(right, i); 2154 struct btrfs_item *item = btrfs_item_nr(right, i);
2160 u32 ioff; 2155 u32 ioff;
2161 2156
2162 if (!right->map_token) { 2157 if (!right->map_token) {
2163 map_extent_buffer(right, (unsigned long)item, 2158 map_extent_buffer(right, (unsigned long)item,
2164 sizeof(struct btrfs_item), 2159 sizeof(struct btrfs_item),
2165 &right->map_token, &right->kaddr, 2160 &right->map_token, &right->kaddr,
2166 &right->map_start, &right->map_len, 2161 &right->map_start, &right->map_len,
2167 KM_USER1); 2162 KM_USER1);
2168 } 2163 }
2169 2164
2170 ioff = btrfs_item_offset(right, item); 2165 ioff = btrfs_item_offset(right, item);
2171 btrfs_set_item_offset(right, item, ioff + rt_data_off); 2166 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2172 } 2167 }
2173 2168
2174 if (right->map_token) { 2169 if (right->map_token) {
2175 unmap_extent_buffer(right, right->map_token, KM_USER1); 2170 unmap_extent_buffer(right, right->map_token, KM_USER1);
2176 right->map_token = NULL; 2171 right->map_token = NULL;
2177 } 2172 }
2178 2173
2179 btrfs_set_header_nritems(l, mid); 2174 btrfs_set_header_nritems(l, mid);
2180 ret = 0; 2175 ret = 0;
2181 btrfs_item_key(right, &disk_key, 0); 2176 btrfs_item_key(right, &disk_key, 0);
2182 wret = insert_ptr(trans, root, path, &disk_key, right->start, 2177 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2183 path->slots[1] + 1, 1); 2178 path->slots[1] + 1, 1);
2184 if (wret) 2179 if (wret)
2185 ret = wret; 2180 ret = wret;
2186 2181
2187 btrfs_mark_buffer_dirty(right); 2182 btrfs_mark_buffer_dirty(right);
2188 btrfs_mark_buffer_dirty(l); 2183 btrfs_mark_buffer_dirty(l);
2189 BUG_ON(path->slots[0] != slot); 2184 BUG_ON(path->slots[0] != slot);
2190 2185
2191 if (mid <= slot) { 2186 if (mid <= slot) {
2192 free_extent_buffer(path->nodes[0]); 2187 free_extent_buffer(path->nodes[0]);
2193 path->nodes[0] = right; 2188 path->nodes[0] = right;
2194 path->slots[0] -= mid; 2189 path->slots[0] -= mid;
2195 path->slots[1] += 1; 2190 path->slots[1] += 1;
2196 } else 2191 } else
2197 free_extent_buffer(right); 2192 free_extent_buffer(right);
2198 2193
2199 BUG_ON(path->slots[0] < 0); 2194 BUG_ON(path->slots[0] < 0);
2200 2195
2201 if (double_split) { 2196 if (double_split) {
2202 BUG_ON(num_doubles != 0); 2197 BUG_ON(num_doubles != 0);
2203 num_doubles++; 2198 num_doubles++;
2204 goto again; 2199 goto again;
2205 } 2200 }
2206 return ret; 2201 return ret;
2207 } 2202 }
2208 2203
2209 int btrfs_truncate_item(struct btrfs_trans_handle *trans, 2204 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2210 struct btrfs_root *root, 2205 struct btrfs_root *root,
2211 struct btrfs_path *path, 2206 struct btrfs_path *path,
2212 u32 new_size, int from_end) 2207 u32 new_size, int from_end)
2213 { 2208 {
2214 int ret = 0; 2209 int ret = 0;
2215 int slot; 2210 int slot;
2216 int slot_orig; 2211 int slot_orig;
2217 struct extent_buffer *leaf; 2212 struct extent_buffer *leaf;
2218 struct btrfs_item *item; 2213 struct btrfs_item *item;
2219 u32 nritems; 2214 u32 nritems;
2220 unsigned int data_end; 2215 unsigned int data_end;
2221 unsigned int old_data_start; 2216 unsigned int old_data_start;
2222 unsigned int old_size; 2217 unsigned int old_size;
2223 unsigned int size_diff; 2218 unsigned int size_diff;
2224 int i; 2219 int i;
2225 2220
2226 slot_orig = path->slots[0]; 2221 slot_orig = path->slots[0];
2227 leaf = path->nodes[0]; 2222 leaf = path->nodes[0];
2228 slot = path->slots[0]; 2223 slot = path->slots[0];
2229 2224
2230 old_size = btrfs_item_size_nr(leaf, slot); 2225 old_size = btrfs_item_size_nr(leaf, slot);
2231 if (old_size == new_size) 2226 if (old_size == new_size)
2232 return 0; 2227 return 0;
2233 2228
2234 nritems = btrfs_header_nritems(leaf); 2229 nritems = btrfs_header_nritems(leaf);
2235 data_end = leaf_data_end(root, leaf); 2230 data_end = leaf_data_end(root, leaf);
2236 2231
2237 old_data_start = btrfs_item_offset_nr(leaf, slot); 2232 old_data_start = btrfs_item_offset_nr(leaf, slot);
2238 2233
2239 size_diff = old_size - new_size; 2234 size_diff = old_size - new_size;
2240 2235
2241 BUG_ON(slot < 0); 2236 BUG_ON(slot < 0);
2242 BUG_ON(slot >= nritems); 2237 BUG_ON(slot >= nritems);
2243 2238
2244 /* 2239 /*
2245 * item0..itemN ... dataN.offset..dataN.size .. data0.size 2240 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2246 */ 2241 */
2247 /* first correct the data pointers */ 2242 /* first correct the data pointers */
2248 for (i = slot; i < nritems; i++) { 2243 for (i = slot; i < nritems; i++) {
2249 u32 ioff; 2244 u32 ioff;
2250 item = btrfs_item_nr(leaf, i); 2245 item = btrfs_item_nr(leaf, i);
2251 2246
2252 if (!leaf->map_token) { 2247 if (!leaf->map_token) {
2253 map_extent_buffer(leaf, (unsigned long)item, 2248 map_extent_buffer(leaf, (unsigned long)item,
2254 sizeof(struct btrfs_item), 2249 sizeof(struct btrfs_item),
2255 &leaf->map_token, &leaf->kaddr, 2250 &leaf->map_token, &leaf->kaddr,
2256 &leaf->map_start, &leaf->map_len, 2251 &leaf->map_start, &leaf->map_len,
2257 KM_USER1); 2252 KM_USER1);
2258 } 2253 }
2259 2254
2260 ioff = btrfs_item_offset(leaf, item); 2255 ioff = btrfs_item_offset(leaf, item);
2261 btrfs_set_item_offset(leaf, item, ioff + size_diff); 2256 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2262 } 2257 }
2263 2258
2264 if (leaf->map_token) { 2259 if (leaf->map_token) {
2265 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 2260 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2266 leaf->map_token = NULL; 2261 leaf->map_token = NULL;
2267 } 2262 }
2268 2263
2269 /* shift the data */ 2264 /* shift the data */
2270 if (from_end) { 2265 if (from_end) {
2271 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 2266 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2272 data_end + size_diff, btrfs_leaf_data(leaf) + 2267 data_end + size_diff, btrfs_leaf_data(leaf) +
2273 data_end, old_data_start + new_size - data_end); 2268 data_end, old_data_start + new_size - data_end);
2274 } else { 2269 } else {
2275 struct btrfs_disk_key disk_key; 2270 struct btrfs_disk_key disk_key;
2276 u64 offset; 2271 u64 offset;
2277 2272
2278 btrfs_item_key(leaf, &disk_key, slot); 2273 btrfs_item_key(leaf, &disk_key, slot);
2279 2274
2280 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { 2275 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2281 unsigned long ptr; 2276 unsigned long ptr;
2282 struct btrfs_file_extent_item *fi; 2277 struct btrfs_file_extent_item *fi;
2283 2278
2284 fi = btrfs_item_ptr(leaf, slot, 2279 fi = btrfs_item_ptr(leaf, slot,
2285 struct btrfs_file_extent_item); 2280 struct btrfs_file_extent_item);
2286 fi = (struct btrfs_file_extent_item *)( 2281 fi = (struct btrfs_file_extent_item *)(
2287 (unsigned long)fi - size_diff); 2282 (unsigned long)fi - size_diff);
2288 2283
2289 if (btrfs_file_extent_type(leaf, fi) == 2284 if (btrfs_file_extent_type(leaf, fi) ==
2290 BTRFS_FILE_EXTENT_INLINE) { 2285 BTRFS_FILE_EXTENT_INLINE) {
2291 ptr = btrfs_item_ptr_offset(leaf, slot); 2286 ptr = btrfs_item_ptr_offset(leaf, slot);
2292 memmove_extent_buffer(leaf, ptr, 2287 memmove_extent_buffer(leaf, ptr,
2293 (unsigned long)fi, 2288 (unsigned long)fi,
2294 offsetof(struct btrfs_file_extent_item, 2289 offsetof(struct btrfs_file_extent_item,
2295 disk_bytenr)); 2290 disk_bytenr));
2296 } 2291 }
2297 } 2292 }
2298 2293
2299 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 2294 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2300 data_end + size_diff, btrfs_leaf_data(leaf) + 2295 data_end + size_diff, btrfs_leaf_data(leaf) +
2301 data_end, old_data_start - data_end); 2296 data_end, old_data_start - data_end);
2302 2297
2303 offset = btrfs_disk_key_offset(&disk_key); 2298 offset = btrfs_disk_key_offset(&disk_key);
2304 btrfs_set_disk_key_offset(&disk_key, offset + size_diff); 2299 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2305 btrfs_set_item_key(leaf, &disk_key, slot); 2300 btrfs_set_item_key(leaf, &disk_key, slot);
2306 if (slot == 0) 2301 if (slot == 0)
2307 fixup_low_keys(trans, root, path, &disk_key, 1); 2302 fixup_low_keys(trans, root, path, &disk_key, 1);
2308 } 2303 }
2309 2304
2310 item = btrfs_item_nr(leaf, slot); 2305 item = btrfs_item_nr(leaf, slot);
2311 btrfs_set_item_size(leaf, item, new_size); 2306 btrfs_set_item_size(leaf, item, new_size);
2312 btrfs_mark_buffer_dirty(leaf); 2307 btrfs_mark_buffer_dirty(leaf);
2313 2308
2314 ret = 0; 2309 ret = 0;
2315 if (btrfs_leaf_free_space(root, leaf) < 0) { 2310 if (btrfs_leaf_free_space(root, leaf) < 0) {
2316 btrfs_print_leaf(root, leaf); 2311 btrfs_print_leaf(root, leaf);
2317 BUG(); 2312 BUG();
2318 } 2313 }
2319 return ret; 2314 return ret;
2320 } 2315 }
2321 2316
2322 int btrfs_extend_item(struct btrfs_trans_handle *trans, 2317 int btrfs_extend_item(struct btrfs_trans_handle *trans,
2323 struct btrfs_root *root, struct btrfs_path *path, 2318 struct btrfs_root *root, struct btrfs_path *path,
2324 u32 data_size) 2319 u32 data_size)
2325 { 2320 {
2326 int ret = 0; 2321 int ret = 0;
2327 int slot; 2322 int slot;
2328 int slot_orig; 2323 int slot_orig;
2329 struct extent_buffer *leaf; 2324 struct extent_buffer *leaf;
2330 struct btrfs_item *item; 2325 struct btrfs_item *item;
2331 u32 nritems; 2326 u32 nritems;
2332 unsigned int data_end; 2327 unsigned int data_end;
2333 unsigned int old_data; 2328 unsigned int old_data;
2334 unsigned int old_size; 2329 unsigned int old_size;
2335 int i; 2330 int i;
2336 2331
2337 slot_orig = path->slots[0]; 2332 slot_orig = path->slots[0];
2338 leaf = path->nodes[0]; 2333 leaf = path->nodes[0];
2339 2334
2340 nritems = btrfs_header_nritems(leaf); 2335 nritems = btrfs_header_nritems(leaf);
2341 data_end = leaf_data_end(root, leaf); 2336 data_end = leaf_data_end(root, leaf);
2342 2337
2343 if (btrfs_leaf_free_space(root, leaf) < data_size) { 2338 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2344 btrfs_print_leaf(root, leaf); 2339 btrfs_print_leaf(root, leaf);
2345 BUG(); 2340 BUG();
2346 } 2341 }
2347 slot = path->slots[0]; 2342 slot = path->slots[0];
2348 old_data = btrfs_item_end_nr(leaf, slot); 2343 old_data = btrfs_item_end_nr(leaf, slot);
2349 2344
2350 BUG_ON(slot < 0); 2345 BUG_ON(slot < 0);
2351 if (slot >= nritems) { 2346 if (slot >= nritems) {
2352 btrfs_print_leaf(root, leaf); 2347 btrfs_print_leaf(root, leaf);
2353 printk("slot %d too large, nritems %d\n", slot, nritems); 2348 printk("slot %d too large, nritems %d\n", slot, nritems);
2354 BUG_ON(1); 2349 BUG_ON(1);
2355 } 2350 }
2356 2351
2357 /* 2352 /*
2358 * item0..itemN ... dataN.offset..dataN.size .. data0.size 2353 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2359 */ 2354 */
2360 /* first correct the data pointers */ 2355 /* first correct the data pointers */
2361 for (i = slot; i < nritems; i++) { 2356 for (i = slot; i < nritems; i++) {
2362 u32 ioff; 2357 u32 ioff;
2363 item = btrfs_item_nr(leaf, i); 2358 item = btrfs_item_nr(leaf, i);
2364 2359
2365 if (!leaf->map_token) { 2360 if (!leaf->map_token) {
2366 map_extent_buffer(leaf, (unsigned long)item, 2361 map_extent_buffer(leaf, (unsigned long)item,
2367 sizeof(struct btrfs_item), 2362 sizeof(struct btrfs_item),
2368 &leaf->map_token, &leaf->kaddr, 2363 &leaf->map_token, &leaf->kaddr,
2369 &leaf->map_start, &leaf->map_len, 2364 &leaf->map_start, &leaf->map_len,
2370 KM_USER1); 2365 KM_USER1);
2371 } 2366 }
2372 ioff = btrfs_item_offset(leaf, item); 2367 ioff = btrfs_item_offset(leaf, item);
2373 btrfs_set_item_offset(leaf, item, ioff - data_size); 2368 btrfs_set_item_offset(leaf, item, ioff - data_size);
2374 } 2369 }
2375 2370
2376 if (leaf->map_token) { 2371 if (leaf->map_token) {
2377 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 2372 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2378 leaf->map_token = NULL; 2373 leaf->map_token = NULL;
2379 } 2374 }
2380 2375
2381 /* shift the data */ 2376 /* shift the data */
2382 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 2377 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2383 data_end - data_size, btrfs_leaf_data(leaf) + 2378 data_end - data_size, btrfs_leaf_data(leaf) +
2384 data_end, old_data - data_end); 2379 data_end, old_data - data_end);
2385 2380
2386 data_end = old_data; 2381 data_end = old_data;
2387 old_size = btrfs_item_size_nr(leaf, slot); 2382 old_size = btrfs_item_size_nr(leaf, slot);
2388 item = btrfs_item_nr(leaf, slot); 2383 item = btrfs_item_nr(leaf, slot);
2389 btrfs_set_item_size(leaf, item, old_size + data_size); 2384 btrfs_set_item_size(leaf, item, old_size + data_size);
2390 btrfs_mark_buffer_dirty(leaf); 2385 btrfs_mark_buffer_dirty(leaf);
2391 2386
2392 ret = 0; 2387 ret = 0;
2393 if (btrfs_leaf_free_space(root, leaf) < 0) { 2388 if (btrfs_leaf_free_space(root, leaf) < 0) {
2394 btrfs_print_leaf(root, leaf); 2389 btrfs_print_leaf(root, leaf);
2395 BUG(); 2390 BUG();
2396 } 2391 }
2397 return ret; 2392 return ret;
2398 } 2393 }
2399 2394
2400 /* 2395 /*
2401 * Given a key and some data, insert an item into the tree. 2396 * Given a key and some data, insert an item into the tree.
2402 * This does all the path init required, making room in the tree if needed. 2397 * This does all the path init required, making room in the tree if needed.
2403 */ 2398 */
2404 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, 2399 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2405 struct btrfs_root *root, 2400 struct btrfs_root *root,
2406 struct btrfs_path *path, 2401 struct btrfs_path *path,
2407 struct btrfs_key *cpu_key, u32 *data_size, 2402 struct btrfs_key *cpu_key, u32 *data_size,
2408 int nr) 2403 int nr)
2409 { 2404 {
2410 struct extent_buffer *leaf; 2405 struct extent_buffer *leaf;
2411 struct btrfs_item *item; 2406 struct btrfs_item *item;
2412 int ret = 0; 2407 int ret = 0;
2413 int slot; 2408 int slot;
2414 int slot_orig; 2409 int slot_orig;
2415 int i; 2410 int i;
2416 u32 nritems; 2411 u32 nritems;
2417 u32 total_size = 0; 2412 u32 total_size = 0;
2418 u32 total_data = 0; 2413 u32 total_data = 0;
2419 unsigned int data_end; 2414 unsigned int data_end;
2420 struct btrfs_disk_key disk_key; 2415 struct btrfs_disk_key disk_key;
2421 2416
2422 for (i = 0; i < nr; i++) { 2417 for (i = 0; i < nr; i++) {
2423 total_data += data_size[i]; 2418 total_data += data_size[i];
2424 } 2419 }
2425 2420
2426 /* create a root if there isn't one */ 2421 /* create a root if there isn't one */
2427 if (!root->node) 2422 if (!root->node)
2428 BUG(); 2423 BUG();
2429 2424
2430 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item); 2425 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2431 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); 2426 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2432 if (ret == 0) { 2427 if (ret == 0) {
2433 return -EEXIST; 2428 return -EEXIST;
2434 } 2429 }
2435 if (ret < 0) 2430 if (ret < 0)
2436 goto out; 2431 goto out;
2437 2432
2438 slot_orig = path->slots[0]; 2433 slot_orig = path->slots[0];
2439 leaf = path->nodes[0]; 2434 leaf = path->nodes[0];
2440 2435
2441 nritems = btrfs_header_nritems(leaf); 2436 nritems = btrfs_header_nritems(leaf);
2442 data_end = leaf_data_end(root, leaf); 2437 data_end = leaf_data_end(root, leaf);
2443 2438
2444 if (btrfs_leaf_free_space(root, leaf) < 2439 if (btrfs_leaf_free_space(root, leaf) <
2445 sizeof(struct btrfs_item) + total_size) { 2440 sizeof(struct btrfs_item) + total_size) {
2446 btrfs_print_leaf(root, leaf); 2441 btrfs_print_leaf(root, leaf);
2447 printk("not enough freespace need %u have %d\n", 2442 printk("not enough freespace need %u have %d\n",
2448 total_size, btrfs_leaf_free_space(root, leaf)); 2443 total_size, btrfs_leaf_free_space(root, leaf));
2449 BUG(); 2444 BUG();
2450 } 2445 }
2451 2446
2452 slot = path->slots[0]; 2447 slot = path->slots[0];
2453 BUG_ON(slot < 0); 2448 BUG_ON(slot < 0);
2454 2449
2455 if (slot != nritems) { 2450 if (slot != nritems) {
2456 int i; 2451 int i;
2457 unsigned int old_data = btrfs_item_end_nr(leaf, slot); 2452 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2458 2453
2459 if (old_data < data_end) { 2454 if (old_data < data_end) {
2460 btrfs_print_leaf(root, leaf); 2455 btrfs_print_leaf(root, leaf);
2461 printk("slot %d old_data %d data_end %d\n", 2456 printk("slot %d old_data %d data_end %d\n",
2462 slot, old_data, data_end); 2457 slot, old_data, data_end);
2463 BUG_ON(1); 2458 BUG_ON(1);
2464 } 2459 }
2465 /* 2460 /*
2466 * item0..itemN ... dataN.offset..dataN.size .. data0.size 2461 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2467 */ 2462 */
2468 /* first correct the data pointers */ 2463 /* first correct the data pointers */
2469 WARN_ON(leaf->map_token); 2464 WARN_ON(leaf->map_token);
2470 for (i = slot; i < nritems; i++) { 2465 for (i = slot; i < nritems; i++) {
2471 u32 ioff; 2466 u32 ioff;
2472 2467
2473 item = btrfs_item_nr(leaf, i); 2468 item = btrfs_item_nr(leaf, i);
2474 if (!leaf->map_token) { 2469 if (!leaf->map_token) {
2475 map_extent_buffer(leaf, (unsigned long)item, 2470 map_extent_buffer(leaf, (unsigned long)item,
2476 sizeof(struct btrfs_item), 2471 sizeof(struct btrfs_item),
2477 &leaf->map_token, &leaf->kaddr, 2472 &leaf->map_token, &leaf->kaddr,
2478 &leaf->map_start, &leaf->map_len, 2473 &leaf->map_start, &leaf->map_len,
2479 KM_USER1); 2474 KM_USER1);
2480 } 2475 }
2481 2476
2482 ioff = btrfs_item_offset(leaf, item); 2477 ioff = btrfs_item_offset(leaf, item);
2483 btrfs_set_item_offset(leaf, item, ioff - total_data); 2478 btrfs_set_item_offset(leaf, item, ioff - total_data);
2484 } 2479 }
2485 if (leaf->map_token) { 2480 if (leaf->map_token) {
2486 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 2481 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2487 leaf->map_token = NULL; 2482 leaf->map_token = NULL;
2488 } 2483 }
2489 2484
2490 /* shift the items */ 2485 /* shift the items */
2491 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), 2486 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2492 btrfs_item_nr_offset(slot), 2487 btrfs_item_nr_offset(slot),
2493 (nritems - slot) * sizeof(struct btrfs_item)); 2488 (nritems - slot) * sizeof(struct btrfs_item));
2494 2489
2495 /* shift the data */ 2490 /* shift the data */
2496 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 2491 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2497 data_end - total_data, btrfs_leaf_data(leaf) + 2492 data_end - total_data, btrfs_leaf_data(leaf) +
2498 data_end, old_data - data_end); 2493 data_end, old_data - data_end);
2499 data_end = old_data; 2494 data_end = old_data;
2500 } 2495 }
2501 2496
2502 /* setup the item for the new data */ 2497 /* setup the item for the new data */
2503 for (i = 0; i < nr; i++) { 2498 for (i = 0; i < nr; i++) {
2504 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); 2499 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2505 btrfs_set_item_key(leaf, &disk_key, slot + i); 2500 btrfs_set_item_key(leaf, &disk_key, slot + i);
2506 item = btrfs_item_nr(leaf, slot + i); 2501 item = btrfs_item_nr(leaf, slot + i);
2507 btrfs_set_item_offset(leaf, item, data_end - data_size[i]); 2502 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2508 data_end -= data_size[i]; 2503 data_end -= data_size[i];
2509 btrfs_set_item_size(leaf, item, data_size[i]); 2504 btrfs_set_item_size(leaf, item, data_size[i]);
2510 } 2505 }
2511 btrfs_set_header_nritems(leaf, nritems + nr); 2506 btrfs_set_header_nritems(leaf, nritems + nr);
2512 btrfs_mark_buffer_dirty(leaf); 2507 btrfs_mark_buffer_dirty(leaf);
2513 2508
2514 ret = 0; 2509 ret = 0;
2515 if (slot == 0) { 2510 if (slot == 0) {
2516 btrfs_cpu_key_to_disk(&disk_key, cpu_key); 2511 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2517 ret = fixup_low_keys(trans, root, path, &disk_key, 1); 2512 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
2518 } 2513 }
2519 2514
2520 if (btrfs_leaf_free_space(root, leaf) < 0) { 2515 if (btrfs_leaf_free_space(root, leaf) < 0) {
2521 btrfs_print_leaf(root, leaf); 2516 btrfs_print_leaf(root, leaf);
2522 BUG(); 2517 BUG();
2523 } 2518 }
2524 2519
2525 out: 2520 out:
2526 return ret; 2521 return ret;
2527 } 2522 }
2528 2523
2529 /* 2524 /*
2530 * Given a key and some data, insert an item into the tree. 2525 * Given a key and some data, insert an item into the tree.
2531 * This does all the path init required, making room in the tree if needed. 2526 * This does all the path init required, making room in the tree if needed.
2532 */ 2527 */
2533 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root 2528 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2534 *root, struct btrfs_key *cpu_key, void *data, u32 2529 *root, struct btrfs_key *cpu_key, void *data, u32
2535 data_size) 2530 data_size)
2536 { 2531 {
2537 int ret = 0; 2532 int ret = 0;
2538 struct btrfs_path *path; 2533 struct btrfs_path *path;
2539 struct extent_buffer *leaf; 2534 struct extent_buffer *leaf;
2540 unsigned long ptr; 2535 unsigned long ptr;
2541 2536
2542 path = btrfs_alloc_path(); 2537 path = btrfs_alloc_path();
2543 BUG_ON(!path); 2538 BUG_ON(!path);
2544 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); 2539 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2545 if (!ret) { 2540 if (!ret) {
2546 leaf = path->nodes[0]; 2541 leaf = path->nodes[0];
2547 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 2542 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2548 write_extent_buffer(leaf, data, ptr, data_size); 2543 write_extent_buffer(leaf, data, ptr, data_size);
2549 btrfs_mark_buffer_dirty(leaf); 2544 btrfs_mark_buffer_dirty(leaf);
2550 } 2545 }
2551 btrfs_free_path(path); 2546 btrfs_free_path(path);
2552 return ret; 2547 return ret;
2553 } 2548 }
2554 2549
2555 /* 2550 /*
2556 * delete the pointer from a given node. 2551 * delete the pointer from a given node.
2557 * 2552 *
2558 * If the delete empties a node, the node is removed from the tree, 2553 * If the delete empties a node, the node is removed from the tree,
2559 * continuing all the way the root if required. The root is converted into 2554 * continuing all the way the root if required. The root is converted into
2560 * a leaf if all the nodes are emptied. 2555 * a leaf if all the nodes are emptied.
2561 */ 2556 */
2562 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, 2557 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2563 struct btrfs_path *path, int level, int slot) 2558 struct btrfs_path *path, int level, int slot)
2564 { 2559 {
2565 struct extent_buffer *parent = path->nodes[level]; 2560 struct extent_buffer *parent = path->nodes[level];
2566 u32 nritems; 2561 u32 nritems;
2567 int ret = 0; 2562 int ret = 0;
2568 int wret; 2563 int wret;
2569 2564
2570 nritems = btrfs_header_nritems(parent); 2565 nritems = btrfs_header_nritems(parent);
2571 if (slot != nritems -1) { 2566 if (slot != nritems -1) {
2572 memmove_extent_buffer(parent, 2567 memmove_extent_buffer(parent,
2573 btrfs_node_key_ptr_offset(slot), 2568 btrfs_node_key_ptr_offset(slot),
2574 btrfs_node_key_ptr_offset(slot + 1), 2569 btrfs_node_key_ptr_offset(slot + 1),
2575 sizeof(struct btrfs_key_ptr) * 2570 sizeof(struct btrfs_key_ptr) *
2576 (nritems - slot - 1)); 2571 (nritems - slot - 1));
2577 } 2572 }
2578 nritems--; 2573 nritems--;
2579 btrfs_set_header_nritems(parent, nritems); 2574 btrfs_set_header_nritems(parent, nritems);
2580 if (nritems == 0 && parent == root->node) { 2575 if (nritems == 0 && parent == root->node) {
2581 BUG_ON(btrfs_header_level(root->node) != 1); 2576 BUG_ON(btrfs_header_level(root->node) != 1);
2582 /* just turn the root into a leaf and break */ 2577 /* just turn the root into a leaf and break */
2583 btrfs_set_header_level(root->node, 0); 2578 btrfs_set_header_level(root->node, 0);
2584 } else if (slot == 0) { 2579 } else if (slot == 0) {
2585 struct btrfs_disk_key disk_key; 2580 struct btrfs_disk_key disk_key;
2586 2581
2587 btrfs_node_key(parent, &disk_key, 0); 2582 btrfs_node_key(parent, &disk_key, 0);
2588 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1); 2583 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
2589 if (wret) 2584 if (wret)
2590 ret = wret; 2585 ret = wret;
2591 } 2586 }
2592 btrfs_mark_buffer_dirty(parent); 2587 btrfs_mark_buffer_dirty(parent);
2593 return ret; 2588 return ret;
2594 } 2589 }
2595 2590
2596 /* 2591 /*
2597 * delete the item at the leaf level in path. If that empties 2592 * delete the item at the leaf level in path. If that empties
2598 * the leaf, remove it from the tree 2593 * the leaf, remove it from the tree
2599 */ 2594 */
2600 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, 2595 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2601 struct btrfs_path *path, int slot, int nr) 2596 struct btrfs_path *path, int slot, int nr)
2602 { 2597 {
2603 struct extent_buffer *leaf; 2598 struct extent_buffer *leaf;
2604 struct btrfs_item *item; 2599 struct btrfs_item *item;
2605 int last_off; 2600 int last_off;
2606 int dsize = 0; 2601 int dsize = 0;
2607 int ret = 0; 2602 int ret = 0;
2608 int wret; 2603 int wret;
2609 int i; 2604 int i;
2610 u32 nritems; 2605 u32 nritems;
2611 2606
2612 leaf = path->nodes[0]; 2607 leaf = path->nodes[0];
2613 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); 2608 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2614 2609
2615 for (i = 0; i < nr; i++) 2610 for (i = 0; i < nr; i++)
2616 dsize += btrfs_item_size_nr(leaf, slot + i); 2611 dsize += btrfs_item_size_nr(leaf, slot + i);
2617 2612
2618 nritems = btrfs_header_nritems(leaf); 2613 nritems = btrfs_header_nritems(leaf);
2619 2614
2620 if (slot + nr != nritems) { 2615 if (slot + nr != nritems) {
2621 int i; 2616 int i;
2622 int data_end = leaf_data_end(root, leaf); 2617 int data_end = leaf_data_end(root, leaf);
2623 2618
2624 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 2619 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2625 data_end + dsize, 2620 data_end + dsize,
2626 btrfs_leaf_data(leaf) + data_end, 2621 btrfs_leaf_data(leaf) + data_end,
2627 last_off - data_end); 2622 last_off - data_end);
2628 2623
2629 for (i = slot + nr; i < nritems; i++) { 2624 for (i = slot + nr; i < nritems; i++) {
2630 u32 ioff; 2625 u32 ioff;
2631 2626
2632 item = btrfs_item_nr(leaf, i); 2627 item = btrfs_item_nr(leaf, i);
2633 if (!leaf->map_token) { 2628 if (!leaf->map_token) {
2634 map_extent_buffer(leaf, (unsigned long)item, 2629 map_extent_buffer(leaf, (unsigned long)item,
2635 sizeof(struct btrfs_item), 2630 sizeof(struct btrfs_item),
2636 &leaf->map_token, &leaf->kaddr, 2631 &leaf->map_token, &leaf->kaddr,
2637 &leaf->map_start, &leaf->map_len, 2632 &leaf->map_start, &leaf->map_len,
2638 KM_USER1); 2633 KM_USER1);
2639 } 2634 }
2640 ioff = btrfs_item_offset(leaf, item); 2635 ioff = btrfs_item_offset(leaf, item);
2641 btrfs_set_item_offset(leaf, item, ioff + dsize); 2636 btrfs_set_item_offset(leaf, item, ioff + dsize);
2642 } 2637 }
2643 2638
2644 if (leaf->map_token) { 2639 if (leaf->map_token) {
2645 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 2640 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2646 leaf->map_token = NULL; 2641 leaf->map_token = NULL;
2647 } 2642 }
2648 2643
2649 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), 2644 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2650 btrfs_item_nr_offset(slot + nr), 2645 btrfs_item_nr_offset(slot + nr),
2651 sizeof(struct btrfs_item) * 2646 sizeof(struct btrfs_item) *
2652 (nritems - slot - nr)); 2647 (nritems - slot - nr));
2653 } 2648 }
2654 btrfs_set_header_nritems(leaf, nritems - nr); 2649 btrfs_set_header_nritems(leaf, nritems - nr);
2655 nritems -= nr; 2650 nritems -= nr;
2656 2651
2657 /* delete the leaf if we've emptied it */ 2652 /* delete the leaf if we've emptied it */
2658 if (nritems == 0) { 2653 if (nritems == 0) {
2659 if (leaf == root->node) { 2654 if (leaf == root->node) {
2660 btrfs_set_header_level(leaf, 0); 2655 btrfs_set_header_level(leaf, 0);
2661 } else { 2656 } else {
2662 u64 root_gen = btrfs_header_generation(path->nodes[1]); 2657 u64 root_gen = btrfs_header_generation(path->nodes[1]);
2663 clean_tree_block(trans, root, leaf); 2658 clean_tree_block(trans, root, leaf);
2664 wret = del_ptr(trans, root, path, 1, path->slots[1]); 2659 wret = del_ptr(trans, root, path, 1, path->slots[1]);
2665 if (wret) 2660 if (wret)
2666 ret = wret; 2661 ret = wret;
2667 wret = btrfs_free_extent(trans, root, 2662 wret = btrfs_free_extent(trans, root,
2668 leaf->start, leaf->len, 2663 leaf->start, leaf->len,
2669 btrfs_header_owner(path->nodes[1]), 2664 btrfs_header_owner(path->nodes[1]),
2670 root_gen, 0, 0, 1); 2665 root_gen, 0, 0, 1);
2671 if (wret) 2666 if (wret)
2672 ret = wret; 2667 ret = wret;
2673 } 2668 }
2674 } else { 2669 } else {
2675 int used = leaf_space_used(leaf, 0, nritems); 2670 int used = leaf_space_used(leaf, 0, nritems);
2676 if (slot == 0) { 2671 if (slot == 0) {
2677 struct btrfs_disk_key disk_key; 2672 struct btrfs_disk_key disk_key;
2678 2673
2679 btrfs_item_key(leaf, &disk_key, 0); 2674 btrfs_item_key(leaf, &disk_key, 0);
2680 wret = fixup_low_keys(trans, root, path, 2675 wret = fixup_low_keys(trans, root, path,
2681 &disk_key, 1); 2676 &disk_key, 1);
2682 if (wret) 2677 if (wret)
2683 ret = wret; 2678 ret = wret;
2684 } 2679 }
2685 2680
2686 /* delete the leaf if it is mostly empty */ 2681 /* delete the leaf if it is mostly empty */
2687 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) { 2682 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
2688 /* push_leaf_left fixes the path. 2683 /* push_leaf_left fixes the path.
2689 * make sure the path still points to our leaf 2684 * make sure the path still points to our leaf
2690 * for possible call to del_ptr below 2685 * for possible call to del_ptr below
2691 */ 2686 */
2692 slot = path->slots[1]; 2687 slot = path->slots[1];
2693 extent_buffer_get(leaf); 2688 extent_buffer_get(leaf);
2694 2689
2695 wret = push_leaf_left(trans, root, path, 1, 1); 2690 wret = push_leaf_left(trans, root, path, 1, 1);
2696 if (wret < 0 && wret != -ENOSPC) 2691 if (wret < 0 && wret != -ENOSPC)
2697 ret = wret; 2692 ret = wret;
2698 2693
2699 if (path->nodes[0] == leaf && 2694 if (path->nodes[0] == leaf &&
2700 btrfs_header_nritems(leaf)) { 2695 btrfs_header_nritems(leaf)) {
2701 wret = push_leaf_right(trans, root, path, 1, 1); 2696 wret = push_leaf_right(trans, root, path, 1, 1);
2702 if (wret < 0 && wret != -ENOSPC) 2697 if (wret < 0 && wret != -ENOSPC)
2703 ret = wret; 2698 ret = wret;
2704 } 2699 }
2705 2700
2706 if (btrfs_header_nritems(leaf) == 0) { 2701 if (btrfs_header_nritems(leaf) == 0) {
2707 u64 root_gen; 2702 u64 root_gen;
2708 u64 bytenr = leaf->start; 2703 u64 bytenr = leaf->start;
2709 u32 blocksize = leaf->len; 2704 u32 blocksize = leaf->len;
2710 2705
2711 root_gen = btrfs_header_generation( 2706 root_gen = btrfs_header_generation(
2712 path->nodes[1]); 2707 path->nodes[1]);
2713 2708
2714 clean_tree_block(trans, root, leaf); 2709 clean_tree_block(trans, root, leaf);
2715 2710
2716 wret = del_ptr(trans, root, path, 1, slot); 2711 wret = del_ptr(trans, root, path, 1, slot);
2717 if (wret) 2712 if (wret)
2718 ret = wret; 2713 ret = wret;
2719 2714
2720 free_extent_buffer(leaf); 2715 free_extent_buffer(leaf);
2721 wret = btrfs_free_extent(trans, root, bytenr, 2716 wret = btrfs_free_extent(trans, root, bytenr,
2722 blocksize, 2717 blocksize,
2723 btrfs_header_owner(path->nodes[1]), 2718 btrfs_header_owner(path->nodes[1]),
2724 root_gen, 0, 0, 1); 2719 root_gen, 0, 0, 1);
2725 if (wret) 2720 if (wret)
2726 ret = wret; 2721 ret = wret;
2727 } else { 2722 } else {
2728 btrfs_mark_buffer_dirty(leaf); 2723 btrfs_mark_buffer_dirty(leaf);
2729 free_extent_buffer(leaf); 2724 free_extent_buffer(leaf);
2730 } 2725 }
2731 } else { 2726 } else {
2732 btrfs_mark_buffer_dirty(leaf); 2727 btrfs_mark_buffer_dirty(leaf);
2733 } 2728 }
2734 } 2729 }
2735 return ret; 2730 return ret;
2736 } 2731 }
2737 2732
2738 /* 2733 /*
2739 * walk up the tree as far as required to find the previous leaf. 2734 * walk up the tree as far as required to find the previous leaf.
2740 * returns 0 if it found something or 1 if there are no lesser leaves. 2735 * returns 0 if it found something or 1 if there are no lesser leaves.
2741 * returns < 0 on io errors. 2736 * returns < 0 on io errors.
2742 */ 2737 */
2743 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) 2738 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2744 { 2739 {
2745 u64 bytenr;
2746 int slot; 2740 int slot;
2747 int level = 1; 2741 int level = 1;
2748 struct extent_buffer *c; 2742 struct extent_buffer *c;
2749 struct extent_buffer *next = NULL; 2743 struct extent_buffer *next = NULL;
2750 2744
2751 while(level < BTRFS_MAX_LEVEL) { 2745 while(level < BTRFS_MAX_LEVEL) {
2752 if (!path->nodes[level]) 2746 if (!path->nodes[level])
2753 return 1; 2747 return 1;
2754 2748
2755 slot = path->slots[level]; 2749 slot = path->slots[level];
2756 c = path->nodes[level]; 2750 c = path->nodes[level];
2757 if (slot == 0) { 2751 if (slot == 0) {
2758 level++; 2752 level++;
2759 if (level == BTRFS_MAX_LEVEL) 2753 if (level == BTRFS_MAX_LEVEL)
2760 return 1; 2754 return 1;
2761 continue; 2755 continue;
2762 } 2756 }
2763 slot--; 2757 slot--;
2764 2758
2765 bytenr = btrfs_node_blockptr(c, slot);
2766 if (next) 2759 if (next)
2767 free_extent_buffer(next); 2760 free_extent_buffer(next);
2768 2761
2769 next = read_tree_block(root, bytenr, 2762 next = read_node_slot(root, c, slot);
2770 btrfs_level_size(root, level - 1));
2771 break; 2763 break;
2772 } 2764 }
2773 path->slots[level] = slot; 2765 path->slots[level] = slot;
2774 while(1) { 2766 while(1) {
2775 level--; 2767 level--;
2776 c = path->nodes[level]; 2768 c = path->nodes[level];
2777 free_extent_buffer(c); 2769 free_extent_buffer(c);
2778 slot = btrfs_header_nritems(next); 2770 slot = btrfs_header_nritems(next);
2779 if (slot != 0) 2771 if (slot != 0)
2780 slot--; 2772 slot--;
2781 path->nodes[level] = next; 2773 path->nodes[level] = next;
2782 path->slots[level] = slot; 2774 path->slots[level] = slot;
2783 if (!level) 2775 if (!level)
2784 break; 2776 break;
2785 next = read_tree_block(root, btrfs_node_blockptr(next, slot), 2777 next = read_node_slot(root, next, slot);
2786 btrfs_level_size(root, level - 1));
2787 } 2778 }
2788 return 0; 2779 return 0;
2789 } 2780 }
2790 2781
2791 /* 2782 /*
2792 * walk up the tree as far as required to find the next leaf. 2783 * walk up the tree as far as required to find the next leaf.
2793 * returns 0 if it found something or 1 if there are no greater leaves. 2784 * returns 0 if it found something or 1 if there are no greater leaves.
2794 * returns < 0 on io errors. 2785 * returns < 0 on io errors.
2795 */ 2786 */
2796 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) 2787 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2797 { 2788 {
2798 int slot; 2789 int slot;
2799 int level = 1; 2790 int level = 1;
2800 u64 bytenr;
2801 struct extent_buffer *c; 2791 struct extent_buffer *c;
2802 struct extent_buffer *next = NULL; 2792 struct extent_buffer *next = NULL;
2803 2793
2804 while(level < BTRFS_MAX_LEVEL) { 2794 while(level < BTRFS_MAX_LEVEL) {
2805 if (!path->nodes[level]) 2795 if (!path->nodes[level])
2806 return 1; 2796 return 1;
2807 2797
2808 slot = path->slots[level] + 1; 2798 slot = path->slots[level] + 1;
2809 c = path->nodes[level]; 2799 c = path->nodes[level];
2810 if (slot >= btrfs_header_nritems(c)) { 2800 if (slot >= btrfs_header_nritems(c)) {
2811 level++; 2801 level++;
2812 if (level == BTRFS_MAX_LEVEL) 2802 if (level == BTRFS_MAX_LEVEL)
2813 return 1; 2803 return 1;
2814 continue; 2804 continue;
2815 } 2805 }
2816 2806
2817 bytenr = btrfs_node_blockptr(c, slot);
2818 if (next) 2807 if (next)
2819 free_extent_buffer(next); 2808 free_extent_buffer(next);
2820 2809
2821 if (path->reada) 2810 if (path->reada)
2822 reada_for_search(root, path, level, slot, 0); 2811 reada_for_search(root, path, level, slot, 0);
2823 2812
2824 next = read_tree_block(root, bytenr, 2813 next = read_node_slot(root, c, slot);
2825 btrfs_level_size(root, level -1));
2826 break; 2814 break;
2827 } 2815 }
2828 path->slots[level] = slot; 2816 path->slots[level] = slot;
2829 while(1) { 2817 while(1) {
2830 level--; 2818 level--;
2831 c = path->nodes[level]; 2819 c = path->nodes[level];
2832 free_extent_buffer(c); 2820 free_extent_buffer(c);
2833 path->nodes[level] = next; 2821 path->nodes[level] = next;
2834 path->slots[level] = 0; 2822 path->slots[level] = 0;
2835 if (!level) 2823 if (!level)
2836 break; 2824 break;
2837 if (path->reada) 2825 if (path->reada)
2838 reada_for_search(root, path, level, 0, 0); 2826 reada_for_search(root, path, level, 0, 0);
2839 next = read_tree_block(root, btrfs_node_blockptr(next, 0), 2827 next = read_node_slot(root, next, 0);
2840 btrfs_level_size(root, level - 1));
2841 } 2828 }
2842 return 0; 2829 return 0;
2843 } 2830 }
2844 2831
2845 int btrfs_previous_item(struct btrfs_root *root, 2832 int btrfs_previous_item(struct btrfs_root *root,
2846 struct btrfs_path *path, u64 min_objectid, 2833 struct btrfs_path *path, u64 min_objectid,
2847 int type) 2834 int type)
2848 { 2835 {
2849 struct btrfs_key found_key; 2836 struct btrfs_key found_key;
2850 struct extent_buffer *leaf; 2837 struct extent_buffer *leaf;
2851 int ret; 2838 int ret;
2852 2839
2853 while(1) { 2840 while(1) {
2854 if (path->slots[0] == 0) { 2841 if (path->slots[0] == 0) {
2855 ret = btrfs_prev_leaf(root, path); 2842 ret = btrfs_prev_leaf(root, path);
2856 if (ret != 0) 2843 if (ret != 0)
2857 return ret; 2844 return ret;
2858 } else { 2845 } else {
2859 path->slots[0]--; 2846 path->slots[0]--;
2860 } 2847 }
2861 leaf = path->nodes[0]; 2848 leaf = path->nodes[0];
1 /* 1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved. 2 * Copyright (C) 2007 Oracle. All rights reserved.
3 * 3 *
4 * This program is free software; you can redistribute it and/or 4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public 5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation. 6 * License v2 as published by the Free Software Foundation.
7 * 7 *
8 * This program is distributed in the hope that it will be useful, 8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details. 11 * General Public License for more details.
12 * 12 *
13 * You should have received a copy of the GNU General Public 13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the 14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA. 16 * Boston, MA 021110-1307, USA.
17 */ 17 */
18 18
19 #include <linux/fs.h> 19 #include <linux/fs.h>
20 #include <linux/blkdev.h> 20 #include <linux/blkdev.h>
21 #include <linux/scatterlist.h> 21 #include <linux/scatterlist.h>
22 #include <linux/swap.h> 22 #include <linux/swap.h>
23 #include <linux/radix-tree.h> 23 #include <linux/radix-tree.h>
24 #include <linux/writeback.h> 24 #include <linux/writeback.h>
25 #include <linux/buffer_head.h> // for block_sync_page 25 #include <linux/buffer_head.h> // for block_sync_page
26 #include <linux/workqueue.h> 26 #include <linux/workqueue.h>
27 #include "crc32c.h" 27 #include "crc32c.h"
28 #include "ctree.h" 28 #include "ctree.h"
29 #include "disk-io.h" 29 #include "disk-io.h"
30 #include "transaction.h" 30 #include "transaction.h"
31 #include "btrfs_inode.h" 31 #include "btrfs_inode.h"
32 #include "volumes.h" 32 #include "volumes.h"
33 #include "print-tree.h" 33 #include "print-tree.h"
34 34
35 #if 0 35 #if 0
36 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf) 36 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
37 { 37 {
38 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) { 38 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
39 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n", 39 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
40 (unsigned long long)extent_buffer_blocknr(buf), 40 (unsigned long long)extent_buffer_blocknr(buf),
41 (unsigned long long)btrfs_header_blocknr(buf)); 41 (unsigned long long)btrfs_header_blocknr(buf));
42 return 1; 42 return 1;
43 } 43 }
44 return 0; 44 return 0;
45 } 45 }
46 #endif 46 #endif
47 47
48 static struct extent_io_ops btree_extent_io_ops; 48 static struct extent_io_ops btree_extent_io_ops;
49 static struct workqueue_struct *end_io_workqueue; 49 static struct workqueue_struct *end_io_workqueue;
50 static struct workqueue_struct *async_submit_workqueue; 50 static struct workqueue_struct *async_submit_workqueue;
51 51
52 struct end_io_wq { 52 struct end_io_wq {
53 struct bio *bio; 53 struct bio *bio;
54 bio_end_io_t *end_io; 54 bio_end_io_t *end_io;
55 void *private; 55 void *private;
56 struct btrfs_fs_info *info; 56 struct btrfs_fs_info *info;
57 int error; 57 int error;
58 int metadata; 58 int metadata;
59 struct list_head list; 59 struct list_head list;
60 }; 60 };
61 61
62 struct async_submit_bio { 62 struct async_submit_bio {
63 struct inode *inode; 63 struct inode *inode;
64 struct bio *bio; 64 struct bio *bio;
65 struct list_head list; 65 struct list_head list;
66 extent_submit_bio_hook_t *submit_bio_hook; 66 extent_submit_bio_hook_t *submit_bio_hook;
67 int rw; 67 int rw;
68 int mirror_num; 68 int mirror_num;
69 }; 69 };
70 70
71 struct extent_map *btree_get_extent(struct inode *inode, struct page *page, 71 struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
72 size_t page_offset, u64 start, u64 len, 72 size_t page_offset, u64 start, u64 len,
73 int create) 73 int create)
74 { 74 {
75 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 75 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
76 struct extent_map *em; 76 struct extent_map *em;
77 int ret; 77 int ret;
78 78
79 spin_lock(&em_tree->lock); 79 spin_lock(&em_tree->lock);
80 em = lookup_extent_mapping(em_tree, start, len); 80 em = lookup_extent_mapping(em_tree, start, len);
81 if (em) { 81 if (em) {
82 em->bdev = 82 em->bdev =
83 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 83 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
84 spin_unlock(&em_tree->lock); 84 spin_unlock(&em_tree->lock);
85 goto out; 85 goto out;
86 } 86 }
87 spin_unlock(&em_tree->lock); 87 spin_unlock(&em_tree->lock);
88 88
89 em = alloc_extent_map(GFP_NOFS); 89 em = alloc_extent_map(GFP_NOFS);
90 if (!em) { 90 if (!em) {
91 em = ERR_PTR(-ENOMEM); 91 em = ERR_PTR(-ENOMEM);
92 goto out; 92 goto out;
93 } 93 }
94 em->start = 0; 94 em->start = 0;
95 em->len = (u64)-1; 95 em->len = (u64)-1;
96 em->block_start = 0; 96 em->block_start = 0;
97 em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 97 em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
98 98
99 spin_lock(&em_tree->lock); 99 spin_lock(&em_tree->lock);
100 ret = add_extent_mapping(em_tree, em); 100 ret = add_extent_mapping(em_tree, em);
101 if (ret == -EEXIST) { 101 if (ret == -EEXIST) {
102 u64 failed_start = em->start; 102 u64 failed_start = em->start;
103 u64 failed_len = em->len; 103 u64 failed_len = em->len;
104 104
105 printk("failed to insert %Lu %Lu -> %Lu into tree\n", 105 printk("failed to insert %Lu %Lu -> %Lu into tree\n",
106 em->start, em->len, em->block_start); 106 em->start, em->len, em->block_start);
107 free_extent_map(em); 107 free_extent_map(em);
108 em = lookup_extent_mapping(em_tree, start, len); 108 em = lookup_extent_mapping(em_tree, start, len);
109 if (em) { 109 if (em) {
110 printk("after failing, found %Lu %Lu %Lu\n", 110 printk("after failing, found %Lu %Lu %Lu\n",
111 em->start, em->len, em->block_start); 111 em->start, em->len, em->block_start);
112 ret = 0; 112 ret = 0;
113 } else { 113 } else {
114 em = lookup_extent_mapping(em_tree, failed_start, 114 em = lookup_extent_mapping(em_tree, failed_start,
115 failed_len); 115 failed_len);
116 if (em) { 116 if (em) {
117 printk("double failure lookup gives us " 117 printk("double failure lookup gives us "
118 "%Lu %Lu -> %Lu\n", em->start, 118 "%Lu %Lu -> %Lu\n", em->start,
119 em->len, em->block_start); 119 em->len, em->block_start);
120 free_extent_map(em); 120 free_extent_map(em);
121 } 121 }
122 ret = -EIO; 122 ret = -EIO;
123 } 123 }
124 } else if (ret) { 124 } else if (ret) {
125 free_extent_map(em); 125 free_extent_map(em);
126 em = NULL; 126 em = NULL;
127 } 127 }
128 spin_unlock(&em_tree->lock); 128 spin_unlock(&em_tree->lock);
129 129
130 if (ret) 130 if (ret)
131 em = ERR_PTR(ret); 131 em = ERR_PTR(ret);
132 out: 132 out:
133 return em; 133 return em;
134 } 134 }
135 135
136 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len) 136 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
137 { 137 {
138 return btrfs_crc32c(seed, data, len); 138 return btrfs_crc32c(seed, data, len);
139 } 139 }
140 140
141 void btrfs_csum_final(u32 crc, char *result) 141 void btrfs_csum_final(u32 crc, char *result)
142 { 142 {
143 *(__le32 *)result = ~cpu_to_le32(crc); 143 *(__le32 *)result = ~cpu_to_le32(crc);
144 } 144 }
145 145
146 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, 146 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
147 int verify) 147 int verify)
148 { 148 {
149 char result[BTRFS_CRC32_SIZE]; 149 char result[BTRFS_CRC32_SIZE];
150 unsigned long len; 150 unsigned long len;
151 unsigned long cur_len; 151 unsigned long cur_len;
152 unsigned long offset = BTRFS_CSUM_SIZE; 152 unsigned long offset = BTRFS_CSUM_SIZE;
153 char *map_token = NULL; 153 char *map_token = NULL;
154 char *kaddr; 154 char *kaddr;
155 unsigned long map_start; 155 unsigned long map_start;
156 unsigned long map_len; 156 unsigned long map_len;
157 int err; 157 int err;
158 u32 crc = ~(u32)0; 158 u32 crc = ~(u32)0;
159 159
160 len = buf->len - offset; 160 len = buf->len - offset;
161 while(len > 0) { 161 while(len > 0) {
162 err = map_private_extent_buffer(buf, offset, 32, 162 err = map_private_extent_buffer(buf, offset, 32,
163 &map_token, &kaddr, 163 &map_token, &kaddr,
164 &map_start, &map_len, KM_USER0); 164 &map_start, &map_len, KM_USER0);
165 if (err) { 165 if (err) {
166 printk("failed to map extent buffer! %lu\n", 166 printk("failed to map extent buffer! %lu\n",
167 offset); 167 offset);
168 return 1; 168 return 1;
169 } 169 }
170 cur_len = min(len, map_len - (offset - map_start)); 170 cur_len = min(len, map_len - (offset - map_start));
171 crc = btrfs_csum_data(root, kaddr + offset - map_start, 171 crc = btrfs_csum_data(root, kaddr + offset - map_start,
172 crc, cur_len); 172 crc, cur_len);
173 len -= cur_len; 173 len -= cur_len;
174 offset += cur_len; 174 offset += cur_len;
175 unmap_extent_buffer(buf, map_token, KM_USER0); 175 unmap_extent_buffer(buf, map_token, KM_USER0);
176 } 176 }
177 btrfs_csum_final(crc, result); 177 btrfs_csum_final(crc, result);
178 178
179 if (verify) { 179 if (verify) {
180 int from_this_trans = 0; 180 int from_this_trans = 0;
181 181
182 if (root->fs_info->running_transaction && 182 if (root->fs_info->running_transaction &&
183 btrfs_header_generation(buf) == 183 btrfs_header_generation(buf) ==
184 root->fs_info->running_transaction->transid) 184 root->fs_info->running_transaction->transid)
185 from_this_trans = 1; 185 from_this_trans = 1;
186 186
187 /* FIXME, this is not good */ 187 /* FIXME, this is not good */
188 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) { 188 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
189 u32 val; 189 u32 val;
190 u32 found = 0; 190 u32 found = 0;
191 memcpy(&found, result, BTRFS_CRC32_SIZE); 191 memcpy(&found, result, BTRFS_CRC32_SIZE);
192 192
193 read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE); 193 read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
194 printk("btrfs: %s checksum verify failed on %llu " 194 printk("btrfs: %s checksum verify failed on %llu "
195 "wanted %X found %X from_this_trans %d " 195 "wanted %X found %X from_this_trans %d "
196 "level %d\n", 196 "level %d\n",
197 root->fs_info->sb->s_id, 197 root->fs_info->sb->s_id,
198 buf->start, val, found, from_this_trans, 198 buf->start, val, found, from_this_trans,
199 btrfs_header_level(buf)); 199 btrfs_header_level(buf));
200 return 1; 200 return 1;
201 } 201 }
202 } else { 202 } else {
203 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE); 203 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
204 } 204 }
205 return 0; 205 return 0;
206 } 206 }
207 207
208 static int btree_read_extent_buffer_pages(struct btrfs_root *root, 208 static int btree_read_extent_buffer_pages(struct btrfs_root *root,
209 struct extent_buffer *eb, 209 struct extent_buffer *eb,
210 u64 start) 210 u64 start, u64 parent_transid)
211 { 211 {
212 struct extent_io_tree *io_tree; 212 struct extent_io_tree *io_tree;
213 int ret; 213 int ret;
214 int num_copies = 0; 214 int num_copies = 0;
215 int mirror_num = 0; 215 int mirror_num = 0;
216 216
217 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree; 217 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
218 while (1) { 218 while (1) {
219 ret = read_extent_buffer_pages(io_tree, eb, start, 1, 219 ret = read_extent_buffer_pages(io_tree, eb, start, 1,
220 btree_get_extent, mirror_num); 220 btree_get_extent, mirror_num);
221 if (!ret) 221 if (!ret)
222 return ret; 222 return ret;
223 223
224 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree, 224 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
225 eb->start, eb->len); 225 eb->start, eb->len);
226 if (num_copies == 1) 226 if (num_copies == 1)
227 return ret; 227 return ret;
228 228
229 mirror_num++; 229 mirror_num++;
230 if (mirror_num > num_copies) 230 if (mirror_num > num_copies)
231 return ret; 231 return ret;
232 } 232 }
233 return -EIO; 233 return -EIO;
234 } 234 }
235 235
236 int csum_dirty_buffer(struct btrfs_root *root, struct page *page) 236 int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
237 { 237 {
238 struct extent_io_tree *tree; 238 struct extent_io_tree *tree;
239 u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 239 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
240 u64 found_start; 240 u64 found_start;
241 int found_level; 241 int found_level;
242 unsigned long len; 242 unsigned long len;
243 struct extent_buffer *eb; 243 struct extent_buffer *eb;
244 int ret; 244 int ret;
245 245
246 tree = &BTRFS_I(page->mapping->host)->io_tree; 246 tree = &BTRFS_I(page->mapping->host)->io_tree;
247 247
248 if (page->private == EXTENT_PAGE_PRIVATE) 248 if (page->private == EXTENT_PAGE_PRIVATE)
249 goto out; 249 goto out;
250 if (!page->private) 250 if (!page->private)
251 goto out; 251 goto out;
252 len = page->private >> 2; 252 len = page->private >> 2;
253 if (len == 0) { 253 if (len == 0) {
254 WARN_ON(1); 254 WARN_ON(1);
255 } 255 }
256 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS); 256 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
257 ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE); 257 ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE,
258 btrfs_header_generation(eb));
258 BUG_ON(ret); 259 BUG_ON(ret);
259 btrfs_clear_buffer_defrag(eb); 260 btrfs_clear_buffer_defrag(eb);
260 found_start = btrfs_header_bytenr(eb); 261 found_start = btrfs_header_bytenr(eb);
261 if (found_start != start) { 262 if (found_start != start) {
262 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n", 263 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
263 start, found_start, len); 264 start, found_start, len);
264 WARN_ON(1); 265 WARN_ON(1);
265 goto err; 266 goto err;
266 } 267 }
267 if (eb->first_page != page) { 268 if (eb->first_page != page) {
268 printk("bad first page %lu %lu\n", eb->first_page->index, 269 printk("bad first page %lu %lu\n", eb->first_page->index,
269 page->index); 270 page->index);
270 WARN_ON(1); 271 WARN_ON(1);
271 goto err; 272 goto err;
272 } 273 }
273 if (!PageUptodate(page)) { 274 if (!PageUptodate(page)) {
274 printk("csum not up to date page %lu\n", page->index); 275 printk("csum not up to date page %lu\n", page->index);
275 WARN_ON(1); 276 WARN_ON(1);
276 goto err; 277 goto err;
277 } 278 }
278 found_level = btrfs_header_level(eb); 279 found_level = btrfs_header_level(eb);
279 spin_lock(&root->fs_info->hash_lock); 280 spin_lock(&root->fs_info->hash_lock);
280 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); 281 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
281 spin_unlock(&root->fs_info->hash_lock); 282 spin_unlock(&root->fs_info->hash_lock);
282 csum_tree_block(root, eb, 0); 283 csum_tree_block(root, eb, 0);
283 err: 284 err:
284 free_extent_buffer(eb); 285 free_extent_buffer(eb);
285 out: 286 out:
286 return 0; 287 return 0;
287 } 288 }
288 289
289 static int btree_writepage_io_hook(struct page *page, u64 start, u64 end) 290 static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
290 { 291 {
291 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 292 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
292 293
293 csum_dirty_buffer(root, page); 294 csum_dirty_buffer(root, page);
294 return 0; 295 return 0;
295 } 296 }
296 297
297 int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end, 298 int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
298 struct extent_state *state) 299 struct extent_state *state)
299 { 300 {
300 struct extent_io_tree *tree; 301 struct extent_io_tree *tree;
301 u64 found_start; 302 u64 found_start;
302 int found_level; 303 int found_level;
303 unsigned long len; 304 unsigned long len;
304 struct extent_buffer *eb; 305 struct extent_buffer *eb;
305 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 306 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
306 int ret = 0; 307 int ret = 0;
307 308
308 tree = &BTRFS_I(page->mapping->host)->io_tree; 309 tree = &BTRFS_I(page->mapping->host)->io_tree;
309 if (page->private == EXTENT_PAGE_PRIVATE) 310 if (page->private == EXTENT_PAGE_PRIVATE)
310 goto out; 311 goto out;
311 if (!page->private) 312 if (!page->private)
312 goto out; 313 goto out;
313 len = page->private >> 2; 314 len = page->private >> 2;
314 if (len == 0) { 315 if (len == 0) {
315 WARN_ON(1); 316 WARN_ON(1);
316 } 317 }
317 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS); 318 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
318 319
319 btrfs_clear_buffer_defrag(eb); 320 btrfs_clear_buffer_defrag(eb);
320 found_start = btrfs_header_bytenr(eb); 321 found_start = btrfs_header_bytenr(eb);
321 if (found_start != start) { 322 if (found_start != start) {
322 ret = -EIO; 323 ret = -EIO;
323 goto err; 324 goto err;
324 } 325 }
325 if (eb->first_page != page) { 326 if (eb->first_page != page) {
326 printk("bad first page %lu %lu\n", eb->first_page->index, 327 printk("bad first page %lu %lu\n", eb->first_page->index,
327 page->index); 328 page->index);
328 WARN_ON(1); 329 WARN_ON(1);
329 ret = -EIO; 330 ret = -EIO;
330 goto err; 331 goto err;
331 } 332 }
332 found_level = btrfs_header_level(eb); 333 found_level = btrfs_header_level(eb);
333 334
334 ret = csum_tree_block(root, eb, 1); 335 ret = csum_tree_block(root, eb, 1);
335 if (ret) 336 if (ret)
336 ret = -EIO; 337 ret = -EIO;
337 338
338 end = min_t(u64, eb->len, PAGE_CACHE_SIZE); 339 end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
339 end = eb->start + end - 1; 340 end = eb->start + end - 1;
340 release_extent_buffer_tail_pages(eb); 341 release_extent_buffer_tail_pages(eb);
341 err: 342 err:
342 free_extent_buffer(eb); 343 free_extent_buffer(eb);
343 out: 344 out:
344 return ret; 345 return ret;
345 } 346 }
346 347
347 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) 348 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
348 static void end_workqueue_bio(struct bio *bio, int err) 349 static void end_workqueue_bio(struct bio *bio, int err)
349 #else 350 #else
350 static int end_workqueue_bio(struct bio *bio, 351 static int end_workqueue_bio(struct bio *bio,
351 unsigned int bytes_done, int err) 352 unsigned int bytes_done, int err)
352 #endif 353 #endif
353 { 354 {
354 struct end_io_wq *end_io_wq = bio->bi_private; 355 struct end_io_wq *end_io_wq = bio->bi_private;
355 struct btrfs_fs_info *fs_info; 356 struct btrfs_fs_info *fs_info;
356 unsigned long flags; 357 unsigned long flags;
357 358
358 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) 359 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
359 if (bio->bi_size) 360 if (bio->bi_size)
360 return 1; 361 return 1;
361 #endif 362 #endif
362 363
363 fs_info = end_io_wq->info; 364 fs_info = end_io_wq->info;
364 spin_lock_irqsave(&fs_info->end_io_work_lock, flags); 365 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
365 end_io_wq->error = err; 366 end_io_wq->error = err;
366 list_add_tail(&end_io_wq->list, &fs_info->end_io_work_list); 367 list_add_tail(&end_io_wq->list, &fs_info->end_io_work_list);
367 spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags); 368 spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
368 queue_work(end_io_workqueue, &fs_info->end_io_work); 369 queue_work(end_io_workqueue, &fs_info->end_io_work);
369 370
370 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) 371 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
371 return 0; 372 return 0;
372 #endif 373 #endif
373 } 374 }
374 375
375 int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 376 int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
376 int metadata) 377 int metadata)
377 { 378 {
378 struct end_io_wq *end_io_wq; 379 struct end_io_wq *end_io_wq;
379 end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS); 380 end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
380 if (!end_io_wq) 381 if (!end_io_wq)
381 return -ENOMEM; 382 return -ENOMEM;
382 383
383 end_io_wq->private = bio->bi_private; 384 end_io_wq->private = bio->bi_private;
384 end_io_wq->end_io = bio->bi_end_io; 385 end_io_wq->end_io = bio->bi_end_io;
385 end_io_wq->info = info; 386 end_io_wq->info = info;
386 end_io_wq->error = 0; 387 end_io_wq->error = 0;
387 end_io_wq->bio = bio; 388 end_io_wq->bio = bio;
388 end_io_wq->metadata = metadata; 389 end_io_wq->metadata = metadata;
389 390
390 bio->bi_private = end_io_wq; 391 bio->bi_private = end_io_wq;
391 bio->bi_end_io = end_workqueue_bio; 392 bio->bi_end_io = end_workqueue_bio;
392 return 0; 393 return 0;
393 } 394 }
394 395
395 int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, 396 int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
396 int rw, struct bio *bio, int mirror_num, 397 int rw, struct bio *bio, int mirror_num,
397 extent_submit_bio_hook_t *submit_bio_hook) 398 extent_submit_bio_hook_t *submit_bio_hook)
398 { 399 {
399 struct async_submit_bio *async; 400 struct async_submit_bio *async;
400 401
401 /* 402 /*
402 * inline writerback should stay inline, only hop to the async 403 * inline writerback should stay inline, only hop to the async
403 * queue if we're pdflush 404 * queue if we're pdflush
404 */ 405 */
405 if (!current_is_pdflush()) 406 if (!current_is_pdflush())
406 return submit_bio_hook(inode, rw, bio, mirror_num); 407 return submit_bio_hook(inode, rw, bio, mirror_num);
407 408
408 async = kmalloc(sizeof(*async), GFP_NOFS); 409 async = kmalloc(sizeof(*async), GFP_NOFS);
409 if (!async) 410 if (!async)
410 return -ENOMEM; 411 return -ENOMEM;
411 412
412 async->inode = inode; 413 async->inode = inode;
413 async->rw = rw; 414 async->rw = rw;
414 async->bio = bio; 415 async->bio = bio;
415 async->mirror_num = mirror_num; 416 async->mirror_num = mirror_num;
416 async->submit_bio_hook = submit_bio_hook; 417 async->submit_bio_hook = submit_bio_hook;
417 418
418 spin_lock(&fs_info->async_submit_work_lock); 419 spin_lock(&fs_info->async_submit_work_lock);
419 list_add_tail(&async->list, &fs_info->async_submit_work_list); 420 list_add_tail(&async->list, &fs_info->async_submit_work_list);
420 spin_unlock(&fs_info->async_submit_work_lock); 421 spin_unlock(&fs_info->async_submit_work_lock);
421 422
422 queue_work(async_submit_workqueue, &fs_info->async_submit_work); 423 queue_work(async_submit_workqueue, &fs_info->async_submit_work);
423 return 0; 424 return 0;
424 } 425 }
425 426
426 static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, 427 static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
427 int mirror_num) 428 int mirror_num)
428 { 429 {
429 struct btrfs_root *root = BTRFS_I(inode)->root; 430 struct btrfs_root *root = BTRFS_I(inode)->root;
430 u64 offset; 431 u64 offset;
431 int ret; 432 int ret;
432 433
433 offset = bio->bi_sector << 9; 434 offset = bio->bi_sector << 9;
434 435
435 if (rw & (1 << BIO_RW)) { 436 if (rw & (1 << BIO_RW)) {
436 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num); 437 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num);
437 } 438 }
438 439
439 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1); 440 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1);
440 BUG_ON(ret); 441 BUG_ON(ret);
441 442
442 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num); 443 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num);
443 } 444 }
444 445
445 static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, 446 static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
446 int mirror_num) 447 int mirror_num)
447 { 448 {
448 if (!(rw & (1 << BIO_RW))) { 449 if (!(rw & (1 << BIO_RW))) {
449 return __btree_submit_bio_hook(inode, rw, bio, mirror_num); 450 return __btree_submit_bio_hook(inode, rw, bio, mirror_num);
450 } 451 }
451 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, 452 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
452 inode, rw, bio, mirror_num, 453 inode, rw, bio, mirror_num,
453 __btree_submit_bio_hook); 454 __btree_submit_bio_hook);
454 } 455 }
455 456
456 static int btree_writepage(struct page *page, struct writeback_control *wbc) 457 static int btree_writepage(struct page *page, struct writeback_control *wbc)
457 { 458 {
458 struct extent_io_tree *tree; 459 struct extent_io_tree *tree;
459 tree = &BTRFS_I(page->mapping->host)->io_tree; 460 tree = &BTRFS_I(page->mapping->host)->io_tree;
460 return extent_write_full_page(tree, page, btree_get_extent, wbc); 461 return extent_write_full_page(tree, page, btree_get_extent, wbc);
461 } 462 }
462 463
463 static int btree_writepages(struct address_space *mapping, 464 static int btree_writepages(struct address_space *mapping,
464 struct writeback_control *wbc) 465 struct writeback_control *wbc)
465 { 466 {
466 struct extent_io_tree *tree; 467 struct extent_io_tree *tree;
467 tree = &BTRFS_I(mapping->host)->io_tree; 468 tree = &BTRFS_I(mapping->host)->io_tree;
468 if (wbc->sync_mode == WB_SYNC_NONE) { 469 if (wbc->sync_mode == WB_SYNC_NONE) {
469 u64 num_dirty; 470 u64 num_dirty;
470 u64 start = 0; 471 u64 start = 0;
471 unsigned long thresh = 96 * 1024 * 1024; 472 unsigned long thresh = 96 * 1024 * 1024;
472 473
473 if (wbc->for_kupdate) 474 if (wbc->for_kupdate)
474 return 0; 475 return 0;
475 476
476 if (current_is_pdflush()) { 477 if (current_is_pdflush()) {
477 thresh = 96 * 1024 * 1024; 478 thresh = 96 * 1024 * 1024;
478 } else { 479 } else {
479 thresh = 8 * 1024 * 1024; 480 thresh = 8 * 1024 * 1024;
480 } 481 }
481 num_dirty = count_range_bits(tree, &start, (u64)-1, 482 num_dirty = count_range_bits(tree, &start, (u64)-1,
482 thresh, EXTENT_DIRTY); 483 thresh, EXTENT_DIRTY);
483 if (num_dirty < thresh) { 484 if (num_dirty < thresh) {
484 return 0; 485 return 0;
485 } 486 }
486 } 487 }
487 return extent_writepages(tree, mapping, btree_get_extent, wbc); 488 return extent_writepages(tree, mapping, btree_get_extent, wbc);
488 } 489 }
489 490
490 int btree_readpage(struct file *file, struct page *page) 491 int btree_readpage(struct file *file, struct page *page)
491 { 492 {
492 struct extent_io_tree *tree; 493 struct extent_io_tree *tree;
493 tree = &BTRFS_I(page->mapping->host)->io_tree; 494 tree = &BTRFS_I(page->mapping->host)->io_tree;
494 return extent_read_full_page(tree, page, btree_get_extent); 495 return extent_read_full_page(tree, page, btree_get_extent);
495 } 496 }
496 497
497 static int btree_releasepage(struct page *page, gfp_t gfp_flags) 498 static int btree_releasepage(struct page *page, gfp_t gfp_flags)
498 { 499 {
499 struct extent_io_tree *tree; 500 struct extent_io_tree *tree;
500 struct extent_map_tree *map; 501 struct extent_map_tree *map;
501 int ret; 502 int ret;
502 503
503 if (page_count(page) > 3) { 504 if (page_count(page) > 3) {
504 /* once for page->private, once for the caller, once 505 /* once for page->private, once for the caller, once
505 * once for the page cache 506 * once for the page cache
506 */ 507 */
507 return 0; 508 return 0;
508 } 509 }
509 tree = &BTRFS_I(page->mapping->host)->io_tree; 510 tree = &BTRFS_I(page->mapping->host)->io_tree;
510 map = &BTRFS_I(page->mapping->host)->extent_tree; 511 map = &BTRFS_I(page->mapping->host)->extent_tree;
511 ret = try_release_extent_state(map, tree, page, gfp_flags); 512 ret = try_release_extent_state(map, tree, page, gfp_flags);
512 if (ret == 1) { 513 if (ret == 1) {
513 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE); 514 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
514 ClearPagePrivate(page); 515 ClearPagePrivate(page);
515 set_page_private(page, 0); 516 set_page_private(page, 0);
516 page_cache_release(page); 517 page_cache_release(page);
517 } 518 }
518 return ret; 519 return ret;
519 } 520 }
520 521
521 static void btree_invalidatepage(struct page *page, unsigned long offset) 522 static void btree_invalidatepage(struct page *page, unsigned long offset)
522 { 523 {
523 struct extent_io_tree *tree; 524 struct extent_io_tree *tree;
524 tree = &BTRFS_I(page->mapping->host)->io_tree; 525 tree = &BTRFS_I(page->mapping->host)->io_tree;
525 extent_invalidatepage(tree, page, offset); 526 extent_invalidatepage(tree, page, offset);
526 btree_releasepage(page, GFP_NOFS); 527 btree_releasepage(page, GFP_NOFS);
527 if (PagePrivate(page)) { 528 if (PagePrivate(page)) {
528 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE); 529 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
529 ClearPagePrivate(page); 530 ClearPagePrivate(page);
530 set_page_private(page, 0); 531 set_page_private(page, 0);
531 page_cache_release(page); 532 page_cache_release(page);
532 } 533 }
533 } 534 }
534 535
535 #if 0 536 #if 0
536 static int btree_writepage(struct page *page, struct writeback_control *wbc) 537 static int btree_writepage(struct page *page, struct writeback_control *wbc)
537 { 538 {
538 struct buffer_head *bh; 539 struct buffer_head *bh;
539 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 540 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
540 struct buffer_head *head; 541 struct buffer_head *head;
541 if (!page_has_buffers(page)) { 542 if (!page_has_buffers(page)) {
542 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 543 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
543 (1 << BH_Dirty)|(1 << BH_Uptodate)); 544 (1 << BH_Dirty)|(1 << BH_Uptodate));
544 } 545 }
545 head = page_buffers(page); 546 head = page_buffers(page);
546 bh = head; 547 bh = head;
547 do { 548 do {
548 if (buffer_dirty(bh)) 549 if (buffer_dirty(bh))
549 csum_tree_block(root, bh, 0); 550 csum_tree_block(root, bh, 0);
550 bh = bh->b_this_page; 551 bh = bh->b_this_page;
551 } while (bh != head); 552 } while (bh != head);
552 return block_write_full_page(page, btree_get_block, wbc); 553 return block_write_full_page(page, btree_get_block, wbc);
553 } 554 }
554 #endif 555 #endif
555 556
556 static struct address_space_operations btree_aops = { 557 static struct address_space_operations btree_aops = {
557 .readpage = btree_readpage, 558 .readpage = btree_readpage,
558 .writepage = btree_writepage, 559 .writepage = btree_writepage,
559 .writepages = btree_writepages, 560 .writepages = btree_writepages,
560 .releasepage = btree_releasepage, 561 .releasepage = btree_releasepage,
561 .invalidatepage = btree_invalidatepage, 562 .invalidatepage = btree_invalidatepage,
562 .sync_page = block_sync_page, 563 .sync_page = block_sync_page,
563 }; 564 };
564 565
565 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize) 566 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
567 u64 parent_transid)
566 { 568 {
567 struct extent_buffer *buf = NULL; 569 struct extent_buffer *buf = NULL;
568 struct inode *btree_inode = root->fs_info->btree_inode; 570 struct inode *btree_inode = root->fs_info->btree_inode;
569 int ret = 0; 571 int ret = 0;
570 572
571 buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 573 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
572 if (!buf) 574 if (!buf)
573 return 0; 575 return 0;
574 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 576 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
575 buf, 0, 0, btree_get_extent, 0); 577 buf, 0, 0, btree_get_extent, 0);
576 free_extent_buffer(buf); 578 free_extent_buffer(buf);
577 return ret; 579 return ret;
578 } 580 }
579 581
580 static int close_all_devices(struct btrfs_fs_info *fs_info) 582 static int close_all_devices(struct btrfs_fs_info *fs_info)
581 { 583 {
582 struct list_head *list; 584 struct list_head *list;
583 struct list_head *next; 585 struct list_head *next;
584 struct btrfs_device *device; 586 struct btrfs_device *device;
585 587
586 list = &fs_info->fs_devices->devices; 588 list = &fs_info->fs_devices->devices;
587 list_for_each(next, list) { 589 list_for_each(next, list) {
588 device = list_entry(next, struct btrfs_device, dev_list); 590 device = list_entry(next, struct btrfs_device, dev_list);
589 close_bdev_excl(device->bdev); 591 close_bdev_excl(device->bdev);
590 device->bdev = NULL; 592 device->bdev = NULL;
591 } 593 }
592 return 0; 594 return 0;
593 } 595 }
594 596
595 int btrfs_verify_block_csum(struct btrfs_root *root,
596 struct extent_buffer *buf)
597 {
598 return btrfs_buffer_uptodate(buf);
599 }
600
601 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, 597 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
602 u64 bytenr, u32 blocksize) 598 u64 bytenr, u32 blocksize)
603 { 599 {
604 struct inode *btree_inode = root->fs_info->btree_inode; 600 struct inode *btree_inode = root->fs_info->btree_inode;
605 struct extent_buffer *eb; 601 struct extent_buffer *eb;
606 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree, 602 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
607 bytenr, blocksize, GFP_NOFS); 603 bytenr, blocksize, GFP_NOFS);
608 return eb; 604 return eb;
609 } 605 }
610 606
611 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, 607 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
612 u64 bytenr, u32 blocksize) 608 u64 bytenr, u32 blocksize)
613 { 609 {
614 struct inode *btree_inode = root->fs_info->btree_inode; 610 struct inode *btree_inode = root->fs_info->btree_inode;
615 struct extent_buffer *eb; 611 struct extent_buffer *eb;
616 612
617 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree, 613 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
618 bytenr, blocksize, NULL, GFP_NOFS); 614 bytenr, blocksize, NULL, GFP_NOFS);
619 return eb; 615 return eb;
620 } 616 }
621 617
622 618
623 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, 619 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
624 u32 blocksize) 620 u32 blocksize, u64 parent_transid)
625 { 621 {
626 struct extent_buffer *buf = NULL; 622 struct extent_buffer *buf = NULL;
627 struct inode *btree_inode = root->fs_info->btree_inode; 623 struct inode *btree_inode = root->fs_info->btree_inode;
628 struct extent_io_tree *io_tree; 624 struct extent_io_tree *io_tree;
629 int ret; 625 int ret;
630 626
631 io_tree = &BTRFS_I(btree_inode)->io_tree; 627 io_tree = &BTRFS_I(btree_inode)->io_tree;
632 628
633 buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 629 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
634 if (!buf) 630 if (!buf)
635 return NULL; 631 return NULL;
636 632
637 ret = btree_read_extent_buffer_pages(root, buf, 0); 633 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
638 634
639 if (ret == 0) { 635 if (ret == 0) {
640 buf->flags |= EXTENT_UPTODATE; 636 buf->flags |= EXTENT_UPTODATE;
641 } 637 }
642 return buf; 638 return buf;
643 639
644 } 640 }
645 641
646 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, 642 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
647 struct extent_buffer *buf) 643 struct extent_buffer *buf)
648 { 644 {
649 struct inode *btree_inode = root->fs_info->btree_inode; 645 struct inode *btree_inode = root->fs_info->btree_inode;
650 if (btrfs_header_generation(buf) == 646 if (btrfs_header_generation(buf) ==
651 root->fs_info->running_transaction->transid) 647 root->fs_info->running_transaction->transid)
652 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, 648 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
653 buf); 649 buf);
654 return 0; 650 return 0;
655 } 651 }
656 652
657 int wait_on_tree_block_writeback(struct btrfs_root *root, 653 int wait_on_tree_block_writeback(struct btrfs_root *root,
658 struct extent_buffer *buf) 654 struct extent_buffer *buf)
659 { 655 {
660 struct inode *btree_inode = root->fs_info->btree_inode; 656 struct inode *btree_inode = root->fs_info->btree_inode;
661 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree, 657 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
662 buf); 658 buf);
663 return 0; 659 return 0;
664 } 660 }
665 661
666 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, 662 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
667 u32 stripesize, struct btrfs_root *root, 663 u32 stripesize, struct btrfs_root *root,
668 struct btrfs_fs_info *fs_info, 664 struct btrfs_fs_info *fs_info,
669 u64 objectid) 665 u64 objectid)
670 { 666 {
671 root->node = NULL; 667 root->node = NULL;
672 root->inode = NULL; 668 root->inode = NULL;
673 root->commit_root = NULL; 669 root->commit_root = NULL;
674 root->sectorsize = sectorsize; 670 root->sectorsize = sectorsize;
675 root->nodesize = nodesize; 671 root->nodesize = nodesize;
676 root->leafsize = leafsize; 672 root->leafsize = leafsize;
677 root->stripesize = stripesize; 673 root->stripesize = stripesize;
678 root->ref_cows = 0; 674 root->ref_cows = 0;
679 root->track_dirty = 0; 675 root->track_dirty = 0;
680 676
681 root->fs_info = fs_info; 677 root->fs_info = fs_info;
682 root->objectid = objectid; 678 root->objectid = objectid;
683 root->last_trans = 0; 679 root->last_trans = 0;
684 root->highest_inode = 0; 680 root->highest_inode = 0;
685 root->last_inode_alloc = 0; 681 root->last_inode_alloc = 0;
686 root->name = NULL; 682 root->name = NULL;
687 root->in_sysfs = 0; 683 root->in_sysfs = 0;
688 684
689 INIT_LIST_HEAD(&root->dirty_list); 685 INIT_LIST_HEAD(&root->dirty_list);
690 memset(&root->root_key, 0, sizeof(root->root_key)); 686 memset(&root->root_key, 0, sizeof(root->root_key));
691 memset(&root->root_item, 0, sizeof(root->root_item)); 687 memset(&root->root_item, 0, sizeof(root->root_item));
692 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 688 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
693 memset(&root->root_kobj, 0, sizeof(root->root_kobj)); 689 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
694 init_completion(&root->kobj_unregister); 690 init_completion(&root->kobj_unregister);
695 root->defrag_running = 0; 691 root->defrag_running = 0;
696 root->defrag_level = 0; 692 root->defrag_level = 0;
697 root->root_key.objectid = objectid; 693 root->root_key.objectid = objectid;
698 return 0; 694 return 0;
699 } 695 }
700 696
701 static int find_and_setup_root(struct btrfs_root *tree_root, 697 static int find_and_setup_root(struct btrfs_root *tree_root,
702 struct btrfs_fs_info *fs_info, 698 struct btrfs_fs_info *fs_info,
703 u64 objectid, 699 u64 objectid,
704 struct btrfs_root *root) 700 struct btrfs_root *root)
705 { 701 {
706 int ret; 702 int ret;
707 u32 blocksize; 703 u32 blocksize;
708 704
709 __setup_root(tree_root->nodesize, tree_root->leafsize, 705 __setup_root(tree_root->nodesize, tree_root->leafsize,
710 tree_root->sectorsize, tree_root->stripesize, 706 tree_root->sectorsize, tree_root->stripesize,
711 root, fs_info, objectid); 707 root, fs_info, objectid);
712 ret = btrfs_find_last_root(tree_root, objectid, 708 ret = btrfs_find_last_root(tree_root, objectid,
713 &root->root_item, &root->root_key); 709 &root->root_item, &root->root_key);
714 BUG_ON(ret); 710 BUG_ON(ret);
715 711
716 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 712 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
717 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 713 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
718 blocksize); 714 blocksize, 0);
719 BUG_ON(!root->node); 715 BUG_ON(!root->node);
720 return 0; 716 return 0;
721 } 717 }
722 718
723 struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, 719 struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
724 struct btrfs_key *location) 720 struct btrfs_key *location)
725 { 721 {
726 struct btrfs_root *root; 722 struct btrfs_root *root;
727 struct btrfs_root *tree_root = fs_info->tree_root; 723 struct btrfs_root *tree_root = fs_info->tree_root;
728 struct btrfs_path *path; 724 struct btrfs_path *path;
729 struct extent_buffer *l; 725 struct extent_buffer *l;
730 u64 highest_inode; 726 u64 highest_inode;
731 u32 blocksize; 727 u32 blocksize;
732 int ret = 0; 728 int ret = 0;
733 729
734 root = kzalloc(sizeof(*root), GFP_NOFS); 730 root = kzalloc(sizeof(*root), GFP_NOFS);
735 if (!root) 731 if (!root)
736 return ERR_PTR(-ENOMEM); 732 return ERR_PTR(-ENOMEM);
737 if (location->offset == (u64)-1) { 733 if (location->offset == (u64)-1) {
738 ret = find_and_setup_root(tree_root, fs_info, 734 ret = find_and_setup_root(tree_root, fs_info,
739 location->objectid, root); 735 location->objectid, root);
740 if (ret) { 736 if (ret) {
741 kfree(root); 737 kfree(root);
742 return ERR_PTR(ret); 738 return ERR_PTR(ret);
743 } 739 }
744 goto insert; 740 goto insert;
745 } 741 }
746 742
747 __setup_root(tree_root->nodesize, tree_root->leafsize, 743 __setup_root(tree_root->nodesize, tree_root->leafsize,
748 tree_root->sectorsize, tree_root->stripesize, 744 tree_root->sectorsize, tree_root->stripesize,
749 root, fs_info, location->objectid); 745 root, fs_info, location->objectid);
750 746
751 path = btrfs_alloc_path(); 747 path = btrfs_alloc_path();
752 BUG_ON(!path); 748 BUG_ON(!path);
753 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); 749 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
754 if (ret != 0) { 750 if (ret != 0) {
755 if (ret > 0) 751 if (ret > 0)
756 ret = -ENOENT; 752 ret = -ENOENT;
757 goto out; 753 goto out;
758 } 754 }
759 l = path->nodes[0]; 755 l = path->nodes[0];
760 read_extent_buffer(l, &root->root_item, 756 read_extent_buffer(l, &root->root_item,
761 btrfs_item_ptr_offset(l, path->slots[0]), 757 btrfs_item_ptr_offset(l, path->slots[0]),
762 sizeof(root->root_item)); 758 sizeof(root->root_item));
763 memcpy(&root->root_key, location, sizeof(*location)); 759 memcpy(&root->root_key, location, sizeof(*location));
764 ret = 0; 760 ret = 0;
765 out: 761 out:
766 btrfs_release_path(root, path); 762 btrfs_release_path(root, path);
767 btrfs_free_path(path); 763 btrfs_free_path(path);
768 if (ret) { 764 if (ret) {
769 kfree(root); 765 kfree(root);
770 return ERR_PTR(ret); 766 return ERR_PTR(ret);
771 } 767 }
772 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 768 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
773 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 769 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
774 blocksize); 770 blocksize, 0);
775 BUG_ON(!root->node); 771 BUG_ON(!root->node);
776 insert: 772 insert:
777 root->ref_cows = 1; 773 root->ref_cows = 1;
778 ret = btrfs_find_highest_inode(root, &highest_inode); 774 ret = btrfs_find_highest_inode(root, &highest_inode);
779 if (ret == 0) { 775 if (ret == 0) {
780 root->highest_inode = highest_inode; 776 root->highest_inode = highest_inode;
781 root->last_inode_alloc = highest_inode; 777 root->last_inode_alloc = highest_inode;
782 } 778 }
783 return root; 779 return root;
784 } 780 }
785 781
786 struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 782 struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
787 u64 root_objectid) 783 u64 root_objectid)
788 { 784 {
789 struct btrfs_root *root; 785 struct btrfs_root *root;
790 786
791 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID) 787 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
792 return fs_info->tree_root; 788 return fs_info->tree_root;
793 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) 789 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
794 return fs_info->extent_root; 790 return fs_info->extent_root;
795 791
796 root = radix_tree_lookup(&fs_info->fs_roots_radix, 792 root = radix_tree_lookup(&fs_info->fs_roots_radix,
797 (unsigned long)root_objectid); 793 (unsigned long)root_objectid);
798 return root; 794 return root;
799 } 795 }
800 796
801 struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, 797 struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
802 struct btrfs_key *location) 798 struct btrfs_key *location)
803 { 799 {
804 struct btrfs_root *root; 800 struct btrfs_root *root;
805 int ret; 801 int ret;
806 802
807 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 803 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
808 return fs_info->tree_root; 804 return fs_info->tree_root;
809 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) 805 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
810 return fs_info->extent_root; 806 return fs_info->extent_root;
811 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) 807 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
812 return fs_info->chunk_root; 808 return fs_info->chunk_root;
813 if (location->objectid == BTRFS_DEV_TREE_OBJECTID) 809 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
814 return fs_info->dev_root; 810 return fs_info->dev_root;
815 811
816 root = radix_tree_lookup(&fs_info->fs_roots_radix, 812 root = radix_tree_lookup(&fs_info->fs_roots_radix,
817 (unsigned long)location->objectid); 813 (unsigned long)location->objectid);
818 if (root) 814 if (root)
819 return root; 815 return root;
820 816
821 root = btrfs_read_fs_root_no_radix(fs_info, location); 817 root = btrfs_read_fs_root_no_radix(fs_info, location);
822 if (IS_ERR(root)) 818 if (IS_ERR(root))
823 return root; 819 return root;
824 ret = radix_tree_insert(&fs_info->fs_roots_radix, 820 ret = radix_tree_insert(&fs_info->fs_roots_radix,
825 (unsigned long)root->root_key.objectid, 821 (unsigned long)root->root_key.objectid,
826 root); 822 root);
827 if (ret) { 823 if (ret) {
828 free_extent_buffer(root->node); 824 free_extent_buffer(root->node);
829 kfree(root); 825 kfree(root);
830 return ERR_PTR(ret); 826 return ERR_PTR(ret);
831 } 827 }
832 ret = btrfs_find_dead_roots(fs_info->tree_root, 828 ret = btrfs_find_dead_roots(fs_info->tree_root,
833 root->root_key.objectid, root); 829 root->root_key.objectid, root);
834 BUG_ON(ret); 830 BUG_ON(ret);
835 831
836 return root; 832 return root;
837 } 833 }
838 834
839 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, 835 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
840 struct btrfs_key *location, 836 struct btrfs_key *location,
841 const char *name, int namelen) 837 const char *name, int namelen)
842 { 838 {
843 struct btrfs_root *root; 839 struct btrfs_root *root;
844 int ret; 840 int ret;
845 841
846 root = btrfs_read_fs_root_no_name(fs_info, location); 842 root = btrfs_read_fs_root_no_name(fs_info, location);
847 if (!root) 843 if (!root)
848 return NULL; 844 return NULL;
849 845
850 if (root->in_sysfs) 846 if (root->in_sysfs)
851 return root; 847 return root;
852 848
853 ret = btrfs_set_root_name(root, name, namelen); 849 ret = btrfs_set_root_name(root, name, namelen);
854 if (ret) { 850 if (ret) {
855 free_extent_buffer(root->node); 851 free_extent_buffer(root->node);
856 kfree(root); 852 kfree(root);
857 return ERR_PTR(ret); 853 return ERR_PTR(ret);
858 } 854 }
859 855
860 ret = btrfs_sysfs_add_root(root); 856 ret = btrfs_sysfs_add_root(root);
861 if (ret) { 857 if (ret) {
862 free_extent_buffer(root->node); 858 free_extent_buffer(root->node);
863 kfree(root->name); 859 kfree(root->name);
864 kfree(root); 860 kfree(root);
865 return ERR_PTR(ret); 861 return ERR_PTR(ret);
866 } 862 }
867 root->in_sysfs = 1; 863 root->in_sysfs = 1;
868 return root; 864 return root;
869 } 865 }
870 #if 0 866 #if 0
871 static int add_hasher(struct btrfs_fs_info *info, char *type) { 867 static int add_hasher(struct btrfs_fs_info *info, char *type) {
872 struct btrfs_hasher *hasher; 868 struct btrfs_hasher *hasher;
873 869
874 hasher = kmalloc(sizeof(*hasher), GFP_NOFS); 870 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
875 if (!hasher) 871 if (!hasher)
876 return -ENOMEM; 872 return -ENOMEM;
877 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC); 873 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
878 if (!hasher->hash_tfm) { 874 if (!hasher->hash_tfm) {
879 kfree(hasher); 875 kfree(hasher);
880 return -EINVAL; 876 return -EINVAL;
881 } 877 }
882 spin_lock(&info->hash_lock); 878 spin_lock(&info->hash_lock);
883 list_add(&hasher->list, &info->hashers); 879 list_add(&hasher->list, &info->hashers);
884 spin_unlock(&info->hash_lock); 880 spin_unlock(&info->hash_lock);
885 return 0; 881 return 0;
886 } 882 }
887 #endif 883 #endif
888 884
889 static int btrfs_congested_fn(void *congested_data, int bdi_bits) 885 static int btrfs_congested_fn(void *congested_data, int bdi_bits)
890 { 886 {
891 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; 887 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
892 int ret = 0; 888 int ret = 0;
893 struct list_head *cur; 889 struct list_head *cur;
894 struct btrfs_device *device; 890 struct btrfs_device *device;
895 struct backing_dev_info *bdi; 891 struct backing_dev_info *bdi;
896 892
897 list_for_each(cur, &info->fs_devices->devices) { 893 list_for_each(cur, &info->fs_devices->devices) {
898 device = list_entry(cur, struct btrfs_device, dev_list); 894 device = list_entry(cur, struct btrfs_device, dev_list);
899 bdi = blk_get_backing_dev_info(device->bdev); 895 bdi = blk_get_backing_dev_info(device->bdev);
900 if (bdi && bdi_congested(bdi, bdi_bits)) { 896 if (bdi && bdi_congested(bdi, bdi_bits)) {
901 ret = 1; 897 ret = 1;
902 break; 898 break;
903 } 899 }
904 } 900 }
905 return ret; 901 return ret;
906 } 902 }
907 903
908 /* 904 /*
909 * this unplugs every device on the box, and it is only used when page 905 * this unplugs every device on the box, and it is only used when page
910 * is null 906 * is null
911 */ 907 */
912 static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page) 908 static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
913 { 909 {
914 struct list_head *cur; 910 struct list_head *cur;
915 struct btrfs_device *device; 911 struct btrfs_device *device;
916 struct btrfs_fs_info *info; 912 struct btrfs_fs_info *info;
917 913
918 info = (struct btrfs_fs_info *)bdi->unplug_io_data; 914 info = (struct btrfs_fs_info *)bdi->unplug_io_data;
919 list_for_each(cur, &info->fs_devices->devices) { 915 list_for_each(cur, &info->fs_devices->devices) {
920 device = list_entry(cur, struct btrfs_device, dev_list); 916 device = list_entry(cur, struct btrfs_device, dev_list);
921 bdi = blk_get_backing_dev_info(device->bdev); 917 bdi = blk_get_backing_dev_info(device->bdev);
922 if (bdi->unplug_io_fn) { 918 if (bdi->unplug_io_fn) {
923 bdi->unplug_io_fn(bdi, page); 919 bdi->unplug_io_fn(bdi, page);
924 } 920 }
925 } 921 }
926 } 922 }
927 923
928 void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page) 924 void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
929 { 925 {
930 struct inode *inode; 926 struct inode *inode;
931 struct extent_map_tree *em_tree; 927 struct extent_map_tree *em_tree;
932 struct extent_map *em; 928 struct extent_map *em;
933 struct address_space *mapping; 929 struct address_space *mapping;
934 u64 offset; 930 u64 offset;
935 931
936 /* the generic O_DIRECT read code does this */ 932 /* the generic O_DIRECT read code does this */
937 if (!page) { 933 if (!page) {
938 __unplug_io_fn(bdi, page); 934 __unplug_io_fn(bdi, page);
939 return; 935 return;
940 } 936 }
941 937
942 /* 938 /*
943 * page->mapping may change at any time. Get a consistent copy 939 * page->mapping may change at any time. Get a consistent copy
944 * and use that for everything below 940 * and use that for everything below
945 */ 941 */
946 smp_mb(); 942 smp_mb();
947 mapping = page->mapping; 943 mapping = page->mapping;
948 if (!mapping) 944 if (!mapping)
949 return; 945 return;
950 946
951 inode = mapping->host; 947 inode = mapping->host;
952 offset = page_offset(page); 948 offset = page_offset(page);
953 949
954 em_tree = &BTRFS_I(inode)->extent_tree; 950 em_tree = &BTRFS_I(inode)->extent_tree;
955 spin_lock(&em_tree->lock); 951 spin_lock(&em_tree->lock);
956 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); 952 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
957 spin_unlock(&em_tree->lock); 953 spin_unlock(&em_tree->lock);
958 if (!em) 954 if (!em)
959 return; 955 return;
960 956
961 offset = offset - em->start; 957 offset = offset - em->start;
962 btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree, 958 btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree,
963 em->block_start + offset, page); 959 em->block_start + offset, page);
964 free_extent_map(em); 960 free_extent_map(em);
965 } 961 }
966 962
967 static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) 963 static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
968 { 964 {
969 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) 965 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
970 bdi_init(bdi); 966 bdi_init(bdi);
971 #endif 967 #endif
972 bdi->ra_pages = default_backing_dev_info.ra_pages; 968 bdi->ra_pages = default_backing_dev_info.ra_pages;
973 bdi->state = 0; 969 bdi->state = 0;
974 bdi->capabilities = default_backing_dev_info.capabilities; 970 bdi->capabilities = default_backing_dev_info.capabilities;
975 bdi->unplug_io_fn = btrfs_unplug_io_fn; 971 bdi->unplug_io_fn = btrfs_unplug_io_fn;
976 bdi->unplug_io_data = info; 972 bdi->unplug_io_data = info;
977 bdi->congested_fn = btrfs_congested_fn; 973 bdi->congested_fn = btrfs_congested_fn;
978 bdi->congested_data = info; 974 bdi->congested_data = info;
979 return 0; 975 return 0;
980 } 976 }
981 977
982 static int bio_ready_for_csum(struct bio *bio) 978 static int bio_ready_for_csum(struct bio *bio)
983 { 979 {
984 u64 length = 0; 980 u64 length = 0;
985 u64 buf_len = 0; 981 u64 buf_len = 0;
986 u64 start = 0; 982 u64 start = 0;
987 struct page *page; 983 struct page *page;
988 struct extent_io_tree *io_tree = NULL; 984 struct extent_io_tree *io_tree = NULL;
989 struct btrfs_fs_info *info = NULL; 985 struct btrfs_fs_info *info = NULL;
990 struct bio_vec *bvec; 986 struct bio_vec *bvec;
991 int i; 987 int i;
992 int ret; 988 int ret;
993 989
994 bio_for_each_segment(bvec, bio, i) { 990 bio_for_each_segment(bvec, bio, i) {
995 page = bvec->bv_page; 991 page = bvec->bv_page;
996 if (page->private == EXTENT_PAGE_PRIVATE) { 992 if (page->private == EXTENT_PAGE_PRIVATE) {
997 length += bvec->bv_len; 993 length += bvec->bv_len;
998 continue; 994 continue;
999 } 995 }
1000 if (!page->private) { 996 if (!page->private) {
1001 length += bvec->bv_len; 997 length += bvec->bv_len;
1002 continue; 998 continue;
1003 } 999 }
1004 length = bvec->bv_len; 1000 length = bvec->bv_len;
1005 buf_len = page->private >> 2; 1001 buf_len = page->private >> 2;
1006 start = page_offset(page) + bvec->bv_offset; 1002 start = page_offset(page) + bvec->bv_offset;
1007 io_tree = &BTRFS_I(page->mapping->host)->io_tree; 1003 io_tree = &BTRFS_I(page->mapping->host)->io_tree;
1008 info = BTRFS_I(page->mapping->host)->root->fs_info; 1004 info = BTRFS_I(page->mapping->host)->root->fs_info;
1009 } 1005 }
1010 /* are we fully contained in this bio? */ 1006 /* are we fully contained in this bio? */
1011 if (buf_len <= length) 1007 if (buf_len <= length)
1012 return 1; 1008 return 1;
1013 1009
1014 ret = extent_range_uptodate(io_tree, start + length, 1010 ret = extent_range_uptodate(io_tree, start + length,
1015 start + buf_len - 1); 1011 start + buf_len - 1);
1016 if (ret == 1) 1012 if (ret == 1)
1017 return ret; 1013 return ret;
1018 return ret; 1014 return ret;
1019 } 1015 }
1020 1016
1021 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) 1017 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1022 static void btrfs_end_io_csum(void *p) 1018 static void btrfs_end_io_csum(void *p)
1023 #else 1019 #else
1024 static void btrfs_end_io_csum(struct work_struct *work) 1020 static void btrfs_end_io_csum(struct work_struct *work)
1025 #endif 1021 #endif
1026 { 1022 {
1027 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) 1023 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1028 struct btrfs_fs_info *fs_info = p; 1024 struct btrfs_fs_info *fs_info = p;
1029 #else 1025 #else
1030 struct btrfs_fs_info *fs_info = container_of(work, 1026 struct btrfs_fs_info *fs_info = container_of(work,
1031 struct btrfs_fs_info, 1027 struct btrfs_fs_info,
1032 end_io_work); 1028 end_io_work);
1033 #endif 1029 #endif
1034 unsigned long flags; 1030 unsigned long flags;
1035 struct end_io_wq *end_io_wq; 1031 struct end_io_wq *end_io_wq;
1036 struct bio *bio; 1032 struct bio *bio;
1037 struct list_head *next; 1033 struct list_head *next;
1038 int error; 1034 int error;
1039 int was_empty; 1035 int was_empty;
1040 1036
1041 while(1) { 1037 while(1) {
1042 spin_lock_irqsave(&fs_info->end_io_work_lock, flags); 1038 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
1043 if (list_empty(&fs_info->end_io_work_list)) { 1039 if (list_empty(&fs_info->end_io_work_list)) {
1044 spin_unlock_irqrestore(&fs_info->end_io_work_lock, 1040 spin_unlock_irqrestore(&fs_info->end_io_work_lock,
1045 flags); 1041 flags);
1046 return; 1042 return;
1047 } 1043 }
1048 next = fs_info->end_io_work_list.next; 1044 next = fs_info->end_io_work_list.next;
1049 list_del(next); 1045 list_del(next);
1050 spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags); 1046 spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
1051 1047
1052 end_io_wq = list_entry(next, struct end_io_wq, list); 1048 end_io_wq = list_entry(next, struct end_io_wq, list);
1053 1049
1054 bio = end_io_wq->bio; 1050 bio = end_io_wq->bio;
1055 if (end_io_wq->metadata && !bio_ready_for_csum(bio)) { 1051 if (end_io_wq->metadata && !bio_ready_for_csum(bio)) {
1056 spin_lock_irqsave(&fs_info->end_io_work_lock, flags); 1052 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
1057 was_empty = list_empty(&fs_info->end_io_work_list); 1053 was_empty = list_empty(&fs_info->end_io_work_list);
1058 list_add_tail(&end_io_wq->list, 1054 list_add_tail(&end_io_wq->list,
1059 &fs_info->end_io_work_list); 1055 &fs_info->end_io_work_list);
1060 spin_unlock_irqrestore(&fs_info->end_io_work_lock, 1056 spin_unlock_irqrestore(&fs_info->end_io_work_lock,
1061 flags); 1057 flags);
1062 if (was_empty) 1058 if (was_empty)
1063 return; 1059 return;
1064 continue; 1060 continue;
1065 } 1061 }
1066 error = end_io_wq->error; 1062 error = end_io_wq->error;
1067 bio->bi_private = end_io_wq->private; 1063 bio->bi_private = end_io_wq->private;
1068 bio->bi_end_io = end_io_wq->end_io; 1064 bio->bi_end_io = end_io_wq->end_io;
1069 kfree(end_io_wq); 1065 kfree(end_io_wq);
1070 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) 1066 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1071 bio_endio(bio, bio->bi_size, error); 1067 bio_endio(bio, bio->bi_size, error);
1072 #else 1068 #else
1073 bio_endio(bio, error); 1069 bio_endio(bio, error);
1074 #endif 1070 #endif
1075 } 1071 }
1076 } 1072 }
1077 1073
1078 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) 1074 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1079 static void btrfs_async_submit_work(void *p) 1075 static void btrfs_async_submit_work(void *p)
1080 #else 1076 #else
1081 static void btrfs_async_submit_work(struct work_struct *work) 1077 static void btrfs_async_submit_work(struct work_struct *work)
1082 #endif 1078 #endif
1083 { 1079 {
1084 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) 1080 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1085 struct btrfs_fs_info *fs_info = p; 1081 struct btrfs_fs_info *fs_info = p;
1086 #else 1082 #else
1087 struct btrfs_fs_info *fs_info = container_of(work, 1083 struct btrfs_fs_info *fs_info = container_of(work,
1088 struct btrfs_fs_info, 1084 struct btrfs_fs_info,
1089 async_submit_work); 1085 async_submit_work);
1090 #endif 1086 #endif
1091 struct async_submit_bio *async; 1087 struct async_submit_bio *async;
1092 struct list_head *next; 1088 struct list_head *next;
1093 1089
1094 while(1) { 1090 while(1) {
1095 spin_lock(&fs_info->async_submit_work_lock); 1091 spin_lock(&fs_info->async_submit_work_lock);
1096 if (list_empty(&fs_info->async_submit_work_list)) { 1092 if (list_empty(&fs_info->async_submit_work_list)) {
1097 spin_unlock(&fs_info->async_submit_work_lock); 1093 spin_unlock(&fs_info->async_submit_work_lock);
1098 return; 1094 return;
1099 } 1095 }
1100 next = fs_info->async_submit_work_list.next; 1096 next = fs_info->async_submit_work_list.next;
1101 list_del(next); 1097 list_del(next);
1102 spin_unlock(&fs_info->async_submit_work_lock); 1098 spin_unlock(&fs_info->async_submit_work_lock);
1103 1099
1104 async = list_entry(next, struct async_submit_bio, list); 1100 async = list_entry(next, struct async_submit_bio, list);
1105 async->submit_bio_hook(async->inode, async->rw, async->bio, 1101 async->submit_bio_hook(async->inode, async->rw, async->bio,
1106 async->mirror_num); 1102 async->mirror_num);
1107 kfree(async); 1103 kfree(async);
1108 } 1104 }
1109 } 1105 }
1110 1106
1111 struct btrfs_root *open_ctree(struct super_block *sb, 1107 struct btrfs_root *open_ctree(struct super_block *sb,
1112 struct btrfs_fs_devices *fs_devices) 1108 struct btrfs_fs_devices *fs_devices)
1113 { 1109 {
1114 u32 sectorsize; 1110 u32 sectorsize;
1115 u32 nodesize; 1111 u32 nodesize;
1116 u32 leafsize; 1112 u32 leafsize;
1117 u32 blocksize; 1113 u32 blocksize;
1118 u32 stripesize; 1114 u32 stripesize;
1119 struct buffer_head *bh; 1115 struct buffer_head *bh;
1120 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), 1116 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
1121 GFP_NOFS); 1117 GFP_NOFS);
1122 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), 1118 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
1123 GFP_NOFS); 1119 GFP_NOFS);
1124 struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info), 1120 struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
1125 GFP_NOFS); 1121 GFP_NOFS);
1126 struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root), 1122 struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
1127 GFP_NOFS); 1123 GFP_NOFS);
1128 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root), 1124 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
1129 GFP_NOFS); 1125 GFP_NOFS);
1130 int ret; 1126 int ret;
1131 int err = -EINVAL; 1127 int err = -EINVAL;
1132 struct btrfs_super_block *disk_super; 1128 struct btrfs_super_block *disk_super;
1133 1129
1134 if (!extent_root || !tree_root || !fs_info) { 1130 if (!extent_root || !tree_root || !fs_info) {
1135 err = -ENOMEM; 1131 err = -ENOMEM;
1136 goto fail; 1132 goto fail;
1137 } 1133 }
1138 end_io_workqueue = create_workqueue("btrfs-end-io"); 1134 end_io_workqueue = create_workqueue("btrfs-end-io");
1139 BUG_ON(!end_io_workqueue); 1135 BUG_ON(!end_io_workqueue);
1140 async_submit_workqueue = create_workqueue("btrfs-async-submit"); 1136 async_submit_workqueue = create_workqueue("btrfs-async-submit");
1141 1137
1142 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); 1138 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
1143 INIT_LIST_HEAD(&fs_info->trans_list); 1139 INIT_LIST_HEAD(&fs_info->trans_list);
1144 INIT_LIST_HEAD(&fs_info->dead_roots); 1140 INIT_LIST_HEAD(&fs_info->dead_roots);
1145 INIT_LIST_HEAD(&fs_info->hashers); 1141 INIT_LIST_HEAD(&fs_info->hashers);
1146 INIT_LIST_HEAD(&fs_info->end_io_work_list); 1142 INIT_LIST_HEAD(&fs_info->end_io_work_list);
1147 INIT_LIST_HEAD(&fs_info->async_submit_work_list); 1143 INIT_LIST_HEAD(&fs_info->async_submit_work_list);
1148 spin_lock_init(&fs_info->hash_lock); 1144 spin_lock_init(&fs_info->hash_lock);
1149 spin_lock_init(&fs_info->end_io_work_lock); 1145 spin_lock_init(&fs_info->end_io_work_lock);
1150 spin_lock_init(&fs_info->async_submit_work_lock); 1146 spin_lock_init(&fs_info->async_submit_work_lock);
1151 spin_lock_init(&fs_info->delalloc_lock); 1147 spin_lock_init(&fs_info->delalloc_lock);
1152 spin_lock_init(&fs_info->new_trans_lock); 1148 spin_lock_init(&fs_info->new_trans_lock);
1153 1149
1154 init_completion(&fs_info->kobj_unregister); 1150 init_completion(&fs_info->kobj_unregister);
1155 fs_info->tree_root = tree_root; 1151 fs_info->tree_root = tree_root;
1156 fs_info->extent_root = extent_root; 1152 fs_info->extent_root = extent_root;
1157 fs_info->chunk_root = chunk_root; 1153 fs_info->chunk_root = chunk_root;
1158 fs_info->dev_root = dev_root; 1154 fs_info->dev_root = dev_root;
1159 fs_info->fs_devices = fs_devices; 1155 fs_info->fs_devices = fs_devices;
1160 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 1156 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
1161 INIT_LIST_HEAD(&fs_info->space_info); 1157 INIT_LIST_HEAD(&fs_info->space_info);
1162 btrfs_mapping_init(&fs_info->mapping_tree); 1158 btrfs_mapping_init(&fs_info->mapping_tree);
1163 fs_info->sb = sb; 1159 fs_info->sb = sb;
1164 fs_info->max_extent = (u64)-1; 1160 fs_info->max_extent = (u64)-1;
1165 fs_info->max_inline = 8192 * 1024; 1161 fs_info->max_inline = 8192 * 1024;
1166 setup_bdi(fs_info, &fs_info->bdi); 1162 setup_bdi(fs_info, &fs_info->bdi);
1167 fs_info->btree_inode = new_inode(sb); 1163 fs_info->btree_inode = new_inode(sb);
1168 fs_info->btree_inode->i_ino = 1; 1164 fs_info->btree_inode->i_ino = 1;
1169 fs_info->btree_inode->i_nlink = 1; 1165 fs_info->btree_inode->i_nlink = 1;
1170 1166
1171 sb->s_blocksize = 4096; 1167 sb->s_blocksize = 4096;
1172 sb->s_blocksize_bits = blksize_bits(4096); 1168 sb->s_blocksize_bits = blksize_bits(4096);
1173 1169
1174 /* 1170 /*
1175 * we set the i_size on the btree inode to the max possible int. 1171 * we set the i_size on the btree inode to the max possible int.
1176 * the real end of the address space is determined by all of 1172 * the real end of the address space is determined by all of
1177 * the devices in the system 1173 * the devices in the system
1178 */ 1174 */
1179 fs_info->btree_inode->i_size = OFFSET_MAX; 1175 fs_info->btree_inode->i_size = OFFSET_MAX;
1180 fs_info->btree_inode->i_mapping->a_ops = &btree_aops; 1176 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
1181 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi; 1177 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
1182 1178
1183 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, 1179 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
1184 fs_info->btree_inode->i_mapping, 1180 fs_info->btree_inode->i_mapping,
1185 GFP_NOFS); 1181 GFP_NOFS);
1186 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree, 1182 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
1187 GFP_NOFS); 1183 GFP_NOFS);
1188 1184
1189 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; 1185 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
1190 1186
1191 extent_io_tree_init(&fs_info->free_space_cache, 1187 extent_io_tree_init(&fs_info->free_space_cache,
1192 fs_info->btree_inode->i_mapping, GFP_NOFS); 1188 fs_info->btree_inode->i_mapping, GFP_NOFS);
1193 extent_io_tree_init(&fs_info->block_group_cache, 1189 extent_io_tree_init(&fs_info->block_group_cache,
1194 fs_info->btree_inode->i_mapping, GFP_NOFS); 1190 fs_info->btree_inode->i_mapping, GFP_NOFS);
1195 extent_io_tree_init(&fs_info->pinned_extents, 1191 extent_io_tree_init(&fs_info->pinned_extents,
1196 fs_info->btree_inode->i_mapping, GFP_NOFS); 1192 fs_info->btree_inode->i_mapping, GFP_NOFS);
1197 extent_io_tree_init(&fs_info->pending_del, 1193 extent_io_tree_init(&fs_info->pending_del,
1198 fs_info->btree_inode->i_mapping, GFP_NOFS); 1194 fs_info->btree_inode->i_mapping, GFP_NOFS);
1199 extent_io_tree_init(&fs_info->extent_ins, 1195 extent_io_tree_init(&fs_info->extent_ins,
1200 fs_info->btree_inode->i_mapping, GFP_NOFS); 1196 fs_info->btree_inode->i_mapping, GFP_NOFS);
1201 fs_info->do_barriers = 1; 1197 fs_info->do_barriers = 1;
1202 1198
1203 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) 1199 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1204 INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum, fs_info); 1200 INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum, fs_info);
1205 INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work, 1201 INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work,
1206 fs_info); 1202 fs_info);
1207 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info); 1203 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
1208 #else 1204 #else
1209 INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum); 1205 INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum);
1210 INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work); 1206 INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work);
1211 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner); 1207 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
1212 #endif 1208 #endif
1213 BTRFS_I(fs_info->btree_inode)->root = tree_root; 1209 BTRFS_I(fs_info->btree_inode)->root = tree_root;
1214 memset(&BTRFS_I(fs_info->btree_inode)->location, 0, 1210 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
1215 sizeof(struct btrfs_key)); 1211 sizeof(struct btrfs_key));
1216 insert_inode_hash(fs_info->btree_inode); 1212 insert_inode_hash(fs_info->btree_inode);
1217 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 1213 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
1218 1214
1219 mutex_init(&fs_info->trans_mutex); 1215 mutex_init(&fs_info->trans_mutex);
1220 mutex_init(&fs_info->fs_mutex); 1216 mutex_init(&fs_info->fs_mutex);
1221 1217
1222 #if 0 1218 #if 0
1223 ret = add_hasher(fs_info, "crc32c"); 1219 ret = add_hasher(fs_info, "crc32c");
1224 if (ret) { 1220 if (ret) {
1225 printk("btrfs: failed hash setup, modprobe cryptomgr?\n"); 1221 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
1226 err = -ENOMEM; 1222 err = -ENOMEM;
1227 goto fail_iput; 1223 goto fail_iput;
1228 } 1224 }
1229 #endif 1225 #endif
1230 __setup_root(4096, 4096, 4096, 4096, tree_root, 1226 __setup_root(4096, 4096, 4096, 4096, tree_root,
1231 fs_info, BTRFS_ROOT_TREE_OBJECTID); 1227 fs_info, BTRFS_ROOT_TREE_OBJECTID);
1232 1228
1233 1229
1234 bh = __bread(fs_devices->latest_bdev, 1230 bh = __bread(fs_devices->latest_bdev,
1235 BTRFS_SUPER_INFO_OFFSET / 4096, 4096); 1231 BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
1236 if (!bh) 1232 if (!bh)
1237 goto fail_iput; 1233 goto fail_iput;
1238 1234
1239 memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy)); 1235 memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
1240 brelse(bh); 1236 brelse(bh);
1241 1237
1242 memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE); 1238 memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
1243 1239
1244 disk_super = &fs_info->super_copy; 1240 disk_super = &fs_info->super_copy;
1245 if (!btrfs_super_root(disk_super)) 1241 if (!btrfs_super_root(disk_super))
1246 goto fail_sb_buffer; 1242 goto fail_sb_buffer;
1247 1243
1248 if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) { 1244 if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
1249 printk("Btrfs: wanted %llu devices, but found %llu\n", 1245 printk("Btrfs: wanted %llu devices, but found %llu\n",
1250 (unsigned long long)btrfs_super_num_devices(disk_super), 1246 (unsigned long long)btrfs_super_num_devices(disk_super),
1251 (unsigned long long)fs_devices->num_devices); 1247 (unsigned long long)fs_devices->num_devices);
1252 goto fail_sb_buffer; 1248 goto fail_sb_buffer;
1253 } 1249 }
1254 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); 1250 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
1255 1251
1256 nodesize = btrfs_super_nodesize(disk_super); 1252 nodesize = btrfs_super_nodesize(disk_super);
1257 leafsize = btrfs_super_leafsize(disk_super); 1253 leafsize = btrfs_super_leafsize(disk_super);
1258 sectorsize = btrfs_super_sectorsize(disk_super); 1254 sectorsize = btrfs_super_sectorsize(disk_super);
1259 stripesize = btrfs_super_stripesize(disk_super); 1255 stripesize = btrfs_super_stripesize(disk_super);
1260 tree_root->nodesize = nodesize; 1256 tree_root->nodesize = nodesize;
1261 tree_root->leafsize = leafsize; 1257 tree_root->leafsize = leafsize;
1262 tree_root->sectorsize = sectorsize; 1258 tree_root->sectorsize = sectorsize;
1263 tree_root->stripesize = stripesize; 1259 tree_root->stripesize = stripesize;
1264 1260
1265 sb->s_blocksize = sectorsize; 1261 sb->s_blocksize = sectorsize;
1266 sb->s_blocksize_bits = blksize_bits(sectorsize); 1262 sb->s_blocksize_bits = blksize_bits(sectorsize);
1267 1263
1268 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, 1264 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
1269 sizeof(disk_super->magic))) { 1265 sizeof(disk_super->magic))) {
1270 printk("btrfs: valid FS not found on %s\n", sb->s_id); 1266 printk("btrfs: valid FS not found on %s\n", sb->s_id);
1271 goto fail_sb_buffer; 1267 goto fail_sb_buffer;
1272 } 1268 }
1273 1269
1274 mutex_lock(&fs_info->fs_mutex); 1270 mutex_lock(&fs_info->fs_mutex);
1275 1271
1276 ret = btrfs_read_sys_array(tree_root); 1272 ret = btrfs_read_sys_array(tree_root);
1277 if (ret) { 1273 if (ret) {
1278 printk("btrfs: failed to read the system array on %s\n", 1274 printk("btrfs: failed to read the system array on %s\n",
1279 sb->s_id); 1275 sb->s_id);
1280 goto fail_sys_array; 1276 goto fail_sys_array;
1281 } 1277 }
1282 1278
1283 blocksize = btrfs_level_size(tree_root, 1279 blocksize = btrfs_level_size(tree_root,
1284 btrfs_super_chunk_root_level(disk_super)); 1280 btrfs_super_chunk_root_level(disk_super));
1285 1281
1286 __setup_root(nodesize, leafsize, sectorsize, stripesize, 1282 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1287 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 1283 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1288 1284
1289 chunk_root->node = read_tree_block(chunk_root, 1285 chunk_root->node = read_tree_block(chunk_root,
1290 btrfs_super_chunk_root(disk_super), 1286 btrfs_super_chunk_root(disk_super),
1291 blocksize); 1287 blocksize, 0);
1292 BUG_ON(!chunk_root->node); 1288 BUG_ON(!chunk_root->node);
1293 1289
1294 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, 1290 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
1295 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node), 1291 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
1296 BTRFS_UUID_SIZE); 1292 BTRFS_UUID_SIZE);
1297 1293
1298 ret = btrfs_read_chunk_tree(chunk_root); 1294 ret = btrfs_read_chunk_tree(chunk_root);
1299 BUG_ON(ret); 1295 BUG_ON(ret);
1300 1296
1301 blocksize = btrfs_level_size(tree_root, 1297 blocksize = btrfs_level_size(tree_root,
1302 btrfs_super_root_level(disk_super)); 1298 btrfs_super_root_level(disk_super));
1303 1299
1304 1300
1305 tree_root->node = read_tree_block(tree_root, 1301 tree_root->node = read_tree_block(tree_root,
1306 btrfs_super_root(disk_super), 1302 btrfs_super_root(disk_super),
1307 blocksize); 1303 blocksize, 0);
1308 if (!tree_root->node) 1304 if (!tree_root->node)
1309 goto fail_sb_buffer; 1305 goto fail_sb_buffer;
1310 1306
1311 1307
1312 ret = find_and_setup_root(tree_root, fs_info, 1308 ret = find_and_setup_root(tree_root, fs_info,
1313 BTRFS_EXTENT_TREE_OBJECTID, extent_root); 1309 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
1314 if (ret) 1310 if (ret)
1315 goto fail_tree_root; 1311 goto fail_tree_root;
1316 extent_root->track_dirty = 1; 1312 extent_root->track_dirty = 1;
1317 1313
1318 ret = find_and_setup_root(tree_root, fs_info, 1314 ret = find_and_setup_root(tree_root, fs_info,
1319 BTRFS_DEV_TREE_OBJECTID, dev_root); 1315 BTRFS_DEV_TREE_OBJECTID, dev_root);
1320 dev_root->track_dirty = 1; 1316 dev_root->track_dirty = 1;
1321 1317
1322 if (ret) 1318 if (ret)
1323 goto fail_extent_root; 1319 goto fail_extent_root;
1324 1320
1325 btrfs_read_block_groups(extent_root); 1321 btrfs_read_block_groups(extent_root);
1326 1322
1327 fs_info->generation = btrfs_super_generation(disk_super) + 1; 1323 fs_info->generation = btrfs_super_generation(disk_super) + 1;
1328 fs_info->data_alloc_profile = (u64)-1; 1324 fs_info->data_alloc_profile = (u64)-1;
1329 fs_info->metadata_alloc_profile = (u64)-1; 1325 fs_info->metadata_alloc_profile = (u64)-1;
1330 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile; 1326 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1331 1327
1332 mutex_unlock(&fs_info->fs_mutex); 1328 mutex_unlock(&fs_info->fs_mutex);
1333 return tree_root; 1329 return tree_root;
1334 1330
1335 fail_extent_root: 1331 fail_extent_root:
1336 free_extent_buffer(extent_root->node); 1332 free_extent_buffer(extent_root->node);
1337 fail_tree_root: 1333 fail_tree_root:
1338 free_extent_buffer(tree_root->node); 1334 free_extent_buffer(tree_root->node);
1339 fail_sys_array: 1335 fail_sys_array:
1340 mutex_unlock(&fs_info->fs_mutex); 1336 mutex_unlock(&fs_info->fs_mutex);
1341 fail_sb_buffer: 1337 fail_sb_buffer:
1342 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree); 1338 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
1343 fail_iput: 1339 fail_iput:
1344 iput(fs_info->btree_inode); 1340 iput(fs_info->btree_inode);
1345 fail: 1341 fail:
1346 close_all_devices(fs_info); 1342 close_all_devices(fs_info);
1347 btrfs_mapping_tree_free(&fs_info->mapping_tree); 1343 btrfs_mapping_tree_free(&fs_info->mapping_tree);
1348 1344
1349 kfree(extent_root); 1345 kfree(extent_root);
1350 kfree(tree_root); 1346 kfree(tree_root);
1351 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) 1347 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
1352 bdi_destroy(&fs_info->bdi); 1348 bdi_destroy(&fs_info->bdi);
1353 #endif 1349 #endif
1354 kfree(fs_info); 1350 kfree(fs_info);
1355 return ERR_PTR(err); 1351 return ERR_PTR(err);
1356 } 1352 }
1357 1353
1358 static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) 1354 static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
1359 { 1355 {
1360 char b[BDEVNAME_SIZE]; 1356 char b[BDEVNAME_SIZE];
1361 1357
1362 if (uptodate) { 1358 if (uptodate) {
1363 set_buffer_uptodate(bh); 1359 set_buffer_uptodate(bh);
1364 } else { 1360 } else {
1365 if (!buffer_eopnotsupp(bh) && printk_ratelimit()) { 1361 if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
1366 printk(KERN_WARNING "lost page write due to " 1362 printk(KERN_WARNING "lost page write due to "
1367 "I/O error on %s\n", 1363 "I/O error on %s\n",
1368 bdevname(bh->b_bdev, b)); 1364 bdevname(bh->b_bdev, b));
1369 } 1365 }
1370 set_buffer_write_io_error(bh); 1366 set_buffer_write_io_error(bh);
1371 clear_buffer_uptodate(bh); 1367 clear_buffer_uptodate(bh);
1372 } 1368 }
1373 unlock_buffer(bh); 1369 unlock_buffer(bh);
1374 put_bh(bh); 1370 put_bh(bh);
1375 } 1371 }
1376 1372
1377 int write_all_supers(struct btrfs_root *root) 1373 int write_all_supers(struct btrfs_root *root)
1378 { 1374 {
1379 struct list_head *cur; 1375 struct list_head *cur;
1380 struct list_head *head = &root->fs_info->fs_devices->devices; 1376 struct list_head *head = &root->fs_info->fs_devices->devices;
1381 struct btrfs_device *dev; 1377 struct btrfs_device *dev;
1382 struct btrfs_super_block *sb; 1378 struct btrfs_super_block *sb;
1383 struct btrfs_dev_item *dev_item; 1379 struct btrfs_dev_item *dev_item;
1384 struct buffer_head *bh; 1380 struct buffer_head *bh;
1385 int ret; 1381 int ret;
1386 int do_barriers; 1382 int do_barriers;
1387 int max_errors; 1383 int max_errors;
1388 int total_errors = 0; 1384 int total_errors = 0;
1389 u32 crc; 1385 u32 crc;
1390 u64 flags; 1386 u64 flags;
1391 1387
1392 max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1; 1388 max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1393 do_barriers = !btrfs_test_opt(root, NOBARRIER); 1389 do_barriers = !btrfs_test_opt(root, NOBARRIER);
1394 1390
1395 sb = &root->fs_info->super_for_commit; 1391 sb = &root->fs_info->super_for_commit;
1396 dev_item = &sb->dev_item; 1392 dev_item = &sb->dev_item;
1397 list_for_each(cur, head) { 1393 list_for_each(cur, head) {
1398 dev = list_entry(cur, struct btrfs_device, dev_list); 1394 dev = list_entry(cur, struct btrfs_device, dev_list);
1399 btrfs_set_stack_device_type(dev_item, dev->type); 1395 btrfs_set_stack_device_type(dev_item, dev->type);
1400 btrfs_set_stack_device_id(dev_item, dev->devid); 1396 btrfs_set_stack_device_id(dev_item, dev->devid);
1401 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes); 1397 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1402 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used); 1398 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1403 btrfs_set_stack_device_io_align(dev_item, dev->io_align); 1399 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1404 btrfs_set_stack_device_io_width(dev_item, dev->io_width); 1400 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1405 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); 1401 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1406 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); 1402 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1407 flags = btrfs_super_flags(sb); 1403 flags = btrfs_super_flags(sb);
1408 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); 1404 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1409 1405
1410 1406
1411 crc = ~(u32)0; 1407 crc = ~(u32)0;
1412 crc = btrfs_csum_data(root, (char *)sb + BTRFS_CSUM_SIZE, crc, 1408 crc = btrfs_csum_data(root, (char *)sb + BTRFS_CSUM_SIZE, crc,
1413 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); 1409 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1414 btrfs_csum_final(crc, sb->csum); 1410 btrfs_csum_final(crc, sb->csum);
1415 1411
1416 bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 1412 bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET / 4096,
1417 BTRFS_SUPER_INFO_SIZE); 1413 BTRFS_SUPER_INFO_SIZE);
1418 1414
1419 memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); 1415 memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
1420 dev->pending_io = bh; 1416 dev->pending_io = bh;
1421 1417
1422 get_bh(bh); 1418 get_bh(bh);
1423 set_buffer_uptodate(bh); 1419 set_buffer_uptodate(bh);
1424 lock_buffer(bh); 1420 lock_buffer(bh);
1425 bh->b_end_io = btrfs_end_buffer_write_sync; 1421 bh->b_end_io = btrfs_end_buffer_write_sync;
1426 1422
1427 if (do_barriers && dev->barriers) { 1423 if (do_barriers && dev->barriers) {
1428 ret = submit_bh(WRITE_BARRIER, bh); 1424 ret = submit_bh(WRITE_BARRIER, bh);
1429 if (ret == -EOPNOTSUPP) { 1425 if (ret == -EOPNOTSUPP) {
1430 printk("btrfs: disabling barriers on dev %s\n", 1426 printk("btrfs: disabling barriers on dev %s\n",
1431 dev->name); 1427 dev->name);
1432 set_buffer_uptodate(bh); 1428 set_buffer_uptodate(bh);
1433 dev->barriers = 0; 1429 dev->barriers = 0;
1434 get_bh(bh); 1430 get_bh(bh);
1435 lock_buffer(bh); 1431 lock_buffer(bh);
1436 ret = submit_bh(WRITE, bh); 1432 ret = submit_bh(WRITE, bh);
1437 } 1433 }
1438 } else { 1434 } else {
1439 ret = submit_bh(WRITE, bh); 1435 ret = submit_bh(WRITE, bh);
1440 } 1436 }
1441 if (ret) 1437 if (ret)
1442 total_errors++; 1438 total_errors++;
1443 } 1439 }
1444 if (total_errors > max_errors) { 1440 if (total_errors > max_errors) {
1445 printk("btrfs: %d errors while writing supers\n", total_errors); 1441 printk("btrfs: %d errors while writing supers\n", total_errors);
1446 BUG(); 1442 BUG();
1447 } 1443 }
1448 total_errors = 0; 1444 total_errors = 0;
1449 1445
1450 list_for_each(cur, head) { 1446 list_for_each(cur, head) {
1451 dev = list_entry(cur, struct btrfs_device, dev_list); 1447 dev = list_entry(cur, struct btrfs_device, dev_list);
1452 BUG_ON(!dev->pending_io); 1448 BUG_ON(!dev->pending_io);
1453 bh = dev->pending_io; 1449 bh = dev->pending_io;
1454 wait_on_buffer(bh); 1450 wait_on_buffer(bh);
1455 if (!buffer_uptodate(dev->pending_io)) { 1451 if (!buffer_uptodate(dev->pending_io)) {
1456 if (do_barriers && dev->barriers) { 1452 if (do_barriers && dev->barriers) {
1457 printk("btrfs: disabling barriers on dev %s\n", 1453 printk("btrfs: disabling barriers on dev %s\n",
1458 dev->name); 1454 dev->name);
1459 set_buffer_uptodate(bh); 1455 set_buffer_uptodate(bh);
1460 get_bh(bh); 1456 get_bh(bh);
1461 lock_buffer(bh); 1457 lock_buffer(bh);
1462 dev->barriers = 0; 1458 dev->barriers = 0;
1463 ret = submit_bh(WRITE, bh); 1459 ret = submit_bh(WRITE, bh);
1464 BUG_ON(ret); 1460 BUG_ON(ret);
1465 wait_on_buffer(bh); 1461 wait_on_buffer(bh);
1466 BUG_ON(!buffer_uptodate(bh)); 1462 BUG_ON(!buffer_uptodate(bh));
1467 } else { 1463 } else {
1468 total_errors++; 1464 total_errors++;
1469 } 1465 }
1470 1466
1471 } 1467 }
1472 dev->pending_io = NULL; 1468 dev->pending_io = NULL;
1473 brelse(bh); 1469 brelse(bh);
1474 } 1470 }
1475 if (total_errors > max_errors) { 1471 if (total_errors > max_errors) {
1476 printk("btrfs: %d errors while writing supers\n", total_errors); 1472 printk("btrfs: %d errors while writing supers\n", total_errors);
1477 BUG(); 1473 BUG();
1478 } 1474 }
1479 return 0; 1475 return 0;
1480 } 1476 }
1481 1477
1482 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root 1478 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
1483 *root) 1479 *root)
1484 { 1480 {
1485 int ret; 1481 int ret;
1486 1482
1487 ret = write_all_supers(root); 1483 ret = write_all_supers(root);
1488 return ret; 1484 return ret;
1489 } 1485 }
1490 1486
1491 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) 1487 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
1492 { 1488 {
1493 radix_tree_delete(&fs_info->fs_roots_radix, 1489 radix_tree_delete(&fs_info->fs_roots_radix,
1494 (unsigned long)root->root_key.objectid); 1490 (unsigned long)root->root_key.objectid);
1495 if (root->in_sysfs) 1491 if (root->in_sysfs)
1496 btrfs_sysfs_del_root(root); 1492 btrfs_sysfs_del_root(root);
1497 if (root->inode) 1493 if (root->inode)
1498 iput(root->inode); 1494 iput(root->inode);
1499 if (root->node) 1495 if (root->node)
1500 free_extent_buffer(root->node); 1496 free_extent_buffer(root->node);
1501 if (root->commit_root) 1497 if (root->commit_root)
1502 free_extent_buffer(root->commit_root); 1498 free_extent_buffer(root->commit_root);
1503 if (root->name) 1499 if (root->name)
1504 kfree(root->name); 1500 kfree(root->name);
1505 kfree(root); 1501 kfree(root);
1506 return 0; 1502 return 0;
1507 } 1503 }
1508 1504
1509 static int del_fs_roots(struct btrfs_fs_info *fs_info) 1505 static int del_fs_roots(struct btrfs_fs_info *fs_info)
1510 { 1506 {
1511 int ret; 1507 int ret;
1512 struct btrfs_root *gang[8]; 1508 struct btrfs_root *gang[8];
1513 int i; 1509 int i;
1514 1510
1515 while(1) { 1511 while(1) {
1516 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 1512 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
1517 (void **)gang, 0, 1513 (void **)gang, 0,
1518 ARRAY_SIZE(gang)); 1514 ARRAY_SIZE(gang));
1519 if (!ret) 1515 if (!ret)
1520 break; 1516 break;
1521 for (i = 0; i < ret; i++) 1517 for (i = 0; i < ret; i++)
1522 btrfs_free_fs_root(fs_info, gang[i]); 1518 btrfs_free_fs_root(fs_info, gang[i]);
1523 } 1519 }
1524 return 0; 1520 return 0;
1525 } 1521 }
1526 1522
1527 int close_ctree(struct btrfs_root *root) 1523 int close_ctree(struct btrfs_root *root)
1528 { 1524 {
1529 int ret; 1525 int ret;
1530 struct btrfs_trans_handle *trans; 1526 struct btrfs_trans_handle *trans;
1531 struct btrfs_fs_info *fs_info = root->fs_info; 1527 struct btrfs_fs_info *fs_info = root->fs_info;
1532 1528
1533 fs_info->closing = 1; 1529 fs_info->closing = 1;
1534 btrfs_transaction_flush_work(root); 1530 btrfs_transaction_flush_work(root);
1535 mutex_lock(&fs_info->fs_mutex); 1531 mutex_lock(&fs_info->fs_mutex);
1536 btrfs_defrag_dirty_roots(root->fs_info); 1532 btrfs_defrag_dirty_roots(root->fs_info);
1537 trans = btrfs_start_transaction(root, 1); 1533 trans = btrfs_start_transaction(root, 1);
1538 ret = btrfs_commit_transaction(trans, root); 1534 ret = btrfs_commit_transaction(trans, root);
1539 /* run commit again to drop the original snapshot */ 1535 /* run commit again to drop the original snapshot */
1540 trans = btrfs_start_transaction(root, 1); 1536 trans = btrfs_start_transaction(root, 1);
1541 btrfs_commit_transaction(trans, root); 1537 btrfs_commit_transaction(trans, root);
1542 ret = btrfs_write_and_wait_transaction(NULL, root); 1538 ret = btrfs_write_and_wait_transaction(NULL, root);
1543 BUG_ON(ret); 1539 BUG_ON(ret);
1544 1540
1545 write_ctree_super(NULL, root); 1541 write_ctree_super(NULL, root);
1546 mutex_unlock(&fs_info->fs_mutex); 1542 mutex_unlock(&fs_info->fs_mutex);
1547 1543
1548 btrfs_transaction_flush_work(root); 1544 btrfs_transaction_flush_work(root);
1549 1545
1550 if (fs_info->delalloc_bytes) { 1546 if (fs_info->delalloc_bytes) {
1551 printk("btrfs: at unmount delalloc count %Lu\n", 1547 printk("btrfs: at unmount delalloc count %Lu\n",
1552 fs_info->delalloc_bytes); 1548 fs_info->delalloc_bytes);
1553 } 1549 }
1554 if (fs_info->extent_root->node) 1550 if (fs_info->extent_root->node)
1555 free_extent_buffer(fs_info->extent_root->node); 1551 free_extent_buffer(fs_info->extent_root->node);
1556 1552
1557 if (fs_info->tree_root->node) 1553 if (fs_info->tree_root->node)
1558 free_extent_buffer(fs_info->tree_root->node); 1554 free_extent_buffer(fs_info->tree_root->node);
1559 1555
1560 if (root->fs_info->chunk_root->node); 1556 if (root->fs_info->chunk_root->node);
1561 free_extent_buffer(root->fs_info->chunk_root->node); 1557 free_extent_buffer(root->fs_info->chunk_root->node);
1562 1558
1563 if (root->fs_info->dev_root->node); 1559 if (root->fs_info->dev_root->node);
1564 free_extent_buffer(root->fs_info->dev_root->node); 1560 free_extent_buffer(root->fs_info->dev_root->node);
1565 1561
1566 btrfs_free_block_groups(root->fs_info); 1562 btrfs_free_block_groups(root->fs_info);
1567 del_fs_roots(fs_info); 1563 del_fs_roots(fs_info);
1568 1564
1569 filemap_write_and_wait(fs_info->btree_inode->i_mapping); 1565 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
1570 1566
1571 extent_io_tree_empty_lru(&fs_info->free_space_cache); 1567 extent_io_tree_empty_lru(&fs_info->free_space_cache);
1572 extent_io_tree_empty_lru(&fs_info->block_group_cache); 1568 extent_io_tree_empty_lru(&fs_info->block_group_cache);
1573 extent_io_tree_empty_lru(&fs_info->pinned_extents); 1569 extent_io_tree_empty_lru(&fs_info->pinned_extents);
1574 extent_io_tree_empty_lru(&fs_info->pending_del); 1570 extent_io_tree_empty_lru(&fs_info->pending_del);
1575 extent_io_tree_empty_lru(&fs_info->extent_ins); 1571 extent_io_tree_empty_lru(&fs_info->extent_ins);
1576 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree); 1572 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
1577 1573
1578 flush_workqueue(async_submit_workqueue); 1574 flush_workqueue(async_submit_workqueue);
1579 flush_workqueue(end_io_workqueue); 1575 flush_workqueue(end_io_workqueue);
1580 1576
1581 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); 1577 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
1582 1578
1583 flush_workqueue(async_submit_workqueue); 1579 flush_workqueue(async_submit_workqueue);
1584 destroy_workqueue(async_submit_workqueue); 1580 destroy_workqueue(async_submit_workqueue);
1585 1581
1586 flush_workqueue(end_io_workqueue); 1582 flush_workqueue(end_io_workqueue);
1587 destroy_workqueue(end_io_workqueue); 1583 destroy_workqueue(end_io_workqueue);
1588 1584
1589 iput(fs_info->btree_inode); 1585 iput(fs_info->btree_inode);
1590 #if 0 1586 #if 0
1591 while(!list_empty(&fs_info->hashers)) { 1587 while(!list_empty(&fs_info->hashers)) {
1592 struct btrfs_hasher *hasher; 1588 struct btrfs_hasher *hasher;
1593 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher, 1589 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
1594 hashers); 1590 hashers);
1595 list_del(&hasher->hashers); 1591 list_del(&hasher->hashers);
1596 crypto_free_hash(&fs_info->hash_tfm); 1592 crypto_free_hash(&fs_info->hash_tfm);
1597 kfree(hasher); 1593 kfree(hasher);
1598 } 1594 }
1599 #endif 1595 #endif
1600 close_all_devices(fs_info); 1596 close_all_devices(fs_info);
1601 btrfs_mapping_tree_free(&fs_info->mapping_tree); 1597 btrfs_mapping_tree_free(&fs_info->mapping_tree);
1602 1598
1603 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) 1599 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
1604 bdi_destroy(&fs_info->bdi); 1600 bdi_destroy(&fs_info->bdi);
1605 #endif 1601 #endif
1606 1602
1607 kfree(fs_info->extent_root); 1603 kfree(fs_info->extent_root);
1608 kfree(fs_info->tree_root); 1604 kfree(fs_info->tree_root);
1609 kfree(fs_info->chunk_root); 1605 kfree(fs_info->chunk_root);
1610 kfree(fs_info->dev_root); 1606 kfree(fs_info->dev_root);
1611 return 0; 1607 return 0;
1612 } 1608 }
1613 1609
1614 int btrfs_buffer_uptodate(struct extent_buffer *buf) 1610 int btrfs_buffer_uptodate(struct extent_buffer *buf)
1615 { 1611 {
1616 struct inode *btree_inode = buf->first_page->mapping->host; 1612 struct inode *btree_inode = buf->first_page->mapping->host;
1617 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf); 1613 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
1618 } 1614 }
1619 1615
1620 int btrfs_set_buffer_uptodate(struct extent_buffer *buf) 1616 int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
1621 { 1617 {
1622 struct inode *btree_inode = buf->first_page->mapping->host; 1618 struct inode *btree_inode = buf->first_page->mapping->host;
1623 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, 1619 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
1624 buf); 1620 buf);
1625 } 1621 }
1626 1622
1627 void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 1623 void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
1628 { 1624 {
1629 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; 1625 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1630 u64 transid = btrfs_header_generation(buf); 1626 u64 transid = btrfs_header_generation(buf);
1631 struct inode *btree_inode = root->fs_info->btree_inode; 1627 struct inode *btree_inode = root->fs_info->btree_inode;
1632 1628
1633 if (transid != root->fs_info->generation) { 1629 if (transid != root->fs_info->generation) {
1634 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n", 1630 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
1635 (unsigned long long)buf->start, 1631 (unsigned long long)buf->start,
1636 transid, root->fs_info->generation); 1632 transid, root->fs_info->generation);
1637 WARN_ON(1); 1633 WARN_ON(1);
1638 } 1634 }
1639 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf); 1635 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
1640 } 1636 }
1641 1637
1642 void btrfs_throttle(struct btrfs_root *root) 1638 void btrfs_throttle(struct btrfs_root *root)
1643 { 1639 {
1644 struct backing_dev_info *bdi; 1640 struct backing_dev_info *bdi;
1645 1641
1646 bdi = &root->fs_info->bdi; 1642 bdi = &root->fs_info->bdi;
1647 if (root->fs_info->throttles && bdi_write_congested(bdi)) { 1643 if (root->fs_info->throttles && bdi_write_congested(bdi)) {
1648 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18) 1644 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1649 congestion_wait(WRITE, HZ/20); 1645 congestion_wait(WRITE, HZ/20);
1650 #else 1646 #else
1651 blk_congestion_wait(WRITE, HZ/20); 1647 blk_congestion_wait(WRITE, HZ/20);
1652 #endif 1648 #endif
1653 } 1649 }
1654 } 1650 }
1655 1651
1656 void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) 1652 void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
1657 { 1653 {
1658 /* 1654 /*
1659 * looks as though older kernels can get into trouble with 1655 * looks as though older kernels can get into trouble with
1660 * this code, they end up stuck in balance_dirty_pages forever 1656 * this code, they end up stuck in balance_dirty_pages forever
1661 */ 1657 */
1662 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) 1658 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
1663 struct extent_io_tree *tree; 1659 struct extent_io_tree *tree;
1664 u64 num_dirty; 1660 u64 num_dirty;
1665 u64 start = 0; 1661 u64 start = 0;
1666 unsigned long thresh = 16 * 1024 * 1024; 1662 unsigned long thresh = 16 * 1024 * 1024;
1667 tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree; 1663 tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
1668 1664
1669 if (current_is_pdflush()) 1665 if (current_is_pdflush())
1670 return; 1666 return;
1671 1667
1672 num_dirty = count_range_bits(tree, &start, (u64)-1, 1668 num_dirty = count_range_bits(tree, &start, (u64)-1,
1673 thresh, EXTENT_DIRTY); 1669 thresh, EXTENT_DIRTY);
1674 if (num_dirty > thresh) { 1670 if (num_dirty > thresh) {
1675 balance_dirty_pages_ratelimited_nr( 1671 balance_dirty_pages_ratelimited_nr(
1676 root->fs_info->btree_inode->i_mapping, 1); 1672 root->fs_info->btree_inode->i_mapping, 1);
1677 } 1673 }
1678 #else 1674 #else
1679 return; 1675 return;
1680 #endif 1676 #endif
1681 } 1677 }
1682 1678
1683 void btrfs_set_buffer_defrag(struct extent_buffer *buf) 1679 void btrfs_set_buffer_defrag(struct extent_buffer *buf)
1684 { 1680 {
1685 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; 1681 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1686 struct inode *btree_inode = root->fs_info->btree_inode; 1682 struct inode *btree_inode = root->fs_info->btree_inode;
1687 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start, 1683 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
1688 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS); 1684 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
1689 } 1685 }
1690 1686
1691 void btrfs_set_buffer_defrag_done(struct extent_buffer *buf) 1687 void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
1692 { 1688 {
1693 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; 1689 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1694 struct inode *btree_inode = root->fs_info->btree_inode; 1690 struct inode *btree_inode = root->fs_info->btree_inode;
1695 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start, 1691 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
1696 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE, 1692 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
1697 GFP_NOFS); 1693 GFP_NOFS);
1698 } 1694 }
1699 1695
1700 int btrfs_buffer_defrag(struct extent_buffer *buf) 1696 int btrfs_buffer_defrag(struct extent_buffer *buf)
1701 { 1697 {
1702 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; 1698 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1703 struct inode *btree_inode = root->fs_info->btree_inode; 1699 struct inode *btree_inode = root->fs_info->btree_inode;
1704 return test_range_bit(&BTRFS_I(btree_inode)->io_tree, 1700 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
1705 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0); 1701 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
1706 } 1702 }
1707 1703
1708 int btrfs_buffer_defrag_done(struct extent_buffer *buf) 1704 int btrfs_buffer_defrag_done(struct extent_buffer *buf)
1709 { 1705 {
1710 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; 1706 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1711 struct inode *btree_inode = root->fs_info->btree_inode; 1707 struct inode *btree_inode = root->fs_info->btree_inode;
1712 return test_range_bit(&BTRFS_I(btree_inode)->io_tree, 1708 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
1713 buf->start, buf->start + buf->len - 1, 1709 buf->start, buf->start + buf->len - 1,
1714 EXTENT_DEFRAG_DONE, 0); 1710 EXTENT_DEFRAG_DONE, 0);
1715 } 1711 }
1716 1712
1717 int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf) 1713 int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
1718 { 1714 {
1719 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; 1715 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1720 struct inode *btree_inode = root->fs_info->btree_inode; 1716 struct inode *btree_inode = root->fs_info->btree_inode;
1721 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree, 1717 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
1722 buf->start, buf->start + buf->len - 1, 1718 buf->start, buf->start + buf->len - 1,
1723 EXTENT_DEFRAG_DONE, GFP_NOFS); 1719 EXTENT_DEFRAG_DONE, GFP_NOFS);
1724 } 1720 }
1725 1721
1726 int btrfs_clear_buffer_defrag(struct extent_buffer *buf) 1722 int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
1727 { 1723 {
1728 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; 1724 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1729 struct inode *btree_inode = root->fs_info->btree_inode; 1725 struct inode *btree_inode = root->fs_info->btree_inode;
1730 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree, 1726 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
1731 buf->start, buf->start + buf->len - 1, 1727 buf->start, buf->start + buf->len - 1,
1732 EXTENT_DEFRAG, GFP_NOFS); 1728 EXTENT_DEFRAG, GFP_NOFS);
1733 } 1729 }
1734 1730
1735 int btrfs_read_buffer(struct extent_buffer *buf) 1731 int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
1736 { 1732 {
1737 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; 1733 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1738 int ret; 1734 int ret;
1739 ret = btree_read_extent_buffer_pages(root, buf, 0); 1735 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
1740 if (ret == 0) { 1736 if (ret == 0) {
1741 buf->flags |= EXTENT_UPTODATE; 1737 buf->flags |= EXTENT_UPTODATE;
1742 } 1738 }
1743 return ret; 1739 return ret;
1744 } 1740 }
1745 1741
1746 static struct extent_io_ops btree_extent_io_ops = { 1742 static struct extent_io_ops btree_extent_io_ops = {
1747 .writepage_io_hook = btree_writepage_io_hook, 1743 .writepage_io_hook = btree_writepage_io_hook,
1748 .readpage_end_io_hook = btree_readpage_end_io_hook, 1744 .readpage_end_io_hook = btree_readpage_end_io_hook,
1749 .submit_bio_hook = btree_submit_bio_hook, 1745 .submit_bio_hook = btree_submit_bio_hook,
1750 /* note we're sharing with inode.c for the merge bio hook */ 1746 /* note we're sharing with inode.c for the merge bio hook */
1751 .merge_bio_hook = btrfs_merge_bio_hook, 1747 .merge_bio_hook = btrfs_merge_bio_hook,
1 /* 1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved. 2 * Copyright (C) 2007 Oracle. All rights reserved.
3 * 3 *
4 * This program is free software; you can redistribute it and/or 4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public 5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation. 6 * License v2 as published by the Free Software Foundation.
7 * 7 *
8 * This program is distributed in the hope that it will be useful, 8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details. 11 * General Public License for more details.
12 * 12 *
13 * You should have received a copy of the GNU General Public 13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the 14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA. 16 * Boston, MA 021110-1307, USA.
17 */ 17 */
18 18
19 #ifndef __DISKIO__ 19 #ifndef __DISKIO__
20 #define __DISKIO__ 20 #define __DISKIO__
21 21
22 #define BTRFS_SUPER_INFO_OFFSET (16 * 1024) 22 #define BTRFS_SUPER_INFO_OFFSET (16 * 1024)
23 #define BTRFS_SUPER_INFO_SIZE 4096 23 #define BTRFS_SUPER_INFO_SIZE 4096
24 struct btrfs_device; 24 struct btrfs_device;
25 struct btrfs_fs_devices; 25 struct btrfs_fs_devices;
26 26
27 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, 27 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
28 u32 blocksize); 28 u32 blocksize, u64 parent_transid);
29 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize); 29 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
30 u64 parent_transid);
30 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, 31 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
31 u64 bytenr, u32 blocksize); 32 u64 bytenr, u32 blocksize);
32 int clean_tree_block(struct btrfs_trans_handle *trans, 33 int clean_tree_block(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root, struct extent_buffer *buf); 34 struct btrfs_root *root, struct extent_buffer *buf);
34 struct btrfs_root *open_ctree(struct super_block *sb, 35 struct btrfs_root *open_ctree(struct super_block *sb,
35 struct btrfs_fs_devices *fs_devices); 36 struct btrfs_fs_devices *fs_devices);
36 int close_ctree(struct btrfs_root *root); 37 int close_ctree(struct btrfs_root *root);
37 int write_ctree_super(struct btrfs_trans_handle *trans, 38 int write_ctree_super(struct btrfs_trans_handle *trans,
38 struct btrfs_root *root); 39 struct btrfs_root *root);
39 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, 40 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
40 u64 bytenr, u32 blocksize); 41 u64 bytenr, u32 blocksize);
41 struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, 42 struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
42 u64 root_objectid); 43 u64 root_objectid);
43 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, 44 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
44 struct btrfs_key *location, 45 struct btrfs_key *location,
45 const char *name, int namelen); 46 const char *name, int namelen);
46 struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, 47 struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
47 struct btrfs_key *location); 48 struct btrfs_key *location);
48 struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, 49 struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
49 struct btrfs_key *location); 50 struct btrfs_key *location);
50 int btrfs_insert_dev_radix(struct btrfs_root *root, 51 int btrfs_insert_dev_radix(struct btrfs_root *root,
51 struct block_device *bdev, 52 struct block_device *bdev,
52 u64 device_id, 53 u64 device_id,
53 u64 block_start, 54 u64 block_start,
54 u64 num_blocks); 55 u64 num_blocks);
55 void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr); 56 void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr);
56 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root); 57 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root);
57 void btrfs_mark_buffer_dirty(struct extent_buffer *buf); 58 void btrfs_mark_buffer_dirty(struct extent_buffer *buf);
58 int btrfs_buffer_uptodate(struct extent_buffer *buf); 59 int btrfs_buffer_uptodate(struct extent_buffer *buf);
59 int btrfs_set_buffer_uptodate(struct extent_buffer *buf); 60 int btrfs_set_buffer_uptodate(struct extent_buffer *buf);
60 int wait_on_tree_block_writeback(struct btrfs_root *root, 61 int wait_on_tree_block_writeback(struct btrfs_root *root,
61 struct extent_buffer *buf); 62 struct extent_buffer *buf);
62 void btrfs_set_buffer_defrag(struct extent_buffer *buf); 63 void btrfs_set_buffer_defrag(struct extent_buffer *buf);
63 void btrfs_set_buffer_defrag_done(struct extent_buffer *buf); 64 void btrfs_set_buffer_defrag_done(struct extent_buffer *buf);
64 int btrfs_buffer_defrag(struct extent_buffer *buf); 65 int btrfs_buffer_defrag(struct extent_buffer *buf);
65 int btrfs_buffer_defrag_done(struct extent_buffer *buf); 66 int btrfs_buffer_defrag_done(struct extent_buffer *buf);
66 int btrfs_clear_buffer_defrag(struct extent_buffer *buf); 67 int btrfs_clear_buffer_defrag(struct extent_buffer *buf);
67 int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf); 68 int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf);
68 int btrfs_read_buffer(struct extent_buffer *buf); 69 int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid);
69 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len); 70 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len);
70 void btrfs_csum_final(u32 crc, char *result); 71 void btrfs_csum_final(u32 crc, char *result);
71 void btrfs_throttle(struct btrfs_root *root); 72 void btrfs_throttle(struct btrfs_root *root);
72 int btrfs_open_device(struct btrfs_device *dev); 73 int btrfs_open_device(struct btrfs_device *dev);
73 int btrfs_verify_block_csum(struct btrfs_root *root, 74 int btrfs_verify_block_csum(struct btrfs_root *root,
74 struct extent_buffer *buf); 75 struct extent_buffer *buf);
75 int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 76 int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
76 int metadata); 77 int metadata);
77 int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, 78 int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
78 int rw, struct bio *bio, int mirror_num, 79 int rw, struct bio *bio, int mirror_num,
79 extent_submit_bio_hook_t *submit_bio_hook); 80 extent_submit_bio_hook_t *submit_bio_hook);
80 #endif 81 #endif
81 82
fs/btrfs/extent-tree.c
1 /* 1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved. 2 * Copyright (C) 2007 Oracle. All rights reserved.
3 * 3 *
4 * This program is free software; you can redistribute it and/or 4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public 5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation. 6 * License v2 as published by the Free Software Foundation.
7 * 7 *
8 * This program is distributed in the hope that it will be useful, 8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details. 11 * General Public License for more details.
12 * 12 *
13 * You should have received a copy of the GNU General Public 13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the 14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA. 16 * Boston, MA 021110-1307, USA.
17 */ 17 */
18 #include <linux/sched.h> 18 #include <linux/sched.h>
19 #include <linux/pagemap.h> 19 #include <linux/pagemap.h>
20 #include <linux/writeback.h> 20 #include <linux/writeback.h>
21 #include "hash.h" 21 #include "hash.h"
22 #include "crc32c.h" 22 #include "crc32c.h"
23 #include "ctree.h" 23 #include "ctree.h"
24 #include "disk-io.h" 24 #include "disk-io.h"
25 #include "print-tree.h" 25 #include "print-tree.h"
26 #include "transaction.h" 26 #include "transaction.h"
27 #include "volumes.h" 27 #include "volumes.h"
28 28
29 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK 29 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
30 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE 30 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
31 #define BLOCK_GROUP_SYSTEM EXTENT_NEW 31 #define BLOCK_GROUP_SYSTEM EXTENT_NEW
32 32
33 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY 33 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
34 34
35 static int finish_current_insert(struct btrfs_trans_handle *trans, struct 35 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
36 btrfs_root *extent_root); 36 btrfs_root *extent_root);
37 static int del_pending_extents(struct btrfs_trans_handle *trans, struct 37 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
38 btrfs_root *extent_root); 38 btrfs_root *extent_root);
39 39
40 40
41 static int cache_block_group(struct btrfs_root *root, 41 static int cache_block_group(struct btrfs_root *root,
42 struct btrfs_block_group_cache *block_group) 42 struct btrfs_block_group_cache *block_group)
43 { 43 {
44 struct btrfs_path *path; 44 struct btrfs_path *path;
45 int ret; 45 int ret;
46 struct btrfs_key key; 46 struct btrfs_key key;
47 struct extent_buffer *leaf; 47 struct extent_buffer *leaf;
48 struct extent_io_tree *free_space_cache; 48 struct extent_io_tree *free_space_cache;
49 int slot; 49 int slot;
50 u64 last = 0; 50 u64 last = 0;
51 u64 hole_size; 51 u64 hole_size;
52 u64 first_free; 52 u64 first_free;
53 int found = 0; 53 int found = 0;
54 54
55 if (!block_group) 55 if (!block_group)
56 return 0; 56 return 0;
57 57
58 root = root->fs_info->extent_root; 58 root = root->fs_info->extent_root;
59 free_space_cache = &root->fs_info->free_space_cache; 59 free_space_cache = &root->fs_info->free_space_cache;
60 60
61 if (block_group->cached) 61 if (block_group->cached)
62 return 0; 62 return 0;
63 63
64 path = btrfs_alloc_path(); 64 path = btrfs_alloc_path();
65 if (!path) 65 if (!path)
66 return -ENOMEM; 66 return -ENOMEM;
67 67
68 path->reada = 2; 68 path->reada = 2;
69 first_free = block_group->key.objectid; 69 first_free = block_group->key.objectid;
70 key.objectid = block_group->key.objectid; 70 key.objectid = block_group->key.objectid;
71 key.offset = 0; 71 key.offset = 0;
72 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); 72 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
73 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 73 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
74 if (ret < 0) 74 if (ret < 0)
75 return ret; 75 return ret;
76 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY); 76 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
77 if (ret < 0) 77 if (ret < 0)
78 return ret; 78 return ret;
79 if (ret == 0) { 79 if (ret == 0) {
80 leaf = path->nodes[0]; 80 leaf = path->nodes[0];
81 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 81 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
82 if (key.objectid + key.offset > first_free) 82 if (key.objectid + key.offset > first_free)
83 first_free = key.objectid + key.offset; 83 first_free = key.objectid + key.offset;
84 } 84 }
85 while(1) { 85 while(1) {
86 leaf = path->nodes[0]; 86 leaf = path->nodes[0];
87 slot = path->slots[0]; 87 slot = path->slots[0];
88 if (slot >= btrfs_header_nritems(leaf)) { 88 if (slot >= btrfs_header_nritems(leaf)) {
89 ret = btrfs_next_leaf(root, path); 89 ret = btrfs_next_leaf(root, path);
90 if (ret < 0) 90 if (ret < 0)
91 goto err; 91 goto err;
92 if (ret == 0) { 92 if (ret == 0) {
93 continue; 93 continue;
94 } else { 94 } else {
95 break; 95 break;
96 } 96 }
97 } 97 }
98 btrfs_item_key_to_cpu(leaf, &key, slot); 98 btrfs_item_key_to_cpu(leaf, &key, slot);
99 if (key.objectid < block_group->key.objectid) { 99 if (key.objectid < block_group->key.objectid) {
100 goto next; 100 goto next;
101 } 101 }
102 if (key.objectid >= block_group->key.objectid + 102 if (key.objectid >= block_group->key.objectid +
103 block_group->key.offset) { 103 block_group->key.offset) {
104 break; 104 break;
105 } 105 }
106 106
107 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { 107 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
108 if (!found) { 108 if (!found) {
109 last = first_free; 109 last = first_free;
110 found = 1; 110 found = 1;
111 } 111 }
112 if (key.objectid > last) { 112 if (key.objectid > last) {
113 hole_size = key.objectid - last; 113 hole_size = key.objectid - last;
114 set_extent_dirty(free_space_cache, last, 114 set_extent_dirty(free_space_cache, last,
115 last + hole_size - 1, 115 last + hole_size - 1,
116 GFP_NOFS); 116 GFP_NOFS);
117 } 117 }
118 last = key.objectid + key.offset; 118 last = key.objectid + key.offset;
119 } 119 }
120 next: 120 next:
121 path->slots[0]++; 121 path->slots[0]++;
122 } 122 }
123 123
124 if (!found) 124 if (!found)
125 last = first_free; 125 last = first_free;
126 if (block_group->key.objectid + 126 if (block_group->key.objectid +
127 block_group->key.offset > last) { 127 block_group->key.offset > last) {
128 hole_size = block_group->key.objectid + 128 hole_size = block_group->key.objectid +
129 block_group->key.offset - last; 129 block_group->key.offset - last;
130 set_extent_dirty(free_space_cache, last, 130 set_extent_dirty(free_space_cache, last,
131 last + hole_size - 1, GFP_NOFS); 131 last + hole_size - 1, GFP_NOFS);
132 } 132 }
133 block_group->cached = 1; 133 block_group->cached = 1;
134 err: 134 err:
135 btrfs_free_path(path); 135 btrfs_free_path(path);
136 return 0; 136 return 0;
137 } 137 }
138 138
139 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct 139 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
140 btrfs_fs_info *info, 140 btrfs_fs_info *info,
141 u64 bytenr) 141 u64 bytenr)
142 { 142 {
143 struct extent_io_tree *block_group_cache; 143 struct extent_io_tree *block_group_cache;
144 struct btrfs_block_group_cache *block_group = NULL; 144 struct btrfs_block_group_cache *block_group = NULL;
145 u64 ptr; 145 u64 ptr;
146 u64 start; 146 u64 start;
147 u64 end; 147 u64 end;
148 int ret; 148 int ret;
149 149
150 bytenr = max_t(u64, bytenr, 150 bytenr = max_t(u64, bytenr,
151 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE); 151 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
152 block_group_cache = &info->block_group_cache; 152 block_group_cache = &info->block_group_cache;
153 ret = find_first_extent_bit(block_group_cache, 153 ret = find_first_extent_bit(block_group_cache,
154 bytenr, &start, &end, 154 bytenr, &start, &end,
155 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA | 155 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
156 BLOCK_GROUP_SYSTEM); 156 BLOCK_GROUP_SYSTEM);
157 if (ret) { 157 if (ret) {
158 return NULL; 158 return NULL;
159 } 159 }
160 ret = get_state_private(block_group_cache, start, &ptr); 160 ret = get_state_private(block_group_cache, start, &ptr);
161 if (ret) 161 if (ret)
162 return NULL; 162 return NULL;
163 163
164 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr; 164 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
165 if (block_group->key.objectid <= bytenr && bytenr < 165 if (block_group->key.objectid <= bytenr && bytenr <
166 block_group->key.objectid + block_group->key.offset) 166 block_group->key.objectid + block_group->key.offset)
167 return block_group; 167 return block_group;
168 return NULL; 168 return NULL;
169 } 169 }
170 170
171 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) 171 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
172 { 172 {
173 return (cache->flags & bits) == bits; 173 return (cache->flags & bits) == bits;
174 } 174 }
175 175
176 static int noinline find_search_start(struct btrfs_root *root, 176 static int noinline find_search_start(struct btrfs_root *root,
177 struct btrfs_block_group_cache **cache_ret, 177 struct btrfs_block_group_cache **cache_ret,
178 u64 *start_ret, int num, int data) 178 u64 *start_ret, int num, int data)
179 { 179 {
180 int ret; 180 int ret;
181 struct btrfs_block_group_cache *cache = *cache_ret; 181 struct btrfs_block_group_cache *cache = *cache_ret;
182 struct extent_io_tree *free_space_cache; 182 struct extent_io_tree *free_space_cache;
183 struct extent_state *state; 183 struct extent_state *state;
184 u64 last; 184 u64 last;
185 u64 start = 0; 185 u64 start = 0;
186 u64 cache_miss = 0; 186 u64 cache_miss = 0;
187 u64 total_fs_bytes; 187 u64 total_fs_bytes;
188 u64 search_start = *start_ret; 188 u64 search_start = *start_ret;
189 int wrapped = 0; 189 int wrapped = 0;
190 190
191 if (!cache) 191 if (!cache)
192 goto out; 192 goto out;
193 193
194 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); 194 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
195 free_space_cache = &root->fs_info->free_space_cache; 195 free_space_cache = &root->fs_info->free_space_cache;
196 196
197 again: 197 again:
198 ret = cache_block_group(root, cache); 198 ret = cache_block_group(root, cache);
199 if (ret) 199 if (ret)
200 goto out; 200 goto out;
201 201
202 last = max(search_start, cache->key.objectid); 202 last = max(search_start, cache->key.objectid);
203 if (!block_group_bits(cache, data) || cache->ro) { 203 if (!block_group_bits(cache, data) || cache->ro) {
204 goto new_group; 204 goto new_group;
205 } 205 }
206 206
207 spin_lock_irq(&free_space_cache->lock); 207 spin_lock_irq(&free_space_cache->lock);
208 state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY); 208 state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
209 while(1) { 209 while(1) {
210 if (!state) { 210 if (!state) {
211 if (!cache_miss) 211 if (!cache_miss)
212 cache_miss = last; 212 cache_miss = last;
213 spin_unlock_irq(&free_space_cache->lock); 213 spin_unlock_irq(&free_space_cache->lock);
214 goto new_group; 214 goto new_group;
215 } 215 }
216 216
217 start = max(last, state->start); 217 start = max(last, state->start);
218 last = state->end + 1; 218 last = state->end + 1;
219 if (last - start < num) { 219 if (last - start < num) {
220 if (last == cache->key.objectid + cache->key.offset) 220 if (last == cache->key.objectid + cache->key.offset)
221 cache_miss = start; 221 cache_miss = start;
222 do { 222 do {
223 state = extent_state_next(state); 223 state = extent_state_next(state);
224 } while(state && !(state->state & EXTENT_DIRTY)); 224 } while(state && !(state->state & EXTENT_DIRTY));
225 continue; 225 continue;
226 } 226 }
227 spin_unlock_irq(&free_space_cache->lock); 227 spin_unlock_irq(&free_space_cache->lock);
228 if (cache->ro) 228 if (cache->ro)
229 goto new_group; 229 goto new_group;
230 if (start + num > cache->key.objectid + cache->key.offset) 230 if (start + num > cache->key.objectid + cache->key.offset)
231 goto new_group; 231 goto new_group;
232 if (start + num > total_fs_bytes) 232 if (start + num > total_fs_bytes)
233 goto new_group; 233 goto new_group;
234 if (!block_group_bits(cache, data)) { 234 if (!block_group_bits(cache, data)) {
235 printk("block group bits don't match %Lu %d\n", cache->flags, data); 235 printk("block group bits don't match %Lu %d\n", cache->flags, data);
236 } 236 }
237 *start_ret = start; 237 *start_ret = start;
238 return 0; 238 return 0;
239 } 239 }
240 out: 240 out:
241 cache = btrfs_lookup_block_group(root->fs_info, search_start); 241 cache = btrfs_lookup_block_group(root->fs_info, search_start);
242 if (!cache) { 242 if (!cache) {
243 printk("Unable to find block group for %Lu\n", search_start); 243 printk("Unable to find block group for %Lu\n", search_start);
244 WARN_ON(1); 244 WARN_ON(1);
245 } 245 }
246 return -ENOSPC; 246 return -ENOSPC;
247 247
248 new_group: 248 new_group:
249 last = cache->key.objectid + cache->key.offset; 249 last = cache->key.objectid + cache->key.offset;
250 wrapped: 250 wrapped:
251 cache = btrfs_lookup_block_group(root->fs_info, last); 251 cache = btrfs_lookup_block_group(root->fs_info, last);
252 if (!cache || cache->key.objectid >= total_fs_bytes) { 252 if (!cache || cache->key.objectid >= total_fs_bytes) {
253 no_cache: 253 no_cache:
254 if (!wrapped) { 254 if (!wrapped) {
255 wrapped = 1; 255 wrapped = 1;
256 last = search_start; 256 last = search_start;
257 goto wrapped; 257 goto wrapped;
258 } 258 }
259 goto out; 259 goto out;
260 } 260 }
261 if (cache_miss && !cache->cached) { 261 if (cache_miss && !cache->cached) {
262 cache_block_group(root, cache); 262 cache_block_group(root, cache);
263 last = cache_miss; 263 last = cache_miss;
264 cache = btrfs_lookup_block_group(root->fs_info, last); 264 cache = btrfs_lookup_block_group(root->fs_info, last);
265 } 265 }
266 cache = btrfs_find_block_group(root, cache, last, data, 0); 266 cache = btrfs_find_block_group(root, cache, last, data, 0);
267 if (!cache) 267 if (!cache)
268 goto no_cache; 268 goto no_cache;
269 *cache_ret = cache; 269 *cache_ret = cache;
270 cache_miss = 0; 270 cache_miss = 0;
271 goto again; 271 goto again;
272 } 272 }
273 273
274 static u64 div_factor(u64 num, int factor) 274 static u64 div_factor(u64 num, int factor)
275 { 275 {
276 if (factor == 10) 276 if (factor == 10)
277 return num; 277 return num;
278 num *= factor; 278 num *= factor;
279 do_div(num, 10); 279 do_div(num, 10);
280 return num; 280 return num;
281 } 281 }
282 282
283 static int block_group_state_bits(u64 flags) 283 static int block_group_state_bits(u64 flags)
284 { 284 {
285 int bits = 0; 285 int bits = 0;
286 if (flags & BTRFS_BLOCK_GROUP_DATA) 286 if (flags & BTRFS_BLOCK_GROUP_DATA)
287 bits |= BLOCK_GROUP_DATA; 287 bits |= BLOCK_GROUP_DATA;
288 if (flags & BTRFS_BLOCK_GROUP_METADATA) 288 if (flags & BTRFS_BLOCK_GROUP_METADATA)
289 bits |= BLOCK_GROUP_METADATA; 289 bits |= BLOCK_GROUP_METADATA;
290 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 290 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
291 bits |= BLOCK_GROUP_SYSTEM; 291 bits |= BLOCK_GROUP_SYSTEM;
292 return bits; 292 return bits;
293 } 293 }
294 294
295 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, 295 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
296 struct btrfs_block_group_cache 296 struct btrfs_block_group_cache
297 *hint, u64 search_start, 297 *hint, u64 search_start,
298 int data, int owner) 298 int data, int owner)
299 { 299 {
300 struct btrfs_block_group_cache *cache; 300 struct btrfs_block_group_cache *cache;
301 struct extent_io_tree *block_group_cache; 301 struct extent_io_tree *block_group_cache;
302 struct btrfs_block_group_cache *found_group = NULL; 302 struct btrfs_block_group_cache *found_group = NULL;
303 struct btrfs_fs_info *info = root->fs_info; 303 struct btrfs_fs_info *info = root->fs_info;
304 u64 used; 304 u64 used;
305 u64 last = 0; 305 u64 last = 0;
306 u64 hint_last; 306 u64 hint_last;
307 u64 start; 307 u64 start;
308 u64 end; 308 u64 end;
309 u64 free_check; 309 u64 free_check;
310 u64 ptr; 310 u64 ptr;
311 u64 total_fs_bytes; 311 u64 total_fs_bytes;
312 int bit; 312 int bit;
313 int ret; 313 int ret;
314 int full_search = 0; 314 int full_search = 0;
315 int factor = 10; 315 int factor = 10;
316 316
317 block_group_cache = &info->block_group_cache; 317 block_group_cache = &info->block_group_cache;
318 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); 318 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
319 319
320 if (data & BTRFS_BLOCK_GROUP_METADATA) 320 if (data & BTRFS_BLOCK_GROUP_METADATA)
321 factor = 9; 321 factor = 9;
322 322
323 bit = block_group_state_bits(data); 323 bit = block_group_state_bits(data);
324 324
325 if (search_start && search_start < total_fs_bytes) { 325 if (search_start && search_start < total_fs_bytes) {
326 struct btrfs_block_group_cache *shint; 326 struct btrfs_block_group_cache *shint;
327 shint = btrfs_lookup_block_group(info, search_start); 327 shint = btrfs_lookup_block_group(info, search_start);
328 if (shint && block_group_bits(shint, data) && !shint->ro) { 328 if (shint && block_group_bits(shint, data) && !shint->ro) {
329 used = btrfs_block_group_used(&shint->item); 329 used = btrfs_block_group_used(&shint->item);
330 if (used + shint->pinned < 330 if (used + shint->pinned <
331 div_factor(shint->key.offset, factor)) { 331 div_factor(shint->key.offset, factor)) {
332 return shint; 332 return shint;
333 } 333 }
334 } 334 }
335 } 335 }
336 if (hint && !hint->ro && block_group_bits(hint, data) && 336 if (hint && !hint->ro && block_group_bits(hint, data) &&
337 hint->key.objectid < total_fs_bytes) { 337 hint->key.objectid < total_fs_bytes) {
338 used = btrfs_block_group_used(&hint->item); 338 used = btrfs_block_group_used(&hint->item);
339 if (used + hint->pinned < 339 if (used + hint->pinned <
340 div_factor(hint->key.offset, factor)) { 340 div_factor(hint->key.offset, factor)) {
341 return hint; 341 return hint;
342 } 342 }
343 last = hint->key.objectid + hint->key.offset; 343 last = hint->key.objectid + hint->key.offset;
344 hint_last = last; 344 hint_last = last;
345 } else { 345 } else {
346 if (hint) 346 if (hint)
347 hint_last = max(hint->key.objectid, search_start); 347 hint_last = max(hint->key.objectid, search_start);
348 else 348 else
349 hint_last = search_start; 349 hint_last = search_start;
350 350
351 if (hint_last >= total_fs_bytes) 351 if (hint_last >= total_fs_bytes)
352 hint_last = search_start; 352 hint_last = search_start;
353 last = hint_last; 353 last = hint_last;
354 } 354 }
355 again: 355 again:
356 while(1) { 356 while(1) {
357 ret = find_first_extent_bit(block_group_cache, last, 357 ret = find_first_extent_bit(block_group_cache, last,
358 &start, &end, bit); 358 &start, &end, bit);
359 if (ret) 359 if (ret)
360 break; 360 break;
361 361
362 ret = get_state_private(block_group_cache, start, &ptr); 362 ret = get_state_private(block_group_cache, start, &ptr);
363 if (ret) 363 if (ret)
364 break; 364 break;
365 365
366 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr; 366 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
367 last = cache->key.objectid + cache->key.offset; 367 last = cache->key.objectid + cache->key.offset;
368 used = btrfs_block_group_used(&cache->item); 368 used = btrfs_block_group_used(&cache->item);
369 369
370 if (cache->key.objectid > total_fs_bytes) 370 if (cache->key.objectid > total_fs_bytes)
371 break; 371 break;
372 372
373 if (!cache->ro && block_group_bits(cache, data)) { 373 if (!cache->ro && block_group_bits(cache, data)) {
374 if (full_search) 374 if (full_search)
375 free_check = cache->key.offset; 375 free_check = cache->key.offset;
376 else 376 else
377 free_check = div_factor(cache->key.offset, 377 free_check = div_factor(cache->key.offset,
378 factor); 378 factor);
379 379
380 if (used + cache->pinned < free_check) { 380 if (used + cache->pinned < free_check) {
381 found_group = cache; 381 found_group = cache;
382 goto found; 382 goto found;
383 } 383 }
384 } 384 }
385 cond_resched(); 385 cond_resched();
386 } 386 }
387 if (!full_search) { 387 if (!full_search) {
388 last = search_start; 388 last = search_start;
389 full_search = 1; 389 full_search = 1;
390 goto again; 390 goto again;
391 } 391 }
392 found: 392 found:
393 return found_group; 393 return found_group;
394 } 394 }
395 395
396 static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation, 396 static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
397 u64 owner, u64 owner_offset) 397 u64 owner, u64 owner_offset)
398 { 398 {
399 u32 high_crc = ~(u32)0; 399 u32 high_crc = ~(u32)0;
400 u32 low_crc = ~(u32)0; 400 u32 low_crc = ~(u32)0;
401 __le64 lenum; 401 __le64 lenum;
402 lenum = cpu_to_le64(root_objectid); 402 lenum = cpu_to_le64(root_objectid);
403 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); 403 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
404 lenum = cpu_to_le64(ref_generation); 404 lenum = cpu_to_le64(ref_generation);
405 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); 405 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
406 if (owner >= BTRFS_FIRST_FREE_OBJECTID) { 406 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
407 lenum = cpu_to_le64(owner); 407 lenum = cpu_to_le64(owner);
408 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); 408 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
409 lenum = cpu_to_le64(owner_offset); 409 lenum = cpu_to_le64(owner_offset);
410 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); 410 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
411 } 411 }
412 return ((u64)high_crc << 32) | (u64)low_crc; 412 return ((u64)high_crc << 32) | (u64)low_crc;
413 } 413 }
414 414
415 static int match_extent_ref(struct extent_buffer *leaf, 415 static int match_extent_ref(struct extent_buffer *leaf,
416 struct btrfs_extent_ref *disk_ref, 416 struct btrfs_extent_ref *disk_ref,
417 struct btrfs_extent_ref *cpu_ref) 417 struct btrfs_extent_ref *cpu_ref)
418 { 418 {
419 int ret; 419 int ret;
420 int len; 420 int len;
421 421
422 if (cpu_ref->objectid) 422 if (cpu_ref->objectid)
423 len = sizeof(*cpu_ref); 423 len = sizeof(*cpu_ref);
424 else 424 else
425 len = 2 * sizeof(u64); 425 len = 2 * sizeof(u64);
426 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref, 426 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
427 len); 427 len);
428 return ret == 0; 428 return ret == 0;
429 } 429 }
430 430
431 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans, 431 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
432 struct btrfs_root *root, 432 struct btrfs_root *root,
433 struct btrfs_path *path, u64 bytenr, 433 struct btrfs_path *path, u64 bytenr,
434 u64 root_objectid, 434 u64 root_objectid,
435 u64 ref_generation, u64 owner, 435 u64 ref_generation, u64 owner,
436 u64 owner_offset, int del) 436 u64 owner_offset, int del)
437 { 437 {
438 u64 hash; 438 u64 hash;
439 struct btrfs_key key; 439 struct btrfs_key key;
440 struct btrfs_key found_key; 440 struct btrfs_key found_key;
441 struct btrfs_extent_ref ref; 441 struct btrfs_extent_ref ref;
442 struct extent_buffer *leaf; 442 struct extent_buffer *leaf;
443 struct btrfs_extent_ref *disk_ref; 443 struct btrfs_extent_ref *disk_ref;
444 int ret; 444 int ret;
445 int ret2; 445 int ret2;
446 446
447 btrfs_set_stack_ref_root(&ref, root_objectid); 447 btrfs_set_stack_ref_root(&ref, root_objectid);
448 btrfs_set_stack_ref_generation(&ref, ref_generation); 448 btrfs_set_stack_ref_generation(&ref, ref_generation);
449 btrfs_set_stack_ref_objectid(&ref, owner); 449 btrfs_set_stack_ref_objectid(&ref, owner);
450 btrfs_set_stack_ref_offset(&ref, owner_offset); 450 btrfs_set_stack_ref_offset(&ref, owner_offset);
451 451
452 hash = hash_extent_ref(root_objectid, ref_generation, owner, 452 hash = hash_extent_ref(root_objectid, ref_generation, owner,
453 owner_offset); 453 owner_offset);
454 key.offset = hash; 454 key.offset = hash;
455 key.objectid = bytenr; 455 key.objectid = bytenr;
456 key.type = BTRFS_EXTENT_REF_KEY; 456 key.type = BTRFS_EXTENT_REF_KEY;
457 457
458 while (1) { 458 while (1) {
459 ret = btrfs_search_slot(trans, root, &key, path, 459 ret = btrfs_search_slot(trans, root, &key, path,
460 del ? -1 : 0, del); 460 del ? -1 : 0, del);
461 if (ret < 0) 461 if (ret < 0)
462 goto out; 462 goto out;
463 leaf = path->nodes[0]; 463 leaf = path->nodes[0];
464 if (ret != 0) { 464 if (ret != 0) {
465 u32 nritems = btrfs_header_nritems(leaf); 465 u32 nritems = btrfs_header_nritems(leaf);
466 if (path->slots[0] >= nritems) { 466 if (path->slots[0] >= nritems) {
467 ret2 = btrfs_next_leaf(root, path); 467 ret2 = btrfs_next_leaf(root, path);
468 if (ret2) 468 if (ret2)
469 goto out; 469 goto out;
470 leaf = path->nodes[0]; 470 leaf = path->nodes[0];
471 } 471 }
472 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 472 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
473 if (found_key.objectid != bytenr || 473 if (found_key.objectid != bytenr ||
474 found_key.type != BTRFS_EXTENT_REF_KEY) 474 found_key.type != BTRFS_EXTENT_REF_KEY)
475 goto out; 475 goto out;
476 key.offset = found_key.offset; 476 key.offset = found_key.offset;
477 if (del) { 477 if (del) {
478 btrfs_release_path(root, path); 478 btrfs_release_path(root, path);
479 continue; 479 continue;
480 } 480 }
481 } 481 }
482 disk_ref = btrfs_item_ptr(path->nodes[0], 482 disk_ref = btrfs_item_ptr(path->nodes[0],
483 path->slots[0], 483 path->slots[0],
484 struct btrfs_extent_ref); 484 struct btrfs_extent_ref);
485 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) { 485 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
486 ret = 0; 486 ret = 0;
487 goto out; 487 goto out;
488 } 488 }
489 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 489 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
490 key.offset = found_key.offset + 1; 490 key.offset = found_key.offset + 1;
491 btrfs_release_path(root, path); 491 btrfs_release_path(root, path);
492 } 492 }
493 out: 493 out:
494 return ret; 494 return ret;
495 } 495 }
496 496
497 /* 497 /*
498 * Back reference rules. Back refs have three main goals: 498 * Back reference rules. Back refs have three main goals:
499 * 499 *
500 * 1) differentiate between all holders of references to an extent so that 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 501 * when a reference is dropped we can make sure it was a valid reference
502 * before freeing the extent. 502 * before freeing the extent.
503 * 503 *
504 * 2) Provide enough information to quickly find the holders of an extent 504 * 2) Provide enough information to quickly find the holders of an extent
505 * if we notice a given block is corrupted or bad. 505 * if we notice a given block is corrupted or bad.
506 * 506 *
507 * 3) Make it easy to migrate blocks for FS shrinking or storage pool 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 508 * maintenance. This is actually the same as #2, but with a slightly
509 * different use case. 509 * different use case.
510 * 510 *
511 * File extents can be referenced by: 511 * File extents can be referenced by:
512 * 512 *
513 * - multiple snapshots, subvolumes, or different generations in one subvol 513 * - multiple snapshots, subvolumes, or different generations in one subvol
514 * - different files inside a single subvolume (in theory, not implemented yet) 514 * - different files inside a single subvolume (in theory, not implemented yet)
515 * - different offsets inside a file (bookend extents in file.c) 515 * - different offsets inside a file (bookend extents in file.c)
516 * 516 *
517 * The extent ref structure has fields for: 517 * The extent ref structure has fields for:
518 * 518 *
519 * - Objectid of the subvolume root 519 * - Objectid of the subvolume root
520 * - Generation number of the tree holding the reference 520 * - Generation number of the tree holding the reference
521 * - objectid of the file holding the reference 521 * - objectid of the file holding the reference
522 * - offset in the file corresponding to the key holding the reference 522 * - offset in the file corresponding to the key holding the reference
523 * 523 *
524 * When a file extent is allocated the fields are filled in: 524 * When a file extent is allocated the fields are filled in:
525 * (root_key.objectid, trans->transid, inode objectid, offset in file) 525 * (root_key.objectid, trans->transid, inode objectid, offset in file)
526 * 526 *
527 * When a leaf is cow'd new references are added for every file extent found 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 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. 529 * will be different when the block is cow'd.
530 * 530 *
531 * (root_key.objectid, trans->transid, inode objectid, offset in file) 531 * (root_key.objectid, trans->transid, inode objectid, offset in file)
532 * 532 *
533 * When a file extent is removed either during snapshot deletion or file 533 * When a file extent is removed either during snapshot deletion or file
534 * truncation, the corresponding back reference is found 534 * truncation, the corresponding back reference is found
535 * by searching for: 535 * by searching for:
536 * 536 *
537 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf), 537 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
538 * inode objectid, offset in file) 538 * inode objectid, offset in file)
539 * 539 *
540 * Btree extents can be referenced by: 540 * Btree extents can be referenced by:
541 * 541 *
542 * - Different subvolumes 542 * - Different subvolumes
543 * - Different generations of the same subvolume 543 * - Different generations of the same subvolume
544 * 544 *
545 * Storing sufficient information for a full reverse mapping of a btree 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, 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 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 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 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 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 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. 552 * level higher than the level recorded in the backref.
553 * 553 *
554 * Some btrees do not do reference counting on their extents. These 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 555 * include the extent tree and the tree of tree roots. Backrefs for these
556 * trees always have a generation of zero. 556 * trees always have a generation of zero.
557 * 557 *
558 * When a tree block is created, back references are inserted: 558 * When a tree block is created, back references are inserted:
559 * 559 *
560 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid) 560 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
561 * 561 *
562 * When a tree block is cow'd in a reference counted root, 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. 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): 564 * These are of the form (trans->transid will have increased since creation):
565 * 565 *
566 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid) 566 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
567 * 567 *
568 * Because the lowest_key_objectid and the level are just hints 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: 569 * they are not used when backrefs are deleted. When a backref is deleted:
570 * 570 *
571 * if backref was for a tree root: 571 * if backref was for a tree root:
572 * root_objectid = root->root_key.objectid 572 * root_objectid = root->root_key.objectid
573 * else 573 * else
574 * root_objectid = btrfs_header_owner(parent) 574 * root_objectid = btrfs_header_owner(parent)
575 * 575 *
576 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0) 576 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
577 * 577 *
578 * Back Reference Key hashing: 578 * Back Reference Key hashing:
579 * 579 *
580 * Back references have four fields, each 64 bits long. Unfortunately, 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. 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 582 * The key objectid corresponds to the first byte in the extent, and the
583 * key type is set to BTRFS_EXTENT_REF_KEY 583 * key type is set to BTRFS_EXTENT_REF_KEY
584 */ 584 */
585 int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans, 585 int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
586 struct btrfs_root *root, 586 struct btrfs_root *root,
587 struct btrfs_path *path, u64 bytenr, 587 struct btrfs_path *path, u64 bytenr,
588 u64 root_objectid, u64 ref_generation, 588 u64 root_objectid, u64 ref_generation,
589 u64 owner, u64 owner_offset) 589 u64 owner, u64 owner_offset)
590 { 590 {
591 u64 hash; 591 u64 hash;
592 struct btrfs_key key; 592 struct btrfs_key key;
593 struct btrfs_extent_ref ref; 593 struct btrfs_extent_ref ref;
594 struct btrfs_extent_ref *disk_ref; 594 struct btrfs_extent_ref *disk_ref;
595 int ret; 595 int ret;
596 596
597 btrfs_set_stack_ref_root(&ref, root_objectid); 597 btrfs_set_stack_ref_root(&ref, root_objectid);
598 btrfs_set_stack_ref_generation(&ref, ref_generation); 598 btrfs_set_stack_ref_generation(&ref, ref_generation);
599 btrfs_set_stack_ref_objectid(&ref, owner); 599 btrfs_set_stack_ref_objectid(&ref, owner);
600 btrfs_set_stack_ref_offset(&ref, owner_offset); 600 btrfs_set_stack_ref_offset(&ref, owner_offset);
601 601
602 hash = hash_extent_ref(root_objectid, ref_generation, owner, 602 hash = hash_extent_ref(root_objectid, ref_generation, owner,
603 owner_offset); 603 owner_offset);
604 key.offset = hash; 604 key.offset = hash;
605 key.objectid = bytenr; 605 key.objectid = bytenr;
606 key.type = BTRFS_EXTENT_REF_KEY; 606 key.type = BTRFS_EXTENT_REF_KEY;
607 607
608 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref)); 608 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
609 while (ret == -EEXIST) { 609 while (ret == -EEXIST) {
610 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 610 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
611 struct btrfs_extent_ref); 611 struct btrfs_extent_ref);
612 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) 612 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
613 goto out; 613 goto out;
614 key.offset++; 614 key.offset++;
615 btrfs_release_path(root, path); 615 btrfs_release_path(root, path);
616 ret = btrfs_insert_empty_item(trans, root, path, &key, 616 ret = btrfs_insert_empty_item(trans, root, path, &key,
617 sizeof(ref)); 617 sizeof(ref));
618 } 618 }
619 if (ret) 619 if (ret)
620 goto out; 620 goto out;
621 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 621 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
622 struct btrfs_extent_ref); 622 struct btrfs_extent_ref);
623 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref, 623 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
624 sizeof(ref)); 624 sizeof(ref));
625 btrfs_mark_buffer_dirty(path->nodes[0]); 625 btrfs_mark_buffer_dirty(path->nodes[0]);
626 out: 626 out:
627 btrfs_release_path(root, path); 627 btrfs_release_path(root, path);
628 return ret; 628 return ret;
629 } 629 }
630 630
631 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, 631 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
632 struct btrfs_root *root, 632 struct btrfs_root *root,
633 u64 bytenr, u64 num_bytes, 633 u64 bytenr, u64 num_bytes,
634 u64 root_objectid, u64 ref_generation, 634 u64 root_objectid, u64 ref_generation,
635 u64 owner, u64 owner_offset) 635 u64 owner, u64 owner_offset)
636 { 636 {
637 struct btrfs_path *path; 637 struct btrfs_path *path;
638 int ret; 638 int ret;
639 struct btrfs_key key; 639 struct btrfs_key key;
640 struct extent_buffer *l; 640 struct extent_buffer *l;
641 struct btrfs_extent_item *item; 641 struct btrfs_extent_item *item;
642 u32 refs; 642 u32 refs;
643 643
644 WARN_ON(num_bytes < root->sectorsize); 644 WARN_ON(num_bytes < root->sectorsize);
645 path = btrfs_alloc_path(); 645 path = btrfs_alloc_path();
646 if (!path) 646 if (!path)
647 return -ENOMEM; 647 return -ENOMEM;
648 648
649 path->reada = 1; 649 path->reada = 1;
650 key.objectid = bytenr; 650 key.objectid = bytenr;
651 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); 651 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
652 key.offset = num_bytes; 652 key.offset = num_bytes;
653 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, 653 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
654 0, 1); 654 0, 1);
655 if (ret < 0) 655 if (ret < 0)
656 return ret; 656 return ret;
657 if (ret != 0) { 657 if (ret != 0) {
658 BUG(); 658 BUG();
659 } 659 }
660 BUG_ON(ret != 0); 660 BUG_ON(ret != 0);
661 l = path->nodes[0]; 661 l = path->nodes[0];
662 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); 662 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
663 refs = btrfs_extent_refs(l, item); 663 refs = btrfs_extent_refs(l, item);
664 btrfs_set_extent_refs(l, item, refs + 1); 664 btrfs_set_extent_refs(l, item, refs + 1);
665 btrfs_mark_buffer_dirty(path->nodes[0]); 665 btrfs_mark_buffer_dirty(path->nodes[0]);
666 666
667 btrfs_release_path(root->fs_info->extent_root, path); 667 btrfs_release_path(root->fs_info->extent_root, path);
668 668
669 path->reada = 1; 669 path->reada = 1;
670 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root, 670 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
671 path, bytenr, root_objectid, 671 path, bytenr, root_objectid,
672 ref_generation, owner, owner_offset); 672 ref_generation, owner, owner_offset);
673 BUG_ON(ret); 673 BUG_ON(ret);
674 finish_current_insert(trans, root->fs_info->extent_root); 674 finish_current_insert(trans, root->fs_info->extent_root);
675 del_pending_extents(trans, root->fs_info->extent_root); 675 del_pending_extents(trans, root->fs_info->extent_root);
676 676
677 btrfs_free_path(path); 677 btrfs_free_path(path);
678 return 0; 678 return 0;
679 } 679 }
680 680
681 int btrfs_extent_post_op(struct btrfs_trans_handle *trans, 681 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
682 struct btrfs_root *root) 682 struct btrfs_root *root)
683 { 683 {
684 finish_current_insert(trans, root->fs_info->extent_root); 684 finish_current_insert(trans, root->fs_info->extent_root);
685 del_pending_extents(trans, root->fs_info->extent_root); 685 del_pending_extents(trans, root->fs_info->extent_root);
686 return 0; 686 return 0;
687 } 687 }
688 688
689 static int lookup_extent_ref(struct btrfs_trans_handle *trans, 689 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
690 struct btrfs_root *root, u64 bytenr, 690 struct btrfs_root *root, u64 bytenr,
691 u64 num_bytes, u32 *refs) 691 u64 num_bytes, u32 *refs)
692 { 692 {
693 struct btrfs_path *path; 693 struct btrfs_path *path;
694 int ret; 694 int ret;
695 struct btrfs_key key; 695 struct btrfs_key key;
696 struct extent_buffer *l; 696 struct extent_buffer *l;
697 struct btrfs_extent_item *item; 697 struct btrfs_extent_item *item;
698 698
699 WARN_ON(num_bytes < root->sectorsize); 699 WARN_ON(num_bytes < root->sectorsize);
700 path = btrfs_alloc_path(); 700 path = btrfs_alloc_path();
701 path->reada = 1; 701 path->reada = 1;
702 key.objectid = bytenr; 702 key.objectid = bytenr;
703 key.offset = num_bytes; 703 key.offset = num_bytes;
704 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); 704 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
705 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, 705 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
706 0, 0); 706 0, 0);
707 if (ret < 0) 707 if (ret < 0)
708 goto out; 708 goto out;
709 if (ret != 0) { 709 if (ret != 0) {
710 btrfs_print_leaf(root, path->nodes[0]); 710 btrfs_print_leaf(root, path->nodes[0]);
711 printk("failed to find block number %Lu\n", bytenr); 711 printk("failed to find block number %Lu\n", bytenr);
712 BUG(); 712 BUG();
713 } 713 }
714 l = path->nodes[0]; 714 l = path->nodes[0];
715 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); 715 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
716 *refs = btrfs_extent_refs(l, item); 716 *refs = btrfs_extent_refs(l, item);
717 out: 717 out:
718 btrfs_free_path(path); 718 btrfs_free_path(path);
719 return 0; 719 return 0;
720 } 720 }
721 721
722 u32 btrfs_count_snapshots_in_path(struct btrfs_root *root, 722 u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
723 struct btrfs_path *count_path, 723 struct btrfs_path *count_path,
724 u64 expected_owner, 724 u64 expected_owner,
725 u64 first_extent) 725 u64 first_extent)
726 { 726 {
727 struct btrfs_root *extent_root = root->fs_info->extent_root; 727 struct btrfs_root *extent_root = root->fs_info->extent_root;
728 struct btrfs_path *path; 728 struct btrfs_path *path;
729 u64 bytenr; 729 u64 bytenr;
730 u64 found_objectid; 730 u64 found_objectid;
731 u64 found_owner; 731 u64 found_owner;
732 u64 root_objectid = root->root_key.objectid; 732 u64 root_objectid = root->root_key.objectid;
733 u32 total_count = 0; 733 u32 total_count = 0;
734 u32 extent_refs; 734 u32 extent_refs;
735 u32 cur_count; 735 u32 cur_count;
736 u32 nritems; 736 u32 nritems;
737 int ret; 737 int ret;
738 struct btrfs_key key; 738 struct btrfs_key key;
739 struct btrfs_key found_key; 739 struct btrfs_key found_key;
740 struct extent_buffer *l; 740 struct extent_buffer *l;
741 struct btrfs_extent_item *item; 741 struct btrfs_extent_item *item;
742 struct btrfs_extent_ref *ref_item; 742 struct btrfs_extent_ref *ref_item;
743 int level = -1; 743 int level = -1;
744 744
745 path = btrfs_alloc_path(); 745 path = btrfs_alloc_path();
746 again: 746 again:
747 if (level == -1) 747 if (level == -1)
748 bytenr = first_extent; 748 bytenr = first_extent;
749 else 749 else
750 bytenr = count_path->nodes[level]->start; 750 bytenr = count_path->nodes[level]->start;
751 751
752 cur_count = 0; 752 cur_count = 0;
753 key.objectid = bytenr; 753 key.objectid = bytenr;
754 key.offset = 0; 754 key.offset = 0;
755 755
756 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); 756 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
757 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 757 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
758 if (ret < 0) 758 if (ret < 0)
759 goto out; 759 goto out;
760 BUG_ON(ret == 0); 760 BUG_ON(ret == 0);
761 761
762 l = path->nodes[0]; 762 l = path->nodes[0];
763 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]); 763 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
764 764
765 if (found_key.objectid != bytenr || 765 if (found_key.objectid != bytenr ||
766 found_key.type != BTRFS_EXTENT_ITEM_KEY) { 766 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
767 goto out; 767 goto out;
768 } 768 }
769 769
770 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); 770 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
771 extent_refs = btrfs_extent_refs(l, item); 771 extent_refs = btrfs_extent_refs(l, item);
772 while (1) { 772 while (1) {
773 l = path->nodes[0]; 773 l = path->nodes[0];
774 nritems = btrfs_header_nritems(l); 774 nritems = btrfs_header_nritems(l);
775 if (path->slots[0] >= nritems) { 775 if (path->slots[0] >= nritems) {
776 ret = btrfs_next_leaf(extent_root, path); 776 ret = btrfs_next_leaf(extent_root, path);
777 if (ret == 0) 777 if (ret == 0)
778 continue; 778 continue;
779 break; 779 break;
780 } 780 }
781 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]); 781 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
782 if (found_key.objectid != bytenr) 782 if (found_key.objectid != bytenr)
783 break; 783 break;
784 784
785 if (found_key.type != BTRFS_EXTENT_REF_KEY) { 785 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
786 path->slots[0]++; 786 path->slots[0]++;
787 continue; 787 continue;
788 } 788 }
789 789
790 cur_count++; 790 cur_count++;
791 ref_item = btrfs_item_ptr(l, path->slots[0], 791 ref_item = btrfs_item_ptr(l, path->slots[0],
792 struct btrfs_extent_ref); 792 struct btrfs_extent_ref);
793 found_objectid = btrfs_ref_root(l, ref_item); 793 found_objectid = btrfs_ref_root(l, ref_item);
794 794
795 if (found_objectid != root_objectid) { 795 if (found_objectid != root_objectid) {
796 total_count = 2; 796 total_count = 2;
797 goto out; 797 goto out;
798 } 798 }
799 if (level == -1) { 799 if (level == -1) {
800 found_owner = btrfs_ref_objectid(l, ref_item); 800 found_owner = btrfs_ref_objectid(l, ref_item);
801 if (found_owner != expected_owner) { 801 if (found_owner != expected_owner) {
802 total_count = 2; 802 total_count = 2;
803 goto out; 803 goto out;
804 } 804 }
805 /* 805 /*
806 * nasty. we don't count a reference held by 806 * nasty. we don't count a reference held by
807 * the running transaction. This allows nodatacow 807 * the running transaction. This allows nodatacow
808 * to avoid cow most of the time 808 * to avoid cow most of the time
809 */ 809 */
810 if (found_owner >= BTRFS_FIRST_FREE_OBJECTID && 810 if (found_owner >= BTRFS_FIRST_FREE_OBJECTID &&
811 btrfs_ref_generation(l, ref_item) == 811 btrfs_ref_generation(l, ref_item) ==
812 root->fs_info->generation) { 812 root->fs_info->generation) {
813 extent_refs--; 813 extent_refs--;
814 } 814 }
815 } 815 }
816 total_count = 1; 816 total_count = 1;
817 path->slots[0]++; 817 path->slots[0]++;
818 } 818 }
819 /* 819 /*
820 * if there is more than one reference against a data extent, 820 * if there is more than one reference against a data extent,
821 * we have to assume the other ref is another snapshot 821 * we have to assume the other ref is another snapshot
822 */ 822 */
823 if (level == -1 && extent_refs > 1) { 823 if (level == -1 && extent_refs > 1) {
824 total_count = 2; 824 total_count = 2;
825 goto out; 825 goto out;
826 } 826 }
827 if (cur_count == 0) { 827 if (cur_count == 0) {
828 total_count = 0; 828 total_count = 0;
829 goto out; 829 goto out;
830 } 830 }
831 if (level >= 0 && root->node == count_path->nodes[level]) 831 if (level >= 0 && root->node == count_path->nodes[level])
832 goto out; 832 goto out;
833 level++; 833 level++;
834 btrfs_release_path(root, path); 834 btrfs_release_path(root, path);
835 goto again; 835 goto again;
836 836
837 out: 837 out:
838 btrfs_free_path(path); 838 btrfs_free_path(path);
839 return total_count; 839 return total_count;
840 } 840 }
841 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans, 841 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
842 struct btrfs_root *root, u64 owner_objectid) 842 struct btrfs_root *root, u64 owner_objectid)
843 { 843 {
844 u64 generation; 844 u64 generation;
845 u64 key_objectid; 845 u64 key_objectid;
846 u64 level; 846 u64 level;
847 u32 nritems; 847 u32 nritems;
848 struct btrfs_disk_key disk_key; 848 struct btrfs_disk_key disk_key;
849 849
850 level = btrfs_header_level(root->node); 850 level = btrfs_header_level(root->node);
851 generation = trans->transid; 851 generation = trans->transid;
852 nritems = btrfs_header_nritems(root->node); 852 nritems = btrfs_header_nritems(root->node);
853 if (nritems > 0) { 853 if (nritems > 0) {
854 if (level == 0) 854 if (level == 0)
855 btrfs_item_key(root->node, &disk_key, 0); 855 btrfs_item_key(root->node, &disk_key, 0);
856 else 856 else
857 btrfs_node_key(root->node, &disk_key, 0); 857 btrfs_node_key(root->node, &disk_key, 0);
858 key_objectid = btrfs_disk_key_objectid(&disk_key); 858 key_objectid = btrfs_disk_key_objectid(&disk_key);
859 } else { 859 } else {
860 key_objectid = 0; 860 key_objectid = 0;
861 } 861 }
862 return btrfs_inc_extent_ref(trans, root, root->node->start, 862 return btrfs_inc_extent_ref(trans, root, root->node->start,
863 root->node->len, owner_objectid, 863 root->node->len, owner_objectid,
864 generation, level, key_objectid); 864 generation, level, key_objectid);
865 } 865 }
866 866
867 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, 867 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
868 struct extent_buffer *buf) 868 struct extent_buffer *buf)
869 { 869 {
870 u64 bytenr; 870 u64 bytenr;
871 u32 nritems; 871 u32 nritems;
872 struct btrfs_key key; 872 struct btrfs_key key;
873 struct btrfs_file_extent_item *fi; 873 struct btrfs_file_extent_item *fi;
874 int i; 874 int i;
875 int level; 875 int level;
876 int ret; 876 int ret;
877 int faili; 877 int faili;
878 878
879 if (!root->ref_cows) 879 if (!root->ref_cows)
880 return 0; 880 return 0;
881 881
882 level = btrfs_header_level(buf); 882 level = btrfs_header_level(buf);
883 nritems = btrfs_header_nritems(buf); 883 nritems = btrfs_header_nritems(buf);
884 for (i = 0; i < nritems; i++) { 884 for (i = 0; i < nritems; i++) {
885 if (level == 0) { 885 if (level == 0) {
886 u64 disk_bytenr; 886 u64 disk_bytenr;
887 btrfs_item_key_to_cpu(buf, &key, i); 887 btrfs_item_key_to_cpu(buf, &key, i);
888 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) 888 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
889 continue; 889 continue;
890 fi = btrfs_item_ptr(buf, i, 890 fi = btrfs_item_ptr(buf, i,
891 struct btrfs_file_extent_item); 891 struct btrfs_file_extent_item);
892 if (btrfs_file_extent_type(buf, fi) == 892 if (btrfs_file_extent_type(buf, fi) ==
893 BTRFS_FILE_EXTENT_INLINE) 893 BTRFS_FILE_EXTENT_INLINE)
894 continue; 894 continue;
895 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); 895 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
896 if (disk_bytenr == 0) 896 if (disk_bytenr == 0)
897 continue; 897 continue;
898 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, 898 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
899 btrfs_file_extent_disk_num_bytes(buf, fi), 899 btrfs_file_extent_disk_num_bytes(buf, fi),
900 root->root_key.objectid, trans->transid, 900 root->root_key.objectid, trans->transid,
901 key.objectid, key.offset); 901 key.objectid, key.offset);
902 if (ret) { 902 if (ret) {
903 faili = i; 903 faili = i;
904 goto fail; 904 goto fail;
905 } 905 }
906 } else { 906 } else {
907 bytenr = btrfs_node_blockptr(buf, i); 907 bytenr = btrfs_node_blockptr(buf, i);
908 btrfs_node_key_to_cpu(buf, &key, i); 908 btrfs_node_key_to_cpu(buf, &key, i);
909 ret = btrfs_inc_extent_ref(trans, root, bytenr, 909 ret = btrfs_inc_extent_ref(trans, root, bytenr,
910 btrfs_level_size(root, level - 1), 910 btrfs_level_size(root, level - 1),
911 root->root_key.objectid, 911 root->root_key.objectid,
912 trans->transid, 912 trans->transid,
913 level - 1, key.objectid); 913 level - 1, key.objectid);
914 if (ret) { 914 if (ret) {
915 faili = i; 915 faili = i;
916 goto fail; 916 goto fail;
917 } 917 }
918 } 918 }
919 } 919 }
920 return 0; 920 return 0;
921 fail: 921 fail:
922 WARN_ON(1); 922 WARN_ON(1);
923 #if 0 923 #if 0
924 for (i =0; i < faili; i++) { 924 for (i =0; i < faili; i++) {
925 if (level == 0) { 925 if (level == 0) {
926 u64 disk_bytenr; 926 u64 disk_bytenr;
927 btrfs_item_key_to_cpu(buf, &key, i); 927 btrfs_item_key_to_cpu(buf, &key, i);
928 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) 928 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
929 continue; 929 continue;
930 fi = btrfs_item_ptr(buf, i, 930 fi = btrfs_item_ptr(buf, i,
931 struct btrfs_file_extent_item); 931 struct btrfs_file_extent_item);
932 if (btrfs_file_extent_type(buf, fi) == 932 if (btrfs_file_extent_type(buf, fi) ==
933 BTRFS_FILE_EXTENT_INLINE) 933 BTRFS_FILE_EXTENT_INLINE)
934 continue; 934 continue;
935 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); 935 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
936 if (disk_bytenr == 0) 936 if (disk_bytenr == 0)
937 continue; 937 continue;
938 err = btrfs_free_extent(trans, root, disk_bytenr, 938 err = btrfs_free_extent(trans, root, disk_bytenr,
939 btrfs_file_extent_disk_num_bytes(buf, 939 btrfs_file_extent_disk_num_bytes(buf,
940 fi), 0); 940 fi), 0);
941 BUG_ON(err); 941 BUG_ON(err);
942 } else { 942 } else {
943 bytenr = btrfs_node_blockptr(buf, i); 943 bytenr = btrfs_node_blockptr(buf, i);
944 err = btrfs_free_extent(trans, root, bytenr, 944 err = btrfs_free_extent(trans, root, bytenr,
945 btrfs_level_size(root, level - 1), 0); 945 btrfs_level_size(root, level - 1), 0);
946 BUG_ON(err); 946 BUG_ON(err);
947 } 947 }
948 } 948 }
949 #endif 949 #endif
950 return ret; 950 return ret;
951 } 951 }
952 952
953 static int write_one_cache_group(struct btrfs_trans_handle *trans, 953 static int write_one_cache_group(struct btrfs_trans_handle *trans,
954 struct btrfs_root *root, 954 struct btrfs_root *root,
955 struct btrfs_path *path, 955 struct btrfs_path *path,
956 struct btrfs_block_group_cache *cache) 956 struct btrfs_block_group_cache *cache)
957 { 957 {
958 int ret; 958 int ret;
959 int pending_ret; 959 int pending_ret;
960 struct btrfs_root *extent_root = root->fs_info->extent_root; 960 struct btrfs_root *extent_root = root->fs_info->extent_root;
961 unsigned long bi; 961 unsigned long bi;
962 struct extent_buffer *leaf; 962 struct extent_buffer *leaf;
963 963
964 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); 964 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
965 if (ret < 0) 965 if (ret < 0)
966 goto fail; 966 goto fail;
967 BUG_ON(ret); 967 BUG_ON(ret);
968 968
969 leaf = path->nodes[0]; 969 leaf = path->nodes[0];
970 bi = btrfs_item_ptr_offset(leaf, path->slots[0]); 970 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
971 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); 971 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
972 btrfs_mark_buffer_dirty(leaf); 972 btrfs_mark_buffer_dirty(leaf);
973 btrfs_release_path(extent_root, path); 973 btrfs_release_path(extent_root, path);
974 fail: 974 fail:
975 finish_current_insert(trans, extent_root); 975 finish_current_insert(trans, extent_root);
976 pending_ret = del_pending_extents(trans, extent_root); 976 pending_ret = del_pending_extents(trans, extent_root);
977 if (ret) 977 if (ret)
978 return ret; 978 return ret;
979 if (pending_ret) 979 if (pending_ret)
980 return pending_ret; 980 return pending_ret;
981 return 0; 981 return 0;
982 982
983 } 983 }
984 984
985 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, 985 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
986 struct btrfs_root *root) 986 struct btrfs_root *root)
987 { 987 {
988 struct extent_io_tree *block_group_cache; 988 struct extent_io_tree *block_group_cache;
989 struct btrfs_block_group_cache *cache; 989 struct btrfs_block_group_cache *cache;
990 int ret; 990 int ret;
991 int err = 0; 991 int err = 0;
992 int werr = 0; 992 int werr = 0;
993 struct btrfs_path *path; 993 struct btrfs_path *path;
994 u64 last = 0; 994 u64 last = 0;
995 u64 start; 995 u64 start;
996 u64 end; 996 u64 end;
997 u64 ptr; 997 u64 ptr;
998 998
999 block_group_cache = &root->fs_info->block_group_cache; 999 block_group_cache = &root->fs_info->block_group_cache;
1000 path = btrfs_alloc_path(); 1000 path = btrfs_alloc_path();
1001 if (!path) 1001 if (!path)
1002 return -ENOMEM; 1002 return -ENOMEM;
1003 1003
1004 while(1) { 1004 while(1) {
1005 ret = find_first_extent_bit(block_group_cache, last, 1005 ret = find_first_extent_bit(block_group_cache, last,
1006 &start, &end, BLOCK_GROUP_DIRTY); 1006 &start, &end, BLOCK_GROUP_DIRTY);
1007 if (ret) 1007 if (ret)
1008 break; 1008 break;
1009 1009
1010 last = end + 1; 1010 last = end + 1;
1011 ret = get_state_private(block_group_cache, start, &ptr); 1011 ret = get_state_private(block_group_cache, start, &ptr);
1012 if (ret) 1012 if (ret)
1013 break; 1013 break;
1014 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr; 1014 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1015 err = write_one_cache_group(trans, root, 1015 err = write_one_cache_group(trans, root,
1016 path, cache); 1016 path, cache);
1017 /* 1017 /*
1018 * if we fail to write the cache group, we want 1018 * if we fail to write the cache group, we want
1019 * to keep it marked dirty in hopes that a later 1019 * to keep it marked dirty in hopes that a later
1020 * write will work 1020 * write will work
1021 */ 1021 */
1022 if (err) { 1022 if (err) {
1023 werr = err; 1023 werr = err;
1024 continue; 1024 continue;
1025 } 1025 }
1026 clear_extent_bits(block_group_cache, start, end, 1026 clear_extent_bits(block_group_cache, start, end,
1027 BLOCK_GROUP_DIRTY, GFP_NOFS); 1027 BLOCK_GROUP_DIRTY, GFP_NOFS);
1028 } 1028 }
1029 btrfs_free_path(path); 1029 btrfs_free_path(path);
1030 return werr; 1030 return werr;
1031 } 1031 }
1032 1032
1033 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, 1033 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1034 u64 flags) 1034 u64 flags)
1035 { 1035 {
1036 struct list_head *head = &info->space_info; 1036 struct list_head *head = &info->space_info;
1037 struct list_head *cur; 1037 struct list_head *cur;
1038 struct btrfs_space_info *found; 1038 struct btrfs_space_info *found;
1039 list_for_each(cur, head) { 1039 list_for_each(cur, head) {
1040 found = list_entry(cur, struct btrfs_space_info, list); 1040 found = list_entry(cur, struct btrfs_space_info, list);
1041 if (found->flags == flags) 1041 if (found->flags == flags)
1042 return found; 1042 return found;
1043 } 1043 }
1044 return NULL; 1044 return NULL;
1045 1045
1046 } 1046 }
1047 1047
1048 static int update_space_info(struct btrfs_fs_info *info, u64 flags, 1048 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1049 u64 total_bytes, u64 bytes_used, 1049 u64 total_bytes, u64 bytes_used,
1050 struct btrfs_space_info **space_info) 1050 struct btrfs_space_info **space_info)
1051 { 1051 {
1052 struct btrfs_space_info *found; 1052 struct btrfs_space_info *found;
1053 1053
1054 found = __find_space_info(info, flags); 1054 found = __find_space_info(info, flags);
1055 if (found) { 1055 if (found) {
1056 found->total_bytes += total_bytes; 1056 found->total_bytes += total_bytes;
1057 found->bytes_used += bytes_used; 1057 found->bytes_used += bytes_used;
1058 found->full = 0; 1058 found->full = 0;
1059 WARN_ON(found->total_bytes < found->bytes_used); 1059 WARN_ON(found->total_bytes < found->bytes_used);
1060 *space_info = found; 1060 *space_info = found;
1061 return 0; 1061 return 0;
1062 } 1062 }
1063 found = kmalloc(sizeof(*found), GFP_NOFS); 1063 found = kmalloc(sizeof(*found), GFP_NOFS);
1064 if (!found) 1064 if (!found)
1065 return -ENOMEM; 1065 return -ENOMEM;
1066 1066
1067 list_add(&found->list, &info->space_info); 1067 list_add(&found->list, &info->space_info);
1068 found->flags = flags; 1068 found->flags = flags;
1069 found->total_bytes = total_bytes; 1069 found->total_bytes = total_bytes;
1070 found->bytes_used = bytes_used; 1070 found->bytes_used = bytes_used;
1071 found->bytes_pinned = 0; 1071 found->bytes_pinned = 0;
1072 found->full = 0; 1072 found->full = 0;
1073 *space_info = found; 1073 *space_info = found;
1074 return 0; 1074 return 0;
1075 } 1075 }
1076 1076
1077 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) 1077 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1078 { 1078 {
1079 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | 1079 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1080 BTRFS_BLOCK_GROUP_RAID1 | 1080 BTRFS_BLOCK_GROUP_RAID1 |
1081 BTRFS_BLOCK_GROUP_RAID10 | 1081 BTRFS_BLOCK_GROUP_RAID10 |
1082 BTRFS_BLOCK_GROUP_DUP); 1082 BTRFS_BLOCK_GROUP_DUP);
1083 if (extra_flags) { 1083 if (extra_flags) {
1084 if (flags & BTRFS_BLOCK_GROUP_DATA) 1084 if (flags & BTRFS_BLOCK_GROUP_DATA)
1085 fs_info->avail_data_alloc_bits |= extra_flags; 1085 fs_info->avail_data_alloc_bits |= extra_flags;
1086 if (flags & BTRFS_BLOCK_GROUP_METADATA) 1086 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1087 fs_info->avail_metadata_alloc_bits |= extra_flags; 1087 fs_info->avail_metadata_alloc_bits |= extra_flags;
1088 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 1088 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1089 fs_info->avail_system_alloc_bits |= extra_flags; 1089 fs_info->avail_system_alloc_bits |= extra_flags;
1090 } 1090 }
1091 } 1091 }
1092 1092
1093 static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags) 1093 static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1094 { 1094 {
1095 u64 num_devices = root->fs_info->fs_devices->num_devices; 1095 u64 num_devices = root->fs_info->fs_devices->num_devices;
1096 1096
1097 if (num_devices == 1) 1097 if (num_devices == 1)
1098 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); 1098 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1099 if (num_devices < 4) 1099 if (num_devices < 4)
1100 flags &= ~BTRFS_BLOCK_GROUP_RAID10; 1100 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1101 1101
1102 if ((flags & BTRFS_BLOCK_GROUP_DUP) && 1102 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1103 (flags & (BTRFS_BLOCK_GROUP_RAID1 | 1103 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1104 BTRFS_BLOCK_GROUP_RAID10))) { 1104 BTRFS_BLOCK_GROUP_RAID10))) {
1105 flags &= ~BTRFS_BLOCK_GROUP_DUP; 1105 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1106 } 1106 }
1107 1107
1108 if ((flags & BTRFS_BLOCK_GROUP_RAID1) && 1108 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1109 (flags & BTRFS_BLOCK_GROUP_RAID10)) { 1109 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1110 flags &= ~BTRFS_BLOCK_GROUP_RAID1; 1110 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1111 } 1111 }
1112 1112
1113 if ((flags & BTRFS_BLOCK_GROUP_RAID0) && 1113 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1114 ((flags & BTRFS_BLOCK_GROUP_RAID1) | 1114 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1115 (flags & BTRFS_BLOCK_GROUP_RAID10) | 1115 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1116 (flags & BTRFS_BLOCK_GROUP_DUP))) 1116 (flags & BTRFS_BLOCK_GROUP_DUP)))
1117 flags &= ~BTRFS_BLOCK_GROUP_RAID0; 1117 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1118 return flags; 1118 return flags;
1119 } 1119 }
1120 1120
1121 static int do_chunk_alloc(struct btrfs_trans_handle *trans, 1121 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1122 struct btrfs_root *extent_root, u64 alloc_bytes, 1122 struct btrfs_root *extent_root, u64 alloc_bytes,
1123 u64 flags) 1123 u64 flags)
1124 { 1124 {
1125 struct btrfs_space_info *space_info; 1125 struct btrfs_space_info *space_info;
1126 u64 thresh; 1126 u64 thresh;
1127 u64 start; 1127 u64 start;
1128 u64 num_bytes; 1128 u64 num_bytes;
1129 int ret; 1129 int ret;
1130 1130
1131 flags = reduce_alloc_profile(extent_root, flags); 1131 flags = reduce_alloc_profile(extent_root, flags);
1132 1132
1133 space_info = __find_space_info(extent_root->fs_info, flags); 1133 space_info = __find_space_info(extent_root->fs_info, flags);
1134 if (!space_info) { 1134 if (!space_info) {
1135 ret = update_space_info(extent_root->fs_info, flags, 1135 ret = update_space_info(extent_root->fs_info, flags,
1136 0, 0, &space_info); 1136 0, 0, &space_info);
1137 BUG_ON(ret); 1137 BUG_ON(ret);
1138 } 1138 }
1139 BUG_ON(!space_info); 1139 BUG_ON(!space_info);
1140 1140
1141 if (space_info->full) 1141 if (space_info->full)
1142 return 0; 1142 return 0;
1143 1143
1144 thresh = div_factor(space_info->total_bytes, 6); 1144 thresh = div_factor(space_info->total_bytes, 6);
1145 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) < 1145 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1146 thresh) 1146 thresh)
1147 return 0; 1147 return 0;
1148 1148
1149 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags); 1149 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1150 if (ret == -ENOSPC) { 1150 if (ret == -ENOSPC) {
1151 printk("space info full %Lu\n", flags); 1151 printk("space info full %Lu\n", flags);
1152 space_info->full = 1; 1152 space_info->full = 1;
1153 return 0; 1153 return 0;
1154 } 1154 }
1155 1155
1156 BUG_ON(ret); 1156 BUG_ON(ret);
1157 1157
1158 ret = btrfs_make_block_group(trans, extent_root, 0, flags, 1158 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
1159 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes); 1159 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1160 BUG_ON(ret); 1160 BUG_ON(ret);
1161 1161
1162 return 0; 1162 return 0;
1163 } 1163 }
1164 1164
1165 static int update_block_group(struct btrfs_trans_handle *trans, 1165 static int update_block_group(struct btrfs_trans_handle *trans,
1166 struct btrfs_root *root, 1166 struct btrfs_root *root,
1167 u64 bytenr, u64 num_bytes, int alloc, 1167 u64 bytenr, u64 num_bytes, int alloc,
1168 int mark_free) 1168 int mark_free)
1169 { 1169 {
1170 struct btrfs_block_group_cache *cache; 1170 struct btrfs_block_group_cache *cache;
1171 struct btrfs_fs_info *info = root->fs_info; 1171 struct btrfs_fs_info *info = root->fs_info;
1172 u64 total = num_bytes; 1172 u64 total = num_bytes;
1173 u64 old_val; 1173 u64 old_val;
1174 u64 byte_in_group; 1174 u64 byte_in_group;
1175 u64 start; 1175 u64 start;
1176 u64 end; 1176 u64 end;
1177 1177
1178 while(total) { 1178 while(total) {
1179 cache = btrfs_lookup_block_group(info, bytenr); 1179 cache = btrfs_lookup_block_group(info, bytenr);
1180 if (!cache) { 1180 if (!cache) {
1181 return -1; 1181 return -1;
1182 } 1182 }
1183 byte_in_group = bytenr - cache->key.objectid; 1183 byte_in_group = bytenr - cache->key.objectid;
1184 WARN_ON(byte_in_group > cache->key.offset); 1184 WARN_ON(byte_in_group > cache->key.offset);
1185 start = cache->key.objectid; 1185 start = cache->key.objectid;
1186 end = start + cache->key.offset - 1; 1186 end = start + cache->key.offset - 1;
1187 set_extent_bits(&info->block_group_cache, start, end, 1187 set_extent_bits(&info->block_group_cache, start, end,
1188 BLOCK_GROUP_DIRTY, GFP_NOFS); 1188 BLOCK_GROUP_DIRTY, GFP_NOFS);
1189 1189
1190 old_val = btrfs_block_group_used(&cache->item); 1190 old_val = btrfs_block_group_used(&cache->item);
1191 num_bytes = min(total, cache->key.offset - byte_in_group); 1191 num_bytes = min(total, cache->key.offset - byte_in_group);
1192 if (alloc) { 1192 if (alloc) {
1193 old_val += num_bytes; 1193 old_val += num_bytes;
1194 cache->space_info->bytes_used += num_bytes; 1194 cache->space_info->bytes_used += num_bytes;
1195 } else { 1195 } else {
1196 old_val -= num_bytes; 1196 old_val -= num_bytes;
1197 cache->space_info->bytes_used -= num_bytes; 1197 cache->space_info->bytes_used -= num_bytes;
1198 if (mark_free) { 1198 if (mark_free) {
1199 set_extent_dirty(&info->free_space_cache, 1199 set_extent_dirty(&info->free_space_cache,
1200 bytenr, bytenr + num_bytes - 1, 1200 bytenr, bytenr + num_bytes - 1,
1201 GFP_NOFS); 1201 GFP_NOFS);
1202 } 1202 }
1203 } 1203 }
1204 btrfs_set_block_group_used(&cache->item, old_val); 1204 btrfs_set_block_group_used(&cache->item, old_val);
1205 total -= num_bytes; 1205 total -= num_bytes;
1206 bytenr += num_bytes; 1206 bytenr += num_bytes;
1207 } 1207 }
1208 return 0; 1208 return 0;
1209 } 1209 }
1210 1210
1211 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) 1211 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1212 { 1212 {
1213 u64 start; 1213 u64 start;
1214 u64 end; 1214 u64 end;
1215 int ret; 1215 int ret;
1216 ret = find_first_extent_bit(&root->fs_info->block_group_cache, 1216 ret = find_first_extent_bit(&root->fs_info->block_group_cache,
1217 search_start, &start, &end, 1217 search_start, &start, &end,
1218 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA | 1218 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
1219 BLOCK_GROUP_SYSTEM); 1219 BLOCK_GROUP_SYSTEM);
1220 if (ret) 1220 if (ret)
1221 return 0; 1221 return 0;
1222 return start; 1222 return start;
1223 } 1223 }
1224 1224
1225 1225
1226 static int update_pinned_extents(struct btrfs_root *root, 1226 static int update_pinned_extents(struct btrfs_root *root,
1227 u64 bytenr, u64 num, int pin) 1227 u64 bytenr, u64 num, int pin)
1228 { 1228 {
1229 u64 len; 1229 u64 len;
1230 struct btrfs_block_group_cache *cache; 1230 struct btrfs_block_group_cache *cache;
1231 struct btrfs_fs_info *fs_info = root->fs_info; 1231 struct btrfs_fs_info *fs_info = root->fs_info;
1232 1232
1233 if (pin) { 1233 if (pin) {
1234 set_extent_dirty(&fs_info->pinned_extents, 1234 set_extent_dirty(&fs_info->pinned_extents,
1235 bytenr, bytenr + num - 1, GFP_NOFS); 1235 bytenr, bytenr + num - 1, GFP_NOFS);
1236 } else { 1236 } else {
1237 clear_extent_dirty(&fs_info->pinned_extents, 1237 clear_extent_dirty(&fs_info->pinned_extents,
1238 bytenr, bytenr + num - 1, GFP_NOFS); 1238 bytenr, bytenr + num - 1, GFP_NOFS);
1239 } 1239 }
1240 while (num > 0) { 1240 while (num > 0) {
1241 cache = btrfs_lookup_block_group(fs_info, bytenr); 1241 cache = btrfs_lookup_block_group(fs_info, bytenr);
1242 if (!cache) { 1242 if (!cache) {
1243 u64 first = first_logical_byte(root, bytenr); 1243 u64 first = first_logical_byte(root, bytenr);
1244 WARN_ON(first < bytenr); 1244 WARN_ON(first < bytenr);
1245 len = min(first - bytenr, num); 1245 len = min(first - bytenr, num);
1246 } else { 1246 } else {
1247 len = min(num, cache->key.offset - 1247 len = min(num, cache->key.offset -
1248 (bytenr - cache->key.objectid)); 1248 (bytenr - cache->key.objectid));
1249 } 1249 }
1250 if (pin) { 1250 if (pin) {
1251 if (cache) { 1251 if (cache) {
1252 cache->pinned += len; 1252 cache->pinned += len;
1253 cache->space_info->bytes_pinned += len; 1253 cache->space_info->bytes_pinned += len;
1254 } 1254 }
1255 fs_info->total_pinned += len; 1255 fs_info->total_pinned += len;
1256 } else { 1256 } else {
1257 if (cache) { 1257 if (cache) {
1258 cache->pinned -= len; 1258 cache->pinned -= len;
1259 cache->space_info->bytes_pinned -= len; 1259 cache->space_info->bytes_pinned -= len;
1260 } 1260 }
1261 fs_info->total_pinned -= len; 1261 fs_info->total_pinned -= len;
1262 } 1262 }
1263 bytenr += len; 1263 bytenr += len;
1264 num -= len; 1264 num -= len;
1265 } 1265 }
1266 return 0; 1266 return 0;
1267 } 1267 }
1268 1268
1269 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy) 1269 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
1270 { 1270 {
1271 u64 last = 0; 1271 u64 last = 0;
1272 u64 start; 1272 u64 start;
1273 u64 end; 1273 u64 end;
1274 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; 1274 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
1275 int ret; 1275 int ret;
1276 1276
1277 while(1) { 1277 while(1) {
1278 ret = find_first_extent_bit(pinned_extents, last, 1278 ret = find_first_extent_bit(pinned_extents, last,
1279 &start, &end, EXTENT_DIRTY); 1279 &start, &end, EXTENT_DIRTY);
1280 if (ret) 1280 if (ret)
1281 break; 1281 break;
1282 set_extent_dirty(copy, start, end, GFP_NOFS); 1282 set_extent_dirty(copy, start, end, GFP_NOFS);
1283 last = end + 1; 1283 last = end + 1;
1284 } 1284 }
1285 return 0; 1285 return 0;
1286 } 1286 }
1287 1287
1288 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, 1288 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1289 struct btrfs_root *root, 1289 struct btrfs_root *root,
1290 struct extent_io_tree *unpin) 1290 struct extent_io_tree *unpin)
1291 { 1291 {
1292 u64 start; 1292 u64 start;
1293 u64 end; 1293 u64 end;
1294 int ret; 1294 int ret;
1295 struct extent_io_tree *free_space_cache; 1295 struct extent_io_tree *free_space_cache;
1296 free_space_cache = &root->fs_info->free_space_cache; 1296 free_space_cache = &root->fs_info->free_space_cache;
1297 1297
1298 while(1) { 1298 while(1) {
1299 ret = find_first_extent_bit(unpin, 0, &start, &end, 1299 ret = find_first_extent_bit(unpin, 0, &start, &end,
1300 EXTENT_DIRTY); 1300 EXTENT_DIRTY);
1301 if (ret) 1301 if (ret)
1302 break; 1302 break;
1303 update_pinned_extents(root, start, end + 1 - start, 0); 1303 update_pinned_extents(root, start, end + 1 - start, 0);
1304 clear_extent_dirty(unpin, start, end, GFP_NOFS); 1304 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1305 set_extent_dirty(free_space_cache, start, end, GFP_NOFS); 1305 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1306 } 1306 }
1307 return 0; 1307 return 0;
1308 } 1308 }
1309 1309
1310 static int finish_current_insert(struct btrfs_trans_handle *trans, 1310 static int finish_current_insert(struct btrfs_trans_handle *trans,
1311 struct btrfs_root *extent_root) 1311 struct btrfs_root *extent_root)
1312 { 1312 {
1313 u64 start; 1313 u64 start;
1314 u64 end; 1314 u64 end;
1315 struct btrfs_fs_info *info = extent_root->fs_info; 1315 struct btrfs_fs_info *info = extent_root->fs_info;
1316 struct extent_buffer *eb; 1316 struct extent_buffer *eb;
1317 struct btrfs_path *path; 1317 struct btrfs_path *path;
1318 struct btrfs_key ins; 1318 struct btrfs_key ins;
1319 struct btrfs_disk_key first; 1319 struct btrfs_disk_key first;
1320 struct btrfs_extent_item extent_item; 1320 struct btrfs_extent_item extent_item;
1321 int ret; 1321 int ret;
1322 int level; 1322 int level;
1323 int err = 0; 1323 int err = 0;
1324 1324
1325 btrfs_set_stack_extent_refs(&extent_item, 1); 1325 btrfs_set_stack_extent_refs(&extent_item, 1);
1326 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY); 1326 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
1327 path = btrfs_alloc_path(); 1327 path = btrfs_alloc_path();
1328 1328
1329 while(1) { 1329 while(1) {
1330 ret = find_first_extent_bit(&info->extent_ins, 0, &start, 1330 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1331 &end, EXTENT_LOCKED); 1331 &end, EXTENT_LOCKED);
1332 if (ret) 1332 if (ret)
1333 break; 1333 break;
1334 1334
1335 ins.objectid = start; 1335 ins.objectid = start;
1336 ins.offset = end + 1 - start; 1336 ins.offset = end + 1 - start;
1337 err = btrfs_insert_item(trans, extent_root, &ins, 1337 err = btrfs_insert_item(trans, extent_root, &ins,
1338 &extent_item, sizeof(extent_item)); 1338 &extent_item, sizeof(extent_item));
1339 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED, 1339 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1340 GFP_NOFS); 1340 GFP_NOFS);
1341 eb = read_tree_block(extent_root, ins.objectid, ins.offset); 1341 eb = read_tree_block(extent_root, ins.objectid, ins.offset,
1342 trans->transid);
1342 level = btrfs_header_level(eb); 1343 level = btrfs_header_level(eb);
1343 if (level == 0) { 1344 if (level == 0) {
1344 btrfs_item_key(eb, &first, 0); 1345 btrfs_item_key(eb, &first, 0);
1345 } else { 1346 } else {
1346 btrfs_node_key(eb, &first, 0); 1347 btrfs_node_key(eb, &first, 0);
1347 } 1348 }
1348 err = btrfs_insert_extent_backref(trans, extent_root, path, 1349 err = btrfs_insert_extent_backref(trans, extent_root, path,
1349 start, extent_root->root_key.objectid, 1350 start, extent_root->root_key.objectid,
1350 0, level, 1351 0, level,
1351 btrfs_disk_key_objectid(&first)); 1352 btrfs_disk_key_objectid(&first));
1352 BUG_ON(err); 1353 BUG_ON(err);
1353 free_extent_buffer(eb); 1354 free_extent_buffer(eb);
1354 } 1355 }
1355 btrfs_free_path(path); 1356 btrfs_free_path(path);
1356 return 0; 1357 return 0;
1357 } 1358 }
1358 1359
1359 static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes, 1360 static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1360 int pending) 1361 int pending)
1361 { 1362 {
1362 int err = 0; 1363 int err = 0;
1363 struct extent_buffer *buf; 1364 struct extent_buffer *buf;
1364 1365
1365 if (!pending) { 1366 if (!pending) {
1366 buf = btrfs_find_tree_block(root, bytenr, num_bytes); 1367 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1367 if (buf) { 1368 if (buf) {
1368 if (btrfs_buffer_uptodate(buf)) { 1369 if (btrfs_buffer_uptodate(buf)) {
1369 u64 transid = 1370 u64 transid =
1370 root->fs_info->running_transaction->transid; 1371 root->fs_info->running_transaction->transid;
1371 u64 header_transid = 1372 u64 header_transid =
1372 btrfs_header_generation(buf); 1373 btrfs_header_generation(buf);
1373 if (header_transid == transid && 1374 if (header_transid == transid &&
1374 !btrfs_header_flag(buf, 1375 !btrfs_header_flag(buf,
1375 BTRFS_HEADER_FLAG_WRITTEN)) { 1376 BTRFS_HEADER_FLAG_WRITTEN)) {
1376 clean_tree_block(NULL, root, buf); 1377 clean_tree_block(NULL, root, buf);
1377 free_extent_buffer(buf); 1378 free_extent_buffer(buf);
1378 return 1; 1379 return 1;
1379 } 1380 }
1380 } 1381 }
1381 free_extent_buffer(buf); 1382 free_extent_buffer(buf);
1382 } 1383 }
1383 update_pinned_extents(root, bytenr, num_bytes, 1); 1384 update_pinned_extents(root, bytenr, num_bytes, 1);
1384 } else { 1385 } else {
1385 set_extent_bits(&root->fs_info->pending_del, 1386 set_extent_bits(&root->fs_info->pending_del,
1386 bytenr, bytenr + num_bytes - 1, 1387 bytenr, bytenr + num_bytes - 1,
1387 EXTENT_LOCKED, GFP_NOFS); 1388 EXTENT_LOCKED, GFP_NOFS);
1388 } 1389 }
1389 BUG_ON(err < 0); 1390 BUG_ON(err < 0);
1390 return 0; 1391 return 0;
1391 } 1392 }
1392 1393
1393 /* 1394 /*
1394 * remove an extent from the root, returns 0 on success 1395 * remove an extent from the root, returns 0 on success
1395 */ 1396 */
1396 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root 1397 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1397 *root, u64 bytenr, u64 num_bytes, 1398 *root, u64 bytenr, u64 num_bytes,
1398 u64 root_objectid, u64 ref_generation, 1399 u64 root_objectid, u64 ref_generation,
1399 u64 owner_objectid, u64 owner_offset, int pin, 1400 u64 owner_objectid, u64 owner_offset, int pin,
1400 int mark_free) 1401 int mark_free)
1401 { 1402 {
1402 struct btrfs_path *path; 1403 struct btrfs_path *path;
1403 struct btrfs_key key; 1404 struct btrfs_key key;
1404 struct btrfs_fs_info *info = root->fs_info; 1405 struct btrfs_fs_info *info = root->fs_info;
1405 struct btrfs_root *extent_root = info->extent_root; 1406 struct btrfs_root *extent_root = info->extent_root;
1406 struct extent_buffer *leaf; 1407 struct extent_buffer *leaf;
1407 int ret; 1408 int ret;
1408 int extent_slot = 0; 1409 int extent_slot = 0;
1409 int found_extent = 0; 1410 int found_extent = 0;
1410 int num_to_del = 1; 1411 int num_to_del = 1;
1411 struct btrfs_extent_item *ei; 1412 struct btrfs_extent_item *ei;
1412 u32 refs; 1413 u32 refs;
1413 1414
1414 key.objectid = bytenr; 1415 key.objectid = bytenr;
1415 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); 1416 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1416 key.offset = num_bytes; 1417 key.offset = num_bytes;
1417 path = btrfs_alloc_path(); 1418 path = btrfs_alloc_path();
1418 if (!path) 1419 if (!path)
1419 return -ENOMEM; 1420 return -ENOMEM;
1420 1421
1421 path->reada = 1; 1422 path->reada = 1;
1422 ret = lookup_extent_backref(trans, extent_root, path, 1423 ret = lookup_extent_backref(trans, extent_root, path,
1423 bytenr, root_objectid, 1424 bytenr, root_objectid,
1424 ref_generation, 1425 ref_generation,
1425 owner_objectid, owner_offset, 1); 1426 owner_objectid, owner_offset, 1);
1426 if (ret == 0) { 1427 if (ret == 0) {
1427 struct btrfs_key found_key; 1428 struct btrfs_key found_key;
1428 extent_slot = path->slots[0]; 1429 extent_slot = path->slots[0];
1429 while(extent_slot > 0) { 1430 while(extent_slot > 0) {
1430 extent_slot--; 1431 extent_slot--;
1431 btrfs_item_key_to_cpu(path->nodes[0], &found_key, 1432 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1432 extent_slot); 1433 extent_slot);
1433 if (found_key.objectid != bytenr) 1434 if (found_key.objectid != bytenr)
1434 break; 1435 break;
1435 if (found_key.type == BTRFS_EXTENT_ITEM_KEY && 1436 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1436 found_key.offset == num_bytes) { 1437 found_key.offset == num_bytes) {
1437 found_extent = 1; 1438 found_extent = 1;
1438 break; 1439 break;
1439 } 1440 }
1440 if (path->slots[0] - extent_slot > 5) 1441 if (path->slots[0] - extent_slot > 5)
1441 break; 1442 break;
1442 } 1443 }
1443 if (!found_extent) 1444 if (!found_extent)
1444 ret = btrfs_del_item(trans, extent_root, path); 1445 ret = btrfs_del_item(trans, extent_root, path);
1445 } else { 1446 } else {
1446 btrfs_print_leaf(extent_root, path->nodes[0]); 1447 btrfs_print_leaf(extent_root, path->nodes[0]);
1447 WARN_ON(1); 1448 WARN_ON(1);
1448 printk("Unable to find ref byte nr %Lu root %Lu " 1449 printk("Unable to find ref byte nr %Lu root %Lu "
1449 " gen %Lu owner %Lu offset %Lu\n", bytenr, 1450 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1450 root_objectid, ref_generation, owner_objectid, 1451 root_objectid, ref_generation, owner_objectid,
1451 owner_offset); 1452 owner_offset);
1452 } 1453 }
1453 if (!found_extent) { 1454 if (!found_extent) {
1454 btrfs_release_path(extent_root, path); 1455 btrfs_release_path(extent_root, path);
1455 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); 1456 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1456 if (ret < 0) 1457 if (ret < 0)
1457 return ret; 1458 return ret;
1458 BUG_ON(ret); 1459 BUG_ON(ret);
1459 extent_slot = path->slots[0]; 1460 extent_slot = path->slots[0];
1460 } 1461 }
1461 1462
1462 leaf = path->nodes[0]; 1463 leaf = path->nodes[0];
1463 ei = btrfs_item_ptr(leaf, extent_slot, 1464 ei = btrfs_item_ptr(leaf, extent_slot,
1464 struct btrfs_extent_item); 1465 struct btrfs_extent_item);
1465 refs = btrfs_extent_refs(leaf, ei); 1466 refs = btrfs_extent_refs(leaf, ei);
1466 BUG_ON(refs == 0); 1467 BUG_ON(refs == 0);
1467 refs -= 1; 1468 refs -= 1;
1468 btrfs_set_extent_refs(leaf, ei, refs); 1469 btrfs_set_extent_refs(leaf, ei, refs);
1469 1470
1470 btrfs_mark_buffer_dirty(leaf); 1471 btrfs_mark_buffer_dirty(leaf);
1471 1472
1472 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) { 1473 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1473 /* if the back ref and the extent are next to each other 1474 /* if the back ref and the extent are next to each other
1474 * they get deleted below in one shot 1475 * they get deleted below in one shot
1475 */ 1476 */
1476 path->slots[0] = extent_slot; 1477 path->slots[0] = extent_slot;
1477 num_to_del = 2; 1478 num_to_del = 2;
1478 } else if (found_extent) { 1479 } else if (found_extent) {
1479 /* otherwise delete the extent back ref */ 1480 /* otherwise delete the extent back ref */
1480 ret = btrfs_del_item(trans, extent_root, path); 1481 ret = btrfs_del_item(trans, extent_root, path);
1481 BUG_ON(ret); 1482 BUG_ON(ret);
1482 /* if refs are 0, we need to setup the path for deletion */ 1483 /* if refs are 0, we need to setup the path for deletion */
1483 if (refs == 0) { 1484 if (refs == 0) {
1484 btrfs_release_path(extent_root, path); 1485 btrfs_release_path(extent_root, path);
1485 ret = btrfs_search_slot(trans, extent_root, &key, path, 1486 ret = btrfs_search_slot(trans, extent_root, &key, path,
1486 -1, 1); 1487 -1, 1);
1487 if (ret < 0) 1488 if (ret < 0)
1488 return ret; 1489 return ret;
1489 BUG_ON(ret); 1490 BUG_ON(ret);
1490 } 1491 }
1491 } 1492 }
1492 1493
1493 if (refs == 0) { 1494 if (refs == 0) {
1494 u64 super_used; 1495 u64 super_used;
1495 u64 root_used; 1496 u64 root_used;
1496 1497
1497 if (pin) { 1498 if (pin) {
1498 ret = pin_down_bytes(root, bytenr, num_bytes, 0); 1499 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
1499 if (ret > 0) 1500 if (ret > 0)
1500 mark_free = 1; 1501 mark_free = 1;
1501 BUG_ON(ret < 0); 1502 BUG_ON(ret < 0);
1502 } 1503 }
1503 1504
1504 /* block accounting for super block */ 1505 /* block accounting for super block */
1505 super_used = btrfs_super_bytes_used(&info->super_copy); 1506 super_used = btrfs_super_bytes_used(&info->super_copy);
1506 btrfs_set_super_bytes_used(&info->super_copy, 1507 btrfs_set_super_bytes_used(&info->super_copy,
1507 super_used - num_bytes); 1508 super_used - num_bytes);
1508 1509
1509 /* block accounting for root item */ 1510 /* block accounting for root item */
1510 root_used = btrfs_root_used(&root->root_item); 1511 root_used = btrfs_root_used(&root->root_item);
1511 btrfs_set_root_used(&root->root_item, 1512 btrfs_set_root_used(&root->root_item,
1512 root_used - num_bytes); 1513 root_used - num_bytes);
1513 ret = btrfs_del_items(trans, extent_root, path, path->slots[0], 1514 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1514 num_to_del); 1515 num_to_del);
1515 if (ret) { 1516 if (ret) {
1516 return ret; 1517 return ret;
1517 } 1518 }
1518 ret = update_block_group(trans, root, bytenr, num_bytes, 0, 1519 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1519 mark_free); 1520 mark_free);
1520 BUG_ON(ret); 1521 BUG_ON(ret);
1521 } 1522 }
1522 btrfs_free_path(path); 1523 btrfs_free_path(path);
1523 finish_current_insert(trans, extent_root); 1524 finish_current_insert(trans, extent_root);
1524 return ret; 1525 return ret;
1525 } 1526 }
1526 1527
1527 /* 1528 /*
1528 * find all the blocks marked as pending in the radix tree and remove 1529 * find all the blocks marked as pending in the radix tree and remove
1529 * them from the extent map 1530 * them from the extent map
1530 */ 1531 */
1531 static int del_pending_extents(struct btrfs_trans_handle *trans, struct 1532 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1532 btrfs_root *extent_root) 1533 btrfs_root *extent_root)
1533 { 1534 {
1534 int ret; 1535 int ret;
1535 int err = 0; 1536 int err = 0;
1536 u64 start; 1537 u64 start;
1537 u64 end; 1538 u64 end;
1538 struct extent_io_tree *pending_del; 1539 struct extent_io_tree *pending_del;
1539 struct extent_io_tree *pinned_extents; 1540 struct extent_io_tree *pinned_extents;
1540 1541
1541 pending_del = &extent_root->fs_info->pending_del; 1542 pending_del = &extent_root->fs_info->pending_del;
1542 pinned_extents = &extent_root->fs_info->pinned_extents; 1543 pinned_extents = &extent_root->fs_info->pinned_extents;
1543 1544
1544 while(1) { 1545 while(1) {
1545 ret = find_first_extent_bit(pending_del, 0, &start, &end, 1546 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1546 EXTENT_LOCKED); 1547 EXTENT_LOCKED);
1547 if (ret) 1548 if (ret)
1548 break; 1549 break;
1549 update_pinned_extents(extent_root, start, end + 1 - start, 1); 1550 update_pinned_extents(extent_root, start, end + 1 - start, 1);
1550 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED, 1551 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1551 GFP_NOFS); 1552 GFP_NOFS);
1552 ret = __free_extent(trans, extent_root, 1553 ret = __free_extent(trans, extent_root,
1553 start, end + 1 - start, 1554 start, end + 1 - start,
1554 extent_root->root_key.objectid, 1555 extent_root->root_key.objectid,
1555 0, 0, 0, 0, 0); 1556 0, 0, 0, 0, 0);
1556 if (ret) 1557 if (ret)
1557 err = ret; 1558 err = ret;
1558 } 1559 }
1559 return err; 1560 return err;
1560 } 1561 }
1561 1562
1562 /* 1563 /*
1563 * remove an extent from the root, returns 0 on success 1564 * remove an extent from the root, returns 0 on success
1564 */ 1565 */
1565 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root 1566 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1566 *root, u64 bytenr, u64 num_bytes, 1567 *root, u64 bytenr, u64 num_bytes,
1567 u64 root_objectid, u64 ref_generation, 1568 u64 root_objectid, u64 ref_generation,
1568 u64 owner_objectid, u64 owner_offset, int pin) 1569 u64 owner_objectid, u64 owner_offset, int pin)
1569 { 1570 {
1570 struct btrfs_root *extent_root = root->fs_info->extent_root; 1571 struct btrfs_root *extent_root = root->fs_info->extent_root;
1571 int pending_ret; 1572 int pending_ret;
1572 int ret; 1573 int ret;
1573 1574
1574 WARN_ON(num_bytes < root->sectorsize); 1575 WARN_ON(num_bytes < root->sectorsize);
1575 if (!root->ref_cows) 1576 if (!root->ref_cows)
1576 ref_generation = 0; 1577 ref_generation = 0;
1577 1578
1578 if (root == extent_root) { 1579 if (root == extent_root) {
1579 pin_down_bytes(root, bytenr, num_bytes, 1); 1580 pin_down_bytes(root, bytenr, num_bytes, 1);
1580 return 0; 1581 return 0;
1581 } 1582 }
1582 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid, 1583 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1583 ref_generation, owner_objectid, owner_offset, 1584 ref_generation, owner_objectid, owner_offset,
1584 pin, pin == 0); 1585 pin, pin == 0);
1585 pending_ret = del_pending_extents(trans, root->fs_info->extent_root); 1586 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
1586 return ret ? ret : pending_ret; 1587 return ret ? ret : pending_ret;
1587 } 1588 }
1588 1589
1589 static u64 stripe_align(struct btrfs_root *root, u64 val) 1590 static u64 stripe_align(struct btrfs_root *root, u64 val)
1590 { 1591 {
1591 u64 mask = ((u64)root->stripesize - 1); 1592 u64 mask = ((u64)root->stripesize - 1);
1592 u64 ret = (val + mask) & ~mask; 1593 u64 ret = (val + mask) & ~mask;
1593 return ret; 1594 return ret;
1594 } 1595 }
1595 1596
1596 /* 1597 /*
1597 * walks the btree of allocated extents and find a hole of a given size. 1598 * walks the btree of allocated extents and find a hole of a given size.
1598 * The key ins is changed to record the hole: 1599 * The key ins is changed to record the hole:
1599 * ins->objectid == block start 1600 * ins->objectid == block start
1600 * ins->flags = BTRFS_EXTENT_ITEM_KEY 1601 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1601 * ins->offset == number of blocks 1602 * ins->offset == number of blocks
1602 * Any available blocks before search_start are skipped. 1603 * Any available blocks before search_start are skipped.
1603 */ 1604 */
1604 static int noinline find_free_extent(struct btrfs_trans_handle *trans, 1605 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1605 struct btrfs_root *orig_root, 1606 struct btrfs_root *orig_root,
1606 u64 num_bytes, u64 empty_size, 1607 u64 num_bytes, u64 empty_size,
1607 u64 search_start, u64 search_end, 1608 u64 search_start, u64 search_end,
1608 u64 hint_byte, struct btrfs_key *ins, 1609 u64 hint_byte, struct btrfs_key *ins,
1609 u64 exclude_start, u64 exclude_nr, 1610 u64 exclude_start, u64 exclude_nr,
1610 int data) 1611 int data)
1611 { 1612 {
1612 int ret; 1613 int ret;
1613 u64 orig_search_start; 1614 u64 orig_search_start;
1614 struct btrfs_root * root = orig_root->fs_info->extent_root; 1615 struct btrfs_root * root = orig_root->fs_info->extent_root;
1615 struct btrfs_fs_info *info = root->fs_info; 1616 struct btrfs_fs_info *info = root->fs_info;
1616 u64 total_needed = num_bytes; 1617 u64 total_needed = num_bytes;
1617 u64 *last_ptr = NULL; 1618 u64 *last_ptr = NULL;
1618 struct btrfs_block_group_cache *block_group; 1619 struct btrfs_block_group_cache *block_group;
1619 int full_scan = 0; 1620 int full_scan = 0;
1620 int wrapped = 0; 1621 int wrapped = 0;
1621 int empty_cluster = 2 * 1024 * 1024; 1622 int empty_cluster = 2 * 1024 * 1024;
1622 1623
1623 WARN_ON(num_bytes < root->sectorsize); 1624 WARN_ON(num_bytes < root->sectorsize);
1624 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); 1625 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1625 1626
1626 if (data & BTRFS_BLOCK_GROUP_METADATA) { 1627 if (data & BTRFS_BLOCK_GROUP_METADATA) {
1627 last_ptr = &root->fs_info->last_alloc; 1628 last_ptr = &root->fs_info->last_alloc;
1628 empty_cluster = 256 * 1024; 1629 empty_cluster = 256 * 1024;
1629 } 1630 }
1630 1631
1631 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) { 1632 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
1632 last_ptr = &root->fs_info->last_data_alloc; 1633 last_ptr = &root->fs_info->last_data_alloc;
1633 } 1634 }
1634 1635
1635 if (last_ptr) { 1636 if (last_ptr) {
1636 if (*last_ptr) 1637 if (*last_ptr)
1637 hint_byte = *last_ptr; 1638 hint_byte = *last_ptr;
1638 else { 1639 else {
1639 empty_size += empty_cluster; 1640 empty_size += empty_cluster;
1640 } 1641 }
1641 } 1642 }
1642 1643
1643 search_start = max(search_start, first_logical_byte(root, 0)); 1644 search_start = max(search_start, first_logical_byte(root, 0));
1644 orig_search_start = search_start; 1645 orig_search_start = search_start;
1645 1646
1646 if (search_end == (u64)-1) 1647 if (search_end == (u64)-1)
1647 search_end = btrfs_super_total_bytes(&info->super_copy); 1648 search_end = btrfs_super_total_bytes(&info->super_copy);
1648 1649
1649 if (hint_byte) { 1650 if (hint_byte) {
1650 block_group = btrfs_lookup_block_group(info, hint_byte); 1651 block_group = btrfs_lookup_block_group(info, hint_byte);
1651 if (!block_group) 1652 if (!block_group)
1652 hint_byte = search_start; 1653 hint_byte = search_start;
1653 block_group = btrfs_find_block_group(root, block_group, 1654 block_group = btrfs_find_block_group(root, block_group,
1654 hint_byte, data, 1); 1655 hint_byte, data, 1);
1655 if (last_ptr && *last_ptr == 0 && block_group) 1656 if (last_ptr && *last_ptr == 0 && block_group)
1656 hint_byte = block_group->key.objectid; 1657 hint_byte = block_group->key.objectid;
1657 } else { 1658 } else {
1658 block_group = btrfs_find_block_group(root, 1659 block_group = btrfs_find_block_group(root,
1659 trans->block_group, 1660 trans->block_group,
1660 search_start, data, 1); 1661 search_start, data, 1);
1661 } 1662 }
1662 search_start = max(search_start, hint_byte); 1663 search_start = max(search_start, hint_byte);
1663 1664
1664 total_needed += empty_size; 1665 total_needed += empty_size;
1665 1666
1666 check_failed: 1667 check_failed:
1667 if (!block_group) { 1668 if (!block_group) {
1668 block_group = btrfs_lookup_block_group(info, search_start); 1669 block_group = btrfs_lookup_block_group(info, search_start);
1669 if (!block_group) 1670 if (!block_group)
1670 block_group = btrfs_lookup_block_group(info, 1671 block_group = btrfs_lookup_block_group(info,
1671 orig_search_start); 1672 orig_search_start);
1672 } 1673 }
1673 ret = find_search_start(root, &block_group, &search_start, 1674 ret = find_search_start(root, &block_group, &search_start,
1674 total_needed, data); 1675 total_needed, data);
1675 if (ret == -ENOSPC && last_ptr && *last_ptr) { 1676 if (ret == -ENOSPC && last_ptr && *last_ptr) {
1676 *last_ptr = 0; 1677 *last_ptr = 0;
1677 block_group = btrfs_lookup_block_group(info, 1678 block_group = btrfs_lookup_block_group(info,
1678 orig_search_start); 1679 orig_search_start);
1679 search_start = orig_search_start; 1680 search_start = orig_search_start;
1680 ret = find_search_start(root, &block_group, &search_start, 1681 ret = find_search_start(root, &block_group, &search_start,
1681 total_needed, data); 1682 total_needed, data);
1682 } 1683 }
1683 if (ret == -ENOSPC) 1684 if (ret == -ENOSPC)
1684 goto enospc; 1685 goto enospc;
1685 if (ret) 1686 if (ret)
1686 goto error; 1687 goto error;
1687 1688
1688 if (last_ptr && *last_ptr && search_start != *last_ptr) { 1689 if (last_ptr && *last_ptr && search_start != *last_ptr) {
1689 *last_ptr = 0; 1690 *last_ptr = 0;
1690 if (!empty_size) { 1691 if (!empty_size) {
1691 empty_size += empty_cluster; 1692 empty_size += empty_cluster;
1692 total_needed += empty_size; 1693 total_needed += empty_size;
1693 } 1694 }
1694 block_group = btrfs_lookup_block_group(info, 1695 block_group = btrfs_lookup_block_group(info,
1695 orig_search_start); 1696 orig_search_start);
1696 search_start = orig_search_start; 1697 search_start = orig_search_start;
1697 ret = find_search_start(root, &block_group, 1698 ret = find_search_start(root, &block_group,
1698 &search_start, total_needed, data); 1699 &search_start, total_needed, data);
1699 if (ret == -ENOSPC) 1700 if (ret == -ENOSPC)
1700 goto enospc; 1701 goto enospc;
1701 if (ret) 1702 if (ret)
1702 goto error; 1703 goto error;
1703 } 1704 }
1704 1705
1705 search_start = stripe_align(root, search_start); 1706 search_start = stripe_align(root, search_start);
1706 ins->objectid = search_start; 1707 ins->objectid = search_start;
1707 ins->offset = num_bytes; 1708 ins->offset = num_bytes;
1708 1709
1709 if (ins->objectid + num_bytes >= search_end) 1710 if (ins->objectid + num_bytes >= search_end)
1710 goto enospc; 1711 goto enospc;
1711 1712
1712 if (ins->objectid + num_bytes > 1713 if (ins->objectid + num_bytes >
1713 block_group->key.objectid + block_group->key.offset) { 1714 block_group->key.objectid + block_group->key.offset) {
1714 search_start = block_group->key.objectid + 1715 search_start = block_group->key.objectid +
1715 block_group->key.offset; 1716 block_group->key.offset;
1716 goto new_group; 1717 goto new_group;
1717 } 1718 }
1718 1719
1719 if (test_range_bit(&info->extent_ins, ins->objectid, 1720 if (test_range_bit(&info->extent_ins, ins->objectid,
1720 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) { 1721 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1721 search_start = ins->objectid + num_bytes; 1722 search_start = ins->objectid + num_bytes;
1722 goto new_group; 1723 goto new_group;
1723 } 1724 }
1724 1725
1725 if (test_range_bit(&info->pinned_extents, ins->objectid, 1726 if (test_range_bit(&info->pinned_extents, ins->objectid,
1726 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) { 1727 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1727 search_start = ins->objectid + num_bytes; 1728 search_start = ins->objectid + num_bytes;
1728 goto new_group; 1729 goto new_group;
1729 } 1730 }
1730 1731
1731 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start && 1732 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1732 ins->objectid < exclude_start + exclude_nr)) { 1733 ins->objectid < exclude_start + exclude_nr)) {
1733 search_start = exclude_start + exclude_nr; 1734 search_start = exclude_start + exclude_nr;
1734 goto new_group; 1735 goto new_group;
1735 } 1736 }
1736 1737
1737 if (!(data & BTRFS_BLOCK_GROUP_DATA)) { 1738 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
1738 block_group = btrfs_lookup_block_group(info, ins->objectid); 1739 block_group = btrfs_lookup_block_group(info, ins->objectid);
1739 if (block_group) 1740 if (block_group)
1740 trans->block_group = block_group; 1741 trans->block_group = block_group;
1741 } 1742 }
1742 ins->offset = num_bytes; 1743 ins->offset = num_bytes;
1743 if (last_ptr) { 1744 if (last_ptr) {
1744 *last_ptr = ins->objectid + ins->offset; 1745 *last_ptr = ins->objectid + ins->offset;
1745 if (*last_ptr == 1746 if (*last_ptr ==
1746 btrfs_super_total_bytes(&root->fs_info->super_copy)) { 1747 btrfs_super_total_bytes(&root->fs_info->super_copy)) {
1747 *last_ptr = 0; 1748 *last_ptr = 0;
1748 } 1749 }
1749 } 1750 }
1750 return 0; 1751 return 0;
1751 1752
1752 new_group: 1753 new_group:
1753 if (search_start + num_bytes >= search_end) { 1754 if (search_start + num_bytes >= search_end) {
1754 enospc: 1755 enospc:
1755 search_start = orig_search_start; 1756 search_start = orig_search_start;
1756 if (full_scan) { 1757 if (full_scan) {
1757 ret = -ENOSPC; 1758 ret = -ENOSPC;
1758 goto error; 1759 goto error;
1759 } 1760 }
1760 if (wrapped) { 1761 if (wrapped) {
1761 if (!full_scan) 1762 if (!full_scan)
1762 total_needed -= empty_size; 1763 total_needed -= empty_size;
1763 full_scan = 1; 1764 full_scan = 1;
1764 } else 1765 } else
1765 wrapped = 1; 1766 wrapped = 1;
1766 } 1767 }
1767 block_group = btrfs_lookup_block_group(info, search_start); 1768 block_group = btrfs_lookup_block_group(info, search_start);
1768 cond_resched(); 1769 cond_resched();
1769 block_group = btrfs_find_block_group(root, block_group, 1770 block_group = btrfs_find_block_group(root, block_group,
1770 search_start, data, 0); 1771 search_start, data, 0);
1771 goto check_failed; 1772 goto check_failed;
1772 1773
1773 error: 1774 error:
1774 return ret; 1775 return ret;
1775 } 1776 }
1776 1777
1777 /* 1778 /*
1778 * finds a free extent and does all the dirty work required for allocation 1779 * finds a free extent and does all the dirty work required for allocation
1779 * returns the key for the extent through ins, and a tree buffer for 1780 * returns the key for the extent through ins, and a tree buffer for
1780 * the first block of the extent through buf. 1781 * the first block of the extent through buf.
1781 * 1782 *
1782 * returns 0 if everything worked, non-zero otherwise. 1783 * returns 0 if everything worked, non-zero otherwise.
1783 */ 1784 */
1784 int btrfs_alloc_extent(struct btrfs_trans_handle *trans, 1785 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1785 struct btrfs_root *root, 1786 struct btrfs_root *root,
1786 u64 num_bytes, u64 min_alloc_size, 1787 u64 num_bytes, u64 min_alloc_size,
1787 u64 root_objectid, u64 ref_generation, 1788 u64 root_objectid, u64 ref_generation,
1788 u64 owner, u64 owner_offset, 1789 u64 owner, u64 owner_offset,
1789 u64 empty_size, u64 hint_byte, 1790 u64 empty_size, u64 hint_byte,
1790 u64 search_end, struct btrfs_key *ins, u64 data) 1791 u64 search_end, struct btrfs_key *ins, u64 data)
1791 { 1792 {
1792 int ret; 1793 int ret;
1793 int pending_ret; 1794 int pending_ret;
1794 u64 super_used; 1795 u64 super_used;
1795 u64 root_used; 1796 u64 root_used;
1796 u64 search_start = 0; 1797 u64 search_start = 0;
1797 u64 alloc_profile; 1798 u64 alloc_profile;
1798 u32 sizes[2]; 1799 u32 sizes[2];
1799 struct btrfs_fs_info *info = root->fs_info; 1800 struct btrfs_fs_info *info = root->fs_info;
1800 struct btrfs_root *extent_root = info->extent_root; 1801 struct btrfs_root *extent_root = info->extent_root;
1801 struct btrfs_extent_item *extent_item; 1802 struct btrfs_extent_item *extent_item;
1802 struct btrfs_extent_ref *ref; 1803 struct btrfs_extent_ref *ref;
1803 struct btrfs_path *path; 1804 struct btrfs_path *path;
1804 struct btrfs_key keys[2]; 1805 struct btrfs_key keys[2];
1805 1806
1806 if (data) { 1807 if (data) {
1807 alloc_profile = info->avail_data_alloc_bits & 1808 alloc_profile = info->avail_data_alloc_bits &
1808 info->data_alloc_profile; 1809 info->data_alloc_profile;
1809 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; 1810 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1810 } else if (root == root->fs_info->chunk_root) { 1811 } else if (root == root->fs_info->chunk_root) {
1811 alloc_profile = info->avail_system_alloc_bits & 1812 alloc_profile = info->avail_system_alloc_bits &
1812 info->system_alloc_profile; 1813 info->system_alloc_profile;
1813 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; 1814 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1814 } else { 1815 } else {
1815 alloc_profile = info->avail_metadata_alloc_bits & 1816 alloc_profile = info->avail_metadata_alloc_bits &
1816 info->metadata_alloc_profile; 1817 info->metadata_alloc_profile;
1817 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; 1818 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1818 } 1819 }
1819 again: 1820 again:
1820 data = reduce_alloc_profile(root, data); 1821 data = reduce_alloc_profile(root, data);
1821 if (root->ref_cows) { 1822 if (root->ref_cows) {
1822 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { 1823 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
1823 ret = do_chunk_alloc(trans, root->fs_info->extent_root, 1824 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1824 2 * 1024 * 1024, 1825 2 * 1024 * 1024,
1825 BTRFS_BLOCK_GROUP_METADATA | 1826 BTRFS_BLOCK_GROUP_METADATA |
1826 (info->metadata_alloc_profile & 1827 (info->metadata_alloc_profile &
1827 info->avail_metadata_alloc_bits)); 1828 info->avail_metadata_alloc_bits));
1828 BUG_ON(ret); 1829 BUG_ON(ret);
1829 } 1830 }
1830 ret = do_chunk_alloc(trans, root->fs_info->extent_root, 1831 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1831 num_bytes + 2 * 1024 * 1024, data); 1832 num_bytes + 2 * 1024 * 1024, data);
1832 BUG_ON(ret); 1833 BUG_ON(ret);
1833 } 1834 }
1834 1835
1835 WARN_ON(num_bytes < root->sectorsize); 1836 WARN_ON(num_bytes < root->sectorsize);
1836 ret = find_free_extent(trans, root, num_bytes, empty_size, 1837 ret = find_free_extent(trans, root, num_bytes, empty_size,
1837 search_start, search_end, hint_byte, ins, 1838 search_start, search_end, hint_byte, ins,
1838 trans->alloc_exclude_start, 1839 trans->alloc_exclude_start,
1839 trans->alloc_exclude_nr, data); 1840 trans->alloc_exclude_nr, data);
1840 1841
1841 if (ret == -ENOSPC && num_bytes > min_alloc_size) { 1842 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
1842 num_bytes = num_bytes >> 1; 1843 num_bytes = num_bytes >> 1;
1843 num_bytes = max(num_bytes, min_alloc_size); 1844 num_bytes = max(num_bytes, min_alloc_size);
1844 goto again; 1845 goto again;
1845 } 1846 }
1846 if (ret) { 1847 if (ret) {
1847 printk("allocation failed flags %Lu\n", data); 1848 printk("allocation failed flags %Lu\n", data);
1848 } 1849 }
1849 BUG_ON(ret); 1850 BUG_ON(ret);
1850 if (ret) 1851 if (ret)
1851 return ret; 1852 return ret;
1852 1853
1853 /* block accounting for super block */ 1854 /* block accounting for super block */
1854 super_used = btrfs_super_bytes_used(&info->super_copy); 1855 super_used = btrfs_super_bytes_used(&info->super_copy);
1855 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); 1856 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1856 1857
1857 /* block accounting for root item */ 1858 /* block accounting for root item */
1858 root_used = btrfs_root_used(&root->root_item); 1859 root_used = btrfs_root_used(&root->root_item);
1859 btrfs_set_root_used(&root->root_item, root_used + num_bytes); 1860 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1860 1861
1861 clear_extent_dirty(&root->fs_info->free_space_cache, 1862 clear_extent_dirty(&root->fs_info->free_space_cache,
1862 ins->objectid, ins->objectid + ins->offset - 1, 1863 ins->objectid, ins->objectid + ins->offset - 1,
1863 GFP_NOFS); 1864 GFP_NOFS);
1864 1865
1865 if (root == extent_root) { 1866 if (root == extent_root) {
1866 set_extent_bits(&root->fs_info->extent_ins, ins->objectid, 1867 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1867 ins->objectid + ins->offset - 1, 1868 ins->objectid + ins->offset - 1,
1868 EXTENT_LOCKED, GFP_NOFS); 1869 EXTENT_LOCKED, GFP_NOFS);
1869 goto update_block; 1870 goto update_block;
1870 } 1871 }
1871 1872
1872 WARN_ON(trans->alloc_exclude_nr); 1873 WARN_ON(trans->alloc_exclude_nr);
1873 trans->alloc_exclude_start = ins->objectid; 1874 trans->alloc_exclude_start = ins->objectid;
1874 trans->alloc_exclude_nr = ins->offset; 1875 trans->alloc_exclude_nr = ins->offset;
1875 1876
1876 memcpy(&keys[0], ins, sizeof(*ins)); 1877 memcpy(&keys[0], ins, sizeof(*ins));
1877 keys[1].offset = hash_extent_ref(root_objectid, ref_generation, 1878 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
1878 owner, owner_offset); 1879 owner, owner_offset);
1879 keys[1].objectid = ins->objectid; 1880 keys[1].objectid = ins->objectid;
1880 keys[1].type = BTRFS_EXTENT_REF_KEY; 1881 keys[1].type = BTRFS_EXTENT_REF_KEY;
1881 sizes[0] = sizeof(*extent_item); 1882 sizes[0] = sizeof(*extent_item);
1882 sizes[1] = sizeof(*ref); 1883 sizes[1] = sizeof(*ref);
1883 1884
1884 path = btrfs_alloc_path(); 1885 path = btrfs_alloc_path();
1885 BUG_ON(!path); 1886 BUG_ON(!path);
1886 1887
1887 ret = btrfs_insert_empty_items(trans, extent_root, path, keys, 1888 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
1888 sizes, 2); 1889 sizes, 2);
1889 1890
1890 BUG_ON(ret); 1891 BUG_ON(ret);
1891 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], 1892 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1892 struct btrfs_extent_item); 1893 struct btrfs_extent_item);
1893 btrfs_set_extent_refs(path->nodes[0], extent_item, 1); 1894 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
1894 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, 1895 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1895 struct btrfs_extent_ref); 1896 struct btrfs_extent_ref);
1896 1897
1897 btrfs_set_ref_root(path->nodes[0], ref, root_objectid); 1898 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
1898 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); 1899 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
1899 btrfs_set_ref_objectid(path->nodes[0], ref, owner); 1900 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
1900 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset); 1901 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
1901 1902
1902 btrfs_mark_buffer_dirty(path->nodes[0]); 1903 btrfs_mark_buffer_dirty(path->nodes[0]);
1903 1904
1904 trans->alloc_exclude_start = 0; 1905 trans->alloc_exclude_start = 0;
1905 trans->alloc_exclude_nr = 0; 1906 trans->alloc_exclude_nr = 0;
1906 btrfs_free_path(path); 1907 btrfs_free_path(path);
1907 finish_current_insert(trans, extent_root); 1908 finish_current_insert(trans, extent_root);
1908 pending_ret = del_pending_extents(trans, extent_root); 1909 pending_ret = del_pending_extents(trans, extent_root);
1909 1910
1910 if (ret) { 1911 if (ret) {
1911 return ret; 1912 return ret;
1912 } 1913 }
1913 if (pending_ret) { 1914 if (pending_ret) {
1914 return pending_ret; 1915 return pending_ret;
1915 } 1916 }
1916 1917
1917 update_block: 1918 update_block:
1918 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0); 1919 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
1919 if (ret) { 1920 if (ret) {
1920 printk("update block group failed for %Lu %Lu\n", 1921 printk("update block group failed for %Lu %Lu\n",
1921 ins->objectid, ins->offset); 1922 ins->objectid, ins->offset);
1922 BUG(); 1923 BUG();
1923 } 1924 }
1924 return 0; 1925 return 0;
1925 } 1926 }
1926 1927
1927 /* 1928 /*
1928 * helper function to allocate a block for a given tree 1929 * helper function to allocate a block for a given tree
1929 * returns the tree buffer or NULL. 1930 * returns the tree buffer or NULL.
1930 */ 1931 */
1931 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, 1932 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1932 struct btrfs_root *root, 1933 struct btrfs_root *root,
1933 u32 blocksize, 1934 u32 blocksize,
1934 u64 root_objectid, u64 hint, 1935 u64 root_objectid, u64 hint,
1935 u64 empty_size) 1936 u64 empty_size)
1936 { 1937 {
1937 u64 ref_generation; 1938 u64 ref_generation;
1938 1939
1939 if (root->ref_cows) 1940 if (root->ref_cows)
1940 ref_generation = trans->transid; 1941 ref_generation = trans->transid;
1941 else 1942 else
1942 ref_generation = 0; 1943 ref_generation = 0;
1943 1944
1944 1945
1945 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid, 1946 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1946 ref_generation, 0, 0, hint, empty_size); 1947 ref_generation, 0, 0, hint, empty_size);
1947 } 1948 }
1948 1949
1949 /* 1950 /*
1950 * helper function to allocate a block for a given tree 1951 * helper function to allocate a block for a given tree
1951 * returns the tree buffer or NULL. 1952 * returns the tree buffer or NULL.
1952 */ 1953 */
1953 struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans, 1954 struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1954 struct btrfs_root *root, 1955 struct btrfs_root *root,
1955 u32 blocksize, 1956 u32 blocksize,
1956 u64 root_objectid, 1957 u64 root_objectid,
1957 u64 ref_generation, 1958 u64 ref_generation,
1958 u64 first_objectid, 1959 u64 first_objectid,
1959 int level, 1960 int level,
1960 u64 hint, 1961 u64 hint,
1961 u64 empty_size) 1962 u64 empty_size)
1962 { 1963 {
1963 struct btrfs_key ins; 1964 struct btrfs_key ins;
1964 int ret; 1965 int ret;
1965 struct extent_buffer *buf; 1966 struct extent_buffer *buf;
1966 1967
1967 ret = btrfs_alloc_extent(trans, root, blocksize, blocksize, 1968 ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
1968 root_objectid, ref_generation, 1969 root_objectid, ref_generation,
1969 level, first_objectid, empty_size, hint, 1970 level, first_objectid, empty_size, hint,
1970 (u64)-1, &ins, 0); 1971 (u64)-1, &ins, 0);
1971 if (ret) { 1972 if (ret) {
1972 BUG_ON(ret > 0); 1973 BUG_ON(ret > 0);
1973 return ERR_PTR(ret); 1974 return ERR_PTR(ret);
1974 } 1975 }
1975 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize); 1976 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1976 if (!buf) { 1977 if (!buf) {
1977 btrfs_free_extent(trans, root, ins.objectid, blocksize, 1978 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1978 root->root_key.objectid, ref_generation, 1979 root->root_key.objectid, ref_generation,
1979 0, 0, 0); 1980 0, 0, 0);
1980 return ERR_PTR(-ENOMEM); 1981 return ERR_PTR(-ENOMEM);
1981 } 1982 }
1982 btrfs_set_header_generation(buf, trans->transid); 1983 btrfs_set_header_generation(buf, trans->transid);
1983 clean_tree_block(trans, root, buf); 1984 clean_tree_block(trans, root, buf);
1984 btrfs_set_buffer_uptodate(buf); 1985 btrfs_set_buffer_uptodate(buf);
1985 1986
1986 if (PageDirty(buf->first_page)) { 1987 if (PageDirty(buf->first_page)) {
1987 printk("page %lu dirty\n", buf->first_page->index); 1988 printk("page %lu dirty\n", buf->first_page->index);
1988 WARN_ON(1); 1989 WARN_ON(1);
1989 } 1990 }
1990 1991
1991 set_extent_dirty(&trans->transaction->dirty_pages, buf->start, 1992 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1992 buf->start + buf->len - 1, GFP_NOFS); 1993 buf->start + buf->len - 1, GFP_NOFS);
1993 if (!btrfs_test_opt(root, SSD)) 1994 if (!btrfs_test_opt(root, SSD))
1994 btrfs_set_buffer_defrag(buf); 1995 btrfs_set_buffer_defrag(buf);
1995 trans->blocks_used++; 1996 trans->blocks_used++;
1996 return buf; 1997 return buf;
1997 } 1998 }
1998 1999
1999 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans, 2000 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2000 struct btrfs_root *root, 2001 struct btrfs_root *root,
2001 struct extent_buffer *leaf) 2002 struct extent_buffer *leaf)
2002 { 2003 {
2003 u64 leaf_owner; 2004 u64 leaf_owner;
2004 u64 leaf_generation; 2005 u64 leaf_generation;
2005 struct btrfs_key key; 2006 struct btrfs_key key;
2006 struct btrfs_file_extent_item *fi; 2007 struct btrfs_file_extent_item *fi;
2007 int i; 2008 int i;
2008 int nritems; 2009 int nritems;
2009 int ret; 2010 int ret;
2010 2011
2011 BUG_ON(!btrfs_is_leaf(leaf)); 2012 BUG_ON(!btrfs_is_leaf(leaf));
2012 nritems = btrfs_header_nritems(leaf); 2013 nritems = btrfs_header_nritems(leaf);
2013 leaf_owner = btrfs_header_owner(leaf); 2014 leaf_owner = btrfs_header_owner(leaf);
2014 leaf_generation = btrfs_header_generation(leaf); 2015 leaf_generation = btrfs_header_generation(leaf);
2015 2016
2016 for (i = 0; i < nritems; i++) { 2017 for (i = 0; i < nritems; i++) {
2017 u64 disk_bytenr; 2018 u64 disk_bytenr;
2018 2019
2019 btrfs_item_key_to_cpu(leaf, &key, i); 2020 btrfs_item_key_to_cpu(leaf, &key, i);
2020 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) 2021 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2021 continue; 2022 continue;
2022 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); 2023 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2023 if (btrfs_file_extent_type(leaf, fi) == 2024 if (btrfs_file_extent_type(leaf, fi) ==
2024 BTRFS_FILE_EXTENT_INLINE) 2025 BTRFS_FILE_EXTENT_INLINE)
2025 continue; 2026 continue;
2026 /* 2027 /*
2027 * FIXME make sure to insert a trans record that 2028 * FIXME make sure to insert a trans record that
2028 * repeats the snapshot del on crash 2029 * repeats the snapshot del on crash
2029 */ 2030 */
2030 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); 2031 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2031 if (disk_bytenr == 0) 2032 if (disk_bytenr == 0)
2032 continue; 2033 continue;
2033 ret = btrfs_free_extent(trans, root, disk_bytenr, 2034 ret = btrfs_free_extent(trans, root, disk_bytenr,
2034 btrfs_file_extent_disk_num_bytes(leaf, fi), 2035 btrfs_file_extent_disk_num_bytes(leaf, fi),
2035 leaf_owner, leaf_generation, 2036 leaf_owner, leaf_generation,
2036 key.objectid, key.offset, 0); 2037 key.objectid, key.offset, 0);
2037 BUG_ON(ret); 2038 BUG_ON(ret);
2038 } 2039 }
2039 return 0; 2040 return 0;
2040 } 2041 }
2041 2042
2042 static void noinline reada_walk_down(struct btrfs_root *root, 2043 static void noinline reada_walk_down(struct btrfs_root *root,
2043 struct extent_buffer *node, 2044 struct extent_buffer *node,
2044 int slot) 2045 int slot)
2045 { 2046 {
2046 u64 bytenr; 2047 u64 bytenr;
2047 u64 last = 0; 2048 u64 last = 0;
2048 u32 nritems; 2049 u32 nritems;
2049 u32 refs; 2050 u32 refs;
2050 u32 blocksize; 2051 u32 blocksize;
2051 int ret; 2052 int ret;
2052 int i; 2053 int i;
2053 int level; 2054 int level;
2054 int skipped = 0; 2055 int skipped = 0;
2055 2056
2056 nritems = btrfs_header_nritems(node); 2057 nritems = btrfs_header_nritems(node);
2057 level = btrfs_header_level(node); 2058 level = btrfs_header_level(node);
2058 if (level) 2059 if (level)
2059 return; 2060 return;
2060 2061
2061 for (i = slot; i < nritems && skipped < 32; i++) { 2062 for (i = slot; i < nritems && skipped < 32; i++) {
2062 bytenr = btrfs_node_blockptr(node, i); 2063 bytenr = btrfs_node_blockptr(node, i);
2063 if (last && ((bytenr > last && bytenr - last > 32 * 1024) || 2064 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2064 (last > bytenr && last - bytenr > 32 * 1024))) { 2065 (last > bytenr && last - bytenr > 32 * 1024))) {
2065 skipped++; 2066 skipped++;
2066 continue; 2067 continue;
2067 } 2068 }
2068 blocksize = btrfs_level_size(root, level - 1); 2069 blocksize = btrfs_level_size(root, level - 1);
2069 if (i != slot) { 2070 if (i != slot) {
2070 ret = lookup_extent_ref(NULL, root, bytenr, 2071 ret = lookup_extent_ref(NULL, root, bytenr,
2071 blocksize, &refs); 2072 blocksize, &refs);
2072 BUG_ON(ret); 2073 BUG_ON(ret);
2073 if (refs != 1) { 2074 if (refs != 1) {
2074 skipped++; 2075 skipped++;
2075 continue; 2076 continue;
2076 } 2077 }
2077 } 2078 }
2078 mutex_unlock(&root->fs_info->fs_mutex); 2079 mutex_unlock(&root->fs_info->fs_mutex);
2079 ret = readahead_tree_block(root, bytenr, blocksize); 2080 ret = readahead_tree_block(root, bytenr, blocksize,
2081 btrfs_node_ptr_generation(node, i));
2080 last = bytenr + blocksize; 2082 last = bytenr + blocksize;
2081 cond_resched(); 2083 cond_resched();
2082 mutex_lock(&root->fs_info->fs_mutex); 2084 mutex_lock(&root->fs_info->fs_mutex);
2083 if (ret) 2085 if (ret)
2084 break; 2086 break;
2085 } 2087 }
2086 } 2088 }
2087 2089
2088 /* 2090 /*
2089 * helper function for drop_snapshot, this walks down the tree dropping ref 2091 * helper function for drop_snapshot, this walks down the tree dropping ref
2090 * counts as it goes. 2092 * counts as it goes.
2091 */ 2093 */
2092 static int noinline walk_down_tree(struct btrfs_trans_handle *trans, 2094 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2093 struct btrfs_root *root, 2095 struct btrfs_root *root,
2094 struct btrfs_path *path, int *level) 2096 struct btrfs_path *path, int *level)
2095 { 2097 {
2096 u64 root_owner; 2098 u64 root_owner;
2097 u64 root_gen; 2099 u64 root_gen;
2098 u64 bytenr; 2100 u64 bytenr;
2101 u64 ptr_gen;
2099 struct extent_buffer *next; 2102 struct extent_buffer *next;
2100 struct extent_buffer *cur; 2103 struct extent_buffer *cur;
2101 struct extent_buffer *parent; 2104 struct extent_buffer *parent;
2102 u32 blocksize; 2105 u32 blocksize;
2103 int ret; 2106 int ret;
2104 u32 refs; 2107 u32 refs;
2105 2108
2106 WARN_ON(*level < 0); 2109 WARN_ON(*level < 0);
2107 WARN_ON(*level >= BTRFS_MAX_LEVEL); 2110 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2108 ret = lookup_extent_ref(trans, root, 2111 ret = lookup_extent_ref(trans, root,
2109 path->nodes[*level]->start, 2112 path->nodes[*level]->start,
2110 path->nodes[*level]->len, &refs); 2113 path->nodes[*level]->len, &refs);
2111 BUG_ON(ret); 2114 BUG_ON(ret);
2112 if (refs > 1) 2115 if (refs > 1)
2113 goto out; 2116 goto out;
2114 2117
2115 /* 2118 /*
2116 * walk down to the last node level and free all the leaves 2119 * walk down to the last node level and free all the leaves
2117 */ 2120 */
2118 while(*level >= 0) { 2121 while(*level >= 0) {
2119 WARN_ON(*level < 0); 2122 WARN_ON(*level < 0);
2120 WARN_ON(*level >= BTRFS_MAX_LEVEL); 2123 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2121 cur = path->nodes[*level]; 2124 cur = path->nodes[*level];
2122 2125
2123 if (btrfs_header_level(cur) != *level) 2126 if (btrfs_header_level(cur) != *level)
2124 WARN_ON(1); 2127 WARN_ON(1);
2125 2128
2126 if (path->slots[*level] >= 2129 if (path->slots[*level] >=
2127 btrfs_header_nritems(cur)) 2130 btrfs_header_nritems(cur))
2128 break; 2131 break;
2129 if (*level == 0) { 2132 if (*level == 0) {
2130 ret = drop_leaf_ref(trans, root, cur); 2133 ret = drop_leaf_ref(trans, root, cur);
2131 BUG_ON(ret); 2134 BUG_ON(ret);
2132 break; 2135 break;
2133 } 2136 }
2134 bytenr = btrfs_node_blockptr(cur, path->slots[*level]); 2137 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2138 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2135 blocksize = btrfs_level_size(root, *level - 1); 2139 blocksize = btrfs_level_size(root, *level - 1);
2136 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs); 2140 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
2137 BUG_ON(ret); 2141 BUG_ON(ret);
2138 if (refs != 1) { 2142 if (refs != 1) {
2139 parent = path->nodes[*level]; 2143 parent = path->nodes[*level];
2140 root_owner = btrfs_header_owner(parent); 2144 root_owner = btrfs_header_owner(parent);
2141 root_gen = btrfs_header_generation(parent); 2145 root_gen = btrfs_header_generation(parent);
2142 path->slots[*level]++; 2146 path->slots[*level]++;
2143 ret = btrfs_free_extent(trans, root, bytenr, 2147 ret = btrfs_free_extent(trans, root, bytenr,
2144 blocksize, root_owner, 2148 blocksize, root_owner,
2145 root_gen, 0, 0, 1); 2149 root_gen, 0, 0, 1);
2146 BUG_ON(ret); 2150 BUG_ON(ret);
2147 continue; 2151 continue;
2148 } 2152 }
2149 next = btrfs_find_tree_block(root, bytenr, blocksize); 2153 next = btrfs_find_tree_block(root, bytenr, blocksize);
2150 if (!next || !btrfs_buffer_uptodate(next)) { 2154 if (!next || !btrfs_buffer_uptodate(next)) {
2151 free_extent_buffer(next); 2155 free_extent_buffer(next);
2152 reada_walk_down(root, cur, path->slots[*level]); 2156 reada_walk_down(root, cur, path->slots[*level]);
2153 2157
2154 mutex_unlock(&root->fs_info->fs_mutex); 2158 mutex_unlock(&root->fs_info->fs_mutex);
2155 next = read_tree_block(root, bytenr, blocksize); 2159 next = read_tree_block(root, bytenr, blocksize,
2160 ptr_gen);
2156 mutex_lock(&root->fs_info->fs_mutex); 2161 mutex_lock(&root->fs_info->fs_mutex);
2157 2162
2158 /* we've dropped the lock, double check */ 2163 /* we've dropped the lock, double check */
2159 ret = lookup_extent_ref(trans, root, bytenr, 2164 ret = lookup_extent_ref(trans, root, bytenr,
2160 blocksize, &refs); 2165 blocksize, &refs);
2161 BUG_ON(ret); 2166 BUG_ON(ret);
2162 if (refs != 1) { 2167 if (refs != 1) {
2163 parent = path->nodes[*level]; 2168 parent = path->nodes[*level];
2164 root_owner = btrfs_header_owner(parent); 2169 root_owner = btrfs_header_owner(parent);
2165 root_gen = btrfs_header_generation(parent); 2170 root_gen = btrfs_header_generation(parent);
2166 2171
2167 path->slots[*level]++; 2172 path->slots[*level]++;
2168 free_extent_buffer(next); 2173 free_extent_buffer(next);
2169 ret = btrfs_free_extent(trans, root, bytenr, 2174 ret = btrfs_free_extent(trans, root, bytenr,
2170 blocksize, 2175 blocksize,
2171 root_owner, 2176 root_owner,
2172 root_gen, 0, 0, 1); 2177 root_gen, 0, 0, 1);
2173 BUG_ON(ret); 2178 BUG_ON(ret);
2174 continue; 2179 continue;
2175 } 2180 }
2176 } else if (next) {
2177 btrfs_verify_block_csum(root, next);
2178 } 2181 }
2179 WARN_ON(*level <= 0); 2182 WARN_ON(*level <= 0);
2180 if (path->nodes[*level-1]) 2183 if (path->nodes[*level-1])
2181 free_extent_buffer(path->nodes[*level-1]); 2184 free_extent_buffer(path->nodes[*level-1]);
2182 path->nodes[*level-1] = next; 2185 path->nodes[*level-1] = next;
2183 *level = btrfs_header_level(next); 2186 *level = btrfs_header_level(next);
2184 path->slots[*level] = 0; 2187 path->slots[*level] = 0;
2185 } 2188 }
2186 out: 2189 out:
2187 WARN_ON(*level < 0); 2190 WARN_ON(*level < 0);
2188 WARN_ON(*level >= BTRFS_MAX_LEVEL); 2191 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2189 2192
2190 if (path->nodes[*level] == root->node) { 2193 if (path->nodes[*level] == root->node) {
2191 root_owner = root->root_key.objectid; 2194 root_owner = root->root_key.objectid;
2192 parent = path->nodes[*level]; 2195 parent = path->nodes[*level];
2193 } else { 2196 } else {
2194 parent = path->nodes[*level + 1]; 2197 parent = path->nodes[*level + 1];
2195 root_owner = btrfs_header_owner(parent); 2198 root_owner = btrfs_header_owner(parent);
2196 } 2199 }
2197 2200
2198 root_gen = btrfs_header_generation(parent); 2201 root_gen = btrfs_header_generation(parent);
2199 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start, 2202 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
2200 path->nodes[*level]->len, 2203 path->nodes[*level]->len,
2201 root_owner, root_gen, 0, 0, 1); 2204 root_owner, root_gen, 0, 0, 1);
2202 free_extent_buffer(path->nodes[*level]); 2205 free_extent_buffer(path->nodes[*level]);
2203 path->nodes[*level] = NULL; 2206 path->nodes[*level] = NULL;
2204 *level += 1; 2207 *level += 1;
2205 BUG_ON(ret); 2208 BUG_ON(ret);
2206 return 0; 2209 return 0;
2207 } 2210 }
2208 2211
2209 /* 2212 /*
2210 * helper for dropping snapshots. This walks back up the tree in the path 2213 * helper for dropping snapshots. This walks back up the tree in the path
2211 * to find the first node higher up where we haven't yet gone through 2214 * to find the first node higher up where we haven't yet gone through
2212 * all the slots 2215 * all the slots
2213 */ 2216 */
2214 static int noinline walk_up_tree(struct btrfs_trans_handle *trans, 2217 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2215 struct btrfs_root *root, 2218 struct btrfs_root *root,
2216 struct btrfs_path *path, int *level) 2219 struct btrfs_path *path, int *level)
2217 { 2220 {
2218 u64 root_owner; 2221 u64 root_owner;
2219 u64 root_gen; 2222 u64 root_gen;
2220 struct btrfs_root_item *root_item = &root->root_item; 2223 struct btrfs_root_item *root_item = &root->root_item;
2221 int i; 2224 int i;
2222 int slot; 2225 int slot;
2223 int ret; 2226 int ret;
2224 2227
2225 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { 2228 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2226 slot = path->slots[i]; 2229 slot = path->slots[i];
2227 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { 2230 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2228 struct extent_buffer *node; 2231 struct extent_buffer *node;
2229 struct btrfs_disk_key disk_key; 2232 struct btrfs_disk_key disk_key;
2230 node = path->nodes[i]; 2233 node = path->nodes[i];
2231 path->slots[i]++; 2234 path->slots[i]++;
2232 *level = i; 2235 *level = i;
2233 WARN_ON(*level == 0); 2236 WARN_ON(*level == 0);
2234 btrfs_node_key(node, &disk_key, path->slots[i]); 2237 btrfs_node_key(node, &disk_key, path->slots[i]);
2235 memcpy(&root_item->drop_progress, 2238 memcpy(&root_item->drop_progress,
2236 &disk_key, sizeof(disk_key)); 2239 &disk_key, sizeof(disk_key));
2237 root_item->drop_level = i; 2240 root_item->drop_level = i;
2238 return 0; 2241 return 0;
2239 } else { 2242 } else {
2240 if (path->nodes[*level] == root->node) { 2243 if (path->nodes[*level] == root->node) {
2241 root_owner = root->root_key.objectid; 2244 root_owner = root->root_key.objectid;
2242 root_gen = 2245 root_gen =
2243 btrfs_header_generation(path->nodes[*level]); 2246 btrfs_header_generation(path->nodes[*level]);
2244 } else { 2247 } else {
2245 struct extent_buffer *node; 2248 struct extent_buffer *node;
2246 node = path->nodes[*level + 1]; 2249 node = path->nodes[*level + 1];
2247 root_owner = btrfs_header_owner(node); 2250 root_owner = btrfs_header_owner(node);
2248 root_gen = btrfs_header_generation(node); 2251 root_gen = btrfs_header_generation(node);
2249 } 2252 }
2250 ret = btrfs_free_extent(trans, root, 2253 ret = btrfs_free_extent(trans, root,
2251 path->nodes[*level]->start, 2254 path->nodes[*level]->start,
2252 path->nodes[*level]->len, 2255 path->nodes[*level]->len,
2253 root_owner, root_gen, 0, 0, 1); 2256 root_owner, root_gen, 0, 0, 1);
2254 BUG_ON(ret); 2257 BUG_ON(ret);
2255 free_extent_buffer(path->nodes[*level]); 2258 free_extent_buffer(path->nodes[*level]);
2256 path->nodes[*level] = NULL; 2259 path->nodes[*level] = NULL;
2257 *level = i + 1; 2260 *level = i + 1;
2258 } 2261 }
2259 } 2262 }
2260 return 1; 2263 return 1;
2261 } 2264 }
2262 2265
2263 /* 2266 /*
2264 * drop the reference count on the tree rooted at 'snap'. This traverses 2267 * drop the reference count on the tree rooted at 'snap'. This traverses
2265 * the tree freeing any blocks that have a ref count of zero after being 2268 * the tree freeing any blocks that have a ref count of zero after being
2266 * decremented. 2269 * decremented.
2267 */ 2270 */
2268 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root 2271 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2269 *root) 2272 *root)
2270 { 2273 {
2271 int ret = 0; 2274 int ret = 0;
2272 int wret; 2275 int wret;
2273 int level; 2276 int level;
2274 struct btrfs_path *path; 2277 struct btrfs_path *path;
2275 int i; 2278 int i;
2276 int orig_level; 2279 int orig_level;
2277 struct btrfs_root_item *root_item = &root->root_item; 2280 struct btrfs_root_item *root_item = &root->root_item;
2278 2281
2279 path = btrfs_alloc_path(); 2282 path = btrfs_alloc_path();
2280 BUG_ON(!path); 2283 BUG_ON(!path);
2281 2284
2282 level = btrfs_header_level(root->node); 2285 level = btrfs_header_level(root->node);
2283 orig_level = level; 2286 orig_level = level;
2284 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { 2287 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2285 path->nodes[level] = root->node; 2288 path->nodes[level] = root->node;
2286 extent_buffer_get(root->node); 2289 extent_buffer_get(root->node);
2287 path->slots[level] = 0; 2290 path->slots[level] = 0;
2288 } else { 2291 } else {
2289 struct btrfs_key key; 2292 struct btrfs_key key;
2290 struct btrfs_disk_key found_key; 2293 struct btrfs_disk_key found_key;
2291 struct extent_buffer *node; 2294 struct extent_buffer *node;
2292 2295
2293 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); 2296 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2294 level = root_item->drop_level; 2297 level = root_item->drop_level;
2295 path->lowest_level = level; 2298 path->lowest_level = level;
2296 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2299 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2297 if (wret < 0) { 2300 if (wret < 0) {
2298 ret = wret; 2301 ret = wret;
2299 goto out; 2302 goto out;
2300 } 2303 }
2301 node = path->nodes[level]; 2304 node = path->nodes[level];
2302 btrfs_node_key(node, &found_key, path->slots[level]); 2305 btrfs_node_key(node, &found_key, path->slots[level]);
2303 WARN_ON(memcmp(&found_key, &root_item->drop_progress, 2306 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2304 sizeof(found_key))); 2307 sizeof(found_key)));
2305 } 2308 }
2306 while(1) { 2309 while(1) {
2307 wret = walk_down_tree(trans, root, path, &level); 2310 wret = walk_down_tree(trans, root, path, &level);
2308 if (wret > 0) 2311 if (wret > 0)
2309 break; 2312 break;
2310 if (wret < 0) 2313 if (wret < 0)
2311 ret = wret; 2314 ret = wret;
2312 2315
2313 wret = walk_up_tree(trans, root, path, &level); 2316 wret = walk_up_tree(trans, root, path, &level);
2314 if (wret > 0) 2317 if (wret > 0)
2315 break; 2318 break;
2316 if (wret < 0) 2319 if (wret < 0)
2317 ret = wret; 2320 ret = wret;
2318 ret = -EAGAIN; 2321 ret = -EAGAIN;
2319 break; 2322 break;
2320 } 2323 }
2321 for (i = 0; i <= orig_level; i++) { 2324 for (i = 0; i <= orig_level; i++) {
2322 if (path->nodes[i]) { 2325 if (path->nodes[i]) {
2323 free_extent_buffer(path->nodes[i]); 2326 free_extent_buffer(path->nodes[i]);
2324 path->nodes[i] = NULL; 2327 path->nodes[i] = NULL;
2325 } 2328 }
2326 } 2329 }
2327 out: 2330 out:
2328 btrfs_free_path(path); 2331 btrfs_free_path(path);
2329 return ret; 2332 return ret;
2330 } 2333 }
2331 2334
2332 int btrfs_free_block_groups(struct btrfs_fs_info *info) 2335 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2333 { 2336 {
2334 u64 start; 2337 u64 start;
2335 u64 end; 2338 u64 end;
2336 u64 ptr; 2339 u64 ptr;
2337 int ret; 2340 int ret;
2338 while(1) { 2341 while(1) {
2339 ret = find_first_extent_bit(&info->block_group_cache, 0, 2342 ret = find_first_extent_bit(&info->block_group_cache, 0,
2340 &start, &end, (unsigned int)-1); 2343 &start, &end, (unsigned int)-1);
2341 if (ret) 2344 if (ret)
2342 break; 2345 break;
2343 ret = get_state_private(&info->block_group_cache, start, &ptr); 2346 ret = get_state_private(&info->block_group_cache, start, &ptr);
2344 if (!ret) 2347 if (!ret)
2345 kfree((void *)(unsigned long)ptr); 2348 kfree((void *)(unsigned long)ptr);
2346 clear_extent_bits(&info->block_group_cache, start, 2349 clear_extent_bits(&info->block_group_cache, start,
2347 end, (unsigned int)-1, GFP_NOFS); 2350 end, (unsigned int)-1, GFP_NOFS);
2348 } 2351 }
2349 while(1) { 2352 while(1) {
2350 ret = find_first_extent_bit(&info->free_space_cache, 0, 2353 ret = find_first_extent_bit(&info->free_space_cache, 0,
2351 &start, &end, EXTENT_DIRTY); 2354 &start, &end, EXTENT_DIRTY);
2352 if (ret) 2355 if (ret)
2353 break; 2356 break;
2354 clear_extent_dirty(&info->free_space_cache, start, 2357 clear_extent_dirty(&info->free_space_cache, start,
2355 end, GFP_NOFS); 2358 end, GFP_NOFS);
2356 } 2359 }
2357 return 0; 2360 return 0;
2358 } 2361 }
2359 2362
2360 static unsigned long calc_ra(unsigned long start, unsigned long last, 2363 static unsigned long calc_ra(unsigned long start, unsigned long last,
2361 unsigned long nr) 2364 unsigned long nr)
2362 { 2365 {
2363 return min(last, start + nr - 1); 2366 return min(last, start + nr - 1);
2364 } 2367 }
2365 2368
2366 static int noinline relocate_inode_pages(struct inode *inode, u64 start, 2369 static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2367 u64 len) 2370 u64 len)
2368 { 2371 {
2369 u64 page_start; 2372 u64 page_start;
2370 u64 page_end; 2373 u64 page_end;
2371 unsigned long last_index; 2374 unsigned long last_index;
2372 unsigned long i; 2375 unsigned long i;
2373 struct page *page; 2376 struct page *page;
2374 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 2377 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2375 struct file_ra_state *ra; 2378 struct file_ra_state *ra;
2376 unsigned long total_read = 0; 2379 unsigned long total_read = 0;
2377 unsigned long ra_pages; 2380 unsigned long ra_pages;
2378 struct btrfs_trans_handle *trans; 2381 struct btrfs_trans_handle *trans;
2379 2382
2380 ra = kzalloc(sizeof(*ra), GFP_NOFS); 2383 ra = kzalloc(sizeof(*ra), GFP_NOFS);
2381 2384
2382 mutex_lock(&inode->i_mutex); 2385 mutex_lock(&inode->i_mutex);
2383 i = start >> PAGE_CACHE_SHIFT; 2386 i = start >> PAGE_CACHE_SHIFT;
2384 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; 2387 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2385 2388
2386 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages; 2389 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
2387 2390
2388 file_ra_state_init(ra, inode->i_mapping); 2391 file_ra_state_init(ra, inode->i_mapping);
2389 2392
2390 for (; i <= last_index; i++) { 2393 for (; i <= last_index; i++) {
2391 if (total_read % ra_pages == 0) { 2394 if (total_read % ra_pages == 0) {
2392 btrfs_force_ra(inode->i_mapping, ra, NULL, i, 2395 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
2393 calc_ra(i, last_index, ra_pages)); 2396 calc_ra(i, last_index, ra_pages));
2394 } 2397 }
2395 total_read++; 2398 total_read++;
2396 if (((u64)i << PAGE_CACHE_SHIFT) > inode->i_size) 2399 if (((u64)i << PAGE_CACHE_SHIFT) > inode->i_size)
2397 goto truncate_racing; 2400 goto truncate_racing;
2398 2401
2399 page = grab_cache_page(inode->i_mapping, i); 2402 page = grab_cache_page(inode->i_mapping, i);
2400 if (!page) { 2403 if (!page) {
2401 goto out_unlock; 2404 goto out_unlock;
2402 } 2405 }
2403 if (!PageUptodate(page)) { 2406 if (!PageUptodate(page)) {
2404 btrfs_readpage(NULL, page); 2407 btrfs_readpage(NULL, page);
2405 lock_page(page); 2408 lock_page(page);
2406 if (!PageUptodate(page)) { 2409 if (!PageUptodate(page)) {
2407 unlock_page(page); 2410 unlock_page(page);
2408 page_cache_release(page); 2411 page_cache_release(page);
2409 goto out_unlock; 2412 goto out_unlock;
2410 } 2413 }
2411 } 2414 }
2412 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) 2415 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2413 ClearPageDirty(page); 2416 ClearPageDirty(page);
2414 #else 2417 #else
2415 cancel_dirty_page(page, PAGE_CACHE_SIZE); 2418 cancel_dirty_page(page, PAGE_CACHE_SIZE);
2416 #endif 2419 #endif
2417 wait_on_page_writeback(page); 2420 wait_on_page_writeback(page);
2418 set_page_extent_mapped(page); 2421 set_page_extent_mapped(page);
2419 page_start = (u64)page->index << PAGE_CACHE_SHIFT; 2422 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2420 page_end = page_start + PAGE_CACHE_SIZE - 1; 2423 page_end = page_start + PAGE_CACHE_SIZE - 1;
2421 2424
2422 lock_extent(io_tree, page_start, page_end, GFP_NOFS); 2425 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2423 2426
2424 set_extent_delalloc(io_tree, page_start, 2427 set_extent_delalloc(io_tree, page_start,
2425 page_end, GFP_NOFS); 2428 page_end, GFP_NOFS);
2426 set_page_dirty(page); 2429 set_page_dirty(page);
2427 2430
2428 unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 2431 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2429 unlock_page(page); 2432 unlock_page(page);
2430 page_cache_release(page); 2433 page_cache_release(page);
2431 } 2434 }
2432 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 2435 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2433 total_read); 2436 total_read);
2434 2437
2435 out_unlock: 2438 out_unlock:
2436 kfree(ra); 2439 kfree(ra);
2437 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1); 2440 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
2438 if (trans) { 2441 if (trans) {
2439 btrfs_add_ordered_inode(inode); 2442 btrfs_add_ordered_inode(inode);
2440 btrfs_end_transaction(trans, BTRFS_I(inode)->root); 2443 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
2441 mark_inode_dirty(inode); 2444 mark_inode_dirty(inode);
2442 } 2445 }
2443 mutex_unlock(&inode->i_mutex); 2446 mutex_unlock(&inode->i_mutex);
2444 return 0; 2447 return 0;
2445 2448
2446 truncate_racing: 2449 truncate_racing:
2447 vmtruncate(inode, inode->i_size); 2450 vmtruncate(inode, inode->i_size);
2448 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 2451 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2449 total_read); 2452 total_read);
2450 goto out_unlock; 2453 goto out_unlock;
2451 } 2454 }
2452 2455
2453 /* 2456 /*
2454 * The back references tell us which tree holds a ref on a block, 2457 * The back references tell us which tree holds a ref on a block,
2455 * but it is possible for the tree root field in the reference to 2458 * but it is possible for the tree root field in the reference to
2456 * reflect the original root before a snapshot was made. In this 2459 * reflect the original root before a snapshot was made. In this
2457 * case we should search through all the children of a given root 2460 * case we should search through all the children of a given root
2458 * to find potential holders of references on a block. 2461 * to find potential holders of references on a block.
2459 * 2462 *
2460 * Instead, we do something a little less fancy and just search 2463 * Instead, we do something a little less fancy and just search
2461 * all the roots for a given key/block combination. 2464 * all the roots for a given key/block combination.
2462 */ 2465 */
2463 static int find_root_for_ref(struct btrfs_root *root, 2466 static int find_root_for_ref(struct btrfs_root *root,
2464 struct btrfs_path *path, 2467 struct btrfs_path *path,
2465 struct btrfs_key *key0, 2468 struct btrfs_key *key0,
2466 int level, 2469 int level,
2467 int file_key, 2470 int file_key,
2468 struct btrfs_root **found_root, 2471 struct btrfs_root **found_root,
2469 u64 bytenr) 2472 u64 bytenr)
2470 { 2473 {
2471 struct btrfs_key root_location; 2474 struct btrfs_key root_location;
2472 struct btrfs_root *cur_root = *found_root; 2475 struct btrfs_root *cur_root = *found_root;
2473 struct btrfs_file_extent_item *file_extent; 2476 struct btrfs_file_extent_item *file_extent;
2474 u64 root_search_start = BTRFS_FS_TREE_OBJECTID; 2477 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
2475 u64 found_bytenr; 2478 u64 found_bytenr;
2476 int ret; 2479 int ret;
2477 int i; 2480 int i;
2478 2481
2479 root_location.offset = (u64)-1; 2482 root_location.offset = (u64)-1;
2480 root_location.type = BTRFS_ROOT_ITEM_KEY; 2483 root_location.type = BTRFS_ROOT_ITEM_KEY;
2481 path->lowest_level = level; 2484 path->lowest_level = level;
2482 path->reada = 0; 2485 path->reada = 0;
2483 while(1) { 2486 while(1) {
2484 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0); 2487 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
2485 found_bytenr = 0; 2488 found_bytenr = 0;
2486 if (ret == 0 && file_key) { 2489 if (ret == 0 && file_key) {
2487 struct extent_buffer *leaf = path->nodes[0]; 2490 struct extent_buffer *leaf = path->nodes[0];
2488 file_extent = btrfs_item_ptr(leaf, path->slots[0], 2491 file_extent = btrfs_item_ptr(leaf, path->slots[0],
2489 struct btrfs_file_extent_item); 2492 struct btrfs_file_extent_item);
2490 if (btrfs_file_extent_type(leaf, file_extent) == 2493 if (btrfs_file_extent_type(leaf, file_extent) ==
2491 BTRFS_FILE_EXTENT_REG) { 2494 BTRFS_FILE_EXTENT_REG) {
2492 found_bytenr = 2495 found_bytenr =
2493 btrfs_file_extent_disk_bytenr(leaf, 2496 btrfs_file_extent_disk_bytenr(leaf,
2494 file_extent); 2497 file_extent);
2495 } 2498 }
2496 } else if (!file_key) { 2499 } else if (!file_key) {
2497 if (path->nodes[level]) 2500 if (path->nodes[level])
2498 found_bytenr = path->nodes[level]->start; 2501 found_bytenr = path->nodes[level]->start;
2499 } 2502 }
2500 2503
2501 for (i = level; i < BTRFS_MAX_LEVEL; i++) { 2504 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2502 if (!path->nodes[i]) 2505 if (!path->nodes[i])
2503 break; 2506 break;
2504 free_extent_buffer(path->nodes[i]); 2507 free_extent_buffer(path->nodes[i]);
2505 path->nodes[i] = NULL; 2508 path->nodes[i] = NULL;
2506 } 2509 }
2507 btrfs_release_path(cur_root, path); 2510 btrfs_release_path(cur_root, path);
2508 2511
2509 if (found_bytenr == bytenr) { 2512 if (found_bytenr == bytenr) {
2510 *found_root = cur_root; 2513 *found_root = cur_root;
2511 ret = 0; 2514 ret = 0;
2512 goto out; 2515 goto out;
2513 } 2516 }
2514 ret = btrfs_search_root(root->fs_info->tree_root, 2517 ret = btrfs_search_root(root->fs_info->tree_root,
2515 root_search_start, &root_search_start); 2518 root_search_start, &root_search_start);
2516 if (ret) 2519 if (ret)
2517 break; 2520 break;
2518 2521
2519 root_location.objectid = root_search_start; 2522 root_location.objectid = root_search_start;
2520 cur_root = btrfs_read_fs_root_no_name(root->fs_info, 2523 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
2521 &root_location); 2524 &root_location);
2522 if (!cur_root) { 2525 if (!cur_root) {
2523 ret = 1; 2526 ret = 1;
2524 break; 2527 break;
2525 } 2528 }
2526 } 2529 }
2527 out: 2530 out:
2528 path->lowest_level = 0; 2531 path->lowest_level = 0;
2529 return ret; 2532 return ret;
2530 } 2533 }
2531 2534
2532 /* 2535 /*
2533 * note, this releases the path 2536 * note, this releases the path
2534 */ 2537 */
2535 static int noinline relocate_one_reference(struct btrfs_root *extent_root, 2538 static int noinline relocate_one_reference(struct btrfs_root *extent_root,
2536 struct btrfs_path *path, 2539 struct btrfs_path *path,
2537 struct btrfs_key *extent_key) 2540 struct btrfs_key *extent_key)
2538 { 2541 {
2539 struct inode *inode; 2542 struct inode *inode;
2540 struct btrfs_root *found_root; 2543 struct btrfs_root *found_root;
2541 struct btrfs_key root_location; 2544 struct btrfs_key root_location;
2542 struct btrfs_key found_key; 2545 struct btrfs_key found_key;
2543 struct btrfs_extent_ref *ref; 2546 struct btrfs_extent_ref *ref;
2544 u64 ref_root; 2547 u64 ref_root;
2545 u64 ref_gen; 2548 u64 ref_gen;
2546 u64 ref_objectid; 2549 u64 ref_objectid;
2547 u64 ref_offset; 2550 u64 ref_offset;
2548 int ret; 2551 int ret;
2549 int level; 2552 int level;
2550 2553
2551 ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 2554 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2552 struct btrfs_extent_ref); 2555 struct btrfs_extent_ref);
2553 ref_root = btrfs_ref_root(path->nodes[0], ref); 2556 ref_root = btrfs_ref_root(path->nodes[0], ref);
2554 ref_gen = btrfs_ref_generation(path->nodes[0], ref); 2557 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2555 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref); 2558 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2556 ref_offset = btrfs_ref_offset(path->nodes[0], ref); 2559 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2557 btrfs_release_path(extent_root, path); 2560 btrfs_release_path(extent_root, path);
2558 2561
2559 root_location.objectid = ref_root; 2562 root_location.objectid = ref_root;
2560 if (ref_gen == 0) 2563 if (ref_gen == 0)
2561 root_location.offset = 0; 2564 root_location.offset = 0;
2562 else 2565 else
2563 root_location.offset = (u64)-1; 2566 root_location.offset = (u64)-1;
2564 root_location.type = BTRFS_ROOT_ITEM_KEY; 2567 root_location.type = BTRFS_ROOT_ITEM_KEY;
2565 2568
2566 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info, 2569 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
2567 &root_location); 2570 &root_location);
2568 BUG_ON(!found_root); 2571 BUG_ON(!found_root);
2569 2572
2570 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) { 2573 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2571 found_key.objectid = ref_objectid; 2574 found_key.objectid = ref_objectid;
2572 found_key.type = BTRFS_EXTENT_DATA_KEY; 2575 found_key.type = BTRFS_EXTENT_DATA_KEY;
2573 found_key.offset = ref_offset; 2576 found_key.offset = ref_offset;
2574 level = 0; 2577 level = 0;
2575 2578
2576 ret = find_root_for_ref(extent_root, path, &found_key, 2579 ret = find_root_for_ref(extent_root, path, &found_key,
2577 level, 1, &found_root, 2580 level, 1, &found_root,
2578 extent_key->objectid); 2581 extent_key->objectid);
2579 2582
2580 if (ret) 2583 if (ret)
2581 goto out; 2584 goto out;
2582 2585
2583 mutex_unlock(&extent_root->fs_info->fs_mutex); 2586 mutex_unlock(&extent_root->fs_info->fs_mutex);
2584 inode = btrfs_iget_locked(extent_root->fs_info->sb, 2587 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2585 ref_objectid, found_root); 2588 ref_objectid, found_root);
2586 if (inode->i_state & I_NEW) { 2589 if (inode->i_state & I_NEW) {
2587 /* the inode and parent dir are two different roots */ 2590 /* the inode and parent dir are two different roots */
2588 BTRFS_I(inode)->root = found_root; 2591 BTRFS_I(inode)->root = found_root;
2589 BTRFS_I(inode)->location.objectid = ref_objectid; 2592 BTRFS_I(inode)->location.objectid = ref_objectid;
2590 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; 2593 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2591 BTRFS_I(inode)->location.offset = 0; 2594 BTRFS_I(inode)->location.offset = 0;
2592 btrfs_read_locked_inode(inode); 2595 btrfs_read_locked_inode(inode);
2593 unlock_new_inode(inode); 2596 unlock_new_inode(inode);
2594 2597
2595 } 2598 }
2596 /* this can happen if the reference is not against 2599 /* this can happen if the reference is not against
2597 * the latest version of the tree root 2600 * the latest version of the tree root
2598 */ 2601 */
2599 if (is_bad_inode(inode)) { 2602 if (is_bad_inode(inode)) {
2600 mutex_lock(&extent_root->fs_info->fs_mutex); 2603 mutex_lock(&extent_root->fs_info->fs_mutex);
2601 goto out; 2604 goto out;
2602 } 2605 }
2603 relocate_inode_pages(inode, ref_offset, extent_key->offset); 2606 relocate_inode_pages(inode, ref_offset, extent_key->offset);
2604 iput(inode); 2607 iput(inode);
2605 mutex_lock(&extent_root->fs_info->fs_mutex); 2608 mutex_lock(&extent_root->fs_info->fs_mutex);
2606 } else { 2609 } else {
2607 struct btrfs_trans_handle *trans; 2610 struct btrfs_trans_handle *trans;
2608 struct extent_buffer *eb; 2611 struct extent_buffer *eb;
2609 int i; 2612 int i;
2610 2613
2611 eb = read_tree_block(found_root, extent_key->objectid, 2614 eb = read_tree_block(found_root, extent_key->objectid,
2612 extent_key->offset); 2615 extent_key->offset, 0);
2613 level = btrfs_header_level(eb); 2616 level = btrfs_header_level(eb);
2614 2617
2615 if (level == 0) 2618 if (level == 0)
2616 btrfs_item_key_to_cpu(eb, &found_key, 0); 2619 btrfs_item_key_to_cpu(eb, &found_key, 0);
2617 else 2620 else
2618 btrfs_node_key_to_cpu(eb, &found_key, 0); 2621 btrfs_node_key_to_cpu(eb, &found_key, 0);
2619 2622
2620 free_extent_buffer(eb); 2623 free_extent_buffer(eb);
2621 2624
2622 ret = find_root_for_ref(extent_root, path, &found_key, 2625 ret = find_root_for_ref(extent_root, path, &found_key,
2623 level, 0, &found_root, 2626 level, 0, &found_root,
2624 extent_key->objectid); 2627 extent_key->objectid);
2625 2628
2626 if (ret) 2629 if (ret)
2627 goto out; 2630 goto out;
2628 2631
2629 trans = btrfs_start_transaction(found_root, 1); 2632 trans = btrfs_start_transaction(found_root, 1);
2630 2633
2631 path->lowest_level = level; 2634 path->lowest_level = level;
2632 path->reada = 2; 2635 path->reada = 2;
2633 ret = btrfs_search_slot(trans, found_root, &found_key, path, 2636 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2634 0, 1); 2637 0, 1);
2635 path->lowest_level = 0; 2638 path->lowest_level = 0;
2636 for (i = level; i < BTRFS_MAX_LEVEL; i++) { 2639 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2637 if (!path->nodes[i]) 2640 if (!path->nodes[i])
2638 break; 2641 break;
2639 free_extent_buffer(path->nodes[i]); 2642 free_extent_buffer(path->nodes[i]);
2640 path->nodes[i] = NULL; 2643 path->nodes[i] = NULL;
2641 } 2644 }
2642 btrfs_release_path(found_root, path); 2645 btrfs_release_path(found_root, path);
2643 btrfs_end_transaction(trans, found_root); 2646 btrfs_end_transaction(trans, found_root);
2644 } 2647 }
2645 2648
2646 out: 2649 out:
2647 return 0; 2650 return 0;
2648 } 2651 }
2649 2652
2650 static int noinline del_extent_zero(struct btrfs_root *extent_root, 2653 static int noinline del_extent_zero(struct btrfs_root *extent_root,
2651 struct btrfs_path *path, 2654 struct btrfs_path *path,
2652 struct btrfs_key *extent_key) 2655 struct btrfs_key *extent_key)
2653 { 2656 {
2654 int ret; 2657 int ret;
2655 struct btrfs_trans_handle *trans; 2658 struct btrfs_trans_handle *trans;
2656 2659
2657 trans = btrfs_start_transaction(extent_root, 1); 2660 trans = btrfs_start_transaction(extent_root, 1);
2658 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); 2661 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
2659 if (ret > 0) { 2662 if (ret > 0) {
2660 ret = -EIO; 2663 ret = -EIO;
2661 goto out; 2664 goto out;
2662 } 2665 }
2663 if (ret < 0) 2666 if (ret < 0)
2664 goto out; 2667 goto out;
2665 ret = btrfs_del_item(trans, extent_root, path); 2668 ret = btrfs_del_item(trans, extent_root, path);
2666 out: 2669 out:
2667 btrfs_end_transaction(trans, extent_root); 2670 btrfs_end_transaction(trans, extent_root);
2668 return ret; 2671 return ret;
2669 } 2672 }
2670 2673
2671 static int noinline relocate_one_extent(struct btrfs_root *extent_root, 2674 static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2672 struct btrfs_path *path, 2675 struct btrfs_path *path,
2673 struct btrfs_key *extent_key) 2676 struct btrfs_key *extent_key)
2674 { 2677 {
2675 struct btrfs_key key; 2678 struct btrfs_key key;
2676 struct btrfs_key found_key; 2679 struct btrfs_key found_key;
2677 struct extent_buffer *leaf; 2680 struct extent_buffer *leaf;
2678 u32 nritems; 2681 u32 nritems;
2679 u32 item_size; 2682 u32 item_size;
2680 int ret = 0; 2683 int ret = 0;
2681 2684
2682 if (extent_key->objectid == 0) { 2685 if (extent_key->objectid == 0) {
2683 ret = del_extent_zero(extent_root, path, extent_key); 2686 ret = del_extent_zero(extent_root, path, extent_key);
2684 goto out; 2687 goto out;
2685 } 2688 }
2686 key.objectid = extent_key->objectid; 2689 key.objectid = extent_key->objectid;
2687 key.type = BTRFS_EXTENT_REF_KEY; 2690 key.type = BTRFS_EXTENT_REF_KEY;
2688 key.offset = 0; 2691 key.offset = 0;
2689 2692
2690 while(1) { 2693 while(1) {
2691 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 2694 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2692 2695
2693 if (ret < 0) 2696 if (ret < 0)
2694 goto out; 2697 goto out;
2695 2698
2696 ret = 0; 2699 ret = 0;
2697 leaf = path->nodes[0]; 2700 leaf = path->nodes[0];
2698 nritems = btrfs_header_nritems(leaf); 2701 nritems = btrfs_header_nritems(leaf);
2699 if (path->slots[0] == nritems) { 2702 if (path->slots[0] == nritems) {
2700 ret = btrfs_next_leaf(extent_root, path); 2703 ret = btrfs_next_leaf(extent_root, path);
2701 if (ret > 0) { 2704 if (ret > 0) {
2702 ret = 0; 2705 ret = 0;
2703 goto out; 2706 goto out;
2704 } 2707 }
2705 if (ret < 0) 2708 if (ret < 0)
2706 goto out; 2709 goto out;
2707 leaf = path->nodes[0]; 2710 leaf = path->nodes[0];
2708 } 2711 }
2709 2712
2710 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 2713 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2711 if (found_key.objectid != extent_key->objectid) { 2714 if (found_key.objectid != extent_key->objectid) {
2712 break; 2715 break;
2713 } 2716 }
2714 2717
2715 if (found_key.type != BTRFS_EXTENT_REF_KEY) { 2718 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
2716 break; 2719 break;
2717 } 2720 }
2718 2721
2719 key.offset = found_key.offset + 1; 2722 key.offset = found_key.offset + 1;
2720 item_size = btrfs_item_size_nr(leaf, path->slots[0]); 2723 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2721 2724
2722 ret = relocate_one_reference(extent_root, path, extent_key); 2725 ret = relocate_one_reference(extent_root, path, extent_key);
2723 if (ret) 2726 if (ret)
2724 goto out; 2727 goto out;
2725 } 2728 }
2726 ret = 0; 2729 ret = 0;
2727 out: 2730 out:
2728 btrfs_release_path(extent_root, path); 2731 btrfs_release_path(extent_root, path);
2729 return ret; 2732 return ret;
2730 } 2733 }
2731 2734
2732 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) 2735 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
2733 { 2736 {
2734 u64 num_devices; 2737 u64 num_devices;
2735 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | 2738 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
2736 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; 2739 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
2737 2740
2738 num_devices = root->fs_info->fs_devices->num_devices; 2741 num_devices = root->fs_info->fs_devices->num_devices;
2739 if (num_devices == 1) { 2742 if (num_devices == 1) {
2740 stripped |= BTRFS_BLOCK_GROUP_DUP; 2743 stripped |= BTRFS_BLOCK_GROUP_DUP;
2741 stripped = flags & ~stripped; 2744 stripped = flags & ~stripped;
2742 2745
2743 /* turn raid0 into single device chunks */ 2746 /* turn raid0 into single device chunks */
2744 if (flags & BTRFS_BLOCK_GROUP_RAID0) 2747 if (flags & BTRFS_BLOCK_GROUP_RAID0)
2745 return stripped; 2748 return stripped;
2746 2749
2747 /* turn mirroring into duplication */ 2750 /* turn mirroring into duplication */
2748 if (flags & (BTRFS_BLOCK_GROUP_RAID1 | 2751 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2749 BTRFS_BLOCK_GROUP_RAID10)) 2752 BTRFS_BLOCK_GROUP_RAID10))
2750 return stripped | BTRFS_BLOCK_GROUP_DUP; 2753 return stripped | BTRFS_BLOCK_GROUP_DUP;
2751 return flags; 2754 return flags;
2752 } else { 2755 } else {
2753 /* they already had raid on here, just return */ 2756 /* they already had raid on here, just return */
2754 if (flags & stripped) 2757 if (flags & stripped)
2755 return flags; 2758 return flags;
2756 2759
2757 stripped |= BTRFS_BLOCK_GROUP_DUP; 2760 stripped |= BTRFS_BLOCK_GROUP_DUP;
2758 stripped = flags & ~stripped; 2761 stripped = flags & ~stripped;
2759 2762
2760 /* switch duplicated blocks with raid1 */ 2763 /* switch duplicated blocks with raid1 */
2761 if (flags & BTRFS_BLOCK_GROUP_DUP) 2764 if (flags & BTRFS_BLOCK_GROUP_DUP)
2762 return stripped | BTRFS_BLOCK_GROUP_RAID1; 2765 return stripped | BTRFS_BLOCK_GROUP_RAID1;
2763 2766
2764 /* turn single device chunks into raid0 */ 2767 /* turn single device chunks into raid0 */
2765 return stripped | BTRFS_BLOCK_GROUP_RAID0; 2768 return stripped | BTRFS_BLOCK_GROUP_RAID0;
2766 } 2769 }
2767 return flags; 2770 return flags;
2768 } 2771 }
2769 2772
2770 int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start) 2773 int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
2771 { 2774 {
2772 struct btrfs_trans_handle *trans; 2775 struct btrfs_trans_handle *trans;
2773 struct btrfs_root *tree_root = root->fs_info->tree_root; 2776 struct btrfs_root *tree_root = root->fs_info->tree_root;
2774 struct btrfs_path *path; 2777 struct btrfs_path *path;
2775 u64 cur_byte; 2778 u64 cur_byte;
2776 u64 total_found; 2779 u64 total_found;
2777 u64 shrink_last_byte; 2780 u64 shrink_last_byte;
2778 u64 new_alloc_flags; 2781 u64 new_alloc_flags;
2779 struct btrfs_block_group_cache *shrink_block_group; 2782 struct btrfs_block_group_cache *shrink_block_group;
2780 struct btrfs_fs_info *info = root->fs_info; 2783 struct btrfs_fs_info *info = root->fs_info;
2781 struct btrfs_key key; 2784 struct btrfs_key key;
2782 struct btrfs_key found_key; 2785 struct btrfs_key found_key;
2783 struct extent_buffer *leaf; 2786 struct extent_buffer *leaf;
2784 u32 nritems; 2787 u32 nritems;
2785 int ret; 2788 int ret;
2786 int progress; 2789 int progress;
2787 2790
2788 shrink_block_group = btrfs_lookup_block_group(root->fs_info, 2791 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
2789 shrink_start); 2792 shrink_start);
2790 BUG_ON(!shrink_block_group); 2793 BUG_ON(!shrink_block_group);
2791 2794
2792 shrink_last_byte = shrink_start + shrink_block_group->key.offset; 2795 shrink_last_byte = shrink_start + shrink_block_group->key.offset;
2793 2796
2794 shrink_block_group->space_info->total_bytes -= 2797 shrink_block_group->space_info->total_bytes -=
2795 shrink_block_group->key.offset; 2798 shrink_block_group->key.offset;
2796 path = btrfs_alloc_path(); 2799 path = btrfs_alloc_path();
2797 root = root->fs_info->extent_root; 2800 root = root->fs_info->extent_root;
2798 path->reada = 2; 2801 path->reada = 2;
2799 2802
2800 printk("btrfs relocating block group %llu flags %llu\n", 2803 printk("btrfs relocating block group %llu flags %llu\n",
2801 (unsigned long long)shrink_start, 2804 (unsigned long long)shrink_start,
2802 (unsigned long long)shrink_block_group->flags); 2805 (unsigned long long)shrink_block_group->flags);
2803 2806
2804 again: 2807 again:
2805 if (btrfs_block_group_used(&shrink_block_group->item) > 0) { 2808 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
2806 u64 calc; 2809 u64 calc;
2807 2810
2808 trans = btrfs_start_transaction(root, 1); 2811 trans = btrfs_start_transaction(root, 1);
2809 new_alloc_flags = update_block_group_flags(root, 2812 new_alloc_flags = update_block_group_flags(root,
2810 shrink_block_group->flags); 2813 shrink_block_group->flags);
2811 if (new_alloc_flags != shrink_block_group->flags) { 2814 if (new_alloc_flags != shrink_block_group->flags) {
2812 calc = 2815 calc =
2813 btrfs_block_group_used(&shrink_block_group->item); 2816 btrfs_block_group_used(&shrink_block_group->item);
2814 } else { 2817 } else {
2815 calc = shrink_block_group->key.offset; 2818 calc = shrink_block_group->key.offset;
2816 } 2819 }
2817 do_chunk_alloc(trans, root->fs_info->extent_root, 2820 do_chunk_alloc(trans, root->fs_info->extent_root,
2818 calc + 2 * 1024 * 1024, new_alloc_flags); 2821 calc + 2 * 1024 * 1024, new_alloc_flags);
2819 btrfs_end_transaction(trans, root); 2822 btrfs_end_transaction(trans, root);
2820 } 2823 }
2821 shrink_block_group->ro = 1; 2824 shrink_block_group->ro = 1;
2822 2825
2823 total_found = 0; 2826 total_found = 0;
2824 progress = 0; 2827 progress = 0;
2825 key.objectid = shrink_start; 2828 key.objectid = shrink_start;
2826 key.offset = 0; 2829 key.offset = 0;
2827 key.type = 0; 2830 key.type = 0;
2828 cur_byte = key.objectid; 2831 cur_byte = key.objectid;
2829 2832
2830 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2833 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2831 if (ret < 0) 2834 if (ret < 0)
2832 goto out; 2835 goto out;
2833 2836
2834 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY); 2837 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
2835 if (ret < 0) 2838 if (ret < 0)
2836 goto out; 2839 goto out;
2837 2840
2838 if (ret == 0) { 2841 if (ret == 0) {
2839 leaf = path->nodes[0]; 2842 leaf = path->nodes[0];
2840 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 2843 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2841 if (found_key.objectid + found_key.offset > shrink_start && 2844 if (found_key.objectid + found_key.offset > shrink_start &&
2842 found_key.objectid < shrink_last_byte) { 2845 found_key.objectid < shrink_last_byte) {
2843 cur_byte = found_key.objectid; 2846 cur_byte = found_key.objectid;
2844 key.objectid = cur_byte; 2847 key.objectid = cur_byte;
2845 } 2848 }
2846 } 2849 }
2847 btrfs_release_path(root, path); 2850 btrfs_release_path(root, path);
2848 2851
2849 while(1) { 2852 while(1) {
2850 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2853 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2851 if (ret < 0) 2854 if (ret < 0)
2852 goto out; 2855 goto out;
2853 2856
2854 leaf = path->nodes[0]; 2857 leaf = path->nodes[0];
2855 nritems = btrfs_header_nritems(leaf); 2858 nritems = btrfs_header_nritems(leaf);
2856 next: 2859 next:
2857 if (path->slots[0] >= nritems) { 2860 if (path->slots[0] >= nritems) {
2858 ret = btrfs_next_leaf(root, path); 2861 ret = btrfs_next_leaf(root, path);
2859 if (ret < 0) 2862 if (ret < 0)
2860 goto out; 2863 goto out;
2861 if (ret == 1) { 2864 if (ret == 1) {
2862 ret = 0; 2865 ret = 0;
2863 break; 2866 break;
2864 } 2867 }
2865 leaf = path->nodes[0]; 2868 leaf = path->nodes[0];
2866 nritems = btrfs_header_nritems(leaf); 2869 nritems = btrfs_header_nritems(leaf);
2867 } 2870 }
2868 2871
2869 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 2872 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2870 2873
2871 if (found_key.objectid >= shrink_last_byte) 2874 if (found_key.objectid >= shrink_last_byte)
2872 break; 2875 break;
2873 2876
2874 if (progress && need_resched()) { 2877 if (progress && need_resched()) {
2875 memcpy(&key, &found_key, sizeof(key)); 2878 memcpy(&key, &found_key, sizeof(key));
2876 mutex_unlock(&root->fs_info->fs_mutex); 2879 mutex_unlock(&root->fs_info->fs_mutex);
2877 cond_resched(); 2880 cond_resched();
2878 mutex_lock(&root->fs_info->fs_mutex); 2881 mutex_lock(&root->fs_info->fs_mutex);
2879 btrfs_release_path(root, path); 2882 btrfs_release_path(root, path);
2880 btrfs_search_slot(NULL, root, &key, path, 0, 0); 2883 btrfs_search_slot(NULL, root, &key, path, 0, 0);
2881 progress = 0; 2884 progress = 0;
2882 goto next; 2885 goto next;
2883 } 2886 }
2884 progress = 1; 2887 progress = 1;
2885 2888
2886 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY || 2889 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
2887 found_key.objectid + found_key.offset <= cur_byte) { 2890 found_key.objectid + found_key.offset <= cur_byte) {
2888 path->slots[0]++; 2891 path->slots[0]++;
2889 goto next; 2892 goto next;
2890 } 2893 }
2891 2894
2892 total_found++; 2895 total_found++;
2893 cur_byte = found_key.objectid + found_key.offset; 2896 cur_byte = found_key.objectid + found_key.offset;
2894 key.objectid = cur_byte; 2897 key.objectid = cur_byte;
2895 btrfs_release_path(root, path); 2898 btrfs_release_path(root, path);
2896 ret = relocate_one_extent(root, path, &found_key); 2899 ret = relocate_one_extent(root, path, &found_key);
2897 } 2900 }
2898 2901
2899 btrfs_release_path(root, path); 2902 btrfs_release_path(root, path);
2900 2903
2901 if (total_found > 0) { 2904 if (total_found > 0) {
2902 printk("btrfs relocate found %llu last extent was %llu\n", 2905 printk("btrfs relocate found %llu last extent was %llu\n",
2903 (unsigned long long)total_found, 2906 (unsigned long long)total_found,
2904 (unsigned long long)found_key.objectid); 2907 (unsigned long long)found_key.objectid);
2905 trans = btrfs_start_transaction(tree_root, 1); 2908 trans = btrfs_start_transaction(tree_root, 1);
2906 btrfs_commit_transaction(trans, tree_root); 2909 btrfs_commit_transaction(trans, tree_root);
2907 2910
2908 mutex_unlock(&root->fs_info->fs_mutex); 2911 mutex_unlock(&root->fs_info->fs_mutex);
2909 btrfs_clean_old_snapshots(tree_root); 2912 btrfs_clean_old_snapshots(tree_root);
2910 mutex_lock(&root->fs_info->fs_mutex); 2913 mutex_lock(&root->fs_info->fs_mutex);
2911 2914
2912 trans = btrfs_start_transaction(tree_root, 1); 2915 trans = btrfs_start_transaction(tree_root, 1);
2913 btrfs_commit_transaction(trans, tree_root); 2916 btrfs_commit_transaction(trans, tree_root);
2914 goto again; 2917 goto again;
2915 } 2918 }
2916 2919
2917 /* 2920 /*
2918 * we've freed all the extents, now remove the block 2921 * we've freed all the extents, now remove the block
2919 * group item from the tree 2922 * group item from the tree
2920 */ 2923 */
2921 trans = btrfs_start_transaction(root, 1); 2924 trans = btrfs_start_transaction(root, 1);
2922 memcpy(&key, &shrink_block_group->key, sizeof(key)); 2925 memcpy(&key, &shrink_block_group->key, sizeof(key));
2923 2926
2924 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 2927 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2925 if (ret > 0) 2928 if (ret > 0)
2926 ret = -EIO; 2929 ret = -EIO;
2927 if (ret < 0) 2930 if (ret < 0)
2928 goto out; 2931 goto out;
2929 2932
2930 leaf = path->nodes[0]; 2933 leaf = path->nodes[0];
2931 nritems = btrfs_header_nritems(leaf); 2934 nritems = btrfs_header_nritems(leaf);
2932 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 2935 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2933 kfree(shrink_block_group); 2936 kfree(shrink_block_group);
2934 2937
2935 clear_extent_bits(&info->block_group_cache, found_key.objectid, 2938 clear_extent_bits(&info->block_group_cache, found_key.objectid,
2936 found_key.objectid + found_key.offset - 1, 2939 found_key.objectid + found_key.offset - 1,
2937 (unsigned int)-1, GFP_NOFS); 2940 (unsigned int)-1, GFP_NOFS);
2938 2941
2939 btrfs_del_item(trans, root, path); 2942 btrfs_del_item(trans, root, path);
2940 clear_extent_dirty(&info->free_space_cache, 2943 clear_extent_dirty(&info->free_space_cache,
2941 shrink_start, shrink_last_byte - 1, 2944 shrink_start, shrink_last_byte - 1,
2942 GFP_NOFS); 2945 GFP_NOFS);
2943 btrfs_commit_transaction(trans, root); 2946 btrfs_commit_transaction(trans, root);
2944 out: 2947 out:
2945 btrfs_free_path(path); 2948 btrfs_free_path(path);
2946 return ret; 2949 return ret;
2947 } 2950 }
2948 2951
2949 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path, 2952 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
2950 struct btrfs_key *key) 2953 struct btrfs_key *key)
2951 { 2954 {
2952 int ret; 2955 int ret;
2953 struct btrfs_key found_key; 2956 struct btrfs_key found_key;
2954 struct extent_buffer *leaf; 2957 struct extent_buffer *leaf;
2955 int slot; 2958 int slot;
2956 2959
2957 ret = btrfs_search_slot(NULL, root, key, path, 0, 0); 2960 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
2958 if (ret < 0) 2961 if (ret < 0)
2959 return ret; 2962 return ret;
2960 while(1) { 2963 while(1) {
2961 slot = path->slots[0]; 2964 slot = path->slots[0];
2962 leaf = path->nodes[0]; 2965 leaf = path->nodes[0];
2963 if (slot >= btrfs_header_nritems(leaf)) { 2966 if (slot >= btrfs_header_nritems(leaf)) {
2964 ret = btrfs_next_leaf(root, path); 2967 ret = btrfs_next_leaf(root, path);
2965 if (ret == 0) 2968 if (ret == 0)
2966 continue; 2969 continue;
2967 if (ret < 0) 2970 if (ret < 0)
2968 goto error; 2971 goto error;
2969 break; 2972 break;
2970 } 2973 }
2971 btrfs_item_key_to_cpu(leaf, &found_key, slot); 2974 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2972 2975
2973 if (found_key.objectid >= key->objectid && 2976 if (found_key.objectid >= key->objectid &&
2974 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) 2977 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
2975 return 0; 2978 return 0;
2976 path->slots[0]++; 2979 path->slots[0]++;
2977 } 2980 }
2978 ret = -ENOENT; 2981 ret = -ENOENT;
2979 error: 2982 error:
2980 return ret; 2983 return ret;
2981 } 2984 }
2982 2985
2983 int btrfs_read_block_groups(struct btrfs_root *root) 2986 int btrfs_read_block_groups(struct btrfs_root *root)
2984 { 2987 {
2985 struct btrfs_path *path; 2988 struct btrfs_path *path;
2986 int ret; 2989 int ret;
2987 int bit; 2990 int bit;
2988 struct btrfs_block_group_cache *cache; 2991 struct btrfs_block_group_cache *cache;
2989 struct btrfs_fs_info *info = root->fs_info; 2992 struct btrfs_fs_info *info = root->fs_info;
2990 struct btrfs_space_info *space_info; 2993 struct btrfs_space_info *space_info;
2991 struct extent_io_tree *block_group_cache; 2994 struct extent_io_tree *block_group_cache;
2992 struct btrfs_key key; 2995 struct btrfs_key key;
2993 struct btrfs_key found_key; 2996 struct btrfs_key found_key;
2994 struct extent_buffer *leaf; 2997 struct extent_buffer *leaf;
2995 2998
2996 block_group_cache = &info->block_group_cache; 2999 block_group_cache = &info->block_group_cache;
2997 root = info->extent_root; 3000 root = info->extent_root;
2998 key.objectid = 0; 3001 key.objectid = 0;
2999 key.offset = 0; 3002 key.offset = 0;
3000 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); 3003 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3001 path = btrfs_alloc_path(); 3004 path = btrfs_alloc_path();
3002 if (!path) 3005 if (!path)
3003 return -ENOMEM; 3006 return -ENOMEM;
3004 3007
3005 while(1) { 3008 while(1) {
3006 ret = find_first_block_group(root, path, &key); 3009 ret = find_first_block_group(root, path, &key);
3007 if (ret > 0) { 3010 if (ret > 0) {
3008 ret = 0; 3011 ret = 0;
3009 goto error; 3012 goto error;
3010 } 3013 }
3011 if (ret != 0) 3014 if (ret != 0)
3012 goto error; 3015 goto error;
3013 3016
3014 leaf = path->nodes[0]; 3017 leaf = path->nodes[0];
3015 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 3018 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3016 cache = kzalloc(sizeof(*cache), GFP_NOFS); 3019 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3017 if (!cache) { 3020 if (!cache) {
3018 ret = -ENOMEM; 3021 ret = -ENOMEM;
3019 break; 3022 break;
3020 } 3023 }
3021 3024
3022 read_extent_buffer(leaf, &cache->item, 3025 read_extent_buffer(leaf, &cache->item,
3023 btrfs_item_ptr_offset(leaf, path->slots[0]), 3026 btrfs_item_ptr_offset(leaf, path->slots[0]),
3024 sizeof(cache->item)); 3027 sizeof(cache->item));
3025 memcpy(&cache->key, &found_key, sizeof(found_key)); 3028 memcpy(&cache->key, &found_key, sizeof(found_key));
3026 3029
3027 key.objectid = found_key.objectid + found_key.offset; 3030 key.objectid = found_key.objectid + found_key.offset;
3028 btrfs_release_path(root, path); 3031 btrfs_release_path(root, path);
3029 cache->flags = btrfs_block_group_flags(&cache->item); 3032 cache->flags = btrfs_block_group_flags(&cache->item);
3030 bit = 0; 3033 bit = 0;
3031 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) { 3034 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3032 bit = BLOCK_GROUP_DATA; 3035 bit = BLOCK_GROUP_DATA;
3033 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { 3036 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3034 bit = BLOCK_GROUP_SYSTEM; 3037 bit = BLOCK_GROUP_SYSTEM;
3035 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) { 3038 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
3036 bit = BLOCK_GROUP_METADATA; 3039 bit = BLOCK_GROUP_METADATA;
3037 } 3040 }
3038 set_avail_alloc_bits(info, cache->flags); 3041 set_avail_alloc_bits(info, cache->flags);
3039 3042
3040 ret = update_space_info(info, cache->flags, found_key.offset, 3043 ret = update_space_info(info, cache->flags, found_key.offset,
3041 btrfs_block_group_used(&cache->item), 3044 btrfs_block_group_used(&cache->item),
3042 &space_info); 3045 &space_info);
3043 BUG_ON(ret); 3046 BUG_ON(ret);
3044 cache->space_info = space_info; 3047 cache->space_info = space_info;
3045 3048
3046 /* use EXTENT_LOCKED to prevent merging */ 3049 /* use EXTENT_LOCKED to prevent merging */
3047 set_extent_bits(block_group_cache, found_key.objectid, 3050 set_extent_bits(block_group_cache, found_key.objectid,
3048 found_key.objectid + found_key.offset - 1, 3051 found_key.objectid + found_key.offset - 1,
3049 bit | EXTENT_LOCKED, GFP_NOFS); 3052 bit | EXTENT_LOCKED, GFP_NOFS);
3050 set_state_private(block_group_cache, found_key.objectid, 3053 set_state_private(block_group_cache, found_key.objectid,
3051 (unsigned long)cache); 3054 (unsigned long)cache);
3052 3055
3053 if (key.objectid >= 3056 if (key.objectid >=
3054 btrfs_super_total_bytes(&info->super_copy)) 3057 btrfs_super_total_bytes(&info->super_copy))
3055 break; 3058 break;
3056 } 3059 }
3057 ret = 0; 3060 ret = 0;
3058 error: 3061 error:
3059 btrfs_free_path(path); 3062 btrfs_free_path(path);
3060 return ret; 3063 return ret;
3061 } 3064 }
3062 3065
3063 int btrfs_make_block_group(struct btrfs_trans_handle *trans, 3066 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3064 struct btrfs_root *root, u64 bytes_used, 3067 struct btrfs_root *root, u64 bytes_used,
3065 u64 type, u64 chunk_objectid, u64 chunk_offset, 3068 u64 type, u64 chunk_objectid, u64 chunk_offset,
3066 u64 size) 3069 u64 size)
3067 { 3070 {
3068 int ret; 3071 int ret;
3069 int bit = 0; 3072 int bit = 0;
3070 struct btrfs_root *extent_root; 3073 struct btrfs_root *extent_root;
3071 struct btrfs_block_group_cache *cache; 3074 struct btrfs_block_group_cache *cache;
3072 struct extent_io_tree *block_group_cache; 3075 struct extent_io_tree *block_group_cache;
3073 3076
3074 extent_root = root->fs_info->extent_root; 3077 extent_root = root->fs_info->extent_root;
3075 block_group_cache = &root->fs_info->block_group_cache; 3078 block_group_cache = &root->fs_info->block_group_cache;
3076 3079
3077 cache = kzalloc(sizeof(*cache), GFP_NOFS); 3080 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3078 BUG_ON(!cache); 3081 BUG_ON(!cache);
3079 cache->key.objectid = chunk_offset; 3082 cache->key.objectid = chunk_offset;
3080 cache->key.offset = size; 3083 cache->key.offset = size;
3081 3084
3082 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY); 3085 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3083 memset(&cache->item, 0, sizeof(cache->item)); 3086 memset(&cache->item, 0, sizeof(cache->item));
3084 btrfs_set_block_group_used(&cache->item, bytes_used); 3087 btrfs_set_block_group_used(&cache->item, bytes_used);
3085 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); 3088 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3086 cache->flags = type; 3089 cache->flags = type;
3087 btrfs_set_block_group_flags(&cache->item, type); 3090 btrfs_set_block_group_flags(&cache->item, type);
3088 3091
3089 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, 3092 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
3090 &cache->space_info); 3093 &cache->space_info);
3091 BUG_ON(ret); 3094 BUG_ON(ret);
3092 3095
3093 bit = block_group_state_bits(type); 3096 bit = block_group_state_bits(type);
3094 set_extent_bits(block_group_cache, chunk_offset, 3097 set_extent_bits(block_group_cache, chunk_offset,
3095 chunk_offset + size - 1, 3098 chunk_offset + size - 1,
3096 bit | EXTENT_LOCKED, GFP_NOFS); 3099 bit | EXTENT_LOCKED, GFP_NOFS);
3097 3100
3098 set_state_private(block_group_cache, chunk_offset, 3101 set_state_private(block_group_cache, chunk_offset,
3099 (unsigned long)cache); 3102 (unsigned long)cache);
3100 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, 3103 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3101 sizeof(cache->item)); 3104 sizeof(cache->item));
3102 BUG_ON(ret); 3105 BUG_ON(ret);
3103 3106
3104 finish_current_insert(trans, extent_root); 3107 finish_current_insert(trans, extent_root);
3105 ret = del_pending_extents(trans, extent_root); 3108 ret = del_pending_extents(trans, extent_root);
3106 BUG_ON(ret); 3109 BUG_ON(ret);
3107 set_avail_alloc_bits(extent_root->fs_info, type); 3110 set_avail_alloc_bits(extent_root->fs_info, type);
3108 return 0; 3111 return 0;
fs/btrfs/print-tree.c
1 /* 1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved. 2 * Copyright (C) 2007 Oracle. All rights reserved.
3 * 3 *
4 * This program is free software; you can redistribute it and/or 4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public 5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation. 6 * License v2 as published by the Free Software Foundation.
7 * 7 *
8 * This program is distributed in the hope that it will be useful, 8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details. 11 * General Public License for more details.
12 * 12 *
13 * You should have received a copy of the GNU General Public 13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the 14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA. 16 * Boston, MA 021110-1307, USA.
17 */ 17 */
18 18
19 #include "ctree.h" 19 #include "ctree.h"
20 #include "disk-io.h" 20 #include "disk-io.h"
21 #include "print-tree.h" 21 #include "print-tree.h"
22 22
23 static void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk) 23 static void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
24 { 24 {
25 int num_stripes = btrfs_chunk_num_stripes(eb, chunk); 25 int num_stripes = btrfs_chunk_num_stripes(eb, chunk);
26 int i; 26 int i;
27 printk("\t\tchunk length %llu owner %llu type %llu num_stripes %d\n", 27 printk("\t\tchunk length %llu owner %llu type %llu num_stripes %d\n",
28 (unsigned long long)btrfs_chunk_length(eb, chunk), 28 (unsigned long long)btrfs_chunk_length(eb, chunk),
29 (unsigned long long)btrfs_chunk_owner(eb, chunk), 29 (unsigned long long)btrfs_chunk_owner(eb, chunk),
30 (unsigned long long)btrfs_chunk_type(eb, chunk), 30 (unsigned long long)btrfs_chunk_type(eb, chunk),
31 num_stripes); 31 num_stripes);
32 for (i = 0 ; i < num_stripes ; i++) { 32 for (i = 0 ; i < num_stripes ; i++) {
33 printk("\t\t\tstripe %d devid %llu offset %llu\n", i, 33 printk("\t\t\tstripe %d devid %llu offset %llu\n", i,
34 (unsigned long long)btrfs_stripe_devid_nr(eb, chunk, i), 34 (unsigned long long)btrfs_stripe_devid_nr(eb, chunk, i),
35 (unsigned long long)btrfs_stripe_offset_nr(eb, chunk, i)); 35 (unsigned long long)btrfs_stripe_offset_nr(eb, chunk, i));
36 } 36 }
37 } 37 }
38 static void print_dev_item(struct extent_buffer *eb, 38 static void print_dev_item(struct extent_buffer *eb,
39 struct btrfs_dev_item *dev_item) 39 struct btrfs_dev_item *dev_item)
40 { 40 {
41 printk("\t\tdev item devid %llu " 41 printk("\t\tdev item devid %llu "
42 "total_bytes %llu bytes used %Lu\n", 42 "total_bytes %llu bytes used %Lu\n",
43 (unsigned long long)btrfs_device_id(eb, dev_item), 43 (unsigned long long)btrfs_device_id(eb, dev_item),
44 (unsigned long long)btrfs_device_total_bytes(eb, dev_item), 44 (unsigned long long)btrfs_device_total_bytes(eb, dev_item),
45 (unsigned long long)btrfs_device_bytes_used(eb, dev_item)); 45 (unsigned long long)btrfs_device_bytes_used(eb, dev_item));
46 } 46 }
47 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l) 47 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
48 { 48 {
49 int i; 49 int i;
50 u32 nr = btrfs_header_nritems(l); 50 u32 nr = btrfs_header_nritems(l);
51 struct btrfs_item *item; 51 struct btrfs_item *item;
52 struct btrfs_extent_item *ei; 52 struct btrfs_extent_item *ei;
53 struct btrfs_root_item *ri; 53 struct btrfs_root_item *ri;
54 struct btrfs_dir_item *di; 54 struct btrfs_dir_item *di;
55 struct btrfs_inode_item *ii; 55 struct btrfs_inode_item *ii;
56 struct btrfs_block_group_item *bi; 56 struct btrfs_block_group_item *bi;
57 struct btrfs_file_extent_item *fi; 57 struct btrfs_file_extent_item *fi;
58 struct btrfs_key key; 58 struct btrfs_key key;
59 struct btrfs_key found_key; 59 struct btrfs_key found_key;
60 struct btrfs_extent_ref *ref; 60 struct btrfs_extent_ref *ref;
61 struct btrfs_dev_extent *dev_extent; 61 struct btrfs_dev_extent *dev_extent;
62 u32 type; 62 u32 type;
63 63
64 printk("leaf %llu total ptrs %d free space %d\n", 64 printk("leaf %llu total ptrs %d free space %d\n",
65 (unsigned long long)btrfs_header_bytenr(l), nr, 65 (unsigned long long)btrfs_header_bytenr(l), nr,
66 btrfs_leaf_free_space(root, l)); 66 btrfs_leaf_free_space(root, l));
67 for (i = 0 ; i < nr ; i++) { 67 for (i = 0 ; i < nr ; i++) {
68 item = btrfs_item_nr(l, i); 68 item = btrfs_item_nr(l, i);
69 btrfs_item_key_to_cpu(l, &key, i); 69 btrfs_item_key_to_cpu(l, &key, i);
70 type = btrfs_key_type(&key); 70 type = btrfs_key_type(&key);
71 printk("\titem %d key (%llu %x %llu) itemoff %d itemsize %d\n", 71 printk("\titem %d key (%llu %x %llu) itemoff %d itemsize %d\n",
72 i, 72 i,
73 (unsigned long long)key.objectid, type, 73 (unsigned long long)key.objectid, type,
74 (unsigned long long)key.offset, 74 (unsigned long long)key.offset,
75 btrfs_item_offset(l, item), btrfs_item_size(l, item)); 75 btrfs_item_offset(l, item), btrfs_item_size(l, item));
76 switch (type) { 76 switch (type) {
77 case BTRFS_INODE_ITEM_KEY: 77 case BTRFS_INODE_ITEM_KEY:
78 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item); 78 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
79 printk("\t\tinode generation %llu size %llu mode %o\n", 79 printk("\t\tinode generation %llu size %llu mode %o\n",
80 (unsigned long long)btrfs_inode_generation(l, ii), 80 (unsigned long long)btrfs_inode_generation(l, ii),
81 (unsigned long long)btrfs_inode_size(l, ii), 81 (unsigned long long)btrfs_inode_size(l, ii),
82 btrfs_inode_mode(l, ii)); 82 btrfs_inode_mode(l, ii));
83 break; 83 break;
84 case BTRFS_DIR_ITEM_KEY: 84 case BTRFS_DIR_ITEM_KEY:
85 di = btrfs_item_ptr(l, i, struct btrfs_dir_item); 85 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
86 btrfs_dir_item_key_to_cpu(l, di, &found_key); 86 btrfs_dir_item_key_to_cpu(l, di, &found_key);
87 printk("\t\tdir oid %llu type %u\n", 87 printk("\t\tdir oid %llu type %u\n",
88 (unsigned long long)found_key.objectid, 88 (unsigned long long)found_key.objectid,
89 btrfs_dir_type(l, di)); 89 btrfs_dir_type(l, di));
90 break; 90 break;
91 case BTRFS_ROOT_ITEM_KEY: 91 case BTRFS_ROOT_ITEM_KEY:
92 ri = btrfs_item_ptr(l, i, struct btrfs_root_item); 92 ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
93 printk("\t\troot data bytenr %llu refs %u\n", 93 printk("\t\troot data bytenr %llu refs %u\n",
94 (unsigned long long)btrfs_disk_root_bytenr(l, ri), 94 (unsigned long long)btrfs_disk_root_bytenr(l, ri),
95 btrfs_disk_root_refs(l, ri)); 95 btrfs_disk_root_refs(l, ri));
96 break; 96 break;
97 case BTRFS_EXTENT_ITEM_KEY: 97 case BTRFS_EXTENT_ITEM_KEY:
98 ei = btrfs_item_ptr(l, i, struct btrfs_extent_item); 98 ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
99 printk("\t\textent data refs %u\n", 99 printk("\t\textent data refs %u\n",
100 btrfs_extent_refs(l, ei)); 100 btrfs_extent_refs(l, ei));
101 break; 101 break;
102 case BTRFS_EXTENT_REF_KEY: 102 case BTRFS_EXTENT_REF_KEY:
103 ref = btrfs_item_ptr(l, i, struct btrfs_extent_ref); 103 ref = btrfs_item_ptr(l, i, struct btrfs_extent_ref);
104 printk("\t\textent back ref root %llu gen %llu " 104 printk("\t\textent back ref root %llu gen %llu "
105 "owner %llu offset %llu\n", 105 "owner %llu offset %llu\n",
106 (unsigned long long)btrfs_ref_root(l, ref), 106 (unsigned long long)btrfs_ref_root(l, ref),
107 (unsigned long long)btrfs_ref_generation(l, ref), 107 (unsigned long long)btrfs_ref_generation(l, ref),
108 (unsigned long long)btrfs_ref_objectid(l, ref), 108 (unsigned long long)btrfs_ref_objectid(l, ref),
109 (unsigned long long)btrfs_ref_offset(l, ref)); 109 (unsigned long long)btrfs_ref_offset(l, ref));
110 break; 110 break;
111 111
112 case BTRFS_EXTENT_DATA_KEY: 112 case BTRFS_EXTENT_DATA_KEY:
113 fi = btrfs_item_ptr(l, i, 113 fi = btrfs_item_ptr(l, i,
114 struct btrfs_file_extent_item); 114 struct btrfs_file_extent_item);
115 if (btrfs_file_extent_type(l, fi) == 115 if (btrfs_file_extent_type(l, fi) ==
116 BTRFS_FILE_EXTENT_INLINE) { 116 BTRFS_FILE_EXTENT_INLINE) {
117 printk("\t\tinline extent data size %u\n", 117 printk("\t\tinline extent data size %u\n",
118 btrfs_file_extent_inline_len(l, item)); 118 btrfs_file_extent_inline_len(l, item));
119 break; 119 break;
120 } 120 }
121 printk("\t\textent data disk bytenr %llu nr %llu\n", 121 printk("\t\textent data disk bytenr %llu nr %llu\n",
122 (unsigned long long)btrfs_file_extent_disk_bytenr(l, fi), 122 (unsigned long long)btrfs_file_extent_disk_bytenr(l, fi),
123 (unsigned long long)btrfs_file_extent_disk_num_bytes(l, fi)); 123 (unsigned long long)btrfs_file_extent_disk_num_bytes(l, fi));
124 printk("\t\textent data offset %llu nr %llu\n", 124 printk("\t\textent data offset %llu nr %llu\n",
125 (unsigned long long)btrfs_file_extent_offset(l, fi), 125 (unsigned long long)btrfs_file_extent_offset(l, fi),
126 (unsigned long long)btrfs_file_extent_num_bytes(l, fi)); 126 (unsigned long long)btrfs_file_extent_num_bytes(l, fi));
127 break; 127 break;
128 case BTRFS_BLOCK_GROUP_ITEM_KEY: 128 case BTRFS_BLOCK_GROUP_ITEM_KEY:
129 bi = btrfs_item_ptr(l, i, 129 bi = btrfs_item_ptr(l, i,
130 struct btrfs_block_group_item); 130 struct btrfs_block_group_item);
131 printk("\t\tblock group used %llu\n", 131 printk("\t\tblock group used %llu\n",
132 (unsigned long long)btrfs_disk_block_group_used(l, bi)); 132 (unsigned long long)btrfs_disk_block_group_used(l, bi));
133 break; 133 break;
134 case BTRFS_CHUNK_ITEM_KEY: 134 case BTRFS_CHUNK_ITEM_KEY:
135 print_chunk(l, btrfs_item_ptr(l, i, struct btrfs_chunk)); 135 print_chunk(l, btrfs_item_ptr(l, i, struct btrfs_chunk));
136 break; 136 break;
137 case BTRFS_DEV_ITEM_KEY: 137 case BTRFS_DEV_ITEM_KEY:
138 print_dev_item(l, btrfs_item_ptr(l, i, 138 print_dev_item(l, btrfs_item_ptr(l, i,
139 struct btrfs_dev_item)); 139 struct btrfs_dev_item));
140 break; 140 break;
141 case BTRFS_DEV_EXTENT_KEY: 141 case BTRFS_DEV_EXTENT_KEY:
142 dev_extent = btrfs_item_ptr(l, i, 142 dev_extent = btrfs_item_ptr(l, i,
143 struct btrfs_dev_extent); 143 struct btrfs_dev_extent);
144 printk("\t\tdev extent chunk_tree %llu\n" 144 printk("\t\tdev extent chunk_tree %llu\n"
145 "\t\tchunk objectid %llu chunk offset %llu " 145 "\t\tchunk objectid %llu chunk offset %llu "
146 "length %llu\n", 146 "length %llu\n",
147 (unsigned long long) 147 (unsigned long long)
148 btrfs_dev_extent_chunk_tree(l, dev_extent), 148 btrfs_dev_extent_chunk_tree(l, dev_extent),
149 (unsigned long long) 149 (unsigned long long)
150 btrfs_dev_extent_chunk_objectid(l, dev_extent), 150 btrfs_dev_extent_chunk_objectid(l, dev_extent),
151 (unsigned long long) 151 (unsigned long long)
152 btrfs_dev_extent_chunk_offset(l, dev_extent), 152 btrfs_dev_extent_chunk_offset(l, dev_extent),
153 (unsigned long long) 153 (unsigned long long)
154 btrfs_dev_extent_length(l, dev_extent)); 154 btrfs_dev_extent_length(l, dev_extent));
155 }; 155 };
156 } 156 }
157 } 157 }
158 158
159 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *c) 159 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *c)
160 { 160 {
161 int i; u32 nr; 161 int i; u32 nr;
162 struct btrfs_key key; 162 struct btrfs_key key;
163 int level; 163 int level;
164 164
165 if (!c) 165 if (!c)
166 return; 166 return;
167 nr = btrfs_header_nritems(c); 167 nr = btrfs_header_nritems(c);
168 level = btrfs_header_level(c); 168 level = btrfs_header_level(c);
169 if (level == 0) { 169 if (level == 0) {
170 btrfs_print_leaf(root, c); 170 btrfs_print_leaf(root, c);
171 return; 171 return;
172 } 172 }
173 printk("node %llu level %d total ptrs %d free spc %u\n", 173 printk("node %llu level %d total ptrs %d free spc %u\n",
174 (unsigned long long)btrfs_header_bytenr(c), 174 (unsigned long long)btrfs_header_bytenr(c),
175 btrfs_header_level(c), nr, 175 btrfs_header_level(c), nr,
176 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr); 176 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr);
177 for (i = 0; i < nr; i++) { 177 for (i = 0; i < nr; i++) {
178 btrfs_node_key_to_cpu(c, &key, i); 178 btrfs_node_key_to_cpu(c, &key, i);
179 printk("\tkey %d (%llu %u %llu) block %llu\n", 179 printk("\tkey %d (%llu %u %llu) block %llu\n",
180 i, 180 i,
181 (unsigned long long)key.objectid, 181 (unsigned long long)key.objectid,
182 key.type, 182 key.type,
183 (unsigned long long)key.offset, 183 (unsigned long long)key.offset,
184 (unsigned long long)btrfs_node_blockptr(c, i)); 184 (unsigned long long)btrfs_node_blockptr(c, i));
185 } 185 }
186 for (i = 0; i < nr; i++) { 186 for (i = 0; i < nr; i++) {
187 struct extent_buffer *next = read_tree_block(root, 187 struct extent_buffer *next = read_tree_block(root,
188 btrfs_node_blockptr(c, i), 188 btrfs_node_blockptr(c, i),
189 btrfs_level_size(root, level - 1)); 189 btrfs_level_size(root, level - 1),
190 btrfs_node_ptr_generation(c, i));
190 if (btrfs_is_leaf(next) && 191 if (btrfs_is_leaf(next) &&
191 btrfs_header_level(c) != 1) 192 btrfs_header_level(c) != 1)
192 BUG(); 193 BUG();
193 if (btrfs_header_level(next) != 194 if (btrfs_header_level(next) !=
194 btrfs_header_level(c) - 1) 195 btrfs_header_level(c) - 1)
195 BUG(); 196 BUG();
196 btrfs_print_tree(root, next); 197 btrfs_print_tree(root, next);
197 free_extent_buffer(next); 198 free_extent_buffer(next);
198 } 199 }
199 } 200 }
200 201
201 202
fs/btrfs/tree-defrag.c
1 /* 1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved. 2 * Copyright (C) 2007 Oracle. All rights reserved.
3 * 3 *
4 * This program is free software; you can redistribute it and/or 4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public 5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation. 6 * License v2 as published by the Free Software Foundation.
7 * 7 *
8 * This program is distributed in the hope that it will be useful, 8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details. 11 * General Public License for more details.
12 * 12 *
13 * You should have received a copy of the GNU General Public 13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the 14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA. 16 * Boston, MA 021110-1307, USA.
17 */ 17 */
18 18
19 #include <linux/sched.h> 19 #include <linux/sched.h>
20 #include "ctree.h" 20 #include "ctree.h"
21 #include "disk-io.h" 21 #include "disk-io.h"
22 #include "print-tree.h" 22 #include "print-tree.h"
23 #include "transaction.h" 23 #include "transaction.h"
24 24
25 static void reada_defrag(struct btrfs_root *root, 25 static void reada_defrag(struct btrfs_root *root,
26 struct extent_buffer *node) 26 struct extent_buffer *node)
27 { 27 {
28 int i; 28 int i;
29 u32 nritems; 29 u32 nritems;
30 u64 bytenr; 30 u64 bytenr;
31 u64 gen;
31 u32 blocksize; 32 u32 blocksize;
32 int ret; 33 int ret;
33 34
34 blocksize = btrfs_level_size(root, btrfs_header_level(node) - 1); 35 blocksize = btrfs_level_size(root, btrfs_header_level(node) - 1);
35 nritems = btrfs_header_nritems(node); 36 nritems = btrfs_header_nritems(node);
36 for (i = 0; i < nritems; i++) { 37 for (i = 0; i < nritems; i++) {
37 bytenr = btrfs_node_blockptr(node, i); 38 bytenr = btrfs_node_blockptr(node, i);
38 ret = readahead_tree_block(root, bytenr, blocksize); 39 gen = btrfs_node_ptr_generation(node, i);
40 ret = readahead_tree_block(root, bytenr, blocksize, gen);
39 if (ret) 41 if (ret)
40 break; 42 break;
41 } 43 }
42 } 44 }
43 45
44 static int defrag_walk_down(struct btrfs_trans_handle *trans, 46 static int defrag_walk_down(struct btrfs_trans_handle *trans,
45 struct btrfs_root *root, 47 struct btrfs_root *root,
46 struct btrfs_path *path, int *level, 48 struct btrfs_path *path, int *level,
47 int cache_only, u64 *last_ret) 49 int cache_only, u64 *last_ret)
48 { 50 {
49 struct extent_buffer *next; 51 struct extent_buffer *next;
50 struct extent_buffer *cur; 52 struct extent_buffer *cur;
51 u64 bytenr; 53 u64 bytenr;
52 int ret = 0; 54 int ret = 0;
53 int is_extent = 0; 55 int is_extent = 0;
54 56
55 WARN_ON(*level < 0); 57 WARN_ON(*level < 0);
56 WARN_ON(*level >= BTRFS_MAX_LEVEL); 58 WARN_ON(*level >= BTRFS_MAX_LEVEL);
57 59
58 if (root->fs_info->extent_root == root) 60 if (root->fs_info->extent_root == root)
59 is_extent = 1; 61 is_extent = 1;
60 62
61 if (*level == 1 && cache_only && path->nodes[1] && 63 if (*level == 1 && cache_only && path->nodes[1] &&
62 !btrfs_buffer_defrag(path->nodes[1])) { 64 !btrfs_buffer_defrag(path->nodes[1])) {
63 goto out; 65 goto out;
64 } 66 }
65 while(*level > 0) { 67 while(*level > 0) {
66 WARN_ON(*level < 0); 68 WARN_ON(*level < 0);
67 WARN_ON(*level >= BTRFS_MAX_LEVEL); 69 WARN_ON(*level >= BTRFS_MAX_LEVEL);
68 cur = path->nodes[*level]; 70 cur = path->nodes[*level];
69 71
70 if (!cache_only && *level > 1 && path->slots[*level] == 0) 72 if (!cache_only && *level > 1 && path->slots[*level] == 0)
71 reada_defrag(root, cur); 73 reada_defrag(root, cur);
72 74
73 if (btrfs_header_level(cur) != *level) 75 if (btrfs_header_level(cur) != *level)
74 WARN_ON(1); 76 WARN_ON(1);
75 77
76 if (path->slots[*level] >= 78 if (path->slots[*level] >=
77 btrfs_header_nritems(cur)) 79 btrfs_header_nritems(cur))
78 break; 80 break;
79 81
80 if (*level == 1) { 82 if (*level == 1) {
81 WARN_ON(btrfs_header_generation(path->nodes[*level]) != 83 WARN_ON(btrfs_header_generation(path->nodes[*level]) !=
82 trans->transid); 84 trans->transid);
83 ret = btrfs_realloc_node(trans, root, 85 ret = btrfs_realloc_node(trans, root,
84 path->nodes[*level], 86 path->nodes[*level],
85 path->slots[*level], 87 path->slots[*level],
86 cache_only, last_ret, 88 cache_only, last_ret,
87 &root->defrag_progress); 89 &root->defrag_progress);
88 if (is_extent) 90 if (is_extent)
89 btrfs_extent_post_op(trans, root); 91 btrfs_extent_post_op(trans, root);
90 92
91 break; 93 break;
92 } 94 }
93 bytenr = btrfs_node_blockptr(cur, path->slots[*level]); 95 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
94 96
95 if (cache_only) { 97 if (cache_only) {
96 next = btrfs_find_tree_block(root, bytenr, 98 next = btrfs_find_tree_block(root, bytenr,
97 btrfs_level_size(root, *level - 1)); 99 btrfs_level_size(root, *level - 1));
98 if (!next || !btrfs_buffer_uptodate(next) || 100 if (!next || !btrfs_buffer_uptodate(next) ||
99 !btrfs_buffer_defrag(next)) { 101 !btrfs_buffer_defrag(next)) {
100 free_extent_buffer(next); 102 free_extent_buffer(next);
101 path->slots[*level]++; 103 path->slots[*level]++;
102 continue; 104 continue;
103 } 105 }
104 btrfs_verify_block_csum(root, next);
105 } else { 106 } else {
106 next = read_tree_block(root, bytenr, 107 next = read_tree_block(root, bytenr,
107 btrfs_level_size(root, *level - 1)); 108 btrfs_level_size(root, *level - 1),
109 btrfs_node_ptr_generation(cur,
110 path->slots[*level]));
108 } 111 }
109 ret = btrfs_cow_block(trans, root, next, path->nodes[*level], 112 ret = btrfs_cow_block(trans, root, next, path->nodes[*level],
110 path->slots[*level], &next); 113 path->slots[*level], &next);
111 BUG_ON(ret); 114 BUG_ON(ret);
112 if (is_extent) 115 if (is_extent)
113 btrfs_extent_post_op(trans, root); 116 btrfs_extent_post_op(trans, root);
114 117
115 WARN_ON(*level <= 0); 118 WARN_ON(*level <= 0);
116 if (path->nodes[*level-1]) 119 if (path->nodes[*level-1])
117 free_extent_buffer(path->nodes[*level-1]); 120 free_extent_buffer(path->nodes[*level-1]);
118 path->nodes[*level-1] = next; 121 path->nodes[*level-1] = next;
119 *level = btrfs_header_level(next); 122 *level = btrfs_header_level(next);
120 path->slots[*level] = 0; 123 path->slots[*level] = 0;
121 } 124 }
122 WARN_ON(*level < 0); 125 WARN_ON(*level < 0);
123 WARN_ON(*level >= BTRFS_MAX_LEVEL); 126 WARN_ON(*level >= BTRFS_MAX_LEVEL);
124 127
125 btrfs_clear_buffer_defrag(path->nodes[*level]); 128 btrfs_clear_buffer_defrag(path->nodes[*level]);
126 out: 129 out:
127 free_extent_buffer(path->nodes[*level]); 130 free_extent_buffer(path->nodes[*level]);
128 path->nodes[*level] = NULL; 131 path->nodes[*level] = NULL;
129 *level += 1; 132 *level += 1;
130 WARN_ON(ret && ret != -EAGAIN); 133 WARN_ON(ret && ret != -EAGAIN);
131 return ret; 134 return ret;
132 } 135 }
133 136
134 static int defrag_walk_up(struct btrfs_trans_handle *trans, 137 static int defrag_walk_up(struct btrfs_trans_handle *trans,
135 struct btrfs_root *root, 138 struct btrfs_root *root,
136 struct btrfs_path *path, int *level, 139 struct btrfs_path *path, int *level,
137 int cache_only) 140 int cache_only)
138 { 141 {
139 int i; 142 int i;
140 int slot; 143 int slot;
141 struct extent_buffer *node; 144 struct extent_buffer *node;
142 145
143 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { 146 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
144 slot = path->slots[i]; 147 slot = path->slots[i];
145 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { 148 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
146 path->slots[i]++; 149 path->slots[i]++;
147 *level = i; 150 *level = i;
148 node = path->nodes[i]; 151 node = path->nodes[i];
149 WARN_ON(i == 0); 152 WARN_ON(i == 0);
150 btrfs_node_key_to_cpu(node, &root->defrag_progress, 153 btrfs_node_key_to_cpu(node, &root->defrag_progress,
151 path->slots[i]); 154 path->slots[i]);
152 root->defrag_level = i; 155 root->defrag_level = i;
153 return 0; 156 return 0;
154 } else { 157 } else {
155 btrfs_clear_buffer_defrag(path->nodes[*level]); 158 btrfs_clear_buffer_defrag(path->nodes[*level]);
156 free_extent_buffer(path->nodes[*level]); 159 free_extent_buffer(path->nodes[*level]);
157 path->nodes[*level] = NULL; 160 path->nodes[*level] = NULL;
158 *level = i + 1; 161 *level = i + 1;
159 } 162 }
160 } 163 }
161 return 1; 164 return 1;
162 } 165 }
163 166
164 int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, 167 int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
165 struct btrfs_root *root, int cache_only) 168 struct btrfs_root *root, int cache_only)
166 { 169 {
167 struct btrfs_path *path = NULL; 170 struct btrfs_path *path = NULL;
168 struct extent_buffer *tmp; 171 struct extent_buffer *tmp;
169 int ret = 0; 172 int ret = 0;
170 int wret; 173 int wret;
171 int level; 174 int level;
172 int orig_level; 175 int orig_level;
173 int i; 176 int i;
174 int is_extent = 0; 177 int is_extent = 0;
175 u64 last_ret = 0; 178 u64 last_ret = 0;
176 179
177 if (root->fs_info->extent_root == root) 180 if (root->fs_info->extent_root == root)
178 is_extent = 1; 181 is_extent = 1;
179 182
180 if (root->ref_cows == 0 && !is_extent) 183 if (root->ref_cows == 0 && !is_extent)
181 goto out; 184 goto out;
182 185
183 if (btrfs_test_opt(root, SSD)) 186 if (btrfs_test_opt(root, SSD))
184 goto out; 187 goto out;
185 188
186 path = btrfs_alloc_path(); 189 path = btrfs_alloc_path();
187 if (!path) 190 if (!path)
188 return -ENOMEM; 191 return -ENOMEM;
189 192
190 level = btrfs_header_level(root->node); 193 level = btrfs_header_level(root->node);
191 orig_level = level; 194 orig_level = level;
192 195
193 if (level == 0) { 196 if (level == 0) {
194 goto out; 197 goto out;
195 } 198 }
196 if (root->defrag_progress.objectid == 0) { 199 if (root->defrag_progress.objectid == 0) {
197 extent_buffer_get(root->node); 200 extent_buffer_get(root->node);
198 ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp); 201 ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
199 BUG_ON(ret); 202 BUG_ON(ret);
200 path->nodes[level] = root->node; 203 path->nodes[level] = root->node;
201 path->slots[level] = 0; 204 path->slots[level] = 0;
202 if (is_extent) 205 if (is_extent)
203 btrfs_extent_post_op(trans, root); 206 btrfs_extent_post_op(trans, root);
204 } else { 207 } else {
205 level = root->defrag_level; 208 level = root->defrag_level;
206 path->lowest_level = level; 209 path->lowest_level = level;
207 wret = btrfs_search_slot(trans, root, &root->defrag_progress, 210 wret = btrfs_search_slot(trans, root, &root->defrag_progress,
208 path, 0, 1); 211 path, 0, 1);
209 212
210 if (is_extent) 213 if (is_extent)
211 btrfs_extent_post_op(trans, root); 214 btrfs_extent_post_op(trans, root);
212 215
213 if (wret < 0) { 216 if (wret < 0) {
214 ret = wret; 217 ret = wret;
215 goto out; 218 goto out;
216 } 219 }
217 220
218 while(level > 0 && !path->nodes[level]) 221 while(level > 0 && !path->nodes[level])
219 level--; 222 level--;
220 223
221 if (!path->nodes[level]) { 224 if (!path->nodes[level]) {
222 ret = 0; 225 ret = 0;
223 goto out; 226 goto out;
224 } 227 }
225 } 228 }
226 229
227 while(1) { 230 while(1) {
228 wret = defrag_walk_down(trans, root, path, &level, cache_only, 231 wret = defrag_walk_down(trans, root, path, &level, cache_only,
229 &last_ret); 232 &last_ret);
230 if (wret > 0) 233 if (wret > 0)
231 break; 234 break;
232 if (wret < 0) 235 if (wret < 0)
233 ret = wret; 236 ret = wret;
234 237
235 wret = defrag_walk_up(trans, root, path, &level, cache_only); 238 wret = defrag_walk_up(trans, root, path, &level, cache_only);
236 if (wret > 0) 239 if (wret > 0)
237 break; 240 break;
238 if (wret < 0) 241 if (wret < 0)
239 ret = wret; 242 ret = wret;
240 else 243 else
241 ret = -EAGAIN; 244 ret = -EAGAIN;
242 break; 245 break;
243 } 246 }
244 for (i = 0; i <= orig_level; i++) { 247 for (i = 0; i <= orig_level; i++) {
245 if (path->nodes[i]) { 248 if (path->nodes[i]) {
246 free_extent_buffer(path->nodes[i]); 249 free_extent_buffer(path->nodes[i]);
247 path->nodes[i] = NULL; 250 path->nodes[i] = NULL;
248 } 251 }
249 } 252 }
250 out: 253 out:
251 if (path) 254 if (path)
252 btrfs_free_path(path); 255 btrfs_free_path(path);
253 if (ret != -EAGAIN) { 256 if (ret != -EAGAIN) {
254 memset(&root->defrag_progress, 0, 257 memset(&root->defrag_progress, 0,
255 sizeof(root->defrag_progress)); 258 sizeof(root->defrag_progress));
256 } 259 }
257 return ret; 260 return ret;
258 } 261 }