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