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