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 | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 22 | #include <asm/div64.h> |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 23 | #include "ctree.h" |
| 24 | #include "extent_map.h" |
| 25 | #include "disk-io.h" |
| 26 | #include "transaction.h" |
| 27 | #include "print-tree.h" |
| 28 | #include "volumes.h" |
| 29 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 30 | struct map_lookup { |
| 31 | u64 type; |
| 32 | int io_align; |
| 33 | int io_width; |
| 34 | int stripe_len; |
| 35 | int sector_size; |
| 36 | int num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 37 | int sub_stripes; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 38 | struct btrfs_bio_stripe stripes[]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | #define map_lookup_size(n) (sizeof(struct map_lookup) + \ |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 42 | (sizeof(struct btrfs_bio_stripe) * (n))) |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 43 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 44 | static DEFINE_MUTEX(uuid_mutex); |
| 45 | static LIST_HEAD(fs_uuids); |
| 46 | |
| 47 | int btrfs_cleanup_fs_uuids(void) |
| 48 | { |
| 49 | struct btrfs_fs_devices *fs_devices; |
| 50 | struct list_head *uuid_cur; |
| 51 | struct list_head *devices_cur; |
| 52 | struct btrfs_device *dev; |
| 53 | |
| 54 | list_for_each(uuid_cur, &fs_uuids) { |
| 55 | fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices, |
| 56 | list); |
| 57 | while(!list_empty(&fs_devices->devices)) { |
| 58 | devices_cur = fs_devices->devices.next; |
| 59 | dev = list_entry(devices_cur, struct btrfs_device, |
| 60 | dev_list); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 61 | if (dev->bdev) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 62 | close_bdev_excl(dev->bdev); |
| 63 | } |
| 64 | list_del(&dev->dev_list); |
| 65 | kfree(dev); |
| 66 | } |
| 67 | } |
| 68 | return 0; |
| 69 | } |
| 70 | |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 71 | static struct btrfs_device *__find_device(struct list_head *head, u64 devid, |
| 72 | u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 73 | { |
| 74 | struct btrfs_device *dev; |
| 75 | struct list_head *cur; |
| 76 | |
| 77 | list_for_each(cur, head) { |
| 78 | dev = list_entry(cur, struct btrfs_device, dev_list); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 79 | if (dev->devid == devid && |
| 80 | !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE)) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 81 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 82 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 83 | } |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | static struct btrfs_fs_devices *find_fsid(u8 *fsid) |
| 88 | { |
| 89 | struct list_head *cur; |
| 90 | struct btrfs_fs_devices *fs_devices; |
| 91 | |
| 92 | list_for_each(cur, &fs_uuids) { |
| 93 | fs_devices = list_entry(cur, struct btrfs_fs_devices, list); |
| 94 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 95 | return fs_devices; |
| 96 | } |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | static int device_list_add(const char *path, |
| 101 | struct btrfs_super_block *disk_super, |
| 102 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) |
| 103 | { |
| 104 | struct btrfs_device *device; |
| 105 | struct btrfs_fs_devices *fs_devices; |
| 106 | u64 found_transid = btrfs_super_generation(disk_super); |
| 107 | |
| 108 | fs_devices = find_fsid(disk_super->fsid); |
| 109 | if (!fs_devices) { |
| 110 | fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS); |
| 111 | if (!fs_devices) |
| 112 | return -ENOMEM; |
| 113 | INIT_LIST_HEAD(&fs_devices->devices); |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 114 | INIT_LIST_HEAD(&fs_devices->alloc_list); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 115 | list_add(&fs_devices->list, &fs_uuids); |
| 116 | memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE); |
| 117 | fs_devices->latest_devid = devid; |
| 118 | fs_devices->latest_trans = found_transid; |
| 119 | fs_devices->lowest_devid = (u64)-1; |
| 120 | fs_devices->num_devices = 0; |
| 121 | device = NULL; |
| 122 | } else { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 123 | device = __find_device(&fs_devices->devices, devid, |
| 124 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 125 | } |
| 126 | if (!device) { |
| 127 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 128 | if (!device) { |
| 129 | /* we can safely leave the fs_devices entry around */ |
| 130 | return -ENOMEM; |
| 131 | } |
| 132 | device->devid = devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 133 | memcpy(device->uuid, disk_super->dev_item.uuid, |
| 134 | BTRFS_UUID_SIZE); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 135 | device->barriers = 1; |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 136 | spin_lock_init(&device->io_lock); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 137 | device->name = kstrdup(path, GFP_NOFS); |
| 138 | if (!device->name) { |
| 139 | kfree(device); |
| 140 | return -ENOMEM; |
| 141 | } |
| 142 | list_add(&device->dev_list, &fs_devices->devices); |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 143 | list_add(&device->dev_alloc_list, &fs_devices->alloc_list); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 144 | fs_devices->num_devices++; |
| 145 | } |
| 146 | |
| 147 | if (found_transid > fs_devices->latest_trans) { |
| 148 | fs_devices->latest_devid = devid; |
| 149 | fs_devices->latest_trans = found_transid; |
| 150 | } |
| 151 | if (fs_devices->lowest_devid > devid) { |
| 152 | fs_devices->lowest_devid = devid; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 153 | } |
| 154 | *fs_devices_ret = fs_devices; |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 159 | { |
| 160 | struct list_head *head = &fs_devices->devices; |
| 161 | struct list_head *cur; |
| 162 | struct btrfs_device *device; |
| 163 | |
| 164 | mutex_lock(&uuid_mutex); |
| 165 | list_for_each(cur, head) { |
| 166 | device = list_entry(cur, struct btrfs_device, dev_list); |
| 167 | if (device->bdev) { |
| 168 | close_bdev_excl(device->bdev); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 169 | } |
| 170 | device->bdev = NULL; |
| 171 | } |
| 172 | mutex_unlock(&uuid_mutex); |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 177 | int flags, void *holder) |
| 178 | { |
| 179 | struct block_device *bdev; |
| 180 | struct list_head *head = &fs_devices->devices; |
| 181 | struct list_head *cur; |
| 182 | struct btrfs_device *device; |
| 183 | int ret; |
| 184 | |
| 185 | mutex_lock(&uuid_mutex); |
| 186 | list_for_each(cur, head) { |
| 187 | device = list_entry(cur, struct btrfs_device, dev_list); |
| 188 | bdev = open_bdev_excl(device->name, flags, holder); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 189 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 190 | if (IS_ERR(bdev)) { |
| 191 | printk("open %s failed\n", device->name); |
| 192 | ret = PTR_ERR(bdev); |
| 193 | goto fail; |
| 194 | } |
| 195 | if (device->devid == fs_devices->latest_devid) |
| 196 | fs_devices->latest_bdev = bdev; |
| 197 | if (device->devid == fs_devices->lowest_devid) { |
| 198 | fs_devices->lowest_bdev = bdev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 199 | } |
| 200 | device->bdev = bdev; |
| 201 | } |
| 202 | mutex_unlock(&uuid_mutex); |
| 203 | return 0; |
| 204 | fail: |
| 205 | mutex_unlock(&uuid_mutex); |
| 206 | btrfs_close_devices(fs_devices); |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | int btrfs_scan_one_device(const char *path, int flags, void *holder, |
| 211 | struct btrfs_fs_devices **fs_devices_ret) |
| 212 | { |
| 213 | struct btrfs_super_block *disk_super; |
| 214 | struct block_device *bdev; |
| 215 | struct buffer_head *bh; |
| 216 | int ret; |
| 217 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 218 | u64 transid; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 219 | |
| 220 | mutex_lock(&uuid_mutex); |
| 221 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 222 | bdev = open_bdev_excl(path, flags, holder); |
| 223 | |
| 224 | if (IS_ERR(bdev)) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 225 | ret = PTR_ERR(bdev); |
| 226 | goto error; |
| 227 | } |
| 228 | |
| 229 | ret = set_blocksize(bdev, 4096); |
| 230 | if (ret) |
| 231 | goto error_close; |
| 232 | bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096); |
| 233 | if (!bh) { |
| 234 | ret = -EIO; |
| 235 | goto error_close; |
| 236 | } |
| 237 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 238 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, |
| 239 | sizeof(disk_super->magic))) { |
Yan | e58ca02 | 2008-04-01 11:21:34 -0400 | [diff] [blame] | 240 | ret = -EINVAL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 241 | goto error_brelse; |
| 242 | } |
| 243 | devid = le64_to_cpu(disk_super->dev_item.devid); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 244 | transid = btrfs_super_generation(disk_super); |
Chris Mason | 7ae9c09 | 2008-04-18 10:29:49 -0400 | [diff] [blame] | 245 | if (disk_super->label[0]) |
| 246 | printk("device label %s ", disk_super->label); |
| 247 | else { |
| 248 | /* FIXME, make a readl uuid parser */ |
| 249 | printk("device fsid %llx-%llx ", |
| 250 | *(unsigned long long *)disk_super->fsid, |
| 251 | *(unsigned long long *)(disk_super->fsid + 8)); |
| 252 | } |
| 253 | printk("devid %Lu transid %Lu %s\n", devid, transid, path); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 254 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
| 255 | |
| 256 | error_brelse: |
| 257 | brelse(bh); |
| 258 | error_close: |
| 259 | close_bdev_excl(bdev); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 260 | error: |
| 261 | mutex_unlock(&uuid_mutex); |
| 262 | return ret; |
| 263 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 264 | |
| 265 | /* |
| 266 | * this uses a pretty simple search, the expectation is that it is |
| 267 | * called very infrequently and that a given device has a small number |
| 268 | * of extents |
| 269 | */ |
| 270 | static int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 271 | struct btrfs_device *device, |
| 272 | struct btrfs_path *path, |
| 273 | u64 num_bytes, u64 *start) |
| 274 | { |
| 275 | struct btrfs_key key; |
| 276 | struct btrfs_root *root = device->dev_root; |
| 277 | struct btrfs_dev_extent *dev_extent = NULL; |
| 278 | u64 hole_size = 0; |
| 279 | u64 last_byte = 0; |
| 280 | u64 search_start = 0; |
| 281 | u64 search_end = device->total_bytes; |
| 282 | int ret; |
| 283 | int slot = 0; |
| 284 | int start_found; |
| 285 | struct extent_buffer *l; |
| 286 | |
| 287 | start_found = 0; |
| 288 | path->reada = 2; |
| 289 | |
| 290 | /* FIXME use last free of some kind */ |
| 291 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 292 | /* we don't want to overwrite the superblock on the drive, |
| 293 | * so we make sure to start at an offset of at least 1MB |
| 294 | */ |
| 295 | search_start = max((u64)1024 * 1024, search_start); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 296 | key.objectid = device->devid; |
| 297 | key.offset = search_start; |
| 298 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 299 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); |
| 300 | if (ret < 0) |
| 301 | goto error; |
| 302 | ret = btrfs_previous_item(root, path, 0, key.type); |
| 303 | if (ret < 0) |
| 304 | goto error; |
| 305 | l = path->nodes[0]; |
| 306 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 307 | while (1) { |
| 308 | l = path->nodes[0]; |
| 309 | slot = path->slots[0]; |
| 310 | if (slot >= btrfs_header_nritems(l)) { |
| 311 | ret = btrfs_next_leaf(root, path); |
| 312 | if (ret == 0) |
| 313 | continue; |
| 314 | if (ret < 0) |
| 315 | goto error; |
| 316 | no_more_items: |
| 317 | if (!start_found) { |
| 318 | if (search_start >= search_end) { |
| 319 | ret = -ENOSPC; |
| 320 | goto error; |
| 321 | } |
| 322 | *start = search_start; |
| 323 | start_found = 1; |
| 324 | goto check_pending; |
| 325 | } |
| 326 | *start = last_byte > search_start ? |
| 327 | last_byte : search_start; |
| 328 | if (search_end <= *start) { |
| 329 | ret = -ENOSPC; |
| 330 | goto error; |
| 331 | } |
| 332 | goto check_pending; |
| 333 | } |
| 334 | btrfs_item_key_to_cpu(l, &key, slot); |
| 335 | |
| 336 | if (key.objectid < device->devid) |
| 337 | goto next; |
| 338 | |
| 339 | if (key.objectid > device->devid) |
| 340 | goto no_more_items; |
| 341 | |
| 342 | if (key.offset >= search_start && key.offset > last_byte && |
| 343 | start_found) { |
| 344 | if (last_byte < search_start) |
| 345 | last_byte = search_start; |
| 346 | hole_size = key.offset - last_byte; |
| 347 | if (key.offset > last_byte && |
| 348 | hole_size >= num_bytes) { |
| 349 | *start = last_byte; |
| 350 | goto check_pending; |
| 351 | } |
| 352 | } |
| 353 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) { |
| 354 | goto next; |
| 355 | } |
| 356 | |
| 357 | start_found = 1; |
| 358 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 359 | last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent); |
| 360 | next: |
| 361 | path->slots[0]++; |
| 362 | cond_resched(); |
| 363 | } |
| 364 | check_pending: |
| 365 | /* we have to make sure we didn't find an extent that has already |
| 366 | * been allocated by the map tree or the original allocation |
| 367 | */ |
| 368 | btrfs_release_path(root, path); |
| 369 | BUG_ON(*start < search_start); |
| 370 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 371 | if (*start + num_bytes > search_end) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 372 | ret = -ENOSPC; |
| 373 | goto error; |
| 374 | } |
| 375 | /* check for pending inserts here */ |
| 376 | return 0; |
| 377 | |
| 378 | error: |
| 379 | btrfs_release_path(root, path); |
| 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 384 | struct btrfs_device *device, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 385 | u64 chunk_tree, u64 chunk_objectid, |
| 386 | u64 chunk_offset, |
| 387 | u64 num_bytes, u64 *start) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 388 | { |
| 389 | int ret; |
| 390 | struct btrfs_path *path; |
| 391 | struct btrfs_root *root = device->dev_root; |
| 392 | struct btrfs_dev_extent *extent; |
| 393 | struct extent_buffer *leaf; |
| 394 | struct btrfs_key key; |
| 395 | |
| 396 | path = btrfs_alloc_path(); |
| 397 | if (!path) |
| 398 | return -ENOMEM; |
| 399 | |
| 400 | ret = find_free_dev_extent(trans, device, path, num_bytes, start); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 401 | if (ret) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 402 | goto err; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 403 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 404 | |
| 405 | key.objectid = device->devid; |
| 406 | key.offset = *start; |
| 407 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 408 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 409 | sizeof(*extent)); |
| 410 | BUG_ON(ret); |
| 411 | |
| 412 | leaf = path->nodes[0]; |
| 413 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 414 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 415 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 416 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 417 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 418 | |
| 419 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
| 420 | (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent), |
| 421 | BTRFS_UUID_SIZE); |
| 422 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 423 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 424 | btrfs_mark_buffer_dirty(leaf); |
| 425 | err: |
| 426 | btrfs_free_path(path); |
| 427 | return ret; |
| 428 | } |
| 429 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 430 | 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] | 431 | { |
| 432 | struct btrfs_path *path; |
| 433 | int ret; |
| 434 | struct btrfs_key key; |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 435 | struct btrfs_chunk *chunk; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 436 | struct btrfs_key found_key; |
| 437 | |
| 438 | path = btrfs_alloc_path(); |
| 439 | BUG_ON(!path); |
| 440 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 441 | key.objectid = objectid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 442 | key.offset = (u64)-1; |
| 443 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 444 | |
| 445 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 446 | if (ret < 0) |
| 447 | goto error; |
| 448 | |
| 449 | BUG_ON(ret == 0); |
| 450 | |
| 451 | ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY); |
| 452 | if (ret) { |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 453 | *offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 454 | } else { |
| 455 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 456 | path->slots[0]); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 457 | if (found_key.objectid != objectid) |
| 458 | *offset = 0; |
| 459 | else { |
| 460 | chunk = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 461 | struct btrfs_chunk); |
| 462 | *offset = found_key.offset + |
| 463 | btrfs_chunk_length(path->nodes[0], chunk); |
| 464 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 465 | } |
| 466 | ret = 0; |
| 467 | error: |
| 468 | btrfs_free_path(path); |
| 469 | return ret; |
| 470 | } |
| 471 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 472 | static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path, |
| 473 | u64 *objectid) |
| 474 | { |
| 475 | int ret; |
| 476 | struct btrfs_key key; |
| 477 | struct btrfs_key found_key; |
| 478 | |
| 479 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 480 | key.type = BTRFS_DEV_ITEM_KEY; |
| 481 | key.offset = (u64)-1; |
| 482 | |
| 483 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 484 | if (ret < 0) |
| 485 | goto error; |
| 486 | |
| 487 | BUG_ON(ret == 0); |
| 488 | |
| 489 | ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID, |
| 490 | BTRFS_DEV_ITEM_KEY); |
| 491 | if (ret) { |
| 492 | *objectid = 1; |
| 493 | } else { |
| 494 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 495 | path->slots[0]); |
| 496 | *objectid = found_key.offset + 1; |
| 497 | } |
| 498 | ret = 0; |
| 499 | error: |
| 500 | btrfs_release_path(root, path); |
| 501 | return ret; |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * the device information is stored in the chunk root |
| 506 | * the btrfs_device struct should be fully filled in |
| 507 | */ |
| 508 | int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 509 | struct btrfs_root *root, |
| 510 | struct btrfs_device *device) |
| 511 | { |
| 512 | int ret; |
| 513 | struct btrfs_path *path; |
| 514 | struct btrfs_dev_item *dev_item; |
| 515 | struct extent_buffer *leaf; |
| 516 | struct btrfs_key key; |
| 517 | unsigned long ptr; |
| 518 | u64 free_devid; |
| 519 | |
| 520 | root = root->fs_info->chunk_root; |
| 521 | |
| 522 | path = btrfs_alloc_path(); |
| 523 | if (!path) |
| 524 | return -ENOMEM; |
| 525 | |
| 526 | ret = find_next_devid(root, path, &free_devid); |
| 527 | if (ret) |
| 528 | goto out; |
| 529 | |
| 530 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 531 | key.type = BTRFS_DEV_ITEM_KEY; |
| 532 | key.offset = free_devid; |
| 533 | |
| 534 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 535 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 536 | if (ret) |
| 537 | goto out; |
| 538 | |
| 539 | leaf = path->nodes[0]; |
| 540 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 541 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 542 | device->devid = free_devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 543 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 544 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 545 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 546 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 547 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 548 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
| 549 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 550 | btrfs_set_device_group(leaf, dev_item, 0); |
| 551 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 552 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 553 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 554 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 555 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 556 | btrfs_mark_buffer_dirty(leaf); |
| 557 | ret = 0; |
| 558 | |
| 559 | out: |
| 560 | btrfs_free_path(path); |
| 561 | return ret; |
| 562 | } |
| 563 | int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 564 | struct btrfs_device *device) |
| 565 | { |
| 566 | int ret; |
| 567 | struct btrfs_path *path; |
| 568 | struct btrfs_root *root; |
| 569 | struct btrfs_dev_item *dev_item; |
| 570 | struct extent_buffer *leaf; |
| 571 | struct btrfs_key key; |
| 572 | |
| 573 | root = device->dev_root->fs_info->chunk_root; |
| 574 | |
| 575 | path = btrfs_alloc_path(); |
| 576 | if (!path) |
| 577 | return -ENOMEM; |
| 578 | |
| 579 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 580 | key.type = BTRFS_DEV_ITEM_KEY; |
| 581 | key.offset = device->devid; |
| 582 | |
| 583 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 584 | if (ret < 0) |
| 585 | goto out; |
| 586 | |
| 587 | if (ret > 0) { |
| 588 | ret = -ENOENT; |
| 589 | goto out; |
| 590 | } |
| 591 | |
| 592 | leaf = path->nodes[0]; |
| 593 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 594 | |
| 595 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 596 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 597 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 598 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 599 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 600 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
| 601 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
| 602 | btrfs_mark_buffer_dirty(leaf); |
| 603 | |
| 604 | out: |
| 605 | btrfs_free_path(path); |
| 606 | return ret; |
| 607 | } |
| 608 | |
| 609 | int btrfs_add_system_chunk(struct btrfs_trans_handle *trans, |
| 610 | struct btrfs_root *root, |
| 611 | struct btrfs_key *key, |
| 612 | struct btrfs_chunk *chunk, int item_size) |
| 613 | { |
| 614 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; |
| 615 | struct btrfs_disk_key disk_key; |
| 616 | u32 array_size; |
| 617 | u8 *ptr; |
| 618 | |
| 619 | array_size = btrfs_super_sys_array_size(super_copy); |
| 620 | if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) |
| 621 | return -EFBIG; |
| 622 | |
| 623 | ptr = super_copy->sys_chunk_array + array_size; |
| 624 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 625 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 626 | ptr += sizeof(disk_key); |
| 627 | memcpy(ptr, chunk, item_size); |
| 628 | item_size += sizeof(disk_key); |
| 629 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
| 630 | return 0; |
| 631 | } |
| 632 | |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 633 | static u64 div_factor(u64 num, int factor) |
| 634 | { |
| 635 | if (factor == 10) |
| 636 | return num; |
| 637 | num *= factor; |
| 638 | do_div(num, 10); |
| 639 | return num; |
| 640 | } |
| 641 | |
| 642 | static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes, |
| 643 | int sub_stripes) |
| 644 | { |
| 645 | if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP)) |
| 646 | return calc_size; |
| 647 | else if (type & BTRFS_BLOCK_GROUP_RAID10) |
| 648 | return calc_size * (num_stripes / sub_stripes); |
| 649 | else |
| 650 | return calc_size * num_stripes; |
| 651 | } |
| 652 | |
| 653 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 654 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 655 | struct btrfs_root *extent_root, u64 *start, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 656 | u64 *num_bytes, u64 type) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 657 | { |
| 658 | u64 dev_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 659 | struct btrfs_fs_info *info = extent_root->fs_info; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 660 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 661 | struct btrfs_stripe *stripes; |
| 662 | struct btrfs_device *device = NULL; |
| 663 | struct btrfs_chunk *chunk; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 664 | struct list_head private_devs; |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 665 | struct list_head *dev_list; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 666 | struct list_head *cur; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 667 | struct extent_map_tree *em_tree; |
| 668 | struct map_lookup *map; |
| 669 | struct extent_map *em; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 670 | int min_stripe_size = 1 * 1024 * 1024; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 671 | u64 physical; |
| 672 | u64 calc_size = 1024 * 1024 * 1024; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 673 | u64 max_chunk_size = calc_size; |
| 674 | u64 min_free; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 675 | u64 avail; |
| 676 | u64 max_avail = 0; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 677 | u64 percent_max; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 678 | int num_stripes = 1; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 679 | int min_stripes = 1; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 680 | int sub_stripes = 0; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 681 | int looped = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 682 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 683 | int index; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 684 | int stripe_len = 64 * 1024; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 685 | struct btrfs_key key; |
| 686 | |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 687 | dev_list = &extent_root->fs_info->fs_devices->alloc_list; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 688 | if (list_empty(dev_list)) |
| 689 | return -ENOSPC; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 690 | |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 691 | if (type & (BTRFS_BLOCK_GROUP_RAID0)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 692 | num_stripes = btrfs_super_num_devices(&info->super_copy); |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 693 | min_stripes = 2; |
| 694 | } |
| 695 | if (type & (BTRFS_BLOCK_GROUP_DUP)) { |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 696 | num_stripes = 2; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 697 | min_stripes = 2; |
| 698 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 699 | if (type & (BTRFS_BLOCK_GROUP_RAID1)) { |
| 700 | num_stripes = min_t(u64, 2, |
| 701 | btrfs_super_num_devices(&info->super_copy)); |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 702 | if (num_stripes < 2) |
| 703 | return -ENOSPC; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 704 | min_stripes = 2; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 705 | } |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 706 | if (type & (BTRFS_BLOCK_GROUP_RAID10)) { |
| 707 | num_stripes = btrfs_super_num_devices(&info->super_copy); |
| 708 | if (num_stripes < 4) |
| 709 | return -ENOSPC; |
| 710 | num_stripes &= ~(u32)1; |
| 711 | sub_stripes = 2; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 712 | min_stripes = 4; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 713 | } |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 714 | |
| 715 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 716 | max_chunk_size = 10 * calc_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 717 | min_stripe_size = 64 * 1024 * 1024; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 718 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
| 719 | max_chunk_size = 4 * calc_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 720 | min_stripe_size = 32 * 1024 * 1024; |
| 721 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 722 | calc_size = 8 * 1024 * 1024; |
| 723 | max_chunk_size = calc_size * 2; |
| 724 | min_stripe_size = 1 * 1024 * 1024; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /* we don't want a chunk larger than 10% of the FS */ |
| 728 | percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1); |
| 729 | max_chunk_size = min(percent_max, max_chunk_size); |
| 730 | |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 731 | again: |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 732 | if (calc_size * num_stripes > max_chunk_size) { |
| 733 | calc_size = max_chunk_size; |
| 734 | do_div(calc_size, num_stripes); |
| 735 | do_div(calc_size, stripe_len); |
| 736 | calc_size *= stripe_len; |
| 737 | } |
| 738 | /* we don't want tiny stripes */ |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 739 | calc_size = max_t(u64, min_stripe_size, calc_size); |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 740 | |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 741 | do_div(calc_size, stripe_len); |
| 742 | calc_size *= stripe_len; |
| 743 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 744 | INIT_LIST_HEAD(&private_devs); |
| 745 | cur = dev_list->next; |
| 746 | index = 0; |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 747 | |
| 748 | if (type & BTRFS_BLOCK_GROUP_DUP) |
| 749 | min_free = calc_size * 2; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 750 | else |
| 751 | min_free = calc_size; |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 752 | |
Chris Mason | ad5bd91 | 2008-04-21 08:28:10 -0400 | [diff] [blame] | 753 | /* we add 1MB because we never use the first 1MB of the device */ |
| 754 | min_free += 1024 * 1024; |
| 755 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 756 | /* build a private list of devices we will allocate from */ |
| 757 | while(index < num_stripes) { |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 758 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 759 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 760 | avail = device->total_bytes - device->bytes_used; |
| 761 | cur = cur->next; |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 762 | if (avail >= min_free) { |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 763 | list_move_tail(&device->dev_alloc_list, &private_devs); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 764 | index++; |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 765 | if (type & BTRFS_BLOCK_GROUP_DUP) |
| 766 | index++; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 767 | } else if (avail > max_avail) |
| 768 | max_avail = avail; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 769 | if (cur == dev_list) |
| 770 | break; |
| 771 | } |
| 772 | if (index < num_stripes) { |
| 773 | list_splice(&private_devs, dev_list); |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 774 | if (index >= min_stripes) { |
| 775 | num_stripes = index; |
| 776 | if (type & (BTRFS_BLOCK_GROUP_RAID10)) { |
| 777 | num_stripes /= sub_stripes; |
| 778 | num_stripes *= sub_stripes; |
| 779 | } |
| 780 | looped = 1; |
| 781 | goto again; |
| 782 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 783 | if (!looped && max_avail > 0) { |
| 784 | looped = 1; |
| 785 | calc_size = max_avail; |
| 786 | goto again; |
| 787 | } |
| 788 | return -ENOSPC; |
| 789 | } |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 790 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 791 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 792 | ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 793 | &key.offset); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 794 | if (ret) |
| 795 | return ret; |
| 796 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 797 | chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS); |
| 798 | if (!chunk) |
| 799 | return -ENOMEM; |
| 800 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 801 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 802 | if (!map) { |
| 803 | kfree(chunk); |
| 804 | return -ENOMEM; |
| 805 | } |
| 806 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 807 | stripes = &chunk->stripe; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 808 | *num_bytes = chunk_bytes_by_type(type, calc_size, |
| 809 | num_stripes, sub_stripes); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 810 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 811 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 812 | index = 0; |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 813 | printk("new chunk type %Lu start %Lu size %Lu\n", type, key.offset, *num_bytes); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 814 | while(index < num_stripes) { |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 815 | struct btrfs_stripe *stripe; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 816 | BUG_ON(list_empty(&private_devs)); |
| 817 | cur = private_devs.next; |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 818 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 819 | |
| 820 | /* loop over this device again if we're doing a dup group */ |
| 821 | if (!(type & BTRFS_BLOCK_GROUP_DUP) || |
| 822 | (index == num_stripes - 1)) |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 823 | list_move_tail(&device->dev_alloc_list, dev_list); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 824 | |
| 825 | ret = btrfs_alloc_dev_extent(trans, device, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 826 | info->chunk_root->root_key.objectid, |
| 827 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset, |
| 828 | calc_size, &dev_offset); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 829 | BUG_ON(ret); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 830 | printk("alloc chunk start %Lu size %Lu from dev %Lu type %Lu\n", key.offset, calc_size, device->devid, type); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 831 | device->bytes_used += calc_size; |
| 832 | ret = btrfs_update_device(trans, device); |
| 833 | BUG_ON(ret); |
| 834 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 835 | map->stripes[index].dev = device; |
| 836 | map->stripes[index].physical = dev_offset; |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 837 | stripe = stripes + index; |
| 838 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 839 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 840 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 841 | physical = dev_offset; |
| 842 | index++; |
| 843 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 844 | BUG_ON(!list_empty(&private_devs)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 845 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 846 | /* key was set above */ |
| 847 | btrfs_set_stack_chunk_length(chunk, *num_bytes); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 848 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 849 | btrfs_set_stack_chunk_stripe_len(chunk, stripe_len); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 850 | btrfs_set_stack_chunk_type(chunk, type); |
| 851 | btrfs_set_stack_chunk_num_stripes(chunk, num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 852 | btrfs_set_stack_chunk_io_align(chunk, stripe_len); |
| 853 | btrfs_set_stack_chunk_io_width(chunk, stripe_len); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 854 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 855 | btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 856 | map->sector_size = extent_root->sectorsize; |
| 857 | map->stripe_len = stripe_len; |
| 858 | map->io_align = stripe_len; |
| 859 | map->io_width = stripe_len; |
| 860 | map->type = type; |
| 861 | map->num_stripes = num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 862 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 863 | |
| 864 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, |
| 865 | btrfs_chunk_item_size(num_stripes)); |
| 866 | BUG_ON(ret); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 867 | *start = key.offset;; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 868 | |
| 869 | em = alloc_extent_map(GFP_NOFS); |
| 870 | if (!em) |
| 871 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 872 | em->bdev = (struct block_device *)map; |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 873 | em->start = key.offset; |
| 874 | em->len = *num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 875 | em->block_start = 0; |
| 876 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 877 | kfree(chunk); |
| 878 | |
| 879 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
| 880 | spin_lock(&em_tree->lock); |
| 881 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 882 | spin_unlock(&em_tree->lock); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 883 | BUG_ON(ret); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 884 | free_extent_map(em); |
| 885 | return ret; |
| 886 | } |
| 887 | |
| 888 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 889 | { |
| 890 | extent_map_tree_init(&tree->map_tree, GFP_NOFS); |
| 891 | } |
| 892 | |
| 893 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 894 | { |
| 895 | struct extent_map *em; |
| 896 | |
| 897 | while(1) { |
| 898 | spin_lock(&tree->map_tree.lock); |
| 899 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 900 | if (em) |
| 901 | remove_extent_mapping(&tree->map_tree, em); |
| 902 | spin_unlock(&tree->map_tree.lock); |
| 903 | if (!em) |
| 904 | break; |
| 905 | kfree(em->bdev); |
| 906 | /* once for us */ |
| 907 | free_extent_map(em); |
| 908 | /* once for the tree */ |
| 909 | free_extent_map(em); |
| 910 | } |
| 911 | } |
| 912 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 913 | int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len) |
| 914 | { |
| 915 | struct extent_map *em; |
| 916 | struct map_lookup *map; |
| 917 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 918 | int ret; |
| 919 | |
| 920 | spin_lock(&em_tree->lock); |
| 921 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 922 | spin_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 923 | BUG_ON(!em); |
| 924 | |
| 925 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 926 | map = (struct map_lookup *)em->bdev; |
| 927 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 928 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 929 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 930 | ret = map->sub_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 931 | else |
| 932 | ret = 1; |
| 933 | free_extent_map(em); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 934 | return ret; |
| 935 | } |
| 936 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 937 | static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, |
| 938 | u64 logical, u64 *length, |
| 939 | struct btrfs_multi_bio **multi_ret, |
| 940 | int mirror_num, struct page *unplug_page) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 941 | { |
| 942 | struct extent_map *em; |
| 943 | struct map_lookup *map; |
| 944 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 945 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 946 | u64 stripe_offset; |
| 947 | u64 stripe_nr; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 948 | int stripes_allocated = 8; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 949 | int stripes_required = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 950 | int stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 951 | int i; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 952 | int num_stripes; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 953 | struct btrfs_multi_bio *multi = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 954 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 955 | if (multi_ret && !(rw & (1 << BIO_RW))) { |
| 956 | stripes_allocated = 1; |
| 957 | } |
| 958 | again: |
| 959 | if (multi_ret) { |
| 960 | multi = kzalloc(btrfs_multi_bio_size(stripes_allocated), |
| 961 | GFP_NOFS); |
| 962 | if (!multi) |
| 963 | return -ENOMEM; |
| 964 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 965 | |
| 966 | spin_lock(&em_tree->lock); |
| 967 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 968 | spin_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 969 | |
| 970 | if (!em && unplug_page) |
| 971 | return 0; |
| 972 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 973 | if (!em) { |
| 974 | printk("unable to find logical %Lu\n", logical); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 975 | BUG(); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 976 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 977 | |
| 978 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 979 | map = (struct map_lookup *)em->bdev; |
| 980 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 981 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 982 | if (mirror_num > map->num_stripes) |
| 983 | mirror_num = 0; |
| 984 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 985 | /* 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] | 986 | if (rw & (1 << BIO_RW)) { |
| 987 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 988 | BTRFS_BLOCK_GROUP_DUP)) { |
| 989 | stripes_required = map->num_stripes; |
| 990 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 991 | stripes_required = map->sub_stripes; |
| 992 | } |
| 993 | } |
| 994 | if (multi_ret && rw == WRITE && |
| 995 | stripes_allocated < stripes_required) { |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 996 | stripes_allocated = map->num_stripes; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 997 | free_extent_map(em); |
| 998 | kfree(multi); |
| 999 | goto again; |
| 1000 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1001 | stripe_nr = offset; |
| 1002 | /* |
| 1003 | * stripe_nr counts the total number of stripes we have to stride |
| 1004 | * to get to this block |
| 1005 | */ |
| 1006 | do_div(stripe_nr, map->stripe_len); |
| 1007 | |
| 1008 | stripe_offset = stripe_nr * map->stripe_len; |
| 1009 | BUG_ON(offset < stripe_offset); |
| 1010 | |
| 1011 | /* stripe_offset is the offset of this block in its stripe*/ |
| 1012 | stripe_offset = offset - stripe_offset; |
| 1013 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1014 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1015 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1016 | BTRFS_BLOCK_GROUP_DUP)) { |
| 1017 | /* we limit the length of each bio to what fits in a stripe */ |
| 1018 | *length = min_t(u64, em->len - offset, |
| 1019 | map->stripe_len - stripe_offset); |
| 1020 | } else { |
| 1021 | *length = em->len - offset; |
| 1022 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1023 | |
| 1024 | if (!multi_ret && !unplug_page) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1025 | goto out; |
| 1026 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1027 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1028 | stripe_index = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1029 | if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1030 | if (unplug_page || (rw & (1 << BIO_RW))) |
| 1031 | num_stripes = map->num_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1032 | else if (mirror_num) { |
| 1033 | stripe_index = mirror_num - 1; |
| 1034 | } else { |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1035 | u64 orig_stripe_nr = stripe_nr; |
| 1036 | stripe_index = do_div(orig_stripe_nr, num_stripes); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1037 | } |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1038 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1039 | if (rw & (1 << BIO_RW)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1040 | num_stripes = map->num_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1041 | else if (mirror_num) |
| 1042 | stripe_index = mirror_num - 1; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1043 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 1044 | int factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1045 | |
| 1046 | stripe_index = do_div(stripe_nr, factor); |
| 1047 | stripe_index *= map->sub_stripes; |
| 1048 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1049 | if (unplug_page || (rw & (1 << BIO_RW))) |
| 1050 | num_stripes = map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1051 | else if (mirror_num) |
| 1052 | stripe_index += mirror_num - 1; |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1053 | else { |
| 1054 | u64 orig_stripe_nr = stripe_nr; |
| 1055 | stripe_index += do_div(orig_stripe_nr, |
| 1056 | map->sub_stripes); |
| 1057 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1058 | } else { |
| 1059 | /* |
| 1060 | * after this do_div call, stripe_nr is the number of stripes |
| 1061 | * on this device we have to walk to find the data, and |
| 1062 | * stripe_index is the number of our device in the stripe array |
| 1063 | */ |
| 1064 | stripe_index = do_div(stripe_nr, map->num_stripes); |
| 1065 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1066 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1067 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1068 | for (i = 0; i < num_stripes; i++) { |
| 1069 | if (unplug_page) { |
| 1070 | struct btrfs_device *device; |
| 1071 | struct backing_dev_info *bdi; |
| 1072 | |
| 1073 | device = map->stripes[stripe_index].dev; |
| 1074 | bdi = blk_get_backing_dev_info(device->bdev); |
| 1075 | if (bdi->unplug_io_fn) { |
| 1076 | bdi->unplug_io_fn(bdi, unplug_page); |
| 1077 | } |
| 1078 | } else { |
| 1079 | multi->stripes[i].physical = |
| 1080 | map->stripes[stripe_index].physical + |
| 1081 | stripe_offset + stripe_nr * map->stripe_len; |
| 1082 | multi->stripes[i].dev = map->stripes[stripe_index].dev; |
| 1083 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1084 | stripe_index++; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1085 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1086 | if (multi_ret) { |
| 1087 | *multi_ret = multi; |
| 1088 | multi->num_stripes = num_stripes; |
| 1089 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1090 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1091 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1092 | return 0; |
| 1093 | } |
| 1094 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1095 | int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, |
| 1096 | u64 logical, u64 *length, |
| 1097 | struct btrfs_multi_bio **multi_ret, int mirror_num) |
| 1098 | { |
| 1099 | return __btrfs_map_block(map_tree, rw, logical, length, multi_ret, |
| 1100 | mirror_num, NULL); |
| 1101 | } |
| 1102 | |
| 1103 | int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree, |
| 1104 | u64 logical, struct page *page) |
| 1105 | { |
| 1106 | u64 length = PAGE_CACHE_SIZE; |
| 1107 | return __btrfs_map_block(map_tree, READ, logical, &length, |
| 1108 | NULL, 0, page); |
| 1109 | } |
| 1110 | |
| 1111 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1112 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
| 1113 | static void end_bio_multi_stripe(struct bio *bio, int err) |
| 1114 | #else |
| 1115 | static int end_bio_multi_stripe(struct bio *bio, |
| 1116 | unsigned int bytes_done, int err) |
| 1117 | #endif |
| 1118 | { |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1119 | struct btrfs_multi_bio *multi = bio->bi_private; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1120 | |
| 1121 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 1122 | if (bio->bi_size) |
| 1123 | return 1; |
| 1124 | #endif |
| 1125 | if (err) |
| 1126 | multi->error = err; |
| 1127 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1128 | if (atomic_dec_and_test(&multi->stripes_pending)) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1129 | bio->bi_private = multi->private; |
| 1130 | bio->bi_end_io = multi->end_io; |
| 1131 | |
| 1132 | if (!err && multi->error) |
| 1133 | err = multi->error; |
| 1134 | kfree(multi); |
| 1135 | |
Miguel | 73f61b2 | 2008-04-11 15:50:59 -0400 | [diff] [blame] | 1136 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 1137 | bio_endio(bio, bio->bi_size, err); |
| 1138 | #else |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1139 | bio_endio(bio, err); |
Miguel | 73f61b2 | 2008-04-11 15:50:59 -0400 | [diff] [blame] | 1140 | #endif |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1141 | } else { |
| 1142 | bio_put(bio); |
| 1143 | } |
| 1144 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23) |
| 1145 | return 0; |
| 1146 | #endif |
| 1147 | } |
| 1148 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1149 | int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio, |
| 1150 | int mirror_num) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1151 | { |
| 1152 | struct btrfs_mapping_tree *map_tree; |
| 1153 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1154 | struct bio *first_bio = bio; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1155 | u64 logical = bio->bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1156 | u64 length = 0; |
| 1157 | u64 map_length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1158 | struct btrfs_multi_bio *multi = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1159 | int ret; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1160 | int dev_nr = 0; |
| 1161 | int total_devs = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1162 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1163 | length = bio->bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1164 | map_tree = &root->fs_info->mapping_tree; |
| 1165 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1166 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1167 | ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi, |
| 1168 | mirror_num); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1169 | BUG_ON(ret); |
| 1170 | |
| 1171 | total_devs = multi->num_stripes; |
| 1172 | if (map_length < length) { |
| 1173 | printk("mapping failed logical %Lu bio len %Lu " |
| 1174 | "len %Lu\n", logical, length, map_length); |
| 1175 | BUG(); |
| 1176 | } |
| 1177 | multi->end_io = first_bio->bi_end_io; |
| 1178 | multi->private = first_bio->bi_private; |
| 1179 | atomic_set(&multi->stripes_pending, multi->num_stripes); |
| 1180 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1181 | while(dev_nr < total_devs) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1182 | if (total_devs > 1) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1183 | if (dev_nr < total_devs - 1) { |
| 1184 | bio = bio_clone(first_bio, GFP_NOFS); |
| 1185 | BUG_ON(!bio); |
| 1186 | } else { |
| 1187 | bio = first_bio; |
| 1188 | } |
| 1189 | bio->bi_private = multi; |
| 1190 | bio->bi_end_io = end_bio_multi_stripe; |
| 1191 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1192 | bio->bi_sector = multi->stripes[dev_nr].physical >> 9; |
| 1193 | dev = multi->stripes[dev_nr].dev; |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 1194 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1195 | bio->bi_bdev = dev->bdev; |
| 1196 | spin_lock(&dev->io_lock); |
| 1197 | dev->total_ios++; |
| 1198 | spin_unlock(&dev->io_lock); |
| 1199 | submit_bio(rw, bio); |
| 1200 | dev_nr++; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1201 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1202 | if (total_devs == 1) |
| 1203 | kfree(multi); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1204 | return 0; |
| 1205 | } |
| 1206 | |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 1207 | struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid, |
| 1208 | u8 *uuid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1209 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1210 | struct list_head *head = &root->fs_info->fs_devices->devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1211 | |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 1212 | return __find_device(head, devid, uuid); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 1216 | struct extent_buffer *leaf, |
| 1217 | struct btrfs_chunk *chunk) |
| 1218 | { |
| 1219 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 1220 | struct map_lookup *map; |
| 1221 | struct extent_map *em; |
| 1222 | u64 logical; |
| 1223 | u64 length; |
| 1224 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 1225 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1226 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1227 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1228 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1229 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1230 | logical = key->offset; |
| 1231 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1232 | spin_lock(&map_tree->map_tree.lock); |
| 1233 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 1234 | spin_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1235 | |
| 1236 | /* already mapped? */ |
| 1237 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 1238 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1239 | return 0; |
| 1240 | } else if (em) { |
| 1241 | free_extent_map(em); |
| 1242 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1243 | |
| 1244 | map = kzalloc(sizeof(*map), GFP_NOFS); |
| 1245 | if (!map) |
| 1246 | return -ENOMEM; |
| 1247 | |
| 1248 | em = alloc_extent_map(GFP_NOFS); |
| 1249 | if (!em) |
| 1250 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1251 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 1252 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1253 | if (!map) { |
| 1254 | free_extent_map(em); |
| 1255 | return -ENOMEM; |
| 1256 | } |
| 1257 | |
| 1258 | em->bdev = (struct block_device *)map; |
| 1259 | em->start = logical; |
| 1260 | em->len = length; |
| 1261 | em->block_start = 0; |
| 1262 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1263 | map->num_stripes = num_stripes; |
| 1264 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 1265 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 1266 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 1267 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 1268 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1269 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1270 | for (i = 0; i < num_stripes; i++) { |
| 1271 | map->stripes[i].physical = |
| 1272 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 1273 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 1274 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 1275 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 1276 | BTRFS_UUID_SIZE); |
| 1277 | map->stripes[i].dev = btrfs_find_device(root, devid, uuid); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1278 | if (!map->stripes[i].dev) { |
| 1279 | kfree(map); |
| 1280 | free_extent_map(em); |
| 1281 | return -EIO; |
| 1282 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | spin_lock(&map_tree->map_tree.lock); |
| 1286 | ret = add_extent_mapping(&map_tree->map_tree, em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1287 | spin_unlock(&map_tree->map_tree.lock); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 1288 | BUG_ON(ret); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1289 | free_extent_map(em); |
| 1290 | |
| 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | static int fill_device_from_item(struct extent_buffer *leaf, |
| 1295 | struct btrfs_dev_item *dev_item, |
| 1296 | struct btrfs_device *device) |
| 1297 | { |
| 1298 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1299 | |
| 1300 | device->devid = btrfs_device_id(leaf, dev_item); |
| 1301 | device->total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 1302 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
| 1303 | device->type = btrfs_device_type(leaf, dev_item); |
| 1304 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 1305 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 1306 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1307 | |
| 1308 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1309 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1310 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1311 | return 0; |
| 1312 | } |
| 1313 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1314 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1315 | struct extent_buffer *leaf, |
| 1316 | struct btrfs_dev_item *dev_item) |
| 1317 | { |
| 1318 | struct btrfs_device *device; |
| 1319 | u64 devid; |
| 1320 | int ret; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 1321 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 1322 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1323 | devid = btrfs_device_id(leaf, dev_item); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 1324 | read_extent_buffer(leaf, dev_uuid, |
| 1325 | (unsigned long)btrfs_device_uuid(dev_item), |
| 1326 | BTRFS_UUID_SIZE); |
| 1327 | device = btrfs_find_device(root, devid, dev_uuid); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1328 | if (!device) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1329 | printk("warning devid %Lu not found already\n", devid); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 1330 | device = kzalloc(sizeof(*device), GFP_NOFS); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1331 | if (!device) |
| 1332 | return -ENOMEM; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1333 | list_add(&device->dev_list, |
| 1334 | &root->fs_info->fs_devices->devices); |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 1335 | list_add(&device->dev_alloc_list, |
| 1336 | &root->fs_info->fs_devices->alloc_list); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 1337 | device->barriers = 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1338 | spin_lock_init(&device->io_lock); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1339 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1340 | |
| 1341 | fill_device_from_item(leaf, dev_item, device); |
| 1342 | device->dev_root = root->fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1343 | ret = 0; |
| 1344 | #if 0 |
| 1345 | ret = btrfs_open_device(device); |
| 1346 | if (ret) { |
| 1347 | kfree(device); |
| 1348 | } |
| 1349 | #endif |
| 1350 | return ret; |
| 1351 | } |
| 1352 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1353 | int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf) |
| 1354 | { |
| 1355 | struct btrfs_dev_item *dev_item; |
| 1356 | |
| 1357 | dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block, |
| 1358 | dev_item); |
| 1359 | return read_one_dev(root, buf, dev_item); |
| 1360 | } |
| 1361 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1362 | int btrfs_read_sys_array(struct btrfs_root *root) |
| 1363 | { |
| 1364 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; |
| 1365 | struct extent_buffer *sb = root->fs_info->sb_buffer; |
| 1366 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1367 | struct btrfs_chunk *chunk; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame^] | 1368 | u8 *ptr; |
| 1369 | unsigned long sb_ptr; |
| 1370 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1371 | u32 num_stripes; |
| 1372 | u32 array_size; |
| 1373 | u32 len = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1374 | u32 cur; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame^] | 1375 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1376 | |
| 1377 | array_size = btrfs_super_sys_array_size(super_copy); |
| 1378 | |
| 1379 | /* |
| 1380 | * we do this loop twice, once for the device items and |
| 1381 | * once for all of the chunks. This way there are device |
| 1382 | * structs filled in for every chunk |
| 1383 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1384 | ptr = super_copy->sys_chunk_array; |
| 1385 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 1386 | cur = 0; |
| 1387 | |
| 1388 | while (cur < array_size) { |
| 1389 | disk_key = (struct btrfs_disk_key *)ptr; |
| 1390 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 1391 | |
| 1392 | len = sizeof(*disk_key); |
| 1393 | ptr += len; |
| 1394 | sb_ptr += len; |
| 1395 | cur += len; |
| 1396 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1397 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1398 | chunk = (struct btrfs_chunk *)sb_ptr; |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1399 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame^] | 1400 | if (ret) |
| 1401 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1402 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 1403 | len = btrfs_chunk_item_size(num_stripes); |
| 1404 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame^] | 1405 | ret = -EIO; |
| 1406 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1407 | } |
| 1408 | ptr += len; |
| 1409 | sb_ptr += len; |
| 1410 | cur += len; |
| 1411 | } |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame^] | 1412 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 1416 | { |
| 1417 | struct btrfs_path *path; |
| 1418 | struct extent_buffer *leaf; |
| 1419 | struct btrfs_key key; |
| 1420 | struct btrfs_key found_key; |
| 1421 | int ret; |
| 1422 | int slot; |
| 1423 | |
| 1424 | root = root->fs_info->chunk_root; |
| 1425 | |
| 1426 | path = btrfs_alloc_path(); |
| 1427 | if (!path) |
| 1428 | return -ENOMEM; |
| 1429 | |
| 1430 | /* first we search for all of the device items, and then we |
| 1431 | * read in all of the chunk items. This way we can create chunk |
| 1432 | * mappings that reference all of the devices that are afound |
| 1433 | */ |
| 1434 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1435 | key.offset = 0; |
| 1436 | key.type = 0; |
| 1437 | again: |
| 1438 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1439 | while(1) { |
| 1440 | leaf = path->nodes[0]; |
| 1441 | slot = path->slots[0]; |
| 1442 | if (slot >= btrfs_header_nritems(leaf)) { |
| 1443 | ret = btrfs_next_leaf(root, path); |
| 1444 | if (ret == 0) |
| 1445 | continue; |
| 1446 | if (ret < 0) |
| 1447 | goto error; |
| 1448 | break; |
| 1449 | } |
| 1450 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 1451 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { |
| 1452 | if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID) |
| 1453 | break; |
| 1454 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 1455 | struct btrfs_dev_item *dev_item; |
| 1456 | dev_item = btrfs_item_ptr(leaf, slot, |
| 1457 | struct btrfs_dev_item); |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1458 | ret = read_one_dev(root, leaf, dev_item); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1459 | BUG_ON(ret); |
| 1460 | } |
| 1461 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 1462 | struct btrfs_chunk *chunk; |
| 1463 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 1464 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
| 1465 | } |
| 1466 | path->slots[0]++; |
| 1467 | } |
| 1468 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { |
| 1469 | key.objectid = 0; |
| 1470 | btrfs_release_path(root, path); |
| 1471 | goto again; |
| 1472 | } |
| 1473 | |
| 1474 | btrfs_free_path(path); |
| 1475 | ret = 0; |
| 1476 | error: |
| 1477 | return ret; |
| 1478 | } |
| 1479 | |