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> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 28 | #include <linux/raid/pq.h> |
| 29 | #include <asm/div64.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 30 | #include "compat.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 31 | #include "ctree.h" |
| 32 | #include "extent_map.h" |
| 33 | #include "disk-io.h" |
| 34 | #include "transaction.h" |
| 35 | #include "print-tree.h" |
| 36 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 37 | #include "raid56.h" |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 38 | #include "async-thread.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 39 | #include "check-integrity.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 40 | #include "rcu-string.h" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame] | 41 | #include "math.h" |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 42 | #include "dev-replace.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 43 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 44 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
| 45 | struct btrfs_root *root, |
| 46 | struct btrfs_device *device); |
| 47 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 48 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 49 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 50 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 51 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 52 | static DEFINE_MUTEX(uuid_mutex); |
| 53 | static LIST_HEAD(fs_uuids); |
| 54 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 55 | static void lock_chunks(struct btrfs_root *root) |
| 56 | { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 57 | mutex_lock(&root->fs_info->chunk_mutex); |
| 58 | } |
| 59 | |
| 60 | static void unlock_chunks(struct btrfs_root *root) |
| 61 | { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 62 | mutex_unlock(&root->fs_info->chunk_mutex); |
| 63 | } |
| 64 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 65 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 66 | { |
| 67 | struct btrfs_device *device; |
| 68 | WARN_ON(fs_devices->opened); |
| 69 | while (!list_empty(&fs_devices->devices)) { |
| 70 | device = list_entry(fs_devices->devices.next, |
| 71 | struct btrfs_device, dev_list); |
| 72 | list_del(&device->dev_list); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 73 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 74 | kfree(device); |
| 75 | } |
| 76 | kfree(fs_devices); |
| 77 | } |
| 78 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 79 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 80 | enum kobject_action action) |
| 81 | { |
| 82 | int ret; |
| 83 | |
| 84 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 85 | if (ret) |
| 86 | pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n", |
| 87 | action, |
| 88 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 89 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 90 | } |
| 91 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 92 | void btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 93 | { |
| 94 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 95 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 96 | while (!list_empty(&fs_uuids)) { |
| 97 | fs_devices = list_entry(fs_uuids.next, |
| 98 | struct btrfs_fs_devices, list); |
| 99 | list_del(&fs_devices->list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 100 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 101 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 102 | } |
| 103 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 104 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
| 105 | u64 devid, u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 106 | { |
| 107 | struct btrfs_device *dev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 108 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 109 | list_for_each_entry(dev, head, dev_list) { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 110 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 111 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 112 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 113 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 114 | } |
| 115 | return NULL; |
| 116 | } |
| 117 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 118 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 119 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 120 | struct btrfs_fs_devices *fs_devices; |
| 121 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 122 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 123 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 124 | return fs_devices; |
| 125 | } |
| 126 | return NULL; |
| 127 | } |
| 128 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 129 | static int |
| 130 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 131 | int flush, struct block_device **bdev, |
| 132 | struct buffer_head **bh) |
| 133 | { |
| 134 | int ret; |
| 135 | |
| 136 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 137 | |
| 138 | if (IS_ERR(*bdev)) { |
| 139 | ret = PTR_ERR(*bdev); |
| 140 | printk(KERN_INFO "btrfs: open %s failed\n", device_path); |
| 141 | goto error; |
| 142 | } |
| 143 | |
| 144 | if (flush) |
| 145 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
| 146 | ret = set_blocksize(*bdev, 4096); |
| 147 | if (ret) { |
| 148 | blkdev_put(*bdev, flags); |
| 149 | goto error; |
| 150 | } |
| 151 | invalidate_bdev(*bdev); |
| 152 | *bh = btrfs_read_dev_super(*bdev); |
| 153 | if (!*bh) { |
| 154 | ret = -EINVAL; |
| 155 | blkdev_put(*bdev, flags); |
| 156 | goto error; |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | |
| 161 | error: |
| 162 | *bdev = NULL; |
| 163 | *bh = NULL; |
| 164 | return ret; |
| 165 | } |
| 166 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 167 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 168 | struct bio *head, struct bio *tail) |
| 169 | { |
| 170 | |
| 171 | struct bio *old_head; |
| 172 | |
| 173 | old_head = pending_bios->head; |
| 174 | pending_bios->head = head; |
| 175 | if (pending_bios->tail) |
| 176 | tail->bi_next = old_head; |
| 177 | else |
| 178 | pending_bios->tail = tail; |
| 179 | } |
| 180 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 181 | /* |
| 182 | * we try to collect pending bios for a device so we don't get a large |
| 183 | * number of procs sending bios down to the same device. This greatly |
| 184 | * improves the schedulers ability to collect and merge the bios. |
| 185 | * |
| 186 | * But, it also turns into a long list of bios to process and that is sure |
| 187 | * to eventually make the worker thread block. The solution here is to |
| 188 | * make some progress and then put this work struct back at the end of |
| 189 | * the list if the block device is congested. This way, multiple devices |
| 190 | * can make progress from a single worker thread. |
| 191 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 192 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 193 | { |
| 194 | struct bio *pending; |
| 195 | struct backing_dev_info *bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 196 | struct btrfs_fs_info *fs_info; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 197 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 198 | struct bio *tail; |
| 199 | struct bio *cur; |
| 200 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 201 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 202 | unsigned long batch_run = 0; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 203 | unsigned long limit; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 204 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 205 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 206 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 207 | struct blk_plug plug; |
| 208 | |
| 209 | /* |
| 210 | * this function runs all the bios we've collected for |
| 211 | * a particular device. We don't want to wander off to |
| 212 | * another device without first sending all of these down. |
| 213 | * So, setup a plug here and finish it off before we return |
| 214 | */ |
| 215 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 216 | |
Chris Mason | bedf762 | 2009-04-03 10:32:58 -0400 | [diff] [blame] | 217 | bdi = blk_get_backing_dev_info(device->bdev); |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 218 | fs_info = device->dev_root->fs_info; |
| 219 | limit = btrfs_async_submit_limit(fs_info); |
| 220 | limit = limit * 2 / 3; |
| 221 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 222 | loop: |
| 223 | spin_lock(&device->io_lock); |
| 224 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 225 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 226 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 227 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 228 | /* take all the bios off the list at once and process them |
| 229 | * later on (without the lock held). But, remember the |
| 230 | * tail and other pointers so the bios can be properly reinserted |
| 231 | * into the list if we hit congestion |
| 232 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 233 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 234 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 235 | force_reg = 1; |
| 236 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 237 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 238 | force_reg = 0; |
| 239 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 240 | |
| 241 | pending = pending_bios->head; |
| 242 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 243 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 244 | |
| 245 | /* |
| 246 | * if pending was null this time around, no bios need processing |
| 247 | * at all and we can stop. Otherwise it'll loop back up again |
| 248 | * and do an additional check so no bios are missed. |
| 249 | * |
| 250 | * device->running_pending is used to synchronize with the |
| 251 | * schedule_bio code. |
| 252 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 253 | if (device->pending_sync_bios.head == NULL && |
| 254 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 255 | again = 0; |
| 256 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 257 | } else { |
| 258 | again = 1; |
| 259 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 260 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 261 | |
| 262 | pending_bios->head = NULL; |
| 263 | pending_bios->tail = NULL; |
| 264 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 265 | spin_unlock(&device->io_lock); |
| 266 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 267 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 268 | |
| 269 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 270 | /* we want to work on both lists, but do more bios on the |
| 271 | * sync list than the regular list |
| 272 | */ |
| 273 | if ((num_run > 32 && |
| 274 | pending_bios != &device->pending_sync_bios && |
| 275 | device->pending_sync_bios.head) || |
| 276 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 277 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 278 | spin_lock(&device->io_lock); |
| 279 | requeue_list(pending_bios, pending, tail); |
| 280 | goto loop_lock; |
| 281 | } |
| 282 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 283 | cur = pending; |
| 284 | pending = pending->bi_next; |
| 285 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 286 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 287 | if (atomic_dec_return(&fs_info->nr_async_bios) < limit && |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 288 | waitqueue_active(&fs_info->async_submit_wait)) |
| 289 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 290 | |
| 291 | BUG_ON(atomic_read(&cur->bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 292 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 293 | /* |
| 294 | * if we're doing the sync list, record that our |
| 295 | * plug has some sync requests on it |
| 296 | * |
| 297 | * If we're doing the regular list and there are |
| 298 | * sync requests sitting around, unplug before |
| 299 | * we add more |
| 300 | */ |
| 301 | if (pending_bios == &device->pending_sync_bios) { |
| 302 | sync_pending = 1; |
| 303 | } else if (sync_pending) { |
| 304 | blk_finish_plug(&plug); |
| 305 | blk_start_plug(&plug); |
| 306 | sync_pending = 0; |
| 307 | } |
| 308 | |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 309 | btrfsic_submit_bio(cur->bi_rw, cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 310 | num_run++; |
| 311 | batch_run++; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 312 | if (need_resched()) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 313 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 314 | |
| 315 | /* |
| 316 | * we made progress, there is more work to do and the bdi |
| 317 | * is now congested. Back off and let other work structs |
| 318 | * run instead |
| 319 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 320 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 321 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 322 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 323 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 324 | ioc = current->io_context; |
| 325 | |
| 326 | /* |
| 327 | * the main goal here is that we don't want to |
| 328 | * block if we're going to be able to submit |
| 329 | * more requests without blocking. |
| 330 | * |
| 331 | * This code does two great things, it pokes into |
| 332 | * the elevator code from a filesystem _and_ |
| 333 | * it makes assumptions about how batching works. |
| 334 | */ |
| 335 | if (ioc && ioc->nr_batch_requests > 0 && |
| 336 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 337 | (last_waited == 0 || |
| 338 | ioc->last_waited == last_waited)) { |
| 339 | /* |
| 340 | * we want to go through our batch of |
| 341 | * requests and stop. So, we copy out |
| 342 | * the ioc->last_waited time and test |
| 343 | * against it before looping |
| 344 | */ |
| 345 | last_waited = ioc->last_waited; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 346 | if (need_resched()) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 347 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 348 | continue; |
| 349 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 350 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 351 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 352 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 353 | |
| 354 | spin_unlock(&device->io_lock); |
| 355 | btrfs_requeue_work(&device->work); |
| 356 | goto done; |
| 357 | } |
Chris Mason | d85c8a6f | 2011-12-15 15:38:41 -0500 | [diff] [blame] | 358 | /* unplug every 64 requests just for good measure */ |
| 359 | if (batch_run % 64 == 0) { |
| 360 | blk_finish_plug(&plug); |
| 361 | blk_start_plug(&plug); |
| 362 | sync_pending = 0; |
| 363 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 364 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 365 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 366 | cond_resched(); |
| 367 | if (again) |
| 368 | goto loop; |
| 369 | |
| 370 | spin_lock(&device->io_lock); |
| 371 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 372 | goto loop_lock; |
| 373 | spin_unlock(&device->io_lock); |
| 374 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 375 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 376 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 377 | } |
| 378 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 379 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 380 | { |
| 381 | struct btrfs_device *device; |
| 382 | |
| 383 | device = container_of(work, struct btrfs_device, work); |
| 384 | run_scheduled_bios(device); |
| 385 | } |
| 386 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 387 | static noinline int device_list_add(const char *path, |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 388 | struct btrfs_super_block *disk_super, |
| 389 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) |
| 390 | { |
| 391 | struct btrfs_device *device; |
| 392 | struct btrfs_fs_devices *fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 393 | struct rcu_string *name; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 394 | u64 found_transid = btrfs_super_generation(disk_super); |
| 395 | |
| 396 | fs_devices = find_fsid(disk_super->fsid); |
| 397 | if (!fs_devices) { |
Chris Mason | 515dc32 | 2008-05-16 13:30:15 -0400 | [diff] [blame] | 398 | fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 399 | if (!fs_devices) |
| 400 | return -ENOMEM; |
| 401 | INIT_LIST_HEAD(&fs_devices->devices); |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 402 | INIT_LIST_HEAD(&fs_devices->alloc_list); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 403 | list_add(&fs_devices->list, &fs_uuids); |
| 404 | memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE); |
| 405 | fs_devices->latest_devid = devid; |
| 406 | fs_devices->latest_trans = found_transid; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 407 | mutex_init(&fs_devices->device_list_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 408 | device = NULL; |
| 409 | } else { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 410 | device = __find_device(&fs_devices->devices, devid, |
| 411 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 412 | } |
| 413 | if (!device) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 414 | if (fs_devices->opened) |
| 415 | return -EBUSY; |
| 416 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 417 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 418 | if (!device) { |
| 419 | /* we can safely leave the fs_devices entry around */ |
| 420 | return -ENOMEM; |
| 421 | } |
| 422 | device->devid = devid; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 423 | device->dev_stats_valid = 0; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 424 | device->work.func = pending_bios_fn; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 425 | memcpy(device->uuid, disk_super->dev_item.uuid, |
| 426 | BTRFS_UUID_SIZE); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 427 | spin_lock_init(&device->io_lock); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 428 | |
| 429 | name = rcu_string_strdup(path, GFP_NOFS); |
| 430 | if (!name) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 431 | kfree(device); |
| 432 | return -ENOMEM; |
| 433 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 434 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 435 | INIT_LIST_HEAD(&device->dev_alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 436 | |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 437 | /* init readahead state */ |
| 438 | spin_lock_init(&device->reada_lock); |
| 439 | device->reada_curr_zone = NULL; |
| 440 | atomic_set(&device->reada_in_flight, 0); |
| 441 | device->reada_next = 0; |
| 442 | INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT); |
| 443 | INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT); |
| 444 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 445 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 446 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 447 | mutex_unlock(&fs_devices->device_list_mutex); |
| 448 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 449 | device->fs_devices = fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 450 | fs_devices->num_devices++; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 451 | } else if (!device->name || strcmp(device->name->str, path)) { |
| 452 | name = rcu_string_strdup(path, GFP_NOFS); |
TARUISI Hiroaki | 3a0524d | 2010-02-09 06:36:45 +0000 | [diff] [blame] | 453 | if (!name) |
| 454 | return -ENOMEM; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 455 | rcu_string_free(device->name); |
| 456 | rcu_assign_pointer(device->name, name); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 457 | if (device->missing) { |
| 458 | fs_devices->missing_devices--; |
| 459 | device->missing = 0; |
| 460 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | if (found_transid > fs_devices->latest_trans) { |
| 464 | fs_devices->latest_devid = devid; |
| 465 | fs_devices->latest_trans = found_transid; |
| 466 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 467 | *fs_devices_ret = fs_devices; |
| 468 | return 0; |
| 469 | } |
| 470 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 471 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 472 | { |
| 473 | struct btrfs_fs_devices *fs_devices; |
| 474 | struct btrfs_device *device; |
| 475 | struct btrfs_device *orig_dev; |
| 476 | |
| 477 | fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
| 478 | if (!fs_devices) |
| 479 | return ERR_PTR(-ENOMEM); |
| 480 | |
| 481 | INIT_LIST_HEAD(&fs_devices->devices); |
| 482 | INIT_LIST_HEAD(&fs_devices->alloc_list); |
| 483 | INIT_LIST_HEAD(&fs_devices->list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 484 | mutex_init(&fs_devices->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 485 | fs_devices->latest_devid = orig->latest_devid; |
| 486 | fs_devices->latest_trans = orig->latest_trans; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 487 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 488 | memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid)); |
| 489 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 490 | /* 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] | 491 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 492 | struct rcu_string *name; |
| 493 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 494 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 495 | if (!device) |
| 496 | goto error; |
| 497 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 498 | /* |
| 499 | * This is ok to do without rcu read locked because we hold the |
| 500 | * uuid mutex so nothing we touch in here is going to disappear. |
| 501 | */ |
| 502 | name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS); |
| 503 | if (!name) { |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 504 | kfree(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 505 | goto error; |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 506 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 507 | rcu_assign_pointer(device->name, name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 508 | |
| 509 | device->devid = orig_dev->devid; |
| 510 | device->work.func = pending_bios_fn; |
| 511 | memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 512 | spin_lock_init(&device->io_lock); |
| 513 | INIT_LIST_HEAD(&device->dev_list); |
| 514 | INIT_LIST_HEAD(&device->dev_alloc_list); |
| 515 | |
| 516 | list_add(&device->dev_list, &fs_devices->devices); |
| 517 | device->fs_devices = fs_devices; |
| 518 | fs_devices->num_devices++; |
| 519 | } |
| 520 | return fs_devices; |
| 521 | error: |
| 522 | free_fs_devices(fs_devices); |
| 523 | return ERR_PTR(-ENOMEM); |
| 524 | } |
| 525 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 526 | void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info, |
| 527 | struct btrfs_fs_devices *fs_devices, int step) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 528 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 529 | struct btrfs_device *device, *next; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 530 | |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 531 | struct block_device *latest_bdev = NULL; |
| 532 | u64 latest_devid = 0; |
| 533 | u64 latest_transid = 0; |
| 534 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 535 | mutex_lock(&uuid_mutex); |
| 536 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 537 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 538 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 539 | if (device->in_fs_metadata) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 540 | if (!device->is_tgtdev_for_dev_replace && |
| 541 | (!latest_transid || |
| 542 | device->generation > latest_transid)) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 543 | latest_devid = device->devid; |
| 544 | latest_transid = device->generation; |
| 545 | latest_bdev = device->bdev; |
| 546 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 547 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 548 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 549 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 550 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 551 | /* |
| 552 | * In the first step, keep the device which has |
| 553 | * the correct fsid and the devid that is used |
| 554 | * for the dev_replace procedure. |
| 555 | * In the second step, the dev_replace state is |
| 556 | * read from the device tree and it is known |
| 557 | * whether the procedure is really active or |
| 558 | * not, which means whether this device is |
| 559 | * used or whether it should be removed. |
| 560 | */ |
| 561 | if (step == 0 || device->is_tgtdev_for_dev_replace) { |
| 562 | continue; |
| 563 | } |
| 564 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 565 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 566 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 567 | device->bdev = NULL; |
| 568 | fs_devices->open_devices--; |
| 569 | } |
| 570 | if (device->writeable) { |
| 571 | list_del_init(&device->dev_alloc_list); |
| 572 | device->writeable = 0; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 573 | if (!device->is_tgtdev_for_dev_replace) |
| 574 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 575 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 576 | list_del_init(&device->dev_list); |
| 577 | fs_devices->num_devices--; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 578 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 579 | kfree(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 580 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 581 | |
| 582 | if (fs_devices->seed) { |
| 583 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 584 | goto again; |
| 585 | } |
| 586 | |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 587 | fs_devices->latest_bdev = latest_bdev; |
| 588 | fs_devices->latest_devid = latest_devid; |
| 589 | fs_devices->latest_trans = latest_transid; |
| 590 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 591 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 592 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 593 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 594 | static void __free_device(struct work_struct *work) |
| 595 | { |
| 596 | struct btrfs_device *device; |
| 597 | |
| 598 | device = container_of(work, struct btrfs_device, rcu_work); |
| 599 | |
| 600 | if (device->bdev) |
| 601 | blkdev_put(device->bdev, device->mode); |
| 602 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 603 | rcu_string_free(device->name); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 604 | kfree(device); |
| 605 | } |
| 606 | |
| 607 | static void free_device(struct rcu_head *head) |
| 608 | { |
| 609 | struct btrfs_device *device; |
| 610 | |
| 611 | device = container_of(head, struct btrfs_device, rcu); |
| 612 | |
| 613 | INIT_WORK(&device->rcu_work, __free_device); |
| 614 | schedule_work(&device->rcu_work); |
| 615 | } |
| 616 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 617 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 618 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 619 | struct btrfs_device *device; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 620 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 621 | if (--fs_devices->opened > 0) |
| 622 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 623 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 624 | mutex_lock(&fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 625 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 626 | struct btrfs_device *new_device; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 627 | struct rcu_string *name; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 628 | |
| 629 | if (device->bdev) |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 630 | fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 631 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 632 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 633 | list_del_init(&device->dev_alloc_list); |
| 634 | fs_devices->rw_devices--; |
| 635 | } |
| 636 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 637 | if (device->can_discard) |
| 638 | fs_devices->num_can_discard--; |
| 639 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 640 | new_device = kmalloc(sizeof(*new_device), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 641 | BUG_ON(!new_device); /* -ENOMEM */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 642 | memcpy(new_device, device, sizeof(*new_device)); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 643 | |
| 644 | /* Safe because we are under uuid_mutex */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 645 | if (device->name) { |
| 646 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
| 647 | BUG_ON(device->name && !name); /* -ENOMEM */ |
| 648 | rcu_assign_pointer(new_device->name, name); |
| 649 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 650 | new_device->bdev = NULL; |
| 651 | new_device->writeable = 0; |
| 652 | new_device->in_fs_metadata = 0; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 653 | new_device->can_discard = 0; |
Thomas Gleixner | 1cba0cd | 2013-02-20 14:06:20 -0500 | [diff] [blame] | 654 | spin_lock_init(&new_device->io_lock); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 655 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
| 656 | |
| 657 | call_rcu(&device->rcu, free_device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 658 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 659 | mutex_unlock(&fs_devices->device_list_mutex); |
| 660 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 661 | WARN_ON(fs_devices->open_devices); |
| 662 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 663 | fs_devices->opened = 0; |
| 664 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 665 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 666 | return 0; |
| 667 | } |
| 668 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 669 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 670 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 671 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 672 | int ret; |
| 673 | |
| 674 | mutex_lock(&uuid_mutex); |
| 675 | ret = __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 676 | if (!fs_devices->opened) { |
| 677 | seed_devices = fs_devices->seed; |
| 678 | fs_devices->seed = NULL; |
| 679 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 680 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 681 | |
| 682 | while (seed_devices) { |
| 683 | fs_devices = seed_devices; |
| 684 | seed_devices = fs_devices->seed; |
| 685 | __btrfs_close_devices(fs_devices); |
| 686 | free_fs_devices(fs_devices); |
| 687 | } |
Eric Sandeen | bc17862 | 2013-03-09 15:18:39 +0000 | [diff] [blame] | 688 | /* |
| 689 | * Wait for rcu kworkers under __btrfs_close_devices |
| 690 | * to finish all blkdev_puts so device is really |
| 691 | * free when umount is done. |
| 692 | */ |
| 693 | rcu_barrier(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 694 | return ret; |
| 695 | } |
| 696 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 697 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 698 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 699 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 700 | struct request_queue *q; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 701 | struct block_device *bdev; |
| 702 | struct list_head *head = &fs_devices->devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 703 | struct btrfs_device *device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 704 | struct block_device *latest_bdev = NULL; |
| 705 | struct buffer_head *bh; |
| 706 | struct btrfs_super_block *disk_super; |
| 707 | u64 latest_devid = 0; |
| 708 | u64 latest_transid = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 709 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 710 | int seeding = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 711 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 712 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 713 | flags |= FMODE_EXCL; |
| 714 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 715 | list_for_each_entry(device, head, dev_list) { |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 716 | if (device->bdev) |
| 717 | continue; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 718 | if (!device->name) |
| 719 | continue; |
| 720 | |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 721 | /* Just open everything we can; ignore failures here */ |
| 722 | if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 723 | &bdev, &bh)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 724 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 725 | |
| 726 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 727 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 728 | if (devid != device->devid) |
| 729 | goto error_brelse; |
| 730 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 731 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
| 732 | BTRFS_UUID_SIZE)) |
| 733 | goto error_brelse; |
| 734 | |
| 735 | device->generation = btrfs_super_generation(disk_super); |
| 736 | if (!latest_transid || device->generation > latest_transid) { |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 737 | latest_devid = devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 738 | latest_transid = device->generation; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 739 | latest_bdev = bdev; |
| 740 | } |
| 741 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 742 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| 743 | device->writeable = 0; |
| 744 | } else { |
| 745 | device->writeable = !bdev_read_only(bdev); |
| 746 | seeding = 0; |
| 747 | } |
| 748 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 749 | q = bdev_get_queue(bdev); |
| 750 | if (blk_queue_discard(q)) { |
| 751 | device->can_discard = 1; |
| 752 | fs_devices->num_can_discard++; |
| 753 | } |
| 754 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 755 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 756 | device->in_fs_metadata = 0; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 757 | device->mode = flags; |
| 758 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 759 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 760 | fs_devices->rotating = 1; |
| 761 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 762 | fs_devices->open_devices++; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 763 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 764 | fs_devices->rw_devices++; |
| 765 | list_add(&device->dev_alloc_list, |
| 766 | &fs_devices->alloc_list); |
| 767 | } |
Xiao Guangrong | 4f6c932 | 2011-04-20 10:06:40 +0000 | [diff] [blame] | 768 | brelse(bh); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 769 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 770 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 771 | error_brelse: |
| 772 | brelse(bh); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 773 | blkdev_put(bdev, flags); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 774 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 775 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 776 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 777 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 778 | goto out; |
| 779 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 780 | fs_devices->seeding = seeding; |
| 781 | fs_devices->opened = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 782 | fs_devices->latest_bdev = latest_bdev; |
| 783 | fs_devices->latest_devid = latest_devid; |
| 784 | fs_devices->latest_trans = latest_transid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 785 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 786 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 787 | return ret; |
| 788 | } |
| 789 | |
| 790 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 791 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 792 | { |
| 793 | int ret; |
| 794 | |
| 795 | mutex_lock(&uuid_mutex); |
| 796 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 797 | fs_devices->opened++; |
| 798 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 799 | } else { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 800 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 801 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 802 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 803 | return ret; |
| 804 | } |
| 805 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 806 | /* |
| 807 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 808 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 809 | * is read via pagecache |
| 810 | */ |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 811 | 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] | 812 | struct btrfs_fs_devices **fs_devices_ret) |
| 813 | { |
| 814 | struct btrfs_super_block *disk_super; |
| 815 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 816 | struct page *page; |
| 817 | void *p; |
| 818 | int ret = -EINVAL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 819 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 820 | u64 transid; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 821 | u64 total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 822 | u64 bytenr; |
| 823 | pgoff_t index; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 824 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 825 | /* |
| 826 | * we would like to check all the supers, but that would make |
| 827 | * a btrfs mount succeed after a mkfs from a different FS. |
| 828 | * So, we need to add a special mount option to scan for |
| 829 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 830 | */ |
| 831 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 832 | flags |= FMODE_EXCL; |
Al Viro | 10f6327 | 2011-11-17 15:05:22 -0500 | [diff] [blame] | 833 | mutex_lock(&uuid_mutex); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 834 | |
| 835 | bdev = blkdev_get_by_path(path, flags, holder); |
| 836 | |
| 837 | if (IS_ERR(bdev)) { |
| 838 | ret = PTR_ERR(bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 839 | goto error; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /* make sure our super fits in the device */ |
| 843 | if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode)) |
| 844 | goto error_bdev_put; |
| 845 | |
| 846 | /* make sure our super fits in the page */ |
| 847 | if (sizeof(*disk_super) > PAGE_CACHE_SIZE) |
| 848 | goto error_bdev_put; |
| 849 | |
| 850 | /* make sure our super doesn't straddle pages on disk */ |
| 851 | index = bytenr >> PAGE_CACHE_SHIFT; |
| 852 | if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index) |
| 853 | goto error_bdev_put; |
| 854 | |
| 855 | /* pull in the page with our super */ |
| 856 | page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 857 | index, GFP_NOFS); |
| 858 | |
| 859 | if (IS_ERR_OR_NULL(page)) |
| 860 | goto error_bdev_put; |
| 861 | |
| 862 | p = kmap(page); |
| 863 | |
| 864 | /* align our pointer to the offset of the super block */ |
| 865 | disk_super = p + (bytenr & ~PAGE_CACHE_MASK); |
| 866 | |
| 867 | if (btrfs_super_bytenr(disk_super) != bytenr || |
Zach Brown | cdb4c57 | 2013-02-20 00:55:13 +0000 | [diff] [blame] | 868 | disk_super->magic != cpu_to_le64(BTRFS_MAGIC)) |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 869 | goto error_unmap; |
| 870 | |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 871 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 872 | transid = btrfs_super_generation(disk_super); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 873 | total_devices = btrfs_super_num_devices(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 874 | |
Stefan Behrens | d03f918 | 2012-11-05 13:10:49 +0000 | [diff] [blame] | 875 | if (disk_super->label[0]) { |
| 876 | if (disk_super->label[BTRFS_LABEL_SIZE - 1]) |
| 877 | disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 878 | printk(KERN_INFO "device label %s ", disk_super->label); |
Stefan Behrens | d03f918 | 2012-11-05 13:10:49 +0000 | [diff] [blame] | 879 | } else { |
Ilya Dryomov | 22b63a2 | 2011-02-09 16:05:31 +0200 | [diff] [blame] | 880 | printk(KERN_INFO "device fsid %pU ", disk_super->fsid); |
Stefan Behrens | d03f918 | 2012-11-05 13:10:49 +0000 | [diff] [blame] | 881 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 882 | |
Roland Dreier | 119e10c | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 883 | printk(KERN_CONT "devid %llu transid %llu %s\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 884 | (unsigned long long)devid, (unsigned long long)transid, path); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 885 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 886 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 887 | if (!ret && fs_devices_ret) |
| 888 | (*fs_devices_ret)->total_devices = total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 889 | |
| 890 | error_unmap: |
| 891 | kunmap(page); |
| 892 | page_cache_release(page); |
| 893 | |
| 894 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 895 | blkdev_put(bdev, flags); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 896 | error: |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 897 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 898 | return ret; |
| 899 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 900 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 901 | /* helper to account the used device space in the range */ |
| 902 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, |
| 903 | u64 end, u64 *length) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 904 | { |
| 905 | struct btrfs_key key; |
| 906 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 907 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 908 | struct btrfs_path *path; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 909 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 910 | int ret; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 911 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 912 | struct extent_buffer *l; |
| 913 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 914 | *length = 0; |
| 915 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 916 | if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 917 | return 0; |
| 918 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 919 | path = btrfs_alloc_path(); |
| 920 | if (!path) |
| 921 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 922 | path->reada = 2; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 923 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 924 | key.objectid = device->devid; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 925 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 926 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 927 | |
| 928 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 929 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 930 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 931 | if (ret > 0) { |
| 932 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 933 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 934 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 935 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 936 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 937 | while (1) { |
| 938 | l = path->nodes[0]; |
| 939 | slot = path->slots[0]; |
| 940 | if (slot >= btrfs_header_nritems(l)) { |
| 941 | ret = btrfs_next_leaf(root, path); |
| 942 | if (ret == 0) |
| 943 | continue; |
| 944 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 945 | goto out; |
| 946 | |
| 947 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 948 | } |
| 949 | btrfs_item_key_to_cpu(l, &key, slot); |
| 950 | |
| 951 | if (key.objectid < device->devid) |
| 952 | goto next; |
| 953 | |
| 954 | if (key.objectid > device->devid) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 955 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 956 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 957 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 958 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 959 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 960 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 961 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 962 | dev_extent); |
| 963 | if (key.offset <= start && extent_end > end) { |
| 964 | *length = end - start + 1; |
| 965 | break; |
| 966 | } else if (key.offset <= start && extent_end > start) |
| 967 | *length += extent_end - start; |
| 968 | else if (key.offset > start && extent_end <= end) |
| 969 | *length += extent_end - key.offset; |
| 970 | else if (key.offset > start && key.offset <= end) { |
| 971 | *length += end - key.offset + 1; |
| 972 | break; |
| 973 | } else if (key.offset > end) |
| 974 | break; |
| 975 | |
| 976 | next: |
| 977 | path->slots[0]++; |
| 978 | } |
| 979 | ret = 0; |
| 980 | out: |
| 981 | btrfs_free_path(path); |
| 982 | return ret; |
| 983 | } |
| 984 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 985 | /* |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 986 | * find_free_dev_extent - find free space in the specified device |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 987 | * @device: the device which we search the free space in |
| 988 | * @num_bytes: the size of the free space that we need |
| 989 | * @start: store the start of the free space. |
| 990 | * @len: the size of the free space. that we find, or the size of the max |
| 991 | * free space if we don't find suitable free space |
| 992 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 993 | * this uses a pretty simple search, the expectation is that it is |
| 994 | * called very infrequently and that a given device has a small number |
| 995 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 996 | * |
| 997 | * @start is used to store the start of the free space if we find. But if we |
| 998 | * don't find suitable free space, it will be used to store the start position |
| 999 | * of the max free space. |
| 1000 | * |
| 1001 | * @len is used to store the size of the free space that we find. |
| 1002 | * But if we don't find suitable free space, it is used to store the size of |
| 1003 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1004 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1005 | int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1006 | u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1007 | { |
| 1008 | struct btrfs_key key; |
| 1009 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1010 | struct btrfs_dev_extent *dev_extent; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1011 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1012 | u64 hole_size; |
| 1013 | u64 max_hole_start; |
| 1014 | u64 max_hole_size; |
| 1015 | u64 extent_end; |
| 1016 | u64 search_start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1017 | u64 search_end = device->total_bytes; |
| 1018 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1019 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1020 | struct extent_buffer *l; |
| 1021 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1022 | /* FIXME use last free of some kind */ |
| 1023 | |
| 1024 | /* we don't want to overwrite the superblock on the drive, |
| 1025 | * so we make sure to start at an offset of at least 1MB |
| 1026 | */ |
Arne Jansen | a9c9bf6 | 2011-04-12 11:01:20 +0200 | [diff] [blame] | 1027 | search_start = max(root->fs_info->alloc_start, 1024ull * 1024); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1028 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1029 | max_hole_start = search_start; |
| 1030 | max_hole_size = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1031 | hole_size = 0; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1032 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1033 | if (search_start >= search_end || device->is_tgtdev_for_dev_replace) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1034 | ret = -ENOSPC; |
| 1035 | goto error; |
| 1036 | } |
| 1037 | |
| 1038 | path = btrfs_alloc_path(); |
| 1039 | if (!path) { |
| 1040 | ret = -ENOMEM; |
| 1041 | goto error; |
| 1042 | } |
| 1043 | path->reada = 2; |
| 1044 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1045 | key.objectid = device->devid; |
| 1046 | key.offset = search_start; |
| 1047 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1048 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1049 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1050 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1051 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1052 | if (ret > 0) { |
| 1053 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1054 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1055 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1056 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1057 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1058 | while (1) { |
| 1059 | l = path->nodes[0]; |
| 1060 | slot = path->slots[0]; |
| 1061 | if (slot >= btrfs_header_nritems(l)) { |
| 1062 | ret = btrfs_next_leaf(root, path); |
| 1063 | if (ret == 0) |
| 1064 | continue; |
| 1065 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1066 | goto out; |
| 1067 | |
| 1068 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1069 | } |
| 1070 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1071 | |
| 1072 | if (key.objectid < device->devid) |
| 1073 | goto next; |
| 1074 | |
| 1075 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1076 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1077 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1078 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) |
| 1079 | goto next; |
| 1080 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1081 | if (key.offset > search_start) { |
| 1082 | hole_size = key.offset - search_start; |
| 1083 | |
| 1084 | if (hole_size > max_hole_size) { |
| 1085 | max_hole_start = search_start; |
| 1086 | max_hole_size = hole_size; |
| 1087 | } |
| 1088 | |
| 1089 | /* |
| 1090 | * If this free space is greater than which we need, |
| 1091 | * it must be the max free space that we have found |
| 1092 | * until now, so max_hole_start must point to the start |
| 1093 | * of this free space and the length of this free space |
| 1094 | * is stored in max_hole_size. Thus, we return |
| 1095 | * max_hole_start and max_hole_size and go back to the |
| 1096 | * caller. |
| 1097 | */ |
| 1098 | if (hole_size >= num_bytes) { |
| 1099 | ret = 0; |
| 1100 | goto out; |
| 1101 | } |
| 1102 | } |
| 1103 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1104 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1105 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1106 | dev_extent); |
| 1107 | if (extent_end > search_start) |
| 1108 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1109 | next: |
| 1110 | path->slots[0]++; |
| 1111 | cond_resched(); |
| 1112 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1113 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1114 | /* |
| 1115 | * At this point, search_start should be the end of |
| 1116 | * allocated dev extents, and when shrinking the device, |
| 1117 | * search_end may be smaller than search_start. |
| 1118 | */ |
| 1119 | if (search_end > search_start) |
| 1120 | hole_size = search_end - search_start; |
| 1121 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1122 | if (hole_size > max_hole_size) { |
| 1123 | max_hole_start = search_start; |
| 1124 | max_hole_size = hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1125 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1126 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1127 | /* See above. */ |
| 1128 | if (hole_size < num_bytes) |
| 1129 | ret = -ENOSPC; |
| 1130 | else |
| 1131 | ret = 0; |
| 1132 | |
| 1133 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1134 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1135 | error: |
| 1136 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1137 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1138 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1139 | return ret; |
| 1140 | } |
| 1141 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1142 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1143 | struct btrfs_device *device, |
| 1144 | u64 start) |
| 1145 | { |
| 1146 | int ret; |
| 1147 | struct btrfs_path *path; |
| 1148 | struct btrfs_root *root = device->dev_root; |
| 1149 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1150 | struct btrfs_key found_key; |
| 1151 | struct extent_buffer *leaf = NULL; |
| 1152 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1153 | |
| 1154 | path = btrfs_alloc_path(); |
| 1155 | if (!path) |
| 1156 | return -ENOMEM; |
| 1157 | |
| 1158 | key.objectid = device->devid; |
| 1159 | key.offset = start; |
| 1160 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1161 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1162 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1163 | if (ret > 0) { |
| 1164 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1165 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1166 | if (ret) |
| 1167 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1168 | leaf = path->nodes[0]; |
| 1169 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1170 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1171 | struct btrfs_dev_extent); |
| 1172 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1173 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1174 | key = found_key; |
| 1175 | btrfs_release_path(path); |
| 1176 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1177 | } else if (ret == 0) { |
| 1178 | leaf = path->nodes[0]; |
| 1179 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1180 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1181 | } else { |
| 1182 | btrfs_error(root->fs_info, ret, "Slot search failed"); |
| 1183 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1184 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1185 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 1186 | if (device->bytes_used > 0) { |
| 1187 | u64 len = btrfs_dev_extent_length(leaf, extent); |
| 1188 | device->bytes_used -= len; |
| 1189 | spin_lock(&root->fs_info->free_chunk_lock); |
| 1190 | root->fs_info->free_chunk_space += len; |
| 1191 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 1192 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1193 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1194 | if (ret) { |
| 1195 | btrfs_error(root->fs_info, ret, |
| 1196 | "Failed to remove dev extent item"); |
| 1197 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1198 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1199 | btrfs_free_path(path); |
| 1200 | return ret; |
| 1201 | } |
| 1202 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1203 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1204 | struct btrfs_device *device, |
| 1205 | u64 chunk_tree, u64 chunk_objectid, |
| 1206 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1207 | { |
| 1208 | int ret; |
| 1209 | struct btrfs_path *path; |
| 1210 | struct btrfs_root *root = device->dev_root; |
| 1211 | struct btrfs_dev_extent *extent; |
| 1212 | struct extent_buffer *leaf; |
| 1213 | struct btrfs_key key; |
| 1214 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1215 | WARN_ON(!device->in_fs_metadata); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1216 | WARN_ON(device->is_tgtdev_for_dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1217 | path = btrfs_alloc_path(); |
| 1218 | if (!path) |
| 1219 | return -ENOMEM; |
| 1220 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1221 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1222 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1223 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1224 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1225 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1226 | if (ret) |
| 1227 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1228 | |
| 1229 | leaf = path->nodes[0]; |
| 1230 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1231 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1232 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 1233 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 1234 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1235 | |
| 1236 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
| 1237 | (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent), |
| 1238 | BTRFS_UUID_SIZE); |
| 1239 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1240 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1241 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1242 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1243 | btrfs_free_path(path); |
| 1244 | return ret; |
| 1245 | } |
| 1246 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 1247 | static noinline int find_next_chunk(struct btrfs_root *root, |
| 1248 | u64 objectid, u64 *offset) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1249 | { |
| 1250 | struct btrfs_path *path; |
| 1251 | int ret; |
| 1252 | struct btrfs_key key; |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1253 | struct btrfs_chunk *chunk; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1254 | struct btrfs_key found_key; |
| 1255 | |
| 1256 | path = btrfs_alloc_path(); |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 1257 | if (!path) |
| 1258 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1259 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1260 | key.objectid = objectid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1261 | key.offset = (u64)-1; |
| 1262 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 1263 | |
| 1264 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1265 | if (ret < 0) |
| 1266 | goto error; |
| 1267 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1268 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1269 | |
| 1270 | ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY); |
| 1271 | if (ret) { |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1272 | *offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1273 | } else { |
| 1274 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1275 | path->slots[0]); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1276 | if (found_key.objectid != objectid) |
| 1277 | *offset = 0; |
| 1278 | else { |
| 1279 | chunk = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1280 | struct btrfs_chunk); |
| 1281 | *offset = found_key.offset + |
| 1282 | btrfs_chunk_length(path->nodes[0], chunk); |
| 1283 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1284 | } |
| 1285 | ret = 0; |
| 1286 | error: |
| 1287 | btrfs_free_path(path); |
| 1288 | return ret; |
| 1289 | } |
| 1290 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1291 | static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1292 | { |
| 1293 | int ret; |
| 1294 | struct btrfs_key key; |
| 1295 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1296 | struct btrfs_path *path; |
| 1297 | |
| 1298 | root = root->fs_info->chunk_root; |
| 1299 | |
| 1300 | path = btrfs_alloc_path(); |
| 1301 | if (!path) |
| 1302 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1303 | |
| 1304 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1305 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1306 | key.offset = (u64)-1; |
| 1307 | |
| 1308 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1309 | if (ret < 0) |
| 1310 | goto error; |
| 1311 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1312 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1313 | |
| 1314 | ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID, |
| 1315 | BTRFS_DEV_ITEM_KEY); |
| 1316 | if (ret) { |
| 1317 | *objectid = 1; |
| 1318 | } else { |
| 1319 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1320 | path->slots[0]); |
| 1321 | *objectid = found_key.offset + 1; |
| 1322 | } |
| 1323 | ret = 0; |
| 1324 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1325 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1326 | return ret; |
| 1327 | } |
| 1328 | |
| 1329 | /* |
| 1330 | * the device information is stored in the chunk root |
| 1331 | * the btrfs_device struct should be fully filled in |
| 1332 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1333 | static int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 1334 | struct btrfs_root *root, |
| 1335 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1336 | { |
| 1337 | int ret; |
| 1338 | struct btrfs_path *path; |
| 1339 | struct btrfs_dev_item *dev_item; |
| 1340 | struct extent_buffer *leaf; |
| 1341 | struct btrfs_key key; |
| 1342 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1343 | |
| 1344 | root = root->fs_info->chunk_root; |
| 1345 | |
| 1346 | path = btrfs_alloc_path(); |
| 1347 | if (!path) |
| 1348 | return -ENOMEM; |
| 1349 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1350 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1351 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1352 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1353 | |
| 1354 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1355 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1356 | if (ret) |
| 1357 | goto out; |
| 1358 | |
| 1359 | leaf = path->nodes[0]; |
| 1360 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1361 | |
| 1362 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1363 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1364 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1365 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1366 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1367 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1368 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
| 1369 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1370 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1371 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1372 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1373 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1374 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1375 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1376 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1377 | ptr = (unsigned long)btrfs_device_fsid(dev_item); |
| 1378 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1379 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1380 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1381 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1382 | out: |
| 1383 | btrfs_free_path(path); |
| 1384 | return ret; |
| 1385 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1386 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1387 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 1388 | struct btrfs_device *device) |
| 1389 | { |
| 1390 | int ret; |
| 1391 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1392 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1393 | struct btrfs_trans_handle *trans; |
| 1394 | |
| 1395 | root = root->fs_info->chunk_root; |
| 1396 | |
| 1397 | path = btrfs_alloc_path(); |
| 1398 | if (!path) |
| 1399 | return -ENOMEM; |
| 1400 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1401 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1402 | if (IS_ERR(trans)) { |
| 1403 | btrfs_free_path(path); |
| 1404 | return PTR_ERR(trans); |
| 1405 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1406 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1407 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1408 | key.offset = device->devid; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1409 | lock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1410 | |
| 1411 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1412 | if (ret < 0) |
| 1413 | goto out; |
| 1414 | |
| 1415 | if (ret > 0) { |
| 1416 | ret = -ENOENT; |
| 1417 | goto out; |
| 1418 | } |
| 1419 | |
| 1420 | ret = btrfs_del_item(trans, root, path); |
| 1421 | if (ret) |
| 1422 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1423 | out: |
| 1424 | btrfs_free_path(path); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1425 | unlock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1426 | btrfs_commit_transaction(trans, root); |
| 1427 | return ret; |
| 1428 | } |
| 1429 | |
| 1430 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 1431 | { |
| 1432 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1433 | struct btrfs_device *next_device; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1434 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1435 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1436 | struct btrfs_super_block *disk_super; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1437 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1438 | u64 all_avail; |
| 1439 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1440 | u64 num_devices; |
| 1441 | u8 *dev_uuid; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1442 | unsigned seq; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1443 | int ret = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1444 | bool clear_super = false; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1445 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1446 | mutex_lock(&uuid_mutex); |
| 1447 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1448 | do { |
| 1449 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 1450 | |
| 1451 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 1452 | root->fs_info->avail_system_alloc_bits | |
| 1453 | root->fs_info->avail_metadata_alloc_bits; |
| 1454 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1455 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1456 | num_devices = root->fs_info->fs_devices->num_devices; |
| 1457 | btrfs_dev_replace_lock(&root->fs_info->dev_replace); |
| 1458 | if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) { |
| 1459 | WARN_ON(num_devices < 1); |
| 1460 | num_devices--; |
| 1461 | } |
| 1462 | btrfs_dev_replace_unlock(&root->fs_info->dev_replace); |
| 1463 | |
| 1464 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame^] | 1465 | ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1466 | goto out; |
| 1467 | } |
| 1468 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1469 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame^] | 1470 | ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1471 | goto out; |
| 1472 | } |
| 1473 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1474 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) && |
| 1475 | root->fs_info->fs_devices->rw_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame^] | 1476 | ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1477 | goto out; |
| 1478 | } |
| 1479 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) && |
| 1480 | root->fs_info->fs_devices->rw_devices <= 3) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame^] | 1481 | ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1482 | goto out; |
| 1483 | } |
| 1484 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1485 | if (strcmp(device_path, "missing") == 0) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1486 | struct list_head *devices; |
| 1487 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1488 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1489 | device = NULL; |
| 1490 | devices = &root->fs_info->fs_devices->devices; |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1491 | /* |
| 1492 | * It is safe to read the devices since the volume_mutex |
| 1493 | * is held. |
| 1494 | */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1495 | list_for_each_entry(tmp, devices, dev_list) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1496 | if (tmp->in_fs_metadata && |
| 1497 | !tmp->is_tgtdev_for_dev_replace && |
| 1498 | !tmp->bdev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1499 | device = tmp; |
| 1500 | break; |
| 1501 | } |
| 1502 | } |
| 1503 | bdev = NULL; |
| 1504 | bh = NULL; |
| 1505 | disk_super = NULL; |
| 1506 | if (!device) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame^] | 1507 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1508 | goto out; |
| 1509 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1510 | } else { |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1511 | ret = btrfs_get_bdev_and_sb(device_path, |
Lukas Czerner | cc975eb | 2012-12-07 10:09:19 +0000 | [diff] [blame] | 1512 | FMODE_WRITE | FMODE_EXCL, |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1513 | root->fs_info->bdev_holder, 0, |
| 1514 | &bdev, &bh); |
| 1515 | if (ret) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1516 | goto out; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1517 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1518 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1519 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1520 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1521 | disk_super->fsid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1522 | if (!device) { |
| 1523 | ret = -ENOENT; |
| 1524 | goto error_brelse; |
| 1525 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1526 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1527 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1528 | if (device->is_tgtdev_for_dev_replace) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame^] | 1529 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1530 | goto error_brelse; |
| 1531 | } |
| 1532 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1533 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame^] | 1534 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1535 | goto error_brelse; |
| 1536 | } |
| 1537 | |
| 1538 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1539 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1540 | list_del_init(&device->dev_alloc_list); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1541 | unlock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1542 | root->fs_info->fs_devices->rw_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1543 | clear_super = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1544 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1545 | |
| 1546 | ret = btrfs_shrink_device(device, 0); |
| 1547 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1548 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1549 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1550 | /* |
| 1551 | * TODO: the superblock still includes this device in its num_devices |
| 1552 | * counter although write_all_supers() is not locked out. This |
| 1553 | * could give a filesystem state which requires a degraded mount. |
| 1554 | */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1555 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1556 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1557 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1558 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 1559 | spin_lock(&root->fs_info->free_chunk_lock); |
| 1560 | root->fs_info->free_chunk_space = device->total_bytes - |
| 1561 | device->bytes_used; |
| 1562 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 1563 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1564 | device->in_fs_metadata = 0; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1565 | btrfs_scrub_cancel_dev(root->fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1566 | |
| 1567 | /* |
| 1568 | * the device list mutex makes sure that we don't change |
| 1569 | * the device list while someone else is writing out all |
| 1570 | * the device supers. |
| 1571 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1572 | |
| 1573 | cur_devices = device->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1574 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1575 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1576 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1577 | device->fs_devices->num_devices--; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1578 | device->fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1579 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1580 | if (device->missing) |
| 1581 | root->fs_info->fs_devices->missing_devices--; |
| 1582 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1583 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1584 | struct btrfs_device, dev_list); |
| 1585 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1586 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1587 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1588 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1589 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1590 | if (device->bdev) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1591 | device->fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1592 | |
| 1593 | call_rcu(&device->rcu, free_device); |
| 1594 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1595 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1596 | num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; |
| 1597 | btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1598 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1599 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1600 | struct btrfs_fs_devices *fs_devices; |
| 1601 | fs_devices = root->fs_info->fs_devices; |
| 1602 | while (fs_devices) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1603 | if (fs_devices->seed == cur_devices) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1604 | break; |
| 1605 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1606 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1607 | fs_devices->seed = cur_devices->seed; |
| 1608 | cur_devices->seed = NULL; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1609 | lock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1610 | __btrfs_close_devices(cur_devices); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1611 | unlock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1612 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1613 | } |
| 1614 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1615 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1616 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
| 1617 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1618 | /* |
| 1619 | * at this point, the device is zero sized. We want to |
| 1620 | * remove it from the devices list and zero out the old super |
| 1621 | */ |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1622 | if (clear_super && disk_super) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1623 | /* make sure this device isn't detected as part of |
| 1624 | * the FS anymore |
| 1625 | */ |
| 1626 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1627 | set_buffer_dirty(bh); |
| 1628 | sync_dirty_buffer(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1629 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1630 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1631 | ret = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1632 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 1633 | /* Notify udev that device has changed */ |
Eric Sandeen | 3c91160 | 2013-01-31 00:55:02 +0000 | [diff] [blame] | 1634 | if (bdev) |
| 1635 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 1636 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1637 | error_brelse: |
| 1638 | brelse(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1639 | if (bdev) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1640 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1641 | out: |
| 1642 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1643 | return ret; |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1644 | error_undo: |
| 1645 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1646 | lock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1647 | list_add(&device->dev_alloc_list, |
| 1648 | &root->fs_info->fs_devices->alloc_list); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1649 | unlock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1650 | root->fs_info->fs_devices->rw_devices++; |
| 1651 | } |
| 1652 | goto error_brelse; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1653 | } |
| 1654 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1655 | void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info, |
| 1656 | struct btrfs_device *srcdev) |
| 1657 | { |
| 1658 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
| 1659 | list_del_rcu(&srcdev->dev_list); |
| 1660 | list_del_rcu(&srcdev->dev_alloc_list); |
| 1661 | fs_info->fs_devices->num_devices--; |
| 1662 | if (srcdev->missing) { |
| 1663 | fs_info->fs_devices->missing_devices--; |
| 1664 | fs_info->fs_devices->rw_devices++; |
| 1665 | } |
| 1666 | if (srcdev->can_discard) |
| 1667 | fs_info->fs_devices->num_can_discard--; |
| 1668 | if (srcdev->bdev) |
| 1669 | fs_info->fs_devices->open_devices--; |
| 1670 | |
| 1671 | call_rcu(&srcdev->rcu, free_device); |
| 1672 | } |
| 1673 | |
| 1674 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 1675 | struct btrfs_device *tgtdev) |
| 1676 | { |
| 1677 | struct btrfs_device *next_device; |
| 1678 | |
| 1679 | WARN_ON(!tgtdev); |
| 1680 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 1681 | if (tgtdev->bdev) { |
| 1682 | btrfs_scratch_superblock(tgtdev); |
| 1683 | fs_info->fs_devices->open_devices--; |
| 1684 | } |
| 1685 | fs_info->fs_devices->num_devices--; |
| 1686 | if (tgtdev->can_discard) |
| 1687 | fs_info->fs_devices->num_can_discard++; |
| 1688 | |
| 1689 | next_device = list_entry(fs_info->fs_devices->devices.next, |
| 1690 | struct btrfs_device, dev_list); |
| 1691 | if (tgtdev->bdev == fs_info->sb->s_bdev) |
| 1692 | fs_info->sb->s_bdev = next_device->bdev; |
| 1693 | if (tgtdev->bdev == fs_info->fs_devices->latest_bdev) |
| 1694 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1695 | list_del_rcu(&tgtdev->dev_list); |
| 1696 | |
| 1697 | call_rcu(&tgtdev->rcu, free_device); |
| 1698 | |
| 1699 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
| 1700 | } |
| 1701 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1702 | static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
| 1703 | struct btrfs_device **device) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1704 | { |
| 1705 | int ret = 0; |
| 1706 | struct btrfs_super_block *disk_super; |
| 1707 | u64 devid; |
| 1708 | u8 *dev_uuid; |
| 1709 | struct block_device *bdev; |
| 1710 | struct buffer_head *bh; |
| 1711 | |
| 1712 | *device = NULL; |
| 1713 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
| 1714 | root->fs_info->bdev_holder, 0, &bdev, &bh); |
| 1715 | if (ret) |
| 1716 | return ret; |
| 1717 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1718 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 1719 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1720 | *device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1721 | disk_super->fsid); |
| 1722 | brelse(bh); |
| 1723 | if (!*device) |
| 1724 | ret = -ENOENT; |
| 1725 | blkdev_put(bdev, FMODE_READ); |
| 1726 | return ret; |
| 1727 | } |
| 1728 | |
| 1729 | int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, |
| 1730 | char *device_path, |
| 1731 | struct btrfs_device **device) |
| 1732 | { |
| 1733 | *device = NULL; |
| 1734 | if (strcmp(device_path, "missing") == 0) { |
| 1735 | struct list_head *devices; |
| 1736 | struct btrfs_device *tmp; |
| 1737 | |
| 1738 | devices = &root->fs_info->fs_devices->devices; |
| 1739 | /* |
| 1740 | * It is safe to read the devices since the volume_mutex |
| 1741 | * is held by the caller. |
| 1742 | */ |
| 1743 | list_for_each_entry(tmp, devices, dev_list) { |
| 1744 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 1745 | *device = tmp; |
| 1746 | break; |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | if (!*device) { |
| 1751 | pr_err("btrfs: no missing device found\n"); |
| 1752 | return -ENOENT; |
| 1753 | } |
| 1754 | |
| 1755 | return 0; |
| 1756 | } else { |
| 1757 | return btrfs_find_device_by_path(root, device_path, device); |
| 1758 | } |
| 1759 | } |
| 1760 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1761 | /* |
| 1762 | * does all the dirty work required for changing file system's UUID. |
| 1763 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1764 | static int btrfs_prepare_sprout(struct btrfs_root *root) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1765 | { |
| 1766 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 1767 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1768 | struct btrfs_fs_devices *seed_devices; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1769 | struct btrfs_super_block *disk_super = root->fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1770 | struct btrfs_device *device; |
| 1771 | u64 super_flags; |
| 1772 | |
| 1773 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1774 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1775 | return -EINVAL; |
| 1776 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1777 | seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
| 1778 | if (!seed_devices) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1779 | return -ENOMEM; |
| 1780 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1781 | old_devices = clone_fs_devices(fs_devices); |
| 1782 | if (IS_ERR(old_devices)) { |
| 1783 | kfree(seed_devices); |
| 1784 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1785 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1786 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1787 | list_add(&old_devices->list, &fs_uuids); |
| 1788 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1789 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 1790 | seed_devices->opened = 1; |
| 1791 | INIT_LIST_HEAD(&seed_devices->devices); |
| 1792 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1793 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1794 | |
| 1795 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1796 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 1797 | synchronize_rcu); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1798 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 1799 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1800 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
| 1801 | list_for_each_entry(device, &seed_devices->devices, dev_list) { |
| 1802 | device->fs_devices = seed_devices; |
| 1803 | } |
| 1804 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1805 | fs_devices->seeding = 0; |
| 1806 | fs_devices->num_devices = 0; |
| 1807 | fs_devices->open_devices = 0; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1808 | fs_devices->total_devices = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1809 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1810 | |
| 1811 | generate_random_uuid(fs_devices->fsid); |
| 1812 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 1813 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 1814 | super_flags = btrfs_super_flags(disk_super) & |
| 1815 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 1816 | btrfs_set_super_flags(disk_super, super_flags); |
| 1817 | |
| 1818 | return 0; |
| 1819 | } |
| 1820 | |
| 1821 | /* |
| 1822 | * strore the expected generation for seed devices in device items. |
| 1823 | */ |
| 1824 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 1825 | struct btrfs_root *root) |
| 1826 | { |
| 1827 | struct btrfs_path *path; |
| 1828 | struct extent_buffer *leaf; |
| 1829 | struct btrfs_dev_item *dev_item; |
| 1830 | struct btrfs_device *device; |
| 1831 | struct btrfs_key key; |
| 1832 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 1833 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 1834 | u64 devid; |
| 1835 | int ret; |
| 1836 | |
| 1837 | path = btrfs_alloc_path(); |
| 1838 | if (!path) |
| 1839 | return -ENOMEM; |
| 1840 | |
| 1841 | root = root->fs_info->chunk_root; |
| 1842 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1843 | key.offset = 0; |
| 1844 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1845 | |
| 1846 | while (1) { |
| 1847 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 1848 | if (ret < 0) |
| 1849 | goto error; |
| 1850 | |
| 1851 | leaf = path->nodes[0]; |
| 1852 | next_slot: |
| 1853 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1854 | ret = btrfs_next_leaf(root, path); |
| 1855 | if (ret > 0) |
| 1856 | break; |
| 1857 | if (ret < 0) |
| 1858 | goto error; |
| 1859 | leaf = path->nodes[0]; |
| 1860 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1861 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1862 | continue; |
| 1863 | } |
| 1864 | |
| 1865 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1866 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 1867 | key.type != BTRFS_DEV_ITEM_KEY) |
| 1868 | break; |
| 1869 | |
| 1870 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 1871 | struct btrfs_dev_item); |
| 1872 | devid = btrfs_device_id(leaf, dev_item); |
| 1873 | read_extent_buffer(leaf, dev_uuid, |
| 1874 | (unsigned long)btrfs_device_uuid(dev_item), |
| 1875 | BTRFS_UUID_SIZE); |
| 1876 | read_extent_buffer(leaf, fs_uuid, |
| 1877 | (unsigned long)btrfs_device_fsid(dev_item), |
| 1878 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1879 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
| 1880 | fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1881 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1882 | |
| 1883 | if (device->fs_devices->seeding) { |
| 1884 | btrfs_set_device_generation(leaf, dev_item, |
| 1885 | device->generation); |
| 1886 | btrfs_mark_buffer_dirty(leaf); |
| 1887 | } |
| 1888 | |
| 1889 | path->slots[0]++; |
| 1890 | goto next_slot; |
| 1891 | } |
| 1892 | ret = 0; |
| 1893 | error: |
| 1894 | btrfs_free_path(path); |
| 1895 | return ret; |
| 1896 | } |
| 1897 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1898 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 1899 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1900 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1901 | struct btrfs_trans_handle *trans; |
| 1902 | struct btrfs_device *device; |
| 1903 | struct block_device *bdev; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1904 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1905 | struct super_block *sb = root->fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1906 | struct rcu_string *name; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1907 | u64 total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1908 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1909 | int ret = 0; |
| 1910 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1911 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 1912 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1913 | |
Li Zefan | a5d1633 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1914 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1915 | root->fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 1916 | if (IS_ERR(bdev)) |
| 1917 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1918 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1919 | if (root->fs_info->fs_devices->seeding) { |
| 1920 | seeding_dev = 1; |
| 1921 | down_write(&sb->s_umount); |
| 1922 | mutex_lock(&uuid_mutex); |
| 1923 | } |
| 1924 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 1925 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1926 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1927 | devices = &root->fs_info->fs_devices->devices; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 1928 | |
| 1929 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1930 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1931 | if (device->bdev == bdev) { |
| 1932 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 1933 | mutex_unlock( |
| 1934 | &root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1935 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1936 | } |
| 1937 | } |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 1938 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1939 | |
| 1940 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 1941 | if (!device) { |
| 1942 | /* we can safely leave the fs_devices entry around */ |
| 1943 | ret = -ENOMEM; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1944 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1945 | } |
| 1946 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1947 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 1948 | if (!name) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1949 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1950 | ret = -ENOMEM; |
| 1951 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1952 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1953 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1954 | |
| 1955 | ret = find_next_devid(root, &device->devid); |
| 1956 | if (ret) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1957 | rcu_string_free(device->name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1958 | kfree(device); |
| 1959 | goto error; |
| 1960 | } |
| 1961 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1962 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1963 | if (IS_ERR(trans)) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1964 | rcu_string_free(device->name); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1965 | kfree(device); |
| 1966 | ret = PTR_ERR(trans); |
| 1967 | goto error; |
| 1968 | } |
| 1969 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1970 | lock_chunks(root); |
| 1971 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1972 | q = bdev_get_queue(bdev); |
| 1973 | if (blk_queue_discard(q)) |
| 1974 | device->can_discard = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1975 | device->writeable = 1; |
| 1976 | device->work.func = pending_bios_fn; |
| 1977 | generate_random_uuid(device->uuid); |
| 1978 | spin_lock_init(&device->io_lock); |
| 1979 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1980 | device->io_width = root->sectorsize; |
| 1981 | device->io_align = root->sectorsize; |
| 1982 | device->sector_size = root->sectorsize; |
| 1983 | device->total_bytes = i_size_read(bdev->bd_inode); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 1984 | device->disk_total_bytes = device->total_bytes; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1985 | device->dev_root = root->fs_info->dev_root; |
| 1986 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1987 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1988 | device->is_tgtdev_for_dev_replace = 0; |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 1989 | device->mode = FMODE_EXCL; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 1990 | set_blocksize(device->bdev, 4096); |
| 1991 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1992 | if (seeding_dev) { |
| 1993 | sb->s_flags &= ~MS_RDONLY; |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1994 | ret = btrfs_prepare_sprout(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1995 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | device->fs_devices = root->fs_info->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1999 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2000 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2001 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2002 | list_add(&device->dev_alloc_list, |
| 2003 | &root->fs_info->fs_devices->alloc_list); |
| 2004 | root->fs_info->fs_devices->num_devices++; |
| 2005 | root->fs_info->fs_devices->open_devices++; |
| 2006 | root->fs_info->fs_devices->rw_devices++; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 2007 | root->fs_info->fs_devices->total_devices++; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2008 | if (device->can_discard) |
| 2009 | root->fs_info->fs_devices->num_can_discard++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2010 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 2011 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2012 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2013 | root->fs_info->free_chunk_space += device->total_bytes; |
| 2014 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2015 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2016 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 2017 | root->fs_info->fs_devices->rotating = 1; |
| 2018 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2019 | total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy); |
| 2020 | btrfs_set_super_total_bytes(root->fs_info->super_copy, |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2021 | total_bytes + device->total_bytes); |
| 2022 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2023 | total_bytes = btrfs_super_num_devices(root->fs_info->super_copy); |
| 2024 | btrfs_set_super_num_devices(root->fs_info->super_copy, |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2025 | total_bytes + 1); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2026 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2027 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2028 | if (seeding_dev) { |
| 2029 | ret = init_first_rw_device(trans, root, device); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2030 | if (ret) { |
| 2031 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2032 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2033 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2034 | ret = btrfs_finish_sprout(trans, root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2035 | if (ret) { |
| 2036 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2037 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2038 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2039 | } else { |
| 2040 | ret = btrfs_add_device(trans, root, device); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2041 | if (ret) { |
| 2042 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2043 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2044 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2045 | } |
| 2046 | |
Chris Mason | 913d952 | 2009-03-10 13:17:18 -0400 | [diff] [blame] | 2047 | /* |
| 2048 | * we've got more storage, clear any full flags on the space |
| 2049 | * infos |
| 2050 | */ |
| 2051 | btrfs_clear_space_info_full(root->fs_info); |
| 2052 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2053 | unlock_chunks(root); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2054 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 2055 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2056 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2057 | |
| 2058 | if (seeding_dev) { |
| 2059 | mutex_unlock(&uuid_mutex); |
| 2060 | up_write(&sb->s_umount); |
| 2061 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2062 | if (ret) /* transaction commit */ |
| 2063 | return ret; |
| 2064 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2065 | ret = btrfs_relocate_sys_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2066 | if (ret < 0) |
| 2067 | btrfs_error(root->fs_info, ret, |
| 2068 | "Failed to relocate sys chunks after " |
| 2069 | "device initialization. This can be fixed " |
| 2070 | "using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2071 | trans = btrfs_attach_transaction(root); |
| 2072 | if (IS_ERR(trans)) { |
| 2073 | if (PTR_ERR(trans) == -ENOENT) |
| 2074 | return 0; |
| 2075 | return PTR_ERR(trans); |
| 2076 | } |
| 2077 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2078 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2079 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2080 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2081 | |
| 2082 | error_trans: |
| 2083 | unlock_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2084 | btrfs_end_transaction(trans, root); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2085 | rcu_string_free(device->name); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2086 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2087 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2088 | blkdev_put(bdev, FMODE_EXCL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2089 | if (seeding_dev) { |
| 2090 | mutex_unlock(&uuid_mutex); |
| 2091 | up_write(&sb->s_umount); |
| 2092 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2093 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2094 | } |
| 2095 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2096 | int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path, |
| 2097 | struct btrfs_device **device_out) |
| 2098 | { |
| 2099 | struct request_queue *q; |
| 2100 | struct btrfs_device *device; |
| 2101 | struct block_device *bdev; |
| 2102 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2103 | struct list_head *devices; |
| 2104 | struct rcu_string *name; |
| 2105 | int ret = 0; |
| 2106 | |
| 2107 | *device_out = NULL; |
| 2108 | if (fs_info->fs_devices->seeding) |
| 2109 | return -EINVAL; |
| 2110 | |
| 2111 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| 2112 | fs_info->bdev_holder); |
| 2113 | if (IS_ERR(bdev)) |
| 2114 | return PTR_ERR(bdev); |
| 2115 | |
| 2116 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 2117 | |
| 2118 | devices = &fs_info->fs_devices->devices; |
| 2119 | list_for_each_entry(device, devices, dev_list) { |
| 2120 | if (device->bdev == bdev) { |
| 2121 | ret = -EEXIST; |
| 2122 | goto error; |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 2127 | if (!device) { |
| 2128 | ret = -ENOMEM; |
| 2129 | goto error; |
| 2130 | } |
| 2131 | |
| 2132 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2133 | if (!name) { |
| 2134 | kfree(device); |
| 2135 | ret = -ENOMEM; |
| 2136 | goto error; |
| 2137 | } |
| 2138 | rcu_assign_pointer(device->name, name); |
| 2139 | |
| 2140 | q = bdev_get_queue(bdev); |
| 2141 | if (blk_queue_discard(q)) |
| 2142 | device->can_discard = 1; |
| 2143 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 2144 | device->writeable = 1; |
| 2145 | device->work.func = pending_bios_fn; |
| 2146 | generate_random_uuid(device->uuid); |
| 2147 | device->devid = BTRFS_DEV_REPLACE_DEVID; |
| 2148 | spin_lock_init(&device->io_lock); |
| 2149 | device->generation = 0; |
| 2150 | device->io_width = root->sectorsize; |
| 2151 | device->io_align = root->sectorsize; |
| 2152 | device->sector_size = root->sectorsize; |
| 2153 | device->total_bytes = i_size_read(bdev->bd_inode); |
| 2154 | device->disk_total_bytes = device->total_bytes; |
| 2155 | device->dev_root = fs_info->dev_root; |
| 2156 | device->bdev = bdev; |
| 2157 | device->in_fs_metadata = 1; |
| 2158 | device->is_tgtdev_for_dev_replace = 1; |
| 2159 | device->mode = FMODE_EXCL; |
| 2160 | set_blocksize(device->bdev, 4096); |
| 2161 | device->fs_devices = fs_info->fs_devices; |
| 2162 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 2163 | fs_info->fs_devices->num_devices++; |
| 2164 | fs_info->fs_devices->open_devices++; |
| 2165 | if (device->can_discard) |
| 2166 | fs_info->fs_devices->num_can_discard++; |
| 2167 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2168 | |
| 2169 | *device_out = device; |
| 2170 | return ret; |
| 2171 | |
| 2172 | error: |
| 2173 | blkdev_put(bdev, FMODE_EXCL); |
| 2174 | return ret; |
| 2175 | } |
| 2176 | |
| 2177 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, |
| 2178 | struct btrfs_device *tgtdev) |
| 2179 | { |
| 2180 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 2181 | tgtdev->io_width = fs_info->dev_root->sectorsize; |
| 2182 | tgtdev->io_align = fs_info->dev_root->sectorsize; |
| 2183 | tgtdev->sector_size = fs_info->dev_root->sectorsize; |
| 2184 | tgtdev->dev_root = fs_info->dev_root; |
| 2185 | tgtdev->in_fs_metadata = 1; |
| 2186 | } |
| 2187 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2188 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2189 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2190 | { |
| 2191 | int ret; |
| 2192 | struct btrfs_path *path; |
| 2193 | struct btrfs_root *root; |
| 2194 | struct btrfs_dev_item *dev_item; |
| 2195 | struct extent_buffer *leaf; |
| 2196 | struct btrfs_key key; |
| 2197 | |
| 2198 | root = device->dev_root->fs_info->chunk_root; |
| 2199 | |
| 2200 | path = btrfs_alloc_path(); |
| 2201 | if (!path) |
| 2202 | return -ENOMEM; |
| 2203 | |
| 2204 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2205 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2206 | key.offset = device->devid; |
| 2207 | |
| 2208 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2209 | if (ret < 0) |
| 2210 | goto out; |
| 2211 | |
| 2212 | if (ret > 0) { |
| 2213 | ret = -ENOENT; |
| 2214 | goto out; |
| 2215 | } |
| 2216 | |
| 2217 | leaf = path->nodes[0]; |
| 2218 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2219 | |
| 2220 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2221 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2222 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2223 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2224 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 2225 | btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2226 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
| 2227 | btrfs_mark_buffer_dirty(leaf); |
| 2228 | |
| 2229 | out: |
| 2230 | btrfs_free_path(path); |
| 2231 | return ret; |
| 2232 | } |
| 2233 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2234 | static int __btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2235 | struct btrfs_device *device, u64 new_size) |
| 2236 | { |
| 2237 | struct btrfs_super_block *super_copy = |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2238 | device->dev_root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2239 | u64 old_total = btrfs_super_total_bytes(super_copy); |
| 2240 | u64 diff = new_size - device->total_bytes; |
| 2241 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2242 | if (!device->writeable) |
| 2243 | return -EACCES; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2244 | if (new_size <= device->total_bytes || |
| 2245 | device->is_tgtdev_for_dev_replace) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2246 | return -EINVAL; |
| 2247 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2248 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2249 | device->fs_devices->total_rw_bytes += diff; |
| 2250 | |
| 2251 | device->total_bytes = new_size; |
Chris Mason | 9779b72 | 2009-07-24 16:41:41 -0400 | [diff] [blame] | 2252 | device->disk_total_bytes = new_size; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2253 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
| 2254 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2255 | return btrfs_update_device(trans, device); |
| 2256 | } |
| 2257 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2258 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
| 2259 | struct btrfs_device *device, u64 new_size) |
| 2260 | { |
| 2261 | int ret; |
| 2262 | lock_chunks(device->dev_root); |
| 2263 | ret = __btrfs_grow_device(trans, device, new_size); |
| 2264 | unlock_chunks(device->dev_root); |
| 2265 | return ret; |
| 2266 | } |
| 2267 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2268 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
| 2269 | struct btrfs_root *root, |
| 2270 | u64 chunk_tree, u64 chunk_objectid, |
| 2271 | u64 chunk_offset) |
| 2272 | { |
| 2273 | int ret; |
| 2274 | struct btrfs_path *path; |
| 2275 | struct btrfs_key key; |
| 2276 | |
| 2277 | root = root->fs_info->chunk_root; |
| 2278 | path = btrfs_alloc_path(); |
| 2279 | if (!path) |
| 2280 | return -ENOMEM; |
| 2281 | |
| 2282 | key.objectid = chunk_objectid; |
| 2283 | key.offset = chunk_offset; |
| 2284 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2285 | |
| 2286 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2287 | if (ret < 0) |
| 2288 | goto out; |
| 2289 | else if (ret > 0) { /* Logic error or corruption */ |
| 2290 | btrfs_error(root->fs_info, -ENOENT, |
| 2291 | "Failed lookup while freeing chunk."); |
| 2292 | ret = -ENOENT; |
| 2293 | goto out; |
| 2294 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2295 | |
| 2296 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2297 | if (ret < 0) |
| 2298 | btrfs_error(root->fs_info, ret, |
| 2299 | "Failed to delete chunk item."); |
| 2300 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2301 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2302 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2303 | } |
| 2304 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2305 | 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] | 2306 | chunk_offset) |
| 2307 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2308 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2309 | struct btrfs_disk_key *disk_key; |
| 2310 | struct btrfs_chunk *chunk; |
| 2311 | u8 *ptr; |
| 2312 | int ret = 0; |
| 2313 | u32 num_stripes; |
| 2314 | u32 array_size; |
| 2315 | u32 len = 0; |
| 2316 | u32 cur; |
| 2317 | struct btrfs_key key; |
| 2318 | |
| 2319 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2320 | |
| 2321 | ptr = super_copy->sys_chunk_array; |
| 2322 | cur = 0; |
| 2323 | |
| 2324 | while (cur < array_size) { |
| 2325 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2326 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2327 | |
| 2328 | len = sizeof(*disk_key); |
| 2329 | |
| 2330 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2331 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2332 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2333 | len += btrfs_chunk_item_size(num_stripes); |
| 2334 | } else { |
| 2335 | ret = -EIO; |
| 2336 | break; |
| 2337 | } |
| 2338 | if (key.objectid == chunk_objectid && |
| 2339 | key.offset == chunk_offset) { |
| 2340 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2341 | array_size -= len; |
| 2342 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2343 | } else { |
| 2344 | ptr += len; |
| 2345 | cur += len; |
| 2346 | } |
| 2347 | } |
| 2348 | return ret; |
| 2349 | } |
| 2350 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2351 | static int btrfs_relocate_chunk(struct btrfs_root *root, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2352 | u64 chunk_tree, u64 chunk_objectid, |
| 2353 | u64 chunk_offset) |
| 2354 | { |
| 2355 | struct extent_map_tree *em_tree; |
| 2356 | struct btrfs_root *extent_root; |
| 2357 | struct btrfs_trans_handle *trans; |
| 2358 | struct extent_map *em; |
| 2359 | struct map_lookup *map; |
| 2360 | int ret; |
| 2361 | int i; |
| 2362 | |
| 2363 | root = root->fs_info->chunk_root; |
| 2364 | extent_root = root->fs_info->extent_root; |
| 2365 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 2366 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2367 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
| 2368 | if (ret) |
| 2369 | return -ENOSPC; |
| 2370 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2371 | /* step one, relocate all the extents inside this chunk */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2372 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2373 | if (ret) |
| 2374 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2375 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2376 | trans = btrfs_start_transaction(root, 0); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 2377 | if (IS_ERR(trans)) { |
| 2378 | ret = PTR_ERR(trans); |
| 2379 | btrfs_std_error(root->fs_info, ret); |
| 2380 | return ret; |
| 2381 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2382 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2383 | lock_chunks(root); |
| 2384 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2385 | /* |
| 2386 | * step two, delete the device extents and the |
| 2387 | * chunk tree entries |
| 2388 | */ |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2389 | read_lock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2390 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2391 | read_unlock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2392 | |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 2393 | BUG_ON(!em || em->start > chunk_offset || |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2394 | em->start + em->len < chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2395 | map = (struct map_lookup *)em->bdev; |
| 2396 | |
| 2397 | for (i = 0; i < map->num_stripes; i++) { |
| 2398 | ret = btrfs_free_dev_extent(trans, map->stripes[i].dev, |
| 2399 | map->stripes[i].physical); |
| 2400 | BUG_ON(ret); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2401 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2402 | if (map->stripes[i].dev) { |
| 2403 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2404 | BUG_ON(ret); |
| 2405 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2406 | } |
| 2407 | ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid, |
| 2408 | chunk_offset); |
| 2409 | |
| 2410 | BUG_ON(ret); |
| 2411 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2412 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
| 2413 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2414 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2415 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 2416 | BUG_ON(ret); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2417 | } |
| 2418 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2419 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset); |
| 2420 | BUG_ON(ret); |
| 2421 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2422 | write_lock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2423 | remove_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2424 | write_unlock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2425 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2426 | kfree(map); |
| 2427 | em->bdev = NULL; |
| 2428 | |
| 2429 | /* once for the tree */ |
| 2430 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2431 | /* once for us */ |
| 2432 | free_extent_map(em); |
| 2433 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2434 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2435 | btrfs_end_transaction(trans, root); |
| 2436 | return 0; |
| 2437 | } |
| 2438 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2439 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 2440 | { |
| 2441 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 2442 | struct btrfs_path *path; |
| 2443 | struct extent_buffer *leaf; |
| 2444 | struct btrfs_chunk *chunk; |
| 2445 | struct btrfs_key key; |
| 2446 | struct btrfs_key found_key; |
| 2447 | u64 chunk_tree = chunk_root->root_key.objectid; |
| 2448 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2449 | bool retried = false; |
| 2450 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2451 | int ret; |
| 2452 | |
| 2453 | path = btrfs_alloc_path(); |
| 2454 | if (!path) |
| 2455 | return -ENOMEM; |
| 2456 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2457 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2458 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2459 | key.offset = (u64)-1; |
| 2460 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2461 | |
| 2462 | while (1) { |
| 2463 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 2464 | if (ret < 0) |
| 2465 | goto error; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2466 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2467 | |
| 2468 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2469 | key.type); |
| 2470 | if (ret < 0) |
| 2471 | goto error; |
| 2472 | if (ret > 0) |
| 2473 | break; |
| 2474 | |
| 2475 | leaf = path->nodes[0]; |
| 2476 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2477 | |
| 2478 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2479 | struct btrfs_chunk); |
| 2480 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2481 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2482 | |
| 2483 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2484 | ret = btrfs_relocate_chunk(chunk_root, chunk_tree, |
| 2485 | found_key.objectid, |
| 2486 | found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2487 | if (ret == -ENOSPC) |
| 2488 | failed++; |
| 2489 | else if (ret) |
| 2490 | BUG(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | if (found_key.offset == 0) |
| 2494 | break; |
| 2495 | key.offset = found_key.offset - 1; |
| 2496 | } |
| 2497 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2498 | if (failed && !retried) { |
| 2499 | failed = 0; |
| 2500 | retried = true; |
| 2501 | goto again; |
| 2502 | } else if (failed && retried) { |
| 2503 | WARN_ON(1); |
| 2504 | ret = -ENOSPC; |
| 2505 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2506 | error: |
| 2507 | btrfs_free_path(path); |
| 2508 | return ret; |
| 2509 | } |
| 2510 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2511 | static int insert_balance_item(struct btrfs_root *root, |
| 2512 | struct btrfs_balance_control *bctl) |
| 2513 | { |
| 2514 | struct btrfs_trans_handle *trans; |
| 2515 | struct btrfs_balance_item *item; |
| 2516 | struct btrfs_disk_balance_args disk_bargs; |
| 2517 | struct btrfs_path *path; |
| 2518 | struct extent_buffer *leaf; |
| 2519 | struct btrfs_key key; |
| 2520 | int ret, err; |
| 2521 | |
| 2522 | path = btrfs_alloc_path(); |
| 2523 | if (!path) |
| 2524 | return -ENOMEM; |
| 2525 | |
| 2526 | trans = btrfs_start_transaction(root, 0); |
| 2527 | if (IS_ERR(trans)) { |
| 2528 | btrfs_free_path(path); |
| 2529 | return PTR_ERR(trans); |
| 2530 | } |
| 2531 | |
| 2532 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2533 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2534 | key.offset = 0; |
| 2535 | |
| 2536 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2537 | sizeof(*item)); |
| 2538 | if (ret) |
| 2539 | goto out; |
| 2540 | |
| 2541 | leaf = path->nodes[0]; |
| 2542 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 2543 | |
| 2544 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 2545 | |
| 2546 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 2547 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 2548 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 2549 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 2550 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 2551 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 2552 | |
| 2553 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 2554 | |
| 2555 | btrfs_mark_buffer_dirty(leaf); |
| 2556 | out: |
| 2557 | btrfs_free_path(path); |
| 2558 | err = btrfs_commit_transaction(trans, root); |
| 2559 | if (err && !ret) |
| 2560 | ret = err; |
| 2561 | return ret; |
| 2562 | } |
| 2563 | |
| 2564 | static int del_balance_item(struct btrfs_root *root) |
| 2565 | { |
| 2566 | struct btrfs_trans_handle *trans; |
| 2567 | struct btrfs_path *path; |
| 2568 | struct btrfs_key key; |
| 2569 | int ret, err; |
| 2570 | |
| 2571 | path = btrfs_alloc_path(); |
| 2572 | if (!path) |
| 2573 | return -ENOMEM; |
| 2574 | |
| 2575 | trans = btrfs_start_transaction(root, 0); |
| 2576 | if (IS_ERR(trans)) { |
| 2577 | btrfs_free_path(path); |
| 2578 | return PTR_ERR(trans); |
| 2579 | } |
| 2580 | |
| 2581 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2582 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2583 | key.offset = 0; |
| 2584 | |
| 2585 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2586 | if (ret < 0) |
| 2587 | goto out; |
| 2588 | if (ret > 0) { |
| 2589 | ret = -ENOENT; |
| 2590 | goto out; |
| 2591 | } |
| 2592 | |
| 2593 | ret = btrfs_del_item(trans, root, path); |
| 2594 | out: |
| 2595 | btrfs_free_path(path); |
| 2596 | err = btrfs_commit_transaction(trans, root); |
| 2597 | if (err && !ret) |
| 2598 | ret = err; |
| 2599 | return ret; |
| 2600 | } |
| 2601 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2602 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2603 | * This is a heuristic used to reduce the number of chunks balanced on |
| 2604 | * resume after balance was interrupted. |
| 2605 | */ |
| 2606 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 2607 | { |
| 2608 | /* |
| 2609 | * Turn on soft mode for chunk types that were being converted. |
| 2610 | */ |
| 2611 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2612 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2613 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2614 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2615 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2616 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2617 | |
| 2618 | /* |
| 2619 | * Turn on usage filter if is not already used. The idea is |
| 2620 | * that chunks that we have already balanced should be |
| 2621 | * reasonably full. Don't do it for chunks that are being |
| 2622 | * converted - that will keep us from relocating unconverted |
| 2623 | * (albeit full) chunks. |
| 2624 | */ |
| 2625 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2626 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2627 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2628 | bctl->data.usage = 90; |
| 2629 | } |
| 2630 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2631 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2632 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2633 | bctl->sys.usage = 90; |
| 2634 | } |
| 2635 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2636 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2637 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2638 | bctl->meta.usage = 90; |
| 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | /* |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2643 | * Should be called with both balance and volume mutexes held to |
| 2644 | * serialize other volume operations (add_dev/rm_dev/resize) with |
| 2645 | * restriper. Same goes for unset_balance_control. |
| 2646 | */ |
| 2647 | static void set_balance_control(struct btrfs_balance_control *bctl) |
| 2648 | { |
| 2649 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
| 2650 | |
| 2651 | BUG_ON(fs_info->balance_ctl); |
| 2652 | |
| 2653 | spin_lock(&fs_info->balance_lock); |
| 2654 | fs_info->balance_ctl = bctl; |
| 2655 | spin_unlock(&fs_info->balance_lock); |
| 2656 | } |
| 2657 | |
| 2658 | static void unset_balance_control(struct btrfs_fs_info *fs_info) |
| 2659 | { |
| 2660 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 2661 | |
| 2662 | BUG_ON(!fs_info->balance_ctl); |
| 2663 | |
| 2664 | spin_lock(&fs_info->balance_lock); |
| 2665 | fs_info->balance_ctl = NULL; |
| 2666 | spin_unlock(&fs_info->balance_lock); |
| 2667 | |
| 2668 | kfree(bctl); |
| 2669 | } |
| 2670 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2671 | /* |
| 2672 | * Balance filters. Return 1 if chunk should be filtered out |
| 2673 | * (should not be balanced). |
| 2674 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2675 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2676 | struct btrfs_balance_args *bargs) |
| 2677 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2678 | chunk_type = chunk_to_extended(chunk_type) & |
| 2679 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2680 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2681 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2682 | return 0; |
| 2683 | |
| 2684 | return 1; |
| 2685 | } |
| 2686 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2687 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
| 2688 | struct btrfs_balance_args *bargs) |
| 2689 | { |
| 2690 | struct btrfs_block_group_cache *cache; |
| 2691 | u64 chunk_used, user_thresh; |
| 2692 | int ret = 1; |
| 2693 | |
| 2694 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 2695 | chunk_used = btrfs_block_group_used(&cache->item); |
| 2696 | |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 2697 | if (bargs->usage == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 2698 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 2699 | else if (bargs->usage > 100) |
| 2700 | user_thresh = cache->key.offset; |
| 2701 | else |
| 2702 | user_thresh = div_factor_fine(cache->key.offset, |
| 2703 | bargs->usage); |
| 2704 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2705 | if (chunk_used < user_thresh) |
| 2706 | ret = 0; |
| 2707 | |
| 2708 | btrfs_put_block_group(cache); |
| 2709 | return ret; |
| 2710 | } |
| 2711 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2712 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 2713 | struct btrfs_chunk *chunk, |
| 2714 | struct btrfs_balance_args *bargs) |
| 2715 | { |
| 2716 | struct btrfs_stripe *stripe; |
| 2717 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2718 | int i; |
| 2719 | |
| 2720 | for (i = 0; i < num_stripes; i++) { |
| 2721 | stripe = btrfs_stripe_nr(chunk, i); |
| 2722 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 2723 | return 0; |
| 2724 | } |
| 2725 | |
| 2726 | return 1; |
| 2727 | } |
| 2728 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2729 | /* [pstart, pend) */ |
| 2730 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 2731 | struct btrfs_chunk *chunk, |
| 2732 | u64 chunk_offset, |
| 2733 | struct btrfs_balance_args *bargs) |
| 2734 | { |
| 2735 | struct btrfs_stripe *stripe; |
| 2736 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2737 | u64 stripe_offset; |
| 2738 | u64 stripe_length; |
| 2739 | int factor; |
| 2740 | int i; |
| 2741 | |
| 2742 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 2743 | return 0; |
| 2744 | |
| 2745 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2746 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 2747 | factor = num_stripes / 2; |
| 2748 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 2749 | factor = num_stripes - 1; |
| 2750 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 2751 | factor = num_stripes - 2; |
| 2752 | } else { |
| 2753 | factor = num_stripes; |
| 2754 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2755 | |
| 2756 | for (i = 0; i < num_stripes; i++) { |
| 2757 | stripe = btrfs_stripe_nr(chunk, i); |
| 2758 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 2759 | continue; |
| 2760 | |
| 2761 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 2762 | stripe_length = btrfs_chunk_length(leaf, chunk); |
| 2763 | do_div(stripe_length, factor); |
| 2764 | |
| 2765 | if (stripe_offset < bargs->pend && |
| 2766 | stripe_offset + stripe_length > bargs->pstart) |
| 2767 | return 0; |
| 2768 | } |
| 2769 | |
| 2770 | return 1; |
| 2771 | } |
| 2772 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2773 | /* [vstart, vend) */ |
| 2774 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 2775 | struct btrfs_chunk *chunk, |
| 2776 | u64 chunk_offset, |
| 2777 | struct btrfs_balance_args *bargs) |
| 2778 | { |
| 2779 | if (chunk_offset < bargs->vend && |
| 2780 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 2781 | /* at least part of the chunk is inside this vrange */ |
| 2782 | return 0; |
| 2783 | |
| 2784 | return 1; |
| 2785 | } |
| 2786 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2787 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2788 | struct btrfs_balance_args *bargs) |
| 2789 | { |
| 2790 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 2791 | return 0; |
| 2792 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2793 | chunk_type = chunk_to_extended(chunk_type) & |
| 2794 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2795 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2796 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2797 | return 1; |
| 2798 | |
| 2799 | return 0; |
| 2800 | } |
| 2801 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2802 | static int should_balance_chunk(struct btrfs_root *root, |
| 2803 | struct extent_buffer *leaf, |
| 2804 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 2805 | { |
| 2806 | struct btrfs_balance_control *bctl = root->fs_info->balance_ctl; |
| 2807 | struct btrfs_balance_args *bargs = NULL; |
| 2808 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 2809 | |
| 2810 | /* type filter */ |
| 2811 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 2812 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 2813 | return 0; |
| 2814 | } |
| 2815 | |
| 2816 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 2817 | bargs = &bctl->data; |
| 2818 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2819 | bargs = &bctl->sys; |
| 2820 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 2821 | bargs = &bctl->meta; |
| 2822 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2823 | /* profiles filter */ |
| 2824 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 2825 | chunk_profiles_filter(chunk_type, bargs)) { |
| 2826 | return 0; |
| 2827 | } |
| 2828 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2829 | /* usage filter */ |
| 2830 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2831 | chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) { |
| 2832 | return 0; |
| 2833 | } |
| 2834 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2835 | /* devid filter */ |
| 2836 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 2837 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 2838 | return 0; |
| 2839 | } |
| 2840 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2841 | /* drange filter, makes sense only with devid filter */ |
| 2842 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
| 2843 | chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 2844 | return 0; |
| 2845 | } |
| 2846 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2847 | /* vrange filter */ |
| 2848 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 2849 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 2850 | return 0; |
| 2851 | } |
| 2852 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2853 | /* soft profile changing mode */ |
| 2854 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 2855 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 2856 | return 0; |
| 2857 | } |
| 2858 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2859 | return 1; |
| 2860 | } |
| 2861 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2862 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2863 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2864 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2865 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 2866 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 2867 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2868 | struct btrfs_device *device; |
| 2869 | u64 old_size; |
| 2870 | u64 size_to_free; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2871 | struct btrfs_chunk *chunk; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2872 | struct btrfs_path *path; |
| 2873 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2874 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2875 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2876 | struct extent_buffer *leaf; |
| 2877 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2878 | int ret; |
| 2879 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2880 | bool counting = true; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2881 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2882 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2883 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2884 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2885 | old_size = device->total_bytes; |
| 2886 | size_to_free = div_factor(old_size, 1); |
| 2887 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2888 | if (!device->writeable || |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2889 | device->total_bytes - device->bytes_used > size_to_free || |
| 2890 | device->is_tgtdev_for_dev_replace) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2891 | continue; |
| 2892 | |
| 2893 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2894 | if (ret == -ENOSPC) |
| 2895 | break; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2896 | BUG_ON(ret); |
| 2897 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2898 | trans = btrfs_start_transaction(dev_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2899 | BUG_ON(IS_ERR(trans)); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2900 | |
| 2901 | ret = btrfs_grow_device(trans, device, old_size); |
| 2902 | BUG_ON(ret); |
| 2903 | |
| 2904 | btrfs_end_transaction(trans, dev_root); |
| 2905 | } |
| 2906 | |
| 2907 | /* step two, relocate all the chunks */ |
| 2908 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 2909 | if (!path) { |
| 2910 | ret = -ENOMEM; |
| 2911 | goto error; |
| 2912 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2913 | |
| 2914 | /* zero out stat counters */ |
| 2915 | spin_lock(&fs_info->balance_lock); |
| 2916 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 2917 | spin_unlock(&fs_info->balance_lock); |
| 2918 | again: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2919 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2920 | key.offset = (u64)-1; |
| 2921 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2922 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2923 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2924 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2925 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2926 | ret = -ECANCELED; |
| 2927 | goto error; |
| 2928 | } |
| 2929 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2930 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 2931 | if (ret < 0) |
| 2932 | goto error; |
| 2933 | |
| 2934 | /* |
| 2935 | * this shouldn't happen, it means the last relocate |
| 2936 | * failed |
| 2937 | */ |
| 2938 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2939 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2940 | |
| 2941 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 2942 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2943 | if (ret) { |
| 2944 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2945 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2946 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2947 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2948 | leaf = path->nodes[0]; |
| 2949 | slot = path->slots[0]; |
| 2950 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2951 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2952 | if (found_key.objectid != key.objectid) |
| 2953 | break; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2954 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2955 | /* chunk zero is special */ |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2956 | if (found_key.offset == 0) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2957 | break; |
| 2958 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2959 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 2960 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2961 | if (!counting) { |
| 2962 | spin_lock(&fs_info->balance_lock); |
| 2963 | bctl->stat.considered++; |
| 2964 | spin_unlock(&fs_info->balance_lock); |
| 2965 | } |
| 2966 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2967 | ret = should_balance_chunk(chunk_root, leaf, chunk, |
| 2968 | found_key.offset); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2969 | btrfs_release_path(path); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2970 | if (!ret) |
| 2971 | goto loop; |
| 2972 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2973 | if (counting) { |
| 2974 | spin_lock(&fs_info->balance_lock); |
| 2975 | bctl->stat.expected++; |
| 2976 | spin_unlock(&fs_info->balance_lock); |
| 2977 | goto loop; |
| 2978 | } |
| 2979 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2980 | ret = btrfs_relocate_chunk(chunk_root, |
| 2981 | chunk_root->root_key.objectid, |
| 2982 | found_key.objectid, |
| 2983 | found_key.offset); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 2984 | if (ret && ret != -ENOSPC) |
| 2985 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2986 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2987 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2988 | } else { |
| 2989 | spin_lock(&fs_info->balance_lock); |
| 2990 | bctl->stat.completed++; |
| 2991 | spin_unlock(&fs_info->balance_lock); |
| 2992 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2993 | loop: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2994 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2995 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2996 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2997 | if (counting) { |
| 2998 | btrfs_release_path(path); |
| 2999 | counting = false; |
| 3000 | goto again; |
| 3001 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3002 | error: |
| 3003 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3004 | if (enospc_errors) { |
| 3005 | printk(KERN_INFO "btrfs: %d enospc errors during balance\n", |
| 3006 | enospc_errors); |
| 3007 | if (!ret) |
| 3008 | ret = -ENOSPC; |
| 3009 | } |
| 3010 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3011 | return ret; |
| 3012 | } |
| 3013 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3014 | /** |
| 3015 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3016 | * @flags: profile to validate |
| 3017 | * @extended: if true @flags is treated as an extended profile |
| 3018 | */ |
| 3019 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3020 | { |
| 3021 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3022 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3023 | |
| 3024 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3025 | |
| 3026 | /* 1) check that all other bits are zeroed */ |
| 3027 | if (flags & ~mask) |
| 3028 | return 0; |
| 3029 | |
| 3030 | /* 2) see if profile is reduced */ |
| 3031 | if (flags == 0) |
| 3032 | return !extended; /* "0" is valid for usual profiles */ |
| 3033 | |
| 3034 | /* true if exactly one bit set */ |
| 3035 | return (flags & (flags - 1)) == 0; |
| 3036 | } |
| 3037 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3038 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3039 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3040 | /* cancel requested || normal exit path */ |
| 3041 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3042 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3043 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3044 | } |
| 3045 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3046 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
| 3047 | { |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3048 | int ret; |
| 3049 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3050 | unset_balance_control(fs_info); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3051 | ret = del_balance_item(fs_info->tree_root); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 3052 | if (ret) |
| 3053 | btrfs_std_error(fs_info, ret); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3054 | |
| 3055 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3056 | } |
| 3057 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3058 | 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] | 3059 | struct btrfs_ioctl_balance_args *bargs); |
| 3060 | |
| 3061 | /* |
| 3062 | * Should be called with both balance and volume mutexes held |
| 3063 | */ |
| 3064 | int btrfs_balance(struct btrfs_balance_control *bctl, |
| 3065 | struct btrfs_ioctl_balance_args *bargs) |
| 3066 | { |
| 3067 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3068 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3069 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3070 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3071 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3072 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3073 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3074 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3075 | atomic_read(&fs_info->balance_pause_req) || |
| 3076 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3077 | ret = -EINVAL; |
| 3078 | goto out; |
| 3079 | } |
| 3080 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3081 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3082 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3083 | mixed = 1; |
| 3084 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3085 | /* |
| 3086 | * In case of mixed groups both data and meta should be picked, |
| 3087 | * and identical options should be given for both of them. |
| 3088 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3089 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3090 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3091 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3092 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3093 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
| 3094 | printk(KERN_ERR "btrfs: with mixed groups data and " |
| 3095 | "metadata balance options must be the same\n"); |
| 3096 | ret = -EINVAL; |
| 3097 | goto out; |
| 3098 | } |
| 3099 | } |
| 3100 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3101 | num_devices = fs_info->fs_devices->num_devices; |
| 3102 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 3103 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 3104 | BUG_ON(num_devices < 1); |
| 3105 | num_devices--; |
| 3106 | } |
| 3107 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3108 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3109 | if (num_devices == 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3110 | allowed |= BTRFS_BLOCK_GROUP_DUP; |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3111 | else if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3112 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3113 | if (num_devices > 2) |
| 3114 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 3115 | if (num_devices > 3) |
| 3116 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 3117 | BTRFS_BLOCK_GROUP_RAID6); |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3118 | if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3119 | (!alloc_profile_is_valid(bctl->data.target, 1) || |
| 3120 | (bctl->data.target & ~allowed))) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3121 | printk(KERN_ERR "btrfs: unable to start balance with target " |
| 3122 | "data profile %llu\n", |
| 3123 | (unsigned long long)bctl->data.target); |
| 3124 | ret = -EINVAL; |
| 3125 | goto out; |
| 3126 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3127 | if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3128 | (!alloc_profile_is_valid(bctl->meta.target, 1) || |
| 3129 | (bctl->meta.target & ~allowed))) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3130 | printk(KERN_ERR "btrfs: unable to start balance with target " |
| 3131 | "metadata profile %llu\n", |
| 3132 | (unsigned long long)bctl->meta.target); |
| 3133 | ret = -EINVAL; |
| 3134 | goto out; |
| 3135 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3136 | if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3137 | (!alloc_profile_is_valid(bctl->sys.target, 1) || |
| 3138 | (bctl->sys.target & ~allowed))) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3139 | printk(KERN_ERR "btrfs: unable to start balance with target " |
| 3140 | "system profile %llu\n", |
| 3141 | (unsigned long long)bctl->sys.target); |
| 3142 | ret = -EINVAL; |
| 3143 | goto out; |
| 3144 | } |
| 3145 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3146 | /* allow dup'ed data chunks only in mixed mode */ |
| 3147 | if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3148 | (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3149 | printk(KERN_ERR "btrfs: dup for data is not allowed\n"); |
| 3150 | ret = -EINVAL; |
| 3151 | goto out; |
| 3152 | } |
| 3153 | |
| 3154 | /* allow to reduce meta or sys integrity only if force set */ |
| 3155 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3156 | BTRFS_BLOCK_GROUP_RAID10 | |
| 3157 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3158 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3159 | do { |
| 3160 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3161 | |
| 3162 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3163 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3164 | !(bctl->sys.target & allowed)) || |
| 3165 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3166 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3167 | !(bctl->meta.target & allowed))) { |
| 3168 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
| 3169 | printk(KERN_INFO "btrfs: force reducing metadata " |
| 3170 | "integrity\n"); |
| 3171 | } else { |
| 3172 | printk(KERN_ERR "btrfs: balance will reduce metadata " |
| 3173 | "integrity, use force if you want this\n"); |
| 3174 | ret = -EINVAL; |
| 3175 | goto out; |
| 3176 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3177 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3178 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3179 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3180 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3181 | int num_tolerated_disk_barrier_failures; |
| 3182 | u64 target = bctl->sys.target; |
| 3183 | |
| 3184 | num_tolerated_disk_barrier_failures = |
| 3185 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3186 | if (num_tolerated_disk_barrier_failures > 0 && |
| 3187 | (target & |
| 3188 | (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 | |
| 3189 | BTRFS_AVAIL_ALLOC_BIT_SINGLE))) |
| 3190 | num_tolerated_disk_barrier_failures = 0; |
| 3191 | else if (num_tolerated_disk_barrier_failures > 1 && |
| 3192 | (target & |
| 3193 | (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))) |
| 3194 | num_tolerated_disk_barrier_failures = 1; |
| 3195 | |
| 3196 | fs_info->num_tolerated_disk_barrier_failures = |
| 3197 | num_tolerated_disk_barrier_failures; |
| 3198 | } |
| 3199 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3200 | ret = insert_balance_item(fs_info->tree_root, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3201 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3202 | goto out; |
| 3203 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3204 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3205 | BUG_ON(ret == -EEXIST); |
| 3206 | set_balance_control(bctl); |
| 3207 | } else { |
| 3208 | BUG_ON(ret != -EEXIST); |
| 3209 | spin_lock(&fs_info->balance_lock); |
| 3210 | update_balance_args(bctl); |
| 3211 | spin_unlock(&fs_info->balance_lock); |
| 3212 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3213 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3214 | atomic_inc(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3215 | mutex_unlock(&fs_info->balance_mutex); |
| 3216 | |
| 3217 | ret = __btrfs_balance(fs_info); |
| 3218 | |
| 3219 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3220 | atomic_dec(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3221 | |
Ilya Dryomov | bf023ec | 2013-02-12 16:27:46 +0000 | [diff] [blame] | 3222 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3223 | fs_info->num_tolerated_disk_barrier_failures = |
| 3224 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3225 | } |
| 3226 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3227 | if (bargs) { |
| 3228 | memset(bargs, 0, sizeof(*bargs)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3229 | update_ioctl_balance_args(fs_info, 0, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3230 | } |
| 3231 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3232 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3233 | balance_need_close(fs_info)) { |
| 3234 | __cancel_balance(fs_info); |
| 3235 | } |
| 3236 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3237 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3238 | |
| 3239 | return ret; |
| 3240 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3241 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
| 3242 | __cancel_balance(fs_info); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3243 | else { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3244 | kfree(bctl); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3245 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 3246 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3247 | return ret; |
| 3248 | } |
| 3249 | |
| 3250 | static int balance_kthread(void *data) |
| 3251 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3252 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3253 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3254 | |
| 3255 | mutex_lock(&fs_info->volume_mutex); |
| 3256 | mutex_lock(&fs_info->balance_mutex); |
| 3257 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3258 | if (fs_info->balance_ctl) { |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3259 | printk(KERN_INFO "btrfs: continuing balance\n"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3260 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3261 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3262 | |
| 3263 | mutex_unlock(&fs_info->balance_mutex); |
| 3264 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3265 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3266 | return ret; |
| 3267 | } |
| 3268 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3269 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 3270 | { |
| 3271 | struct task_struct *tsk; |
| 3272 | |
| 3273 | spin_lock(&fs_info->balance_lock); |
| 3274 | if (!fs_info->balance_ctl) { |
| 3275 | spin_unlock(&fs_info->balance_lock); |
| 3276 | return 0; |
| 3277 | } |
| 3278 | spin_unlock(&fs_info->balance_lock); |
| 3279 | |
| 3280 | if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) { |
| 3281 | printk(KERN_INFO "btrfs: force skipping balance\n"); |
| 3282 | return 0; |
| 3283 | } |
| 3284 | |
| 3285 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
| 3286 | if (IS_ERR(tsk)) |
| 3287 | return PTR_ERR(tsk); |
| 3288 | |
| 3289 | return 0; |
| 3290 | } |
| 3291 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3292 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3293 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3294 | struct btrfs_balance_control *bctl; |
| 3295 | struct btrfs_balance_item *item; |
| 3296 | struct btrfs_disk_balance_args disk_bargs; |
| 3297 | struct btrfs_path *path; |
| 3298 | struct extent_buffer *leaf; |
| 3299 | struct btrfs_key key; |
| 3300 | int ret; |
| 3301 | |
| 3302 | path = btrfs_alloc_path(); |
| 3303 | if (!path) |
| 3304 | return -ENOMEM; |
| 3305 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3306 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 3307 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 3308 | key.offset = 0; |
| 3309 | |
| 3310 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3311 | if (ret < 0) |
| 3312 | goto out; |
| 3313 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3314 | ret = 0; |
| 3315 | goto out; |
| 3316 | } |
| 3317 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3318 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3319 | if (!bctl) { |
| 3320 | ret = -ENOMEM; |
| 3321 | goto out; |
| 3322 | } |
| 3323 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3324 | leaf = path->nodes[0]; |
| 3325 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3326 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3327 | bctl->fs_info = fs_info; |
| 3328 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3329 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3330 | |
| 3331 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3332 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3333 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3334 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3335 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3336 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3337 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3338 | WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); |
| 3339 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3340 | mutex_lock(&fs_info->volume_mutex); |
| 3341 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3342 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3343 | set_balance_control(bctl); |
| 3344 | |
| 3345 | mutex_unlock(&fs_info->balance_mutex); |
| 3346 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3347 | out: |
| 3348 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3349 | return ret; |
| 3350 | } |
| 3351 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3352 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3353 | { |
| 3354 | int ret = 0; |
| 3355 | |
| 3356 | mutex_lock(&fs_info->balance_mutex); |
| 3357 | if (!fs_info->balance_ctl) { |
| 3358 | mutex_unlock(&fs_info->balance_mutex); |
| 3359 | return -ENOTCONN; |
| 3360 | } |
| 3361 | |
| 3362 | if (atomic_read(&fs_info->balance_running)) { |
| 3363 | atomic_inc(&fs_info->balance_pause_req); |
| 3364 | mutex_unlock(&fs_info->balance_mutex); |
| 3365 | |
| 3366 | wait_event(fs_info->balance_wait_q, |
| 3367 | atomic_read(&fs_info->balance_running) == 0); |
| 3368 | |
| 3369 | mutex_lock(&fs_info->balance_mutex); |
| 3370 | /* we are good with balance_ctl ripped off from under us */ |
| 3371 | BUG_ON(atomic_read(&fs_info->balance_running)); |
| 3372 | atomic_dec(&fs_info->balance_pause_req); |
| 3373 | } else { |
| 3374 | ret = -ENOTCONN; |
| 3375 | } |
| 3376 | |
| 3377 | mutex_unlock(&fs_info->balance_mutex); |
| 3378 | return ret; |
| 3379 | } |
| 3380 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3381 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 3382 | { |
| 3383 | mutex_lock(&fs_info->balance_mutex); |
| 3384 | if (!fs_info->balance_ctl) { |
| 3385 | mutex_unlock(&fs_info->balance_mutex); |
| 3386 | return -ENOTCONN; |
| 3387 | } |
| 3388 | |
| 3389 | atomic_inc(&fs_info->balance_cancel_req); |
| 3390 | /* |
| 3391 | * if we are running just wait and return, balance item is |
| 3392 | * deleted in btrfs_balance in this case |
| 3393 | */ |
| 3394 | if (atomic_read(&fs_info->balance_running)) { |
| 3395 | mutex_unlock(&fs_info->balance_mutex); |
| 3396 | wait_event(fs_info->balance_wait_q, |
| 3397 | atomic_read(&fs_info->balance_running) == 0); |
| 3398 | mutex_lock(&fs_info->balance_mutex); |
| 3399 | } else { |
| 3400 | /* __cancel_balance needs volume_mutex */ |
| 3401 | mutex_unlock(&fs_info->balance_mutex); |
| 3402 | mutex_lock(&fs_info->volume_mutex); |
| 3403 | mutex_lock(&fs_info->balance_mutex); |
| 3404 | |
| 3405 | if (fs_info->balance_ctl) |
| 3406 | __cancel_balance(fs_info); |
| 3407 | |
| 3408 | mutex_unlock(&fs_info->volume_mutex); |
| 3409 | } |
| 3410 | |
| 3411 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); |
| 3412 | atomic_dec(&fs_info->balance_cancel_req); |
| 3413 | mutex_unlock(&fs_info->balance_mutex); |
| 3414 | return 0; |
| 3415 | } |
| 3416 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3417 | /* |
| 3418 | * shrinking a device means finding all of the device extents past |
| 3419 | * the new size, and then following the back refs to the chunks. |
| 3420 | * The chunk relocation code actually frees the device extent |
| 3421 | */ |
| 3422 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 3423 | { |
| 3424 | struct btrfs_trans_handle *trans; |
| 3425 | struct btrfs_root *root = device->dev_root; |
| 3426 | struct btrfs_dev_extent *dev_extent = NULL; |
| 3427 | struct btrfs_path *path; |
| 3428 | u64 length; |
| 3429 | u64 chunk_tree; |
| 3430 | u64 chunk_objectid; |
| 3431 | u64 chunk_offset; |
| 3432 | int ret; |
| 3433 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3434 | int failed = 0; |
| 3435 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3436 | struct extent_buffer *l; |
| 3437 | struct btrfs_key key; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3438 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3439 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3440 | u64 old_size = device->total_bytes; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3441 | u64 diff = device->total_bytes - new_size; |
| 3442 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3443 | if (device->is_tgtdev_for_dev_replace) |
| 3444 | return -EINVAL; |
| 3445 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3446 | path = btrfs_alloc_path(); |
| 3447 | if (!path) |
| 3448 | return -ENOMEM; |
| 3449 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3450 | path->reada = 2; |
| 3451 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3452 | lock_chunks(root); |
| 3453 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3454 | device->total_bytes = new_size; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3455 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3456 | device->fs_devices->total_rw_bytes -= diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3457 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3458 | root->fs_info->free_chunk_space -= diff; |
| 3459 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 3460 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3461 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3462 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3463 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3464 | key.objectid = device->devid; |
| 3465 | key.offset = (u64)-1; |
| 3466 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 3467 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 3468 | do { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3469 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3470 | if (ret < 0) |
| 3471 | goto done; |
| 3472 | |
| 3473 | ret = btrfs_previous_item(root, path, 0, key.type); |
| 3474 | if (ret < 0) |
| 3475 | goto done; |
| 3476 | if (ret) { |
| 3477 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3478 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 3479 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3480 | } |
| 3481 | |
| 3482 | l = path->nodes[0]; |
| 3483 | slot = path->slots[0]; |
| 3484 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 3485 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3486 | if (key.objectid != device->devid) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3487 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 3488 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3489 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3490 | |
| 3491 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 3492 | length = btrfs_dev_extent_length(l, dev_extent); |
| 3493 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3494 | if (key.offset + length <= new_size) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3495 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3496 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3497 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3498 | |
| 3499 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); |
| 3500 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); |
| 3501 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3502 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3503 | |
| 3504 | ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid, |
| 3505 | chunk_offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3506 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3507 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3508 | if (ret == -ENOSPC) |
| 3509 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 3510 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3511 | |
| 3512 | if (failed && !retried) { |
| 3513 | failed = 0; |
| 3514 | retried = true; |
| 3515 | goto again; |
| 3516 | } else if (failed && retried) { |
| 3517 | ret = -ENOSPC; |
| 3518 | lock_chunks(root); |
| 3519 | |
| 3520 | device->total_bytes = old_size; |
| 3521 | if (device->writeable) |
| 3522 | device->fs_devices->total_rw_bytes += diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3523 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3524 | root->fs_info->free_chunk_space += diff; |
| 3525 | spin_unlock(&root->fs_info->free_chunk_lock); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3526 | unlock_chunks(root); |
| 3527 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3528 | } |
| 3529 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3530 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3531 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3532 | if (IS_ERR(trans)) { |
| 3533 | ret = PTR_ERR(trans); |
| 3534 | goto done; |
| 3535 | } |
| 3536 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3537 | lock_chunks(root); |
| 3538 | |
| 3539 | device->disk_total_bytes = new_size; |
| 3540 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 3541 | ret = btrfs_update_device(trans, device); |
| 3542 | if (ret) { |
| 3543 | unlock_chunks(root); |
| 3544 | btrfs_end_transaction(trans, root); |
| 3545 | goto done; |
| 3546 | } |
| 3547 | WARN_ON(diff > old_total); |
| 3548 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
| 3549 | unlock_chunks(root); |
| 3550 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3551 | done: |
| 3552 | btrfs_free_path(path); |
| 3553 | return ret; |
| 3554 | } |
| 3555 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 3556 | static int btrfs_add_system_chunk(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3557 | struct btrfs_key *key, |
| 3558 | struct btrfs_chunk *chunk, int item_size) |
| 3559 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3560 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3561 | struct btrfs_disk_key disk_key; |
| 3562 | u32 array_size; |
| 3563 | u8 *ptr; |
| 3564 | |
| 3565 | array_size = btrfs_super_sys_array_size(super_copy); |
| 3566 | if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) |
| 3567 | return -EFBIG; |
| 3568 | |
| 3569 | ptr = super_copy->sys_chunk_array + array_size; |
| 3570 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 3571 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 3572 | ptr += sizeof(disk_key); |
| 3573 | memcpy(ptr, chunk, item_size); |
| 3574 | item_size += sizeof(disk_key); |
| 3575 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
| 3576 | return 0; |
| 3577 | } |
| 3578 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3579 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3580 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3581 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3582 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3583 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3584 | const struct btrfs_device_info *di_a = a; |
| 3585 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3586 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3587 | if (di_a->max_avail > di_b->max_avail) |
| 3588 | return -1; |
| 3589 | if (di_a->max_avail < di_b->max_avail) |
| 3590 | return 1; |
| 3591 | if (di_a->total_avail > di_b->total_avail) |
| 3592 | return -1; |
| 3593 | if (di_a->total_avail < di_b->total_avail) |
| 3594 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3595 | return 0; |
| 3596 | } |
| 3597 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3598 | static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 3599 | [BTRFS_RAID_RAID10] = { |
| 3600 | .sub_stripes = 2, |
| 3601 | .dev_stripes = 1, |
| 3602 | .devs_max = 0, /* 0 == as many as possible */ |
| 3603 | .devs_min = 4, |
| 3604 | .devs_increment = 2, |
| 3605 | .ncopies = 2, |
| 3606 | }, |
| 3607 | [BTRFS_RAID_RAID1] = { |
| 3608 | .sub_stripes = 1, |
| 3609 | .dev_stripes = 1, |
| 3610 | .devs_max = 2, |
| 3611 | .devs_min = 2, |
| 3612 | .devs_increment = 2, |
| 3613 | .ncopies = 2, |
| 3614 | }, |
| 3615 | [BTRFS_RAID_DUP] = { |
| 3616 | .sub_stripes = 1, |
| 3617 | .dev_stripes = 2, |
| 3618 | .devs_max = 1, |
| 3619 | .devs_min = 1, |
| 3620 | .devs_increment = 1, |
| 3621 | .ncopies = 2, |
| 3622 | }, |
| 3623 | [BTRFS_RAID_RAID0] = { |
| 3624 | .sub_stripes = 1, |
| 3625 | .dev_stripes = 1, |
| 3626 | .devs_max = 0, |
| 3627 | .devs_min = 2, |
| 3628 | .devs_increment = 1, |
| 3629 | .ncopies = 1, |
| 3630 | }, |
| 3631 | [BTRFS_RAID_SINGLE] = { |
| 3632 | .sub_stripes = 1, |
| 3633 | .dev_stripes = 1, |
| 3634 | .devs_max = 1, |
| 3635 | .devs_min = 1, |
| 3636 | .devs_increment = 1, |
| 3637 | .ncopies = 1, |
| 3638 | }, |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 3639 | [BTRFS_RAID_RAID5] = { |
| 3640 | .sub_stripes = 1, |
| 3641 | .dev_stripes = 1, |
| 3642 | .devs_max = 0, |
| 3643 | .devs_min = 2, |
| 3644 | .devs_increment = 1, |
| 3645 | .ncopies = 2, |
| 3646 | }, |
| 3647 | [BTRFS_RAID_RAID6] = { |
| 3648 | .sub_stripes = 1, |
| 3649 | .dev_stripes = 1, |
| 3650 | .devs_max = 0, |
| 3651 | .devs_min = 3, |
| 3652 | .devs_increment = 1, |
| 3653 | .ncopies = 3, |
| 3654 | }, |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 3655 | }; |
| 3656 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3657 | static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target) |
| 3658 | { |
| 3659 | /* TODO allow them to set a preferred stripe size */ |
| 3660 | return 64 * 1024; |
| 3661 | } |
| 3662 | |
| 3663 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 3664 | { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3665 | if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))) |
| 3666 | return; |
| 3667 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 3668 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3669 | } |
| 3670 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3671 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 3672 | struct btrfs_root *extent_root, |
| 3673 | struct map_lookup **map_ret, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3674 | u64 *num_bytes_out, u64 *stripe_size_out, |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3675 | u64 start, u64 type) |
| 3676 | { |
| 3677 | struct btrfs_fs_info *info = extent_root->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3678 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
| 3679 | struct list_head *cur; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3680 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3681 | struct extent_map_tree *em_tree; |
| 3682 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3683 | struct btrfs_device_info *devices_info = NULL; |
| 3684 | u64 total_avail; |
| 3685 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3686 | int data_stripes; /* number of stripes that count for |
| 3687 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3688 | int sub_stripes; /* sub_stripes info for map */ |
| 3689 | int dev_stripes; /* stripes per dev */ |
| 3690 | int devs_max; /* max devs to use */ |
| 3691 | int devs_min; /* min devs needed */ |
| 3692 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 3693 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3694 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3695 | u64 max_stripe_size; |
| 3696 | u64 max_chunk_size; |
| 3697 | u64 stripe_size; |
| 3698 | u64 num_bytes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3699 | u64 raid_stripe_len = BTRFS_STRIPE_LEN; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3700 | int ndevs; |
| 3701 | int i; |
| 3702 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 3703 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3704 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3705 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3706 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3707 | if (list_empty(&fs_devices->alloc_list)) |
| 3708 | return -ENOSPC; |
| 3709 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 3710 | index = __get_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3711 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 3712 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 3713 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 3714 | devs_max = btrfs_raid_array[index].devs_max; |
| 3715 | devs_min = btrfs_raid_array[index].devs_min; |
| 3716 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 3717 | ncopies = btrfs_raid_array[index].ncopies; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3718 | |
| 3719 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 3720 | max_stripe_size = 1024 * 1024 * 1024; |
| 3721 | max_chunk_size = 10 * max_stripe_size; |
| 3722 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 3723 | /* for larger filesystems, use larger metadata chunks */ |
| 3724 | if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024) |
| 3725 | max_stripe_size = 1024 * 1024 * 1024; |
| 3726 | else |
| 3727 | max_stripe_size = 256 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3728 | max_chunk_size = max_stripe_size; |
| 3729 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Chris Mason | 96bdc7d | 2012-01-16 08:13:11 -0500 | [diff] [blame] | 3730 | max_stripe_size = 32 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3731 | max_chunk_size = 2 * max_stripe_size; |
| 3732 | } else { |
| 3733 | printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n", |
| 3734 | type); |
| 3735 | BUG_ON(1); |
| 3736 | } |
| 3737 | |
| 3738 | /* we don't want a chunk larger than 10% of writeable space */ |
| 3739 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 3740 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3741 | |
| 3742 | devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices, |
| 3743 | GFP_NOFS); |
| 3744 | if (!devices_info) |
| 3745 | return -ENOMEM; |
| 3746 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3747 | cur = fs_devices->alloc_list.next; |
| 3748 | |
| 3749 | /* |
| 3750 | * in the first pass through the devices list, we gather information |
| 3751 | * about the available holes on each device. |
| 3752 | */ |
| 3753 | ndevs = 0; |
| 3754 | while (cur != &fs_devices->alloc_list) { |
| 3755 | struct btrfs_device *device; |
| 3756 | u64 max_avail; |
| 3757 | u64 dev_offset; |
| 3758 | |
| 3759 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
| 3760 | |
| 3761 | cur = cur->next; |
| 3762 | |
| 3763 | if (!device->writeable) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 3764 | WARN(1, KERN_ERR |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3765 | "btrfs: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3766 | continue; |
| 3767 | } |
| 3768 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3769 | if (!device->in_fs_metadata || |
| 3770 | device->is_tgtdev_for_dev_replace) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3771 | continue; |
| 3772 | |
| 3773 | if (device->total_bytes > device->bytes_used) |
| 3774 | total_avail = device->total_bytes - device->bytes_used; |
| 3775 | else |
| 3776 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 3777 | |
| 3778 | /* If there is no space on this device, skip it. */ |
| 3779 | if (total_avail == 0) |
| 3780 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3781 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 3782 | ret = find_free_dev_extent(device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3783 | max_stripe_size * dev_stripes, |
| 3784 | &dev_offset, &max_avail); |
| 3785 | if (ret && ret != -ENOSPC) |
| 3786 | goto error; |
| 3787 | |
| 3788 | if (ret == 0) |
| 3789 | max_avail = max_stripe_size * dev_stripes; |
| 3790 | |
| 3791 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
| 3792 | continue; |
| 3793 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 3794 | if (ndevs == fs_devices->rw_devices) { |
| 3795 | WARN(1, "%s: found more than %llu devices\n", |
| 3796 | __func__, fs_devices->rw_devices); |
| 3797 | break; |
| 3798 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3799 | devices_info[ndevs].dev_offset = dev_offset; |
| 3800 | devices_info[ndevs].max_avail = max_avail; |
| 3801 | devices_info[ndevs].total_avail = total_avail; |
| 3802 | devices_info[ndevs].dev = device; |
| 3803 | ++ndevs; |
| 3804 | } |
| 3805 | |
| 3806 | /* |
| 3807 | * now sort the devices by hole size / available space |
| 3808 | */ |
| 3809 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 3810 | btrfs_cmp_device_info, NULL); |
| 3811 | |
| 3812 | /* round down to number of usable stripes */ |
| 3813 | ndevs -= ndevs % devs_increment; |
| 3814 | |
| 3815 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
| 3816 | ret = -ENOSPC; |
| 3817 | goto error; |
| 3818 | } |
| 3819 | |
| 3820 | if (devs_max && ndevs > devs_max) |
| 3821 | ndevs = devs_max; |
| 3822 | /* |
| 3823 | * the primary goal is to maximize the number of stripes, so use as many |
| 3824 | * devices as possible, even if the stripes are not maximum sized. |
| 3825 | */ |
| 3826 | stripe_size = devices_info[ndevs-1].max_avail; |
| 3827 | num_stripes = ndevs * dev_stripes; |
| 3828 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3829 | /* |
| 3830 | * this will have to be fixed for RAID1 and RAID10 over |
| 3831 | * more drives |
| 3832 | */ |
| 3833 | data_stripes = num_stripes / ncopies; |
| 3834 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3835 | if (type & BTRFS_BLOCK_GROUP_RAID5) { |
| 3836 | raid_stripe_len = find_raid56_stripe_len(ndevs - 1, |
| 3837 | btrfs_super_stripesize(info->super_copy)); |
| 3838 | data_stripes = num_stripes - 1; |
| 3839 | } |
| 3840 | if (type & BTRFS_BLOCK_GROUP_RAID6) { |
| 3841 | raid_stripe_len = find_raid56_stripe_len(ndevs - 2, |
| 3842 | btrfs_super_stripesize(info->super_copy)); |
| 3843 | data_stripes = num_stripes - 2; |
| 3844 | } |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 3845 | |
| 3846 | /* |
| 3847 | * Use the number of data stripes to figure out how big this chunk |
| 3848 | * is really going to be in terms of logical address space, |
| 3849 | * and compare that answer with the max chunk size |
| 3850 | */ |
| 3851 | if (stripe_size * data_stripes > max_chunk_size) { |
| 3852 | u64 mask = (1ULL << 24) - 1; |
| 3853 | stripe_size = max_chunk_size; |
| 3854 | do_div(stripe_size, data_stripes); |
| 3855 | |
| 3856 | /* bump the answer up to a 16MB boundary */ |
| 3857 | stripe_size = (stripe_size + mask) & ~mask; |
| 3858 | |
| 3859 | /* but don't go higher than the limits we found |
| 3860 | * while searching for free extents |
| 3861 | */ |
| 3862 | if (stripe_size > devices_info[ndevs-1].max_avail) |
| 3863 | stripe_size = devices_info[ndevs-1].max_avail; |
| 3864 | } |
| 3865 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3866 | do_div(stripe_size, dev_stripes); |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 3867 | |
| 3868 | /* align to BTRFS_STRIPE_LEN */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3869 | do_div(stripe_size, raid_stripe_len); |
| 3870 | stripe_size *= raid_stripe_len; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3871 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3872 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 3873 | if (!map) { |
| 3874 | ret = -ENOMEM; |
| 3875 | goto error; |
| 3876 | } |
| 3877 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 3878 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3879 | for (i = 0; i < ndevs; ++i) { |
| 3880 | for (j = 0; j < dev_stripes; ++j) { |
| 3881 | int s = i * dev_stripes + j; |
| 3882 | map->stripes[s].dev = devices_info[i].dev; |
| 3883 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 3884 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 3885 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3886 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3887 | map->sector_size = extent_root->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3888 | map->stripe_len = raid_stripe_len; |
| 3889 | map->io_align = raid_stripe_len; |
| 3890 | map->io_width = raid_stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3891 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 3892 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3893 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3894 | *map_ret = map; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3895 | num_bytes = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3896 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3897 | *stripe_size_out = stripe_size; |
| 3898 | *num_bytes_out = num_bytes; |
| 3899 | |
| 3900 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3901 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 3902 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3903 | if (!em) { |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3904 | ret = -ENOMEM; |
| 3905 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3906 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3907 | em->bdev = (struct block_device *)map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3908 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3909 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3910 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3911 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3912 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3913 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3914 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 3915 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3916 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 3917 | if (ret) { |
| 3918 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 3919 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 3920 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3921 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3922 | for (i = 0; i < map->num_stripes; ++i) { |
| 3923 | struct btrfs_device *device; |
| 3924 | u64 dev_offset; |
| 3925 | |
| 3926 | device = map->stripes[i].dev; |
| 3927 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3928 | |
| 3929 | ret = btrfs_alloc_dev_extent(trans, device, |
| 3930 | info->chunk_root->root_key.objectid, |
| 3931 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3932 | start, dev_offset, stripe_size); |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 3933 | if (ret) |
| 3934 | goto error_dev_extent; |
| 3935 | } |
| 3936 | |
| 3937 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 3938 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 3939 | start, num_bytes); |
| 3940 | if (ret) { |
| 3941 | i = map->num_stripes - 1; |
| 3942 | goto error_dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3943 | } |
| 3944 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 3945 | free_extent_map(em); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3946 | check_raid56_incompat_flag(extent_root->fs_info, type); |
| 3947 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3948 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3949 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3950 | |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 3951 | error_dev_extent: |
| 3952 | for (; i >= 0; i--) { |
| 3953 | struct btrfs_device *device; |
| 3954 | int err; |
| 3955 | |
| 3956 | device = map->stripes[i].dev; |
| 3957 | err = btrfs_free_dev_extent(trans, device, start); |
| 3958 | if (err) { |
| 3959 | btrfs_abort_transaction(trans, extent_root, err); |
| 3960 | break; |
| 3961 | } |
| 3962 | } |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 3963 | write_lock(&em_tree->lock); |
| 3964 | remove_extent_mapping(em_tree, em); |
| 3965 | write_unlock(&em_tree->lock); |
| 3966 | |
| 3967 | /* One for our allocation */ |
| 3968 | free_extent_map(em); |
| 3969 | /* One for the tree reference */ |
| 3970 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3971 | error: |
| 3972 | kfree(map); |
| 3973 | kfree(devices_info); |
| 3974 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | static int __finish_chunk_alloc(struct btrfs_trans_handle *trans, |
| 3978 | struct btrfs_root *extent_root, |
| 3979 | struct map_lookup *map, u64 chunk_offset, |
| 3980 | u64 chunk_size, u64 stripe_size) |
| 3981 | { |
| 3982 | u64 dev_offset; |
| 3983 | struct btrfs_key key; |
| 3984 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 3985 | struct btrfs_device *device; |
| 3986 | struct btrfs_chunk *chunk; |
| 3987 | struct btrfs_stripe *stripe; |
| 3988 | size_t item_size = btrfs_chunk_item_size(map->num_stripes); |
| 3989 | int index = 0; |
| 3990 | int ret; |
| 3991 | |
| 3992 | chunk = kzalloc(item_size, GFP_NOFS); |
| 3993 | if (!chunk) |
| 3994 | return -ENOMEM; |
| 3995 | |
| 3996 | index = 0; |
| 3997 | while (index < map->num_stripes) { |
| 3998 | device = map->stripes[index].dev; |
| 3999 | device->bytes_used += stripe_size; |
| 4000 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 4001 | if (ret) |
| 4002 | goto out_free; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4003 | index++; |
| 4004 | } |
| 4005 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4006 | spin_lock(&extent_root->fs_info->free_chunk_lock); |
| 4007 | extent_root->fs_info->free_chunk_space -= (stripe_size * |
| 4008 | map->num_stripes); |
| 4009 | spin_unlock(&extent_root->fs_info->free_chunk_lock); |
| 4010 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4011 | index = 0; |
| 4012 | stripe = &chunk->stripe; |
| 4013 | while (index < map->num_stripes) { |
| 4014 | device = map->stripes[index].dev; |
| 4015 | dev_offset = map->stripes[index].physical; |
| 4016 | |
| 4017 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 4018 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 4019 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 4020 | stripe++; |
| 4021 | index++; |
| 4022 | } |
| 4023 | |
| 4024 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 4025 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 4026 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 4027 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 4028 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 4029 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 4030 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 4031 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 4032 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 4033 | |
| 4034 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4035 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4036 | key.offset = chunk_offset; |
| 4037 | |
| 4038 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4039 | |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4040 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4041 | /* |
| 4042 | * TODO: Cleanup of inserted chunk root in case of |
| 4043 | * failure. |
| 4044 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4045 | ret = btrfs_add_system_chunk(chunk_root, &key, chunk, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4046 | item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4047 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4048 | |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 4049 | out_free: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4050 | kfree(chunk); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4051 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | /* |
| 4055 | * Chunk allocation falls into two parts. The first part does works |
| 4056 | * that make the new allocated chunk useable, but not do any operation |
| 4057 | * that modifies the chunk tree. The second part does the works that |
| 4058 | * require modifying the chunk tree. This division is important for the |
| 4059 | * bootstrap process of adding storage to a seed btrfs. |
| 4060 | */ |
| 4061 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 4062 | struct btrfs_root *extent_root, u64 type) |
| 4063 | { |
| 4064 | u64 chunk_offset; |
| 4065 | u64 chunk_size; |
| 4066 | u64 stripe_size; |
| 4067 | struct map_lookup *map; |
| 4068 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 4069 | int ret; |
| 4070 | |
| 4071 | ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4072 | &chunk_offset); |
| 4073 | if (ret) |
| 4074 | return ret; |
| 4075 | |
| 4076 | ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size, |
| 4077 | &stripe_size, chunk_offset, type); |
| 4078 | if (ret) |
| 4079 | return ret; |
| 4080 | |
| 4081 | ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, |
| 4082 | chunk_size, stripe_size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4083 | if (ret) |
| 4084 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4085 | return 0; |
| 4086 | } |
| 4087 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4088 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4089 | struct btrfs_root *root, |
| 4090 | struct btrfs_device *device) |
| 4091 | { |
| 4092 | u64 chunk_offset; |
| 4093 | u64 sys_chunk_offset; |
| 4094 | u64 chunk_size; |
| 4095 | u64 sys_chunk_size; |
| 4096 | u64 stripe_size; |
| 4097 | u64 sys_stripe_size; |
| 4098 | u64 alloc_profile; |
| 4099 | struct map_lookup *map; |
| 4100 | struct map_lookup *sys_map; |
| 4101 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4102 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4103 | int ret; |
| 4104 | |
| 4105 | ret = find_next_chunk(fs_info->chunk_root, |
| 4106 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset); |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 4107 | if (ret) |
| 4108 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4109 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4110 | alloc_profile = btrfs_get_alloc_profile(extent_root, 0); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4111 | ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size, |
| 4112 | &stripe_size, chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4113 | if (ret) |
| 4114 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4115 | |
| 4116 | sys_chunk_offset = chunk_offset + chunk_size; |
| 4117 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4118 | alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4119 | ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map, |
| 4120 | &sys_chunk_size, &sys_stripe_size, |
| 4121 | sys_chunk_offset, alloc_profile); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4122 | if (ret) { |
| 4123 | btrfs_abort_transaction(trans, root, ret); |
| 4124 | goto out; |
| 4125 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4126 | |
| 4127 | ret = btrfs_add_device(trans, fs_info->chunk_root, device); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4128 | if (ret) { |
| 4129 | btrfs_abort_transaction(trans, root, ret); |
| 4130 | goto out; |
| 4131 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4132 | |
| 4133 | /* |
| 4134 | * Modifying chunk tree needs allocating new blocks from both |
| 4135 | * system block group and metadata block group. So we only can |
| 4136 | * do operations require modifying the chunk tree after both |
| 4137 | * block groups were created. |
| 4138 | */ |
| 4139 | ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, |
| 4140 | chunk_size, stripe_size); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4141 | if (ret) { |
| 4142 | btrfs_abort_transaction(trans, root, ret); |
| 4143 | goto out; |
| 4144 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4145 | |
| 4146 | ret = __finish_chunk_alloc(trans, extent_root, sys_map, |
| 4147 | sys_chunk_offset, sys_chunk_size, |
| 4148 | sys_stripe_size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4149 | if (ret) |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4150 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4151 | |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4152 | out: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4153 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4154 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4155 | } |
| 4156 | |
| 4157 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 4158 | { |
| 4159 | struct extent_map *em; |
| 4160 | struct map_lookup *map; |
| 4161 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 4162 | int readonly = 0; |
| 4163 | int i; |
| 4164 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4165 | read_lock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4166 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4167 | read_unlock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4168 | if (!em) |
| 4169 | return 1; |
| 4170 | |
Josef Bacik | f48b907 | 2010-01-27 02:07:59 +0000 | [diff] [blame] | 4171 | if (btrfs_test_opt(root, DEGRADED)) { |
| 4172 | free_extent_map(em); |
| 4173 | return 0; |
| 4174 | } |
| 4175 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4176 | map = (struct map_lookup *)em->bdev; |
| 4177 | for (i = 0; i < map->num_stripes; i++) { |
| 4178 | if (!map->stripes[i].dev->writeable) { |
| 4179 | readonly = 1; |
| 4180 | break; |
| 4181 | } |
| 4182 | } |
| 4183 | free_extent_map(em); |
| 4184 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 4188 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 4189 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4190 | } |
| 4191 | |
| 4192 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 4193 | { |
| 4194 | struct extent_map *em; |
| 4195 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4196 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4197 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4198 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 4199 | if (em) |
| 4200 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4201 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4202 | if (!em) |
| 4203 | break; |
| 4204 | kfree(em->bdev); |
| 4205 | /* once for us */ |
| 4206 | free_extent_map(em); |
| 4207 | /* once for the tree */ |
| 4208 | free_extent_map(em); |
| 4209 | } |
| 4210 | } |
| 4211 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4212 | 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] | 4213 | { |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4214 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4215 | struct extent_map *em; |
| 4216 | struct map_lookup *map; |
| 4217 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4218 | int ret; |
| 4219 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4220 | read_lock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4221 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4222 | read_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4223 | |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4224 | /* |
| 4225 | * We could return errors for these cases, but that could get ugly and |
| 4226 | * we'd probably do the same thing which is just not do anything else |
| 4227 | * and exit, so return 1 so the callers don't try to use other copies. |
| 4228 | */ |
| 4229 | if (!em) { |
| 4230 | btrfs_emerg(fs_info, "No mapping for %Lu-%Lu\n", logical, |
| 4231 | logical+len); |
| 4232 | return 1; |
| 4233 | } |
| 4234 | |
| 4235 | if (em->start > logical || em->start + em->len < logical) { |
| 4236 | btrfs_emerg(fs_info, "Invalid mapping for %Lu-%Lu, got " |
| 4237 | "%Lu-%Lu\n", logical, logical+len, em->start, |
| 4238 | em->start + em->len); |
| 4239 | return 1; |
| 4240 | } |
| 4241 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4242 | map = (struct map_lookup *)em->bdev; |
| 4243 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 4244 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4245 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4246 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4247 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 4248 | ret = 2; |
| 4249 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 4250 | ret = 3; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4251 | else |
| 4252 | ret = 1; |
| 4253 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4254 | |
| 4255 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 4256 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) |
| 4257 | ret++; |
| 4258 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
| 4259 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4260 | return ret; |
| 4261 | } |
| 4262 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4263 | unsigned long btrfs_full_stripe_len(struct btrfs_root *root, |
| 4264 | struct btrfs_mapping_tree *map_tree, |
| 4265 | u64 logical) |
| 4266 | { |
| 4267 | struct extent_map *em; |
| 4268 | struct map_lookup *map; |
| 4269 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4270 | unsigned long len = root->sectorsize; |
| 4271 | |
| 4272 | read_lock(&em_tree->lock); |
| 4273 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4274 | read_unlock(&em_tree->lock); |
| 4275 | BUG_ON(!em); |
| 4276 | |
| 4277 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4278 | map = (struct map_lookup *)em->bdev; |
| 4279 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 4280 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4281 | len = map->stripe_len * nr_data_stripes(map); |
| 4282 | } |
| 4283 | free_extent_map(em); |
| 4284 | return len; |
| 4285 | } |
| 4286 | |
| 4287 | int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree, |
| 4288 | u64 logical, u64 len, int mirror_num) |
| 4289 | { |
| 4290 | struct extent_map *em; |
| 4291 | struct map_lookup *map; |
| 4292 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4293 | int ret = 0; |
| 4294 | |
| 4295 | read_lock(&em_tree->lock); |
| 4296 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4297 | read_unlock(&em_tree->lock); |
| 4298 | BUG_ON(!em); |
| 4299 | |
| 4300 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4301 | map = (struct map_lookup *)em->bdev; |
| 4302 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 4303 | BTRFS_BLOCK_GROUP_RAID6)) |
| 4304 | ret = 1; |
| 4305 | free_extent_map(em); |
| 4306 | return ret; |
| 4307 | } |
| 4308 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4309 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
| 4310 | struct map_lookup *map, int first, int num, |
| 4311 | int optimal, int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4312 | { |
| 4313 | int i; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4314 | int tolerance; |
| 4315 | struct btrfs_device *srcdev; |
| 4316 | |
| 4317 | if (dev_replace_is_ongoing && |
| 4318 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 4319 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 4320 | srcdev = fs_info->dev_replace.srcdev; |
| 4321 | else |
| 4322 | srcdev = NULL; |
| 4323 | |
| 4324 | /* |
| 4325 | * try to avoid the drive that is the source drive for a |
| 4326 | * dev-replace procedure, only choose it if no other non-missing |
| 4327 | * mirror is available |
| 4328 | */ |
| 4329 | for (tolerance = 0; tolerance < 2; tolerance++) { |
| 4330 | if (map->stripes[optimal].dev->bdev && |
| 4331 | (tolerance || map->stripes[optimal].dev != srcdev)) |
| 4332 | return optimal; |
| 4333 | for (i = first; i < first + num; i++) { |
| 4334 | if (map->stripes[i].dev->bdev && |
| 4335 | (tolerance || map->stripes[i].dev != srcdev)) |
| 4336 | return i; |
| 4337 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4338 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4339 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4340 | /* we couldn't find one that doesn't fail. Just return something |
| 4341 | * and the io error handling code will clean up eventually |
| 4342 | */ |
| 4343 | return optimal; |
| 4344 | } |
| 4345 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4346 | static inline int parity_smaller(u64 a, u64 b) |
| 4347 | { |
| 4348 | return a > b; |
| 4349 | } |
| 4350 | |
| 4351 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
| 4352 | static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map) |
| 4353 | { |
| 4354 | struct btrfs_bio_stripe s; |
| 4355 | int i; |
| 4356 | u64 l; |
| 4357 | int again = 1; |
| 4358 | |
| 4359 | while (again) { |
| 4360 | again = 0; |
| 4361 | for (i = 0; i < bbio->num_stripes - 1; i++) { |
| 4362 | if (parity_smaller(raid_map[i], raid_map[i+1])) { |
| 4363 | s = bbio->stripes[i]; |
| 4364 | l = raid_map[i]; |
| 4365 | bbio->stripes[i] = bbio->stripes[i+1]; |
| 4366 | raid_map[i] = raid_map[i+1]; |
| 4367 | bbio->stripes[i+1] = s; |
| 4368 | raid_map[i+1] = l; |
| 4369 | again = 1; |
| 4370 | } |
| 4371 | } |
| 4372 | } |
| 4373 | } |
| 4374 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4375 | 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] | 4376 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4377 | struct btrfs_bio **bbio_ret, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4378 | int mirror_num, u64 **raid_map_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4379 | { |
| 4380 | struct extent_map *em; |
| 4381 | struct map_lookup *map; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4382 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4383 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4384 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4385 | u64 stripe_offset; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4386 | u64 stripe_end_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4387 | u64 stripe_nr; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4388 | u64 stripe_nr_orig; |
| 4389 | u64 stripe_nr_end; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4390 | u64 stripe_len; |
| 4391 | u64 *raid_map = NULL; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4392 | int stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4393 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4394 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4395 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 4396 | int max_errors = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4397 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4398 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 4399 | int dev_replace_is_ongoing = 0; |
| 4400 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4401 | int patch_the_first_stripe_for_dev_replace = 0; |
| 4402 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4403 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4404 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4405 | read_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4406 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4407 | read_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4408 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4409 | if (!em) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4410 | btrfs_crit(fs_info, "unable to find logical %llu len %llu", |
| 4411 | (unsigned long long)logical, |
| 4412 | (unsigned long long)*length); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4413 | return -EINVAL; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4414 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4415 | |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4416 | if (em->start > logical || em->start + em->len < logical) { |
| 4417 | btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, " |
| 4418 | "found %Lu-%Lu\n", logical, em->start, |
| 4419 | em->start + em->len); |
| 4420 | return -EINVAL; |
| 4421 | } |
| 4422 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4423 | map = (struct map_lookup *)em->bdev; |
| 4424 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4425 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4426 | if (mirror_num > map->num_stripes) |
| 4427 | mirror_num = 0; |
| 4428 | |
| 4429 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4430 | stripe_nr = offset; |
| 4431 | /* |
| 4432 | * stripe_nr counts the total number of stripes we have to stride |
| 4433 | * to get to this block |
| 4434 | */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4435 | do_div(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4436 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4437 | stripe_offset = stripe_nr * stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4438 | BUG_ON(offset < stripe_offset); |
| 4439 | |
| 4440 | /* stripe_offset is the offset of this block in its stripe*/ |
| 4441 | stripe_offset = offset - stripe_offset; |
| 4442 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4443 | /* if we're here for raid56, we need to know the stripe aligned start */ |
| 4444 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4445 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 4446 | raid56_full_stripe_start = offset; |
| 4447 | |
| 4448 | /* allow a write of a full stripe, but make sure we don't |
| 4449 | * allow straddling of stripes |
| 4450 | */ |
| 4451 | do_div(raid56_full_stripe_start, full_stripe_len); |
| 4452 | raid56_full_stripe_start *= full_stripe_len; |
| 4453 | } |
| 4454 | |
| 4455 | if (rw & REQ_DISCARD) { |
| 4456 | /* we don't discard raid56 yet */ |
| 4457 | if (map->type & |
| 4458 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4459 | ret = -EOPNOTSUPP; |
| 4460 | goto out; |
| 4461 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4462 | *length = min_t(u64, em->len - offset, *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4463 | } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 4464 | u64 max_len; |
| 4465 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 4466 | For other RAID types and for RAID[56] reads, just allow a single |
| 4467 | stripe (on a single disk). */ |
| 4468 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) && |
| 4469 | (rw & REQ_WRITE)) { |
| 4470 | max_len = stripe_len * nr_data_stripes(map) - |
| 4471 | (offset - raid56_full_stripe_start); |
| 4472 | } else { |
| 4473 | /* we limit the length of each bio to what fits in a stripe */ |
| 4474 | max_len = stripe_len - stripe_offset; |
| 4475 | } |
| 4476 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4477 | } else { |
| 4478 | *length = em->len - offset; |
| 4479 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4480 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4481 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 4482 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4483 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4484 | goto out; |
| 4485 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4486 | btrfs_dev_replace_lock(dev_replace); |
| 4487 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 4488 | if (!dev_replace_is_ongoing) |
| 4489 | btrfs_dev_replace_unlock(dev_replace); |
| 4490 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4491 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
| 4492 | !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) && |
| 4493 | dev_replace->tgtdev != NULL) { |
| 4494 | /* |
| 4495 | * in dev-replace case, for repair case (that's the only |
| 4496 | * case where the mirror is selected explicitly when |
| 4497 | * calling btrfs_map_block), blocks left of the left cursor |
| 4498 | * can also be read from the target drive. |
| 4499 | * For REQ_GET_READ_MIRRORS, the target drive is added as |
| 4500 | * the last one to the array of stripes. For READ, it also |
| 4501 | * needs to be supported using the same mirror number. |
| 4502 | * If the requested block is not left of the left cursor, |
| 4503 | * EIO is returned. This can happen because btrfs_num_copies() |
| 4504 | * returns one more in the dev-replace case. |
| 4505 | */ |
| 4506 | u64 tmp_length = *length; |
| 4507 | struct btrfs_bio *tmp_bbio = NULL; |
| 4508 | int tmp_num_stripes; |
| 4509 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 4510 | int index_srcdev = 0; |
| 4511 | int found = 0; |
| 4512 | u64 physical_of_found = 0; |
| 4513 | |
| 4514 | ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4515 | logical, &tmp_length, &tmp_bbio, 0, NULL); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4516 | if (ret) { |
| 4517 | WARN_ON(tmp_bbio != NULL); |
| 4518 | goto out; |
| 4519 | } |
| 4520 | |
| 4521 | tmp_num_stripes = tmp_bbio->num_stripes; |
| 4522 | if (mirror_num > tmp_num_stripes) { |
| 4523 | /* |
| 4524 | * REQ_GET_READ_MIRRORS does not contain this |
| 4525 | * mirror, that means that the requested area |
| 4526 | * is not left of the left cursor |
| 4527 | */ |
| 4528 | ret = -EIO; |
| 4529 | kfree(tmp_bbio); |
| 4530 | goto out; |
| 4531 | } |
| 4532 | |
| 4533 | /* |
| 4534 | * process the rest of the function using the mirror_num |
| 4535 | * of the source drive. Therefore look it up first. |
| 4536 | * At the end, patch the device pointer to the one of the |
| 4537 | * target drive. |
| 4538 | */ |
| 4539 | for (i = 0; i < tmp_num_stripes; i++) { |
| 4540 | if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) { |
| 4541 | /* |
| 4542 | * In case of DUP, in order to keep it |
| 4543 | * simple, only add the mirror with the |
| 4544 | * lowest physical address |
| 4545 | */ |
| 4546 | if (found && |
| 4547 | physical_of_found <= |
| 4548 | tmp_bbio->stripes[i].physical) |
| 4549 | continue; |
| 4550 | index_srcdev = i; |
| 4551 | found = 1; |
| 4552 | physical_of_found = |
| 4553 | tmp_bbio->stripes[i].physical; |
| 4554 | } |
| 4555 | } |
| 4556 | |
| 4557 | if (found) { |
| 4558 | mirror_num = index_srcdev + 1; |
| 4559 | patch_the_first_stripe_for_dev_replace = 1; |
| 4560 | physical_to_patch_in_first_stripe = physical_of_found; |
| 4561 | } else { |
| 4562 | WARN_ON(1); |
| 4563 | ret = -EIO; |
| 4564 | kfree(tmp_bbio); |
| 4565 | goto out; |
| 4566 | } |
| 4567 | |
| 4568 | kfree(tmp_bbio); |
| 4569 | } else if (mirror_num > map->num_stripes) { |
| 4570 | mirror_num = 0; |
| 4571 | } |
| 4572 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4573 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4574 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4575 | stripe_nr_orig = stripe_nr; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4576 | stripe_nr_end = ALIGN(offset + *length, map->stripe_len); |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4577 | do_div(stripe_nr_end, map->stripe_len); |
| 4578 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 4579 | (offset + *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4580 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4581 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 4582 | if (rw & REQ_DISCARD) |
| 4583 | num_stripes = min_t(u64, map->num_stripes, |
| 4584 | stripe_nr_end - stripe_nr_orig); |
| 4585 | stripe_index = do_div(stripe_nr, map->num_stripes); |
| 4586 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 4587 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4588 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 4589 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4590 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4591 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4592 | stripe_index = find_live_mirror(fs_info, map, 0, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4593 | map->num_stripes, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4594 | current->pid % map->num_stripes, |
| 4595 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4596 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4597 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 4598 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4599 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 4600 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4601 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4602 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4603 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4604 | } else { |
| 4605 | mirror_num = 1; |
| 4606 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 4607 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4608 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 4609 | int factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4610 | |
| 4611 | stripe_index = do_div(stripe_nr, factor); |
| 4612 | stripe_index *= map->sub_stripes; |
| 4613 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 4614 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4615 | num_stripes = map->sub_stripes; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4616 | else if (rw & REQ_DISCARD) |
| 4617 | num_stripes = min_t(u64, map->sub_stripes * |
| 4618 | (stripe_nr_end - stripe_nr_orig), |
| 4619 | map->num_stripes); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4620 | else if (mirror_num) |
| 4621 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4622 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 4623 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4624 | stripe_index = find_live_mirror(fs_info, map, |
| 4625 | stripe_index, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4626 | map->sub_stripes, stripe_index + |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4627 | current->pid % map->sub_stripes, |
| 4628 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 4629 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4630 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4631 | |
| 4632 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 4633 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4634 | u64 tmp; |
| 4635 | |
| 4636 | if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1) |
| 4637 | && raid_map_ret) { |
| 4638 | int i, rot; |
| 4639 | |
| 4640 | /* push stripe_nr back to the start of the full stripe */ |
| 4641 | stripe_nr = raid56_full_stripe_start; |
| 4642 | do_div(stripe_nr, stripe_len); |
| 4643 | |
| 4644 | stripe_index = do_div(stripe_nr, nr_data_stripes(map)); |
| 4645 | |
| 4646 | /* RAID[56] write or recovery. Return all stripes */ |
| 4647 | num_stripes = map->num_stripes; |
| 4648 | max_errors = nr_parity_stripes(map); |
| 4649 | |
| 4650 | raid_map = kmalloc(sizeof(u64) * num_stripes, |
| 4651 | GFP_NOFS); |
| 4652 | if (!raid_map) { |
| 4653 | ret = -ENOMEM; |
| 4654 | goto out; |
| 4655 | } |
| 4656 | |
| 4657 | /* Work out the disk rotation on this stripe-set */ |
| 4658 | tmp = stripe_nr; |
| 4659 | rot = do_div(tmp, num_stripes); |
| 4660 | |
| 4661 | /* Fill in the logical address of each stripe */ |
| 4662 | tmp = stripe_nr * nr_data_stripes(map); |
| 4663 | for (i = 0; i < nr_data_stripes(map); i++) |
| 4664 | raid_map[(i+rot) % num_stripes] = |
| 4665 | em->start + (tmp + i) * map->stripe_len; |
| 4666 | |
| 4667 | raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 4668 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 4669 | raid_map[(i+rot+1) % num_stripes] = |
| 4670 | RAID6_Q_STRIPE; |
| 4671 | |
| 4672 | *length = map->stripe_len; |
| 4673 | stripe_index = 0; |
| 4674 | stripe_offset = 0; |
| 4675 | } else { |
| 4676 | /* |
| 4677 | * Mirror #0 or #1 means the original data block. |
| 4678 | * Mirror #2 is RAID5 parity block. |
| 4679 | * Mirror #3 is RAID6 Q block. |
| 4680 | */ |
| 4681 | stripe_index = do_div(stripe_nr, nr_data_stripes(map)); |
| 4682 | if (mirror_num > 1) |
| 4683 | stripe_index = nr_data_stripes(map) + |
| 4684 | mirror_num - 2; |
| 4685 | |
| 4686 | /* We distribute the parity blocks across stripes */ |
| 4687 | tmp = stripe_nr + stripe_index; |
| 4688 | stripe_index = do_div(tmp, map->num_stripes); |
| 4689 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4690 | } else { |
| 4691 | /* |
| 4692 | * after this do_div call, stripe_nr is the number of stripes |
| 4693 | * on this device we have to walk to find the data, and |
| 4694 | * stripe_index is the number of our device in the stripe array |
| 4695 | */ |
| 4696 | stripe_index = do_div(stripe_nr, map->num_stripes); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4697 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4698 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4699 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4700 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4701 | num_alloc_stripes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4702 | if (dev_replace_is_ongoing) { |
| 4703 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
| 4704 | num_alloc_stripes <<= 1; |
| 4705 | if (rw & REQ_GET_READ_MIRRORS) |
| 4706 | num_alloc_stripes++; |
| 4707 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4708 | bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4709 | if (!bbio) { |
| 4710 | ret = -ENOMEM; |
| 4711 | goto out; |
| 4712 | } |
| 4713 | atomic_set(&bbio->error, 0); |
| 4714 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4715 | if (rw & REQ_DISCARD) { |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4716 | int factor = 0; |
| 4717 | int sub_stripes = 0; |
| 4718 | u64 stripes_per_dev = 0; |
| 4719 | u32 remaining_stripes = 0; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4720 | u32 last_stripe = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4721 | |
| 4722 | if (map->type & |
| 4723 | (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 4724 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 4725 | sub_stripes = 1; |
| 4726 | else |
| 4727 | sub_stripes = map->sub_stripes; |
| 4728 | |
| 4729 | factor = map->num_stripes / sub_stripes; |
| 4730 | stripes_per_dev = div_u64_rem(stripe_nr_end - |
| 4731 | stripe_nr_orig, |
| 4732 | factor, |
| 4733 | &remaining_stripes); |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4734 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 4735 | last_stripe *= sub_stripes; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4736 | } |
| 4737 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4738 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4739 | bbio->stripes[i].physical = |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4740 | map->stripes[stripe_index].physical + |
| 4741 | stripe_offset + stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4742 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4743 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4744 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 4745 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 4746 | bbio->stripes[i].length = stripes_per_dev * |
| 4747 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4748 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4749 | if (i / sub_stripes < remaining_stripes) |
| 4750 | bbio->stripes[i].length += |
| 4751 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4752 | |
| 4753 | /* |
| 4754 | * Special for the first stripe and |
| 4755 | * the last stripe: |
| 4756 | * |
| 4757 | * |-------|...|-------| |
| 4758 | * |----------| |
| 4759 | * off end_off |
| 4760 | */ |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4761 | if (i < sub_stripes) |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4762 | bbio->stripes[i].length -= |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4763 | stripe_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4764 | |
| 4765 | if (stripe_index >= last_stripe && |
| 4766 | stripe_index <= (last_stripe + |
| 4767 | sub_stripes - 1)) |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4768 | bbio->stripes[i].length -= |
| 4769 | stripe_end_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4770 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4771 | if (i == sub_stripes - 1) |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4772 | stripe_offset = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4773 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4774 | bbio->stripes[i].length = *length; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4775 | |
| 4776 | stripe_index++; |
| 4777 | if (stripe_index == map->num_stripes) { |
| 4778 | /* This could only happen for RAID0/10 */ |
| 4779 | stripe_index = 0; |
| 4780 | stripe_nr++; |
| 4781 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4782 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4783 | } else { |
| 4784 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4785 | bbio->stripes[i].physical = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 4786 | map->stripes[stripe_index].physical + |
| 4787 | stripe_offset + |
| 4788 | stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4789 | bbio->stripes[i].dev = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 4790 | map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4791 | stripe_index++; |
| 4792 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4793 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4794 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 4795 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) { |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4796 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 4797 | BTRFS_BLOCK_GROUP_RAID10 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4798 | BTRFS_BLOCK_GROUP_RAID5 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4799 | BTRFS_BLOCK_GROUP_DUP)) { |
| 4800 | max_errors = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4801 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4802 | max_errors = 2; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4803 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4804 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4805 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4806 | if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) && |
| 4807 | dev_replace->tgtdev != NULL) { |
| 4808 | int index_where_to_add; |
| 4809 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 4810 | |
| 4811 | /* |
| 4812 | * duplicate the write operations while the dev replace |
| 4813 | * procedure is running. Since the copying of the old disk |
| 4814 | * to the new disk takes place at run time while the |
| 4815 | * filesystem is mounted writable, the regular write |
| 4816 | * operations to the old disk have to be duplicated to go |
| 4817 | * to the new disk as well. |
| 4818 | * Note that device->missing is handled by the caller, and |
| 4819 | * that the write to the old disk is already set up in the |
| 4820 | * stripes array. |
| 4821 | */ |
| 4822 | index_where_to_add = num_stripes; |
| 4823 | for (i = 0; i < num_stripes; i++) { |
| 4824 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 4825 | /* write to new disk, too */ |
| 4826 | struct btrfs_bio_stripe *new = |
| 4827 | bbio->stripes + index_where_to_add; |
| 4828 | struct btrfs_bio_stripe *old = |
| 4829 | bbio->stripes + i; |
| 4830 | |
| 4831 | new->physical = old->physical; |
| 4832 | new->length = old->length; |
| 4833 | new->dev = dev_replace->tgtdev; |
| 4834 | index_where_to_add++; |
| 4835 | max_errors++; |
| 4836 | } |
| 4837 | } |
| 4838 | num_stripes = index_where_to_add; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4839 | } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) && |
| 4840 | dev_replace->tgtdev != NULL) { |
| 4841 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 4842 | int index_srcdev = 0; |
| 4843 | int found = 0; |
| 4844 | u64 physical_of_found = 0; |
| 4845 | |
| 4846 | /* |
| 4847 | * During the dev-replace procedure, the target drive can |
| 4848 | * also be used to read data in case it is needed to repair |
| 4849 | * a corrupt block elsewhere. This is possible if the |
| 4850 | * requested area is left of the left cursor. In this area, |
| 4851 | * the target drive is a full copy of the source drive. |
| 4852 | */ |
| 4853 | for (i = 0; i < num_stripes; i++) { |
| 4854 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 4855 | /* |
| 4856 | * In case of DUP, in order to keep it |
| 4857 | * simple, only add the mirror with the |
| 4858 | * lowest physical address |
| 4859 | */ |
| 4860 | if (found && |
| 4861 | physical_of_found <= |
| 4862 | bbio->stripes[i].physical) |
| 4863 | continue; |
| 4864 | index_srcdev = i; |
| 4865 | found = 1; |
| 4866 | physical_of_found = bbio->stripes[i].physical; |
| 4867 | } |
| 4868 | } |
| 4869 | if (found) { |
| 4870 | u64 length = map->stripe_len; |
| 4871 | |
| 4872 | if (physical_of_found + length <= |
| 4873 | dev_replace->cursor_left) { |
| 4874 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 4875 | bbio->stripes + num_stripes; |
| 4876 | |
| 4877 | tgtdev_stripe->physical = physical_of_found; |
| 4878 | tgtdev_stripe->length = |
| 4879 | bbio->stripes[index_srcdev].length; |
| 4880 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 4881 | |
| 4882 | num_stripes++; |
| 4883 | } |
| 4884 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4885 | } |
| 4886 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4887 | *bbio_ret = bbio; |
| 4888 | bbio->num_stripes = num_stripes; |
| 4889 | bbio->max_errors = max_errors; |
| 4890 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4891 | |
| 4892 | /* |
| 4893 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 4894 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 4895 | * available as a mirror |
| 4896 | */ |
| 4897 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 4898 | WARN_ON(num_stripes > 1); |
| 4899 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 4900 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 4901 | bbio->mirror_num = map->num_stripes + 1; |
| 4902 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4903 | if (raid_map) { |
| 4904 | sort_parity_stripes(bbio, raid_map); |
| 4905 | *raid_map_ret = raid_map; |
| 4906 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4907 | out: |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4908 | if (dev_replace_is_ongoing) |
| 4909 | btrfs_dev_replace_unlock(dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4910 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4911 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4912 | } |
| 4913 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4914 | int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4915 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4916 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4917 | { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4918 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4919 | mirror_num, NULL); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4920 | } |
| 4921 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4922 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 4923 | u64 chunk_start, u64 physical, u64 devid, |
| 4924 | u64 **logical, int *naddrs, int *stripe_len) |
| 4925 | { |
| 4926 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4927 | struct extent_map *em; |
| 4928 | struct map_lookup *map; |
| 4929 | u64 *buf; |
| 4930 | u64 bytenr; |
| 4931 | u64 length; |
| 4932 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4933 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4934 | int i, j, nr = 0; |
| 4935 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4936 | read_lock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4937 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4938 | read_unlock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4939 | |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 4940 | if (!em) { |
| 4941 | printk(KERN_ERR "btrfs: couldn't find em for chunk %Lu\n", |
| 4942 | chunk_start); |
| 4943 | return -EIO; |
| 4944 | } |
| 4945 | |
| 4946 | if (em->start != chunk_start) { |
| 4947 | printk(KERN_ERR "btrfs: bad chunk start, em=%Lu, wanted=%Lu\n", |
| 4948 | em->start, chunk_start); |
| 4949 | free_extent_map(em); |
| 4950 | return -EIO; |
| 4951 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4952 | map = (struct map_lookup *)em->bdev; |
| 4953 | |
| 4954 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4955 | rmap_len = map->stripe_len; |
| 4956 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4957 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4958 | do_div(length, map->num_stripes / map->sub_stripes); |
| 4959 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 4960 | do_div(length, map->num_stripes); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4961 | else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 4962 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4963 | do_div(length, nr_data_stripes(map)); |
| 4964 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 4965 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4966 | |
| 4967 | buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4968 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4969 | |
| 4970 | for (i = 0; i < map->num_stripes; i++) { |
| 4971 | if (devid && map->stripes[i].dev->devid != devid) |
| 4972 | continue; |
| 4973 | if (map->stripes[i].physical > physical || |
| 4974 | map->stripes[i].physical + length <= physical) |
| 4975 | continue; |
| 4976 | |
| 4977 | stripe_nr = physical - map->stripes[i].physical; |
| 4978 | do_div(stripe_nr, map->stripe_len); |
| 4979 | |
| 4980 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 4981 | stripe_nr = stripe_nr * map->num_stripes + i; |
| 4982 | do_div(stripe_nr, map->sub_stripes); |
| 4983 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 4984 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4985 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 4986 | * Alternatively, just use rmap_len below instead of |
| 4987 | * map->stripe_len */ |
| 4988 | |
| 4989 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 4990 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4991 | for (j = 0; j < nr; j++) { |
| 4992 | if (buf[j] == bytenr) |
| 4993 | break; |
| 4994 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 4995 | if (j == nr) { |
| 4996 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4997 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 4998 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4999 | } |
| 5000 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5001 | *logical = buf; |
| 5002 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5003 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5004 | |
| 5005 | free_extent_map(em); |
| 5006 | return 0; |
| 5007 | } |
| 5008 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5009 | static void btrfs_end_bio(struct bio *bio, int err) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5010 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5011 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5012 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5013 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5014 | if (err) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5015 | atomic_inc(&bbio->error); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5016 | if (err == -EIO || err == -EREMOTEIO) { |
| 5017 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5018 | btrfs_io_bio(bio)->stripe_index; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5019 | struct btrfs_device *dev; |
| 5020 | |
| 5021 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 5022 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5023 | if (dev->bdev) { |
| 5024 | if (bio->bi_rw & WRITE) |
| 5025 | btrfs_dev_stat_inc(dev, |
| 5026 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 5027 | else |
| 5028 | btrfs_dev_stat_inc(dev, |
| 5029 | BTRFS_DEV_STAT_READ_ERRS); |
| 5030 | if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH) |
| 5031 | btrfs_dev_stat_inc(dev, |
| 5032 | BTRFS_DEV_STAT_FLUSH_ERRS); |
| 5033 | btrfs_dev_stat_print_on_error(dev); |
| 5034 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5035 | } |
| 5036 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5037 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5038 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5039 | is_orig_bio = 1; |
| 5040 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5041 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5042 | if (!is_orig_bio) { |
| 5043 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5044 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5045 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5046 | bio->bi_private = bbio->private; |
| 5047 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5048 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5049 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5050 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5051 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5052 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5053 | err = -EIO; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 5054 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5055 | /* |
| 5056 | * this bio is actually up to date, we didn't |
| 5057 | * go over the max number of errors |
| 5058 | */ |
| 5059 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5060 | err = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5061 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5062 | kfree(bbio); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5063 | |
| 5064 | bio_endio(bio, err); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5065 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5066 | bio_put(bio); |
| 5067 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5068 | } |
| 5069 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5070 | struct async_sched { |
| 5071 | struct bio *bio; |
| 5072 | int rw; |
| 5073 | struct btrfs_fs_info *info; |
| 5074 | struct btrfs_work work; |
| 5075 | }; |
| 5076 | |
| 5077 | /* |
| 5078 | * see run_scheduled_bios for a description of why bios are collected for |
| 5079 | * async submit. |
| 5080 | * |
| 5081 | * This will add one bio to the pending list for a device and make sure |
| 5082 | * the work struct is scheduled. |
| 5083 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5084 | static noinline void btrfs_schedule_bio(struct btrfs_root *root, |
| 5085 | struct btrfs_device *device, |
| 5086 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5087 | { |
| 5088 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5089 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5090 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5091 | if (device->missing || !device->bdev) { |
| 5092 | bio_endio(bio, -EIO); |
| 5093 | return; |
| 5094 | } |
| 5095 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5096 | /* don't bother with additional async steps for reads, right now */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5097 | if (!(rw & REQ_WRITE)) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5098 | bio_get(bio); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 5099 | btrfsic_submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5100 | bio_put(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5101 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5102 | } |
| 5103 | |
| 5104 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5105 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5106 | * higher layers. Otherwise, the async bio makes it appear we have |
| 5107 | * made progress against dirty pages when we've really just put it |
| 5108 | * on a queue for later |
| 5109 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5110 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5111 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5112 | bio->bi_next = NULL; |
| 5113 | bio->bi_rw |= rw; |
| 5114 | |
| 5115 | spin_lock(&device->io_lock); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5116 | if (bio->bi_rw & REQ_SYNC) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5117 | pending_bios = &device->pending_sync_bios; |
| 5118 | else |
| 5119 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5120 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5121 | if (pending_bios->tail) |
| 5122 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5123 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5124 | pending_bios->tail = bio; |
| 5125 | if (!pending_bios->head) |
| 5126 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5127 | if (device->running_pending) |
| 5128 | should_queue = 0; |
| 5129 | |
| 5130 | spin_unlock(&device->io_lock); |
| 5131 | |
| 5132 | if (should_queue) |
Chris Mason | 1cc127b | 2008-06-12 14:46:17 -0400 | [diff] [blame] | 5133 | btrfs_queue_worker(&root->fs_info->submit_workers, |
| 5134 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5135 | } |
| 5136 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5137 | static int bio_size_ok(struct block_device *bdev, struct bio *bio, |
| 5138 | sector_t sector) |
| 5139 | { |
| 5140 | struct bio_vec *prev; |
| 5141 | struct request_queue *q = bdev_get_queue(bdev); |
| 5142 | unsigned short max_sectors = queue_max_sectors(q); |
| 5143 | struct bvec_merge_data bvm = { |
| 5144 | .bi_bdev = bdev, |
| 5145 | .bi_sector = sector, |
| 5146 | .bi_rw = bio->bi_rw, |
| 5147 | }; |
| 5148 | |
| 5149 | if (bio->bi_vcnt == 0) { |
| 5150 | WARN_ON(1); |
| 5151 | return 1; |
| 5152 | } |
| 5153 | |
| 5154 | prev = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
| 5155 | if ((bio->bi_size >> 9) > max_sectors) |
| 5156 | return 0; |
| 5157 | |
| 5158 | if (!q->merge_bvec_fn) |
| 5159 | return 1; |
| 5160 | |
| 5161 | bvm.bi_size = bio->bi_size - prev->bv_len; |
| 5162 | if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) |
| 5163 | return 0; |
| 5164 | return 1; |
| 5165 | } |
| 5166 | |
| 5167 | static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5168 | struct bio *bio, u64 physical, int dev_nr, |
| 5169 | int rw, int async) |
| 5170 | { |
| 5171 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
| 5172 | |
| 5173 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5174 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5175 | bio->bi_end_io = btrfs_end_bio; |
| 5176 | bio->bi_sector = physical >> 9; |
| 5177 | #ifdef DEBUG |
| 5178 | { |
| 5179 | struct rcu_string *name; |
| 5180 | |
| 5181 | rcu_read_lock(); |
| 5182 | name = rcu_dereference(dev->name); |
Masanari Iida | d142324 | 2012-10-31 15:16:32 +0000 | [diff] [blame] | 5183 | pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu " |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5184 | "(%s id %llu), size=%u\n", rw, |
| 5185 | (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev, |
| 5186 | name->str, dev->devid, bio->bi_size); |
| 5187 | rcu_read_unlock(); |
| 5188 | } |
| 5189 | #endif |
| 5190 | bio->bi_bdev = dev->bdev; |
| 5191 | if (async) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5192 | btrfs_schedule_bio(root, dev, rw, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5193 | else |
| 5194 | btrfsic_submit_bio(rw, bio); |
| 5195 | } |
| 5196 | |
| 5197 | static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5198 | struct bio *first_bio, struct btrfs_device *dev, |
| 5199 | int dev_nr, int rw, int async) |
| 5200 | { |
| 5201 | struct bio_vec *bvec = first_bio->bi_io_vec; |
| 5202 | struct bio *bio; |
| 5203 | int nr_vecs = bio_get_nr_vecs(dev->bdev); |
| 5204 | u64 physical = bbio->stripes[dev_nr].physical; |
| 5205 | |
| 5206 | again: |
| 5207 | bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS); |
| 5208 | if (!bio) |
| 5209 | return -ENOMEM; |
| 5210 | |
| 5211 | while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) { |
| 5212 | if (bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 5213 | bvec->bv_offset) < bvec->bv_len) { |
| 5214 | u64 len = bio->bi_size; |
| 5215 | |
| 5216 | atomic_inc(&bbio->stripes_pending); |
| 5217 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, |
| 5218 | rw, async); |
| 5219 | physical += len; |
| 5220 | goto again; |
| 5221 | } |
| 5222 | bvec++; |
| 5223 | } |
| 5224 | |
| 5225 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async); |
| 5226 | return 0; |
| 5227 | } |
| 5228 | |
| 5229 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 5230 | { |
| 5231 | atomic_inc(&bbio->error); |
| 5232 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
| 5233 | bio->bi_private = bbio->private; |
| 5234 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5235 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5236 | bio->bi_sector = logical >> 9; |
| 5237 | kfree(bbio); |
| 5238 | bio_endio(bio, -EIO); |
| 5239 | } |
| 5240 | } |
| 5241 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5242 | 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] | 5243 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5244 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5245 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5246 | struct bio *first_bio = bio; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 5247 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5248 | u64 length = 0; |
| 5249 | u64 map_length; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5250 | u64 *raid_map = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5251 | int ret; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5252 | int dev_nr = 0; |
| 5253 | int total_devs = 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5254 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5255 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5256 | length = bio->bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5257 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5258 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5259 | ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio, |
| 5260 | mirror_num, &raid_map); |
| 5261 | if (ret) /* -ENOMEM */ |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5262 | return ret; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5263 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5264 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5265 | bbio->orig_bio = first_bio; |
| 5266 | bbio->private = first_bio->bi_private; |
| 5267 | bbio->end_io = first_bio->bi_end_io; |
| 5268 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 5269 | |
| 5270 | if (raid_map) { |
| 5271 | /* In this case, map_length has been set to the length of |
| 5272 | a single stripe; not the whole write */ |
| 5273 | if (rw & WRITE) { |
| 5274 | return raid56_parity_write(root, bio, bbio, |
| 5275 | raid_map, map_length); |
| 5276 | } else { |
| 5277 | return raid56_parity_recover(root, bio, bbio, |
| 5278 | raid_map, map_length, |
| 5279 | mirror_num); |
| 5280 | } |
| 5281 | } |
| 5282 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5283 | if (map_length < length) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5284 | btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu", |
| 5285 | (unsigned long long)logical, |
| 5286 | (unsigned long long)length, |
| 5287 | (unsigned long long)map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5288 | BUG(); |
| 5289 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5290 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5291 | while (dev_nr < total_devs) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5292 | dev = bbio->stripes[dev_nr].dev; |
| 5293 | if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) { |
| 5294 | bbio_error(bbio, first_bio, logical); |
| 5295 | dev_nr++; |
| 5296 | continue; |
| 5297 | } |
| 5298 | |
| 5299 | /* |
| 5300 | * Check and see if we're ok with this bio based on it's size |
| 5301 | * and offset with the given device. |
| 5302 | */ |
| 5303 | if (!bio_size_ok(dev->bdev, first_bio, |
| 5304 | bbio->stripes[dev_nr].physical >> 9)) { |
| 5305 | ret = breakup_stripe_bio(root, bbio, first_bio, dev, |
| 5306 | dev_nr, rw, async_submit); |
| 5307 | BUG_ON(ret); |
| 5308 | dev_nr++; |
| 5309 | continue; |
| 5310 | } |
| 5311 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5312 | if (dev_nr < total_devs - 1) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5313 | bio = btrfs_bio_clone(first_bio, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5314 | BUG_ON(!bio); /* -ENOMEM */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5315 | } else { |
| 5316 | bio = first_bio; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5317 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5318 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5319 | submit_stripe_bio(root, bbio, bio, |
| 5320 | bbio->stripes[dev_nr].physical, dev_nr, rw, |
| 5321 | async_submit); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5322 | dev_nr++; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5323 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5324 | return 0; |
| 5325 | } |
| 5326 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5327 | 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] | 5328 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5329 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5330 | struct btrfs_device *device; |
| 5331 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5332 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5333 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5334 | while (cur_devices) { |
| 5335 | if (!fsid || |
| 5336 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 5337 | device = __find_device(&cur_devices->devices, |
| 5338 | devid, uuid); |
| 5339 | if (device) |
| 5340 | return device; |
| 5341 | } |
| 5342 | cur_devices = cur_devices->seed; |
| 5343 | } |
| 5344 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5345 | } |
| 5346 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5347 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
| 5348 | u64 devid, u8 *dev_uuid) |
| 5349 | { |
| 5350 | struct btrfs_device *device; |
| 5351 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 5352 | |
| 5353 | device = kzalloc(sizeof(*device), GFP_NOFS); |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 5354 | if (!device) |
| 5355 | return NULL; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5356 | list_add(&device->dev_list, |
| 5357 | &fs_devices->devices); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5358 | device->devid = devid; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5359 | device->work.func = pending_bios_fn; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5360 | device->fs_devices = fs_devices; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 5361 | device->missing = 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5362 | fs_devices->num_devices++; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 5363 | fs_devices->missing_devices++; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5364 | spin_lock_init(&device->io_lock); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 5365 | INIT_LIST_HEAD(&device->dev_alloc_list); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5366 | memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE); |
| 5367 | return device; |
| 5368 | } |
| 5369 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5370 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 5371 | struct extent_buffer *leaf, |
| 5372 | struct btrfs_chunk *chunk) |
| 5373 | { |
| 5374 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 5375 | struct map_lookup *map; |
| 5376 | struct extent_map *em; |
| 5377 | u64 logical; |
| 5378 | u64 length; |
| 5379 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5380 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5381 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5382 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5383 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5384 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5385 | logical = key->offset; |
| 5386 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5387 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5388 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5389 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5390 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5391 | |
| 5392 | /* already mapped? */ |
| 5393 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 5394 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5395 | return 0; |
| 5396 | } else if (em) { |
| 5397 | free_extent_map(em); |
| 5398 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5399 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5400 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5401 | if (!em) |
| 5402 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5403 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 5404 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5405 | if (!map) { |
| 5406 | free_extent_map(em); |
| 5407 | return -ENOMEM; |
| 5408 | } |
| 5409 | |
| 5410 | em->bdev = (struct block_device *)map; |
| 5411 | em->start = logical; |
| 5412 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 5413 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5414 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5415 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5416 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5417 | map->num_stripes = num_stripes; |
| 5418 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 5419 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 5420 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 5421 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 5422 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5423 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5424 | for (i = 0; i < num_stripes; i++) { |
| 5425 | map->stripes[i].physical = |
| 5426 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 5427 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5428 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 5429 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 5430 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5431 | map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, |
| 5432 | uuid, NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5433 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5434 | kfree(map); |
| 5435 | free_extent_map(em); |
| 5436 | return -EIO; |
| 5437 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5438 | if (!map->stripes[i].dev) { |
| 5439 | map->stripes[i].dev = |
| 5440 | add_missing_dev(root, devid, uuid); |
| 5441 | if (!map->stripes[i].dev) { |
| 5442 | kfree(map); |
| 5443 | free_extent_map(em); |
| 5444 | return -EIO; |
| 5445 | } |
| 5446 | } |
| 5447 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5448 | } |
| 5449 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5450 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5451 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5452 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5453 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5454 | free_extent_map(em); |
| 5455 | |
| 5456 | return 0; |
| 5457 | } |
| 5458 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5459 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5460 | struct btrfs_dev_item *dev_item, |
| 5461 | struct btrfs_device *device) |
| 5462 | { |
| 5463 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5464 | |
| 5465 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 5466 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 5467 | device->total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5468 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
| 5469 | device->type = btrfs_device_type(leaf, dev_item); |
| 5470 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 5471 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 5472 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 5473 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 5474 | device->is_tgtdev_for_dev_replace = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5475 | |
| 5476 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5477 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5478 | } |
| 5479 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5480 | static int open_seed_devices(struct btrfs_root *root, u8 *fsid) |
| 5481 | { |
| 5482 | struct btrfs_fs_devices *fs_devices; |
| 5483 | int ret; |
| 5484 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 5485 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5486 | |
| 5487 | fs_devices = root->fs_info->fs_devices->seed; |
| 5488 | while (fs_devices) { |
| 5489 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 5490 | ret = 0; |
| 5491 | goto out; |
| 5492 | } |
| 5493 | fs_devices = fs_devices->seed; |
| 5494 | } |
| 5495 | |
| 5496 | fs_devices = find_fsid(fsid); |
| 5497 | if (!fs_devices) { |
| 5498 | ret = -ENOENT; |
| 5499 | goto out; |
| 5500 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5501 | |
| 5502 | fs_devices = clone_fs_devices(fs_devices); |
| 5503 | if (IS_ERR(fs_devices)) { |
| 5504 | ret = PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5505 | goto out; |
| 5506 | } |
| 5507 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 5508 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 5509 | root->fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 5510 | if (ret) { |
| 5511 | free_fs_devices(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5512 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 5513 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5514 | |
| 5515 | if (!fs_devices->seeding) { |
| 5516 | __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5517 | free_fs_devices(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5518 | ret = -EINVAL; |
| 5519 | goto out; |
| 5520 | } |
| 5521 | |
| 5522 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 5523 | root->fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5524 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5525 | return ret; |
| 5526 | } |
| 5527 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5528 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5529 | struct extent_buffer *leaf, |
| 5530 | struct btrfs_dev_item *dev_item) |
| 5531 | { |
| 5532 | struct btrfs_device *device; |
| 5533 | u64 devid; |
| 5534 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5535 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5536 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 5537 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5538 | devid = btrfs_device_id(leaf, dev_item); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5539 | read_extent_buffer(leaf, dev_uuid, |
| 5540 | (unsigned long)btrfs_device_uuid(dev_item), |
| 5541 | BTRFS_UUID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5542 | read_extent_buffer(leaf, fs_uuid, |
| 5543 | (unsigned long)btrfs_device_fsid(dev_item), |
| 5544 | BTRFS_UUID_SIZE); |
| 5545 | |
| 5546 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
| 5547 | ret = open_seed_devices(root, fs_uuid); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5548 | if (ret && !btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5549 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5550 | } |
| 5551 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5552 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5553 | if (!device || !device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5554 | if (!btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5555 | return -EIO; |
| 5556 | |
| 5557 | if (!device) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5558 | btrfs_warn(root->fs_info, "devid %llu missing", |
| 5559 | (unsigned long long)devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5560 | device = add_missing_dev(root, devid, dev_uuid); |
| 5561 | if (!device) |
| 5562 | return -ENOMEM; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 5563 | } else if (!device->missing) { |
| 5564 | /* |
| 5565 | * this happens when a device that was properly setup |
| 5566 | * in the device info lists suddenly goes bad. |
| 5567 | * device->bdev is NULL, and so we have to set |
| 5568 | * device->missing to one here |
| 5569 | */ |
| 5570 | root->fs_info->fs_devices->missing_devices++; |
| 5571 | device->missing = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5572 | } |
| 5573 | } |
| 5574 | |
| 5575 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 5576 | BUG_ON(device->writeable); |
| 5577 | if (device->generation != |
| 5578 | btrfs_device_generation(leaf, dev_item)) |
| 5579 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5580 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5581 | |
| 5582 | fill_device_from_item(leaf, dev_item, device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5583 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 5584 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5585 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 5586 | spin_lock(&root->fs_info->free_chunk_lock); |
| 5587 | root->fs_info->free_chunk_space += device->total_bytes - |
| 5588 | device->bytes_used; |
| 5589 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 5590 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5591 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5592 | return ret; |
| 5593 | } |
| 5594 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5595 | int btrfs_read_sys_array(struct btrfs_root *root) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5596 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 5597 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5598 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5599 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5600 | struct btrfs_chunk *chunk; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 5601 | u8 *ptr; |
| 5602 | unsigned long sb_ptr; |
| 5603 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5604 | u32 num_stripes; |
| 5605 | u32 array_size; |
| 5606 | u32 len = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5607 | u32 cur; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 5608 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5609 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5610 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET, |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5611 | BTRFS_SUPER_INFO_SIZE); |
| 5612 | if (!sb) |
| 5613 | return -ENOMEM; |
| 5614 | btrfs_set_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 5615 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 5616 | /* |
| 5617 | * The sb extent buffer is artifical and just used to read the system array. |
| 5618 | * btrfs_set_buffer_uptodate() call does not properly mark all it's |
| 5619 | * pages up-to-date when the page is larger: extent does not cover the |
| 5620 | * whole page and consequently check_page_uptodate does not find all |
| 5621 | * the page's extents up-to-date (the hole beyond sb), |
| 5622 | * write_extent_buffer then triggers a WARN_ON. |
| 5623 | * |
| 5624 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 5625 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 5626 | * to silence the warning eg. on PowerPC 64. |
| 5627 | */ |
| 5628 | if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5629 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5630 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5631 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5632 | array_size = btrfs_super_sys_array_size(super_copy); |
| 5633 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5634 | ptr = super_copy->sys_chunk_array; |
| 5635 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 5636 | cur = 0; |
| 5637 | |
| 5638 | while (cur < array_size) { |
| 5639 | disk_key = (struct btrfs_disk_key *)ptr; |
| 5640 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 5641 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5642 | len = sizeof(*disk_key); ptr += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5643 | sb_ptr += len; |
| 5644 | cur += len; |
| 5645 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5646 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5647 | chunk = (struct btrfs_chunk *)sb_ptr; |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5648 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 5649 | if (ret) |
| 5650 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5651 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 5652 | len = btrfs_chunk_item_size(num_stripes); |
| 5653 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 5654 | ret = -EIO; |
| 5655 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5656 | } |
| 5657 | ptr += len; |
| 5658 | sb_ptr += len; |
| 5659 | cur += len; |
| 5660 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5661 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 5662 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5663 | } |
| 5664 | |
| 5665 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 5666 | { |
| 5667 | struct btrfs_path *path; |
| 5668 | struct extent_buffer *leaf; |
| 5669 | struct btrfs_key key; |
| 5670 | struct btrfs_key found_key; |
| 5671 | int ret; |
| 5672 | int slot; |
| 5673 | |
| 5674 | root = root->fs_info->chunk_root; |
| 5675 | |
| 5676 | path = btrfs_alloc_path(); |
| 5677 | if (!path) |
| 5678 | return -ENOMEM; |
| 5679 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 5680 | mutex_lock(&uuid_mutex); |
| 5681 | lock_chunks(root); |
| 5682 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5683 | /* first we search for all of the device items, and then we |
| 5684 | * read in all of the chunk items. This way we can create chunk |
| 5685 | * mappings that reference all of the devices that are afound |
| 5686 | */ |
| 5687 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 5688 | key.offset = 0; |
| 5689 | key.type = 0; |
| 5690 | again: |
| 5691 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 5692 | if (ret < 0) |
| 5693 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5694 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5695 | leaf = path->nodes[0]; |
| 5696 | slot = path->slots[0]; |
| 5697 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5698 | ret = btrfs_next_leaf(root, path); |
| 5699 | if (ret == 0) |
| 5700 | continue; |
| 5701 | if (ret < 0) |
| 5702 | goto error; |
| 5703 | break; |
| 5704 | } |
| 5705 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5706 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { |
| 5707 | if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID) |
| 5708 | break; |
| 5709 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 5710 | struct btrfs_dev_item *dev_item; |
| 5711 | dev_item = btrfs_item_ptr(leaf, slot, |
| 5712 | struct btrfs_dev_item); |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5713 | ret = read_one_dev(root, leaf, dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5714 | if (ret) |
| 5715 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5716 | } |
| 5717 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 5718 | struct btrfs_chunk *chunk; |
| 5719 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 5720 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5721 | if (ret) |
| 5722 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5723 | } |
| 5724 | path->slots[0]++; |
| 5725 | } |
| 5726 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { |
| 5727 | key.objectid = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5728 | btrfs_release_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5729 | goto again; |
| 5730 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5731 | ret = 0; |
| 5732 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 5733 | unlock_chunks(root); |
| 5734 | mutex_unlock(&uuid_mutex); |
| 5735 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5736 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5737 | return ret; |
| 5738 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5739 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 5740 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 5741 | { |
| 5742 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 5743 | struct btrfs_device *device; |
| 5744 | |
| 5745 | mutex_lock(&fs_devices->device_list_mutex); |
| 5746 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
| 5747 | device->dev_root = fs_info->dev_root; |
| 5748 | mutex_unlock(&fs_devices->device_list_mutex); |
| 5749 | } |
| 5750 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5751 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 5752 | { |
| 5753 | int i; |
| 5754 | |
| 5755 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 5756 | btrfs_dev_stat_reset(dev, i); |
| 5757 | } |
| 5758 | |
| 5759 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 5760 | { |
| 5761 | struct btrfs_key key; |
| 5762 | struct btrfs_key found_key; |
| 5763 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 5764 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 5765 | struct extent_buffer *eb; |
| 5766 | int slot; |
| 5767 | int ret = 0; |
| 5768 | struct btrfs_device *device; |
| 5769 | struct btrfs_path *path = NULL; |
| 5770 | int i; |
| 5771 | |
| 5772 | path = btrfs_alloc_path(); |
| 5773 | if (!path) { |
| 5774 | ret = -ENOMEM; |
| 5775 | goto out; |
| 5776 | } |
| 5777 | |
| 5778 | mutex_lock(&fs_devices->device_list_mutex); |
| 5779 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 5780 | int item_size; |
| 5781 | struct btrfs_dev_stats_item *ptr; |
| 5782 | |
| 5783 | key.objectid = 0; |
| 5784 | key.type = BTRFS_DEV_STATS_KEY; |
| 5785 | key.offset = device->devid; |
| 5786 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 5787 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5788 | __btrfs_reset_dev_stats(device); |
| 5789 | device->dev_stats_valid = 1; |
| 5790 | btrfs_release_path(path); |
| 5791 | continue; |
| 5792 | } |
| 5793 | slot = path->slots[0]; |
| 5794 | eb = path->nodes[0]; |
| 5795 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 5796 | item_size = btrfs_item_size_nr(eb, slot); |
| 5797 | |
| 5798 | ptr = btrfs_item_ptr(eb, slot, |
| 5799 | struct btrfs_dev_stats_item); |
| 5800 | |
| 5801 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 5802 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 5803 | btrfs_dev_stat_set(device, i, |
| 5804 | btrfs_dev_stats_value(eb, ptr, i)); |
| 5805 | else |
| 5806 | btrfs_dev_stat_reset(device, i); |
| 5807 | } |
| 5808 | |
| 5809 | device->dev_stats_valid = 1; |
| 5810 | btrfs_dev_stat_print_on_load(device); |
| 5811 | btrfs_release_path(path); |
| 5812 | } |
| 5813 | mutex_unlock(&fs_devices->device_list_mutex); |
| 5814 | |
| 5815 | out: |
| 5816 | btrfs_free_path(path); |
| 5817 | return ret < 0 ? ret : 0; |
| 5818 | } |
| 5819 | |
| 5820 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
| 5821 | struct btrfs_root *dev_root, |
| 5822 | struct btrfs_device *device) |
| 5823 | { |
| 5824 | struct btrfs_path *path; |
| 5825 | struct btrfs_key key; |
| 5826 | struct extent_buffer *eb; |
| 5827 | struct btrfs_dev_stats_item *ptr; |
| 5828 | int ret; |
| 5829 | int i; |
| 5830 | |
| 5831 | key.objectid = 0; |
| 5832 | key.type = BTRFS_DEV_STATS_KEY; |
| 5833 | key.offset = device->devid; |
| 5834 | |
| 5835 | path = btrfs_alloc_path(); |
| 5836 | BUG_ON(!path); |
| 5837 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 5838 | if (ret < 0) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5839 | printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n", |
| 5840 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5841 | goto out; |
| 5842 | } |
| 5843 | |
| 5844 | if (ret == 0 && |
| 5845 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 5846 | /* need to delete old one and insert a new one */ |
| 5847 | ret = btrfs_del_item(trans, dev_root, path); |
| 5848 | if (ret != 0) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5849 | printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n", |
| 5850 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5851 | goto out; |
| 5852 | } |
| 5853 | ret = 1; |
| 5854 | } |
| 5855 | |
| 5856 | if (ret == 1) { |
| 5857 | /* need to insert a new item */ |
| 5858 | btrfs_release_path(path); |
| 5859 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 5860 | &key, sizeof(*ptr)); |
| 5861 | if (ret < 0) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5862 | printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n", |
| 5863 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5864 | goto out; |
| 5865 | } |
| 5866 | } |
| 5867 | |
| 5868 | eb = path->nodes[0]; |
| 5869 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 5870 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 5871 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 5872 | btrfs_dev_stat_read(device, i)); |
| 5873 | btrfs_mark_buffer_dirty(eb); |
| 5874 | |
| 5875 | out: |
| 5876 | btrfs_free_path(path); |
| 5877 | return ret; |
| 5878 | } |
| 5879 | |
| 5880 | /* |
| 5881 | * called from commit_transaction. Writes all changed device stats to disk. |
| 5882 | */ |
| 5883 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 5884 | struct btrfs_fs_info *fs_info) |
| 5885 | { |
| 5886 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 5887 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 5888 | struct btrfs_device *device; |
| 5889 | int ret = 0; |
| 5890 | |
| 5891 | mutex_lock(&fs_devices->device_list_mutex); |
| 5892 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 5893 | if (!device->dev_stats_valid || !device->dev_stats_dirty) |
| 5894 | continue; |
| 5895 | |
| 5896 | ret = update_dev_stat_item(trans, dev_root, device); |
| 5897 | if (!ret) |
| 5898 | device->dev_stats_dirty = 0; |
| 5899 | } |
| 5900 | mutex_unlock(&fs_devices->device_list_mutex); |
| 5901 | |
| 5902 | return ret; |
| 5903 | } |
| 5904 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5905 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 5906 | { |
| 5907 | btrfs_dev_stat_inc(dev, index); |
| 5908 | btrfs_dev_stat_print_on_error(dev); |
| 5909 | } |
| 5910 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5911 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5912 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5913 | if (!dev->dev_stats_valid) |
| 5914 | return; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5915 | printk_ratelimited_in_rcu(KERN_ERR |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5916 | "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] | 5917 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5918 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 5919 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 5920 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 5921 | btrfs_dev_stat_read(dev, |
| 5922 | BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 5923 | btrfs_dev_stat_read(dev, |
| 5924 | BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 5925 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5926 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5927 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 5928 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 5929 | int i; |
| 5930 | |
| 5931 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 5932 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 5933 | break; |
| 5934 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 5935 | return; /* all values == 0, suppress message */ |
| 5936 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5937 | printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n", |
| 5938 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5939 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 5940 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 5941 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 5942 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 5943 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 5944 | } |
| 5945 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5946 | int btrfs_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 5947 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5948 | { |
| 5949 | struct btrfs_device *dev; |
| 5950 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 5951 | int i; |
| 5952 | |
| 5953 | mutex_lock(&fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5954 | dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5955 | mutex_unlock(&fs_devices->device_list_mutex); |
| 5956 | |
| 5957 | if (!dev) { |
| 5958 | printk(KERN_WARNING |
| 5959 | "btrfs: get dev_stats failed, device not found\n"); |
| 5960 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5961 | } else if (!dev->dev_stats_valid) { |
| 5962 | printk(KERN_WARNING |
| 5963 | "btrfs: get dev_stats failed, not yet valid\n"); |
| 5964 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 5965 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5966 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 5967 | if (stats->nr_items > i) |
| 5968 | stats->values[i] = |
| 5969 | btrfs_dev_stat_read_and_reset(dev, i); |
| 5970 | else |
| 5971 | btrfs_dev_stat_reset(dev, i); |
| 5972 | } |
| 5973 | } else { |
| 5974 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 5975 | if (stats->nr_items > i) |
| 5976 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 5977 | } |
| 5978 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 5979 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 5980 | return 0; |
| 5981 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 5982 | |
| 5983 | int btrfs_scratch_superblock(struct btrfs_device *device) |
| 5984 | { |
| 5985 | struct buffer_head *bh; |
| 5986 | struct btrfs_super_block *disk_super; |
| 5987 | |
| 5988 | bh = btrfs_read_dev_super(device->bdev); |
| 5989 | if (!bh) |
| 5990 | return -EINVAL; |
| 5991 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 5992 | |
| 5993 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 5994 | set_buffer_dirty(bh); |
| 5995 | sync_dirty_buffer(bh); |
| 5996 | brelse(bh); |
| 5997 | |
| 5998 | return 0; |
| 5999 | } |