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