blob: 76ee7a4ae00c7f420274c8de52e073421e0b97e7 [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 Masoneb60cea2007-02-02 09:18:22 -050034
Chris Mason5f39d392007-10-15 16:14:19 -040035#if 0
36static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
Chris Mason7eccb902007-04-11 15:53:25 -040037{
Chris Mason5f39d392007-10-15 16:14:19 -040038 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
39 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
40 (unsigned long long)extent_buffer_blocknr(buf),
41 (unsigned long long)btrfs_header_blocknr(buf));
Chris Mason39279cc2007-06-12 06:35:45 -040042 return 1;
Chris Masond98237b2007-03-28 13:57:48 -040043 }
Chris Mason9a8dd152007-02-23 08:38:36 -050044 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050045}
Chris Mason5f39d392007-10-15 16:14:19 -040046#endif
Chris Masoneb60cea2007-02-02 09:18:22 -050047
Chris Masond1310b22008-01-24 16:13:08 -050048static struct extent_io_ops btree_extent_io_ops;
Chris Masonce9adaa2008-04-09 16:28:12 -040049static struct workqueue_struct *end_io_workqueue;
Chris Mason44b8bd72008-04-16 11:14:51 -040050static struct workqueue_struct *async_submit_workqueue;
Chris Masonce9adaa2008-04-09 16:28:12 -040051
52struct end_io_wq {
53 struct bio *bio;
54 bio_end_io_t *end_io;
55 void *private;
56 struct btrfs_fs_info *info;
57 int error;
Chris Mason22c59942008-04-09 16:28:12 -040058 int metadata;
Chris Masonce9adaa2008-04-09 16:28:12 -040059 struct list_head list;
60};
Chris Mason0da54682007-11-07 21:08:01 -050061
Chris Mason44b8bd72008-04-16 11:14:51 -040062struct async_submit_bio {
63 struct inode *inode;
64 struct bio *bio;
65 struct list_head list;
66 extent_submit_bio_hook_t *submit_bio_hook;
67 int rw;
68 int mirror_num;
69};
70
Chris Mason5f39d392007-10-15 16:14:19 -040071struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
Chris Masond1310b22008-01-24 16:13:08 -050072 size_t page_offset, u64 start, u64 len,
Chris Mason5f39d392007-10-15 16:14:19 -040073 int create)
74{
75 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
76 struct extent_map *em;
77 int ret;
Chris Masond98237b2007-03-28 13:57:48 -040078
Chris Mason5f39d392007-10-15 16:14:19 -040079again:
Chris Masond1310b22008-01-24 16:13:08 -050080 spin_lock(&em_tree->lock);
81 em = lookup_extent_mapping(em_tree, start, len);
82 spin_unlock(&em_tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -040083 if (em) {
84 goto out;
85 }
86 em = alloc_extent_map(GFP_NOFS);
87 if (!em) {
88 em = ERR_PTR(-ENOMEM);
89 goto out;
90 }
91 em->start = 0;
Chris Masond1310b22008-01-24 16:13:08 -050092 em->len = i_size_read(inode);
Chris Mason5f39d392007-10-15 16:14:19 -040093 em->block_start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -040094 em->bdev = inode->i_sb->s_bdev;
Chris Masond1310b22008-01-24 16:13:08 -050095
96 spin_lock(&em_tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -040097 ret = add_extent_mapping(em_tree, em);
Chris Masond1310b22008-01-24 16:13:08 -050098 spin_unlock(&em_tree->lock);
99
Chris Mason5f39d392007-10-15 16:14:19 -0400100 if (ret == -EEXIST) {
101 free_extent_map(em);
102 em = NULL;
103 goto again;
104 } else if (ret) {
105 em = ERR_PTR(ret);
106 }
107out:
108 return em;
109}
110
Chris Mason19c00dd2007-10-15 16:19:22 -0400111u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
112{
Miguela5eb62e2008-04-11 15:45:51 -0400113 return btrfs_crc32c(seed, data, len);
Chris Mason19c00dd2007-10-15 16:19:22 -0400114}
115
116void btrfs_csum_final(u32 crc, char *result)
117{
118 *(__le32 *)result = ~cpu_to_le32(crc);
119}
120
121static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
122 int verify)
123{
124 char result[BTRFS_CRC32_SIZE];
125 unsigned long len;
126 unsigned long cur_len;
127 unsigned long offset = BTRFS_CSUM_SIZE;
128 char *map_token = NULL;
129 char *kaddr;
130 unsigned long map_start;
131 unsigned long map_len;
132 int err;
133 u32 crc = ~(u32)0;
134
135 len = buf->len - offset;
136 while(len > 0) {
137 err = map_private_extent_buffer(buf, offset, 32,
138 &map_token, &kaddr,
139 &map_start, &map_len, KM_USER0);
140 if (err) {
141 printk("failed to map extent buffer! %lu\n",
142 offset);
143 return 1;
144 }
145 cur_len = min(len, map_len - (offset - map_start));
146 crc = btrfs_csum_data(root, kaddr + offset - map_start,
147 crc, cur_len);
148 len -= cur_len;
149 offset += cur_len;
150 unmap_extent_buffer(buf, map_token, KM_USER0);
151 }
152 btrfs_csum_final(crc, result);
153
154 if (verify) {
Chris Masone4204de2008-01-08 15:46:27 -0500155 int from_this_trans = 0;
156
157 if (root->fs_info->running_transaction &&
158 btrfs_header_generation(buf) ==
159 root->fs_info->running_transaction->transid)
160 from_this_trans = 1;
161
162 /* FIXME, this is not good */
Chris Mason63b10fc2008-04-01 11:21:32 -0400163 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
Chris Masone4204de2008-01-08 15:46:27 -0500164 u32 val;
165 u32 found = 0;
166 memcpy(&found, result, BTRFS_CRC32_SIZE);
167
168 read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
169 printk("btrfs: %s checksum verify failed on %llu "
Chris Mason63b10fc2008-04-01 11:21:32 -0400170 "wanted %X found %X from_this_trans %d "
171 "level %d\n",
Chris Mason19c00dd2007-10-15 16:19:22 -0400172 root->fs_info->sb->s_id,
Chris Mason63b10fc2008-04-01 11:21:32 -0400173 buf->start, val, found, from_this_trans,
174 btrfs_header_level(buf));
Chris Mason19c00dd2007-10-15 16:19:22 -0400175 return 1;
176 }
177 } else {
178 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
179 }
180 return 0;
181}
182
Chris Masonf1885912008-04-09 16:28:12 -0400183static int btree_read_extent_buffer_pages(struct btrfs_root *root,
184 struct extent_buffer *eb,
185 u64 start)
186{
187 struct extent_io_tree *io_tree;
188 int ret;
189 int num_copies = 0;
190 int mirror_num = 0;
191
192 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
193 while (1) {
194 ret = read_extent_buffer_pages(io_tree, eb, start, 1,
195 btree_get_extent, mirror_num);
196 if (!ret) {
197 if (mirror_num)
198printk("good read %Lu mirror %d total %d\n", eb->start, mirror_num, num_copies);
199 return ret;
200 }
201 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
202 eb->start, eb->len);
203printk("failed to read %Lu mirror %d total %d\n", eb->start, mirror_num, num_copies);
204 if (num_copies == 1) {
205printk("reading %Lu failed only one copy\n", eb->start);
206 return ret;
207 }
208 mirror_num++;
209 if (mirror_num > num_copies) {
210printk("bailing at mirror %d of %d\n", mirror_num, num_copies);
211 return ret;
212 }
213 }
214printk("read extent buffer page last\n");
215 return -EIO;
216}
Chris Mason19c00dd2007-10-15 16:19:22 -0400217
218int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
219{
Chris Masond1310b22008-01-24 16:13:08 -0500220 struct extent_io_tree *tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400221 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason19c00dd2007-10-15 16:19:22 -0400222 u64 found_start;
223 int found_level;
224 unsigned long len;
225 struct extent_buffer *eb;
Chris Masonf1885912008-04-09 16:28:12 -0400226 int ret;
227
Chris Masond1310b22008-01-24 16:13:08 -0500228 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason19c00dd2007-10-15 16:19:22 -0400229
230 if (page->private == EXTENT_PAGE_PRIVATE)
231 goto out;
232 if (!page->private)
233 goto out;
234 len = page->private >> 2;
235 if (len == 0) {
236 WARN_ON(1);
237 }
238 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
Chris Masonf1885912008-04-09 16:28:12 -0400239 ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE);
240 BUG_ON(ret);
Chris Masone18e4802008-01-18 10:54:22 -0500241 btrfs_clear_buffer_defrag(eb);
Chris Mason19c00dd2007-10-15 16:19:22 -0400242 found_start = btrfs_header_bytenr(eb);
243 if (found_start != start) {
244 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
245 start, found_start, len);
Chris Mason55c69072008-01-09 15:55:33 -0500246 WARN_ON(1);
247 goto err;
248 }
249 if (eb->first_page != page) {
250 printk("bad first page %lu %lu\n", eb->first_page->index,
251 page->index);
252 WARN_ON(1);
253 goto err;
254 }
255 if (!PageUptodate(page)) {
256 printk("csum not up to date page %lu\n", page->index);
257 WARN_ON(1);
258 goto err;
Chris Mason19c00dd2007-10-15 16:19:22 -0400259 }
260 found_level = btrfs_header_level(eb);
Chris Mason63b10fc2008-04-01 11:21:32 -0400261 spin_lock(&root->fs_info->hash_lock);
262 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
263 spin_unlock(&root->fs_info->hash_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -0400264 csum_tree_block(root, eb, 0);
Chris Mason55c69072008-01-09 15:55:33 -0500265err:
Chris Mason19c00dd2007-10-15 16:19:22 -0400266 free_extent_buffer(eb);
267out:
268 return 0;
269}
270
Chris Mason0da54682007-11-07 21:08:01 -0500271static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
272{
273 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
274
275 csum_dirty_buffer(root, page);
276 return 0;
277}
278
Chris Masonce9adaa2008-04-09 16:28:12 -0400279int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
280 struct extent_state *state)
281{
282 struct extent_io_tree *tree;
283 u64 found_start;
284 int found_level;
285 unsigned long len;
286 struct extent_buffer *eb;
287 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masonf1885912008-04-09 16:28:12 -0400288 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -0400289
290 tree = &BTRFS_I(page->mapping->host)->io_tree;
291 if (page->private == EXTENT_PAGE_PRIVATE)
292 goto out;
293 if (!page->private)
294 goto out;
295 len = page->private >> 2;
296 if (len == 0) {
297 WARN_ON(1);
298 }
299 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
Chris Masonf1885912008-04-09 16:28:12 -0400300
Chris Masonce9adaa2008-04-09 16:28:12 -0400301 btrfs_clear_buffer_defrag(eb);
302 found_start = btrfs_header_bytenr(eb);
303 if (found_start != start) {
Chris Masonf1885912008-04-09 16:28:12 -0400304printk("bad start on %Lu found %Lu\n", eb->start, found_start);
305 ret = -EIO;
Chris Masonce9adaa2008-04-09 16:28:12 -0400306 goto err;
307 }
308 if (eb->first_page != page) {
309 printk("bad first page %lu %lu\n", eb->first_page->index,
310 page->index);
311 WARN_ON(1);
Chris Masonf1885912008-04-09 16:28:12 -0400312 ret = -EIO;
Chris Masonce9adaa2008-04-09 16:28:12 -0400313 goto err;
314 }
315 found_level = btrfs_header_level(eb);
316
317 ret = csum_tree_block(root, eb, 1);
Chris Masonf1885912008-04-09 16:28:12 -0400318 if (ret)
319 ret = -EIO;
Chris Masonce9adaa2008-04-09 16:28:12 -0400320
321 end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
322 end = eb->start + end - 1;
323 release_extent_buffer_tail_pages(eb);
324err:
325 free_extent_buffer(eb);
326out:
Chris Masonf1885912008-04-09 16:28:12 -0400327 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -0400328}
329
330#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
331static void end_workqueue_bio(struct bio *bio, int err)
332#else
333static int end_workqueue_bio(struct bio *bio,
334 unsigned int bytes_done, int err)
335#endif
336{
337 struct end_io_wq *end_io_wq = bio->bi_private;
338 struct btrfs_fs_info *fs_info;
339 unsigned long flags;
340
341#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
342 if (bio->bi_size)
343 return 1;
344#endif
345
346 fs_info = end_io_wq->info;
347 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
348 end_io_wq->error = err;
349 list_add_tail(&end_io_wq->list, &fs_info->end_io_work_list);
350 spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
351 queue_work(end_io_workqueue, &fs_info->end_io_work);
352
353#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
354 return 0;
355#endif
356}
357
Chris Mason22c59942008-04-09 16:28:12 -0400358int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
359 int metadata)
Chris Mason0b86a832008-03-24 15:01:56 -0400360{
Chris Masonce9adaa2008-04-09 16:28:12 -0400361 struct end_io_wq *end_io_wq;
Chris Masonce9adaa2008-04-09 16:28:12 -0400362 end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
363 if (!end_io_wq)
364 return -ENOMEM;
365
366 end_io_wq->private = bio->bi_private;
367 end_io_wq->end_io = bio->bi_end_io;
Chris Mason22c59942008-04-09 16:28:12 -0400368 end_io_wq->info = info;
Chris Masonce9adaa2008-04-09 16:28:12 -0400369 end_io_wq->error = 0;
370 end_io_wq->bio = bio;
Chris Mason22c59942008-04-09 16:28:12 -0400371 end_io_wq->metadata = metadata;
Chris Masonce9adaa2008-04-09 16:28:12 -0400372
373 bio->bi_private = end_io_wq;
374 bio->bi_end_io = end_workqueue_bio;
Chris Mason22c59942008-04-09 16:28:12 -0400375 return 0;
376}
377
Chris Mason44b8bd72008-04-16 11:14:51 -0400378int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
379 int rw, struct bio *bio, int mirror_num,
380 extent_submit_bio_hook_t *submit_bio_hook)
381{
382 struct async_submit_bio *async;
383
Chris Mason7b859fe2008-04-16 13:06:16 -0400384 /*
385 * inline writerback should stay inline, only hop to the async
386 * queue if we're pdflush
387 */
388 if (!current_is_pdflush())
389 return submit_bio_hook(inode, rw, bio, mirror_num);
390
Chris Mason44b8bd72008-04-16 11:14:51 -0400391 async = kmalloc(sizeof(*async), GFP_NOFS);
392 if (!async)
393 return -ENOMEM;
394
395 async->inode = inode;
396 async->rw = rw;
397 async->bio = bio;
398 async->mirror_num = mirror_num;
399 async->submit_bio_hook = submit_bio_hook;
400
401 spin_lock(&fs_info->async_submit_work_lock);
402 list_add_tail(&async->list, &fs_info->async_submit_work_list);
403 spin_unlock(&fs_info->async_submit_work_lock);
404
405 queue_work(async_submit_workqueue, &fs_info->async_submit_work);
406 return 0;
407}
408
409static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400410 int mirror_num)
Chris Mason22c59942008-04-09 16:28:12 -0400411{
412 struct btrfs_root *root = BTRFS_I(inode)->root;
413 u64 offset;
414 int ret;
415
416 offset = bio->bi_sector << 9;
417
418 if (rw & (1 << BIO_RW)) {
Chris Masonf1885912008-04-09 16:28:12 -0400419 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num);
Chris Mason22c59942008-04-09 16:28:12 -0400420 }
421
422 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1);
423 BUG_ON(ret);
Chris Masonce9adaa2008-04-09 16:28:12 -0400424
Chris Mason0b86a832008-03-24 15:01:56 -0400425 if (offset == BTRFS_SUPER_INFO_OFFSET) {
Chris Masonf2984462008-04-10 16:19:33 -0400426 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason0b86a832008-03-24 15:01:56 -0400427 submit_bio(rw, bio);
428 return 0;
429 }
Chris Masonf1885912008-04-09 16:28:12 -0400430 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num);
Chris Mason0b86a832008-03-24 15:01:56 -0400431}
432
Chris Mason44b8bd72008-04-16 11:14:51 -0400433static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
434 int mirror_num)
435{
436 if (!(rw & (1 << BIO_RW))) {
437 return __btree_submit_bio_hook(inode, rw, bio, mirror_num);
438 }
439 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
440 inode, rw, bio, mirror_num,
441 __btree_submit_bio_hook);
442}
443
Chris Mason5f39d392007-10-15 16:14:19 -0400444static int btree_writepage(struct page *page, struct writeback_control *wbc)
445{
Chris Masond1310b22008-01-24 16:13:08 -0500446 struct extent_io_tree *tree;
447 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400448 return extent_write_full_page(tree, page, btree_get_extent, wbc);
449}
Chris Mason0da54682007-11-07 21:08:01 -0500450
451static int btree_writepages(struct address_space *mapping,
452 struct writeback_control *wbc)
453{
Chris Masond1310b22008-01-24 16:13:08 -0500454 struct extent_io_tree *tree;
455 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500456 if (wbc->sync_mode == WB_SYNC_NONE) {
Chris Mason793955b2007-11-26 16:34:41 -0800457 u64 num_dirty;
458 u64 start = 0;
459 unsigned long thresh = 96 * 1024 * 1024;
Chris Mason448d6402007-11-27 07:52:01 -0800460
461 if (wbc->for_kupdate)
462 return 0;
463
Chris Masonca664622007-11-27 11:16:35 -0500464 if (current_is_pdflush()) {
465 thresh = 96 * 1024 * 1024;
466 } else {
467 thresh = 8 * 1024 * 1024;
468 }
Chris Mason1832a6d2007-12-21 16:27:21 -0500469 num_dirty = count_range_bits(tree, &start, (u64)-1,
470 thresh, EXTENT_DIRTY);
Chris Mason793955b2007-11-26 16:34:41 -0800471 if (num_dirty < thresh) {
472 return 0;
473 }
474 }
Chris Mason0da54682007-11-07 21:08:01 -0500475 return extent_writepages(tree, mapping, btree_get_extent, wbc);
476}
477
Chris Mason5f39d392007-10-15 16:14:19 -0400478int btree_readpage(struct file *file, struct page *page)
479{
Chris Masond1310b22008-01-24 16:13:08 -0500480 struct extent_io_tree *tree;
481 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400482 return extent_read_full_page(tree, page, btree_get_extent);
483}
484
Chris Mason70dec802008-01-29 09:59:12 -0500485static int btree_releasepage(struct page *page, gfp_t gfp_flags)
Chris Mason5f39d392007-10-15 16:14:19 -0400486{
Chris Masond1310b22008-01-24 16:13:08 -0500487 struct extent_io_tree *tree;
488 struct extent_map_tree *map;
Chris Mason5f39d392007-10-15 16:14:19 -0400489 int ret;
490
Chris Mason3dd39912008-04-11 10:51:07 -0400491 if (page_count(page) > 3) {
492 /* once for page->private, once for the caller, once
493 * once for the page cache
494 */
495 return 0;
496 }
Chris Masond1310b22008-01-24 16:13:08 -0500497 tree = &BTRFS_I(page->mapping->host)->io_tree;
498 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -0500499 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400500 if (ret == 1) {
Chris Mason728131d2008-04-09 16:28:12 -0400501 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -0400502 ClearPagePrivate(page);
503 set_page_private(page, 0);
504 page_cache_release(page);
505 }
Chris Masond98237b2007-03-28 13:57:48 -0400506 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400507}
Chris Mason123abc82007-03-14 14:14:43 -0400508
Chris Mason5f39d392007-10-15 16:14:19 -0400509static void btree_invalidatepage(struct page *page, unsigned long offset)
Chris Masond98237b2007-03-28 13:57:48 -0400510{
Chris Masond1310b22008-01-24 16:13:08 -0500511 struct extent_io_tree *tree;
512 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400513 extent_invalidatepage(tree, page, offset);
514 btree_releasepage(page, GFP_NOFS);
Chris Masond98237b2007-03-28 13:57:48 -0400515}
516
Chris Mason5f39d392007-10-15 16:14:19 -0400517#if 0
Chris Masond98237b2007-03-28 13:57:48 -0400518static int btree_writepage(struct page *page, struct writeback_control *wbc)
519{
Chris Mason87cbda52007-03-28 19:44:27 -0400520 struct buffer_head *bh;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400521 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Mason87cbda52007-03-28 19:44:27 -0400522 struct buffer_head *head;
Chris Mason87cbda52007-03-28 19:44:27 -0400523 if (!page_has_buffers(page)) {
524 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
525 (1 << BH_Dirty)|(1 << BH_Uptodate));
526 }
527 head = page_buffers(page);
528 bh = head;
529 do {
530 if (buffer_dirty(bh))
531 csum_tree_block(root, bh, 0);
532 bh = bh->b_this_page;
533 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400534 return block_write_full_page(page, btree_get_block, wbc);
535}
Chris Mason5f39d392007-10-15 16:14:19 -0400536#endif
Chris Masond98237b2007-03-28 13:57:48 -0400537
538static struct address_space_operations btree_aops = {
539 .readpage = btree_readpage,
540 .writepage = btree_writepage,
Chris Mason0da54682007-11-07 21:08:01 -0500541 .writepages = btree_writepages,
Chris Mason5f39d392007-10-15 16:14:19 -0400542 .releasepage = btree_releasepage,
543 .invalidatepage = btree_invalidatepage,
Chris Masond98237b2007-03-28 13:57:48 -0400544 .sync_page = block_sync_page,
545};
546
Chris Masondb945352007-10-15 16:15:53 -0400547int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
Chris Mason090d1872007-05-01 08:53:32 -0400548{
Chris Mason5f39d392007-10-15 16:14:19 -0400549 struct extent_buffer *buf = NULL;
550 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonde428b62007-05-18 13:28:27 -0400551 int ret = 0;
Chris Mason090d1872007-05-01 08:53:32 -0400552
Chris Masondb945352007-10-15 16:15:53 -0400553 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400554 if (!buf)
Chris Mason090d1872007-05-01 08:53:32 -0400555 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500556 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
Chris Masonf1885912008-04-09 16:28:12 -0400557 buf, 0, 0, btree_get_extent, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400558 free_extent_buffer(buf);
Chris Masonde428b62007-05-18 13:28:27 -0400559 return ret;
Chris Mason090d1872007-05-01 08:53:32 -0400560}
561
Chris Mason0b86a832008-03-24 15:01:56 -0400562static int close_all_devices(struct btrfs_fs_info *fs_info)
563{
564 struct list_head *list;
565 struct list_head *next;
566 struct btrfs_device *device;
567
Chris Mason8a4b83c2008-03-24 15:02:07 -0400568 list = &fs_info->fs_devices->devices;
569 list_for_each(next, list) {
Chris Mason0b86a832008-03-24 15:01:56 -0400570 device = list_entry(next, struct btrfs_device, dev_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400571 if (device->bdev && device->bdev != fs_info->sb->s_bdev)
572 close_bdev_excl(device->bdev);
573 device->bdev = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400574 }
575 return 0;
576}
577
Chris Mason0999df52008-04-01 13:48:14 -0400578int btrfs_verify_block_csum(struct btrfs_root *root,
579 struct extent_buffer *buf)
580{
Chris Masonce9adaa2008-04-09 16:28:12 -0400581 return btrfs_buffer_uptodate(buf);
Chris Mason0999df52008-04-01 13:48:14 -0400582}
583
584struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
585 u64 bytenr, u32 blocksize)
586{
587 struct inode *btree_inode = root->fs_info->btree_inode;
588 struct extent_buffer *eb;
589 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
590 bytenr, blocksize, GFP_NOFS);
591 return eb;
592}
593
594struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
595 u64 bytenr, u32 blocksize)
596{
597 struct inode *btree_inode = root->fs_info->btree_inode;
598 struct extent_buffer *eb;
599
600 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
601 bytenr, blocksize, NULL, GFP_NOFS);
602 return eb;
603}
604
605
Chris Masondb945352007-10-15 16:15:53 -0400606struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
607 u32 blocksize)
Chris Masone20d96d2007-03-22 12:13:20 -0400608{
Chris Mason5f39d392007-10-15 16:14:19 -0400609 struct extent_buffer *buf = NULL;
610 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -0500611 struct extent_io_tree *io_tree;
Chris Mason19c00dd2007-10-15 16:19:22 -0400612 int ret;
613
Chris Masond1310b22008-01-24 16:13:08 -0500614 io_tree = &BTRFS_I(btree_inode)->io_tree;
Chris Masone20d96d2007-03-22 12:13:20 -0400615
Chris Masondb945352007-10-15 16:15:53 -0400616 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400617 if (!buf)
618 return NULL;
Chris Masone4204de2008-01-08 15:46:27 -0500619
Chris Masonf1885912008-04-09 16:28:12 -0400620 ret = btree_read_extent_buffer_pages(root, buf, 0);
Chris Masonce9adaa2008-04-09 16:28:12 -0400621
622 if (ret == 0) {
623 buf->flags |= EXTENT_UPTODATE;
624 }
Chris Mason5f39d392007-10-15 16:14:19 -0400625 return buf;
Chris Masonce9adaa2008-04-09 16:28:12 -0400626
Chris Masoneb60cea2007-02-02 09:18:22 -0500627}
628
Chris Masone089f052007-03-16 16:20:31 -0400629int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400630 struct extent_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500631{
Chris Mason5f39d392007-10-15 16:14:19 -0400632 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason55c69072008-01-09 15:55:33 -0500633 if (btrfs_header_generation(buf) ==
634 root->fs_info->running_transaction->transid)
Chris Masond1310b22008-01-24 16:13:08 -0500635 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
Chris Mason55c69072008-01-09 15:55:33 -0500636 buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400637 return 0;
638}
639
640int wait_on_tree_block_writeback(struct btrfs_root *root,
641 struct extent_buffer *buf)
642{
643 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -0500644 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -0400645 buf);
646 return 0;
647}
648
Chris Masondb945352007-10-15 16:15:53 -0400649static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500650 u32 stripesize, struct btrfs_root *root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400651 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400652 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500653{
Chris Masoncfaa7292007-02-21 17:04:57 -0500654 root->node = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400655 root->inode = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500656 root->commit_root = NULL;
Chris Masondb945352007-10-15 16:15:53 -0400657 root->sectorsize = sectorsize;
658 root->nodesize = nodesize;
659 root->leafsize = leafsize;
Chris Mason87ee04e2007-11-30 11:30:34 -0500660 root->stripesize = stripesize;
Chris Mason123abc82007-03-14 14:14:43 -0400661 root->ref_cows = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400662 root->track_dirty = 0;
663
Chris Mason9f5fae22007-03-20 14:38:32 -0400664 root->fs_info = fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400665 root->objectid = objectid;
666 root->last_trans = 0;
Chris Mason1b05da22007-04-10 12:13:09 -0400667 root->highest_inode = 0;
668 root->last_inode_alloc = 0;
Josef Bacik58176a92007-08-29 15:47:34 -0400669 root->name = NULL;
Chris Mason4313b392008-01-03 09:08:48 -0500670 root->in_sysfs = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400671
672 INIT_LIST_HEAD(&root->dirty_list);
Chris Mason3768f362007-03-13 16:47:54 -0400673 memset(&root->root_key, 0, sizeof(root->root_key));
674 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -0400675 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Josef Bacik58176a92007-08-29 15:47:34 -0400676 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
677 init_completion(&root->kobj_unregister);
Chris Mason6702ed42007-08-07 16:15:09 -0400678 root->defrag_running = 0;
679 root->defrag_level = 0;
Chris Mason4d775672007-04-20 20:23:12 -0400680 root->root_key.objectid = objectid;
Chris Mason3768f362007-03-13 16:47:54 -0400681 return 0;
682}
683
Chris Masondb945352007-10-15 16:15:53 -0400684static int find_and_setup_root(struct btrfs_root *tree_root,
Chris Mason9f5fae22007-03-20 14:38:32 -0400685 struct btrfs_fs_info *fs_info,
686 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400687 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400688{
689 int ret;
Chris Masondb945352007-10-15 16:15:53 -0400690 u32 blocksize;
Chris Mason3768f362007-03-13 16:47:54 -0400691
Chris Masondb945352007-10-15 16:15:53 -0400692 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500693 tree_root->sectorsize, tree_root->stripesize,
694 root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400695 ret = btrfs_find_last_root(tree_root, objectid,
696 &root->root_item, &root->root_key);
697 BUG_ON(ret);
698
Chris Masondb945352007-10-15 16:15:53 -0400699 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
700 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
701 blocksize);
Chris Mason3768f362007-03-13 16:47:54 -0400702 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500703 return 0;
704}
705
Chris Mason5eda7b52007-06-22 14:16:25 -0400706struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
707 struct btrfs_key *location)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400708{
709 struct btrfs_root *root;
710 struct btrfs_root *tree_root = fs_info->tree_root;
711 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400712 struct extent_buffer *l;
Chris Mason1b05da22007-04-10 12:13:09 -0400713 u64 highest_inode;
Chris Masondb945352007-10-15 16:15:53 -0400714 u32 blocksize;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400715 int ret = 0;
716
Chris Mason5eda7b52007-06-22 14:16:25 -0400717 root = kzalloc(sizeof(*root), GFP_NOFS);
Chris Mason0cf6c622007-06-09 09:22:25 -0400718 if (!root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400719 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400720 if (location->offset == (u64)-1) {
Chris Masondb945352007-10-15 16:15:53 -0400721 ret = find_and_setup_root(tree_root, fs_info,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400722 location->objectid, root);
723 if (ret) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400724 kfree(root);
725 return ERR_PTR(ret);
726 }
727 goto insert;
728 }
729
Chris Masondb945352007-10-15 16:15:53 -0400730 __setup_root(tree_root->nodesize, tree_root->leafsize,
Chris Mason87ee04e2007-11-30 11:30:34 -0500731 tree_root->sectorsize, tree_root->stripesize,
732 root, fs_info, location->objectid);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400733
734 path = btrfs_alloc_path();
735 BUG_ON(!path);
736 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
737 if (ret != 0) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400738 if (ret > 0)
739 ret = -ENOENT;
740 goto out;
741 }
Chris Mason5f39d392007-10-15 16:14:19 -0400742 l = path->nodes[0];
743 read_extent_buffer(l, &root->root_item,
744 btrfs_item_ptr_offset(l, path->slots[0]),
Chris Mason0f7d52f2007-04-09 10:42:37 -0400745 sizeof(root->root_item));
Yan Zheng44b36eb2007-10-19 09:22:56 -0400746 memcpy(&root->root_key, location, sizeof(*location));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400747 ret = 0;
748out:
749 btrfs_release_path(root, path);
750 btrfs_free_path(path);
751 if (ret) {
752 kfree(root);
753 return ERR_PTR(ret);
754 }
Chris Masondb945352007-10-15 16:15:53 -0400755 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
756 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
757 blocksize);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400758 BUG_ON(!root->node);
759insert:
Chris Mason0f7d52f2007-04-09 10:42:37 -0400760 root->ref_cows = 1;
Chris Mason5eda7b52007-06-22 14:16:25 -0400761 ret = btrfs_find_highest_inode(root, &highest_inode);
762 if (ret == 0) {
763 root->highest_inode = highest_inode;
764 root->last_inode_alloc = highest_inode;
765 }
766 return root;
767}
768
Chris Masondc17ff82008-01-08 15:46:30 -0500769struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
770 u64 root_objectid)
771{
772 struct btrfs_root *root;
773
774 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
775 return fs_info->tree_root;
776 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
777 return fs_info->extent_root;
778
779 root = radix_tree_lookup(&fs_info->fs_roots_radix,
780 (unsigned long)root_objectid);
781 return root;
782}
783
Chris Masonedbd8d42007-12-21 16:27:24 -0500784struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
785 struct btrfs_key *location)
Chris Mason5eda7b52007-06-22 14:16:25 -0400786{
787 struct btrfs_root *root;
788 int ret;
789
Chris Masonedbd8d42007-12-21 16:27:24 -0500790 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
791 return fs_info->tree_root;
792 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
793 return fs_info->extent_root;
794
Chris Mason5eda7b52007-06-22 14:16:25 -0400795 root = radix_tree_lookup(&fs_info->fs_roots_radix,
796 (unsigned long)location->objectid);
797 if (root)
798 return root;
799
800 root = btrfs_read_fs_root_no_radix(fs_info, location);
801 if (IS_ERR(root))
802 return root;
Chris Mason2619ba12007-04-10 16:58:11 -0400803 ret = radix_tree_insert(&fs_info->fs_roots_radix,
804 (unsigned long)root->root_key.objectid,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400805 root);
806 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400807 free_extent_buffer(root->node);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400808 kfree(root);
809 return ERR_PTR(ret);
810 }
Chris Masonedbd8d42007-12-21 16:27:24 -0500811 ret = btrfs_find_dead_roots(fs_info->tree_root,
812 root->root_key.objectid, root);
813 BUG_ON(ret);
814
815 return root;
816}
817
818struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
819 struct btrfs_key *location,
820 const char *name, int namelen)
821{
822 struct btrfs_root *root;
823 int ret;
824
825 root = btrfs_read_fs_root_no_name(fs_info, location);
826 if (!root)
827 return NULL;
Josef Bacik58176a92007-08-29 15:47:34 -0400828
Chris Mason4313b392008-01-03 09:08:48 -0500829 if (root->in_sysfs)
830 return root;
831
Josef Bacik58176a92007-08-29 15:47:34 -0400832 ret = btrfs_set_root_name(root, name, namelen);
833 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400834 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400835 kfree(root);
836 return ERR_PTR(ret);
837 }
838
839 ret = btrfs_sysfs_add_root(root);
840 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400841 free_extent_buffer(root->node);
Josef Bacik58176a92007-08-29 15:47:34 -0400842 kfree(root->name);
843 kfree(root);
844 return ERR_PTR(ret);
845 }
Chris Mason4313b392008-01-03 09:08:48 -0500846 root->in_sysfs = 1;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400847 return root;
848}
Chris Mason19c00dd2007-10-15 16:19:22 -0400849#if 0
850static int add_hasher(struct btrfs_fs_info *info, char *type) {
851 struct btrfs_hasher *hasher;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400852
Chris Mason19c00dd2007-10-15 16:19:22 -0400853 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
854 if (!hasher)
855 return -ENOMEM;
856 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
857 if (!hasher->hash_tfm) {
858 kfree(hasher);
859 return -EINVAL;
860 }
861 spin_lock(&info->hash_lock);
862 list_add(&hasher->list, &info->hashers);
863 spin_unlock(&info->hash_lock);
864 return 0;
865}
866#endif
Chris Mason04160082008-03-26 10:28:07 -0400867
868static int btrfs_congested_fn(void *congested_data, int bdi_bits)
869{
870 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
871 int ret = 0;
872 struct list_head *cur;
873 struct btrfs_device *device;
874 struct backing_dev_info *bdi;
875
876 list_for_each(cur, &info->fs_devices->devices) {
877 device = list_entry(cur, struct btrfs_device, dev_list);
878 bdi = blk_get_backing_dev_info(device->bdev);
879 if (bdi && bdi_congested(bdi, bdi_bits)) {
880 ret = 1;
881 break;
882 }
883 }
884 return ret;
885}
886
887void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
888{
889 struct list_head *cur;
890 struct btrfs_device *device;
891 struct btrfs_fs_info *info;
892
893 info = (struct btrfs_fs_info *)bdi->unplug_io_data;
894 list_for_each(cur, &info->fs_devices->devices) {
895 device = list_entry(cur, struct btrfs_device, dev_list);
896 bdi = blk_get_backing_dev_info(device->bdev);
897 if (bdi->unplug_io_fn) {
898 bdi->unplug_io_fn(bdi, page);
899 }
900 }
901}
902
903static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
904{
Chris Masonb248a412008-04-14 09:48:18 -0400905#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
Chris Mason04160082008-03-26 10:28:07 -0400906 bdi_init(bdi);
Chris Masonb248a412008-04-14 09:48:18 -0400907#endif
Chris Mason83041ad2008-03-26 12:02:55 -0400908 bdi->ra_pages = default_backing_dev_info.ra_pages * 4;
Chris Mason04160082008-03-26 10:28:07 -0400909 bdi->state = 0;
910 bdi->capabilities = default_backing_dev_info.capabilities;
911 bdi->unplug_io_fn = btrfs_unplug_io_fn;
912 bdi->unplug_io_data = info;
913 bdi->congested_fn = btrfs_congested_fn;
914 bdi->congested_data = info;
915 return 0;
916}
917
Chris Masonce9adaa2008-04-09 16:28:12 -0400918static int bio_ready_for_csum(struct bio *bio)
919{
920 u64 length = 0;
921 u64 buf_len = 0;
922 u64 start = 0;
923 struct page *page;
924 struct extent_io_tree *io_tree = NULL;
925 struct btrfs_fs_info *info = NULL;
926 struct bio_vec *bvec;
927 int i;
928 int ret;
929
930 bio_for_each_segment(bvec, bio, i) {
931 page = bvec->bv_page;
932 if (page->private == EXTENT_PAGE_PRIVATE) {
933 length += bvec->bv_len;
934 continue;
935 }
936 if (!page->private) {
937 length += bvec->bv_len;
938 continue;
939 }
940 length = bvec->bv_len;
941 buf_len = page->private >> 2;
942 start = page_offset(page) + bvec->bv_offset;
943 io_tree = &BTRFS_I(page->mapping->host)->io_tree;
944 info = BTRFS_I(page->mapping->host)->root->fs_info;
945 }
946 /* are we fully contained in this bio? */
947 if (buf_len <= length)
948 return 1;
949
950 ret = extent_range_uptodate(io_tree, start + length,
951 start + buf_len - 1);
952 if (ret == 1)
953 return ret;
954 return ret;
955}
956
957#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
Chris Mason44b8bd72008-04-16 11:14:51 -0400958static void btrfs_end_io_csum(void *p)
Chris Masonce9adaa2008-04-09 16:28:12 -0400959#else
Chris Mason44b8bd72008-04-16 11:14:51 -0400960static void btrfs_end_io_csum(struct work_struct *work)
Chris Masonce9adaa2008-04-09 16:28:12 -0400961#endif
962{
963#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
964 struct btrfs_fs_info *fs_info = p;
965#else
966 struct btrfs_fs_info *fs_info = container_of(work,
967 struct btrfs_fs_info,
968 end_io_work);
969#endif
970 unsigned long flags;
971 struct end_io_wq *end_io_wq;
972 struct bio *bio;
973 struct list_head *next;
974 int error;
975 int was_empty;
976
977 while(1) {
978 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
979 if (list_empty(&fs_info->end_io_work_list)) {
980 spin_unlock_irqrestore(&fs_info->end_io_work_lock,
981 flags);
982 return;
983 }
984 next = fs_info->end_io_work_list.next;
985 list_del(next);
986 spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
987
988 end_io_wq = list_entry(next, struct end_io_wq, list);
989
990 bio = end_io_wq->bio;
Chris Mason22c59942008-04-09 16:28:12 -0400991 if (end_io_wq->metadata && !bio_ready_for_csum(bio)) {
Chris Masonce9adaa2008-04-09 16:28:12 -0400992 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
993 was_empty = list_empty(&fs_info->end_io_work_list);
994 list_add_tail(&end_io_wq->list,
995 &fs_info->end_io_work_list);
996 spin_unlock_irqrestore(&fs_info->end_io_work_lock,
997 flags);
998 if (was_empty)
999 return;
1000 continue;
1001 }
1002 error = end_io_wq->error;
1003 bio->bi_private = end_io_wq->private;
1004 bio->bi_end_io = end_io_wq->end_io;
1005 kfree(end_io_wq);
Miguel73f61b22008-04-11 15:50:59 -04001006#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
Chris Masonb248a412008-04-14 09:48:18 -04001007 bio_endio(bio, bio->bi_size, error);
Miguel73f61b22008-04-11 15:50:59 -04001008#else
Chris Masonce9adaa2008-04-09 16:28:12 -04001009 bio_endio(bio, error);
Miguel73f61b22008-04-11 15:50:59 -04001010#endif
Chris Masonce9adaa2008-04-09 16:28:12 -04001011 }
1012}
1013
Chris Mason44b8bd72008-04-16 11:14:51 -04001014#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1015static void btrfs_async_submit_work(void *p)
1016#else
1017static void btrfs_async_submit_work(struct work_struct *work)
1018#endif
1019{
1020#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1021 struct btrfs_fs_info *fs_info = p;
1022#else
1023 struct btrfs_fs_info *fs_info = container_of(work,
1024 struct btrfs_fs_info,
1025 async_submit_work);
1026#endif
1027 struct async_submit_bio *async;
1028 struct list_head *next;
1029
1030 while(1) {
1031 spin_lock(&fs_info->async_submit_work_lock);
1032 if (list_empty(&fs_info->async_submit_work_list)) {
1033 spin_unlock(&fs_info->async_submit_work_lock);
1034 return;
1035 }
1036 next = fs_info->async_submit_work_list.next;
1037 list_del(next);
1038 spin_unlock(&fs_info->async_submit_work_lock);
1039
1040 async = list_entry(next, struct async_submit_bio, list);
1041 async->submit_bio_hook(async->inode, async->rw, async->bio,
1042 async->mirror_num);
1043 kfree(async);
1044 }
1045}
1046
Chris Mason8a4b83c2008-03-24 15:02:07 -04001047struct btrfs_root *open_ctree(struct super_block *sb,
1048 struct btrfs_fs_devices *fs_devices)
Chris Masoneb60cea2007-02-02 09:18:22 -05001049{
Chris Masondb945352007-10-15 16:15:53 -04001050 u32 sectorsize;
1051 u32 nodesize;
1052 u32 leafsize;
1053 u32 blocksize;
Chris Mason87ee04e2007-11-30 11:30:34 -05001054 u32 stripesize;
Chris Masone20d96d2007-03-22 12:13:20 -04001055 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
1056 GFP_NOFS);
1057 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
1058 GFP_NOFS);
Chris Mason8790d502008-04-03 16:29:03 -04001059 struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
Chris Masone20d96d2007-03-22 12:13:20 -04001060 GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04001061 struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
1062 GFP_NOFS);
1063 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
1064 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -05001065 int ret;
Yane58ca022008-04-01 11:21:34 -04001066 int err = -EINVAL;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001067 struct btrfs_super_block *disk_super;
Chris Mason8790d502008-04-03 16:29:03 -04001068
Chris Mason39279cc2007-06-12 06:35:45 -04001069 if (!extent_root || !tree_root || !fs_info) {
1070 err = -ENOMEM;
1071 goto fail;
1072 }
Chris Masonce9adaa2008-04-09 16:28:12 -04001073 end_io_workqueue = create_workqueue("btrfs-end-io");
1074 BUG_ON(!end_io_workqueue);
Chris Mason44b8bd72008-04-16 11:14:51 -04001075 async_submit_workqueue = create_workqueue("btrfs-async-submit");
Chris Masonce9adaa2008-04-09 16:28:12 -04001076
Chris Mason0f7d52f2007-04-09 10:42:37 -04001077 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
Chris Mason8fd17792007-04-19 21:01:03 -04001078 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -04001079 INIT_LIST_HEAD(&fs_info->dead_roots);
Chris Mason19c00dd2007-10-15 16:19:22 -04001080 INIT_LIST_HEAD(&fs_info->hashers);
Chris Masonce9adaa2008-04-09 16:28:12 -04001081 INIT_LIST_HEAD(&fs_info->end_io_work_list);
Chris Mason44b8bd72008-04-16 11:14:51 -04001082 INIT_LIST_HEAD(&fs_info->async_submit_work_list);
Chris Mason19c00dd2007-10-15 16:19:22 -04001083 spin_lock_init(&fs_info->hash_lock);
Chris Masonce9adaa2008-04-09 16:28:12 -04001084 spin_lock_init(&fs_info->end_io_work_lock);
Chris Mason44b8bd72008-04-16 11:14:51 -04001085 spin_lock_init(&fs_info->async_submit_work_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -05001086 spin_lock_init(&fs_info->delalloc_lock);
Chris Masoncee36a02008-01-15 08:40:48 -05001087 spin_lock_init(&fs_info->new_trans_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -04001088
Josef Bacik58176a92007-08-29 15:47:34 -04001089 init_completion(&fs_info->kobj_unregister);
Chris Masonf2984462008-04-10 16:19:33 -04001090 sb_set_blocksize(sb, BTRFS_SUPER_INFO_SIZE);
Chris Mason9f5fae22007-03-20 14:38:32 -04001091 fs_info->tree_root = tree_root;
1092 fs_info->extent_root = extent_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001093 fs_info->chunk_root = chunk_root;
1094 fs_info->dev_root = dev_root;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001095 fs_info->fs_devices = fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04001096 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
Chris Mason6324fbf2008-03-24 15:01:59 -04001097 INIT_LIST_HEAD(&fs_info->space_info);
Chris Mason0b86a832008-03-24 15:01:56 -04001098 btrfs_mapping_init(&fs_info->mapping_tree);
Chris Masone20d96d2007-03-22 12:13:20 -04001099 fs_info->sb = sb;
Chris Masonc59f8952007-12-17 20:14:04 -05001100 fs_info->max_extent = (u64)-1;
Chris Mason6f568d32008-01-29 16:03:38 -05001101 fs_info->max_inline = 8192 * 1024;
Chris Mason04160082008-03-26 10:28:07 -04001102 setup_bdi(fs_info, &fs_info->bdi);
Chris Masond98237b2007-03-28 13:57:48 -04001103 fs_info->btree_inode = new_inode(sb);
1104 fs_info->btree_inode->i_ino = 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001105 fs_info->btree_inode->i_nlink = 1;
Chris Masond98237b2007-03-28 13:57:48 -04001106 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
1107 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
Chris Mason04160082008-03-26 10:28:07 -04001108 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
1109
Chris Masond1310b22008-01-24 16:13:08 -05001110 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -04001111 fs_info->btree_inode->i_mapping,
1112 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001113 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
1114 GFP_NOFS);
Chris Mason0da54682007-11-07 21:08:01 -05001115
Chris Masond1310b22008-01-24 16:13:08 -05001116 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
1117
1118 extent_io_tree_init(&fs_info->free_space_cache,
Chris Masonf510cfe2007-10-15 16:14:48 -04001119 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001120 extent_io_tree_init(&fs_info->block_group_cache,
Chris Mason96b51792007-10-15 16:15:19 -04001121 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001122 extent_io_tree_init(&fs_info->pinned_extents,
Chris Mason1a5bc162007-10-15 16:15:26 -04001123 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001124 extent_io_tree_init(&fs_info->pending_del,
Chris Mason1a5bc162007-10-15 16:15:26 -04001125 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001126 extent_io_tree_init(&fs_info->extent_ins,
Chris Mason1a5bc162007-10-15 16:15:26 -04001127 fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Masone66f7092007-04-20 13:16:02 -04001128 fs_info->do_barriers = 1;
Chris Masone18e4802008-01-18 10:54:22 -05001129
Chris Mason6da6aba2007-12-18 16:15:09 -05001130#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
Chris Masonb248a412008-04-14 09:48:18 -04001131 INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum, fs_info);
Chris Mason44b8bd72008-04-16 11:14:51 -04001132 INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work,
1133 fs_info);
Chris Mason6da6aba2007-12-18 16:15:09 -05001134 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
1135#else
Chris Masonb248a412008-04-14 09:48:18 -04001136 INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum);
Chris Mason44b8bd72008-04-16 11:14:51 -04001137 INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work);
Chris Mason08607c12007-06-08 15:33:54 -04001138 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
Chris Mason6da6aba2007-12-18 16:15:09 -05001139#endif
Chris Mason0f7d52f2007-04-09 10:42:37 -04001140 BTRFS_I(fs_info->btree_inode)->root = tree_root;
1141 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
1142 sizeof(struct btrfs_key));
Chris Mason22b0ebd2007-03-30 08:47:31 -04001143 insert_inode_hash(fs_info->btree_inode);
Chris Masond98237b2007-03-28 13:57:48 -04001144 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001145
Chris Mason79154b12007-03-22 15:59:16 -04001146 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -04001147 mutex_init(&fs_info->fs_mutex);
Chris Mason3768f362007-03-13 16:47:54 -04001148
Chris Mason19c00dd2007-10-15 16:19:22 -04001149#if 0
1150 ret = add_hasher(fs_info, "crc32c");
1151 if (ret) {
1152 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
1153 err = -ENOMEM;
1154 goto fail_iput;
1155 }
1156#endif
Chris Mason0b86a832008-03-24 15:01:56 -04001157 __setup_root(4096, 4096, 4096, 4096, tree_root,
Chris Mason2c90e5d2007-04-02 10:50:19 -04001158 fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -04001159
Chris Mason2c90e5d2007-04-02 10:50:19 -04001160 fs_info->sb_buffer = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -04001161 BTRFS_SUPER_INFO_OFFSET,
Chris Mason0b86a832008-03-24 15:01:56 -04001162 4096);
Chris Masond98237b2007-03-28 13:57:48 -04001163
Chris Mason0f7d52f2007-04-09 10:42:37 -04001164 if (!fs_info->sb_buffer)
Chris Mason39279cc2007-06-12 06:35:45 -04001165 goto fail_iput;
Chris Mason39279cc2007-06-12 06:35:45 -04001166
Chris Mason5f39d392007-10-15 16:14:19 -04001167 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
1168 sizeof(fs_info->super_copy));
1169
1170 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
1171 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
1172 BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001173
Chris Mason5f39d392007-10-15 16:14:19 -04001174 disk_super = &fs_info->super_copy;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001175 if (!btrfs_super_root(disk_super))
Chris Mason39279cc2007-06-12 06:35:45 -04001176 goto fail_sb_buffer;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001177
Chris Mason8a4b83c2008-03-24 15:02:07 -04001178 if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
1179 printk("Btrfs: wanted %llu devices, but found %llu\n",
1180 (unsigned long long)btrfs_super_num_devices(disk_super),
1181 (unsigned long long)fs_devices->num_devices);
1182 goto fail_sb_buffer;
1183 }
Chris Masondb945352007-10-15 16:15:53 -04001184 nodesize = btrfs_super_nodesize(disk_super);
1185 leafsize = btrfs_super_leafsize(disk_super);
1186 sectorsize = btrfs_super_sectorsize(disk_super);
Chris Mason87ee04e2007-11-30 11:30:34 -05001187 stripesize = btrfs_super_stripesize(disk_super);
Chris Masondb945352007-10-15 16:15:53 -04001188 tree_root->nodesize = nodesize;
1189 tree_root->leafsize = leafsize;
1190 tree_root->sectorsize = sectorsize;
Chris Mason87ee04e2007-11-30 11:30:34 -05001191 tree_root->stripesize = stripesize;
Chris Masonff79f812007-10-15 16:22:25 -04001192 sb_set_blocksize(sb, sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04001193
Chris Mason8352d8a2007-04-12 10:43:05 -04001194 i_size_write(fs_info->btree_inode,
Chris Masondb945352007-10-15 16:15:53 -04001195 btrfs_super_total_bytes(disk_super));
Chris Mason8352d8a2007-04-12 10:43:05 -04001196
Chris Mason39279cc2007-06-12 06:35:45 -04001197 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
1198 sizeof(disk_super->magic))) {
1199 printk("btrfs: valid FS not found on %s\n", sb->s_id);
1200 goto fail_sb_buffer;
1201 }
Chris Mason19c00dd2007-10-15 16:19:22 -04001202
Chris Mason0b86a832008-03-24 15:01:56 -04001203 mutex_lock(&fs_info->fs_mutex);
Chris Mason0d81ba52008-03-24 15:02:07 -04001204
Chris Mason0b86a832008-03-24 15:01:56 -04001205 ret = btrfs_read_sys_array(tree_root);
1206 BUG_ON(ret);
1207
1208 blocksize = btrfs_level_size(tree_root,
1209 btrfs_super_chunk_root_level(disk_super));
1210
1211 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1212 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1213
1214 chunk_root->node = read_tree_block(chunk_root,
1215 btrfs_super_chunk_root(disk_super),
1216 blocksize);
1217 BUG_ON(!chunk_root->node);
1218
Chris Masone17cade2008-04-15 15:41:47 -04001219 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
1220 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
1221 BTRFS_UUID_SIZE);
1222
Chris Mason0b86a832008-03-24 15:01:56 -04001223 ret = btrfs_read_chunk_tree(chunk_root);
1224 BUG_ON(ret);
1225
Chris Masondb945352007-10-15 16:15:53 -04001226 blocksize = btrfs_level_size(tree_root,
1227 btrfs_super_root_level(disk_super));
Chris Mason19c00dd2007-10-15 16:19:22 -04001228
Chris Mason0b86a832008-03-24 15:01:56 -04001229
Chris Masone20d96d2007-03-22 12:13:20 -04001230 tree_root->node = read_tree_block(tree_root,
Chris Masondb945352007-10-15 16:15:53 -04001231 btrfs_super_root(disk_super),
1232 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04001233 if (!tree_root->node)
1234 goto fail_sb_buffer;
Chris Mason3768f362007-03-13 16:47:54 -04001235
Chris Masondb945352007-10-15 16:15:53 -04001236
1237 ret = find_and_setup_root(tree_root, fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -04001238 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason0b86a832008-03-24 15:01:56 -04001239 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001240 goto fail_tree_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001241 extent_root->track_dirty = 1;
1242
1243 ret = find_and_setup_root(tree_root, fs_info,
1244 BTRFS_DEV_TREE_OBJECTID, dev_root);
1245 dev_root->track_dirty = 1;
1246
1247 if (ret)
1248 goto fail_extent_root;
Chris Mason3768f362007-03-13 16:47:54 -04001249
Chris Mason9078a3e2007-04-26 16:46:15 -04001250 btrfs_read_block_groups(extent_root);
1251
Chris Mason0f7d52f2007-04-09 10:42:37 -04001252 fs_info->generation = btrfs_super_generation(disk_super) + 1;
Chris Masond18a2c42008-04-04 15:40:00 -04001253 fs_info->data_alloc_profile = (u64)-1;
1254 fs_info->metadata_alloc_profile = (u64)-1;
1255 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1256
Chris Mason5be6f7f2007-04-05 13:35:25 -04001257 mutex_unlock(&fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001258 return tree_root;
Chris Mason39279cc2007-06-12 06:35:45 -04001259
Chris Mason0b86a832008-03-24 15:01:56 -04001260fail_extent_root:
1261 free_extent_buffer(extent_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -04001262fail_tree_root:
Chris Mason0b86a832008-03-24 15:01:56 -04001263 mutex_unlock(&fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001264 free_extent_buffer(tree_root->node);
Chris Mason39279cc2007-06-12 06:35:45 -04001265fail_sb_buffer:
Chris Mason5f39d392007-10-15 16:14:19 -04001266 free_extent_buffer(fs_info->sb_buffer);
Chris Mason2d2ae542008-03-26 16:24:23 -04001267 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04001268fail_iput:
1269 iput(fs_info->btree_inode);
1270fail:
Chris Mason8a4b83c2008-03-24 15:02:07 -04001271 close_all_devices(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04001272 kfree(extent_root);
1273 kfree(tree_root);
Chris Masonb248a412008-04-14 09:48:18 -04001274#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
Chris Mason2d2ae542008-03-26 16:24:23 -04001275 bdi_destroy(&fs_info->bdi);
Chris Masonb248a412008-04-14 09:48:18 -04001276#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001277 kfree(fs_info);
1278 return ERR_PTR(err);
Chris Masoneb60cea2007-02-02 09:18:22 -05001279}
1280
Chris Masonf2984462008-04-10 16:19:33 -04001281static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
1282{
1283 char b[BDEVNAME_SIZE];
1284
1285 if (uptodate) {
1286 set_buffer_uptodate(bh);
1287 } else {
1288 if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
1289 printk(KERN_WARNING "lost page write due to "
1290 "I/O error on %s\n",
1291 bdevname(bh->b_bdev, b));
1292 }
1293 set_buffer_write_io_error(bh);
1294 clear_buffer_uptodate(bh);
1295 }
1296 unlock_buffer(bh);
1297 put_bh(bh);
1298}
1299
1300int write_all_supers(struct btrfs_root *root)
1301{
1302 struct list_head *cur;
1303 struct list_head *head = &root->fs_info->fs_devices->devices;
1304 struct btrfs_device *dev;
1305 struct extent_buffer *sb;
1306 struct btrfs_dev_item *dev_item;
1307 struct buffer_head *bh;
1308 int ret;
1309 int do_barriers;
1310
1311 do_barriers = !btrfs_test_opt(root, NOBARRIER);
1312
1313 sb = root->fs_info->sb_buffer;
1314 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
1315 dev_item);
1316 list_for_each(cur, head) {
1317 dev = list_entry(cur, struct btrfs_device, dev_list);
1318 btrfs_set_device_type(sb, dev_item, dev->type);
1319 btrfs_set_device_id(sb, dev_item, dev->devid);
1320 btrfs_set_device_total_bytes(sb, dev_item, dev->total_bytes);
1321 btrfs_set_device_bytes_used(sb, dev_item, dev->bytes_used);
1322 btrfs_set_device_io_align(sb, dev_item, dev->io_align);
1323 btrfs_set_device_io_width(sb, dev_item, dev->io_width);
1324 btrfs_set_device_sector_size(sb, dev_item, dev->sector_size);
1325 write_extent_buffer(sb, dev->uuid,
1326 (unsigned long)btrfs_device_uuid(dev_item),
Chris Masone17cade2008-04-15 15:41:47 -04001327 BTRFS_UUID_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -04001328
1329 btrfs_set_header_flag(sb, BTRFS_HEADER_FLAG_WRITTEN);
1330 csum_tree_block(root, sb, 0);
1331
1332 bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET /
1333 root->fs_info->sb->s_blocksize,
1334 BTRFS_SUPER_INFO_SIZE);
1335
1336 read_extent_buffer(sb, bh->b_data, 0, BTRFS_SUPER_INFO_SIZE);
1337 dev->pending_io = bh;
1338
1339 get_bh(bh);
1340 set_buffer_uptodate(bh);
1341 lock_buffer(bh);
1342 bh->b_end_io = btrfs_end_buffer_write_sync;
1343
1344 if (do_barriers && dev->barriers) {
1345 ret = submit_bh(WRITE_BARRIER, bh);
1346 if (ret == -EOPNOTSUPP) {
1347 printk("btrfs: disabling barriers on dev %s\n",
1348 dev->name);
1349 set_buffer_uptodate(bh);
1350 dev->barriers = 0;
1351 get_bh(bh);
1352 lock_buffer(bh);
1353 ret = submit_bh(WRITE, bh);
1354 }
1355 } else {
1356 ret = submit_bh(WRITE, bh);
1357 }
1358 BUG_ON(ret);
1359 }
1360
1361 list_for_each(cur, head) {
1362 dev = list_entry(cur, struct btrfs_device, dev_list);
1363 BUG_ON(!dev->pending_io);
1364 bh = dev->pending_io;
1365 wait_on_buffer(bh);
1366 if (!buffer_uptodate(dev->pending_io)) {
1367 if (do_barriers && dev->barriers) {
1368 printk("btrfs: disabling barriers on dev %s\n",
1369 dev->name);
1370 set_buffer_uptodate(bh);
1371 get_bh(bh);
1372 lock_buffer(bh);
1373 dev->barriers = 0;
1374 ret = submit_bh(WRITE, bh);
1375 BUG_ON(ret);
1376 wait_on_buffer(bh);
1377 BUG_ON(!buffer_uptodate(bh));
1378 } else {
1379 BUG();
1380 }
1381
1382 }
1383 dev->pending_io = NULL;
1384 brelse(bh);
1385 }
1386 return 0;
1387}
1388
Chris Masone089f052007-03-16 16:20:31 -04001389int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -04001390 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -05001391{
Chris Masone66f7092007-04-20 13:16:02 -04001392 int ret;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001393
Chris Masonf2984462008-04-10 16:19:33 -04001394 ret = write_all_supers(root);
1395#if 0
Chris Mason21ad10c2008-01-09 09:23:21 -05001396 if (!btrfs_test_opt(root, NOBARRIER))
1397 blkdev_issue_flush(sb->s_bdev, NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001398 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
Chris Mason5f39d392007-10-15 16:14:19 -04001399 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
1400 super->start, super->len);
Chris Mason21ad10c2008-01-09 09:23:21 -05001401 if (!btrfs_test_opt(root, NOBARRIER))
1402 blkdev_issue_flush(sb->s_bdev, NULL);
Chris Masonf2984462008-04-10 16:19:33 -04001403#endif
Chris Mason5f39d392007-10-15 16:14:19 -04001404 return ret;
Chris Masoncfaa7292007-02-21 17:04:57 -05001405}
1406
Chris Mason5eda7b52007-06-22 14:16:25 -04001407int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -04001408{
1409 radix_tree_delete(&fs_info->fs_roots_radix,
1410 (unsigned long)root->root_key.objectid);
Chris Masonb99aa6c2008-01-14 14:41:16 -05001411 if (root->in_sysfs)
1412 btrfs_sysfs_del_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -04001413 if (root->inode)
1414 iput(root->inode);
1415 if (root->node)
Chris Mason5f39d392007-10-15 16:14:19 -04001416 free_extent_buffer(root->node);
Chris Mason2619ba12007-04-10 16:58:11 -04001417 if (root->commit_root)
Chris Mason5f39d392007-10-15 16:14:19 -04001418 free_extent_buffer(root->commit_root);
Josef Bacik58176a92007-08-29 15:47:34 -04001419 if (root->name)
1420 kfree(root->name);
Chris Mason2619ba12007-04-10 16:58:11 -04001421 kfree(root);
1422 return 0;
1423}
1424
Chris Mason35b7e472007-05-02 15:53:43 -04001425static int del_fs_roots(struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -04001426{
1427 int ret;
1428 struct btrfs_root *gang[8];
1429 int i;
1430
1431 while(1) {
1432 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
1433 (void **)gang, 0,
1434 ARRAY_SIZE(gang));
1435 if (!ret)
1436 break;
Chris Mason2619ba12007-04-10 16:58:11 -04001437 for (i = 0; i < ret; i++)
Chris Mason5eda7b52007-06-22 14:16:25 -04001438 btrfs_free_fs_root(fs_info, gang[i]);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001439 }
1440 return 0;
1441}
Chris Masonb4100d62007-04-12 12:14:00 -04001442
Chris Masone20d96d2007-03-22 12:13:20 -04001443int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -05001444{
Chris Mason3768f362007-03-13 16:47:54 -04001445 int ret;
Chris Masone089f052007-03-16 16:20:31 -04001446 struct btrfs_trans_handle *trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001447 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone089f052007-03-16 16:20:31 -04001448
Chris Masonfacda1e2007-06-08 18:11:48 -04001449 fs_info->closing = 1;
Chris Mason08607c12007-06-08 15:33:54 -04001450 btrfs_transaction_flush_work(root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001451 mutex_lock(&fs_info->fs_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -04001452 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason79154b12007-03-22 15:59:16 -04001453 trans = btrfs_start_transaction(root, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001454 ret = btrfs_commit_transaction(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -04001455 /* run commit again to drop the original snapshot */
1456 trans = btrfs_start_transaction(root, 1);
1457 btrfs_commit_transaction(trans, root);
1458 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -04001459 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -04001460 write_ctree_super(NULL, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001461 mutex_unlock(&fs_info->fs_mutex);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001462
Chris Masonb0c68f82008-01-31 11:05:37 -05001463 if (fs_info->delalloc_bytes) {
1464 printk("btrfs: at unmount delalloc count %Lu\n",
1465 fs_info->delalloc_bytes);
1466 }
Chris Mason0f7d52f2007-04-09 10:42:37 -04001467 if (fs_info->extent_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -04001468 free_extent_buffer(fs_info->extent_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -04001469
Chris Mason0f7d52f2007-04-09 10:42:37 -04001470 if (fs_info->tree_root->node)
Chris Mason5f39d392007-10-15 16:14:19 -04001471 free_extent_buffer(fs_info->tree_root->node);
Chris Masonf510cfe2007-10-15 16:14:48 -04001472
Chris Mason0b86a832008-03-24 15:01:56 -04001473 if (root->fs_info->chunk_root->node);
1474 free_extent_buffer(root->fs_info->chunk_root->node);
1475
1476 if (root->fs_info->dev_root->node);
1477 free_extent_buffer(root->fs_info->dev_root->node);
1478
Chris Mason5f39d392007-10-15 16:14:19 -04001479 free_extent_buffer(fs_info->sb_buffer);
Chris Mason7eccb902007-04-11 15:53:25 -04001480
Chris Mason9078a3e2007-04-26 16:46:15 -04001481 btrfs_free_block_groups(root->fs_info);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001482 del_fs_roots(fs_info);
Chris Masond10c5f32007-12-17 20:14:04 -05001483
1484 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
1485
Chris Masond1310b22008-01-24 16:13:08 -05001486 extent_io_tree_empty_lru(&fs_info->free_space_cache);
1487 extent_io_tree_empty_lru(&fs_info->block_group_cache);
1488 extent_io_tree_empty_lru(&fs_info->pinned_extents);
1489 extent_io_tree_empty_lru(&fs_info->pending_del);
1490 extent_io_tree_empty_lru(&fs_info->extent_ins);
1491 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
Chris Masond10c5f32007-12-17 20:14:04 -05001492
Chris Masondb945352007-10-15 16:15:53 -04001493 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
Chris Masonce9adaa2008-04-09 16:28:12 -04001494 flush_workqueue(end_io_workqueue);
1495 destroy_workqueue(end_io_workqueue);
Chris Masond10c5f32007-12-17 20:14:04 -05001496
Chris Mason44b8bd72008-04-16 11:14:51 -04001497 flush_workqueue(async_submit_workqueue);
1498 destroy_workqueue(async_submit_workqueue);
1499
Chris Masondb945352007-10-15 16:15:53 -04001500 iput(fs_info->btree_inode);
Chris Mason19c00dd2007-10-15 16:19:22 -04001501#if 0
1502 while(!list_empty(&fs_info->hashers)) {
1503 struct btrfs_hasher *hasher;
1504 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
1505 hashers);
1506 list_del(&hasher->hashers);
1507 crypto_free_hash(&fs_info->hash_tfm);
1508 kfree(hasher);
1509 }
1510#endif
Chris Mason0b86a832008-03-24 15:01:56 -04001511 close_all_devices(fs_info);
1512 btrfs_mapping_tree_free(&fs_info->mapping_tree);
Chris Masonb248a412008-04-14 09:48:18 -04001513
1514#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
Chris Mason04160082008-03-26 10:28:07 -04001515 bdi_destroy(&fs_info->bdi);
Chris Masonb248a412008-04-14 09:48:18 -04001516#endif
Chris Mason0b86a832008-03-24 15:01:56 -04001517
Chris Mason0f7d52f2007-04-09 10:42:37 -04001518 kfree(fs_info->extent_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001519 kfree(fs_info->tree_root);
Chris Mason0b86a832008-03-24 15:01:56 -04001520 kfree(fs_info->chunk_root);
1521 kfree(fs_info->dev_root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001522 return 0;
1523}
1524
Chris Mason5f39d392007-10-15 16:14:19 -04001525int btrfs_buffer_uptodate(struct extent_buffer *buf)
Chris Masonccd467d2007-06-28 15:57:36 -04001526{
Chris Mason810191f2007-10-15 16:18:55 -04001527 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001528 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001529}
Chris Mason6702ed42007-08-07 16:15:09 -04001530
Chris Mason5f39d392007-10-15 16:14:19 -04001531int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
1532{
Chris Mason810191f2007-10-15 16:18:55 -04001533 struct inode *btree_inode = buf->first_page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001534 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
Chris Mason5f39d392007-10-15 16:14:19 -04001535 buf);
1536}
1537
1538void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
1539{
Chris Mason810191f2007-10-15 16:18:55 -04001540 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04001541 u64 transid = btrfs_header_generation(buf);
1542 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason6702ed42007-08-07 16:15:09 -04001543
Chris Masonccd467d2007-06-28 15:57:36 -04001544 if (transid != root->fs_info->generation) {
1545 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
Chris Masondb945352007-10-15 16:15:53 -04001546 (unsigned long long)buf->start,
Chris Masonccd467d2007-06-28 15:57:36 -04001547 transid, root->fs_info->generation);
1548 WARN_ON(1);
1549 }
Chris Masond1310b22008-01-24 16:13:08 -05001550 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001551}
1552
Chris Masone2008b62008-01-08 15:46:30 -05001553void btrfs_throttle(struct btrfs_root *root)
1554{
Chris Mason55c69072008-01-09 15:55:33 -05001555 struct backing_dev_info *bdi;
1556
1557 bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
Chris Mason04005cc2008-01-17 12:01:41 -05001558 if (root->fs_info->throttles && bdi_write_congested(bdi)) {
1559#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
Chris Mason55c69072008-01-09 15:55:33 -05001560 congestion_wait(WRITE, HZ/20);
Chris Mason04005cc2008-01-17 12:01:41 -05001561#else
1562 blk_congestion_wait(WRITE, HZ/20);
1563#endif
1564 }
Chris Masone2008b62008-01-08 15:46:30 -05001565}
1566
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001567void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
Chris Mason35b7e472007-05-02 15:53:43 -04001568{
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001569 balance_dirty_pages_ratelimited_nr(
Chris Masond7fc6402008-02-18 12:12:38 -05001570 root->fs_info->btree_inode->i_mapping, 1);
Chris Mason35b7e472007-05-02 15:53:43 -04001571}
Chris Mason6b800532007-10-15 16:17:34 -04001572
1573void btrfs_set_buffer_defrag(struct extent_buffer *buf)
1574{
Chris Mason810191f2007-10-15 16:18:55 -04001575 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001576 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001577 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
Chris Mason6b800532007-10-15 16:17:34 -04001578 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
1579}
1580
1581void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
1582{
Chris Mason810191f2007-10-15 16:18:55 -04001583 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001584 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001585 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
Chris Mason6b800532007-10-15 16:17:34 -04001586 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
1587 GFP_NOFS);
1588}
1589
1590int btrfs_buffer_defrag(struct extent_buffer *buf)
1591{
Chris Mason810191f2007-10-15 16:18:55 -04001592 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001593 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001594 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001595 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
1596}
1597
1598int btrfs_buffer_defrag_done(struct extent_buffer *buf)
1599{
Chris Mason810191f2007-10-15 16:18:55 -04001600 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001601 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001602 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001603 buf->start, buf->start + buf->len - 1,
1604 EXTENT_DEFRAG_DONE, 0);
1605}
1606
1607int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
1608{
Chris Mason810191f2007-10-15 16:18:55 -04001609 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001610 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001611 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001612 buf->start, buf->start + buf->len - 1,
1613 EXTENT_DEFRAG_DONE, GFP_NOFS);
1614}
1615
1616int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
1617{
Chris Mason810191f2007-10-15 16:18:55 -04001618 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Mason6b800532007-10-15 16:17:34 -04001619 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masond1310b22008-01-24 16:13:08 -05001620 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
Chris Mason6b800532007-10-15 16:17:34 -04001621 buf->start, buf->start + buf->len - 1,
1622 EXTENT_DEFRAG, GFP_NOFS);
1623}
1624
1625int btrfs_read_buffer(struct extent_buffer *buf)
1626{
Chris Mason810191f2007-10-15 16:18:55 -04001627 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
Chris Masonce9adaa2008-04-09 16:28:12 -04001628 int ret;
Chris Masonf1885912008-04-09 16:28:12 -04001629 ret = btree_read_extent_buffer_pages(root, buf, 0);
Chris Masonce9adaa2008-04-09 16:28:12 -04001630 if (ret == 0) {
1631 buf->flags |= EXTENT_UPTODATE;
1632 }
1633 return ret;
Chris Mason6b800532007-10-15 16:17:34 -04001634}
Chris Mason0da54682007-11-07 21:08:01 -05001635
Chris Masond1310b22008-01-24 16:13:08 -05001636static struct extent_io_ops btree_extent_io_ops = {
Chris Mason0da54682007-11-07 21:08:01 -05001637 .writepage_io_hook = btree_writepage_io_hook,
Chris Masonce9adaa2008-04-09 16:28:12 -04001638 .readpage_end_io_hook = btree_readpage_end_io_hook,
Chris Mason0b86a832008-03-24 15:01:56 -04001639 .submit_bio_hook = btree_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04001640 /* note we're sharing with inode.c for the merge bio hook */
1641 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason0da54682007-11-07 21:08:01 -05001642};