blob: 317afc7a2af4f89df81568d461682cc1b7ce25ab [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020026#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020027#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050028#include <linux/raid/pq.h>
29#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050030#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "ctree.h"
32#include "extent_map.h"
33#include "disk-io.h"
34#include "transaction.h"
35#include "print-tree.h"
36#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050037#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040038#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010039#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040040#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060041#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010042#include "dev-replace.h"
Chris Mason0b86a832008-03-24 15:01:56 -040043
Yan Zheng2b820322008-11-17 21:11:30 -050044static int init_first_rw_device(struct btrfs_trans_handle *trans,
45 struct btrfs_root *root,
46 struct btrfs_device *device);
47static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020048static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +000049static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020050static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050051
Chris Mason8a4b83c2008-03-24 15:02:07 -040052static DEFINE_MUTEX(uuid_mutex);
53static LIST_HEAD(fs_uuids);
54
Chris Mason7d9eb122008-07-08 14:19:17 -040055static void lock_chunks(struct btrfs_root *root)
56{
Chris Mason7d9eb122008-07-08 14:19:17 -040057 mutex_lock(&root->fs_info->chunk_mutex);
58}
59
60static void unlock_chunks(struct btrfs_root *root)
61{
Chris Mason7d9eb122008-07-08 14:19:17 -040062 mutex_unlock(&root->fs_info->chunk_mutex);
63}
64
Yan Zhenge4404d62008-12-12 10:03:26 -050065static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
66{
67 struct btrfs_device *device;
68 WARN_ON(fs_devices->opened);
69 while (!list_empty(&fs_devices->devices)) {
70 device = list_entry(fs_devices->devices.next,
71 struct btrfs_device, dev_list);
72 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -040073 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -050074 kfree(device);
75 }
76 kfree(fs_devices);
77}
78
Lukas Czernerb8b8ff52012-12-06 19:25:48 +000079static void btrfs_kobject_uevent(struct block_device *bdev,
80 enum kobject_action action)
81{
82 int ret;
83
84 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
85 if (ret)
86 pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n",
87 action,
88 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
89 &disk_to_dev(bdev->bd_disk)->kobj);
90}
91
Jeff Mahoney143bede2012-03-01 14:56:26 +010092void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -040093{
94 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040095
Yan Zheng2b820322008-11-17 21:11:30 -050096 while (!list_empty(&fs_uuids)) {
97 fs_devices = list_entry(fs_uuids.next,
98 struct btrfs_fs_devices, list);
99 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500100 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400101 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400102}
103
Chris Masona1b32a52008-09-05 16:09:51 -0400104static noinline struct btrfs_device *__find_device(struct list_head *head,
105 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400106{
107 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400108
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500109 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400110 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400111 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400112 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400113 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400114 }
115 return NULL;
116}
117
Chris Masona1b32a52008-09-05 16:09:51 -0400118static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400119{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400120 struct btrfs_fs_devices *fs_devices;
121
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500122 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400123 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
124 return fs_devices;
125 }
126 return NULL;
127}
128
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100129static int
130btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
131 int flush, struct block_device **bdev,
132 struct buffer_head **bh)
133{
134 int ret;
135
136 *bdev = blkdev_get_by_path(device_path, flags, holder);
137
138 if (IS_ERR(*bdev)) {
139 ret = PTR_ERR(*bdev);
140 printk(KERN_INFO "btrfs: open %s failed\n", device_path);
141 goto error;
142 }
143
144 if (flush)
145 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
146 ret = set_blocksize(*bdev, 4096);
147 if (ret) {
148 blkdev_put(*bdev, flags);
149 goto error;
150 }
151 invalidate_bdev(*bdev);
152 *bh = btrfs_read_dev_super(*bdev);
153 if (!*bh) {
154 ret = -EINVAL;
155 blkdev_put(*bdev, flags);
156 goto error;
157 }
158
159 return 0;
160
161error:
162 *bdev = NULL;
163 *bh = NULL;
164 return ret;
165}
166
Chris Masonffbd5172009-04-20 15:50:09 -0400167static void requeue_list(struct btrfs_pending_bios *pending_bios,
168 struct bio *head, struct bio *tail)
169{
170
171 struct bio *old_head;
172
173 old_head = pending_bios->head;
174 pending_bios->head = head;
175 if (pending_bios->tail)
176 tail->bi_next = old_head;
177 else
178 pending_bios->tail = tail;
179}
180
Chris Mason8b712842008-06-11 16:50:36 -0400181/*
182 * we try to collect pending bios for a device so we don't get a large
183 * number of procs sending bios down to the same device. This greatly
184 * improves the schedulers ability to collect and merge the bios.
185 *
186 * But, it also turns into a long list of bios to process and that is sure
187 * to eventually make the worker thread block. The solution here is to
188 * make some progress and then put this work struct back at the end of
189 * the list if the block device is congested. This way, multiple devices
190 * can make progress from a single worker thread.
191 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100192static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400193{
194 struct bio *pending;
195 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400196 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400197 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400198 struct bio *tail;
199 struct bio *cur;
200 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400201 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400202 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400203 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400204 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400205 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000206 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400207 struct blk_plug plug;
208
209 /*
210 * this function runs all the bios we've collected for
211 * a particular device. We don't want to wander off to
212 * another device without first sending all of these down.
213 * So, setup a plug here and finish it off before we return
214 */
215 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400216
Chris Masonbedf7622009-04-03 10:32:58 -0400217 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400218 fs_info = device->dev_root->fs_info;
219 limit = btrfs_async_submit_limit(fs_info);
220 limit = limit * 2 / 3;
221
Chris Mason8b712842008-06-11 16:50:36 -0400222loop:
223 spin_lock(&device->io_lock);
224
Chris Masona6837052009-02-04 09:19:41 -0500225loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400226 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400227
Chris Mason8b712842008-06-11 16:50:36 -0400228 /* take all the bios off the list at once and process them
229 * later on (without the lock held). But, remember the
230 * tail and other pointers so the bios can be properly reinserted
231 * into the list if we hit congestion
232 */
Chris Masond84275c2009-06-09 15:39:08 -0400233 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400234 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400235 force_reg = 1;
236 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400237 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400238 force_reg = 0;
239 }
Chris Masonffbd5172009-04-20 15:50:09 -0400240
241 pending = pending_bios->head;
242 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400243 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400244
245 /*
246 * if pending was null this time around, no bios need processing
247 * at all and we can stop. Otherwise it'll loop back up again
248 * and do an additional check so no bios are missed.
249 *
250 * device->running_pending is used to synchronize with the
251 * schedule_bio code.
252 */
Chris Masonffbd5172009-04-20 15:50:09 -0400253 if (device->pending_sync_bios.head == NULL &&
254 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400255 again = 0;
256 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400257 } else {
258 again = 1;
259 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400260 }
Chris Masonffbd5172009-04-20 15:50:09 -0400261
262 pending_bios->head = NULL;
263 pending_bios->tail = NULL;
264
Chris Mason8b712842008-06-11 16:50:36 -0400265 spin_unlock(&device->io_lock);
266
Chris Masond3977122009-01-05 21:25:51 -0500267 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400268
269 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400270 /* we want to work on both lists, but do more bios on the
271 * sync list than the regular list
272 */
273 if ((num_run > 32 &&
274 pending_bios != &device->pending_sync_bios &&
275 device->pending_sync_bios.head) ||
276 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
277 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400278 spin_lock(&device->io_lock);
279 requeue_list(pending_bios, pending, tail);
280 goto loop_lock;
281 }
282
Chris Mason8b712842008-06-11 16:50:36 -0400283 cur = pending;
284 pending = pending->bi_next;
285 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400286
Josef Bacik66657b32012-08-01 15:36:24 -0400287 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400288 waitqueue_active(&fs_info->async_submit_wait))
289 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400290
291 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400292
Chris Mason2ab1ba62011-08-04 14:28:36 -0400293 /*
294 * if we're doing the sync list, record that our
295 * plug has some sync requests on it
296 *
297 * If we're doing the regular list and there are
298 * sync requests sitting around, unplug before
299 * we add more
300 */
301 if (pending_bios == &device->pending_sync_bios) {
302 sync_pending = 1;
303 } else if (sync_pending) {
304 blk_finish_plug(&plug);
305 blk_start_plug(&plug);
306 sync_pending = 0;
307 }
308
Stefan Behrens21adbd52011-11-09 13:44:05 +0100309 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400310 num_run++;
311 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100312 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400313 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400314
315 /*
316 * we made progress, there is more work to do and the bdi
317 * is now congested. Back off and let other work structs
318 * run instead
319 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400320 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500321 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400322 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400323
Chris Masonb765ead2009-04-03 10:27:10 -0400324 ioc = current->io_context;
325
326 /*
327 * the main goal here is that we don't want to
328 * block if we're going to be able to submit
329 * more requests without blocking.
330 *
331 * This code does two great things, it pokes into
332 * the elevator code from a filesystem _and_
333 * it makes assumptions about how batching works.
334 */
335 if (ioc && ioc->nr_batch_requests > 0 &&
336 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
337 (last_waited == 0 ||
338 ioc->last_waited == last_waited)) {
339 /*
340 * we want to go through our batch of
341 * requests and stop. So, we copy out
342 * the ioc->last_waited time and test
343 * against it before looping
344 */
345 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100346 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400347 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400348 continue;
349 }
Chris Mason8b712842008-06-11 16:50:36 -0400350 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400351 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500352 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400353
354 spin_unlock(&device->io_lock);
355 btrfs_requeue_work(&device->work);
356 goto done;
357 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500358 /* unplug every 64 requests just for good measure */
359 if (batch_run % 64 == 0) {
360 blk_finish_plug(&plug);
361 blk_start_plug(&plug);
362 sync_pending = 0;
363 }
Chris Mason8b712842008-06-11 16:50:36 -0400364 }
Chris Masonffbd5172009-04-20 15:50:09 -0400365
Chris Mason51684082010-03-10 15:33:32 -0500366 cond_resched();
367 if (again)
368 goto loop;
369
370 spin_lock(&device->io_lock);
371 if (device->pending_bios.head || device->pending_sync_bios.head)
372 goto loop_lock;
373 spin_unlock(&device->io_lock);
374
Chris Mason8b712842008-06-11 16:50:36 -0400375done:
Chris Mason211588a2011-04-19 20:12:40 -0400376 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400377}
378
Christoph Hellwigb2950862008-12-02 09:54:17 -0500379static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400380{
381 struct btrfs_device *device;
382
383 device = container_of(work, struct btrfs_device, work);
384 run_scheduled_bios(device);
385}
386
Chris Masona1b32a52008-09-05 16:09:51 -0400387static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400388 struct btrfs_super_block *disk_super,
389 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
390{
391 struct btrfs_device *device;
392 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400393 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400394 u64 found_transid = btrfs_super_generation(disk_super);
395
396 fs_devices = find_fsid(disk_super->fsid);
397 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400398 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400399 if (!fs_devices)
400 return -ENOMEM;
401 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400402 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400403 list_add(&fs_devices->list, &fs_uuids);
404 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
405 fs_devices->latest_devid = devid;
406 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400407 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400408 device = NULL;
409 } else {
Chris Masona4437552008-04-18 10:29:38 -0400410 device = __find_device(&fs_devices->devices, devid,
411 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400412 }
413 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500414 if (fs_devices->opened)
415 return -EBUSY;
416
Chris Mason8a4b83c2008-03-24 15:02:07 -0400417 device = kzalloc(sizeof(*device), GFP_NOFS);
418 if (!device) {
419 /* we can safely leave the fs_devices entry around */
420 return -ENOMEM;
421 }
422 device->devid = devid;
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200423 device->dev_stats_valid = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400424 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400425 memcpy(device->uuid, disk_super->dev_item.uuid,
426 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400427 spin_lock_init(&device->io_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400428
429 name = rcu_string_strdup(path, GFP_NOFS);
430 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400431 kfree(device);
432 return -ENOMEM;
433 }
Josef Bacik606686e2012-06-04 14:03:51 -0400434 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -0500435 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400436
Arne Jansen90519d62011-05-23 14:30:00 +0200437 /* init readahead state */
438 spin_lock_init(&device->reada_lock);
439 device->reada_curr_zone = NULL;
440 atomic_set(&device->reada_in_flight, 0);
441 device->reada_next = 0;
442 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
443 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
444
Chris Masone5e9a522009-06-10 15:17:02 -0400445 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000446 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400447 mutex_unlock(&fs_devices->device_list_mutex);
448
Yan Zheng2b820322008-11-17 21:11:30 -0500449 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400450 fs_devices->num_devices++;
Josef Bacik606686e2012-06-04 14:03:51 -0400451 } else if (!device->name || strcmp(device->name->str, path)) {
452 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000453 if (!name)
454 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400455 rcu_string_free(device->name);
456 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500457 if (device->missing) {
458 fs_devices->missing_devices--;
459 device->missing = 0;
460 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400461 }
462
463 if (found_transid > fs_devices->latest_trans) {
464 fs_devices->latest_devid = devid;
465 fs_devices->latest_trans = found_transid;
466 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400467 *fs_devices_ret = fs_devices;
468 return 0;
469}
470
Yan Zhenge4404d62008-12-12 10:03:26 -0500471static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
472{
473 struct btrfs_fs_devices *fs_devices;
474 struct btrfs_device *device;
475 struct btrfs_device *orig_dev;
476
477 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
478 if (!fs_devices)
479 return ERR_PTR(-ENOMEM);
480
481 INIT_LIST_HEAD(&fs_devices->devices);
482 INIT_LIST_HEAD(&fs_devices->alloc_list);
483 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400484 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500485 fs_devices->latest_devid = orig->latest_devid;
486 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400487 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500488 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
489
Xiao Guangrong46224702011-04-20 10:08:47 +0000490 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500491 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400492 struct rcu_string *name;
493
Yan Zhenge4404d62008-12-12 10:03:26 -0500494 device = kzalloc(sizeof(*device), GFP_NOFS);
495 if (!device)
496 goto error;
497
Josef Bacik606686e2012-06-04 14:03:51 -0400498 /*
499 * This is ok to do without rcu read locked because we hold the
500 * uuid mutex so nothing we touch in here is going to disappear.
501 */
502 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
503 if (!name) {
Julia Lawallfd2696f2009-09-29 13:51:04 -0400504 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500505 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400506 }
Josef Bacik606686e2012-06-04 14:03:51 -0400507 rcu_assign_pointer(device->name, name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500508
509 device->devid = orig_dev->devid;
510 device->work.func = pending_bios_fn;
511 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500512 spin_lock_init(&device->io_lock);
513 INIT_LIST_HEAD(&device->dev_list);
514 INIT_LIST_HEAD(&device->dev_alloc_list);
515
516 list_add(&device->dev_list, &fs_devices->devices);
517 device->fs_devices = fs_devices;
518 fs_devices->num_devices++;
519 }
520 return fs_devices;
521error:
522 free_fs_devices(fs_devices);
523 return ERR_PTR(-ENOMEM);
524}
525
Stefan Behrens8dabb742012-11-06 13:15:27 +0100526void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
527 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400528{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500529 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400530
Chris Masona6b0d5c2012-02-20 20:53:43 -0500531 struct block_device *latest_bdev = NULL;
532 u64 latest_devid = 0;
533 u64 latest_transid = 0;
534
Chris Masondfe25022008-05-13 13:46:40 -0400535 mutex_lock(&uuid_mutex);
536again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000537 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500538 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500539 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100540 if (!device->is_tgtdev_for_dev_replace &&
541 (!latest_transid ||
542 device->generation > latest_transid)) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500543 latest_devid = device->devid;
544 latest_transid = device->generation;
545 latest_bdev = device->bdev;
546 }
Yan Zheng2b820322008-11-17 21:11:30 -0500547 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500548 }
Yan Zheng2b820322008-11-17 21:11:30 -0500549
Stefan Behrens8dabb742012-11-06 13:15:27 +0100550 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
551 /*
552 * In the first step, keep the device which has
553 * the correct fsid and the devid that is used
554 * for the dev_replace procedure.
555 * In the second step, the dev_replace state is
556 * read from the device tree and it is known
557 * whether the procedure is really active or
558 * not, which means whether this device is
559 * used or whether it should be removed.
560 */
561 if (step == 0 || device->is_tgtdev_for_dev_replace) {
562 continue;
563 }
564 }
Yan Zheng2b820322008-11-17 21:11:30 -0500565 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100566 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500567 device->bdev = NULL;
568 fs_devices->open_devices--;
569 }
570 if (device->writeable) {
571 list_del_init(&device->dev_alloc_list);
572 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100573 if (!device->is_tgtdev_for_dev_replace)
574 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500575 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500576 list_del_init(&device->dev_list);
577 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400578 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500579 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400580 }
Yan Zheng2b820322008-11-17 21:11:30 -0500581
582 if (fs_devices->seed) {
583 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500584 goto again;
585 }
586
Chris Masona6b0d5c2012-02-20 20:53:43 -0500587 fs_devices->latest_bdev = latest_bdev;
588 fs_devices->latest_devid = latest_devid;
589 fs_devices->latest_trans = latest_transid;
590
Chris Masondfe25022008-05-13 13:46:40 -0400591 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400592}
Chris Masona0af4692008-05-13 16:03:06 -0400593
Xiao Guangrong1f781602011-04-20 10:09:16 +0000594static void __free_device(struct work_struct *work)
595{
596 struct btrfs_device *device;
597
598 device = container_of(work, struct btrfs_device, rcu_work);
599
600 if (device->bdev)
601 blkdev_put(device->bdev, device->mode);
602
Josef Bacik606686e2012-06-04 14:03:51 -0400603 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000604 kfree(device);
605}
606
607static void free_device(struct rcu_head *head)
608{
609 struct btrfs_device *device;
610
611 device = container_of(head, struct btrfs_device, rcu);
612
613 INIT_WORK(&device->rcu_work, __free_device);
614 schedule_work(&device->rcu_work);
615}
616
Yan Zheng2b820322008-11-17 21:11:30 -0500617static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400618{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400619 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500620
Yan Zheng2b820322008-11-17 21:11:30 -0500621 if (--fs_devices->opened > 0)
622 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400623
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000624 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500625 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000626 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400627 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000628
629 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400630 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000631
Stefan Behrens8dabb742012-11-06 13:15:27 +0100632 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -0500633 list_del_init(&device->dev_alloc_list);
634 fs_devices->rw_devices--;
635 }
636
Josef Bacikd5e20032011-08-04 14:52:27 +0000637 if (device->can_discard)
638 fs_devices->num_can_discard--;
639
Xiao Guangrong1f781602011-04-20 10:09:16 +0000640 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100641 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000642 memcpy(new_device, device, sizeof(*new_device));
Josef Bacik606686e2012-06-04 14:03:51 -0400643
644 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400645 if (device->name) {
646 name = rcu_string_strdup(device->name->str, GFP_NOFS);
647 BUG_ON(device->name && !name); /* -ENOMEM */
648 rcu_assign_pointer(new_device->name, name);
649 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000650 new_device->bdev = NULL;
651 new_device->writeable = 0;
652 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000653 new_device->can_discard = 0;
Thomas Gleixner1cba0cd2013-02-20 14:06:20 -0500654 spin_lock_init(&new_device->io_lock);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000655 list_replace_rcu(&device->dev_list, &new_device->dev_list);
656
657 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400658 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000659 mutex_unlock(&fs_devices->device_list_mutex);
660
Yan Zhenge4404d62008-12-12 10:03:26 -0500661 WARN_ON(fs_devices->open_devices);
662 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500663 fs_devices->opened = 0;
664 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500665
Chris Mason8a4b83c2008-03-24 15:02:07 -0400666 return 0;
667}
668
Yan Zheng2b820322008-11-17 21:11:30 -0500669int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
670{
Yan Zhenge4404d62008-12-12 10:03:26 -0500671 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500672 int ret;
673
674 mutex_lock(&uuid_mutex);
675 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500676 if (!fs_devices->opened) {
677 seed_devices = fs_devices->seed;
678 fs_devices->seed = NULL;
679 }
Yan Zheng2b820322008-11-17 21:11:30 -0500680 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500681
682 while (seed_devices) {
683 fs_devices = seed_devices;
684 seed_devices = fs_devices->seed;
685 __btrfs_close_devices(fs_devices);
686 free_fs_devices(fs_devices);
687 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000688 /*
689 * Wait for rcu kworkers under __btrfs_close_devices
690 * to finish all blkdev_puts so device is really
691 * free when umount is done.
692 */
693 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500694 return ret;
695}
696
Yan Zhenge4404d62008-12-12 10:03:26 -0500697static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
698 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400699{
Josef Bacikd5e20032011-08-04 14:52:27 +0000700 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400701 struct block_device *bdev;
702 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400703 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400704 struct block_device *latest_bdev = NULL;
705 struct buffer_head *bh;
706 struct btrfs_super_block *disk_super;
707 u64 latest_devid = 0;
708 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400709 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500710 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400711 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400712
Tejun Heod4d77622010-11-13 11:55:18 +0100713 flags |= FMODE_EXCL;
714
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500715 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400716 if (device->bdev)
717 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400718 if (!device->name)
719 continue;
720
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000721 /* Just open everything we can; ignore failures here */
722 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
723 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100724 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400725
726 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000727 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400728 if (devid != device->devid)
729 goto error_brelse;
730
Yan Zheng2b820322008-11-17 21:11:30 -0500731 if (memcmp(device->uuid, disk_super->dev_item.uuid,
732 BTRFS_UUID_SIZE))
733 goto error_brelse;
734
735 device->generation = btrfs_super_generation(disk_super);
736 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400737 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500738 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400739 latest_bdev = bdev;
740 }
741
Yan Zheng2b820322008-11-17 21:11:30 -0500742 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
743 device->writeable = 0;
744 } else {
745 device->writeable = !bdev_read_only(bdev);
746 seeding = 0;
747 }
748
Josef Bacikd5e20032011-08-04 14:52:27 +0000749 q = bdev_get_queue(bdev);
750 if (blk_queue_discard(q)) {
751 device->can_discard = 1;
752 fs_devices->num_can_discard++;
753 }
754
Chris Mason8a4b83c2008-03-24 15:02:07 -0400755 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400756 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500757 device->mode = flags;
758
Chris Masonc2898112009-06-10 09:51:32 -0400759 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
760 fs_devices->rotating = 1;
761
Chris Masona0af4692008-05-13 16:03:06 -0400762 fs_devices->open_devices++;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100763 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -0500764 fs_devices->rw_devices++;
765 list_add(&device->dev_alloc_list,
766 &fs_devices->alloc_list);
767 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000768 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400769 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400770
Chris Masona0af4692008-05-13 16:03:06 -0400771error_brelse:
772 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100773 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400774 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400775 }
Chris Masona0af4692008-05-13 16:03:06 -0400776 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300777 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400778 goto out;
779 }
Yan Zheng2b820322008-11-17 21:11:30 -0500780 fs_devices->seeding = seeding;
781 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400782 fs_devices->latest_bdev = latest_bdev;
783 fs_devices->latest_devid = latest_devid;
784 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500785 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400786out:
Yan Zheng2b820322008-11-17 21:11:30 -0500787 return ret;
788}
789
790int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500791 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500792{
793 int ret;
794
795 mutex_lock(&uuid_mutex);
796 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500797 fs_devices->opened++;
798 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500799 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500800 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500801 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400802 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400803 return ret;
804}
805
David Sterba6f60cbd2013-02-15 11:31:02 -0700806/*
807 * Look for a btrfs signature on a device. This may be called out of the mount path
808 * and we are not allowed to call set_blocksize during the scan. The superblock
809 * is read via pagecache
810 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500811int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400812 struct btrfs_fs_devices **fs_devices_ret)
813{
814 struct btrfs_super_block *disk_super;
815 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700816 struct page *page;
817 void *p;
818 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400819 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400820 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400821 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700822 u64 bytenr;
823 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400824
David Sterba6f60cbd2013-02-15 11:31:02 -0700825 /*
826 * we would like to check all the supers, but that would make
827 * a btrfs mount succeed after a mkfs from a different FS.
828 * So, we need to add a special mount option to scan for
829 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
830 */
831 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100832 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500833 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700834
835 bdev = blkdev_get_by_path(path, flags, holder);
836
837 if (IS_ERR(bdev)) {
838 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100839 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700840 }
841
842 /* make sure our super fits in the device */
843 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
844 goto error_bdev_put;
845
846 /* make sure our super fits in the page */
847 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
848 goto error_bdev_put;
849
850 /* make sure our super doesn't straddle pages on disk */
851 index = bytenr >> PAGE_CACHE_SHIFT;
852 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
853 goto error_bdev_put;
854
855 /* pull in the page with our super */
856 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
857 index, GFP_NOFS);
858
859 if (IS_ERR_OR_NULL(page))
860 goto error_bdev_put;
861
862 p = kmap(page);
863
864 /* align our pointer to the offset of the super block */
865 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
866
867 if (btrfs_super_bytenr(disk_super) != bytenr ||
Zach Browncdb4c572013-02-20 00:55:13 +0000868 disk_super->magic != cpu_to_le64(BTRFS_MAGIC))
David Sterba6f60cbd2013-02-15 11:31:02 -0700869 goto error_unmap;
870
Xiao Guangronga3438322010-01-06 11:48:18 +0000871 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400872 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400873 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700874
Stefan Behrensd03f9182012-11-05 13:10:49 +0000875 if (disk_super->label[0]) {
876 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
877 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
Chris Masond3977122009-01-05 21:25:51 -0500878 printk(KERN_INFO "device label %s ", disk_super->label);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000879 } else {
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200880 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000881 }
David Sterba6f60cbd2013-02-15 11:31:02 -0700882
Roland Dreier119e10c2009-01-21 10:49:16 -0500883 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500884 (unsigned long long)devid, (unsigned long long)transid, path);
David Sterba6f60cbd2013-02-15 11:31:02 -0700885
Chris Mason8a4b83c2008-03-24 15:02:07 -0400886 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400887 if (!ret && fs_devices_ret)
888 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700889
890error_unmap:
891 kunmap(page);
892 page_cache_release(page);
893
894error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100895 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400896error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100897 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400898 return ret;
899}
Chris Mason0b86a832008-03-24 15:01:56 -0400900
Miao Xie6d07bce2011-01-05 10:07:31 +0000901/* helper to account the used device space in the range */
902int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
903 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400904{
905 struct btrfs_key key;
906 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000907 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500908 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000909 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400910 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000911 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400912 struct extent_buffer *l;
913
Miao Xie6d07bce2011-01-05 10:07:31 +0000914 *length = 0;
915
Stefan Behrens63a212a2012-11-05 18:29:28 +0100916 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000917 return 0;
918
Yan Zheng2b820322008-11-17 21:11:30 -0500919 path = btrfs_alloc_path();
920 if (!path)
921 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400922 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400923
Chris Mason0b86a832008-03-24 15:01:56 -0400924 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000925 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400926 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000927
928 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400929 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000930 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400931 if (ret > 0) {
932 ret = btrfs_previous_item(root, path, key.objectid, key.type);
933 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000934 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400935 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000936
Chris Mason0b86a832008-03-24 15:01:56 -0400937 while (1) {
938 l = path->nodes[0];
939 slot = path->slots[0];
940 if (slot >= btrfs_header_nritems(l)) {
941 ret = btrfs_next_leaf(root, path);
942 if (ret == 0)
943 continue;
944 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000945 goto out;
946
947 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400948 }
949 btrfs_item_key_to_cpu(l, &key, slot);
950
951 if (key.objectid < device->devid)
952 goto next;
953
954 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000955 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400956
Chris Masond3977122009-01-05 21:25:51 -0500957 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400958 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400959
Chris Mason0b86a832008-03-24 15:01:56 -0400960 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000961 extent_end = key.offset + btrfs_dev_extent_length(l,
962 dev_extent);
963 if (key.offset <= start && extent_end > end) {
964 *length = end - start + 1;
965 break;
966 } else if (key.offset <= start && extent_end > start)
967 *length += extent_end - start;
968 else if (key.offset > start && extent_end <= end)
969 *length += extent_end - key.offset;
970 else if (key.offset > start && key.offset <= end) {
971 *length += end - key.offset + 1;
972 break;
973 } else if (key.offset > end)
974 break;
975
976next:
977 path->slots[0]++;
978 }
979 ret = 0;
980out:
981 btrfs_free_path(path);
982 return ret;
983}
984
Chris Mason0b86a832008-03-24 15:01:56 -0400985/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000986 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000987 * @device: the device which we search the free space in
988 * @num_bytes: the size of the free space that we need
989 * @start: store the start of the free space.
990 * @len: the size of the free space. that we find, or the size of the max
991 * free space if we don't find suitable free space
992 *
Chris Mason0b86a832008-03-24 15:01:56 -0400993 * this uses a pretty simple search, the expectation is that it is
994 * called very infrequently and that a given device has a small number
995 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000996 *
997 * @start is used to store the start of the free space if we find. But if we
998 * don't find suitable free space, it will be used to store the start position
999 * of the max free space.
1000 *
1001 * @len is used to store the size of the free space that we find.
1002 * But if we don't find suitable free space, it is used to store the size of
1003 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001004 */
Li Zefan125ccb02011-12-08 15:07:24 +08001005int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001006 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001007{
1008 struct btrfs_key key;
1009 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001010 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001011 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001012 u64 hole_size;
1013 u64 max_hole_start;
1014 u64 max_hole_size;
1015 u64 extent_end;
1016 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001017 u64 search_end = device->total_bytes;
1018 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001019 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001020 struct extent_buffer *l;
1021
Chris Mason0b86a832008-03-24 15:01:56 -04001022 /* FIXME use last free of some kind */
1023
1024 /* we don't want to overwrite the superblock on the drive,
1025 * so we make sure to start at an offset of at least 1MB
1026 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001027 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001028
Miao Xie7bfc8372011-01-05 10:07:26 +00001029 max_hole_start = search_start;
1030 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001031 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001032
Stefan Behrens63a212a2012-11-05 18:29:28 +01001033 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001034 ret = -ENOSPC;
1035 goto error;
1036 }
1037
1038 path = btrfs_alloc_path();
1039 if (!path) {
1040 ret = -ENOMEM;
1041 goto error;
1042 }
1043 path->reada = 2;
1044
Chris Mason0b86a832008-03-24 15:01:56 -04001045 key.objectid = device->devid;
1046 key.offset = search_start;
1047 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001048
Li Zefan125ccb02011-12-08 15:07:24 +08001049 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001050 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001051 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001052 if (ret > 0) {
1053 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1054 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001055 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001056 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001057
Chris Mason0b86a832008-03-24 15:01:56 -04001058 while (1) {
1059 l = path->nodes[0];
1060 slot = path->slots[0];
1061 if (slot >= btrfs_header_nritems(l)) {
1062 ret = btrfs_next_leaf(root, path);
1063 if (ret == 0)
1064 continue;
1065 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001066 goto out;
1067
1068 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001069 }
1070 btrfs_item_key_to_cpu(l, &key, slot);
1071
1072 if (key.objectid < device->devid)
1073 goto next;
1074
1075 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001076 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001077
Chris Mason0b86a832008-03-24 15:01:56 -04001078 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1079 goto next;
1080
Miao Xie7bfc8372011-01-05 10:07:26 +00001081 if (key.offset > search_start) {
1082 hole_size = key.offset - search_start;
1083
1084 if (hole_size > max_hole_size) {
1085 max_hole_start = search_start;
1086 max_hole_size = hole_size;
1087 }
1088
1089 /*
1090 * If this free space is greater than which we need,
1091 * it must be the max free space that we have found
1092 * until now, so max_hole_start must point to the start
1093 * of this free space and the length of this free space
1094 * is stored in max_hole_size. Thus, we return
1095 * max_hole_start and max_hole_size and go back to the
1096 * caller.
1097 */
1098 if (hole_size >= num_bytes) {
1099 ret = 0;
1100 goto out;
1101 }
1102 }
1103
Chris Mason0b86a832008-03-24 15:01:56 -04001104 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001105 extent_end = key.offset + btrfs_dev_extent_length(l,
1106 dev_extent);
1107 if (extent_end > search_start)
1108 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001109next:
1110 path->slots[0]++;
1111 cond_resched();
1112 }
Chris Mason0b86a832008-03-24 15:01:56 -04001113
liubo38c01b92011-08-02 02:39:03 +00001114 /*
1115 * At this point, search_start should be the end of
1116 * allocated dev extents, and when shrinking the device,
1117 * search_end may be smaller than search_start.
1118 */
1119 if (search_end > search_start)
1120 hole_size = search_end - search_start;
1121
Miao Xie7bfc8372011-01-05 10:07:26 +00001122 if (hole_size > max_hole_size) {
1123 max_hole_start = search_start;
1124 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001125 }
Chris Mason0b86a832008-03-24 15:01:56 -04001126
Miao Xie7bfc8372011-01-05 10:07:26 +00001127 /* See above. */
1128 if (hole_size < num_bytes)
1129 ret = -ENOSPC;
1130 else
1131 ret = 0;
1132
1133out:
Yan Zheng2b820322008-11-17 21:11:30 -05001134 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001135error:
1136 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001137 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001138 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001139 return ret;
1140}
1141
Christoph Hellwigb2950862008-12-02 09:54:17 -05001142static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001143 struct btrfs_device *device,
1144 u64 start)
1145{
1146 int ret;
1147 struct btrfs_path *path;
1148 struct btrfs_root *root = device->dev_root;
1149 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001150 struct btrfs_key found_key;
1151 struct extent_buffer *leaf = NULL;
1152 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001153
1154 path = btrfs_alloc_path();
1155 if (!path)
1156 return -ENOMEM;
1157
1158 key.objectid = device->devid;
1159 key.offset = start;
1160 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001161again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001162 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001163 if (ret > 0) {
1164 ret = btrfs_previous_item(root, path, key.objectid,
1165 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001166 if (ret)
1167 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001168 leaf = path->nodes[0];
1169 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1170 extent = btrfs_item_ptr(leaf, path->slots[0],
1171 struct btrfs_dev_extent);
1172 BUG_ON(found_key.offset > start || found_key.offset +
1173 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001174 key = found_key;
1175 btrfs_release_path(path);
1176 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001177 } else if (ret == 0) {
1178 leaf = path->nodes[0];
1179 extent = btrfs_item_ptr(leaf, path->slots[0],
1180 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001181 } else {
1182 btrfs_error(root->fs_info, ret, "Slot search failed");
1183 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001184 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001185
Josef Bacik2bf64752011-09-26 17:12:22 -04001186 if (device->bytes_used > 0) {
1187 u64 len = btrfs_dev_extent_length(leaf, extent);
1188 device->bytes_used -= len;
1189 spin_lock(&root->fs_info->free_chunk_lock);
1190 root->fs_info->free_chunk_space += len;
1191 spin_unlock(&root->fs_info->free_chunk_lock);
1192 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001193 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001194 if (ret) {
1195 btrfs_error(root->fs_info, ret,
1196 "Failed to remove dev extent item");
1197 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001198out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001199 btrfs_free_path(path);
1200 return ret;
1201}
1202
Eric Sandeen48a3b632013-04-25 20:41:01 +00001203static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1204 struct btrfs_device *device,
1205 u64 chunk_tree, u64 chunk_objectid,
1206 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001207{
1208 int ret;
1209 struct btrfs_path *path;
1210 struct btrfs_root *root = device->dev_root;
1211 struct btrfs_dev_extent *extent;
1212 struct extent_buffer *leaf;
1213 struct btrfs_key key;
1214
Chris Masondfe25022008-05-13 13:46:40 -04001215 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001216 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001217 path = btrfs_alloc_path();
1218 if (!path)
1219 return -ENOMEM;
1220
Chris Mason0b86a832008-03-24 15:01:56 -04001221 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001222 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001223 key.type = BTRFS_DEV_EXTENT_KEY;
1224 ret = btrfs_insert_empty_item(trans, root, path, &key,
1225 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001226 if (ret)
1227 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001228
1229 leaf = path->nodes[0];
1230 extent = btrfs_item_ptr(leaf, path->slots[0],
1231 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001232 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1233 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1234 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1235
1236 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1237 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1238 BTRFS_UUID_SIZE);
1239
Chris Mason0b86a832008-03-24 15:01:56 -04001240 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1241 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001242out:
Chris Mason0b86a832008-03-24 15:01:56 -04001243 btrfs_free_path(path);
1244 return ret;
1245}
1246
Chris Masona1b32a52008-09-05 16:09:51 -04001247static noinline int find_next_chunk(struct btrfs_root *root,
1248 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001249{
1250 struct btrfs_path *path;
1251 int ret;
1252 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001253 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001254 struct btrfs_key found_key;
1255
1256 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001257 if (!path)
1258 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001259
Chris Masone17cade2008-04-15 15:41:47 -04001260 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001261 key.offset = (u64)-1;
1262 key.type = BTRFS_CHUNK_ITEM_KEY;
1263
1264 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1265 if (ret < 0)
1266 goto error;
1267
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001268 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001269
1270 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1271 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001272 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001273 } else {
1274 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1275 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001276 if (found_key.objectid != objectid)
1277 *offset = 0;
1278 else {
1279 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1280 struct btrfs_chunk);
1281 *offset = found_key.offset +
1282 btrfs_chunk_length(path->nodes[0], chunk);
1283 }
Chris Mason0b86a832008-03-24 15:01:56 -04001284 }
1285 ret = 0;
1286error:
1287 btrfs_free_path(path);
1288 return ret;
1289}
1290
Yan Zheng2b820322008-11-17 21:11:30 -05001291static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001292{
1293 int ret;
1294 struct btrfs_key key;
1295 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001296 struct btrfs_path *path;
1297
1298 root = root->fs_info->chunk_root;
1299
1300 path = btrfs_alloc_path();
1301 if (!path)
1302 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001303
1304 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1305 key.type = BTRFS_DEV_ITEM_KEY;
1306 key.offset = (u64)-1;
1307
1308 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1309 if (ret < 0)
1310 goto error;
1311
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001312 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001313
1314 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1315 BTRFS_DEV_ITEM_KEY);
1316 if (ret) {
1317 *objectid = 1;
1318 } else {
1319 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1320 path->slots[0]);
1321 *objectid = found_key.offset + 1;
1322 }
1323 ret = 0;
1324error:
Yan Zheng2b820322008-11-17 21:11:30 -05001325 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001326 return ret;
1327}
1328
1329/*
1330 * the device information is stored in the chunk root
1331 * the btrfs_device struct should be fully filled in
1332 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001333static int btrfs_add_device(struct btrfs_trans_handle *trans,
1334 struct btrfs_root *root,
1335 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001336{
1337 int ret;
1338 struct btrfs_path *path;
1339 struct btrfs_dev_item *dev_item;
1340 struct extent_buffer *leaf;
1341 struct btrfs_key key;
1342 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001343
1344 root = root->fs_info->chunk_root;
1345
1346 path = btrfs_alloc_path();
1347 if (!path)
1348 return -ENOMEM;
1349
Chris Mason0b86a832008-03-24 15:01:56 -04001350 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1351 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001352 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001353
1354 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001355 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001356 if (ret)
1357 goto out;
1358
1359 leaf = path->nodes[0];
1360 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1361
1362 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001363 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001364 btrfs_set_device_type(leaf, dev_item, device->type);
1365 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1366 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1367 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001368 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1369 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001370 btrfs_set_device_group(leaf, dev_item, 0);
1371 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1372 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001373 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001374
Chris Mason0b86a832008-03-24 15:01:56 -04001375 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001376 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001377 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1378 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001379 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001380
Yan Zheng2b820322008-11-17 21:11:30 -05001381 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001382out:
1383 btrfs_free_path(path);
1384 return ret;
1385}
Chris Mason8f18cf12008-04-25 16:53:30 -04001386
Chris Masona061fc82008-05-07 11:43:44 -04001387static int btrfs_rm_dev_item(struct btrfs_root *root,
1388 struct btrfs_device *device)
1389{
1390 int ret;
1391 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001392 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001393 struct btrfs_trans_handle *trans;
1394
1395 root = root->fs_info->chunk_root;
1396
1397 path = btrfs_alloc_path();
1398 if (!path)
1399 return -ENOMEM;
1400
Yan, Zhenga22285a2010-05-16 10:48:46 -04001401 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001402 if (IS_ERR(trans)) {
1403 btrfs_free_path(path);
1404 return PTR_ERR(trans);
1405 }
Chris Masona061fc82008-05-07 11:43:44 -04001406 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1407 key.type = BTRFS_DEV_ITEM_KEY;
1408 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001409 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001410
1411 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1412 if (ret < 0)
1413 goto out;
1414
1415 if (ret > 0) {
1416 ret = -ENOENT;
1417 goto out;
1418 }
1419
1420 ret = btrfs_del_item(trans, root, path);
1421 if (ret)
1422 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001423out:
1424 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001425 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001426 btrfs_commit_transaction(trans, root);
1427 return ret;
1428}
1429
1430int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1431{
1432 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001433 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001434 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001435 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001436 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001437 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001438 u64 all_avail;
1439 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001440 u64 num_devices;
1441 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001442 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001443 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001444 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001445
Chris Masona061fc82008-05-07 11:43:44 -04001446 mutex_lock(&uuid_mutex);
1447
Miao Xiede98ced2013-01-29 10:13:12 +00001448 do {
1449 seq = read_seqbegin(&root->fs_info->profiles_lock);
1450
1451 all_avail = root->fs_info->avail_data_alloc_bits |
1452 root->fs_info->avail_system_alloc_bits |
1453 root->fs_info->avail_metadata_alloc_bits;
1454 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001455
Stefan Behrens8dabb742012-11-06 13:15:27 +01001456 num_devices = root->fs_info->fs_devices->num_devices;
1457 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1458 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1459 WARN_ON(num_devices < 1);
1460 num_devices--;
1461 }
1462 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1463
1464 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001465 printk(KERN_ERR "btrfs: unable to go below four devices "
1466 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001467 ret = -EINVAL;
1468 goto out;
1469 }
1470
Stefan Behrens8dabb742012-11-06 13:15:27 +01001471 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001472 printk(KERN_ERR "btrfs: unable to go below two "
1473 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001474 ret = -EINVAL;
1475 goto out;
1476 }
1477
David Woodhouse53b381b2013-01-29 18:40:14 -05001478 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1479 root->fs_info->fs_devices->rw_devices <= 2) {
1480 printk(KERN_ERR "btrfs: unable to go below two "
1481 "devices on raid5\n");
1482 ret = -EINVAL;
1483 goto out;
1484 }
1485 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1486 root->fs_info->fs_devices->rw_devices <= 3) {
1487 printk(KERN_ERR "btrfs: unable to go below three "
1488 "devices on raid6\n");
1489 ret = -EINVAL;
1490 goto out;
1491 }
1492
Chris Masondfe25022008-05-13 13:46:40 -04001493 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001494 struct list_head *devices;
1495 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001496
Chris Masondfe25022008-05-13 13:46:40 -04001497 device = NULL;
1498 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001499 /*
1500 * It is safe to read the devices since the volume_mutex
1501 * is held.
1502 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001503 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001504 if (tmp->in_fs_metadata &&
1505 !tmp->is_tgtdev_for_dev_replace &&
1506 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001507 device = tmp;
1508 break;
1509 }
1510 }
1511 bdev = NULL;
1512 bh = NULL;
1513 disk_super = NULL;
1514 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001515 printk(KERN_ERR "btrfs: no missing devices found to "
1516 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001517 goto out;
1518 }
Chris Masondfe25022008-05-13 13:46:40 -04001519 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001520 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001521 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001522 root->fs_info->bdev_holder, 0,
1523 &bdev, &bh);
1524 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001525 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001526 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001527 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001528 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001529 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001530 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001531 if (!device) {
1532 ret = -ENOENT;
1533 goto error_brelse;
1534 }
Chris Masondfe25022008-05-13 13:46:40 -04001535 }
Yan Zheng2b820322008-11-17 21:11:30 -05001536
Stefan Behrens63a212a2012-11-05 18:29:28 +01001537 if (device->is_tgtdev_for_dev_replace) {
1538 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1539 ret = -EINVAL;
1540 goto error_brelse;
1541 }
1542
Yan Zheng2b820322008-11-17 21:11:30 -05001543 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001544 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1545 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001546 ret = -EINVAL;
1547 goto error_brelse;
1548 }
1549
1550 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001551 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001552 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001553 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001554 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001555 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001556 }
Chris Masona061fc82008-05-07 11:43:44 -04001557
1558 ret = btrfs_shrink_device(device, 0);
1559 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001560 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001561
Stefan Behrens63a212a2012-11-05 18:29:28 +01001562 /*
1563 * TODO: the superblock still includes this device in its num_devices
1564 * counter although write_all_supers() is not locked out. This
1565 * could give a filesystem state which requires a degraded mount.
1566 */
Chris Masona061fc82008-05-07 11:43:44 -04001567 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1568 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001569 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001570
Josef Bacik2bf64752011-09-26 17:12:22 -04001571 spin_lock(&root->fs_info->free_chunk_lock);
1572 root->fs_info->free_chunk_space = device->total_bytes -
1573 device->bytes_used;
1574 spin_unlock(&root->fs_info->free_chunk_lock);
1575
Yan Zheng2b820322008-11-17 21:11:30 -05001576 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001577 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001578
1579 /*
1580 * the device list mutex makes sure that we don't change
1581 * the device list while someone else is writing out all
1582 * the device supers.
1583 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001584
1585 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001586 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001587 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001588
Yan Zhenge4404d62008-12-12 10:03:26 -05001589 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001590 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001591
Chris Masoncd02dca2010-12-13 14:56:23 -05001592 if (device->missing)
1593 root->fs_info->fs_devices->missing_devices--;
1594
Yan Zheng2b820322008-11-17 21:11:30 -05001595 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1596 struct btrfs_device, dev_list);
1597 if (device->bdev == root->fs_info->sb->s_bdev)
1598 root->fs_info->sb->s_bdev = next_device->bdev;
1599 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1600 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1601
Xiao Guangrong1f781602011-04-20 10:09:16 +00001602 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001603 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001604
1605 call_rcu(&device->rcu, free_device);
1606 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001607
David Sterba6c417612011-04-13 15:41:04 +02001608 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1609 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001610
Xiao Guangrong1f781602011-04-20 10:09:16 +00001611 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001612 struct btrfs_fs_devices *fs_devices;
1613 fs_devices = root->fs_info->fs_devices;
1614 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001615 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001616 break;
1617 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001618 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001619 fs_devices->seed = cur_devices->seed;
1620 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001621 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001622 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001623 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001624 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001625 }
1626
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001627 root->fs_info->num_tolerated_disk_barrier_failures =
1628 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1629
Yan Zheng2b820322008-11-17 21:11:30 -05001630 /*
1631 * at this point, the device is zero sized. We want to
1632 * remove it from the devices list and zero out the old super
1633 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001634 if (clear_super && disk_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001635 /* make sure this device isn't detected as part of
1636 * the FS anymore
1637 */
1638 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1639 set_buffer_dirty(bh);
1640 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001641 }
Chris Masona061fc82008-05-07 11:43:44 -04001642
Chris Masona061fc82008-05-07 11:43:44 -04001643 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001644
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001645 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001646 if (bdev)
1647 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001648
Chris Masona061fc82008-05-07 11:43:44 -04001649error_brelse:
1650 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001651 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001652 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001653out:
1654 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001655 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001656error_undo:
1657 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001658 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001659 list_add(&device->dev_alloc_list,
1660 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001661 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001662 root->fs_info->fs_devices->rw_devices++;
1663 }
1664 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001665}
1666
Stefan Behrense93c89c2012-11-05 17:33:06 +01001667void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1668 struct btrfs_device *srcdev)
1669{
1670 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1671 list_del_rcu(&srcdev->dev_list);
1672 list_del_rcu(&srcdev->dev_alloc_list);
1673 fs_info->fs_devices->num_devices--;
1674 if (srcdev->missing) {
1675 fs_info->fs_devices->missing_devices--;
1676 fs_info->fs_devices->rw_devices++;
1677 }
1678 if (srcdev->can_discard)
1679 fs_info->fs_devices->num_can_discard--;
1680 if (srcdev->bdev)
1681 fs_info->fs_devices->open_devices--;
1682
1683 call_rcu(&srcdev->rcu, free_device);
1684}
1685
1686void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1687 struct btrfs_device *tgtdev)
1688{
1689 struct btrfs_device *next_device;
1690
1691 WARN_ON(!tgtdev);
1692 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1693 if (tgtdev->bdev) {
1694 btrfs_scratch_superblock(tgtdev);
1695 fs_info->fs_devices->open_devices--;
1696 }
1697 fs_info->fs_devices->num_devices--;
1698 if (tgtdev->can_discard)
1699 fs_info->fs_devices->num_can_discard++;
1700
1701 next_device = list_entry(fs_info->fs_devices->devices.next,
1702 struct btrfs_device, dev_list);
1703 if (tgtdev->bdev == fs_info->sb->s_bdev)
1704 fs_info->sb->s_bdev = next_device->bdev;
1705 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1706 fs_info->fs_devices->latest_bdev = next_device->bdev;
1707 list_del_rcu(&tgtdev->dev_list);
1708
1709 call_rcu(&tgtdev->rcu, free_device);
1710
1711 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1712}
1713
Eric Sandeen48a3b632013-04-25 20:41:01 +00001714static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1715 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001716{
1717 int ret = 0;
1718 struct btrfs_super_block *disk_super;
1719 u64 devid;
1720 u8 *dev_uuid;
1721 struct block_device *bdev;
1722 struct buffer_head *bh;
1723
1724 *device = NULL;
1725 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1726 root->fs_info->bdev_holder, 0, &bdev, &bh);
1727 if (ret)
1728 return ret;
1729 disk_super = (struct btrfs_super_block *)bh->b_data;
1730 devid = btrfs_stack_device_id(&disk_super->dev_item);
1731 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001732 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001733 disk_super->fsid);
1734 brelse(bh);
1735 if (!*device)
1736 ret = -ENOENT;
1737 blkdev_put(bdev, FMODE_READ);
1738 return ret;
1739}
1740
1741int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1742 char *device_path,
1743 struct btrfs_device **device)
1744{
1745 *device = NULL;
1746 if (strcmp(device_path, "missing") == 0) {
1747 struct list_head *devices;
1748 struct btrfs_device *tmp;
1749
1750 devices = &root->fs_info->fs_devices->devices;
1751 /*
1752 * It is safe to read the devices since the volume_mutex
1753 * is held by the caller.
1754 */
1755 list_for_each_entry(tmp, devices, dev_list) {
1756 if (tmp->in_fs_metadata && !tmp->bdev) {
1757 *device = tmp;
1758 break;
1759 }
1760 }
1761
1762 if (!*device) {
1763 pr_err("btrfs: no missing device found\n");
1764 return -ENOENT;
1765 }
1766
1767 return 0;
1768 } else {
1769 return btrfs_find_device_by_path(root, device_path, device);
1770 }
1771}
1772
Yan Zheng2b820322008-11-17 21:11:30 -05001773/*
1774 * does all the dirty work required for changing file system's UUID.
1775 */
Li Zefan125ccb02011-12-08 15:07:24 +08001776static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001777{
1778 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1779 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001780 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001781 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001782 struct btrfs_device *device;
1783 u64 super_flags;
1784
1785 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001786 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001787 return -EINVAL;
1788
Yan Zhenge4404d62008-12-12 10:03:26 -05001789 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1790 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001791 return -ENOMEM;
1792
Yan Zhenge4404d62008-12-12 10:03:26 -05001793 old_devices = clone_fs_devices(fs_devices);
1794 if (IS_ERR(old_devices)) {
1795 kfree(seed_devices);
1796 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001797 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001798
Yan Zheng2b820322008-11-17 21:11:30 -05001799 list_add(&old_devices->list, &fs_uuids);
1800
Yan Zhenge4404d62008-12-12 10:03:26 -05001801 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1802 seed_devices->opened = 1;
1803 INIT_LIST_HEAD(&seed_devices->devices);
1804 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001805 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001806
1807 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001808 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1809 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001810 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1811
Yan Zhenge4404d62008-12-12 10:03:26 -05001812 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1813 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1814 device->fs_devices = seed_devices;
1815 }
1816
Yan Zheng2b820322008-11-17 21:11:30 -05001817 fs_devices->seeding = 0;
1818 fs_devices->num_devices = 0;
1819 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001820 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001821 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001822
1823 generate_random_uuid(fs_devices->fsid);
1824 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1825 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1826 super_flags = btrfs_super_flags(disk_super) &
1827 ~BTRFS_SUPER_FLAG_SEEDING;
1828 btrfs_set_super_flags(disk_super, super_flags);
1829
1830 return 0;
1831}
1832
1833/*
1834 * strore the expected generation for seed devices in device items.
1835 */
1836static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1837 struct btrfs_root *root)
1838{
1839 struct btrfs_path *path;
1840 struct extent_buffer *leaf;
1841 struct btrfs_dev_item *dev_item;
1842 struct btrfs_device *device;
1843 struct btrfs_key key;
1844 u8 fs_uuid[BTRFS_UUID_SIZE];
1845 u8 dev_uuid[BTRFS_UUID_SIZE];
1846 u64 devid;
1847 int ret;
1848
1849 path = btrfs_alloc_path();
1850 if (!path)
1851 return -ENOMEM;
1852
1853 root = root->fs_info->chunk_root;
1854 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1855 key.offset = 0;
1856 key.type = BTRFS_DEV_ITEM_KEY;
1857
1858 while (1) {
1859 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1860 if (ret < 0)
1861 goto error;
1862
1863 leaf = path->nodes[0];
1864next_slot:
1865 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1866 ret = btrfs_next_leaf(root, path);
1867 if (ret > 0)
1868 break;
1869 if (ret < 0)
1870 goto error;
1871 leaf = path->nodes[0];
1872 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001873 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001874 continue;
1875 }
1876
1877 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1878 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1879 key.type != BTRFS_DEV_ITEM_KEY)
1880 break;
1881
1882 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1883 struct btrfs_dev_item);
1884 devid = btrfs_device_id(leaf, dev_item);
1885 read_extent_buffer(leaf, dev_uuid,
1886 (unsigned long)btrfs_device_uuid(dev_item),
1887 BTRFS_UUID_SIZE);
1888 read_extent_buffer(leaf, fs_uuid,
1889 (unsigned long)btrfs_device_fsid(dev_item),
1890 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001891 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1892 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001893 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001894
1895 if (device->fs_devices->seeding) {
1896 btrfs_set_device_generation(leaf, dev_item,
1897 device->generation);
1898 btrfs_mark_buffer_dirty(leaf);
1899 }
1900
1901 path->slots[0]++;
1902 goto next_slot;
1903 }
1904 ret = 0;
1905error:
1906 btrfs_free_path(path);
1907 return ret;
1908}
1909
Chris Mason788f20e2008-04-28 15:29:42 -04001910int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1911{
Josef Bacikd5e20032011-08-04 14:52:27 +00001912 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001913 struct btrfs_trans_handle *trans;
1914 struct btrfs_device *device;
1915 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001916 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001917 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001918 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001919 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001920 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001921 int ret = 0;
1922
Yan Zheng2b820322008-11-17 21:11:30 -05001923 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001924 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001925
Li Zefana5d16332011-12-07 20:08:40 -05001926 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001927 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001928 if (IS_ERR(bdev))
1929 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001930
Yan Zheng2b820322008-11-17 21:11:30 -05001931 if (root->fs_info->fs_devices->seeding) {
1932 seeding_dev = 1;
1933 down_write(&sb->s_umount);
1934 mutex_lock(&uuid_mutex);
1935 }
1936
Chris Mason8c8bee12008-09-29 11:19:10 -04001937 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001938
Chris Mason788f20e2008-04-28 15:29:42 -04001939 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00001940
1941 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001942 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001943 if (device->bdev == bdev) {
1944 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00001945 mutex_unlock(
1946 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001947 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001948 }
1949 }
Liu Bod25628b2012-11-14 14:35:30 +00001950 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001951
1952 device = kzalloc(sizeof(*device), GFP_NOFS);
1953 if (!device) {
1954 /* we can safely leave the fs_devices entry around */
1955 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001956 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001957 }
1958
Josef Bacik606686e2012-06-04 14:03:51 -04001959 name = rcu_string_strdup(device_path, GFP_NOFS);
1960 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001961 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001962 ret = -ENOMEM;
1963 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001964 }
Josef Bacik606686e2012-06-04 14:03:51 -04001965 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001966
1967 ret = find_next_devid(root, &device->devid);
1968 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001969 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001970 kfree(device);
1971 goto error;
1972 }
1973
Yan, Zhenga22285a2010-05-16 10:48:46 -04001974 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001975 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001976 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001977 kfree(device);
1978 ret = PTR_ERR(trans);
1979 goto error;
1980 }
1981
Yan Zheng2b820322008-11-17 21:11:30 -05001982 lock_chunks(root);
1983
Josef Bacikd5e20032011-08-04 14:52:27 +00001984 q = bdev_get_queue(bdev);
1985 if (blk_queue_discard(q))
1986 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001987 device->writeable = 1;
1988 device->work.func = pending_bios_fn;
1989 generate_random_uuid(device->uuid);
1990 spin_lock_init(&device->io_lock);
1991 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001992 device->io_width = root->sectorsize;
1993 device->io_align = root->sectorsize;
1994 device->sector_size = root->sectorsize;
1995 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001996 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001997 device->dev_root = root->fs_info->dev_root;
1998 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001999 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002000 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002001 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002002 set_blocksize(device->bdev, 4096);
2003
Yan Zheng2b820322008-11-17 21:11:30 -05002004 if (seeding_dev) {
2005 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002006 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002007 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002008 }
2009
2010 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002011
Chris Masone5e9a522009-06-10 15:17:02 -04002012 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002013 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002014 list_add(&device->dev_alloc_list,
2015 &root->fs_info->fs_devices->alloc_list);
2016 root->fs_info->fs_devices->num_devices++;
2017 root->fs_info->fs_devices->open_devices++;
2018 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002019 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002020 if (device->can_discard)
2021 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002022 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2023
Josef Bacik2bf64752011-09-26 17:12:22 -04002024 spin_lock(&root->fs_info->free_chunk_lock);
2025 root->fs_info->free_chunk_space += device->total_bytes;
2026 spin_unlock(&root->fs_info->free_chunk_lock);
2027
Chris Masonc2898112009-06-10 09:51:32 -04002028 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2029 root->fs_info->fs_devices->rotating = 1;
2030
David Sterba6c417612011-04-13 15:41:04 +02002031 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2032 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002033 total_bytes + device->total_bytes);
2034
David Sterba6c417612011-04-13 15:41:04 +02002035 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2036 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002037 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04002038 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002039
Yan Zheng2b820322008-11-17 21:11:30 -05002040 if (seeding_dev) {
2041 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002042 if (ret) {
2043 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002044 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002045 }
Yan Zheng2b820322008-11-17 21:11:30 -05002046 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002047 if (ret) {
2048 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002049 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002050 }
Yan Zheng2b820322008-11-17 21:11:30 -05002051 } else {
2052 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002053 if (ret) {
2054 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002055 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002056 }
Yan Zheng2b820322008-11-17 21:11:30 -05002057 }
2058
Chris Mason913d9522009-03-10 13:17:18 -04002059 /*
2060 * we've got more storage, clear any full flags on the space
2061 * infos
2062 */
2063 btrfs_clear_space_info_full(root->fs_info);
2064
Chris Mason7d9eb122008-07-08 14:19:17 -04002065 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002066 root->fs_info->num_tolerated_disk_barrier_failures =
2067 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002068 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002069
2070 if (seeding_dev) {
2071 mutex_unlock(&uuid_mutex);
2072 up_write(&sb->s_umount);
2073
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002074 if (ret) /* transaction commit */
2075 return ret;
2076
Yan Zheng2b820322008-11-17 21:11:30 -05002077 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002078 if (ret < 0)
2079 btrfs_error(root->fs_info, ret,
2080 "Failed to relocate sys chunks after "
2081 "device initialization. This can be fixed "
2082 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002083 trans = btrfs_attach_transaction(root);
2084 if (IS_ERR(trans)) {
2085 if (PTR_ERR(trans) == -ENOENT)
2086 return 0;
2087 return PTR_ERR(trans);
2088 }
2089 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002090 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002091
Chris Mason788f20e2008-04-28 15:29:42 -04002092 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002093
2094error_trans:
2095 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002096 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002097 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002098 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002099error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002100 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002101 if (seeding_dev) {
2102 mutex_unlock(&uuid_mutex);
2103 up_write(&sb->s_umount);
2104 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002105 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002106}
2107
Stefan Behrense93c89c2012-11-05 17:33:06 +01002108int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2109 struct btrfs_device **device_out)
2110{
2111 struct request_queue *q;
2112 struct btrfs_device *device;
2113 struct block_device *bdev;
2114 struct btrfs_fs_info *fs_info = root->fs_info;
2115 struct list_head *devices;
2116 struct rcu_string *name;
2117 int ret = 0;
2118
2119 *device_out = NULL;
2120 if (fs_info->fs_devices->seeding)
2121 return -EINVAL;
2122
2123 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2124 fs_info->bdev_holder);
2125 if (IS_ERR(bdev))
2126 return PTR_ERR(bdev);
2127
2128 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2129
2130 devices = &fs_info->fs_devices->devices;
2131 list_for_each_entry(device, devices, dev_list) {
2132 if (device->bdev == bdev) {
2133 ret = -EEXIST;
2134 goto error;
2135 }
2136 }
2137
2138 device = kzalloc(sizeof(*device), GFP_NOFS);
2139 if (!device) {
2140 ret = -ENOMEM;
2141 goto error;
2142 }
2143
2144 name = rcu_string_strdup(device_path, GFP_NOFS);
2145 if (!name) {
2146 kfree(device);
2147 ret = -ENOMEM;
2148 goto error;
2149 }
2150 rcu_assign_pointer(device->name, name);
2151
2152 q = bdev_get_queue(bdev);
2153 if (blk_queue_discard(q))
2154 device->can_discard = 1;
2155 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2156 device->writeable = 1;
2157 device->work.func = pending_bios_fn;
2158 generate_random_uuid(device->uuid);
2159 device->devid = BTRFS_DEV_REPLACE_DEVID;
2160 spin_lock_init(&device->io_lock);
2161 device->generation = 0;
2162 device->io_width = root->sectorsize;
2163 device->io_align = root->sectorsize;
2164 device->sector_size = root->sectorsize;
2165 device->total_bytes = i_size_read(bdev->bd_inode);
2166 device->disk_total_bytes = device->total_bytes;
2167 device->dev_root = fs_info->dev_root;
2168 device->bdev = bdev;
2169 device->in_fs_metadata = 1;
2170 device->is_tgtdev_for_dev_replace = 1;
2171 device->mode = FMODE_EXCL;
2172 set_blocksize(device->bdev, 4096);
2173 device->fs_devices = fs_info->fs_devices;
2174 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2175 fs_info->fs_devices->num_devices++;
2176 fs_info->fs_devices->open_devices++;
2177 if (device->can_discard)
2178 fs_info->fs_devices->num_can_discard++;
2179 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2180
2181 *device_out = device;
2182 return ret;
2183
2184error:
2185 blkdev_put(bdev, FMODE_EXCL);
2186 return ret;
2187}
2188
2189void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2190 struct btrfs_device *tgtdev)
2191{
2192 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2193 tgtdev->io_width = fs_info->dev_root->sectorsize;
2194 tgtdev->io_align = fs_info->dev_root->sectorsize;
2195 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2196 tgtdev->dev_root = fs_info->dev_root;
2197 tgtdev->in_fs_metadata = 1;
2198}
2199
Chris Masond3977122009-01-05 21:25:51 -05002200static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2201 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002202{
2203 int ret;
2204 struct btrfs_path *path;
2205 struct btrfs_root *root;
2206 struct btrfs_dev_item *dev_item;
2207 struct extent_buffer *leaf;
2208 struct btrfs_key key;
2209
2210 root = device->dev_root->fs_info->chunk_root;
2211
2212 path = btrfs_alloc_path();
2213 if (!path)
2214 return -ENOMEM;
2215
2216 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2217 key.type = BTRFS_DEV_ITEM_KEY;
2218 key.offset = device->devid;
2219
2220 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2221 if (ret < 0)
2222 goto out;
2223
2224 if (ret > 0) {
2225 ret = -ENOENT;
2226 goto out;
2227 }
2228
2229 leaf = path->nodes[0];
2230 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2231
2232 btrfs_set_device_id(leaf, dev_item, device->devid);
2233 btrfs_set_device_type(leaf, dev_item, device->type);
2234 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2235 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2236 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002237 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002238 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2239 btrfs_mark_buffer_dirty(leaf);
2240
2241out:
2242 btrfs_free_path(path);
2243 return ret;
2244}
2245
Chris Mason7d9eb122008-07-08 14:19:17 -04002246static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002247 struct btrfs_device *device, u64 new_size)
2248{
2249 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002250 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002251 u64 old_total = btrfs_super_total_bytes(super_copy);
2252 u64 diff = new_size - device->total_bytes;
2253
Yan Zheng2b820322008-11-17 21:11:30 -05002254 if (!device->writeable)
2255 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002256 if (new_size <= device->total_bytes ||
2257 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002258 return -EINVAL;
2259
Chris Mason8f18cf12008-04-25 16:53:30 -04002260 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002261 device->fs_devices->total_rw_bytes += diff;
2262
2263 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002264 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002265 btrfs_clear_space_info_full(device->dev_root->fs_info);
2266
Chris Mason8f18cf12008-04-25 16:53:30 -04002267 return btrfs_update_device(trans, device);
2268}
2269
Chris Mason7d9eb122008-07-08 14:19:17 -04002270int btrfs_grow_device(struct btrfs_trans_handle *trans,
2271 struct btrfs_device *device, u64 new_size)
2272{
2273 int ret;
2274 lock_chunks(device->dev_root);
2275 ret = __btrfs_grow_device(trans, device, new_size);
2276 unlock_chunks(device->dev_root);
2277 return ret;
2278}
2279
Chris Mason8f18cf12008-04-25 16:53:30 -04002280static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2281 struct btrfs_root *root,
2282 u64 chunk_tree, u64 chunk_objectid,
2283 u64 chunk_offset)
2284{
2285 int ret;
2286 struct btrfs_path *path;
2287 struct btrfs_key key;
2288
2289 root = root->fs_info->chunk_root;
2290 path = btrfs_alloc_path();
2291 if (!path)
2292 return -ENOMEM;
2293
2294 key.objectid = chunk_objectid;
2295 key.offset = chunk_offset;
2296 key.type = BTRFS_CHUNK_ITEM_KEY;
2297
2298 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002299 if (ret < 0)
2300 goto out;
2301 else if (ret > 0) { /* Logic error or corruption */
2302 btrfs_error(root->fs_info, -ENOENT,
2303 "Failed lookup while freeing chunk.");
2304 ret = -ENOENT;
2305 goto out;
2306 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002307
2308 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002309 if (ret < 0)
2310 btrfs_error(root->fs_info, ret,
2311 "Failed to delete chunk item.");
2312out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002313 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002314 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002315}
2316
Christoph Hellwigb2950862008-12-02 09:54:17 -05002317static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002318 chunk_offset)
2319{
David Sterba6c417612011-04-13 15:41:04 +02002320 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002321 struct btrfs_disk_key *disk_key;
2322 struct btrfs_chunk *chunk;
2323 u8 *ptr;
2324 int ret = 0;
2325 u32 num_stripes;
2326 u32 array_size;
2327 u32 len = 0;
2328 u32 cur;
2329 struct btrfs_key key;
2330
2331 array_size = btrfs_super_sys_array_size(super_copy);
2332
2333 ptr = super_copy->sys_chunk_array;
2334 cur = 0;
2335
2336 while (cur < array_size) {
2337 disk_key = (struct btrfs_disk_key *)ptr;
2338 btrfs_disk_key_to_cpu(&key, disk_key);
2339
2340 len = sizeof(*disk_key);
2341
2342 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2343 chunk = (struct btrfs_chunk *)(ptr + len);
2344 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2345 len += btrfs_chunk_item_size(num_stripes);
2346 } else {
2347 ret = -EIO;
2348 break;
2349 }
2350 if (key.objectid == chunk_objectid &&
2351 key.offset == chunk_offset) {
2352 memmove(ptr, ptr + len, array_size - (cur + len));
2353 array_size -= len;
2354 btrfs_set_super_sys_array_size(super_copy, array_size);
2355 } else {
2356 ptr += len;
2357 cur += len;
2358 }
2359 }
2360 return ret;
2361}
2362
Christoph Hellwigb2950862008-12-02 09:54:17 -05002363static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002364 u64 chunk_tree, u64 chunk_objectid,
2365 u64 chunk_offset)
2366{
2367 struct extent_map_tree *em_tree;
2368 struct btrfs_root *extent_root;
2369 struct btrfs_trans_handle *trans;
2370 struct extent_map *em;
2371 struct map_lookup *map;
2372 int ret;
2373 int i;
2374
2375 root = root->fs_info->chunk_root;
2376 extent_root = root->fs_info->extent_root;
2377 em_tree = &root->fs_info->mapping_tree.map_tree;
2378
Josef Bacikba1bf482009-09-11 16:11:19 -04002379 ret = btrfs_can_relocate(extent_root, chunk_offset);
2380 if (ret)
2381 return -ENOSPC;
2382
Chris Mason8f18cf12008-04-25 16:53:30 -04002383 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002384 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002385 if (ret)
2386 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002387
Yan, Zhenga22285a2010-05-16 10:48:46 -04002388 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002389 if (IS_ERR(trans)) {
2390 ret = PTR_ERR(trans);
2391 btrfs_std_error(root->fs_info, ret);
2392 return ret;
2393 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002394
Chris Mason7d9eb122008-07-08 14:19:17 -04002395 lock_chunks(root);
2396
Chris Mason8f18cf12008-04-25 16:53:30 -04002397 /*
2398 * step two, delete the device extents and the
2399 * chunk tree entries
2400 */
Chris Mason890871b2009-09-02 16:24:52 -04002401 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002402 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002403 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002404
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002405 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002406 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002407 map = (struct map_lookup *)em->bdev;
2408
2409 for (i = 0; i < map->num_stripes; i++) {
2410 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2411 map->stripes[i].physical);
2412 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002413
Chris Masondfe25022008-05-13 13:46:40 -04002414 if (map->stripes[i].dev) {
2415 ret = btrfs_update_device(trans, map->stripes[i].dev);
2416 BUG_ON(ret);
2417 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002418 }
2419 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2420 chunk_offset);
2421
2422 BUG_ON(ret);
2423
liubo1abe9b82011-03-24 11:18:59 +00002424 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2425
Chris Mason8f18cf12008-04-25 16:53:30 -04002426 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2427 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2428 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002429 }
2430
Zheng Yan1a40e232008-09-26 10:09:34 -04002431 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2432 BUG_ON(ret);
2433
Chris Mason890871b2009-09-02 16:24:52 -04002434 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002435 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002436 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002437
Chris Mason8f18cf12008-04-25 16:53:30 -04002438 kfree(map);
2439 em->bdev = NULL;
2440
2441 /* once for the tree */
2442 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002443 /* once for us */
2444 free_extent_map(em);
2445
Chris Mason7d9eb122008-07-08 14:19:17 -04002446 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002447 btrfs_end_transaction(trans, root);
2448 return 0;
2449}
2450
Yan Zheng2b820322008-11-17 21:11:30 -05002451static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2452{
2453 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2454 struct btrfs_path *path;
2455 struct extent_buffer *leaf;
2456 struct btrfs_chunk *chunk;
2457 struct btrfs_key key;
2458 struct btrfs_key found_key;
2459 u64 chunk_tree = chunk_root->root_key.objectid;
2460 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002461 bool retried = false;
2462 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002463 int ret;
2464
2465 path = btrfs_alloc_path();
2466 if (!path)
2467 return -ENOMEM;
2468
Josef Bacikba1bf482009-09-11 16:11:19 -04002469again:
Yan Zheng2b820322008-11-17 21:11:30 -05002470 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2471 key.offset = (u64)-1;
2472 key.type = BTRFS_CHUNK_ITEM_KEY;
2473
2474 while (1) {
2475 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2476 if (ret < 0)
2477 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002478 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002479
2480 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2481 key.type);
2482 if (ret < 0)
2483 goto error;
2484 if (ret > 0)
2485 break;
2486
2487 leaf = path->nodes[0];
2488 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2489
2490 chunk = btrfs_item_ptr(leaf, path->slots[0],
2491 struct btrfs_chunk);
2492 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002493 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002494
2495 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2496 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2497 found_key.objectid,
2498 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002499 if (ret == -ENOSPC)
2500 failed++;
2501 else if (ret)
2502 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002503 }
2504
2505 if (found_key.offset == 0)
2506 break;
2507 key.offset = found_key.offset - 1;
2508 }
2509 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002510 if (failed && !retried) {
2511 failed = 0;
2512 retried = true;
2513 goto again;
2514 } else if (failed && retried) {
2515 WARN_ON(1);
2516 ret = -ENOSPC;
2517 }
Yan Zheng2b820322008-11-17 21:11:30 -05002518error:
2519 btrfs_free_path(path);
2520 return ret;
2521}
2522
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002523static int insert_balance_item(struct btrfs_root *root,
2524 struct btrfs_balance_control *bctl)
2525{
2526 struct btrfs_trans_handle *trans;
2527 struct btrfs_balance_item *item;
2528 struct btrfs_disk_balance_args disk_bargs;
2529 struct btrfs_path *path;
2530 struct extent_buffer *leaf;
2531 struct btrfs_key key;
2532 int ret, err;
2533
2534 path = btrfs_alloc_path();
2535 if (!path)
2536 return -ENOMEM;
2537
2538 trans = btrfs_start_transaction(root, 0);
2539 if (IS_ERR(trans)) {
2540 btrfs_free_path(path);
2541 return PTR_ERR(trans);
2542 }
2543
2544 key.objectid = BTRFS_BALANCE_OBJECTID;
2545 key.type = BTRFS_BALANCE_ITEM_KEY;
2546 key.offset = 0;
2547
2548 ret = btrfs_insert_empty_item(trans, root, path, &key,
2549 sizeof(*item));
2550 if (ret)
2551 goto out;
2552
2553 leaf = path->nodes[0];
2554 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2555
2556 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2557
2558 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2559 btrfs_set_balance_data(leaf, item, &disk_bargs);
2560 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2561 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2562 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2563 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2564
2565 btrfs_set_balance_flags(leaf, item, bctl->flags);
2566
2567 btrfs_mark_buffer_dirty(leaf);
2568out:
2569 btrfs_free_path(path);
2570 err = btrfs_commit_transaction(trans, root);
2571 if (err && !ret)
2572 ret = err;
2573 return ret;
2574}
2575
2576static int del_balance_item(struct btrfs_root *root)
2577{
2578 struct btrfs_trans_handle *trans;
2579 struct btrfs_path *path;
2580 struct btrfs_key key;
2581 int ret, err;
2582
2583 path = btrfs_alloc_path();
2584 if (!path)
2585 return -ENOMEM;
2586
2587 trans = btrfs_start_transaction(root, 0);
2588 if (IS_ERR(trans)) {
2589 btrfs_free_path(path);
2590 return PTR_ERR(trans);
2591 }
2592
2593 key.objectid = BTRFS_BALANCE_OBJECTID;
2594 key.type = BTRFS_BALANCE_ITEM_KEY;
2595 key.offset = 0;
2596
2597 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2598 if (ret < 0)
2599 goto out;
2600 if (ret > 0) {
2601 ret = -ENOENT;
2602 goto out;
2603 }
2604
2605 ret = btrfs_del_item(trans, root, path);
2606out:
2607 btrfs_free_path(path);
2608 err = btrfs_commit_transaction(trans, root);
2609 if (err && !ret)
2610 ret = err;
2611 return ret;
2612}
2613
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002614/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002615 * This is a heuristic used to reduce the number of chunks balanced on
2616 * resume after balance was interrupted.
2617 */
2618static void update_balance_args(struct btrfs_balance_control *bctl)
2619{
2620 /*
2621 * Turn on soft mode for chunk types that were being converted.
2622 */
2623 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2624 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2625 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2626 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2627 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2628 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2629
2630 /*
2631 * Turn on usage filter if is not already used. The idea is
2632 * that chunks that we have already balanced should be
2633 * reasonably full. Don't do it for chunks that are being
2634 * converted - that will keep us from relocating unconverted
2635 * (albeit full) chunks.
2636 */
2637 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2638 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2639 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2640 bctl->data.usage = 90;
2641 }
2642 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2643 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2644 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2645 bctl->sys.usage = 90;
2646 }
2647 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2648 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2649 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2650 bctl->meta.usage = 90;
2651 }
2652}
2653
2654/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002655 * Should be called with both balance and volume mutexes held to
2656 * serialize other volume operations (add_dev/rm_dev/resize) with
2657 * restriper. Same goes for unset_balance_control.
2658 */
2659static void set_balance_control(struct btrfs_balance_control *bctl)
2660{
2661 struct btrfs_fs_info *fs_info = bctl->fs_info;
2662
2663 BUG_ON(fs_info->balance_ctl);
2664
2665 spin_lock(&fs_info->balance_lock);
2666 fs_info->balance_ctl = bctl;
2667 spin_unlock(&fs_info->balance_lock);
2668}
2669
2670static void unset_balance_control(struct btrfs_fs_info *fs_info)
2671{
2672 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2673
2674 BUG_ON(!fs_info->balance_ctl);
2675
2676 spin_lock(&fs_info->balance_lock);
2677 fs_info->balance_ctl = NULL;
2678 spin_unlock(&fs_info->balance_lock);
2679
2680 kfree(bctl);
2681}
2682
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002683/*
2684 * Balance filters. Return 1 if chunk should be filtered out
2685 * (should not be balanced).
2686 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002687static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002688 struct btrfs_balance_args *bargs)
2689{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002690 chunk_type = chunk_to_extended(chunk_type) &
2691 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002692
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002693 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002694 return 0;
2695
2696 return 1;
2697}
2698
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002699static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2700 struct btrfs_balance_args *bargs)
2701{
2702 struct btrfs_block_group_cache *cache;
2703 u64 chunk_used, user_thresh;
2704 int ret = 1;
2705
2706 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2707 chunk_used = btrfs_block_group_used(&cache->item);
2708
Ilya Dryomova105bb82013-01-21 15:15:56 +02002709 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002710 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002711 else if (bargs->usage > 100)
2712 user_thresh = cache->key.offset;
2713 else
2714 user_thresh = div_factor_fine(cache->key.offset,
2715 bargs->usage);
2716
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002717 if (chunk_used < user_thresh)
2718 ret = 0;
2719
2720 btrfs_put_block_group(cache);
2721 return ret;
2722}
2723
Ilya Dryomov409d4042012-01-16 22:04:47 +02002724static int chunk_devid_filter(struct extent_buffer *leaf,
2725 struct btrfs_chunk *chunk,
2726 struct btrfs_balance_args *bargs)
2727{
2728 struct btrfs_stripe *stripe;
2729 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2730 int i;
2731
2732 for (i = 0; i < num_stripes; i++) {
2733 stripe = btrfs_stripe_nr(chunk, i);
2734 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2735 return 0;
2736 }
2737
2738 return 1;
2739}
2740
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002741/* [pstart, pend) */
2742static int chunk_drange_filter(struct extent_buffer *leaf,
2743 struct btrfs_chunk *chunk,
2744 u64 chunk_offset,
2745 struct btrfs_balance_args *bargs)
2746{
2747 struct btrfs_stripe *stripe;
2748 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2749 u64 stripe_offset;
2750 u64 stripe_length;
2751 int factor;
2752 int i;
2753
2754 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2755 return 0;
2756
2757 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002758 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2759 factor = num_stripes / 2;
2760 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2761 factor = num_stripes - 1;
2762 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2763 factor = num_stripes - 2;
2764 } else {
2765 factor = num_stripes;
2766 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002767
2768 for (i = 0; i < num_stripes; i++) {
2769 stripe = btrfs_stripe_nr(chunk, i);
2770 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2771 continue;
2772
2773 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2774 stripe_length = btrfs_chunk_length(leaf, chunk);
2775 do_div(stripe_length, factor);
2776
2777 if (stripe_offset < bargs->pend &&
2778 stripe_offset + stripe_length > bargs->pstart)
2779 return 0;
2780 }
2781
2782 return 1;
2783}
2784
Ilya Dryomovea671762012-01-16 22:04:48 +02002785/* [vstart, vend) */
2786static int chunk_vrange_filter(struct extent_buffer *leaf,
2787 struct btrfs_chunk *chunk,
2788 u64 chunk_offset,
2789 struct btrfs_balance_args *bargs)
2790{
2791 if (chunk_offset < bargs->vend &&
2792 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2793 /* at least part of the chunk is inside this vrange */
2794 return 0;
2795
2796 return 1;
2797}
2798
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002799static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002800 struct btrfs_balance_args *bargs)
2801{
2802 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2803 return 0;
2804
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002805 chunk_type = chunk_to_extended(chunk_type) &
2806 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002807
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002808 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002809 return 1;
2810
2811 return 0;
2812}
2813
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002814static int should_balance_chunk(struct btrfs_root *root,
2815 struct extent_buffer *leaf,
2816 struct btrfs_chunk *chunk, u64 chunk_offset)
2817{
2818 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2819 struct btrfs_balance_args *bargs = NULL;
2820 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2821
2822 /* type filter */
2823 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2824 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2825 return 0;
2826 }
2827
2828 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2829 bargs = &bctl->data;
2830 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2831 bargs = &bctl->sys;
2832 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2833 bargs = &bctl->meta;
2834
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002835 /* profiles filter */
2836 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2837 chunk_profiles_filter(chunk_type, bargs)) {
2838 return 0;
2839 }
2840
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002841 /* usage filter */
2842 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2843 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2844 return 0;
2845 }
2846
Ilya Dryomov409d4042012-01-16 22:04:47 +02002847 /* devid filter */
2848 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2849 chunk_devid_filter(leaf, chunk, bargs)) {
2850 return 0;
2851 }
2852
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002853 /* drange filter, makes sense only with devid filter */
2854 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2855 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2856 return 0;
2857 }
2858
Ilya Dryomovea671762012-01-16 22:04:48 +02002859 /* vrange filter */
2860 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2861 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2862 return 0;
2863 }
2864
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002865 /* soft profile changing mode */
2866 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2867 chunk_soft_convert_filter(chunk_type, bargs)) {
2868 return 0;
2869 }
2870
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002871 return 1;
2872}
2873
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002874static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002875{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002876 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002877 struct btrfs_root *chunk_root = fs_info->chunk_root;
2878 struct btrfs_root *dev_root = fs_info->dev_root;
2879 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002880 struct btrfs_device *device;
2881 u64 old_size;
2882 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002883 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002884 struct btrfs_path *path;
2885 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002886 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002887 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002888 struct extent_buffer *leaf;
2889 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002890 int ret;
2891 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002892 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002893
Chris Masonec44a352008-04-28 15:29:52 -04002894 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002895 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002896 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002897 old_size = device->total_bytes;
2898 size_to_free = div_factor(old_size, 1);
2899 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002900 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01002901 device->total_bytes - device->bytes_used > size_to_free ||
2902 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04002903 continue;
2904
2905 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002906 if (ret == -ENOSPC)
2907 break;
Chris Masonec44a352008-04-28 15:29:52 -04002908 BUG_ON(ret);
2909
Yan, Zhenga22285a2010-05-16 10:48:46 -04002910 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002911 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002912
2913 ret = btrfs_grow_device(trans, device, old_size);
2914 BUG_ON(ret);
2915
2916 btrfs_end_transaction(trans, dev_root);
2917 }
2918
2919 /* step two, relocate all the chunks */
2920 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002921 if (!path) {
2922 ret = -ENOMEM;
2923 goto error;
2924 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002925
2926 /* zero out stat counters */
2927 spin_lock(&fs_info->balance_lock);
2928 memset(&bctl->stat, 0, sizeof(bctl->stat));
2929 spin_unlock(&fs_info->balance_lock);
2930again:
Chris Masonec44a352008-04-28 15:29:52 -04002931 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2932 key.offset = (u64)-1;
2933 key.type = BTRFS_CHUNK_ITEM_KEY;
2934
Chris Masond3977122009-01-05 21:25:51 -05002935 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002936 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002937 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002938 ret = -ECANCELED;
2939 goto error;
2940 }
2941
Chris Masonec44a352008-04-28 15:29:52 -04002942 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2943 if (ret < 0)
2944 goto error;
2945
2946 /*
2947 * this shouldn't happen, it means the last relocate
2948 * failed
2949 */
2950 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002951 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002952
2953 ret = btrfs_previous_item(chunk_root, path, 0,
2954 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002955 if (ret) {
2956 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002957 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002958 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002959
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002960 leaf = path->nodes[0];
2961 slot = path->slots[0];
2962 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2963
Chris Masonec44a352008-04-28 15:29:52 -04002964 if (found_key.objectid != key.objectid)
2965 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002966
Chris Masonec44a352008-04-28 15:29:52 -04002967 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002968 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002969 break;
2970
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002971 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2972
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002973 if (!counting) {
2974 spin_lock(&fs_info->balance_lock);
2975 bctl->stat.considered++;
2976 spin_unlock(&fs_info->balance_lock);
2977 }
2978
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002979 ret = should_balance_chunk(chunk_root, leaf, chunk,
2980 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002981 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002982 if (!ret)
2983 goto loop;
2984
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002985 if (counting) {
2986 spin_lock(&fs_info->balance_lock);
2987 bctl->stat.expected++;
2988 spin_unlock(&fs_info->balance_lock);
2989 goto loop;
2990 }
2991
Chris Masonec44a352008-04-28 15:29:52 -04002992 ret = btrfs_relocate_chunk(chunk_root,
2993 chunk_root->root_key.objectid,
2994 found_key.objectid,
2995 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002996 if (ret && ret != -ENOSPC)
2997 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002998 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002999 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003000 } else {
3001 spin_lock(&fs_info->balance_lock);
3002 bctl->stat.completed++;
3003 spin_unlock(&fs_info->balance_lock);
3004 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003005loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04003006 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003007 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003008
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003009 if (counting) {
3010 btrfs_release_path(path);
3011 counting = false;
3012 goto again;
3013 }
Chris Masonec44a352008-04-28 15:29:52 -04003014error:
3015 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003016 if (enospc_errors) {
3017 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
3018 enospc_errors);
3019 if (!ret)
3020 ret = -ENOSPC;
3021 }
3022
Chris Masonec44a352008-04-28 15:29:52 -04003023 return ret;
3024}
3025
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003026/**
3027 * alloc_profile_is_valid - see if a given profile is valid and reduced
3028 * @flags: profile to validate
3029 * @extended: if true @flags is treated as an extended profile
3030 */
3031static int alloc_profile_is_valid(u64 flags, int extended)
3032{
3033 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3034 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3035
3036 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3037
3038 /* 1) check that all other bits are zeroed */
3039 if (flags & ~mask)
3040 return 0;
3041
3042 /* 2) see if profile is reduced */
3043 if (flags == 0)
3044 return !extended; /* "0" is valid for usual profiles */
3045
3046 /* true if exactly one bit set */
3047 return (flags & (flags - 1)) == 0;
3048}
3049
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003050static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3051{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003052 /* cancel requested || normal exit path */
3053 return atomic_read(&fs_info->balance_cancel_req) ||
3054 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3055 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003056}
3057
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003058static void __cancel_balance(struct btrfs_fs_info *fs_info)
3059{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003060 int ret;
3061
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003062 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003063 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003064 if (ret)
3065 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003066
3067 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003068}
3069
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003070void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003071 struct btrfs_ioctl_balance_args *bargs);
3072
3073/*
3074 * Should be called with both balance and volume mutexes held
3075 */
3076int btrfs_balance(struct btrfs_balance_control *bctl,
3077 struct btrfs_ioctl_balance_args *bargs)
3078{
3079 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003080 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003081 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003082 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003083 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003084 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003085
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003086 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003087 atomic_read(&fs_info->balance_pause_req) ||
3088 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003089 ret = -EINVAL;
3090 goto out;
3091 }
3092
Ilya Dryomove4837f82012-03-27 17:09:17 +03003093 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3094 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3095 mixed = 1;
3096
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003097 /*
3098 * In case of mixed groups both data and meta should be picked,
3099 * and identical options should be given for both of them.
3100 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003101 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3102 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003103 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3104 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3105 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3106 printk(KERN_ERR "btrfs: with mixed groups data and "
3107 "metadata balance options must be the same\n");
3108 ret = -EINVAL;
3109 goto out;
3110 }
3111 }
3112
Stefan Behrens8dabb742012-11-06 13:15:27 +01003113 num_devices = fs_info->fs_devices->num_devices;
3114 btrfs_dev_replace_lock(&fs_info->dev_replace);
3115 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3116 BUG_ON(num_devices < 1);
3117 num_devices--;
3118 }
3119 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003120 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003121 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003122 allowed |= BTRFS_BLOCK_GROUP_DUP;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003123 else if (num_devices < 4)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003124 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
3125 else
3126 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003127 BTRFS_BLOCK_GROUP_RAID10 |
3128 BTRFS_BLOCK_GROUP_RAID5 |
3129 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003130
Ilya Dryomov6728b192012-03-27 17:09:17 +03003131 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3132 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3133 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003134 printk(KERN_ERR "btrfs: unable to start balance with target "
3135 "data profile %llu\n",
3136 (unsigned long long)bctl->data.target);
3137 ret = -EINVAL;
3138 goto out;
3139 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003140 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3141 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3142 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003143 printk(KERN_ERR "btrfs: unable to start balance with target "
3144 "metadata profile %llu\n",
3145 (unsigned long long)bctl->meta.target);
3146 ret = -EINVAL;
3147 goto out;
3148 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003149 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3150 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3151 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003152 printk(KERN_ERR "btrfs: unable to start balance with target "
3153 "system profile %llu\n",
3154 (unsigned long long)bctl->sys.target);
3155 ret = -EINVAL;
3156 goto out;
3157 }
3158
Ilya Dryomove4837f82012-03-27 17:09:17 +03003159 /* allow dup'ed data chunks only in mixed mode */
3160 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003161 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003162 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3163 ret = -EINVAL;
3164 goto out;
3165 }
3166
3167 /* allow to reduce meta or sys integrity only if force set */
3168 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003169 BTRFS_BLOCK_GROUP_RAID10 |
3170 BTRFS_BLOCK_GROUP_RAID5 |
3171 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003172 do {
3173 seq = read_seqbegin(&fs_info->profiles_lock);
3174
3175 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3176 (fs_info->avail_system_alloc_bits & allowed) &&
3177 !(bctl->sys.target & allowed)) ||
3178 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3179 (fs_info->avail_metadata_alloc_bits & allowed) &&
3180 !(bctl->meta.target & allowed))) {
3181 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3182 printk(KERN_INFO "btrfs: force reducing metadata "
3183 "integrity\n");
3184 } else {
3185 printk(KERN_ERR "btrfs: balance will reduce metadata "
3186 "integrity, use force if you want this\n");
3187 ret = -EINVAL;
3188 goto out;
3189 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003190 }
Miao Xiede98ced2013-01-29 10:13:12 +00003191 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003192
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003193 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3194 int num_tolerated_disk_barrier_failures;
3195 u64 target = bctl->sys.target;
3196
3197 num_tolerated_disk_barrier_failures =
3198 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3199 if (num_tolerated_disk_barrier_failures > 0 &&
3200 (target &
3201 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3202 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3203 num_tolerated_disk_barrier_failures = 0;
3204 else if (num_tolerated_disk_barrier_failures > 1 &&
3205 (target &
3206 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3207 num_tolerated_disk_barrier_failures = 1;
3208
3209 fs_info->num_tolerated_disk_barrier_failures =
3210 num_tolerated_disk_barrier_failures;
3211 }
3212
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003213 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003214 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003215 goto out;
3216
Ilya Dryomov59641012012-01-16 22:04:48 +02003217 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3218 BUG_ON(ret == -EEXIST);
3219 set_balance_control(bctl);
3220 } else {
3221 BUG_ON(ret != -EEXIST);
3222 spin_lock(&fs_info->balance_lock);
3223 update_balance_args(bctl);
3224 spin_unlock(&fs_info->balance_lock);
3225 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003226
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003227 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003228 mutex_unlock(&fs_info->balance_mutex);
3229
3230 ret = __btrfs_balance(fs_info);
3231
3232 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003233 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003234
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003235 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3236 fs_info->num_tolerated_disk_barrier_failures =
3237 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3238 }
3239
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003240 if (bargs) {
3241 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003242 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003243 }
3244
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003245 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3246 balance_need_close(fs_info)) {
3247 __cancel_balance(fs_info);
3248 }
3249
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003250 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003251
3252 return ret;
3253out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003254 if (bctl->flags & BTRFS_BALANCE_RESUME)
3255 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003256 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003257 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003258 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3259 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003260 return ret;
3261}
3262
3263static int balance_kthread(void *data)
3264{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003265 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003266 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003267
3268 mutex_lock(&fs_info->volume_mutex);
3269 mutex_lock(&fs_info->balance_mutex);
3270
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003271 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003272 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003273 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003274 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003275
3276 mutex_unlock(&fs_info->balance_mutex);
3277 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003278
Ilya Dryomov59641012012-01-16 22:04:48 +02003279 return ret;
3280}
3281
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003282int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3283{
3284 struct task_struct *tsk;
3285
3286 spin_lock(&fs_info->balance_lock);
3287 if (!fs_info->balance_ctl) {
3288 spin_unlock(&fs_info->balance_lock);
3289 return 0;
3290 }
3291 spin_unlock(&fs_info->balance_lock);
3292
3293 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3294 printk(KERN_INFO "btrfs: force skipping balance\n");
3295 return 0;
3296 }
3297
3298 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3299 if (IS_ERR(tsk))
3300 return PTR_ERR(tsk);
3301
3302 return 0;
3303}
3304
Ilya Dryomov68310a52012-06-22 12:24:12 -06003305int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003306{
Ilya Dryomov59641012012-01-16 22:04:48 +02003307 struct btrfs_balance_control *bctl;
3308 struct btrfs_balance_item *item;
3309 struct btrfs_disk_balance_args disk_bargs;
3310 struct btrfs_path *path;
3311 struct extent_buffer *leaf;
3312 struct btrfs_key key;
3313 int ret;
3314
3315 path = btrfs_alloc_path();
3316 if (!path)
3317 return -ENOMEM;
3318
Ilya Dryomov68310a52012-06-22 12:24:12 -06003319 key.objectid = BTRFS_BALANCE_OBJECTID;
3320 key.type = BTRFS_BALANCE_ITEM_KEY;
3321 key.offset = 0;
3322
3323 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3324 if (ret < 0)
3325 goto out;
3326 if (ret > 0) { /* ret = -ENOENT; */
3327 ret = 0;
3328 goto out;
3329 }
3330
Ilya Dryomov59641012012-01-16 22:04:48 +02003331 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3332 if (!bctl) {
3333 ret = -ENOMEM;
3334 goto out;
3335 }
3336
Ilya Dryomov59641012012-01-16 22:04:48 +02003337 leaf = path->nodes[0];
3338 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3339
Ilya Dryomov68310a52012-06-22 12:24:12 -06003340 bctl->fs_info = fs_info;
3341 bctl->flags = btrfs_balance_flags(leaf, item);
3342 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003343
3344 btrfs_balance_data(leaf, item, &disk_bargs);
3345 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3346 btrfs_balance_meta(leaf, item, &disk_bargs);
3347 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3348 btrfs_balance_sys(leaf, item, &disk_bargs);
3349 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3350
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003351 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3352
Ilya Dryomov68310a52012-06-22 12:24:12 -06003353 mutex_lock(&fs_info->volume_mutex);
3354 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003355
Ilya Dryomov68310a52012-06-22 12:24:12 -06003356 set_balance_control(bctl);
3357
3358 mutex_unlock(&fs_info->balance_mutex);
3359 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003360out:
3361 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003362 return ret;
3363}
3364
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003365int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3366{
3367 int ret = 0;
3368
3369 mutex_lock(&fs_info->balance_mutex);
3370 if (!fs_info->balance_ctl) {
3371 mutex_unlock(&fs_info->balance_mutex);
3372 return -ENOTCONN;
3373 }
3374
3375 if (atomic_read(&fs_info->balance_running)) {
3376 atomic_inc(&fs_info->balance_pause_req);
3377 mutex_unlock(&fs_info->balance_mutex);
3378
3379 wait_event(fs_info->balance_wait_q,
3380 atomic_read(&fs_info->balance_running) == 0);
3381
3382 mutex_lock(&fs_info->balance_mutex);
3383 /* we are good with balance_ctl ripped off from under us */
3384 BUG_ON(atomic_read(&fs_info->balance_running));
3385 atomic_dec(&fs_info->balance_pause_req);
3386 } else {
3387 ret = -ENOTCONN;
3388 }
3389
3390 mutex_unlock(&fs_info->balance_mutex);
3391 return ret;
3392}
3393
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003394int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3395{
3396 mutex_lock(&fs_info->balance_mutex);
3397 if (!fs_info->balance_ctl) {
3398 mutex_unlock(&fs_info->balance_mutex);
3399 return -ENOTCONN;
3400 }
3401
3402 atomic_inc(&fs_info->balance_cancel_req);
3403 /*
3404 * if we are running just wait and return, balance item is
3405 * deleted in btrfs_balance in this case
3406 */
3407 if (atomic_read(&fs_info->balance_running)) {
3408 mutex_unlock(&fs_info->balance_mutex);
3409 wait_event(fs_info->balance_wait_q,
3410 atomic_read(&fs_info->balance_running) == 0);
3411 mutex_lock(&fs_info->balance_mutex);
3412 } else {
3413 /* __cancel_balance needs volume_mutex */
3414 mutex_unlock(&fs_info->balance_mutex);
3415 mutex_lock(&fs_info->volume_mutex);
3416 mutex_lock(&fs_info->balance_mutex);
3417
3418 if (fs_info->balance_ctl)
3419 __cancel_balance(fs_info);
3420
3421 mutex_unlock(&fs_info->volume_mutex);
3422 }
3423
3424 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3425 atomic_dec(&fs_info->balance_cancel_req);
3426 mutex_unlock(&fs_info->balance_mutex);
3427 return 0;
3428}
3429
Chris Mason8f18cf12008-04-25 16:53:30 -04003430/*
3431 * shrinking a device means finding all of the device extents past
3432 * the new size, and then following the back refs to the chunks.
3433 * The chunk relocation code actually frees the device extent
3434 */
3435int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3436{
3437 struct btrfs_trans_handle *trans;
3438 struct btrfs_root *root = device->dev_root;
3439 struct btrfs_dev_extent *dev_extent = NULL;
3440 struct btrfs_path *path;
3441 u64 length;
3442 u64 chunk_tree;
3443 u64 chunk_objectid;
3444 u64 chunk_offset;
3445 int ret;
3446 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003447 int failed = 0;
3448 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003449 struct extent_buffer *l;
3450 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003451 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003452 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003453 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003454 u64 diff = device->total_bytes - new_size;
3455
Stefan Behrens63a212a2012-11-05 18:29:28 +01003456 if (device->is_tgtdev_for_dev_replace)
3457 return -EINVAL;
3458
Chris Mason8f18cf12008-04-25 16:53:30 -04003459 path = btrfs_alloc_path();
3460 if (!path)
3461 return -ENOMEM;
3462
Chris Mason8f18cf12008-04-25 16:53:30 -04003463 path->reada = 2;
3464
Chris Mason7d9eb122008-07-08 14:19:17 -04003465 lock_chunks(root);
3466
Chris Mason8f18cf12008-04-25 16:53:30 -04003467 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003468 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003469 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003470 spin_lock(&root->fs_info->free_chunk_lock);
3471 root->fs_info->free_chunk_space -= diff;
3472 spin_unlock(&root->fs_info->free_chunk_lock);
3473 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003474 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003475
Josef Bacikba1bf482009-09-11 16:11:19 -04003476again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003477 key.objectid = device->devid;
3478 key.offset = (u64)-1;
3479 key.type = BTRFS_DEV_EXTENT_KEY;
3480
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003481 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003482 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3483 if (ret < 0)
3484 goto done;
3485
3486 ret = btrfs_previous_item(root, path, 0, key.type);
3487 if (ret < 0)
3488 goto done;
3489 if (ret) {
3490 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003491 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003492 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003493 }
3494
3495 l = path->nodes[0];
3496 slot = path->slots[0];
3497 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3498
Josef Bacikba1bf482009-09-11 16:11:19 -04003499 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003500 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003501 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003502 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003503
3504 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3505 length = btrfs_dev_extent_length(l, dev_extent);
3506
Josef Bacikba1bf482009-09-11 16:11:19 -04003507 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003508 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003509 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003510 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003511
3512 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3513 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3514 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003515 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003516
3517 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3518 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003519 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003520 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003521 if (ret == -ENOSPC)
3522 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003523 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003524
3525 if (failed && !retried) {
3526 failed = 0;
3527 retried = true;
3528 goto again;
3529 } else if (failed && retried) {
3530 ret = -ENOSPC;
3531 lock_chunks(root);
3532
3533 device->total_bytes = old_size;
3534 if (device->writeable)
3535 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003536 spin_lock(&root->fs_info->free_chunk_lock);
3537 root->fs_info->free_chunk_space += diff;
3538 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003539 unlock_chunks(root);
3540 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003541 }
3542
Chris Balld6397ba2009-04-27 07:29:03 -04003543 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003544 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003545 if (IS_ERR(trans)) {
3546 ret = PTR_ERR(trans);
3547 goto done;
3548 }
3549
Chris Balld6397ba2009-04-27 07:29:03 -04003550 lock_chunks(root);
3551
3552 device->disk_total_bytes = new_size;
3553 /* Now btrfs_update_device() will change the on-disk size. */
3554 ret = btrfs_update_device(trans, device);
3555 if (ret) {
3556 unlock_chunks(root);
3557 btrfs_end_transaction(trans, root);
3558 goto done;
3559 }
3560 WARN_ON(diff > old_total);
3561 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3562 unlock_chunks(root);
3563 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003564done:
3565 btrfs_free_path(path);
3566 return ret;
3567}
3568
Li Zefan125ccb02011-12-08 15:07:24 +08003569static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003570 struct btrfs_key *key,
3571 struct btrfs_chunk *chunk, int item_size)
3572{
David Sterba6c417612011-04-13 15:41:04 +02003573 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003574 struct btrfs_disk_key disk_key;
3575 u32 array_size;
3576 u8 *ptr;
3577
3578 array_size = btrfs_super_sys_array_size(super_copy);
3579 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3580 return -EFBIG;
3581
3582 ptr = super_copy->sys_chunk_array + array_size;
3583 btrfs_cpu_key_to_disk(&disk_key, key);
3584 memcpy(ptr, &disk_key, sizeof(disk_key));
3585 ptr += sizeof(disk_key);
3586 memcpy(ptr, chunk, item_size);
3587 item_size += sizeof(disk_key);
3588 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3589 return 0;
3590}
3591
Miao Xieb2117a32011-01-05 10:07:28 +00003592/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003593 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003594 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003595static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003596{
Arne Jansen73c5de02011-04-12 12:07:57 +02003597 const struct btrfs_device_info *di_a = a;
3598 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003599
Arne Jansen73c5de02011-04-12 12:07:57 +02003600 if (di_a->max_avail > di_b->max_avail)
3601 return -1;
3602 if (di_a->max_avail < di_b->max_avail)
3603 return 1;
3604 if (di_a->total_avail > di_b->total_avail)
3605 return -1;
3606 if (di_a->total_avail < di_b->total_avail)
3607 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003608 return 0;
3609}
3610
Eric Sandeen48a3b632013-04-25 20:41:01 +00003611static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00003612 [BTRFS_RAID_RAID10] = {
3613 .sub_stripes = 2,
3614 .dev_stripes = 1,
3615 .devs_max = 0, /* 0 == as many as possible */
3616 .devs_min = 4,
3617 .devs_increment = 2,
3618 .ncopies = 2,
3619 },
3620 [BTRFS_RAID_RAID1] = {
3621 .sub_stripes = 1,
3622 .dev_stripes = 1,
3623 .devs_max = 2,
3624 .devs_min = 2,
3625 .devs_increment = 2,
3626 .ncopies = 2,
3627 },
3628 [BTRFS_RAID_DUP] = {
3629 .sub_stripes = 1,
3630 .dev_stripes = 2,
3631 .devs_max = 1,
3632 .devs_min = 1,
3633 .devs_increment = 1,
3634 .ncopies = 2,
3635 },
3636 [BTRFS_RAID_RAID0] = {
3637 .sub_stripes = 1,
3638 .dev_stripes = 1,
3639 .devs_max = 0,
3640 .devs_min = 2,
3641 .devs_increment = 1,
3642 .ncopies = 1,
3643 },
3644 [BTRFS_RAID_SINGLE] = {
3645 .sub_stripes = 1,
3646 .dev_stripes = 1,
3647 .devs_max = 1,
3648 .devs_min = 1,
3649 .devs_increment = 1,
3650 .ncopies = 1,
3651 },
Chris Masone942f882013-02-20 14:06:05 -05003652 [BTRFS_RAID_RAID5] = {
3653 .sub_stripes = 1,
3654 .dev_stripes = 1,
3655 .devs_max = 0,
3656 .devs_min = 2,
3657 .devs_increment = 1,
3658 .ncopies = 2,
3659 },
3660 [BTRFS_RAID_RAID6] = {
3661 .sub_stripes = 1,
3662 .dev_stripes = 1,
3663 .devs_max = 0,
3664 .devs_min = 3,
3665 .devs_increment = 1,
3666 .ncopies = 3,
3667 },
Liu Bo31e50222012-11-21 14:18:10 +00003668};
3669
David Woodhouse53b381b2013-01-29 18:40:14 -05003670static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
3671{
3672 /* TODO allow them to set a preferred stripe size */
3673 return 64 * 1024;
3674}
3675
3676static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
3677{
David Woodhouse53b381b2013-01-29 18:40:14 -05003678 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
3679 return;
3680
Miao Xieceda0862013-04-11 10:30:16 +00003681 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05003682}
3683
Miao Xieb2117a32011-01-05 10:07:28 +00003684static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3685 struct btrfs_root *extent_root,
3686 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003687 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003688 u64 start, u64 type)
3689{
3690 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003691 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3692 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003693 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003694 struct extent_map_tree *em_tree;
3695 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003696 struct btrfs_device_info *devices_info = NULL;
3697 u64 total_avail;
3698 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05003699 int data_stripes; /* number of stripes that count for
3700 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02003701 int sub_stripes; /* sub_stripes info for map */
3702 int dev_stripes; /* stripes per dev */
3703 int devs_max; /* max devs to use */
3704 int devs_min; /* min devs needed */
3705 int devs_increment; /* ndevs has to be a multiple of this */
3706 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003707 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003708 u64 max_stripe_size;
3709 u64 max_chunk_size;
3710 u64 stripe_size;
3711 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05003712 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02003713 int ndevs;
3714 int i;
3715 int j;
Liu Bo31e50222012-11-21 14:18:10 +00003716 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00003717
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003718 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003719
Miao Xieb2117a32011-01-05 10:07:28 +00003720 if (list_empty(&fs_devices->alloc_list))
3721 return -ENOSPC;
3722
Liu Bo31e50222012-11-21 14:18:10 +00003723 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02003724
Liu Bo31e50222012-11-21 14:18:10 +00003725 sub_stripes = btrfs_raid_array[index].sub_stripes;
3726 dev_stripes = btrfs_raid_array[index].dev_stripes;
3727 devs_max = btrfs_raid_array[index].devs_max;
3728 devs_min = btrfs_raid_array[index].devs_min;
3729 devs_increment = btrfs_raid_array[index].devs_increment;
3730 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02003731
3732 if (type & BTRFS_BLOCK_GROUP_DATA) {
3733 max_stripe_size = 1024 * 1024 * 1024;
3734 max_chunk_size = 10 * max_stripe_size;
3735 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003736 /* for larger filesystems, use larger metadata chunks */
3737 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3738 max_stripe_size = 1024 * 1024 * 1024;
3739 else
3740 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003741 max_chunk_size = max_stripe_size;
3742 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003743 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003744 max_chunk_size = 2 * max_stripe_size;
3745 } else {
3746 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3747 type);
3748 BUG_ON(1);
3749 }
3750
3751 /* we don't want a chunk larger than 10% of writeable space */
3752 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3753 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003754
3755 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3756 GFP_NOFS);
3757 if (!devices_info)
3758 return -ENOMEM;
3759
Arne Jansen73c5de02011-04-12 12:07:57 +02003760 cur = fs_devices->alloc_list.next;
3761
3762 /*
3763 * in the first pass through the devices list, we gather information
3764 * about the available holes on each device.
3765 */
3766 ndevs = 0;
3767 while (cur != &fs_devices->alloc_list) {
3768 struct btrfs_device *device;
3769 u64 max_avail;
3770 u64 dev_offset;
3771
3772 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3773
3774 cur = cur->next;
3775
3776 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003777 WARN(1, KERN_ERR
Arne Jansen73c5de02011-04-12 12:07:57 +02003778 "btrfs: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02003779 continue;
3780 }
3781
Stefan Behrens63a212a2012-11-05 18:29:28 +01003782 if (!device->in_fs_metadata ||
3783 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02003784 continue;
3785
3786 if (device->total_bytes > device->bytes_used)
3787 total_avail = device->total_bytes - device->bytes_used;
3788 else
3789 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003790
3791 /* If there is no space on this device, skip it. */
3792 if (total_avail == 0)
3793 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003794
Li Zefan125ccb02011-12-08 15:07:24 +08003795 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003796 max_stripe_size * dev_stripes,
3797 &dev_offset, &max_avail);
3798 if (ret && ret != -ENOSPC)
3799 goto error;
3800
3801 if (ret == 0)
3802 max_avail = max_stripe_size * dev_stripes;
3803
3804 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3805 continue;
3806
Eric Sandeen063d0062013-01-31 00:55:01 +00003807 if (ndevs == fs_devices->rw_devices) {
3808 WARN(1, "%s: found more than %llu devices\n",
3809 __func__, fs_devices->rw_devices);
3810 break;
3811 }
Arne Jansen73c5de02011-04-12 12:07:57 +02003812 devices_info[ndevs].dev_offset = dev_offset;
3813 devices_info[ndevs].max_avail = max_avail;
3814 devices_info[ndevs].total_avail = total_avail;
3815 devices_info[ndevs].dev = device;
3816 ++ndevs;
3817 }
3818
3819 /*
3820 * now sort the devices by hole size / available space
3821 */
3822 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3823 btrfs_cmp_device_info, NULL);
3824
3825 /* round down to number of usable stripes */
3826 ndevs -= ndevs % devs_increment;
3827
3828 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3829 ret = -ENOSPC;
3830 goto error;
3831 }
3832
3833 if (devs_max && ndevs > devs_max)
3834 ndevs = devs_max;
3835 /*
3836 * the primary goal is to maximize the number of stripes, so use as many
3837 * devices as possible, even if the stripes are not maximum sized.
3838 */
3839 stripe_size = devices_info[ndevs-1].max_avail;
3840 num_stripes = ndevs * dev_stripes;
3841
David Woodhouse53b381b2013-01-29 18:40:14 -05003842 /*
3843 * this will have to be fixed for RAID1 and RAID10 over
3844 * more drives
3845 */
3846 data_stripes = num_stripes / ncopies;
3847
David Woodhouse53b381b2013-01-29 18:40:14 -05003848 if (type & BTRFS_BLOCK_GROUP_RAID5) {
3849 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
3850 btrfs_super_stripesize(info->super_copy));
3851 data_stripes = num_stripes - 1;
3852 }
3853 if (type & BTRFS_BLOCK_GROUP_RAID6) {
3854 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
3855 btrfs_super_stripesize(info->super_copy));
3856 data_stripes = num_stripes - 2;
3857 }
Chris Mason86db2572013-02-20 16:23:40 -05003858
3859 /*
3860 * Use the number of data stripes to figure out how big this chunk
3861 * is really going to be in terms of logical address space,
3862 * and compare that answer with the max chunk size
3863 */
3864 if (stripe_size * data_stripes > max_chunk_size) {
3865 u64 mask = (1ULL << 24) - 1;
3866 stripe_size = max_chunk_size;
3867 do_div(stripe_size, data_stripes);
3868
3869 /* bump the answer up to a 16MB boundary */
3870 stripe_size = (stripe_size + mask) & ~mask;
3871
3872 /* but don't go higher than the limits we found
3873 * while searching for free extents
3874 */
3875 if (stripe_size > devices_info[ndevs-1].max_avail)
3876 stripe_size = devices_info[ndevs-1].max_avail;
3877 }
3878
Arne Jansen73c5de02011-04-12 12:07:57 +02003879 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003880
3881 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05003882 do_div(stripe_size, raid_stripe_len);
3883 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02003884
Miao Xieb2117a32011-01-05 10:07:28 +00003885 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3886 if (!map) {
3887 ret = -ENOMEM;
3888 goto error;
3889 }
3890 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003891
Arne Jansen73c5de02011-04-12 12:07:57 +02003892 for (i = 0; i < ndevs; ++i) {
3893 for (j = 0; j < dev_stripes; ++j) {
3894 int s = i * dev_stripes + j;
3895 map->stripes[s].dev = devices_info[i].dev;
3896 map->stripes[s].physical = devices_info[i].dev_offset +
3897 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003898 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003899 }
Chris Mason593060d2008-03-25 16:50:33 -04003900 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05003901 map->stripe_len = raid_stripe_len;
3902 map->io_align = raid_stripe_len;
3903 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04003904 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003905 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003906
Yan Zheng2b820322008-11-17 21:11:30 -05003907 *map_ret = map;
David Woodhouse53b381b2013-01-29 18:40:14 -05003908 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003909
Arne Jansen73c5de02011-04-12 12:07:57 +02003910 *stripe_size_out = stripe_size;
3911 *num_bytes_out = num_bytes;
3912
3913 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003914
David Sterba172ddd62011-04-21 00:48:27 +02003915 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003916 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003917 ret = -ENOMEM;
3918 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003919 }
Chris Mason0b86a832008-03-24 15:01:56 -04003920 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003921 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003922 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003923 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003924 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003925
Chris Mason0b86a832008-03-24 15:01:56 -04003926 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003927 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003928 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04003929 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003930 if (ret) {
3931 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003932 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003933 }
Yan Zheng2b820322008-11-17 21:11:30 -05003934
Arne Jansen73c5de02011-04-12 12:07:57 +02003935 for (i = 0; i < map->num_stripes; ++i) {
3936 struct btrfs_device *device;
3937 u64 dev_offset;
3938
3939 device = map->stripes[i].dev;
3940 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003941
3942 ret = btrfs_alloc_dev_extent(trans, device,
3943 info->chunk_root->root_key.objectid,
3944 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003945 start, dev_offset, stripe_size);
Josef Bacik04487482013-01-29 15:03:37 -05003946 if (ret)
3947 goto error_dev_extent;
3948 }
3949
3950 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3951 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3952 start, num_bytes);
3953 if (ret) {
3954 i = map->num_stripes - 1;
3955 goto error_dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05003956 }
3957
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003958 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05003959 check_raid56_incompat_flag(extent_root->fs_info, type);
3960
Miao Xieb2117a32011-01-05 10:07:28 +00003961 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003962 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003963
Josef Bacik04487482013-01-29 15:03:37 -05003964error_dev_extent:
3965 for (; i >= 0; i--) {
3966 struct btrfs_device *device;
3967 int err;
3968
3969 device = map->stripes[i].dev;
3970 err = btrfs_free_dev_extent(trans, device, start);
3971 if (err) {
3972 btrfs_abort_transaction(trans, extent_root, err);
3973 break;
3974 }
3975 }
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003976 write_lock(&em_tree->lock);
3977 remove_extent_mapping(em_tree, em);
3978 write_unlock(&em_tree->lock);
3979
3980 /* One for our allocation */
3981 free_extent_map(em);
3982 /* One for the tree reference */
3983 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00003984error:
3985 kfree(map);
3986 kfree(devices_info);
3987 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003988}
3989
3990static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3991 struct btrfs_root *extent_root,
3992 struct map_lookup *map, u64 chunk_offset,
3993 u64 chunk_size, u64 stripe_size)
3994{
3995 u64 dev_offset;
3996 struct btrfs_key key;
3997 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3998 struct btrfs_device *device;
3999 struct btrfs_chunk *chunk;
4000 struct btrfs_stripe *stripe;
4001 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
4002 int index = 0;
4003 int ret;
4004
4005 chunk = kzalloc(item_size, GFP_NOFS);
4006 if (!chunk)
4007 return -ENOMEM;
4008
4009 index = 0;
4010 while (index < map->num_stripes) {
4011 device = map->stripes[index].dev;
4012 device->bytes_used += stripe_size;
4013 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004014 if (ret)
4015 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05004016 index++;
4017 }
4018
Josef Bacik2bf64752011-09-26 17:12:22 -04004019 spin_lock(&extent_root->fs_info->free_chunk_lock);
4020 extent_root->fs_info->free_chunk_space -= (stripe_size *
4021 map->num_stripes);
4022 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4023
Yan Zheng2b820322008-11-17 21:11:30 -05004024 index = 0;
4025 stripe = &chunk->stripe;
4026 while (index < map->num_stripes) {
4027 device = map->stripes[index].dev;
4028 dev_offset = map->stripes[index].physical;
4029
4030 btrfs_set_stack_stripe_devid(stripe, device->devid);
4031 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4032 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4033 stripe++;
4034 index++;
4035 }
4036
4037 btrfs_set_stack_chunk_length(chunk, chunk_size);
4038 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4039 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4040 btrfs_set_stack_chunk_type(chunk, map->type);
4041 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4042 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4043 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4044 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4045 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4046
4047 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4048 key.type = BTRFS_CHUNK_ITEM_KEY;
4049 key.offset = chunk_offset;
4050
4051 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004052
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004053 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4054 /*
4055 * TODO: Cleanup of inserted chunk root in case of
4056 * failure.
4057 */
Li Zefan125ccb02011-12-08 15:07:24 +08004058 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004059 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004060 }
liubo1abe9b82011-03-24 11:18:59 +00004061
Mark Fasheh3acd3952011-09-08 17:40:01 -07004062out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05004063 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004064 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004065}
4066
4067/*
4068 * Chunk allocation falls into two parts. The first part does works
4069 * that make the new allocated chunk useable, but not do any operation
4070 * that modifies the chunk tree. The second part does the works that
4071 * require modifying the chunk tree. This division is important for the
4072 * bootstrap process of adding storage to a seed btrfs.
4073 */
4074int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4075 struct btrfs_root *extent_root, u64 type)
4076{
4077 u64 chunk_offset;
4078 u64 chunk_size;
4079 u64 stripe_size;
4080 struct map_lookup *map;
4081 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4082 int ret;
4083
4084 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4085 &chunk_offset);
4086 if (ret)
4087 return ret;
4088
4089 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4090 &stripe_size, chunk_offset, type);
4091 if (ret)
4092 return ret;
4093
4094 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4095 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004096 if (ret)
4097 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004098 return 0;
4099}
4100
Chris Masond3977122009-01-05 21:25:51 -05004101static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004102 struct btrfs_root *root,
4103 struct btrfs_device *device)
4104{
4105 u64 chunk_offset;
4106 u64 sys_chunk_offset;
4107 u64 chunk_size;
4108 u64 sys_chunk_size;
4109 u64 stripe_size;
4110 u64 sys_stripe_size;
4111 u64 alloc_profile;
4112 struct map_lookup *map;
4113 struct map_lookup *sys_map;
4114 struct btrfs_fs_info *fs_info = root->fs_info;
4115 struct btrfs_root *extent_root = fs_info->extent_root;
4116 int ret;
4117
4118 ret = find_next_chunk(fs_info->chunk_root,
4119 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07004120 if (ret)
4121 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004122
Miao Xiede98ced2013-01-29 10:13:12 +00004123 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05004124 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4125 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004126 if (ret)
4127 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004128
4129 sys_chunk_offset = chunk_offset + chunk_size;
4130
Miao Xiede98ced2013-01-29 10:13:12 +00004131 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05004132 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
4133 &sys_chunk_size, &sys_stripe_size,
4134 sys_chunk_offset, alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004135 if (ret) {
4136 btrfs_abort_transaction(trans, root, ret);
4137 goto out;
4138 }
Yan Zheng2b820322008-11-17 21:11:30 -05004139
4140 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
David Sterba005d6422012-09-18 07:52:32 -06004141 if (ret) {
4142 btrfs_abort_transaction(trans, root, ret);
4143 goto out;
4144 }
Yan Zheng2b820322008-11-17 21:11:30 -05004145
4146 /*
4147 * Modifying chunk tree needs allocating new blocks from both
4148 * system block group and metadata block group. So we only can
4149 * do operations require modifying the chunk tree after both
4150 * block groups were created.
4151 */
4152 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4153 chunk_size, stripe_size);
David Sterba005d6422012-09-18 07:52:32 -06004154 if (ret) {
4155 btrfs_abort_transaction(trans, root, ret);
4156 goto out;
4157 }
Yan Zheng2b820322008-11-17 21:11:30 -05004158
4159 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
4160 sys_chunk_offset, sys_chunk_size,
4161 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004162 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004163 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004164
David Sterba005d6422012-09-18 07:52:32 -06004165out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004166
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004167 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004168}
4169
4170int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4171{
4172 struct extent_map *em;
4173 struct map_lookup *map;
4174 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4175 int readonly = 0;
4176 int i;
4177
Chris Mason890871b2009-09-02 16:24:52 -04004178 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004179 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004180 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004181 if (!em)
4182 return 1;
4183
Josef Bacikf48b9072010-01-27 02:07:59 +00004184 if (btrfs_test_opt(root, DEGRADED)) {
4185 free_extent_map(em);
4186 return 0;
4187 }
4188
Yan Zheng2b820322008-11-17 21:11:30 -05004189 map = (struct map_lookup *)em->bdev;
4190 for (i = 0; i < map->num_stripes; i++) {
4191 if (!map->stripes[i].dev->writeable) {
4192 readonly = 1;
4193 break;
4194 }
4195 }
4196 free_extent_map(em);
4197 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004198}
4199
4200void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4201{
David Sterbaa8067e02011-04-21 00:34:43 +02004202 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004203}
4204
4205void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4206{
4207 struct extent_map *em;
4208
Chris Masond3977122009-01-05 21:25:51 -05004209 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004210 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004211 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4212 if (em)
4213 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004214 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004215 if (!em)
4216 break;
4217 kfree(em->bdev);
4218 /* once for us */
4219 free_extent_map(em);
4220 /* once for the tree */
4221 free_extent_map(em);
4222 }
4223}
4224
Stefan Behrens5d964052012-11-05 14:59:07 +01004225int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004226{
Stefan Behrens5d964052012-11-05 14:59:07 +01004227 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004228 struct extent_map *em;
4229 struct map_lookup *map;
4230 struct extent_map_tree *em_tree = &map_tree->map_tree;
4231 int ret;
4232
Chris Mason890871b2009-09-02 16:24:52 -04004233 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004234 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004235 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004236
Josef Bacikfb7669b2013-04-23 10:53:18 -04004237 /*
4238 * We could return errors for these cases, but that could get ugly and
4239 * we'd probably do the same thing which is just not do anything else
4240 * and exit, so return 1 so the callers don't try to use other copies.
4241 */
4242 if (!em) {
4243 btrfs_emerg(fs_info, "No mapping for %Lu-%Lu\n", logical,
4244 logical+len);
4245 return 1;
4246 }
4247
4248 if (em->start > logical || em->start + em->len < logical) {
4249 btrfs_emerg(fs_info, "Invalid mapping for %Lu-%Lu, got "
4250 "%Lu-%Lu\n", logical, logical+len, em->start,
4251 em->start + em->len);
4252 return 1;
4253 }
4254
Chris Masonf1885912008-04-09 16:28:12 -04004255 map = (struct map_lookup *)em->bdev;
4256 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4257 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004258 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4259 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004260 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4261 ret = 2;
4262 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4263 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004264 else
4265 ret = 1;
4266 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004267
4268 btrfs_dev_replace_lock(&fs_info->dev_replace);
4269 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4270 ret++;
4271 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4272
Chris Masonf1885912008-04-09 16:28:12 -04004273 return ret;
4274}
4275
David Woodhouse53b381b2013-01-29 18:40:14 -05004276unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4277 struct btrfs_mapping_tree *map_tree,
4278 u64 logical)
4279{
4280 struct extent_map *em;
4281 struct map_lookup *map;
4282 struct extent_map_tree *em_tree = &map_tree->map_tree;
4283 unsigned long len = root->sectorsize;
4284
4285 read_lock(&em_tree->lock);
4286 em = lookup_extent_mapping(em_tree, logical, len);
4287 read_unlock(&em_tree->lock);
4288 BUG_ON(!em);
4289
4290 BUG_ON(em->start > logical || em->start + em->len < logical);
4291 map = (struct map_lookup *)em->bdev;
4292 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4293 BTRFS_BLOCK_GROUP_RAID6)) {
4294 len = map->stripe_len * nr_data_stripes(map);
4295 }
4296 free_extent_map(em);
4297 return len;
4298}
4299
4300int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4301 u64 logical, u64 len, int mirror_num)
4302{
4303 struct extent_map *em;
4304 struct map_lookup *map;
4305 struct extent_map_tree *em_tree = &map_tree->map_tree;
4306 int ret = 0;
4307
4308 read_lock(&em_tree->lock);
4309 em = lookup_extent_mapping(em_tree, logical, len);
4310 read_unlock(&em_tree->lock);
4311 BUG_ON(!em);
4312
4313 BUG_ON(em->start > logical || em->start + em->len < logical);
4314 map = (struct map_lookup *)em->bdev;
4315 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4316 BTRFS_BLOCK_GROUP_RAID6))
4317 ret = 1;
4318 free_extent_map(em);
4319 return ret;
4320}
4321
Stefan Behrens30d98612012-11-06 14:52:18 +01004322static int find_live_mirror(struct btrfs_fs_info *fs_info,
4323 struct map_lookup *map, int first, int num,
4324 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004325{
4326 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004327 int tolerance;
4328 struct btrfs_device *srcdev;
4329
4330 if (dev_replace_is_ongoing &&
4331 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4332 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4333 srcdev = fs_info->dev_replace.srcdev;
4334 else
4335 srcdev = NULL;
4336
4337 /*
4338 * try to avoid the drive that is the source drive for a
4339 * dev-replace procedure, only choose it if no other non-missing
4340 * mirror is available
4341 */
4342 for (tolerance = 0; tolerance < 2; tolerance++) {
4343 if (map->stripes[optimal].dev->bdev &&
4344 (tolerance || map->stripes[optimal].dev != srcdev))
4345 return optimal;
4346 for (i = first; i < first + num; i++) {
4347 if (map->stripes[i].dev->bdev &&
4348 (tolerance || map->stripes[i].dev != srcdev))
4349 return i;
4350 }
Chris Masondfe25022008-05-13 13:46:40 -04004351 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004352
Chris Masondfe25022008-05-13 13:46:40 -04004353 /* we couldn't find one that doesn't fail. Just return something
4354 * and the io error handling code will clean up eventually
4355 */
4356 return optimal;
4357}
4358
David Woodhouse53b381b2013-01-29 18:40:14 -05004359static inline int parity_smaller(u64 a, u64 b)
4360{
4361 return a > b;
4362}
4363
4364/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4365static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4366{
4367 struct btrfs_bio_stripe s;
4368 int i;
4369 u64 l;
4370 int again = 1;
4371
4372 while (again) {
4373 again = 0;
4374 for (i = 0; i < bbio->num_stripes - 1; i++) {
4375 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4376 s = bbio->stripes[i];
4377 l = raid_map[i];
4378 bbio->stripes[i] = bbio->stripes[i+1];
4379 raid_map[i] = raid_map[i+1];
4380 bbio->stripes[i+1] = s;
4381 raid_map[i+1] = l;
4382 again = 1;
4383 }
4384 }
4385 }
4386}
4387
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004388static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004389 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004390 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004391 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004392{
4393 struct extent_map *em;
4394 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004395 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004396 struct extent_map_tree *em_tree = &map_tree->map_tree;
4397 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004398 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004399 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004400 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004401 u64 stripe_nr_orig;
4402 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004403 u64 stripe_len;
4404 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004405 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004406 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004407 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004408 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004409 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004410 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004411 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4412 int dev_replace_is_ongoing = 0;
4413 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004414 int patch_the_first_stripe_for_dev_replace = 0;
4415 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004416 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004417
Chris Mason890871b2009-09-02 16:24:52 -04004418 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004419 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004420 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004421
Chris Mason3b951512008-04-17 11:29:12 -04004422 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004423 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
4424 (unsigned long long)logical,
4425 (unsigned long long)*length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004426 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004427 }
Chris Mason0b86a832008-03-24 15:01:56 -04004428
Josef Bacik9bb91872013-04-19 23:45:33 -04004429 if (em->start > logical || em->start + em->len < logical) {
4430 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
4431 "found %Lu-%Lu\n", logical, em->start,
4432 em->start + em->len);
4433 return -EINVAL;
4434 }
4435
Chris Mason0b86a832008-03-24 15:01:56 -04004436 map = (struct map_lookup *)em->bdev;
4437 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004438
David Woodhouse53b381b2013-01-29 18:40:14 -05004439 if (mirror_num > map->num_stripes)
4440 mirror_num = 0;
4441
4442 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004443 stripe_nr = offset;
4444 /*
4445 * stripe_nr counts the total number of stripes we have to stride
4446 * to get to this block
4447 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004448 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004449
David Woodhouse53b381b2013-01-29 18:40:14 -05004450 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004451 BUG_ON(offset < stripe_offset);
4452
4453 /* stripe_offset is the offset of this block in its stripe*/
4454 stripe_offset = offset - stripe_offset;
4455
David Woodhouse53b381b2013-01-29 18:40:14 -05004456 /* if we're here for raid56, we need to know the stripe aligned start */
4457 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4458 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4459 raid56_full_stripe_start = offset;
4460
4461 /* allow a write of a full stripe, but make sure we don't
4462 * allow straddling of stripes
4463 */
4464 do_div(raid56_full_stripe_start, full_stripe_len);
4465 raid56_full_stripe_start *= full_stripe_len;
4466 }
4467
4468 if (rw & REQ_DISCARD) {
4469 /* we don't discard raid56 yet */
4470 if (map->type &
4471 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4472 ret = -EOPNOTSUPP;
4473 goto out;
4474 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004475 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004476 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4477 u64 max_len;
4478 /* For writes to RAID[56], allow a full stripeset across all disks.
4479 For other RAID types and for RAID[56] reads, just allow a single
4480 stripe (on a single disk). */
4481 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4482 (rw & REQ_WRITE)) {
4483 max_len = stripe_len * nr_data_stripes(map) -
4484 (offset - raid56_full_stripe_start);
4485 } else {
4486 /* we limit the length of each bio to what fits in a stripe */
4487 max_len = stripe_len - stripe_offset;
4488 }
4489 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004490 } else {
4491 *length = em->len - offset;
4492 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004493
David Woodhouse53b381b2013-01-29 18:40:14 -05004494 /* This is for when we're called from btrfs_merge_bio_hook() and all
4495 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004496 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004497 goto out;
4498
Stefan Behrens472262f2012-11-06 14:43:46 +01004499 btrfs_dev_replace_lock(dev_replace);
4500 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4501 if (!dev_replace_is_ongoing)
4502 btrfs_dev_replace_unlock(dev_replace);
4503
Stefan Behrensad6d6202012-11-06 15:06:47 +01004504 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4505 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4506 dev_replace->tgtdev != NULL) {
4507 /*
4508 * in dev-replace case, for repair case (that's the only
4509 * case where the mirror is selected explicitly when
4510 * calling btrfs_map_block), blocks left of the left cursor
4511 * can also be read from the target drive.
4512 * For REQ_GET_READ_MIRRORS, the target drive is added as
4513 * the last one to the array of stripes. For READ, it also
4514 * needs to be supported using the same mirror number.
4515 * If the requested block is not left of the left cursor,
4516 * EIO is returned. This can happen because btrfs_num_copies()
4517 * returns one more in the dev-replace case.
4518 */
4519 u64 tmp_length = *length;
4520 struct btrfs_bio *tmp_bbio = NULL;
4521 int tmp_num_stripes;
4522 u64 srcdev_devid = dev_replace->srcdev->devid;
4523 int index_srcdev = 0;
4524 int found = 0;
4525 u64 physical_of_found = 0;
4526
4527 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004528 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004529 if (ret) {
4530 WARN_ON(tmp_bbio != NULL);
4531 goto out;
4532 }
4533
4534 tmp_num_stripes = tmp_bbio->num_stripes;
4535 if (mirror_num > tmp_num_stripes) {
4536 /*
4537 * REQ_GET_READ_MIRRORS does not contain this
4538 * mirror, that means that the requested area
4539 * is not left of the left cursor
4540 */
4541 ret = -EIO;
4542 kfree(tmp_bbio);
4543 goto out;
4544 }
4545
4546 /*
4547 * process the rest of the function using the mirror_num
4548 * of the source drive. Therefore look it up first.
4549 * At the end, patch the device pointer to the one of the
4550 * target drive.
4551 */
4552 for (i = 0; i < tmp_num_stripes; i++) {
4553 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4554 /*
4555 * In case of DUP, in order to keep it
4556 * simple, only add the mirror with the
4557 * lowest physical address
4558 */
4559 if (found &&
4560 physical_of_found <=
4561 tmp_bbio->stripes[i].physical)
4562 continue;
4563 index_srcdev = i;
4564 found = 1;
4565 physical_of_found =
4566 tmp_bbio->stripes[i].physical;
4567 }
4568 }
4569
4570 if (found) {
4571 mirror_num = index_srcdev + 1;
4572 patch_the_first_stripe_for_dev_replace = 1;
4573 physical_to_patch_in_first_stripe = physical_of_found;
4574 } else {
4575 WARN_ON(1);
4576 ret = -EIO;
4577 kfree(tmp_bbio);
4578 goto out;
4579 }
4580
4581 kfree(tmp_bbio);
4582 } else if (mirror_num > map->num_stripes) {
4583 mirror_num = 0;
4584 }
4585
Chris Masonf2d8d742008-04-21 10:03:05 -04004586 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004587 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004588 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00004589 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00004590 do_div(stripe_nr_end, map->stripe_len);
4591 stripe_end_offset = stripe_nr_end * map->stripe_len -
4592 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004593
Li Dongyangfce3bb92011-03-24 10:24:26 +00004594 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4595 if (rw & REQ_DISCARD)
4596 num_stripes = min_t(u64, map->num_stripes,
4597 stripe_nr_end - stripe_nr_orig);
4598 stripe_index = do_div(stripe_nr, map->num_stripes);
4599 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004600 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004601 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04004602 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04004603 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004604 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01004605 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04004606 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01004607 current->pid % map->num_stripes,
4608 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004609 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004610 }
Chris Mason2fff7342008-04-29 14:12:09 -04004611
Chris Mason611f0e02008-04-03 16:29:03 -04004612 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004613 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04004614 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004615 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04004616 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004617 } else {
4618 mirror_num = 1;
4619 }
Chris Mason2fff7342008-04-29 14:12:09 -04004620
Chris Mason321aecc2008-04-16 10:49:51 -04004621 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4622 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004623
4624 stripe_index = do_div(stripe_nr, factor);
4625 stripe_index *= map->sub_stripes;
4626
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004627 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004628 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004629 else if (rw & REQ_DISCARD)
4630 num_stripes = min_t(u64, map->sub_stripes *
4631 (stripe_nr_end - stripe_nr_orig),
4632 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04004633 else if (mirror_num)
4634 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004635 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04004636 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01004637 stripe_index = find_live_mirror(fs_info, map,
4638 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04004639 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01004640 current->pid % map->sub_stripes,
4641 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04004642 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004643 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004644
4645 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4646 BTRFS_BLOCK_GROUP_RAID6)) {
4647 u64 tmp;
4648
4649 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
4650 && raid_map_ret) {
4651 int i, rot;
4652
4653 /* push stripe_nr back to the start of the full stripe */
4654 stripe_nr = raid56_full_stripe_start;
4655 do_div(stripe_nr, stripe_len);
4656
4657 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4658
4659 /* RAID[56] write or recovery. Return all stripes */
4660 num_stripes = map->num_stripes;
4661 max_errors = nr_parity_stripes(map);
4662
4663 raid_map = kmalloc(sizeof(u64) * num_stripes,
4664 GFP_NOFS);
4665 if (!raid_map) {
4666 ret = -ENOMEM;
4667 goto out;
4668 }
4669
4670 /* Work out the disk rotation on this stripe-set */
4671 tmp = stripe_nr;
4672 rot = do_div(tmp, num_stripes);
4673
4674 /* Fill in the logical address of each stripe */
4675 tmp = stripe_nr * nr_data_stripes(map);
4676 for (i = 0; i < nr_data_stripes(map); i++)
4677 raid_map[(i+rot) % num_stripes] =
4678 em->start + (tmp + i) * map->stripe_len;
4679
4680 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
4681 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4682 raid_map[(i+rot+1) % num_stripes] =
4683 RAID6_Q_STRIPE;
4684
4685 *length = map->stripe_len;
4686 stripe_index = 0;
4687 stripe_offset = 0;
4688 } else {
4689 /*
4690 * Mirror #0 or #1 means the original data block.
4691 * Mirror #2 is RAID5 parity block.
4692 * Mirror #3 is RAID6 Q block.
4693 */
4694 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4695 if (mirror_num > 1)
4696 stripe_index = nr_data_stripes(map) +
4697 mirror_num - 2;
4698
4699 /* We distribute the parity blocks across stripes */
4700 tmp = stripe_nr + stripe_index;
4701 stripe_index = do_div(tmp, map->num_stripes);
4702 }
Chris Mason8790d502008-04-03 16:29:03 -04004703 } else {
4704 /*
4705 * after this do_div call, stripe_nr is the number of stripes
4706 * on this device we have to walk to find the data, and
4707 * stripe_index is the number of our device in the stripe array
4708 */
4709 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004710 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04004711 }
Chris Mason593060d2008-03-25 16:50:33 -04004712 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04004713
Stefan Behrens472262f2012-11-06 14:43:46 +01004714 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004715 if (dev_replace_is_ongoing) {
4716 if (rw & (REQ_WRITE | REQ_DISCARD))
4717 num_alloc_stripes <<= 1;
4718 if (rw & REQ_GET_READ_MIRRORS)
4719 num_alloc_stripes++;
4720 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004721 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08004722 if (!bbio) {
4723 ret = -ENOMEM;
4724 goto out;
4725 }
4726 atomic_set(&bbio->error, 0);
4727
Li Dongyangfce3bb92011-03-24 10:24:26 +00004728 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08004729 int factor = 0;
4730 int sub_stripes = 0;
4731 u64 stripes_per_dev = 0;
4732 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04004733 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004734
4735 if (map->type &
4736 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4737 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4738 sub_stripes = 1;
4739 else
4740 sub_stripes = map->sub_stripes;
4741
4742 factor = map->num_stripes / sub_stripes;
4743 stripes_per_dev = div_u64_rem(stripe_nr_end -
4744 stripe_nr_orig,
4745 factor,
4746 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04004747 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4748 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004749 }
4750
Li Dongyangfce3bb92011-03-24 10:24:26 +00004751 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004752 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04004753 map->stripes[stripe_index].physical +
4754 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004755 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004756
Li Zefanec9ef7a2011-12-01 14:06:42 +08004757 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4758 BTRFS_BLOCK_GROUP_RAID10)) {
4759 bbio->stripes[i].length = stripes_per_dev *
4760 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004761
Li Zefanec9ef7a2011-12-01 14:06:42 +08004762 if (i / sub_stripes < remaining_stripes)
4763 bbio->stripes[i].length +=
4764 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004765
4766 /*
4767 * Special for the first stripe and
4768 * the last stripe:
4769 *
4770 * |-------|...|-------|
4771 * |----------|
4772 * off end_off
4773 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08004774 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004775 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00004776 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004777
4778 if (stripe_index >= last_stripe &&
4779 stripe_index <= (last_stripe +
4780 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08004781 bbio->stripes[i].length -=
4782 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004783
Li Zefanec9ef7a2011-12-01 14:06:42 +08004784 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00004785 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004786 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02004787 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004788
4789 stripe_index++;
4790 if (stripe_index == map->num_stripes) {
4791 /* This could only happen for RAID0/10 */
4792 stripe_index = 0;
4793 stripe_nr++;
4794 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004795 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004796 } else {
4797 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004798 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004799 map->stripes[stripe_index].physical +
4800 stripe_offset +
4801 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004802 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004803 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004804 stripe_index++;
4805 }
Chris Mason593060d2008-03-25 16:50:33 -04004806 }
Li Zefande11cc12011-12-01 12:55:47 +08004807
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004808 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
Li Zefande11cc12011-12-01 12:55:47 +08004809 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4810 BTRFS_BLOCK_GROUP_RAID10 |
David Woodhouse53b381b2013-01-29 18:40:14 -05004811 BTRFS_BLOCK_GROUP_RAID5 |
Li Zefande11cc12011-12-01 12:55:47 +08004812 BTRFS_BLOCK_GROUP_DUP)) {
4813 max_errors = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05004814 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4815 max_errors = 2;
Li Zefande11cc12011-12-01 12:55:47 +08004816 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004817 }
Li Zefande11cc12011-12-01 12:55:47 +08004818
Stefan Behrens472262f2012-11-06 14:43:46 +01004819 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
4820 dev_replace->tgtdev != NULL) {
4821 int index_where_to_add;
4822 u64 srcdev_devid = dev_replace->srcdev->devid;
4823
4824 /*
4825 * duplicate the write operations while the dev replace
4826 * procedure is running. Since the copying of the old disk
4827 * to the new disk takes place at run time while the
4828 * filesystem is mounted writable, the regular write
4829 * operations to the old disk have to be duplicated to go
4830 * to the new disk as well.
4831 * Note that device->missing is handled by the caller, and
4832 * that the write to the old disk is already set up in the
4833 * stripes array.
4834 */
4835 index_where_to_add = num_stripes;
4836 for (i = 0; i < num_stripes; i++) {
4837 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4838 /* write to new disk, too */
4839 struct btrfs_bio_stripe *new =
4840 bbio->stripes + index_where_to_add;
4841 struct btrfs_bio_stripe *old =
4842 bbio->stripes + i;
4843
4844 new->physical = old->physical;
4845 new->length = old->length;
4846 new->dev = dev_replace->tgtdev;
4847 index_where_to_add++;
4848 max_errors++;
4849 }
4850 }
4851 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004852 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
4853 dev_replace->tgtdev != NULL) {
4854 u64 srcdev_devid = dev_replace->srcdev->devid;
4855 int index_srcdev = 0;
4856 int found = 0;
4857 u64 physical_of_found = 0;
4858
4859 /*
4860 * During the dev-replace procedure, the target drive can
4861 * also be used to read data in case it is needed to repair
4862 * a corrupt block elsewhere. This is possible if the
4863 * requested area is left of the left cursor. In this area,
4864 * the target drive is a full copy of the source drive.
4865 */
4866 for (i = 0; i < num_stripes; i++) {
4867 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4868 /*
4869 * In case of DUP, in order to keep it
4870 * simple, only add the mirror with the
4871 * lowest physical address
4872 */
4873 if (found &&
4874 physical_of_found <=
4875 bbio->stripes[i].physical)
4876 continue;
4877 index_srcdev = i;
4878 found = 1;
4879 physical_of_found = bbio->stripes[i].physical;
4880 }
4881 }
4882 if (found) {
4883 u64 length = map->stripe_len;
4884
4885 if (physical_of_found + length <=
4886 dev_replace->cursor_left) {
4887 struct btrfs_bio_stripe *tgtdev_stripe =
4888 bbio->stripes + num_stripes;
4889
4890 tgtdev_stripe->physical = physical_of_found;
4891 tgtdev_stripe->length =
4892 bbio->stripes[index_srcdev].length;
4893 tgtdev_stripe->dev = dev_replace->tgtdev;
4894
4895 num_stripes++;
4896 }
4897 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004898 }
4899
Li Zefande11cc12011-12-01 12:55:47 +08004900 *bbio_ret = bbio;
4901 bbio->num_stripes = num_stripes;
4902 bbio->max_errors = max_errors;
4903 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004904
4905 /*
4906 * this is the case that REQ_READ && dev_replace_is_ongoing &&
4907 * mirror_num == num_stripes + 1 && dev_replace target drive is
4908 * available as a mirror
4909 */
4910 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
4911 WARN_ON(num_stripes > 1);
4912 bbio->stripes[0].dev = dev_replace->tgtdev;
4913 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
4914 bbio->mirror_num = map->num_stripes + 1;
4915 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004916 if (raid_map) {
4917 sort_parity_stripes(bbio, raid_map);
4918 *raid_map_ret = raid_map;
4919 }
Chris Masoncea9e442008-04-09 16:28:12 -04004920out:
Stefan Behrens472262f2012-11-06 14:43:46 +01004921 if (dev_replace_is_ongoing)
4922 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04004923 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08004924 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004925}
4926
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004927int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004928 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004929 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04004930{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004931 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004932 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04004933}
4934
Yan Zhenga512bbf2008-12-08 16:46:26 -05004935int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4936 u64 chunk_start, u64 physical, u64 devid,
4937 u64 **logical, int *naddrs, int *stripe_len)
4938{
4939 struct extent_map_tree *em_tree = &map_tree->map_tree;
4940 struct extent_map *em;
4941 struct map_lookup *map;
4942 u64 *buf;
4943 u64 bytenr;
4944 u64 length;
4945 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05004946 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05004947 int i, j, nr = 0;
4948
Chris Mason890871b2009-09-02 16:24:52 -04004949 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004950 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004951 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004952
Josef Bacik835d9742013-03-19 12:13:25 -04004953 if (!em) {
4954 printk(KERN_ERR "btrfs: couldn't find em for chunk %Lu\n",
4955 chunk_start);
4956 return -EIO;
4957 }
4958
4959 if (em->start != chunk_start) {
4960 printk(KERN_ERR "btrfs: bad chunk start, em=%Lu, wanted=%Lu\n",
4961 em->start, chunk_start);
4962 free_extent_map(em);
4963 return -EIO;
4964 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004965 map = (struct map_lookup *)em->bdev;
4966
4967 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05004968 rmap_len = map->stripe_len;
4969
Yan Zhenga512bbf2008-12-08 16:46:26 -05004970 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4971 do_div(length, map->num_stripes / map->sub_stripes);
4972 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4973 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05004974 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4975 BTRFS_BLOCK_GROUP_RAID6)) {
4976 do_div(length, nr_data_stripes(map));
4977 rmap_len = map->stripe_len * nr_data_stripes(map);
4978 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004979
4980 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004981 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05004982
4983 for (i = 0; i < map->num_stripes; i++) {
4984 if (devid && map->stripes[i].dev->devid != devid)
4985 continue;
4986 if (map->stripes[i].physical > physical ||
4987 map->stripes[i].physical + length <= physical)
4988 continue;
4989
4990 stripe_nr = physical - map->stripes[i].physical;
4991 do_div(stripe_nr, map->stripe_len);
4992
4993 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4994 stripe_nr = stripe_nr * map->num_stripes + i;
4995 do_div(stripe_nr, map->sub_stripes);
4996 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4997 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05004998 } /* else if RAID[56], multiply by nr_data_stripes().
4999 * Alternatively, just use rmap_len below instead of
5000 * map->stripe_len */
5001
5002 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005003 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005004 for (j = 0; j < nr; j++) {
5005 if (buf[j] == bytenr)
5006 break;
5007 }
Chris Mason934d3752008-12-08 16:43:10 -05005008 if (j == nr) {
5009 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005010 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005011 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005012 }
5013
Yan Zhenga512bbf2008-12-08 16:46:26 -05005014 *logical = buf;
5015 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005016 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005017
5018 free_extent_map(em);
5019 return 0;
5020}
5021
Jan Schmidta1d3c472011-08-04 17:15:33 +02005022static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005023{
Chris Mason9be33952013-05-17 18:30:14 -04005024 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005025 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005026
Stefan Behrens442a4f62012-05-25 16:06:08 +02005027 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005028 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005029 if (err == -EIO || err == -EREMOTEIO) {
5030 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005031 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005032 struct btrfs_device *dev;
5033
5034 BUG_ON(stripe_index >= bbio->num_stripes);
5035 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005036 if (dev->bdev) {
5037 if (bio->bi_rw & WRITE)
5038 btrfs_dev_stat_inc(dev,
5039 BTRFS_DEV_STAT_WRITE_ERRS);
5040 else
5041 btrfs_dev_stat_inc(dev,
5042 BTRFS_DEV_STAT_READ_ERRS);
5043 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5044 btrfs_dev_stat_inc(dev,
5045 BTRFS_DEV_STAT_FLUSH_ERRS);
5046 btrfs_dev_stat_print_on_error(dev);
5047 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005048 }
5049 }
Chris Mason8790d502008-04-03 16:29:03 -04005050
Jan Schmidta1d3c472011-08-04 17:15:33 +02005051 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005052 is_orig_bio = 1;
5053
Jan Schmidta1d3c472011-08-04 17:15:33 +02005054 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005055 if (!is_orig_bio) {
5056 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005057 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005058 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005059 bio->bi_private = bbio->private;
5060 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005061 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005062 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005063 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005064 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005065 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005066 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005067 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005068 /*
5069 * this bio is actually up to date, we didn't
5070 * go over the max number of errors
5071 */
5072 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005073 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005074 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005075 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04005076
5077 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005078 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005079 bio_put(bio);
5080 }
Chris Mason8790d502008-04-03 16:29:03 -04005081}
5082
Chris Mason8b712842008-06-11 16:50:36 -04005083struct async_sched {
5084 struct bio *bio;
5085 int rw;
5086 struct btrfs_fs_info *info;
5087 struct btrfs_work work;
5088};
5089
5090/*
5091 * see run_scheduled_bios for a description of why bios are collected for
5092 * async submit.
5093 *
5094 * This will add one bio to the pending list for a device and make sure
5095 * the work struct is scheduled.
5096 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005097static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5098 struct btrfs_device *device,
5099 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005100{
5101 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005102 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005103
David Woodhouse53b381b2013-01-29 18:40:14 -05005104 if (device->missing || !device->bdev) {
5105 bio_endio(bio, -EIO);
5106 return;
5107 }
5108
Chris Mason8b712842008-06-11 16:50:36 -04005109 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005110 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005111 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005112 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005113 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005114 return;
Chris Mason8b712842008-06-11 16:50:36 -04005115 }
5116
5117 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005118 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005119 * higher layers. Otherwise, the async bio makes it appear we have
5120 * made progress against dirty pages when we've really just put it
5121 * on a queue for later
5122 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005123 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005124 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005125 bio->bi_next = NULL;
5126 bio->bi_rw |= rw;
5127
5128 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005129 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005130 pending_bios = &device->pending_sync_bios;
5131 else
5132 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005133
Chris Masonffbd5172009-04-20 15:50:09 -04005134 if (pending_bios->tail)
5135 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005136
Chris Masonffbd5172009-04-20 15:50:09 -04005137 pending_bios->tail = bio;
5138 if (!pending_bios->head)
5139 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005140 if (device->running_pending)
5141 should_queue = 0;
5142
5143 spin_unlock(&device->io_lock);
5144
5145 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04005146 btrfs_queue_worker(&root->fs_info->submit_workers,
5147 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005148}
5149
Josef Bacikde1ee922012-10-19 16:50:56 -04005150static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5151 sector_t sector)
5152{
5153 struct bio_vec *prev;
5154 struct request_queue *q = bdev_get_queue(bdev);
5155 unsigned short max_sectors = queue_max_sectors(q);
5156 struct bvec_merge_data bvm = {
5157 .bi_bdev = bdev,
5158 .bi_sector = sector,
5159 .bi_rw = bio->bi_rw,
5160 };
5161
5162 if (bio->bi_vcnt == 0) {
5163 WARN_ON(1);
5164 return 1;
5165 }
5166
5167 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
5168 if ((bio->bi_size >> 9) > max_sectors)
5169 return 0;
5170
5171 if (!q->merge_bvec_fn)
5172 return 1;
5173
5174 bvm.bi_size = bio->bi_size - prev->bv_len;
5175 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5176 return 0;
5177 return 1;
5178}
5179
5180static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5181 struct bio *bio, u64 physical, int dev_nr,
5182 int rw, int async)
5183{
5184 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5185
5186 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005187 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005188 bio->bi_end_io = btrfs_end_bio;
5189 bio->bi_sector = physical >> 9;
5190#ifdef DEBUG
5191 {
5192 struct rcu_string *name;
5193
5194 rcu_read_lock();
5195 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005196 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005197 "(%s id %llu), size=%u\n", rw,
5198 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5199 name->str, dev->devid, bio->bi_size);
5200 rcu_read_unlock();
5201 }
5202#endif
5203 bio->bi_bdev = dev->bdev;
5204 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005205 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005206 else
5207 btrfsic_submit_bio(rw, bio);
5208}
5209
5210static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5211 struct bio *first_bio, struct btrfs_device *dev,
5212 int dev_nr, int rw, int async)
5213{
5214 struct bio_vec *bvec = first_bio->bi_io_vec;
5215 struct bio *bio;
5216 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5217 u64 physical = bbio->stripes[dev_nr].physical;
5218
5219again:
5220 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5221 if (!bio)
5222 return -ENOMEM;
5223
5224 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5225 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5226 bvec->bv_offset) < bvec->bv_len) {
5227 u64 len = bio->bi_size;
5228
5229 atomic_inc(&bbio->stripes_pending);
5230 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5231 rw, async);
5232 physical += len;
5233 goto again;
5234 }
5235 bvec++;
5236 }
5237
5238 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5239 return 0;
5240}
5241
5242static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5243{
5244 atomic_inc(&bbio->error);
5245 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5246 bio->bi_private = bbio->private;
5247 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005248 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Josef Bacikde1ee922012-10-19 16:50:56 -04005249 bio->bi_sector = logical >> 9;
5250 kfree(bbio);
5251 bio_endio(bio, -EIO);
5252 }
5253}
5254
Chris Masonf1885912008-04-09 16:28:12 -04005255int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005256 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005257{
Chris Mason0b86a832008-03-24 15:01:56 -04005258 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005259 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04005260 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005261 u64 length = 0;
5262 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005263 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005264 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005265 int dev_nr = 0;
5266 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005267 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005268
Chris Masonf2d8d742008-04-21 10:03:05 -04005269 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005270 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005271
David Woodhouse53b381b2013-01-29 18:40:14 -05005272 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5273 mirror_num, &raid_map);
5274 if (ret) /* -ENOMEM */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005275 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04005276
Jan Schmidta1d3c472011-08-04 17:15:33 +02005277 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005278 bbio->orig_bio = first_bio;
5279 bbio->private = first_bio->bi_private;
5280 bbio->end_io = first_bio->bi_end_io;
5281 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5282
5283 if (raid_map) {
5284 /* In this case, map_length has been set to the length of
5285 a single stripe; not the whole write */
5286 if (rw & WRITE) {
5287 return raid56_parity_write(root, bio, bbio,
5288 raid_map, map_length);
5289 } else {
5290 return raid56_parity_recover(root, bio, bbio,
5291 raid_map, map_length,
5292 mirror_num);
5293 }
5294 }
5295
Chris Masoncea9e442008-04-09 16:28:12 -04005296 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005297 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
5298 (unsigned long long)logical,
5299 (unsigned long long)length,
5300 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005301 BUG();
5302 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005303
Chris Masond3977122009-01-05 21:25:51 -05005304 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005305 dev = bbio->stripes[dev_nr].dev;
5306 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5307 bbio_error(bbio, first_bio, logical);
5308 dev_nr++;
5309 continue;
5310 }
5311
5312 /*
5313 * Check and see if we're ok with this bio based on it's size
5314 * and offset with the given device.
5315 */
5316 if (!bio_size_ok(dev->bdev, first_bio,
5317 bbio->stripes[dev_nr].physical >> 9)) {
5318 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5319 dev_nr, rw, async_submit);
5320 BUG_ON(ret);
5321 dev_nr++;
5322 continue;
5323 }
5324
Jan Schmidta1d3c472011-08-04 17:15:33 +02005325 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005326 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005327 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005328 } else {
5329 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04005330 }
Josef Bacik606686e2012-06-04 14:03:51 -04005331
Josef Bacikde1ee922012-10-19 16:50:56 -04005332 submit_stripe_bio(root, bbio, bio,
5333 bbio->stripes[dev_nr].physical, dev_nr, rw,
5334 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005335 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005336 }
Chris Mason0b86a832008-03-24 15:01:56 -04005337 return 0;
5338}
5339
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005340struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005341 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005342{
Yan Zheng2b820322008-11-17 21:11:30 -05005343 struct btrfs_device *device;
5344 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005345
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005346 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005347 while (cur_devices) {
5348 if (!fsid ||
5349 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5350 device = __find_device(&cur_devices->devices,
5351 devid, uuid);
5352 if (device)
5353 return device;
5354 }
5355 cur_devices = cur_devices->seed;
5356 }
5357 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005358}
5359
Chris Masondfe25022008-05-13 13:46:40 -04005360static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5361 u64 devid, u8 *dev_uuid)
5362{
5363 struct btrfs_device *device;
5364 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5365
5366 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005367 if (!device)
5368 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04005369 list_add(&device->dev_list,
5370 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04005371 device->dev_root = root->fs_info->dev_root;
5372 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04005373 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05005374 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05005375 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04005376 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05005377 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04005378 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05005379 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04005380 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
5381 return device;
5382}
5383
Chris Mason0b86a832008-03-24 15:01:56 -04005384static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5385 struct extent_buffer *leaf,
5386 struct btrfs_chunk *chunk)
5387{
5388 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5389 struct map_lookup *map;
5390 struct extent_map *em;
5391 u64 logical;
5392 u64 length;
5393 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005394 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005395 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005396 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005397 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005398
Chris Masone17cade2008-04-15 15:41:47 -04005399 logical = key->offset;
5400 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005401
Chris Mason890871b2009-09-02 16:24:52 -04005402 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005403 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005404 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005405
5406 /* already mapped? */
5407 if (em && em->start <= logical && em->start + em->len > logical) {
5408 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005409 return 0;
5410 } else if (em) {
5411 free_extent_map(em);
5412 }
Chris Mason0b86a832008-03-24 15:01:56 -04005413
David Sterba172ddd62011-04-21 00:48:27 +02005414 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005415 if (!em)
5416 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005417 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5418 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005419 if (!map) {
5420 free_extent_map(em);
5421 return -ENOMEM;
5422 }
5423
5424 em->bdev = (struct block_device *)map;
5425 em->start = logical;
5426 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005427 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005428 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005429 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005430
Chris Mason593060d2008-03-25 16:50:33 -04005431 map->num_stripes = num_stripes;
5432 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5433 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5434 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5435 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5436 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005437 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005438 for (i = 0; i < num_stripes; i++) {
5439 map->stripes[i].physical =
5440 btrfs_stripe_offset_nr(leaf, chunk, i);
5441 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005442 read_extent_buffer(leaf, uuid, (unsigned long)
5443 btrfs_stripe_dev_uuid_nr(chunk, i),
5444 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005445 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5446 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005447 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005448 kfree(map);
5449 free_extent_map(em);
5450 return -EIO;
5451 }
Chris Masondfe25022008-05-13 13:46:40 -04005452 if (!map->stripes[i].dev) {
5453 map->stripes[i].dev =
5454 add_missing_dev(root, devid, uuid);
5455 if (!map->stripes[i].dev) {
5456 kfree(map);
5457 free_extent_map(em);
5458 return -EIO;
5459 }
5460 }
5461 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005462 }
5463
Chris Mason890871b2009-09-02 16:24:52 -04005464 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005465 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005466 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005467 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005468 free_extent_map(em);
5469
5470 return 0;
5471}
5472
Jeff Mahoney143bede2012-03-01 14:56:26 +01005473static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005474 struct btrfs_dev_item *dev_item,
5475 struct btrfs_device *device)
5476{
5477 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005478
5479 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005480 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5481 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005482 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5483 device->type = btrfs_device_type(leaf, dev_item);
5484 device->io_align = btrfs_device_io_align(leaf, dev_item);
5485 device->io_width = btrfs_device_io_width(leaf, dev_item);
5486 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005487 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005488 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005489
5490 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005491 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005492}
5493
Yan Zheng2b820322008-11-17 21:11:30 -05005494static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5495{
5496 struct btrfs_fs_devices *fs_devices;
5497 int ret;
5498
Li Zefanb367e472011-12-07 11:38:24 +08005499 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005500
5501 fs_devices = root->fs_info->fs_devices->seed;
5502 while (fs_devices) {
5503 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5504 ret = 0;
5505 goto out;
5506 }
5507 fs_devices = fs_devices->seed;
5508 }
5509
5510 fs_devices = find_fsid(fsid);
5511 if (!fs_devices) {
5512 ret = -ENOENT;
5513 goto out;
5514 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005515
5516 fs_devices = clone_fs_devices(fs_devices);
5517 if (IS_ERR(fs_devices)) {
5518 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005519 goto out;
5520 }
5521
Christoph Hellwig97288f22008-12-02 06:36:09 -05005522 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05005523 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02005524 if (ret) {
5525 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005526 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02005527 }
Yan Zheng2b820322008-11-17 21:11:30 -05005528
5529 if (!fs_devices->seeding) {
5530 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005531 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005532 ret = -EINVAL;
5533 goto out;
5534 }
5535
5536 fs_devices->seed = root->fs_info->fs_devices->seed;
5537 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005538out:
Yan Zheng2b820322008-11-17 21:11:30 -05005539 return ret;
5540}
5541
Chris Mason0d81ba52008-03-24 15:02:07 -04005542static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04005543 struct extent_buffer *leaf,
5544 struct btrfs_dev_item *dev_item)
5545{
5546 struct btrfs_device *device;
5547 u64 devid;
5548 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005549 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04005550 u8 dev_uuid[BTRFS_UUID_SIZE];
5551
Chris Mason0b86a832008-03-24 15:01:56 -04005552 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04005553 read_extent_buffer(leaf, dev_uuid,
5554 (unsigned long)btrfs_device_uuid(dev_item),
5555 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05005556 read_extent_buffer(leaf, fs_uuid,
5557 (unsigned long)btrfs_device_fsid(dev_item),
5558 BTRFS_UUID_SIZE);
5559
5560 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
5561 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05005562 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005563 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005564 }
5565
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005566 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05005567 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05005568 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005569 return -EIO;
5570
5571 if (!device) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005572 btrfs_warn(root->fs_info, "devid %llu missing",
5573 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05005574 device = add_missing_dev(root, devid, dev_uuid);
5575 if (!device)
5576 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05005577 } else if (!device->missing) {
5578 /*
5579 * this happens when a device that was properly setup
5580 * in the device info lists suddenly goes bad.
5581 * device->bdev is NULL, and so we have to set
5582 * device->missing to one here
5583 */
5584 root->fs_info->fs_devices->missing_devices++;
5585 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05005586 }
5587 }
5588
5589 if (device->fs_devices != root->fs_info->fs_devices) {
5590 BUG_ON(device->writeable);
5591 if (device->generation !=
5592 btrfs_device_generation(leaf, dev_item))
5593 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04005594 }
Chris Mason0b86a832008-03-24 15:01:56 -04005595
5596 fill_device_from_item(leaf, dev_item, device);
5597 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04005598 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01005599 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05005600 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04005601 spin_lock(&root->fs_info->free_chunk_lock);
5602 root->fs_info->free_chunk_space += device->total_bytes -
5603 device->bytes_used;
5604 spin_unlock(&root->fs_info->free_chunk_lock);
5605 }
Chris Mason0b86a832008-03-24 15:01:56 -04005606 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005607 return ret;
5608}
5609
Yan Zhenge4404d62008-12-12 10:03:26 -05005610int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04005611{
David Sterba6c417612011-04-13 15:41:04 +02005612 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04005613 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04005614 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04005615 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04005616 u8 *ptr;
5617 unsigned long sb_ptr;
5618 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005619 u32 num_stripes;
5620 u32 array_size;
5621 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005622 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04005623 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04005624
Yan Zhenge4404d62008-12-12 10:03:26 -05005625 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04005626 BTRFS_SUPER_INFO_SIZE);
5627 if (!sb)
5628 return -ENOMEM;
5629 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04005630 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02005631 /*
5632 * The sb extent buffer is artifical and just used to read the system array.
5633 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5634 * pages up-to-date when the page is larger: extent does not cover the
5635 * whole page and consequently check_page_uptodate does not find all
5636 * the page's extents up-to-date (the hole beyond sb),
5637 * write_extent_buffer then triggers a WARN_ON.
5638 *
5639 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5640 * but sb spans only this function. Add an explicit SetPageUptodate call
5641 * to silence the warning eg. on PowerPC 64.
5642 */
5643 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04005644 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05005645
Chris Masona061fc82008-05-07 11:43:44 -04005646 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005647 array_size = btrfs_super_sys_array_size(super_copy);
5648
Chris Mason0b86a832008-03-24 15:01:56 -04005649 ptr = super_copy->sys_chunk_array;
5650 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
5651 cur = 0;
5652
5653 while (cur < array_size) {
5654 disk_key = (struct btrfs_disk_key *)ptr;
5655 btrfs_disk_key_to_cpu(&key, disk_key);
5656
Chris Masona061fc82008-05-07 11:43:44 -04005657 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04005658 sb_ptr += len;
5659 cur += len;
5660
Chris Mason0d81ba52008-03-24 15:02:07 -04005661 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04005662 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04005663 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04005664 if (ret)
5665 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005666 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
5667 len = btrfs_chunk_item_size(num_stripes);
5668 } else {
Chris Mason84eed902008-04-25 09:04:37 -04005669 ret = -EIO;
5670 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005671 }
5672 ptr += len;
5673 sb_ptr += len;
5674 cur += len;
5675 }
Chris Masona061fc82008-05-07 11:43:44 -04005676 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04005677 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005678}
5679
5680int btrfs_read_chunk_tree(struct btrfs_root *root)
5681{
5682 struct btrfs_path *path;
5683 struct extent_buffer *leaf;
5684 struct btrfs_key key;
5685 struct btrfs_key found_key;
5686 int ret;
5687 int slot;
5688
5689 root = root->fs_info->chunk_root;
5690
5691 path = btrfs_alloc_path();
5692 if (!path)
5693 return -ENOMEM;
5694
Li Zefanb367e472011-12-07 11:38:24 +08005695 mutex_lock(&uuid_mutex);
5696 lock_chunks(root);
5697
Chris Mason0b86a832008-03-24 15:01:56 -04005698 /* first we search for all of the device items, and then we
5699 * read in all of the chunk items. This way we can create chunk
5700 * mappings that reference all of the devices that are afound
5701 */
5702 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5703 key.offset = 0;
5704 key.type = 0;
5705again:
5706 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00005707 if (ret < 0)
5708 goto error;
Chris Masond3977122009-01-05 21:25:51 -05005709 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005710 leaf = path->nodes[0];
5711 slot = path->slots[0];
5712 if (slot >= btrfs_header_nritems(leaf)) {
5713 ret = btrfs_next_leaf(root, path);
5714 if (ret == 0)
5715 continue;
5716 if (ret < 0)
5717 goto error;
5718 break;
5719 }
5720 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5721 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5722 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
5723 break;
5724 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5725 struct btrfs_dev_item *dev_item;
5726 dev_item = btrfs_item_ptr(leaf, slot,
5727 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04005728 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05005729 if (ret)
5730 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005731 }
5732 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5733 struct btrfs_chunk *chunk;
5734 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5735 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05005736 if (ret)
5737 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005738 }
5739 path->slots[0]++;
5740 }
5741 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5742 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005743 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005744 goto again;
5745 }
Chris Mason0b86a832008-03-24 15:01:56 -04005746 ret = 0;
5747error:
Li Zefanb367e472011-12-07 11:38:24 +08005748 unlock_chunks(root);
5749 mutex_unlock(&uuid_mutex);
5750
Yan Zheng2b820322008-11-17 21:11:30 -05005751 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005752 return ret;
5753}
Stefan Behrens442a4f62012-05-25 16:06:08 +02005754
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005755static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
5756{
5757 int i;
5758
5759 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5760 btrfs_dev_stat_reset(dev, i);
5761}
5762
5763int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
5764{
5765 struct btrfs_key key;
5766 struct btrfs_key found_key;
5767 struct btrfs_root *dev_root = fs_info->dev_root;
5768 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5769 struct extent_buffer *eb;
5770 int slot;
5771 int ret = 0;
5772 struct btrfs_device *device;
5773 struct btrfs_path *path = NULL;
5774 int i;
5775
5776 path = btrfs_alloc_path();
5777 if (!path) {
5778 ret = -ENOMEM;
5779 goto out;
5780 }
5781
5782 mutex_lock(&fs_devices->device_list_mutex);
5783 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5784 int item_size;
5785 struct btrfs_dev_stats_item *ptr;
5786
5787 key.objectid = 0;
5788 key.type = BTRFS_DEV_STATS_KEY;
5789 key.offset = device->devid;
5790 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
5791 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005792 __btrfs_reset_dev_stats(device);
5793 device->dev_stats_valid = 1;
5794 btrfs_release_path(path);
5795 continue;
5796 }
5797 slot = path->slots[0];
5798 eb = path->nodes[0];
5799 btrfs_item_key_to_cpu(eb, &found_key, slot);
5800 item_size = btrfs_item_size_nr(eb, slot);
5801
5802 ptr = btrfs_item_ptr(eb, slot,
5803 struct btrfs_dev_stats_item);
5804
5805 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5806 if (item_size >= (1 + i) * sizeof(__le64))
5807 btrfs_dev_stat_set(device, i,
5808 btrfs_dev_stats_value(eb, ptr, i));
5809 else
5810 btrfs_dev_stat_reset(device, i);
5811 }
5812
5813 device->dev_stats_valid = 1;
5814 btrfs_dev_stat_print_on_load(device);
5815 btrfs_release_path(path);
5816 }
5817 mutex_unlock(&fs_devices->device_list_mutex);
5818
5819out:
5820 btrfs_free_path(path);
5821 return ret < 0 ? ret : 0;
5822}
5823
5824static int update_dev_stat_item(struct btrfs_trans_handle *trans,
5825 struct btrfs_root *dev_root,
5826 struct btrfs_device *device)
5827{
5828 struct btrfs_path *path;
5829 struct btrfs_key key;
5830 struct extent_buffer *eb;
5831 struct btrfs_dev_stats_item *ptr;
5832 int ret;
5833 int i;
5834
5835 key.objectid = 0;
5836 key.type = BTRFS_DEV_STATS_KEY;
5837 key.offset = device->devid;
5838
5839 path = btrfs_alloc_path();
5840 BUG_ON(!path);
5841 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
5842 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005843 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
5844 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005845 goto out;
5846 }
5847
5848 if (ret == 0 &&
5849 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
5850 /* need to delete old one and insert a new one */
5851 ret = btrfs_del_item(trans, dev_root, path);
5852 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005853 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5854 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005855 goto out;
5856 }
5857 ret = 1;
5858 }
5859
5860 if (ret == 1) {
5861 /* need to insert a new item */
5862 btrfs_release_path(path);
5863 ret = btrfs_insert_empty_item(trans, dev_root, path,
5864 &key, sizeof(*ptr));
5865 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005866 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5867 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005868 goto out;
5869 }
5870 }
5871
5872 eb = path->nodes[0];
5873 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5874 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5875 btrfs_set_dev_stats_value(eb, ptr, i,
5876 btrfs_dev_stat_read(device, i));
5877 btrfs_mark_buffer_dirty(eb);
5878
5879out:
5880 btrfs_free_path(path);
5881 return ret;
5882}
5883
5884/*
5885 * called from commit_transaction. Writes all changed device stats to disk.
5886 */
5887int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5888 struct btrfs_fs_info *fs_info)
5889{
5890 struct btrfs_root *dev_root = fs_info->dev_root;
5891 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5892 struct btrfs_device *device;
5893 int ret = 0;
5894
5895 mutex_lock(&fs_devices->device_list_mutex);
5896 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5897 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5898 continue;
5899
5900 ret = update_dev_stat_item(trans, dev_root, device);
5901 if (!ret)
5902 device->dev_stats_dirty = 0;
5903 }
5904 mutex_unlock(&fs_devices->device_list_mutex);
5905
5906 return ret;
5907}
5908
Stefan Behrens442a4f62012-05-25 16:06:08 +02005909void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5910{
5911 btrfs_dev_stat_inc(dev, index);
5912 btrfs_dev_stat_print_on_error(dev);
5913}
5914
Eric Sandeen48a3b632013-04-25 20:41:01 +00005915static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02005916{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005917 if (!dev->dev_stats_valid)
5918 return;
Josef Bacik606686e2012-06-04 14:03:51 -04005919 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02005920 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04005921 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02005922 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5923 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5924 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5925 btrfs_dev_stat_read(dev,
5926 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5927 btrfs_dev_stat_read(dev,
5928 BTRFS_DEV_STAT_GENERATION_ERRS));
5929}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005930
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005931static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5932{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06005933 int i;
5934
5935 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5936 if (btrfs_dev_stat_read(dev, i) != 0)
5937 break;
5938 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5939 return; /* all values == 0, suppress message */
5940
Josef Bacik606686e2012-06-04 14:03:51 -04005941 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5942 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005943 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5944 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5945 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5946 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5947 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5948}
5949
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005950int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06005951 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005952{
5953 struct btrfs_device *dev;
5954 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5955 int i;
5956
5957 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005958 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005959 mutex_unlock(&fs_devices->device_list_mutex);
5960
5961 if (!dev) {
5962 printk(KERN_WARNING
5963 "btrfs: get dev_stats failed, device not found\n");
5964 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005965 } else if (!dev->dev_stats_valid) {
5966 printk(KERN_WARNING
5967 "btrfs: get dev_stats failed, not yet valid\n");
5968 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06005969 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005970 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5971 if (stats->nr_items > i)
5972 stats->values[i] =
5973 btrfs_dev_stat_read_and_reset(dev, i);
5974 else
5975 btrfs_dev_stat_reset(dev, i);
5976 }
5977 } else {
5978 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5979 if (stats->nr_items > i)
5980 stats->values[i] = btrfs_dev_stat_read(dev, i);
5981 }
5982 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
5983 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
5984 return 0;
5985}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01005986
5987int btrfs_scratch_superblock(struct btrfs_device *device)
5988{
5989 struct buffer_head *bh;
5990 struct btrfs_super_block *disk_super;
5991
5992 bh = btrfs_read_dev_super(device->bdev);
5993 if (!bh)
5994 return -EINVAL;
5995 disk_super = (struct btrfs_super_block *)bh->b_data;
5996
5997 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
5998 set_buffer_dirty(bh);
5999 sync_dirty_buffer(bh);
6000 brelse(bh);
6001
6002 return 0;
6003}