blob: 7fc774639a7879251ec32433d69b18d071d5e7fb [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
Qu Wenruode091432014-04-16 17:02:32 +08001387/*
1388 * Function to update ctime/mtime for a given device path.
1389 * Mainly used for ctime/mtime based probe like libblkid.
1390 */
1391static void update_dev_time(char *path_name)
1392{
1393 struct file *filp;
1394
1395 filp = filp_open(path_name, O_RDWR, 0);
1396 if (!filp)
1397 return;
1398 file_update_time(filp);
1399 filp_close(filp, NULL);
1400 return;
1401}
1402
Chris Masona061fc82008-05-07 11:43:44 -04001403static int btrfs_rm_dev_item(struct btrfs_root *root,
1404 struct btrfs_device *device)
1405{
1406 int ret;
1407 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001408 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001409 struct btrfs_trans_handle *trans;
1410
1411 root = root->fs_info->chunk_root;
1412
1413 path = btrfs_alloc_path();
1414 if (!path)
1415 return -ENOMEM;
1416
Yan, Zhenga22285a2010-05-16 10:48:46 -04001417 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001418 if (IS_ERR(trans)) {
1419 btrfs_free_path(path);
1420 return PTR_ERR(trans);
1421 }
Chris Masona061fc82008-05-07 11:43:44 -04001422 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1423 key.type = BTRFS_DEV_ITEM_KEY;
1424 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001425 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001426
1427 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1428 if (ret < 0)
1429 goto out;
1430
1431 if (ret > 0) {
1432 ret = -ENOENT;
1433 goto out;
1434 }
1435
1436 ret = btrfs_del_item(trans, root, path);
1437 if (ret)
1438 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001439out:
1440 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001441 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001442 btrfs_commit_transaction(trans, root);
1443 return ret;
1444}
1445
1446int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1447{
1448 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001449 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001450 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001451 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001452 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001453 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001454 u64 all_avail;
1455 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001456 u64 num_devices;
1457 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001458 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001459 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001460 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001461
Chris Masona061fc82008-05-07 11:43:44 -04001462 mutex_lock(&uuid_mutex);
1463
Miao Xiede98ced2013-01-29 10:13:12 +00001464 do {
1465 seq = read_seqbegin(&root->fs_info->profiles_lock);
1466
1467 all_avail = root->fs_info->avail_data_alloc_bits |
1468 root->fs_info->avail_system_alloc_bits |
1469 root->fs_info->avail_metadata_alloc_bits;
1470 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001471
Stefan Behrens8dabb742012-11-06 13:15:27 +01001472 num_devices = root->fs_info->fs_devices->num_devices;
1473 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1474 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1475 WARN_ON(num_devices < 1);
1476 num_devices--;
1477 }
1478 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1479
1480 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001481 printk(KERN_ERR "btrfs: unable to go below four devices "
1482 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001483 ret = -EINVAL;
1484 goto out;
1485 }
1486
Stefan Behrens8dabb742012-11-06 13:15:27 +01001487 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001488 printk(KERN_ERR "btrfs: unable to go below two "
1489 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001490 ret = -EINVAL;
1491 goto out;
1492 }
1493
David Woodhouse53b381b2013-01-29 18:40:14 -05001494 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1495 root->fs_info->fs_devices->rw_devices <= 2) {
1496 printk(KERN_ERR "btrfs: unable to go below two "
1497 "devices on raid5\n");
1498 ret = -EINVAL;
1499 goto out;
1500 }
1501 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1502 root->fs_info->fs_devices->rw_devices <= 3) {
1503 printk(KERN_ERR "btrfs: unable to go below three "
1504 "devices on raid6\n");
1505 ret = -EINVAL;
1506 goto out;
1507 }
1508
Chris Masondfe25022008-05-13 13:46:40 -04001509 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001510 struct list_head *devices;
1511 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001512
Chris Masondfe25022008-05-13 13:46:40 -04001513 device = NULL;
1514 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001515 /*
1516 * It is safe to read the devices since the volume_mutex
1517 * is held.
1518 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001519 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001520 if (tmp->in_fs_metadata &&
1521 !tmp->is_tgtdev_for_dev_replace &&
1522 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001523 device = tmp;
1524 break;
1525 }
1526 }
1527 bdev = NULL;
1528 bh = NULL;
1529 disk_super = NULL;
1530 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001531 printk(KERN_ERR "btrfs: no missing devices found to "
1532 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001533 goto out;
1534 }
Chris Masondfe25022008-05-13 13:46:40 -04001535 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001536 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001537 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001538 root->fs_info->bdev_holder, 0,
1539 &bdev, &bh);
1540 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001541 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001542 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001543 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001544 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001545 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001546 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001547 if (!device) {
1548 ret = -ENOENT;
1549 goto error_brelse;
1550 }
Chris Masondfe25022008-05-13 13:46:40 -04001551 }
Yan Zheng2b820322008-11-17 21:11:30 -05001552
Stefan Behrens63a212a2012-11-05 18:29:28 +01001553 if (device->is_tgtdev_for_dev_replace) {
1554 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1555 ret = -EINVAL;
1556 goto error_brelse;
1557 }
1558
Yan Zheng2b820322008-11-17 21:11:30 -05001559 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001560 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1561 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001562 ret = -EINVAL;
1563 goto error_brelse;
1564 }
1565
1566 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001567 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001568 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001569 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001570 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001571 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001572 }
Chris Masona061fc82008-05-07 11:43:44 -04001573
1574 ret = btrfs_shrink_device(device, 0);
1575 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001576 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001577
Stefan Behrens63a212a2012-11-05 18:29:28 +01001578 /*
1579 * TODO: the superblock still includes this device in its num_devices
1580 * counter although write_all_supers() is not locked out. This
1581 * could give a filesystem state which requires a degraded mount.
1582 */
Chris Masona061fc82008-05-07 11:43:44 -04001583 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1584 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001585 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001586
Josef Bacik2bf64752011-09-26 17:12:22 -04001587 spin_lock(&root->fs_info->free_chunk_lock);
1588 root->fs_info->free_chunk_space = device->total_bytes -
1589 device->bytes_used;
1590 spin_unlock(&root->fs_info->free_chunk_lock);
1591
Yan Zheng2b820322008-11-17 21:11:30 -05001592 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001593 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001594
1595 /*
1596 * the device list mutex makes sure that we don't change
1597 * the device list while someone else is writing out all
1598 * the device supers.
1599 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001600
1601 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001602 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001603 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001604
Yan Zhenge4404d62008-12-12 10:03:26 -05001605 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001606 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001607
Chris Masoncd02dca2010-12-13 14:56:23 -05001608 if (device->missing)
1609 root->fs_info->fs_devices->missing_devices--;
1610
Yan Zheng2b820322008-11-17 21:11:30 -05001611 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1612 struct btrfs_device, dev_list);
1613 if (device->bdev == root->fs_info->sb->s_bdev)
1614 root->fs_info->sb->s_bdev = next_device->bdev;
1615 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1616 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1617
Xiao Guangrong1f781602011-04-20 10:09:16 +00001618 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001619 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001620
1621 call_rcu(&device->rcu, free_device);
1622 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001623
David Sterba6c417612011-04-13 15:41:04 +02001624 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1625 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001626
Xiao Guangrong1f781602011-04-20 10:09:16 +00001627 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001628 struct btrfs_fs_devices *fs_devices;
1629 fs_devices = root->fs_info->fs_devices;
1630 while (fs_devices) {
Rickard Strandqvist2346e1e2014-05-22 22:43:43 +02001631 if (fs_devices->seed == cur_devices) {
1632 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001633 break;
Rickard Strandqvist2346e1e2014-05-22 22:43:43 +02001634 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001635 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001636 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001637 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001638 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001639 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001640 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001641 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001642 }
1643
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001644 root->fs_info->num_tolerated_disk_barrier_failures =
1645 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1646
Yan Zheng2b820322008-11-17 21:11:30 -05001647 /*
1648 * at this point, the device is zero sized. We want to
1649 * remove it from the devices list and zero out the old super
1650 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001651 if (clear_super && disk_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001652 /* make sure this device isn't detected as part of
1653 * the FS anymore
1654 */
1655 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1656 set_buffer_dirty(bh);
1657 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001658 }
Chris Masona061fc82008-05-07 11:43:44 -04001659
Chris Masona061fc82008-05-07 11:43:44 -04001660 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001661
Qu Wenruode091432014-04-16 17:02:32 +08001662 if (bdev) {
1663 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001664 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001665
Qu Wenruode091432014-04-16 17:02:32 +08001666 /* Update ctime/mtime for device path for libblkid */
1667 update_dev_time(device_path);
1668 }
1669
Chris Masona061fc82008-05-07 11:43:44 -04001670error_brelse:
1671 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001672 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001673 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001674out:
1675 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001676 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001677error_undo:
1678 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001679 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001680 list_add(&device->dev_alloc_list,
1681 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001682 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001683 root->fs_info->fs_devices->rw_devices++;
1684 }
1685 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001686}
1687
Stefan Behrense93c89c2012-11-05 17:33:06 +01001688void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1689 struct btrfs_device *srcdev)
1690{
1691 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1692 list_del_rcu(&srcdev->dev_list);
1693 list_del_rcu(&srcdev->dev_alloc_list);
1694 fs_info->fs_devices->num_devices--;
1695 if (srcdev->missing) {
1696 fs_info->fs_devices->missing_devices--;
1697 fs_info->fs_devices->rw_devices++;
1698 }
1699 if (srcdev->can_discard)
1700 fs_info->fs_devices->num_can_discard--;
1701 if (srcdev->bdev)
1702 fs_info->fs_devices->open_devices--;
1703
1704 call_rcu(&srcdev->rcu, free_device);
1705}
1706
1707void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1708 struct btrfs_device *tgtdev)
1709{
1710 struct btrfs_device *next_device;
1711
1712 WARN_ON(!tgtdev);
1713 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1714 if (tgtdev->bdev) {
1715 btrfs_scratch_superblock(tgtdev);
1716 fs_info->fs_devices->open_devices--;
1717 }
1718 fs_info->fs_devices->num_devices--;
1719 if (tgtdev->can_discard)
1720 fs_info->fs_devices->num_can_discard++;
1721
1722 next_device = list_entry(fs_info->fs_devices->devices.next,
1723 struct btrfs_device, dev_list);
1724 if (tgtdev->bdev == fs_info->sb->s_bdev)
1725 fs_info->sb->s_bdev = next_device->bdev;
1726 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1727 fs_info->fs_devices->latest_bdev = next_device->bdev;
1728 list_del_rcu(&tgtdev->dev_list);
1729
1730 call_rcu(&tgtdev->rcu, free_device);
1731
1732 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1733}
1734
Eric Sandeen48a3b632013-04-25 20:41:01 +00001735static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1736 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001737{
1738 int ret = 0;
1739 struct btrfs_super_block *disk_super;
1740 u64 devid;
1741 u8 *dev_uuid;
1742 struct block_device *bdev;
1743 struct buffer_head *bh;
1744
1745 *device = NULL;
1746 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1747 root->fs_info->bdev_holder, 0, &bdev, &bh);
1748 if (ret)
1749 return ret;
1750 disk_super = (struct btrfs_super_block *)bh->b_data;
1751 devid = btrfs_stack_device_id(&disk_super->dev_item);
1752 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001753 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001754 disk_super->fsid);
1755 brelse(bh);
1756 if (!*device)
1757 ret = -ENOENT;
1758 blkdev_put(bdev, FMODE_READ);
1759 return ret;
1760}
1761
1762int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1763 char *device_path,
1764 struct btrfs_device **device)
1765{
1766 *device = NULL;
1767 if (strcmp(device_path, "missing") == 0) {
1768 struct list_head *devices;
1769 struct btrfs_device *tmp;
1770
1771 devices = &root->fs_info->fs_devices->devices;
1772 /*
1773 * It is safe to read the devices since the volume_mutex
1774 * is held by the caller.
1775 */
1776 list_for_each_entry(tmp, devices, dev_list) {
1777 if (tmp->in_fs_metadata && !tmp->bdev) {
1778 *device = tmp;
1779 break;
1780 }
1781 }
1782
1783 if (!*device) {
1784 pr_err("btrfs: no missing device found\n");
1785 return -ENOENT;
1786 }
1787
1788 return 0;
1789 } else {
1790 return btrfs_find_device_by_path(root, device_path, device);
1791 }
1792}
1793
Yan Zheng2b820322008-11-17 21:11:30 -05001794/*
1795 * does all the dirty work required for changing file system's UUID.
1796 */
Li Zefan125ccb02011-12-08 15:07:24 +08001797static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001798{
1799 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1800 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001801 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001802 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001803 struct btrfs_device *device;
1804 u64 super_flags;
1805
1806 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001807 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001808 return -EINVAL;
1809
Yan Zhenge4404d62008-12-12 10:03:26 -05001810 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1811 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001812 return -ENOMEM;
1813
Yan Zhenge4404d62008-12-12 10:03:26 -05001814 old_devices = clone_fs_devices(fs_devices);
1815 if (IS_ERR(old_devices)) {
1816 kfree(seed_devices);
1817 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001818 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001819
Yan Zheng2b820322008-11-17 21:11:30 -05001820 list_add(&old_devices->list, &fs_uuids);
1821
Yan Zhenge4404d62008-12-12 10:03:26 -05001822 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1823 seed_devices->opened = 1;
1824 INIT_LIST_HEAD(&seed_devices->devices);
1825 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001826 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001827
1828 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001829 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1830 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001831 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1832
Yan Zhenge4404d62008-12-12 10:03:26 -05001833 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1834 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1835 device->fs_devices = seed_devices;
1836 }
1837
Yan Zheng2b820322008-11-17 21:11:30 -05001838 fs_devices->seeding = 0;
1839 fs_devices->num_devices = 0;
1840 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001841 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001842
1843 generate_random_uuid(fs_devices->fsid);
1844 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1845 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1846 super_flags = btrfs_super_flags(disk_super) &
1847 ~BTRFS_SUPER_FLAG_SEEDING;
1848 btrfs_set_super_flags(disk_super, super_flags);
1849
1850 return 0;
1851}
1852
1853/*
1854 * strore the expected generation for seed devices in device items.
1855 */
1856static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1857 struct btrfs_root *root)
1858{
1859 struct btrfs_path *path;
1860 struct extent_buffer *leaf;
1861 struct btrfs_dev_item *dev_item;
1862 struct btrfs_device *device;
1863 struct btrfs_key key;
1864 u8 fs_uuid[BTRFS_UUID_SIZE];
1865 u8 dev_uuid[BTRFS_UUID_SIZE];
1866 u64 devid;
1867 int ret;
1868
1869 path = btrfs_alloc_path();
1870 if (!path)
1871 return -ENOMEM;
1872
1873 root = root->fs_info->chunk_root;
1874 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1875 key.offset = 0;
1876 key.type = BTRFS_DEV_ITEM_KEY;
1877
1878 while (1) {
1879 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1880 if (ret < 0)
1881 goto error;
1882
1883 leaf = path->nodes[0];
1884next_slot:
1885 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1886 ret = btrfs_next_leaf(root, path);
1887 if (ret > 0)
1888 break;
1889 if (ret < 0)
1890 goto error;
1891 leaf = path->nodes[0];
1892 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001893 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001894 continue;
1895 }
1896
1897 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1898 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1899 key.type != BTRFS_DEV_ITEM_KEY)
1900 break;
1901
1902 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1903 struct btrfs_dev_item);
1904 devid = btrfs_device_id(leaf, dev_item);
1905 read_extent_buffer(leaf, dev_uuid,
1906 (unsigned long)btrfs_device_uuid(dev_item),
1907 BTRFS_UUID_SIZE);
1908 read_extent_buffer(leaf, fs_uuid,
1909 (unsigned long)btrfs_device_fsid(dev_item),
1910 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001911 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1912 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001913 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001914
1915 if (device->fs_devices->seeding) {
1916 btrfs_set_device_generation(leaf, dev_item,
1917 device->generation);
1918 btrfs_mark_buffer_dirty(leaf);
1919 }
1920
1921 path->slots[0]++;
1922 goto next_slot;
1923 }
1924 ret = 0;
1925error:
1926 btrfs_free_path(path);
1927 return ret;
1928}
1929
Chris Mason788f20e2008-04-28 15:29:42 -04001930int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1931{
Josef Bacikd5e20032011-08-04 14:52:27 +00001932 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001933 struct btrfs_trans_handle *trans;
1934 struct btrfs_device *device;
1935 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001936 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001937 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001938 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001939 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001940 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001941 int ret = 0;
1942
Yan Zheng2b820322008-11-17 21:11:30 -05001943 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001944 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001945
Li Zefana5d16332011-12-07 20:08:40 -05001946 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001947 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001948 if (IS_ERR(bdev))
1949 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001950
Yan Zheng2b820322008-11-17 21:11:30 -05001951 if (root->fs_info->fs_devices->seeding) {
1952 seeding_dev = 1;
1953 down_write(&sb->s_umount);
1954 mutex_lock(&uuid_mutex);
1955 }
1956
Chris Mason8c8bee12008-09-29 11:19:10 -04001957 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001958
Chris Mason788f20e2008-04-28 15:29:42 -04001959 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00001960
1961 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001962 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001963 if (device->bdev == bdev) {
1964 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00001965 mutex_unlock(
1966 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001967 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001968 }
1969 }
Liu Bod25628b2012-11-14 14:35:30 +00001970 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001971
1972 device = kzalloc(sizeof(*device), GFP_NOFS);
1973 if (!device) {
1974 /* we can safely leave the fs_devices entry around */
1975 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001976 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001977 }
1978
Josef Bacik606686e2012-06-04 14:03:51 -04001979 name = rcu_string_strdup(device_path, GFP_NOFS);
1980 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001981 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001982 ret = -ENOMEM;
1983 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001984 }
Josef Bacik606686e2012-06-04 14:03:51 -04001985 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001986
1987 ret = find_next_devid(root, &device->devid);
1988 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001989 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001990 kfree(device);
1991 goto error;
1992 }
1993
Yan, Zhenga22285a2010-05-16 10:48:46 -04001994 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001995 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001996 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001997 kfree(device);
1998 ret = PTR_ERR(trans);
1999 goto error;
2000 }
2001
Yan Zheng2b820322008-11-17 21:11:30 -05002002 lock_chunks(root);
2003
Josef Bacikd5e20032011-08-04 14:52:27 +00002004 q = bdev_get_queue(bdev);
2005 if (blk_queue_discard(q))
2006 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002007 device->writeable = 1;
2008 device->work.func = pending_bios_fn;
2009 generate_random_uuid(device->uuid);
2010 spin_lock_init(&device->io_lock);
2011 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002012 device->io_width = root->sectorsize;
2013 device->io_align = root->sectorsize;
2014 device->sector_size = root->sectorsize;
2015 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002016 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002017 device->dev_root = root->fs_info->dev_root;
2018 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002019 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002020 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002021 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002022 set_blocksize(device->bdev, 4096);
2023
Yan Zheng2b820322008-11-17 21:11:30 -05002024 if (seeding_dev) {
2025 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002026 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002027 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002028 }
2029
2030 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002031
Chris Masone5e9a522009-06-10 15:17:02 -04002032 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002033 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002034 list_add(&device->dev_alloc_list,
2035 &root->fs_info->fs_devices->alloc_list);
2036 root->fs_info->fs_devices->num_devices++;
2037 root->fs_info->fs_devices->open_devices++;
2038 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002039 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002040 if (device->can_discard)
2041 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002042 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2043
Josef Bacik2bf64752011-09-26 17:12:22 -04002044 spin_lock(&root->fs_info->free_chunk_lock);
2045 root->fs_info->free_chunk_space += device->total_bytes;
2046 spin_unlock(&root->fs_info->free_chunk_lock);
2047
Chris Masonc2898112009-06-10 09:51:32 -04002048 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2049 root->fs_info->fs_devices->rotating = 1;
2050
David Sterba6c417612011-04-13 15:41:04 +02002051 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2052 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002053 total_bytes + device->total_bytes);
2054
David Sterba6c417612011-04-13 15:41:04 +02002055 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2056 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002057 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04002058 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002059
Yan Zheng2b820322008-11-17 21:11:30 -05002060 if (seeding_dev) {
2061 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002062 if (ret) {
2063 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002064 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002065 }
Yan Zheng2b820322008-11-17 21:11:30 -05002066 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002067 if (ret) {
2068 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002069 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002070 }
Yan Zheng2b820322008-11-17 21:11:30 -05002071 } else {
2072 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002073 if (ret) {
2074 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002075 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002076 }
Yan Zheng2b820322008-11-17 21:11:30 -05002077 }
2078
Chris Mason913d9522009-03-10 13:17:18 -04002079 /*
2080 * we've got more storage, clear any full flags on the space
2081 * infos
2082 */
2083 btrfs_clear_space_info_full(root->fs_info);
2084
Chris Mason7d9eb122008-07-08 14:19:17 -04002085 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002086 root->fs_info->num_tolerated_disk_barrier_failures =
2087 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002088 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002089
2090 if (seeding_dev) {
2091 mutex_unlock(&uuid_mutex);
2092 up_write(&sb->s_umount);
2093
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002094 if (ret) /* transaction commit */
2095 return ret;
2096
Yan Zheng2b820322008-11-17 21:11:30 -05002097 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002098 if (ret < 0)
2099 btrfs_error(root->fs_info, ret,
2100 "Failed to relocate sys chunks after "
2101 "device initialization. This can be fixed "
2102 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002103 trans = btrfs_attach_transaction(root);
2104 if (IS_ERR(trans)) {
2105 if (PTR_ERR(trans) == -ENOENT)
2106 return 0;
2107 return PTR_ERR(trans);
2108 }
2109 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002110 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002111
Qu Wenruode091432014-04-16 17:02:32 +08002112 /* Update ctime/mtime for libblkid */
2113 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002114 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002115
2116error_trans:
2117 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002118 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002119 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002120 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002121error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002122 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002123 if (seeding_dev) {
2124 mutex_unlock(&uuid_mutex);
2125 up_write(&sb->s_umount);
2126 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002127 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002128}
2129
Stefan Behrense93c89c2012-11-05 17:33:06 +01002130int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2131 struct btrfs_device **device_out)
2132{
2133 struct request_queue *q;
2134 struct btrfs_device *device;
2135 struct block_device *bdev;
2136 struct btrfs_fs_info *fs_info = root->fs_info;
2137 struct list_head *devices;
2138 struct rcu_string *name;
2139 int ret = 0;
2140
2141 *device_out = NULL;
2142 if (fs_info->fs_devices->seeding)
2143 return -EINVAL;
2144
2145 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2146 fs_info->bdev_holder);
2147 if (IS_ERR(bdev))
2148 return PTR_ERR(bdev);
2149
2150 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2151
2152 devices = &fs_info->fs_devices->devices;
2153 list_for_each_entry(device, devices, dev_list) {
2154 if (device->bdev == bdev) {
2155 ret = -EEXIST;
2156 goto error;
2157 }
2158 }
2159
2160 device = kzalloc(sizeof(*device), GFP_NOFS);
2161 if (!device) {
2162 ret = -ENOMEM;
2163 goto error;
2164 }
2165
2166 name = rcu_string_strdup(device_path, GFP_NOFS);
2167 if (!name) {
2168 kfree(device);
2169 ret = -ENOMEM;
2170 goto error;
2171 }
2172 rcu_assign_pointer(device->name, name);
2173
2174 q = bdev_get_queue(bdev);
2175 if (blk_queue_discard(q))
2176 device->can_discard = 1;
2177 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2178 device->writeable = 1;
2179 device->work.func = pending_bios_fn;
2180 generate_random_uuid(device->uuid);
2181 device->devid = BTRFS_DEV_REPLACE_DEVID;
2182 spin_lock_init(&device->io_lock);
2183 device->generation = 0;
2184 device->io_width = root->sectorsize;
2185 device->io_align = root->sectorsize;
2186 device->sector_size = root->sectorsize;
2187 device->total_bytes = i_size_read(bdev->bd_inode);
2188 device->disk_total_bytes = device->total_bytes;
2189 device->dev_root = fs_info->dev_root;
2190 device->bdev = bdev;
2191 device->in_fs_metadata = 1;
2192 device->is_tgtdev_for_dev_replace = 1;
2193 device->mode = FMODE_EXCL;
2194 set_blocksize(device->bdev, 4096);
2195 device->fs_devices = fs_info->fs_devices;
2196 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2197 fs_info->fs_devices->num_devices++;
2198 fs_info->fs_devices->open_devices++;
2199 if (device->can_discard)
2200 fs_info->fs_devices->num_can_discard++;
2201 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2202
2203 *device_out = device;
2204 return ret;
2205
2206error:
2207 blkdev_put(bdev, FMODE_EXCL);
2208 return ret;
2209}
2210
2211void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2212 struct btrfs_device *tgtdev)
2213{
2214 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2215 tgtdev->io_width = fs_info->dev_root->sectorsize;
2216 tgtdev->io_align = fs_info->dev_root->sectorsize;
2217 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2218 tgtdev->dev_root = fs_info->dev_root;
2219 tgtdev->in_fs_metadata = 1;
2220}
2221
Chris Masond3977122009-01-05 21:25:51 -05002222static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2223 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002224{
2225 int ret;
2226 struct btrfs_path *path;
2227 struct btrfs_root *root;
2228 struct btrfs_dev_item *dev_item;
2229 struct extent_buffer *leaf;
2230 struct btrfs_key key;
2231
2232 root = device->dev_root->fs_info->chunk_root;
2233
2234 path = btrfs_alloc_path();
2235 if (!path)
2236 return -ENOMEM;
2237
2238 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2239 key.type = BTRFS_DEV_ITEM_KEY;
2240 key.offset = device->devid;
2241
2242 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2243 if (ret < 0)
2244 goto out;
2245
2246 if (ret > 0) {
2247 ret = -ENOENT;
2248 goto out;
2249 }
2250
2251 leaf = path->nodes[0];
2252 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2253
2254 btrfs_set_device_id(leaf, dev_item, device->devid);
2255 btrfs_set_device_type(leaf, dev_item, device->type);
2256 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2257 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2258 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002259 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002260 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2261 btrfs_mark_buffer_dirty(leaf);
2262
2263out:
2264 btrfs_free_path(path);
2265 return ret;
2266}
2267
Chris Mason7d9eb122008-07-08 14:19:17 -04002268static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002269 struct btrfs_device *device, u64 new_size)
2270{
2271 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002272 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002273 u64 old_total = btrfs_super_total_bytes(super_copy);
2274 u64 diff = new_size - device->total_bytes;
2275
Yan Zheng2b820322008-11-17 21:11:30 -05002276 if (!device->writeable)
2277 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002278 if (new_size <= device->total_bytes ||
2279 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002280 return -EINVAL;
2281
Chris Mason8f18cf12008-04-25 16:53:30 -04002282 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002283 device->fs_devices->total_rw_bytes += diff;
2284
2285 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002286 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002287 btrfs_clear_space_info_full(device->dev_root->fs_info);
2288
Chris Mason8f18cf12008-04-25 16:53:30 -04002289 return btrfs_update_device(trans, device);
2290}
2291
Chris Mason7d9eb122008-07-08 14:19:17 -04002292int btrfs_grow_device(struct btrfs_trans_handle *trans,
2293 struct btrfs_device *device, u64 new_size)
2294{
2295 int ret;
2296 lock_chunks(device->dev_root);
2297 ret = __btrfs_grow_device(trans, device, new_size);
2298 unlock_chunks(device->dev_root);
2299 return ret;
2300}
2301
Chris Mason8f18cf12008-04-25 16:53:30 -04002302static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2303 struct btrfs_root *root,
2304 u64 chunk_tree, u64 chunk_objectid,
2305 u64 chunk_offset)
2306{
2307 int ret;
2308 struct btrfs_path *path;
2309 struct btrfs_key key;
2310
2311 root = root->fs_info->chunk_root;
2312 path = btrfs_alloc_path();
2313 if (!path)
2314 return -ENOMEM;
2315
2316 key.objectid = chunk_objectid;
2317 key.offset = chunk_offset;
2318 key.type = BTRFS_CHUNK_ITEM_KEY;
2319
2320 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002321 if (ret < 0)
2322 goto out;
2323 else if (ret > 0) { /* Logic error or corruption */
2324 btrfs_error(root->fs_info, -ENOENT,
2325 "Failed lookup while freeing chunk.");
2326 ret = -ENOENT;
2327 goto out;
2328 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002329
2330 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002331 if (ret < 0)
2332 btrfs_error(root->fs_info, ret,
2333 "Failed to delete chunk item.");
2334out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002335 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002336 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002337}
2338
Christoph Hellwigb2950862008-12-02 09:54:17 -05002339static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002340 chunk_offset)
2341{
David Sterba6c417612011-04-13 15:41:04 +02002342 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002343 struct btrfs_disk_key *disk_key;
2344 struct btrfs_chunk *chunk;
2345 u8 *ptr;
2346 int ret = 0;
2347 u32 num_stripes;
2348 u32 array_size;
2349 u32 len = 0;
2350 u32 cur;
2351 struct btrfs_key key;
2352
2353 array_size = btrfs_super_sys_array_size(super_copy);
2354
2355 ptr = super_copy->sys_chunk_array;
2356 cur = 0;
2357
2358 while (cur < array_size) {
2359 disk_key = (struct btrfs_disk_key *)ptr;
2360 btrfs_disk_key_to_cpu(&key, disk_key);
2361
2362 len = sizeof(*disk_key);
2363
2364 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2365 chunk = (struct btrfs_chunk *)(ptr + len);
2366 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2367 len += btrfs_chunk_item_size(num_stripes);
2368 } else {
2369 ret = -EIO;
2370 break;
2371 }
2372 if (key.objectid == chunk_objectid &&
2373 key.offset == chunk_offset) {
2374 memmove(ptr, ptr + len, array_size - (cur + len));
2375 array_size -= len;
2376 btrfs_set_super_sys_array_size(super_copy, array_size);
2377 } else {
2378 ptr += len;
2379 cur += len;
2380 }
2381 }
2382 return ret;
2383}
2384
Christoph Hellwigb2950862008-12-02 09:54:17 -05002385static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002386 u64 chunk_tree, u64 chunk_objectid,
2387 u64 chunk_offset)
2388{
2389 struct extent_map_tree *em_tree;
2390 struct btrfs_root *extent_root;
2391 struct btrfs_trans_handle *trans;
2392 struct extent_map *em;
2393 struct map_lookup *map;
2394 int ret;
2395 int i;
2396
2397 root = root->fs_info->chunk_root;
2398 extent_root = root->fs_info->extent_root;
2399 em_tree = &root->fs_info->mapping_tree.map_tree;
2400
Josef Bacikba1bf482009-09-11 16:11:19 -04002401 ret = btrfs_can_relocate(extent_root, chunk_offset);
2402 if (ret)
2403 return -ENOSPC;
2404
Chris Mason8f18cf12008-04-25 16:53:30 -04002405 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002406 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002407 if (ret)
2408 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002409
Yan, Zhenga22285a2010-05-16 10:48:46 -04002410 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002411 if (IS_ERR(trans)) {
2412 ret = PTR_ERR(trans);
2413 btrfs_std_error(root->fs_info, ret);
2414 return ret;
2415 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002416
Chris Mason7d9eb122008-07-08 14:19:17 -04002417 lock_chunks(root);
2418
Chris Mason8f18cf12008-04-25 16:53:30 -04002419 /*
2420 * step two, delete the device extents and the
2421 * chunk tree entries
2422 */
Chris Mason890871b2009-09-02 16:24:52 -04002423 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002424 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002425 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002426
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002427 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002428 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002429 map = (struct map_lookup *)em->bdev;
2430
2431 for (i = 0; i < map->num_stripes; i++) {
2432 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2433 map->stripes[i].physical);
2434 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002435
Chris Masondfe25022008-05-13 13:46:40 -04002436 if (map->stripes[i].dev) {
2437 ret = btrfs_update_device(trans, map->stripes[i].dev);
2438 BUG_ON(ret);
2439 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002440 }
2441 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2442 chunk_offset);
2443
2444 BUG_ON(ret);
2445
liubo1abe9b82011-03-24 11:18:59 +00002446 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2447
Chris Mason8f18cf12008-04-25 16:53:30 -04002448 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2449 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2450 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002451 }
2452
Zheng Yan1a40e232008-09-26 10:09:34 -04002453 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2454 BUG_ON(ret);
2455
Chris Mason890871b2009-09-02 16:24:52 -04002456 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002457 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002458 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002459
Chris Mason8f18cf12008-04-25 16:53:30 -04002460 kfree(map);
2461 em->bdev = NULL;
2462
2463 /* once for the tree */
2464 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002465 /* once for us */
2466 free_extent_map(em);
2467
Chris Mason7d9eb122008-07-08 14:19:17 -04002468 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002469 btrfs_end_transaction(trans, root);
2470 return 0;
2471}
2472
Yan Zheng2b820322008-11-17 21:11:30 -05002473static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2474{
2475 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2476 struct btrfs_path *path;
2477 struct extent_buffer *leaf;
2478 struct btrfs_chunk *chunk;
2479 struct btrfs_key key;
2480 struct btrfs_key found_key;
2481 u64 chunk_tree = chunk_root->root_key.objectid;
2482 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002483 bool retried = false;
2484 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002485 int ret;
2486
2487 path = btrfs_alloc_path();
2488 if (!path)
2489 return -ENOMEM;
2490
Josef Bacikba1bf482009-09-11 16:11:19 -04002491again:
Yan Zheng2b820322008-11-17 21:11:30 -05002492 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2493 key.offset = (u64)-1;
2494 key.type = BTRFS_CHUNK_ITEM_KEY;
2495
2496 while (1) {
2497 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2498 if (ret < 0)
2499 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002500 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002501
2502 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2503 key.type);
2504 if (ret < 0)
2505 goto error;
2506 if (ret > 0)
2507 break;
2508
2509 leaf = path->nodes[0];
2510 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2511
2512 chunk = btrfs_item_ptr(leaf, path->slots[0],
2513 struct btrfs_chunk);
2514 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002515 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002516
2517 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2518 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2519 found_key.objectid,
2520 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002521 if (ret == -ENOSPC)
2522 failed++;
2523 else if (ret)
2524 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002525 }
2526
2527 if (found_key.offset == 0)
2528 break;
2529 key.offset = found_key.offset - 1;
2530 }
2531 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002532 if (failed && !retried) {
2533 failed = 0;
2534 retried = true;
2535 goto again;
2536 } else if (failed && retried) {
2537 WARN_ON(1);
2538 ret = -ENOSPC;
2539 }
Yan Zheng2b820322008-11-17 21:11:30 -05002540error:
2541 btrfs_free_path(path);
2542 return ret;
2543}
2544
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002545static int insert_balance_item(struct btrfs_root *root,
2546 struct btrfs_balance_control *bctl)
2547{
2548 struct btrfs_trans_handle *trans;
2549 struct btrfs_balance_item *item;
2550 struct btrfs_disk_balance_args disk_bargs;
2551 struct btrfs_path *path;
2552 struct extent_buffer *leaf;
2553 struct btrfs_key key;
2554 int ret, err;
2555
2556 path = btrfs_alloc_path();
2557 if (!path)
2558 return -ENOMEM;
2559
2560 trans = btrfs_start_transaction(root, 0);
2561 if (IS_ERR(trans)) {
2562 btrfs_free_path(path);
2563 return PTR_ERR(trans);
2564 }
2565
2566 key.objectid = BTRFS_BALANCE_OBJECTID;
2567 key.type = BTRFS_BALANCE_ITEM_KEY;
2568 key.offset = 0;
2569
2570 ret = btrfs_insert_empty_item(trans, root, path, &key,
2571 sizeof(*item));
2572 if (ret)
2573 goto out;
2574
2575 leaf = path->nodes[0];
2576 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2577
2578 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2579
2580 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2581 btrfs_set_balance_data(leaf, item, &disk_bargs);
2582 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2583 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2584 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2585 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2586
2587 btrfs_set_balance_flags(leaf, item, bctl->flags);
2588
2589 btrfs_mark_buffer_dirty(leaf);
2590out:
2591 btrfs_free_path(path);
2592 err = btrfs_commit_transaction(trans, root);
2593 if (err && !ret)
2594 ret = err;
2595 return ret;
2596}
2597
2598static int del_balance_item(struct btrfs_root *root)
2599{
2600 struct btrfs_trans_handle *trans;
2601 struct btrfs_path *path;
2602 struct btrfs_key key;
2603 int ret, err;
2604
2605 path = btrfs_alloc_path();
2606 if (!path)
2607 return -ENOMEM;
2608
2609 trans = btrfs_start_transaction(root, 0);
2610 if (IS_ERR(trans)) {
2611 btrfs_free_path(path);
2612 return PTR_ERR(trans);
2613 }
2614
2615 key.objectid = BTRFS_BALANCE_OBJECTID;
2616 key.type = BTRFS_BALANCE_ITEM_KEY;
2617 key.offset = 0;
2618
2619 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2620 if (ret < 0)
2621 goto out;
2622 if (ret > 0) {
2623 ret = -ENOENT;
2624 goto out;
2625 }
2626
2627 ret = btrfs_del_item(trans, root, path);
2628out:
2629 btrfs_free_path(path);
2630 err = btrfs_commit_transaction(trans, root);
2631 if (err && !ret)
2632 ret = err;
2633 return ret;
2634}
2635
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002636/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002637 * This is a heuristic used to reduce the number of chunks balanced on
2638 * resume after balance was interrupted.
2639 */
2640static void update_balance_args(struct btrfs_balance_control *bctl)
2641{
2642 /*
2643 * Turn on soft mode for chunk types that were being converted.
2644 */
2645 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2646 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2647 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2648 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2649 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2650 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2651
2652 /*
2653 * Turn on usage filter if is not already used. The idea is
2654 * that chunks that we have already balanced should be
2655 * reasonably full. Don't do it for chunks that are being
2656 * converted - that will keep us from relocating unconverted
2657 * (albeit full) chunks.
2658 */
2659 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2660 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2661 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2662 bctl->data.usage = 90;
2663 }
2664 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2665 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2666 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2667 bctl->sys.usage = 90;
2668 }
2669 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2670 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2671 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2672 bctl->meta.usage = 90;
2673 }
2674}
2675
2676/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002677 * Should be called with both balance and volume mutexes held to
2678 * serialize other volume operations (add_dev/rm_dev/resize) with
2679 * restriper. Same goes for unset_balance_control.
2680 */
2681static void set_balance_control(struct btrfs_balance_control *bctl)
2682{
2683 struct btrfs_fs_info *fs_info = bctl->fs_info;
2684
2685 BUG_ON(fs_info->balance_ctl);
2686
2687 spin_lock(&fs_info->balance_lock);
2688 fs_info->balance_ctl = bctl;
2689 spin_unlock(&fs_info->balance_lock);
2690}
2691
2692static void unset_balance_control(struct btrfs_fs_info *fs_info)
2693{
2694 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2695
2696 BUG_ON(!fs_info->balance_ctl);
2697
2698 spin_lock(&fs_info->balance_lock);
2699 fs_info->balance_ctl = NULL;
2700 spin_unlock(&fs_info->balance_lock);
2701
2702 kfree(bctl);
2703}
2704
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002705/*
2706 * Balance filters. Return 1 if chunk should be filtered out
2707 * (should not be balanced).
2708 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002709static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002710 struct btrfs_balance_args *bargs)
2711{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002712 chunk_type = chunk_to_extended(chunk_type) &
2713 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002714
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002715 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002716 return 0;
2717
2718 return 1;
2719}
2720
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002721static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2722 struct btrfs_balance_args *bargs)
2723{
2724 struct btrfs_block_group_cache *cache;
2725 u64 chunk_used, user_thresh;
2726 int ret = 1;
2727
2728 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2729 chunk_used = btrfs_block_group_used(&cache->item);
2730
Ilya Dryomova105bb82013-01-21 15:15:56 +02002731 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002732 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002733 else if (bargs->usage > 100)
2734 user_thresh = cache->key.offset;
2735 else
2736 user_thresh = div_factor_fine(cache->key.offset,
2737 bargs->usage);
2738
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002739 if (chunk_used < user_thresh)
2740 ret = 0;
2741
2742 btrfs_put_block_group(cache);
2743 return ret;
2744}
2745
Ilya Dryomov409d4042012-01-16 22:04:47 +02002746static int chunk_devid_filter(struct extent_buffer *leaf,
2747 struct btrfs_chunk *chunk,
2748 struct btrfs_balance_args *bargs)
2749{
2750 struct btrfs_stripe *stripe;
2751 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2752 int i;
2753
2754 for (i = 0; i < num_stripes; i++) {
2755 stripe = btrfs_stripe_nr(chunk, i);
2756 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2757 return 0;
2758 }
2759
2760 return 1;
2761}
2762
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002763/* [pstart, pend) */
2764static int chunk_drange_filter(struct extent_buffer *leaf,
2765 struct btrfs_chunk *chunk,
2766 u64 chunk_offset,
2767 struct btrfs_balance_args *bargs)
2768{
2769 struct btrfs_stripe *stripe;
2770 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2771 u64 stripe_offset;
2772 u64 stripe_length;
2773 int factor;
2774 int i;
2775
2776 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2777 return 0;
2778
2779 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002780 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2781 factor = num_stripes / 2;
2782 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2783 factor = num_stripes - 1;
2784 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2785 factor = num_stripes - 2;
2786 } else {
2787 factor = num_stripes;
2788 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002789
2790 for (i = 0; i < num_stripes; i++) {
2791 stripe = btrfs_stripe_nr(chunk, i);
2792 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2793 continue;
2794
2795 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2796 stripe_length = btrfs_chunk_length(leaf, chunk);
2797 do_div(stripe_length, factor);
2798
2799 if (stripe_offset < bargs->pend &&
2800 stripe_offset + stripe_length > bargs->pstart)
2801 return 0;
2802 }
2803
2804 return 1;
2805}
2806
Ilya Dryomovea671762012-01-16 22:04:48 +02002807/* [vstart, vend) */
2808static int chunk_vrange_filter(struct extent_buffer *leaf,
2809 struct btrfs_chunk *chunk,
2810 u64 chunk_offset,
2811 struct btrfs_balance_args *bargs)
2812{
2813 if (chunk_offset < bargs->vend &&
2814 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2815 /* at least part of the chunk is inside this vrange */
2816 return 0;
2817
2818 return 1;
2819}
2820
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002821static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002822 struct btrfs_balance_args *bargs)
2823{
2824 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2825 return 0;
2826
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002827 chunk_type = chunk_to_extended(chunk_type) &
2828 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002829
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002830 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002831 return 1;
2832
2833 return 0;
2834}
2835
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002836static int should_balance_chunk(struct btrfs_root *root,
2837 struct extent_buffer *leaf,
2838 struct btrfs_chunk *chunk, u64 chunk_offset)
2839{
2840 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2841 struct btrfs_balance_args *bargs = NULL;
2842 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2843
2844 /* type filter */
2845 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2846 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2847 return 0;
2848 }
2849
2850 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2851 bargs = &bctl->data;
2852 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2853 bargs = &bctl->sys;
2854 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2855 bargs = &bctl->meta;
2856
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002857 /* profiles filter */
2858 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2859 chunk_profiles_filter(chunk_type, bargs)) {
2860 return 0;
2861 }
2862
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002863 /* usage filter */
2864 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2865 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2866 return 0;
2867 }
2868
Ilya Dryomov409d4042012-01-16 22:04:47 +02002869 /* devid filter */
2870 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2871 chunk_devid_filter(leaf, chunk, bargs)) {
2872 return 0;
2873 }
2874
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002875 /* drange filter, makes sense only with devid filter */
2876 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2877 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2878 return 0;
2879 }
2880
Ilya Dryomovea671762012-01-16 22:04:48 +02002881 /* vrange filter */
2882 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2883 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2884 return 0;
2885 }
2886
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002887 /* soft profile changing mode */
2888 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2889 chunk_soft_convert_filter(chunk_type, bargs)) {
2890 return 0;
2891 }
2892
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002893 return 1;
2894}
2895
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002896static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002897{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002898 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002899 struct btrfs_root *chunk_root = fs_info->chunk_root;
2900 struct btrfs_root *dev_root = fs_info->dev_root;
2901 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002902 struct btrfs_device *device;
2903 u64 old_size;
2904 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002905 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002906 struct btrfs_path *path;
2907 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002908 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002909 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002910 struct extent_buffer *leaf;
2911 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002912 int ret;
2913 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002914 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002915
Chris Masonec44a352008-04-28 15:29:52 -04002916 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002917 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002918 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002919 old_size = device->total_bytes;
2920 size_to_free = div_factor(old_size, 1);
2921 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002922 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01002923 device->total_bytes - device->bytes_used > size_to_free ||
2924 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04002925 continue;
2926
2927 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002928 if (ret == -ENOSPC)
2929 break;
Chris Masonec44a352008-04-28 15:29:52 -04002930 BUG_ON(ret);
2931
Yan, Zhenga22285a2010-05-16 10:48:46 -04002932 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002933 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002934
2935 ret = btrfs_grow_device(trans, device, old_size);
2936 BUG_ON(ret);
2937
2938 btrfs_end_transaction(trans, dev_root);
2939 }
2940
2941 /* step two, relocate all the chunks */
2942 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002943 if (!path) {
2944 ret = -ENOMEM;
2945 goto error;
2946 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002947
2948 /* zero out stat counters */
2949 spin_lock(&fs_info->balance_lock);
2950 memset(&bctl->stat, 0, sizeof(bctl->stat));
2951 spin_unlock(&fs_info->balance_lock);
2952again:
Chris Masonec44a352008-04-28 15:29:52 -04002953 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2954 key.offset = (u64)-1;
2955 key.type = BTRFS_CHUNK_ITEM_KEY;
2956
Chris Masond3977122009-01-05 21:25:51 -05002957 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002958 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002959 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002960 ret = -ECANCELED;
2961 goto error;
2962 }
2963
Chris Masonec44a352008-04-28 15:29:52 -04002964 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2965 if (ret < 0)
2966 goto error;
2967
2968 /*
2969 * this shouldn't happen, it means the last relocate
2970 * failed
2971 */
2972 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002973 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002974
2975 ret = btrfs_previous_item(chunk_root, path, 0,
2976 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002977 if (ret) {
2978 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002979 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002980 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002981
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002982 leaf = path->nodes[0];
2983 slot = path->slots[0];
2984 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2985
Chris Masonec44a352008-04-28 15:29:52 -04002986 if (found_key.objectid != key.objectid)
2987 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002988
Chris Masonec44a352008-04-28 15:29:52 -04002989 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002990 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002991 break;
2992
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002993 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2994
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002995 if (!counting) {
2996 spin_lock(&fs_info->balance_lock);
2997 bctl->stat.considered++;
2998 spin_unlock(&fs_info->balance_lock);
2999 }
3000
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003001 ret = should_balance_chunk(chunk_root, leaf, chunk,
3002 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003003 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003004 if (!ret)
3005 goto loop;
3006
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003007 if (counting) {
3008 spin_lock(&fs_info->balance_lock);
3009 bctl->stat.expected++;
3010 spin_unlock(&fs_info->balance_lock);
3011 goto loop;
3012 }
3013
Chris Masonec44a352008-04-28 15:29:52 -04003014 ret = btrfs_relocate_chunk(chunk_root,
3015 chunk_root->root_key.objectid,
3016 found_key.objectid,
3017 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003018 if (ret && ret != -ENOSPC)
3019 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003020 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003021 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003022 } else {
3023 spin_lock(&fs_info->balance_lock);
3024 bctl->stat.completed++;
3025 spin_unlock(&fs_info->balance_lock);
3026 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003027loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04003028 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003029 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003030
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003031 if (counting) {
3032 btrfs_release_path(path);
3033 counting = false;
3034 goto again;
3035 }
Chris Masonec44a352008-04-28 15:29:52 -04003036error:
3037 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003038 if (enospc_errors) {
3039 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
3040 enospc_errors);
3041 if (!ret)
3042 ret = -ENOSPC;
3043 }
3044
Chris Masonec44a352008-04-28 15:29:52 -04003045 return ret;
3046}
3047
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003048/**
3049 * alloc_profile_is_valid - see if a given profile is valid and reduced
3050 * @flags: profile to validate
3051 * @extended: if true @flags is treated as an extended profile
3052 */
3053static int alloc_profile_is_valid(u64 flags, int extended)
3054{
3055 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3056 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3057
3058 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3059
3060 /* 1) check that all other bits are zeroed */
3061 if (flags & ~mask)
3062 return 0;
3063
3064 /* 2) see if profile is reduced */
3065 if (flags == 0)
3066 return !extended; /* "0" is valid for usual profiles */
3067
3068 /* true if exactly one bit set */
3069 return (flags & (flags - 1)) == 0;
3070}
3071
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003072static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3073{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003074 /* cancel requested || normal exit path */
3075 return atomic_read(&fs_info->balance_cancel_req) ||
3076 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3077 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003078}
3079
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003080static void __cancel_balance(struct btrfs_fs_info *fs_info)
3081{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003082 int ret;
3083
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003084 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003085 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003086 if (ret)
3087 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003088
3089 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003090}
3091
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003092void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003093 struct btrfs_ioctl_balance_args *bargs);
3094
3095/*
3096 * Should be called with both balance and volume mutexes held
3097 */
3098int btrfs_balance(struct btrfs_balance_control *bctl,
3099 struct btrfs_ioctl_balance_args *bargs)
3100{
3101 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003102 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003103 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003104 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003105 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003106 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003107
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003108 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003109 atomic_read(&fs_info->balance_pause_req) ||
3110 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003111 ret = -EINVAL;
3112 goto out;
3113 }
3114
Ilya Dryomove4837f82012-03-27 17:09:17 +03003115 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3116 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3117 mixed = 1;
3118
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003119 /*
3120 * In case of mixed groups both data and meta should be picked,
3121 * and identical options should be given for both of them.
3122 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003123 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3124 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003125 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3126 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3127 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3128 printk(KERN_ERR "btrfs: with mixed groups data and "
3129 "metadata balance options must be the same\n");
3130 ret = -EINVAL;
3131 goto out;
3132 }
3133 }
3134
Stefan Behrens8dabb742012-11-06 13:15:27 +01003135 num_devices = fs_info->fs_devices->num_devices;
3136 btrfs_dev_replace_lock(&fs_info->dev_replace);
3137 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3138 BUG_ON(num_devices < 1);
3139 num_devices--;
3140 }
3141 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003142 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003143 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003144 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003145 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003146 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003147 if (num_devices > 2)
3148 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3149 if (num_devices > 3)
3150 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3151 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003152 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3153 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3154 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003155 printk(KERN_ERR "btrfs: unable to start balance with target "
3156 "data profile %llu\n",
3157 (unsigned long long)bctl->data.target);
3158 ret = -EINVAL;
3159 goto out;
3160 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003161 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3162 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3163 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003164 printk(KERN_ERR "btrfs: unable to start balance with target "
3165 "metadata profile %llu\n",
3166 (unsigned long long)bctl->meta.target);
3167 ret = -EINVAL;
3168 goto out;
3169 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003170 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3171 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3172 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003173 printk(KERN_ERR "btrfs: unable to start balance with target "
3174 "system profile %llu\n",
3175 (unsigned long long)bctl->sys.target);
3176 ret = -EINVAL;
3177 goto out;
3178 }
3179
Ilya Dryomove4837f82012-03-27 17:09:17 +03003180 /* allow dup'ed data chunks only in mixed mode */
3181 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003182 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003183 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3184 ret = -EINVAL;
3185 goto out;
3186 }
3187
3188 /* allow to reduce meta or sys integrity only if force set */
3189 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003190 BTRFS_BLOCK_GROUP_RAID10 |
3191 BTRFS_BLOCK_GROUP_RAID5 |
3192 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003193 do {
3194 seq = read_seqbegin(&fs_info->profiles_lock);
3195
3196 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3197 (fs_info->avail_system_alloc_bits & allowed) &&
3198 !(bctl->sys.target & allowed)) ||
3199 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3200 (fs_info->avail_metadata_alloc_bits & allowed) &&
3201 !(bctl->meta.target & allowed))) {
3202 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3203 printk(KERN_INFO "btrfs: force reducing metadata "
3204 "integrity\n");
3205 } else {
3206 printk(KERN_ERR "btrfs: balance will reduce metadata "
3207 "integrity, use force if you want this\n");
3208 ret = -EINVAL;
3209 goto out;
3210 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003211 }
Miao Xiede98ced2013-01-29 10:13:12 +00003212 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003213
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003214 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3215 int num_tolerated_disk_barrier_failures;
3216 u64 target = bctl->sys.target;
3217
3218 num_tolerated_disk_barrier_failures =
3219 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3220 if (num_tolerated_disk_barrier_failures > 0 &&
3221 (target &
3222 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3223 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3224 num_tolerated_disk_barrier_failures = 0;
3225 else if (num_tolerated_disk_barrier_failures > 1 &&
3226 (target &
3227 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3228 num_tolerated_disk_barrier_failures = 1;
3229
3230 fs_info->num_tolerated_disk_barrier_failures =
3231 num_tolerated_disk_barrier_failures;
3232 }
3233
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003234 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003235 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003236 goto out;
3237
Ilya Dryomov59641012012-01-16 22:04:48 +02003238 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3239 BUG_ON(ret == -EEXIST);
3240 set_balance_control(bctl);
3241 } else {
3242 BUG_ON(ret != -EEXIST);
3243 spin_lock(&fs_info->balance_lock);
3244 update_balance_args(bctl);
3245 spin_unlock(&fs_info->balance_lock);
3246 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003247
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003248 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003249 mutex_unlock(&fs_info->balance_mutex);
3250
3251 ret = __btrfs_balance(fs_info);
3252
3253 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003254 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003255
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003256 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3257 fs_info->num_tolerated_disk_barrier_failures =
3258 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3259 }
3260
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003261 if (bargs) {
3262 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003263 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003264 }
3265
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003266 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3267 balance_need_close(fs_info)) {
3268 __cancel_balance(fs_info);
3269 }
3270
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003271 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003272
3273 return ret;
3274out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003275 if (bctl->flags & BTRFS_BALANCE_RESUME)
3276 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003277 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003278 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003279 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3280 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003281 return ret;
3282}
3283
3284static int balance_kthread(void *data)
3285{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003286 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003287 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003288
3289 mutex_lock(&fs_info->volume_mutex);
3290 mutex_lock(&fs_info->balance_mutex);
3291
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003292 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003293 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003294 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003295 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003296
3297 mutex_unlock(&fs_info->balance_mutex);
3298 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003299
Ilya Dryomov59641012012-01-16 22:04:48 +02003300 return ret;
3301}
3302
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003303int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3304{
3305 struct task_struct *tsk;
3306
3307 spin_lock(&fs_info->balance_lock);
3308 if (!fs_info->balance_ctl) {
3309 spin_unlock(&fs_info->balance_lock);
3310 return 0;
3311 }
3312 spin_unlock(&fs_info->balance_lock);
3313
3314 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3315 printk(KERN_INFO "btrfs: force skipping balance\n");
3316 return 0;
3317 }
3318
3319 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3320 if (IS_ERR(tsk))
3321 return PTR_ERR(tsk);
3322
3323 return 0;
3324}
3325
Ilya Dryomov68310a52012-06-22 12:24:12 -06003326int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003327{
Ilya Dryomov59641012012-01-16 22:04:48 +02003328 struct btrfs_balance_control *bctl;
3329 struct btrfs_balance_item *item;
3330 struct btrfs_disk_balance_args disk_bargs;
3331 struct btrfs_path *path;
3332 struct extent_buffer *leaf;
3333 struct btrfs_key key;
3334 int ret;
3335
3336 path = btrfs_alloc_path();
3337 if (!path)
3338 return -ENOMEM;
3339
Ilya Dryomov68310a52012-06-22 12:24:12 -06003340 key.objectid = BTRFS_BALANCE_OBJECTID;
3341 key.type = BTRFS_BALANCE_ITEM_KEY;
3342 key.offset = 0;
3343
3344 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3345 if (ret < 0)
3346 goto out;
3347 if (ret > 0) { /* ret = -ENOENT; */
3348 ret = 0;
3349 goto out;
3350 }
3351
Ilya Dryomov59641012012-01-16 22:04:48 +02003352 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3353 if (!bctl) {
3354 ret = -ENOMEM;
3355 goto out;
3356 }
3357
Ilya Dryomov59641012012-01-16 22:04:48 +02003358 leaf = path->nodes[0];
3359 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3360
Ilya Dryomov68310a52012-06-22 12:24:12 -06003361 bctl->fs_info = fs_info;
3362 bctl->flags = btrfs_balance_flags(leaf, item);
3363 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003364
3365 btrfs_balance_data(leaf, item, &disk_bargs);
3366 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3367 btrfs_balance_meta(leaf, item, &disk_bargs);
3368 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3369 btrfs_balance_sys(leaf, item, &disk_bargs);
3370 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3371
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003372 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3373
Ilya Dryomov68310a52012-06-22 12:24:12 -06003374 mutex_lock(&fs_info->volume_mutex);
3375 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003376
Ilya Dryomov68310a52012-06-22 12:24:12 -06003377 set_balance_control(bctl);
3378
3379 mutex_unlock(&fs_info->balance_mutex);
3380 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003381out:
3382 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003383 return ret;
3384}
3385
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003386int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3387{
3388 int ret = 0;
3389
3390 mutex_lock(&fs_info->balance_mutex);
3391 if (!fs_info->balance_ctl) {
3392 mutex_unlock(&fs_info->balance_mutex);
3393 return -ENOTCONN;
3394 }
3395
3396 if (atomic_read(&fs_info->balance_running)) {
3397 atomic_inc(&fs_info->balance_pause_req);
3398 mutex_unlock(&fs_info->balance_mutex);
3399
3400 wait_event(fs_info->balance_wait_q,
3401 atomic_read(&fs_info->balance_running) == 0);
3402
3403 mutex_lock(&fs_info->balance_mutex);
3404 /* we are good with balance_ctl ripped off from under us */
3405 BUG_ON(atomic_read(&fs_info->balance_running));
3406 atomic_dec(&fs_info->balance_pause_req);
3407 } else {
3408 ret = -ENOTCONN;
3409 }
3410
3411 mutex_unlock(&fs_info->balance_mutex);
3412 return ret;
3413}
3414
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003415int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3416{
3417 mutex_lock(&fs_info->balance_mutex);
3418 if (!fs_info->balance_ctl) {
3419 mutex_unlock(&fs_info->balance_mutex);
3420 return -ENOTCONN;
3421 }
3422
3423 atomic_inc(&fs_info->balance_cancel_req);
3424 /*
3425 * if we are running just wait and return, balance item is
3426 * deleted in btrfs_balance in this case
3427 */
3428 if (atomic_read(&fs_info->balance_running)) {
3429 mutex_unlock(&fs_info->balance_mutex);
3430 wait_event(fs_info->balance_wait_q,
3431 atomic_read(&fs_info->balance_running) == 0);
3432 mutex_lock(&fs_info->balance_mutex);
3433 } else {
3434 /* __cancel_balance needs volume_mutex */
3435 mutex_unlock(&fs_info->balance_mutex);
3436 mutex_lock(&fs_info->volume_mutex);
3437 mutex_lock(&fs_info->balance_mutex);
3438
3439 if (fs_info->balance_ctl)
3440 __cancel_balance(fs_info);
3441
3442 mutex_unlock(&fs_info->volume_mutex);
3443 }
3444
3445 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3446 atomic_dec(&fs_info->balance_cancel_req);
3447 mutex_unlock(&fs_info->balance_mutex);
3448 return 0;
3449}
3450
Chris Mason8f18cf12008-04-25 16:53:30 -04003451/*
3452 * shrinking a device means finding all of the device extents past
3453 * the new size, and then following the back refs to the chunks.
3454 * The chunk relocation code actually frees the device extent
3455 */
3456int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3457{
3458 struct btrfs_trans_handle *trans;
3459 struct btrfs_root *root = device->dev_root;
3460 struct btrfs_dev_extent *dev_extent = NULL;
3461 struct btrfs_path *path;
3462 u64 length;
3463 u64 chunk_tree;
3464 u64 chunk_objectid;
3465 u64 chunk_offset;
3466 int ret;
3467 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003468 int failed = 0;
3469 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003470 struct extent_buffer *l;
3471 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003472 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003473 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003474 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003475 u64 diff = device->total_bytes - new_size;
3476
Stefan Behrens63a212a2012-11-05 18:29:28 +01003477 if (device->is_tgtdev_for_dev_replace)
3478 return -EINVAL;
3479
Chris Mason8f18cf12008-04-25 16:53:30 -04003480 path = btrfs_alloc_path();
3481 if (!path)
3482 return -ENOMEM;
3483
Chris Mason8f18cf12008-04-25 16:53:30 -04003484 path->reada = 2;
3485
Chris Mason7d9eb122008-07-08 14:19:17 -04003486 lock_chunks(root);
3487
Chris Mason8f18cf12008-04-25 16:53:30 -04003488 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003489 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003490 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003491 spin_lock(&root->fs_info->free_chunk_lock);
3492 root->fs_info->free_chunk_space -= diff;
3493 spin_unlock(&root->fs_info->free_chunk_lock);
3494 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003495 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003496
Josef Bacikba1bf482009-09-11 16:11:19 -04003497again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003498 key.objectid = device->devid;
3499 key.offset = (u64)-1;
3500 key.type = BTRFS_DEV_EXTENT_KEY;
3501
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003502 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003503 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3504 if (ret < 0)
3505 goto done;
3506
3507 ret = btrfs_previous_item(root, path, 0, key.type);
3508 if (ret < 0)
3509 goto done;
3510 if (ret) {
3511 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003512 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003513 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003514 }
3515
3516 l = path->nodes[0];
3517 slot = path->slots[0];
3518 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3519
Josef Bacikba1bf482009-09-11 16:11:19 -04003520 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003521 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003522 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003523 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003524
3525 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3526 length = btrfs_dev_extent_length(l, dev_extent);
3527
Josef Bacikba1bf482009-09-11 16:11:19 -04003528 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003529 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003530 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003531 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003532
3533 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3534 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3535 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003536 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003537
3538 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3539 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003540 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003541 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003542 if (ret == -ENOSPC)
3543 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003544 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003545
3546 if (failed && !retried) {
3547 failed = 0;
3548 retried = true;
3549 goto again;
3550 } else if (failed && retried) {
3551 ret = -ENOSPC;
3552 lock_chunks(root);
3553
3554 device->total_bytes = old_size;
3555 if (device->writeable)
3556 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003557 spin_lock(&root->fs_info->free_chunk_lock);
3558 root->fs_info->free_chunk_space += diff;
3559 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003560 unlock_chunks(root);
3561 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003562 }
3563
Chris Balld6397ba2009-04-27 07:29:03 -04003564 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003565 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003566 if (IS_ERR(trans)) {
3567 ret = PTR_ERR(trans);
3568 goto done;
3569 }
3570
Chris Balld6397ba2009-04-27 07:29:03 -04003571 lock_chunks(root);
3572
3573 device->disk_total_bytes = new_size;
3574 /* Now btrfs_update_device() will change the on-disk size. */
3575 ret = btrfs_update_device(trans, device);
3576 if (ret) {
3577 unlock_chunks(root);
3578 btrfs_end_transaction(trans, root);
3579 goto done;
3580 }
3581 WARN_ON(diff > old_total);
3582 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3583 unlock_chunks(root);
3584 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003585done:
3586 btrfs_free_path(path);
3587 return ret;
3588}
3589
Li Zefan125ccb02011-12-08 15:07:24 +08003590static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003591 struct btrfs_key *key,
3592 struct btrfs_chunk *chunk, int item_size)
3593{
David Sterba6c417612011-04-13 15:41:04 +02003594 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003595 struct btrfs_disk_key disk_key;
3596 u32 array_size;
3597 u8 *ptr;
3598
3599 array_size = btrfs_super_sys_array_size(super_copy);
3600 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3601 return -EFBIG;
3602
3603 ptr = super_copy->sys_chunk_array + array_size;
3604 btrfs_cpu_key_to_disk(&disk_key, key);
3605 memcpy(ptr, &disk_key, sizeof(disk_key));
3606 ptr += sizeof(disk_key);
3607 memcpy(ptr, chunk, item_size);
3608 item_size += sizeof(disk_key);
3609 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3610 return 0;
3611}
3612
Miao Xieb2117a32011-01-05 10:07:28 +00003613/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003614 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003615 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003616static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003617{
Arne Jansen73c5de02011-04-12 12:07:57 +02003618 const struct btrfs_device_info *di_a = a;
3619 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003620
Arne Jansen73c5de02011-04-12 12:07:57 +02003621 if (di_a->max_avail > di_b->max_avail)
3622 return -1;
3623 if (di_a->max_avail < di_b->max_avail)
3624 return 1;
3625 if (di_a->total_avail > di_b->total_avail)
3626 return -1;
3627 if (di_a->total_avail < di_b->total_avail)
3628 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003629 return 0;
3630}
3631
Eric Sandeen48a3b632013-04-25 20:41:01 +00003632static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00003633 [BTRFS_RAID_RAID10] = {
3634 .sub_stripes = 2,
3635 .dev_stripes = 1,
3636 .devs_max = 0, /* 0 == as many as possible */
3637 .devs_min = 4,
3638 .devs_increment = 2,
3639 .ncopies = 2,
3640 },
3641 [BTRFS_RAID_RAID1] = {
3642 .sub_stripes = 1,
3643 .dev_stripes = 1,
3644 .devs_max = 2,
3645 .devs_min = 2,
3646 .devs_increment = 2,
3647 .ncopies = 2,
3648 },
3649 [BTRFS_RAID_DUP] = {
3650 .sub_stripes = 1,
3651 .dev_stripes = 2,
3652 .devs_max = 1,
3653 .devs_min = 1,
3654 .devs_increment = 1,
3655 .ncopies = 2,
3656 },
3657 [BTRFS_RAID_RAID0] = {
3658 .sub_stripes = 1,
3659 .dev_stripes = 1,
3660 .devs_max = 0,
3661 .devs_min = 2,
3662 .devs_increment = 1,
3663 .ncopies = 1,
3664 },
3665 [BTRFS_RAID_SINGLE] = {
3666 .sub_stripes = 1,
3667 .dev_stripes = 1,
3668 .devs_max = 1,
3669 .devs_min = 1,
3670 .devs_increment = 1,
3671 .ncopies = 1,
3672 },
Chris Masone942f882013-02-20 14:06:05 -05003673 [BTRFS_RAID_RAID5] = {
3674 .sub_stripes = 1,
3675 .dev_stripes = 1,
3676 .devs_max = 0,
3677 .devs_min = 2,
3678 .devs_increment = 1,
3679 .ncopies = 2,
3680 },
3681 [BTRFS_RAID_RAID6] = {
3682 .sub_stripes = 1,
3683 .dev_stripes = 1,
3684 .devs_max = 0,
3685 .devs_min = 3,
3686 .devs_increment = 1,
3687 .ncopies = 3,
3688 },
Liu Bo31e50222012-11-21 14:18:10 +00003689};
3690
David Woodhouse53b381b2013-01-29 18:40:14 -05003691static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
3692{
3693 /* TODO allow them to set a preferred stripe size */
3694 return 64 * 1024;
3695}
3696
3697static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
3698{
David Woodhouse53b381b2013-01-29 18:40:14 -05003699 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
3700 return;
3701
Miao Xieceda0862013-04-11 10:30:16 +00003702 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05003703}
3704
Miao Xieb2117a32011-01-05 10:07:28 +00003705static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3706 struct btrfs_root *extent_root,
3707 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003708 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003709 u64 start, u64 type)
3710{
3711 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003712 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3713 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003714 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003715 struct extent_map_tree *em_tree;
3716 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003717 struct btrfs_device_info *devices_info = NULL;
3718 u64 total_avail;
3719 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05003720 int data_stripes; /* number of stripes that count for
3721 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02003722 int sub_stripes; /* sub_stripes info for map */
3723 int dev_stripes; /* stripes per dev */
3724 int devs_max; /* max devs to use */
3725 int devs_min; /* min devs needed */
3726 int devs_increment; /* ndevs has to be a multiple of this */
3727 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003728 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003729 u64 max_stripe_size;
3730 u64 max_chunk_size;
3731 u64 stripe_size;
3732 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05003733 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02003734 int ndevs;
3735 int i;
3736 int j;
Liu Bo31e50222012-11-21 14:18:10 +00003737 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00003738
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003739 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003740
Miao Xieb2117a32011-01-05 10:07:28 +00003741 if (list_empty(&fs_devices->alloc_list))
3742 return -ENOSPC;
3743
Liu Bo31e50222012-11-21 14:18:10 +00003744 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02003745
Liu Bo31e50222012-11-21 14:18:10 +00003746 sub_stripes = btrfs_raid_array[index].sub_stripes;
3747 dev_stripes = btrfs_raid_array[index].dev_stripes;
3748 devs_max = btrfs_raid_array[index].devs_max;
3749 devs_min = btrfs_raid_array[index].devs_min;
3750 devs_increment = btrfs_raid_array[index].devs_increment;
3751 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02003752
3753 if (type & BTRFS_BLOCK_GROUP_DATA) {
3754 max_stripe_size = 1024 * 1024 * 1024;
3755 max_chunk_size = 10 * max_stripe_size;
3756 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003757 /* for larger filesystems, use larger metadata chunks */
3758 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3759 max_stripe_size = 1024 * 1024 * 1024;
3760 else
3761 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003762 max_chunk_size = max_stripe_size;
3763 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003764 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003765 max_chunk_size = 2 * max_stripe_size;
3766 } else {
3767 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3768 type);
3769 BUG_ON(1);
3770 }
3771
3772 /* we don't want a chunk larger than 10% of writeable space */
3773 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3774 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003775
3776 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3777 GFP_NOFS);
3778 if (!devices_info)
3779 return -ENOMEM;
3780
Arne Jansen73c5de02011-04-12 12:07:57 +02003781 cur = fs_devices->alloc_list.next;
3782
3783 /*
3784 * in the first pass through the devices list, we gather information
3785 * about the available holes on each device.
3786 */
3787 ndevs = 0;
3788 while (cur != &fs_devices->alloc_list) {
3789 struct btrfs_device *device;
3790 u64 max_avail;
3791 u64 dev_offset;
3792
3793 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3794
3795 cur = cur->next;
3796
3797 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003798 WARN(1, KERN_ERR
Arne Jansen73c5de02011-04-12 12:07:57 +02003799 "btrfs: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02003800 continue;
3801 }
3802
Stefan Behrens63a212a2012-11-05 18:29:28 +01003803 if (!device->in_fs_metadata ||
3804 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02003805 continue;
3806
3807 if (device->total_bytes > device->bytes_used)
3808 total_avail = device->total_bytes - device->bytes_used;
3809 else
3810 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003811
3812 /* If there is no space on this device, skip it. */
3813 if (total_avail == 0)
3814 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003815
Li Zefan125ccb02011-12-08 15:07:24 +08003816 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003817 max_stripe_size * dev_stripes,
3818 &dev_offset, &max_avail);
3819 if (ret && ret != -ENOSPC)
3820 goto error;
3821
3822 if (ret == 0)
3823 max_avail = max_stripe_size * dev_stripes;
3824
3825 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3826 continue;
3827
Eric Sandeen063d0062013-01-31 00:55:01 +00003828 if (ndevs == fs_devices->rw_devices) {
3829 WARN(1, "%s: found more than %llu devices\n",
3830 __func__, fs_devices->rw_devices);
3831 break;
3832 }
Arne Jansen73c5de02011-04-12 12:07:57 +02003833 devices_info[ndevs].dev_offset = dev_offset;
3834 devices_info[ndevs].max_avail = max_avail;
3835 devices_info[ndevs].total_avail = total_avail;
3836 devices_info[ndevs].dev = device;
3837 ++ndevs;
3838 }
3839
3840 /*
3841 * now sort the devices by hole size / available space
3842 */
3843 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3844 btrfs_cmp_device_info, NULL);
3845
3846 /* round down to number of usable stripes */
3847 ndevs -= ndevs % devs_increment;
3848
3849 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3850 ret = -ENOSPC;
3851 goto error;
3852 }
3853
3854 if (devs_max && ndevs > devs_max)
3855 ndevs = devs_max;
3856 /*
3857 * the primary goal is to maximize the number of stripes, so use as many
3858 * devices as possible, even if the stripes are not maximum sized.
3859 */
3860 stripe_size = devices_info[ndevs-1].max_avail;
3861 num_stripes = ndevs * dev_stripes;
3862
David Woodhouse53b381b2013-01-29 18:40:14 -05003863 /*
3864 * this will have to be fixed for RAID1 and RAID10 over
3865 * more drives
3866 */
3867 data_stripes = num_stripes / ncopies;
3868
David Woodhouse53b381b2013-01-29 18:40:14 -05003869 if (type & BTRFS_BLOCK_GROUP_RAID5) {
3870 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
3871 btrfs_super_stripesize(info->super_copy));
3872 data_stripes = num_stripes - 1;
3873 }
3874 if (type & BTRFS_BLOCK_GROUP_RAID6) {
3875 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
3876 btrfs_super_stripesize(info->super_copy));
3877 data_stripes = num_stripes - 2;
3878 }
Chris Mason86db2572013-02-20 16:23:40 -05003879
3880 /*
3881 * Use the number of data stripes to figure out how big this chunk
3882 * is really going to be in terms of logical address space,
3883 * and compare that answer with the max chunk size
3884 */
3885 if (stripe_size * data_stripes > max_chunk_size) {
3886 u64 mask = (1ULL << 24) - 1;
3887 stripe_size = max_chunk_size;
3888 do_div(stripe_size, data_stripes);
3889
3890 /* bump the answer up to a 16MB boundary */
3891 stripe_size = (stripe_size + mask) & ~mask;
3892
3893 /* but don't go higher than the limits we found
3894 * while searching for free extents
3895 */
3896 if (stripe_size > devices_info[ndevs-1].max_avail)
3897 stripe_size = devices_info[ndevs-1].max_avail;
3898 }
3899
Arne Jansen73c5de02011-04-12 12:07:57 +02003900 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003901
3902 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05003903 do_div(stripe_size, raid_stripe_len);
3904 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02003905
Miao Xieb2117a32011-01-05 10:07:28 +00003906 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3907 if (!map) {
3908 ret = -ENOMEM;
3909 goto error;
3910 }
3911 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003912
Arne Jansen73c5de02011-04-12 12:07:57 +02003913 for (i = 0; i < ndevs; ++i) {
3914 for (j = 0; j < dev_stripes; ++j) {
3915 int s = i * dev_stripes + j;
3916 map->stripes[s].dev = devices_info[i].dev;
3917 map->stripes[s].physical = devices_info[i].dev_offset +
3918 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003919 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003920 }
Chris Mason593060d2008-03-25 16:50:33 -04003921 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05003922 map->stripe_len = raid_stripe_len;
3923 map->io_align = raid_stripe_len;
3924 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04003925 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003926 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003927
Yan Zheng2b820322008-11-17 21:11:30 -05003928 *map_ret = map;
David Woodhouse53b381b2013-01-29 18:40:14 -05003929 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003930
Arne Jansen73c5de02011-04-12 12:07:57 +02003931 *stripe_size_out = stripe_size;
3932 *num_bytes_out = num_bytes;
3933
3934 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003935
David Sterba172ddd62011-04-21 00:48:27 +02003936 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003937 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003938 ret = -ENOMEM;
3939 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003940 }
Chris Mason0b86a832008-03-24 15:01:56 -04003941 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003942 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003943 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003944 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003945 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003946
Chris Mason0b86a832008-03-24 15:01:56 -04003947 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003948 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003949 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04003950 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003951 if (ret) {
3952 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003953 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003954 }
Yan Zheng2b820322008-11-17 21:11:30 -05003955
Arne Jansen73c5de02011-04-12 12:07:57 +02003956 for (i = 0; i < map->num_stripes; ++i) {
3957 struct btrfs_device *device;
3958 u64 dev_offset;
3959
3960 device = map->stripes[i].dev;
3961 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003962
3963 ret = btrfs_alloc_dev_extent(trans, device,
3964 info->chunk_root->root_key.objectid,
3965 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003966 start, dev_offset, stripe_size);
Josef Bacik04487482013-01-29 15:03:37 -05003967 if (ret)
3968 goto error_dev_extent;
3969 }
3970
3971 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3972 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3973 start, num_bytes);
3974 if (ret) {
3975 i = map->num_stripes - 1;
3976 goto error_dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05003977 }
3978
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003979 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05003980 check_raid56_incompat_flag(extent_root->fs_info, type);
3981
Miao Xieb2117a32011-01-05 10:07:28 +00003982 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003983 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003984
Josef Bacik04487482013-01-29 15:03:37 -05003985error_dev_extent:
3986 for (; i >= 0; i--) {
3987 struct btrfs_device *device;
3988 int err;
3989
3990 device = map->stripes[i].dev;
3991 err = btrfs_free_dev_extent(trans, device, start);
3992 if (err) {
3993 btrfs_abort_transaction(trans, extent_root, err);
3994 break;
3995 }
3996 }
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003997 write_lock(&em_tree->lock);
3998 remove_extent_mapping(em_tree, em);
3999 write_unlock(&em_tree->lock);
4000
4001 /* One for our allocation */
4002 free_extent_map(em);
4003 /* One for the tree reference */
4004 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004005error:
4006 kfree(map);
4007 kfree(devices_info);
4008 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004009}
4010
4011static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
4012 struct btrfs_root *extent_root,
4013 struct map_lookup *map, u64 chunk_offset,
4014 u64 chunk_size, u64 stripe_size)
4015{
4016 u64 dev_offset;
4017 struct btrfs_key key;
4018 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4019 struct btrfs_device *device;
4020 struct btrfs_chunk *chunk;
4021 struct btrfs_stripe *stripe;
4022 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
4023 int index = 0;
4024 int ret;
4025
4026 chunk = kzalloc(item_size, GFP_NOFS);
4027 if (!chunk)
4028 return -ENOMEM;
4029
4030 index = 0;
4031 while (index < map->num_stripes) {
4032 device = map->stripes[index].dev;
4033 device->bytes_used += stripe_size;
4034 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004035 if (ret)
4036 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05004037 index++;
4038 }
4039
Josef Bacik2bf64752011-09-26 17:12:22 -04004040 spin_lock(&extent_root->fs_info->free_chunk_lock);
4041 extent_root->fs_info->free_chunk_space -= (stripe_size *
4042 map->num_stripes);
4043 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4044
Yan Zheng2b820322008-11-17 21:11:30 -05004045 index = 0;
4046 stripe = &chunk->stripe;
4047 while (index < map->num_stripes) {
4048 device = map->stripes[index].dev;
4049 dev_offset = map->stripes[index].physical;
4050
4051 btrfs_set_stack_stripe_devid(stripe, device->devid);
4052 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4053 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4054 stripe++;
4055 index++;
4056 }
4057
4058 btrfs_set_stack_chunk_length(chunk, chunk_size);
4059 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4060 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4061 btrfs_set_stack_chunk_type(chunk, map->type);
4062 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4063 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4064 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4065 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4066 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4067
4068 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4069 key.type = BTRFS_CHUNK_ITEM_KEY;
4070 key.offset = chunk_offset;
4071
4072 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004073
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004074 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4075 /*
4076 * TODO: Cleanup of inserted chunk root in case of
4077 * failure.
4078 */
Li Zefan125ccb02011-12-08 15:07:24 +08004079 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004080 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004081 }
liubo1abe9b82011-03-24 11:18:59 +00004082
Mark Fasheh3acd3952011-09-08 17:40:01 -07004083out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05004084 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004085 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004086}
4087
4088/*
4089 * Chunk allocation falls into two parts. The first part does works
4090 * that make the new allocated chunk useable, but not do any operation
4091 * that modifies the chunk tree. The second part does the works that
4092 * require modifying the chunk tree. This division is important for the
4093 * bootstrap process of adding storage to a seed btrfs.
4094 */
4095int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4096 struct btrfs_root *extent_root, u64 type)
4097{
4098 u64 chunk_offset;
4099 u64 chunk_size;
4100 u64 stripe_size;
4101 struct map_lookup *map;
4102 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4103 int ret;
4104
4105 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4106 &chunk_offset);
4107 if (ret)
4108 return ret;
4109
4110 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4111 &stripe_size, chunk_offset, type);
4112 if (ret)
4113 return ret;
4114
4115 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4116 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004117 if (ret)
4118 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004119 return 0;
4120}
4121
Chris Masond3977122009-01-05 21:25:51 -05004122static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004123 struct btrfs_root *root,
4124 struct btrfs_device *device)
4125{
4126 u64 chunk_offset;
4127 u64 sys_chunk_offset;
4128 u64 chunk_size;
4129 u64 sys_chunk_size;
4130 u64 stripe_size;
4131 u64 sys_stripe_size;
4132 u64 alloc_profile;
4133 struct map_lookup *map;
4134 struct map_lookup *sys_map;
4135 struct btrfs_fs_info *fs_info = root->fs_info;
4136 struct btrfs_root *extent_root = fs_info->extent_root;
4137 int ret;
4138
4139 ret = find_next_chunk(fs_info->chunk_root,
4140 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07004141 if (ret)
4142 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004143
Miao Xiede98ced2013-01-29 10:13:12 +00004144 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05004145 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4146 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004147 if (ret)
4148 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004149
4150 sys_chunk_offset = chunk_offset + chunk_size;
4151
Miao Xiede98ced2013-01-29 10:13:12 +00004152 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05004153 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
4154 &sys_chunk_size, &sys_stripe_size,
4155 sys_chunk_offset, alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004156 if (ret) {
4157 btrfs_abort_transaction(trans, root, ret);
4158 goto out;
4159 }
Yan Zheng2b820322008-11-17 21:11:30 -05004160
4161 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
David Sterba005d6422012-09-18 07:52:32 -06004162 if (ret) {
4163 btrfs_abort_transaction(trans, root, ret);
4164 goto out;
4165 }
Yan Zheng2b820322008-11-17 21:11:30 -05004166
4167 /*
4168 * Modifying chunk tree needs allocating new blocks from both
4169 * system block group and metadata block group. So we only can
4170 * do operations require modifying the chunk tree after both
4171 * block groups were created.
4172 */
4173 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4174 chunk_size, stripe_size);
David Sterba005d6422012-09-18 07:52:32 -06004175 if (ret) {
4176 btrfs_abort_transaction(trans, root, ret);
4177 goto out;
4178 }
Yan Zheng2b820322008-11-17 21:11:30 -05004179
4180 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
4181 sys_chunk_offset, sys_chunk_size,
4182 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004183 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004184 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004185
David Sterba005d6422012-09-18 07:52:32 -06004186out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004187
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004188 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004189}
4190
4191int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4192{
4193 struct extent_map *em;
4194 struct map_lookup *map;
4195 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4196 int readonly = 0;
4197 int i;
4198
Chris Mason890871b2009-09-02 16:24:52 -04004199 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004200 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004201 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004202 if (!em)
4203 return 1;
4204
Josef Bacikf48b9072010-01-27 02:07:59 +00004205 if (btrfs_test_opt(root, DEGRADED)) {
4206 free_extent_map(em);
4207 return 0;
4208 }
4209
Yan Zheng2b820322008-11-17 21:11:30 -05004210 map = (struct map_lookup *)em->bdev;
4211 for (i = 0; i < map->num_stripes; i++) {
4212 if (!map->stripes[i].dev->writeable) {
4213 readonly = 1;
4214 break;
4215 }
4216 }
4217 free_extent_map(em);
4218 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004219}
4220
4221void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4222{
David Sterbaa8067e02011-04-21 00:34:43 +02004223 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004224}
4225
4226void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4227{
4228 struct extent_map *em;
4229
Chris Masond3977122009-01-05 21:25:51 -05004230 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004231 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004232 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4233 if (em)
4234 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004235 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004236 if (!em)
4237 break;
4238 kfree(em->bdev);
4239 /* once for us */
4240 free_extent_map(em);
4241 /* once for the tree */
4242 free_extent_map(em);
4243 }
4244}
4245
Stefan Behrens5d964052012-11-05 14:59:07 +01004246int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004247{
Stefan Behrens5d964052012-11-05 14:59:07 +01004248 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004249 struct extent_map *em;
4250 struct map_lookup *map;
4251 struct extent_map_tree *em_tree = &map_tree->map_tree;
4252 int ret;
4253
Chris Mason890871b2009-09-02 16:24:52 -04004254 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004255 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004256 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004257
Josef Bacikfb7669b2013-04-23 10:53:18 -04004258 /*
4259 * We could return errors for these cases, but that could get ugly and
4260 * we'd probably do the same thing which is just not do anything else
4261 * and exit, so return 1 so the callers don't try to use other copies.
4262 */
4263 if (!em) {
4264 btrfs_emerg(fs_info, "No mapping for %Lu-%Lu\n", logical,
4265 logical+len);
4266 return 1;
4267 }
4268
4269 if (em->start > logical || em->start + em->len < logical) {
4270 btrfs_emerg(fs_info, "Invalid mapping for %Lu-%Lu, got "
4271 "%Lu-%Lu\n", logical, logical+len, em->start,
4272 em->start + em->len);
Liu Bofb5834f2013-09-29 10:33:16 +08004273 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004274 return 1;
4275 }
4276
Chris Masonf1885912008-04-09 16:28:12 -04004277 map = (struct map_lookup *)em->bdev;
4278 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4279 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004280 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4281 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004282 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4283 ret = 2;
4284 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4285 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004286 else
4287 ret = 1;
4288 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004289
4290 btrfs_dev_replace_lock(&fs_info->dev_replace);
4291 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4292 ret++;
4293 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4294
Chris Masonf1885912008-04-09 16:28:12 -04004295 return ret;
4296}
4297
David Woodhouse53b381b2013-01-29 18:40:14 -05004298unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4299 struct btrfs_mapping_tree *map_tree,
4300 u64 logical)
4301{
4302 struct extent_map *em;
4303 struct map_lookup *map;
4304 struct extent_map_tree *em_tree = &map_tree->map_tree;
4305 unsigned long len = root->sectorsize;
4306
4307 read_lock(&em_tree->lock);
4308 em = lookup_extent_mapping(em_tree, logical, len);
4309 read_unlock(&em_tree->lock);
4310 BUG_ON(!em);
4311
4312 BUG_ON(em->start > logical || em->start + em->len < logical);
4313 map = (struct map_lookup *)em->bdev;
4314 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4315 BTRFS_BLOCK_GROUP_RAID6)) {
4316 len = map->stripe_len * nr_data_stripes(map);
4317 }
4318 free_extent_map(em);
4319 return len;
4320}
4321
4322int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4323 u64 logical, u64 len, int mirror_num)
4324{
4325 struct extent_map *em;
4326 struct map_lookup *map;
4327 struct extent_map_tree *em_tree = &map_tree->map_tree;
4328 int ret = 0;
4329
4330 read_lock(&em_tree->lock);
4331 em = lookup_extent_mapping(em_tree, logical, len);
4332 read_unlock(&em_tree->lock);
4333 BUG_ON(!em);
4334
4335 BUG_ON(em->start > logical || em->start + em->len < logical);
4336 map = (struct map_lookup *)em->bdev;
4337 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4338 BTRFS_BLOCK_GROUP_RAID6))
4339 ret = 1;
4340 free_extent_map(em);
4341 return ret;
4342}
4343
Stefan Behrens30d98612012-11-06 14:52:18 +01004344static int find_live_mirror(struct btrfs_fs_info *fs_info,
4345 struct map_lookup *map, int first, int num,
4346 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004347{
4348 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004349 int tolerance;
4350 struct btrfs_device *srcdev;
4351
4352 if (dev_replace_is_ongoing &&
4353 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4354 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4355 srcdev = fs_info->dev_replace.srcdev;
4356 else
4357 srcdev = NULL;
4358
4359 /*
4360 * try to avoid the drive that is the source drive for a
4361 * dev-replace procedure, only choose it if no other non-missing
4362 * mirror is available
4363 */
4364 for (tolerance = 0; tolerance < 2; tolerance++) {
4365 if (map->stripes[optimal].dev->bdev &&
4366 (tolerance || map->stripes[optimal].dev != srcdev))
4367 return optimal;
4368 for (i = first; i < first + num; i++) {
4369 if (map->stripes[i].dev->bdev &&
4370 (tolerance || map->stripes[i].dev != srcdev))
4371 return i;
4372 }
Chris Masondfe25022008-05-13 13:46:40 -04004373 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004374
Chris Masondfe25022008-05-13 13:46:40 -04004375 /* we couldn't find one that doesn't fail. Just return something
4376 * and the io error handling code will clean up eventually
4377 */
4378 return optimal;
4379}
4380
David Woodhouse53b381b2013-01-29 18:40:14 -05004381static inline int parity_smaller(u64 a, u64 b)
4382{
4383 return a > b;
4384}
4385
4386/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4387static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4388{
4389 struct btrfs_bio_stripe s;
4390 int i;
4391 u64 l;
4392 int again = 1;
4393
4394 while (again) {
4395 again = 0;
4396 for (i = 0; i < bbio->num_stripes - 1; i++) {
4397 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4398 s = bbio->stripes[i];
4399 l = raid_map[i];
4400 bbio->stripes[i] = bbio->stripes[i+1];
4401 raid_map[i] = raid_map[i+1];
4402 bbio->stripes[i+1] = s;
4403 raid_map[i+1] = l;
4404 again = 1;
4405 }
4406 }
4407 }
4408}
4409
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004410static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004411 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004412 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004413 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004414{
4415 struct extent_map *em;
4416 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004417 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004418 struct extent_map_tree *em_tree = &map_tree->map_tree;
4419 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004420 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004421 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004422 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004423 u64 stripe_nr_orig;
4424 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004425 u64 stripe_len;
4426 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004427 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004428 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004429 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004430 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004431 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004432 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004433 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4434 int dev_replace_is_ongoing = 0;
4435 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004436 int patch_the_first_stripe_for_dev_replace = 0;
4437 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004438 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004439
Chris Mason890871b2009-09-02 16:24:52 -04004440 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004441 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004442 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004443
Chris Mason3b951512008-04-17 11:29:12 -04004444 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004445 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
4446 (unsigned long long)logical,
4447 (unsigned long long)*length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004448 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004449 }
Chris Mason0b86a832008-03-24 15:01:56 -04004450
Josef Bacik9bb91872013-04-19 23:45:33 -04004451 if (em->start > logical || em->start + em->len < logical) {
4452 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
4453 "found %Lu-%Lu\n", logical, em->start,
4454 em->start + em->len);
Liu Bofb5834f2013-09-29 10:33:16 +08004455 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004456 return -EINVAL;
4457 }
4458
Chris Mason0b86a832008-03-24 15:01:56 -04004459 map = (struct map_lookup *)em->bdev;
4460 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004461
David Woodhouse53b381b2013-01-29 18:40:14 -05004462 if (mirror_num > map->num_stripes)
4463 mirror_num = 0;
4464
4465 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004466 stripe_nr = offset;
4467 /*
4468 * stripe_nr counts the total number of stripes we have to stride
4469 * to get to this block
4470 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004471 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004472
David Woodhouse53b381b2013-01-29 18:40:14 -05004473 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004474 BUG_ON(offset < stripe_offset);
4475
4476 /* stripe_offset is the offset of this block in its stripe*/
4477 stripe_offset = offset - stripe_offset;
4478
David Woodhouse53b381b2013-01-29 18:40:14 -05004479 /* if we're here for raid56, we need to know the stripe aligned start */
4480 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4481 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4482 raid56_full_stripe_start = offset;
4483
4484 /* allow a write of a full stripe, but make sure we don't
4485 * allow straddling of stripes
4486 */
4487 do_div(raid56_full_stripe_start, full_stripe_len);
4488 raid56_full_stripe_start *= full_stripe_len;
4489 }
4490
4491 if (rw & REQ_DISCARD) {
4492 /* we don't discard raid56 yet */
4493 if (map->type &
4494 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4495 ret = -EOPNOTSUPP;
4496 goto out;
4497 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004498 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004499 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4500 u64 max_len;
4501 /* For writes to RAID[56], allow a full stripeset across all disks.
4502 For other RAID types and for RAID[56] reads, just allow a single
4503 stripe (on a single disk). */
4504 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4505 (rw & REQ_WRITE)) {
4506 max_len = stripe_len * nr_data_stripes(map) -
4507 (offset - raid56_full_stripe_start);
4508 } else {
4509 /* we limit the length of each bio to what fits in a stripe */
4510 max_len = stripe_len - stripe_offset;
4511 }
4512 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004513 } else {
4514 *length = em->len - offset;
4515 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004516
David Woodhouse53b381b2013-01-29 18:40:14 -05004517 /* This is for when we're called from btrfs_merge_bio_hook() and all
4518 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004519 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004520 goto out;
4521
Stefan Behrens472262f2012-11-06 14:43:46 +01004522 btrfs_dev_replace_lock(dev_replace);
4523 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4524 if (!dev_replace_is_ongoing)
4525 btrfs_dev_replace_unlock(dev_replace);
4526
Stefan Behrensad6d6202012-11-06 15:06:47 +01004527 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4528 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4529 dev_replace->tgtdev != NULL) {
4530 /*
4531 * in dev-replace case, for repair case (that's the only
4532 * case where the mirror is selected explicitly when
4533 * calling btrfs_map_block), blocks left of the left cursor
4534 * can also be read from the target drive.
4535 * For REQ_GET_READ_MIRRORS, the target drive is added as
4536 * the last one to the array of stripes. For READ, it also
4537 * needs to be supported using the same mirror number.
4538 * If the requested block is not left of the left cursor,
4539 * EIO is returned. This can happen because btrfs_num_copies()
4540 * returns one more in the dev-replace case.
4541 */
4542 u64 tmp_length = *length;
4543 struct btrfs_bio *tmp_bbio = NULL;
4544 int tmp_num_stripes;
4545 u64 srcdev_devid = dev_replace->srcdev->devid;
4546 int index_srcdev = 0;
4547 int found = 0;
4548 u64 physical_of_found = 0;
4549
4550 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004551 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004552 if (ret) {
4553 WARN_ON(tmp_bbio != NULL);
4554 goto out;
4555 }
4556
4557 tmp_num_stripes = tmp_bbio->num_stripes;
4558 if (mirror_num > tmp_num_stripes) {
4559 /*
4560 * REQ_GET_READ_MIRRORS does not contain this
4561 * mirror, that means that the requested area
4562 * is not left of the left cursor
4563 */
4564 ret = -EIO;
4565 kfree(tmp_bbio);
4566 goto out;
4567 }
4568
4569 /*
4570 * process the rest of the function using the mirror_num
4571 * of the source drive. Therefore look it up first.
4572 * At the end, patch the device pointer to the one of the
4573 * target drive.
4574 */
4575 for (i = 0; i < tmp_num_stripes; i++) {
4576 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4577 /*
4578 * In case of DUP, in order to keep it
4579 * simple, only add the mirror with the
4580 * lowest physical address
4581 */
4582 if (found &&
4583 physical_of_found <=
4584 tmp_bbio->stripes[i].physical)
4585 continue;
4586 index_srcdev = i;
4587 found = 1;
4588 physical_of_found =
4589 tmp_bbio->stripes[i].physical;
4590 }
4591 }
4592
4593 if (found) {
4594 mirror_num = index_srcdev + 1;
4595 patch_the_first_stripe_for_dev_replace = 1;
4596 physical_to_patch_in_first_stripe = physical_of_found;
4597 } else {
4598 WARN_ON(1);
4599 ret = -EIO;
4600 kfree(tmp_bbio);
4601 goto out;
4602 }
4603
4604 kfree(tmp_bbio);
4605 } else if (mirror_num > map->num_stripes) {
4606 mirror_num = 0;
4607 }
4608
Chris Masonf2d8d742008-04-21 10:03:05 -04004609 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004610 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004611 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00004612 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00004613 do_div(stripe_nr_end, map->stripe_len);
4614 stripe_end_offset = stripe_nr_end * map->stripe_len -
4615 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004616
Li Dongyangfce3bb92011-03-24 10:24:26 +00004617 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4618 if (rw & REQ_DISCARD)
4619 num_stripes = min_t(u64, map->num_stripes,
4620 stripe_nr_end - stripe_nr_orig);
4621 stripe_index = do_div(stripe_nr, map->num_stripes);
4622 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004623 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004624 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04004625 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04004626 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004627 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01004628 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04004629 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01004630 current->pid % map->num_stripes,
4631 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004632 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004633 }
Chris Mason2fff7342008-04-29 14:12:09 -04004634
Chris Mason611f0e02008-04-03 16:29:03 -04004635 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004636 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04004637 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004638 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04004639 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004640 } else {
4641 mirror_num = 1;
4642 }
Chris Mason2fff7342008-04-29 14:12:09 -04004643
Chris Mason321aecc2008-04-16 10:49:51 -04004644 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4645 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004646
4647 stripe_index = do_div(stripe_nr, factor);
4648 stripe_index *= map->sub_stripes;
4649
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004650 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004651 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004652 else if (rw & REQ_DISCARD)
4653 num_stripes = min_t(u64, map->sub_stripes *
4654 (stripe_nr_end - stripe_nr_orig),
4655 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04004656 else if (mirror_num)
4657 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004658 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04004659 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01004660 stripe_index = find_live_mirror(fs_info, map,
4661 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04004662 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01004663 current->pid % map->sub_stripes,
4664 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04004665 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004666 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004667
4668 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4669 BTRFS_BLOCK_GROUP_RAID6)) {
4670 u64 tmp;
4671
4672 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
4673 && raid_map_ret) {
4674 int i, rot;
4675
4676 /* push stripe_nr back to the start of the full stripe */
4677 stripe_nr = raid56_full_stripe_start;
4678 do_div(stripe_nr, stripe_len);
4679
4680 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4681
4682 /* RAID[56] write or recovery. Return all stripes */
4683 num_stripes = map->num_stripes;
4684 max_errors = nr_parity_stripes(map);
4685
4686 raid_map = kmalloc(sizeof(u64) * num_stripes,
4687 GFP_NOFS);
4688 if (!raid_map) {
4689 ret = -ENOMEM;
4690 goto out;
4691 }
4692
4693 /* Work out the disk rotation on this stripe-set */
4694 tmp = stripe_nr;
4695 rot = do_div(tmp, num_stripes);
4696
4697 /* Fill in the logical address of each stripe */
4698 tmp = stripe_nr * nr_data_stripes(map);
4699 for (i = 0; i < nr_data_stripes(map); i++)
4700 raid_map[(i+rot) % num_stripes] =
4701 em->start + (tmp + i) * map->stripe_len;
4702
4703 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
4704 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4705 raid_map[(i+rot+1) % num_stripes] =
4706 RAID6_Q_STRIPE;
4707
4708 *length = map->stripe_len;
4709 stripe_index = 0;
4710 stripe_offset = 0;
4711 } else {
4712 /*
4713 * Mirror #0 or #1 means the original data block.
4714 * Mirror #2 is RAID5 parity block.
4715 * Mirror #3 is RAID6 Q block.
4716 */
4717 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4718 if (mirror_num > 1)
4719 stripe_index = nr_data_stripes(map) +
4720 mirror_num - 2;
4721
4722 /* We distribute the parity blocks across stripes */
4723 tmp = stripe_nr + stripe_index;
4724 stripe_index = do_div(tmp, map->num_stripes);
4725 }
Chris Mason8790d502008-04-03 16:29:03 -04004726 } else {
4727 /*
4728 * after this do_div call, stripe_nr is the number of stripes
4729 * on this device we have to walk to find the data, and
4730 * stripe_index is the number of our device in the stripe array
4731 */
4732 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004733 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04004734 }
Chris Mason593060d2008-03-25 16:50:33 -04004735 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04004736
Stefan Behrens472262f2012-11-06 14:43:46 +01004737 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004738 if (dev_replace_is_ongoing) {
4739 if (rw & (REQ_WRITE | REQ_DISCARD))
4740 num_alloc_stripes <<= 1;
4741 if (rw & REQ_GET_READ_MIRRORS)
4742 num_alloc_stripes++;
4743 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004744 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08004745 if (!bbio) {
4746 ret = -ENOMEM;
4747 goto out;
4748 }
4749 atomic_set(&bbio->error, 0);
4750
Li Dongyangfce3bb92011-03-24 10:24:26 +00004751 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08004752 int factor = 0;
4753 int sub_stripes = 0;
4754 u64 stripes_per_dev = 0;
4755 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04004756 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004757
4758 if (map->type &
4759 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4760 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4761 sub_stripes = 1;
4762 else
4763 sub_stripes = map->sub_stripes;
4764
4765 factor = map->num_stripes / sub_stripes;
4766 stripes_per_dev = div_u64_rem(stripe_nr_end -
4767 stripe_nr_orig,
4768 factor,
4769 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04004770 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4771 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004772 }
4773
Li Dongyangfce3bb92011-03-24 10:24:26 +00004774 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004775 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04004776 map->stripes[stripe_index].physical +
4777 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004778 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004779
Li Zefanec9ef7a2011-12-01 14:06:42 +08004780 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4781 BTRFS_BLOCK_GROUP_RAID10)) {
4782 bbio->stripes[i].length = stripes_per_dev *
4783 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004784
Li Zefanec9ef7a2011-12-01 14:06:42 +08004785 if (i / sub_stripes < remaining_stripes)
4786 bbio->stripes[i].length +=
4787 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004788
4789 /*
4790 * Special for the first stripe and
4791 * the last stripe:
4792 *
4793 * |-------|...|-------|
4794 * |----------|
4795 * off end_off
4796 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08004797 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004798 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00004799 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004800
4801 if (stripe_index >= last_stripe &&
4802 stripe_index <= (last_stripe +
4803 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08004804 bbio->stripes[i].length -=
4805 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004806
Li Zefanec9ef7a2011-12-01 14:06:42 +08004807 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00004808 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004809 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02004810 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004811
4812 stripe_index++;
4813 if (stripe_index == map->num_stripes) {
4814 /* This could only happen for RAID0/10 */
4815 stripe_index = 0;
4816 stripe_nr++;
4817 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004818 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004819 } else {
4820 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004821 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004822 map->stripes[stripe_index].physical +
4823 stripe_offset +
4824 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004825 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004826 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004827 stripe_index++;
4828 }
Chris Mason593060d2008-03-25 16:50:33 -04004829 }
Li Zefande11cc12011-12-01 12:55:47 +08004830
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004831 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
Li Zefande11cc12011-12-01 12:55:47 +08004832 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4833 BTRFS_BLOCK_GROUP_RAID10 |
David Woodhouse53b381b2013-01-29 18:40:14 -05004834 BTRFS_BLOCK_GROUP_RAID5 |
Li Zefande11cc12011-12-01 12:55:47 +08004835 BTRFS_BLOCK_GROUP_DUP)) {
4836 max_errors = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05004837 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4838 max_errors = 2;
Li Zefande11cc12011-12-01 12:55:47 +08004839 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004840 }
Li Zefande11cc12011-12-01 12:55:47 +08004841
Stefan Behrens472262f2012-11-06 14:43:46 +01004842 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
4843 dev_replace->tgtdev != NULL) {
4844 int index_where_to_add;
4845 u64 srcdev_devid = dev_replace->srcdev->devid;
4846
4847 /*
4848 * duplicate the write operations while the dev replace
4849 * procedure is running. Since the copying of the old disk
4850 * to the new disk takes place at run time while the
4851 * filesystem is mounted writable, the regular write
4852 * operations to the old disk have to be duplicated to go
4853 * to the new disk as well.
4854 * Note that device->missing is handled by the caller, and
4855 * that the write to the old disk is already set up in the
4856 * stripes array.
4857 */
4858 index_where_to_add = num_stripes;
4859 for (i = 0; i < num_stripes; i++) {
4860 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4861 /* write to new disk, too */
4862 struct btrfs_bio_stripe *new =
4863 bbio->stripes + index_where_to_add;
4864 struct btrfs_bio_stripe *old =
4865 bbio->stripes + i;
4866
4867 new->physical = old->physical;
4868 new->length = old->length;
4869 new->dev = dev_replace->tgtdev;
4870 index_where_to_add++;
4871 max_errors++;
4872 }
4873 }
4874 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004875 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
4876 dev_replace->tgtdev != NULL) {
4877 u64 srcdev_devid = dev_replace->srcdev->devid;
4878 int index_srcdev = 0;
4879 int found = 0;
4880 u64 physical_of_found = 0;
4881
4882 /*
4883 * During the dev-replace procedure, the target drive can
4884 * also be used to read data in case it is needed to repair
4885 * a corrupt block elsewhere. This is possible if the
4886 * requested area is left of the left cursor. In this area,
4887 * the target drive is a full copy of the source drive.
4888 */
4889 for (i = 0; i < num_stripes; i++) {
4890 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4891 /*
4892 * In case of DUP, in order to keep it
4893 * simple, only add the mirror with the
4894 * lowest physical address
4895 */
4896 if (found &&
4897 physical_of_found <=
4898 bbio->stripes[i].physical)
4899 continue;
4900 index_srcdev = i;
4901 found = 1;
4902 physical_of_found = bbio->stripes[i].physical;
4903 }
4904 }
4905 if (found) {
4906 u64 length = map->stripe_len;
4907
4908 if (physical_of_found + length <=
4909 dev_replace->cursor_left) {
4910 struct btrfs_bio_stripe *tgtdev_stripe =
4911 bbio->stripes + num_stripes;
4912
4913 tgtdev_stripe->physical = physical_of_found;
4914 tgtdev_stripe->length =
4915 bbio->stripes[index_srcdev].length;
4916 tgtdev_stripe->dev = dev_replace->tgtdev;
4917
4918 num_stripes++;
4919 }
4920 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004921 }
4922
Li Zefande11cc12011-12-01 12:55:47 +08004923 *bbio_ret = bbio;
4924 bbio->num_stripes = num_stripes;
4925 bbio->max_errors = max_errors;
4926 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004927
4928 /*
4929 * this is the case that REQ_READ && dev_replace_is_ongoing &&
4930 * mirror_num == num_stripes + 1 && dev_replace target drive is
4931 * available as a mirror
4932 */
4933 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
4934 WARN_ON(num_stripes > 1);
4935 bbio->stripes[0].dev = dev_replace->tgtdev;
4936 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
4937 bbio->mirror_num = map->num_stripes + 1;
4938 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004939 if (raid_map) {
4940 sort_parity_stripes(bbio, raid_map);
4941 *raid_map_ret = raid_map;
4942 }
Chris Masoncea9e442008-04-09 16:28:12 -04004943out:
Stefan Behrens472262f2012-11-06 14:43:46 +01004944 if (dev_replace_is_ongoing)
4945 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04004946 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08004947 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004948}
4949
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004950int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004951 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004952 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04004953{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004954 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004955 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04004956}
4957
Yan Zhenga512bbf2008-12-08 16:46:26 -05004958int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4959 u64 chunk_start, u64 physical, u64 devid,
4960 u64 **logical, int *naddrs, int *stripe_len)
4961{
4962 struct extent_map_tree *em_tree = &map_tree->map_tree;
4963 struct extent_map *em;
4964 struct map_lookup *map;
4965 u64 *buf;
4966 u64 bytenr;
4967 u64 length;
4968 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05004969 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05004970 int i, j, nr = 0;
4971
Chris Mason890871b2009-09-02 16:24:52 -04004972 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004973 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004974 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004975
Josef Bacik835d9742013-03-19 12:13:25 -04004976 if (!em) {
4977 printk(KERN_ERR "btrfs: couldn't find em for chunk %Lu\n",
4978 chunk_start);
4979 return -EIO;
4980 }
4981
4982 if (em->start != chunk_start) {
4983 printk(KERN_ERR "btrfs: bad chunk start, em=%Lu, wanted=%Lu\n",
4984 em->start, chunk_start);
4985 free_extent_map(em);
4986 return -EIO;
4987 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004988 map = (struct map_lookup *)em->bdev;
4989
4990 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05004991 rmap_len = map->stripe_len;
4992
Yan Zhenga512bbf2008-12-08 16:46:26 -05004993 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4994 do_div(length, map->num_stripes / map->sub_stripes);
4995 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4996 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05004997 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4998 BTRFS_BLOCK_GROUP_RAID6)) {
4999 do_div(length, nr_data_stripes(map));
5000 rmap_len = map->stripe_len * nr_data_stripes(map);
5001 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005002
5003 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005004 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005005
5006 for (i = 0; i < map->num_stripes; i++) {
5007 if (devid && map->stripes[i].dev->devid != devid)
5008 continue;
5009 if (map->stripes[i].physical > physical ||
5010 map->stripes[i].physical + length <= physical)
5011 continue;
5012
5013 stripe_nr = physical - map->stripes[i].physical;
5014 do_div(stripe_nr, map->stripe_len);
5015
5016 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5017 stripe_nr = stripe_nr * map->num_stripes + i;
5018 do_div(stripe_nr, map->sub_stripes);
5019 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5020 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005021 } /* else if RAID[56], multiply by nr_data_stripes().
5022 * Alternatively, just use rmap_len below instead of
5023 * map->stripe_len */
5024
5025 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005026 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005027 for (j = 0; j < nr; j++) {
5028 if (buf[j] == bytenr)
5029 break;
5030 }
Chris Mason934d3752008-12-08 16:43:10 -05005031 if (j == nr) {
5032 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005033 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005034 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005035 }
5036
Yan Zhenga512bbf2008-12-08 16:46:26 -05005037 *logical = buf;
5038 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005039 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005040
5041 free_extent_map(em);
5042 return 0;
5043}
5044
Jan Schmidta1d3c472011-08-04 17:15:33 +02005045static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005046{
Chris Mason9be33952013-05-17 18:30:14 -04005047 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005048 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005049
Stefan Behrens442a4f62012-05-25 16:06:08 +02005050 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005051 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005052 if (err == -EIO || err == -EREMOTEIO) {
5053 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005054 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005055 struct btrfs_device *dev;
5056
5057 BUG_ON(stripe_index >= bbio->num_stripes);
5058 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005059 if (dev->bdev) {
5060 if (bio->bi_rw & WRITE)
5061 btrfs_dev_stat_inc(dev,
5062 BTRFS_DEV_STAT_WRITE_ERRS);
5063 else
5064 btrfs_dev_stat_inc(dev,
5065 BTRFS_DEV_STAT_READ_ERRS);
5066 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5067 btrfs_dev_stat_inc(dev,
5068 BTRFS_DEV_STAT_FLUSH_ERRS);
5069 btrfs_dev_stat_print_on_error(dev);
5070 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005071 }
5072 }
Chris Mason8790d502008-04-03 16:29:03 -04005073
Jan Schmidta1d3c472011-08-04 17:15:33 +02005074 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005075 is_orig_bio = 1;
5076
Jan Schmidta1d3c472011-08-04 17:15:33 +02005077 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005078 if (!is_orig_bio) {
5079 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005080 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005081 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005082 bio->bi_private = bbio->private;
5083 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005084 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005085 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005086 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005087 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005088 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005089 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005090 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005091 /*
5092 * this bio is actually up to date, we didn't
5093 * go over the max number of errors
5094 */
5095 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005096 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005097 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005098 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04005099
5100 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005101 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005102 bio_put(bio);
5103 }
Chris Mason8790d502008-04-03 16:29:03 -04005104}
5105
Chris Mason8b712842008-06-11 16:50:36 -04005106struct async_sched {
5107 struct bio *bio;
5108 int rw;
5109 struct btrfs_fs_info *info;
5110 struct btrfs_work work;
5111};
5112
5113/*
5114 * see run_scheduled_bios for a description of why bios are collected for
5115 * async submit.
5116 *
5117 * This will add one bio to the pending list for a device and make sure
5118 * the work struct is scheduled.
5119 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005120static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5121 struct btrfs_device *device,
5122 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005123{
5124 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005125 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005126
David Woodhouse53b381b2013-01-29 18:40:14 -05005127 if (device->missing || !device->bdev) {
5128 bio_endio(bio, -EIO);
5129 return;
5130 }
5131
Chris Mason8b712842008-06-11 16:50:36 -04005132 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005133 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005134 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005135 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005136 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005137 return;
Chris Mason8b712842008-06-11 16:50:36 -04005138 }
5139
5140 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005141 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005142 * higher layers. Otherwise, the async bio makes it appear we have
5143 * made progress against dirty pages when we've really just put it
5144 * on a queue for later
5145 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005146 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005147 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005148 bio->bi_next = NULL;
5149 bio->bi_rw |= rw;
5150
5151 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005152 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005153 pending_bios = &device->pending_sync_bios;
5154 else
5155 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005156
Chris Masonffbd5172009-04-20 15:50:09 -04005157 if (pending_bios->tail)
5158 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005159
Chris Masonffbd5172009-04-20 15:50:09 -04005160 pending_bios->tail = bio;
5161 if (!pending_bios->head)
5162 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005163 if (device->running_pending)
5164 should_queue = 0;
5165
5166 spin_unlock(&device->io_lock);
5167
5168 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04005169 btrfs_queue_worker(&root->fs_info->submit_workers,
5170 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005171}
5172
Josef Bacikde1ee922012-10-19 16:50:56 -04005173static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5174 sector_t sector)
5175{
5176 struct bio_vec *prev;
5177 struct request_queue *q = bdev_get_queue(bdev);
5178 unsigned short max_sectors = queue_max_sectors(q);
5179 struct bvec_merge_data bvm = {
5180 .bi_bdev = bdev,
5181 .bi_sector = sector,
5182 .bi_rw = bio->bi_rw,
5183 };
5184
5185 if (bio->bi_vcnt == 0) {
5186 WARN_ON(1);
5187 return 1;
5188 }
5189
5190 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005191 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005192 return 0;
5193
5194 if (!q->merge_bvec_fn)
5195 return 1;
5196
5197 bvm.bi_size = bio->bi_size - prev->bv_len;
5198 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5199 return 0;
5200 return 1;
5201}
5202
5203static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5204 struct bio *bio, u64 physical, int dev_nr,
5205 int rw, int async)
5206{
5207 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5208
5209 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005210 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005211 bio->bi_end_io = btrfs_end_bio;
5212 bio->bi_sector = physical >> 9;
5213#ifdef DEBUG
5214 {
5215 struct rcu_string *name;
5216
5217 rcu_read_lock();
5218 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005219 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005220 "(%s id %llu), size=%u\n", rw,
5221 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5222 name->str, dev->devid, bio->bi_size);
5223 rcu_read_unlock();
5224 }
5225#endif
5226 bio->bi_bdev = dev->bdev;
5227 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005228 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005229 else
5230 btrfsic_submit_bio(rw, bio);
5231}
5232
5233static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5234 struct bio *first_bio, struct btrfs_device *dev,
5235 int dev_nr, int rw, int async)
5236{
5237 struct bio_vec *bvec = first_bio->bi_io_vec;
5238 struct bio *bio;
5239 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5240 u64 physical = bbio->stripes[dev_nr].physical;
5241
5242again:
5243 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5244 if (!bio)
5245 return -ENOMEM;
5246
5247 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5248 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5249 bvec->bv_offset) < bvec->bv_len) {
5250 u64 len = bio->bi_size;
5251
5252 atomic_inc(&bbio->stripes_pending);
5253 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5254 rw, async);
5255 physical += len;
5256 goto again;
5257 }
5258 bvec++;
5259 }
5260
5261 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5262 return 0;
5263}
5264
5265static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5266{
5267 atomic_inc(&bbio->error);
5268 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5269 bio->bi_private = bbio->private;
5270 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005271 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Josef Bacikde1ee922012-10-19 16:50:56 -04005272 bio->bi_sector = logical >> 9;
5273 kfree(bbio);
5274 bio_endio(bio, -EIO);
5275 }
5276}
5277
Chris Masonf1885912008-04-09 16:28:12 -04005278int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005279 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005280{
Chris Mason0b86a832008-03-24 15:01:56 -04005281 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005282 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04005283 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005284 u64 length = 0;
5285 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005286 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005287 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005288 int dev_nr = 0;
5289 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005290 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005291
Chris Masonf2d8d742008-04-21 10:03:05 -04005292 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005293 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005294
David Woodhouse53b381b2013-01-29 18:40:14 -05005295 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5296 mirror_num, &raid_map);
5297 if (ret) /* -ENOMEM */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005298 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04005299
Jan Schmidta1d3c472011-08-04 17:15:33 +02005300 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005301 bbio->orig_bio = first_bio;
5302 bbio->private = first_bio->bi_private;
5303 bbio->end_io = first_bio->bi_end_io;
5304 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5305
5306 if (raid_map) {
5307 /* In this case, map_length has been set to the length of
5308 a single stripe; not the whole write */
5309 if (rw & WRITE) {
5310 return raid56_parity_write(root, bio, bbio,
5311 raid_map, map_length);
5312 } else {
5313 return raid56_parity_recover(root, bio, bbio,
5314 raid_map, map_length,
5315 mirror_num);
5316 }
5317 }
5318
Chris Masoncea9e442008-04-09 16:28:12 -04005319 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005320 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
5321 (unsigned long long)logical,
5322 (unsigned long long)length,
5323 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005324 BUG();
5325 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005326
Chris Masond3977122009-01-05 21:25:51 -05005327 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005328 dev = bbio->stripes[dev_nr].dev;
5329 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5330 bbio_error(bbio, first_bio, logical);
5331 dev_nr++;
5332 continue;
5333 }
5334
5335 /*
5336 * Check and see if we're ok with this bio based on it's size
5337 * and offset with the given device.
5338 */
5339 if (!bio_size_ok(dev->bdev, first_bio,
5340 bbio->stripes[dev_nr].physical >> 9)) {
5341 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5342 dev_nr, rw, async_submit);
5343 BUG_ON(ret);
5344 dev_nr++;
5345 continue;
5346 }
5347
Jan Schmidta1d3c472011-08-04 17:15:33 +02005348 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005349 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005350 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005351 } else {
5352 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04005353 }
Josef Bacik606686e2012-06-04 14:03:51 -04005354
Josef Bacikde1ee922012-10-19 16:50:56 -04005355 submit_stripe_bio(root, bbio, bio,
5356 bbio->stripes[dev_nr].physical, dev_nr, rw,
5357 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005358 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005359 }
Chris Mason0b86a832008-03-24 15:01:56 -04005360 return 0;
5361}
5362
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005363struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005364 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005365{
Yan Zheng2b820322008-11-17 21:11:30 -05005366 struct btrfs_device *device;
5367 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005368
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005369 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005370 while (cur_devices) {
5371 if (!fsid ||
5372 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5373 device = __find_device(&cur_devices->devices,
5374 devid, uuid);
5375 if (device)
5376 return device;
5377 }
5378 cur_devices = cur_devices->seed;
5379 }
5380 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005381}
5382
Chris Masondfe25022008-05-13 13:46:40 -04005383static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5384 u64 devid, u8 *dev_uuid)
5385{
5386 struct btrfs_device *device;
5387 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5388
5389 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005390 if (!device)
5391 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04005392 list_add(&device->dev_list,
5393 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04005394 device->dev_root = root->fs_info->dev_root;
5395 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04005396 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05005397 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05005398 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04005399 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05005400 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04005401 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05005402 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04005403 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
5404 return device;
5405}
5406
Chris Mason0b86a832008-03-24 15:01:56 -04005407static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5408 struct extent_buffer *leaf,
5409 struct btrfs_chunk *chunk)
5410{
5411 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5412 struct map_lookup *map;
5413 struct extent_map *em;
5414 u64 logical;
5415 u64 length;
5416 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005417 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005418 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005419 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005420 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005421
Chris Masone17cade2008-04-15 15:41:47 -04005422 logical = key->offset;
5423 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005424
Chris Mason890871b2009-09-02 16:24:52 -04005425 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005426 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005427 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005428
5429 /* already mapped? */
5430 if (em && em->start <= logical && em->start + em->len > logical) {
5431 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005432 return 0;
5433 } else if (em) {
5434 free_extent_map(em);
5435 }
Chris Mason0b86a832008-03-24 15:01:56 -04005436
David Sterba172ddd62011-04-21 00:48:27 +02005437 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005438 if (!em)
5439 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005440 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5441 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005442 if (!map) {
5443 free_extent_map(em);
5444 return -ENOMEM;
5445 }
5446
5447 em->bdev = (struct block_device *)map;
5448 em->start = logical;
5449 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005450 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005451 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005452 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005453
Chris Mason593060d2008-03-25 16:50:33 -04005454 map->num_stripes = num_stripes;
5455 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5456 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5457 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5458 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5459 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005460 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005461 for (i = 0; i < num_stripes; i++) {
5462 map->stripes[i].physical =
5463 btrfs_stripe_offset_nr(leaf, chunk, i);
5464 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005465 read_extent_buffer(leaf, uuid, (unsigned long)
5466 btrfs_stripe_dev_uuid_nr(chunk, i),
5467 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005468 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5469 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005470 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005471 kfree(map);
5472 free_extent_map(em);
5473 return -EIO;
5474 }
Chris Masondfe25022008-05-13 13:46:40 -04005475 if (!map->stripes[i].dev) {
5476 map->stripes[i].dev =
5477 add_missing_dev(root, devid, uuid);
5478 if (!map->stripes[i].dev) {
5479 kfree(map);
5480 free_extent_map(em);
5481 return -EIO;
5482 }
5483 }
5484 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005485 }
5486
Chris Mason890871b2009-09-02 16:24:52 -04005487 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005488 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005489 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005490 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005491 free_extent_map(em);
5492
5493 return 0;
5494}
5495
Jeff Mahoney143bede2012-03-01 14:56:26 +01005496static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005497 struct btrfs_dev_item *dev_item,
5498 struct btrfs_device *device)
5499{
5500 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005501
5502 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005503 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5504 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005505 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5506 device->type = btrfs_device_type(leaf, dev_item);
5507 device->io_align = btrfs_device_io_align(leaf, dev_item);
5508 device->io_width = btrfs_device_io_width(leaf, dev_item);
5509 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005510 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005511 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005512
5513 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005514 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005515}
5516
Yan Zheng2b820322008-11-17 21:11:30 -05005517static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5518{
5519 struct btrfs_fs_devices *fs_devices;
5520 int ret;
5521
Li Zefanb367e472011-12-07 11:38:24 +08005522 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005523
5524 fs_devices = root->fs_info->fs_devices->seed;
5525 while (fs_devices) {
5526 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5527 ret = 0;
5528 goto out;
5529 }
5530 fs_devices = fs_devices->seed;
5531 }
5532
5533 fs_devices = find_fsid(fsid);
5534 if (!fs_devices) {
5535 ret = -ENOENT;
5536 goto out;
5537 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005538
5539 fs_devices = clone_fs_devices(fs_devices);
5540 if (IS_ERR(fs_devices)) {
5541 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005542 goto out;
5543 }
5544
Christoph Hellwig97288f22008-12-02 06:36:09 -05005545 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05005546 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02005547 if (ret) {
5548 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005549 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02005550 }
Yan Zheng2b820322008-11-17 21:11:30 -05005551
5552 if (!fs_devices->seeding) {
5553 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005554 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005555 ret = -EINVAL;
5556 goto out;
5557 }
5558
5559 fs_devices->seed = root->fs_info->fs_devices->seed;
5560 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005561out:
Yan Zheng2b820322008-11-17 21:11:30 -05005562 return ret;
5563}
5564
Chris Mason0d81ba52008-03-24 15:02:07 -04005565static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04005566 struct extent_buffer *leaf,
5567 struct btrfs_dev_item *dev_item)
5568{
5569 struct btrfs_device *device;
5570 u64 devid;
5571 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005572 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04005573 u8 dev_uuid[BTRFS_UUID_SIZE];
5574
Chris Mason0b86a832008-03-24 15:01:56 -04005575 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04005576 read_extent_buffer(leaf, dev_uuid,
5577 (unsigned long)btrfs_device_uuid(dev_item),
5578 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05005579 read_extent_buffer(leaf, fs_uuid,
5580 (unsigned long)btrfs_device_fsid(dev_item),
5581 BTRFS_UUID_SIZE);
5582
5583 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
5584 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05005585 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005586 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005587 }
5588
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005589 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05005590 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05005591 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005592 return -EIO;
5593
5594 if (!device) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005595 btrfs_warn(root->fs_info, "devid %llu missing",
5596 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05005597 device = add_missing_dev(root, devid, dev_uuid);
5598 if (!device)
5599 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05005600 } else if (!device->missing) {
5601 /*
5602 * this happens when a device that was properly setup
5603 * in the device info lists suddenly goes bad.
5604 * device->bdev is NULL, and so we have to set
5605 * device->missing to one here
5606 */
5607 root->fs_info->fs_devices->missing_devices++;
5608 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05005609 }
5610 }
5611
5612 if (device->fs_devices != root->fs_info->fs_devices) {
5613 BUG_ON(device->writeable);
5614 if (device->generation !=
5615 btrfs_device_generation(leaf, dev_item))
5616 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04005617 }
Chris Mason0b86a832008-03-24 15:01:56 -04005618
5619 fill_device_from_item(leaf, dev_item, device);
5620 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04005621 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01005622 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05005623 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04005624 spin_lock(&root->fs_info->free_chunk_lock);
5625 root->fs_info->free_chunk_space += device->total_bytes -
5626 device->bytes_used;
5627 spin_unlock(&root->fs_info->free_chunk_lock);
5628 }
Chris Mason0b86a832008-03-24 15:01:56 -04005629 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005630 return ret;
5631}
5632
Yan Zhenge4404d62008-12-12 10:03:26 -05005633int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04005634{
David Sterba6c417612011-04-13 15:41:04 +02005635 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04005636 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04005637 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04005638 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04005639 u8 *ptr;
5640 unsigned long sb_ptr;
5641 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005642 u32 num_stripes;
5643 u32 array_size;
5644 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005645 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04005646 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04005647
Yan Zhenge4404d62008-12-12 10:03:26 -05005648 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04005649 BTRFS_SUPER_INFO_SIZE);
5650 if (!sb)
5651 return -ENOMEM;
5652 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04005653 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02005654 /*
5655 * The sb extent buffer is artifical and just used to read the system array.
5656 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5657 * pages up-to-date when the page is larger: extent does not cover the
5658 * whole page and consequently check_page_uptodate does not find all
5659 * the page's extents up-to-date (the hole beyond sb),
5660 * write_extent_buffer then triggers a WARN_ON.
5661 *
5662 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5663 * but sb spans only this function. Add an explicit SetPageUptodate call
5664 * to silence the warning eg. on PowerPC 64.
5665 */
5666 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04005667 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05005668
Chris Masona061fc82008-05-07 11:43:44 -04005669 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005670 array_size = btrfs_super_sys_array_size(super_copy);
5671
Chris Mason0b86a832008-03-24 15:01:56 -04005672 ptr = super_copy->sys_chunk_array;
5673 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
5674 cur = 0;
5675
5676 while (cur < array_size) {
5677 disk_key = (struct btrfs_disk_key *)ptr;
5678 btrfs_disk_key_to_cpu(&key, disk_key);
5679
Chris Masona061fc82008-05-07 11:43:44 -04005680 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04005681 sb_ptr += len;
5682 cur += len;
5683
Chris Mason0d81ba52008-03-24 15:02:07 -04005684 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04005685 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04005686 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04005687 if (ret)
5688 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005689 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
5690 len = btrfs_chunk_item_size(num_stripes);
5691 } else {
Chris Mason84eed902008-04-25 09:04:37 -04005692 ret = -EIO;
5693 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005694 }
5695 ptr += len;
5696 sb_ptr += len;
5697 cur += len;
5698 }
Chris Masona061fc82008-05-07 11:43:44 -04005699 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04005700 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005701}
5702
5703int btrfs_read_chunk_tree(struct btrfs_root *root)
5704{
5705 struct btrfs_path *path;
5706 struct extent_buffer *leaf;
5707 struct btrfs_key key;
5708 struct btrfs_key found_key;
5709 int ret;
5710 int slot;
5711
5712 root = root->fs_info->chunk_root;
5713
5714 path = btrfs_alloc_path();
5715 if (!path)
5716 return -ENOMEM;
5717
Li Zefanb367e472011-12-07 11:38:24 +08005718 mutex_lock(&uuid_mutex);
5719 lock_chunks(root);
5720
Chris Mason0b86a832008-03-24 15:01:56 -04005721 /* first we search for all of the device items, and then we
5722 * read in all of the chunk items. This way we can create chunk
5723 * mappings that reference all of the devices that are afound
5724 */
5725 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5726 key.offset = 0;
5727 key.type = 0;
5728again:
5729 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00005730 if (ret < 0)
5731 goto error;
Chris Masond3977122009-01-05 21:25:51 -05005732 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005733 leaf = path->nodes[0];
5734 slot = path->slots[0];
5735 if (slot >= btrfs_header_nritems(leaf)) {
5736 ret = btrfs_next_leaf(root, path);
5737 if (ret == 0)
5738 continue;
5739 if (ret < 0)
5740 goto error;
5741 break;
5742 }
5743 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5744 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5745 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
5746 break;
5747 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5748 struct btrfs_dev_item *dev_item;
5749 dev_item = btrfs_item_ptr(leaf, slot,
5750 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04005751 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05005752 if (ret)
5753 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005754 }
5755 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5756 struct btrfs_chunk *chunk;
5757 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5758 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05005759 if (ret)
5760 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005761 }
5762 path->slots[0]++;
5763 }
5764 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5765 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005766 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005767 goto again;
5768 }
Chris Mason0b86a832008-03-24 15:01:56 -04005769 ret = 0;
5770error:
Li Zefanb367e472011-12-07 11:38:24 +08005771 unlock_chunks(root);
5772 mutex_unlock(&uuid_mutex);
5773
Yan Zheng2b820322008-11-17 21:11:30 -05005774 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005775 return ret;
5776}
Stefan Behrens442a4f62012-05-25 16:06:08 +02005777
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005778static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
5779{
5780 int i;
5781
5782 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5783 btrfs_dev_stat_reset(dev, i);
5784}
5785
5786int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
5787{
5788 struct btrfs_key key;
5789 struct btrfs_key found_key;
5790 struct btrfs_root *dev_root = fs_info->dev_root;
5791 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5792 struct extent_buffer *eb;
5793 int slot;
5794 int ret = 0;
5795 struct btrfs_device *device;
5796 struct btrfs_path *path = NULL;
5797 int i;
5798
5799 path = btrfs_alloc_path();
5800 if (!path) {
5801 ret = -ENOMEM;
5802 goto out;
5803 }
5804
5805 mutex_lock(&fs_devices->device_list_mutex);
5806 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5807 int item_size;
5808 struct btrfs_dev_stats_item *ptr;
5809
5810 key.objectid = 0;
5811 key.type = BTRFS_DEV_STATS_KEY;
5812 key.offset = device->devid;
5813 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
5814 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005815 __btrfs_reset_dev_stats(device);
5816 device->dev_stats_valid = 1;
5817 btrfs_release_path(path);
5818 continue;
5819 }
5820 slot = path->slots[0];
5821 eb = path->nodes[0];
5822 btrfs_item_key_to_cpu(eb, &found_key, slot);
5823 item_size = btrfs_item_size_nr(eb, slot);
5824
5825 ptr = btrfs_item_ptr(eb, slot,
5826 struct btrfs_dev_stats_item);
5827
5828 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5829 if (item_size >= (1 + i) * sizeof(__le64))
5830 btrfs_dev_stat_set(device, i,
5831 btrfs_dev_stats_value(eb, ptr, i));
5832 else
5833 btrfs_dev_stat_reset(device, i);
5834 }
5835
5836 device->dev_stats_valid = 1;
5837 btrfs_dev_stat_print_on_load(device);
5838 btrfs_release_path(path);
5839 }
5840 mutex_unlock(&fs_devices->device_list_mutex);
5841
5842out:
5843 btrfs_free_path(path);
5844 return ret < 0 ? ret : 0;
5845}
5846
5847static int update_dev_stat_item(struct btrfs_trans_handle *trans,
5848 struct btrfs_root *dev_root,
5849 struct btrfs_device *device)
5850{
5851 struct btrfs_path *path;
5852 struct btrfs_key key;
5853 struct extent_buffer *eb;
5854 struct btrfs_dev_stats_item *ptr;
5855 int ret;
5856 int i;
5857
5858 key.objectid = 0;
5859 key.type = BTRFS_DEV_STATS_KEY;
5860 key.offset = device->devid;
5861
5862 path = btrfs_alloc_path();
5863 BUG_ON(!path);
5864 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
5865 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005866 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
5867 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005868 goto out;
5869 }
5870
5871 if (ret == 0 &&
5872 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
5873 /* need to delete old one and insert a new one */
5874 ret = btrfs_del_item(trans, dev_root, path);
5875 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005876 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5877 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005878 goto out;
5879 }
5880 ret = 1;
5881 }
5882
5883 if (ret == 1) {
5884 /* need to insert a new item */
5885 btrfs_release_path(path);
5886 ret = btrfs_insert_empty_item(trans, dev_root, path,
5887 &key, sizeof(*ptr));
5888 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005889 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5890 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005891 goto out;
5892 }
5893 }
5894
5895 eb = path->nodes[0];
5896 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5897 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5898 btrfs_set_dev_stats_value(eb, ptr, i,
5899 btrfs_dev_stat_read(device, i));
5900 btrfs_mark_buffer_dirty(eb);
5901
5902out:
5903 btrfs_free_path(path);
5904 return ret;
5905}
5906
5907/*
5908 * called from commit_transaction. Writes all changed device stats to disk.
5909 */
5910int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5911 struct btrfs_fs_info *fs_info)
5912{
5913 struct btrfs_root *dev_root = fs_info->dev_root;
5914 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5915 struct btrfs_device *device;
5916 int ret = 0;
5917
5918 mutex_lock(&fs_devices->device_list_mutex);
5919 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5920 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5921 continue;
5922
5923 ret = update_dev_stat_item(trans, dev_root, device);
5924 if (!ret)
5925 device->dev_stats_dirty = 0;
5926 }
5927 mutex_unlock(&fs_devices->device_list_mutex);
5928
5929 return ret;
5930}
5931
Stefan Behrens442a4f62012-05-25 16:06:08 +02005932void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5933{
5934 btrfs_dev_stat_inc(dev, index);
5935 btrfs_dev_stat_print_on_error(dev);
5936}
5937
Eric Sandeen48a3b632013-04-25 20:41:01 +00005938static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02005939{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005940 if (!dev->dev_stats_valid)
5941 return;
Josef Bacik606686e2012-06-04 14:03:51 -04005942 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02005943 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04005944 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02005945 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5946 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5947 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5948 btrfs_dev_stat_read(dev,
5949 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5950 btrfs_dev_stat_read(dev,
5951 BTRFS_DEV_STAT_GENERATION_ERRS));
5952}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005953
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005954static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5955{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06005956 int i;
5957
5958 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5959 if (btrfs_dev_stat_read(dev, i) != 0)
5960 break;
5961 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5962 return; /* all values == 0, suppress message */
5963
Josef Bacik606686e2012-06-04 14:03:51 -04005964 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5965 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005966 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5967 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5968 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5969 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5970 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5971}
5972
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005973int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06005974 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005975{
5976 struct btrfs_device *dev;
5977 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5978 int i;
5979
5980 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005981 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005982 mutex_unlock(&fs_devices->device_list_mutex);
5983
5984 if (!dev) {
5985 printk(KERN_WARNING
5986 "btrfs: get dev_stats failed, device not found\n");
5987 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005988 } else if (!dev->dev_stats_valid) {
5989 printk(KERN_WARNING
5990 "btrfs: get dev_stats failed, not yet valid\n");
5991 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06005992 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005993 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5994 if (stats->nr_items > i)
5995 stats->values[i] =
5996 btrfs_dev_stat_read_and_reset(dev, i);
5997 else
5998 btrfs_dev_stat_reset(dev, i);
5999 }
6000 } else {
6001 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6002 if (stats->nr_items > i)
6003 stats->values[i] = btrfs_dev_stat_read(dev, i);
6004 }
6005 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6006 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6007 return 0;
6008}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006009
6010int btrfs_scratch_superblock(struct btrfs_device *device)
6011{
6012 struct buffer_head *bh;
6013 struct btrfs_super_block *disk_super;
6014
6015 bh = btrfs_read_dev_super(device->bdev);
6016 if (!bh)
6017 return -EINVAL;
6018 disk_super = (struct btrfs_super_block *)bh->b_data;
6019
6020 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6021 set_buffer_dirty(bh);
6022 sync_dirty_buffer(bh);
6023 brelse(bh);
6024
6025 return 0;
6026}