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