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