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