]>
Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
2 | * Copyright (C) 2007 Oracle. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program; if not, write to the | |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
16 | * Boston, MA 021110-1307, USA. | |
17 | */ | |
18 | ||
e20d96d6 | 19 | #include <linux/fs.h> |
d98237b3 | 20 | #include <linux/blkdev.h> |
87cbda5c | 21 | #include <linux/scatterlist.h> |
22b0ebda | 22 | #include <linux/swap.h> |
0f7d52f4 | 23 | #include <linux/radix-tree.h> |
35b7e476 | 24 | #include <linux/writeback.h> |
5f39d397 | 25 | #include <linux/buffer_head.h> // for block_sync_page |
ce9adaa5 | 26 | #include <linux/workqueue.h> |
a5eb62e3 | 27 | #include "crc32c.h" |
eb60ceac CM |
28 | #include "ctree.h" |
29 | #include "disk-io.h" | |
e089f05c | 30 | #include "transaction.h" |
0f7d52f4 | 31 | #include "btrfs_inode.h" |
0b86a832 | 32 | #include "volumes.h" |
db94535d | 33 | #include "print-tree.h" |
8b712842 | 34 | #include "async-thread.h" |
eb60ceac | 35 | |
5f39d397 CM |
36 | #if 0 |
37 | static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf) | |
7eccb903 | 38 | { |
5f39d397 CM |
39 | if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) { |
40 | printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n", | |
41 | (unsigned long long)extent_buffer_blocknr(buf), | |
42 | (unsigned long long)btrfs_header_blocknr(buf)); | |
39279cc3 | 43 | return 1; |
d98237b3 | 44 | } |
9a8dd150 | 45 | return 0; |
eb60ceac | 46 | } |
5f39d397 | 47 | #endif |
eb60ceac | 48 | |
d1310b2e | 49 | static struct extent_io_ops btree_extent_io_ops; |
8b712842 | 50 | static void end_workqueue_fn(struct btrfs_work *work); |
ce9adaa5 CM |
51 | |
52 | struct end_io_wq { | |
53 | struct bio *bio; | |
54 | bio_end_io_t *end_io; | |
55 | void *private; | |
56 | struct btrfs_fs_info *info; | |
57 | int error; | |
22c59948 | 58 | int metadata; |
ce9adaa5 | 59 | struct list_head list; |
8b712842 | 60 | struct btrfs_work work; |
ce9adaa5 | 61 | }; |
0da5468f | 62 | |
44b8bd7e CM |
63 | struct async_submit_bio { |
64 | struct inode *inode; | |
65 | struct bio *bio; | |
66 | struct list_head list; | |
67 | extent_submit_bio_hook_t *submit_bio_hook; | |
68 | int rw; | |
69 | int mirror_num; | |
8b712842 | 70 | struct btrfs_work work; |
44b8bd7e CM |
71 | }; |
72 | ||
5f39d397 | 73 | struct extent_map *btree_get_extent(struct inode *inode, struct page *page, |
d1310b2e | 74 | size_t page_offset, u64 start, u64 len, |
5f39d397 | 75 | int create) |
7eccb903 | 76 | { |
5f39d397 CM |
77 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
78 | struct extent_map *em; | |
79 | int ret; | |
80 | ||
d1310b2e CM |
81 | spin_lock(&em_tree->lock); |
82 | em = lookup_extent_mapping(em_tree, start, len); | |
a061fc8d CM |
83 | if (em) { |
84 | em->bdev = | |
85 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; | |
86 | spin_unlock(&em_tree->lock); | |
5f39d397 | 87 | goto out; |
a061fc8d CM |
88 | } |
89 | spin_unlock(&em_tree->lock); | |
7b13b7b1 | 90 | |
5f39d397 CM |
91 | em = alloc_extent_map(GFP_NOFS); |
92 | if (!em) { | |
93 | em = ERR_PTR(-ENOMEM); | |
94 | goto out; | |
95 | } | |
96 | em->start = 0; | |
0afbaf8c | 97 | em->len = (u64)-1; |
5f39d397 | 98 | em->block_start = 0; |
a061fc8d | 99 | em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; |
d1310b2e CM |
100 | |
101 | spin_lock(&em_tree->lock); | |
5f39d397 CM |
102 | ret = add_extent_mapping(em_tree, em); |
103 | if (ret == -EEXIST) { | |
0afbaf8c CM |
104 | u64 failed_start = em->start; |
105 | u64 failed_len = em->len; | |
106 | ||
107 | printk("failed to insert %Lu %Lu -> %Lu into tree\n", | |
108 | em->start, em->len, em->block_start); | |
5f39d397 | 109 | free_extent_map(em); |
7b13b7b1 | 110 | em = lookup_extent_mapping(em_tree, start, len); |
0afbaf8c CM |
111 | if (em) { |
112 | printk("after failing, found %Lu %Lu %Lu\n", | |
113 | em->start, em->len, em->block_start); | |
7b13b7b1 | 114 | ret = 0; |
0afbaf8c CM |
115 | } else { |
116 | em = lookup_extent_mapping(em_tree, failed_start, | |
117 | failed_len); | |
118 | if (em) { | |
119 | printk("double failure lookup gives us " | |
120 | "%Lu %Lu -> %Lu\n", em->start, | |
121 | em->len, em->block_start); | |
122 | free_extent_map(em); | |
123 | } | |
7b13b7b1 | 124 | ret = -EIO; |
0afbaf8c | 125 | } |
5f39d397 | 126 | } else if (ret) { |
7b13b7b1 CM |
127 | free_extent_map(em); |
128 | em = NULL; | |
5f39d397 | 129 | } |
7b13b7b1 CM |
130 | spin_unlock(&em_tree->lock); |
131 | ||
132 | if (ret) | |
133 | em = ERR_PTR(ret); | |
5f39d397 CM |
134 | out: |
135 | return em; | |
7eccb903 CM |
136 | } |
137 | ||
19c00ddc CM |
138 | u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len) |
139 | { | |
a5eb62e3 | 140 | return btrfs_crc32c(seed, data, len); |
19c00ddc CM |
141 | } |
142 | ||
143 | void btrfs_csum_final(u32 crc, char *result) | |
144 | { | |
145 | *(__le32 *)result = ~cpu_to_le32(crc); | |
146 | } | |
147 | ||
148 | static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, | |
149 | int verify) | |
150 | { | |
151 | char result[BTRFS_CRC32_SIZE]; | |
152 | unsigned long len; | |
153 | unsigned long cur_len; | |
154 | unsigned long offset = BTRFS_CSUM_SIZE; | |
155 | char *map_token = NULL; | |
156 | char *kaddr; | |
157 | unsigned long map_start; | |
158 | unsigned long map_len; | |
159 | int err; | |
160 | u32 crc = ~(u32)0; | |
161 | ||
162 | len = buf->len - offset; | |
163 | while(len > 0) { | |
164 | err = map_private_extent_buffer(buf, offset, 32, | |
165 | &map_token, &kaddr, | |
166 | &map_start, &map_len, KM_USER0); | |
167 | if (err) { | |
168 | printk("failed to map extent buffer! %lu\n", | |
169 | offset); | |
170 | return 1; | |
171 | } | |
172 | cur_len = min(len, map_len - (offset - map_start)); | |
173 | crc = btrfs_csum_data(root, kaddr + offset - map_start, | |
174 | crc, cur_len); | |
175 | len -= cur_len; | |
176 | offset += cur_len; | |
177 | unmap_extent_buffer(buf, map_token, KM_USER0); | |
178 | } | |
179 | btrfs_csum_final(crc, result); | |
180 | ||
181 | if (verify) { | |
e4204ded CM |
182 | int from_this_trans = 0; |
183 | ||
184 | if (root->fs_info->running_transaction && | |
185 | btrfs_header_generation(buf) == | |
186 | root->fs_info->running_transaction->transid) | |
187 | from_this_trans = 1; | |
188 | ||
189 | /* FIXME, this is not good */ | |
63b10fc4 | 190 | if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) { |
e4204ded CM |
191 | u32 val; |
192 | u32 found = 0; | |
193 | memcpy(&found, result, BTRFS_CRC32_SIZE); | |
194 | ||
195 | read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE); | |
196 | printk("btrfs: %s checksum verify failed on %llu " | |
63b10fc4 CM |
197 | "wanted %X found %X from_this_trans %d " |
198 | "level %d\n", | |
19c00ddc | 199 | root->fs_info->sb->s_id, |
63b10fc4 CM |
200 | buf->start, val, found, from_this_trans, |
201 | btrfs_header_level(buf)); | |
19c00ddc CM |
202 | return 1; |
203 | } | |
204 | } else { | |
205 | write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE); | |
206 | } | |
207 | return 0; | |
208 | } | |
209 | ||
1259ab75 CM |
210 | static int verify_parent_transid(struct extent_io_tree *io_tree, |
211 | struct extent_buffer *eb, u64 parent_transid) | |
212 | { | |
213 | int ret; | |
214 | ||
215 | if (!parent_transid || btrfs_header_generation(eb) == parent_transid) | |
216 | return 0; | |
217 | ||
218 | lock_extent(io_tree, eb->start, eb->start + eb->len - 1, GFP_NOFS); | |
219 | if (extent_buffer_uptodate(io_tree, eb) && | |
220 | btrfs_header_generation(eb) == parent_transid) { | |
221 | ret = 0; | |
222 | goto out; | |
223 | } | |
224 | printk("parent transid verify failed on %llu wanted %llu found %llu\n", | |
225 | (unsigned long long)eb->start, | |
226 | (unsigned long long)parent_transid, | |
227 | (unsigned long long)btrfs_header_generation(eb)); | |
228 | ret = 1; | |
229 | out: | |
230 | clear_extent_buffer_uptodate(io_tree, eb); | |
231 | unlock_extent(io_tree, eb->start, eb->start + eb->len - 1, | |
232 | GFP_NOFS); | |
233 | return ret; | |
234 | ||
235 | } | |
236 | ||
f188591e CM |
237 | static int btree_read_extent_buffer_pages(struct btrfs_root *root, |
238 | struct extent_buffer *eb, | |
ca7a79ad | 239 | u64 start, u64 parent_transid) |
f188591e CM |
240 | { |
241 | struct extent_io_tree *io_tree; | |
242 | int ret; | |
243 | int num_copies = 0; | |
244 | int mirror_num = 0; | |
245 | ||
246 | io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree; | |
247 | while (1) { | |
248 | ret = read_extent_buffer_pages(io_tree, eb, start, 1, | |
249 | btree_get_extent, mirror_num); | |
1259ab75 CM |
250 | if (!ret && |
251 | !verify_parent_transid(io_tree, eb, parent_transid)) | |
f188591e | 252 | return ret; |
4235298e | 253 | |
f188591e CM |
254 | num_copies = btrfs_num_copies(&root->fs_info->mapping_tree, |
255 | eb->start, eb->len); | |
4235298e | 256 | if (num_copies == 1) |
f188591e | 257 | return ret; |
4235298e | 258 | |
f188591e | 259 | mirror_num++; |
4235298e | 260 | if (mirror_num > num_copies) |
f188591e | 261 | return ret; |
f188591e | 262 | } |
f188591e CM |
263 | return -EIO; |
264 | } | |
19c00ddc CM |
265 | |
266 | int csum_dirty_buffer(struct btrfs_root *root, struct page *page) | |
267 | { | |
d1310b2e | 268 | struct extent_io_tree *tree; |
35ebb934 | 269 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
19c00ddc CM |
270 | u64 found_start; |
271 | int found_level; | |
272 | unsigned long len; | |
273 | struct extent_buffer *eb; | |
f188591e CM |
274 | int ret; |
275 | ||
d1310b2e | 276 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
19c00ddc CM |
277 | |
278 | if (page->private == EXTENT_PAGE_PRIVATE) | |
279 | goto out; | |
280 | if (!page->private) | |
281 | goto out; | |
282 | len = page->private >> 2; | |
283 | if (len == 0) { | |
284 | WARN_ON(1); | |
285 | } | |
286 | eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS); | |
ca7a79ad CM |
287 | ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE, |
288 | btrfs_header_generation(eb)); | |
f188591e | 289 | BUG_ON(ret); |
e18e4809 | 290 | btrfs_clear_buffer_defrag(eb); |
19c00ddc CM |
291 | found_start = btrfs_header_bytenr(eb); |
292 | if (found_start != start) { | |
293 | printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n", | |
294 | start, found_start, len); | |
55c69072 CM |
295 | WARN_ON(1); |
296 | goto err; | |
297 | } | |
298 | if (eb->first_page != page) { | |
299 | printk("bad first page %lu %lu\n", eb->first_page->index, | |
300 | page->index); | |
301 | WARN_ON(1); | |
302 | goto err; | |
303 | } | |
304 | if (!PageUptodate(page)) { | |
305 | printk("csum not up to date page %lu\n", page->index); | |
306 | WARN_ON(1); | |
307 | goto err; | |
19c00ddc CM |
308 | } |
309 | found_level = btrfs_header_level(eb); | |
63b10fc4 CM |
310 | spin_lock(&root->fs_info->hash_lock); |
311 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); | |
312 | spin_unlock(&root->fs_info->hash_lock); | |
19c00ddc | 313 | csum_tree_block(root, eb, 0); |
55c69072 | 314 | err: |
19c00ddc CM |
315 | free_extent_buffer(eb); |
316 | out: | |
317 | return 0; | |
318 | } | |
319 | ||
0da5468f | 320 | static int btree_writepage_io_hook(struct page *page, u64 start, u64 end) |
d98237b3 | 321 | { |
19c00ddc | 322 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
19c00ddc CM |
323 | |
324 | csum_dirty_buffer(root, page); | |
0da5468f CM |
325 | return 0; |
326 | } | |
327 | ||
ce9adaa5 CM |
328 | int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end, |
329 | struct extent_state *state) | |
330 | { | |
331 | struct extent_io_tree *tree; | |
332 | u64 found_start; | |
333 | int found_level; | |
334 | unsigned long len; | |
335 | struct extent_buffer *eb; | |
336 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; | |
f188591e | 337 | int ret = 0; |
ce9adaa5 CM |
338 | |
339 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
340 | if (page->private == EXTENT_PAGE_PRIVATE) | |
341 | goto out; | |
342 | if (!page->private) | |
343 | goto out; | |
344 | len = page->private >> 2; | |
345 | if (len == 0) { | |
346 | WARN_ON(1); | |
347 | } | |
348 | eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS); | |
f188591e | 349 | |
ce9adaa5 CM |
350 | btrfs_clear_buffer_defrag(eb); |
351 | found_start = btrfs_header_bytenr(eb); | |
352 | if (found_start != start) { | |
f188591e | 353 | ret = -EIO; |
ce9adaa5 CM |
354 | goto err; |
355 | } | |
356 | if (eb->first_page != page) { | |
357 | printk("bad first page %lu %lu\n", eb->first_page->index, | |
358 | page->index); | |
359 | WARN_ON(1); | |
f188591e | 360 | ret = -EIO; |
ce9adaa5 CM |
361 | goto err; |
362 | } | |
1259ab75 CM |
363 | if (memcmp_extent_buffer(eb, root->fs_info->fsid, |
364 | (unsigned long)btrfs_header_fsid(eb), | |
365 | BTRFS_FSID_SIZE)) { | |
366 | printk("bad fsid on block %Lu\n", eb->start); | |
367 | ret = -EIO; | |
368 | goto err; | |
369 | } | |
ce9adaa5 CM |
370 | found_level = btrfs_header_level(eb); |
371 | ||
372 | ret = csum_tree_block(root, eb, 1); | |
f188591e CM |
373 | if (ret) |
374 | ret = -EIO; | |
ce9adaa5 CM |
375 | |
376 | end = min_t(u64, eb->len, PAGE_CACHE_SIZE); | |
377 | end = eb->start + end - 1; | |
378 | release_extent_buffer_tail_pages(eb); | |
379 | err: | |
380 | free_extent_buffer(eb); | |
381 | out: | |
f188591e | 382 | return ret; |
ce9adaa5 CM |
383 | } |
384 | ||
385 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) | |
386 | static void end_workqueue_bio(struct bio *bio, int err) | |
387 | #else | |
388 | static int end_workqueue_bio(struct bio *bio, | |
389 | unsigned int bytes_done, int err) | |
390 | #endif | |
391 | { | |
392 | struct end_io_wq *end_io_wq = bio->bi_private; | |
393 | struct btrfs_fs_info *fs_info; | |
ce9adaa5 CM |
394 | |
395 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) | |
396 | if (bio->bi_size) | |
397 | return 1; | |
398 | #endif | |
399 | ||
400 | fs_info = end_io_wq->info; | |
ce9adaa5 | 401 | end_io_wq->error = err; |
8b712842 CM |
402 | end_io_wq->work.func = end_workqueue_fn; |
403 | end_io_wq->work.flags = 0; | |
404 | btrfs_queue_worker(&fs_info->endio_workers, &end_io_wq->work); | |
ce9adaa5 CM |
405 | |
406 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) | |
407 | return 0; | |
408 | #endif | |
409 | } | |
410 | ||
22c59948 CM |
411 | int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, |
412 | int metadata) | |
0b86a832 | 413 | { |
ce9adaa5 | 414 | struct end_io_wq *end_io_wq; |
ce9adaa5 CM |
415 | end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS); |
416 | if (!end_io_wq) | |
417 | return -ENOMEM; | |
418 | ||
419 | end_io_wq->private = bio->bi_private; | |
420 | end_io_wq->end_io = bio->bi_end_io; | |
22c59948 | 421 | end_io_wq->info = info; |
ce9adaa5 CM |
422 | end_io_wq->error = 0; |
423 | end_io_wq->bio = bio; | |
22c59948 | 424 | end_io_wq->metadata = metadata; |
ce9adaa5 CM |
425 | |
426 | bio->bi_private = end_io_wq; | |
427 | bio->bi_end_io = end_workqueue_bio; | |
22c59948 CM |
428 | return 0; |
429 | } | |
430 | ||
8b712842 CM |
431 | static void run_one_async_submit(struct btrfs_work *work) |
432 | { | |
433 | struct btrfs_fs_info *fs_info; | |
434 | struct async_submit_bio *async; | |
435 | ||
436 | async = container_of(work, struct async_submit_bio, work); | |
437 | fs_info = BTRFS_I(async->inode)->root->fs_info; | |
438 | atomic_dec(&fs_info->nr_async_submits); | |
439 | async->submit_bio_hook(async->inode, async->rw, async->bio, | |
440 | async->mirror_num); | |
441 | kfree(async); | |
442 | } | |
443 | ||
44b8bd7e CM |
444 | int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, |
445 | int rw, struct bio *bio, int mirror_num, | |
446 | extent_submit_bio_hook_t *submit_bio_hook) | |
447 | { | |
448 | struct async_submit_bio *async; | |
449 | ||
450 | async = kmalloc(sizeof(*async), GFP_NOFS); | |
451 | if (!async) | |
452 | return -ENOMEM; | |
453 | ||
454 | async->inode = inode; | |
455 | async->rw = rw; | |
456 | async->bio = bio; | |
457 | async->mirror_num = mirror_num; | |
458 | async->submit_bio_hook = submit_bio_hook; | |
8b712842 CM |
459 | async->work.func = run_one_async_submit; |
460 | async->work.flags = 0; | |
cb03c743 | 461 | atomic_inc(&fs_info->nr_async_submits); |
8b712842 | 462 | btrfs_queue_worker(&fs_info->workers, &async->work); |
44b8bd7e CM |
463 | return 0; |
464 | } | |
465 | ||
466 | static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, | |
f188591e | 467 | int mirror_num) |
22c59948 CM |
468 | { |
469 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
470 | u64 offset; | |
471 | int ret; | |
472 | ||
473 | offset = bio->bi_sector << 9; | |
474 | ||
8b712842 CM |
475 | /* |
476 | * when we're called for a write, we're already in the async | |
477 | * submission context. Just jump ingo btrfs_map_bio | |
478 | */ | |
22c59948 | 479 | if (rw & (1 << BIO_RW)) { |
8b712842 CM |
480 | return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, |
481 | mirror_num, 0); | |
22c59948 CM |
482 | } |
483 | ||
8b712842 CM |
484 | /* |
485 | * called for a read, do the setup so that checksum validation | |
486 | * can happen in the async kernel threads | |
487 | */ | |
22c59948 CM |
488 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1); |
489 | BUG_ON(ret); | |
ce9adaa5 | 490 | |
8b712842 | 491 | return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1); |
0b86a832 CM |
492 | } |
493 | ||
44b8bd7e CM |
494 | static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
495 | int mirror_num) | |
496 | { | |
8b712842 CM |
497 | /* |
498 | * kthread helpers are used to submit writes so that checksumming | |
499 | * can happen in parallel across all CPUs | |
500 | */ | |
44b8bd7e CM |
501 | if (!(rw & (1 << BIO_RW))) { |
502 | return __btree_submit_bio_hook(inode, rw, bio, mirror_num); | |
503 | } | |
504 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, | |
505 | inode, rw, bio, mirror_num, | |
506 | __btree_submit_bio_hook); | |
507 | } | |
508 | ||
0da5468f CM |
509 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
510 | { | |
d1310b2e CM |
511 | struct extent_io_tree *tree; |
512 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
5f39d397 CM |
513 | return extent_write_full_page(tree, page, btree_get_extent, wbc); |
514 | } | |
0da5468f CM |
515 | |
516 | static int btree_writepages(struct address_space *mapping, | |
517 | struct writeback_control *wbc) | |
518 | { | |
d1310b2e CM |
519 | struct extent_io_tree *tree; |
520 | tree = &BTRFS_I(mapping->host)->io_tree; | |
d8d5f3e1 | 521 | if (wbc->sync_mode == WB_SYNC_NONE) { |
793955bc CM |
522 | u64 num_dirty; |
523 | u64 start = 0; | |
524 | unsigned long thresh = 96 * 1024 * 1024; | |
448d640b CM |
525 | |
526 | if (wbc->for_kupdate) | |
527 | return 0; | |
528 | ||
ca664626 CM |
529 | if (current_is_pdflush()) { |
530 | thresh = 96 * 1024 * 1024; | |
531 | } else { | |
532 | thresh = 8 * 1024 * 1024; | |
533 | } | |
1832a6d5 CM |
534 | num_dirty = count_range_bits(tree, &start, (u64)-1, |
535 | thresh, EXTENT_DIRTY); | |
793955bc CM |
536 | if (num_dirty < thresh) { |
537 | return 0; | |
538 | } | |
539 | } | |
0da5468f CM |
540 | return extent_writepages(tree, mapping, btree_get_extent, wbc); |
541 | } | |
542 | ||
5f39d397 CM |
543 | int btree_readpage(struct file *file, struct page *page) |
544 | { | |
d1310b2e CM |
545 | struct extent_io_tree *tree; |
546 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
5f39d397 CM |
547 | return extent_read_full_page(tree, page, btree_get_extent); |
548 | } | |
22b0ebda | 549 | |
70dec807 | 550 | static int btree_releasepage(struct page *page, gfp_t gfp_flags) |
5f39d397 | 551 | { |
d1310b2e CM |
552 | struct extent_io_tree *tree; |
553 | struct extent_map_tree *map; | |
5f39d397 | 554 | int ret; |
d98237b3 | 555 | |
3dd39914 CM |
556 | if (page_count(page) > 3) { |
557 | /* once for page->private, once for the caller, once | |
558 | * once for the page cache | |
559 | */ | |
560 | return 0; | |
561 | } | |
d1310b2e CM |
562 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
563 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
7b13b7b1 | 564 | ret = try_release_extent_state(map, tree, page, gfp_flags); |
5f39d397 | 565 | if (ret == 1) { |
728131d8 | 566 | invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE); |
5f39d397 CM |
567 | ClearPagePrivate(page); |
568 | set_page_private(page, 0); | |
569 | page_cache_release(page); | |
570 | } | |
d98237b3 CM |
571 | return ret; |
572 | } | |
573 | ||
5f39d397 | 574 | static void btree_invalidatepage(struct page *page, unsigned long offset) |
d98237b3 | 575 | { |
d1310b2e CM |
576 | struct extent_io_tree *tree; |
577 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
5f39d397 CM |
578 | extent_invalidatepage(tree, page, offset); |
579 | btree_releasepage(page, GFP_NOFS); | |
9ad6b7bc | 580 | if (PagePrivate(page)) { |
4ef64eae | 581 | invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE); |
9ad6b7bc CM |
582 | ClearPagePrivate(page); |
583 | set_page_private(page, 0); | |
584 | page_cache_release(page); | |
585 | } | |
d98237b3 CM |
586 | } |
587 | ||
5f39d397 | 588 | #if 0 |
d98237b3 | 589 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
ed2ff2cb | 590 | { |
87cbda5c | 591 | struct buffer_head *bh; |
0f7d52f4 | 592 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
87cbda5c | 593 | struct buffer_head *head; |
87cbda5c CM |
594 | if (!page_has_buffers(page)) { |
595 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, | |
596 | (1 << BH_Dirty)|(1 << BH_Uptodate)); | |
597 | } | |
598 | head = page_buffers(page); | |
599 | bh = head; | |
600 | do { | |
601 | if (buffer_dirty(bh)) | |
602 | csum_tree_block(root, bh, 0); | |
603 | bh = bh->b_this_page; | |
604 | } while (bh != head); | |
d98237b3 | 605 | return block_write_full_page(page, btree_get_block, wbc); |
ed2ff2cb | 606 | } |
5f39d397 | 607 | #endif |
eb60ceac | 608 | |
d98237b3 CM |
609 | static struct address_space_operations btree_aops = { |
610 | .readpage = btree_readpage, | |
611 | .writepage = btree_writepage, | |
0da5468f | 612 | .writepages = btree_writepages, |
5f39d397 CM |
613 | .releasepage = btree_releasepage, |
614 | .invalidatepage = btree_invalidatepage, | |
d98237b3 CM |
615 | .sync_page = block_sync_page, |
616 | }; | |
617 | ||
ca7a79ad CM |
618 | int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize, |
619 | u64 parent_transid) | |
090d1875 | 620 | { |
5f39d397 CM |
621 | struct extent_buffer *buf = NULL; |
622 | struct inode *btree_inode = root->fs_info->btree_inode; | |
de428b63 | 623 | int ret = 0; |
090d1875 | 624 | |
db94535d | 625 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
5f39d397 | 626 | if (!buf) |
090d1875 | 627 | return 0; |
d1310b2e | 628 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, |
f188591e | 629 | buf, 0, 0, btree_get_extent, 0); |
5f39d397 | 630 | free_extent_buffer(buf); |
de428b63 | 631 | return ret; |
090d1875 CM |
632 | } |
633 | ||
0999df54 CM |
634 | struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, |
635 | u64 bytenr, u32 blocksize) | |
636 | { | |
637 | struct inode *btree_inode = root->fs_info->btree_inode; | |
638 | struct extent_buffer *eb; | |
639 | eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree, | |
640 | bytenr, blocksize, GFP_NOFS); | |
641 | return eb; | |
642 | } | |
643 | ||
644 | struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, | |
645 | u64 bytenr, u32 blocksize) | |
646 | { | |
647 | struct inode *btree_inode = root->fs_info->btree_inode; | |
648 | struct extent_buffer *eb; | |
649 | ||
650 | eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree, | |
651 | bytenr, blocksize, NULL, GFP_NOFS); | |
652 | return eb; | |
653 | } | |
654 | ||
655 | ||
656 | struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, | |
ca7a79ad | 657 | u32 blocksize, u64 parent_transid) |
0999df54 CM |
658 | { |
659 | struct extent_buffer *buf = NULL; | |
660 | struct inode *btree_inode = root->fs_info->btree_inode; | |
661 | struct extent_io_tree *io_tree; | |
662 | int ret; | |
663 | ||
664 | io_tree = &BTRFS_I(btree_inode)->io_tree; | |
665 | ||
666 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); | |
667 | if (!buf) | |
668 | return NULL; | |
0999df54 | 669 | |
ca7a79ad | 670 | ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid); |
ce9adaa5 CM |
671 | |
672 | if (ret == 0) { | |
673 | buf->flags |= EXTENT_UPTODATE; | |
674 | } | |
5f39d397 | 675 | return buf; |
ce9adaa5 | 676 | |
eb60ceac CM |
677 | } |
678 | ||
e089f05c | 679 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
5f39d397 | 680 | struct extent_buffer *buf) |
ed2ff2cb | 681 | { |
5f39d397 | 682 | struct inode *btree_inode = root->fs_info->btree_inode; |
55c69072 CM |
683 | if (btrfs_header_generation(buf) == |
684 | root->fs_info->running_transaction->transid) | |
d1310b2e | 685 | clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, |
55c69072 | 686 | buf); |
5f39d397 CM |
687 | return 0; |
688 | } | |
689 | ||
690 | int wait_on_tree_block_writeback(struct btrfs_root *root, | |
691 | struct extent_buffer *buf) | |
692 | { | |
693 | struct inode *btree_inode = root->fs_info->btree_inode; | |
d1310b2e | 694 | wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree, |
5f39d397 CM |
695 | buf); |
696 | return 0; | |
697 | } | |
698 | ||
db94535d | 699 | static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, |
87ee04eb | 700 | u32 stripesize, struct btrfs_root *root, |
9f5fae2f | 701 | struct btrfs_fs_info *fs_info, |
e20d96d6 | 702 | u64 objectid) |
d97e63b6 | 703 | { |
cfaa7295 | 704 | root->node = NULL; |
0f7d52f4 | 705 | root->inode = NULL; |
a28ec197 | 706 | root->commit_root = NULL; |
db94535d CM |
707 | root->sectorsize = sectorsize; |
708 | root->nodesize = nodesize; | |
709 | root->leafsize = leafsize; | |
87ee04eb | 710 | root->stripesize = stripesize; |
123abc88 | 711 | root->ref_cows = 0; |
0b86a832 CM |
712 | root->track_dirty = 0; |
713 | ||
9f5fae2f | 714 | root->fs_info = fs_info; |
0f7d52f4 CM |
715 | root->objectid = objectid; |
716 | root->last_trans = 0; | |
1b05da2e CM |
717 | root->highest_inode = 0; |
718 | root->last_inode_alloc = 0; | |
58176a96 | 719 | root->name = NULL; |
4313b399 | 720 | root->in_sysfs = 0; |
0b86a832 CM |
721 | |
722 | INIT_LIST_HEAD(&root->dirty_list); | |
3768f368 CM |
723 | memset(&root->root_key, 0, sizeof(root->root_key)); |
724 | memset(&root->root_item, 0, sizeof(root->root_item)); | |
6702ed49 | 725 | memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); |
58176a96 JB |
726 | memset(&root->root_kobj, 0, sizeof(root->root_kobj)); |
727 | init_completion(&root->kobj_unregister); | |
6702ed49 CM |
728 | root->defrag_running = 0; |
729 | root->defrag_level = 0; | |
4d775673 | 730 | root->root_key.objectid = objectid; |
3768f368 CM |
731 | return 0; |
732 | } | |
733 | ||
db94535d | 734 | static int find_and_setup_root(struct btrfs_root *tree_root, |
9f5fae2f CM |
735 | struct btrfs_fs_info *fs_info, |
736 | u64 objectid, | |
e20d96d6 | 737 | struct btrfs_root *root) |
3768f368 CM |
738 | { |
739 | int ret; | |
db94535d | 740 | u32 blocksize; |
3768f368 | 741 | |
db94535d | 742 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
87ee04eb CM |
743 | tree_root->sectorsize, tree_root->stripesize, |
744 | root, fs_info, objectid); | |
3768f368 CM |
745 | ret = btrfs_find_last_root(tree_root, objectid, |
746 | &root->root_item, &root->root_key); | |
747 | BUG_ON(ret); | |
748 | ||
db94535d CM |
749 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); |
750 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), | |
ca7a79ad | 751 | blocksize, 0); |
3768f368 | 752 | BUG_ON(!root->node); |
d97e63b6 CM |
753 | return 0; |
754 | } | |
755 | ||
5eda7b5e CM |
756 | struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, |
757 | struct btrfs_key *location) | |
0f7d52f4 CM |
758 | { |
759 | struct btrfs_root *root; | |
760 | struct btrfs_root *tree_root = fs_info->tree_root; | |
761 | struct btrfs_path *path; | |
5f39d397 | 762 | struct extent_buffer *l; |
1b05da2e | 763 | u64 highest_inode; |
db94535d | 764 | u32 blocksize; |
0f7d52f4 CM |
765 | int ret = 0; |
766 | ||
5eda7b5e | 767 | root = kzalloc(sizeof(*root), GFP_NOFS); |
0cf6c620 | 768 | if (!root) |
0f7d52f4 | 769 | return ERR_PTR(-ENOMEM); |
0f7d52f4 | 770 | if (location->offset == (u64)-1) { |
db94535d | 771 | ret = find_and_setup_root(tree_root, fs_info, |
0f7d52f4 CM |
772 | location->objectid, root); |
773 | if (ret) { | |
0f7d52f4 CM |
774 | kfree(root); |
775 | return ERR_PTR(ret); | |
776 | } | |
777 | goto insert; | |
778 | } | |
779 | ||
db94535d | 780 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
87ee04eb CM |
781 | tree_root->sectorsize, tree_root->stripesize, |
782 | root, fs_info, location->objectid); | |
0f7d52f4 CM |
783 | |
784 | path = btrfs_alloc_path(); | |
785 | BUG_ON(!path); | |
786 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); | |
787 | if (ret != 0) { | |
0f7d52f4 CM |
788 | if (ret > 0) |
789 | ret = -ENOENT; | |
790 | goto out; | |
791 | } | |
5f39d397 CM |
792 | l = path->nodes[0]; |
793 | read_extent_buffer(l, &root->root_item, | |
794 | btrfs_item_ptr_offset(l, path->slots[0]), | |
0f7d52f4 | 795 | sizeof(root->root_item)); |
44b36eb2 | 796 | memcpy(&root->root_key, location, sizeof(*location)); |
0f7d52f4 CM |
797 | ret = 0; |
798 | out: | |
799 | btrfs_release_path(root, path); | |
800 | btrfs_free_path(path); | |
801 | if (ret) { | |
802 | kfree(root); | |
803 | return ERR_PTR(ret); | |
804 | } | |
db94535d CM |
805 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); |
806 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), | |
ca7a79ad | 807 | blocksize, 0); |
0f7d52f4 CM |
808 | BUG_ON(!root->node); |
809 | insert: | |
0f7d52f4 | 810 | root->ref_cows = 1; |
5eda7b5e CM |
811 | ret = btrfs_find_highest_inode(root, &highest_inode); |
812 | if (ret == 0) { | |
813 | root->highest_inode = highest_inode; | |
814 | root->last_inode_alloc = highest_inode; | |
815 | } | |
816 | return root; | |
817 | } | |
818 | ||
dc17ff8f CM |
819 | struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, |
820 | u64 root_objectid) | |
821 | { | |
822 | struct btrfs_root *root; | |
823 | ||
824 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID) | |
825 | return fs_info->tree_root; | |
826 | if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) | |
827 | return fs_info->extent_root; | |
828 | ||
829 | root = radix_tree_lookup(&fs_info->fs_roots_radix, | |
830 | (unsigned long)root_objectid); | |
831 | return root; | |
832 | } | |
833 | ||
edbd8d4e CM |
834 | struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, |
835 | struct btrfs_key *location) | |
5eda7b5e CM |
836 | { |
837 | struct btrfs_root *root; | |
838 | int ret; | |
839 | ||
edbd8d4e CM |
840 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) |
841 | return fs_info->tree_root; | |
842 | if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) | |
843 | return fs_info->extent_root; | |
8f18cf13 CM |
844 | if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID) |
845 | return fs_info->chunk_root; | |
846 | if (location->objectid == BTRFS_DEV_TREE_OBJECTID) | |
847 | return fs_info->dev_root; | |
edbd8d4e | 848 | |
5eda7b5e CM |
849 | root = radix_tree_lookup(&fs_info->fs_roots_radix, |
850 | (unsigned long)location->objectid); | |
851 | if (root) | |
852 | return root; | |
853 | ||
854 | root = btrfs_read_fs_root_no_radix(fs_info, location); | |
855 | if (IS_ERR(root)) | |
856 | return root; | |
2619ba1f CM |
857 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
858 | (unsigned long)root->root_key.objectid, | |
0f7d52f4 CM |
859 | root); |
860 | if (ret) { | |
5f39d397 | 861 | free_extent_buffer(root->node); |
0f7d52f4 CM |
862 | kfree(root); |
863 | return ERR_PTR(ret); | |
864 | } | |
edbd8d4e CM |
865 | ret = btrfs_find_dead_roots(fs_info->tree_root, |
866 | root->root_key.objectid, root); | |
867 | BUG_ON(ret); | |
868 | ||
869 | return root; | |
870 | } | |
871 | ||
872 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, | |
873 | struct btrfs_key *location, | |
874 | const char *name, int namelen) | |
875 | { | |
876 | struct btrfs_root *root; | |
877 | int ret; | |
878 | ||
879 | root = btrfs_read_fs_root_no_name(fs_info, location); | |
880 | if (!root) | |
881 | return NULL; | |
58176a96 | 882 | |
4313b399 CM |
883 | if (root->in_sysfs) |
884 | return root; | |
885 | ||
58176a96 JB |
886 | ret = btrfs_set_root_name(root, name, namelen); |
887 | if (ret) { | |
5f39d397 | 888 | free_extent_buffer(root->node); |
58176a96 JB |
889 | kfree(root); |
890 | return ERR_PTR(ret); | |
891 | } | |
892 | ||
893 | ret = btrfs_sysfs_add_root(root); | |
894 | if (ret) { | |
5f39d397 | 895 | free_extent_buffer(root->node); |
58176a96 JB |
896 | kfree(root->name); |
897 | kfree(root); | |
898 | return ERR_PTR(ret); | |
899 | } | |
4313b399 | 900 | root->in_sysfs = 1; |
0f7d52f4 CM |
901 | return root; |
902 | } | |
19c00ddc CM |
903 | #if 0 |
904 | static int add_hasher(struct btrfs_fs_info *info, char *type) { | |
905 | struct btrfs_hasher *hasher; | |
906 | ||
907 | hasher = kmalloc(sizeof(*hasher), GFP_NOFS); | |
908 | if (!hasher) | |
909 | return -ENOMEM; | |
910 | hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC); | |
911 | if (!hasher->hash_tfm) { | |
912 | kfree(hasher); | |
913 | return -EINVAL; | |
914 | } | |
915 | spin_lock(&info->hash_lock); | |
916 | list_add(&hasher->list, &info->hashers); | |
917 | spin_unlock(&info->hash_lock); | |
918 | return 0; | |
919 | } | |
920 | #endif | |
04160088 CM |
921 | |
922 | static int btrfs_congested_fn(void *congested_data, int bdi_bits) | |
923 | { | |
924 | struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; | |
925 | int ret = 0; | |
cb03c743 | 926 | int limit = 256 * info->fs_devices->open_devices; |
04160088 CM |
927 | struct list_head *cur; |
928 | struct btrfs_device *device; | |
929 | struct backing_dev_info *bdi; | |
930 | ||
cb03c743 CM |
931 | if ((bdi_bits & (1 << BDI_write_congested)) && |
932 | atomic_read(&info->nr_async_submits) > limit) { | |
933 | return 1; | |
934 | } | |
935 | ||
04160088 CM |
936 | list_for_each(cur, &info->fs_devices->devices) { |
937 | device = list_entry(cur, struct btrfs_device, dev_list); | |
dfe25020 CM |
938 | if (!device->bdev) |
939 | continue; | |
04160088 CM |
940 | bdi = blk_get_backing_dev_info(device->bdev); |
941 | if (bdi && bdi_congested(bdi, bdi_bits)) { | |
942 | ret = 1; | |
943 | break; | |
944 | } | |
945 | } | |
946 | return ret; | |
947 | } | |
948 | ||
38b66988 CM |
949 | /* |
950 | * this unplugs every device on the box, and it is only used when page | |
951 | * is null | |
952 | */ | |
953 | static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page) | |
954 | { | |
955 | struct list_head *cur; | |
956 | struct btrfs_device *device; | |
957 | struct btrfs_fs_info *info; | |
958 | ||
959 | info = (struct btrfs_fs_info *)bdi->unplug_io_data; | |
960 | list_for_each(cur, &info->fs_devices->devices) { | |
961 | device = list_entry(cur, struct btrfs_device, dev_list); | |
962 | bdi = blk_get_backing_dev_info(device->bdev); | |
963 | if (bdi->unplug_io_fn) { | |
964 | bdi->unplug_io_fn(bdi, page); | |
965 | } | |
966 | } | |
967 | } | |
968 | ||
04160088 CM |
969 | void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page) |
970 | { | |
38b66988 | 971 | struct inode *inode; |
f2d8d74d CM |
972 | struct extent_map_tree *em_tree; |
973 | struct extent_map *em; | |
bcbfce8a | 974 | struct address_space *mapping; |
38b66988 CM |
975 | u64 offset; |
976 | ||
bcbfce8a | 977 | /* the generic O_DIRECT read code does this */ |
38b66988 CM |
978 | if (!page) { |
979 | __unplug_io_fn(bdi, page); | |
980 | return; | |
981 | } | |
982 | ||
bcbfce8a CM |
983 | /* |
984 | * page->mapping may change at any time. Get a consistent copy | |
985 | * and use that for everything below | |
986 | */ | |
987 | smp_mb(); | |
988 | mapping = page->mapping; | |
989 | if (!mapping) | |
990 | return; | |
991 | ||
992 | inode = mapping->host; | |
38b66988 | 993 | offset = page_offset(page); |
04160088 | 994 | |
f2d8d74d CM |
995 | em_tree = &BTRFS_I(inode)->extent_tree; |
996 | spin_lock(&em_tree->lock); | |
997 | em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); | |
998 | spin_unlock(&em_tree->lock); | |
999 | if (!em) | |
1000 | return; | |
1001 | ||
1002 | offset = offset - em->start; | |
1003 | btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree, | |
1004 | em->block_start + offset, page); | |
1005 | free_extent_map(em); | |
04160088 CM |
1006 | } |
1007 | ||
1008 | static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) | |
1009 | { | |
51ebc0d3 | 1010 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
04160088 | 1011 | bdi_init(bdi); |
b248a415 | 1012 | #endif |
4575c9cc | 1013 | bdi->ra_pages = default_backing_dev_info.ra_pages; |
04160088 CM |
1014 | bdi->state = 0; |
1015 | bdi->capabilities = default_backing_dev_info.capabilities; | |
1016 | bdi->unplug_io_fn = btrfs_unplug_io_fn; | |
1017 | bdi->unplug_io_data = info; | |
1018 | bdi->congested_fn = btrfs_congested_fn; | |
1019 | bdi->congested_data = info; | |
1020 | return 0; | |
1021 | } | |
1022 | ||
ce9adaa5 CM |
1023 | static int bio_ready_for_csum(struct bio *bio) |
1024 | { | |
1025 | u64 length = 0; | |
1026 | u64 buf_len = 0; | |
1027 | u64 start = 0; | |
1028 | struct page *page; | |
1029 | struct extent_io_tree *io_tree = NULL; | |
1030 | struct btrfs_fs_info *info = NULL; | |
1031 | struct bio_vec *bvec; | |
1032 | int i; | |
1033 | int ret; | |
1034 | ||
1035 | bio_for_each_segment(bvec, bio, i) { | |
1036 | page = bvec->bv_page; | |
1037 | if (page->private == EXTENT_PAGE_PRIVATE) { | |
1038 | length += bvec->bv_len; | |
1039 | continue; | |
1040 | } | |
1041 | if (!page->private) { | |
1042 | length += bvec->bv_len; | |
1043 | continue; | |
1044 | } | |
1045 | length = bvec->bv_len; | |
1046 | buf_len = page->private >> 2; | |
1047 | start = page_offset(page) + bvec->bv_offset; | |
1048 | io_tree = &BTRFS_I(page->mapping->host)->io_tree; | |
1049 | info = BTRFS_I(page->mapping->host)->root->fs_info; | |
1050 | } | |
1051 | /* are we fully contained in this bio? */ | |
1052 | if (buf_len <= length) | |
1053 | return 1; | |
1054 | ||
1055 | ret = extent_range_uptodate(io_tree, start + length, | |
1056 | start + buf_len - 1); | |
1057 | if (ret == 1) | |
1058 | return ret; | |
1059 | return ret; | |
1060 | } | |
1061 | ||
8b712842 CM |
1062 | /* |
1063 | * called by the kthread helper functions to finally call the bio end_io | |
1064 | * functions. This is where read checksum verification actually happens | |
1065 | */ | |
1066 | static void end_workqueue_fn(struct btrfs_work *work) | |
ce9adaa5 | 1067 | { |
ce9adaa5 | 1068 | struct bio *bio; |
8b712842 CM |
1069 | struct end_io_wq *end_io_wq; |
1070 | struct btrfs_fs_info *fs_info; | |
ce9adaa5 | 1071 | int error; |
ce9adaa5 | 1072 | |
8b712842 CM |
1073 | end_io_wq = container_of(work, struct end_io_wq, work); |
1074 | bio = end_io_wq->bio; | |
1075 | fs_info = end_io_wq->info; | |
ce9adaa5 | 1076 | |
8b712842 CM |
1077 | /* metadata bios are special because the whole tree block must |
1078 | * be checksummed at once. This makes sure the entire block is in | |
1079 | * ram and up to date before trying to verify things. For | |
1080 | * blocksize <= pagesize, it is basically a noop | |
1081 | */ | |
1082 | if (end_io_wq->metadata && !bio_ready_for_csum(bio)) { | |
1083 | btrfs_queue_worker(&fs_info->endio_workers, | |
1084 | &end_io_wq->work); | |
1085 | return; | |
1086 | } | |
1087 | error = end_io_wq->error; | |
1088 | bio->bi_private = end_io_wq->private; | |
1089 | bio->bi_end_io = end_io_wq->end_io; | |
1090 | kfree(end_io_wq); | |
1091 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) | |
1092 | bio_endio(bio, bio->bi_size, error); | |
44b8bd7e | 1093 | #else |
8b712842 | 1094 | bio_endio(bio, error); |
44b8bd7e | 1095 | #endif |
44b8bd7e CM |
1096 | } |
1097 | ||
8a4b83cc | 1098 | struct btrfs_root *open_ctree(struct super_block *sb, |
dfe25020 CM |
1099 | struct btrfs_fs_devices *fs_devices, |
1100 | char *options) | |
2e635a27 | 1101 | { |
db94535d CM |
1102 | u32 sectorsize; |
1103 | u32 nodesize; | |
1104 | u32 leafsize; | |
1105 | u32 blocksize; | |
87ee04eb | 1106 | u32 stripesize; |
a061fc8d | 1107 | struct buffer_head *bh; |
e20d96d6 CM |
1108 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
1109 | GFP_NOFS); | |
1110 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), | |
1111 | GFP_NOFS); | |
8790d502 | 1112 | struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info), |
e20d96d6 | 1113 | GFP_NOFS); |
0b86a832 CM |
1114 | struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root), |
1115 | GFP_NOFS); | |
1116 | struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root), | |
1117 | GFP_NOFS); | |
eb60ceac | 1118 | int ret; |
e58ca020 | 1119 | int err = -EINVAL; |
2c90e5d6 | 1120 | struct btrfs_super_block *disk_super; |
8790d502 | 1121 | |
39279cc3 CM |
1122 | if (!extent_root || !tree_root || !fs_info) { |
1123 | err = -ENOMEM; | |
1124 | goto fail; | |
1125 | } | |
0f7d52f4 | 1126 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
8fd17795 | 1127 | INIT_LIST_HEAD(&fs_info->trans_list); |
facda1e7 | 1128 | INIT_LIST_HEAD(&fs_info->dead_roots); |
19c00ddc CM |
1129 | INIT_LIST_HEAD(&fs_info->hashers); |
1130 | spin_lock_init(&fs_info->hash_lock); | |
1832a6d5 | 1131 | spin_lock_init(&fs_info->delalloc_lock); |
cee36a03 | 1132 | spin_lock_init(&fs_info->new_trans_lock); |
19c00ddc | 1133 | |
58176a96 | 1134 | init_completion(&fs_info->kobj_unregister); |
9f5fae2f CM |
1135 | fs_info->tree_root = tree_root; |
1136 | fs_info->extent_root = extent_root; | |
0b86a832 CM |
1137 | fs_info->chunk_root = chunk_root; |
1138 | fs_info->dev_root = dev_root; | |
8a4b83cc | 1139 | fs_info->fs_devices = fs_devices; |
0b86a832 | 1140 | INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); |
6324fbf3 | 1141 | INIT_LIST_HEAD(&fs_info->space_info); |
0b86a832 | 1142 | btrfs_mapping_init(&fs_info->mapping_tree); |
cb03c743 | 1143 | atomic_set(&fs_info->nr_async_submits, 0); |
e20d96d6 | 1144 | fs_info->sb = sb; |
c59f8951 | 1145 | fs_info->max_extent = (u64)-1; |
6f568d35 | 1146 | fs_info->max_inline = 8192 * 1024; |
04160088 | 1147 | setup_bdi(fs_info, &fs_info->bdi); |
d98237b3 CM |
1148 | fs_info->btree_inode = new_inode(sb); |
1149 | fs_info->btree_inode->i_ino = 1; | |
2c90e5d6 | 1150 | fs_info->btree_inode->i_nlink = 1; |
0afbaf8c | 1151 | |
a061fc8d CM |
1152 | sb->s_blocksize = 4096; |
1153 | sb->s_blocksize_bits = blksize_bits(4096); | |
1154 | ||
0afbaf8c CM |
1155 | /* |
1156 | * we set the i_size on the btree inode to the max possible int. | |
1157 | * the real end of the address space is determined by all of | |
1158 | * the devices in the system | |
1159 | */ | |
1160 | fs_info->btree_inode->i_size = OFFSET_MAX; | |
d98237b3 | 1161 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; |
04160088 CM |
1162 | fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi; |
1163 | ||
d1310b2e | 1164 | extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, |
5f39d397 CM |
1165 | fs_info->btree_inode->i_mapping, |
1166 | GFP_NOFS); | |
d1310b2e CM |
1167 | extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree, |
1168 | GFP_NOFS); | |
1169 | ||
1170 | BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; | |
0da5468f | 1171 | |
d1310b2e | 1172 | extent_io_tree_init(&fs_info->free_space_cache, |
f510cfec | 1173 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
d1310b2e | 1174 | extent_io_tree_init(&fs_info->block_group_cache, |
96b5179d | 1175 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
d1310b2e | 1176 | extent_io_tree_init(&fs_info->pinned_extents, |
1a5bc167 | 1177 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
d1310b2e | 1178 | extent_io_tree_init(&fs_info->pending_del, |
1a5bc167 | 1179 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
d1310b2e | 1180 | extent_io_tree_init(&fs_info->extent_ins, |
1a5bc167 | 1181 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
e66f709b | 1182 | fs_info->do_barriers = 1; |
e18e4809 | 1183 | |
6da6abae CM |
1184 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
1185 | INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info); | |
1186 | #else | |
08607c1b | 1187 | INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner); |
6da6abae | 1188 | #endif |
0f7d52f4 CM |
1189 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
1190 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, | |
1191 | sizeof(struct btrfs_key)); | |
22b0ebda | 1192 | insert_inode_hash(fs_info->btree_inode); |
d98237b3 | 1193 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
39279cc3 | 1194 | |
79154b1b | 1195 | mutex_init(&fs_info->trans_mutex); |
d561c025 | 1196 | mutex_init(&fs_info->fs_mutex); |
3768f368 | 1197 | |
8b712842 CM |
1198 | /* we need to start all the end_io workers up front because the |
1199 | * queue work function gets called at interrupt time. The endio | |
1200 | * workers don't normally start IO, so some number of them <= the | |
1201 | * number of cpus is fine. They handle checksumming after a read. | |
1202 | * | |
1203 | * The other worker threads do start IO, so the max is larger than | |
1204 | * the number of CPUs. FIXME, tune this for huge machines | |
1205 | */ | |
1206 | btrfs_init_workers(&fs_info->workers, num_online_cpus() * 2); | |
1207 | btrfs_init_workers(&fs_info->endio_workers, num_online_cpus()); | |
1208 | btrfs_start_workers(&fs_info->workers, 1); | |
1209 | btrfs_start_workers(&fs_info->endio_workers, num_online_cpus()); | |
1210 | ||
19c00ddc CM |
1211 | #if 0 |
1212 | ret = add_hasher(fs_info, "crc32c"); | |
1213 | if (ret) { | |
1214 | printk("btrfs: failed hash setup, modprobe cryptomgr?\n"); | |
1215 | err = -ENOMEM; | |
1216 | goto fail_iput; | |
1217 | } | |
1218 | #endif | |
0b86a832 | 1219 | __setup_root(4096, 4096, 4096, 4096, tree_root, |
2c90e5d6 | 1220 | fs_info, BTRFS_ROOT_TREE_OBJECTID); |
7eccb903 | 1221 | |
d98237b3 | 1222 | |
a061fc8d CM |
1223 | bh = __bread(fs_devices->latest_bdev, |
1224 | BTRFS_SUPER_INFO_OFFSET / 4096, 4096); | |
1225 | if (!bh) | |
39279cc3 | 1226 | goto fail_iput; |
39279cc3 | 1227 | |
a061fc8d CM |
1228 | memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy)); |
1229 | brelse(bh); | |
5f39d397 | 1230 | |
a061fc8d | 1231 | memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE); |
0b86a832 | 1232 | |
5f39d397 | 1233 | disk_super = &fs_info->super_copy; |
0f7d52f4 | 1234 | if (!btrfs_super_root(disk_super)) |
39279cc3 | 1235 | goto fail_sb_buffer; |
0f7d52f4 | 1236 | |
edf24abe CH |
1237 | err = btrfs_parse_options(tree_root, options); |
1238 | if (err) | |
1239 | goto fail_sb_buffer; | |
dfe25020 | 1240 | |
edf24abe | 1241 | err = -EINVAL; |
a0af469b | 1242 | if (btrfs_super_num_devices(disk_super) > fs_devices->open_devices) { |
8a4b83cc CM |
1243 | printk("Btrfs: wanted %llu devices, but found %llu\n", |
1244 | (unsigned long long)btrfs_super_num_devices(disk_super), | |
a0af469b | 1245 | (unsigned long long)fs_devices->open_devices); |
dfe25020 CM |
1246 | if (btrfs_test_opt(tree_root, DEGRADED)) |
1247 | printk("continuing in degraded mode\n"); | |
1248 | else { | |
1249 | goto fail_sb_buffer; | |
1250 | } | |
8a4b83cc | 1251 | } |
dfe25020 | 1252 | |
4575c9cc CM |
1253 | fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); |
1254 | ||
db94535d CM |
1255 | nodesize = btrfs_super_nodesize(disk_super); |
1256 | leafsize = btrfs_super_leafsize(disk_super); | |
1257 | sectorsize = btrfs_super_sectorsize(disk_super); | |
87ee04eb | 1258 | stripesize = btrfs_super_stripesize(disk_super); |
db94535d CM |
1259 | tree_root->nodesize = nodesize; |
1260 | tree_root->leafsize = leafsize; | |
1261 | tree_root->sectorsize = sectorsize; | |
87ee04eb | 1262 | tree_root->stripesize = stripesize; |
a061fc8d CM |
1263 | |
1264 | sb->s_blocksize = sectorsize; | |
1265 | sb->s_blocksize_bits = blksize_bits(sectorsize); | |
db94535d | 1266 | |
39279cc3 CM |
1267 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, |
1268 | sizeof(disk_super->magic))) { | |
1269 | printk("btrfs: valid FS not found on %s\n", sb->s_id); | |
1270 | goto fail_sb_buffer; | |
1271 | } | |
19c00ddc | 1272 | |
0b86a832 | 1273 | mutex_lock(&fs_info->fs_mutex); |
0d81ba5d | 1274 | |
0b86a832 | 1275 | ret = btrfs_read_sys_array(tree_root); |
84eed90f CM |
1276 | if (ret) { |
1277 | printk("btrfs: failed to read the system array on %s\n", | |
1278 | sb->s_id); | |
1279 | goto fail_sys_array; | |
1280 | } | |
0b86a832 CM |
1281 | |
1282 | blocksize = btrfs_level_size(tree_root, | |
1283 | btrfs_super_chunk_root_level(disk_super)); | |
1284 | ||
1285 | __setup_root(nodesize, leafsize, sectorsize, stripesize, | |
1286 | chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); | |
1287 | ||
1288 | chunk_root->node = read_tree_block(chunk_root, | |
1289 | btrfs_super_chunk_root(disk_super), | |
ca7a79ad | 1290 | blocksize, 0); |
0b86a832 CM |
1291 | BUG_ON(!chunk_root->node); |
1292 | ||
e17cade2 CM |
1293 | read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, |
1294 | (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node), | |
1295 | BTRFS_UUID_SIZE); | |
1296 | ||
0b86a832 CM |
1297 | ret = btrfs_read_chunk_tree(chunk_root); |
1298 | BUG_ON(ret); | |
1299 | ||
dfe25020 CM |
1300 | btrfs_close_extra_devices(fs_devices); |
1301 | ||
db94535d CM |
1302 | blocksize = btrfs_level_size(tree_root, |
1303 | btrfs_super_root_level(disk_super)); | |
19c00ddc | 1304 | |
0b86a832 | 1305 | |
e20d96d6 | 1306 | tree_root->node = read_tree_block(tree_root, |
db94535d | 1307 | btrfs_super_root(disk_super), |
ca7a79ad | 1308 | blocksize, 0); |
39279cc3 CM |
1309 | if (!tree_root->node) |
1310 | goto fail_sb_buffer; | |
3768f368 | 1311 | |
db94535d CM |
1312 | |
1313 | ret = find_and_setup_root(tree_root, fs_info, | |
e20d96d6 | 1314 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
0b86a832 | 1315 | if (ret) |
39279cc3 | 1316 | goto fail_tree_root; |
0b86a832 CM |
1317 | extent_root->track_dirty = 1; |
1318 | ||
1319 | ret = find_and_setup_root(tree_root, fs_info, | |
1320 | BTRFS_DEV_TREE_OBJECTID, dev_root); | |
1321 | dev_root->track_dirty = 1; | |
1322 | ||
1323 | if (ret) | |
1324 | goto fail_extent_root; | |
3768f368 | 1325 | |
9078a3e1 CM |
1326 | btrfs_read_block_groups(extent_root); |
1327 | ||
0f7d52f4 | 1328 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
d18a2c44 CM |
1329 | fs_info->data_alloc_profile = (u64)-1; |
1330 | fs_info->metadata_alloc_profile = (u64)-1; | |
1331 | fs_info->system_alloc_profile = fs_info->metadata_alloc_profile; | |
1332 | ||
5be6f7f1 | 1333 | mutex_unlock(&fs_info->fs_mutex); |
0f7d52f4 | 1334 | return tree_root; |
39279cc3 | 1335 | |
0b86a832 CM |
1336 | fail_extent_root: |
1337 | free_extent_buffer(extent_root->node); | |
39279cc3 | 1338 | fail_tree_root: |
5f39d397 | 1339 | free_extent_buffer(tree_root->node); |
84eed90f CM |
1340 | fail_sys_array: |
1341 | mutex_unlock(&fs_info->fs_mutex); | |
39279cc3 | 1342 | fail_sb_buffer: |
2d2ae547 | 1343 | extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree); |
39279cc3 CM |
1344 | fail_iput: |
1345 | iput(fs_info->btree_inode); | |
8b712842 CM |
1346 | btrfs_stop_workers(&fs_info->workers); |
1347 | btrfs_stop_workers(&fs_info->endio_workers); | |
39279cc3 | 1348 | fail: |
dfe25020 | 1349 | btrfs_close_devices(fs_info->fs_devices); |
84eed90f CM |
1350 | btrfs_mapping_tree_free(&fs_info->mapping_tree); |
1351 | ||
39279cc3 CM |
1352 | kfree(extent_root); |
1353 | kfree(tree_root); | |
51ebc0d3 | 1354 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
2d2ae547 | 1355 | bdi_destroy(&fs_info->bdi); |
b248a415 | 1356 | #endif |
39279cc3 CM |
1357 | kfree(fs_info); |
1358 | return ERR_PTR(err); | |
eb60ceac CM |
1359 | } |
1360 | ||
f2984462 CM |
1361 | static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) |
1362 | { | |
1363 | char b[BDEVNAME_SIZE]; | |
1364 | ||
1365 | if (uptodate) { | |
1366 | set_buffer_uptodate(bh); | |
1367 | } else { | |
1368 | if (!buffer_eopnotsupp(bh) && printk_ratelimit()) { | |
1369 | printk(KERN_WARNING "lost page write due to " | |
1370 | "I/O error on %s\n", | |
1371 | bdevname(bh->b_bdev, b)); | |
1372 | } | |
1259ab75 CM |
1373 | /* note, we dont' set_buffer_write_io_error because we have |
1374 | * our own ways of dealing with the IO errors | |
1375 | */ | |
f2984462 CM |
1376 | clear_buffer_uptodate(bh); |
1377 | } | |
1378 | unlock_buffer(bh); | |
1379 | put_bh(bh); | |
1380 | } | |
1381 | ||
1382 | int write_all_supers(struct btrfs_root *root) | |
1383 | { | |
1384 | struct list_head *cur; | |
1385 | struct list_head *head = &root->fs_info->fs_devices->devices; | |
1386 | struct btrfs_device *dev; | |
a061fc8d | 1387 | struct btrfs_super_block *sb; |
f2984462 CM |
1388 | struct btrfs_dev_item *dev_item; |
1389 | struct buffer_head *bh; | |
1390 | int ret; | |
1391 | int do_barriers; | |
a236aed1 CM |
1392 | int max_errors; |
1393 | int total_errors = 0; | |
a061fc8d CM |
1394 | u32 crc; |
1395 | u64 flags; | |
f2984462 | 1396 | |
a236aed1 | 1397 | max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1; |
f2984462 CM |
1398 | do_barriers = !btrfs_test_opt(root, NOBARRIER); |
1399 | ||
a061fc8d CM |
1400 | sb = &root->fs_info->super_for_commit; |
1401 | dev_item = &sb->dev_item; | |
f2984462 CM |
1402 | list_for_each(cur, head) { |
1403 | dev = list_entry(cur, struct btrfs_device, dev_list); | |
dfe25020 CM |
1404 | if (!dev->bdev) { |
1405 | total_errors++; | |
1406 | continue; | |
1407 | } | |
1408 | if (!dev->in_fs_metadata) | |
1409 | continue; | |
1410 | ||
a061fc8d CM |
1411 | btrfs_set_stack_device_type(dev_item, dev->type); |
1412 | btrfs_set_stack_device_id(dev_item, dev->devid); | |
1413 | btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes); | |
1414 | btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used); | |
1415 | btrfs_set_stack_device_io_align(dev_item, dev->io_align); | |
1416 | btrfs_set_stack_device_io_width(dev_item, dev->io_width); | |
1417 | btrfs_set_stack_device_sector_size(dev_item, dev->sector_size); | |
1418 | memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE); | |
1419 | flags = btrfs_super_flags(sb); | |
1420 | btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN); | |
1421 | ||
1422 | ||
1423 | crc = ~(u32)0; | |
1424 | crc = btrfs_csum_data(root, (char *)sb + BTRFS_CSUM_SIZE, crc, | |
1425 | BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); | |
1426 | btrfs_csum_final(crc, sb->csum); | |
1427 | ||
1428 | bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET / 4096, | |
f2984462 CM |
1429 | BTRFS_SUPER_INFO_SIZE); |
1430 | ||
a061fc8d | 1431 | memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); |
f2984462 CM |
1432 | dev->pending_io = bh; |
1433 | ||
1434 | get_bh(bh); | |
1435 | set_buffer_uptodate(bh); | |
1436 | lock_buffer(bh); | |
1437 | bh->b_end_io = btrfs_end_buffer_write_sync; | |
1438 | ||
1439 | if (do_barriers && dev->barriers) { | |
1440 | ret = submit_bh(WRITE_BARRIER, bh); | |
1441 | if (ret == -EOPNOTSUPP) { | |
1442 | printk("btrfs: disabling barriers on dev %s\n", | |
1443 | dev->name); | |
1444 | set_buffer_uptodate(bh); | |
1445 | dev->barriers = 0; | |
1446 | get_bh(bh); | |
1447 | lock_buffer(bh); | |
1448 | ret = submit_bh(WRITE, bh); | |
1449 | } | |
1450 | } else { | |
1451 | ret = submit_bh(WRITE, bh); | |
1452 | } | |
a236aed1 CM |
1453 | if (ret) |
1454 | total_errors++; | |
f2984462 | 1455 | } |
a236aed1 CM |
1456 | if (total_errors > max_errors) { |
1457 | printk("btrfs: %d errors while writing supers\n", total_errors); | |
1458 | BUG(); | |
1459 | } | |
1460 | total_errors = 0; | |
f2984462 CM |
1461 | |
1462 | list_for_each(cur, head) { | |
1463 | dev = list_entry(cur, struct btrfs_device, dev_list); | |
dfe25020 CM |
1464 | if (!dev->bdev) |
1465 | continue; | |
1466 | if (!dev->in_fs_metadata) | |
1467 | continue; | |
1468 | ||
f2984462 CM |
1469 | BUG_ON(!dev->pending_io); |
1470 | bh = dev->pending_io; | |
1471 | wait_on_buffer(bh); | |
1472 | if (!buffer_uptodate(dev->pending_io)) { | |
1473 | if (do_barriers && dev->barriers) { | |
1474 | printk("btrfs: disabling barriers on dev %s\n", | |
1475 | dev->name); | |
1476 | set_buffer_uptodate(bh); | |
1477 | get_bh(bh); | |
1478 | lock_buffer(bh); | |
1479 | dev->barriers = 0; | |
1480 | ret = submit_bh(WRITE, bh); | |
1481 | BUG_ON(ret); | |
1482 | wait_on_buffer(bh); | |
1259ab75 CM |
1483 | if (!buffer_uptodate(bh)) |
1484 | total_errors++; | |
f2984462 | 1485 | } else { |
a236aed1 | 1486 | total_errors++; |
f2984462 CM |
1487 | } |
1488 | ||
1489 | } | |
1490 | dev->pending_io = NULL; | |
1491 | brelse(bh); | |
1492 | } | |
a236aed1 CM |
1493 | if (total_errors > max_errors) { |
1494 | printk("btrfs: %d errors while writing supers\n", total_errors); | |
1495 | BUG(); | |
1496 | } | |
f2984462 CM |
1497 | return 0; |
1498 | } | |
1499 | ||
e089f05c | 1500 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
79154b1b | 1501 | *root) |
eb60ceac | 1502 | { |
e66f709b | 1503 | int ret; |
5f39d397 | 1504 | |
f2984462 | 1505 | ret = write_all_supers(root); |
5f39d397 | 1506 | return ret; |
cfaa7295 CM |
1507 | } |
1508 | ||
5eda7b5e | 1509 | int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) |
2619ba1f CM |
1510 | { |
1511 | radix_tree_delete(&fs_info->fs_roots_radix, | |
1512 | (unsigned long)root->root_key.objectid); | |
b99aa6cb CM |
1513 | if (root->in_sysfs) |
1514 | btrfs_sysfs_del_root(root); | |
2619ba1f CM |
1515 | if (root->inode) |
1516 | iput(root->inode); | |
1517 | if (root->node) | |
5f39d397 | 1518 | free_extent_buffer(root->node); |
2619ba1f | 1519 | if (root->commit_root) |
5f39d397 | 1520 | free_extent_buffer(root->commit_root); |
58176a96 JB |
1521 | if (root->name) |
1522 | kfree(root->name); | |
2619ba1f CM |
1523 | kfree(root); |
1524 | return 0; | |
1525 | } | |
1526 | ||
35b7e476 | 1527 | static int del_fs_roots(struct btrfs_fs_info *fs_info) |
0f7d52f4 CM |
1528 | { |
1529 | int ret; | |
1530 | struct btrfs_root *gang[8]; | |
1531 | int i; | |
1532 | ||
1533 | while(1) { | |
1534 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, | |
1535 | (void **)gang, 0, | |
1536 | ARRAY_SIZE(gang)); | |
1537 | if (!ret) | |
1538 | break; | |
2619ba1f | 1539 | for (i = 0; i < ret; i++) |
5eda7b5e | 1540 | btrfs_free_fs_root(fs_info, gang[i]); |
0f7d52f4 CM |
1541 | } |
1542 | return 0; | |
1543 | } | |
b4100d64 | 1544 | |
e20d96d6 | 1545 | int close_ctree(struct btrfs_root *root) |
cfaa7295 | 1546 | { |
3768f368 | 1547 | int ret; |
e089f05c | 1548 | struct btrfs_trans_handle *trans; |
0f7d52f4 | 1549 | struct btrfs_fs_info *fs_info = root->fs_info; |
e089f05c | 1550 | |
facda1e7 | 1551 | fs_info->closing = 1; |
08607c1b | 1552 | btrfs_transaction_flush_work(root); |
0f7d52f4 | 1553 | mutex_lock(&fs_info->fs_mutex); |
6702ed49 | 1554 | btrfs_defrag_dirty_roots(root->fs_info); |
79154b1b | 1555 | trans = btrfs_start_transaction(root, 1); |
54aa1f4d | 1556 | ret = btrfs_commit_transaction(trans, root); |
79154b1b CM |
1557 | /* run commit again to drop the original snapshot */ |
1558 | trans = btrfs_start_transaction(root, 1); | |
1559 | btrfs_commit_transaction(trans, root); | |
1560 | ret = btrfs_write_and_wait_transaction(NULL, root); | |
3768f368 | 1561 | BUG_ON(ret); |
d6bfde87 | 1562 | |
79154b1b | 1563 | write_ctree_super(NULL, root); |
0f7d52f4 CM |
1564 | mutex_unlock(&fs_info->fs_mutex); |
1565 | ||
9ad6b7bc CM |
1566 | btrfs_transaction_flush_work(root); |
1567 | ||
b0c68f8b CM |
1568 | if (fs_info->delalloc_bytes) { |
1569 | printk("btrfs: at unmount delalloc count %Lu\n", | |
1570 | fs_info->delalloc_bytes); | |
1571 | } | |
0f7d52f4 | 1572 | if (fs_info->extent_root->node) |
5f39d397 | 1573 | free_extent_buffer(fs_info->extent_root->node); |
f510cfec | 1574 | |
0f7d52f4 | 1575 | if (fs_info->tree_root->node) |
5f39d397 | 1576 | free_extent_buffer(fs_info->tree_root->node); |
f510cfec | 1577 | |
0b86a832 CM |
1578 | if (root->fs_info->chunk_root->node); |
1579 | free_extent_buffer(root->fs_info->chunk_root->node); | |
1580 | ||
1581 | if (root->fs_info->dev_root->node); | |
1582 | free_extent_buffer(root->fs_info->dev_root->node); | |
1583 | ||
9078a3e1 | 1584 | btrfs_free_block_groups(root->fs_info); |
0f7d52f4 | 1585 | del_fs_roots(fs_info); |
d10c5f31 CM |
1586 | |
1587 | filemap_write_and_wait(fs_info->btree_inode->i_mapping); | |
1588 | ||
d1310b2e CM |
1589 | extent_io_tree_empty_lru(&fs_info->free_space_cache); |
1590 | extent_io_tree_empty_lru(&fs_info->block_group_cache); | |
1591 | extent_io_tree_empty_lru(&fs_info->pinned_extents); | |
1592 | extent_io_tree_empty_lru(&fs_info->pending_del); | |
1593 | extent_io_tree_empty_lru(&fs_info->extent_ins); | |
1594 | extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree); | |
d10c5f31 | 1595 | |
db94535d | 1596 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); |
9ad6b7bc | 1597 | |
8b712842 CM |
1598 | btrfs_stop_workers(&fs_info->workers); |
1599 | btrfs_stop_workers(&fs_info->endio_workers); | |
d6bfde87 | 1600 | |
db94535d | 1601 | iput(fs_info->btree_inode); |
19c00ddc CM |
1602 | #if 0 |
1603 | while(!list_empty(&fs_info->hashers)) { | |
1604 | struct btrfs_hasher *hasher; | |
1605 | hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher, | |
1606 | hashers); | |
1607 | list_del(&hasher->hashers); | |
1608 | crypto_free_hash(&fs_info->hash_tfm); | |
1609 | kfree(hasher); | |
1610 | } | |
1611 | #endif | |
dfe25020 | 1612 | btrfs_close_devices(fs_info->fs_devices); |
0b86a832 | 1613 | btrfs_mapping_tree_free(&fs_info->mapping_tree); |
b248a415 | 1614 | |
51ebc0d3 | 1615 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
04160088 | 1616 | bdi_destroy(&fs_info->bdi); |
b248a415 | 1617 | #endif |
0b86a832 | 1618 | |
0f7d52f4 | 1619 | kfree(fs_info->extent_root); |
0f7d52f4 | 1620 | kfree(fs_info->tree_root); |
0b86a832 CM |
1621 | kfree(fs_info->chunk_root); |
1622 | kfree(fs_info->dev_root); | |
eb60ceac CM |
1623 | return 0; |
1624 | } | |
1625 | ||
1259ab75 | 1626 | int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid) |
5f39d397 | 1627 | { |
1259ab75 | 1628 | int ret; |
810191ff | 1629 | struct inode *btree_inode = buf->first_page->mapping->host; |
1259ab75 CM |
1630 | |
1631 | ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf); | |
1632 | if (!ret) | |
1633 | return ret; | |
1634 | ||
1635 | ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf, | |
1636 | parent_transid); | |
1637 | return !ret; | |
5f39d397 CM |
1638 | } |
1639 | ||
1640 | int btrfs_set_buffer_uptodate(struct extent_buffer *buf) | |
ccd467d6 | 1641 | { |
810191ff | 1642 | struct inode *btree_inode = buf->first_page->mapping->host; |
d1310b2e | 1643 | return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, |
5f39d397 CM |
1644 | buf); |
1645 | } | |
6702ed49 | 1646 | |
5f39d397 CM |
1647 | void btrfs_mark_buffer_dirty(struct extent_buffer *buf) |
1648 | { | |
810191ff | 1649 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
5f39d397 CM |
1650 | u64 transid = btrfs_header_generation(buf); |
1651 | struct inode *btree_inode = root->fs_info->btree_inode; | |
6702ed49 | 1652 | |
ccd467d6 CM |
1653 | if (transid != root->fs_info->generation) { |
1654 | printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n", | |
db94535d | 1655 | (unsigned long long)buf->start, |
ccd467d6 CM |
1656 | transid, root->fs_info->generation); |
1657 | WARN_ON(1); | |
1658 | } | |
d1310b2e | 1659 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf); |
eb60ceac CM |
1660 | } |
1661 | ||
e2008b61 CM |
1662 | void btrfs_throttle(struct btrfs_root *root) |
1663 | { | |
55c69072 CM |
1664 | struct backing_dev_info *bdi; |
1665 | ||
a061fc8d | 1666 | bdi = &root->fs_info->bdi; |
04005cc7 CM |
1667 | if (root->fs_info->throttles && bdi_write_congested(bdi)) { |
1668 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18) | |
55c69072 | 1669 | congestion_wait(WRITE, HZ/20); |
04005cc7 CM |
1670 | #else |
1671 | blk_congestion_wait(WRITE, HZ/20); | |
1672 | #endif | |
1673 | } | |
e2008b61 CM |
1674 | } |
1675 | ||
d3c2fdcf | 1676 | void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) |
35b7e476 | 1677 | { |
188de649 CM |
1678 | /* |
1679 | * looks as though older kernels can get into trouble with | |
1680 | * this code, they end up stuck in balance_dirty_pages forever | |
1681 | */ | |
d6bfde87 CM |
1682 | struct extent_io_tree *tree; |
1683 | u64 num_dirty; | |
1684 | u64 start = 0; | |
1685 | unsigned long thresh = 16 * 1024 * 1024; | |
1686 | tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree; | |
1687 | ||
1688 | if (current_is_pdflush()) | |
1689 | return; | |
1690 | ||
1691 | num_dirty = count_range_bits(tree, &start, (u64)-1, | |
1692 | thresh, EXTENT_DIRTY); | |
1693 | if (num_dirty > thresh) { | |
1694 | balance_dirty_pages_ratelimited_nr( | |
d7fc640e | 1695 | root->fs_info->btree_inode->i_mapping, 1); |
d6bfde87 | 1696 | } |
188de649 | 1697 | return; |
35b7e476 | 1698 | } |
6b80053d CM |
1699 | |
1700 | void btrfs_set_buffer_defrag(struct extent_buffer *buf) | |
1701 | { | |
810191ff | 1702 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1703 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1704 | set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start, |
6b80053d CM |
1705 | buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS); |
1706 | } | |
1707 | ||
1708 | void btrfs_set_buffer_defrag_done(struct extent_buffer *buf) | |
1709 | { | |
810191ff | 1710 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1711 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1712 | set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start, |
6b80053d CM |
1713 | buf->start + buf->len - 1, EXTENT_DEFRAG_DONE, |
1714 | GFP_NOFS); | |
1715 | } | |
1716 | ||
1717 | int btrfs_buffer_defrag(struct extent_buffer *buf) | |
1718 | { | |
810191ff | 1719 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1720 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1721 | return test_range_bit(&BTRFS_I(btree_inode)->io_tree, |
6b80053d CM |
1722 | buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0); |
1723 | } | |
1724 | ||
1725 | int btrfs_buffer_defrag_done(struct extent_buffer *buf) | |
1726 | { | |
810191ff | 1727 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1728 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1729 | return test_range_bit(&BTRFS_I(btree_inode)->io_tree, |
6b80053d CM |
1730 | buf->start, buf->start + buf->len - 1, |
1731 | EXTENT_DEFRAG_DONE, 0); | |
1732 | } | |
1733 | ||
1734 | int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf) | |
1735 | { | |
810191ff | 1736 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1737 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1738 | return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree, |
6b80053d CM |
1739 | buf->start, buf->start + buf->len - 1, |
1740 | EXTENT_DEFRAG_DONE, GFP_NOFS); | |
1741 | } | |
1742 | ||
1743 | int btrfs_clear_buffer_defrag(struct extent_buffer *buf) | |
1744 | { | |
810191ff | 1745 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1746 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1747 | return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree, |
6b80053d CM |
1748 | buf->start, buf->start + buf->len - 1, |
1749 | EXTENT_DEFRAG, GFP_NOFS); | |
1750 | } | |
1751 | ||
ca7a79ad | 1752 | int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) |
6b80053d | 1753 | { |
810191ff | 1754 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
ce9adaa5 | 1755 | int ret; |
ca7a79ad | 1756 | ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid); |
ce9adaa5 CM |
1757 | if (ret == 0) { |
1758 | buf->flags |= EXTENT_UPTODATE; | |
1759 | } | |
1760 | return ret; | |
6b80053d | 1761 | } |
0da5468f | 1762 | |
d1310b2e | 1763 | static struct extent_io_ops btree_extent_io_ops = { |
0da5468f | 1764 | .writepage_io_hook = btree_writepage_io_hook, |
ce9adaa5 | 1765 | .readpage_end_io_hook = btree_readpage_end_io_hook, |
0b86a832 | 1766 | .submit_bio_hook = btree_submit_bio_hook, |
239b14b3 CM |
1767 | /* note we're sharing with inode.c for the merge bio hook */ |
1768 | .merge_bio_hook = btrfs_merge_bio_hook, | |
0da5468f | 1769 | }; |