blob: f638803549e05bc2d70b55ebc487798065f87914 [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 Mason87cbda52007-03-28 19:44:27 -040021#include <linux/scatterlist.h>
Chris Mason22b0ebd2007-03-30 08:47:31 -040022#include <linux/swap.h>
Chris Mason0f7d52f2007-04-09 10:42:37 -040023#include <linux/radix-tree.h>
Chris Mason35b7e472007-05-02 15:53:43 -040024#include <linux/writeback.h>
Chris Mason5f39d392007-10-15 16:14:19 -040025#include <linux/buffer_head.h> // for block_sync_page
Chris Masonce9adaa2008-04-09 16:28:12 -040026#include <linux/workqueue.h>
Miguela5eb62e2008-04-11 15:45:51 -040027#include "crc32c.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 Mason8b712842008-06-11 16:50:36 -040034#include "async-thread.h"
Chris Mason925baed2008-06-25 16:01:30 -040035#include "locking.h"
Chris Masoneb60cea2007-02-02 09:18:22 -050036
Chris Mason5f39d392007-10-15 16:14:19 -040037#if 0
38static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
Chris Mason7eccb902007-04-11 15:53:25 -040039{
Chris Mason5f39d392007-10-15 16:14:19 -040040 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
41 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
42 (unsigned long long)extent_buffer_blocknr(buf),
43 (unsigned long long)btrfs_header_blocknr(buf));
Chris Mason39279cc2007-06-12 06:35:45 -040044 return 1;
Chris Masond98237b2007-03-28 13:57:48 -040045 }
Chris Mason9a8dd152007-02-23 08:38:36 -050046 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050047}
Chris Mason5f39d392007-10-15 16:14:19 -040048#endif
Chris Masoneb60cea2007-02-02 09:18:22 -050049
Chris Masond1310b22008-01-24 16:13:08 -050050static struct extent_io_ops btree_extent_io_ops;
Chris Mason8b712842008-06-11 16:50:36 -040051static void end_workqueue_fn(struct btrfs_work *work);
Chris Masonce9adaa2008-04-09 16:28:12 -040052
53struct end_io_wq {
54 struct bio *bio;
55 bio_end_io_t *end_io;
56 void *private;
57 struct btrfs_fs_info *info;
58 int error;
Chris Mason22c59942008-04-09 16:28:12 -040059 int metadata;
Chris Masonce9adaa2008-04-09 16:28:12 -040060 struct list_head list;
Chris Mason8b712842008-06-11 16:50:36 -040061 struct btrfs_work work;
Chris Masonce9adaa2008-04-09 16:28:12 -040062};
Chris Mason0da54682007-11-07 21:08:01 -050063
Chris Mason44b8bd72008-04-16 11:14:51 -040064struct async_submit_bio {
65 struct inode *inode;
66 struct bio *bio;
67 struct list_head list;
68 extent_submit_bio_hook_t *submit_bio_hook;
69 int rw;
70 int mirror_num;
Chris Mason8b712842008-06-11 16:50:36 -040071 struct btrfs_work work;
Chris Mason44b8bd72008-04-16 11:14:51 -040072};
73
Chris Mason5f39d392007-10-15 16:14:19 -040074struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
Chris Masond1310b22008-01-24 16:13:08 -050075 size_t page_offset, u64 start, u64 len,
Chris Mason5f39d392007-10-15 16:14:19 -040076 int create)
77{
78 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
79 struct extent_map *em;
80 int ret;
Chris Masond98237b2007-03-28 13:57:48 -040081
Chris Masond1310b22008-01-24 16:13:08 -050082 spin_lock(&em_tree->lock);
83 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -040084 if (em) {
85 em->bdev =
86 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
87 spin_unlock(&em_tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -040088 goto out;
Chris Masona061fc82008-05-07 11:43:44 -040089 }
90 spin_unlock(&em_tree->lock);
Chris Mason7b13b7b2008-04-18 10:29:50 -040091
Chris Mason5f39d392007-10-15 16:14:19 -040092 em = alloc_extent_map(GFP_NOFS);
93 if (!em) {
94 em = ERR_PTR(-ENOMEM);
95 goto out;
96 }
97 em->start = 0;
Chris Mason0afbaf82008-04-18 14:17:20 -040098 em->len = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -040099 em->block_start = 0;
Chris Masona061fc82008-05-07 11:43:44 -0400100 em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -0500101
102 spin_lock(&em_tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -0400103 ret = add_extent_mapping(em_tree, em);
104 if (ret == -EEXIST) {
Chris Mason0afbaf82008-04-18 14:17:20 -0400105 u64 failed_start = em->start;
106 u64 failed_len = em->len;
107
108 printk("failed to insert %Lu %Lu -> %Lu into tree\n",
109 em->start, em->len, em->block_start);
Chris Mason5f39d392007-10-15 16:14:19 -0400110 free_extent_map(em);
Chris Mason7b13b7b2008-04-18 10:29:50 -0400111 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason0afbaf82008-04-18 14:17:20 -0400112 if (em) {
113 printk("after failing, found %Lu %Lu %Lu\n",
114 em->start, em->len, em->block_start);
Chris Mason7b13b7b2008-04-18 10:29:50 -0400115 ret = 0;
Chris Mason0afbaf82008-04-18 14:17:20 -0400116 } else {
117 em = lookup_extent_mapping(em_tree, failed_start,
118 failed_len);
119 if (em) {
120 printk("double failure lookup gives us "
121 "%Lu %Lu -> %Lu\n", em->start,
122 em->len, em->block_start);
123 free_extent_map(em);
124 }
Chris Mason7b13b7b2008-04-18 10:29:50 -0400125 ret = -EIO;
Chris Mason0afbaf82008-04-18 14:17:20 -0400126 }
Chris Mason5f39d392007-10-15 16:14:19 -0400127 } else if (ret) {
Chris Mason7b13b7b2008-04-18 10:29:50 -0400128 free_extent_map(em);
129 em = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400130 }
Chris Mason7b13b7b2008-04-18 10:29:50 -0400131 spin_unlock(&em_tree->lock);
132
133 if (ret)
134 em = ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400135out:
136 return em;
137}
138
Chris Mason19c00dd2007-10-15 16:19:22 -0400139u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
140{
Miguela5eb62e2008-04-11 15:45:51 -0400141 return btrfs_crc32c(seed, data, len);
Chris Mason19c00dd2007-10-15 16:19:22 -0400142}
143
144void btrfs_csum_final(u32 crc, char *result)
145{
146 *(__le32 *)result = ~cpu_to_le32(crc);
147}
148
149static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
150 int verify)
151{
152 char result[BTRFS_CRC32_SIZE];
153 unsigned long len;
154 unsigned long cur_len;
155 unsigned long offset = BTRFS_CSUM_SIZE;
156 char *map_token = NULL;
157 char *kaddr;
158 unsigned long map_start;
159 unsigned long map_len;
160 int err;
161 u32 crc = ~(u32)0;
162
163 len = buf->len - offset;
164 while(len > 0) {
165 err = map_private_extent_buffer(buf, offset, 32,
166 &map_token, &kaddr,
167 &map_start, &map_len, KM_USER0);
168 if (err) {
169 printk("failed to map extent buffer! %lu\n",
170 offset);
171 return 1;
172 }
173 cur_len = min(len, map_len - (offset - map_start));
174 crc = btrfs_csum_data(root, kaddr + offset - map_start,
175 crc, cur_len);
176 len -= cur_len;
177 offset += cur_len;
178 unmap_extent_buffer(buf, map_token, KM_USER0);
179 }
180 btrfs_csum_final(crc, result);
181
182 if (verify) {
Chris Masone4204de2008-01-08 15:46:27 -0500183 int from_this_trans = 0;
184
185 if (root->fs_info->running_transaction &&
186 btrfs_header_generation(buf) ==
187 root->fs_info->running_transaction->transid)
188 from_this_trans = 1;
189
190 /* FIXME, this is not good */
Chris Mason63b10fc2008-04-01 11:21:32 -0400191 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
Chris Masone4204de2008-01-08 15:46:27 -0500192 u32 val;
193 u32 found = 0;
194 memcpy(&found, result, BTRFS_CRC32_SIZE);
195
196 read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
197 printk("btrfs: %s checksum verify failed on %llu "
Chris Mason63b10fc2008-04-01 11:21:32 -0400198 "wanted %X found %X from_this_trans %d "
199 "level %d\n",
Chris Mason19c00dd2007-10-15 16:19:22 -0400200 root->fs_info->sb->s_id,
Chris Mason63b10fc2008-04-01 11:21:32 -0400201 buf->start, val, found, from_this_trans,
202 btrfs_header_level(buf));
Chris Mason19c00dd2007-10-15 16:19:22 -0400203 return 1;
204 }
205 } else {
206 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
207 }
208 return 0;
209}
210
Chris Mason1259ab72008-05-12 13:39:03 -0400211static int verify_parent_transid(struct extent_io_tree *io_tree,
212 struct extent_buffer *eb, u64 parent_transid)
213{
214 int ret;
215
216 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
217 return 0;
218
219 lock_extent(io_tree, eb->start, eb->start + eb->len - 1, GFP_NOFS);
220 if (extent_buffer_uptodate(io_tree, eb) &&
221 btrfs_header_generation(eb) == parent_transid) {
222 ret = 0;
223 goto out;
224 }
225 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
226 (unsigned long long)eb->start,
227 (unsigned long long)parent_transid,
228 (unsigned long long)btrfs_header_generation(eb));
229 ret = 1;
230out:
231 clear_extent_buffer_uptodate(io_tree, eb);
232 unlock_extent(io_tree, eb->start, eb->start + eb->len - 1,
233 GFP_NOFS);
234 return ret;
235
236}
237
Chris Masonf1885912008-04-09 16:28:12 -0400238static int btree_read_extent_buffer_pages(struct btrfs_root *root,
239 struct extent_buffer *eb,
Chris Masonca7a79a2008-05-12 12:59:19 -0400240 u64 start, u64 parent_transid)
Chris Masonf1885912008-04-09 16:28:12 -0400241{
242 struct extent_io_tree *io_tree;
243 int ret;
244 int num_copies = 0;
245 int mirror_num = 0;
246
247 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
248 while (1) {
249 ret = read_extent_buffer_pages(io_tree, eb, start, 1,
250 btree_get_extent, mirror_num);
Chris Mason1259ab72008-05-12 13:39:03 -0400251 if (!ret &&
252 !verify_parent_transid(io_tree, eb, parent_transid))
Chris Masonf1885912008-04-09 16:28:12 -0400253 return ret;
Chris Mason42352982008-04-28 16:40:52 -0400254
Chris Masonf1885912008-04-09 16:28:12 -0400255 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
256 eb->start, eb->len);
Chris Mason42352982008-04-28 16:40:52 -0400257 if (num_copies == 1)
Chris Masonf1885912008-04-09 16:28:12 -0400258 return ret;
Chris Mason42352982008-04-28 16:40:52 -0400259
Chris Masonf1885912008-04-09 16:28:12 -0400260 mirror_num++;
Chris Mason42352982008-04-28 16:40:52 -0400261 if (mirror_num > num_copies)
Chris Masonf1885912008-04-09 16:28:12 -0400262 return ret;
Chris Masonf1885912008-04-09 16:28:12 -0400263 }
Chris Masonf1885912008-04-09 16:28:12 -0400264 return -EIO;
265}
Chris Mason19c00dd2007-10-15 16:19:22 -0400266
267int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
268{
Chris Masond1310b22008-01-24 16:13:08 -0500269 struct extent_io_tree *tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400270 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason19c00dd2007-10-15 16:19:22 -0400271 u64 found_start;
272 int found_level;
273 unsigned long len;
274 struct extent_buffer *eb;
Chris Masonf1885912008-04-09 16:28:12 -0400275 int ret;
276
Chris Masond1310b22008-01-24 16:13:08 -0500277 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason19c00dd2007-10-15 16:19:22 -0400278
279 if (page->private == EXTENT_PAGE_PRIVATE)
280 goto out;
281 if (!page->private)
282 goto out;
283 len = page->private >> 2;
284 if (len == 0) {
285 WARN_ON(1);
286 }
287 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
Chris Masonca7a79a2008-05-12 12:59:19 -0400288 ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE,
289 btrfs_header_generation(eb));
Chris Masonf1885912008-04-09 16:28:12 -0400290 BUG_ON(ret);
Chris Masone18e4802008-01-18 10:54:22 -0500291 btrfs_clear_buffer_defrag(eb);
Chris Mason19c00dd2007-10-15 16:19:22 -0400292 found_start = btrfs_header_bytenr(eb);
293 if (found_start != start) {
294 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
295 start, found_start, len);
Chris Mason55c69072008-01-09 15:55:33 -0500296 WARN_ON(1);
297 goto err;
298 }
299 if (eb->first_page != page) {
300 printk("bad first page %lu %lu\n", eb->first_page->index,
301 page->index);
302 WARN_ON(1);
303 goto err;
304 }
305 if (!PageUptodate(page)) {
306 printk("csum not up to date page %lu\n", page->index);
307 WARN_ON(1);
308 goto err;
Chris Mason19c00dd2007-10-15 16:19:22 -0400309 }
310 found_level = btrfs_header_level(eb);
Chris Mason63b10fc2008-04-01 11:21:32 -0400311 spin_lock(&root->fs_info->hash_lock);
312 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
313 spin_unlock(&root->fs_info->hash_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -0400314 csum_tree_block(root, eb, 0);
Chris Mason55c69072008-01-09 15:55:33 -0500315err:
Chris Mason19c00dd2007-10-15 16:19:22 -0400316 free_extent_buffer(eb);
317out:
318 return 0;
319}
320
Chris Mason0da54682007-11-07 21:08:01 -0500321static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
322{
323 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
324
325 csum_dirty_buffer(root, page);
326 return 0;
327}
328
Chris Masonce9adaa2008-04-09 16:28:12 -0400329int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
330 struct extent_state *state)
331{
332 struct extent_io_tree *tree;
333 u64 found_start;
334 int found_level;
335 unsigned long len;
336 struct extent_buffer *eb;
337 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masonf1885912008-04-09 16:28:12 -0400338 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -0400339
340 tree = &BTRFS_I(page->mapping->host)->io_tree;
341 if (page->private == EXTENT_PAGE_PRIVATE)
342 goto out;
343 if (!page->private)
344 goto out;
345 len = page->private >> 2;
346 if (len == 0) {
347 WARN_ON(1);
348 }
349 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
Chris Masonf1885912008-04-09 16:28:12 -0400350
Chris Masonce9adaa2008-04-09 16:28:12 -0400351 btrfs_clear_buffer_defrag(eb);
352 found_start = btrfs_header_bytenr(eb);
353 if (found_start != start) {
Chris Masonf1885912008-04-09 16:28:12 -0400354 ret = -EIO;
Chris Masonce9adaa2008-04-09 16:28:12 -0400355 goto err;
356 }
357 if (eb->first_page != page) {
358 printk("bad first page %lu %lu\n", eb->first_page->index,
359 page->index);
360 WARN_ON(1);
Chris Masonf1885912008-04-09 16:28:12 -0400361 ret = -EIO;
Chris Masonce9adaa2008-04-09 16:28:12 -0400362 goto err;
363 }
Chris Mason1259ab72008-05-12 13:39:03 -0400364 if (memcmp_extent_buffer(eb, root->fs_info->fsid,
365 (unsigned long)btrfs_header_fsid(eb),
366 BTRFS_FSID_SIZE)) {
367 printk("bad fsid on block %Lu\n", eb->start);
368 ret = -EIO;
369 goto err;
370 }
Chris Masonce9adaa2008-04-09 16:28:12 -0400371 found_level = btrfs_header_level(eb);
372
373 ret = csum_tree_block(root, eb, 1);
Chris Masonf1885912008-04-09 16:28:12 -0400374 if (ret)
375 ret = -EIO;
Chris Masonce9adaa2008-04-09 16:28:12 -0400376
377 end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
378 end = eb->start + end - 1;
379 release_extent_buffer_tail_pages(eb);
380err:
381 free_extent_buffer(eb);
382out:
Chris Masonf1885912008-04-09 16:28:12 -0400383 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -0400384}
385
386#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
387static void end_workqueue_bio(struct bio *bio, int err)
388#else
389static int end_workqueue_bio(struct bio *bio,
390 unsigned int bytes_done, int err)
391#endif
392{
393 struct end_io_wq *end_io_wq = bio->bi_private;
394 struct btrfs_fs_info *fs_info;
Chris Masonce9adaa2008-04-09 16:28:12 -0400395
396#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
397 if (bio->bi_size)
398 return 1;
399#endif
400
401 fs_info = end_io_wq->info;
Chris Masonce9adaa2008-04-09 16:28:12 -0400402 end_io_wq->error = err;
Chris Mason8b712842008-06-11 16:50:36 -0400403 end_io_wq->work.func = end_workqueue_fn;
404 end_io_wq->work.flags = 0;
405 btrfs_queue_worker(&fs_info->endio_workers, &end_io_wq->work);
Chris Masonce9adaa2008-04-09 16:28:12 -0400406
407#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
408 return 0;
409#endif
410}
411
Chris Mason22c59942008-04-09 16:28:12 -0400412int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
413 int metadata)
Chris Mason0b86a832008-03-24 15:01:56 -0400414{
Chris Masonce9adaa2008-04-09 16:28:12 -0400415 struct end_io_wq *end_io_wq;
Chris Masonce9adaa2008-04-09 16:28:12 -0400416 end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
417 if (!end_io_wq)
418 return -ENOMEM;
419
420 end_io_wq->private = bio->bi_private;
421 end_io_wq->end_io = bio->bi_end_io;
Chris Mason22c59942008-04-09 16:28:12 -0400422 end_io_wq->info = info;
Chris Masonce9adaa2008-04-09 16:28:12 -0400423 end_io_wq->error = 0;
424 end_io_wq->bio = bio;
Chris Mason22c59942008-04-09 16:28:12 -0400425 end_io_wq->metadata = metadata;
Chris Masonce9adaa2008-04-09 16:28:12 -0400426
427 bio->bi_private = end_io_wq;
428 bio->bi_end_io = end_workqueue_bio;
Chris Mason22c59942008-04-09 16:28:12 -0400429 return 0;
430}
431
Chris Mason8b712842008-06-11 16:50:36 -0400432static void run_one_async_submit(struct btrfs_work *work)
433{
434 struct btrfs_fs_info *fs_info;
435 struct async_submit_bio *async;
436
437 async = container_of(work, struct async_submit_bio, work);
438 fs_info = BTRFS_I(async->inode)->root->fs_info;
439 atomic_dec(&fs_info->nr_async_submits);
440 async->submit_bio_hook(async->inode, async->rw, async->bio,
441 async->mirror_num);
442 kfree(async);
443}
444
Chris Mason44b8bd72008-04-16 11:14:51 -0400445int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
446 int rw, struct bio *bio, int mirror_num,
447 extent_submit_bio_hook_t *submit_bio_hook)
448{
449 struct async_submit_bio *async;
450
451 async = kmalloc(sizeof(*async), GFP_NOFS);
452 if (!async)
453 return -ENOMEM;
454
455 async->inode = inode;
456 async->rw = rw;
457 async->bio = bio;
458 async->mirror_num = mirror_num;
459 async->submit_bio_hook = submit_bio_hook;
Chris Mason8b712842008-06-11 16:50:36 -0400460 async->work.func = run_one_async_submit;
461 async->work.flags = 0;
Chris Masoncb03c742008-05-15 16:15:45 -0400462 atomic_inc(&fs_info->nr_async_submits);
Chris Mason8b712842008-06-11 16:50:36 -0400463 btrfs_queue_worker(&fs_info->workers, &async->work);
Chris Mason44b8bd72008-04-16 11:14:51 -0400464 return 0;
465}
466
467static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400468 int mirror_num)
Chris Mason22c59942008-04-09 16:28:12 -0400469{
470 struct btrfs_root *root = BTRFS_I(inode)->root;
471 u64 offset;
472 int ret;
473
474 offset = bio->bi_sector << 9;
475
Chris Mason8b712842008-06-11 16:50:36 -0400476 /*
477 * when we're called for a write, we're already in the async
478 * submission context. Just jump ingo btrfs_map_bio
479 */
Chris Mason22c59942008-04-09 16:28:12 -0400480 if (rw & (1 << BIO_RW)) {
Chris Mason8b712842008-06-11 16:50:36 -0400481 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio,
482 mirror_num, 0);
Chris Mason22c59942008-04-09 16:28:12 -0400483 }
484
Chris Mason8b712842008-06-11 16:50:36 -0400485 /*
486 * called for a read, do the setup so that checksum validation
487 * can happen in the async kernel threads
488 */
Chris Mason22c59942008-04-09 16:28:12 -0400489 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1);
490 BUG_ON(ret);
Chris Masonce9adaa2008-04-09 16:28:12 -0400491
Chris Mason8b712842008-06-11 16:50:36 -0400492 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1);
Chris Mason0b86a832008-03-24 15:01:56 -0400493}
494
Chris Mason44b8bd72008-04-16 11:14:51 -0400495static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
496 int mirror_num)
497{
Chris Mason8b712842008-06-11 16:50:36 -0400498 /*
499 * kthread helpers are used to submit writes so that checksumming
500 * can happen in parallel across all CPUs
501 */
Chris Mason44b8bd72008-04-16 11:14:51 -0400502 if (!(rw & (1 << BIO_RW))) {
503 return __btree_submit_bio_hook(inode, rw, bio, mirror_num);
504 }
505 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
506 inode, rw, bio, mirror_num,
507 __btree_submit_bio_hook);
508}
509
Chris Mason5f39d392007-10-15 16:14:19 -0400510static int btree_writepage(struct page *page, struct writeback_control *wbc)
511{
Chris Masond1310b22008-01-24 16:13:08 -0500512 struct extent_io_tree *tree;
513 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400514 return extent_write_full_page(tree, page, btree_get_extent, wbc);
515}
Chris Mason0da54682007-11-07 21:08:01 -0500516
517static int btree_writepages(struct address_space *mapping,
518 struct writeback_control *wbc)
519{
Chris Masond1310b22008-01-24 16:13:08 -0500520 struct extent_io_tree *tree;
521 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500522 if (wbc->sync_mode == WB_SYNC_NONE) {
Chris Mason793955b2007-11-26 16:34:41 -0800523 u64 num_dirty;
524 u64 start = 0;
525 unsigned long thresh = 96 * 1024 * 1024;
Chris Mason448d6402007-11-27 07:52:01 -0800526
527 if (wbc->for_kupdate)
528 return 0;
529
Chris Masonca664622007-11-27 11:16:35 -0500530 if (current_is_pdflush()) {
531 thresh = 96 * 1024 * 1024;
532 } else {
533 thresh = 8 * 1024 * 1024;
534 }
Chris Mason1832a6d2007-12-21 16:27:21 -0500535 num_dirty = count_range_bits(tree, &start, (u64)-1,
536 thresh, EXTENT_DIRTY);
Chris Mason793955b2007-11-26 16:34:41 -0800537 if (num_dirty < thresh) {
538 return 0;
539 }
540 }
Chris Mason0da54682007-11-07 21:08:01 -0500541 return extent_writepages(tree, mapping, btree_get_extent, wbc);
542}
543
Chris Mason5f39d392007-10-15 16:14:19 -0400544int btree_readpage(struct file *file, struct page *page)
545{
Chris Masond1310b22008-01-24 16:13:08 -0500546 struct extent_io_tree *tree;
547 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400548 return extent_read_full_page(tree, page, btree_get_extent);
549}
550
Chris Mason70dec802008-01-29 09:59:12 -0500551static int btree_releasepage(struct page *page, gfp_t gfp_flags)
Chris Mason5f39d392007-10-15 16:14:19 -0400552{
Chris Masond1310b22008-01-24 16:13:08 -0500553 struct extent_io_tree *tree;
554 struct extent_map_tree *map;
Chris Mason5f39d392007-10-15 16:14:19 -0400555 int ret;
556
Chris Mason3dd39912008-04-11 10:51:07 -0400557 if (page_count(page) > 3) {
558 /* once for page->private, once for the caller, once
559 * once for the page cache
560 */
561 return 0;
562 }
Chris Masond1310b22008-01-24 16:13:08 -0500563 tree = &BTRFS_I(page->mapping->host)->io_tree;
564 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason7b13b7b2008-04-18 10:29:50 -0400565 ret = try_release_extent_state(map, tree, page, gfp_flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400566 if (ret == 1) {
Chris Mason728131d2008-04-09 16:28:12 -0400567 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -0400568 ClearPagePrivate(page);
569 set_page_private(page, 0);
570 page_cache_release(page);
571 }
Chris Masond98237b2007-03-28 13:57:48 -0400572 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400573}
Chris Mason123abc82007-03-14 14:14:43 -0400574
Chris Mason5f39d392007-10-15 16:14:19 -0400575static void btree_invalidatepage(struct page *page, unsigned long offset)
Chris Masond98237b2007-03-28 13:57:48 -0400576{
Chris Masond1310b22008-01-24 16:13:08 -0500577 struct extent_io_tree *tree;
578 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400579 extent_invalidatepage(tree, page, offset);
580 btree_releasepage(page, GFP_NOFS);
Chris Mason9ad6b7b2008-04-18 16:11:30 -0400581 if (PagePrivate(page)) {
Chris Mason4ef64ea2008-04-21 08:52:50 -0400582 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Mason9ad6b7b2008-04-18 16:11:30 -0400583 ClearPagePrivate(page);
584 set_page_private(page, 0);
585 page_cache_release(page);
586 }
Chris Masond98237b2007-03-28 13:57:48 -0400587}
588
Chris Mason5f39d392007-10-15 16:14:19 -0400589#if 0
Chris Masond98237b2007-03-28 13:57:48 -0400590static int btree_writepage(struct page *page, struct writeback_control *wbc)
591{
Chris Mason87cbda52007-03-28 19:44:27 -0400592 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400593 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400594 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400595 if (!page_has_buffers(page)) {
596 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
597 (1 << BH_Dirty)|(1 << BH_Uptodate));
598 }
599 head = page_buffers(page);
600 bh = head;
601 do {
602 if (buffer_dirty(bh))
603 csum_tree_block(root, bh, 0);
604 bh = bh->b_this_page;
605 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400606 return block_write_full_page(page, btree_get_block, wbc);
607}
Chris Mason5f39d392007-10-15 16:14:19 -0400608#endif
Chris Masond98237b2007-03-28 13:57:48 -0400609
610static struct address_space_operations btree_aops = {
611 .readpage = btree_readpage,
612 .writepage = btree_writepage,
Chris Mason0da54682007-11-07 21:08:01 -0500613 .writepages = btree_writepages,
Chris Mason5f39d392007-10-15 16:14:19 -0400614 .releasepage = btree_releasepage,
615 .invalidatepage = btree_invalidatepage,
Chris Masond98237b2007-03-28 13:57:48 -0400616 .sync_page = block_sync_page,
617};
618
Chris Masonca7a79a2008-05-12 12:59:19 -0400619int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
620 u64 parent_transid)
Chris Mason090d1872007-05-01 08:53:32 -0400621{
Chris Mason5f39d392007-10-15 16:14:19 -0400622 struct extent_buffer *buf = NULL;
623 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonde428b62007-05-18 13:28:27 -0400624 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400625
Chris Masondb945352007-10-15 16:15:53 -0400626 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400627 if (!buf)
Chris Mason090d1872007-05-01 08:53:32 -0400628 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500629 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
Chris Masonf1885912008-04-09 16:28:12 -0400630 buf, 0, 0, btree_get_extent, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400631 free_extent_buffer(buf);
Chris Masonde428b62007-05-18 13:28:27 -0400632 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400633}
634
Chris Mason0999df52008-04-01 13:48:14 -0400635struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
636 u64 bytenr, u32 blocksize)
637{
638 struct inode *btree_inode = root->fs_info->btree_inode;
639 struct extent_buffer *eb;
640 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
641 bytenr, blocksize, GFP_NOFS);
642 return eb;
643}
644
645struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
646 u64 bytenr, u32 blocksize)
647{
648 struct inode *btree_inode = root->fs_info->btree_inode;
649 struct extent_buffer *eb;
650
651 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
652 bytenr, blocksize, NULL, GFP_NOFS);
653 return eb;
654}
655
656
Chris Masondb945352007-10-15 16:15:53 -0400657struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400658 u32 blocksize, u64 parent_transid)
Chris Masone20d96d2007-03-22 12:13:20 -0400659{
Chris Mason5f39d392007-10-15 16:14:19 -0400660 struct extent_buffer *buf = NULL;
661 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -0500662 struct extent_io_tree *io_tree;
Chris Mason19c00dd2007-10-15 16:19:22 -0400663 int ret;
664
Chris Masond1310b22008-01-24 16:13:08 -0500665 io_tree = &BTRFS_I(btree_inode)->io_tree;
Chris Masone20d96d2007-03-22 12:13:20 -0400666
Chris Masondb945352007-10-15 16:15:53 -0400667 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400668 if (!buf)
669 return NULL;
Chris Masone4204de2008-01-08 15:46:27 -0500670
Chris Masonca7a79a2008-05-12 12:59:19 -0400671 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
Chris Masonce9adaa2008-04-09 16:28:12 -0400672
673 if (ret == 0) {
674 buf->flags |= EXTENT_UPTODATE;
675 }
Chris Mason5f39d392007-10-15 16:14:19 -0400676 return buf;
Chris Masonce9adaa2008-04-09 16:28:12 -0400677
Chris Masoneb60cea2007-02-02 09:18:22 -0500678}
679
Chris Masone089f052007-03-16 16:20:31 -0400680int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400681 struct extent_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500682{
Chris Mason5f39d392007-10-15 16:14:19 -0400683 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason55c69072008-01-09 15:55:33 -0500684 if (btrfs_header_generation(buf) ==
Chris Mason925baed2008-06-25 16:01:30 -0400685 root->fs_info->running_transaction->transid) {
686 WARN_ON(!btrfs_tree_locked(buf));
Chris Masond1310b22008-01-24 16:13:08 -0500687 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
Chris Mason55c69072008-01-09 15:55:33 -0500688 buf);
Chris Mason925baed2008-06-25 16:01:30 -0400689 }
Chris Mason5f39d392007-10-15 16:14:19 -0400690 return 0;
691}
692
693int wait_on_tree_block_writeback(struct btrfs_root *root,
694 struct extent_buffer *buf)
695{
696 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -0500697 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -0400698 buf);
699 return 0;
700}
701
Chris Masondb945352007-10-15 16:15:53 -0400702static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500703 u32 stripesize, struct btrfs_root *root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400704 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400705 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500706{
Chris Masoncfaa7292007-02-21 17:04:57 -0500707 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400708 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500709 root->commit_root = NULL;
Chris Masondb945352007-10-15 16:15:53 -0400710 root->sectorsize = sectorsize;
711 root->nodesize = nodesize;
712 root->leafsize = leafsize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500713 root->stripesize = stripesize;
Chris Mason123abc82007-03-14 14:14:43 -0400714 root->ref_cows = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400715 root->track_dirty = 0;
716
Chris Mason9f5fae22007-03-20 14:38:32 -0400717 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400718 root->objectid = objectid;
719 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400720 root->highest_inode = 0;
721 root->last_inode_alloc = 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400722 root->name = NULL;
Chris Mason4313b392008-01-03 09:08:48 -0500723 root->in_sysfs = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400724
725 INIT_LIST_HEAD(&root->dirty_list);
Chris Mason925baed2008-06-25 16:01:30 -0400726 spin_lock_init(&root->node_lock);
Chris Masona2135012008-06-25 16:01:30 -0400727 mutex_init(&root->objectid_mutex);
Chris Mason3768f362007-03-13 16:47:54 -0400728 memset(&root->root_key, 0, sizeof(root->root_key));
729 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -0400730 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Josef Bacik58176a92007-08-29 15:47:34 -0400731 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
732 init_completion(&root->kobj_unregister);
Chris Mason6702ed42007-08-07 16:15:09 -0400733 root->defrag_running = 0;
734 root->defrag_level = 0;
Chris Mason4d775672007-04-20 20:23:12 -0400735 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400736 return 0;
737}
738
Chris Masondb945352007-10-15 16:15:53 -0400739static int find_and_setup_root(struct btrfs_root *tree_root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400740 struct btrfs_fs_info *fs_info,
741 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400742 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400743{
744 int ret;
Chris Masondb945352007-10-15 16:15:53 -0400745 u32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -0400746
Chris Masondb945352007-10-15 16:15:53 -0400747 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500748 tree_root->sectorsize, tree_root->stripesize,
749 root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400750 ret = btrfs_find_last_root(tree_root, objectid,
751 &root->root_item, &root->root_key);
752 BUG_ON(ret);
753
Chris Masondb945352007-10-15 16:15:53 -0400754 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
755 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
Chris Masonca7a79a2008-05-12 12:59:19 -0400756 blocksize, 0);
Chris Mason3768f362007-03-13 16:47:54 -0400757 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500758 return 0;
759}
760
Chris Mason5eda7b52007-06-22 14:16:25 -0400761struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
762 struct btrfs_key *location)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400763{
764 struct btrfs_root *root;
765 struct btrfs_root *tree_root = fs_info->tree_root;
766 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400767 struct extent_buffer *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400768 u64 highest_inode;
Chris Masondb945352007-10-15 16:15:53 -0400769 u32 blocksize;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400770 int ret = 0;
771
Chris Mason5eda7b52007-06-22 14:16:25 -0400772 root = kzalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400773 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400774 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400775 if (location->offset == (u64)-1) {
Chris Masondb945352007-10-15 16:15:53 -0400776 ret = find_and_setup_root(tree_root, fs_info,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400777 location->objectid, root);
778 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400779 kfree(root);
780 return ERR_PTR(ret);
781 }
782 goto insert;
783 }
784
Chris Masondb945352007-10-15 16:15:53 -0400785 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500786 tree_root->sectorsize, tree_root->stripesize,
787 root, fs_info, location->objectid);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400788
789 path = btrfs_alloc_path();
790 BUG_ON(!path);
791 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
792 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400793 if (ret > 0)
794 ret = -ENOENT;
795 goto out;
796 }
Chris Mason5f39d392007-10-15 16:14:19 -0400797 l = path->nodes[0];
798 read_extent_buffer(l, &root->root_item,
799 btrfs_item_ptr_offset(l, path->slots[0]),
Chris Mason0f7d52f2007-04-09 10:42:37 -0400800 sizeof(root->root_item));
Yan Zheng44b36eb2007-10-19 09:22:56 -0400801 memcpy(&root->root_key, location, sizeof(*location));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400802 ret = 0;
803out:
804 btrfs_release_path(root, path);
805 btrfs_free_path(path);
806 if (ret) {
807 kfree(root);
808 return ERR_PTR(ret);
809 }
Chris Masondb945352007-10-15 16:15:53 -0400810 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
811 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
Chris Masonca7a79a2008-05-12 12:59:19 -0400812 blocksize, 0);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400813 BUG_ON(!root->node);
814insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400815 root->ref_cows = 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400816 ret = btrfs_find_highest_inode(root, &highest_inode);
817 if (ret == 0) {
818 root->highest_inode = highest_inode;
819 root->last_inode_alloc = highest_inode;
820 }
821 return root;
822}
823
Chris Masondc17ff82008-01-08 15:46:30 -0500824struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
825 u64 root_objectid)
826{
827 struct btrfs_root *root;
828
829 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
830 return fs_info->tree_root;
831 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
832 return fs_info->extent_root;
833
834 root = radix_tree_lookup(&fs_info->fs_roots_radix,
835 (unsigned long)root_objectid);
836 return root;
837}
838
Chris Masonedbd8d42007-12-21 16:27:24 -0500839struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
840 struct btrfs_key *location)
Chris Mason5eda7b52007-06-22 14:16:25 -0400841{
842 struct btrfs_root *root;
843 int ret;
844
Chris Masonedbd8d42007-12-21 16:27:24 -0500845 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
846 return fs_info->tree_root;
847 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
848 return fs_info->extent_root;
Chris Mason8f18cf12008-04-25 16:53:30 -0400849 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
850 return fs_info->chunk_root;
851 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
852 return fs_info->dev_root;
Chris Masonedbd8d42007-12-21 16:27:24 -0500853
Chris Mason5eda7b52007-06-22 14:16:25 -0400854 root = radix_tree_lookup(&fs_info->fs_roots_radix,
855 (unsigned long)location->objectid);
856 if (root)
857 return root;
858
859 root = btrfs_read_fs_root_no_radix(fs_info, location);
860 if (IS_ERR(root))
861 return root;
Chris Mason2619ba12007-04-10 16:58:11 -0400862 ret = radix_tree_insert(&fs_info->fs_roots_radix,
863 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400864 root);
865 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400866 free_extent_buffer(root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400867 kfree(root);
868 return ERR_PTR(ret);
869 }
Chris Masonedbd8d42007-12-21 16:27:24 -0500870 ret = btrfs_find_dead_roots(fs_info->tree_root,
871 root->root_key.objectid, root);
872 BUG_ON(ret);
873
874 return root;
875}
876
877struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
878 struct btrfs_key *location,
879 const char *name, int namelen)
880{
881 struct btrfs_root *root;
882 int ret;
883
884 root = btrfs_read_fs_root_no_name(fs_info, location);
885 if (!root)
886 return NULL;
Josef Bacik58176a92007-08-29 15:47:34 -0400887
Chris Mason4313b392008-01-03 09:08:48 -0500888 if (root->in_sysfs)
889 return root;
890
Josef Bacik58176a92007-08-29 15:47:34 -0400891 ret = btrfs_set_root_name(root, name, namelen);
892 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400893 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400894 kfree(root);
895 return ERR_PTR(ret);
896 }
897
898 ret = btrfs_sysfs_add_root(root);
899 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400900 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400901 kfree(root->name);
902 kfree(root);
903 return ERR_PTR(ret);
904 }
Chris Mason4313b392008-01-03 09:08:48 -0500905 root->in_sysfs = 1;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400906 return root;
907}
Chris Mason19c00dd2007-10-15 16:19:22 -0400908#if 0
909static int add_hasher(struct btrfs_fs_info *info, char *type) {
910 struct btrfs_hasher *hasher;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400911
Chris Mason19c00dd2007-10-15 16:19:22 -0400912 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
913 if (!hasher)
914 return -ENOMEM;
915 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
916 if (!hasher->hash_tfm) {
917 kfree(hasher);
918 return -EINVAL;
919 }
920 spin_lock(&info->hash_lock);
921 list_add(&hasher->list, &info->hashers);
922 spin_unlock(&info->hash_lock);
923 return 0;
924}
925#endif
Chris Mason04160082008-03-26 10:28:07 -0400926
927static int btrfs_congested_fn(void *congested_data, int bdi_bits)
928{
929 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
930 int ret = 0;
Chris Masoncb03c742008-05-15 16:15:45 -0400931 int limit = 256 * info->fs_devices->open_devices;
Chris Mason04160082008-03-26 10:28:07 -0400932 struct list_head *cur;
933 struct btrfs_device *device;
934 struct backing_dev_info *bdi;
935
Chris Masoncb03c742008-05-15 16:15:45 -0400936 if ((bdi_bits & (1 << BDI_write_congested)) &&
937 atomic_read(&info->nr_async_submits) > limit) {
938 return 1;
939 }
940
Chris Mason04160082008-03-26 10:28:07 -0400941 list_for_each(cur, &info->fs_devices->devices) {
942 device = list_entry(cur, struct btrfs_device, dev_list);
Chris Masondfe25022008-05-13 13:46:40 -0400943 if (!device->bdev)
944 continue;
Chris Mason04160082008-03-26 10:28:07 -0400945 bdi = blk_get_backing_dev_info(device->bdev);
946 if (bdi && bdi_congested(bdi, bdi_bits)) {
947 ret = 1;
948 break;
949 }
950 }
951 return ret;
952}
953
Chris Mason38b66982008-04-22 09:22:11 -0400954/*
955 * this unplugs every device on the box, and it is only used when page
956 * is null
957 */
958static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
959{
960 struct list_head *cur;
961 struct btrfs_device *device;
962 struct btrfs_fs_info *info;
963
964 info = (struct btrfs_fs_info *)bdi->unplug_io_data;
965 list_for_each(cur, &info->fs_devices->devices) {
966 device = list_entry(cur, struct btrfs_device, dev_list);
967 bdi = blk_get_backing_dev_info(device->bdev);
968 if (bdi->unplug_io_fn) {
969 bdi->unplug_io_fn(bdi, page);
970 }
971 }
972}
973
Chris Mason04160082008-03-26 10:28:07 -0400974void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
975{
Chris Mason38b66982008-04-22 09:22:11 -0400976 struct inode *inode;
Chris Masonf2d8d742008-04-21 10:03:05 -0400977 struct extent_map_tree *em_tree;
978 struct extent_map *em;
Chris Masonbcbfce82008-04-22 13:26:47 -0400979 struct address_space *mapping;
Chris Mason38b66982008-04-22 09:22:11 -0400980 u64 offset;
981
Chris Masonbcbfce82008-04-22 13:26:47 -0400982 /* the generic O_DIRECT read code does this */
Chris Mason38b66982008-04-22 09:22:11 -0400983 if (!page) {
984 __unplug_io_fn(bdi, page);
985 return;
986 }
987
Chris Masonbcbfce82008-04-22 13:26:47 -0400988 /*
989 * page->mapping may change at any time. Get a consistent copy
990 * and use that for everything below
991 */
992 smp_mb();
993 mapping = page->mapping;
994 if (!mapping)
995 return;
996
997 inode = mapping->host;
Chris Mason38b66982008-04-22 09:22:11 -0400998 offset = page_offset(page);
Chris Mason04160082008-03-26 10:28:07 -0400999
Chris Masonf2d8d742008-04-21 10:03:05 -04001000 em_tree = &BTRFS_I(inode)->extent_tree;
1001 spin_lock(&em_tree->lock);
1002 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
1003 spin_unlock(&em_tree->lock);
1004 if (!em)
1005 return;
1006
1007 offset = offset - em->start;
1008 btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree,
1009 em->block_start + offset, page);
1010 free_extent_map(em);
Chris Mason04160082008-03-26 10:28:07 -04001011}
1012
1013static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
1014{
Jan Engelhardt51ebc0d2008-06-09 22:19:40 -04001015#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason04160082008-03-26 10:28:07 -04001016 bdi_init(bdi);
Chris Masonb248a412008-04-14 09:48:18 -04001017#endif
Chris Mason4575c9c2008-04-18 16:13:31 -04001018 bdi->ra_pages = default_backing_dev_info.ra_pages;
Chris Mason04160082008-03-26 10:28:07 -04001019 bdi->state = 0;
1020 bdi->capabilities = default_backing_dev_info.capabilities;
1021 bdi->unplug_io_fn = btrfs_unplug_io_fn;
1022 bdi->unplug_io_data = info;
1023 bdi->congested_fn = btrfs_congested_fn;
1024 bdi->congested_data = info;
1025 return 0;
1026}
1027
Chris Masonce9adaa2008-04-09 16:28:12 -04001028static int bio_ready_for_csum(struct bio *bio)
1029{
1030 u64 length = 0;
1031 u64 buf_len = 0;
1032 u64 start = 0;
1033 struct page *page;
1034 struct extent_io_tree *io_tree = NULL;
1035 struct btrfs_fs_info *info = NULL;
1036 struct bio_vec *bvec;
1037 int i;
1038 int ret;
1039
1040 bio_for_each_segment(bvec, bio, i) {
1041 page = bvec->bv_page;
1042 if (page->private == EXTENT_PAGE_PRIVATE) {
1043 length += bvec->bv_len;
1044 continue;
1045 }
1046 if (!page->private) {
1047 length += bvec->bv_len;
1048 continue;
1049 }
1050 length = bvec->bv_len;
1051 buf_len = page->private >> 2;
1052 start = page_offset(page) + bvec->bv_offset;
1053 io_tree = &BTRFS_I(page->mapping->host)->io_tree;
1054 info = BTRFS_I(page->mapping->host)->root->fs_info;
1055 }
1056 /* are we fully contained in this bio? */
1057 if (buf_len <= length)
1058 return 1;
1059
1060 ret = extent_range_uptodate(io_tree, start + length,
1061 start + buf_len - 1);
1062 if (ret == 1)
1063 return ret;
1064 return ret;
1065}
1066
Chris Mason8b712842008-06-11 16:50:36 -04001067/*
1068 * called by the kthread helper functions to finally call the bio end_io
1069 * functions. This is where read checksum verification actually happens
1070 */
1071static void end_workqueue_fn(struct btrfs_work *work)
Chris Masonce9adaa2008-04-09 16:28:12 -04001072{
Chris Masonce9adaa2008-04-09 16:28:12 -04001073 struct bio *bio;
Chris Mason8b712842008-06-11 16:50:36 -04001074 struct end_io_wq *end_io_wq;
1075 struct btrfs_fs_info *fs_info;
Chris Masonce9adaa2008-04-09 16:28:12 -04001076 int error;
Chris Masonce9adaa2008-04-09 16:28:12 -04001077
Chris Mason8b712842008-06-11 16:50:36 -04001078 end_io_wq = container_of(work, struct end_io_wq, work);
1079 bio = end_io_wq->bio;
1080 fs_info = end_io_wq->info;
Chris Masonce9adaa2008-04-09 16:28:12 -04001081
Chris Mason8b712842008-06-11 16:50:36 -04001082 /* metadata bios are special because the whole tree block must
1083 * be checksummed at once. This makes sure the entire block is in
1084 * ram and up to date before trying to verify things. For
1085 * blocksize <= pagesize, it is basically a noop
1086 */
1087 if (end_io_wq->metadata && !bio_ready_for_csum(bio)) {
1088 btrfs_queue_worker(&fs_info->endio_workers,
1089 &end_io_wq->work);
1090 return;
1091 }
1092 error = end_io_wq->error;
1093 bio->bi_private = end_io_wq->private;
1094 bio->bi_end_io = end_io_wq->end_io;
1095 kfree(end_io_wq);
Miguel73f61b22008-04-11 15:50:59 -04001096#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Mason8b712842008-06-11 16:50:36 -04001097 bio_endio(bio, bio->bi_size, error);
Miguel73f61b22008-04-11 15:50:59 -04001098#else
Chris Mason8b712842008-06-11 16:50:36 -04001099 bio_endio(bio, error);
Miguel73f61b22008-04-11 15:50:59 -04001100#endif
Chris Mason44b8bd72008-04-16 11:14:51 -04001101}
1102
Chris Mason8a4b83c2008-03-24 15:02:07 -04001103struct btrfs_root *open_ctree(struct super_block *sb,
Chris Masondfe25022008-05-13 13:46:40 -04001104 struct btrfs_fs_devices *fs_devices,
1105 char *options)
Chris Masoneb60cea2007-02-02 09:18:22 -05001106{
Chris Masondb945352007-10-15 16:15:53 -04001107 u32 sectorsize;
1108 u32 nodesize;
1109 u32 leafsize;
1110 u32 blocksize;
Chris Mason87ee04e2007-11-30 11:30:34 -05001111 u32 stripesize;
Chris Masona061fc82008-05-07 11:43:44 -04001112 struct buffer_head *bh;
Chris Masone20d96d2007-03-22 12:13:20 -04001113 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
1114 GFP_NOFS);
1115 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
1116 GFP_NOFS);
Chris Mason8790d502008-04-03 16:29:03 -04001117 struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
Chris Masone20d96d2007-03-22 12:13:20 -04001118 GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04001119 struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
1120 GFP_NOFS);
1121 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
1122 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -05001123 int ret;
Yane58ca022008-04-01 11:21:34 -04001124 int err = -EINVAL;
Chris Mason4543df72008-06-11 21:47:56 -04001125
Chris Mason2c90e5d2007-04-02 10:50:19 -04001126 struct btrfs_super_block *disk_super;
Chris Mason8790d502008-04-03 16:29:03 -04001127
Chris Mason39279cc2007-06-12 06:35:45 -04001128 if (!extent_root || !tree_root || !fs_info) {
1129 err = -ENOMEM;
1130 goto fail;
1131 }
Chris Mason0f7d52f2007-04-09 10:42:37 -04001132 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason8fd17792007-04-19 21:01:03 -04001133 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -04001134 INIT_LIST_HEAD(&fs_info->dead_roots);
Chris Mason19c00dd2007-10-15 16:19:22 -04001135 INIT_LIST_HEAD(&fs_info->hashers);
1136 spin_lock_init(&fs_info->hash_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -05001137 spin_lock_init(&fs_info->delalloc_lock);
Chris Masoncee36a02008-01-15 08:40:48 -05001138 spin_lock_init(&fs_info->new_trans_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -04001139
Josef Bacik58176a92007-08-29 15:47:34 -04001140 init_completion(&fs_info->kobj_unregister);
Chris Mason9f5fae22007-03-20 14:38:32 -04001141 fs_info->tree_root = tree_root;
1142 fs_info->extent_root = extent_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001143 fs_info->chunk_root = chunk_root;
1144 fs_info->dev_root = dev_root;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001145 fs_info->fs_devices = fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04001146 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
Chris Mason6324fbf2008-03-24 15:01:59 -04001147 INIT_LIST_HEAD(&fs_info->space_info);
Chris Mason0b86a832008-03-24 15:01:56 -04001148 btrfs_mapping_init(&fs_info->mapping_tree);
Chris Masoncb03c742008-05-15 16:15:45 -04001149 atomic_set(&fs_info->nr_async_submits, 0);
Chris Masona2135012008-06-25 16:01:30 -04001150 atomic_set(&fs_info->throttles, 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001151 fs_info->sb = sb;
Chris Masonc59f8952007-12-17 20:14:04 -05001152 fs_info->max_extent = (u64)-1;
Chris Mason6f568d32008-01-29 16:03:38 -05001153 fs_info->max_inline = 8192 * 1024;
Chris Mason04160082008-03-26 10:28:07 -04001154 setup_bdi(fs_info, &fs_info->bdi);
Chris Masond98237b2007-03-28 13:57:48 -04001155 fs_info->btree_inode = new_inode(sb);
1156 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001157 fs_info->btree_inode->i_nlink = 1;
Chris Mason4543df72008-06-11 21:47:56 -04001158 fs_info->thread_pool_size = min(num_online_cpus() + 2, 8);
Chris Mason0afbaf82008-04-18 14:17:20 -04001159
Chris Masona061fc82008-05-07 11:43:44 -04001160 sb->s_blocksize = 4096;
1161 sb->s_blocksize_bits = blksize_bits(4096);
1162
Chris Mason0afbaf82008-04-18 14:17:20 -04001163 /*
1164 * we set the i_size on the btree inode to the max possible int.
1165 * the real end of the address space is determined by all of
1166 * the devices in the system
1167 */
1168 fs_info->btree_inode->i_size = OFFSET_MAX;
Chris Masond98237b2007-03-28 13:57:48 -04001169 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason04160082008-03-26 10:28:07 -04001170 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
1171
Chris Masond1310b22008-01-24 16:13:08 -05001172 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -04001173 fs_info->btree_inode->i_mapping,
1174 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001175 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
1176 GFP_NOFS);
Chris Mason0da54682007-11-07 21:08:01 -05001177
Chris Masond1310b22008-01-24 16:13:08 -05001178 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
1179
1180 extent_io_tree_init(&fs_info->free_space_cache,
Chris Masonf510cfe2007-10-15 16:14:48 -04001181 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001182 extent_io_tree_init(&fs_info->block_group_cache,
Chris Mason96b51792007-10-15 16:15:19 -04001183 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001184 extent_io_tree_init(&fs_info->pinned_extents,
Chris Mason1a5bc162007-10-15 16:15:26 -04001185 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001186 extent_io_tree_init(&fs_info->pending_del,
Chris Mason1a5bc162007-10-15 16:15:26 -04001187 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001188 extent_io_tree_init(&fs_info->extent_ins,
Chris Mason1a5bc162007-10-15 16:15:26 -04001189 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masone66f7092007-04-20 13:16:02 -04001190 fs_info->do_barriers = 1;
Chris Masone18e4802008-01-18 10:54:22 -05001191
Chris Mason6da6aba2007-12-18 16:15:09 -05001192#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1193 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
1194#else
Chris Mason08607c12007-06-08 15:33:54 -04001195 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason6da6aba2007-12-18 16:15:09 -05001196#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -04001197 BTRFS_I(fs_info->btree_inode)->root = tree_root;
1198 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
1199 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -04001200 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -04001201 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001202
Chris Mason79154b12007-03-22 15:59:16 -04001203 mutex_init(&fs_info->trans_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001204 mutex_init(&fs_info->drop_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04001205 mutex_init(&fs_info->alloc_mutex);
1206 mutex_init(&fs_info->chunk_mutex);
Chris Mason3768f362007-03-13 16:47:54 -04001207
Chris Mason19c00dd2007-10-15 16:19:22 -04001208#if 0
1209 ret = add_hasher(fs_info, "crc32c");
1210 if (ret) {
1211 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
1212 err = -ENOMEM;
1213 goto fail_iput;
1214 }
1215#endif
Chris Mason0b86a832008-03-24 15:01:56 -04001216 __setup_root(4096, 4096, 4096, 4096, tree_root,
Chris Mason2c90e5d2007-04-02 10:50:19 -04001217 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -04001218
Chris Masond98237b2007-03-28 13:57:48 -04001219
Chris Masona061fc82008-05-07 11:43:44 -04001220 bh = __bread(fs_devices->latest_bdev,
1221 BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
1222 if (!bh)
Chris Mason39279cc2007-06-12 06:35:45 -04001223 goto fail_iput;
Chris Mason39279cc2007-06-12 06:35:45 -04001224
Chris Masona061fc82008-05-07 11:43:44 -04001225 memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
1226 brelse(bh);
Chris Mason5f39d392007-10-15 16:14:19 -04001227
Chris Masona061fc82008-05-07 11:43:44 -04001228 memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001229
Chris Mason5f39d392007-10-15 16:14:19 -04001230 disk_super = &fs_info->super_copy;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001231 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -04001232 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001233
Christoph Hellwigedf24ab2008-06-10 10:40:29 -04001234 err = btrfs_parse_options(tree_root, options);
1235 if (err)
1236 goto fail_sb_buffer;
Chris Masondfe25022008-05-13 13:46:40 -04001237
Chris Mason4543df72008-06-11 21:47:56 -04001238 /*
1239 * we need to start all the end_io workers up front because the
1240 * queue work function gets called at interrupt time, and so it
1241 * cannot dynamically grow.
1242 */
1243 btrfs_init_workers(&fs_info->workers, fs_info->thread_pool_size);
Chris Mason1cc127b2008-06-12 14:46:17 -04001244 btrfs_init_workers(&fs_info->submit_workers, fs_info->thread_pool_size);
Chris Mason4543df72008-06-11 21:47:56 -04001245 btrfs_init_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
1246 btrfs_start_workers(&fs_info->workers, 1);
Chris Mason1cc127b2008-06-12 14:46:17 -04001247 btrfs_start_workers(&fs_info->submit_workers, 1);
Chris Mason4543df72008-06-11 21:47:56 -04001248 btrfs_start_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
1249
1250
Christoph Hellwigedf24ab2008-06-10 10:40:29 -04001251 err = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001252 if (btrfs_super_num_devices(disk_super) > fs_devices->open_devices) {
Chris Mason8a4b83c2008-03-24 15:02:07 -04001253 printk("Btrfs: wanted %llu devices, but found %llu\n",
1254 (unsigned long long)btrfs_super_num_devices(disk_super),
Chris Masona0af4692008-05-13 16:03:06 -04001255 (unsigned long long)fs_devices->open_devices);
Chris Masondfe25022008-05-13 13:46:40 -04001256 if (btrfs_test_opt(tree_root, DEGRADED))
1257 printk("continuing in degraded mode\n");
1258 else {
1259 goto fail_sb_buffer;
1260 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001261 }
Chris Masondfe25022008-05-13 13:46:40 -04001262
Chris Mason4575c9c2008-04-18 16:13:31 -04001263 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
1264
Chris Masondb945352007-10-15 16:15:53 -04001265 nodesize = btrfs_super_nodesize(disk_super);
1266 leafsize = btrfs_super_leafsize(disk_super);
1267 sectorsize = btrfs_super_sectorsize(disk_super);
Chris Mason87ee04e2007-11-30 11:30:34 -05001268 stripesize = btrfs_super_stripesize(disk_super);
Chris Masondb945352007-10-15 16:15:53 -04001269 tree_root->nodesize = nodesize;
1270 tree_root->leafsize = leafsize;
1271 tree_root->sectorsize = sectorsize;
Chris Mason87ee04e2007-11-30 11:30:34 -05001272 tree_root->stripesize = stripesize;
Chris Masona061fc82008-05-07 11:43:44 -04001273
1274 sb->s_blocksize = sectorsize;
1275 sb->s_blocksize_bits = blksize_bits(sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04001276
Chris Mason39279cc2007-06-12 06:35:45 -04001277 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
1278 sizeof(disk_super->magic))) {
1279 printk("btrfs: valid FS not found on %s\n", sb->s_id);
1280 goto fail_sb_buffer;
1281 }
Chris Mason19c00dd2007-10-15 16:19:22 -04001282
Chris Mason925baed2008-06-25 16:01:30 -04001283 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04001284 ret = btrfs_read_sys_array(tree_root);
Chris Mason925baed2008-06-25 16:01:30 -04001285 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason84eed902008-04-25 09:04:37 -04001286 if (ret) {
1287 printk("btrfs: failed to read the system array on %s\n",
1288 sb->s_id);
1289 goto fail_sys_array;
1290 }
Chris Mason0b86a832008-03-24 15:01:56 -04001291
1292 blocksize = btrfs_level_size(tree_root,
1293 btrfs_super_chunk_root_level(disk_super));
1294
1295 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1296 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1297
1298 chunk_root->node = read_tree_block(chunk_root,
1299 btrfs_super_chunk_root(disk_super),
Chris Masonca7a79a2008-05-12 12:59:19 -04001300 blocksize, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001301 BUG_ON(!chunk_root->node);
1302
Chris Masone17cade2008-04-15 15:41:47 -04001303 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
1304 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
1305 BTRFS_UUID_SIZE);
1306
Chris Mason925baed2008-06-25 16:01:30 -04001307 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04001308 ret = btrfs_read_chunk_tree(chunk_root);
Chris Mason925baed2008-06-25 16:01:30 -04001309 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04001310 BUG_ON(ret);
1311
Chris Masondfe25022008-05-13 13:46:40 -04001312 btrfs_close_extra_devices(fs_devices);
1313
Chris Masondb945352007-10-15 16:15:53 -04001314 blocksize = btrfs_level_size(tree_root,
1315 btrfs_super_root_level(disk_super));
Chris Mason19c00dd2007-10-15 16:19:22 -04001316
Chris Mason0b86a832008-03-24 15:01:56 -04001317
Chris Masone20d96d2007-03-22 12:13:20 -04001318 tree_root->node = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -04001319 btrfs_super_root(disk_super),
Chris Masonca7a79a2008-05-12 12:59:19 -04001320 blocksize, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001321 if (!tree_root->node)
1322 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -04001323
Chris Masondb945352007-10-15 16:15:53 -04001324
1325 ret = find_and_setup_root(tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -04001326 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason0b86a832008-03-24 15:01:56 -04001327 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001328 goto fail_tree_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001329 extent_root->track_dirty = 1;
1330
1331 ret = find_and_setup_root(tree_root, fs_info,
1332 BTRFS_DEV_TREE_OBJECTID, dev_root);
1333 dev_root->track_dirty = 1;
1334
1335 if (ret)
1336 goto fail_extent_root;
Chris Mason3768f362007-03-13 16:47:54 -04001337
Chris Mason9078a3e2007-04-26 16:46:15 -04001338 btrfs_read_block_groups(extent_root);
1339
Chris Mason0f7d52f2007-04-09 10:42:37 -04001340 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Masond18a2c42008-04-04 15:40:00 -04001341 fs_info->data_alloc_profile = (u64)-1;
1342 fs_info->metadata_alloc_profile = (u64)-1;
1343 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1344
Chris Mason0f7d52f2007-04-09 10:42:37 -04001345 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -04001346
Chris Mason0b86a832008-03-24 15:01:56 -04001347fail_extent_root:
1348 free_extent_buffer(extent_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -04001349fail_tree_root:
Chris Mason5f39d392007-10-15 16:14:19 -04001350 free_extent_buffer(tree_root->node);
Chris Mason84eed902008-04-25 09:04:37 -04001351fail_sys_array:
Chris Mason39279cc2007-06-12 06:35:45 -04001352fail_sb_buffer:
Chris Mason2d2ae542008-03-26 16:24:23 -04001353 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
Chris Mason8b712842008-06-11 16:50:36 -04001354 btrfs_stop_workers(&fs_info->workers);
1355 btrfs_stop_workers(&fs_info->endio_workers);
Chris Mason1cc127b2008-06-12 14:46:17 -04001356 btrfs_stop_workers(&fs_info->submit_workers);
Chris Mason4543df72008-06-11 21:47:56 -04001357fail_iput:
1358 iput(fs_info->btree_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001359fail:
Chris Masondfe25022008-05-13 13:46:40 -04001360 btrfs_close_devices(fs_info->fs_devices);
Chris Mason84eed902008-04-25 09:04:37 -04001361 btrfs_mapping_tree_free(&fs_info->mapping_tree);
1362
Chris Mason39279cc2007-06-12 06:35:45 -04001363 kfree(extent_root);
1364 kfree(tree_root);
Jan Engelhardt51ebc0d2008-06-09 22:19:40 -04001365#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason2d2ae542008-03-26 16:24:23 -04001366 bdi_destroy(&fs_info->bdi);
Chris Masonb248a412008-04-14 09:48:18 -04001367#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001368 kfree(fs_info);
1369 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -05001370}
1371
Chris Masonf2984462008-04-10 16:19:33 -04001372static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
1373{
1374 char b[BDEVNAME_SIZE];
1375
1376 if (uptodate) {
1377 set_buffer_uptodate(bh);
1378 } else {
1379 if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
1380 printk(KERN_WARNING "lost page write due to "
1381 "I/O error on %s\n",
1382 bdevname(bh->b_bdev, b));
1383 }
Chris Mason1259ab72008-05-12 13:39:03 -04001384 /* note, we dont' set_buffer_write_io_error because we have
1385 * our own ways of dealing with the IO errors
1386 */
Chris Masonf2984462008-04-10 16:19:33 -04001387 clear_buffer_uptodate(bh);
1388 }
1389 unlock_buffer(bh);
1390 put_bh(bh);
1391}
1392
1393int write_all_supers(struct btrfs_root *root)
1394{
1395 struct list_head *cur;
1396 struct list_head *head = &root->fs_info->fs_devices->devices;
1397 struct btrfs_device *dev;
Chris Masona061fc82008-05-07 11:43:44 -04001398 struct btrfs_super_block *sb;
Chris Masonf2984462008-04-10 16:19:33 -04001399 struct btrfs_dev_item *dev_item;
1400 struct buffer_head *bh;
1401 int ret;
1402 int do_barriers;
Chris Masona236aed2008-04-29 09:38:00 -04001403 int max_errors;
1404 int total_errors = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001405 u32 crc;
1406 u64 flags;
Chris Masonf2984462008-04-10 16:19:33 -04001407
Chris Masona236aed2008-04-29 09:38:00 -04001408 max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
Chris Masonf2984462008-04-10 16:19:33 -04001409 do_barriers = !btrfs_test_opt(root, NOBARRIER);
1410
Chris Masona061fc82008-05-07 11:43:44 -04001411 sb = &root->fs_info->super_for_commit;
1412 dev_item = &sb->dev_item;
Chris Masonf2984462008-04-10 16:19:33 -04001413 list_for_each(cur, head) {
1414 dev = list_entry(cur, struct btrfs_device, dev_list);
Chris Masondfe25022008-05-13 13:46:40 -04001415 if (!dev->bdev) {
1416 total_errors++;
1417 continue;
1418 }
1419 if (!dev->in_fs_metadata)
1420 continue;
1421
Chris Masona061fc82008-05-07 11:43:44 -04001422 btrfs_set_stack_device_type(dev_item, dev->type);
1423 btrfs_set_stack_device_id(dev_item, dev->devid);
1424 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1425 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1426 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1427 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1428 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1429 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1430 flags = btrfs_super_flags(sb);
1431 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonf2984462008-04-10 16:19:33 -04001432
Chris Masonf2984462008-04-10 16:19:33 -04001433
Chris Masona061fc82008-05-07 11:43:44 -04001434 crc = ~(u32)0;
1435 crc = btrfs_csum_data(root, (char *)sb + BTRFS_CSUM_SIZE, crc,
1436 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1437 btrfs_csum_final(crc, sb->csum);
1438
1439 bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET / 4096,
Chris Masonf2984462008-04-10 16:19:33 -04001440 BTRFS_SUPER_INFO_SIZE);
1441
Chris Masona061fc82008-05-07 11:43:44 -04001442 memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -04001443 dev->pending_io = bh;
1444
1445 get_bh(bh);
1446 set_buffer_uptodate(bh);
1447 lock_buffer(bh);
1448 bh->b_end_io = btrfs_end_buffer_write_sync;
1449
1450 if (do_barriers && dev->barriers) {
1451 ret = submit_bh(WRITE_BARRIER, bh);
1452 if (ret == -EOPNOTSUPP) {
1453 printk("btrfs: disabling barriers on dev %s\n",
1454 dev->name);
1455 set_buffer_uptodate(bh);
1456 dev->barriers = 0;
1457 get_bh(bh);
1458 lock_buffer(bh);
1459 ret = submit_bh(WRITE, bh);
1460 }
1461 } else {
1462 ret = submit_bh(WRITE, bh);
1463 }
Chris Masona236aed2008-04-29 09:38:00 -04001464 if (ret)
1465 total_errors++;
Chris Masonf2984462008-04-10 16:19:33 -04001466 }
Chris Masona236aed2008-04-29 09:38:00 -04001467 if (total_errors > max_errors) {
1468 printk("btrfs: %d errors while writing supers\n", total_errors);
1469 BUG();
1470 }
1471 total_errors = 0;
Chris Masonf2984462008-04-10 16:19:33 -04001472
1473 list_for_each(cur, head) {
1474 dev = list_entry(cur, struct btrfs_device, dev_list);
Chris Masondfe25022008-05-13 13:46:40 -04001475 if (!dev->bdev)
1476 continue;
1477 if (!dev->in_fs_metadata)
1478 continue;
1479
Chris Masonf2984462008-04-10 16:19:33 -04001480 BUG_ON(!dev->pending_io);
1481 bh = dev->pending_io;
1482 wait_on_buffer(bh);
1483 if (!buffer_uptodate(dev->pending_io)) {
1484 if (do_barriers && dev->barriers) {
1485 printk("btrfs: disabling barriers on dev %s\n",
1486 dev->name);
1487 set_buffer_uptodate(bh);
1488 get_bh(bh);
1489 lock_buffer(bh);
1490 dev->barriers = 0;
1491 ret = submit_bh(WRITE, bh);
1492 BUG_ON(ret);
1493 wait_on_buffer(bh);
Chris Mason1259ab72008-05-12 13:39:03 -04001494 if (!buffer_uptodate(bh))
1495 total_errors++;
Chris Masonf2984462008-04-10 16:19:33 -04001496 } else {
Chris Masona236aed2008-04-29 09:38:00 -04001497 total_errors++;
Chris Masonf2984462008-04-10 16:19:33 -04001498 }
1499
1500 }
1501 dev->pending_io = NULL;
1502 brelse(bh);
1503 }
Chris Masona236aed2008-04-29 09:38:00 -04001504 if (total_errors > max_errors) {
1505 printk("btrfs: %d errors while writing supers\n", total_errors);
1506 BUG();
1507 }
Chris Masonf2984462008-04-10 16:19:33 -04001508 return 0;
1509}
1510
Chris Masone089f052007-03-16 16:20:31 -04001511int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -04001512 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -05001513{
Chris Masone66f7092007-04-20 13:16:02 -04001514 int ret;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001515
Chris Masonf2984462008-04-10 16:19:33 -04001516 ret = write_all_supers(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001517 return ret;
Chris Masoncfaa7292007-02-21 17:04:57 -05001518}
1519
Chris Mason5eda7b52007-06-22 14:16:25 -04001520int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -04001521{
1522 radix_tree_delete(&fs_info->fs_roots_radix,
1523 (unsigned long)root->root_key.objectid);
Chris Masonb99aa6c2008-01-14 14:41:16 -05001524 if (root->in_sysfs)
1525 btrfs_sysfs_del_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -04001526 if (root->inode)
1527 iput(root->inode);
1528 if (root->node)
Chris Mason5f39d392007-10-15 16:14:19 -04001529 free_extent_buffer(root->node);
Chris Mason2619ba12007-04-10 16:58:11 -04001530 if (root->commit_root)
Chris Mason5f39d392007-10-15 16:14:19 -04001531 free_extent_buffer(root->commit_root);
Josef Bacik58176a92007-08-29 15:47:34 -04001532 if (root->name)
1533 kfree(root->name);
Chris Mason2619ba12007-04-10 16:58:11 -04001534 kfree(root);
1535 return 0;
1536}
1537
Chris Mason35b7e472007-05-02 15:53:43 -04001538static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -04001539{
1540 int ret;
1541 struct btrfs_root *gang[8];
1542 int i;
1543
1544 while(1) {
1545 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
1546 (void **)gang, 0,
1547 ARRAY_SIZE(gang));
1548 if (!ret)
1549 break;
Chris Mason2619ba12007-04-10 16:58:11 -04001550 for (i = 0; i < ret; i++)
Chris Mason5eda7b52007-06-22 14:16:25 -04001551 btrfs_free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001552 }
1553 return 0;
1554}
Chris Masonb4100d62007-04-12 12:14:00 -04001555
Chris Masone20d96d2007-03-22 12:13:20 -04001556int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -05001557{
Chris Mason3768f362007-03-13 16:47:54 -04001558 int ret;
Chris Masone089f052007-03-16 16:20:31 -04001559 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001560 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -04001561
Chris Masonfacda1e2007-06-08 18:11:48 -04001562 fs_info->closing = 1;
Chris Masona2135012008-06-25 16:01:30 -04001563 smp_mb();
1564
Chris Mason08607c12007-06-08 15:33:54 -04001565 btrfs_transaction_flush_work(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001566 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason79154b12007-03-22 15:59:16 -04001567 trans = btrfs_start_transaction(root, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001568 ret = btrfs_commit_transaction(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -04001569 /* run commit again to drop the original snapshot */
1570 trans = btrfs_start_transaction(root, 1);
1571 btrfs_commit_transaction(trans, root);
1572 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -04001573 BUG_ON(ret);
Chris Masond6bfde82008-04-30 13:59:35 -04001574
Chris Mason79154b12007-03-22 15:59:16 -04001575 write_ctree_super(NULL, root);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001576
Chris Mason9ad6b7b2008-04-18 16:11:30 -04001577 btrfs_transaction_flush_work(root);
1578
Chris Masonb0c68f82008-01-31 11:05:37 -05001579 if (fs_info->delalloc_bytes) {
1580 printk("btrfs: at unmount delalloc count %Lu\n",
1581 fs_info->delalloc_bytes);
1582 }
Chris Mason0f7d52f2007-04-09 10:42:37 -04001583 if (fs_info->extent_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -04001584 free_extent_buffer(fs_info->extent_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -04001585
Chris Mason0f7d52f2007-04-09 10:42:37 -04001586 if (fs_info->tree_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -04001587 free_extent_buffer(fs_info->tree_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -04001588
Chris Mason0b86a832008-03-24 15:01:56 -04001589 if (root->fs_info->chunk_root->node);
1590 free_extent_buffer(root->fs_info->chunk_root->node);
1591
1592 if (root->fs_info->dev_root->node);
1593 free_extent_buffer(root->fs_info->dev_root->node);
1594
Chris Mason9078a3e2007-04-26 16:46:15 -04001595 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001596 del_fs_roots(fs_info);
Chris Masond10c5f32007-12-17 20:14:04 -05001597
1598 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
1599
Chris Masond1310b22008-01-24 16:13:08 -05001600 extent_io_tree_empty_lru(&fs_info->free_space_cache);
1601 extent_io_tree_empty_lru(&fs_info->block_group_cache);
1602 extent_io_tree_empty_lru(&fs_info->pinned_extents);
1603 extent_io_tree_empty_lru(&fs_info->pending_del);
1604 extent_io_tree_empty_lru(&fs_info->extent_ins);
1605 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
Chris Masond10c5f32007-12-17 20:14:04 -05001606
Chris Masondb945352007-10-15 16:15:53 -04001607 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04001608
Chris Mason8b712842008-06-11 16:50:36 -04001609 btrfs_stop_workers(&fs_info->workers);
1610 btrfs_stop_workers(&fs_info->endio_workers);
Chris Mason1cc127b2008-06-12 14:46:17 -04001611 btrfs_stop_workers(&fs_info->submit_workers);
Chris Masond6bfde82008-04-30 13:59:35 -04001612
Chris Masondb945352007-10-15 16:15:53 -04001613 iput(fs_info->btree_inode);
Chris Mason19c00dd2007-10-15 16:19:22 -04001614#if 0
1615 while(!list_empty(&fs_info->hashers)) {
1616 struct btrfs_hasher *hasher;
1617 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
1618 hashers);
1619 list_del(&hasher->hashers);
1620 crypto_free_hash(&fs_info->hash_tfm);
1621 kfree(hasher);
1622 }
1623#endif
Chris Masondfe25022008-05-13 13:46:40 -04001624 btrfs_close_devices(fs_info->fs_devices);
Chris Mason0b86a832008-03-24 15:01:56 -04001625 btrfs_mapping_tree_free(&fs_info->mapping_tree);
Chris Masonb248a412008-04-14 09:48:18 -04001626
Jan Engelhardt51ebc0d2008-06-09 22:19:40 -04001627#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason04160082008-03-26 10:28:07 -04001628 bdi_destroy(&fs_info->bdi);
Chris Masonb248a412008-04-14 09:48:18 -04001629#endif
Chris Mason0b86a832008-03-24 15:01:56 -04001630
Chris Mason0f7d52f2007-04-09 10:42:37 -04001631 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001632 kfree(fs_info->tree_root);
Chris Mason0b86a832008-03-24 15:01:56 -04001633 kfree(fs_info->chunk_root);
1634 kfree(fs_info->dev_root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001635 return 0;
1636}
1637
Chris Mason1259ab72008-05-12 13:39:03 -04001638int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
Chris Masonccd467d2007-06-28 15:57:36 -04001639{
Chris Mason1259ab72008-05-12 13:39:03 -04001640 int ret;
Chris Mason810191f2007-10-15 16:18:55 -04001641 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Mason1259ab72008-05-12 13:39:03 -04001642
1643 ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
1644 if (!ret)
1645 return ret;
1646
1647 ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
1648 parent_transid);
1649 return !ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001650}
Chris Mason6702ed42007-08-07 16:15:09 -04001651
Chris Mason5f39d392007-10-15 16:14:19 -04001652int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
1653{
Chris Mason810191f2007-10-15 16:18:55 -04001654 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001655 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -04001656 buf);
1657}
1658
1659void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
1660{
Chris Mason810191f2007-10-15 16:18:55 -04001661 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04001662 u64 transid = btrfs_header_generation(buf);
1663 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason6702ed42007-08-07 16:15:09 -04001664
Chris Mason925baed2008-06-25 16:01:30 -04001665 WARN_ON(!btrfs_tree_locked(buf));
Chris Masonccd467d2007-06-28 15:57:36 -04001666 if (transid != root->fs_info->generation) {
1667 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
Chris Masondb945352007-10-15 16:15:53 -04001668 (unsigned long long)buf->start,
Chris Masonccd467d2007-06-28 15:57:36 -04001669 transid, root->fs_info->generation);
1670 WARN_ON(1);
1671 }
Chris Masond1310b22008-01-24 16:13:08 -05001672 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001673}
1674
Chris Masone2008b62008-01-08 15:46:30 -05001675void btrfs_throttle(struct btrfs_root *root)
1676{
Chris Mason55c69072008-01-09 15:55:33 -05001677 struct backing_dev_info *bdi;
1678
Chris Masona061fc82008-05-07 11:43:44 -04001679 bdi = &root->fs_info->bdi;
Chris Masona2135012008-06-25 16:01:30 -04001680 if (atomic_read(&root->fs_info->throttles) &&
1681 bdi_write_congested(bdi)) {
Chris Mason04005cc2008-01-17 12:01:41 -05001682#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
Chris Mason55c69072008-01-09 15:55:33 -05001683 congestion_wait(WRITE, HZ/20);
Chris Mason04005cc2008-01-17 12:01:41 -05001684#else
1685 blk_congestion_wait(WRITE, HZ/20);
1686#endif
1687 }
Chris Masone2008b62008-01-08 15:46:30 -05001688}
1689
Chris Masond3c2fdc2007-09-17 10:58:06 -04001690void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
Chris Mason35b7e472007-05-02 15:53:43 -04001691{
Chris Mason188de642008-05-09 11:52:25 -04001692 /*
1693 * looks as though older kernels can get into trouble with
1694 * this code, they end up stuck in balance_dirty_pages forever
1695 */
Chris Masond6bfde82008-04-30 13:59:35 -04001696 struct extent_io_tree *tree;
1697 u64 num_dirty;
1698 u64 start = 0;
1699 unsigned long thresh = 16 * 1024 * 1024;
1700 tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
1701
1702 if (current_is_pdflush())
1703 return;
1704
1705 num_dirty = count_range_bits(tree, &start, (u64)-1,
1706 thresh, EXTENT_DIRTY);
1707 if (num_dirty > thresh) {
1708 balance_dirty_pages_ratelimited_nr(
Chris Masond7fc6402008-02-18 12:12:38 -05001709 root->fs_info->btree_inode->i_mapping, 1);
Chris Masond6bfde82008-04-30 13:59:35 -04001710 }
Chris Mason188de642008-05-09 11:52:25 -04001711 return;
Chris Mason35b7e472007-05-02 15:53:43 -04001712}
Chris Mason6b800532007-10-15 16:17:34 -04001713
1714void btrfs_set_buffer_defrag(struct extent_buffer *buf)
1715{
Chris Mason810191f2007-10-15 16:18:55 -04001716 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001717 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001718 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
Chris Mason6b800532007-10-15 16:17:34 -04001719 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
1720}
1721
1722void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
1723{
Chris Mason810191f2007-10-15 16:18:55 -04001724 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001725 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001726 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
Chris Mason6b800532007-10-15 16:17:34 -04001727 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
1728 GFP_NOFS);
1729}
1730
1731int btrfs_buffer_defrag(struct extent_buffer *buf)
1732{
Chris Mason810191f2007-10-15 16:18:55 -04001733 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001734 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001735 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001736 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
1737}
1738
1739int btrfs_buffer_defrag_done(struct extent_buffer *buf)
1740{
Chris Mason810191f2007-10-15 16:18:55 -04001741 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001742 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001743 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001744 buf->start, buf->start + buf->len - 1,
1745 EXTENT_DEFRAG_DONE, 0);
1746}
1747
1748int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
1749{
Chris Mason810191f2007-10-15 16:18:55 -04001750 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001751 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001752 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001753 buf->start, buf->start + buf->len - 1,
1754 EXTENT_DEFRAG_DONE, GFP_NOFS);
1755}
1756
1757int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
1758{
Chris Mason810191f2007-10-15 16:18:55 -04001759 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001760 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001761 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001762 buf->start, buf->start + buf->len - 1,
1763 EXTENT_DEFRAG, GFP_NOFS);
1764}
1765
Chris Masonca7a79a2008-05-12 12:59:19 -04001766int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
Chris Mason6b800532007-10-15 16:17:34 -04001767{
Chris Mason810191f2007-10-15 16:18:55 -04001768 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Masonce9adaa2008-04-09 16:28:12 -04001769 int ret;
Chris Masonca7a79a2008-05-12 12:59:19 -04001770 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
Chris Masonce9adaa2008-04-09 16:28:12 -04001771 if (ret == 0) {
1772 buf->flags |= EXTENT_UPTODATE;
1773 }
1774 return ret;
Chris Mason6b800532007-10-15 16:17:34 -04001775}
Chris Mason0da54682007-11-07 21:08:01 -05001776
Chris Masond1310b22008-01-24 16:13:08 -05001777static struct extent_io_ops btree_extent_io_ops = {
Chris Mason0da54682007-11-07 21:08:01 -05001778 .writepage_io_hook = btree_writepage_io_hook,
Chris Masonce9adaa2008-04-09 16:28:12 -04001779 .readpage_end_io_hook = btree_readpage_end_io_hook,
Chris Mason0b86a832008-03-24 15:01:56 -04001780 .submit_bio_hook = btree_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04001781 /* note we're sharing with inode.c for the merge bio hook */
1782 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason0da54682007-11-07 21:08:01 -05001783};