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