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