blob: 94ff87d0eae4a77c6ec1d656d3921becd28d85d2 [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;
Chris Mason22c59942008-04-09 16:28:12 -040057 int metadata;
Chris Masonce9adaa2008-04-09 16:28:12 -040058 struct list_head list;
59};
Chris Mason0da54682007-11-07 21:08:01 -050060
Chris Mason5f39d392007-10-15 16:14:19 -040061struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
Chris Masond1310b22008-01-24 16:13:08 -050062 size_t page_offset, u64 start, u64 len,
Chris Mason5f39d392007-10-15 16:14:19 -040063 int create)
64{
65 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
66 struct extent_map *em;
67 int ret;
Chris Masond98237b2007-03-28 13:57:48 -040068
Chris Mason5f39d392007-10-15 16:14:19 -040069again:
Chris Masond1310b22008-01-24 16:13:08 -050070 spin_lock(&em_tree->lock);
71 em = lookup_extent_mapping(em_tree, start, len);
72 spin_unlock(&em_tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -040073 if (em) {
74 goto out;
75 }
76 em = alloc_extent_map(GFP_NOFS);
77 if (!em) {
78 em = ERR_PTR(-ENOMEM);
79 goto out;
80 }
81 em->start = 0;
Chris Masond1310b22008-01-24 16:13:08 -050082 em->len = i_size_read(inode);
Chris Mason5f39d392007-10-15 16:14:19 -040083 em->block_start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -040084 em->bdev = inode->i_sb->s_bdev;
Chris Masond1310b22008-01-24 16:13:08 -050085
86 spin_lock(&em_tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -040087 ret = add_extent_mapping(em_tree, em);
Chris Masond1310b22008-01-24 16:13:08 -050088 spin_unlock(&em_tree->lock);
89
Chris Mason5f39d392007-10-15 16:14:19 -040090 if (ret == -EEXIST) {
91 free_extent_map(em);
92 em = NULL;
93 goto again;
94 } else if (ret) {
95 em = ERR_PTR(ret);
96 }
97out:
98 return em;
99}
100
Chris Mason19c00dd2007-10-15 16:19:22 -0400101u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
102{
103 return crc32c(seed, data, len);
104}
105
106void btrfs_csum_final(u32 crc, char *result)
107{
108 *(__le32 *)result = ~cpu_to_le32(crc);
109}
110
111static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
112 int verify)
113{
114 char result[BTRFS_CRC32_SIZE];
115 unsigned long len;
116 unsigned long cur_len;
117 unsigned long offset = BTRFS_CSUM_SIZE;
118 char *map_token = NULL;
119 char *kaddr;
120 unsigned long map_start;
121 unsigned long map_len;
122 int err;
123 u32 crc = ~(u32)0;
124
125 len = buf->len - offset;
126 while(len > 0) {
127 err = map_private_extent_buffer(buf, offset, 32,
128 &map_token, &kaddr,
129 &map_start, &map_len, KM_USER0);
130 if (err) {
131 printk("failed to map extent buffer! %lu\n",
132 offset);
133 return 1;
134 }
135 cur_len = min(len, map_len - (offset - map_start));
136 crc = btrfs_csum_data(root, kaddr + offset - map_start,
137 crc, cur_len);
138 len -= cur_len;
139 offset += cur_len;
140 unmap_extent_buffer(buf, map_token, KM_USER0);
141 }
142 btrfs_csum_final(crc, result);
143
144 if (verify) {
Chris Masone4204de2008-01-08 15:46:27 -0500145 int from_this_trans = 0;
146
147 if (root->fs_info->running_transaction &&
148 btrfs_header_generation(buf) ==
149 root->fs_info->running_transaction->transid)
150 from_this_trans = 1;
151
152 /* FIXME, this is not good */
Chris Mason63b10fc2008-04-01 11:21:32 -0400153 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
Chris Masone4204de2008-01-08 15:46:27 -0500154 u32 val;
155 u32 found = 0;
156 memcpy(&found, result, BTRFS_CRC32_SIZE);
157
158 read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
Chris Mason63b10fc2008-04-01 11:21:32 -0400159 WARN_ON(1);
Chris Masone4204de2008-01-08 15:46:27 -0500160 printk("btrfs: %s checksum verify failed on %llu "
Chris Mason63b10fc2008-04-01 11:21:32 -0400161 "wanted %X found %X from_this_trans %d "
162 "level %d\n",
Chris Mason19c00dd2007-10-15 16:19:22 -0400163 root->fs_info->sb->s_id,
Chris Mason63b10fc2008-04-01 11:21:32 -0400164 buf->start, val, found, from_this_trans,
165 btrfs_header_level(buf));
Chris Mason19c00dd2007-10-15 16:19:22 -0400166 return 1;
167 }
168 } else {
169 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
170 }
171 return 0;
172}
173
174
175int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
176{
Chris Masond1310b22008-01-24 16:13:08 -0500177 struct extent_io_tree *tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400178 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason19c00dd2007-10-15 16:19:22 -0400179 u64 found_start;
180 int found_level;
181 unsigned long len;
182 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -0500183 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason19c00dd2007-10-15 16:19:22 -0400184
185 if (page->private == EXTENT_PAGE_PRIVATE)
186 goto out;
187 if (!page->private)
188 goto out;
189 len = page->private >> 2;
190 if (len == 0) {
191 WARN_ON(1);
192 }
193 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
Chris Masona86c12c2008-02-07 10:50:54 -0500194 read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1,
195 btree_get_extent);
Chris Masone18e4802008-01-18 10:54:22 -0500196 btrfs_clear_buffer_defrag(eb);
Chris Mason19c00dd2007-10-15 16:19:22 -0400197 found_start = btrfs_header_bytenr(eb);
198 if (found_start != start) {
199 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
200 start, found_start, len);
Chris Mason55c69072008-01-09 15:55:33 -0500201 WARN_ON(1);
202 goto err;
203 }
204 if (eb->first_page != page) {
205 printk("bad first page %lu %lu\n", eb->first_page->index,
206 page->index);
207 WARN_ON(1);
208 goto err;
209 }
210 if (!PageUptodate(page)) {
211 printk("csum not up to date page %lu\n", page->index);
212 WARN_ON(1);
213 goto err;
Chris Mason19c00dd2007-10-15 16:19:22 -0400214 }
215 found_level = btrfs_header_level(eb);
Chris Mason63b10fc2008-04-01 11:21:32 -0400216 spin_lock(&root->fs_info->hash_lock);
217 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
218 spin_unlock(&root->fs_info->hash_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -0400219 csum_tree_block(root, eb, 0);
Chris Mason55c69072008-01-09 15:55:33 -0500220err:
Chris Mason19c00dd2007-10-15 16:19:22 -0400221 free_extent_buffer(eb);
222out:
223 return 0;
224}
225
Chris Mason0da54682007-11-07 21:08:01 -0500226static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
227{
228 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
229
230 csum_dirty_buffer(root, page);
231 return 0;
232}
233
Chris Masonce9adaa2008-04-09 16:28:12 -0400234int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
235 struct extent_state *state)
236{
237 struct extent_io_tree *tree;
238 u64 found_start;
239 int found_level;
240 unsigned long len;
241 struct extent_buffer *eb;
242 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
243 int ret;
244
245 tree = &BTRFS_I(page->mapping->host)->io_tree;
246 if (page->private == EXTENT_PAGE_PRIVATE)
247 goto out;
248 if (!page->private)
249 goto out;
250 len = page->private >> 2;
251 if (len == 0) {
252 WARN_ON(1);
253 }
254 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
255 read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1,
256 btree_get_extent);
257 btrfs_clear_buffer_defrag(eb);
258 found_start = btrfs_header_bytenr(eb);
259 if (found_start != start) {
260 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
261 start, found_start, len);
262 WARN_ON(1);
263 goto err;
264 }
265 if (eb->first_page != page) {
266 printk("bad first page %lu %lu\n", eb->first_page->index,
267 page->index);
268 WARN_ON(1);
269 goto err;
270 }
271 found_level = btrfs_header_level(eb);
272
273 ret = csum_tree_block(root, eb, 1);
274
275 end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
276 end = eb->start + end - 1;
277 release_extent_buffer_tail_pages(eb);
278err:
279 free_extent_buffer(eb);
280out:
281 return 0;
282}
283
284#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
285static void end_workqueue_bio(struct bio *bio, int err)
286#else
287static int end_workqueue_bio(struct bio *bio,
288 unsigned int bytes_done, int err)
289#endif
290{
291 struct end_io_wq *end_io_wq = bio->bi_private;
292 struct btrfs_fs_info *fs_info;
293 unsigned long flags;
294
295#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
296 if (bio->bi_size)
297 return 1;
298#endif
299
300 fs_info = end_io_wq->info;
301 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
302 end_io_wq->error = err;
303 list_add_tail(&end_io_wq->list, &fs_info->end_io_work_list);
304 spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
305 queue_work(end_io_workqueue, &fs_info->end_io_work);
306
307#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
308 return 0;
309#endif
310}
311
Chris Mason22c59942008-04-09 16:28:12 -0400312int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
313 int metadata)
Chris Mason0b86a832008-03-24 15:01:56 -0400314{
Chris Masonce9adaa2008-04-09 16:28:12 -0400315 struct end_io_wq *end_io_wq;
Chris Masonce9adaa2008-04-09 16:28:12 -0400316 end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
317 if (!end_io_wq)
318 return -ENOMEM;
319
320 end_io_wq->private = bio->bi_private;
321 end_io_wq->end_io = bio->bi_end_io;
Chris Mason22c59942008-04-09 16:28:12 -0400322 end_io_wq->info = info;
Chris Masonce9adaa2008-04-09 16:28:12 -0400323 end_io_wq->error = 0;
324 end_io_wq->bio = bio;
Chris Mason22c59942008-04-09 16:28:12 -0400325 end_io_wq->metadata = metadata;
Chris Masonce9adaa2008-04-09 16:28:12 -0400326
327 bio->bi_private = end_io_wq;
328 bio->bi_end_io = end_workqueue_bio;
Chris Mason22c59942008-04-09 16:28:12 -0400329 return 0;
330}
331
332static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio)
333{
334 struct btrfs_root *root = BTRFS_I(inode)->root;
335 u64 offset;
336 int ret;
337
338 offset = bio->bi_sector << 9;
339
340 if (rw & (1 << BIO_RW)) {
341 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
342 }
343
344 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1);
345 BUG_ON(ret);
Chris Masonce9adaa2008-04-09 16:28:12 -0400346
Chris Mason0b86a832008-03-24 15:01:56 -0400347 if (offset == BTRFS_SUPER_INFO_OFFSET) {
348 bio->bi_bdev = root->fs_info->sb->s_bdev;
349 submit_bio(rw, bio);
350 return 0;
351 }
352 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
353}
354
Chris Mason5f39d392007-10-15 16:14:19 -0400355static int btree_writepage(struct page *page, struct writeback_control *wbc)
356{
Chris Masond1310b22008-01-24 16:13:08 -0500357 struct extent_io_tree *tree;
358 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400359 return extent_write_full_page(tree, page, btree_get_extent, wbc);
360}
Chris Mason0da54682007-11-07 21:08:01 -0500361
362static int btree_writepages(struct address_space *mapping,
363 struct writeback_control *wbc)
364{
Chris Masond1310b22008-01-24 16:13:08 -0500365 struct extent_io_tree *tree;
366 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500367 if (wbc->sync_mode == WB_SYNC_NONE) {
Chris Mason793955b2007-11-26 16:34:41 -0800368 u64 num_dirty;
369 u64 start = 0;
370 unsigned long thresh = 96 * 1024 * 1024;
Chris Mason448d6402007-11-27 07:52:01 -0800371
372 if (wbc->for_kupdate)
373 return 0;
374
Chris Masonca664622007-11-27 11:16:35 -0500375 if (current_is_pdflush()) {
376 thresh = 96 * 1024 * 1024;
377 } else {
378 thresh = 8 * 1024 * 1024;
379 }
Chris Mason1832a6d2007-12-21 16:27:21 -0500380 num_dirty = count_range_bits(tree, &start, (u64)-1,
381 thresh, EXTENT_DIRTY);
Chris Mason793955b2007-11-26 16:34:41 -0800382 if (num_dirty < thresh) {
383 return 0;
384 }
385 }
Chris Mason0da54682007-11-07 21:08:01 -0500386 return extent_writepages(tree, mapping, btree_get_extent, wbc);
387}
388
Chris Mason5f39d392007-10-15 16:14:19 -0400389int btree_readpage(struct file *file, struct page *page)
390{
Chris Masond1310b22008-01-24 16:13:08 -0500391 struct extent_io_tree *tree;
392 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400393 return extent_read_full_page(tree, page, btree_get_extent);
394}
395
Chris Mason70dec802008-01-29 09:59:12 -0500396static int btree_releasepage(struct page *page, gfp_t gfp_flags)
Chris Mason5f39d392007-10-15 16:14:19 -0400397{
Chris Masond1310b22008-01-24 16:13:08 -0500398 struct extent_io_tree *tree;
399 struct extent_map_tree *map;
Chris Mason5f39d392007-10-15 16:14:19 -0400400 int ret;
401
Chris Masond1310b22008-01-24 16:13:08 -0500402 tree = &BTRFS_I(page->mapping->host)->io_tree;
403 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -0500404 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400405 if (ret == 1) {
Chris Mason728131d2008-04-09 16:28:12 -0400406 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -0400407 ClearPagePrivate(page);
408 set_page_private(page, 0);
409 page_cache_release(page);
410 }
Chris Masond98237b2007-03-28 13:57:48 -0400411 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400412}
Chris Mason123abc82007-03-14 14:14:43 -0400413
Chris Mason5f39d392007-10-15 16:14:19 -0400414static void btree_invalidatepage(struct page *page, unsigned long offset)
Chris Masond98237b2007-03-28 13:57:48 -0400415{
Chris Masond1310b22008-01-24 16:13:08 -0500416 struct extent_io_tree *tree;
417 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400418 extent_invalidatepage(tree, page, offset);
419 btree_releasepage(page, GFP_NOFS);
Chris Masond98237b2007-03-28 13:57:48 -0400420}
421
Chris Mason5f39d392007-10-15 16:14:19 -0400422#if 0
Chris Masond98237b2007-03-28 13:57:48 -0400423static int btree_writepage(struct page *page, struct writeback_control *wbc)
424{
Chris Mason87cbda52007-03-28 19:44:27 -0400425 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400426 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400427 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400428 if (!page_has_buffers(page)) {
429 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
430 (1 << BH_Dirty)|(1 << BH_Uptodate));
431 }
432 head = page_buffers(page);
433 bh = head;
434 do {
435 if (buffer_dirty(bh))
436 csum_tree_block(root, bh, 0);
437 bh = bh->b_this_page;
438 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400439 return block_write_full_page(page, btree_get_block, wbc);
440}
Chris Mason5f39d392007-10-15 16:14:19 -0400441#endif
Chris Masond98237b2007-03-28 13:57:48 -0400442
443static struct address_space_operations btree_aops = {
444 .readpage = btree_readpage,
445 .writepage = btree_writepage,
Chris Mason0da54682007-11-07 21:08:01 -0500446 .writepages = btree_writepages,
Chris Mason5f39d392007-10-15 16:14:19 -0400447 .releasepage = btree_releasepage,
448 .invalidatepage = btree_invalidatepage,
Chris Masond98237b2007-03-28 13:57:48 -0400449 .sync_page = block_sync_page,
450};
451
Chris Masondb945352007-10-15 16:15:53 -0400452int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
Chris Mason090d1872007-05-01 08:53:32 -0400453{
Chris Mason5f39d392007-10-15 16:14:19 -0400454 struct extent_buffer *buf = NULL;
455 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonde428b62007-05-18 13:28:27 -0400456 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400457
Chris Masondb945352007-10-15 16:15:53 -0400458 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400459 if (!buf)
Chris Mason090d1872007-05-01 08:53:32 -0400460 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500461 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
Chris Masona86c12c2008-02-07 10:50:54 -0500462 buf, 0, 0, btree_get_extent);
Chris Mason5f39d392007-10-15 16:14:19 -0400463 free_extent_buffer(buf);
Chris Masonde428b62007-05-18 13:28:27 -0400464 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400465}
466
Chris Mason0b86a832008-03-24 15:01:56 -0400467static int close_all_devices(struct btrfs_fs_info *fs_info)
468{
469 struct list_head *list;
470 struct list_head *next;
471 struct btrfs_device *device;
472
Chris Mason8a4b83c2008-03-24 15:02:07 -0400473 list = &fs_info->fs_devices->devices;
474 list_for_each(next, list) {
Chris Mason0b86a832008-03-24 15:01:56 -0400475 device = list_entry(next, struct btrfs_device, dev_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400476 if (device->bdev && device->bdev != fs_info->sb->s_bdev)
477 close_bdev_excl(device->bdev);
478 device->bdev = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400479 }
480 return 0;
481}
482
Chris Mason0999df52008-04-01 13:48:14 -0400483int btrfs_verify_block_csum(struct btrfs_root *root,
484 struct extent_buffer *buf)
485{
Chris Masonce9adaa2008-04-09 16:28:12 -0400486 return btrfs_buffer_uptodate(buf);
Chris Mason0999df52008-04-01 13:48:14 -0400487}
488
489struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
490 u64 bytenr, u32 blocksize)
491{
492 struct inode *btree_inode = root->fs_info->btree_inode;
493 struct extent_buffer *eb;
494 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
495 bytenr, blocksize, GFP_NOFS);
496 return eb;
497}
498
499struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
500 u64 bytenr, u32 blocksize)
501{
502 struct inode *btree_inode = root->fs_info->btree_inode;
503 struct extent_buffer *eb;
504
505 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
506 bytenr, blocksize, NULL, GFP_NOFS);
507 return eb;
508}
509
510
Chris Masondb945352007-10-15 16:15:53 -0400511struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
512 u32 blocksize)
Chris Masone20d96d2007-03-22 12:13:20 -0400513{
Chris Mason5f39d392007-10-15 16:14:19 -0400514 struct extent_buffer *buf = NULL;
515 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -0500516 struct extent_io_tree *io_tree;
Chris Mason19c00dd2007-10-15 16:19:22 -0400517 int ret;
518
Chris Masond1310b22008-01-24 16:13:08 -0500519 io_tree = &BTRFS_I(btree_inode)->io_tree;
Chris Masone20d96d2007-03-22 12:13:20 -0400520
Chris Masondb945352007-10-15 16:15:53 -0400521 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400522 if (!buf)
523 return NULL;
Chris Masone4204de2008-01-08 15:46:27 -0500524
Chris Masonce9adaa2008-04-09 16:28:12 -0400525 ret = read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 0,
526 1, btree_get_extent);
527
528 if (ret == 0) {
529 buf->flags |= EXTENT_UPTODATE;
530 }
Chris Mason5f39d392007-10-15 16:14:19 -0400531 return buf;
Chris Masonce9adaa2008-04-09 16:28:12 -0400532
Chris Masoneb60cea2007-02-02 09:18:22 -0500533}
534
Chris Masone089f052007-03-16 16:20:31 -0400535int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400536 struct extent_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500537{
Chris Mason5f39d392007-10-15 16:14:19 -0400538 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason55c69072008-01-09 15:55:33 -0500539 if (btrfs_header_generation(buf) ==
540 root->fs_info->running_transaction->transid)
Chris Masond1310b22008-01-24 16:13:08 -0500541 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
Chris Mason55c69072008-01-09 15:55:33 -0500542 buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400543 return 0;
544}
545
546int wait_on_tree_block_writeback(struct btrfs_root *root,
547 struct extent_buffer *buf)
548{
549 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -0500550 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -0400551 buf);
552 return 0;
553}
554
Chris Masondb945352007-10-15 16:15:53 -0400555static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500556 u32 stripesize, struct btrfs_root *root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400557 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400558 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500559{
Chris Masoncfaa7292007-02-21 17:04:57 -0500560 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400561 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500562 root->commit_root = NULL;
Chris Masondb945352007-10-15 16:15:53 -0400563 root->sectorsize = sectorsize;
564 root->nodesize = nodesize;
565 root->leafsize = leafsize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500566 root->stripesize = stripesize;
Chris Mason123abc82007-03-14 14:14:43 -0400567 root->ref_cows = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400568 root->track_dirty = 0;
569
Chris Mason9f5fae22007-03-20 14:38:32 -0400570 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400571 root->objectid = objectid;
572 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400573 root->highest_inode = 0;
574 root->last_inode_alloc = 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400575 root->name = NULL;
Chris Mason4313b392008-01-03 09:08:48 -0500576 root->in_sysfs = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400577
578 INIT_LIST_HEAD(&root->dirty_list);
Chris Mason3768f362007-03-13 16:47:54 -0400579 memset(&root->root_key, 0, sizeof(root->root_key));
580 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -0400581 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Josef Bacik58176a92007-08-29 15:47:34 -0400582 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
583 init_completion(&root->kobj_unregister);
Chris Mason6702ed42007-08-07 16:15:09 -0400584 root->defrag_running = 0;
585 root->defrag_level = 0;
Chris Mason4d775672007-04-20 20:23:12 -0400586 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400587 return 0;
588}
589
Chris Masondb945352007-10-15 16:15:53 -0400590static int find_and_setup_root(struct btrfs_root *tree_root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400591 struct btrfs_fs_info *fs_info,
592 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400593 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400594{
595 int ret;
Chris Masondb945352007-10-15 16:15:53 -0400596 u32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -0400597
Chris Masondb945352007-10-15 16:15:53 -0400598 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500599 tree_root->sectorsize, tree_root->stripesize,
600 root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400601 ret = btrfs_find_last_root(tree_root, objectid,
602 &root->root_item, &root->root_key);
603 BUG_ON(ret);
604
Chris Masondb945352007-10-15 16:15:53 -0400605 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
606 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
607 blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400608 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500609 return 0;
610}
611
Chris Mason5eda7b52007-06-22 14:16:25 -0400612struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
613 struct btrfs_key *location)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400614{
615 struct btrfs_root *root;
616 struct btrfs_root *tree_root = fs_info->tree_root;
617 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400618 struct extent_buffer *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400619 u64 highest_inode;
Chris Masondb945352007-10-15 16:15:53 -0400620 u32 blocksize;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400621 int ret = 0;
622
Chris Mason5eda7b52007-06-22 14:16:25 -0400623 root = kzalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400624 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400625 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400626 if (location->offset == (u64)-1) {
Chris Masondb945352007-10-15 16:15:53 -0400627 ret = find_and_setup_root(tree_root, fs_info,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400628 location->objectid, root);
629 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400630 kfree(root);
631 return ERR_PTR(ret);
632 }
633 goto insert;
634 }
635
Chris Masondb945352007-10-15 16:15:53 -0400636 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500637 tree_root->sectorsize, tree_root->stripesize,
638 root, fs_info, location->objectid);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400639
640 path = btrfs_alloc_path();
641 BUG_ON(!path);
642 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
643 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400644 if (ret > 0)
645 ret = -ENOENT;
646 goto out;
647 }
Chris Mason5f39d392007-10-15 16:14:19 -0400648 l = path->nodes[0];
649 read_extent_buffer(l, &root->root_item,
650 btrfs_item_ptr_offset(l, path->slots[0]),
Chris Mason0f7d52f2007-04-09 10:42:37 -0400651 sizeof(root->root_item));
Yan Zheng44b36eb2007-10-19 09:22:56 -0400652 memcpy(&root->root_key, location, sizeof(*location));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400653 ret = 0;
654out:
655 btrfs_release_path(root, path);
656 btrfs_free_path(path);
657 if (ret) {
658 kfree(root);
659 return ERR_PTR(ret);
660 }
Chris Masondb945352007-10-15 16:15:53 -0400661 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
662 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
663 blocksize);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400664 BUG_ON(!root->node);
665insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400666 root->ref_cows = 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400667 ret = btrfs_find_highest_inode(root, &highest_inode);
668 if (ret == 0) {
669 root->highest_inode = highest_inode;
670 root->last_inode_alloc = highest_inode;
671 }
672 return root;
673}
674
Chris Masondc17ff82008-01-08 15:46:30 -0500675struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
676 u64 root_objectid)
677{
678 struct btrfs_root *root;
679
680 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
681 return fs_info->tree_root;
682 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
683 return fs_info->extent_root;
684
685 root = radix_tree_lookup(&fs_info->fs_roots_radix,
686 (unsigned long)root_objectid);
687 return root;
688}
689
Chris Masonedbd8d42007-12-21 16:27:24 -0500690struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
691 struct btrfs_key *location)
Chris Mason5eda7b52007-06-22 14:16:25 -0400692{
693 struct btrfs_root *root;
694 int ret;
695
Chris Masonedbd8d42007-12-21 16:27:24 -0500696 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
697 return fs_info->tree_root;
698 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
699 return fs_info->extent_root;
700
Chris Mason5eda7b52007-06-22 14:16:25 -0400701 root = radix_tree_lookup(&fs_info->fs_roots_radix,
702 (unsigned long)location->objectid);
703 if (root)
704 return root;
705
706 root = btrfs_read_fs_root_no_radix(fs_info, location);
707 if (IS_ERR(root))
708 return root;
Chris Mason2619ba12007-04-10 16:58:11 -0400709 ret = radix_tree_insert(&fs_info->fs_roots_radix,
710 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400711 root);
712 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400713 free_extent_buffer(root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400714 kfree(root);
715 return ERR_PTR(ret);
716 }
Chris Masonedbd8d42007-12-21 16:27:24 -0500717 ret = btrfs_find_dead_roots(fs_info->tree_root,
718 root->root_key.objectid, root);
719 BUG_ON(ret);
720
721 return root;
722}
723
724struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
725 struct btrfs_key *location,
726 const char *name, int namelen)
727{
728 struct btrfs_root *root;
729 int ret;
730
731 root = btrfs_read_fs_root_no_name(fs_info, location);
732 if (!root)
733 return NULL;
Josef Bacik58176a92007-08-29 15:47:34 -0400734
Chris Mason4313b392008-01-03 09:08:48 -0500735 if (root->in_sysfs)
736 return root;
737
Josef Bacik58176a92007-08-29 15:47:34 -0400738 ret = btrfs_set_root_name(root, name, namelen);
739 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400740 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400741 kfree(root);
742 return ERR_PTR(ret);
743 }
744
745 ret = btrfs_sysfs_add_root(root);
746 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400747 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400748 kfree(root->name);
749 kfree(root);
750 return ERR_PTR(ret);
751 }
Chris Mason4313b392008-01-03 09:08:48 -0500752 root->in_sysfs = 1;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400753 return root;
754}
Chris Mason19c00dd2007-10-15 16:19:22 -0400755#if 0
756static int add_hasher(struct btrfs_fs_info *info, char *type) {
757 struct btrfs_hasher *hasher;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400758
Chris Mason19c00dd2007-10-15 16:19:22 -0400759 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
760 if (!hasher)
761 return -ENOMEM;
762 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
763 if (!hasher->hash_tfm) {
764 kfree(hasher);
765 return -EINVAL;
766 }
767 spin_lock(&info->hash_lock);
768 list_add(&hasher->list, &info->hashers);
769 spin_unlock(&info->hash_lock);
770 return 0;
771}
772#endif
Chris Mason04160082008-03-26 10:28:07 -0400773
774static int btrfs_congested_fn(void *congested_data, int bdi_bits)
775{
776 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
777 int ret = 0;
778 struct list_head *cur;
779 struct btrfs_device *device;
780 struct backing_dev_info *bdi;
781
782 list_for_each(cur, &info->fs_devices->devices) {
783 device = list_entry(cur, struct btrfs_device, dev_list);
784 bdi = blk_get_backing_dev_info(device->bdev);
785 if (bdi && bdi_congested(bdi, bdi_bits)) {
786 ret = 1;
787 break;
788 }
789 }
790 return ret;
791}
792
793void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
794{
795 struct list_head *cur;
796 struct btrfs_device *device;
797 struct btrfs_fs_info *info;
798
799 info = (struct btrfs_fs_info *)bdi->unplug_io_data;
800 list_for_each(cur, &info->fs_devices->devices) {
801 device = list_entry(cur, struct btrfs_device, dev_list);
802 bdi = blk_get_backing_dev_info(device->bdev);
803 if (bdi->unplug_io_fn) {
804 bdi->unplug_io_fn(bdi, page);
805 }
806 }
807}
808
809static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
810{
811 bdi_init(bdi);
Chris Mason83041ad2008-03-26 12:02:55 -0400812 bdi->ra_pages = default_backing_dev_info.ra_pages * 4;
Chris Mason04160082008-03-26 10:28:07 -0400813 bdi->state = 0;
814 bdi->capabilities = default_backing_dev_info.capabilities;
815 bdi->unplug_io_fn = btrfs_unplug_io_fn;
816 bdi->unplug_io_data = info;
817 bdi->congested_fn = btrfs_congested_fn;
818 bdi->congested_data = info;
819 return 0;
820}
821
Chris Masonce9adaa2008-04-09 16:28:12 -0400822static int bio_ready_for_csum(struct bio *bio)
823{
824 u64 length = 0;
825 u64 buf_len = 0;
826 u64 start = 0;
827 struct page *page;
828 struct extent_io_tree *io_tree = NULL;
829 struct btrfs_fs_info *info = NULL;
830 struct bio_vec *bvec;
831 int i;
832 int ret;
833
834 bio_for_each_segment(bvec, bio, i) {
835 page = bvec->bv_page;
836 if (page->private == EXTENT_PAGE_PRIVATE) {
837 length += bvec->bv_len;
838 continue;
839 }
840 if (!page->private) {
841 length += bvec->bv_len;
842 continue;
843 }
844 length = bvec->bv_len;
845 buf_len = page->private >> 2;
846 start = page_offset(page) + bvec->bv_offset;
847 io_tree = &BTRFS_I(page->mapping->host)->io_tree;
848 info = BTRFS_I(page->mapping->host)->root->fs_info;
849 }
850 /* are we fully contained in this bio? */
851 if (buf_len <= length)
852 return 1;
853
854 ret = extent_range_uptodate(io_tree, start + length,
855 start + buf_len - 1);
856 if (ret == 1)
857 return ret;
858 return ret;
859}
860
861#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
862void btrfs_end_io_csum(void *p)
863#else
864void btrfs_end_io_csum(struct work_struct *work)
865#endif
866{
867#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
868 struct btrfs_fs_info *fs_info = p;
869#else
870 struct btrfs_fs_info *fs_info = container_of(work,
871 struct btrfs_fs_info,
872 end_io_work);
873#endif
874 unsigned long flags;
875 struct end_io_wq *end_io_wq;
876 struct bio *bio;
877 struct list_head *next;
878 int error;
879 int was_empty;
880
881 while(1) {
882 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
883 if (list_empty(&fs_info->end_io_work_list)) {
884 spin_unlock_irqrestore(&fs_info->end_io_work_lock,
885 flags);
886 return;
887 }
888 next = fs_info->end_io_work_list.next;
889 list_del(next);
890 spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
891
892 end_io_wq = list_entry(next, struct end_io_wq, list);
893
894 bio = end_io_wq->bio;
Chris Mason22c59942008-04-09 16:28:12 -0400895 if (end_io_wq->metadata && !bio_ready_for_csum(bio)) {
Chris Masonce9adaa2008-04-09 16:28:12 -0400896 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
897 was_empty = list_empty(&fs_info->end_io_work_list);
898 list_add_tail(&end_io_wq->list,
899 &fs_info->end_io_work_list);
900 spin_unlock_irqrestore(&fs_info->end_io_work_lock,
901 flags);
902 if (was_empty)
903 return;
904 continue;
905 }
906 error = end_io_wq->error;
907 bio->bi_private = end_io_wq->private;
908 bio->bi_end_io = end_io_wq->end_io;
909 kfree(end_io_wq);
910 bio_endio(bio, error);
911 }
912}
913
914
Chris Mason8a4b83c2008-03-24 15:02:07 -0400915struct btrfs_root *open_ctree(struct super_block *sb,
916 struct btrfs_fs_devices *fs_devices)
Chris Masoneb60cea2007-02-02 09:18:22 -0500917{
Chris Masondb945352007-10-15 16:15:53 -0400918 u32 sectorsize;
919 u32 nodesize;
920 u32 leafsize;
921 u32 blocksize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500922 u32 stripesize;
Chris Masone20d96d2007-03-22 12:13:20 -0400923 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
924 GFP_NOFS);
925 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
926 GFP_NOFS);
Chris Mason8790d502008-04-03 16:29:03 -0400927 struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
Chris Masone20d96d2007-03-22 12:13:20 -0400928 GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -0400929 struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
930 GFP_NOFS);
931 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
932 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500933 int ret;
Yane58ca022008-04-01 11:21:34 -0400934 int err = -EINVAL;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400935 struct btrfs_super_block *disk_super;
Chris Mason8790d502008-04-03 16:29:03 -0400936
Chris Mason39279cc2007-06-12 06:35:45 -0400937 if (!extent_root || !tree_root || !fs_info) {
938 err = -ENOMEM;
939 goto fail;
940 }
Chris Masonce9adaa2008-04-09 16:28:12 -0400941 end_io_workqueue = create_workqueue("btrfs-end-io");
942 BUG_ON(!end_io_workqueue);
943
Chris Mason0f7d52f2007-04-09 10:42:37 -0400944 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason8fd17792007-04-19 21:01:03 -0400945 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -0400946 INIT_LIST_HEAD(&fs_info->dead_roots);
Chris Mason19c00dd2007-10-15 16:19:22 -0400947 INIT_LIST_HEAD(&fs_info->hashers);
Chris Masonce9adaa2008-04-09 16:28:12 -0400948 INIT_LIST_HEAD(&fs_info->end_io_work_list);
Chris Mason19c00dd2007-10-15 16:19:22 -0400949 spin_lock_init(&fs_info->hash_lock);
Chris Masonce9adaa2008-04-09 16:28:12 -0400950 spin_lock_init(&fs_info->end_io_work_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -0500951 spin_lock_init(&fs_info->delalloc_lock);
Chris Masoncee36a02008-01-15 08:40:48 -0500952 spin_lock_init(&fs_info->new_trans_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -0400953
Josef Bacik58176a92007-08-29 15:47:34 -0400954 init_completion(&fs_info->kobj_unregister);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400955 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400956 fs_info->tree_root = tree_root;
957 fs_info->extent_root = extent_root;
Chris Mason0b86a832008-03-24 15:01:56 -0400958 fs_info->chunk_root = chunk_root;
959 fs_info->dev_root = dev_root;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400960 fs_info->fs_devices = fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -0400961 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
Chris Mason6324fbf2008-03-24 15:01:59 -0400962 INIT_LIST_HEAD(&fs_info->space_info);
Chris Mason0b86a832008-03-24 15:01:56 -0400963 btrfs_mapping_init(&fs_info->mapping_tree);
Chris Masone20d96d2007-03-22 12:13:20 -0400964 fs_info->sb = sb;
Chris Masonc59f8952007-12-17 20:14:04 -0500965 fs_info->max_extent = (u64)-1;
Chris Mason6f568d32008-01-29 16:03:38 -0500966 fs_info->max_inline = 8192 * 1024;
Chris Mason04160082008-03-26 10:28:07 -0400967 setup_bdi(fs_info, &fs_info->bdi);
Chris Masond98237b2007-03-28 13:57:48 -0400968 fs_info->btree_inode = new_inode(sb);
969 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400970 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400971 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
972 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason04160082008-03-26 10:28:07 -0400973 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
974
Chris Masond1310b22008-01-24 16:13:08 -0500975 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -0400976 fs_info->btree_inode->i_mapping,
977 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500978 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
979 GFP_NOFS);
Chris Mason0da54682007-11-07 21:08:01 -0500980
Chris Masond1310b22008-01-24 16:13:08 -0500981 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
982
983 extent_io_tree_init(&fs_info->free_space_cache,
Chris Masonf510cfe2007-10-15 16:14:48 -0400984 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500985 extent_io_tree_init(&fs_info->block_group_cache,
Chris Mason96b51792007-10-15 16:15:19 -0400986 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500987 extent_io_tree_init(&fs_info->pinned_extents,
Chris Mason1a5bc162007-10-15 16:15:26 -0400988 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500989 extent_io_tree_init(&fs_info->pending_del,
Chris Mason1a5bc162007-10-15 16:15:26 -0400990 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500991 extent_io_tree_init(&fs_info->extent_ins,
Chris Mason1a5bc162007-10-15 16:15:26 -0400992 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masone66f7092007-04-20 13:16:02 -0400993 fs_info->do_barriers = 1;
Chris Masone18e4802008-01-18 10:54:22 -0500994
Chris Masonce9adaa2008-04-09 16:28:12 -0400995 INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum);
Chris Mason6da6aba2007-12-18 16:15:09 -0500996#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
997 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
998#else
Chris Mason08607c12007-06-08 15:33:54 -0400999 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason6da6aba2007-12-18 16:15:09 -05001000#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -04001001 BTRFS_I(fs_info->btree_inode)->root = tree_root;
1002 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
1003 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -04001004 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -04001005 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001006
Chris Mason79154b12007-03-22 15:59:16 -04001007 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -04001008 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -04001009
Chris Mason19c00dd2007-10-15 16:19:22 -04001010#if 0
1011 ret = add_hasher(fs_info, "crc32c");
1012 if (ret) {
1013 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
1014 err = -ENOMEM;
1015 goto fail_iput;
1016 }
1017#endif
Chris Mason0b86a832008-03-24 15:01:56 -04001018 __setup_root(4096, 4096, 4096, 4096, tree_root,
Chris Mason2c90e5d2007-04-02 10:50:19 -04001019 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -04001020
Chris Mason2c90e5d2007-04-02 10:50:19 -04001021 fs_info->sb_buffer = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -04001022 BTRFS_SUPER_INFO_OFFSET,
Chris Mason0b86a832008-03-24 15:01:56 -04001023 4096);
Chris Masond98237b2007-03-28 13:57:48 -04001024
Chris Mason0f7d52f2007-04-09 10:42:37 -04001025 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -04001026 goto fail_iput;
Chris Mason39279cc2007-06-12 06:35:45 -04001027
Chris Mason5f39d392007-10-15 16:14:19 -04001028 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
1029 sizeof(fs_info->super_copy));
1030
1031 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
1032 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
1033 BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001034
Chris Mason5f39d392007-10-15 16:14:19 -04001035 disk_super = &fs_info->super_copy;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001036 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -04001037 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001038
Chris Mason8a4b83c2008-03-24 15:02:07 -04001039 if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
1040 printk("Btrfs: wanted %llu devices, but found %llu\n",
1041 (unsigned long long)btrfs_super_num_devices(disk_super),
1042 (unsigned long long)fs_devices->num_devices);
1043 goto fail_sb_buffer;
1044 }
Chris Masondb945352007-10-15 16:15:53 -04001045 nodesize = btrfs_super_nodesize(disk_super);
1046 leafsize = btrfs_super_leafsize(disk_super);
1047 sectorsize = btrfs_super_sectorsize(disk_super);
Chris Mason87ee04e2007-11-30 11:30:34 -05001048 stripesize = btrfs_super_stripesize(disk_super);
Chris Masondb945352007-10-15 16:15:53 -04001049 tree_root->nodesize = nodesize;
1050 tree_root->leafsize = leafsize;
1051 tree_root->sectorsize = sectorsize;
Chris Mason87ee04e2007-11-30 11:30:34 -05001052 tree_root->stripesize = stripesize;
Chris Masonff79f812007-10-15 16:22:25 -04001053 sb_set_blocksize(sb, sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04001054
Chris Mason8352d8a2007-04-12 10:43:05 -04001055 i_size_write(fs_info->btree_inode,
Chris Masondb945352007-10-15 16:15:53 -04001056 btrfs_super_total_bytes(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -04001057
Chris Mason39279cc2007-06-12 06:35:45 -04001058 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
1059 sizeof(disk_super->magic))) {
1060 printk("btrfs: valid FS not found on %s\n", sb->s_id);
1061 goto fail_sb_buffer;
1062 }
Chris Mason19c00dd2007-10-15 16:19:22 -04001063
Chris Mason0b86a832008-03-24 15:01:56 -04001064 mutex_lock(&fs_info->fs_mutex);
Chris Mason0d81ba52008-03-24 15:02:07 -04001065
Chris Mason0b86a832008-03-24 15:01:56 -04001066 ret = btrfs_read_sys_array(tree_root);
1067 BUG_ON(ret);
1068
1069 blocksize = btrfs_level_size(tree_root,
1070 btrfs_super_chunk_root_level(disk_super));
1071
1072 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1073 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1074
1075 chunk_root->node = read_tree_block(chunk_root,
1076 btrfs_super_chunk_root(disk_super),
1077 blocksize);
1078 BUG_ON(!chunk_root->node);
1079
1080 ret = btrfs_read_chunk_tree(chunk_root);
1081 BUG_ON(ret);
1082
Chris Masondb945352007-10-15 16:15:53 -04001083 blocksize = btrfs_level_size(tree_root,
1084 btrfs_super_root_level(disk_super));
Chris Mason19c00dd2007-10-15 16:19:22 -04001085
Chris Mason0b86a832008-03-24 15:01:56 -04001086
Chris Masone20d96d2007-03-22 12:13:20 -04001087 tree_root->node = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -04001088 btrfs_super_root(disk_super),
1089 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04001090 if (!tree_root->node)
1091 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -04001092
Chris Masondb945352007-10-15 16:15:53 -04001093
1094 ret = find_and_setup_root(tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -04001095 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason0b86a832008-03-24 15:01:56 -04001096 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001097 goto fail_tree_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001098 extent_root->track_dirty = 1;
1099
1100 ret = find_and_setup_root(tree_root, fs_info,
1101 BTRFS_DEV_TREE_OBJECTID, dev_root);
1102 dev_root->track_dirty = 1;
1103
1104 if (ret)
1105 goto fail_extent_root;
Chris Mason3768f362007-03-13 16:47:54 -04001106
Chris Mason9078a3e2007-04-26 16:46:15 -04001107 btrfs_read_block_groups(extent_root);
1108
Chris Mason0f7d52f2007-04-09 10:42:37 -04001109 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Masond18a2c42008-04-04 15:40:00 -04001110 fs_info->data_alloc_profile = (u64)-1;
1111 fs_info->metadata_alloc_profile = (u64)-1;
1112 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1113
Chris Mason5be6f7f2007-04-05 13:35:25 -04001114 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001115 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -04001116
Chris Mason0b86a832008-03-24 15:01:56 -04001117fail_extent_root:
1118 free_extent_buffer(extent_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -04001119fail_tree_root:
Chris Mason0b86a832008-03-24 15:01:56 -04001120 mutex_unlock(&fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001121 free_extent_buffer(tree_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -04001122fail_sb_buffer:
Chris Mason5f39d392007-10-15 16:14:19 -04001123 free_extent_buffer(fs_info->sb_buffer);
Chris Mason2d2ae542008-03-26 16:24:23 -04001124 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04001125fail_iput:
1126 iput(fs_info->btree_inode);
1127fail:
Chris Mason8a4b83c2008-03-24 15:02:07 -04001128 close_all_devices(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04001129 kfree(extent_root);
1130 kfree(tree_root);
Chris Mason2d2ae542008-03-26 16:24:23 -04001131 bdi_destroy(&fs_info->bdi);
Chris Mason39279cc2007-06-12 06:35:45 -04001132 kfree(fs_info);
1133 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -05001134}
1135
Chris Masone089f052007-03-16 16:20:31 -04001136int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -04001137 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -05001138{
Chris Masone66f7092007-04-20 13:16:02 -04001139 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001140 struct extent_buffer *super = root->fs_info->sb_buffer;
1141 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason21ad10c2008-01-09 09:23:21 -05001142 struct super_block *sb = root->fs_info->sb;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001143
Chris Mason21ad10c2008-01-09 09:23:21 -05001144 if (!btrfs_test_opt(root, NOBARRIER))
1145 blkdev_issue_flush(sb->s_bdev, NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001146 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
Chris Mason5f39d392007-10-15 16:14:19 -04001147 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
1148 super->start, super->len);
Chris Mason21ad10c2008-01-09 09:23:21 -05001149 if (!btrfs_test_opt(root, NOBARRIER))
1150 blkdev_issue_flush(sb->s_bdev, NULL);
Chris Mason5f39d392007-10-15 16:14:19 -04001151 return ret;
Chris Masoncfaa7292007-02-21 17:04:57 -05001152}
1153
Chris Mason5eda7b52007-06-22 14:16:25 -04001154int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -04001155{
1156 radix_tree_delete(&fs_info->fs_roots_radix,
1157 (unsigned long)root->root_key.objectid);
Chris Masonb99aa6c2008-01-14 14:41:16 -05001158 if (root->in_sysfs)
1159 btrfs_sysfs_del_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -04001160 if (root->inode)
1161 iput(root->inode);
1162 if (root->node)
Chris Mason5f39d392007-10-15 16:14:19 -04001163 free_extent_buffer(root->node);
Chris Mason2619ba12007-04-10 16:58:11 -04001164 if (root->commit_root)
Chris Mason5f39d392007-10-15 16:14:19 -04001165 free_extent_buffer(root->commit_root);
Josef Bacik58176a92007-08-29 15:47:34 -04001166 if (root->name)
1167 kfree(root->name);
Chris Mason2619ba12007-04-10 16:58:11 -04001168 kfree(root);
1169 return 0;
1170}
1171
Chris Mason35b7e472007-05-02 15:53:43 -04001172static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -04001173{
1174 int ret;
1175 struct btrfs_root *gang[8];
1176 int i;
1177
1178 while(1) {
1179 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
1180 (void **)gang, 0,
1181 ARRAY_SIZE(gang));
1182 if (!ret)
1183 break;
Chris Mason2619ba12007-04-10 16:58:11 -04001184 for (i = 0; i < ret; i++)
Chris Mason5eda7b52007-06-22 14:16:25 -04001185 btrfs_free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001186 }
1187 return 0;
1188}
Chris Masonb4100d62007-04-12 12:14:00 -04001189
Chris Masone20d96d2007-03-22 12:13:20 -04001190int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -05001191{
Chris Mason3768f362007-03-13 16:47:54 -04001192 int ret;
Chris Masone089f052007-03-16 16:20:31 -04001193 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001194 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -04001195
Chris Masonfacda1e2007-06-08 18:11:48 -04001196 fs_info->closing = 1;
Chris Mason08607c12007-06-08 15:33:54 -04001197 btrfs_transaction_flush_work(root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001198 mutex_lock(&fs_info->fs_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -04001199 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason79154b12007-03-22 15:59:16 -04001200 trans = btrfs_start_transaction(root, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001201 ret = btrfs_commit_transaction(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -04001202 /* run commit again to drop the original snapshot */
1203 trans = btrfs_start_transaction(root, 1);
1204 btrfs_commit_transaction(trans, root);
1205 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -04001206 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -04001207 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001208 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001209
Chris Masonb0c68f82008-01-31 11:05:37 -05001210 if (fs_info->delalloc_bytes) {
1211 printk("btrfs: at unmount delalloc count %Lu\n",
1212 fs_info->delalloc_bytes);
1213 }
Chris Mason0f7d52f2007-04-09 10:42:37 -04001214 if (fs_info->extent_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -04001215 free_extent_buffer(fs_info->extent_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -04001216
Chris Mason0f7d52f2007-04-09 10:42:37 -04001217 if (fs_info->tree_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -04001218 free_extent_buffer(fs_info->tree_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -04001219
Chris Mason0b86a832008-03-24 15:01:56 -04001220 if (root->fs_info->chunk_root->node);
1221 free_extent_buffer(root->fs_info->chunk_root->node);
1222
1223 if (root->fs_info->dev_root->node);
1224 free_extent_buffer(root->fs_info->dev_root->node);
1225
Chris Mason5f39d392007-10-15 16:14:19 -04001226 free_extent_buffer(fs_info->sb_buffer);
Chris Mason7eccb902007-04-11 15:53:25 -04001227
Chris Mason9078a3e2007-04-26 16:46:15 -04001228 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001229 del_fs_roots(fs_info);
Chris Masond10c5f32007-12-17 20:14:04 -05001230
1231 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
1232
Chris Masond1310b22008-01-24 16:13:08 -05001233 extent_io_tree_empty_lru(&fs_info->free_space_cache);
1234 extent_io_tree_empty_lru(&fs_info->block_group_cache);
1235 extent_io_tree_empty_lru(&fs_info->pinned_extents);
1236 extent_io_tree_empty_lru(&fs_info->pending_del);
1237 extent_io_tree_empty_lru(&fs_info->extent_ins);
1238 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
Chris Masond10c5f32007-12-17 20:14:04 -05001239
Chris Masondb945352007-10-15 16:15:53 -04001240 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
Chris Masonce9adaa2008-04-09 16:28:12 -04001241 flush_workqueue(end_io_workqueue);
1242 destroy_workqueue(end_io_workqueue);
Chris Masond10c5f32007-12-17 20:14:04 -05001243
Chris Masondb945352007-10-15 16:15:53 -04001244 iput(fs_info->btree_inode);
Chris Mason19c00dd2007-10-15 16:19:22 -04001245#if 0
1246 while(!list_empty(&fs_info->hashers)) {
1247 struct btrfs_hasher *hasher;
1248 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
1249 hashers);
1250 list_del(&hasher->hashers);
1251 crypto_free_hash(&fs_info->hash_tfm);
1252 kfree(hasher);
1253 }
1254#endif
Chris Mason0b86a832008-03-24 15:01:56 -04001255 close_all_devices(fs_info);
1256 btrfs_mapping_tree_free(&fs_info->mapping_tree);
Chris Mason04160082008-03-26 10:28:07 -04001257 bdi_destroy(&fs_info->bdi);
Chris Mason0b86a832008-03-24 15:01:56 -04001258
Chris Mason0f7d52f2007-04-09 10:42:37 -04001259 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001260 kfree(fs_info->tree_root);
Chris Mason0b86a832008-03-24 15:01:56 -04001261 kfree(fs_info->chunk_root);
1262 kfree(fs_info->dev_root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001263 return 0;
1264}
1265
Chris Mason5f39d392007-10-15 16:14:19 -04001266int btrfs_buffer_uptodate(struct extent_buffer *buf)
Chris Masonccd467d2007-06-28 15:57:36 -04001267{
Chris Mason810191f2007-10-15 16:18:55 -04001268 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001269 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001270}
Chris Mason6702ed42007-08-07 16:15:09 -04001271
Chris Mason5f39d392007-10-15 16:14:19 -04001272int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
1273{
Chris Mason810191f2007-10-15 16:18:55 -04001274 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001275 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -04001276 buf);
1277}
1278
1279void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
1280{
Chris Mason810191f2007-10-15 16:18:55 -04001281 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04001282 u64 transid = btrfs_header_generation(buf);
1283 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason6702ed42007-08-07 16:15:09 -04001284
Chris Masonccd467d2007-06-28 15:57:36 -04001285 if (transid != root->fs_info->generation) {
1286 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
Chris Masondb945352007-10-15 16:15:53 -04001287 (unsigned long long)buf->start,
Chris Masonccd467d2007-06-28 15:57:36 -04001288 transid, root->fs_info->generation);
1289 WARN_ON(1);
1290 }
Chris Masond1310b22008-01-24 16:13:08 -05001291 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001292}
1293
Chris Masone2008b62008-01-08 15:46:30 -05001294void btrfs_throttle(struct btrfs_root *root)
1295{
Chris Mason55c69072008-01-09 15:55:33 -05001296 struct backing_dev_info *bdi;
1297
1298 bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
Chris Mason04005cc2008-01-17 12:01:41 -05001299 if (root->fs_info->throttles && bdi_write_congested(bdi)) {
1300#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
Chris Mason55c69072008-01-09 15:55:33 -05001301 congestion_wait(WRITE, HZ/20);
Chris Mason04005cc2008-01-17 12:01:41 -05001302#else
1303 blk_congestion_wait(WRITE, HZ/20);
1304#endif
1305 }
Chris Masone2008b62008-01-08 15:46:30 -05001306}
1307
Chris Masond3c2fdc2007-09-17 10:58:06 -04001308void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
Chris Mason35b7e472007-05-02 15:53:43 -04001309{
Chris Masond3c2fdc2007-09-17 10:58:06 -04001310 balance_dirty_pages_ratelimited_nr(
Chris Masond7fc6402008-02-18 12:12:38 -05001311 root->fs_info->btree_inode->i_mapping, 1);
Chris Mason35b7e472007-05-02 15:53:43 -04001312}
Chris Mason6b800532007-10-15 16:17:34 -04001313
1314void btrfs_set_buffer_defrag(struct extent_buffer *buf)
1315{
Chris Mason810191f2007-10-15 16:18:55 -04001316 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001317 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001318 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
Chris Mason6b800532007-10-15 16:17:34 -04001319 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
1320}
1321
1322void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
1323{
Chris Mason810191f2007-10-15 16:18:55 -04001324 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001325 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001326 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
Chris Mason6b800532007-10-15 16:17:34 -04001327 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
1328 GFP_NOFS);
1329}
1330
1331int btrfs_buffer_defrag(struct extent_buffer *buf)
1332{
Chris Mason810191f2007-10-15 16:18:55 -04001333 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001334 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001335 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001336 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
1337}
1338
1339int btrfs_buffer_defrag_done(struct extent_buffer *buf)
1340{
Chris Mason810191f2007-10-15 16:18:55 -04001341 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001342 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001343 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001344 buf->start, buf->start + buf->len - 1,
1345 EXTENT_DEFRAG_DONE, 0);
1346}
1347
1348int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
1349{
Chris Mason810191f2007-10-15 16:18:55 -04001350 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001351 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001352 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001353 buf->start, buf->start + buf->len - 1,
1354 EXTENT_DEFRAG_DONE, GFP_NOFS);
1355}
1356
1357int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
1358{
Chris Mason810191f2007-10-15 16:18:55 -04001359 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001360 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001361 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001362 buf->start, buf->start + buf->len - 1,
1363 EXTENT_DEFRAG, GFP_NOFS);
1364}
1365
1366int btrfs_read_buffer(struct extent_buffer *buf)
1367{
Chris Mason810191f2007-10-15 16:18:55 -04001368 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001369 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonce9adaa2008-04-09 16:28:12 -04001370 int ret;
1371 ret = read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
Chris Masona86c12c2008-02-07 10:50:54 -05001372 buf, 0, 1, btree_get_extent);
Chris Masonce9adaa2008-04-09 16:28:12 -04001373 if (ret == 0) {
1374 buf->flags |= EXTENT_UPTODATE;
1375 }
1376 return ret;
Chris Mason6b800532007-10-15 16:17:34 -04001377}
Chris Mason0da54682007-11-07 21:08:01 -05001378
Chris Masond1310b22008-01-24 16:13:08 -05001379static struct extent_io_ops btree_extent_io_ops = {
Chris Mason0da54682007-11-07 21:08:01 -05001380 .writepage_io_hook = btree_writepage_io_hook,
Chris Masonce9adaa2008-04-09 16:28:12 -04001381 .readpage_end_io_hook = btree_readpage_end_io_hook,
Chris Mason0b86a832008-03-24 15:01:56 -04001382 .submit_bio_hook = btree_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04001383 /* note we're sharing with inode.c for the merge bio hook */
1384 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason0da54682007-11-07 21:08:01 -05001385};