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