blob: 82109204788c67ac5809e9be916198755a58ff39 [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 Masonce9adaa2008-04-09 16:28:12 -040027#include <linux/workqueue.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050028#include "ctree.h"
29#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -040030#include "transaction.h"
Chris Mason0f7d52f2007-04-09 10:42:37 -040031#include "btrfs_inode.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "volumes.h"
Chris Masondb945352007-10-15 16:15:53 -040033#include "print-tree.h"
Chris Masoneb60cea2007-02-02 09:18:22 -050034
Chris Mason5f39d392007-10-15 16:14:19 -040035#if 0
36static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
Chris Mason7eccb902007-04-11 15:53:25 -040037{
Chris Mason5f39d392007-10-15 16:14:19 -040038 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
39 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
40 (unsigned long long)extent_buffer_blocknr(buf),
41 (unsigned long long)btrfs_header_blocknr(buf));
Chris Mason39279cc2007-06-12 06:35:45 -040042 return 1;
Chris Masond98237b2007-03-28 13:57:48 -040043 }
Chris Mason9a8dd152007-02-23 08:38:36 -050044 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050045}
Chris Mason5f39d392007-10-15 16:14:19 -040046#endif
Chris Masoneb60cea2007-02-02 09:18:22 -050047
Chris Masond1310b22008-01-24 16:13:08 -050048static struct extent_io_ops btree_extent_io_ops;
Chris Masonce9adaa2008-04-09 16:28:12 -040049static struct workqueue_struct *end_io_workqueue;
50
51struct end_io_wq {
52 struct bio *bio;
53 bio_end_io_t *end_io;
54 void *private;
55 struct btrfs_fs_info *info;
56 int error;
57 struct list_head list;
58};
Chris Mason0da54682007-11-07 21:08:01 -050059
Chris Mason5f39d392007-10-15 16:14:19 -040060struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
Chris Masond1310b22008-01-24 16:13:08 -050061 size_t page_offset, u64 start, u64 len,
Chris Mason5f39d392007-10-15 16:14:19 -040062 int create)
63{
64 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
65 struct extent_map *em;
66 int ret;
Chris Masond98237b2007-03-28 13:57:48 -040067
Chris Mason5f39d392007-10-15 16:14:19 -040068again:
Chris Masond1310b22008-01-24 16:13:08 -050069 spin_lock(&em_tree->lock);
70 em = lookup_extent_mapping(em_tree, start, len);
71 spin_unlock(&em_tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -040072 if (em) {
73 goto out;
74 }
75 em = alloc_extent_map(GFP_NOFS);
76 if (!em) {
77 em = ERR_PTR(-ENOMEM);
78 goto out;
79 }
80 em->start = 0;
Chris Masond1310b22008-01-24 16:13:08 -050081 em->len = i_size_read(inode);
Chris Mason5f39d392007-10-15 16:14:19 -040082 em->block_start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -040083 em->bdev = inode->i_sb->s_bdev;
Chris Masond1310b22008-01-24 16:13:08 -050084
85 spin_lock(&em_tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -040086 ret = add_extent_mapping(em_tree, em);
Chris Masond1310b22008-01-24 16:13:08 -050087 spin_unlock(&em_tree->lock);
88
Chris Mason5f39d392007-10-15 16:14:19 -040089 if (ret == -EEXIST) {
90 free_extent_map(em);
91 em = NULL;
92 goto again;
93 } else if (ret) {
94 em = ERR_PTR(ret);
95 }
96out:
97 return em;
98}
99
Chris Mason19c00dd2007-10-15 16:19:22 -0400100u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
101{
102 return crc32c(seed, data, len);
103}
104
105void btrfs_csum_final(u32 crc, char *result)
106{
107 *(__le32 *)result = ~cpu_to_le32(crc);
108}
109
110static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
111 int verify)
112{
113 char result[BTRFS_CRC32_SIZE];
114 unsigned long len;
115 unsigned long cur_len;
116 unsigned long offset = BTRFS_CSUM_SIZE;
117 char *map_token = NULL;
118 char *kaddr;
119 unsigned long map_start;
120 unsigned long map_len;
121 int err;
122 u32 crc = ~(u32)0;
123
124 len = buf->len - offset;
125 while(len > 0) {
126 err = map_private_extent_buffer(buf, offset, 32,
127 &map_token, &kaddr,
128 &map_start, &map_len, KM_USER0);
129 if (err) {
130 printk("failed to map extent buffer! %lu\n",
131 offset);
132 return 1;
133 }
134 cur_len = min(len, map_len - (offset - map_start));
135 crc = btrfs_csum_data(root, kaddr + offset - map_start,
136 crc, cur_len);
137 len -= cur_len;
138 offset += cur_len;
139 unmap_extent_buffer(buf, map_token, KM_USER0);
140 }
141 btrfs_csum_final(crc, result);
142
143 if (verify) {
Chris Masone4204de2008-01-08 15:46:27 -0500144 int from_this_trans = 0;
145
146 if (root->fs_info->running_transaction &&
147 btrfs_header_generation(buf) ==
148 root->fs_info->running_transaction->transid)
149 from_this_trans = 1;
150
151 /* FIXME, this is not good */
Chris Mason63b10fc2008-04-01 11:21:32 -0400152 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
Chris Masone4204de2008-01-08 15:46:27 -0500153 u32 val;
154 u32 found = 0;
155 memcpy(&found, result, BTRFS_CRC32_SIZE);
156
157 read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
Chris Mason63b10fc2008-04-01 11:21:32 -0400158 WARN_ON(1);
Chris Masone4204de2008-01-08 15:46:27 -0500159 printk("btrfs: %s checksum verify failed on %llu "
Chris Mason63b10fc2008-04-01 11:21:32 -0400160 "wanted %X found %X from_this_trans %d "
161 "level %d\n",
Chris Mason19c00dd2007-10-15 16:19:22 -0400162 root->fs_info->sb->s_id,
Chris Mason63b10fc2008-04-01 11:21:32 -0400163 buf->start, val, found, from_this_trans,
164 btrfs_header_level(buf));
Chris Mason19c00dd2007-10-15 16:19:22 -0400165 return 1;
166 }
167 } else {
168 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
169 }
170 return 0;
171}
172
173
174int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
175{
Chris Masond1310b22008-01-24 16:13:08 -0500176 struct extent_io_tree *tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400177 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason19c00dd2007-10-15 16:19:22 -0400178 u64 found_start;
179 int found_level;
180 unsigned long len;
181 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -0500182 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason19c00dd2007-10-15 16:19:22 -0400183
184 if (page->private == EXTENT_PAGE_PRIVATE)
185 goto out;
186 if (!page->private)
187 goto out;
188 len = page->private >> 2;
189 if (len == 0) {
190 WARN_ON(1);
191 }
192 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
Chris Masona86c12c2008-02-07 10:50:54 -0500193 read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1,
194 btree_get_extent);
Chris Masone18e4802008-01-18 10:54:22 -0500195 btrfs_clear_buffer_defrag(eb);
Chris Mason19c00dd2007-10-15 16:19:22 -0400196 found_start = btrfs_header_bytenr(eb);
197 if (found_start != start) {
198 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
199 start, found_start, len);
Chris Mason55c69072008-01-09 15:55:33 -0500200 WARN_ON(1);
201 goto err;
202 }
203 if (eb->first_page != page) {
204 printk("bad first page %lu %lu\n", eb->first_page->index,
205 page->index);
206 WARN_ON(1);
207 goto err;
208 }
209 if (!PageUptodate(page)) {
210 printk("csum not up to date page %lu\n", page->index);
211 WARN_ON(1);
212 goto err;
Chris Mason19c00dd2007-10-15 16:19:22 -0400213 }
214 found_level = btrfs_header_level(eb);
Chris Mason63b10fc2008-04-01 11:21:32 -0400215 spin_lock(&root->fs_info->hash_lock);
216 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
217 spin_unlock(&root->fs_info->hash_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -0400218 csum_tree_block(root, eb, 0);
Chris Mason55c69072008-01-09 15:55:33 -0500219err:
Chris Mason19c00dd2007-10-15 16:19:22 -0400220 free_extent_buffer(eb);
221out:
222 return 0;
223}
224
Chris Mason0da54682007-11-07 21:08:01 -0500225static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
226{
227 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
228
229 csum_dirty_buffer(root, page);
230 return 0;
231}
232
Chris Masonce9adaa2008-04-09 16:28:12 -0400233int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
234 struct extent_state *state)
235{
236 struct extent_io_tree *tree;
237 u64 found_start;
238 int found_level;
239 unsigned long len;
240 struct extent_buffer *eb;
241 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
242 int ret;
243
244 tree = &BTRFS_I(page->mapping->host)->io_tree;
245 if (page->private == EXTENT_PAGE_PRIVATE)
246 goto out;
247 if (!page->private)
248 goto out;
249 len = page->private >> 2;
250 if (len == 0) {
251 WARN_ON(1);
252 }
253 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
254 read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1,
255 btree_get_extent);
256 btrfs_clear_buffer_defrag(eb);
257 found_start = btrfs_header_bytenr(eb);
258 if (found_start != start) {
259 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
260 start, found_start, len);
261 WARN_ON(1);
262 goto err;
263 }
264 if (eb->first_page != page) {
265 printk("bad first page %lu %lu\n", eb->first_page->index,
266 page->index);
267 WARN_ON(1);
268 goto err;
269 }
270 found_level = btrfs_header_level(eb);
271
272 ret = csum_tree_block(root, eb, 1);
273
274 end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
275 end = eb->start + end - 1;
276 release_extent_buffer_tail_pages(eb);
277err:
278 free_extent_buffer(eb);
279out:
280 return 0;
281}
282
283#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
284static void end_workqueue_bio(struct bio *bio, int err)
285#else
286static int end_workqueue_bio(struct bio *bio,
287 unsigned int bytes_done, int err)
288#endif
289{
290 struct end_io_wq *end_io_wq = bio->bi_private;
291 struct btrfs_fs_info *fs_info;
292 unsigned long flags;
293
294#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
295 if (bio->bi_size)
296 return 1;
297#endif
298
299 fs_info = end_io_wq->info;
300 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
301 end_io_wq->error = err;
302 list_add_tail(&end_io_wq->list, &fs_info->end_io_work_list);
303 spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
304 queue_work(end_io_workqueue, &fs_info->end_io_work);
305
306#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
307 return 0;
308#endif
309}
310
Chris Mason0b86a832008-03-24 15:01:56 -0400311static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio)
312{
313 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonce9adaa2008-04-09 16:28:12 -0400314 struct end_io_wq *end_io_wq;
Chris Mason0b86a832008-03-24 15:01:56 -0400315 u64 offset;
316 offset = bio->bi_sector << 9;
Chris Masonce9adaa2008-04-09 16:28:12 -0400317
318 if (rw & (1 << BIO_RW)) {
319 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
320 }
321
322 end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
323 if (!end_io_wq)
324 return -ENOMEM;
325
326 end_io_wq->private = bio->bi_private;
327 end_io_wq->end_io = bio->bi_end_io;
328 end_io_wq->info = root->fs_info;
329 end_io_wq->error = 0;
330 end_io_wq->bio = bio;
331
332 bio->bi_private = end_io_wq;
333 bio->bi_end_io = end_workqueue_bio;
334
Chris Mason0b86a832008-03-24 15:01:56 -0400335 if (offset == BTRFS_SUPER_INFO_OFFSET) {
336 bio->bi_bdev = root->fs_info->sb->s_bdev;
337 submit_bio(rw, bio);
338 return 0;
339 }
340 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
341}
342
Chris Mason5f39d392007-10-15 16:14:19 -0400343static int btree_writepage(struct page *page, struct writeback_control *wbc)
344{
Chris Masond1310b22008-01-24 16:13:08 -0500345 struct extent_io_tree *tree;
346 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400347 return extent_write_full_page(tree, page, btree_get_extent, wbc);
348}
Chris Mason0da54682007-11-07 21:08:01 -0500349
350static int btree_writepages(struct address_space *mapping,
351 struct writeback_control *wbc)
352{
Chris Masond1310b22008-01-24 16:13:08 -0500353 struct extent_io_tree *tree;
354 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500355 if (wbc->sync_mode == WB_SYNC_NONE) {
Chris Mason793955b2007-11-26 16:34:41 -0800356 u64 num_dirty;
357 u64 start = 0;
358 unsigned long thresh = 96 * 1024 * 1024;
Chris Mason448d6402007-11-27 07:52:01 -0800359
360 if (wbc->for_kupdate)
361 return 0;
362
Chris Masonca664622007-11-27 11:16:35 -0500363 if (current_is_pdflush()) {
364 thresh = 96 * 1024 * 1024;
365 } else {
366 thresh = 8 * 1024 * 1024;
367 }
Chris Mason1832a6d2007-12-21 16:27:21 -0500368 num_dirty = count_range_bits(tree, &start, (u64)-1,
369 thresh, EXTENT_DIRTY);
Chris Mason793955b2007-11-26 16:34:41 -0800370 if (num_dirty < thresh) {
371 return 0;
372 }
373 }
Chris Mason0da54682007-11-07 21:08:01 -0500374 return extent_writepages(tree, mapping, btree_get_extent, wbc);
375}
376
Chris Mason5f39d392007-10-15 16:14:19 -0400377int btree_readpage(struct file *file, struct page *page)
378{
Chris Masond1310b22008-01-24 16:13:08 -0500379 struct extent_io_tree *tree;
380 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400381 return extent_read_full_page(tree, page, btree_get_extent);
382}
383
Chris Mason70dec802008-01-29 09:59:12 -0500384static int btree_releasepage(struct page *page, gfp_t gfp_flags)
Chris Mason5f39d392007-10-15 16:14:19 -0400385{
Chris Masond1310b22008-01-24 16:13:08 -0500386 struct extent_io_tree *tree;
387 struct extent_map_tree *map;
Chris Mason5f39d392007-10-15 16:14:19 -0400388 int ret;
389
Chris Masond1310b22008-01-24 16:13:08 -0500390 tree = &BTRFS_I(page->mapping->host)->io_tree;
391 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -0500392 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400393 if (ret == 1) {
Chris Mason728131d2008-04-09 16:28:12 -0400394 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -0400395 ClearPagePrivate(page);
396 set_page_private(page, 0);
397 page_cache_release(page);
398 }
Chris Masond98237b2007-03-28 13:57:48 -0400399 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400400}
Chris Mason123abc82007-03-14 14:14:43 -0400401
Chris Mason5f39d392007-10-15 16:14:19 -0400402static void btree_invalidatepage(struct page *page, unsigned long offset)
Chris Masond98237b2007-03-28 13:57:48 -0400403{
Chris Masond1310b22008-01-24 16:13:08 -0500404 struct extent_io_tree *tree;
405 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400406 extent_invalidatepage(tree, page, offset);
407 btree_releasepage(page, GFP_NOFS);
Chris Masond98237b2007-03-28 13:57:48 -0400408}
409
Chris Mason5f39d392007-10-15 16:14:19 -0400410#if 0
Chris Masond98237b2007-03-28 13:57:48 -0400411static int btree_writepage(struct page *page, struct writeback_control *wbc)
412{
Chris Mason87cbda52007-03-28 19:44:27 -0400413 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400414 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400415 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400416 if (!page_has_buffers(page)) {
417 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
418 (1 << BH_Dirty)|(1 << BH_Uptodate));
419 }
420 head = page_buffers(page);
421 bh = head;
422 do {
423 if (buffer_dirty(bh))
424 csum_tree_block(root, bh, 0);
425 bh = bh->b_this_page;
426 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400427 return block_write_full_page(page, btree_get_block, wbc);
428}
Chris Mason5f39d392007-10-15 16:14:19 -0400429#endif
Chris Masond98237b2007-03-28 13:57:48 -0400430
431static struct address_space_operations btree_aops = {
432 .readpage = btree_readpage,
433 .writepage = btree_writepage,
Chris Mason0da54682007-11-07 21:08:01 -0500434 .writepages = btree_writepages,
Chris Mason5f39d392007-10-15 16:14:19 -0400435 .releasepage = btree_releasepage,
436 .invalidatepage = btree_invalidatepage,
Chris Masond98237b2007-03-28 13:57:48 -0400437 .sync_page = block_sync_page,
438};
439
Chris Masondb945352007-10-15 16:15:53 -0400440int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
Chris Mason090d1872007-05-01 08:53:32 -0400441{
Chris Mason5f39d392007-10-15 16:14:19 -0400442 struct extent_buffer *buf = NULL;
443 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonde428b62007-05-18 13:28:27 -0400444 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400445
Chris Masondb945352007-10-15 16:15:53 -0400446 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400447 if (!buf)
Chris Mason090d1872007-05-01 08:53:32 -0400448 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500449 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
Chris Masona86c12c2008-02-07 10:50:54 -0500450 buf, 0, 0, btree_get_extent);
Chris Mason5f39d392007-10-15 16:14:19 -0400451 free_extent_buffer(buf);
Chris Masonde428b62007-05-18 13:28:27 -0400452 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400453}
454
Chris Mason0b86a832008-03-24 15:01:56 -0400455static int close_all_devices(struct btrfs_fs_info *fs_info)
456{
457 struct list_head *list;
458 struct list_head *next;
459 struct btrfs_device *device;
460
Chris Mason8a4b83c2008-03-24 15:02:07 -0400461 list = &fs_info->fs_devices->devices;
462 list_for_each(next, list) {
Chris Mason0b86a832008-03-24 15:01:56 -0400463 device = list_entry(next, struct btrfs_device, dev_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400464 if (device->bdev && device->bdev != fs_info->sb->s_bdev)
465 close_bdev_excl(device->bdev);
466 device->bdev = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400467 }
468 return 0;
469}
470
Chris Mason0999df52008-04-01 13:48:14 -0400471int btrfs_verify_block_csum(struct btrfs_root *root,
472 struct extent_buffer *buf)
473{
Chris Masonce9adaa2008-04-09 16:28:12 -0400474 return btrfs_buffer_uptodate(buf);
Chris Mason0999df52008-04-01 13:48:14 -0400475}
476
477struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
478 u64 bytenr, u32 blocksize)
479{
480 struct inode *btree_inode = root->fs_info->btree_inode;
481 struct extent_buffer *eb;
482 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
483 bytenr, blocksize, GFP_NOFS);
484 return eb;
485}
486
487struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
488 u64 bytenr, u32 blocksize)
489{
490 struct inode *btree_inode = root->fs_info->btree_inode;
491 struct extent_buffer *eb;
492
493 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
494 bytenr, blocksize, NULL, GFP_NOFS);
495 return eb;
496}
497
498
Chris Masondb945352007-10-15 16:15:53 -0400499struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
500 u32 blocksize)
Chris Masone20d96d2007-03-22 12:13:20 -0400501{
Chris Mason5f39d392007-10-15 16:14:19 -0400502 struct extent_buffer *buf = NULL;
503 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -0500504 struct extent_io_tree *io_tree;
Chris Mason19c00dd2007-10-15 16:19:22 -0400505 int ret;
506
Chris Masond1310b22008-01-24 16:13:08 -0500507 io_tree = &BTRFS_I(btree_inode)->io_tree;
Chris Masone20d96d2007-03-22 12:13:20 -0400508
Chris Masondb945352007-10-15 16:15:53 -0400509 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400510 if (!buf)
511 return NULL;
Chris Masone4204de2008-01-08 15:46:27 -0500512
Chris Masonce9adaa2008-04-09 16:28:12 -0400513 ret = read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 0,
514 1, btree_get_extent);
515
516 if (ret == 0) {
517 buf->flags |= EXTENT_UPTODATE;
518 }
Chris Mason5f39d392007-10-15 16:14:19 -0400519 return buf;
Chris Masonce9adaa2008-04-09 16:28:12 -0400520
Chris Masoneb60cea2007-02-02 09:18:22 -0500521}
522
Chris Masone089f052007-03-16 16:20:31 -0400523int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400524 struct extent_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500525{
Chris Mason5f39d392007-10-15 16:14:19 -0400526 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason55c69072008-01-09 15:55:33 -0500527 if (btrfs_header_generation(buf) ==
528 root->fs_info->running_transaction->transid)
Chris Masond1310b22008-01-24 16:13:08 -0500529 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
Chris Mason55c69072008-01-09 15:55:33 -0500530 buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400531 return 0;
532}
533
534int wait_on_tree_block_writeback(struct btrfs_root *root,
535 struct extent_buffer *buf)
536{
537 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -0500538 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -0400539 buf);
540 return 0;
541}
542
Chris Masondb945352007-10-15 16:15:53 -0400543static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500544 u32 stripesize, struct btrfs_root *root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400545 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400546 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500547{
Chris Masoncfaa7292007-02-21 17:04:57 -0500548 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400549 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500550 root->commit_root = NULL;
Chris Masondb945352007-10-15 16:15:53 -0400551 root->sectorsize = sectorsize;
552 root->nodesize = nodesize;
553 root->leafsize = leafsize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500554 root->stripesize = stripesize;
Chris Mason123abc82007-03-14 14:14:43 -0400555 root->ref_cows = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400556 root->track_dirty = 0;
557
Chris Mason9f5fae22007-03-20 14:38:32 -0400558 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400559 root->objectid = objectid;
560 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400561 root->highest_inode = 0;
562 root->last_inode_alloc = 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400563 root->name = NULL;
Chris Mason4313b392008-01-03 09:08:48 -0500564 root->in_sysfs = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400565
566 INIT_LIST_HEAD(&root->dirty_list);
Chris Mason3768f362007-03-13 16:47:54 -0400567 memset(&root->root_key, 0, sizeof(root->root_key));
568 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -0400569 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Josef Bacik58176a92007-08-29 15:47:34 -0400570 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
571 init_completion(&root->kobj_unregister);
Chris Mason6702ed42007-08-07 16:15:09 -0400572 root->defrag_running = 0;
573 root->defrag_level = 0;
Chris Mason4d775672007-04-20 20:23:12 -0400574 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400575 return 0;
576}
577
Chris Masondb945352007-10-15 16:15:53 -0400578static int find_and_setup_root(struct btrfs_root *tree_root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400579 struct btrfs_fs_info *fs_info,
580 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400581 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400582{
583 int ret;
Chris Masondb945352007-10-15 16:15:53 -0400584 u32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -0400585
Chris Masondb945352007-10-15 16:15:53 -0400586 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500587 tree_root->sectorsize, tree_root->stripesize,
588 root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400589 ret = btrfs_find_last_root(tree_root, objectid,
590 &root->root_item, &root->root_key);
591 BUG_ON(ret);
592
Chris Masondb945352007-10-15 16:15:53 -0400593 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
594 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
595 blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400596 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500597 return 0;
598}
599
Chris Mason5eda7b52007-06-22 14:16:25 -0400600struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
601 struct btrfs_key *location)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400602{
603 struct btrfs_root *root;
604 struct btrfs_root *tree_root = fs_info->tree_root;
605 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400606 struct extent_buffer *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400607 u64 highest_inode;
Chris Masondb945352007-10-15 16:15:53 -0400608 u32 blocksize;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400609 int ret = 0;
610
Chris Mason5eda7b52007-06-22 14:16:25 -0400611 root = kzalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400612 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400613 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400614 if (location->offset == (u64)-1) {
Chris Masondb945352007-10-15 16:15:53 -0400615 ret = find_and_setup_root(tree_root, fs_info,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400616 location->objectid, root);
617 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400618 kfree(root);
619 return ERR_PTR(ret);
620 }
621 goto insert;
622 }
623
Chris Masondb945352007-10-15 16:15:53 -0400624 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500625 tree_root->sectorsize, tree_root->stripesize,
626 root, fs_info, location->objectid);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400627
628 path = btrfs_alloc_path();
629 BUG_ON(!path);
630 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
631 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400632 if (ret > 0)
633 ret = -ENOENT;
634 goto out;
635 }
Chris Mason5f39d392007-10-15 16:14:19 -0400636 l = path->nodes[0];
637 read_extent_buffer(l, &root->root_item,
638 btrfs_item_ptr_offset(l, path->slots[0]),
Chris Mason0f7d52f2007-04-09 10:42:37 -0400639 sizeof(root->root_item));
Yan Zheng44b36eb2007-10-19 09:22:56 -0400640 memcpy(&root->root_key, location, sizeof(*location));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400641 ret = 0;
642out:
643 btrfs_release_path(root, path);
644 btrfs_free_path(path);
645 if (ret) {
646 kfree(root);
647 return ERR_PTR(ret);
648 }
Chris Masondb945352007-10-15 16:15:53 -0400649 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
650 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
651 blocksize);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400652 BUG_ON(!root->node);
653insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400654 root->ref_cows = 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400655 ret = btrfs_find_highest_inode(root, &highest_inode);
656 if (ret == 0) {
657 root->highest_inode = highest_inode;
658 root->last_inode_alloc = highest_inode;
659 }
660 return root;
661}
662
Chris Masondc17ff82008-01-08 15:46:30 -0500663struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
664 u64 root_objectid)
665{
666 struct btrfs_root *root;
667
668 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
669 return fs_info->tree_root;
670 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
671 return fs_info->extent_root;
672
673 root = radix_tree_lookup(&fs_info->fs_roots_radix,
674 (unsigned long)root_objectid);
675 return root;
676}
677
Chris Masonedbd8d42007-12-21 16:27:24 -0500678struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
679 struct btrfs_key *location)
Chris Mason5eda7b52007-06-22 14:16:25 -0400680{
681 struct btrfs_root *root;
682 int ret;
683
Chris Masonedbd8d42007-12-21 16:27:24 -0500684 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
685 return fs_info->tree_root;
686 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
687 return fs_info->extent_root;
688
Chris Mason5eda7b52007-06-22 14:16:25 -0400689 root = radix_tree_lookup(&fs_info->fs_roots_radix,
690 (unsigned long)location->objectid);
691 if (root)
692 return root;
693
694 root = btrfs_read_fs_root_no_radix(fs_info, location);
695 if (IS_ERR(root))
696 return root;
Chris Mason2619ba12007-04-10 16:58:11 -0400697 ret = radix_tree_insert(&fs_info->fs_roots_radix,
698 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400699 root);
700 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400701 free_extent_buffer(root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400702 kfree(root);
703 return ERR_PTR(ret);
704 }
Chris Masonedbd8d42007-12-21 16:27:24 -0500705 ret = btrfs_find_dead_roots(fs_info->tree_root,
706 root->root_key.objectid, root);
707 BUG_ON(ret);
708
709 return root;
710}
711
712struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
713 struct btrfs_key *location,
714 const char *name, int namelen)
715{
716 struct btrfs_root *root;
717 int ret;
718
719 root = btrfs_read_fs_root_no_name(fs_info, location);
720 if (!root)
721 return NULL;
Josef Bacik58176a92007-08-29 15:47:34 -0400722
Chris Mason4313b392008-01-03 09:08:48 -0500723 if (root->in_sysfs)
724 return root;
725
Josef Bacik58176a92007-08-29 15:47:34 -0400726 ret = btrfs_set_root_name(root, name, namelen);
727 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400728 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400729 kfree(root);
730 return ERR_PTR(ret);
731 }
732
733 ret = btrfs_sysfs_add_root(root);
734 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400735 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400736 kfree(root->name);
737 kfree(root);
738 return ERR_PTR(ret);
739 }
Chris Mason4313b392008-01-03 09:08:48 -0500740 root->in_sysfs = 1;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400741 return root;
742}
Chris Mason19c00dd2007-10-15 16:19:22 -0400743#if 0
744static int add_hasher(struct btrfs_fs_info *info, char *type) {
745 struct btrfs_hasher *hasher;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400746
Chris Mason19c00dd2007-10-15 16:19:22 -0400747 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
748 if (!hasher)
749 return -ENOMEM;
750 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
751 if (!hasher->hash_tfm) {
752 kfree(hasher);
753 return -EINVAL;
754 }
755 spin_lock(&info->hash_lock);
756 list_add(&hasher->list, &info->hashers);
757 spin_unlock(&info->hash_lock);
758 return 0;
759}
760#endif
Chris Mason04160082008-03-26 10:28:07 -0400761
762static int btrfs_congested_fn(void *congested_data, int bdi_bits)
763{
764 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
765 int ret = 0;
766 struct list_head *cur;
767 struct btrfs_device *device;
768 struct backing_dev_info *bdi;
769
770 list_for_each(cur, &info->fs_devices->devices) {
771 device = list_entry(cur, struct btrfs_device, dev_list);
772 bdi = blk_get_backing_dev_info(device->bdev);
773 if (bdi && bdi_congested(bdi, bdi_bits)) {
774 ret = 1;
775 break;
776 }
777 }
778 return ret;
779}
780
781void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
782{
783 struct list_head *cur;
784 struct btrfs_device *device;
785 struct btrfs_fs_info *info;
786
787 info = (struct btrfs_fs_info *)bdi->unplug_io_data;
788 list_for_each(cur, &info->fs_devices->devices) {
789 device = list_entry(cur, struct btrfs_device, dev_list);
790 bdi = blk_get_backing_dev_info(device->bdev);
791 if (bdi->unplug_io_fn) {
792 bdi->unplug_io_fn(bdi, page);
793 }
794 }
795}
796
797static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
798{
799 bdi_init(bdi);
Chris Mason83041ad2008-03-26 12:02:55 -0400800 bdi->ra_pages = default_backing_dev_info.ra_pages * 4;
Chris Mason04160082008-03-26 10:28:07 -0400801 bdi->state = 0;
802 bdi->capabilities = default_backing_dev_info.capabilities;
803 bdi->unplug_io_fn = btrfs_unplug_io_fn;
804 bdi->unplug_io_data = info;
805 bdi->congested_fn = btrfs_congested_fn;
806 bdi->congested_data = info;
807 return 0;
808}
809
Chris Masonce9adaa2008-04-09 16:28:12 -0400810static int bio_ready_for_csum(struct bio *bio)
811{
812 u64 length = 0;
813 u64 buf_len = 0;
814 u64 start = 0;
815 struct page *page;
816 struct extent_io_tree *io_tree = NULL;
817 struct btrfs_fs_info *info = NULL;
818 struct bio_vec *bvec;
819 int i;
820 int ret;
821
822 bio_for_each_segment(bvec, bio, i) {
823 page = bvec->bv_page;
824 if (page->private == EXTENT_PAGE_PRIVATE) {
825 length += bvec->bv_len;
826 continue;
827 }
828 if (!page->private) {
829 length += bvec->bv_len;
830 continue;
831 }
832 length = bvec->bv_len;
833 buf_len = page->private >> 2;
834 start = page_offset(page) + bvec->bv_offset;
835 io_tree = &BTRFS_I(page->mapping->host)->io_tree;
836 info = BTRFS_I(page->mapping->host)->root->fs_info;
837 }
838 /* are we fully contained in this bio? */
839 if (buf_len <= length)
840 return 1;
841
842 ret = extent_range_uptodate(io_tree, start + length,
843 start + buf_len - 1);
844 if (ret == 1)
845 return ret;
846 return ret;
847}
848
849#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
850void btrfs_end_io_csum(void *p)
851#else
852void btrfs_end_io_csum(struct work_struct *work)
853#endif
854{
855#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
856 struct btrfs_fs_info *fs_info = p;
857#else
858 struct btrfs_fs_info *fs_info = container_of(work,
859 struct btrfs_fs_info,
860 end_io_work);
861#endif
862 unsigned long flags;
863 struct end_io_wq *end_io_wq;
864 struct bio *bio;
865 struct list_head *next;
866 int error;
867 int was_empty;
868
869 while(1) {
870 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
871 if (list_empty(&fs_info->end_io_work_list)) {
872 spin_unlock_irqrestore(&fs_info->end_io_work_lock,
873 flags);
874 return;
875 }
876 next = fs_info->end_io_work_list.next;
877 list_del(next);
878 spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
879
880 end_io_wq = list_entry(next, struct end_io_wq, list);
881
882 bio = end_io_wq->bio;
883 if (!bio_ready_for_csum(bio)) {
884 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
885 was_empty = list_empty(&fs_info->end_io_work_list);
886 list_add_tail(&end_io_wq->list,
887 &fs_info->end_io_work_list);
888 spin_unlock_irqrestore(&fs_info->end_io_work_lock,
889 flags);
890 if (was_empty)
891 return;
892 continue;
893 }
894 error = end_io_wq->error;
895 bio->bi_private = end_io_wq->private;
896 bio->bi_end_io = end_io_wq->end_io;
897 kfree(end_io_wq);
898 bio_endio(bio, error);
899 }
900}
901
902
Chris Mason8a4b83c2008-03-24 15:02:07 -0400903struct btrfs_root *open_ctree(struct super_block *sb,
904 struct btrfs_fs_devices *fs_devices)
Chris Masoneb60cea2007-02-02 09:18:22 -0500905{
Chris Masondb945352007-10-15 16:15:53 -0400906 u32 sectorsize;
907 u32 nodesize;
908 u32 leafsize;
909 u32 blocksize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500910 u32 stripesize;
Chris Masone20d96d2007-03-22 12:13:20 -0400911 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
912 GFP_NOFS);
913 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
914 GFP_NOFS);
Chris Mason8790d502008-04-03 16:29:03 -0400915 struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
Chris Masone20d96d2007-03-22 12:13:20 -0400916 GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -0400917 struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
918 GFP_NOFS);
919 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
920 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500921 int ret;
Yane58ca022008-04-01 11:21:34 -0400922 int err = -EINVAL;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400923 struct btrfs_super_block *disk_super;
Chris Mason8790d502008-04-03 16:29:03 -0400924
Chris Mason39279cc2007-06-12 06:35:45 -0400925 if (!extent_root || !tree_root || !fs_info) {
926 err = -ENOMEM;
927 goto fail;
928 }
Chris Masonce9adaa2008-04-09 16:28:12 -0400929 end_io_workqueue = create_workqueue("btrfs-end-io");
930 BUG_ON(!end_io_workqueue);
931
Chris Mason0f7d52f2007-04-09 10:42:37 -0400932 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason8fd17792007-04-19 21:01:03 -0400933 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -0400934 INIT_LIST_HEAD(&fs_info->dead_roots);
Chris Mason19c00dd2007-10-15 16:19:22 -0400935 INIT_LIST_HEAD(&fs_info->hashers);
Chris Masonce9adaa2008-04-09 16:28:12 -0400936 INIT_LIST_HEAD(&fs_info->end_io_work_list);
Chris Mason19c00dd2007-10-15 16:19:22 -0400937 spin_lock_init(&fs_info->hash_lock);
Chris Masonce9adaa2008-04-09 16:28:12 -0400938 spin_lock_init(&fs_info->end_io_work_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -0500939 spin_lock_init(&fs_info->delalloc_lock);
Chris Masoncee36a02008-01-15 08:40:48 -0500940 spin_lock_init(&fs_info->new_trans_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -0400941
Josef Bacik58176a92007-08-29 15:47:34 -0400942 init_completion(&fs_info->kobj_unregister);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400943 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400944 fs_info->tree_root = tree_root;
945 fs_info->extent_root = extent_root;
Chris Mason0b86a832008-03-24 15:01:56 -0400946 fs_info->chunk_root = chunk_root;
947 fs_info->dev_root = dev_root;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400948 fs_info->fs_devices = fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -0400949 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
Chris Mason6324fbf2008-03-24 15:01:59 -0400950 INIT_LIST_HEAD(&fs_info->space_info);
Chris Mason0b86a832008-03-24 15:01:56 -0400951 btrfs_mapping_init(&fs_info->mapping_tree);
Chris Masone20d96d2007-03-22 12:13:20 -0400952 fs_info->sb = sb;
Chris Masonc59f8952007-12-17 20:14:04 -0500953 fs_info->max_extent = (u64)-1;
Chris Mason6f568d32008-01-29 16:03:38 -0500954 fs_info->max_inline = 8192 * 1024;
Chris Mason04160082008-03-26 10:28:07 -0400955 setup_bdi(fs_info, &fs_info->bdi);
Chris Masond98237b2007-03-28 13:57:48 -0400956 fs_info->btree_inode = new_inode(sb);
957 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400958 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400959 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
960 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason04160082008-03-26 10:28:07 -0400961 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
962
Chris Masond1310b22008-01-24 16:13:08 -0500963 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -0400964 fs_info->btree_inode->i_mapping,
965 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500966 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
967 GFP_NOFS);
Chris Mason0da54682007-11-07 21:08:01 -0500968
Chris Masond1310b22008-01-24 16:13:08 -0500969 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
970
971 extent_io_tree_init(&fs_info->free_space_cache,
Chris Masonf510cfe2007-10-15 16:14:48 -0400972 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500973 extent_io_tree_init(&fs_info->block_group_cache,
Chris Mason96b51792007-10-15 16:15:19 -0400974 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500975 extent_io_tree_init(&fs_info->pinned_extents,
Chris Mason1a5bc162007-10-15 16:15:26 -0400976 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500977 extent_io_tree_init(&fs_info->pending_del,
Chris Mason1a5bc162007-10-15 16:15:26 -0400978 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500979 extent_io_tree_init(&fs_info->extent_ins,
Chris Mason1a5bc162007-10-15 16:15:26 -0400980 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masone66f7092007-04-20 13:16:02 -0400981 fs_info->do_barriers = 1;
Chris Masone18e4802008-01-18 10:54:22 -0500982
Chris Masonce9adaa2008-04-09 16:28:12 -0400983 INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum);
Chris Mason6da6aba2007-12-18 16:15:09 -0500984#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
985 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
986#else
Chris Mason08607c12007-06-08 15:33:54 -0400987 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason6da6aba2007-12-18 16:15:09 -0500988#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -0400989 BTRFS_I(fs_info->btree_inode)->root = tree_root;
990 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
991 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400992 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400993 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400994
Chris Mason79154b12007-03-22 15:59:16 -0400995 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400996 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400997
Chris Mason19c00dd2007-10-15 16:19:22 -0400998#if 0
999 ret = add_hasher(fs_info, "crc32c");
1000 if (ret) {
1001 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
1002 err = -ENOMEM;
1003 goto fail_iput;
1004 }
1005#endif
Chris Mason0b86a832008-03-24 15:01:56 -04001006 __setup_root(4096, 4096, 4096, 4096, tree_root,
Chris Mason2c90e5d2007-04-02 10:50:19 -04001007 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -04001008
Chris Mason2c90e5d2007-04-02 10:50:19 -04001009 fs_info->sb_buffer = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -04001010 BTRFS_SUPER_INFO_OFFSET,
Chris Mason0b86a832008-03-24 15:01:56 -04001011 4096);
Chris Masond98237b2007-03-28 13:57:48 -04001012
Chris Mason0f7d52f2007-04-09 10:42:37 -04001013 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -04001014 goto fail_iput;
Chris Mason39279cc2007-06-12 06:35:45 -04001015
Chris Mason5f39d392007-10-15 16:14:19 -04001016 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
1017 sizeof(fs_info->super_copy));
1018
1019 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
1020 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
1021 BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001022
Chris Mason5f39d392007-10-15 16:14:19 -04001023 disk_super = &fs_info->super_copy;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001024 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -04001025 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001026
Chris Mason8a4b83c2008-03-24 15:02:07 -04001027 if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
1028 printk("Btrfs: wanted %llu devices, but found %llu\n",
1029 (unsigned long long)btrfs_super_num_devices(disk_super),
1030 (unsigned long long)fs_devices->num_devices);
1031 goto fail_sb_buffer;
1032 }
Chris Masondb945352007-10-15 16:15:53 -04001033 nodesize = btrfs_super_nodesize(disk_super);
1034 leafsize = btrfs_super_leafsize(disk_super);
1035 sectorsize = btrfs_super_sectorsize(disk_super);
Chris Mason87ee04e2007-11-30 11:30:34 -05001036 stripesize = btrfs_super_stripesize(disk_super);
Chris Masondb945352007-10-15 16:15:53 -04001037 tree_root->nodesize = nodesize;
1038 tree_root->leafsize = leafsize;
1039 tree_root->sectorsize = sectorsize;
Chris Mason87ee04e2007-11-30 11:30:34 -05001040 tree_root->stripesize = stripesize;
Chris Masonff79f812007-10-15 16:22:25 -04001041 sb_set_blocksize(sb, sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04001042
Chris Mason8352d8a2007-04-12 10:43:05 -04001043 i_size_write(fs_info->btree_inode,
Chris Masondb945352007-10-15 16:15:53 -04001044 btrfs_super_total_bytes(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -04001045
Chris Mason39279cc2007-06-12 06:35:45 -04001046 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
1047 sizeof(disk_super->magic))) {
1048 printk("btrfs: valid FS not found on %s\n", sb->s_id);
1049 goto fail_sb_buffer;
1050 }
Chris Mason19c00dd2007-10-15 16:19:22 -04001051
Chris Mason0b86a832008-03-24 15:01:56 -04001052 mutex_lock(&fs_info->fs_mutex);
Chris Mason0d81ba52008-03-24 15:02:07 -04001053
Chris Mason0b86a832008-03-24 15:01:56 -04001054 ret = btrfs_read_sys_array(tree_root);
1055 BUG_ON(ret);
1056
1057 blocksize = btrfs_level_size(tree_root,
1058 btrfs_super_chunk_root_level(disk_super));
1059
1060 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1061 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1062
1063 chunk_root->node = read_tree_block(chunk_root,
1064 btrfs_super_chunk_root(disk_super),
1065 blocksize);
1066 BUG_ON(!chunk_root->node);
1067
1068 ret = btrfs_read_chunk_tree(chunk_root);
1069 BUG_ON(ret);
1070
Chris Masondb945352007-10-15 16:15:53 -04001071 blocksize = btrfs_level_size(tree_root,
1072 btrfs_super_root_level(disk_super));
Chris Mason19c00dd2007-10-15 16:19:22 -04001073
Chris Mason0b86a832008-03-24 15:01:56 -04001074
Chris Masone20d96d2007-03-22 12:13:20 -04001075 tree_root->node = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -04001076 btrfs_super_root(disk_super),
1077 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04001078 if (!tree_root->node)
1079 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -04001080
Chris Masondb945352007-10-15 16:15:53 -04001081
1082 ret = find_and_setup_root(tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -04001083 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason0b86a832008-03-24 15:01:56 -04001084 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001085 goto fail_tree_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001086 extent_root->track_dirty = 1;
1087
1088 ret = find_and_setup_root(tree_root, fs_info,
1089 BTRFS_DEV_TREE_OBJECTID, dev_root);
1090 dev_root->track_dirty = 1;
1091
1092 if (ret)
1093 goto fail_extent_root;
Chris Mason3768f362007-03-13 16:47:54 -04001094
Chris Mason9078a3e2007-04-26 16:46:15 -04001095 btrfs_read_block_groups(extent_root);
1096
Chris Mason0f7d52f2007-04-09 10:42:37 -04001097 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Masond18a2c42008-04-04 15:40:00 -04001098 fs_info->data_alloc_profile = (u64)-1;
1099 fs_info->metadata_alloc_profile = (u64)-1;
1100 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1101
Chris Mason5be6f7f2007-04-05 13:35:25 -04001102 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001103 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -04001104
Chris Mason0b86a832008-03-24 15:01:56 -04001105fail_extent_root:
1106 free_extent_buffer(extent_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -04001107fail_tree_root:
Chris Mason0b86a832008-03-24 15:01:56 -04001108 mutex_unlock(&fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001109 free_extent_buffer(tree_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -04001110fail_sb_buffer:
Chris Mason5f39d392007-10-15 16:14:19 -04001111 free_extent_buffer(fs_info->sb_buffer);
Chris Mason2d2ae542008-03-26 16:24:23 -04001112 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04001113fail_iput:
1114 iput(fs_info->btree_inode);
1115fail:
Chris Mason8a4b83c2008-03-24 15:02:07 -04001116 close_all_devices(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04001117 kfree(extent_root);
1118 kfree(tree_root);
Chris Mason2d2ae542008-03-26 16:24:23 -04001119 bdi_destroy(&fs_info->bdi);
Chris Mason39279cc2007-06-12 06:35:45 -04001120 kfree(fs_info);
1121 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -05001122}
1123
Chris Masone089f052007-03-16 16:20:31 -04001124int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -04001125 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -05001126{
Chris Masone66f7092007-04-20 13:16:02 -04001127 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001128 struct extent_buffer *super = root->fs_info->sb_buffer;
1129 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason21ad10c2008-01-09 09:23:21 -05001130 struct super_block *sb = root->fs_info->sb;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001131
Chris Mason21ad10c2008-01-09 09:23:21 -05001132 if (!btrfs_test_opt(root, NOBARRIER))
1133 blkdev_issue_flush(sb->s_bdev, NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001134 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
Chris Mason5f39d392007-10-15 16:14:19 -04001135 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
1136 super->start, super->len);
Chris Mason21ad10c2008-01-09 09:23:21 -05001137 if (!btrfs_test_opt(root, NOBARRIER))
1138 blkdev_issue_flush(sb->s_bdev, NULL);
Chris Mason5f39d392007-10-15 16:14:19 -04001139 return ret;
Chris Masoncfaa7292007-02-21 17:04:57 -05001140}
1141
Chris Mason5eda7b52007-06-22 14:16:25 -04001142int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -04001143{
1144 radix_tree_delete(&fs_info->fs_roots_radix,
1145 (unsigned long)root->root_key.objectid);
Chris Masonb99aa6c2008-01-14 14:41:16 -05001146 if (root->in_sysfs)
1147 btrfs_sysfs_del_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -04001148 if (root->inode)
1149 iput(root->inode);
1150 if (root->node)
Chris Mason5f39d392007-10-15 16:14:19 -04001151 free_extent_buffer(root->node);
Chris Mason2619ba12007-04-10 16:58:11 -04001152 if (root->commit_root)
Chris Mason5f39d392007-10-15 16:14:19 -04001153 free_extent_buffer(root->commit_root);
Josef Bacik58176a92007-08-29 15:47:34 -04001154 if (root->name)
1155 kfree(root->name);
Chris Mason2619ba12007-04-10 16:58:11 -04001156 kfree(root);
1157 return 0;
1158}
1159
Chris Mason35b7e472007-05-02 15:53:43 -04001160static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -04001161{
1162 int ret;
1163 struct btrfs_root *gang[8];
1164 int i;
1165
1166 while(1) {
1167 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
1168 (void **)gang, 0,
1169 ARRAY_SIZE(gang));
1170 if (!ret)
1171 break;
Chris Mason2619ba12007-04-10 16:58:11 -04001172 for (i = 0; i < ret; i++)
Chris Mason5eda7b52007-06-22 14:16:25 -04001173 btrfs_free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001174 }
1175 return 0;
1176}
Chris Masonb4100d62007-04-12 12:14:00 -04001177
Chris Masone20d96d2007-03-22 12:13:20 -04001178int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -05001179{
Chris Mason3768f362007-03-13 16:47:54 -04001180 int ret;
Chris Masone089f052007-03-16 16:20:31 -04001181 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001182 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -04001183
Chris Masonfacda1e2007-06-08 18:11:48 -04001184 fs_info->closing = 1;
Chris Mason08607c12007-06-08 15:33:54 -04001185 btrfs_transaction_flush_work(root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001186 mutex_lock(&fs_info->fs_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -04001187 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason79154b12007-03-22 15:59:16 -04001188 trans = btrfs_start_transaction(root, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001189 ret = btrfs_commit_transaction(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -04001190 /* run commit again to drop the original snapshot */
1191 trans = btrfs_start_transaction(root, 1);
1192 btrfs_commit_transaction(trans, root);
1193 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -04001194 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -04001195 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001196 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001197
Chris Masonb0c68f82008-01-31 11:05:37 -05001198 if (fs_info->delalloc_bytes) {
1199 printk("btrfs: at unmount delalloc count %Lu\n",
1200 fs_info->delalloc_bytes);
1201 }
Chris Mason0f7d52f2007-04-09 10:42:37 -04001202 if (fs_info->extent_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -04001203 free_extent_buffer(fs_info->extent_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -04001204
Chris Mason0f7d52f2007-04-09 10:42:37 -04001205 if (fs_info->tree_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -04001206 free_extent_buffer(fs_info->tree_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -04001207
Chris Mason0b86a832008-03-24 15:01:56 -04001208 if (root->fs_info->chunk_root->node);
1209 free_extent_buffer(root->fs_info->chunk_root->node);
1210
1211 if (root->fs_info->dev_root->node);
1212 free_extent_buffer(root->fs_info->dev_root->node);
1213
Chris Mason5f39d392007-10-15 16:14:19 -04001214 free_extent_buffer(fs_info->sb_buffer);
Chris Mason7eccb902007-04-11 15:53:25 -04001215
Chris Mason9078a3e2007-04-26 16:46:15 -04001216 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001217 del_fs_roots(fs_info);
Chris Masond10c5f32007-12-17 20:14:04 -05001218
1219 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
1220
Chris Masond1310b22008-01-24 16:13:08 -05001221 extent_io_tree_empty_lru(&fs_info->free_space_cache);
1222 extent_io_tree_empty_lru(&fs_info->block_group_cache);
1223 extent_io_tree_empty_lru(&fs_info->pinned_extents);
1224 extent_io_tree_empty_lru(&fs_info->pending_del);
1225 extent_io_tree_empty_lru(&fs_info->extent_ins);
1226 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
Chris Masond10c5f32007-12-17 20:14:04 -05001227
Chris Masondb945352007-10-15 16:15:53 -04001228 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
Chris Masonce9adaa2008-04-09 16:28:12 -04001229 flush_workqueue(end_io_workqueue);
1230 destroy_workqueue(end_io_workqueue);
Chris Masond10c5f32007-12-17 20:14:04 -05001231
Chris Masondb945352007-10-15 16:15:53 -04001232 iput(fs_info->btree_inode);
Chris Mason19c00dd2007-10-15 16:19:22 -04001233#if 0
1234 while(!list_empty(&fs_info->hashers)) {
1235 struct btrfs_hasher *hasher;
1236 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
1237 hashers);
1238 list_del(&hasher->hashers);
1239 crypto_free_hash(&fs_info->hash_tfm);
1240 kfree(hasher);
1241 }
1242#endif
Chris Mason0b86a832008-03-24 15:01:56 -04001243 close_all_devices(fs_info);
1244 btrfs_mapping_tree_free(&fs_info->mapping_tree);
Chris Mason04160082008-03-26 10:28:07 -04001245 bdi_destroy(&fs_info->bdi);
Chris Mason0b86a832008-03-24 15:01:56 -04001246
Chris Mason0f7d52f2007-04-09 10:42:37 -04001247 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001248 kfree(fs_info->tree_root);
Chris Mason0b86a832008-03-24 15:01:56 -04001249 kfree(fs_info->chunk_root);
1250 kfree(fs_info->dev_root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001251 return 0;
1252}
1253
Chris Mason5f39d392007-10-15 16:14:19 -04001254int btrfs_buffer_uptodate(struct extent_buffer *buf)
Chris Masonccd467d2007-06-28 15:57:36 -04001255{
Chris Mason810191f2007-10-15 16:18:55 -04001256 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001257 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001258}
Chris Mason6702ed42007-08-07 16:15:09 -04001259
Chris Mason5f39d392007-10-15 16:14:19 -04001260int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
1261{
Chris Mason810191f2007-10-15 16:18:55 -04001262 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001263 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -04001264 buf);
1265}
1266
1267void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
1268{
Chris Mason810191f2007-10-15 16:18:55 -04001269 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04001270 u64 transid = btrfs_header_generation(buf);
1271 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason6702ed42007-08-07 16:15:09 -04001272
Chris Masonccd467d2007-06-28 15:57:36 -04001273 if (transid != root->fs_info->generation) {
1274 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
Chris Masondb945352007-10-15 16:15:53 -04001275 (unsigned long long)buf->start,
Chris Masonccd467d2007-06-28 15:57:36 -04001276 transid, root->fs_info->generation);
1277 WARN_ON(1);
1278 }
Chris Masond1310b22008-01-24 16:13:08 -05001279 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001280}
1281
Chris Masone2008b62008-01-08 15:46:30 -05001282void btrfs_throttle(struct btrfs_root *root)
1283{
Chris Mason55c69072008-01-09 15:55:33 -05001284 struct backing_dev_info *bdi;
1285
1286 bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
Chris Mason04005cc2008-01-17 12:01:41 -05001287 if (root->fs_info->throttles && bdi_write_congested(bdi)) {
1288#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
Chris Mason55c69072008-01-09 15:55:33 -05001289 congestion_wait(WRITE, HZ/20);
Chris Mason04005cc2008-01-17 12:01:41 -05001290#else
1291 blk_congestion_wait(WRITE, HZ/20);
1292#endif
1293 }
Chris Masone2008b62008-01-08 15:46:30 -05001294}
1295
Chris Masond3c2fdc2007-09-17 10:58:06 -04001296void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
Chris Mason35b7e472007-05-02 15:53:43 -04001297{
Chris Masond3c2fdc2007-09-17 10:58:06 -04001298 balance_dirty_pages_ratelimited_nr(
Chris Masond7fc6402008-02-18 12:12:38 -05001299 root->fs_info->btree_inode->i_mapping, 1);
Chris Mason35b7e472007-05-02 15:53:43 -04001300}
Chris Mason6b800532007-10-15 16:17:34 -04001301
1302void btrfs_set_buffer_defrag(struct extent_buffer *buf)
1303{
Chris Mason810191f2007-10-15 16:18:55 -04001304 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001305 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001306 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
Chris Mason6b800532007-10-15 16:17:34 -04001307 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
1308}
1309
1310void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
1311{
Chris Mason810191f2007-10-15 16:18:55 -04001312 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001313 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001314 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
Chris Mason6b800532007-10-15 16:17:34 -04001315 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
1316 GFP_NOFS);
1317}
1318
1319int btrfs_buffer_defrag(struct extent_buffer *buf)
1320{
Chris Mason810191f2007-10-15 16:18:55 -04001321 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001322 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001323 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001324 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
1325}
1326
1327int btrfs_buffer_defrag_done(struct extent_buffer *buf)
1328{
Chris Mason810191f2007-10-15 16:18:55 -04001329 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001330 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001331 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001332 buf->start, buf->start + buf->len - 1,
1333 EXTENT_DEFRAG_DONE, 0);
1334}
1335
1336int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
1337{
Chris Mason810191f2007-10-15 16:18:55 -04001338 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001339 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001340 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001341 buf->start, buf->start + buf->len - 1,
1342 EXTENT_DEFRAG_DONE, GFP_NOFS);
1343}
1344
1345int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
1346{
Chris Mason810191f2007-10-15 16:18:55 -04001347 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001348 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001349 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001350 buf->start, buf->start + buf->len - 1,
1351 EXTENT_DEFRAG, GFP_NOFS);
1352}
1353
1354int btrfs_read_buffer(struct extent_buffer *buf)
1355{
Chris Mason810191f2007-10-15 16:18:55 -04001356 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001357 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonce9adaa2008-04-09 16:28:12 -04001358 int ret;
1359 ret = read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
Chris Masona86c12c2008-02-07 10:50:54 -05001360 buf, 0, 1, btree_get_extent);
Chris Masonce9adaa2008-04-09 16:28:12 -04001361 if (ret == 0) {
1362 buf->flags |= EXTENT_UPTODATE;
1363 }
1364 return ret;
Chris Mason6b800532007-10-15 16:17:34 -04001365}
Chris Mason0da54682007-11-07 21:08:01 -05001366
Chris Masond1310b22008-01-24 16:13:08 -05001367static struct extent_io_ops btree_extent_io_ops = {
Chris Mason0da54682007-11-07 21:08:01 -05001368 .writepage_io_hook = btree_writepage_io_hook,
Chris Masonce9adaa2008-04-09 16:28:12 -04001369 .readpage_end_io_hook = btree_readpage_end_io_hook,
Chris Mason0b86a832008-03-24 15:01:56 -04001370 .submit_bio_hook = btree_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04001371 /* note we're sharing with inode.c for the merge bio hook */
1372 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason0da54682007-11-07 21:08:01 -05001373};