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