blob: e40fb318ad99e16d10393e5fb6af601708b836fb [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masone20d96d2007-03-22 12:13:20 -040019#include <linux/fs.h>
Chris Masond98237b2007-03-28 13:57:48 -040020#include <linux/blkdev.h>
Chris Mason11bd1432007-06-22 14:16:24 -040021#include <linux/crc32c.h>
Chris Mason87cbda52007-03-28 19:44:27 -040022#include <linux/scatterlist.h>
Chris Mason22b0ebd2007-03-30 08:47:31 -040023#include <linux/swap.h>
Chris Mason0f7d52f2007-04-09 10:42:37 -040024#include <linux/radix-tree.h>
Chris Mason35b7e472007-05-02 15:53:43 -040025#include <linux/writeback.h>
Chris Mason5f39d392007-10-15 16:14:19 -040026#include <linux/buffer_head.h> // for block_sync_page
Chris Masoneb60cea2007-02-02 09:18:22 -050027#include "ctree.h"
28#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -040029#include "transaction.h"
Chris Mason0f7d52f2007-04-09 10:42:37 -040030#include "btrfs_inode.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "volumes.h"
Chris Masondb945352007-10-15 16:15:53 -040032#include "print-tree.h"
Chris Masoneb60cea2007-02-02 09:18:22 -050033
Chris Mason5f39d392007-10-15 16:14:19 -040034#if 0
35static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
Chris Mason7eccb902007-04-11 15:53:25 -040036{
Chris Mason5f39d392007-10-15 16:14:19 -040037 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
38 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
39 (unsigned long long)extent_buffer_blocknr(buf),
40 (unsigned long long)btrfs_header_blocknr(buf));
Chris Mason39279cc2007-06-12 06:35:45 -040041 return 1;
Chris Masond98237b2007-03-28 13:57:48 -040042 }
Chris Mason9a8dd152007-02-23 08:38:36 -050043 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050044}
Chris Mason5f39d392007-10-15 16:14:19 -040045#endif
Chris Masoneb60cea2007-02-02 09:18:22 -050046
Chris Masond1310b22008-01-24 16:13:08 -050047static struct extent_io_ops btree_extent_io_ops;
Chris Mason0da54682007-11-07 21:08:01 -050048
Chris Mason5f39d392007-10-15 16:14:19 -040049struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
Chris Masond1310b22008-01-24 16:13:08 -050050 size_t page_offset, u64 start, u64 len,
Chris Mason5f39d392007-10-15 16:14:19 -040051 int create)
52{
53 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
54 struct extent_map *em;
55 int ret;
Chris Masond98237b2007-03-28 13:57:48 -040056
Chris Mason5f39d392007-10-15 16:14:19 -040057again:
Chris Masond1310b22008-01-24 16:13:08 -050058 spin_lock(&em_tree->lock);
59 em = lookup_extent_mapping(em_tree, start, len);
60 spin_unlock(&em_tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -040061 if (em) {
62 goto out;
63 }
64 em = alloc_extent_map(GFP_NOFS);
65 if (!em) {
66 em = ERR_PTR(-ENOMEM);
67 goto out;
68 }
69 em->start = 0;
Chris Masond1310b22008-01-24 16:13:08 -050070 em->len = i_size_read(inode);
Chris Mason5f39d392007-10-15 16:14:19 -040071 em->block_start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -040072 em->bdev = inode->i_sb->s_bdev;
Chris Masond1310b22008-01-24 16:13:08 -050073
74 spin_lock(&em_tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -040075 ret = add_extent_mapping(em_tree, em);
Chris Masond1310b22008-01-24 16:13:08 -050076 spin_unlock(&em_tree->lock);
77
Chris Mason5f39d392007-10-15 16:14:19 -040078 if (ret == -EEXIST) {
79 free_extent_map(em);
80 em = NULL;
81 goto again;
82 } else if (ret) {
83 em = ERR_PTR(ret);
84 }
85out:
86 return em;
87}
88
Chris Mason19c00dd2007-10-15 16:19:22 -040089u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
90{
91 return crc32c(seed, data, len);
92}
93
94void btrfs_csum_final(u32 crc, char *result)
95{
96 *(__le32 *)result = ~cpu_to_le32(crc);
97}
98
99static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
100 int verify)
101{
102 char result[BTRFS_CRC32_SIZE];
103 unsigned long len;
104 unsigned long cur_len;
105 unsigned long offset = BTRFS_CSUM_SIZE;
106 char *map_token = NULL;
107 char *kaddr;
108 unsigned long map_start;
109 unsigned long map_len;
110 int err;
111 u32 crc = ~(u32)0;
112
113 len = buf->len - offset;
114 while(len > 0) {
115 err = map_private_extent_buffer(buf, offset, 32,
116 &map_token, &kaddr,
117 &map_start, &map_len, KM_USER0);
118 if (err) {
119 printk("failed to map extent buffer! %lu\n",
120 offset);
121 return 1;
122 }
123 cur_len = min(len, map_len - (offset - map_start));
124 crc = btrfs_csum_data(root, kaddr + offset - map_start,
125 crc, cur_len);
126 len -= cur_len;
127 offset += cur_len;
128 unmap_extent_buffer(buf, map_token, KM_USER0);
129 }
130 btrfs_csum_final(crc, result);
131
132 if (verify) {
Chris Masone4204de2008-01-08 15:46:27 -0500133 int from_this_trans = 0;
134
135 if (root->fs_info->running_transaction &&
136 btrfs_header_generation(buf) ==
137 root->fs_info->running_transaction->transid)
138 from_this_trans = 1;
139
140 /* FIXME, this is not good */
Chris Mason63b10fc2008-04-01 11:21:32 -0400141 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
Chris Masone4204de2008-01-08 15:46:27 -0500142 u32 val;
143 u32 found = 0;
144 memcpy(&found, result, BTRFS_CRC32_SIZE);
145
146 read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
Chris Mason63b10fc2008-04-01 11:21:32 -0400147 WARN_ON(1);
Chris Masone4204de2008-01-08 15:46:27 -0500148 printk("btrfs: %s checksum verify failed on %llu "
Chris Mason63b10fc2008-04-01 11:21:32 -0400149 "wanted %X found %X from_this_trans %d "
150 "level %d\n",
Chris Mason19c00dd2007-10-15 16:19:22 -0400151 root->fs_info->sb->s_id,
Chris Mason63b10fc2008-04-01 11:21:32 -0400152 buf->start, val, found, from_this_trans,
153 btrfs_header_level(buf));
Chris Mason19c00dd2007-10-15 16:19:22 -0400154 return 1;
155 }
156 } else {
157 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
158 }
159 return 0;
160}
161
162
163int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
164{
Chris Masond1310b22008-01-24 16:13:08 -0500165 struct extent_io_tree *tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400166 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason19c00dd2007-10-15 16:19:22 -0400167 u64 found_start;
168 int found_level;
169 unsigned long len;
170 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -0500171 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason19c00dd2007-10-15 16:19:22 -0400172
173 if (page->private == EXTENT_PAGE_PRIVATE)
174 goto out;
175 if (!page->private)
176 goto out;
177 len = page->private >> 2;
178 if (len == 0) {
179 WARN_ON(1);
180 }
181 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
Chris Masona86c12c2008-02-07 10:50:54 -0500182 read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1,
183 btree_get_extent);
Chris Masone18e4802008-01-18 10:54:22 -0500184 btrfs_clear_buffer_defrag(eb);
Chris Mason19c00dd2007-10-15 16:19:22 -0400185 found_start = btrfs_header_bytenr(eb);
186 if (found_start != start) {
187 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
188 start, found_start, len);
Chris Mason55c69072008-01-09 15:55:33 -0500189 WARN_ON(1);
190 goto err;
191 }
192 if (eb->first_page != page) {
193 printk("bad first page %lu %lu\n", eb->first_page->index,
194 page->index);
195 WARN_ON(1);
196 goto err;
197 }
198 if (!PageUptodate(page)) {
199 printk("csum not up to date page %lu\n", page->index);
200 WARN_ON(1);
201 goto err;
Chris Mason19c00dd2007-10-15 16:19:22 -0400202 }
203 found_level = btrfs_header_level(eb);
Chris Mason63b10fc2008-04-01 11:21:32 -0400204 spin_lock(&root->fs_info->hash_lock);
205 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
206 spin_unlock(&root->fs_info->hash_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -0400207 csum_tree_block(root, eb, 0);
Chris Mason55c69072008-01-09 15:55:33 -0500208err:
Chris Mason19c00dd2007-10-15 16:19:22 -0400209 free_extent_buffer(eb);
210out:
211 return 0;
212}
213
Chris Mason0da54682007-11-07 21:08:01 -0500214static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
215{
216 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
217
218 csum_dirty_buffer(root, page);
219 return 0;
220}
221
Chris Mason0b86a832008-03-24 15:01:56 -0400222static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio)
223{
224 struct btrfs_root *root = BTRFS_I(inode)->root;
225 u64 offset;
226 offset = bio->bi_sector << 9;
227 if (offset == BTRFS_SUPER_INFO_OFFSET) {
228 bio->bi_bdev = root->fs_info->sb->s_bdev;
229 submit_bio(rw, bio);
230 return 0;
231 }
232 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
233}
234
Chris Mason5f39d392007-10-15 16:14:19 -0400235static int btree_writepage(struct page *page, struct writeback_control *wbc)
236{
Chris Masond1310b22008-01-24 16:13:08 -0500237 struct extent_io_tree *tree;
238 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400239 return extent_write_full_page(tree, page, btree_get_extent, wbc);
240}
Chris Mason0da54682007-11-07 21:08:01 -0500241
242static int btree_writepages(struct address_space *mapping,
243 struct writeback_control *wbc)
244{
Chris Masond1310b22008-01-24 16:13:08 -0500245 struct extent_io_tree *tree;
246 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500247 if (wbc->sync_mode == WB_SYNC_NONE) {
Chris Mason793955b2007-11-26 16:34:41 -0800248 u64 num_dirty;
249 u64 start = 0;
250 unsigned long thresh = 96 * 1024 * 1024;
Chris Mason448d6402007-11-27 07:52:01 -0800251
252 if (wbc->for_kupdate)
253 return 0;
254
Chris Masonca664622007-11-27 11:16:35 -0500255 if (current_is_pdflush()) {
256 thresh = 96 * 1024 * 1024;
257 } else {
258 thresh = 8 * 1024 * 1024;
259 }
Chris Mason1832a6d2007-12-21 16:27:21 -0500260 num_dirty = count_range_bits(tree, &start, (u64)-1,
261 thresh, EXTENT_DIRTY);
Chris Mason793955b2007-11-26 16:34:41 -0800262 if (num_dirty < thresh) {
263 return 0;
264 }
265 }
Chris Mason0da54682007-11-07 21:08:01 -0500266 return extent_writepages(tree, mapping, btree_get_extent, wbc);
267}
268
Chris Mason5f39d392007-10-15 16:14:19 -0400269int btree_readpage(struct file *file, struct page *page)
270{
Chris Masond1310b22008-01-24 16:13:08 -0500271 struct extent_io_tree *tree;
272 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400273 return extent_read_full_page(tree, page, btree_get_extent);
274}
275
Chris Mason70dec802008-01-29 09:59:12 -0500276static int btree_releasepage(struct page *page, gfp_t gfp_flags)
Chris Mason5f39d392007-10-15 16:14:19 -0400277{
Chris Masond1310b22008-01-24 16:13:08 -0500278 struct extent_io_tree *tree;
279 struct extent_map_tree *map;
Chris Mason5f39d392007-10-15 16:14:19 -0400280 int ret;
281
Chris Masond1310b22008-01-24 16:13:08 -0500282 tree = &BTRFS_I(page->mapping->host)->io_tree;
283 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -0500284 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400285 if (ret == 1) {
286 ClearPagePrivate(page);
287 set_page_private(page, 0);
288 page_cache_release(page);
289 }
Chris Masond98237b2007-03-28 13:57:48 -0400290 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400291}
Chris Mason123abc82007-03-14 14:14:43 -0400292
Chris Mason5f39d392007-10-15 16:14:19 -0400293static void btree_invalidatepage(struct page *page, unsigned long offset)
Chris Masond98237b2007-03-28 13:57:48 -0400294{
Chris Masond1310b22008-01-24 16:13:08 -0500295 struct extent_io_tree *tree;
296 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400297 extent_invalidatepage(tree, page, offset);
298 btree_releasepage(page, GFP_NOFS);
Chris Masond98237b2007-03-28 13:57:48 -0400299}
300
Chris Mason5f39d392007-10-15 16:14:19 -0400301#if 0
Chris Masond98237b2007-03-28 13:57:48 -0400302static int btree_writepage(struct page *page, struct writeback_control *wbc)
303{
Chris Mason87cbda52007-03-28 19:44:27 -0400304 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400305 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400306 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400307 if (!page_has_buffers(page)) {
308 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
309 (1 << BH_Dirty)|(1 << BH_Uptodate));
310 }
311 head = page_buffers(page);
312 bh = head;
313 do {
314 if (buffer_dirty(bh))
315 csum_tree_block(root, bh, 0);
316 bh = bh->b_this_page;
317 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400318 return block_write_full_page(page, btree_get_block, wbc);
319}
Chris Mason5f39d392007-10-15 16:14:19 -0400320#endif
Chris Masond98237b2007-03-28 13:57:48 -0400321
322static struct address_space_operations btree_aops = {
323 .readpage = btree_readpage,
324 .writepage = btree_writepage,
Chris Mason0da54682007-11-07 21:08:01 -0500325 .writepages = btree_writepages,
Chris Mason5f39d392007-10-15 16:14:19 -0400326 .releasepage = btree_releasepage,
327 .invalidatepage = btree_invalidatepage,
Chris Masond98237b2007-03-28 13:57:48 -0400328 .sync_page = block_sync_page,
329};
330
Chris Masondb945352007-10-15 16:15:53 -0400331int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
Chris Mason090d1872007-05-01 08:53:32 -0400332{
Chris Mason5f39d392007-10-15 16:14:19 -0400333 struct extent_buffer *buf = NULL;
334 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonde428b62007-05-18 13:28:27 -0400335 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400336
Chris Masondb945352007-10-15 16:15:53 -0400337 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400338 if (!buf)
Chris Mason090d1872007-05-01 08:53:32 -0400339 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500340 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
Chris Masona86c12c2008-02-07 10:50:54 -0500341 buf, 0, 0, btree_get_extent);
Chris Mason5f39d392007-10-15 16:14:19 -0400342 free_extent_buffer(buf);
Chris Masonde428b62007-05-18 13:28:27 -0400343 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400344}
345
Chris Mason0b86a832008-03-24 15:01:56 -0400346static int close_all_devices(struct btrfs_fs_info *fs_info)
347{
348 struct list_head *list;
349 struct list_head *next;
350 struct btrfs_device *device;
351
Chris Mason8a4b83c2008-03-24 15:02:07 -0400352 list = &fs_info->fs_devices->devices;
353 list_for_each(next, list) {
Chris Mason0b86a832008-03-24 15:01:56 -0400354 device = list_entry(next, struct btrfs_device, dev_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400355 if (device->bdev && device->bdev != fs_info->sb->s_bdev)
356 close_bdev_excl(device->bdev);
357 device->bdev = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400358 }
359 return 0;
360}
361
Chris Mason0999df52008-04-01 13:48:14 -0400362int btrfs_verify_block_csum(struct btrfs_root *root,
363 struct extent_buffer *buf)
364{
365 struct extent_io_tree *io_tree;
366 u64 end;
367 int ret;
368
369 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
370 if (buf->flags & EXTENT_CSUM)
371 return 0;
372
373 end = min_t(u64, buf->len, PAGE_CACHE_SIZE);
374 end = buf->start + end - 1;
375 if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
376 buf->flags |= EXTENT_CSUM;
377 return 0;
378 }
379
380 lock_extent(io_tree, buf->start, end, GFP_NOFS);
381
382 if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
383 buf->flags |= EXTENT_CSUM;
384 ret = 0;
385 goto out_unlock;
386 }
387
388 ret = csum_tree_block(root, buf, 1);
389 set_extent_bits(io_tree, buf->start, end, EXTENT_CSUM, GFP_NOFS);
390 buf->flags |= EXTENT_CSUM;
391
392out_unlock:
393 unlock_extent(io_tree, buf->start, end, GFP_NOFS);
394 return ret;
395}
396
397struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
398 u64 bytenr, u32 blocksize)
399{
400 struct inode *btree_inode = root->fs_info->btree_inode;
401 struct extent_buffer *eb;
402 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
403 bytenr, blocksize, GFP_NOFS);
404 return eb;
405}
406
407struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
408 u64 bytenr, u32 blocksize)
409{
410 struct inode *btree_inode = root->fs_info->btree_inode;
411 struct extent_buffer *eb;
412
413 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
414 bytenr, blocksize, NULL, GFP_NOFS);
415 return eb;
416}
417
418
Chris Masondb945352007-10-15 16:15:53 -0400419struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
420 u32 blocksize)
Chris Masone20d96d2007-03-22 12:13:20 -0400421{
Chris Mason5f39d392007-10-15 16:14:19 -0400422 struct extent_buffer *buf = NULL;
423 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -0500424 struct extent_io_tree *io_tree;
Chris Mason19c00dd2007-10-15 16:19:22 -0400425 int ret;
426
Chris Masond1310b22008-01-24 16:13:08 -0500427 io_tree = &BTRFS_I(btree_inode)->io_tree;
Chris Masone20d96d2007-03-22 12:13:20 -0400428
Chris Masondb945352007-10-15 16:15:53 -0400429 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400430 if (!buf)
431 return NULL;
Chris Masona86c12c2008-02-07 10:50:54 -0500432 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 0, 1,
433 btree_get_extent);
Chris Masone4204de2008-01-08 15:46:27 -0500434
Chris Mason0999df52008-04-01 13:48:14 -0400435 ret = btrfs_verify_block_csum(root, buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400436 return buf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500437}
438
Chris Masone089f052007-03-16 16:20:31 -0400439int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400440 struct extent_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500441{
Chris Mason5f39d392007-10-15 16:14:19 -0400442 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason55c69072008-01-09 15:55:33 -0500443 if (btrfs_header_generation(buf) ==
444 root->fs_info->running_transaction->transid)
Chris Masond1310b22008-01-24 16:13:08 -0500445 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
Chris Mason55c69072008-01-09 15:55:33 -0500446 buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400447 return 0;
448}
449
450int wait_on_tree_block_writeback(struct btrfs_root *root,
451 struct extent_buffer *buf)
452{
453 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -0500454 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -0400455 buf);
456 return 0;
457}
458
Chris Masondb945352007-10-15 16:15:53 -0400459static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500460 u32 stripesize, struct btrfs_root *root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400461 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400462 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500463{
Chris Masoncfaa7292007-02-21 17:04:57 -0500464 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400465 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500466 root->commit_root = NULL;
Chris Masondb945352007-10-15 16:15:53 -0400467 root->sectorsize = sectorsize;
468 root->nodesize = nodesize;
469 root->leafsize = leafsize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500470 root->stripesize = stripesize;
Chris Mason123abc82007-03-14 14:14:43 -0400471 root->ref_cows = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400472 root->track_dirty = 0;
473
Chris Mason9f5fae22007-03-20 14:38:32 -0400474 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400475 root->objectid = objectid;
476 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400477 root->highest_inode = 0;
478 root->last_inode_alloc = 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400479 root->name = NULL;
Chris Mason4313b392008-01-03 09:08:48 -0500480 root->in_sysfs = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400481
482 INIT_LIST_HEAD(&root->dirty_list);
Chris Mason3768f362007-03-13 16:47:54 -0400483 memset(&root->root_key, 0, sizeof(root->root_key));
484 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -0400485 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Josef Bacik58176a92007-08-29 15:47:34 -0400486 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
487 init_completion(&root->kobj_unregister);
Chris Mason6702ed42007-08-07 16:15:09 -0400488 root->defrag_running = 0;
489 root->defrag_level = 0;
Chris Mason4d775672007-04-20 20:23:12 -0400490 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400491 return 0;
492}
493
Chris Masondb945352007-10-15 16:15:53 -0400494static int find_and_setup_root(struct btrfs_root *tree_root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400495 struct btrfs_fs_info *fs_info,
496 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400497 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400498{
499 int ret;
Chris Masondb945352007-10-15 16:15:53 -0400500 u32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -0400501
Chris Masondb945352007-10-15 16:15:53 -0400502 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500503 tree_root->sectorsize, tree_root->stripesize,
504 root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400505 ret = btrfs_find_last_root(tree_root, objectid,
506 &root->root_item, &root->root_key);
507 BUG_ON(ret);
508
Chris Masondb945352007-10-15 16:15:53 -0400509 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
510 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
511 blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400512 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500513 return 0;
514}
515
Chris Mason5eda7b52007-06-22 14:16:25 -0400516struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
517 struct btrfs_key *location)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400518{
519 struct btrfs_root *root;
520 struct btrfs_root *tree_root = fs_info->tree_root;
521 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400522 struct extent_buffer *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400523 u64 highest_inode;
Chris Masondb945352007-10-15 16:15:53 -0400524 u32 blocksize;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400525 int ret = 0;
526
Chris Mason5eda7b52007-06-22 14:16:25 -0400527 root = kzalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400528 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400529 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400530 if (location->offset == (u64)-1) {
Chris Masondb945352007-10-15 16:15:53 -0400531 ret = find_and_setup_root(tree_root, fs_info,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400532 location->objectid, root);
533 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400534 kfree(root);
535 return ERR_PTR(ret);
536 }
537 goto insert;
538 }
539
Chris Masondb945352007-10-15 16:15:53 -0400540 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500541 tree_root->sectorsize, tree_root->stripesize,
542 root, fs_info, location->objectid);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400543
544 path = btrfs_alloc_path();
545 BUG_ON(!path);
546 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
547 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400548 if (ret > 0)
549 ret = -ENOENT;
550 goto out;
551 }
Chris Mason5f39d392007-10-15 16:14:19 -0400552 l = path->nodes[0];
553 read_extent_buffer(l, &root->root_item,
554 btrfs_item_ptr_offset(l, path->slots[0]),
Chris Mason0f7d52f2007-04-09 10:42:37 -0400555 sizeof(root->root_item));
Yan Zheng44b36eb2007-10-19 09:22:56 -0400556 memcpy(&root->root_key, location, sizeof(*location));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400557 ret = 0;
558out:
559 btrfs_release_path(root, path);
560 btrfs_free_path(path);
561 if (ret) {
562 kfree(root);
563 return ERR_PTR(ret);
564 }
Chris Masondb945352007-10-15 16:15:53 -0400565 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
566 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
567 blocksize);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400568 BUG_ON(!root->node);
569insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400570 root->ref_cows = 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400571 ret = btrfs_find_highest_inode(root, &highest_inode);
572 if (ret == 0) {
573 root->highest_inode = highest_inode;
574 root->last_inode_alloc = highest_inode;
575 }
576 return root;
577}
578
Chris Masondc17ff82008-01-08 15:46:30 -0500579struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
580 u64 root_objectid)
581{
582 struct btrfs_root *root;
583
584 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
585 return fs_info->tree_root;
586 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
587 return fs_info->extent_root;
588
589 root = radix_tree_lookup(&fs_info->fs_roots_radix,
590 (unsigned long)root_objectid);
591 return root;
592}
593
Chris Masonedbd8d42007-12-21 16:27:24 -0500594struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
595 struct btrfs_key *location)
Chris Mason5eda7b52007-06-22 14:16:25 -0400596{
597 struct btrfs_root *root;
598 int ret;
599
Chris Masonedbd8d42007-12-21 16:27:24 -0500600 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
601 return fs_info->tree_root;
602 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
603 return fs_info->extent_root;
604
Chris Mason5eda7b52007-06-22 14:16:25 -0400605 root = radix_tree_lookup(&fs_info->fs_roots_radix,
606 (unsigned long)location->objectid);
607 if (root)
608 return root;
609
610 root = btrfs_read_fs_root_no_radix(fs_info, location);
611 if (IS_ERR(root))
612 return root;
Chris Mason2619ba12007-04-10 16:58:11 -0400613 ret = radix_tree_insert(&fs_info->fs_roots_radix,
614 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400615 root);
616 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400617 free_extent_buffer(root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400618 kfree(root);
619 return ERR_PTR(ret);
620 }
Chris Masonedbd8d42007-12-21 16:27:24 -0500621 ret = btrfs_find_dead_roots(fs_info->tree_root,
622 root->root_key.objectid, root);
623 BUG_ON(ret);
624
625 return root;
626}
627
628struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
629 struct btrfs_key *location,
630 const char *name, int namelen)
631{
632 struct btrfs_root *root;
633 int ret;
634
635 root = btrfs_read_fs_root_no_name(fs_info, location);
636 if (!root)
637 return NULL;
Josef Bacik58176a92007-08-29 15:47:34 -0400638
Chris Mason4313b392008-01-03 09:08:48 -0500639 if (root->in_sysfs)
640 return root;
641
Josef Bacik58176a92007-08-29 15:47:34 -0400642 ret = btrfs_set_root_name(root, name, namelen);
643 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400644 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400645 kfree(root);
646 return ERR_PTR(ret);
647 }
648
649 ret = btrfs_sysfs_add_root(root);
650 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400651 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400652 kfree(root->name);
653 kfree(root);
654 return ERR_PTR(ret);
655 }
Chris Mason4313b392008-01-03 09:08:48 -0500656 root->in_sysfs = 1;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400657 return root;
658}
Chris Mason19c00dd2007-10-15 16:19:22 -0400659#if 0
660static int add_hasher(struct btrfs_fs_info *info, char *type) {
661 struct btrfs_hasher *hasher;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400662
Chris Mason19c00dd2007-10-15 16:19:22 -0400663 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
664 if (!hasher)
665 return -ENOMEM;
666 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
667 if (!hasher->hash_tfm) {
668 kfree(hasher);
669 return -EINVAL;
670 }
671 spin_lock(&info->hash_lock);
672 list_add(&hasher->list, &info->hashers);
673 spin_unlock(&info->hash_lock);
674 return 0;
675}
676#endif
Chris Mason04160082008-03-26 10:28:07 -0400677
678static int btrfs_congested_fn(void *congested_data, int bdi_bits)
679{
680 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
681 int ret = 0;
682 struct list_head *cur;
683 struct btrfs_device *device;
684 struct backing_dev_info *bdi;
685
686 list_for_each(cur, &info->fs_devices->devices) {
687 device = list_entry(cur, struct btrfs_device, dev_list);
688 bdi = blk_get_backing_dev_info(device->bdev);
689 if (bdi && bdi_congested(bdi, bdi_bits)) {
690 ret = 1;
691 break;
692 }
693 }
694 return ret;
695}
696
697void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
698{
699 struct list_head *cur;
700 struct btrfs_device *device;
701 struct btrfs_fs_info *info;
702
703 info = (struct btrfs_fs_info *)bdi->unplug_io_data;
704 list_for_each(cur, &info->fs_devices->devices) {
705 device = list_entry(cur, struct btrfs_device, dev_list);
706 bdi = blk_get_backing_dev_info(device->bdev);
707 if (bdi->unplug_io_fn) {
708 bdi->unplug_io_fn(bdi, page);
709 }
710 }
711}
712
713static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
714{
715 bdi_init(bdi);
Chris Mason83041ad2008-03-26 12:02:55 -0400716 bdi->ra_pages = default_backing_dev_info.ra_pages * 4;
Chris Mason04160082008-03-26 10:28:07 -0400717 bdi->state = 0;
718 bdi->capabilities = default_backing_dev_info.capabilities;
719 bdi->unplug_io_fn = btrfs_unplug_io_fn;
720 bdi->unplug_io_data = info;
721 bdi->congested_fn = btrfs_congested_fn;
722 bdi->congested_data = info;
723 return 0;
724}
725
Chris Mason8a4b83c2008-03-24 15:02:07 -0400726struct btrfs_root *open_ctree(struct super_block *sb,
727 struct btrfs_fs_devices *fs_devices)
Chris Masoneb60cea2007-02-02 09:18:22 -0500728{
Chris Masondb945352007-10-15 16:15:53 -0400729 u32 sectorsize;
730 u32 nodesize;
731 u32 leafsize;
732 u32 blocksize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500733 u32 stripesize;
Chris Masone20d96d2007-03-22 12:13:20 -0400734 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
735 GFP_NOFS);
736 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
737 GFP_NOFS);
Chris Masone20d96d2007-03-22 12:13:20 -0400738 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
739 GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -0400740 struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
741 GFP_NOFS);
742 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
743 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500744 int ret;
Yane58ca022008-04-01 11:21:34 -0400745 int err = -EINVAL;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400746 struct btrfs_super_block *disk_super;
Chris Mason39279cc2007-06-12 06:35:45 -0400747 if (!extent_root || !tree_root || !fs_info) {
748 err = -ENOMEM;
749 goto fail;
750 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400751 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason8fd17792007-04-19 21:01:03 -0400752 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -0400753 INIT_LIST_HEAD(&fs_info->dead_roots);
Chris Mason19c00dd2007-10-15 16:19:22 -0400754 INIT_LIST_HEAD(&fs_info->hashers);
755 spin_lock_init(&fs_info->hash_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -0500756 spin_lock_init(&fs_info->delalloc_lock);
Chris Masoncee36a02008-01-15 08:40:48 -0500757 spin_lock_init(&fs_info->new_trans_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -0400758
Josef Bacik58176a92007-08-29 15:47:34 -0400759 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
760 init_completion(&fs_info->kobj_unregister);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400761 sb_set_blocksize(sb, 4096);
Chris Mason9f5fae22007-03-20 14:38:32 -0400762 fs_info->running_transaction = NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400763 fs_info->last_trans_committed = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400764 fs_info->tree_root = tree_root;
765 fs_info->extent_root = extent_root;
Chris Mason0b86a832008-03-24 15:01:56 -0400766 fs_info->chunk_root = chunk_root;
767 fs_info->dev_root = dev_root;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400768 fs_info->fs_devices = fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -0400769 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
Chris Mason6324fbf2008-03-24 15:01:59 -0400770 INIT_LIST_HEAD(&fs_info->space_info);
Chris Mason0b86a832008-03-24 15:01:56 -0400771 btrfs_mapping_init(&fs_info->mapping_tree);
Chris Masone20d96d2007-03-22 12:13:20 -0400772 fs_info->sb = sb;
Chris Masone2008b62008-01-08 15:46:30 -0500773 fs_info->throttles = 0;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500774 fs_info->mount_opt = 0;
Chris Masonc59f8952007-12-17 20:14:04 -0500775 fs_info->max_extent = (u64)-1;
Chris Mason6f568d32008-01-29 16:03:38 -0500776 fs_info->max_inline = 8192 * 1024;
Chris Mason1832a6d2007-12-21 16:27:21 -0500777 fs_info->delalloc_bytes = 0;
Chris Mason04160082008-03-26 10:28:07 -0400778 setup_bdi(fs_info, &fs_info->bdi);
Chris Masond98237b2007-03-28 13:57:48 -0400779 fs_info->btree_inode = new_inode(sb);
780 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400781 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -0400782 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
783 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason04160082008-03-26 10:28:07 -0400784 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
785
Chris Masond1310b22008-01-24 16:13:08 -0500786 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -0400787 fs_info->btree_inode->i_mapping,
788 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500789 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
790 GFP_NOFS);
Chris Mason0da54682007-11-07 21:08:01 -0500791
Chris Masond1310b22008-01-24 16:13:08 -0500792 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
793
794 extent_io_tree_init(&fs_info->free_space_cache,
Chris Masonf510cfe2007-10-15 16:14:48 -0400795 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500796 extent_io_tree_init(&fs_info->block_group_cache,
Chris Mason96b51792007-10-15 16:15:19 -0400797 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500798 extent_io_tree_init(&fs_info->pinned_extents,
Chris Mason1a5bc162007-10-15 16:15:26 -0400799 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500800 extent_io_tree_init(&fs_info->pending_del,
Chris Mason1a5bc162007-10-15 16:15:26 -0400801 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500802 extent_io_tree_init(&fs_info->extent_ins,
Chris Mason1a5bc162007-10-15 16:15:26 -0400803 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masone66f7092007-04-20 13:16:02 -0400804 fs_info->do_barriers = 1;
Chris Masonfacda1e2007-06-08 18:11:48 -0400805 fs_info->closing = 0;
Yan324ae4d2007-11-16 14:57:08 -0500806 fs_info->total_pinned = 0;
Chris Masone18e4802008-01-18 10:54:22 -0500807 fs_info->last_alloc = 0;
Chris Mason4529ba42008-01-31 16:45:07 -0500808 fs_info->last_data_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -0400809 fs_info->extra_alloc_bits = 0;
810 fs_info->extra_data_alloc_bits = 0;
Chris Masone18e4802008-01-18 10:54:22 -0500811
Chris Mason6da6aba2007-12-18 16:15:09 -0500812#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
813 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
814#else
Chris Mason08607c12007-06-08 15:33:54 -0400815 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason6da6aba2007-12-18 16:15:09 -0500816#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -0400817 BTRFS_I(fs_info->btree_inode)->root = tree_root;
818 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
819 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -0400820 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -0400821 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400822
Chris Mason79154b12007-03-22 15:59:16 -0400823 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400824 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400825
Chris Mason19c00dd2007-10-15 16:19:22 -0400826#if 0
827 ret = add_hasher(fs_info, "crc32c");
828 if (ret) {
829 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
830 err = -ENOMEM;
831 goto fail_iput;
832 }
833#endif
Chris Mason0b86a832008-03-24 15:01:56 -0400834 __setup_root(4096, 4096, 4096, 4096, tree_root,
Chris Mason2c90e5d2007-04-02 10:50:19 -0400835 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -0400836
Chris Mason2c90e5d2007-04-02 10:50:19 -0400837 fs_info->sb_buffer = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400838 BTRFS_SUPER_INFO_OFFSET,
Chris Mason0b86a832008-03-24 15:01:56 -0400839 4096);
Chris Masond98237b2007-03-28 13:57:48 -0400840
Chris Mason0f7d52f2007-04-09 10:42:37 -0400841 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -0400842 goto fail_iput;
Chris Mason39279cc2007-06-12 06:35:45 -0400843
Chris Mason5f39d392007-10-15 16:14:19 -0400844 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
845 sizeof(fs_info->super_copy));
846
847 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
848 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
849 BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -0400850
Chris Mason5f39d392007-10-15 16:14:19 -0400851 disk_super = &fs_info->super_copy;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400852 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -0400853 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400854
Chris Mason8a4b83c2008-03-24 15:02:07 -0400855 if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
856 printk("Btrfs: wanted %llu devices, but found %llu\n",
857 (unsigned long long)btrfs_super_num_devices(disk_super),
858 (unsigned long long)fs_devices->num_devices);
859 goto fail_sb_buffer;
860 }
Chris Masondb945352007-10-15 16:15:53 -0400861 nodesize = btrfs_super_nodesize(disk_super);
862 leafsize = btrfs_super_leafsize(disk_super);
863 sectorsize = btrfs_super_sectorsize(disk_super);
Chris Mason87ee04e2007-11-30 11:30:34 -0500864 stripesize = btrfs_super_stripesize(disk_super);
Chris Masondb945352007-10-15 16:15:53 -0400865 tree_root->nodesize = nodesize;
866 tree_root->leafsize = leafsize;
867 tree_root->sectorsize = sectorsize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500868 tree_root->stripesize = stripesize;
Chris Masonff79f812007-10-15 16:22:25 -0400869 sb_set_blocksize(sb, sectorsize);
Chris Masondb945352007-10-15 16:15:53 -0400870
Chris Mason8352d8a2007-04-12 10:43:05 -0400871 i_size_write(fs_info->btree_inode,
Chris Masondb945352007-10-15 16:15:53 -0400872 btrfs_super_total_bytes(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -0400873
Chris Mason39279cc2007-06-12 06:35:45 -0400874 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
875 sizeof(disk_super->magic))) {
876 printk("btrfs: valid FS not found on %s\n", sb->s_id);
877 goto fail_sb_buffer;
878 }
Chris Mason19c00dd2007-10-15 16:19:22 -0400879
Chris Mason0b86a832008-03-24 15:01:56 -0400880 mutex_lock(&fs_info->fs_mutex);
Chris Mason0d81ba52008-03-24 15:02:07 -0400881
Chris Mason0b86a832008-03-24 15:01:56 -0400882 ret = btrfs_read_sys_array(tree_root);
883 BUG_ON(ret);
884
885 blocksize = btrfs_level_size(tree_root,
886 btrfs_super_chunk_root_level(disk_super));
887
888 __setup_root(nodesize, leafsize, sectorsize, stripesize,
889 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
890
891 chunk_root->node = read_tree_block(chunk_root,
892 btrfs_super_chunk_root(disk_super),
893 blocksize);
894 BUG_ON(!chunk_root->node);
895
896 ret = btrfs_read_chunk_tree(chunk_root);
897 BUG_ON(ret);
898
Chris Masondb945352007-10-15 16:15:53 -0400899 blocksize = btrfs_level_size(tree_root,
900 btrfs_super_root_level(disk_super));
Chris Mason19c00dd2007-10-15 16:19:22 -0400901
Chris Mason0b86a832008-03-24 15:01:56 -0400902
Chris Masone20d96d2007-03-22 12:13:20 -0400903 tree_root->node = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -0400904 btrfs_super_root(disk_super),
905 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -0400906 if (!tree_root->node)
907 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -0400908
Chris Masondb945352007-10-15 16:15:53 -0400909
910 ret = find_and_setup_root(tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400911 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason0b86a832008-03-24 15:01:56 -0400912 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400913 goto fail_tree_root;
Chris Mason0b86a832008-03-24 15:01:56 -0400914 extent_root->track_dirty = 1;
915
916 ret = find_and_setup_root(tree_root, fs_info,
917 BTRFS_DEV_TREE_OBJECTID, dev_root);
918 dev_root->track_dirty = 1;
919
920 if (ret)
921 goto fail_extent_root;
Chris Mason3768f362007-03-13 16:47:54 -0400922
Chris Mason9078a3e2007-04-26 16:46:15 -0400923 btrfs_read_block_groups(extent_root);
924
Chris Mason0f7d52f2007-04-09 10:42:37 -0400925 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400926 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400927 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -0400928
Chris Mason0b86a832008-03-24 15:01:56 -0400929fail_extent_root:
930 free_extent_buffer(extent_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -0400931fail_tree_root:
Chris Mason0b86a832008-03-24 15:01:56 -0400932 mutex_unlock(&fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -0400933 free_extent_buffer(tree_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -0400934fail_sb_buffer:
Chris Mason5f39d392007-10-15 16:14:19 -0400935 free_extent_buffer(fs_info->sb_buffer);
Chris Mason2d2ae542008-03-26 16:24:23 -0400936 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
Chris Mason39279cc2007-06-12 06:35:45 -0400937fail_iput:
938 iput(fs_info->btree_inode);
939fail:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400940 close_all_devices(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -0400941 kfree(extent_root);
942 kfree(tree_root);
Chris Mason2d2ae542008-03-26 16:24:23 -0400943 bdi_destroy(&fs_info->bdi);
Chris Mason39279cc2007-06-12 06:35:45 -0400944 kfree(fs_info);
945 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -0500946}
947
Chris Masone089f052007-03-16 16:20:31 -0400948int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400949 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500950{
Chris Masone66f7092007-04-20 13:16:02 -0400951 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -0400952 struct extent_buffer *super = root->fs_info->sb_buffer;
953 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason21ad10c2008-01-09 09:23:21 -0500954 struct super_block *sb = root->fs_info->sb;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400955
Chris Mason21ad10c2008-01-09 09:23:21 -0500956 if (!btrfs_test_opt(root, NOBARRIER))
957 blkdev_issue_flush(sb->s_bdev, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500958 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
Chris Mason5f39d392007-10-15 16:14:19 -0400959 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
960 super->start, super->len);
Chris Mason21ad10c2008-01-09 09:23:21 -0500961 if (!btrfs_test_opt(root, NOBARRIER))
962 blkdev_issue_flush(sb->s_bdev, NULL);
Chris Mason5f39d392007-10-15 16:14:19 -0400963 return ret;
Chris Masoncfaa7292007-02-21 17:04:57 -0500964}
965
Chris Mason5eda7b52007-06-22 14:16:25 -0400966int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -0400967{
968 radix_tree_delete(&fs_info->fs_roots_radix,
969 (unsigned long)root->root_key.objectid);
Chris Masonb99aa6c2008-01-14 14:41:16 -0500970 if (root->in_sysfs)
971 btrfs_sysfs_del_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -0400972 if (root->inode)
973 iput(root->inode);
974 if (root->node)
Chris Mason5f39d392007-10-15 16:14:19 -0400975 free_extent_buffer(root->node);
Chris Mason2619ba12007-04-10 16:58:11 -0400976 if (root->commit_root)
Chris Mason5f39d392007-10-15 16:14:19 -0400977 free_extent_buffer(root->commit_root);
Josef Bacik58176a92007-08-29 15:47:34 -0400978 if (root->name)
979 kfree(root->name);
Chris Mason2619ba12007-04-10 16:58:11 -0400980 kfree(root);
981 return 0;
982}
983
Chris Mason35b7e472007-05-02 15:53:43 -0400984static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400985{
986 int ret;
987 struct btrfs_root *gang[8];
988 int i;
989
990 while(1) {
991 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
992 (void **)gang, 0,
993 ARRAY_SIZE(gang));
994 if (!ret)
995 break;
Chris Mason2619ba12007-04-10 16:58:11 -0400996 for (i = 0; i < ret; i++)
Chris Mason5eda7b52007-06-22 14:16:25 -0400997 btrfs_free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400998 }
999 return 0;
1000}
Chris Masonb4100d62007-04-12 12:14:00 -04001001
Chris Masone20d96d2007-03-22 12:13:20 -04001002int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -05001003{
Chris Mason3768f362007-03-13 16:47:54 -04001004 int ret;
Chris Masone089f052007-03-16 16:20:31 -04001005 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001006 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -04001007
Chris Masonfacda1e2007-06-08 18:11:48 -04001008 fs_info->closing = 1;
Chris Mason08607c12007-06-08 15:33:54 -04001009 btrfs_transaction_flush_work(root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001010 mutex_lock(&fs_info->fs_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -04001011 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason79154b12007-03-22 15:59:16 -04001012 trans = btrfs_start_transaction(root, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001013 ret = btrfs_commit_transaction(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -04001014 /* run commit again to drop the original snapshot */
1015 trans = btrfs_start_transaction(root, 1);
1016 btrfs_commit_transaction(trans, root);
1017 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -04001018 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -04001019 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001020 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001021
Chris Masonb0c68f82008-01-31 11:05:37 -05001022 if (fs_info->delalloc_bytes) {
1023 printk("btrfs: at unmount delalloc count %Lu\n",
1024 fs_info->delalloc_bytes);
1025 }
Chris Mason0f7d52f2007-04-09 10:42:37 -04001026 if (fs_info->extent_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -04001027 free_extent_buffer(fs_info->extent_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -04001028
Chris Mason0f7d52f2007-04-09 10:42:37 -04001029 if (fs_info->tree_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -04001030 free_extent_buffer(fs_info->tree_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -04001031
Chris Mason0b86a832008-03-24 15:01:56 -04001032 if (root->fs_info->chunk_root->node);
1033 free_extent_buffer(root->fs_info->chunk_root->node);
1034
1035 if (root->fs_info->dev_root->node);
1036 free_extent_buffer(root->fs_info->dev_root->node);
1037
Chris Mason5f39d392007-10-15 16:14:19 -04001038 free_extent_buffer(fs_info->sb_buffer);
Chris Mason7eccb902007-04-11 15:53:25 -04001039
Chris Mason9078a3e2007-04-26 16:46:15 -04001040 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001041 del_fs_roots(fs_info);
Chris Masond10c5f32007-12-17 20:14:04 -05001042
1043 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
1044
Chris Masond1310b22008-01-24 16:13:08 -05001045 extent_io_tree_empty_lru(&fs_info->free_space_cache);
1046 extent_io_tree_empty_lru(&fs_info->block_group_cache);
1047 extent_io_tree_empty_lru(&fs_info->pinned_extents);
1048 extent_io_tree_empty_lru(&fs_info->pending_del);
1049 extent_io_tree_empty_lru(&fs_info->extent_ins);
1050 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
Chris Masond10c5f32007-12-17 20:14:04 -05001051
Chris Masondb945352007-10-15 16:15:53 -04001052 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
Chris Masond10c5f32007-12-17 20:14:04 -05001053
Chris Masondb945352007-10-15 16:15:53 -04001054 iput(fs_info->btree_inode);
Chris Mason19c00dd2007-10-15 16:19:22 -04001055#if 0
1056 while(!list_empty(&fs_info->hashers)) {
1057 struct btrfs_hasher *hasher;
1058 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
1059 hashers);
1060 list_del(&hasher->hashers);
1061 crypto_free_hash(&fs_info->hash_tfm);
1062 kfree(hasher);
1063 }
1064#endif
Chris Mason0b86a832008-03-24 15:01:56 -04001065 close_all_devices(fs_info);
1066 btrfs_mapping_tree_free(&fs_info->mapping_tree);
Chris Mason04160082008-03-26 10:28:07 -04001067 bdi_destroy(&fs_info->bdi);
Chris Mason0b86a832008-03-24 15:01:56 -04001068
Chris Mason0f7d52f2007-04-09 10:42:37 -04001069 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001070 kfree(fs_info->tree_root);
Chris Mason0b86a832008-03-24 15:01:56 -04001071 kfree(fs_info->chunk_root);
1072 kfree(fs_info->dev_root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001073 return 0;
1074}
1075
Chris Mason5f39d392007-10-15 16:14:19 -04001076int btrfs_buffer_uptodate(struct extent_buffer *buf)
Chris Masonccd467d2007-06-28 15:57:36 -04001077{
Chris Mason810191f2007-10-15 16:18:55 -04001078 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001079 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001080}
Chris Mason6702ed42007-08-07 16:15:09 -04001081
Chris Mason5f39d392007-10-15 16:14:19 -04001082int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
1083{
Chris Mason810191f2007-10-15 16:18:55 -04001084 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001085 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -04001086 buf);
1087}
1088
1089void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
1090{
Chris Mason810191f2007-10-15 16:18:55 -04001091 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04001092 u64 transid = btrfs_header_generation(buf);
1093 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason6702ed42007-08-07 16:15:09 -04001094
Chris Masonccd467d2007-06-28 15:57:36 -04001095 if (transid != root->fs_info->generation) {
1096 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
Chris Masondb945352007-10-15 16:15:53 -04001097 (unsigned long long)buf->start,
Chris Masonccd467d2007-06-28 15:57:36 -04001098 transid, root->fs_info->generation);
1099 WARN_ON(1);
1100 }
Chris Masond1310b22008-01-24 16:13:08 -05001101 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001102}
1103
Chris Masone2008b62008-01-08 15:46:30 -05001104void btrfs_throttle(struct btrfs_root *root)
1105{
Chris Mason55c69072008-01-09 15:55:33 -05001106 struct backing_dev_info *bdi;
1107
1108 bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
Chris Mason04005cc2008-01-17 12:01:41 -05001109 if (root->fs_info->throttles && bdi_write_congested(bdi)) {
1110#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
Chris Mason55c69072008-01-09 15:55:33 -05001111 congestion_wait(WRITE, HZ/20);
Chris Mason04005cc2008-01-17 12:01:41 -05001112#else
1113 blk_congestion_wait(WRITE, HZ/20);
1114#endif
1115 }
Chris Masone2008b62008-01-08 15:46:30 -05001116}
1117
Chris Masond3c2fdc2007-09-17 10:58:06 -04001118void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
Chris Mason35b7e472007-05-02 15:53:43 -04001119{
Chris Masond3c2fdc2007-09-17 10:58:06 -04001120 balance_dirty_pages_ratelimited_nr(
Chris Masond7fc6402008-02-18 12:12:38 -05001121 root->fs_info->btree_inode->i_mapping, 1);
Chris Mason35b7e472007-05-02 15:53:43 -04001122}
Chris Mason6b800532007-10-15 16:17:34 -04001123
1124void btrfs_set_buffer_defrag(struct extent_buffer *buf)
1125{
Chris Mason810191f2007-10-15 16:18:55 -04001126 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001127 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001128 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
Chris Mason6b800532007-10-15 16:17:34 -04001129 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
1130}
1131
1132void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
1133{
Chris Mason810191f2007-10-15 16:18:55 -04001134 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001135 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001136 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
Chris Mason6b800532007-10-15 16:17:34 -04001137 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
1138 GFP_NOFS);
1139}
1140
1141int btrfs_buffer_defrag(struct extent_buffer *buf)
1142{
Chris Mason810191f2007-10-15 16:18:55 -04001143 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001144 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001145 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001146 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
1147}
1148
1149int btrfs_buffer_defrag_done(struct extent_buffer *buf)
1150{
Chris Mason810191f2007-10-15 16:18:55 -04001151 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001152 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001153 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001154 buf->start, buf->start + buf->len - 1,
1155 EXTENT_DEFRAG_DONE, 0);
1156}
1157
1158int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
1159{
Chris Mason810191f2007-10-15 16:18:55 -04001160 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001161 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001162 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001163 buf->start, buf->start + buf->len - 1,
1164 EXTENT_DEFRAG_DONE, GFP_NOFS);
1165}
1166
1167int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
1168{
Chris Mason810191f2007-10-15 16:18:55 -04001169 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001170 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001171 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001172 buf->start, buf->start + buf->len - 1,
1173 EXTENT_DEFRAG, GFP_NOFS);
1174}
1175
1176int btrfs_read_buffer(struct extent_buffer *buf)
1177{
Chris Mason810191f2007-10-15 16:18:55 -04001178 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001179 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001180 return read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
Chris Masona86c12c2008-02-07 10:50:54 -05001181 buf, 0, 1, btree_get_extent);
Chris Mason6b800532007-10-15 16:17:34 -04001182}
Chris Mason0da54682007-11-07 21:08:01 -05001183
Chris Masond1310b22008-01-24 16:13:08 -05001184static struct extent_io_ops btree_extent_io_ops = {
Chris Mason0da54682007-11-07 21:08:01 -05001185 .writepage_io_hook = btree_writepage_io_hook,
Chris Mason0b86a832008-03-24 15:01:56 -04001186 .submit_bio_hook = btree_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04001187 /* note we're sharing with inode.c for the merge bio hook */
1188 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason0da54682007-11-07 21:08:01 -05001189};