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