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 | a8c93d4 | 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 | */ |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 535 | if (fs_devices->opened) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 536 | return -EBUSY; |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 537 | } else { |
| 538 | /* |
| 539 | * That is if the FS is _not_ mounted and if you |
| 540 | * are here, that means there is more than one |
| 541 | * disk with same uuid and devid.We keep the one |
| 542 | * with larger generation number or the last-in if |
| 543 | * generation are equal. |
| 544 | */ |
| 545 | if (found_transid < device->generation) |
| 546 | return -EEXIST; |
| 547 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 548 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 549 | name = rcu_string_strdup(path, GFP_NOFS); |
TARUISI Hiroaki | 3a0524d | 2010-02-09 06:36:45 +0000 | [diff] [blame] | 550 | if (!name) |
| 551 | return -ENOMEM; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 552 | rcu_string_free(device->name); |
| 553 | rcu_assign_pointer(device->name, name); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 554 | if (device->missing) { |
| 555 | fs_devices->missing_devices--; |
| 556 | device->missing = 0; |
| 557 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 558 | } |
| 559 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 560 | /* |
| 561 | * Unmount does not free the btrfs_device struct but would zero |
| 562 | * generation along with most of the other members. So just update |
| 563 | * it back. We need it to pick the disk with largest generation |
| 564 | * (as above). |
| 565 | */ |
| 566 | if (!fs_devices->opened) |
| 567 | device->generation = found_transid; |
| 568 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 569 | if (found_transid > fs_devices->latest_trans) { |
| 570 | fs_devices->latest_devid = devid; |
| 571 | fs_devices->latest_trans = found_transid; |
| 572 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 573 | *fs_devices_ret = fs_devices; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 574 | |
| 575 | return ret; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 576 | } |
| 577 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 578 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 579 | { |
| 580 | struct btrfs_fs_devices *fs_devices; |
| 581 | struct btrfs_device *device; |
| 582 | struct btrfs_device *orig_dev; |
| 583 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 584 | fs_devices = alloc_fs_devices(orig->fsid); |
| 585 | if (IS_ERR(fs_devices)) |
| 586 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 587 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 588 | fs_devices->latest_devid = orig->latest_devid; |
| 589 | fs_devices->latest_trans = orig->latest_trans; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 590 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 591 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 592 | /* 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] | 593 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 594 | struct rcu_string *name; |
| 595 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 596 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 597 | orig_dev->uuid); |
| 598 | if (IS_ERR(device)) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 599 | goto error; |
| 600 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 601 | /* |
| 602 | * This is ok to do without rcu read locked because we hold the |
| 603 | * uuid mutex so nothing we touch in here is going to disappear. |
| 604 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 605 | if (orig_dev->name) { |
| 606 | name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS); |
| 607 | if (!name) { |
| 608 | kfree(device); |
| 609 | goto error; |
| 610 | } |
| 611 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 612 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 613 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 614 | list_add(&device->dev_list, &fs_devices->devices); |
| 615 | device->fs_devices = fs_devices; |
| 616 | fs_devices->num_devices++; |
| 617 | } |
| 618 | return fs_devices; |
| 619 | error: |
| 620 | free_fs_devices(fs_devices); |
| 621 | return ERR_PTR(-ENOMEM); |
| 622 | } |
| 623 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 624 | void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info, |
| 625 | struct btrfs_fs_devices *fs_devices, int step) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 626 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 627 | struct btrfs_device *device, *next; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 628 | |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 629 | struct block_device *latest_bdev = NULL; |
| 630 | u64 latest_devid = 0; |
| 631 | u64 latest_transid = 0; |
| 632 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 633 | mutex_lock(&uuid_mutex); |
| 634 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 635 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 636 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 637 | if (device->in_fs_metadata) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 638 | if (!device->is_tgtdev_for_dev_replace && |
| 639 | (!latest_transid || |
| 640 | device->generation > latest_transid)) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 641 | latest_devid = device->devid; |
| 642 | latest_transid = device->generation; |
| 643 | latest_bdev = device->bdev; |
| 644 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 645 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 646 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 647 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 648 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 649 | /* |
| 650 | * In the first step, keep the device which has |
| 651 | * the correct fsid and the devid that is used |
| 652 | * for the dev_replace procedure. |
| 653 | * In the second step, the dev_replace state is |
| 654 | * read from the device tree and it is known |
| 655 | * whether the procedure is really active or |
| 656 | * not, which means whether this device is |
| 657 | * used or whether it should be removed. |
| 658 | */ |
| 659 | if (step == 0 || device->is_tgtdev_for_dev_replace) { |
| 660 | continue; |
| 661 | } |
| 662 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 663 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 664 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 665 | device->bdev = NULL; |
| 666 | fs_devices->open_devices--; |
| 667 | } |
| 668 | if (device->writeable) { |
| 669 | list_del_init(&device->dev_alloc_list); |
| 670 | device->writeable = 0; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 671 | if (!device->is_tgtdev_for_dev_replace) |
| 672 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 673 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 674 | list_del_init(&device->dev_list); |
| 675 | fs_devices->num_devices--; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 676 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 677 | kfree(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 678 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 679 | |
| 680 | if (fs_devices->seed) { |
| 681 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 682 | goto again; |
| 683 | } |
| 684 | |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 685 | fs_devices->latest_bdev = latest_bdev; |
| 686 | fs_devices->latest_devid = latest_devid; |
| 687 | fs_devices->latest_trans = latest_transid; |
| 688 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 689 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 690 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 691 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 692 | static void __free_device(struct work_struct *work) |
| 693 | { |
| 694 | struct btrfs_device *device; |
| 695 | |
| 696 | device = container_of(work, struct btrfs_device, rcu_work); |
| 697 | |
| 698 | if (device->bdev) |
| 699 | blkdev_put(device->bdev, device->mode); |
| 700 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 701 | rcu_string_free(device->name); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 702 | kfree(device); |
| 703 | } |
| 704 | |
| 705 | static void free_device(struct rcu_head *head) |
| 706 | { |
| 707 | struct btrfs_device *device; |
| 708 | |
| 709 | device = container_of(head, struct btrfs_device, rcu); |
| 710 | |
| 711 | INIT_WORK(&device->rcu_work, __free_device); |
| 712 | schedule_work(&device->rcu_work); |
| 713 | } |
| 714 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 715 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 716 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 717 | struct btrfs_device *device; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 718 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 719 | if (--fs_devices->opened > 0) |
| 720 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 721 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 722 | mutex_lock(&fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 723 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 724 | struct btrfs_device *new_device; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 725 | struct rcu_string *name; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 726 | |
| 727 | if (device->bdev) |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 728 | fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 729 | |
Ilya Dryomov | f747cab | 2013-10-10 20:37:29 +0300 | [diff] [blame] | 730 | if (device->writeable && |
| 731 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 732 | list_del_init(&device->dev_alloc_list); |
| 733 | fs_devices->rw_devices--; |
| 734 | } |
| 735 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 736 | if (device->can_discard) |
| 737 | fs_devices->num_can_discard--; |
Josef Bacik | 726551e | 2013-08-26 13:45:53 -0400 | [diff] [blame] | 738 | if (device->missing) |
| 739 | fs_devices->missing_devices--; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 740 | |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 741 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 742 | device->uuid); |
| 743 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 744 | |
| 745 | /* Safe because we are under uuid_mutex */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 746 | if (device->name) { |
| 747 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 748 | BUG_ON(!name); /* -ENOMEM */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 749 | rcu_assign_pointer(new_device->name, name); |
| 750 | } |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 751 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 752 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 753 | new_device->fs_devices = device->fs_devices; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 754 | |
| 755 | call_rcu(&device->rcu, free_device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 756 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 757 | mutex_unlock(&fs_devices->device_list_mutex); |
| 758 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 759 | WARN_ON(fs_devices->open_devices); |
| 760 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 761 | fs_devices->opened = 0; |
| 762 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 763 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 764 | return 0; |
| 765 | } |
| 766 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 767 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 768 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 769 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 770 | int ret; |
| 771 | |
| 772 | mutex_lock(&uuid_mutex); |
| 773 | ret = __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 774 | if (!fs_devices->opened) { |
| 775 | seed_devices = fs_devices->seed; |
| 776 | fs_devices->seed = NULL; |
| 777 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 778 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 779 | |
| 780 | while (seed_devices) { |
| 781 | fs_devices = seed_devices; |
| 782 | seed_devices = fs_devices->seed; |
| 783 | __btrfs_close_devices(fs_devices); |
| 784 | free_fs_devices(fs_devices); |
| 785 | } |
Eric Sandeen | bc17862 | 2013-03-09 15:18:39 +0000 | [diff] [blame] | 786 | /* |
| 787 | * Wait for rcu kworkers under __btrfs_close_devices |
| 788 | * to finish all blkdev_puts so device is really |
| 789 | * free when umount is done. |
| 790 | */ |
| 791 | rcu_barrier(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 792 | return ret; |
| 793 | } |
| 794 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 795 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 796 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 797 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 798 | struct request_queue *q; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 799 | struct block_device *bdev; |
| 800 | struct list_head *head = &fs_devices->devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 801 | struct btrfs_device *device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 802 | struct block_device *latest_bdev = NULL; |
| 803 | struct buffer_head *bh; |
| 804 | struct btrfs_super_block *disk_super; |
| 805 | u64 latest_devid = 0; |
| 806 | u64 latest_transid = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 807 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 808 | int seeding = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 809 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 810 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 811 | flags |= FMODE_EXCL; |
| 812 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 813 | list_for_each_entry(device, head, dev_list) { |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 814 | if (device->bdev) |
| 815 | continue; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 816 | if (!device->name) |
| 817 | continue; |
| 818 | |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 819 | /* Just open everything we can; ignore failures here */ |
| 820 | if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 821 | &bdev, &bh)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 822 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 823 | |
| 824 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 825 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 826 | if (devid != device->devid) |
| 827 | goto error_brelse; |
| 828 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 829 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
| 830 | BTRFS_UUID_SIZE)) |
| 831 | goto error_brelse; |
| 832 | |
| 833 | device->generation = btrfs_super_generation(disk_super); |
| 834 | if (!latest_transid || device->generation > latest_transid) { |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 835 | latest_devid = devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 836 | latest_transid = device->generation; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 837 | latest_bdev = bdev; |
| 838 | } |
| 839 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 840 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| 841 | device->writeable = 0; |
| 842 | } else { |
| 843 | device->writeable = !bdev_read_only(bdev); |
| 844 | seeding = 0; |
| 845 | } |
| 846 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 847 | q = bdev_get_queue(bdev); |
| 848 | if (blk_queue_discard(q)) { |
| 849 | device->can_discard = 1; |
| 850 | fs_devices->num_can_discard++; |
| 851 | } |
| 852 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 853 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 854 | device->in_fs_metadata = 0; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 855 | device->mode = flags; |
| 856 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 857 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 858 | fs_devices->rotating = 1; |
| 859 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 860 | fs_devices->open_devices++; |
Ilya Dryomov | 55e50e4 | 2013-09-01 18:56:44 +0300 | [diff] [blame] | 861 | if (device->writeable && |
| 862 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 863 | fs_devices->rw_devices++; |
| 864 | list_add(&device->dev_alloc_list, |
| 865 | &fs_devices->alloc_list); |
| 866 | } |
Xiao Guangrong | 4f6c932 | 2011-04-20 10:06:40 +0000 | [diff] [blame] | 867 | brelse(bh); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 868 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 869 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 870 | error_brelse: |
| 871 | brelse(bh); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 872 | blkdev_put(bdev, flags); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 873 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 874 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 875 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 876 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 877 | goto out; |
| 878 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 879 | fs_devices->seeding = seeding; |
| 880 | fs_devices->opened = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 881 | fs_devices->latest_bdev = latest_bdev; |
| 882 | fs_devices->latest_devid = latest_devid; |
| 883 | fs_devices->latest_trans = latest_transid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 884 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 885 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 886 | return ret; |
| 887 | } |
| 888 | |
| 889 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 890 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 891 | { |
| 892 | int ret; |
| 893 | |
| 894 | mutex_lock(&uuid_mutex); |
| 895 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 896 | fs_devices->opened++; |
| 897 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 898 | } else { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 899 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 900 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 901 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 902 | return ret; |
| 903 | } |
| 904 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 905 | /* |
| 906 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 907 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 908 | * is read via pagecache |
| 909 | */ |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 910 | 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] | 911 | struct btrfs_fs_devices **fs_devices_ret) |
| 912 | { |
| 913 | struct btrfs_super_block *disk_super; |
| 914 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 915 | struct page *page; |
| 916 | void *p; |
| 917 | int ret = -EINVAL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 918 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 919 | u64 transid; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 920 | u64 total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 921 | u64 bytenr; |
| 922 | pgoff_t index; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 923 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 924 | /* |
| 925 | * we would like to check all the supers, but that would make |
| 926 | * a btrfs mount succeed after a mkfs from a different FS. |
| 927 | * So, we need to add a special mount option to scan for |
| 928 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 929 | */ |
| 930 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 931 | flags |= FMODE_EXCL; |
Al Viro | 10f6327 | 2011-11-17 15:05:22 -0500 | [diff] [blame] | 932 | mutex_lock(&uuid_mutex); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 933 | |
| 934 | bdev = blkdev_get_by_path(path, flags, holder); |
| 935 | |
| 936 | if (IS_ERR(bdev)) { |
| 937 | ret = PTR_ERR(bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 938 | goto error; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /* make sure our super fits in the device */ |
| 942 | if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode)) |
| 943 | goto error_bdev_put; |
| 944 | |
| 945 | /* make sure our super fits in the page */ |
| 946 | if (sizeof(*disk_super) > PAGE_CACHE_SIZE) |
| 947 | goto error_bdev_put; |
| 948 | |
| 949 | /* make sure our super doesn't straddle pages on disk */ |
| 950 | index = bytenr >> PAGE_CACHE_SHIFT; |
| 951 | if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index) |
| 952 | goto error_bdev_put; |
| 953 | |
| 954 | /* pull in the page with our super */ |
| 955 | page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 956 | index, GFP_NOFS); |
| 957 | |
| 958 | if (IS_ERR_OR_NULL(page)) |
| 959 | goto error_bdev_put; |
| 960 | |
| 961 | p = kmap(page); |
| 962 | |
| 963 | /* align our pointer to the offset of the super block */ |
| 964 | disk_super = p + (bytenr & ~PAGE_CACHE_MASK); |
| 965 | |
| 966 | if (btrfs_super_bytenr(disk_super) != bytenr || |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 967 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 968 | goto error_unmap; |
| 969 | |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 970 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 971 | transid = btrfs_super_generation(disk_super); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 972 | total_devices = btrfs_super_num_devices(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 973 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 974 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 975 | if (ret > 0) { |
| 976 | if (disk_super->label[0]) { |
| 977 | if (disk_super->label[BTRFS_LABEL_SIZE - 1]) |
| 978 | disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 979 | printk(KERN_INFO "BTRFS: device label %s ", disk_super->label); |
| 980 | } else { |
| 981 | printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid); |
| 982 | } |
| 983 | |
| 984 | printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path); |
| 985 | ret = 0; |
| 986 | } |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 987 | if (!ret && fs_devices_ret) |
| 988 | (*fs_devices_ret)->total_devices = total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 989 | |
| 990 | error_unmap: |
| 991 | kunmap(page); |
| 992 | page_cache_release(page); |
| 993 | |
| 994 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 995 | blkdev_put(bdev, flags); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 996 | error: |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 997 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 998 | return ret; |
| 999 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1000 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1001 | /* helper to account the used device space in the range */ |
| 1002 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, |
| 1003 | u64 end, u64 *length) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1004 | { |
| 1005 | struct btrfs_key key; |
| 1006 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1007 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1008 | struct btrfs_path *path; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1009 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1010 | int ret; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1011 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1012 | struct extent_buffer *l; |
| 1013 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1014 | *length = 0; |
| 1015 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1016 | if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1017 | return 0; |
| 1018 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1019 | path = btrfs_alloc_path(); |
| 1020 | if (!path) |
| 1021 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1022 | path->reada = 2; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1023 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1024 | key.objectid = device->devid; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1025 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1026 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1027 | |
| 1028 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1029 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1030 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1031 | if (ret > 0) { |
| 1032 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1033 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1034 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1035 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1036 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1037 | while (1) { |
| 1038 | l = path->nodes[0]; |
| 1039 | slot = path->slots[0]; |
| 1040 | if (slot >= btrfs_header_nritems(l)) { |
| 1041 | ret = btrfs_next_leaf(root, path); |
| 1042 | if (ret == 0) |
| 1043 | continue; |
| 1044 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1045 | goto out; |
| 1046 | |
| 1047 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1048 | } |
| 1049 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1050 | |
| 1051 | if (key.objectid < device->devid) |
| 1052 | goto next; |
| 1053 | |
| 1054 | if (key.objectid > device->devid) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1055 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1056 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1057 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1058 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1059 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1060 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1061 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1062 | dev_extent); |
| 1063 | if (key.offset <= start && extent_end > end) { |
| 1064 | *length = end - start + 1; |
| 1065 | break; |
| 1066 | } else if (key.offset <= start && extent_end > start) |
| 1067 | *length += extent_end - start; |
| 1068 | else if (key.offset > start && extent_end <= end) |
| 1069 | *length += extent_end - key.offset; |
| 1070 | else if (key.offset > start && key.offset <= end) { |
| 1071 | *length += end - key.offset + 1; |
| 1072 | break; |
| 1073 | } else if (key.offset > end) |
| 1074 | break; |
| 1075 | |
| 1076 | next: |
| 1077 | path->slots[0]++; |
| 1078 | } |
| 1079 | ret = 0; |
| 1080 | out: |
| 1081 | btrfs_free_path(path); |
| 1082 | return ret; |
| 1083 | } |
| 1084 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1085 | static int contains_pending_extent(struct btrfs_trans_handle *trans, |
| 1086 | struct btrfs_device *device, |
| 1087 | u64 *start, u64 len) |
| 1088 | { |
| 1089 | struct extent_map *em; |
| 1090 | int ret = 0; |
| 1091 | |
| 1092 | list_for_each_entry(em, &trans->transaction->pending_chunks, list) { |
| 1093 | struct map_lookup *map; |
| 1094 | int i; |
| 1095 | |
| 1096 | map = (struct map_lookup *)em->bdev; |
| 1097 | for (i = 0; i < map->num_stripes; i++) { |
| 1098 | if (map->stripes[i].dev != device) |
| 1099 | continue; |
| 1100 | if (map->stripes[i].physical >= *start + len || |
| 1101 | map->stripes[i].physical + em->orig_block_len <= |
| 1102 | *start) |
| 1103 | continue; |
| 1104 | *start = map->stripes[i].physical + |
| 1105 | em->orig_block_len; |
| 1106 | ret = 1; |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | return ret; |
| 1111 | } |
| 1112 | |
| 1113 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1114 | /* |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1115 | * find_free_dev_extent - find free space in the specified device |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1116 | * @device: the device which we search the free space in |
| 1117 | * @num_bytes: the size of the free space that we need |
| 1118 | * @start: store the start of the free space. |
| 1119 | * @len: the size of the free space. that we find, or the size of the max |
| 1120 | * free space if we don't find suitable free space |
| 1121 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1122 | * this uses a pretty simple search, the expectation is that it is |
| 1123 | * called very infrequently and that a given device has a small number |
| 1124 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1125 | * |
| 1126 | * @start is used to store the start of the free space if we find. But if we |
| 1127 | * don't find suitable free space, it will be used to store the start position |
| 1128 | * of the max free space. |
| 1129 | * |
| 1130 | * @len is used to store the size of the free space that we find. |
| 1131 | * But if we don't find suitable free space, it is used to store the size of |
| 1132 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1133 | */ |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1134 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 1135 | struct btrfs_device *device, u64 num_bytes, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1136 | u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1137 | { |
| 1138 | struct btrfs_key key; |
| 1139 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1140 | struct btrfs_dev_extent *dev_extent; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1141 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1142 | u64 hole_size; |
| 1143 | u64 max_hole_start; |
| 1144 | u64 max_hole_size; |
| 1145 | u64 extent_end; |
| 1146 | u64 search_start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1147 | u64 search_end = device->total_bytes; |
| 1148 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1149 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1150 | struct extent_buffer *l; |
| 1151 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1152 | /* FIXME use last free of some kind */ |
| 1153 | |
| 1154 | /* we don't want to overwrite the superblock on the drive, |
| 1155 | * so we make sure to start at an offset of at least 1MB |
| 1156 | */ |
Arne Jansen | a9c9bf6 | 2011-04-12 11:01:20 +0200 | [diff] [blame] | 1157 | search_start = max(root->fs_info->alloc_start, 1024ull * 1024); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1158 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1159 | path = btrfs_alloc_path(); |
| 1160 | if (!path) |
| 1161 | return -ENOMEM; |
| 1162 | again: |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1163 | max_hole_start = search_start; |
| 1164 | max_hole_size = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1165 | hole_size = 0; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1166 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1167 | if (search_start >= search_end || device->is_tgtdev_for_dev_replace) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1168 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1169 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1172 | path->reada = 2; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1173 | path->search_commit_root = 1; |
| 1174 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1175 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1176 | key.objectid = device->devid; |
| 1177 | key.offset = search_start; |
| 1178 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1179 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1180 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1181 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1182 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1183 | if (ret > 0) { |
| 1184 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1185 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1186 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1187 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1188 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1189 | while (1) { |
| 1190 | l = path->nodes[0]; |
| 1191 | slot = path->slots[0]; |
| 1192 | if (slot >= btrfs_header_nritems(l)) { |
| 1193 | ret = btrfs_next_leaf(root, path); |
| 1194 | if (ret == 0) |
| 1195 | continue; |
| 1196 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1197 | goto out; |
| 1198 | |
| 1199 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1200 | } |
| 1201 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1202 | |
| 1203 | if (key.objectid < device->devid) |
| 1204 | goto next; |
| 1205 | |
| 1206 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1207 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1208 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1209 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) |
| 1210 | goto next; |
| 1211 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1212 | if (key.offset > search_start) { |
| 1213 | hole_size = key.offset - search_start; |
| 1214 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1215 | /* |
| 1216 | * Have to check before we set max_hole_start, otherwise |
| 1217 | * we could end up sending back this offset anyway. |
| 1218 | */ |
| 1219 | if (contains_pending_extent(trans, device, |
| 1220 | &search_start, |
| 1221 | hole_size)) |
| 1222 | hole_size = 0; |
| 1223 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1224 | if (hole_size > max_hole_size) { |
| 1225 | max_hole_start = search_start; |
| 1226 | max_hole_size = hole_size; |
| 1227 | } |
| 1228 | |
| 1229 | /* |
| 1230 | * If this free space is greater than which we need, |
| 1231 | * it must be the max free space that we have found |
| 1232 | * until now, so max_hole_start must point to the start |
| 1233 | * of this free space and the length of this free space |
| 1234 | * is stored in max_hole_size. Thus, we return |
| 1235 | * max_hole_start and max_hole_size and go back to the |
| 1236 | * caller. |
| 1237 | */ |
| 1238 | if (hole_size >= num_bytes) { |
| 1239 | ret = 0; |
| 1240 | goto out; |
| 1241 | } |
| 1242 | } |
| 1243 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1244 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1245 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1246 | dev_extent); |
| 1247 | if (extent_end > search_start) |
| 1248 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1249 | next: |
| 1250 | path->slots[0]++; |
| 1251 | cond_resched(); |
| 1252 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1253 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1254 | /* |
| 1255 | * At this point, search_start should be the end of |
| 1256 | * allocated dev extents, and when shrinking the device, |
| 1257 | * search_end may be smaller than search_start. |
| 1258 | */ |
| 1259 | if (search_end > search_start) |
| 1260 | hole_size = search_end - search_start; |
| 1261 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1262 | if (hole_size > max_hole_size) { |
| 1263 | max_hole_start = search_start; |
| 1264 | max_hole_size = hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1265 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1266 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1267 | if (contains_pending_extent(trans, device, &search_start, hole_size)) { |
| 1268 | btrfs_release_path(path); |
| 1269 | goto again; |
| 1270 | } |
| 1271 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1272 | /* See above. */ |
| 1273 | if (hole_size < num_bytes) |
| 1274 | ret = -ENOSPC; |
| 1275 | else |
| 1276 | ret = 0; |
| 1277 | |
| 1278 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1279 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1280 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1281 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1282 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1283 | return ret; |
| 1284 | } |
| 1285 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1286 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1287 | struct btrfs_device *device, |
| 1288 | u64 start) |
| 1289 | { |
| 1290 | int ret; |
| 1291 | struct btrfs_path *path; |
| 1292 | struct btrfs_root *root = device->dev_root; |
| 1293 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1294 | struct btrfs_key found_key; |
| 1295 | struct extent_buffer *leaf = NULL; |
| 1296 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1297 | |
| 1298 | path = btrfs_alloc_path(); |
| 1299 | if (!path) |
| 1300 | return -ENOMEM; |
| 1301 | |
| 1302 | key.objectid = device->devid; |
| 1303 | key.offset = start; |
| 1304 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1305 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1306 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1307 | if (ret > 0) { |
| 1308 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1309 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1310 | if (ret) |
| 1311 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1312 | leaf = path->nodes[0]; |
| 1313 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1314 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1315 | struct btrfs_dev_extent); |
| 1316 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1317 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1318 | key = found_key; |
| 1319 | btrfs_release_path(path); |
| 1320 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1321 | } else if (ret == 0) { |
| 1322 | leaf = path->nodes[0]; |
| 1323 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1324 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1325 | } else { |
| 1326 | btrfs_error(root->fs_info, ret, "Slot search failed"); |
| 1327 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1328 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1329 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 1330 | if (device->bytes_used > 0) { |
| 1331 | u64 len = btrfs_dev_extent_length(leaf, extent); |
| 1332 | device->bytes_used -= len; |
| 1333 | spin_lock(&root->fs_info->free_chunk_lock); |
| 1334 | root->fs_info->free_chunk_space += len; |
| 1335 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 1336 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1337 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1338 | if (ret) { |
| 1339 | btrfs_error(root->fs_info, ret, |
| 1340 | "Failed to remove dev extent item"); |
| 1341 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1342 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1343 | btrfs_free_path(path); |
| 1344 | return ret; |
| 1345 | } |
| 1346 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1347 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1348 | struct btrfs_device *device, |
| 1349 | u64 chunk_tree, u64 chunk_objectid, |
| 1350 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1351 | { |
| 1352 | int ret; |
| 1353 | struct btrfs_path *path; |
| 1354 | struct btrfs_root *root = device->dev_root; |
| 1355 | struct btrfs_dev_extent *extent; |
| 1356 | struct extent_buffer *leaf; |
| 1357 | struct btrfs_key key; |
| 1358 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1359 | WARN_ON(!device->in_fs_metadata); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1360 | WARN_ON(device->is_tgtdev_for_dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1361 | path = btrfs_alloc_path(); |
| 1362 | if (!path) |
| 1363 | return -ENOMEM; |
| 1364 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1365 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1366 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1367 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1368 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1369 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1370 | if (ret) |
| 1371 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1372 | |
| 1373 | leaf = path->nodes[0]; |
| 1374 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1375 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1376 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 1377 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 1378 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1379 | |
| 1380 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | 231e88f | 2013-08-20 13:20:13 +0200 | [diff] [blame] | 1381 | btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1382 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1383 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1384 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1385 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1386 | btrfs_free_path(path); |
| 1387 | return ret; |
| 1388 | } |
| 1389 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1390 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1391 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1392 | struct extent_map_tree *em_tree; |
| 1393 | struct extent_map *em; |
| 1394 | struct rb_node *n; |
| 1395 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1396 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1397 | em_tree = &fs_info->mapping_tree.map_tree; |
| 1398 | read_lock(&em_tree->lock); |
| 1399 | n = rb_last(&em_tree->map); |
| 1400 | if (n) { |
| 1401 | em = rb_entry(n, struct extent_map, rb_node); |
| 1402 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1403 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1404 | read_unlock(&em_tree->lock); |
| 1405 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1406 | return ret; |
| 1407 | } |
| 1408 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1409 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1410 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1411 | { |
| 1412 | int ret; |
| 1413 | struct btrfs_key key; |
| 1414 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1415 | struct btrfs_path *path; |
| 1416 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1417 | path = btrfs_alloc_path(); |
| 1418 | if (!path) |
| 1419 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1420 | |
| 1421 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1422 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1423 | key.offset = (u64)-1; |
| 1424 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1425 | 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] | 1426 | if (ret < 0) |
| 1427 | goto error; |
| 1428 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1429 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1430 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1431 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1432 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1433 | BTRFS_DEV_ITEM_KEY); |
| 1434 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1435 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1436 | } else { |
| 1437 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1438 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1439 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1440 | } |
| 1441 | ret = 0; |
| 1442 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1443 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1444 | return ret; |
| 1445 | } |
| 1446 | |
| 1447 | /* |
| 1448 | * the device information is stored in the chunk root |
| 1449 | * the btrfs_device struct should be fully filled in |
| 1450 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1451 | static int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 1452 | struct btrfs_root *root, |
| 1453 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1454 | { |
| 1455 | int ret; |
| 1456 | struct btrfs_path *path; |
| 1457 | struct btrfs_dev_item *dev_item; |
| 1458 | struct extent_buffer *leaf; |
| 1459 | struct btrfs_key key; |
| 1460 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1461 | |
| 1462 | root = root->fs_info->chunk_root; |
| 1463 | |
| 1464 | path = btrfs_alloc_path(); |
| 1465 | if (!path) |
| 1466 | return -ENOMEM; |
| 1467 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1468 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1469 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1470 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1471 | |
| 1472 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1473 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1474 | if (ret) |
| 1475 | goto out; |
| 1476 | |
| 1477 | leaf = path->nodes[0]; |
| 1478 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1479 | |
| 1480 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1481 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1482 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1483 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1484 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1485 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7df69d3 | 2014-07-24 11:37:13 +0800 | [diff] [blame] | 1486 | btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1487 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1488 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1489 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1490 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1491 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1492 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1493 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1494 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1495 | ptr = btrfs_device_fsid(dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1496 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1497 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1498 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1499 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1500 | out: |
| 1501 | btrfs_free_path(path); |
| 1502 | return ret; |
| 1503 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1504 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1505 | /* |
| 1506 | * Function to update ctime/mtime for a given device path. |
| 1507 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1508 | */ |
| 1509 | static void update_dev_time(char *path_name) |
| 1510 | { |
| 1511 | struct file *filp; |
| 1512 | |
| 1513 | filp = filp_open(path_name, O_RDWR, 0); |
| 1514 | if (!filp) |
| 1515 | return; |
| 1516 | file_update_time(filp); |
| 1517 | filp_close(filp, NULL); |
| 1518 | return; |
| 1519 | } |
| 1520 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1521 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 1522 | struct btrfs_device *device) |
| 1523 | { |
| 1524 | int ret; |
| 1525 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1526 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1527 | struct btrfs_trans_handle *trans; |
| 1528 | |
| 1529 | root = root->fs_info->chunk_root; |
| 1530 | |
| 1531 | path = btrfs_alloc_path(); |
| 1532 | if (!path) |
| 1533 | return -ENOMEM; |
| 1534 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1535 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1536 | if (IS_ERR(trans)) { |
| 1537 | btrfs_free_path(path); |
| 1538 | return PTR_ERR(trans); |
| 1539 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1540 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1541 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1542 | key.offset = device->devid; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1543 | lock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1544 | |
| 1545 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1546 | if (ret < 0) |
| 1547 | goto out; |
| 1548 | |
| 1549 | if (ret > 0) { |
| 1550 | ret = -ENOENT; |
| 1551 | goto out; |
| 1552 | } |
| 1553 | |
| 1554 | ret = btrfs_del_item(trans, root, path); |
| 1555 | if (ret) |
| 1556 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1557 | out: |
| 1558 | btrfs_free_path(path); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1559 | unlock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1560 | btrfs_commit_transaction(trans, root); |
| 1561 | return ret; |
| 1562 | } |
| 1563 | |
| 1564 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 1565 | { |
| 1566 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1567 | struct btrfs_device *next_device; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1568 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1569 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1570 | struct btrfs_super_block *disk_super; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1571 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1572 | u64 all_avail; |
| 1573 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1574 | u64 num_devices; |
| 1575 | u8 *dev_uuid; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1576 | unsigned seq; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1577 | int ret = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1578 | bool clear_super = false; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1579 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1580 | mutex_lock(&uuid_mutex); |
| 1581 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1582 | do { |
| 1583 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 1584 | |
| 1585 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 1586 | root->fs_info->avail_system_alloc_bits | |
| 1587 | root->fs_info->avail_metadata_alloc_bits; |
| 1588 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1589 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1590 | num_devices = root->fs_info->fs_devices->num_devices; |
| 1591 | btrfs_dev_replace_lock(&root->fs_info->dev_replace); |
| 1592 | if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) { |
| 1593 | WARN_ON(num_devices < 1); |
| 1594 | num_devices--; |
| 1595 | } |
| 1596 | btrfs_dev_replace_unlock(&root->fs_info->dev_replace); |
| 1597 | |
| 1598 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1599 | ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1600 | goto out; |
| 1601 | } |
| 1602 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1603 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1604 | ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1605 | goto out; |
| 1606 | } |
| 1607 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1608 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) && |
| 1609 | root->fs_info->fs_devices->rw_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1610 | ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1611 | goto out; |
| 1612 | } |
| 1613 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) && |
| 1614 | root->fs_info->fs_devices->rw_devices <= 3) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1615 | ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1616 | goto out; |
| 1617 | } |
| 1618 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1619 | if (strcmp(device_path, "missing") == 0) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1620 | struct list_head *devices; |
| 1621 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1622 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1623 | device = NULL; |
| 1624 | devices = &root->fs_info->fs_devices->devices; |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1625 | /* |
| 1626 | * It is safe to read the devices since the volume_mutex |
| 1627 | * is held. |
| 1628 | */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1629 | list_for_each_entry(tmp, devices, dev_list) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1630 | if (tmp->in_fs_metadata && |
| 1631 | !tmp->is_tgtdev_for_dev_replace && |
| 1632 | !tmp->bdev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1633 | device = tmp; |
| 1634 | break; |
| 1635 | } |
| 1636 | } |
| 1637 | bdev = NULL; |
| 1638 | bh = NULL; |
| 1639 | disk_super = NULL; |
| 1640 | if (!device) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1641 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1642 | goto out; |
| 1643 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1644 | } else { |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1645 | ret = btrfs_get_bdev_and_sb(device_path, |
Lukas Czerner | cc975eb | 2012-12-07 10:09:19 +0000 | [diff] [blame] | 1646 | FMODE_WRITE | FMODE_EXCL, |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1647 | root->fs_info->bdev_holder, 0, |
| 1648 | &bdev, &bh); |
| 1649 | if (ret) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1650 | goto out; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1651 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1652 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1653 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1654 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1655 | disk_super->fsid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1656 | if (!device) { |
| 1657 | ret = -ENOENT; |
| 1658 | goto error_brelse; |
| 1659 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1660 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1661 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1662 | if (device->is_tgtdev_for_dev_replace) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1663 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1664 | goto error_brelse; |
| 1665 | } |
| 1666 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1667 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1668 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1669 | goto error_brelse; |
| 1670 | } |
| 1671 | |
| 1672 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1673 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1674 | list_del_init(&device->dev_alloc_list); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1675 | unlock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1676 | root->fs_info->fs_devices->rw_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1677 | clear_super = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1678 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1679 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1680 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1681 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1682 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1683 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1684 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1685 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1686 | /* |
| 1687 | * TODO: the superblock still includes this device in its num_devices |
| 1688 | * counter although write_all_supers() is not locked out. This |
| 1689 | * could give a filesystem state which requires a degraded mount. |
| 1690 | */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1691 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1692 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1693 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1694 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 1695 | spin_lock(&root->fs_info->free_chunk_lock); |
| 1696 | root->fs_info->free_chunk_space = device->total_bytes - |
| 1697 | device->bytes_used; |
| 1698 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 1699 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1700 | device->in_fs_metadata = 0; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1701 | btrfs_scrub_cancel_dev(root->fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1702 | |
| 1703 | /* |
| 1704 | * the device list mutex makes sure that we don't change |
| 1705 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1706 | * the device supers. Whoever is writing all supers, should |
| 1707 | * lock the device list mutex before getting the number of |
| 1708 | * devices in the super block (super_copy). Conversely, |
| 1709 | * whoever updates the number of devices in the super block |
| 1710 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1711 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1712 | |
| 1713 | cur_devices = device->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1714 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1715 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1716 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1717 | device->fs_devices->num_devices--; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1718 | device->fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1719 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1720 | if (device->missing) |
Miao Xie | 3a7d55c | 2014-07-16 18:38:01 +0800 | [diff] [blame] | 1721 | device->fs_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1722 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1723 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1724 | struct btrfs_device, dev_list); |
| 1725 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1726 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1727 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1728 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1729 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1730 | if (device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1731 | device->fs_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1732 | /* remove sysfs entry */ |
| 1733 | btrfs_kobj_rm_device(root->fs_info, device); |
| 1734 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1735 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1736 | call_rcu(&device->rcu, free_device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1737 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1738 | num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; |
| 1739 | 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] | 1740 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1741 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1742 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1743 | struct btrfs_fs_devices *fs_devices; |
| 1744 | fs_devices = root->fs_info->fs_devices; |
| 1745 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1746 | if (fs_devices->seed == cur_devices) { |
| 1747 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1748 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1749 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1750 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1751 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1752 | cur_devices->seed = NULL; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1753 | lock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1754 | __btrfs_close_devices(cur_devices); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1755 | unlock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1756 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1757 | } |
| 1758 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1759 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1760 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
| 1761 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1762 | /* |
| 1763 | * at this point, the device is zero sized. We want to |
| 1764 | * remove it from the devices list and zero out the old super |
| 1765 | */ |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1766 | if (clear_super && disk_super) { |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1767 | u64 bytenr; |
| 1768 | int i; |
| 1769 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1770 | /* make sure this device isn't detected as part of |
| 1771 | * the FS anymore |
| 1772 | */ |
| 1773 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1774 | set_buffer_dirty(bh); |
| 1775 | sync_dirty_buffer(bh); |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1776 | |
| 1777 | /* clear the mirror copies of super block on the disk |
| 1778 | * being removed, 0th copy is been taken care above and |
| 1779 | * the below would take of the rest |
| 1780 | */ |
| 1781 | for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 1782 | bytenr = btrfs_sb_offset(i); |
| 1783 | if (bytenr + BTRFS_SUPER_INFO_SIZE >= |
| 1784 | i_size_read(bdev->bd_inode)) |
| 1785 | break; |
| 1786 | |
| 1787 | brelse(bh); |
| 1788 | bh = __bread(bdev, bytenr / 4096, |
| 1789 | BTRFS_SUPER_INFO_SIZE); |
| 1790 | if (!bh) |
| 1791 | continue; |
| 1792 | |
| 1793 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1794 | |
| 1795 | if (btrfs_super_bytenr(disk_super) != bytenr || |
| 1796 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) { |
| 1797 | continue; |
| 1798 | } |
| 1799 | memset(&disk_super->magic, 0, |
| 1800 | sizeof(disk_super->magic)); |
| 1801 | set_buffer_dirty(bh); |
| 1802 | sync_dirty_buffer(bh); |
| 1803 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1804 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1805 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1806 | ret = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1807 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1808 | if (bdev) { |
| 1809 | /* Notify udev that device has changed */ |
Eric Sandeen | 3c91160 | 2013-01-31 00:55:02 +0000 | [diff] [blame] | 1810 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 1811 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1812 | /* Update ctime/mtime for device path for libblkid */ |
| 1813 | update_dev_time(device_path); |
| 1814 | } |
| 1815 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1816 | error_brelse: |
| 1817 | brelse(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1818 | if (bdev) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1819 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1820 | out: |
| 1821 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1822 | return ret; |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1823 | error_undo: |
| 1824 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1825 | lock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1826 | list_add(&device->dev_alloc_list, |
| 1827 | &root->fs_info->fs_devices->alloc_list); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1828 | unlock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1829 | root->fs_info->fs_devices->rw_devices++; |
| 1830 | } |
| 1831 | goto error_brelse; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1832 | } |
| 1833 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1834 | void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info, |
| 1835 | struct btrfs_device *srcdev) |
| 1836 | { |
| 1837 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1838 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1839 | list_del_rcu(&srcdev->dev_list); |
| 1840 | list_del_rcu(&srcdev->dev_alloc_list); |
| 1841 | fs_info->fs_devices->num_devices--; |
| 1842 | if (srcdev->missing) { |
| 1843 | fs_info->fs_devices->missing_devices--; |
| 1844 | fs_info->fs_devices->rw_devices++; |
| 1845 | } |
| 1846 | if (srcdev->can_discard) |
| 1847 | fs_info->fs_devices->num_can_discard--; |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1848 | if (srcdev->bdev) { |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1849 | fs_info->fs_devices->open_devices--; |
| 1850 | |
Miao Xie | ff61d17 | 2014-07-24 11:37:06 +0800 | [diff] [blame] | 1851 | /* |
| 1852 | * zero out the old super if it is not writable |
| 1853 | * (e.g. seed device) |
| 1854 | */ |
| 1855 | if (srcdev->writeable) |
| 1856 | btrfs_scratch_superblock(srcdev); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1857 | } |
| 1858 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1859 | call_rcu(&srcdev->rcu, free_device); |
| 1860 | } |
| 1861 | |
| 1862 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 1863 | struct btrfs_device *tgtdev) |
| 1864 | { |
| 1865 | struct btrfs_device *next_device; |
| 1866 | |
| 1867 | WARN_ON(!tgtdev); |
| 1868 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 1869 | if (tgtdev->bdev) { |
| 1870 | btrfs_scratch_superblock(tgtdev); |
| 1871 | fs_info->fs_devices->open_devices--; |
| 1872 | } |
| 1873 | fs_info->fs_devices->num_devices--; |
| 1874 | if (tgtdev->can_discard) |
| 1875 | fs_info->fs_devices->num_can_discard++; |
| 1876 | |
| 1877 | next_device = list_entry(fs_info->fs_devices->devices.next, |
| 1878 | struct btrfs_device, dev_list); |
| 1879 | if (tgtdev->bdev == fs_info->sb->s_bdev) |
| 1880 | fs_info->sb->s_bdev = next_device->bdev; |
| 1881 | if (tgtdev->bdev == fs_info->fs_devices->latest_bdev) |
| 1882 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1883 | list_del_rcu(&tgtdev->dev_list); |
| 1884 | |
| 1885 | call_rcu(&tgtdev->rcu, free_device); |
| 1886 | |
| 1887 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
| 1888 | } |
| 1889 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1890 | static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
| 1891 | struct btrfs_device **device) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1892 | { |
| 1893 | int ret = 0; |
| 1894 | struct btrfs_super_block *disk_super; |
| 1895 | u64 devid; |
| 1896 | u8 *dev_uuid; |
| 1897 | struct block_device *bdev; |
| 1898 | struct buffer_head *bh; |
| 1899 | |
| 1900 | *device = NULL; |
| 1901 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
| 1902 | root->fs_info->bdev_holder, 0, &bdev, &bh); |
| 1903 | if (ret) |
| 1904 | return ret; |
| 1905 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1906 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 1907 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1908 | *device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1909 | disk_super->fsid); |
| 1910 | brelse(bh); |
| 1911 | if (!*device) |
| 1912 | ret = -ENOENT; |
| 1913 | blkdev_put(bdev, FMODE_READ); |
| 1914 | return ret; |
| 1915 | } |
| 1916 | |
| 1917 | int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, |
| 1918 | char *device_path, |
| 1919 | struct btrfs_device **device) |
| 1920 | { |
| 1921 | *device = NULL; |
| 1922 | if (strcmp(device_path, "missing") == 0) { |
| 1923 | struct list_head *devices; |
| 1924 | struct btrfs_device *tmp; |
| 1925 | |
| 1926 | devices = &root->fs_info->fs_devices->devices; |
| 1927 | /* |
| 1928 | * It is safe to read the devices since the volume_mutex |
| 1929 | * is held by the caller. |
| 1930 | */ |
| 1931 | list_for_each_entry(tmp, devices, dev_list) { |
| 1932 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 1933 | *device = tmp; |
| 1934 | break; |
| 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | if (!*device) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1939 | btrfs_err(root->fs_info, "no missing device found"); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1940 | return -ENOENT; |
| 1941 | } |
| 1942 | |
| 1943 | return 0; |
| 1944 | } else { |
| 1945 | return btrfs_find_device_by_path(root, device_path, device); |
| 1946 | } |
| 1947 | } |
| 1948 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1949 | /* |
| 1950 | * does all the dirty work required for changing file system's UUID. |
| 1951 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1952 | static int btrfs_prepare_sprout(struct btrfs_root *root) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1953 | { |
| 1954 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 1955 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1956 | struct btrfs_fs_devices *seed_devices; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1957 | struct btrfs_super_block *disk_super = root->fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1958 | struct btrfs_device *device; |
| 1959 | u64 super_flags; |
| 1960 | |
| 1961 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1962 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1963 | return -EINVAL; |
| 1964 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 1965 | seed_devices = __alloc_fs_devices(); |
| 1966 | if (IS_ERR(seed_devices)) |
| 1967 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1968 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1969 | old_devices = clone_fs_devices(fs_devices); |
| 1970 | if (IS_ERR(old_devices)) { |
| 1971 | kfree(seed_devices); |
| 1972 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1973 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1974 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1975 | list_add(&old_devices->list, &fs_uuids); |
| 1976 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1977 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 1978 | seed_devices->opened = 1; |
| 1979 | INIT_LIST_HEAD(&seed_devices->devices); |
| 1980 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1981 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1982 | |
| 1983 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1984 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 1985 | synchronize_rcu); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1986 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1987 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
| 1988 | list_for_each_entry(device, &seed_devices->devices, dev_list) { |
| 1989 | device->fs_devices = seed_devices; |
| 1990 | } |
| 1991 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1992 | fs_devices->seeding = 0; |
| 1993 | fs_devices->num_devices = 0; |
| 1994 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 1995 | fs_devices->missing_devices = 0; |
| 1996 | fs_devices->num_can_discard = 0; |
| 1997 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1998 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1999 | |
| 2000 | generate_random_uuid(fs_devices->fsid); |
| 2001 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 2002 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2003 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2004 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2005 | super_flags = btrfs_super_flags(disk_super) & |
| 2006 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2007 | btrfs_set_super_flags(disk_super, super_flags); |
| 2008 | |
| 2009 | return 0; |
| 2010 | } |
| 2011 | |
| 2012 | /* |
| 2013 | * strore the expected generation for seed devices in device items. |
| 2014 | */ |
| 2015 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 2016 | struct btrfs_root *root) |
| 2017 | { |
| 2018 | struct btrfs_path *path; |
| 2019 | struct extent_buffer *leaf; |
| 2020 | struct btrfs_dev_item *dev_item; |
| 2021 | struct btrfs_device *device; |
| 2022 | struct btrfs_key key; |
| 2023 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 2024 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2025 | u64 devid; |
| 2026 | int ret; |
| 2027 | |
| 2028 | path = btrfs_alloc_path(); |
| 2029 | if (!path) |
| 2030 | return -ENOMEM; |
| 2031 | |
| 2032 | root = root->fs_info->chunk_root; |
| 2033 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2034 | key.offset = 0; |
| 2035 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2036 | |
| 2037 | while (1) { |
| 2038 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2039 | if (ret < 0) |
| 2040 | goto error; |
| 2041 | |
| 2042 | leaf = path->nodes[0]; |
| 2043 | next_slot: |
| 2044 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2045 | ret = btrfs_next_leaf(root, path); |
| 2046 | if (ret > 0) |
| 2047 | break; |
| 2048 | if (ret < 0) |
| 2049 | goto error; |
| 2050 | leaf = path->nodes[0]; |
| 2051 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2052 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2053 | continue; |
| 2054 | } |
| 2055 | |
| 2056 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2057 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2058 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2059 | break; |
| 2060 | |
| 2061 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2062 | struct btrfs_dev_item); |
| 2063 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2064 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2065 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2066 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2067 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2068 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
| 2069 | fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2070 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2071 | |
| 2072 | if (device->fs_devices->seeding) { |
| 2073 | btrfs_set_device_generation(leaf, dev_item, |
| 2074 | device->generation); |
| 2075 | btrfs_mark_buffer_dirty(leaf); |
| 2076 | } |
| 2077 | |
| 2078 | path->slots[0]++; |
| 2079 | goto next_slot; |
| 2080 | } |
| 2081 | ret = 0; |
| 2082 | error: |
| 2083 | btrfs_free_path(path); |
| 2084 | return ret; |
| 2085 | } |
| 2086 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2087 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 2088 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2089 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2090 | struct btrfs_trans_handle *trans; |
| 2091 | struct btrfs_device *device; |
| 2092 | struct block_device *bdev; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2093 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2094 | struct super_block *sb = root->fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2095 | struct rcu_string *name; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2096 | u64 total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2097 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2098 | int ret = 0; |
| 2099 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2100 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2101 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2102 | |
Li Zefan | a5d1633 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2103 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2104 | root->fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2105 | if (IS_ERR(bdev)) |
| 2106 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2107 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2108 | if (root->fs_info->fs_devices->seeding) { |
| 2109 | seeding_dev = 1; |
| 2110 | down_write(&sb->s_umount); |
| 2111 | mutex_lock(&uuid_mutex); |
| 2112 | } |
| 2113 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2114 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2115 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2116 | devices = &root->fs_info->fs_devices->devices; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2117 | |
| 2118 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2119 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2120 | if (device->bdev == bdev) { |
| 2121 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2122 | mutex_unlock( |
| 2123 | &root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2124 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2125 | } |
| 2126 | } |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2127 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2128 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2129 | device = btrfs_alloc_device(root->fs_info, NULL, NULL); |
| 2130 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2131 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2132 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2133 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2134 | } |
| 2135 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2136 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2137 | if (!name) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2138 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2139 | ret = -ENOMEM; |
| 2140 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2141 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2142 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2143 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2144 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2145 | if (IS_ERR(trans)) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2146 | rcu_string_free(device->name); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2147 | kfree(device); |
| 2148 | ret = PTR_ERR(trans); |
| 2149 | goto error; |
| 2150 | } |
| 2151 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2152 | lock_chunks(root); |
| 2153 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2154 | q = bdev_get_queue(bdev); |
| 2155 | if (blk_queue_discard(q)) |
| 2156 | device->can_discard = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2157 | device->writeable = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2158 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2159 | device->io_width = root->sectorsize; |
| 2160 | device->io_align = root->sectorsize; |
| 2161 | device->sector_size = root->sectorsize; |
| 2162 | device->total_bytes = i_size_read(bdev->bd_inode); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2163 | device->disk_total_bytes = device->total_bytes; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2164 | device->dev_root = root->fs_info->dev_root; |
| 2165 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2166 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2167 | device->is_tgtdev_for_dev_replace = 0; |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2168 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2169 | device->dev_stats_valid = 1; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2170 | set_blocksize(device->bdev, 4096); |
| 2171 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2172 | if (seeding_dev) { |
| 2173 | sb->s_flags &= ~MS_RDONLY; |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2174 | ret = btrfs_prepare_sprout(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2175 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | device->fs_devices = root->fs_info->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2179 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2180 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2181 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2182 | list_add(&device->dev_alloc_list, |
| 2183 | &root->fs_info->fs_devices->alloc_list); |
| 2184 | root->fs_info->fs_devices->num_devices++; |
| 2185 | root->fs_info->fs_devices->open_devices++; |
| 2186 | root->fs_info->fs_devices->rw_devices++; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 2187 | root->fs_info->fs_devices->total_devices++; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2188 | if (device->can_discard) |
| 2189 | root->fs_info->fs_devices->num_can_discard++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2190 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 2191 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2192 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2193 | root->fs_info->free_chunk_space += device->total_bytes; |
| 2194 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2195 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2196 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 2197 | root->fs_info->fs_devices->rotating = 1; |
| 2198 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2199 | total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy); |
| 2200 | btrfs_set_super_total_bytes(root->fs_info->super_copy, |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2201 | total_bytes + device->total_bytes); |
| 2202 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2203 | total_bytes = btrfs_super_num_devices(root->fs_info->super_copy); |
| 2204 | btrfs_set_super_num_devices(root->fs_info->super_copy, |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2205 | total_bytes + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2206 | |
| 2207 | /* add sysfs device entry */ |
| 2208 | btrfs_kobj_add_device(root->fs_info, device); |
| 2209 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2210 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2211 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2212 | if (seeding_dev) { |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2213 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2214 | ret = init_first_rw_device(trans, root, device); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2215 | if (ret) { |
| 2216 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2217 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2218 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2219 | ret = btrfs_finish_sprout(trans, root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2220 | if (ret) { |
| 2221 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2222 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2223 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2224 | |
| 2225 | /* Sprouting would change fsid of the mounted root, |
| 2226 | * so rename the fsid on the sysfs |
| 2227 | */ |
| 2228 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
| 2229 | root->fs_info->fsid); |
| 2230 | if (kobject_rename(&root->fs_info->super_kobj, fsid_buf)) |
| 2231 | goto error_trans; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2232 | } else { |
| 2233 | ret = btrfs_add_device(trans, root, device); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2234 | if (ret) { |
| 2235 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2236 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2237 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2238 | } |
| 2239 | |
Chris Mason | 913d952 | 2009-03-10 13:17:18 -0400 | [diff] [blame] | 2240 | /* |
| 2241 | * we've got more storage, clear any full flags on the space |
| 2242 | * infos |
| 2243 | */ |
| 2244 | btrfs_clear_space_info_full(root->fs_info); |
| 2245 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2246 | unlock_chunks(root); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2247 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 2248 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2249 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2250 | |
| 2251 | if (seeding_dev) { |
| 2252 | mutex_unlock(&uuid_mutex); |
| 2253 | up_write(&sb->s_umount); |
| 2254 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2255 | if (ret) /* transaction commit */ |
| 2256 | return ret; |
| 2257 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2258 | ret = btrfs_relocate_sys_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2259 | if (ret < 0) |
| 2260 | btrfs_error(root->fs_info, ret, |
| 2261 | "Failed to relocate sys chunks after " |
| 2262 | "device initialization. This can be fixed " |
| 2263 | "using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2264 | trans = btrfs_attach_transaction(root); |
| 2265 | if (IS_ERR(trans)) { |
| 2266 | if (PTR_ERR(trans) == -ENOENT) |
| 2267 | return 0; |
| 2268 | return PTR_ERR(trans); |
| 2269 | } |
| 2270 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2271 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2272 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2273 | /* Update ctime/mtime for libblkid */ |
| 2274 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2275 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2276 | |
| 2277 | error_trans: |
| 2278 | unlock_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2279 | btrfs_end_transaction(trans, root); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2280 | rcu_string_free(device->name); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2281 | btrfs_kobj_rm_device(root->fs_info, device); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2282 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2283 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2284 | blkdev_put(bdev, FMODE_EXCL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2285 | if (seeding_dev) { |
| 2286 | mutex_unlock(&uuid_mutex); |
| 2287 | up_write(&sb->s_umount); |
| 2288 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2289 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2290 | } |
| 2291 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2292 | int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path, |
| 2293 | struct btrfs_device **device_out) |
| 2294 | { |
| 2295 | struct request_queue *q; |
| 2296 | struct btrfs_device *device; |
| 2297 | struct block_device *bdev; |
| 2298 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2299 | struct list_head *devices; |
| 2300 | struct rcu_string *name; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2301 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2302 | int ret = 0; |
| 2303 | |
| 2304 | *device_out = NULL; |
| 2305 | if (fs_info->fs_devices->seeding) |
| 2306 | return -EINVAL; |
| 2307 | |
| 2308 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| 2309 | fs_info->bdev_holder); |
| 2310 | if (IS_ERR(bdev)) |
| 2311 | return PTR_ERR(bdev); |
| 2312 | |
| 2313 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 2314 | |
| 2315 | devices = &fs_info->fs_devices->devices; |
| 2316 | list_for_each_entry(device, devices, dev_list) { |
| 2317 | if (device->bdev == bdev) { |
| 2318 | ret = -EEXIST; |
| 2319 | goto error; |
| 2320 | } |
| 2321 | } |
| 2322 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2323 | device = btrfs_alloc_device(NULL, &devid, NULL); |
| 2324 | if (IS_ERR(device)) { |
| 2325 | ret = PTR_ERR(device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2326 | goto error; |
| 2327 | } |
| 2328 | |
| 2329 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2330 | if (!name) { |
| 2331 | kfree(device); |
| 2332 | ret = -ENOMEM; |
| 2333 | goto error; |
| 2334 | } |
| 2335 | rcu_assign_pointer(device->name, name); |
| 2336 | |
| 2337 | q = bdev_get_queue(bdev); |
| 2338 | if (blk_queue_discard(q)) |
| 2339 | device->can_discard = 1; |
| 2340 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 2341 | device->writeable = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2342 | device->generation = 0; |
| 2343 | device->io_width = root->sectorsize; |
| 2344 | device->io_align = root->sectorsize; |
| 2345 | device->sector_size = root->sectorsize; |
| 2346 | device->total_bytes = i_size_read(bdev->bd_inode); |
| 2347 | device->disk_total_bytes = device->total_bytes; |
| 2348 | device->dev_root = fs_info->dev_root; |
| 2349 | device->bdev = bdev; |
| 2350 | device->in_fs_metadata = 1; |
| 2351 | device->is_tgtdev_for_dev_replace = 1; |
| 2352 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2353 | device->dev_stats_valid = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2354 | set_blocksize(device->bdev, 4096); |
| 2355 | device->fs_devices = fs_info->fs_devices; |
| 2356 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 2357 | fs_info->fs_devices->num_devices++; |
| 2358 | fs_info->fs_devices->open_devices++; |
| 2359 | if (device->can_discard) |
| 2360 | fs_info->fs_devices->num_can_discard++; |
| 2361 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2362 | |
| 2363 | *device_out = device; |
| 2364 | return ret; |
| 2365 | |
| 2366 | error: |
| 2367 | blkdev_put(bdev, FMODE_EXCL); |
| 2368 | return ret; |
| 2369 | } |
| 2370 | |
| 2371 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, |
| 2372 | struct btrfs_device *tgtdev) |
| 2373 | { |
| 2374 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 2375 | tgtdev->io_width = fs_info->dev_root->sectorsize; |
| 2376 | tgtdev->io_align = fs_info->dev_root->sectorsize; |
| 2377 | tgtdev->sector_size = fs_info->dev_root->sectorsize; |
| 2378 | tgtdev->dev_root = fs_info->dev_root; |
| 2379 | tgtdev->in_fs_metadata = 1; |
| 2380 | } |
| 2381 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2382 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2383 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2384 | { |
| 2385 | int ret; |
| 2386 | struct btrfs_path *path; |
| 2387 | struct btrfs_root *root; |
| 2388 | struct btrfs_dev_item *dev_item; |
| 2389 | struct extent_buffer *leaf; |
| 2390 | struct btrfs_key key; |
| 2391 | |
| 2392 | root = device->dev_root->fs_info->chunk_root; |
| 2393 | |
| 2394 | path = btrfs_alloc_path(); |
| 2395 | if (!path) |
| 2396 | return -ENOMEM; |
| 2397 | |
| 2398 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2399 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2400 | key.offset = device->devid; |
| 2401 | |
| 2402 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2403 | if (ret < 0) |
| 2404 | goto out; |
| 2405 | |
| 2406 | if (ret > 0) { |
| 2407 | ret = -ENOENT; |
| 2408 | goto out; |
| 2409 | } |
| 2410 | |
| 2411 | leaf = path->nodes[0]; |
| 2412 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2413 | |
| 2414 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2415 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2416 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2417 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2418 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 2419 | btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2420 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
| 2421 | btrfs_mark_buffer_dirty(leaf); |
| 2422 | |
| 2423 | out: |
| 2424 | btrfs_free_path(path); |
| 2425 | return ret; |
| 2426 | } |
| 2427 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2428 | static int __btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2429 | struct btrfs_device *device, u64 new_size) |
| 2430 | { |
| 2431 | struct btrfs_super_block *super_copy = |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2432 | device->dev_root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2433 | u64 old_total = btrfs_super_total_bytes(super_copy); |
| 2434 | u64 diff = new_size - device->total_bytes; |
| 2435 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2436 | if (!device->writeable) |
| 2437 | return -EACCES; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2438 | if (new_size <= device->total_bytes || |
| 2439 | device->is_tgtdev_for_dev_replace) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2440 | return -EINVAL; |
| 2441 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2442 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2443 | device->fs_devices->total_rw_bytes += diff; |
| 2444 | |
| 2445 | device->total_bytes = new_size; |
Chris Mason | 9779b72 | 2009-07-24 16:41:41 -0400 | [diff] [blame] | 2446 | device->disk_total_bytes = new_size; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2447 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
| 2448 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2449 | return btrfs_update_device(trans, device); |
| 2450 | } |
| 2451 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2452 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
| 2453 | struct btrfs_device *device, u64 new_size) |
| 2454 | { |
| 2455 | int ret; |
| 2456 | lock_chunks(device->dev_root); |
| 2457 | ret = __btrfs_grow_device(trans, device, new_size); |
| 2458 | unlock_chunks(device->dev_root); |
| 2459 | return ret; |
| 2460 | } |
| 2461 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2462 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
| 2463 | struct btrfs_root *root, |
| 2464 | u64 chunk_tree, u64 chunk_objectid, |
| 2465 | u64 chunk_offset) |
| 2466 | { |
| 2467 | int ret; |
| 2468 | struct btrfs_path *path; |
| 2469 | struct btrfs_key key; |
| 2470 | |
| 2471 | root = root->fs_info->chunk_root; |
| 2472 | path = btrfs_alloc_path(); |
| 2473 | if (!path) |
| 2474 | return -ENOMEM; |
| 2475 | |
| 2476 | key.objectid = chunk_objectid; |
| 2477 | key.offset = chunk_offset; |
| 2478 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2479 | |
| 2480 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2481 | if (ret < 0) |
| 2482 | goto out; |
| 2483 | else if (ret > 0) { /* Logic error or corruption */ |
| 2484 | btrfs_error(root->fs_info, -ENOENT, |
| 2485 | "Failed lookup while freeing chunk."); |
| 2486 | ret = -ENOENT; |
| 2487 | goto out; |
| 2488 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2489 | |
| 2490 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2491 | if (ret < 0) |
| 2492 | btrfs_error(root->fs_info, ret, |
| 2493 | "Failed to delete chunk item."); |
| 2494 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2495 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2496 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2497 | } |
| 2498 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2499 | 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] | 2500 | chunk_offset) |
| 2501 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2502 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2503 | struct btrfs_disk_key *disk_key; |
| 2504 | struct btrfs_chunk *chunk; |
| 2505 | u8 *ptr; |
| 2506 | int ret = 0; |
| 2507 | u32 num_stripes; |
| 2508 | u32 array_size; |
| 2509 | u32 len = 0; |
| 2510 | u32 cur; |
| 2511 | struct btrfs_key key; |
| 2512 | |
| 2513 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2514 | |
| 2515 | ptr = super_copy->sys_chunk_array; |
| 2516 | cur = 0; |
| 2517 | |
| 2518 | while (cur < array_size) { |
| 2519 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2520 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2521 | |
| 2522 | len = sizeof(*disk_key); |
| 2523 | |
| 2524 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2525 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2526 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2527 | len += btrfs_chunk_item_size(num_stripes); |
| 2528 | } else { |
| 2529 | ret = -EIO; |
| 2530 | break; |
| 2531 | } |
| 2532 | if (key.objectid == chunk_objectid && |
| 2533 | key.offset == chunk_offset) { |
| 2534 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2535 | array_size -= len; |
| 2536 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2537 | } else { |
| 2538 | ptr += len; |
| 2539 | cur += len; |
| 2540 | } |
| 2541 | } |
| 2542 | return ret; |
| 2543 | } |
| 2544 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2545 | static int btrfs_relocate_chunk(struct btrfs_root *root, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2546 | u64 chunk_tree, u64 chunk_objectid, |
| 2547 | u64 chunk_offset) |
| 2548 | { |
| 2549 | struct extent_map_tree *em_tree; |
| 2550 | struct btrfs_root *extent_root; |
| 2551 | struct btrfs_trans_handle *trans; |
| 2552 | struct extent_map *em; |
| 2553 | struct map_lookup *map; |
| 2554 | int ret; |
| 2555 | int i; |
| 2556 | |
| 2557 | root = root->fs_info->chunk_root; |
| 2558 | extent_root = root->fs_info->extent_root; |
| 2559 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 2560 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2561 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
| 2562 | if (ret) |
| 2563 | return -ENOSPC; |
| 2564 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2565 | /* step one, relocate all the extents inside this chunk */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2566 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2567 | if (ret) |
| 2568 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2569 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2570 | trans = btrfs_start_transaction(root, 0); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 2571 | if (IS_ERR(trans)) { |
| 2572 | ret = PTR_ERR(trans); |
| 2573 | btrfs_std_error(root->fs_info, ret); |
| 2574 | return ret; |
| 2575 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2576 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2577 | lock_chunks(root); |
| 2578 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2579 | /* |
| 2580 | * step two, delete the device extents and the |
| 2581 | * chunk tree entries |
| 2582 | */ |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2583 | read_lock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2584 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2585 | read_unlock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2586 | |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 2587 | BUG_ON(!em || em->start > chunk_offset || |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2588 | em->start + em->len < chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2589 | map = (struct map_lookup *)em->bdev; |
| 2590 | |
| 2591 | for (i = 0; i < map->num_stripes; i++) { |
| 2592 | ret = btrfs_free_dev_extent(trans, map->stripes[i].dev, |
| 2593 | map->stripes[i].physical); |
| 2594 | BUG_ON(ret); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2595 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2596 | if (map->stripes[i].dev) { |
| 2597 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2598 | BUG_ON(ret); |
| 2599 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2600 | } |
| 2601 | ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid, |
| 2602 | chunk_offset); |
| 2603 | |
| 2604 | BUG_ON(ret); |
| 2605 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2606 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
| 2607 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2608 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2609 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 2610 | BUG_ON(ret); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2611 | } |
| 2612 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2613 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset); |
| 2614 | BUG_ON(ret); |
| 2615 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2616 | write_lock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2617 | remove_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2618 | write_unlock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2619 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2620 | /* once for the tree */ |
| 2621 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2622 | /* once for us */ |
| 2623 | free_extent_map(em); |
| 2624 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2625 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2626 | btrfs_end_transaction(trans, root); |
| 2627 | return 0; |
| 2628 | } |
| 2629 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2630 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 2631 | { |
| 2632 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 2633 | struct btrfs_path *path; |
| 2634 | struct extent_buffer *leaf; |
| 2635 | struct btrfs_chunk *chunk; |
| 2636 | struct btrfs_key key; |
| 2637 | struct btrfs_key found_key; |
| 2638 | u64 chunk_tree = chunk_root->root_key.objectid; |
| 2639 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2640 | bool retried = false; |
| 2641 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2642 | int ret; |
| 2643 | |
| 2644 | path = btrfs_alloc_path(); |
| 2645 | if (!path) |
| 2646 | return -ENOMEM; |
| 2647 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2648 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2649 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2650 | key.offset = (u64)-1; |
| 2651 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2652 | |
| 2653 | while (1) { |
| 2654 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 2655 | if (ret < 0) |
| 2656 | goto error; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2657 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2658 | |
| 2659 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2660 | key.type); |
| 2661 | if (ret < 0) |
| 2662 | goto error; |
| 2663 | if (ret > 0) |
| 2664 | break; |
| 2665 | |
| 2666 | leaf = path->nodes[0]; |
| 2667 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2668 | |
| 2669 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2670 | struct btrfs_chunk); |
| 2671 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2672 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2673 | |
| 2674 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2675 | ret = btrfs_relocate_chunk(chunk_root, chunk_tree, |
| 2676 | found_key.objectid, |
| 2677 | found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2678 | if (ret == -ENOSPC) |
| 2679 | failed++; |
| 2680 | else if (ret) |
| 2681 | BUG(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | if (found_key.offset == 0) |
| 2685 | break; |
| 2686 | key.offset = found_key.offset - 1; |
| 2687 | } |
| 2688 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2689 | if (failed && !retried) { |
| 2690 | failed = 0; |
| 2691 | retried = true; |
| 2692 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2693 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2694 | ret = -ENOSPC; |
| 2695 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2696 | error: |
| 2697 | btrfs_free_path(path); |
| 2698 | return ret; |
| 2699 | } |
| 2700 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2701 | static int insert_balance_item(struct btrfs_root *root, |
| 2702 | struct btrfs_balance_control *bctl) |
| 2703 | { |
| 2704 | struct btrfs_trans_handle *trans; |
| 2705 | struct btrfs_balance_item *item; |
| 2706 | struct btrfs_disk_balance_args disk_bargs; |
| 2707 | struct btrfs_path *path; |
| 2708 | struct extent_buffer *leaf; |
| 2709 | struct btrfs_key key; |
| 2710 | int ret, err; |
| 2711 | |
| 2712 | path = btrfs_alloc_path(); |
| 2713 | if (!path) |
| 2714 | return -ENOMEM; |
| 2715 | |
| 2716 | trans = btrfs_start_transaction(root, 0); |
| 2717 | if (IS_ERR(trans)) { |
| 2718 | btrfs_free_path(path); |
| 2719 | return PTR_ERR(trans); |
| 2720 | } |
| 2721 | |
| 2722 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2723 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2724 | key.offset = 0; |
| 2725 | |
| 2726 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2727 | sizeof(*item)); |
| 2728 | if (ret) |
| 2729 | goto out; |
| 2730 | |
| 2731 | leaf = path->nodes[0]; |
| 2732 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 2733 | |
| 2734 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 2735 | |
| 2736 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 2737 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 2738 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 2739 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 2740 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 2741 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 2742 | |
| 2743 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 2744 | |
| 2745 | btrfs_mark_buffer_dirty(leaf); |
| 2746 | out: |
| 2747 | btrfs_free_path(path); |
| 2748 | err = btrfs_commit_transaction(trans, root); |
| 2749 | if (err && !ret) |
| 2750 | ret = err; |
| 2751 | return ret; |
| 2752 | } |
| 2753 | |
| 2754 | static int del_balance_item(struct btrfs_root *root) |
| 2755 | { |
| 2756 | struct btrfs_trans_handle *trans; |
| 2757 | struct btrfs_path *path; |
| 2758 | struct btrfs_key key; |
| 2759 | int ret, err; |
| 2760 | |
| 2761 | path = btrfs_alloc_path(); |
| 2762 | if (!path) |
| 2763 | return -ENOMEM; |
| 2764 | |
| 2765 | trans = btrfs_start_transaction(root, 0); |
| 2766 | if (IS_ERR(trans)) { |
| 2767 | btrfs_free_path(path); |
| 2768 | return PTR_ERR(trans); |
| 2769 | } |
| 2770 | |
| 2771 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2772 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2773 | key.offset = 0; |
| 2774 | |
| 2775 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2776 | if (ret < 0) |
| 2777 | goto out; |
| 2778 | if (ret > 0) { |
| 2779 | ret = -ENOENT; |
| 2780 | goto out; |
| 2781 | } |
| 2782 | |
| 2783 | ret = btrfs_del_item(trans, root, path); |
| 2784 | out: |
| 2785 | btrfs_free_path(path); |
| 2786 | err = btrfs_commit_transaction(trans, root); |
| 2787 | if (err && !ret) |
| 2788 | ret = err; |
| 2789 | return ret; |
| 2790 | } |
| 2791 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2792 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2793 | * This is a heuristic used to reduce the number of chunks balanced on |
| 2794 | * resume after balance was interrupted. |
| 2795 | */ |
| 2796 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 2797 | { |
| 2798 | /* |
| 2799 | * Turn on soft mode for chunk types that were being converted. |
| 2800 | */ |
| 2801 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2802 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2803 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2804 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2805 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2806 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2807 | |
| 2808 | /* |
| 2809 | * Turn on usage filter if is not already used. The idea is |
| 2810 | * that chunks that we have already balanced should be |
| 2811 | * reasonably full. Don't do it for chunks that are being |
| 2812 | * converted - that will keep us from relocating unconverted |
| 2813 | * (albeit full) chunks. |
| 2814 | */ |
| 2815 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2816 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2817 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2818 | bctl->data.usage = 90; |
| 2819 | } |
| 2820 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2821 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2822 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2823 | bctl->sys.usage = 90; |
| 2824 | } |
| 2825 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2826 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2827 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2828 | bctl->meta.usage = 90; |
| 2829 | } |
| 2830 | } |
| 2831 | |
| 2832 | /* |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2833 | * Should be called with both balance and volume mutexes held to |
| 2834 | * serialize other volume operations (add_dev/rm_dev/resize) with |
| 2835 | * restriper. Same goes for unset_balance_control. |
| 2836 | */ |
| 2837 | static void set_balance_control(struct btrfs_balance_control *bctl) |
| 2838 | { |
| 2839 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
| 2840 | |
| 2841 | BUG_ON(fs_info->balance_ctl); |
| 2842 | |
| 2843 | spin_lock(&fs_info->balance_lock); |
| 2844 | fs_info->balance_ctl = bctl; |
| 2845 | spin_unlock(&fs_info->balance_lock); |
| 2846 | } |
| 2847 | |
| 2848 | static void unset_balance_control(struct btrfs_fs_info *fs_info) |
| 2849 | { |
| 2850 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 2851 | |
| 2852 | BUG_ON(!fs_info->balance_ctl); |
| 2853 | |
| 2854 | spin_lock(&fs_info->balance_lock); |
| 2855 | fs_info->balance_ctl = NULL; |
| 2856 | spin_unlock(&fs_info->balance_lock); |
| 2857 | |
| 2858 | kfree(bctl); |
| 2859 | } |
| 2860 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2861 | /* |
| 2862 | * Balance filters. Return 1 if chunk should be filtered out |
| 2863 | * (should not be balanced). |
| 2864 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2865 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2866 | struct btrfs_balance_args *bargs) |
| 2867 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2868 | chunk_type = chunk_to_extended(chunk_type) & |
| 2869 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2870 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2871 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2872 | return 0; |
| 2873 | |
| 2874 | return 1; |
| 2875 | } |
| 2876 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2877 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
| 2878 | struct btrfs_balance_args *bargs) |
| 2879 | { |
| 2880 | struct btrfs_block_group_cache *cache; |
| 2881 | u64 chunk_used, user_thresh; |
| 2882 | int ret = 1; |
| 2883 | |
| 2884 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 2885 | chunk_used = btrfs_block_group_used(&cache->item); |
| 2886 | |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 2887 | if (bargs->usage == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 2888 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 2889 | else if (bargs->usage > 100) |
| 2890 | user_thresh = cache->key.offset; |
| 2891 | else |
| 2892 | user_thresh = div_factor_fine(cache->key.offset, |
| 2893 | bargs->usage); |
| 2894 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2895 | if (chunk_used < user_thresh) |
| 2896 | ret = 0; |
| 2897 | |
| 2898 | btrfs_put_block_group(cache); |
| 2899 | return ret; |
| 2900 | } |
| 2901 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2902 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 2903 | struct btrfs_chunk *chunk, |
| 2904 | struct btrfs_balance_args *bargs) |
| 2905 | { |
| 2906 | struct btrfs_stripe *stripe; |
| 2907 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2908 | int i; |
| 2909 | |
| 2910 | for (i = 0; i < num_stripes; i++) { |
| 2911 | stripe = btrfs_stripe_nr(chunk, i); |
| 2912 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 2913 | return 0; |
| 2914 | } |
| 2915 | |
| 2916 | return 1; |
| 2917 | } |
| 2918 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2919 | /* [pstart, pend) */ |
| 2920 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 2921 | struct btrfs_chunk *chunk, |
| 2922 | u64 chunk_offset, |
| 2923 | struct btrfs_balance_args *bargs) |
| 2924 | { |
| 2925 | struct btrfs_stripe *stripe; |
| 2926 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2927 | u64 stripe_offset; |
| 2928 | u64 stripe_length; |
| 2929 | int factor; |
| 2930 | int i; |
| 2931 | |
| 2932 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 2933 | return 0; |
| 2934 | |
| 2935 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2936 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 2937 | factor = num_stripes / 2; |
| 2938 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 2939 | factor = num_stripes - 1; |
| 2940 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 2941 | factor = num_stripes - 2; |
| 2942 | } else { |
| 2943 | factor = num_stripes; |
| 2944 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2945 | |
| 2946 | for (i = 0; i < num_stripes; i++) { |
| 2947 | stripe = btrfs_stripe_nr(chunk, i); |
| 2948 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 2949 | continue; |
| 2950 | |
| 2951 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 2952 | stripe_length = btrfs_chunk_length(leaf, chunk); |
| 2953 | do_div(stripe_length, factor); |
| 2954 | |
| 2955 | if (stripe_offset < bargs->pend && |
| 2956 | stripe_offset + stripe_length > bargs->pstart) |
| 2957 | return 0; |
| 2958 | } |
| 2959 | |
| 2960 | return 1; |
| 2961 | } |
| 2962 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2963 | /* [vstart, vend) */ |
| 2964 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 2965 | struct btrfs_chunk *chunk, |
| 2966 | u64 chunk_offset, |
| 2967 | struct btrfs_balance_args *bargs) |
| 2968 | { |
| 2969 | if (chunk_offset < bargs->vend && |
| 2970 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 2971 | /* at least part of the chunk is inside this vrange */ |
| 2972 | return 0; |
| 2973 | |
| 2974 | return 1; |
| 2975 | } |
| 2976 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2977 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2978 | struct btrfs_balance_args *bargs) |
| 2979 | { |
| 2980 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 2981 | return 0; |
| 2982 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2983 | chunk_type = chunk_to_extended(chunk_type) & |
| 2984 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2985 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2986 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2987 | return 1; |
| 2988 | |
| 2989 | return 0; |
| 2990 | } |
| 2991 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2992 | static int should_balance_chunk(struct btrfs_root *root, |
| 2993 | struct extent_buffer *leaf, |
| 2994 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 2995 | { |
| 2996 | struct btrfs_balance_control *bctl = root->fs_info->balance_ctl; |
| 2997 | struct btrfs_balance_args *bargs = NULL; |
| 2998 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 2999 | |
| 3000 | /* type filter */ |
| 3001 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3002 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3003 | return 0; |
| 3004 | } |
| 3005 | |
| 3006 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3007 | bargs = &bctl->data; |
| 3008 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3009 | bargs = &bctl->sys; |
| 3010 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3011 | bargs = &bctl->meta; |
| 3012 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3013 | /* profiles filter */ |
| 3014 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3015 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3016 | return 0; |
| 3017 | } |
| 3018 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3019 | /* usage filter */ |
| 3020 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3021 | chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) { |
| 3022 | return 0; |
| 3023 | } |
| 3024 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3025 | /* devid filter */ |
| 3026 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3027 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3028 | return 0; |
| 3029 | } |
| 3030 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3031 | /* drange filter, makes sense only with devid filter */ |
| 3032 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
| 3033 | chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3034 | return 0; |
| 3035 | } |
| 3036 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3037 | /* vrange filter */ |
| 3038 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3039 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3040 | return 0; |
| 3041 | } |
| 3042 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3043 | /* soft profile changing mode */ |
| 3044 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3045 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3046 | return 0; |
| 3047 | } |
| 3048 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3049 | /* |
| 3050 | * limited by count, must be the last filter |
| 3051 | */ |
| 3052 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3053 | if (bargs->limit == 0) |
| 3054 | return 0; |
| 3055 | else |
| 3056 | bargs->limit--; |
| 3057 | } |
| 3058 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3059 | return 1; |
| 3060 | } |
| 3061 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3062 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3063 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3064 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3065 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 3066 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 3067 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3068 | struct btrfs_device *device; |
| 3069 | u64 old_size; |
| 3070 | u64 size_to_free; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3071 | struct btrfs_chunk *chunk; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3072 | struct btrfs_path *path; |
| 3073 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3074 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3075 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3076 | struct extent_buffer *leaf; |
| 3077 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3078 | int ret; |
| 3079 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3080 | bool counting = true; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3081 | u64 limit_data = bctl->data.limit; |
| 3082 | u64 limit_meta = bctl->meta.limit; |
| 3083 | u64 limit_sys = bctl->sys.limit; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3084 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3085 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3086 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3087 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3088 | old_size = device->total_bytes; |
| 3089 | size_to_free = div_factor(old_size, 1); |
| 3090 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3091 | if (!device->writeable || |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3092 | device->total_bytes - device->bytes_used > size_to_free || |
| 3093 | device->is_tgtdev_for_dev_replace) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3094 | continue; |
| 3095 | |
| 3096 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3097 | if (ret == -ENOSPC) |
| 3098 | break; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3099 | BUG_ON(ret); |
| 3100 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3101 | trans = btrfs_start_transaction(dev_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3102 | BUG_ON(IS_ERR(trans)); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3103 | |
| 3104 | ret = btrfs_grow_device(trans, device, old_size); |
| 3105 | BUG_ON(ret); |
| 3106 | |
| 3107 | btrfs_end_transaction(trans, dev_root); |
| 3108 | } |
| 3109 | |
| 3110 | /* step two, relocate all the chunks */ |
| 3111 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3112 | if (!path) { |
| 3113 | ret = -ENOMEM; |
| 3114 | goto error; |
| 3115 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3116 | |
| 3117 | /* zero out stat counters */ |
| 3118 | spin_lock(&fs_info->balance_lock); |
| 3119 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3120 | spin_unlock(&fs_info->balance_lock); |
| 3121 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3122 | if (!counting) { |
| 3123 | bctl->data.limit = limit_data; |
| 3124 | bctl->meta.limit = limit_meta; |
| 3125 | bctl->sys.limit = limit_sys; |
| 3126 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3127 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3128 | key.offset = (u64)-1; |
| 3129 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3130 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3131 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3132 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3133 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3134 | ret = -ECANCELED; |
| 3135 | goto error; |
| 3136 | } |
| 3137 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3138 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 3139 | if (ret < 0) |
| 3140 | goto error; |
| 3141 | |
| 3142 | /* |
| 3143 | * this shouldn't happen, it means the last relocate |
| 3144 | * failed |
| 3145 | */ |
| 3146 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3147 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3148 | |
| 3149 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3150 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3151 | if (ret) { |
| 3152 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3153 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3154 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3155 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3156 | leaf = path->nodes[0]; |
| 3157 | slot = path->slots[0]; |
| 3158 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3159 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3160 | if (found_key.objectid != key.objectid) |
| 3161 | break; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3162 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3163 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 3164 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3165 | if (!counting) { |
| 3166 | spin_lock(&fs_info->balance_lock); |
| 3167 | bctl->stat.considered++; |
| 3168 | spin_unlock(&fs_info->balance_lock); |
| 3169 | } |
| 3170 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3171 | ret = should_balance_chunk(chunk_root, leaf, chunk, |
| 3172 | found_key.offset); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3173 | btrfs_release_path(path); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3174 | if (!ret) |
| 3175 | goto loop; |
| 3176 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3177 | if (counting) { |
| 3178 | spin_lock(&fs_info->balance_lock); |
| 3179 | bctl->stat.expected++; |
| 3180 | spin_unlock(&fs_info->balance_lock); |
| 3181 | goto loop; |
| 3182 | } |
| 3183 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3184 | ret = btrfs_relocate_chunk(chunk_root, |
| 3185 | chunk_root->root_key.objectid, |
| 3186 | found_key.objectid, |
| 3187 | found_key.offset); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 3188 | if (ret && ret != -ENOSPC) |
| 3189 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3190 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3191 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3192 | } else { |
| 3193 | spin_lock(&fs_info->balance_lock); |
| 3194 | bctl->stat.completed++; |
| 3195 | spin_unlock(&fs_info->balance_lock); |
| 3196 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3197 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3198 | if (found_key.offset == 0) |
| 3199 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3200 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3201 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3202 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3203 | if (counting) { |
| 3204 | btrfs_release_path(path); |
| 3205 | counting = false; |
| 3206 | goto again; |
| 3207 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3208 | error: |
| 3209 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3210 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3211 | btrfs_info(fs_info, "%d enospc errors during balance", |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3212 | enospc_errors); |
| 3213 | if (!ret) |
| 3214 | ret = -ENOSPC; |
| 3215 | } |
| 3216 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3217 | return ret; |
| 3218 | } |
| 3219 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3220 | /** |
| 3221 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3222 | * @flags: profile to validate |
| 3223 | * @extended: if true @flags is treated as an extended profile |
| 3224 | */ |
| 3225 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3226 | { |
| 3227 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3228 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3229 | |
| 3230 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3231 | |
| 3232 | /* 1) check that all other bits are zeroed */ |
| 3233 | if (flags & ~mask) |
| 3234 | return 0; |
| 3235 | |
| 3236 | /* 2) see if profile is reduced */ |
| 3237 | if (flags == 0) |
| 3238 | return !extended; /* "0" is valid for usual profiles */ |
| 3239 | |
| 3240 | /* true if exactly one bit set */ |
| 3241 | return (flags & (flags - 1)) == 0; |
| 3242 | } |
| 3243 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3244 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3245 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3246 | /* cancel requested || normal exit path */ |
| 3247 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3248 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3249 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3250 | } |
| 3251 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3252 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
| 3253 | { |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3254 | int ret; |
| 3255 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3256 | unset_balance_control(fs_info); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3257 | ret = del_balance_item(fs_info->tree_root); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 3258 | if (ret) |
| 3259 | btrfs_std_error(fs_info, ret); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3260 | |
| 3261 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3262 | } |
| 3263 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3264 | /* |
| 3265 | * Should be called with both balance and volume mutexes held |
| 3266 | */ |
| 3267 | int btrfs_balance(struct btrfs_balance_control *bctl, |
| 3268 | struct btrfs_ioctl_balance_args *bargs) |
| 3269 | { |
| 3270 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3271 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3272 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3273 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3274 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3275 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3276 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3277 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3278 | atomic_read(&fs_info->balance_pause_req) || |
| 3279 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3280 | ret = -EINVAL; |
| 3281 | goto out; |
| 3282 | } |
| 3283 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3284 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3285 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3286 | mixed = 1; |
| 3287 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3288 | /* |
| 3289 | * In case of mixed groups both data and meta should be picked, |
| 3290 | * and identical options should be given for both of them. |
| 3291 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3292 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3293 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3294 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3295 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3296 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3297 | btrfs_err(fs_info, "with mixed groups data and " |
| 3298 | "metadata balance options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3299 | ret = -EINVAL; |
| 3300 | goto out; |
| 3301 | } |
| 3302 | } |
| 3303 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3304 | num_devices = fs_info->fs_devices->num_devices; |
| 3305 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 3306 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 3307 | BUG_ON(num_devices < 1); |
| 3308 | num_devices--; |
| 3309 | } |
| 3310 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3311 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3312 | if (num_devices == 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3313 | allowed |= BTRFS_BLOCK_GROUP_DUP; |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3314 | else if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3315 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3316 | if (num_devices > 2) |
| 3317 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 3318 | if (num_devices > 3) |
| 3319 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 3320 | BTRFS_BLOCK_GROUP_RAID6); |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3321 | if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3322 | (!alloc_profile_is_valid(bctl->data.target, 1) || |
| 3323 | (bctl->data.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3324 | btrfs_err(fs_info, "unable to start balance with target " |
| 3325 | "data profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3326 | bctl->data.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3327 | ret = -EINVAL; |
| 3328 | goto out; |
| 3329 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3330 | if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3331 | (!alloc_profile_is_valid(bctl->meta.target, 1) || |
| 3332 | (bctl->meta.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3333 | btrfs_err(fs_info, |
| 3334 | "unable to start balance with target metadata profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3335 | bctl->meta.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3336 | ret = -EINVAL; |
| 3337 | goto out; |
| 3338 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3339 | if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3340 | (!alloc_profile_is_valid(bctl->sys.target, 1) || |
| 3341 | (bctl->sys.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3342 | btrfs_err(fs_info, |
| 3343 | "unable to start balance with target system profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3344 | bctl->sys.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3345 | ret = -EINVAL; |
| 3346 | goto out; |
| 3347 | } |
| 3348 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3349 | /* allow dup'ed data chunks only in mixed mode */ |
| 3350 | if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3351 | (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3352 | btrfs_err(fs_info, "dup for data is not allowed"); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3353 | ret = -EINVAL; |
| 3354 | goto out; |
| 3355 | } |
| 3356 | |
| 3357 | /* allow to reduce meta or sys integrity only if force set */ |
| 3358 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3359 | BTRFS_BLOCK_GROUP_RAID10 | |
| 3360 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3361 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3362 | do { |
| 3363 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3364 | |
| 3365 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3366 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3367 | !(bctl->sys.target & allowed)) || |
| 3368 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3369 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3370 | !(bctl->meta.target & allowed))) { |
| 3371 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3372 | btrfs_info(fs_info, "force reducing metadata integrity"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3373 | } else { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3374 | btrfs_err(fs_info, "balance will reduce metadata " |
| 3375 | "integrity, use force if you want this"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3376 | ret = -EINVAL; |
| 3377 | goto out; |
| 3378 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3379 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3380 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3381 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3382 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3383 | int num_tolerated_disk_barrier_failures; |
| 3384 | u64 target = bctl->sys.target; |
| 3385 | |
| 3386 | num_tolerated_disk_barrier_failures = |
| 3387 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3388 | if (num_tolerated_disk_barrier_failures > 0 && |
| 3389 | (target & |
| 3390 | (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 | |
| 3391 | BTRFS_AVAIL_ALLOC_BIT_SINGLE))) |
| 3392 | num_tolerated_disk_barrier_failures = 0; |
| 3393 | else if (num_tolerated_disk_barrier_failures > 1 && |
| 3394 | (target & |
| 3395 | (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))) |
| 3396 | num_tolerated_disk_barrier_failures = 1; |
| 3397 | |
| 3398 | fs_info->num_tolerated_disk_barrier_failures = |
| 3399 | num_tolerated_disk_barrier_failures; |
| 3400 | } |
| 3401 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3402 | ret = insert_balance_item(fs_info->tree_root, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3403 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3404 | goto out; |
| 3405 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3406 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3407 | BUG_ON(ret == -EEXIST); |
| 3408 | set_balance_control(bctl); |
| 3409 | } else { |
| 3410 | BUG_ON(ret != -EEXIST); |
| 3411 | spin_lock(&fs_info->balance_lock); |
| 3412 | update_balance_args(bctl); |
| 3413 | spin_unlock(&fs_info->balance_lock); |
| 3414 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3415 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3416 | atomic_inc(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3417 | mutex_unlock(&fs_info->balance_mutex); |
| 3418 | |
| 3419 | ret = __btrfs_balance(fs_info); |
| 3420 | |
| 3421 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3422 | atomic_dec(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3423 | |
Ilya Dryomov | bf023ec | 2013-02-12 16:27:46 +0000 | [diff] [blame] | 3424 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3425 | fs_info->num_tolerated_disk_barrier_failures = |
| 3426 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3427 | } |
| 3428 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3429 | if (bargs) { |
| 3430 | memset(bargs, 0, sizeof(*bargs)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3431 | update_ioctl_balance_args(fs_info, 0, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3432 | } |
| 3433 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3434 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3435 | balance_need_close(fs_info)) { |
| 3436 | __cancel_balance(fs_info); |
| 3437 | } |
| 3438 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3439 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3440 | |
| 3441 | return ret; |
| 3442 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3443 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
| 3444 | __cancel_balance(fs_info); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3445 | else { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3446 | kfree(bctl); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3447 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 3448 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3449 | return ret; |
| 3450 | } |
| 3451 | |
| 3452 | static int balance_kthread(void *data) |
| 3453 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3454 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3455 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3456 | |
| 3457 | mutex_lock(&fs_info->volume_mutex); |
| 3458 | mutex_lock(&fs_info->balance_mutex); |
| 3459 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3460 | if (fs_info->balance_ctl) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3461 | btrfs_info(fs_info, "continuing balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3462 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3463 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3464 | |
| 3465 | mutex_unlock(&fs_info->balance_mutex); |
| 3466 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3467 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3468 | return ret; |
| 3469 | } |
| 3470 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3471 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 3472 | { |
| 3473 | struct task_struct *tsk; |
| 3474 | |
| 3475 | spin_lock(&fs_info->balance_lock); |
| 3476 | if (!fs_info->balance_ctl) { |
| 3477 | spin_unlock(&fs_info->balance_lock); |
| 3478 | return 0; |
| 3479 | } |
| 3480 | spin_unlock(&fs_info->balance_lock); |
| 3481 | |
| 3482 | if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3483 | btrfs_info(fs_info, "force skipping balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3484 | return 0; |
| 3485 | } |
| 3486 | |
| 3487 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 3488 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3489 | } |
| 3490 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3491 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3492 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3493 | struct btrfs_balance_control *bctl; |
| 3494 | struct btrfs_balance_item *item; |
| 3495 | struct btrfs_disk_balance_args disk_bargs; |
| 3496 | struct btrfs_path *path; |
| 3497 | struct extent_buffer *leaf; |
| 3498 | struct btrfs_key key; |
| 3499 | int ret; |
| 3500 | |
| 3501 | path = btrfs_alloc_path(); |
| 3502 | if (!path) |
| 3503 | return -ENOMEM; |
| 3504 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3505 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 3506 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 3507 | key.offset = 0; |
| 3508 | |
| 3509 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3510 | if (ret < 0) |
| 3511 | goto out; |
| 3512 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3513 | ret = 0; |
| 3514 | goto out; |
| 3515 | } |
| 3516 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3517 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3518 | if (!bctl) { |
| 3519 | ret = -ENOMEM; |
| 3520 | goto out; |
| 3521 | } |
| 3522 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3523 | leaf = path->nodes[0]; |
| 3524 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3525 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3526 | bctl->fs_info = fs_info; |
| 3527 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3528 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3529 | |
| 3530 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3531 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3532 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3533 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3534 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3535 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3536 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3537 | WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); |
| 3538 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3539 | mutex_lock(&fs_info->volume_mutex); |
| 3540 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3541 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3542 | set_balance_control(bctl); |
| 3543 | |
| 3544 | mutex_unlock(&fs_info->balance_mutex); |
| 3545 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3546 | out: |
| 3547 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3548 | return ret; |
| 3549 | } |
| 3550 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3551 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3552 | { |
| 3553 | int ret = 0; |
| 3554 | |
| 3555 | mutex_lock(&fs_info->balance_mutex); |
| 3556 | if (!fs_info->balance_ctl) { |
| 3557 | mutex_unlock(&fs_info->balance_mutex); |
| 3558 | return -ENOTCONN; |
| 3559 | } |
| 3560 | |
| 3561 | if (atomic_read(&fs_info->balance_running)) { |
| 3562 | atomic_inc(&fs_info->balance_pause_req); |
| 3563 | mutex_unlock(&fs_info->balance_mutex); |
| 3564 | |
| 3565 | wait_event(fs_info->balance_wait_q, |
| 3566 | atomic_read(&fs_info->balance_running) == 0); |
| 3567 | |
| 3568 | mutex_lock(&fs_info->balance_mutex); |
| 3569 | /* we are good with balance_ctl ripped off from under us */ |
| 3570 | BUG_ON(atomic_read(&fs_info->balance_running)); |
| 3571 | atomic_dec(&fs_info->balance_pause_req); |
| 3572 | } else { |
| 3573 | ret = -ENOTCONN; |
| 3574 | } |
| 3575 | |
| 3576 | mutex_unlock(&fs_info->balance_mutex); |
| 3577 | return ret; |
| 3578 | } |
| 3579 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3580 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 3581 | { |
Ilya Dryomov | e649e58 | 2013-10-10 20:40:21 +0300 | [diff] [blame] | 3582 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 3583 | return -EROFS; |
| 3584 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3585 | mutex_lock(&fs_info->balance_mutex); |
| 3586 | if (!fs_info->balance_ctl) { |
| 3587 | mutex_unlock(&fs_info->balance_mutex); |
| 3588 | return -ENOTCONN; |
| 3589 | } |
| 3590 | |
| 3591 | atomic_inc(&fs_info->balance_cancel_req); |
| 3592 | /* |
| 3593 | * if we are running just wait and return, balance item is |
| 3594 | * deleted in btrfs_balance in this case |
| 3595 | */ |
| 3596 | if (atomic_read(&fs_info->balance_running)) { |
| 3597 | mutex_unlock(&fs_info->balance_mutex); |
| 3598 | wait_event(fs_info->balance_wait_q, |
| 3599 | atomic_read(&fs_info->balance_running) == 0); |
| 3600 | mutex_lock(&fs_info->balance_mutex); |
| 3601 | } else { |
| 3602 | /* __cancel_balance needs volume_mutex */ |
| 3603 | mutex_unlock(&fs_info->balance_mutex); |
| 3604 | mutex_lock(&fs_info->volume_mutex); |
| 3605 | mutex_lock(&fs_info->balance_mutex); |
| 3606 | |
| 3607 | if (fs_info->balance_ctl) |
| 3608 | __cancel_balance(fs_info); |
| 3609 | |
| 3610 | mutex_unlock(&fs_info->volume_mutex); |
| 3611 | } |
| 3612 | |
| 3613 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); |
| 3614 | atomic_dec(&fs_info->balance_cancel_req); |
| 3615 | mutex_unlock(&fs_info->balance_mutex); |
| 3616 | return 0; |
| 3617 | } |
| 3618 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3619 | static int btrfs_uuid_scan_kthread(void *data) |
| 3620 | { |
| 3621 | struct btrfs_fs_info *fs_info = data; |
| 3622 | struct btrfs_root *root = fs_info->tree_root; |
| 3623 | struct btrfs_key key; |
| 3624 | struct btrfs_key max_key; |
| 3625 | struct btrfs_path *path = NULL; |
| 3626 | int ret = 0; |
| 3627 | struct extent_buffer *eb; |
| 3628 | int slot; |
| 3629 | struct btrfs_root_item root_item; |
| 3630 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3631 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3632 | |
| 3633 | path = btrfs_alloc_path(); |
| 3634 | if (!path) { |
| 3635 | ret = -ENOMEM; |
| 3636 | goto out; |
| 3637 | } |
| 3638 | |
| 3639 | key.objectid = 0; |
| 3640 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3641 | key.offset = 0; |
| 3642 | |
| 3643 | max_key.objectid = (u64)-1; |
| 3644 | max_key.type = BTRFS_ROOT_ITEM_KEY; |
| 3645 | max_key.offset = (u64)-1; |
| 3646 | |
| 3647 | path->keep_locks = 1; |
| 3648 | |
| 3649 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3650 | ret = btrfs_search_forward(root, &key, path, 0); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3651 | if (ret) { |
| 3652 | if (ret > 0) |
| 3653 | ret = 0; |
| 3654 | break; |
| 3655 | } |
| 3656 | |
| 3657 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 3658 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3659 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 3660 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 3661 | goto skip; |
| 3662 | |
| 3663 | eb = path->nodes[0]; |
| 3664 | slot = path->slots[0]; |
| 3665 | item_size = btrfs_item_size_nr(eb, slot); |
| 3666 | if (item_size < sizeof(root_item)) |
| 3667 | goto skip; |
| 3668 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3669 | read_extent_buffer(eb, &root_item, |
| 3670 | btrfs_item_ptr_offset(eb, slot), |
| 3671 | (int)sizeof(root_item)); |
| 3672 | if (btrfs_root_refs(&root_item) == 0) |
| 3673 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3674 | |
| 3675 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 3676 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 3677 | if (trans) |
| 3678 | goto update_tree; |
| 3679 | |
| 3680 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3681 | /* |
| 3682 | * 1 - subvol uuid item |
| 3683 | * 1 - received_subvol uuid item |
| 3684 | */ |
| 3685 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 3686 | if (IS_ERR(trans)) { |
| 3687 | ret = PTR_ERR(trans); |
| 3688 | break; |
| 3689 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3690 | continue; |
| 3691 | } else { |
| 3692 | goto skip; |
| 3693 | } |
| 3694 | update_tree: |
| 3695 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3696 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3697 | root_item.uuid, |
| 3698 | BTRFS_UUID_KEY_SUBVOL, |
| 3699 | key.objectid); |
| 3700 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3701 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3702 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3703 | break; |
| 3704 | } |
| 3705 | } |
| 3706 | |
| 3707 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3708 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3709 | root_item.received_uuid, |
| 3710 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 3711 | key.objectid); |
| 3712 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3713 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3714 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3715 | break; |
| 3716 | } |
| 3717 | } |
| 3718 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3719 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3720 | if (trans) { |
| 3721 | ret = btrfs_end_transaction(trans, fs_info->uuid_root); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3722 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3723 | if (ret) |
| 3724 | break; |
| 3725 | } |
| 3726 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3727 | btrfs_release_path(path); |
| 3728 | if (key.offset < (u64)-1) { |
| 3729 | key.offset++; |
| 3730 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 3731 | key.offset = 0; |
| 3732 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3733 | } else if (key.objectid < (u64)-1) { |
| 3734 | key.offset = 0; |
| 3735 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3736 | key.objectid++; |
| 3737 | } else { |
| 3738 | break; |
| 3739 | } |
| 3740 | cond_resched(); |
| 3741 | } |
| 3742 | |
| 3743 | out: |
| 3744 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3745 | if (trans && !IS_ERR(trans)) |
| 3746 | btrfs_end_transaction(trans, fs_info->uuid_root); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3747 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3748 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3749 | else |
| 3750 | fs_info->update_uuid_tree_gen = 1; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3751 | up(&fs_info->uuid_tree_rescan_sem); |
| 3752 | return 0; |
| 3753 | } |
| 3754 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3755 | /* |
| 3756 | * Callback for btrfs_uuid_tree_iterate(). |
| 3757 | * returns: |
| 3758 | * 0 check succeeded, the entry is not outdated. |
| 3759 | * < 0 if an error occured. |
| 3760 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 3761 | */ |
| 3762 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 3763 | u8 *uuid, u8 type, u64 subid) |
| 3764 | { |
| 3765 | struct btrfs_key key; |
| 3766 | int ret = 0; |
| 3767 | struct btrfs_root *subvol_root; |
| 3768 | |
| 3769 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 3770 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 3771 | goto out; |
| 3772 | |
| 3773 | key.objectid = subid; |
| 3774 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3775 | key.offset = (u64)-1; |
| 3776 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 3777 | if (IS_ERR(subvol_root)) { |
| 3778 | ret = PTR_ERR(subvol_root); |
| 3779 | if (ret == -ENOENT) |
| 3780 | ret = 1; |
| 3781 | goto out; |
| 3782 | } |
| 3783 | |
| 3784 | switch (type) { |
| 3785 | case BTRFS_UUID_KEY_SUBVOL: |
| 3786 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 3787 | ret = 1; |
| 3788 | break; |
| 3789 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 3790 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 3791 | BTRFS_UUID_SIZE)) |
| 3792 | ret = 1; |
| 3793 | break; |
| 3794 | } |
| 3795 | |
| 3796 | out: |
| 3797 | return ret; |
| 3798 | } |
| 3799 | |
| 3800 | static int btrfs_uuid_rescan_kthread(void *data) |
| 3801 | { |
| 3802 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 3803 | int ret; |
| 3804 | |
| 3805 | /* |
| 3806 | * 1st step is to iterate through the existing UUID tree and |
| 3807 | * to delete all entries that contain outdated data. |
| 3808 | * 2nd step is to add all missing entries to the UUID tree. |
| 3809 | */ |
| 3810 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 3811 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3812 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3813 | up(&fs_info->uuid_tree_rescan_sem); |
| 3814 | return ret; |
| 3815 | } |
| 3816 | return btrfs_uuid_scan_kthread(data); |
| 3817 | } |
| 3818 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3819 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 3820 | { |
| 3821 | struct btrfs_trans_handle *trans; |
| 3822 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 3823 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3824 | struct task_struct *task; |
| 3825 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3826 | |
| 3827 | /* |
| 3828 | * 1 - root node |
| 3829 | * 1 - root item |
| 3830 | */ |
| 3831 | trans = btrfs_start_transaction(tree_root, 2); |
| 3832 | if (IS_ERR(trans)) |
| 3833 | return PTR_ERR(trans); |
| 3834 | |
| 3835 | uuid_root = btrfs_create_tree(trans, fs_info, |
| 3836 | BTRFS_UUID_TREE_OBJECTID); |
| 3837 | if (IS_ERR(uuid_root)) { |
| 3838 | btrfs_abort_transaction(trans, tree_root, |
| 3839 | PTR_ERR(uuid_root)); |
| 3840 | return PTR_ERR(uuid_root); |
| 3841 | } |
| 3842 | |
| 3843 | fs_info->uuid_root = uuid_root; |
| 3844 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3845 | ret = btrfs_commit_transaction(trans, tree_root); |
| 3846 | if (ret) |
| 3847 | return ret; |
| 3848 | |
| 3849 | down(&fs_info->uuid_tree_rescan_sem); |
| 3850 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 3851 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3852 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3853 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3854 | up(&fs_info->uuid_tree_rescan_sem); |
| 3855 | return PTR_ERR(task); |
| 3856 | } |
| 3857 | |
| 3858 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3859 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3860 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3861 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 3862 | { |
| 3863 | struct task_struct *task; |
| 3864 | |
| 3865 | down(&fs_info->uuid_tree_rescan_sem); |
| 3866 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 3867 | if (IS_ERR(task)) { |
| 3868 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3869 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3870 | up(&fs_info->uuid_tree_rescan_sem); |
| 3871 | return PTR_ERR(task); |
| 3872 | } |
| 3873 | |
| 3874 | return 0; |
| 3875 | } |
| 3876 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3877 | /* |
| 3878 | * shrinking a device means finding all of the device extents past |
| 3879 | * the new size, and then following the back refs to the chunks. |
| 3880 | * The chunk relocation code actually frees the device extent |
| 3881 | */ |
| 3882 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 3883 | { |
| 3884 | struct btrfs_trans_handle *trans; |
| 3885 | struct btrfs_root *root = device->dev_root; |
| 3886 | struct btrfs_dev_extent *dev_extent = NULL; |
| 3887 | struct btrfs_path *path; |
| 3888 | u64 length; |
| 3889 | u64 chunk_tree; |
| 3890 | u64 chunk_objectid; |
| 3891 | u64 chunk_offset; |
| 3892 | int ret; |
| 3893 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3894 | int failed = 0; |
| 3895 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3896 | struct extent_buffer *l; |
| 3897 | struct btrfs_key key; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3898 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3899 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3900 | u64 old_size = device->total_bytes; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3901 | u64 diff = device->total_bytes - new_size; |
| 3902 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3903 | if (device->is_tgtdev_for_dev_replace) |
| 3904 | return -EINVAL; |
| 3905 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3906 | path = btrfs_alloc_path(); |
| 3907 | if (!path) |
| 3908 | return -ENOMEM; |
| 3909 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3910 | path->reada = 2; |
| 3911 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3912 | lock_chunks(root); |
| 3913 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3914 | device->total_bytes = new_size; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3915 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3916 | device->fs_devices->total_rw_bytes -= diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3917 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3918 | root->fs_info->free_chunk_space -= diff; |
| 3919 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 3920 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3921 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3922 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3923 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3924 | key.objectid = device->devid; |
| 3925 | key.offset = (u64)-1; |
| 3926 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 3927 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 3928 | do { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3929 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3930 | if (ret < 0) |
| 3931 | goto done; |
| 3932 | |
| 3933 | ret = btrfs_previous_item(root, path, 0, key.type); |
| 3934 | if (ret < 0) |
| 3935 | goto done; |
| 3936 | if (ret) { |
| 3937 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3938 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 3939 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3940 | } |
| 3941 | |
| 3942 | l = path->nodes[0]; |
| 3943 | slot = path->slots[0]; |
| 3944 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 3945 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3946 | if (key.objectid != device->devid) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3947 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 3948 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3949 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3950 | |
| 3951 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 3952 | length = btrfs_dev_extent_length(l, dev_extent); |
| 3953 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3954 | if (key.offset + length <= new_size) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3955 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3956 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3957 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3958 | |
| 3959 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); |
| 3960 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); |
| 3961 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3962 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3963 | |
| 3964 | ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid, |
| 3965 | chunk_offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3966 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3967 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3968 | if (ret == -ENOSPC) |
| 3969 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 3970 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3971 | |
| 3972 | if (failed && !retried) { |
| 3973 | failed = 0; |
| 3974 | retried = true; |
| 3975 | goto again; |
| 3976 | } else if (failed && retried) { |
| 3977 | ret = -ENOSPC; |
| 3978 | lock_chunks(root); |
| 3979 | |
| 3980 | device->total_bytes = old_size; |
| 3981 | if (device->writeable) |
| 3982 | device->fs_devices->total_rw_bytes += diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3983 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3984 | root->fs_info->free_chunk_space += diff; |
| 3985 | spin_unlock(&root->fs_info->free_chunk_lock); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3986 | unlock_chunks(root); |
| 3987 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3988 | } |
| 3989 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3990 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3991 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3992 | if (IS_ERR(trans)) { |
| 3993 | ret = PTR_ERR(trans); |
| 3994 | goto done; |
| 3995 | } |
| 3996 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3997 | lock_chunks(root); |
| 3998 | |
| 3999 | device->disk_total_bytes = new_size; |
| 4000 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4001 | ret = btrfs_update_device(trans, device); |
| 4002 | if (ret) { |
| 4003 | unlock_chunks(root); |
| 4004 | btrfs_end_transaction(trans, root); |
| 4005 | goto done; |
| 4006 | } |
| 4007 | WARN_ON(diff > old_total); |
| 4008 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
| 4009 | unlock_chunks(root); |
| 4010 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4011 | done: |
| 4012 | btrfs_free_path(path); |
| 4013 | return ret; |
| 4014 | } |
| 4015 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4016 | static int btrfs_add_system_chunk(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4017 | struct btrfs_key *key, |
| 4018 | struct btrfs_chunk *chunk, int item_size) |
| 4019 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4020 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4021 | struct btrfs_disk_key disk_key; |
| 4022 | u32 array_size; |
| 4023 | u8 *ptr; |
| 4024 | |
| 4025 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4026 | if (array_size + item_size + sizeof(disk_key) |
| 4027 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4028 | return -EFBIG; |
| 4029 | |
| 4030 | ptr = super_copy->sys_chunk_array + array_size; |
| 4031 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4032 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4033 | ptr += sizeof(disk_key); |
| 4034 | memcpy(ptr, chunk, item_size); |
| 4035 | item_size += sizeof(disk_key); |
| 4036 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
| 4037 | return 0; |
| 4038 | } |
| 4039 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4040 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4041 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4042 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4043 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4044 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4045 | const struct btrfs_device_info *di_a = a; |
| 4046 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4047 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4048 | if (di_a->max_avail > di_b->max_avail) |
| 4049 | return -1; |
| 4050 | if (di_a->max_avail < di_b->max_avail) |
| 4051 | return 1; |
| 4052 | if (di_a->total_avail > di_b->total_avail) |
| 4053 | return -1; |
| 4054 | if (di_a->total_avail < di_b->total_avail) |
| 4055 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4056 | return 0; |
| 4057 | } |
| 4058 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4059 | static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 4060 | [BTRFS_RAID_RAID10] = { |
| 4061 | .sub_stripes = 2, |
| 4062 | .dev_stripes = 1, |
| 4063 | .devs_max = 0, /* 0 == as many as possible */ |
| 4064 | .devs_min = 4, |
| 4065 | .devs_increment = 2, |
| 4066 | .ncopies = 2, |
| 4067 | }, |
| 4068 | [BTRFS_RAID_RAID1] = { |
| 4069 | .sub_stripes = 1, |
| 4070 | .dev_stripes = 1, |
| 4071 | .devs_max = 2, |
| 4072 | .devs_min = 2, |
| 4073 | .devs_increment = 2, |
| 4074 | .ncopies = 2, |
| 4075 | }, |
| 4076 | [BTRFS_RAID_DUP] = { |
| 4077 | .sub_stripes = 1, |
| 4078 | .dev_stripes = 2, |
| 4079 | .devs_max = 1, |
| 4080 | .devs_min = 1, |
| 4081 | .devs_increment = 1, |
| 4082 | .ncopies = 2, |
| 4083 | }, |
| 4084 | [BTRFS_RAID_RAID0] = { |
| 4085 | .sub_stripes = 1, |
| 4086 | .dev_stripes = 1, |
| 4087 | .devs_max = 0, |
| 4088 | .devs_min = 2, |
| 4089 | .devs_increment = 1, |
| 4090 | .ncopies = 1, |
| 4091 | }, |
| 4092 | [BTRFS_RAID_SINGLE] = { |
| 4093 | .sub_stripes = 1, |
| 4094 | .dev_stripes = 1, |
| 4095 | .devs_max = 1, |
| 4096 | .devs_min = 1, |
| 4097 | .devs_increment = 1, |
| 4098 | .ncopies = 1, |
| 4099 | }, |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 4100 | [BTRFS_RAID_RAID5] = { |
| 4101 | .sub_stripes = 1, |
| 4102 | .dev_stripes = 1, |
| 4103 | .devs_max = 0, |
| 4104 | .devs_min = 2, |
| 4105 | .devs_increment = 1, |
| 4106 | .ncopies = 2, |
| 4107 | }, |
| 4108 | [BTRFS_RAID_RAID6] = { |
| 4109 | .sub_stripes = 1, |
| 4110 | .dev_stripes = 1, |
| 4111 | .devs_max = 0, |
| 4112 | .devs_min = 3, |
| 4113 | .devs_increment = 1, |
| 4114 | .ncopies = 3, |
| 4115 | }, |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4116 | }; |
| 4117 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4118 | static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target) |
| 4119 | { |
| 4120 | /* TODO allow them to set a preferred stripe size */ |
| 4121 | return 64 * 1024; |
| 4122 | } |
| 4123 | |
| 4124 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4125 | { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4126 | if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))) |
| 4127 | return; |
| 4128 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4129 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4130 | } |
| 4131 | |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4132 | #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \ |
| 4133 | - sizeof(struct btrfs_item) \ |
| 4134 | - sizeof(struct btrfs_chunk)) \ |
| 4135 | / sizeof(struct btrfs_stripe) + 1) |
| 4136 | |
| 4137 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ |
| 4138 | - 2 * sizeof(struct btrfs_disk_key) \ |
| 4139 | - 2 * sizeof(struct btrfs_chunk)) \ |
| 4140 | / sizeof(struct btrfs_stripe) + 1) |
| 4141 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4142 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4143 | struct btrfs_root *extent_root, u64 start, |
| 4144 | u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4145 | { |
| 4146 | struct btrfs_fs_info *info = extent_root->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4147 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
| 4148 | struct list_head *cur; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4149 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4150 | struct extent_map_tree *em_tree; |
| 4151 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4152 | struct btrfs_device_info *devices_info = NULL; |
| 4153 | u64 total_avail; |
| 4154 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4155 | int data_stripes; /* number of stripes that count for |
| 4156 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4157 | int sub_stripes; /* sub_stripes info for map */ |
| 4158 | int dev_stripes; /* stripes per dev */ |
| 4159 | int devs_max; /* max devs to use */ |
| 4160 | int devs_min; /* min devs needed */ |
| 4161 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4162 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4163 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4164 | u64 max_stripe_size; |
| 4165 | u64 max_chunk_size; |
| 4166 | u64 stripe_size; |
| 4167 | u64 num_bytes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4168 | u64 raid_stripe_len = BTRFS_STRIPE_LEN; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4169 | int ndevs; |
| 4170 | int i; |
| 4171 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4172 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4173 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4174 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4175 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4176 | if (list_empty(&fs_devices->alloc_list)) |
| 4177 | return -ENOSPC; |
| 4178 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4179 | index = __get_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4180 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4181 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4182 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4183 | devs_max = btrfs_raid_array[index].devs_max; |
| 4184 | devs_min = btrfs_raid_array[index].devs_min; |
| 4185 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4186 | ncopies = btrfs_raid_array[index].ncopies; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4187 | |
| 4188 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 4189 | max_stripe_size = 1024 * 1024 * 1024; |
| 4190 | max_chunk_size = 10 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4191 | if (!devs_max) |
| 4192 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4193 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4194 | /* for larger filesystems, use larger metadata chunks */ |
| 4195 | if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024) |
| 4196 | max_stripe_size = 1024 * 1024 * 1024; |
| 4197 | else |
| 4198 | max_stripe_size = 256 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4199 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4200 | if (!devs_max) |
| 4201 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4202 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Chris Mason | 96bdc7d | 2012-01-16 08:13:11 -0500 | [diff] [blame] | 4203 | max_stripe_size = 32 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4204 | max_chunk_size = 2 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4205 | if (!devs_max) |
| 4206 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4207 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4208 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4209 | type); |
| 4210 | BUG_ON(1); |
| 4211 | } |
| 4212 | |
| 4213 | /* we don't want a chunk larger than 10% of writeable space */ |
| 4214 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4215 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4216 | |
| 4217 | devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices, |
| 4218 | GFP_NOFS); |
| 4219 | if (!devices_info) |
| 4220 | return -ENOMEM; |
| 4221 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4222 | cur = fs_devices->alloc_list.next; |
| 4223 | |
| 4224 | /* |
| 4225 | * in the first pass through the devices list, we gather information |
| 4226 | * about the available holes on each device. |
| 4227 | */ |
| 4228 | ndevs = 0; |
| 4229 | while (cur != &fs_devices->alloc_list) { |
| 4230 | struct btrfs_device *device; |
| 4231 | u64 max_avail; |
| 4232 | u64 dev_offset; |
| 4233 | |
| 4234 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
| 4235 | |
| 4236 | cur = cur->next; |
| 4237 | |
| 4238 | if (!device->writeable) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4239 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4240 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4241 | continue; |
| 4242 | } |
| 4243 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4244 | if (!device->in_fs_metadata || |
| 4245 | device->is_tgtdev_for_dev_replace) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4246 | continue; |
| 4247 | |
| 4248 | if (device->total_bytes > device->bytes_used) |
| 4249 | total_avail = device->total_bytes - device->bytes_used; |
| 4250 | else |
| 4251 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4252 | |
| 4253 | /* If there is no space on this device, skip it. */ |
| 4254 | if (total_avail == 0) |
| 4255 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4256 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4257 | ret = find_free_dev_extent(trans, device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4258 | max_stripe_size * dev_stripes, |
| 4259 | &dev_offset, &max_avail); |
| 4260 | if (ret && ret != -ENOSPC) |
| 4261 | goto error; |
| 4262 | |
| 4263 | if (ret == 0) |
| 4264 | max_avail = max_stripe_size * dev_stripes; |
| 4265 | |
| 4266 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
| 4267 | continue; |
| 4268 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4269 | if (ndevs == fs_devices->rw_devices) { |
| 4270 | WARN(1, "%s: found more than %llu devices\n", |
| 4271 | __func__, fs_devices->rw_devices); |
| 4272 | break; |
| 4273 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4274 | devices_info[ndevs].dev_offset = dev_offset; |
| 4275 | devices_info[ndevs].max_avail = max_avail; |
| 4276 | devices_info[ndevs].total_avail = total_avail; |
| 4277 | devices_info[ndevs].dev = device; |
| 4278 | ++ndevs; |
| 4279 | } |
| 4280 | |
| 4281 | /* |
| 4282 | * now sort the devices by hole size / available space |
| 4283 | */ |
| 4284 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4285 | btrfs_cmp_device_info, NULL); |
| 4286 | |
| 4287 | /* round down to number of usable stripes */ |
| 4288 | ndevs -= ndevs % devs_increment; |
| 4289 | |
| 4290 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
| 4291 | ret = -ENOSPC; |
| 4292 | goto error; |
| 4293 | } |
| 4294 | |
| 4295 | if (devs_max && ndevs > devs_max) |
| 4296 | ndevs = devs_max; |
| 4297 | /* |
| 4298 | * the primary goal is to maximize the number of stripes, so use as many |
| 4299 | * devices as possible, even if the stripes are not maximum sized. |
| 4300 | */ |
| 4301 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4302 | num_stripes = ndevs * dev_stripes; |
| 4303 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4304 | /* |
| 4305 | * this will have to be fixed for RAID1 and RAID10 over |
| 4306 | * more drives |
| 4307 | */ |
| 4308 | data_stripes = num_stripes / ncopies; |
| 4309 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4310 | if (type & BTRFS_BLOCK_GROUP_RAID5) { |
| 4311 | raid_stripe_len = find_raid56_stripe_len(ndevs - 1, |
| 4312 | btrfs_super_stripesize(info->super_copy)); |
| 4313 | data_stripes = num_stripes - 1; |
| 4314 | } |
| 4315 | if (type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4316 | raid_stripe_len = find_raid56_stripe_len(ndevs - 2, |
| 4317 | btrfs_super_stripesize(info->super_copy)); |
| 4318 | data_stripes = num_stripes - 2; |
| 4319 | } |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4320 | |
| 4321 | /* |
| 4322 | * Use the number of data stripes to figure out how big this chunk |
| 4323 | * is really going to be in terms of logical address space, |
| 4324 | * and compare that answer with the max chunk size |
| 4325 | */ |
| 4326 | if (stripe_size * data_stripes > max_chunk_size) { |
| 4327 | u64 mask = (1ULL << 24) - 1; |
| 4328 | stripe_size = max_chunk_size; |
| 4329 | do_div(stripe_size, data_stripes); |
| 4330 | |
| 4331 | /* bump the answer up to a 16MB boundary */ |
| 4332 | stripe_size = (stripe_size + mask) & ~mask; |
| 4333 | |
| 4334 | /* but don't go higher than the limits we found |
| 4335 | * while searching for free extents |
| 4336 | */ |
| 4337 | if (stripe_size > devices_info[ndevs-1].max_avail) |
| 4338 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4339 | } |
| 4340 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4341 | do_div(stripe_size, dev_stripes); |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 4342 | |
| 4343 | /* align to BTRFS_STRIPE_LEN */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4344 | do_div(stripe_size, raid_stripe_len); |
| 4345 | stripe_size *= raid_stripe_len; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4346 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4347 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 4348 | if (!map) { |
| 4349 | ret = -ENOMEM; |
| 4350 | goto error; |
| 4351 | } |
| 4352 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 4353 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4354 | for (i = 0; i < ndevs; ++i) { |
| 4355 | for (j = 0; j < dev_stripes; ++j) { |
| 4356 | int s = i * dev_stripes + j; |
| 4357 | map->stripes[s].dev = devices_info[i].dev; |
| 4358 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 4359 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 4360 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4361 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4362 | map->sector_size = extent_root->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4363 | map->stripe_len = raid_stripe_len; |
| 4364 | map->io_align = raid_stripe_len; |
| 4365 | map->io_width = raid_stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4366 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4367 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4368 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4369 | num_bytes = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4370 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4371 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4372 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4373 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4374 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4375 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4376 | ret = -ENOMEM; |
| 4377 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4378 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4379 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4380 | em->bdev = (struct block_device *)map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4381 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4382 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4383 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4384 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4385 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4386 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4387 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4388 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4389 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4390 | if (!ret) { |
| 4391 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
| 4392 | atomic_inc(&em->refs); |
| 4393 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4394 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4395 | if (ret) { |
| 4396 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 4397 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4398 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4399 | |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 4400 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 4401 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4402 | start, num_bytes); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4403 | if (ret) |
| 4404 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4405 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4406 | free_extent_map(em); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4407 | check_raid56_incompat_flag(extent_root->fs_info, type); |
| 4408 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4409 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4410 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4411 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4412 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4413 | write_lock(&em_tree->lock); |
| 4414 | remove_extent_mapping(em_tree, em); |
| 4415 | write_unlock(&em_tree->lock); |
| 4416 | |
| 4417 | /* One for our allocation */ |
| 4418 | free_extent_map(em); |
| 4419 | /* One for the tree reference */ |
| 4420 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4421 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4422 | kfree(devices_info); |
| 4423 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4424 | } |
| 4425 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4426 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4427 | struct btrfs_root *extent_root, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4428 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4429 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4430 | struct btrfs_key key; |
| 4431 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 4432 | struct btrfs_device *device; |
| 4433 | struct btrfs_chunk *chunk; |
| 4434 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4435 | struct extent_map_tree *em_tree; |
| 4436 | struct extent_map *em; |
| 4437 | struct map_lookup *map; |
| 4438 | size_t item_size; |
| 4439 | u64 dev_offset; |
| 4440 | u64 stripe_size; |
| 4441 | int i = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4442 | int ret; |
| 4443 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4444 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
| 4445 | read_lock(&em_tree->lock); |
| 4446 | em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size); |
| 4447 | read_unlock(&em_tree->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4448 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4449 | if (!em) { |
| 4450 | btrfs_crit(extent_root->fs_info, "unable to find logical " |
| 4451 | "%Lu len %Lu", chunk_offset, chunk_size); |
| 4452 | return -EINVAL; |
| 4453 | } |
| 4454 | |
| 4455 | if (em->start != chunk_offset || em->len != chunk_size) { |
| 4456 | btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted" |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4457 | " %Lu-%Lu, found %Lu-%Lu", chunk_offset, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4458 | chunk_size, em->start, em->len); |
| 4459 | free_extent_map(em); |
| 4460 | return -EINVAL; |
| 4461 | } |
| 4462 | |
| 4463 | map = (struct map_lookup *)em->bdev; |
| 4464 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 4465 | stripe_size = em->orig_block_len; |
| 4466 | |
| 4467 | chunk = kzalloc(item_size, GFP_NOFS); |
| 4468 | if (!chunk) { |
| 4469 | ret = -ENOMEM; |
| 4470 | goto out; |
| 4471 | } |
| 4472 | |
| 4473 | for (i = 0; i < map->num_stripes; i++) { |
| 4474 | device = map->stripes[i].dev; |
| 4475 | dev_offset = map->stripes[i].physical; |
| 4476 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4477 | device->bytes_used += stripe_size; |
| 4478 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 4479 | if (ret) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4480 | goto out; |
| 4481 | ret = btrfs_alloc_dev_extent(trans, device, |
| 4482 | chunk_root->root_key.objectid, |
| 4483 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4484 | chunk_offset, dev_offset, |
| 4485 | stripe_size); |
| 4486 | if (ret) |
| 4487 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4488 | } |
| 4489 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4490 | spin_lock(&extent_root->fs_info->free_chunk_lock); |
| 4491 | extent_root->fs_info->free_chunk_space -= (stripe_size * |
| 4492 | map->num_stripes); |
| 4493 | spin_unlock(&extent_root->fs_info->free_chunk_lock); |
| 4494 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4495 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4496 | for (i = 0; i < map->num_stripes; i++) { |
| 4497 | device = map->stripes[i].dev; |
| 4498 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4499 | |
| 4500 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 4501 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 4502 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 4503 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4504 | } |
| 4505 | |
| 4506 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 4507 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 4508 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 4509 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 4510 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 4511 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 4512 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 4513 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 4514 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 4515 | |
| 4516 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4517 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4518 | key.offset = chunk_offset; |
| 4519 | |
| 4520 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4521 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4522 | /* |
| 4523 | * TODO: Cleanup of inserted chunk root in case of |
| 4524 | * failure. |
| 4525 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4526 | ret = btrfs_add_system_chunk(chunk_root, &key, chunk, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4527 | item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4528 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4529 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4530 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4531 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4532 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4533 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | /* |
| 4537 | * Chunk allocation falls into two parts. The first part does works |
| 4538 | * that make the new allocated chunk useable, but not do any operation |
| 4539 | * that modifies the chunk tree. The second part does the works that |
| 4540 | * require modifying the chunk tree. This division is important for the |
| 4541 | * bootstrap process of adding storage to a seed btrfs. |
| 4542 | */ |
| 4543 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 4544 | struct btrfs_root *extent_root, u64 type) |
| 4545 | { |
| 4546 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4547 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4548 | chunk_offset = find_next_chunk(extent_root->fs_info); |
| 4549 | return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4550 | } |
| 4551 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4552 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4553 | struct btrfs_root *root, |
| 4554 | struct btrfs_device *device) |
| 4555 | { |
| 4556 | u64 chunk_offset; |
| 4557 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4558 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4559 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4560 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4561 | int ret; |
| 4562 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4563 | chunk_offset = find_next_chunk(fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4564 | alloc_profile = btrfs_get_alloc_profile(extent_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4565 | ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset, |
| 4566 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4567 | if (ret) |
| 4568 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4569 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4570 | sys_chunk_offset = find_next_chunk(root->fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4571 | alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4572 | ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset, |
| 4573 | alloc_profile); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4574 | if (ret) { |
| 4575 | btrfs_abort_transaction(trans, root, ret); |
| 4576 | goto out; |
| 4577 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4578 | |
| 4579 | ret = btrfs_add_device(trans, fs_info->chunk_root, device); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4580 | if (ret) |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4581 | btrfs_abort_transaction(trans, root, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4582 | out: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4583 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4584 | } |
| 4585 | |
| 4586 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 4587 | { |
| 4588 | struct extent_map *em; |
| 4589 | struct map_lookup *map; |
| 4590 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 4591 | int readonly = 0; |
| 4592 | int i; |
| 4593 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4594 | read_lock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4595 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4596 | read_unlock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4597 | if (!em) |
| 4598 | return 1; |
| 4599 | |
Josef Bacik | f48b907 | 2010-01-27 02:07:59 +0000 | [diff] [blame] | 4600 | if (btrfs_test_opt(root, DEGRADED)) { |
| 4601 | free_extent_map(em); |
| 4602 | return 0; |
| 4603 | } |
| 4604 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4605 | map = (struct map_lookup *)em->bdev; |
| 4606 | for (i = 0; i < map->num_stripes; i++) { |
| 4607 | if (!map->stripes[i].dev->writeable) { |
| 4608 | readonly = 1; |
| 4609 | break; |
| 4610 | } |
| 4611 | } |
| 4612 | free_extent_map(em); |
| 4613 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4614 | } |
| 4615 | |
| 4616 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 4617 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 4618 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4619 | } |
| 4620 | |
| 4621 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 4622 | { |
| 4623 | struct extent_map *em; |
| 4624 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4625 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4626 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4627 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 4628 | if (em) |
| 4629 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4630 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4631 | if (!em) |
| 4632 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4633 | /* once for us */ |
| 4634 | free_extent_map(em); |
| 4635 | /* once for the tree */ |
| 4636 | free_extent_map(em); |
| 4637 | } |
| 4638 | } |
| 4639 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4640 | 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] | 4641 | { |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4642 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4643 | struct extent_map *em; |
| 4644 | struct map_lookup *map; |
| 4645 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4646 | int ret; |
| 4647 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4648 | read_lock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4649 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4650 | read_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4651 | |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4652 | /* |
| 4653 | * We could return errors for these cases, but that could get ugly and |
| 4654 | * we'd probably do the same thing which is just not do anything else |
| 4655 | * and exit, so return 1 so the callers don't try to use other copies. |
| 4656 | */ |
| 4657 | if (!em) { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4658 | btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4659 | logical+len); |
| 4660 | return 1; |
| 4661 | } |
| 4662 | |
| 4663 | if (em->start > logical || em->start + em->len < logical) { |
David Sterba | ccf39f9 | 2013-07-11 15:41:11 +0200 | [diff] [blame] | 4664 | btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4665 | "%Lu-%Lu", logical, logical+len, em->start, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4666 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4667 | free_extent_map(em); |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4668 | return 1; |
| 4669 | } |
| 4670 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4671 | map = (struct map_lookup *)em->bdev; |
| 4672 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 4673 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4674 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4675 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4676 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 4677 | ret = 2; |
| 4678 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 4679 | ret = 3; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4680 | else |
| 4681 | ret = 1; |
| 4682 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4683 | |
| 4684 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 4685 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) |
| 4686 | ret++; |
| 4687 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
| 4688 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4689 | return ret; |
| 4690 | } |
| 4691 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4692 | unsigned long btrfs_full_stripe_len(struct btrfs_root *root, |
| 4693 | struct btrfs_mapping_tree *map_tree, |
| 4694 | u64 logical) |
| 4695 | { |
| 4696 | struct extent_map *em; |
| 4697 | struct map_lookup *map; |
| 4698 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4699 | unsigned long len = root->sectorsize; |
| 4700 | |
| 4701 | read_lock(&em_tree->lock); |
| 4702 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4703 | read_unlock(&em_tree->lock); |
| 4704 | BUG_ON(!em); |
| 4705 | |
| 4706 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4707 | map = (struct map_lookup *)em->bdev; |
| 4708 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 4709 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4710 | len = map->stripe_len * nr_data_stripes(map); |
| 4711 | } |
| 4712 | free_extent_map(em); |
| 4713 | return len; |
| 4714 | } |
| 4715 | |
| 4716 | int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree, |
| 4717 | u64 logical, u64 len, int mirror_num) |
| 4718 | { |
| 4719 | struct extent_map *em; |
| 4720 | struct map_lookup *map; |
| 4721 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4722 | int ret = 0; |
| 4723 | |
| 4724 | read_lock(&em_tree->lock); |
| 4725 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4726 | read_unlock(&em_tree->lock); |
| 4727 | BUG_ON(!em); |
| 4728 | |
| 4729 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4730 | map = (struct map_lookup *)em->bdev; |
| 4731 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 4732 | BTRFS_BLOCK_GROUP_RAID6)) |
| 4733 | ret = 1; |
| 4734 | free_extent_map(em); |
| 4735 | return ret; |
| 4736 | } |
| 4737 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4738 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
| 4739 | struct map_lookup *map, int first, int num, |
| 4740 | int optimal, int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4741 | { |
| 4742 | int i; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4743 | int tolerance; |
| 4744 | struct btrfs_device *srcdev; |
| 4745 | |
| 4746 | if (dev_replace_is_ongoing && |
| 4747 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 4748 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 4749 | srcdev = fs_info->dev_replace.srcdev; |
| 4750 | else |
| 4751 | srcdev = NULL; |
| 4752 | |
| 4753 | /* |
| 4754 | * try to avoid the drive that is the source drive for a |
| 4755 | * dev-replace procedure, only choose it if no other non-missing |
| 4756 | * mirror is available |
| 4757 | */ |
| 4758 | for (tolerance = 0; tolerance < 2; tolerance++) { |
| 4759 | if (map->stripes[optimal].dev->bdev && |
| 4760 | (tolerance || map->stripes[optimal].dev != srcdev)) |
| 4761 | return optimal; |
| 4762 | for (i = first; i < first + num; i++) { |
| 4763 | if (map->stripes[i].dev->bdev && |
| 4764 | (tolerance || map->stripes[i].dev != srcdev)) |
| 4765 | return i; |
| 4766 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4767 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4768 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4769 | /* we couldn't find one that doesn't fail. Just return something |
| 4770 | * and the io error handling code will clean up eventually |
| 4771 | */ |
| 4772 | return optimal; |
| 4773 | } |
| 4774 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4775 | static inline int parity_smaller(u64 a, u64 b) |
| 4776 | { |
| 4777 | return a > b; |
| 4778 | } |
| 4779 | |
| 4780 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
| 4781 | static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map) |
| 4782 | { |
| 4783 | struct btrfs_bio_stripe s; |
| 4784 | int i; |
| 4785 | u64 l; |
| 4786 | int again = 1; |
| 4787 | |
| 4788 | while (again) { |
| 4789 | again = 0; |
| 4790 | for (i = 0; i < bbio->num_stripes - 1; i++) { |
| 4791 | if (parity_smaller(raid_map[i], raid_map[i+1])) { |
| 4792 | s = bbio->stripes[i]; |
| 4793 | l = raid_map[i]; |
| 4794 | bbio->stripes[i] = bbio->stripes[i+1]; |
| 4795 | raid_map[i] = raid_map[i+1]; |
| 4796 | bbio->stripes[i+1] = s; |
| 4797 | raid_map[i+1] = l; |
| 4798 | again = 1; |
| 4799 | } |
| 4800 | } |
| 4801 | } |
| 4802 | } |
| 4803 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4804 | 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] | 4805 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4806 | struct btrfs_bio **bbio_ret, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4807 | int mirror_num, u64 **raid_map_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4808 | { |
| 4809 | struct extent_map *em; |
| 4810 | struct map_lookup *map; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4811 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4812 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4813 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4814 | u64 stripe_offset; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4815 | u64 stripe_end_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4816 | u64 stripe_nr; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4817 | u64 stripe_nr_orig; |
| 4818 | u64 stripe_nr_end; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4819 | u64 stripe_len; |
| 4820 | u64 *raid_map = NULL; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4821 | int stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4822 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4823 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4824 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 4825 | int max_errors = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4826 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4827 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 4828 | int dev_replace_is_ongoing = 0; |
| 4829 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4830 | int patch_the_first_stripe_for_dev_replace = 0; |
| 4831 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4832 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4833 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4834 | read_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4835 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4836 | read_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4837 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4838 | if (!em) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4839 | btrfs_crit(fs_info, "unable to find logical %llu len %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4840 | logical, *length); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4841 | return -EINVAL; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4842 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4843 | |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4844 | if (em->start > logical || em->start + em->len < logical) { |
| 4845 | btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4846 | "found %Lu-%Lu", logical, em->start, |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4847 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4848 | free_extent_map(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4849 | return -EINVAL; |
| 4850 | } |
| 4851 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4852 | map = (struct map_lookup *)em->bdev; |
| 4853 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4854 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4855 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4856 | stripe_nr = offset; |
| 4857 | /* |
| 4858 | * stripe_nr counts the total number of stripes we have to stride |
| 4859 | * to get to this block |
| 4860 | */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4861 | do_div(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4862 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4863 | stripe_offset = stripe_nr * stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4864 | BUG_ON(offset < stripe_offset); |
| 4865 | |
| 4866 | /* stripe_offset is the offset of this block in its stripe*/ |
| 4867 | stripe_offset = offset - stripe_offset; |
| 4868 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4869 | /* if we're here for raid56, we need to know the stripe aligned start */ |
| 4870 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4871 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 4872 | raid56_full_stripe_start = offset; |
| 4873 | |
| 4874 | /* allow a write of a full stripe, but make sure we don't |
| 4875 | * allow straddling of stripes |
| 4876 | */ |
| 4877 | do_div(raid56_full_stripe_start, full_stripe_len); |
| 4878 | raid56_full_stripe_start *= full_stripe_len; |
| 4879 | } |
| 4880 | |
| 4881 | if (rw & REQ_DISCARD) { |
| 4882 | /* we don't discard raid56 yet */ |
| 4883 | if (map->type & |
| 4884 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4885 | ret = -EOPNOTSUPP; |
| 4886 | goto out; |
| 4887 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4888 | *length = min_t(u64, em->len - offset, *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4889 | } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 4890 | u64 max_len; |
| 4891 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 4892 | For other RAID types and for RAID[56] reads, just allow a single |
| 4893 | stripe (on a single disk). */ |
| 4894 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) && |
| 4895 | (rw & REQ_WRITE)) { |
| 4896 | max_len = stripe_len * nr_data_stripes(map) - |
| 4897 | (offset - raid56_full_stripe_start); |
| 4898 | } else { |
| 4899 | /* we limit the length of each bio to what fits in a stripe */ |
| 4900 | max_len = stripe_len - stripe_offset; |
| 4901 | } |
| 4902 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4903 | } else { |
| 4904 | *length = em->len - offset; |
| 4905 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4906 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4907 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 4908 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4909 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4910 | goto out; |
| 4911 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4912 | btrfs_dev_replace_lock(dev_replace); |
| 4913 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 4914 | if (!dev_replace_is_ongoing) |
| 4915 | btrfs_dev_replace_unlock(dev_replace); |
| 4916 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4917 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
| 4918 | !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) && |
| 4919 | dev_replace->tgtdev != NULL) { |
| 4920 | /* |
| 4921 | * in dev-replace case, for repair case (that's the only |
| 4922 | * case where the mirror is selected explicitly when |
| 4923 | * calling btrfs_map_block), blocks left of the left cursor |
| 4924 | * can also be read from the target drive. |
| 4925 | * For REQ_GET_READ_MIRRORS, the target drive is added as |
| 4926 | * the last one to the array of stripes. For READ, it also |
| 4927 | * needs to be supported using the same mirror number. |
| 4928 | * If the requested block is not left of the left cursor, |
| 4929 | * EIO is returned. This can happen because btrfs_num_copies() |
| 4930 | * returns one more in the dev-replace case. |
| 4931 | */ |
| 4932 | u64 tmp_length = *length; |
| 4933 | struct btrfs_bio *tmp_bbio = NULL; |
| 4934 | int tmp_num_stripes; |
| 4935 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 4936 | int index_srcdev = 0; |
| 4937 | int found = 0; |
| 4938 | u64 physical_of_found = 0; |
| 4939 | |
| 4940 | ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4941 | logical, &tmp_length, &tmp_bbio, 0, NULL); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4942 | if (ret) { |
| 4943 | WARN_ON(tmp_bbio != NULL); |
| 4944 | goto out; |
| 4945 | } |
| 4946 | |
| 4947 | tmp_num_stripes = tmp_bbio->num_stripes; |
| 4948 | if (mirror_num > tmp_num_stripes) { |
| 4949 | /* |
| 4950 | * REQ_GET_READ_MIRRORS does not contain this |
| 4951 | * mirror, that means that the requested area |
| 4952 | * is not left of the left cursor |
| 4953 | */ |
| 4954 | ret = -EIO; |
| 4955 | kfree(tmp_bbio); |
| 4956 | goto out; |
| 4957 | } |
| 4958 | |
| 4959 | /* |
| 4960 | * process the rest of the function using the mirror_num |
| 4961 | * of the source drive. Therefore look it up first. |
| 4962 | * At the end, patch the device pointer to the one of the |
| 4963 | * target drive. |
| 4964 | */ |
| 4965 | for (i = 0; i < tmp_num_stripes; i++) { |
| 4966 | if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) { |
| 4967 | /* |
| 4968 | * In case of DUP, in order to keep it |
| 4969 | * simple, only add the mirror with the |
| 4970 | * lowest physical address |
| 4971 | */ |
| 4972 | if (found && |
| 4973 | physical_of_found <= |
| 4974 | tmp_bbio->stripes[i].physical) |
| 4975 | continue; |
| 4976 | index_srcdev = i; |
| 4977 | found = 1; |
| 4978 | physical_of_found = |
| 4979 | tmp_bbio->stripes[i].physical; |
| 4980 | } |
| 4981 | } |
| 4982 | |
| 4983 | if (found) { |
| 4984 | mirror_num = index_srcdev + 1; |
| 4985 | patch_the_first_stripe_for_dev_replace = 1; |
| 4986 | physical_to_patch_in_first_stripe = physical_of_found; |
| 4987 | } else { |
| 4988 | WARN_ON(1); |
| 4989 | ret = -EIO; |
| 4990 | kfree(tmp_bbio); |
| 4991 | goto out; |
| 4992 | } |
| 4993 | |
| 4994 | kfree(tmp_bbio); |
| 4995 | } else if (mirror_num > map->num_stripes) { |
| 4996 | mirror_num = 0; |
| 4997 | } |
| 4998 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4999 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5000 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5001 | stripe_nr_orig = stripe_nr; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 5002 | stripe_nr_end = ALIGN(offset + *length, map->stripe_len); |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5003 | do_div(stripe_nr_end, map->stripe_len); |
| 5004 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5005 | (offset + *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5006 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5007 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5008 | if (rw & REQ_DISCARD) |
| 5009 | num_stripes = min_t(u64, map->num_stripes, |
| 5010 | stripe_nr_end - stripe_nr_orig); |
| 5011 | stripe_index = do_div(stripe_nr, map->num_stripes); |
| 5012 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5013 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5014 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5015 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5016 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5017 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5018 | stripe_index = find_live_mirror(fs_info, map, 0, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5019 | map->num_stripes, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5020 | current->pid % map->num_stripes, |
| 5021 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5022 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5023 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5024 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5025 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5026 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5027 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5028 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5029 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5030 | } else { |
| 5031 | mirror_num = 1; |
| 5032 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5033 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5034 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5035 | int factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5036 | |
| 5037 | stripe_index = do_div(stripe_nr, factor); |
| 5038 | stripe_index *= map->sub_stripes; |
| 5039 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5040 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5041 | num_stripes = map->sub_stripes; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5042 | else if (rw & REQ_DISCARD) |
| 5043 | num_stripes = min_t(u64, map->sub_stripes * |
| 5044 | (stripe_nr_end - stripe_nr_orig), |
| 5045 | map->num_stripes); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5046 | else if (mirror_num) |
| 5047 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5048 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5049 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5050 | stripe_index = find_live_mirror(fs_info, map, |
| 5051 | stripe_index, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5052 | map->sub_stripes, stripe_index + |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5053 | current->pid % map->sub_stripes, |
| 5054 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5055 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5056 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5057 | |
| 5058 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 5059 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 5060 | u64 tmp; |
| 5061 | |
| 5062 | if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1) |
| 5063 | && raid_map_ret) { |
| 5064 | int i, rot; |
| 5065 | |
| 5066 | /* push stripe_nr back to the start of the full stripe */ |
| 5067 | stripe_nr = raid56_full_stripe_start; |
| 5068 | do_div(stripe_nr, stripe_len); |
| 5069 | |
| 5070 | stripe_index = do_div(stripe_nr, nr_data_stripes(map)); |
| 5071 | |
| 5072 | /* RAID[56] write or recovery. Return all stripes */ |
| 5073 | num_stripes = map->num_stripes; |
| 5074 | max_errors = nr_parity_stripes(map); |
| 5075 | |
Dulshani Gunawardhana | d9b0d9b | 2013-10-31 10:32:18 +0530 | [diff] [blame] | 5076 | raid_map = kmalloc_array(num_stripes, sizeof(u64), |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5077 | GFP_NOFS); |
| 5078 | if (!raid_map) { |
| 5079 | ret = -ENOMEM; |
| 5080 | goto out; |
| 5081 | } |
| 5082 | |
| 5083 | /* Work out the disk rotation on this stripe-set */ |
| 5084 | tmp = stripe_nr; |
| 5085 | rot = do_div(tmp, num_stripes); |
| 5086 | |
| 5087 | /* Fill in the logical address of each stripe */ |
| 5088 | tmp = stripe_nr * nr_data_stripes(map); |
| 5089 | for (i = 0; i < nr_data_stripes(map); i++) |
| 5090 | raid_map[(i+rot) % num_stripes] = |
| 5091 | em->start + (tmp + i) * map->stripe_len; |
| 5092 | |
| 5093 | raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 5094 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 5095 | raid_map[(i+rot+1) % num_stripes] = |
| 5096 | RAID6_Q_STRIPE; |
| 5097 | |
| 5098 | *length = map->stripe_len; |
| 5099 | stripe_index = 0; |
| 5100 | stripe_offset = 0; |
| 5101 | } else { |
| 5102 | /* |
| 5103 | * Mirror #0 or #1 means the original data block. |
| 5104 | * Mirror #2 is RAID5 parity block. |
| 5105 | * Mirror #3 is RAID6 Q block. |
| 5106 | */ |
| 5107 | stripe_index = do_div(stripe_nr, nr_data_stripes(map)); |
| 5108 | if (mirror_num > 1) |
| 5109 | stripe_index = nr_data_stripes(map) + |
| 5110 | mirror_num - 2; |
| 5111 | |
| 5112 | /* We distribute the parity blocks across stripes */ |
| 5113 | tmp = stripe_nr + stripe_index; |
| 5114 | stripe_index = do_div(tmp, map->num_stripes); |
| 5115 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5116 | } else { |
| 5117 | /* |
| 5118 | * after this do_div call, stripe_nr is the number of stripes |
| 5119 | * on this device we have to walk to find the data, and |
| 5120 | * stripe_index is the number of our device in the stripe array |
| 5121 | */ |
| 5122 | stripe_index = do_div(stripe_nr, map->num_stripes); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5123 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5124 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5125 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5126 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5127 | num_alloc_stripes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5128 | if (dev_replace_is_ongoing) { |
| 5129 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
| 5130 | num_alloc_stripes <<= 1; |
| 5131 | if (rw & REQ_GET_READ_MIRRORS) |
| 5132 | num_alloc_stripes++; |
| 5133 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5134 | bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5135 | if (!bbio) { |
Dave Jones | eb2067f | 2013-07-30 13:42:17 -0400 | [diff] [blame] | 5136 | kfree(raid_map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5137 | ret = -ENOMEM; |
| 5138 | goto out; |
| 5139 | } |
| 5140 | atomic_set(&bbio->error, 0); |
| 5141 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5142 | if (rw & REQ_DISCARD) { |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5143 | int factor = 0; |
| 5144 | int sub_stripes = 0; |
| 5145 | u64 stripes_per_dev = 0; |
| 5146 | u32 remaining_stripes = 0; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5147 | u32 last_stripe = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5148 | |
| 5149 | if (map->type & |
| 5150 | (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5151 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5152 | sub_stripes = 1; |
| 5153 | else |
| 5154 | sub_stripes = map->sub_stripes; |
| 5155 | |
| 5156 | factor = map->num_stripes / sub_stripes; |
| 5157 | stripes_per_dev = div_u64_rem(stripe_nr_end - |
| 5158 | stripe_nr_orig, |
| 5159 | factor, |
| 5160 | &remaining_stripes); |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5161 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5162 | last_stripe *= sub_stripes; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5163 | } |
| 5164 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5165 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5166 | bbio->stripes[i].physical = |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5167 | map->stripes[stripe_index].physical + |
| 5168 | stripe_offset + stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5169 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5170 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5171 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5172 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5173 | bbio->stripes[i].length = stripes_per_dev * |
| 5174 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5175 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5176 | if (i / sub_stripes < remaining_stripes) |
| 5177 | bbio->stripes[i].length += |
| 5178 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5179 | |
| 5180 | /* |
| 5181 | * Special for the first stripe and |
| 5182 | * the last stripe: |
| 5183 | * |
| 5184 | * |-------|...|-------| |
| 5185 | * |----------| |
| 5186 | * off end_off |
| 5187 | */ |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5188 | if (i < sub_stripes) |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5189 | bbio->stripes[i].length -= |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5190 | stripe_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5191 | |
| 5192 | if (stripe_index >= last_stripe && |
| 5193 | stripe_index <= (last_stripe + |
| 5194 | sub_stripes - 1)) |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5195 | bbio->stripes[i].length -= |
| 5196 | stripe_end_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5197 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5198 | if (i == sub_stripes - 1) |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5199 | stripe_offset = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5200 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5201 | bbio->stripes[i].length = *length; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5202 | |
| 5203 | stripe_index++; |
| 5204 | if (stripe_index == map->num_stripes) { |
| 5205 | /* This could only happen for RAID0/10 */ |
| 5206 | stripe_index = 0; |
| 5207 | stripe_nr++; |
| 5208 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5209 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5210 | } else { |
| 5211 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5212 | bbio->stripes[i].physical = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5213 | map->stripes[stripe_index].physical + |
| 5214 | stripe_offset + |
| 5215 | stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5216 | bbio->stripes[i].dev = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5217 | map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5218 | stripe_index++; |
| 5219 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5220 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5221 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5222 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) { |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5223 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 5224 | BTRFS_BLOCK_GROUP_RAID10 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5225 | BTRFS_BLOCK_GROUP_RAID5 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5226 | BTRFS_BLOCK_GROUP_DUP)) { |
| 5227 | max_errors = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5228 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 5229 | max_errors = 2; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5230 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5231 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5232 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5233 | if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) && |
| 5234 | dev_replace->tgtdev != NULL) { |
| 5235 | int index_where_to_add; |
| 5236 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5237 | |
| 5238 | /* |
| 5239 | * duplicate the write operations while the dev replace |
| 5240 | * procedure is running. Since the copying of the old disk |
| 5241 | * to the new disk takes place at run time while the |
| 5242 | * filesystem is mounted writable, the regular write |
| 5243 | * operations to the old disk have to be duplicated to go |
| 5244 | * to the new disk as well. |
| 5245 | * Note that device->missing is handled by the caller, and |
| 5246 | * that the write to the old disk is already set up in the |
| 5247 | * stripes array. |
| 5248 | */ |
| 5249 | index_where_to_add = num_stripes; |
| 5250 | for (i = 0; i < num_stripes; i++) { |
| 5251 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5252 | /* write to new disk, too */ |
| 5253 | struct btrfs_bio_stripe *new = |
| 5254 | bbio->stripes + index_where_to_add; |
| 5255 | struct btrfs_bio_stripe *old = |
| 5256 | bbio->stripes + i; |
| 5257 | |
| 5258 | new->physical = old->physical; |
| 5259 | new->length = old->length; |
| 5260 | new->dev = dev_replace->tgtdev; |
| 5261 | index_where_to_add++; |
| 5262 | max_errors++; |
| 5263 | } |
| 5264 | } |
| 5265 | num_stripes = index_where_to_add; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5266 | } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) && |
| 5267 | dev_replace->tgtdev != NULL) { |
| 5268 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5269 | int index_srcdev = 0; |
| 5270 | int found = 0; |
| 5271 | u64 physical_of_found = 0; |
| 5272 | |
| 5273 | /* |
| 5274 | * During the dev-replace procedure, the target drive can |
| 5275 | * also be used to read data in case it is needed to repair |
| 5276 | * a corrupt block elsewhere. This is possible if the |
| 5277 | * requested area is left of the left cursor. In this area, |
| 5278 | * the target drive is a full copy of the source drive. |
| 5279 | */ |
| 5280 | for (i = 0; i < num_stripes; i++) { |
| 5281 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5282 | /* |
| 5283 | * In case of DUP, in order to keep it |
| 5284 | * simple, only add the mirror with the |
| 5285 | * lowest physical address |
| 5286 | */ |
| 5287 | if (found && |
| 5288 | physical_of_found <= |
| 5289 | bbio->stripes[i].physical) |
| 5290 | continue; |
| 5291 | index_srcdev = i; |
| 5292 | found = 1; |
| 5293 | physical_of_found = bbio->stripes[i].physical; |
| 5294 | } |
| 5295 | } |
| 5296 | if (found) { |
| 5297 | u64 length = map->stripe_len; |
| 5298 | |
| 5299 | if (physical_of_found + length <= |
| 5300 | dev_replace->cursor_left) { |
| 5301 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5302 | bbio->stripes + num_stripes; |
| 5303 | |
| 5304 | tgtdev_stripe->physical = physical_of_found; |
| 5305 | tgtdev_stripe->length = |
| 5306 | bbio->stripes[index_srcdev].length; |
| 5307 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 5308 | |
| 5309 | num_stripes++; |
| 5310 | } |
| 5311 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5312 | } |
| 5313 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5314 | *bbio_ret = bbio; |
| 5315 | bbio->num_stripes = num_stripes; |
| 5316 | bbio->max_errors = max_errors; |
| 5317 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5318 | |
| 5319 | /* |
| 5320 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 5321 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 5322 | * available as a mirror |
| 5323 | */ |
| 5324 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 5325 | WARN_ON(num_stripes > 1); |
| 5326 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 5327 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 5328 | bbio->mirror_num = map->num_stripes + 1; |
| 5329 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5330 | if (raid_map) { |
| 5331 | sort_parity_stripes(bbio, raid_map); |
| 5332 | *raid_map_ret = raid_map; |
| 5333 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5334 | out: |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5335 | if (dev_replace_is_ongoing) |
| 5336 | btrfs_dev_replace_unlock(dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5337 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5338 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5339 | } |
| 5340 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5341 | int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5342 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5343 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5344 | { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5345 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5346 | mirror_num, NULL); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5347 | } |
| 5348 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5349 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 5350 | u64 chunk_start, u64 physical, u64 devid, |
| 5351 | u64 **logical, int *naddrs, int *stripe_len) |
| 5352 | { |
| 5353 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5354 | struct extent_map *em; |
| 5355 | struct map_lookup *map; |
| 5356 | u64 *buf; |
| 5357 | u64 bytenr; |
| 5358 | u64 length; |
| 5359 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5360 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5361 | int i, j, nr = 0; |
| 5362 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5363 | read_lock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5364 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5365 | read_unlock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5366 | |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5367 | if (!em) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5368 | printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5369 | chunk_start); |
| 5370 | return -EIO; |
| 5371 | } |
| 5372 | |
| 5373 | if (em->start != chunk_start) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5374 | printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5375 | em->start, chunk_start); |
| 5376 | free_extent_map(em); |
| 5377 | return -EIO; |
| 5378 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5379 | map = (struct map_lookup *)em->bdev; |
| 5380 | |
| 5381 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5382 | rmap_len = map->stripe_len; |
| 5383 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5384 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5385 | do_div(length, map->num_stripes / map->sub_stripes); |
| 5386 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5387 | do_div(length, map->num_stripes); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5388 | else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 5389 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 5390 | do_div(length, nr_data_stripes(map)); |
| 5391 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 5392 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5393 | |
| 5394 | buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5395 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5396 | |
| 5397 | for (i = 0; i < map->num_stripes; i++) { |
| 5398 | if (devid && map->stripes[i].dev->devid != devid) |
| 5399 | continue; |
| 5400 | if (map->stripes[i].physical > physical || |
| 5401 | map->stripes[i].physical + length <= physical) |
| 5402 | continue; |
| 5403 | |
| 5404 | stripe_nr = physical - map->stripes[i].physical; |
| 5405 | do_div(stripe_nr, map->stripe_len); |
| 5406 | |
| 5407 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5408 | stripe_nr = stripe_nr * map->num_stripes + i; |
| 5409 | do_div(stripe_nr, map->sub_stripes); |
| 5410 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5411 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5412 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 5413 | * Alternatively, just use rmap_len below instead of |
| 5414 | * map->stripe_len */ |
| 5415 | |
| 5416 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5417 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5418 | for (j = 0; j < nr; j++) { |
| 5419 | if (buf[j] == bytenr) |
| 5420 | break; |
| 5421 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5422 | if (j == nr) { |
| 5423 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5424 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5425 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5426 | } |
| 5427 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5428 | *logical = buf; |
| 5429 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5430 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5431 | |
| 5432 | free_extent_map(em); |
| 5433 | return 0; |
| 5434 | } |
| 5435 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5436 | static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err) |
| 5437 | { |
| 5438 | if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED)) |
| 5439 | bio_endio_nodec(bio, err); |
| 5440 | else |
| 5441 | bio_endio(bio, err); |
| 5442 | kfree(bbio); |
| 5443 | } |
| 5444 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5445 | static void btrfs_end_bio(struct bio *bio, int err) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5446 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5447 | struct btrfs_bio *bbio = bio->bi_private; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5448 | struct btrfs_device *dev = bbio->stripes[0].dev; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5449 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5450 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5451 | if (err) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5452 | atomic_inc(&bbio->error); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5453 | if (err == -EIO || err == -EREMOTEIO) { |
| 5454 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5455 | btrfs_io_bio(bio)->stripe_index; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5456 | |
| 5457 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 5458 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5459 | if (dev->bdev) { |
| 5460 | if (bio->bi_rw & WRITE) |
| 5461 | btrfs_dev_stat_inc(dev, |
| 5462 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 5463 | else |
| 5464 | btrfs_dev_stat_inc(dev, |
| 5465 | BTRFS_DEV_STAT_READ_ERRS); |
| 5466 | if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH) |
| 5467 | btrfs_dev_stat_inc(dev, |
| 5468 | BTRFS_DEV_STAT_FLUSH_ERRS); |
| 5469 | btrfs_dev_stat_print_on_error(dev); |
| 5470 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5471 | } |
| 5472 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5473 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5474 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5475 | is_orig_bio = 1; |
| 5476 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5477 | btrfs_bio_counter_dec(bbio->fs_info); |
| 5478 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5479 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5480 | if (!is_orig_bio) { |
| 5481 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5482 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5483 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 5484 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5485 | bio->bi_private = bbio->private; |
| 5486 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5487 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5488 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5489 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5490 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5491 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5492 | err = -EIO; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 5493 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5494 | /* |
| 5495 | * this bio is actually up to date, we didn't |
| 5496 | * go over the max number of errors |
| 5497 | */ |
| 5498 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5499 | err = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5500 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5501 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5502 | btrfs_end_bbio(bbio, bio, err); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5503 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5504 | bio_put(bio); |
| 5505 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5506 | } |
| 5507 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5508 | /* |
| 5509 | * see run_scheduled_bios for a description of why bios are collected for |
| 5510 | * async submit. |
| 5511 | * |
| 5512 | * This will add one bio to the pending list for a device and make sure |
| 5513 | * the work struct is scheduled. |
| 5514 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5515 | static noinline void btrfs_schedule_bio(struct btrfs_root *root, |
| 5516 | struct btrfs_device *device, |
| 5517 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5518 | { |
| 5519 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5520 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5521 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5522 | if (device->missing || !device->bdev) { |
| 5523 | bio_endio(bio, -EIO); |
| 5524 | return; |
| 5525 | } |
| 5526 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5527 | /* don't bother with additional async steps for reads, right now */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5528 | if (!(rw & REQ_WRITE)) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5529 | bio_get(bio); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 5530 | btrfsic_submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5531 | bio_put(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5532 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5533 | } |
| 5534 | |
| 5535 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5536 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5537 | * higher layers. Otherwise, the async bio makes it appear we have |
| 5538 | * made progress against dirty pages when we've really just put it |
| 5539 | * on a queue for later |
| 5540 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5541 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5542 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5543 | bio->bi_next = NULL; |
| 5544 | bio->bi_rw |= rw; |
| 5545 | |
| 5546 | spin_lock(&device->io_lock); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5547 | if (bio->bi_rw & REQ_SYNC) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5548 | pending_bios = &device->pending_sync_bios; |
| 5549 | else |
| 5550 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5551 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5552 | if (pending_bios->tail) |
| 5553 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5554 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5555 | pending_bios->tail = bio; |
| 5556 | if (!pending_bios->head) |
| 5557 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5558 | if (device->running_pending) |
| 5559 | should_queue = 0; |
| 5560 | |
| 5561 | spin_unlock(&device->io_lock); |
| 5562 | |
| 5563 | if (should_queue) |
Qu Wenruo | a8c93d4 | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 5564 | btrfs_queue_work(root->fs_info->submit_workers, |
| 5565 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5566 | } |
| 5567 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5568 | static int bio_size_ok(struct block_device *bdev, struct bio *bio, |
| 5569 | sector_t sector) |
| 5570 | { |
| 5571 | struct bio_vec *prev; |
| 5572 | struct request_queue *q = bdev_get_queue(bdev); |
Akinobu Mita | 475bf36 | 2013-11-18 22:13:18 +0900 | [diff] [blame] | 5573 | unsigned int max_sectors = queue_max_sectors(q); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5574 | struct bvec_merge_data bvm = { |
| 5575 | .bi_bdev = bdev, |
| 5576 | .bi_sector = sector, |
| 5577 | .bi_rw = bio->bi_rw, |
| 5578 | }; |
| 5579 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 5580 | if (WARN_ON(bio->bi_vcnt == 0)) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5581 | return 1; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5582 | |
| 5583 | prev = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 5584 | if (bio_sectors(bio) > max_sectors) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5585 | return 0; |
| 5586 | |
| 5587 | if (!q->merge_bvec_fn) |
| 5588 | return 1; |
| 5589 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5590 | bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5591 | if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) |
| 5592 | return 0; |
| 5593 | return 1; |
| 5594 | } |
| 5595 | |
| 5596 | static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5597 | struct bio *bio, u64 physical, int dev_nr, |
| 5598 | int rw, int async) |
| 5599 | { |
| 5600 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
| 5601 | |
| 5602 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5603 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5604 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5605 | bio->bi_iter.bi_sector = physical >> 9; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5606 | #ifdef DEBUG |
| 5607 | { |
| 5608 | struct rcu_string *name; |
| 5609 | |
| 5610 | rcu_read_lock(); |
| 5611 | name = rcu_dereference(dev->name); |
Masanari Iida | d142324 | 2012-10-31 15:16:32 +0000 | [diff] [blame] | 5612 | pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu " |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5613 | "(%s id %llu), size=%u\n", rw, |
| 5614 | (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev, |
| 5615 | name->str, dev->devid, bio->bi_size); |
| 5616 | rcu_read_unlock(); |
| 5617 | } |
| 5618 | #endif |
| 5619 | bio->bi_bdev = dev->bdev; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5620 | |
| 5621 | btrfs_bio_counter_inc_noblocked(root->fs_info); |
| 5622 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5623 | if (async) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5624 | btrfs_schedule_bio(root, dev, rw, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5625 | else |
| 5626 | btrfsic_submit_bio(rw, bio); |
| 5627 | } |
| 5628 | |
| 5629 | static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5630 | struct bio *first_bio, struct btrfs_device *dev, |
| 5631 | int dev_nr, int rw, int async) |
| 5632 | { |
| 5633 | struct bio_vec *bvec = first_bio->bi_io_vec; |
| 5634 | struct bio *bio; |
| 5635 | int nr_vecs = bio_get_nr_vecs(dev->bdev); |
| 5636 | u64 physical = bbio->stripes[dev_nr].physical; |
| 5637 | |
| 5638 | again: |
| 5639 | bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS); |
| 5640 | if (!bio) |
| 5641 | return -ENOMEM; |
| 5642 | |
| 5643 | while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) { |
| 5644 | if (bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 5645 | bvec->bv_offset) < bvec->bv_len) { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5646 | u64 len = bio->bi_iter.bi_size; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5647 | |
| 5648 | atomic_inc(&bbio->stripes_pending); |
| 5649 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, |
| 5650 | rw, async); |
| 5651 | physical += len; |
| 5652 | goto again; |
| 5653 | } |
| 5654 | bvec++; |
| 5655 | } |
| 5656 | |
| 5657 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async); |
| 5658 | return 0; |
| 5659 | } |
| 5660 | |
| 5661 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 5662 | { |
| 5663 | atomic_inc(&bbio->error); |
| 5664 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5665 | /* Shoud be the original bio. */ |
| 5666 | WARN_ON(bio != bbio->orig_bio); |
| 5667 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5668 | bio->bi_private = bbio->private; |
| 5669 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5670 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5671 | bio->bi_iter.bi_sector = logical >> 9; |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5672 | |
| 5673 | btrfs_end_bbio(bbio, bio, -EIO); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5674 | } |
| 5675 | } |
| 5676 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5677 | 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] | 5678 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5679 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5680 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5681 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5682 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5683 | u64 length = 0; |
| 5684 | u64 map_length; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5685 | u64 *raid_map = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5686 | int ret; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5687 | int dev_nr = 0; |
| 5688 | int total_devs = 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5689 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5690 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5691 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5692 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5693 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5694 | btrfs_bio_counter_inc_blocked(root->fs_info); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5695 | ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio, |
| 5696 | mirror_num, &raid_map); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5697 | if (ret) { |
| 5698 | btrfs_bio_counter_dec(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5699 | return ret; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5700 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5701 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5702 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5703 | bbio->orig_bio = first_bio; |
| 5704 | bbio->private = first_bio->bi_private; |
| 5705 | bbio->end_io = first_bio->bi_end_io; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5706 | bbio->fs_info = root->fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5707 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 5708 | |
| 5709 | if (raid_map) { |
| 5710 | /* In this case, map_length has been set to the length of |
| 5711 | a single stripe; not the whole write */ |
| 5712 | if (rw & WRITE) { |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5713 | ret = raid56_parity_write(root, bio, bbio, |
| 5714 | raid_map, map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5715 | } else { |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5716 | ret = raid56_parity_recover(root, bio, bbio, |
| 5717 | raid_map, map_length, |
| 5718 | mirror_num); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5719 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5720 | /* |
| 5721 | * FIXME, replace dosen't support raid56 yet, please fix |
| 5722 | * it in the future. |
| 5723 | */ |
| 5724 | btrfs_bio_counter_dec(root->fs_info); |
| 5725 | return ret; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5726 | } |
| 5727 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5728 | if (map_length < length) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5729 | 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] | 5730 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5731 | BUG(); |
| 5732 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5733 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5734 | while (dev_nr < total_devs) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5735 | dev = bbio->stripes[dev_nr].dev; |
| 5736 | if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) { |
| 5737 | bbio_error(bbio, first_bio, logical); |
| 5738 | dev_nr++; |
| 5739 | continue; |
| 5740 | } |
| 5741 | |
| 5742 | /* |
| 5743 | * Check and see if we're ok with this bio based on it's size |
| 5744 | * and offset with the given device. |
| 5745 | */ |
| 5746 | if (!bio_size_ok(dev->bdev, first_bio, |
| 5747 | bbio->stripes[dev_nr].physical >> 9)) { |
| 5748 | ret = breakup_stripe_bio(root, bbio, first_bio, dev, |
| 5749 | dev_nr, rw, async_submit); |
| 5750 | BUG_ON(ret); |
| 5751 | dev_nr++; |
| 5752 | continue; |
| 5753 | } |
| 5754 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5755 | if (dev_nr < total_devs - 1) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5756 | bio = btrfs_bio_clone(first_bio, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5757 | BUG_ON(!bio); /* -ENOMEM */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5758 | } else { |
| 5759 | bio = first_bio; |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5760 | bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5761 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5762 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5763 | submit_stripe_bio(root, bbio, bio, |
| 5764 | bbio->stripes[dev_nr].physical, dev_nr, rw, |
| 5765 | async_submit); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5766 | dev_nr++; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5767 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5768 | btrfs_bio_counter_dec(root->fs_info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5769 | return 0; |
| 5770 | } |
| 5771 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5772 | 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] | 5773 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5774 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5775 | struct btrfs_device *device; |
| 5776 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5777 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5778 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5779 | while (cur_devices) { |
| 5780 | if (!fsid || |
| 5781 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 5782 | device = __find_device(&cur_devices->devices, |
| 5783 | devid, uuid); |
| 5784 | if (device) |
| 5785 | return device; |
| 5786 | } |
| 5787 | cur_devices = cur_devices->seed; |
| 5788 | } |
| 5789 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5790 | } |
| 5791 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5792 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
| 5793 | u64 devid, u8 *dev_uuid) |
| 5794 | { |
| 5795 | struct btrfs_device *device; |
| 5796 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 5797 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5798 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 5799 | if (IS_ERR(device)) |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 5800 | return NULL; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5801 | |
| 5802 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5803 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5804 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5805 | |
| 5806 | device->missing = 1; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 5807 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5808 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5809 | return device; |
| 5810 | } |
| 5811 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5812 | /** |
| 5813 | * btrfs_alloc_device - allocate struct btrfs_device |
| 5814 | * @fs_info: used only for generating a new devid, can be NULL if |
| 5815 | * devid is provided (i.e. @devid != NULL). |
| 5816 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 5817 | * is generated. |
| 5818 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 5819 | * is generated. |
| 5820 | * |
| 5821 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() |
| 5822 | * on error. Returned struct is not linked onto any lists and can be |
| 5823 | * destroyed with kfree() right away. |
| 5824 | */ |
| 5825 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 5826 | const u64 *devid, |
| 5827 | const u8 *uuid) |
| 5828 | { |
| 5829 | struct btrfs_device *dev; |
| 5830 | u64 tmp; |
| 5831 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 5832 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5833 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5834 | |
| 5835 | dev = __alloc_device(); |
| 5836 | if (IS_ERR(dev)) |
| 5837 | return dev; |
| 5838 | |
| 5839 | if (devid) |
| 5840 | tmp = *devid; |
| 5841 | else { |
| 5842 | int ret; |
| 5843 | |
| 5844 | ret = find_next_devid(fs_info, &tmp); |
| 5845 | if (ret) { |
| 5846 | kfree(dev); |
| 5847 | return ERR_PTR(ret); |
| 5848 | } |
| 5849 | } |
| 5850 | dev->devid = tmp; |
| 5851 | |
| 5852 | if (uuid) |
| 5853 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 5854 | else |
| 5855 | generate_random_uuid(dev->uuid); |
| 5856 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 5857 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
| 5858 | pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5859 | |
| 5860 | return dev; |
| 5861 | } |
| 5862 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5863 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 5864 | struct extent_buffer *leaf, |
| 5865 | struct btrfs_chunk *chunk) |
| 5866 | { |
| 5867 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 5868 | struct map_lookup *map; |
| 5869 | struct extent_map *em; |
| 5870 | u64 logical; |
| 5871 | u64 length; |
| 5872 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5873 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5874 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5875 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5876 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5877 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5878 | logical = key->offset; |
| 5879 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5880 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5881 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5882 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5883 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5884 | |
| 5885 | /* already mapped? */ |
| 5886 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 5887 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5888 | return 0; |
| 5889 | } else if (em) { |
| 5890 | free_extent_map(em); |
| 5891 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5892 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5893 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5894 | if (!em) |
| 5895 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5896 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 5897 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5898 | if (!map) { |
| 5899 | free_extent_map(em); |
| 5900 | return -ENOMEM; |
| 5901 | } |
| 5902 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 5903 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5904 | em->bdev = (struct block_device *)map; |
| 5905 | em->start = logical; |
| 5906 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 5907 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5908 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5909 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5910 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5911 | map->num_stripes = num_stripes; |
| 5912 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 5913 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 5914 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 5915 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 5916 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5917 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5918 | for (i = 0; i < num_stripes; i++) { |
| 5919 | map->stripes[i].physical = |
| 5920 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 5921 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5922 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 5923 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 5924 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5925 | map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, |
| 5926 | uuid, NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5927 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5928 | free_extent_map(em); |
| 5929 | return -EIO; |
| 5930 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5931 | if (!map->stripes[i].dev) { |
| 5932 | map->stripes[i].dev = |
| 5933 | add_missing_dev(root, devid, uuid); |
| 5934 | if (!map->stripes[i].dev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5935 | free_extent_map(em); |
| 5936 | return -EIO; |
| 5937 | } |
| 5938 | } |
| 5939 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5940 | } |
| 5941 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5942 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5943 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5944 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5945 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5946 | free_extent_map(em); |
| 5947 | |
| 5948 | return 0; |
| 5949 | } |
| 5950 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5951 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5952 | struct btrfs_dev_item *dev_item, |
| 5953 | struct btrfs_device *device) |
| 5954 | { |
| 5955 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5956 | |
| 5957 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 5958 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 5959 | device->total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5960 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
| 5961 | device->type = btrfs_device_type(leaf, dev_item); |
| 5962 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 5963 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 5964 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 5965 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 5966 | device->is_tgtdev_for_dev_replace = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5967 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 5968 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5969 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5970 | } |
| 5971 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5972 | static int open_seed_devices(struct btrfs_root *root, u8 *fsid) |
| 5973 | { |
| 5974 | struct btrfs_fs_devices *fs_devices; |
| 5975 | int ret; |
| 5976 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 5977 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5978 | |
| 5979 | fs_devices = root->fs_info->fs_devices->seed; |
| 5980 | while (fs_devices) { |
| 5981 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 5982 | ret = 0; |
| 5983 | goto out; |
| 5984 | } |
| 5985 | fs_devices = fs_devices->seed; |
| 5986 | } |
| 5987 | |
| 5988 | fs_devices = find_fsid(fsid); |
| 5989 | if (!fs_devices) { |
| 5990 | ret = -ENOENT; |
| 5991 | goto out; |
| 5992 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5993 | |
| 5994 | fs_devices = clone_fs_devices(fs_devices); |
| 5995 | if (IS_ERR(fs_devices)) { |
| 5996 | ret = PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5997 | goto out; |
| 5998 | } |
| 5999 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 6000 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 6001 | root->fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6002 | if (ret) { |
| 6003 | free_fs_devices(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6004 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6005 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6006 | |
| 6007 | if (!fs_devices->seeding) { |
| 6008 | __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6009 | free_fs_devices(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6010 | ret = -EINVAL; |
| 6011 | goto out; |
| 6012 | } |
| 6013 | |
| 6014 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 6015 | root->fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6016 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6017 | return ret; |
| 6018 | } |
| 6019 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6020 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6021 | struct extent_buffer *leaf, |
| 6022 | struct btrfs_dev_item *dev_item) |
| 6023 | { |
| 6024 | struct btrfs_device *device; |
| 6025 | u64 devid; |
| 6026 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6027 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6028 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6029 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6030 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6031 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6032 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6033 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6034 | BTRFS_UUID_SIZE); |
| 6035 | |
| 6036 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
| 6037 | ret = open_seed_devices(root, fs_uuid); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6038 | if (ret && !btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6039 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6040 | } |
| 6041 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6042 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6043 | if (!device || !device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6044 | if (!btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6045 | return -EIO; |
| 6046 | |
| 6047 | if (!device) { |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6048 | btrfs_warn(root->fs_info, "devid %llu missing", devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6049 | device = add_missing_dev(root, devid, dev_uuid); |
| 6050 | if (!device) |
| 6051 | return -ENOMEM; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6052 | } else if (!device->missing) { |
| 6053 | /* |
| 6054 | * this happens when a device that was properly setup |
| 6055 | * in the device info lists suddenly goes bad. |
| 6056 | * device->bdev is NULL, and so we have to set |
| 6057 | * device->missing to one here |
| 6058 | */ |
| 6059 | root->fs_info->fs_devices->missing_devices++; |
| 6060 | device->missing = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6061 | } |
| 6062 | } |
| 6063 | |
| 6064 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 6065 | BUG_ON(device->writeable); |
| 6066 | if (device->generation != |
| 6067 | btrfs_device_generation(leaf, dev_item)) |
| 6068 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6069 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6070 | |
| 6071 | fill_device_from_item(leaf, dev_item, device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6072 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6073 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6074 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6075 | spin_lock(&root->fs_info->free_chunk_lock); |
| 6076 | root->fs_info->free_chunk_space += device->total_bytes - |
| 6077 | device->bytes_used; |
| 6078 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 6079 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6080 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6081 | return ret; |
| 6082 | } |
| 6083 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6084 | int btrfs_read_sys_array(struct btrfs_root *root) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6085 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6086 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6087 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6088 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6089 | struct btrfs_chunk *chunk; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6090 | u8 *ptr; |
| 6091 | unsigned long sb_ptr; |
| 6092 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6093 | u32 num_stripes; |
| 6094 | u32 array_size; |
| 6095 | u32 len = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6096 | u32 cur; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6097 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6098 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6099 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET, |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6100 | BTRFS_SUPER_INFO_SIZE); |
| 6101 | if (!sb) |
| 6102 | return -ENOMEM; |
| 6103 | btrfs_set_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6104 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6105 | /* |
| 6106 | * The sb extent buffer is artifical and just used to read the system array. |
| 6107 | * btrfs_set_buffer_uptodate() call does not properly mark all it's |
| 6108 | * pages up-to-date when the page is larger: extent does not cover the |
| 6109 | * whole page and consequently check_page_uptodate does not find all |
| 6110 | * the page's extents up-to-date (the hole beyond sb), |
| 6111 | * write_extent_buffer then triggers a WARN_ON. |
| 6112 | * |
| 6113 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6114 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6115 | * to silence the warning eg. on PowerPC 64. |
| 6116 | */ |
| 6117 | if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6118 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6119 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6120 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6121 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6122 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6123 | ptr = super_copy->sys_chunk_array; |
| 6124 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6125 | cur = 0; |
| 6126 | |
| 6127 | while (cur < array_size) { |
| 6128 | disk_key = (struct btrfs_disk_key *)ptr; |
| 6129 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6130 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6131 | len = sizeof(*disk_key); ptr += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6132 | sb_ptr += len; |
| 6133 | cur += len; |
| 6134 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6135 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6136 | chunk = (struct btrfs_chunk *)sb_ptr; |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6137 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6138 | if (ret) |
| 6139 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6140 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 6141 | len = btrfs_chunk_item_size(num_stripes); |
| 6142 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6143 | ret = -EIO; |
| 6144 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6145 | } |
| 6146 | ptr += len; |
| 6147 | sb_ptr += len; |
| 6148 | cur += len; |
| 6149 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6150 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6151 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6152 | } |
| 6153 | |
| 6154 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 6155 | { |
| 6156 | struct btrfs_path *path; |
| 6157 | struct extent_buffer *leaf; |
| 6158 | struct btrfs_key key; |
| 6159 | struct btrfs_key found_key; |
| 6160 | int ret; |
| 6161 | int slot; |
| 6162 | |
| 6163 | root = root->fs_info->chunk_root; |
| 6164 | |
| 6165 | path = btrfs_alloc_path(); |
| 6166 | if (!path) |
| 6167 | return -ENOMEM; |
| 6168 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6169 | mutex_lock(&uuid_mutex); |
| 6170 | lock_chunks(root); |
| 6171 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6172 | /* |
| 6173 | * Read all device items, and then all the chunk items. All |
| 6174 | * device items are found before any chunk item (their object id |
| 6175 | * is smaller than the lowest possible object id for a chunk |
| 6176 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6177 | */ |
| 6178 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 6179 | key.offset = 0; |
| 6180 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6181 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 6182 | if (ret < 0) |
| 6183 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6184 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6185 | leaf = path->nodes[0]; |
| 6186 | slot = path->slots[0]; |
| 6187 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6188 | ret = btrfs_next_leaf(root, path); |
| 6189 | if (ret == 0) |
| 6190 | continue; |
| 6191 | if (ret < 0) |
| 6192 | goto error; |
| 6193 | break; |
| 6194 | } |
| 6195 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6196 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 6197 | struct btrfs_dev_item *dev_item; |
| 6198 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6199 | struct btrfs_dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6200 | ret = read_one_dev(root, leaf, dev_item); |
| 6201 | if (ret) |
| 6202 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6203 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 6204 | struct btrfs_chunk *chunk; |
| 6205 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 6206 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6207 | if (ret) |
| 6208 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6209 | } |
| 6210 | path->slots[0]++; |
| 6211 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6212 | ret = 0; |
| 6213 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6214 | unlock_chunks(root); |
| 6215 | mutex_unlock(&uuid_mutex); |
| 6216 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6217 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6218 | return ret; |
| 6219 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6220 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6221 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 6222 | { |
| 6223 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6224 | struct btrfs_device *device; |
| 6225 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 6226 | while (fs_devices) { |
| 6227 | mutex_lock(&fs_devices->device_list_mutex); |
| 6228 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
| 6229 | device->dev_root = fs_info->dev_root; |
| 6230 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6231 | |
| 6232 | fs_devices = fs_devices->seed; |
| 6233 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6234 | } |
| 6235 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6236 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 6237 | { |
| 6238 | int i; |
| 6239 | |
| 6240 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6241 | btrfs_dev_stat_reset(dev, i); |
| 6242 | } |
| 6243 | |
| 6244 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 6245 | { |
| 6246 | struct btrfs_key key; |
| 6247 | struct btrfs_key found_key; |
| 6248 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6249 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6250 | struct extent_buffer *eb; |
| 6251 | int slot; |
| 6252 | int ret = 0; |
| 6253 | struct btrfs_device *device; |
| 6254 | struct btrfs_path *path = NULL; |
| 6255 | int i; |
| 6256 | |
| 6257 | path = btrfs_alloc_path(); |
| 6258 | if (!path) { |
| 6259 | ret = -ENOMEM; |
| 6260 | goto out; |
| 6261 | } |
| 6262 | |
| 6263 | mutex_lock(&fs_devices->device_list_mutex); |
| 6264 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 6265 | int item_size; |
| 6266 | struct btrfs_dev_stats_item *ptr; |
| 6267 | |
| 6268 | key.objectid = 0; |
| 6269 | key.type = BTRFS_DEV_STATS_KEY; |
| 6270 | key.offset = device->devid; |
| 6271 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 6272 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6273 | __btrfs_reset_dev_stats(device); |
| 6274 | device->dev_stats_valid = 1; |
| 6275 | btrfs_release_path(path); |
| 6276 | continue; |
| 6277 | } |
| 6278 | slot = path->slots[0]; |
| 6279 | eb = path->nodes[0]; |
| 6280 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 6281 | item_size = btrfs_item_size_nr(eb, slot); |
| 6282 | |
| 6283 | ptr = btrfs_item_ptr(eb, slot, |
| 6284 | struct btrfs_dev_stats_item); |
| 6285 | |
| 6286 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6287 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 6288 | btrfs_dev_stat_set(device, i, |
| 6289 | btrfs_dev_stats_value(eb, ptr, i)); |
| 6290 | else |
| 6291 | btrfs_dev_stat_reset(device, i); |
| 6292 | } |
| 6293 | |
| 6294 | device->dev_stats_valid = 1; |
| 6295 | btrfs_dev_stat_print_on_load(device); |
| 6296 | btrfs_release_path(path); |
| 6297 | } |
| 6298 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6299 | |
| 6300 | out: |
| 6301 | btrfs_free_path(path); |
| 6302 | return ret < 0 ? ret : 0; |
| 6303 | } |
| 6304 | |
| 6305 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
| 6306 | struct btrfs_root *dev_root, |
| 6307 | struct btrfs_device *device) |
| 6308 | { |
| 6309 | struct btrfs_path *path; |
| 6310 | struct btrfs_key key; |
| 6311 | struct extent_buffer *eb; |
| 6312 | struct btrfs_dev_stats_item *ptr; |
| 6313 | int ret; |
| 6314 | int i; |
| 6315 | |
| 6316 | key.objectid = 0; |
| 6317 | key.type = BTRFS_DEV_STATS_KEY; |
| 6318 | key.offset = device->devid; |
| 6319 | |
| 6320 | path = btrfs_alloc_path(); |
| 6321 | BUG_ON(!path); |
| 6322 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 6323 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6324 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6325 | "error %d while searching for dev_stats item for device %s!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6326 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6327 | goto out; |
| 6328 | } |
| 6329 | |
| 6330 | if (ret == 0 && |
| 6331 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 6332 | /* need to delete old one and insert a new one */ |
| 6333 | ret = btrfs_del_item(trans, dev_root, path); |
| 6334 | if (ret != 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6335 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6336 | "delete too small dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6337 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6338 | goto out; |
| 6339 | } |
| 6340 | ret = 1; |
| 6341 | } |
| 6342 | |
| 6343 | if (ret == 1) { |
| 6344 | /* need to insert a new item */ |
| 6345 | btrfs_release_path(path); |
| 6346 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 6347 | &key, sizeof(*ptr)); |
| 6348 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6349 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6350 | "insert dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6351 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6352 | goto out; |
| 6353 | } |
| 6354 | } |
| 6355 | |
| 6356 | eb = path->nodes[0]; |
| 6357 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 6358 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6359 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 6360 | btrfs_dev_stat_read(device, i)); |
| 6361 | btrfs_mark_buffer_dirty(eb); |
| 6362 | |
| 6363 | out: |
| 6364 | btrfs_free_path(path); |
| 6365 | return ret; |
| 6366 | } |
| 6367 | |
| 6368 | /* |
| 6369 | * called from commit_transaction. Writes all changed device stats to disk. |
| 6370 | */ |
| 6371 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 6372 | struct btrfs_fs_info *fs_info) |
| 6373 | { |
| 6374 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6375 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6376 | struct btrfs_device *device; |
| 6377 | int ret = 0; |
| 6378 | |
| 6379 | mutex_lock(&fs_devices->device_list_mutex); |
| 6380 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 6381 | if (!device->dev_stats_valid || !device->dev_stats_dirty) |
| 6382 | continue; |
| 6383 | |
| 6384 | ret = update_dev_stat_item(trans, dev_root, device); |
| 6385 | if (!ret) |
| 6386 | device->dev_stats_dirty = 0; |
| 6387 | } |
| 6388 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6389 | |
| 6390 | return ret; |
| 6391 | } |
| 6392 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6393 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 6394 | { |
| 6395 | btrfs_dev_stat_inc(dev, index); |
| 6396 | btrfs_dev_stat_print_on_error(dev); |
| 6397 | } |
| 6398 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6399 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6400 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6401 | if (!dev->dev_stats_valid) |
| 6402 | return; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6403 | printk_ratelimited_in_rcu(KERN_ERR "BTRFS: " |
| 6404 | "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] | 6405 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6406 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6407 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6408 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6409 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6410 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6411 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6412 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6413 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 6414 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 6415 | int i; |
| 6416 | |
| 6417 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6418 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 6419 | break; |
| 6420 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 6421 | return; /* all values == 0, suppress message */ |
| 6422 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6423 | printk_in_rcu(KERN_INFO "BTRFS: " |
| 6424 | "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] | 6425 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6426 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6427 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6428 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 6429 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6430 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 6431 | } |
| 6432 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6433 | int btrfs_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6434 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6435 | { |
| 6436 | struct btrfs_device *dev; |
| 6437 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6438 | int i; |
| 6439 | |
| 6440 | mutex_lock(&fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6441 | dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6442 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6443 | |
| 6444 | if (!dev) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6445 | btrfs_warn(root->fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6446 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6447 | } else if (!dev->dev_stats_valid) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6448 | btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6449 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6450 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6451 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6452 | if (stats->nr_items > i) |
| 6453 | stats->values[i] = |
| 6454 | btrfs_dev_stat_read_and_reset(dev, i); |
| 6455 | else |
| 6456 | btrfs_dev_stat_reset(dev, i); |
| 6457 | } |
| 6458 | } else { |
| 6459 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6460 | if (stats->nr_items > i) |
| 6461 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 6462 | } |
| 6463 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 6464 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 6465 | return 0; |
| 6466 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6467 | |
| 6468 | int btrfs_scratch_superblock(struct btrfs_device *device) |
| 6469 | { |
| 6470 | struct buffer_head *bh; |
| 6471 | struct btrfs_super_block *disk_super; |
| 6472 | |
| 6473 | bh = btrfs_read_dev_super(device->bdev); |
| 6474 | if (!bh) |
| 6475 | return -EINVAL; |
| 6476 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 6477 | |
| 6478 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 6479 | set_buffer_dirty(bh); |
| 6480 | sync_dirty_buffer(bh); |
| 6481 | brelse(bh); |
| 6482 | |
| 6483 | return 0; |
| 6484 | } |