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