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