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> |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 26 | #include <linux/ratelimit.h> |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 27 | #include <linux/kthread.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" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 37 | #include "rcu-string.h" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame] | 38 | #include "math.h" |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 39 | #include "dev-replace.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 40 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 41 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
| 42 | struct btrfs_root *root, |
| 43 | struct btrfs_device *device); |
| 44 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 45 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev); |
| 46 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 47 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 48 | static DEFINE_MUTEX(uuid_mutex); |
| 49 | static LIST_HEAD(fs_uuids); |
| 50 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 51 | static void lock_chunks(struct btrfs_root *root) |
| 52 | { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 53 | mutex_lock(&root->fs_info->chunk_mutex); |
| 54 | } |
| 55 | |
| 56 | static void unlock_chunks(struct btrfs_root *root) |
| 57 | { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 58 | mutex_unlock(&root->fs_info->chunk_mutex); |
| 59 | } |
| 60 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 61 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 62 | { |
| 63 | struct btrfs_device *device; |
| 64 | WARN_ON(fs_devices->opened); |
| 65 | while (!list_empty(&fs_devices->devices)) { |
| 66 | device = list_entry(fs_devices->devices.next, |
| 67 | struct btrfs_device, dev_list); |
| 68 | list_del(&device->dev_list); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 69 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 70 | kfree(device); |
| 71 | } |
| 72 | kfree(fs_devices); |
| 73 | } |
| 74 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 75 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 76 | enum kobject_action action) |
| 77 | { |
| 78 | int ret; |
| 79 | |
| 80 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 81 | if (ret) |
| 82 | pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n", |
| 83 | action, |
| 84 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 85 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 86 | } |
| 87 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 88 | void btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 89 | { |
| 90 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 91 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 92 | while (!list_empty(&fs_uuids)) { |
| 93 | fs_devices = list_entry(fs_uuids.next, |
| 94 | struct btrfs_fs_devices, list); |
| 95 | list_del(&fs_devices->list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 96 | free_fs_devices(fs_devices); |
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 | } |
| 99 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 100 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
| 101 | u64 devid, u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 102 | { |
| 103 | struct btrfs_device *dev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 104 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 105 | list_for_each_entry(dev, head, dev_list) { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 106 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 107 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 108 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 109 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 110 | } |
| 111 | return NULL; |
| 112 | } |
| 113 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 114 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 115 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 116 | struct btrfs_fs_devices *fs_devices; |
| 117 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 118 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 119 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 120 | return fs_devices; |
| 121 | } |
| 122 | return NULL; |
| 123 | } |
| 124 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 125 | static int |
| 126 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 127 | int flush, struct block_device **bdev, |
| 128 | struct buffer_head **bh) |
| 129 | { |
| 130 | int ret; |
| 131 | |
| 132 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 133 | |
| 134 | if (IS_ERR(*bdev)) { |
| 135 | ret = PTR_ERR(*bdev); |
| 136 | printk(KERN_INFO "btrfs: open %s failed\n", device_path); |
| 137 | goto error; |
| 138 | } |
| 139 | |
| 140 | if (flush) |
| 141 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
| 142 | ret = set_blocksize(*bdev, 4096); |
| 143 | if (ret) { |
| 144 | blkdev_put(*bdev, flags); |
| 145 | goto error; |
| 146 | } |
| 147 | invalidate_bdev(*bdev); |
| 148 | *bh = btrfs_read_dev_super(*bdev); |
| 149 | if (!*bh) { |
| 150 | ret = -EINVAL; |
| 151 | blkdev_put(*bdev, flags); |
| 152 | goto error; |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | |
| 157 | error: |
| 158 | *bdev = NULL; |
| 159 | *bh = NULL; |
| 160 | return ret; |
| 161 | } |
| 162 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 163 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 164 | struct bio *head, struct bio *tail) |
| 165 | { |
| 166 | |
| 167 | struct bio *old_head; |
| 168 | |
| 169 | old_head = pending_bios->head; |
| 170 | pending_bios->head = head; |
| 171 | if (pending_bios->tail) |
| 172 | tail->bi_next = old_head; |
| 173 | else |
| 174 | pending_bios->tail = tail; |
| 175 | } |
| 176 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 177 | /* |
| 178 | * we try to collect pending bios for a device so we don't get a large |
| 179 | * number of procs sending bios down to the same device. This greatly |
| 180 | * improves the schedulers ability to collect and merge the bios. |
| 181 | * |
| 182 | * But, it also turns into a long list of bios to process and that is sure |
| 183 | * to eventually make the worker thread block. The solution here is to |
| 184 | * make some progress and then put this work struct back at the end of |
| 185 | * the list if the block device is congested. This way, multiple devices |
| 186 | * can make progress from a single worker thread. |
| 187 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 188 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 189 | { |
| 190 | struct bio *pending; |
| 191 | struct backing_dev_info *bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 192 | struct btrfs_fs_info *fs_info; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 193 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 194 | struct bio *tail; |
| 195 | struct bio *cur; |
| 196 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 197 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 198 | unsigned long batch_run = 0; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 199 | unsigned long limit; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 200 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 201 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 202 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 203 | struct blk_plug plug; |
| 204 | |
| 205 | /* |
| 206 | * this function runs all the bios we've collected for |
| 207 | * a particular device. We don't want to wander off to |
| 208 | * another device without first sending all of these down. |
| 209 | * So, setup a plug here and finish it off before we return |
| 210 | */ |
| 211 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 212 | |
Chris Mason | bedf762 | 2009-04-03 10:32:58 -0400 | [diff] [blame] | 213 | bdi = blk_get_backing_dev_info(device->bdev); |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 214 | fs_info = device->dev_root->fs_info; |
| 215 | limit = btrfs_async_submit_limit(fs_info); |
| 216 | limit = limit * 2 / 3; |
| 217 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 218 | loop: |
| 219 | spin_lock(&device->io_lock); |
| 220 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 221 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 222 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 223 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 224 | /* take all the bios off the list at once and process them |
| 225 | * later on (without the lock held). But, remember the |
| 226 | * tail and other pointers so the bios can be properly reinserted |
| 227 | * into the list if we hit congestion |
| 228 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 229 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 230 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 231 | force_reg = 1; |
| 232 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 233 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 234 | force_reg = 0; |
| 235 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 236 | |
| 237 | pending = pending_bios->head; |
| 238 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 239 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 240 | |
| 241 | /* |
| 242 | * if pending was null this time around, no bios need processing |
| 243 | * at all and we can stop. Otherwise it'll loop back up again |
| 244 | * and do an additional check so no bios are missed. |
| 245 | * |
| 246 | * device->running_pending is used to synchronize with the |
| 247 | * schedule_bio code. |
| 248 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 249 | if (device->pending_sync_bios.head == NULL && |
| 250 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 251 | again = 0; |
| 252 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 253 | } else { |
| 254 | again = 1; |
| 255 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 256 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 257 | |
| 258 | pending_bios->head = NULL; |
| 259 | pending_bios->tail = NULL; |
| 260 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 261 | spin_unlock(&device->io_lock); |
| 262 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 263 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 264 | |
| 265 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 266 | /* we want to work on both lists, but do more bios on the |
| 267 | * sync list than the regular list |
| 268 | */ |
| 269 | if ((num_run > 32 && |
| 270 | pending_bios != &device->pending_sync_bios && |
| 271 | device->pending_sync_bios.head) || |
| 272 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 273 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 274 | spin_lock(&device->io_lock); |
| 275 | requeue_list(pending_bios, pending, tail); |
| 276 | goto loop_lock; |
| 277 | } |
| 278 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 279 | cur = pending; |
| 280 | pending = pending->bi_next; |
| 281 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 282 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 283 | if (atomic_dec_return(&fs_info->nr_async_bios) < limit && |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 284 | waitqueue_active(&fs_info->async_submit_wait)) |
| 285 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 286 | |
| 287 | BUG_ON(atomic_read(&cur->bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 288 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 289 | /* |
| 290 | * if we're doing the sync list, record that our |
| 291 | * plug has some sync requests on it |
| 292 | * |
| 293 | * If we're doing the regular list and there are |
| 294 | * sync requests sitting around, unplug before |
| 295 | * we add more |
| 296 | */ |
| 297 | if (pending_bios == &device->pending_sync_bios) { |
| 298 | sync_pending = 1; |
| 299 | } else if (sync_pending) { |
| 300 | blk_finish_plug(&plug); |
| 301 | blk_start_plug(&plug); |
| 302 | sync_pending = 0; |
| 303 | } |
| 304 | |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 305 | btrfsic_submit_bio(cur->bi_rw, cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 306 | num_run++; |
| 307 | batch_run++; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 308 | if (need_resched()) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 309 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 310 | |
| 311 | /* |
| 312 | * we made progress, there is more work to do and the bdi |
| 313 | * is now congested. Back off and let other work structs |
| 314 | * run instead |
| 315 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 316 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 317 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 318 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 319 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 320 | ioc = current->io_context; |
| 321 | |
| 322 | /* |
| 323 | * the main goal here is that we don't want to |
| 324 | * block if we're going to be able to submit |
| 325 | * more requests without blocking. |
| 326 | * |
| 327 | * This code does two great things, it pokes into |
| 328 | * the elevator code from a filesystem _and_ |
| 329 | * it makes assumptions about how batching works. |
| 330 | */ |
| 331 | if (ioc && ioc->nr_batch_requests > 0 && |
| 332 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 333 | (last_waited == 0 || |
| 334 | ioc->last_waited == last_waited)) { |
| 335 | /* |
| 336 | * we want to go through our batch of |
| 337 | * requests and stop. So, we copy out |
| 338 | * the ioc->last_waited time and test |
| 339 | * against it before looping |
| 340 | */ |
| 341 | last_waited = ioc->last_waited; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 342 | if (need_resched()) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 343 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 344 | continue; |
| 345 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 346 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 347 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 348 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 349 | |
| 350 | spin_unlock(&device->io_lock); |
| 351 | btrfs_requeue_work(&device->work); |
| 352 | goto done; |
| 353 | } |
Chris Mason | d85c8a6f | 2011-12-15 15:38:41 -0500 | [diff] [blame] | 354 | /* unplug every 64 requests just for good measure */ |
| 355 | if (batch_run % 64 == 0) { |
| 356 | blk_finish_plug(&plug); |
| 357 | blk_start_plug(&plug); |
| 358 | sync_pending = 0; |
| 359 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 360 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 361 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 362 | cond_resched(); |
| 363 | if (again) |
| 364 | goto loop; |
| 365 | |
| 366 | spin_lock(&device->io_lock); |
| 367 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 368 | goto loop_lock; |
| 369 | spin_unlock(&device->io_lock); |
| 370 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 371 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 372 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 373 | } |
| 374 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 375 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 376 | { |
| 377 | struct btrfs_device *device; |
| 378 | |
| 379 | device = container_of(work, struct btrfs_device, work); |
| 380 | run_scheduled_bios(device); |
| 381 | } |
| 382 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 383 | static noinline int device_list_add(const char *path, |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 384 | struct btrfs_super_block *disk_super, |
| 385 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) |
| 386 | { |
| 387 | struct btrfs_device *device; |
| 388 | struct btrfs_fs_devices *fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 389 | struct rcu_string *name; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 390 | u64 found_transid = btrfs_super_generation(disk_super); |
| 391 | |
| 392 | fs_devices = find_fsid(disk_super->fsid); |
| 393 | if (!fs_devices) { |
Chris Mason | 515dc32 | 2008-05-16 13:30:15 -0400 | [diff] [blame] | 394 | fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 395 | if (!fs_devices) |
| 396 | return -ENOMEM; |
| 397 | INIT_LIST_HEAD(&fs_devices->devices); |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 398 | INIT_LIST_HEAD(&fs_devices->alloc_list); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 399 | list_add(&fs_devices->list, &fs_uuids); |
| 400 | memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE); |
| 401 | fs_devices->latest_devid = devid; |
| 402 | fs_devices->latest_trans = found_transid; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 403 | mutex_init(&fs_devices->device_list_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 404 | device = NULL; |
| 405 | } else { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 406 | device = __find_device(&fs_devices->devices, devid, |
| 407 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 408 | } |
| 409 | if (!device) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 410 | if (fs_devices->opened) |
| 411 | return -EBUSY; |
| 412 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 413 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 414 | if (!device) { |
| 415 | /* we can safely leave the fs_devices entry around */ |
| 416 | return -ENOMEM; |
| 417 | } |
| 418 | device->devid = devid; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 419 | device->dev_stats_valid = 0; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 420 | device->work.func = pending_bios_fn; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 421 | memcpy(device->uuid, disk_super->dev_item.uuid, |
| 422 | BTRFS_UUID_SIZE); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 423 | spin_lock_init(&device->io_lock); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 424 | |
| 425 | name = rcu_string_strdup(path, GFP_NOFS); |
| 426 | if (!name) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 427 | kfree(device); |
| 428 | return -ENOMEM; |
| 429 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 430 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 431 | INIT_LIST_HEAD(&device->dev_alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 432 | |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 433 | /* init readahead state */ |
| 434 | spin_lock_init(&device->reada_lock); |
| 435 | device->reada_curr_zone = NULL; |
| 436 | atomic_set(&device->reada_in_flight, 0); |
| 437 | device->reada_next = 0; |
| 438 | INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT); |
| 439 | INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT); |
| 440 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 441 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 442 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 443 | mutex_unlock(&fs_devices->device_list_mutex); |
| 444 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 445 | device->fs_devices = fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 446 | fs_devices->num_devices++; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 447 | } else if (!device->name || strcmp(device->name->str, path)) { |
| 448 | name = rcu_string_strdup(path, GFP_NOFS); |
TARUISI Hiroaki | 3a0524d | 2010-02-09 06:36:45 +0000 | [diff] [blame] | 449 | if (!name) |
| 450 | return -ENOMEM; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 451 | rcu_string_free(device->name); |
| 452 | rcu_assign_pointer(device->name, name); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 453 | if (device->missing) { |
| 454 | fs_devices->missing_devices--; |
| 455 | device->missing = 0; |
| 456 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | if (found_transid > fs_devices->latest_trans) { |
| 460 | fs_devices->latest_devid = devid; |
| 461 | fs_devices->latest_trans = found_transid; |
| 462 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 463 | *fs_devices_ret = fs_devices; |
| 464 | return 0; |
| 465 | } |
| 466 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 467 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 468 | { |
| 469 | struct btrfs_fs_devices *fs_devices; |
| 470 | struct btrfs_device *device; |
| 471 | struct btrfs_device *orig_dev; |
| 472 | |
| 473 | fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
| 474 | if (!fs_devices) |
| 475 | return ERR_PTR(-ENOMEM); |
| 476 | |
| 477 | INIT_LIST_HEAD(&fs_devices->devices); |
| 478 | INIT_LIST_HEAD(&fs_devices->alloc_list); |
| 479 | INIT_LIST_HEAD(&fs_devices->list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 480 | mutex_init(&fs_devices->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 481 | fs_devices->latest_devid = orig->latest_devid; |
| 482 | fs_devices->latest_trans = orig->latest_trans; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 483 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 484 | memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid)); |
| 485 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 486 | /* 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] | 487 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 488 | struct rcu_string *name; |
| 489 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 490 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 491 | if (!device) |
| 492 | goto error; |
| 493 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 494 | /* |
| 495 | * This is ok to do without rcu read locked because we hold the |
| 496 | * uuid mutex so nothing we touch in here is going to disappear. |
| 497 | */ |
| 498 | name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS); |
| 499 | if (!name) { |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 500 | kfree(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 501 | goto error; |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 502 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 503 | rcu_assign_pointer(device->name, name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 504 | |
| 505 | device->devid = orig_dev->devid; |
| 506 | device->work.func = pending_bios_fn; |
| 507 | memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 508 | spin_lock_init(&device->io_lock); |
| 509 | INIT_LIST_HEAD(&device->dev_list); |
| 510 | INIT_LIST_HEAD(&device->dev_alloc_list); |
| 511 | |
| 512 | list_add(&device->dev_list, &fs_devices->devices); |
| 513 | device->fs_devices = fs_devices; |
| 514 | fs_devices->num_devices++; |
| 515 | } |
| 516 | return fs_devices; |
| 517 | error: |
| 518 | free_fs_devices(fs_devices); |
| 519 | return ERR_PTR(-ENOMEM); |
| 520 | } |
| 521 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 522 | void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info, |
| 523 | struct btrfs_fs_devices *fs_devices, int step) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 524 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 525 | struct btrfs_device *device, *next; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 526 | |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 527 | struct block_device *latest_bdev = NULL; |
| 528 | u64 latest_devid = 0; |
| 529 | u64 latest_transid = 0; |
| 530 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 531 | mutex_lock(&uuid_mutex); |
| 532 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 533 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 534 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 535 | if (device->in_fs_metadata) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 536 | if (!device->is_tgtdev_for_dev_replace && |
| 537 | (!latest_transid || |
| 538 | device->generation > latest_transid)) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 539 | latest_devid = device->devid; |
| 540 | latest_transid = device->generation; |
| 541 | latest_bdev = device->bdev; |
| 542 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 543 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 544 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 545 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 546 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 547 | /* |
| 548 | * In the first step, keep the device which has |
| 549 | * the correct fsid and the devid that is used |
| 550 | * for the dev_replace procedure. |
| 551 | * In the second step, the dev_replace state is |
| 552 | * read from the device tree and it is known |
| 553 | * whether the procedure is really active or |
| 554 | * not, which means whether this device is |
| 555 | * used or whether it should be removed. |
| 556 | */ |
| 557 | if (step == 0 || device->is_tgtdev_for_dev_replace) { |
| 558 | continue; |
| 559 | } |
| 560 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 561 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 562 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 563 | device->bdev = NULL; |
| 564 | fs_devices->open_devices--; |
| 565 | } |
| 566 | if (device->writeable) { |
| 567 | list_del_init(&device->dev_alloc_list); |
| 568 | device->writeable = 0; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 569 | if (!device->is_tgtdev_for_dev_replace) |
| 570 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 571 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 572 | list_del_init(&device->dev_list); |
| 573 | fs_devices->num_devices--; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 574 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 575 | kfree(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 576 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 577 | |
| 578 | if (fs_devices->seed) { |
| 579 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 580 | goto again; |
| 581 | } |
| 582 | |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 583 | fs_devices->latest_bdev = latest_bdev; |
| 584 | fs_devices->latest_devid = latest_devid; |
| 585 | fs_devices->latest_trans = latest_transid; |
| 586 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 587 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 588 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 589 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 590 | static void __free_device(struct work_struct *work) |
| 591 | { |
| 592 | struct btrfs_device *device; |
| 593 | |
| 594 | device = container_of(work, struct btrfs_device, rcu_work); |
| 595 | |
| 596 | if (device->bdev) |
| 597 | blkdev_put(device->bdev, device->mode); |
| 598 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 599 | rcu_string_free(device->name); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 600 | kfree(device); |
| 601 | } |
| 602 | |
| 603 | static void free_device(struct rcu_head *head) |
| 604 | { |
| 605 | struct btrfs_device *device; |
| 606 | |
| 607 | device = container_of(head, struct btrfs_device, rcu); |
| 608 | |
| 609 | INIT_WORK(&device->rcu_work, __free_device); |
| 610 | schedule_work(&device->rcu_work); |
| 611 | } |
| 612 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 613 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 614 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 615 | struct btrfs_device *device; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 616 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 617 | if (--fs_devices->opened > 0) |
| 618 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 619 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 620 | mutex_lock(&fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 621 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 622 | struct btrfs_device *new_device; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 623 | struct rcu_string *name; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 624 | |
| 625 | if (device->bdev) |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 626 | fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 627 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 628 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 629 | list_del_init(&device->dev_alloc_list); |
| 630 | fs_devices->rw_devices--; |
| 631 | } |
| 632 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 633 | if (device->can_discard) |
| 634 | fs_devices->num_can_discard--; |
| 635 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 636 | new_device = kmalloc(sizeof(*new_device), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 637 | BUG_ON(!new_device); /* -ENOMEM */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 638 | memcpy(new_device, device, sizeof(*new_device)); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 639 | |
| 640 | /* Safe because we are under uuid_mutex */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 641 | if (device->name) { |
| 642 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
| 643 | BUG_ON(device->name && !name); /* -ENOMEM */ |
| 644 | rcu_assign_pointer(new_device->name, name); |
| 645 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 646 | new_device->bdev = NULL; |
| 647 | new_device->writeable = 0; |
| 648 | new_device->in_fs_metadata = 0; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 649 | new_device->can_discard = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 650 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
| 651 | |
| 652 | call_rcu(&device->rcu, free_device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 653 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 654 | mutex_unlock(&fs_devices->device_list_mutex); |
| 655 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 656 | WARN_ON(fs_devices->open_devices); |
| 657 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 658 | fs_devices->opened = 0; |
| 659 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 660 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 661 | return 0; |
| 662 | } |
| 663 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 664 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 665 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 666 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 667 | int ret; |
| 668 | |
| 669 | mutex_lock(&uuid_mutex); |
| 670 | ret = __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 671 | if (!fs_devices->opened) { |
| 672 | seed_devices = fs_devices->seed; |
| 673 | fs_devices->seed = NULL; |
| 674 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 675 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 676 | |
| 677 | while (seed_devices) { |
| 678 | fs_devices = seed_devices; |
| 679 | seed_devices = fs_devices->seed; |
| 680 | __btrfs_close_devices(fs_devices); |
| 681 | free_fs_devices(fs_devices); |
| 682 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 683 | return ret; |
| 684 | } |
| 685 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 686 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 687 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 688 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 689 | struct request_queue *q; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 690 | struct block_device *bdev; |
| 691 | struct list_head *head = &fs_devices->devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 692 | struct btrfs_device *device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 693 | struct block_device *latest_bdev = NULL; |
| 694 | struct buffer_head *bh; |
| 695 | struct btrfs_super_block *disk_super; |
| 696 | u64 latest_devid = 0; |
| 697 | u64 latest_transid = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 698 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 699 | int seeding = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 700 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 701 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 702 | flags |= FMODE_EXCL; |
| 703 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 704 | list_for_each_entry(device, head, dev_list) { |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 705 | if (device->bdev) |
| 706 | continue; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 707 | if (!device->name) |
| 708 | continue; |
| 709 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 710 | ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 711 | &bdev, &bh); |
| 712 | if (ret) |
| 713 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 714 | |
| 715 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 716 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 717 | if (devid != device->devid) |
| 718 | goto error_brelse; |
| 719 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 720 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
| 721 | BTRFS_UUID_SIZE)) |
| 722 | goto error_brelse; |
| 723 | |
| 724 | device->generation = btrfs_super_generation(disk_super); |
| 725 | if (!latest_transid || device->generation > latest_transid) { |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 726 | latest_devid = devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 727 | latest_transid = device->generation; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 728 | latest_bdev = bdev; |
| 729 | } |
| 730 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 731 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| 732 | device->writeable = 0; |
| 733 | } else { |
| 734 | device->writeable = !bdev_read_only(bdev); |
| 735 | seeding = 0; |
| 736 | } |
| 737 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 738 | q = bdev_get_queue(bdev); |
| 739 | if (blk_queue_discard(q)) { |
| 740 | device->can_discard = 1; |
| 741 | fs_devices->num_can_discard++; |
| 742 | } |
| 743 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 744 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 745 | device->in_fs_metadata = 0; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 746 | device->mode = flags; |
| 747 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 748 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 749 | fs_devices->rotating = 1; |
| 750 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 751 | fs_devices->open_devices++; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 752 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 753 | fs_devices->rw_devices++; |
| 754 | list_add(&device->dev_alloc_list, |
| 755 | &fs_devices->alloc_list); |
| 756 | } |
Xiao Guangrong | 4f6c932 | 2011-04-20 10:06:40 +0000 | [diff] [blame] | 757 | brelse(bh); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 758 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 759 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 760 | error_brelse: |
| 761 | brelse(bh); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 762 | blkdev_put(bdev, flags); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 763 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 764 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 765 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 766 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 767 | goto out; |
| 768 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 769 | fs_devices->seeding = seeding; |
| 770 | fs_devices->opened = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 771 | fs_devices->latest_bdev = latest_bdev; |
| 772 | fs_devices->latest_devid = latest_devid; |
| 773 | fs_devices->latest_trans = latest_transid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 774 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 775 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 776 | return ret; |
| 777 | } |
| 778 | |
| 779 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 780 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 781 | { |
| 782 | int ret; |
| 783 | |
| 784 | mutex_lock(&uuid_mutex); |
| 785 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 786 | fs_devices->opened++; |
| 787 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 788 | } else { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 789 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 790 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 791 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 792 | return ret; |
| 793 | } |
| 794 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 795 | /* |
| 796 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 797 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 798 | * is read via pagecache |
| 799 | */ |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 800 | 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] | 801 | struct btrfs_fs_devices **fs_devices_ret) |
| 802 | { |
| 803 | struct btrfs_super_block *disk_super; |
| 804 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 805 | struct page *page; |
| 806 | void *p; |
| 807 | int ret = -EINVAL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 808 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 809 | u64 transid; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 810 | u64 total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 811 | u64 bytenr; |
| 812 | pgoff_t index; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 813 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 814 | /* |
| 815 | * we would like to check all the supers, but that would make |
| 816 | * a btrfs mount succeed after a mkfs from a different FS. |
| 817 | * So, we need to add a special mount option to scan for |
| 818 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 819 | */ |
| 820 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 821 | flags |= FMODE_EXCL; |
Al Viro | 10f6327 | 2011-11-17 15:05:22 -0500 | [diff] [blame] | 822 | mutex_lock(&uuid_mutex); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 823 | |
| 824 | bdev = blkdev_get_by_path(path, flags, holder); |
| 825 | |
| 826 | if (IS_ERR(bdev)) { |
| 827 | ret = PTR_ERR(bdev); |
| 828 | printk(KERN_INFO "btrfs: open %s failed\n", path); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 829 | goto error; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | /* make sure our super fits in the device */ |
| 833 | if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode)) |
| 834 | goto error_bdev_put; |
| 835 | |
| 836 | /* make sure our super fits in the page */ |
| 837 | if (sizeof(*disk_super) > PAGE_CACHE_SIZE) |
| 838 | goto error_bdev_put; |
| 839 | |
| 840 | /* make sure our super doesn't straddle pages on disk */ |
| 841 | index = bytenr >> PAGE_CACHE_SHIFT; |
| 842 | if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index) |
| 843 | goto error_bdev_put; |
| 844 | |
| 845 | /* pull in the page with our super */ |
| 846 | page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 847 | index, GFP_NOFS); |
| 848 | |
| 849 | if (IS_ERR_OR_NULL(page)) |
| 850 | goto error_bdev_put; |
| 851 | |
| 852 | p = kmap(page); |
| 853 | |
| 854 | /* align our pointer to the offset of the super block */ |
| 855 | disk_super = p + (bytenr & ~PAGE_CACHE_MASK); |
| 856 | |
| 857 | if (btrfs_super_bytenr(disk_super) != bytenr || |
| 858 | strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, |
| 859 | sizeof(disk_super->magic))) |
| 860 | goto error_unmap; |
| 861 | |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 862 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 863 | transid = btrfs_super_generation(disk_super); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 864 | total_devices = btrfs_super_num_devices(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 865 | |
Stefan Behrens | d03f918 | 2012-11-05 13:10:49 +0000 | [diff] [blame] | 866 | if (disk_super->label[0]) { |
| 867 | if (disk_super->label[BTRFS_LABEL_SIZE - 1]) |
| 868 | disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 869 | printk(KERN_INFO "device label %s ", disk_super->label); |
Stefan Behrens | d03f918 | 2012-11-05 13:10:49 +0000 | [diff] [blame] | 870 | } else { |
Ilya Dryomov | 22b63a2 | 2011-02-09 16:05:31 +0200 | [diff] [blame] | 871 | printk(KERN_INFO "device fsid %pU ", disk_super->fsid); |
Stefan Behrens | d03f918 | 2012-11-05 13:10:49 +0000 | [diff] [blame] | 872 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 873 | |
Roland Dreier | 119e10c | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 874 | printk(KERN_CONT "devid %llu transid %llu %s\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 875 | (unsigned long long)devid, (unsigned long long)transid, path); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 876 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 877 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 878 | if (!ret && fs_devices_ret) |
| 879 | (*fs_devices_ret)->total_devices = total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 880 | |
| 881 | error_unmap: |
| 882 | kunmap(page); |
| 883 | page_cache_release(page); |
| 884 | |
| 885 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 886 | blkdev_put(bdev, flags); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 887 | error: |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 888 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 889 | return ret; |
| 890 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 891 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 892 | /* helper to account the used device space in the range */ |
| 893 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, |
| 894 | u64 end, u64 *length) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 895 | { |
| 896 | struct btrfs_key key; |
| 897 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 898 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 899 | struct btrfs_path *path; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 900 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 901 | int ret; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 902 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 903 | struct extent_buffer *l; |
| 904 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 905 | *length = 0; |
| 906 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 907 | if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 908 | return 0; |
| 909 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 910 | path = btrfs_alloc_path(); |
| 911 | if (!path) |
| 912 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 913 | path->reada = 2; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 914 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 915 | key.objectid = device->devid; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 916 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 917 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 918 | |
| 919 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 920 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 921 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 922 | if (ret > 0) { |
| 923 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 924 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 925 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 926 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 927 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 928 | while (1) { |
| 929 | l = path->nodes[0]; |
| 930 | slot = path->slots[0]; |
| 931 | if (slot >= btrfs_header_nritems(l)) { |
| 932 | ret = btrfs_next_leaf(root, path); |
| 933 | if (ret == 0) |
| 934 | continue; |
| 935 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 936 | goto out; |
| 937 | |
| 938 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 939 | } |
| 940 | btrfs_item_key_to_cpu(l, &key, slot); |
| 941 | |
| 942 | if (key.objectid < device->devid) |
| 943 | goto next; |
| 944 | |
| 945 | if (key.objectid > device->devid) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 946 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 947 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 948 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 949 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 950 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 951 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 952 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 953 | dev_extent); |
| 954 | if (key.offset <= start && extent_end > end) { |
| 955 | *length = end - start + 1; |
| 956 | break; |
| 957 | } else if (key.offset <= start && extent_end > start) |
| 958 | *length += extent_end - start; |
| 959 | else if (key.offset > start && extent_end <= end) |
| 960 | *length += extent_end - key.offset; |
| 961 | else if (key.offset > start && key.offset <= end) { |
| 962 | *length += end - key.offset + 1; |
| 963 | break; |
| 964 | } else if (key.offset > end) |
| 965 | break; |
| 966 | |
| 967 | next: |
| 968 | path->slots[0]++; |
| 969 | } |
| 970 | ret = 0; |
| 971 | out: |
| 972 | btrfs_free_path(path); |
| 973 | return ret; |
| 974 | } |
| 975 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 976 | /* |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 977 | * find_free_dev_extent - find free space in the specified device |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 978 | * @device: the device which we search the free space in |
| 979 | * @num_bytes: the size of the free space that we need |
| 980 | * @start: store the start of the free space. |
| 981 | * @len: the size of the free space. that we find, or the size of the max |
| 982 | * free space if we don't find suitable free space |
| 983 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 984 | * this uses a pretty simple search, the expectation is that it is |
| 985 | * called very infrequently and that a given device has a small number |
| 986 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 987 | * |
| 988 | * @start is used to store the start of the free space if we find. But if we |
| 989 | * don't find suitable free space, it will be used to store the start position |
| 990 | * of the max free space. |
| 991 | * |
| 992 | * @len is used to store the size of the free space that we find. |
| 993 | * But if we don't find suitable free space, it is used to store the size of |
| 994 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 995 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 996 | int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 997 | u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 998 | { |
| 999 | struct btrfs_key key; |
| 1000 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1001 | struct btrfs_dev_extent *dev_extent; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1002 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1003 | u64 hole_size; |
| 1004 | u64 max_hole_start; |
| 1005 | u64 max_hole_size; |
| 1006 | u64 extent_end; |
| 1007 | u64 search_start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1008 | u64 search_end = device->total_bytes; |
| 1009 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1010 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1011 | struct extent_buffer *l; |
| 1012 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1013 | /* FIXME use last free of some kind */ |
| 1014 | |
| 1015 | /* we don't want to overwrite the superblock on the drive, |
| 1016 | * so we make sure to start at an offset of at least 1MB |
| 1017 | */ |
Arne Jansen | a9c9bf6 | 2011-04-12 11:01:20 +0200 | [diff] [blame] | 1018 | search_start = max(root->fs_info->alloc_start, 1024ull * 1024); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1019 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1020 | max_hole_start = search_start; |
| 1021 | max_hole_size = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1022 | hole_size = 0; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1023 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1024 | if (search_start >= search_end || device->is_tgtdev_for_dev_replace) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1025 | ret = -ENOSPC; |
| 1026 | goto error; |
| 1027 | } |
| 1028 | |
| 1029 | path = btrfs_alloc_path(); |
| 1030 | if (!path) { |
| 1031 | ret = -ENOMEM; |
| 1032 | goto error; |
| 1033 | } |
| 1034 | path->reada = 2; |
| 1035 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1036 | key.objectid = device->devid; |
| 1037 | key.offset = search_start; |
| 1038 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1039 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1040 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1041 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1042 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1043 | if (ret > 0) { |
| 1044 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1045 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1046 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1047 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1048 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1049 | while (1) { |
| 1050 | l = path->nodes[0]; |
| 1051 | slot = path->slots[0]; |
| 1052 | if (slot >= btrfs_header_nritems(l)) { |
| 1053 | ret = btrfs_next_leaf(root, path); |
| 1054 | if (ret == 0) |
| 1055 | continue; |
| 1056 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1057 | goto out; |
| 1058 | |
| 1059 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1060 | } |
| 1061 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1062 | |
| 1063 | if (key.objectid < device->devid) |
| 1064 | goto next; |
| 1065 | |
| 1066 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1067 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1068 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1069 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) |
| 1070 | goto next; |
| 1071 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1072 | if (key.offset > search_start) { |
| 1073 | hole_size = key.offset - search_start; |
| 1074 | |
| 1075 | if (hole_size > max_hole_size) { |
| 1076 | max_hole_start = search_start; |
| 1077 | max_hole_size = hole_size; |
| 1078 | } |
| 1079 | |
| 1080 | /* |
| 1081 | * If this free space is greater than which we need, |
| 1082 | * it must be the max free space that we have found |
| 1083 | * until now, so max_hole_start must point to the start |
| 1084 | * of this free space and the length of this free space |
| 1085 | * is stored in max_hole_size. Thus, we return |
| 1086 | * max_hole_start and max_hole_size and go back to the |
| 1087 | * caller. |
| 1088 | */ |
| 1089 | if (hole_size >= num_bytes) { |
| 1090 | ret = 0; |
| 1091 | goto out; |
| 1092 | } |
| 1093 | } |
| 1094 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1095 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1096 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1097 | dev_extent); |
| 1098 | if (extent_end > search_start) |
| 1099 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1100 | next: |
| 1101 | path->slots[0]++; |
| 1102 | cond_resched(); |
| 1103 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1104 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1105 | /* |
| 1106 | * At this point, search_start should be the end of |
| 1107 | * allocated dev extents, and when shrinking the device, |
| 1108 | * search_end may be smaller than search_start. |
| 1109 | */ |
| 1110 | if (search_end > search_start) |
| 1111 | hole_size = search_end - search_start; |
| 1112 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1113 | if (hole_size > max_hole_size) { |
| 1114 | max_hole_start = search_start; |
| 1115 | max_hole_size = hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1116 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1117 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1118 | /* See above. */ |
| 1119 | if (hole_size < num_bytes) |
| 1120 | ret = -ENOSPC; |
| 1121 | else |
| 1122 | ret = 0; |
| 1123 | |
| 1124 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1125 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1126 | error: |
| 1127 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1128 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1129 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1130 | return ret; |
| 1131 | } |
| 1132 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1133 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1134 | struct btrfs_device *device, |
| 1135 | u64 start) |
| 1136 | { |
| 1137 | int ret; |
| 1138 | struct btrfs_path *path; |
| 1139 | struct btrfs_root *root = device->dev_root; |
| 1140 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1141 | struct btrfs_key found_key; |
| 1142 | struct extent_buffer *leaf = NULL; |
| 1143 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1144 | |
| 1145 | path = btrfs_alloc_path(); |
| 1146 | if (!path) |
| 1147 | return -ENOMEM; |
| 1148 | |
| 1149 | key.objectid = device->devid; |
| 1150 | key.offset = start; |
| 1151 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1152 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1153 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1154 | if (ret > 0) { |
| 1155 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1156 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1157 | if (ret) |
| 1158 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1159 | leaf = path->nodes[0]; |
| 1160 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1161 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1162 | struct btrfs_dev_extent); |
| 1163 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1164 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1165 | key = found_key; |
| 1166 | btrfs_release_path(path); |
| 1167 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1168 | } else if (ret == 0) { |
| 1169 | leaf = path->nodes[0]; |
| 1170 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1171 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1172 | } else { |
| 1173 | btrfs_error(root->fs_info, ret, "Slot search failed"); |
| 1174 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1175 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1176 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 1177 | if (device->bytes_used > 0) { |
| 1178 | u64 len = btrfs_dev_extent_length(leaf, extent); |
| 1179 | device->bytes_used -= len; |
| 1180 | spin_lock(&root->fs_info->free_chunk_lock); |
| 1181 | root->fs_info->free_chunk_space += len; |
| 1182 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 1183 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1184 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1185 | if (ret) { |
| 1186 | btrfs_error(root->fs_info, ret, |
| 1187 | "Failed to remove dev extent item"); |
| 1188 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1189 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1190 | btrfs_free_path(path); |
| 1191 | return ret; |
| 1192 | } |
| 1193 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1194 | int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1195 | struct btrfs_device *device, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1196 | u64 chunk_tree, u64 chunk_objectid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1197 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1198 | { |
| 1199 | int ret; |
| 1200 | struct btrfs_path *path; |
| 1201 | struct btrfs_root *root = device->dev_root; |
| 1202 | struct btrfs_dev_extent *extent; |
| 1203 | struct extent_buffer *leaf; |
| 1204 | struct btrfs_key key; |
| 1205 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1206 | WARN_ON(!device->in_fs_metadata); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1207 | WARN_ON(device->is_tgtdev_for_dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1208 | path = btrfs_alloc_path(); |
| 1209 | if (!path) |
| 1210 | return -ENOMEM; |
| 1211 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1212 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1213 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1214 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1215 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1216 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1217 | if (ret) |
| 1218 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1219 | |
| 1220 | leaf = path->nodes[0]; |
| 1221 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1222 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1223 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 1224 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 1225 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1226 | |
| 1227 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
| 1228 | (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent), |
| 1229 | BTRFS_UUID_SIZE); |
| 1230 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1231 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1232 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1233 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1234 | btrfs_free_path(path); |
| 1235 | return ret; |
| 1236 | } |
| 1237 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 1238 | static noinline int find_next_chunk(struct btrfs_root *root, |
| 1239 | u64 objectid, u64 *offset) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1240 | { |
| 1241 | struct btrfs_path *path; |
| 1242 | int ret; |
| 1243 | struct btrfs_key key; |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1244 | struct btrfs_chunk *chunk; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1245 | struct btrfs_key found_key; |
| 1246 | |
| 1247 | path = btrfs_alloc_path(); |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 1248 | if (!path) |
| 1249 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1250 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1251 | key.objectid = objectid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1252 | key.offset = (u64)-1; |
| 1253 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 1254 | |
| 1255 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1256 | if (ret < 0) |
| 1257 | goto error; |
| 1258 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1259 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1260 | |
| 1261 | ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY); |
| 1262 | if (ret) { |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1263 | *offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1264 | } else { |
| 1265 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1266 | path->slots[0]); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1267 | if (found_key.objectid != objectid) |
| 1268 | *offset = 0; |
| 1269 | else { |
| 1270 | chunk = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1271 | struct btrfs_chunk); |
| 1272 | *offset = found_key.offset + |
| 1273 | btrfs_chunk_length(path->nodes[0], chunk); |
| 1274 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1275 | } |
| 1276 | ret = 0; |
| 1277 | error: |
| 1278 | btrfs_free_path(path); |
| 1279 | return ret; |
| 1280 | } |
| 1281 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1282 | static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1283 | { |
| 1284 | int ret; |
| 1285 | struct btrfs_key key; |
| 1286 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1287 | struct btrfs_path *path; |
| 1288 | |
| 1289 | root = root->fs_info->chunk_root; |
| 1290 | |
| 1291 | path = btrfs_alloc_path(); |
| 1292 | if (!path) |
| 1293 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1294 | |
| 1295 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1296 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1297 | key.offset = (u64)-1; |
| 1298 | |
| 1299 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1300 | if (ret < 0) |
| 1301 | goto error; |
| 1302 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1303 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1304 | |
| 1305 | ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID, |
| 1306 | BTRFS_DEV_ITEM_KEY); |
| 1307 | if (ret) { |
| 1308 | *objectid = 1; |
| 1309 | } else { |
| 1310 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1311 | path->slots[0]); |
| 1312 | *objectid = found_key.offset + 1; |
| 1313 | } |
| 1314 | ret = 0; |
| 1315 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1316 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1317 | return ret; |
| 1318 | } |
| 1319 | |
| 1320 | /* |
| 1321 | * the device information is stored in the chunk root |
| 1322 | * the btrfs_device struct should be fully filled in |
| 1323 | */ |
| 1324 | int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 1325 | struct btrfs_root *root, |
| 1326 | struct btrfs_device *device) |
| 1327 | { |
| 1328 | int ret; |
| 1329 | struct btrfs_path *path; |
| 1330 | struct btrfs_dev_item *dev_item; |
| 1331 | struct extent_buffer *leaf; |
| 1332 | struct btrfs_key key; |
| 1333 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1334 | |
| 1335 | root = root->fs_info->chunk_root; |
| 1336 | |
| 1337 | path = btrfs_alloc_path(); |
| 1338 | if (!path) |
| 1339 | return -ENOMEM; |
| 1340 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1341 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1342 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1343 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1344 | |
| 1345 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1346 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1347 | if (ret) |
| 1348 | goto out; |
| 1349 | |
| 1350 | leaf = path->nodes[0]; |
| 1351 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1352 | |
| 1353 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1354 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1355 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1356 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1357 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1358 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1359 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
| 1360 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1361 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1362 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1363 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1364 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1365 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1366 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1367 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1368 | ptr = (unsigned long)btrfs_device_fsid(dev_item); |
| 1369 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1370 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1371 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1372 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1373 | out: |
| 1374 | btrfs_free_path(path); |
| 1375 | return ret; |
| 1376 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1377 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1378 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 1379 | struct btrfs_device *device) |
| 1380 | { |
| 1381 | int ret; |
| 1382 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1383 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1384 | struct btrfs_trans_handle *trans; |
| 1385 | |
| 1386 | root = root->fs_info->chunk_root; |
| 1387 | |
| 1388 | path = btrfs_alloc_path(); |
| 1389 | if (!path) |
| 1390 | return -ENOMEM; |
| 1391 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1392 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1393 | if (IS_ERR(trans)) { |
| 1394 | btrfs_free_path(path); |
| 1395 | return PTR_ERR(trans); |
| 1396 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1397 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1398 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1399 | key.offset = device->devid; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1400 | lock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1401 | |
| 1402 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1403 | if (ret < 0) |
| 1404 | goto out; |
| 1405 | |
| 1406 | if (ret > 0) { |
| 1407 | ret = -ENOENT; |
| 1408 | goto out; |
| 1409 | } |
| 1410 | |
| 1411 | ret = btrfs_del_item(trans, root, path); |
| 1412 | if (ret) |
| 1413 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1414 | out: |
| 1415 | btrfs_free_path(path); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1416 | unlock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1417 | btrfs_commit_transaction(trans, root); |
| 1418 | return ret; |
| 1419 | } |
| 1420 | |
| 1421 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 1422 | { |
| 1423 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1424 | struct btrfs_device *next_device; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1425 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1426 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1427 | struct btrfs_super_block *disk_super; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1428 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1429 | u64 all_avail; |
| 1430 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1431 | u64 num_devices; |
| 1432 | u8 *dev_uuid; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1433 | unsigned seq; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1434 | int ret = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1435 | bool clear_super = false; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1436 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1437 | mutex_lock(&uuid_mutex); |
| 1438 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1439 | do { |
| 1440 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 1441 | |
| 1442 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 1443 | root->fs_info->avail_system_alloc_bits | |
| 1444 | root->fs_info->avail_metadata_alloc_bits; |
| 1445 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1446 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1447 | num_devices = root->fs_info->fs_devices->num_devices; |
| 1448 | btrfs_dev_replace_lock(&root->fs_info->dev_replace); |
| 1449 | if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) { |
| 1450 | WARN_ON(num_devices < 1); |
| 1451 | num_devices--; |
| 1452 | } |
| 1453 | btrfs_dev_replace_unlock(&root->fs_info->dev_replace); |
| 1454 | |
| 1455 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1456 | printk(KERN_ERR "btrfs: unable to go below four devices " |
| 1457 | "on raid10\n"); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1458 | ret = -EINVAL; |
| 1459 | goto out; |
| 1460 | } |
| 1461 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1462 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1463 | printk(KERN_ERR "btrfs: unable to go below two " |
| 1464 | "devices on raid1\n"); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1465 | ret = -EINVAL; |
| 1466 | goto out; |
| 1467 | } |
| 1468 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1469 | if (strcmp(device_path, "missing") == 0) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1470 | struct list_head *devices; |
| 1471 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1472 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1473 | device = NULL; |
| 1474 | devices = &root->fs_info->fs_devices->devices; |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1475 | /* |
| 1476 | * It is safe to read the devices since the volume_mutex |
| 1477 | * is held. |
| 1478 | */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1479 | list_for_each_entry(tmp, devices, dev_list) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1480 | if (tmp->in_fs_metadata && |
| 1481 | !tmp->is_tgtdev_for_dev_replace && |
| 1482 | !tmp->bdev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1483 | device = tmp; |
| 1484 | break; |
| 1485 | } |
| 1486 | } |
| 1487 | bdev = NULL; |
| 1488 | bh = NULL; |
| 1489 | disk_super = NULL; |
| 1490 | if (!device) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1491 | printk(KERN_ERR "btrfs: no missing devices found to " |
| 1492 | "remove\n"); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1493 | goto out; |
| 1494 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1495 | } else { |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1496 | ret = btrfs_get_bdev_and_sb(device_path, |
Lukas Czerner | cc975eb | 2012-12-07 10:09:19 +0000 | [diff] [blame] | 1497 | FMODE_WRITE | FMODE_EXCL, |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1498 | root->fs_info->bdev_holder, 0, |
| 1499 | &bdev, &bh); |
| 1500 | if (ret) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1501 | goto out; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1502 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1503 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1504 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1505 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1506 | disk_super->fsid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1507 | if (!device) { |
| 1508 | ret = -ENOENT; |
| 1509 | goto error_brelse; |
| 1510 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1511 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1512 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1513 | if (device->is_tgtdev_for_dev_replace) { |
| 1514 | pr_err("btrfs: unable to remove the dev_replace target dev\n"); |
| 1515 | ret = -EINVAL; |
| 1516 | goto error_brelse; |
| 1517 | } |
| 1518 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1519 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1520 | printk(KERN_ERR "btrfs: unable to remove the only writeable " |
| 1521 | "device\n"); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1522 | ret = -EINVAL; |
| 1523 | goto error_brelse; |
| 1524 | } |
| 1525 | |
| 1526 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1527 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1528 | list_del_init(&device->dev_alloc_list); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1529 | unlock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1530 | root->fs_info->fs_devices->rw_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1531 | clear_super = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1532 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1533 | |
| 1534 | ret = btrfs_shrink_device(device, 0); |
| 1535 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1536 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1537 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1538 | /* |
| 1539 | * TODO: the superblock still includes this device in its num_devices |
| 1540 | * counter although write_all_supers() is not locked out. This |
| 1541 | * could give a filesystem state which requires a degraded mount. |
| 1542 | */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1543 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1544 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1545 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1546 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 1547 | spin_lock(&root->fs_info->free_chunk_lock); |
| 1548 | root->fs_info->free_chunk_space = device->total_bytes - |
| 1549 | device->bytes_used; |
| 1550 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 1551 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1552 | device->in_fs_metadata = 0; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1553 | btrfs_scrub_cancel_dev(root->fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1554 | |
| 1555 | /* |
| 1556 | * the device list mutex makes sure that we don't change |
| 1557 | * the device list while someone else is writing out all |
| 1558 | * the device supers. |
| 1559 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1560 | |
| 1561 | cur_devices = device->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1562 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1563 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1564 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1565 | device->fs_devices->num_devices--; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1566 | device->fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1567 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1568 | if (device->missing) |
| 1569 | root->fs_info->fs_devices->missing_devices--; |
| 1570 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1571 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1572 | struct btrfs_device, dev_list); |
| 1573 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1574 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1575 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1576 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1577 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1578 | if (device->bdev) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1579 | device->fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1580 | |
| 1581 | call_rcu(&device->rcu, free_device); |
| 1582 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1583 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1584 | num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; |
| 1585 | btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1586 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1587 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1588 | struct btrfs_fs_devices *fs_devices; |
| 1589 | fs_devices = root->fs_info->fs_devices; |
| 1590 | while (fs_devices) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1591 | if (fs_devices->seed == cur_devices) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1592 | break; |
| 1593 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1594 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1595 | fs_devices->seed = cur_devices->seed; |
| 1596 | cur_devices->seed = NULL; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1597 | lock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1598 | __btrfs_close_devices(cur_devices); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1599 | unlock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1600 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1601 | } |
| 1602 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1603 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1604 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
| 1605 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1606 | /* |
| 1607 | * at this point, the device is zero sized. We want to |
| 1608 | * remove it from the devices list and zero out the old super |
| 1609 | */ |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1610 | if (clear_super && disk_super) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1611 | /* make sure this device isn't detected as part of |
| 1612 | * the FS anymore |
| 1613 | */ |
| 1614 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1615 | set_buffer_dirty(bh); |
| 1616 | sync_dirty_buffer(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1617 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1618 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1619 | ret = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1620 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 1621 | /* Notify udev that device has changed */ |
Eric Sandeen | 3c91160 | 2013-01-31 00:55:02 +0000 | [diff] [blame] | 1622 | if (bdev) |
| 1623 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 1624 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1625 | error_brelse: |
| 1626 | brelse(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1627 | if (bdev) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1628 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1629 | out: |
| 1630 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1631 | return ret; |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1632 | error_undo: |
| 1633 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1634 | lock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1635 | list_add(&device->dev_alloc_list, |
| 1636 | &root->fs_info->fs_devices->alloc_list); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1637 | unlock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1638 | root->fs_info->fs_devices->rw_devices++; |
| 1639 | } |
| 1640 | goto error_brelse; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1641 | } |
| 1642 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1643 | void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info, |
| 1644 | struct btrfs_device *srcdev) |
| 1645 | { |
| 1646 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
| 1647 | list_del_rcu(&srcdev->dev_list); |
| 1648 | list_del_rcu(&srcdev->dev_alloc_list); |
| 1649 | fs_info->fs_devices->num_devices--; |
| 1650 | if (srcdev->missing) { |
| 1651 | fs_info->fs_devices->missing_devices--; |
| 1652 | fs_info->fs_devices->rw_devices++; |
| 1653 | } |
| 1654 | if (srcdev->can_discard) |
| 1655 | fs_info->fs_devices->num_can_discard--; |
| 1656 | if (srcdev->bdev) |
| 1657 | fs_info->fs_devices->open_devices--; |
| 1658 | |
| 1659 | call_rcu(&srcdev->rcu, free_device); |
| 1660 | } |
| 1661 | |
| 1662 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 1663 | struct btrfs_device *tgtdev) |
| 1664 | { |
| 1665 | struct btrfs_device *next_device; |
| 1666 | |
| 1667 | WARN_ON(!tgtdev); |
| 1668 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 1669 | if (tgtdev->bdev) { |
| 1670 | btrfs_scratch_superblock(tgtdev); |
| 1671 | fs_info->fs_devices->open_devices--; |
| 1672 | } |
| 1673 | fs_info->fs_devices->num_devices--; |
| 1674 | if (tgtdev->can_discard) |
| 1675 | fs_info->fs_devices->num_can_discard++; |
| 1676 | |
| 1677 | next_device = list_entry(fs_info->fs_devices->devices.next, |
| 1678 | struct btrfs_device, dev_list); |
| 1679 | if (tgtdev->bdev == fs_info->sb->s_bdev) |
| 1680 | fs_info->sb->s_bdev = next_device->bdev; |
| 1681 | if (tgtdev->bdev == fs_info->fs_devices->latest_bdev) |
| 1682 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1683 | list_del_rcu(&tgtdev->dev_list); |
| 1684 | |
| 1685 | call_rcu(&tgtdev->rcu, free_device); |
| 1686 | |
| 1687 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
| 1688 | } |
| 1689 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1690 | int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
| 1691 | struct btrfs_device **device) |
| 1692 | { |
| 1693 | int ret = 0; |
| 1694 | struct btrfs_super_block *disk_super; |
| 1695 | u64 devid; |
| 1696 | u8 *dev_uuid; |
| 1697 | struct block_device *bdev; |
| 1698 | struct buffer_head *bh; |
| 1699 | |
| 1700 | *device = NULL; |
| 1701 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
| 1702 | root->fs_info->bdev_holder, 0, &bdev, &bh); |
| 1703 | if (ret) |
| 1704 | return ret; |
| 1705 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1706 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 1707 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1708 | *device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1709 | disk_super->fsid); |
| 1710 | brelse(bh); |
| 1711 | if (!*device) |
| 1712 | ret = -ENOENT; |
| 1713 | blkdev_put(bdev, FMODE_READ); |
| 1714 | return ret; |
| 1715 | } |
| 1716 | |
| 1717 | int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, |
| 1718 | char *device_path, |
| 1719 | struct btrfs_device **device) |
| 1720 | { |
| 1721 | *device = NULL; |
| 1722 | if (strcmp(device_path, "missing") == 0) { |
| 1723 | struct list_head *devices; |
| 1724 | struct btrfs_device *tmp; |
| 1725 | |
| 1726 | devices = &root->fs_info->fs_devices->devices; |
| 1727 | /* |
| 1728 | * It is safe to read the devices since the volume_mutex |
| 1729 | * is held by the caller. |
| 1730 | */ |
| 1731 | list_for_each_entry(tmp, devices, dev_list) { |
| 1732 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 1733 | *device = tmp; |
| 1734 | break; |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | if (!*device) { |
| 1739 | pr_err("btrfs: no missing device found\n"); |
| 1740 | return -ENOENT; |
| 1741 | } |
| 1742 | |
| 1743 | return 0; |
| 1744 | } else { |
| 1745 | return btrfs_find_device_by_path(root, device_path, device); |
| 1746 | } |
| 1747 | } |
| 1748 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1749 | /* |
| 1750 | * does all the dirty work required for changing file system's UUID. |
| 1751 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1752 | static int btrfs_prepare_sprout(struct btrfs_root *root) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1753 | { |
| 1754 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 1755 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1756 | struct btrfs_fs_devices *seed_devices; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1757 | struct btrfs_super_block *disk_super = root->fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1758 | struct btrfs_device *device; |
| 1759 | u64 super_flags; |
| 1760 | |
| 1761 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1762 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1763 | return -EINVAL; |
| 1764 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1765 | seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
| 1766 | if (!seed_devices) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1767 | return -ENOMEM; |
| 1768 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1769 | old_devices = clone_fs_devices(fs_devices); |
| 1770 | if (IS_ERR(old_devices)) { |
| 1771 | kfree(seed_devices); |
| 1772 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1773 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1774 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1775 | list_add(&old_devices->list, &fs_uuids); |
| 1776 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1777 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 1778 | seed_devices->opened = 1; |
| 1779 | INIT_LIST_HEAD(&seed_devices->devices); |
| 1780 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1781 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1782 | |
| 1783 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1784 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 1785 | synchronize_rcu); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1786 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 1787 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1788 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
| 1789 | list_for_each_entry(device, &seed_devices->devices, dev_list) { |
| 1790 | device->fs_devices = seed_devices; |
| 1791 | } |
| 1792 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1793 | fs_devices->seeding = 0; |
| 1794 | fs_devices->num_devices = 0; |
| 1795 | fs_devices->open_devices = 0; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1796 | fs_devices->total_devices = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1797 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1798 | |
| 1799 | generate_random_uuid(fs_devices->fsid); |
| 1800 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 1801 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 1802 | super_flags = btrfs_super_flags(disk_super) & |
| 1803 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 1804 | btrfs_set_super_flags(disk_super, super_flags); |
| 1805 | |
| 1806 | return 0; |
| 1807 | } |
| 1808 | |
| 1809 | /* |
| 1810 | * strore the expected generation for seed devices in device items. |
| 1811 | */ |
| 1812 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 1813 | struct btrfs_root *root) |
| 1814 | { |
| 1815 | struct btrfs_path *path; |
| 1816 | struct extent_buffer *leaf; |
| 1817 | struct btrfs_dev_item *dev_item; |
| 1818 | struct btrfs_device *device; |
| 1819 | struct btrfs_key key; |
| 1820 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 1821 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 1822 | u64 devid; |
| 1823 | int ret; |
| 1824 | |
| 1825 | path = btrfs_alloc_path(); |
| 1826 | if (!path) |
| 1827 | return -ENOMEM; |
| 1828 | |
| 1829 | root = root->fs_info->chunk_root; |
| 1830 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1831 | key.offset = 0; |
| 1832 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1833 | |
| 1834 | while (1) { |
| 1835 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 1836 | if (ret < 0) |
| 1837 | goto error; |
| 1838 | |
| 1839 | leaf = path->nodes[0]; |
| 1840 | next_slot: |
| 1841 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1842 | ret = btrfs_next_leaf(root, path); |
| 1843 | if (ret > 0) |
| 1844 | break; |
| 1845 | if (ret < 0) |
| 1846 | goto error; |
| 1847 | leaf = path->nodes[0]; |
| 1848 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1849 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1850 | continue; |
| 1851 | } |
| 1852 | |
| 1853 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1854 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 1855 | key.type != BTRFS_DEV_ITEM_KEY) |
| 1856 | break; |
| 1857 | |
| 1858 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 1859 | struct btrfs_dev_item); |
| 1860 | devid = btrfs_device_id(leaf, dev_item); |
| 1861 | read_extent_buffer(leaf, dev_uuid, |
| 1862 | (unsigned long)btrfs_device_uuid(dev_item), |
| 1863 | BTRFS_UUID_SIZE); |
| 1864 | read_extent_buffer(leaf, fs_uuid, |
| 1865 | (unsigned long)btrfs_device_fsid(dev_item), |
| 1866 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1867 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
| 1868 | fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1869 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1870 | |
| 1871 | if (device->fs_devices->seeding) { |
| 1872 | btrfs_set_device_generation(leaf, dev_item, |
| 1873 | device->generation); |
| 1874 | btrfs_mark_buffer_dirty(leaf); |
| 1875 | } |
| 1876 | |
| 1877 | path->slots[0]++; |
| 1878 | goto next_slot; |
| 1879 | } |
| 1880 | ret = 0; |
| 1881 | error: |
| 1882 | btrfs_free_path(path); |
| 1883 | return ret; |
| 1884 | } |
| 1885 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1886 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 1887 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1888 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1889 | struct btrfs_trans_handle *trans; |
| 1890 | struct btrfs_device *device; |
| 1891 | struct block_device *bdev; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1892 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1893 | struct super_block *sb = root->fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1894 | struct rcu_string *name; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1895 | u64 total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1896 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1897 | int ret = 0; |
| 1898 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1899 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 1900 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1901 | |
Li Zefan | a5d1633 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1902 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1903 | root->fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 1904 | if (IS_ERR(bdev)) |
| 1905 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1906 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1907 | if (root->fs_info->fs_devices->seeding) { |
| 1908 | seeding_dev = 1; |
| 1909 | down_write(&sb->s_umount); |
| 1910 | mutex_lock(&uuid_mutex); |
| 1911 | } |
| 1912 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 1913 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1914 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1915 | devices = &root->fs_info->fs_devices->devices; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 1916 | |
| 1917 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1918 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1919 | if (device->bdev == bdev) { |
| 1920 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 1921 | mutex_unlock( |
| 1922 | &root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1923 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1924 | } |
| 1925 | } |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 1926 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1927 | |
| 1928 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 1929 | if (!device) { |
| 1930 | /* we can safely leave the fs_devices entry around */ |
| 1931 | ret = -ENOMEM; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1932 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1933 | } |
| 1934 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1935 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 1936 | if (!name) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1937 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1938 | ret = -ENOMEM; |
| 1939 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1940 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1941 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1942 | |
| 1943 | ret = find_next_devid(root, &device->devid); |
| 1944 | if (ret) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1945 | rcu_string_free(device->name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1946 | kfree(device); |
| 1947 | goto error; |
| 1948 | } |
| 1949 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1950 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1951 | if (IS_ERR(trans)) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1952 | rcu_string_free(device->name); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1953 | kfree(device); |
| 1954 | ret = PTR_ERR(trans); |
| 1955 | goto error; |
| 1956 | } |
| 1957 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1958 | lock_chunks(root); |
| 1959 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1960 | q = bdev_get_queue(bdev); |
| 1961 | if (blk_queue_discard(q)) |
| 1962 | device->can_discard = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1963 | device->writeable = 1; |
| 1964 | device->work.func = pending_bios_fn; |
| 1965 | generate_random_uuid(device->uuid); |
| 1966 | spin_lock_init(&device->io_lock); |
| 1967 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1968 | device->io_width = root->sectorsize; |
| 1969 | device->io_align = root->sectorsize; |
| 1970 | device->sector_size = root->sectorsize; |
| 1971 | device->total_bytes = i_size_read(bdev->bd_inode); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 1972 | device->disk_total_bytes = device->total_bytes; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1973 | device->dev_root = root->fs_info->dev_root; |
| 1974 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1975 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1976 | device->is_tgtdev_for_dev_replace = 0; |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 1977 | device->mode = FMODE_EXCL; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 1978 | set_blocksize(device->bdev, 4096); |
| 1979 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1980 | if (seeding_dev) { |
| 1981 | sb->s_flags &= ~MS_RDONLY; |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1982 | ret = btrfs_prepare_sprout(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1983 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | device->fs_devices = root->fs_info->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1987 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1988 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1989 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1990 | list_add(&device->dev_alloc_list, |
| 1991 | &root->fs_info->fs_devices->alloc_list); |
| 1992 | root->fs_info->fs_devices->num_devices++; |
| 1993 | root->fs_info->fs_devices->open_devices++; |
| 1994 | root->fs_info->fs_devices->rw_devices++; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1995 | root->fs_info->fs_devices->total_devices++; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1996 | if (device->can_discard) |
| 1997 | root->fs_info->fs_devices->num_can_discard++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1998 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 1999 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2000 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2001 | root->fs_info->free_chunk_space += device->total_bytes; |
| 2002 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2003 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2004 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 2005 | root->fs_info->fs_devices->rotating = 1; |
| 2006 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2007 | total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy); |
| 2008 | btrfs_set_super_total_bytes(root->fs_info->super_copy, |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2009 | total_bytes + device->total_bytes); |
| 2010 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2011 | total_bytes = btrfs_super_num_devices(root->fs_info->super_copy); |
| 2012 | btrfs_set_super_num_devices(root->fs_info->super_copy, |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2013 | total_bytes + 1); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2014 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2015 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2016 | if (seeding_dev) { |
| 2017 | ret = init_first_rw_device(trans, root, device); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2018 | if (ret) { |
| 2019 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2020 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2021 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2022 | ret = btrfs_finish_sprout(trans, root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2023 | if (ret) { |
| 2024 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2025 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2026 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2027 | } else { |
| 2028 | ret = btrfs_add_device(trans, root, device); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2029 | if (ret) { |
| 2030 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2031 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2032 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2033 | } |
| 2034 | |
Chris Mason | 913d952 | 2009-03-10 13:17:18 -0400 | [diff] [blame] | 2035 | /* |
| 2036 | * we've got more storage, clear any full flags on the space |
| 2037 | * infos |
| 2038 | */ |
| 2039 | btrfs_clear_space_info_full(root->fs_info); |
| 2040 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2041 | unlock_chunks(root); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2042 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 2043 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2044 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2045 | |
| 2046 | if (seeding_dev) { |
| 2047 | mutex_unlock(&uuid_mutex); |
| 2048 | up_write(&sb->s_umount); |
| 2049 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2050 | if (ret) /* transaction commit */ |
| 2051 | return ret; |
| 2052 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2053 | ret = btrfs_relocate_sys_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2054 | if (ret < 0) |
| 2055 | btrfs_error(root->fs_info, ret, |
| 2056 | "Failed to relocate sys chunks after " |
| 2057 | "device initialization. This can be fixed " |
| 2058 | "using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2059 | trans = btrfs_attach_transaction(root); |
| 2060 | if (IS_ERR(trans)) { |
| 2061 | if (PTR_ERR(trans) == -ENOENT) |
| 2062 | return 0; |
| 2063 | return PTR_ERR(trans); |
| 2064 | } |
| 2065 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2066 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2067 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2068 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2069 | |
| 2070 | error_trans: |
| 2071 | unlock_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2072 | btrfs_end_transaction(trans, root); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2073 | rcu_string_free(device->name); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2074 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2075 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2076 | blkdev_put(bdev, FMODE_EXCL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2077 | if (seeding_dev) { |
| 2078 | mutex_unlock(&uuid_mutex); |
| 2079 | up_write(&sb->s_umount); |
| 2080 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2081 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2082 | } |
| 2083 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2084 | int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path, |
| 2085 | struct btrfs_device **device_out) |
| 2086 | { |
| 2087 | struct request_queue *q; |
| 2088 | struct btrfs_device *device; |
| 2089 | struct block_device *bdev; |
| 2090 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2091 | struct list_head *devices; |
| 2092 | struct rcu_string *name; |
| 2093 | int ret = 0; |
| 2094 | |
| 2095 | *device_out = NULL; |
| 2096 | if (fs_info->fs_devices->seeding) |
| 2097 | return -EINVAL; |
| 2098 | |
| 2099 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| 2100 | fs_info->bdev_holder); |
| 2101 | if (IS_ERR(bdev)) |
| 2102 | return PTR_ERR(bdev); |
| 2103 | |
| 2104 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 2105 | |
| 2106 | devices = &fs_info->fs_devices->devices; |
| 2107 | list_for_each_entry(device, devices, dev_list) { |
| 2108 | if (device->bdev == bdev) { |
| 2109 | ret = -EEXIST; |
| 2110 | goto error; |
| 2111 | } |
| 2112 | } |
| 2113 | |
| 2114 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 2115 | if (!device) { |
| 2116 | ret = -ENOMEM; |
| 2117 | goto error; |
| 2118 | } |
| 2119 | |
| 2120 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2121 | if (!name) { |
| 2122 | kfree(device); |
| 2123 | ret = -ENOMEM; |
| 2124 | goto error; |
| 2125 | } |
| 2126 | rcu_assign_pointer(device->name, name); |
| 2127 | |
| 2128 | q = bdev_get_queue(bdev); |
| 2129 | if (blk_queue_discard(q)) |
| 2130 | device->can_discard = 1; |
| 2131 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 2132 | device->writeable = 1; |
| 2133 | device->work.func = pending_bios_fn; |
| 2134 | generate_random_uuid(device->uuid); |
| 2135 | device->devid = BTRFS_DEV_REPLACE_DEVID; |
| 2136 | spin_lock_init(&device->io_lock); |
| 2137 | device->generation = 0; |
| 2138 | device->io_width = root->sectorsize; |
| 2139 | device->io_align = root->sectorsize; |
| 2140 | device->sector_size = root->sectorsize; |
| 2141 | device->total_bytes = i_size_read(bdev->bd_inode); |
| 2142 | device->disk_total_bytes = device->total_bytes; |
| 2143 | device->dev_root = fs_info->dev_root; |
| 2144 | device->bdev = bdev; |
| 2145 | device->in_fs_metadata = 1; |
| 2146 | device->is_tgtdev_for_dev_replace = 1; |
| 2147 | device->mode = FMODE_EXCL; |
| 2148 | set_blocksize(device->bdev, 4096); |
| 2149 | device->fs_devices = fs_info->fs_devices; |
| 2150 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 2151 | fs_info->fs_devices->num_devices++; |
| 2152 | fs_info->fs_devices->open_devices++; |
| 2153 | if (device->can_discard) |
| 2154 | fs_info->fs_devices->num_can_discard++; |
| 2155 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2156 | |
| 2157 | *device_out = device; |
| 2158 | return ret; |
| 2159 | |
| 2160 | error: |
| 2161 | blkdev_put(bdev, FMODE_EXCL); |
| 2162 | return ret; |
| 2163 | } |
| 2164 | |
| 2165 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, |
| 2166 | struct btrfs_device *tgtdev) |
| 2167 | { |
| 2168 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 2169 | tgtdev->io_width = fs_info->dev_root->sectorsize; |
| 2170 | tgtdev->io_align = fs_info->dev_root->sectorsize; |
| 2171 | tgtdev->sector_size = fs_info->dev_root->sectorsize; |
| 2172 | tgtdev->dev_root = fs_info->dev_root; |
| 2173 | tgtdev->in_fs_metadata = 1; |
| 2174 | } |
| 2175 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2176 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2177 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2178 | { |
| 2179 | int ret; |
| 2180 | struct btrfs_path *path; |
| 2181 | struct btrfs_root *root; |
| 2182 | struct btrfs_dev_item *dev_item; |
| 2183 | struct extent_buffer *leaf; |
| 2184 | struct btrfs_key key; |
| 2185 | |
| 2186 | root = device->dev_root->fs_info->chunk_root; |
| 2187 | |
| 2188 | path = btrfs_alloc_path(); |
| 2189 | if (!path) |
| 2190 | return -ENOMEM; |
| 2191 | |
| 2192 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2193 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2194 | key.offset = device->devid; |
| 2195 | |
| 2196 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2197 | if (ret < 0) |
| 2198 | goto out; |
| 2199 | |
| 2200 | if (ret > 0) { |
| 2201 | ret = -ENOENT; |
| 2202 | goto out; |
| 2203 | } |
| 2204 | |
| 2205 | leaf = path->nodes[0]; |
| 2206 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2207 | |
| 2208 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2209 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2210 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2211 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2212 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 2213 | btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2214 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
| 2215 | btrfs_mark_buffer_dirty(leaf); |
| 2216 | |
| 2217 | out: |
| 2218 | btrfs_free_path(path); |
| 2219 | return ret; |
| 2220 | } |
| 2221 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2222 | static int __btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2223 | struct btrfs_device *device, u64 new_size) |
| 2224 | { |
| 2225 | struct btrfs_super_block *super_copy = |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2226 | device->dev_root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2227 | u64 old_total = btrfs_super_total_bytes(super_copy); |
| 2228 | u64 diff = new_size - device->total_bytes; |
| 2229 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2230 | if (!device->writeable) |
| 2231 | return -EACCES; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2232 | if (new_size <= device->total_bytes || |
| 2233 | device->is_tgtdev_for_dev_replace) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2234 | return -EINVAL; |
| 2235 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2236 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2237 | device->fs_devices->total_rw_bytes += diff; |
| 2238 | |
| 2239 | device->total_bytes = new_size; |
Chris Mason | 9779b72 | 2009-07-24 16:41:41 -0400 | [diff] [blame] | 2240 | device->disk_total_bytes = new_size; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2241 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
| 2242 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2243 | return btrfs_update_device(trans, device); |
| 2244 | } |
| 2245 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2246 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
| 2247 | struct btrfs_device *device, u64 new_size) |
| 2248 | { |
| 2249 | int ret; |
| 2250 | lock_chunks(device->dev_root); |
| 2251 | ret = __btrfs_grow_device(trans, device, new_size); |
| 2252 | unlock_chunks(device->dev_root); |
| 2253 | return ret; |
| 2254 | } |
| 2255 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2256 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
| 2257 | struct btrfs_root *root, |
| 2258 | u64 chunk_tree, u64 chunk_objectid, |
| 2259 | u64 chunk_offset) |
| 2260 | { |
| 2261 | int ret; |
| 2262 | struct btrfs_path *path; |
| 2263 | struct btrfs_key key; |
| 2264 | |
| 2265 | root = root->fs_info->chunk_root; |
| 2266 | path = btrfs_alloc_path(); |
| 2267 | if (!path) |
| 2268 | return -ENOMEM; |
| 2269 | |
| 2270 | key.objectid = chunk_objectid; |
| 2271 | key.offset = chunk_offset; |
| 2272 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2273 | |
| 2274 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2275 | if (ret < 0) |
| 2276 | goto out; |
| 2277 | else if (ret > 0) { /* Logic error or corruption */ |
| 2278 | btrfs_error(root->fs_info, -ENOENT, |
| 2279 | "Failed lookup while freeing chunk."); |
| 2280 | ret = -ENOENT; |
| 2281 | goto out; |
| 2282 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2283 | |
| 2284 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2285 | if (ret < 0) |
| 2286 | btrfs_error(root->fs_info, ret, |
| 2287 | "Failed to delete chunk item."); |
| 2288 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2289 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2290 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2291 | } |
| 2292 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2293 | 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] | 2294 | chunk_offset) |
| 2295 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2296 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2297 | struct btrfs_disk_key *disk_key; |
| 2298 | struct btrfs_chunk *chunk; |
| 2299 | u8 *ptr; |
| 2300 | int ret = 0; |
| 2301 | u32 num_stripes; |
| 2302 | u32 array_size; |
| 2303 | u32 len = 0; |
| 2304 | u32 cur; |
| 2305 | struct btrfs_key key; |
| 2306 | |
| 2307 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2308 | |
| 2309 | ptr = super_copy->sys_chunk_array; |
| 2310 | cur = 0; |
| 2311 | |
| 2312 | while (cur < array_size) { |
| 2313 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2314 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2315 | |
| 2316 | len = sizeof(*disk_key); |
| 2317 | |
| 2318 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2319 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2320 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2321 | len += btrfs_chunk_item_size(num_stripes); |
| 2322 | } else { |
| 2323 | ret = -EIO; |
| 2324 | break; |
| 2325 | } |
| 2326 | if (key.objectid == chunk_objectid && |
| 2327 | key.offset == chunk_offset) { |
| 2328 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2329 | array_size -= len; |
| 2330 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2331 | } else { |
| 2332 | ptr += len; |
| 2333 | cur += len; |
| 2334 | } |
| 2335 | } |
| 2336 | return ret; |
| 2337 | } |
| 2338 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2339 | static int btrfs_relocate_chunk(struct btrfs_root *root, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2340 | u64 chunk_tree, u64 chunk_objectid, |
| 2341 | u64 chunk_offset) |
| 2342 | { |
| 2343 | struct extent_map_tree *em_tree; |
| 2344 | struct btrfs_root *extent_root; |
| 2345 | struct btrfs_trans_handle *trans; |
| 2346 | struct extent_map *em; |
| 2347 | struct map_lookup *map; |
| 2348 | int ret; |
| 2349 | int i; |
| 2350 | |
| 2351 | root = root->fs_info->chunk_root; |
| 2352 | extent_root = root->fs_info->extent_root; |
| 2353 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 2354 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2355 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
| 2356 | if (ret) |
| 2357 | return -ENOSPC; |
| 2358 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2359 | /* step one, relocate all the extents inside this chunk */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2360 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2361 | if (ret) |
| 2362 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2363 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2364 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2365 | BUG_ON(IS_ERR(trans)); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2366 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2367 | lock_chunks(root); |
| 2368 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2369 | /* |
| 2370 | * step two, delete the device extents and the |
| 2371 | * chunk tree entries |
| 2372 | */ |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2373 | read_lock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2374 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2375 | read_unlock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2376 | |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 2377 | BUG_ON(!em || em->start > chunk_offset || |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2378 | em->start + em->len < chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2379 | map = (struct map_lookup *)em->bdev; |
| 2380 | |
| 2381 | for (i = 0; i < map->num_stripes; i++) { |
| 2382 | ret = btrfs_free_dev_extent(trans, map->stripes[i].dev, |
| 2383 | map->stripes[i].physical); |
| 2384 | BUG_ON(ret); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2385 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2386 | if (map->stripes[i].dev) { |
| 2387 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2388 | BUG_ON(ret); |
| 2389 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2390 | } |
| 2391 | ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid, |
| 2392 | chunk_offset); |
| 2393 | |
| 2394 | BUG_ON(ret); |
| 2395 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2396 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
| 2397 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2398 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2399 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 2400 | BUG_ON(ret); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2401 | } |
| 2402 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2403 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset); |
| 2404 | BUG_ON(ret); |
| 2405 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2406 | write_lock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2407 | remove_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2408 | write_unlock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2409 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2410 | kfree(map); |
| 2411 | em->bdev = NULL; |
| 2412 | |
| 2413 | /* once for the tree */ |
| 2414 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2415 | /* once for us */ |
| 2416 | free_extent_map(em); |
| 2417 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2418 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2419 | btrfs_end_transaction(trans, root); |
| 2420 | return 0; |
| 2421 | } |
| 2422 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2423 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 2424 | { |
| 2425 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 2426 | struct btrfs_path *path; |
| 2427 | struct extent_buffer *leaf; |
| 2428 | struct btrfs_chunk *chunk; |
| 2429 | struct btrfs_key key; |
| 2430 | struct btrfs_key found_key; |
| 2431 | u64 chunk_tree = chunk_root->root_key.objectid; |
| 2432 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2433 | bool retried = false; |
| 2434 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2435 | int ret; |
| 2436 | |
| 2437 | path = btrfs_alloc_path(); |
| 2438 | if (!path) |
| 2439 | return -ENOMEM; |
| 2440 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2441 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2442 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2443 | key.offset = (u64)-1; |
| 2444 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2445 | |
| 2446 | while (1) { |
| 2447 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 2448 | if (ret < 0) |
| 2449 | goto error; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2450 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2451 | |
| 2452 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2453 | key.type); |
| 2454 | if (ret < 0) |
| 2455 | goto error; |
| 2456 | if (ret > 0) |
| 2457 | break; |
| 2458 | |
| 2459 | leaf = path->nodes[0]; |
| 2460 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2461 | |
| 2462 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2463 | struct btrfs_chunk); |
| 2464 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2465 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2466 | |
| 2467 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2468 | ret = btrfs_relocate_chunk(chunk_root, chunk_tree, |
| 2469 | found_key.objectid, |
| 2470 | found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2471 | if (ret == -ENOSPC) |
| 2472 | failed++; |
| 2473 | else if (ret) |
| 2474 | BUG(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | if (found_key.offset == 0) |
| 2478 | break; |
| 2479 | key.offset = found_key.offset - 1; |
| 2480 | } |
| 2481 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2482 | if (failed && !retried) { |
| 2483 | failed = 0; |
| 2484 | retried = true; |
| 2485 | goto again; |
| 2486 | } else if (failed && retried) { |
| 2487 | WARN_ON(1); |
| 2488 | ret = -ENOSPC; |
| 2489 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2490 | error: |
| 2491 | btrfs_free_path(path); |
| 2492 | return ret; |
| 2493 | } |
| 2494 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2495 | static int insert_balance_item(struct btrfs_root *root, |
| 2496 | struct btrfs_balance_control *bctl) |
| 2497 | { |
| 2498 | struct btrfs_trans_handle *trans; |
| 2499 | struct btrfs_balance_item *item; |
| 2500 | struct btrfs_disk_balance_args disk_bargs; |
| 2501 | struct btrfs_path *path; |
| 2502 | struct extent_buffer *leaf; |
| 2503 | struct btrfs_key key; |
| 2504 | int ret, err; |
| 2505 | |
| 2506 | path = btrfs_alloc_path(); |
| 2507 | if (!path) |
| 2508 | return -ENOMEM; |
| 2509 | |
| 2510 | trans = btrfs_start_transaction(root, 0); |
| 2511 | if (IS_ERR(trans)) { |
| 2512 | btrfs_free_path(path); |
| 2513 | return PTR_ERR(trans); |
| 2514 | } |
| 2515 | |
| 2516 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2517 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2518 | key.offset = 0; |
| 2519 | |
| 2520 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2521 | sizeof(*item)); |
| 2522 | if (ret) |
| 2523 | goto out; |
| 2524 | |
| 2525 | leaf = path->nodes[0]; |
| 2526 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 2527 | |
| 2528 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 2529 | |
| 2530 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 2531 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 2532 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 2533 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 2534 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 2535 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 2536 | |
| 2537 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 2538 | |
| 2539 | btrfs_mark_buffer_dirty(leaf); |
| 2540 | out: |
| 2541 | btrfs_free_path(path); |
| 2542 | err = btrfs_commit_transaction(trans, root); |
| 2543 | if (err && !ret) |
| 2544 | ret = err; |
| 2545 | return ret; |
| 2546 | } |
| 2547 | |
| 2548 | static int del_balance_item(struct btrfs_root *root) |
| 2549 | { |
| 2550 | struct btrfs_trans_handle *trans; |
| 2551 | struct btrfs_path *path; |
| 2552 | struct btrfs_key key; |
| 2553 | int ret, err; |
| 2554 | |
| 2555 | path = btrfs_alloc_path(); |
| 2556 | if (!path) |
| 2557 | return -ENOMEM; |
| 2558 | |
| 2559 | trans = btrfs_start_transaction(root, 0); |
| 2560 | if (IS_ERR(trans)) { |
| 2561 | btrfs_free_path(path); |
| 2562 | return PTR_ERR(trans); |
| 2563 | } |
| 2564 | |
| 2565 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2566 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2567 | key.offset = 0; |
| 2568 | |
| 2569 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2570 | if (ret < 0) |
| 2571 | goto out; |
| 2572 | if (ret > 0) { |
| 2573 | ret = -ENOENT; |
| 2574 | goto out; |
| 2575 | } |
| 2576 | |
| 2577 | ret = btrfs_del_item(trans, root, path); |
| 2578 | out: |
| 2579 | btrfs_free_path(path); |
| 2580 | err = btrfs_commit_transaction(trans, root); |
| 2581 | if (err && !ret) |
| 2582 | ret = err; |
| 2583 | return ret; |
| 2584 | } |
| 2585 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2586 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2587 | * This is a heuristic used to reduce the number of chunks balanced on |
| 2588 | * resume after balance was interrupted. |
| 2589 | */ |
| 2590 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 2591 | { |
| 2592 | /* |
| 2593 | * Turn on soft mode for chunk types that were being converted. |
| 2594 | */ |
| 2595 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2596 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2597 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2598 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2599 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2600 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2601 | |
| 2602 | /* |
| 2603 | * Turn on usage filter if is not already used. The idea is |
| 2604 | * that chunks that we have already balanced should be |
| 2605 | * reasonably full. Don't do it for chunks that are being |
| 2606 | * converted - that will keep us from relocating unconverted |
| 2607 | * (albeit full) chunks. |
| 2608 | */ |
| 2609 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2610 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2611 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2612 | bctl->data.usage = 90; |
| 2613 | } |
| 2614 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2615 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2616 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2617 | bctl->sys.usage = 90; |
| 2618 | } |
| 2619 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2620 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2621 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2622 | bctl->meta.usage = 90; |
| 2623 | } |
| 2624 | } |
| 2625 | |
| 2626 | /* |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2627 | * Should be called with both balance and volume mutexes held to |
| 2628 | * serialize other volume operations (add_dev/rm_dev/resize) with |
| 2629 | * restriper. Same goes for unset_balance_control. |
| 2630 | */ |
| 2631 | static void set_balance_control(struct btrfs_balance_control *bctl) |
| 2632 | { |
| 2633 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
| 2634 | |
| 2635 | BUG_ON(fs_info->balance_ctl); |
| 2636 | |
| 2637 | spin_lock(&fs_info->balance_lock); |
| 2638 | fs_info->balance_ctl = bctl; |
| 2639 | spin_unlock(&fs_info->balance_lock); |
| 2640 | } |
| 2641 | |
| 2642 | static void unset_balance_control(struct btrfs_fs_info *fs_info) |
| 2643 | { |
| 2644 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 2645 | |
| 2646 | BUG_ON(!fs_info->balance_ctl); |
| 2647 | |
| 2648 | spin_lock(&fs_info->balance_lock); |
| 2649 | fs_info->balance_ctl = NULL; |
| 2650 | spin_unlock(&fs_info->balance_lock); |
| 2651 | |
| 2652 | kfree(bctl); |
| 2653 | } |
| 2654 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2655 | /* |
| 2656 | * Balance filters. Return 1 if chunk should be filtered out |
| 2657 | * (should not be balanced). |
| 2658 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2659 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2660 | struct btrfs_balance_args *bargs) |
| 2661 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2662 | chunk_type = chunk_to_extended(chunk_type) & |
| 2663 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2664 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2665 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2666 | return 0; |
| 2667 | |
| 2668 | return 1; |
| 2669 | } |
| 2670 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2671 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
| 2672 | struct btrfs_balance_args *bargs) |
| 2673 | { |
| 2674 | struct btrfs_block_group_cache *cache; |
| 2675 | u64 chunk_used, user_thresh; |
| 2676 | int ret = 1; |
| 2677 | |
| 2678 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 2679 | chunk_used = btrfs_block_group_used(&cache->item); |
| 2680 | |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 2681 | if (bargs->usage == 0) |
| 2682 | user_thresh = 0; |
| 2683 | else if (bargs->usage > 100) |
| 2684 | user_thresh = cache->key.offset; |
| 2685 | else |
| 2686 | user_thresh = div_factor_fine(cache->key.offset, |
| 2687 | bargs->usage); |
| 2688 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2689 | if (chunk_used < user_thresh) |
| 2690 | ret = 0; |
| 2691 | |
| 2692 | btrfs_put_block_group(cache); |
| 2693 | return ret; |
| 2694 | } |
| 2695 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2696 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 2697 | struct btrfs_chunk *chunk, |
| 2698 | struct btrfs_balance_args *bargs) |
| 2699 | { |
| 2700 | struct btrfs_stripe *stripe; |
| 2701 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2702 | int i; |
| 2703 | |
| 2704 | for (i = 0; i < num_stripes; i++) { |
| 2705 | stripe = btrfs_stripe_nr(chunk, i); |
| 2706 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 2707 | return 0; |
| 2708 | } |
| 2709 | |
| 2710 | return 1; |
| 2711 | } |
| 2712 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2713 | /* [pstart, pend) */ |
| 2714 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 2715 | struct btrfs_chunk *chunk, |
| 2716 | u64 chunk_offset, |
| 2717 | struct btrfs_balance_args *bargs) |
| 2718 | { |
| 2719 | struct btrfs_stripe *stripe; |
| 2720 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2721 | u64 stripe_offset; |
| 2722 | u64 stripe_length; |
| 2723 | int factor; |
| 2724 | int i; |
| 2725 | |
| 2726 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 2727 | return 0; |
| 2728 | |
| 2729 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
| 2730 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) |
| 2731 | factor = 2; |
| 2732 | else |
| 2733 | factor = 1; |
| 2734 | factor = num_stripes / factor; |
| 2735 | |
| 2736 | for (i = 0; i < num_stripes; i++) { |
| 2737 | stripe = btrfs_stripe_nr(chunk, i); |
| 2738 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 2739 | continue; |
| 2740 | |
| 2741 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 2742 | stripe_length = btrfs_chunk_length(leaf, chunk); |
| 2743 | do_div(stripe_length, factor); |
| 2744 | |
| 2745 | if (stripe_offset < bargs->pend && |
| 2746 | stripe_offset + stripe_length > bargs->pstart) |
| 2747 | return 0; |
| 2748 | } |
| 2749 | |
| 2750 | return 1; |
| 2751 | } |
| 2752 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2753 | /* [vstart, vend) */ |
| 2754 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 2755 | struct btrfs_chunk *chunk, |
| 2756 | u64 chunk_offset, |
| 2757 | struct btrfs_balance_args *bargs) |
| 2758 | { |
| 2759 | if (chunk_offset < bargs->vend && |
| 2760 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 2761 | /* at least part of the chunk is inside this vrange */ |
| 2762 | return 0; |
| 2763 | |
| 2764 | return 1; |
| 2765 | } |
| 2766 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2767 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2768 | struct btrfs_balance_args *bargs) |
| 2769 | { |
| 2770 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 2771 | return 0; |
| 2772 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2773 | chunk_type = chunk_to_extended(chunk_type) & |
| 2774 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2775 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2776 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2777 | return 1; |
| 2778 | |
| 2779 | return 0; |
| 2780 | } |
| 2781 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2782 | static int should_balance_chunk(struct btrfs_root *root, |
| 2783 | struct extent_buffer *leaf, |
| 2784 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 2785 | { |
| 2786 | struct btrfs_balance_control *bctl = root->fs_info->balance_ctl; |
| 2787 | struct btrfs_balance_args *bargs = NULL; |
| 2788 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 2789 | |
| 2790 | /* type filter */ |
| 2791 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 2792 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 2793 | return 0; |
| 2794 | } |
| 2795 | |
| 2796 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 2797 | bargs = &bctl->data; |
| 2798 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2799 | bargs = &bctl->sys; |
| 2800 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 2801 | bargs = &bctl->meta; |
| 2802 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2803 | /* profiles filter */ |
| 2804 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 2805 | chunk_profiles_filter(chunk_type, bargs)) { |
| 2806 | return 0; |
| 2807 | } |
| 2808 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2809 | /* usage filter */ |
| 2810 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2811 | chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) { |
| 2812 | return 0; |
| 2813 | } |
| 2814 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2815 | /* devid filter */ |
| 2816 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 2817 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 2818 | return 0; |
| 2819 | } |
| 2820 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2821 | /* drange filter, makes sense only with devid filter */ |
| 2822 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
| 2823 | chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 2824 | return 0; |
| 2825 | } |
| 2826 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2827 | /* vrange filter */ |
| 2828 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 2829 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 2830 | return 0; |
| 2831 | } |
| 2832 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2833 | /* soft profile changing mode */ |
| 2834 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 2835 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 2836 | return 0; |
| 2837 | } |
| 2838 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2839 | return 1; |
| 2840 | } |
| 2841 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2842 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2843 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2844 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2845 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 2846 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 2847 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2848 | struct btrfs_device *device; |
| 2849 | u64 old_size; |
| 2850 | u64 size_to_free; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2851 | struct btrfs_chunk *chunk; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2852 | struct btrfs_path *path; |
| 2853 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2854 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2855 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2856 | struct extent_buffer *leaf; |
| 2857 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2858 | int ret; |
| 2859 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2860 | bool counting = true; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2861 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2862 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2863 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2864 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2865 | old_size = device->total_bytes; |
| 2866 | size_to_free = div_factor(old_size, 1); |
| 2867 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2868 | if (!device->writeable || |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2869 | device->total_bytes - device->bytes_used > size_to_free || |
| 2870 | device->is_tgtdev_for_dev_replace) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2871 | continue; |
| 2872 | |
| 2873 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2874 | if (ret == -ENOSPC) |
| 2875 | break; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2876 | BUG_ON(ret); |
| 2877 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2878 | trans = btrfs_start_transaction(dev_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2879 | BUG_ON(IS_ERR(trans)); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2880 | |
| 2881 | ret = btrfs_grow_device(trans, device, old_size); |
| 2882 | BUG_ON(ret); |
| 2883 | |
| 2884 | btrfs_end_transaction(trans, dev_root); |
| 2885 | } |
| 2886 | |
| 2887 | /* step two, relocate all the chunks */ |
| 2888 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 2889 | if (!path) { |
| 2890 | ret = -ENOMEM; |
| 2891 | goto error; |
| 2892 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2893 | |
| 2894 | /* zero out stat counters */ |
| 2895 | spin_lock(&fs_info->balance_lock); |
| 2896 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 2897 | spin_unlock(&fs_info->balance_lock); |
| 2898 | again: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2899 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2900 | key.offset = (u64)-1; |
| 2901 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2902 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2903 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2904 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2905 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2906 | ret = -ECANCELED; |
| 2907 | goto error; |
| 2908 | } |
| 2909 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2910 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 2911 | if (ret < 0) |
| 2912 | goto error; |
| 2913 | |
| 2914 | /* |
| 2915 | * this shouldn't happen, it means the last relocate |
| 2916 | * failed |
| 2917 | */ |
| 2918 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2919 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2920 | |
| 2921 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 2922 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2923 | if (ret) { |
| 2924 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2925 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2926 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2927 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2928 | leaf = path->nodes[0]; |
| 2929 | slot = path->slots[0]; |
| 2930 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2931 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2932 | if (found_key.objectid != key.objectid) |
| 2933 | break; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2934 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2935 | /* chunk zero is special */ |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2936 | if (found_key.offset == 0) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2937 | break; |
| 2938 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2939 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 2940 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2941 | if (!counting) { |
| 2942 | spin_lock(&fs_info->balance_lock); |
| 2943 | bctl->stat.considered++; |
| 2944 | spin_unlock(&fs_info->balance_lock); |
| 2945 | } |
| 2946 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2947 | ret = should_balance_chunk(chunk_root, leaf, chunk, |
| 2948 | found_key.offset); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2949 | btrfs_release_path(path); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2950 | if (!ret) |
| 2951 | goto loop; |
| 2952 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2953 | if (counting) { |
| 2954 | spin_lock(&fs_info->balance_lock); |
| 2955 | bctl->stat.expected++; |
| 2956 | spin_unlock(&fs_info->balance_lock); |
| 2957 | goto loop; |
| 2958 | } |
| 2959 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2960 | ret = btrfs_relocate_chunk(chunk_root, |
| 2961 | chunk_root->root_key.objectid, |
| 2962 | found_key.objectid, |
| 2963 | found_key.offset); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 2964 | if (ret && ret != -ENOSPC) |
| 2965 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2966 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2967 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2968 | } else { |
| 2969 | spin_lock(&fs_info->balance_lock); |
| 2970 | bctl->stat.completed++; |
| 2971 | spin_unlock(&fs_info->balance_lock); |
| 2972 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2973 | loop: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2974 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2975 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2976 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2977 | if (counting) { |
| 2978 | btrfs_release_path(path); |
| 2979 | counting = false; |
| 2980 | goto again; |
| 2981 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2982 | error: |
| 2983 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2984 | if (enospc_errors) { |
| 2985 | printk(KERN_INFO "btrfs: %d enospc errors during balance\n", |
| 2986 | enospc_errors); |
| 2987 | if (!ret) |
| 2988 | ret = -ENOSPC; |
| 2989 | } |
| 2990 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2991 | return ret; |
| 2992 | } |
| 2993 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 2994 | /** |
| 2995 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 2996 | * @flags: profile to validate |
| 2997 | * @extended: if true @flags is treated as an extended profile |
| 2998 | */ |
| 2999 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3000 | { |
| 3001 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3002 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3003 | |
| 3004 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3005 | |
| 3006 | /* 1) check that all other bits are zeroed */ |
| 3007 | if (flags & ~mask) |
| 3008 | return 0; |
| 3009 | |
| 3010 | /* 2) see if profile is reduced */ |
| 3011 | if (flags == 0) |
| 3012 | return !extended; /* "0" is valid for usual profiles */ |
| 3013 | |
| 3014 | /* true if exactly one bit set */ |
| 3015 | return (flags & (flags - 1)) == 0; |
| 3016 | } |
| 3017 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3018 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3019 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3020 | /* cancel requested || normal exit path */ |
| 3021 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3022 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3023 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3024 | } |
| 3025 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3026 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
| 3027 | { |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3028 | int ret; |
| 3029 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3030 | unset_balance_control(fs_info); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3031 | ret = del_balance_item(fs_info->tree_root); |
| 3032 | BUG_ON(ret); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3033 | |
| 3034 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3035 | } |
| 3036 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3037 | 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] | 3038 | struct btrfs_ioctl_balance_args *bargs); |
| 3039 | |
| 3040 | /* |
| 3041 | * Should be called with both balance and volume mutexes held |
| 3042 | */ |
| 3043 | int btrfs_balance(struct btrfs_balance_control *bctl, |
| 3044 | struct btrfs_ioctl_balance_args *bargs) |
| 3045 | { |
| 3046 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3047 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3048 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3049 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3050 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3051 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3052 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3053 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3054 | atomic_read(&fs_info->balance_pause_req) || |
| 3055 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3056 | ret = -EINVAL; |
| 3057 | goto out; |
| 3058 | } |
| 3059 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3060 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3061 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3062 | mixed = 1; |
| 3063 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3064 | /* |
| 3065 | * In case of mixed groups both data and meta should be picked, |
| 3066 | * and identical options should be given for both of them. |
| 3067 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3068 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3069 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3070 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3071 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3072 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
| 3073 | printk(KERN_ERR "btrfs: with mixed groups data and " |
| 3074 | "metadata balance options must be the same\n"); |
| 3075 | ret = -EINVAL; |
| 3076 | goto out; |
| 3077 | } |
| 3078 | } |
| 3079 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3080 | num_devices = fs_info->fs_devices->num_devices; |
| 3081 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 3082 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 3083 | BUG_ON(num_devices < 1); |
| 3084 | num_devices--; |
| 3085 | } |
| 3086 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3087 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3088 | if (num_devices == 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3089 | allowed |= BTRFS_BLOCK_GROUP_DUP; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3090 | else if (num_devices < 4) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3091 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
| 3092 | else |
| 3093 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 | |
| 3094 | BTRFS_BLOCK_GROUP_RAID10); |
| 3095 | |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3096 | if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3097 | (!alloc_profile_is_valid(bctl->data.target, 1) || |
| 3098 | (bctl->data.target & ~allowed))) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3099 | printk(KERN_ERR "btrfs: unable to start balance with target " |
| 3100 | "data profile %llu\n", |
| 3101 | (unsigned long long)bctl->data.target); |
| 3102 | ret = -EINVAL; |
| 3103 | goto out; |
| 3104 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3105 | if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3106 | (!alloc_profile_is_valid(bctl->meta.target, 1) || |
| 3107 | (bctl->meta.target & ~allowed))) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3108 | printk(KERN_ERR "btrfs: unable to start balance with target " |
| 3109 | "metadata profile %llu\n", |
| 3110 | (unsigned long long)bctl->meta.target); |
| 3111 | ret = -EINVAL; |
| 3112 | goto out; |
| 3113 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3114 | if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3115 | (!alloc_profile_is_valid(bctl->sys.target, 1) || |
| 3116 | (bctl->sys.target & ~allowed))) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3117 | printk(KERN_ERR "btrfs: unable to start balance with target " |
| 3118 | "system profile %llu\n", |
| 3119 | (unsigned long long)bctl->sys.target); |
| 3120 | ret = -EINVAL; |
| 3121 | goto out; |
| 3122 | } |
| 3123 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3124 | /* allow dup'ed data chunks only in mixed mode */ |
| 3125 | if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3126 | (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3127 | printk(KERN_ERR "btrfs: dup for data is not allowed\n"); |
| 3128 | ret = -EINVAL; |
| 3129 | goto out; |
| 3130 | } |
| 3131 | |
| 3132 | /* allow to reduce meta or sys integrity only if force set */ |
| 3133 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 3134 | BTRFS_BLOCK_GROUP_RAID10; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3135 | do { |
| 3136 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3137 | |
| 3138 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3139 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3140 | !(bctl->sys.target & allowed)) || |
| 3141 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3142 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3143 | !(bctl->meta.target & allowed))) { |
| 3144 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
| 3145 | printk(KERN_INFO "btrfs: force reducing metadata " |
| 3146 | "integrity\n"); |
| 3147 | } else { |
| 3148 | printk(KERN_ERR "btrfs: balance will reduce metadata " |
| 3149 | "integrity, use force if you want this\n"); |
| 3150 | ret = -EINVAL; |
| 3151 | goto out; |
| 3152 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3153 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3154 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3155 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3156 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3157 | int num_tolerated_disk_barrier_failures; |
| 3158 | u64 target = bctl->sys.target; |
| 3159 | |
| 3160 | num_tolerated_disk_barrier_failures = |
| 3161 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3162 | if (num_tolerated_disk_barrier_failures > 0 && |
| 3163 | (target & |
| 3164 | (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 | |
| 3165 | BTRFS_AVAIL_ALLOC_BIT_SINGLE))) |
| 3166 | num_tolerated_disk_barrier_failures = 0; |
| 3167 | else if (num_tolerated_disk_barrier_failures > 1 && |
| 3168 | (target & |
| 3169 | (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))) |
| 3170 | num_tolerated_disk_barrier_failures = 1; |
| 3171 | |
| 3172 | fs_info->num_tolerated_disk_barrier_failures = |
| 3173 | num_tolerated_disk_barrier_failures; |
| 3174 | } |
| 3175 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3176 | ret = insert_balance_item(fs_info->tree_root, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3177 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3178 | goto out; |
| 3179 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3180 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3181 | BUG_ON(ret == -EEXIST); |
| 3182 | set_balance_control(bctl); |
| 3183 | } else { |
| 3184 | BUG_ON(ret != -EEXIST); |
| 3185 | spin_lock(&fs_info->balance_lock); |
| 3186 | update_balance_args(bctl); |
| 3187 | spin_unlock(&fs_info->balance_lock); |
| 3188 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3189 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3190 | atomic_inc(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3191 | mutex_unlock(&fs_info->balance_mutex); |
| 3192 | |
| 3193 | ret = __btrfs_balance(fs_info); |
| 3194 | |
| 3195 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3196 | atomic_dec(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3197 | |
| 3198 | if (bargs) { |
| 3199 | memset(bargs, 0, sizeof(*bargs)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3200 | update_ioctl_balance_args(fs_info, 0, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3201 | } |
| 3202 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3203 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3204 | balance_need_close(fs_info)) { |
| 3205 | __cancel_balance(fs_info); |
| 3206 | } |
| 3207 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3208 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3209 | fs_info->num_tolerated_disk_barrier_failures = |
| 3210 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3211 | } |
| 3212 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3213 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3214 | |
| 3215 | return ret; |
| 3216 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3217 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
| 3218 | __cancel_balance(fs_info); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3219 | else { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3220 | kfree(bctl); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3221 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 3222 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3223 | return ret; |
| 3224 | } |
| 3225 | |
| 3226 | static int balance_kthread(void *data) |
| 3227 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3228 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3229 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3230 | |
| 3231 | mutex_lock(&fs_info->volume_mutex); |
| 3232 | mutex_lock(&fs_info->balance_mutex); |
| 3233 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3234 | if (fs_info->balance_ctl) { |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3235 | printk(KERN_INFO "btrfs: continuing balance\n"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3236 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3237 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3238 | |
| 3239 | mutex_unlock(&fs_info->balance_mutex); |
| 3240 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3241 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3242 | return ret; |
| 3243 | } |
| 3244 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3245 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 3246 | { |
| 3247 | struct task_struct *tsk; |
| 3248 | |
| 3249 | spin_lock(&fs_info->balance_lock); |
| 3250 | if (!fs_info->balance_ctl) { |
| 3251 | spin_unlock(&fs_info->balance_lock); |
| 3252 | return 0; |
| 3253 | } |
| 3254 | spin_unlock(&fs_info->balance_lock); |
| 3255 | |
| 3256 | if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) { |
| 3257 | printk(KERN_INFO "btrfs: force skipping balance\n"); |
| 3258 | return 0; |
| 3259 | } |
| 3260 | |
| 3261 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
| 3262 | if (IS_ERR(tsk)) |
| 3263 | return PTR_ERR(tsk); |
| 3264 | |
| 3265 | return 0; |
| 3266 | } |
| 3267 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3268 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3269 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3270 | struct btrfs_balance_control *bctl; |
| 3271 | struct btrfs_balance_item *item; |
| 3272 | struct btrfs_disk_balance_args disk_bargs; |
| 3273 | struct btrfs_path *path; |
| 3274 | struct extent_buffer *leaf; |
| 3275 | struct btrfs_key key; |
| 3276 | int ret; |
| 3277 | |
| 3278 | path = btrfs_alloc_path(); |
| 3279 | if (!path) |
| 3280 | return -ENOMEM; |
| 3281 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3282 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 3283 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 3284 | key.offset = 0; |
| 3285 | |
| 3286 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3287 | if (ret < 0) |
| 3288 | goto out; |
| 3289 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3290 | ret = 0; |
| 3291 | goto out; |
| 3292 | } |
| 3293 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3294 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3295 | if (!bctl) { |
| 3296 | ret = -ENOMEM; |
| 3297 | goto out; |
| 3298 | } |
| 3299 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3300 | leaf = path->nodes[0]; |
| 3301 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3302 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3303 | bctl->fs_info = fs_info; |
| 3304 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3305 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3306 | |
| 3307 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3308 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3309 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3310 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3311 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3312 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3313 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3314 | WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); |
| 3315 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3316 | mutex_lock(&fs_info->volume_mutex); |
| 3317 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3318 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3319 | set_balance_control(bctl); |
| 3320 | |
| 3321 | mutex_unlock(&fs_info->balance_mutex); |
| 3322 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3323 | out: |
| 3324 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3325 | return ret; |
| 3326 | } |
| 3327 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3328 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3329 | { |
| 3330 | int ret = 0; |
| 3331 | |
| 3332 | mutex_lock(&fs_info->balance_mutex); |
| 3333 | if (!fs_info->balance_ctl) { |
| 3334 | mutex_unlock(&fs_info->balance_mutex); |
| 3335 | return -ENOTCONN; |
| 3336 | } |
| 3337 | |
| 3338 | if (atomic_read(&fs_info->balance_running)) { |
| 3339 | atomic_inc(&fs_info->balance_pause_req); |
| 3340 | mutex_unlock(&fs_info->balance_mutex); |
| 3341 | |
| 3342 | wait_event(fs_info->balance_wait_q, |
| 3343 | atomic_read(&fs_info->balance_running) == 0); |
| 3344 | |
| 3345 | mutex_lock(&fs_info->balance_mutex); |
| 3346 | /* we are good with balance_ctl ripped off from under us */ |
| 3347 | BUG_ON(atomic_read(&fs_info->balance_running)); |
| 3348 | atomic_dec(&fs_info->balance_pause_req); |
| 3349 | } else { |
| 3350 | ret = -ENOTCONN; |
| 3351 | } |
| 3352 | |
| 3353 | mutex_unlock(&fs_info->balance_mutex); |
| 3354 | return ret; |
| 3355 | } |
| 3356 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3357 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 3358 | { |
| 3359 | mutex_lock(&fs_info->balance_mutex); |
| 3360 | if (!fs_info->balance_ctl) { |
| 3361 | mutex_unlock(&fs_info->balance_mutex); |
| 3362 | return -ENOTCONN; |
| 3363 | } |
| 3364 | |
| 3365 | atomic_inc(&fs_info->balance_cancel_req); |
| 3366 | /* |
| 3367 | * if we are running just wait and return, balance item is |
| 3368 | * deleted in btrfs_balance in this case |
| 3369 | */ |
| 3370 | if (atomic_read(&fs_info->balance_running)) { |
| 3371 | mutex_unlock(&fs_info->balance_mutex); |
| 3372 | wait_event(fs_info->balance_wait_q, |
| 3373 | atomic_read(&fs_info->balance_running) == 0); |
| 3374 | mutex_lock(&fs_info->balance_mutex); |
| 3375 | } else { |
| 3376 | /* __cancel_balance needs volume_mutex */ |
| 3377 | mutex_unlock(&fs_info->balance_mutex); |
| 3378 | mutex_lock(&fs_info->volume_mutex); |
| 3379 | mutex_lock(&fs_info->balance_mutex); |
| 3380 | |
| 3381 | if (fs_info->balance_ctl) |
| 3382 | __cancel_balance(fs_info); |
| 3383 | |
| 3384 | mutex_unlock(&fs_info->volume_mutex); |
| 3385 | } |
| 3386 | |
| 3387 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); |
| 3388 | atomic_dec(&fs_info->balance_cancel_req); |
| 3389 | mutex_unlock(&fs_info->balance_mutex); |
| 3390 | return 0; |
| 3391 | } |
| 3392 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3393 | /* |
| 3394 | * shrinking a device means finding all of the device extents past |
| 3395 | * the new size, and then following the back refs to the chunks. |
| 3396 | * The chunk relocation code actually frees the device extent |
| 3397 | */ |
| 3398 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 3399 | { |
| 3400 | struct btrfs_trans_handle *trans; |
| 3401 | struct btrfs_root *root = device->dev_root; |
| 3402 | struct btrfs_dev_extent *dev_extent = NULL; |
| 3403 | struct btrfs_path *path; |
| 3404 | u64 length; |
| 3405 | u64 chunk_tree; |
| 3406 | u64 chunk_objectid; |
| 3407 | u64 chunk_offset; |
| 3408 | int ret; |
| 3409 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3410 | int failed = 0; |
| 3411 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3412 | struct extent_buffer *l; |
| 3413 | struct btrfs_key key; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3414 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3415 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3416 | u64 old_size = device->total_bytes; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3417 | u64 diff = device->total_bytes - new_size; |
| 3418 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3419 | if (device->is_tgtdev_for_dev_replace) |
| 3420 | return -EINVAL; |
| 3421 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3422 | path = btrfs_alloc_path(); |
| 3423 | if (!path) |
| 3424 | return -ENOMEM; |
| 3425 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3426 | path->reada = 2; |
| 3427 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3428 | lock_chunks(root); |
| 3429 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3430 | device->total_bytes = new_size; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3431 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3432 | device->fs_devices->total_rw_bytes -= diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3433 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3434 | root->fs_info->free_chunk_space -= diff; |
| 3435 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 3436 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3437 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3438 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3439 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3440 | key.objectid = device->devid; |
| 3441 | key.offset = (u64)-1; |
| 3442 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 3443 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 3444 | do { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3445 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3446 | if (ret < 0) |
| 3447 | goto done; |
| 3448 | |
| 3449 | ret = btrfs_previous_item(root, path, 0, key.type); |
| 3450 | if (ret < 0) |
| 3451 | goto done; |
| 3452 | if (ret) { |
| 3453 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3454 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 3455 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3456 | } |
| 3457 | |
| 3458 | l = path->nodes[0]; |
| 3459 | slot = path->slots[0]; |
| 3460 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 3461 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3462 | if (key.objectid != device->devid) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3463 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 3464 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3465 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3466 | |
| 3467 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 3468 | length = btrfs_dev_extent_length(l, dev_extent); |
| 3469 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3470 | if (key.offset + length <= new_size) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3471 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3472 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3473 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3474 | |
| 3475 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); |
| 3476 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); |
| 3477 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3478 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3479 | |
| 3480 | ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid, |
| 3481 | chunk_offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3482 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3483 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3484 | if (ret == -ENOSPC) |
| 3485 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 3486 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3487 | |
| 3488 | if (failed && !retried) { |
| 3489 | failed = 0; |
| 3490 | retried = true; |
| 3491 | goto again; |
| 3492 | } else if (failed && retried) { |
| 3493 | ret = -ENOSPC; |
| 3494 | lock_chunks(root); |
| 3495 | |
| 3496 | device->total_bytes = old_size; |
| 3497 | if (device->writeable) |
| 3498 | device->fs_devices->total_rw_bytes += diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3499 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3500 | root->fs_info->free_chunk_space += diff; |
| 3501 | spin_unlock(&root->fs_info->free_chunk_lock); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3502 | unlock_chunks(root); |
| 3503 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3504 | } |
| 3505 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3506 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3507 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3508 | if (IS_ERR(trans)) { |
| 3509 | ret = PTR_ERR(trans); |
| 3510 | goto done; |
| 3511 | } |
| 3512 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3513 | lock_chunks(root); |
| 3514 | |
| 3515 | device->disk_total_bytes = new_size; |
| 3516 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 3517 | ret = btrfs_update_device(trans, device); |
| 3518 | if (ret) { |
| 3519 | unlock_chunks(root); |
| 3520 | btrfs_end_transaction(trans, root); |
| 3521 | goto done; |
| 3522 | } |
| 3523 | WARN_ON(diff > old_total); |
| 3524 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
| 3525 | unlock_chunks(root); |
| 3526 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3527 | done: |
| 3528 | btrfs_free_path(path); |
| 3529 | return ret; |
| 3530 | } |
| 3531 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 3532 | static int btrfs_add_system_chunk(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3533 | struct btrfs_key *key, |
| 3534 | struct btrfs_chunk *chunk, int item_size) |
| 3535 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3536 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3537 | struct btrfs_disk_key disk_key; |
| 3538 | u32 array_size; |
| 3539 | u8 *ptr; |
| 3540 | |
| 3541 | array_size = btrfs_super_sys_array_size(super_copy); |
| 3542 | if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) |
| 3543 | return -EFBIG; |
| 3544 | |
| 3545 | ptr = super_copy->sys_chunk_array + array_size; |
| 3546 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 3547 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 3548 | ptr += sizeof(disk_key); |
| 3549 | memcpy(ptr, chunk, item_size); |
| 3550 | item_size += sizeof(disk_key); |
| 3551 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
| 3552 | return 0; |
| 3553 | } |
| 3554 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3555 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3556 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3557 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3558 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3559 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3560 | const struct btrfs_device_info *di_a = a; |
| 3561 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3562 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3563 | if (di_a->max_avail > di_b->max_avail) |
| 3564 | return -1; |
| 3565 | if (di_a->max_avail < di_b->max_avail) |
| 3566 | return 1; |
| 3567 | if (di_a->total_avail > di_b->total_avail) |
| 3568 | return -1; |
| 3569 | if (di_a->total_avail < di_b->total_avail) |
| 3570 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3571 | return 0; |
| 3572 | } |
| 3573 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 3574 | struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 3575 | [BTRFS_RAID_RAID10] = { |
| 3576 | .sub_stripes = 2, |
| 3577 | .dev_stripes = 1, |
| 3578 | .devs_max = 0, /* 0 == as many as possible */ |
| 3579 | .devs_min = 4, |
| 3580 | .devs_increment = 2, |
| 3581 | .ncopies = 2, |
| 3582 | }, |
| 3583 | [BTRFS_RAID_RAID1] = { |
| 3584 | .sub_stripes = 1, |
| 3585 | .dev_stripes = 1, |
| 3586 | .devs_max = 2, |
| 3587 | .devs_min = 2, |
| 3588 | .devs_increment = 2, |
| 3589 | .ncopies = 2, |
| 3590 | }, |
| 3591 | [BTRFS_RAID_DUP] = { |
| 3592 | .sub_stripes = 1, |
| 3593 | .dev_stripes = 2, |
| 3594 | .devs_max = 1, |
| 3595 | .devs_min = 1, |
| 3596 | .devs_increment = 1, |
| 3597 | .ncopies = 2, |
| 3598 | }, |
| 3599 | [BTRFS_RAID_RAID0] = { |
| 3600 | .sub_stripes = 1, |
| 3601 | .dev_stripes = 1, |
| 3602 | .devs_max = 0, |
| 3603 | .devs_min = 2, |
| 3604 | .devs_increment = 1, |
| 3605 | .ncopies = 1, |
| 3606 | }, |
| 3607 | [BTRFS_RAID_SINGLE] = { |
| 3608 | .sub_stripes = 1, |
| 3609 | .dev_stripes = 1, |
| 3610 | .devs_max = 1, |
| 3611 | .devs_min = 1, |
| 3612 | .devs_increment = 1, |
| 3613 | .ncopies = 1, |
| 3614 | }, |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 3615 | }; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 3616 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3617 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 3618 | struct btrfs_root *extent_root, |
| 3619 | struct map_lookup **map_ret, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3620 | u64 *num_bytes_out, u64 *stripe_size_out, |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3621 | u64 start, u64 type) |
| 3622 | { |
| 3623 | struct btrfs_fs_info *info = extent_root->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3624 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
| 3625 | struct list_head *cur; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3626 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3627 | struct extent_map_tree *em_tree; |
| 3628 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3629 | struct btrfs_device_info *devices_info = NULL; |
| 3630 | u64 total_avail; |
| 3631 | int num_stripes; /* total number of stripes to allocate */ |
| 3632 | int sub_stripes; /* sub_stripes info for map */ |
| 3633 | int dev_stripes; /* stripes per dev */ |
| 3634 | int devs_max; /* max devs to use */ |
| 3635 | int devs_min; /* min devs needed */ |
| 3636 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 3637 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3638 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3639 | u64 max_stripe_size; |
| 3640 | u64 max_chunk_size; |
| 3641 | u64 stripe_size; |
| 3642 | u64 num_bytes; |
| 3643 | int ndevs; |
| 3644 | int i; |
| 3645 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 3646 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3647 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3648 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3649 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3650 | if (list_empty(&fs_devices->alloc_list)) |
| 3651 | return -ENOSPC; |
| 3652 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 3653 | index = __get_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3654 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 3655 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 3656 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 3657 | devs_max = btrfs_raid_array[index].devs_max; |
| 3658 | devs_min = btrfs_raid_array[index].devs_min; |
| 3659 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 3660 | ncopies = btrfs_raid_array[index].ncopies; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3661 | |
| 3662 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 3663 | max_stripe_size = 1024 * 1024 * 1024; |
| 3664 | max_chunk_size = 10 * max_stripe_size; |
| 3665 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 3666 | /* for larger filesystems, use larger metadata chunks */ |
| 3667 | if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024) |
| 3668 | max_stripe_size = 1024 * 1024 * 1024; |
| 3669 | else |
| 3670 | max_stripe_size = 256 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3671 | max_chunk_size = max_stripe_size; |
| 3672 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Chris Mason | 96bdc7d | 2012-01-16 08:13:11 -0500 | [diff] [blame] | 3673 | max_stripe_size = 32 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3674 | max_chunk_size = 2 * max_stripe_size; |
| 3675 | } else { |
| 3676 | printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n", |
| 3677 | type); |
| 3678 | BUG_ON(1); |
| 3679 | } |
| 3680 | |
| 3681 | /* we don't want a chunk larger than 10% of writeable space */ |
| 3682 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 3683 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3684 | |
| 3685 | devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices, |
| 3686 | GFP_NOFS); |
| 3687 | if (!devices_info) |
| 3688 | return -ENOMEM; |
| 3689 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3690 | cur = fs_devices->alloc_list.next; |
| 3691 | |
| 3692 | /* |
| 3693 | * in the first pass through the devices list, we gather information |
| 3694 | * about the available holes on each device. |
| 3695 | */ |
| 3696 | ndevs = 0; |
| 3697 | while (cur != &fs_devices->alloc_list) { |
| 3698 | struct btrfs_device *device; |
| 3699 | u64 max_avail; |
| 3700 | u64 dev_offset; |
| 3701 | |
| 3702 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
| 3703 | |
| 3704 | cur = cur->next; |
| 3705 | |
| 3706 | if (!device->writeable) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 3707 | WARN(1, KERN_ERR |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3708 | "btrfs: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3709 | continue; |
| 3710 | } |
| 3711 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3712 | if (!device->in_fs_metadata || |
| 3713 | device->is_tgtdev_for_dev_replace) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3714 | continue; |
| 3715 | |
| 3716 | if (device->total_bytes > device->bytes_used) |
| 3717 | total_avail = device->total_bytes - device->bytes_used; |
| 3718 | else |
| 3719 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 3720 | |
| 3721 | /* If there is no space on this device, skip it. */ |
| 3722 | if (total_avail == 0) |
| 3723 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3724 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 3725 | ret = find_free_dev_extent(device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3726 | max_stripe_size * dev_stripes, |
| 3727 | &dev_offset, &max_avail); |
| 3728 | if (ret && ret != -ENOSPC) |
| 3729 | goto error; |
| 3730 | |
| 3731 | if (ret == 0) |
| 3732 | max_avail = max_stripe_size * dev_stripes; |
| 3733 | |
| 3734 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
| 3735 | continue; |
| 3736 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame^] | 3737 | if (ndevs == fs_devices->rw_devices) { |
| 3738 | WARN(1, "%s: found more than %llu devices\n", |
| 3739 | __func__, fs_devices->rw_devices); |
| 3740 | break; |
| 3741 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3742 | devices_info[ndevs].dev_offset = dev_offset; |
| 3743 | devices_info[ndevs].max_avail = max_avail; |
| 3744 | devices_info[ndevs].total_avail = total_avail; |
| 3745 | devices_info[ndevs].dev = device; |
| 3746 | ++ndevs; |
| 3747 | } |
| 3748 | |
| 3749 | /* |
| 3750 | * now sort the devices by hole size / available space |
| 3751 | */ |
| 3752 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 3753 | btrfs_cmp_device_info, NULL); |
| 3754 | |
| 3755 | /* round down to number of usable stripes */ |
| 3756 | ndevs -= ndevs % devs_increment; |
| 3757 | |
| 3758 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
| 3759 | ret = -ENOSPC; |
| 3760 | goto error; |
| 3761 | } |
| 3762 | |
| 3763 | if (devs_max && ndevs > devs_max) |
| 3764 | ndevs = devs_max; |
| 3765 | /* |
| 3766 | * the primary goal is to maximize the number of stripes, so use as many |
| 3767 | * devices as possible, even if the stripes are not maximum sized. |
| 3768 | */ |
| 3769 | stripe_size = devices_info[ndevs-1].max_avail; |
| 3770 | num_stripes = ndevs * dev_stripes; |
| 3771 | |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 3772 | if (stripe_size * ndevs > max_chunk_size * ncopies) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3773 | stripe_size = max_chunk_size * ncopies; |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 3774 | do_div(stripe_size, ndevs); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | do_div(stripe_size, dev_stripes); |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 3778 | |
| 3779 | /* align to BTRFS_STRIPE_LEN */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3780 | do_div(stripe_size, BTRFS_STRIPE_LEN); |
| 3781 | stripe_size *= BTRFS_STRIPE_LEN; |
| 3782 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3783 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 3784 | if (!map) { |
| 3785 | ret = -ENOMEM; |
| 3786 | goto error; |
| 3787 | } |
| 3788 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 3789 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3790 | for (i = 0; i < ndevs; ++i) { |
| 3791 | for (j = 0; j < dev_stripes; ++j) { |
| 3792 | int s = i * dev_stripes + j; |
| 3793 | map->stripes[s].dev = devices_info[i].dev; |
| 3794 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 3795 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 3796 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3797 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3798 | map->sector_size = extent_root->sectorsize; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3799 | map->stripe_len = BTRFS_STRIPE_LEN; |
| 3800 | map->io_align = BTRFS_STRIPE_LEN; |
| 3801 | map->io_width = BTRFS_STRIPE_LEN; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3802 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 3803 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3804 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3805 | *map_ret = map; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3806 | num_bytes = stripe_size * (num_stripes / ncopies); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3807 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3808 | *stripe_size_out = stripe_size; |
| 3809 | *num_bytes_out = num_bytes; |
| 3810 | |
| 3811 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3812 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 3813 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3814 | if (!em) { |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3815 | ret = -ENOMEM; |
| 3816 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3817 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3818 | em->bdev = (struct block_device *)map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3819 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3820 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3821 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3822 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3823 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3824 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3825 | write_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3826 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3827 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 3828 | if (ret) { |
| 3829 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 3830 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 3831 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3832 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3833 | for (i = 0; i < map->num_stripes; ++i) { |
| 3834 | struct btrfs_device *device; |
| 3835 | u64 dev_offset; |
| 3836 | |
| 3837 | device = map->stripes[i].dev; |
| 3838 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3839 | |
| 3840 | ret = btrfs_alloc_dev_extent(trans, device, |
| 3841 | info->chunk_root->root_key.objectid, |
| 3842 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3843 | start, dev_offset, stripe_size); |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 3844 | if (ret) |
| 3845 | goto error_dev_extent; |
| 3846 | } |
| 3847 | |
| 3848 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 3849 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 3850 | start, num_bytes); |
| 3851 | if (ret) { |
| 3852 | i = map->num_stripes - 1; |
| 3853 | goto error_dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3854 | } |
| 3855 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 3856 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3857 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3858 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3859 | |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 3860 | error_dev_extent: |
| 3861 | for (; i >= 0; i--) { |
| 3862 | struct btrfs_device *device; |
| 3863 | int err; |
| 3864 | |
| 3865 | device = map->stripes[i].dev; |
| 3866 | err = btrfs_free_dev_extent(trans, device, start); |
| 3867 | if (err) { |
| 3868 | btrfs_abort_transaction(trans, extent_root, err); |
| 3869 | break; |
| 3870 | } |
| 3871 | } |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 3872 | write_lock(&em_tree->lock); |
| 3873 | remove_extent_mapping(em_tree, em); |
| 3874 | write_unlock(&em_tree->lock); |
| 3875 | |
| 3876 | /* One for our allocation */ |
| 3877 | free_extent_map(em); |
| 3878 | /* One for the tree reference */ |
| 3879 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3880 | error: |
| 3881 | kfree(map); |
| 3882 | kfree(devices_info); |
| 3883 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | static int __finish_chunk_alloc(struct btrfs_trans_handle *trans, |
| 3887 | struct btrfs_root *extent_root, |
| 3888 | struct map_lookup *map, u64 chunk_offset, |
| 3889 | u64 chunk_size, u64 stripe_size) |
| 3890 | { |
| 3891 | u64 dev_offset; |
| 3892 | struct btrfs_key key; |
| 3893 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 3894 | struct btrfs_device *device; |
| 3895 | struct btrfs_chunk *chunk; |
| 3896 | struct btrfs_stripe *stripe; |
| 3897 | size_t item_size = btrfs_chunk_item_size(map->num_stripes); |
| 3898 | int index = 0; |
| 3899 | int ret; |
| 3900 | |
| 3901 | chunk = kzalloc(item_size, GFP_NOFS); |
| 3902 | if (!chunk) |
| 3903 | return -ENOMEM; |
| 3904 | |
| 3905 | index = 0; |
| 3906 | while (index < map->num_stripes) { |
| 3907 | device = map->stripes[index].dev; |
| 3908 | device->bytes_used += stripe_size; |
| 3909 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 3910 | if (ret) |
| 3911 | goto out_free; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3912 | index++; |
| 3913 | } |
| 3914 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3915 | spin_lock(&extent_root->fs_info->free_chunk_lock); |
| 3916 | extent_root->fs_info->free_chunk_space -= (stripe_size * |
| 3917 | map->num_stripes); |
| 3918 | spin_unlock(&extent_root->fs_info->free_chunk_lock); |
| 3919 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3920 | index = 0; |
| 3921 | stripe = &chunk->stripe; |
| 3922 | while (index < map->num_stripes) { |
| 3923 | device = map->stripes[index].dev; |
| 3924 | dev_offset = map->stripes[index].physical; |
| 3925 | |
| 3926 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 3927 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 3928 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 3929 | stripe++; |
| 3930 | index++; |
| 3931 | } |
| 3932 | |
| 3933 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 3934 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 3935 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 3936 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 3937 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 3938 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 3939 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 3940 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 3941 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 3942 | |
| 3943 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3944 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3945 | key.offset = chunk_offset; |
| 3946 | |
| 3947 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3948 | |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 3949 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 3950 | /* |
| 3951 | * TODO: Cleanup of inserted chunk root in case of |
| 3952 | * failure. |
| 3953 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 3954 | ret = btrfs_add_system_chunk(chunk_root, &key, chunk, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3955 | item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3956 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3957 | |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 3958 | out_free: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3959 | kfree(chunk); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 3960 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | /* |
| 3964 | * Chunk allocation falls into two parts. The first part does works |
| 3965 | * that make the new allocated chunk useable, but not do any operation |
| 3966 | * that modifies the chunk tree. The second part does the works that |
| 3967 | * require modifying the chunk tree. This division is important for the |
| 3968 | * bootstrap process of adding storage to a seed btrfs. |
| 3969 | */ |
| 3970 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 3971 | struct btrfs_root *extent_root, u64 type) |
| 3972 | { |
| 3973 | u64 chunk_offset; |
| 3974 | u64 chunk_size; |
| 3975 | u64 stripe_size; |
| 3976 | struct map_lookup *map; |
| 3977 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 3978 | int ret; |
| 3979 | |
| 3980 | ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 3981 | &chunk_offset); |
| 3982 | if (ret) |
| 3983 | return ret; |
| 3984 | |
| 3985 | ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size, |
| 3986 | &stripe_size, chunk_offset, type); |
| 3987 | if (ret) |
| 3988 | return ret; |
| 3989 | |
| 3990 | ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, |
| 3991 | chunk_size, stripe_size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3992 | if (ret) |
| 3993 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3994 | return 0; |
| 3995 | } |
| 3996 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3997 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3998 | struct btrfs_root *root, |
| 3999 | struct btrfs_device *device) |
| 4000 | { |
| 4001 | u64 chunk_offset; |
| 4002 | u64 sys_chunk_offset; |
| 4003 | u64 chunk_size; |
| 4004 | u64 sys_chunk_size; |
| 4005 | u64 stripe_size; |
| 4006 | u64 sys_stripe_size; |
| 4007 | u64 alloc_profile; |
| 4008 | struct map_lookup *map; |
| 4009 | struct map_lookup *sys_map; |
| 4010 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4011 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4012 | int ret; |
| 4013 | |
| 4014 | ret = find_next_chunk(fs_info->chunk_root, |
| 4015 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset); |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 4016 | if (ret) |
| 4017 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4018 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4019 | alloc_profile = btrfs_get_alloc_profile(extent_root, 0); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4020 | ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size, |
| 4021 | &stripe_size, chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4022 | if (ret) |
| 4023 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4024 | |
| 4025 | sys_chunk_offset = chunk_offset + chunk_size; |
| 4026 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4027 | alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4028 | ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map, |
| 4029 | &sys_chunk_size, &sys_stripe_size, |
| 4030 | sys_chunk_offset, alloc_profile); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4031 | if (ret) { |
| 4032 | btrfs_abort_transaction(trans, root, ret); |
| 4033 | goto out; |
| 4034 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4035 | |
| 4036 | ret = btrfs_add_device(trans, fs_info->chunk_root, device); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4037 | if (ret) { |
| 4038 | btrfs_abort_transaction(trans, root, ret); |
| 4039 | goto out; |
| 4040 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4041 | |
| 4042 | /* |
| 4043 | * Modifying chunk tree needs allocating new blocks from both |
| 4044 | * system block group and metadata block group. So we only can |
| 4045 | * do operations require modifying the chunk tree after both |
| 4046 | * block groups were created. |
| 4047 | */ |
| 4048 | ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, |
| 4049 | chunk_size, stripe_size); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4050 | if (ret) { |
| 4051 | btrfs_abort_transaction(trans, root, ret); |
| 4052 | goto out; |
| 4053 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4054 | |
| 4055 | ret = __finish_chunk_alloc(trans, extent_root, sys_map, |
| 4056 | sys_chunk_offset, sys_chunk_size, |
| 4057 | sys_stripe_size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4058 | if (ret) |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4059 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4060 | |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4061 | out: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4062 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4063 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4064 | } |
| 4065 | |
| 4066 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 4067 | { |
| 4068 | struct extent_map *em; |
| 4069 | struct map_lookup *map; |
| 4070 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 4071 | int readonly = 0; |
| 4072 | int i; |
| 4073 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4074 | read_lock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4075 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4076 | read_unlock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4077 | if (!em) |
| 4078 | return 1; |
| 4079 | |
Josef Bacik | f48b907 | 2010-01-27 02:07:59 +0000 | [diff] [blame] | 4080 | if (btrfs_test_opt(root, DEGRADED)) { |
| 4081 | free_extent_map(em); |
| 4082 | return 0; |
| 4083 | } |
| 4084 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4085 | map = (struct map_lookup *)em->bdev; |
| 4086 | for (i = 0; i < map->num_stripes; i++) { |
| 4087 | if (!map->stripes[i].dev->writeable) { |
| 4088 | readonly = 1; |
| 4089 | break; |
| 4090 | } |
| 4091 | } |
| 4092 | free_extent_map(em); |
| 4093 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 4097 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 4098 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 4102 | { |
| 4103 | struct extent_map *em; |
| 4104 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4105 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4106 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4107 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 4108 | if (em) |
| 4109 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4110 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4111 | if (!em) |
| 4112 | break; |
| 4113 | kfree(em->bdev); |
| 4114 | /* once for us */ |
| 4115 | free_extent_map(em); |
| 4116 | /* once for the tree */ |
| 4117 | free_extent_map(em); |
| 4118 | } |
| 4119 | } |
| 4120 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4121 | int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4122 | { |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4123 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4124 | struct extent_map *em; |
| 4125 | struct map_lookup *map; |
| 4126 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4127 | int ret; |
| 4128 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4129 | read_lock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4130 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4131 | read_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4132 | BUG_ON(!em); |
| 4133 | |
| 4134 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4135 | map = (struct map_lookup *)em->bdev; |
| 4136 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 4137 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4138 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4139 | ret = map->sub_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4140 | else |
| 4141 | ret = 1; |
| 4142 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4143 | |
| 4144 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 4145 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) |
| 4146 | ret++; |
| 4147 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
| 4148 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4149 | return ret; |
| 4150 | } |
| 4151 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4152 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
| 4153 | struct map_lookup *map, int first, int num, |
| 4154 | int optimal, int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4155 | { |
| 4156 | int i; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4157 | int tolerance; |
| 4158 | struct btrfs_device *srcdev; |
| 4159 | |
| 4160 | if (dev_replace_is_ongoing && |
| 4161 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 4162 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 4163 | srcdev = fs_info->dev_replace.srcdev; |
| 4164 | else |
| 4165 | srcdev = NULL; |
| 4166 | |
| 4167 | /* |
| 4168 | * try to avoid the drive that is the source drive for a |
| 4169 | * dev-replace procedure, only choose it if no other non-missing |
| 4170 | * mirror is available |
| 4171 | */ |
| 4172 | for (tolerance = 0; tolerance < 2; tolerance++) { |
| 4173 | if (map->stripes[optimal].dev->bdev && |
| 4174 | (tolerance || map->stripes[optimal].dev != srcdev)) |
| 4175 | return optimal; |
| 4176 | for (i = first; i < first + num; i++) { |
| 4177 | if (map->stripes[i].dev->bdev && |
| 4178 | (tolerance || map->stripes[i].dev != srcdev)) |
| 4179 | return i; |
| 4180 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4181 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4182 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4183 | /* we couldn't find one that doesn't fail. Just return something |
| 4184 | * and the io error handling code will clean up eventually |
| 4185 | */ |
| 4186 | return optimal; |
| 4187 | } |
| 4188 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4189 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4190 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4191 | struct btrfs_bio **bbio_ret, |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 4192 | int mirror_num) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4193 | { |
| 4194 | struct extent_map *em; |
| 4195 | struct map_lookup *map; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4196 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4197 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4198 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4199 | u64 stripe_offset; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4200 | u64 stripe_end_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4201 | u64 stripe_nr; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4202 | u64 stripe_nr_orig; |
| 4203 | u64 stripe_nr_end; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4204 | int stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4205 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4206 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4207 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 4208 | int max_errors = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4209 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4210 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 4211 | int dev_replace_is_ongoing = 0; |
| 4212 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4213 | int patch_the_first_stripe_for_dev_replace = 0; |
| 4214 | u64 physical_to_patch_in_first_stripe = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4215 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4216 | read_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4217 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4218 | read_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4219 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4220 | if (!em) { |
Daniel J Blueman | 4894066 | 2012-05-07 06:35:38 -0600 | [diff] [blame] | 4221 | printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4222 | (unsigned long long)logical, |
| 4223 | (unsigned long long)*length); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4224 | BUG(); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4225 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4226 | |
| 4227 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4228 | map = (struct map_lookup *)em->bdev; |
| 4229 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4230 | |
| 4231 | stripe_nr = offset; |
| 4232 | /* |
| 4233 | * stripe_nr counts the total number of stripes we have to stride |
| 4234 | * to get to this block |
| 4235 | */ |
| 4236 | do_div(stripe_nr, map->stripe_len); |
| 4237 | |
| 4238 | stripe_offset = stripe_nr * map->stripe_len; |
| 4239 | BUG_ON(offset < stripe_offset); |
| 4240 | |
| 4241 | /* stripe_offset is the offset of this block in its stripe*/ |
| 4242 | stripe_offset = offset - stripe_offset; |
| 4243 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4244 | if (rw & REQ_DISCARD) |
| 4245 | *length = min_t(u64, em->len - offset, *length); |
Ilya Dryomov | 52ba692 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4246 | else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4247 | /* we limit the length of each bio to what fits in a stripe */ |
| 4248 | *length = min_t(u64, em->len - offset, |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4249 | map->stripe_len - stripe_offset); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4250 | } else { |
| 4251 | *length = em->len - offset; |
| 4252 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4253 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4254 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4255 | goto out; |
| 4256 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4257 | btrfs_dev_replace_lock(dev_replace); |
| 4258 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 4259 | if (!dev_replace_is_ongoing) |
| 4260 | btrfs_dev_replace_unlock(dev_replace); |
| 4261 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4262 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
| 4263 | !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) && |
| 4264 | dev_replace->tgtdev != NULL) { |
| 4265 | /* |
| 4266 | * in dev-replace case, for repair case (that's the only |
| 4267 | * case where the mirror is selected explicitly when |
| 4268 | * calling btrfs_map_block), blocks left of the left cursor |
| 4269 | * can also be read from the target drive. |
| 4270 | * For REQ_GET_READ_MIRRORS, the target drive is added as |
| 4271 | * the last one to the array of stripes. For READ, it also |
| 4272 | * needs to be supported using the same mirror number. |
| 4273 | * If the requested block is not left of the left cursor, |
| 4274 | * EIO is returned. This can happen because btrfs_num_copies() |
| 4275 | * returns one more in the dev-replace case. |
| 4276 | */ |
| 4277 | u64 tmp_length = *length; |
| 4278 | struct btrfs_bio *tmp_bbio = NULL; |
| 4279 | int tmp_num_stripes; |
| 4280 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 4281 | int index_srcdev = 0; |
| 4282 | int found = 0; |
| 4283 | u64 physical_of_found = 0; |
| 4284 | |
| 4285 | ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, |
| 4286 | logical, &tmp_length, &tmp_bbio, 0); |
| 4287 | if (ret) { |
| 4288 | WARN_ON(tmp_bbio != NULL); |
| 4289 | goto out; |
| 4290 | } |
| 4291 | |
| 4292 | tmp_num_stripes = tmp_bbio->num_stripes; |
| 4293 | if (mirror_num > tmp_num_stripes) { |
| 4294 | /* |
| 4295 | * REQ_GET_READ_MIRRORS does not contain this |
| 4296 | * mirror, that means that the requested area |
| 4297 | * is not left of the left cursor |
| 4298 | */ |
| 4299 | ret = -EIO; |
| 4300 | kfree(tmp_bbio); |
| 4301 | goto out; |
| 4302 | } |
| 4303 | |
| 4304 | /* |
| 4305 | * process the rest of the function using the mirror_num |
| 4306 | * of the source drive. Therefore look it up first. |
| 4307 | * At the end, patch the device pointer to the one of the |
| 4308 | * target drive. |
| 4309 | */ |
| 4310 | for (i = 0; i < tmp_num_stripes; i++) { |
| 4311 | if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) { |
| 4312 | /* |
| 4313 | * In case of DUP, in order to keep it |
| 4314 | * simple, only add the mirror with the |
| 4315 | * lowest physical address |
| 4316 | */ |
| 4317 | if (found && |
| 4318 | physical_of_found <= |
| 4319 | tmp_bbio->stripes[i].physical) |
| 4320 | continue; |
| 4321 | index_srcdev = i; |
| 4322 | found = 1; |
| 4323 | physical_of_found = |
| 4324 | tmp_bbio->stripes[i].physical; |
| 4325 | } |
| 4326 | } |
| 4327 | |
| 4328 | if (found) { |
| 4329 | mirror_num = index_srcdev + 1; |
| 4330 | patch_the_first_stripe_for_dev_replace = 1; |
| 4331 | physical_to_patch_in_first_stripe = physical_of_found; |
| 4332 | } else { |
| 4333 | WARN_ON(1); |
| 4334 | ret = -EIO; |
| 4335 | kfree(tmp_bbio); |
| 4336 | goto out; |
| 4337 | } |
| 4338 | |
| 4339 | kfree(tmp_bbio); |
| 4340 | } else if (mirror_num > map->num_stripes) { |
| 4341 | mirror_num = 0; |
| 4342 | } |
| 4343 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4344 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4345 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4346 | stripe_nr_orig = stripe_nr; |
| 4347 | stripe_nr_end = (offset + *length + map->stripe_len - 1) & |
| 4348 | (~(map->stripe_len - 1)); |
| 4349 | do_div(stripe_nr_end, map->stripe_len); |
| 4350 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 4351 | (offset + *length); |
| 4352 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 4353 | if (rw & REQ_DISCARD) |
| 4354 | num_stripes = min_t(u64, map->num_stripes, |
| 4355 | stripe_nr_end - stripe_nr_orig); |
| 4356 | stripe_index = do_div(stripe_nr, map->num_stripes); |
| 4357 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 4358 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4359 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 4360 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4361 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4362 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4363 | stripe_index = find_live_mirror(fs_info, map, 0, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4364 | map->num_stripes, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4365 | current->pid % map->num_stripes, |
| 4366 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4367 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4368 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 4369 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4370 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 4371 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4372 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4373 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4374 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4375 | } else { |
| 4376 | mirror_num = 1; |
| 4377 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 4378 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4379 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 4380 | int factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4381 | |
| 4382 | stripe_index = do_div(stripe_nr, factor); |
| 4383 | stripe_index *= map->sub_stripes; |
| 4384 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 4385 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4386 | num_stripes = map->sub_stripes; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4387 | else if (rw & REQ_DISCARD) |
| 4388 | num_stripes = min_t(u64, map->sub_stripes * |
| 4389 | (stripe_nr_end - stripe_nr_orig), |
| 4390 | map->num_stripes); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4391 | else if (mirror_num) |
| 4392 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4393 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 4394 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4395 | stripe_index = find_live_mirror(fs_info, map, |
| 4396 | stripe_index, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4397 | map->sub_stripes, stripe_index + |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4398 | current->pid % map->sub_stripes, |
| 4399 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 4400 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4401 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4402 | } else { |
| 4403 | /* |
| 4404 | * after this do_div call, stripe_nr is the number of stripes |
| 4405 | * on this device we have to walk to find the data, and |
| 4406 | * stripe_index is the number of our device in the stripe array |
| 4407 | */ |
| 4408 | stripe_index = do_div(stripe_nr, map->num_stripes); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4409 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4410 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4411 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4412 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4413 | num_alloc_stripes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4414 | if (dev_replace_is_ongoing) { |
| 4415 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
| 4416 | num_alloc_stripes <<= 1; |
| 4417 | if (rw & REQ_GET_READ_MIRRORS) |
| 4418 | num_alloc_stripes++; |
| 4419 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4420 | bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4421 | if (!bbio) { |
| 4422 | ret = -ENOMEM; |
| 4423 | goto out; |
| 4424 | } |
| 4425 | atomic_set(&bbio->error, 0); |
| 4426 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4427 | if (rw & REQ_DISCARD) { |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4428 | int factor = 0; |
| 4429 | int sub_stripes = 0; |
| 4430 | u64 stripes_per_dev = 0; |
| 4431 | u32 remaining_stripes = 0; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4432 | u32 last_stripe = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4433 | |
| 4434 | if (map->type & |
| 4435 | (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 4436 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 4437 | sub_stripes = 1; |
| 4438 | else |
| 4439 | sub_stripes = map->sub_stripes; |
| 4440 | |
| 4441 | factor = map->num_stripes / sub_stripes; |
| 4442 | stripes_per_dev = div_u64_rem(stripe_nr_end - |
| 4443 | stripe_nr_orig, |
| 4444 | factor, |
| 4445 | &remaining_stripes); |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4446 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 4447 | last_stripe *= sub_stripes; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4448 | } |
| 4449 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4450 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4451 | bbio->stripes[i].physical = |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4452 | map->stripes[stripe_index].physical + |
| 4453 | stripe_offset + stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4454 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4455 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4456 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 4457 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 4458 | bbio->stripes[i].length = stripes_per_dev * |
| 4459 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4460 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4461 | if (i / sub_stripes < remaining_stripes) |
| 4462 | bbio->stripes[i].length += |
| 4463 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4464 | |
| 4465 | /* |
| 4466 | * Special for the first stripe and |
| 4467 | * the last stripe: |
| 4468 | * |
| 4469 | * |-------|...|-------| |
| 4470 | * |----------| |
| 4471 | * off end_off |
| 4472 | */ |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4473 | if (i < sub_stripes) |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4474 | bbio->stripes[i].length -= |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4475 | stripe_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4476 | |
| 4477 | if (stripe_index >= last_stripe && |
| 4478 | stripe_index <= (last_stripe + |
| 4479 | sub_stripes - 1)) |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4480 | bbio->stripes[i].length -= |
| 4481 | stripe_end_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4482 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4483 | if (i == sub_stripes - 1) |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4484 | stripe_offset = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4485 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4486 | bbio->stripes[i].length = *length; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4487 | |
| 4488 | stripe_index++; |
| 4489 | if (stripe_index == map->num_stripes) { |
| 4490 | /* This could only happen for RAID0/10 */ |
| 4491 | stripe_index = 0; |
| 4492 | stripe_nr++; |
| 4493 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4494 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4495 | } else { |
| 4496 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4497 | bbio->stripes[i].physical = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 4498 | map->stripes[stripe_index].physical + |
| 4499 | stripe_offset + |
| 4500 | stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4501 | bbio->stripes[i].dev = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 4502 | map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4503 | stripe_index++; |
| 4504 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4505 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4506 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 4507 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) { |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4508 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 4509 | BTRFS_BLOCK_GROUP_RAID10 | |
| 4510 | BTRFS_BLOCK_GROUP_DUP)) { |
| 4511 | max_errors = 1; |
| 4512 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4513 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4514 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4515 | if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) && |
| 4516 | dev_replace->tgtdev != NULL) { |
| 4517 | int index_where_to_add; |
| 4518 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 4519 | |
| 4520 | /* |
| 4521 | * duplicate the write operations while the dev replace |
| 4522 | * procedure is running. Since the copying of the old disk |
| 4523 | * to the new disk takes place at run time while the |
| 4524 | * filesystem is mounted writable, the regular write |
| 4525 | * operations to the old disk have to be duplicated to go |
| 4526 | * to the new disk as well. |
| 4527 | * Note that device->missing is handled by the caller, and |
| 4528 | * that the write to the old disk is already set up in the |
| 4529 | * stripes array. |
| 4530 | */ |
| 4531 | index_where_to_add = num_stripes; |
| 4532 | for (i = 0; i < num_stripes; i++) { |
| 4533 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 4534 | /* write to new disk, too */ |
| 4535 | struct btrfs_bio_stripe *new = |
| 4536 | bbio->stripes + index_where_to_add; |
| 4537 | struct btrfs_bio_stripe *old = |
| 4538 | bbio->stripes + i; |
| 4539 | |
| 4540 | new->physical = old->physical; |
| 4541 | new->length = old->length; |
| 4542 | new->dev = dev_replace->tgtdev; |
| 4543 | index_where_to_add++; |
| 4544 | max_errors++; |
| 4545 | } |
| 4546 | } |
| 4547 | num_stripes = index_where_to_add; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4548 | } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) && |
| 4549 | dev_replace->tgtdev != NULL) { |
| 4550 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 4551 | int index_srcdev = 0; |
| 4552 | int found = 0; |
| 4553 | u64 physical_of_found = 0; |
| 4554 | |
| 4555 | /* |
| 4556 | * During the dev-replace procedure, the target drive can |
| 4557 | * also be used to read data in case it is needed to repair |
| 4558 | * a corrupt block elsewhere. This is possible if the |
| 4559 | * requested area is left of the left cursor. In this area, |
| 4560 | * the target drive is a full copy of the source drive. |
| 4561 | */ |
| 4562 | for (i = 0; i < num_stripes; i++) { |
| 4563 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 4564 | /* |
| 4565 | * In case of DUP, in order to keep it |
| 4566 | * simple, only add the mirror with the |
| 4567 | * lowest physical address |
| 4568 | */ |
| 4569 | if (found && |
| 4570 | physical_of_found <= |
| 4571 | bbio->stripes[i].physical) |
| 4572 | continue; |
| 4573 | index_srcdev = i; |
| 4574 | found = 1; |
| 4575 | physical_of_found = bbio->stripes[i].physical; |
| 4576 | } |
| 4577 | } |
| 4578 | if (found) { |
| 4579 | u64 length = map->stripe_len; |
| 4580 | |
| 4581 | if (physical_of_found + length <= |
| 4582 | dev_replace->cursor_left) { |
| 4583 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 4584 | bbio->stripes + num_stripes; |
| 4585 | |
| 4586 | tgtdev_stripe->physical = physical_of_found; |
| 4587 | tgtdev_stripe->length = |
| 4588 | bbio->stripes[index_srcdev].length; |
| 4589 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 4590 | |
| 4591 | num_stripes++; |
| 4592 | } |
| 4593 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4594 | } |
| 4595 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4596 | *bbio_ret = bbio; |
| 4597 | bbio->num_stripes = num_stripes; |
| 4598 | bbio->max_errors = max_errors; |
| 4599 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4600 | |
| 4601 | /* |
| 4602 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 4603 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 4604 | * available as a mirror |
| 4605 | */ |
| 4606 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 4607 | WARN_ON(num_stripes > 1); |
| 4608 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 4609 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 4610 | bbio->mirror_num = map->num_stripes + 1; |
| 4611 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4612 | out: |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4613 | if (dev_replace_is_ongoing) |
| 4614 | btrfs_dev_replace_unlock(dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4615 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4616 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4617 | } |
| 4618 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4619 | int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4620 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4621 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4622 | { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4623 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 4624 | mirror_num); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4625 | } |
| 4626 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4627 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 4628 | u64 chunk_start, u64 physical, u64 devid, |
| 4629 | u64 **logical, int *naddrs, int *stripe_len) |
| 4630 | { |
| 4631 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4632 | struct extent_map *em; |
| 4633 | struct map_lookup *map; |
| 4634 | u64 *buf; |
| 4635 | u64 bytenr; |
| 4636 | u64 length; |
| 4637 | u64 stripe_nr; |
| 4638 | int i, j, nr = 0; |
| 4639 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4640 | read_lock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4641 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4642 | read_unlock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4643 | |
| 4644 | BUG_ON(!em || em->start != chunk_start); |
| 4645 | map = (struct map_lookup *)em->bdev; |
| 4646 | |
| 4647 | length = em->len; |
| 4648 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4649 | do_div(length, map->num_stripes / map->sub_stripes); |
| 4650 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 4651 | do_div(length, map->num_stripes); |
| 4652 | |
| 4653 | buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4654 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4655 | |
| 4656 | for (i = 0; i < map->num_stripes; i++) { |
| 4657 | if (devid && map->stripes[i].dev->devid != devid) |
| 4658 | continue; |
| 4659 | if (map->stripes[i].physical > physical || |
| 4660 | map->stripes[i].physical + length <= physical) |
| 4661 | continue; |
| 4662 | |
| 4663 | stripe_nr = physical - map->stripes[i].physical; |
| 4664 | do_div(stripe_nr, map->stripe_len); |
| 4665 | |
| 4666 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 4667 | stripe_nr = stripe_nr * map->num_stripes + i; |
| 4668 | do_div(stripe_nr, map->sub_stripes); |
| 4669 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 4670 | stripe_nr = stripe_nr * map->num_stripes + i; |
| 4671 | } |
| 4672 | bytenr = chunk_start + stripe_nr * map->stripe_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 4673 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4674 | for (j = 0; j < nr; j++) { |
| 4675 | if (buf[j] == bytenr) |
| 4676 | break; |
| 4677 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 4678 | if (j == nr) { |
| 4679 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4680 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 4681 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4682 | } |
| 4683 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4684 | *logical = buf; |
| 4685 | *naddrs = nr; |
| 4686 | *stripe_len = map->stripe_len; |
| 4687 | |
| 4688 | free_extent_map(em); |
| 4689 | return 0; |
| 4690 | } |
| 4691 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 4692 | static void *merge_stripe_index_into_bio_private(void *bi_private, |
| 4693 | unsigned int stripe_index) |
| 4694 | { |
| 4695 | /* |
| 4696 | * with single, dup, RAID0, RAID1 and RAID10, stripe_index is |
| 4697 | * at most 1. |
| 4698 | * The alternative solution (instead of stealing bits from the |
| 4699 | * pointer) would be to allocate an intermediate structure |
| 4700 | * that contains the old private pointer plus the stripe_index. |
| 4701 | */ |
| 4702 | BUG_ON((((uintptr_t)bi_private) & 3) != 0); |
| 4703 | BUG_ON(stripe_index > 3); |
| 4704 | return (void *)(((uintptr_t)bi_private) | stripe_index); |
| 4705 | } |
| 4706 | |
| 4707 | static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private) |
| 4708 | { |
| 4709 | return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3)); |
| 4710 | } |
| 4711 | |
| 4712 | static unsigned int extract_stripe_index_from_bio_private(void *bi_private) |
| 4713 | { |
| 4714 | return (unsigned int)((uintptr_t)bi_private) & 3; |
| 4715 | } |
| 4716 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4717 | static void btrfs_end_bio(struct bio *bio, int err) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4718 | { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 4719 | struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 4720 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4721 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 4722 | if (err) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4723 | atomic_inc(&bbio->error); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 4724 | if (err == -EIO || err == -EREMOTEIO) { |
| 4725 | unsigned int stripe_index = |
| 4726 | extract_stripe_index_from_bio_private( |
| 4727 | bio->bi_private); |
| 4728 | struct btrfs_device *dev; |
| 4729 | |
| 4730 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 4731 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 4732 | if (dev->bdev) { |
| 4733 | if (bio->bi_rw & WRITE) |
| 4734 | btrfs_dev_stat_inc(dev, |
| 4735 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 4736 | else |
| 4737 | btrfs_dev_stat_inc(dev, |
| 4738 | BTRFS_DEV_STAT_READ_ERRS); |
| 4739 | if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH) |
| 4740 | btrfs_dev_stat_inc(dev, |
| 4741 | BTRFS_DEV_STAT_FLUSH_ERRS); |
| 4742 | btrfs_dev_stat_print_on_error(dev); |
| 4743 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 4744 | } |
| 4745 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4746 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4747 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 4748 | is_orig_bio = 1; |
| 4749 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4750 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 4751 | if (!is_orig_bio) { |
| 4752 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4753 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 4754 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4755 | bio->bi_private = bbio->private; |
| 4756 | bio->bi_end_io = bbio->end_io; |
Jan Schmidt | 2774b2c | 2011-06-16 12:05:19 +0200 | [diff] [blame] | 4757 | bio->bi_bdev = (struct block_device *) |
| 4758 | (unsigned long)bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 4759 | /* only send an error to the higher layers if it is |
| 4760 | * beyond the tolerance of the multi-bio |
| 4761 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4762 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 4763 | err = -EIO; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 4764 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 4765 | /* |
| 4766 | * this bio is actually up to date, we didn't |
| 4767 | * go over the max number of errors |
| 4768 | */ |
| 4769 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 4770 | err = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 4771 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4772 | kfree(bbio); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4773 | |
| 4774 | bio_endio(bio, err); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 4775 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4776 | bio_put(bio); |
| 4777 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4778 | } |
| 4779 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4780 | struct async_sched { |
| 4781 | struct bio *bio; |
| 4782 | int rw; |
| 4783 | struct btrfs_fs_info *info; |
| 4784 | struct btrfs_work work; |
| 4785 | }; |
| 4786 | |
| 4787 | /* |
| 4788 | * see run_scheduled_bios for a description of why bios are collected for |
| 4789 | * async submit. |
| 4790 | * |
| 4791 | * This will add one bio to the pending list for a device and make sure |
| 4792 | * the work struct is scheduled. |
| 4793 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4794 | static noinline void schedule_bio(struct btrfs_root *root, |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 4795 | struct btrfs_device *device, |
| 4796 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4797 | { |
| 4798 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4799 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4800 | |
| 4801 | /* don't bother with additional async steps for reads, right now */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 4802 | if (!(rw & REQ_WRITE)) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 4803 | bio_get(bio); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 4804 | btrfsic_submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 4805 | bio_put(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4806 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4807 | } |
| 4808 | |
| 4809 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 4810 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4811 | * higher layers. Otherwise, the async bio makes it appear we have |
| 4812 | * made progress against dirty pages when we've really just put it |
| 4813 | * on a queue for later |
| 4814 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 4815 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 4816 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4817 | bio->bi_next = NULL; |
| 4818 | bio->bi_rw |= rw; |
| 4819 | |
| 4820 | spin_lock(&device->io_lock); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 4821 | if (bio->bi_rw & REQ_SYNC) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4822 | pending_bios = &device->pending_sync_bios; |
| 4823 | else |
| 4824 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4825 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4826 | if (pending_bios->tail) |
| 4827 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4828 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4829 | pending_bios->tail = bio; |
| 4830 | if (!pending_bios->head) |
| 4831 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4832 | if (device->running_pending) |
| 4833 | should_queue = 0; |
| 4834 | |
| 4835 | spin_unlock(&device->io_lock); |
| 4836 | |
| 4837 | if (should_queue) |
Chris Mason | 1cc127b | 2008-06-12 14:46:17 -0400 | [diff] [blame] | 4838 | btrfs_queue_worker(&root->fs_info->submit_workers, |
| 4839 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4840 | } |
| 4841 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 4842 | static int bio_size_ok(struct block_device *bdev, struct bio *bio, |
| 4843 | sector_t sector) |
| 4844 | { |
| 4845 | struct bio_vec *prev; |
| 4846 | struct request_queue *q = bdev_get_queue(bdev); |
| 4847 | unsigned short max_sectors = queue_max_sectors(q); |
| 4848 | struct bvec_merge_data bvm = { |
| 4849 | .bi_bdev = bdev, |
| 4850 | .bi_sector = sector, |
| 4851 | .bi_rw = bio->bi_rw, |
| 4852 | }; |
| 4853 | |
| 4854 | if (bio->bi_vcnt == 0) { |
| 4855 | WARN_ON(1); |
| 4856 | return 1; |
| 4857 | } |
| 4858 | |
| 4859 | prev = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
| 4860 | if ((bio->bi_size >> 9) > max_sectors) |
| 4861 | return 0; |
| 4862 | |
| 4863 | if (!q->merge_bvec_fn) |
| 4864 | return 1; |
| 4865 | |
| 4866 | bvm.bi_size = bio->bi_size - prev->bv_len; |
| 4867 | if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) |
| 4868 | return 0; |
| 4869 | return 1; |
| 4870 | } |
| 4871 | |
| 4872 | static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 4873 | struct bio *bio, u64 physical, int dev_nr, |
| 4874 | int rw, int async) |
| 4875 | { |
| 4876 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
| 4877 | |
| 4878 | bio->bi_private = bbio; |
| 4879 | bio->bi_private = merge_stripe_index_into_bio_private( |
| 4880 | bio->bi_private, (unsigned int)dev_nr); |
| 4881 | bio->bi_end_io = btrfs_end_bio; |
| 4882 | bio->bi_sector = physical >> 9; |
| 4883 | #ifdef DEBUG |
| 4884 | { |
| 4885 | struct rcu_string *name; |
| 4886 | |
| 4887 | rcu_read_lock(); |
| 4888 | name = rcu_dereference(dev->name); |
Masanari Iida | d142324 | 2012-10-31 15:16:32 +0000 | [diff] [blame] | 4889 | pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu " |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 4890 | "(%s id %llu), size=%u\n", rw, |
| 4891 | (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev, |
| 4892 | name->str, dev->devid, bio->bi_size); |
| 4893 | rcu_read_unlock(); |
| 4894 | } |
| 4895 | #endif |
| 4896 | bio->bi_bdev = dev->bdev; |
| 4897 | if (async) |
| 4898 | schedule_bio(root, dev, rw, bio); |
| 4899 | else |
| 4900 | btrfsic_submit_bio(rw, bio); |
| 4901 | } |
| 4902 | |
| 4903 | static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 4904 | struct bio *first_bio, struct btrfs_device *dev, |
| 4905 | int dev_nr, int rw, int async) |
| 4906 | { |
| 4907 | struct bio_vec *bvec = first_bio->bi_io_vec; |
| 4908 | struct bio *bio; |
| 4909 | int nr_vecs = bio_get_nr_vecs(dev->bdev); |
| 4910 | u64 physical = bbio->stripes[dev_nr].physical; |
| 4911 | |
| 4912 | again: |
| 4913 | bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS); |
| 4914 | if (!bio) |
| 4915 | return -ENOMEM; |
| 4916 | |
| 4917 | while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) { |
| 4918 | if (bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 4919 | bvec->bv_offset) < bvec->bv_len) { |
| 4920 | u64 len = bio->bi_size; |
| 4921 | |
| 4922 | atomic_inc(&bbio->stripes_pending); |
| 4923 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, |
| 4924 | rw, async); |
| 4925 | physical += len; |
| 4926 | goto again; |
| 4927 | } |
| 4928 | bvec++; |
| 4929 | } |
| 4930 | |
| 4931 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async); |
| 4932 | return 0; |
| 4933 | } |
| 4934 | |
| 4935 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 4936 | { |
| 4937 | atomic_inc(&bbio->error); |
| 4938 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
| 4939 | bio->bi_private = bbio->private; |
| 4940 | bio->bi_end_io = bbio->end_io; |
| 4941 | bio->bi_bdev = (struct block_device *) |
| 4942 | (unsigned long)bbio->mirror_num; |
| 4943 | bio->bi_sector = logical >> 9; |
| 4944 | kfree(bbio); |
| 4945 | bio_endio(bio, -EIO); |
| 4946 | } |
| 4947 | } |
| 4948 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4949 | 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] | 4950 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4951 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4952 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4953 | struct bio *first_bio = bio; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 4954 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4955 | u64 length = 0; |
| 4956 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4957 | int ret; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4958 | int dev_nr = 0; |
| 4959 | int total_devs = 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4960 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4961 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4962 | length = bio->bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4963 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4964 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4965 | ret = btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4966 | mirror_num); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 4967 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4968 | return ret; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4969 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4970 | total_devs = bbio->num_stripes; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4971 | if (map_length < length) { |
Daniel J Blueman | 4894066 | 2012-05-07 06:35:38 -0600 | [diff] [blame] | 4972 | printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu " |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4973 | "len %llu\n", (unsigned long long)logical, |
| 4974 | (unsigned long long)length, |
| 4975 | (unsigned long long)map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4976 | BUG(); |
| 4977 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4978 | |
| 4979 | bbio->orig_bio = first_bio; |
| 4980 | bbio->private = first_bio->bi_private; |
| 4981 | bbio->end_io = first_bio->bi_end_io; |
| 4982 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4983 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4984 | while (dev_nr < total_devs) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 4985 | dev = bbio->stripes[dev_nr].dev; |
| 4986 | if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) { |
| 4987 | bbio_error(bbio, first_bio, logical); |
| 4988 | dev_nr++; |
| 4989 | continue; |
| 4990 | } |
| 4991 | |
| 4992 | /* |
| 4993 | * Check and see if we're ok with this bio based on it's size |
| 4994 | * and offset with the given device. |
| 4995 | */ |
| 4996 | if (!bio_size_ok(dev->bdev, first_bio, |
| 4997 | bbio->stripes[dev_nr].physical >> 9)) { |
| 4998 | ret = breakup_stripe_bio(root, bbio, first_bio, dev, |
| 4999 | dev_nr, rw, async_submit); |
| 5000 | BUG_ON(ret); |
| 5001 | dev_nr++; |
| 5002 | continue; |
| 5003 | } |
| 5004 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5005 | if (dev_nr < total_devs - 1) { |
| 5006 | bio = bio_clone(first_bio, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5007 | BUG_ON(!bio); /* -ENOMEM */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5008 | } else { |
| 5009 | bio = first_bio; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5010 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5011 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5012 | submit_stripe_bio(root, bbio, bio, |
| 5013 | bbio->stripes[dev_nr].physical, dev_nr, rw, |
| 5014 | async_submit); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5015 | dev_nr++; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5016 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5017 | return 0; |
| 5018 | } |
| 5019 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5020 | struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5021 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5022 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5023 | struct btrfs_device *device; |
| 5024 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5025 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5026 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5027 | while (cur_devices) { |
| 5028 | if (!fsid || |
| 5029 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 5030 | device = __find_device(&cur_devices->devices, |
| 5031 | devid, uuid); |
| 5032 | if (device) |
| 5033 | return device; |
| 5034 | } |
| 5035 | cur_devices = cur_devices->seed; |
| 5036 | } |
| 5037 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5038 | } |
| 5039 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5040 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
| 5041 | u64 devid, u8 *dev_uuid) |
| 5042 | { |
| 5043 | struct btrfs_device *device; |
| 5044 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 5045 | |
| 5046 | device = kzalloc(sizeof(*device), GFP_NOFS); |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 5047 | if (!device) |
| 5048 | return NULL; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5049 | list_add(&device->dev_list, |
| 5050 | &fs_devices->devices); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5051 | device->dev_root = root->fs_info->dev_root; |
| 5052 | device->devid = devid; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5053 | device->work.func = pending_bios_fn; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5054 | device->fs_devices = fs_devices; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 5055 | device->missing = 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5056 | fs_devices->num_devices++; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 5057 | fs_devices->missing_devices++; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5058 | spin_lock_init(&device->io_lock); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 5059 | INIT_LIST_HEAD(&device->dev_alloc_list); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5060 | memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE); |
| 5061 | return device; |
| 5062 | } |
| 5063 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5064 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 5065 | struct extent_buffer *leaf, |
| 5066 | struct btrfs_chunk *chunk) |
| 5067 | { |
| 5068 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 5069 | struct map_lookup *map; |
| 5070 | struct extent_map *em; |
| 5071 | u64 logical; |
| 5072 | u64 length; |
| 5073 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5074 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5075 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5076 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5077 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5078 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5079 | logical = key->offset; |
| 5080 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5081 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5082 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5083 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5084 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5085 | |
| 5086 | /* already mapped? */ |
| 5087 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 5088 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5089 | return 0; |
| 5090 | } else if (em) { |
| 5091 | free_extent_map(em); |
| 5092 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5093 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5094 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5095 | if (!em) |
| 5096 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5097 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 5098 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5099 | if (!map) { |
| 5100 | free_extent_map(em); |
| 5101 | return -ENOMEM; |
| 5102 | } |
| 5103 | |
| 5104 | em->bdev = (struct block_device *)map; |
| 5105 | em->start = logical; |
| 5106 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 5107 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5108 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5109 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5110 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5111 | map->num_stripes = num_stripes; |
| 5112 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 5113 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 5114 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 5115 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 5116 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5117 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5118 | for (i = 0; i < num_stripes; i++) { |
| 5119 | map->stripes[i].physical = |
| 5120 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 5121 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5122 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 5123 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 5124 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5125 | map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, |
| 5126 | uuid, NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5127 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5128 | kfree(map); |
| 5129 | free_extent_map(em); |
| 5130 | return -EIO; |
| 5131 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5132 | if (!map->stripes[i].dev) { |
| 5133 | map->stripes[i].dev = |
| 5134 | add_missing_dev(root, devid, uuid); |
| 5135 | if (!map->stripes[i].dev) { |
| 5136 | kfree(map); |
| 5137 | free_extent_map(em); |
| 5138 | return -EIO; |
| 5139 | } |
| 5140 | } |
| 5141 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5142 | } |
| 5143 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5144 | write_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5145 | ret = add_extent_mapping(&map_tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5146 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5147 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5148 | free_extent_map(em); |
| 5149 | |
| 5150 | return 0; |
| 5151 | } |
| 5152 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5153 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5154 | struct btrfs_dev_item *dev_item, |
| 5155 | struct btrfs_device *device) |
| 5156 | { |
| 5157 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5158 | |
| 5159 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 5160 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 5161 | device->total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5162 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
| 5163 | device->type = btrfs_device_type(leaf, dev_item); |
| 5164 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 5165 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 5166 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 5167 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 5168 | device->is_tgtdev_for_dev_replace = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5169 | |
| 5170 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5171 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5172 | } |
| 5173 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5174 | static int open_seed_devices(struct btrfs_root *root, u8 *fsid) |
| 5175 | { |
| 5176 | struct btrfs_fs_devices *fs_devices; |
| 5177 | int ret; |
| 5178 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 5179 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5180 | |
| 5181 | fs_devices = root->fs_info->fs_devices->seed; |
| 5182 | while (fs_devices) { |
| 5183 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 5184 | ret = 0; |
| 5185 | goto out; |
| 5186 | } |
| 5187 | fs_devices = fs_devices->seed; |
| 5188 | } |
| 5189 | |
| 5190 | fs_devices = find_fsid(fsid); |
| 5191 | if (!fs_devices) { |
| 5192 | ret = -ENOENT; |
| 5193 | goto out; |
| 5194 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5195 | |
| 5196 | fs_devices = clone_fs_devices(fs_devices); |
| 5197 | if (IS_ERR(fs_devices)) { |
| 5198 | ret = PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5199 | goto out; |
| 5200 | } |
| 5201 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 5202 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 5203 | root->fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 5204 | if (ret) { |
| 5205 | free_fs_devices(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5206 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 5207 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5208 | |
| 5209 | if (!fs_devices->seeding) { |
| 5210 | __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5211 | free_fs_devices(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5212 | ret = -EINVAL; |
| 5213 | goto out; |
| 5214 | } |
| 5215 | |
| 5216 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 5217 | root->fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5218 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5219 | return ret; |
| 5220 | } |
| 5221 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5222 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5223 | struct extent_buffer *leaf, |
| 5224 | struct btrfs_dev_item *dev_item) |
| 5225 | { |
| 5226 | struct btrfs_device *device; |
| 5227 | u64 devid; |
| 5228 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5229 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5230 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 5231 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5232 | devid = btrfs_device_id(leaf, dev_item); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5233 | read_extent_buffer(leaf, dev_uuid, |
| 5234 | (unsigned long)btrfs_device_uuid(dev_item), |
| 5235 | BTRFS_UUID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5236 | read_extent_buffer(leaf, fs_uuid, |
| 5237 | (unsigned long)btrfs_device_fsid(dev_item), |
| 5238 | BTRFS_UUID_SIZE); |
| 5239 | |
| 5240 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
| 5241 | ret = open_seed_devices(root, fs_uuid); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5242 | if (ret && !btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5243 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5244 | } |
| 5245 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5246 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5247 | if (!device || !device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5248 | if (!btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5249 | return -EIO; |
| 5250 | |
| 5251 | if (!device) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5252 | printk(KERN_WARNING "warning devid %llu missing\n", |
| 5253 | (unsigned long long)devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5254 | device = add_missing_dev(root, devid, dev_uuid); |
| 5255 | if (!device) |
| 5256 | return -ENOMEM; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 5257 | } else if (!device->missing) { |
| 5258 | /* |
| 5259 | * this happens when a device that was properly setup |
| 5260 | * in the device info lists suddenly goes bad. |
| 5261 | * device->bdev is NULL, and so we have to set |
| 5262 | * device->missing to one here |
| 5263 | */ |
| 5264 | root->fs_info->fs_devices->missing_devices++; |
| 5265 | device->missing = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5266 | } |
| 5267 | } |
| 5268 | |
| 5269 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 5270 | BUG_ON(device->writeable); |
| 5271 | if (device->generation != |
| 5272 | btrfs_device_generation(leaf, dev_item)) |
| 5273 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5274 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5275 | |
| 5276 | fill_device_from_item(leaf, dev_item, device); |
| 5277 | device->dev_root = root->fs_info->dev_root; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5278 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 5279 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5280 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 5281 | spin_lock(&root->fs_info->free_chunk_lock); |
| 5282 | root->fs_info->free_chunk_space += device->total_bytes - |
| 5283 | device->bytes_used; |
| 5284 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 5285 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5286 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5287 | return ret; |
| 5288 | } |
| 5289 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5290 | int btrfs_read_sys_array(struct btrfs_root *root) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5291 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 5292 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5293 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5294 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5295 | struct btrfs_chunk *chunk; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 5296 | u8 *ptr; |
| 5297 | unsigned long sb_ptr; |
| 5298 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5299 | u32 num_stripes; |
| 5300 | u32 array_size; |
| 5301 | u32 len = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5302 | u32 cur; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 5303 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5304 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5305 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET, |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5306 | BTRFS_SUPER_INFO_SIZE); |
| 5307 | if (!sb) |
| 5308 | return -ENOMEM; |
| 5309 | btrfs_set_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 5310 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 5311 | /* |
| 5312 | * The sb extent buffer is artifical and just used to read the system array. |
| 5313 | * btrfs_set_buffer_uptodate() call does not properly mark all it's |
| 5314 | * pages up-to-date when the page is larger: extent does not cover the |
| 5315 | * whole page and consequently check_page_uptodate does not find all |
| 5316 | * the page's extents up-to-date (the hole beyond sb), |
| 5317 | * write_extent_buffer then triggers a WARN_ON. |
| 5318 | * |
| 5319 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 5320 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 5321 | * to silence the warning eg. on PowerPC 64. |
| 5322 | */ |
| 5323 | if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5324 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5325 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5326 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5327 | array_size = btrfs_super_sys_array_size(super_copy); |
| 5328 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5329 | ptr = super_copy->sys_chunk_array; |
| 5330 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 5331 | cur = 0; |
| 5332 | |
| 5333 | while (cur < array_size) { |
| 5334 | disk_key = (struct btrfs_disk_key *)ptr; |
| 5335 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 5336 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5337 | len = sizeof(*disk_key); ptr += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5338 | sb_ptr += len; |
| 5339 | cur += len; |
| 5340 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5341 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5342 | chunk = (struct btrfs_chunk *)sb_ptr; |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5343 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 5344 | if (ret) |
| 5345 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5346 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 5347 | len = btrfs_chunk_item_size(num_stripes); |
| 5348 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 5349 | ret = -EIO; |
| 5350 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5351 | } |
| 5352 | ptr += len; |
| 5353 | sb_ptr += len; |
| 5354 | cur += len; |
| 5355 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5356 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 5357 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5358 | } |
| 5359 | |
| 5360 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 5361 | { |
| 5362 | struct btrfs_path *path; |
| 5363 | struct extent_buffer *leaf; |
| 5364 | struct btrfs_key key; |
| 5365 | struct btrfs_key found_key; |
| 5366 | int ret; |
| 5367 | int slot; |
| 5368 | |
| 5369 | root = root->fs_info->chunk_root; |
| 5370 | |
| 5371 | path = btrfs_alloc_path(); |
| 5372 | if (!path) |
| 5373 | return -ENOMEM; |
| 5374 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 5375 | mutex_lock(&uuid_mutex); |
| 5376 | lock_chunks(root); |
| 5377 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5378 | /* first we search for all of the device items, and then we |
| 5379 | * read in all of the chunk items. This way we can create chunk |
| 5380 | * mappings that reference all of the devices that are afound |
| 5381 | */ |
| 5382 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 5383 | key.offset = 0; |
| 5384 | key.type = 0; |
| 5385 | again: |
| 5386 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 5387 | if (ret < 0) |
| 5388 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5389 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5390 | leaf = path->nodes[0]; |
| 5391 | slot = path->slots[0]; |
| 5392 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5393 | ret = btrfs_next_leaf(root, path); |
| 5394 | if (ret == 0) |
| 5395 | continue; |
| 5396 | if (ret < 0) |
| 5397 | goto error; |
| 5398 | break; |
| 5399 | } |
| 5400 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5401 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { |
| 5402 | if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID) |
| 5403 | break; |
| 5404 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 5405 | struct btrfs_dev_item *dev_item; |
| 5406 | dev_item = btrfs_item_ptr(leaf, slot, |
| 5407 | struct btrfs_dev_item); |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5408 | ret = read_one_dev(root, leaf, dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5409 | if (ret) |
| 5410 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5411 | } |
| 5412 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 5413 | struct btrfs_chunk *chunk; |
| 5414 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 5415 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5416 | if (ret) |
| 5417 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5418 | } |
| 5419 | path->slots[0]++; |
| 5420 | } |
| 5421 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { |
| 5422 | key.objectid = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5423 | btrfs_release_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5424 | goto again; |
| 5425 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5426 | ret = 0; |
| 5427 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 5428 | unlock_chunks(root); |
| 5429 | mutex_unlock(&uuid_mutex); |
| 5430 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5431 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5432 | return ret; |
| 5433 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5434 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5435 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 5436 | { |
| 5437 | int i; |
| 5438 | |
| 5439 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 5440 | btrfs_dev_stat_reset(dev, i); |
| 5441 | } |
| 5442 | |
| 5443 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 5444 | { |
| 5445 | struct btrfs_key key; |
| 5446 | struct btrfs_key found_key; |
| 5447 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 5448 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 5449 | struct extent_buffer *eb; |
| 5450 | int slot; |
| 5451 | int ret = 0; |
| 5452 | struct btrfs_device *device; |
| 5453 | struct btrfs_path *path = NULL; |
| 5454 | int i; |
| 5455 | |
| 5456 | path = btrfs_alloc_path(); |
| 5457 | if (!path) { |
| 5458 | ret = -ENOMEM; |
| 5459 | goto out; |
| 5460 | } |
| 5461 | |
| 5462 | mutex_lock(&fs_devices->device_list_mutex); |
| 5463 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 5464 | int item_size; |
| 5465 | struct btrfs_dev_stats_item *ptr; |
| 5466 | |
| 5467 | key.objectid = 0; |
| 5468 | key.type = BTRFS_DEV_STATS_KEY; |
| 5469 | key.offset = device->devid; |
| 5470 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 5471 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5472 | __btrfs_reset_dev_stats(device); |
| 5473 | device->dev_stats_valid = 1; |
| 5474 | btrfs_release_path(path); |
| 5475 | continue; |
| 5476 | } |
| 5477 | slot = path->slots[0]; |
| 5478 | eb = path->nodes[0]; |
| 5479 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 5480 | item_size = btrfs_item_size_nr(eb, slot); |
| 5481 | |
| 5482 | ptr = btrfs_item_ptr(eb, slot, |
| 5483 | struct btrfs_dev_stats_item); |
| 5484 | |
| 5485 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 5486 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 5487 | btrfs_dev_stat_set(device, i, |
| 5488 | btrfs_dev_stats_value(eb, ptr, i)); |
| 5489 | else |
| 5490 | btrfs_dev_stat_reset(device, i); |
| 5491 | } |
| 5492 | |
| 5493 | device->dev_stats_valid = 1; |
| 5494 | btrfs_dev_stat_print_on_load(device); |
| 5495 | btrfs_release_path(path); |
| 5496 | } |
| 5497 | mutex_unlock(&fs_devices->device_list_mutex); |
| 5498 | |
| 5499 | out: |
| 5500 | btrfs_free_path(path); |
| 5501 | return ret < 0 ? ret : 0; |
| 5502 | } |
| 5503 | |
| 5504 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
| 5505 | struct btrfs_root *dev_root, |
| 5506 | struct btrfs_device *device) |
| 5507 | { |
| 5508 | struct btrfs_path *path; |
| 5509 | struct btrfs_key key; |
| 5510 | struct extent_buffer *eb; |
| 5511 | struct btrfs_dev_stats_item *ptr; |
| 5512 | int ret; |
| 5513 | int i; |
| 5514 | |
| 5515 | key.objectid = 0; |
| 5516 | key.type = BTRFS_DEV_STATS_KEY; |
| 5517 | key.offset = device->devid; |
| 5518 | |
| 5519 | path = btrfs_alloc_path(); |
| 5520 | BUG_ON(!path); |
| 5521 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 5522 | if (ret < 0) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5523 | printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n", |
| 5524 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5525 | goto out; |
| 5526 | } |
| 5527 | |
| 5528 | if (ret == 0 && |
| 5529 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 5530 | /* need to delete old one and insert a new one */ |
| 5531 | ret = btrfs_del_item(trans, dev_root, path); |
| 5532 | if (ret != 0) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5533 | printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n", |
| 5534 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5535 | goto out; |
| 5536 | } |
| 5537 | ret = 1; |
| 5538 | } |
| 5539 | |
| 5540 | if (ret == 1) { |
| 5541 | /* need to insert a new item */ |
| 5542 | btrfs_release_path(path); |
| 5543 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 5544 | &key, sizeof(*ptr)); |
| 5545 | if (ret < 0) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5546 | printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n", |
| 5547 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5548 | goto out; |
| 5549 | } |
| 5550 | } |
| 5551 | |
| 5552 | eb = path->nodes[0]; |
| 5553 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 5554 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 5555 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 5556 | btrfs_dev_stat_read(device, i)); |
| 5557 | btrfs_mark_buffer_dirty(eb); |
| 5558 | |
| 5559 | out: |
| 5560 | btrfs_free_path(path); |
| 5561 | return ret; |
| 5562 | } |
| 5563 | |
| 5564 | /* |
| 5565 | * called from commit_transaction. Writes all changed device stats to disk. |
| 5566 | */ |
| 5567 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 5568 | struct btrfs_fs_info *fs_info) |
| 5569 | { |
| 5570 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 5571 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 5572 | struct btrfs_device *device; |
| 5573 | int ret = 0; |
| 5574 | |
| 5575 | mutex_lock(&fs_devices->device_list_mutex); |
| 5576 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 5577 | if (!device->dev_stats_valid || !device->dev_stats_dirty) |
| 5578 | continue; |
| 5579 | |
| 5580 | ret = update_dev_stat_item(trans, dev_root, device); |
| 5581 | if (!ret) |
| 5582 | device->dev_stats_dirty = 0; |
| 5583 | } |
| 5584 | mutex_unlock(&fs_devices->device_list_mutex); |
| 5585 | |
| 5586 | return ret; |
| 5587 | } |
| 5588 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5589 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 5590 | { |
| 5591 | btrfs_dev_stat_inc(dev, index); |
| 5592 | btrfs_dev_stat_print_on_error(dev); |
| 5593 | } |
| 5594 | |
| 5595 | void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
| 5596 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5597 | if (!dev->dev_stats_valid) |
| 5598 | return; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5599 | printk_ratelimited_in_rcu(KERN_ERR |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5600 | "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5601 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5602 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 5603 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 5604 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 5605 | btrfs_dev_stat_read(dev, |
| 5606 | BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 5607 | btrfs_dev_stat_read(dev, |
| 5608 | BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 5609 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5610 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5611 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 5612 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 5613 | int i; |
| 5614 | |
| 5615 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 5616 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 5617 | break; |
| 5618 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 5619 | return; /* all values == 0, suppress message */ |
| 5620 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5621 | printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n", |
| 5622 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5623 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 5624 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 5625 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 5626 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 5627 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 5628 | } |
| 5629 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5630 | int btrfs_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 5631 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5632 | { |
| 5633 | struct btrfs_device *dev; |
| 5634 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 5635 | int i; |
| 5636 | |
| 5637 | mutex_lock(&fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5638 | dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5639 | mutex_unlock(&fs_devices->device_list_mutex); |
| 5640 | |
| 5641 | if (!dev) { |
| 5642 | printk(KERN_WARNING |
| 5643 | "btrfs: get dev_stats failed, device not found\n"); |
| 5644 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5645 | } else if (!dev->dev_stats_valid) { |
| 5646 | printk(KERN_WARNING |
| 5647 | "btrfs: get dev_stats failed, not yet valid\n"); |
| 5648 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 5649 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5650 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 5651 | if (stats->nr_items > i) |
| 5652 | stats->values[i] = |
| 5653 | btrfs_dev_stat_read_and_reset(dev, i); |
| 5654 | else |
| 5655 | btrfs_dev_stat_reset(dev, i); |
| 5656 | } |
| 5657 | } else { |
| 5658 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 5659 | if (stats->nr_items > i) |
| 5660 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 5661 | } |
| 5662 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 5663 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 5664 | return 0; |
| 5665 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 5666 | |
| 5667 | int btrfs_scratch_superblock(struct btrfs_device *device) |
| 5668 | { |
| 5669 | struct buffer_head *bh; |
| 5670 | struct btrfs_super_block *disk_super; |
| 5671 | |
| 5672 | bh = btrfs_read_dev_super(device->bdev); |
| 5673 | if (!bh) |
| 5674 | return -EINVAL; |
| 5675 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 5676 | |
| 5677 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 5678 | set_buffer_dirty(bh); |
| 5679 | sync_dirty_buffer(bh); |
| 5680 | brelse(bh); |
| 5681 | |
| 5682 | return 0; |
| 5683 | } |