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