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