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