disk-io.c 66.1 KB
Newer Older
Chris Mason's avatar
Chris Mason committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * Copyright (C) 2007 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

Chris Mason's avatar
Chris Mason committed
19
#include <linux/fs.h>
20
#include <linux/blkdev.h>
Chris Mason's avatar
Chris Mason committed
21
#include <linux/scatterlist.h>
Chris Mason's avatar
Chris Mason committed
22
#include <linux/swap.h>
23
#include <linux/radix-tree.h>
24
#include <linux/writeback.h>
25
#include <linux/buffer_head.h>
26
#include <linux/workqueue.h>
27
#include <linux/kthread.h>
Chris Mason's avatar
Chris Mason committed
28
#include <linux/freezer.h>
29
#include <linux/crc32c.h>
Chris Mason's avatar
Chris Mason committed
30
#include "compat.h"
31
32
#include "ctree.h"
#include "disk-io.h"
33
#include "transaction.h"
34
#include "btrfs_inode.h"
35
#include "volumes.h"
36
#include "print-tree.h"
37
#include "async-thread.h"
38
#include "locking.h"
39
#include "tree-log.h"
40
#include "free-space-cache.h"
41

42
static struct extent_io_ops btree_extent_io_ops;
43
static void end_workqueue_fn(struct btrfs_work *work);
44

Chris Mason's avatar
Chris Mason committed
45
46
47
48
49
/*
 * end_io_wq structs are used to do processing in task context when an IO is
 * complete.  This is used during reads to verify checksums, and it is used
 * by writes to insert metadata for new file extents after IO is complete.
 */
50
51
52
53
54
55
struct end_io_wq {
	struct bio *bio;
	bio_end_io_t *end_io;
	void *private;
	struct btrfs_fs_info *info;
	int error;
56
	int metadata;
57
	struct list_head list;
58
	struct btrfs_work work;
59
};
60

Chris Mason's avatar
Chris Mason committed
61
62
63
64
65
/*
 * async submit bios are used to offload expensive checksumming
 * onto the worker threads.  They checksum file and metadata bios
 * just before they are sent down the IO stack.
 */
66
67
68
69
struct async_submit_bio {
	struct inode *inode;
	struct bio *bio;
	struct list_head list;
70
71
	extent_submit_bio_hook_t *submit_bio_start;
	extent_submit_bio_hook_t *submit_bio_done;
72
73
	int rw;
	int mirror_num;
74
	unsigned long bio_flags;
75
	struct btrfs_work work;
76
77
};

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* These are used to set the lockdep class on the extent buffer locks.
 * The class is set by the readpage_end_io_hook after the buffer has
 * passed csum validation but before the pages are unlocked.
 *
 * The lockdep class is also set by btrfs_init_new_buffer on freshly
 * allocated blocks.
 *
 * The class is based on the level in the tree block, which allows lockdep
 * to know that lower nodes nest inside the locks of higher nodes.
 *
 * We also add a check to make sure the highest level of the tree is
 * the same as our lockdep setup here.  If BTRFS_MAX_LEVEL changes, this
 * code needs update as well.
 */
#ifdef CONFIG_DEBUG_LOCK_ALLOC
# if BTRFS_MAX_LEVEL != 8
#  error
# endif
static struct lock_class_key btrfs_eb_class[BTRFS_MAX_LEVEL + 1];
static const char *btrfs_eb_name[BTRFS_MAX_LEVEL + 1] = {
	/* leaf */
	"btrfs-extent-00",
	"btrfs-extent-01",
	"btrfs-extent-02",
	"btrfs-extent-03",
	"btrfs-extent-04",
	"btrfs-extent-05",
	"btrfs-extent-06",
	"btrfs-extent-07",
	/* highest possible level */
	"btrfs-extent-08",
};
#endif

Chris Mason's avatar
Chris Mason committed
112
113
114
115
/*
 * extents on the btree inode are pretty simple, there's one extent
 * that covers the entire device
 */
116
117
118
static struct extent_map *btree_get_extent(struct inode *inode,
		struct page *page, size_t page_offset, u64 start, u64 len,
		int create)
119
{
120
121
122
123
	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
	struct extent_map *em;
	int ret;

124
125
	spin_lock(&em_tree->lock);
	em = lookup_extent_mapping(em_tree, start, len);
126
127
128
129
	if (em) {
		em->bdev =
			BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
		spin_unlock(&em_tree->lock);
130
		goto out;
131
132
	}
	spin_unlock(&em_tree->lock);
133

134
135
136
137
138
139
	em = alloc_extent_map(GFP_NOFS);
	if (!em) {
		em = ERR_PTR(-ENOMEM);
		goto out;
	}
	em->start = 0;
140
	em->len = (u64)-1;
141
	em->block_len = (u64)-1;
142
	em->block_start = 0;
143
	em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
144
145

	spin_lock(&em_tree->lock);
146
147
	ret = add_extent_mapping(em_tree, em);
	if (ret == -EEXIST) {
148
149
150
		u64 failed_start = em->start;
		u64 failed_len = em->len;

151
		free_extent_map(em);
152
		em = lookup_extent_mapping(em_tree, start, len);
153
		if (em) {
154
			ret = 0;
155
156
157
		} else {
			em = lookup_extent_mapping(em_tree, failed_start,
						   failed_len);
158
			ret = -EIO;
159
		}
160
	} else if (ret) {
161
162
		free_extent_map(em);
		em = NULL;
163
	}
164
165
166
167
	spin_unlock(&em_tree->lock);

	if (ret)
		em = ERR_PTR(ret);
168
169
out:
	return em;
170
171
}

172
173
u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
{
174
	return crc32c(seed, data, len);
175
176
177
178
179
180
181
}

void btrfs_csum_final(u32 crc, char *result)
{
	*(__le32 *)result = ~cpu_to_le32(crc);
}

Chris Mason's avatar
Chris Mason committed
182
183
184
185
/*
 * compute the csum for a btree block, and either verify it or write it
 * into the csum field of the block.
 */
186
187
188
static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
			   int verify)
{
189
190
191
	u16 csum_size =
		btrfs_super_csum_size(&root->fs_info->super_copy);
	char *result = NULL;
192
193
194
195
196
197
198
199
200
	unsigned long len;
	unsigned long cur_len;
	unsigned long offset = BTRFS_CSUM_SIZE;
	char *map_token = NULL;
	char *kaddr;
	unsigned long map_start;
	unsigned long map_len;
	int err;
	u32 crc = ~(u32)0;
201
	unsigned long inline_result;
202
203

	len = buf->len - offset;
204
	while (len > 0) {
205
206
207
		err = map_private_extent_buffer(buf, offset, 32,
					&map_token, &kaddr,
					&map_start, &map_len, KM_USER0);
208
		if (err)
209
210
211
212
213
214
215
216
			return 1;
		cur_len = min(len, map_len - (offset - map_start));
		crc = btrfs_csum_data(root, kaddr + offset - map_start,
				      crc, cur_len);
		len -= cur_len;
		offset += cur_len;
		unmap_extent_buffer(buf, map_token, KM_USER0);
	}
217
218
219
220
221
222
223
224
	if (csum_size > sizeof(inline_result)) {
		result = kzalloc(csum_size * sizeof(char), GFP_NOFS);
		if (!result)
			return 1;
	} else {
		result = (char *)&inline_result;
	}

225
226
227
	btrfs_csum_final(crc, result);

	if (verify) {
228
		if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
229
230
			u32 val;
			u32 found = 0;
231
			memcpy(&found, result, csum_size);
232

233
			read_extent_buffer(buf, &val, 0, csum_size);
234
235
236
237
238
239
240
241
			if (printk_ratelimit()) {
				printk(KERN_INFO "btrfs: %s checksum verify "
				       "failed on %llu wanted %X found %X "
				       "level %d\n",
				       root->fs_info->sb->s_id,
				       (unsigned long long)buf->start, val, found,
				       btrfs_header_level(buf));
			}
242
243
			if (result != (char *)&inline_result)
				kfree(result);
244
245
246
			return 1;
		}
	} else {
247
		write_extent_buffer(buf, result, 0, csum_size);
248
	}
249
250
	if (result != (char *)&inline_result)
		kfree(result);
251
252
253
	return 0;
}

Chris Mason's avatar
Chris Mason committed
254
255
256
257
258
259
/*
 * we can't consider a given block up to date unless the transid of the
 * block matches the transid in the parent node's pointer.  This is how we
 * detect blocks that either didn't get written at all or got written
 * in the wrong place.
 */
260
261
262
263
264
265
266
267
268
269
270
271
272
273
static int verify_parent_transid(struct extent_io_tree *io_tree,
				 struct extent_buffer *eb, u64 parent_transid)
{
	int ret;

	if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
		return 0;

	lock_extent(io_tree, eb->start, eb->start + eb->len - 1, GFP_NOFS);
	if (extent_buffer_uptodate(io_tree, eb) &&
	    btrfs_header_generation(eb) == parent_transid) {
		ret = 0;
		goto out;
	}
274
275
276
277
278
279
280
	if (printk_ratelimit()) {
		printk("parent transid verify failed on %llu wanted %llu "
		       "found %llu\n",
		       (unsigned long long)eb->start,
		       (unsigned long long)parent_transid,
		       (unsigned long long)btrfs_header_generation(eb));
	}
281
282
	ret = 1;
	clear_extent_buffer_uptodate(io_tree, eb);
Chris Mason's avatar
Chris Mason committed
283
out:
284
285
286
287
288
	unlock_extent(io_tree, eb->start, eb->start + eb->len - 1,
		      GFP_NOFS);
	return ret;
}

Chris Mason's avatar
Chris Mason committed
289
290
291
292
/*
 * helper to read a given tree block, doing retries as required when
 * the checksums don't match and we have alternate mirrors to try.
 */
293
294
static int btree_read_extent_buffer_pages(struct btrfs_root *root,
					  struct extent_buffer *eb,
295
					  u64 start, u64 parent_transid)
296
297
298
299
300
301
302
303
304
305
{
	struct extent_io_tree *io_tree;
	int ret;
	int num_copies = 0;
	int mirror_num = 0;

	io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
	while (1) {
		ret = read_extent_buffer_pages(io_tree, eb, start, 1,
					       btree_get_extent, mirror_num);
306
307
		if (!ret &&
		    !verify_parent_transid(io_tree, eb, parent_transid))
308
			return ret;
309

310
311
		num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
					      eb->start, eb->len);
Chris Mason's avatar
Chris Mason committed
312
		if (num_copies == 1)
313
			return ret;
Chris Mason's avatar
Chris Mason committed
314

315
		mirror_num++;
Chris Mason's avatar
Chris Mason committed
316
		if (mirror_num > num_copies)
317
318
319
320
			return ret;
	}
	return -EIO;
}
321

Chris Mason's avatar
Chris Mason committed
322
/*
323
324
 * checksum a dirty tree block before IO.  This has extra checks to make sure
 * we only fill in the checksum field in the first page of a multi-page block
Chris Mason's avatar
Chris Mason committed
325
 */
326

327
static int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
328
{
329
	struct extent_io_tree *tree;
330
	u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
331
332
333
334
	u64 found_start;
	int found_level;
	unsigned long len;
	struct extent_buffer *eb;
335
336
	int ret;

337
	tree = &BTRFS_I(page->mapping->host)->io_tree;
338
339
340
341
342
343

	if (page->private == EXTENT_PAGE_PRIVATE)
		goto out;
	if (!page->private)
		goto out;
	len = page->private >> 2;
344
345
	WARN_ON(len == 0);

346
	eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
347
348
	ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE,
					     btrfs_header_generation(eb));
349
	BUG_ON(ret);
350
351
	found_start = btrfs_header_bytenr(eb);
	if (found_start != start) {
352
353
354
355
356
357
358
359
360
361
		WARN_ON(1);
		goto err;
	}
	if (eb->first_page != page) {
		WARN_ON(1);
		goto err;
	}
	if (!PageUptodate(page)) {
		WARN_ON(1);
		goto err;
362
363
	}
	found_level = btrfs_header_level(eb);
Chris Mason's avatar
Chris Mason committed
364

365
	csum_tree_block(root, eb, 0);
366
err:
367
368
369
370
371
	free_extent_buffer(eb);
out:
	return 0;
}

Yan Zheng's avatar
Yan Zheng committed
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
static int check_tree_block_fsid(struct btrfs_root *root,
				 struct extent_buffer *eb)
{
	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
	u8 fsid[BTRFS_UUID_SIZE];
	int ret = 1;

	read_extent_buffer(eb, fsid, (unsigned long)btrfs_header_fsid(eb),
			   BTRFS_FSID_SIZE);
	while (fs_devices) {
		if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) {
			ret = 0;
			break;
		}
		fs_devices = fs_devices->seed;
	}
	return ret;
}

391
392
393
394
395
396
397
398
399
#ifdef CONFIG_DEBUG_LOCK_ALLOC
void btrfs_set_buffer_lockdep_class(struct extent_buffer *eb, int level)
{
	lockdep_set_class_and_name(&eb->lock,
			   &btrfs_eb_class[level],
			   btrfs_eb_name[level]);
}
#endif

400
static int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
401
402
403
404
405
406
407
408
			       struct extent_state *state)
{
	struct extent_io_tree *tree;
	u64 found_start;
	int found_level;
	unsigned long len;
	struct extent_buffer *eb;
	struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
409
	int ret = 0;
410
411
412
413
414
415

	tree = &BTRFS_I(page->mapping->host)->io_tree;
	if (page->private == EXTENT_PAGE_PRIVATE)
		goto out;
	if (!page->private)
		goto out;
416

417
	len = page->private >> 2;
418
419
	WARN_ON(len == 0);

420
	eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
421

422
	found_start = btrfs_header_bytenr(eb);
423
	if (found_start != start) {
424
425
426
427
428
429
		if (printk_ratelimit()) {
			printk(KERN_INFO "btrfs bad tree block start "
			       "%llu %llu\n",
			       (unsigned long long)found_start,
			       (unsigned long long)eb->start);
		}
430
		ret = -EIO;
431
432
433
		goto err;
	}
	if (eb->first_page != page) {
434
435
		printk(KERN_INFO "btrfs bad first page %lu %lu\n",
		       eb->first_page->index, page->index);
436
		WARN_ON(1);
437
		ret = -EIO;
438
439
		goto err;
	}
Yan Zheng's avatar
Yan Zheng committed
440
	if (check_tree_block_fsid(root, eb)) {
441
442
443
444
		if (printk_ratelimit()) {
			printk(KERN_INFO "btrfs bad fsid on block %llu\n",
			       (unsigned long long)eb->start);
		}
445
446
447
		ret = -EIO;
		goto err;
	}
448
449
	found_level = btrfs_header_level(eb);

450
451
	btrfs_set_buffer_lockdep_class(eb, found_level);

452
	ret = csum_tree_block(root, eb, 1);
453
454
	if (ret)
		ret = -EIO;
455
456
457
458
459
460

	end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
	end = eb->start + end - 1;
err:
	free_extent_buffer(eb);
out:
461
	return ret;
462
463
464
465
466
467
468
469
470
}

static void end_workqueue_bio(struct bio *bio, int err)
{
	struct end_io_wq *end_io_wq = bio->bi_private;
	struct btrfs_fs_info *fs_info;

	fs_info = end_io_wq->info;
	end_io_wq->error = err;
471
472
	end_io_wq->work.func = end_workqueue_fn;
	end_io_wq->work.flags = 0;
473
474

	if (bio->bi_rw & (1 << BIO_RW)) {
475
476
477
478
479
480
		if (end_io_wq->metadata)
			btrfs_queue_worker(&fs_info->endio_meta_write_workers,
					   &end_io_wq->work);
		else
			btrfs_queue_worker(&fs_info->endio_write_workers,
					   &end_io_wq->work);
481
482
483
484
485
486
487
488
	} else {
		if (end_io_wq->metadata)
			btrfs_queue_worker(&fs_info->endio_meta_workers,
					   &end_io_wq->work);
		else
			btrfs_queue_worker(&fs_info->endio_workers,
					   &end_io_wq->work);
	}
489
490
}

491
492
int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
			int metadata)
493
{
494
495
496
497
498
499
500
	struct end_io_wq *end_io_wq;
	end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
	if (!end_io_wq)
		return -ENOMEM;

	end_io_wq->private = bio->bi_private;
	end_io_wq->end_io = bio->bi_end_io;
501
	end_io_wq->info = info;
502
503
	end_io_wq->error = 0;
	end_io_wq->bio = bio;
504
	end_io_wq->metadata = metadata;
505
506
507

	bio->bi_private = end_io_wq;
	bio->bi_end_io = end_workqueue_bio;
508
509
510
	return 0;
}

511
unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info)
512
{
513
514
515
516
517
	unsigned long limit = min_t(unsigned long,
				    info->workers.max_workers,
				    info->fs_devices->open_devices);
	return 256 * limit;
}
518

519
520
int btrfs_congested_async(struct btrfs_fs_info *info, int iodone)
{
521
522
	return atomic_read(&info->nr_async_bios) >
		btrfs_async_submit_limit(info);
523
524
}

525
526
527
528
529
530
531
532
533
534
535
536
static void run_one_async_start(struct btrfs_work *work)
{
	struct btrfs_fs_info *fs_info;
	struct async_submit_bio *async;

	async = container_of(work, struct  async_submit_bio, work);
	fs_info = BTRFS_I(async->inode)->root->fs_info;
	async->submit_bio_start(async->inode, async->rw, async->bio,
			       async->mirror_num, async->bio_flags);
}

static void run_one_async_done(struct btrfs_work *work)
537
538
539
{
	struct btrfs_fs_info *fs_info;
	struct async_submit_bio *async;
540
	int limit;
541
542
543

	async = container_of(work, struct  async_submit_bio, work);
	fs_info = BTRFS_I(async->inode)->root->fs_info;
544

545
	limit = btrfs_async_submit_limit(fs_info);
546
547
	limit = limit * 2 / 3;

548
	atomic_dec(&fs_info->nr_async_submits);
549

550
551
	if (atomic_read(&fs_info->nr_async_submits) < limit &&
	    waitqueue_active(&fs_info->async_submit_wait))
552
553
		wake_up(&fs_info->async_submit_wait);

554
	async->submit_bio_done(async->inode, async->rw, async->bio,
555
			       async->mirror_num, async->bio_flags);
556
557
558
559
560
561
562
}

static void run_one_async_free(struct btrfs_work *work)
{
	struct async_submit_bio *async;

	async = container_of(work, struct  async_submit_bio, work);
563
564
565
	kfree(async);
}

566
567
int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
			int rw, struct bio *bio, int mirror_num,
568
			unsigned long bio_flags,
569
570
			extent_submit_bio_hook_t *submit_bio_start,
			extent_submit_bio_hook_t *submit_bio_done)
571
572
573
574
575
576
577
578
579
580
581
{
	struct async_submit_bio *async;

	async = kmalloc(sizeof(*async), GFP_NOFS);
	if (!async)
		return -ENOMEM;

	async->inode = inode;
	async->rw = rw;
	async->bio = bio;
	async->mirror_num = mirror_num;
582
583
584
585
586
587
588
	async->submit_bio_start = submit_bio_start;
	async->submit_bio_done = submit_bio_done;

	async->work.func = run_one_async_start;
	async->work.ordered_func = run_one_async_done;
	async->work.ordered_free = run_one_async_free;

589
	async->work.flags = 0;
590
	async->bio_flags = bio_flags;
591

592
	atomic_inc(&fs_info->nr_async_submits);
593
594
595
596

	if (rw & (1 << BIO_RW_SYNCIO))
		btrfs_set_work_high_prio(&async->work);

597
	btrfs_queue_worker(&fs_info->workers, &async->work);
598

599
	while (atomic_read(&fs_info->async_submit_draining) &&
600
601
602
603
604
	      atomic_read(&fs_info->nr_async_submits)) {
		wait_event(fs_info->async_submit_wait,
			   (atomic_read(&fs_info->nr_async_submits) == 0));
	}

605
606
607
	return 0;
}

608
609
610
611
612
613
614
static int btree_csum_one_bio(struct bio *bio)
{
	struct bio_vec *bvec = bio->bi_io_vec;
	int bio_index = 0;
	struct btrfs_root *root;

	WARN_ON(bio->bi_vcnt <= 0);
615
	while (bio_index < bio->bi_vcnt) {
616
617
618
619
620
621
622
623
		root = BTRFS_I(bvec->bv_page->mapping->host)->root;
		csum_dirty_buffer(root, bvec->bv_page);
		bio_index++;
		bvec++;
	}
	return 0;
}

624
625
626
static int __btree_submit_bio_start(struct inode *inode, int rw,
				    struct bio *bio, int mirror_num,
				    unsigned long bio_flags)
627
{
628
629
	/*
	 * when we're called for a write, we're already in the async
630
	 * submission context.  Just jump into btrfs_map_bio
631
	 */
632
633
634
	btree_csum_one_bio(bio);
	return 0;
}
635

636
637
638
static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
				 int mirror_num, unsigned long bio_flags)
{
639
	/*
640
641
	 * when we're called for a write, we're already in the async
	 * submission context.  Just jump into btrfs_map_bio
642
643
	 */
	return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1);
644
645
}

646
static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
647
				 int mirror_num, unsigned long bio_flags)
648
{
649
650
651
652
653
654
	int ret;

	ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info,
					  bio, 1);
	BUG_ON(ret);

655
	if (!(rw & (1 << BIO_RW))) {
656
657
658
659
660
		/*
		 * called for a read, do the setup so that checksum validation
		 * can happen in the async kernel threads
		 */
		return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio,
661
				     mirror_num, 0);
662
	}
663

664
665
666
667
	/*
	 * kthread helpers are used to submit writes so that checksumming
	 * can happen in parallel across all CPUs
	 */
668
	return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
669
				   inode, rw, bio, mirror_num, 0,
670
671
				   __btree_submit_bio_start,
				   __btree_submit_bio_done);
672
673
}

674
675
static int btree_writepage(struct page *page, struct writeback_control *wbc)
{
676
	struct extent_io_tree *tree;
677
678
679
680
	struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
	struct extent_buffer *eb;
	int was_dirty;

681
	tree = &BTRFS_I(page->mapping->host)->io_tree;
682
683
684
685
	if (!(current->flags & PF_MEMALLOC)) {
		return extent_write_full_page(tree, page,
					      btree_get_extent, wbc);
	}
686

687
688
689
690
691
692
693
694
695
696
	redirty_page_for_writepage(wbc, page);
	eb = btrfs_find_tree_block(root, page_offset(page),
				      PAGE_CACHE_SIZE);
	WARN_ON(!eb);

	was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
	if (!was_dirty) {
		spin_lock(&root->fs_info->delalloc_lock);
		root->fs_info->dirty_metadata_bytes += PAGE_CACHE_SIZE;
		spin_unlock(&root->fs_info->delalloc_lock);
697
	}
698
699
700
701
	free_extent_buffer(eb);

	unlock_page(page);
	return 0;
702
}
703
704
705
706

static int btree_writepages(struct address_space *mapping,
			    struct writeback_control *wbc)
{
707
708
	struct extent_io_tree *tree;
	tree = &BTRFS_I(mapping->host)->io_tree;
709
	if (wbc->sync_mode == WB_SYNC_NONE) {
710
		struct btrfs_root *root = BTRFS_I(mapping->host)->root;
711
		u64 num_dirty;
712
		unsigned long thresh = 32 * 1024 * 1024;
713
714
715
716

		if (wbc->for_kupdate)
			return 0;

717
718
		/* this is a bit racy, but that's ok */
		num_dirty = root->fs_info->dirty_metadata_bytes;
719
		if (num_dirty < thresh)
720
721
			return 0;
	}
722
723
724
	return extent_writepages(tree, mapping, btree_get_extent, wbc);
}

725
static int btree_readpage(struct file *file, struct page *page)
726
{
727
728
	struct extent_io_tree *tree;
	tree = &BTRFS_I(page->mapping->host)->io_tree;
729
730
	return extent_read_full_page(tree, page, btree_get_extent);
}
Chris Mason's avatar
Chris Mason committed
731

732
static int btree_releasepage(struct page *page, gfp_t gfp_flags)
733
{
734
735
	struct extent_io_tree *tree;
	struct extent_map_tree *map;
736
	int ret;
737

738
	if (PageWriteback(page) || PageDirty(page))
739
		return 0;
740

741
742
	tree = &BTRFS_I(page->mapping->host)->io_tree;
	map = &BTRFS_I(page->mapping->host)->extent_tree;
743

744
	ret = try_release_extent_state(map, tree, page, gfp_flags);
745
	if (!ret)
746
747
748
		return 0;

	ret = try_release_extent_buffer(tree, page);
749
750
751
752
753
	if (ret == 1) {
		ClearPagePrivate(page);
		set_page_private(page, 0);
		page_cache_release(page);
	}
754

755
756
757
	return ret;
}

758
static void btree_invalidatepage(struct page *page, unsigned long offset)
759
{
760
761
	struct extent_io_tree *tree;
	tree = &BTRFS_I(page->mapping->host)->io_tree;
762
763
	extent_invalidatepage(tree, page, offset);
	btree_releasepage(page, GFP_NOFS);
764
	if (PagePrivate(page)) {
765
766
		printk(KERN_WARNING "btrfs warning page private not zero "
		       "on page %llu\n", (unsigned long long)page_offset(page));
767
768
769
770
		ClearPagePrivate(page);
		set_page_private(page, 0);
		page_cache_release(page);
	}
771
772
773
774
775
}

static struct address_space_operations btree_aops = {
	.readpage	= btree_readpage,
	.writepage	= btree_writepage,
776
	.writepages	= btree_writepages,
777
778
	.releasepage	= btree_releasepage,
	.invalidatepage = btree_invalidatepage,
779
780
781
	.sync_page	= block_sync_page,
};

782
783
int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
			 u64 parent_transid)
Chris Mason's avatar
Chris Mason committed
784
{
785
786
	struct extent_buffer *buf = NULL;
	struct inode *btree_inode = root->fs_info->btree_inode;
787
	int ret = 0;
Chris Mason's avatar
Chris Mason committed
788

789
	buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
790
	if (!buf)
Chris Mason's avatar
Chris Mason committed
791
		return 0;
792
	read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
793
				 buf, 0, 0, btree_get_extent, 0);
794
	free_extent_buffer(buf);
795
	return ret;
Chris Mason's avatar
Chris Mason committed
796
797
}

798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
					    u64 bytenr, u32 blocksize)
{
	struct inode *btree_inode = root->fs_info->btree_inode;
	struct extent_buffer *eb;
	eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
				bytenr, blocksize, GFP_NOFS);
	return eb;
}

struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
						 u64 bytenr, u32 blocksize)
{
	struct inode *btree_inode = root->fs_info->btree_inode;
	struct extent_buffer *eb;

	eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
				 bytenr, blocksize, NULL, GFP_NOFS);
	return eb;
}


820
821
822
int btrfs_write_tree_block(struct extent_buffer *buf)
{
	return btrfs_fdatawrite_range(buf->first_page->mapping, buf->start,
823
				      buf->start + buf->len - 1, WB_SYNC_ALL);
824
825
826
827
828
}

int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
{
	return btrfs_wait_on_page_writeback_range(buf->first_page->mapping,
829
				  buf->start, buf->start + buf->len - 1);
830
831
}

832
struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
833
				      u32 blocksize, u64 parent_transid)
834
835
836
837
838
839
840
841
842
843
844
845
{
	struct extent_buffer *buf = NULL;
	struct inode *btree_inode = root->fs_info->btree_inode;
	struct extent_io_tree *io_tree;
	int ret;

	io_tree = &BTRFS_I(btree_inode)->io_tree;

	buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
	if (!buf)
		return NULL;

846
	ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
847

848
	if (ret == 0)
849
		set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags);
850
	return buf;
851

852
853
}

854
int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
855
		     struct extent_buffer *buf)
856
{
857
	struct inode *btree_inode = root->fs_info->btree_inode;
858
	if (btrfs_header_generation(buf) ==
859
	    root->fs_info->running_transaction->transid) {
860
		btrfs_assert_tree_locked(buf);
861

862
863
864
865
866
867
868
869
		if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) {
			spin_lock(&root->fs_info->delalloc_lock);
			if (root->fs_info->dirty_metadata_bytes >= buf->len)
				root->fs_info->dirty_metadata_bytes -= buf->len;
			else
				WARN_ON(1);
			spin_unlock(&root->fs_info->delalloc_lock);
		}
870

871
872
		/* ugh, clear_extent_buffer_dirty needs to lock the page */
		btrfs_set_lock_blocking(buf);
873
		clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
874
					  buf);
875
	}
876
877
878
	return 0;
}

879
static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
880
			u32 stripesize, struct btrfs_root *root,
881
			struct btrfs_fs_info *fs_info,
Chris Mason's avatar
Chris Mason committed
882
			u64 objectid)
883
{
Chris Mason's avatar
Chris Mason committed
884
	root->node = NULL;
885
	root->commit_root = NULL;
886
887
888
	root->sectorsize = sectorsize;
	root->nodesize = nodesize;
	root->leafsize = leafsize;
889
	root->stripesize = stripesize;
890
	root->ref_cows = 0;
891
892
	root->track_dirty = 0;

893
	root->fs_info = fs_info;
894
895
	root->objectid = objectid;
	root->last_trans = 0;
Chris Mason's avatar
Chris Mason committed
896
897
	root->highest_inode = 0;
	root->last_inode_alloc = 0;
898
	root->name = NULL;
899
	root->in_sysfs = 0;
900
	root->inode_tree.rb_node = NULL;
901
902

	INIT_LIST_HEAD(&root->dirty_list);
903
	INIT_LIST_HEAD(&root->orphan_list);
904
	INIT_LIST_HEAD(&root->root_list);
905
	spin_lock_init(&root->node_lock);
906
	spin_lock_init(&root->list_lock);
907
	spin_lock_init(&root->inode_lock);
908
	mutex_init(&root->objectid_mutex);
909
	mutex_init(&root->log_mutex);
Yan Zheng's avatar
Yan Zheng committed
910
911
912
913
914
915
916
917
	init_waitqueue_head(&root->log_writer_wait);
	init_waitqueue_head(&root->log_commit_wait[0]);
	init_waitqueue_head(&root->log_commit_wait[1]);
	atomic_set(&root->log_commit[0], 0);
	atomic_set(&root->log_commit[1], 0);
	atomic_set(&root->log_writers, 0);
	root->log_batch = 0;
	root->log_transid = 0;
918
919
	extent_io_tree_init(&root->dirty_log_pages,
			     fs_info->btree_inode->i_mapping, GFP_NOFS);
920

921
922
	memset(&root->root_key, 0, sizeof(root->root_key));
	memset(&root->root_item, 0, sizeof(root->root_item));
923
	memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
924
	memset(&root->root_kobj, 0, sizeof(root->root_kobj));
925
	root->defrag_trans_start = fs_info->generation;
926
	init_completion(&root->kobj_unregister);
927
928
	root->defrag_running = 0;
	root->defrag_level = 0;
929
	root->root_key.objectid = objectid;
930
931
932
933
934
935
	root->anon_super.s_root = NULL;
	root->anon_super.s_dev = 0;
	INIT_LIST_HEAD(&root->anon_super.s_list);
	INIT_LIST_HEAD(&root->anon_super.s_instances);
	init_rwsem(&root->anon_super.s_umount);

936
937
938
	return 0;
}

939
static int find_and_setup_root(struct btrfs_root *tree_root,
940
941
			       struct btrfs_fs_info *fs_info,
			       u64 objectid,
Chris Mason's avatar
Chris Mason committed
942
			       struct btrfs_root *root)
943
944
{
	int ret;
945
	u32 blocksize;
946
	u64 generation;
947

948
	__setup_root(tree_root->nodesize, tree_root->leafsize,
949
950
		     tree_root->sectorsize, tree_root->stripesize,
		     root, fs_info, objectid);
951
952
953
954
	ret = btrfs_find_last_root(tree_root, objectid,
				   &root->root_item, &root->root_key);
	BUG_ON(ret);

955
	generation = btrfs_root_generation(&root->root_item);
956
957
	blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
	root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
958
				     blocksize, generation);
959
	root->commit_root = btrfs_root_node(root);
960
	BUG_ON(!root->node);
961
962
963
	return 0;
}

964
965
966
967
int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
			     struct btrfs_fs_info *fs_info)
{
	struct extent_buffer *eb;
968
969
970
	struct btrfs_root *log_root_tree = fs_info->log_root_tree;
	u64 start = 0;
	u64 end = 0;
971
972
	int ret;

973
	if (!log_root_tree)
974
975
		return 0;

976
	while (1) {
977
978
979
980
981
982
983
984
		ret = find_first_extent_bit(&log_root_tree->dirty_log_pages,
				    0, &start, &end, EXTENT_DIRTY);
		if (ret)
			break;

		clear_extent_dirty(&log_root_tree->dirty_log_pages,
				   start, end, GFP_NOFS);
	}
985
986
987
988
989
	eb = fs_info->log_root_tree->node;

	WARN_ON(btrfs_header_level(eb) != 0);
	WARN_ON(btrfs_header_nritems(eb) != 0);

990
991
	ret = btrfs_free_reserved_extent(fs_info->tree_root,
				eb->start, eb->len);
992
993
994
995
996
997
998
999
	BUG_ON(ret);

	free_extent_buffer(eb);
	kfree(fs_info->log_root_tree);
	fs_info->log_root_tree = NULL;
	return 0;
}

Yan Zheng's avatar
Yan Zheng committed
1000
static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,