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