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