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