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