Btrfs: Implement raid0 when multiple devices are present
[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/crc32c.h>
22 #include <linux/scatterlist.h>
23 #include <linux/swap.h>
24 #include <linux/radix-tree.h>
25 #include <linux/writeback.h>
26 #include <linux/buffer_head.h> // for block_sync_page
27 #include "ctree.h"
28 #include "disk-io.h"
29 #include "transaction.h"
30 #include "btrfs_inode.h"
31 #include "volumes.h"
32 #include "print-tree.h"
33
34 #if 0
35 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
36 {
37 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
38 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
39 (unsigned long long)extent_buffer_blocknr(buf),
40 (unsigned long long)btrfs_header_blocknr(buf));
41 return 1;
42 }
43 return 0;
44 }
45 #endif
46
47 static struct extent_io_ops btree_extent_io_ops;
48
49 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
50 u64 bytenr, u32 blocksize)
51 {
52 struct inode *btree_inode = root->fs_info->btree_inode;
53 struct extent_buffer *eb;
54 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
55 bytenr, blocksize, GFP_NOFS);
56 return eb;
57 }
58
59 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
60 u64 bytenr, u32 blocksize)
61 {
62 struct inode *btree_inode = root->fs_info->btree_inode;
63 struct extent_buffer *eb;
64
65 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
66 bytenr, blocksize, NULL, GFP_NOFS);
67 return eb;
68 }
69
70 struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
71 size_t page_offset, u64 start, u64 len,
72 int create)
73 {
74 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
75 struct extent_map *em;
76 int ret;
77
78 again:
79 spin_lock(&em_tree->lock);
80 em = lookup_extent_mapping(em_tree, start, len);
81 spin_unlock(&em_tree->lock);
82 if (em) {
83 goto out;
84 }
85 em = alloc_extent_map(GFP_NOFS);
86 if (!em) {
87 em = ERR_PTR(-ENOMEM);
88 goto out;
89 }
90 em->start = 0;
91 em->len = i_size_read(inode);
92 em->block_start = 0;
93 em->bdev = inode->i_sb->s_bdev;
94
95 spin_lock(&em_tree->lock);
96 ret = add_extent_mapping(em_tree, em);
97 spin_unlock(&em_tree->lock);
98
99 if (ret == -EEXIST) {
100 free_extent_map(em);
101 em = NULL;
102 goto again;
103 } else if (ret) {
104 em = ERR_PTR(ret);
105 }
106 out:
107 return em;
108 }
109
110 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
111 {
112 return crc32c(seed, data, len);
113 }
114
115 void btrfs_csum_final(u32 crc, char *result)
116 {
117 *(__le32 *)result = ~cpu_to_le32(crc);
118 }
119
120 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
121 int verify)
122 {
123 char result[BTRFS_CRC32_SIZE];
124 unsigned long len;
125 unsigned long cur_len;
126 unsigned long offset = BTRFS_CSUM_SIZE;
127 char *map_token = NULL;
128 char *kaddr;
129 unsigned long map_start;
130 unsigned long map_len;
131 int err;
132 u32 crc = ~(u32)0;
133
134 len = buf->len - offset;
135 while(len > 0) {
136 err = map_private_extent_buffer(buf, offset, 32,
137 &map_token, &kaddr,
138 &map_start, &map_len, KM_USER0);
139 if (err) {
140 printk("failed to map extent buffer! %lu\n",
141 offset);
142 return 1;
143 }
144 cur_len = min(len, map_len - (offset - map_start));
145 crc = btrfs_csum_data(root, kaddr + offset - map_start,
146 crc, cur_len);
147 len -= cur_len;
148 offset += cur_len;
149 unmap_extent_buffer(buf, map_token, KM_USER0);
150 }
151 btrfs_csum_final(crc, result);
152
153 if (verify) {
154 int from_this_trans = 0;
155
156 if (root->fs_info->running_transaction &&
157 btrfs_header_generation(buf) ==
158 root->fs_info->running_transaction->transid)
159 from_this_trans = 1;
160
161 /* FIXME, this is not good */
162 if (from_this_trans == 0 &&
163 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\n",
171 root->fs_info->sb->s_id,
172 buf->start, val, found, from_this_trans);
173 return 1;
174 }
175 } else {
176 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
177 }
178 return 0;
179 }
180
181
182 int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
183 {
184 struct extent_io_tree *tree;
185 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
186 u64 found_start;
187 int found_level;
188 unsigned long len;
189 struct extent_buffer *eb;
190 tree = &BTRFS_I(page->mapping->host)->io_tree;
191
192 if (page->private == EXTENT_PAGE_PRIVATE)
193 goto out;
194 if (!page->private)
195 goto out;
196 len = page->private >> 2;
197 if (len == 0) {
198 WARN_ON(1);
199 }
200 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
201 read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1,
202 btree_get_extent);
203 btrfs_clear_buffer_defrag(eb);
204 found_start = btrfs_header_bytenr(eb);
205 if (found_start != start) {
206 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
207 start, found_start, len);
208 WARN_ON(1);
209 goto err;
210 }
211 if (eb->first_page != page) {
212 printk("bad first page %lu %lu\n", eb->first_page->index,
213 page->index);
214 WARN_ON(1);
215 goto err;
216 }
217 if (!PageUptodate(page)) {
218 printk("csum not up to date page %lu\n", page->index);
219 WARN_ON(1);
220 goto err;
221 }
222 found_level = btrfs_header_level(eb);
223 csum_tree_block(root, eb, 0);
224 err:
225 free_extent_buffer(eb);
226 out:
227 return 0;
228 }
229
230 static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
231 {
232 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
233
234 csum_dirty_buffer(root, page);
235 return 0;
236 }
237
238 static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio)
239 {
240 struct btrfs_root *root = BTRFS_I(inode)->root;
241 u64 offset;
242 offset = bio->bi_sector << 9;
243 if (offset == BTRFS_SUPER_INFO_OFFSET) {
244 bio->bi_bdev = root->fs_info->sb->s_bdev;
245 submit_bio(rw, bio);
246 return 0;
247 }
248 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
249 }
250
251 static int btree_writepage(struct page *page, struct writeback_control *wbc)
252 {
253 struct extent_io_tree *tree;
254 tree = &BTRFS_I(page->mapping->host)->io_tree;
255 return extent_write_full_page(tree, page, btree_get_extent, wbc);
256 }
257
258 static int btree_writepages(struct address_space *mapping,
259 struct writeback_control *wbc)
260 {
261 struct extent_io_tree *tree;
262 tree = &BTRFS_I(mapping->host)->io_tree;
263 if (wbc->sync_mode == WB_SYNC_NONE) {
264 u64 num_dirty;
265 u64 start = 0;
266 unsigned long thresh = 96 * 1024 * 1024;
267
268 if (wbc->for_kupdate)
269 return 0;
270
271 if (current_is_pdflush()) {
272 thresh = 96 * 1024 * 1024;
273 } else {
274 thresh = 8 * 1024 * 1024;
275 }
276 num_dirty = count_range_bits(tree, &start, (u64)-1,
277 thresh, EXTENT_DIRTY);
278 if (num_dirty < thresh) {
279 return 0;
280 }
281 }
282 return extent_writepages(tree, mapping, btree_get_extent, wbc);
283 }
284
285 int btree_readpage(struct file *file, struct page *page)
286 {
287 struct extent_io_tree *tree;
288 tree = &BTRFS_I(page->mapping->host)->io_tree;
289 return extent_read_full_page(tree, page, btree_get_extent);
290 }
291
292 static int btree_releasepage(struct page *page, gfp_t gfp_flags)
293 {
294 struct extent_io_tree *tree;
295 struct extent_map_tree *map;
296 int ret;
297
298 tree = &BTRFS_I(page->mapping->host)->io_tree;
299 map = &BTRFS_I(page->mapping->host)->extent_tree;
300 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
301 if (ret == 1) {
302 ClearPagePrivate(page);
303 set_page_private(page, 0);
304 page_cache_release(page);
305 }
306 return ret;
307 }
308
309 static void btree_invalidatepage(struct page *page, unsigned long offset)
310 {
311 struct extent_io_tree *tree;
312 tree = &BTRFS_I(page->mapping->host)->io_tree;
313 extent_invalidatepage(tree, page, offset);
314 btree_releasepage(page, GFP_NOFS);
315 }
316
317 #if 0
318 static int btree_writepage(struct page *page, struct writeback_control *wbc)
319 {
320 struct buffer_head *bh;
321 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
322 struct buffer_head *head;
323 if (!page_has_buffers(page)) {
324 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
325 (1 << BH_Dirty)|(1 << BH_Uptodate));
326 }
327 head = page_buffers(page);
328 bh = head;
329 do {
330 if (buffer_dirty(bh))
331 csum_tree_block(root, bh, 0);
332 bh = bh->b_this_page;
333 } while (bh != head);
334 return block_write_full_page(page, btree_get_block, wbc);
335 }
336 #endif
337
338 static struct address_space_operations btree_aops = {
339 .readpage = btree_readpage,
340 .writepage = btree_writepage,
341 .writepages = btree_writepages,
342 .releasepage = btree_releasepage,
343 .invalidatepage = btree_invalidatepage,
344 .sync_page = block_sync_page,
345 };
346
347 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
348 {
349 struct extent_buffer *buf = NULL;
350 struct inode *btree_inode = root->fs_info->btree_inode;
351 int ret = 0;
352
353 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
354 if (!buf)
355 return 0;
356 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
357 buf, 0, 0, btree_get_extent);
358 free_extent_buffer(buf);
359 return ret;
360 }
361
362 static int close_all_devices(struct btrfs_fs_info *fs_info)
363 {
364 struct list_head *list;
365 struct list_head *next;
366 struct btrfs_device *device;
367
368 list = &fs_info->fs_devices->devices;
369 list_for_each(next, list) {
370 device = list_entry(next, struct btrfs_device, dev_list);
371 if (device->bdev && device->bdev != fs_info->sb->s_bdev)
372 close_bdev_excl(device->bdev);
373 device->bdev = NULL;
374 }
375 return 0;
376 }
377
378 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
379 u32 blocksize)
380 {
381 struct extent_buffer *buf = NULL;
382 struct inode *btree_inode = root->fs_info->btree_inode;
383 struct extent_io_tree *io_tree;
384 u64 end;
385 int ret;
386
387 io_tree = &BTRFS_I(btree_inode)->io_tree;
388
389 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
390 if (!buf)
391 return NULL;
392 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 0, 1,
393 btree_get_extent);
394
395 if (buf->flags & EXTENT_CSUM)
396 return buf;
397
398 end = buf->start + PAGE_CACHE_SIZE - 1;
399 if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
400 buf->flags |= EXTENT_CSUM;
401 return buf;
402 }
403
404 lock_extent(io_tree, buf->start, end, GFP_NOFS);
405
406 if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
407 buf->flags |= EXTENT_CSUM;
408 goto out_unlock;
409 }
410
411 ret = csum_tree_block(root, buf, 1);
412 set_extent_bits(io_tree, buf->start, end, EXTENT_CSUM, GFP_NOFS);
413 buf->flags |= EXTENT_CSUM;
414
415 out_unlock:
416 unlock_extent(io_tree, buf->start, end, GFP_NOFS);
417 return buf;
418 }
419
420 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
421 struct extent_buffer *buf)
422 {
423 struct inode *btree_inode = root->fs_info->btree_inode;
424 if (btrfs_header_generation(buf) ==
425 root->fs_info->running_transaction->transid)
426 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
427 buf);
428 return 0;
429 }
430
431 int wait_on_tree_block_writeback(struct btrfs_root *root,
432 struct extent_buffer *buf)
433 {
434 struct inode *btree_inode = root->fs_info->btree_inode;
435 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
436 buf);
437 return 0;
438 }
439
440 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
441 u32 stripesize, struct btrfs_root *root,
442 struct btrfs_fs_info *fs_info,
443 u64 objectid)
444 {
445 root->node = NULL;
446 root->inode = NULL;
447 root->commit_root = NULL;
448 root->sectorsize = sectorsize;
449 root->nodesize = nodesize;
450 root->leafsize = leafsize;
451 root->stripesize = stripesize;
452 root->ref_cows = 0;
453 root->track_dirty = 0;
454
455 root->fs_info = fs_info;
456 root->objectid = objectid;
457 root->last_trans = 0;
458 root->highest_inode = 0;
459 root->last_inode_alloc = 0;
460 root->name = NULL;
461 root->in_sysfs = 0;
462
463 INIT_LIST_HEAD(&root->dirty_list);
464 memset(&root->root_key, 0, sizeof(root->root_key));
465 memset(&root->root_item, 0, sizeof(root->root_item));
466 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
467 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
468 init_completion(&root->kobj_unregister);
469 root->defrag_running = 0;
470 root->defrag_level = 0;
471 root->root_key.objectid = objectid;
472 return 0;
473 }
474
475 static int find_and_setup_root(struct btrfs_root *tree_root,
476 struct btrfs_fs_info *fs_info,
477 u64 objectid,
478 struct btrfs_root *root)
479 {
480 int ret;
481 u32 blocksize;
482
483 __setup_root(tree_root->nodesize, tree_root->leafsize,
484 tree_root->sectorsize, tree_root->stripesize,
485 root, fs_info, objectid);
486 ret = btrfs_find_last_root(tree_root, objectid,
487 &root->root_item, &root->root_key);
488 BUG_ON(ret);
489
490 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
491 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
492 blocksize);
493 BUG_ON(!root->node);
494 return 0;
495 }
496
497 struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
498 struct btrfs_key *location)
499 {
500 struct btrfs_root *root;
501 struct btrfs_root *tree_root = fs_info->tree_root;
502 struct btrfs_path *path;
503 struct extent_buffer *l;
504 u64 highest_inode;
505 u32 blocksize;
506 int ret = 0;
507
508 root = kzalloc(sizeof(*root), GFP_NOFS);
509 if (!root)
510 return ERR_PTR(-ENOMEM);
511 if (location->offset == (u64)-1) {
512 ret = find_and_setup_root(tree_root, fs_info,
513 location->objectid, root);
514 if (ret) {
515 kfree(root);
516 return ERR_PTR(ret);
517 }
518 goto insert;
519 }
520
521 __setup_root(tree_root->nodesize, tree_root->leafsize,
522 tree_root->sectorsize, tree_root->stripesize,
523 root, fs_info, location->objectid);
524
525 path = btrfs_alloc_path();
526 BUG_ON(!path);
527 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
528 if (ret != 0) {
529 if (ret > 0)
530 ret = -ENOENT;
531 goto out;
532 }
533 l = path->nodes[0];
534 read_extent_buffer(l, &root->root_item,
535 btrfs_item_ptr_offset(l, path->slots[0]),
536 sizeof(root->root_item));
537 memcpy(&root->root_key, location, sizeof(*location));
538 ret = 0;
539 out:
540 btrfs_release_path(root, path);
541 btrfs_free_path(path);
542 if (ret) {
543 kfree(root);
544 return ERR_PTR(ret);
545 }
546 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
547 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
548 blocksize);
549 BUG_ON(!root->node);
550 insert:
551 root->ref_cows = 1;
552 ret = btrfs_find_highest_inode(root, &highest_inode);
553 if (ret == 0) {
554 root->highest_inode = highest_inode;
555 root->last_inode_alloc = highest_inode;
556 }
557 return root;
558 }
559
560 struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
561 u64 root_objectid)
562 {
563 struct btrfs_root *root;
564
565 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
566 return fs_info->tree_root;
567 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
568 return fs_info->extent_root;
569
570 root = radix_tree_lookup(&fs_info->fs_roots_radix,
571 (unsigned long)root_objectid);
572 return root;
573 }
574
575 struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
576 struct btrfs_key *location)
577 {
578 struct btrfs_root *root;
579 int ret;
580
581 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
582 return fs_info->tree_root;
583 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
584 return fs_info->extent_root;
585
586 root = radix_tree_lookup(&fs_info->fs_roots_radix,
587 (unsigned long)location->objectid);
588 if (root)
589 return root;
590
591 root = btrfs_read_fs_root_no_radix(fs_info, location);
592 if (IS_ERR(root))
593 return root;
594 ret = radix_tree_insert(&fs_info->fs_roots_radix,
595 (unsigned long)root->root_key.objectid,
596 root);
597 if (ret) {
598 free_extent_buffer(root->node);
599 kfree(root);
600 return ERR_PTR(ret);
601 }
602 ret = btrfs_find_dead_roots(fs_info->tree_root,
603 root->root_key.objectid, root);
604 BUG_ON(ret);
605
606 return root;
607 }
608
609 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
610 struct btrfs_key *location,
611 const char *name, int namelen)
612 {
613 struct btrfs_root *root;
614 int ret;
615
616 root = btrfs_read_fs_root_no_name(fs_info, location);
617 if (!root)
618 return NULL;
619
620 if (root->in_sysfs)
621 return root;
622
623 ret = btrfs_set_root_name(root, name, namelen);
624 if (ret) {
625 free_extent_buffer(root->node);
626 kfree(root);
627 return ERR_PTR(ret);
628 }
629
630 ret = btrfs_sysfs_add_root(root);
631 if (ret) {
632 free_extent_buffer(root->node);
633 kfree(root->name);
634 kfree(root);
635 return ERR_PTR(ret);
636 }
637 root->in_sysfs = 1;
638 return root;
639 }
640 #if 0
641 static int add_hasher(struct btrfs_fs_info *info, char *type) {
642 struct btrfs_hasher *hasher;
643
644 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
645 if (!hasher)
646 return -ENOMEM;
647 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
648 if (!hasher->hash_tfm) {
649 kfree(hasher);
650 return -EINVAL;
651 }
652 spin_lock(&info->hash_lock);
653 list_add(&hasher->list, &info->hashers);
654 spin_unlock(&info->hash_lock);
655 return 0;
656 }
657 #endif
658 struct btrfs_root *open_ctree(struct super_block *sb,
659 struct btrfs_fs_devices *fs_devices)
660 {
661 u32 sectorsize;
662 u32 nodesize;
663 u32 leafsize;
664 u32 blocksize;
665 u32 stripesize;
666 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
667 GFP_NOFS);
668 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
669 GFP_NOFS);
670 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
671 GFP_NOFS);
672 struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
673 GFP_NOFS);
674 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
675 GFP_NOFS);
676 int ret;
677 int err = -EIO;
678 struct btrfs_super_block *disk_super;
679
680 if (!extent_root || !tree_root || !fs_info) {
681 err = -ENOMEM;
682 goto fail;
683 }
684 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
685 INIT_LIST_HEAD(&fs_info->trans_list);
686 INIT_LIST_HEAD(&fs_info->dead_roots);
687 INIT_LIST_HEAD(&fs_info->hashers);
688 spin_lock_init(&fs_info->hash_lock);
689 spin_lock_init(&fs_info->delalloc_lock);
690 spin_lock_init(&fs_info->new_trans_lock);
691
692 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
693 init_completion(&fs_info->kobj_unregister);
694 sb_set_blocksize(sb, 4096);
695 fs_info->running_transaction = NULL;
696 fs_info->last_trans_committed = 0;
697 fs_info->tree_root = tree_root;
698 fs_info->extent_root = extent_root;
699 fs_info->chunk_root = chunk_root;
700 fs_info->dev_root = dev_root;
701 fs_info->fs_devices = fs_devices;
702 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
703 INIT_LIST_HEAD(&fs_info->space_info);
704 btrfs_mapping_init(&fs_info->mapping_tree);
705 fs_info->sb = sb;
706 fs_info->throttles = 0;
707 fs_info->mount_opt = 0;
708 fs_info->max_extent = (u64)-1;
709 fs_info->max_inline = 8192 * 1024;
710 fs_info->delalloc_bytes = 0;
711 fs_info->btree_inode = new_inode(sb);
712 fs_info->btree_inode->i_ino = 1;
713 fs_info->btree_inode->i_nlink = 1;
714 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
715 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
716 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
717 fs_info->btree_inode->i_mapping,
718 GFP_NOFS);
719 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
720 GFP_NOFS);
721
722 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
723
724 extent_io_tree_init(&fs_info->free_space_cache,
725 fs_info->btree_inode->i_mapping, GFP_NOFS);
726 extent_io_tree_init(&fs_info->block_group_cache,
727 fs_info->btree_inode->i_mapping, GFP_NOFS);
728 extent_io_tree_init(&fs_info->pinned_extents,
729 fs_info->btree_inode->i_mapping, GFP_NOFS);
730 extent_io_tree_init(&fs_info->pending_del,
731 fs_info->btree_inode->i_mapping, GFP_NOFS);
732 extent_io_tree_init(&fs_info->extent_ins,
733 fs_info->btree_inode->i_mapping, GFP_NOFS);
734 fs_info->do_barriers = 1;
735 fs_info->closing = 0;
736 fs_info->total_pinned = 0;
737 fs_info->last_alloc = 0;
738 fs_info->last_data_alloc = 0;
739 fs_info->extra_alloc_bits = 0;
740 fs_info->extra_data_alloc_bits = 0;
741
742 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
743 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
744 #else
745 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
746 #endif
747 BTRFS_I(fs_info->btree_inode)->root = tree_root;
748 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
749 sizeof(struct btrfs_key));
750 insert_inode_hash(fs_info->btree_inode);
751 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
752
753 mutex_init(&fs_info->trans_mutex);
754 mutex_init(&fs_info->fs_mutex);
755
756 #if 0
757 ret = add_hasher(fs_info, "crc32c");
758 if (ret) {
759 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
760 err = -ENOMEM;
761 goto fail_iput;
762 }
763 #endif
764 __setup_root(4096, 4096, 4096, 4096, tree_root,
765 fs_info, BTRFS_ROOT_TREE_OBJECTID);
766
767 fs_info->sb_buffer = read_tree_block(tree_root,
768 BTRFS_SUPER_INFO_OFFSET,
769 4096);
770
771 if (!fs_info->sb_buffer)
772 goto fail_iput;
773
774 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
775 sizeof(fs_info->super_copy));
776
777 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
778 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
779 BTRFS_FSID_SIZE);
780
781 disk_super = &fs_info->super_copy;
782 if (!btrfs_super_root(disk_super))
783 goto fail_sb_buffer;
784
785 if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
786 printk("Btrfs: wanted %llu devices, but found %llu\n",
787 (unsigned long long)btrfs_super_num_devices(disk_super),
788 (unsigned long long)fs_devices->num_devices);
789 goto fail_sb_buffer;
790 }
791 nodesize = btrfs_super_nodesize(disk_super);
792 leafsize = btrfs_super_leafsize(disk_super);
793 sectorsize = btrfs_super_sectorsize(disk_super);
794 stripesize = btrfs_super_stripesize(disk_super);
795 tree_root->nodesize = nodesize;
796 tree_root->leafsize = leafsize;
797 tree_root->sectorsize = sectorsize;
798 tree_root->stripesize = stripesize;
799 sb_set_blocksize(sb, sectorsize);
800
801 i_size_write(fs_info->btree_inode,
802 btrfs_super_total_bytes(disk_super));
803
804 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
805 sizeof(disk_super->magic))) {
806 printk("btrfs: valid FS not found on %s\n", sb->s_id);
807 goto fail_sb_buffer;
808 }
809
810 mutex_lock(&fs_info->fs_mutex);
811
812 ret = btrfs_read_sys_array(tree_root);
813 BUG_ON(ret);
814
815 blocksize = btrfs_level_size(tree_root,
816 btrfs_super_chunk_root_level(disk_super));
817
818 __setup_root(nodesize, leafsize, sectorsize, stripesize,
819 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
820
821 chunk_root->node = read_tree_block(chunk_root,
822 btrfs_super_chunk_root(disk_super),
823 blocksize);
824 BUG_ON(!chunk_root->node);
825
826 ret = btrfs_read_chunk_tree(chunk_root);
827 BUG_ON(ret);
828
829 blocksize = btrfs_level_size(tree_root,
830 btrfs_super_root_level(disk_super));
831
832
833 tree_root->node = read_tree_block(tree_root,
834 btrfs_super_root(disk_super),
835 blocksize);
836 if (!tree_root->node)
837 goto fail_sb_buffer;
838
839
840 ret = find_and_setup_root(tree_root, fs_info,
841 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
842 if (ret)
843 goto fail_tree_root;
844 extent_root->track_dirty = 1;
845
846 ret = find_and_setup_root(tree_root, fs_info,
847 BTRFS_DEV_TREE_OBJECTID, dev_root);
848 dev_root->track_dirty = 1;
849
850 if (ret)
851 goto fail_extent_root;
852
853 btrfs_read_block_groups(extent_root);
854
855 fs_info->generation = btrfs_super_generation(disk_super) + 1;
856 mutex_unlock(&fs_info->fs_mutex);
857 return tree_root;
858
859 fail_extent_root:
860 free_extent_buffer(extent_root->node);
861 fail_tree_root:
862 mutex_unlock(&fs_info->fs_mutex);
863 free_extent_buffer(tree_root->node);
864 fail_sb_buffer:
865 free_extent_buffer(fs_info->sb_buffer);
866 fail_iput:
867 iput(fs_info->btree_inode);
868 fail:
869 close_all_devices(fs_info);
870 kfree(extent_root);
871 kfree(tree_root);
872 kfree(fs_info);
873 return ERR_PTR(err);
874 }
875
876 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
877 *root)
878 {
879 int ret;
880 struct extent_buffer *super = root->fs_info->sb_buffer;
881 struct inode *btree_inode = root->fs_info->btree_inode;
882 struct super_block *sb = root->fs_info->sb;
883
884 if (!btrfs_test_opt(root, NOBARRIER))
885 blkdev_issue_flush(sb->s_bdev, NULL);
886 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
887 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
888 super->start, super->len);
889 if (!btrfs_test_opt(root, NOBARRIER))
890 blkdev_issue_flush(sb->s_bdev, NULL);
891 return ret;
892 }
893
894 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
895 {
896 radix_tree_delete(&fs_info->fs_roots_radix,
897 (unsigned long)root->root_key.objectid);
898 if (root->in_sysfs)
899 btrfs_sysfs_del_root(root);
900 if (root->inode)
901 iput(root->inode);
902 if (root->node)
903 free_extent_buffer(root->node);
904 if (root->commit_root)
905 free_extent_buffer(root->commit_root);
906 if (root->name)
907 kfree(root->name);
908 kfree(root);
909 return 0;
910 }
911
912 static int del_fs_roots(struct btrfs_fs_info *fs_info)
913 {
914 int ret;
915 struct btrfs_root *gang[8];
916 int i;
917
918 while(1) {
919 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
920 (void **)gang, 0,
921 ARRAY_SIZE(gang));
922 if (!ret)
923 break;
924 for (i = 0; i < ret; i++)
925 btrfs_free_fs_root(fs_info, gang[i]);
926 }
927 return 0;
928 }
929
930 int close_ctree(struct btrfs_root *root)
931 {
932 int ret;
933 struct btrfs_trans_handle *trans;
934 struct btrfs_fs_info *fs_info = root->fs_info;
935
936 fs_info->closing = 1;
937 btrfs_transaction_flush_work(root);
938 mutex_lock(&fs_info->fs_mutex);
939 btrfs_defrag_dirty_roots(root->fs_info);
940 trans = btrfs_start_transaction(root, 1);
941 ret = btrfs_commit_transaction(trans, root);
942 /* run commit again to drop the original snapshot */
943 trans = btrfs_start_transaction(root, 1);
944 btrfs_commit_transaction(trans, root);
945 ret = btrfs_write_and_wait_transaction(NULL, root);
946 BUG_ON(ret);
947 write_ctree_super(NULL, root);
948 mutex_unlock(&fs_info->fs_mutex);
949
950 if (fs_info->delalloc_bytes) {
951 printk("btrfs: at unmount delalloc count %Lu\n",
952 fs_info->delalloc_bytes);
953 }
954 if (fs_info->extent_root->node)
955 free_extent_buffer(fs_info->extent_root->node);
956
957 if (fs_info->tree_root->node)
958 free_extent_buffer(fs_info->tree_root->node);
959
960 if (root->fs_info->chunk_root->node);
961 free_extent_buffer(root->fs_info->chunk_root->node);
962
963 if (root->fs_info->dev_root->node);
964 free_extent_buffer(root->fs_info->dev_root->node);
965
966 free_extent_buffer(fs_info->sb_buffer);
967
968 btrfs_free_block_groups(root->fs_info);
969 del_fs_roots(fs_info);
970
971 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
972
973 extent_io_tree_empty_lru(&fs_info->free_space_cache);
974 extent_io_tree_empty_lru(&fs_info->block_group_cache);
975 extent_io_tree_empty_lru(&fs_info->pinned_extents);
976 extent_io_tree_empty_lru(&fs_info->pending_del);
977 extent_io_tree_empty_lru(&fs_info->extent_ins);
978 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
979
980 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
981
982 iput(fs_info->btree_inode);
983 #if 0
984 while(!list_empty(&fs_info->hashers)) {
985 struct btrfs_hasher *hasher;
986 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
987 hashers);
988 list_del(&hasher->hashers);
989 crypto_free_hash(&fs_info->hash_tfm);
990 kfree(hasher);
991 }
992 #endif
993 close_all_devices(fs_info);
994 btrfs_mapping_tree_free(&fs_info->mapping_tree);
995
996 kfree(fs_info->extent_root);
997 kfree(fs_info->tree_root);
998 kfree(fs_info->chunk_root);
999 kfree(fs_info->dev_root);
1000 return 0;
1001 }
1002
1003 int btrfs_buffer_uptodate(struct extent_buffer *buf)
1004 {
1005 struct inode *btree_inode = buf->first_page->mapping->host;
1006 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
1007 }
1008
1009 int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
1010 {
1011 struct inode *btree_inode = buf->first_page->mapping->host;
1012 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
1013 buf);
1014 }
1015
1016 void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
1017 {
1018 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1019 u64 transid = btrfs_header_generation(buf);
1020 struct inode *btree_inode = root->fs_info->btree_inode;
1021
1022 if (transid != root->fs_info->generation) {
1023 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
1024 (unsigned long long)buf->start,
1025 transid, root->fs_info->generation);
1026 WARN_ON(1);
1027 }
1028 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
1029 }
1030
1031 void btrfs_throttle(struct btrfs_root *root)
1032 {
1033 struct backing_dev_info *bdi;
1034
1035 bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
1036 if (root->fs_info->throttles && bdi_write_congested(bdi)) {
1037 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1038 congestion_wait(WRITE, HZ/20);
1039 #else
1040 blk_congestion_wait(WRITE, HZ/20);
1041 #endif
1042 }
1043 }
1044
1045 void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
1046 {
1047 balance_dirty_pages_ratelimited_nr(
1048 root->fs_info->btree_inode->i_mapping, 1);
1049 }
1050
1051 void btrfs_set_buffer_defrag(struct extent_buffer *buf)
1052 {
1053 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1054 struct inode *btree_inode = root->fs_info->btree_inode;
1055 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
1056 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
1057 }
1058
1059 void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
1060 {
1061 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1062 struct inode *btree_inode = root->fs_info->btree_inode;
1063 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
1064 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
1065 GFP_NOFS);
1066 }
1067
1068 int btrfs_buffer_defrag(struct extent_buffer *buf)
1069 {
1070 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1071 struct inode *btree_inode = root->fs_info->btree_inode;
1072 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
1073 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
1074 }
1075
1076 int btrfs_buffer_defrag_done(struct extent_buffer *buf)
1077 {
1078 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1079 struct inode *btree_inode = root->fs_info->btree_inode;
1080 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
1081 buf->start, buf->start + buf->len - 1,
1082 EXTENT_DEFRAG_DONE, 0);
1083 }
1084
1085 int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
1086 {
1087 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1088 struct inode *btree_inode = root->fs_info->btree_inode;
1089 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
1090 buf->start, buf->start + buf->len - 1,
1091 EXTENT_DEFRAG_DONE, GFP_NOFS);
1092 }
1093
1094 int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
1095 {
1096 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1097 struct inode *btree_inode = root->fs_info->btree_inode;
1098 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
1099 buf->start, buf->start + buf->len - 1,
1100 EXTENT_DEFRAG, GFP_NOFS);
1101 }
1102
1103 int btrfs_read_buffer(struct extent_buffer *buf)
1104 {
1105 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1106 struct inode *btree_inode = root->fs_info->btree_inode;
1107 return read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
1108 buf, 0, 1, btree_get_extent);
1109 }
1110
1111 static struct extent_io_ops btree_extent_io_ops = {
1112 .writepage_io_hook = btree_writepage_io_hook,
1113 .submit_bio_hook = btree_submit_bio_hook,
1114 /* note we're sharing with inode.c for the merge bio hook */
1115 .merge_bio_hook = btrfs_merge_bio_hook,
1116 };
This page took 0.054538 seconds and 6 git commands to generate.