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 | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 23 | #include <asm/div64.h> |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 24 | #include "ctree.h" |
| 25 | #include "extent_map.h" |
| 26 | #include "disk-io.h" |
| 27 | #include "transaction.h" |
| 28 | #include "print-tree.h" |
| 29 | #include "volumes.h" |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 30 | #include "async-thread.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 31 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 32 | struct map_lookup { |
| 33 | u64 type; |
| 34 | int io_align; |
| 35 | int io_width; |
| 36 | int stripe_len; |
| 37 | int sector_size; |
| 38 | int num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 39 | int sub_stripes; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 40 | struct btrfs_bio_stripe stripes[]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | #define map_lookup_size(n) (sizeof(struct map_lookup) + \ |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 44 | (sizeof(struct btrfs_bio_stripe) * (n))) |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 45 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 46 | static DEFINE_MUTEX(uuid_mutex); |
| 47 | static LIST_HEAD(fs_uuids); |
| 48 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 49 | void btrfs_lock_volumes(void) |
| 50 | { |
| 51 | mutex_lock(&uuid_mutex); |
| 52 | } |
| 53 | |
| 54 | void btrfs_unlock_volumes(void) |
| 55 | { |
| 56 | mutex_unlock(&uuid_mutex); |
| 57 | } |
| 58 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 59 | static void lock_chunks(struct btrfs_root *root) |
| 60 | { |
| 61 | mutex_lock(&root->fs_info->alloc_mutex); |
| 62 | mutex_lock(&root->fs_info->chunk_mutex); |
| 63 | } |
| 64 | |
| 65 | static void unlock_chunks(struct btrfs_root *root) |
| 66 | { |
| 67 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 68 | mutex_unlock(&root->fs_info->chunk_mutex); |
| 69 | } |
| 70 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 71 | int btrfs_cleanup_fs_uuids(void) |
| 72 | { |
| 73 | struct btrfs_fs_devices *fs_devices; |
| 74 | struct list_head *uuid_cur; |
| 75 | struct list_head *devices_cur; |
| 76 | struct btrfs_device *dev; |
| 77 | |
| 78 | list_for_each(uuid_cur, &fs_uuids) { |
| 79 | fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices, |
| 80 | list); |
| 81 | while(!list_empty(&fs_devices->devices)) { |
| 82 | devices_cur = fs_devices->devices.next; |
| 83 | dev = list_entry(devices_cur, struct btrfs_device, |
| 84 | dev_list); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 85 | if (dev->bdev) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 86 | close_bdev_excl(dev->bdev); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 87 | fs_devices->open_devices--; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 88 | } |
| 89 | list_del(&dev->dev_list); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 90 | kfree(dev->name); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 91 | kfree(dev); |
| 92 | } |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 97 | static struct btrfs_device *__find_device(struct list_head *head, u64 devid, |
| 98 | u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 99 | { |
| 100 | struct btrfs_device *dev; |
| 101 | struct list_head *cur; |
| 102 | |
| 103 | list_for_each(cur, head) { |
| 104 | dev = list_entry(cur, struct btrfs_device, dev_list); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 105 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 106 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 107 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 108 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 109 | } |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | static struct btrfs_fs_devices *find_fsid(u8 *fsid) |
| 114 | { |
| 115 | struct list_head *cur; |
| 116 | struct btrfs_fs_devices *fs_devices; |
| 117 | |
| 118 | list_for_each(cur, &fs_uuids) { |
| 119 | fs_devices = list_entry(cur, struct btrfs_fs_devices, list); |
| 120 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 121 | return fs_devices; |
| 122 | } |
| 123 | return NULL; |
| 124 | } |
| 125 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 126 | /* |
| 127 | * we try to collect pending bios for a device so we don't get a large |
| 128 | * number of procs sending bios down to the same device. This greatly |
| 129 | * improves the schedulers ability to collect and merge the bios. |
| 130 | * |
| 131 | * But, it also turns into a long list of bios to process and that is sure |
| 132 | * to eventually make the worker thread block. The solution here is to |
| 133 | * make some progress and then put this work struct back at the end of |
| 134 | * the list if the block device is congested. This way, multiple devices |
| 135 | * can make progress from a single worker thread. |
| 136 | */ |
| 137 | int run_scheduled_bios(struct btrfs_device *device) |
| 138 | { |
| 139 | struct bio *pending; |
| 140 | struct backing_dev_info *bdi; |
| 141 | struct bio *tail; |
| 142 | struct bio *cur; |
| 143 | int again = 0; |
| 144 | unsigned long num_run = 0; |
| 145 | |
| 146 | bdi = device->bdev->bd_inode->i_mapping->backing_dev_info; |
| 147 | loop: |
| 148 | spin_lock(&device->io_lock); |
| 149 | |
| 150 | /* take all the bios off the list at once and process them |
| 151 | * later on (without the lock held). But, remember the |
| 152 | * tail and other pointers so the bios can be properly reinserted |
| 153 | * into the list if we hit congestion |
| 154 | */ |
| 155 | pending = device->pending_bios; |
| 156 | tail = device->pending_bio_tail; |
| 157 | WARN_ON(pending && !tail); |
| 158 | device->pending_bios = NULL; |
| 159 | device->pending_bio_tail = NULL; |
| 160 | |
| 161 | /* |
| 162 | * if pending was null this time around, no bios need processing |
| 163 | * at all and we can stop. Otherwise it'll loop back up again |
| 164 | * and do an additional check so no bios are missed. |
| 165 | * |
| 166 | * device->running_pending is used to synchronize with the |
| 167 | * schedule_bio code. |
| 168 | */ |
| 169 | if (pending) { |
| 170 | again = 1; |
| 171 | device->running_pending = 1; |
| 172 | } else { |
| 173 | again = 0; |
| 174 | device->running_pending = 0; |
| 175 | } |
| 176 | spin_unlock(&device->io_lock); |
| 177 | |
| 178 | while(pending) { |
| 179 | cur = pending; |
| 180 | pending = pending->bi_next; |
| 181 | cur->bi_next = NULL; |
| 182 | atomic_dec(&device->dev_root->fs_info->nr_async_submits); |
| 183 | submit_bio(cur->bi_rw, cur); |
| 184 | num_run++; |
| 185 | |
| 186 | /* |
| 187 | * we made progress, there is more work to do and the bdi |
| 188 | * is now congested. Back off and let other work structs |
| 189 | * run instead |
| 190 | */ |
| 191 | if (pending && num_run && bdi_write_congested(bdi)) { |
| 192 | struct bio *old_head; |
| 193 | |
| 194 | spin_lock(&device->io_lock); |
| 195 | old_head = device->pending_bios; |
| 196 | device->pending_bios = pending; |
| 197 | if (device->pending_bio_tail) |
| 198 | tail->bi_next = old_head; |
| 199 | else |
| 200 | device->pending_bio_tail = tail; |
| 201 | |
| 202 | spin_unlock(&device->io_lock); |
| 203 | btrfs_requeue_work(&device->work); |
| 204 | goto done; |
| 205 | } |
| 206 | } |
| 207 | if (again) |
| 208 | goto loop; |
| 209 | done: |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | void pending_bios_fn(struct btrfs_work *work) |
| 214 | { |
| 215 | struct btrfs_device *device; |
| 216 | |
| 217 | device = container_of(work, struct btrfs_device, work); |
| 218 | run_scheduled_bios(device); |
| 219 | } |
| 220 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 221 | static int device_list_add(const char *path, |
| 222 | struct btrfs_super_block *disk_super, |
| 223 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) |
| 224 | { |
| 225 | struct btrfs_device *device; |
| 226 | struct btrfs_fs_devices *fs_devices; |
| 227 | u64 found_transid = btrfs_super_generation(disk_super); |
| 228 | |
| 229 | fs_devices = find_fsid(disk_super->fsid); |
| 230 | if (!fs_devices) { |
Chris Mason | 515dc32 | 2008-05-16 13:30:15 -0400 | [diff] [blame] | 231 | fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 232 | if (!fs_devices) |
| 233 | return -ENOMEM; |
| 234 | INIT_LIST_HEAD(&fs_devices->devices); |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 235 | INIT_LIST_HEAD(&fs_devices->alloc_list); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 236 | list_add(&fs_devices->list, &fs_uuids); |
| 237 | memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE); |
| 238 | fs_devices->latest_devid = devid; |
| 239 | fs_devices->latest_trans = found_transid; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 240 | device = NULL; |
| 241 | } else { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 242 | device = __find_device(&fs_devices->devices, devid, |
| 243 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 244 | } |
| 245 | if (!device) { |
| 246 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 247 | if (!device) { |
| 248 | /* we can safely leave the fs_devices entry around */ |
| 249 | return -ENOMEM; |
| 250 | } |
| 251 | device->devid = devid; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 252 | device->work.func = pending_bios_fn; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 253 | memcpy(device->uuid, disk_super->dev_item.uuid, |
| 254 | BTRFS_UUID_SIZE); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 255 | device->barriers = 1; |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 256 | spin_lock_init(&device->io_lock); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 257 | device->name = kstrdup(path, GFP_NOFS); |
| 258 | if (!device->name) { |
| 259 | kfree(device); |
| 260 | return -ENOMEM; |
| 261 | } |
| 262 | list_add(&device->dev_list, &fs_devices->devices); |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 263 | list_add(&device->dev_alloc_list, &fs_devices->alloc_list); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 264 | fs_devices->num_devices++; |
| 265 | } |
| 266 | |
| 267 | if (found_transid > fs_devices->latest_trans) { |
| 268 | fs_devices->latest_devid = devid; |
| 269 | fs_devices->latest_trans = found_transid; |
| 270 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 271 | *fs_devices_ret = fs_devices; |
| 272 | return 0; |
| 273 | } |
| 274 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 275 | int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices) |
| 276 | { |
| 277 | struct list_head *head = &fs_devices->devices; |
| 278 | struct list_head *cur; |
| 279 | struct btrfs_device *device; |
| 280 | |
| 281 | mutex_lock(&uuid_mutex); |
| 282 | again: |
| 283 | list_for_each(cur, head) { |
| 284 | device = list_entry(cur, struct btrfs_device, dev_list); |
| 285 | if (!device->in_fs_metadata) { |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 286 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 287 | list_del(&device->dev_list); |
| 288 | list_del(&device->dev_alloc_list); |
| 289 | fs_devices->num_devices--; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 290 | if (device->bdev) { |
| 291 | bdev = device->bdev; |
| 292 | fs_devices->open_devices--; |
| 293 | mutex_unlock(&uuid_mutex); |
| 294 | close_bdev_excl(bdev); |
| 295 | mutex_lock(&uuid_mutex); |
| 296 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 297 | kfree(device->name); |
| 298 | kfree(device); |
| 299 | goto again; |
| 300 | } |
| 301 | } |
| 302 | mutex_unlock(&uuid_mutex); |
| 303 | return 0; |
| 304 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 305 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 306 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 307 | { |
| 308 | struct list_head *head = &fs_devices->devices; |
| 309 | struct list_head *cur; |
| 310 | struct btrfs_device *device; |
| 311 | |
| 312 | mutex_lock(&uuid_mutex); |
| 313 | list_for_each(cur, head) { |
| 314 | device = list_entry(cur, struct btrfs_device, dev_list); |
| 315 | if (device->bdev) { |
| 316 | close_bdev_excl(device->bdev); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 317 | fs_devices->open_devices--; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 318 | } |
| 319 | device->bdev = NULL; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 320 | device->in_fs_metadata = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 321 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 322 | fs_devices->mounted = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 323 | mutex_unlock(&uuid_mutex); |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 328 | int flags, void *holder) |
| 329 | { |
| 330 | struct block_device *bdev; |
| 331 | struct list_head *head = &fs_devices->devices; |
| 332 | struct list_head *cur; |
| 333 | struct btrfs_device *device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 334 | struct block_device *latest_bdev = NULL; |
| 335 | struct buffer_head *bh; |
| 336 | struct btrfs_super_block *disk_super; |
| 337 | u64 latest_devid = 0; |
| 338 | u64 latest_transid = 0; |
| 339 | u64 transid; |
| 340 | u64 devid; |
| 341 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 342 | |
| 343 | mutex_lock(&uuid_mutex); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 344 | if (fs_devices->mounted) |
| 345 | goto out; |
| 346 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 347 | list_for_each(cur, head) { |
| 348 | device = list_entry(cur, struct btrfs_device, dev_list); |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 349 | if (device->bdev) |
| 350 | continue; |
| 351 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 352 | if (!device->name) |
| 353 | continue; |
| 354 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 355 | bdev = open_bdev_excl(device->name, flags, holder); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 356 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 357 | if (IS_ERR(bdev)) { |
| 358 | printk("open %s failed\n", device->name); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 359 | goto error; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 360 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 361 | set_blocksize(bdev, 4096); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 362 | |
| 363 | bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096); |
| 364 | if (!bh) |
| 365 | goto error_close; |
| 366 | |
| 367 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 368 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, |
| 369 | sizeof(disk_super->magic))) |
| 370 | goto error_brelse; |
| 371 | |
| 372 | devid = le64_to_cpu(disk_super->dev_item.devid); |
| 373 | if (devid != device->devid) |
| 374 | goto error_brelse; |
| 375 | |
| 376 | transid = btrfs_super_generation(disk_super); |
Chris Mason | 6af5ac3 | 2008-05-16 13:14:57 -0400 | [diff] [blame] | 377 | if (!latest_transid || transid > latest_transid) { |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 378 | latest_devid = devid; |
| 379 | latest_transid = transid; |
| 380 | latest_bdev = bdev; |
| 381 | } |
| 382 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 383 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 384 | device->in_fs_metadata = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 385 | fs_devices->open_devices++; |
| 386 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 387 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 388 | error_brelse: |
| 389 | brelse(bh); |
| 390 | error_close: |
| 391 | close_bdev_excl(bdev); |
| 392 | error: |
| 393 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 394 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 395 | if (fs_devices->open_devices == 0) { |
| 396 | ret = -EIO; |
| 397 | goto out; |
| 398 | } |
| 399 | fs_devices->mounted = 1; |
| 400 | fs_devices->latest_bdev = latest_bdev; |
| 401 | fs_devices->latest_devid = latest_devid; |
| 402 | fs_devices->latest_trans = latest_transid; |
| 403 | out: |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 404 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 405 | return ret; |
| 406 | } |
| 407 | |
| 408 | int btrfs_scan_one_device(const char *path, int flags, void *holder, |
| 409 | struct btrfs_fs_devices **fs_devices_ret) |
| 410 | { |
| 411 | struct btrfs_super_block *disk_super; |
| 412 | struct block_device *bdev; |
| 413 | struct buffer_head *bh; |
| 414 | int ret; |
| 415 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 416 | u64 transid; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 417 | |
| 418 | mutex_lock(&uuid_mutex); |
| 419 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 420 | bdev = open_bdev_excl(path, flags, holder); |
| 421 | |
| 422 | if (IS_ERR(bdev)) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 423 | ret = PTR_ERR(bdev); |
| 424 | goto error; |
| 425 | } |
| 426 | |
| 427 | ret = set_blocksize(bdev, 4096); |
| 428 | if (ret) |
| 429 | goto error_close; |
| 430 | bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096); |
| 431 | if (!bh) { |
| 432 | ret = -EIO; |
| 433 | goto error_close; |
| 434 | } |
| 435 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 436 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, |
| 437 | sizeof(disk_super->magic))) { |
Yan | e58ca02 | 2008-04-01 11:21:34 -0400 | [diff] [blame] | 438 | ret = -EINVAL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 439 | goto error_brelse; |
| 440 | } |
| 441 | devid = le64_to_cpu(disk_super->dev_item.devid); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 442 | transid = btrfs_super_generation(disk_super); |
Chris Mason | 7ae9c09 | 2008-04-18 10:29:49 -0400 | [diff] [blame] | 443 | if (disk_super->label[0]) |
| 444 | printk("device label %s ", disk_super->label); |
| 445 | else { |
| 446 | /* FIXME, make a readl uuid parser */ |
| 447 | printk("device fsid %llx-%llx ", |
| 448 | *(unsigned long long *)disk_super->fsid, |
| 449 | *(unsigned long long *)(disk_super->fsid + 8)); |
| 450 | } |
| 451 | printk("devid %Lu transid %Lu %s\n", devid, transid, path); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 452 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
| 453 | |
| 454 | error_brelse: |
| 455 | brelse(bh); |
| 456 | error_close: |
| 457 | close_bdev_excl(bdev); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 458 | error: |
| 459 | mutex_unlock(&uuid_mutex); |
| 460 | return ret; |
| 461 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 462 | |
| 463 | /* |
| 464 | * this uses a pretty simple search, the expectation is that it is |
| 465 | * called very infrequently and that a given device has a small number |
| 466 | * of extents |
| 467 | */ |
| 468 | static int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 469 | struct btrfs_device *device, |
| 470 | struct btrfs_path *path, |
| 471 | u64 num_bytes, u64 *start) |
| 472 | { |
| 473 | struct btrfs_key key; |
| 474 | struct btrfs_root *root = device->dev_root; |
| 475 | struct btrfs_dev_extent *dev_extent = NULL; |
| 476 | u64 hole_size = 0; |
| 477 | u64 last_byte = 0; |
| 478 | u64 search_start = 0; |
| 479 | u64 search_end = device->total_bytes; |
| 480 | int ret; |
| 481 | int slot = 0; |
| 482 | int start_found; |
| 483 | struct extent_buffer *l; |
| 484 | |
| 485 | start_found = 0; |
| 486 | path->reada = 2; |
| 487 | |
| 488 | /* FIXME use last free of some kind */ |
| 489 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 490 | /* we don't want to overwrite the superblock on the drive, |
| 491 | * so we make sure to start at an offset of at least 1MB |
| 492 | */ |
| 493 | search_start = max((u64)1024 * 1024, search_start); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 494 | |
| 495 | if (root->fs_info->alloc_start + num_bytes <= device->total_bytes) |
| 496 | search_start = max(root->fs_info->alloc_start, search_start); |
| 497 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 498 | key.objectid = device->devid; |
| 499 | key.offset = search_start; |
| 500 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 501 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); |
| 502 | if (ret < 0) |
| 503 | goto error; |
| 504 | ret = btrfs_previous_item(root, path, 0, key.type); |
| 505 | if (ret < 0) |
| 506 | goto error; |
| 507 | l = path->nodes[0]; |
| 508 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 509 | while (1) { |
| 510 | l = path->nodes[0]; |
| 511 | slot = path->slots[0]; |
| 512 | if (slot >= btrfs_header_nritems(l)) { |
| 513 | ret = btrfs_next_leaf(root, path); |
| 514 | if (ret == 0) |
| 515 | continue; |
| 516 | if (ret < 0) |
| 517 | goto error; |
| 518 | no_more_items: |
| 519 | if (!start_found) { |
| 520 | if (search_start >= search_end) { |
| 521 | ret = -ENOSPC; |
| 522 | goto error; |
| 523 | } |
| 524 | *start = search_start; |
| 525 | start_found = 1; |
| 526 | goto check_pending; |
| 527 | } |
| 528 | *start = last_byte > search_start ? |
| 529 | last_byte : search_start; |
| 530 | if (search_end <= *start) { |
| 531 | ret = -ENOSPC; |
| 532 | goto error; |
| 533 | } |
| 534 | goto check_pending; |
| 535 | } |
| 536 | btrfs_item_key_to_cpu(l, &key, slot); |
| 537 | |
| 538 | if (key.objectid < device->devid) |
| 539 | goto next; |
| 540 | |
| 541 | if (key.objectid > device->devid) |
| 542 | goto no_more_items; |
| 543 | |
| 544 | if (key.offset >= search_start && key.offset > last_byte && |
| 545 | start_found) { |
| 546 | if (last_byte < search_start) |
| 547 | last_byte = search_start; |
| 548 | hole_size = key.offset - last_byte; |
| 549 | if (key.offset > last_byte && |
| 550 | hole_size >= num_bytes) { |
| 551 | *start = last_byte; |
| 552 | goto check_pending; |
| 553 | } |
| 554 | } |
| 555 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) { |
| 556 | goto next; |
| 557 | } |
| 558 | |
| 559 | start_found = 1; |
| 560 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 561 | last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent); |
| 562 | next: |
| 563 | path->slots[0]++; |
| 564 | cond_resched(); |
| 565 | } |
| 566 | check_pending: |
| 567 | /* we have to make sure we didn't find an extent that has already |
| 568 | * been allocated by the map tree or the original allocation |
| 569 | */ |
| 570 | btrfs_release_path(root, path); |
| 571 | BUG_ON(*start < search_start); |
| 572 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 573 | if (*start + num_bytes > search_end) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 574 | ret = -ENOSPC; |
| 575 | goto error; |
| 576 | } |
| 577 | /* check for pending inserts here */ |
| 578 | return 0; |
| 579 | |
| 580 | error: |
| 581 | btrfs_release_path(root, path); |
| 582 | return ret; |
| 583 | } |
| 584 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 585 | int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
| 586 | struct btrfs_device *device, |
| 587 | u64 start) |
| 588 | { |
| 589 | int ret; |
| 590 | struct btrfs_path *path; |
| 591 | struct btrfs_root *root = device->dev_root; |
| 592 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 593 | struct btrfs_key found_key; |
| 594 | struct extent_buffer *leaf = NULL; |
| 595 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 596 | |
| 597 | path = btrfs_alloc_path(); |
| 598 | if (!path) |
| 599 | return -ENOMEM; |
| 600 | |
| 601 | key.objectid = device->devid; |
| 602 | key.offset = start; |
| 603 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 604 | |
| 605 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 606 | if (ret > 0) { |
| 607 | ret = btrfs_previous_item(root, path, key.objectid, |
| 608 | BTRFS_DEV_EXTENT_KEY); |
| 609 | BUG_ON(ret); |
| 610 | leaf = path->nodes[0]; |
| 611 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 612 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 613 | struct btrfs_dev_extent); |
| 614 | BUG_ON(found_key.offset > start || found_key.offset + |
| 615 | btrfs_dev_extent_length(leaf, extent) < start); |
| 616 | ret = 0; |
| 617 | } else if (ret == 0) { |
| 618 | leaf = path->nodes[0]; |
| 619 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 620 | struct btrfs_dev_extent); |
| 621 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 622 | BUG_ON(ret); |
| 623 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 624 | if (device->bytes_used > 0) |
| 625 | device->bytes_used -= btrfs_dev_extent_length(leaf, extent); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 626 | ret = btrfs_del_item(trans, root, path); |
| 627 | BUG_ON(ret); |
| 628 | |
| 629 | btrfs_free_path(path); |
| 630 | return ret; |
| 631 | } |
| 632 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 633 | int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 634 | struct btrfs_device *device, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 635 | u64 chunk_tree, u64 chunk_objectid, |
| 636 | u64 chunk_offset, |
| 637 | u64 num_bytes, u64 *start) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 638 | { |
| 639 | int ret; |
| 640 | struct btrfs_path *path; |
| 641 | struct btrfs_root *root = device->dev_root; |
| 642 | struct btrfs_dev_extent *extent; |
| 643 | struct extent_buffer *leaf; |
| 644 | struct btrfs_key key; |
| 645 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 646 | WARN_ON(!device->in_fs_metadata); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 647 | path = btrfs_alloc_path(); |
| 648 | if (!path) |
| 649 | return -ENOMEM; |
| 650 | |
| 651 | ret = find_free_dev_extent(trans, device, path, num_bytes, start); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 652 | if (ret) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 653 | goto err; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 654 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 655 | |
| 656 | key.objectid = device->devid; |
| 657 | key.offset = *start; |
| 658 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 659 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 660 | sizeof(*extent)); |
| 661 | BUG_ON(ret); |
| 662 | |
| 663 | leaf = path->nodes[0]; |
| 664 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 665 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 666 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 667 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 668 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 669 | |
| 670 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
| 671 | (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent), |
| 672 | BTRFS_UUID_SIZE); |
| 673 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 674 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 675 | btrfs_mark_buffer_dirty(leaf); |
| 676 | err: |
| 677 | btrfs_free_path(path); |
| 678 | return ret; |
| 679 | } |
| 680 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 681 | static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 682 | { |
| 683 | struct btrfs_path *path; |
| 684 | int ret; |
| 685 | struct btrfs_key key; |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 686 | struct btrfs_chunk *chunk; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 687 | struct btrfs_key found_key; |
| 688 | |
| 689 | path = btrfs_alloc_path(); |
| 690 | BUG_ON(!path); |
| 691 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 692 | key.objectid = objectid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 693 | key.offset = (u64)-1; |
| 694 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 695 | |
| 696 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 697 | if (ret < 0) |
| 698 | goto error; |
| 699 | |
| 700 | BUG_ON(ret == 0); |
| 701 | |
| 702 | ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY); |
| 703 | if (ret) { |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 704 | *offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 705 | } else { |
| 706 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 707 | path->slots[0]); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 708 | if (found_key.objectid != objectid) |
| 709 | *offset = 0; |
| 710 | else { |
| 711 | chunk = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 712 | struct btrfs_chunk); |
| 713 | *offset = found_key.offset + |
| 714 | btrfs_chunk_length(path->nodes[0], chunk); |
| 715 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 716 | } |
| 717 | ret = 0; |
| 718 | error: |
| 719 | btrfs_free_path(path); |
| 720 | return ret; |
| 721 | } |
| 722 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 723 | static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path, |
| 724 | u64 *objectid) |
| 725 | { |
| 726 | int ret; |
| 727 | struct btrfs_key key; |
| 728 | struct btrfs_key found_key; |
| 729 | |
| 730 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 731 | key.type = BTRFS_DEV_ITEM_KEY; |
| 732 | key.offset = (u64)-1; |
| 733 | |
| 734 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 735 | if (ret < 0) |
| 736 | goto error; |
| 737 | |
| 738 | BUG_ON(ret == 0); |
| 739 | |
| 740 | ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID, |
| 741 | BTRFS_DEV_ITEM_KEY); |
| 742 | if (ret) { |
| 743 | *objectid = 1; |
| 744 | } else { |
| 745 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 746 | path->slots[0]); |
| 747 | *objectid = found_key.offset + 1; |
| 748 | } |
| 749 | ret = 0; |
| 750 | error: |
| 751 | btrfs_release_path(root, path); |
| 752 | return ret; |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * the device information is stored in the chunk root |
| 757 | * the btrfs_device struct should be fully filled in |
| 758 | */ |
| 759 | int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 760 | struct btrfs_root *root, |
| 761 | struct btrfs_device *device) |
| 762 | { |
| 763 | int ret; |
| 764 | struct btrfs_path *path; |
| 765 | struct btrfs_dev_item *dev_item; |
| 766 | struct extent_buffer *leaf; |
| 767 | struct btrfs_key key; |
| 768 | unsigned long ptr; |
Chris Mason | 006a58a | 2008-05-02 14:43:15 -0400 | [diff] [blame] | 769 | u64 free_devid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 770 | |
| 771 | root = root->fs_info->chunk_root; |
| 772 | |
| 773 | path = btrfs_alloc_path(); |
| 774 | if (!path) |
| 775 | return -ENOMEM; |
| 776 | |
| 777 | ret = find_next_devid(root, path, &free_devid); |
| 778 | if (ret) |
| 779 | goto out; |
| 780 | |
| 781 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 782 | key.type = BTRFS_DEV_ITEM_KEY; |
| 783 | key.offset = free_devid; |
| 784 | |
| 785 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 786 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 787 | if (ret) |
| 788 | goto out; |
| 789 | |
| 790 | leaf = path->nodes[0]; |
| 791 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 792 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 793 | device->devid = free_devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 794 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 795 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 796 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 797 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 798 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 799 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
| 800 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 801 | btrfs_set_device_group(leaf, dev_item, 0); |
| 802 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 803 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 804 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 805 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 806 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 807 | btrfs_mark_buffer_dirty(leaf); |
| 808 | ret = 0; |
| 809 | |
| 810 | out: |
| 811 | btrfs_free_path(path); |
| 812 | return ret; |
| 813 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 814 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 815 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 816 | struct btrfs_device *device) |
| 817 | { |
| 818 | int ret; |
| 819 | struct btrfs_path *path; |
| 820 | struct block_device *bdev = device->bdev; |
| 821 | struct btrfs_device *next_dev; |
| 822 | struct btrfs_key key; |
| 823 | u64 total_bytes; |
| 824 | struct btrfs_fs_devices *fs_devices; |
| 825 | struct btrfs_trans_handle *trans; |
| 826 | |
| 827 | root = root->fs_info->chunk_root; |
| 828 | |
| 829 | path = btrfs_alloc_path(); |
| 830 | if (!path) |
| 831 | return -ENOMEM; |
| 832 | |
| 833 | trans = btrfs_start_transaction(root, 1); |
| 834 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 835 | key.type = BTRFS_DEV_ITEM_KEY; |
| 836 | key.offset = device->devid; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 837 | lock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 838 | |
| 839 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 840 | if (ret < 0) |
| 841 | goto out; |
| 842 | |
| 843 | if (ret > 0) { |
| 844 | ret = -ENOENT; |
| 845 | goto out; |
| 846 | } |
| 847 | |
| 848 | ret = btrfs_del_item(trans, root, path); |
| 849 | if (ret) |
| 850 | goto out; |
| 851 | |
| 852 | /* |
| 853 | * at this point, the device is zero sized. We want to |
| 854 | * remove it from the devices list and zero out the old super |
| 855 | */ |
| 856 | list_del_init(&device->dev_list); |
| 857 | list_del_init(&device->dev_alloc_list); |
| 858 | fs_devices = root->fs_info->fs_devices; |
| 859 | |
| 860 | next_dev = list_entry(fs_devices->devices.next, struct btrfs_device, |
| 861 | dev_list); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 862 | if (bdev == root->fs_info->sb->s_bdev) |
| 863 | root->fs_info->sb->s_bdev = next_dev->bdev; |
| 864 | if (bdev == fs_devices->latest_bdev) |
| 865 | fs_devices->latest_bdev = next_dev->bdev; |
| 866 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 867 | total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy); |
| 868 | btrfs_set_super_num_devices(&root->fs_info->super_copy, |
| 869 | total_bytes - 1); |
| 870 | out: |
| 871 | btrfs_free_path(path); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 872 | unlock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 873 | btrfs_commit_transaction(trans, root); |
| 874 | return ret; |
| 875 | } |
| 876 | |
| 877 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 878 | { |
| 879 | struct btrfs_device *device; |
| 880 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 881 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 882 | struct btrfs_super_block *disk_super; |
| 883 | u64 all_avail; |
| 884 | u64 devid; |
| 885 | int ret = 0; |
| 886 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 887 | mutex_lock(&uuid_mutex); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 888 | mutex_lock(&root->fs_info->volume_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 889 | |
| 890 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 891 | root->fs_info->avail_system_alloc_bits | |
| 892 | root->fs_info->avail_metadata_alloc_bits; |
| 893 | |
| 894 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 895 | btrfs_super_num_devices(&root->fs_info->super_copy) <= 4) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 896 | printk("btrfs: unable to go below four devices on raid10\n"); |
| 897 | ret = -EINVAL; |
| 898 | goto out; |
| 899 | } |
| 900 | |
| 901 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 902 | btrfs_super_num_devices(&root->fs_info->super_copy) <= 2) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 903 | printk("btrfs: unable to go below two devices on raid1\n"); |
| 904 | ret = -EINVAL; |
| 905 | goto out; |
| 906 | } |
| 907 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 908 | if (strcmp(device_path, "missing") == 0) { |
| 909 | struct list_head *cur; |
| 910 | struct list_head *devices; |
| 911 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 912 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 913 | device = NULL; |
| 914 | devices = &root->fs_info->fs_devices->devices; |
| 915 | list_for_each(cur, devices) { |
| 916 | tmp = list_entry(cur, struct btrfs_device, dev_list); |
| 917 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 918 | device = tmp; |
| 919 | break; |
| 920 | } |
| 921 | } |
| 922 | bdev = NULL; |
| 923 | bh = NULL; |
| 924 | disk_super = NULL; |
| 925 | if (!device) { |
| 926 | printk("btrfs: no missing devices found to remove\n"); |
| 927 | goto out; |
| 928 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 929 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 930 | } else { |
| 931 | bdev = open_bdev_excl(device_path, 0, |
| 932 | root->fs_info->bdev_holder); |
| 933 | if (IS_ERR(bdev)) { |
| 934 | ret = PTR_ERR(bdev); |
| 935 | goto out; |
| 936 | } |
| 937 | |
| 938 | bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096); |
| 939 | if (!bh) { |
| 940 | ret = -EIO; |
| 941 | goto error_close; |
| 942 | } |
| 943 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 944 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, |
| 945 | sizeof(disk_super->magic))) { |
| 946 | ret = -ENOENT; |
| 947 | goto error_brelse; |
| 948 | } |
| 949 | if (memcmp(disk_super->fsid, root->fs_info->fsid, |
| 950 | BTRFS_FSID_SIZE)) { |
| 951 | ret = -ENOENT; |
| 952 | goto error_brelse; |
| 953 | } |
| 954 | devid = le64_to_cpu(disk_super->dev_item.devid); |
| 955 | device = btrfs_find_device(root, devid, NULL); |
| 956 | if (!device) { |
| 957 | ret = -ENOENT; |
| 958 | goto error_brelse; |
| 959 | } |
| 960 | |
| 961 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 962 | root->fs_info->fs_devices->num_devices--; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 963 | root->fs_info->fs_devices->open_devices--; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 964 | |
| 965 | ret = btrfs_shrink_device(device, 0); |
| 966 | if (ret) |
| 967 | goto error_brelse; |
| 968 | |
| 969 | |
| 970 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 971 | if (ret) |
| 972 | goto error_brelse; |
| 973 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 974 | if (bh) { |
| 975 | /* make sure this device isn't detected as part of |
| 976 | * the FS anymore |
| 977 | */ |
| 978 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 979 | set_buffer_dirty(bh); |
| 980 | sync_dirty_buffer(bh); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 981 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 982 | brelse(bh); |
| 983 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 984 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 985 | if (device->bdev) { |
| 986 | /* one close for the device struct or super_block */ |
| 987 | close_bdev_excl(device->bdev); |
| 988 | } |
| 989 | if (bdev) { |
| 990 | /* one close for us */ |
| 991 | close_bdev_excl(bdev); |
| 992 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 993 | kfree(device->name); |
| 994 | kfree(device); |
| 995 | ret = 0; |
| 996 | goto out; |
| 997 | |
| 998 | error_brelse: |
| 999 | brelse(bh); |
| 1000 | error_close: |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1001 | if (bdev) |
| 1002 | close_bdev_excl(bdev); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1003 | out: |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1004 | mutex_unlock(&root->fs_info->volume_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1005 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1006 | return ret; |
| 1007 | } |
| 1008 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1009 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 1010 | { |
| 1011 | struct btrfs_trans_handle *trans; |
| 1012 | struct btrfs_device *device; |
| 1013 | struct block_device *bdev; |
| 1014 | struct list_head *cur; |
| 1015 | struct list_head *devices; |
| 1016 | u64 total_bytes; |
| 1017 | int ret = 0; |
| 1018 | |
| 1019 | |
| 1020 | bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder); |
| 1021 | if (!bdev) { |
| 1022 | return -EIO; |
| 1023 | } |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1024 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1025 | mutex_lock(&root->fs_info->volume_mutex); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1026 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1027 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1028 | lock_chunks(root); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1029 | devices = &root->fs_info->fs_devices->devices; |
| 1030 | list_for_each(cur, devices) { |
| 1031 | device = list_entry(cur, struct btrfs_device, dev_list); |
| 1032 | if (device->bdev == bdev) { |
| 1033 | ret = -EEXIST; |
| 1034 | goto out; |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 1039 | if (!device) { |
| 1040 | /* we can safely leave the fs_devices entry around */ |
| 1041 | ret = -ENOMEM; |
| 1042 | goto out_close_bdev; |
| 1043 | } |
| 1044 | |
| 1045 | device->barriers = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1046 | device->work.func = pending_bios_fn; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1047 | generate_random_uuid(device->uuid); |
| 1048 | spin_lock_init(&device->io_lock); |
| 1049 | device->name = kstrdup(device_path, GFP_NOFS); |
| 1050 | if (!device->name) { |
| 1051 | kfree(device); |
| 1052 | goto out_close_bdev; |
| 1053 | } |
| 1054 | device->io_width = root->sectorsize; |
| 1055 | device->io_align = root->sectorsize; |
| 1056 | device->sector_size = root->sectorsize; |
| 1057 | device->total_bytes = i_size_read(bdev->bd_inode); |
| 1058 | device->dev_root = root->fs_info->dev_root; |
| 1059 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1060 | device->in_fs_metadata = 1; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1061 | |
| 1062 | ret = btrfs_add_device(trans, root, device); |
| 1063 | if (ret) |
| 1064 | goto out_close_bdev; |
| 1065 | |
| 1066 | total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
| 1067 | btrfs_set_super_total_bytes(&root->fs_info->super_copy, |
| 1068 | total_bytes + device->total_bytes); |
| 1069 | |
| 1070 | total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy); |
| 1071 | btrfs_set_super_num_devices(&root->fs_info->super_copy, |
| 1072 | total_bytes + 1); |
| 1073 | |
| 1074 | list_add(&device->dev_list, &root->fs_info->fs_devices->devices); |
| 1075 | list_add(&device->dev_alloc_list, |
| 1076 | &root->fs_info->fs_devices->alloc_list); |
| 1077 | root->fs_info->fs_devices->num_devices++; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1078 | root->fs_info->fs_devices->open_devices++; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1079 | out: |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1080 | unlock_chunks(root); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1081 | btrfs_end_transaction(trans, root); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1082 | mutex_unlock(&root->fs_info->volume_mutex); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1083 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1084 | return ret; |
| 1085 | |
| 1086 | out_close_bdev: |
| 1087 | close_bdev_excl(bdev); |
| 1088 | goto out; |
| 1089 | } |
| 1090 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1091 | int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 1092 | struct btrfs_device *device) |
| 1093 | { |
| 1094 | int ret; |
| 1095 | struct btrfs_path *path; |
| 1096 | struct btrfs_root *root; |
| 1097 | struct btrfs_dev_item *dev_item; |
| 1098 | struct extent_buffer *leaf; |
| 1099 | struct btrfs_key key; |
| 1100 | |
| 1101 | root = device->dev_root->fs_info->chunk_root; |
| 1102 | |
| 1103 | path = btrfs_alloc_path(); |
| 1104 | if (!path) |
| 1105 | return -ENOMEM; |
| 1106 | |
| 1107 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1108 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1109 | key.offset = device->devid; |
| 1110 | |
| 1111 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 1112 | if (ret < 0) |
| 1113 | goto out; |
| 1114 | |
| 1115 | if (ret > 0) { |
| 1116 | ret = -ENOENT; |
| 1117 | goto out; |
| 1118 | } |
| 1119 | |
| 1120 | leaf = path->nodes[0]; |
| 1121 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1122 | |
| 1123 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 1124 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1125 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1126 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1127 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1128 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
| 1129 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
| 1130 | btrfs_mark_buffer_dirty(leaf); |
| 1131 | |
| 1132 | out: |
| 1133 | btrfs_free_path(path); |
| 1134 | return ret; |
| 1135 | } |
| 1136 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1137 | static int __btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1138 | struct btrfs_device *device, u64 new_size) |
| 1139 | { |
| 1140 | struct btrfs_super_block *super_copy = |
| 1141 | &device->dev_root->fs_info->super_copy; |
| 1142 | u64 old_total = btrfs_super_total_bytes(super_copy); |
| 1143 | u64 diff = new_size - device->total_bytes; |
| 1144 | |
| 1145 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
| 1146 | return btrfs_update_device(trans, device); |
| 1147 | } |
| 1148 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1149 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
| 1150 | struct btrfs_device *device, u64 new_size) |
| 1151 | { |
| 1152 | int ret; |
| 1153 | lock_chunks(device->dev_root); |
| 1154 | ret = __btrfs_grow_device(trans, device, new_size); |
| 1155 | unlock_chunks(device->dev_root); |
| 1156 | return ret; |
| 1157 | } |
| 1158 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1159 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
| 1160 | struct btrfs_root *root, |
| 1161 | u64 chunk_tree, u64 chunk_objectid, |
| 1162 | u64 chunk_offset) |
| 1163 | { |
| 1164 | int ret; |
| 1165 | struct btrfs_path *path; |
| 1166 | struct btrfs_key key; |
| 1167 | |
| 1168 | root = root->fs_info->chunk_root; |
| 1169 | path = btrfs_alloc_path(); |
| 1170 | if (!path) |
| 1171 | return -ENOMEM; |
| 1172 | |
| 1173 | key.objectid = chunk_objectid; |
| 1174 | key.offset = chunk_offset; |
| 1175 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 1176 | |
| 1177 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1178 | BUG_ON(ret); |
| 1179 | |
| 1180 | ret = btrfs_del_item(trans, root, path); |
| 1181 | BUG_ON(ret); |
| 1182 | |
| 1183 | btrfs_free_path(path); |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64 |
| 1188 | chunk_offset) |
| 1189 | { |
| 1190 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; |
| 1191 | struct btrfs_disk_key *disk_key; |
| 1192 | struct btrfs_chunk *chunk; |
| 1193 | u8 *ptr; |
| 1194 | int ret = 0; |
| 1195 | u32 num_stripes; |
| 1196 | u32 array_size; |
| 1197 | u32 len = 0; |
| 1198 | u32 cur; |
| 1199 | struct btrfs_key key; |
| 1200 | |
| 1201 | array_size = btrfs_super_sys_array_size(super_copy); |
| 1202 | |
| 1203 | ptr = super_copy->sys_chunk_array; |
| 1204 | cur = 0; |
| 1205 | |
| 1206 | while (cur < array_size) { |
| 1207 | disk_key = (struct btrfs_disk_key *)ptr; |
| 1208 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 1209 | |
| 1210 | len = sizeof(*disk_key); |
| 1211 | |
| 1212 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 1213 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 1214 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 1215 | len += btrfs_chunk_item_size(num_stripes); |
| 1216 | } else { |
| 1217 | ret = -EIO; |
| 1218 | break; |
| 1219 | } |
| 1220 | if (key.objectid == chunk_objectid && |
| 1221 | key.offset == chunk_offset) { |
| 1222 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 1223 | array_size -= len; |
| 1224 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 1225 | } else { |
| 1226 | ptr += len; |
| 1227 | cur += len; |
| 1228 | } |
| 1229 | } |
| 1230 | return ret; |
| 1231 | } |
| 1232 | |
| 1233 | |
| 1234 | int btrfs_relocate_chunk(struct btrfs_root *root, |
| 1235 | u64 chunk_tree, u64 chunk_objectid, |
| 1236 | u64 chunk_offset) |
| 1237 | { |
| 1238 | struct extent_map_tree *em_tree; |
| 1239 | struct btrfs_root *extent_root; |
| 1240 | struct btrfs_trans_handle *trans; |
| 1241 | struct extent_map *em; |
| 1242 | struct map_lookup *map; |
| 1243 | int ret; |
| 1244 | int i; |
| 1245 | |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 1246 | printk("btrfs relocating chunk %llu\n", |
| 1247 | (unsigned long long)chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1248 | root = root->fs_info->chunk_root; |
| 1249 | extent_root = root->fs_info->extent_root; |
| 1250 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 1251 | |
| 1252 | /* step one, relocate all the extents inside this chunk */ |
| 1253 | ret = btrfs_shrink_extent_tree(extent_root, chunk_offset); |
| 1254 | BUG_ON(ret); |
| 1255 | |
| 1256 | trans = btrfs_start_transaction(root, 1); |
| 1257 | BUG_ON(!trans); |
| 1258 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1259 | lock_chunks(root); |
| 1260 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1261 | /* |
| 1262 | * step two, delete the device extents and the |
| 1263 | * chunk tree entries |
| 1264 | */ |
| 1265 | spin_lock(&em_tree->lock); |
| 1266 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 1267 | spin_unlock(&em_tree->lock); |
| 1268 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1269 | BUG_ON(em->start > chunk_offset || |
| 1270 | em->start + em->len < chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1271 | map = (struct map_lookup *)em->bdev; |
| 1272 | |
| 1273 | for (i = 0; i < map->num_stripes; i++) { |
| 1274 | ret = btrfs_free_dev_extent(trans, map->stripes[i].dev, |
| 1275 | map->stripes[i].physical); |
| 1276 | BUG_ON(ret); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1277 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1278 | if (map->stripes[i].dev) { |
| 1279 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 1280 | BUG_ON(ret); |
| 1281 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1282 | } |
| 1283 | ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid, |
| 1284 | chunk_offset); |
| 1285 | |
| 1286 | BUG_ON(ret); |
| 1287 | |
| 1288 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 1289 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 1290 | BUG_ON(ret); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1291 | } |
| 1292 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1293 | spin_lock(&em_tree->lock); |
| 1294 | remove_extent_mapping(em_tree, em); |
| 1295 | kfree(map); |
| 1296 | em->bdev = NULL; |
| 1297 | |
| 1298 | /* once for the tree */ |
| 1299 | free_extent_map(em); |
| 1300 | spin_unlock(&em_tree->lock); |
| 1301 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1302 | /* once for us */ |
| 1303 | free_extent_map(em); |
| 1304 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1305 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1306 | btrfs_end_transaction(trans, root); |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1310 | static u64 div_factor(u64 num, int factor) |
| 1311 | { |
| 1312 | if (factor == 10) |
| 1313 | return num; |
| 1314 | num *= factor; |
| 1315 | do_div(num, 10); |
| 1316 | return num; |
| 1317 | } |
| 1318 | |
| 1319 | |
| 1320 | int btrfs_balance(struct btrfs_root *dev_root) |
| 1321 | { |
| 1322 | int ret; |
| 1323 | struct list_head *cur; |
| 1324 | struct list_head *devices = &dev_root->fs_info->fs_devices->devices; |
| 1325 | struct btrfs_device *device; |
| 1326 | u64 old_size; |
| 1327 | u64 size_to_free; |
| 1328 | struct btrfs_path *path; |
| 1329 | struct btrfs_key key; |
| 1330 | struct btrfs_chunk *chunk; |
| 1331 | struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root; |
| 1332 | struct btrfs_trans_handle *trans; |
| 1333 | struct btrfs_key found_key; |
| 1334 | |
| 1335 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1336 | mutex_lock(&dev_root->fs_info->volume_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1337 | dev_root = dev_root->fs_info->dev_root; |
| 1338 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1339 | /* step one make some room on all the devices */ |
| 1340 | list_for_each(cur, devices) { |
| 1341 | device = list_entry(cur, struct btrfs_device, dev_list); |
| 1342 | old_size = device->total_bytes; |
| 1343 | size_to_free = div_factor(old_size, 1); |
| 1344 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); |
| 1345 | if (device->total_bytes - device->bytes_used > size_to_free) |
| 1346 | continue; |
| 1347 | |
| 1348 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
| 1349 | BUG_ON(ret); |
| 1350 | |
| 1351 | trans = btrfs_start_transaction(dev_root, 1); |
| 1352 | BUG_ON(!trans); |
| 1353 | |
| 1354 | ret = btrfs_grow_device(trans, device, old_size); |
| 1355 | BUG_ON(ret); |
| 1356 | |
| 1357 | btrfs_end_transaction(trans, dev_root); |
| 1358 | } |
| 1359 | |
| 1360 | /* step two, relocate all the chunks */ |
| 1361 | path = btrfs_alloc_path(); |
| 1362 | BUG_ON(!path); |
| 1363 | |
| 1364 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 1365 | key.offset = (u64)-1; |
| 1366 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 1367 | |
| 1368 | while(1) { |
| 1369 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 1370 | if (ret < 0) |
| 1371 | goto error; |
| 1372 | |
| 1373 | /* |
| 1374 | * this shouldn't happen, it means the last relocate |
| 1375 | * failed |
| 1376 | */ |
| 1377 | if (ret == 0) |
| 1378 | break; |
| 1379 | |
| 1380 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 1381 | BTRFS_CHUNK_ITEM_KEY); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1382 | if (ret) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1383 | break; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1384 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1385 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1386 | path->slots[0]); |
| 1387 | if (found_key.objectid != key.objectid) |
| 1388 | break; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1389 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1390 | chunk = btrfs_item_ptr(path->nodes[0], |
| 1391 | path->slots[0], |
| 1392 | struct btrfs_chunk); |
| 1393 | key.offset = found_key.offset; |
| 1394 | /* chunk zero is special */ |
| 1395 | if (key.offset == 0) |
| 1396 | break; |
| 1397 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1398 | btrfs_release_path(chunk_root, path); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1399 | ret = btrfs_relocate_chunk(chunk_root, |
| 1400 | chunk_root->root_key.objectid, |
| 1401 | found_key.objectid, |
| 1402 | found_key.offset); |
| 1403 | BUG_ON(ret); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1404 | } |
| 1405 | ret = 0; |
| 1406 | error: |
| 1407 | btrfs_free_path(path); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1408 | mutex_unlock(&dev_root->fs_info->volume_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1409 | return ret; |
| 1410 | } |
| 1411 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1412 | /* |
| 1413 | * shrinking a device means finding all of the device extents past |
| 1414 | * the new size, and then following the back refs to the chunks. |
| 1415 | * The chunk relocation code actually frees the device extent |
| 1416 | */ |
| 1417 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 1418 | { |
| 1419 | struct btrfs_trans_handle *trans; |
| 1420 | struct btrfs_root *root = device->dev_root; |
| 1421 | struct btrfs_dev_extent *dev_extent = NULL; |
| 1422 | struct btrfs_path *path; |
| 1423 | u64 length; |
| 1424 | u64 chunk_tree; |
| 1425 | u64 chunk_objectid; |
| 1426 | u64 chunk_offset; |
| 1427 | int ret; |
| 1428 | int slot; |
| 1429 | struct extent_buffer *l; |
| 1430 | struct btrfs_key key; |
| 1431 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; |
| 1432 | u64 old_total = btrfs_super_total_bytes(super_copy); |
| 1433 | u64 diff = device->total_bytes - new_size; |
| 1434 | |
| 1435 | |
| 1436 | path = btrfs_alloc_path(); |
| 1437 | if (!path) |
| 1438 | return -ENOMEM; |
| 1439 | |
| 1440 | trans = btrfs_start_transaction(root, 1); |
| 1441 | if (!trans) { |
| 1442 | ret = -ENOMEM; |
| 1443 | goto done; |
| 1444 | } |
| 1445 | |
| 1446 | path->reada = 2; |
| 1447 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1448 | lock_chunks(root); |
| 1449 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1450 | device->total_bytes = new_size; |
| 1451 | ret = btrfs_update_device(trans, device); |
| 1452 | if (ret) { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1453 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1454 | btrfs_end_transaction(trans, root); |
| 1455 | goto done; |
| 1456 | } |
| 1457 | WARN_ON(diff > old_total); |
| 1458 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1459 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1460 | btrfs_end_transaction(trans, root); |
| 1461 | |
| 1462 | key.objectid = device->devid; |
| 1463 | key.offset = (u64)-1; |
| 1464 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1465 | |
| 1466 | while (1) { |
| 1467 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1468 | if (ret < 0) |
| 1469 | goto done; |
| 1470 | |
| 1471 | ret = btrfs_previous_item(root, path, 0, key.type); |
| 1472 | if (ret < 0) |
| 1473 | goto done; |
| 1474 | if (ret) { |
| 1475 | ret = 0; |
| 1476 | goto done; |
| 1477 | } |
| 1478 | |
| 1479 | l = path->nodes[0]; |
| 1480 | slot = path->slots[0]; |
| 1481 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 1482 | |
| 1483 | if (key.objectid != device->devid) |
| 1484 | goto done; |
| 1485 | |
| 1486 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 1487 | length = btrfs_dev_extent_length(l, dev_extent); |
| 1488 | |
| 1489 | if (key.offset + length <= new_size) |
| 1490 | goto done; |
| 1491 | |
| 1492 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); |
| 1493 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); |
| 1494 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
| 1495 | btrfs_release_path(root, path); |
| 1496 | |
| 1497 | ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid, |
| 1498 | chunk_offset); |
| 1499 | if (ret) |
| 1500 | goto done; |
| 1501 | } |
| 1502 | |
| 1503 | done: |
| 1504 | btrfs_free_path(path); |
| 1505 | return ret; |
| 1506 | } |
| 1507 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1508 | int btrfs_add_system_chunk(struct btrfs_trans_handle *trans, |
| 1509 | struct btrfs_root *root, |
| 1510 | struct btrfs_key *key, |
| 1511 | struct btrfs_chunk *chunk, int item_size) |
| 1512 | { |
| 1513 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; |
| 1514 | struct btrfs_disk_key disk_key; |
| 1515 | u32 array_size; |
| 1516 | u8 *ptr; |
| 1517 | |
| 1518 | array_size = btrfs_super_sys_array_size(super_copy); |
| 1519 | if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) |
| 1520 | return -EFBIG; |
| 1521 | |
| 1522 | ptr = super_copy->sys_chunk_array + array_size; |
| 1523 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 1524 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 1525 | ptr += sizeof(disk_key); |
| 1526 | memcpy(ptr, chunk, item_size); |
| 1527 | item_size += sizeof(disk_key); |
| 1528 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1532 | static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes, |
| 1533 | int sub_stripes) |
| 1534 | { |
| 1535 | if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP)) |
| 1536 | return calc_size; |
| 1537 | else if (type & BTRFS_BLOCK_GROUP_RAID10) |
| 1538 | return calc_size * (num_stripes / sub_stripes); |
| 1539 | else |
| 1540 | return calc_size * num_stripes; |
| 1541 | } |
| 1542 | |
| 1543 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1544 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 1545 | struct btrfs_root *extent_root, u64 *start, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1546 | u64 *num_bytes, u64 type) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1547 | { |
| 1548 | u64 dev_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1549 | struct btrfs_fs_info *info = extent_root->fs_info; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1550 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1551 | struct btrfs_path *path; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1552 | struct btrfs_stripe *stripes; |
| 1553 | struct btrfs_device *device = NULL; |
| 1554 | struct btrfs_chunk *chunk; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1555 | struct list_head private_devs; |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 1556 | struct list_head *dev_list; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1557 | struct list_head *cur; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1558 | struct extent_map_tree *em_tree; |
| 1559 | struct map_lookup *map; |
| 1560 | struct extent_map *em; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1561 | int min_stripe_size = 1 * 1024 * 1024; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1562 | u64 physical; |
| 1563 | u64 calc_size = 1024 * 1024 * 1024; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1564 | u64 max_chunk_size = calc_size; |
| 1565 | u64 min_free; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1566 | u64 avail; |
| 1567 | u64 max_avail = 0; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1568 | u64 percent_max; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1569 | int num_stripes = 1; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1570 | int min_stripes = 1; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1571 | int sub_stripes = 0; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1572 | int looped = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1573 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1574 | int index; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1575 | int stripe_len = 64 * 1024; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1576 | struct btrfs_key key; |
| 1577 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1578 | if ((type & BTRFS_BLOCK_GROUP_RAID1) && |
| 1579 | (type & BTRFS_BLOCK_GROUP_DUP)) { |
| 1580 | WARN_ON(1); |
| 1581 | type &= ~BTRFS_BLOCK_GROUP_DUP; |
| 1582 | } |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 1583 | dev_list = &extent_root->fs_info->fs_devices->alloc_list; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1584 | if (list_empty(dev_list)) |
| 1585 | return -ENOSPC; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1586 | |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1587 | if (type & (BTRFS_BLOCK_GROUP_RAID0)) { |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1588 | num_stripes = extent_root->fs_info->fs_devices->open_devices; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1589 | min_stripes = 2; |
| 1590 | } |
| 1591 | if (type & (BTRFS_BLOCK_GROUP_DUP)) { |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1592 | num_stripes = 2; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1593 | min_stripes = 2; |
| 1594 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1595 | if (type & (BTRFS_BLOCK_GROUP_RAID1)) { |
| 1596 | num_stripes = min_t(u64, 2, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1597 | extent_root->fs_info->fs_devices->open_devices); |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1598 | if (num_stripes < 2) |
| 1599 | return -ENOSPC; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1600 | min_stripes = 2; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1601 | } |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1602 | if (type & (BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1603 | num_stripes = extent_root->fs_info->fs_devices->open_devices; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1604 | if (num_stripes < 4) |
| 1605 | return -ENOSPC; |
| 1606 | num_stripes &= ~(u32)1; |
| 1607 | sub_stripes = 2; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1608 | min_stripes = 4; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1609 | } |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1610 | |
| 1611 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 1612 | max_chunk_size = 10 * calc_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1613 | min_stripe_size = 64 * 1024 * 1024; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1614 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
| 1615 | max_chunk_size = 4 * calc_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1616 | min_stripe_size = 32 * 1024 * 1024; |
| 1617 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 1618 | calc_size = 8 * 1024 * 1024; |
| 1619 | max_chunk_size = calc_size * 2; |
| 1620 | min_stripe_size = 1 * 1024 * 1024; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1621 | } |
| 1622 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1623 | path = btrfs_alloc_path(); |
| 1624 | if (!path) |
| 1625 | return -ENOMEM; |
| 1626 | |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1627 | /* we don't want a chunk larger than 10% of the FS */ |
| 1628 | percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1); |
| 1629 | max_chunk_size = min(percent_max, max_chunk_size); |
| 1630 | |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1631 | again: |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1632 | if (calc_size * num_stripes > max_chunk_size) { |
| 1633 | calc_size = max_chunk_size; |
| 1634 | do_div(calc_size, num_stripes); |
| 1635 | do_div(calc_size, stripe_len); |
| 1636 | calc_size *= stripe_len; |
| 1637 | } |
| 1638 | /* we don't want tiny stripes */ |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1639 | calc_size = max_t(u64, min_stripe_size, calc_size); |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1640 | |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1641 | do_div(calc_size, stripe_len); |
| 1642 | calc_size *= stripe_len; |
| 1643 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1644 | INIT_LIST_HEAD(&private_devs); |
| 1645 | cur = dev_list->next; |
| 1646 | index = 0; |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1647 | |
| 1648 | if (type & BTRFS_BLOCK_GROUP_DUP) |
| 1649 | min_free = calc_size * 2; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1650 | else |
| 1651 | min_free = calc_size; |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1652 | |
Chris Mason | ad5bd91 | 2008-04-21 08:28:10 -0400 | [diff] [blame] | 1653 | /* we add 1MB because we never use the first 1MB of the device */ |
| 1654 | min_free += 1024 * 1024; |
| 1655 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1656 | /* build a private list of devices we will allocate from */ |
| 1657 | while(index < num_stripes) { |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 1658 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1659 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1660 | if (device->total_bytes > device->bytes_used) |
| 1661 | avail = device->total_bytes - device->bytes_used; |
| 1662 | else |
| 1663 | avail = 0; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1664 | cur = cur->next; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1665 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1666 | if (device->in_fs_metadata && avail >= min_free) { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1667 | u64 ignored_start = 0; |
| 1668 | ret = find_free_dev_extent(trans, device, path, |
| 1669 | min_free, |
| 1670 | &ignored_start); |
| 1671 | if (ret == 0) { |
| 1672 | list_move_tail(&device->dev_alloc_list, |
| 1673 | &private_devs); |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1674 | index++; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1675 | if (type & BTRFS_BLOCK_GROUP_DUP) |
| 1676 | index++; |
| 1677 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1678 | } else if (device->in_fs_metadata && avail > max_avail) |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1679 | max_avail = avail; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1680 | if (cur == dev_list) |
| 1681 | break; |
| 1682 | } |
| 1683 | if (index < num_stripes) { |
| 1684 | list_splice(&private_devs, dev_list); |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1685 | if (index >= min_stripes) { |
| 1686 | num_stripes = index; |
| 1687 | if (type & (BTRFS_BLOCK_GROUP_RAID10)) { |
| 1688 | num_stripes /= sub_stripes; |
| 1689 | num_stripes *= sub_stripes; |
| 1690 | } |
| 1691 | looped = 1; |
| 1692 | goto again; |
| 1693 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1694 | if (!looped && max_avail > 0) { |
| 1695 | looped = 1; |
| 1696 | calc_size = max_avail; |
| 1697 | goto again; |
| 1698 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1699 | btrfs_free_path(path); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1700 | return -ENOSPC; |
| 1701 | } |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1702 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 1703 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 1704 | ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 1705 | &key.offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1706 | if (ret) { |
| 1707 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1708 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1709 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1710 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1711 | chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1712 | if (!chunk) { |
| 1713 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1714 | return -ENOMEM; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1715 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1716 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1717 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 1718 | if (!map) { |
| 1719 | kfree(chunk); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1720 | btrfs_free_path(path); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1721 | return -ENOMEM; |
| 1722 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1723 | btrfs_free_path(path); |
| 1724 | path = NULL; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1725 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1726 | stripes = &chunk->stripe; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1727 | *num_bytes = chunk_bytes_by_type(type, calc_size, |
| 1728 | num_stripes, sub_stripes); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1729 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1730 | index = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1731 | while(index < num_stripes) { |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1732 | struct btrfs_stripe *stripe; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1733 | BUG_ON(list_empty(&private_devs)); |
| 1734 | cur = private_devs.next; |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 1735 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1736 | |
| 1737 | /* loop over this device again if we're doing a dup group */ |
| 1738 | if (!(type & BTRFS_BLOCK_GROUP_DUP) || |
| 1739 | (index == num_stripes - 1)) |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 1740 | list_move_tail(&device->dev_alloc_list, dev_list); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1741 | |
| 1742 | ret = btrfs_alloc_dev_extent(trans, device, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1743 | info->chunk_root->root_key.objectid, |
| 1744 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset, |
| 1745 | calc_size, &dev_offset); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1746 | BUG_ON(ret); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1747 | device->bytes_used += calc_size; |
| 1748 | ret = btrfs_update_device(trans, device); |
| 1749 | BUG_ON(ret); |
| 1750 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1751 | map->stripes[index].dev = device; |
| 1752 | map->stripes[index].physical = dev_offset; |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1753 | stripe = stripes + index; |
| 1754 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 1755 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 1756 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1757 | physical = dev_offset; |
| 1758 | index++; |
| 1759 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1760 | BUG_ON(!list_empty(&private_devs)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1761 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1762 | /* key was set above */ |
| 1763 | btrfs_set_stack_chunk_length(chunk, *num_bytes); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1764 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1765 | btrfs_set_stack_chunk_stripe_len(chunk, stripe_len); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1766 | btrfs_set_stack_chunk_type(chunk, type); |
| 1767 | btrfs_set_stack_chunk_num_stripes(chunk, num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1768 | btrfs_set_stack_chunk_io_align(chunk, stripe_len); |
| 1769 | btrfs_set_stack_chunk_io_width(chunk, stripe_len); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1770 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1771 | btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1772 | map->sector_size = extent_root->sectorsize; |
| 1773 | map->stripe_len = stripe_len; |
| 1774 | map->io_align = stripe_len; |
| 1775 | map->io_width = stripe_len; |
| 1776 | map->type = type; |
| 1777 | map->num_stripes = num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1778 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1779 | |
| 1780 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, |
| 1781 | btrfs_chunk_item_size(num_stripes)); |
| 1782 | BUG_ON(ret); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1783 | *start = key.offset;; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1784 | |
| 1785 | em = alloc_extent_map(GFP_NOFS); |
| 1786 | if (!em) |
| 1787 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1788 | em->bdev = (struct block_device *)map; |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1789 | em->start = key.offset; |
| 1790 | em->len = *num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1791 | em->block_start = 0; |
| 1792 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1793 | if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 1794 | ret = btrfs_add_system_chunk(trans, chunk_root, &key, |
| 1795 | chunk, btrfs_chunk_item_size(num_stripes)); |
| 1796 | BUG_ON(ret); |
| 1797 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1798 | kfree(chunk); |
| 1799 | |
| 1800 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
| 1801 | spin_lock(&em_tree->lock); |
| 1802 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1803 | spin_unlock(&em_tree->lock); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 1804 | BUG_ON(ret); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1805 | free_extent_map(em); |
| 1806 | return ret; |
| 1807 | } |
| 1808 | |
| 1809 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 1810 | { |
| 1811 | extent_map_tree_init(&tree->map_tree, GFP_NOFS); |
| 1812 | } |
| 1813 | |
| 1814 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 1815 | { |
| 1816 | struct extent_map *em; |
| 1817 | |
| 1818 | while(1) { |
| 1819 | spin_lock(&tree->map_tree.lock); |
| 1820 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 1821 | if (em) |
| 1822 | remove_extent_mapping(&tree->map_tree, em); |
| 1823 | spin_unlock(&tree->map_tree.lock); |
| 1824 | if (!em) |
| 1825 | break; |
| 1826 | kfree(em->bdev); |
| 1827 | /* once for us */ |
| 1828 | free_extent_map(em); |
| 1829 | /* once for the tree */ |
| 1830 | free_extent_map(em); |
| 1831 | } |
| 1832 | } |
| 1833 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1834 | int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len) |
| 1835 | { |
| 1836 | struct extent_map *em; |
| 1837 | struct map_lookup *map; |
| 1838 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 1839 | int ret; |
| 1840 | |
| 1841 | spin_lock(&em_tree->lock); |
| 1842 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 1843 | spin_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1844 | BUG_ON(!em); |
| 1845 | |
| 1846 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 1847 | map = (struct map_lookup *)em->bdev; |
| 1848 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 1849 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1850 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 1851 | ret = map->sub_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1852 | else |
| 1853 | ret = 1; |
| 1854 | free_extent_map(em); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1855 | return ret; |
| 1856 | } |
| 1857 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1858 | static int find_live_mirror(struct map_lookup *map, int first, int num, |
| 1859 | int optimal) |
| 1860 | { |
| 1861 | int i; |
| 1862 | if (map->stripes[optimal].dev->bdev) |
| 1863 | return optimal; |
| 1864 | for (i = first; i < first + num; i++) { |
| 1865 | if (map->stripes[i].dev->bdev) |
| 1866 | return i; |
| 1867 | } |
| 1868 | /* we couldn't find one that doesn't fail. Just return something |
| 1869 | * and the io error handling code will clean up eventually |
| 1870 | */ |
| 1871 | return optimal; |
| 1872 | } |
| 1873 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1874 | static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, |
| 1875 | u64 logical, u64 *length, |
| 1876 | struct btrfs_multi_bio **multi_ret, |
| 1877 | int mirror_num, struct page *unplug_page) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1878 | { |
| 1879 | struct extent_map *em; |
| 1880 | struct map_lookup *map; |
| 1881 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 1882 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1883 | u64 stripe_offset; |
| 1884 | u64 stripe_nr; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1885 | int stripes_allocated = 8; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1886 | int stripes_required = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1887 | int stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1888 | int i; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1889 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 1890 | int max_errors = 0; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1891 | struct btrfs_multi_bio *multi = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1892 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1893 | if (multi_ret && !(rw & (1 << BIO_RW))) { |
| 1894 | stripes_allocated = 1; |
| 1895 | } |
| 1896 | again: |
| 1897 | if (multi_ret) { |
| 1898 | multi = kzalloc(btrfs_multi_bio_size(stripes_allocated), |
| 1899 | GFP_NOFS); |
| 1900 | if (!multi) |
| 1901 | return -ENOMEM; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 1902 | |
| 1903 | atomic_set(&multi->error, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1904 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1905 | |
| 1906 | spin_lock(&em_tree->lock); |
| 1907 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 1908 | spin_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1909 | |
| 1910 | if (!em && unplug_page) |
| 1911 | return 0; |
| 1912 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1913 | if (!em) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1914 | printk("unable to find logical %Lu len %Lu\n", logical, *length); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1915 | BUG(); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1916 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1917 | |
| 1918 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 1919 | map = (struct map_lookup *)em->bdev; |
| 1920 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1921 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1922 | if (mirror_num > map->num_stripes) |
| 1923 | mirror_num = 0; |
| 1924 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1925 | /* 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] | 1926 | if (rw & (1 << BIO_RW)) { |
| 1927 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 1928 | BTRFS_BLOCK_GROUP_DUP)) { |
| 1929 | stripes_required = map->num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 1930 | max_errors = 1; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1931 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 1932 | stripes_required = map->sub_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 1933 | max_errors = 1; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1934 | } |
| 1935 | } |
| 1936 | if (multi_ret && rw == WRITE && |
| 1937 | stripes_allocated < stripes_required) { |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1938 | stripes_allocated = map->num_stripes; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1939 | free_extent_map(em); |
| 1940 | kfree(multi); |
| 1941 | goto again; |
| 1942 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1943 | stripe_nr = offset; |
| 1944 | /* |
| 1945 | * stripe_nr counts the total number of stripes we have to stride |
| 1946 | * to get to this block |
| 1947 | */ |
| 1948 | do_div(stripe_nr, map->stripe_len); |
| 1949 | |
| 1950 | stripe_offset = stripe_nr * map->stripe_len; |
| 1951 | BUG_ON(offset < stripe_offset); |
| 1952 | |
| 1953 | /* stripe_offset is the offset of this block in its stripe*/ |
| 1954 | stripe_offset = offset - stripe_offset; |
| 1955 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1956 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1957 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1958 | BTRFS_BLOCK_GROUP_DUP)) { |
| 1959 | /* we limit the length of each bio to what fits in a stripe */ |
| 1960 | *length = min_t(u64, em->len - offset, |
| 1961 | map->stripe_len - stripe_offset); |
| 1962 | } else { |
| 1963 | *length = em->len - offset; |
| 1964 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1965 | |
| 1966 | if (!multi_ret && !unplug_page) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1967 | goto out; |
| 1968 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1969 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1970 | stripe_index = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1971 | if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1972 | if (unplug_page || (rw & (1 << BIO_RW))) |
| 1973 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 1974 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1975 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1976 | else { |
| 1977 | stripe_index = find_live_mirror(map, 0, |
| 1978 | map->num_stripes, |
| 1979 | current->pid % map->num_stripes); |
| 1980 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 1981 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1982 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1983 | if (rw & (1 << BIO_RW)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1984 | num_stripes = map->num_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1985 | else if (mirror_num) |
| 1986 | stripe_index = mirror_num - 1; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 1987 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1988 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 1989 | int factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1990 | |
| 1991 | stripe_index = do_div(stripe_nr, factor); |
| 1992 | stripe_index *= map->sub_stripes; |
| 1993 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1994 | if (unplug_page || (rw & (1 << BIO_RW))) |
| 1995 | num_stripes = map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1996 | else if (mirror_num) |
| 1997 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1998 | else { |
| 1999 | stripe_index = find_live_mirror(map, stripe_index, |
| 2000 | map->sub_stripes, stripe_index + |
| 2001 | current->pid % map->sub_stripes); |
| 2002 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2003 | } else { |
| 2004 | /* |
| 2005 | * after this do_div call, stripe_nr is the number of stripes |
| 2006 | * on this device we have to walk to find the data, and |
| 2007 | * stripe_index is the number of our device in the stripe array |
| 2008 | */ |
| 2009 | stripe_index = do_div(stripe_nr, map->num_stripes); |
| 2010 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2011 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2012 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2013 | for (i = 0; i < num_stripes; i++) { |
| 2014 | if (unplug_page) { |
| 2015 | struct btrfs_device *device; |
| 2016 | struct backing_dev_info *bdi; |
| 2017 | |
| 2018 | device = map->stripes[stripe_index].dev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2019 | if (device->bdev) { |
| 2020 | bdi = blk_get_backing_dev_info(device->bdev); |
| 2021 | if (bdi->unplug_io_fn) { |
| 2022 | bdi->unplug_io_fn(bdi, unplug_page); |
| 2023 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2024 | } |
| 2025 | } else { |
| 2026 | multi->stripes[i].physical = |
| 2027 | map->stripes[stripe_index].physical + |
| 2028 | stripe_offset + stripe_nr * map->stripe_len; |
| 2029 | multi->stripes[i].dev = map->stripes[stripe_index].dev; |
| 2030 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2031 | stripe_index++; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2032 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2033 | if (multi_ret) { |
| 2034 | *multi_ret = multi; |
| 2035 | multi->num_stripes = num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 2036 | multi->max_errors = max_errors; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2037 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2038 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2039 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2040 | return 0; |
| 2041 | } |
| 2042 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2043 | int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, |
| 2044 | u64 logical, u64 *length, |
| 2045 | struct btrfs_multi_bio **multi_ret, int mirror_num) |
| 2046 | { |
| 2047 | return __btrfs_map_block(map_tree, rw, logical, length, multi_ret, |
| 2048 | mirror_num, NULL); |
| 2049 | } |
| 2050 | |
| 2051 | int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree, |
| 2052 | u64 logical, struct page *page) |
| 2053 | { |
| 2054 | u64 length = PAGE_CACHE_SIZE; |
| 2055 | return __btrfs_map_block(map_tree, READ, logical, &length, |
| 2056 | NULL, 0, page); |
| 2057 | } |
| 2058 | |
| 2059 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2060 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
| 2061 | static void end_bio_multi_stripe(struct bio *bio, int err) |
| 2062 | #else |
| 2063 | static int end_bio_multi_stripe(struct bio *bio, |
| 2064 | unsigned int bytes_done, int err) |
| 2065 | #endif |
| 2066 | { |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2067 | struct btrfs_multi_bio *multi = bio->bi_private; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2068 | |
| 2069 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 2070 | if (bio->bi_size) |
| 2071 | return 1; |
| 2072 | #endif |
| 2073 | if (err) |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 2074 | atomic_inc(&multi->error); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2075 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2076 | if (atomic_dec_and_test(&multi->stripes_pending)) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2077 | bio->bi_private = multi->private; |
| 2078 | bio->bi_end_io = multi->end_io; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 2079 | /* only send an error to the higher layers if it is |
| 2080 | * beyond the tolerance of the multi-bio |
| 2081 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2082 | if (atomic_read(&multi->error) > multi->max_errors) { |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 2083 | err = -EIO; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2084 | } else if (err) { |
| 2085 | /* |
| 2086 | * this bio is actually up to date, we didn't |
| 2087 | * go over the max number of errors |
| 2088 | */ |
| 2089 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 2090 | err = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2091 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2092 | kfree(multi); |
| 2093 | |
Miguel | 73f61b2 | 2008-04-11 15:50:59 -0400 | [diff] [blame] | 2094 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 2095 | bio_endio(bio, bio->bi_size, err); |
| 2096 | #else |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2097 | bio_endio(bio, err); |
Miguel | 73f61b2 | 2008-04-11 15:50:59 -0400 | [diff] [blame] | 2098 | #endif |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2099 | } else { |
| 2100 | bio_put(bio); |
| 2101 | } |
| 2102 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 2103 | return 0; |
| 2104 | #endif |
| 2105 | } |
| 2106 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 2107 | struct async_sched { |
| 2108 | struct bio *bio; |
| 2109 | int rw; |
| 2110 | struct btrfs_fs_info *info; |
| 2111 | struct btrfs_work work; |
| 2112 | }; |
| 2113 | |
| 2114 | /* |
| 2115 | * see run_scheduled_bios for a description of why bios are collected for |
| 2116 | * async submit. |
| 2117 | * |
| 2118 | * This will add one bio to the pending list for a device and make sure |
| 2119 | * the work struct is scheduled. |
| 2120 | */ |
| 2121 | int schedule_bio(struct btrfs_root *root, struct btrfs_device *device, |
| 2122 | int rw, struct bio *bio) |
| 2123 | { |
| 2124 | int should_queue = 1; |
| 2125 | |
| 2126 | /* don't bother with additional async steps for reads, right now */ |
| 2127 | if (!(rw & (1 << BIO_RW))) { |
| 2128 | submit_bio(rw, bio); |
| 2129 | return 0; |
| 2130 | } |
| 2131 | |
| 2132 | /* |
| 2133 | * nr_async_sumbits allows us to reliably return congestion to the |
| 2134 | * higher layers. Otherwise, the async bio makes it appear we have |
| 2135 | * made progress against dirty pages when we've really just put it |
| 2136 | * on a queue for later |
| 2137 | */ |
| 2138 | atomic_inc(&root->fs_info->nr_async_submits); |
| 2139 | bio->bi_next = NULL; |
| 2140 | bio->bi_rw |= rw; |
| 2141 | |
| 2142 | spin_lock(&device->io_lock); |
| 2143 | |
| 2144 | if (device->pending_bio_tail) |
| 2145 | device->pending_bio_tail->bi_next = bio; |
| 2146 | |
| 2147 | device->pending_bio_tail = bio; |
| 2148 | if (!device->pending_bios) |
| 2149 | device->pending_bios = bio; |
| 2150 | if (device->running_pending) |
| 2151 | should_queue = 0; |
| 2152 | |
| 2153 | spin_unlock(&device->io_lock); |
| 2154 | |
| 2155 | if (should_queue) |
Chris Mason | 1cc127b | 2008-06-12 14:46:17 -0400 | [diff] [blame] | 2156 | btrfs_queue_worker(&root->fs_info->submit_workers, |
| 2157 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 2158 | return 0; |
| 2159 | } |
| 2160 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2161 | 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] | 2162 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2163 | { |
| 2164 | struct btrfs_mapping_tree *map_tree; |
| 2165 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2166 | struct bio *first_bio = bio; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2167 | u64 logical = bio->bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2168 | u64 length = 0; |
| 2169 | u64 map_length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2170 | struct btrfs_multi_bio *multi = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2171 | int ret; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2172 | int dev_nr = 0; |
| 2173 | int total_devs = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2174 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2175 | length = bio->bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2176 | map_tree = &root->fs_info->mapping_tree; |
| 2177 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2178 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2179 | ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi, |
| 2180 | mirror_num); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2181 | BUG_ON(ret); |
| 2182 | |
| 2183 | total_devs = multi->num_stripes; |
| 2184 | if (map_length < length) { |
| 2185 | printk("mapping failed logical %Lu bio len %Lu " |
| 2186 | "len %Lu\n", logical, length, map_length); |
| 2187 | BUG(); |
| 2188 | } |
| 2189 | multi->end_io = first_bio->bi_end_io; |
| 2190 | multi->private = first_bio->bi_private; |
| 2191 | atomic_set(&multi->stripes_pending, multi->num_stripes); |
| 2192 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2193 | while(dev_nr < total_devs) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2194 | if (total_devs > 1) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2195 | if (dev_nr < total_devs - 1) { |
| 2196 | bio = bio_clone(first_bio, GFP_NOFS); |
| 2197 | BUG_ON(!bio); |
| 2198 | } else { |
| 2199 | bio = first_bio; |
| 2200 | } |
| 2201 | bio->bi_private = multi; |
| 2202 | bio->bi_end_io = end_bio_multi_stripe; |
| 2203 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2204 | bio->bi_sector = multi->stripes[dev_nr].physical >> 9; |
| 2205 | dev = multi->stripes[dev_nr].dev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2206 | if (dev && dev->bdev) { |
| 2207 | bio->bi_bdev = dev->bdev; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 2208 | if (async_submit) |
| 2209 | schedule_bio(root, dev, rw, bio); |
| 2210 | else |
| 2211 | submit_bio(rw, bio); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2212 | } else { |
| 2213 | bio->bi_bdev = root->fs_info->fs_devices->latest_bdev; |
| 2214 | bio->bi_sector = logical >> 9; |
| 2215 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 2216 | bio_endio(bio, bio->bi_size, -EIO); |
| 2217 | #else |
| 2218 | bio_endio(bio, -EIO); |
| 2219 | #endif |
| 2220 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2221 | dev_nr++; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2222 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2223 | if (total_devs == 1) |
| 2224 | kfree(multi); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2225 | return 0; |
| 2226 | } |
| 2227 | |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 2228 | struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid, |
| 2229 | u8 *uuid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2230 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2231 | struct list_head *head = &root->fs_info->fs_devices->devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2232 | |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 2233 | return __find_device(head, devid, uuid); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2234 | } |
| 2235 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2236 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
| 2237 | u64 devid, u8 *dev_uuid) |
| 2238 | { |
| 2239 | struct btrfs_device *device; |
| 2240 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 2241 | |
| 2242 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 2243 | list_add(&device->dev_list, |
| 2244 | &fs_devices->devices); |
| 2245 | list_add(&device->dev_alloc_list, |
| 2246 | &fs_devices->alloc_list); |
| 2247 | device->barriers = 1; |
| 2248 | device->dev_root = root->fs_info->dev_root; |
| 2249 | device->devid = devid; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 2250 | device->work.func = pending_bios_fn; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2251 | fs_devices->num_devices++; |
| 2252 | spin_lock_init(&device->io_lock); |
| 2253 | memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE); |
| 2254 | return device; |
| 2255 | } |
| 2256 | |
| 2257 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2258 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 2259 | struct extent_buffer *leaf, |
| 2260 | struct btrfs_chunk *chunk) |
| 2261 | { |
| 2262 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 2263 | struct map_lookup *map; |
| 2264 | struct extent_map *em; |
| 2265 | u64 logical; |
| 2266 | u64 length; |
| 2267 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 2268 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2269 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2270 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2271 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2272 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 2273 | logical = key->offset; |
| 2274 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2275 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2276 | spin_lock(&map_tree->map_tree.lock); |
| 2277 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 2278 | spin_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2279 | |
| 2280 | /* already mapped? */ |
| 2281 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 2282 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2283 | return 0; |
| 2284 | } else if (em) { |
| 2285 | free_extent_map(em); |
| 2286 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2287 | |
| 2288 | map = kzalloc(sizeof(*map), GFP_NOFS); |
| 2289 | if (!map) |
| 2290 | return -ENOMEM; |
| 2291 | |
| 2292 | em = alloc_extent_map(GFP_NOFS); |
| 2293 | if (!em) |
| 2294 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2295 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2296 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2297 | if (!map) { |
| 2298 | free_extent_map(em); |
| 2299 | return -ENOMEM; |
| 2300 | } |
| 2301 | |
| 2302 | em->bdev = (struct block_device *)map; |
| 2303 | em->start = logical; |
| 2304 | em->len = length; |
| 2305 | em->block_start = 0; |
| 2306 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2307 | map->num_stripes = num_stripes; |
| 2308 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 2309 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 2310 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 2311 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 2312 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2313 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2314 | for (i = 0; i < num_stripes; i++) { |
| 2315 | map->stripes[i].physical = |
| 2316 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 2317 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 2318 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 2319 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 2320 | BTRFS_UUID_SIZE); |
| 2321 | map->stripes[i].dev = btrfs_find_device(root, devid, uuid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2322 | |
| 2323 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2324 | kfree(map); |
| 2325 | free_extent_map(em); |
| 2326 | return -EIO; |
| 2327 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2328 | if (!map->stripes[i].dev) { |
| 2329 | map->stripes[i].dev = |
| 2330 | add_missing_dev(root, devid, uuid); |
| 2331 | if (!map->stripes[i].dev) { |
| 2332 | kfree(map); |
| 2333 | free_extent_map(em); |
| 2334 | return -EIO; |
| 2335 | } |
| 2336 | } |
| 2337 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | spin_lock(&map_tree->map_tree.lock); |
| 2341 | ret = add_extent_mapping(&map_tree->map_tree, em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2342 | spin_unlock(&map_tree->map_tree.lock); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 2343 | BUG_ON(ret); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2344 | free_extent_map(em); |
| 2345 | |
| 2346 | return 0; |
| 2347 | } |
| 2348 | |
| 2349 | static int fill_device_from_item(struct extent_buffer *leaf, |
| 2350 | struct btrfs_dev_item *dev_item, |
| 2351 | struct btrfs_device *device) |
| 2352 | { |
| 2353 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2354 | |
| 2355 | device->devid = btrfs_device_id(leaf, dev_item); |
| 2356 | device->total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 2357 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
| 2358 | device->type = btrfs_device_type(leaf, dev_item); |
| 2359 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 2360 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 2361 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2362 | |
| 2363 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 2364 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2365 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2366 | return 0; |
| 2367 | } |
| 2368 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2369 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2370 | struct extent_buffer *leaf, |
| 2371 | struct btrfs_dev_item *dev_item) |
| 2372 | { |
| 2373 | struct btrfs_device *device; |
| 2374 | u64 devid; |
| 2375 | int ret; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 2376 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2377 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2378 | devid = btrfs_device_id(leaf, dev_item); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 2379 | read_extent_buffer(leaf, dev_uuid, |
| 2380 | (unsigned long)btrfs_device_uuid(dev_item), |
| 2381 | BTRFS_UUID_SIZE); |
| 2382 | device = btrfs_find_device(root, devid, dev_uuid); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2383 | if (!device) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2384 | printk("warning devid %Lu missing\n", devid); |
| 2385 | device = add_missing_dev(root, devid, dev_uuid); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2386 | if (!device) |
| 2387 | return -ENOMEM; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2388 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2389 | |
| 2390 | fill_device_from_item(leaf, dev_item, device); |
| 2391 | device->dev_root = root->fs_info->dev_root; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2392 | device->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2393 | ret = 0; |
| 2394 | #if 0 |
| 2395 | ret = btrfs_open_device(device); |
| 2396 | if (ret) { |
| 2397 | kfree(device); |
| 2398 | } |
| 2399 | #endif |
| 2400 | return ret; |
| 2401 | } |
| 2402 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2403 | int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf) |
| 2404 | { |
| 2405 | struct btrfs_dev_item *dev_item; |
| 2406 | |
| 2407 | dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block, |
| 2408 | dev_item); |
| 2409 | return read_one_dev(root, buf, dev_item); |
| 2410 | } |
| 2411 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2412 | int btrfs_read_sys_array(struct btrfs_root *root) |
| 2413 | { |
| 2414 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2415 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2416 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2417 | struct btrfs_chunk *chunk; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 2418 | u8 *ptr; |
| 2419 | unsigned long sb_ptr; |
| 2420 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2421 | u32 num_stripes; |
| 2422 | u32 array_size; |
| 2423 | u32 len = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2424 | u32 cur; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 2425 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2426 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2427 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET, |
| 2428 | BTRFS_SUPER_INFO_SIZE); |
| 2429 | if (!sb) |
| 2430 | return -ENOMEM; |
| 2431 | btrfs_set_buffer_uptodate(sb); |
| 2432 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2433 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2434 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2435 | ptr = super_copy->sys_chunk_array; |
| 2436 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 2437 | cur = 0; |
| 2438 | |
| 2439 | while (cur < array_size) { |
| 2440 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2441 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2442 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2443 | len = sizeof(*disk_key); ptr += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2444 | sb_ptr += len; |
| 2445 | cur += len; |
| 2446 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2447 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2448 | chunk = (struct btrfs_chunk *)sb_ptr; |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2449 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 2450 | if (ret) |
| 2451 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2452 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 2453 | len = btrfs_chunk_item_size(num_stripes); |
| 2454 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 2455 | ret = -EIO; |
| 2456 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2457 | } |
| 2458 | ptr += len; |
| 2459 | sb_ptr += len; |
| 2460 | cur += len; |
| 2461 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2462 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 2463 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 2467 | { |
| 2468 | struct btrfs_path *path; |
| 2469 | struct extent_buffer *leaf; |
| 2470 | struct btrfs_key key; |
| 2471 | struct btrfs_key found_key; |
| 2472 | int ret; |
| 2473 | int slot; |
| 2474 | |
| 2475 | root = root->fs_info->chunk_root; |
| 2476 | |
| 2477 | path = btrfs_alloc_path(); |
| 2478 | if (!path) |
| 2479 | return -ENOMEM; |
| 2480 | |
| 2481 | /* first we search for all of the device items, and then we |
| 2482 | * read in all of the chunk items. This way we can create chunk |
| 2483 | * mappings that reference all of the devices that are afound |
| 2484 | */ |
| 2485 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2486 | key.offset = 0; |
| 2487 | key.type = 0; |
| 2488 | again: |
| 2489 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2490 | while(1) { |
| 2491 | leaf = path->nodes[0]; |
| 2492 | slot = path->slots[0]; |
| 2493 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2494 | ret = btrfs_next_leaf(root, path); |
| 2495 | if (ret == 0) |
| 2496 | continue; |
| 2497 | if (ret < 0) |
| 2498 | goto error; |
| 2499 | break; |
| 2500 | } |
| 2501 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2502 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { |
| 2503 | if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID) |
| 2504 | break; |
| 2505 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 2506 | struct btrfs_dev_item *dev_item; |
| 2507 | dev_item = btrfs_item_ptr(leaf, slot, |
| 2508 | struct btrfs_dev_item); |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2509 | ret = read_one_dev(root, leaf, dev_item); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2510 | BUG_ON(ret); |
| 2511 | } |
| 2512 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2513 | struct btrfs_chunk *chunk; |
| 2514 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 2515 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
| 2516 | } |
| 2517 | path->slots[0]++; |
| 2518 | } |
| 2519 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { |
| 2520 | key.objectid = 0; |
| 2521 | btrfs_release_path(root, path); |
| 2522 | goto again; |
| 2523 | } |
| 2524 | |
| 2525 | btrfs_free_path(path); |
| 2526 | ret = 0; |
| 2527 | error: |
| 2528 | return ret; |
| 2529 | } |
| 2530 | |