blob: cd8172dcd7542bcffa1890b9a3dcfffc6db06310 [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) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001631 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001632 break;
1633 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001634 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001635 fs_devices->seed = cur_devices->seed;
1636 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001637 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001638 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001639 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001640 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001641 }
1642
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001643 root->fs_info->num_tolerated_disk_barrier_failures =
1644 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1645
Yan Zheng2b820322008-11-17 21:11:30 -05001646 /*
1647 * at this point, the device is zero sized. We want to
1648 * remove it from the devices list and zero out the old super
1649 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001650 if (clear_super && disk_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001651 /* make sure this device isn't detected as part of
1652 * the FS anymore
1653 */
1654 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1655 set_buffer_dirty(bh);
1656 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001657 }
Chris Masona061fc82008-05-07 11:43:44 -04001658
Chris Masona061fc82008-05-07 11:43:44 -04001659 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001660
Qu Wenruode091432014-04-16 17:02:32 +08001661 if (bdev) {
1662 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001663 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001664
Qu Wenruode091432014-04-16 17:02:32 +08001665 /* Update ctime/mtime for device path for libblkid */
1666 update_dev_time(device_path);
1667 }
1668
Chris Masona061fc82008-05-07 11:43:44 -04001669error_brelse:
1670 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001671 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001672 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001673out:
1674 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001675 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001676error_undo:
1677 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001678 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001679 list_add(&device->dev_alloc_list,
1680 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001681 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001682 root->fs_info->fs_devices->rw_devices++;
1683 }
1684 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001685}
1686
Stefan Behrense93c89c2012-11-05 17:33:06 +01001687void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1688 struct btrfs_device *srcdev)
1689{
1690 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1691 list_del_rcu(&srcdev->dev_list);
1692 list_del_rcu(&srcdev->dev_alloc_list);
1693 fs_info->fs_devices->num_devices--;
1694 if (srcdev->missing) {
1695 fs_info->fs_devices->missing_devices--;
1696 fs_info->fs_devices->rw_devices++;
1697 }
1698 if (srcdev->can_discard)
1699 fs_info->fs_devices->num_can_discard--;
1700 if (srcdev->bdev)
1701 fs_info->fs_devices->open_devices--;
1702
1703 call_rcu(&srcdev->rcu, free_device);
1704}
1705
1706void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1707 struct btrfs_device *tgtdev)
1708{
1709 struct btrfs_device *next_device;
1710
1711 WARN_ON(!tgtdev);
1712 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1713 if (tgtdev->bdev) {
1714 btrfs_scratch_superblock(tgtdev);
1715 fs_info->fs_devices->open_devices--;
1716 }
1717 fs_info->fs_devices->num_devices--;
1718 if (tgtdev->can_discard)
1719 fs_info->fs_devices->num_can_discard++;
1720
1721 next_device = list_entry(fs_info->fs_devices->devices.next,
1722 struct btrfs_device, dev_list);
1723 if (tgtdev->bdev == fs_info->sb->s_bdev)
1724 fs_info->sb->s_bdev = next_device->bdev;
1725 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1726 fs_info->fs_devices->latest_bdev = next_device->bdev;
1727 list_del_rcu(&tgtdev->dev_list);
1728
1729 call_rcu(&tgtdev->rcu, free_device);
1730
1731 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1732}
1733
Eric Sandeen48a3b632013-04-25 20:41:01 +00001734static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1735 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001736{
1737 int ret = 0;
1738 struct btrfs_super_block *disk_super;
1739 u64 devid;
1740 u8 *dev_uuid;
1741 struct block_device *bdev;
1742 struct buffer_head *bh;
1743
1744 *device = NULL;
1745 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1746 root->fs_info->bdev_holder, 0, &bdev, &bh);
1747 if (ret)
1748 return ret;
1749 disk_super = (struct btrfs_super_block *)bh->b_data;
1750 devid = btrfs_stack_device_id(&disk_super->dev_item);
1751 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001752 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001753 disk_super->fsid);
1754 brelse(bh);
1755 if (!*device)
1756 ret = -ENOENT;
1757 blkdev_put(bdev, FMODE_READ);
1758 return ret;
1759}
1760
1761int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1762 char *device_path,
1763 struct btrfs_device **device)
1764{
1765 *device = NULL;
1766 if (strcmp(device_path, "missing") == 0) {
1767 struct list_head *devices;
1768 struct btrfs_device *tmp;
1769
1770 devices = &root->fs_info->fs_devices->devices;
1771 /*
1772 * It is safe to read the devices since the volume_mutex
1773 * is held by the caller.
1774 */
1775 list_for_each_entry(tmp, devices, dev_list) {
1776 if (tmp->in_fs_metadata && !tmp->bdev) {
1777 *device = tmp;
1778 break;
1779 }
1780 }
1781
1782 if (!*device) {
1783 pr_err("btrfs: no missing device found\n");
1784 return -ENOENT;
1785 }
1786
1787 return 0;
1788 } else {
1789 return btrfs_find_device_by_path(root, device_path, device);
1790 }
1791}
1792
Yan Zheng2b820322008-11-17 21:11:30 -05001793/*
1794 * does all the dirty work required for changing file system's UUID.
1795 */
Li Zefan125ccb02011-12-08 15:07:24 +08001796static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001797{
1798 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1799 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001800 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001801 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001802 struct btrfs_device *device;
1803 u64 super_flags;
1804
1805 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001806 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001807 return -EINVAL;
1808
Yan Zhenge4404d62008-12-12 10:03:26 -05001809 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1810 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001811 return -ENOMEM;
1812
Yan Zhenge4404d62008-12-12 10:03:26 -05001813 old_devices = clone_fs_devices(fs_devices);
1814 if (IS_ERR(old_devices)) {
1815 kfree(seed_devices);
1816 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001817 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001818
Yan Zheng2b820322008-11-17 21:11:30 -05001819 list_add(&old_devices->list, &fs_uuids);
1820
Yan Zhenge4404d62008-12-12 10:03:26 -05001821 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1822 seed_devices->opened = 1;
1823 INIT_LIST_HEAD(&seed_devices->devices);
1824 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001825 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001826
1827 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001828 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1829 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001830 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1831
Yan Zhenge4404d62008-12-12 10:03:26 -05001832 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1833 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1834 device->fs_devices = seed_devices;
1835 }
1836
Yan Zheng2b820322008-11-17 21:11:30 -05001837 fs_devices->seeding = 0;
1838 fs_devices->num_devices = 0;
1839 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001840 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001841
1842 generate_random_uuid(fs_devices->fsid);
1843 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1844 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1845 super_flags = btrfs_super_flags(disk_super) &
1846 ~BTRFS_SUPER_FLAG_SEEDING;
1847 btrfs_set_super_flags(disk_super, super_flags);
1848
1849 return 0;
1850}
1851
1852/*
1853 * strore the expected generation for seed devices in device items.
1854 */
1855static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1856 struct btrfs_root *root)
1857{
1858 struct btrfs_path *path;
1859 struct extent_buffer *leaf;
1860 struct btrfs_dev_item *dev_item;
1861 struct btrfs_device *device;
1862 struct btrfs_key key;
1863 u8 fs_uuid[BTRFS_UUID_SIZE];
1864 u8 dev_uuid[BTRFS_UUID_SIZE];
1865 u64 devid;
1866 int ret;
1867
1868 path = btrfs_alloc_path();
1869 if (!path)
1870 return -ENOMEM;
1871
1872 root = root->fs_info->chunk_root;
1873 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1874 key.offset = 0;
1875 key.type = BTRFS_DEV_ITEM_KEY;
1876
1877 while (1) {
1878 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1879 if (ret < 0)
1880 goto error;
1881
1882 leaf = path->nodes[0];
1883next_slot:
1884 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1885 ret = btrfs_next_leaf(root, path);
1886 if (ret > 0)
1887 break;
1888 if (ret < 0)
1889 goto error;
1890 leaf = path->nodes[0];
1891 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001892 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001893 continue;
1894 }
1895
1896 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1897 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1898 key.type != BTRFS_DEV_ITEM_KEY)
1899 break;
1900
1901 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1902 struct btrfs_dev_item);
1903 devid = btrfs_device_id(leaf, dev_item);
1904 read_extent_buffer(leaf, dev_uuid,
1905 (unsigned long)btrfs_device_uuid(dev_item),
1906 BTRFS_UUID_SIZE);
1907 read_extent_buffer(leaf, fs_uuid,
1908 (unsigned long)btrfs_device_fsid(dev_item),
1909 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001910 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1911 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001912 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001913
1914 if (device->fs_devices->seeding) {
1915 btrfs_set_device_generation(leaf, dev_item,
1916 device->generation);
1917 btrfs_mark_buffer_dirty(leaf);
1918 }
1919
1920 path->slots[0]++;
1921 goto next_slot;
1922 }
1923 ret = 0;
1924error:
1925 btrfs_free_path(path);
1926 return ret;
1927}
1928
Chris Mason788f20e2008-04-28 15:29:42 -04001929int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1930{
Josef Bacikd5e20032011-08-04 14:52:27 +00001931 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001932 struct btrfs_trans_handle *trans;
1933 struct btrfs_device *device;
1934 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001935 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001936 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001937 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001938 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001939 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001940 int ret = 0;
1941
Yan Zheng2b820322008-11-17 21:11:30 -05001942 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001943 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001944
Li Zefana5d16332011-12-07 20:08:40 -05001945 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001946 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001947 if (IS_ERR(bdev))
1948 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001949
Yan Zheng2b820322008-11-17 21:11:30 -05001950 if (root->fs_info->fs_devices->seeding) {
1951 seeding_dev = 1;
1952 down_write(&sb->s_umount);
1953 mutex_lock(&uuid_mutex);
1954 }
1955
Chris Mason8c8bee12008-09-29 11:19:10 -04001956 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001957
Chris Mason788f20e2008-04-28 15:29:42 -04001958 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00001959
1960 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001961 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001962 if (device->bdev == bdev) {
1963 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00001964 mutex_unlock(
1965 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001966 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001967 }
1968 }
Liu Bod25628b2012-11-14 14:35:30 +00001969 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001970
1971 device = kzalloc(sizeof(*device), GFP_NOFS);
1972 if (!device) {
1973 /* we can safely leave the fs_devices entry around */
1974 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001975 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001976 }
1977
Josef Bacik606686e2012-06-04 14:03:51 -04001978 name = rcu_string_strdup(device_path, GFP_NOFS);
1979 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001980 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001981 ret = -ENOMEM;
1982 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001983 }
Josef Bacik606686e2012-06-04 14:03:51 -04001984 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001985
1986 ret = find_next_devid(root, &device->devid);
1987 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001988 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001989 kfree(device);
1990 goto error;
1991 }
1992
Yan, Zhenga22285a2010-05-16 10:48:46 -04001993 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001994 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001995 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001996 kfree(device);
1997 ret = PTR_ERR(trans);
1998 goto error;
1999 }
2000
Yan Zheng2b820322008-11-17 21:11:30 -05002001 lock_chunks(root);
2002
Josef Bacikd5e20032011-08-04 14:52:27 +00002003 q = bdev_get_queue(bdev);
2004 if (blk_queue_discard(q))
2005 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002006 device->writeable = 1;
2007 device->work.func = pending_bios_fn;
2008 generate_random_uuid(device->uuid);
2009 spin_lock_init(&device->io_lock);
2010 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002011 device->io_width = root->sectorsize;
2012 device->io_align = root->sectorsize;
2013 device->sector_size = root->sectorsize;
2014 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002015 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002016 device->dev_root = root->fs_info->dev_root;
2017 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002018 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002019 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002020 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002021 set_blocksize(device->bdev, 4096);
2022
Yan Zheng2b820322008-11-17 21:11:30 -05002023 if (seeding_dev) {
2024 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002025 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002026 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002027 }
2028
2029 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002030
Chris Masone5e9a522009-06-10 15:17:02 -04002031 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002032 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002033 list_add(&device->dev_alloc_list,
2034 &root->fs_info->fs_devices->alloc_list);
2035 root->fs_info->fs_devices->num_devices++;
2036 root->fs_info->fs_devices->open_devices++;
2037 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002038 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002039 if (device->can_discard)
2040 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002041 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2042
Josef Bacik2bf64752011-09-26 17:12:22 -04002043 spin_lock(&root->fs_info->free_chunk_lock);
2044 root->fs_info->free_chunk_space += device->total_bytes;
2045 spin_unlock(&root->fs_info->free_chunk_lock);
2046
Chris Masonc2898112009-06-10 09:51:32 -04002047 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2048 root->fs_info->fs_devices->rotating = 1;
2049
David Sterba6c417612011-04-13 15:41:04 +02002050 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2051 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002052 total_bytes + device->total_bytes);
2053
David Sterba6c417612011-04-13 15:41:04 +02002054 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2055 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002056 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04002057 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002058
Yan Zheng2b820322008-11-17 21:11:30 -05002059 if (seeding_dev) {
2060 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002061 if (ret) {
2062 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002063 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002064 }
Yan Zheng2b820322008-11-17 21:11:30 -05002065 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002066 if (ret) {
2067 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002068 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002069 }
Yan Zheng2b820322008-11-17 21:11:30 -05002070 } else {
2071 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002072 if (ret) {
2073 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002074 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002075 }
Yan Zheng2b820322008-11-17 21:11:30 -05002076 }
2077
Chris Mason913d9522009-03-10 13:17:18 -04002078 /*
2079 * we've got more storage, clear any full flags on the space
2080 * infos
2081 */
2082 btrfs_clear_space_info_full(root->fs_info);
2083
Chris Mason7d9eb122008-07-08 14:19:17 -04002084 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002085 root->fs_info->num_tolerated_disk_barrier_failures =
2086 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002087 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002088
2089 if (seeding_dev) {
2090 mutex_unlock(&uuid_mutex);
2091 up_write(&sb->s_umount);
2092
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002093 if (ret) /* transaction commit */
2094 return ret;
2095
Yan Zheng2b820322008-11-17 21:11:30 -05002096 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002097 if (ret < 0)
2098 btrfs_error(root->fs_info, ret,
2099 "Failed to relocate sys chunks after "
2100 "device initialization. This can be fixed "
2101 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002102 trans = btrfs_attach_transaction(root);
2103 if (IS_ERR(trans)) {
2104 if (PTR_ERR(trans) == -ENOENT)
2105 return 0;
2106 return PTR_ERR(trans);
2107 }
2108 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002109 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002110
Qu Wenruode091432014-04-16 17:02:32 +08002111 /* Update ctime/mtime for libblkid */
2112 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002113 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002114
2115error_trans:
2116 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002117 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002118 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002119 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002120error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002121 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002122 if (seeding_dev) {
2123 mutex_unlock(&uuid_mutex);
2124 up_write(&sb->s_umount);
2125 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002126 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002127}
2128
Stefan Behrense93c89c2012-11-05 17:33:06 +01002129int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2130 struct btrfs_device **device_out)
2131{
2132 struct request_queue *q;
2133 struct btrfs_device *device;
2134 struct block_device *bdev;
2135 struct btrfs_fs_info *fs_info = root->fs_info;
2136 struct list_head *devices;
2137 struct rcu_string *name;
2138 int ret = 0;
2139
2140 *device_out = NULL;
2141 if (fs_info->fs_devices->seeding)
2142 return -EINVAL;
2143
2144 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2145 fs_info->bdev_holder);
2146 if (IS_ERR(bdev))
2147 return PTR_ERR(bdev);
2148
2149 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2150
2151 devices = &fs_info->fs_devices->devices;
2152 list_for_each_entry(device, devices, dev_list) {
2153 if (device->bdev == bdev) {
2154 ret = -EEXIST;
2155 goto error;
2156 }
2157 }
2158
2159 device = kzalloc(sizeof(*device), GFP_NOFS);
2160 if (!device) {
2161 ret = -ENOMEM;
2162 goto error;
2163 }
2164
2165 name = rcu_string_strdup(device_path, GFP_NOFS);
2166 if (!name) {
2167 kfree(device);
2168 ret = -ENOMEM;
2169 goto error;
2170 }
2171 rcu_assign_pointer(device->name, name);
2172
2173 q = bdev_get_queue(bdev);
2174 if (blk_queue_discard(q))
2175 device->can_discard = 1;
2176 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2177 device->writeable = 1;
2178 device->work.func = pending_bios_fn;
2179 generate_random_uuid(device->uuid);
2180 device->devid = BTRFS_DEV_REPLACE_DEVID;
2181 spin_lock_init(&device->io_lock);
2182 device->generation = 0;
2183 device->io_width = root->sectorsize;
2184 device->io_align = root->sectorsize;
2185 device->sector_size = root->sectorsize;
2186 device->total_bytes = i_size_read(bdev->bd_inode);
2187 device->disk_total_bytes = device->total_bytes;
2188 device->dev_root = fs_info->dev_root;
2189 device->bdev = bdev;
2190 device->in_fs_metadata = 1;
2191 device->is_tgtdev_for_dev_replace = 1;
2192 device->mode = FMODE_EXCL;
2193 set_blocksize(device->bdev, 4096);
2194 device->fs_devices = fs_info->fs_devices;
2195 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2196 fs_info->fs_devices->num_devices++;
2197 fs_info->fs_devices->open_devices++;
2198 if (device->can_discard)
2199 fs_info->fs_devices->num_can_discard++;
2200 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2201
2202 *device_out = device;
2203 return ret;
2204
2205error:
2206 blkdev_put(bdev, FMODE_EXCL);
2207 return ret;
2208}
2209
2210void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2211 struct btrfs_device *tgtdev)
2212{
2213 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2214 tgtdev->io_width = fs_info->dev_root->sectorsize;
2215 tgtdev->io_align = fs_info->dev_root->sectorsize;
2216 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2217 tgtdev->dev_root = fs_info->dev_root;
2218 tgtdev->in_fs_metadata = 1;
2219}
2220
Chris Masond3977122009-01-05 21:25:51 -05002221static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2222 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002223{
2224 int ret;
2225 struct btrfs_path *path;
2226 struct btrfs_root *root;
2227 struct btrfs_dev_item *dev_item;
2228 struct extent_buffer *leaf;
2229 struct btrfs_key key;
2230
2231 root = device->dev_root->fs_info->chunk_root;
2232
2233 path = btrfs_alloc_path();
2234 if (!path)
2235 return -ENOMEM;
2236
2237 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2238 key.type = BTRFS_DEV_ITEM_KEY;
2239 key.offset = device->devid;
2240
2241 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2242 if (ret < 0)
2243 goto out;
2244
2245 if (ret > 0) {
2246 ret = -ENOENT;
2247 goto out;
2248 }
2249
2250 leaf = path->nodes[0];
2251 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2252
2253 btrfs_set_device_id(leaf, dev_item, device->devid);
2254 btrfs_set_device_type(leaf, dev_item, device->type);
2255 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2256 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2257 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002258 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002259 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2260 btrfs_mark_buffer_dirty(leaf);
2261
2262out:
2263 btrfs_free_path(path);
2264 return ret;
2265}
2266
Chris Mason7d9eb122008-07-08 14:19:17 -04002267static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002268 struct btrfs_device *device, u64 new_size)
2269{
2270 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002271 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002272 u64 old_total = btrfs_super_total_bytes(super_copy);
2273 u64 diff = new_size - device->total_bytes;
2274
Yan Zheng2b820322008-11-17 21:11:30 -05002275 if (!device->writeable)
2276 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002277 if (new_size <= device->total_bytes ||
2278 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002279 return -EINVAL;
2280
Chris Mason8f18cf12008-04-25 16:53:30 -04002281 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002282 device->fs_devices->total_rw_bytes += diff;
2283
2284 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002285 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002286 btrfs_clear_space_info_full(device->dev_root->fs_info);
2287
Chris Mason8f18cf12008-04-25 16:53:30 -04002288 return btrfs_update_device(trans, device);
2289}
2290
Chris Mason7d9eb122008-07-08 14:19:17 -04002291int btrfs_grow_device(struct btrfs_trans_handle *trans,
2292 struct btrfs_device *device, u64 new_size)
2293{
2294 int ret;
2295 lock_chunks(device->dev_root);
2296 ret = __btrfs_grow_device(trans, device, new_size);
2297 unlock_chunks(device->dev_root);
2298 return ret;
2299}
2300
Chris Mason8f18cf12008-04-25 16:53:30 -04002301static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2302 struct btrfs_root *root,
2303 u64 chunk_tree, u64 chunk_objectid,
2304 u64 chunk_offset)
2305{
2306 int ret;
2307 struct btrfs_path *path;
2308 struct btrfs_key key;
2309
2310 root = root->fs_info->chunk_root;
2311 path = btrfs_alloc_path();
2312 if (!path)
2313 return -ENOMEM;
2314
2315 key.objectid = chunk_objectid;
2316 key.offset = chunk_offset;
2317 key.type = BTRFS_CHUNK_ITEM_KEY;
2318
2319 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002320 if (ret < 0)
2321 goto out;
2322 else if (ret > 0) { /* Logic error or corruption */
2323 btrfs_error(root->fs_info, -ENOENT,
2324 "Failed lookup while freeing chunk.");
2325 ret = -ENOENT;
2326 goto out;
2327 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002328
2329 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002330 if (ret < 0)
2331 btrfs_error(root->fs_info, ret,
2332 "Failed to delete chunk item.");
2333out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002334 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002335 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002336}
2337
Christoph Hellwigb2950862008-12-02 09:54:17 -05002338static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002339 chunk_offset)
2340{
David Sterba6c417612011-04-13 15:41:04 +02002341 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002342 struct btrfs_disk_key *disk_key;
2343 struct btrfs_chunk *chunk;
2344 u8 *ptr;
2345 int ret = 0;
2346 u32 num_stripes;
2347 u32 array_size;
2348 u32 len = 0;
2349 u32 cur;
2350 struct btrfs_key key;
2351
2352 array_size = btrfs_super_sys_array_size(super_copy);
2353
2354 ptr = super_copy->sys_chunk_array;
2355 cur = 0;
2356
2357 while (cur < array_size) {
2358 disk_key = (struct btrfs_disk_key *)ptr;
2359 btrfs_disk_key_to_cpu(&key, disk_key);
2360
2361 len = sizeof(*disk_key);
2362
2363 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2364 chunk = (struct btrfs_chunk *)(ptr + len);
2365 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2366 len += btrfs_chunk_item_size(num_stripes);
2367 } else {
2368 ret = -EIO;
2369 break;
2370 }
2371 if (key.objectid == chunk_objectid &&
2372 key.offset == chunk_offset) {
2373 memmove(ptr, ptr + len, array_size - (cur + len));
2374 array_size -= len;
2375 btrfs_set_super_sys_array_size(super_copy, array_size);
2376 } else {
2377 ptr += len;
2378 cur += len;
2379 }
2380 }
2381 return ret;
2382}
2383
Christoph Hellwigb2950862008-12-02 09:54:17 -05002384static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002385 u64 chunk_tree, u64 chunk_objectid,
2386 u64 chunk_offset)
2387{
2388 struct extent_map_tree *em_tree;
2389 struct btrfs_root *extent_root;
2390 struct btrfs_trans_handle *trans;
2391 struct extent_map *em;
2392 struct map_lookup *map;
2393 int ret;
2394 int i;
2395
2396 root = root->fs_info->chunk_root;
2397 extent_root = root->fs_info->extent_root;
2398 em_tree = &root->fs_info->mapping_tree.map_tree;
2399
Josef Bacikba1bf482009-09-11 16:11:19 -04002400 ret = btrfs_can_relocate(extent_root, chunk_offset);
2401 if (ret)
2402 return -ENOSPC;
2403
Chris Mason8f18cf12008-04-25 16:53:30 -04002404 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002405 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002406 if (ret)
2407 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002408
Yan, Zhenga22285a2010-05-16 10:48:46 -04002409 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002410 if (IS_ERR(trans)) {
2411 ret = PTR_ERR(trans);
2412 btrfs_std_error(root->fs_info, ret);
2413 return ret;
2414 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002415
Chris Mason7d9eb122008-07-08 14:19:17 -04002416 lock_chunks(root);
2417
Chris Mason8f18cf12008-04-25 16:53:30 -04002418 /*
2419 * step two, delete the device extents and the
2420 * chunk tree entries
2421 */
Chris Mason890871b2009-09-02 16:24:52 -04002422 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002423 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002424 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002425
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002426 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002427 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002428 map = (struct map_lookup *)em->bdev;
2429
2430 for (i = 0; i < map->num_stripes; i++) {
2431 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2432 map->stripes[i].physical);
2433 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002434
Chris Masondfe25022008-05-13 13:46:40 -04002435 if (map->stripes[i].dev) {
2436 ret = btrfs_update_device(trans, map->stripes[i].dev);
2437 BUG_ON(ret);
2438 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002439 }
2440 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2441 chunk_offset);
2442
2443 BUG_ON(ret);
2444
liubo1abe9b82011-03-24 11:18:59 +00002445 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2446
Chris Mason8f18cf12008-04-25 16:53:30 -04002447 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2448 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2449 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002450 }
2451
Zheng Yan1a40e232008-09-26 10:09:34 -04002452 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2453 BUG_ON(ret);
2454
Chris Mason890871b2009-09-02 16:24:52 -04002455 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002456 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002457 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002458
Chris Mason8f18cf12008-04-25 16:53:30 -04002459 kfree(map);
2460 em->bdev = NULL;
2461
2462 /* once for the tree */
2463 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002464 /* once for us */
2465 free_extent_map(em);
2466
Chris Mason7d9eb122008-07-08 14:19:17 -04002467 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002468 btrfs_end_transaction(trans, root);
2469 return 0;
2470}
2471
Yan Zheng2b820322008-11-17 21:11:30 -05002472static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2473{
2474 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2475 struct btrfs_path *path;
2476 struct extent_buffer *leaf;
2477 struct btrfs_chunk *chunk;
2478 struct btrfs_key key;
2479 struct btrfs_key found_key;
2480 u64 chunk_tree = chunk_root->root_key.objectid;
2481 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002482 bool retried = false;
2483 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002484 int ret;
2485
2486 path = btrfs_alloc_path();
2487 if (!path)
2488 return -ENOMEM;
2489
Josef Bacikba1bf482009-09-11 16:11:19 -04002490again:
Yan Zheng2b820322008-11-17 21:11:30 -05002491 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2492 key.offset = (u64)-1;
2493 key.type = BTRFS_CHUNK_ITEM_KEY;
2494
2495 while (1) {
2496 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2497 if (ret < 0)
2498 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002499 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002500
2501 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2502 key.type);
2503 if (ret < 0)
2504 goto error;
2505 if (ret > 0)
2506 break;
2507
2508 leaf = path->nodes[0];
2509 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2510
2511 chunk = btrfs_item_ptr(leaf, path->slots[0],
2512 struct btrfs_chunk);
2513 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002514 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002515
2516 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2517 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2518 found_key.objectid,
2519 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002520 if (ret == -ENOSPC)
2521 failed++;
2522 else if (ret)
2523 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002524 }
2525
2526 if (found_key.offset == 0)
2527 break;
2528 key.offset = found_key.offset - 1;
2529 }
2530 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002531 if (failed && !retried) {
2532 failed = 0;
2533 retried = true;
2534 goto again;
2535 } else if (failed && retried) {
2536 WARN_ON(1);
2537 ret = -ENOSPC;
2538 }
Yan Zheng2b820322008-11-17 21:11:30 -05002539error:
2540 btrfs_free_path(path);
2541 return ret;
2542}
2543
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002544static int insert_balance_item(struct btrfs_root *root,
2545 struct btrfs_balance_control *bctl)
2546{
2547 struct btrfs_trans_handle *trans;
2548 struct btrfs_balance_item *item;
2549 struct btrfs_disk_balance_args disk_bargs;
2550 struct btrfs_path *path;
2551 struct extent_buffer *leaf;
2552 struct btrfs_key key;
2553 int ret, err;
2554
2555 path = btrfs_alloc_path();
2556 if (!path)
2557 return -ENOMEM;
2558
2559 trans = btrfs_start_transaction(root, 0);
2560 if (IS_ERR(trans)) {
2561 btrfs_free_path(path);
2562 return PTR_ERR(trans);
2563 }
2564
2565 key.objectid = BTRFS_BALANCE_OBJECTID;
2566 key.type = BTRFS_BALANCE_ITEM_KEY;
2567 key.offset = 0;
2568
2569 ret = btrfs_insert_empty_item(trans, root, path, &key,
2570 sizeof(*item));
2571 if (ret)
2572 goto out;
2573
2574 leaf = path->nodes[0];
2575 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2576
2577 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2578
2579 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2580 btrfs_set_balance_data(leaf, item, &disk_bargs);
2581 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2582 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2583 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2584 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2585
2586 btrfs_set_balance_flags(leaf, item, bctl->flags);
2587
2588 btrfs_mark_buffer_dirty(leaf);
2589out:
2590 btrfs_free_path(path);
2591 err = btrfs_commit_transaction(trans, root);
2592 if (err && !ret)
2593 ret = err;
2594 return ret;
2595}
2596
2597static int del_balance_item(struct btrfs_root *root)
2598{
2599 struct btrfs_trans_handle *trans;
2600 struct btrfs_path *path;
2601 struct btrfs_key key;
2602 int ret, err;
2603
2604 path = btrfs_alloc_path();
2605 if (!path)
2606 return -ENOMEM;
2607
2608 trans = btrfs_start_transaction(root, 0);
2609 if (IS_ERR(trans)) {
2610 btrfs_free_path(path);
2611 return PTR_ERR(trans);
2612 }
2613
2614 key.objectid = BTRFS_BALANCE_OBJECTID;
2615 key.type = BTRFS_BALANCE_ITEM_KEY;
2616 key.offset = 0;
2617
2618 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2619 if (ret < 0)
2620 goto out;
2621 if (ret > 0) {
2622 ret = -ENOENT;
2623 goto out;
2624 }
2625
2626 ret = btrfs_del_item(trans, root, path);
2627out:
2628 btrfs_free_path(path);
2629 err = btrfs_commit_transaction(trans, root);
2630 if (err && !ret)
2631 ret = err;
2632 return ret;
2633}
2634
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002635/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002636 * This is a heuristic used to reduce the number of chunks balanced on
2637 * resume after balance was interrupted.
2638 */
2639static void update_balance_args(struct btrfs_balance_control *bctl)
2640{
2641 /*
2642 * Turn on soft mode for chunk types that were being converted.
2643 */
2644 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2645 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2646 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2647 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2648 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2649 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2650
2651 /*
2652 * Turn on usage filter if is not already used. The idea is
2653 * that chunks that we have already balanced should be
2654 * reasonably full. Don't do it for chunks that are being
2655 * converted - that will keep us from relocating unconverted
2656 * (albeit full) chunks.
2657 */
2658 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2659 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2660 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2661 bctl->data.usage = 90;
2662 }
2663 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2664 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2665 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2666 bctl->sys.usage = 90;
2667 }
2668 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2669 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2670 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2671 bctl->meta.usage = 90;
2672 }
2673}
2674
2675/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002676 * Should be called with both balance and volume mutexes held to
2677 * serialize other volume operations (add_dev/rm_dev/resize) with
2678 * restriper. Same goes for unset_balance_control.
2679 */
2680static void set_balance_control(struct btrfs_balance_control *bctl)
2681{
2682 struct btrfs_fs_info *fs_info = bctl->fs_info;
2683
2684 BUG_ON(fs_info->balance_ctl);
2685
2686 spin_lock(&fs_info->balance_lock);
2687 fs_info->balance_ctl = bctl;
2688 spin_unlock(&fs_info->balance_lock);
2689}
2690
2691static void unset_balance_control(struct btrfs_fs_info *fs_info)
2692{
2693 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2694
2695 BUG_ON(!fs_info->balance_ctl);
2696
2697 spin_lock(&fs_info->balance_lock);
2698 fs_info->balance_ctl = NULL;
2699 spin_unlock(&fs_info->balance_lock);
2700
2701 kfree(bctl);
2702}
2703
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002704/*
2705 * Balance filters. Return 1 if chunk should be filtered out
2706 * (should not be balanced).
2707 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002708static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002709 struct btrfs_balance_args *bargs)
2710{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002711 chunk_type = chunk_to_extended(chunk_type) &
2712 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002713
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002714 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002715 return 0;
2716
2717 return 1;
2718}
2719
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002720static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2721 struct btrfs_balance_args *bargs)
2722{
2723 struct btrfs_block_group_cache *cache;
2724 u64 chunk_used, user_thresh;
2725 int ret = 1;
2726
2727 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2728 chunk_used = btrfs_block_group_used(&cache->item);
2729
Ilya Dryomova105bb82013-01-21 15:15:56 +02002730 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002731 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002732 else if (bargs->usage > 100)
2733 user_thresh = cache->key.offset;
2734 else
2735 user_thresh = div_factor_fine(cache->key.offset,
2736 bargs->usage);
2737
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002738 if (chunk_used < user_thresh)
2739 ret = 0;
2740
2741 btrfs_put_block_group(cache);
2742 return ret;
2743}
2744
Ilya Dryomov409d4042012-01-16 22:04:47 +02002745static int chunk_devid_filter(struct extent_buffer *leaf,
2746 struct btrfs_chunk *chunk,
2747 struct btrfs_balance_args *bargs)
2748{
2749 struct btrfs_stripe *stripe;
2750 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2751 int i;
2752
2753 for (i = 0; i < num_stripes; i++) {
2754 stripe = btrfs_stripe_nr(chunk, i);
2755 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2756 return 0;
2757 }
2758
2759 return 1;
2760}
2761
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002762/* [pstart, pend) */
2763static int chunk_drange_filter(struct extent_buffer *leaf,
2764 struct btrfs_chunk *chunk,
2765 u64 chunk_offset,
2766 struct btrfs_balance_args *bargs)
2767{
2768 struct btrfs_stripe *stripe;
2769 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2770 u64 stripe_offset;
2771 u64 stripe_length;
2772 int factor;
2773 int i;
2774
2775 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2776 return 0;
2777
2778 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002779 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2780 factor = num_stripes / 2;
2781 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2782 factor = num_stripes - 1;
2783 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2784 factor = num_stripes - 2;
2785 } else {
2786 factor = num_stripes;
2787 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002788
2789 for (i = 0; i < num_stripes; i++) {
2790 stripe = btrfs_stripe_nr(chunk, i);
2791 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2792 continue;
2793
2794 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2795 stripe_length = btrfs_chunk_length(leaf, chunk);
2796 do_div(stripe_length, factor);
2797
2798 if (stripe_offset < bargs->pend &&
2799 stripe_offset + stripe_length > bargs->pstart)
2800 return 0;
2801 }
2802
2803 return 1;
2804}
2805
Ilya Dryomovea671762012-01-16 22:04:48 +02002806/* [vstart, vend) */
2807static int chunk_vrange_filter(struct extent_buffer *leaf,
2808 struct btrfs_chunk *chunk,
2809 u64 chunk_offset,
2810 struct btrfs_balance_args *bargs)
2811{
2812 if (chunk_offset < bargs->vend &&
2813 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2814 /* at least part of the chunk is inside this vrange */
2815 return 0;
2816
2817 return 1;
2818}
2819
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002820static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002821 struct btrfs_balance_args *bargs)
2822{
2823 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2824 return 0;
2825
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002826 chunk_type = chunk_to_extended(chunk_type) &
2827 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002828
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002829 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002830 return 1;
2831
2832 return 0;
2833}
2834
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002835static int should_balance_chunk(struct btrfs_root *root,
2836 struct extent_buffer *leaf,
2837 struct btrfs_chunk *chunk, u64 chunk_offset)
2838{
2839 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2840 struct btrfs_balance_args *bargs = NULL;
2841 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2842
2843 /* type filter */
2844 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2845 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2846 return 0;
2847 }
2848
2849 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2850 bargs = &bctl->data;
2851 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2852 bargs = &bctl->sys;
2853 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2854 bargs = &bctl->meta;
2855
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002856 /* profiles filter */
2857 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2858 chunk_profiles_filter(chunk_type, bargs)) {
2859 return 0;
2860 }
2861
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002862 /* usage filter */
2863 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2864 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2865 return 0;
2866 }
2867
Ilya Dryomov409d4042012-01-16 22:04:47 +02002868 /* devid filter */
2869 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2870 chunk_devid_filter(leaf, chunk, bargs)) {
2871 return 0;
2872 }
2873
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002874 /* drange filter, makes sense only with devid filter */
2875 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2876 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2877 return 0;
2878 }
2879
Ilya Dryomovea671762012-01-16 22:04:48 +02002880 /* vrange filter */
2881 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2882 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2883 return 0;
2884 }
2885
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002886 /* soft profile changing mode */
2887 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2888 chunk_soft_convert_filter(chunk_type, bargs)) {
2889 return 0;
2890 }
2891
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002892 return 1;
2893}
2894
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002895static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002896{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002897 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002898 struct btrfs_root *chunk_root = fs_info->chunk_root;
2899 struct btrfs_root *dev_root = fs_info->dev_root;
2900 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002901 struct btrfs_device *device;
2902 u64 old_size;
2903 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002904 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002905 struct btrfs_path *path;
2906 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002907 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002908 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002909 struct extent_buffer *leaf;
2910 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002911 int ret;
2912 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002913 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002914
Chris Masonec44a352008-04-28 15:29:52 -04002915 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002916 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002917 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002918 old_size = device->total_bytes;
2919 size_to_free = div_factor(old_size, 1);
2920 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002921 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01002922 device->total_bytes - device->bytes_used > size_to_free ||
2923 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04002924 continue;
2925
2926 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002927 if (ret == -ENOSPC)
2928 break;
Chris Masonec44a352008-04-28 15:29:52 -04002929 BUG_ON(ret);
2930
Yan, Zhenga22285a2010-05-16 10:48:46 -04002931 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002932 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002933
2934 ret = btrfs_grow_device(trans, device, old_size);
2935 BUG_ON(ret);
2936
2937 btrfs_end_transaction(trans, dev_root);
2938 }
2939
2940 /* step two, relocate all the chunks */
2941 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002942 if (!path) {
2943 ret = -ENOMEM;
2944 goto error;
2945 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002946
2947 /* zero out stat counters */
2948 spin_lock(&fs_info->balance_lock);
2949 memset(&bctl->stat, 0, sizeof(bctl->stat));
2950 spin_unlock(&fs_info->balance_lock);
2951again:
Chris Masonec44a352008-04-28 15:29:52 -04002952 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2953 key.offset = (u64)-1;
2954 key.type = BTRFS_CHUNK_ITEM_KEY;
2955
Chris Masond3977122009-01-05 21:25:51 -05002956 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002957 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002958 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002959 ret = -ECANCELED;
2960 goto error;
2961 }
2962
Chris Masonec44a352008-04-28 15:29:52 -04002963 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2964 if (ret < 0)
2965 goto error;
2966
2967 /*
2968 * this shouldn't happen, it means the last relocate
2969 * failed
2970 */
2971 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002972 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002973
2974 ret = btrfs_previous_item(chunk_root, path, 0,
2975 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002976 if (ret) {
2977 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002978 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002979 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002980
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002981 leaf = path->nodes[0];
2982 slot = path->slots[0];
2983 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2984
Chris Masonec44a352008-04-28 15:29:52 -04002985 if (found_key.objectid != key.objectid)
2986 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002987
Chris Masonec44a352008-04-28 15:29:52 -04002988 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002989 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002990 break;
2991
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002992 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2993
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002994 if (!counting) {
2995 spin_lock(&fs_info->balance_lock);
2996 bctl->stat.considered++;
2997 spin_unlock(&fs_info->balance_lock);
2998 }
2999
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003000 ret = should_balance_chunk(chunk_root, leaf, chunk,
3001 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003002 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003003 if (!ret)
3004 goto loop;
3005
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003006 if (counting) {
3007 spin_lock(&fs_info->balance_lock);
3008 bctl->stat.expected++;
3009 spin_unlock(&fs_info->balance_lock);
3010 goto loop;
3011 }
3012
Chris Masonec44a352008-04-28 15:29:52 -04003013 ret = btrfs_relocate_chunk(chunk_root,
3014 chunk_root->root_key.objectid,
3015 found_key.objectid,
3016 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003017 if (ret && ret != -ENOSPC)
3018 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003019 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003020 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003021 } else {
3022 spin_lock(&fs_info->balance_lock);
3023 bctl->stat.completed++;
3024 spin_unlock(&fs_info->balance_lock);
3025 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003026loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04003027 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003028 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003029
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003030 if (counting) {
3031 btrfs_release_path(path);
3032 counting = false;
3033 goto again;
3034 }
Chris Masonec44a352008-04-28 15:29:52 -04003035error:
3036 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003037 if (enospc_errors) {
3038 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
3039 enospc_errors);
3040 if (!ret)
3041 ret = -ENOSPC;
3042 }
3043
Chris Masonec44a352008-04-28 15:29:52 -04003044 return ret;
3045}
3046
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003047/**
3048 * alloc_profile_is_valid - see if a given profile is valid and reduced
3049 * @flags: profile to validate
3050 * @extended: if true @flags is treated as an extended profile
3051 */
3052static int alloc_profile_is_valid(u64 flags, int extended)
3053{
3054 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3055 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3056
3057 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3058
3059 /* 1) check that all other bits are zeroed */
3060 if (flags & ~mask)
3061 return 0;
3062
3063 /* 2) see if profile is reduced */
3064 if (flags == 0)
3065 return !extended; /* "0" is valid for usual profiles */
3066
3067 /* true if exactly one bit set */
3068 return (flags & (flags - 1)) == 0;
3069}
3070
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003071static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3072{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003073 /* cancel requested || normal exit path */
3074 return atomic_read(&fs_info->balance_cancel_req) ||
3075 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3076 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003077}
3078
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003079static void __cancel_balance(struct btrfs_fs_info *fs_info)
3080{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003081 int ret;
3082
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003083 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003084 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003085 if (ret)
3086 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003087
3088 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003089}
3090
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003091void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003092 struct btrfs_ioctl_balance_args *bargs);
3093
3094/*
3095 * Should be called with both balance and volume mutexes held
3096 */
3097int btrfs_balance(struct btrfs_balance_control *bctl,
3098 struct btrfs_ioctl_balance_args *bargs)
3099{
3100 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003101 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003102 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003103 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003104 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003105 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003106
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003107 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003108 atomic_read(&fs_info->balance_pause_req) ||
3109 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003110 ret = -EINVAL;
3111 goto out;
3112 }
3113
Ilya Dryomove4837f82012-03-27 17:09:17 +03003114 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3115 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3116 mixed = 1;
3117
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003118 /*
3119 * In case of mixed groups both data and meta should be picked,
3120 * and identical options should be given for both of them.
3121 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003122 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3123 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003124 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3125 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3126 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3127 printk(KERN_ERR "btrfs: with mixed groups data and "
3128 "metadata balance options must be the same\n");
3129 ret = -EINVAL;
3130 goto out;
3131 }
3132 }
3133
Stefan Behrens8dabb742012-11-06 13:15:27 +01003134 num_devices = fs_info->fs_devices->num_devices;
3135 btrfs_dev_replace_lock(&fs_info->dev_replace);
3136 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3137 BUG_ON(num_devices < 1);
3138 num_devices--;
3139 }
3140 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003141 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003142 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003143 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003144 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003145 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003146 if (num_devices > 2)
3147 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3148 if (num_devices > 3)
3149 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3150 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003151 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3152 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3153 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003154 printk(KERN_ERR "btrfs: unable to start balance with target "
3155 "data profile %llu\n",
3156 (unsigned long long)bctl->data.target);
3157 ret = -EINVAL;
3158 goto out;
3159 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003160 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3161 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3162 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003163 printk(KERN_ERR "btrfs: unable to start balance with target "
3164 "metadata profile %llu\n",
3165 (unsigned long long)bctl->meta.target);
3166 ret = -EINVAL;
3167 goto out;
3168 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003169 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3170 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3171 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003172 printk(KERN_ERR "btrfs: unable to start balance with target "
3173 "system profile %llu\n",
3174 (unsigned long long)bctl->sys.target);
3175 ret = -EINVAL;
3176 goto out;
3177 }
3178
Ilya Dryomove4837f82012-03-27 17:09:17 +03003179 /* allow dup'ed data chunks only in mixed mode */
3180 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003181 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003182 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3183 ret = -EINVAL;
3184 goto out;
3185 }
3186
3187 /* allow to reduce meta or sys integrity only if force set */
3188 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003189 BTRFS_BLOCK_GROUP_RAID10 |
3190 BTRFS_BLOCK_GROUP_RAID5 |
3191 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003192 do {
3193 seq = read_seqbegin(&fs_info->profiles_lock);
3194
3195 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3196 (fs_info->avail_system_alloc_bits & allowed) &&
3197 !(bctl->sys.target & allowed)) ||
3198 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3199 (fs_info->avail_metadata_alloc_bits & allowed) &&
3200 !(bctl->meta.target & allowed))) {
3201 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3202 printk(KERN_INFO "btrfs: force reducing metadata "
3203 "integrity\n");
3204 } else {
3205 printk(KERN_ERR "btrfs: balance will reduce metadata "
3206 "integrity, use force if you want this\n");
3207 ret = -EINVAL;
3208 goto out;
3209 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003210 }
Miao Xiede98ced2013-01-29 10:13:12 +00003211 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003212
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003213 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3214 int num_tolerated_disk_barrier_failures;
3215 u64 target = bctl->sys.target;
3216
3217 num_tolerated_disk_barrier_failures =
3218 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3219 if (num_tolerated_disk_barrier_failures > 0 &&
3220 (target &
3221 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3222 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3223 num_tolerated_disk_barrier_failures = 0;
3224 else if (num_tolerated_disk_barrier_failures > 1 &&
3225 (target &
3226 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3227 num_tolerated_disk_barrier_failures = 1;
3228
3229 fs_info->num_tolerated_disk_barrier_failures =
3230 num_tolerated_disk_barrier_failures;
3231 }
3232
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003233 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003234 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003235 goto out;
3236
Ilya Dryomov59641012012-01-16 22:04:48 +02003237 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3238 BUG_ON(ret == -EEXIST);
3239 set_balance_control(bctl);
3240 } else {
3241 BUG_ON(ret != -EEXIST);
3242 spin_lock(&fs_info->balance_lock);
3243 update_balance_args(bctl);
3244 spin_unlock(&fs_info->balance_lock);
3245 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003246
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003247 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003248 mutex_unlock(&fs_info->balance_mutex);
3249
3250 ret = __btrfs_balance(fs_info);
3251
3252 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003253 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003254
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003255 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3256 fs_info->num_tolerated_disk_barrier_failures =
3257 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3258 }
3259
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003260 if (bargs) {
3261 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003262 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003263 }
3264
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003265 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3266 balance_need_close(fs_info)) {
3267 __cancel_balance(fs_info);
3268 }
3269
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003270 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003271
3272 return ret;
3273out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003274 if (bctl->flags & BTRFS_BALANCE_RESUME)
3275 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003276 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003277 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003278 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3279 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003280 return ret;
3281}
3282
3283static int balance_kthread(void *data)
3284{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003285 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003286 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003287
3288 mutex_lock(&fs_info->volume_mutex);
3289 mutex_lock(&fs_info->balance_mutex);
3290
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003291 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003292 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003293 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003294 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003295
3296 mutex_unlock(&fs_info->balance_mutex);
3297 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003298
Ilya Dryomov59641012012-01-16 22:04:48 +02003299 return ret;
3300}
3301
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003302int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3303{
3304 struct task_struct *tsk;
3305
3306 spin_lock(&fs_info->balance_lock);
3307 if (!fs_info->balance_ctl) {
3308 spin_unlock(&fs_info->balance_lock);
3309 return 0;
3310 }
3311 spin_unlock(&fs_info->balance_lock);
3312
3313 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3314 printk(KERN_INFO "btrfs: force skipping balance\n");
3315 return 0;
3316 }
3317
3318 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3319 if (IS_ERR(tsk))
3320 return PTR_ERR(tsk);
3321
3322 return 0;
3323}
3324
Ilya Dryomov68310a52012-06-22 12:24:12 -06003325int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003326{
Ilya Dryomov59641012012-01-16 22:04:48 +02003327 struct btrfs_balance_control *bctl;
3328 struct btrfs_balance_item *item;
3329 struct btrfs_disk_balance_args disk_bargs;
3330 struct btrfs_path *path;
3331 struct extent_buffer *leaf;
3332 struct btrfs_key key;
3333 int ret;
3334
3335 path = btrfs_alloc_path();
3336 if (!path)
3337 return -ENOMEM;
3338
Ilya Dryomov68310a52012-06-22 12:24:12 -06003339 key.objectid = BTRFS_BALANCE_OBJECTID;
3340 key.type = BTRFS_BALANCE_ITEM_KEY;
3341 key.offset = 0;
3342
3343 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3344 if (ret < 0)
3345 goto out;
3346 if (ret > 0) { /* ret = -ENOENT; */
3347 ret = 0;
3348 goto out;
3349 }
3350
Ilya Dryomov59641012012-01-16 22:04:48 +02003351 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3352 if (!bctl) {
3353 ret = -ENOMEM;
3354 goto out;
3355 }
3356
Ilya Dryomov59641012012-01-16 22:04:48 +02003357 leaf = path->nodes[0];
3358 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3359
Ilya Dryomov68310a52012-06-22 12:24:12 -06003360 bctl->fs_info = fs_info;
3361 bctl->flags = btrfs_balance_flags(leaf, item);
3362 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003363
3364 btrfs_balance_data(leaf, item, &disk_bargs);
3365 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3366 btrfs_balance_meta(leaf, item, &disk_bargs);
3367 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3368 btrfs_balance_sys(leaf, item, &disk_bargs);
3369 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3370
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003371 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3372
Ilya Dryomov68310a52012-06-22 12:24:12 -06003373 mutex_lock(&fs_info->volume_mutex);
3374 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003375
Ilya Dryomov68310a52012-06-22 12:24:12 -06003376 set_balance_control(bctl);
3377
3378 mutex_unlock(&fs_info->balance_mutex);
3379 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003380out:
3381 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003382 return ret;
3383}
3384
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003385int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3386{
3387 int ret = 0;
3388
3389 mutex_lock(&fs_info->balance_mutex);
3390 if (!fs_info->balance_ctl) {
3391 mutex_unlock(&fs_info->balance_mutex);
3392 return -ENOTCONN;
3393 }
3394
3395 if (atomic_read(&fs_info->balance_running)) {
3396 atomic_inc(&fs_info->balance_pause_req);
3397 mutex_unlock(&fs_info->balance_mutex);
3398
3399 wait_event(fs_info->balance_wait_q,
3400 atomic_read(&fs_info->balance_running) == 0);
3401
3402 mutex_lock(&fs_info->balance_mutex);
3403 /* we are good with balance_ctl ripped off from under us */
3404 BUG_ON(atomic_read(&fs_info->balance_running));
3405 atomic_dec(&fs_info->balance_pause_req);
3406 } else {
3407 ret = -ENOTCONN;
3408 }
3409
3410 mutex_unlock(&fs_info->balance_mutex);
3411 return ret;
3412}
3413
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003414int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3415{
3416 mutex_lock(&fs_info->balance_mutex);
3417 if (!fs_info->balance_ctl) {
3418 mutex_unlock(&fs_info->balance_mutex);
3419 return -ENOTCONN;
3420 }
3421
3422 atomic_inc(&fs_info->balance_cancel_req);
3423 /*
3424 * if we are running just wait and return, balance item is
3425 * deleted in btrfs_balance in this case
3426 */
3427 if (atomic_read(&fs_info->balance_running)) {
3428 mutex_unlock(&fs_info->balance_mutex);
3429 wait_event(fs_info->balance_wait_q,
3430 atomic_read(&fs_info->balance_running) == 0);
3431 mutex_lock(&fs_info->balance_mutex);
3432 } else {
3433 /* __cancel_balance needs volume_mutex */
3434 mutex_unlock(&fs_info->balance_mutex);
3435 mutex_lock(&fs_info->volume_mutex);
3436 mutex_lock(&fs_info->balance_mutex);
3437
3438 if (fs_info->balance_ctl)
3439 __cancel_balance(fs_info);
3440
3441 mutex_unlock(&fs_info->volume_mutex);
3442 }
3443
3444 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3445 atomic_dec(&fs_info->balance_cancel_req);
3446 mutex_unlock(&fs_info->balance_mutex);
3447 return 0;
3448}
3449
Chris Mason8f18cf12008-04-25 16:53:30 -04003450/*
3451 * shrinking a device means finding all of the device extents past
3452 * the new size, and then following the back refs to the chunks.
3453 * The chunk relocation code actually frees the device extent
3454 */
3455int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3456{
3457 struct btrfs_trans_handle *trans;
3458 struct btrfs_root *root = device->dev_root;
3459 struct btrfs_dev_extent *dev_extent = NULL;
3460 struct btrfs_path *path;
3461 u64 length;
3462 u64 chunk_tree;
3463 u64 chunk_objectid;
3464 u64 chunk_offset;
3465 int ret;
3466 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003467 int failed = 0;
3468 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003469 struct extent_buffer *l;
3470 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003471 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003472 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003473 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003474 u64 diff = device->total_bytes - new_size;
3475
Stefan Behrens63a212a2012-11-05 18:29:28 +01003476 if (device->is_tgtdev_for_dev_replace)
3477 return -EINVAL;
3478
Chris Mason8f18cf12008-04-25 16:53:30 -04003479 path = btrfs_alloc_path();
3480 if (!path)
3481 return -ENOMEM;
3482
Chris Mason8f18cf12008-04-25 16:53:30 -04003483 path->reada = 2;
3484
Chris Mason7d9eb122008-07-08 14:19:17 -04003485 lock_chunks(root);
3486
Chris Mason8f18cf12008-04-25 16:53:30 -04003487 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003488 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003489 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003490 spin_lock(&root->fs_info->free_chunk_lock);
3491 root->fs_info->free_chunk_space -= diff;
3492 spin_unlock(&root->fs_info->free_chunk_lock);
3493 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003494 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003495
Josef Bacikba1bf482009-09-11 16:11:19 -04003496again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003497 key.objectid = device->devid;
3498 key.offset = (u64)-1;
3499 key.type = BTRFS_DEV_EXTENT_KEY;
3500
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003501 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003502 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3503 if (ret < 0)
3504 goto done;
3505
3506 ret = btrfs_previous_item(root, path, 0, key.type);
3507 if (ret < 0)
3508 goto done;
3509 if (ret) {
3510 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003511 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003512 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003513 }
3514
3515 l = path->nodes[0];
3516 slot = path->slots[0];
3517 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3518
Josef Bacikba1bf482009-09-11 16:11:19 -04003519 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003520 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003521 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003522 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003523
3524 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3525 length = btrfs_dev_extent_length(l, dev_extent);
3526
Josef Bacikba1bf482009-09-11 16:11:19 -04003527 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003528 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003529 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003530 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003531
3532 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3533 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3534 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003535 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003536
3537 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3538 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003539 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003540 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003541 if (ret == -ENOSPC)
3542 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003543 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003544
3545 if (failed && !retried) {
3546 failed = 0;
3547 retried = true;
3548 goto again;
3549 } else if (failed && retried) {
3550 ret = -ENOSPC;
3551 lock_chunks(root);
3552
3553 device->total_bytes = old_size;
3554 if (device->writeable)
3555 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003556 spin_lock(&root->fs_info->free_chunk_lock);
3557 root->fs_info->free_chunk_space += diff;
3558 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003559 unlock_chunks(root);
3560 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003561 }
3562
Chris Balld6397ba2009-04-27 07:29:03 -04003563 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003564 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003565 if (IS_ERR(trans)) {
3566 ret = PTR_ERR(trans);
3567 goto done;
3568 }
3569
Chris Balld6397ba2009-04-27 07:29:03 -04003570 lock_chunks(root);
3571
3572 device->disk_total_bytes = new_size;
3573 /* Now btrfs_update_device() will change the on-disk size. */
3574 ret = btrfs_update_device(trans, device);
3575 if (ret) {
3576 unlock_chunks(root);
3577 btrfs_end_transaction(trans, root);
3578 goto done;
3579 }
3580 WARN_ON(diff > old_total);
3581 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3582 unlock_chunks(root);
3583 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003584done:
3585 btrfs_free_path(path);
3586 return ret;
3587}
3588
Li Zefan125ccb02011-12-08 15:07:24 +08003589static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003590 struct btrfs_key *key,
3591 struct btrfs_chunk *chunk, int item_size)
3592{
David Sterba6c417612011-04-13 15:41:04 +02003593 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003594 struct btrfs_disk_key disk_key;
3595 u32 array_size;
3596 u8 *ptr;
3597
3598 array_size = btrfs_super_sys_array_size(super_copy);
3599 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3600 return -EFBIG;
3601
3602 ptr = super_copy->sys_chunk_array + array_size;
3603 btrfs_cpu_key_to_disk(&disk_key, key);
3604 memcpy(ptr, &disk_key, sizeof(disk_key));
3605 ptr += sizeof(disk_key);
3606 memcpy(ptr, chunk, item_size);
3607 item_size += sizeof(disk_key);
3608 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3609 return 0;
3610}
3611
Miao Xieb2117a32011-01-05 10:07:28 +00003612/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003613 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003614 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003615static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003616{
Arne Jansen73c5de02011-04-12 12:07:57 +02003617 const struct btrfs_device_info *di_a = a;
3618 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003619
Arne Jansen73c5de02011-04-12 12:07:57 +02003620 if (di_a->max_avail > di_b->max_avail)
3621 return -1;
3622 if (di_a->max_avail < di_b->max_avail)
3623 return 1;
3624 if (di_a->total_avail > di_b->total_avail)
3625 return -1;
3626 if (di_a->total_avail < di_b->total_avail)
3627 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003628 return 0;
3629}
3630
Eric Sandeen48a3b632013-04-25 20:41:01 +00003631static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00003632 [BTRFS_RAID_RAID10] = {
3633 .sub_stripes = 2,
3634 .dev_stripes = 1,
3635 .devs_max = 0, /* 0 == as many as possible */
3636 .devs_min = 4,
3637 .devs_increment = 2,
3638 .ncopies = 2,
3639 },
3640 [BTRFS_RAID_RAID1] = {
3641 .sub_stripes = 1,
3642 .dev_stripes = 1,
3643 .devs_max = 2,
3644 .devs_min = 2,
3645 .devs_increment = 2,
3646 .ncopies = 2,
3647 },
3648 [BTRFS_RAID_DUP] = {
3649 .sub_stripes = 1,
3650 .dev_stripes = 2,
3651 .devs_max = 1,
3652 .devs_min = 1,
3653 .devs_increment = 1,
3654 .ncopies = 2,
3655 },
3656 [BTRFS_RAID_RAID0] = {
3657 .sub_stripes = 1,
3658 .dev_stripes = 1,
3659 .devs_max = 0,
3660 .devs_min = 2,
3661 .devs_increment = 1,
3662 .ncopies = 1,
3663 },
3664 [BTRFS_RAID_SINGLE] = {
3665 .sub_stripes = 1,
3666 .dev_stripes = 1,
3667 .devs_max = 1,
3668 .devs_min = 1,
3669 .devs_increment = 1,
3670 .ncopies = 1,
3671 },
Chris Masone942f882013-02-20 14:06:05 -05003672 [BTRFS_RAID_RAID5] = {
3673 .sub_stripes = 1,
3674 .dev_stripes = 1,
3675 .devs_max = 0,
3676 .devs_min = 2,
3677 .devs_increment = 1,
3678 .ncopies = 2,
3679 },
3680 [BTRFS_RAID_RAID6] = {
3681 .sub_stripes = 1,
3682 .dev_stripes = 1,
3683 .devs_max = 0,
3684 .devs_min = 3,
3685 .devs_increment = 1,
3686 .ncopies = 3,
3687 },
Liu Bo31e50222012-11-21 14:18:10 +00003688};
3689
David Woodhouse53b381b2013-01-29 18:40:14 -05003690static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
3691{
3692 /* TODO allow them to set a preferred stripe size */
3693 return 64 * 1024;
3694}
3695
3696static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
3697{
David Woodhouse53b381b2013-01-29 18:40:14 -05003698 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
3699 return;
3700
Miao Xieceda0862013-04-11 10:30:16 +00003701 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05003702}
3703
Miao Xieb2117a32011-01-05 10:07:28 +00003704static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3705 struct btrfs_root *extent_root,
3706 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003707 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003708 u64 start, u64 type)
3709{
3710 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003711 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3712 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003713 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003714 struct extent_map_tree *em_tree;
3715 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003716 struct btrfs_device_info *devices_info = NULL;
3717 u64 total_avail;
3718 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05003719 int data_stripes; /* number of stripes that count for
3720 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02003721 int sub_stripes; /* sub_stripes info for map */
3722 int dev_stripes; /* stripes per dev */
3723 int devs_max; /* max devs to use */
3724 int devs_min; /* min devs needed */
3725 int devs_increment; /* ndevs has to be a multiple of this */
3726 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003727 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003728 u64 max_stripe_size;
3729 u64 max_chunk_size;
3730 u64 stripe_size;
3731 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05003732 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02003733 int ndevs;
3734 int i;
3735 int j;
Liu Bo31e50222012-11-21 14:18:10 +00003736 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00003737
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003738 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003739
Miao Xieb2117a32011-01-05 10:07:28 +00003740 if (list_empty(&fs_devices->alloc_list))
3741 return -ENOSPC;
3742
Liu Bo31e50222012-11-21 14:18:10 +00003743 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02003744
Liu Bo31e50222012-11-21 14:18:10 +00003745 sub_stripes = btrfs_raid_array[index].sub_stripes;
3746 dev_stripes = btrfs_raid_array[index].dev_stripes;
3747 devs_max = btrfs_raid_array[index].devs_max;
3748 devs_min = btrfs_raid_array[index].devs_min;
3749 devs_increment = btrfs_raid_array[index].devs_increment;
3750 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02003751
3752 if (type & BTRFS_BLOCK_GROUP_DATA) {
3753 max_stripe_size = 1024 * 1024 * 1024;
3754 max_chunk_size = 10 * max_stripe_size;
3755 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003756 /* for larger filesystems, use larger metadata chunks */
3757 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3758 max_stripe_size = 1024 * 1024 * 1024;
3759 else
3760 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003761 max_chunk_size = max_stripe_size;
3762 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003763 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003764 max_chunk_size = 2 * max_stripe_size;
3765 } else {
3766 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3767 type);
3768 BUG_ON(1);
3769 }
3770
3771 /* we don't want a chunk larger than 10% of writeable space */
3772 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3773 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003774
3775 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3776 GFP_NOFS);
3777 if (!devices_info)
3778 return -ENOMEM;
3779
Arne Jansen73c5de02011-04-12 12:07:57 +02003780 cur = fs_devices->alloc_list.next;
3781
3782 /*
3783 * in the first pass through the devices list, we gather information
3784 * about the available holes on each device.
3785 */
3786 ndevs = 0;
3787 while (cur != &fs_devices->alloc_list) {
3788 struct btrfs_device *device;
3789 u64 max_avail;
3790 u64 dev_offset;
3791
3792 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3793
3794 cur = cur->next;
3795
3796 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003797 WARN(1, KERN_ERR
Arne Jansen73c5de02011-04-12 12:07:57 +02003798 "btrfs: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02003799 continue;
3800 }
3801
Stefan Behrens63a212a2012-11-05 18:29:28 +01003802 if (!device->in_fs_metadata ||
3803 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02003804 continue;
3805
3806 if (device->total_bytes > device->bytes_used)
3807 total_avail = device->total_bytes - device->bytes_used;
3808 else
3809 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003810
3811 /* If there is no space on this device, skip it. */
3812 if (total_avail == 0)
3813 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003814
Li Zefan125ccb02011-12-08 15:07:24 +08003815 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003816 max_stripe_size * dev_stripes,
3817 &dev_offset, &max_avail);
3818 if (ret && ret != -ENOSPC)
3819 goto error;
3820
3821 if (ret == 0)
3822 max_avail = max_stripe_size * dev_stripes;
3823
3824 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3825 continue;
3826
Eric Sandeen063d0062013-01-31 00:55:01 +00003827 if (ndevs == fs_devices->rw_devices) {
3828 WARN(1, "%s: found more than %llu devices\n",
3829 __func__, fs_devices->rw_devices);
3830 break;
3831 }
Arne Jansen73c5de02011-04-12 12:07:57 +02003832 devices_info[ndevs].dev_offset = dev_offset;
3833 devices_info[ndevs].max_avail = max_avail;
3834 devices_info[ndevs].total_avail = total_avail;
3835 devices_info[ndevs].dev = device;
3836 ++ndevs;
3837 }
3838
3839 /*
3840 * now sort the devices by hole size / available space
3841 */
3842 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3843 btrfs_cmp_device_info, NULL);
3844
3845 /* round down to number of usable stripes */
3846 ndevs -= ndevs % devs_increment;
3847
3848 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3849 ret = -ENOSPC;
3850 goto error;
3851 }
3852
3853 if (devs_max && ndevs > devs_max)
3854 ndevs = devs_max;
3855 /*
3856 * the primary goal is to maximize the number of stripes, so use as many
3857 * devices as possible, even if the stripes are not maximum sized.
3858 */
3859 stripe_size = devices_info[ndevs-1].max_avail;
3860 num_stripes = ndevs * dev_stripes;
3861
David Woodhouse53b381b2013-01-29 18:40:14 -05003862 /*
3863 * this will have to be fixed for RAID1 and RAID10 over
3864 * more drives
3865 */
3866 data_stripes = num_stripes / ncopies;
3867
David Woodhouse53b381b2013-01-29 18:40:14 -05003868 if (type & BTRFS_BLOCK_GROUP_RAID5) {
3869 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
3870 btrfs_super_stripesize(info->super_copy));
3871 data_stripes = num_stripes - 1;
3872 }
3873 if (type & BTRFS_BLOCK_GROUP_RAID6) {
3874 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
3875 btrfs_super_stripesize(info->super_copy));
3876 data_stripes = num_stripes - 2;
3877 }
Chris Mason86db2572013-02-20 16:23:40 -05003878
3879 /*
3880 * Use the number of data stripes to figure out how big this chunk
3881 * is really going to be in terms of logical address space,
3882 * and compare that answer with the max chunk size
3883 */
3884 if (stripe_size * data_stripes > max_chunk_size) {
3885 u64 mask = (1ULL << 24) - 1;
3886 stripe_size = max_chunk_size;
3887 do_div(stripe_size, data_stripes);
3888
3889 /* bump the answer up to a 16MB boundary */
3890 stripe_size = (stripe_size + mask) & ~mask;
3891
3892 /* but don't go higher than the limits we found
3893 * while searching for free extents
3894 */
3895 if (stripe_size > devices_info[ndevs-1].max_avail)
3896 stripe_size = devices_info[ndevs-1].max_avail;
3897 }
3898
Arne Jansen73c5de02011-04-12 12:07:57 +02003899 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003900
3901 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05003902 do_div(stripe_size, raid_stripe_len);
3903 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02003904
Miao Xieb2117a32011-01-05 10:07:28 +00003905 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3906 if (!map) {
3907 ret = -ENOMEM;
3908 goto error;
3909 }
3910 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003911
Arne Jansen73c5de02011-04-12 12:07:57 +02003912 for (i = 0; i < ndevs; ++i) {
3913 for (j = 0; j < dev_stripes; ++j) {
3914 int s = i * dev_stripes + j;
3915 map->stripes[s].dev = devices_info[i].dev;
3916 map->stripes[s].physical = devices_info[i].dev_offset +
3917 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003918 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003919 }
Chris Mason593060d2008-03-25 16:50:33 -04003920 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05003921 map->stripe_len = raid_stripe_len;
3922 map->io_align = raid_stripe_len;
3923 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04003924 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003925 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003926
Yan Zheng2b820322008-11-17 21:11:30 -05003927 *map_ret = map;
David Woodhouse53b381b2013-01-29 18:40:14 -05003928 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003929
Arne Jansen73c5de02011-04-12 12:07:57 +02003930 *stripe_size_out = stripe_size;
3931 *num_bytes_out = num_bytes;
3932
3933 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003934
David Sterba172ddd62011-04-21 00:48:27 +02003935 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003936 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003937 ret = -ENOMEM;
3938 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003939 }
Chris Mason0b86a832008-03-24 15:01:56 -04003940 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003941 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003942 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003943 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003944 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003945
Chris Mason0b86a832008-03-24 15:01:56 -04003946 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003947 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003948 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04003949 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003950 if (ret) {
3951 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003952 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003953 }
Yan Zheng2b820322008-11-17 21:11:30 -05003954
Arne Jansen73c5de02011-04-12 12:07:57 +02003955 for (i = 0; i < map->num_stripes; ++i) {
3956 struct btrfs_device *device;
3957 u64 dev_offset;
3958
3959 device = map->stripes[i].dev;
3960 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003961
3962 ret = btrfs_alloc_dev_extent(trans, device,
3963 info->chunk_root->root_key.objectid,
3964 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003965 start, dev_offset, stripe_size);
Josef Bacik04487482013-01-29 15:03:37 -05003966 if (ret)
3967 goto error_dev_extent;
3968 }
3969
3970 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3971 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3972 start, num_bytes);
3973 if (ret) {
3974 i = map->num_stripes - 1;
3975 goto error_dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05003976 }
3977
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003978 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05003979 check_raid56_incompat_flag(extent_root->fs_info, type);
3980
Miao Xieb2117a32011-01-05 10:07:28 +00003981 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003982 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003983
Josef Bacik04487482013-01-29 15:03:37 -05003984error_dev_extent:
3985 for (; i >= 0; i--) {
3986 struct btrfs_device *device;
3987 int err;
3988
3989 device = map->stripes[i].dev;
3990 err = btrfs_free_dev_extent(trans, device, start);
3991 if (err) {
3992 btrfs_abort_transaction(trans, extent_root, err);
3993 break;
3994 }
3995 }
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003996 write_lock(&em_tree->lock);
3997 remove_extent_mapping(em_tree, em);
3998 write_unlock(&em_tree->lock);
3999
4000 /* One for our allocation */
4001 free_extent_map(em);
4002 /* One for the tree reference */
4003 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004004error:
4005 kfree(map);
4006 kfree(devices_info);
4007 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004008}
4009
4010static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
4011 struct btrfs_root *extent_root,
4012 struct map_lookup *map, u64 chunk_offset,
4013 u64 chunk_size, u64 stripe_size)
4014{
4015 u64 dev_offset;
4016 struct btrfs_key key;
4017 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4018 struct btrfs_device *device;
4019 struct btrfs_chunk *chunk;
4020 struct btrfs_stripe *stripe;
4021 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
4022 int index = 0;
4023 int ret;
4024
4025 chunk = kzalloc(item_size, GFP_NOFS);
4026 if (!chunk)
4027 return -ENOMEM;
4028
4029 index = 0;
4030 while (index < map->num_stripes) {
4031 device = map->stripes[index].dev;
4032 device->bytes_used += stripe_size;
4033 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004034 if (ret)
4035 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05004036 index++;
4037 }
4038
Josef Bacik2bf64752011-09-26 17:12:22 -04004039 spin_lock(&extent_root->fs_info->free_chunk_lock);
4040 extent_root->fs_info->free_chunk_space -= (stripe_size *
4041 map->num_stripes);
4042 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4043
Yan Zheng2b820322008-11-17 21:11:30 -05004044 index = 0;
4045 stripe = &chunk->stripe;
4046 while (index < map->num_stripes) {
4047 device = map->stripes[index].dev;
4048 dev_offset = map->stripes[index].physical;
4049
4050 btrfs_set_stack_stripe_devid(stripe, device->devid);
4051 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4052 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4053 stripe++;
4054 index++;
4055 }
4056
4057 btrfs_set_stack_chunk_length(chunk, chunk_size);
4058 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4059 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4060 btrfs_set_stack_chunk_type(chunk, map->type);
4061 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4062 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4063 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4064 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4065 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4066
4067 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4068 key.type = BTRFS_CHUNK_ITEM_KEY;
4069 key.offset = chunk_offset;
4070
4071 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004072
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004073 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4074 /*
4075 * TODO: Cleanup of inserted chunk root in case of
4076 * failure.
4077 */
Li Zefan125ccb02011-12-08 15:07:24 +08004078 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004079 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004080 }
liubo1abe9b82011-03-24 11:18:59 +00004081
Mark Fasheh3acd3952011-09-08 17:40:01 -07004082out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05004083 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004084 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004085}
4086
4087/*
4088 * Chunk allocation falls into two parts. The first part does works
4089 * that make the new allocated chunk useable, but not do any operation
4090 * that modifies the chunk tree. The second part does the works that
4091 * require modifying the chunk tree. This division is important for the
4092 * bootstrap process of adding storage to a seed btrfs.
4093 */
4094int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4095 struct btrfs_root *extent_root, u64 type)
4096{
4097 u64 chunk_offset;
4098 u64 chunk_size;
4099 u64 stripe_size;
4100 struct map_lookup *map;
4101 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4102 int ret;
4103
4104 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4105 &chunk_offset);
4106 if (ret)
4107 return ret;
4108
4109 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4110 &stripe_size, chunk_offset, type);
4111 if (ret)
4112 return ret;
4113
4114 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4115 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004116 if (ret)
4117 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004118 return 0;
4119}
4120
Chris Masond3977122009-01-05 21:25:51 -05004121static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004122 struct btrfs_root *root,
4123 struct btrfs_device *device)
4124{
4125 u64 chunk_offset;
4126 u64 sys_chunk_offset;
4127 u64 chunk_size;
4128 u64 sys_chunk_size;
4129 u64 stripe_size;
4130 u64 sys_stripe_size;
4131 u64 alloc_profile;
4132 struct map_lookup *map;
4133 struct map_lookup *sys_map;
4134 struct btrfs_fs_info *fs_info = root->fs_info;
4135 struct btrfs_root *extent_root = fs_info->extent_root;
4136 int ret;
4137
4138 ret = find_next_chunk(fs_info->chunk_root,
4139 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07004140 if (ret)
4141 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004142
Miao Xiede98ced2013-01-29 10:13:12 +00004143 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05004144 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4145 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004146 if (ret)
4147 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004148
4149 sys_chunk_offset = chunk_offset + chunk_size;
4150
Miao Xiede98ced2013-01-29 10:13:12 +00004151 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05004152 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
4153 &sys_chunk_size, &sys_stripe_size,
4154 sys_chunk_offset, alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004155 if (ret) {
4156 btrfs_abort_transaction(trans, root, ret);
4157 goto out;
4158 }
Yan Zheng2b820322008-11-17 21:11:30 -05004159
4160 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
David Sterba005d6422012-09-18 07:52:32 -06004161 if (ret) {
4162 btrfs_abort_transaction(trans, root, ret);
4163 goto out;
4164 }
Yan Zheng2b820322008-11-17 21:11:30 -05004165
4166 /*
4167 * Modifying chunk tree needs allocating new blocks from both
4168 * system block group and metadata block group. So we only can
4169 * do operations require modifying the chunk tree after both
4170 * block groups were created.
4171 */
4172 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4173 chunk_size, stripe_size);
David Sterba005d6422012-09-18 07:52:32 -06004174 if (ret) {
4175 btrfs_abort_transaction(trans, root, ret);
4176 goto out;
4177 }
Yan Zheng2b820322008-11-17 21:11:30 -05004178
4179 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
4180 sys_chunk_offset, sys_chunk_size,
4181 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004182 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004183 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004184
David Sterba005d6422012-09-18 07:52:32 -06004185out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004186
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004187 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004188}
4189
4190int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4191{
4192 struct extent_map *em;
4193 struct map_lookup *map;
4194 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4195 int readonly = 0;
4196 int i;
4197
Chris Mason890871b2009-09-02 16:24:52 -04004198 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004199 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004200 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004201 if (!em)
4202 return 1;
4203
Josef Bacikf48b9072010-01-27 02:07:59 +00004204 if (btrfs_test_opt(root, DEGRADED)) {
4205 free_extent_map(em);
4206 return 0;
4207 }
4208
Yan Zheng2b820322008-11-17 21:11:30 -05004209 map = (struct map_lookup *)em->bdev;
4210 for (i = 0; i < map->num_stripes; i++) {
4211 if (!map->stripes[i].dev->writeable) {
4212 readonly = 1;
4213 break;
4214 }
4215 }
4216 free_extent_map(em);
4217 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004218}
4219
4220void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4221{
David Sterbaa8067e02011-04-21 00:34:43 +02004222 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004223}
4224
4225void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4226{
4227 struct extent_map *em;
4228
Chris Masond3977122009-01-05 21:25:51 -05004229 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004230 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004231 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4232 if (em)
4233 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004234 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004235 if (!em)
4236 break;
4237 kfree(em->bdev);
4238 /* once for us */
4239 free_extent_map(em);
4240 /* once for the tree */
4241 free_extent_map(em);
4242 }
4243}
4244
Stefan Behrens5d964052012-11-05 14:59:07 +01004245int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004246{
Stefan Behrens5d964052012-11-05 14:59:07 +01004247 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004248 struct extent_map *em;
4249 struct map_lookup *map;
4250 struct extent_map_tree *em_tree = &map_tree->map_tree;
4251 int ret;
4252
Chris Mason890871b2009-09-02 16:24:52 -04004253 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004254 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004255 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004256
Josef Bacikfb7669b2013-04-23 10:53:18 -04004257 /*
4258 * We could return errors for these cases, but that could get ugly and
4259 * we'd probably do the same thing which is just not do anything else
4260 * and exit, so return 1 so the callers don't try to use other copies.
4261 */
4262 if (!em) {
4263 btrfs_emerg(fs_info, "No mapping for %Lu-%Lu\n", logical,
4264 logical+len);
4265 return 1;
4266 }
4267
4268 if (em->start > logical || em->start + em->len < logical) {
4269 btrfs_emerg(fs_info, "Invalid mapping for %Lu-%Lu, got "
4270 "%Lu-%Lu\n", logical, logical+len, em->start,
4271 em->start + em->len);
Liu Bofb5834f2013-09-29 10:33:16 +08004272 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004273 return 1;
4274 }
4275
Chris Masonf1885912008-04-09 16:28:12 -04004276 map = (struct map_lookup *)em->bdev;
4277 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4278 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004279 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4280 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004281 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4282 ret = 2;
4283 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4284 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004285 else
4286 ret = 1;
4287 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004288
4289 btrfs_dev_replace_lock(&fs_info->dev_replace);
4290 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4291 ret++;
4292 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4293
Chris Masonf1885912008-04-09 16:28:12 -04004294 return ret;
4295}
4296
David Woodhouse53b381b2013-01-29 18:40:14 -05004297unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4298 struct btrfs_mapping_tree *map_tree,
4299 u64 logical)
4300{
4301 struct extent_map *em;
4302 struct map_lookup *map;
4303 struct extent_map_tree *em_tree = &map_tree->map_tree;
4304 unsigned long len = root->sectorsize;
4305
4306 read_lock(&em_tree->lock);
4307 em = lookup_extent_mapping(em_tree, logical, len);
4308 read_unlock(&em_tree->lock);
4309 BUG_ON(!em);
4310
4311 BUG_ON(em->start > logical || em->start + em->len < logical);
4312 map = (struct map_lookup *)em->bdev;
4313 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4314 BTRFS_BLOCK_GROUP_RAID6)) {
4315 len = map->stripe_len * nr_data_stripes(map);
4316 }
4317 free_extent_map(em);
4318 return len;
4319}
4320
4321int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4322 u64 logical, u64 len, int mirror_num)
4323{
4324 struct extent_map *em;
4325 struct map_lookup *map;
4326 struct extent_map_tree *em_tree = &map_tree->map_tree;
4327 int ret = 0;
4328
4329 read_lock(&em_tree->lock);
4330 em = lookup_extent_mapping(em_tree, logical, len);
4331 read_unlock(&em_tree->lock);
4332 BUG_ON(!em);
4333
4334 BUG_ON(em->start > logical || em->start + em->len < logical);
4335 map = (struct map_lookup *)em->bdev;
4336 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4337 BTRFS_BLOCK_GROUP_RAID6))
4338 ret = 1;
4339 free_extent_map(em);
4340 return ret;
4341}
4342
Stefan Behrens30d98612012-11-06 14:52:18 +01004343static int find_live_mirror(struct btrfs_fs_info *fs_info,
4344 struct map_lookup *map, int first, int num,
4345 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004346{
4347 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004348 int tolerance;
4349 struct btrfs_device *srcdev;
4350
4351 if (dev_replace_is_ongoing &&
4352 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4353 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4354 srcdev = fs_info->dev_replace.srcdev;
4355 else
4356 srcdev = NULL;
4357
4358 /*
4359 * try to avoid the drive that is the source drive for a
4360 * dev-replace procedure, only choose it if no other non-missing
4361 * mirror is available
4362 */
4363 for (tolerance = 0; tolerance < 2; tolerance++) {
4364 if (map->stripes[optimal].dev->bdev &&
4365 (tolerance || map->stripes[optimal].dev != srcdev))
4366 return optimal;
4367 for (i = first; i < first + num; i++) {
4368 if (map->stripes[i].dev->bdev &&
4369 (tolerance || map->stripes[i].dev != srcdev))
4370 return i;
4371 }
Chris Masondfe25022008-05-13 13:46:40 -04004372 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004373
Chris Masondfe25022008-05-13 13:46:40 -04004374 /* we couldn't find one that doesn't fail. Just return something
4375 * and the io error handling code will clean up eventually
4376 */
4377 return optimal;
4378}
4379
David Woodhouse53b381b2013-01-29 18:40:14 -05004380static inline int parity_smaller(u64 a, u64 b)
4381{
4382 return a > b;
4383}
4384
4385/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4386static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4387{
4388 struct btrfs_bio_stripe s;
4389 int i;
4390 u64 l;
4391 int again = 1;
4392
4393 while (again) {
4394 again = 0;
4395 for (i = 0; i < bbio->num_stripes - 1; i++) {
4396 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4397 s = bbio->stripes[i];
4398 l = raid_map[i];
4399 bbio->stripes[i] = bbio->stripes[i+1];
4400 raid_map[i] = raid_map[i+1];
4401 bbio->stripes[i+1] = s;
4402 raid_map[i+1] = l;
4403 again = 1;
4404 }
4405 }
4406 }
4407}
4408
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004409static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004410 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004411 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004412 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004413{
4414 struct extent_map *em;
4415 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004416 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004417 struct extent_map_tree *em_tree = &map_tree->map_tree;
4418 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004419 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004420 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004421 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004422 u64 stripe_nr_orig;
4423 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004424 u64 stripe_len;
4425 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004426 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004427 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004428 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004429 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004430 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004431 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004432 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4433 int dev_replace_is_ongoing = 0;
4434 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004435 int patch_the_first_stripe_for_dev_replace = 0;
4436 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004437 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004438
Chris Mason890871b2009-09-02 16:24:52 -04004439 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004440 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004441 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004442
Chris Mason3b951512008-04-17 11:29:12 -04004443 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004444 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
4445 (unsigned long long)logical,
4446 (unsigned long long)*length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004447 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004448 }
Chris Mason0b86a832008-03-24 15:01:56 -04004449
Josef Bacik9bb91872013-04-19 23:45:33 -04004450 if (em->start > logical || em->start + em->len < logical) {
4451 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
4452 "found %Lu-%Lu\n", logical, em->start,
4453 em->start + em->len);
Liu Bofb5834f2013-09-29 10:33:16 +08004454 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004455 return -EINVAL;
4456 }
4457
Chris Mason0b86a832008-03-24 15:01:56 -04004458 map = (struct map_lookup *)em->bdev;
4459 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004460
David Woodhouse53b381b2013-01-29 18:40:14 -05004461 if (mirror_num > map->num_stripes)
4462 mirror_num = 0;
4463
4464 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004465 stripe_nr = offset;
4466 /*
4467 * stripe_nr counts the total number of stripes we have to stride
4468 * to get to this block
4469 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004470 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004471
David Woodhouse53b381b2013-01-29 18:40:14 -05004472 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004473 BUG_ON(offset < stripe_offset);
4474
4475 /* stripe_offset is the offset of this block in its stripe*/
4476 stripe_offset = offset - stripe_offset;
4477
David Woodhouse53b381b2013-01-29 18:40:14 -05004478 /* if we're here for raid56, we need to know the stripe aligned start */
4479 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4480 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4481 raid56_full_stripe_start = offset;
4482
4483 /* allow a write of a full stripe, but make sure we don't
4484 * allow straddling of stripes
4485 */
4486 do_div(raid56_full_stripe_start, full_stripe_len);
4487 raid56_full_stripe_start *= full_stripe_len;
4488 }
4489
4490 if (rw & REQ_DISCARD) {
4491 /* we don't discard raid56 yet */
4492 if (map->type &
4493 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4494 ret = -EOPNOTSUPP;
4495 goto out;
4496 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004497 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004498 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4499 u64 max_len;
4500 /* For writes to RAID[56], allow a full stripeset across all disks.
4501 For other RAID types and for RAID[56] reads, just allow a single
4502 stripe (on a single disk). */
4503 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4504 (rw & REQ_WRITE)) {
4505 max_len = stripe_len * nr_data_stripes(map) -
4506 (offset - raid56_full_stripe_start);
4507 } else {
4508 /* we limit the length of each bio to what fits in a stripe */
4509 max_len = stripe_len - stripe_offset;
4510 }
4511 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004512 } else {
4513 *length = em->len - offset;
4514 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004515
David Woodhouse53b381b2013-01-29 18:40:14 -05004516 /* This is for when we're called from btrfs_merge_bio_hook() and all
4517 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004518 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004519 goto out;
4520
Stefan Behrens472262f2012-11-06 14:43:46 +01004521 btrfs_dev_replace_lock(dev_replace);
4522 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4523 if (!dev_replace_is_ongoing)
4524 btrfs_dev_replace_unlock(dev_replace);
4525
Stefan Behrensad6d6202012-11-06 15:06:47 +01004526 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4527 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4528 dev_replace->tgtdev != NULL) {
4529 /*
4530 * in dev-replace case, for repair case (that's the only
4531 * case where the mirror is selected explicitly when
4532 * calling btrfs_map_block), blocks left of the left cursor
4533 * can also be read from the target drive.
4534 * For REQ_GET_READ_MIRRORS, the target drive is added as
4535 * the last one to the array of stripes. For READ, it also
4536 * needs to be supported using the same mirror number.
4537 * If the requested block is not left of the left cursor,
4538 * EIO is returned. This can happen because btrfs_num_copies()
4539 * returns one more in the dev-replace case.
4540 */
4541 u64 tmp_length = *length;
4542 struct btrfs_bio *tmp_bbio = NULL;
4543 int tmp_num_stripes;
4544 u64 srcdev_devid = dev_replace->srcdev->devid;
4545 int index_srcdev = 0;
4546 int found = 0;
4547 u64 physical_of_found = 0;
4548
4549 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004550 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004551 if (ret) {
4552 WARN_ON(tmp_bbio != NULL);
4553 goto out;
4554 }
4555
4556 tmp_num_stripes = tmp_bbio->num_stripes;
4557 if (mirror_num > tmp_num_stripes) {
4558 /*
4559 * REQ_GET_READ_MIRRORS does not contain this
4560 * mirror, that means that the requested area
4561 * is not left of the left cursor
4562 */
4563 ret = -EIO;
4564 kfree(tmp_bbio);
4565 goto out;
4566 }
4567
4568 /*
4569 * process the rest of the function using the mirror_num
4570 * of the source drive. Therefore look it up first.
4571 * At the end, patch the device pointer to the one of the
4572 * target drive.
4573 */
4574 for (i = 0; i < tmp_num_stripes; i++) {
4575 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4576 /*
4577 * In case of DUP, in order to keep it
4578 * simple, only add the mirror with the
4579 * lowest physical address
4580 */
4581 if (found &&
4582 physical_of_found <=
4583 tmp_bbio->stripes[i].physical)
4584 continue;
4585 index_srcdev = i;
4586 found = 1;
4587 physical_of_found =
4588 tmp_bbio->stripes[i].physical;
4589 }
4590 }
4591
4592 if (found) {
4593 mirror_num = index_srcdev + 1;
4594 patch_the_first_stripe_for_dev_replace = 1;
4595 physical_to_patch_in_first_stripe = physical_of_found;
4596 } else {
4597 WARN_ON(1);
4598 ret = -EIO;
4599 kfree(tmp_bbio);
4600 goto out;
4601 }
4602
4603 kfree(tmp_bbio);
4604 } else if (mirror_num > map->num_stripes) {
4605 mirror_num = 0;
4606 }
4607
Chris Masonf2d8d742008-04-21 10:03:05 -04004608 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004609 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004610 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00004611 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00004612 do_div(stripe_nr_end, map->stripe_len);
4613 stripe_end_offset = stripe_nr_end * map->stripe_len -
4614 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004615
Li Dongyangfce3bb92011-03-24 10:24:26 +00004616 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4617 if (rw & REQ_DISCARD)
4618 num_stripes = min_t(u64, map->num_stripes,
4619 stripe_nr_end - stripe_nr_orig);
4620 stripe_index = do_div(stripe_nr, map->num_stripes);
4621 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004622 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004623 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04004624 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04004625 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004626 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01004627 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04004628 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01004629 current->pid % map->num_stripes,
4630 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004631 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004632 }
Chris Mason2fff7342008-04-29 14:12:09 -04004633
Chris Mason611f0e02008-04-03 16:29:03 -04004634 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004635 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04004636 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004637 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04004638 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004639 } else {
4640 mirror_num = 1;
4641 }
Chris Mason2fff7342008-04-29 14:12:09 -04004642
Chris Mason321aecc2008-04-16 10:49:51 -04004643 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4644 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004645
4646 stripe_index = do_div(stripe_nr, factor);
4647 stripe_index *= map->sub_stripes;
4648
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004649 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004650 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004651 else if (rw & REQ_DISCARD)
4652 num_stripes = min_t(u64, map->sub_stripes *
4653 (stripe_nr_end - stripe_nr_orig),
4654 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04004655 else if (mirror_num)
4656 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004657 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04004658 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01004659 stripe_index = find_live_mirror(fs_info, map,
4660 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04004661 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01004662 current->pid % map->sub_stripes,
4663 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04004664 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004665 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004666
4667 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4668 BTRFS_BLOCK_GROUP_RAID6)) {
4669 u64 tmp;
4670
4671 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
4672 && raid_map_ret) {
4673 int i, rot;
4674
4675 /* push stripe_nr back to the start of the full stripe */
4676 stripe_nr = raid56_full_stripe_start;
4677 do_div(stripe_nr, stripe_len);
4678
4679 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4680
4681 /* RAID[56] write or recovery. Return all stripes */
4682 num_stripes = map->num_stripes;
4683 max_errors = nr_parity_stripes(map);
4684
4685 raid_map = kmalloc(sizeof(u64) * num_stripes,
4686 GFP_NOFS);
4687 if (!raid_map) {
4688 ret = -ENOMEM;
4689 goto out;
4690 }
4691
4692 /* Work out the disk rotation on this stripe-set */
4693 tmp = stripe_nr;
4694 rot = do_div(tmp, num_stripes);
4695
4696 /* Fill in the logical address of each stripe */
4697 tmp = stripe_nr * nr_data_stripes(map);
4698 for (i = 0; i < nr_data_stripes(map); i++)
4699 raid_map[(i+rot) % num_stripes] =
4700 em->start + (tmp + i) * map->stripe_len;
4701
4702 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
4703 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4704 raid_map[(i+rot+1) % num_stripes] =
4705 RAID6_Q_STRIPE;
4706
4707 *length = map->stripe_len;
4708 stripe_index = 0;
4709 stripe_offset = 0;
4710 } else {
4711 /*
4712 * Mirror #0 or #1 means the original data block.
4713 * Mirror #2 is RAID5 parity block.
4714 * Mirror #3 is RAID6 Q block.
4715 */
4716 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4717 if (mirror_num > 1)
4718 stripe_index = nr_data_stripes(map) +
4719 mirror_num - 2;
4720
4721 /* We distribute the parity blocks across stripes */
4722 tmp = stripe_nr + stripe_index;
4723 stripe_index = do_div(tmp, map->num_stripes);
4724 }
Chris Mason8790d502008-04-03 16:29:03 -04004725 } else {
4726 /*
4727 * after this do_div call, stripe_nr is the number of stripes
4728 * on this device we have to walk to find the data, and
4729 * stripe_index is the number of our device in the stripe array
4730 */
4731 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004732 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04004733 }
Chris Mason593060d2008-03-25 16:50:33 -04004734 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04004735
Stefan Behrens472262f2012-11-06 14:43:46 +01004736 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004737 if (dev_replace_is_ongoing) {
4738 if (rw & (REQ_WRITE | REQ_DISCARD))
4739 num_alloc_stripes <<= 1;
4740 if (rw & REQ_GET_READ_MIRRORS)
4741 num_alloc_stripes++;
4742 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004743 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08004744 if (!bbio) {
4745 ret = -ENOMEM;
4746 goto out;
4747 }
4748 atomic_set(&bbio->error, 0);
4749
Li Dongyangfce3bb92011-03-24 10:24:26 +00004750 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08004751 int factor = 0;
4752 int sub_stripes = 0;
4753 u64 stripes_per_dev = 0;
4754 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04004755 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004756
4757 if (map->type &
4758 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4759 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4760 sub_stripes = 1;
4761 else
4762 sub_stripes = map->sub_stripes;
4763
4764 factor = map->num_stripes / sub_stripes;
4765 stripes_per_dev = div_u64_rem(stripe_nr_end -
4766 stripe_nr_orig,
4767 factor,
4768 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04004769 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4770 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004771 }
4772
Li Dongyangfce3bb92011-03-24 10:24:26 +00004773 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004774 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04004775 map->stripes[stripe_index].physical +
4776 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004777 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004778
Li Zefanec9ef7a2011-12-01 14:06:42 +08004779 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4780 BTRFS_BLOCK_GROUP_RAID10)) {
4781 bbio->stripes[i].length = stripes_per_dev *
4782 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004783
Li Zefanec9ef7a2011-12-01 14:06:42 +08004784 if (i / sub_stripes < remaining_stripes)
4785 bbio->stripes[i].length +=
4786 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004787
4788 /*
4789 * Special for the first stripe and
4790 * the last stripe:
4791 *
4792 * |-------|...|-------|
4793 * |----------|
4794 * off end_off
4795 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08004796 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004797 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00004798 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004799
4800 if (stripe_index >= last_stripe &&
4801 stripe_index <= (last_stripe +
4802 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08004803 bbio->stripes[i].length -=
4804 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004805
Li Zefanec9ef7a2011-12-01 14:06:42 +08004806 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00004807 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004808 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02004809 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004810
4811 stripe_index++;
4812 if (stripe_index == map->num_stripes) {
4813 /* This could only happen for RAID0/10 */
4814 stripe_index = 0;
4815 stripe_nr++;
4816 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004817 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004818 } else {
4819 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004820 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004821 map->stripes[stripe_index].physical +
4822 stripe_offset +
4823 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004824 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004825 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004826 stripe_index++;
4827 }
Chris Mason593060d2008-03-25 16:50:33 -04004828 }
Li Zefande11cc12011-12-01 12:55:47 +08004829
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004830 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
Li Zefande11cc12011-12-01 12:55:47 +08004831 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4832 BTRFS_BLOCK_GROUP_RAID10 |
David Woodhouse53b381b2013-01-29 18:40:14 -05004833 BTRFS_BLOCK_GROUP_RAID5 |
Li Zefande11cc12011-12-01 12:55:47 +08004834 BTRFS_BLOCK_GROUP_DUP)) {
4835 max_errors = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05004836 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4837 max_errors = 2;
Li Zefande11cc12011-12-01 12:55:47 +08004838 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004839 }
Li Zefande11cc12011-12-01 12:55:47 +08004840
Stefan Behrens472262f2012-11-06 14:43:46 +01004841 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
4842 dev_replace->tgtdev != NULL) {
4843 int index_where_to_add;
4844 u64 srcdev_devid = dev_replace->srcdev->devid;
4845
4846 /*
4847 * duplicate the write operations while the dev replace
4848 * procedure is running. Since the copying of the old disk
4849 * to the new disk takes place at run time while the
4850 * filesystem is mounted writable, the regular write
4851 * operations to the old disk have to be duplicated to go
4852 * to the new disk as well.
4853 * Note that device->missing is handled by the caller, and
4854 * that the write to the old disk is already set up in the
4855 * stripes array.
4856 */
4857 index_where_to_add = num_stripes;
4858 for (i = 0; i < num_stripes; i++) {
4859 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4860 /* write to new disk, too */
4861 struct btrfs_bio_stripe *new =
4862 bbio->stripes + index_where_to_add;
4863 struct btrfs_bio_stripe *old =
4864 bbio->stripes + i;
4865
4866 new->physical = old->physical;
4867 new->length = old->length;
4868 new->dev = dev_replace->tgtdev;
4869 index_where_to_add++;
4870 max_errors++;
4871 }
4872 }
4873 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004874 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
4875 dev_replace->tgtdev != NULL) {
4876 u64 srcdev_devid = dev_replace->srcdev->devid;
4877 int index_srcdev = 0;
4878 int found = 0;
4879 u64 physical_of_found = 0;
4880
4881 /*
4882 * During the dev-replace procedure, the target drive can
4883 * also be used to read data in case it is needed to repair
4884 * a corrupt block elsewhere. This is possible if the
4885 * requested area is left of the left cursor. In this area,
4886 * the target drive is a full copy of the source drive.
4887 */
4888 for (i = 0; i < num_stripes; i++) {
4889 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4890 /*
4891 * In case of DUP, in order to keep it
4892 * simple, only add the mirror with the
4893 * lowest physical address
4894 */
4895 if (found &&
4896 physical_of_found <=
4897 bbio->stripes[i].physical)
4898 continue;
4899 index_srcdev = i;
4900 found = 1;
4901 physical_of_found = bbio->stripes[i].physical;
4902 }
4903 }
4904 if (found) {
4905 u64 length = map->stripe_len;
4906
4907 if (physical_of_found + length <=
4908 dev_replace->cursor_left) {
4909 struct btrfs_bio_stripe *tgtdev_stripe =
4910 bbio->stripes + num_stripes;
4911
4912 tgtdev_stripe->physical = physical_of_found;
4913 tgtdev_stripe->length =
4914 bbio->stripes[index_srcdev].length;
4915 tgtdev_stripe->dev = dev_replace->tgtdev;
4916
4917 num_stripes++;
4918 }
4919 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004920 }
4921
Li Zefande11cc12011-12-01 12:55:47 +08004922 *bbio_ret = bbio;
4923 bbio->num_stripes = num_stripes;
4924 bbio->max_errors = max_errors;
4925 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004926
4927 /*
4928 * this is the case that REQ_READ && dev_replace_is_ongoing &&
4929 * mirror_num == num_stripes + 1 && dev_replace target drive is
4930 * available as a mirror
4931 */
4932 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
4933 WARN_ON(num_stripes > 1);
4934 bbio->stripes[0].dev = dev_replace->tgtdev;
4935 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
4936 bbio->mirror_num = map->num_stripes + 1;
4937 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004938 if (raid_map) {
4939 sort_parity_stripes(bbio, raid_map);
4940 *raid_map_ret = raid_map;
4941 }
Chris Masoncea9e442008-04-09 16:28:12 -04004942out:
Stefan Behrens472262f2012-11-06 14:43:46 +01004943 if (dev_replace_is_ongoing)
4944 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04004945 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08004946 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004947}
4948
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004949int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004950 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004951 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04004952{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004953 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004954 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04004955}
4956
Yan Zhenga512bbf2008-12-08 16:46:26 -05004957int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4958 u64 chunk_start, u64 physical, u64 devid,
4959 u64 **logical, int *naddrs, int *stripe_len)
4960{
4961 struct extent_map_tree *em_tree = &map_tree->map_tree;
4962 struct extent_map *em;
4963 struct map_lookup *map;
4964 u64 *buf;
4965 u64 bytenr;
4966 u64 length;
4967 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05004968 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05004969 int i, j, nr = 0;
4970
Chris Mason890871b2009-09-02 16:24:52 -04004971 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004972 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004973 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004974
Josef Bacik835d9742013-03-19 12:13:25 -04004975 if (!em) {
4976 printk(KERN_ERR "btrfs: couldn't find em for chunk %Lu\n",
4977 chunk_start);
4978 return -EIO;
4979 }
4980
4981 if (em->start != chunk_start) {
4982 printk(KERN_ERR "btrfs: bad chunk start, em=%Lu, wanted=%Lu\n",
4983 em->start, chunk_start);
4984 free_extent_map(em);
4985 return -EIO;
4986 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004987 map = (struct map_lookup *)em->bdev;
4988
4989 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05004990 rmap_len = map->stripe_len;
4991
Yan Zhenga512bbf2008-12-08 16:46:26 -05004992 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4993 do_div(length, map->num_stripes / map->sub_stripes);
4994 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4995 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05004996 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4997 BTRFS_BLOCK_GROUP_RAID6)) {
4998 do_div(length, nr_data_stripes(map));
4999 rmap_len = map->stripe_len * nr_data_stripes(map);
5000 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005001
5002 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005003 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005004
5005 for (i = 0; i < map->num_stripes; i++) {
5006 if (devid && map->stripes[i].dev->devid != devid)
5007 continue;
5008 if (map->stripes[i].physical > physical ||
5009 map->stripes[i].physical + length <= physical)
5010 continue;
5011
5012 stripe_nr = physical - map->stripes[i].physical;
5013 do_div(stripe_nr, map->stripe_len);
5014
5015 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5016 stripe_nr = stripe_nr * map->num_stripes + i;
5017 do_div(stripe_nr, map->sub_stripes);
5018 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5019 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005020 } /* else if RAID[56], multiply by nr_data_stripes().
5021 * Alternatively, just use rmap_len below instead of
5022 * map->stripe_len */
5023
5024 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005025 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005026 for (j = 0; j < nr; j++) {
5027 if (buf[j] == bytenr)
5028 break;
5029 }
Chris Mason934d3752008-12-08 16:43:10 -05005030 if (j == nr) {
5031 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005032 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005033 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005034 }
5035
Yan Zhenga512bbf2008-12-08 16:46:26 -05005036 *logical = buf;
5037 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005038 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005039
5040 free_extent_map(em);
5041 return 0;
5042}
5043
Jan Schmidta1d3c472011-08-04 17:15:33 +02005044static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005045{
Chris Mason9be33952013-05-17 18:30:14 -04005046 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005047 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005048
Stefan Behrens442a4f62012-05-25 16:06:08 +02005049 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005050 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005051 if (err == -EIO || err == -EREMOTEIO) {
5052 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005053 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005054 struct btrfs_device *dev;
5055
5056 BUG_ON(stripe_index >= bbio->num_stripes);
5057 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005058 if (dev->bdev) {
5059 if (bio->bi_rw & WRITE)
5060 btrfs_dev_stat_inc(dev,
5061 BTRFS_DEV_STAT_WRITE_ERRS);
5062 else
5063 btrfs_dev_stat_inc(dev,
5064 BTRFS_DEV_STAT_READ_ERRS);
5065 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5066 btrfs_dev_stat_inc(dev,
5067 BTRFS_DEV_STAT_FLUSH_ERRS);
5068 btrfs_dev_stat_print_on_error(dev);
5069 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005070 }
5071 }
Chris Mason8790d502008-04-03 16:29:03 -04005072
Jan Schmidta1d3c472011-08-04 17:15:33 +02005073 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005074 is_orig_bio = 1;
5075
Jan Schmidta1d3c472011-08-04 17:15:33 +02005076 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005077 if (!is_orig_bio) {
5078 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005079 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005080 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005081 bio->bi_private = bbio->private;
5082 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005083 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005084 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005085 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005086 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005087 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005088 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005089 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005090 /*
5091 * this bio is actually up to date, we didn't
5092 * go over the max number of errors
5093 */
5094 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005095 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005096 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005097 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04005098
5099 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005100 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005101 bio_put(bio);
5102 }
Chris Mason8790d502008-04-03 16:29:03 -04005103}
5104
Chris Mason8b712842008-06-11 16:50:36 -04005105struct async_sched {
5106 struct bio *bio;
5107 int rw;
5108 struct btrfs_fs_info *info;
5109 struct btrfs_work work;
5110};
5111
5112/*
5113 * see run_scheduled_bios for a description of why bios are collected for
5114 * async submit.
5115 *
5116 * This will add one bio to the pending list for a device and make sure
5117 * the work struct is scheduled.
5118 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005119static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5120 struct btrfs_device *device,
5121 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005122{
5123 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005124 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005125
David Woodhouse53b381b2013-01-29 18:40:14 -05005126 if (device->missing || !device->bdev) {
5127 bio_endio(bio, -EIO);
5128 return;
5129 }
5130
Chris Mason8b712842008-06-11 16:50:36 -04005131 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005132 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005133 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005134 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005135 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005136 return;
Chris Mason8b712842008-06-11 16:50:36 -04005137 }
5138
5139 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005140 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005141 * higher layers. Otherwise, the async bio makes it appear we have
5142 * made progress against dirty pages when we've really just put it
5143 * on a queue for later
5144 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005145 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005146 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005147 bio->bi_next = NULL;
5148 bio->bi_rw |= rw;
5149
5150 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005151 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005152 pending_bios = &device->pending_sync_bios;
5153 else
5154 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005155
Chris Masonffbd5172009-04-20 15:50:09 -04005156 if (pending_bios->tail)
5157 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005158
Chris Masonffbd5172009-04-20 15:50:09 -04005159 pending_bios->tail = bio;
5160 if (!pending_bios->head)
5161 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005162 if (device->running_pending)
5163 should_queue = 0;
5164
5165 spin_unlock(&device->io_lock);
5166
5167 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04005168 btrfs_queue_worker(&root->fs_info->submit_workers,
5169 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005170}
5171
Josef Bacikde1ee922012-10-19 16:50:56 -04005172static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5173 sector_t sector)
5174{
5175 struct bio_vec *prev;
5176 struct request_queue *q = bdev_get_queue(bdev);
5177 unsigned short max_sectors = queue_max_sectors(q);
5178 struct bvec_merge_data bvm = {
5179 .bi_bdev = bdev,
5180 .bi_sector = sector,
5181 .bi_rw = bio->bi_rw,
5182 };
5183
5184 if (bio->bi_vcnt == 0) {
5185 WARN_ON(1);
5186 return 1;
5187 }
5188
5189 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005190 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005191 return 0;
5192
5193 if (!q->merge_bvec_fn)
5194 return 1;
5195
5196 bvm.bi_size = bio->bi_size - prev->bv_len;
5197 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5198 return 0;
5199 return 1;
5200}
5201
5202static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5203 struct bio *bio, u64 physical, int dev_nr,
5204 int rw, int async)
5205{
5206 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5207
5208 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005209 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005210 bio->bi_end_io = btrfs_end_bio;
5211 bio->bi_sector = physical >> 9;
5212#ifdef DEBUG
5213 {
5214 struct rcu_string *name;
5215
5216 rcu_read_lock();
5217 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005218 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005219 "(%s id %llu), size=%u\n", rw,
5220 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5221 name->str, dev->devid, bio->bi_size);
5222 rcu_read_unlock();
5223 }
5224#endif
5225 bio->bi_bdev = dev->bdev;
5226 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005227 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005228 else
5229 btrfsic_submit_bio(rw, bio);
5230}
5231
5232static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5233 struct bio *first_bio, struct btrfs_device *dev,
5234 int dev_nr, int rw, int async)
5235{
5236 struct bio_vec *bvec = first_bio->bi_io_vec;
5237 struct bio *bio;
5238 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5239 u64 physical = bbio->stripes[dev_nr].physical;
5240
5241again:
5242 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5243 if (!bio)
5244 return -ENOMEM;
5245
5246 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5247 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5248 bvec->bv_offset) < bvec->bv_len) {
5249 u64 len = bio->bi_size;
5250
5251 atomic_inc(&bbio->stripes_pending);
5252 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5253 rw, async);
5254 physical += len;
5255 goto again;
5256 }
5257 bvec++;
5258 }
5259
5260 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5261 return 0;
5262}
5263
5264static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5265{
5266 atomic_inc(&bbio->error);
5267 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5268 bio->bi_private = bbio->private;
5269 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005270 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Josef Bacikde1ee922012-10-19 16:50:56 -04005271 bio->bi_sector = logical >> 9;
5272 kfree(bbio);
5273 bio_endio(bio, -EIO);
5274 }
5275}
5276
Chris Masonf1885912008-04-09 16:28:12 -04005277int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005278 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005279{
Chris Mason0b86a832008-03-24 15:01:56 -04005280 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005281 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04005282 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005283 u64 length = 0;
5284 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005285 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005286 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005287 int dev_nr = 0;
5288 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005289 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005290
Chris Masonf2d8d742008-04-21 10:03:05 -04005291 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005292 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005293
David Woodhouse53b381b2013-01-29 18:40:14 -05005294 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5295 mirror_num, &raid_map);
5296 if (ret) /* -ENOMEM */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005297 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04005298
Jan Schmidta1d3c472011-08-04 17:15:33 +02005299 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005300 bbio->orig_bio = first_bio;
5301 bbio->private = first_bio->bi_private;
5302 bbio->end_io = first_bio->bi_end_io;
5303 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5304
5305 if (raid_map) {
5306 /* In this case, map_length has been set to the length of
5307 a single stripe; not the whole write */
5308 if (rw & WRITE) {
5309 return raid56_parity_write(root, bio, bbio,
5310 raid_map, map_length);
5311 } else {
5312 return raid56_parity_recover(root, bio, bbio,
5313 raid_map, map_length,
5314 mirror_num);
5315 }
5316 }
5317
Chris Masoncea9e442008-04-09 16:28:12 -04005318 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005319 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
5320 (unsigned long long)logical,
5321 (unsigned long long)length,
5322 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005323 BUG();
5324 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005325
Chris Masond3977122009-01-05 21:25:51 -05005326 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005327 dev = bbio->stripes[dev_nr].dev;
5328 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5329 bbio_error(bbio, first_bio, logical);
5330 dev_nr++;
5331 continue;
5332 }
5333
5334 /*
5335 * Check and see if we're ok with this bio based on it's size
5336 * and offset with the given device.
5337 */
5338 if (!bio_size_ok(dev->bdev, first_bio,
5339 bbio->stripes[dev_nr].physical >> 9)) {
5340 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5341 dev_nr, rw, async_submit);
5342 BUG_ON(ret);
5343 dev_nr++;
5344 continue;
5345 }
5346
Jan Schmidta1d3c472011-08-04 17:15:33 +02005347 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005348 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005349 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005350 } else {
5351 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04005352 }
Josef Bacik606686e2012-06-04 14:03:51 -04005353
Josef Bacikde1ee922012-10-19 16:50:56 -04005354 submit_stripe_bio(root, bbio, bio,
5355 bbio->stripes[dev_nr].physical, dev_nr, rw,
5356 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005357 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005358 }
Chris Mason0b86a832008-03-24 15:01:56 -04005359 return 0;
5360}
5361
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005362struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005363 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005364{
Yan Zheng2b820322008-11-17 21:11:30 -05005365 struct btrfs_device *device;
5366 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005367
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005368 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005369 while (cur_devices) {
5370 if (!fsid ||
5371 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5372 device = __find_device(&cur_devices->devices,
5373 devid, uuid);
5374 if (device)
5375 return device;
5376 }
5377 cur_devices = cur_devices->seed;
5378 }
5379 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005380}
5381
Chris Masondfe25022008-05-13 13:46:40 -04005382static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5383 u64 devid, u8 *dev_uuid)
5384{
5385 struct btrfs_device *device;
5386 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5387
5388 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005389 if (!device)
5390 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04005391 list_add(&device->dev_list,
5392 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04005393 device->dev_root = root->fs_info->dev_root;
5394 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04005395 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05005396 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05005397 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04005398 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05005399 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04005400 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05005401 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04005402 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
5403 return device;
5404}
5405
Chris Mason0b86a832008-03-24 15:01:56 -04005406static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5407 struct extent_buffer *leaf,
5408 struct btrfs_chunk *chunk)
5409{
5410 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5411 struct map_lookup *map;
5412 struct extent_map *em;
5413 u64 logical;
5414 u64 length;
5415 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005416 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005417 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005418 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005419 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005420
Chris Masone17cade2008-04-15 15:41:47 -04005421 logical = key->offset;
5422 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005423
Chris Mason890871b2009-09-02 16:24:52 -04005424 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005425 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005426 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005427
5428 /* already mapped? */
5429 if (em && em->start <= logical && em->start + em->len > logical) {
5430 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005431 return 0;
5432 } else if (em) {
5433 free_extent_map(em);
5434 }
Chris Mason0b86a832008-03-24 15:01:56 -04005435
David Sterba172ddd62011-04-21 00:48:27 +02005436 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005437 if (!em)
5438 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005439 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5440 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005441 if (!map) {
5442 free_extent_map(em);
5443 return -ENOMEM;
5444 }
5445
5446 em->bdev = (struct block_device *)map;
5447 em->start = logical;
5448 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005449 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005450 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005451 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005452
Chris Mason593060d2008-03-25 16:50:33 -04005453 map->num_stripes = num_stripes;
5454 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5455 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5456 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5457 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5458 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005459 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005460 for (i = 0; i < num_stripes; i++) {
5461 map->stripes[i].physical =
5462 btrfs_stripe_offset_nr(leaf, chunk, i);
5463 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005464 read_extent_buffer(leaf, uuid, (unsigned long)
5465 btrfs_stripe_dev_uuid_nr(chunk, i),
5466 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005467 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5468 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005469 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005470 kfree(map);
5471 free_extent_map(em);
5472 return -EIO;
5473 }
Chris Masondfe25022008-05-13 13:46:40 -04005474 if (!map->stripes[i].dev) {
5475 map->stripes[i].dev =
5476 add_missing_dev(root, devid, uuid);
5477 if (!map->stripes[i].dev) {
5478 kfree(map);
5479 free_extent_map(em);
5480 return -EIO;
5481 }
5482 }
5483 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005484 }
5485
Chris Mason890871b2009-09-02 16:24:52 -04005486 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005487 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005488 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005489 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005490 free_extent_map(em);
5491
5492 return 0;
5493}
5494
Jeff Mahoney143bede2012-03-01 14:56:26 +01005495static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005496 struct btrfs_dev_item *dev_item,
5497 struct btrfs_device *device)
5498{
5499 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005500
5501 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005502 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5503 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005504 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5505 device->type = btrfs_device_type(leaf, dev_item);
5506 device->io_align = btrfs_device_io_align(leaf, dev_item);
5507 device->io_width = btrfs_device_io_width(leaf, dev_item);
5508 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005509 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005510 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005511
5512 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005513 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005514}
5515
Yan Zheng2b820322008-11-17 21:11:30 -05005516static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5517{
5518 struct btrfs_fs_devices *fs_devices;
5519 int ret;
5520
Li Zefanb367e472011-12-07 11:38:24 +08005521 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005522
5523 fs_devices = root->fs_info->fs_devices->seed;
5524 while (fs_devices) {
5525 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5526 ret = 0;
5527 goto out;
5528 }
5529 fs_devices = fs_devices->seed;
5530 }
5531
5532 fs_devices = find_fsid(fsid);
5533 if (!fs_devices) {
5534 ret = -ENOENT;
5535 goto out;
5536 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005537
5538 fs_devices = clone_fs_devices(fs_devices);
5539 if (IS_ERR(fs_devices)) {
5540 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005541 goto out;
5542 }
5543
Christoph Hellwig97288f22008-12-02 06:36:09 -05005544 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05005545 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02005546 if (ret) {
5547 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005548 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02005549 }
Yan Zheng2b820322008-11-17 21:11:30 -05005550
5551 if (!fs_devices->seeding) {
5552 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005553 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005554 ret = -EINVAL;
5555 goto out;
5556 }
5557
5558 fs_devices->seed = root->fs_info->fs_devices->seed;
5559 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005560out:
Yan Zheng2b820322008-11-17 21:11:30 -05005561 return ret;
5562}
5563
Chris Mason0d81ba52008-03-24 15:02:07 -04005564static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04005565 struct extent_buffer *leaf,
5566 struct btrfs_dev_item *dev_item)
5567{
5568 struct btrfs_device *device;
5569 u64 devid;
5570 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005571 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04005572 u8 dev_uuid[BTRFS_UUID_SIZE];
5573
Chris Mason0b86a832008-03-24 15:01:56 -04005574 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04005575 read_extent_buffer(leaf, dev_uuid,
5576 (unsigned long)btrfs_device_uuid(dev_item),
5577 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05005578 read_extent_buffer(leaf, fs_uuid,
5579 (unsigned long)btrfs_device_fsid(dev_item),
5580 BTRFS_UUID_SIZE);
5581
5582 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
5583 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05005584 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005585 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005586 }
5587
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005588 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05005589 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05005590 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005591 return -EIO;
5592
5593 if (!device) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005594 btrfs_warn(root->fs_info, "devid %llu missing",
5595 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05005596 device = add_missing_dev(root, devid, dev_uuid);
5597 if (!device)
5598 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05005599 } else if (!device->missing) {
5600 /*
5601 * this happens when a device that was properly setup
5602 * in the device info lists suddenly goes bad.
5603 * device->bdev is NULL, and so we have to set
5604 * device->missing to one here
5605 */
5606 root->fs_info->fs_devices->missing_devices++;
5607 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05005608 }
5609 }
5610
5611 if (device->fs_devices != root->fs_info->fs_devices) {
5612 BUG_ON(device->writeable);
5613 if (device->generation !=
5614 btrfs_device_generation(leaf, dev_item))
5615 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04005616 }
Chris Mason0b86a832008-03-24 15:01:56 -04005617
5618 fill_device_from_item(leaf, dev_item, device);
5619 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04005620 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01005621 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05005622 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04005623 spin_lock(&root->fs_info->free_chunk_lock);
5624 root->fs_info->free_chunk_space += device->total_bytes -
5625 device->bytes_used;
5626 spin_unlock(&root->fs_info->free_chunk_lock);
5627 }
Chris Mason0b86a832008-03-24 15:01:56 -04005628 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005629 return ret;
5630}
5631
Yan Zhenge4404d62008-12-12 10:03:26 -05005632int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04005633{
David Sterba6c417612011-04-13 15:41:04 +02005634 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04005635 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04005636 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04005637 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04005638 u8 *ptr;
5639 unsigned long sb_ptr;
5640 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005641 u32 num_stripes;
5642 u32 array_size;
5643 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005644 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04005645 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04005646
Yan Zhenge4404d62008-12-12 10:03:26 -05005647 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04005648 BTRFS_SUPER_INFO_SIZE);
5649 if (!sb)
5650 return -ENOMEM;
5651 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04005652 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02005653 /*
5654 * The sb extent buffer is artifical and just used to read the system array.
5655 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5656 * pages up-to-date when the page is larger: extent does not cover the
5657 * whole page and consequently check_page_uptodate does not find all
5658 * the page's extents up-to-date (the hole beyond sb),
5659 * write_extent_buffer then triggers a WARN_ON.
5660 *
5661 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5662 * but sb spans only this function. Add an explicit SetPageUptodate call
5663 * to silence the warning eg. on PowerPC 64.
5664 */
5665 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04005666 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05005667
Chris Masona061fc82008-05-07 11:43:44 -04005668 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005669 array_size = btrfs_super_sys_array_size(super_copy);
5670
Chris Mason0b86a832008-03-24 15:01:56 -04005671 ptr = super_copy->sys_chunk_array;
5672 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
5673 cur = 0;
5674
5675 while (cur < array_size) {
5676 disk_key = (struct btrfs_disk_key *)ptr;
5677 btrfs_disk_key_to_cpu(&key, disk_key);
5678
Chris Masona061fc82008-05-07 11:43:44 -04005679 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04005680 sb_ptr += len;
5681 cur += len;
5682
Chris Mason0d81ba52008-03-24 15:02:07 -04005683 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04005684 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04005685 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04005686 if (ret)
5687 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005688 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
5689 len = btrfs_chunk_item_size(num_stripes);
5690 } else {
Chris Mason84eed902008-04-25 09:04:37 -04005691 ret = -EIO;
5692 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005693 }
5694 ptr += len;
5695 sb_ptr += len;
5696 cur += len;
5697 }
Chris Masona061fc82008-05-07 11:43:44 -04005698 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04005699 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005700}
5701
5702int btrfs_read_chunk_tree(struct btrfs_root *root)
5703{
5704 struct btrfs_path *path;
5705 struct extent_buffer *leaf;
5706 struct btrfs_key key;
5707 struct btrfs_key found_key;
5708 int ret;
5709 int slot;
5710
5711 root = root->fs_info->chunk_root;
5712
5713 path = btrfs_alloc_path();
5714 if (!path)
5715 return -ENOMEM;
5716
Li Zefanb367e472011-12-07 11:38:24 +08005717 mutex_lock(&uuid_mutex);
5718 lock_chunks(root);
5719
Chris Mason0b86a832008-03-24 15:01:56 -04005720 /* first we search for all of the device items, and then we
5721 * read in all of the chunk items. This way we can create chunk
5722 * mappings that reference all of the devices that are afound
5723 */
5724 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5725 key.offset = 0;
5726 key.type = 0;
5727again:
5728 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00005729 if (ret < 0)
5730 goto error;
Chris Masond3977122009-01-05 21:25:51 -05005731 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005732 leaf = path->nodes[0];
5733 slot = path->slots[0];
5734 if (slot >= btrfs_header_nritems(leaf)) {
5735 ret = btrfs_next_leaf(root, path);
5736 if (ret == 0)
5737 continue;
5738 if (ret < 0)
5739 goto error;
5740 break;
5741 }
5742 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5743 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5744 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
5745 break;
5746 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5747 struct btrfs_dev_item *dev_item;
5748 dev_item = btrfs_item_ptr(leaf, slot,
5749 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04005750 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05005751 if (ret)
5752 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005753 }
5754 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5755 struct btrfs_chunk *chunk;
5756 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5757 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05005758 if (ret)
5759 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005760 }
5761 path->slots[0]++;
5762 }
5763 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5764 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005765 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005766 goto again;
5767 }
Chris Mason0b86a832008-03-24 15:01:56 -04005768 ret = 0;
5769error:
Li Zefanb367e472011-12-07 11:38:24 +08005770 unlock_chunks(root);
5771 mutex_unlock(&uuid_mutex);
5772
Yan Zheng2b820322008-11-17 21:11:30 -05005773 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005774 return ret;
5775}
Stefan Behrens442a4f62012-05-25 16:06:08 +02005776
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005777static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
5778{
5779 int i;
5780
5781 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5782 btrfs_dev_stat_reset(dev, i);
5783}
5784
5785int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
5786{
5787 struct btrfs_key key;
5788 struct btrfs_key found_key;
5789 struct btrfs_root *dev_root = fs_info->dev_root;
5790 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5791 struct extent_buffer *eb;
5792 int slot;
5793 int ret = 0;
5794 struct btrfs_device *device;
5795 struct btrfs_path *path = NULL;
5796 int i;
5797
5798 path = btrfs_alloc_path();
5799 if (!path) {
5800 ret = -ENOMEM;
5801 goto out;
5802 }
5803
5804 mutex_lock(&fs_devices->device_list_mutex);
5805 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5806 int item_size;
5807 struct btrfs_dev_stats_item *ptr;
5808
5809 key.objectid = 0;
5810 key.type = BTRFS_DEV_STATS_KEY;
5811 key.offset = device->devid;
5812 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
5813 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005814 __btrfs_reset_dev_stats(device);
5815 device->dev_stats_valid = 1;
5816 btrfs_release_path(path);
5817 continue;
5818 }
5819 slot = path->slots[0];
5820 eb = path->nodes[0];
5821 btrfs_item_key_to_cpu(eb, &found_key, slot);
5822 item_size = btrfs_item_size_nr(eb, slot);
5823
5824 ptr = btrfs_item_ptr(eb, slot,
5825 struct btrfs_dev_stats_item);
5826
5827 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5828 if (item_size >= (1 + i) * sizeof(__le64))
5829 btrfs_dev_stat_set(device, i,
5830 btrfs_dev_stats_value(eb, ptr, i));
5831 else
5832 btrfs_dev_stat_reset(device, i);
5833 }
5834
5835 device->dev_stats_valid = 1;
5836 btrfs_dev_stat_print_on_load(device);
5837 btrfs_release_path(path);
5838 }
5839 mutex_unlock(&fs_devices->device_list_mutex);
5840
5841out:
5842 btrfs_free_path(path);
5843 return ret < 0 ? ret : 0;
5844}
5845
5846static int update_dev_stat_item(struct btrfs_trans_handle *trans,
5847 struct btrfs_root *dev_root,
5848 struct btrfs_device *device)
5849{
5850 struct btrfs_path *path;
5851 struct btrfs_key key;
5852 struct extent_buffer *eb;
5853 struct btrfs_dev_stats_item *ptr;
5854 int ret;
5855 int i;
5856
5857 key.objectid = 0;
5858 key.type = BTRFS_DEV_STATS_KEY;
5859 key.offset = device->devid;
5860
5861 path = btrfs_alloc_path();
5862 BUG_ON(!path);
5863 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
5864 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005865 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
5866 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005867 goto out;
5868 }
5869
5870 if (ret == 0 &&
5871 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
5872 /* need to delete old one and insert a new one */
5873 ret = btrfs_del_item(trans, dev_root, path);
5874 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005875 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5876 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005877 goto out;
5878 }
5879 ret = 1;
5880 }
5881
5882 if (ret == 1) {
5883 /* need to insert a new item */
5884 btrfs_release_path(path);
5885 ret = btrfs_insert_empty_item(trans, dev_root, path,
5886 &key, sizeof(*ptr));
5887 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005888 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5889 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005890 goto out;
5891 }
5892 }
5893
5894 eb = path->nodes[0];
5895 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5896 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5897 btrfs_set_dev_stats_value(eb, ptr, i,
5898 btrfs_dev_stat_read(device, i));
5899 btrfs_mark_buffer_dirty(eb);
5900
5901out:
5902 btrfs_free_path(path);
5903 return ret;
5904}
5905
5906/*
5907 * called from commit_transaction. Writes all changed device stats to disk.
5908 */
5909int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5910 struct btrfs_fs_info *fs_info)
5911{
5912 struct btrfs_root *dev_root = fs_info->dev_root;
5913 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5914 struct btrfs_device *device;
5915 int ret = 0;
5916
5917 mutex_lock(&fs_devices->device_list_mutex);
5918 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5919 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5920 continue;
5921
5922 ret = update_dev_stat_item(trans, dev_root, device);
5923 if (!ret)
5924 device->dev_stats_dirty = 0;
5925 }
5926 mutex_unlock(&fs_devices->device_list_mutex);
5927
5928 return ret;
5929}
5930
Stefan Behrens442a4f62012-05-25 16:06:08 +02005931void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5932{
5933 btrfs_dev_stat_inc(dev, index);
5934 btrfs_dev_stat_print_on_error(dev);
5935}
5936
Eric Sandeen48a3b632013-04-25 20:41:01 +00005937static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02005938{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005939 if (!dev->dev_stats_valid)
5940 return;
Josef Bacik606686e2012-06-04 14:03:51 -04005941 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02005942 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04005943 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02005944 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5945 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5946 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5947 btrfs_dev_stat_read(dev,
5948 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5949 btrfs_dev_stat_read(dev,
5950 BTRFS_DEV_STAT_GENERATION_ERRS));
5951}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005952
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005953static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5954{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06005955 int i;
5956
5957 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5958 if (btrfs_dev_stat_read(dev, i) != 0)
5959 break;
5960 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5961 return; /* all values == 0, suppress message */
5962
Josef Bacik606686e2012-06-04 14:03:51 -04005963 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5964 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005965 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5966 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5967 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5968 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5969 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5970}
5971
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005972int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06005973 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005974{
5975 struct btrfs_device *dev;
5976 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5977 int i;
5978
5979 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005980 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005981 mutex_unlock(&fs_devices->device_list_mutex);
5982
5983 if (!dev) {
5984 printk(KERN_WARNING
5985 "btrfs: get dev_stats failed, device not found\n");
5986 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005987 } else if (!dev->dev_stats_valid) {
5988 printk(KERN_WARNING
5989 "btrfs: get dev_stats failed, not yet valid\n");
5990 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06005991 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005992 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5993 if (stats->nr_items > i)
5994 stats->values[i] =
5995 btrfs_dev_stat_read_and_reset(dev, i);
5996 else
5997 btrfs_dev_stat_reset(dev, i);
5998 }
5999 } else {
6000 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6001 if (stats->nr_items > i)
6002 stats->values[i] = btrfs_dev_stat_read(dev, i);
6003 }
6004 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6005 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6006 return 0;
6007}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006008
6009int btrfs_scratch_superblock(struct btrfs_device *device)
6010{
6011 struct buffer_head *bh;
6012 struct btrfs_super_block *disk_super;
6013
6014 bh = btrfs_read_dev_super(device->bdev);
6015 if (!bh)
6016 return -EINVAL;
6017 disk_super = (struct btrfs_super_block *)bh->b_data;
6018
6019 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6020 set_buffer_dirty(bh);
6021 sync_dirty_buffer(bh);
6022 brelse(bh);
6023
6024 return 0;
6025}