98ff4fbcb3864177a00b7929052a856e98689245
[deliverable/linux.git] / fs / btrfs / disk-io.c
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
19 #include <linux/fs.h>
20 #include <linux/blkdev.h>
21 #include <linux/scatterlist.h>
22 #include <linux/swap.h>
23 #include <linux/radix-tree.h>
24 #include <linux/writeback.h>
25 #include <linux/buffer_head.h> // for block_sync_page
26 #include <linux/workqueue.h>
27 #include "crc32c.h"
28 #include "ctree.h"
29 #include "disk-io.h"
30 #include "transaction.h"
31 #include "btrfs_inode.h"
32 #include "volumes.h"
33 #include "print-tree.h"
34 #include "async-thread.h"
35
36 #if 0
37 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
38 {
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));
43 return 1;
44 }
45 return 0;
46 }
47 #endif
48
49 static struct extent_io_ops btree_extent_io_ops;
50 static void end_workqueue_fn(struct btrfs_work *work);
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;
58 int metadata;
59 struct list_head list;
60 struct btrfs_work work;
61 };
62
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;
70 struct btrfs_work work;
71 };
72
73 struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
74 size_t page_offset, u64 start, u64 len,
75 int create)
76 {
77 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
78 struct extent_map *em;
79 int ret;
80
81 spin_lock(&em_tree->lock);
82 em = lookup_extent_mapping(em_tree, start, len);
83 if (em) {
84 em->bdev =
85 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
86 spin_unlock(&em_tree->lock);
87 goto out;
88 }
89 spin_unlock(&em_tree->lock);
90
91 em = alloc_extent_map(GFP_NOFS);
92 if (!em) {
93 em = ERR_PTR(-ENOMEM);
94 goto out;
95 }
96 em->start = 0;
97 em->len = (u64)-1;
98 em->block_start = 0;
99 em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
100
101 spin_lock(&em_tree->lock);
102 ret = add_extent_mapping(em_tree, em);
103 if (ret == -EEXIST) {
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);
109 free_extent_map(em);
110 em = lookup_extent_mapping(em_tree, start, len);
111 if (em) {
112 printk("after failing, found %Lu %Lu %Lu\n",
113 em->start, em->len, em->block_start);
114 ret = 0;
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 }
124 ret = -EIO;
125 }
126 } else if (ret) {
127 free_extent_map(em);
128 em = NULL;
129 }
130 spin_unlock(&em_tree->lock);
131
132 if (ret)
133 em = ERR_PTR(ret);
134 out:
135 return em;
136 }
137
138 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
139 {
140 return btrfs_crc32c(seed, data, len);
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) {
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 */
190 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
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 "
197 "wanted %X found %X from_this_trans %d "
198 "level %d\n",
199 root->fs_info->sb->s_id,
200 buf->start, val, found, from_this_trans,
201 btrfs_header_level(buf));
202 return 1;
203 }
204 } else {
205 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
206 }
207 return 0;
208 }
209
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
237 static int btree_read_extent_buffer_pages(struct btrfs_root *root,
238 struct extent_buffer *eb,
239 u64 start, u64 parent_transid)
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);
250 if (!ret &&
251 !verify_parent_transid(io_tree, eb, parent_transid))
252 return ret;
253
254 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
255 eb->start, eb->len);
256 if (num_copies == 1)
257 return ret;
258
259 mirror_num++;
260 if (mirror_num > num_copies)
261 return ret;
262 }
263 return -EIO;
264 }
265
266 int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
267 {
268 struct extent_io_tree *tree;
269 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
270 u64 found_start;
271 int found_level;
272 unsigned long len;
273 struct extent_buffer *eb;
274 int ret;
275
276 tree = &BTRFS_I(page->mapping->host)->io_tree;
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);
287 ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE,
288 btrfs_header_generation(eb));
289 BUG_ON(ret);
290 btrfs_clear_buffer_defrag(eb);
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);
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;
308 }
309 found_level = btrfs_header_level(eb);
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);
313 csum_tree_block(root, eb, 0);
314 err:
315 free_extent_buffer(eb);
316 out:
317 return 0;
318 }
319
320 static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
321 {
322 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
323
324 csum_dirty_buffer(root, page);
325 return 0;
326 }
327
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;
337 int ret = 0;
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);
349
350 btrfs_clear_buffer_defrag(eb);
351 found_start = btrfs_header_bytenr(eb);
352 if (found_start != start) {
353 ret = -EIO;
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);
360 ret = -EIO;
361 goto err;
362 }
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 }
370 found_level = btrfs_header_level(eb);
371
372 ret = csum_tree_block(root, eb, 1);
373 if (ret)
374 ret = -EIO;
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:
382 return ret;
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;
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;
401 end_io_wq->error = err;
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);
405
406 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
407 return 0;
408 #endif
409 }
410
411 int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
412 int metadata)
413 {
414 struct end_io_wq *end_io_wq;
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;
421 end_io_wq->info = info;
422 end_io_wq->error = 0;
423 end_io_wq->bio = bio;
424 end_io_wq->metadata = metadata;
425
426 bio->bi_private = end_io_wq;
427 bio->bi_end_io = end_workqueue_bio;
428 return 0;
429 }
430
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
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;
459 async->work.func = run_one_async_submit;
460 async->work.flags = 0;
461 atomic_inc(&fs_info->nr_async_submits);
462 btrfs_queue_worker(&fs_info->workers, &async->work);
463 return 0;
464 }
465
466 static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
467 int mirror_num)
468 {
469 struct btrfs_root *root = BTRFS_I(inode)->root;
470 u64 offset;
471 int ret;
472
473 offset = bio->bi_sector << 9;
474
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 */
479 if (rw & (1 << BIO_RW)) {
480 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio,
481 mirror_num, 0);
482 }
483
484 /*
485 * called for a read, do the setup so that checksum validation
486 * can happen in the async kernel threads
487 */
488 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1);
489 BUG_ON(ret);
490
491 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1);
492 }
493
494 static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
495 int mirror_num)
496 {
497 /*
498 * kthread helpers are used to submit writes so that checksumming
499 * can happen in parallel across all CPUs
500 */
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
509 static int btree_writepage(struct page *page, struct writeback_control *wbc)
510 {
511 struct extent_io_tree *tree;
512 tree = &BTRFS_I(page->mapping->host)->io_tree;
513 return extent_write_full_page(tree, page, btree_get_extent, wbc);
514 }
515
516 static int btree_writepages(struct address_space *mapping,
517 struct writeback_control *wbc)
518 {
519 struct extent_io_tree *tree;
520 tree = &BTRFS_I(mapping->host)->io_tree;
521 if (wbc->sync_mode == WB_SYNC_NONE) {
522 u64 num_dirty;
523 u64 start = 0;
524 unsigned long thresh = 96 * 1024 * 1024;
525
526 if (wbc->for_kupdate)
527 return 0;
528
529 if (current_is_pdflush()) {
530 thresh = 96 * 1024 * 1024;
531 } else {
532 thresh = 8 * 1024 * 1024;
533 }
534 num_dirty = count_range_bits(tree, &start, (u64)-1,
535 thresh, EXTENT_DIRTY);
536 if (num_dirty < thresh) {
537 return 0;
538 }
539 }
540 return extent_writepages(tree, mapping, btree_get_extent, wbc);
541 }
542
543 int btree_readpage(struct file *file, struct page *page)
544 {
545 struct extent_io_tree *tree;
546 tree = &BTRFS_I(page->mapping->host)->io_tree;
547 return extent_read_full_page(tree, page, btree_get_extent);
548 }
549
550 static int btree_releasepage(struct page *page, gfp_t gfp_flags)
551 {
552 struct extent_io_tree *tree;
553 struct extent_map_tree *map;
554 int ret;
555
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 }
562 tree = &BTRFS_I(page->mapping->host)->io_tree;
563 map = &BTRFS_I(page->mapping->host)->extent_tree;
564 ret = try_release_extent_state(map, tree, page, gfp_flags);
565 if (ret == 1) {
566 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
567 ClearPagePrivate(page);
568 set_page_private(page, 0);
569 page_cache_release(page);
570 }
571 return ret;
572 }
573
574 static void btree_invalidatepage(struct page *page, unsigned long offset)
575 {
576 struct extent_io_tree *tree;
577 tree = &BTRFS_I(page->mapping->host)->io_tree;
578 extent_invalidatepage(tree, page, offset);
579 btree_releasepage(page, GFP_NOFS);
580 if (PagePrivate(page)) {
581 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
582 ClearPagePrivate(page);
583 set_page_private(page, 0);
584 page_cache_release(page);
585 }
586 }
587
588 #if 0
589 static int btree_writepage(struct page *page, struct writeback_control *wbc)
590 {
591 struct buffer_head *bh;
592 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
593 struct buffer_head *head;
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);
605 return block_write_full_page(page, btree_get_block, wbc);
606 }
607 #endif
608
609 static struct address_space_operations btree_aops = {
610 .readpage = btree_readpage,
611 .writepage = btree_writepage,
612 .writepages = btree_writepages,
613 .releasepage = btree_releasepage,
614 .invalidatepage = btree_invalidatepage,
615 .sync_page = block_sync_page,
616 };
617
618 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
619 u64 parent_transid)
620 {
621 struct extent_buffer *buf = NULL;
622 struct inode *btree_inode = root->fs_info->btree_inode;
623 int ret = 0;
624
625 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
626 if (!buf)
627 return 0;
628 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
629 buf, 0, 0, btree_get_extent, 0);
630 free_extent_buffer(buf);
631 return ret;
632 }
633
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,
657 u32 blocksize, u64 parent_transid)
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;
669
670 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
671
672 if (ret == 0) {
673 buf->flags |= EXTENT_UPTODATE;
674 }
675 return buf;
676
677 }
678
679 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
680 struct extent_buffer *buf)
681 {
682 struct inode *btree_inode = root->fs_info->btree_inode;
683 if (btrfs_header_generation(buf) ==
684 root->fs_info->running_transaction->transid)
685 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
686 buf);
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;
694 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
695 buf);
696 return 0;
697 }
698
699 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
700 u32 stripesize, struct btrfs_root *root,
701 struct btrfs_fs_info *fs_info,
702 u64 objectid)
703 {
704 root->node = NULL;
705 root->inode = NULL;
706 root->commit_root = NULL;
707 root->sectorsize = sectorsize;
708 root->nodesize = nodesize;
709 root->leafsize = leafsize;
710 root->stripesize = stripesize;
711 root->ref_cows = 0;
712 root->track_dirty = 0;
713
714 root->fs_info = fs_info;
715 root->objectid = objectid;
716 root->last_trans = 0;
717 root->highest_inode = 0;
718 root->last_inode_alloc = 0;
719 root->name = NULL;
720 root->in_sysfs = 0;
721
722 INIT_LIST_HEAD(&root->dirty_list);
723 memset(&root->root_key, 0, sizeof(root->root_key));
724 memset(&root->root_item, 0, sizeof(root->root_item));
725 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
726 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
727 init_completion(&root->kobj_unregister);
728 root->defrag_running = 0;
729 root->defrag_level = 0;
730 root->root_key.objectid = objectid;
731 return 0;
732 }
733
734 static int find_and_setup_root(struct btrfs_root *tree_root,
735 struct btrfs_fs_info *fs_info,
736 u64 objectid,
737 struct btrfs_root *root)
738 {
739 int ret;
740 u32 blocksize;
741
742 __setup_root(tree_root->nodesize, tree_root->leafsize,
743 tree_root->sectorsize, tree_root->stripesize,
744 root, fs_info, objectid);
745 ret = btrfs_find_last_root(tree_root, objectid,
746 &root->root_item, &root->root_key);
747 BUG_ON(ret);
748
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),
751 blocksize, 0);
752 BUG_ON(!root->node);
753 return 0;
754 }
755
756 struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
757 struct btrfs_key *location)
758 {
759 struct btrfs_root *root;
760 struct btrfs_root *tree_root = fs_info->tree_root;
761 struct btrfs_path *path;
762 struct extent_buffer *l;
763 u64 highest_inode;
764 u32 blocksize;
765 int ret = 0;
766
767 root = kzalloc(sizeof(*root), GFP_NOFS);
768 if (!root)
769 return ERR_PTR(-ENOMEM);
770 if (location->offset == (u64)-1) {
771 ret = find_and_setup_root(tree_root, fs_info,
772 location->objectid, root);
773 if (ret) {
774 kfree(root);
775 return ERR_PTR(ret);
776 }
777 goto insert;
778 }
779
780 __setup_root(tree_root->nodesize, tree_root->leafsize,
781 tree_root->sectorsize, tree_root->stripesize,
782 root, fs_info, location->objectid);
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) {
788 if (ret > 0)
789 ret = -ENOENT;
790 goto out;
791 }
792 l = path->nodes[0];
793 read_extent_buffer(l, &root->root_item,
794 btrfs_item_ptr_offset(l, path->slots[0]),
795 sizeof(root->root_item));
796 memcpy(&root->root_key, location, sizeof(*location));
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 }
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),
807 blocksize, 0);
808 BUG_ON(!root->node);
809 insert:
810 root->ref_cows = 1;
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
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
834 struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
835 struct btrfs_key *location)
836 {
837 struct btrfs_root *root;
838 int ret;
839
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;
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;
848
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;
857 ret = radix_tree_insert(&fs_info->fs_roots_radix,
858 (unsigned long)root->root_key.objectid,
859 root);
860 if (ret) {
861 free_extent_buffer(root->node);
862 kfree(root);
863 return ERR_PTR(ret);
864 }
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;
882
883 if (root->in_sysfs)
884 return root;
885
886 ret = btrfs_set_root_name(root, name, namelen);
887 if (ret) {
888 free_extent_buffer(root->node);
889 kfree(root);
890 return ERR_PTR(ret);
891 }
892
893 ret = btrfs_sysfs_add_root(root);
894 if (ret) {
895 free_extent_buffer(root->node);
896 kfree(root->name);
897 kfree(root);
898 return ERR_PTR(ret);
899 }
900 root->in_sysfs = 1;
901 return root;
902 }
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
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;
926 int limit = 256 * info->fs_devices->open_devices;
927 struct list_head *cur;
928 struct btrfs_device *device;
929 struct backing_dev_info *bdi;
930
931 if ((bdi_bits & (1 << BDI_write_congested)) &&
932 atomic_read(&info->nr_async_submits) > limit) {
933 return 1;
934 }
935
936 list_for_each(cur, &info->fs_devices->devices) {
937 device = list_entry(cur, struct btrfs_device, dev_list);
938 if (!device->bdev)
939 continue;
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
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
969 void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
970 {
971 struct inode *inode;
972 struct extent_map_tree *em_tree;
973 struct extent_map *em;
974 struct address_space *mapping;
975 u64 offset;
976
977 /* the generic O_DIRECT read code does this */
978 if (!page) {
979 __unplug_io_fn(bdi, page);
980 return;
981 }
982
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;
993 offset = page_offset(page);
994
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);
1006 }
1007
1008 static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
1009 {
1010 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1011 bdi_init(bdi);
1012 #endif
1013 bdi->ra_pages = default_backing_dev_info.ra_pages;
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
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
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)
1067 {
1068 struct bio *bio;
1069 struct end_io_wq *end_io_wq;
1070 struct btrfs_fs_info *fs_info;
1071 int error;
1072
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;
1076
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);
1093 #else
1094 bio_endio(bio, error);
1095 #endif
1096 }
1097
1098 struct btrfs_root *open_ctree(struct super_block *sb,
1099 struct btrfs_fs_devices *fs_devices,
1100 char *options)
1101 {
1102 u32 sectorsize;
1103 u32 nodesize;
1104 u32 leafsize;
1105 u32 blocksize;
1106 u32 stripesize;
1107 struct buffer_head *bh;
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);
1112 struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
1113 GFP_NOFS);
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);
1118 int ret;
1119 int err = -EINVAL;
1120 struct btrfs_super_block *disk_super;
1121
1122 if (!extent_root || !tree_root || !fs_info) {
1123 err = -ENOMEM;
1124 goto fail;
1125 }
1126 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
1127 INIT_LIST_HEAD(&fs_info->trans_list);
1128 INIT_LIST_HEAD(&fs_info->dead_roots);
1129 INIT_LIST_HEAD(&fs_info->hashers);
1130 spin_lock_init(&fs_info->hash_lock);
1131 spin_lock_init(&fs_info->delalloc_lock);
1132 spin_lock_init(&fs_info->new_trans_lock);
1133
1134 init_completion(&fs_info->kobj_unregister);
1135 fs_info->tree_root = tree_root;
1136 fs_info->extent_root = extent_root;
1137 fs_info->chunk_root = chunk_root;
1138 fs_info->dev_root = dev_root;
1139 fs_info->fs_devices = fs_devices;
1140 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
1141 INIT_LIST_HEAD(&fs_info->space_info);
1142 btrfs_mapping_init(&fs_info->mapping_tree);
1143 atomic_set(&fs_info->nr_async_submits, 0);
1144 fs_info->sb = sb;
1145 fs_info->max_extent = (u64)-1;
1146 fs_info->max_inline = 8192 * 1024;
1147 setup_bdi(fs_info, &fs_info->bdi);
1148 fs_info->btree_inode = new_inode(sb);
1149 fs_info->btree_inode->i_ino = 1;
1150 fs_info->btree_inode->i_nlink = 1;
1151
1152 sb->s_blocksize = 4096;
1153 sb->s_blocksize_bits = blksize_bits(4096);
1154
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;
1161 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
1162 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
1163
1164 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
1165 fs_info->btree_inode->i_mapping,
1166 GFP_NOFS);
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;
1171
1172 extent_io_tree_init(&fs_info->free_space_cache,
1173 fs_info->btree_inode->i_mapping, GFP_NOFS);
1174 extent_io_tree_init(&fs_info->block_group_cache,
1175 fs_info->btree_inode->i_mapping, GFP_NOFS);
1176 extent_io_tree_init(&fs_info->pinned_extents,
1177 fs_info->btree_inode->i_mapping, GFP_NOFS);
1178 extent_io_tree_init(&fs_info->pending_del,
1179 fs_info->btree_inode->i_mapping, GFP_NOFS);
1180 extent_io_tree_init(&fs_info->extent_ins,
1181 fs_info->btree_inode->i_mapping, GFP_NOFS);
1182 fs_info->do_barriers = 1;
1183
1184 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1185 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
1186 #else
1187 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
1188 #endif
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));
1192 insert_inode_hash(fs_info->btree_inode);
1193 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
1194
1195 mutex_init(&fs_info->trans_mutex);
1196 mutex_init(&fs_info->fs_mutex);
1197
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
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
1219 __setup_root(4096, 4096, 4096, 4096, tree_root,
1220 fs_info, BTRFS_ROOT_TREE_OBJECTID);
1221
1222
1223 bh = __bread(fs_devices->latest_bdev,
1224 BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
1225 if (!bh)
1226 goto fail_iput;
1227
1228 memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
1229 brelse(bh);
1230
1231 memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
1232
1233 disk_super = &fs_info->super_copy;
1234 if (!btrfs_super_root(disk_super))
1235 goto fail_sb_buffer;
1236
1237 err = btrfs_parse_options(tree_root, options);
1238 if (err)
1239 goto fail_sb_buffer;
1240
1241 err = -EINVAL;
1242 if (btrfs_super_num_devices(disk_super) > fs_devices->open_devices) {
1243 printk("Btrfs: wanted %llu devices, but found %llu\n",
1244 (unsigned long long)btrfs_super_num_devices(disk_super),
1245 (unsigned long long)fs_devices->open_devices);
1246 if (btrfs_test_opt(tree_root, DEGRADED))
1247 printk("continuing in degraded mode\n");
1248 else {
1249 goto fail_sb_buffer;
1250 }
1251 }
1252
1253 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
1254
1255 nodesize = btrfs_super_nodesize(disk_super);
1256 leafsize = btrfs_super_leafsize(disk_super);
1257 sectorsize = btrfs_super_sectorsize(disk_super);
1258 stripesize = btrfs_super_stripesize(disk_super);
1259 tree_root->nodesize = nodesize;
1260 tree_root->leafsize = leafsize;
1261 tree_root->sectorsize = sectorsize;
1262 tree_root->stripesize = stripesize;
1263
1264 sb->s_blocksize = sectorsize;
1265 sb->s_blocksize_bits = blksize_bits(sectorsize);
1266
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 }
1272
1273 mutex_lock(&fs_info->fs_mutex);
1274
1275 ret = btrfs_read_sys_array(tree_root);
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 }
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),
1290 blocksize, 0);
1291 BUG_ON(!chunk_root->node);
1292
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
1297 ret = btrfs_read_chunk_tree(chunk_root);
1298 BUG_ON(ret);
1299
1300 btrfs_close_extra_devices(fs_devices);
1301
1302 blocksize = btrfs_level_size(tree_root,
1303 btrfs_super_root_level(disk_super));
1304
1305
1306 tree_root->node = read_tree_block(tree_root,
1307 btrfs_super_root(disk_super),
1308 blocksize, 0);
1309 if (!tree_root->node)
1310 goto fail_sb_buffer;
1311
1312
1313 ret = find_and_setup_root(tree_root, fs_info,
1314 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
1315 if (ret)
1316 goto fail_tree_root;
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;
1325
1326 btrfs_read_block_groups(extent_root);
1327
1328 fs_info->generation = btrfs_super_generation(disk_super) + 1;
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
1333 mutex_unlock(&fs_info->fs_mutex);
1334 return tree_root;
1335
1336 fail_extent_root:
1337 free_extent_buffer(extent_root->node);
1338 fail_tree_root:
1339 free_extent_buffer(tree_root->node);
1340 fail_sys_array:
1341 mutex_unlock(&fs_info->fs_mutex);
1342 fail_sb_buffer:
1343 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
1344 fail_iput:
1345 iput(fs_info->btree_inode);
1346 btrfs_stop_workers(&fs_info->workers);
1347 btrfs_stop_workers(&fs_info->endio_workers);
1348 fail:
1349 btrfs_close_devices(fs_info->fs_devices);
1350 btrfs_mapping_tree_free(&fs_info->mapping_tree);
1351
1352 kfree(extent_root);
1353 kfree(tree_root);
1354 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1355 bdi_destroy(&fs_info->bdi);
1356 #endif
1357 kfree(fs_info);
1358 return ERR_PTR(err);
1359 }
1360
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 }
1373 /* note, we dont' set_buffer_write_io_error because we have
1374 * our own ways of dealing with the IO errors
1375 */
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;
1387 struct btrfs_super_block *sb;
1388 struct btrfs_dev_item *dev_item;
1389 struct buffer_head *bh;
1390 int ret;
1391 int do_barriers;
1392 int max_errors;
1393 int total_errors = 0;
1394 u32 crc;
1395 u64 flags;
1396
1397 max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1398 do_barriers = !btrfs_test_opt(root, NOBARRIER);
1399
1400 sb = &root->fs_info->super_for_commit;
1401 dev_item = &sb->dev_item;
1402 list_for_each(cur, head) {
1403 dev = list_entry(cur, struct btrfs_device, dev_list);
1404 if (!dev->bdev) {
1405 total_errors++;
1406 continue;
1407 }
1408 if (!dev->in_fs_metadata)
1409 continue;
1410
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,
1429 BTRFS_SUPER_INFO_SIZE);
1430
1431 memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
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 }
1453 if (ret)
1454 total_errors++;
1455 }
1456 if (total_errors > max_errors) {
1457 printk("btrfs: %d errors while writing supers\n", total_errors);
1458 BUG();
1459 }
1460 total_errors = 0;
1461
1462 list_for_each(cur, head) {
1463 dev = list_entry(cur, struct btrfs_device, dev_list);
1464 if (!dev->bdev)
1465 continue;
1466 if (!dev->in_fs_metadata)
1467 continue;
1468
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);
1483 if (!buffer_uptodate(bh))
1484 total_errors++;
1485 } else {
1486 total_errors++;
1487 }
1488
1489 }
1490 dev->pending_io = NULL;
1491 brelse(bh);
1492 }
1493 if (total_errors > max_errors) {
1494 printk("btrfs: %d errors while writing supers\n", total_errors);
1495 BUG();
1496 }
1497 return 0;
1498 }
1499
1500 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
1501 *root)
1502 {
1503 int ret;
1504
1505 ret = write_all_supers(root);
1506 return ret;
1507 }
1508
1509 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
1510 {
1511 radix_tree_delete(&fs_info->fs_roots_radix,
1512 (unsigned long)root->root_key.objectid);
1513 if (root->in_sysfs)
1514 btrfs_sysfs_del_root(root);
1515 if (root->inode)
1516 iput(root->inode);
1517 if (root->node)
1518 free_extent_buffer(root->node);
1519 if (root->commit_root)
1520 free_extent_buffer(root->commit_root);
1521 if (root->name)
1522 kfree(root->name);
1523 kfree(root);
1524 return 0;
1525 }
1526
1527 static int del_fs_roots(struct btrfs_fs_info *fs_info)
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;
1539 for (i = 0; i < ret; i++)
1540 btrfs_free_fs_root(fs_info, gang[i]);
1541 }
1542 return 0;
1543 }
1544
1545 int close_ctree(struct btrfs_root *root)
1546 {
1547 int ret;
1548 struct btrfs_trans_handle *trans;
1549 struct btrfs_fs_info *fs_info = root->fs_info;
1550
1551 fs_info->closing = 1;
1552 btrfs_transaction_flush_work(root);
1553 mutex_lock(&fs_info->fs_mutex);
1554 btrfs_defrag_dirty_roots(root->fs_info);
1555 trans = btrfs_start_transaction(root, 1);
1556 ret = btrfs_commit_transaction(trans, root);
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);
1561 BUG_ON(ret);
1562
1563 write_ctree_super(NULL, root);
1564 mutex_unlock(&fs_info->fs_mutex);
1565
1566 btrfs_transaction_flush_work(root);
1567
1568 if (fs_info->delalloc_bytes) {
1569 printk("btrfs: at unmount delalloc count %Lu\n",
1570 fs_info->delalloc_bytes);
1571 }
1572 if (fs_info->extent_root->node)
1573 free_extent_buffer(fs_info->extent_root->node);
1574
1575 if (fs_info->tree_root->node)
1576 free_extent_buffer(fs_info->tree_root->node);
1577
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
1584 btrfs_free_block_groups(root->fs_info);
1585 del_fs_roots(fs_info);
1586
1587 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
1588
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);
1595
1596 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
1597
1598 btrfs_stop_workers(&fs_info->workers);
1599 btrfs_stop_workers(&fs_info->endio_workers);
1600
1601 iput(fs_info->btree_inode);
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
1612 btrfs_close_devices(fs_info->fs_devices);
1613 btrfs_mapping_tree_free(&fs_info->mapping_tree);
1614
1615 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1616 bdi_destroy(&fs_info->bdi);
1617 #endif
1618
1619 kfree(fs_info->extent_root);
1620 kfree(fs_info->tree_root);
1621 kfree(fs_info->chunk_root);
1622 kfree(fs_info->dev_root);
1623 return 0;
1624 }
1625
1626 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1627 {
1628 int ret;
1629 struct inode *btree_inode = buf->first_page->mapping->host;
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;
1638 }
1639
1640 int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
1641 {
1642 struct inode *btree_inode = buf->first_page->mapping->host;
1643 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
1644 buf);
1645 }
1646
1647 void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
1648 {
1649 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1650 u64 transid = btrfs_header_generation(buf);
1651 struct inode *btree_inode = root->fs_info->btree_inode;
1652
1653 if (transid != root->fs_info->generation) {
1654 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
1655 (unsigned long long)buf->start,
1656 transid, root->fs_info->generation);
1657 WARN_ON(1);
1658 }
1659 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
1660 }
1661
1662 void btrfs_throttle(struct btrfs_root *root)
1663 {
1664 struct backing_dev_info *bdi;
1665
1666 bdi = &root->fs_info->bdi;
1667 if (root->fs_info->throttles && bdi_write_congested(bdi)) {
1668 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1669 congestion_wait(WRITE, HZ/20);
1670 #else
1671 blk_congestion_wait(WRITE, HZ/20);
1672 #endif
1673 }
1674 }
1675
1676 void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
1677 {
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 */
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(
1695 root->fs_info->btree_inode->i_mapping, 1);
1696 }
1697 return;
1698 }
1699
1700 void btrfs_set_buffer_defrag(struct extent_buffer *buf)
1701 {
1702 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1703 struct inode *btree_inode = root->fs_info->btree_inode;
1704 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
1705 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
1706 }
1707
1708 void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
1709 {
1710 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1711 struct inode *btree_inode = root->fs_info->btree_inode;
1712 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
1713 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
1714 GFP_NOFS);
1715 }
1716
1717 int btrfs_buffer_defrag(struct extent_buffer *buf)
1718 {
1719 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1720 struct inode *btree_inode = root->fs_info->btree_inode;
1721 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
1722 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
1723 }
1724
1725 int btrfs_buffer_defrag_done(struct extent_buffer *buf)
1726 {
1727 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1728 struct inode *btree_inode = root->fs_info->btree_inode;
1729 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
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 {
1736 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1737 struct inode *btree_inode = root->fs_info->btree_inode;
1738 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
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 {
1745 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1746 struct inode *btree_inode = root->fs_info->btree_inode;
1747 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
1748 buf->start, buf->start + buf->len - 1,
1749 EXTENT_DEFRAG, GFP_NOFS);
1750 }
1751
1752 int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
1753 {
1754 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1755 int ret;
1756 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
1757 if (ret == 0) {
1758 buf->flags |= EXTENT_UPTODATE;
1759 }
1760 return ret;
1761 }
1762
1763 static struct extent_io_ops btree_extent_io_ops = {
1764 .writepage_io_hook = btree_writepage_io_hook,
1765 .readpage_end_io_hook = btree_readpage_end_io_hook,
1766 .submit_bio_hook = btree_submit_bio_hook,
1767 /* note we're sharing with inode.c for the merge bio hook */
1768 .merge_bio_hook = btrfs_merge_bio_hook,
1769 };
This page took 0.068372 seconds and 5 git commands to generate.