volumes.c 91.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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.
 */
#include <linux/sched.h>
#include <linux/bio.h>
20
#include <linux/slab.h>
21
#include <linux/buffer_head.h>
22
#include <linux/blkdev.h>
23
#include <linux/random.h>
24
#include <linux/iocontext.h>
25
#include <linux/capability.h>
26
#include <asm/div64.h>
Chris Mason's avatar
Chris Mason committed
27
#include "compat.h"
28 29 30 31 32 33
#include "ctree.h"
#include "extent_map.h"
#include "disk-io.h"
#include "transaction.h"
#include "print-tree.h"
#include "volumes.h"
34
#include "async-thread.h"
35

Yan Zheng's avatar
Yan Zheng committed
36 37 38 39 40
static int init_first_rw_device(struct btrfs_trans_handle *trans,
				struct btrfs_root *root,
				struct btrfs_device *device);
static int btrfs_relocate_sys_chunks(struct btrfs_root *root);

41 42 43
static DEFINE_MUTEX(uuid_mutex);
static LIST_HEAD(fs_uuids);

44 45 46 47 48 49 50 51 52 53
static void lock_chunks(struct btrfs_root *root)
{
	mutex_lock(&root->fs_info->chunk_mutex);
}

static void unlock_chunks(struct btrfs_root *root)
{
	mutex_unlock(&root->fs_info->chunk_mutex);
}

Yan Zheng's avatar
Yan Zheng committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67
static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
{
	struct btrfs_device *device;
	WARN_ON(fs_devices->opened);
	while (!list_empty(&fs_devices->devices)) {
		device = list_entry(fs_devices->devices.next,
				    struct btrfs_device, dev_list);
		list_del(&device->dev_list);
		kfree(device->name);
		kfree(device);
	}
	kfree(fs_devices);
}

68 69 70 71
int btrfs_cleanup_fs_uuids(void)
{
	struct btrfs_fs_devices *fs_devices;

Yan Zheng's avatar
Yan Zheng committed
72 73 74 75
	while (!list_empty(&fs_uuids)) {
		fs_devices = list_entry(fs_uuids.next,
					struct btrfs_fs_devices, list);
		list_del(&fs_devices->list);
Yan Zheng's avatar
Yan Zheng committed
76
		free_fs_devices(fs_devices);
77 78 79 80
	}
	return 0;
}

81 82
static noinline struct btrfs_device *__find_device(struct list_head *head,
						   u64 devid, u8 *uuid)
83 84 85
{
	struct btrfs_device *dev;

86
	list_for_each_entry(dev, head, dev_list) {
87
		if (dev->devid == devid &&
88
		    (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
89
			return dev;
90
		}
91 92 93 94
	}
	return NULL;
}

95
static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
96 97 98
{
	struct btrfs_fs_devices *fs_devices;

99
	list_for_each_entry(fs_devices, &fs_uuids, list) {
100 101 102 103 104 105
		if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
			return fs_devices;
	}
	return NULL;
}

106 107 108 109 110 111 112 113 114 115 116 117 118 119
static void requeue_list(struct btrfs_pending_bios *pending_bios,
			struct bio *head, struct bio *tail)
{

	struct bio *old_head;

	old_head = pending_bios->head;
	pending_bios->head = head;
	if (pending_bios->tail)
		tail->bi_next = old_head;
	else
		pending_bios->tail = tail;
}

120 121 122 123 124 125 126 127 128 129 130
/*
 * we try to collect pending bios for a device so we don't get a large
 * number of procs sending bios down to the same device.  This greatly
 * improves the schedulers ability to collect and merge the bios.
 *
 * But, it also turns into a long list of bios to process and that is sure
 * to eventually make the worker thread block.  The solution here is to
 * make some progress and then put this work struct back at the end of
 * the list if the block device is congested.  This way, multiple devices
 * can make progress from a single worker thread.
 */
131
static noinline int run_scheduled_bios(struct btrfs_device *device)
132 133 134
{
	struct bio *pending;
	struct backing_dev_info *bdi;
135
	struct btrfs_fs_info *fs_info;
136
	struct btrfs_pending_bios *pending_bios;
137 138 139
	struct bio *tail;
	struct bio *cur;
	int again = 0;
140
	unsigned long num_run;
141
	unsigned long batch_run = 0;
142
	unsigned long limit;
143
	unsigned long last_waited = 0;
144
	int force_reg = 0;
145 146 147 148 149 150 151 152 153
	struct blk_plug plug;

	/*
	 * this function runs all the bios we've collected for
	 * a particular device.  We don't want to wander off to
	 * another device without first sending all of these down.
	 * So, setup a plug here and finish it off before we return
	 */
	blk_start_plug(&plug);
154

155
	bdi = blk_get_backing_dev_info(device->bdev);
156 157 158 159
	fs_info = device->dev_root->fs_info;
	limit = btrfs_async_submit_limit(fs_info);
	limit = limit * 2 / 3;

160 161 162
loop:
	spin_lock(&device->io_lock);

163
loop_lock:
164
	num_run = 0;
165

166 167 168 169 170
	/* take all the bios off the list at once and process them
	 * later on (without the lock held).  But, remember the
	 * tail and other pointers so the bios can be properly reinserted
	 * into the list if we hit congestion
	 */
171
	if (!force_reg && device->pending_sync_bios.head) {
172
		pending_bios = &device->pending_sync_bios;
173 174
		force_reg = 1;
	} else {
175
		pending_bios = &device->pending_bios;
176 177
		force_reg = 0;
	}
178 179 180

	pending = pending_bios->head;
	tail = pending_bios->tail;
181 182 183 184 185 186 187 188 189 190
	WARN_ON(pending && !tail);

	/*
	 * if pending was null this time around, no bios need processing
	 * at all and we can stop.  Otherwise it'll loop back up again
	 * and do an additional check so no bios are missed.
	 *
	 * device->running_pending is used to synchronize with the
	 * schedule_bio code.
	 */
191 192
	if (device->pending_sync_bios.head == NULL &&
	    device->pending_bios.head == NULL) {
193 194
		again = 0;
		device->running_pending = 0;
195 196 197
	} else {
		again = 1;
		device->running_pending = 1;
198
	}
199 200 201 202

	pending_bios->head = NULL;
	pending_bios->tail = NULL;

203 204
	spin_unlock(&device->io_lock);

205
	while (pending) {
206 207

		rmb();
208 209 210 211 212 213 214 215
		/* we want to work on both lists, but do more bios on the
		 * sync list than the regular list
		 */
		if ((num_run > 32 &&
		    pending_bios != &device->pending_sync_bios &&
		    device->pending_sync_bios.head) ||
		   (num_run > 64 && pending_bios == &device->pending_sync_bios &&
		    device->pending_bios.head)) {
216 217 218 219 220
			spin_lock(&device->io_lock);
			requeue_list(pending_bios, pending, tail);
			goto loop_lock;
		}

221 222 223
		cur = pending;
		pending = pending->bi_next;
		cur->bi_next = NULL;
224 225 226 227 228
		atomic_dec(&fs_info->nr_async_bios);

		if (atomic_read(&fs_info->nr_async_bios) < limit &&
		    waitqueue_active(&fs_info->async_submit_wait))
			wake_up(&fs_info->async_submit_wait);
229 230

		BUG_ON(atomic_read(&cur->bi_cnt) == 0);
231

232 233 234
		submit_bio(cur->bi_rw, cur);
		num_run++;
		batch_run++;
Jens Axboe's avatar
Jens Axboe committed
235
		if (need_resched())
236
			cond_resched();
237 238 239 240 241 242

		/*
		 * we made progress, there is more work to do and the bdi
		 * is now congested.  Back off and let other work structs
		 * run instead
		 */
Chris Mason's avatar
Chris Mason committed
243
		if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
244
		    fs_info->fs_devices->open_devices > 1) {
245
			struct io_context *ioc;
246

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
			ioc = current->io_context;

			/*
			 * the main goal here is that we don't want to
			 * block if we're going to be able to submit
			 * more requests without blocking.
			 *
			 * This code does two great things, it pokes into
			 * the elevator code from a filesystem _and_
			 * it makes assumptions about how batching works.
			 */
			if (ioc && ioc->nr_batch_requests > 0 &&
			    time_before(jiffies, ioc->last_waited + HZ/50UL) &&
			    (last_waited == 0 ||
			     ioc->last_waited == last_waited)) {
				/*
				 * we want to go through our batch of
				 * requests and stop.  So, we copy out
				 * the ioc->last_waited time and test
				 * against it before looping
				 */
				last_waited = ioc->last_waited;
Jens Axboe's avatar
Jens Axboe committed
269
				if (need_resched())
270
					cond_resched();
271 272
				continue;
			}
273
			spin_lock(&device->io_lock);
274
			requeue_list(pending_bios, pending, tail);
275
			device->running_pending = 1;
276 277 278 279 280 281

			spin_unlock(&device->io_lock);
			btrfs_requeue_work(&device->work);
			goto done;
		}
	}
282

283 284 285 286 287 288 289 290 291
	cond_resched();
	if (again)
		goto loop;

	spin_lock(&device->io_lock);
	if (device->pending_bios.head || device->pending_sync_bios.head)
		goto loop_lock;
	spin_unlock(&device->io_lock);

292
done:
293
	blk_finish_plug(&plug);
294 295 296
	return 0;
}

297
static void pending_bios_fn(struct btrfs_work *work)
298 299 300 301 302 303 304
{
	struct btrfs_device *device;

	device = container_of(work, struct btrfs_device, work);
	run_scheduled_bios(device);
}

305
static noinline int device_list_add(const char *path,
306 307 308 309 310 311
			   struct btrfs_super_block *disk_super,
			   u64 devid, struct btrfs_fs_devices **fs_devices_ret)
{
	struct btrfs_device *device;
	struct btrfs_fs_devices *fs_devices;
	u64 found_transid = btrfs_super_generation(disk_super);
312
	char *name;
313 314 315

	fs_devices = find_fsid(disk_super->fsid);
	if (!fs_devices) {
316
		fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
317 318 319
		if (!fs_devices)
			return -ENOMEM;
		INIT_LIST_HEAD(&fs_devices->devices);
320
		INIT_LIST_HEAD(&fs_devices->alloc_list);
321 322 323 324
		list_add(&fs_devices->list, &fs_uuids);
		memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
		fs_devices->latest_devid = devid;
		fs_devices->latest_trans = found_transid;
325
		mutex_init(&fs_devices->device_list_mutex);
326 327
		device = NULL;
	} else {
328 329
		device = __find_device(&fs_devices->devices, devid,
				       disk_super->dev_item.uuid);
330 331
	}
	if (!device) {
Yan Zheng's avatar
Yan Zheng committed
332 333 334
		if (fs_devices->opened)
			return -EBUSY;

335 336 337 338 339 340
		device = kzalloc(sizeof(*device), GFP_NOFS);
		if (!device) {
			/* we can safely leave the fs_devices entry around */
			return -ENOMEM;
		}
		device->devid = devid;
341
		device->work.func = pending_bios_fn;
342 343
		memcpy(device->uuid, disk_super->dev_item.uuid,
		       BTRFS_UUID_SIZE);
344
		spin_lock_init(&device->io_lock);
345 346 347 348 349
		device->name = kstrdup(path, GFP_NOFS);
		if (!device->name) {
			kfree(device);
			return -ENOMEM;
		}
Yan Zheng's avatar
Yan Zheng committed
350
		INIT_LIST_HEAD(&device->dev_alloc_list);
351 352

		mutex_lock(&fs_devices->device_list_mutex);
353
		list_add_rcu(&device->dev_list, &fs_devices->devices);
354 355
		mutex_unlock(&fs_devices->device_list_mutex);

Yan Zheng's avatar
Yan Zheng committed
356
		device->fs_devices = fs_devices;
357
		fs_devices->num_devices++;
358
	} else if (!device->name || strcmp(device->name, path)) {
359 360 361 362 363
		name = kstrdup(path, GFP_NOFS);
		if (!name)
			return -ENOMEM;
		kfree(device->name);
		device->name = name;
364 365 366 367
		if (device->missing) {
			fs_devices->missing_devices--;
			device->missing = 0;
		}
368 369 370 371 372 373 374 375 376 377
	}

	if (found_transid > fs_devices->latest_trans) {
		fs_devices->latest_devid = devid;
		fs_devices->latest_trans = found_transid;
	}
	*fs_devices_ret = fs_devices;
	return 0;
}

Yan Zheng's avatar
Yan Zheng committed
378 379 380 381 382 383 384 385 386 387 388 389 390
static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
{
	struct btrfs_fs_devices *fs_devices;
	struct btrfs_device *device;
	struct btrfs_device *orig_dev;

	fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
	if (!fs_devices)
		return ERR_PTR(-ENOMEM);

	INIT_LIST_HEAD(&fs_devices->devices);
	INIT_LIST_HEAD(&fs_devices->alloc_list);
	INIT_LIST_HEAD(&fs_devices->list);
391
	mutex_init(&fs_devices->device_list_mutex);
Yan Zheng's avatar
Yan Zheng committed
392 393 394 395
	fs_devices->latest_devid = orig->latest_devid;
	fs_devices->latest_trans = orig->latest_trans;
	memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));

396
	/* We have held the volume lock, it is safe to get the devices. */
Yan Zheng's avatar
Yan Zheng committed
397 398 399 400 401 402
	list_for_each_entry(orig_dev, &orig->devices, dev_list) {
		device = kzalloc(sizeof(*device), GFP_NOFS);
		if (!device)
			goto error;

		device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawall's avatar
Julia Lawall committed
403 404
		if (!device->name) {
			kfree(device);
Yan Zheng's avatar
Yan Zheng committed
405
			goto error;
Julia Lawall's avatar
Julia Lawall committed
406
		}
Yan Zheng's avatar
Yan Zheng committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

		device->devid = orig_dev->devid;
		device->work.func = pending_bios_fn;
		memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
		spin_lock_init(&device->io_lock);
		INIT_LIST_HEAD(&device->dev_list);
		INIT_LIST_HEAD(&device->dev_alloc_list);

		list_add(&device->dev_list, &fs_devices->devices);
		device->fs_devices = fs_devices;
		fs_devices->num_devices++;
	}
	return fs_devices;
error:
	free_fs_devices(fs_devices);
	return ERR_PTR(-ENOMEM);
}

425 426
int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
{
427
	struct btrfs_device *device, *next;
428 429 430

	mutex_lock(&uuid_mutex);
again:
431
	/* This is the initialized path, it is safe to release the devices. */
432
	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng's avatar
Yan Zheng committed
433 434 435 436
		if (device->in_fs_metadata)
			continue;

		if (device->bdev) {
437
			blkdev_put(device->bdev, device->mode);
Yan Zheng's avatar
Yan Zheng committed
438 439 440 441 442 443 444 445
			device->bdev = NULL;
			fs_devices->open_devices--;
		}
		if (device->writeable) {
			list_del_init(&device->dev_alloc_list);
			device->writeable = 0;
			fs_devices->rw_devices--;
		}
Yan Zheng's avatar
Yan Zheng committed
446 447 448 449
		list_del_init(&device->dev_list);
		fs_devices->num_devices--;
		kfree(device->name);
		kfree(device);
450
	}
Yan Zheng's avatar
Yan Zheng committed
451 452 453 454 455 456

	if (fs_devices->seed) {
		fs_devices = fs_devices->seed;
		goto again;
	}

457 458 459
	mutex_unlock(&uuid_mutex);
	return 0;
}
460

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
static void __free_device(struct work_struct *work)
{
	struct btrfs_device *device;

	device = container_of(work, struct btrfs_device, rcu_work);

	if (device->bdev)
		blkdev_put(device->bdev, device->mode);

	kfree(device->name);
	kfree(device);
}

static void free_device(struct rcu_head *head)
{
	struct btrfs_device *device;

	device = container_of(head, struct btrfs_device, rcu);

	INIT_WORK(&device->rcu_work, __free_device);
	schedule_work(&device->rcu_work);
}

Yan Zheng's avatar
Yan Zheng committed
484
static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
485 486
{
	struct btrfs_device *device;
Yan Zheng's avatar
Yan Zheng committed
487

Yan Zheng's avatar
Yan Zheng committed
488 489
	if (--fs_devices->opened > 0)
		return 0;
490

491
	mutex_lock(&fs_devices->device_list_mutex);
492
	list_for_each_entry(device, &fs_devices->devices, dev_list) {
493 494 495
		struct btrfs_device *new_device;

		if (device->bdev)
496
			fs_devices->open_devices--;
497

Yan Zheng's avatar
Yan Zheng committed
498 499 500 501 502
		if (device->writeable) {
			list_del_init(&device->dev_alloc_list);
			fs_devices->rw_devices--;
		}

503 504 505 506
		new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
		BUG_ON(!new_device);
		memcpy(new_device, device, sizeof(*new_device));
		new_device->name = kstrdup(device->name, GFP_NOFS);
507
		BUG_ON(device->name && !new_device->name);
508 509 510 511 512 513
		new_device->bdev = NULL;
		new_device->writeable = 0;
		new_device->in_fs_metadata = 0;
		list_replace_rcu(&device->dev_list, &new_device->dev_list);

		call_rcu(&device->rcu, free_device);
514
	}
515 516
	mutex_unlock(&fs_devices->device_list_mutex);

Yan Zheng's avatar
Yan Zheng committed
517 518
	WARN_ON(fs_devices->open_devices);
	WARN_ON(fs_devices->rw_devices);
Yan Zheng's avatar
Yan Zheng committed
519 520 521
	fs_devices->opened = 0;
	fs_devices->seeding = 0;

522 523 524
	return 0;
}

Yan Zheng's avatar
Yan Zheng committed
525 526
int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
{
Yan Zheng's avatar
Yan Zheng committed
527
	struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng's avatar
Yan Zheng committed
528 529 530 531
	int ret;

	mutex_lock(&uuid_mutex);
	ret = __btrfs_close_devices(fs_devices);
Yan Zheng's avatar
Yan Zheng committed
532 533 534 535
	if (!fs_devices->opened) {
		seed_devices = fs_devices->seed;
		fs_devices->seed = NULL;
	}
Yan Zheng's avatar
Yan Zheng committed
536
	mutex_unlock(&uuid_mutex);
Yan Zheng's avatar
Yan Zheng committed
537 538 539 540 541 542 543

	while (seed_devices) {
		fs_devices = seed_devices;
		seed_devices = fs_devices->seed;
		__btrfs_close_devices(fs_devices);
		free_fs_devices(fs_devices);
	}
Yan Zheng's avatar
Yan Zheng committed
544 545 546
	return ret;
}

Yan Zheng's avatar
Yan Zheng committed
547 548
static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
				fmode_t flags, void *holder)
549 550 551 552
{
	struct block_device *bdev;
	struct list_head *head = &fs_devices->devices;
	struct btrfs_device *device;
553 554 555 556 557 558
	struct block_device *latest_bdev = NULL;
	struct buffer_head *bh;
	struct btrfs_super_block *disk_super;
	u64 latest_devid = 0;
	u64 latest_transid = 0;
	u64 devid;
Yan Zheng's avatar
Yan Zheng committed
559
	int seeding = 1;
560
	int ret = 0;
561

562 563
	flags |= FMODE_EXCL;

564
	list_for_each_entry(device, head, dev_list) {
565 566
		if (device->bdev)
			continue;
567 568 569
		if (!device->name)
			continue;

570
		bdev = blkdev_get_by_path(device->name, flags, holder);
571
		if (IS_ERR(bdev)) {
572
			printk(KERN_INFO "open %s failed\n", device->name);
573
			goto error;
574
		}
575
		set_blocksize(bdev, 4096);
576

Yan Zheng's avatar
Yan Zheng committed
577
		bh = btrfs_read_dev_super(bdev);
578 579
		if (!bh) {
			ret = -EINVAL;
580
			goto error_close;
581
		}
582 583

		disk_super = (struct btrfs_super_block *)bh->b_data;
584
		devid = btrfs_stack_device_id(&disk_super->dev_item);
585 586 587
		if (devid != device->devid)
			goto error_brelse;

Yan Zheng's avatar
Yan Zheng committed
588 589 590 591 592 593
		if (memcmp(device->uuid, disk_super->dev_item.uuid,
			   BTRFS_UUID_SIZE))
			goto error_brelse;

		device->generation = btrfs_super_generation(disk_super);
		if (!latest_transid || device->generation > latest_transid) {
594
			latest_devid = devid;
Yan Zheng's avatar
Yan Zheng committed
595
			latest_transid = device->generation;
596 597 598
			latest_bdev = bdev;
		}

Yan Zheng's avatar
Yan Zheng committed
599 600 601 602 603 604 605
		if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
			device->writeable = 0;
		} else {
			device->writeable = !bdev_read_only(bdev);
			seeding = 0;
		}

606
		device->bdev = bdev;
607
		device->in_fs_metadata = 0;
608 609
		device->mode = flags;

Chris Mason's avatar
Chris Mason committed
610 611 612
		if (!blk_queue_nonrot(bdev_get_queue(bdev)))
			fs_devices->rotating = 1;

613
		fs_devices->open_devices++;
Yan Zheng's avatar
Yan Zheng committed
614 615 616 617 618
		if (device->writeable) {
			fs_devices->rw_devices++;
			list_add(&device->dev_alloc_list,
				 &fs_devices->alloc_list);
		}
619
		brelse(bh);
620
		continue;
621

622 623 624
error_brelse:
		brelse(bh);
error_close:
625
		blkdev_put(bdev, flags);
626 627
error:
		continue;
628
	}
629 630 631 632
	if (fs_devices->open_devices == 0) {
		ret = -EIO;
		goto out;
	}
Yan Zheng's avatar
Yan Zheng committed
633 634
	fs_devices->seeding = seeding;
	fs_devices->opened = 1;
635 636 637
	fs_devices->latest_bdev = latest_bdev;
	fs_devices->latest_devid = latest_devid;
	fs_devices->latest_trans = latest_transid;
Yan Zheng's avatar
Yan Zheng committed
638
	fs_devices->total_rw_bytes = 0;
639
out:
Yan Zheng's avatar
Yan Zheng committed
640 641 642 643
	return ret;
}

int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
644
		       fmode_t flags, void *holder)
Yan Zheng's avatar
Yan Zheng committed
645 646 647 648 649
{
	int ret;

	mutex_lock(&uuid_mutex);
	if (fs_devices->opened) {
Yan Zheng's avatar
Yan Zheng committed
650 651
		fs_devices->opened++;
		ret = 0;
Yan Zheng's avatar
Yan Zheng committed
652
	} else {
653
		ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng's avatar
Yan Zheng committed
654
	}
655 656 657 658
	mutex_unlock(&uuid_mutex);
	return ret;
}

659
int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
660 661 662 663 664 665 666
			  struct btrfs_fs_devices **fs_devices_ret)
{
	struct btrfs_super_block *disk_super;
	struct block_device *bdev;
	struct buffer_head *bh;
	int ret;
	u64 devid;
667
	u64 transid;
668 669 670

	mutex_lock(&uuid_mutex);

671 672
	flags |= FMODE_EXCL;
	bdev = blkdev_get_by_path(path, flags, holder);
673 674 675 676 677 678 679 680 681

	if (IS_ERR(bdev)) {
		ret = PTR_ERR(bdev);
		goto error;
	}

	ret = set_blocksize(bdev, 4096);
	if (ret)
		goto error_close;
Yan Zheng's avatar
Yan Zheng committed
682
	bh = btrfs_read_dev_super(bdev);
683
	if (!bh) {
684
		ret = -EINVAL;
685 686 687
		goto error_close;
	}
	disk_super = (struct btrfs_super_block *)bh->b_data;
688
	devid = btrfs_stack_device_id(&disk_super->dev_item);
689
	transid = btrfs_super_generation(disk_super);
690
	if (disk_super->label[0])
691
		printk(KERN_INFO "device label %s ", disk_super->label);
692 693
	else {
		/* FIXME, make a readl uuid parser */
694
		printk(KERN_INFO "device fsid %llx-%llx ",
695 696 697
		       *(unsigned long long *)disk_super->fsid,
		       *(unsigned long long *)(disk_super->fsid + 8));
	}
698
	printk(KERN_CONT "devid %llu transid %llu %s\n",
699
	       (unsigned long long)devid, (unsigned long long)transid, path);
700 701 702 703
	ret = device_list_add(path, disk_super, devid, fs_devices_ret);

	brelse(bh);
error_close:
704
	blkdev_put(bdev, flags);
705 706 707 708
error:
	mutex_unlock(&uuid_mutex);
	return ret;
}
709

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
/* helper to account the used device space in the range */
int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
				   u64 end, u64 *length)
{
	struct btrfs_key key;
	struct btrfs_root *root = device->dev_root;
	struct btrfs_dev_extent *dev_extent;
	struct btrfs_path *path;
	u64 extent_end;
	int ret;
	int slot;
	struct extent_buffer *l;

	*length = 0;

	if (start >= device->total_bytes)
		return 0;

	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;
	path->reada = 2;

	key.objectid = device->devid;
	key.offset = start;
	key.type = BTRFS_DEV_EXTENT_KEY;

	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
	if (ret < 0)
		goto out;
	if (ret > 0) {
		ret = btrfs_previous_item(root, path, key.objectid, key.type);
		if (ret < 0)
			goto out;
	}

	while (1) {
		l = path->nodes[0];
		slot = path->slots[0];
		if (slot >= btrfs_header_nritems(l)) {
			ret = btrfs_next_leaf(root, path);
			if (ret == 0)
				continue;
			if (ret < 0)
				goto out;

			break;
		}
		btrfs_item_key_to_cpu(l, &key, slot);

		if (key.objectid < device->devid)
			goto next;

		if (key.objectid > device->devid)
			break;

		if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
			goto next;

		dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
		extent_end = key.offset + btrfs_dev_extent_length(l,
								  dev_extent);
		if (key.offset <= start && extent_end > end) {
			*length = end - start + 1;
			break;
		} else if (key.offset <= start && extent_end > start)
			*length += extent_end - start;
		else if (key.offset > start && extent_end <= end)
			*length += extent_end - key.offset;
		else if (key.offset > start && key.offset <= end) {
			*length += end - key.offset + 1;
			break;
		} else if (key.offset > end)
			break;

next:
		path->slots[0]++;
	}
	ret = 0;
out:
	btrfs_free_path(path);
	return ret;
}

794
/*
795 796 797 798 799 800 801 802
 * find_free_dev_extent - find free space in the specified device
 * @trans:	transaction handler
 * @device:	the device which we search the free space in
 * @num_bytes:	the size of the free space that we need
 * @start:	store the start of the free space.
 * @len:	the size of the free space. that we find, or the size of the max
 * 		free space if we don't find suitable free space
 *
803 804 805
 * this uses a pretty simple search, the expectation is that it is
 * called very infrequently and that a given device has a small number
 * of extents
806 807 808 809 810 811 812 813
 *
 * @start is used to store the start of the free space if we find. But if we
 * don't find suitable free space, it will be used to store the start position
 * of the max free space.
 *
 * @len is used to store the size of the free space that we find.
 * But if we don't find suitable free space, it is used to store the size of
 * the max free space.
814
 */
815 816
int find_free_dev_extent(struct btrfs_trans_handle *trans,
			 struct btrfs_device *device, u64 num_bytes,
817
			 u64 *start, u64 *len)
818 819 820
{
	struct btrfs_key key;
	struct btrfs_root *root = device->dev_root;
821
	struct btrfs_dev_extent *dev_extent;
Yan Zheng's avatar
Yan Zheng committed
822
	struct btrfs_path *path;
823 824 825 826 827
	u64 hole_size;
	u64 max_hole_start;
	u64 max_hole_size;
	u64 extent_end;
	u64 search_start;
828 829
	u64 search_end = device->total_bytes;
	int ret;
830
	int slot;
831 832 833 834
	struct extent_buffer *l;

	/* FIXME use last free of some kind */

835 836 837
	/* we don't want to overwrite the superblock on the drive,
	 * so we make sure to start at an offset of at least 1MB
	 */
Arne Jansen's avatar
Arne Jansen committed
838
	search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
839

840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
	max_hole_start = search_start;
	max_hole_size = 0;

	if (search_start >= search_end) {
		ret = -ENOSPC;
		goto error;
	}

	path = btrfs_alloc_path();
	if (!path) {
		ret = -ENOMEM;
		goto error;
	}
	path->reada = 2;

855 856 857
	key.objectid = device->devid;
	key.offset = search_start;
	key.type = BTRFS_DEV_EXTENT_KEY;
858

859 860
	ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
	if (ret < 0)
861
		goto out;
862 863 864
	if (ret > 0) {
		ret = btrfs_previous_item(root, path, key.objectid, key.type);
		if (ret < 0)
865
			goto out;
866
	}
867

868 869 870 871 872 873 874 875
	while (1) {
		l = path->nodes[0];
		slot = path->slots[0];
		if (slot >= btrfs_header_nritems(l)) {
			ret = btrfs_next_leaf(root, path);
			if (ret == 0)
				continue;
			if (ret < 0)
876 877 878
				goto out;

			break;
879 880 881 882 883 884 885
		}
		btrfs_item_key_to_cpu(l, &key, slot);

		if (key.objectid < device->devid)
			goto next;

		if (key.objectid > device->devid)
886
			break;
887

888 889
		if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
			goto next;
890

891 892
		if (key.offset > search_start) {
			hole_size = key.offset - search_start;
893

894 895 896 897
			if (hole_size > max_hole_size) {
				max_hole_start = search_start;
				max_hole_size = hole_size;
			}
898

899 900 901 902 903 904 905 906 907 908 909 910
			/*
			 * If this free space is greater than which we need,
			 * it must be the max free space that we have found
			 * until now, so max_hole_start must point to the start
			 * of this free space and the length of this free space
			 * is stored in max_hole_size. Thus, we return
			 * max_hole_start and max_hole_size and go back to the
			 * caller.
			 */
			if (hole_size >= num_bytes) {
				ret = 0;
				goto out;
911 912 913 914
			}
		}

		dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
915 916 917 918
		extent_end = key.offset + btrfs_dev_extent_length(l,
								  dev_extent);
		if (extent_end > search_start)
			search_start = extent_end;
919 920 921 922 923
next:
		path->slots[0]++;
		cond_resched();
	}

924 925 926 927
	hole_size = search_end- search_start;
	if (hole_size > max_hole_size) {
		max_hole_start = search_start;
		max_hole_size = hole_size;
928 929
	}

930 931 932 933 934 935 936
	/* See above. */
	if (hole_size < num_bytes)
		ret = -ENOSPC;
	else
		ret = 0;

out:
Yan Zheng's avatar
Yan Zheng committed
937
	btrfs_free_path(path);
938 939
error:
	*start = max_hole_start;
940
	if (len)
941
		*len = max_hole_size;
942 943 944
	return ret;
}

945
static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
946 947 948 949 950 951 952
			  struct btrfs_device *device,
			  u64 start)
{
	int ret;
	struct btrfs_path *path;
	struct btrfs_root *root = device->dev_root;
	struct btrfs_key key;
953 954 955
	struct btrfs_key found_key;
	struct extent_buffer *leaf = NULL;
	struct btrfs_dev_extent *extent = NULL;
956 957 958 959 960 961 962 963 964 965

	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;

	key.objectid = device->devid;
	key.offset = start;
	key.type = BTRFS_DEV_EXTENT_KEY;

	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
966 967 968
	if (ret > 0) {
		ret = btrfs_previous_item(root, path, key.objectid,
					  BTRFS_DEV_EXTENT_KEY);
969 970
		if (ret)
			goto out;
971 972 973 974 975 976 977 978 979 980 981
		leaf = path->nodes[0];
		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
		extent = btrfs_item_ptr(leaf, path->slots[0],
					struct btrfs_dev_extent);
		BUG_ON(found_key.offset > start || found_key.offset +
		       btrfs_dev_extent_length(leaf, extent) < start);
	} else if (ret == 0) {
		leaf = path->nodes[0];
		extent = btrfs_item_ptr(leaf, path->slots[0],
					struct btrfs_dev_extent);
	}
982 983
	BUG_ON(ret);

984 985
	if (device->bytes_used > 0)
		device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
986 987
	ret = btrfs_del_item(trans, root, path);

988
out:
989 990 991 992
	btrfs_free_path(path);
	return ret;
}

Yan Zheng's avatar
Yan Zheng committed
993
int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
994
			   struct btrfs_device *device,
995
			   u64 chunk_tree, u64 chunk_objectid,
Yan Zheng's avatar
Yan Zheng committed
996
			   u64 chunk_offset, u64 start, u64 num_bytes)
997 998 999 1000 1001 1002 1003 1004
{
	int ret;
	struct btrfs_path *path;
	struct btrfs_root *root = device->dev_root;
	struct btrfs_dev_extent *extent;
	struct extent_buffer *leaf;
	struct btrfs_key key;

1005
	WARN_ON(!device->in_fs_metadata);
1006 1007 1008 1009 1010
	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;

	key.objectid = device->devid;
Yan Zheng's avatar
Yan Zheng committed
1011
	key.offset = start;
1012 1013 1014 1015 1016 1017 1018 1019
	key.type = BTRFS_DEV_EXTENT_KEY;
	ret = btrfs_insert_empty_item(trans, root, path, &key,
				      sizeof(*extent));
	BUG_ON(ret);

	leaf = path->nodes[0];
	extent = btrfs_item_ptr(leaf, path->slots[0],
				struct btrfs_dev_extent);
1020 1021 1022 1023 1024 1025 1026 1027
	btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
	btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
	btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);

	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
		    (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
		    BTRFS_UUID_SIZE);

1028 1029 1030 1031 1032 1033
	btrfs_set_dev_extent_length(leaf, extent, num_bytes);
	btrfs_mark_buffer_dirty(leaf);
	btrfs_free_path(path);
	return ret;
}

1034 1035
static noinline int find_next_chunk(struct btrfs_root *root,
				    u64 objectid, u64 *offset)
1036 1037 1038 1039
{
	struct btrfs_path *path;
	int ret;
	struct btrfs_key key;
1040
	struct btrfs_chunk *chunk;
1041 1042 1043 1044 1045
	struct btrfs_key found_key;

	path = btrfs_alloc_path();
	BUG_ON(!path);

1046
	key.objectid = objectid;
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
	key.offset = (u64)-1;
	key.type = BTRFS_CHUNK_ITEM_KEY;

	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
	if (ret < 0)
		goto error;

	BUG_ON(ret == 0);

	ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
	if (ret) {
1058
		*offset = 0;
1059 1060 1061
	} else {
		btrfs_item_key_to_cpu(path->nodes[0], &found_key,
				      path->slots[0]);
1062 1063 1064 1065 1066 1067 1068 1069
		if (found_key.objectid != objectid)
			*offset = 0;
		else {
			chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
					       struct btrfs_chunk);
			*offset = found_key.offset +
				btrfs_chunk_length(path->nodes[0], chunk);
		}
1070 1071 1072 1073 1074 1075 1076
	}
	ret = 0;
error:
	btrfs_free_path(path);
	return ret;
}

Yan Zheng's avatar
Yan Zheng committed
1077
static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1078 1079 1080 1081
{
	int ret;
	struct btrfs_key key;
	struct btrfs_key found_key;
Yan Zheng's avatar
Yan Zheng committed
1082 1083 1084 1085 1086 1087 1088
	struct btrfs_path *path;

	root = root->fs_info->chunk_root;

	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110

	key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
	key.type = BTRFS_DEV_ITEM_KEY;
	key.offset = (u64)-1;

	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
	if (ret < 0)
		goto error;

	BUG_ON(ret == 0);

	ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
				  BTRFS_DEV_ITEM_KEY);
	if (ret) {
		*objectid = 1;
	} else {
		btrfs_item_key_to_cpu(path->nodes[0], &found_key,
				      path->slots[0]);
		*objectid = found_key.offset + 1;
	}
	ret = 0;
error:
Yan Zheng's avatar
Yan Zheng committed
1111
	btrfs_free_path(path);
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
	return ret;
}

/*
 * the device information is stored in the chunk root
 * the btrfs_device struct should be fully filled in
 */
int btrfs_add_device(struct btrfs_trans_handle *trans,
		     struct btrfs_root *root,
		     struct btrfs_device *device)
{
	int ret;
	struct btrfs_path *path;
	struct btrfs_dev_item *dev_item;
	struct extent_buffer *leaf;
	struct btrfs_key key;
	unsigned long ptr;

	root = root->fs_info->chunk_root;

	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;

	key.objectid = BTRFS_DEV_ITEMS_OBJECTID;