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