xref: /openbmc/linux/fs/btrfs/volumes.c (revision 05bcf503)
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 #include <linux/sched.h>
19 #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 <linux/ratelimit.h>
27 #include <linux/kthread.h>
28 #include <asm/div64.h>
29 #include "compat.h"
30 #include "ctree.h"
31 #include "extent_map.h"
32 #include "disk-io.h"
33 #include "transaction.h"
34 #include "print-tree.h"
35 #include "volumes.h"
36 #include "async-thread.h"
37 #include "check-integrity.h"
38 #include "rcu-string.h"
39 
40 static int init_first_rw_device(struct btrfs_trans_handle *trans,
41 				struct btrfs_root *root,
42 				struct btrfs_device *device);
43 static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
44 static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
45 static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
46 
47 static DEFINE_MUTEX(uuid_mutex);
48 static LIST_HEAD(fs_uuids);
49 
50 static void lock_chunks(struct btrfs_root *root)
51 {
52 	mutex_lock(&root->fs_info->chunk_mutex);
53 }
54 
55 static void unlock_chunks(struct btrfs_root *root)
56 {
57 	mutex_unlock(&root->fs_info->chunk_mutex);
58 }
59 
60 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
61 {
62 	struct btrfs_device *device;
63 	WARN_ON(fs_devices->opened);
64 	while (!list_empty(&fs_devices->devices)) {
65 		device = list_entry(fs_devices->devices.next,
66 				    struct btrfs_device, dev_list);
67 		list_del(&device->dev_list);
68 		rcu_string_free(device->name);
69 		kfree(device);
70 	}
71 	kfree(fs_devices);
72 }
73 
74 void btrfs_cleanup_fs_uuids(void)
75 {
76 	struct btrfs_fs_devices *fs_devices;
77 
78 	while (!list_empty(&fs_uuids)) {
79 		fs_devices = list_entry(fs_uuids.next,
80 					struct btrfs_fs_devices, list);
81 		list_del(&fs_devices->list);
82 		free_fs_devices(fs_devices);
83 	}
84 }
85 
86 static noinline struct btrfs_device *__find_device(struct list_head *head,
87 						   u64 devid, u8 *uuid)
88 {
89 	struct btrfs_device *dev;
90 
91 	list_for_each_entry(dev, head, dev_list) {
92 		if (dev->devid == devid &&
93 		    (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
94 			return dev;
95 		}
96 	}
97 	return NULL;
98 }
99 
100 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
101 {
102 	struct btrfs_fs_devices *fs_devices;
103 
104 	list_for_each_entry(fs_devices, &fs_uuids, list) {
105 		if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
106 			return fs_devices;
107 	}
108 	return NULL;
109 }
110 
111 static void requeue_list(struct btrfs_pending_bios *pending_bios,
112 			struct bio *head, struct bio *tail)
113 {
114 
115 	struct bio *old_head;
116 
117 	old_head = pending_bios->head;
118 	pending_bios->head = head;
119 	if (pending_bios->tail)
120 		tail->bi_next = old_head;
121 	else
122 		pending_bios->tail = tail;
123 }
124 
125 /*
126  * we try to collect pending bios for a device so we don't get a large
127  * number of procs sending bios down to the same device.  This greatly
128  * improves the schedulers ability to collect and merge the bios.
129  *
130  * But, it also turns into a long list of bios to process and that is sure
131  * to eventually make the worker thread block.  The solution here is to
132  * make some progress and then put this work struct back at the end of
133  * the list if the block device is congested.  This way, multiple devices
134  * can make progress from a single worker thread.
135  */
136 static noinline void run_scheduled_bios(struct btrfs_device *device)
137 {
138 	struct bio *pending;
139 	struct backing_dev_info *bdi;
140 	struct btrfs_fs_info *fs_info;
141 	struct btrfs_pending_bios *pending_bios;
142 	struct bio *tail;
143 	struct bio *cur;
144 	int again = 0;
145 	unsigned long num_run;
146 	unsigned long batch_run = 0;
147 	unsigned long limit;
148 	unsigned long last_waited = 0;
149 	int force_reg = 0;
150 	int sync_pending = 0;
151 	struct blk_plug plug;
152 
153 	/*
154 	 * this function runs all the bios we've collected for
155 	 * a particular device.  We don't want to wander off to
156 	 * another device without first sending all of these down.
157 	 * So, setup a plug here and finish it off before we return
158 	 */
159 	blk_start_plug(&plug);
160 
161 	bdi = blk_get_backing_dev_info(device->bdev);
162 	fs_info = device->dev_root->fs_info;
163 	limit = btrfs_async_submit_limit(fs_info);
164 	limit = limit * 2 / 3;
165 
166 loop:
167 	spin_lock(&device->io_lock);
168 
169 loop_lock:
170 	num_run = 0;
171 
172 	/* take all the bios off the list at once and process them
173 	 * later on (without the lock held).  But, remember the
174 	 * tail and other pointers so the bios can be properly reinserted
175 	 * into the list if we hit congestion
176 	 */
177 	if (!force_reg && device->pending_sync_bios.head) {
178 		pending_bios = &device->pending_sync_bios;
179 		force_reg = 1;
180 	} else {
181 		pending_bios = &device->pending_bios;
182 		force_reg = 0;
183 	}
184 
185 	pending = pending_bios->head;
186 	tail = pending_bios->tail;
187 	WARN_ON(pending && !tail);
188 
189 	/*
190 	 * if pending was null this time around, no bios need processing
191 	 * at all and we can stop.  Otherwise it'll loop back up again
192 	 * and do an additional check so no bios are missed.
193 	 *
194 	 * device->running_pending is used to synchronize with the
195 	 * schedule_bio code.
196 	 */
197 	if (device->pending_sync_bios.head == NULL &&
198 	    device->pending_bios.head == NULL) {
199 		again = 0;
200 		device->running_pending = 0;
201 	} else {
202 		again = 1;
203 		device->running_pending = 1;
204 	}
205 
206 	pending_bios->head = NULL;
207 	pending_bios->tail = NULL;
208 
209 	spin_unlock(&device->io_lock);
210 
211 	while (pending) {
212 
213 		rmb();
214 		/* we want to work on both lists, but do more bios on the
215 		 * sync list than the regular list
216 		 */
217 		if ((num_run > 32 &&
218 		    pending_bios != &device->pending_sync_bios &&
219 		    device->pending_sync_bios.head) ||
220 		   (num_run > 64 && pending_bios == &device->pending_sync_bios &&
221 		    device->pending_bios.head)) {
222 			spin_lock(&device->io_lock);
223 			requeue_list(pending_bios, pending, tail);
224 			goto loop_lock;
225 		}
226 
227 		cur = pending;
228 		pending = pending->bi_next;
229 		cur->bi_next = NULL;
230 
231 		if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
232 		    waitqueue_active(&fs_info->async_submit_wait))
233 			wake_up(&fs_info->async_submit_wait);
234 
235 		BUG_ON(atomic_read(&cur->bi_cnt) == 0);
236 
237 		/*
238 		 * if we're doing the sync list, record that our
239 		 * plug has some sync requests on it
240 		 *
241 		 * If we're doing the regular list and there are
242 		 * sync requests sitting around, unplug before
243 		 * we add more
244 		 */
245 		if (pending_bios == &device->pending_sync_bios) {
246 			sync_pending = 1;
247 		} else if (sync_pending) {
248 			blk_finish_plug(&plug);
249 			blk_start_plug(&plug);
250 			sync_pending = 0;
251 		}
252 
253 		btrfsic_submit_bio(cur->bi_rw, cur);
254 		num_run++;
255 		batch_run++;
256 		if (need_resched())
257 			cond_resched();
258 
259 		/*
260 		 * we made progress, there is more work to do and the bdi
261 		 * is now congested.  Back off and let other work structs
262 		 * run instead
263 		 */
264 		if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
265 		    fs_info->fs_devices->open_devices > 1) {
266 			struct io_context *ioc;
267 
268 			ioc = current->io_context;
269 
270 			/*
271 			 * the main goal here is that we don't want to
272 			 * block if we're going to be able to submit
273 			 * more requests without blocking.
274 			 *
275 			 * This code does two great things, it pokes into
276 			 * the elevator code from a filesystem _and_
277 			 * it makes assumptions about how batching works.
278 			 */
279 			if (ioc && ioc->nr_batch_requests > 0 &&
280 			    time_before(jiffies, ioc->last_waited + HZ/50UL) &&
281 			    (last_waited == 0 ||
282 			     ioc->last_waited == last_waited)) {
283 				/*
284 				 * we want to go through our batch of
285 				 * requests and stop.  So, we copy out
286 				 * the ioc->last_waited time and test
287 				 * against it before looping
288 				 */
289 				last_waited = ioc->last_waited;
290 				if (need_resched())
291 					cond_resched();
292 				continue;
293 			}
294 			spin_lock(&device->io_lock);
295 			requeue_list(pending_bios, pending, tail);
296 			device->running_pending = 1;
297 
298 			spin_unlock(&device->io_lock);
299 			btrfs_requeue_work(&device->work);
300 			goto done;
301 		}
302 		/* unplug every 64 requests just for good measure */
303 		if (batch_run % 64 == 0) {
304 			blk_finish_plug(&plug);
305 			blk_start_plug(&plug);
306 			sync_pending = 0;
307 		}
308 	}
309 
310 	cond_resched();
311 	if (again)
312 		goto loop;
313 
314 	spin_lock(&device->io_lock);
315 	if (device->pending_bios.head || device->pending_sync_bios.head)
316 		goto loop_lock;
317 	spin_unlock(&device->io_lock);
318 
319 done:
320 	blk_finish_plug(&plug);
321 }
322 
323 static void pending_bios_fn(struct btrfs_work *work)
324 {
325 	struct btrfs_device *device;
326 
327 	device = container_of(work, struct btrfs_device, work);
328 	run_scheduled_bios(device);
329 }
330 
331 static noinline int device_list_add(const char *path,
332 			   struct btrfs_super_block *disk_super,
333 			   u64 devid, struct btrfs_fs_devices **fs_devices_ret)
334 {
335 	struct btrfs_device *device;
336 	struct btrfs_fs_devices *fs_devices;
337 	struct rcu_string *name;
338 	u64 found_transid = btrfs_super_generation(disk_super);
339 
340 	fs_devices = find_fsid(disk_super->fsid);
341 	if (!fs_devices) {
342 		fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
343 		if (!fs_devices)
344 			return -ENOMEM;
345 		INIT_LIST_HEAD(&fs_devices->devices);
346 		INIT_LIST_HEAD(&fs_devices->alloc_list);
347 		list_add(&fs_devices->list, &fs_uuids);
348 		memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
349 		fs_devices->latest_devid = devid;
350 		fs_devices->latest_trans = found_transid;
351 		mutex_init(&fs_devices->device_list_mutex);
352 		device = NULL;
353 	} else {
354 		device = __find_device(&fs_devices->devices, devid,
355 				       disk_super->dev_item.uuid);
356 	}
357 	if (!device) {
358 		if (fs_devices->opened)
359 			return -EBUSY;
360 
361 		device = kzalloc(sizeof(*device), GFP_NOFS);
362 		if (!device) {
363 			/* we can safely leave the fs_devices entry around */
364 			return -ENOMEM;
365 		}
366 		device->devid = devid;
367 		device->dev_stats_valid = 0;
368 		device->work.func = pending_bios_fn;
369 		memcpy(device->uuid, disk_super->dev_item.uuid,
370 		       BTRFS_UUID_SIZE);
371 		spin_lock_init(&device->io_lock);
372 
373 		name = rcu_string_strdup(path, GFP_NOFS);
374 		if (!name) {
375 			kfree(device);
376 			return -ENOMEM;
377 		}
378 		rcu_assign_pointer(device->name, name);
379 		INIT_LIST_HEAD(&device->dev_alloc_list);
380 
381 		/* init readahead state */
382 		spin_lock_init(&device->reada_lock);
383 		device->reada_curr_zone = NULL;
384 		atomic_set(&device->reada_in_flight, 0);
385 		device->reada_next = 0;
386 		INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
387 		INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
388 
389 		mutex_lock(&fs_devices->device_list_mutex);
390 		list_add_rcu(&device->dev_list, &fs_devices->devices);
391 		mutex_unlock(&fs_devices->device_list_mutex);
392 
393 		device->fs_devices = fs_devices;
394 		fs_devices->num_devices++;
395 	} else if (!device->name || strcmp(device->name->str, path)) {
396 		name = rcu_string_strdup(path, GFP_NOFS);
397 		if (!name)
398 			return -ENOMEM;
399 		rcu_string_free(device->name);
400 		rcu_assign_pointer(device->name, name);
401 		if (device->missing) {
402 			fs_devices->missing_devices--;
403 			device->missing = 0;
404 		}
405 	}
406 
407 	if (found_transid > fs_devices->latest_trans) {
408 		fs_devices->latest_devid = devid;
409 		fs_devices->latest_trans = found_transid;
410 	}
411 	*fs_devices_ret = fs_devices;
412 	return 0;
413 }
414 
415 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
416 {
417 	struct btrfs_fs_devices *fs_devices;
418 	struct btrfs_device *device;
419 	struct btrfs_device *orig_dev;
420 
421 	fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
422 	if (!fs_devices)
423 		return ERR_PTR(-ENOMEM);
424 
425 	INIT_LIST_HEAD(&fs_devices->devices);
426 	INIT_LIST_HEAD(&fs_devices->alloc_list);
427 	INIT_LIST_HEAD(&fs_devices->list);
428 	mutex_init(&fs_devices->device_list_mutex);
429 	fs_devices->latest_devid = orig->latest_devid;
430 	fs_devices->latest_trans = orig->latest_trans;
431 	fs_devices->total_devices = orig->total_devices;
432 	memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
433 
434 	/* We have held the volume lock, it is safe to get the devices. */
435 	list_for_each_entry(orig_dev, &orig->devices, dev_list) {
436 		struct rcu_string *name;
437 
438 		device = kzalloc(sizeof(*device), GFP_NOFS);
439 		if (!device)
440 			goto error;
441 
442 		/*
443 		 * This is ok to do without rcu read locked because we hold the
444 		 * uuid mutex so nothing we touch in here is going to disappear.
445 		 */
446 		name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
447 		if (!name) {
448 			kfree(device);
449 			goto error;
450 		}
451 		rcu_assign_pointer(device->name, name);
452 
453 		device->devid = orig_dev->devid;
454 		device->work.func = pending_bios_fn;
455 		memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
456 		spin_lock_init(&device->io_lock);
457 		INIT_LIST_HEAD(&device->dev_list);
458 		INIT_LIST_HEAD(&device->dev_alloc_list);
459 
460 		list_add(&device->dev_list, &fs_devices->devices);
461 		device->fs_devices = fs_devices;
462 		fs_devices->num_devices++;
463 	}
464 	return fs_devices;
465 error:
466 	free_fs_devices(fs_devices);
467 	return ERR_PTR(-ENOMEM);
468 }
469 
470 void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
471 {
472 	struct btrfs_device *device, *next;
473 
474 	struct block_device *latest_bdev = NULL;
475 	u64 latest_devid = 0;
476 	u64 latest_transid = 0;
477 
478 	mutex_lock(&uuid_mutex);
479 again:
480 	/* This is the initialized path, it is safe to release the devices. */
481 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
482 		if (device->in_fs_metadata) {
483 			if (!latest_transid ||
484 			    device->generation > latest_transid) {
485 				latest_devid = device->devid;
486 				latest_transid = device->generation;
487 				latest_bdev = device->bdev;
488 			}
489 			continue;
490 		}
491 
492 		if (device->bdev) {
493 			blkdev_put(device->bdev, device->mode);
494 			device->bdev = NULL;
495 			fs_devices->open_devices--;
496 		}
497 		if (device->writeable) {
498 			list_del_init(&device->dev_alloc_list);
499 			device->writeable = 0;
500 			fs_devices->rw_devices--;
501 		}
502 		list_del_init(&device->dev_list);
503 		fs_devices->num_devices--;
504 		rcu_string_free(device->name);
505 		kfree(device);
506 	}
507 
508 	if (fs_devices->seed) {
509 		fs_devices = fs_devices->seed;
510 		goto again;
511 	}
512 
513 	fs_devices->latest_bdev = latest_bdev;
514 	fs_devices->latest_devid = latest_devid;
515 	fs_devices->latest_trans = latest_transid;
516 
517 	mutex_unlock(&uuid_mutex);
518 }
519 
520 static void __free_device(struct work_struct *work)
521 {
522 	struct btrfs_device *device;
523 
524 	device = container_of(work, struct btrfs_device, rcu_work);
525 
526 	if (device->bdev)
527 		blkdev_put(device->bdev, device->mode);
528 
529 	rcu_string_free(device->name);
530 	kfree(device);
531 }
532 
533 static void free_device(struct rcu_head *head)
534 {
535 	struct btrfs_device *device;
536 
537 	device = container_of(head, struct btrfs_device, rcu);
538 
539 	INIT_WORK(&device->rcu_work, __free_device);
540 	schedule_work(&device->rcu_work);
541 }
542 
543 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
544 {
545 	struct btrfs_device *device;
546 
547 	if (--fs_devices->opened > 0)
548 		return 0;
549 
550 	mutex_lock(&fs_devices->device_list_mutex);
551 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
552 		struct btrfs_device *new_device;
553 		struct rcu_string *name;
554 
555 		if (device->bdev)
556 			fs_devices->open_devices--;
557 
558 		if (device->writeable) {
559 			list_del_init(&device->dev_alloc_list);
560 			fs_devices->rw_devices--;
561 		}
562 
563 		if (device->can_discard)
564 			fs_devices->num_can_discard--;
565 
566 		new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
567 		BUG_ON(!new_device); /* -ENOMEM */
568 		memcpy(new_device, device, sizeof(*new_device));
569 
570 		/* Safe because we are under uuid_mutex */
571 		if (device->name) {
572 			name = rcu_string_strdup(device->name->str, GFP_NOFS);
573 			BUG_ON(device->name && !name); /* -ENOMEM */
574 			rcu_assign_pointer(new_device->name, name);
575 		}
576 		new_device->bdev = NULL;
577 		new_device->writeable = 0;
578 		new_device->in_fs_metadata = 0;
579 		new_device->can_discard = 0;
580 		list_replace_rcu(&device->dev_list, &new_device->dev_list);
581 
582 		call_rcu(&device->rcu, free_device);
583 	}
584 	mutex_unlock(&fs_devices->device_list_mutex);
585 
586 	WARN_ON(fs_devices->open_devices);
587 	WARN_ON(fs_devices->rw_devices);
588 	fs_devices->opened = 0;
589 	fs_devices->seeding = 0;
590 
591 	return 0;
592 }
593 
594 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
595 {
596 	struct btrfs_fs_devices *seed_devices = NULL;
597 	int ret;
598 
599 	mutex_lock(&uuid_mutex);
600 	ret = __btrfs_close_devices(fs_devices);
601 	if (!fs_devices->opened) {
602 		seed_devices = fs_devices->seed;
603 		fs_devices->seed = NULL;
604 	}
605 	mutex_unlock(&uuid_mutex);
606 
607 	while (seed_devices) {
608 		fs_devices = seed_devices;
609 		seed_devices = fs_devices->seed;
610 		__btrfs_close_devices(fs_devices);
611 		free_fs_devices(fs_devices);
612 	}
613 	return ret;
614 }
615 
616 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
617 				fmode_t flags, void *holder)
618 {
619 	struct request_queue *q;
620 	struct block_device *bdev;
621 	struct list_head *head = &fs_devices->devices;
622 	struct btrfs_device *device;
623 	struct block_device *latest_bdev = NULL;
624 	struct buffer_head *bh;
625 	struct btrfs_super_block *disk_super;
626 	u64 latest_devid = 0;
627 	u64 latest_transid = 0;
628 	u64 devid;
629 	int seeding = 1;
630 	int ret = 0;
631 
632 	flags |= FMODE_EXCL;
633 
634 	list_for_each_entry(device, head, dev_list) {
635 		if (device->bdev)
636 			continue;
637 		if (!device->name)
638 			continue;
639 
640 		bdev = blkdev_get_by_path(device->name->str, flags, holder);
641 		if (IS_ERR(bdev)) {
642 			printk(KERN_INFO "btrfs: open %s failed\n", device->name->str);
643 			goto error;
644 		}
645 		filemap_write_and_wait(bdev->bd_inode->i_mapping);
646 		invalidate_bdev(bdev);
647 		set_blocksize(bdev, 4096);
648 
649 		bh = btrfs_read_dev_super(bdev);
650 		if (!bh)
651 			goto error_close;
652 
653 		disk_super = (struct btrfs_super_block *)bh->b_data;
654 		devid = btrfs_stack_device_id(&disk_super->dev_item);
655 		if (devid != device->devid)
656 			goto error_brelse;
657 
658 		if (memcmp(device->uuid, disk_super->dev_item.uuid,
659 			   BTRFS_UUID_SIZE))
660 			goto error_brelse;
661 
662 		device->generation = btrfs_super_generation(disk_super);
663 		if (!latest_transid || device->generation > latest_transid) {
664 			latest_devid = devid;
665 			latest_transid = device->generation;
666 			latest_bdev = bdev;
667 		}
668 
669 		if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
670 			device->writeable = 0;
671 		} else {
672 			device->writeable = !bdev_read_only(bdev);
673 			seeding = 0;
674 		}
675 
676 		q = bdev_get_queue(bdev);
677 		if (blk_queue_discard(q)) {
678 			device->can_discard = 1;
679 			fs_devices->num_can_discard++;
680 		}
681 
682 		device->bdev = bdev;
683 		device->in_fs_metadata = 0;
684 		device->mode = flags;
685 
686 		if (!blk_queue_nonrot(bdev_get_queue(bdev)))
687 			fs_devices->rotating = 1;
688 
689 		fs_devices->open_devices++;
690 		if (device->writeable) {
691 			fs_devices->rw_devices++;
692 			list_add(&device->dev_alloc_list,
693 				 &fs_devices->alloc_list);
694 		}
695 		brelse(bh);
696 		continue;
697 
698 error_brelse:
699 		brelse(bh);
700 error_close:
701 		blkdev_put(bdev, flags);
702 error:
703 		continue;
704 	}
705 	if (fs_devices->open_devices == 0) {
706 		ret = -EINVAL;
707 		goto out;
708 	}
709 	fs_devices->seeding = seeding;
710 	fs_devices->opened = 1;
711 	fs_devices->latest_bdev = latest_bdev;
712 	fs_devices->latest_devid = latest_devid;
713 	fs_devices->latest_trans = latest_transid;
714 	fs_devices->total_rw_bytes = 0;
715 out:
716 	return ret;
717 }
718 
719 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
720 		       fmode_t flags, void *holder)
721 {
722 	int ret;
723 
724 	mutex_lock(&uuid_mutex);
725 	if (fs_devices->opened) {
726 		fs_devices->opened++;
727 		ret = 0;
728 	} else {
729 		ret = __btrfs_open_devices(fs_devices, flags, holder);
730 	}
731 	mutex_unlock(&uuid_mutex);
732 	return ret;
733 }
734 
735 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
736 			  struct btrfs_fs_devices **fs_devices_ret)
737 {
738 	struct btrfs_super_block *disk_super;
739 	struct block_device *bdev;
740 	struct buffer_head *bh;
741 	int ret;
742 	u64 devid;
743 	u64 transid;
744 	u64 total_devices;
745 
746 	flags |= FMODE_EXCL;
747 	bdev = blkdev_get_by_path(path, flags, holder);
748 
749 	if (IS_ERR(bdev)) {
750 		ret = PTR_ERR(bdev);
751 		goto error;
752 	}
753 
754 	mutex_lock(&uuid_mutex);
755 	ret = set_blocksize(bdev, 4096);
756 	if (ret)
757 		goto error_close;
758 	bh = btrfs_read_dev_super(bdev);
759 	if (!bh) {
760 		ret = -EINVAL;
761 		goto error_close;
762 	}
763 	disk_super = (struct btrfs_super_block *)bh->b_data;
764 	devid = btrfs_stack_device_id(&disk_super->dev_item);
765 	transid = btrfs_super_generation(disk_super);
766 	total_devices = btrfs_super_num_devices(disk_super);
767 	if (disk_super->label[0])
768 		printk(KERN_INFO "device label %s ", disk_super->label);
769 	else
770 		printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
771 	printk(KERN_CONT "devid %llu transid %llu %s\n",
772 	       (unsigned long long)devid, (unsigned long long)transid, path);
773 	ret = device_list_add(path, disk_super, devid, fs_devices_ret);
774 	if (!ret && fs_devices_ret)
775 		(*fs_devices_ret)->total_devices = total_devices;
776 	brelse(bh);
777 error_close:
778 	mutex_unlock(&uuid_mutex);
779 	blkdev_put(bdev, flags);
780 error:
781 	return ret;
782 }
783 
784 /* helper to account the used device space in the range */
785 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
786 				   u64 end, u64 *length)
787 {
788 	struct btrfs_key key;
789 	struct btrfs_root *root = device->dev_root;
790 	struct btrfs_dev_extent *dev_extent;
791 	struct btrfs_path *path;
792 	u64 extent_end;
793 	int ret;
794 	int slot;
795 	struct extent_buffer *l;
796 
797 	*length = 0;
798 
799 	if (start >= device->total_bytes)
800 		return 0;
801 
802 	path = btrfs_alloc_path();
803 	if (!path)
804 		return -ENOMEM;
805 	path->reada = 2;
806 
807 	key.objectid = device->devid;
808 	key.offset = start;
809 	key.type = BTRFS_DEV_EXTENT_KEY;
810 
811 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
812 	if (ret < 0)
813 		goto out;
814 	if (ret > 0) {
815 		ret = btrfs_previous_item(root, path, key.objectid, key.type);
816 		if (ret < 0)
817 			goto out;
818 	}
819 
820 	while (1) {
821 		l = path->nodes[0];
822 		slot = path->slots[0];
823 		if (slot >= btrfs_header_nritems(l)) {
824 			ret = btrfs_next_leaf(root, path);
825 			if (ret == 0)
826 				continue;
827 			if (ret < 0)
828 				goto out;
829 
830 			break;
831 		}
832 		btrfs_item_key_to_cpu(l, &key, slot);
833 
834 		if (key.objectid < device->devid)
835 			goto next;
836 
837 		if (key.objectid > device->devid)
838 			break;
839 
840 		if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
841 			goto next;
842 
843 		dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
844 		extent_end = key.offset + btrfs_dev_extent_length(l,
845 								  dev_extent);
846 		if (key.offset <= start && extent_end > end) {
847 			*length = end - start + 1;
848 			break;
849 		} else if (key.offset <= start && extent_end > start)
850 			*length += extent_end - start;
851 		else if (key.offset > start && extent_end <= end)
852 			*length += extent_end - key.offset;
853 		else if (key.offset > start && key.offset <= end) {
854 			*length += end - key.offset + 1;
855 			break;
856 		} else if (key.offset > end)
857 			break;
858 
859 next:
860 		path->slots[0]++;
861 	}
862 	ret = 0;
863 out:
864 	btrfs_free_path(path);
865 	return ret;
866 }
867 
868 /*
869  * find_free_dev_extent - find free space in the specified device
870  * @device:	the device which we search the free space in
871  * @num_bytes:	the size of the free space that we need
872  * @start:	store the start of the free space.
873  * @len:	the size of the free space. that we find, or the size of the max
874  * 		free space if we don't find suitable free space
875  *
876  * this uses a pretty simple search, the expectation is that it is
877  * called very infrequently and that a given device has a small number
878  * of extents
879  *
880  * @start is used to store the start of the free space if we find. But if we
881  * don't find suitable free space, it will be used to store the start position
882  * of the max free space.
883  *
884  * @len is used to store the size of the free space that we find.
885  * But if we don't find suitable free space, it is used to store the size of
886  * the max free space.
887  */
888 int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
889 			 u64 *start, u64 *len)
890 {
891 	struct btrfs_key key;
892 	struct btrfs_root *root = device->dev_root;
893 	struct btrfs_dev_extent *dev_extent;
894 	struct btrfs_path *path;
895 	u64 hole_size;
896 	u64 max_hole_start;
897 	u64 max_hole_size;
898 	u64 extent_end;
899 	u64 search_start;
900 	u64 search_end = device->total_bytes;
901 	int ret;
902 	int slot;
903 	struct extent_buffer *l;
904 
905 	/* FIXME use last free of some kind */
906 
907 	/* we don't want to overwrite the superblock on the drive,
908 	 * so we make sure to start at an offset of at least 1MB
909 	 */
910 	search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
911 
912 	max_hole_start = search_start;
913 	max_hole_size = 0;
914 	hole_size = 0;
915 
916 	if (search_start >= search_end) {
917 		ret = -ENOSPC;
918 		goto error;
919 	}
920 
921 	path = btrfs_alloc_path();
922 	if (!path) {
923 		ret = -ENOMEM;
924 		goto error;
925 	}
926 	path->reada = 2;
927 
928 	key.objectid = device->devid;
929 	key.offset = search_start;
930 	key.type = BTRFS_DEV_EXTENT_KEY;
931 
932 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
933 	if (ret < 0)
934 		goto out;
935 	if (ret > 0) {
936 		ret = btrfs_previous_item(root, path, key.objectid, key.type);
937 		if (ret < 0)
938 			goto out;
939 	}
940 
941 	while (1) {
942 		l = path->nodes[0];
943 		slot = path->slots[0];
944 		if (slot >= btrfs_header_nritems(l)) {
945 			ret = btrfs_next_leaf(root, path);
946 			if (ret == 0)
947 				continue;
948 			if (ret < 0)
949 				goto out;
950 
951 			break;
952 		}
953 		btrfs_item_key_to_cpu(l, &key, slot);
954 
955 		if (key.objectid < device->devid)
956 			goto next;
957 
958 		if (key.objectid > device->devid)
959 			break;
960 
961 		if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
962 			goto next;
963 
964 		if (key.offset > search_start) {
965 			hole_size = key.offset - search_start;
966 
967 			if (hole_size > max_hole_size) {
968 				max_hole_start = search_start;
969 				max_hole_size = hole_size;
970 			}
971 
972 			/*
973 			 * If this free space is greater than which we need,
974 			 * it must be the max free space that we have found
975 			 * until now, so max_hole_start must point to the start
976 			 * of this free space and the length of this free space
977 			 * is stored in max_hole_size. Thus, we return
978 			 * max_hole_start and max_hole_size and go back to the
979 			 * caller.
980 			 */
981 			if (hole_size >= num_bytes) {
982 				ret = 0;
983 				goto out;
984 			}
985 		}
986 
987 		dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
988 		extent_end = key.offset + btrfs_dev_extent_length(l,
989 								  dev_extent);
990 		if (extent_end > search_start)
991 			search_start = extent_end;
992 next:
993 		path->slots[0]++;
994 		cond_resched();
995 	}
996 
997 	/*
998 	 * At this point, search_start should be the end of
999 	 * allocated dev extents, and when shrinking the device,
1000 	 * search_end may be smaller than search_start.
1001 	 */
1002 	if (search_end > search_start)
1003 		hole_size = search_end - search_start;
1004 
1005 	if (hole_size > max_hole_size) {
1006 		max_hole_start = search_start;
1007 		max_hole_size = hole_size;
1008 	}
1009 
1010 	/* See above. */
1011 	if (hole_size < num_bytes)
1012 		ret = -ENOSPC;
1013 	else
1014 		ret = 0;
1015 
1016 out:
1017 	btrfs_free_path(path);
1018 error:
1019 	*start = max_hole_start;
1020 	if (len)
1021 		*len = max_hole_size;
1022 	return ret;
1023 }
1024 
1025 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1026 			  struct btrfs_device *device,
1027 			  u64 start)
1028 {
1029 	int ret;
1030 	struct btrfs_path *path;
1031 	struct btrfs_root *root = device->dev_root;
1032 	struct btrfs_key key;
1033 	struct btrfs_key found_key;
1034 	struct extent_buffer *leaf = NULL;
1035 	struct btrfs_dev_extent *extent = NULL;
1036 
1037 	path = btrfs_alloc_path();
1038 	if (!path)
1039 		return -ENOMEM;
1040 
1041 	key.objectid = device->devid;
1042 	key.offset = start;
1043 	key.type = BTRFS_DEV_EXTENT_KEY;
1044 again:
1045 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1046 	if (ret > 0) {
1047 		ret = btrfs_previous_item(root, path, key.objectid,
1048 					  BTRFS_DEV_EXTENT_KEY);
1049 		if (ret)
1050 			goto out;
1051 		leaf = path->nodes[0];
1052 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1053 		extent = btrfs_item_ptr(leaf, path->slots[0],
1054 					struct btrfs_dev_extent);
1055 		BUG_ON(found_key.offset > start || found_key.offset +
1056 		       btrfs_dev_extent_length(leaf, extent) < start);
1057 		key = found_key;
1058 		btrfs_release_path(path);
1059 		goto again;
1060 	} else if (ret == 0) {
1061 		leaf = path->nodes[0];
1062 		extent = btrfs_item_ptr(leaf, path->slots[0],
1063 					struct btrfs_dev_extent);
1064 	} else {
1065 		btrfs_error(root->fs_info, ret, "Slot search failed");
1066 		goto out;
1067 	}
1068 
1069 	if (device->bytes_used > 0) {
1070 		u64 len = btrfs_dev_extent_length(leaf, extent);
1071 		device->bytes_used -= len;
1072 		spin_lock(&root->fs_info->free_chunk_lock);
1073 		root->fs_info->free_chunk_space += len;
1074 		spin_unlock(&root->fs_info->free_chunk_lock);
1075 	}
1076 	ret = btrfs_del_item(trans, root, path);
1077 	if (ret) {
1078 		btrfs_error(root->fs_info, ret,
1079 			    "Failed to remove dev extent item");
1080 	}
1081 out:
1082 	btrfs_free_path(path);
1083 	return ret;
1084 }
1085 
1086 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1087 			   struct btrfs_device *device,
1088 			   u64 chunk_tree, u64 chunk_objectid,
1089 			   u64 chunk_offset, u64 start, u64 num_bytes)
1090 {
1091 	int ret;
1092 	struct btrfs_path *path;
1093 	struct btrfs_root *root = device->dev_root;
1094 	struct btrfs_dev_extent *extent;
1095 	struct extent_buffer *leaf;
1096 	struct btrfs_key key;
1097 
1098 	WARN_ON(!device->in_fs_metadata);
1099 	path = btrfs_alloc_path();
1100 	if (!path)
1101 		return -ENOMEM;
1102 
1103 	key.objectid = device->devid;
1104 	key.offset = start;
1105 	key.type = BTRFS_DEV_EXTENT_KEY;
1106 	ret = btrfs_insert_empty_item(trans, root, path, &key,
1107 				      sizeof(*extent));
1108 	if (ret)
1109 		goto out;
1110 
1111 	leaf = path->nodes[0];
1112 	extent = btrfs_item_ptr(leaf, path->slots[0],
1113 				struct btrfs_dev_extent);
1114 	btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1115 	btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1116 	btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1117 
1118 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1119 		    (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1120 		    BTRFS_UUID_SIZE);
1121 
1122 	btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1123 	btrfs_mark_buffer_dirty(leaf);
1124 out:
1125 	btrfs_free_path(path);
1126 	return ret;
1127 }
1128 
1129 static noinline int find_next_chunk(struct btrfs_root *root,
1130 				    u64 objectid, u64 *offset)
1131 {
1132 	struct btrfs_path *path;
1133 	int ret;
1134 	struct btrfs_key key;
1135 	struct btrfs_chunk *chunk;
1136 	struct btrfs_key found_key;
1137 
1138 	path = btrfs_alloc_path();
1139 	if (!path)
1140 		return -ENOMEM;
1141 
1142 	key.objectid = objectid;
1143 	key.offset = (u64)-1;
1144 	key.type = BTRFS_CHUNK_ITEM_KEY;
1145 
1146 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1147 	if (ret < 0)
1148 		goto error;
1149 
1150 	BUG_ON(ret == 0); /* Corruption */
1151 
1152 	ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1153 	if (ret) {
1154 		*offset = 0;
1155 	} else {
1156 		btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1157 				      path->slots[0]);
1158 		if (found_key.objectid != objectid)
1159 			*offset = 0;
1160 		else {
1161 			chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1162 					       struct btrfs_chunk);
1163 			*offset = found_key.offset +
1164 				btrfs_chunk_length(path->nodes[0], chunk);
1165 		}
1166 	}
1167 	ret = 0;
1168 error:
1169 	btrfs_free_path(path);
1170 	return ret;
1171 }
1172 
1173 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1174 {
1175 	int ret;
1176 	struct btrfs_key key;
1177 	struct btrfs_key found_key;
1178 	struct btrfs_path *path;
1179 
1180 	root = root->fs_info->chunk_root;
1181 
1182 	path = btrfs_alloc_path();
1183 	if (!path)
1184 		return -ENOMEM;
1185 
1186 	key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1187 	key.type = BTRFS_DEV_ITEM_KEY;
1188 	key.offset = (u64)-1;
1189 
1190 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1191 	if (ret < 0)
1192 		goto error;
1193 
1194 	BUG_ON(ret == 0); /* Corruption */
1195 
1196 	ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1197 				  BTRFS_DEV_ITEM_KEY);
1198 	if (ret) {
1199 		*objectid = 1;
1200 	} else {
1201 		btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1202 				      path->slots[0]);
1203 		*objectid = found_key.offset + 1;
1204 	}
1205 	ret = 0;
1206 error:
1207 	btrfs_free_path(path);
1208 	return ret;
1209 }
1210 
1211 /*
1212  * the device information is stored in the chunk root
1213  * the btrfs_device struct should be fully filled in
1214  */
1215 int btrfs_add_device(struct btrfs_trans_handle *trans,
1216 		     struct btrfs_root *root,
1217 		     struct btrfs_device *device)
1218 {
1219 	int ret;
1220 	struct btrfs_path *path;
1221 	struct btrfs_dev_item *dev_item;
1222 	struct extent_buffer *leaf;
1223 	struct btrfs_key key;
1224 	unsigned long ptr;
1225 
1226 	root = root->fs_info->chunk_root;
1227 
1228 	path = btrfs_alloc_path();
1229 	if (!path)
1230 		return -ENOMEM;
1231 
1232 	key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1233 	key.type = BTRFS_DEV_ITEM_KEY;
1234 	key.offset = device->devid;
1235 
1236 	ret = btrfs_insert_empty_item(trans, root, path, &key,
1237 				      sizeof(*dev_item));
1238 	if (ret)
1239 		goto out;
1240 
1241 	leaf = path->nodes[0];
1242 	dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1243 
1244 	btrfs_set_device_id(leaf, dev_item, device->devid);
1245 	btrfs_set_device_generation(leaf, dev_item, 0);
1246 	btrfs_set_device_type(leaf, dev_item, device->type);
1247 	btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1248 	btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1249 	btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1250 	btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1251 	btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1252 	btrfs_set_device_group(leaf, dev_item, 0);
1253 	btrfs_set_device_seek_speed(leaf, dev_item, 0);
1254 	btrfs_set_device_bandwidth(leaf, dev_item, 0);
1255 	btrfs_set_device_start_offset(leaf, dev_item, 0);
1256 
1257 	ptr = (unsigned long)btrfs_device_uuid(dev_item);
1258 	write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1259 	ptr = (unsigned long)btrfs_device_fsid(dev_item);
1260 	write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1261 	btrfs_mark_buffer_dirty(leaf);
1262 
1263 	ret = 0;
1264 out:
1265 	btrfs_free_path(path);
1266 	return ret;
1267 }
1268 
1269 static int btrfs_rm_dev_item(struct btrfs_root *root,
1270 			     struct btrfs_device *device)
1271 {
1272 	int ret;
1273 	struct btrfs_path *path;
1274 	struct btrfs_key key;
1275 	struct btrfs_trans_handle *trans;
1276 
1277 	root = root->fs_info->chunk_root;
1278 
1279 	path = btrfs_alloc_path();
1280 	if (!path)
1281 		return -ENOMEM;
1282 
1283 	trans = btrfs_start_transaction(root, 0);
1284 	if (IS_ERR(trans)) {
1285 		btrfs_free_path(path);
1286 		return PTR_ERR(trans);
1287 	}
1288 	key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1289 	key.type = BTRFS_DEV_ITEM_KEY;
1290 	key.offset = device->devid;
1291 	lock_chunks(root);
1292 
1293 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1294 	if (ret < 0)
1295 		goto out;
1296 
1297 	if (ret > 0) {
1298 		ret = -ENOENT;
1299 		goto out;
1300 	}
1301 
1302 	ret = btrfs_del_item(trans, root, path);
1303 	if (ret)
1304 		goto out;
1305 out:
1306 	btrfs_free_path(path);
1307 	unlock_chunks(root);
1308 	btrfs_commit_transaction(trans, root);
1309 	return ret;
1310 }
1311 
1312 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1313 {
1314 	struct btrfs_device *device;
1315 	struct btrfs_device *next_device;
1316 	struct block_device *bdev;
1317 	struct buffer_head *bh = NULL;
1318 	struct btrfs_super_block *disk_super;
1319 	struct btrfs_fs_devices *cur_devices;
1320 	u64 all_avail;
1321 	u64 devid;
1322 	u64 num_devices;
1323 	u8 *dev_uuid;
1324 	int ret = 0;
1325 	bool clear_super = false;
1326 
1327 	mutex_lock(&uuid_mutex);
1328 
1329 	all_avail = root->fs_info->avail_data_alloc_bits |
1330 		root->fs_info->avail_system_alloc_bits |
1331 		root->fs_info->avail_metadata_alloc_bits;
1332 
1333 	if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1334 	    root->fs_info->fs_devices->num_devices <= 4) {
1335 		printk(KERN_ERR "btrfs: unable to go below four devices "
1336 		       "on raid10\n");
1337 		ret = -EINVAL;
1338 		goto out;
1339 	}
1340 
1341 	if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1342 	    root->fs_info->fs_devices->num_devices <= 2) {
1343 		printk(KERN_ERR "btrfs: unable to go below two "
1344 		       "devices on raid1\n");
1345 		ret = -EINVAL;
1346 		goto out;
1347 	}
1348 
1349 	if (strcmp(device_path, "missing") == 0) {
1350 		struct list_head *devices;
1351 		struct btrfs_device *tmp;
1352 
1353 		device = NULL;
1354 		devices = &root->fs_info->fs_devices->devices;
1355 		/*
1356 		 * It is safe to read the devices since the volume_mutex
1357 		 * is held.
1358 		 */
1359 		list_for_each_entry(tmp, devices, dev_list) {
1360 			if (tmp->in_fs_metadata && !tmp->bdev) {
1361 				device = tmp;
1362 				break;
1363 			}
1364 		}
1365 		bdev = NULL;
1366 		bh = NULL;
1367 		disk_super = NULL;
1368 		if (!device) {
1369 			printk(KERN_ERR "btrfs: no missing devices found to "
1370 			       "remove\n");
1371 			goto out;
1372 		}
1373 	} else {
1374 		bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1375 					  root->fs_info->bdev_holder);
1376 		if (IS_ERR(bdev)) {
1377 			ret = PTR_ERR(bdev);
1378 			goto out;
1379 		}
1380 
1381 		set_blocksize(bdev, 4096);
1382 		invalidate_bdev(bdev);
1383 		bh = btrfs_read_dev_super(bdev);
1384 		if (!bh) {
1385 			ret = -EINVAL;
1386 			goto error_close;
1387 		}
1388 		disk_super = (struct btrfs_super_block *)bh->b_data;
1389 		devid = btrfs_stack_device_id(&disk_super->dev_item);
1390 		dev_uuid = disk_super->dev_item.uuid;
1391 		device = btrfs_find_device(root, devid, dev_uuid,
1392 					   disk_super->fsid);
1393 		if (!device) {
1394 			ret = -ENOENT;
1395 			goto error_brelse;
1396 		}
1397 	}
1398 
1399 	if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1400 		printk(KERN_ERR "btrfs: unable to remove the only writeable "
1401 		       "device\n");
1402 		ret = -EINVAL;
1403 		goto error_brelse;
1404 	}
1405 
1406 	if (device->writeable) {
1407 		lock_chunks(root);
1408 		list_del_init(&device->dev_alloc_list);
1409 		unlock_chunks(root);
1410 		root->fs_info->fs_devices->rw_devices--;
1411 		clear_super = true;
1412 	}
1413 
1414 	ret = btrfs_shrink_device(device, 0);
1415 	if (ret)
1416 		goto error_undo;
1417 
1418 	ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1419 	if (ret)
1420 		goto error_undo;
1421 
1422 	spin_lock(&root->fs_info->free_chunk_lock);
1423 	root->fs_info->free_chunk_space = device->total_bytes -
1424 		device->bytes_used;
1425 	spin_unlock(&root->fs_info->free_chunk_lock);
1426 
1427 	device->in_fs_metadata = 0;
1428 	btrfs_scrub_cancel_dev(root, device);
1429 
1430 	/*
1431 	 * the device list mutex makes sure that we don't change
1432 	 * the device list while someone else is writing out all
1433 	 * the device supers.
1434 	 */
1435 
1436 	cur_devices = device->fs_devices;
1437 	mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1438 	list_del_rcu(&device->dev_list);
1439 
1440 	device->fs_devices->num_devices--;
1441 	device->fs_devices->total_devices--;
1442 
1443 	if (device->missing)
1444 		root->fs_info->fs_devices->missing_devices--;
1445 
1446 	next_device = list_entry(root->fs_info->fs_devices->devices.next,
1447 				 struct btrfs_device, dev_list);
1448 	if (device->bdev == root->fs_info->sb->s_bdev)
1449 		root->fs_info->sb->s_bdev = next_device->bdev;
1450 	if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1451 		root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1452 
1453 	if (device->bdev)
1454 		device->fs_devices->open_devices--;
1455 
1456 	call_rcu(&device->rcu, free_device);
1457 	mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1458 
1459 	num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1460 	btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
1461 
1462 	if (cur_devices->open_devices == 0) {
1463 		struct btrfs_fs_devices *fs_devices;
1464 		fs_devices = root->fs_info->fs_devices;
1465 		while (fs_devices) {
1466 			if (fs_devices->seed == cur_devices)
1467 				break;
1468 			fs_devices = fs_devices->seed;
1469 		}
1470 		fs_devices->seed = cur_devices->seed;
1471 		cur_devices->seed = NULL;
1472 		lock_chunks(root);
1473 		__btrfs_close_devices(cur_devices);
1474 		unlock_chunks(root);
1475 		free_fs_devices(cur_devices);
1476 	}
1477 
1478 	root->fs_info->num_tolerated_disk_barrier_failures =
1479 		btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1480 
1481 	/*
1482 	 * at this point, the device is zero sized.  We want to
1483 	 * remove it from the devices list and zero out the old super
1484 	 */
1485 	if (clear_super) {
1486 		/* make sure this device isn't detected as part of
1487 		 * the FS anymore
1488 		 */
1489 		memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1490 		set_buffer_dirty(bh);
1491 		sync_dirty_buffer(bh);
1492 	}
1493 
1494 	ret = 0;
1495 
1496 error_brelse:
1497 	brelse(bh);
1498 error_close:
1499 	if (bdev)
1500 		blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1501 out:
1502 	mutex_unlock(&uuid_mutex);
1503 	return ret;
1504 error_undo:
1505 	if (device->writeable) {
1506 		lock_chunks(root);
1507 		list_add(&device->dev_alloc_list,
1508 			 &root->fs_info->fs_devices->alloc_list);
1509 		unlock_chunks(root);
1510 		root->fs_info->fs_devices->rw_devices++;
1511 	}
1512 	goto error_brelse;
1513 }
1514 
1515 /*
1516  * does all the dirty work required for changing file system's UUID.
1517  */
1518 static int btrfs_prepare_sprout(struct btrfs_root *root)
1519 {
1520 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1521 	struct btrfs_fs_devices *old_devices;
1522 	struct btrfs_fs_devices *seed_devices;
1523 	struct btrfs_super_block *disk_super = root->fs_info->super_copy;
1524 	struct btrfs_device *device;
1525 	u64 super_flags;
1526 
1527 	BUG_ON(!mutex_is_locked(&uuid_mutex));
1528 	if (!fs_devices->seeding)
1529 		return -EINVAL;
1530 
1531 	seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1532 	if (!seed_devices)
1533 		return -ENOMEM;
1534 
1535 	old_devices = clone_fs_devices(fs_devices);
1536 	if (IS_ERR(old_devices)) {
1537 		kfree(seed_devices);
1538 		return PTR_ERR(old_devices);
1539 	}
1540 
1541 	list_add(&old_devices->list, &fs_uuids);
1542 
1543 	memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1544 	seed_devices->opened = 1;
1545 	INIT_LIST_HEAD(&seed_devices->devices);
1546 	INIT_LIST_HEAD(&seed_devices->alloc_list);
1547 	mutex_init(&seed_devices->device_list_mutex);
1548 
1549 	mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1550 	list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1551 			      synchronize_rcu);
1552 	mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1553 
1554 	list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1555 	list_for_each_entry(device, &seed_devices->devices, dev_list) {
1556 		device->fs_devices = seed_devices;
1557 	}
1558 
1559 	fs_devices->seeding = 0;
1560 	fs_devices->num_devices = 0;
1561 	fs_devices->open_devices = 0;
1562 	fs_devices->total_devices = 0;
1563 	fs_devices->seed = seed_devices;
1564 
1565 	generate_random_uuid(fs_devices->fsid);
1566 	memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1567 	memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1568 	super_flags = btrfs_super_flags(disk_super) &
1569 		      ~BTRFS_SUPER_FLAG_SEEDING;
1570 	btrfs_set_super_flags(disk_super, super_flags);
1571 
1572 	return 0;
1573 }
1574 
1575 /*
1576  * strore the expected generation for seed devices in device items.
1577  */
1578 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1579 			       struct btrfs_root *root)
1580 {
1581 	struct btrfs_path *path;
1582 	struct extent_buffer *leaf;
1583 	struct btrfs_dev_item *dev_item;
1584 	struct btrfs_device *device;
1585 	struct btrfs_key key;
1586 	u8 fs_uuid[BTRFS_UUID_SIZE];
1587 	u8 dev_uuid[BTRFS_UUID_SIZE];
1588 	u64 devid;
1589 	int ret;
1590 
1591 	path = btrfs_alloc_path();
1592 	if (!path)
1593 		return -ENOMEM;
1594 
1595 	root = root->fs_info->chunk_root;
1596 	key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1597 	key.offset = 0;
1598 	key.type = BTRFS_DEV_ITEM_KEY;
1599 
1600 	while (1) {
1601 		ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1602 		if (ret < 0)
1603 			goto error;
1604 
1605 		leaf = path->nodes[0];
1606 next_slot:
1607 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1608 			ret = btrfs_next_leaf(root, path);
1609 			if (ret > 0)
1610 				break;
1611 			if (ret < 0)
1612 				goto error;
1613 			leaf = path->nodes[0];
1614 			btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1615 			btrfs_release_path(path);
1616 			continue;
1617 		}
1618 
1619 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1620 		if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1621 		    key.type != BTRFS_DEV_ITEM_KEY)
1622 			break;
1623 
1624 		dev_item = btrfs_item_ptr(leaf, path->slots[0],
1625 					  struct btrfs_dev_item);
1626 		devid = btrfs_device_id(leaf, dev_item);
1627 		read_extent_buffer(leaf, dev_uuid,
1628 				   (unsigned long)btrfs_device_uuid(dev_item),
1629 				   BTRFS_UUID_SIZE);
1630 		read_extent_buffer(leaf, fs_uuid,
1631 				   (unsigned long)btrfs_device_fsid(dev_item),
1632 				   BTRFS_UUID_SIZE);
1633 		device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1634 		BUG_ON(!device); /* Logic error */
1635 
1636 		if (device->fs_devices->seeding) {
1637 			btrfs_set_device_generation(leaf, dev_item,
1638 						    device->generation);
1639 			btrfs_mark_buffer_dirty(leaf);
1640 		}
1641 
1642 		path->slots[0]++;
1643 		goto next_slot;
1644 	}
1645 	ret = 0;
1646 error:
1647 	btrfs_free_path(path);
1648 	return ret;
1649 }
1650 
1651 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1652 {
1653 	struct request_queue *q;
1654 	struct btrfs_trans_handle *trans;
1655 	struct btrfs_device *device;
1656 	struct block_device *bdev;
1657 	struct list_head *devices;
1658 	struct super_block *sb = root->fs_info->sb;
1659 	struct rcu_string *name;
1660 	u64 total_bytes;
1661 	int seeding_dev = 0;
1662 	int ret = 0;
1663 
1664 	if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1665 		return -EROFS;
1666 
1667 	bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
1668 				  root->fs_info->bdev_holder);
1669 	if (IS_ERR(bdev))
1670 		return PTR_ERR(bdev);
1671 
1672 	if (root->fs_info->fs_devices->seeding) {
1673 		seeding_dev = 1;
1674 		down_write(&sb->s_umount);
1675 		mutex_lock(&uuid_mutex);
1676 	}
1677 
1678 	filemap_write_and_wait(bdev->bd_inode->i_mapping);
1679 
1680 	devices = &root->fs_info->fs_devices->devices;
1681 	/*
1682 	 * we have the volume lock, so we don't need the extra
1683 	 * device list mutex while reading the list here.
1684 	 */
1685 	list_for_each_entry(device, devices, dev_list) {
1686 		if (device->bdev == bdev) {
1687 			ret = -EEXIST;
1688 			goto error;
1689 		}
1690 	}
1691 
1692 	device = kzalloc(sizeof(*device), GFP_NOFS);
1693 	if (!device) {
1694 		/* we can safely leave the fs_devices entry around */
1695 		ret = -ENOMEM;
1696 		goto error;
1697 	}
1698 
1699 	name = rcu_string_strdup(device_path, GFP_NOFS);
1700 	if (!name) {
1701 		kfree(device);
1702 		ret = -ENOMEM;
1703 		goto error;
1704 	}
1705 	rcu_assign_pointer(device->name, name);
1706 
1707 	ret = find_next_devid(root, &device->devid);
1708 	if (ret) {
1709 		rcu_string_free(device->name);
1710 		kfree(device);
1711 		goto error;
1712 	}
1713 
1714 	trans = btrfs_start_transaction(root, 0);
1715 	if (IS_ERR(trans)) {
1716 		rcu_string_free(device->name);
1717 		kfree(device);
1718 		ret = PTR_ERR(trans);
1719 		goto error;
1720 	}
1721 
1722 	lock_chunks(root);
1723 
1724 	q = bdev_get_queue(bdev);
1725 	if (blk_queue_discard(q))
1726 		device->can_discard = 1;
1727 	device->writeable = 1;
1728 	device->work.func = pending_bios_fn;
1729 	generate_random_uuid(device->uuid);
1730 	spin_lock_init(&device->io_lock);
1731 	device->generation = trans->transid;
1732 	device->io_width = root->sectorsize;
1733 	device->io_align = root->sectorsize;
1734 	device->sector_size = root->sectorsize;
1735 	device->total_bytes = i_size_read(bdev->bd_inode);
1736 	device->disk_total_bytes = device->total_bytes;
1737 	device->dev_root = root->fs_info->dev_root;
1738 	device->bdev = bdev;
1739 	device->in_fs_metadata = 1;
1740 	device->mode = FMODE_EXCL;
1741 	set_blocksize(device->bdev, 4096);
1742 
1743 	if (seeding_dev) {
1744 		sb->s_flags &= ~MS_RDONLY;
1745 		ret = btrfs_prepare_sprout(root);
1746 		BUG_ON(ret); /* -ENOMEM */
1747 	}
1748 
1749 	device->fs_devices = root->fs_info->fs_devices;
1750 
1751 	mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1752 	list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
1753 	list_add(&device->dev_alloc_list,
1754 		 &root->fs_info->fs_devices->alloc_list);
1755 	root->fs_info->fs_devices->num_devices++;
1756 	root->fs_info->fs_devices->open_devices++;
1757 	root->fs_info->fs_devices->rw_devices++;
1758 	root->fs_info->fs_devices->total_devices++;
1759 	if (device->can_discard)
1760 		root->fs_info->fs_devices->num_can_discard++;
1761 	root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1762 
1763 	spin_lock(&root->fs_info->free_chunk_lock);
1764 	root->fs_info->free_chunk_space += device->total_bytes;
1765 	spin_unlock(&root->fs_info->free_chunk_lock);
1766 
1767 	if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1768 		root->fs_info->fs_devices->rotating = 1;
1769 
1770 	total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1771 	btrfs_set_super_total_bytes(root->fs_info->super_copy,
1772 				    total_bytes + device->total_bytes);
1773 
1774 	total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1775 	btrfs_set_super_num_devices(root->fs_info->super_copy,
1776 				    total_bytes + 1);
1777 	mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1778 
1779 	if (seeding_dev) {
1780 		ret = init_first_rw_device(trans, root, device);
1781 		if (ret) {
1782 			btrfs_abort_transaction(trans, root, ret);
1783 			goto error_trans;
1784 		}
1785 		ret = btrfs_finish_sprout(trans, root);
1786 		if (ret) {
1787 			btrfs_abort_transaction(trans, root, ret);
1788 			goto error_trans;
1789 		}
1790 	} else {
1791 		ret = btrfs_add_device(trans, root, device);
1792 		if (ret) {
1793 			btrfs_abort_transaction(trans, root, ret);
1794 			goto error_trans;
1795 		}
1796 	}
1797 
1798 	/*
1799 	 * we've got more storage, clear any full flags on the space
1800 	 * infos
1801 	 */
1802 	btrfs_clear_space_info_full(root->fs_info);
1803 
1804 	unlock_chunks(root);
1805 	root->fs_info->num_tolerated_disk_barrier_failures =
1806 		btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1807 	ret = btrfs_commit_transaction(trans, root);
1808 
1809 	if (seeding_dev) {
1810 		mutex_unlock(&uuid_mutex);
1811 		up_write(&sb->s_umount);
1812 
1813 		if (ret) /* transaction commit */
1814 			return ret;
1815 
1816 		ret = btrfs_relocate_sys_chunks(root);
1817 		if (ret < 0)
1818 			btrfs_error(root->fs_info, ret,
1819 				    "Failed to relocate sys chunks after "
1820 				    "device initialization. This can be fixed "
1821 				    "using the \"btrfs balance\" command.");
1822 		trans = btrfs_attach_transaction(root);
1823 		if (IS_ERR(trans)) {
1824 			if (PTR_ERR(trans) == -ENOENT)
1825 				return 0;
1826 			return PTR_ERR(trans);
1827 		}
1828 		ret = btrfs_commit_transaction(trans, root);
1829 	}
1830 
1831 	return ret;
1832 
1833 error_trans:
1834 	unlock_chunks(root);
1835 	btrfs_end_transaction(trans, root);
1836 	rcu_string_free(device->name);
1837 	kfree(device);
1838 error:
1839 	blkdev_put(bdev, FMODE_EXCL);
1840 	if (seeding_dev) {
1841 		mutex_unlock(&uuid_mutex);
1842 		up_write(&sb->s_umount);
1843 	}
1844 	return ret;
1845 }
1846 
1847 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1848 					struct btrfs_device *device)
1849 {
1850 	int ret;
1851 	struct btrfs_path *path;
1852 	struct btrfs_root *root;
1853 	struct btrfs_dev_item *dev_item;
1854 	struct extent_buffer *leaf;
1855 	struct btrfs_key key;
1856 
1857 	root = device->dev_root->fs_info->chunk_root;
1858 
1859 	path = btrfs_alloc_path();
1860 	if (!path)
1861 		return -ENOMEM;
1862 
1863 	key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1864 	key.type = BTRFS_DEV_ITEM_KEY;
1865 	key.offset = device->devid;
1866 
1867 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1868 	if (ret < 0)
1869 		goto out;
1870 
1871 	if (ret > 0) {
1872 		ret = -ENOENT;
1873 		goto out;
1874 	}
1875 
1876 	leaf = path->nodes[0];
1877 	dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1878 
1879 	btrfs_set_device_id(leaf, dev_item, device->devid);
1880 	btrfs_set_device_type(leaf, dev_item, device->type);
1881 	btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1882 	btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1883 	btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1884 	btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
1885 	btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1886 	btrfs_mark_buffer_dirty(leaf);
1887 
1888 out:
1889 	btrfs_free_path(path);
1890 	return ret;
1891 }
1892 
1893 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1894 		      struct btrfs_device *device, u64 new_size)
1895 {
1896 	struct btrfs_super_block *super_copy =
1897 		device->dev_root->fs_info->super_copy;
1898 	u64 old_total = btrfs_super_total_bytes(super_copy);
1899 	u64 diff = new_size - device->total_bytes;
1900 
1901 	if (!device->writeable)
1902 		return -EACCES;
1903 	if (new_size <= device->total_bytes)
1904 		return -EINVAL;
1905 
1906 	btrfs_set_super_total_bytes(super_copy, old_total + diff);
1907 	device->fs_devices->total_rw_bytes += diff;
1908 
1909 	device->total_bytes = new_size;
1910 	device->disk_total_bytes = new_size;
1911 	btrfs_clear_space_info_full(device->dev_root->fs_info);
1912 
1913 	return btrfs_update_device(trans, device);
1914 }
1915 
1916 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1917 		      struct btrfs_device *device, u64 new_size)
1918 {
1919 	int ret;
1920 	lock_chunks(device->dev_root);
1921 	ret = __btrfs_grow_device(trans, device, new_size);
1922 	unlock_chunks(device->dev_root);
1923 	return ret;
1924 }
1925 
1926 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1927 			    struct btrfs_root *root,
1928 			    u64 chunk_tree, u64 chunk_objectid,
1929 			    u64 chunk_offset)
1930 {
1931 	int ret;
1932 	struct btrfs_path *path;
1933 	struct btrfs_key key;
1934 
1935 	root = root->fs_info->chunk_root;
1936 	path = btrfs_alloc_path();
1937 	if (!path)
1938 		return -ENOMEM;
1939 
1940 	key.objectid = chunk_objectid;
1941 	key.offset = chunk_offset;
1942 	key.type = BTRFS_CHUNK_ITEM_KEY;
1943 
1944 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1945 	if (ret < 0)
1946 		goto out;
1947 	else if (ret > 0) { /* Logic error or corruption */
1948 		btrfs_error(root->fs_info, -ENOENT,
1949 			    "Failed lookup while freeing chunk.");
1950 		ret = -ENOENT;
1951 		goto out;
1952 	}
1953 
1954 	ret = btrfs_del_item(trans, root, path);
1955 	if (ret < 0)
1956 		btrfs_error(root->fs_info, ret,
1957 			    "Failed to delete chunk item.");
1958 out:
1959 	btrfs_free_path(path);
1960 	return ret;
1961 }
1962 
1963 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1964 			chunk_offset)
1965 {
1966 	struct btrfs_super_block *super_copy = root->fs_info->super_copy;
1967 	struct btrfs_disk_key *disk_key;
1968 	struct btrfs_chunk *chunk;
1969 	u8 *ptr;
1970 	int ret = 0;
1971 	u32 num_stripes;
1972 	u32 array_size;
1973 	u32 len = 0;
1974 	u32 cur;
1975 	struct btrfs_key key;
1976 
1977 	array_size = btrfs_super_sys_array_size(super_copy);
1978 
1979 	ptr = super_copy->sys_chunk_array;
1980 	cur = 0;
1981 
1982 	while (cur < array_size) {
1983 		disk_key = (struct btrfs_disk_key *)ptr;
1984 		btrfs_disk_key_to_cpu(&key, disk_key);
1985 
1986 		len = sizeof(*disk_key);
1987 
1988 		if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1989 			chunk = (struct btrfs_chunk *)(ptr + len);
1990 			num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1991 			len += btrfs_chunk_item_size(num_stripes);
1992 		} else {
1993 			ret = -EIO;
1994 			break;
1995 		}
1996 		if (key.objectid == chunk_objectid &&
1997 		    key.offset == chunk_offset) {
1998 			memmove(ptr, ptr + len, array_size - (cur + len));
1999 			array_size -= len;
2000 			btrfs_set_super_sys_array_size(super_copy, array_size);
2001 		} else {
2002 			ptr += len;
2003 			cur += len;
2004 		}
2005 	}
2006 	return ret;
2007 }
2008 
2009 static int btrfs_relocate_chunk(struct btrfs_root *root,
2010 			 u64 chunk_tree, u64 chunk_objectid,
2011 			 u64 chunk_offset)
2012 {
2013 	struct extent_map_tree *em_tree;
2014 	struct btrfs_root *extent_root;
2015 	struct btrfs_trans_handle *trans;
2016 	struct extent_map *em;
2017 	struct map_lookup *map;
2018 	int ret;
2019 	int i;
2020 
2021 	root = root->fs_info->chunk_root;
2022 	extent_root = root->fs_info->extent_root;
2023 	em_tree = &root->fs_info->mapping_tree.map_tree;
2024 
2025 	ret = btrfs_can_relocate(extent_root, chunk_offset);
2026 	if (ret)
2027 		return -ENOSPC;
2028 
2029 	/* step one, relocate all the extents inside this chunk */
2030 	ret = btrfs_relocate_block_group(extent_root, chunk_offset);
2031 	if (ret)
2032 		return ret;
2033 
2034 	trans = btrfs_start_transaction(root, 0);
2035 	BUG_ON(IS_ERR(trans));
2036 
2037 	lock_chunks(root);
2038 
2039 	/*
2040 	 * step two, delete the device extents and the
2041 	 * chunk tree entries
2042 	 */
2043 	read_lock(&em_tree->lock);
2044 	em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2045 	read_unlock(&em_tree->lock);
2046 
2047 	BUG_ON(!em || em->start > chunk_offset ||
2048 	       em->start + em->len < chunk_offset);
2049 	map = (struct map_lookup *)em->bdev;
2050 
2051 	for (i = 0; i < map->num_stripes; i++) {
2052 		ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2053 					    map->stripes[i].physical);
2054 		BUG_ON(ret);
2055 
2056 		if (map->stripes[i].dev) {
2057 			ret = btrfs_update_device(trans, map->stripes[i].dev);
2058 			BUG_ON(ret);
2059 		}
2060 	}
2061 	ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2062 			       chunk_offset);
2063 
2064 	BUG_ON(ret);
2065 
2066 	trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2067 
2068 	if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2069 		ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2070 		BUG_ON(ret);
2071 	}
2072 
2073 	ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2074 	BUG_ON(ret);
2075 
2076 	write_lock(&em_tree->lock);
2077 	remove_extent_mapping(em_tree, em);
2078 	write_unlock(&em_tree->lock);
2079 
2080 	kfree(map);
2081 	em->bdev = NULL;
2082 
2083 	/* once for the tree */
2084 	free_extent_map(em);
2085 	/* once for us */
2086 	free_extent_map(em);
2087 
2088 	unlock_chunks(root);
2089 	btrfs_end_transaction(trans, root);
2090 	return 0;
2091 }
2092 
2093 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2094 {
2095 	struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2096 	struct btrfs_path *path;
2097 	struct extent_buffer *leaf;
2098 	struct btrfs_chunk *chunk;
2099 	struct btrfs_key key;
2100 	struct btrfs_key found_key;
2101 	u64 chunk_tree = chunk_root->root_key.objectid;
2102 	u64 chunk_type;
2103 	bool retried = false;
2104 	int failed = 0;
2105 	int ret;
2106 
2107 	path = btrfs_alloc_path();
2108 	if (!path)
2109 		return -ENOMEM;
2110 
2111 again:
2112 	key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2113 	key.offset = (u64)-1;
2114 	key.type = BTRFS_CHUNK_ITEM_KEY;
2115 
2116 	while (1) {
2117 		ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2118 		if (ret < 0)
2119 			goto error;
2120 		BUG_ON(ret == 0); /* Corruption */
2121 
2122 		ret = btrfs_previous_item(chunk_root, path, key.objectid,
2123 					  key.type);
2124 		if (ret < 0)
2125 			goto error;
2126 		if (ret > 0)
2127 			break;
2128 
2129 		leaf = path->nodes[0];
2130 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2131 
2132 		chunk = btrfs_item_ptr(leaf, path->slots[0],
2133 				       struct btrfs_chunk);
2134 		chunk_type = btrfs_chunk_type(leaf, chunk);
2135 		btrfs_release_path(path);
2136 
2137 		if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2138 			ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2139 						   found_key.objectid,
2140 						   found_key.offset);
2141 			if (ret == -ENOSPC)
2142 				failed++;
2143 			else if (ret)
2144 				BUG();
2145 		}
2146 
2147 		if (found_key.offset == 0)
2148 			break;
2149 		key.offset = found_key.offset - 1;
2150 	}
2151 	ret = 0;
2152 	if (failed && !retried) {
2153 		failed = 0;
2154 		retried = true;
2155 		goto again;
2156 	} else if (failed && retried) {
2157 		WARN_ON(1);
2158 		ret = -ENOSPC;
2159 	}
2160 error:
2161 	btrfs_free_path(path);
2162 	return ret;
2163 }
2164 
2165 static int insert_balance_item(struct btrfs_root *root,
2166 			       struct btrfs_balance_control *bctl)
2167 {
2168 	struct btrfs_trans_handle *trans;
2169 	struct btrfs_balance_item *item;
2170 	struct btrfs_disk_balance_args disk_bargs;
2171 	struct btrfs_path *path;
2172 	struct extent_buffer *leaf;
2173 	struct btrfs_key key;
2174 	int ret, err;
2175 
2176 	path = btrfs_alloc_path();
2177 	if (!path)
2178 		return -ENOMEM;
2179 
2180 	trans = btrfs_start_transaction(root, 0);
2181 	if (IS_ERR(trans)) {
2182 		btrfs_free_path(path);
2183 		return PTR_ERR(trans);
2184 	}
2185 
2186 	key.objectid = BTRFS_BALANCE_OBJECTID;
2187 	key.type = BTRFS_BALANCE_ITEM_KEY;
2188 	key.offset = 0;
2189 
2190 	ret = btrfs_insert_empty_item(trans, root, path, &key,
2191 				      sizeof(*item));
2192 	if (ret)
2193 		goto out;
2194 
2195 	leaf = path->nodes[0];
2196 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2197 
2198 	memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2199 
2200 	btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2201 	btrfs_set_balance_data(leaf, item, &disk_bargs);
2202 	btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2203 	btrfs_set_balance_meta(leaf, item, &disk_bargs);
2204 	btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2205 	btrfs_set_balance_sys(leaf, item, &disk_bargs);
2206 
2207 	btrfs_set_balance_flags(leaf, item, bctl->flags);
2208 
2209 	btrfs_mark_buffer_dirty(leaf);
2210 out:
2211 	btrfs_free_path(path);
2212 	err = btrfs_commit_transaction(trans, root);
2213 	if (err && !ret)
2214 		ret = err;
2215 	return ret;
2216 }
2217 
2218 static int del_balance_item(struct btrfs_root *root)
2219 {
2220 	struct btrfs_trans_handle *trans;
2221 	struct btrfs_path *path;
2222 	struct btrfs_key key;
2223 	int ret, err;
2224 
2225 	path = btrfs_alloc_path();
2226 	if (!path)
2227 		return -ENOMEM;
2228 
2229 	trans = btrfs_start_transaction(root, 0);
2230 	if (IS_ERR(trans)) {
2231 		btrfs_free_path(path);
2232 		return PTR_ERR(trans);
2233 	}
2234 
2235 	key.objectid = BTRFS_BALANCE_OBJECTID;
2236 	key.type = BTRFS_BALANCE_ITEM_KEY;
2237 	key.offset = 0;
2238 
2239 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2240 	if (ret < 0)
2241 		goto out;
2242 	if (ret > 0) {
2243 		ret = -ENOENT;
2244 		goto out;
2245 	}
2246 
2247 	ret = btrfs_del_item(trans, root, path);
2248 out:
2249 	btrfs_free_path(path);
2250 	err = btrfs_commit_transaction(trans, root);
2251 	if (err && !ret)
2252 		ret = err;
2253 	return ret;
2254 }
2255 
2256 /*
2257  * This is a heuristic used to reduce the number of chunks balanced on
2258  * resume after balance was interrupted.
2259  */
2260 static void update_balance_args(struct btrfs_balance_control *bctl)
2261 {
2262 	/*
2263 	 * Turn on soft mode for chunk types that were being converted.
2264 	 */
2265 	if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2266 		bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2267 	if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2268 		bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2269 	if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2270 		bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2271 
2272 	/*
2273 	 * Turn on usage filter if is not already used.  The idea is
2274 	 * that chunks that we have already balanced should be
2275 	 * reasonably full.  Don't do it for chunks that are being
2276 	 * converted - that will keep us from relocating unconverted
2277 	 * (albeit full) chunks.
2278 	 */
2279 	if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2280 	    !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2281 		bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2282 		bctl->data.usage = 90;
2283 	}
2284 	if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2285 	    !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2286 		bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2287 		bctl->sys.usage = 90;
2288 	}
2289 	if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2290 	    !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2291 		bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2292 		bctl->meta.usage = 90;
2293 	}
2294 }
2295 
2296 /*
2297  * Should be called with both balance and volume mutexes held to
2298  * serialize other volume operations (add_dev/rm_dev/resize) with
2299  * restriper.  Same goes for unset_balance_control.
2300  */
2301 static void set_balance_control(struct btrfs_balance_control *bctl)
2302 {
2303 	struct btrfs_fs_info *fs_info = bctl->fs_info;
2304 
2305 	BUG_ON(fs_info->balance_ctl);
2306 
2307 	spin_lock(&fs_info->balance_lock);
2308 	fs_info->balance_ctl = bctl;
2309 	spin_unlock(&fs_info->balance_lock);
2310 }
2311 
2312 static void unset_balance_control(struct btrfs_fs_info *fs_info)
2313 {
2314 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2315 
2316 	BUG_ON(!fs_info->balance_ctl);
2317 
2318 	spin_lock(&fs_info->balance_lock);
2319 	fs_info->balance_ctl = NULL;
2320 	spin_unlock(&fs_info->balance_lock);
2321 
2322 	kfree(bctl);
2323 }
2324 
2325 /*
2326  * Balance filters.  Return 1 if chunk should be filtered out
2327  * (should not be balanced).
2328  */
2329 static int chunk_profiles_filter(u64 chunk_type,
2330 				 struct btrfs_balance_args *bargs)
2331 {
2332 	chunk_type = chunk_to_extended(chunk_type) &
2333 				BTRFS_EXTENDED_PROFILE_MASK;
2334 
2335 	if (bargs->profiles & chunk_type)
2336 		return 0;
2337 
2338 	return 1;
2339 }
2340 
2341 static u64 div_factor_fine(u64 num, int factor)
2342 {
2343 	if (factor <= 0)
2344 		return 0;
2345 	if (factor >= 100)
2346 		return num;
2347 
2348 	num *= factor;
2349 	do_div(num, 100);
2350 	return num;
2351 }
2352 
2353 static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2354 			      struct btrfs_balance_args *bargs)
2355 {
2356 	struct btrfs_block_group_cache *cache;
2357 	u64 chunk_used, user_thresh;
2358 	int ret = 1;
2359 
2360 	cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2361 	chunk_used = btrfs_block_group_used(&cache->item);
2362 
2363 	user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2364 	if (chunk_used < user_thresh)
2365 		ret = 0;
2366 
2367 	btrfs_put_block_group(cache);
2368 	return ret;
2369 }
2370 
2371 static int chunk_devid_filter(struct extent_buffer *leaf,
2372 			      struct btrfs_chunk *chunk,
2373 			      struct btrfs_balance_args *bargs)
2374 {
2375 	struct btrfs_stripe *stripe;
2376 	int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2377 	int i;
2378 
2379 	for (i = 0; i < num_stripes; i++) {
2380 		stripe = btrfs_stripe_nr(chunk, i);
2381 		if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2382 			return 0;
2383 	}
2384 
2385 	return 1;
2386 }
2387 
2388 /* [pstart, pend) */
2389 static int chunk_drange_filter(struct extent_buffer *leaf,
2390 			       struct btrfs_chunk *chunk,
2391 			       u64 chunk_offset,
2392 			       struct btrfs_balance_args *bargs)
2393 {
2394 	struct btrfs_stripe *stripe;
2395 	int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2396 	u64 stripe_offset;
2397 	u64 stripe_length;
2398 	int factor;
2399 	int i;
2400 
2401 	if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2402 		return 0;
2403 
2404 	if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2405 	     BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2406 		factor = 2;
2407 	else
2408 		factor = 1;
2409 	factor = num_stripes / factor;
2410 
2411 	for (i = 0; i < num_stripes; i++) {
2412 		stripe = btrfs_stripe_nr(chunk, i);
2413 		if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2414 			continue;
2415 
2416 		stripe_offset = btrfs_stripe_offset(leaf, stripe);
2417 		stripe_length = btrfs_chunk_length(leaf, chunk);
2418 		do_div(stripe_length, factor);
2419 
2420 		if (stripe_offset < bargs->pend &&
2421 		    stripe_offset + stripe_length > bargs->pstart)
2422 			return 0;
2423 	}
2424 
2425 	return 1;
2426 }
2427 
2428 /* [vstart, vend) */
2429 static int chunk_vrange_filter(struct extent_buffer *leaf,
2430 			       struct btrfs_chunk *chunk,
2431 			       u64 chunk_offset,
2432 			       struct btrfs_balance_args *bargs)
2433 {
2434 	if (chunk_offset < bargs->vend &&
2435 	    chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2436 		/* at least part of the chunk is inside this vrange */
2437 		return 0;
2438 
2439 	return 1;
2440 }
2441 
2442 static int chunk_soft_convert_filter(u64 chunk_type,
2443 				     struct btrfs_balance_args *bargs)
2444 {
2445 	if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2446 		return 0;
2447 
2448 	chunk_type = chunk_to_extended(chunk_type) &
2449 				BTRFS_EXTENDED_PROFILE_MASK;
2450 
2451 	if (bargs->target == chunk_type)
2452 		return 1;
2453 
2454 	return 0;
2455 }
2456 
2457 static int should_balance_chunk(struct btrfs_root *root,
2458 				struct extent_buffer *leaf,
2459 				struct btrfs_chunk *chunk, u64 chunk_offset)
2460 {
2461 	struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2462 	struct btrfs_balance_args *bargs = NULL;
2463 	u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2464 
2465 	/* type filter */
2466 	if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2467 	      (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2468 		return 0;
2469 	}
2470 
2471 	if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2472 		bargs = &bctl->data;
2473 	else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2474 		bargs = &bctl->sys;
2475 	else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2476 		bargs = &bctl->meta;
2477 
2478 	/* profiles filter */
2479 	if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2480 	    chunk_profiles_filter(chunk_type, bargs)) {
2481 		return 0;
2482 	}
2483 
2484 	/* usage filter */
2485 	if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2486 	    chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2487 		return 0;
2488 	}
2489 
2490 	/* devid filter */
2491 	if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2492 	    chunk_devid_filter(leaf, chunk, bargs)) {
2493 		return 0;
2494 	}
2495 
2496 	/* drange filter, makes sense only with devid filter */
2497 	if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2498 	    chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2499 		return 0;
2500 	}
2501 
2502 	/* vrange filter */
2503 	if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2504 	    chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2505 		return 0;
2506 	}
2507 
2508 	/* soft profile changing mode */
2509 	if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2510 	    chunk_soft_convert_filter(chunk_type, bargs)) {
2511 		return 0;
2512 	}
2513 
2514 	return 1;
2515 }
2516 
2517 static u64 div_factor(u64 num, int factor)
2518 {
2519 	if (factor == 10)
2520 		return num;
2521 	num *= factor;
2522 	do_div(num, 10);
2523 	return num;
2524 }
2525 
2526 static int __btrfs_balance(struct btrfs_fs_info *fs_info)
2527 {
2528 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2529 	struct btrfs_root *chunk_root = fs_info->chunk_root;
2530 	struct btrfs_root *dev_root = fs_info->dev_root;
2531 	struct list_head *devices;
2532 	struct btrfs_device *device;
2533 	u64 old_size;
2534 	u64 size_to_free;
2535 	struct btrfs_chunk *chunk;
2536 	struct btrfs_path *path;
2537 	struct btrfs_key key;
2538 	struct btrfs_key found_key;
2539 	struct btrfs_trans_handle *trans;
2540 	struct extent_buffer *leaf;
2541 	int slot;
2542 	int ret;
2543 	int enospc_errors = 0;
2544 	bool counting = true;
2545 
2546 	/* step one make some room on all the devices */
2547 	devices = &fs_info->fs_devices->devices;
2548 	list_for_each_entry(device, devices, dev_list) {
2549 		old_size = device->total_bytes;
2550 		size_to_free = div_factor(old_size, 1);
2551 		size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2552 		if (!device->writeable ||
2553 		    device->total_bytes - device->bytes_used > size_to_free)
2554 			continue;
2555 
2556 		ret = btrfs_shrink_device(device, old_size - size_to_free);
2557 		if (ret == -ENOSPC)
2558 			break;
2559 		BUG_ON(ret);
2560 
2561 		trans = btrfs_start_transaction(dev_root, 0);
2562 		BUG_ON(IS_ERR(trans));
2563 
2564 		ret = btrfs_grow_device(trans, device, old_size);
2565 		BUG_ON(ret);
2566 
2567 		btrfs_end_transaction(trans, dev_root);
2568 	}
2569 
2570 	/* step two, relocate all the chunks */
2571 	path = btrfs_alloc_path();
2572 	if (!path) {
2573 		ret = -ENOMEM;
2574 		goto error;
2575 	}
2576 
2577 	/* zero out stat counters */
2578 	spin_lock(&fs_info->balance_lock);
2579 	memset(&bctl->stat, 0, sizeof(bctl->stat));
2580 	spin_unlock(&fs_info->balance_lock);
2581 again:
2582 	key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2583 	key.offset = (u64)-1;
2584 	key.type = BTRFS_CHUNK_ITEM_KEY;
2585 
2586 	while (1) {
2587 		if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
2588 		    atomic_read(&fs_info->balance_cancel_req)) {
2589 			ret = -ECANCELED;
2590 			goto error;
2591 		}
2592 
2593 		ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2594 		if (ret < 0)
2595 			goto error;
2596 
2597 		/*
2598 		 * this shouldn't happen, it means the last relocate
2599 		 * failed
2600 		 */
2601 		if (ret == 0)
2602 			BUG(); /* FIXME break ? */
2603 
2604 		ret = btrfs_previous_item(chunk_root, path, 0,
2605 					  BTRFS_CHUNK_ITEM_KEY);
2606 		if (ret) {
2607 			ret = 0;
2608 			break;
2609 		}
2610 
2611 		leaf = path->nodes[0];
2612 		slot = path->slots[0];
2613 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
2614 
2615 		if (found_key.objectid != key.objectid)
2616 			break;
2617 
2618 		/* chunk zero is special */
2619 		if (found_key.offset == 0)
2620 			break;
2621 
2622 		chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2623 
2624 		if (!counting) {
2625 			spin_lock(&fs_info->balance_lock);
2626 			bctl->stat.considered++;
2627 			spin_unlock(&fs_info->balance_lock);
2628 		}
2629 
2630 		ret = should_balance_chunk(chunk_root, leaf, chunk,
2631 					   found_key.offset);
2632 		btrfs_release_path(path);
2633 		if (!ret)
2634 			goto loop;
2635 
2636 		if (counting) {
2637 			spin_lock(&fs_info->balance_lock);
2638 			bctl->stat.expected++;
2639 			spin_unlock(&fs_info->balance_lock);
2640 			goto loop;
2641 		}
2642 
2643 		ret = btrfs_relocate_chunk(chunk_root,
2644 					   chunk_root->root_key.objectid,
2645 					   found_key.objectid,
2646 					   found_key.offset);
2647 		if (ret && ret != -ENOSPC)
2648 			goto error;
2649 		if (ret == -ENOSPC) {
2650 			enospc_errors++;
2651 		} else {
2652 			spin_lock(&fs_info->balance_lock);
2653 			bctl->stat.completed++;
2654 			spin_unlock(&fs_info->balance_lock);
2655 		}
2656 loop:
2657 		key.offset = found_key.offset - 1;
2658 	}
2659 
2660 	if (counting) {
2661 		btrfs_release_path(path);
2662 		counting = false;
2663 		goto again;
2664 	}
2665 error:
2666 	btrfs_free_path(path);
2667 	if (enospc_errors) {
2668 		printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2669 		       enospc_errors);
2670 		if (!ret)
2671 			ret = -ENOSPC;
2672 	}
2673 
2674 	return ret;
2675 }
2676 
2677 /**
2678  * alloc_profile_is_valid - see if a given profile is valid and reduced
2679  * @flags: profile to validate
2680  * @extended: if true @flags is treated as an extended profile
2681  */
2682 static int alloc_profile_is_valid(u64 flags, int extended)
2683 {
2684 	u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2685 			       BTRFS_BLOCK_GROUP_PROFILE_MASK);
2686 
2687 	flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2688 
2689 	/* 1) check that all other bits are zeroed */
2690 	if (flags & ~mask)
2691 		return 0;
2692 
2693 	/* 2) see if profile is reduced */
2694 	if (flags == 0)
2695 		return !extended; /* "0" is valid for usual profiles */
2696 
2697 	/* true if exactly one bit set */
2698 	return (flags & (flags - 1)) == 0;
2699 }
2700 
2701 static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2702 {
2703 	/* cancel requested || normal exit path */
2704 	return atomic_read(&fs_info->balance_cancel_req) ||
2705 		(atomic_read(&fs_info->balance_pause_req) == 0 &&
2706 		 atomic_read(&fs_info->balance_cancel_req) == 0);
2707 }
2708 
2709 static void __cancel_balance(struct btrfs_fs_info *fs_info)
2710 {
2711 	int ret;
2712 
2713 	unset_balance_control(fs_info);
2714 	ret = del_balance_item(fs_info->tree_root);
2715 	BUG_ON(ret);
2716 }
2717 
2718 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
2719 			       struct btrfs_ioctl_balance_args *bargs);
2720 
2721 /*
2722  * Should be called with both balance and volume mutexes held
2723  */
2724 int btrfs_balance(struct btrfs_balance_control *bctl,
2725 		  struct btrfs_ioctl_balance_args *bargs)
2726 {
2727 	struct btrfs_fs_info *fs_info = bctl->fs_info;
2728 	u64 allowed;
2729 	int mixed = 0;
2730 	int ret;
2731 
2732 	if (btrfs_fs_closing(fs_info) ||
2733 	    atomic_read(&fs_info->balance_pause_req) ||
2734 	    atomic_read(&fs_info->balance_cancel_req)) {
2735 		ret = -EINVAL;
2736 		goto out;
2737 	}
2738 
2739 	allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2740 	if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2741 		mixed = 1;
2742 
2743 	/*
2744 	 * In case of mixed groups both data and meta should be picked,
2745 	 * and identical options should be given for both of them.
2746 	 */
2747 	allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2748 	if (mixed && (bctl->flags & allowed)) {
2749 		if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2750 		    !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2751 		    memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2752 			printk(KERN_ERR "btrfs: with mixed groups data and "
2753 			       "metadata balance options must be the same\n");
2754 			ret = -EINVAL;
2755 			goto out;
2756 		}
2757 	}
2758 
2759 	allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2760 	if (fs_info->fs_devices->num_devices == 1)
2761 		allowed |= BTRFS_BLOCK_GROUP_DUP;
2762 	else if (fs_info->fs_devices->num_devices < 4)
2763 		allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2764 	else
2765 		allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2766 				BTRFS_BLOCK_GROUP_RAID10);
2767 
2768 	if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2769 	    (!alloc_profile_is_valid(bctl->data.target, 1) ||
2770 	     (bctl->data.target & ~allowed))) {
2771 		printk(KERN_ERR "btrfs: unable to start balance with target "
2772 		       "data profile %llu\n",
2773 		       (unsigned long long)bctl->data.target);
2774 		ret = -EINVAL;
2775 		goto out;
2776 	}
2777 	if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2778 	    (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2779 	     (bctl->meta.target & ~allowed))) {
2780 		printk(KERN_ERR "btrfs: unable to start balance with target "
2781 		       "metadata profile %llu\n",
2782 		       (unsigned long long)bctl->meta.target);
2783 		ret = -EINVAL;
2784 		goto out;
2785 	}
2786 	if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2787 	    (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2788 	     (bctl->sys.target & ~allowed))) {
2789 		printk(KERN_ERR "btrfs: unable to start balance with target "
2790 		       "system profile %llu\n",
2791 		       (unsigned long long)bctl->sys.target);
2792 		ret = -EINVAL;
2793 		goto out;
2794 	}
2795 
2796 	/* allow dup'ed data chunks only in mixed mode */
2797 	if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2798 	    (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
2799 		printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2800 		ret = -EINVAL;
2801 		goto out;
2802 	}
2803 
2804 	/* allow to reduce meta or sys integrity only if force set */
2805 	allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2806 			BTRFS_BLOCK_GROUP_RAID10;
2807 	if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2808 	     (fs_info->avail_system_alloc_bits & allowed) &&
2809 	     !(bctl->sys.target & allowed)) ||
2810 	    ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2811 	     (fs_info->avail_metadata_alloc_bits & allowed) &&
2812 	     !(bctl->meta.target & allowed))) {
2813 		if (bctl->flags & BTRFS_BALANCE_FORCE) {
2814 			printk(KERN_INFO "btrfs: force reducing metadata "
2815 			       "integrity\n");
2816 		} else {
2817 			printk(KERN_ERR "btrfs: balance will reduce metadata "
2818 			       "integrity, use force if you want this\n");
2819 			ret = -EINVAL;
2820 			goto out;
2821 		}
2822 	}
2823 
2824 	if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
2825 		int num_tolerated_disk_barrier_failures;
2826 		u64 target = bctl->sys.target;
2827 
2828 		num_tolerated_disk_barrier_failures =
2829 			btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
2830 		if (num_tolerated_disk_barrier_failures > 0 &&
2831 		    (target &
2832 		     (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
2833 		      BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
2834 			num_tolerated_disk_barrier_failures = 0;
2835 		else if (num_tolerated_disk_barrier_failures > 1 &&
2836 			 (target &
2837 			  (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
2838 			num_tolerated_disk_barrier_failures = 1;
2839 
2840 		fs_info->num_tolerated_disk_barrier_failures =
2841 			num_tolerated_disk_barrier_failures;
2842 	}
2843 
2844 	ret = insert_balance_item(fs_info->tree_root, bctl);
2845 	if (ret && ret != -EEXIST)
2846 		goto out;
2847 
2848 	if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2849 		BUG_ON(ret == -EEXIST);
2850 		set_balance_control(bctl);
2851 	} else {
2852 		BUG_ON(ret != -EEXIST);
2853 		spin_lock(&fs_info->balance_lock);
2854 		update_balance_args(bctl);
2855 		spin_unlock(&fs_info->balance_lock);
2856 	}
2857 
2858 	atomic_inc(&fs_info->balance_running);
2859 	mutex_unlock(&fs_info->balance_mutex);
2860 
2861 	ret = __btrfs_balance(fs_info);
2862 
2863 	mutex_lock(&fs_info->balance_mutex);
2864 	atomic_dec(&fs_info->balance_running);
2865 
2866 	if (bargs) {
2867 		memset(bargs, 0, sizeof(*bargs));
2868 		update_ioctl_balance_args(fs_info, 0, bargs);
2869 	}
2870 
2871 	if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2872 	    balance_need_close(fs_info)) {
2873 		__cancel_balance(fs_info);
2874 	}
2875 
2876 	if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
2877 		fs_info->num_tolerated_disk_barrier_failures =
2878 			btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
2879 	}
2880 
2881 	wake_up(&fs_info->balance_wait_q);
2882 
2883 	return ret;
2884 out:
2885 	if (bctl->flags & BTRFS_BALANCE_RESUME)
2886 		__cancel_balance(fs_info);
2887 	else
2888 		kfree(bctl);
2889 	return ret;
2890 }
2891 
2892 static int balance_kthread(void *data)
2893 {
2894 	struct btrfs_fs_info *fs_info = data;
2895 	int ret = 0;
2896 
2897 	mutex_lock(&fs_info->volume_mutex);
2898 	mutex_lock(&fs_info->balance_mutex);
2899 
2900 	if (fs_info->balance_ctl) {
2901 		printk(KERN_INFO "btrfs: continuing balance\n");
2902 		ret = btrfs_balance(fs_info->balance_ctl, NULL);
2903 	}
2904 
2905 	mutex_unlock(&fs_info->balance_mutex);
2906 	mutex_unlock(&fs_info->volume_mutex);
2907 
2908 	return ret;
2909 }
2910 
2911 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
2912 {
2913 	struct task_struct *tsk;
2914 
2915 	spin_lock(&fs_info->balance_lock);
2916 	if (!fs_info->balance_ctl) {
2917 		spin_unlock(&fs_info->balance_lock);
2918 		return 0;
2919 	}
2920 	spin_unlock(&fs_info->balance_lock);
2921 
2922 	if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2923 		printk(KERN_INFO "btrfs: force skipping balance\n");
2924 		return 0;
2925 	}
2926 
2927 	tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
2928 	if (IS_ERR(tsk))
2929 		return PTR_ERR(tsk);
2930 
2931 	return 0;
2932 }
2933 
2934 int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
2935 {
2936 	struct btrfs_balance_control *bctl;
2937 	struct btrfs_balance_item *item;
2938 	struct btrfs_disk_balance_args disk_bargs;
2939 	struct btrfs_path *path;
2940 	struct extent_buffer *leaf;
2941 	struct btrfs_key key;
2942 	int ret;
2943 
2944 	path = btrfs_alloc_path();
2945 	if (!path)
2946 		return -ENOMEM;
2947 
2948 	key.objectid = BTRFS_BALANCE_OBJECTID;
2949 	key.type = BTRFS_BALANCE_ITEM_KEY;
2950 	key.offset = 0;
2951 
2952 	ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2953 	if (ret < 0)
2954 		goto out;
2955 	if (ret > 0) { /* ret = -ENOENT; */
2956 		ret = 0;
2957 		goto out;
2958 	}
2959 
2960 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2961 	if (!bctl) {
2962 		ret = -ENOMEM;
2963 		goto out;
2964 	}
2965 
2966 	leaf = path->nodes[0];
2967 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2968 
2969 	bctl->fs_info = fs_info;
2970 	bctl->flags = btrfs_balance_flags(leaf, item);
2971 	bctl->flags |= BTRFS_BALANCE_RESUME;
2972 
2973 	btrfs_balance_data(leaf, item, &disk_bargs);
2974 	btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2975 	btrfs_balance_meta(leaf, item, &disk_bargs);
2976 	btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2977 	btrfs_balance_sys(leaf, item, &disk_bargs);
2978 	btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2979 
2980 	mutex_lock(&fs_info->volume_mutex);
2981 	mutex_lock(&fs_info->balance_mutex);
2982 
2983 	set_balance_control(bctl);
2984 
2985 	mutex_unlock(&fs_info->balance_mutex);
2986 	mutex_unlock(&fs_info->volume_mutex);
2987 out:
2988 	btrfs_free_path(path);
2989 	return ret;
2990 }
2991 
2992 int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2993 {
2994 	int ret = 0;
2995 
2996 	mutex_lock(&fs_info->balance_mutex);
2997 	if (!fs_info->balance_ctl) {
2998 		mutex_unlock(&fs_info->balance_mutex);
2999 		return -ENOTCONN;
3000 	}
3001 
3002 	if (atomic_read(&fs_info->balance_running)) {
3003 		atomic_inc(&fs_info->balance_pause_req);
3004 		mutex_unlock(&fs_info->balance_mutex);
3005 
3006 		wait_event(fs_info->balance_wait_q,
3007 			   atomic_read(&fs_info->balance_running) == 0);
3008 
3009 		mutex_lock(&fs_info->balance_mutex);
3010 		/* we are good with balance_ctl ripped off from under us */
3011 		BUG_ON(atomic_read(&fs_info->balance_running));
3012 		atomic_dec(&fs_info->balance_pause_req);
3013 	} else {
3014 		ret = -ENOTCONN;
3015 	}
3016 
3017 	mutex_unlock(&fs_info->balance_mutex);
3018 	return ret;
3019 }
3020 
3021 int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3022 {
3023 	mutex_lock(&fs_info->balance_mutex);
3024 	if (!fs_info->balance_ctl) {
3025 		mutex_unlock(&fs_info->balance_mutex);
3026 		return -ENOTCONN;
3027 	}
3028 
3029 	atomic_inc(&fs_info->balance_cancel_req);
3030 	/*
3031 	 * if we are running just wait and return, balance item is
3032 	 * deleted in btrfs_balance in this case
3033 	 */
3034 	if (atomic_read(&fs_info->balance_running)) {
3035 		mutex_unlock(&fs_info->balance_mutex);
3036 		wait_event(fs_info->balance_wait_q,
3037 			   atomic_read(&fs_info->balance_running) == 0);
3038 		mutex_lock(&fs_info->balance_mutex);
3039 	} else {
3040 		/* __cancel_balance needs volume_mutex */
3041 		mutex_unlock(&fs_info->balance_mutex);
3042 		mutex_lock(&fs_info->volume_mutex);
3043 		mutex_lock(&fs_info->balance_mutex);
3044 
3045 		if (fs_info->balance_ctl)
3046 			__cancel_balance(fs_info);
3047 
3048 		mutex_unlock(&fs_info->volume_mutex);
3049 	}
3050 
3051 	BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3052 	atomic_dec(&fs_info->balance_cancel_req);
3053 	mutex_unlock(&fs_info->balance_mutex);
3054 	return 0;
3055 }
3056 
3057 /*
3058  * shrinking a device means finding all of the device extents past
3059  * the new size, and then following the back refs to the chunks.
3060  * The chunk relocation code actually frees the device extent
3061  */
3062 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3063 {
3064 	struct btrfs_trans_handle *trans;
3065 	struct btrfs_root *root = device->dev_root;
3066 	struct btrfs_dev_extent *dev_extent = NULL;
3067 	struct btrfs_path *path;
3068 	u64 length;
3069 	u64 chunk_tree;
3070 	u64 chunk_objectid;
3071 	u64 chunk_offset;
3072 	int ret;
3073 	int slot;
3074 	int failed = 0;
3075 	bool retried = false;
3076 	struct extent_buffer *l;
3077 	struct btrfs_key key;
3078 	struct btrfs_super_block *super_copy = root->fs_info->super_copy;
3079 	u64 old_total = btrfs_super_total_bytes(super_copy);
3080 	u64 old_size = device->total_bytes;
3081 	u64 diff = device->total_bytes - new_size;
3082 
3083 	if (new_size >= device->total_bytes)
3084 		return -EINVAL;
3085 
3086 	path = btrfs_alloc_path();
3087 	if (!path)
3088 		return -ENOMEM;
3089 
3090 	path->reada = 2;
3091 
3092 	lock_chunks(root);
3093 
3094 	device->total_bytes = new_size;
3095 	if (device->writeable) {
3096 		device->fs_devices->total_rw_bytes -= diff;
3097 		spin_lock(&root->fs_info->free_chunk_lock);
3098 		root->fs_info->free_chunk_space -= diff;
3099 		spin_unlock(&root->fs_info->free_chunk_lock);
3100 	}
3101 	unlock_chunks(root);
3102 
3103 again:
3104 	key.objectid = device->devid;
3105 	key.offset = (u64)-1;
3106 	key.type = BTRFS_DEV_EXTENT_KEY;
3107 
3108 	do {
3109 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3110 		if (ret < 0)
3111 			goto done;
3112 
3113 		ret = btrfs_previous_item(root, path, 0, key.type);
3114 		if (ret < 0)
3115 			goto done;
3116 		if (ret) {
3117 			ret = 0;
3118 			btrfs_release_path(path);
3119 			break;
3120 		}
3121 
3122 		l = path->nodes[0];
3123 		slot = path->slots[0];
3124 		btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3125 
3126 		if (key.objectid != device->devid) {
3127 			btrfs_release_path(path);
3128 			break;
3129 		}
3130 
3131 		dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3132 		length = btrfs_dev_extent_length(l, dev_extent);
3133 
3134 		if (key.offset + length <= new_size) {
3135 			btrfs_release_path(path);
3136 			break;
3137 		}
3138 
3139 		chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3140 		chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3141 		chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3142 		btrfs_release_path(path);
3143 
3144 		ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3145 					   chunk_offset);
3146 		if (ret && ret != -ENOSPC)
3147 			goto done;
3148 		if (ret == -ENOSPC)
3149 			failed++;
3150 	} while (key.offset-- > 0);
3151 
3152 	if (failed && !retried) {
3153 		failed = 0;
3154 		retried = true;
3155 		goto again;
3156 	} else if (failed && retried) {
3157 		ret = -ENOSPC;
3158 		lock_chunks(root);
3159 
3160 		device->total_bytes = old_size;
3161 		if (device->writeable)
3162 			device->fs_devices->total_rw_bytes += diff;
3163 		spin_lock(&root->fs_info->free_chunk_lock);
3164 		root->fs_info->free_chunk_space += diff;
3165 		spin_unlock(&root->fs_info->free_chunk_lock);
3166 		unlock_chunks(root);
3167 		goto done;
3168 	}
3169 
3170 	/* Shrinking succeeded, else we would be at "done". */
3171 	trans = btrfs_start_transaction(root, 0);
3172 	if (IS_ERR(trans)) {
3173 		ret = PTR_ERR(trans);
3174 		goto done;
3175 	}
3176 
3177 	lock_chunks(root);
3178 
3179 	device->disk_total_bytes = new_size;
3180 	/* Now btrfs_update_device() will change the on-disk size. */
3181 	ret = btrfs_update_device(trans, device);
3182 	if (ret) {
3183 		unlock_chunks(root);
3184 		btrfs_end_transaction(trans, root);
3185 		goto done;
3186 	}
3187 	WARN_ON(diff > old_total);
3188 	btrfs_set_super_total_bytes(super_copy, old_total - diff);
3189 	unlock_chunks(root);
3190 	btrfs_end_transaction(trans, root);
3191 done:
3192 	btrfs_free_path(path);
3193 	return ret;
3194 }
3195 
3196 static int btrfs_add_system_chunk(struct btrfs_root *root,
3197 			   struct btrfs_key *key,
3198 			   struct btrfs_chunk *chunk, int item_size)
3199 {
3200 	struct btrfs_super_block *super_copy = root->fs_info->super_copy;
3201 	struct btrfs_disk_key disk_key;
3202 	u32 array_size;
3203 	u8 *ptr;
3204 
3205 	array_size = btrfs_super_sys_array_size(super_copy);
3206 	if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3207 		return -EFBIG;
3208 
3209 	ptr = super_copy->sys_chunk_array + array_size;
3210 	btrfs_cpu_key_to_disk(&disk_key, key);
3211 	memcpy(ptr, &disk_key, sizeof(disk_key));
3212 	ptr += sizeof(disk_key);
3213 	memcpy(ptr, chunk, item_size);
3214 	item_size += sizeof(disk_key);
3215 	btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3216 	return 0;
3217 }
3218 
3219 /*
3220  * sort the devices in descending order by max_avail, total_avail
3221  */
3222 static int btrfs_cmp_device_info(const void *a, const void *b)
3223 {
3224 	const struct btrfs_device_info *di_a = a;
3225 	const struct btrfs_device_info *di_b = b;
3226 
3227 	if (di_a->max_avail > di_b->max_avail)
3228 		return -1;
3229 	if (di_a->max_avail < di_b->max_avail)
3230 		return 1;
3231 	if (di_a->total_avail > di_b->total_avail)
3232 		return -1;
3233 	if (di_a->total_avail < di_b->total_avail)
3234 		return 1;
3235 	return 0;
3236 }
3237 
3238 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3239 			       struct btrfs_root *extent_root,
3240 			       struct map_lookup **map_ret,
3241 			       u64 *num_bytes_out, u64 *stripe_size_out,
3242 			       u64 start, u64 type)
3243 {
3244 	struct btrfs_fs_info *info = extent_root->fs_info;
3245 	struct btrfs_fs_devices *fs_devices = info->fs_devices;
3246 	struct list_head *cur;
3247 	struct map_lookup *map = NULL;
3248 	struct extent_map_tree *em_tree;
3249 	struct extent_map *em;
3250 	struct btrfs_device_info *devices_info = NULL;
3251 	u64 total_avail;
3252 	int num_stripes;	/* total number of stripes to allocate */
3253 	int sub_stripes;	/* sub_stripes info for map */
3254 	int dev_stripes;	/* stripes per dev */
3255 	int devs_max;		/* max devs to use */
3256 	int devs_min;		/* min devs needed */
3257 	int devs_increment;	/* ndevs has to be a multiple of this */
3258 	int ncopies;		/* how many copies to data has */
3259 	int ret;
3260 	u64 max_stripe_size;
3261 	u64 max_chunk_size;
3262 	u64 stripe_size;
3263 	u64 num_bytes;
3264 	int ndevs;
3265 	int i;
3266 	int j;
3267 
3268 	BUG_ON(!alloc_profile_is_valid(type, 0));
3269 
3270 	if (list_empty(&fs_devices->alloc_list))
3271 		return -ENOSPC;
3272 
3273 	sub_stripes = 1;
3274 	dev_stripes = 1;
3275 	devs_increment = 1;
3276 	ncopies = 1;
3277 	devs_max = 0;	/* 0 == as many as possible */
3278 	devs_min = 1;
3279 
3280 	/*
3281 	 * define the properties of each RAID type.
3282 	 * FIXME: move this to a global table and use it in all RAID
3283 	 * calculation code
3284 	 */
3285 	if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3286 		dev_stripes = 2;
3287 		ncopies = 2;
3288 		devs_max = 1;
3289 	} else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3290 		devs_min = 2;
3291 	} else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3292 		devs_increment = 2;
3293 		ncopies = 2;
3294 		devs_max = 2;
3295 		devs_min = 2;
3296 	} else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3297 		sub_stripes = 2;
3298 		devs_increment = 2;
3299 		ncopies = 2;
3300 		devs_min = 4;
3301 	} else {
3302 		devs_max = 1;
3303 	}
3304 
3305 	if (type & BTRFS_BLOCK_GROUP_DATA) {
3306 		max_stripe_size = 1024 * 1024 * 1024;
3307 		max_chunk_size = 10 * max_stripe_size;
3308 	} else if (type & BTRFS_BLOCK_GROUP_METADATA) {
3309 		/* for larger filesystems, use larger metadata chunks */
3310 		if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3311 			max_stripe_size = 1024 * 1024 * 1024;
3312 		else
3313 			max_stripe_size = 256 * 1024 * 1024;
3314 		max_chunk_size = max_stripe_size;
3315 	} else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
3316 		max_stripe_size = 32 * 1024 * 1024;
3317 		max_chunk_size = 2 * max_stripe_size;
3318 	} else {
3319 		printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3320 		       type);
3321 		BUG_ON(1);
3322 	}
3323 
3324 	/* we don't want a chunk larger than 10% of writeable space */
3325 	max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3326 			     max_chunk_size);
3327 
3328 	devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3329 			       GFP_NOFS);
3330 	if (!devices_info)
3331 		return -ENOMEM;
3332 
3333 	cur = fs_devices->alloc_list.next;
3334 
3335 	/*
3336 	 * in the first pass through the devices list, we gather information
3337 	 * about the available holes on each device.
3338 	 */
3339 	ndevs = 0;
3340 	while (cur != &fs_devices->alloc_list) {
3341 		struct btrfs_device *device;
3342 		u64 max_avail;
3343 		u64 dev_offset;
3344 
3345 		device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3346 
3347 		cur = cur->next;
3348 
3349 		if (!device->writeable) {
3350 			printk(KERN_ERR
3351 			       "btrfs: read-only device in alloc_list\n");
3352 			WARN_ON(1);
3353 			continue;
3354 		}
3355 
3356 		if (!device->in_fs_metadata)
3357 			continue;
3358 
3359 		if (device->total_bytes > device->bytes_used)
3360 			total_avail = device->total_bytes - device->bytes_used;
3361 		else
3362 			total_avail = 0;
3363 
3364 		/* If there is no space on this device, skip it. */
3365 		if (total_avail == 0)
3366 			continue;
3367 
3368 		ret = find_free_dev_extent(device,
3369 					   max_stripe_size * dev_stripes,
3370 					   &dev_offset, &max_avail);
3371 		if (ret && ret != -ENOSPC)
3372 			goto error;
3373 
3374 		if (ret == 0)
3375 			max_avail = max_stripe_size * dev_stripes;
3376 
3377 		if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3378 			continue;
3379 
3380 		devices_info[ndevs].dev_offset = dev_offset;
3381 		devices_info[ndevs].max_avail = max_avail;
3382 		devices_info[ndevs].total_avail = total_avail;
3383 		devices_info[ndevs].dev = device;
3384 		++ndevs;
3385 	}
3386 
3387 	/*
3388 	 * now sort the devices by hole size / available space
3389 	 */
3390 	sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3391 	     btrfs_cmp_device_info, NULL);
3392 
3393 	/* round down to number of usable stripes */
3394 	ndevs -= ndevs % devs_increment;
3395 
3396 	if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3397 		ret = -ENOSPC;
3398 		goto error;
3399 	}
3400 
3401 	if (devs_max && ndevs > devs_max)
3402 		ndevs = devs_max;
3403 	/*
3404 	 * the primary goal is to maximize the number of stripes, so use as many
3405 	 * devices as possible, even if the stripes are not maximum sized.
3406 	 */
3407 	stripe_size = devices_info[ndevs-1].max_avail;
3408 	num_stripes = ndevs * dev_stripes;
3409 
3410 	if (stripe_size * ndevs > max_chunk_size * ncopies) {
3411 		stripe_size = max_chunk_size * ncopies;
3412 		do_div(stripe_size, ndevs);
3413 	}
3414 
3415 	do_div(stripe_size, dev_stripes);
3416 
3417 	/* align to BTRFS_STRIPE_LEN */
3418 	do_div(stripe_size, BTRFS_STRIPE_LEN);
3419 	stripe_size *= BTRFS_STRIPE_LEN;
3420 
3421 	map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3422 	if (!map) {
3423 		ret = -ENOMEM;
3424 		goto error;
3425 	}
3426 	map->num_stripes = num_stripes;
3427 
3428 	for (i = 0; i < ndevs; ++i) {
3429 		for (j = 0; j < dev_stripes; ++j) {
3430 			int s = i * dev_stripes + j;
3431 			map->stripes[s].dev = devices_info[i].dev;
3432 			map->stripes[s].physical = devices_info[i].dev_offset +
3433 						   j * stripe_size;
3434 		}
3435 	}
3436 	map->sector_size = extent_root->sectorsize;
3437 	map->stripe_len = BTRFS_STRIPE_LEN;
3438 	map->io_align = BTRFS_STRIPE_LEN;
3439 	map->io_width = BTRFS_STRIPE_LEN;
3440 	map->type = type;
3441 	map->sub_stripes = sub_stripes;
3442 
3443 	*map_ret = map;
3444 	num_bytes = stripe_size * (num_stripes / ncopies);
3445 
3446 	*stripe_size_out = stripe_size;
3447 	*num_bytes_out = num_bytes;
3448 
3449 	trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
3450 
3451 	em = alloc_extent_map();
3452 	if (!em) {
3453 		ret = -ENOMEM;
3454 		goto error;
3455 	}
3456 	em->bdev = (struct block_device *)map;
3457 	em->start = start;
3458 	em->len = num_bytes;
3459 	em->block_start = 0;
3460 	em->block_len = em->len;
3461 
3462 	em_tree = &extent_root->fs_info->mapping_tree.map_tree;
3463 	write_lock(&em_tree->lock);
3464 	ret = add_extent_mapping(em_tree, em);
3465 	write_unlock(&em_tree->lock);
3466 	free_extent_map(em);
3467 	if (ret)
3468 		goto error;
3469 
3470 	ret = btrfs_make_block_group(trans, extent_root, 0, type,
3471 				     BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3472 				     start, num_bytes);
3473 	if (ret)
3474 		goto error;
3475 
3476 	for (i = 0; i < map->num_stripes; ++i) {
3477 		struct btrfs_device *device;
3478 		u64 dev_offset;
3479 
3480 		device = map->stripes[i].dev;
3481 		dev_offset = map->stripes[i].physical;
3482 
3483 		ret = btrfs_alloc_dev_extent(trans, device,
3484 				info->chunk_root->root_key.objectid,
3485 				BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3486 				start, dev_offset, stripe_size);
3487 		if (ret) {
3488 			btrfs_abort_transaction(trans, extent_root, ret);
3489 			goto error;
3490 		}
3491 	}
3492 
3493 	kfree(devices_info);
3494 	return 0;
3495 
3496 error:
3497 	kfree(map);
3498 	kfree(devices_info);
3499 	return ret;
3500 }
3501 
3502 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3503 				struct btrfs_root *extent_root,
3504 				struct map_lookup *map, u64 chunk_offset,
3505 				u64 chunk_size, u64 stripe_size)
3506 {
3507 	u64 dev_offset;
3508 	struct btrfs_key key;
3509 	struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3510 	struct btrfs_device *device;
3511 	struct btrfs_chunk *chunk;
3512 	struct btrfs_stripe *stripe;
3513 	size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3514 	int index = 0;
3515 	int ret;
3516 
3517 	chunk = kzalloc(item_size, GFP_NOFS);
3518 	if (!chunk)
3519 		return -ENOMEM;
3520 
3521 	index = 0;
3522 	while (index < map->num_stripes) {
3523 		device = map->stripes[index].dev;
3524 		device->bytes_used += stripe_size;
3525 		ret = btrfs_update_device(trans, device);
3526 		if (ret)
3527 			goto out_free;
3528 		index++;
3529 	}
3530 
3531 	spin_lock(&extent_root->fs_info->free_chunk_lock);
3532 	extent_root->fs_info->free_chunk_space -= (stripe_size *
3533 						   map->num_stripes);
3534 	spin_unlock(&extent_root->fs_info->free_chunk_lock);
3535 
3536 	index = 0;
3537 	stripe = &chunk->stripe;
3538 	while (index < map->num_stripes) {
3539 		device = map->stripes[index].dev;
3540 		dev_offset = map->stripes[index].physical;
3541 
3542 		btrfs_set_stack_stripe_devid(stripe, device->devid);
3543 		btrfs_set_stack_stripe_offset(stripe, dev_offset);
3544 		memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3545 		stripe++;
3546 		index++;
3547 	}
3548 
3549 	btrfs_set_stack_chunk_length(chunk, chunk_size);
3550 	btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3551 	btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3552 	btrfs_set_stack_chunk_type(chunk, map->type);
3553 	btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3554 	btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3555 	btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3556 	btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3557 	btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3558 
3559 	key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3560 	key.type = BTRFS_CHUNK_ITEM_KEY;
3561 	key.offset = chunk_offset;
3562 
3563 	ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
3564 
3565 	if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3566 		/*
3567 		 * TODO: Cleanup of inserted chunk root in case of
3568 		 * failure.
3569 		 */
3570 		ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
3571 					     item_size);
3572 	}
3573 
3574 out_free:
3575 	kfree(chunk);
3576 	return ret;
3577 }
3578 
3579 /*
3580  * Chunk allocation falls into two parts. The first part does works
3581  * that make the new allocated chunk useable, but not do any operation
3582  * that modifies the chunk tree. The second part does the works that
3583  * require modifying the chunk tree. This division is important for the
3584  * bootstrap process of adding storage to a seed btrfs.
3585  */
3586 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3587 		      struct btrfs_root *extent_root, u64 type)
3588 {
3589 	u64 chunk_offset;
3590 	u64 chunk_size;
3591 	u64 stripe_size;
3592 	struct map_lookup *map;
3593 	struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3594 	int ret;
3595 
3596 	ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3597 			      &chunk_offset);
3598 	if (ret)
3599 		return ret;
3600 
3601 	ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3602 				  &stripe_size, chunk_offset, type);
3603 	if (ret)
3604 		return ret;
3605 
3606 	ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3607 				   chunk_size, stripe_size);
3608 	if (ret)
3609 		return ret;
3610 	return 0;
3611 }
3612 
3613 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
3614 					 struct btrfs_root *root,
3615 					 struct btrfs_device *device)
3616 {
3617 	u64 chunk_offset;
3618 	u64 sys_chunk_offset;
3619 	u64 chunk_size;
3620 	u64 sys_chunk_size;
3621 	u64 stripe_size;
3622 	u64 sys_stripe_size;
3623 	u64 alloc_profile;
3624 	struct map_lookup *map;
3625 	struct map_lookup *sys_map;
3626 	struct btrfs_fs_info *fs_info = root->fs_info;
3627 	struct btrfs_root *extent_root = fs_info->extent_root;
3628 	int ret;
3629 
3630 	ret = find_next_chunk(fs_info->chunk_root,
3631 			      BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
3632 	if (ret)
3633 		return ret;
3634 
3635 	alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
3636 				fs_info->avail_metadata_alloc_bits;
3637 	alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3638 
3639 	ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3640 				  &stripe_size, chunk_offset, alloc_profile);
3641 	if (ret)
3642 		return ret;
3643 
3644 	sys_chunk_offset = chunk_offset + chunk_size;
3645 
3646 	alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
3647 				fs_info->avail_system_alloc_bits;
3648 	alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3649 
3650 	ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3651 				  &sys_chunk_size, &sys_stripe_size,
3652 				  sys_chunk_offset, alloc_profile);
3653 	if (ret) {
3654 		btrfs_abort_transaction(trans, root, ret);
3655 		goto out;
3656 	}
3657 
3658 	ret = btrfs_add_device(trans, fs_info->chunk_root, device);
3659 	if (ret) {
3660 		btrfs_abort_transaction(trans, root, ret);
3661 		goto out;
3662 	}
3663 
3664 	/*
3665 	 * Modifying chunk tree needs allocating new blocks from both
3666 	 * system block group and metadata block group. So we only can
3667 	 * do operations require modifying the chunk tree after both
3668 	 * block groups were created.
3669 	 */
3670 	ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3671 				   chunk_size, stripe_size);
3672 	if (ret) {
3673 		btrfs_abort_transaction(trans, root, ret);
3674 		goto out;
3675 	}
3676 
3677 	ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3678 				   sys_chunk_offset, sys_chunk_size,
3679 				   sys_stripe_size);
3680 	if (ret)
3681 		btrfs_abort_transaction(trans, root, ret);
3682 
3683 out:
3684 
3685 	return ret;
3686 }
3687 
3688 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3689 {
3690 	struct extent_map *em;
3691 	struct map_lookup *map;
3692 	struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3693 	int readonly = 0;
3694 	int i;
3695 
3696 	read_lock(&map_tree->map_tree.lock);
3697 	em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3698 	read_unlock(&map_tree->map_tree.lock);
3699 	if (!em)
3700 		return 1;
3701 
3702 	if (btrfs_test_opt(root, DEGRADED)) {
3703 		free_extent_map(em);
3704 		return 0;
3705 	}
3706 
3707 	map = (struct map_lookup *)em->bdev;
3708 	for (i = 0; i < map->num_stripes; i++) {
3709 		if (!map->stripes[i].dev->writeable) {
3710 			readonly = 1;
3711 			break;
3712 		}
3713 	}
3714 	free_extent_map(em);
3715 	return readonly;
3716 }
3717 
3718 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3719 {
3720 	extent_map_tree_init(&tree->map_tree);
3721 }
3722 
3723 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3724 {
3725 	struct extent_map *em;
3726 
3727 	while (1) {
3728 		write_lock(&tree->map_tree.lock);
3729 		em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3730 		if (em)
3731 			remove_extent_mapping(&tree->map_tree, em);
3732 		write_unlock(&tree->map_tree.lock);
3733 		if (!em)
3734 			break;
3735 		kfree(em->bdev);
3736 		/* once for us */
3737 		free_extent_map(em);
3738 		/* once for the tree */
3739 		free_extent_map(em);
3740 	}
3741 }
3742 
3743 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3744 {
3745 	struct extent_map *em;
3746 	struct map_lookup *map;
3747 	struct extent_map_tree *em_tree = &map_tree->map_tree;
3748 	int ret;
3749 
3750 	read_lock(&em_tree->lock);
3751 	em = lookup_extent_mapping(em_tree, logical, len);
3752 	read_unlock(&em_tree->lock);
3753 	BUG_ON(!em);
3754 
3755 	BUG_ON(em->start > logical || em->start + em->len < logical);
3756 	map = (struct map_lookup *)em->bdev;
3757 	if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3758 		ret = map->num_stripes;
3759 	else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3760 		ret = map->sub_stripes;
3761 	else
3762 		ret = 1;
3763 	free_extent_map(em);
3764 	return ret;
3765 }
3766 
3767 static int find_live_mirror(struct map_lookup *map, int first, int num,
3768 			    int optimal)
3769 {
3770 	int i;
3771 	if (map->stripes[optimal].dev->bdev)
3772 		return optimal;
3773 	for (i = first; i < first + num; i++) {
3774 		if (map->stripes[i].dev->bdev)
3775 			return i;
3776 	}
3777 	/* we couldn't find one that doesn't fail.  Just return something
3778 	 * and the io error handling code will clean up eventually
3779 	 */
3780 	return optimal;
3781 }
3782 
3783 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3784 			     u64 logical, u64 *length,
3785 			     struct btrfs_bio **bbio_ret,
3786 			     int mirror_num)
3787 {
3788 	struct extent_map *em;
3789 	struct map_lookup *map;
3790 	struct extent_map_tree *em_tree = &map_tree->map_tree;
3791 	u64 offset;
3792 	u64 stripe_offset;
3793 	u64 stripe_end_offset;
3794 	u64 stripe_nr;
3795 	u64 stripe_nr_orig;
3796 	u64 stripe_nr_end;
3797 	int stripe_index;
3798 	int i;
3799 	int ret = 0;
3800 	int num_stripes;
3801 	int max_errors = 0;
3802 	struct btrfs_bio *bbio = NULL;
3803 
3804 	read_lock(&em_tree->lock);
3805 	em = lookup_extent_mapping(em_tree, logical, *length);
3806 	read_unlock(&em_tree->lock);
3807 
3808 	if (!em) {
3809 		printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
3810 		       (unsigned long long)logical,
3811 		       (unsigned long long)*length);
3812 		BUG();
3813 	}
3814 
3815 	BUG_ON(em->start > logical || em->start + em->len < logical);
3816 	map = (struct map_lookup *)em->bdev;
3817 	offset = logical - em->start;
3818 
3819 	if (mirror_num > map->num_stripes)
3820 		mirror_num = 0;
3821 
3822 	stripe_nr = offset;
3823 	/*
3824 	 * stripe_nr counts the total number of stripes we have to stride
3825 	 * to get to this block
3826 	 */
3827 	do_div(stripe_nr, map->stripe_len);
3828 
3829 	stripe_offset = stripe_nr * map->stripe_len;
3830 	BUG_ON(offset < stripe_offset);
3831 
3832 	/* stripe_offset is the offset of this block in its stripe*/
3833 	stripe_offset = offset - stripe_offset;
3834 
3835 	if (rw & REQ_DISCARD)
3836 		*length = min_t(u64, em->len - offset, *length);
3837 	else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
3838 		/* we limit the length of each bio to what fits in a stripe */
3839 		*length = min_t(u64, em->len - offset,
3840 				map->stripe_len - stripe_offset);
3841 	} else {
3842 		*length = em->len - offset;
3843 	}
3844 
3845 	if (!bbio_ret)
3846 		goto out;
3847 
3848 	num_stripes = 1;
3849 	stripe_index = 0;
3850 	stripe_nr_orig = stripe_nr;
3851 	stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3852 			(~(map->stripe_len - 1));
3853 	do_div(stripe_nr_end, map->stripe_len);
3854 	stripe_end_offset = stripe_nr_end * map->stripe_len -
3855 			    (offset + *length);
3856 	if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3857 		if (rw & REQ_DISCARD)
3858 			num_stripes = min_t(u64, map->num_stripes,
3859 					    stripe_nr_end - stripe_nr_orig);
3860 		stripe_index = do_div(stripe_nr, map->num_stripes);
3861 	} else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3862 		if (rw & (REQ_WRITE | REQ_DISCARD))
3863 			num_stripes = map->num_stripes;
3864 		else if (mirror_num)
3865 			stripe_index = mirror_num - 1;
3866 		else {
3867 			stripe_index = find_live_mirror(map, 0,
3868 					    map->num_stripes,
3869 					    current->pid % map->num_stripes);
3870 			mirror_num = stripe_index + 1;
3871 		}
3872 
3873 	} else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3874 		if (rw & (REQ_WRITE | REQ_DISCARD)) {
3875 			num_stripes = map->num_stripes;
3876 		} else if (mirror_num) {
3877 			stripe_index = mirror_num - 1;
3878 		} else {
3879 			mirror_num = 1;
3880 		}
3881 
3882 	} else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3883 		int factor = map->num_stripes / map->sub_stripes;
3884 
3885 		stripe_index = do_div(stripe_nr, factor);
3886 		stripe_index *= map->sub_stripes;
3887 
3888 		if (rw & REQ_WRITE)
3889 			num_stripes = map->sub_stripes;
3890 		else if (rw & REQ_DISCARD)
3891 			num_stripes = min_t(u64, map->sub_stripes *
3892 					    (stripe_nr_end - stripe_nr_orig),
3893 					    map->num_stripes);
3894 		else if (mirror_num)
3895 			stripe_index += mirror_num - 1;
3896 		else {
3897 			int old_stripe_index = stripe_index;
3898 			stripe_index = find_live_mirror(map, stripe_index,
3899 					      map->sub_stripes, stripe_index +
3900 					      current->pid % map->sub_stripes);
3901 			mirror_num = stripe_index - old_stripe_index + 1;
3902 		}
3903 	} else {
3904 		/*
3905 		 * after this do_div call, stripe_nr is the number of stripes
3906 		 * on this device we have to walk to find the data, and
3907 		 * stripe_index is the number of our device in the stripe array
3908 		 */
3909 		stripe_index = do_div(stripe_nr, map->num_stripes);
3910 		mirror_num = stripe_index + 1;
3911 	}
3912 	BUG_ON(stripe_index >= map->num_stripes);
3913 
3914 	bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3915 	if (!bbio) {
3916 		ret = -ENOMEM;
3917 		goto out;
3918 	}
3919 	atomic_set(&bbio->error, 0);
3920 
3921 	if (rw & REQ_DISCARD) {
3922 		int factor = 0;
3923 		int sub_stripes = 0;
3924 		u64 stripes_per_dev = 0;
3925 		u32 remaining_stripes = 0;
3926 		u32 last_stripe = 0;
3927 
3928 		if (map->type &
3929 		    (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3930 			if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3931 				sub_stripes = 1;
3932 			else
3933 				sub_stripes = map->sub_stripes;
3934 
3935 			factor = map->num_stripes / sub_stripes;
3936 			stripes_per_dev = div_u64_rem(stripe_nr_end -
3937 						      stripe_nr_orig,
3938 						      factor,
3939 						      &remaining_stripes);
3940 			div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
3941 			last_stripe *= sub_stripes;
3942 		}
3943 
3944 		for (i = 0; i < num_stripes; i++) {
3945 			bbio->stripes[i].physical =
3946 				map->stripes[stripe_index].physical +
3947 				stripe_offset + stripe_nr * map->stripe_len;
3948 			bbio->stripes[i].dev = map->stripes[stripe_index].dev;
3949 
3950 			if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3951 					 BTRFS_BLOCK_GROUP_RAID10)) {
3952 				bbio->stripes[i].length = stripes_per_dev *
3953 							  map->stripe_len;
3954 
3955 				if (i / sub_stripes < remaining_stripes)
3956 					bbio->stripes[i].length +=
3957 						map->stripe_len;
3958 
3959 				/*
3960 				 * Special for the first stripe and
3961 				 * the last stripe:
3962 				 *
3963 				 * |-------|...|-------|
3964 				 *     |----------|
3965 				 *    off     end_off
3966 				 */
3967 				if (i < sub_stripes)
3968 					bbio->stripes[i].length -=
3969 						stripe_offset;
3970 
3971 				if (stripe_index >= last_stripe &&
3972 				    stripe_index <= (last_stripe +
3973 						     sub_stripes - 1))
3974 					bbio->stripes[i].length -=
3975 						stripe_end_offset;
3976 
3977 				if (i == sub_stripes - 1)
3978 					stripe_offset = 0;
3979 			} else
3980 				bbio->stripes[i].length = *length;
3981 
3982 			stripe_index++;
3983 			if (stripe_index == map->num_stripes) {
3984 				/* This could only happen for RAID0/10 */
3985 				stripe_index = 0;
3986 				stripe_nr++;
3987 			}
3988 		}
3989 	} else {
3990 		for (i = 0; i < num_stripes; i++) {
3991 			bbio->stripes[i].physical =
3992 				map->stripes[stripe_index].physical +
3993 				stripe_offset +
3994 				stripe_nr * map->stripe_len;
3995 			bbio->stripes[i].dev =
3996 				map->stripes[stripe_index].dev;
3997 			stripe_index++;
3998 		}
3999 	}
4000 
4001 	if (rw & REQ_WRITE) {
4002 		if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4003 				 BTRFS_BLOCK_GROUP_RAID10 |
4004 				 BTRFS_BLOCK_GROUP_DUP)) {
4005 			max_errors = 1;
4006 		}
4007 	}
4008 
4009 	*bbio_ret = bbio;
4010 	bbio->num_stripes = num_stripes;
4011 	bbio->max_errors = max_errors;
4012 	bbio->mirror_num = mirror_num;
4013 out:
4014 	free_extent_map(em);
4015 	return ret;
4016 }
4017 
4018 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
4019 		      u64 logical, u64 *length,
4020 		      struct btrfs_bio **bbio_ret, int mirror_num)
4021 {
4022 	return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
4023 				 mirror_num);
4024 }
4025 
4026 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4027 		     u64 chunk_start, u64 physical, u64 devid,
4028 		     u64 **logical, int *naddrs, int *stripe_len)
4029 {
4030 	struct extent_map_tree *em_tree = &map_tree->map_tree;
4031 	struct extent_map *em;
4032 	struct map_lookup *map;
4033 	u64 *buf;
4034 	u64 bytenr;
4035 	u64 length;
4036 	u64 stripe_nr;
4037 	int i, j, nr = 0;
4038 
4039 	read_lock(&em_tree->lock);
4040 	em = lookup_extent_mapping(em_tree, chunk_start, 1);
4041 	read_unlock(&em_tree->lock);
4042 
4043 	BUG_ON(!em || em->start != chunk_start);
4044 	map = (struct map_lookup *)em->bdev;
4045 
4046 	length = em->len;
4047 	if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4048 		do_div(length, map->num_stripes / map->sub_stripes);
4049 	else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4050 		do_div(length, map->num_stripes);
4051 
4052 	buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
4053 	BUG_ON(!buf); /* -ENOMEM */
4054 
4055 	for (i = 0; i < map->num_stripes; i++) {
4056 		if (devid && map->stripes[i].dev->devid != devid)
4057 			continue;
4058 		if (map->stripes[i].physical > physical ||
4059 		    map->stripes[i].physical + length <= physical)
4060 			continue;
4061 
4062 		stripe_nr = physical - map->stripes[i].physical;
4063 		do_div(stripe_nr, map->stripe_len);
4064 
4065 		if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4066 			stripe_nr = stripe_nr * map->num_stripes + i;
4067 			do_div(stripe_nr, map->sub_stripes);
4068 		} else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4069 			stripe_nr = stripe_nr * map->num_stripes + i;
4070 		}
4071 		bytenr = chunk_start + stripe_nr * map->stripe_len;
4072 		WARN_ON(nr >= map->num_stripes);
4073 		for (j = 0; j < nr; j++) {
4074 			if (buf[j] == bytenr)
4075 				break;
4076 		}
4077 		if (j == nr) {
4078 			WARN_ON(nr >= map->num_stripes);
4079 			buf[nr++] = bytenr;
4080 		}
4081 	}
4082 
4083 	*logical = buf;
4084 	*naddrs = nr;
4085 	*stripe_len = map->stripe_len;
4086 
4087 	free_extent_map(em);
4088 	return 0;
4089 }
4090 
4091 static void *merge_stripe_index_into_bio_private(void *bi_private,
4092 						 unsigned int stripe_index)
4093 {
4094 	/*
4095 	 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4096 	 * at most 1.
4097 	 * The alternative solution (instead of stealing bits from the
4098 	 * pointer) would be to allocate an intermediate structure
4099 	 * that contains the old private pointer plus the stripe_index.
4100 	 */
4101 	BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4102 	BUG_ON(stripe_index > 3);
4103 	return (void *)(((uintptr_t)bi_private) | stripe_index);
4104 }
4105 
4106 static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4107 {
4108 	return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4109 }
4110 
4111 static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
4112 {
4113 	return (unsigned int)((uintptr_t)bi_private) & 3;
4114 }
4115 
4116 static void btrfs_end_bio(struct bio *bio, int err)
4117 {
4118 	struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
4119 	int is_orig_bio = 0;
4120 
4121 	if (err) {
4122 		atomic_inc(&bbio->error);
4123 		if (err == -EIO || err == -EREMOTEIO) {
4124 			unsigned int stripe_index =
4125 				extract_stripe_index_from_bio_private(
4126 					bio->bi_private);
4127 			struct btrfs_device *dev;
4128 
4129 			BUG_ON(stripe_index >= bbio->num_stripes);
4130 			dev = bbio->stripes[stripe_index].dev;
4131 			if (dev->bdev) {
4132 				if (bio->bi_rw & WRITE)
4133 					btrfs_dev_stat_inc(dev,
4134 						BTRFS_DEV_STAT_WRITE_ERRS);
4135 				else
4136 					btrfs_dev_stat_inc(dev,
4137 						BTRFS_DEV_STAT_READ_ERRS);
4138 				if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
4139 					btrfs_dev_stat_inc(dev,
4140 						BTRFS_DEV_STAT_FLUSH_ERRS);
4141 				btrfs_dev_stat_print_on_error(dev);
4142 			}
4143 		}
4144 	}
4145 
4146 	if (bio == bbio->orig_bio)
4147 		is_orig_bio = 1;
4148 
4149 	if (atomic_dec_and_test(&bbio->stripes_pending)) {
4150 		if (!is_orig_bio) {
4151 			bio_put(bio);
4152 			bio = bbio->orig_bio;
4153 		}
4154 		bio->bi_private = bbio->private;
4155 		bio->bi_end_io = bbio->end_io;
4156 		bio->bi_bdev = (struct block_device *)
4157 					(unsigned long)bbio->mirror_num;
4158 		/* only send an error to the higher layers if it is
4159 		 * beyond the tolerance of the multi-bio
4160 		 */
4161 		if (atomic_read(&bbio->error) > bbio->max_errors) {
4162 			err = -EIO;
4163 		} else {
4164 			/*
4165 			 * this bio is actually up to date, we didn't
4166 			 * go over the max number of errors
4167 			 */
4168 			set_bit(BIO_UPTODATE, &bio->bi_flags);
4169 			err = 0;
4170 		}
4171 		kfree(bbio);
4172 
4173 		bio_endio(bio, err);
4174 	} else if (!is_orig_bio) {
4175 		bio_put(bio);
4176 	}
4177 }
4178 
4179 struct async_sched {
4180 	struct bio *bio;
4181 	int rw;
4182 	struct btrfs_fs_info *info;
4183 	struct btrfs_work work;
4184 };
4185 
4186 /*
4187  * see run_scheduled_bios for a description of why bios are collected for
4188  * async submit.
4189  *
4190  * This will add one bio to the pending list for a device and make sure
4191  * the work struct is scheduled.
4192  */
4193 static noinline void schedule_bio(struct btrfs_root *root,
4194 				 struct btrfs_device *device,
4195 				 int rw, struct bio *bio)
4196 {
4197 	int should_queue = 1;
4198 	struct btrfs_pending_bios *pending_bios;
4199 
4200 	/* don't bother with additional async steps for reads, right now */
4201 	if (!(rw & REQ_WRITE)) {
4202 		bio_get(bio);
4203 		btrfsic_submit_bio(rw, bio);
4204 		bio_put(bio);
4205 		return;
4206 	}
4207 
4208 	/*
4209 	 * nr_async_bios allows us to reliably return congestion to the
4210 	 * higher layers.  Otherwise, the async bio makes it appear we have
4211 	 * made progress against dirty pages when we've really just put it
4212 	 * on a queue for later
4213 	 */
4214 	atomic_inc(&root->fs_info->nr_async_bios);
4215 	WARN_ON(bio->bi_next);
4216 	bio->bi_next = NULL;
4217 	bio->bi_rw |= rw;
4218 
4219 	spin_lock(&device->io_lock);
4220 	if (bio->bi_rw & REQ_SYNC)
4221 		pending_bios = &device->pending_sync_bios;
4222 	else
4223 		pending_bios = &device->pending_bios;
4224 
4225 	if (pending_bios->tail)
4226 		pending_bios->tail->bi_next = bio;
4227 
4228 	pending_bios->tail = bio;
4229 	if (!pending_bios->head)
4230 		pending_bios->head = bio;
4231 	if (device->running_pending)
4232 		should_queue = 0;
4233 
4234 	spin_unlock(&device->io_lock);
4235 
4236 	if (should_queue)
4237 		btrfs_queue_worker(&root->fs_info->submit_workers,
4238 				   &device->work);
4239 }
4240 
4241 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
4242 		  int mirror_num, int async_submit)
4243 {
4244 	struct btrfs_mapping_tree *map_tree;
4245 	struct btrfs_device *dev;
4246 	struct bio *first_bio = bio;
4247 	u64 logical = (u64)bio->bi_sector << 9;
4248 	u64 length = 0;
4249 	u64 map_length;
4250 	int ret;
4251 	int dev_nr = 0;
4252 	int total_devs = 1;
4253 	struct btrfs_bio *bbio = NULL;
4254 
4255 	length = bio->bi_size;
4256 	map_tree = &root->fs_info->mapping_tree;
4257 	map_length = length;
4258 
4259 	ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
4260 			      mirror_num);
4261 	if (ret) /* -ENOMEM */
4262 		return ret;
4263 
4264 	total_devs = bbio->num_stripes;
4265 	if (map_length < length) {
4266 		printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
4267 		       "len %llu\n", (unsigned long long)logical,
4268 		       (unsigned long long)length,
4269 		       (unsigned long long)map_length);
4270 		BUG();
4271 	}
4272 
4273 	bbio->orig_bio = first_bio;
4274 	bbio->private = first_bio->bi_private;
4275 	bbio->end_io = first_bio->bi_end_io;
4276 	atomic_set(&bbio->stripes_pending, bbio->num_stripes);
4277 
4278 	while (dev_nr < total_devs) {
4279 		if (dev_nr < total_devs - 1) {
4280 			bio = bio_clone(first_bio, GFP_NOFS);
4281 			BUG_ON(!bio); /* -ENOMEM */
4282 		} else {
4283 			bio = first_bio;
4284 		}
4285 		bio->bi_private = bbio;
4286 		bio->bi_private = merge_stripe_index_into_bio_private(
4287 				bio->bi_private, (unsigned int)dev_nr);
4288 		bio->bi_end_io = btrfs_end_bio;
4289 		bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4290 		dev = bbio->stripes[dev_nr].dev;
4291 		if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
4292 #ifdef DEBUG
4293 			struct rcu_string *name;
4294 
4295 			rcu_read_lock();
4296 			name = rcu_dereference(dev->name);
4297 			pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4298 				 "(%s id %llu), size=%u\n", rw,
4299 				 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4300 				 name->str, dev->devid, bio->bi_size);
4301 			rcu_read_unlock();
4302 #endif
4303 			bio->bi_bdev = dev->bdev;
4304 			if (async_submit)
4305 				schedule_bio(root, dev, rw, bio);
4306 			else
4307 				btrfsic_submit_bio(rw, bio);
4308 		} else {
4309 			bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4310 			bio->bi_sector = logical >> 9;
4311 			bio_endio(bio, -EIO);
4312 		}
4313 		dev_nr++;
4314 	}
4315 	return 0;
4316 }
4317 
4318 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
4319 				       u8 *uuid, u8 *fsid)
4320 {
4321 	struct btrfs_device *device;
4322 	struct btrfs_fs_devices *cur_devices;
4323 
4324 	cur_devices = root->fs_info->fs_devices;
4325 	while (cur_devices) {
4326 		if (!fsid ||
4327 		    !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4328 			device = __find_device(&cur_devices->devices,
4329 					       devid, uuid);
4330 			if (device)
4331 				return device;
4332 		}
4333 		cur_devices = cur_devices->seed;
4334 	}
4335 	return NULL;
4336 }
4337 
4338 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4339 					    u64 devid, u8 *dev_uuid)
4340 {
4341 	struct btrfs_device *device;
4342 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4343 
4344 	device = kzalloc(sizeof(*device), GFP_NOFS);
4345 	if (!device)
4346 		return NULL;
4347 	list_add(&device->dev_list,
4348 		 &fs_devices->devices);
4349 	device->dev_root = root->fs_info->dev_root;
4350 	device->devid = devid;
4351 	device->work.func = pending_bios_fn;
4352 	device->fs_devices = fs_devices;
4353 	device->missing = 1;
4354 	fs_devices->num_devices++;
4355 	fs_devices->missing_devices++;
4356 	spin_lock_init(&device->io_lock);
4357 	INIT_LIST_HEAD(&device->dev_alloc_list);
4358 	memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4359 	return device;
4360 }
4361 
4362 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4363 			  struct extent_buffer *leaf,
4364 			  struct btrfs_chunk *chunk)
4365 {
4366 	struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4367 	struct map_lookup *map;
4368 	struct extent_map *em;
4369 	u64 logical;
4370 	u64 length;
4371 	u64 devid;
4372 	u8 uuid[BTRFS_UUID_SIZE];
4373 	int num_stripes;
4374 	int ret;
4375 	int i;
4376 
4377 	logical = key->offset;
4378 	length = btrfs_chunk_length(leaf, chunk);
4379 
4380 	read_lock(&map_tree->map_tree.lock);
4381 	em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
4382 	read_unlock(&map_tree->map_tree.lock);
4383 
4384 	/* already mapped? */
4385 	if (em && em->start <= logical && em->start + em->len > logical) {
4386 		free_extent_map(em);
4387 		return 0;
4388 	} else if (em) {
4389 		free_extent_map(em);
4390 	}
4391 
4392 	em = alloc_extent_map();
4393 	if (!em)
4394 		return -ENOMEM;
4395 	num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4396 	map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4397 	if (!map) {
4398 		free_extent_map(em);
4399 		return -ENOMEM;
4400 	}
4401 
4402 	em->bdev = (struct block_device *)map;
4403 	em->start = logical;
4404 	em->len = length;
4405 	em->block_start = 0;
4406 	em->block_len = em->len;
4407 
4408 	map->num_stripes = num_stripes;
4409 	map->io_width = btrfs_chunk_io_width(leaf, chunk);
4410 	map->io_align = btrfs_chunk_io_align(leaf, chunk);
4411 	map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4412 	map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4413 	map->type = btrfs_chunk_type(leaf, chunk);
4414 	map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
4415 	for (i = 0; i < num_stripes; i++) {
4416 		map->stripes[i].physical =
4417 			btrfs_stripe_offset_nr(leaf, chunk, i);
4418 		devid = btrfs_stripe_devid_nr(leaf, chunk, i);
4419 		read_extent_buffer(leaf, uuid, (unsigned long)
4420 				   btrfs_stripe_dev_uuid_nr(chunk, i),
4421 				   BTRFS_UUID_SIZE);
4422 		map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4423 							NULL);
4424 		if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
4425 			kfree(map);
4426 			free_extent_map(em);
4427 			return -EIO;
4428 		}
4429 		if (!map->stripes[i].dev) {
4430 			map->stripes[i].dev =
4431 				add_missing_dev(root, devid, uuid);
4432 			if (!map->stripes[i].dev) {
4433 				kfree(map);
4434 				free_extent_map(em);
4435 				return -EIO;
4436 			}
4437 		}
4438 		map->stripes[i].dev->in_fs_metadata = 1;
4439 	}
4440 
4441 	write_lock(&map_tree->map_tree.lock);
4442 	ret = add_extent_mapping(&map_tree->map_tree, em);
4443 	write_unlock(&map_tree->map_tree.lock);
4444 	BUG_ON(ret); /* Tree corruption */
4445 	free_extent_map(em);
4446 
4447 	return 0;
4448 }
4449 
4450 static void fill_device_from_item(struct extent_buffer *leaf,
4451 				 struct btrfs_dev_item *dev_item,
4452 				 struct btrfs_device *device)
4453 {
4454 	unsigned long ptr;
4455 
4456 	device->devid = btrfs_device_id(leaf, dev_item);
4457 	device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4458 	device->total_bytes = device->disk_total_bytes;
4459 	device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4460 	device->type = btrfs_device_type(leaf, dev_item);
4461 	device->io_align = btrfs_device_io_align(leaf, dev_item);
4462 	device->io_width = btrfs_device_io_width(leaf, dev_item);
4463 	device->sector_size = btrfs_device_sector_size(leaf, dev_item);
4464 
4465 	ptr = (unsigned long)btrfs_device_uuid(dev_item);
4466 	read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
4467 }
4468 
4469 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4470 {
4471 	struct btrfs_fs_devices *fs_devices;
4472 	int ret;
4473 
4474 	BUG_ON(!mutex_is_locked(&uuid_mutex));
4475 
4476 	fs_devices = root->fs_info->fs_devices->seed;
4477 	while (fs_devices) {
4478 		if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4479 			ret = 0;
4480 			goto out;
4481 		}
4482 		fs_devices = fs_devices->seed;
4483 	}
4484 
4485 	fs_devices = find_fsid(fsid);
4486 	if (!fs_devices) {
4487 		ret = -ENOENT;
4488 		goto out;
4489 	}
4490 
4491 	fs_devices = clone_fs_devices(fs_devices);
4492 	if (IS_ERR(fs_devices)) {
4493 		ret = PTR_ERR(fs_devices);
4494 		goto out;
4495 	}
4496 
4497 	ret = __btrfs_open_devices(fs_devices, FMODE_READ,
4498 				   root->fs_info->bdev_holder);
4499 	if (ret) {
4500 		free_fs_devices(fs_devices);
4501 		goto out;
4502 	}
4503 
4504 	if (!fs_devices->seeding) {
4505 		__btrfs_close_devices(fs_devices);
4506 		free_fs_devices(fs_devices);
4507 		ret = -EINVAL;
4508 		goto out;
4509 	}
4510 
4511 	fs_devices->seed = root->fs_info->fs_devices->seed;
4512 	root->fs_info->fs_devices->seed = fs_devices;
4513 out:
4514 	return ret;
4515 }
4516 
4517 static int read_one_dev(struct btrfs_root *root,
4518 			struct extent_buffer *leaf,
4519 			struct btrfs_dev_item *dev_item)
4520 {
4521 	struct btrfs_device *device;
4522 	u64 devid;
4523 	int ret;
4524 	u8 fs_uuid[BTRFS_UUID_SIZE];
4525 	u8 dev_uuid[BTRFS_UUID_SIZE];
4526 
4527 	devid = btrfs_device_id(leaf, dev_item);
4528 	read_extent_buffer(leaf, dev_uuid,
4529 			   (unsigned long)btrfs_device_uuid(dev_item),
4530 			   BTRFS_UUID_SIZE);
4531 	read_extent_buffer(leaf, fs_uuid,
4532 			   (unsigned long)btrfs_device_fsid(dev_item),
4533 			   BTRFS_UUID_SIZE);
4534 
4535 	if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4536 		ret = open_seed_devices(root, fs_uuid);
4537 		if (ret && !btrfs_test_opt(root, DEGRADED))
4538 			return ret;
4539 	}
4540 
4541 	device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4542 	if (!device || !device->bdev) {
4543 		if (!btrfs_test_opt(root, DEGRADED))
4544 			return -EIO;
4545 
4546 		if (!device) {
4547 			printk(KERN_WARNING "warning devid %llu missing\n",
4548 			       (unsigned long long)devid);
4549 			device = add_missing_dev(root, devid, dev_uuid);
4550 			if (!device)
4551 				return -ENOMEM;
4552 		} else if (!device->missing) {
4553 			/*
4554 			 * this happens when a device that was properly setup
4555 			 * in the device info lists suddenly goes bad.
4556 			 * device->bdev is NULL, and so we have to set
4557 			 * device->missing to one here
4558 			 */
4559 			root->fs_info->fs_devices->missing_devices++;
4560 			device->missing = 1;
4561 		}
4562 	}
4563 
4564 	if (device->fs_devices != root->fs_info->fs_devices) {
4565 		BUG_ON(device->writeable);
4566 		if (device->generation !=
4567 		    btrfs_device_generation(leaf, dev_item))
4568 			return -EINVAL;
4569 	}
4570 
4571 	fill_device_from_item(leaf, dev_item, device);
4572 	device->dev_root = root->fs_info->dev_root;
4573 	device->in_fs_metadata = 1;
4574 	if (device->writeable) {
4575 		device->fs_devices->total_rw_bytes += device->total_bytes;
4576 		spin_lock(&root->fs_info->free_chunk_lock);
4577 		root->fs_info->free_chunk_space += device->total_bytes -
4578 			device->bytes_used;
4579 		spin_unlock(&root->fs_info->free_chunk_lock);
4580 	}
4581 	ret = 0;
4582 	return ret;
4583 }
4584 
4585 int btrfs_read_sys_array(struct btrfs_root *root)
4586 {
4587 	struct btrfs_super_block *super_copy = root->fs_info->super_copy;
4588 	struct extent_buffer *sb;
4589 	struct btrfs_disk_key *disk_key;
4590 	struct btrfs_chunk *chunk;
4591 	u8 *ptr;
4592 	unsigned long sb_ptr;
4593 	int ret = 0;
4594 	u32 num_stripes;
4595 	u32 array_size;
4596 	u32 len = 0;
4597 	u32 cur;
4598 	struct btrfs_key key;
4599 
4600 	sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
4601 					  BTRFS_SUPER_INFO_SIZE);
4602 	if (!sb)
4603 		return -ENOMEM;
4604 	btrfs_set_buffer_uptodate(sb);
4605 	btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
4606 	/*
4607 	 * The sb extent buffer is artifical and just used to read the system array.
4608 	 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4609 	 * pages up-to-date when the page is larger: extent does not cover the
4610 	 * whole page and consequently check_page_uptodate does not find all
4611 	 * the page's extents up-to-date (the hole beyond sb),
4612 	 * write_extent_buffer then triggers a WARN_ON.
4613 	 *
4614 	 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4615 	 * but sb spans only this function. Add an explicit SetPageUptodate call
4616 	 * to silence the warning eg. on PowerPC 64.
4617 	 */
4618 	if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
4619 		SetPageUptodate(sb->pages[0]);
4620 
4621 	write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
4622 	array_size = btrfs_super_sys_array_size(super_copy);
4623 
4624 	ptr = super_copy->sys_chunk_array;
4625 	sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4626 	cur = 0;
4627 
4628 	while (cur < array_size) {
4629 		disk_key = (struct btrfs_disk_key *)ptr;
4630 		btrfs_disk_key_to_cpu(&key, disk_key);
4631 
4632 		len = sizeof(*disk_key); ptr += len;
4633 		sb_ptr += len;
4634 		cur += len;
4635 
4636 		if (key.type == BTRFS_CHUNK_ITEM_KEY) {
4637 			chunk = (struct btrfs_chunk *)sb_ptr;
4638 			ret = read_one_chunk(root, &key, sb, chunk);
4639 			if (ret)
4640 				break;
4641 			num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4642 			len = btrfs_chunk_item_size(num_stripes);
4643 		} else {
4644 			ret = -EIO;
4645 			break;
4646 		}
4647 		ptr += len;
4648 		sb_ptr += len;
4649 		cur += len;
4650 	}
4651 	free_extent_buffer(sb);
4652 	return ret;
4653 }
4654 
4655 int btrfs_read_chunk_tree(struct btrfs_root *root)
4656 {
4657 	struct btrfs_path *path;
4658 	struct extent_buffer *leaf;
4659 	struct btrfs_key key;
4660 	struct btrfs_key found_key;
4661 	int ret;
4662 	int slot;
4663 
4664 	root = root->fs_info->chunk_root;
4665 
4666 	path = btrfs_alloc_path();
4667 	if (!path)
4668 		return -ENOMEM;
4669 
4670 	mutex_lock(&uuid_mutex);
4671 	lock_chunks(root);
4672 
4673 	/* first we search for all of the device items, and then we
4674 	 * read in all of the chunk items.  This way we can create chunk
4675 	 * mappings that reference all of the devices that are afound
4676 	 */
4677 	key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4678 	key.offset = 0;
4679 	key.type = 0;
4680 again:
4681 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4682 	if (ret < 0)
4683 		goto error;
4684 	while (1) {
4685 		leaf = path->nodes[0];
4686 		slot = path->slots[0];
4687 		if (slot >= btrfs_header_nritems(leaf)) {
4688 			ret = btrfs_next_leaf(root, path);
4689 			if (ret == 0)
4690 				continue;
4691 			if (ret < 0)
4692 				goto error;
4693 			break;
4694 		}
4695 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
4696 		if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4697 			if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4698 				break;
4699 			if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4700 				struct btrfs_dev_item *dev_item;
4701 				dev_item = btrfs_item_ptr(leaf, slot,
4702 						  struct btrfs_dev_item);
4703 				ret = read_one_dev(root, leaf, dev_item);
4704 				if (ret)
4705 					goto error;
4706 			}
4707 		} else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4708 			struct btrfs_chunk *chunk;
4709 			chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4710 			ret = read_one_chunk(root, &found_key, leaf, chunk);
4711 			if (ret)
4712 				goto error;
4713 		}
4714 		path->slots[0]++;
4715 	}
4716 	if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4717 		key.objectid = 0;
4718 		btrfs_release_path(path);
4719 		goto again;
4720 	}
4721 	ret = 0;
4722 error:
4723 	unlock_chunks(root);
4724 	mutex_unlock(&uuid_mutex);
4725 
4726 	btrfs_free_path(path);
4727 	return ret;
4728 }
4729 
4730 static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
4731 {
4732 	int i;
4733 
4734 	for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4735 		btrfs_dev_stat_reset(dev, i);
4736 }
4737 
4738 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
4739 {
4740 	struct btrfs_key key;
4741 	struct btrfs_key found_key;
4742 	struct btrfs_root *dev_root = fs_info->dev_root;
4743 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
4744 	struct extent_buffer *eb;
4745 	int slot;
4746 	int ret = 0;
4747 	struct btrfs_device *device;
4748 	struct btrfs_path *path = NULL;
4749 	int i;
4750 
4751 	path = btrfs_alloc_path();
4752 	if (!path) {
4753 		ret = -ENOMEM;
4754 		goto out;
4755 	}
4756 
4757 	mutex_lock(&fs_devices->device_list_mutex);
4758 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
4759 		int item_size;
4760 		struct btrfs_dev_stats_item *ptr;
4761 
4762 		key.objectid = 0;
4763 		key.type = BTRFS_DEV_STATS_KEY;
4764 		key.offset = device->devid;
4765 		ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
4766 		if (ret) {
4767 			__btrfs_reset_dev_stats(device);
4768 			device->dev_stats_valid = 1;
4769 			btrfs_release_path(path);
4770 			continue;
4771 		}
4772 		slot = path->slots[0];
4773 		eb = path->nodes[0];
4774 		btrfs_item_key_to_cpu(eb, &found_key, slot);
4775 		item_size = btrfs_item_size_nr(eb, slot);
4776 
4777 		ptr = btrfs_item_ptr(eb, slot,
4778 				     struct btrfs_dev_stats_item);
4779 
4780 		for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
4781 			if (item_size >= (1 + i) * sizeof(__le64))
4782 				btrfs_dev_stat_set(device, i,
4783 					btrfs_dev_stats_value(eb, ptr, i));
4784 			else
4785 				btrfs_dev_stat_reset(device, i);
4786 		}
4787 
4788 		device->dev_stats_valid = 1;
4789 		btrfs_dev_stat_print_on_load(device);
4790 		btrfs_release_path(path);
4791 	}
4792 	mutex_unlock(&fs_devices->device_list_mutex);
4793 
4794 out:
4795 	btrfs_free_path(path);
4796 	return ret < 0 ? ret : 0;
4797 }
4798 
4799 static int update_dev_stat_item(struct btrfs_trans_handle *trans,
4800 				struct btrfs_root *dev_root,
4801 				struct btrfs_device *device)
4802 {
4803 	struct btrfs_path *path;
4804 	struct btrfs_key key;
4805 	struct extent_buffer *eb;
4806 	struct btrfs_dev_stats_item *ptr;
4807 	int ret;
4808 	int i;
4809 
4810 	key.objectid = 0;
4811 	key.type = BTRFS_DEV_STATS_KEY;
4812 	key.offset = device->devid;
4813 
4814 	path = btrfs_alloc_path();
4815 	BUG_ON(!path);
4816 	ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
4817 	if (ret < 0) {
4818 		printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
4819 			      ret, rcu_str_deref(device->name));
4820 		goto out;
4821 	}
4822 
4823 	if (ret == 0 &&
4824 	    btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
4825 		/* need to delete old one and insert a new one */
4826 		ret = btrfs_del_item(trans, dev_root, path);
4827 		if (ret != 0) {
4828 			printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
4829 				      rcu_str_deref(device->name), ret);
4830 			goto out;
4831 		}
4832 		ret = 1;
4833 	}
4834 
4835 	if (ret == 1) {
4836 		/* need to insert a new item */
4837 		btrfs_release_path(path);
4838 		ret = btrfs_insert_empty_item(trans, dev_root, path,
4839 					      &key, sizeof(*ptr));
4840 		if (ret < 0) {
4841 			printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
4842 				      rcu_str_deref(device->name), ret);
4843 			goto out;
4844 		}
4845 	}
4846 
4847 	eb = path->nodes[0];
4848 	ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
4849 	for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4850 		btrfs_set_dev_stats_value(eb, ptr, i,
4851 					  btrfs_dev_stat_read(device, i));
4852 	btrfs_mark_buffer_dirty(eb);
4853 
4854 out:
4855 	btrfs_free_path(path);
4856 	return ret;
4857 }
4858 
4859 /*
4860  * called from commit_transaction. Writes all changed device stats to disk.
4861  */
4862 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
4863 			struct btrfs_fs_info *fs_info)
4864 {
4865 	struct btrfs_root *dev_root = fs_info->dev_root;
4866 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
4867 	struct btrfs_device *device;
4868 	int ret = 0;
4869 
4870 	mutex_lock(&fs_devices->device_list_mutex);
4871 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
4872 		if (!device->dev_stats_valid || !device->dev_stats_dirty)
4873 			continue;
4874 
4875 		ret = update_dev_stat_item(trans, dev_root, device);
4876 		if (!ret)
4877 			device->dev_stats_dirty = 0;
4878 	}
4879 	mutex_unlock(&fs_devices->device_list_mutex);
4880 
4881 	return ret;
4882 }
4883 
4884 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
4885 {
4886 	btrfs_dev_stat_inc(dev, index);
4887 	btrfs_dev_stat_print_on_error(dev);
4888 }
4889 
4890 void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
4891 {
4892 	if (!dev->dev_stats_valid)
4893 		return;
4894 	printk_ratelimited_in_rcu(KERN_ERR
4895 			   "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
4896 			   rcu_str_deref(dev->name),
4897 			   btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
4898 			   btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
4899 			   btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
4900 			   btrfs_dev_stat_read(dev,
4901 					       BTRFS_DEV_STAT_CORRUPTION_ERRS),
4902 			   btrfs_dev_stat_read(dev,
4903 					       BTRFS_DEV_STAT_GENERATION_ERRS));
4904 }
4905 
4906 static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
4907 {
4908 	int i;
4909 
4910 	for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4911 		if (btrfs_dev_stat_read(dev, i) != 0)
4912 			break;
4913 	if (i == BTRFS_DEV_STAT_VALUES_MAX)
4914 		return; /* all values == 0, suppress message */
4915 
4916 	printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
4917 	       rcu_str_deref(dev->name),
4918 	       btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
4919 	       btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
4920 	       btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
4921 	       btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
4922 	       btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
4923 }
4924 
4925 int btrfs_get_dev_stats(struct btrfs_root *root,
4926 			struct btrfs_ioctl_get_dev_stats *stats)
4927 {
4928 	struct btrfs_device *dev;
4929 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4930 	int i;
4931 
4932 	mutex_lock(&fs_devices->device_list_mutex);
4933 	dev = btrfs_find_device(root, stats->devid, NULL, NULL);
4934 	mutex_unlock(&fs_devices->device_list_mutex);
4935 
4936 	if (!dev) {
4937 		printk(KERN_WARNING
4938 		       "btrfs: get dev_stats failed, device not found\n");
4939 		return -ENODEV;
4940 	} else if (!dev->dev_stats_valid) {
4941 		printk(KERN_WARNING
4942 		       "btrfs: get dev_stats failed, not yet valid\n");
4943 		return -ENODEV;
4944 	} else if (stats->flags & BTRFS_DEV_STATS_RESET) {
4945 		for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
4946 			if (stats->nr_items > i)
4947 				stats->values[i] =
4948 					btrfs_dev_stat_read_and_reset(dev, i);
4949 			else
4950 				btrfs_dev_stat_reset(dev, i);
4951 		}
4952 	} else {
4953 		for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4954 			if (stats->nr_items > i)
4955 				stats->values[i] = btrfs_dev_stat_read(dev, i);
4956 	}
4957 	if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
4958 		stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
4959 	return 0;
4960 }
4961