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