blob: 5d1f9bca271232d95e36b2e81b4e57f8cdc6d934 [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);
Chris Masone18e4802008-01-18 10:54:22 -0500196 btrfs_clear_buffer_defrag(eb);
Chris Mason19c00dd2007-10-15 16:19:22 -0400197 found_start = btrfs_header_bytenr(eb);
198 if (found_start != start) {
199 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
200 start, found_start, len);
Chris Mason55c69072008-01-09 15:55:33 -0500201 WARN_ON(1);
202 goto err;
203 }
204 if (eb->first_page != page) {
205 printk("bad first page %lu %lu\n", eb->first_page->index,
206 page->index);
207 WARN_ON(1);
208 goto err;
209 }
210 if (!PageUptodate(page)) {
211 printk("csum not up to date page %lu\n", page->index);
212 WARN_ON(1);
213 goto err;
Chris Mason19c00dd2007-10-15 16:19:22 -0400214 }
215 found_level = btrfs_header_level(eb);
216 csum_tree_block(root, eb, 0);
Chris Mason55c69072008-01-09 15:55:33 -0500217err:
Chris Mason19c00dd2007-10-15 16:19:22 -0400218 free_extent_buffer(eb);
219out:
220 return 0;
221}
222
Chris Mason0da54682007-11-07 21:08:01 -0500223static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
224{
225 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
226
227 csum_dirty_buffer(root, page);
228 return 0;
229}
230
Chris Mason5f39d392007-10-15 16:14:19 -0400231static int btree_writepage(struct page *page, struct writeback_control *wbc)
232{
233 struct extent_map_tree *tree;
234 tree = &BTRFS_I(page->mapping->host)->extent_tree;
235 return extent_write_full_page(tree, page, btree_get_extent, wbc);
236}
Chris Mason0da54682007-11-07 21:08:01 -0500237
238static int btree_writepages(struct address_space *mapping,
239 struct writeback_control *wbc)
240{
241 struct extent_map_tree *tree;
242 tree = &BTRFS_I(mapping->host)->extent_tree;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500243 if (wbc->sync_mode == WB_SYNC_NONE) {
Chris Mason793955b2007-11-26 16:34:41 -0800244 u64 num_dirty;
245 u64 start = 0;
246 unsigned long thresh = 96 * 1024 * 1024;
Chris Mason448d6402007-11-27 07:52:01 -0800247
248 if (wbc->for_kupdate)
249 return 0;
250
Chris Masonca664622007-11-27 11:16:35 -0500251 if (current_is_pdflush()) {
252 thresh = 96 * 1024 * 1024;
253 } else {
254 thresh = 8 * 1024 * 1024;
255 }
Chris Mason1832a6d2007-12-21 16:27:21 -0500256 num_dirty = count_range_bits(tree, &start, (u64)-1,
257 thresh, EXTENT_DIRTY);
Chris Mason793955b2007-11-26 16:34:41 -0800258 if (num_dirty < thresh) {
259 return 0;
260 }
261 }
Chris Mason0da54682007-11-07 21:08:01 -0500262 return extent_writepages(tree, mapping, btree_get_extent, wbc);
263}
264
Chris Mason5f39d392007-10-15 16:14:19 -0400265int btree_readpage(struct file *file, struct page *page)
266{
267 struct extent_map_tree *tree;
268 tree = &BTRFS_I(page->mapping->host)->extent_tree;
269 return extent_read_full_page(tree, page, btree_get_extent);
270}
271
272static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
273{
274 struct extent_map_tree *tree;
275 int ret;
276
Chris Mason5f39d392007-10-15 16:14:19 -0400277 tree = &BTRFS_I(page->mapping->host)->extent_tree;
278 ret = try_release_extent_mapping(tree, page);
279 if (ret == 1) {
280 ClearPagePrivate(page);
281 set_page_private(page, 0);
282 page_cache_release(page);
283 }
Chris Masond98237b2007-03-28 13:57:48 -0400284 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400285}
Chris Mason123abc82007-03-14 14:14:43 -0400286
Chris Mason5f39d392007-10-15 16:14:19 -0400287static void btree_invalidatepage(struct page *page, unsigned long offset)
Chris Masond98237b2007-03-28 13:57:48 -0400288{
Chris Mason5f39d392007-10-15 16:14:19 -0400289 struct extent_map_tree *tree;
290 tree = &BTRFS_I(page->mapping->host)->extent_tree;
291 extent_invalidatepage(tree, page, offset);
292 btree_releasepage(page, GFP_NOFS);
Chris Masond98237b2007-03-28 13:57:48 -0400293}
294
Chris Mason5f39d392007-10-15 16:14:19 -0400295#if 0
Chris Masond98237b2007-03-28 13:57:48 -0400296static int btree_writepage(struct page *page, struct writeback_control *wbc)
297{
Chris Mason87cbda52007-03-28 19:44:27 -0400298 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400299 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400300 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400301 if (!page_has_buffers(page)) {
302 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
303 (1 << BH_Dirty)|(1 << BH_Uptodate));
304 }
305 head = page_buffers(page);
306 bh = head;
307 do {
308 if (buffer_dirty(bh))
309 csum_tree_block(root, bh, 0);
310 bh = bh->b_this_page;
311 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400312 return block_write_full_page(page, btree_get_block, wbc);
313}
Chris Mason5f39d392007-10-15 16:14:19 -0400314#endif
Chris Masond98237b2007-03-28 13:57:48 -0400315
316static struct address_space_operations btree_aops = {
317 .readpage = btree_readpage,
318 .writepage = btree_writepage,
Chris Mason0da54682007-11-07 21:08:01 -0500319 .writepages = btree_writepages,
Chris Mason5f39d392007-10-15 16:14:19 -0400320 .releasepage = btree_releasepage,
321 .invalidatepage = btree_invalidatepage,
Chris Masond98237b2007-03-28 13:57:48 -0400322 .sync_page = block_sync_page,
323};
324
Chris Masondb945352007-10-15 16:15:53 -0400325int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
Chris Mason090d1872007-05-01 08:53:32 -0400326{
Chris Mason5f39d392007-10-15 16:14:19 -0400327 struct extent_buffer *buf = NULL;
328 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonde428b62007-05-18 13:28:27 -0400329 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400330
Chris Masondb945352007-10-15 16:15:53 -0400331 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400332 if (!buf)
Chris Mason090d1872007-05-01 08:53:32 -0400333 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400334 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -0400335 buf, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400336 free_extent_buffer(buf);
Chris Masonde428b62007-05-18 13:28:27 -0400337 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400338}
339
Chris Masondb945352007-10-15 16:15:53 -0400340struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
341 u32 blocksize)
Chris Masone20d96d2007-03-22 12:13:20 -0400342{
Chris Mason5f39d392007-10-15 16:14:19 -0400343 struct extent_buffer *buf = NULL;
344 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason19c00dd2007-10-15 16:19:22 -0400345 struct extent_map_tree *extent_tree;
Chris Masone4204de2008-01-08 15:46:27 -0500346 u64 end;
Chris Mason19c00dd2007-10-15 16:19:22 -0400347 int ret;
348
349 extent_tree = &BTRFS_I(btree_inode)->extent_tree;
Chris Masone20d96d2007-03-22 12:13:20 -0400350
Chris Masondb945352007-10-15 16:15:53 -0400351 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400352 if (!buf)
353 return NULL;
354 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -0400355 buf, 0, 1);
Chris Masone4204de2008-01-08 15:46:27 -0500356
357 if (buf->flags & EXTENT_CSUM)
Chris Mason19c00dd2007-10-15 16:19:22 -0400358 return buf;
Chris Masone4204de2008-01-08 15:46:27 -0500359
360 end = buf->start + PAGE_CACHE_SIZE - 1;
361 if (test_range_bit(extent_tree, buf->start, end, EXTENT_CSUM, 1)) {
Chris Mason19c00dd2007-10-15 16:19:22 -0400362 buf->flags |= EXTENT_CSUM;
363 return buf;
364 }
Chris Masone4204de2008-01-08 15:46:27 -0500365
366 lock_extent(extent_tree, buf->start, end, GFP_NOFS);
367
368 if (test_range_bit(extent_tree, buf->start, end, EXTENT_CSUM, 1)) {
369 buf->flags |= EXTENT_CSUM;
370 goto out_unlock;
371 }
372
Chris Mason19c00dd2007-10-15 16:19:22 -0400373 ret = csum_tree_block(root, buf, 1);
Chris Masone4204de2008-01-08 15:46:27 -0500374 set_extent_bits(extent_tree, buf->start, end, EXTENT_CSUM, GFP_NOFS);
Chris Mason19c00dd2007-10-15 16:19:22 -0400375 buf->flags |= EXTENT_CSUM;
Chris Masone4204de2008-01-08 15:46:27 -0500376
377out_unlock:
378 unlock_extent(extent_tree, buf->start, end, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -0400379 return buf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500380}
381
Chris Masone089f052007-03-16 16:20:31 -0400382int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400383 struct extent_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500384{
Chris Mason5f39d392007-10-15 16:14:19 -0400385 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason55c69072008-01-09 15:55:33 -0500386 if (btrfs_header_generation(buf) ==
387 root->fs_info->running_transaction->transid)
388 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree,
389 buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400390 return 0;
391}
392
393int wait_on_tree_block_writeback(struct btrfs_root *root,
394 struct extent_buffer *buf)
395{
396 struct inode *btree_inode = root->fs_info->btree_inode;
397 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
398 buf);
399 return 0;
400}
401
Chris Masondb945352007-10-15 16:15:53 -0400402static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500403 u32 stripesize, struct btrfs_root *root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400404 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400405 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500406{
Chris Masoncfaa7292007-02-21 17:04:57 -0500407 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400408 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500409 root->commit_root = NULL;
Chris Masondb945352007-10-15 16:15:53 -0400410 root->sectorsize = sectorsize;
411 root->nodesize = nodesize;
412 root->leafsize = leafsize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500413 root->stripesize = stripesize;
Chris Mason123abc82007-03-14 14:14:43 -0400414 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400415 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400416 root->objectid = objectid;
417 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400418 root->highest_inode = 0;
419 root->last_inode_alloc = 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400420 root->name = NULL;
Chris Mason4313b392008-01-03 09:08:48 -0500421 root->in_sysfs = 0;
Chris Mason3768f362007-03-13 16:47:54 -0400422 memset(&root->root_key, 0, sizeof(root->root_key));
423 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -0400424 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Josef Bacik58176a92007-08-29 15:47:34 -0400425 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
426 init_completion(&root->kobj_unregister);
Chris Mason6702ed42007-08-07 16:15:09 -0400427 root->defrag_running = 0;
428 root->defrag_level = 0;
Chris Mason4d775672007-04-20 20:23:12 -0400429 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400430 return 0;
431}
432
Chris Masondb945352007-10-15 16:15:53 -0400433static int find_and_setup_root(struct btrfs_root *tree_root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400434 struct btrfs_fs_info *fs_info,
435 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400436 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400437{
438 int ret;
Chris Masondb945352007-10-15 16:15:53 -0400439 u32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -0400440
Chris Masondb945352007-10-15 16:15:53 -0400441 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500442 tree_root->sectorsize, tree_root->stripesize,
443 root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400444 ret = btrfs_find_last_root(tree_root, objectid,
445 &root->root_item, &root->root_key);
446 BUG_ON(ret);
447
Chris Masondb945352007-10-15 16:15:53 -0400448 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
449 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
450 blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400451 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500452 return 0;
453}
454
Chris Mason5eda7b52007-06-22 14:16:25 -0400455struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
456 struct btrfs_key *location)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400457{
458 struct btrfs_root *root;
459 struct btrfs_root *tree_root = fs_info->tree_root;
460 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400461 struct extent_buffer *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400462 u64 highest_inode;
Chris Masondb945352007-10-15 16:15:53 -0400463 u32 blocksize;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400464 int ret = 0;
465
Chris Mason5eda7b52007-06-22 14:16:25 -0400466 root = kzalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400467 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400468 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400469 if (location->offset == (u64)-1) {
Chris Masondb945352007-10-15 16:15:53 -0400470 ret = find_and_setup_root(tree_root, fs_info,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400471 location->objectid, root);
472 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400473 kfree(root);
474 return ERR_PTR(ret);
475 }
476 goto insert;
477 }
478
Chris Masondb945352007-10-15 16:15:53 -0400479 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500480 tree_root->sectorsize, tree_root->stripesize,
481 root, fs_info, location->objectid);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400482
483 path = btrfs_alloc_path();
484 BUG_ON(!path);
485 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
486 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400487 if (ret > 0)
488 ret = -ENOENT;
489 goto out;
490 }
Chris Mason5f39d392007-10-15 16:14:19 -0400491 l = path->nodes[0];
492 read_extent_buffer(l, &root->root_item,
493 btrfs_item_ptr_offset(l, path->slots[0]),
Chris Mason0f7d52f2007-04-09 10:42:37 -0400494 sizeof(root->root_item));
Yan Zheng44b36eb2007-10-19 09:22:56 -0400495 memcpy(&root->root_key, location, sizeof(*location));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400496 ret = 0;
497out:
498 btrfs_release_path(root, path);
499 btrfs_free_path(path);
500 if (ret) {
501 kfree(root);
502 return ERR_PTR(ret);
503 }
Chris Masondb945352007-10-15 16:15:53 -0400504 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
505 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
506 blocksize);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400507 BUG_ON(!root->node);
508insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400509 root->ref_cows = 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400510 ret = btrfs_find_highest_inode(root, &highest_inode);
511 if (ret == 0) {
512 root->highest_inode = highest_inode;
513 root->last_inode_alloc = highest_inode;
514 }
515 return root;
516}
517
Chris Masondc17ff82008-01-08 15:46:30 -0500518struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
519 u64 root_objectid)
520{
521 struct btrfs_root *root;
522
523 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
524 return fs_info->tree_root;
525 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
526 return fs_info->extent_root;
527
528 root = radix_tree_lookup(&fs_info->fs_roots_radix,
529 (unsigned long)root_objectid);
530 return root;
531}
532
Chris Masonedbd8d42007-12-21 16:27:24 -0500533struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
534 struct btrfs_key *location)
Chris Mason5eda7b52007-06-22 14:16:25 -0400535{
536 struct btrfs_root *root;
537 int ret;
538
Chris Masonedbd8d42007-12-21 16:27:24 -0500539 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
540 return fs_info->tree_root;
541 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
542 return fs_info->extent_root;
543
Chris Mason5eda7b52007-06-22 14:16:25 -0400544 root = radix_tree_lookup(&fs_info->fs_roots_radix,
545 (unsigned long)location->objectid);
546 if (root)
547 return root;
548
549 root = btrfs_read_fs_root_no_radix(fs_info, location);
550 if (IS_ERR(root))
551 return root;
Chris Mason2619ba12007-04-10 16:58:11 -0400552 ret = radix_tree_insert(&fs_info->fs_roots_radix,
553 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400554 root);
555 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400556 free_extent_buffer(root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400557 kfree(root);
558 return ERR_PTR(ret);
559 }
Chris Masonedbd8d42007-12-21 16:27:24 -0500560 ret = btrfs_find_dead_roots(fs_info->tree_root,
561 root->root_key.objectid, root);
562 BUG_ON(ret);
563
564 return root;
565}
566
567struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
568 struct btrfs_key *location,
569 const char *name, int namelen)
570{
571 struct btrfs_root *root;
572 int ret;
573
574 root = btrfs_read_fs_root_no_name(fs_info, location);
575 if (!root)
576 return NULL;
Josef Bacik58176a92007-08-29 15:47:34 -0400577
Chris Mason4313b392008-01-03 09:08:48 -0500578 if (root->in_sysfs)
579 return root;
580
Josef Bacik58176a92007-08-29 15:47:34 -0400581 ret = btrfs_set_root_name(root, name, namelen);
582 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400583 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400584 kfree(root);
585 return ERR_PTR(ret);
586 }
587
588 ret = btrfs_sysfs_add_root(root);
589 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400590 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400591 kfree(root->name);
592 kfree(root);
593 return ERR_PTR(ret);
594 }
Chris Mason4313b392008-01-03 09:08:48 -0500595 root->in_sysfs = 1;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400596 return root;
597}
Chris Mason19c00dd2007-10-15 16:19:22 -0400598#if 0
599static int add_hasher(struct btrfs_fs_info *info, char *type) {
600 struct btrfs_hasher *hasher;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400601
Chris Mason19c00dd2007-10-15 16:19:22 -0400602 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
603 if (!hasher)
604 return -ENOMEM;
605 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
606 if (!hasher->hash_tfm) {
607 kfree(hasher);
608 return -EINVAL;
609 }
610 spin_lock(&info->hash_lock);
611 list_add(&hasher->list, &info->hashers);
612 spin_unlock(&info->hash_lock);
613 return 0;
614}
615#endif
Chris Mason2c90e5d2007-04-02 10:50:19 -0400616struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500617{
Chris Masondb945352007-10-15 16:15:53 -0400618 u32 sectorsize;
619 u32 nodesize;
620 u32 leafsize;
621 u32 blocksize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500622 u32 stripesize;
Chris Masone20d96d2007-03-22 12:13:20 -0400623 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
624 GFP_NOFS);
625 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
626 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400627 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
628 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500629 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400630 int err = -EIO;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400631 struct btrfs_super_block *disk_super;
Chris Masoneb60cea2007-02-02 09:18:22 -0500632
Chris Mason39279cc2007-06-12 06:35:45 -0400633 if (!extent_root || !tree_root || !fs_info) {
634 err = -ENOMEM;
635 goto fail;
636 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400637 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason8fd17792007-04-19 21:01:03 -0400638 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -0400639 INIT_LIST_HEAD(&fs_info->dead_roots);
Chris Mason19c00dd2007-10-15 16:19:22 -0400640 INIT_LIST_HEAD(&fs_info->hashers);
641 spin_lock_init(&fs_info->hash_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -0500642 spin_lock_init(&fs_info->delalloc_lock);
Chris Masoncee36a02008-01-15 08:40:48 -0500643 spin_lock_init(&fs_info->new_trans_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -0400644
Josef Bacik58176a92007-08-29 15:47:34 -0400645 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
646 init_completion(&fs_info->kobj_unregister);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400647 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400648 fs_info->running_transaction = NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400649 fs_info->last_trans_committed = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400650 fs_info->tree_root = tree_root;
651 fs_info->extent_root = extent_root;
Chris Masone20d96d2007-03-22 12:13:20 -0400652 fs_info->sb = sb;
Chris Masone2008b62008-01-08 15:46:30 -0500653 fs_info->throttles = 0;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500654 fs_info->mount_opt = 0;
Chris Masonc59f8952007-12-17 20:14:04 -0500655 fs_info->max_extent = (u64)-1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500656 fs_info->delalloc_bytes = 0;
Chris Masond98237b2007-03-28 13:57:48 -0400657 fs_info->btree_inode = new_inode(sb);
658 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400659 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400660 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
661 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason5f39d392007-10-15 16:14:19 -0400662 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
663 fs_info->btree_inode->i_mapping,
664 GFP_NOFS);
Chris Mason0da54682007-11-07 21:08:01 -0500665 BTRFS_I(fs_info->btree_inode)->extent_tree.ops = &btree_extent_map_ops;
666
Chris Masonf510cfe2007-10-15 16:14:48 -0400667 extent_map_tree_init(&fs_info->free_space_cache,
668 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason96b51792007-10-15 16:15:19 -0400669 extent_map_tree_init(&fs_info->block_group_cache,
670 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason1a5bc162007-10-15 16:15:26 -0400671 extent_map_tree_init(&fs_info->pinned_extents,
672 fs_info->btree_inode->i_mapping, GFP_NOFS);
673 extent_map_tree_init(&fs_info->pending_del,
674 fs_info->btree_inode->i_mapping, GFP_NOFS);
675 extent_map_tree_init(&fs_info->extent_ins,
676 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masone66f7092007-04-20 13:16:02 -0400677 fs_info->do_barriers = 1;
Chris Masonfacda1e2007-06-08 18:11:48 -0400678 fs_info->closing = 0;
Yan324ae4d2007-11-16 14:57:08 -0500679 fs_info->total_pinned = 0;
Chris Masone18e4802008-01-18 10:54:22 -0500680 fs_info->last_alloc = 0;
681
Chris Mason6da6aba2007-12-18 16:15:09 -0500682#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
683 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
684#else
Chris Mason08607c12007-06-08 15:33:54 -0400685 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason6da6aba2007-12-18 16:15:09 -0500686#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -0400687 BTRFS_I(fs_info->btree_inode)->root = tree_root;
688 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
689 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400690 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400691 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400692
Chris Mason79154b12007-03-22 15:59:16 -0400693 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400694 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400695
Chris Mason19c00dd2007-10-15 16:19:22 -0400696#if 0
697 ret = add_hasher(fs_info, "crc32c");
698 if (ret) {
699 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
700 err = -ENOMEM;
701 goto fail_iput;
702 }
703#endif
Chris Mason87ee04e2007-11-30 11:30:34 -0500704 __setup_root(512, 512, 512, 512, tree_root,
Chris Mason2c90e5d2007-04-02 10:50:19 -0400705 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400706
Chris Mason2c90e5d2007-04-02 10:50:19 -0400707 fs_info->sb_buffer = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400708 BTRFS_SUPER_INFO_OFFSET,
709 512);
Chris Masond98237b2007-03-28 13:57:48 -0400710
Chris Mason0f7d52f2007-04-09 10:42:37 -0400711 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -0400712 goto fail_iput;
Chris Mason39279cc2007-06-12 06:35:45 -0400713
Chris Mason5f39d392007-10-15 16:14:19 -0400714 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
715 sizeof(fs_info->super_copy));
716
717 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
718 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
719 BTRFS_FSID_SIZE);
720 disk_super = &fs_info->super_copy;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400721 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -0400722 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400723
Chris Masondb945352007-10-15 16:15:53 -0400724 nodesize = btrfs_super_nodesize(disk_super);
725 leafsize = btrfs_super_leafsize(disk_super);
726 sectorsize = btrfs_super_sectorsize(disk_super);
Chris Mason87ee04e2007-11-30 11:30:34 -0500727 stripesize = btrfs_super_stripesize(disk_super);
Chris Masondb945352007-10-15 16:15:53 -0400728 tree_root->nodesize = nodesize;
729 tree_root->leafsize = leafsize;
730 tree_root->sectorsize = sectorsize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500731 tree_root->stripesize = stripesize;
Chris Masonff79f812007-10-15 16:22:25 -0400732 sb_set_blocksize(sb, sectorsize);
Chris Masondb945352007-10-15 16:15:53 -0400733
Chris Mason8352d8a2007-04-12 10:43:05 -0400734 i_size_write(fs_info->btree_inode,
Chris Masondb945352007-10-15 16:15:53 -0400735 btrfs_super_total_bytes(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -0400736
Chris Mason39279cc2007-06-12 06:35:45 -0400737 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
738 sizeof(disk_super->magic))) {
739 printk("btrfs: valid FS not found on %s\n", sb->s_id);
740 goto fail_sb_buffer;
741 }
Chris Mason19c00dd2007-10-15 16:19:22 -0400742
Chris Masondb945352007-10-15 16:15:53 -0400743 blocksize = btrfs_level_size(tree_root,
744 btrfs_super_root_level(disk_super));
Chris Mason19c00dd2007-10-15 16:19:22 -0400745
Chris Masone20d96d2007-03-22 12:13:20 -0400746 tree_root->node = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400747 btrfs_super_root(disk_super),
748 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -0400749 if (!tree_root->node)
750 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -0400751
Chris Mason2c90e5d2007-04-02 10:50:19 -0400752 mutex_lock(&fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400753
754 ret = find_and_setup_root(tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400755 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason39279cc2007-06-12 06:35:45 -0400756 if (ret) {
757 mutex_unlock(&fs_info->fs_mutex);
758 goto fail_tree_root;
759 }
Chris Mason3768f362007-03-13 16:47:54 -0400760
Chris Mason9078a3e2007-04-26 16:46:15 -0400761 btrfs_read_block_groups(extent_root);
762
Chris Mason0f7d52f2007-04-09 10:42:37 -0400763 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400764 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400765 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -0400766
767fail_tree_root:
Chris Mason5f39d392007-10-15 16:14:19 -0400768 free_extent_buffer(tree_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -0400769fail_sb_buffer:
Chris Mason5f39d392007-10-15 16:14:19 -0400770 free_extent_buffer(fs_info->sb_buffer);
Chris Mason39279cc2007-06-12 06:35:45 -0400771fail_iput:
772 iput(fs_info->btree_inode);
773fail:
774 kfree(extent_root);
775 kfree(tree_root);
776 kfree(fs_info);
777 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -0500778}
779
Chris Masone089f052007-03-16 16:20:31 -0400780int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400781 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500782{
Chris Masone66f7092007-04-20 13:16:02 -0400783 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -0400784 struct extent_buffer *super = root->fs_info->sb_buffer;
785 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason21ad10c2008-01-09 09:23:21 -0500786 struct super_block *sb = root->fs_info->sb;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400787
Chris Mason21ad10c2008-01-09 09:23:21 -0500788 if (!btrfs_test_opt(root, NOBARRIER))
789 blkdev_issue_flush(sb->s_bdev, NULL);
Chris Mason5f39d392007-10-15 16:14:19 -0400790 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
791 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
792 super->start, super->len);
Chris Mason21ad10c2008-01-09 09:23:21 -0500793 if (!btrfs_test_opt(root, NOBARRIER))
794 blkdev_issue_flush(sb->s_bdev, NULL);
Chris Mason5f39d392007-10-15 16:14:19 -0400795 return ret;
Chris Masoncfaa7292007-02-21 17:04:57 -0500796}
797
Chris Mason5eda7b52007-06-22 14:16:25 -0400798int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -0400799{
800 radix_tree_delete(&fs_info->fs_roots_radix,
801 (unsigned long)root->root_key.objectid);
Chris Masonb99aa6c2008-01-14 14:41:16 -0500802 if (root->in_sysfs)
803 btrfs_sysfs_del_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -0400804 if (root->inode)
805 iput(root->inode);
806 if (root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400807 free_extent_buffer(root->node);
Chris Mason2619ba12007-04-10 16:58:11 -0400808 if (root->commit_root)
Chris Mason5f39d392007-10-15 16:14:19 -0400809 free_extent_buffer(root->commit_root);
Josef Bacik58176a92007-08-29 15:47:34 -0400810 if (root->name)
811 kfree(root->name);
Chris Mason2619ba12007-04-10 16:58:11 -0400812 kfree(root);
813 return 0;
814}
815
Chris Mason35b7e472007-05-02 15:53:43 -0400816static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400817{
818 int ret;
819 struct btrfs_root *gang[8];
820 int i;
821
822 while(1) {
823 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
824 (void **)gang, 0,
825 ARRAY_SIZE(gang));
826 if (!ret)
827 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400828 for (i = 0; i < ret; i++)
Chris Mason5eda7b52007-06-22 14:16:25 -0400829 btrfs_free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400830 }
831 return 0;
832}
Chris Masonb4100d62007-04-12 12:14:00 -0400833
Chris Masone20d96d2007-03-22 12:13:20 -0400834int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500835{
Chris Mason3768f362007-03-13 16:47:54 -0400836 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400837 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400838 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400839
Chris Masonfacda1e2007-06-08 18:11:48 -0400840 fs_info->closing = 1;
Chris Mason08607c12007-06-08 15:33:54 -0400841 btrfs_transaction_flush_work(root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400842 mutex_lock(&fs_info->fs_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -0400843 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason79154b12007-03-22 15:59:16 -0400844 trans = btrfs_start_transaction(root, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400845 ret = btrfs_commit_transaction(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400846 /* run commit again to drop the original snapshot */
847 trans = btrfs_start_transaction(root, 1);
848 btrfs_commit_transaction(trans, root);
849 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400850 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400851 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400852 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500853
Chris Mason0f7d52f2007-04-09 10:42:37 -0400854 if (fs_info->extent_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400855 free_extent_buffer(fs_info->extent_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -0400856
Chris Mason0f7d52f2007-04-09 10:42:37 -0400857 if (fs_info->tree_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400858 free_extent_buffer(fs_info->tree_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -0400859
Chris Mason5f39d392007-10-15 16:14:19 -0400860 free_extent_buffer(fs_info->sb_buffer);
Chris Mason7eccb902007-04-11 15:53:25 -0400861
Chris Mason9078a3e2007-04-26 16:46:15 -0400862 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400863 del_fs_roots(fs_info);
Chris Masond10c5f32007-12-17 20:14:04 -0500864
865 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
866
867 extent_map_tree_empty_lru(&fs_info->free_space_cache);
868 extent_map_tree_empty_lru(&fs_info->block_group_cache);
869 extent_map_tree_empty_lru(&fs_info->pinned_extents);
870 extent_map_tree_empty_lru(&fs_info->pending_del);
871 extent_map_tree_empty_lru(&fs_info->extent_ins);
Chris Mason19c00dd2007-10-15 16:19:22 -0400872 extent_map_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->extent_tree);
Chris Masond10c5f32007-12-17 20:14:04 -0500873
Chris Masondb945352007-10-15 16:15:53 -0400874 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
Chris Masond10c5f32007-12-17 20:14:04 -0500875
Chris Masondb945352007-10-15 16:15:53 -0400876 iput(fs_info->btree_inode);
Chris Mason19c00dd2007-10-15 16:19:22 -0400877#if 0
878 while(!list_empty(&fs_info->hashers)) {
879 struct btrfs_hasher *hasher;
880 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
881 hashers);
882 list_del(&hasher->hashers);
883 crypto_free_hash(&fs_info->hash_tfm);
884 kfree(hasher);
885 }
886#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -0400887 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400888 kfree(fs_info->tree_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500889 return 0;
890}
891
Chris Mason5f39d392007-10-15 16:14:19 -0400892int btrfs_buffer_uptodate(struct extent_buffer *buf)
Chris Masonccd467d2007-06-28 15:57:36 -0400893{
Chris Mason810191f2007-10-15 16:18:55 -0400894 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Mason5f39d392007-10-15 16:14:19 -0400895 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
896}
Chris Mason6702ed42007-08-07 16:15:09 -0400897
Chris Mason5f39d392007-10-15 16:14:19 -0400898int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
899{
Chris Mason810191f2007-10-15 16:18:55 -0400900 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Mason5f39d392007-10-15 16:14:19 -0400901 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
902 buf);
903}
904
905void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
906{
Chris Mason810191f2007-10-15 16:18:55 -0400907 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason5f39d392007-10-15 16:14:19 -0400908 u64 transid = btrfs_header_generation(buf);
909 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason6702ed42007-08-07 16:15:09 -0400910
Chris Masonccd467d2007-06-28 15:57:36 -0400911 if (transid != root->fs_info->generation) {
912 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
Chris Masondb945352007-10-15 16:15:53 -0400913 (unsigned long long)buf->start,
Chris Masonccd467d2007-06-28 15:57:36 -0400914 transid, root->fs_info->generation);
915 WARN_ON(1);
916 }
Chris Mason5f39d392007-10-15 16:14:19 -0400917 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500918}
919
Chris Masone2008b62008-01-08 15:46:30 -0500920void btrfs_throttle(struct btrfs_root *root)
921{
Chris Mason55c69072008-01-09 15:55:33 -0500922 struct backing_dev_info *bdi;
923
924 bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
Chris Mason04005cc2008-01-17 12:01:41 -0500925 if (root->fs_info->throttles && bdi_write_congested(bdi)) {
926#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
Chris Mason55c69072008-01-09 15:55:33 -0500927 congestion_wait(WRITE, HZ/20);
Chris Mason04005cc2008-01-17 12:01:41 -0500928#else
929 blk_congestion_wait(WRITE, HZ/20);
930#endif
931 }
Chris Masone2008b62008-01-08 15:46:30 -0500932}
933
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400934void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
Chris Mason35b7e472007-05-02 15:53:43 -0400935{
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400936 balance_dirty_pages_ratelimited_nr(
Chris Mason304fced2007-10-15 16:21:17 -0400937 root->fs_info->btree_inode->i_mapping, 1);
Chris Mason35b7e472007-05-02 15:53:43 -0400938}
Chris Mason6b800532007-10-15 16:17:34 -0400939
940void btrfs_set_buffer_defrag(struct extent_buffer *buf)
941{
Chris Mason810191f2007-10-15 16:18:55 -0400942 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400943 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, GFP_NOFS);
946}
947
948void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
949{
Chris Mason810191f2007-10-15 16:18:55 -0400950 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400951 struct inode *btree_inode = root->fs_info->btree_inode;
952 set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
953 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
954 GFP_NOFS);
955}
956
957int btrfs_buffer_defrag(struct extent_buffer *buf)
958{
Chris Mason810191f2007-10-15 16:18:55 -0400959 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400960 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, EXTENT_DEFRAG, 0);
963}
964
965int btrfs_buffer_defrag_done(struct extent_buffer *buf)
966{
Chris Mason810191f2007-10-15 16:18:55 -0400967 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400968 struct inode *btree_inode = root->fs_info->btree_inode;
969 return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
970 buf->start, buf->start + buf->len - 1,
971 EXTENT_DEFRAG_DONE, 0);
972}
973
974int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
975{
Chris Mason810191f2007-10-15 16:18:55 -0400976 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400977 struct inode *btree_inode = root->fs_info->btree_inode;
978 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
979 buf->start, buf->start + buf->len - 1,
980 EXTENT_DEFRAG_DONE, GFP_NOFS);
981}
982
983int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
984{
Chris Mason810191f2007-10-15 16:18:55 -0400985 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400986 struct inode *btree_inode = root->fs_info->btree_inode;
987 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
988 buf->start, buf->start + buf->len - 1,
989 EXTENT_DEFRAG, GFP_NOFS);
990}
991
992int btrfs_read_buffer(struct extent_buffer *buf)
993{
Chris Mason810191f2007-10-15 16:18:55 -0400994 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -0400995 struct inode *btree_inode = root->fs_info->btree_inode;
996 return read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -0400997 buf, 0, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400998}
Chris Mason0da54682007-11-07 21:08:01 -0500999
1000static struct extent_map_ops btree_extent_map_ops = {
1001 .writepage_io_hook = btree_writepage_io_hook,
1002};