Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/bio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 21 | #include <linux/buffer_head.h> |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 22 | #include <linux/blkdev.h> |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 23 | #include <linux/random.h> |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 24 | #include <linux/iocontext.h> |
Ben Hutchings | 6f88a44 | 2010-12-29 14:55:03 +0000 | [diff] [blame] | 25 | #include <linux/capability.h> |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 26 | #include <linux/ratelimit.h> |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 27 | #include <linux/kthread.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 28 | #include <linux/raid/pq.h> |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 29 | #include <linux/semaphore.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 30 | #include <asm/div64.h> |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 31 | #include "ctree.h" |
| 32 | #include "extent_map.h" |
| 33 | #include "disk-io.h" |
| 34 | #include "transaction.h" |
| 35 | #include "print-tree.h" |
| 36 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 37 | #include "raid56.h" |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 38 | #include "async-thread.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 39 | #include "check-integrity.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 40 | #include "rcu-string.h" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame] | 41 | #include "math.h" |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 42 | #include "dev-replace.h" |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 43 | #include "sysfs.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 44 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 45 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
| 46 | struct btrfs_root *root, |
| 47 | struct btrfs_device *device); |
| 48 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 49 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 50 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 51 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 52 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 53 | static DEFINE_MUTEX(uuid_mutex); |
| 54 | static LIST_HEAD(fs_uuids); |
| 55 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 56 | static void lock_chunks(struct btrfs_root *root) |
| 57 | { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 58 | mutex_lock(&root->fs_info->chunk_mutex); |
| 59 | } |
| 60 | |
| 61 | static void unlock_chunks(struct btrfs_root *root) |
| 62 | { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 63 | mutex_unlock(&root->fs_info->chunk_mutex); |
| 64 | } |
| 65 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 66 | static struct btrfs_fs_devices *__alloc_fs_devices(void) |
| 67 | { |
| 68 | struct btrfs_fs_devices *fs_devs; |
| 69 | |
| 70 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS); |
| 71 | if (!fs_devs) |
| 72 | return ERR_PTR(-ENOMEM); |
| 73 | |
| 74 | mutex_init(&fs_devs->device_list_mutex); |
| 75 | |
| 76 | INIT_LIST_HEAD(&fs_devs->devices); |
| 77 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
| 78 | INIT_LIST_HEAD(&fs_devs->list); |
| 79 | |
| 80 | return fs_devs; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
| 85 | * @fsid: a pointer to UUID for this FS. If NULL a new UUID is |
| 86 | * generated. |
| 87 | * |
| 88 | * Return: a pointer to a new &struct btrfs_fs_devices on success; |
| 89 | * ERR_PTR() on error. Returned struct is not linked onto any lists and |
| 90 | * can be destroyed with kfree() right away. |
| 91 | */ |
| 92 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid) |
| 93 | { |
| 94 | struct btrfs_fs_devices *fs_devs; |
| 95 | |
| 96 | fs_devs = __alloc_fs_devices(); |
| 97 | if (IS_ERR(fs_devs)) |
| 98 | return fs_devs; |
| 99 | |
| 100 | if (fsid) |
| 101 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
| 102 | else |
| 103 | generate_random_uuid(fs_devs->fsid); |
| 104 | |
| 105 | return fs_devs; |
| 106 | } |
| 107 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 108 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 109 | { |
| 110 | struct btrfs_device *device; |
| 111 | WARN_ON(fs_devices->opened); |
| 112 | while (!list_empty(&fs_devices->devices)) { |
| 113 | device = list_entry(fs_devices->devices.next, |
| 114 | struct btrfs_device, dev_list); |
| 115 | list_del(&device->dev_list); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 116 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 117 | kfree(device); |
| 118 | } |
| 119 | kfree(fs_devices); |
| 120 | } |
| 121 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 122 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 123 | enum kobject_action action) |
| 124 | { |
| 125 | int ret; |
| 126 | |
| 127 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 128 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 129 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 130 | action, |
| 131 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 132 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 133 | } |
| 134 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 135 | void btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 136 | { |
| 137 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 138 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 139 | while (!list_empty(&fs_uuids)) { |
| 140 | fs_devices = list_entry(fs_uuids.next, |
| 141 | struct btrfs_fs_devices, list); |
| 142 | list_del(&fs_devices->list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 143 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 144 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 147 | static struct btrfs_device *__alloc_device(void) |
| 148 | { |
| 149 | struct btrfs_device *dev; |
| 150 | |
| 151 | dev = kzalloc(sizeof(*dev), GFP_NOFS); |
| 152 | if (!dev) |
| 153 | return ERR_PTR(-ENOMEM); |
| 154 | |
| 155 | INIT_LIST_HEAD(&dev->dev_list); |
| 156 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
| 157 | |
| 158 | spin_lock_init(&dev->io_lock); |
| 159 | |
| 160 | spin_lock_init(&dev->reada_lock); |
| 161 | atomic_set(&dev->reada_in_flight, 0); |
| 162 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT); |
| 163 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT); |
| 164 | |
| 165 | return dev; |
| 166 | } |
| 167 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 168 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
| 169 | u64 devid, u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 170 | { |
| 171 | struct btrfs_device *dev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 172 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 173 | list_for_each_entry(dev, head, dev_list) { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 174 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 175 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 176 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 177 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 178 | } |
| 179 | return NULL; |
| 180 | } |
| 181 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 182 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 183 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 184 | struct btrfs_fs_devices *fs_devices; |
| 185 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 186 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 187 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 188 | return fs_devices; |
| 189 | } |
| 190 | return NULL; |
| 191 | } |
| 192 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 193 | static int |
| 194 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 195 | int flush, struct block_device **bdev, |
| 196 | struct buffer_head **bh) |
| 197 | { |
| 198 | int ret; |
| 199 | |
| 200 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 201 | |
| 202 | if (IS_ERR(*bdev)) { |
| 203 | ret = PTR_ERR(*bdev); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 204 | printk(KERN_INFO "BTRFS: open %s failed\n", device_path); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 205 | goto error; |
| 206 | } |
| 207 | |
| 208 | if (flush) |
| 209 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
| 210 | ret = set_blocksize(*bdev, 4096); |
| 211 | if (ret) { |
| 212 | blkdev_put(*bdev, flags); |
| 213 | goto error; |
| 214 | } |
| 215 | invalidate_bdev(*bdev); |
| 216 | *bh = btrfs_read_dev_super(*bdev); |
| 217 | if (!*bh) { |
| 218 | ret = -EINVAL; |
| 219 | blkdev_put(*bdev, flags); |
| 220 | goto error; |
| 221 | } |
| 222 | |
| 223 | return 0; |
| 224 | |
| 225 | error: |
| 226 | *bdev = NULL; |
| 227 | *bh = NULL; |
| 228 | return ret; |
| 229 | } |
| 230 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 231 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 232 | struct bio *head, struct bio *tail) |
| 233 | { |
| 234 | |
| 235 | struct bio *old_head; |
| 236 | |
| 237 | old_head = pending_bios->head; |
| 238 | pending_bios->head = head; |
| 239 | if (pending_bios->tail) |
| 240 | tail->bi_next = old_head; |
| 241 | else |
| 242 | pending_bios->tail = tail; |
| 243 | } |
| 244 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 245 | /* |
| 246 | * we try to collect pending bios for a device so we don't get a large |
| 247 | * number of procs sending bios down to the same device. This greatly |
| 248 | * improves the schedulers ability to collect and merge the bios. |
| 249 | * |
| 250 | * But, it also turns into a long list of bios to process and that is sure |
| 251 | * to eventually make the worker thread block. The solution here is to |
| 252 | * make some progress and then put this work struct back at the end of |
| 253 | * the list if the block device is congested. This way, multiple devices |
| 254 | * can make progress from a single worker thread. |
| 255 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 256 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 257 | { |
| 258 | struct bio *pending; |
| 259 | struct backing_dev_info *bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 260 | struct btrfs_fs_info *fs_info; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 261 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 262 | struct bio *tail; |
| 263 | struct bio *cur; |
| 264 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 265 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 266 | unsigned long batch_run = 0; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 267 | unsigned long limit; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 268 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 269 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 270 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 271 | struct blk_plug plug; |
| 272 | |
| 273 | /* |
| 274 | * this function runs all the bios we've collected for |
| 275 | * a particular device. We don't want to wander off to |
| 276 | * another device without first sending all of these down. |
| 277 | * So, setup a plug here and finish it off before we return |
| 278 | */ |
| 279 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 280 | |
Chris Mason | bedf762 | 2009-04-03 10:32:58 -0400 | [diff] [blame] | 281 | bdi = blk_get_backing_dev_info(device->bdev); |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 282 | fs_info = device->dev_root->fs_info; |
| 283 | limit = btrfs_async_submit_limit(fs_info); |
| 284 | limit = limit * 2 / 3; |
| 285 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 286 | loop: |
| 287 | spin_lock(&device->io_lock); |
| 288 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 289 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 290 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 291 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 292 | /* take all the bios off the list at once and process them |
| 293 | * later on (without the lock held). But, remember the |
| 294 | * tail and other pointers so the bios can be properly reinserted |
| 295 | * into the list if we hit congestion |
| 296 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 297 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 298 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 299 | force_reg = 1; |
| 300 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 301 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 302 | force_reg = 0; |
| 303 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 304 | |
| 305 | pending = pending_bios->head; |
| 306 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 307 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * if pending was null this time around, no bios need processing |
| 311 | * at all and we can stop. Otherwise it'll loop back up again |
| 312 | * and do an additional check so no bios are missed. |
| 313 | * |
| 314 | * device->running_pending is used to synchronize with the |
| 315 | * schedule_bio code. |
| 316 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 317 | if (device->pending_sync_bios.head == NULL && |
| 318 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 319 | again = 0; |
| 320 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 321 | } else { |
| 322 | again = 1; |
| 323 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 324 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 325 | |
| 326 | pending_bios->head = NULL; |
| 327 | pending_bios->tail = NULL; |
| 328 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 329 | spin_unlock(&device->io_lock); |
| 330 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 331 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 332 | |
| 333 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 334 | /* we want to work on both lists, but do more bios on the |
| 335 | * sync list than the regular list |
| 336 | */ |
| 337 | if ((num_run > 32 && |
| 338 | pending_bios != &device->pending_sync_bios && |
| 339 | device->pending_sync_bios.head) || |
| 340 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 341 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 342 | spin_lock(&device->io_lock); |
| 343 | requeue_list(pending_bios, pending, tail); |
| 344 | goto loop_lock; |
| 345 | } |
| 346 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 347 | cur = pending; |
| 348 | pending = pending->bi_next; |
| 349 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 350 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 351 | if (atomic_dec_return(&fs_info->nr_async_bios) < limit && |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 352 | waitqueue_active(&fs_info->async_submit_wait)) |
| 353 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 354 | |
| 355 | BUG_ON(atomic_read(&cur->bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 356 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 357 | /* |
| 358 | * if we're doing the sync list, record that our |
| 359 | * plug has some sync requests on it |
| 360 | * |
| 361 | * If we're doing the regular list and there are |
| 362 | * sync requests sitting around, unplug before |
| 363 | * we add more |
| 364 | */ |
| 365 | if (pending_bios == &device->pending_sync_bios) { |
| 366 | sync_pending = 1; |
| 367 | } else if (sync_pending) { |
| 368 | blk_finish_plug(&plug); |
| 369 | blk_start_plug(&plug); |
| 370 | sync_pending = 0; |
| 371 | } |
| 372 | |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 373 | btrfsic_submit_bio(cur->bi_rw, cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 374 | num_run++; |
| 375 | batch_run++; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 376 | if (need_resched()) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 377 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 378 | |
| 379 | /* |
| 380 | * we made progress, there is more work to do and the bdi |
| 381 | * is now congested. Back off and let other work structs |
| 382 | * run instead |
| 383 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 384 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 385 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 386 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 387 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 388 | ioc = current->io_context; |
| 389 | |
| 390 | /* |
| 391 | * the main goal here is that we don't want to |
| 392 | * block if we're going to be able to submit |
| 393 | * more requests without blocking. |
| 394 | * |
| 395 | * This code does two great things, it pokes into |
| 396 | * the elevator code from a filesystem _and_ |
| 397 | * it makes assumptions about how batching works. |
| 398 | */ |
| 399 | if (ioc && ioc->nr_batch_requests > 0 && |
| 400 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 401 | (last_waited == 0 || |
| 402 | ioc->last_waited == last_waited)) { |
| 403 | /* |
| 404 | * we want to go through our batch of |
| 405 | * requests and stop. So, we copy out |
| 406 | * the ioc->last_waited time and test |
| 407 | * against it before looping |
| 408 | */ |
| 409 | last_waited = ioc->last_waited; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 410 | if (need_resched()) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 411 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 412 | continue; |
| 413 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 414 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 415 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 416 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 417 | |
| 418 | spin_unlock(&device->io_lock); |
Qu Wenruo | a8c93d4e | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 419 | btrfs_queue_work(fs_info->submit_workers, |
| 420 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 421 | goto done; |
| 422 | } |
Chris Mason | d85c8a6f | 2011-12-15 15:38:41 -0500 | [diff] [blame] | 423 | /* unplug every 64 requests just for good measure */ |
| 424 | if (batch_run % 64 == 0) { |
| 425 | blk_finish_plug(&plug); |
| 426 | blk_start_plug(&plug); |
| 427 | sync_pending = 0; |
| 428 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 429 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 430 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 431 | cond_resched(); |
| 432 | if (again) |
| 433 | goto loop; |
| 434 | |
| 435 | spin_lock(&device->io_lock); |
| 436 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 437 | goto loop_lock; |
| 438 | spin_unlock(&device->io_lock); |
| 439 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 440 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 441 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 442 | } |
| 443 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 444 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 445 | { |
| 446 | struct btrfs_device *device; |
| 447 | |
| 448 | device = container_of(work, struct btrfs_device, work); |
| 449 | run_scheduled_bios(device); |
| 450 | } |
| 451 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 452 | /* |
| 453 | * Add new device to list of registered devices |
| 454 | * |
| 455 | * Returns: |
| 456 | * 1 - first time device is seen |
| 457 | * 0 - device already known |
| 458 | * < 0 - error |
| 459 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 460 | static noinline int device_list_add(const char *path, |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 461 | struct btrfs_super_block *disk_super, |
| 462 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) |
| 463 | { |
| 464 | struct btrfs_device *device; |
| 465 | struct btrfs_fs_devices *fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 466 | struct rcu_string *name; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 467 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 468 | u64 found_transid = btrfs_super_generation(disk_super); |
| 469 | |
| 470 | fs_devices = find_fsid(disk_super->fsid); |
| 471 | if (!fs_devices) { |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 472 | fs_devices = alloc_fs_devices(disk_super->fsid); |
| 473 | if (IS_ERR(fs_devices)) |
| 474 | return PTR_ERR(fs_devices); |
| 475 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 476 | list_add(&fs_devices->list, &fs_uuids); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 477 | fs_devices->latest_devid = devid; |
| 478 | fs_devices->latest_trans = found_transid; |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 479 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 480 | device = NULL; |
| 481 | } else { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 482 | device = __find_device(&fs_devices->devices, devid, |
| 483 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 484 | } |
| 485 | if (!device) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 486 | if (fs_devices->opened) |
| 487 | return -EBUSY; |
| 488 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 489 | device = btrfs_alloc_device(NULL, &devid, |
| 490 | disk_super->dev_item.uuid); |
| 491 | if (IS_ERR(device)) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 492 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 493 | return PTR_ERR(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 494 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 495 | |
| 496 | name = rcu_string_strdup(path, GFP_NOFS); |
| 497 | if (!name) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 498 | kfree(device); |
| 499 | return -ENOMEM; |
| 500 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 501 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 502 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 503 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 504 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 505 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 506 | mutex_unlock(&fs_devices->device_list_mutex); |
| 507 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 508 | ret = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 509 | device->fs_devices = fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 510 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame^] | 511 | /* |
| 512 | * When FS is already mounted. |
| 513 | * 1. If you are here and if the device->name is NULL that |
| 514 | * means this device was missing at time of FS mount. |
| 515 | * 2. If you are here and if the device->name is different |
| 516 | * from 'path' that means either |
| 517 | * a. The same device disappeared and reappeared with |
| 518 | * different name. or |
| 519 | * b. The missing-disk-which-was-replaced, has |
| 520 | * reappeared now. |
| 521 | * |
| 522 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 523 | * and unintentional. |
| 524 | * |
| 525 | * Further in case of 1 and 2a above, the disk at 'path' |
| 526 | * would have missed some transaction when it was away and |
| 527 | * in case of 2a the stale bdev has to be updated as well. |
| 528 | * 2b must not be allowed at all time. |
| 529 | */ |
| 530 | |
| 531 | /* |
| 532 | * As of now don't allow update to btrfs_fs_device through |
| 533 | * the btrfs dev scan cli, after FS has been mounted. |
| 534 | */ |
| 535 | if (fs_devices->opened) |
| 536 | return -EBUSY; |
| 537 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 538 | name = rcu_string_strdup(path, GFP_NOFS); |
TARUISI Hiroaki | 3a0524d | 2010-02-09 06:36:45 +0000 | [diff] [blame] | 539 | if (!name) |
| 540 | return -ENOMEM; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 541 | rcu_string_free(device->name); |
| 542 | rcu_assign_pointer(device->name, name); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 543 | if (device->missing) { |
| 544 | fs_devices->missing_devices--; |
| 545 | device->missing = 0; |
| 546 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | if (found_transid > fs_devices->latest_trans) { |
| 550 | fs_devices->latest_devid = devid; |
| 551 | fs_devices->latest_trans = found_transid; |
| 552 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 553 | *fs_devices_ret = fs_devices; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 554 | |
| 555 | return ret; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 556 | } |
| 557 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 558 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 559 | { |
| 560 | struct btrfs_fs_devices *fs_devices; |
| 561 | struct btrfs_device *device; |
| 562 | struct btrfs_device *orig_dev; |
| 563 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 564 | fs_devices = alloc_fs_devices(orig->fsid); |
| 565 | if (IS_ERR(fs_devices)) |
| 566 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 567 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 568 | fs_devices->latest_devid = orig->latest_devid; |
| 569 | fs_devices->latest_trans = orig->latest_trans; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 570 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 571 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 572 | /* 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] | 573 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 574 | struct rcu_string *name; |
| 575 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 576 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 577 | orig_dev->uuid); |
| 578 | if (IS_ERR(device)) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 579 | goto error; |
| 580 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 581 | /* |
| 582 | * This is ok to do without rcu read locked because we hold the |
| 583 | * uuid mutex so nothing we touch in here is going to disappear. |
| 584 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 585 | if (orig_dev->name) { |
| 586 | name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS); |
| 587 | if (!name) { |
| 588 | kfree(device); |
| 589 | goto error; |
| 590 | } |
| 591 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 592 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 593 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 594 | list_add(&device->dev_list, &fs_devices->devices); |
| 595 | device->fs_devices = fs_devices; |
| 596 | fs_devices->num_devices++; |
| 597 | } |
| 598 | return fs_devices; |
| 599 | error: |
| 600 | free_fs_devices(fs_devices); |
| 601 | return ERR_PTR(-ENOMEM); |
| 602 | } |
| 603 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 604 | void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info, |
| 605 | struct btrfs_fs_devices *fs_devices, int step) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 606 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 607 | struct btrfs_device *device, *next; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 608 | |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 609 | struct block_device *latest_bdev = NULL; |
| 610 | u64 latest_devid = 0; |
| 611 | u64 latest_transid = 0; |
| 612 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 613 | mutex_lock(&uuid_mutex); |
| 614 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 615 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 616 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 617 | if (device->in_fs_metadata) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 618 | if (!device->is_tgtdev_for_dev_replace && |
| 619 | (!latest_transid || |
| 620 | device->generation > latest_transid)) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 621 | latest_devid = device->devid; |
| 622 | latest_transid = device->generation; |
| 623 | latest_bdev = device->bdev; |
| 624 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 625 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 626 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 627 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 628 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 629 | /* |
| 630 | * In the first step, keep the device which has |
| 631 | * the correct fsid and the devid that is used |
| 632 | * for the dev_replace procedure. |
| 633 | * In the second step, the dev_replace state is |
| 634 | * read from the device tree and it is known |
| 635 | * whether the procedure is really active or |
| 636 | * not, which means whether this device is |
| 637 | * used or whether it should be removed. |
| 638 | */ |
| 639 | if (step == 0 || device->is_tgtdev_for_dev_replace) { |
| 640 | continue; |
| 641 | } |
| 642 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 643 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 644 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 645 | device->bdev = NULL; |
| 646 | fs_devices->open_devices--; |
| 647 | } |
| 648 | if (device->writeable) { |
| 649 | list_del_init(&device->dev_alloc_list); |
| 650 | device->writeable = 0; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 651 | if (!device->is_tgtdev_for_dev_replace) |
| 652 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 653 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 654 | list_del_init(&device->dev_list); |
| 655 | fs_devices->num_devices--; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 656 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 657 | kfree(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 658 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 659 | |
| 660 | if (fs_devices->seed) { |
| 661 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 662 | goto again; |
| 663 | } |
| 664 | |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 665 | fs_devices->latest_bdev = latest_bdev; |
| 666 | fs_devices->latest_devid = latest_devid; |
| 667 | fs_devices->latest_trans = latest_transid; |
| 668 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 669 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 670 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 671 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 672 | static void __free_device(struct work_struct *work) |
| 673 | { |
| 674 | struct btrfs_device *device; |
| 675 | |
| 676 | device = container_of(work, struct btrfs_device, rcu_work); |
| 677 | |
| 678 | if (device->bdev) |
| 679 | blkdev_put(device->bdev, device->mode); |
| 680 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 681 | rcu_string_free(device->name); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 682 | kfree(device); |
| 683 | } |
| 684 | |
| 685 | static void free_device(struct rcu_head *head) |
| 686 | { |
| 687 | struct btrfs_device *device; |
| 688 | |
| 689 | device = container_of(head, struct btrfs_device, rcu); |
| 690 | |
| 691 | INIT_WORK(&device->rcu_work, __free_device); |
| 692 | schedule_work(&device->rcu_work); |
| 693 | } |
| 694 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 695 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 696 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 697 | struct btrfs_device *device; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 698 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 699 | if (--fs_devices->opened > 0) |
| 700 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 701 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 702 | mutex_lock(&fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 703 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 704 | struct btrfs_device *new_device; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 705 | struct rcu_string *name; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 706 | |
| 707 | if (device->bdev) |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 708 | fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 709 | |
Ilya Dryomov | f747cab | 2013-10-10 20:37:29 +0300 | [diff] [blame] | 710 | if (device->writeable && |
| 711 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 712 | list_del_init(&device->dev_alloc_list); |
| 713 | fs_devices->rw_devices--; |
| 714 | } |
| 715 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 716 | if (device->can_discard) |
| 717 | fs_devices->num_can_discard--; |
Josef Bacik | 726551e | 2013-08-26 13:45:53 -0400 | [diff] [blame] | 718 | if (device->missing) |
| 719 | fs_devices->missing_devices--; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 720 | |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 721 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 722 | device->uuid); |
| 723 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 724 | |
| 725 | /* Safe because we are under uuid_mutex */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 726 | if (device->name) { |
| 727 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 728 | BUG_ON(!name); /* -ENOMEM */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 729 | rcu_assign_pointer(new_device->name, name); |
| 730 | } |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 731 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 732 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 733 | new_device->fs_devices = device->fs_devices; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 734 | |
| 735 | call_rcu(&device->rcu, free_device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 736 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 737 | mutex_unlock(&fs_devices->device_list_mutex); |
| 738 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 739 | WARN_ON(fs_devices->open_devices); |
| 740 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 741 | fs_devices->opened = 0; |
| 742 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 743 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 744 | return 0; |
| 745 | } |
| 746 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 747 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 748 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 749 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 750 | int ret; |
| 751 | |
| 752 | mutex_lock(&uuid_mutex); |
| 753 | ret = __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 754 | if (!fs_devices->opened) { |
| 755 | seed_devices = fs_devices->seed; |
| 756 | fs_devices->seed = NULL; |
| 757 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 758 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 759 | |
| 760 | while (seed_devices) { |
| 761 | fs_devices = seed_devices; |
| 762 | seed_devices = fs_devices->seed; |
| 763 | __btrfs_close_devices(fs_devices); |
| 764 | free_fs_devices(fs_devices); |
| 765 | } |
Eric Sandeen | bc17862 | 2013-03-09 15:18:39 +0000 | [diff] [blame] | 766 | /* |
| 767 | * Wait for rcu kworkers under __btrfs_close_devices |
| 768 | * to finish all blkdev_puts so device is really |
| 769 | * free when umount is done. |
| 770 | */ |
| 771 | rcu_barrier(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 772 | return ret; |
| 773 | } |
| 774 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 775 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 776 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 777 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 778 | struct request_queue *q; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 779 | struct block_device *bdev; |
| 780 | struct list_head *head = &fs_devices->devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 781 | struct btrfs_device *device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 782 | struct block_device *latest_bdev = NULL; |
| 783 | struct buffer_head *bh; |
| 784 | struct btrfs_super_block *disk_super; |
| 785 | u64 latest_devid = 0; |
| 786 | u64 latest_transid = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 787 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 788 | int seeding = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 789 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 790 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 791 | flags |= FMODE_EXCL; |
| 792 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 793 | list_for_each_entry(device, head, dev_list) { |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 794 | if (device->bdev) |
| 795 | continue; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 796 | if (!device->name) |
| 797 | continue; |
| 798 | |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 799 | /* Just open everything we can; ignore failures here */ |
| 800 | if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 801 | &bdev, &bh)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 802 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 803 | |
| 804 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 805 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 806 | if (devid != device->devid) |
| 807 | goto error_brelse; |
| 808 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 809 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
| 810 | BTRFS_UUID_SIZE)) |
| 811 | goto error_brelse; |
| 812 | |
| 813 | device->generation = btrfs_super_generation(disk_super); |
| 814 | if (!latest_transid || device->generation > latest_transid) { |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 815 | latest_devid = devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 816 | latest_transid = device->generation; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 817 | latest_bdev = bdev; |
| 818 | } |
| 819 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 820 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| 821 | device->writeable = 0; |
| 822 | } else { |
| 823 | device->writeable = !bdev_read_only(bdev); |
| 824 | seeding = 0; |
| 825 | } |
| 826 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 827 | q = bdev_get_queue(bdev); |
| 828 | if (blk_queue_discard(q)) { |
| 829 | device->can_discard = 1; |
| 830 | fs_devices->num_can_discard++; |
| 831 | } |
| 832 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 833 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 834 | device->in_fs_metadata = 0; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 835 | device->mode = flags; |
| 836 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 837 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 838 | fs_devices->rotating = 1; |
| 839 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 840 | fs_devices->open_devices++; |
Ilya Dryomov | 55e50e4 | 2013-09-01 18:56:44 +0300 | [diff] [blame] | 841 | if (device->writeable && |
| 842 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 843 | fs_devices->rw_devices++; |
| 844 | list_add(&device->dev_alloc_list, |
| 845 | &fs_devices->alloc_list); |
| 846 | } |
Xiao Guangrong | 4f6c932 | 2011-04-20 10:06:40 +0000 | [diff] [blame] | 847 | brelse(bh); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 848 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 849 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 850 | error_brelse: |
| 851 | brelse(bh); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 852 | blkdev_put(bdev, flags); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 853 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 854 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 855 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 856 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 857 | goto out; |
| 858 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 859 | fs_devices->seeding = seeding; |
| 860 | fs_devices->opened = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 861 | fs_devices->latest_bdev = latest_bdev; |
| 862 | fs_devices->latest_devid = latest_devid; |
| 863 | fs_devices->latest_trans = latest_transid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 864 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 865 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 870 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 871 | { |
| 872 | int ret; |
| 873 | |
| 874 | mutex_lock(&uuid_mutex); |
| 875 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 876 | fs_devices->opened++; |
| 877 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 878 | } else { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 879 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 880 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 881 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 882 | return ret; |
| 883 | } |
| 884 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 885 | /* |
| 886 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 887 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 888 | * is read via pagecache |
| 889 | */ |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 890 | 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] | 891 | struct btrfs_fs_devices **fs_devices_ret) |
| 892 | { |
| 893 | struct btrfs_super_block *disk_super; |
| 894 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 895 | struct page *page; |
| 896 | void *p; |
| 897 | int ret = -EINVAL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 898 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 899 | u64 transid; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 900 | u64 total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 901 | u64 bytenr; |
| 902 | pgoff_t index; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 903 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 904 | /* |
| 905 | * we would like to check all the supers, but that would make |
| 906 | * a btrfs mount succeed after a mkfs from a different FS. |
| 907 | * So, we need to add a special mount option to scan for |
| 908 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 909 | */ |
| 910 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 911 | flags |= FMODE_EXCL; |
Al Viro | 10f6327 | 2011-11-17 15:05:22 -0500 | [diff] [blame] | 912 | mutex_lock(&uuid_mutex); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 913 | |
| 914 | bdev = blkdev_get_by_path(path, flags, holder); |
| 915 | |
| 916 | if (IS_ERR(bdev)) { |
| 917 | ret = PTR_ERR(bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 918 | goto error; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | /* make sure our super fits in the device */ |
| 922 | if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode)) |
| 923 | goto error_bdev_put; |
| 924 | |
| 925 | /* make sure our super fits in the page */ |
| 926 | if (sizeof(*disk_super) > PAGE_CACHE_SIZE) |
| 927 | goto error_bdev_put; |
| 928 | |
| 929 | /* make sure our super doesn't straddle pages on disk */ |
| 930 | index = bytenr >> PAGE_CACHE_SHIFT; |
| 931 | if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index) |
| 932 | goto error_bdev_put; |
| 933 | |
| 934 | /* pull in the page with our super */ |
| 935 | page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 936 | index, GFP_NOFS); |
| 937 | |
| 938 | if (IS_ERR_OR_NULL(page)) |
| 939 | goto error_bdev_put; |
| 940 | |
| 941 | p = kmap(page); |
| 942 | |
| 943 | /* align our pointer to the offset of the super block */ |
| 944 | disk_super = p + (bytenr & ~PAGE_CACHE_MASK); |
| 945 | |
| 946 | if (btrfs_super_bytenr(disk_super) != bytenr || |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 947 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 948 | goto error_unmap; |
| 949 | |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 950 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 951 | transid = btrfs_super_generation(disk_super); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 952 | total_devices = btrfs_super_num_devices(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 953 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 954 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 955 | if (ret > 0) { |
| 956 | if (disk_super->label[0]) { |
| 957 | if (disk_super->label[BTRFS_LABEL_SIZE - 1]) |
| 958 | disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 959 | printk(KERN_INFO "BTRFS: device label %s ", disk_super->label); |
| 960 | } else { |
| 961 | printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid); |
| 962 | } |
| 963 | |
| 964 | printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path); |
| 965 | ret = 0; |
| 966 | } |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 967 | if (!ret && fs_devices_ret) |
| 968 | (*fs_devices_ret)->total_devices = total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 969 | |
| 970 | error_unmap: |
| 971 | kunmap(page); |
| 972 | page_cache_release(page); |
| 973 | |
| 974 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 975 | blkdev_put(bdev, flags); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 976 | error: |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 977 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 978 | return ret; |
| 979 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 980 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 981 | /* helper to account the used device space in the range */ |
| 982 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, |
| 983 | u64 end, u64 *length) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 984 | { |
| 985 | struct btrfs_key key; |
| 986 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 987 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 988 | struct btrfs_path *path; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 989 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 990 | int ret; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 991 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 992 | struct extent_buffer *l; |
| 993 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 994 | *length = 0; |
| 995 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 996 | if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 997 | return 0; |
| 998 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 999 | path = btrfs_alloc_path(); |
| 1000 | if (!path) |
| 1001 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1002 | path->reada = 2; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1003 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1004 | key.objectid = device->devid; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1005 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1006 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1007 | |
| 1008 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1009 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1010 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1011 | if (ret > 0) { |
| 1012 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1013 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1014 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1015 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1016 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1017 | while (1) { |
| 1018 | l = path->nodes[0]; |
| 1019 | slot = path->slots[0]; |
| 1020 | if (slot >= btrfs_header_nritems(l)) { |
| 1021 | ret = btrfs_next_leaf(root, path); |
| 1022 | if (ret == 0) |
| 1023 | continue; |
| 1024 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1025 | goto out; |
| 1026 | |
| 1027 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1028 | } |
| 1029 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1030 | |
| 1031 | if (key.objectid < device->devid) |
| 1032 | goto next; |
| 1033 | |
| 1034 | if (key.objectid > device->devid) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1035 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1036 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1037 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1038 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1039 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1040 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1041 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1042 | dev_extent); |
| 1043 | if (key.offset <= start && extent_end > end) { |
| 1044 | *length = end - start + 1; |
| 1045 | break; |
| 1046 | } else if (key.offset <= start && extent_end > start) |
| 1047 | *length += extent_end - start; |
| 1048 | else if (key.offset > start && extent_end <= end) |
| 1049 | *length += extent_end - key.offset; |
| 1050 | else if (key.offset > start && key.offset <= end) { |
| 1051 | *length += end - key.offset + 1; |
| 1052 | break; |
| 1053 | } else if (key.offset > end) |
| 1054 | break; |
| 1055 | |
| 1056 | next: |
| 1057 | path->slots[0]++; |
| 1058 | } |
| 1059 | ret = 0; |
| 1060 | out: |
| 1061 | btrfs_free_path(path); |
| 1062 | return ret; |
| 1063 | } |
| 1064 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1065 | static int contains_pending_extent(struct btrfs_trans_handle *trans, |
| 1066 | struct btrfs_device *device, |
| 1067 | u64 *start, u64 len) |
| 1068 | { |
| 1069 | struct extent_map *em; |
| 1070 | int ret = 0; |
| 1071 | |
| 1072 | list_for_each_entry(em, &trans->transaction->pending_chunks, list) { |
| 1073 | struct map_lookup *map; |
| 1074 | int i; |
| 1075 | |
| 1076 | map = (struct map_lookup *)em->bdev; |
| 1077 | for (i = 0; i < map->num_stripes; i++) { |
| 1078 | if (map->stripes[i].dev != device) |
| 1079 | continue; |
| 1080 | if (map->stripes[i].physical >= *start + len || |
| 1081 | map->stripes[i].physical + em->orig_block_len <= |
| 1082 | *start) |
| 1083 | continue; |
| 1084 | *start = map->stripes[i].physical + |
| 1085 | em->orig_block_len; |
| 1086 | ret = 1; |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | return ret; |
| 1091 | } |
| 1092 | |
| 1093 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1094 | /* |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1095 | * find_free_dev_extent - find free space in the specified device |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1096 | * @device: the device which we search the free space in |
| 1097 | * @num_bytes: the size of the free space that we need |
| 1098 | * @start: store the start of the free space. |
| 1099 | * @len: the size of the free space. that we find, or the size of the max |
| 1100 | * free space if we don't find suitable free space |
| 1101 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1102 | * this uses a pretty simple search, the expectation is that it is |
| 1103 | * called very infrequently and that a given device has a small number |
| 1104 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1105 | * |
| 1106 | * @start is used to store the start of the free space if we find. But if we |
| 1107 | * don't find suitable free space, it will be used to store the start position |
| 1108 | * of the max free space. |
| 1109 | * |
| 1110 | * @len is used to store the size of the free space that we find. |
| 1111 | * But if we don't find suitable free space, it is used to store the size of |
| 1112 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1113 | */ |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1114 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 1115 | struct btrfs_device *device, u64 num_bytes, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1116 | u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1117 | { |
| 1118 | struct btrfs_key key; |
| 1119 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1120 | struct btrfs_dev_extent *dev_extent; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1121 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1122 | u64 hole_size; |
| 1123 | u64 max_hole_start; |
| 1124 | u64 max_hole_size; |
| 1125 | u64 extent_end; |
| 1126 | u64 search_start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1127 | u64 search_end = device->total_bytes; |
| 1128 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1129 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1130 | struct extent_buffer *l; |
| 1131 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1132 | /* FIXME use last free of some kind */ |
| 1133 | |
| 1134 | /* we don't want to overwrite the superblock on the drive, |
| 1135 | * so we make sure to start at an offset of at least 1MB |
| 1136 | */ |
Arne Jansen | a9c9bf6 | 2011-04-12 11:01:20 +0200 | [diff] [blame] | 1137 | search_start = max(root->fs_info->alloc_start, 1024ull * 1024); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1138 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1139 | path = btrfs_alloc_path(); |
| 1140 | if (!path) |
| 1141 | return -ENOMEM; |
| 1142 | again: |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1143 | max_hole_start = search_start; |
| 1144 | max_hole_size = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1145 | hole_size = 0; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1146 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1147 | if (search_start >= search_end || device->is_tgtdev_for_dev_replace) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1148 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1149 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1152 | path->reada = 2; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1153 | path->search_commit_root = 1; |
| 1154 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1155 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1156 | key.objectid = device->devid; |
| 1157 | key.offset = search_start; |
| 1158 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1159 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1160 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1161 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1162 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1163 | if (ret > 0) { |
| 1164 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1165 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1166 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1167 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1168 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1169 | while (1) { |
| 1170 | l = path->nodes[0]; |
| 1171 | slot = path->slots[0]; |
| 1172 | if (slot >= btrfs_header_nritems(l)) { |
| 1173 | ret = btrfs_next_leaf(root, path); |
| 1174 | if (ret == 0) |
| 1175 | continue; |
| 1176 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1177 | goto out; |
| 1178 | |
| 1179 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1180 | } |
| 1181 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1182 | |
| 1183 | if (key.objectid < device->devid) |
| 1184 | goto next; |
| 1185 | |
| 1186 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1187 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1188 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1189 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) |
| 1190 | goto next; |
| 1191 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1192 | if (key.offset > search_start) { |
| 1193 | hole_size = key.offset - search_start; |
| 1194 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1195 | /* |
| 1196 | * Have to check before we set max_hole_start, otherwise |
| 1197 | * we could end up sending back this offset anyway. |
| 1198 | */ |
| 1199 | if (contains_pending_extent(trans, device, |
| 1200 | &search_start, |
| 1201 | hole_size)) |
| 1202 | hole_size = 0; |
| 1203 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1204 | if (hole_size > max_hole_size) { |
| 1205 | max_hole_start = search_start; |
| 1206 | max_hole_size = hole_size; |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * If this free space is greater than which we need, |
| 1211 | * it must be the max free space that we have found |
| 1212 | * until now, so max_hole_start must point to the start |
| 1213 | * of this free space and the length of this free space |
| 1214 | * is stored in max_hole_size. Thus, we return |
| 1215 | * max_hole_start and max_hole_size and go back to the |
| 1216 | * caller. |
| 1217 | */ |
| 1218 | if (hole_size >= num_bytes) { |
| 1219 | ret = 0; |
| 1220 | goto out; |
| 1221 | } |
| 1222 | } |
| 1223 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1224 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1225 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1226 | dev_extent); |
| 1227 | if (extent_end > search_start) |
| 1228 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1229 | next: |
| 1230 | path->slots[0]++; |
| 1231 | cond_resched(); |
| 1232 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1233 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1234 | /* |
| 1235 | * At this point, search_start should be the end of |
| 1236 | * allocated dev extents, and when shrinking the device, |
| 1237 | * search_end may be smaller than search_start. |
| 1238 | */ |
| 1239 | if (search_end > search_start) |
| 1240 | hole_size = search_end - search_start; |
| 1241 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1242 | if (hole_size > max_hole_size) { |
| 1243 | max_hole_start = search_start; |
| 1244 | max_hole_size = hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1245 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1246 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1247 | if (contains_pending_extent(trans, device, &search_start, hole_size)) { |
| 1248 | btrfs_release_path(path); |
| 1249 | goto again; |
| 1250 | } |
| 1251 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1252 | /* See above. */ |
| 1253 | if (hole_size < num_bytes) |
| 1254 | ret = -ENOSPC; |
| 1255 | else |
| 1256 | ret = 0; |
| 1257 | |
| 1258 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1259 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1260 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1261 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1262 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1263 | return ret; |
| 1264 | } |
| 1265 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1266 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1267 | struct btrfs_device *device, |
| 1268 | u64 start) |
| 1269 | { |
| 1270 | int ret; |
| 1271 | struct btrfs_path *path; |
| 1272 | struct btrfs_root *root = device->dev_root; |
| 1273 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1274 | struct btrfs_key found_key; |
| 1275 | struct extent_buffer *leaf = NULL; |
| 1276 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1277 | |
| 1278 | path = btrfs_alloc_path(); |
| 1279 | if (!path) |
| 1280 | return -ENOMEM; |
| 1281 | |
| 1282 | key.objectid = device->devid; |
| 1283 | key.offset = start; |
| 1284 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1285 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1286 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1287 | if (ret > 0) { |
| 1288 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1289 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1290 | if (ret) |
| 1291 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1292 | leaf = path->nodes[0]; |
| 1293 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1294 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1295 | struct btrfs_dev_extent); |
| 1296 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1297 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1298 | key = found_key; |
| 1299 | btrfs_release_path(path); |
| 1300 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1301 | } else if (ret == 0) { |
| 1302 | leaf = path->nodes[0]; |
| 1303 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1304 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1305 | } else { |
| 1306 | btrfs_error(root->fs_info, ret, "Slot search failed"); |
| 1307 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1308 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1309 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 1310 | if (device->bytes_used > 0) { |
| 1311 | u64 len = btrfs_dev_extent_length(leaf, extent); |
| 1312 | device->bytes_used -= len; |
| 1313 | spin_lock(&root->fs_info->free_chunk_lock); |
| 1314 | root->fs_info->free_chunk_space += len; |
| 1315 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 1316 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1317 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1318 | if (ret) { |
| 1319 | btrfs_error(root->fs_info, ret, |
| 1320 | "Failed to remove dev extent item"); |
| 1321 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1322 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1323 | btrfs_free_path(path); |
| 1324 | return ret; |
| 1325 | } |
| 1326 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1327 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1328 | struct btrfs_device *device, |
| 1329 | u64 chunk_tree, u64 chunk_objectid, |
| 1330 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1331 | { |
| 1332 | int ret; |
| 1333 | struct btrfs_path *path; |
| 1334 | struct btrfs_root *root = device->dev_root; |
| 1335 | struct btrfs_dev_extent *extent; |
| 1336 | struct extent_buffer *leaf; |
| 1337 | struct btrfs_key key; |
| 1338 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1339 | WARN_ON(!device->in_fs_metadata); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1340 | WARN_ON(device->is_tgtdev_for_dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1341 | path = btrfs_alloc_path(); |
| 1342 | if (!path) |
| 1343 | return -ENOMEM; |
| 1344 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1345 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1346 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1347 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1348 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1349 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1350 | if (ret) |
| 1351 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1352 | |
| 1353 | leaf = path->nodes[0]; |
| 1354 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1355 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1356 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 1357 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 1358 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1359 | |
| 1360 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | 231e88f | 2013-08-20 13:20:13 +0200 | [diff] [blame] | 1361 | btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1362 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1363 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1364 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1365 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1366 | btrfs_free_path(path); |
| 1367 | return ret; |
| 1368 | } |
| 1369 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1370 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1371 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1372 | struct extent_map_tree *em_tree; |
| 1373 | struct extent_map *em; |
| 1374 | struct rb_node *n; |
| 1375 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1376 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1377 | em_tree = &fs_info->mapping_tree.map_tree; |
| 1378 | read_lock(&em_tree->lock); |
| 1379 | n = rb_last(&em_tree->map); |
| 1380 | if (n) { |
| 1381 | em = rb_entry(n, struct extent_map, rb_node); |
| 1382 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1383 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1384 | read_unlock(&em_tree->lock); |
| 1385 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1386 | return ret; |
| 1387 | } |
| 1388 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1389 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1390 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1391 | { |
| 1392 | int ret; |
| 1393 | struct btrfs_key key; |
| 1394 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1395 | struct btrfs_path *path; |
| 1396 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1397 | path = btrfs_alloc_path(); |
| 1398 | if (!path) |
| 1399 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1400 | |
| 1401 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1402 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1403 | key.offset = (u64)-1; |
| 1404 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1405 | ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1406 | if (ret < 0) |
| 1407 | goto error; |
| 1408 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1409 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1410 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1411 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1412 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1413 | BTRFS_DEV_ITEM_KEY); |
| 1414 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1415 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1416 | } else { |
| 1417 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1418 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1419 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1420 | } |
| 1421 | ret = 0; |
| 1422 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1423 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1424 | return ret; |
| 1425 | } |
| 1426 | |
| 1427 | /* |
| 1428 | * the device information is stored in the chunk root |
| 1429 | * the btrfs_device struct should be fully filled in |
| 1430 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1431 | static int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 1432 | struct btrfs_root *root, |
| 1433 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1434 | { |
| 1435 | int ret; |
| 1436 | struct btrfs_path *path; |
| 1437 | struct btrfs_dev_item *dev_item; |
| 1438 | struct extent_buffer *leaf; |
| 1439 | struct btrfs_key key; |
| 1440 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1441 | |
| 1442 | root = root->fs_info->chunk_root; |
| 1443 | |
| 1444 | path = btrfs_alloc_path(); |
| 1445 | if (!path) |
| 1446 | return -ENOMEM; |
| 1447 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1448 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1449 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1450 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1451 | |
| 1452 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1453 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1454 | if (ret) |
| 1455 | goto out; |
| 1456 | |
| 1457 | leaf = path->nodes[0]; |
| 1458 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1459 | |
| 1460 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1461 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1462 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1463 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1464 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1465 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1466 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
| 1467 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1468 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1469 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1470 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1471 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1472 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1473 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1474 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1475 | ptr = btrfs_device_fsid(dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1476 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1477 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1478 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1479 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1480 | out: |
| 1481 | btrfs_free_path(path); |
| 1482 | return ret; |
| 1483 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1484 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1485 | /* |
| 1486 | * Function to update ctime/mtime for a given device path. |
| 1487 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1488 | */ |
| 1489 | static void update_dev_time(char *path_name) |
| 1490 | { |
| 1491 | struct file *filp; |
| 1492 | |
| 1493 | filp = filp_open(path_name, O_RDWR, 0); |
| 1494 | if (!filp) |
| 1495 | return; |
| 1496 | file_update_time(filp); |
| 1497 | filp_close(filp, NULL); |
| 1498 | return; |
| 1499 | } |
| 1500 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1501 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 1502 | struct btrfs_device *device) |
| 1503 | { |
| 1504 | int ret; |
| 1505 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1506 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1507 | struct btrfs_trans_handle *trans; |
| 1508 | |
| 1509 | root = root->fs_info->chunk_root; |
| 1510 | |
| 1511 | path = btrfs_alloc_path(); |
| 1512 | if (!path) |
| 1513 | return -ENOMEM; |
| 1514 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1515 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1516 | if (IS_ERR(trans)) { |
| 1517 | btrfs_free_path(path); |
| 1518 | return PTR_ERR(trans); |
| 1519 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1520 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1521 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1522 | key.offset = device->devid; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1523 | lock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1524 | |
| 1525 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1526 | if (ret < 0) |
| 1527 | goto out; |
| 1528 | |
| 1529 | if (ret > 0) { |
| 1530 | ret = -ENOENT; |
| 1531 | goto out; |
| 1532 | } |
| 1533 | |
| 1534 | ret = btrfs_del_item(trans, root, path); |
| 1535 | if (ret) |
| 1536 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1537 | out: |
| 1538 | btrfs_free_path(path); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1539 | unlock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1540 | btrfs_commit_transaction(trans, root); |
| 1541 | return ret; |
| 1542 | } |
| 1543 | |
| 1544 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 1545 | { |
| 1546 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1547 | struct btrfs_device *next_device; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1548 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1549 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1550 | struct btrfs_super_block *disk_super; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1551 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1552 | u64 all_avail; |
| 1553 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1554 | u64 num_devices; |
| 1555 | u8 *dev_uuid; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1556 | unsigned seq; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1557 | int ret = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1558 | bool clear_super = false; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1559 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1560 | mutex_lock(&uuid_mutex); |
| 1561 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1562 | do { |
| 1563 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 1564 | |
| 1565 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 1566 | root->fs_info->avail_system_alloc_bits | |
| 1567 | root->fs_info->avail_metadata_alloc_bits; |
| 1568 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1569 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1570 | num_devices = root->fs_info->fs_devices->num_devices; |
| 1571 | btrfs_dev_replace_lock(&root->fs_info->dev_replace); |
| 1572 | if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) { |
| 1573 | WARN_ON(num_devices < 1); |
| 1574 | num_devices--; |
| 1575 | } |
| 1576 | btrfs_dev_replace_unlock(&root->fs_info->dev_replace); |
| 1577 | |
| 1578 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1579 | ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1580 | goto out; |
| 1581 | } |
| 1582 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1583 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1584 | ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1585 | goto out; |
| 1586 | } |
| 1587 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1588 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) && |
| 1589 | root->fs_info->fs_devices->rw_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1590 | ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1591 | goto out; |
| 1592 | } |
| 1593 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) && |
| 1594 | root->fs_info->fs_devices->rw_devices <= 3) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1595 | ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1596 | goto out; |
| 1597 | } |
| 1598 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1599 | if (strcmp(device_path, "missing") == 0) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1600 | struct list_head *devices; |
| 1601 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1602 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1603 | device = NULL; |
| 1604 | devices = &root->fs_info->fs_devices->devices; |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1605 | /* |
| 1606 | * It is safe to read the devices since the volume_mutex |
| 1607 | * is held. |
| 1608 | */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1609 | list_for_each_entry(tmp, devices, dev_list) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1610 | if (tmp->in_fs_metadata && |
| 1611 | !tmp->is_tgtdev_for_dev_replace && |
| 1612 | !tmp->bdev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1613 | device = tmp; |
| 1614 | break; |
| 1615 | } |
| 1616 | } |
| 1617 | bdev = NULL; |
| 1618 | bh = NULL; |
| 1619 | disk_super = NULL; |
| 1620 | if (!device) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1621 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1622 | goto out; |
| 1623 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1624 | } else { |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1625 | ret = btrfs_get_bdev_and_sb(device_path, |
Lukas Czerner | cc975eb | 2012-12-07 10:09:19 +0000 | [diff] [blame] | 1626 | FMODE_WRITE | FMODE_EXCL, |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1627 | root->fs_info->bdev_holder, 0, |
| 1628 | &bdev, &bh); |
| 1629 | if (ret) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1630 | goto out; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1631 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1632 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1633 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1634 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1635 | disk_super->fsid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1636 | if (!device) { |
| 1637 | ret = -ENOENT; |
| 1638 | goto error_brelse; |
| 1639 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1640 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1641 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1642 | if (device->is_tgtdev_for_dev_replace) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1643 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1644 | goto error_brelse; |
| 1645 | } |
| 1646 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1647 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1648 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1649 | goto error_brelse; |
| 1650 | } |
| 1651 | |
| 1652 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1653 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1654 | list_del_init(&device->dev_alloc_list); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1655 | unlock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1656 | root->fs_info->fs_devices->rw_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1657 | clear_super = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1658 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1659 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1660 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1661 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1662 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1663 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1664 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1665 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1666 | /* |
| 1667 | * TODO: the superblock still includes this device in its num_devices |
| 1668 | * counter although write_all_supers() is not locked out. This |
| 1669 | * could give a filesystem state which requires a degraded mount. |
| 1670 | */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1671 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1672 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1673 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1674 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 1675 | spin_lock(&root->fs_info->free_chunk_lock); |
| 1676 | root->fs_info->free_chunk_space = device->total_bytes - |
| 1677 | device->bytes_used; |
| 1678 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 1679 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1680 | device->in_fs_metadata = 0; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1681 | btrfs_scrub_cancel_dev(root->fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1682 | |
| 1683 | /* |
| 1684 | * the device list mutex makes sure that we don't change |
| 1685 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1686 | * the device supers. Whoever is writing all supers, should |
| 1687 | * lock the device list mutex before getting the number of |
| 1688 | * devices in the super block (super_copy). Conversely, |
| 1689 | * whoever updates the number of devices in the super block |
| 1690 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1691 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1692 | |
| 1693 | cur_devices = device->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1694 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1695 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1696 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1697 | device->fs_devices->num_devices--; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1698 | device->fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1699 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1700 | if (device->missing) |
| 1701 | root->fs_info->fs_devices->missing_devices--; |
| 1702 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1703 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1704 | struct btrfs_device, dev_list); |
| 1705 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1706 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1707 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1708 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1709 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1710 | if (device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1711 | device->fs_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1712 | /* remove sysfs entry */ |
| 1713 | btrfs_kobj_rm_device(root->fs_info, device); |
| 1714 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1715 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1716 | call_rcu(&device->rcu, free_device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1717 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1718 | num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; |
| 1719 | btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices); |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1720 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1721 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1722 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1723 | struct btrfs_fs_devices *fs_devices; |
| 1724 | fs_devices = root->fs_info->fs_devices; |
| 1725 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1726 | if (fs_devices->seed == cur_devices) { |
| 1727 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1728 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1729 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1730 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1731 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1732 | cur_devices->seed = NULL; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1733 | lock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1734 | __btrfs_close_devices(cur_devices); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1735 | unlock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1736 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1737 | } |
| 1738 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1739 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1740 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
| 1741 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1742 | /* |
| 1743 | * at this point, the device is zero sized. We want to |
| 1744 | * remove it from the devices list and zero out the old super |
| 1745 | */ |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1746 | if (clear_super && disk_super) { |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1747 | u64 bytenr; |
| 1748 | int i; |
| 1749 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1750 | /* make sure this device isn't detected as part of |
| 1751 | * the FS anymore |
| 1752 | */ |
| 1753 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1754 | set_buffer_dirty(bh); |
| 1755 | sync_dirty_buffer(bh); |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1756 | |
| 1757 | /* clear the mirror copies of super block on the disk |
| 1758 | * being removed, 0th copy is been taken care above and |
| 1759 | * the below would take of the rest |
| 1760 | */ |
| 1761 | for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 1762 | bytenr = btrfs_sb_offset(i); |
| 1763 | if (bytenr + BTRFS_SUPER_INFO_SIZE >= |
| 1764 | i_size_read(bdev->bd_inode)) |
| 1765 | break; |
| 1766 | |
| 1767 | brelse(bh); |
| 1768 | bh = __bread(bdev, bytenr / 4096, |
| 1769 | BTRFS_SUPER_INFO_SIZE); |
| 1770 | if (!bh) |
| 1771 | continue; |
| 1772 | |
| 1773 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1774 | |
| 1775 | if (btrfs_super_bytenr(disk_super) != bytenr || |
| 1776 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) { |
| 1777 | continue; |
| 1778 | } |
| 1779 | memset(&disk_super->magic, 0, |
| 1780 | sizeof(disk_super->magic)); |
| 1781 | set_buffer_dirty(bh); |
| 1782 | sync_dirty_buffer(bh); |
| 1783 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1784 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1785 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1786 | ret = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1787 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1788 | if (bdev) { |
| 1789 | /* Notify udev that device has changed */ |
Eric Sandeen | 3c91160 | 2013-01-31 00:55:02 +0000 | [diff] [blame] | 1790 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 1791 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1792 | /* Update ctime/mtime for device path for libblkid */ |
| 1793 | update_dev_time(device_path); |
| 1794 | } |
| 1795 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1796 | error_brelse: |
| 1797 | brelse(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1798 | if (bdev) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1799 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1800 | out: |
| 1801 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1802 | return ret; |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1803 | error_undo: |
| 1804 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1805 | lock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1806 | list_add(&device->dev_alloc_list, |
| 1807 | &root->fs_info->fs_devices->alloc_list); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1808 | unlock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1809 | root->fs_info->fs_devices->rw_devices++; |
| 1810 | } |
| 1811 | goto error_brelse; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1812 | } |
| 1813 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1814 | void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info, |
| 1815 | struct btrfs_device *srcdev) |
| 1816 | { |
| 1817 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1818 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1819 | list_del_rcu(&srcdev->dev_list); |
| 1820 | list_del_rcu(&srcdev->dev_alloc_list); |
| 1821 | fs_info->fs_devices->num_devices--; |
| 1822 | if (srcdev->missing) { |
| 1823 | fs_info->fs_devices->missing_devices--; |
| 1824 | fs_info->fs_devices->rw_devices++; |
| 1825 | } |
| 1826 | if (srcdev->can_discard) |
| 1827 | fs_info->fs_devices->num_can_discard--; |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1828 | if (srcdev->bdev) { |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1829 | fs_info->fs_devices->open_devices--; |
| 1830 | |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1831 | /* zero out the old super */ |
| 1832 | btrfs_scratch_superblock(srcdev); |
| 1833 | } |
| 1834 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1835 | call_rcu(&srcdev->rcu, free_device); |
| 1836 | } |
| 1837 | |
| 1838 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 1839 | struct btrfs_device *tgtdev) |
| 1840 | { |
| 1841 | struct btrfs_device *next_device; |
| 1842 | |
| 1843 | WARN_ON(!tgtdev); |
| 1844 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 1845 | if (tgtdev->bdev) { |
| 1846 | btrfs_scratch_superblock(tgtdev); |
| 1847 | fs_info->fs_devices->open_devices--; |
| 1848 | } |
| 1849 | fs_info->fs_devices->num_devices--; |
| 1850 | if (tgtdev->can_discard) |
| 1851 | fs_info->fs_devices->num_can_discard++; |
| 1852 | |
| 1853 | next_device = list_entry(fs_info->fs_devices->devices.next, |
| 1854 | struct btrfs_device, dev_list); |
| 1855 | if (tgtdev->bdev == fs_info->sb->s_bdev) |
| 1856 | fs_info->sb->s_bdev = next_device->bdev; |
| 1857 | if (tgtdev->bdev == fs_info->fs_devices->latest_bdev) |
| 1858 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1859 | list_del_rcu(&tgtdev->dev_list); |
| 1860 | |
| 1861 | call_rcu(&tgtdev->rcu, free_device); |
| 1862 | |
| 1863 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
| 1864 | } |
| 1865 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1866 | static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
| 1867 | struct btrfs_device **device) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1868 | { |
| 1869 | int ret = 0; |
| 1870 | struct btrfs_super_block *disk_super; |
| 1871 | u64 devid; |
| 1872 | u8 *dev_uuid; |
| 1873 | struct block_device *bdev; |
| 1874 | struct buffer_head *bh; |
| 1875 | |
| 1876 | *device = NULL; |
| 1877 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
| 1878 | root->fs_info->bdev_holder, 0, &bdev, &bh); |
| 1879 | if (ret) |
| 1880 | return ret; |
| 1881 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1882 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 1883 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1884 | *device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1885 | disk_super->fsid); |
| 1886 | brelse(bh); |
| 1887 | if (!*device) |
| 1888 | ret = -ENOENT; |
| 1889 | blkdev_put(bdev, FMODE_READ); |
| 1890 | return ret; |
| 1891 | } |
| 1892 | |
| 1893 | int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, |
| 1894 | char *device_path, |
| 1895 | struct btrfs_device **device) |
| 1896 | { |
| 1897 | *device = NULL; |
| 1898 | if (strcmp(device_path, "missing") == 0) { |
| 1899 | struct list_head *devices; |
| 1900 | struct btrfs_device *tmp; |
| 1901 | |
| 1902 | devices = &root->fs_info->fs_devices->devices; |
| 1903 | /* |
| 1904 | * It is safe to read the devices since the volume_mutex |
| 1905 | * is held by the caller. |
| 1906 | */ |
| 1907 | list_for_each_entry(tmp, devices, dev_list) { |
| 1908 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 1909 | *device = tmp; |
| 1910 | break; |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | if (!*device) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1915 | btrfs_err(root->fs_info, "no missing device found"); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1916 | return -ENOENT; |
| 1917 | } |
| 1918 | |
| 1919 | return 0; |
| 1920 | } else { |
| 1921 | return btrfs_find_device_by_path(root, device_path, device); |
| 1922 | } |
| 1923 | } |
| 1924 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1925 | /* |
| 1926 | * does all the dirty work required for changing file system's UUID. |
| 1927 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1928 | static int btrfs_prepare_sprout(struct btrfs_root *root) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1929 | { |
| 1930 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 1931 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1932 | struct btrfs_fs_devices *seed_devices; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1933 | struct btrfs_super_block *disk_super = root->fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1934 | struct btrfs_device *device; |
| 1935 | u64 super_flags; |
| 1936 | |
| 1937 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1938 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1939 | return -EINVAL; |
| 1940 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 1941 | seed_devices = __alloc_fs_devices(); |
| 1942 | if (IS_ERR(seed_devices)) |
| 1943 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1944 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1945 | old_devices = clone_fs_devices(fs_devices); |
| 1946 | if (IS_ERR(old_devices)) { |
| 1947 | kfree(seed_devices); |
| 1948 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1949 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1950 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1951 | list_add(&old_devices->list, &fs_uuids); |
| 1952 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1953 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 1954 | seed_devices->opened = 1; |
| 1955 | INIT_LIST_HEAD(&seed_devices->devices); |
| 1956 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1957 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1958 | |
| 1959 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1960 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 1961 | synchronize_rcu); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1962 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1963 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
| 1964 | list_for_each_entry(device, &seed_devices->devices, dev_list) { |
| 1965 | device->fs_devices = seed_devices; |
| 1966 | } |
| 1967 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1968 | fs_devices->seeding = 0; |
| 1969 | fs_devices->num_devices = 0; |
| 1970 | fs_devices->open_devices = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1971 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1972 | |
| 1973 | generate_random_uuid(fs_devices->fsid); |
| 1974 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 1975 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 1976 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 1977 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1978 | super_flags = btrfs_super_flags(disk_super) & |
| 1979 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 1980 | btrfs_set_super_flags(disk_super, super_flags); |
| 1981 | |
| 1982 | return 0; |
| 1983 | } |
| 1984 | |
| 1985 | /* |
| 1986 | * strore the expected generation for seed devices in device items. |
| 1987 | */ |
| 1988 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 1989 | struct btrfs_root *root) |
| 1990 | { |
| 1991 | struct btrfs_path *path; |
| 1992 | struct extent_buffer *leaf; |
| 1993 | struct btrfs_dev_item *dev_item; |
| 1994 | struct btrfs_device *device; |
| 1995 | struct btrfs_key key; |
| 1996 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 1997 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 1998 | u64 devid; |
| 1999 | int ret; |
| 2000 | |
| 2001 | path = btrfs_alloc_path(); |
| 2002 | if (!path) |
| 2003 | return -ENOMEM; |
| 2004 | |
| 2005 | root = root->fs_info->chunk_root; |
| 2006 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2007 | key.offset = 0; |
| 2008 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2009 | |
| 2010 | while (1) { |
| 2011 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2012 | if (ret < 0) |
| 2013 | goto error; |
| 2014 | |
| 2015 | leaf = path->nodes[0]; |
| 2016 | next_slot: |
| 2017 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2018 | ret = btrfs_next_leaf(root, path); |
| 2019 | if (ret > 0) |
| 2020 | break; |
| 2021 | if (ret < 0) |
| 2022 | goto error; |
| 2023 | leaf = path->nodes[0]; |
| 2024 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2025 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2026 | continue; |
| 2027 | } |
| 2028 | |
| 2029 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2030 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2031 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2032 | break; |
| 2033 | |
| 2034 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2035 | struct btrfs_dev_item); |
| 2036 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2037 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2038 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2039 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2040 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2041 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
| 2042 | fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2043 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2044 | |
| 2045 | if (device->fs_devices->seeding) { |
| 2046 | btrfs_set_device_generation(leaf, dev_item, |
| 2047 | device->generation); |
| 2048 | btrfs_mark_buffer_dirty(leaf); |
| 2049 | } |
| 2050 | |
| 2051 | path->slots[0]++; |
| 2052 | goto next_slot; |
| 2053 | } |
| 2054 | ret = 0; |
| 2055 | error: |
| 2056 | btrfs_free_path(path); |
| 2057 | return ret; |
| 2058 | } |
| 2059 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2060 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 2061 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2062 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2063 | struct btrfs_trans_handle *trans; |
| 2064 | struct btrfs_device *device; |
| 2065 | struct block_device *bdev; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2066 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2067 | struct super_block *sb = root->fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2068 | struct rcu_string *name; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2069 | u64 total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2070 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2071 | int ret = 0; |
| 2072 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2073 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2074 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2075 | |
Li Zefan | a5d1633 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2076 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2077 | root->fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2078 | if (IS_ERR(bdev)) |
| 2079 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2080 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2081 | if (root->fs_info->fs_devices->seeding) { |
| 2082 | seeding_dev = 1; |
| 2083 | down_write(&sb->s_umount); |
| 2084 | mutex_lock(&uuid_mutex); |
| 2085 | } |
| 2086 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2087 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2088 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2089 | devices = &root->fs_info->fs_devices->devices; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2090 | |
| 2091 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2092 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2093 | if (device->bdev == bdev) { |
| 2094 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2095 | mutex_unlock( |
| 2096 | &root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2097 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2098 | } |
| 2099 | } |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2100 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2101 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2102 | device = btrfs_alloc_device(root->fs_info, NULL, NULL); |
| 2103 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2104 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2105 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2106 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2107 | } |
| 2108 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2109 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2110 | if (!name) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2111 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2112 | ret = -ENOMEM; |
| 2113 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2114 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2115 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2116 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2117 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2118 | if (IS_ERR(trans)) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2119 | rcu_string_free(device->name); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2120 | kfree(device); |
| 2121 | ret = PTR_ERR(trans); |
| 2122 | goto error; |
| 2123 | } |
| 2124 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2125 | lock_chunks(root); |
| 2126 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2127 | q = bdev_get_queue(bdev); |
| 2128 | if (blk_queue_discard(q)) |
| 2129 | device->can_discard = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2130 | device->writeable = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2131 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2132 | device->io_width = root->sectorsize; |
| 2133 | device->io_align = root->sectorsize; |
| 2134 | device->sector_size = root->sectorsize; |
| 2135 | device->total_bytes = i_size_read(bdev->bd_inode); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2136 | device->disk_total_bytes = device->total_bytes; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2137 | device->dev_root = root->fs_info->dev_root; |
| 2138 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2139 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2140 | device->is_tgtdev_for_dev_replace = 0; |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2141 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2142 | device->dev_stats_valid = 1; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2143 | set_blocksize(device->bdev, 4096); |
| 2144 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2145 | if (seeding_dev) { |
| 2146 | sb->s_flags &= ~MS_RDONLY; |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2147 | ret = btrfs_prepare_sprout(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2148 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | device->fs_devices = root->fs_info->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2152 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2153 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2154 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2155 | list_add(&device->dev_alloc_list, |
| 2156 | &root->fs_info->fs_devices->alloc_list); |
| 2157 | root->fs_info->fs_devices->num_devices++; |
| 2158 | root->fs_info->fs_devices->open_devices++; |
| 2159 | root->fs_info->fs_devices->rw_devices++; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 2160 | root->fs_info->fs_devices->total_devices++; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2161 | if (device->can_discard) |
| 2162 | root->fs_info->fs_devices->num_can_discard++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2163 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 2164 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2165 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2166 | root->fs_info->free_chunk_space += device->total_bytes; |
| 2167 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2168 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2169 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 2170 | root->fs_info->fs_devices->rotating = 1; |
| 2171 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2172 | total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy); |
| 2173 | btrfs_set_super_total_bytes(root->fs_info->super_copy, |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2174 | total_bytes + device->total_bytes); |
| 2175 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2176 | total_bytes = btrfs_super_num_devices(root->fs_info->super_copy); |
| 2177 | btrfs_set_super_num_devices(root->fs_info->super_copy, |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2178 | total_bytes + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2179 | |
| 2180 | /* add sysfs device entry */ |
| 2181 | btrfs_kobj_add_device(root->fs_info, device); |
| 2182 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2183 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2184 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2185 | if (seeding_dev) { |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2186 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2187 | ret = init_first_rw_device(trans, root, device); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2188 | if (ret) { |
| 2189 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2190 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2191 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2192 | ret = btrfs_finish_sprout(trans, root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2193 | if (ret) { |
| 2194 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2195 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2196 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2197 | |
| 2198 | /* Sprouting would change fsid of the mounted root, |
| 2199 | * so rename the fsid on the sysfs |
| 2200 | */ |
| 2201 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
| 2202 | root->fs_info->fsid); |
| 2203 | if (kobject_rename(&root->fs_info->super_kobj, fsid_buf)) |
| 2204 | goto error_trans; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2205 | } else { |
| 2206 | ret = btrfs_add_device(trans, root, device); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2207 | if (ret) { |
| 2208 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2209 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2210 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2211 | } |
| 2212 | |
Chris Mason | 913d952 | 2009-03-10 13:17:18 -0400 | [diff] [blame] | 2213 | /* |
| 2214 | * we've got more storage, clear any full flags on the space |
| 2215 | * infos |
| 2216 | */ |
| 2217 | btrfs_clear_space_info_full(root->fs_info); |
| 2218 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2219 | unlock_chunks(root); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2220 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 2221 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2222 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2223 | |
| 2224 | if (seeding_dev) { |
| 2225 | mutex_unlock(&uuid_mutex); |
| 2226 | up_write(&sb->s_umount); |
| 2227 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2228 | if (ret) /* transaction commit */ |
| 2229 | return ret; |
| 2230 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2231 | ret = btrfs_relocate_sys_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2232 | if (ret < 0) |
| 2233 | btrfs_error(root->fs_info, ret, |
| 2234 | "Failed to relocate sys chunks after " |
| 2235 | "device initialization. This can be fixed " |
| 2236 | "using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2237 | trans = btrfs_attach_transaction(root); |
| 2238 | if (IS_ERR(trans)) { |
| 2239 | if (PTR_ERR(trans) == -ENOENT) |
| 2240 | return 0; |
| 2241 | return PTR_ERR(trans); |
| 2242 | } |
| 2243 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2244 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2245 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2246 | /* Update ctime/mtime for libblkid */ |
| 2247 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2248 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2249 | |
| 2250 | error_trans: |
| 2251 | unlock_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2252 | btrfs_end_transaction(trans, root); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2253 | rcu_string_free(device->name); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2254 | btrfs_kobj_rm_device(root->fs_info, device); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2255 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2256 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2257 | blkdev_put(bdev, FMODE_EXCL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2258 | if (seeding_dev) { |
| 2259 | mutex_unlock(&uuid_mutex); |
| 2260 | up_write(&sb->s_umount); |
| 2261 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2262 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2263 | } |
| 2264 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2265 | int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path, |
| 2266 | struct btrfs_device **device_out) |
| 2267 | { |
| 2268 | struct request_queue *q; |
| 2269 | struct btrfs_device *device; |
| 2270 | struct block_device *bdev; |
| 2271 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2272 | struct list_head *devices; |
| 2273 | struct rcu_string *name; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2274 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2275 | int ret = 0; |
| 2276 | |
| 2277 | *device_out = NULL; |
| 2278 | if (fs_info->fs_devices->seeding) |
| 2279 | return -EINVAL; |
| 2280 | |
| 2281 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| 2282 | fs_info->bdev_holder); |
| 2283 | if (IS_ERR(bdev)) |
| 2284 | return PTR_ERR(bdev); |
| 2285 | |
| 2286 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 2287 | |
| 2288 | devices = &fs_info->fs_devices->devices; |
| 2289 | list_for_each_entry(device, devices, dev_list) { |
| 2290 | if (device->bdev == bdev) { |
| 2291 | ret = -EEXIST; |
| 2292 | goto error; |
| 2293 | } |
| 2294 | } |
| 2295 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2296 | device = btrfs_alloc_device(NULL, &devid, NULL); |
| 2297 | if (IS_ERR(device)) { |
| 2298 | ret = PTR_ERR(device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2299 | goto error; |
| 2300 | } |
| 2301 | |
| 2302 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2303 | if (!name) { |
| 2304 | kfree(device); |
| 2305 | ret = -ENOMEM; |
| 2306 | goto error; |
| 2307 | } |
| 2308 | rcu_assign_pointer(device->name, name); |
| 2309 | |
| 2310 | q = bdev_get_queue(bdev); |
| 2311 | if (blk_queue_discard(q)) |
| 2312 | device->can_discard = 1; |
| 2313 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 2314 | device->writeable = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2315 | device->generation = 0; |
| 2316 | device->io_width = root->sectorsize; |
| 2317 | device->io_align = root->sectorsize; |
| 2318 | device->sector_size = root->sectorsize; |
| 2319 | device->total_bytes = i_size_read(bdev->bd_inode); |
| 2320 | device->disk_total_bytes = device->total_bytes; |
| 2321 | device->dev_root = fs_info->dev_root; |
| 2322 | device->bdev = bdev; |
| 2323 | device->in_fs_metadata = 1; |
| 2324 | device->is_tgtdev_for_dev_replace = 1; |
| 2325 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2326 | device->dev_stats_valid = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2327 | set_blocksize(device->bdev, 4096); |
| 2328 | device->fs_devices = fs_info->fs_devices; |
| 2329 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 2330 | fs_info->fs_devices->num_devices++; |
| 2331 | fs_info->fs_devices->open_devices++; |
| 2332 | if (device->can_discard) |
| 2333 | fs_info->fs_devices->num_can_discard++; |
| 2334 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2335 | |
| 2336 | *device_out = device; |
| 2337 | return ret; |
| 2338 | |
| 2339 | error: |
| 2340 | blkdev_put(bdev, FMODE_EXCL); |
| 2341 | return ret; |
| 2342 | } |
| 2343 | |
| 2344 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, |
| 2345 | struct btrfs_device *tgtdev) |
| 2346 | { |
| 2347 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 2348 | tgtdev->io_width = fs_info->dev_root->sectorsize; |
| 2349 | tgtdev->io_align = fs_info->dev_root->sectorsize; |
| 2350 | tgtdev->sector_size = fs_info->dev_root->sectorsize; |
| 2351 | tgtdev->dev_root = fs_info->dev_root; |
| 2352 | tgtdev->in_fs_metadata = 1; |
| 2353 | } |
| 2354 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2355 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2356 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2357 | { |
| 2358 | int ret; |
| 2359 | struct btrfs_path *path; |
| 2360 | struct btrfs_root *root; |
| 2361 | struct btrfs_dev_item *dev_item; |
| 2362 | struct extent_buffer *leaf; |
| 2363 | struct btrfs_key key; |
| 2364 | |
| 2365 | root = device->dev_root->fs_info->chunk_root; |
| 2366 | |
| 2367 | path = btrfs_alloc_path(); |
| 2368 | if (!path) |
| 2369 | return -ENOMEM; |
| 2370 | |
| 2371 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2372 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2373 | key.offset = device->devid; |
| 2374 | |
| 2375 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2376 | if (ret < 0) |
| 2377 | goto out; |
| 2378 | |
| 2379 | if (ret > 0) { |
| 2380 | ret = -ENOENT; |
| 2381 | goto out; |
| 2382 | } |
| 2383 | |
| 2384 | leaf = path->nodes[0]; |
| 2385 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2386 | |
| 2387 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2388 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2389 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2390 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2391 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 2392 | btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2393 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
| 2394 | btrfs_mark_buffer_dirty(leaf); |
| 2395 | |
| 2396 | out: |
| 2397 | btrfs_free_path(path); |
| 2398 | return ret; |
| 2399 | } |
| 2400 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2401 | static int __btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2402 | struct btrfs_device *device, u64 new_size) |
| 2403 | { |
| 2404 | struct btrfs_super_block *super_copy = |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2405 | device->dev_root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2406 | u64 old_total = btrfs_super_total_bytes(super_copy); |
| 2407 | u64 diff = new_size - device->total_bytes; |
| 2408 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2409 | if (!device->writeable) |
| 2410 | return -EACCES; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2411 | if (new_size <= device->total_bytes || |
| 2412 | device->is_tgtdev_for_dev_replace) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2413 | return -EINVAL; |
| 2414 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2415 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2416 | device->fs_devices->total_rw_bytes += diff; |
| 2417 | |
| 2418 | device->total_bytes = new_size; |
Chris Mason | 9779b72 | 2009-07-24 16:41:41 -0400 | [diff] [blame] | 2419 | device->disk_total_bytes = new_size; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2420 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
| 2421 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2422 | return btrfs_update_device(trans, device); |
| 2423 | } |
| 2424 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2425 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
| 2426 | struct btrfs_device *device, u64 new_size) |
| 2427 | { |
| 2428 | int ret; |
| 2429 | lock_chunks(device->dev_root); |
| 2430 | ret = __btrfs_grow_device(trans, device, new_size); |
| 2431 | unlock_chunks(device->dev_root); |
| 2432 | return ret; |
| 2433 | } |
| 2434 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2435 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
| 2436 | struct btrfs_root *root, |
| 2437 | u64 chunk_tree, u64 chunk_objectid, |
| 2438 | u64 chunk_offset) |
| 2439 | { |
| 2440 | int ret; |
| 2441 | struct btrfs_path *path; |
| 2442 | struct btrfs_key key; |
| 2443 | |
| 2444 | root = root->fs_info->chunk_root; |
| 2445 | path = btrfs_alloc_path(); |
| 2446 | if (!path) |
| 2447 | return -ENOMEM; |
| 2448 | |
| 2449 | key.objectid = chunk_objectid; |
| 2450 | key.offset = chunk_offset; |
| 2451 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2452 | |
| 2453 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2454 | if (ret < 0) |
| 2455 | goto out; |
| 2456 | else if (ret > 0) { /* Logic error or corruption */ |
| 2457 | btrfs_error(root->fs_info, -ENOENT, |
| 2458 | "Failed lookup while freeing chunk."); |
| 2459 | ret = -ENOENT; |
| 2460 | goto out; |
| 2461 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2462 | |
| 2463 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2464 | if (ret < 0) |
| 2465 | btrfs_error(root->fs_info, ret, |
| 2466 | "Failed to delete chunk item."); |
| 2467 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2468 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2469 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2470 | } |
| 2471 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2472 | 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] | 2473 | chunk_offset) |
| 2474 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2475 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2476 | struct btrfs_disk_key *disk_key; |
| 2477 | struct btrfs_chunk *chunk; |
| 2478 | u8 *ptr; |
| 2479 | int ret = 0; |
| 2480 | u32 num_stripes; |
| 2481 | u32 array_size; |
| 2482 | u32 len = 0; |
| 2483 | u32 cur; |
| 2484 | struct btrfs_key key; |
| 2485 | |
| 2486 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2487 | |
| 2488 | ptr = super_copy->sys_chunk_array; |
| 2489 | cur = 0; |
| 2490 | |
| 2491 | while (cur < array_size) { |
| 2492 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2493 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2494 | |
| 2495 | len = sizeof(*disk_key); |
| 2496 | |
| 2497 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2498 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2499 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2500 | len += btrfs_chunk_item_size(num_stripes); |
| 2501 | } else { |
| 2502 | ret = -EIO; |
| 2503 | break; |
| 2504 | } |
| 2505 | if (key.objectid == chunk_objectid && |
| 2506 | key.offset == chunk_offset) { |
| 2507 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2508 | array_size -= len; |
| 2509 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2510 | } else { |
| 2511 | ptr += len; |
| 2512 | cur += len; |
| 2513 | } |
| 2514 | } |
| 2515 | return ret; |
| 2516 | } |
| 2517 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2518 | static int btrfs_relocate_chunk(struct btrfs_root *root, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2519 | u64 chunk_tree, u64 chunk_objectid, |
| 2520 | u64 chunk_offset) |
| 2521 | { |
| 2522 | struct extent_map_tree *em_tree; |
| 2523 | struct btrfs_root *extent_root; |
| 2524 | struct btrfs_trans_handle *trans; |
| 2525 | struct extent_map *em; |
| 2526 | struct map_lookup *map; |
| 2527 | int ret; |
| 2528 | int i; |
| 2529 | |
| 2530 | root = root->fs_info->chunk_root; |
| 2531 | extent_root = root->fs_info->extent_root; |
| 2532 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 2533 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2534 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
| 2535 | if (ret) |
| 2536 | return -ENOSPC; |
| 2537 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2538 | /* step one, relocate all the extents inside this chunk */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2539 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2540 | if (ret) |
| 2541 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2542 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2543 | trans = btrfs_start_transaction(root, 0); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 2544 | if (IS_ERR(trans)) { |
| 2545 | ret = PTR_ERR(trans); |
| 2546 | btrfs_std_error(root->fs_info, ret); |
| 2547 | return ret; |
| 2548 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2549 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2550 | lock_chunks(root); |
| 2551 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2552 | /* |
| 2553 | * step two, delete the device extents and the |
| 2554 | * chunk tree entries |
| 2555 | */ |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2556 | read_lock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2557 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2558 | read_unlock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2559 | |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 2560 | BUG_ON(!em || em->start > chunk_offset || |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2561 | em->start + em->len < chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2562 | map = (struct map_lookup *)em->bdev; |
| 2563 | |
| 2564 | for (i = 0; i < map->num_stripes; i++) { |
| 2565 | ret = btrfs_free_dev_extent(trans, map->stripes[i].dev, |
| 2566 | map->stripes[i].physical); |
| 2567 | BUG_ON(ret); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2568 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2569 | if (map->stripes[i].dev) { |
| 2570 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2571 | BUG_ON(ret); |
| 2572 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2573 | } |
| 2574 | ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid, |
| 2575 | chunk_offset); |
| 2576 | |
| 2577 | BUG_ON(ret); |
| 2578 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2579 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
| 2580 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2581 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2582 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 2583 | BUG_ON(ret); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2584 | } |
| 2585 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2586 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset); |
| 2587 | BUG_ON(ret); |
| 2588 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2589 | write_lock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2590 | remove_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2591 | write_unlock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2592 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2593 | /* once for the tree */ |
| 2594 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2595 | /* once for us */ |
| 2596 | free_extent_map(em); |
| 2597 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2598 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2599 | btrfs_end_transaction(trans, root); |
| 2600 | return 0; |
| 2601 | } |
| 2602 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2603 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 2604 | { |
| 2605 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 2606 | struct btrfs_path *path; |
| 2607 | struct extent_buffer *leaf; |
| 2608 | struct btrfs_chunk *chunk; |
| 2609 | struct btrfs_key key; |
| 2610 | struct btrfs_key found_key; |
| 2611 | u64 chunk_tree = chunk_root->root_key.objectid; |
| 2612 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2613 | bool retried = false; |
| 2614 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2615 | int ret; |
| 2616 | |
| 2617 | path = btrfs_alloc_path(); |
| 2618 | if (!path) |
| 2619 | return -ENOMEM; |
| 2620 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2621 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2622 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2623 | key.offset = (u64)-1; |
| 2624 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2625 | |
| 2626 | while (1) { |
| 2627 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 2628 | if (ret < 0) |
| 2629 | goto error; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2630 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2631 | |
| 2632 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2633 | key.type); |
| 2634 | if (ret < 0) |
| 2635 | goto error; |
| 2636 | if (ret > 0) |
| 2637 | break; |
| 2638 | |
| 2639 | leaf = path->nodes[0]; |
| 2640 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2641 | |
| 2642 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2643 | struct btrfs_chunk); |
| 2644 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2645 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2646 | |
| 2647 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2648 | ret = btrfs_relocate_chunk(chunk_root, chunk_tree, |
| 2649 | found_key.objectid, |
| 2650 | found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2651 | if (ret == -ENOSPC) |
| 2652 | failed++; |
| 2653 | else if (ret) |
| 2654 | BUG(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | if (found_key.offset == 0) |
| 2658 | break; |
| 2659 | key.offset = found_key.offset - 1; |
| 2660 | } |
| 2661 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2662 | if (failed && !retried) { |
| 2663 | failed = 0; |
| 2664 | retried = true; |
| 2665 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2666 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2667 | ret = -ENOSPC; |
| 2668 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2669 | error: |
| 2670 | btrfs_free_path(path); |
| 2671 | return ret; |
| 2672 | } |
| 2673 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2674 | static int insert_balance_item(struct btrfs_root *root, |
| 2675 | struct btrfs_balance_control *bctl) |
| 2676 | { |
| 2677 | struct btrfs_trans_handle *trans; |
| 2678 | struct btrfs_balance_item *item; |
| 2679 | struct btrfs_disk_balance_args disk_bargs; |
| 2680 | struct btrfs_path *path; |
| 2681 | struct extent_buffer *leaf; |
| 2682 | struct btrfs_key key; |
| 2683 | int ret, err; |
| 2684 | |
| 2685 | path = btrfs_alloc_path(); |
| 2686 | if (!path) |
| 2687 | return -ENOMEM; |
| 2688 | |
| 2689 | trans = btrfs_start_transaction(root, 0); |
| 2690 | if (IS_ERR(trans)) { |
| 2691 | btrfs_free_path(path); |
| 2692 | return PTR_ERR(trans); |
| 2693 | } |
| 2694 | |
| 2695 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2696 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2697 | key.offset = 0; |
| 2698 | |
| 2699 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2700 | sizeof(*item)); |
| 2701 | if (ret) |
| 2702 | goto out; |
| 2703 | |
| 2704 | leaf = path->nodes[0]; |
| 2705 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 2706 | |
| 2707 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 2708 | |
| 2709 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 2710 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 2711 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 2712 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 2713 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 2714 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 2715 | |
| 2716 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 2717 | |
| 2718 | btrfs_mark_buffer_dirty(leaf); |
| 2719 | out: |
| 2720 | btrfs_free_path(path); |
| 2721 | err = btrfs_commit_transaction(trans, root); |
| 2722 | if (err && !ret) |
| 2723 | ret = err; |
| 2724 | return ret; |
| 2725 | } |
| 2726 | |
| 2727 | static int del_balance_item(struct btrfs_root *root) |
| 2728 | { |
| 2729 | struct btrfs_trans_handle *trans; |
| 2730 | struct btrfs_path *path; |
| 2731 | struct btrfs_key key; |
| 2732 | int ret, err; |
| 2733 | |
| 2734 | path = btrfs_alloc_path(); |
| 2735 | if (!path) |
| 2736 | return -ENOMEM; |
| 2737 | |
| 2738 | trans = btrfs_start_transaction(root, 0); |
| 2739 | if (IS_ERR(trans)) { |
| 2740 | btrfs_free_path(path); |
| 2741 | return PTR_ERR(trans); |
| 2742 | } |
| 2743 | |
| 2744 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2745 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2746 | key.offset = 0; |
| 2747 | |
| 2748 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2749 | if (ret < 0) |
| 2750 | goto out; |
| 2751 | if (ret > 0) { |
| 2752 | ret = -ENOENT; |
| 2753 | goto out; |
| 2754 | } |
| 2755 | |
| 2756 | ret = btrfs_del_item(trans, root, path); |
| 2757 | out: |
| 2758 | btrfs_free_path(path); |
| 2759 | err = btrfs_commit_transaction(trans, root); |
| 2760 | if (err && !ret) |
| 2761 | ret = err; |
| 2762 | return ret; |
| 2763 | } |
| 2764 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2765 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2766 | * This is a heuristic used to reduce the number of chunks balanced on |
| 2767 | * resume after balance was interrupted. |
| 2768 | */ |
| 2769 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 2770 | { |
| 2771 | /* |
| 2772 | * Turn on soft mode for chunk types that were being converted. |
| 2773 | */ |
| 2774 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2775 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2776 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2777 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2778 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2779 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2780 | |
| 2781 | /* |
| 2782 | * Turn on usage filter if is not already used. The idea is |
| 2783 | * that chunks that we have already balanced should be |
| 2784 | * reasonably full. Don't do it for chunks that are being |
| 2785 | * converted - that will keep us from relocating unconverted |
| 2786 | * (albeit full) chunks. |
| 2787 | */ |
| 2788 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2789 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2790 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2791 | bctl->data.usage = 90; |
| 2792 | } |
| 2793 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2794 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2795 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2796 | bctl->sys.usage = 90; |
| 2797 | } |
| 2798 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2799 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2800 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2801 | bctl->meta.usage = 90; |
| 2802 | } |
| 2803 | } |
| 2804 | |
| 2805 | /* |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2806 | * Should be called with both balance and volume mutexes held to |
| 2807 | * serialize other volume operations (add_dev/rm_dev/resize) with |
| 2808 | * restriper. Same goes for unset_balance_control. |
| 2809 | */ |
| 2810 | static void set_balance_control(struct btrfs_balance_control *bctl) |
| 2811 | { |
| 2812 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
| 2813 | |
| 2814 | BUG_ON(fs_info->balance_ctl); |
| 2815 | |
| 2816 | spin_lock(&fs_info->balance_lock); |
| 2817 | fs_info->balance_ctl = bctl; |
| 2818 | spin_unlock(&fs_info->balance_lock); |
| 2819 | } |
| 2820 | |
| 2821 | static void unset_balance_control(struct btrfs_fs_info *fs_info) |
| 2822 | { |
| 2823 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 2824 | |
| 2825 | BUG_ON(!fs_info->balance_ctl); |
| 2826 | |
| 2827 | spin_lock(&fs_info->balance_lock); |
| 2828 | fs_info->balance_ctl = NULL; |
| 2829 | spin_unlock(&fs_info->balance_lock); |
| 2830 | |
| 2831 | kfree(bctl); |
| 2832 | } |
| 2833 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2834 | /* |
| 2835 | * Balance filters. Return 1 if chunk should be filtered out |
| 2836 | * (should not be balanced). |
| 2837 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2838 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2839 | struct btrfs_balance_args *bargs) |
| 2840 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2841 | chunk_type = chunk_to_extended(chunk_type) & |
| 2842 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2843 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2844 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2845 | return 0; |
| 2846 | |
| 2847 | return 1; |
| 2848 | } |
| 2849 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2850 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
| 2851 | struct btrfs_balance_args *bargs) |
| 2852 | { |
| 2853 | struct btrfs_block_group_cache *cache; |
| 2854 | u64 chunk_used, user_thresh; |
| 2855 | int ret = 1; |
| 2856 | |
| 2857 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 2858 | chunk_used = btrfs_block_group_used(&cache->item); |
| 2859 | |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 2860 | if (bargs->usage == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 2861 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 2862 | else if (bargs->usage > 100) |
| 2863 | user_thresh = cache->key.offset; |
| 2864 | else |
| 2865 | user_thresh = div_factor_fine(cache->key.offset, |
| 2866 | bargs->usage); |
| 2867 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2868 | if (chunk_used < user_thresh) |
| 2869 | ret = 0; |
| 2870 | |
| 2871 | btrfs_put_block_group(cache); |
| 2872 | return ret; |
| 2873 | } |
| 2874 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2875 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 2876 | struct btrfs_chunk *chunk, |
| 2877 | struct btrfs_balance_args *bargs) |
| 2878 | { |
| 2879 | struct btrfs_stripe *stripe; |
| 2880 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2881 | int i; |
| 2882 | |
| 2883 | for (i = 0; i < num_stripes; i++) { |
| 2884 | stripe = btrfs_stripe_nr(chunk, i); |
| 2885 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 2886 | return 0; |
| 2887 | } |
| 2888 | |
| 2889 | return 1; |
| 2890 | } |
| 2891 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2892 | /* [pstart, pend) */ |
| 2893 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 2894 | struct btrfs_chunk *chunk, |
| 2895 | u64 chunk_offset, |
| 2896 | struct btrfs_balance_args *bargs) |
| 2897 | { |
| 2898 | struct btrfs_stripe *stripe; |
| 2899 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2900 | u64 stripe_offset; |
| 2901 | u64 stripe_length; |
| 2902 | int factor; |
| 2903 | int i; |
| 2904 | |
| 2905 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 2906 | return 0; |
| 2907 | |
| 2908 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2909 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 2910 | factor = num_stripes / 2; |
| 2911 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 2912 | factor = num_stripes - 1; |
| 2913 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 2914 | factor = num_stripes - 2; |
| 2915 | } else { |
| 2916 | factor = num_stripes; |
| 2917 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2918 | |
| 2919 | for (i = 0; i < num_stripes; i++) { |
| 2920 | stripe = btrfs_stripe_nr(chunk, i); |
| 2921 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 2922 | continue; |
| 2923 | |
| 2924 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 2925 | stripe_length = btrfs_chunk_length(leaf, chunk); |
| 2926 | do_div(stripe_length, factor); |
| 2927 | |
| 2928 | if (stripe_offset < bargs->pend && |
| 2929 | stripe_offset + stripe_length > bargs->pstart) |
| 2930 | return 0; |
| 2931 | } |
| 2932 | |
| 2933 | return 1; |
| 2934 | } |
| 2935 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2936 | /* [vstart, vend) */ |
| 2937 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 2938 | struct btrfs_chunk *chunk, |
| 2939 | u64 chunk_offset, |
| 2940 | struct btrfs_balance_args *bargs) |
| 2941 | { |
| 2942 | if (chunk_offset < bargs->vend && |
| 2943 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 2944 | /* at least part of the chunk is inside this vrange */ |
| 2945 | return 0; |
| 2946 | |
| 2947 | return 1; |
| 2948 | } |
| 2949 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2950 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2951 | struct btrfs_balance_args *bargs) |
| 2952 | { |
| 2953 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 2954 | return 0; |
| 2955 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2956 | chunk_type = chunk_to_extended(chunk_type) & |
| 2957 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2958 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2959 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2960 | return 1; |
| 2961 | |
| 2962 | return 0; |
| 2963 | } |
| 2964 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2965 | static int should_balance_chunk(struct btrfs_root *root, |
| 2966 | struct extent_buffer *leaf, |
| 2967 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 2968 | { |
| 2969 | struct btrfs_balance_control *bctl = root->fs_info->balance_ctl; |
| 2970 | struct btrfs_balance_args *bargs = NULL; |
| 2971 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 2972 | |
| 2973 | /* type filter */ |
| 2974 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 2975 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 2976 | return 0; |
| 2977 | } |
| 2978 | |
| 2979 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 2980 | bargs = &bctl->data; |
| 2981 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2982 | bargs = &bctl->sys; |
| 2983 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 2984 | bargs = &bctl->meta; |
| 2985 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2986 | /* profiles filter */ |
| 2987 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 2988 | chunk_profiles_filter(chunk_type, bargs)) { |
| 2989 | return 0; |
| 2990 | } |
| 2991 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2992 | /* usage filter */ |
| 2993 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2994 | chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) { |
| 2995 | return 0; |
| 2996 | } |
| 2997 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2998 | /* devid filter */ |
| 2999 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3000 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3001 | return 0; |
| 3002 | } |
| 3003 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3004 | /* drange filter, makes sense only with devid filter */ |
| 3005 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
| 3006 | chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3007 | return 0; |
| 3008 | } |
| 3009 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3010 | /* vrange filter */ |
| 3011 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3012 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3013 | return 0; |
| 3014 | } |
| 3015 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3016 | /* soft profile changing mode */ |
| 3017 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3018 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3019 | return 0; |
| 3020 | } |
| 3021 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3022 | /* |
| 3023 | * limited by count, must be the last filter |
| 3024 | */ |
| 3025 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3026 | if (bargs->limit == 0) |
| 3027 | return 0; |
| 3028 | else |
| 3029 | bargs->limit--; |
| 3030 | } |
| 3031 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3032 | return 1; |
| 3033 | } |
| 3034 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3035 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3036 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3037 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3038 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 3039 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 3040 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3041 | struct btrfs_device *device; |
| 3042 | u64 old_size; |
| 3043 | u64 size_to_free; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3044 | struct btrfs_chunk *chunk; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3045 | struct btrfs_path *path; |
| 3046 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3047 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3048 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3049 | struct extent_buffer *leaf; |
| 3050 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3051 | int ret; |
| 3052 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3053 | bool counting = true; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3054 | u64 limit_data = bctl->data.limit; |
| 3055 | u64 limit_meta = bctl->meta.limit; |
| 3056 | u64 limit_sys = bctl->sys.limit; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3057 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3058 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3059 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3060 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3061 | old_size = device->total_bytes; |
| 3062 | size_to_free = div_factor(old_size, 1); |
| 3063 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3064 | if (!device->writeable || |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3065 | device->total_bytes - device->bytes_used > size_to_free || |
| 3066 | device->is_tgtdev_for_dev_replace) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3067 | continue; |
| 3068 | |
| 3069 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3070 | if (ret == -ENOSPC) |
| 3071 | break; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3072 | BUG_ON(ret); |
| 3073 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3074 | trans = btrfs_start_transaction(dev_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3075 | BUG_ON(IS_ERR(trans)); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3076 | |
| 3077 | ret = btrfs_grow_device(trans, device, old_size); |
| 3078 | BUG_ON(ret); |
| 3079 | |
| 3080 | btrfs_end_transaction(trans, dev_root); |
| 3081 | } |
| 3082 | |
| 3083 | /* step two, relocate all the chunks */ |
| 3084 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3085 | if (!path) { |
| 3086 | ret = -ENOMEM; |
| 3087 | goto error; |
| 3088 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3089 | |
| 3090 | /* zero out stat counters */ |
| 3091 | spin_lock(&fs_info->balance_lock); |
| 3092 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3093 | spin_unlock(&fs_info->balance_lock); |
| 3094 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3095 | if (!counting) { |
| 3096 | bctl->data.limit = limit_data; |
| 3097 | bctl->meta.limit = limit_meta; |
| 3098 | bctl->sys.limit = limit_sys; |
| 3099 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3100 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3101 | key.offset = (u64)-1; |
| 3102 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3103 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3104 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3105 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3106 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3107 | ret = -ECANCELED; |
| 3108 | goto error; |
| 3109 | } |
| 3110 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3111 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 3112 | if (ret < 0) |
| 3113 | goto error; |
| 3114 | |
| 3115 | /* |
| 3116 | * this shouldn't happen, it means the last relocate |
| 3117 | * failed |
| 3118 | */ |
| 3119 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3120 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3121 | |
| 3122 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3123 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3124 | if (ret) { |
| 3125 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3126 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3127 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3128 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3129 | leaf = path->nodes[0]; |
| 3130 | slot = path->slots[0]; |
| 3131 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3132 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3133 | if (found_key.objectid != key.objectid) |
| 3134 | break; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3135 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3136 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 3137 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3138 | if (!counting) { |
| 3139 | spin_lock(&fs_info->balance_lock); |
| 3140 | bctl->stat.considered++; |
| 3141 | spin_unlock(&fs_info->balance_lock); |
| 3142 | } |
| 3143 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3144 | ret = should_balance_chunk(chunk_root, leaf, chunk, |
| 3145 | found_key.offset); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3146 | btrfs_release_path(path); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3147 | if (!ret) |
| 3148 | goto loop; |
| 3149 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3150 | if (counting) { |
| 3151 | spin_lock(&fs_info->balance_lock); |
| 3152 | bctl->stat.expected++; |
| 3153 | spin_unlock(&fs_info->balance_lock); |
| 3154 | goto loop; |
| 3155 | } |
| 3156 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3157 | ret = btrfs_relocate_chunk(chunk_root, |
| 3158 | chunk_root->root_key.objectid, |
| 3159 | found_key.objectid, |
| 3160 | found_key.offset); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 3161 | if (ret && ret != -ENOSPC) |
| 3162 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3163 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3164 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3165 | } else { |
| 3166 | spin_lock(&fs_info->balance_lock); |
| 3167 | bctl->stat.completed++; |
| 3168 | spin_unlock(&fs_info->balance_lock); |
| 3169 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3170 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3171 | if (found_key.offset == 0) |
| 3172 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3173 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3174 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3175 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3176 | if (counting) { |
| 3177 | btrfs_release_path(path); |
| 3178 | counting = false; |
| 3179 | goto again; |
| 3180 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3181 | error: |
| 3182 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3183 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3184 | btrfs_info(fs_info, "%d enospc errors during balance", |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3185 | enospc_errors); |
| 3186 | if (!ret) |
| 3187 | ret = -ENOSPC; |
| 3188 | } |
| 3189 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3190 | return ret; |
| 3191 | } |
| 3192 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3193 | /** |
| 3194 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3195 | * @flags: profile to validate |
| 3196 | * @extended: if true @flags is treated as an extended profile |
| 3197 | */ |
| 3198 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3199 | { |
| 3200 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3201 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3202 | |
| 3203 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3204 | |
| 3205 | /* 1) check that all other bits are zeroed */ |
| 3206 | if (flags & ~mask) |
| 3207 | return 0; |
| 3208 | |
| 3209 | /* 2) see if profile is reduced */ |
| 3210 | if (flags == 0) |
| 3211 | return !extended; /* "0" is valid for usual profiles */ |
| 3212 | |
| 3213 | /* true if exactly one bit set */ |
| 3214 | return (flags & (flags - 1)) == 0; |
| 3215 | } |
| 3216 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3217 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3218 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3219 | /* cancel requested || normal exit path */ |
| 3220 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3221 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3222 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3223 | } |
| 3224 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3225 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
| 3226 | { |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3227 | int ret; |
| 3228 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3229 | unset_balance_control(fs_info); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3230 | ret = del_balance_item(fs_info->tree_root); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 3231 | if (ret) |
| 3232 | btrfs_std_error(fs_info, ret); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3233 | |
| 3234 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3235 | } |
| 3236 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3237 | /* |
| 3238 | * Should be called with both balance and volume mutexes held |
| 3239 | */ |
| 3240 | int btrfs_balance(struct btrfs_balance_control *bctl, |
| 3241 | struct btrfs_ioctl_balance_args *bargs) |
| 3242 | { |
| 3243 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3244 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3245 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3246 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3247 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3248 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3249 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3250 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3251 | atomic_read(&fs_info->balance_pause_req) || |
| 3252 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3253 | ret = -EINVAL; |
| 3254 | goto out; |
| 3255 | } |
| 3256 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3257 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3258 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3259 | mixed = 1; |
| 3260 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3261 | /* |
| 3262 | * In case of mixed groups both data and meta should be picked, |
| 3263 | * and identical options should be given for both of them. |
| 3264 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3265 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3266 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3267 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3268 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3269 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3270 | btrfs_err(fs_info, "with mixed groups data and " |
| 3271 | "metadata balance options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3272 | ret = -EINVAL; |
| 3273 | goto out; |
| 3274 | } |
| 3275 | } |
| 3276 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3277 | num_devices = fs_info->fs_devices->num_devices; |
| 3278 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 3279 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 3280 | BUG_ON(num_devices < 1); |
| 3281 | num_devices--; |
| 3282 | } |
| 3283 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3284 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3285 | if (num_devices == 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3286 | allowed |= BTRFS_BLOCK_GROUP_DUP; |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3287 | else if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3288 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3289 | if (num_devices > 2) |
| 3290 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 3291 | if (num_devices > 3) |
| 3292 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 3293 | BTRFS_BLOCK_GROUP_RAID6); |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3294 | if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3295 | (!alloc_profile_is_valid(bctl->data.target, 1) || |
| 3296 | (bctl->data.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3297 | btrfs_err(fs_info, "unable to start balance with target " |
| 3298 | "data profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3299 | bctl->data.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3300 | ret = -EINVAL; |
| 3301 | goto out; |
| 3302 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3303 | if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3304 | (!alloc_profile_is_valid(bctl->meta.target, 1) || |
| 3305 | (bctl->meta.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3306 | btrfs_err(fs_info, |
| 3307 | "unable to start balance with target metadata profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3308 | bctl->meta.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3309 | ret = -EINVAL; |
| 3310 | goto out; |
| 3311 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3312 | if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3313 | (!alloc_profile_is_valid(bctl->sys.target, 1) || |
| 3314 | (bctl->sys.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3315 | btrfs_err(fs_info, |
| 3316 | "unable to start balance with target system profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3317 | bctl->sys.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3318 | ret = -EINVAL; |
| 3319 | goto out; |
| 3320 | } |
| 3321 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3322 | /* allow dup'ed data chunks only in mixed mode */ |
| 3323 | if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3324 | (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3325 | btrfs_err(fs_info, "dup for data is not allowed"); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3326 | ret = -EINVAL; |
| 3327 | goto out; |
| 3328 | } |
| 3329 | |
| 3330 | /* allow to reduce meta or sys integrity only if force set */ |
| 3331 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3332 | BTRFS_BLOCK_GROUP_RAID10 | |
| 3333 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3334 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3335 | do { |
| 3336 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3337 | |
| 3338 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3339 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3340 | !(bctl->sys.target & allowed)) || |
| 3341 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3342 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3343 | !(bctl->meta.target & allowed))) { |
| 3344 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3345 | btrfs_info(fs_info, "force reducing metadata integrity"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3346 | } else { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3347 | btrfs_err(fs_info, "balance will reduce metadata " |
| 3348 | "integrity, use force if you want this"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3349 | ret = -EINVAL; |
| 3350 | goto out; |
| 3351 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3352 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3353 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3354 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3355 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3356 | int num_tolerated_disk_barrier_failures; |
| 3357 | u64 target = bctl->sys.target; |
| 3358 | |
| 3359 | num_tolerated_disk_barrier_failures = |
| 3360 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3361 | if (num_tolerated_disk_barrier_failures > 0 && |
| 3362 | (target & |
| 3363 | (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 | |
| 3364 | BTRFS_AVAIL_ALLOC_BIT_SINGLE))) |
| 3365 | num_tolerated_disk_barrier_failures = 0; |
| 3366 | else if (num_tolerated_disk_barrier_failures > 1 && |
| 3367 | (target & |
| 3368 | (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))) |
| 3369 | num_tolerated_disk_barrier_failures = 1; |
| 3370 | |
| 3371 | fs_info->num_tolerated_disk_barrier_failures = |
| 3372 | num_tolerated_disk_barrier_failures; |
| 3373 | } |
| 3374 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3375 | ret = insert_balance_item(fs_info->tree_root, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3376 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3377 | goto out; |
| 3378 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3379 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3380 | BUG_ON(ret == -EEXIST); |
| 3381 | set_balance_control(bctl); |
| 3382 | } else { |
| 3383 | BUG_ON(ret != -EEXIST); |
| 3384 | spin_lock(&fs_info->balance_lock); |
| 3385 | update_balance_args(bctl); |
| 3386 | spin_unlock(&fs_info->balance_lock); |
| 3387 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3388 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3389 | atomic_inc(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3390 | mutex_unlock(&fs_info->balance_mutex); |
| 3391 | |
| 3392 | ret = __btrfs_balance(fs_info); |
| 3393 | |
| 3394 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3395 | atomic_dec(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3396 | |
Ilya Dryomov | bf023ec | 2013-02-12 16:27:46 +0000 | [diff] [blame] | 3397 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3398 | fs_info->num_tolerated_disk_barrier_failures = |
| 3399 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3400 | } |
| 3401 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3402 | if (bargs) { |
| 3403 | memset(bargs, 0, sizeof(*bargs)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3404 | update_ioctl_balance_args(fs_info, 0, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3405 | } |
| 3406 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3407 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3408 | balance_need_close(fs_info)) { |
| 3409 | __cancel_balance(fs_info); |
| 3410 | } |
| 3411 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3412 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3413 | |
| 3414 | return ret; |
| 3415 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3416 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
| 3417 | __cancel_balance(fs_info); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3418 | else { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3419 | kfree(bctl); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3420 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 3421 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3422 | return ret; |
| 3423 | } |
| 3424 | |
| 3425 | static int balance_kthread(void *data) |
| 3426 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3427 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3428 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3429 | |
| 3430 | mutex_lock(&fs_info->volume_mutex); |
| 3431 | mutex_lock(&fs_info->balance_mutex); |
| 3432 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3433 | if (fs_info->balance_ctl) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3434 | btrfs_info(fs_info, "continuing balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3435 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3436 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3437 | |
| 3438 | mutex_unlock(&fs_info->balance_mutex); |
| 3439 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3440 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3441 | return ret; |
| 3442 | } |
| 3443 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3444 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 3445 | { |
| 3446 | struct task_struct *tsk; |
| 3447 | |
| 3448 | spin_lock(&fs_info->balance_lock); |
| 3449 | if (!fs_info->balance_ctl) { |
| 3450 | spin_unlock(&fs_info->balance_lock); |
| 3451 | return 0; |
| 3452 | } |
| 3453 | spin_unlock(&fs_info->balance_lock); |
| 3454 | |
| 3455 | if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3456 | btrfs_info(fs_info, "force skipping balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3457 | return 0; |
| 3458 | } |
| 3459 | |
| 3460 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 3461 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3462 | } |
| 3463 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3464 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3465 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3466 | struct btrfs_balance_control *bctl; |
| 3467 | struct btrfs_balance_item *item; |
| 3468 | struct btrfs_disk_balance_args disk_bargs; |
| 3469 | struct btrfs_path *path; |
| 3470 | struct extent_buffer *leaf; |
| 3471 | struct btrfs_key key; |
| 3472 | int ret; |
| 3473 | |
| 3474 | path = btrfs_alloc_path(); |
| 3475 | if (!path) |
| 3476 | return -ENOMEM; |
| 3477 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3478 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 3479 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 3480 | key.offset = 0; |
| 3481 | |
| 3482 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3483 | if (ret < 0) |
| 3484 | goto out; |
| 3485 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3486 | ret = 0; |
| 3487 | goto out; |
| 3488 | } |
| 3489 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3490 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3491 | if (!bctl) { |
| 3492 | ret = -ENOMEM; |
| 3493 | goto out; |
| 3494 | } |
| 3495 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3496 | leaf = path->nodes[0]; |
| 3497 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3498 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3499 | bctl->fs_info = fs_info; |
| 3500 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3501 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3502 | |
| 3503 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3504 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3505 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3506 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3507 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3508 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3509 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3510 | WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); |
| 3511 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3512 | mutex_lock(&fs_info->volume_mutex); |
| 3513 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3514 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3515 | set_balance_control(bctl); |
| 3516 | |
| 3517 | mutex_unlock(&fs_info->balance_mutex); |
| 3518 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3519 | out: |
| 3520 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3521 | return ret; |
| 3522 | } |
| 3523 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3524 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3525 | { |
| 3526 | int ret = 0; |
| 3527 | |
| 3528 | mutex_lock(&fs_info->balance_mutex); |
| 3529 | if (!fs_info->balance_ctl) { |
| 3530 | mutex_unlock(&fs_info->balance_mutex); |
| 3531 | return -ENOTCONN; |
| 3532 | } |
| 3533 | |
| 3534 | if (atomic_read(&fs_info->balance_running)) { |
| 3535 | atomic_inc(&fs_info->balance_pause_req); |
| 3536 | mutex_unlock(&fs_info->balance_mutex); |
| 3537 | |
| 3538 | wait_event(fs_info->balance_wait_q, |
| 3539 | atomic_read(&fs_info->balance_running) == 0); |
| 3540 | |
| 3541 | mutex_lock(&fs_info->balance_mutex); |
| 3542 | /* we are good with balance_ctl ripped off from under us */ |
| 3543 | BUG_ON(atomic_read(&fs_info->balance_running)); |
| 3544 | atomic_dec(&fs_info->balance_pause_req); |
| 3545 | } else { |
| 3546 | ret = -ENOTCONN; |
| 3547 | } |
| 3548 | |
| 3549 | mutex_unlock(&fs_info->balance_mutex); |
| 3550 | return ret; |
| 3551 | } |
| 3552 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3553 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 3554 | { |
Ilya Dryomov | e649e58 | 2013-10-10 20:40:21 +0300 | [diff] [blame] | 3555 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 3556 | return -EROFS; |
| 3557 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3558 | mutex_lock(&fs_info->balance_mutex); |
| 3559 | if (!fs_info->balance_ctl) { |
| 3560 | mutex_unlock(&fs_info->balance_mutex); |
| 3561 | return -ENOTCONN; |
| 3562 | } |
| 3563 | |
| 3564 | atomic_inc(&fs_info->balance_cancel_req); |
| 3565 | /* |
| 3566 | * if we are running just wait and return, balance item is |
| 3567 | * deleted in btrfs_balance in this case |
| 3568 | */ |
| 3569 | if (atomic_read(&fs_info->balance_running)) { |
| 3570 | mutex_unlock(&fs_info->balance_mutex); |
| 3571 | wait_event(fs_info->balance_wait_q, |
| 3572 | atomic_read(&fs_info->balance_running) == 0); |
| 3573 | mutex_lock(&fs_info->balance_mutex); |
| 3574 | } else { |
| 3575 | /* __cancel_balance needs volume_mutex */ |
| 3576 | mutex_unlock(&fs_info->balance_mutex); |
| 3577 | mutex_lock(&fs_info->volume_mutex); |
| 3578 | mutex_lock(&fs_info->balance_mutex); |
| 3579 | |
| 3580 | if (fs_info->balance_ctl) |
| 3581 | __cancel_balance(fs_info); |
| 3582 | |
| 3583 | mutex_unlock(&fs_info->volume_mutex); |
| 3584 | } |
| 3585 | |
| 3586 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); |
| 3587 | atomic_dec(&fs_info->balance_cancel_req); |
| 3588 | mutex_unlock(&fs_info->balance_mutex); |
| 3589 | return 0; |
| 3590 | } |
| 3591 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3592 | static int btrfs_uuid_scan_kthread(void *data) |
| 3593 | { |
| 3594 | struct btrfs_fs_info *fs_info = data; |
| 3595 | struct btrfs_root *root = fs_info->tree_root; |
| 3596 | struct btrfs_key key; |
| 3597 | struct btrfs_key max_key; |
| 3598 | struct btrfs_path *path = NULL; |
| 3599 | int ret = 0; |
| 3600 | struct extent_buffer *eb; |
| 3601 | int slot; |
| 3602 | struct btrfs_root_item root_item; |
| 3603 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3604 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3605 | |
| 3606 | path = btrfs_alloc_path(); |
| 3607 | if (!path) { |
| 3608 | ret = -ENOMEM; |
| 3609 | goto out; |
| 3610 | } |
| 3611 | |
| 3612 | key.objectid = 0; |
| 3613 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3614 | key.offset = 0; |
| 3615 | |
| 3616 | max_key.objectid = (u64)-1; |
| 3617 | max_key.type = BTRFS_ROOT_ITEM_KEY; |
| 3618 | max_key.offset = (u64)-1; |
| 3619 | |
| 3620 | path->keep_locks = 1; |
| 3621 | |
| 3622 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3623 | ret = btrfs_search_forward(root, &key, path, 0); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3624 | if (ret) { |
| 3625 | if (ret > 0) |
| 3626 | ret = 0; |
| 3627 | break; |
| 3628 | } |
| 3629 | |
| 3630 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 3631 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3632 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 3633 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 3634 | goto skip; |
| 3635 | |
| 3636 | eb = path->nodes[0]; |
| 3637 | slot = path->slots[0]; |
| 3638 | item_size = btrfs_item_size_nr(eb, slot); |
| 3639 | if (item_size < sizeof(root_item)) |
| 3640 | goto skip; |
| 3641 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3642 | read_extent_buffer(eb, &root_item, |
| 3643 | btrfs_item_ptr_offset(eb, slot), |
| 3644 | (int)sizeof(root_item)); |
| 3645 | if (btrfs_root_refs(&root_item) == 0) |
| 3646 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3647 | |
| 3648 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 3649 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 3650 | if (trans) |
| 3651 | goto update_tree; |
| 3652 | |
| 3653 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3654 | /* |
| 3655 | * 1 - subvol uuid item |
| 3656 | * 1 - received_subvol uuid item |
| 3657 | */ |
| 3658 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 3659 | if (IS_ERR(trans)) { |
| 3660 | ret = PTR_ERR(trans); |
| 3661 | break; |
| 3662 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3663 | continue; |
| 3664 | } else { |
| 3665 | goto skip; |
| 3666 | } |
| 3667 | update_tree: |
| 3668 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3669 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3670 | root_item.uuid, |
| 3671 | BTRFS_UUID_KEY_SUBVOL, |
| 3672 | key.objectid); |
| 3673 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3674 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3675 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3676 | break; |
| 3677 | } |
| 3678 | } |
| 3679 | |
| 3680 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3681 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3682 | root_item.received_uuid, |
| 3683 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 3684 | key.objectid); |
| 3685 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3686 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3687 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3688 | break; |
| 3689 | } |
| 3690 | } |
| 3691 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3692 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3693 | if (trans) { |
| 3694 | ret = btrfs_end_transaction(trans, fs_info->uuid_root); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3695 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3696 | if (ret) |
| 3697 | break; |
| 3698 | } |
| 3699 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3700 | btrfs_release_path(path); |
| 3701 | if (key.offset < (u64)-1) { |
| 3702 | key.offset++; |
| 3703 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 3704 | key.offset = 0; |
| 3705 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3706 | } else if (key.objectid < (u64)-1) { |
| 3707 | key.offset = 0; |
| 3708 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3709 | key.objectid++; |
| 3710 | } else { |
| 3711 | break; |
| 3712 | } |
| 3713 | cond_resched(); |
| 3714 | } |
| 3715 | |
| 3716 | out: |
| 3717 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3718 | if (trans && !IS_ERR(trans)) |
| 3719 | btrfs_end_transaction(trans, fs_info->uuid_root); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3720 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3721 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3722 | else |
| 3723 | fs_info->update_uuid_tree_gen = 1; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3724 | up(&fs_info->uuid_tree_rescan_sem); |
| 3725 | return 0; |
| 3726 | } |
| 3727 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3728 | /* |
| 3729 | * Callback for btrfs_uuid_tree_iterate(). |
| 3730 | * returns: |
| 3731 | * 0 check succeeded, the entry is not outdated. |
| 3732 | * < 0 if an error occured. |
| 3733 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 3734 | */ |
| 3735 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 3736 | u8 *uuid, u8 type, u64 subid) |
| 3737 | { |
| 3738 | struct btrfs_key key; |
| 3739 | int ret = 0; |
| 3740 | struct btrfs_root *subvol_root; |
| 3741 | |
| 3742 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 3743 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 3744 | goto out; |
| 3745 | |
| 3746 | key.objectid = subid; |
| 3747 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3748 | key.offset = (u64)-1; |
| 3749 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 3750 | if (IS_ERR(subvol_root)) { |
| 3751 | ret = PTR_ERR(subvol_root); |
| 3752 | if (ret == -ENOENT) |
| 3753 | ret = 1; |
| 3754 | goto out; |
| 3755 | } |
| 3756 | |
| 3757 | switch (type) { |
| 3758 | case BTRFS_UUID_KEY_SUBVOL: |
| 3759 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 3760 | ret = 1; |
| 3761 | break; |
| 3762 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 3763 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 3764 | BTRFS_UUID_SIZE)) |
| 3765 | ret = 1; |
| 3766 | break; |
| 3767 | } |
| 3768 | |
| 3769 | out: |
| 3770 | return ret; |
| 3771 | } |
| 3772 | |
| 3773 | static int btrfs_uuid_rescan_kthread(void *data) |
| 3774 | { |
| 3775 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 3776 | int ret; |
| 3777 | |
| 3778 | /* |
| 3779 | * 1st step is to iterate through the existing UUID tree and |
| 3780 | * to delete all entries that contain outdated data. |
| 3781 | * 2nd step is to add all missing entries to the UUID tree. |
| 3782 | */ |
| 3783 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 3784 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3785 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3786 | up(&fs_info->uuid_tree_rescan_sem); |
| 3787 | return ret; |
| 3788 | } |
| 3789 | return btrfs_uuid_scan_kthread(data); |
| 3790 | } |
| 3791 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3792 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 3793 | { |
| 3794 | struct btrfs_trans_handle *trans; |
| 3795 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 3796 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3797 | struct task_struct *task; |
| 3798 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3799 | |
| 3800 | /* |
| 3801 | * 1 - root node |
| 3802 | * 1 - root item |
| 3803 | */ |
| 3804 | trans = btrfs_start_transaction(tree_root, 2); |
| 3805 | if (IS_ERR(trans)) |
| 3806 | return PTR_ERR(trans); |
| 3807 | |
| 3808 | uuid_root = btrfs_create_tree(trans, fs_info, |
| 3809 | BTRFS_UUID_TREE_OBJECTID); |
| 3810 | if (IS_ERR(uuid_root)) { |
| 3811 | btrfs_abort_transaction(trans, tree_root, |
| 3812 | PTR_ERR(uuid_root)); |
| 3813 | return PTR_ERR(uuid_root); |
| 3814 | } |
| 3815 | |
| 3816 | fs_info->uuid_root = uuid_root; |
| 3817 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3818 | ret = btrfs_commit_transaction(trans, tree_root); |
| 3819 | if (ret) |
| 3820 | return ret; |
| 3821 | |
| 3822 | down(&fs_info->uuid_tree_rescan_sem); |
| 3823 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 3824 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3825 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3826 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3827 | up(&fs_info->uuid_tree_rescan_sem); |
| 3828 | return PTR_ERR(task); |
| 3829 | } |
| 3830 | |
| 3831 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3832 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3833 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3834 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 3835 | { |
| 3836 | struct task_struct *task; |
| 3837 | |
| 3838 | down(&fs_info->uuid_tree_rescan_sem); |
| 3839 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 3840 | if (IS_ERR(task)) { |
| 3841 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3842 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3843 | up(&fs_info->uuid_tree_rescan_sem); |
| 3844 | return PTR_ERR(task); |
| 3845 | } |
| 3846 | |
| 3847 | return 0; |
| 3848 | } |
| 3849 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3850 | /* |
| 3851 | * shrinking a device means finding all of the device extents past |
| 3852 | * the new size, and then following the back refs to the chunks. |
| 3853 | * The chunk relocation code actually frees the device extent |
| 3854 | */ |
| 3855 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 3856 | { |
| 3857 | struct btrfs_trans_handle *trans; |
| 3858 | struct btrfs_root *root = device->dev_root; |
| 3859 | struct btrfs_dev_extent *dev_extent = NULL; |
| 3860 | struct btrfs_path *path; |
| 3861 | u64 length; |
| 3862 | u64 chunk_tree; |
| 3863 | u64 chunk_objectid; |
| 3864 | u64 chunk_offset; |
| 3865 | int ret; |
| 3866 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3867 | int failed = 0; |
| 3868 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3869 | struct extent_buffer *l; |
| 3870 | struct btrfs_key key; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3871 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3872 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3873 | u64 old_size = device->total_bytes; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3874 | u64 diff = device->total_bytes - new_size; |
| 3875 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3876 | if (device->is_tgtdev_for_dev_replace) |
| 3877 | return -EINVAL; |
| 3878 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3879 | path = btrfs_alloc_path(); |
| 3880 | if (!path) |
| 3881 | return -ENOMEM; |
| 3882 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3883 | path->reada = 2; |
| 3884 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3885 | lock_chunks(root); |
| 3886 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3887 | device->total_bytes = new_size; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3888 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3889 | device->fs_devices->total_rw_bytes -= diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3890 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3891 | root->fs_info->free_chunk_space -= diff; |
| 3892 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 3893 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3894 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3895 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3896 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3897 | key.objectid = device->devid; |
| 3898 | key.offset = (u64)-1; |
| 3899 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 3900 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 3901 | do { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3902 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3903 | if (ret < 0) |
| 3904 | goto done; |
| 3905 | |
| 3906 | ret = btrfs_previous_item(root, path, 0, key.type); |
| 3907 | if (ret < 0) |
| 3908 | goto done; |
| 3909 | if (ret) { |
| 3910 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3911 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 3912 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3913 | } |
| 3914 | |
| 3915 | l = path->nodes[0]; |
| 3916 | slot = path->slots[0]; |
| 3917 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 3918 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3919 | if (key.objectid != device->devid) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3920 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 3921 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3922 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3923 | |
| 3924 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 3925 | length = btrfs_dev_extent_length(l, dev_extent); |
| 3926 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3927 | if (key.offset + length <= new_size) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3928 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3929 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3930 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3931 | |
| 3932 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); |
| 3933 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); |
| 3934 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3935 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3936 | |
| 3937 | ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid, |
| 3938 | chunk_offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3939 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3940 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3941 | if (ret == -ENOSPC) |
| 3942 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 3943 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3944 | |
| 3945 | if (failed && !retried) { |
| 3946 | failed = 0; |
| 3947 | retried = true; |
| 3948 | goto again; |
| 3949 | } else if (failed && retried) { |
| 3950 | ret = -ENOSPC; |
| 3951 | lock_chunks(root); |
| 3952 | |
| 3953 | device->total_bytes = old_size; |
| 3954 | if (device->writeable) |
| 3955 | device->fs_devices->total_rw_bytes += diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3956 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3957 | root->fs_info->free_chunk_space += diff; |
| 3958 | spin_unlock(&root->fs_info->free_chunk_lock); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3959 | unlock_chunks(root); |
| 3960 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3961 | } |
| 3962 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3963 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3964 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3965 | if (IS_ERR(trans)) { |
| 3966 | ret = PTR_ERR(trans); |
| 3967 | goto done; |
| 3968 | } |
| 3969 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3970 | lock_chunks(root); |
| 3971 | |
| 3972 | device->disk_total_bytes = new_size; |
| 3973 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 3974 | ret = btrfs_update_device(trans, device); |
| 3975 | if (ret) { |
| 3976 | unlock_chunks(root); |
| 3977 | btrfs_end_transaction(trans, root); |
| 3978 | goto done; |
| 3979 | } |
| 3980 | WARN_ON(diff > old_total); |
| 3981 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
| 3982 | unlock_chunks(root); |
| 3983 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3984 | done: |
| 3985 | btrfs_free_path(path); |
| 3986 | return ret; |
| 3987 | } |
| 3988 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 3989 | static int btrfs_add_system_chunk(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3990 | struct btrfs_key *key, |
| 3991 | struct btrfs_chunk *chunk, int item_size) |
| 3992 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3993 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3994 | struct btrfs_disk_key disk_key; |
| 3995 | u32 array_size; |
| 3996 | u8 *ptr; |
| 3997 | |
| 3998 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 3999 | if (array_size + item_size + sizeof(disk_key) |
| 4000 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4001 | return -EFBIG; |
| 4002 | |
| 4003 | ptr = super_copy->sys_chunk_array + array_size; |
| 4004 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4005 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4006 | ptr += sizeof(disk_key); |
| 4007 | memcpy(ptr, chunk, item_size); |
| 4008 | item_size += sizeof(disk_key); |
| 4009 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
| 4010 | return 0; |
| 4011 | } |
| 4012 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4013 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4014 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4015 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4016 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4017 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4018 | const struct btrfs_device_info *di_a = a; |
| 4019 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4020 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4021 | if (di_a->max_avail > di_b->max_avail) |
| 4022 | return -1; |
| 4023 | if (di_a->max_avail < di_b->max_avail) |
| 4024 | return 1; |
| 4025 | if (di_a->total_avail > di_b->total_avail) |
| 4026 | return -1; |
| 4027 | if (di_a->total_avail < di_b->total_avail) |
| 4028 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4029 | return 0; |
| 4030 | } |
| 4031 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4032 | static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 4033 | [BTRFS_RAID_RAID10] = { |
| 4034 | .sub_stripes = 2, |
| 4035 | .dev_stripes = 1, |
| 4036 | .devs_max = 0, /* 0 == as many as possible */ |
| 4037 | .devs_min = 4, |
| 4038 | .devs_increment = 2, |
| 4039 | .ncopies = 2, |
| 4040 | }, |
| 4041 | [BTRFS_RAID_RAID1] = { |
| 4042 | .sub_stripes = 1, |
| 4043 | .dev_stripes = 1, |
| 4044 | .devs_max = 2, |
| 4045 | .devs_min = 2, |
| 4046 | .devs_increment = 2, |
| 4047 | .ncopies = 2, |
| 4048 | }, |
| 4049 | [BTRFS_RAID_DUP] = { |
| 4050 | .sub_stripes = 1, |
| 4051 | .dev_stripes = 2, |
| 4052 | .devs_max = 1, |
| 4053 | .devs_min = 1, |
| 4054 | .devs_increment = 1, |
| 4055 | .ncopies = 2, |
| 4056 | }, |
| 4057 | [BTRFS_RAID_RAID0] = { |
| 4058 | .sub_stripes = 1, |
| 4059 | .dev_stripes = 1, |
| 4060 | .devs_max = 0, |
| 4061 | .devs_min = 2, |
| 4062 | .devs_increment = 1, |
| 4063 | .ncopies = 1, |
| 4064 | }, |
| 4065 | [BTRFS_RAID_SINGLE] = { |
| 4066 | .sub_stripes = 1, |
| 4067 | .dev_stripes = 1, |
| 4068 | .devs_max = 1, |
| 4069 | .devs_min = 1, |
| 4070 | .devs_increment = 1, |
| 4071 | .ncopies = 1, |
| 4072 | }, |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 4073 | [BTRFS_RAID_RAID5] = { |
| 4074 | .sub_stripes = 1, |
| 4075 | .dev_stripes = 1, |
| 4076 | .devs_max = 0, |
| 4077 | .devs_min = 2, |
| 4078 | .devs_increment = 1, |
| 4079 | .ncopies = 2, |
| 4080 | }, |
| 4081 | [BTRFS_RAID_RAID6] = { |
| 4082 | .sub_stripes = 1, |
| 4083 | .dev_stripes = 1, |
| 4084 | .devs_max = 0, |
| 4085 | .devs_min = 3, |
| 4086 | .devs_increment = 1, |
| 4087 | .ncopies = 3, |
| 4088 | }, |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4089 | }; |
| 4090 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4091 | static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target) |
| 4092 | { |
| 4093 | /* TODO allow them to set a preferred stripe size */ |
| 4094 | return 64 * 1024; |
| 4095 | } |
| 4096 | |
| 4097 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4098 | { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4099 | if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))) |
| 4100 | return; |
| 4101 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4102 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4103 | } |
| 4104 | |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4105 | #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \ |
| 4106 | - sizeof(struct btrfs_item) \ |
| 4107 | - sizeof(struct btrfs_chunk)) \ |
| 4108 | / sizeof(struct btrfs_stripe) + 1) |
| 4109 | |
| 4110 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ |
| 4111 | - 2 * sizeof(struct btrfs_disk_key) \ |
| 4112 | - 2 * sizeof(struct btrfs_chunk)) \ |
| 4113 | / sizeof(struct btrfs_stripe) + 1) |
| 4114 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4115 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4116 | struct btrfs_root *extent_root, u64 start, |
| 4117 | u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4118 | { |
| 4119 | struct btrfs_fs_info *info = extent_root->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4120 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
| 4121 | struct list_head *cur; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4122 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4123 | struct extent_map_tree *em_tree; |
| 4124 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4125 | struct btrfs_device_info *devices_info = NULL; |
| 4126 | u64 total_avail; |
| 4127 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4128 | int data_stripes; /* number of stripes that count for |
| 4129 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4130 | int sub_stripes; /* sub_stripes info for map */ |
| 4131 | int dev_stripes; /* stripes per dev */ |
| 4132 | int devs_max; /* max devs to use */ |
| 4133 | int devs_min; /* min devs needed */ |
| 4134 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4135 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4136 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4137 | u64 max_stripe_size; |
| 4138 | u64 max_chunk_size; |
| 4139 | u64 stripe_size; |
| 4140 | u64 num_bytes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4141 | u64 raid_stripe_len = BTRFS_STRIPE_LEN; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4142 | int ndevs; |
| 4143 | int i; |
| 4144 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4145 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4146 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4147 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4148 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4149 | if (list_empty(&fs_devices->alloc_list)) |
| 4150 | return -ENOSPC; |
| 4151 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4152 | index = __get_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4153 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4154 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4155 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4156 | devs_max = btrfs_raid_array[index].devs_max; |
| 4157 | devs_min = btrfs_raid_array[index].devs_min; |
| 4158 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4159 | ncopies = btrfs_raid_array[index].ncopies; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4160 | |
| 4161 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 4162 | max_stripe_size = 1024 * 1024 * 1024; |
| 4163 | max_chunk_size = 10 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4164 | if (!devs_max) |
| 4165 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4166 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4167 | /* for larger filesystems, use larger metadata chunks */ |
| 4168 | if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024) |
| 4169 | max_stripe_size = 1024 * 1024 * 1024; |
| 4170 | else |
| 4171 | max_stripe_size = 256 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4172 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4173 | if (!devs_max) |
| 4174 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4175 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Chris Mason | 96bdc7d | 2012-01-16 08:13:11 -0500 | [diff] [blame] | 4176 | max_stripe_size = 32 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4177 | max_chunk_size = 2 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4178 | if (!devs_max) |
| 4179 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4180 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4181 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4182 | type); |
| 4183 | BUG_ON(1); |
| 4184 | } |
| 4185 | |
| 4186 | /* we don't want a chunk larger than 10% of writeable space */ |
| 4187 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4188 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4189 | |
| 4190 | devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices, |
| 4191 | GFP_NOFS); |
| 4192 | if (!devices_info) |
| 4193 | return -ENOMEM; |
| 4194 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4195 | cur = fs_devices->alloc_list.next; |
| 4196 | |
| 4197 | /* |
| 4198 | * in the first pass through the devices list, we gather information |
| 4199 | * about the available holes on each device. |
| 4200 | */ |
| 4201 | ndevs = 0; |
| 4202 | while (cur != &fs_devices->alloc_list) { |
| 4203 | struct btrfs_device *device; |
| 4204 | u64 max_avail; |
| 4205 | u64 dev_offset; |
| 4206 | |
| 4207 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
| 4208 | |
| 4209 | cur = cur->next; |
| 4210 | |
| 4211 | if (!device->writeable) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4212 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4213 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4214 | continue; |
| 4215 | } |
| 4216 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4217 | if (!device->in_fs_metadata || |
| 4218 | device->is_tgtdev_for_dev_replace) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4219 | continue; |
| 4220 | |
| 4221 | if (device->total_bytes > device->bytes_used) |
| 4222 | total_avail = device->total_bytes - device->bytes_used; |
| 4223 | else |
| 4224 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4225 | |
| 4226 | /* If there is no space on this device, skip it. */ |
| 4227 | if (total_avail == 0) |
| 4228 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4229 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4230 | ret = find_free_dev_extent(trans, device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4231 | max_stripe_size * dev_stripes, |
| 4232 | &dev_offset, &max_avail); |
| 4233 | if (ret && ret != -ENOSPC) |
| 4234 | goto error; |
| 4235 | |
| 4236 | if (ret == 0) |
| 4237 | max_avail = max_stripe_size * dev_stripes; |
| 4238 | |
| 4239 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
| 4240 | continue; |
| 4241 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4242 | if (ndevs == fs_devices->rw_devices) { |
| 4243 | WARN(1, "%s: found more than %llu devices\n", |
| 4244 | __func__, fs_devices->rw_devices); |
| 4245 | break; |
| 4246 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4247 | devices_info[ndevs].dev_offset = dev_offset; |
| 4248 | devices_info[ndevs].max_avail = max_avail; |
| 4249 | devices_info[ndevs].total_avail = total_avail; |
| 4250 | devices_info[ndevs].dev = device; |
| 4251 | ++ndevs; |
| 4252 | } |
| 4253 | |
| 4254 | /* |
| 4255 | * now sort the devices by hole size / available space |
| 4256 | */ |
| 4257 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4258 | btrfs_cmp_device_info, NULL); |
| 4259 | |
| 4260 | /* round down to number of usable stripes */ |
| 4261 | ndevs -= ndevs % devs_increment; |
| 4262 | |
| 4263 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
| 4264 | ret = -ENOSPC; |
| 4265 | goto error; |
| 4266 | } |
| 4267 | |
| 4268 | if (devs_max && ndevs > devs_max) |
| 4269 | ndevs = devs_max; |
| 4270 | /* |
| 4271 | * the primary goal is to maximize the number of stripes, so use as many |
| 4272 | * devices as possible, even if the stripes are not maximum sized. |
| 4273 | */ |
| 4274 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4275 | num_stripes = ndevs * dev_stripes; |
| 4276 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4277 | /* |
| 4278 | * this will have to be fixed for RAID1 and RAID10 over |
| 4279 | * more drives |
| 4280 | */ |
| 4281 | data_stripes = num_stripes / ncopies; |
| 4282 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4283 | if (type & BTRFS_BLOCK_GROUP_RAID5) { |
| 4284 | raid_stripe_len = find_raid56_stripe_len(ndevs - 1, |
| 4285 | btrfs_super_stripesize(info->super_copy)); |
| 4286 | data_stripes = num_stripes - 1; |
| 4287 | } |
| 4288 | if (type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4289 | raid_stripe_len = find_raid56_stripe_len(ndevs - 2, |
| 4290 | btrfs_super_stripesize(info->super_copy)); |
| 4291 | data_stripes = num_stripes - 2; |
| 4292 | } |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4293 | |
| 4294 | /* |
| 4295 | * Use the number of data stripes to figure out how big this chunk |
| 4296 | * is really going to be in terms of logical address space, |
| 4297 | * and compare that answer with the max chunk size |
| 4298 | */ |
| 4299 | if (stripe_size * data_stripes > max_chunk_size) { |
| 4300 | u64 mask = (1ULL << 24) - 1; |
| 4301 | stripe_size = max_chunk_size; |
| 4302 | do_div(stripe_size, data_stripes); |
| 4303 | |
| 4304 | /* bump the answer up to a 16MB boundary */ |
| 4305 | stripe_size = (stripe_size + mask) & ~mask; |
| 4306 | |
| 4307 | /* but don't go higher than the limits we found |
| 4308 | * while searching for free extents |
| 4309 | */ |
| 4310 | if (stripe_size > devices_info[ndevs-1].max_avail) |
| 4311 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4312 | } |
| 4313 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4314 | do_div(stripe_size, dev_stripes); |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 4315 | |
| 4316 | /* align to BTRFS_STRIPE_LEN */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4317 | do_div(stripe_size, raid_stripe_len); |
| 4318 | stripe_size *= raid_stripe_len; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4319 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4320 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 4321 | if (!map) { |
| 4322 | ret = -ENOMEM; |
| 4323 | goto error; |
| 4324 | } |
| 4325 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 4326 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4327 | for (i = 0; i < ndevs; ++i) { |
| 4328 | for (j = 0; j < dev_stripes; ++j) { |
| 4329 | int s = i * dev_stripes + j; |
| 4330 | map->stripes[s].dev = devices_info[i].dev; |
| 4331 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 4332 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 4333 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4334 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4335 | map->sector_size = extent_root->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4336 | map->stripe_len = raid_stripe_len; |
| 4337 | map->io_align = raid_stripe_len; |
| 4338 | map->io_width = raid_stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4339 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4340 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4341 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4342 | num_bytes = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4343 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4344 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4345 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4346 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4347 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4348 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4349 | ret = -ENOMEM; |
| 4350 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4351 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4352 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4353 | em->bdev = (struct block_device *)map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4354 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4355 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4356 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4357 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4358 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4359 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4360 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4361 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4362 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4363 | if (!ret) { |
| 4364 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
| 4365 | atomic_inc(&em->refs); |
| 4366 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4367 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4368 | if (ret) { |
| 4369 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 4370 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4371 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4372 | |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 4373 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 4374 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4375 | start, num_bytes); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4376 | if (ret) |
| 4377 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4378 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4379 | free_extent_map(em); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4380 | check_raid56_incompat_flag(extent_root->fs_info, type); |
| 4381 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4382 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4383 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4384 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4385 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4386 | write_lock(&em_tree->lock); |
| 4387 | remove_extent_mapping(em_tree, em); |
| 4388 | write_unlock(&em_tree->lock); |
| 4389 | |
| 4390 | /* One for our allocation */ |
| 4391 | free_extent_map(em); |
| 4392 | /* One for the tree reference */ |
| 4393 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4394 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4395 | kfree(devices_info); |
| 4396 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4397 | } |
| 4398 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4399 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4400 | struct btrfs_root *extent_root, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4401 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4402 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4403 | struct btrfs_key key; |
| 4404 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 4405 | struct btrfs_device *device; |
| 4406 | struct btrfs_chunk *chunk; |
| 4407 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4408 | struct extent_map_tree *em_tree; |
| 4409 | struct extent_map *em; |
| 4410 | struct map_lookup *map; |
| 4411 | size_t item_size; |
| 4412 | u64 dev_offset; |
| 4413 | u64 stripe_size; |
| 4414 | int i = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4415 | int ret; |
| 4416 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4417 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
| 4418 | read_lock(&em_tree->lock); |
| 4419 | em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size); |
| 4420 | read_unlock(&em_tree->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4421 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4422 | if (!em) { |
| 4423 | btrfs_crit(extent_root->fs_info, "unable to find logical " |
| 4424 | "%Lu len %Lu", chunk_offset, chunk_size); |
| 4425 | return -EINVAL; |
| 4426 | } |
| 4427 | |
| 4428 | if (em->start != chunk_offset || em->len != chunk_size) { |
| 4429 | btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted" |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4430 | " %Lu-%Lu, found %Lu-%Lu", chunk_offset, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4431 | chunk_size, em->start, em->len); |
| 4432 | free_extent_map(em); |
| 4433 | return -EINVAL; |
| 4434 | } |
| 4435 | |
| 4436 | map = (struct map_lookup *)em->bdev; |
| 4437 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 4438 | stripe_size = em->orig_block_len; |
| 4439 | |
| 4440 | chunk = kzalloc(item_size, GFP_NOFS); |
| 4441 | if (!chunk) { |
| 4442 | ret = -ENOMEM; |
| 4443 | goto out; |
| 4444 | } |
| 4445 | |
| 4446 | for (i = 0; i < map->num_stripes; i++) { |
| 4447 | device = map->stripes[i].dev; |
| 4448 | dev_offset = map->stripes[i].physical; |
| 4449 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4450 | device->bytes_used += stripe_size; |
| 4451 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 4452 | if (ret) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4453 | goto out; |
| 4454 | ret = btrfs_alloc_dev_extent(trans, device, |
| 4455 | chunk_root->root_key.objectid, |
| 4456 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4457 | chunk_offset, dev_offset, |
| 4458 | stripe_size); |
| 4459 | if (ret) |
| 4460 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4461 | } |
| 4462 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4463 | spin_lock(&extent_root->fs_info->free_chunk_lock); |
| 4464 | extent_root->fs_info->free_chunk_space -= (stripe_size * |
| 4465 | map->num_stripes); |
| 4466 | spin_unlock(&extent_root->fs_info->free_chunk_lock); |
| 4467 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4468 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4469 | for (i = 0; i < map->num_stripes; i++) { |
| 4470 | device = map->stripes[i].dev; |
| 4471 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4472 | |
| 4473 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 4474 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 4475 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 4476 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4477 | } |
| 4478 | |
| 4479 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 4480 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 4481 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 4482 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 4483 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 4484 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 4485 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 4486 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 4487 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 4488 | |
| 4489 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4490 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4491 | key.offset = chunk_offset; |
| 4492 | |
| 4493 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4494 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4495 | /* |
| 4496 | * TODO: Cleanup of inserted chunk root in case of |
| 4497 | * failure. |
| 4498 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4499 | ret = btrfs_add_system_chunk(chunk_root, &key, chunk, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4500 | item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4501 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4502 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4503 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4504 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4505 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4506 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4507 | } |
| 4508 | |
| 4509 | /* |
| 4510 | * Chunk allocation falls into two parts. The first part does works |
| 4511 | * that make the new allocated chunk useable, but not do any operation |
| 4512 | * that modifies the chunk tree. The second part does the works that |
| 4513 | * require modifying the chunk tree. This division is important for the |
| 4514 | * bootstrap process of adding storage to a seed btrfs. |
| 4515 | */ |
| 4516 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 4517 | struct btrfs_root *extent_root, u64 type) |
| 4518 | { |
| 4519 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4520 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4521 | chunk_offset = find_next_chunk(extent_root->fs_info); |
| 4522 | return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4523 | } |
| 4524 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4525 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4526 | struct btrfs_root *root, |
| 4527 | struct btrfs_device *device) |
| 4528 | { |
| 4529 | u64 chunk_offset; |
| 4530 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4531 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4532 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4533 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4534 | int ret; |
| 4535 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4536 | chunk_offset = find_next_chunk(fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4537 | alloc_profile = btrfs_get_alloc_profile(extent_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4538 | ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset, |
| 4539 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4540 | if (ret) |
| 4541 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4542 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4543 | sys_chunk_offset = find_next_chunk(root->fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4544 | alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4545 | ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset, |
| 4546 | alloc_profile); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4547 | if (ret) { |
| 4548 | btrfs_abort_transaction(trans, root, ret); |
| 4549 | goto out; |
| 4550 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4551 | |
| 4552 | ret = btrfs_add_device(trans, fs_info->chunk_root, device); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4553 | if (ret) |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4554 | btrfs_abort_transaction(trans, root, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4555 | out: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4556 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4557 | } |
| 4558 | |
| 4559 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 4560 | { |
| 4561 | struct extent_map *em; |
| 4562 | struct map_lookup *map; |
| 4563 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 4564 | int readonly = 0; |
| 4565 | int i; |
| 4566 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4567 | read_lock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4568 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4569 | read_unlock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4570 | if (!em) |
| 4571 | return 1; |
| 4572 | |
Josef Bacik | f48b907 | 2010-01-27 02:07:59 +0000 | [diff] [blame] | 4573 | if (btrfs_test_opt(root, DEGRADED)) { |
| 4574 | free_extent_map(em); |
| 4575 | return 0; |
| 4576 | } |
| 4577 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4578 | map = (struct map_lookup *)em->bdev; |
| 4579 | for (i = 0; i < map->num_stripes; i++) { |
| 4580 | if (!map->stripes[i].dev->writeable) { |
| 4581 | readonly = 1; |
| 4582 | break; |
| 4583 | } |
| 4584 | } |
| 4585 | free_extent_map(em); |
| 4586 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4587 | } |
| 4588 | |
| 4589 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 4590 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 4591 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4592 | } |
| 4593 | |
| 4594 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 4595 | { |
| 4596 | struct extent_map *em; |
| 4597 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4598 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4599 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4600 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 4601 | if (em) |
| 4602 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4603 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4604 | if (!em) |
| 4605 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4606 | /* once for us */ |
| 4607 | free_extent_map(em); |
| 4608 | /* once for the tree */ |
| 4609 | free_extent_map(em); |
| 4610 | } |
| 4611 | } |
| 4612 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4613 | 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] | 4614 | { |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4615 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4616 | struct extent_map *em; |
| 4617 | struct map_lookup *map; |
| 4618 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4619 | int ret; |
| 4620 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4621 | read_lock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4622 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4623 | read_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4624 | |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4625 | /* |
| 4626 | * We could return errors for these cases, but that could get ugly and |
| 4627 | * we'd probably do the same thing which is just not do anything else |
| 4628 | * and exit, so return 1 so the callers don't try to use other copies. |
| 4629 | */ |
| 4630 | if (!em) { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4631 | btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4632 | logical+len); |
| 4633 | return 1; |
| 4634 | } |
| 4635 | |
| 4636 | if (em->start > logical || em->start + em->len < logical) { |
David Sterba | ccf39f9 | 2013-07-11 15:41:11 +0200 | [diff] [blame] | 4637 | btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4638 | "%Lu-%Lu", logical, logical+len, em->start, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4639 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4640 | free_extent_map(em); |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4641 | return 1; |
| 4642 | } |
| 4643 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4644 | map = (struct map_lookup *)em->bdev; |
| 4645 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 4646 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4647 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4648 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4649 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 4650 | ret = 2; |
| 4651 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 4652 | ret = 3; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4653 | else |
| 4654 | ret = 1; |
| 4655 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4656 | |
| 4657 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 4658 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) |
| 4659 | ret++; |
| 4660 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
| 4661 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4662 | return ret; |
| 4663 | } |
| 4664 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4665 | unsigned long btrfs_full_stripe_len(struct btrfs_root *root, |
| 4666 | struct btrfs_mapping_tree *map_tree, |
| 4667 | u64 logical) |
| 4668 | { |
| 4669 | struct extent_map *em; |
| 4670 | struct map_lookup *map; |
| 4671 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4672 | unsigned long len = root->sectorsize; |
| 4673 | |
| 4674 | read_lock(&em_tree->lock); |
| 4675 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4676 | read_unlock(&em_tree->lock); |
| 4677 | BUG_ON(!em); |
| 4678 | |
| 4679 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4680 | map = (struct map_lookup *)em->bdev; |
| 4681 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 4682 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4683 | len = map->stripe_len * nr_data_stripes(map); |
| 4684 | } |
| 4685 | free_extent_map(em); |
| 4686 | return len; |
| 4687 | } |
| 4688 | |
| 4689 | int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree, |
| 4690 | u64 logical, u64 len, int mirror_num) |
| 4691 | { |
| 4692 | struct extent_map *em; |
| 4693 | struct map_lookup *map; |
| 4694 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4695 | int ret = 0; |
| 4696 | |
| 4697 | read_lock(&em_tree->lock); |
| 4698 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4699 | read_unlock(&em_tree->lock); |
| 4700 | BUG_ON(!em); |
| 4701 | |
| 4702 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4703 | map = (struct map_lookup *)em->bdev; |
| 4704 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 4705 | BTRFS_BLOCK_GROUP_RAID6)) |
| 4706 | ret = 1; |
| 4707 | free_extent_map(em); |
| 4708 | return ret; |
| 4709 | } |
| 4710 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4711 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
| 4712 | struct map_lookup *map, int first, int num, |
| 4713 | int optimal, int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4714 | { |
| 4715 | int i; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4716 | int tolerance; |
| 4717 | struct btrfs_device *srcdev; |
| 4718 | |
| 4719 | if (dev_replace_is_ongoing && |
| 4720 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 4721 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 4722 | srcdev = fs_info->dev_replace.srcdev; |
| 4723 | else |
| 4724 | srcdev = NULL; |
| 4725 | |
| 4726 | /* |
| 4727 | * try to avoid the drive that is the source drive for a |
| 4728 | * dev-replace procedure, only choose it if no other non-missing |
| 4729 | * mirror is available |
| 4730 | */ |
| 4731 | for (tolerance = 0; tolerance < 2; tolerance++) { |
| 4732 | if (map->stripes[optimal].dev->bdev && |
| 4733 | (tolerance || map->stripes[optimal].dev != srcdev)) |
| 4734 | return optimal; |
| 4735 | for (i = first; i < first + num; i++) { |
| 4736 | if (map->stripes[i].dev->bdev && |
| 4737 | (tolerance || map->stripes[i].dev != srcdev)) |
| 4738 | return i; |
| 4739 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4740 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4741 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4742 | /* we couldn't find one that doesn't fail. Just return something |
| 4743 | * and the io error handling code will clean up eventually |
| 4744 | */ |
| 4745 | return optimal; |
| 4746 | } |
| 4747 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4748 | static inline int parity_smaller(u64 a, u64 b) |
| 4749 | { |
| 4750 | return a > b; |
| 4751 | } |
| 4752 | |
| 4753 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
| 4754 | static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map) |
| 4755 | { |
| 4756 | struct btrfs_bio_stripe s; |
| 4757 | int i; |
| 4758 | u64 l; |
| 4759 | int again = 1; |
| 4760 | |
| 4761 | while (again) { |
| 4762 | again = 0; |
| 4763 | for (i = 0; i < bbio->num_stripes - 1; i++) { |
| 4764 | if (parity_smaller(raid_map[i], raid_map[i+1])) { |
| 4765 | s = bbio->stripes[i]; |
| 4766 | l = raid_map[i]; |
| 4767 | bbio->stripes[i] = bbio->stripes[i+1]; |
| 4768 | raid_map[i] = raid_map[i+1]; |
| 4769 | bbio->stripes[i+1] = s; |
| 4770 | raid_map[i+1] = l; |
| 4771 | again = 1; |
| 4772 | } |
| 4773 | } |
| 4774 | } |
| 4775 | } |
| 4776 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4777 | 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] | 4778 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4779 | struct btrfs_bio **bbio_ret, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4780 | int mirror_num, u64 **raid_map_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4781 | { |
| 4782 | struct extent_map *em; |
| 4783 | struct map_lookup *map; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4784 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4785 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4786 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4787 | u64 stripe_offset; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4788 | u64 stripe_end_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4789 | u64 stripe_nr; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4790 | u64 stripe_nr_orig; |
| 4791 | u64 stripe_nr_end; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4792 | u64 stripe_len; |
| 4793 | u64 *raid_map = NULL; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4794 | int stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4795 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4796 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4797 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 4798 | int max_errors = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4799 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4800 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 4801 | int dev_replace_is_ongoing = 0; |
| 4802 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4803 | int patch_the_first_stripe_for_dev_replace = 0; |
| 4804 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4805 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4806 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4807 | read_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4808 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4809 | read_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4810 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4811 | if (!em) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4812 | btrfs_crit(fs_info, "unable to find logical %llu len %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4813 | logical, *length); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4814 | return -EINVAL; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4815 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4816 | |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4817 | if (em->start > logical || em->start + em->len < logical) { |
| 4818 | btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4819 | "found %Lu-%Lu", logical, em->start, |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4820 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4821 | free_extent_map(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4822 | return -EINVAL; |
| 4823 | } |
| 4824 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4825 | map = (struct map_lookup *)em->bdev; |
| 4826 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4827 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4828 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4829 | stripe_nr = offset; |
| 4830 | /* |
| 4831 | * stripe_nr counts the total number of stripes we have to stride |
| 4832 | * to get to this block |
| 4833 | */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4834 | do_div(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4835 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4836 | stripe_offset = stripe_nr * stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4837 | BUG_ON(offset < stripe_offset); |
| 4838 | |
| 4839 | /* stripe_offset is the offset of this block in its stripe*/ |
| 4840 | stripe_offset = offset - stripe_offset; |
| 4841 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4842 | /* if we're here for raid56, we need to know the stripe aligned start */ |
| 4843 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4844 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 4845 | raid56_full_stripe_start = offset; |
| 4846 | |
| 4847 | /* allow a write of a full stripe, but make sure we don't |
| 4848 | * allow straddling of stripes |
| 4849 | */ |
| 4850 | do_div(raid56_full_stripe_start, full_stripe_len); |
| 4851 | raid56_full_stripe_start *= full_stripe_len; |
| 4852 | } |
| 4853 | |
| 4854 | if (rw & REQ_DISCARD) { |
| 4855 | /* we don't discard raid56 yet */ |
| 4856 | if (map->type & |
| 4857 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4858 | ret = -EOPNOTSUPP; |
| 4859 | goto out; |
| 4860 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4861 | *length = min_t(u64, em->len - offset, *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4862 | } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 4863 | u64 max_len; |
| 4864 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 4865 | For other RAID types and for RAID[56] reads, just allow a single |
| 4866 | stripe (on a single disk). */ |
| 4867 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) && |
| 4868 | (rw & REQ_WRITE)) { |
| 4869 | max_len = stripe_len * nr_data_stripes(map) - |
| 4870 | (offset - raid56_full_stripe_start); |
| 4871 | } else { |
| 4872 | /* we limit the length of each bio to what fits in a stripe */ |
| 4873 | max_len = stripe_len - stripe_offset; |
| 4874 | } |
| 4875 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4876 | } else { |
| 4877 | *length = em->len - offset; |
| 4878 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4879 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4880 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 4881 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4882 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4883 | goto out; |
| 4884 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4885 | btrfs_dev_replace_lock(dev_replace); |
| 4886 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 4887 | if (!dev_replace_is_ongoing) |
| 4888 | btrfs_dev_replace_unlock(dev_replace); |
| 4889 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4890 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
| 4891 | !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) && |
| 4892 | dev_replace->tgtdev != NULL) { |
| 4893 | /* |
| 4894 | * in dev-replace case, for repair case (that's the only |
| 4895 | * case where the mirror is selected explicitly when |
| 4896 | * calling btrfs_map_block), blocks left of the left cursor |
| 4897 | * can also be read from the target drive. |
| 4898 | * For REQ_GET_READ_MIRRORS, the target drive is added as |
| 4899 | * the last one to the array of stripes. For READ, it also |
| 4900 | * needs to be supported using the same mirror number. |
| 4901 | * If the requested block is not left of the left cursor, |
| 4902 | * EIO is returned. This can happen because btrfs_num_copies() |
| 4903 | * returns one more in the dev-replace case. |
| 4904 | */ |
| 4905 | u64 tmp_length = *length; |
| 4906 | struct btrfs_bio *tmp_bbio = NULL; |
| 4907 | int tmp_num_stripes; |
| 4908 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 4909 | int index_srcdev = 0; |
| 4910 | int found = 0; |
| 4911 | u64 physical_of_found = 0; |
| 4912 | |
| 4913 | ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4914 | logical, &tmp_length, &tmp_bbio, 0, NULL); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4915 | if (ret) { |
| 4916 | WARN_ON(tmp_bbio != NULL); |
| 4917 | goto out; |
| 4918 | } |
| 4919 | |
| 4920 | tmp_num_stripes = tmp_bbio->num_stripes; |
| 4921 | if (mirror_num > tmp_num_stripes) { |
| 4922 | /* |
| 4923 | * REQ_GET_READ_MIRRORS does not contain this |
| 4924 | * mirror, that means that the requested area |
| 4925 | * is not left of the left cursor |
| 4926 | */ |
| 4927 | ret = -EIO; |
| 4928 | kfree(tmp_bbio); |
| 4929 | goto out; |
| 4930 | } |
| 4931 | |
| 4932 | /* |
| 4933 | * process the rest of the function using the mirror_num |
| 4934 | * of the source drive. Therefore look it up first. |
| 4935 | * At the end, patch the device pointer to the one of the |
| 4936 | * target drive. |
| 4937 | */ |
| 4938 | for (i = 0; i < tmp_num_stripes; i++) { |
| 4939 | if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) { |
| 4940 | /* |
| 4941 | * In case of DUP, in order to keep it |
| 4942 | * simple, only add the mirror with the |
| 4943 | * lowest physical address |
| 4944 | */ |
| 4945 | if (found && |
| 4946 | physical_of_found <= |
| 4947 | tmp_bbio->stripes[i].physical) |
| 4948 | continue; |
| 4949 | index_srcdev = i; |
| 4950 | found = 1; |
| 4951 | physical_of_found = |
| 4952 | tmp_bbio->stripes[i].physical; |
| 4953 | } |
| 4954 | } |
| 4955 | |
| 4956 | if (found) { |
| 4957 | mirror_num = index_srcdev + 1; |
| 4958 | patch_the_first_stripe_for_dev_replace = 1; |
| 4959 | physical_to_patch_in_first_stripe = physical_of_found; |
| 4960 | } else { |
| 4961 | WARN_ON(1); |
| 4962 | ret = -EIO; |
| 4963 | kfree(tmp_bbio); |
| 4964 | goto out; |
| 4965 | } |
| 4966 | |
| 4967 | kfree(tmp_bbio); |
| 4968 | } else if (mirror_num > map->num_stripes) { |
| 4969 | mirror_num = 0; |
| 4970 | } |
| 4971 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4972 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4973 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4974 | stripe_nr_orig = stripe_nr; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4975 | stripe_nr_end = ALIGN(offset + *length, map->stripe_len); |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4976 | do_div(stripe_nr_end, map->stripe_len); |
| 4977 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 4978 | (offset + *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4979 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4980 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 4981 | if (rw & REQ_DISCARD) |
| 4982 | num_stripes = min_t(u64, map->num_stripes, |
| 4983 | stripe_nr_end - stripe_nr_orig); |
| 4984 | stripe_index = do_div(stripe_nr, map->num_stripes); |
| 4985 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 4986 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4987 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 4988 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4989 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4990 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4991 | stripe_index = find_live_mirror(fs_info, map, 0, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4992 | map->num_stripes, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4993 | current->pid % map->num_stripes, |
| 4994 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4995 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4996 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 4997 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4998 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 4999 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5000 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5001 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5002 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5003 | } else { |
| 5004 | mirror_num = 1; |
| 5005 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5006 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5007 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5008 | int factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5009 | |
| 5010 | stripe_index = do_div(stripe_nr, factor); |
| 5011 | stripe_index *= map->sub_stripes; |
| 5012 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5013 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5014 | num_stripes = map->sub_stripes; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5015 | else if (rw & REQ_DISCARD) |
| 5016 | num_stripes = min_t(u64, map->sub_stripes * |
| 5017 | (stripe_nr_end - stripe_nr_orig), |
| 5018 | map->num_stripes); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5019 | else if (mirror_num) |
| 5020 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5021 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5022 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5023 | stripe_index = find_live_mirror(fs_info, map, |
| 5024 | stripe_index, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5025 | map->sub_stripes, stripe_index + |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5026 | current->pid % map->sub_stripes, |
| 5027 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5028 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5029 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5030 | |
| 5031 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 5032 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 5033 | u64 tmp; |
| 5034 | |
| 5035 | if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1) |
| 5036 | && raid_map_ret) { |
| 5037 | int i, rot; |
| 5038 | |
| 5039 | /* push stripe_nr back to the start of the full stripe */ |
| 5040 | stripe_nr = raid56_full_stripe_start; |
| 5041 | do_div(stripe_nr, stripe_len); |
| 5042 | |
| 5043 | stripe_index = do_div(stripe_nr, nr_data_stripes(map)); |
| 5044 | |
| 5045 | /* RAID[56] write or recovery. Return all stripes */ |
| 5046 | num_stripes = map->num_stripes; |
| 5047 | max_errors = nr_parity_stripes(map); |
| 5048 | |
Dulshani Gunawardhana | d9b0d9b | 2013-10-31 10:32:18 +0530 | [diff] [blame] | 5049 | raid_map = kmalloc_array(num_stripes, sizeof(u64), |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5050 | GFP_NOFS); |
| 5051 | if (!raid_map) { |
| 5052 | ret = -ENOMEM; |
| 5053 | goto out; |
| 5054 | } |
| 5055 | |
| 5056 | /* Work out the disk rotation on this stripe-set */ |
| 5057 | tmp = stripe_nr; |
| 5058 | rot = do_div(tmp, num_stripes); |
| 5059 | |
| 5060 | /* Fill in the logical address of each stripe */ |
| 5061 | tmp = stripe_nr * nr_data_stripes(map); |
| 5062 | for (i = 0; i < nr_data_stripes(map); i++) |
| 5063 | raid_map[(i+rot) % num_stripes] = |
| 5064 | em->start + (tmp + i) * map->stripe_len; |
| 5065 | |
| 5066 | raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 5067 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 5068 | raid_map[(i+rot+1) % num_stripes] = |
| 5069 | RAID6_Q_STRIPE; |
| 5070 | |
| 5071 | *length = map->stripe_len; |
| 5072 | stripe_index = 0; |
| 5073 | stripe_offset = 0; |
| 5074 | } else { |
| 5075 | /* |
| 5076 | * Mirror #0 or #1 means the original data block. |
| 5077 | * Mirror #2 is RAID5 parity block. |
| 5078 | * Mirror #3 is RAID6 Q block. |
| 5079 | */ |
| 5080 | stripe_index = do_div(stripe_nr, nr_data_stripes(map)); |
| 5081 | if (mirror_num > 1) |
| 5082 | stripe_index = nr_data_stripes(map) + |
| 5083 | mirror_num - 2; |
| 5084 | |
| 5085 | /* We distribute the parity blocks across stripes */ |
| 5086 | tmp = stripe_nr + stripe_index; |
| 5087 | stripe_index = do_div(tmp, map->num_stripes); |
| 5088 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5089 | } else { |
| 5090 | /* |
| 5091 | * after this do_div call, stripe_nr is the number of stripes |
| 5092 | * on this device we have to walk to find the data, and |
| 5093 | * stripe_index is the number of our device in the stripe array |
| 5094 | */ |
| 5095 | stripe_index = do_div(stripe_nr, map->num_stripes); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5096 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5097 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5098 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5099 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5100 | num_alloc_stripes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5101 | if (dev_replace_is_ongoing) { |
| 5102 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
| 5103 | num_alloc_stripes <<= 1; |
| 5104 | if (rw & REQ_GET_READ_MIRRORS) |
| 5105 | num_alloc_stripes++; |
| 5106 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5107 | bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5108 | if (!bbio) { |
Dave Jones | eb2067f | 2013-07-30 13:42:17 -0400 | [diff] [blame] | 5109 | kfree(raid_map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5110 | ret = -ENOMEM; |
| 5111 | goto out; |
| 5112 | } |
| 5113 | atomic_set(&bbio->error, 0); |
| 5114 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5115 | if (rw & REQ_DISCARD) { |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5116 | int factor = 0; |
| 5117 | int sub_stripes = 0; |
| 5118 | u64 stripes_per_dev = 0; |
| 5119 | u32 remaining_stripes = 0; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5120 | u32 last_stripe = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5121 | |
| 5122 | if (map->type & |
| 5123 | (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5124 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5125 | sub_stripes = 1; |
| 5126 | else |
| 5127 | sub_stripes = map->sub_stripes; |
| 5128 | |
| 5129 | factor = map->num_stripes / sub_stripes; |
| 5130 | stripes_per_dev = div_u64_rem(stripe_nr_end - |
| 5131 | stripe_nr_orig, |
| 5132 | factor, |
| 5133 | &remaining_stripes); |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5134 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5135 | last_stripe *= sub_stripes; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5136 | } |
| 5137 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5138 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5139 | bbio->stripes[i].physical = |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5140 | map->stripes[stripe_index].physical + |
| 5141 | stripe_offset + stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5142 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5143 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5144 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5145 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5146 | bbio->stripes[i].length = stripes_per_dev * |
| 5147 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5148 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5149 | if (i / sub_stripes < remaining_stripes) |
| 5150 | bbio->stripes[i].length += |
| 5151 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5152 | |
| 5153 | /* |
| 5154 | * Special for the first stripe and |
| 5155 | * the last stripe: |
| 5156 | * |
| 5157 | * |-------|...|-------| |
| 5158 | * |----------| |
| 5159 | * off end_off |
| 5160 | */ |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5161 | if (i < sub_stripes) |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5162 | bbio->stripes[i].length -= |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5163 | stripe_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5164 | |
| 5165 | if (stripe_index >= last_stripe && |
| 5166 | stripe_index <= (last_stripe + |
| 5167 | sub_stripes - 1)) |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5168 | bbio->stripes[i].length -= |
| 5169 | stripe_end_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5170 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5171 | if (i == sub_stripes - 1) |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5172 | stripe_offset = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5173 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5174 | bbio->stripes[i].length = *length; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5175 | |
| 5176 | stripe_index++; |
| 5177 | if (stripe_index == map->num_stripes) { |
| 5178 | /* This could only happen for RAID0/10 */ |
| 5179 | stripe_index = 0; |
| 5180 | stripe_nr++; |
| 5181 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5182 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5183 | } else { |
| 5184 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5185 | bbio->stripes[i].physical = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5186 | map->stripes[stripe_index].physical + |
| 5187 | stripe_offset + |
| 5188 | stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5189 | bbio->stripes[i].dev = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5190 | map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5191 | stripe_index++; |
| 5192 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5193 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5194 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5195 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) { |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5196 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 5197 | BTRFS_BLOCK_GROUP_RAID10 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5198 | BTRFS_BLOCK_GROUP_RAID5 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5199 | BTRFS_BLOCK_GROUP_DUP)) { |
| 5200 | max_errors = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5201 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 5202 | max_errors = 2; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5203 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5204 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5205 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5206 | if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) && |
| 5207 | dev_replace->tgtdev != NULL) { |
| 5208 | int index_where_to_add; |
| 5209 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5210 | |
| 5211 | /* |
| 5212 | * duplicate the write operations while the dev replace |
| 5213 | * procedure is running. Since the copying of the old disk |
| 5214 | * to the new disk takes place at run time while the |
| 5215 | * filesystem is mounted writable, the regular write |
| 5216 | * operations to the old disk have to be duplicated to go |
| 5217 | * to the new disk as well. |
| 5218 | * Note that device->missing is handled by the caller, and |
| 5219 | * that the write to the old disk is already set up in the |
| 5220 | * stripes array. |
| 5221 | */ |
| 5222 | index_where_to_add = num_stripes; |
| 5223 | for (i = 0; i < num_stripes; i++) { |
| 5224 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5225 | /* write to new disk, too */ |
| 5226 | struct btrfs_bio_stripe *new = |
| 5227 | bbio->stripes + index_where_to_add; |
| 5228 | struct btrfs_bio_stripe *old = |
| 5229 | bbio->stripes + i; |
| 5230 | |
| 5231 | new->physical = old->physical; |
| 5232 | new->length = old->length; |
| 5233 | new->dev = dev_replace->tgtdev; |
| 5234 | index_where_to_add++; |
| 5235 | max_errors++; |
| 5236 | } |
| 5237 | } |
| 5238 | num_stripes = index_where_to_add; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5239 | } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) && |
| 5240 | dev_replace->tgtdev != NULL) { |
| 5241 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5242 | int index_srcdev = 0; |
| 5243 | int found = 0; |
| 5244 | u64 physical_of_found = 0; |
| 5245 | |
| 5246 | /* |
| 5247 | * During the dev-replace procedure, the target drive can |
| 5248 | * also be used to read data in case it is needed to repair |
| 5249 | * a corrupt block elsewhere. This is possible if the |
| 5250 | * requested area is left of the left cursor. In this area, |
| 5251 | * the target drive is a full copy of the source drive. |
| 5252 | */ |
| 5253 | for (i = 0; i < num_stripes; i++) { |
| 5254 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5255 | /* |
| 5256 | * In case of DUP, in order to keep it |
| 5257 | * simple, only add the mirror with the |
| 5258 | * lowest physical address |
| 5259 | */ |
| 5260 | if (found && |
| 5261 | physical_of_found <= |
| 5262 | bbio->stripes[i].physical) |
| 5263 | continue; |
| 5264 | index_srcdev = i; |
| 5265 | found = 1; |
| 5266 | physical_of_found = bbio->stripes[i].physical; |
| 5267 | } |
| 5268 | } |
| 5269 | if (found) { |
| 5270 | u64 length = map->stripe_len; |
| 5271 | |
| 5272 | if (physical_of_found + length <= |
| 5273 | dev_replace->cursor_left) { |
| 5274 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5275 | bbio->stripes + num_stripes; |
| 5276 | |
| 5277 | tgtdev_stripe->physical = physical_of_found; |
| 5278 | tgtdev_stripe->length = |
| 5279 | bbio->stripes[index_srcdev].length; |
| 5280 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 5281 | |
| 5282 | num_stripes++; |
| 5283 | } |
| 5284 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5285 | } |
| 5286 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5287 | *bbio_ret = bbio; |
| 5288 | bbio->num_stripes = num_stripes; |
| 5289 | bbio->max_errors = max_errors; |
| 5290 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5291 | |
| 5292 | /* |
| 5293 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 5294 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 5295 | * available as a mirror |
| 5296 | */ |
| 5297 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 5298 | WARN_ON(num_stripes > 1); |
| 5299 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 5300 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 5301 | bbio->mirror_num = map->num_stripes + 1; |
| 5302 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5303 | if (raid_map) { |
| 5304 | sort_parity_stripes(bbio, raid_map); |
| 5305 | *raid_map_ret = raid_map; |
| 5306 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5307 | out: |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5308 | if (dev_replace_is_ongoing) |
| 5309 | btrfs_dev_replace_unlock(dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5310 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5311 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5312 | } |
| 5313 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5314 | int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5315 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5316 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5317 | { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5318 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5319 | mirror_num, NULL); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5320 | } |
| 5321 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5322 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 5323 | u64 chunk_start, u64 physical, u64 devid, |
| 5324 | u64 **logical, int *naddrs, int *stripe_len) |
| 5325 | { |
| 5326 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5327 | struct extent_map *em; |
| 5328 | struct map_lookup *map; |
| 5329 | u64 *buf; |
| 5330 | u64 bytenr; |
| 5331 | u64 length; |
| 5332 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5333 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5334 | int i, j, nr = 0; |
| 5335 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5336 | read_lock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5337 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5338 | read_unlock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5339 | |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5340 | if (!em) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5341 | printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5342 | chunk_start); |
| 5343 | return -EIO; |
| 5344 | } |
| 5345 | |
| 5346 | if (em->start != chunk_start) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5347 | printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5348 | em->start, chunk_start); |
| 5349 | free_extent_map(em); |
| 5350 | return -EIO; |
| 5351 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5352 | map = (struct map_lookup *)em->bdev; |
| 5353 | |
| 5354 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5355 | rmap_len = map->stripe_len; |
| 5356 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5357 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5358 | do_div(length, map->num_stripes / map->sub_stripes); |
| 5359 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5360 | do_div(length, map->num_stripes); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5361 | else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 5362 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 5363 | do_div(length, nr_data_stripes(map)); |
| 5364 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 5365 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5366 | |
| 5367 | buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5368 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5369 | |
| 5370 | for (i = 0; i < map->num_stripes; i++) { |
| 5371 | if (devid && map->stripes[i].dev->devid != devid) |
| 5372 | continue; |
| 5373 | if (map->stripes[i].physical > physical || |
| 5374 | map->stripes[i].physical + length <= physical) |
| 5375 | continue; |
| 5376 | |
| 5377 | stripe_nr = physical - map->stripes[i].physical; |
| 5378 | do_div(stripe_nr, map->stripe_len); |
| 5379 | |
| 5380 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5381 | stripe_nr = stripe_nr * map->num_stripes + i; |
| 5382 | do_div(stripe_nr, map->sub_stripes); |
| 5383 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5384 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5385 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 5386 | * Alternatively, just use rmap_len below instead of |
| 5387 | * map->stripe_len */ |
| 5388 | |
| 5389 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5390 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5391 | for (j = 0; j < nr; j++) { |
| 5392 | if (buf[j] == bytenr) |
| 5393 | break; |
| 5394 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5395 | if (j == nr) { |
| 5396 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5397 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5398 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5399 | } |
| 5400 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5401 | *logical = buf; |
| 5402 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5403 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5404 | |
| 5405 | free_extent_map(em); |
| 5406 | return 0; |
| 5407 | } |
| 5408 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5409 | static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err) |
| 5410 | { |
| 5411 | if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED)) |
| 5412 | bio_endio_nodec(bio, err); |
| 5413 | else |
| 5414 | bio_endio(bio, err); |
| 5415 | kfree(bbio); |
| 5416 | } |
| 5417 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5418 | static void btrfs_end_bio(struct bio *bio, int err) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5419 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5420 | struct btrfs_bio *bbio = bio->bi_private; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5421 | struct btrfs_device *dev = bbio->stripes[0].dev; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5422 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5423 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5424 | if (err) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5425 | atomic_inc(&bbio->error); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5426 | if (err == -EIO || err == -EREMOTEIO) { |
| 5427 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5428 | btrfs_io_bio(bio)->stripe_index; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5429 | |
| 5430 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 5431 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5432 | if (dev->bdev) { |
| 5433 | if (bio->bi_rw & WRITE) |
| 5434 | btrfs_dev_stat_inc(dev, |
| 5435 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 5436 | else |
| 5437 | btrfs_dev_stat_inc(dev, |
| 5438 | BTRFS_DEV_STAT_READ_ERRS); |
| 5439 | if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH) |
| 5440 | btrfs_dev_stat_inc(dev, |
| 5441 | BTRFS_DEV_STAT_FLUSH_ERRS); |
| 5442 | btrfs_dev_stat_print_on_error(dev); |
| 5443 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5444 | } |
| 5445 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5446 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5447 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5448 | is_orig_bio = 1; |
| 5449 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5450 | btrfs_bio_counter_dec(bbio->fs_info); |
| 5451 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5452 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5453 | if (!is_orig_bio) { |
| 5454 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5455 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5456 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 5457 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5458 | bio->bi_private = bbio->private; |
| 5459 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5460 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5461 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5462 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5463 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5464 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5465 | err = -EIO; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 5466 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5467 | /* |
| 5468 | * this bio is actually up to date, we didn't |
| 5469 | * go over the max number of errors |
| 5470 | */ |
| 5471 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5472 | err = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5473 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5474 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5475 | btrfs_end_bbio(bbio, bio, err); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5476 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5477 | bio_put(bio); |
| 5478 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5479 | } |
| 5480 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5481 | /* |
| 5482 | * see run_scheduled_bios for a description of why bios are collected for |
| 5483 | * async submit. |
| 5484 | * |
| 5485 | * This will add one bio to the pending list for a device and make sure |
| 5486 | * the work struct is scheduled. |
| 5487 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5488 | static noinline void btrfs_schedule_bio(struct btrfs_root *root, |
| 5489 | struct btrfs_device *device, |
| 5490 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5491 | { |
| 5492 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5493 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5494 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5495 | if (device->missing || !device->bdev) { |
| 5496 | bio_endio(bio, -EIO); |
| 5497 | return; |
| 5498 | } |
| 5499 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5500 | /* don't bother with additional async steps for reads, right now */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5501 | if (!(rw & REQ_WRITE)) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5502 | bio_get(bio); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 5503 | btrfsic_submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5504 | bio_put(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5505 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5506 | } |
| 5507 | |
| 5508 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5509 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5510 | * higher layers. Otherwise, the async bio makes it appear we have |
| 5511 | * made progress against dirty pages when we've really just put it |
| 5512 | * on a queue for later |
| 5513 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5514 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5515 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5516 | bio->bi_next = NULL; |
| 5517 | bio->bi_rw |= rw; |
| 5518 | |
| 5519 | spin_lock(&device->io_lock); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5520 | if (bio->bi_rw & REQ_SYNC) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5521 | pending_bios = &device->pending_sync_bios; |
| 5522 | else |
| 5523 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5524 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5525 | if (pending_bios->tail) |
| 5526 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5527 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5528 | pending_bios->tail = bio; |
| 5529 | if (!pending_bios->head) |
| 5530 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5531 | if (device->running_pending) |
| 5532 | should_queue = 0; |
| 5533 | |
| 5534 | spin_unlock(&device->io_lock); |
| 5535 | |
| 5536 | if (should_queue) |
Qu Wenruo | a8c93d4e | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 5537 | btrfs_queue_work(root->fs_info->submit_workers, |
| 5538 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5539 | } |
| 5540 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5541 | static int bio_size_ok(struct block_device *bdev, struct bio *bio, |
| 5542 | sector_t sector) |
| 5543 | { |
| 5544 | struct bio_vec *prev; |
| 5545 | struct request_queue *q = bdev_get_queue(bdev); |
Akinobu Mita | 475bf36 | 2013-11-18 22:13:18 +0900 | [diff] [blame] | 5546 | unsigned int max_sectors = queue_max_sectors(q); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5547 | struct bvec_merge_data bvm = { |
| 5548 | .bi_bdev = bdev, |
| 5549 | .bi_sector = sector, |
| 5550 | .bi_rw = bio->bi_rw, |
| 5551 | }; |
| 5552 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 5553 | if (WARN_ON(bio->bi_vcnt == 0)) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5554 | return 1; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5555 | |
| 5556 | prev = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 5557 | if (bio_sectors(bio) > max_sectors) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5558 | return 0; |
| 5559 | |
| 5560 | if (!q->merge_bvec_fn) |
| 5561 | return 1; |
| 5562 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5563 | bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5564 | if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) |
| 5565 | return 0; |
| 5566 | return 1; |
| 5567 | } |
| 5568 | |
| 5569 | static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5570 | struct bio *bio, u64 physical, int dev_nr, |
| 5571 | int rw, int async) |
| 5572 | { |
| 5573 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
| 5574 | |
| 5575 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5576 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5577 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5578 | bio->bi_iter.bi_sector = physical >> 9; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5579 | #ifdef DEBUG |
| 5580 | { |
| 5581 | struct rcu_string *name; |
| 5582 | |
| 5583 | rcu_read_lock(); |
| 5584 | name = rcu_dereference(dev->name); |
Masanari Iida | d142324 | 2012-10-31 15:16:32 +0000 | [diff] [blame] | 5585 | pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu " |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5586 | "(%s id %llu), size=%u\n", rw, |
| 5587 | (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev, |
| 5588 | name->str, dev->devid, bio->bi_size); |
| 5589 | rcu_read_unlock(); |
| 5590 | } |
| 5591 | #endif |
| 5592 | bio->bi_bdev = dev->bdev; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5593 | |
| 5594 | btrfs_bio_counter_inc_noblocked(root->fs_info); |
| 5595 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5596 | if (async) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5597 | btrfs_schedule_bio(root, dev, rw, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5598 | else |
| 5599 | btrfsic_submit_bio(rw, bio); |
| 5600 | } |
| 5601 | |
| 5602 | static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5603 | struct bio *first_bio, struct btrfs_device *dev, |
| 5604 | int dev_nr, int rw, int async) |
| 5605 | { |
| 5606 | struct bio_vec *bvec = first_bio->bi_io_vec; |
| 5607 | struct bio *bio; |
| 5608 | int nr_vecs = bio_get_nr_vecs(dev->bdev); |
| 5609 | u64 physical = bbio->stripes[dev_nr].physical; |
| 5610 | |
| 5611 | again: |
| 5612 | bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS); |
| 5613 | if (!bio) |
| 5614 | return -ENOMEM; |
| 5615 | |
| 5616 | while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) { |
| 5617 | if (bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 5618 | bvec->bv_offset) < bvec->bv_len) { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5619 | u64 len = bio->bi_iter.bi_size; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5620 | |
| 5621 | atomic_inc(&bbio->stripes_pending); |
| 5622 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, |
| 5623 | rw, async); |
| 5624 | physical += len; |
| 5625 | goto again; |
| 5626 | } |
| 5627 | bvec++; |
| 5628 | } |
| 5629 | |
| 5630 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async); |
| 5631 | return 0; |
| 5632 | } |
| 5633 | |
| 5634 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 5635 | { |
| 5636 | atomic_inc(&bbio->error); |
| 5637 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5638 | /* Shoud be the original bio. */ |
| 5639 | WARN_ON(bio != bbio->orig_bio); |
| 5640 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5641 | bio->bi_private = bbio->private; |
| 5642 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5643 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5644 | bio->bi_iter.bi_sector = logical >> 9; |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5645 | |
| 5646 | btrfs_end_bbio(bbio, bio, -EIO); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5647 | } |
| 5648 | } |
| 5649 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5650 | 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] | 5651 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5652 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5653 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5654 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5655 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5656 | u64 length = 0; |
| 5657 | u64 map_length; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5658 | u64 *raid_map = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5659 | int ret; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5660 | int dev_nr = 0; |
| 5661 | int total_devs = 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5662 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5663 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5664 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5665 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5666 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5667 | btrfs_bio_counter_inc_blocked(root->fs_info); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5668 | ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio, |
| 5669 | mirror_num, &raid_map); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5670 | if (ret) { |
| 5671 | btrfs_bio_counter_dec(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5672 | return ret; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5673 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5674 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5675 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5676 | bbio->orig_bio = first_bio; |
| 5677 | bbio->private = first_bio->bi_private; |
| 5678 | bbio->end_io = first_bio->bi_end_io; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5679 | bbio->fs_info = root->fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5680 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 5681 | |
| 5682 | if (raid_map) { |
| 5683 | /* In this case, map_length has been set to the length of |
| 5684 | a single stripe; not the whole write */ |
| 5685 | if (rw & WRITE) { |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5686 | ret = raid56_parity_write(root, bio, bbio, |
| 5687 | raid_map, map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5688 | } else { |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5689 | ret = raid56_parity_recover(root, bio, bbio, |
| 5690 | raid_map, map_length, |
| 5691 | mirror_num); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5692 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5693 | /* |
| 5694 | * FIXME, replace dosen't support raid56 yet, please fix |
| 5695 | * it in the future. |
| 5696 | */ |
| 5697 | btrfs_bio_counter_dec(root->fs_info); |
| 5698 | return ret; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5699 | } |
| 5700 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5701 | if (map_length < length) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5702 | btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5703 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5704 | BUG(); |
| 5705 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5706 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5707 | while (dev_nr < total_devs) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5708 | dev = bbio->stripes[dev_nr].dev; |
| 5709 | if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) { |
| 5710 | bbio_error(bbio, first_bio, logical); |
| 5711 | dev_nr++; |
| 5712 | continue; |
| 5713 | } |
| 5714 | |
| 5715 | /* |
| 5716 | * Check and see if we're ok with this bio based on it's size |
| 5717 | * and offset with the given device. |
| 5718 | */ |
| 5719 | if (!bio_size_ok(dev->bdev, first_bio, |
| 5720 | bbio->stripes[dev_nr].physical >> 9)) { |
| 5721 | ret = breakup_stripe_bio(root, bbio, first_bio, dev, |
| 5722 | dev_nr, rw, async_submit); |
| 5723 | BUG_ON(ret); |
| 5724 | dev_nr++; |
| 5725 | continue; |
| 5726 | } |
| 5727 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5728 | if (dev_nr < total_devs - 1) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5729 | bio = btrfs_bio_clone(first_bio, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5730 | BUG_ON(!bio); /* -ENOMEM */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5731 | } else { |
| 5732 | bio = first_bio; |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5733 | bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5734 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5735 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5736 | submit_stripe_bio(root, bbio, bio, |
| 5737 | bbio->stripes[dev_nr].physical, dev_nr, rw, |
| 5738 | async_submit); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5739 | dev_nr++; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5740 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5741 | btrfs_bio_counter_dec(root->fs_info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5742 | return 0; |
| 5743 | } |
| 5744 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5745 | 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] | 5746 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5747 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5748 | struct btrfs_device *device; |
| 5749 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5750 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5751 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5752 | while (cur_devices) { |
| 5753 | if (!fsid || |
| 5754 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 5755 | device = __find_device(&cur_devices->devices, |
| 5756 | devid, uuid); |
| 5757 | if (device) |
| 5758 | return device; |
| 5759 | } |
| 5760 | cur_devices = cur_devices->seed; |
| 5761 | } |
| 5762 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5763 | } |
| 5764 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5765 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
| 5766 | u64 devid, u8 *dev_uuid) |
| 5767 | { |
| 5768 | struct btrfs_device *device; |
| 5769 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 5770 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5771 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 5772 | if (IS_ERR(device)) |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 5773 | return NULL; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5774 | |
| 5775 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5776 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5777 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5778 | |
| 5779 | device->missing = 1; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 5780 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5781 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5782 | return device; |
| 5783 | } |
| 5784 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5785 | /** |
| 5786 | * btrfs_alloc_device - allocate struct btrfs_device |
| 5787 | * @fs_info: used only for generating a new devid, can be NULL if |
| 5788 | * devid is provided (i.e. @devid != NULL). |
| 5789 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 5790 | * is generated. |
| 5791 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 5792 | * is generated. |
| 5793 | * |
| 5794 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() |
| 5795 | * on error. Returned struct is not linked onto any lists and can be |
| 5796 | * destroyed with kfree() right away. |
| 5797 | */ |
| 5798 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 5799 | const u64 *devid, |
| 5800 | const u8 *uuid) |
| 5801 | { |
| 5802 | struct btrfs_device *dev; |
| 5803 | u64 tmp; |
| 5804 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 5805 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5806 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5807 | |
| 5808 | dev = __alloc_device(); |
| 5809 | if (IS_ERR(dev)) |
| 5810 | return dev; |
| 5811 | |
| 5812 | if (devid) |
| 5813 | tmp = *devid; |
| 5814 | else { |
| 5815 | int ret; |
| 5816 | |
| 5817 | ret = find_next_devid(fs_info, &tmp); |
| 5818 | if (ret) { |
| 5819 | kfree(dev); |
| 5820 | return ERR_PTR(ret); |
| 5821 | } |
| 5822 | } |
| 5823 | dev->devid = tmp; |
| 5824 | |
| 5825 | if (uuid) |
| 5826 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 5827 | else |
| 5828 | generate_random_uuid(dev->uuid); |
| 5829 | |
Qu Wenruo | a8c93d4e | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 5830 | btrfs_init_work(&dev->work, pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5831 | |
| 5832 | return dev; |
| 5833 | } |
| 5834 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5835 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 5836 | struct extent_buffer *leaf, |
| 5837 | struct btrfs_chunk *chunk) |
| 5838 | { |
| 5839 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 5840 | struct map_lookup *map; |
| 5841 | struct extent_map *em; |
| 5842 | u64 logical; |
| 5843 | u64 length; |
| 5844 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5845 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5846 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5847 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5848 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5849 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5850 | logical = key->offset; |
| 5851 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5852 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5853 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5854 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5855 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5856 | |
| 5857 | /* already mapped? */ |
| 5858 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 5859 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5860 | return 0; |
| 5861 | } else if (em) { |
| 5862 | free_extent_map(em); |
| 5863 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5864 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5865 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5866 | if (!em) |
| 5867 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5868 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 5869 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5870 | if (!map) { |
| 5871 | free_extent_map(em); |
| 5872 | return -ENOMEM; |
| 5873 | } |
| 5874 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 5875 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5876 | em->bdev = (struct block_device *)map; |
| 5877 | em->start = logical; |
| 5878 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 5879 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5880 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5881 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5882 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5883 | map->num_stripes = num_stripes; |
| 5884 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 5885 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 5886 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 5887 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 5888 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5889 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5890 | for (i = 0; i < num_stripes; i++) { |
| 5891 | map->stripes[i].physical = |
| 5892 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 5893 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5894 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 5895 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 5896 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5897 | map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, |
| 5898 | uuid, NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5899 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5900 | free_extent_map(em); |
| 5901 | return -EIO; |
| 5902 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5903 | if (!map->stripes[i].dev) { |
| 5904 | map->stripes[i].dev = |
| 5905 | add_missing_dev(root, devid, uuid); |
| 5906 | if (!map->stripes[i].dev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5907 | free_extent_map(em); |
| 5908 | return -EIO; |
| 5909 | } |
| 5910 | } |
| 5911 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5912 | } |
| 5913 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5914 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5915 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5916 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5917 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5918 | free_extent_map(em); |
| 5919 | |
| 5920 | return 0; |
| 5921 | } |
| 5922 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5923 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5924 | struct btrfs_dev_item *dev_item, |
| 5925 | struct btrfs_device *device) |
| 5926 | { |
| 5927 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5928 | |
| 5929 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 5930 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 5931 | device->total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5932 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
| 5933 | device->type = btrfs_device_type(leaf, dev_item); |
| 5934 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 5935 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 5936 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 5937 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 5938 | device->is_tgtdev_for_dev_replace = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5939 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 5940 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5941 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5942 | } |
| 5943 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5944 | static int open_seed_devices(struct btrfs_root *root, u8 *fsid) |
| 5945 | { |
| 5946 | struct btrfs_fs_devices *fs_devices; |
| 5947 | int ret; |
| 5948 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 5949 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5950 | |
| 5951 | fs_devices = root->fs_info->fs_devices->seed; |
| 5952 | while (fs_devices) { |
| 5953 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 5954 | ret = 0; |
| 5955 | goto out; |
| 5956 | } |
| 5957 | fs_devices = fs_devices->seed; |
| 5958 | } |
| 5959 | |
| 5960 | fs_devices = find_fsid(fsid); |
| 5961 | if (!fs_devices) { |
| 5962 | ret = -ENOENT; |
| 5963 | goto out; |
| 5964 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5965 | |
| 5966 | fs_devices = clone_fs_devices(fs_devices); |
| 5967 | if (IS_ERR(fs_devices)) { |
| 5968 | ret = PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5969 | goto out; |
| 5970 | } |
| 5971 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 5972 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 5973 | root->fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 5974 | if (ret) { |
| 5975 | free_fs_devices(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5976 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 5977 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5978 | |
| 5979 | if (!fs_devices->seeding) { |
| 5980 | __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5981 | free_fs_devices(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5982 | ret = -EINVAL; |
| 5983 | goto out; |
| 5984 | } |
| 5985 | |
| 5986 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 5987 | root->fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5988 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5989 | return ret; |
| 5990 | } |
| 5991 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5992 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5993 | struct extent_buffer *leaf, |
| 5994 | struct btrfs_dev_item *dev_item) |
| 5995 | { |
| 5996 | struct btrfs_device *device; |
| 5997 | u64 devid; |
| 5998 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5999 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6000 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6001 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6002 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6003 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6004 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6005 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6006 | BTRFS_UUID_SIZE); |
| 6007 | |
| 6008 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
| 6009 | ret = open_seed_devices(root, fs_uuid); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6010 | if (ret && !btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6011 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6012 | } |
| 6013 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6014 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6015 | if (!device || !device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6016 | if (!btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6017 | return -EIO; |
| 6018 | |
| 6019 | if (!device) { |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6020 | btrfs_warn(root->fs_info, "devid %llu missing", devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6021 | device = add_missing_dev(root, devid, dev_uuid); |
| 6022 | if (!device) |
| 6023 | return -ENOMEM; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6024 | } else if (!device->missing) { |
| 6025 | /* |
| 6026 | * this happens when a device that was properly setup |
| 6027 | * in the device info lists suddenly goes bad. |
| 6028 | * device->bdev is NULL, and so we have to set |
| 6029 | * device->missing to one here |
| 6030 | */ |
| 6031 | root->fs_info->fs_devices->missing_devices++; |
| 6032 | device->missing = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6033 | } |
| 6034 | } |
| 6035 | |
| 6036 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 6037 | BUG_ON(device->writeable); |
| 6038 | if (device->generation != |
| 6039 | btrfs_device_generation(leaf, dev_item)) |
| 6040 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6041 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6042 | |
| 6043 | fill_device_from_item(leaf, dev_item, device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6044 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6045 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6046 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6047 | spin_lock(&root->fs_info->free_chunk_lock); |
| 6048 | root->fs_info->free_chunk_space += device->total_bytes - |
| 6049 | device->bytes_used; |
| 6050 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 6051 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6052 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6053 | return ret; |
| 6054 | } |
| 6055 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6056 | int btrfs_read_sys_array(struct btrfs_root *root) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6057 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6058 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6059 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6060 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6061 | struct btrfs_chunk *chunk; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6062 | u8 *ptr; |
| 6063 | unsigned long sb_ptr; |
| 6064 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6065 | u32 num_stripes; |
| 6066 | u32 array_size; |
| 6067 | u32 len = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6068 | u32 cur; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6069 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6070 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6071 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET, |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6072 | BTRFS_SUPER_INFO_SIZE); |
| 6073 | if (!sb) |
| 6074 | return -ENOMEM; |
| 6075 | btrfs_set_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6076 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6077 | /* |
| 6078 | * The sb extent buffer is artifical and just used to read the system array. |
| 6079 | * btrfs_set_buffer_uptodate() call does not properly mark all it's |
| 6080 | * pages up-to-date when the page is larger: extent does not cover the |
| 6081 | * whole page and consequently check_page_uptodate does not find all |
| 6082 | * the page's extents up-to-date (the hole beyond sb), |
| 6083 | * write_extent_buffer then triggers a WARN_ON. |
| 6084 | * |
| 6085 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6086 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6087 | * to silence the warning eg. on PowerPC 64. |
| 6088 | */ |
| 6089 | if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6090 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6091 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6092 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6093 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6094 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6095 | ptr = super_copy->sys_chunk_array; |
| 6096 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6097 | cur = 0; |
| 6098 | |
| 6099 | while (cur < array_size) { |
| 6100 | disk_key = (struct btrfs_disk_key *)ptr; |
| 6101 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6102 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6103 | len = sizeof(*disk_key); ptr += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6104 | sb_ptr += len; |
| 6105 | cur += len; |
| 6106 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6107 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6108 | chunk = (struct btrfs_chunk *)sb_ptr; |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6109 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6110 | if (ret) |
| 6111 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6112 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 6113 | len = btrfs_chunk_item_size(num_stripes); |
| 6114 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6115 | ret = -EIO; |
| 6116 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6117 | } |
| 6118 | ptr += len; |
| 6119 | sb_ptr += len; |
| 6120 | cur += len; |
| 6121 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6122 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6123 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6124 | } |
| 6125 | |
| 6126 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 6127 | { |
| 6128 | struct btrfs_path *path; |
| 6129 | struct extent_buffer *leaf; |
| 6130 | struct btrfs_key key; |
| 6131 | struct btrfs_key found_key; |
| 6132 | int ret; |
| 6133 | int slot; |
| 6134 | |
| 6135 | root = root->fs_info->chunk_root; |
| 6136 | |
| 6137 | path = btrfs_alloc_path(); |
| 6138 | if (!path) |
| 6139 | return -ENOMEM; |
| 6140 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6141 | mutex_lock(&uuid_mutex); |
| 6142 | lock_chunks(root); |
| 6143 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6144 | /* |
| 6145 | * Read all device items, and then all the chunk items. All |
| 6146 | * device items are found before any chunk item (their object id |
| 6147 | * is smaller than the lowest possible object id for a chunk |
| 6148 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6149 | */ |
| 6150 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 6151 | key.offset = 0; |
| 6152 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6153 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 6154 | if (ret < 0) |
| 6155 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6156 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6157 | leaf = path->nodes[0]; |
| 6158 | slot = path->slots[0]; |
| 6159 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6160 | ret = btrfs_next_leaf(root, path); |
| 6161 | if (ret == 0) |
| 6162 | continue; |
| 6163 | if (ret < 0) |
| 6164 | goto error; |
| 6165 | break; |
| 6166 | } |
| 6167 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6168 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 6169 | struct btrfs_dev_item *dev_item; |
| 6170 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6171 | struct btrfs_dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6172 | ret = read_one_dev(root, leaf, dev_item); |
| 6173 | if (ret) |
| 6174 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6175 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 6176 | struct btrfs_chunk *chunk; |
| 6177 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 6178 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6179 | if (ret) |
| 6180 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6181 | } |
| 6182 | path->slots[0]++; |
| 6183 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6184 | ret = 0; |
| 6185 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6186 | unlock_chunks(root); |
| 6187 | mutex_unlock(&uuid_mutex); |
| 6188 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6189 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6190 | return ret; |
| 6191 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6192 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6193 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 6194 | { |
| 6195 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6196 | struct btrfs_device *device; |
| 6197 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 6198 | while (fs_devices) { |
| 6199 | mutex_lock(&fs_devices->device_list_mutex); |
| 6200 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
| 6201 | device->dev_root = fs_info->dev_root; |
| 6202 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6203 | |
| 6204 | fs_devices = fs_devices->seed; |
| 6205 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6206 | } |
| 6207 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6208 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 6209 | { |
| 6210 | int i; |
| 6211 | |
| 6212 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6213 | btrfs_dev_stat_reset(dev, i); |
| 6214 | } |
| 6215 | |
| 6216 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 6217 | { |
| 6218 | struct btrfs_key key; |
| 6219 | struct btrfs_key found_key; |
| 6220 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6221 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6222 | struct extent_buffer *eb; |
| 6223 | int slot; |
| 6224 | int ret = 0; |
| 6225 | struct btrfs_device *device; |
| 6226 | struct btrfs_path *path = NULL; |
| 6227 | int i; |
| 6228 | |
| 6229 | path = btrfs_alloc_path(); |
| 6230 | if (!path) { |
| 6231 | ret = -ENOMEM; |
| 6232 | goto out; |
| 6233 | } |
| 6234 | |
| 6235 | mutex_lock(&fs_devices->device_list_mutex); |
| 6236 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 6237 | int item_size; |
| 6238 | struct btrfs_dev_stats_item *ptr; |
| 6239 | |
| 6240 | key.objectid = 0; |
| 6241 | key.type = BTRFS_DEV_STATS_KEY; |
| 6242 | key.offset = device->devid; |
| 6243 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 6244 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6245 | __btrfs_reset_dev_stats(device); |
| 6246 | device->dev_stats_valid = 1; |
| 6247 | btrfs_release_path(path); |
| 6248 | continue; |
| 6249 | } |
| 6250 | slot = path->slots[0]; |
| 6251 | eb = path->nodes[0]; |
| 6252 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 6253 | item_size = btrfs_item_size_nr(eb, slot); |
| 6254 | |
| 6255 | ptr = btrfs_item_ptr(eb, slot, |
| 6256 | struct btrfs_dev_stats_item); |
| 6257 | |
| 6258 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6259 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 6260 | btrfs_dev_stat_set(device, i, |
| 6261 | btrfs_dev_stats_value(eb, ptr, i)); |
| 6262 | else |
| 6263 | btrfs_dev_stat_reset(device, i); |
| 6264 | } |
| 6265 | |
| 6266 | device->dev_stats_valid = 1; |
| 6267 | btrfs_dev_stat_print_on_load(device); |
| 6268 | btrfs_release_path(path); |
| 6269 | } |
| 6270 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6271 | |
| 6272 | out: |
| 6273 | btrfs_free_path(path); |
| 6274 | return ret < 0 ? ret : 0; |
| 6275 | } |
| 6276 | |
| 6277 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
| 6278 | struct btrfs_root *dev_root, |
| 6279 | struct btrfs_device *device) |
| 6280 | { |
| 6281 | struct btrfs_path *path; |
| 6282 | struct btrfs_key key; |
| 6283 | struct extent_buffer *eb; |
| 6284 | struct btrfs_dev_stats_item *ptr; |
| 6285 | int ret; |
| 6286 | int i; |
| 6287 | |
| 6288 | key.objectid = 0; |
| 6289 | key.type = BTRFS_DEV_STATS_KEY; |
| 6290 | key.offset = device->devid; |
| 6291 | |
| 6292 | path = btrfs_alloc_path(); |
| 6293 | BUG_ON(!path); |
| 6294 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 6295 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6296 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6297 | "error %d while searching for dev_stats item for device %s!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6298 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6299 | goto out; |
| 6300 | } |
| 6301 | |
| 6302 | if (ret == 0 && |
| 6303 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 6304 | /* need to delete old one and insert a new one */ |
| 6305 | ret = btrfs_del_item(trans, dev_root, path); |
| 6306 | if (ret != 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6307 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6308 | "delete too small dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6309 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6310 | goto out; |
| 6311 | } |
| 6312 | ret = 1; |
| 6313 | } |
| 6314 | |
| 6315 | if (ret == 1) { |
| 6316 | /* need to insert a new item */ |
| 6317 | btrfs_release_path(path); |
| 6318 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 6319 | &key, sizeof(*ptr)); |
| 6320 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6321 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6322 | "insert dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6323 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6324 | goto out; |
| 6325 | } |
| 6326 | } |
| 6327 | |
| 6328 | eb = path->nodes[0]; |
| 6329 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 6330 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6331 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 6332 | btrfs_dev_stat_read(device, i)); |
| 6333 | btrfs_mark_buffer_dirty(eb); |
| 6334 | |
| 6335 | out: |
| 6336 | btrfs_free_path(path); |
| 6337 | return ret; |
| 6338 | } |
| 6339 | |
| 6340 | /* |
| 6341 | * called from commit_transaction. Writes all changed device stats to disk. |
| 6342 | */ |
| 6343 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 6344 | struct btrfs_fs_info *fs_info) |
| 6345 | { |
| 6346 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6347 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6348 | struct btrfs_device *device; |
| 6349 | int ret = 0; |
| 6350 | |
| 6351 | mutex_lock(&fs_devices->device_list_mutex); |
| 6352 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 6353 | if (!device->dev_stats_valid || !device->dev_stats_dirty) |
| 6354 | continue; |
| 6355 | |
| 6356 | ret = update_dev_stat_item(trans, dev_root, device); |
| 6357 | if (!ret) |
| 6358 | device->dev_stats_dirty = 0; |
| 6359 | } |
| 6360 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6361 | |
| 6362 | return ret; |
| 6363 | } |
| 6364 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6365 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 6366 | { |
| 6367 | btrfs_dev_stat_inc(dev, index); |
| 6368 | btrfs_dev_stat_print_on_error(dev); |
| 6369 | } |
| 6370 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6371 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6372 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6373 | if (!dev->dev_stats_valid) |
| 6374 | return; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6375 | printk_ratelimited_in_rcu(KERN_ERR "BTRFS: " |
| 6376 | "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] | 6377 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6378 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6379 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6380 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6381 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6382 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6383 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6384 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6385 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 6386 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 6387 | int i; |
| 6388 | |
| 6389 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6390 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 6391 | break; |
| 6392 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 6393 | return; /* all values == 0, suppress message */ |
| 6394 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6395 | printk_in_rcu(KERN_INFO "BTRFS: " |
| 6396 | "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] | 6397 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6398 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6399 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6400 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 6401 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6402 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 6403 | } |
| 6404 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6405 | int btrfs_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6406 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6407 | { |
| 6408 | struct btrfs_device *dev; |
| 6409 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6410 | int i; |
| 6411 | |
| 6412 | mutex_lock(&fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6413 | dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6414 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6415 | |
| 6416 | if (!dev) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6417 | btrfs_warn(root->fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6418 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6419 | } else if (!dev->dev_stats_valid) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6420 | btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6421 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6422 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6423 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6424 | if (stats->nr_items > i) |
| 6425 | stats->values[i] = |
| 6426 | btrfs_dev_stat_read_and_reset(dev, i); |
| 6427 | else |
| 6428 | btrfs_dev_stat_reset(dev, i); |
| 6429 | } |
| 6430 | } else { |
| 6431 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6432 | if (stats->nr_items > i) |
| 6433 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 6434 | } |
| 6435 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 6436 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 6437 | return 0; |
| 6438 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6439 | |
| 6440 | int btrfs_scratch_superblock(struct btrfs_device *device) |
| 6441 | { |
| 6442 | struct buffer_head *bh; |
| 6443 | struct btrfs_super_block *disk_super; |
| 6444 | |
| 6445 | bh = btrfs_read_dev_super(device->bdev); |
| 6446 | if (!bh) |
| 6447 | return -EINVAL; |
| 6448 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 6449 | |
| 6450 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 6451 | set_buffer_dirty(bh); |
| 6452 | sync_dirty_buffer(bh); |
| 6453 | brelse(bh); |
| 6454 | |
| 6455 | return 0; |
| 6456 | } |