blob: 4890151cd68d6143357857b2351ee6c1f831ad56 [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 Mason0b86a832008-03-24 15:01:56 -040031#include "volumes.h"
Chris Masondb945352007-10-15 16:15:53 -040032#include "print-tree.h"
Chris Masoneb60cea2007-02-02 09:18:22 -050033
Chris Mason5f39d392007-10-15 16:14:19 -040034#if 0
35static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
Chris Mason7eccb902007-04-11 15:53:25 -040036{
Chris Mason5f39d392007-10-15 16:14:19 -040037 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
38 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
39 (unsigned long long)extent_buffer_blocknr(buf),
40 (unsigned long long)btrfs_header_blocknr(buf));
Chris Mason39279cc2007-06-12 06:35:45 -040041 return 1;
Chris Masond98237b2007-03-28 13:57:48 -040042 }
Chris Mason9a8dd152007-02-23 08:38:36 -050043 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050044}
Chris Mason5f39d392007-10-15 16:14:19 -040045#endif
Chris Masoneb60cea2007-02-02 09:18:22 -050046
Chris Masond1310b22008-01-24 16:13:08 -050047static struct extent_io_ops btree_extent_io_ops;
Chris Mason0da54682007-11-07 21:08:01 -050048
Chris Mason5f39d392007-10-15 16:14:19 -040049struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -040050 u64 bytenr, u32 blocksize)
Chris Masoned2ff2c2007-03-01 18:59:40 -050051{
Chris Mason5f39d392007-10-15 16:14:19 -040052 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonf510cfe2007-10-15 16:14:48 -040053 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050054 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
Chris Masondb945352007-10-15 16:15:53 -040055 bytenr, blocksize, GFP_NOFS);
Chris Masonf510cfe2007-10-15 16:14:48 -040056 return eb;
Chris Masoned2ff2c2007-03-01 18:59:40 -050057}
58
Chris Mason5f39d392007-10-15 16:14:19 -040059struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -040060 u64 bytenr, u32 blocksize)
Chris Masoneb60cea2007-02-02 09:18:22 -050061{
Chris Mason5f39d392007-10-15 16:14:19 -040062 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonf510cfe2007-10-15 16:14:48 -040063 struct extent_buffer *eb;
Chris Masondb945352007-10-15 16:15:53 -040064
Chris Masond1310b22008-01-24 16:13:08 -050065 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
Chris Mason19c00dd2007-10-15 16:19:22 -040066 bytenr, blocksize, NULL, GFP_NOFS);
Chris Masonf510cfe2007-10-15 16:14:48 -040067 return eb;
Chris Mason5f39d392007-10-15 16:14:19 -040068}
Chris Mason22b0ebd2007-03-30 08:47:31 -040069
Chris Mason5f39d392007-10-15 16:14:19 -040070struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
Chris Masond1310b22008-01-24 16:13:08 -050071 size_t page_offset, u64 start, u64 len,
Chris Mason5f39d392007-10-15 16:14:19 -040072 int create)
73{
74 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
75 struct extent_map *em;
76 int ret;
Chris Masond98237b2007-03-28 13:57:48 -040077
Chris Mason5f39d392007-10-15 16:14:19 -040078again:
Chris Masond1310b22008-01-24 16:13:08 -050079 spin_lock(&em_tree->lock);
80 em = lookup_extent_mapping(em_tree, start, len);
81 spin_unlock(&em_tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -040082 if (em) {
83 goto out;
84 }
85 em = alloc_extent_map(GFP_NOFS);
86 if (!em) {
87 em = ERR_PTR(-ENOMEM);
88 goto out;
89 }
90 em->start = 0;
Chris Masond1310b22008-01-24 16:13:08 -050091 em->len = i_size_read(inode);
Chris Mason5f39d392007-10-15 16:14:19 -040092 em->block_start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -040093 em->bdev = inode->i_sb->s_bdev;
Chris Masond1310b22008-01-24 16:13:08 -050094
95 spin_lock(&em_tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -040096 ret = add_extent_mapping(em_tree, em);
Chris Masond1310b22008-01-24 16:13:08 -050097 spin_unlock(&em_tree->lock);
98
Chris Mason5f39d392007-10-15 16:14:19 -040099 if (ret == -EEXIST) {
100 free_extent_map(em);
101 em = NULL;
102 goto again;
103 } else if (ret) {
104 em = ERR_PTR(ret);
105 }
106out:
107 return em;
108}
109
Chris Mason19c00dd2007-10-15 16:19:22 -0400110u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
111{
112 return crc32c(seed, data, len);
113}
114
115void btrfs_csum_final(u32 crc, char *result)
116{
117 *(__le32 *)result = ~cpu_to_le32(crc);
118}
119
120static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
121 int verify)
122{
123 char result[BTRFS_CRC32_SIZE];
124 unsigned long len;
125 unsigned long cur_len;
126 unsigned long offset = BTRFS_CSUM_SIZE;
127 char *map_token = NULL;
128 char *kaddr;
129 unsigned long map_start;
130 unsigned long map_len;
131 int err;
132 u32 crc = ~(u32)0;
133
134 len = buf->len - offset;
135 while(len > 0) {
136 err = map_private_extent_buffer(buf, offset, 32,
137 &map_token, &kaddr,
138 &map_start, &map_len, KM_USER0);
139 if (err) {
140 printk("failed to map extent buffer! %lu\n",
141 offset);
142 return 1;
143 }
144 cur_len = min(len, map_len - (offset - map_start));
145 crc = btrfs_csum_data(root, kaddr + offset - map_start,
146 crc, cur_len);
147 len -= cur_len;
148 offset += cur_len;
149 unmap_extent_buffer(buf, map_token, KM_USER0);
150 }
151 btrfs_csum_final(crc, result);
152
153 if (verify) {
Chris Masone4204de2008-01-08 15:46:27 -0500154 int from_this_trans = 0;
155
156 if (root->fs_info->running_transaction &&
157 btrfs_header_generation(buf) ==
158 root->fs_info->running_transaction->transid)
159 from_this_trans = 1;
160
161 /* FIXME, this is not good */
162 if (from_this_trans == 0 &&
163 memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
164 u32 val;
165 u32 found = 0;
166 memcpy(&found, result, BTRFS_CRC32_SIZE);
167
168 read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
169 printk("btrfs: %s checksum verify failed on %llu "
170 "wanted %X found %X from_this_trans %d\n",
Chris Mason19c00dd2007-10-15 16:19:22 -0400171 root->fs_info->sb->s_id,
Chris Masone4204de2008-01-08 15:46:27 -0500172 buf->start, val, found, from_this_trans);
Chris Mason19c00dd2007-10-15 16:19:22 -0400173 return 1;
174 }
175 } else {
176 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
177 }
178 return 0;
179}
180
181
182int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
183{
Chris Masond1310b22008-01-24 16:13:08 -0500184 struct extent_io_tree *tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400185 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason19c00dd2007-10-15 16:19:22 -0400186 u64 found_start;
187 int found_level;
188 unsigned long len;
189 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -0500190 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason19c00dd2007-10-15 16:19:22 -0400191
192 if (page->private == EXTENT_PAGE_PRIVATE)
193 goto out;
194 if (!page->private)
195 goto out;
196 len = page->private >> 2;
197 if (len == 0) {
198 WARN_ON(1);
199 }
200 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
Chris Masona86c12c2008-02-07 10:50:54 -0500201 read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1,
202 btree_get_extent);
Chris Masone18e4802008-01-18 10:54:22 -0500203 btrfs_clear_buffer_defrag(eb);
Chris Mason19c00dd2007-10-15 16:19:22 -0400204 found_start = btrfs_header_bytenr(eb);
205 if (found_start != start) {
206 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
207 start, found_start, len);
Chris Mason55c69072008-01-09 15:55:33 -0500208 WARN_ON(1);
209 goto err;
210 }
211 if (eb->first_page != page) {
212 printk("bad first page %lu %lu\n", eb->first_page->index,
213 page->index);
214 WARN_ON(1);
215 goto err;
216 }
217 if (!PageUptodate(page)) {
218 printk("csum not up to date page %lu\n", page->index);
219 WARN_ON(1);
220 goto err;
Chris Mason19c00dd2007-10-15 16:19:22 -0400221 }
222 found_level = btrfs_header_level(eb);
223 csum_tree_block(root, eb, 0);
Chris Mason55c69072008-01-09 15:55:33 -0500224err:
Chris Mason19c00dd2007-10-15 16:19:22 -0400225 free_extent_buffer(eb);
226out:
227 return 0;
228}
229
Chris Mason0da54682007-11-07 21:08:01 -0500230static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
231{
232 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
233
234 csum_dirty_buffer(root, page);
235 return 0;
236}
237
Chris Mason0b86a832008-03-24 15:01:56 -0400238static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio)
239{
240 struct btrfs_root *root = BTRFS_I(inode)->root;
241 u64 offset;
242 offset = bio->bi_sector << 9;
243 if (offset == BTRFS_SUPER_INFO_OFFSET) {
244 bio->bi_bdev = root->fs_info->sb->s_bdev;
245 submit_bio(rw, bio);
246 return 0;
247 }
248 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
249}
250
Chris Mason5f39d392007-10-15 16:14:19 -0400251static int btree_writepage(struct page *page, struct writeback_control *wbc)
252{
Chris Masond1310b22008-01-24 16:13:08 -0500253 struct extent_io_tree *tree;
254 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400255 return extent_write_full_page(tree, page, btree_get_extent, wbc);
256}
Chris Mason0da54682007-11-07 21:08:01 -0500257
258static int btree_writepages(struct address_space *mapping,
259 struct writeback_control *wbc)
260{
Chris Masond1310b22008-01-24 16:13:08 -0500261 struct extent_io_tree *tree;
262 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500263 if (wbc->sync_mode == WB_SYNC_NONE) {
Chris Mason793955b2007-11-26 16:34:41 -0800264 u64 num_dirty;
265 u64 start = 0;
266 unsigned long thresh = 96 * 1024 * 1024;
Chris Mason448d6402007-11-27 07:52:01 -0800267
268 if (wbc->for_kupdate)
269 return 0;
270
Chris Masonca664622007-11-27 11:16:35 -0500271 if (current_is_pdflush()) {
272 thresh = 96 * 1024 * 1024;
273 } else {
274 thresh = 8 * 1024 * 1024;
275 }
Chris Mason1832a6d2007-12-21 16:27:21 -0500276 num_dirty = count_range_bits(tree, &start, (u64)-1,
277 thresh, EXTENT_DIRTY);
Chris Mason793955b2007-11-26 16:34:41 -0800278 if (num_dirty < thresh) {
279 return 0;
280 }
281 }
Chris Mason0da54682007-11-07 21:08:01 -0500282 return extent_writepages(tree, mapping, btree_get_extent, wbc);
283}
284
Chris Mason5f39d392007-10-15 16:14:19 -0400285int btree_readpage(struct file *file, struct page *page)
286{
Chris Masond1310b22008-01-24 16:13:08 -0500287 struct extent_io_tree *tree;
288 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400289 return extent_read_full_page(tree, page, btree_get_extent);
290}
291
Chris Mason70dec802008-01-29 09:59:12 -0500292static int btree_releasepage(struct page *page, gfp_t gfp_flags)
Chris Mason5f39d392007-10-15 16:14:19 -0400293{
Chris Masond1310b22008-01-24 16:13:08 -0500294 struct extent_io_tree *tree;
295 struct extent_map_tree *map;
Chris Mason5f39d392007-10-15 16:14:19 -0400296 int ret;
297
Chris Masond1310b22008-01-24 16:13:08 -0500298 tree = &BTRFS_I(page->mapping->host)->io_tree;
299 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -0500300 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400301 if (ret == 1) {
302 ClearPagePrivate(page);
303 set_page_private(page, 0);
304 page_cache_release(page);
305 }
Chris Masond98237b2007-03-28 13:57:48 -0400306 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400307}
Chris Mason123abc82007-03-14 14:14:43 -0400308
Chris Mason5f39d392007-10-15 16:14:19 -0400309static void btree_invalidatepage(struct page *page, unsigned long offset)
Chris Masond98237b2007-03-28 13:57:48 -0400310{
Chris Masond1310b22008-01-24 16:13:08 -0500311 struct extent_io_tree *tree;
312 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400313 extent_invalidatepage(tree, page, offset);
314 btree_releasepage(page, GFP_NOFS);
Chris Masond98237b2007-03-28 13:57:48 -0400315}
316
Chris Mason5f39d392007-10-15 16:14:19 -0400317#if 0
Chris Masond98237b2007-03-28 13:57:48 -0400318static int btree_writepage(struct page *page, struct writeback_control *wbc)
319{
Chris Mason87cbda52007-03-28 19:44:27 -0400320 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400321 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400322 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400323 if (!page_has_buffers(page)) {
324 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
325 (1 << BH_Dirty)|(1 << BH_Uptodate));
326 }
327 head = page_buffers(page);
328 bh = head;
329 do {
330 if (buffer_dirty(bh))
331 csum_tree_block(root, bh, 0);
332 bh = bh->b_this_page;
333 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400334 return block_write_full_page(page, btree_get_block, wbc);
335}
Chris Mason5f39d392007-10-15 16:14:19 -0400336#endif
Chris Masond98237b2007-03-28 13:57:48 -0400337
338static struct address_space_operations btree_aops = {
339 .readpage = btree_readpage,
340 .writepage = btree_writepage,
Chris Mason0da54682007-11-07 21:08:01 -0500341 .writepages = btree_writepages,
Chris Mason5f39d392007-10-15 16:14:19 -0400342 .releasepage = btree_releasepage,
343 .invalidatepage = btree_invalidatepage,
Chris Masond98237b2007-03-28 13:57:48 -0400344 .sync_page = block_sync_page,
345};
346
Chris Masondb945352007-10-15 16:15:53 -0400347int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
Chris Mason090d1872007-05-01 08:53:32 -0400348{
Chris Mason5f39d392007-10-15 16:14:19 -0400349 struct extent_buffer *buf = NULL;
350 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonde428b62007-05-18 13:28:27 -0400351 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400352
Chris Masondb945352007-10-15 16:15:53 -0400353 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400354 if (!buf)
Chris Mason090d1872007-05-01 08:53:32 -0400355 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500356 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
Chris Masona86c12c2008-02-07 10:50:54 -0500357 buf, 0, 0, btree_get_extent);
Chris Mason5f39d392007-10-15 16:14:19 -0400358 free_extent_buffer(buf);
Chris Masonde428b62007-05-18 13:28:27 -0400359 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400360}
361
Chris Mason0b86a832008-03-24 15:01:56 -0400362static int close_all_devices(struct btrfs_fs_info *fs_info)
363{
364 struct list_head *list;
365 struct list_head *next;
366 struct btrfs_device *device;
367
368 list = &fs_info->devices;
369 while(!list_empty(list)) {
370 next = list->next;
371 list_del(next);
372 device = list_entry(next, struct btrfs_device, dev_list);
Chris Mason0b86a832008-03-24 15:01:56 -0400373 kfree(device);
374 }
375 return 0;
376}
377
Chris Masondb945352007-10-15 16:15:53 -0400378struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
379 u32 blocksize)
Chris Masone20d96d2007-03-22 12:13:20 -0400380{
Chris Mason5f39d392007-10-15 16:14:19 -0400381 struct extent_buffer *buf = NULL;
382 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -0500383 struct extent_io_tree *io_tree;
Chris Masone4204de2008-01-08 15:46:27 -0500384 u64 end;
Chris Mason19c00dd2007-10-15 16:19:22 -0400385 int ret;
386
Chris Masond1310b22008-01-24 16:13:08 -0500387 io_tree = &BTRFS_I(btree_inode)->io_tree;
Chris Masone20d96d2007-03-22 12:13:20 -0400388
Chris Masondb945352007-10-15 16:15:53 -0400389 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400390 if (!buf)
391 return NULL;
Chris Masona86c12c2008-02-07 10:50:54 -0500392 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 0, 1,
393 btree_get_extent);
Chris Masone4204de2008-01-08 15:46:27 -0500394
395 if (buf->flags & EXTENT_CSUM)
Chris Mason19c00dd2007-10-15 16:19:22 -0400396 return buf;
Chris Masone4204de2008-01-08 15:46:27 -0500397
398 end = buf->start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500399 if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
Chris Mason19c00dd2007-10-15 16:19:22 -0400400 buf->flags |= EXTENT_CSUM;
401 return buf;
402 }
Chris Masone4204de2008-01-08 15:46:27 -0500403
Chris Masond1310b22008-01-24 16:13:08 -0500404 lock_extent(io_tree, buf->start, end, GFP_NOFS);
Chris Masone4204de2008-01-08 15:46:27 -0500405
Chris Masond1310b22008-01-24 16:13:08 -0500406 if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
Chris Masone4204de2008-01-08 15:46:27 -0500407 buf->flags |= EXTENT_CSUM;
408 goto out_unlock;
409 }
410
Chris Mason19c00dd2007-10-15 16:19:22 -0400411 ret = csum_tree_block(root, buf, 1);
Chris Masond1310b22008-01-24 16:13:08 -0500412 set_extent_bits(io_tree, buf->start, end, EXTENT_CSUM, GFP_NOFS);
Chris Mason19c00dd2007-10-15 16:19:22 -0400413 buf->flags |= EXTENT_CSUM;
Chris Masone4204de2008-01-08 15:46:27 -0500414
415out_unlock:
Chris Masond1310b22008-01-24 16:13:08 -0500416 unlock_extent(io_tree, buf->start, end, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -0400417 return buf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500418}
419
Chris Masone089f052007-03-16 16:20:31 -0400420int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400421 struct extent_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500422{
Chris Mason5f39d392007-10-15 16:14:19 -0400423 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason55c69072008-01-09 15:55:33 -0500424 if (btrfs_header_generation(buf) ==
425 root->fs_info->running_transaction->transid)
Chris Masond1310b22008-01-24 16:13:08 -0500426 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
Chris Mason55c69072008-01-09 15:55:33 -0500427 buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400428 return 0;
429}
430
431int wait_on_tree_block_writeback(struct btrfs_root *root,
432 struct extent_buffer *buf)
433{
434 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -0500435 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -0400436 buf);
437 return 0;
438}
439
Chris Masondb945352007-10-15 16:15:53 -0400440static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500441 u32 stripesize, struct btrfs_root *root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400442 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400443 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500444{
Chris Masoncfaa7292007-02-21 17:04:57 -0500445 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400446 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500447 root->commit_root = NULL;
Chris Masondb945352007-10-15 16:15:53 -0400448 root->sectorsize = sectorsize;
449 root->nodesize = nodesize;
450 root->leafsize = leafsize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500451 root->stripesize = stripesize;
Chris Mason123abc82007-03-14 14:14:43 -0400452 root->ref_cows = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400453 root->track_dirty = 0;
454
Chris Mason9f5fae22007-03-20 14:38:32 -0400455 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400456 root->objectid = objectid;
457 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400458 root->highest_inode = 0;
459 root->last_inode_alloc = 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400460 root->name = NULL;
Chris Mason4313b392008-01-03 09:08:48 -0500461 root->in_sysfs = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400462
463 INIT_LIST_HEAD(&root->dirty_list);
Chris Mason3768f362007-03-13 16:47:54 -0400464 memset(&root->root_key, 0, sizeof(root->root_key));
465 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -0400466 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Josef Bacik58176a92007-08-29 15:47:34 -0400467 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
468 init_completion(&root->kobj_unregister);
Chris Mason6702ed42007-08-07 16:15:09 -0400469 root->defrag_running = 0;
470 root->defrag_level = 0;
Chris Mason4d775672007-04-20 20:23:12 -0400471 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400472 return 0;
473}
474
Chris Masondb945352007-10-15 16:15:53 -0400475static int find_and_setup_root(struct btrfs_root *tree_root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400476 struct btrfs_fs_info *fs_info,
477 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400478 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400479{
480 int ret;
Chris Masondb945352007-10-15 16:15:53 -0400481 u32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -0400482
Chris Masondb945352007-10-15 16:15:53 -0400483 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500484 tree_root->sectorsize, tree_root->stripesize,
485 root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400486 ret = btrfs_find_last_root(tree_root, objectid,
487 &root->root_item, &root->root_key);
488 BUG_ON(ret);
489
Chris Masondb945352007-10-15 16:15:53 -0400490 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
491 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
492 blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400493 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500494 return 0;
495}
496
Chris Mason5eda7b52007-06-22 14:16:25 -0400497struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
498 struct btrfs_key *location)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400499{
500 struct btrfs_root *root;
501 struct btrfs_root *tree_root = fs_info->tree_root;
502 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400503 struct extent_buffer *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400504 u64 highest_inode;
Chris Masondb945352007-10-15 16:15:53 -0400505 u32 blocksize;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400506 int ret = 0;
507
Chris Mason5eda7b52007-06-22 14:16:25 -0400508 root = kzalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400509 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400510 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400511 if (location->offset == (u64)-1) {
Chris Masondb945352007-10-15 16:15:53 -0400512 ret = find_and_setup_root(tree_root, fs_info,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400513 location->objectid, root);
514 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400515 kfree(root);
516 return ERR_PTR(ret);
517 }
518 goto insert;
519 }
520
Chris Masondb945352007-10-15 16:15:53 -0400521 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500522 tree_root->sectorsize, tree_root->stripesize,
523 root, fs_info, location->objectid);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400524
525 path = btrfs_alloc_path();
526 BUG_ON(!path);
527 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
528 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400529 if (ret > 0)
530 ret = -ENOENT;
531 goto out;
532 }
Chris Mason5f39d392007-10-15 16:14:19 -0400533 l = path->nodes[0];
534 read_extent_buffer(l, &root->root_item,
535 btrfs_item_ptr_offset(l, path->slots[0]),
Chris Mason0f7d52f2007-04-09 10:42:37 -0400536 sizeof(root->root_item));
Yan Zheng44b36eb2007-10-19 09:22:56 -0400537 memcpy(&root->root_key, location, sizeof(*location));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400538 ret = 0;
539out:
540 btrfs_release_path(root, path);
541 btrfs_free_path(path);
542 if (ret) {
543 kfree(root);
544 return ERR_PTR(ret);
545 }
Chris Masondb945352007-10-15 16:15:53 -0400546 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
547 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
548 blocksize);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400549 BUG_ON(!root->node);
550insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400551 root->ref_cows = 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400552 ret = btrfs_find_highest_inode(root, &highest_inode);
553 if (ret == 0) {
554 root->highest_inode = highest_inode;
555 root->last_inode_alloc = highest_inode;
556 }
557 return root;
558}
559
Chris Masondc17ff82008-01-08 15:46:30 -0500560struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
561 u64 root_objectid)
562{
563 struct btrfs_root *root;
564
565 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
566 return fs_info->tree_root;
567 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
568 return fs_info->extent_root;
569
570 root = radix_tree_lookup(&fs_info->fs_roots_radix,
571 (unsigned long)root_objectid);
572 return root;
573}
574
Chris Masonedbd8d42007-12-21 16:27:24 -0500575struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
576 struct btrfs_key *location)
Chris Mason5eda7b52007-06-22 14:16:25 -0400577{
578 struct btrfs_root *root;
579 int ret;
580
Chris Masonedbd8d42007-12-21 16:27:24 -0500581 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
582 return fs_info->tree_root;
583 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
584 return fs_info->extent_root;
585
Chris Mason5eda7b52007-06-22 14:16:25 -0400586 root = radix_tree_lookup(&fs_info->fs_roots_radix,
587 (unsigned long)location->objectid);
588 if (root)
589 return root;
590
591 root = btrfs_read_fs_root_no_radix(fs_info, location);
592 if (IS_ERR(root))
593 return root;
Chris Mason2619ba12007-04-10 16:58:11 -0400594 ret = radix_tree_insert(&fs_info->fs_roots_radix,
595 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400596 root);
597 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400598 free_extent_buffer(root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400599 kfree(root);
600 return ERR_PTR(ret);
601 }
Chris Masonedbd8d42007-12-21 16:27:24 -0500602 ret = btrfs_find_dead_roots(fs_info->tree_root,
603 root->root_key.objectid, root);
604 BUG_ON(ret);
605
606 return root;
607}
608
609struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
610 struct btrfs_key *location,
611 const char *name, int namelen)
612{
613 struct btrfs_root *root;
614 int ret;
615
616 root = btrfs_read_fs_root_no_name(fs_info, location);
617 if (!root)
618 return NULL;
Josef Bacik58176a92007-08-29 15:47:34 -0400619
Chris Mason4313b392008-01-03 09:08:48 -0500620 if (root->in_sysfs)
621 return root;
622
Josef Bacik58176a92007-08-29 15:47:34 -0400623 ret = btrfs_set_root_name(root, name, namelen);
624 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400625 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400626 kfree(root);
627 return ERR_PTR(ret);
628 }
629
630 ret = btrfs_sysfs_add_root(root);
631 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400632 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400633 kfree(root->name);
634 kfree(root);
635 return ERR_PTR(ret);
636 }
Chris Mason4313b392008-01-03 09:08:48 -0500637 root->in_sysfs = 1;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400638 return root;
639}
Chris Mason19c00dd2007-10-15 16:19:22 -0400640#if 0
641static int add_hasher(struct btrfs_fs_info *info, char *type) {
642 struct btrfs_hasher *hasher;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400643
Chris Mason19c00dd2007-10-15 16:19:22 -0400644 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
645 if (!hasher)
646 return -ENOMEM;
647 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
648 if (!hasher->hash_tfm) {
649 kfree(hasher);
650 return -EINVAL;
651 }
652 spin_lock(&info->hash_lock);
653 list_add(&hasher->list, &info->hashers);
654 spin_unlock(&info->hash_lock);
655 return 0;
656}
657#endif
Chris Mason2c90e5d2007-04-02 10:50:19 -0400658struct btrfs_root *open_ctree(struct super_block *sb)
Chris Masoneb60cea2007-02-02 09:18:22 -0500659{
Chris Masondb945352007-10-15 16:15:53 -0400660 u32 sectorsize;
661 u32 nodesize;
662 u32 leafsize;
663 u32 blocksize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500664 u32 stripesize;
Chris Masone20d96d2007-03-22 12:13:20 -0400665 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
666 GFP_NOFS);
667 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
668 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400669 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
670 GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -0400671 struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
672 GFP_NOFS);
673 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
674 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500675 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400676 int err = -EIO;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400677 struct btrfs_super_block *disk_super;
Chris Masoneb60cea2007-02-02 09:18:22 -0500678
Chris Mason39279cc2007-06-12 06:35:45 -0400679 if (!extent_root || !tree_root || !fs_info) {
680 err = -ENOMEM;
681 goto fail;
682 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400683 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason8fd17792007-04-19 21:01:03 -0400684 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -0400685 INIT_LIST_HEAD(&fs_info->dead_roots);
Chris Mason19c00dd2007-10-15 16:19:22 -0400686 INIT_LIST_HEAD(&fs_info->hashers);
687 spin_lock_init(&fs_info->hash_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -0500688 spin_lock_init(&fs_info->delalloc_lock);
Chris Masoncee36a02008-01-15 08:40:48 -0500689 spin_lock_init(&fs_info->new_trans_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -0400690
Josef Bacik58176a92007-08-29 15:47:34 -0400691 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
692 init_completion(&fs_info->kobj_unregister);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400693 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400694 fs_info->running_transaction = NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400695 fs_info->last_trans_committed = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400696 fs_info->tree_root = tree_root;
697 fs_info->extent_root = extent_root;
Chris Mason0b86a832008-03-24 15:01:56 -0400698 fs_info->chunk_root = chunk_root;
699 fs_info->dev_root = dev_root;
700 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
701 INIT_LIST_HEAD(&fs_info->devices);
Chris Mason6324fbf2008-03-24 15:01:59 -0400702 INIT_LIST_HEAD(&fs_info->space_info);
Chris Mason0b86a832008-03-24 15:01:56 -0400703 btrfs_mapping_init(&fs_info->mapping_tree);
Chris Masone20d96d2007-03-22 12:13:20 -0400704 fs_info->sb = sb;
Chris Masone2008b62008-01-08 15:46:30 -0500705 fs_info->throttles = 0;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500706 fs_info->mount_opt = 0;
Chris Masonc59f8952007-12-17 20:14:04 -0500707 fs_info->max_extent = (u64)-1;
Chris Mason6f568d32008-01-29 16:03:38 -0500708 fs_info->max_inline = 8192 * 1024;
Chris Mason1832a6d2007-12-21 16:27:21 -0500709 fs_info->delalloc_bytes = 0;
Chris Masond98237b2007-03-28 13:57:48 -0400710 fs_info->btree_inode = new_inode(sb);
711 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400712 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400713 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
714 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Masond1310b22008-01-24 16:13:08 -0500715 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -0400716 fs_info->btree_inode->i_mapping,
717 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500718 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
719 GFP_NOFS);
Chris Mason0da54682007-11-07 21:08:01 -0500720
Chris Masond1310b22008-01-24 16:13:08 -0500721 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
722
723 extent_io_tree_init(&fs_info->free_space_cache,
Chris Masonf510cfe2007-10-15 16:14:48 -0400724 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500725 extent_io_tree_init(&fs_info->block_group_cache,
Chris Mason96b51792007-10-15 16:15:19 -0400726 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500727 extent_io_tree_init(&fs_info->pinned_extents,
Chris Mason1a5bc162007-10-15 16:15:26 -0400728 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500729 extent_io_tree_init(&fs_info->pending_del,
Chris Mason1a5bc162007-10-15 16:15:26 -0400730 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500731 extent_io_tree_init(&fs_info->extent_ins,
Chris Mason1a5bc162007-10-15 16:15:26 -0400732 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masone66f7092007-04-20 13:16:02 -0400733 fs_info->do_barriers = 1;
Chris Masonfacda1e2007-06-08 18:11:48 -0400734 fs_info->closing = 0;
Yan324ae4d2007-11-16 14:57:08 -0500735 fs_info->total_pinned = 0;
Chris Masone18e4802008-01-18 10:54:22 -0500736 fs_info->last_alloc = 0;
Chris Mason4529ba42008-01-31 16:45:07 -0500737 fs_info->last_data_alloc = 0;
Chris Masone18e4802008-01-18 10:54:22 -0500738
Chris Mason6da6aba2007-12-18 16:15:09 -0500739#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
740 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
741#else
Chris Mason08607c12007-06-08 15:33:54 -0400742 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason6da6aba2007-12-18 16:15:09 -0500743#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -0400744 BTRFS_I(fs_info->btree_inode)->root = tree_root;
745 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
746 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400747 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400748 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400749
Chris Mason79154b12007-03-22 15:59:16 -0400750 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400751 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400752
Chris Mason19c00dd2007-10-15 16:19:22 -0400753#if 0
754 ret = add_hasher(fs_info, "crc32c");
755 if (ret) {
756 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
757 err = -ENOMEM;
758 goto fail_iput;
759 }
760#endif
Chris Mason0b86a832008-03-24 15:01:56 -0400761 __setup_root(4096, 4096, 4096, 4096, tree_root,
Chris Mason2c90e5d2007-04-02 10:50:19 -0400762 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400763
Chris Mason2c90e5d2007-04-02 10:50:19 -0400764 fs_info->sb_buffer = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400765 BTRFS_SUPER_INFO_OFFSET,
Chris Mason0b86a832008-03-24 15:01:56 -0400766 4096);
Chris Masond98237b2007-03-28 13:57:48 -0400767
Chris Mason0f7d52f2007-04-09 10:42:37 -0400768 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -0400769 goto fail_iput;
Chris Mason39279cc2007-06-12 06:35:45 -0400770
Chris Mason5f39d392007-10-15 16:14:19 -0400771 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
772 sizeof(fs_info->super_copy));
773
774 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
775 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
776 BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -0400777
Chris Mason5f39d392007-10-15 16:14:19 -0400778 disk_super = &fs_info->super_copy;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400779 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -0400780 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400781
Chris Masondb945352007-10-15 16:15:53 -0400782 nodesize = btrfs_super_nodesize(disk_super);
783 leafsize = btrfs_super_leafsize(disk_super);
784 sectorsize = btrfs_super_sectorsize(disk_super);
Chris Mason87ee04e2007-11-30 11:30:34 -0500785 stripesize = btrfs_super_stripesize(disk_super);
Chris Masondb945352007-10-15 16:15:53 -0400786 tree_root->nodesize = nodesize;
787 tree_root->leafsize = leafsize;
788 tree_root->sectorsize = sectorsize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500789 tree_root->stripesize = stripesize;
Chris Masonff79f812007-10-15 16:22:25 -0400790 sb_set_blocksize(sb, sectorsize);
Chris Masondb945352007-10-15 16:15:53 -0400791
Chris Mason8352d8a2007-04-12 10:43:05 -0400792 i_size_write(fs_info->btree_inode,
Chris Masondb945352007-10-15 16:15:53 -0400793 btrfs_super_total_bytes(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -0400794
Chris Mason39279cc2007-06-12 06:35:45 -0400795 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
796 sizeof(disk_super->magic))) {
797 printk("btrfs: valid FS not found on %s\n", sb->s_id);
798 goto fail_sb_buffer;
799 }
Chris Mason19c00dd2007-10-15 16:19:22 -0400800
Chris Mason0b86a832008-03-24 15:01:56 -0400801 mutex_lock(&fs_info->fs_mutex);
Chris Mason0d81ba52008-03-24 15:02:07 -0400802 ret = btrfs_read_super_device(tree_root, fs_info->sb_buffer);
803 BUG_ON(ret);
804
Chris Mason0b86a832008-03-24 15:01:56 -0400805 ret = btrfs_read_sys_array(tree_root);
806 BUG_ON(ret);
807
808 blocksize = btrfs_level_size(tree_root,
809 btrfs_super_chunk_root_level(disk_super));
810
811 __setup_root(nodesize, leafsize, sectorsize, stripesize,
812 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
813
814 chunk_root->node = read_tree_block(chunk_root,
815 btrfs_super_chunk_root(disk_super),
816 blocksize);
817 BUG_ON(!chunk_root->node);
818
819 ret = btrfs_read_chunk_tree(chunk_root);
820 BUG_ON(ret);
821
Chris Masondb945352007-10-15 16:15:53 -0400822 blocksize = btrfs_level_size(tree_root,
823 btrfs_super_root_level(disk_super));
Chris Mason19c00dd2007-10-15 16:19:22 -0400824
Chris Mason0b86a832008-03-24 15:01:56 -0400825
Chris Masone20d96d2007-03-22 12:13:20 -0400826 tree_root->node = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400827 btrfs_super_root(disk_super),
828 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -0400829 if (!tree_root->node)
830 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -0400831
Chris Masondb945352007-10-15 16:15:53 -0400832
833 ret = find_and_setup_root(tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400834 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason0b86a832008-03-24 15:01:56 -0400835 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400836 goto fail_tree_root;
Chris Mason0b86a832008-03-24 15:01:56 -0400837 extent_root->track_dirty = 1;
838
839 ret = find_and_setup_root(tree_root, fs_info,
840 BTRFS_DEV_TREE_OBJECTID, dev_root);
841 dev_root->track_dirty = 1;
842
843 if (ret)
844 goto fail_extent_root;
Chris Mason3768f362007-03-13 16:47:54 -0400845
Chris Mason9078a3e2007-04-26 16:46:15 -0400846 btrfs_read_block_groups(extent_root);
847
Chris Mason0f7d52f2007-04-09 10:42:37 -0400848 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400849 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400850 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -0400851
Chris Mason0b86a832008-03-24 15:01:56 -0400852fail_extent_root:
853 free_extent_buffer(extent_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -0400854fail_tree_root:
Chris Mason0b86a832008-03-24 15:01:56 -0400855 mutex_unlock(&fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -0400856 free_extent_buffer(tree_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -0400857fail_sb_buffer:
Chris Mason5f39d392007-10-15 16:14:19 -0400858 free_extent_buffer(fs_info->sb_buffer);
Chris Mason39279cc2007-06-12 06:35:45 -0400859fail_iput:
860 iput(fs_info->btree_inode);
861fail:
862 kfree(extent_root);
863 kfree(tree_root);
864 kfree(fs_info);
865 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -0500866}
867
Chris Masone089f052007-03-16 16:20:31 -0400868int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400869 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500870{
Chris Masone66f7092007-04-20 13:16:02 -0400871 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -0400872 struct extent_buffer *super = root->fs_info->sb_buffer;
873 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason21ad10c2008-01-09 09:23:21 -0500874 struct super_block *sb = root->fs_info->sb;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400875
Chris Mason21ad10c2008-01-09 09:23:21 -0500876 if (!btrfs_test_opt(root, NOBARRIER))
877 blkdev_issue_flush(sb->s_bdev, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500878 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
Chris Mason5f39d392007-10-15 16:14:19 -0400879 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
880 super->start, super->len);
Chris Mason21ad10c2008-01-09 09:23:21 -0500881 if (!btrfs_test_opt(root, NOBARRIER))
882 blkdev_issue_flush(sb->s_bdev, NULL);
Chris Mason5f39d392007-10-15 16:14:19 -0400883 return ret;
Chris Masoncfaa7292007-02-21 17:04:57 -0500884}
885
Chris Mason5eda7b52007-06-22 14:16:25 -0400886int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -0400887{
888 radix_tree_delete(&fs_info->fs_roots_radix,
889 (unsigned long)root->root_key.objectid);
Chris Masonb99aa6c2008-01-14 14:41:16 -0500890 if (root->in_sysfs)
891 btrfs_sysfs_del_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -0400892 if (root->inode)
893 iput(root->inode);
894 if (root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400895 free_extent_buffer(root->node);
Chris Mason2619ba12007-04-10 16:58:11 -0400896 if (root->commit_root)
Chris Mason5f39d392007-10-15 16:14:19 -0400897 free_extent_buffer(root->commit_root);
Josef Bacik58176a92007-08-29 15:47:34 -0400898 if (root->name)
899 kfree(root->name);
Chris Mason2619ba12007-04-10 16:58:11 -0400900 kfree(root);
901 return 0;
902}
903
Chris Mason35b7e472007-05-02 15:53:43 -0400904static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400905{
906 int ret;
907 struct btrfs_root *gang[8];
908 int i;
909
910 while(1) {
911 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
912 (void **)gang, 0,
913 ARRAY_SIZE(gang));
914 if (!ret)
915 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400916 for (i = 0; i < ret; i++)
Chris Mason5eda7b52007-06-22 14:16:25 -0400917 btrfs_free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400918 }
919 return 0;
920}
Chris Masonb4100d62007-04-12 12:14:00 -0400921
Chris Masone20d96d2007-03-22 12:13:20 -0400922int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500923{
Chris Mason3768f362007-03-13 16:47:54 -0400924 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400925 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400926 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -0400927
Chris Masonfacda1e2007-06-08 18:11:48 -0400928 fs_info->closing = 1;
Chris Mason08607c12007-06-08 15:33:54 -0400929 btrfs_transaction_flush_work(root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400930 mutex_lock(&fs_info->fs_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -0400931 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason79154b12007-03-22 15:59:16 -0400932 trans = btrfs_start_transaction(root, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400933 ret = btrfs_commit_transaction(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400934 /* run commit again to drop the original snapshot */
935 trans = btrfs_start_transaction(root, 1);
936 btrfs_commit_transaction(trans, root);
937 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400938 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400939 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400940 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500941
Chris Masonb0c68f82008-01-31 11:05:37 -0500942 if (fs_info->delalloc_bytes) {
943 printk("btrfs: at unmount delalloc count %Lu\n",
944 fs_info->delalloc_bytes);
945 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400946 if (fs_info->extent_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400947 free_extent_buffer(fs_info->extent_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -0400948
Chris Mason0f7d52f2007-04-09 10:42:37 -0400949 if (fs_info->tree_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400950 free_extent_buffer(fs_info->tree_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -0400951
Chris Mason0b86a832008-03-24 15:01:56 -0400952 if (root->fs_info->chunk_root->node);
953 free_extent_buffer(root->fs_info->chunk_root->node);
954
955 if (root->fs_info->dev_root->node);
956 free_extent_buffer(root->fs_info->dev_root->node);
957
Chris Mason5f39d392007-10-15 16:14:19 -0400958 free_extent_buffer(fs_info->sb_buffer);
Chris Mason7eccb902007-04-11 15:53:25 -0400959
Chris Mason9078a3e2007-04-26 16:46:15 -0400960 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400961 del_fs_roots(fs_info);
Chris Masond10c5f32007-12-17 20:14:04 -0500962
963 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
964
Chris Masond1310b22008-01-24 16:13:08 -0500965 extent_io_tree_empty_lru(&fs_info->free_space_cache);
966 extent_io_tree_empty_lru(&fs_info->block_group_cache);
967 extent_io_tree_empty_lru(&fs_info->pinned_extents);
968 extent_io_tree_empty_lru(&fs_info->pending_del);
969 extent_io_tree_empty_lru(&fs_info->extent_ins);
970 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
Chris Masond10c5f32007-12-17 20:14:04 -0500971
Chris Masondb945352007-10-15 16:15:53 -0400972 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
Chris Masond10c5f32007-12-17 20:14:04 -0500973
Chris Masondb945352007-10-15 16:15:53 -0400974 iput(fs_info->btree_inode);
Chris Mason19c00dd2007-10-15 16:19:22 -0400975#if 0
976 while(!list_empty(&fs_info->hashers)) {
977 struct btrfs_hasher *hasher;
978 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
979 hashers);
980 list_del(&hasher->hashers);
981 crypto_free_hash(&fs_info->hash_tfm);
982 kfree(hasher);
983 }
984#endif
Chris Mason0b86a832008-03-24 15:01:56 -0400985 close_all_devices(fs_info);
986 btrfs_mapping_tree_free(&fs_info->mapping_tree);
987
Chris Mason0f7d52f2007-04-09 10:42:37 -0400988 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400989 kfree(fs_info->tree_root);
Chris Mason0b86a832008-03-24 15:01:56 -0400990 kfree(fs_info->chunk_root);
991 kfree(fs_info->dev_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500992 return 0;
993}
994
Chris Mason5f39d392007-10-15 16:14:19 -0400995int btrfs_buffer_uptodate(struct extent_buffer *buf)
Chris Masonccd467d2007-06-28 15:57:36 -0400996{
Chris Mason810191f2007-10-15 16:18:55 -0400997 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500998 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400999}
Chris Mason6702ed42007-08-07 16:15:09 -04001000
Chris Mason5f39d392007-10-15 16:14:19 -04001001int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
1002{
Chris Mason810191f2007-10-15 16:18:55 -04001003 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001004 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -04001005 buf);
1006}
1007
1008void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
1009{
Chris Mason810191f2007-10-15 16:18:55 -04001010 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04001011 u64 transid = btrfs_header_generation(buf);
1012 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason6702ed42007-08-07 16:15:09 -04001013
Chris Masonccd467d2007-06-28 15:57:36 -04001014 if (transid != root->fs_info->generation) {
1015 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
Chris Masondb945352007-10-15 16:15:53 -04001016 (unsigned long long)buf->start,
Chris Masonccd467d2007-06-28 15:57:36 -04001017 transid, root->fs_info->generation);
1018 WARN_ON(1);
1019 }
Chris Masond1310b22008-01-24 16:13:08 -05001020 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001021}
1022
Chris Masone2008b62008-01-08 15:46:30 -05001023void btrfs_throttle(struct btrfs_root *root)
1024{
Chris Mason55c69072008-01-09 15:55:33 -05001025 struct backing_dev_info *bdi;
1026
1027 bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
Chris Mason04005cc2008-01-17 12:01:41 -05001028 if (root->fs_info->throttles && bdi_write_congested(bdi)) {
1029#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
Chris Mason55c69072008-01-09 15:55:33 -05001030 congestion_wait(WRITE, HZ/20);
Chris Mason04005cc2008-01-17 12:01:41 -05001031#else
1032 blk_congestion_wait(WRITE, HZ/20);
1033#endif
1034 }
Chris Masone2008b62008-01-08 15:46:30 -05001035}
1036
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001037void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
Chris Mason35b7e472007-05-02 15:53:43 -04001038{
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001039 balance_dirty_pages_ratelimited_nr(
Chris Masond7fc6402008-02-18 12:12:38 -05001040 root->fs_info->btree_inode->i_mapping, 1);
Chris Mason35b7e472007-05-02 15:53:43 -04001041}
Chris Mason6b800532007-10-15 16:17:34 -04001042
1043void btrfs_set_buffer_defrag(struct extent_buffer *buf)
1044{
Chris Mason810191f2007-10-15 16:18:55 -04001045 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001046 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001047 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
Chris Mason6b800532007-10-15 16:17:34 -04001048 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
1049}
1050
1051void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
1052{
Chris Mason810191f2007-10-15 16:18:55 -04001053 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001054 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001055 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
Chris Mason6b800532007-10-15 16:17:34 -04001056 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
1057 GFP_NOFS);
1058}
1059
1060int btrfs_buffer_defrag(struct extent_buffer *buf)
1061{
Chris Mason810191f2007-10-15 16:18:55 -04001062 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001063 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001064 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001065 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
1066}
1067
1068int btrfs_buffer_defrag_done(struct extent_buffer *buf)
1069{
Chris Mason810191f2007-10-15 16:18:55 -04001070 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001071 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001072 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001073 buf->start, buf->start + buf->len - 1,
1074 EXTENT_DEFRAG_DONE, 0);
1075}
1076
1077int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
1078{
Chris Mason810191f2007-10-15 16:18:55 -04001079 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001080 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001081 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001082 buf->start, buf->start + buf->len - 1,
1083 EXTENT_DEFRAG_DONE, GFP_NOFS);
1084}
1085
1086int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
1087{
Chris Mason810191f2007-10-15 16:18:55 -04001088 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001089 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001090 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001091 buf->start, buf->start + buf->len - 1,
1092 EXTENT_DEFRAG, GFP_NOFS);
1093}
1094
1095int btrfs_read_buffer(struct extent_buffer *buf)
1096{
Chris Mason810191f2007-10-15 16:18:55 -04001097 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001098 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001099 return read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
Chris Masona86c12c2008-02-07 10:50:54 -05001100 buf, 0, 1, btree_get_extent);
Chris Mason6b800532007-10-15 16:17:34 -04001101}
Chris Mason0da54682007-11-07 21:08:01 -05001102
Chris Masond1310b22008-01-24 16:13:08 -05001103static struct extent_io_ops btree_extent_io_ops = {
Chris Mason0da54682007-11-07 21:08:01 -05001104 .writepage_io_hook = btree_writepage_io_hook,
Chris Mason0b86a832008-03-24 15:01:56 -04001105 .submit_bio_hook = btree_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04001106 /* note we're sharing with inode.c for the merge bio hook */
1107 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason0da54682007-11-07 21:08:01 -05001108};