Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/bio.h> |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 20 | #include <linux/buffer_head.h> |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 21 | #include <linux/blkdev.h> |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 22 | #include <linux/random.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 23 | #include <linux/version.h> |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 24 | #include <asm/div64.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 25 | #include "compat.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 26 | #include "ctree.h" |
| 27 | #include "extent_map.h" |
| 28 | #include "disk-io.h" |
| 29 | #include "transaction.h" |
| 30 | #include "print-tree.h" |
| 31 | #include "volumes.h" |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 32 | #include "async-thread.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 33 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 34 | struct map_lookup { |
| 35 | u64 type; |
| 36 | int io_align; |
| 37 | int io_width; |
| 38 | int stripe_len; |
| 39 | int sector_size; |
| 40 | int num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 41 | int sub_stripes; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 42 | struct btrfs_bio_stripe stripes[]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 43 | }; |
| 44 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 45 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
| 46 | struct btrfs_root *root, |
| 47 | struct btrfs_device *device); |
| 48 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root); |
| 49 | |
| 50 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 51 | #define map_lookup_size(n) (sizeof(struct map_lookup) + \ |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 52 | (sizeof(struct btrfs_bio_stripe) * (n))) |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 53 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 54 | static DEFINE_MUTEX(uuid_mutex); |
| 55 | static LIST_HEAD(fs_uuids); |
| 56 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 57 | void btrfs_lock_volumes(void) |
| 58 | { |
| 59 | mutex_lock(&uuid_mutex); |
| 60 | } |
| 61 | |
| 62 | void btrfs_unlock_volumes(void) |
| 63 | { |
| 64 | mutex_unlock(&uuid_mutex); |
| 65 | } |
| 66 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 67 | static void lock_chunks(struct btrfs_root *root) |
| 68 | { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 69 | mutex_lock(&root->fs_info->chunk_mutex); |
| 70 | } |
| 71 | |
| 72 | static void unlock_chunks(struct btrfs_root *root) |
| 73 | { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 74 | mutex_unlock(&root->fs_info->chunk_mutex); |
| 75 | } |
| 76 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 77 | int btrfs_cleanup_fs_uuids(void) |
| 78 | { |
| 79 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 80 | struct btrfs_device *dev; |
| 81 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 82 | while (!list_empty(&fs_uuids)) { |
| 83 | fs_devices = list_entry(fs_uuids.next, |
| 84 | struct btrfs_fs_devices, list); |
| 85 | list_del(&fs_devices->list); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 86 | while(!list_empty(&fs_devices->devices)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 87 | dev = list_entry(fs_devices->devices.next, |
| 88 | struct btrfs_device, dev_list); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 89 | if (dev->bdev) { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 90 | close_bdev_exclusive(dev->bdev, dev->mode); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 91 | fs_devices->open_devices--; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 92 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 93 | fs_devices->num_devices--; |
| 94 | if (dev->writeable) |
| 95 | fs_devices->rw_devices--; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 96 | list_del(&dev->dev_list); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 97 | list_del(&dev->dev_alloc_list); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 98 | kfree(dev->name); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 99 | kfree(dev); |
| 100 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 101 | WARN_ON(fs_devices->num_devices); |
| 102 | WARN_ON(fs_devices->open_devices); |
| 103 | WARN_ON(fs_devices->rw_devices); |
| 104 | kfree(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 109 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
| 110 | u64 devid, u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 111 | { |
| 112 | struct btrfs_device *dev; |
| 113 | struct list_head *cur; |
| 114 | |
| 115 | list_for_each(cur, head) { |
| 116 | dev = list_entry(cur, struct btrfs_device, dev_list); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 117 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 118 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 119 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 120 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 121 | } |
| 122 | return NULL; |
| 123 | } |
| 124 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 125 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 126 | { |
| 127 | struct list_head *cur; |
| 128 | struct btrfs_fs_devices *fs_devices; |
| 129 | |
| 130 | list_for_each(cur, &fs_uuids) { |
| 131 | fs_devices = list_entry(cur, struct btrfs_fs_devices, list); |
| 132 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 133 | return fs_devices; |
| 134 | } |
| 135 | return NULL; |
| 136 | } |
| 137 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 138 | /* |
| 139 | * we try to collect pending bios for a device so we don't get a large |
| 140 | * number of procs sending bios down to the same device. This greatly |
| 141 | * improves the schedulers ability to collect and merge the bios. |
| 142 | * |
| 143 | * But, it also turns into a long list of bios to process and that is sure |
| 144 | * to eventually make the worker thread block. The solution here is to |
| 145 | * make some progress and then put this work struct back at the end of |
| 146 | * the list if the block device is congested. This way, multiple devices |
| 147 | * can make progress from a single worker thread. |
| 148 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 149 | static int noinline run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 150 | { |
| 151 | struct bio *pending; |
| 152 | struct backing_dev_info *bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 153 | struct btrfs_fs_info *fs_info; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 154 | struct bio *tail; |
| 155 | struct bio *cur; |
| 156 | int again = 0; |
| 157 | unsigned long num_run = 0; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 158 | unsigned long limit; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 159 | |
| 160 | bdi = device->bdev->bd_inode->i_mapping->backing_dev_info; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 161 | fs_info = device->dev_root->fs_info; |
| 162 | limit = btrfs_async_submit_limit(fs_info); |
| 163 | limit = limit * 2 / 3; |
| 164 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 165 | loop: |
| 166 | spin_lock(&device->io_lock); |
| 167 | |
| 168 | /* take all the bios off the list at once and process them |
| 169 | * later on (without the lock held). But, remember the |
| 170 | * tail and other pointers so the bios can be properly reinserted |
| 171 | * into the list if we hit congestion |
| 172 | */ |
| 173 | pending = device->pending_bios; |
| 174 | tail = device->pending_bio_tail; |
| 175 | WARN_ON(pending && !tail); |
| 176 | device->pending_bios = NULL; |
| 177 | device->pending_bio_tail = NULL; |
| 178 | |
| 179 | /* |
| 180 | * if pending was null this time around, no bios need processing |
| 181 | * at all and we can stop. Otherwise it'll loop back up again |
| 182 | * and do an additional check so no bios are missed. |
| 183 | * |
| 184 | * device->running_pending is used to synchronize with the |
| 185 | * schedule_bio code. |
| 186 | */ |
| 187 | if (pending) { |
| 188 | again = 1; |
| 189 | device->running_pending = 1; |
| 190 | } else { |
| 191 | again = 0; |
| 192 | device->running_pending = 0; |
| 193 | } |
| 194 | spin_unlock(&device->io_lock); |
| 195 | |
| 196 | while(pending) { |
| 197 | cur = pending; |
| 198 | pending = pending->bi_next; |
| 199 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 200 | atomic_dec(&fs_info->nr_async_bios); |
| 201 | |
| 202 | if (atomic_read(&fs_info->nr_async_bios) < limit && |
| 203 | waitqueue_active(&fs_info->async_submit_wait)) |
| 204 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 205 | |
| 206 | BUG_ON(atomic_read(&cur->bi_cnt) == 0); |
| 207 | bio_get(cur); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 208 | submit_bio(cur->bi_rw, cur); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 209 | bio_put(cur); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 210 | num_run++; |
| 211 | |
| 212 | /* |
| 213 | * we made progress, there is more work to do and the bdi |
| 214 | * is now congested. Back off and let other work structs |
| 215 | * run instead |
| 216 | */ |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 217 | if (pending && bdi_write_congested(bdi) && |
| 218 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 219 | struct bio *old_head; |
| 220 | |
| 221 | spin_lock(&device->io_lock); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 222 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 223 | old_head = device->pending_bios; |
| 224 | device->pending_bios = pending; |
| 225 | if (device->pending_bio_tail) |
| 226 | tail->bi_next = old_head; |
| 227 | else |
| 228 | device->pending_bio_tail = tail; |
| 229 | |
| 230 | spin_unlock(&device->io_lock); |
| 231 | btrfs_requeue_work(&device->work); |
| 232 | goto done; |
| 233 | } |
| 234 | } |
| 235 | if (again) |
| 236 | goto loop; |
| 237 | done: |
| 238 | return 0; |
| 239 | } |
| 240 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 241 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 242 | { |
| 243 | struct btrfs_device *device; |
| 244 | |
| 245 | device = container_of(work, struct btrfs_device, work); |
| 246 | run_scheduled_bios(device); |
| 247 | } |
| 248 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 249 | static noinline int device_list_add(const char *path, |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 250 | struct btrfs_super_block *disk_super, |
| 251 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) |
| 252 | { |
| 253 | struct btrfs_device *device; |
| 254 | struct btrfs_fs_devices *fs_devices; |
| 255 | u64 found_transid = btrfs_super_generation(disk_super); |
| 256 | |
| 257 | fs_devices = find_fsid(disk_super->fsid); |
| 258 | if (!fs_devices) { |
Chris Mason | 515dc32 | 2008-05-16 13:30:15 -0400 | [diff] [blame] | 259 | fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 260 | if (!fs_devices) |
| 261 | return -ENOMEM; |
| 262 | INIT_LIST_HEAD(&fs_devices->devices); |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 263 | INIT_LIST_HEAD(&fs_devices->alloc_list); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 264 | list_add(&fs_devices->list, &fs_uuids); |
| 265 | memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE); |
| 266 | fs_devices->latest_devid = devid; |
| 267 | fs_devices->latest_trans = found_transid; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 268 | device = NULL; |
| 269 | } else { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 270 | device = __find_device(&fs_devices->devices, devid, |
| 271 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 272 | } |
| 273 | if (!device) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 274 | if (fs_devices->opened) |
| 275 | return -EBUSY; |
| 276 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 277 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 278 | if (!device) { |
| 279 | /* we can safely leave the fs_devices entry around */ |
| 280 | return -ENOMEM; |
| 281 | } |
| 282 | device->devid = devid; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 283 | device->work.func = pending_bios_fn; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 284 | memcpy(device->uuid, disk_super->dev_item.uuid, |
| 285 | BTRFS_UUID_SIZE); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 286 | device->barriers = 1; |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 287 | spin_lock_init(&device->io_lock); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 288 | device->name = kstrdup(path, GFP_NOFS); |
| 289 | if (!device->name) { |
| 290 | kfree(device); |
| 291 | return -ENOMEM; |
| 292 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 293 | INIT_LIST_HEAD(&device->dev_alloc_list); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 294 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 295 | device->fs_devices = fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 296 | fs_devices->num_devices++; |
| 297 | } |
| 298 | |
| 299 | if (found_transid > fs_devices->latest_trans) { |
| 300 | fs_devices->latest_devid = devid; |
| 301 | fs_devices->latest_trans = found_transid; |
| 302 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 303 | *fs_devices_ret = fs_devices; |
| 304 | return 0; |
| 305 | } |
| 306 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 307 | int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices) |
| 308 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 309 | struct list_head *tmp; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 310 | struct list_head *cur; |
| 311 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 312 | int seed_devices = 0; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 313 | |
| 314 | mutex_lock(&uuid_mutex); |
| 315 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 316 | list_for_each_safe(cur, tmp, &fs_devices->devices) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 317 | device = list_entry(cur, struct btrfs_device, dev_list); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 318 | if (device->in_fs_metadata) |
| 319 | continue; |
| 320 | |
| 321 | if (device->bdev) { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 322 | close_bdev_exclusive(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 323 | device->bdev = NULL; |
| 324 | fs_devices->open_devices--; |
| 325 | } |
| 326 | if (device->writeable) { |
| 327 | list_del_init(&device->dev_alloc_list); |
| 328 | device->writeable = 0; |
| 329 | fs_devices->rw_devices--; |
| 330 | } |
| 331 | if (!seed_devices) { |
| 332 | list_del_init(&device->dev_list); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 333 | fs_devices->num_devices--; |
| 334 | kfree(device->name); |
| 335 | kfree(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 336 | } |
| 337 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 338 | |
| 339 | if (fs_devices->seed) { |
| 340 | fs_devices = fs_devices->seed; |
| 341 | seed_devices = 1; |
| 342 | goto again; |
| 343 | } |
| 344 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 345 | mutex_unlock(&uuid_mutex); |
| 346 | return 0; |
| 347 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 348 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 349 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 350 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 351 | struct btrfs_fs_devices *seed_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 352 | struct list_head *cur; |
| 353 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 354 | again: |
| 355 | if (--fs_devices->opened > 0) |
| 356 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 357 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 358 | list_for_each(cur, &fs_devices->devices) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 359 | device = list_entry(cur, struct btrfs_device, dev_list); |
| 360 | if (device->bdev) { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 361 | close_bdev_exclusive(device->bdev, device->mode); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 362 | fs_devices->open_devices--; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 363 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 364 | if (device->writeable) { |
| 365 | list_del_init(&device->dev_alloc_list); |
| 366 | fs_devices->rw_devices--; |
| 367 | } |
| 368 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 369 | device->bdev = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 370 | device->writeable = 0; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 371 | device->in_fs_metadata = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 372 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 373 | fs_devices->opened = 0; |
| 374 | fs_devices->seeding = 0; |
| 375 | fs_devices->sprouted = 0; |
| 376 | |
| 377 | seed_devices = fs_devices->seed; |
| 378 | fs_devices->seed = NULL; |
| 379 | if (seed_devices) { |
| 380 | fs_devices = seed_devices; |
| 381 | goto again; |
| 382 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 383 | return 0; |
| 384 | } |
| 385 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 386 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 387 | { |
| 388 | int ret; |
| 389 | |
| 390 | mutex_lock(&uuid_mutex); |
| 391 | ret = __btrfs_close_devices(fs_devices); |
| 392 | mutex_unlock(&uuid_mutex); |
| 393 | return ret; |
| 394 | } |
| 395 | |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 396 | int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 397 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 398 | { |
| 399 | struct block_device *bdev; |
| 400 | struct list_head *head = &fs_devices->devices; |
| 401 | struct list_head *cur; |
| 402 | struct btrfs_device *device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 403 | struct block_device *latest_bdev = NULL; |
| 404 | struct buffer_head *bh; |
| 405 | struct btrfs_super_block *disk_super; |
| 406 | u64 latest_devid = 0; |
| 407 | u64 latest_transid = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 408 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 409 | int seeding = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 410 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 411 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 412 | list_for_each(cur, head) { |
| 413 | device = list_entry(cur, struct btrfs_device, dev_list); |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 414 | if (device->bdev) |
| 415 | continue; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 416 | if (!device->name) |
| 417 | continue; |
| 418 | |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 419 | bdev = open_bdev_exclusive(device->name, flags, holder); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 420 | if (IS_ERR(bdev)) { |
| 421 | printk("open %s failed\n", device->name); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 422 | goto error; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 423 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 424 | set_blocksize(bdev, 4096); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 425 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 426 | bh = btrfs_read_dev_super(bdev); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 427 | if (!bh) |
| 428 | goto error_close; |
| 429 | |
| 430 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 431 | devid = le64_to_cpu(disk_super->dev_item.devid); |
| 432 | if (devid != device->devid) |
| 433 | goto error_brelse; |
| 434 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 435 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
| 436 | BTRFS_UUID_SIZE)) |
| 437 | goto error_brelse; |
| 438 | |
| 439 | device->generation = btrfs_super_generation(disk_super); |
| 440 | if (!latest_transid || device->generation > latest_transid) { |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 441 | latest_devid = devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 442 | latest_transid = device->generation; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 443 | latest_bdev = bdev; |
| 444 | } |
| 445 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 446 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| 447 | device->writeable = 0; |
| 448 | } else { |
| 449 | device->writeable = !bdev_read_only(bdev); |
| 450 | seeding = 0; |
| 451 | } |
| 452 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 453 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 454 | device->in_fs_metadata = 0; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 455 | device->mode = flags; |
| 456 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 457 | fs_devices->open_devices++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 458 | if (device->writeable) { |
| 459 | fs_devices->rw_devices++; |
| 460 | list_add(&device->dev_alloc_list, |
| 461 | &fs_devices->alloc_list); |
| 462 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 463 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 464 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 465 | error_brelse: |
| 466 | brelse(bh); |
| 467 | error_close: |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 468 | close_bdev_exclusive(bdev, FMODE_READ); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 469 | error: |
| 470 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 471 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 472 | if (fs_devices->open_devices == 0) { |
| 473 | ret = -EIO; |
| 474 | goto out; |
| 475 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 476 | fs_devices->seeding = seeding; |
| 477 | fs_devices->opened = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 478 | fs_devices->latest_bdev = latest_bdev; |
| 479 | fs_devices->latest_devid = latest_devid; |
| 480 | fs_devices->latest_trans = latest_transid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 481 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 482 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 483 | return ret; |
| 484 | } |
| 485 | |
| 486 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 487 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 488 | { |
| 489 | int ret; |
| 490 | |
| 491 | mutex_lock(&uuid_mutex); |
| 492 | if (fs_devices->opened) { |
| 493 | if (fs_devices->sprouted) { |
| 494 | ret = -EBUSY; |
| 495 | } else { |
| 496 | fs_devices->opened++; |
| 497 | ret = 0; |
| 498 | } |
| 499 | } else { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 500 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 501 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 502 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 503 | return ret; |
| 504 | } |
| 505 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 506 | int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder, |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 507 | struct btrfs_fs_devices **fs_devices_ret) |
| 508 | { |
| 509 | struct btrfs_super_block *disk_super; |
| 510 | struct block_device *bdev; |
| 511 | struct buffer_head *bh; |
| 512 | int ret; |
| 513 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 514 | u64 transid; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 515 | |
| 516 | mutex_lock(&uuid_mutex); |
| 517 | |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 518 | bdev = open_bdev_exclusive(path, flags, holder); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 519 | |
| 520 | if (IS_ERR(bdev)) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 521 | ret = PTR_ERR(bdev); |
| 522 | goto error; |
| 523 | } |
| 524 | |
| 525 | ret = set_blocksize(bdev, 4096); |
| 526 | if (ret) |
| 527 | goto error_close; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 528 | bh = btrfs_read_dev_super(bdev); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 529 | if (!bh) { |
| 530 | ret = -EIO; |
| 531 | goto error_close; |
| 532 | } |
| 533 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 534 | devid = le64_to_cpu(disk_super->dev_item.devid); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 535 | transid = btrfs_super_generation(disk_super); |
Chris Mason | 7ae9c09 | 2008-04-18 10:29:49 -0400 | [diff] [blame] | 536 | if (disk_super->label[0]) |
| 537 | printk("device label %s ", disk_super->label); |
| 538 | else { |
| 539 | /* FIXME, make a readl uuid parser */ |
| 540 | printk("device fsid %llx-%llx ", |
| 541 | *(unsigned long long *)disk_super->fsid, |
| 542 | *(unsigned long long *)(disk_super->fsid + 8)); |
| 543 | } |
| 544 | printk("devid %Lu transid %Lu %s\n", devid, transid, path); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 545 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
| 546 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 547 | brelse(bh); |
| 548 | error_close: |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 549 | close_bdev_exclusive(bdev, flags); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 550 | error: |
| 551 | mutex_unlock(&uuid_mutex); |
| 552 | return ret; |
| 553 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 554 | |
| 555 | /* |
| 556 | * this uses a pretty simple search, the expectation is that it is |
| 557 | * called very infrequently and that a given device has a small number |
| 558 | * of extents |
| 559 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 560 | static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 561 | struct btrfs_device *device, |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 562 | u64 num_bytes, u64 *start) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 563 | { |
| 564 | struct btrfs_key key; |
| 565 | struct btrfs_root *root = device->dev_root; |
| 566 | struct btrfs_dev_extent *dev_extent = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 567 | struct btrfs_path *path; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 568 | u64 hole_size = 0; |
| 569 | u64 last_byte = 0; |
| 570 | u64 search_start = 0; |
| 571 | u64 search_end = device->total_bytes; |
| 572 | int ret; |
| 573 | int slot = 0; |
| 574 | int start_found; |
| 575 | struct extent_buffer *l; |
| 576 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 577 | path = btrfs_alloc_path(); |
| 578 | if (!path) |
| 579 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 580 | path->reada = 2; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 581 | start_found = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 582 | |
| 583 | /* FIXME use last free of some kind */ |
| 584 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 585 | /* we don't want to overwrite the superblock on the drive, |
| 586 | * so we make sure to start at an offset of at least 1MB |
| 587 | */ |
| 588 | search_start = max((u64)1024 * 1024, search_start); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 589 | |
| 590 | if (root->fs_info->alloc_start + num_bytes <= device->total_bytes) |
| 591 | search_start = max(root->fs_info->alloc_start, search_start); |
| 592 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 593 | key.objectid = device->devid; |
| 594 | key.offset = search_start; |
| 595 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 596 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); |
| 597 | if (ret < 0) |
| 598 | goto error; |
| 599 | ret = btrfs_previous_item(root, path, 0, key.type); |
| 600 | if (ret < 0) |
| 601 | goto error; |
| 602 | l = path->nodes[0]; |
| 603 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 604 | while (1) { |
| 605 | l = path->nodes[0]; |
| 606 | slot = path->slots[0]; |
| 607 | if (slot >= btrfs_header_nritems(l)) { |
| 608 | ret = btrfs_next_leaf(root, path); |
| 609 | if (ret == 0) |
| 610 | continue; |
| 611 | if (ret < 0) |
| 612 | goto error; |
| 613 | no_more_items: |
| 614 | if (!start_found) { |
| 615 | if (search_start >= search_end) { |
| 616 | ret = -ENOSPC; |
| 617 | goto error; |
| 618 | } |
| 619 | *start = search_start; |
| 620 | start_found = 1; |
| 621 | goto check_pending; |
| 622 | } |
| 623 | *start = last_byte > search_start ? |
| 624 | last_byte : search_start; |
| 625 | if (search_end <= *start) { |
| 626 | ret = -ENOSPC; |
| 627 | goto error; |
| 628 | } |
| 629 | goto check_pending; |
| 630 | } |
| 631 | btrfs_item_key_to_cpu(l, &key, slot); |
| 632 | |
| 633 | if (key.objectid < device->devid) |
| 634 | goto next; |
| 635 | |
| 636 | if (key.objectid > device->devid) |
| 637 | goto no_more_items; |
| 638 | |
| 639 | if (key.offset >= search_start && key.offset > last_byte && |
| 640 | start_found) { |
| 641 | if (last_byte < search_start) |
| 642 | last_byte = search_start; |
| 643 | hole_size = key.offset - last_byte; |
| 644 | if (key.offset > last_byte && |
| 645 | hole_size >= num_bytes) { |
| 646 | *start = last_byte; |
| 647 | goto check_pending; |
| 648 | } |
| 649 | } |
| 650 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) { |
| 651 | goto next; |
| 652 | } |
| 653 | |
| 654 | start_found = 1; |
| 655 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 656 | last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent); |
| 657 | next: |
| 658 | path->slots[0]++; |
| 659 | cond_resched(); |
| 660 | } |
| 661 | check_pending: |
| 662 | /* we have to make sure we didn't find an extent that has already |
| 663 | * been allocated by the map tree or the original allocation |
| 664 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 665 | BUG_ON(*start < search_start); |
| 666 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 667 | if (*start + num_bytes > search_end) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 668 | ret = -ENOSPC; |
| 669 | goto error; |
| 670 | } |
| 671 | /* check for pending inserts here */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 672 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 673 | |
| 674 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 675 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 676 | return ret; |
| 677 | } |
| 678 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 679 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 680 | struct btrfs_device *device, |
| 681 | u64 start) |
| 682 | { |
| 683 | int ret; |
| 684 | struct btrfs_path *path; |
| 685 | struct btrfs_root *root = device->dev_root; |
| 686 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 687 | struct btrfs_key found_key; |
| 688 | struct extent_buffer *leaf = NULL; |
| 689 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 690 | |
| 691 | path = btrfs_alloc_path(); |
| 692 | if (!path) |
| 693 | return -ENOMEM; |
| 694 | |
| 695 | key.objectid = device->devid; |
| 696 | key.offset = start; |
| 697 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 698 | |
| 699 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 700 | if (ret > 0) { |
| 701 | ret = btrfs_previous_item(root, path, key.objectid, |
| 702 | BTRFS_DEV_EXTENT_KEY); |
| 703 | BUG_ON(ret); |
| 704 | leaf = path->nodes[0]; |
| 705 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 706 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 707 | struct btrfs_dev_extent); |
| 708 | BUG_ON(found_key.offset > start || found_key.offset + |
| 709 | btrfs_dev_extent_length(leaf, extent) < start); |
| 710 | ret = 0; |
| 711 | } else if (ret == 0) { |
| 712 | leaf = path->nodes[0]; |
| 713 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 714 | struct btrfs_dev_extent); |
| 715 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 716 | BUG_ON(ret); |
| 717 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 718 | if (device->bytes_used > 0) |
| 719 | device->bytes_used -= btrfs_dev_extent_length(leaf, extent); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 720 | ret = btrfs_del_item(trans, root, path); |
| 721 | BUG_ON(ret); |
| 722 | |
| 723 | btrfs_free_path(path); |
| 724 | return ret; |
| 725 | } |
| 726 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 727 | int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 728 | struct btrfs_device *device, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 729 | u64 chunk_tree, u64 chunk_objectid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 730 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 731 | { |
| 732 | int ret; |
| 733 | struct btrfs_path *path; |
| 734 | struct btrfs_root *root = device->dev_root; |
| 735 | struct btrfs_dev_extent *extent; |
| 736 | struct extent_buffer *leaf; |
| 737 | struct btrfs_key key; |
| 738 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 739 | WARN_ON(!device->in_fs_metadata); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 740 | path = btrfs_alloc_path(); |
| 741 | if (!path) |
| 742 | return -ENOMEM; |
| 743 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 744 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 745 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 746 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 747 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 748 | sizeof(*extent)); |
| 749 | BUG_ON(ret); |
| 750 | |
| 751 | leaf = path->nodes[0]; |
| 752 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 753 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 754 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 755 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 756 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 757 | |
| 758 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
| 759 | (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent), |
| 760 | BTRFS_UUID_SIZE); |
| 761 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 762 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 763 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 764 | btrfs_free_path(path); |
| 765 | return ret; |
| 766 | } |
| 767 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 768 | static noinline int find_next_chunk(struct btrfs_root *root, |
| 769 | u64 objectid, u64 *offset) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 770 | { |
| 771 | struct btrfs_path *path; |
| 772 | int ret; |
| 773 | struct btrfs_key key; |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 774 | struct btrfs_chunk *chunk; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 775 | struct btrfs_key found_key; |
| 776 | |
| 777 | path = btrfs_alloc_path(); |
| 778 | BUG_ON(!path); |
| 779 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 780 | key.objectid = objectid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 781 | key.offset = (u64)-1; |
| 782 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 783 | |
| 784 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 785 | if (ret < 0) |
| 786 | goto error; |
| 787 | |
| 788 | BUG_ON(ret == 0); |
| 789 | |
| 790 | ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY); |
| 791 | if (ret) { |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 792 | *offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 793 | } else { |
| 794 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 795 | path->slots[0]); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 796 | if (found_key.objectid != objectid) |
| 797 | *offset = 0; |
| 798 | else { |
| 799 | chunk = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 800 | struct btrfs_chunk); |
| 801 | *offset = found_key.offset + |
| 802 | btrfs_chunk_length(path->nodes[0], chunk); |
| 803 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 804 | } |
| 805 | ret = 0; |
| 806 | error: |
| 807 | btrfs_free_path(path); |
| 808 | return ret; |
| 809 | } |
| 810 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 811 | static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 812 | { |
| 813 | int ret; |
| 814 | struct btrfs_key key; |
| 815 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 816 | struct btrfs_path *path; |
| 817 | |
| 818 | root = root->fs_info->chunk_root; |
| 819 | |
| 820 | path = btrfs_alloc_path(); |
| 821 | if (!path) |
| 822 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 823 | |
| 824 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 825 | key.type = BTRFS_DEV_ITEM_KEY; |
| 826 | key.offset = (u64)-1; |
| 827 | |
| 828 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 829 | if (ret < 0) |
| 830 | goto error; |
| 831 | |
| 832 | BUG_ON(ret == 0); |
| 833 | |
| 834 | ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID, |
| 835 | BTRFS_DEV_ITEM_KEY); |
| 836 | if (ret) { |
| 837 | *objectid = 1; |
| 838 | } else { |
| 839 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 840 | path->slots[0]); |
| 841 | *objectid = found_key.offset + 1; |
| 842 | } |
| 843 | ret = 0; |
| 844 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 845 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 846 | return ret; |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * the device information is stored in the chunk root |
| 851 | * the btrfs_device struct should be fully filled in |
| 852 | */ |
| 853 | int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 854 | struct btrfs_root *root, |
| 855 | struct btrfs_device *device) |
| 856 | { |
| 857 | int ret; |
| 858 | struct btrfs_path *path; |
| 859 | struct btrfs_dev_item *dev_item; |
| 860 | struct extent_buffer *leaf; |
| 861 | struct btrfs_key key; |
| 862 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 863 | |
| 864 | root = root->fs_info->chunk_root; |
| 865 | |
| 866 | path = btrfs_alloc_path(); |
| 867 | if (!path) |
| 868 | return -ENOMEM; |
| 869 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 870 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 871 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 872 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 873 | |
| 874 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 875 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 876 | if (ret) |
| 877 | goto out; |
| 878 | |
| 879 | leaf = path->nodes[0]; |
| 880 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 881 | |
| 882 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 883 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 884 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 885 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 886 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 887 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 888 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
| 889 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 890 | btrfs_set_device_group(leaf, dev_item, 0); |
| 891 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 892 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame^] | 893 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 894 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 895 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 896 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 897 | ptr = (unsigned long)btrfs_device_fsid(dev_item); |
| 898 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 899 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 900 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 901 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 902 | out: |
| 903 | btrfs_free_path(path); |
| 904 | return ret; |
| 905 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 906 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 907 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 908 | struct btrfs_device *device) |
| 909 | { |
| 910 | int ret; |
| 911 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 912 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 913 | struct btrfs_trans_handle *trans; |
| 914 | |
| 915 | root = root->fs_info->chunk_root; |
| 916 | |
| 917 | path = btrfs_alloc_path(); |
| 918 | if (!path) |
| 919 | return -ENOMEM; |
| 920 | |
| 921 | trans = btrfs_start_transaction(root, 1); |
| 922 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 923 | key.type = BTRFS_DEV_ITEM_KEY; |
| 924 | key.offset = device->devid; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 925 | lock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 926 | |
| 927 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 928 | if (ret < 0) |
| 929 | goto out; |
| 930 | |
| 931 | if (ret > 0) { |
| 932 | ret = -ENOENT; |
| 933 | goto out; |
| 934 | } |
| 935 | |
| 936 | ret = btrfs_del_item(trans, root, path); |
| 937 | if (ret) |
| 938 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 939 | out: |
| 940 | btrfs_free_path(path); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 941 | unlock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 942 | btrfs_commit_transaction(trans, root); |
| 943 | return ret; |
| 944 | } |
| 945 | |
| 946 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 947 | { |
| 948 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 949 | struct btrfs_device *next_device; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 950 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 951 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 952 | struct btrfs_super_block *disk_super; |
| 953 | u64 all_avail; |
| 954 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 955 | u64 num_devices; |
| 956 | u8 *dev_uuid; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 957 | int ret = 0; |
| 958 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 959 | mutex_lock(&uuid_mutex); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 960 | mutex_lock(&root->fs_info->volume_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 961 | |
| 962 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 963 | root->fs_info->avail_system_alloc_bits | |
| 964 | root->fs_info->avail_metadata_alloc_bits; |
| 965 | |
| 966 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 967 | root->fs_info->fs_devices->rw_devices <= 4) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 968 | printk("btrfs: unable to go below four devices on raid10\n"); |
| 969 | ret = -EINVAL; |
| 970 | goto out; |
| 971 | } |
| 972 | |
| 973 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 974 | root->fs_info->fs_devices->rw_devices <= 2) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 975 | printk("btrfs: unable to go below two devices on raid1\n"); |
| 976 | ret = -EINVAL; |
| 977 | goto out; |
| 978 | } |
| 979 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 980 | if (strcmp(device_path, "missing") == 0) { |
| 981 | struct list_head *cur; |
| 982 | struct list_head *devices; |
| 983 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 984 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 985 | device = NULL; |
| 986 | devices = &root->fs_info->fs_devices->devices; |
| 987 | list_for_each(cur, devices) { |
| 988 | tmp = list_entry(cur, struct btrfs_device, dev_list); |
| 989 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 990 | device = tmp; |
| 991 | break; |
| 992 | } |
| 993 | } |
| 994 | bdev = NULL; |
| 995 | bh = NULL; |
| 996 | disk_super = NULL; |
| 997 | if (!device) { |
| 998 | printk("btrfs: no missing devices found to remove\n"); |
| 999 | goto out; |
| 1000 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1001 | } else { |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1002 | bdev = open_bdev_exclusive(device_path, FMODE_READ, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1003 | root->fs_info->bdev_holder); |
| 1004 | if (IS_ERR(bdev)) { |
| 1005 | ret = PTR_ERR(bdev); |
| 1006 | goto out; |
| 1007 | } |
| 1008 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1009 | set_blocksize(bdev, 4096); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 1010 | bh = btrfs_read_dev_super(bdev); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1011 | if (!bh) { |
| 1012 | ret = -EIO; |
| 1013 | goto error_close; |
| 1014 | } |
| 1015 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1016 | devid = le64_to_cpu(disk_super->dev_item.devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1017 | dev_uuid = disk_super->dev_item.uuid; |
| 1018 | device = btrfs_find_device(root, devid, dev_uuid, |
| 1019 | disk_super->fsid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1020 | if (!device) { |
| 1021 | ret = -ENOENT; |
| 1022 | goto error_brelse; |
| 1023 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1024 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1025 | |
| 1026 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
| 1027 | printk("btrfs: unable to remove the only writeable device\n"); |
| 1028 | ret = -EINVAL; |
| 1029 | goto error_brelse; |
| 1030 | } |
| 1031 | |
| 1032 | if (device->writeable) { |
| 1033 | list_del_init(&device->dev_alloc_list); |
| 1034 | root->fs_info->fs_devices->rw_devices--; |
| 1035 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1036 | |
| 1037 | ret = btrfs_shrink_device(device, 0); |
| 1038 | if (ret) |
| 1039 | goto error_brelse; |
| 1040 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1041 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1042 | if (ret) |
| 1043 | goto error_brelse; |
| 1044 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1045 | device->in_fs_metadata = 0; |
| 1046 | if (device->fs_devices == root->fs_info->fs_devices) { |
| 1047 | list_del_init(&device->dev_list); |
| 1048 | root->fs_info->fs_devices->num_devices--; |
| 1049 | if (device->bdev) |
| 1050 | device->fs_devices->open_devices--; |
| 1051 | } |
| 1052 | |
| 1053 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1054 | struct btrfs_device, dev_list); |
| 1055 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1056 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1057 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1058 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1059 | |
| 1060 | num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1; |
| 1061 | btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices); |
| 1062 | |
| 1063 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 1064 | BUG_ON(device->writeable); |
| 1065 | brelse(bh); |
| 1066 | if (bdev) |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1067 | close_bdev_exclusive(bdev, FMODE_READ); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1068 | |
| 1069 | if (device->bdev) { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1070 | close_bdev_exclusive(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1071 | device->bdev = NULL; |
| 1072 | device->fs_devices->open_devices--; |
| 1073 | } |
| 1074 | if (device->fs_devices->open_devices == 0) { |
| 1075 | struct btrfs_fs_devices *fs_devices; |
| 1076 | fs_devices = root->fs_info->fs_devices; |
| 1077 | while (fs_devices) { |
| 1078 | if (fs_devices->seed == device->fs_devices) |
| 1079 | break; |
| 1080 | fs_devices = fs_devices->seed; |
| 1081 | } |
| 1082 | fs_devices->seed = device->fs_devices->seed; |
| 1083 | device->fs_devices->seed = NULL; |
| 1084 | __btrfs_close_devices(device->fs_devices); |
| 1085 | } |
| 1086 | ret = 0; |
| 1087 | goto out; |
| 1088 | } |
| 1089 | |
| 1090 | /* |
| 1091 | * at this point, the device is zero sized. We want to |
| 1092 | * remove it from the devices list and zero out the old super |
| 1093 | */ |
| 1094 | if (device->writeable) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1095 | /* make sure this device isn't detected as part of |
| 1096 | * the FS anymore |
| 1097 | */ |
| 1098 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1099 | set_buffer_dirty(bh); |
| 1100 | sync_dirty_buffer(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1101 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1102 | brelse(bh); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1103 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1104 | if (device->bdev) { |
| 1105 | /* one close for the device struct or super_block */ |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1106 | close_bdev_exclusive(device->bdev, device->mode); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1107 | } |
| 1108 | if (bdev) { |
| 1109 | /* one close for us */ |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1110 | close_bdev_exclusive(bdev, FMODE_READ); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1111 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1112 | kfree(device->name); |
| 1113 | kfree(device); |
| 1114 | ret = 0; |
| 1115 | goto out; |
| 1116 | |
| 1117 | error_brelse: |
| 1118 | brelse(bh); |
| 1119 | error_close: |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1120 | if (bdev) |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1121 | close_bdev_exclusive(bdev, FMODE_READ); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1122 | out: |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1123 | mutex_unlock(&root->fs_info->volume_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1124 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1125 | return ret; |
| 1126 | } |
| 1127 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1128 | /* |
| 1129 | * does all the dirty work required for changing file system's UUID. |
| 1130 | */ |
| 1131 | static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans, |
| 1132 | struct btrfs_root *root) |
| 1133 | { |
| 1134 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 1135 | struct btrfs_fs_devices *old_devices; |
| 1136 | struct btrfs_super_block *disk_super = &root->fs_info->super_copy; |
| 1137 | struct btrfs_device *device; |
| 1138 | u64 super_flags; |
| 1139 | |
| 1140 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
| 1141 | if (!fs_devices->seeding || fs_devices->opened != 1) |
| 1142 | return -EINVAL; |
| 1143 | |
| 1144 | old_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
| 1145 | if (!old_devices) |
| 1146 | return -ENOMEM; |
| 1147 | |
| 1148 | memcpy(old_devices, fs_devices, sizeof(*old_devices)); |
| 1149 | old_devices->opened = 1; |
| 1150 | old_devices->sprouted = 1; |
| 1151 | INIT_LIST_HEAD(&old_devices->devices); |
| 1152 | INIT_LIST_HEAD(&old_devices->alloc_list); |
| 1153 | list_splice_init(&fs_devices->devices, &old_devices->devices); |
| 1154 | list_splice_init(&fs_devices->alloc_list, &old_devices->alloc_list); |
| 1155 | list_for_each_entry(device, &old_devices->devices, dev_list) { |
| 1156 | device->fs_devices = old_devices; |
| 1157 | } |
| 1158 | list_add(&old_devices->list, &fs_uuids); |
| 1159 | |
| 1160 | fs_devices->seeding = 0; |
| 1161 | fs_devices->num_devices = 0; |
| 1162 | fs_devices->open_devices = 0; |
| 1163 | fs_devices->seed = old_devices; |
| 1164 | |
| 1165 | generate_random_uuid(fs_devices->fsid); |
| 1166 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 1167 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 1168 | super_flags = btrfs_super_flags(disk_super) & |
| 1169 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 1170 | btrfs_set_super_flags(disk_super, super_flags); |
| 1171 | |
| 1172 | return 0; |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * strore the expected generation for seed devices in device items. |
| 1177 | */ |
| 1178 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 1179 | struct btrfs_root *root) |
| 1180 | { |
| 1181 | struct btrfs_path *path; |
| 1182 | struct extent_buffer *leaf; |
| 1183 | struct btrfs_dev_item *dev_item; |
| 1184 | struct btrfs_device *device; |
| 1185 | struct btrfs_key key; |
| 1186 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 1187 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 1188 | u64 devid; |
| 1189 | int ret; |
| 1190 | |
| 1191 | path = btrfs_alloc_path(); |
| 1192 | if (!path) |
| 1193 | return -ENOMEM; |
| 1194 | |
| 1195 | root = root->fs_info->chunk_root; |
| 1196 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1197 | key.offset = 0; |
| 1198 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1199 | |
| 1200 | while (1) { |
| 1201 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 1202 | if (ret < 0) |
| 1203 | goto error; |
| 1204 | |
| 1205 | leaf = path->nodes[0]; |
| 1206 | next_slot: |
| 1207 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1208 | ret = btrfs_next_leaf(root, path); |
| 1209 | if (ret > 0) |
| 1210 | break; |
| 1211 | if (ret < 0) |
| 1212 | goto error; |
| 1213 | leaf = path->nodes[0]; |
| 1214 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1215 | btrfs_release_path(root, path); |
| 1216 | continue; |
| 1217 | } |
| 1218 | |
| 1219 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1220 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 1221 | key.type != BTRFS_DEV_ITEM_KEY) |
| 1222 | break; |
| 1223 | |
| 1224 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 1225 | struct btrfs_dev_item); |
| 1226 | devid = btrfs_device_id(leaf, dev_item); |
| 1227 | read_extent_buffer(leaf, dev_uuid, |
| 1228 | (unsigned long)btrfs_device_uuid(dev_item), |
| 1229 | BTRFS_UUID_SIZE); |
| 1230 | read_extent_buffer(leaf, fs_uuid, |
| 1231 | (unsigned long)btrfs_device_fsid(dev_item), |
| 1232 | BTRFS_UUID_SIZE); |
| 1233 | device = btrfs_find_device(root, devid, dev_uuid, fs_uuid); |
| 1234 | BUG_ON(!device); |
| 1235 | |
| 1236 | if (device->fs_devices->seeding) { |
| 1237 | btrfs_set_device_generation(leaf, dev_item, |
| 1238 | device->generation); |
| 1239 | btrfs_mark_buffer_dirty(leaf); |
| 1240 | } |
| 1241 | |
| 1242 | path->slots[0]++; |
| 1243 | goto next_slot; |
| 1244 | } |
| 1245 | ret = 0; |
| 1246 | error: |
| 1247 | btrfs_free_path(path); |
| 1248 | return ret; |
| 1249 | } |
| 1250 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1251 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 1252 | { |
| 1253 | struct btrfs_trans_handle *trans; |
| 1254 | struct btrfs_device *device; |
| 1255 | struct block_device *bdev; |
| 1256 | struct list_head *cur; |
| 1257 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1258 | struct super_block *sb = root->fs_info->sb; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1259 | u64 total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1260 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1261 | int ret = 0; |
| 1262 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1263 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
| 1264 | return -EINVAL; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1265 | |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1266 | bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1267 | if (!bdev) { |
| 1268 | return -EIO; |
| 1269 | } |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1270 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1271 | if (root->fs_info->fs_devices->seeding) { |
| 1272 | seeding_dev = 1; |
| 1273 | down_write(&sb->s_umount); |
| 1274 | mutex_lock(&uuid_mutex); |
| 1275 | } |
| 1276 | |
Chris Mason | 8c8bee1d | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 1277 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1278 | mutex_lock(&root->fs_info->volume_mutex); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1279 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1280 | devices = &root->fs_info->fs_devices->devices; |
| 1281 | list_for_each(cur, devices) { |
| 1282 | device = list_entry(cur, struct btrfs_device, dev_list); |
| 1283 | if (device->bdev == bdev) { |
| 1284 | ret = -EEXIST; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1285 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 1290 | if (!device) { |
| 1291 | /* we can safely leave the fs_devices entry around */ |
| 1292 | ret = -ENOMEM; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1293 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1294 | } |
| 1295 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1296 | device->name = kstrdup(device_path, GFP_NOFS); |
| 1297 | if (!device->name) { |
| 1298 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1299 | ret = -ENOMEM; |
| 1300 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1301 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1302 | |
| 1303 | ret = find_next_devid(root, &device->devid); |
| 1304 | if (ret) { |
| 1305 | kfree(device); |
| 1306 | goto error; |
| 1307 | } |
| 1308 | |
| 1309 | trans = btrfs_start_transaction(root, 1); |
| 1310 | lock_chunks(root); |
| 1311 | |
| 1312 | device->barriers = 1; |
| 1313 | device->writeable = 1; |
| 1314 | device->work.func = pending_bios_fn; |
| 1315 | generate_random_uuid(device->uuid); |
| 1316 | spin_lock_init(&device->io_lock); |
| 1317 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1318 | device->io_width = root->sectorsize; |
| 1319 | device->io_align = root->sectorsize; |
| 1320 | device->sector_size = root->sectorsize; |
| 1321 | device->total_bytes = i_size_read(bdev->bd_inode); |
| 1322 | device->dev_root = root->fs_info->dev_root; |
| 1323 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1324 | device->in_fs_metadata = 1; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1325 | device->mode = 0; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 1326 | set_blocksize(device->bdev, 4096); |
| 1327 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1328 | if (seeding_dev) { |
| 1329 | sb->s_flags &= ~MS_RDONLY; |
| 1330 | ret = btrfs_prepare_sprout(trans, root); |
| 1331 | BUG_ON(ret); |
| 1332 | } |
| 1333 | |
| 1334 | device->fs_devices = root->fs_info->fs_devices; |
| 1335 | list_add(&device->dev_list, &root->fs_info->fs_devices->devices); |
| 1336 | list_add(&device->dev_alloc_list, |
| 1337 | &root->fs_info->fs_devices->alloc_list); |
| 1338 | root->fs_info->fs_devices->num_devices++; |
| 1339 | root->fs_info->fs_devices->open_devices++; |
| 1340 | root->fs_info->fs_devices->rw_devices++; |
| 1341 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 1342 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1343 | total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
| 1344 | btrfs_set_super_total_bytes(&root->fs_info->super_copy, |
| 1345 | total_bytes + device->total_bytes); |
| 1346 | |
| 1347 | total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy); |
| 1348 | btrfs_set_super_num_devices(&root->fs_info->super_copy, |
| 1349 | total_bytes + 1); |
| 1350 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1351 | if (seeding_dev) { |
| 1352 | ret = init_first_rw_device(trans, root, device); |
| 1353 | BUG_ON(ret); |
| 1354 | ret = btrfs_finish_sprout(trans, root); |
| 1355 | BUG_ON(ret); |
| 1356 | } else { |
| 1357 | ret = btrfs_add_device(trans, root, device); |
| 1358 | } |
| 1359 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1360 | unlock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1361 | btrfs_commit_transaction(trans, root); |
| 1362 | |
| 1363 | if (seeding_dev) { |
| 1364 | mutex_unlock(&uuid_mutex); |
| 1365 | up_write(&sb->s_umount); |
| 1366 | |
| 1367 | ret = btrfs_relocate_sys_chunks(root); |
| 1368 | BUG_ON(ret); |
| 1369 | } |
| 1370 | out: |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1371 | mutex_unlock(&root->fs_info->volume_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1372 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1373 | error: |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1374 | close_bdev_exclusive(bdev, 0); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1375 | if (seeding_dev) { |
| 1376 | mutex_unlock(&uuid_mutex); |
| 1377 | up_write(&sb->s_umount); |
| 1378 | } |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1379 | goto out; |
| 1380 | } |
| 1381 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1382 | static int noinline btrfs_update_device(struct btrfs_trans_handle *trans, |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 1383 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1384 | { |
| 1385 | int ret; |
| 1386 | struct btrfs_path *path; |
| 1387 | struct btrfs_root *root; |
| 1388 | struct btrfs_dev_item *dev_item; |
| 1389 | struct extent_buffer *leaf; |
| 1390 | struct btrfs_key key; |
| 1391 | |
| 1392 | root = device->dev_root->fs_info->chunk_root; |
| 1393 | |
| 1394 | path = btrfs_alloc_path(); |
| 1395 | if (!path) |
| 1396 | return -ENOMEM; |
| 1397 | |
| 1398 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1399 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1400 | key.offset = device->devid; |
| 1401 | |
| 1402 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 1403 | if (ret < 0) |
| 1404 | goto out; |
| 1405 | |
| 1406 | if (ret > 0) { |
| 1407 | ret = -ENOENT; |
| 1408 | goto out; |
| 1409 | } |
| 1410 | |
| 1411 | leaf = path->nodes[0]; |
| 1412 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1413 | |
| 1414 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 1415 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1416 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1417 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1418 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1419 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
| 1420 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
| 1421 | btrfs_mark_buffer_dirty(leaf); |
| 1422 | |
| 1423 | out: |
| 1424 | btrfs_free_path(path); |
| 1425 | return ret; |
| 1426 | } |
| 1427 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1428 | static int __btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1429 | struct btrfs_device *device, u64 new_size) |
| 1430 | { |
| 1431 | struct btrfs_super_block *super_copy = |
| 1432 | &device->dev_root->fs_info->super_copy; |
| 1433 | u64 old_total = btrfs_super_total_bytes(super_copy); |
| 1434 | u64 diff = new_size - device->total_bytes; |
| 1435 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1436 | if (!device->writeable) |
| 1437 | return -EACCES; |
| 1438 | if (new_size <= device->total_bytes) |
| 1439 | return -EINVAL; |
| 1440 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1441 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1442 | device->fs_devices->total_rw_bytes += diff; |
| 1443 | |
| 1444 | device->total_bytes = new_size; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1445 | return btrfs_update_device(trans, device); |
| 1446 | } |
| 1447 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1448 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
| 1449 | struct btrfs_device *device, u64 new_size) |
| 1450 | { |
| 1451 | int ret; |
| 1452 | lock_chunks(device->dev_root); |
| 1453 | ret = __btrfs_grow_device(trans, device, new_size); |
| 1454 | unlock_chunks(device->dev_root); |
| 1455 | return ret; |
| 1456 | } |
| 1457 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1458 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
| 1459 | struct btrfs_root *root, |
| 1460 | u64 chunk_tree, u64 chunk_objectid, |
| 1461 | u64 chunk_offset) |
| 1462 | { |
| 1463 | int ret; |
| 1464 | struct btrfs_path *path; |
| 1465 | struct btrfs_key key; |
| 1466 | |
| 1467 | root = root->fs_info->chunk_root; |
| 1468 | path = btrfs_alloc_path(); |
| 1469 | if (!path) |
| 1470 | return -ENOMEM; |
| 1471 | |
| 1472 | key.objectid = chunk_objectid; |
| 1473 | key.offset = chunk_offset; |
| 1474 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 1475 | |
| 1476 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1477 | BUG_ON(ret); |
| 1478 | |
| 1479 | ret = btrfs_del_item(trans, root, path); |
| 1480 | BUG_ON(ret); |
| 1481 | |
| 1482 | btrfs_free_path(path); |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1486 | static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64 |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1487 | chunk_offset) |
| 1488 | { |
| 1489 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; |
| 1490 | struct btrfs_disk_key *disk_key; |
| 1491 | struct btrfs_chunk *chunk; |
| 1492 | u8 *ptr; |
| 1493 | int ret = 0; |
| 1494 | u32 num_stripes; |
| 1495 | u32 array_size; |
| 1496 | u32 len = 0; |
| 1497 | u32 cur; |
| 1498 | struct btrfs_key key; |
| 1499 | |
| 1500 | array_size = btrfs_super_sys_array_size(super_copy); |
| 1501 | |
| 1502 | ptr = super_copy->sys_chunk_array; |
| 1503 | cur = 0; |
| 1504 | |
| 1505 | while (cur < array_size) { |
| 1506 | disk_key = (struct btrfs_disk_key *)ptr; |
| 1507 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 1508 | |
| 1509 | len = sizeof(*disk_key); |
| 1510 | |
| 1511 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 1512 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 1513 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 1514 | len += btrfs_chunk_item_size(num_stripes); |
| 1515 | } else { |
| 1516 | ret = -EIO; |
| 1517 | break; |
| 1518 | } |
| 1519 | if (key.objectid == chunk_objectid && |
| 1520 | key.offset == chunk_offset) { |
| 1521 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 1522 | array_size -= len; |
| 1523 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 1524 | } else { |
| 1525 | ptr += len; |
| 1526 | cur += len; |
| 1527 | } |
| 1528 | } |
| 1529 | return ret; |
| 1530 | } |
| 1531 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1532 | static int btrfs_relocate_chunk(struct btrfs_root *root, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1533 | u64 chunk_tree, u64 chunk_objectid, |
| 1534 | u64 chunk_offset) |
| 1535 | { |
| 1536 | struct extent_map_tree *em_tree; |
| 1537 | struct btrfs_root *extent_root; |
| 1538 | struct btrfs_trans_handle *trans; |
| 1539 | struct extent_map *em; |
| 1540 | struct map_lookup *map; |
| 1541 | int ret; |
| 1542 | int i; |
| 1543 | |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 1544 | printk("btrfs relocating chunk %llu\n", |
| 1545 | (unsigned long long)chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1546 | root = root->fs_info->chunk_root; |
| 1547 | extent_root = root->fs_info->extent_root; |
| 1548 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 1549 | |
| 1550 | /* step one, relocate all the extents inside this chunk */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 1551 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1552 | BUG_ON(ret); |
| 1553 | |
| 1554 | trans = btrfs_start_transaction(root, 1); |
| 1555 | BUG_ON(!trans); |
| 1556 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1557 | lock_chunks(root); |
| 1558 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1559 | /* |
| 1560 | * step two, delete the device extents and the |
| 1561 | * chunk tree entries |
| 1562 | */ |
| 1563 | spin_lock(&em_tree->lock); |
| 1564 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 1565 | spin_unlock(&em_tree->lock); |
| 1566 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1567 | BUG_ON(em->start > chunk_offset || |
| 1568 | em->start + em->len < chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1569 | map = (struct map_lookup *)em->bdev; |
| 1570 | |
| 1571 | for (i = 0; i < map->num_stripes; i++) { |
| 1572 | ret = btrfs_free_dev_extent(trans, map->stripes[i].dev, |
| 1573 | map->stripes[i].physical); |
| 1574 | BUG_ON(ret); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1575 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1576 | if (map->stripes[i].dev) { |
| 1577 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 1578 | BUG_ON(ret); |
| 1579 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1580 | } |
| 1581 | ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid, |
| 1582 | chunk_offset); |
| 1583 | |
| 1584 | BUG_ON(ret); |
| 1585 | |
| 1586 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 1587 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 1588 | BUG_ON(ret); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1589 | } |
| 1590 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 1591 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset); |
| 1592 | BUG_ON(ret); |
| 1593 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1594 | spin_lock(&em_tree->lock); |
| 1595 | remove_extent_mapping(em_tree, em); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 1596 | spin_unlock(&em_tree->lock); |
| 1597 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1598 | kfree(map); |
| 1599 | em->bdev = NULL; |
| 1600 | |
| 1601 | /* once for the tree */ |
| 1602 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1603 | /* once for us */ |
| 1604 | free_extent_map(em); |
| 1605 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1606 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1607 | btrfs_end_transaction(trans, root); |
| 1608 | return 0; |
| 1609 | } |
| 1610 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1611 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 1612 | { |
| 1613 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 1614 | struct btrfs_path *path; |
| 1615 | struct extent_buffer *leaf; |
| 1616 | struct btrfs_chunk *chunk; |
| 1617 | struct btrfs_key key; |
| 1618 | struct btrfs_key found_key; |
| 1619 | u64 chunk_tree = chunk_root->root_key.objectid; |
| 1620 | u64 chunk_type; |
| 1621 | int ret; |
| 1622 | |
| 1623 | path = btrfs_alloc_path(); |
| 1624 | if (!path) |
| 1625 | return -ENOMEM; |
| 1626 | |
| 1627 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 1628 | key.offset = (u64)-1; |
| 1629 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 1630 | |
| 1631 | while (1) { |
| 1632 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 1633 | if (ret < 0) |
| 1634 | goto error; |
| 1635 | BUG_ON(ret == 0); |
| 1636 | |
| 1637 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 1638 | key.type); |
| 1639 | if (ret < 0) |
| 1640 | goto error; |
| 1641 | if (ret > 0) |
| 1642 | break; |
| 1643 | |
| 1644 | leaf = path->nodes[0]; |
| 1645 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1646 | |
| 1647 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 1648 | struct btrfs_chunk); |
| 1649 | chunk_type = btrfs_chunk_type(leaf, chunk); |
| 1650 | btrfs_release_path(chunk_root, path); |
| 1651 | |
| 1652 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 1653 | ret = btrfs_relocate_chunk(chunk_root, chunk_tree, |
| 1654 | found_key.objectid, |
| 1655 | found_key.offset); |
| 1656 | BUG_ON(ret); |
| 1657 | } |
| 1658 | |
| 1659 | if (found_key.offset == 0) |
| 1660 | break; |
| 1661 | key.offset = found_key.offset - 1; |
| 1662 | } |
| 1663 | ret = 0; |
| 1664 | error: |
| 1665 | btrfs_free_path(path); |
| 1666 | return ret; |
| 1667 | } |
| 1668 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1669 | static u64 div_factor(u64 num, int factor) |
| 1670 | { |
| 1671 | if (factor == 10) |
| 1672 | return num; |
| 1673 | num *= factor; |
| 1674 | do_div(num, 10); |
| 1675 | return num; |
| 1676 | } |
| 1677 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1678 | int btrfs_balance(struct btrfs_root *dev_root) |
| 1679 | { |
| 1680 | int ret; |
| 1681 | struct list_head *cur; |
| 1682 | struct list_head *devices = &dev_root->fs_info->fs_devices->devices; |
| 1683 | struct btrfs_device *device; |
| 1684 | u64 old_size; |
| 1685 | u64 size_to_free; |
| 1686 | struct btrfs_path *path; |
| 1687 | struct btrfs_key key; |
| 1688 | struct btrfs_chunk *chunk; |
| 1689 | struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root; |
| 1690 | struct btrfs_trans_handle *trans; |
| 1691 | struct btrfs_key found_key; |
| 1692 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1693 | if (dev_root->fs_info->sb->s_flags & MS_RDONLY) |
| 1694 | return -EROFS; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1695 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1696 | mutex_lock(&dev_root->fs_info->volume_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1697 | dev_root = dev_root->fs_info->dev_root; |
| 1698 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1699 | /* step one make some room on all the devices */ |
| 1700 | list_for_each(cur, devices) { |
| 1701 | device = list_entry(cur, struct btrfs_device, dev_list); |
| 1702 | old_size = device->total_bytes; |
| 1703 | size_to_free = div_factor(old_size, 1); |
| 1704 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1705 | if (!device->writeable || |
| 1706 | device->total_bytes - device->bytes_used > size_to_free) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1707 | continue; |
| 1708 | |
| 1709 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
| 1710 | BUG_ON(ret); |
| 1711 | |
| 1712 | trans = btrfs_start_transaction(dev_root, 1); |
| 1713 | BUG_ON(!trans); |
| 1714 | |
| 1715 | ret = btrfs_grow_device(trans, device, old_size); |
| 1716 | BUG_ON(ret); |
| 1717 | |
| 1718 | btrfs_end_transaction(trans, dev_root); |
| 1719 | } |
| 1720 | |
| 1721 | /* step two, relocate all the chunks */ |
| 1722 | path = btrfs_alloc_path(); |
| 1723 | BUG_ON(!path); |
| 1724 | |
| 1725 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 1726 | key.offset = (u64)-1; |
| 1727 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 1728 | |
| 1729 | while(1) { |
| 1730 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 1731 | if (ret < 0) |
| 1732 | goto error; |
| 1733 | |
| 1734 | /* |
| 1735 | * this shouldn't happen, it means the last relocate |
| 1736 | * failed |
| 1737 | */ |
| 1738 | if (ret == 0) |
| 1739 | break; |
| 1740 | |
| 1741 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 1742 | BTRFS_CHUNK_ITEM_KEY); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1743 | if (ret) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1744 | break; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1745 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1746 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1747 | path->slots[0]); |
| 1748 | if (found_key.objectid != key.objectid) |
| 1749 | break; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1750 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1751 | chunk = btrfs_item_ptr(path->nodes[0], |
| 1752 | path->slots[0], |
| 1753 | struct btrfs_chunk); |
| 1754 | key.offset = found_key.offset; |
| 1755 | /* chunk zero is special */ |
| 1756 | if (key.offset == 0) |
| 1757 | break; |
| 1758 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1759 | btrfs_release_path(chunk_root, path); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1760 | ret = btrfs_relocate_chunk(chunk_root, |
| 1761 | chunk_root->root_key.objectid, |
| 1762 | found_key.objectid, |
| 1763 | found_key.offset); |
| 1764 | BUG_ON(ret); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1765 | } |
| 1766 | ret = 0; |
| 1767 | error: |
| 1768 | btrfs_free_path(path); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1769 | mutex_unlock(&dev_root->fs_info->volume_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1770 | return ret; |
| 1771 | } |
| 1772 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1773 | /* |
| 1774 | * shrinking a device means finding all of the device extents past |
| 1775 | * the new size, and then following the back refs to the chunks. |
| 1776 | * The chunk relocation code actually frees the device extent |
| 1777 | */ |
| 1778 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 1779 | { |
| 1780 | struct btrfs_trans_handle *trans; |
| 1781 | struct btrfs_root *root = device->dev_root; |
| 1782 | struct btrfs_dev_extent *dev_extent = NULL; |
| 1783 | struct btrfs_path *path; |
| 1784 | u64 length; |
| 1785 | u64 chunk_tree; |
| 1786 | u64 chunk_objectid; |
| 1787 | u64 chunk_offset; |
| 1788 | int ret; |
| 1789 | int slot; |
| 1790 | struct extent_buffer *l; |
| 1791 | struct btrfs_key key; |
| 1792 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; |
| 1793 | u64 old_total = btrfs_super_total_bytes(super_copy); |
| 1794 | u64 diff = device->total_bytes - new_size; |
| 1795 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1796 | if (new_size >= device->total_bytes) |
| 1797 | return -EINVAL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1798 | |
| 1799 | path = btrfs_alloc_path(); |
| 1800 | if (!path) |
| 1801 | return -ENOMEM; |
| 1802 | |
| 1803 | trans = btrfs_start_transaction(root, 1); |
| 1804 | if (!trans) { |
| 1805 | ret = -ENOMEM; |
| 1806 | goto done; |
| 1807 | } |
| 1808 | |
| 1809 | path->reada = 2; |
| 1810 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1811 | lock_chunks(root); |
| 1812 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1813 | device->total_bytes = new_size; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1814 | if (device->writeable) |
| 1815 | device->fs_devices->total_rw_bytes -= diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1816 | ret = btrfs_update_device(trans, device); |
| 1817 | if (ret) { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1818 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1819 | btrfs_end_transaction(trans, root); |
| 1820 | goto done; |
| 1821 | } |
| 1822 | WARN_ON(diff > old_total); |
| 1823 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1824 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1825 | btrfs_end_transaction(trans, root); |
| 1826 | |
| 1827 | key.objectid = device->devid; |
| 1828 | key.offset = (u64)-1; |
| 1829 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1830 | |
| 1831 | while (1) { |
| 1832 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1833 | if (ret < 0) |
| 1834 | goto done; |
| 1835 | |
| 1836 | ret = btrfs_previous_item(root, path, 0, key.type); |
| 1837 | if (ret < 0) |
| 1838 | goto done; |
| 1839 | if (ret) { |
| 1840 | ret = 0; |
| 1841 | goto done; |
| 1842 | } |
| 1843 | |
| 1844 | l = path->nodes[0]; |
| 1845 | slot = path->slots[0]; |
| 1846 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 1847 | |
| 1848 | if (key.objectid != device->devid) |
| 1849 | goto done; |
| 1850 | |
| 1851 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 1852 | length = btrfs_dev_extent_length(l, dev_extent); |
| 1853 | |
| 1854 | if (key.offset + length <= new_size) |
| 1855 | goto done; |
| 1856 | |
| 1857 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); |
| 1858 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); |
| 1859 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
| 1860 | btrfs_release_path(root, path); |
| 1861 | |
| 1862 | ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid, |
| 1863 | chunk_offset); |
| 1864 | if (ret) |
| 1865 | goto done; |
| 1866 | } |
| 1867 | |
| 1868 | done: |
| 1869 | btrfs_free_path(path); |
| 1870 | return ret; |
| 1871 | } |
| 1872 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1873 | static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1874 | struct btrfs_root *root, |
| 1875 | struct btrfs_key *key, |
| 1876 | struct btrfs_chunk *chunk, int item_size) |
| 1877 | { |
| 1878 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; |
| 1879 | struct btrfs_disk_key disk_key; |
| 1880 | u32 array_size; |
| 1881 | u8 *ptr; |
| 1882 | |
| 1883 | array_size = btrfs_super_sys_array_size(super_copy); |
| 1884 | if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) |
| 1885 | return -EFBIG; |
| 1886 | |
| 1887 | ptr = super_copy->sys_chunk_array + array_size; |
| 1888 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 1889 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 1890 | ptr += sizeof(disk_key); |
| 1891 | memcpy(ptr, chunk, item_size); |
| 1892 | item_size += sizeof(disk_key); |
| 1893 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
| 1894 | return 0; |
| 1895 | } |
| 1896 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 1897 | static u64 noinline chunk_bytes_by_type(u64 type, u64 calc_size, |
| 1898 | int num_stripes, int sub_stripes) |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1899 | { |
| 1900 | if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP)) |
| 1901 | return calc_size; |
| 1902 | else if (type & BTRFS_BLOCK_GROUP_RAID10) |
| 1903 | return calc_size * (num_stripes / sub_stripes); |
| 1904 | else |
| 1905 | return calc_size * num_stripes; |
| 1906 | } |
| 1907 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1908 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 1909 | struct btrfs_root *extent_root, |
| 1910 | struct map_lookup **map_ret, |
| 1911 | u64 *num_bytes, u64 *stripe_size, |
| 1912 | u64 start, u64 type) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1913 | { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1914 | struct btrfs_fs_info *info = extent_root->fs_info; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1915 | struct btrfs_device *device = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1916 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1917 | struct list_head *cur; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1918 | struct map_lookup *map = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1919 | struct extent_map_tree *em_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1920 | struct extent_map *em; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1921 | struct list_head private_devs; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1922 | int min_stripe_size = 1 * 1024 * 1024; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1923 | u64 calc_size = 1024 * 1024 * 1024; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1924 | u64 max_chunk_size = calc_size; |
| 1925 | u64 min_free; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1926 | u64 avail; |
| 1927 | u64 max_avail = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1928 | u64 dev_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1929 | int num_stripes = 1; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1930 | int min_stripes = 1; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1931 | int sub_stripes = 0; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1932 | int looped = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1933 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1934 | int index; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1935 | int stripe_len = 64 * 1024; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1936 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1937 | if ((type & BTRFS_BLOCK_GROUP_RAID1) && |
| 1938 | (type & BTRFS_BLOCK_GROUP_DUP)) { |
| 1939 | WARN_ON(1); |
| 1940 | type &= ~BTRFS_BLOCK_GROUP_DUP; |
| 1941 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1942 | if (list_empty(&fs_devices->alloc_list)) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1943 | return -ENOSPC; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1944 | |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1945 | if (type & (BTRFS_BLOCK_GROUP_RAID0)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1946 | num_stripes = fs_devices->rw_devices; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1947 | min_stripes = 2; |
| 1948 | } |
| 1949 | if (type & (BTRFS_BLOCK_GROUP_DUP)) { |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1950 | num_stripes = 2; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1951 | min_stripes = 2; |
| 1952 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1953 | if (type & (BTRFS_BLOCK_GROUP_RAID1)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1954 | num_stripes = min_t(u64, 2, fs_devices->rw_devices); |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1955 | if (num_stripes < 2) |
| 1956 | return -ENOSPC; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1957 | min_stripes = 2; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1958 | } |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1959 | if (type & (BTRFS_BLOCK_GROUP_RAID10)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1960 | num_stripes = fs_devices->rw_devices; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1961 | if (num_stripes < 4) |
| 1962 | return -ENOSPC; |
| 1963 | num_stripes &= ~(u32)1; |
| 1964 | sub_stripes = 2; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1965 | min_stripes = 4; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1966 | } |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1967 | |
| 1968 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 1969 | max_chunk_size = 10 * calc_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1970 | min_stripe_size = 64 * 1024 * 1024; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1971 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
| 1972 | max_chunk_size = 4 * calc_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1973 | min_stripe_size = 32 * 1024 * 1024; |
| 1974 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 1975 | calc_size = 8 * 1024 * 1024; |
| 1976 | max_chunk_size = calc_size * 2; |
| 1977 | min_stripe_size = 1 * 1024 * 1024; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1978 | } |
| 1979 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1980 | /* we don't want a chunk larger than 10% of writeable space */ |
| 1981 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 1982 | max_chunk_size); |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1983 | |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 1984 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1985 | if (!map || map->num_stripes != num_stripes) { |
| 1986 | kfree(map); |
| 1987 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 1988 | if (!map) |
| 1989 | return -ENOMEM; |
| 1990 | map->num_stripes = num_stripes; |
| 1991 | } |
| 1992 | |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 1993 | if (calc_size * num_stripes > max_chunk_size) { |
| 1994 | calc_size = max_chunk_size; |
| 1995 | do_div(calc_size, num_stripes); |
| 1996 | do_div(calc_size, stripe_len); |
| 1997 | calc_size *= stripe_len; |
| 1998 | } |
| 1999 | /* we don't want tiny stripes */ |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 2000 | calc_size = max_t(u64, min_stripe_size, calc_size); |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 2001 | |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 2002 | do_div(calc_size, stripe_len); |
| 2003 | calc_size *= stripe_len; |
| 2004 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2005 | cur = fs_devices->alloc_list.next; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2006 | index = 0; |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2007 | |
| 2008 | if (type & BTRFS_BLOCK_GROUP_DUP) |
| 2009 | min_free = calc_size * 2; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 2010 | else |
| 2011 | min_free = calc_size; |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2012 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2013 | /* |
| 2014 | * we add 1MB because we never use the first 1MB of the device, unless |
| 2015 | * we've looped, then we are likely allocating the maximum amount of |
| 2016 | * space left already |
| 2017 | */ |
| 2018 | if (!looped) |
| 2019 | min_free += 1024 * 1024; |
Chris Mason | ad5bd91 | 2008-04-21 08:28:10 -0400 | [diff] [blame] | 2020 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2021 | INIT_LIST_HEAD(&private_devs); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2022 | while(index < num_stripes) { |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 2023 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2024 | BUG_ON(!device->writeable); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2025 | if (device->total_bytes > device->bytes_used) |
| 2026 | avail = device->total_bytes - device->bytes_used; |
| 2027 | else |
| 2028 | avail = 0; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2029 | cur = cur->next; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2030 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2031 | if (device->in_fs_metadata && avail >= min_free) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2032 | ret = find_free_dev_extent(trans, device, |
| 2033 | min_free, &dev_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2034 | if (ret == 0) { |
| 2035 | list_move_tail(&device->dev_alloc_list, |
| 2036 | &private_devs); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2037 | map->stripes[index].dev = device; |
| 2038 | map->stripes[index].physical = dev_offset; |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2039 | index++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2040 | if (type & BTRFS_BLOCK_GROUP_DUP) { |
| 2041 | map->stripes[index].dev = device; |
| 2042 | map->stripes[index].physical = |
| 2043 | dev_offset + calc_size; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2044 | index++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2045 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2046 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2047 | } else if (device->in_fs_metadata && avail > max_avail) |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 2048 | max_avail = avail; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2049 | if (cur == &fs_devices->alloc_list) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2050 | break; |
| 2051 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2052 | list_splice(&private_devs, &fs_devices->alloc_list); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2053 | if (index < num_stripes) { |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 2054 | if (index >= min_stripes) { |
| 2055 | num_stripes = index; |
| 2056 | if (type & (BTRFS_BLOCK_GROUP_RAID10)) { |
| 2057 | num_stripes /= sub_stripes; |
| 2058 | num_stripes *= sub_stripes; |
| 2059 | } |
| 2060 | looped = 1; |
| 2061 | goto again; |
| 2062 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2063 | if (!looped && max_avail > 0) { |
| 2064 | looped = 1; |
| 2065 | calc_size = max_avail; |
| 2066 | goto again; |
| 2067 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2068 | kfree(map); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2069 | return -ENOSPC; |
| 2070 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2071 | map->sector_size = extent_root->sectorsize; |
| 2072 | map->stripe_len = stripe_len; |
| 2073 | map->io_align = stripe_len; |
| 2074 | map->io_width = stripe_len; |
| 2075 | map->type = type; |
| 2076 | map->num_stripes = num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2077 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2078 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2079 | *map_ret = map; |
| 2080 | *stripe_size = calc_size; |
| 2081 | *num_bytes = chunk_bytes_by_type(type, calc_size, |
| 2082 | num_stripes, sub_stripes); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2083 | |
| 2084 | em = alloc_extent_map(GFP_NOFS); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2085 | if (!em) { |
| 2086 | kfree(map); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2087 | return -ENOMEM; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2088 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2089 | em->bdev = (struct block_device *)map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2090 | em->start = start; |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 2091 | em->len = *num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2092 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2093 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2094 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2095 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
| 2096 | spin_lock(&em_tree->lock); |
| 2097 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2098 | spin_unlock(&em_tree->lock); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 2099 | BUG_ON(ret); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2100 | free_extent_map(em); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2101 | |
| 2102 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 2103 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 2104 | start, *num_bytes); |
| 2105 | BUG_ON(ret); |
| 2106 | |
| 2107 | index = 0; |
| 2108 | while (index < map->num_stripes) { |
| 2109 | device = map->stripes[index].dev; |
| 2110 | dev_offset = map->stripes[index].physical; |
| 2111 | |
| 2112 | ret = btrfs_alloc_dev_extent(trans, device, |
| 2113 | info->chunk_root->root_key.objectid, |
| 2114 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 2115 | start, dev_offset, calc_size); |
| 2116 | BUG_ON(ret); |
| 2117 | index++; |
| 2118 | } |
| 2119 | |
| 2120 | return 0; |
| 2121 | } |
| 2122 | |
| 2123 | static int __finish_chunk_alloc(struct btrfs_trans_handle *trans, |
| 2124 | struct btrfs_root *extent_root, |
| 2125 | struct map_lookup *map, u64 chunk_offset, |
| 2126 | u64 chunk_size, u64 stripe_size) |
| 2127 | { |
| 2128 | u64 dev_offset; |
| 2129 | struct btrfs_key key; |
| 2130 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 2131 | struct btrfs_device *device; |
| 2132 | struct btrfs_chunk *chunk; |
| 2133 | struct btrfs_stripe *stripe; |
| 2134 | size_t item_size = btrfs_chunk_item_size(map->num_stripes); |
| 2135 | int index = 0; |
| 2136 | int ret; |
| 2137 | |
| 2138 | chunk = kzalloc(item_size, GFP_NOFS); |
| 2139 | if (!chunk) |
| 2140 | return -ENOMEM; |
| 2141 | |
| 2142 | index = 0; |
| 2143 | while (index < map->num_stripes) { |
| 2144 | device = map->stripes[index].dev; |
| 2145 | device->bytes_used += stripe_size; |
| 2146 | ret = btrfs_update_device(trans, device); |
| 2147 | BUG_ON(ret); |
| 2148 | index++; |
| 2149 | } |
| 2150 | |
| 2151 | index = 0; |
| 2152 | stripe = &chunk->stripe; |
| 2153 | while (index < map->num_stripes) { |
| 2154 | device = map->stripes[index].dev; |
| 2155 | dev_offset = map->stripes[index].physical; |
| 2156 | |
| 2157 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 2158 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 2159 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 2160 | stripe++; |
| 2161 | index++; |
| 2162 | } |
| 2163 | |
| 2164 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 2165 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 2166 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 2167 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 2168 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 2169 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 2170 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 2171 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 2172 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 2173 | |
| 2174 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2175 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2176 | key.offset = chunk_offset; |
| 2177 | |
| 2178 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
| 2179 | BUG_ON(ret); |
| 2180 | |
| 2181 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2182 | ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk, |
| 2183 | item_size); |
| 2184 | BUG_ON(ret); |
| 2185 | } |
| 2186 | kfree(chunk); |
| 2187 | return 0; |
| 2188 | } |
| 2189 | |
| 2190 | /* |
| 2191 | * Chunk allocation falls into two parts. The first part does works |
| 2192 | * that make the new allocated chunk useable, but not do any operation |
| 2193 | * that modifies the chunk tree. The second part does the works that |
| 2194 | * require modifying the chunk tree. This division is important for the |
| 2195 | * bootstrap process of adding storage to a seed btrfs. |
| 2196 | */ |
| 2197 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 2198 | struct btrfs_root *extent_root, u64 type) |
| 2199 | { |
| 2200 | u64 chunk_offset; |
| 2201 | u64 chunk_size; |
| 2202 | u64 stripe_size; |
| 2203 | struct map_lookup *map; |
| 2204 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 2205 | int ret; |
| 2206 | |
| 2207 | ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 2208 | &chunk_offset); |
| 2209 | if (ret) |
| 2210 | return ret; |
| 2211 | |
| 2212 | ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size, |
| 2213 | &stripe_size, chunk_offset, type); |
| 2214 | if (ret) |
| 2215 | return ret; |
| 2216 | |
| 2217 | ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, |
| 2218 | chunk_size, stripe_size); |
| 2219 | BUG_ON(ret); |
| 2220 | return 0; |
| 2221 | } |
| 2222 | |
| 2223 | static int noinline init_first_rw_device(struct btrfs_trans_handle *trans, |
| 2224 | struct btrfs_root *root, |
| 2225 | struct btrfs_device *device) |
| 2226 | { |
| 2227 | u64 chunk_offset; |
| 2228 | u64 sys_chunk_offset; |
| 2229 | u64 chunk_size; |
| 2230 | u64 sys_chunk_size; |
| 2231 | u64 stripe_size; |
| 2232 | u64 sys_stripe_size; |
| 2233 | u64 alloc_profile; |
| 2234 | struct map_lookup *map; |
| 2235 | struct map_lookup *sys_map; |
| 2236 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2237 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 2238 | int ret; |
| 2239 | |
| 2240 | ret = find_next_chunk(fs_info->chunk_root, |
| 2241 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset); |
| 2242 | BUG_ON(ret); |
| 2243 | |
| 2244 | alloc_profile = BTRFS_BLOCK_GROUP_METADATA | |
| 2245 | (fs_info->metadata_alloc_profile & |
| 2246 | fs_info->avail_metadata_alloc_bits); |
| 2247 | alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile); |
| 2248 | |
| 2249 | ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size, |
| 2250 | &stripe_size, chunk_offset, alloc_profile); |
| 2251 | BUG_ON(ret); |
| 2252 | |
| 2253 | sys_chunk_offset = chunk_offset + chunk_size; |
| 2254 | |
| 2255 | alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM | |
| 2256 | (fs_info->system_alloc_profile & |
| 2257 | fs_info->avail_system_alloc_bits); |
| 2258 | alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile); |
| 2259 | |
| 2260 | ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map, |
| 2261 | &sys_chunk_size, &sys_stripe_size, |
| 2262 | sys_chunk_offset, alloc_profile); |
| 2263 | BUG_ON(ret); |
| 2264 | |
| 2265 | ret = btrfs_add_device(trans, fs_info->chunk_root, device); |
| 2266 | BUG_ON(ret); |
| 2267 | |
| 2268 | /* |
| 2269 | * Modifying chunk tree needs allocating new blocks from both |
| 2270 | * system block group and metadata block group. So we only can |
| 2271 | * do operations require modifying the chunk tree after both |
| 2272 | * block groups were created. |
| 2273 | */ |
| 2274 | ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, |
| 2275 | chunk_size, stripe_size); |
| 2276 | BUG_ON(ret); |
| 2277 | |
| 2278 | ret = __finish_chunk_alloc(trans, extent_root, sys_map, |
| 2279 | sys_chunk_offset, sys_chunk_size, |
| 2280 | sys_stripe_size); |
| 2281 | BUG_ON(ret); |
| 2282 | return 0; |
| 2283 | } |
| 2284 | |
| 2285 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 2286 | { |
| 2287 | struct extent_map *em; |
| 2288 | struct map_lookup *map; |
| 2289 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 2290 | int readonly = 0; |
| 2291 | int i; |
| 2292 | |
| 2293 | spin_lock(&map_tree->map_tree.lock); |
| 2294 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
| 2295 | spin_unlock(&map_tree->map_tree.lock); |
| 2296 | if (!em) |
| 2297 | return 1; |
| 2298 | |
| 2299 | map = (struct map_lookup *)em->bdev; |
| 2300 | for (i = 0; i < map->num_stripes; i++) { |
| 2301 | if (!map->stripes[i].dev->writeable) { |
| 2302 | readonly = 1; |
| 2303 | break; |
| 2304 | } |
| 2305 | } |
| 2306 | free_extent_map(em); |
| 2307 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2308 | } |
| 2309 | |
| 2310 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 2311 | { |
| 2312 | extent_map_tree_init(&tree->map_tree, GFP_NOFS); |
| 2313 | } |
| 2314 | |
| 2315 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 2316 | { |
| 2317 | struct extent_map *em; |
| 2318 | |
| 2319 | while(1) { |
| 2320 | spin_lock(&tree->map_tree.lock); |
| 2321 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 2322 | if (em) |
| 2323 | remove_extent_mapping(&tree->map_tree, em); |
| 2324 | spin_unlock(&tree->map_tree.lock); |
| 2325 | if (!em) |
| 2326 | break; |
| 2327 | kfree(em->bdev); |
| 2328 | /* once for us */ |
| 2329 | free_extent_map(em); |
| 2330 | /* once for the tree */ |
| 2331 | free_extent_map(em); |
| 2332 | } |
| 2333 | } |
| 2334 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2335 | int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len) |
| 2336 | { |
| 2337 | struct extent_map *em; |
| 2338 | struct map_lookup *map; |
| 2339 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 2340 | int ret; |
| 2341 | |
| 2342 | spin_lock(&em_tree->lock); |
| 2343 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 2344 | spin_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2345 | BUG_ON(!em); |
| 2346 | |
| 2347 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 2348 | map = (struct map_lookup *)em->bdev; |
| 2349 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 2350 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2351 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 2352 | ret = map->sub_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2353 | else |
| 2354 | ret = 1; |
| 2355 | free_extent_map(em); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2356 | return ret; |
| 2357 | } |
| 2358 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2359 | static int find_live_mirror(struct map_lookup *map, int first, int num, |
| 2360 | int optimal) |
| 2361 | { |
| 2362 | int i; |
| 2363 | if (map->stripes[optimal].dev->bdev) |
| 2364 | return optimal; |
| 2365 | for (i = first; i < first + num; i++) { |
| 2366 | if (map->stripes[i].dev->bdev) |
| 2367 | return i; |
| 2368 | } |
| 2369 | /* we couldn't find one that doesn't fail. Just return something |
| 2370 | * and the io error handling code will clean up eventually |
| 2371 | */ |
| 2372 | return optimal; |
| 2373 | } |
| 2374 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2375 | static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, |
| 2376 | u64 logical, u64 *length, |
| 2377 | struct btrfs_multi_bio **multi_ret, |
| 2378 | int mirror_num, struct page *unplug_page) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2379 | { |
| 2380 | struct extent_map *em; |
| 2381 | struct map_lookup *map; |
| 2382 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 2383 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2384 | u64 stripe_offset; |
| 2385 | u64 stripe_nr; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2386 | int stripes_allocated = 8; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2387 | int stripes_required = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2388 | int stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2389 | int i; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2390 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 2391 | int max_errors = 0; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2392 | struct btrfs_multi_bio *multi = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2393 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2394 | if (multi_ret && !(rw & (1 << BIO_RW))) { |
| 2395 | stripes_allocated = 1; |
| 2396 | } |
| 2397 | again: |
| 2398 | if (multi_ret) { |
| 2399 | multi = kzalloc(btrfs_multi_bio_size(stripes_allocated), |
| 2400 | GFP_NOFS); |
| 2401 | if (!multi) |
| 2402 | return -ENOMEM; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 2403 | |
| 2404 | atomic_set(&multi->error, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2405 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2406 | |
| 2407 | spin_lock(&em_tree->lock); |
| 2408 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 2409 | spin_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2410 | |
| 2411 | if (!em && unplug_page) |
| 2412 | return 0; |
| 2413 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2414 | if (!em) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2415 | printk("unable to find logical %Lu len %Lu\n", logical, *length); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2416 | BUG(); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2417 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2418 | |
| 2419 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 2420 | map = (struct map_lookup *)em->bdev; |
| 2421 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2422 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2423 | if (mirror_num > map->num_stripes) |
| 2424 | mirror_num = 0; |
| 2425 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2426 | /* 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] | 2427 | if (rw & (1 << BIO_RW)) { |
| 2428 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 2429 | BTRFS_BLOCK_GROUP_DUP)) { |
| 2430 | stripes_required = map->num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 2431 | max_errors = 1; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2432 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 2433 | stripes_required = map->sub_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 2434 | max_errors = 1; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2435 | } |
| 2436 | } |
| 2437 | if (multi_ret && rw == WRITE && |
| 2438 | stripes_allocated < stripes_required) { |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2439 | stripes_allocated = map->num_stripes; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2440 | free_extent_map(em); |
| 2441 | kfree(multi); |
| 2442 | goto again; |
| 2443 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2444 | stripe_nr = offset; |
| 2445 | /* |
| 2446 | * stripe_nr counts the total number of stripes we have to stride |
| 2447 | * to get to this block |
| 2448 | */ |
| 2449 | do_div(stripe_nr, map->stripe_len); |
| 2450 | |
| 2451 | stripe_offset = stripe_nr * map->stripe_len; |
| 2452 | BUG_ON(offset < stripe_offset); |
| 2453 | |
| 2454 | /* stripe_offset is the offset of this block in its stripe*/ |
| 2455 | stripe_offset = offset - stripe_offset; |
| 2456 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2457 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2458 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2459 | BTRFS_BLOCK_GROUP_DUP)) { |
| 2460 | /* we limit the length of each bio to what fits in a stripe */ |
| 2461 | *length = min_t(u64, em->len - offset, |
| 2462 | map->stripe_len - stripe_offset); |
| 2463 | } else { |
| 2464 | *length = em->len - offset; |
| 2465 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2466 | |
| 2467 | if (!multi_ret && !unplug_page) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2468 | goto out; |
| 2469 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2470 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2471 | stripe_index = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2472 | if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2473 | if (unplug_page || (rw & (1 << BIO_RW))) |
| 2474 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 2475 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2476 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2477 | else { |
| 2478 | stripe_index = find_live_mirror(map, 0, |
| 2479 | map->num_stripes, |
| 2480 | current->pid % map->num_stripes); |
| 2481 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 2482 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2483 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2484 | if (rw & (1 << BIO_RW)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2485 | num_stripes = map->num_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2486 | else if (mirror_num) |
| 2487 | stripe_index = mirror_num - 1; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 2488 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2489 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 2490 | int factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2491 | |
| 2492 | stripe_index = do_div(stripe_nr, factor); |
| 2493 | stripe_index *= map->sub_stripes; |
| 2494 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2495 | if (unplug_page || (rw & (1 << BIO_RW))) |
| 2496 | num_stripes = map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2497 | else if (mirror_num) |
| 2498 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2499 | else { |
| 2500 | stripe_index = find_live_mirror(map, stripe_index, |
| 2501 | map->sub_stripes, stripe_index + |
| 2502 | current->pid % map->sub_stripes); |
| 2503 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2504 | } else { |
| 2505 | /* |
| 2506 | * after this do_div call, stripe_nr is the number of stripes |
| 2507 | * on this device we have to walk to find the data, and |
| 2508 | * stripe_index is the number of our device in the stripe array |
| 2509 | */ |
| 2510 | stripe_index = do_div(stripe_nr, map->num_stripes); |
| 2511 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2512 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2513 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2514 | for (i = 0; i < num_stripes; i++) { |
| 2515 | if (unplug_page) { |
| 2516 | struct btrfs_device *device; |
| 2517 | struct backing_dev_info *bdi; |
| 2518 | |
| 2519 | device = map->stripes[stripe_index].dev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2520 | if (device->bdev) { |
| 2521 | bdi = blk_get_backing_dev_info(device->bdev); |
| 2522 | if (bdi->unplug_io_fn) { |
| 2523 | bdi->unplug_io_fn(bdi, unplug_page); |
| 2524 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2525 | } |
| 2526 | } else { |
| 2527 | multi->stripes[i].physical = |
| 2528 | map->stripes[stripe_index].physical + |
| 2529 | stripe_offset + stripe_nr * map->stripe_len; |
| 2530 | multi->stripes[i].dev = map->stripes[stripe_index].dev; |
| 2531 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2532 | stripe_index++; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2533 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2534 | if (multi_ret) { |
| 2535 | *multi_ret = multi; |
| 2536 | multi->num_stripes = num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 2537 | multi->max_errors = max_errors; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2538 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2539 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2540 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2541 | return 0; |
| 2542 | } |
| 2543 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2544 | int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, |
| 2545 | u64 logical, u64 *length, |
| 2546 | struct btrfs_multi_bio **multi_ret, int mirror_num) |
| 2547 | { |
| 2548 | return __btrfs_map_block(map_tree, rw, logical, length, multi_ret, |
| 2549 | mirror_num, NULL); |
| 2550 | } |
| 2551 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 2552 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 2553 | u64 chunk_start, u64 physical, u64 devid, |
| 2554 | u64 **logical, int *naddrs, int *stripe_len) |
| 2555 | { |
| 2556 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 2557 | struct extent_map *em; |
| 2558 | struct map_lookup *map; |
| 2559 | u64 *buf; |
| 2560 | u64 bytenr; |
| 2561 | u64 length; |
| 2562 | u64 stripe_nr; |
| 2563 | int i, j, nr = 0; |
| 2564 | |
| 2565 | spin_lock(&em_tree->lock); |
| 2566 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
| 2567 | spin_unlock(&em_tree->lock); |
| 2568 | |
| 2569 | BUG_ON(!em || em->start != chunk_start); |
| 2570 | map = (struct map_lookup *)em->bdev; |
| 2571 | |
| 2572 | length = em->len; |
| 2573 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 2574 | do_div(length, map->num_stripes / map->sub_stripes); |
| 2575 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 2576 | do_div(length, map->num_stripes); |
| 2577 | |
| 2578 | buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS); |
| 2579 | BUG_ON(!buf); |
| 2580 | |
| 2581 | for (i = 0; i < map->num_stripes; i++) { |
| 2582 | if (devid && map->stripes[i].dev->devid != devid) |
| 2583 | continue; |
| 2584 | if (map->stripes[i].physical > physical || |
| 2585 | map->stripes[i].physical + length <= physical) |
| 2586 | continue; |
| 2587 | |
| 2588 | stripe_nr = physical - map->stripes[i].physical; |
| 2589 | do_div(stripe_nr, map->stripe_len); |
| 2590 | |
| 2591 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 2592 | stripe_nr = stripe_nr * map->num_stripes + i; |
| 2593 | do_div(stripe_nr, map->sub_stripes); |
| 2594 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 2595 | stripe_nr = stripe_nr * map->num_stripes + i; |
| 2596 | } |
| 2597 | bytenr = chunk_start + stripe_nr * map->stripe_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 2598 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 2599 | for (j = 0; j < nr; j++) { |
| 2600 | if (buf[j] == bytenr) |
| 2601 | break; |
| 2602 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 2603 | if (j == nr) { |
| 2604 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 2605 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 2606 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | for (i = 0; i > nr; i++) { |
| 2610 | struct btrfs_multi_bio *multi; |
| 2611 | struct btrfs_bio_stripe *stripe; |
| 2612 | int ret; |
| 2613 | |
| 2614 | length = 1; |
| 2615 | ret = btrfs_map_block(map_tree, WRITE, buf[i], |
| 2616 | &length, &multi, 0); |
| 2617 | BUG_ON(ret); |
| 2618 | |
| 2619 | stripe = multi->stripes; |
| 2620 | for (j = 0; j < multi->num_stripes; j++) { |
| 2621 | if (stripe->physical >= physical && |
| 2622 | physical < stripe->physical + length) |
| 2623 | break; |
| 2624 | } |
| 2625 | BUG_ON(j >= multi->num_stripes); |
| 2626 | kfree(multi); |
| 2627 | } |
| 2628 | |
| 2629 | *logical = buf; |
| 2630 | *naddrs = nr; |
| 2631 | *stripe_len = map->stripe_len; |
| 2632 | |
| 2633 | free_extent_map(em); |
| 2634 | return 0; |
| 2635 | } |
| 2636 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2637 | int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree, |
| 2638 | u64 logical, struct page *page) |
| 2639 | { |
| 2640 | u64 length = PAGE_CACHE_SIZE; |
| 2641 | return __btrfs_map_block(map_tree, READ, logical, &length, |
| 2642 | NULL, 0, page); |
| 2643 | } |
| 2644 | |
| 2645 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2646 | static void end_bio_multi_stripe(struct bio *bio, int err) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2647 | { |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2648 | struct btrfs_multi_bio *multi = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 2649 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2650 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2651 | if (err) |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 2652 | atomic_inc(&multi->error); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2653 | |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 2654 | if (bio == multi->orig_bio) |
| 2655 | is_orig_bio = 1; |
| 2656 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2657 | if (atomic_dec_and_test(&multi->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 2658 | if (!is_orig_bio) { |
| 2659 | bio_put(bio); |
| 2660 | bio = multi->orig_bio; |
| 2661 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2662 | bio->bi_private = multi->private; |
| 2663 | bio->bi_end_io = multi->end_io; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 2664 | /* only send an error to the higher layers if it is |
| 2665 | * beyond the tolerance of the multi-bio |
| 2666 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2667 | if (atomic_read(&multi->error) > multi->max_errors) { |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 2668 | err = -EIO; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2669 | } else if (err) { |
| 2670 | /* |
| 2671 | * this bio is actually up to date, we didn't |
| 2672 | * go over the max number of errors |
| 2673 | */ |
| 2674 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 2675 | err = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2676 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2677 | kfree(multi); |
| 2678 | |
| 2679 | bio_endio(bio, err); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 2680 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2681 | bio_put(bio); |
| 2682 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2683 | } |
| 2684 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 2685 | struct async_sched { |
| 2686 | struct bio *bio; |
| 2687 | int rw; |
| 2688 | struct btrfs_fs_info *info; |
| 2689 | struct btrfs_work work; |
| 2690 | }; |
| 2691 | |
| 2692 | /* |
| 2693 | * see run_scheduled_bios for a description of why bios are collected for |
| 2694 | * async submit. |
| 2695 | * |
| 2696 | * This will add one bio to the pending list for a device and make sure |
| 2697 | * the work struct is scheduled. |
| 2698 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 2699 | static int noinline schedule_bio(struct btrfs_root *root, |
| 2700 | struct btrfs_device *device, |
| 2701 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 2702 | { |
| 2703 | int should_queue = 1; |
| 2704 | |
| 2705 | /* don't bother with additional async steps for reads, right now */ |
| 2706 | if (!(rw & (1 << BIO_RW))) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 2707 | bio_get(bio); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 2708 | submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 2709 | bio_put(bio); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 2710 | return 0; |
| 2711 | } |
| 2712 | |
| 2713 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 2714 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 2715 | * higher layers. Otherwise, the async bio makes it appear we have |
| 2716 | * made progress against dirty pages when we've really just put it |
| 2717 | * on a queue for later |
| 2718 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 2719 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 2720 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 2721 | bio->bi_next = NULL; |
| 2722 | bio->bi_rw |= rw; |
| 2723 | |
| 2724 | spin_lock(&device->io_lock); |
| 2725 | |
| 2726 | if (device->pending_bio_tail) |
| 2727 | device->pending_bio_tail->bi_next = bio; |
| 2728 | |
| 2729 | device->pending_bio_tail = bio; |
| 2730 | if (!device->pending_bios) |
| 2731 | device->pending_bios = bio; |
| 2732 | if (device->running_pending) |
| 2733 | should_queue = 0; |
| 2734 | |
| 2735 | spin_unlock(&device->io_lock); |
| 2736 | |
| 2737 | if (should_queue) |
Chris Mason | 1cc127b | 2008-06-12 14:46:17 -0400 | [diff] [blame] | 2738 | btrfs_queue_worker(&root->fs_info->submit_workers, |
| 2739 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 2740 | return 0; |
| 2741 | } |
| 2742 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2743 | int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio, |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 2744 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2745 | { |
| 2746 | struct btrfs_mapping_tree *map_tree; |
| 2747 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2748 | struct bio *first_bio = bio; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 2749 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2750 | u64 length = 0; |
| 2751 | u64 map_length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2752 | struct btrfs_multi_bio *multi = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2753 | int ret; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2754 | int dev_nr = 0; |
| 2755 | int total_devs = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2756 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 2757 | length = bio->bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2758 | map_tree = &root->fs_info->mapping_tree; |
| 2759 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2760 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2761 | ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi, |
| 2762 | mirror_num); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2763 | BUG_ON(ret); |
| 2764 | |
| 2765 | total_devs = multi->num_stripes; |
| 2766 | if (map_length < length) { |
| 2767 | printk("mapping failed logical %Lu bio len %Lu " |
| 2768 | "len %Lu\n", logical, length, map_length); |
| 2769 | BUG(); |
| 2770 | } |
| 2771 | multi->end_io = first_bio->bi_end_io; |
| 2772 | multi->private = first_bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 2773 | multi->orig_bio = first_bio; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2774 | atomic_set(&multi->stripes_pending, multi->num_stripes); |
| 2775 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2776 | while(dev_nr < total_devs) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2777 | if (total_devs > 1) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2778 | if (dev_nr < total_devs - 1) { |
| 2779 | bio = bio_clone(first_bio, GFP_NOFS); |
| 2780 | BUG_ON(!bio); |
| 2781 | } else { |
| 2782 | bio = first_bio; |
| 2783 | } |
| 2784 | bio->bi_private = multi; |
| 2785 | bio->bi_end_io = end_bio_multi_stripe; |
| 2786 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2787 | bio->bi_sector = multi->stripes[dev_nr].physical >> 9; |
| 2788 | dev = multi->stripes[dev_nr].dev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2789 | BUG_ON(rw == WRITE && !dev->writeable); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2790 | if (dev && dev->bdev) { |
| 2791 | bio->bi_bdev = dev->bdev; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 2792 | if (async_submit) |
| 2793 | schedule_bio(root, dev, rw, bio); |
| 2794 | else |
| 2795 | submit_bio(rw, bio); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2796 | } else { |
| 2797 | bio->bi_bdev = root->fs_info->fs_devices->latest_bdev; |
| 2798 | bio->bi_sector = logical >> 9; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2799 | bio_endio(bio, -EIO); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2800 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2801 | dev_nr++; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2802 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2803 | if (total_devs == 1) |
| 2804 | kfree(multi); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2805 | return 0; |
| 2806 | } |
| 2807 | |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 2808 | struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2809 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2810 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2811 | struct btrfs_device *device; |
| 2812 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2813 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2814 | cur_devices = root->fs_info->fs_devices; |
| 2815 | while (cur_devices) { |
| 2816 | if (!fsid || |
| 2817 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 2818 | device = __find_device(&cur_devices->devices, |
| 2819 | devid, uuid); |
| 2820 | if (device) |
| 2821 | return device; |
| 2822 | } |
| 2823 | cur_devices = cur_devices->seed; |
| 2824 | } |
| 2825 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2826 | } |
| 2827 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2828 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
| 2829 | u64 devid, u8 *dev_uuid) |
| 2830 | { |
| 2831 | struct btrfs_device *device; |
| 2832 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 2833 | |
| 2834 | device = kzalloc(sizeof(*device), GFP_NOFS); |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 2835 | if (!device) |
| 2836 | return NULL; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2837 | list_add(&device->dev_list, |
| 2838 | &fs_devices->devices); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2839 | device->barriers = 1; |
| 2840 | device->dev_root = root->fs_info->dev_root; |
| 2841 | device->devid = devid; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 2842 | device->work.func = pending_bios_fn; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2843 | fs_devices->num_devices++; |
| 2844 | spin_lock_init(&device->io_lock); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2845 | INIT_LIST_HEAD(&device->dev_alloc_list); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2846 | memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE); |
| 2847 | return device; |
| 2848 | } |
| 2849 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2850 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 2851 | struct extent_buffer *leaf, |
| 2852 | struct btrfs_chunk *chunk) |
| 2853 | { |
| 2854 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 2855 | struct map_lookup *map; |
| 2856 | struct extent_map *em; |
| 2857 | u64 logical; |
| 2858 | u64 length; |
| 2859 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 2860 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2861 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2862 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2863 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2864 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 2865 | logical = key->offset; |
| 2866 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2867 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2868 | spin_lock(&map_tree->map_tree.lock); |
| 2869 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 2870 | spin_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2871 | |
| 2872 | /* already mapped? */ |
| 2873 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 2874 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2875 | return 0; |
| 2876 | } else if (em) { |
| 2877 | free_extent_map(em); |
| 2878 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2879 | |
| 2880 | map = kzalloc(sizeof(*map), GFP_NOFS); |
| 2881 | if (!map) |
| 2882 | return -ENOMEM; |
| 2883 | |
| 2884 | em = alloc_extent_map(GFP_NOFS); |
| 2885 | if (!em) |
| 2886 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2887 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2888 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2889 | if (!map) { |
| 2890 | free_extent_map(em); |
| 2891 | return -ENOMEM; |
| 2892 | } |
| 2893 | |
| 2894 | em->bdev = (struct block_device *)map; |
| 2895 | em->start = logical; |
| 2896 | em->len = length; |
| 2897 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2898 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2899 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2900 | map->num_stripes = num_stripes; |
| 2901 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 2902 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 2903 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 2904 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 2905 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2906 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2907 | for (i = 0; i < num_stripes; i++) { |
| 2908 | map->stripes[i].physical = |
| 2909 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 2910 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 2911 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 2912 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 2913 | BTRFS_UUID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2914 | map->stripes[i].dev = btrfs_find_device(root, devid, uuid, |
| 2915 | NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2916 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2917 | kfree(map); |
| 2918 | free_extent_map(em); |
| 2919 | return -EIO; |
| 2920 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2921 | if (!map->stripes[i].dev) { |
| 2922 | map->stripes[i].dev = |
| 2923 | add_missing_dev(root, devid, uuid); |
| 2924 | if (!map->stripes[i].dev) { |
| 2925 | kfree(map); |
| 2926 | free_extent_map(em); |
| 2927 | return -EIO; |
| 2928 | } |
| 2929 | } |
| 2930 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2931 | } |
| 2932 | |
| 2933 | spin_lock(&map_tree->map_tree.lock); |
| 2934 | ret = add_extent_mapping(&map_tree->map_tree, em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2935 | spin_unlock(&map_tree->map_tree.lock); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 2936 | BUG_ON(ret); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2937 | free_extent_map(em); |
| 2938 | |
| 2939 | return 0; |
| 2940 | } |
| 2941 | |
| 2942 | static int fill_device_from_item(struct extent_buffer *leaf, |
| 2943 | struct btrfs_dev_item *dev_item, |
| 2944 | struct btrfs_device *device) |
| 2945 | { |
| 2946 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2947 | |
| 2948 | device->devid = btrfs_device_id(leaf, dev_item); |
| 2949 | device->total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 2950 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
| 2951 | device->type = btrfs_device_type(leaf, dev_item); |
| 2952 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 2953 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 2954 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2955 | |
| 2956 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 2957 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2958 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2959 | return 0; |
| 2960 | } |
| 2961 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2962 | static int open_seed_devices(struct btrfs_root *root, u8 *fsid) |
| 2963 | { |
| 2964 | struct btrfs_fs_devices *fs_devices; |
| 2965 | int ret; |
| 2966 | |
| 2967 | mutex_lock(&uuid_mutex); |
| 2968 | |
| 2969 | fs_devices = root->fs_info->fs_devices->seed; |
| 2970 | while (fs_devices) { |
| 2971 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 2972 | ret = 0; |
| 2973 | goto out; |
| 2974 | } |
| 2975 | fs_devices = fs_devices->seed; |
| 2976 | } |
| 2977 | |
| 2978 | fs_devices = find_fsid(fsid); |
| 2979 | if (!fs_devices) { |
| 2980 | ret = -ENOENT; |
| 2981 | goto out; |
| 2982 | } |
| 2983 | if (fs_devices->opened) { |
| 2984 | ret = -EBUSY; |
| 2985 | goto out; |
| 2986 | } |
| 2987 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 2988 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 2989 | root->fs_info->bdev_holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2990 | if (ret) |
| 2991 | goto out; |
| 2992 | |
| 2993 | if (!fs_devices->seeding) { |
| 2994 | __btrfs_close_devices(fs_devices); |
| 2995 | ret = -EINVAL; |
| 2996 | goto out; |
| 2997 | } |
| 2998 | |
| 2999 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 3000 | root->fs_info->fs_devices->seed = fs_devices; |
| 3001 | fs_devices->sprouted = 1; |
| 3002 | out: |
| 3003 | mutex_unlock(&uuid_mutex); |
| 3004 | return ret; |
| 3005 | } |
| 3006 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3007 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3008 | struct extent_buffer *leaf, |
| 3009 | struct btrfs_dev_item *dev_item) |
| 3010 | { |
| 3011 | struct btrfs_device *device; |
| 3012 | u64 devid; |
| 3013 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3014 | int seed_devices = 0; |
| 3015 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 3016 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 3017 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3018 | devid = btrfs_device_id(leaf, dev_item); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 3019 | read_extent_buffer(leaf, dev_uuid, |
| 3020 | (unsigned long)btrfs_device_uuid(dev_item), |
| 3021 | BTRFS_UUID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3022 | read_extent_buffer(leaf, fs_uuid, |
| 3023 | (unsigned long)btrfs_device_fsid(dev_item), |
| 3024 | BTRFS_UUID_SIZE); |
| 3025 | |
| 3026 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
| 3027 | ret = open_seed_devices(root, fs_uuid); |
| 3028 | if (ret) |
| 3029 | return ret; |
| 3030 | seed_devices = 1; |
| 3031 | } |
| 3032 | |
| 3033 | device = btrfs_find_device(root, devid, dev_uuid, fs_uuid); |
| 3034 | if (!device || !device->bdev) { |
| 3035 | if (!btrfs_test_opt(root, DEGRADED) || seed_devices) |
| 3036 | return -EIO; |
| 3037 | |
| 3038 | if (!device) { |
| 3039 | printk("warning devid %Lu missing\n", devid); |
| 3040 | device = add_missing_dev(root, devid, dev_uuid); |
| 3041 | if (!device) |
| 3042 | return -ENOMEM; |
| 3043 | } |
| 3044 | } |
| 3045 | |
| 3046 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 3047 | BUG_ON(device->writeable); |
| 3048 | if (device->generation != |
| 3049 | btrfs_device_generation(leaf, dev_item)) |
| 3050 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3051 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3052 | |
| 3053 | fill_device_from_item(leaf, dev_item, device); |
| 3054 | device->dev_root = root->fs_info->dev_root; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 3055 | device->in_fs_metadata = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3056 | if (device->writeable) |
| 3057 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3058 | ret = 0; |
| 3059 | #if 0 |
| 3060 | ret = btrfs_open_device(device); |
| 3061 | if (ret) { |
| 3062 | kfree(device); |
| 3063 | } |
| 3064 | #endif |
| 3065 | return ret; |
| 3066 | } |
| 3067 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3068 | int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf) |
| 3069 | { |
| 3070 | struct btrfs_dev_item *dev_item; |
| 3071 | |
| 3072 | dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block, |
| 3073 | dev_item); |
| 3074 | return read_one_dev(root, buf, dev_item); |
| 3075 | } |
| 3076 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 3077 | int btrfs_read_sys_array(struct btrfs_root *root, u64 sb_bytenr) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3078 | { |
| 3079 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3080 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3081 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3082 | struct btrfs_chunk *chunk; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 3083 | u8 *ptr; |
| 3084 | unsigned long sb_ptr; |
| 3085 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3086 | u32 num_stripes; |
| 3087 | u32 array_size; |
| 3088 | u32 len = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3089 | u32 cur; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 3090 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3091 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 3092 | sb = btrfs_find_create_tree_block(root, sb_bytenr, |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3093 | BTRFS_SUPER_INFO_SIZE); |
| 3094 | if (!sb) |
| 3095 | return -ENOMEM; |
| 3096 | btrfs_set_buffer_uptodate(sb); |
| 3097 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3098 | array_size = btrfs_super_sys_array_size(super_copy); |
| 3099 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3100 | ptr = super_copy->sys_chunk_array; |
| 3101 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 3102 | cur = 0; |
| 3103 | |
| 3104 | while (cur < array_size) { |
| 3105 | disk_key = (struct btrfs_disk_key *)ptr; |
| 3106 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 3107 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3108 | len = sizeof(*disk_key); ptr += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3109 | sb_ptr += len; |
| 3110 | cur += len; |
| 3111 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3112 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3113 | chunk = (struct btrfs_chunk *)sb_ptr; |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3114 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 3115 | if (ret) |
| 3116 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3117 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 3118 | len = btrfs_chunk_item_size(num_stripes); |
| 3119 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 3120 | ret = -EIO; |
| 3121 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3122 | } |
| 3123 | ptr += len; |
| 3124 | sb_ptr += len; |
| 3125 | cur += len; |
| 3126 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3127 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 3128 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3129 | } |
| 3130 | |
| 3131 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 3132 | { |
| 3133 | struct btrfs_path *path; |
| 3134 | struct extent_buffer *leaf; |
| 3135 | struct btrfs_key key; |
| 3136 | struct btrfs_key found_key; |
| 3137 | int ret; |
| 3138 | int slot; |
| 3139 | |
| 3140 | root = root->fs_info->chunk_root; |
| 3141 | |
| 3142 | path = btrfs_alloc_path(); |
| 3143 | if (!path) |
| 3144 | return -ENOMEM; |
| 3145 | |
| 3146 | /* first we search for all of the device items, and then we |
| 3147 | * read in all of the chunk items. This way we can create chunk |
| 3148 | * mappings that reference all of the devices that are afound |
| 3149 | */ |
| 3150 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 3151 | key.offset = 0; |
| 3152 | key.type = 0; |
| 3153 | again: |
| 3154 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3155 | while(1) { |
| 3156 | leaf = path->nodes[0]; |
| 3157 | slot = path->slots[0]; |
| 3158 | if (slot >= btrfs_header_nritems(leaf)) { |
| 3159 | ret = btrfs_next_leaf(root, path); |
| 3160 | if (ret == 0) |
| 3161 | continue; |
| 3162 | if (ret < 0) |
| 3163 | goto error; |
| 3164 | break; |
| 3165 | } |
| 3166 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3167 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { |
| 3168 | if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID) |
| 3169 | break; |
| 3170 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 3171 | struct btrfs_dev_item *dev_item; |
| 3172 | dev_item = btrfs_item_ptr(leaf, slot, |
| 3173 | struct btrfs_dev_item); |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3174 | ret = read_one_dev(root, leaf, dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3175 | if (ret) |
| 3176 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3177 | } |
| 3178 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 3179 | struct btrfs_chunk *chunk; |
| 3180 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 3181 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3182 | if (ret) |
| 3183 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3184 | } |
| 3185 | path->slots[0]++; |
| 3186 | } |
| 3187 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { |
| 3188 | key.objectid = 0; |
| 3189 | btrfs_release_path(root, path); |
| 3190 | goto again; |
| 3191 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3192 | ret = 0; |
| 3193 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3194 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3195 | return ret; |
| 3196 | } |