]>
Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
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 | ||
e20d96d6 | 19 | #include <linux/fs.h> |
d98237b3 | 20 | #include <linux/blkdev.h> |
87cbda5c | 21 | #include <linux/scatterlist.h> |
22b0ebda | 22 | #include <linux/swap.h> |
0f7d52f4 | 23 | #include <linux/radix-tree.h> |
35b7e476 | 24 | #include <linux/writeback.h> |
5f39d397 | 25 | #include <linux/buffer_head.h> // for block_sync_page |
ce9adaa5 | 26 | #include <linux/workqueue.h> |
a5eb62e3 | 27 | #include "crc32c.h" |
eb60ceac CM |
28 | #include "ctree.h" |
29 | #include "disk-io.h" | |
e089f05c | 30 | #include "transaction.h" |
0f7d52f4 | 31 | #include "btrfs_inode.h" |
0b86a832 | 32 | #include "volumes.h" |
db94535d | 33 | #include "print-tree.h" |
eb60ceac | 34 | |
5f39d397 CM |
35 | #if 0 |
36 | static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf) | |
7eccb903 | 37 | { |
5f39d397 CM |
38 | 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)); | |
39279cc3 | 42 | return 1; |
d98237b3 | 43 | } |
9a8dd150 | 44 | return 0; |
eb60ceac | 45 | } |
5f39d397 | 46 | #endif |
eb60ceac | 47 | |
d1310b2e | 48 | static struct extent_io_ops btree_extent_io_ops; |
ce9adaa5 | 49 | static struct workqueue_struct *end_io_workqueue; |
44b8bd7e | 50 | static struct workqueue_struct *async_submit_workqueue; |
ce9adaa5 CM |
51 | |
52 | struct 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; | |
22c59948 | 58 | int metadata; |
ce9adaa5 CM |
59 | struct list_head list; |
60 | }; | |
0da5468f | 61 | |
44b8bd7e CM |
62 | struct 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 | ||
5f39d397 | 71 | struct extent_map *btree_get_extent(struct inode *inode, struct page *page, |
d1310b2e | 72 | size_t page_offset, u64 start, u64 len, |
5f39d397 | 73 | int create) |
7eccb903 | 74 | { |
5f39d397 CM |
75 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
76 | struct extent_map *em; | |
77 | int ret; | |
78 | ||
79 | again: | |
d1310b2e CM |
80 | spin_lock(&em_tree->lock); |
81 | em = lookup_extent_mapping(em_tree, start, len); | |
82 | spin_unlock(&em_tree->lock); | |
5f39d397 CM |
83 | if (em) { |
84 | goto out; | |
7eccb903 | 85 | } |
5f39d397 CM |
86 | em = alloc_extent_map(GFP_NOFS); |
87 | if (!em) { | |
88 | em = ERR_PTR(-ENOMEM); | |
89 | goto out; | |
90 | } | |
91 | em->start = 0; | |
d1310b2e | 92 | em->len = i_size_read(inode); |
5f39d397 | 93 | em->block_start = 0; |
5f39d397 | 94 | em->bdev = inode->i_sb->s_bdev; |
d1310b2e CM |
95 | |
96 | spin_lock(&em_tree->lock); | |
5f39d397 | 97 | ret = add_extent_mapping(em_tree, em); |
d1310b2e CM |
98 | spin_unlock(&em_tree->lock); |
99 | ||
5f39d397 CM |
100 | if (ret == -EEXIST) { |
101 | free_extent_map(em); | |
102 | em = NULL; | |
103 | goto again; | |
104 | } else if (ret) { | |
105 | em = ERR_PTR(ret); | |
106 | } | |
107 | out: | |
108 | return em; | |
7eccb903 CM |
109 | } |
110 | ||
19c00ddc CM |
111 | u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len) |
112 | { | |
a5eb62e3 | 113 | return btrfs_crc32c(seed, data, len); |
19c00ddc CM |
114 | } |
115 | ||
116 | void btrfs_csum_final(u32 crc, char *result) | |
117 | { | |
118 | *(__le32 *)result = ~cpu_to_le32(crc); | |
119 | } | |
120 | ||
121 | static 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) { | |
e4204ded CM |
155 | 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 */ | |
63b10fc4 | 163 | if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) { |
e4204ded CM |
164 | 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 " | |
63b10fc4 CM |
170 | "wanted %X found %X from_this_trans %d " |
171 | "level %d\n", | |
19c00ddc | 172 | root->fs_info->sb->s_id, |
63b10fc4 CM |
173 | buf->start, val, found, from_this_trans, |
174 | btrfs_header_level(buf)); | |
19c00ddc CM |
175 | return 1; |
176 | } | |
177 | } else { | |
178 | write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE); | |
179 | } | |
180 | return 0; | |
181 | } | |
182 | ||
f188591e CM |
183 | static 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) | |
198 | printk("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); | |
203 | printk("failed to read %Lu mirror %d total %d\n", eb->start, mirror_num, num_copies); | |
204 | if (num_copies == 1) { | |
205 | printk("reading %Lu failed only one copy\n", eb->start); | |
206 | return ret; | |
207 | } | |
208 | mirror_num++; | |
209 | if (mirror_num > num_copies) { | |
210 | printk("bailing at mirror %d of %d\n", mirror_num, num_copies); | |
211 | return ret; | |
212 | } | |
213 | } | |
214 | printk("read extent buffer page last\n"); | |
215 | return -EIO; | |
216 | } | |
19c00ddc CM |
217 | |
218 | int csum_dirty_buffer(struct btrfs_root *root, struct page *page) | |
219 | { | |
d1310b2e | 220 | struct extent_io_tree *tree; |
35ebb934 | 221 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
19c00ddc CM |
222 | u64 found_start; |
223 | int found_level; | |
224 | unsigned long len; | |
225 | struct extent_buffer *eb; | |
f188591e CM |
226 | int ret; |
227 | ||
d1310b2e | 228 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
19c00ddc CM |
229 | |
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); | |
f188591e CM |
239 | ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE); |
240 | BUG_ON(ret); | |
e18e4809 | 241 | btrfs_clear_buffer_defrag(eb); |
19c00ddc CM |
242 | 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); | |
55c69072 CM |
246 | 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; | |
19c00ddc CM |
259 | } |
260 | found_level = btrfs_header_level(eb); | |
63b10fc4 CM |
261 | 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); | |
19c00ddc | 264 | csum_tree_block(root, eb, 0); |
55c69072 | 265 | err: |
19c00ddc CM |
266 | free_extent_buffer(eb); |
267 | out: | |
268 | return 0; | |
269 | } | |
270 | ||
0da5468f | 271 | static int btree_writepage_io_hook(struct page *page, u64 start, u64 end) |
d98237b3 | 272 | { |
19c00ddc | 273 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
19c00ddc CM |
274 | |
275 | csum_dirty_buffer(root, page); | |
0da5468f CM |
276 | return 0; |
277 | } | |
278 | ||
ce9adaa5 CM |
279 | int 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; | |
f188591e | 288 | int ret = 0; |
ce9adaa5 CM |
289 | |
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); | |
f188591e | 300 | |
ce9adaa5 CM |
301 | btrfs_clear_buffer_defrag(eb); |
302 | found_start = btrfs_header_bytenr(eb); | |
303 | if (found_start != start) { | |
f188591e CM |
304 | printk("bad start on %Lu found %Lu\n", eb->start, found_start); |
305 | ret = -EIO; | |
ce9adaa5 CM |
306 | 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); | |
f188591e | 312 | ret = -EIO; |
ce9adaa5 CM |
313 | goto err; |
314 | } | |
315 | found_level = btrfs_header_level(eb); | |
316 | ||
317 | ret = csum_tree_block(root, eb, 1); | |
f188591e CM |
318 | if (ret) |
319 | ret = -EIO; | |
ce9adaa5 CM |
320 | |
321 | end = min_t(u64, eb->len, PAGE_CACHE_SIZE); | |
322 | end = eb->start + end - 1; | |
323 | release_extent_buffer_tail_pages(eb); | |
324 | err: | |
325 | free_extent_buffer(eb); | |
326 | out: | |
f188591e | 327 | return ret; |
ce9adaa5 CM |
328 | } |
329 | ||
330 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) | |
331 | static void end_workqueue_bio(struct bio *bio, int err) | |
332 | #else | |
333 | static 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 | ||
22c59948 CM |
358 | int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, |
359 | int metadata) | |
0b86a832 | 360 | { |
ce9adaa5 | 361 | struct end_io_wq *end_io_wq; |
ce9adaa5 CM |
362 | 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; | |
22c59948 | 368 | end_io_wq->info = info; |
ce9adaa5 CM |
369 | end_io_wq->error = 0; |
370 | end_io_wq->bio = bio; | |
22c59948 | 371 | end_io_wq->metadata = metadata; |
ce9adaa5 CM |
372 | |
373 | bio->bi_private = end_io_wq; | |
374 | bio->bi_end_io = end_workqueue_bio; | |
22c59948 CM |
375 | return 0; |
376 | } | |
377 | ||
44b8bd7e CM |
378 | int 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 | ||
7b859fe7 CM |
384 | /* |
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 | ||
44b8bd7e CM |
391 | 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 | ||
409 | static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, | |
f188591e | 410 | int mirror_num) |
22c59948 CM |
411 | { |
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)) { | |
f188591e | 419 | return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num); |
22c59948 CM |
420 | } |
421 | ||
422 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1); | |
423 | BUG_ON(ret); | |
ce9adaa5 | 424 | |
0b86a832 | 425 | if (offset == BTRFS_SUPER_INFO_OFFSET) { |
f2984462 | 426 | bio->bi_bdev = root->fs_info->fs_devices->latest_bdev; |
0b86a832 CM |
427 | submit_bio(rw, bio); |
428 | return 0; | |
429 | } | |
f188591e | 430 | return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num); |
0b86a832 CM |
431 | } |
432 | ||
44b8bd7e CM |
433 | static 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 | ||
0da5468f CM |
444 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
445 | { | |
d1310b2e CM |
446 | struct extent_io_tree *tree; |
447 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
5f39d397 CM |
448 | return extent_write_full_page(tree, page, btree_get_extent, wbc); |
449 | } | |
0da5468f CM |
450 | |
451 | static int btree_writepages(struct address_space *mapping, | |
452 | struct writeback_control *wbc) | |
453 | { | |
d1310b2e CM |
454 | struct extent_io_tree *tree; |
455 | tree = &BTRFS_I(mapping->host)->io_tree; | |
d8d5f3e1 | 456 | if (wbc->sync_mode == WB_SYNC_NONE) { |
793955bc CM |
457 | u64 num_dirty; |
458 | u64 start = 0; | |
459 | unsigned long thresh = 96 * 1024 * 1024; | |
448d640b CM |
460 | |
461 | if (wbc->for_kupdate) | |
462 | return 0; | |
463 | ||
ca664626 CM |
464 | if (current_is_pdflush()) { |
465 | thresh = 96 * 1024 * 1024; | |
466 | } else { | |
467 | thresh = 8 * 1024 * 1024; | |
468 | } | |
1832a6d5 CM |
469 | num_dirty = count_range_bits(tree, &start, (u64)-1, |
470 | thresh, EXTENT_DIRTY); | |
793955bc CM |
471 | if (num_dirty < thresh) { |
472 | return 0; | |
473 | } | |
474 | } | |
0da5468f CM |
475 | return extent_writepages(tree, mapping, btree_get_extent, wbc); |
476 | } | |
477 | ||
5f39d397 CM |
478 | int btree_readpage(struct file *file, struct page *page) |
479 | { | |
d1310b2e CM |
480 | struct extent_io_tree *tree; |
481 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
5f39d397 CM |
482 | return extent_read_full_page(tree, page, btree_get_extent); |
483 | } | |
22b0ebda | 484 | |
70dec807 | 485 | static int btree_releasepage(struct page *page, gfp_t gfp_flags) |
5f39d397 | 486 | { |
d1310b2e CM |
487 | struct extent_io_tree *tree; |
488 | struct extent_map_tree *map; | |
5f39d397 | 489 | int ret; |
d98237b3 | 490 | |
3dd39914 CM |
491 | 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 | } | |
d1310b2e CM |
497 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
498 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 499 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
5f39d397 | 500 | if (ret == 1) { |
728131d8 | 501 | invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE); |
5f39d397 CM |
502 | ClearPagePrivate(page); |
503 | set_page_private(page, 0); | |
504 | page_cache_release(page); | |
505 | } | |
d98237b3 CM |
506 | return ret; |
507 | } | |
508 | ||
5f39d397 | 509 | static void btree_invalidatepage(struct page *page, unsigned long offset) |
d98237b3 | 510 | { |
d1310b2e CM |
511 | struct extent_io_tree *tree; |
512 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
5f39d397 CM |
513 | extent_invalidatepage(tree, page, offset); |
514 | btree_releasepage(page, GFP_NOFS); | |
d98237b3 CM |
515 | } |
516 | ||
5f39d397 | 517 | #if 0 |
d98237b3 | 518 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
ed2ff2cb | 519 | { |
87cbda5c | 520 | struct buffer_head *bh; |
0f7d52f4 | 521 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
87cbda5c | 522 | struct buffer_head *head; |
87cbda5c CM |
523 | 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); | |
d98237b3 | 534 | return block_write_full_page(page, btree_get_block, wbc); |
ed2ff2cb | 535 | } |
5f39d397 | 536 | #endif |
eb60ceac | 537 | |
d98237b3 CM |
538 | static struct address_space_operations btree_aops = { |
539 | .readpage = btree_readpage, | |
540 | .writepage = btree_writepage, | |
0da5468f | 541 | .writepages = btree_writepages, |
5f39d397 CM |
542 | .releasepage = btree_releasepage, |
543 | .invalidatepage = btree_invalidatepage, | |
d98237b3 CM |
544 | .sync_page = block_sync_page, |
545 | }; | |
546 | ||
db94535d | 547 | int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize) |
090d1875 | 548 | { |
5f39d397 CM |
549 | struct extent_buffer *buf = NULL; |
550 | struct inode *btree_inode = root->fs_info->btree_inode; | |
de428b63 | 551 | int ret = 0; |
090d1875 | 552 | |
db94535d | 553 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
5f39d397 | 554 | if (!buf) |
090d1875 | 555 | return 0; |
d1310b2e | 556 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, |
f188591e | 557 | buf, 0, 0, btree_get_extent, 0); |
5f39d397 | 558 | free_extent_buffer(buf); |
de428b63 | 559 | return ret; |
090d1875 CM |
560 | } |
561 | ||
0b86a832 CM |
562 | static 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 | ||
8a4b83cc CM |
568 | list = &fs_info->fs_devices->devices; |
569 | list_for_each(next, list) { | |
0b86a832 | 570 | device = list_entry(next, struct btrfs_device, dev_list); |
8a4b83cc CM |
571 | if (device->bdev && device->bdev != fs_info->sb->s_bdev) |
572 | close_bdev_excl(device->bdev); | |
573 | device->bdev = NULL; | |
0b86a832 CM |
574 | } |
575 | return 0; | |
576 | } | |
577 | ||
0999df54 CM |
578 | int btrfs_verify_block_csum(struct btrfs_root *root, |
579 | struct extent_buffer *buf) | |
eb60ceac | 580 | { |
ce9adaa5 | 581 | return btrfs_buffer_uptodate(buf); |
0999df54 CM |
582 | } |
583 | ||
584 | struct 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 | ||
594 | struct 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 | ||
606 | struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, | |
607 | u32 blocksize) | |
608 | { | |
609 | struct extent_buffer *buf = NULL; | |
610 | struct inode *btree_inode = root->fs_info->btree_inode; | |
611 | struct extent_io_tree *io_tree; | |
612 | int ret; | |
613 | ||
614 | io_tree = &BTRFS_I(btree_inode)->io_tree; | |
615 | ||
616 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); | |
617 | if (!buf) | |
618 | return NULL; | |
0999df54 | 619 | |
f188591e | 620 | ret = btree_read_extent_buffer_pages(root, buf, 0); |
ce9adaa5 CM |
621 | |
622 | if (ret == 0) { | |
623 | buf->flags |= EXTENT_UPTODATE; | |
624 | } | |
5f39d397 | 625 | return buf; |
ce9adaa5 | 626 | |
eb60ceac CM |
627 | } |
628 | ||
e089f05c | 629 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
5f39d397 | 630 | struct extent_buffer *buf) |
ed2ff2cb | 631 | { |
5f39d397 | 632 | struct inode *btree_inode = root->fs_info->btree_inode; |
55c69072 CM |
633 | if (btrfs_header_generation(buf) == |
634 | root->fs_info->running_transaction->transid) | |
d1310b2e | 635 | clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, |
55c69072 | 636 | buf); |
5f39d397 CM |
637 | return 0; |
638 | } | |
639 | ||
640 | int 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; | |
d1310b2e | 644 | wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree, |
5f39d397 CM |
645 | buf); |
646 | return 0; | |
647 | } | |
648 | ||
db94535d | 649 | static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, |
87ee04eb | 650 | u32 stripesize, struct btrfs_root *root, |
9f5fae2f | 651 | struct btrfs_fs_info *fs_info, |
e20d96d6 | 652 | u64 objectid) |
d97e63b6 | 653 | { |
cfaa7295 | 654 | root->node = NULL; |
0f7d52f4 | 655 | root->inode = NULL; |
a28ec197 | 656 | root->commit_root = NULL; |
db94535d CM |
657 | root->sectorsize = sectorsize; |
658 | root->nodesize = nodesize; | |
659 | root->leafsize = leafsize; | |
87ee04eb | 660 | root->stripesize = stripesize; |
123abc88 | 661 | root->ref_cows = 0; |
0b86a832 CM |
662 | root->track_dirty = 0; |
663 | ||
9f5fae2f | 664 | root->fs_info = fs_info; |
0f7d52f4 CM |
665 | root->objectid = objectid; |
666 | root->last_trans = 0; | |
1b05da2e CM |
667 | root->highest_inode = 0; |
668 | root->last_inode_alloc = 0; | |
58176a96 | 669 | root->name = NULL; |
4313b399 | 670 | root->in_sysfs = 0; |
0b86a832 CM |
671 | |
672 | INIT_LIST_HEAD(&root->dirty_list); | |
3768f368 CM |
673 | memset(&root->root_key, 0, sizeof(root->root_key)); |
674 | memset(&root->root_item, 0, sizeof(root->root_item)); | |
6702ed49 | 675 | memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); |
58176a96 JB |
676 | memset(&root->root_kobj, 0, sizeof(root->root_kobj)); |
677 | init_completion(&root->kobj_unregister); | |
6702ed49 CM |
678 | root->defrag_running = 0; |
679 | root->defrag_level = 0; | |
4d775673 | 680 | root->root_key.objectid = objectid; |
3768f368 CM |
681 | return 0; |
682 | } | |
683 | ||
db94535d | 684 | static int find_and_setup_root(struct btrfs_root *tree_root, |
9f5fae2f CM |
685 | struct btrfs_fs_info *fs_info, |
686 | u64 objectid, | |
e20d96d6 | 687 | struct btrfs_root *root) |
3768f368 CM |
688 | { |
689 | int ret; | |
db94535d | 690 | u32 blocksize; |
3768f368 | 691 | |
db94535d | 692 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
87ee04eb CM |
693 | tree_root->sectorsize, tree_root->stripesize, |
694 | root, fs_info, objectid); | |
3768f368 CM |
695 | ret = btrfs_find_last_root(tree_root, objectid, |
696 | &root->root_item, &root->root_key); | |
697 | BUG_ON(ret); | |
698 | ||
db94535d CM |
699 | 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); | |
3768f368 | 702 | BUG_ON(!root->node); |
d97e63b6 CM |
703 | return 0; |
704 | } | |
705 | ||
5eda7b5e CM |
706 | struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, |
707 | struct btrfs_key *location) | |
0f7d52f4 CM |
708 | { |
709 | struct btrfs_root *root; | |
710 | struct btrfs_root *tree_root = fs_info->tree_root; | |
711 | struct btrfs_path *path; | |
5f39d397 | 712 | struct extent_buffer *l; |
1b05da2e | 713 | u64 highest_inode; |
db94535d | 714 | u32 blocksize; |
0f7d52f4 CM |
715 | int ret = 0; |
716 | ||
5eda7b5e | 717 | root = kzalloc(sizeof(*root), GFP_NOFS); |
0cf6c620 | 718 | if (!root) |
0f7d52f4 | 719 | return ERR_PTR(-ENOMEM); |
0f7d52f4 | 720 | if (location->offset == (u64)-1) { |
db94535d | 721 | ret = find_and_setup_root(tree_root, fs_info, |
0f7d52f4 CM |
722 | location->objectid, root); |
723 | if (ret) { | |
0f7d52f4 CM |
724 | kfree(root); |
725 | return ERR_PTR(ret); | |
726 | } | |
727 | goto insert; | |
728 | } | |
729 | ||
db94535d | 730 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
87ee04eb CM |
731 | tree_root->sectorsize, tree_root->stripesize, |
732 | root, fs_info, location->objectid); | |
0f7d52f4 CM |
733 | |
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) { | |
0f7d52f4 CM |
738 | if (ret > 0) |
739 | ret = -ENOENT; | |
740 | goto out; | |
741 | } | |
5f39d397 CM |
742 | l = path->nodes[0]; |
743 | read_extent_buffer(l, &root->root_item, | |
744 | btrfs_item_ptr_offset(l, path->slots[0]), | |
0f7d52f4 | 745 | sizeof(root->root_item)); |
44b36eb2 | 746 | memcpy(&root->root_key, location, sizeof(*location)); |
0f7d52f4 CM |
747 | ret = 0; |
748 | out: | |
749 | btrfs_release_path(root, path); | |
750 | btrfs_free_path(path); | |
751 | if (ret) { | |
752 | kfree(root); | |
753 | return ERR_PTR(ret); | |
754 | } | |
db94535d CM |
755 | 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); | |
0f7d52f4 CM |
758 | BUG_ON(!root->node); |
759 | insert: | |
0f7d52f4 | 760 | root->ref_cows = 1; |
5eda7b5e CM |
761 | 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 | ||
dc17ff8f CM |
769 | struct 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 | ||
edbd8d4e CM |
784 | struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, |
785 | struct btrfs_key *location) | |
5eda7b5e CM |
786 | { |
787 | struct btrfs_root *root; | |
788 | int ret; | |
789 | ||
edbd8d4e CM |
790 | 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 | ||
5eda7b5e CM |
795 | 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; | |
2619ba1f CM |
803 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
804 | (unsigned long)root->root_key.objectid, | |
0f7d52f4 CM |
805 | root); |
806 | if (ret) { | |
5f39d397 | 807 | free_extent_buffer(root->node); |
0f7d52f4 CM |
808 | kfree(root); |
809 | return ERR_PTR(ret); | |
810 | } | |
edbd8d4e CM |
811 | 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 | ||
818 | struct 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; | |
58176a96 | 828 | |
4313b399 CM |
829 | if (root->in_sysfs) |
830 | return root; | |
831 | ||
58176a96 JB |
832 | ret = btrfs_set_root_name(root, name, namelen); |
833 | if (ret) { | |
5f39d397 | 834 | free_extent_buffer(root->node); |
58176a96 JB |
835 | kfree(root); |
836 | return ERR_PTR(ret); | |
837 | } | |
838 | ||
839 | ret = btrfs_sysfs_add_root(root); | |
840 | if (ret) { | |
5f39d397 | 841 | free_extent_buffer(root->node); |
58176a96 JB |
842 | kfree(root->name); |
843 | kfree(root); | |
844 | return ERR_PTR(ret); | |
845 | } | |
4313b399 | 846 | root->in_sysfs = 1; |
0f7d52f4 CM |
847 | return root; |
848 | } | |
19c00ddc CM |
849 | #if 0 |
850 | static int add_hasher(struct btrfs_fs_info *info, char *type) { | |
851 | struct btrfs_hasher *hasher; | |
852 | ||
853 | 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 | |
04160088 CM |
867 | |
868 | static 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 | ||
887 | void 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 | ||
903 | static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) | |
904 | { | |
b248a415 | 905 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) |
04160088 | 906 | bdi_init(bdi); |
b248a415 | 907 | #endif |
83041add | 908 | bdi->ra_pages = default_backing_dev_info.ra_pages * 4; |
04160088 CM |
909 | 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 | ||
ce9adaa5 CM |
918 | static 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) | |
44b8bd7e | 958 | static void btrfs_end_io_csum(void *p) |
ce9adaa5 | 959 | #else |
44b8bd7e | 960 | static void btrfs_end_io_csum(struct work_struct *work) |
ce9adaa5 CM |
961 | #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; | |
22c59948 | 991 | if (end_io_wq->metadata && !bio_ready_for_csum(bio)) { |
ce9adaa5 CM |
992 | 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); | |
73f61b2a | 1006 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
b248a415 | 1007 | bio_endio(bio, bio->bi_size, error); |
73f61b2a | 1008 | #else |
ce9adaa5 | 1009 | bio_endio(bio, error); |
73f61b2a | 1010 | #endif |
ce9adaa5 CM |
1011 | } |
1012 | } | |
1013 | ||
44b8bd7e CM |
1014 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
1015 | static void btrfs_async_submit_work(void *p) | |
1016 | #else | |
1017 | static 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 | ||
8a4b83cc CM |
1047 | struct btrfs_root *open_ctree(struct super_block *sb, |
1048 | struct btrfs_fs_devices *fs_devices) | |
2e635a27 | 1049 | { |
db94535d CM |
1050 | u32 sectorsize; |
1051 | u32 nodesize; | |
1052 | u32 leafsize; | |
1053 | u32 blocksize; | |
87ee04eb | 1054 | u32 stripesize; |
e20d96d6 CM |
1055 | 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); | |
8790d502 | 1059 | struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info), |
e20d96d6 | 1060 | GFP_NOFS); |
0b86a832 CM |
1061 | 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); | |
eb60ceac | 1065 | int ret; |
e58ca020 | 1066 | int err = -EINVAL; |
2c90e5d6 | 1067 | struct btrfs_super_block *disk_super; |
8790d502 | 1068 | |
39279cc3 CM |
1069 | if (!extent_root || !tree_root || !fs_info) { |
1070 | err = -ENOMEM; | |
1071 | goto fail; | |
1072 | } | |
ce9adaa5 CM |
1073 | end_io_workqueue = create_workqueue("btrfs-end-io"); |
1074 | BUG_ON(!end_io_workqueue); | |
44b8bd7e | 1075 | async_submit_workqueue = create_workqueue("btrfs-async-submit"); |
ce9adaa5 | 1076 | |
0f7d52f4 | 1077 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
8fd17795 | 1078 | INIT_LIST_HEAD(&fs_info->trans_list); |
facda1e7 | 1079 | INIT_LIST_HEAD(&fs_info->dead_roots); |
19c00ddc | 1080 | INIT_LIST_HEAD(&fs_info->hashers); |
ce9adaa5 | 1081 | INIT_LIST_HEAD(&fs_info->end_io_work_list); |
44b8bd7e | 1082 | INIT_LIST_HEAD(&fs_info->async_submit_work_list); |
19c00ddc | 1083 | spin_lock_init(&fs_info->hash_lock); |
ce9adaa5 | 1084 | spin_lock_init(&fs_info->end_io_work_lock); |
44b8bd7e | 1085 | spin_lock_init(&fs_info->async_submit_work_lock); |
1832a6d5 | 1086 | spin_lock_init(&fs_info->delalloc_lock); |
cee36a03 | 1087 | spin_lock_init(&fs_info->new_trans_lock); |
19c00ddc | 1088 | |
58176a96 | 1089 | init_completion(&fs_info->kobj_unregister); |
f2984462 | 1090 | sb_set_blocksize(sb, BTRFS_SUPER_INFO_SIZE); |
9f5fae2f CM |
1091 | fs_info->tree_root = tree_root; |
1092 | fs_info->extent_root = extent_root; | |
0b86a832 CM |
1093 | fs_info->chunk_root = chunk_root; |
1094 | fs_info->dev_root = dev_root; | |
8a4b83cc | 1095 | fs_info->fs_devices = fs_devices; |
0b86a832 | 1096 | INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); |
6324fbf3 | 1097 | INIT_LIST_HEAD(&fs_info->space_info); |
0b86a832 | 1098 | btrfs_mapping_init(&fs_info->mapping_tree); |
e20d96d6 | 1099 | fs_info->sb = sb; |
c59f8951 | 1100 | fs_info->max_extent = (u64)-1; |
6f568d35 | 1101 | fs_info->max_inline = 8192 * 1024; |
04160088 | 1102 | setup_bdi(fs_info, &fs_info->bdi); |
d98237b3 CM |
1103 | fs_info->btree_inode = new_inode(sb); |
1104 | fs_info->btree_inode->i_ino = 1; | |
2c90e5d6 | 1105 | fs_info->btree_inode->i_nlink = 1; |
d98237b3 CM |
1106 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
1107 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; | |
04160088 CM |
1108 | fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi; |
1109 | ||
d1310b2e | 1110 | extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, |
5f39d397 CM |
1111 | fs_info->btree_inode->i_mapping, |
1112 | GFP_NOFS); | |
d1310b2e CM |
1113 | extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree, |
1114 | GFP_NOFS); | |
1115 | ||
1116 | BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; | |
0da5468f | 1117 | |
d1310b2e | 1118 | extent_io_tree_init(&fs_info->free_space_cache, |
f510cfec | 1119 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
d1310b2e | 1120 | extent_io_tree_init(&fs_info->block_group_cache, |
96b5179d | 1121 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
d1310b2e | 1122 | extent_io_tree_init(&fs_info->pinned_extents, |
1a5bc167 | 1123 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
d1310b2e | 1124 | extent_io_tree_init(&fs_info->pending_del, |
1a5bc167 | 1125 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
d1310b2e | 1126 | extent_io_tree_init(&fs_info->extent_ins, |
1a5bc167 | 1127 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
e66f709b | 1128 | fs_info->do_barriers = 1; |
e18e4809 | 1129 | |
6da6abae | 1130 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
b248a415 | 1131 | INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum, fs_info); |
44b8bd7e CM |
1132 | INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work, |
1133 | fs_info); | |
6da6abae CM |
1134 | INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info); |
1135 | #else | |
b248a415 | 1136 | INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum); |
44b8bd7e | 1137 | INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work); |
08607c1b | 1138 | INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner); |
6da6abae | 1139 | #endif |
0f7d52f4 CM |
1140 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
1141 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, | |
1142 | sizeof(struct btrfs_key)); | |
22b0ebda | 1143 | insert_inode_hash(fs_info->btree_inode); |
d98237b3 | 1144 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
39279cc3 | 1145 | |
79154b1b | 1146 | mutex_init(&fs_info->trans_mutex); |
d561c025 | 1147 | mutex_init(&fs_info->fs_mutex); |
3768f368 | 1148 | |
19c00ddc CM |
1149 | #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 | |
0b86a832 | 1157 | __setup_root(4096, 4096, 4096, 4096, tree_root, |
2c90e5d6 | 1158 | fs_info, BTRFS_ROOT_TREE_OBJECTID); |
7eccb903 | 1159 | |
2c90e5d6 | 1160 | fs_info->sb_buffer = read_tree_block(tree_root, |
db94535d | 1161 | BTRFS_SUPER_INFO_OFFSET, |
0b86a832 | 1162 | 4096); |
d98237b3 | 1163 | |
0f7d52f4 | 1164 | if (!fs_info->sb_buffer) |
39279cc3 | 1165 | goto fail_iput; |
39279cc3 | 1166 | |
5f39d397 CM |
1167 | 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); | |
0b86a832 | 1173 | |
5f39d397 | 1174 | disk_super = &fs_info->super_copy; |
0f7d52f4 | 1175 | if (!btrfs_super_root(disk_super)) |
39279cc3 | 1176 | goto fail_sb_buffer; |
0f7d52f4 | 1177 | |
8a4b83cc CM |
1178 | 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 | } | |
db94535d CM |
1184 | nodesize = btrfs_super_nodesize(disk_super); |
1185 | leafsize = btrfs_super_leafsize(disk_super); | |
1186 | sectorsize = btrfs_super_sectorsize(disk_super); | |
87ee04eb | 1187 | stripesize = btrfs_super_stripesize(disk_super); |
db94535d CM |
1188 | tree_root->nodesize = nodesize; |
1189 | tree_root->leafsize = leafsize; | |
1190 | tree_root->sectorsize = sectorsize; | |
87ee04eb | 1191 | tree_root->stripesize = stripesize; |
ff79f819 | 1192 | sb_set_blocksize(sb, sectorsize); |
db94535d | 1193 | |
8352d8a4 | 1194 | i_size_write(fs_info->btree_inode, |
db94535d | 1195 | btrfs_super_total_bytes(disk_super)); |
8352d8a4 | 1196 | |
39279cc3 CM |
1197 | 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 | } | |
19c00ddc | 1202 | |
0b86a832 | 1203 | mutex_lock(&fs_info->fs_mutex); |
0d81ba5d | 1204 | |
0b86a832 CM |
1205 | 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 | ||
e17cade2 CM |
1219 | 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 | ||
0b86a832 CM |
1223 | ret = btrfs_read_chunk_tree(chunk_root); |
1224 | BUG_ON(ret); | |
1225 | ||
db94535d CM |
1226 | blocksize = btrfs_level_size(tree_root, |
1227 | btrfs_super_root_level(disk_super)); | |
19c00ddc | 1228 | |
0b86a832 | 1229 | |
e20d96d6 | 1230 | tree_root->node = read_tree_block(tree_root, |
db94535d CM |
1231 | btrfs_super_root(disk_super), |
1232 | blocksize); | |
39279cc3 CM |
1233 | if (!tree_root->node) |
1234 | goto fail_sb_buffer; | |
3768f368 | 1235 | |
db94535d CM |
1236 | |
1237 | ret = find_and_setup_root(tree_root, fs_info, | |
e20d96d6 | 1238 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
0b86a832 | 1239 | if (ret) |
39279cc3 | 1240 | goto fail_tree_root; |
0b86a832 CM |
1241 | 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; | |
3768f368 | 1249 | |
9078a3e1 CM |
1250 | btrfs_read_block_groups(extent_root); |
1251 | ||
0f7d52f4 | 1252 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
d18a2c44 CM |
1253 | 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 | ||
5be6f7f1 | 1257 | mutex_unlock(&fs_info->fs_mutex); |
0f7d52f4 | 1258 | return tree_root; |
39279cc3 | 1259 | |
0b86a832 CM |
1260 | fail_extent_root: |
1261 | free_extent_buffer(extent_root->node); | |
39279cc3 | 1262 | fail_tree_root: |
0b86a832 | 1263 | mutex_unlock(&fs_info->fs_mutex); |
5f39d397 | 1264 | free_extent_buffer(tree_root->node); |
39279cc3 | 1265 | fail_sb_buffer: |
5f39d397 | 1266 | free_extent_buffer(fs_info->sb_buffer); |
2d2ae547 | 1267 | extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree); |
39279cc3 CM |
1268 | fail_iput: |
1269 | iput(fs_info->btree_inode); | |
1270 | fail: | |
8a4b83cc | 1271 | close_all_devices(fs_info); |
39279cc3 CM |
1272 | kfree(extent_root); |
1273 | kfree(tree_root); | |
b248a415 | 1274 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) |
2d2ae547 | 1275 | bdi_destroy(&fs_info->bdi); |
b248a415 | 1276 | #endif |
39279cc3 CM |
1277 | kfree(fs_info); |
1278 | return ERR_PTR(err); | |
eb60ceac CM |
1279 | } |
1280 | ||
f2984462 CM |
1281 | static 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 | ||
1300 | int 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), | |
e17cade2 | 1327 | BTRFS_UUID_SIZE); |
f2984462 CM |
1328 | |
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 | ||
e089f05c | 1389 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
79154b1b | 1390 | *root) |
eb60ceac | 1391 | { |
e66f709b | 1392 | int ret; |
5f39d397 | 1393 | |
f2984462 CM |
1394 | ret = write_all_supers(root); |
1395 | #if 0 | |
21ad10cf CM |
1396 | if (!btrfs_test_opt(root, NOBARRIER)) |
1397 | blkdev_issue_flush(sb->s_bdev, NULL); | |
d1310b2e | 1398 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super); |
5f39d397 CM |
1399 | ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping, |
1400 | super->start, super->len); | |
21ad10cf CM |
1401 | if (!btrfs_test_opt(root, NOBARRIER)) |
1402 | blkdev_issue_flush(sb->s_bdev, NULL); | |
f2984462 | 1403 | #endif |
5f39d397 | 1404 | return ret; |
cfaa7295 CM |
1405 | } |
1406 | ||
5eda7b5e | 1407 | int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) |
2619ba1f CM |
1408 | { |
1409 | radix_tree_delete(&fs_info->fs_roots_radix, | |
1410 | (unsigned long)root->root_key.objectid); | |
b99aa6cb CM |
1411 | if (root->in_sysfs) |
1412 | btrfs_sysfs_del_root(root); | |
2619ba1f CM |
1413 | if (root->inode) |
1414 | iput(root->inode); | |
1415 | if (root->node) | |
5f39d397 | 1416 | free_extent_buffer(root->node); |
2619ba1f | 1417 | if (root->commit_root) |
5f39d397 | 1418 | free_extent_buffer(root->commit_root); |
58176a96 JB |
1419 | if (root->name) |
1420 | kfree(root->name); | |
2619ba1f CM |
1421 | kfree(root); |
1422 | return 0; | |
1423 | } | |
1424 | ||
35b7e476 | 1425 | static int del_fs_roots(struct btrfs_fs_info *fs_info) |
0f7d52f4 CM |
1426 | { |
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; | |
2619ba1f | 1437 | for (i = 0; i < ret; i++) |
5eda7b5e | 1438 | btrfs_free_fs_root(fs_info, gang[i]); |
0f7d52f4 CM |
1439 | } |
1440 | return 0; | |
1441 | } | |
b4100d64 | 1442 | |
e20d96d6 | 1443 | int close_ctree(struct btrfs_root *root) |
cfaa7295 | 1444 | { |
3768f368 | 1445 | int ret; |
e089f05c | 1446 | struct btrfs_trans_handle *trans; |
0f7d52f4 | 1447 | struct btrfs_fs_info *fs_info = root->fs_info; |
e089f05c | 1448 | |
facda1e7 | 1449 | fs_info->closing = 1; |
08607c1b | 1450 | btrfs_transaction_flush_work(root); |
0f7d52f4 | 1451 | mutex_lock(&fs_info->fs_mutex); |
6702ed49 | 1452 | btrfs_defrag_dirty_roots(root->fs_info); |
79154b1b | 1453 | trans = btrfs_start_transaction(root, 1); |
54aa1f4d | 1454 | ret = btrfs_commit_transaction(trans, root); |
79154b1b CM |
1455 | /* 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); | |
3768f368 | 1459 | BUG_ON(ret); |
79154b1b | 1460 | write_ctree_super(NULL, root); |
0f7d52f4 CM |
1461 | mutex_unlock(&fs_info->fs_mutex); |
1462 | ||
b0c68f8b CM |
1463 | if (fs_info->delalloc_bytes) { |
1464 | printk("btrfs: at unmount delalloc count %Lu\n", | |
1465 | fs_info->delalloc_bytes); | |
1466 | } | |
0f7d52f4 | 1467 | if (fs_info->extent_root->node) |
5f39d397 | 1468 | free_extent_buffer(fs_info->extent_root->node); |
f510cfec | 1469 | |
0f7d52f4 | 1470 | if (fs_info->tree_root->node) |
5f39d397 | 1471 | free_extent_buffer(fs_info->tree_root->node); |
f510cfec | 1472 | |
0b86a832 CM |
1473 | 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 | ||
5f39d397 | 1479 | free_extent_buffer(fs_info->sb_buffer); |
7eccb903 | 1480 | |
9078a3e1 | 1481 | btrfs_free_block_groups(root->fs_info); |
0f7d52f4 | 1482 | del_fs_roots(fs_info); |
d10c5f31 CM |
1483 | |
1484 | filemap_write_and_wait(fs_info->btree_inode->i_mapping); | |
1485 | ||
d1310b2e CM |
1486 | 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); | |
d10c5f31 | 1492 | |
db94535d | 1493 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); |
ce9adaa5 CM |
1494 | flush_workqueue(end_io_workqueue); |
1495 | destroy_workqueue(end_io_workqueue); | |
d10c5f31 | 1496 | |
44b8bd7e CM |
1497 | flush_workqueue(async_submit_workqueue); |
1498 | destroy_workqueue(async_submit_workqueue); | |
1499 | ||
db94535d | 1500 | iput(fs_info->btree_inode); |
19c00ddc CM |
1501 | #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 | |
0b86a832 CM |
1511 | close_all_devices(fs_info); |
1512 | btrfs_mapping_tree_free(&fs_info->mapping_tree); | |
b248a415 CM |
1513 | |
1514 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) | |
04160088 | 1515 | bdi_destroy(&fs_info->bdi); |
b248a415 | 1516 | #endif |
0b86a832 | 1517 | |
0f7d52f4 | 1518 | kfree(fs_info->extent_root); |
0f7d52f4 | 1519 | kfree(fs_info->tree_root); |
0b86a832 CM |
1520 | kfree(fs_info->chunk_root); |
1521 | kfree(fs_info->dev_root); | |
eb60ceac CM |
1522 | return 0; |
1523 | } | |
1524 | ||
5f39d397 CM |
1525 | int btrfs_buffer_uptodate(struct extent_buffer *buf) |
1526 | { | |
810191ff | 1527 | struct inode *btree_inode = buf->first_page->mapping->host; |
d1310b2e | 1528 | return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf); |
5f39d397 CM |
1529 | } |
1530 | ||
1531 | int btrfs_set_buffer_uptodate(struct extent_buffer *buf) | |
ccd467d6 | 1532 | { |
810191ff | 1533 | struct inode *btree_inode = buf->first_page->mapping->host; |
d1310b2e | 1534 | return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, |
5f39d397 CM |
1535 | buf); |
1536 | } | |
6702ed49 | 1537 | |
5f39d397 CM |
1538 | void btrfs_mark_buffer_dirty(struct extent_buffer *buf) |
1539 | { | |
810191ff | 1540 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
5f39d397 CM |
1541 | u64 transid = btrfs_header_generation(buf); |
1542 | struct inode *btree_inode = root->fs_info->btree_inode; | |
6702ed49 | 1543 | |
ccd467d6 CM |
1544 | if (transid != root->fs_info->generation) { |
1545 | printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n", | |
db94535d | 1546 | (unsigned long long)buf->start, |
ccd467d6 CM |
1547 | transid, root->fs_info->generation); |
1548 | WARN_ON(1); | |
1549 | } | |
d1310b2e | 1550 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf); |
eb60ceac CM |
1551 | } |
1552 | ||
e2008b61 CM |
1553 | void btrfs_throttle(struct btrfs_root *root) |
1554 | { | |
55c69072 CM |
1555 | struct backing_dev_info *bdi; |
1556 | ||
1557 | bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info; | |
04005cc7 CM |
1558 | if (root->fs_info->throttles && bdi_write_congested(bdi)) { |
1559 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18) | |
55c69072 | 1560 | congestion_wait(WRITE, HZ/20); |
04005cc7 CM |
1561 | #else |
1562 | blk_congestion_wait(WRITE, HZ/20); | |
1563 | #endif | |
1564 | } | |
e2008b61 CM |
1565 | } |
1566 | ||
d3c2fdcf | 1567 | void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) |
35b7e476 | 1568 | { |
d3c2fdcf | 1569 | balance_dirty_pages_ratelimited_nr( |
d7fc640e | 1570 | root->fs_info->btree_inode->i_mapping, 1); |
35b7e476 | 1571 | } |
6b80053d CM |
1572 | |
1573 | void btrfs_set_buffer_defrag(struct extent_buffer *buf) | |
1574 | { | |
810191ff | 1575 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1576 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1577 | set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start, |
6b80053d CM |
1578 | buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS); |
1579 | } | |
1580 | ||
1581 | void btrfs_set_buffer_defrag_done(struct extent_buffer *buf) | |
1582 | { | |
810191ff | 1583 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1584 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1585 | set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start, |
6b80053d CM |
1586 | buf->start + buf->len - 1, EXTENT_DEFRAG_DONE, |
1587 | GFP_NOFS); | |
1588 | } | |
1589 | ||
1590 | int btrfs_buffer_defrag(struct extent_buffer *buf) | |
1591 | { | |
810191ff | 1592 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1593 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1594 | return test_range_bit(&BTRFS_I(btree_inode)->io_tree, |
6b80053d CM |
1595 | buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0); |
1596 | } | |
1597 | ||
1598 | int btrfs_buffer_defrag_done(struct extent_buffer *buf) | |
1599 | { | |
810191ff | 1600 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1601 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1602 | return test_range_bit(&BTRFS_I(btree_inode)->io_tree, |
6b80053d CM |
1603 | buf->start, buf->start + buf->len - 1, |
1604 | EXTENT_DEFRAG_DONE, 0); | |
1605 | } | |
1606 | ||
1607 | int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf) | |
1608 | { | |
810191ff | 1609 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1610 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1611 | return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree, |
6b80053d CM |
1612 | buf->start, buf->start + buf->len - 1, |
1613 | EXTENT_DEFRAG_DONE, GFP_NOFS); | |
1614 | } | |
1615 | ||
1616 | int btrfs_clear_buffer_defrag(struct extent_buffer *buf) | |
1617 | { | |
810191ff | 1618 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1619 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1620 | return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree, |
6b80053d CM |
1621 | buf->start, buf->start + buf->len - 1, |
1622 | EXTENT_DEFRAG, GFP_NOFS); | |
1623 | } | |
1624 | ||
1625 | int btrfs_read_buffer(struct extent_buffer *buf) | |
1626 | { | |
810191ff | 1627 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
ce9adaa5 | 1628 | int ret; |
f188591e | 1629 | ret = btree_read_extent_buffer_pages(root, buf, 0); |
ce9adaa5 CM |
1630 | if (ret == 0) { |
1631 | buf->flags |= EXTENT_UPTODATE; | |
1632 | } | |
1633 | return ret; | |
6b80053d | 1634 | } |
0da5468f | 1635 | |
d1310b2e | 1636 | static struct extent_io_ops btree_extent_io_ops = { |
0da5468f | 1637 | .writepage_io_hook = btree_writepage_io_hook, |
ce9adaa5 | 1638 | .readpage_end_io_hook = btree_readpage_end_io_hook, |
0b86a832 | 1639 | .submit_bio_hook = btree_submit_bio_hook, |
239b14b3 CM |
1640 | /* note we're sharing with inode.c for the merge bio hook */ |
1641 | .merge_bio_hook = btrfs_merge_bio_hook, | |
0da5468f | 1642 | }; |