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