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