]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/btrfs/disk-io.c
Btrfs: add disk ioctl, mostly working
[mv-sheeva.git] / fs / btrfs / disk-io.c
1 #include <linux/module.h>
2 #include <linux/fs.h>
3 #include <linux/blkdev.h>
4 #include <linux/crypto.h>
5 #include <linux/scatterlist.h>
6 #include <linux/swap.h>
7 #include <linux/radix-tree.h>
8 #include "ctree.h"
9 #include "disk-io.h"
10 #include "transaction.h"
11 #include "btrfs_inode.h"
12
13 struct dev_lookup {
14         u64 block_start;
15         u64 num_blocks;
16         struct block_device *bdev;
17 };
18
19 int btrfs_insert_dev_radix(struct btrfs_root *root,
20                            struct block_device *bdev,
21                            u64 block_start,
22                            u64 num_blocks)
23 {
24         struct dev_lookup *lookup;
25         char b[BDEVNAME_SIZE];
26         int ret;
27
28         lookup = kmalloc(sizeof(*lookup), GFP_NOFS);
29         if (!lookup)
30                 return -ENOMEM;
31         lookup->block_start = block_start;
32         lookup->num_blocks = num_blocks;
33         lookup->bdev = bdev;
34 printk("inserting %s into dev radix %Lu %Lu\n", bdevname(bdev, b), block_start, num_blocks);
35
36         ret = radix_tree_insert(&root->fs_info->dev_radix, block_start +
37                                 num_blocks - 1, lookup);
38         return ret;
39 }
40
41 u64 bh_blocknr(struct buffer_head *bh)
42 {
43         int blkbits = bh->b_page->mapping->host->i_blkbits;
44         u64 blocknr = bh->b_page->index << (PAGE_CACHE_SHIFT - blkbits);
45         unsigned long offset;
46
47         if (PageHighMem(bh->b_page))
48                 offset = (unsigned long)bh->b_data;
49         else
50                 offset = bh->b_data - (char *)page_address(bh->b_page);
51         blocknr += offset >> (PAGE_CACHE_SHIFT - blkbits);
52         return blocknr;
53 }
54
55 static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
56 {
57         struct btrfs_node *node = btrfs_buffer_node(buf);
58         if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
59                 printk(KERN_CRIT "bh_blocknr(buf) is %Lu, header is %Lu\n",
60                        bh_blocknr(buf), btrfs_header_blocknr(&node->header));
61                 BUG();
62         }
63         return 0;
64 }
65
66 struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
67 {
68         struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
69         int blockbits = root->fs_info->sb->s_blocksize_bits;
70         unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
71         struct page *page;
72         struct buffer_head *bh;
73         struct buffer_head *head;
74         struct buffer_head *ret = NULL;
75
76
77         page = find_lock_page(mapping, index);
78         if (!page)
79                 return NULL;
80
81         if (!page_has_buffers(page))
82                 goto out_unlock;
83
84         head = page_buffers(page);
85         bh = head;
86         do {
87                 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
88                         ret = bh;
89                         get_bh(bh);
90                         goto out_unlock;
91                 }
92                 bh = bh->b_this_page;
93         } while (bh != head);
94 out_unlock:
95         unlock_page(page);
96         if (ret) {
97                 touch_buffer(ret);
98         }
99         page_cache_release(page);
100         return ret;
101 }
102
103 int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
104                              u64 logical)
105 {
106         struct dev_lookup *lookup[2];
107
108         int ret;
109
110         root = root->fs_info->dev_root;
111         ret = radix_tree_gang_lookup(&root->fs_info->dev_radix,
112                                      (void **)lookup,
113                                      (unsigned long)logical,
114                                      ARRAY_SIZE(lookup));
115         if (ret == 0 || lookup[0]->block_start > logical ||
116             lookup[0]->block_start + lookup[0]->num_blocks <= logical) {
117                 ret = -ENOENT;
118                 goto out;
119         }
120         bh->b_bdev = lookup[0]->bdev;
121         bh->b_blocknr = logical - lookup[0]->block_start;
122         set_buffer_mapped(bh);
123         ret = 0;
124 out:
125         return ret;
126 }
127
128 struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
129                                                  u64 blocknr)
130 {
131         struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
132         int blockbits = root->fs_info->sb->s_blocksize_bits;
133         unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
134         struct page *page;
135         struct buffer_head *bh;
136         struct buffer_head *head;
137         struct buffer_head *ret = NULL;
138         int err;
139         u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
140
141         page = grab_cache_page(mapping, index);
142         if (!page)
143                 return NULL;
144
145         if (!page_has_buffers(page))
146                 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
147         head = page_buffers(page);
148         bh = head;
149         do {
150                 if (!buffer_mapped(bh)) {
151                         err = btrfs_map_bh_to_logical(root, bh, first_block);
152                         BUG_ON(err);
153                 }
154                 if (bh_blocknr(bh) == blocknr) {
155                         ret = bh;
156                         get_bh(bh);
157                         goto out_unlock;
158                 }
159                 bh = bh->b_this_page;
160                 first_block++;
161         } while (bh != head);
162 out_unlock:
163         unlock_page(page);
164         if (ret)
165                 touch_buffer(ret);
166         page_cache_release(page);
167         return ret;
168 }
169
170 static int btree_get_block(struct inode *inode, sector_t iblock,
171                            struct buffer_head *bh, int create)
172 {
173         int err;
174         struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
175         err = btrfs_map_bh_to_logical(root, bh, iblock);
176         return err;
177 }
178
179 int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
180                     char *result)
181 {
182         struct scatterlist sg;
183         struct crypto_hash *tfm = root->fs_info->hash_tfm;
184         struct hash_desc desc;
185         int ret;
186
187         desc.tfm = tfm;
188         desc.flags = 0;
189         sg_init_one(&sg, data, len);
190         spin_lock(&root->fs_info->hash_lock);
191         ret = crypto_hash_digest(&desc, &sg, 1, result);
192         spin_unlock(&root->fs_info->hash_lock);
193         if (ret) {
194                 printk("sha256 digest failed\n");
195         }
196         return ret;
197 }
198 static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
199                            int verify)
200 {
201         char result[BTRFS_CSUM_SIZE];
202         int ret;
203         struct btrfs_node *node;
204
205         ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
206                               bh->b_size - BTRFS_CSUM_SIZE, result);
207         if (ret)
208                 return ret;
209         if (verify) {
210                 if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) {
211                         printk("checksum verify failed on %Lu\n",
212                                bh_blocknr(bh));
213                         return 1;
214                 }
215         } else {
216                 node = btrfs_buffer_node(bh);
217                 memcpy(node->header.csum, result, BTRFS_CSUM_SIZE);
218         }
219         return 0;
220 }
221
222 static int btree_writepage(struct page *page, struct writeback_control *wbc)
223 {
224         struct buffer_head *bh;
225         struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
226         struct buffer_head *head;
227         if (!page_has_buffers(page)) {
228                 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
229                                         (1 << BH_Dirty)|(1 << BH_Uptodate));
230         }
231         head = page_buffers(page);
232         bh = head;
233         do {
234                 if (buffer_dirty(bh))
235                         csum_tree_block(root, bh, 0);
236                 bh = bh->b_this_page;
237         } while (bh != head);
238         return block_write_full_page(page, btree_get_block, wbc);
239 }
240
241 static int btree_readpage(struct file * file, struct page * page)
242 {
243         return block_read_full_page(page, btree_get_block);
244 }
245
246 static struct address_space_operations btree_aops = {
247         .readpage       = btree_readpage,
248         .writepage      = btree_writepage,
249         .sync_page      = block_sync_page,
250 };
251
252 struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
253 {
254         struct buffer_head *bh = NULL;
255
256         bh = btrfs_find_create_tree_block(root, blocknr);
257         if (!bh)
258                 return bh;
259         if (buffer_uptodate(bh))
260                 goto uptodate;
261         lock_buffer(bh);
262         if (!buffer_uptodate(bh)) {
263                 get_bh(bh);
264                 bh->b_end_io = end_buffer_read_sync;
265                 submit_bh(READ, bh);
266                 wait_on_buffer(bh);
267                 if (!buffer_uptodate(bh))
268                         goto fail;
269                 csum_tree_block(root, bh, 1);
270         } else {
271                 unlock_buffer(bh);
272         }
273 uptodate:
274         if (check_tree_block(root, bh))
275                 BUG();
276         return bh;
277 fail:
278         brelse(bh);
279         return NULL;
280 }
281
282 int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
283                      struct buffer_head *buf)
284 {
285         WARN_ON(atomic_read(&buf->b_count) == 0);
286         mark_buffer_dirty(buf);
287         return 0;
288 }
289
290 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
291                      struct buffer_head *buf)
292 {
293         WARN_ON(atomic_read(&buf->b_count) == 0);
294         clear_buffer_dirty(buf);
295         return 0;
296 }
297
298 static int __setup_root(int blocksize,
299                         struct btrfs_root *root,
300                         struct btrfs_fs_info *fs_info,
301                         u64 objectid)
302 {
303         root->node = NULL;
304         root->inode = NULL;
305         root->commit_root = NULL;
306         root->blocksize = blocksize;
307         root->ref_cows = 0;
308         root->fs_info = fs_info;
309         root->objectid = objectid;
310         root->last_trans = 0;
311         root->highest_inode = 0;
312         root->last_inode_alloc = 0;
313         memset(&root->root_key, 0, sizeof(root->root_key));
314         memset(&root->root_item, 0, sizeof(root->root_item));
315         return 0;
316 }
317
318 static int find_and_setup_root(int blocksize,
319                                struct btrfs_root *tree_root,
320                                struct btrfs_fs_info *fs_info,
321                                u64 objectid,
322                                struct btrfs_root *root)
323 {
324         int ret;
325
326         __setup_root(blocksize, root, fs_info, objectid);
327         ret = btrfs_find_last_root(tree_root, objectid,
328                                    &root->root_item, &root->root_key);
329         BUG_ON(ret);
330
331         root->node = read_tree_block(root,
332                                      btrfs_root_blocknr(&root->root_item));
333         BUG_ON(!root->node);
334         return 0;
335 }
336
337 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
338                                       struct btrfs_key *location)
339 {
340         struct btrfs_root *root;
341         struct btrfs_root *tree_root = fs_info->tree_root;
342         struct btrfs_path *path;
343         struct btrfs_leaf *l;
344         u64 highest_inode;
345         int ret = 0;
346
347 printk("read_fs_root looking for %Lu %Lu %u\n", location->objectid, location->offset, location->flags);
348         root = radix_tree_lookup(&fs_info->fs_roots_radix,
349                                  (unsigned long)location->objectid);
350         if (root) {
351 printk("found %p in cache\n", root);
352                 return root;
353         }
354         root = kmalloc(sizeof(*root), GFP_NOFS);
355         if (!root) {
356 printk("failed1\n");
357                 return ERR_PTR(-ENOMEM);
358         }
359         if (location->offset == (u64)-1) {
360                 ret = find_and_setup_root(fs_info->sb->s_blocksize,
361                                           fs_info->tree_root, fs_info,
362                                           location->objectid, root);
363                 if (ret) {
364 printk("failed2\n");
365                         kfree(root);
366                         return ERR_PTR(ret);
367                 }
368                 goto insert;
369         }
370
371         __setup_root(fs_info->sb->s_blocksize, root, fs_info,
372                      location->objectid);
373
374         path = btrfs_alloc_path();
375         BUG_ON(!path);
376         ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
377         if (ret != 0) {
378 printk("internal search_slot gives us %d\n", ret);
379                 if (ret > 0)
380                         ret = -ENOENT;
381                 goto out;
382         }
383         l = btrfs_buffer_leaf(path->nodes[0]);
384         memcpy(&root->root_item,
385                btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
386                sizeof(root->root_item));
387         memcpy(&root->root_key, location, sizeof(*location));
388         ret = 0;
389 out:
390         btrfs_release_path(root, path);
391         btrfs_free_path(path);
392         if (ret) {
393                 kfree(root);
394                 return ERR_PTR(ret);
395         }
396         root->node = read_tree_block(root,
397                                      btrfs_root_blocknr(&root->root_item));
398         BUG_ON(!root->node);
399 insert:
400 printk("inserting %p\n", root);
401         root->ref_cows = 1;
402         ret = radix_tree_insert(&fs_info->fs_roots_radix,
403                                 (unsigned long)root->root_key.objectid,
404                                 root);
405         if (ret) {
406 printk("radix_tree_insert gives us %d\n", ret);
407                 brelse(root->node);
408                 kfree(root);
409                 return ERR_PTR(ret);
410         }
411         ret = btrfs_find_highest_inode(root, &highest_inode);
412         if (ret == 0) {
413                 root->highest_inode = highest_inode;
414                 root->last_inode_alloc = highest_inode;
415 printk("highest inode is %Lu\n", highest_inode);
416         }
417 printk("all worked\n");
418         return root;
419 }
420
421 int btrfs_open_disk(struct btrfs_root *root, u64 block_start, u64 num_blocks,
422                     char *filename, int name_len)
423 {
424         char *null_filename;
425         struct block_device *bdev;
426         int ret;
427
428         if (block_start == 0) {
429 printk("skipping disk with block_start == 0\n");
430 return 0;
431         }
432         null_filename = kmalloc(name_len + 1, GFP_NOFS);
433         if (!null_filename)
434                 return -ENOMEM;
435         memcpy(null_filename, filename, name_len);
436         null_filename[name_len] = '\0';
437
438         bdev = open_bdev_excl(null_filename, O_RDWR, root->fs_info->sb);
439         if (IS_ERR(bdev)) {
440                 ret = PTR_ERR(bdev);
441                 goto out;
442         }
443         set_blocksize(bdev, root->fs_info->sb->s_blocksize);
444         ret = btrfs_insert_dev_radix(root, bdev, block_start, num_blocks);
445         BUG_ON(ret);
446         ret = 0;
447 out:
448         kfree(null_filename);
449         return ret;
450 }
451
452 static int read_device_info(struct btrfs_root *root)
453 {
454         struct btrfs_path *path;
455         int ret;
456         struct btrfs_key key;
457         struct btrfs_leaf *leaf;
458         struct btrfs_device_item *dev_item;
459         int nritems;
460         int slot;
461
462         root = root->fs_info->dev_root;
463
464         path = btrfs_alloc_path();
465         if (!path)
466                 return -ENOMEM;
467         key.objectid = 0;
468         key.offset = 0;
469         key.flags = 0;
470         btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
471
472         mutex_lock(&root->fs_info->fs_mutex);
473         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
474         leaf = btrfs_buffer_leaf(path->nodes[0]);
475         nritems = btrfs_header_nritems(&leaf->header);
476         while(1) {
477                 slot = path->slots[0];
478                 if (slot >= nritems) {
479                         ret = btrfs_next_leaf(root, path);
480                         if (ret)
481                                 break;
482                         leaf = btrfs_buffer_leaf(path->nodes[0]);
483                         nritems = btrfs_header_nritems(&leaf->header);
484                         slot = path->slots[0];
485                 }
486                 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
487                 if (btrfs_key_type(&key) != BTRFS_DEV_ITEM_KEY) {
488                         path->slots[0]++;
489                         continue;
490                 }
491                 dev_item = btrfs_item_ptr(leaf, slot, struct btrfs_device_item);
492 printk("found key %Lu %Lu\n", key.objectid, key.offset);
493                 ret = btrfs_open_disk(root, key.objectid, key.offset,
494                                       (char *)(dev_item + 1),
495                                       btrfs_device_pathlen(dev_item));
496                 BUG_ON(ret);
497                 path->slots[0]++;
498         }
499         btrfs_free_path(path);
500         mutex_unlock(&root->fs_info->fs_mutex);
501         return 0;
502 }
503
504 struct btrfs_root *open_ctree(struct super_block *sb)
505 {
506         struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
507                                                  GFP_NOFS);
508         struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
509                                                  GFP_NOFS);
510         struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
511                                                GFP_NOFS);
512         struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
513                                                 GFP_NOFS);
514         int ret;
515         struct btrfs_super_block *disk_super;
516         struct dev_lookup *dev_lookup;
517
518         init_bit_radix(&fs_info->pinned_radix);
519         init_bit_radix(&fs_info->pending_del_radix);
520         INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
521         INIT_RADIX_TREE(&fs_info->dev_radix, GFP_NOFS);
522         sb_set_blocksize(sb, 4096);
523         fs_info->running_transaction = NULL;
524         fs_info->tree_root = tree_root;
525         fs_info->extent_root = extent_root;
526         fs_info->dev_root = dev_root;
527         fs_info->sb = sb;
528         fs_info->btree_inode = new_inode(sb);
529         fs_info->btree_inode->i_ino = 1;
530         fs_info->btree_inode->i_nlink = 1;
531         fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
532         fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
533         BTRFS_I(fs_info->btree_inode)->root = tree_root;
534         memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
535                sizeof(struct btrfs_key));
536         insert_inode_hash(fs_info->btree_inode);
537         mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
538         fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
539         spin_lock_init(&fs_info->hash_lock);
540         if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
541                 printk("failed to allocate sha256 hash\n");
542                 return NULL;
543         }
544         mutex_init(&fs_info->trans_mutex);
545         mutex_init(&fs_info->fs_mutex);
546         memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
547         memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
548
549         __setup_root(sb->s_blocksize, dev_root,
550                      fs_info, BTRFS_DEV_TREE_OBJECTID);
551
552         __setup_root(sb->s_blocksize, tree_root,
553                      fs_info, BTRFS_ROOT_TREE_OBJECTID);
554
555         dev_lookup = kmalloc(sizeof(*dev_lookup), GFP_NOFS);
556         dev_lookup->block_start = 0;
557         dev_lookup->num_blocks = (u32)-2;
558         dev_lookup->bdev = sb->s_bdev;
559         ret = radix_tree_insert(&fs_info->dev_radix, (u32)-2, dev_lookup);
560         BUG_ON(ret);
561         fs_info->sb_buffer = read_tree_block(tree_root,
562                                              BTRFS_SUPER_INFO_OFFSET /
563                                              sb->s_blocksize);
564
565         if (!fs_info->sb_buffer)
566                 return NULL;
567         disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
568         if (!btrfs_super_root(disk_super))
569                 return NULL;
570
571         i_size_write(fs_info->btree_inode,
572                      btrfs_super_total_blocks(disk_super) <<
573                      fs_info->btree_inode->i_blkbits);
574
575         radix_tree_delete(&fs_info->dev_radix, (u32)-2);
576         dev_lookup->block_start = btrfs_super_device_block_start(disk_super);
577         dev_lookup->num_blocks = btrfs_super_device_num_blocks(disk_super);
578         ret = radix_tree_insert(&fs_info->dev_radix,
579                                 dev_lookup->block_start +
580                                 dev_lookup->num_blocks - 1, dev_lookup);
581         BUG_ON(ret);
582
583         fs_info->disk_super = disk_super;
584
585         dev_root->node = read_tree_block(tree_root,
586                                           btrfs_super_device_root(disk_super));
587
588         ret = read_device_info(dev_root);
589         BUG_ON(ret);
590
591         tree_root->node = read_tree_block(tree_root,
592                                           btrfs_super_root(disk_super));
593         BUG_ON(!tree_root->node);
594
595         mutex_lock(&fs_info->fs_mutex);
596         ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
597                                   BTRFS_EXTENT_TREE_OBJECTID, extent_root);
598         BUG_ON(ret);
599
600         fs_info->generation = btrfs_super_generation(disk_super) + 1;
601         memset(&fs_info->kobj, 0, sizeof(fs_info->kobj));
602         kobj_set_kset_s(fs_info, btrfs_subsys);
603         kobject_set_name(&fs_info->kobj, "%s", sb->s_id);
604         kobject_register(&fs_info->kobj);
605         mutex_unlock(&fs_info->fs_mutex);
606         return tree_root;
607 }
608
609 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
610                       *root)
611 {
612         struct buffer_head *bh = root->fs_info->sb_buffer;
613
614         btrfs_set_super_root(root->fs_info->disk_super,
615                              bh_blocknr(root->fs_info->tree_root->node));
616         lock_buffer(bh);
617         WARN_ON(atomic_read(&bh->b_count) < 1);
618         clear_buffer_dirty(bh);
619         csum_tree_block(root, bh, 0);
620         bh->b_end_io = end_buffer_write_sync;
621         get_bh(bh);
622         submit_bh(WRITE, bh);
623         wait_on_buffer(bh);
624         if (!buffer_uptodate(bh)) {
625                 WARN_ON(1);
626                 return -EIO;
627         }
628         return 0;
629 }
630
631 static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
632 {
633         radix_tree_delete(&fs_info->fs_roots_radix,
634                           (unsigned long)root->root_key.objectid);
635         if (root->inode)
636                 iput(root->inode);
637         if (root->node)
638                 brelse(root->node);
639         if (root->commit_root)
640                 brelse(root->commit_root);
641         kfree(root);
642         return 0;
643 }
644
645 int del_fs_roots(struct btrfs_fs_info *fs_info)
646 {
647         int ret;
648         struct btrfs_root *gang[8];
649         int i;
650
651         while(1) {
652                 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
653                                              (void **)gang, 0,
654                                              ARRAY_SIZE(gang));
655                 if (!ret)
656                         break;
657                 for (i = 0; i < ret; i++)
658                         free_fs_root(fs_info, gang[i]);
659         }
660         return 0;
661 }
662 static int free_dev_radix(struct btrfs_fs_info *fs_info)
663 {
664         struct dev_lookup *lookup[8];
665         struct block_device *super_bdev = fs_info->sb->s_bdev;
666         int ret;
667         int i;
668         while(1) {
669                 ret = radix_tree_gang_lookup(&fs_info->dev_radix,
670                                              (void **)lookup, 0,
671                                              ARRAY_SIZE(lookup));
672                 if (!ret)
673                         break;
674                 for (i = 0; i < ret; i++) {
675                         if (lookup[i]->bdev != super_bdev)
676                                 close_bdev_excl(lookup[i]->bdev);
677                         radix_tree_delete(&fs_info->dev_radix,
678                                           lookup[i]->block_start +
679                                           lookup[i]->num_blocks - 1);
680                         kfree(lookup[i]);
681                 }
682         }
683         return 0;
684 }
685
686 int close_ctree(struct btrfs_root *root)
687 {
688         int ret;
689         struct btrfs_trans_handle *trans;
690         struct btrfs_fs_info *fs_info = root->fs_info;
691
692         mutex_lock(&fs_info->fs_mutex);
693         trans = btrfs_start_transaction(root, 1);
694         btrfs_commit_transaction(trans, root);
695         /* run commit again to  drop the original snapshot */
696         trans = btrfs_start_transaction(root, 1);
697         btrfs_commit_transaction(trans, root);
698         ret = btrfs_write_and_wait_transaction(NULL, root);
699         BUG_ON(ret);
700         write_ctree_super(NULL, root);
701         mutex_unlock(&fs_info->fs_mutex);
702
703         if (fs_info->extent_root->node)
704                 btrfs_block_release(fs_info->extent_root,
705                                     fs_info->extent_root->node);
706         if (fs_info->dev_root->node)
707                 btrfs_block_release(fs_info->dev_root,
708                                     fs_info->dev_root->node);
709         if (fs_info->tree_root->node)
710                 btrfs_block_release(fs_info->tree_root,
711                                     fs_info->tree_root->node);
712         btrfs_block_release(root, fs_info->sb_buffer);
713         crypto_free_hash(fs_info->hash_tfm);
714         truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
715         iput(fs_info->btree_inode);
716
717         free_dev_radix(fs_info);
718         del_fs_roots(fs_info);
719         kfree(fs_info->extent_root);
720         kfree(fs_info->tree_root);
721         kobject_unregister(&fs_info->kobj);
722         return 0;
723 }
724
725 void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
726 {
727         brelse(buf);
728 }
729