blob: 34cf1f1f47be41a1ac037fe8ce70b08e4bebada9 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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
Chris Masone20d96d2007-03-22 12:13:20 -040019#include <linux/fs.h>
Chris Masond98237b2007-03-28 13:57:48 -040020#include <linux/blkdev.h>
Chris Mason11bd1432007-06-22 14:16:24 -040021#include <linux/crc32c.h>
Chris Mason87cbda52007-03-28 19:44:27 -040022#include <linux/scatterlist.h>
Chris Mason22b0ebd2007-03-30 08:47:31 -040023#include <linux/swap.h>
Chris Mason0f7d52f2007-04-09 10:42:37 -040024#include <linux/radix-tree.h>
Chris Mason35b7e472007-05-02 15:53:43 -040025#include <linux/writeback.h>
Chris Mason5f39d392007-10-15 16:14:19 -040026#include <linux/buffer_head.h> // for block_sync_page
Chris Masoneb60cea2007-02-02 09:18:22 -050027#include "ctree.h"
28#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -040029#include "transaction.h"
Chris Mason0f7d52f2007-04-09 10:42:37 -040030#include "btrfs_inode.h"
Chris Masondb945352007-10-15 16:15:53 -040031#include "print-tree.h"
Chris Masoneb60cea2007-02-02 09:18:22 -050032
Chris Mason5f39d392007-10-15 16:14:19 -040033#if 0
34static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
Chris Mason7eccb902007-04-11 15:53:25 -040035{
Chris Mason5f39d392007-10-15 16:14:19 -040036 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));
Chris Mason39279cc2007-06-12 06:35:45 -040040 return 1;
Chris Masond98237b2007-03-28 13:57:48 -040041 }
Chris Mason9a8dd152007-02-23 08:38:36 -050042 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050043}
Chris Mason5f39d392007-10-15 16:14:19 -040044#endif
Chris Masoneb60cea2007-02-02 09:18:22 -050045
Chris Mason0da54682007-11-07 21:08:01 -050046static struct extent_map_ops btree_extent_map_ops;
47
Chris Mason5f39d392007-10-15 16:14:19 -040048struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -040049 u64 bytenr, u32 blocksize)
Chris Masoned2ff2c2007-03-01 18:59:40 -050050{
Chris Mason5f39d392007-10-15 16:14:19 -040051 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonf510cfe2007-10-15 16:14:48 -040052 struct extent_buffer *eb;
53 eb = find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
Chris Masondb945352007-10-15 16:15:53 -040054 bytenr, blocksize, GFP_NOFS);
Chris Masonf510cfe2007-10-15 16:14:48 -040055 return eb;
Chris Masoned2ff2c2007-03-01 18:59:40 -050056}
57
Chris Mason5f39d392007-10-15 16:14:19 -040058struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -040059 u64 bytenr, u32 blocksize)
Chris Masoneb60cea2007-02-02 09:18:22 -050060{
Chris Mason5f39d392007-10-15 16:14:19 -040061 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonf510cfe2007-10-15 16:14:48 -040062 struct extent_buffer *eb;
Chris Masondb945352007-10-15 16:15:53 -040063
Chris Masonf510cfe2007-10-15 16:14:48 -040064 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -040065 bytenr, blocksize, NULL, GFP_NOFS);
Chris Masonf510cfe2007-10-15 16:14:48 -040066 return eb;
Chris Mason5f39d392007-10-15 16:14:19 -040067}
Chris Mason22b0ebd2007-03-30 08:47:31 -040068
Chris Mason5f39d392007-10-15 16:14:19 -040069struct 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;
Chris Masond98237b2007-03-28 13:57:48 -040076
Chris Mason5f39d392007-10-15 16:14:19 -040077again:
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 }
100out:
101 return em;
102}
103
Chris Mason19c00dd2007-10-15 16:19:22 -0400104u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
105{
106 return crc32c(seed, data, len);
107}
108
109void btrfs_csum_final(u32 crc, char *result)
110{
111 *(__le32 *)result = ~cpu_to_le32(crc);
112}
113
114static 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) {
Chris Masone4204de2008-01-08 15:46:27 -0500148 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",
Chris Mason19c00dd2007-10-15 16:19:22 -0400165 root->fs_info->sb->s_id,
Chris Masone4204de2008-01-08 15:46:27 -0500166 buf->start, val, found, from_this_trans);
Chris Mason19c00dd2007-10-15 16:19:22 -0400167 return 1;
168 }
169 } else {
170 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
171 }
172 return 0;
173}
174
175
176int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
177{
178 struct extent_map_tree *tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400179 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason19c00dd2007-10-15 16:19:22 -0400180 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 }
201 found_level = btrfs_header_level(eb);
202 csum_tree_block(root, eb, 0);
203 free_extent_buffer(eb);
204out:
205 return 0;
206}
207
Chris Mason0da54682007-11-07 21:08:01 -0500208static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
209{
210 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
211
212 csum_dirty_buffer(root, page);
213 return 0;
214}
215
Chris Mason5f39d392007-10-15 16:14:19 -0400216static int btree_writepage(struct page *page, struct writeback_control *wbc)
217{
218 struct extent_map_tree *tree;
219 tree = &BTRFS_I(page->mapping->host)->extent_tree;
220 return extent_write_full_page(tree, page, btree_get_extent, wbc);
221}
Chris Mason0da54682007-11-07 21:08:01 -0500222
223static int btree_writepages(struct address_space *mapping,
224 struct writeback_control *wbc)
225{
226 struct extent_map_tree *tree;
227 tree = &BTRFS_I(mapping->host)->extent_tree;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500228 if (wbc->sync_mode == WB_SYNC_NONE) {
Chris Mason793955b2007-11-26 16:34:41 -0800229 u64 num_dirty;
230 u64 start = 0;
231 unsigned long thresh = 96 * 1024 * 1024;
Chris Mason448d6402007-11-27 07:52:01 -0800232
233 if (wbc->for_kupdate)
234 return 0;
235
Chris Masonca664622007-11-27 11:16:35 -0500236 if (current_is_pdflush()) {
237 thresh = 96 * 1024 * 1024;
238 } else {
239 thresh = 8 * 1024 * 1024;
240 }
Chris Mason1832a6d2007-12-21 16:27:21 -0500241 num_dirty = count_range_bits(tree, &start, (u64)-1,
242 thresh, EXTENT_DIRTY);
Chris Mason793955b2007-11-26 16:34:41 -0800243 if (num_dirty < thresh) {
244 return 0;
245 }
246 }
Chris Mason0da54682007-11-07 21:08:01 -0500247 return extent_writepages(tree, mapping, btree_get_extent, wbc);
248}
249
Chris Mason5f39d392007-10-15 16:14:19 -0400250int btree_readpage(struct file *file, struct page *page)
251{
252 struct extent_map_tree *tree;
253 tree = &BTRFS_I(page->mapping->host)->extent_tree;
254 return extent_read_full_page(tree, page, btree_get_extent);
255}
256
257static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
258{
259 struct extent_map_tree *tree;
260 int ret;
261
Chris Mason5f39d392007-10-15 16:14:19 -0400262 tree = &BTRFS_I(page->mapping->host)->extent_tree;
263 ret = try_release_extent_mapping(tree, page);
264 if (ret == 1) {
265 ClearPagePrivate(page);
266 set_page_private(page, 0);
267 page_cache_release(page);
268 }
Chris Masond98237b2007-03-28 13:57:48 -0400269 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400270}
Chris Mason123abc82007-03-14 14:14:43 -0400271
Chris Mason5f39d392007-10-15 16:14:19 -0400272static void btree_invalidatepage(struct page *page, unsigned long offset)
Chris Masond98237b2007-03-28 13:57:48 -0400273{
Chris Mason5f39d392007-10-15 16:14:19 -0400274 struct extent_map_tree *tree;
275 tree = &BTRFS_I(page->mapping->host)->extent_tree;
276 extent_invalidatepage(tree, page, offset);
277 btree_releasepage(page, GFP_NOFS);
Chris Masond98237b2007-03-28 13:57:48 -0400278}
279
Chris Mason5f39d392007-10-15 16:14:19 -0400280#if 0
Chris Masond98237b2007-03-28 13:57:48 -0400281static int btree_writepage(struct page *page, struct writeback_control *wbc)
282{
Chris Mason87cbda52007-03-28 19:44:27 -0400283 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400284 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400285 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400286 if (!page_has_buffers(page)) {
287 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
288 (1 << BH_Dirty)|(1 << BH_Uptodate));
289 }
290 head = page_buffers(page);
291 bh = head;
292 do {
293 if (buffer_dirty(bh))
294 csum_tree_block(root, bh, 0);
295 bh = bh->b_this_page;
296 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400297 return block_write_full_page(page, btree_get_block, wbc);
298}
Chris Mason5f39d392007-10-15 16:14:19 -0400299#endif
Chris Masond98237b2007-03-28 13:57:48 -0400300
301static struct address_space_operations btree_aops = {
302 .readpage = btree_readpage,
303 .writepage = btree_writepage,
Chris Mason0da54682007-11-07 21:08:01 -0500304 .writepages = btree_writepages,
Chris Mason5f39d392007-10-15 16:14:19 -0400305 .releasepage = btree_releasepage,
306 .invalidatepage = btree_invalidatepage,
Chris Masond98237b2007-03-28 13:57:48 -0400307 .sync_page = block_sync_page,
308};
309
Chris Masondb945352007-10-15 16:15:53 -0400310int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
Chris Mason090d1872007-05-01 08:53:32 -0400311{
Chris Mason5f39d392007-10-15 16:14:19 -0400312 struct extent_buffer *buf = NULL;
313 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonde428b62007-05-18 13:28:27 -0400314 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400315
Chris Masondb945352007-10-15 16:15:53 -0400316 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400317 if (!buf)
Chris Mason090d1872007-05-01 08:53:32 -0400318 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400319 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -0400320 buf, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400321 free_extent_buffer(buf);
Chris Masonde428b62007-05-18 13:28:27 -0400322 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400323}
324
Chris Masondb945352007-10-15 16:15:53 -0400325struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
326 u32 blocksize)
Chris Masone20d96d2007-03-22 12:13:20 -0400327{
Chris Mason5f39d392007-10-15 16:14:19 -0400328 struct extent_buffer *buf = NULL;
329 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason19c00dd2007-10-15 16:19:22 -0400330 struct extent_map_tree *extent_tree;
Chris Masone4204de2008-01-08 15:46:27 -0500331 u64 end;
Chris Mason19c00dd2007-10-15 16:19:22 -0400332 int ret;
333
334 extent_tree = &BTRFS_I(btree_inode)->extent_tree;
Chris Masone20d96d2007-03-22 12:13:20 -0400335
Chris Masondb945352007-10-15 16:15:53 -0400336 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400337 if (!buf)
338 return NULL;
339 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -0400340 buf, 0, 1);
Chris Masone4204de2008-01-08 15:46:27 -0500341
342 if (buf->flags & EXTENT_CSUM)
Chris Mason19c00dd2007-10-15 16:19:22 -0400343 return buf;
Chris Masone4204de2008-01-08 15:46:27 -0500344
345 end = buf->start + PAGE_CACHE_SIZE - 1;
346 if (test_range_bit(extent_tree, buf->start, end, EXTENT_CSUM, 1)) {
Chris Mason19c00dd2007-10-15 16:19:22 -0400347 buf->flags |= EXTENT_CSUM;
348 return buf;
349 }
Chris Masone4204de2008-01-08 15:46:27 -0500350
351 lock_extent(extent_tree, buf->start, end, GFP_NOFS);
352
353 if (test_range_bit(extent_tree, buf->start, end, EXTENT_CSUM, 1)) {
354 buf->flags |= EXTENT_CSUM;
355 goto out_unlock;
356 }
357
Chris Mason19c00dd2007-10-15 16:19:22 -0400358 ret = csum_tree_block(root, buf, 1);
Chris Masone4204de2008-01-08 15:46:27 -0500359 set_extent_bits(extent_tree, buf->start, end, EXTENT_CSUM, GFP_NOFS);
Chris Mason19c00dd2007-10-15 16:19:22 -0400360 buf->flags |= EXTENT_CSUM;
Chris Masone4204de2008-01-08 15:46:27 -0500361
362out_unlock:
363 unlock_extent(extent_tree, buf->start, end, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -0400364 return buf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500365}
366
Chris Masone089f052007-03-16 16:20:31 -0400367int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400368 struct extent_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500369{
Chris Mason5f39d392007-10-15 16:14:19 -0400370 struct inode *btree_inode = root->fs_info->btree_inode;
371 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
372 return 0;
373}
374
375int wait_on_tree_block_writeback(struct btrfs_root *root,
376 struct extent_buffer *buf)
377{
378 struct inode *btree_inode = root->fs_info->btree_inode;
379 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
380 buf);
381 return 0;
382}
383
Chris Masondb945352007-10-15 16:15:53 -0400384static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500385 u32 stripesize, struct btrfs_root *root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400386 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400387 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500388{
Chris Masoncfaa7292007-02-21 17:04:57 -0500389 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400390 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500391 root->commit_root = NULL;
Chris Masondb945352007-10-15 16:15:53 -0400392 root->sectorsize = sectorsize;
393 root->nodesize = nodesize;
394 root->leafsize = leafsize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500395 root->stripesize = stripesize;
Chris Mason123abc82007-03-14 14:14:43 -0400396 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400397 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400398 root->objectid = objectid;
399 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400400 root->highest_inode = 0;
401 root->last_inode_alloc = 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400402 root->name = NULL;
Chris Mason4313b392008-01-03 09:08:48 -0500403 root->in_sysfs = 0;
Chris Mason3768f362007-03-13 16:47:54 -0400404 memset(&root->root_key, 0, sizeof(root->root_key));
405 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -0400406 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Josef Bacik58176a92007-08-29 15:47:34 -0400407 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
408 init_completion(&root->kobj_unregister);
Chris Mason6702ed42007-08-07 16:15:09 -0400409 root->defrag_running = 0;
410 root->defrag_level = 0;
Chris Mason4d775672007-04-20 20:23:12 -0400411 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400412 return 0;
413}
414
Chris Masondb945352007-10-15 16:15:53 -0400415static int find_and_setup_root(struct btrfs_root *tree_root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400416 struct btrfs_fs_info *fs_info,
417 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400418 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400419{
420 int ret;
Chris Masondb945352007-10-15 16:15:53 -0400421 u32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -0400422
Chris Masondb945352007-10-15 16:15:53 -0400423 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500424 tree_root->sectorsize, tree_root->stripesize,
425 root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400426 ret = btrfs_find_last_root(tree_root, objectid,
427 &root->root_item, &root->root_key);
428 BUG_ON(ret);
429
Chris Masondb945352007-10-15 16:15:53 -0400430 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
431 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
432 blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400433 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500434 return 0;
435}
436
Chris Mason5eda7b52007-06-22 14:16:25 -0400437struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
438 struct btrfs_key *location)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400439{
440 struct btrfs_root *root;
441 struct btrfs_root *tree_root = fs_info->tree_root;
442 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400443 struct extent_buffer *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400444 u64 highest_inode;
Chris Masondb945352007-10-15 16:15:53 -0400445 u32 blocksize;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400446 int ret = 0;
447
Chris Mason5eda7b52007-06-22 14:16:25 -0400448 root = kzalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400449 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400450 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400451 if (location->offset == (u64)-1) {
Chris Masondb945352007-10-15 16:15:53 -0400452 ret = find_and_setup_root(tree_root, fs_info,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400453 location->objectid, root);
454 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400455 kfree(root);
456 return ERR_PTR(ret);
457 }
458 goto insert;
459 }
460
Chris Masondb945352007-10-15 16:15:53 -0400461 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500462 tree_root->sectorsize, tree_root->stripesize,
463 root, fs_info, location->objectid);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400464
465 path = btrfs_alloc_path();
466 BUG_ON(!path);
467 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
468 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400469 if (ret > 0)
470 ret = -ENOENT;
471 goto out;
472 }
Chris Mason5f39d392007-10-15 16:14:19 -0400473 l = path->nodes[0];
474 read_extent_buffer(l, &root->root_item,
475 btrfs_item_ptr_offset(l, path->slots[0]),
Chris Mason0f7d52f2007-04-09 10:42:37 -0400476 sizeof(root->root_item));
Yan Zheng44b36eb2007-10-19 09:22:56 -0400477 memcpy(&root->root_key, location, sizeof(*location));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400478 ret = 0;
479out:
480 btrfs_release_path(root, path);
481 btrfs_free_path(path);
482 if (ret) {
483 kfree(root);
484 return ERR_PTR(ret);
485 }
Chris Masondb945352007-10-15 16:15:53 -0400486 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
487 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
488 blocksize);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400489 BUG_ON(!root->node);
490insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400491 root->ref_cows = 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400492 ret = btrfs_find_highest_inode(root, &highest_inode);
493 if (ret == 0) {
494 root->highest_inode = highest_inode;
495 root->last_inode_alloc = highest_inode;
496 }
497 return root;
498}
499
Chris Masondc17ff82008-01-08 15:46:30 -0500500struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
501 u64 root_objectid)
502{
503 struct btrfs_root *root;
504
505 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
506 return fs_info->tree_root;
507 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
508 return fs_info->extent_root;
509
510 root = radix_tree_lookup(&fs_info->fs_roots_radix,
511 (unsigned long)root_objectid);
512 return root;
513}
514
Chris Masonedbd8d42007-12-21 16:27:24 -0500515struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
516 struct btrfs_key *location)
Chris Mason5eda7b52007-06-22 14:16:25 -0400517{
518 struct btrfs_root *root;
519 int ret;
520
Chris Masonedbd8d42007-12-21 16:27:24 -0500521 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
522 return fs_info->tree_root;
523 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
524 return fs_info->extent_root;
525
Chris Mason5eda7b52007-06-22 14:16:25 -0400526 root = radix_tree_lookup(&fs_info->fs_roots_radix,
527 (unsigned long)location->objectid);
528 if (root)
529 return root;
530
531 root = btrfs_read_fs_root_no_radix(fs_info, location);
532 if (IS_ERR(root))
533 return root;
Chris Mason2619ba12007-04-10 16:58:11 -0400534 ret = radix_tree_insert(&fs_info->fs_roots_radix,
535 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400536 root);
537 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400538 free_extent_buffer(root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400539 kfree(root);
540 return ERR_PTR(ret);
541 }
Chris Masonedbd8d42007-12-21 16:27:24 -0500542 ret = btrfs_find_dead_roots(fs_info->tree_root,
543 root->root_key.objectid, root);
544 BUG_ON(ret);
545
546 return root;
547}
548
549struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
550 struct btrfs_key *location,
551 const char *name, int namelen)
552{
553 struct btrfs_root *root;
554 int ret;
555
556 root = btrfs_read_fs_root_no_name(fs_info, location);
557 if (!root)
558 return NULL;
Josef Bacik58176a92007-08-29 15:47:34 -0400559
Chris Mason4313b392008-01-03 09:08:48 -0500560 if (root->in_sysfs)
561 return root;
562
Josef Bacik58176a92007-08-29 15:47:34 -0400563 ret = btrfs_set_root_name(root, name, namelen);
564 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400565 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400566 kfree(root);
567 return ERR_PTR(ret);
568 }
569
570 ret = btrfs_sysfs_add_root(root);
571 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400572 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400573 kfree(root->name);
574 kfree(root);
575 return ERR_PTR(ret);
576 }
Chris Mason4313b392008-01-03 09:08:48 -0500577 root->in_sysfs = 1;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400578 return root;
579}
Chris Mason19c00dd2007-10-15 16:19:22 -0400580#if 0
581static int add_hasher(struct btrfs_fs_info *info, char *type) {
582 struct btrfs_hasher *hasher;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400583
Chris Mason19c00dd2007-10-15 16:19:22 -0400584 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
585 if (!hasher)
586 return -ENOMEM;
587 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
588 if (!hasher->hash_tfm) {
589 kfree(hasher);
590 return -EINVAL;
591 }
592 spin_lock(&info->hash_lock);
593 list_add(&hasher->list, &info->hashers);
594 spin_unlock(&info->hash_lock);
595 return 0;
596}
597#endif
Chris Mason2c90e5d2007-04-02 10:50:19 -0400598struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500599{
Chris Masondb945352007-10-15 16:15:53 -0400600 u32 sectorsize;
601 u32 nodesize;
602 u32 leafsize;
603 u32 blocksize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500604 u32 stripesize;
Chris Masone20d96d2007-03-22 12:13:20 -0400605 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
606 GFP_NOFS);
607 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
608 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400609 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
610 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500611 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400612 int err = -EIO;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400613 struct btrfs_super_block *disk_super;
Chris Masoneb60cea2007-02-02 09:18:22 -0500614
Chris Mason39279cc2007-06-12 06:35:45 -0400615 if (!extent_root || !tree_root || !fs_info) {
616 err = -ENOMEM;
617 goto fail;
618 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400619 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason8fd17792007-04-19 21:01:03 -0400620 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -0400621 INIT_LIST_HEAD(&fs_info->dead_roots);
Chris Mason19c00dd2007-10-15 16:19:22 -0400622 INIT_LIST_HEAD(&fs_info->hashers);
623 spin_lock_init(&fs_info->hash_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -0500624 spin_lock_init(&fs_info->delalloc_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -0400625
Josef Bacik58176a92007-08-29 15:47:34 -0400626 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
627 init_completion(&fs_info->kobj_unregister);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400628 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400629 fs_info->running_transaction = NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400630 fs_info->last_trans_committed = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400631 fs_info->tree_root = tree_root;
632 fs_info->extent_root = extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400633 fs_info->sb = sb;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500634 fs_info->mount_opt = 0;
Chris Masonc59f8952007-12-17 20:14:04 -0500635 fs_info->max_extent = (u64)-1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500636 fs_info->delalloc_bytes = 0;
Chris Masond98237b2007-03-28 13:57:48 -0400637 fs_info->btree_inode = new_inode(sb);
638 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400639 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400640 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
641 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason5f39d392007-10-15 16:14:19 -0400642 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
643 fs_info->btree_inode->i_mapping,
644 GFP_NOFS);
Chris Mason0da54682007-11-07 21:08:01 -0500645 BTRFS_I(fs_info->btree_inode)->extent_tree.ops = &btree_extent_map_ops;
646
Chris Masonf510cfe2007-10-15 16:14:48 -0400647 extent_map_tree_init(&fs_info->free_space_cache,
648 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason96b51792007-10-15 16:15:19 -0400649 extent_map_tree_init(&fs_info->block_group_cache,
650 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason1a5bc162007-10-15 16:15:26 -0400651 extent_map_tree_init(&fs_info->pinned_extents,
652 fs_info->btree_inode->i_mapping, GFP_NOFS);
653 extent_map_tree_init(&fs_info->pending_del,
654 fs_info->btree_inode->i_mapping, GFP_NOFS);
655 extent_map_tree_init(&fs_info->extent_ins,
656 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masone66f7092007-04-20 13:16:02 -0400657 fs_info->do_barriers = 1;
Chris Masonfacda1e2007-06-08 18:11:48 -0400658 fs_info->closing = 0;
Yan324ae4d2007-11-16 14:57:08 -0500659 fs_info->total_pinned = 0;
Chris Mason6da6aba2007-12-18 16:15:09 -0500660#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
661 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
662#else
Chris Mason08607c12007-06-08 15:33:54 -0400663 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason6da6aba2007-12-18 16:15:09 -0500664#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -0400665 BTRFS_I(fs_info->btree_inode)->root = tree_root;
666 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
667 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400668 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400669 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400670
Chris Mason79154b12007-03-22 15:59:16 -0400671 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400672 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400673
Chris Mason19c00dd2007-10-15 16:19:22 -0400674#if 0
675 ret = add_hasher(fs_info, "crc32c");
676 if (ret) {
677 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
678 err = -ENOMEM;
679 goto fail_iput;
680 }
681#endif
Chris Mason87ee04e2007-11-30 11:30:34 -0500682 __setup_root(512, 512, 512, 512, tree_root,
Chris Mason2c90e5d2007-04-02 10:50:19 -0400683 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400684
Chris Mason2c90e5d2007-04-02 10:50:19 -0400685 fs_info->sb_buffer = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400686 BTRFS_SUPER_INFO_OFFSET,
687 512);
Chris Masond98237b2007-03-28 13:57:48 -0400688
Chris Mason0f7d52f2007-04-09 10:42:37 -0400689 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -0400690 goto fail_iput;
Chris Mason39279cc2007-06-12 06:35:45 -0400691
Chris Mason5f39d392007-10-15 16:14:19 -0400692 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
693 sizeof(fs_info->super_copy));
694
695 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
696 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
697 BTRFS_FSID_SIZE);
698 disk_super = &fs_info->super_copy;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400699 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -0400700 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400701
Chris Masondb945352007-10-15 16:15:53 -0400702 nodesize = btrfs_super_nodesize(disk_super);
703 leafsize = btrfs_super_leafsize(disk_super);
704 sectorsize = btrfs_super_sectorsize(disk_super);
Chris Mason87ee04e2007-11-30 11:30:34 -0500705 stripesize = btrfs_super_stripesize(disk_super);
Chris Masondb945352007-10-15 16:15:53 -0400706 tree_root->nodesize = nodesize;
707 tree_root->leafsize = leafsize;
708 tree_root->sectorsize = sectorsize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500709 tree_root->stripesize = stripesize;
Chris Masonff79f812007-10-15 16:22:25 -0400710 sb_set_blocksize(sb, sectorsize);
Chris Masondb945352007-10-15 16:15:53 -0400711
Chris Mason8352d8a2007-04-12 10:43:05 -0400712 i_size_write(fs_info->btree_inode,
Chris Masondb945352007-10-15 16:15:53 -0400713 btrfs_super_total_bytes(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -0400714
Chris Mason39279cc2007-06-12 06:35:45 -0400715 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
716 sizeof(disk_super->magic))) {
717 printk("btrfs: valid FS not found on %s\n", sb->s_id);
718 goto fail_sb_buffer;
719 }
Chris Mason19c00dd2007-10-15 16:19:22 -0400720
Chris Masondb945352007-10-15 16:15:53 -0400721 blocksize = btrfs_level_size(tree_root,
722 btrfs_super_root_level(disk_super));
Chris Mason19c00dd2007-10-15 16:19:22 -0400723
Chris Masone20d96d2007-03-22 12:13:20 -0400724 tree_root->node = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400725 btrfs_super_root(disk_super),
726 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -0400727 if (!tree_root->node)
728 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -0400729
Chris Mason2c90e5d2007-04-02 10:50:19 -0400730 mutex_lock(&fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400731
732 ret = find_and_setup_root(tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400733 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason39279cc2007-06-12 06:35:45 -0400734 if (ret) {
735 mutex_unlock(&fs_info->fs_mutex);
736 goto fail_tree_root;
737 }
Chris Mason3768f362007-03-13 16:47:54 -0400738
Chris Mason9078a3e2007-04-26 16:46:15 -0400739 btrfs_read_block_groups(extent_root);
740
Chris Mason0f7d52f2007-04-09 10:42:37 -0400741 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400742 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400743 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -0400744
745fail_tree_root:
Chris Mason5f39d392007-10-15 16:14:19 -0400746 free_extent_buffer(tree_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -0400747fail_sb_buffer:
Chris Mason5f39d392007-10-15 16:14:19 -0400748 free_extent_buffer(fs_info->sb_buffer);
Chris Mason39279cc2007-06-12 06:35:45 -0400749fail_iput:
750 iput(fs_info->btree_inode);
751fail:
752 kfree(extent_root);
753 kfree(tree_root);
754 kfree(fs_info);
755 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -0500756}
757
Chris Masone089f052007-03-16 16:20:31 -0400758int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400759 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500760{
Chris Masone66f7092007-04-20 13:16:02 -0400761 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -0400762 struct extent_buffer *super = root->fs_info->sb_buffer;
763 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400764
Chris Mason5f39d392007-10-15 16:14:19 -0400765 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
766 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
767 super->start, super->len);
768 return ret;
Chris Masoncfaa7292007-02-21 17:04:57 -0500769}
770
Chris Mason5eda7b52007-06-22 14:16:25 -0400771int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -0400772{
773 radix_tree_delete(&fs_info->fs_roots_radix,
774 (unsigned long)root->root_key.objectid);
Josef Bacik58176a92007-08-29 15:47:34 -0400775 btrfs_sysfs_del_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -0400776 if (root->inode)
777 iput(root->inode);
778 if (root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400779 free_extent_buffer(root->node);
Chris Mason2619ba12007-04-10 16:58:11 -0400780 if (root->commit_root)
Chris Mason5f39d392007-10-15 16:14:19 -0400781 free_extent_buffer(root->commit_root);
Josef Bacik58176a92007-08-29 15:47:34 -0400782 if (root->name)
783 kfree(root->name);
Chris Mason2619ba12007-04-10 16:58:11 -0400784 kfree(root);
785 return 0;
786}
787
Chris Mason35b7e472007-05-02 15:53:43 -0400788static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400789{
790 int ret;
791 struct btrfs_root *gang[8];
792 int i;
793
794 while(1) {
795 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
796 (void **)gang, 0,
797 ARRAY_SIZE(gang));
798 if (!ret)
799 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400800 for (i = 0; i < ret; i++)
Chris Mason5eda7b52007-06-22 14:16:25 -0400801 btrfs_free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400802 }
803 return 0;
804}
Chris Masonb4100d62007-04-12 12:14:00 -0400805
Chris Masone20d96d2007-03-22 12:13:20 -0400806int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500807{
Chris Mason3768f362007-03-13 16:47:54 -0400808 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400809 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400810 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400811
Chris Masonfacda1e2007-06-08 18:11:48 -0400812 fs_info->closing = 1;
Chris Mason08607c12007-06-08 15:33:54 -0400813 btrfs_transaction_flush_work(root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400814 mutex_lock(&fs_info->fs_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -0400815 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason79154b12007-03-22 15:59:16 -0400816 trans = btrfs_start_transaction(root, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400817 ret = btrfs_commit_transaction(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400818 /* run commit again to drop the original snapshot */
819 trans = btrfs_start_transaction(root, 1);
820 btrfs_commit_transaction(trans, root);
821 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400822 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400823 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400824 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500825
Chris Mason0f7d52f2007-04-09 10:42:37 -0400826 if (fs_info->extent_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400827 free_extent_buffer(fs_info->extent_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -0400828
Chris Mason0f7d52f2007-04-09 10:42:37 -0400829 if (fs_info->tree_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400830 free_extent_buffer(fs_info->tree_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -0400831
Chris Mason5f39d392007-10-15 16:14:19 -0400832 free_extent_buffer(fs_info->sb_buffer);
Chris Mason7eccb902007-04-11 15:53:25 -0400833
Chris Mason9078a3e2007-04-26 16:46:15 -0400834 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400835 del_fs_roots(fs_info);
Chris Masond10c5f32007-12-17 20:14:04 -0500836
837 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
838
839 extent_map_tree_empty_lru(&fs_info->free_space_cache);
840 extent_map_tree_empty_lru(&fs_info->block_group_cache);
841 extent_map_tree_empty_lru(&fs_info->pinned_extents);
842 extent_map_tree_empty_lru(&fs_info->pending_del);
843 extent_map_tree_empty_lru(&fs_info->extent_ins);
Chris Mason19c00dd2007-10-15 16:19:22 -0400844 extent_map_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->extent_tree);
Chris Masond10c5f32007-12-17 20:14:04 -0500845
Chris Masondb945352007-10-15 16:15:53 -0400846 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
Chris Masond10c5f32007-12-17 20:14:04 -0500847
Chris Masondb945352007-10-15 16:15:53 -0400848 iput(fs_info->btree_inode);
Chris Mason19c00dd2007-10-15 16:19:22 -0400849#if 0
850 while(!list_empty(&fs_info->hashers)) {
851 struct btrfs_hasher *hasher;
852 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
853 hashers);
854 list_del(&hasher->hashers);
855 crypto_free_hash(&fs_info->hash_tfm);
856 kfree(hasher);
857 }
858#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -0400859 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400860 kfree(fs_info->tree_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500861 return 0;
862}
863
Chris Mason5f39d392007-10-15 16:14:19 -0400864int btrfs_buffer_uptodate(struct extent_buffer *buf)
Chris Masonccd467d2007-06-28 15:57:36 -0400865{
Chris Mason810191f2007-10-15 16:18:55 -0400866 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Mason5f39d392007-10-15 16:14:19 -0400867 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
868}
Chris Mason6702ed42007-08-07 16:15:09 -0400869
Chris Mason5f39d392007-10-15 16:14:19 -0400870int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
871{
Chris Mason810191f2007-10-15 16:18:55 -0400872 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Mason5f39d392007-10-15 16:14:19 -0400873 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
874 buf);
875}
876
877void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
878{
Chris Mason810191f2007-10-15 16:18:55 -0400879 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason5f39d392007-10-15 16:14:19 -0400880 u64 transid = btrfs_header_generation(buf);
881 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason6702ed42007-08-07 16:15:09 -0400882
Chris Masonccd467d2007-06-28 15:57:36 -0400883 if (transid != root->fs_info->generation) {
884 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
Chris Masondb945352007-10-15 16:15:53 -0400885 (unsigned long long)buf->start,
Chris Masonccd467d2007-06-28 15:57:36 -0400886 transid, root->fs_info->generation);
887 WARN_ON(1);
888 }
Chris Mason5f39d392007-10-15 16:14:19 -0400889 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500890}
891
Chris Masond3c2fdc2007-09-17 10:58:06 -0400892void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
Chris Mason35b7e472007-05-02 15:53:43 -0400893{
Chris Masond3c2fdc2007-09-17 10:58:06 -0400894 balance_dirty_pages_ratelimited_nr(
Chris Mason304fced2007-10-15 16:21:17 -0400895 root->fs_info->btree_inode->i_mapping, 1);
Chris Mason35b7e472007-05-02 15:53:43 -0400896}
Chris Mason6b800532007-10-15 16:17:34 -0400897
898void btrfs_set_buffer_defrag(struct extent_buffer *buf)
899{
Chris Mason810191f2007-10-15 16:18:55 -0400900 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400901 struct inode *btree_inode = root->fs_info->btree_inode;
902 set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
903 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
904}
905
906void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
907{
Chris Mason810191f2007-10-15 16:18:55 -0400908 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400909 struct inode *btree_inode = root->fs_info->btree_inode;
910 set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
911 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
912 GFP_NOFS);
913}
914
915int btrfs_buffer_defrag(struct extent_buffer *buf)
916{
Chris Mason810191f2007-10-15 16:18:55 -0400917 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400918 struct inode *btree_inode = root->fs_info->btree_inode;
919 return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
920 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
921}
922
923int btrfs_buffer_defrag_done(struct extent_buffer *buf)
924{
Chris Mason810191f2007-10-15 16:18:55 -0400925 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400926 struct inode *btree_inode = root->fs_info->btree_inode;
927 return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
928 buf->start, buf->start + buf->len - 1,
929 EXTENT_DEFRAG_DONE, 0);
930}
931
932int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
933{
Chris Mason810191f2007-10-15 16:18:55 -0400934 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400935 struct inode *btree_inode = root->fs_info->btree_inode;
936 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
937 buf->start, buf->start + buf->len - 1,
938 EXTENT_DEFRAG_DONE, GFP_NOFS);
939}
940
941int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
942{
Chris Mason810191f2007-10-15 16:18:55 -0400943 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400944 struct inode *btree_inode = root->fs_info->btree_inode;
945 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
946 buf->start, buf->start + buf->len - 1,
947 EXTENT_DEFRAG, GFP_NOFS);
948}
949
950int btrfs_read_buffer(struct extent_buffer *buf)
951{
Chris Mason810191f2007-10-15 16:18:55 -0400952 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400953 struct inode *btree_inode = root->fs_info->btree_inode;
954 return read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -0400955 buf, 0, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400956}
Chris Mason0da54682007-11-07 21:08:01 -0500957
958static struct extent_map_ops btree_extent_map_ops = {
959 .writepage_io_hook = btree_writepage_io_hook,
960};