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