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