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