]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame_incremental - fs/btrfs/disk-io.c
Btrfs: Add support for labels in the super block
[mirror_ubuntu-artful-kernel.git] / fs / btrfs / disk-io.c
... / ...
CommitLineData
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
35#if 0
36static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
37{
38 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
39 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
40 (unsigned long long)extent_buffer_blocknr(buf),
41 (unsigned long long)btrfs_header_blocknr(buf));
42 return 1;
43 }
44 return 0;
45}
46#endif
47
48static struct extent_io_ops btree_extent_io_ops;
49static struct workqueue_struct *end_io_workqueue;
50static struct workqueue_struct *async_submit_workqueue;
51
52struct 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};
61
62struct async_submit_bio {
63 struct inode *inode;
64 struct bio *bio;
65 struct list_head list;
66 extent_submit_bio_hook_t *submit_bio_hook;
67 int rw;
68 int mirror_num;
69};
70
71struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
72 size_t page_offset, u64 start, u64 len,
73 int create)
74{
75 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
76 struct extent_map *em;
77 int ret;
78
79again:
80 spin_lock(&em_tree->lock);
81 em = lookup_extent_mapping(em_tree, start, len);
82 spin_unlock(&em_tree->lock);
83 if (em) {
84 goto out;
85 }
86 em = alloc_extent_map(GFP_NOFS);
87 if (!em) {
88 em = ERR_PTR(-ENOMEM);
89 goto out;
90 }
91 em->start = 0;
92 em->len = i_size_read(inode);
93 em->block_start = 0;
94 em->bdev = inode->i_sb->s_bdev;
95
96 spin_lock(&em_tree->lock);
97 ret = add_extent_mapping(em_tree, em);
98 spin_unlock(&em_tree->lock);
99
100 if (ret == -EEXIST) {
101 free_extent_map(em);
102 em = NULL;
103 goto again;
104 } else if (ret) {
105 em = ERR_PTR(ret);
106 }
107out:
108 return em;
109}
110
111u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
112{
113 return btrfs_crc32c(seed, data, len);
114}
115
116void btrfs_csum_final(u32 crc, char *result)
117{
118 *(__le32 *)result = ~cpu_to_le32(crc);
119}
120
121static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
122 int verify)
123{
124 char result[BTRFS_CRC32_SIZE];
125 unsigned long len;
126 unsigned long cur_len;
127 unsigned long offset = BTRFS_CSUM_SIZE;
128 char *map_token = NULL;
129 char *kaddr;
130 unsigned long map_start;
131 unsigned long map_len;
132 int err;
133 u32 crc = ~(u32)0;
134
135 len = buf->len - offset;
136 while(len > 0) {
137 err = map_private_extent_buffer(buf, offset, 32,
138 &map_token, &kaddr,
139 &map_start, &map_len, KM_USER0);
140 if (err) {
141 printk("failed to map extent buffer! %lu\n",
142 offset);
143 return 1;
144 }
145 cur_len = min(len, map_len - (offset - map_start));
146 crc = btrfs_csum_data(root, kaddr + offset - map_start,
147 crc, cur_len);
148 len -= cur_len;
149 offset += cur_len;
150 unmap_extent_buffer(buf, map_token, KM_USER0);
151 }
152 btrfs_csum_final(crc, result);
153
154 if (verify) {
155 int from_this_trans = 0;
156
157 if (root->fs_info->running_transaction &&
158 btrfs_header_generation(buf) ==
159 root->fs_info->running_transaction->transid)
160 from_this_trans = 1;
161
162 /* FIXME, this is not good */
163 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
164 u32 val;
165 u32 found = 0;
166 memcpy(&found, result, BTRFS_CRC32_SIZE);
167
168 read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
169 printk("btrfs: %s checksum verify failed on %llu "
170 "wanted %X found %X from_this_trans %d "
171 "level %d\n",
172 root->fs_info->sb->s_id,
173 buf->start, val, found, from_this_trans,
174 btrfs_header_level(buf));
175 return 1;
176 }
177 } else {
178 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
179 }
180 return 0;
181}
182
183static int btree_read_extent_buffer_pages(struct btrfs_root *root,
184 struct extent_buffer *eb,
185 u64 start)
186{
187 struct extent_io_tree *io_tree;
188 int ret;
189 int num_copies = 0;
190 int mirror_num = 0;
191
192 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
193 while (1) {
194 ret = read_extent_buffer_pages(io_tree, eb, start, 1,
195 btree_get_extent, mirror_num);
196 if (!ret) {
197 if (mirror_num)
198printk("good read %Lu mirror %d total %d\n", eb->start, mirror_num, num_copies);
199 return ret;
200 }
201 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
202 eb->start, eb->len);
203printk("failed to read %Lu mirror %d total %d\n", eb->start, mirror_num, num_copies);
204 if (num_copies == 1) {
205printk("reading %Lu failed only one copy\n", eb->start);
206 return ret;
207 }
208 mirror_num++;
209 if (mirror_num > num_copies) {
210printk("bailing at mirror %d of %d\n", mirror_num, num_copies);
211 return ret;
212 }
213 }
214printk("read extent buffer page last\n");
215 return -EIO;
216}
217
218int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
219{
220 struct extent_io_tree *tree;
221 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
222 u64 found_start;
223 int found_level;
224 unsigned long len;
225 struct extent_buffer *eb;
226 int ret;
227
228 tree = &BTRFS_I(page->mapping->host)->io_tree;
229
230 if (page->private == EXTENT_PAGE_PRIVATE)
231 goto out;
232 if (!page->private)
233 goto out;
234 len = page->private >> 2;
235 if (len == 0) {
236 WARN_ON(1);
237 }
238 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
239 ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE);
240 BUG_ON(ret);
241 btrfs_clear_buffer_defrag(eb);
242 found_start = btrfs_header_bytenr(eb);
243 if (found_start != start) {
244 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
245 start, found_start, len);
246 WARN_ON(1);
247 goto err;
248 }
249 if (eb->first_page != page) {
250 printk("bad first page %lu %lu\n", eb->first_page->index,
251 page->index);
252 WARN_ON(1);
253 goto err;
254 }
255 if (!PageUptodate(page)) {
256 printk("csum not up to date page %lu\n", page->index);
257 WARN_ON(1);
258 goto err;
259 }
260 found_level = btrfs_header_level(eb);
261 spin_lock(&root->fs_info->hash_lock);
262 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
263 spin_unlock(&root->fs_info->hash_lock);
264 csum_tree_block(root, eb, 0);
265err:
266 free_extent_buffer(eb);
267out:
268 return 0;
269}
270
271static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
272{
273 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
274
275 csum_dirty_buffer(root, page);
276 return 0;
277}
278
279int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
280 struct extent_state *state)
281{
282 struct extent_io_tree *tree;
283 u64 found_start;
284 int found_level;
285 unsigned long len;
286 struct extent_buffer *eb;
287 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
288 int ret = 0;
289
290 tree = &BTRFS_I(page->mapping->host)->io_tree;
291 if (page->private == EXTENT_PAGE_PRIVATE)
292 goto out;
293 if (!page->private)
294 goto out;
295 len = page->private >> 2;
296 if (len == 0) {
297 WARN_ON(1);
298 }
299 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
300
301 btrfs_clear_buffer_defrag(eb);
302 found_start = btrfs_header_bytenr(eb);
303 if (found_start != start) {
304printk("bad start on %Lu found %Lu\n", eb->start, found_start);
305 ret = -EIO;
306 goto err;
307 }
308 if (eb->first_page != page) {
309 printk("bad first page %lu %lu\n", eb->first_page->index,
310 page->index);
311 WARN_ON(1);
312 ret = -EIO;
313 goto err;
314 }
315 found_level = btrfs_header_level(eb);
316
317 ret = csum_tree_block(root, eb, 1);
318 if (ret)
319 ret = -EIO;
320
321 end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
322 end = eb->start + end - 1;
323 release_extent_buffer_tail_pages(eb);
324err:
325 free_extent_buffer(eb);
326out:
327 return ret;
328}
329
330#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
331static void end_workqueue_bio(struct bio *bio, int err)
332#else
333static int end_workqueue_bio(struct bio *bio,
334 unsigned int bytes_done, int err)
335#endif
336{
337 struct end_io_wq *end_io_wq = bio->bi_private;
338 struct btrfs_fs_info *fs_info;
339 unsigned long flags;
340
341#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
342 if (bio->bi_size)
343 return 1;
344#endif
345
346 fs_info = end_io_wq->info;
347 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
348 end_io_wq->error = err;
349 list_add_tail(&end_io_wq->list, &fs_info->end_io_work_list);
350 spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
351 queue_work(end_io_workqueue, &fs_info->end_io_work);
352
353#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
354 return 0;
355#endif
356}
357
358int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
359 int metadata)
360{
361 struct end_io_wq *end_io_wq;
362 end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
363 if (!end_io_wq)
364 return -ENOMEM;
365
366 end_io_wq->private = bio->bi_private;
367 end_io_wq->end_io = bio->bi_end_io;
368 end_io_wq->info = info;
369 end_io_wq->error = 0;
370 end_io_wq->bio = bio;
371 end_io_wq->metadata = metadata;
372
373 bio->bi_private = end_io_wq;
374 bio->bi_end_io = end_workqueue_bio;
375 return 0;
376}
377
378int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
379 int rw, struct bio *bio, int mirror_num,
380 extent_submit_bio_hook_t *submit_bio_hook)
381{
382 struct async_submit_bio *async;
383
384 /*
385 * inline writerback should stay inline, only hop to the async
386 * queue if we're pdflush
387 */
388 if (!current_is_pdflush())
389 return submit_bio_hook(inode, rw, bio, mirror_num);
390
391 async = kmalloc(sizeof(*async), GFP_NOFS);
392 if (!async)
393 return -ENOMEM;
394
395 async->inode = inode;
396 async->rw = rw;
397 async->bio = bio;
398 async->mirror_num = mirror_num;
399 async->submit_bio_hook = submit_bio_hook;
400
401 spin_lock(&fs_info->async_submit_work_lock);
402 list_add_tail(&async->list, &fs_info->async_submit_work_list);
403 spin_unlock(&fs_info->async_submit_work_lock);
404
405 queue_work(async_submit_workqueue, &fs_info->async_submit_work);
406 return 0;
407}
408
409static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
410 int mirror_num)
411{
412 struct btrfs_root *root = BTRFS_I(inode)->root;
413 u64 offset;
414 int ret;
415
416 offset = bio->bi_sector << 9;
417
418 if (rw & (1 << BIO_RW)) {
419 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num);
420 }
421
422 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1);
423 BUG_ON(ret);
424
425 if (offset == BTRFS_SUPER_INFO_OFFSET) {
426 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
427 submit_bio(rw, bio);
428 return 0;
429 }
430 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num);
431}
432
433static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
434 int mirror_num)
435{
436 if (!(rw & (1 << BIO_RW))) {
437 return __btree_submit_bio_hook(inode, rw, bio, mirror_num);
438 }
439 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
440 inode, rw, bio, mirror_num,
441 __btree_submit_bio_hook);
442}
443
444static int btree_writepage(struct page *page, struct writeback_control *wbc)
445{
446 struct extent_io_tree *tree;
447 tree = &BTRFS_I(page->mapping->host)->io_tree;
448 return extent_write_full_page(tree, page, btree_get_extent, wbc);
449}
450
451static int btree_writepages(struct address_space *mapping,
452 struct writeback_control *wbc)
453{
454 struct extent_io_tree *tree;
455 tree = &BTRFS_I(mapping->host)->io_tree;
456 if (wbc->sync_mode == WB_SYNC_NONE) {
457 u64 num_dirty;
458 u64 start = 0;
459 unsigned long thresh = 96 * 1024 * 1024;
460
461 if (wbc->for_kupdate)
462 return 0;
463
464 if (current_is_pdflush()) {
465 thresh = 96 * 1024 * 1024;
466 } else {
467 thresh = 8 * 1024 * 1024;
468 }
469 num_dirty = count_range_bits(tree, &start, (u64)-1,
470 thresh, EXTENT_DIRTY);
471 if (num_dirty < thresh) {
472 return 0;
473 }
474 }
475 return extent_writepages(tree, mapping, btree_get_extent, wbc);
476}
477
478int btree_readpage(struct file *file, struct page *page)
479{
480 struct extent_io_tree *tree;
481 tree = &BTRFS_I(page->mapping->host)->io_tree;
482 return extent_read_full_page(tree, page, btree_get_extent);
483}
484
485static int btree_releasepage(struct page *page, gfp_t gfp_flags)
486{
487 struct extent_io_tree *tree;
488 struct extent_map_tree *map;
489 int ret;
490
491 if (page_count(page) > 3) {
492 /* once for page->private, once for the caller, once
493 * once for the page cache
494 */
495 return 0;
496 }
497 tree = &BTRFS_I(page->mapping->host)->io_tree;
498 map = &BTRFS_I(page->mapping->host)->extent_tree;
499 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
500 if (ret == 1) {
501 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
502 ClearPagePrivate(page);
503 set_page_private(page, 0);
504 page_cache_release(page);
505 }
506 return ret;
507}
508
509static void btree_invalidatepage(struct page *page, unsigned long offset)
510{
511 struct extent_io_tree *tree;
512 tree = &BTRFS_I(page->mapping->host)->io_tree;
513 extent_invalidatepage(tree, page, offset);
514 btree_releasepage(page, GFP_NOFS);
515}
516
517#if 0
518static int btree_writepage(struct page *page, struct writeback_control *wbc)
519{
520 struct buffer_head *bh;
521 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
522 struct buffer_head *head;
523 if (!page_has_buffers(page)) {
524 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
525 (1 << BH_Dirty)|(1 << BH_Uptodate));
526 }
527 head = page_buffers(page);
528 bh = head;
529 do {
530 if (buffer_dirty(bh))
531 csum_tree_block(root, bh, 0);
532 bh = bh->b_this_page;
533 } while (bh != head);
534 return block_write_full_page(page, btree_get_block, wbc);
535}
536#endif
537
538static struct address_space_operations btree_aops = {
539 .readpage = btree_readpage,
540 .writepage = btree_writepage,
541 .writepages = btree_writepages,
542 .releasepage = btree_releasepage,
543 .invalidatepage = btree_invalidatepage,
544 .sync_page = block_sync_page,
545};
546
547int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
548{
549 struct extent_buffer *buf = NULL;
550 struct inode *btree_inode = root->fs_info->btree_inode;
551 int ret = 0;
552
553 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
554 if (!buf)
555 return 0;
556 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
557 buf, 0, 0, btree_get_extent, 0);
558 free_extent_buffer(buf);
559 return ret;
560}
561
562static int close_all_devices(struct btrfs_fs_info *fs_info)
563{
564 struct list_head *list;
565 struct list_head *next;
566 struct btrfs_device *device;
567
568 list = &fs_info->fs_devices->devices;
569 list_for_each(next, list) {
570 device = list_entry(next, struct btrfs_device, dev_list);
571 if (device->bdev && device->bdev != fs_info->sb->s_bdev)
572 close_bdev_excl(device->bdev);
573 device->bdev = NULL;
574 }
575 return 0;
576}
577
578int btrfs_verify_block_csum(struct btrfs_root *root,
579 struct extent_buffer *buf)
580{
581 return btrfs_buffer_uptodate(buf);
582}
583
584struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
585 u64 bytenr, u32 blocksize)
586{
587 struct inode *btree_inode = root->fs_info->btree_inode;
588 struct extent_buffer *eb;
589 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
590 bytenr, blocksize, GFP_NOFS);
591 return eb;
592}
593
594struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
595 u64 bytenr, u32 blocksize)
596{
597 struct inode *btree_inode = root->fs_info->btree_inode;
598 struct extent_buffer *eb;
599
600 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
601 bytenr, blocksize, NULL, GFP_NOFS);
602 return eb;
603}
604
605
606struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
607 u32 blocksize)
608{
609 struct extent_buffer *buf = NULL;
610 struct inode *btree_inode = root->fs_info->btree_inode;
611 struct extent_io_tree *io_tree;
612 int ret;
613
614 io_tree = &BTRFS_I(btree_inode)->io_tree;
615
616 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
617 if (!buf)
618 return NULL;
619
620 ret = btree_read_extent_buffer_pages(root, buf, 0);
621
622 if (ret == 0) {
623 buf->flags |= EXTENT_UPTODATE;
624 }
625 return buf;
626
627}
628
629int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
630 struct extent_buffer *buf)
631{
632 struct inode *btree_inode = root->fs_info->btree_inode;
633 if (btrfs_header_generation(buf) ==
634 root->fs_info->running_transaction->transid)
635 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
636 buf);
637 return 0;
638}
639
640int wait_on_tree_block_writeback(struct btrfs_root *root,
641 struct extent_buffer *buf)
642{
643 struct inode *btree_inode = root->fs_info->btree_inode;
644 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
645 buf);
646 return 0;
647}
648
649static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
650 u32 stripesize, struct btrfs_root *root,
651 struct btrfs_fs_info *fs_info,
652 u64 objectid)
653{
654 root->node = NULL;
655 root->inode = NULL;
656 root->commit_root = NULL;
657 root->sectorsize = sectorsize;
658 root->nodesize = nodesize;
659 root->leafsize = leafsize;
660 root->stripesize = stripesize;
661 root->ref_cows = 0;
662 root->track_dirty = 0;
663
664 root->fs_info = fs_info;
665 root->objectid = objectid;
666 root->last_trans = 0;
667 root->highest_inode = 0;
668 root->last_inode_alloc = 0;
669 root->name = NULL;
670 root->in_sysfs = 0;
671
672 INIT_LIST_HEAD(&root->dirty_list);
673 memset(&root->root_key, 0, sizeof(root->root_key));
674 memset(&root->root_item, 0, sizeof(root->root_item));
675 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
676 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
677 init_completion(&root->kobj_unregister);
678 root->defrag_running = 0;
679 root->defrag_level = 0;
680 root->root_key.objectid = objectid;
681 return 0;
682}
683
684static int find_and_setup_root(struct btrfs_root *tree_root,
685 struct btrfs_fs_info *fs_info,
686 u64 objectid,
687 struct btrfs_root *root)
688{
689 int ret;
690 u32 blocksize;
691
692 __setup_root(tree_root->nodesize, tree_root->leafsize,
693 tree_root->sectorsize, tree_root->stripesize,
694 root, fs_info, objectid);
695 ret = btrfs_find_last_root(tree_root, objectid,
696 &root->root_item, &root->root_key);
697 BUG_ON(ret);
698
699 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
700 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
701 blocksize);
702 BUG_ON(!root->node);
703 return 0;
704}
705
706struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
707 struct btrfs_key *location)
708{
709 struct btrfs_root *root;
710 struct btrfs_root *tree_root = fs_info->tree_root;
711 struct btrfs_path *path;
712 struct extent_buffer *l;
713 u64 highest_inode;
714 u32 blocksize;
715 int ret = 0;
716
717 root = kzalloc(sizeof(*root), GFP_NOFS);
718 if (!root)
719 return ERR_PTR(-ENOMEM);
720 if (location->offset == (u64)-1) {
721 ret = find_and_setup_root(tree_root, fs_info,
722 location->objectid, root);
723 if (ret) {
724 kfree(root);
725 return ERR_PTR(ret);
726 }
727 goto insert;
728 }
729
730 __setup_root(tree_root->nodesize, tree_root->leafsize,
731 tree_root->sectorsize, tree_root->stripesize,
732 root, fs_info, location->objectid);
733
734 path = btrfs_alloc_path();
735 BUG_ON(!path);
736 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
737 if (ret != 0) {
738 if (ret > 0)
739 ret = -ENOENT;
740 goto out;
741 }
742 l = path->nodes[0];
743 read_extent_buffer(l, &root->root_item,
744 btrfs_item_ptr_offset(l, path->slots[0]),
745 sizeof(root->root_item));
746 memcpy(&root->root_key, location, sizeof(*location));
747 ret = 0;
748out:
749 btrfs_release_path(root, path);
750 btrfs_free_path(path);
751 if (ret) {
752 kfree(root);
753 return ERR_PTR(ret);
754 }
755 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
756 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
757 blocksize);
758 BUG_ON(!root->node);
759insert:
760 root->ref_cows = 1;
761 ret = btrfs_find_highest_inode(root, &highest_inode);
762 if (ret == 0) {
763 root->highest_inode = highest_inode;
764 root->last_inode_alloc = highest_inode;
765 }
766 return root;
767}
768
769struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
770 u64 root_objectid)
771{
772 struct btrfs_root *root;
773
774 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
775 return fs_info->tree_root;
776 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
777 return fs_info->extent_root;
778
779 root = radix_tree_lookup(&fs_info->fs_roots_radix,
780 (unsigned long)root_objectid);
781 return root;
782}
783
784struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
785 struct btrfs_key *location)
786{
787 struct btrfs_root *root;
788 int ret;
789
790 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
791 return fs_info->tree_root;
792 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
793 return fs_info->extent_root;
794
795 root = radix_tree_lookup(&fs_info->fs_roots_radix,
796 (unsigned long)location->objectid);
797 if (root)
798 return root;
799
800 root = btrfs_read_fs_root_no_radix(fs_info, location);
801 if (IS_ERR(root))
802 return root;
803 ret = radix_tree_insert(&fs_info->fs_roots_radix,
804 (unsigned long)root->root_key.objectid,
805 root);
806 if (ret) {
807 free_extent_buffer(root->node);
808 kfree(root);
809 return ERR_PTR(ret);
810 }
811 ret = btrfs_find_dead_roots(fs_info->tree_root,
812 root->root_key.objectid, root);
813 BUG_ON(ret);
814
815 return root;
816}
817
818struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
819 struct btrfs_key *location,
820 const char *name, int namelen)
821{
822 struct btrfs_root *root;
823 int ret;
824
825 root = btrfs_read_fs_root_no_name(fs_info, location);
826 if (!root)
827 return NULL;
828
829 if (root->in_sysfs)
830 return root;
831
832 ret = btrfs_set_root_name(root, name, namelen);
833 if (ret) {
834 free_extent_buffer(root->node);
835 kfree(root);
836 return ERR_PTR(ret);
837 }
838
839 ret = btrfs_sysfs_add_root(root);
840 if (ret) {
841 free_extent_buffer(root->node);
842 kfree(root->name);
843 kfree(root);
844 return ERR_PTR(ret);
845 }
846 root->in_sysfs = 1;
847 return root;
848}
849#if 0
850static int add_hasher(struct btrfs_fs_info *info, char *type) {
851 struct btrfs_hasher *hasher;
852
853 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
854 if (!hasher)
855 return -ENOMEM;
856 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
857 if (!hasher->hash_tfm) {
858 kfree(hasher);
859 return -EINVAL;
860 }
861 spin_lock(&info->hash_lock);
862 list_add(&hasher->list, &info->hashers);
863 spin_unlock(&info->hash_lock);
864 return 0;
865}
866#endif
867
868static int btrfs_congested_fn(void *congested_data, int bdi_bits)
869{
870 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
871 int ret = 0;
872 struct list_head *cur;
873 struct btrfs_device *device;
874 struct backing_dev_info *bdi;
875
876 list_for_each(cur, &info->fs_devices->devices) {
877 device = list_entry(cur, struct btrfs_device, dev_list);
878 bdi = blk_get_backing_dev_info(device->bdev);
879 if (bdi && bdi_congested(bdi, bdi_bits)) {
880 ret = 1;
881 break;
882 }
883 }
884 return ret;
885}
886
887void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
888{
889 struct list_head *cur;
890 struct btrfs_device *device;
891 struct btrfs_fs_info *info;
892
893 info = (struct btrfs_fs_info *)bdi->unplug_io_data;
894 list_for_each(cur, &info->fs_devices->devices) {
895 device = list_entry(cur, struct btrfs_device, dev_list);
896 bdi = blk_get_backing_dev_info(device->bdev);
897 if (bdi->unplug_io_fn) {
898 bdi->unplug_io_fn(bdi, page);
899 }
900 }
901}
902
903static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
904{
905#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
906 bdi_init(bdi);
907#endif
908 bdi->ra_pages = default_backing_dev_info.ra_pages * 4;
909 bdi->state = 0;
910 bdi->capabilities = default_backing_dev_info.capabilities;
911 bdi->unplug_io_fn = btrfs_unplug_io_fn;
912 bdi->unplug_io_data = info;
913 bdi->congested_fn = btrfs_congested_fn;
914 bdi->congested_data = info;
915 return 0;
916}
917
918static int bio_ready_for_csum(struct bio *bio)
919{
920 u64 length = 0;
921 u64 buf_len = 0;
922 u64 start = 0;
923 struct page *page;
924 struct extent_io_tree *io_tree = NULL;
925 struct btrfs_fs_info *info = NULL;
926 struct bio_vec *bvec;
927 int i;
928 int ret;
929
930 bio_for_each_segment(bvec, bio, i) {
931 page = bvec->bv_page;
932 if (page->private == EXTENT_PAGE_PRIVATE) {
933 length += bvec->bv_len;
934 continue;
935 }
936 if (!page->private) {
937 length += bvec->bv_len;
938 continue;
939 }
940 length = bvec->bv_len;
941 buf_len = page->private >> 2;
942 start = page_offset(page) + bvec->bv_offset;
943 io_tree = &BTRFS_I(page->mapping->host)->io_tree;
944 info = BTRFS_I(page->mapping->host)->root->fs_info;
945 }
946 /* are we fully contained in this bio? */
947 if (buf_len <= length)
948 return 1;
949
950 ret = extent_range_uptodate(io_tree, start + length,
951 start + buf_len - 1);
952 if (ret == 1)
953 return ret;
954 return ret;
955}
956
957#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
958static void btrfs_end_io_csum(void *p)
959#else
960static void btrfs_end_io_csum(struct work_struct *work)
961#endif
962{
963#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
964 struct btrfs_fs_info *fs_info = p;
965#else
966 struct btrfs_fs_info *fs_info = container_of(work,
967 struct btrfs_fs_info,
968 end_io_work);
969#endif
970 unsigned long flags;
971 struct end_io_wq *end_io_wq;
972 struct bio *bio;
973 struct list_head *next;
974 int error;
975 int was_empty;
976
977 while(1) {
978 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
979 if (list_empty(&fs_info->end_io_work_list)) {
980 spin_unlock_irqrestore(&fs_info->end_io_work_lock,
981 flags);
982 return;
983 }
984 next = fs_info->end_io_work_list.next;
985 list_del(next);
986 spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
987
988 end_io_wq = list_entry(next, struct end_io_wq, list);
989
990 bio = end_io_wq->bio;
991 if (end_io_wq->metadata && !bio_ready_for_csum(bio)) {
992 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
993 was_empty = list_empty(&fs_info->end_io_work_list);
994 list_add_tail(&end_io_wq->list,
995 &fs_info->end_io_work_list);
996 spin_unlock_irqrestore(&fs_info->end_io_work_lock,
997 flags);
998 if (was_empty)
999 return;
1000 continue;
1001 }
1002 error = end_io_wq->error;
1003 bio->bi_private = end_io_wq->private;
1004 bio->bi_end_io = end_io_wq->end_io;
1005 kfree(end_io_wq);
1006#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1007 bio_endio(bio, bio->bi_size, error);
1008#else
1009 bio_endio(bio, error);
1010#endif
1011 }
1012}
1013
1014#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1015static void btrfs_async_submit_work(void *p)
1016#else
1017static void btrfs_async_submit_work(struct work_struct *work)
1018#endif
1019{
1020#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1021 struct btrfs_fs_info *fs_info = p;
1022#else
1023 struct btrfs_fs_info *fs_info = container_of(work,
1024 struct btrfs_fs_info,
1025 async_submit_work);
1026#endif
1027 struct async_submit_bio *async;
1028 struct list_head *next;
1029
1030 while(1) {
1031 spin_lock(&fs_info->async_submit_work_lock);
1032 if (list_empty(&fs_info->async_submit_work_list)) {
1033 spin_unlock(&fs_info->async_submit_work_lock);
1034 return;
1035 }
1036 next = fs_info->async_submit_work_list.next;
1037 list_del(next);
1038 spin_unlock(&fs_info->async_submit_work_lock);
1039
1040 async = list_entry(next, struct async_submit_bio, list);
1041 async->submit_bio_hook(async->inode, async->rw, async->bio,
1042 async->mirror_num);
1043 kfree(async);
1044 }
1045}
1046
1047struct btrfs_root *open_ctree(struct super_block *sb,
1048 struct btrfs_fs_devices *fs_devices)
1049{
1050 u32 sectorsize;
1051 u32 nodesize;
1052 u32 leafsize;
1053 u32 blocksize;
1054 u32 stripesize;
1055 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
1056 GFP_NOFS);
1057 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
1058 GFP_NOFS);
1059 struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
1060 GFP_NOFS);
1061 struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
1062 GFP_NOFS);
1063 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
1064 GFP_NOFS);
1065 int ret;
1066 int err = -EINVAL;
1067 struct btrfs_super_block *disk_super;
1068
1069 if (!extent_root || !tree_root || !fs_info) {
1070 err = -ENOMEM;
1071 goto fail;
1072 }
1073 end_io_workqueue = create_workqueue("btrfs-end-io");
1074 BUG_ON(!end_io_workqueue);
1075 async_submit_workqueue = create_workqueue("btrfs-async-submit");
1076
1077 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
1078 INIT_LIST_HEAD(&fs_info->trans_list);
1079 INIT_LIST_HEAD(&fs_info->dead_roots);
1080 INIT_LIST_HEAD(&fs_info->hashers);
1081 INIT_LIST_HEAD(&fs_info->end_io_work_list);
1082 INIT_LIST_HEAD(&fs_info->async_submit_work_list);
1083 spin_lock_init(&fs_info->hash_lock);
1084 spin_lock_init(&fs_info->end_io_work_lock);
1085 spin_lock_init(&fs_info->async_submit_work_lock);
1086 spin_lock_init(&fs_info->delalloc_lock);
1087 spin_lock_init(&fs_info->new_trans_lock);
1088
1089 init_completion(&fs_info->kobj_unregister);
1090 sb_set_blocksize(sb, BTRFS_SUPER_INFO_SIZE);
1091 fs_info->tree_root = tree_root;
1092 fs_info->extent_root = extent_root;
1093 fs_info->chunk_root = chunk_root;
1094 fs_info->dev_root = dev_root;
1095 fs_info->fs_devices = fs_devices;
1096 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
1097 INIT_LIST_HEAD(&fs_info->space_info);
1098 btrfs_mapping_init(&fs_info->mapping_tree);
1099 fs_info->sb = sb;
1100 fs_info->max_extent = (u64)-1;
1101 fs_info->max_inline = 8192 * 1024;
1102 setup_bdi(fs_info, &fs_info->bdi);
1103 fs_info->btree_inode = new_inode(sb);
1104 fs_info->btree_inode->i_ino = 1;
1105 fs_info->btree_inode->i_nlink = 1;
1106 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
1107 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
1108 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
1109
1110 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
1111 fs_info->btree_inode->i_mapping,
1112 GFP_NOFS);
1113 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
1114 GFP_NOFS);
1115
1116 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
1117
1118 extent_io_tree_init(&fs_info->free_space_cache,
1119 fs_info->btree_inode->i_mapping, GFP_NOFS);
1120 extent_io_tree_init(&fs_info->block_group_cache,
1121 fs_info->btree_inode->i_mapping, GFP_NOFS);
1122 extent_io_tree_init(&fs_info->pinned_extents,
1123 fs_info->btree_inode->i_mapping, GFP_NOFS);
1124 extent_io_tree_init(&fs_info->pending_del,
1125 fs_info->btree_inode->i_mapping, GFP_NOFS);
1126 extent_io_tree_init(&fs_info->extent_ins,
1127 fs_info->btree_inode->i_mapping, GFP_NOFS);
1128 fs_info->do_barriers = 1;
1129
1130#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1131 INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum, fs_info);
1132 INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work,
1133 fs_info);
1134 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
1135#else
1136 INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum);
1137 INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work);
1138 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
1139#endif
1140 BTRFS_I(fs_info->btree_inode)->root = tree_root;
1141 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
1142 sizeof(struct btrfs_key));
1143 insert_inode_hash(fs_info->btree_inode);
1144 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
1145
1146 mutex_init(&fs_info->trans_mutex);
1147 mutex_init(&fs_info->fs_mutex);
1148
1149#if 0
1150 ret = add_hasher(fs_info, "crc32c");
1151 if (ret) {
1152 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
1153 err = -ENOMEM;
1154 goto fail_iput;
1155 }
1156#endif
1157 __setup_root(4096, 4096, 4096, 4096, tree_root,
1158 fs_info, BTRFS_ROOT_TREE_OBJECTID);
1159
1160 fs_info->sb_buffer = read_tree_block(tree_root,
1161 BTRFS_SUPER_INFO_OFFSET,
1162 4096);
1163
1164 if (!fs_info->sb_buffer)
1165 goto fail_iput;
1166
1167 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
1168 sizeof(fs_info->super_copy));
1169
1170 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
1171 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
1172 BTRFS_FSID_SIZE);
1173
1174 disk_super = &fs_info->super_copy;
1175 if (!btrfs_super_root(disk_super))
1176 goto fail_sb_buffer;
1177
1178 if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
1179 printk("Btrfs: wanted %llu devices, but found %llu\n",
1180 (unsigned long long)btrfs_super_num_devices(disk_super),
1181 (unsigned long long)fs_devices->num_devices);
1182 goto fail_sb_buffer;
1183 }
1184 nodesize = btrfs_super_nodesize(disk_super);
1185 leafsize = btrfs_super_leafsize(disk_super);
1186 sectorsize = btrfs_super_sectorsize(disk_super);
1187 stripesize = btrfs_super_stripesize(disk_super);
1188 tree_root->nodesize = nodesize;
1189 tree_root->leafsize = leafsize;
1190 tree_root->sectorsize = sectorsize;
1191 tree_root->stripesize = stripesize;
1192 sb_set_blocksize(sb, sectorsize);
1193
1194 i_size_write(fs_info->btree_inode,
1195 btrfs_super_total_bytes(disk_super));
1196
1197 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
1198 sizeof(disk_super->magic))) {
1199 printk("btrfs: valid FS not found on %s\n", sb->s_id);
1200 goto fail_sb_buffer;
1201 }
1202
1203 mutex_lock(&fs_info->fs_mutex);
1204
1205 ret = btrfs_read_sys_array(tree_root);
1206 BUG_ON(ret);
1207
1208 blocksize = btrfs_level_size(tree_root,
1209 btrfs_super_chunk_root_level(disk_super));
1210
1211 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1212 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1213
1214 chunk_root->node = read_tree_block(chunk_root,
1215 btrfs_super_chunk_root(disk_super),
1216 blocksize);
1217 BUG_ON(!chunk_root->node);
1218
1219 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
1220 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
1221 BTRFS_UUID_SIZE);
1222
1223 ret = btrfs_read_chunk_tree(chunk_root);
1224 BUG_ON(ret);
1225
1226 blocksize = btrfs_level_size(tree_root,
1227 btrfs_super_root_level(disk_super));
1228
1229
1230 tree_root->node = read_tree_block(tree_root,
1231 btrfs_super_root(disk_super),
1232 blocksize);
1233 if (!tree_root->node)
1234 goto fail_sb_buffer;
1235
1236
1237 ret = find_and_setup_root(tree_root, fs_info,
1238 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
1239 if (ret)
1240 goto fail_tree_root;
1241 extent_root->track_dirty = 1;
1242
1243 ret = find_and_setup_root(tree_root, fs_info,
1244 BTRFS_DEV_TREE_OBJECTID, dev_root);
1245 dev_root->track_dirty = 1;
1246
1247 if (ret)
1248 goto fail_extent_root;
1249
1250 btrfs_read_block_groups(extent_root);
1251
1252 fs_info->generation = btrfs_super_generation(disk_super) + 1;
1253 fs_info->data_alloc_profile = (u64)-1;
1254 fs_info->metadata_alloc_profile = (u64)-1;
1255 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1256
1257 mutex_unlock(&fs_info->fs_mutex);
1258 return tree_root;
1259
1260fail_extent_root:
1261 free_extent_buffer(extent_root->node);
1262fail_tree_root:
1263 mutex_unlock(&fs_info->fs_mutex);
1264 free_extent_buffer(tree_root->node);
1265fail_sb_buffer:
1266 free_extent_buffer(fs_info->sb_buffer);
1267 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
1268fail_iput:
1269 iput(fs_info->btree_inode);
1270fail:
1271 close_all_devices(fs_info);
1272 kfree(extent_root);
1273 kfree(tree_root);
1274#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
1275 bdi_destroy(&fs_info->bdi);
1276#endif
1277 kfree(fs_info);
1278 return ERR_PTR(err);
1279}
1280
1281static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
1282{
1283 char b[BDEVNAME_SIZE];
1284
1285 if (uptodate) {
1286 set_buffer_uptodate(bh);
1287 } else {
1288 if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
1289 printk(KERN_WARNING "lost page write due to "
1290 "I/O error on %s\n",
1291 bdevname(bh->b_bdev, b));
1292 }
1293 set_buffer_write_io_error(bh);
1294 clear_buffer_uptodate(bh);
1295 }
1296 unlock_buffer(bh);
1297 put_bh(bh);
1298}
1299
1300int write_all_supers(struct btrfs_root *root)
1301{
1302 struct list_head *cur;
1303 struct list_head *head = &root->fs_info->fs_devices->devices;
1304 struct btrfs_device *dev;
1305 struct extent_buffer *sb;
1306 struct btrfs_dev_item *dev_item;
1307 struct buffer_head *bh;
1308 int ret;
1309 int do_barriers;
1310
1311 do_barriers = !btrfs_test_opt(root, NOBARRIER);
1312
1313 sb = root->fs_info->sb_buffer;
1314 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1315 dev_item);
1316 list_for_each(cur, head) {
1317 dev = list_entry(cur, struct btrfs_device, dev_list);
1318 btrfs_set_device_type(sb, dev_item, dev->type);
1319 btrfs_set_device_id(sb, dev_item, dev->devid);
1320 btrfs_set_device_total_bytes(sb, dev_item, dev->total_bytes);
1321 btrfs_set_device_bytes_used(sb, dev_item, dev->bytes_used);
1322 btrfs_set_device_io_align(sb, dev_item, dev->io_align);
1323 btrfs_set_device_io_width(sb, dev_item, dev->io_width);
1324 btrfs_set_device_sector_size(sb, dev_item, dev->sector_size);
1325 write_extent_buffer(sb, dev->uuid,
1326 (unsigned long)btrfs_device_uuid(dev_item),
1327 BTRFS_UUID_SIZE);
1328
1329 btrfs_set_header_flag(sb, BTRFS_HEADER_FLAG_WRITTEN);
1330 csum_tree_block(root, sb, 0);
1331
1332 bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET /
1333 root->fs_info->sb->s_blocksize,
1334 BTRFS_SUPER_INFO_SIZE);
1335
1336 read_extent_buffer(sb, bh->b_data, 0, BTRFS_SUPER_INFO_SIZE);
1337 dev->pending_io = bh;
1338
1339 get_bh(bh);
1340 set_buffer_uptodate(bh);
1341 lock_buffer(bh);
1342 bh->b_end_io = btrfs_end_buffer_write_sync;
1343
1344 if (do_barriers && dev->barriers) {
1345 ret = submit_bh(WRITE_BARRIER, bh);
1346 if (ret == -EOPNOTSUPP) {
1347 printk("btrfs: disabling barriers on dev %s\n",
1348 dev->name);
1349 set_buffer_uptodate(bh);
1350 dev->barriers = 0;
1351 get_bh(bh);
1352 lock_buffer(bh);
1353 ret = submit_bh(WRITE, bh);
1354 }
1355 } else {
1356 ret = submit_bh(WRITE, bh);
1357 }
1358 BUG_ON(ret);
1359 }
1360
1361 list_for_each(cur, head) {
1362 dev = list_entry(cur, struct btrfs_device, dev_list);
1363 BUG_ON(!dev->pending_io);
1364 bh = dev->pending_io;
1365 wait_on_buffer(bh);
1366 if (!buffer_uptodate(dev->pending_io)) {
1367 if (do_barriers && dev->barriers) {
1368 printk("btrfs: disabling barriers on dev %s\n",
1369 dev->name);
1370 set_buffer_uptodate(bh);
1371 get_bh(bh);
1372 lock_buffer(bh);
1373 dev->barriers = 0;
1374 ret = submit_bh(WRITE, bh);
1375 BUG_ON(ret);
1376 wait_on_buffer(bh);
1377 BUG_ON(!buffer_uptodate(bh));
1378 } else {
1379 BUG();
1380 }
1381
1382 }
1383 dev->pending_io = NULL;
1384 brelse(bh);
1385 }
1386 return 0;
1387}
1388
1389int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
1390 *root)
1391{
1392 int ret;
1393
1394 ret = write_all_supers(root);
1395#if 0
1396 if (!btrfs_test_opt(root, NOBARRIER))
1397 blkdev_issue_flush(sb->s_bdev, NULL);
1398 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
1399 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
1400 super->start, super->len);
1401 if (!btrfs_test_opt(root, NOBARRIER))
1402 blkdev_issue_flush(sb->s_bdev, NULL);
1403#endif
1404 return ret;
1405}
1406
1407int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
1408{
1409 radix_tree_delete(&fs_info->fs_roots_radix,
1410 (unsigned long)root->root_key.objectid);
1411 if (root->in_sysfs)
1412 btrfs_sysfs_del_root(root);
1413 if (root->inode)
1414 iput(root->inode);
1415 if (root->node)
1416 free_extent_buffer(root->node);
1417 if (root->commit_root)
1418 free_extent_buffer(root->commit_root);
1419 if (root->name)
1420 kfree(root->name);
1421 kfree(root);
1422 return 0;
1423}
1424
1425static int del_fs_roots(struct btrfs_fs_info *fs_info)
1426{
1427 int ret;
1428 struct btrfs_root *gang[8];
1429 int i;
1430
1431 while(1) {
1432 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
1433 (void **)gang, 0,
1434 ARRAY_SIZE(gang));
1435 if (!ret)
1436 break;
1437 for (i = 0; i < ret; i++)
1438 btrfs_free_fs_root(fs_info, gang[i]);
1439 }
1440 return 0;
1441}
1442
1443int close_ctree(struct btrfs_root *root)
1444{
1445 int ret;
1446 struct btrfs_trans_handle *trans;
1447 struct btrfs_fs_info *fs_info = root->fs_info;
1448
1449 fs_info->closing = 1;
1450 btrfs_transaction_flush_work(root);
1451 mutex_lock(&fs_info->fs_mutex);
1452 btrfs_defrag_dirty_roots(root->fs_info);
1453 trans = btrfs_start_transaction(root, 1);
1454 ret = btrfs_commit_transaction(trans, root);
1455 /* run commit again to drop the original snapshot */
1456 trans = btrfs_start_transaction(root, 1);
1457 btrfs_commit_transaction(trans, root);
1458 ret = btrfs_write_and_wait_transaction(NULL, root);
1459 BUG_ON(ret);
1460 write_ctree_super(NULL, root);
1461 mutex_unlock(&fs_info->fs_mutex);
1462
1463 if (fs_info->delalloc_bytes) {
1464 printk("btrfs: at unmount delalloc count %Lu\n",
1465 fs_info->delalloc_bytes);
1466 }
1467 if (fs_info->extent_root->node)
1468 free_extent_buffer(fs_info->extent_root->node);
1469
1470 if (fs_info->tree_root->node)
1471 free_extent_buffer(fs_info->tree_root->node);
1472
1473 if (root->fs_info->chunk_root->node);
1474 free_extent_buffer(root->fs_info->chunk_root->node);
1475
1476 if (root->fs_info->dev_root->node);
1477 free_extent_buffer(root->fs_info->dev_root->node);
1478
1479 free_extent_buffer(fs_info->sb_buffer);
1480
1481 btrfs_free_block_groups(root->fs_info);
1482 del_fs_roots(fs_info);
1483
1484 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
1485
1486 extent_io_tree_empty_lru(&fs_info->free_space_cache);
1487 extent_io_tree_empty_lru(&fs_info->block_group_cache);
1488 extent_io_tree_empty_lru(&fs_info->pinned_extents);
1489 extent_io_tree_empty_lru(&fs_info->pending_del);
1490 extent_io_tree_empty_lru(&fs_info->extent_ins);
1491 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
1492
1493 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
1494 flush_workqueue(end_io_workqueue);
1495 destroy_workqueue(end_io_workqueue);
1496
1497 flush_workqueue(async_submit_workqueue);
1498 destroy_workqueue(async_submit_workqueue);
1499
1500 iput(fs_info->btree_inode);
1501#if 0
1502 while(!list_empty(&fs_info->hashers)) {
1503 struct btrfs_hasher *hasher;
1504 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
1505 hashers);
1506 list_del(&hasher->hashers);
1507 crypto_free_hash(&fs_info->hash_tfm);
1508 kfree(hasher);
1509 }
1510#endif
1511 close_all_devices(fs_info);
1512 btrfs_mapping_tree_free(&fs_info->mapping_tree);
1513
1514#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
1515 bdi_destroy(&fs_info->bdi);
1516#endif
1517
1518 kfree(fs_info->extent_root);
1519 kfree(fs_info->tree_root);
1520 kfree(fs_info->chunk_root);
1521 kfree(fs_info->dev_root);
1522 return 0;
1523}
1524
1525int btrfs_buffer_uptodate(struct extent_buffer *buf)
1526{
1527 struct inode *btree_inode = buf->first_page->mapping->host;
1528 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
1529}
1530
1531int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
1532{
1533 struct inode *btree_inode = buf->first_page->mapping->host;
1534 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
1535 buf);
1536}
1537
1538void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
1539{
1540 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1541 u64 transid = btrfs_header_generation(buf);
1542 struct inode *btree_inode = root->fs_info->btree_inode;
1543
1544 if (transid != root->fs_info->generation) {
1545 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
1546 (unsigned long long)buf->start,
1547 transid, root->fs_info->generation);
1548 WARN_ON(1);
1549 }
1550 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
1551}
1552
1553void btrfs_throttle(struct btrfs_root *root)
1554{
1555 struct backing_dev_info *bdi;
1556
1557 bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
1558 if (root->fs_info->throttles && bdi_write_congested(bdi)) {
1559#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1560 congestion_wait(WRITE, HZ/20);
1561#else
1562 blk_congestion_wait(WRITE, HZ/20);
1563#endif
1564 }
1565}
1566
1567void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
1568{
1569 balance_dirty_pages_ratelimited_nr(
1570 root->fs_info->btree_inode->i_mapping, 1);
1571}
1572
1573void btrfs_set_buffer_defrag(struct extent_buffer *buf)
1574{
1575 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1576 struct inode *btree_inode = root->fs_info->btree_inode;
1577 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
1578 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
1579}
1580
1581void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
1582{
1583 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1584 struct inode *btree_inode = root->fs_info->btree_inode;
1585 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
1586 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
1587 GFP_NOFS);
1588}
1589
1590int btrfs_buffer_defrag(struct extent_buffer *buf)
1591{
1592 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1593 struct inode *btree_inode = root->fs_info->btree_inode;
1594 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
1595 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
1596}
1597
1598int btrfs_buffer_defrag_done(struct extent_buffer *buf)
1599{
1600 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1601 struct inode *btree_inode = root->fs_info->btree_inode;
1602 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
1603 buf->start, buf->start + buf->len - 1,
1604 EXTENT_DEFRAG_DONE, 0);
1605}
1606
1607int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
1608{
1609 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1610 struct inode *btree_inode = root->fs_info->btree_inode;
1611 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
1612 buf->start, buf->start + buf->len - 1,
1613 EXTENT_DEFRAG_DONE, GFP_NOFS);
1614}
1615
1616int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
1617{
1618 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1619 struct inode *btree_inode = root->fs_info->btree_inode;
1620 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
1621 buf->start, buf->start + buf->len - 1,
1622 EXTENT_DEFRAG, GFP_NOFS);
1623}
1624
1625int btrfs_read_buffer(struct extent_buffer *buf)
1626{
1627 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1628 int ret;
1629 ret = btree_read_extent_buffer_pages(root, buf, 0);
1630 if (ret == 0) {
1631 buf->flags |= EXTENT_UPTODATE;
1632 }
1633 return ret;
1634}
1635
1636static struct extent_io_ops btree_extent_io_ops = {
1637 .writepage_io_hook = btree_writepage_io_hook,
1638 .readpage_end_io_hook = btree_readpage_end_io_hook,
1639 .submit_bio_hook = btree_submit_bio_hook,
1640 /* note we're sharing with inode.c for the merge bio hook */
1641 .merge_bio_hook = btrfs_merge_bio_hook,
1642};