blob: 096b4a3dc5a9ad51777eaecbbbff5460ff68128e [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>
Stefan Behrens803b2f52013-08-15 17:11:21 +020029#include <linux/semaphore.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050030#include <asm/div64.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"
Anand Jain99994cd2014-06-03 11:36:00 +080043#include "sysfs.h"
Chris Mason0b86a832008-03-24 15:01:56 -040044
Yan Zheng2b820322008-11-17 21:11:30 -050045static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020049static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +000050static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020051static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050052
Chris Mason8a4b83c2008-03-24 15:02:07 -040053static DEFINE_MUTEX(uuid_mutex);
54static LIST_HEAD(fs_uuids);
55
Chris Mason7d9eb122008-07-08 14:19:17 -040056static void lock_chunks(struct btrfs_root *root)
57{
Chris Mason7d9eb122008-07-08 14:19:17 -040058 mutex_lock(&root->fs_info->chunk_mutex);
59}
60
61static void unlock_chunks(struct btrfs_root *root)
62{
Chris Mason7d9eb122008-07-08 14:19:17 -040063 mutex_unlock(&root->fs_info->chunk_mutex);
64}
65
Ilya Dryomov2208a372013-08-12 14:33:03 +030066static struct btrfs_fs_devices *__alloc_fs_devices(void)
67{
68 struct btrfs_fs_devices *fs_devs;
69
70 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
71 if (!fs_devs)
72 return ERR_PTR(-ENOMEM);
73
74 mutex_init(&fs_devs->device_list_mutex);
75
76 INIT_LIST_HEAD(&fs_devs->devices);
77 INIT_LIST_HEAD(&fs_devs->alloc_list);
78 INIT_LIST_HEAD(&fs_devs->list);
79
80 return fs_devs;
81}
82
83/**
84 * alloc_fs_devices - allocate struct btrfs_fs_devices
85 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
86 * generated.
87 *
88 * Return: a pointer to a new &struct btrfs_fs_devices on success;
89 * ERR_PTR() on error. Returned struct is not linked onto any lists and
90 * can be destroyed with kfree() right away.
91 */
92static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
93{
94 struct btrfs_fs_devices *fs_devs;
95
96 fs_devs = __alloc_fs_devices();
97 if (IS_ERR(fs_devs))
98 return fs_devs;
99
100 if (fsid)
101 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
102 else
103 generate_random_uuid(fs_devs->fsid);
104
105 return fs_devs;
106}
107
Yan Zhenge4404d62008-12-12 10:03:26 -0500108static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
109{
110 struct btrfs_device *device;
111 WARN_ON(fs_devices->opened);
112 while (!list_empty(&fs_devices->devices)) {
113 device = list_entry(fs_devices->devices.next,
114 struct btrfs_device, dev_list);
115 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400116 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500117 kfree(device);
118 }
119 kfree(fs_devices);
120}
121
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000122static void btrfs_kobject_uevent(struct block_device *bdev,
123 enum kobject_action action)
124{
125 int ret;
126
127 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
128 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500129 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000130 action,
131 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
132 &disk_to_dev(bdev->bd_disk)->kobj);
133}
134
Jeff Mahoney143bede2012-03-01 14:56:26 +0100135void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400136{
137 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400138
Yan Zheng2b820322008-11-17 21:11:30 -0500139 while (!list_empty(&fs_uuids)) {
140 fs_devices = list_entry(fs_uuids.next,
141 struct btrfs_fs_devices, list);
142 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500143 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400144 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400145}
146
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300147static struct btrfs_device *__alloc_device(void)
148{
149 struct btrfs_device *dev;
150
151 dev = kzalloc(sizeof(*dev), GFP_NOFS);
152 if (!dev)
153 return ERR_PTR(-ENOMEM);
154
155 INIT_LIST_HEAD(&dev->dev_list);
156 INIT_LIST_HEAD(&dev->dev_alloc_list);
157
158 spin_lock_init(&dev->io_lock);
159
160 spin_lock_init(&dev->reada_lock);
161 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800162 atomic_set(&dev->dev_stats_ccnt, 0);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300163 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
164 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
165
166 return dev;
167}
168
Chris Masona1b32a52008-09-05 16:09:51 -0400169static noinline struct btrfs_device *__find_device(struct list_head *head,
170 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400171{
172 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400173
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500174 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400175 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400176 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400177 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400178 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400179 }
180 return NULL;
181}
182
Chris Masona1b32a52008-09-05 16:09:51 -0400183static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400184{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400185 struct btrfs_fs_devices *fs_devices;
186
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500187 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400188 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
189 return fs_devices;
190 }
191 return NULL;
192}
193
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100194static int
195btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
196 int flush, struct block_device **bdev,
197 struct buffer_head **bh)
198{
199 int ret;
200
201 *bdev = blkdev_get_by_path(device_path, flags, holder);
202
203 if (IS_ERR(*bdev)) {
204 ret = PTR_ERR(*bdev);
Frank Holtonefe120a2013-12-20 11:37:06 -0500205 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100206 goto error;
207 }
208
209 if (flush)
210 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
211 ret = set_blocksize(*bdev, 4096);
212 if (ret) {
213 blkdev_put(*bdev, flags);
214 goto error;
215 }
216 invalidate_bdev(*bdev);
217 *bh = btrfs_read_dev_super(*bdev);
218 if (!*bh) {
219 ret = -EINVAL;
220 blkdev_put(*bdev, flags);
221 goto error;
222 }
223
224 return 0;
225
226error:
227 *bdev = NULL;
228 *bh = NULL;
229 return ret;
230}
231
Chris Masonffbd5172009-04-20 15:50:09 -0400232static void requeue_list(struct btrfs_pending_bios *pending_bios,
233 struct bio *head, struct bio *tail)
234{
235
236 struct bio *old_head;
237
238 old_head = pending_bios->head;
239 pending_bios->head = head;
240 if (pending_bios->tail)
241 tail->bi_next = old_head;
242 else
243 pending_bios->tail = tail;
244}
245
Chris Mason8b712842008-06-11 16:50:36 -0400246/*
247 * we try to collect pending bios for a device so we don't get a large
248 * number of procs sending bios down to the same device. This greatly
249 * improves the schedulers ability to collect and merge the bios.
250 *
251 * But, it also turns into a long list of bios to process and that is sure
252 * to eventually make the worker thread block. The solution here is to
253 * make some progress and then put this work struct back at the end of
254 * the list if the block device is congested. This way, multiple devices
255 * can make progress from a single worker thread.
256 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100257static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400258{
259 struct bio *pending;
260 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400261 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400262 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400263 struct bio *tail;
264 struct bio *cur;
265 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400266 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400267 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400268 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400269 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400270 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000271 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400272 struct blk_plug plug;
273
274 /*
275 * this function runs all the bios we've collected for
276 * a particular device. We don't want to wander off to
277 * another device without first sending all of these down.
278 * So, setup a plug here and finish it off before we return
279 */
280 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400281
Chris Masonbedf7622009-04-03 10:32:58 -0400282 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400283 fs_info = device->dev_root->fs_info;
284 limit = btrfs_async_submit_limit(fs_info);
285 limit = limit * 2 / 3;
286
Chris Mason8b712842008-06-11 16:50:36 -0400287loop:
288 spin_lock(&device->io_lock);
289
Chris Masona6837052009-02-04 09:19:41 -0500290loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400291 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400292
Chris Mason8b712842008-06-11 16:50:36 -0400293 /* take all the bios off the list at once and process them
294 * later on (without the lock held). But, remember the
295 * tail and other pointers so the bios can be properly reinserted
296 * into the list if we hit congestion
297 */
Chris Masond84275c2009-06-09 15:39:08 -0400298 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400299 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400300 force_reg = 1;
301 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400302 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400303 force_reg = 0;
304 }
Chris Masonffbd5172009-04-20 15:50:09 -0400305
306 pending = pending_bios->head;
307 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400308 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400309
310 /*
311 * if pending was null this time around, no bios need processing
312 * at all and we can stop. Otherwise it'll loop back up again
313 * and do an additional check so no bios are missed.
314 *
315 * device->running_pending is used to synchronize with the
316 * schedule_bio code.
317 */
Chris Masonffbd5172009-04-20 15:50:09 -0400318 if (device->pending_sync_bios.head == NULL &&
319 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400320 again = 0;
321 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400322 } else {
323 again = 1;
324 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400325 }
Chris Masonffbd5172009-04-20 15:50:09 -0400326
327 pending_bios->head = NULL;
328 pending_bios->tail = NULL;
329
Chris Mason8b712842008-06-11 16:50:36 -0400330 spin_unlock(&device->io_lock);
331
Chris Masond3977122009-01-05 21:25:51 -0500332 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400333
334 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400335 /* we want to work on both lists, but do more bios on the
336 * sync list than the regular list
337 */
338 if ((num_run > 32 &&
339 pending_bios != &device->pending_sync_bios &&
340 device->pending_sync_bios.head) ||
341 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
342 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400343 spin_lock(&device->io_lock);
344 requeue_list(pending_bios, pending, tail);
345 goto loop_lock;
346 }
347
Chris Mason8b712842008-06-11 16:50:36 -0400348 cur = pending;
349 pending = pending->bi_next;
350 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400351
Josef Bacik66657b32012-08-01 15:36:24 -0400352 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400353 waitqueue_active(&fs_info->async_submit_wait))
354 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400355
356 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400357
Chris Mason2ab1ba62011-08-04 14:28:36 -0400358 /*
359 * if we're doing the sync list, record that our
360 * plug has some sync requests on it
361 *
362 * If we're doing the regular list and there are
363 * sync requests sitting around, unplug before
364 * we add more
365 */
366 if (pending_bios == &device->pending_sync_bios) {
367 sync_pending = 1;
368 } else if (sync_pending) {
369 blk_finish_plug(&plug);
370 blk_start_plug(&plug);
371 sync_pending = 0;
372 }
373
Stefan Behrens21adbd52011-11-09 13:44:05 +0100374 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400375 num_run++;
376 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100377 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400378 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400379
380 /*
381 * we made progress, there is more work to do and the bdi
382 * is now congested. Back off and let other work structs
383 * run instead
384 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400385 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500386 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400387 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400388
Chris Masonb765ead2009-04-03 10:27:10 -0400389 ioc = current->io_context;
390
391 /*
392 * the main goal here is that we don't want to
393 * block if we're going to be able to submit
394 * more requests without blocking.
395 *
396 * This code does two great things, it pokes into
397 * the elevator code from a filesystem _and_
398 * it makes assumptions about how batching works.
399 */
400 if (ioc && ioc->nr_batch_requests > 0 &&
401 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
402 (last_waited == 0 ||
403 ioc->last_waited == last_waited)) {
404 /*
405 * we want to go through our batch of
406 * requests and stop. So, we copy out
407 * the ioc->last_waited time and test
408 * against it before looping
409 */
410 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100411 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400412 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400413 continue;
414 }
Chris Mason8b712842008-06-11 16:50:36 -0400415 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400416 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500417 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400418
419 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800420 btrfs_queue_work(fs_info->submit_workers,
421 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400422 goto done;
423 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500424 /* unplug every 64 requests just for good measure */
425 if (batch_run % 64 == 0) {
426 blk_finish_plug(&plug);
427 blk_start_plug(&plug);
428 sync_pending = 0;
429 }
Chris Mason8b712842008-06-11 16:50:36 -0400430 }
Chris Masonffbd5172009-04-20 15:50:09 -0400431
Chris Mason51684082010-03-10 15:33:32 -0500432 cond_resched();
433 if (again)
434 goto loop;
435
436 spin_lock(&device->io_lock);
437 if (device->pending_bios.head || device->pending_sync_bios.head)
438 goto loop_lock;
439 spin_unlock(&device->io_lock);
440
Chris Mason8b712842008-06-11 16:50:36 -0400441done:
Chris Mason211588a2011-04-19 20:12:40 -0400442 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400443}
444
Christoph Hellwigb2950862008-12-02 09:54:17 -0500445static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400446{
447 struct btrfs_device *device;
448
449 device = container_of(work, struct btrfs_device, work);
450 run_scheduled_bios(device);
451}
452
David Sterba60999ca2014-03-26 18:26:36 +0100453/*
454 * Add new device to list of registered devices
455 *
456 * Returns:
457 * 1 - first time device is seen
458 * 0 - device already known
459 * < 0 - error
460 */
Chris Masona1b32a52008-09-05 16:09:51 -0400461static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400462 struct btrfs_super_block *disk_super,
463 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
464{
465 struct btrfs_device *device;
466 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400467 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100468 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400469 u64 found_transid = btrfs_super_generation(disk_super);
470
471 fs_devices = find_fsid(disk_super->fsid);
472 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300473 fs_devices = alloc_fs_devices(disk_super->fsid);
474 if (IS_ERR(fs_devices))
475 return PTR_ERR(fs_devices);
476
Chris Mason8a4b83c2008-03-24 15:02:07 -0400477 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300478
Chris Mason8a4b83c2008-03-24 15:02:07 -0400479 device = NULL;
480 } else {
Chris Masona4437552008-04-18 10:29:38 -0400481 device = __find_device(&fs_devices->devices, devid,
482 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400483 }
Miao Xie443f24f2014-07-24 11:37:15 +0800484
Chris Mason8a4b83c2008-03-24 15:02:07 -0400485 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500486 if (fs_devices->opened)
487 return -EBUSY;
488
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300489 device = btrfs_alloc_device(NULL, &devid,
490 disk_super->dev_item.uuid);
491 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400492 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300493 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400494 }
Josef Bacik606686e2012-06-04 14:03:51 -0400495
496 name = rcu_string_strdup(path, GFP_NOFS);
497 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400498 kfree(device);
499 return -ENOMEM;
500 }
Josef Bacik606686e2012-06-04 14:03:51 -0400501 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200502
Chris Masone5e9a522009-06-10 15:17:02 -0400503 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000504 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100505 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400506 mutex_unlock(&fs_devices->device_list_mutex);
507
David Sterba60999ca2014-03-26 18:26:36 +0100508 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500509 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400510 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800511 /*
512 * When FS is already mounted.
513 * 1. If you are here and if the device->name is NULL that
514 * means this device was missing at time of FS mount.
515 * 2. If you are here and if the device->name is different
516 * from 'path' that means either
517 * a. The same device disappeared and reappeared with
518 * different name. or
519 * b. The missing-disk-which-was-replaced, has
520 * reappeared now.
521 *
522 * We must allow 1 and 2a above. But 2b would be a spurious
523 * and unintentional.
524 *
525 * Further in case of 1 and 2a above, the disk at 'path'
526 * would have missed some transaction when it was away and
527 * in case of 2a the stale bdev has to be updated as well.
528 * 2b must not be allowed at all time.
529 */
530
531 /*
532 * As of now don't allow update to btrfs_fs_device through
533 * the btrfs dev scan cli, after FS has been mounted.
534 */
Anand Jain77bdae42014-07-03 18:22:06 +0800535 if (fs_devices->opened) {
Anand Jainb96de002014-07-03 18:22:05 +0800536 return -EBUSY;
Anand Jain77bdae42014-07-03 18:22:06 +0800537 } else {
538 /*
539 * That is if the FS is _not_ mounted and if you
540 * are here, that means there is more than one
541 * disk with same uuid and devid.We keep the one
542 * with larger generation number or the last-in if
543 * generation are equal.
544 */
545 if (found_transid < device->generation)
546 return -EEXIST;
547 }
Anand Jainb96de002014-07-03 18:22:05 +0800548
Josef Bacik606686e2012-06-04 14:03:51 -0400549 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000550 if (!name)
551 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400552 rcu_string_free(device->name);
553 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500554 if (device->missing) {
555 fs_devices->missing_devices--;
556 device->missing = 0;
557 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400558 }
559
Anand Jain77bdae42014-07-03 18:22:06 +0800560 /*
561 * Unmount does not free the btrfs_device struct but would zero
562 * generation along with most of the other members. So just update
563 * it back. We need it to pick the disk with largest generation
564 * (as above).
565 */
566 if (!fs_devices->opened)
567 device->generation = found_transid;
568
Chris Mason8a4b83c2008-03-24 15:02:07 -0400569 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100570
571 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400572}
573
Yan Zhenge4404d62008-12-12 10:03:26 -0500574static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
575{
576 struct btrfs_fs_devices *fs_devices;
577 struct btrfs_device *device;
578 struct btrfs_device *orig_dev;
579
Ilya Dryomov2208a372013-08-12 14:33:03 +0300580 fs_devices = alloc_fs_devices(orig->fsid);
581 if (IS_ERR(fs_devices))
582 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500583
Josef Bacik02db0842012-06-21 16:03:58 -0400584 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500585
Xiao Guangrong46224702011-04-20 10:08:47 +0000586 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500587 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400588 struct rcu_string *name;
589
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300590 device = btrfs_alloc_device(NULL, &orig_dev->devid,
591 orig_dev->uuid);
592 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500593 goto error;
594
Josef Bacik606686e2012-06-04 14:03:51 -0400595 /*
596 * This is ok to do without rcu read locked because we hold the
597 * uuid mutex so nothing we touch in here is going to disappear.
598 */
Anand Jaine755f782014-06-30 17:12:47 +0800599 if (orig_dev->name) {
600 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
601 if (!name) {
602 kfree(device);
603 goto error;
604 }
605 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400606 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500607
Yan Zhenge4404d62008-12-12 10:03:26 -0500608 list_add(&device->dev_list, &fs_devices->devices);
609 device->fs_devices = fs_devices;
610 fs_devices->num_devices++;
611 }
612 return fs_devices;
613error:
614 free_fs_devices(fs_devices);
615 return ERR_PTR(-ENOMEM);
616}
617
Stefan Behrens8dabb742012-11-06 13:15:27 +0100618void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
619 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400620{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500621 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800622 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500623
Chris Masondfe25022008-05-13 13:46:40 -0400624 mutex_lock(&uuid_mutex);
625again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000626 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500627 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500628 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100629 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800630 (!latest_dev ||
631 device->generation > latest_dev->generation)) {
632 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500633 }
Yan Zheng2b820322008-11-17 21:11:30 -0500634 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500635 }
Yan Zheng2b820322008-11-17 21:11:30 -0500636
Stefan Behrens8dabb742012-11-06 13:15:27 +0100637 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
638 /*
639 * In the first step, keep the device which has
640 * the correct fsid and the devid that is used
641 * for the dev_replace procedure.
642 * In the second step, the dev_replace state is
643 * read from the device tree and it is known
644 * whether the procedure is really active or
645 * not, which means whether this device is
646 * used or whether it should be removed.
647 */
648 if (step == 0 || device->is_tgtdev_for_dev_replace) {
649 continue;
650 }
651 }
Yan Zheng2b820322008-11-17 21:11:30 -0500652 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100653 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500654 device->bdev = NULL;
655 fs_devices->open_devices--;
656 }
657 if (device->writeable) {
658 list_del_init(&device->dev_alloc_list);
659 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100660 if (!device->is_tgtdev_for_dev_replace)
661 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500662 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500663 list_del_init(&device->dev_list);
664 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400665 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500666 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400667 }
Yan Zheng2b820322008-11-17 21:11:30 -0500668
669 if (fs_devices->seed) {
670 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500671 goto again;
672 }
673
Miao Xie443f24f2014-07-24 11:37:15 +0800674 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500675
Chris Masondfe25022008-05-13 13:46:40 -0400676 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400677}
Chris Masona0af4692008-05-13 16:03:06 -0400678
Xiao Guangrong1f781602011-04-20 10:09:16 +0000679static void __free_device(struct work_struct *work)
680{
681 struct btrfs_device *device;
682
683 device = container_of(work, struct btrfs_device, rcu_work);
684
685 if (device->bdev)
686 blkdev_put(device->bdev, device->mode);
687
Josef Bacik606686e2012-06-04 14:03:51 -0400688 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000689 kfree(device);
690}
691
692static void free_device(struct rcu_head *head)
693{
694 struct btrfs_device *device;
695
696 device = container_of(head, struct btrfs_device, rcu);
697
698 INIT_WORK(&device->rcu_work, __free_device);
699 schedule_work(&device->rcu_work);
700}
701
Yan Zheng2b820322008-11-17 21:11:30 -0500702static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400703{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400704 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500705
Yan Zheng2b820322008-11-17 21:11:30 -0500706 if (--fs_devices->opened > 0)
707 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400708
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000709 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500710 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000711 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400712 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000713
714 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400715 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000716
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300717 if (device->writeable &&
718 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500719 list_del_init(&device->dev_alloc_list);
720 fs_devices->rw_devices--;
721 }
722
Josef Bacikd5e20032011-08-04 14:52:27 +0000723 if (device->can_discard)
724 fs_devices->num_can_discard--;
Josef Bacik726551e2013-08-26 13:45:53 -0400725 if (device->missing)
726 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000727
Ilya Dryomova1e87802013-08-12 14:33:04 +0300728 new_device = btrfs_alloc_device(NULL, &device->devid,
729 device->uuid);
730 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400731
732 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400733 if (device->name) {
734 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300735 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400736 rcu_assign_pointer(new_device->name, name);
737 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300738
Xiao Guangrong1f781602011-04-20 10:09:16 +0000739 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300740 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000741
742 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400743 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000744 mutex_unlock(&fs_devices->device_list_mutex);
745
Yan Zhenge4404d62008-12-12 10:03:26 -0500746 WARN_ON(fs_devices->open_devices);
747 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500748 fs_devices->opened = 0;
749 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500750
Chris Mason8a4b83c2008-03-24 15:02:07 -0400751 return 0;
752}
753
Yan Zheng2b820322008-11-17 21:11:30 -0500754int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
755{
Yan Zhenge4404d62008-12-12 10:03:26 -0500756 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500757 int ret;
758
759 mutex_lock(&uuid_mutex);
760 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500761 if (!fs_devices->opened) {
762 seed_devices = fs_devices->seed;
763 fs_devices->seed = NULL;
764 }
Yan Zheng2b820322008-11-17 21:11:30 -0500765 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500766
767 while (seed_devices) {
768 fs_devices = seed_devices;
769 seed_devices = fs_devices->seed;
770 __btrfs_close_devices(fs_devices);
771 free_fs_devices(fs_devices);
772 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000773 /*
774 * Wait for rcu kworkers under __btrfs_close_devices
775 * to finish all blkdev_puts so device is really
776 * free when umount is done.
777 */
778 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500779 return ret;
780}
781
Yan Zhenge4404d62008-12-12 10:03:26 -0500782static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
783 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400784{
Josef Bacikd5e20032011-08-04 14:52:27 +0000785 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400786 struct block_device *bdev;
787 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400788 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800789 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400790 struct buffer_head *bh;
791 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400792 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500793 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400794 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400795
Tejun Heod4d77622010-11-13 11:55:18 +0100796 flags |= FMODE_EXCL;
797
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500798 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400799 if (device->bdev)
800 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400801 if (!device->name)
802 continue;
803
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000804 /* Just open everything we can; ignore failures here */
805 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
806 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100807 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400808
809 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000810 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400811 if (devid != device->devid)
812 goto error_brelse;
813
Yan Zheng2b820322008-11-17 21:11:30 -0500814 if (memcmp(device->uuid, disk_super->dev_item.uuid,
815 BTRFS_UUID_SIZE))
816 goto error_brelse;
817
818 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800819 if (!latest_dev ||
820 device->generation > latest_dev->generation)
821 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400822
Yan Zheng2b820322008-11-17 21:11:30 -0500823 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
824 device->writeable = 0;
825 } else {
826 device->writeable = !bdev_read_only(bdev);
827 seeding = 0;
828 }
829
Josef Bacikd5e20032011-08-04 14:52:27 +0000830 q = bdev_get_queue(bdev);
831 if (blk_queue_discard(q)) {
832 device->can_discard = 1;
833 fs_devices->num_can_discard++;
834 }
835
Chris Mason8a4b83c2008-03-24 15:02:07 -0400836 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400837 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500838 device->mode = flags;
839
Chris Masonc2898112009-06-10 09:51:32 -0400840 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
841 fs_devices->rotating = 1;
842
Chris Masona0af4692008-05-13 16:03:06 -0400843 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300844 if (device->writeable &&
845 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500846 fs_devices->rw_devices++;
847 list_add(&device->dev_alloc_list,
848 &fs_devices->alloc_list);
849 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000850 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400851 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400852
Chris Masona0af4692008-05-13 16:03:06 -0400853error_brelse:
854 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100855 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400856 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400857 }
Chris Masona0af4692008-05-13 16:03:06 -0400858 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300859 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400860 goto out;
861 }
Yan Zheng2b820322008-11-17 21:11:30 -0500862 fs_devices->seeding = seeding;
863 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800864 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500865 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400866out:
Yan Zheng2b820322008-11-17 21:11:30 -0500867 return ret;
868}
869
870int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500871 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500872{
873 int ret;
874
875 mutex_lock(&uuid_mutex);
876 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500877 fs_devices->opened++;
878 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500879 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500880 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500881 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400882 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400883 return ret;
884}
885
David Sterba6f60cbd2013-02-15 11:31:02 -0700886/*
887 * Look for a btrfs signature on a device. This may be called out of the mount path
888 * and we are not allowed to call set_blocksize during the scan. The superblock
889 * is read via pagecache
890 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500891int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400892 struct btrfs_fs_devices **fs_devices_ret)
893{
894 struct btrfs_super_block *disk_super;
895 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700896 struct page *page;
897 void *p;
898 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400899 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400900 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400901 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700902 u64 bytenr;
903 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400904
David Sterba6f60cbd2013-02-15 11:31:02 -0700905 /*
906 * we would like to check all the supers, but that would make
907 * a btrfs mount succeed after a mkfs from a different FS.
908 * So, we need to add a special mount option to scan for
909 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
910 */
911 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100912 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500913 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700914
915 bdev = blkdev_get_by_path(path, flags, holder);
916
917 if (IS_ERR(bdev)) {
918 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100919 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700920 }
921
922 /* make sure our super fits in the device */
923 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
924 goto error_bdev_put;
925
926 /* make sure our super fits in the page */
927 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
928 goto error_bdev_put;
929
930 /* make sure our super doesn't straddle pages on disk */
931 index = bytenr >> PAGE_CACHE_SHIFT;
932 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
933 goto error_bdev_put;
934
935 /* pull in the page with our super */
936 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
937 index, GFP_NOFS);
938
939 if (IS_ERR_OR_NULL(page))
940 goto error_bdev_put;
941
942 p = kmap(page);
943
944 /* align our pointer to the offset of the super block */
945 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
946
947 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800948 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700949 goto error_unmap;
950
Xiao Guangronga3438322010-01-06 11:48:18 +0000951 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400952 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400953 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700954
Chris Mason8a4b83c2008-03-24 15:02:07 -0400955 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100956 if (ret > 0) {
957 if (disk_super->label[0]) {
958 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
959 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
960 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
961 } else {
962 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
963 }
964
965 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
966 ret = 0;
967 }
Josef Bacik02db0842012-06-21 16:03:58 -0400968 if (!ret && fs_devices_ret)
969 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700970
971error_unmap:
972 kunmap(page);
973 page_cache_release(page);
974
975error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100976 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400977error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100978 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400979 return ret;
980}
Chris Mason0b86a832008-03-24 15:01:56 -0400981
Miao Xie6d07bce2011-01-05 10:07:31 +0000982/* helper to account the used device space in the range */
983int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
984 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400985{
986 struct btrfs_key key;
987 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000988 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500989 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000990 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400991 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000992 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400993 struct extent_buffer *l;
994
Miao Xie6d07bce2011-01-05 10:07:31 +0000995 *length = 0;
996
Stefan Behrens63a212a2012-11-05 18:29:28 +0100997 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000998 return 0;
999
Yan Zheng2b820322008-11-17 21:11:30 -05001000 path = btrfs_alloc_path();
1001 if (!path)
1002 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001003 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001004
Chris Mason0b86a832008-03-24 15:01:56 -04001005 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001006 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001007 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001008
1009 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001010 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001011 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001012 if (ret > 0) {
1013 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1014 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001015 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001016 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001017
Chris Mason0b86a832008-03-24 15:01:56 -04001018 while (1) {
1019 l = path->nodes[0];
1020 slot = path->slots[0];
1021 if (slot >= btrfs_header_nritems(l)) {
1022 ret = btrfs_next_leaf(root, path);
1023 if (ret == 0)
1024 continue;
1025 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001026 goto out;
1027
1028 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001029 }
1030 btrfs_item_key_to_cpu(l, &key, slot);
1031
1032 if (key.objectid < device->devid)
1033 goto next;
1034
1035 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001036 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001037
David Sterba962a2982014-06-04 18:41:45 +02001038 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001039 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001040
Chris Mason0b86a832008-03-24 15:01:56 -04001041 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001042 extent_end = key.offset + btrfs_dev_extent_length(l,
1043 dev_extent);
1044 if (key.offset <= start && extent_end > end) {
1045 *length = end - start + 1;
1046 break;
1047 } else if (key.offset <= start && extent_end > start)
1048 *length += extent_end - start;
1049 else if (key.offset > start && extent_end <= end)
1050 *length += extent_end - key.offset;
1051 else if (key.offset > start && key.offset <= end) {
1052 *length += end - key.offset + 1;
1053 break;
1054 } else if (key.offset > end)
1055 break;
1056
1057next:
1058 path->slots[0]++;
1059 }
1060 ret = 0;
1061out:
1062 btrfs_free_path(path);
1063 return ret;
1064}
1065
Josef Bacik6df9a952013-06-27 13:22:46 -04001066static int contains_pending_extent(struct btrfs_trans_handle *trans,
1067 struct btrfs_device *device,
1068 u64 *start, u64 len)
1069{
1070 struct extent_map *em;
1071 int ret = 0;
1072
1073 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
1074 struct map_lookup *map;
1075 int i;
1076
1077 map = (struct map_lookup *)em->bdev;
1078 for (i = 0; i < map->num_stripes; i++) {
1079 if (map->stripes[i].dev != device)
1080 continue;
1081 if (map->stripes[i].physical >= *start + len ||
1082 map->stripes[i].physical + em->orig_block_len <=
1083 *start)
1084 continue;
1085 *start = map->stripes[i].physical +
1086 em->orig_block_len;
1087 ret = 1;
1088 }
1089 }
1090
1091 return ret;
1092}
1093
1094
Chris Mason0b86a832008-03-24 15:01:56 -04001095/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001096 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001097 * @device: the device which we search the free space in
1098 * @num_bytes: the size of the free space that we need
1099 * @start: store the start of the free space.
1100 * @len: the size of the free space. that we find, or the size of the max
1101 * free space if we don't find suitable free space
1102 *
Chris Mason0b86a832008-03-24 15:01:56 -04001103 * this uses a pretty simple search, the expectation is that it is
1104 * called very infrequently and that a given device has a small number
1105 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001106 *
1107 * @start is used to store the start of the free space if we find. But if we
1108 * don't find suitable free space, it will be used to store the start position
1109 * of the max free space.
1110 *
1111 * @len is used to store the size of the free space that we find.
1112 * But if we don't find suitable free space, it is used to store the size of
1113 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001114 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001115int find_free_dev_extent(struct btrfs_trans_handle *trans,
1116 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001117 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001118{
1119 struct btrfs_key key;
1120 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001121 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001122 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001123 u64 hole_size;
1124 u64 max_hole_start;
1125 u64 max_hole_size;
1126 u64 extent_end;
1127 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001128 u64 search_end = device->total_bytes;
1129 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001130 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001131 struct extent_buffer *l;
1132
Chris Mason0b86a832008-03-24 15:01:56 -04001133 /* FIXME use last free of some kind */
1134
1135 /* we don't want to overwrite the superblock on the drive,
1136 * so we make sure to start at an offset of at least 1MB
1137 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001138 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001139
Josef Bacik6df9a952013-06-27 13:22:46 -04001140 path = btrfs_alloc_path();
1141 if (!path)
1142 return -ENOMEM;
1143again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001144 max_hole_start = search_start;
1145 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001146 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001147
Stefan Behrens63a212a2012-11-05 18:29:28 +01001148 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001149 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001150 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001151 }
1152
Miao Xie7bfc8372011-01-05 10:07:26 +00001153 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001154 path->search_commit_root = 1;
1155 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001156
Chris Mason0b86a832008-03-24 15:01:56 -04001157 key.objectid = device->devid;
1158 key.offset = search_start;
1159 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001160
Li Zefan125ccb02011-12-08 15:07:24 +08001161 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001162 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001163 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001164 if (ret > 0) {
1165 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1166 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001167 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001168 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001169
Chris Mason0b86a832008-03-24 15:01:56 -04001170 while (1) {
1171 l = path->nodes[0];
1172 slot = path->slots[0];
1173 if (slot >= btrfs_header_nritems(l)) {
1174 ret = btrfs_next_leaf(root, path);
1175 if (ret == 0)
1176 continue;
1177 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001178 goto out;
1179
1180 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001181 }
1182 btrfs_item_key_to_cpu(l, &key, slot);
1183
1184 if (key.objectid < device->devid)
1185 goto next;
1186
1187 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001188 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001189
David Sterba962a2982014-06-04 18:41:45 +02001190 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001191 goto next;
1192
Miao Xie7bfc8372011-01-05 10:07:26 +00001193 if (key.offset > search_start) {
1194 hole_size = key.offset - search_start;
1195
Josef Bacik6df9a952013-06-27 13:22:46 -04001196 /*
1197 * Have to check before we set max_hole_start, otherwise
1198 * we could end up sending back this offset anyway.
1199 */
1200 if (contains_pending_extent(trans, device,
1201 &search_start,
1202 hole_size))
1203 hole_size = 0;
1204
Miao Xie7bfc8372011-01-05 10:07:26 +00001205 if (hole_size > max_hole_size) {
1206 max_hole_start = search_start;
1207 max_hole_size = hole_size;
1208 }
1209
1210 /*
1211 * If this free space is greater than which we need,
1212 * it must be the max free space that we have found
1213 * until now, so max_hole_start must point to the start
1214 * of this free space and the length of this free space
1215 * is stored in max_hole_size. Thus, we return
1216 * max_hole_start and max_hole_size and go back to the
1217 * caller.
1218 */
1219 if (hole_size >= num_bytes) {
1220 ret = 0;
1221 goto out;
1222 }
1223 }
1224
Chris Mason0b86a832008-03-24 15:01:56 -04001225 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001226 extent_end = key.offset + btrfs_dev_extent_length(l,
1227 dev_extent);
1228 if (extent_end > search_start)
1229 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001230next:
1231 path->slots[0]++;
1232 cond_resched();
1233 }
Chris Mason0b86a832008-03-24 15:01:56 -04001234
liubo38c01b92011-08-02 02:39:03 +00001235 /*
1236 * At this point, search_start should be the end of
1237 * allocated dev extents, and when shrinking the device,
1238 * search_end may be smaller than search_start.
1239 */
1240 if (search_end > search_start)
1241 hole_size = search_end - search_start;
1242
Miao Xie7bfc8372011-01-05 10:07:26 +00001243 if (hole_size > max_hole_size) {
1244 max_hole_start = search_start;
1245 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001246 }
Chris Mason0b86a832008-03-24 15:01:56 -04001247
Josef Bacik6df9a952013-06-27 13:22:46 -04001248 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1249 btrfs_release_path(path);
1250 goto again;
1251 }
1252
Miao Xie7bfc8372011-01-05 10:07:26 +00001253 /* See above. */
1254 if (hole_size < num_bytes)
1255 ret = -ENOSPC;
1256 else
1257 ret = 0;
1258
1259out:
Yan Zheng2b820322008-11-17 21:11:30 -05001260 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001261 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001262 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001263 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001264 return ret;
1265}
1266
Christoph Hellwigb2950862008-12-02 09:54:17 -05001267static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001268 struct btrfs_device *device,
1269 u64 start)
1270{
1271 int ret;
1272 struct btrfs_path *path;
1273 struct btrfs_root *root = device->dev_root;
1274 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001275 struct btrfs_key found_key;
1276 struct extent_buffer *leaf = NULL;
1277 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001278
1279 path = btrfs_alloc_path();
1280 if (!path)
1281 return -ENOMEM;
1282
1283 key.objectid = device->devid;
1284 key.offset = start;
1285 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001286again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001287 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001288 if (ret > 0) {
1289 ret = btrfs_previous_item(root, path, key.objectid,
1290 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001291 if (ret)
1292 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001293 leaf = path->nodes[0];
1294 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1295 extent = btrfs_item_ptr(leaf, path->slots[0],
1296 struct btrfs_dev_extent);
1297 BUG_ON(found_key.offset > start || found_key.offset +
1298 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001299 key = found_key;
1300 btrfs_release_path(path);
1301 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001302 } else if (ret == 0) {
1303 leaf = path->nodes[0];
1304 extent = btrfs_item_ptr(leaf, path->slots[0],
1305 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001306 } else {
1307 btrfs_error(root->fs_info, ret, "Slot search failed");
1308 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001309 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001310
Josef Bacik2bf64752011-09-26 17:12:22 -04001311 if (device->bytes_used > 0) {
1312 u64 len = btrfs_dev_extent_length(leaf, extent);
1313 device->bytes_used -= len;
1314 spin_lock(&root->fs_info->free_chunk_lock);
1315 root->fs_info->free_chunk_space += len;
1316 spin_unlock(&root->fs_info->free_chunk_lock);
1317 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001318 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001319 if (ret) {
1320 btrfs_error(root->fs_info, ret,
1321 "Failed to remove dev extent item");
1322 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001323out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001324 btrfs_free_path(path);
1325 return ret;
1326}
1327
Eric Sandeen48a3b632013-04-25 20:41:01 +00001328static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1329 struct btrfs_device *device,
1330 u64 chunk_tree, u64 chunk_objectid,
1331 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001332{
1333 int ret;
1334 struct btrfs_path *path;
1335 struct btrfs_root *root = device->dev_root;
1336 struct btrfs_dev_extent *extent;
1337 struct extent_buffer *leaf;
1338 struct btrfs_key key;
1339
Chris Masondfe25022008-05-13 13:46:40 -04001340 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001341 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001342 path = btrfs_alloc_path();
1343 if (!path)
1344 return -ENOMEM;
1345
Chris Mason0b86a832008-03-24 15:01:56 -04001346 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001347 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001348 key.type = BTRFS_DEV_EXTENT_KEY;
1349 ret = btrfs_insert_empty_item(trans, root, path, &key,
1350 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001351 if (ret)
1352 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001353
1354 leaf = path->nodes[0];
1355 extent = btrfs_item_ptr(leaf, path->slots[0],
1356 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001357 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1358 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1359 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1360
1361 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001362 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001363
Chris Mason0b86a832008-03-24 15:01:56 -04001364 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1365 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001366out:
Chris Mason0b86a832008-03-24 15:01:56 -04001367 btrfs_free_path(path);
1368 return ret;
1369}
1370
Josef Bacik6df9a952013-06-27 13:22:46 -04001371static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001372{
Josef Bacik6df9a952013-06-27 13:22:46 -04001373 struct extent_map_tree *em_tree;
1374 struct extent_map *em;
1375 struct rb_node *n;
1376 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001377
Josef Bacik6df9a952013-06-27 13:22:46 -04001378 em_tree = &fs_info->mapping_tree.map_tree;
1379 read_lock(&em_tree->lock);
1380 n = rb_last(&em_tree->map);
1381 if (n) {
1382 em = rb_entry(n, struct extent_map, rb_node);
1383 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001384 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001385 read_unlock(&em_tree->lock);
1386
Chris Mason0b86a832008-03-24 15:01:56 -04001387 return ret;
1388}
1389
Ilya Dryomov53f10652013-08-12 14:33:01 +03001390static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1391 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001392{
1393 int ret;
1394 struct btrfs_key key;
1395 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001396 struct btrfs_path *path;
1397
Yan Zheng2b820322008-11-17 21:11:30 -05001398 path = btrfs_alloc_path();
1399 if (!path)
1400 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001401
1402 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1403 key.type = BTRFS_DEV_ITEM_KEY;
1404 key.offset = (u64)-1;
1405
Ilya Dryomov53f10652013-08-12 14:33:01 +03001406 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001407 if (ret < 0)
1408 goto error;
1409
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001410 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001411
Ilya Dryomov53f10652013-08-12 14:33:01 +03001412 ret = btrfs_previous_item(fs_info->chunk_root, path,
1413 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001414 BTRFS_DEV_ITEM_KEY);
1415 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001416 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001417 } else {
1418 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1419 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001420 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001421 }
1422 ret = 0;
1423error:
Yan Zheng2b820322008-11-17 21:11:30 -05001424 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001425 return ret;
1426}
1427
1428/*
1429 * the device information is stored in the chunk root
1430 * the btrfs_device struct should be fully filled in
1431 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001432static int btrfs_add_device(struct btrfs_trans_handle *trans,
1433 struct btrfs_root *root,
1434 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001435{
1436 int ret;
1437 struct btrfs_path *path;
1438 struct btrfs_dev_item *dev_item;
1439 struct extent_buffer *leaf;
1440 struct btrfs_key key;
1441 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001442
1443 root = root->fs_info->chunk_root;
1444
1445 path = btrfs_alloc_path();
1446 if (!path)
1447 return -ENOMEM;
1448
Chris Mason0b86a832008-03-24 15:01:56 -04001449 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1450 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001451 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001452
1453 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001454 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001455 if (ret)
1456 goto out;
1457
1458 leaf = path->nodes[0];
1459 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1460
1461 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001462 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001463 btrfs_set_device_type(leaf, dev_item, device->type);
1464 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1465 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1466 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7df69d32014-07-24 11:37:13 +08001467 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001468 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001469 btrfs_set_device_group(leaf, dev_item, 0);
1470 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1471 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001472 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001473
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001474 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001475 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001476 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001477 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001478 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001479
Yan Zheng2b820322008-11-17 21:11:30 -05001480 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001481out:
1482 btrfs_free_path(path);
1483 return ret;
1484}
Chris Mason8f18cf12008-04-25 16:53:30 -04001485
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001486/*
1487 * Function to update ctime/mtime for a given device path.
1488 * Mainly used for ctime/mtime based probe like libblkid.
1489 */
1490static void update_dev_time(char *path_name)
1491{
1492 struct file *filp;
1493
1494 filp = filp_open(path_name, O_RDWR, 0);
1495 if (!filp)
1496 return;
1497 file_update_time(filp);
1498 filp_close(filp, NULL);
1499 return;
1500}
1501
Chris Masona061fc82008-05-07 11:43:44 -04001502static int btrfs_rm_dev_item(struct btrfs_root *root,
1503 struct btrfs_device *device)
1504{
1505 int ret;
1506 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001507 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001508 struct btrfs_trans_handle *trans;
1509
1510 root = root->fs_info->chunk_root;
1511
1512 path = btrfs_alloc_path();
1513 if (!path)
1514 return -ENOMEM;
1515
Yan, Zhenga22285a2010-05-16 10:48:46 -04001516 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001517 if (IS_ERR(trans)) {
1518 btrfs_free_path(path);
1519 return PTR_ERR(trans);
1520 }
Chris Masona061fc82008-05-07 11:43:44 -04001521 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1522 key.type = BTRFS_DEV_ITEM_KEY;
1523 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001524 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001525
1526 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1527 if (ret < 0)
1528 goto out;
1529
1530 if (ret > 0) {
1531 ret = -ENOENT;
1532 goto out;
1533 }
1534
1535 ret = btrfs_del_item(trans, root, path);
1536 if (ret)
1537 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001538out:
1539 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001540 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001541 btrfs_commit_transaction(trans, root);
1542 return ret;
1543}
1544
1545int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1546{
1547 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001548 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001549 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001550 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001551 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001552 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001553 u64 all_avail;
1554 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001555 u64 num_devices;
1556 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001557 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001558 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001559 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001560
Chris Masona061fc82008-05-07 11:43:44 -04001561 mutex_lock(&uuid_mutex);
1562
Miao Xiede98ced2013-01-29 10:13:12 +00001563 do {
1564 seq = read_seqbegin(&root->fs_info->profiles_lock);
1565
1566 all_avail = root->fs_info->avail_data_alloc_bits |
1567 root->fs_info->avail_system_alloc_bits |
1568 root->fs_info->avail_metadata_alloc_bits;
1569 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001570
Stefan Behrens8dabb742012-11-06 13:15:27 +01001571 num_devices = root->fs_info->fs_devices->num_devices;
1572 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1573 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1574 WARN_ON(num_devices < 1);
1575 num_devices--;
1576 }
1577 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1578
1579 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001580 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001581 goto out;
1582 }
1583
Stefan Behrens8dabb742012-11-06 13:15:27 +01001584 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001585 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001586 goto out;
1587 }
1588
David Woodhouse53b381b2013-01-29 18:40:14 -05001589 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1590 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001591 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001592 goto out;
1593 }
1594 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1595 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001596 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001597 goto out;
1598 }
1599
Chris Masondfe25022008-05-13 13:46:40 -04001600 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001601 struct list_head *devices;
1602 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001603
Chris Masondfe25022008-05-13 13:46:40 -04001604 device = NULL;
1605 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001606 /*
1607 * It is safe to read the devices since the volume_mutex
1608 * is held.
1609 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001610 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001611 if (tmp->in_fs_metadata &&
1612 !tmp->is_tgtdev_for_dev_replace &&
1613 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001614 device = tmp;
1615 break;
1616 }
1617 }
1618 bdev = NULL;
1619 bh = NULL;
1620 disk_super = NULL;
1621 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001622 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001623 goto out;
1624 }
Chris Masondfe25022008-05-13 13:46:40 -04001625 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001626 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001627 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001628 root->fs_info->bdev_holder, 0,
1629 &bdev, &bh);
1630 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001631 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001632 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001633 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001634 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001635 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001636 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001637 if (!device) {
1638 ret = -ENOENT;
1639 goto error_brelse;
1640 }
Chris Masondfe25022008-05-13 13:46:40 -04001641 }
Yan Zheng2b820322008-11-17 21:11:30 -05001642
Stefan Behrens63a212a2012-11-05 18:29:28 +01001643 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001644 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001645 goto error_brelse;
1646 }
1647
Yan Zheng2b820322008-11-17 21:11:30 -05001648 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001649 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001650 goto error_brelse;
1651 }
1652
1653 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001654 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001655 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001656 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001657 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001658 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001659 }
Chris Masona061fc82008-05-07 11:43:44 -04001660
Carey Underwoodd7901552013-03-04 16:37:06 -06001661 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001662 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001663 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001664 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001665 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001666
Stefan Behrens63a212a2012-11-05 18:29:28 +01001667 /*
1668 * TODO: the superblock still includes this device in its num_devices
1669 * counter although write_all_supers() is not locked out. This
1670 * could give a filesystem state which requires a degraded mount.
1671 */
Chris Masona061fc82008-05-07 11:43:44 -04001672 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1673 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001674 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001675
Josef Bacik2bf64752011-09-26 17:12:22 -04001676 spin_lock(&root->fs_info->free_chunk_lock);
1677 root->fs_info->free_chunk_space = device->total_bytes -
1678 device->bytes_used;
1679 spin_unlock(&root->fs_info->free_chunk_lock);
1680
Yan Zheng2b820322008-11-17 21:11:30 -05001681 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001682 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001683
1684 /*
1685 * the device list mutex makes sure that we don't change
1686 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001687 * the device supers. Whoever is writing all supers, should
1688 * lock the device list mutex before getting the number of
1689 * devices in the super block (super_copy). Conversely,
1690 * whoever updates the number of devices in the super block
1691 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001692 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001693
1694 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001695 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001696 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001697
Yan Zhenge4404d62008-12-12 10:03:26 -05001698 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001699 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001700
Chris Masoncd02dca2010-12-13 14:56:23 -05001701 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001702 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001703
Yan Zheng2b820322008-11-17 21:11:30 -05001704 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1705 struct btrfs_device, dev_list);
1706 if (device->bdev == root->fs_info->sb->s_bdev)
1707 root->fs_info->sb->s_bdev = next_device->bdev;
1708 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1709 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1710
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001711 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001712 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001713 /* remove sysfs entry */
1714 btrfs_kobj_rm_device(root->fs_info, device);
1715 }
Anand Jain99994cd2014-06-03 11:36:00 +08001716
Xiao Guangrong1f781602011-04-20 10:09:16 +00001717 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001718
David Sterba6c417612011-04-13 15:41:04 +02001719 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1720 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001721 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001722
Xiao Guangrong1f781602011-04-20 10:09:16 +00001723 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001724 struct btrfs_fs_devices *fs_devices;
1725 fs_devices = root->fs_info->fs_devices;
1726 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001727 if (fs_devices->seed == cur_devices) {
1728 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001729 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001730 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001731 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001732 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001733 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001734 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001735 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001736 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001737 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001738 }
1739
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001740 root->fs_info->num_tolerated_disk_barrier_failures =
1741 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1742
Yan Zheng2b820322008-11-17 21:11:30 -05001743 /*
1744 * at this point, the device is zero sized. We want to
1745 * remove it from the devices list and zero out the old super
1746 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001747 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001748 u64 bytenr;
1749 int i;
1750
Chris Masondfe25022008-05-13 13:46:40 -04001751 /* make sure this device isn't detected as part of
1752 * the FS anymore
1753 */
1754 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1755 set_buffer_dirty(bh);
1756 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001757
1758 /* clear the mirror copies of super block on the disk
1759 * being removed, 0th copy is been taken care above and
1760 * the below would take of the rest
1761 */
1762 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1763 bytenr = btrfs_sb_offset(i);
1764 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1765 i_size_read(bdev->bd_inode))
1766 break;
1767
1768 brelse(bh);
1769 bh = __bread(bdev, bytenr / 4096,
1770 BTRFS_SUPER_INFO_SIZE);
1771 if (!bh)
1772 continue;
1773
1774 disk_super = (struct btrfs_super_block *)bh->b_data;
1775
1776 if (btrfs_super_bytenr(disk_super) != bytenr ||
1777 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1778 continue;
1779 }
1780 memset(&disk_super->magic, 0,
1781 sizeof(disk_super->magic));
1782 set_buffer_dirty(bh);
1783 sync_dirty_buffer(bh);
1784 }
Chris Masondfe25022008-05-13 13:46:40 -04001785 }
Chris Masona061fc82008-05-07 11:43:44 -04001786
Chris Masona061fc82008-05-07 11:43:44 -04001787 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001788
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001789 if (bdev) {
1790 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001791 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001792
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001793 /* Update ctime/mtime for device path for libblkid */
1794 update_dev_time(device_path);
1795 }
1796
Chris Masona061fc82008-05-07 11:43:44 -04001797error_brelse:
1798 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001799 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001800 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001801out:
1802 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001803 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001804error_undo:
1805 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001806 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001807 list_add(&device->dev_alloc_list,
1808 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001809 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001810 root->fs_info->fs_devices->rw_devices++;
1811 }
1812 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001813}
1814
Stefan Behrense93c89c2012-11-05 17:33:06 +01001815void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1816 struct btrfs_device *srcdev)
1817{
Anand Jaind51908c2014-08-13 14:24:19 +08001818 struct btrfs_fs_devices *fs_devices;
1819
Stefan Behrense93c89c2012-11-05 17:33:06 +01001820 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001821
Anand Jaind51908c2014-08-13 14:24:19 +08001822 fs_devices = fs_info->fs_devices;
1823
Stefan Behrense93c89c2012-11-05 17:33:06 +01001824 list_del_rcu(&srcdev->dev_list);
1825 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001826 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001827 if (srcdev->missing) {
Anand Jaind51908c2014-08-13 14:24:19 +08001828 fs_devices->missing_devices--;
1829 fs_devices->rw_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001830 }
1831 if (srcdev->can_discard)
Anand Jaind51908c2014-08-13 14:24:19 +08001832 fs_devices->num_can_discard--;
Ilya Dryomov13572722013-10-02 20:41:01 +03001833 if (srcdev->bdev) {
Anand Jaind51908c2014-08-13 14:24:19 +08001834 fs_devices->open_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001835
Miao Xieff61d172014-07-24 11:37:06 +08001836 /*
1837 * zero out the old super if it is not writable
1838 * (e.g. seed device)
1839 */
1840 if (srcdev->writeable)
1841 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03001842 }
1843
Stefan Behrense93c89c2012-11-05 17:33:06 +01001844 call_rcu(&srcdev->rcu, free_device);
1845}
1846
1847void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1848 struct btrfs_device *tgtdev)
1849{
1850 struct btrfs_device *next_device;
1851
1852 WARN_ON(!tgtdev);
1853 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1854 if (tgtdev->bdev) {
1855 btrfs_scratch_superblock(tgtdev);
1856 fs_info->fs_devices->open_devices--;
1857 }
1858 fs_info->fs_devices->num_devices--;
1859 if (tgtdev->can_discard)
1860 fs_info->fs_devices->num_can_discard++;
1861
1862 next_device = list_entry(fs_info->fs_devices->devices.next,
1863 struct btrfs_device, dev_list);
1864 if (tgtdev->bdev == fs_info->sb->s_bdev)
1865 fs_info->sb->s_bdev = next_device->bdev;
1866 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1867 fs_info->fs_devices->latest_bdev = next_device->bdev;
1868 list_del_rcu(&tgtdev->dev_list);
1869
1870 call_rcu(&tgtdev->rcu, free_device);
1871
1872 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1873}
1874
Eric Sandeen48a3b632013-04-25 20:41:01 +00001875static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1876 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001877{
1878 int ret = 0;
1879 struct btrfs_super_block *disk_super;
1880 u64 devid;
1881 u8 *dev_uuid;
1882 struct block_device *bdev;
1883 struct buffer_head *bh;
1884
1885 *device = NULL;
1886 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1887 root->fs_info->bdev_holder, 0, &bdev, &bh);
1888 if (ret)
1889 return ret;
1890 disk_super = (struct btrfs_super_block *)bh->b_data;
1891 devid = btrfs_stack_device_id(&disk_super->dev_item);
1892 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001893 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001894 disk_super->fsid);
1895 brelse(bh);
1896 if (!*device)
1897 ret = -ENOENT;
1898 blkdev_put(bdev, FMODE_READ);
1899 return ret;
1900}
1901
1902int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1903 char *device_path,
1904 struct btrfs_device **device)
1905{
1906 *device = NULL;
1907 if (strcmp(device_path, "missing") == 0) {
1908 struct list_head *devices;
1909 struct btrfs_device *tmp;
1910
1911 devices = &root->fs_info->fs_devices->devices;
1912 /*
1913 * It is safe to read the devices since the volume_mutex
1914 * is held by the caller.
1915 */
1916 list_for_each_entry(tmp, devices, dev_list) {
1917 if (tmp->in_fs_metadata && !tmp->bdev) {
1918 *device = tmp;
1919 break;
1920 }
1921 }
1922
1923 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001924 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001925 return -ENOENT;
1926 }
1927
1928 return 0;
1929 } else {
1930 return btrfs_find_device_by_path(root, device_path, device);
1931 }
1932}
1933
Yan Zheng2b820322008-11-17 21:11:30 -05001934/*
1935 * does all the dirty work required for changing file system's UUID.
1936 */
Li Zefan125ccb02011-12-08 15:07:24 +08001937static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001938{
1939 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1940 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001941 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001942 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001943 struct btrfs_device *device;
1944 u64 super_flags;
1945
1946 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001947 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001948 return -EINVAL;
1949
Ilya Dryomov2208a372013-08-12 14:33:03 +03001950 seed_devices = __alloc_fs_devices();
1951 if (IS_ERR(seed_devices))
1952 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001953
Yan Zhenge4404d62008-12-12 10:03:26 -05001954 old_devices = clone_fs_devices(fs_devices);
1955 if (IS_ERR(old_devices)) {
1956 kfree(seed_devices);
1957 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001958 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001959
Yan Zheng2b820322008-11-17 21:11:30 -05001960 list_add(&old_devices->list, &fs_uuids);
1961
Yan Zhenge4404d62008-12-12 10:03:26 -05001962 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1963 seed_devices->opened = 1;
1964 INIT_LIST_HEAD(&seed_devices->devices);
1965 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001966 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001967
1968 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001969 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1970 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001971
Yan Zhenge4404d62008-12-12 10:03:26 -05001972 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1973 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1974 device->fs_devices = seed_devices;
1975 }
1976
Yan Zheng2b820322008-11-17 21:11:30 -05001977 fs_devices->seeding = 0;
1978 fs_devices->num_devices = 0;
1979 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08001980 fs_devices->missing_devices = 0;
1981 fs_devices->num_can_discard = 0;
1982 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001983 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001984
1985 generate_random_uuid(fs_devices->fsid);
1986 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1987 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01001988 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1989
Yan Zheng2b820322008-11-17 21:11:30 -05001990 super_flags = btrfs_super_flags(disk_super) &
1991 ~BTRFS_SUPER_FLAG_SEEDING;
1992 btrfs_set_super_flags(disk_super, super_flags);
1993
1994 return 0;
1995}
1996
1997/*
1998 * strore the expected generation for seed devices in device items.
1999 */
2000static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2001 struct btrfs_root *root)
2002{
2003 struct btrfs_path *path;
2004 struct extent_buffer *leaf;
2005 struct btrfs_dev_item *dev_item;
2006 struct btrfs_device *device;
2007 struct btrfs_key key;
2008 u8 fs_uuid[BTRFS_UUID_SIZE];
2009 u8 dev_uuid[BTRFS_UUID_SIZE];
2010 u64 devid;
2011 int ret;
2012
2013 path = btrfs_alloc_path();
2014 if (!path)
2015 return -ENOMEM;
2016
2017 root = root->fs_info->chunk_root;
2018 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2019 key.offset = 0;
2020 key.type = BTRFS_DEV_ITEM_KEY;
2021
2022 while (1) {
2023 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2024 if (ret < 0)
2025 goto error;
2026
2027 leaf = path->nodes[0];
2028next_slot:
2029 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2030 ret = btrfs_next_leaf(root, path);
2031 if (ret > 0)
2032 break;
2033 if (ret < 0)
2034 goto error;
2035 leaf = path->nodes[0];
2036 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002037 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002038 continue;
2039 }
2040
2041 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2042 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2043 key.type != BTRFS_DEV_ITEM_KEY)
2044 break;
2045
2046 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2047 struct btrfs_dev_item);
2048 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002049 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002050 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002051 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002052 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002053 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2054 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002055 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002056
2057 if (device->fs_devices->seeding) {
2058 btrfs_set_device_generation(leaf, dev_item,
2059 device->generation);
2060 btrfs_mark_buffer_dirty(leaf);
2061 }
2062
2063 path->slots[0]++;
2064 goto next_slot;
2065 }
2066 ret = 0;
2067error:
2068 btrfs_free_path(path);
2069 return ret;
2070}
2071
Chris Mason788f20e2008-04-28 15:29:42 -04002072int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2073{
Josef Bacikd5e20032011-08-04 14:52:27 +00002074 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002075 struct btrfs_trans_handle *trans;
2076 struct btrfs_device *device;
2077 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002078 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002079 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002080 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04002081 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002082 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002083 int ret = 0;
2084
Yan Zheng2b820322008-11-17 21:11:30 -05002085 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002086 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002087
Li Zefana5d16332011-12-07 20:08:40 -05002088 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002089 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002090 if (IS_ERR(bdev))
2091 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002092
Yan Zheng2b820322008-11-17 21:11:30 -05002093 if (root->fs_info->fs_devices->seeding) {
2094 seeding_dev = 1;
2095 down_write(&sb->s_umount);
2096 mutex_lock(&uuid_mutex);
2097 }
2098
Chris Mason8c8bee12008-09-29 11:19:10 -04002099 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002100
Chris Mason788f20e2008-04-28 15:29:42 -04002101 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002102
2103 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002104 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002105 if (device->bdev == bdev) {
2106 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002107 mutex_unlock(
2108 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002109 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002110 }
2111 }
Liu Bod25628b2012-11-14 14:35:30 +00002112 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002113
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002114 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2115 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002116 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002117 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002118 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002119 }
2120
Josef Bacik606686e2012-06-04 14:03:51 -04002121 name = rcu_string_strdup(device_path, GFP_NOFS);
2122 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002123 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002124 ret = -ENOMEM;
2125 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002126 }
Josef Bacik606686e2012-06-04 14:03:51 -04002127 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002128
Yan, Zhenga22285a2010-05-16 10:48:46 -04002129 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002130 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002131 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002132 kfree(device);
2133 ret = PTR_ERR(trans);
2134 goto error;
2135 }
2136
Yan Zheng2b820322008-11-17 21:11:30 -05002137 lock_chunks(root);
2138
Josef Bacikd5e20032011-08-04 14:52:27 +00002139 q = bdev_get_queue(bdev);
2140 if (blk_queue_discard(q))
2141 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002142 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002143 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002144 device->io_width = root->sectorsize;
2145 device->io_align = root->sectorsize;
2146 device->sector_size = root->sectorsize;
2147 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002148 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002149 device->dev_root = root->fs_info->dev_root;
2150 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002151 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002152 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002153 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002154 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002155 set_blocksize(device->bdev, 4096);
2156
Yan Zheng2b820322008-11-17 21:11:30 -05002157 if (seeding_dev) {
2158 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002159 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002160 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002161 }
2162
2163 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002164
Chris Masone5e9a522009-06-10 15:17:02 -04002165 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002166 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002167 list_add(&device->dev_alloc_list,
2168 &root->fs_info->fs_devices->alloc_list);
2169 root->fs_info->fs_devices->num_devices++;
2170 root->fs_info->fs_devices->open_devices++;
2171 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002172 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002173 if (device->can_discard)
2174 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002175 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2176
Josef Bacik2bf64752011-09-26 17:12:22 -04002177 spin_lock(&root->fs_info->free_chunk_lock);
2178 root->fs_info->free_chunk_space += device->total_bytes;
2179 spin_unlock(&root->fs_info->free_chunk_lock);
2180
Chris Masonc2898112009-06-10 09:51:32 -04002181 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2182 root->fs_info->fs_devices->rotating = 1;
2183
David Sterba6c417612011-04-13 15:41:04 +02002184 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2185 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002186 total_bytes + device->total_bytes);
2187
David Sterba6c417612011-04-13 15:41:04 +02002188 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2189 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002190 total_bytes + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002191
2192 /* add sysfs device entry */
2193 btrfs_kobj_add_device(root->fs_info, device);
2194
Chris Masone5e9a522009-06-10 15:17:02 -04002195 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002196
Yan Zheng2b820322008-11-17 21:11:30 -05002197 if (seeding_dev) {
Anand Jainb2373f22014-06-03 11:36:03 +08002198 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002199 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002200 if (ret) {
2201 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002202 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002203 }
Yan Zheng2b820322008-11-17 21:11:30 -05002204 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002205 if (ret) {
2206 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002207 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002208 }
Anand Jainb2373f22014-06-03 11:36:03 +08002209
2210 /* Sprouting would change fsid of the mounted root,
2211 * so rename the fsid on the sysfs
2212 */
2213 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2214 root->fs_info->fsid);
2215 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2216 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002217 } else {
2218 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002219 if (ret) {
2220 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002221 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002222 }
Yan Zheng2b820322008-11-17 21:11:30 -05002223 }
2224
Chris Mason913d9522009-03-10 13:17:18 -04002225 /*
2226 * we've got more storage, clear any full flags on the space
2227 * infos
2228 */
2229 btrfs_clear_space_info_full(root->fs_info);
2230
Chris Mason7d9eb122008-07-08 14:19:17 -04002231 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002232 root->fs_info->num_tolerated_disk_barrier_failures =
2233 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002234 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002235
2236 if (seeding_dev) {
2237 mutex_unlock(&uuid_mutex);
2238 up_write(&sb->s_umount);
2239
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002240 if (ret) /* transaction commit */
2241 return ret;
2242
Yan Zheng2b820322008-11-17 21:11:30 -05002243 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002244 if (ret < 0)
2245 btrfs_error(root->fs_info, ret,
2246 "Failed to relocate sys chunks after "
2247 "device initialization. This can be fixed "
2248 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002249 trans = btrfs_attach_transaction(root);
2250 if (IS_ERR(trans)) {
2251 if (PTR_ERR(trans) == -ENOENT)
2252 return 0;
2253 return PTR_ERR(trans);
2254 }
2255 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002256 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002257
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002258 /* Update ctime/mtime for libblkid */
2259 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002260 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002261
2262error_trans:
2263 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002264 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002265 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002266 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002267 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002268error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002269 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002270 if (seeding_dev) {
2271 mutex_unlock(&uuid_mutex);
2272 up_write(&sb->s_umount);
2273 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002274 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002275}
2276
Stefan Behrense93c89c2012-11-05 17:33:06 +01002277int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2278 struct btrfs_device **device_out)
2279{
2280 struct request_queue *q;
2281 struct btrfs_device *device;
2282 struct block_device *bdev;
2283 struct btrfs_fs_info *fs_info = root->fs_info;
2284 struct list_head *devices;
2285 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002286 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002287 int ret = 0;
2288
2289 *device_out = NULL;
2290 if (fs_info->fs_devices->seeding)
2291 return -EINVAL;
2292
2293 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2294 fs_info->bdev_holder);
2295 if (IS_ERR(bdev))
2296 return PTR_ERR(bdev);
2297
2298 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2299
2300 devices = &fs_info->fs_devices->devices;
2301 list_for_each_entry(device, devices, dev_list) {
2302 if (device->bdev == bdev) {
2303 ret = -EEXIST;
2304 goto error;
2305 }
2306 }
2307
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002308 device = btrfs_alloc_device(NULL, &devid, NULL);
2309 if (IS_ERR(device)) {
2310 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002311 goto error;
2312 }
2313
2314 name = rcu_string_strdup(device_path, GFP_NOFS);
2315 if (!name) {
2316 kfree(device);
2317 ret = -ENOMEM;
2318 goto error;
2319 }
2320 rcu_assign_pointer(device->name, name);
2321
2322 q = bdev_get_queue(bdev);
2323 if (blk_queue_discard(q))
2324 device->can_discard = 1;
2325 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2326 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002327 device->generation = 0;
2328 device->io_width = root->sectorsize;
2329 device->io_align = root->sectorsize;
2330 device->sector_size = root->sectorsize;
2331 device->total_bytes = i_size_read(bdev->bd_inode);
2332 device->disk_total_bytes = device->total_bytes;
2333 device->dev_root = fs_info->dev_root;
2334 device->bdev = bdev;
2335 device->in_fs_metadata = 1;
2336 device->is_tgtdev_for_dev_replace = 1;
2337 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002338 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002339 set_blocksize(device->bdev, 4096);
2340 device->fs_devices = fs_info->fs_devices;
2341 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2342 fs_info->fs_devices->num_devices++;
2343 fs_info->fs_devices->open_devices++;
2344 if (device->can_discard)
2345 fs_info->fs_devices->num_can_discard++;
2346 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2347
2348 *device_out = device;
2349 return ret;
2350
2351error:
2352 blkdev_put(bdev, FMODE_EXCL);
2353 return ret;
2354}
2355
2356void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2357 struct btrfs_device *tgtdev)
2358{
2359 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2360 tgtdev->io_width = fs_info->dev_root->sectorsize;
2361 tgtdev->io_align = fs_info->dev_root->sectorsize;
2362 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2363 tgtdev->dev_root = fs_info->dev_root;
2364 tgtdev->in_fs_metadata = 1;
2365}
2366
Chris Masond3977122009-01-05 21:25:51 -05002367static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2368 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002369{
2370 int ret;
2371 struct btrfs_path *path;
2372 struct btrfs_root *root;
2373 struct btrfs_dev_item *dev_item;
2374 struct extent_buffer *leaf;
2375 struct btrfs_key key;
2376
2377 root = device->dev_root->fs_info->chunk_root;
2378
2379 path = btrfs_alloc_path();
2380 if (!path)
2381 return -ENOMEM;
2382
2383 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2384 key.type = BTRFS_DEV_ITEM_KEY;
2385 key.offset = device->devid;
2386
2387 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2388 if (ret < 0)
2389 goto out;
2390
2391 if (ret > 0) {
2392 ret = -ENOENT;
2393 goto out;
2394 }
2395
2396 leaf = path->nodes[0];
2397 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2398
2399 btrfs_set_device_id(leaf, dev_item, device->devid);
2400 btrfs_set_device_type(leaf, dev_item, device->type);
2401 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2402 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2403 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002404 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002405 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2406 btrfs_mark_buffer_dirty(leaf);
2407
2408out:
2409 btrfs_free_path(path);
2410 return ret;
2411}
2412
Chris Mason7d9eb122008-07-08 14:19:17 -04002413static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002414 struct btrfs_device *device, u64 new_size)
2415{
2416 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002417 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002418 u64 old_total = btrfs_super_total_bytes(super_copy);
2419 u64 diff = new_size - device->total_bytes;
2420
Yan Zheng2b820322008-11-17 21:11:30 -05002421 if (!device->writeable)
2422 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002423 if (new_size <= device->total_bytes ||
2424 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002425 return -EINVAL;
2426
Chris Mason8f18cf12008-04-25 16:53:30 -04002427 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002428 device->fs_devices->total_rw_bytes += diff;
2429
2430 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002431 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002432 btrfs_clear_space_info_full(device->dev_root->fs_info);
2433
Chris Mason8f18cf12008-04-25 16:53:30 -04002434 return btrfs_update_device(trans, device);
2435}
2436
Chris Mason7d9eb122008-07-08 14:19:17 -04002437int btrfs_grow_device(struct btrfs_trans_handle *trans,
2438 struct btrfs_device *device, u64 new_size)
2439{
2440 int ret;
2441 lock_chunks(device->dev_root);
2442 ret = __btrfs_grow_device(trans, device, new_size);
2443 unlock_chunks(device->dev_root);
2444 return ret;
2445}
2446
Chris Mason8f18cf12008-04-25 16:53:30 -04002447static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2448 struct btrfs_root *root,
2449 u64 chunk_tree, u64 chunk_objectid,
2450 u64 chunk_offset)
2451{
2452 int ret;
2453 struct btrfs_path *path;
2454 struct btrfs_key key;
2455
2456 root = root->fs_info->chunk_root;
2457 path = btrfs_alloc_path();
2458 if (!path)
2459 return -ENOMEM;
2460
2461 key.objectid = chunk_objectid;
2462 key.offset = chunk_offset;
2463 key.type = BTRFS_CHUNK_ITEM_KEY;
2464
2465 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002466 if (ret < 0)
2467 goto out;
2468 else if (ret > 0) { /* Logic error or corruption */
2469 btrfs_error(root->fs_info, -ENOENT,
2470 "Failed lookup while freeing chunk.");
2471 ret = -ENOENT;
2472 goto out;
2473 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002474
2475 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002476 if (ret < 0)
2477 btrfs_error(root->fs_info, ret,
2478 "Failed to delete chunk item.");
2479out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002480 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002481 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002482}
2483
Christoph Hellwigb2950862008-12-02 09:54:17 -05002484static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002485 chunk_offset)
2486{
David Sterba6c417612011-04-13 15:41:04 +02002487 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002488 struct btrfs_disk_key *disk_key;
2489 struct btrfs_chunk *chunk;
2490 u8 *ptr;
2491 int ret = 0;
2492 u32 num_stripes;
2493 u32 array_size;
2494 u32 len = 0;
2495 u32 cur;
2496 struct btrfs_key key;
2497
2498 array_size = btrfs_super_sys_array_size(super_copy);
2499
2500 ptr = super_copy->sys_chunk_array;
2501 cur = 0;
2502
2503 while (cur < array_size) {
2504 disk_key = (struct btrfs_disk_key *)ptr;
2505 btrfs_disk_key_to_cpu(&key, disk_key);
2506
2507 len = sizeof(*disk_key);
2508
2509 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2510 chunk = (struct btrfs_chunk *)(ptr + len);
2511 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2512 len += btrfs_chunk_item_size(num_stripes);
2513 } else {
2514 ret = -EIO;
2515 break;
2516 }
2517 if (key.objectid == chunk_objectid &&
2518 key.offset == chunk_offset) {
2519 memmove(ptr, ptr + len, array_size - (cur + len));
2520 array_size -= len;
2521 btrfs_set_super_sys_array_size(super_copy, array_size);
2522 } else {
2523 ptr += len;
2524 cur += len;
2525 }
2526 }
2527 return ret;
2528}
2529
Christoph Hellwigb2950862008-12-02 09:54:17 -05002530static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002531 u64 chunk_tree, u64 chunk_objectid,
2532 u64 chunk_offset)
2533{
2534 struct extent_map_tree *em_tree;
2535 struct btrfs_root *extent_root;
2536 struct btrfs_trans_handle *trans;
2537 struct extent_map *em;
2538 struct map_lookup *map;
2539 int ret;
2540 int i;
2541
2542 root = root->fs_info->chunk_root;
2543 extent_root = root->fs_info->extent_root;
2544 em_tree = &root->fs_info->mapping_tree.map_tree;
2545
Josef Bacikba1bf482009-09-11 16:11:19 -04002546 ret = btrfs_can_relocate(extent_root, chunk_offset);
2547 if (ret)
2548 return -ENOSPC;
2549
Chris Mason8f18cf12008-04-25 16:53:30 -04002550 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002551 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002552 if (ret)
2553 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002554
Yan, Zhenga22285a2010-05-16 10:48:46 -04002555 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002556 if (IS_ERR(trans)) {
2557 ret = PTR_ERR(trans);
2558 btrfs_std_error(root->fs_info, ret);
2559 return ret;
2560 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002561
Chris Mason7d9eb122008-07-08 14:19:17 -04002562 lock_chunks(root);
2563
Chris Mason8f18cf12008-04-25 16:53:30 -04002564 /*
2565 * step two, delete the device extents and the
2566 * chunk tree entries
2567 */
Chris Mason890871b2009-09-02 16:24:52 -04002568 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002569 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002570 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002571
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002572 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002573 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002574 map = (struct map_lookup *)em->bdev;
2575
2576 for (i = 0; i < map->num_stripes; i++) {
2577 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2578 map->stripes[i].physical);
2579 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002580
Chris Masondfe25022008-05-13 13:46:40 -04002581 if (map->stripes[i].dev) {
2582 ret = btrfs_update_device(trans, map->stripes[i].dev);
2583 BUG_ON(ret);
2584 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002585 }
2586 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2587 chunk_offset);
2588
2589 BUG_ON(ret);
2590
liubo1abe9b82011-03-24 11:18:59 +00002591 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2592
Chris Mason8f18cf12008-04-25 16:53:30 -04002593 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2594 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2595 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002596 }
2597
Zheng Yan1a40e232008-09-26 10:09:34 -04002598 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2599 BUG_ON(ret);
2600
Chris Mason890871b2009-09-02 16:24:52 -04002601 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002602 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002603 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002604
Chris Mason8f18cf12008-04-25 16:53:30 -04002605 /* once for the tree */
2606 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002607 /* once for us */
2608 free_extent_map(em);
2609
Chris Mason7d9eb122008-07-08 14:19:17 -04002610 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002611 btrfs_end_transaction(trans, root);
2612 return 0;
2613}
2614
Yan Zheng2b820322008-11-17 21:11:30 -05002615static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2616{
2617 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2618 struct btrfs_path *path;
2619 struct extent_buffer *leaf;
2620 struct btrfs_chunk *chunk;
2621 struct btrfs_key key;
2622 struct btrfs_key found_key;
2623 u64 chunk_tree = chunk_root->root_key.objectid;
2624 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002625 bool retried = false;
2626 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002627 int ret;
2628
2629 path = btrfs_alloc_path();
2630 if (!path)
2631 return -ENOMEM;
2632
Josef Bacikba1bf482009-09-11 16:11:19 -04002633again:
Yan Zheng2b820322008-11-17 21:11:30 -05002634 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2635 key.offset = (u64)-1;
2636 key.type = BTRFS_CHUNK_ITEM_KEY;
2637
2638 while (1) {
2639 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2640 if (ret < 0)
2641 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002642 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002643
2644 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2645 key.type);
2646 if (ret < 0)
2647 goto error;
2648 if (ret > 0)
2649 break;
2650
2651 leaf = path->nodes[0];
2652 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2653
2654 chunk = btrfs_item_ptr(leaf, path->slots[0],
2655 struct btrfs_chunk);
2656 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002657 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002658
2659 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2660 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2661 found_key.objectid,
2662 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002663 if (ret == -ENOSPC)
2664 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302665 else
2666 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002667 }
2668
2669 if (found_key.offset == 0)
2670 break;
2671 key.offset = found_key.offset - 1;
2672 }
2673 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002674 if (failed && !retried) {
2675 failed = 0;
2676 retried = true;
2677 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302678 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002679 ret = -ENOSPC;
2680 }
Yan Zheng2b820322008-11-17 21:11:30 -05002681error:
2682 btrfs_free_path(path);
2683 return ret;
2684}
2685
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002686static int insert_balance_item(struct btrfs_root *root,
2687 struct btrfs_balance_control *bctl)
2688{
2689 struct btrfs_trans_handle *trans;
2690 struct btrfs_balance_item *item;
2691 struct btrfs_disk_balance_args disk_bargs;
2692 struct btrfs_path *path;
2693 struct extent_buffer *leaf;
2694 struct btrfs_key key;
2695 int ret, err;
2696
2697 path = btrfs_alloc_path();
2698 if (!path)
2699 return -ENOMEM;
2700
2701 trans = btrfs_start_transaction(root, 0);
2702 if (IS_ERR(trans)) {
2703 btrfs_free_path(path);
2704 return PTR_ERR(trans);
2705 }
2706
2707 key.objectid = BTRFS_BALANCE_OBJECTID;
2708 key.type = BTRFS_BALANCE_ITEM_KEY;
2709 key.offset = 0;
2710
2711 ret = btrfs_insert_empty_item(trans, root, path, &key,
2712 sizeof(*item));
2713 if (ret)
2714 goto out;
2715
2716 leaf = path->nodes[0];
2717 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2718
2719 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2720
2721 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2722 btrfs_set_balance_data(leaf, item, &disk_bargs);
2723 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2724 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2725 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2726 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2727
2728 btrfs_set_balance_flags(leaf, item, bctl->flags);
2729
2730 btrfs_mark_buffer_dirty(leaf);
2731out:
2732 btrfs_free_path(path);
2733 err = btrfs_commit_transaction(trans, root);
2734 if (err && !ret)
2735 ret = err;
2736 return ret;
2737}
2738
2739static int del_balance_item(struct btrfs_root *root)
2740{
2741 struct btrfs_trans_handle *trans;
2742 struct btrfs_path *path;
2743 struct btrfs_key key;
2744 int ret, err;
2745
2746 path = btrfs_alloc_path();
2747 if (!path)
2748 return -ENOMEM;
2749
2750 trans = btrfs_start_transaction(root, 0);
2751 if (IS_ERR(trans)) {
2752 btrfs_free_path(path);
2753 return PTR_ERR(trans);
2754 }
2755
2756 key.objectid = BTRFS_BALANCE_OBJECTID;
2757 key.type = BTRFS_BALANCE_ITEM_KEY;
2758 key.offset = 0;
2759
2760 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2761 if (ret < 0)
2762 goto out;
2763 if (ret > 0) {
2764 ret = -ENOENT;
2765 goto out;
2766 }
2767
2768 ret = btrfs_del_item(trans, root, path);
2769out:
2770 btrfs_free_path(path);
2771 err = btrfs_commit_transaction(trans, root);
2772 if (err && !ret)
2773 ret = err;
2774 return ret;
2775}
2776
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002777/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002778 * This is a heuristic used to reduce the number of chunks balanced on
2779 * resume after balance was interrupted.
2780 */
2781static void update_balance_args(struct btrfs_balance_control *bctl)
2782{
2783 /*
2784 * Turn on soft mode for chunk types that were being converted.
2785 */
2786 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2787 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2788 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2789 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2790 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2791 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2792
2793 /*
2794 * Turn on usage filter if is not already used. The idea is
2795 * that chunks that we have already balanced should be
2796 * reasonably full. Don't do it for chunks that are being
2797 * converted - that will keep us from relocating unconverted
2798 * (albeit full) chunks.
2799 */
2800 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2801 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2802 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2803 bctl->data.usage = 90;
2804 }
2805 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2806 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2807 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2808 bctl->sys.usage = 90;
2809 }
2810 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2811 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2812 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2813 bctl->meta.usage = 90;
2814 }
2815}
2816
2817/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002818 * Should be called with both balance and volume mutexes held to
2819 * serialize other volume operations (add_dev/rm_dev/resize) with
2820 * restriper. Same goes for unset_balance_control.
2821 */
2822static void set_balance_control(struct btrfs_balance_control *bctl)
2823{
2824 struct btrfs_fs_info *fs_info = bctl->fs_info;
2825
2826 BUG_ON(fs_info->balance_ctl);
2827
2828 spin_lock(&fs_info->balance_lock);
2829 fs_info->balance_ctl = bctl;
2830 spin_unlock(&fs_info->balance_lock);
2831}
2832
2833static void unset_balance_control(struct btrfs_fs_info *fs_info)
2834{
2835 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2836
2837 BUG_ON(!fs_info->balance_ctl);
2838
2839 spin_lock(&fs_info->balance_lock);
2840 fs_info->balance_ctl = NULL;
2841 spin_unlock(&fs_info->balance_lock);
2842
2843 kfree(bctl);
2844}
2845
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002846/*
2847 * Balance filters. Return 1 if chunk should be filtered out
2848 * (should not be balanced).
2849 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002850static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002851 struct btrfs_balance_args *bargs)
2852{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002853 chunk_type = chunk_to_extended(chunk_type) &
2854 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002855
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002856 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002857 return 0;
2858
2859 return 1;
2860}
2861
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002862static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2863 struct btrfs_balance_args *bargs)
2864{
2865 struct btrfs_block_group_cache *cache;
2866 u64 chunk_used, user_thresh;
2867 int ret = 1;
2868
2869 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2870 chunk_used = btrfs_block_group_used(&cache->item);
2871
Ilya Dryomova105bb82013-01-21 15:15:56 +02002872 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002873 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002874 else if (bargs->usage > 100)
2875 user_thresh = cache->key.offset;
2876 else
2877 user_thresh = div_factor_fine(cache->key.offset,
2878 bargs->usage);
2879
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002880 if (chunk_used < user_thresh)
2881 ret = 0;
2882
2883 btrfs_put_block_group(cache);
2884 return ret;
2885}
2886
Ilya Dryomov409d4042012-01-16 22:04:47 +02002887static int chunk_devid_filter(struct extent_buffer *leaf,
2888 struct btrfs_chunk *chunk,
2889 struct btrfs_balance_args *bargs)
2890{
2891 struct btrfs_stripe *stripe;
2892 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2893 int i;
2894
2895 for (i = 0; i < num_stripes; i++) {
2896 stripe = btrfs_stripe_nr(chunk, i);
2897 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2898 return 0;
2899 }
2900
2901 return 1;
2902}
2903
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002904/* [pstart, pend) */
2905static int chunk_drange_filter(struct extent_buffer *leaf,
2906 struct btrfs_chunk *chunk,
2907 u64 chunk_offset,
2908 struct btrfs_balance_args *bargs)
2909{
2910 struct btrfs_stripe *stripe;
2911 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2912 u64 stripe_offset;
2913 u64 stripe_length;
2914 int factor;
2915 int i;
2916
2917 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2918 return 0;
2919
2920 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002921 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2922 factor = num_stripes / 2;
2923 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2924 factor = num_stripes - 1;
2925 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2926 factor = num_stripes - 2;
2927 } else {
2928 factor = num_stripes;
2929 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002930
2931 for (i = 0; i < num_stripes; i++) {
2932 stripe = btrfs_stripe_nr(chunk, i);
2933 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2934 continue;
2935
2936 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2937 stripe_length = btrfs_chunk_length(leaf, chunk);
2938 do_div(stripe_length, factor);
2939
2940 if (stripe_offset < bargs->pend &&
2941 stripe_offset + stripe_length > bargs->pstart)
2942 return 0;
2943 }
2944
2945 return 1;
2946}
2947
Ilya Dryomovea671762012-01-16 22:04:48 +02002948/* [vstart, vend) */
2949static int chunk_vrange_filter(struct extent_buffer *leaf,
2950 struct btrfs_chunk *chunk,
2951 u64 chunk_offset,
2952 struct btrfs_balance_args *bargs)
2953{
2954 if (chunk_offset < bargs->vend &&
2955 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2956 /* at least part of the chunk is inside this vrange */
2957 return 0;
2958
2959 return 1;
2960}
2961
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002962static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002963 struct btrfs_balance_args *bargs)
2964{
2965 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2966 return 0;
2967
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002968 chunk_type = chunk_to_extended(chunk_type) &
2969 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002970
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002971 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002972 return 1;
2973
2974 return 0;
2975}
2976
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002977static int should_balance_chunk(struct btrfs_root *root,
2978 struct extent_buffer *leaf,
2979 struct btrfs_chunk *chunk, u64 chunk_offset)
2980{
2981 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2982 struct btrfs_balance_args *bargs = NULL;
2983 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2984
2985 /* type filter */
2986 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2987 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2988 return 0;
2989 }
2990
2991 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2992 bargs = &bctl->data;
2993 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2994 bargs = &bctl->sys;
2995 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2996 bargs = &bctl->meta;
2997
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002998 /* profiles filter */
2999 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3000 chunk_profiles_filter(chunk_type, bargs)) {
3001 return 0;
3002 }
3003
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003004 /* usage filter */
3005 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3006 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3007 return 0;
3008 }
3009
Ilya Dryomov409d4042012-01-16 22:04:47 +02003010 /* devid filter */
3011 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3012 chunk_devid_filter(leaf, chunk, bargs)) {
3013 return 0;
3014 }
3015
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003016 /* drange filter, makes sense only with devid filter */
3017 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3018 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3019 return 0;
3020 }
3021
Ilya Dryomovea671762012-01-16 22:04:48 +02003022 /* vrange filter */
3023 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3024 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3025 return 0;
3026 }
3027
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003028 /* soft profile changing mode */
3029 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3030 chunk_soft_convert_filter(chunk_type, bargs)) {
3031 return 0;
3032 }
3033
David Sterba7d824b62014-05-07 17:37:51 +02003034 /*
3035 * limited by count, must be the last filter
3036 */
3037 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3038 if (bargs->limit == 0)
3039 return 0;
3040 else
3041 bargs->limit--;
3042 }
3043
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003044 return 1;
3045}
3046
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003047static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003048{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003049 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003050 struct btrfs_root *chunk_root = fs_info->chunk_root;
3051 struct btrfs_root *dev_root = fs_info->dev_root;
3052 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003053 struct btrfs_device *device;
3054 u64 old_size;
3055 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003056 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003057 struct btrfs_path *path;
3058 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003059 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003060 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003061 struct extent_buffer *leaf;
3062 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003063 int ret;
3064 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003065 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003066 u64 limit_data = bctl->data.limit;
3067 u64 limit_meta = bctl->meta.limit;
3068 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003069
Chris Masonec44a352008-04-28 15:29:52 -04003070 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003071 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003072 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04003073 old_size = device->total_bytes;
3074 size_to_free = div_factor(old_size, 1);
3075 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003076 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003077 device->total_bytes - device->bytes_used > size_to_free ||
3078 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003079 continue;
3080
3081 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003082 if (ret == -ENOSPC)
3083 break;
Chris Masonec44a352008-04-28 15:29:52 -04003084 BUG_ON(ret);
3085
Yan, Zhenga22285a2010-05-16 10:48:46 -04003086 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003087 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003088
3089 ret = btrfs_grow_device(trans, device, old_size);
3090 BUG_ON(ret);
3091
3092 btrfs_end_transaction(trans, dev_root);
3093 }
3094
3095 /* step two, relocate all the chunks */
3096 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003097 if (!path) {
3098 ret = -ENOMEM;
3099 goto error;
3100 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003101
3102 /* zero out stat counters */
3103 spin_lock(&fs_info->balance_lock);
3104 memset(&bctl->stat, 0, sizeof(bctl->stat));
3105 spin_unlock(&fs_info->balance_lock);
3106again:
David Sterba7d824b62014-05-07 17:37:51 +02003107 if (!counting) {
3108 bctl->data.limit = limit_data;
3109 bctl->meta.limit = limit_meta;
3110 bctl->sys.limit = limit_sys;
3111 }
Chris Masonec44a352008-04-28 15:29:52 -04003112 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3113 key.offset = (u64)-1;
3114 key.type = BTRFS_CHUNK_ITEM_KEY;
3115
Chris Masond3977122009-01-05 21:25:51 -05003116 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003117 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003118 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003119 ret = -ECANCELED;
3120 goto error;
3121 }
3122
Chris Masonec44a352008-04-28 15:29:52 -04003123 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3124 if (ret < 0)
3125 goto error;
3126
3127 /*
3128 * this shouldn't happen, it means the last relocate
3129 * failed
3130 */
3131 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003132 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003133
3134 ret = btrfs_previous_item(chunk_root, path, 0,
3135 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003136 if (ret) {
3137 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003138 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003139 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003140
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003141 leaf = path->nodes[0];
3142 slot = path->slots[0];
3143 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3144
Chris Masonec44a352008-04-28 15:29:52 -04003145 if (found_key.objectid != key.objectid)
3146 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003147
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003148 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3149
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003150 if (!counting) {
3151 spin_lock(&fs_info->balance_lock);
3152 bctl->stat.considered++;
3153 spin_unlock(&fs_info->balance_lock);
3154 }
3155
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003156 ret = should_balance_chunk(chunk_root, leaf, chunk,
3157 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003158 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003159 if (!ret)
3160 goto loop;
3161
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003162 if (counting) {
3163 spin_lock(&fs_info->balance_lock);
3164 bctl->stat.expected++;
3165 spin_unlock(&fs_info->balance_lock);
3166 goto loop;
3167 }
3168
Chris Masonec44a352008-04-28 15:29:52 -04003169 ret = btrfs_relocate_chunk(chunk_root,
3170 chunk_root->root_key.objectid,
3171 found_key.objectid,
3172 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003173 if (ret && ret != -ENOSPC)
3174 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003175 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003176 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003177 } else {
3178 spin_lock(&fs_info->balance_lock);
3179 bctl->stat.completed++;
3180 spin_unlock(&fs_info->balance_lock);
3181 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003182loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003183 if (found_key.offset == 0)
3184 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003185 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003186 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003187
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003188 if (counting) {
3189 btrfs_release_path(path);
3190 counting = false;
3191 goto again;
3192 }
Chris Masonec44a352008-04-28 15:29:52 -04003193error:
3194 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003195 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003196 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003197 enospc_errors);
3198 if (!ret)
3199 ret = -ENOSPC;
3200 }
3201
Chris Masonec44a352008-04-28 15:29:52 -04003202 return ret;
3203}
3204
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003205/**
3206 * alloc_profile_is_valid - see if a given profile is valid and reduced
3207 * @flags: profile to validate
3208 * @extended: if true @flags is treated as an extended profile
3209 */
3210static int alloc_profile_is_valid(u64 flags, int extended)
3211{
3212 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3213 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3214
3215 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3216
3217 /* 1) check that all other bits are zeroed */
3218 if (flags & ~mask)
3219 return 0;
3220
3221 /* 2) see if profile is reduced */
3222 if (flags == 0)
3223 return !extended; /* "0" is valid for usual profiles */
3224
3225 /* true if exactly one bit set */
3226 return (flags & (flags - 1)) == 0;
3227}
3228
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003229static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3230{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003231 /* cancel requested || normal exit path */
3232 return atomic_read(&fs_info->balance_cancel_req) ||
3233 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3234 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003235}
3236
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003237static void __cancel_balance(struct btrfs_fs_info *fs_info)
3238{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003239 int ret;
3240
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003241 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003242 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003243 if (ret)
3244 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003245
3246 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003247}
3248
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003249/*
3250 * Should be called with both balance and volume mutexes held
3251 */
3252int btrfs_balance(struct btrfs_balance_control *bctl,
3253 struct btrfs_ioctl_balance_args *bargs)
3254{
3255 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003256 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003257 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003258 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003259 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003260 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003261
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003262 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003263 atomic_read(&fs_info->balance_pause_req) ||
3264 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003265 ret = -EINVAL;
3266 goto out;
3267 }
3268
Ilya Dryomove4837f82012-03-27 17:09:17 +03003269 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3270 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3271 mixed = 1;
3272
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003273 /*
3274 * In case of mixed groups both data and meta should be picked,
3275 * and identical options should be given for both of them.
3276 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003277 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3278 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003279 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3280 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3281 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003282 btrfs_err(fs_info, "with mixed groups data and "
3283 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003284 ret = -EINVAL;
3285 goto out;
3286 }
3287 }
3288
Stefan Behrens8dabb742012-11-06 13:15:27 +01003289 num_devices = fs_info->fs_devices->num_devices;
3290 btrfs_dev_replace_lock(&fs_info->dev_replace);
3291 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3292 BUG_ON(num_devices < 1);
3293 num_devices--;
3294 }
3295 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003296 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003297 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003298 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003299 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003300 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003301 if (num_devices > 2)
3302 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3303 if (num_devices > 3)
3304 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3305 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003306 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3307 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3308 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003309 btrfs_err(fs_info, "unable to start balance with target "
3310 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003311 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003312 ret = -EINVAL;
3313 goto out;
3314 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003315 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3316 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3317 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003318 btrfs_err(fs_info,
3319 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003320 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003321 ret = -EINVAL;
3322 goto out;
3323 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003324 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3325 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3326 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003327 btrfs_err(fs_info,
3328 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003329 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003330 ret = -EINVAL;
3331 goto out;
3332 }
3333
Ilya Dryomove4837f82012-03-27 17:09:17 +03003334 /* allow dup'ed data chunks only in mixed mode */
3335 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003336 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003337 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003338 ret = -EINVAL;
3339 goto out;
3340 }
3341
3342 /* allow to reduce meta or sys integrity only if force set */
3343 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003344 BTRFS_BLOCK_GROUP_RAID10 |
3345 BTRFS_BLOCK_GROUP_RAID5 |
3346 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003347 do {
3348 seq = read_seqbegin(&fs_info->profiles_lock);
3349
3350 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3351 (fs_info->avail_system_alloc_bits & allowed) &&
3352 !(bctl->sys.target & allowed)) ||
3353 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3354 (fs_info->avail_metadata_alloc_bits & allowed) &&
3355 !(bctl->meta.target & allowed))) {
3356 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003357 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003358 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003359 btrfs_err(fs_info, "balance will reduce metadata "
3360 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003361 ret = -EINVAL;
3362 goto out;
3363 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003364 }
Miao Xiede98ced2013-01-29 10:13:12 +00003365 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003366
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003367 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3368 int num_tolerated_disk_barrier_failures;
3369 u64 target = bctl->sys.target;
3370
3371 num_tolerated_disk_barrier_failures =
3372 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3373 if (num_tolerated_disk_barrier_failures > 0 &&
3374 (target &
3375 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3376 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3377 num_tolerated_disk_barrier_failures = 0;
3378 else if (num_tolerated_disk_barrier_failures > 1 &&
3379 (target &
3380 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3381 num_tolerated_disk_barrier_failures = 1;
3382
3383 fs_info->num_tolerated_disk_barrier_failures =
3384 num_tolerated_disk_barrier_failures;
3385 }
3386
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003387 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003388 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003389 goto out;
3390
Ilya Dryomov59641012012-01-16 22:04:48 +02003391 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3392 BUG_ON(ret == -EEXIST);
3393 set_balance_control(bctl);
3394 } else {
3395 BUG_ON(ret != -EEXIST);
3396 spin_lock(&fs_info->balance_lock);
3397 update_balance_args(bctl);
3398 spin_unlock(&fs_info->balance_lock);
3399 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003400
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003401 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003402 mutex_unlock(&fs_info->balance_mutex);
3403
3404 ret = __btrfs_balance(fs_info);
3405
3406 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003407 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003408
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003409 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3410 fs_info->num_tolerated_disk_barrier_failures =
3411 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3412 }
3413
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003414 if (bargs) {
3415 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003416 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003417 }
3418
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003419 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3420 balance_need_close(fs_info)) {
3421 __cancel_balance(fs_info);
3422 }
3423
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003424 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003425
3426 return ret;
3427out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003428 if (bctl->flags & BTRFS_BALANCE_RESUME)
3429 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003430 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003431 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003432 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3433 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003434 return ret;
3435}
3436
3437static int balance_kthread(void *data)
3438{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003439 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003440 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003441
3442 mutex_lock(&fs_info->volume_mutex);
3443 mutex_lock(&fs_info->balance_mutex);
3444
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003445 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003446 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003447 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003448 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003449
3450 mutex_unlock(&fs_info->balance_mutex);
3451 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003452
Ilya Dryomov59641012012-01-16 22:04:48 +02003453 return ret;
3454}
3455
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003456int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3457{
3458 struct task_struct *tsk;
3459
3460 spin_lock(&fs_info->balance_lock);
3461 if (!fs_info->balance_ctl) {
3462 spin_unlock(&fs_info->balance_lock);
3463 return 0;
3464 }
3465 spin_unlock(&fs_info->balance_lock);
3466
3467 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003468 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003469 return 0;
3470 }
3471
3472 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303473 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003474}
3475
Ilya Dryomov68310a52012-06-22 12:24:12 -06003476int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003477{
Ilya Dryomov59641012012-01-16 22:04:48 +02003478 struct btrfs_balance_control *bctl;
3479 struct btrfs_balance_item *item;
3480 struct btrfs_disk_balance_args disk_bargs;
3481 struct btrfs_path *path;
3482 struct extent_buffer *leaf;
3483 struct btrfs_key key;
3484 int ret;
3485
3486 path = btrfs_alloc_path();
3487 if (!path)
3488 return -ENOMEM;
3489
Ilya Dryomov68310a52012-06-22 12:24:12 -06003490 key.objectid = BTRFS_BALANCE_OBJECTID;
3491 key.type = BTRFS_BALANCE_ITEM_KEY;
3492 key.offset = 0;
3493
3494 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3495 if (ret < 0)
3496 goto out;
3497 if (ret > 0) { /* ret = -ENOENT; */
3498 ret = 0;
3499 goto out;
3500 }
3501
Ilya Dryomov59641012012-01-16 22:04:48 +02003502 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3503 if (!bctl) {
3504 ret = -ENOMEM;
3505 goto out;
3506 }
3507
Ilya Dryomov59641012012-01-16 22:04:48 +02003508 leaf = path->nodes[0];
3509 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3510
Ilya Dryomov68310a52012-06-22 12:24:12 -06003511 bctl->fs_info = fs_info;
3512 bctl->flags = btrfs_balance_flags(leaf, item);
3513 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003514
3515 btrfs_balance_data(leaf, item, &disk_bargs);
3516 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3517 btrfs_balance_meta(leaf, item, &disk_bargs);
3518 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3519 btrfs_balance_sys(leaf, item, &disk_bargs);
3520 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3521
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003522 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3523
Ilya Dryomov68310a52012-06-22 12:24:12 -06003524 mutex_lock(&fs_info->volume_mutex);
3525 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003526
Ilya Dryomov68310a52012-06-22 12:24:12 -06003527 set_balance_control(bctl);
3528
3529 mutex_unlock(&fs_info->balance_mutex);
3530 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003531out:
3532 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003533 return ret;
3534}
3535
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003536int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3537{
3538 int ret = 0;
3539
3540 mutex_lock(&fs_info->balance_mutex);
3541 if (!fs_info->balance_ctl) {
3542 mutex_unlock(&fs_info->balance_mutex);
3543 return -ENOTCONN;
3544 }
3545
3546 if (atomic_read(&fs_info->balance_running)) {
3547 atomic_inc(&fs_info->balance_pause_req);
3548 mutex_unlock(&fs_info->balance_mutex);
3549
3550 wait_event(fs_info->balance_wait_q,
3551 atomic_read(&fs_info->balance_running) == 0);
3552
3553 mutex_lock(&fs_info->balance_mutex);
3554 /* we are good with balance_ctl ripped off from under us */
3555 BUG_ON(atomic_read(&fs_info->balance_running));
3556 atomic_dec(&fs_info->balance_pause_req);
3557 } else {
3558 ret = -ENOTCONN;
3559 }
3560
3561 mutex_unlock(&fs_info->balance_mutex);
3562 return ret;
3563}
3564
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003565int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3566{
Ilya Dryomove649e582013-10-10 20:40:21 +03003567 if (fs_info->sb->s_flags & MS_RDONLY)
3568 return -EROFS;
3569
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003570 mutex_lock(&fs_info->balance_mutex);
3571 if (!fs_info->balance_ctl) {
3572 mutex_unlock(&fs_info->balance_mutex);
3573 return -ENOTCONN;
3574 }
3575
3576 atomic_inc(&fs_info->balance_cancel_req);
3577 /*
3578 * if we are running just wait and return, balance item is
3579 * deleted in btrfs_balance in this case
3580 */
3581 if (atomic_read(&fs_info->balance_running)) {
3582 mutex_unlock(&fs_info->balance_mutex);
3583 wait_event(fs_info->balance_wait_q,
3584 atomic_read(&fs_info->balance_running) == 0);
3585 mutex_lock(&fs_info->balance_mutex);
3586 } else {
3587 /* __cancel_balance needs volume_mutex */
3588 mutex_unlock(&fs_info->balance_mutex);
3589 mutex_lock(&fs_info->volume_mutex);
3590 mutex_lock(&fs_info->balance_mutex);
3591
3592 if (fs_info->balance_ctl)
3593 __cancel_balance(fs_info);
3594
3595 mutex_unlock(&fs_info->volume_mutex);
3596 }
3597
3598 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3599 atomic_dec(&fs_info->balance_cancel_req);
3600 mutex_unlock(&fs_info->balance_mutex);
3601 return 0;
3602}
3603
Stefan Behrens803b2f52013-08-15 17:11:21 +02003604static int btrfs_uuid_scan_kthread(void *data)
3605{
3606 struct btrfs_fs_info *fs_info = data;
3607 struct btrfs_root *root = fs_info->tree_root;
3608 struct btrfs_key key;
3609 struct btrfs_key max_key;
3610 struct btrfs_path *path = NULL;
3611 int ret = 0;
3612 struct extent_buffer *eb;
3613 int slot;
3614 struct btrfs_root_item root_item;
3615 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003616 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003617
3618 path = btrfs_alloc_path();
3619 if (!path) {
3620 ret = -ENOMEM;
3621 goto out;
3622 }
3623
3624 key.objectid = 0;
3625 key.type = BTRFS_ROOT_ITEM_KEY;
3626 key.offset = 0;
3627
3628 max_key.objectid = (u64)-1;
3629 max_key.type = BTRFS_ROOT_ITEM_KEY;
3630 max_key.offset = (u64)-1;
3631
Stefan Behrens803b2f52013-08-15 17:11:21 +02003632 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003633 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003634 if (ret) {
3635 if (ret > 0)
3636 ret = 0;
3637 break;
3638 }
3639
3640 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3641 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3642 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3643 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3644 goto skip;
3645
3646 eb = path->nodes[0];
3647 slot = path->slots[0];
3648 item_size = btrfs_item_size_nr(eb, slot);
3649 if (item_size < sizeof(root_item))
3650 goto skip;
3651
Stefan Behrens803b2f52013-08-15 17:11:21 +02003652 read_extent_buffer(eb, &root_item,
3653 btrfs_item_ptr_offset(eb, slot),
3654 (int)sizeof(root_item));
3655 if (btrfs_root_refs(&root_item) == 0)
3656 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003657
3658 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3659 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3660 if (trans)
3661 goto update_tree;
3662
3663 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003664 /*
3665 * 1 - subvol uuid item
3666 * 1 - received_subvol uuid item
3667 */
3668 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3669 if (IS_ERR(trans)) {
3670 ret = PTR_ERR(trans);
3671 break;
3672 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003673 continue;
3674 } else {
3675 goto skip;
3676 }
3677update_tree:
3678 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003679 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3680 root_item.uuid,
3681 BTRFS_UUID_KEY_SUBVOL,
3682 key.objectid);
3683 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003684 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003685 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003686 break;
3687 }
3688 }
3689
3690 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003691 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3692 root_item.received_uuid,
3693 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3694 key.objectid);
3695 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003696 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003697 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003698 break;
3699 }
3700 }
3701
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003702skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003703 if (trans) {
3704 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003705 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003706 if (ret)
3707 break;
3708 }
3709
Stefan Behrens803b2f52013-08-15 17:11:21 +02003710 btrfs_release_path(path);
3711 if (key.offset < (u64)-1) {
3712 key.offset++;
3713 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3714 key.offset = 0;
3715 key.type = BTRFS_ROOT_ITEM_KEY;
3716 } else if (key.objectid < (u64)-1) {
3717 key.offset = 0;
3718 key.type = BTRFS_ROOT_ITEM_KEY;
3719 key.objectid++;
3720 } else {
3721 break;
3722 }
3723 cond_resched();
3724 }
3725
3726out:
3727 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003728 if (trans && !IS_ERR(trans))
3729 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003730 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003731 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003732 else
3733 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003734 up(&fs_info->uuid_tree_rescan_sem);
3735 return 0;
3736}
3737
Stefan Behrens70f80172013-08-15 17:11:23 +02003738/*
3739 * Callback for btrfs_uuid_tree_iterate().
3740 * returns:
3741 * 0 check succeeded, the entry is not outdated.
3742 * < 0 if an error occured.
3743 * > 0 if the check failed, which means the caller shall remove the entry.
3744 */
3745static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3746 u8 *uuid, u8 type, u64 subid)
3747{
3748 struct btrfs_key key;
3749 int ret = 0;
3750 struct btrfs_root *subvol_root;
3751
3752 if (type != BTRFS_UUID_KEY_SUBVOL &&
3753 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3754 goto out;
3755
3756 key.objectid = subid;
3757 key.type = BTRFS_ROOT_ITEM_KEY;
3758 key.offset = (u64)-1;
3759 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3760 if (IS_ERR(subvol_root)) {
3761 ret = PTR_ERR(subvol_root);
3762 if (ret == -ENOENT)
3763 ret = 1;
3764 goto out;
3765 }
3766
3767 switch (type) {
3768 case BTRFS_UUID_KEY_SUBVOL:
3769 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3770 ret = 1;
3771 break;
3772 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3773 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3774 BTRFS_UUID_SIZE))
3775 ret = 1;
3776 break;
3777 }
3778
3779out:
3780 return ret;
3781}
3782
3783static int btrfs_uuid_rescan_kthread(void *data)
3784{
3785 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3786 int ret;
3787
3788 /*
3789 * 1st step is to iterate through the existing UUID tree and
3790 * to delete all entries that contain outdated data.
3791 * 2nd step is to add all missing entries to the UUID tree.
3792 */
3793 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3794 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003795 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003796 up(&fs_info->uuid_tree_rescan_sem);
3797 return ret;
3798 }
3799 return btrfs_uuid_scan_kthread(data);
3800}
3801
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003802int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3803{
3804 struct btrfs_trans_handle *trans;
3805 struct btrfs_root *tree_root = fs_info->tree_root;
3806 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003807 struct task_struct *task;
3808 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003809
3810 /*
3811 * 1 - root node
3812 * 1 - root item
3813 */
3814 trans = btrfs_start_transaction(tree_root, 2);
3815 if (IS_ERR(trans))
3816 return PTR_ERR(trans);
3817
3818 uuid_root = btrfs_create_tree(trans, fs_info,
3819 BTRFS_UUID_TREE_OBJECTID);
3820 if (IS_ERR(uuid_root)) {
3821 btrfs_abort_transaction(trans, tree_root,
3822 PTR_ERR(uuid_root));
3823 return PTR_ERR(uuid_root);
3824 }
3825
3826 fs_info->uuid_root = uuid_root;
3827
Stefan Behrens803b2f52013-08-15 17:11:21 +02003828 ret = btrfs_commit_transaction(trans, tree_root);
3829 if (ret)
3830 return ret;
3831
3832 down(&fs_info->uuid_tree_rescan_sem);
3833 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3834 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003835 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003836 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003837 up(&fs_info->uuid_tree_rescan_sem);
3838 return PTR_ERR(task);
3839 }
3840
3841 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003842}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003843
Stefan Behrens70f80172013-08-15 17:11:23 +02003844int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3845{
3846 struct task_struct *task;
3847
3848 down(&fs_info->uuid_tree_rescan_sem);
3849 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3850 if (IS_ERR(task)) {
3851 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003852 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003853 up(&fs_info->uuid_tree_rescan_sem);
3854 return PTR_ERR(task);
3855 }
3856
3857 return 0;
3858}
3859
Chris Mason8f18cf12008-04-25 16:53:30 -04003860/*
3861 * shrinking a device means finding all of the device extents past
3862 * the new size, and then following the back refs to the chunks.
3863 * The chunk relocation code actually frees the device extent
3864 */
3865int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3866{
3867 struct btrfs_trans_handle *trans;
3868 struct btrfs_root *root = device->dev_root;
3869 struct btrfs_dev_extent *dev_extent = NULL;
3870 struct btrfs_path *path;
3871 u64 length;
3872 u64 chunk_tree;
3873 u64 chunk_objectid;
3874 u64 chunk_offset;
3875 int ret;
3876 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003877 int failed = 0;
3878 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003879 struct extent_buffer *l;
3880 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003881 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003882 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003883 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003884 u64 diff = device->total_bytes - new_size;
3885
Stefan Behrens63a212a2012-11-05 18:29:28 +01003886 if (device->is_tgtdev_for_dev_replace)
3887 return -EINVAL;
3888
Chris Mason8f18cf12008-04-25 16:53:30 -04003889 path = btrfs_alloc_path();
3890 if (!path)
3891 return -ENOMEM;
3892
Chris Mason8f18cf12008-04-25 16:53:30 -04003893 path->reada = 2;
3894
Chris Mason7d9eb122008-07-08 14:19:17 -04003895 lock_chunks(root);
3896
Chris Mason8f18cf12008-04-25 16:53:30 -04003897 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003898 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003899 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003900 spin_lock(&root->fs_info->free_chunk_lock);
3901 root->fs_info->free_chunk_space -= diff;
3902 spin_unlock(&root->fs_info->free_chunk_lock);
3903 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003904 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003905
Josef Bacikba1bf482009-09-11 16:11:19 -04003906again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003907 key.objectid = device->devid;
3908 key.offset = (u64)-1;
3909 key.type = BTRFS_DEV_EXTENT_KEY;
3910
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003911 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003912 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3913 if (ret < 0)
3914 goto done;
3915
3916 ret = btrfs_previous_item(root, path, 0, key.type);
3917 if (ret < 0)
3918 goto done;
3919 if (ret) {
3920 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003921 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003922 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003923 }
3924
3925 l = path->nodes[0];
3926 slot = path->slots[0];
3927 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3928
Josef Bacikba1bf482009-09-11 16:11:19 -04003929 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003930 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003931 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003932 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003933
3934 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3935 length = btrfs_dev_extent_length(l, dev_extent);
3936
Josef Bacikba1bf482009-09-11 16:11:19 -04003937 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003938 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003939 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003940 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003941
3942 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3943 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3944 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003945 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003946
3947 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3948 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003949 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003950 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003951 if (ret == -ENOSPC)
3952 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003953 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003954
3955 if (failed && !retried) {
3956 failed = 0;
3957 retried = true;
3958 goto again;
3959 } else if (failed && retried) {
3960 ret = -ENOSPC;
3961 lock_chunks(root);
3962
3963 device->total_bytes = old_size;
3964 if (device->writeable)
3965 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003966 spin_lock(&root->fs_info->free_chunk_lock);
3967 root->fs_info->free_chunk_space += diff;
3968 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003969 unlock_chunks(root);
3970 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003971 }
3972
Chris Balld6397ba2009-04-27 07:29:03 -04003973 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003974 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003975 if (IS_ERR(trans)) {
3976 ret = PTR_ERR(trans);
3977 goto done;
3978 }
3979
Chris Balld6397ba2009-04-27 07:29:03 -04003980 lock_chunks(root);
3981
3982 device->disk_total_bytes = new_size;
3983 /* Now btrfs_update_device() will change the on-disk size. */
3984 ret = btrfs_update_device(trans, device);
3985 if (ret) {
3986 unlock_chunks(root);
3987 btrfs_end_transaction(trans, root);
3988 goto done;
3989 }
3990 WARN_ON(diff > old_total);
3991 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3992 unlock_chunks(root);
3993 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003994done:
3995 btrfs_free_path(path);
3996 return ret;
3997}
3998
Li Zefan125ccb02011-12-08 15:07:24 +08003999static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004000 struct btrfs_key *key,
4001 struct btrfs_chunk *chunk, int item_size)
4002{
David Sterba6c417612011-04-13 15:41:04 +02004003 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004004 struct btrfs_disk_key disk_key;
4005 u32 array_size;
4006 u8 *ptr;
4007
4008 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004009 if (array_size + item_size + sizeof(disk_key)
4010 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
Chris Mason0b86a832008-03-24 15:01:56 -04004011 return -EFBIG;
4012
4013 ptr = super_copy->sys_chunk_array + array_size;
4014 btrfs_cpu_key_to_disk(&disk_key, key);
4015 memcpy(ptr, &disk_key, sizeof(disk_key));
4016 ptr += sizeof(disk_key);
4017 memcpy(ptr, chunk, item_size);
4018 item_size += sizeof(disk_key);
4019 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4020 return 0;
4021}
4022
Miao Xieb2117a32011-01-05 10:07:28 +00004023/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004024 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004025 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004026static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004027{
Arne Jansen73c5de02011-04-12 12:07:57 +02004028 const struct btrfs_device_info *di_a = a;
4029 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004030
Arne Jansen73c5de02011-04-12 12:07:57 +02004031 if (di_a->max_avail > di_b->max_avail)
4032 return -1;
4033 if (di_a->max_avail < di_b->max_avail)
4034 return 1;
4035 if (di_a->total_avail > di_b->total_avail)
4036 return -1;
4037 if (di_a->total_avail < di_b->total_avail)
4038 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004039 return 0;
4040}
4041
Eric Sandeen48a3b632013-04-25 20:41:01 +00004042static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004043 [BTRFS_RAID_RAID10] = {
4044 .sub_stripes = 2,
4045 .dev_stripes = 1,
4046 .devs_max = 0, /* 0 == as many as possible */
4047 .devs_min = 4,
4048 .devs_increment = 2,
4049 .ncopies = 2,
4050 },
4051 [BTRFS_RAID_RAID1] = {
4052 .sub_stripes = 1,
4053 .dev_stripes = 1,
4054 .devs_max = 2,
4055 .devs_min = 2,
4056 .devs_increment = 2,
4057 .ncopies = 2,
4058 },
4059 [BTRFS_RAID_DUP] = {
4060 .sub_stripes = 1,
4061 .dev_stripes = 2,
4062 .devs_max = 1,
4063 .devs_min = 1,
4064 .devs_increment = 1,
4065 .ncopies = 2,
4066 },
4067 [BTRFS_RAID_RAID0] = {
4068 .sub_stripes = 1,
4069 .dev_stripes = 1,
4070 .devs_max = 0,
4071 .devs_min = 2,
4072 .devs_increment = 1,
4073 .ncopies = 1,
4074 },
4075 [BTRFS_RAID_SINGLE] = {
4076 .sub_stripes = 1,
4077 .dev_stripes = 1,
4078 .devs_max = 1,
4079 .devs_min = 1,
4080 .devs_increment = 1,
4081 .ncopies = 1,
4082 },
Chris Masone942f882013-02-20 14:06:05 -05004083 [BTRFS_RAID_RAID5] = {
4084 .sub_stripes = 1,
4085 .dev_stripes = 1,
4086 .devs_max = 0,
4087 .devs_min = 2,
4088 .devs_increment = 1,
4089 .ncopies = 2,
4090 },
4091 [BTRFS_RAID_RAID6] = {
4092 .sub_stripes = 1,
4093 .dev_stripes = 1,
4094 .devs_max = 0,
4095 .devs_min = 3,
4096 .devs_increment = 1,
4097 .ncopies = 3,
4098 },
Liu Bo31e50222012-11-21 14:18:10 +00004099};
4100
David Woodhouse53b381b2013-01-29 18:40:14 -05004101static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4102{
4103 /* TODO allow them to set a preferred stripe size */
4104 return 64 * 1024;
4105}
4106
4107static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4108{
David Woodhouse53b381b2013-01-29 18:40:14 -05004109 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
4110 return;
4111
Miao Xieceda0862013-04-11 10:30:16 +00004112 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004113}
4114
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004115#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4116 - sizeof(struct btrfs_item) \
4117 - sizeof(struct btrfs_chunk)) \
4118 / sizeof(struct btrfs_stripe) + 1)
4119
4120#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4121 - 2 * sizeof(struct btrfs_disk_key) \
4122 - 2 * sizeof(struct btrfs_chunk)) \
4123 / sizeof(struct btrfs_stripe) + 1)
4124
Miao Xieb2117a32011-01-05 10:07:28 +00004125static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004126 struct btrfs_root *extent_root, u64 start,
4127 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004128{
4129 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004130 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4131 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004132 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004133 struct extent_map_tree *em_tree;
4134 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004135 struct btrfs_device_info *devices_info = NULL;
4136 u64 total_avail;
4137 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004138 int data_stripes; /* number of stripes that count for
4139 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004140 int sub_stripes; /* sub_stripes info for map */
4141 int dev_stripes; /* stripes per dev */
4142 int devs_max; /* max devs to use */
4143 int devs_min; /* min devs needed */
4144 int devs_increment; /* ndevs has to be a multiple of this */
4145 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004146 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004147 u64 max_stripe_size;
4148 u64 max_chunk_size;
4149 u64 stripe_size;
4150 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004151 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004152 int ndevs;
4153 int i;
4154 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004155 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004156
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004157 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004158
Miao Xieb2117a32011-01-05 10:07:28 +00004159 if (list_empty(&fs_devices->alloc_list))
4160 return -ENOSPC;
4161
Liu Bo31e50222012-11-21 14:18:10 +00004162 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004163
Liu Bo31e50222012-11-21 14:18:10 +00004164 sub_stripes = btrfs_raid_array[index].sub_stripes;
4165 dev_stripes = btrfs_raid_array[index].dev_stripes;
4166 devs_max = btrfs_raid_array[index].devs_max;
4167 devs_min = btrfs_raid_array[index].devs_min;
4168 devs_increment = btrfs_raid_array[index].devs_increment;
4169 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004170
4171 if (type & BTRFS_BLOCK_GROUP_DATA) {
4172 max_stripe_size = 1024 * 1024 * 1024;
4173 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004174 if (!devs_max)
4175 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004176 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004177 /* for larger filesystems, use larger metadata chunks */
4178 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4179 max_stripe_size = 1024 * 1024 * 1024;
4180 else
4181 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004182 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004183 if (!devs_max)
4184 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004185 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004186 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004187 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004188 if (!devs_max)
4189 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004190 } else {
David Sterba351fd352014-05-15 16:48:20 +02004191 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004192 type);
4193 BUG_ON(1);
4194 }
4195
4196 /* we don't want a chunk larger than 10% of writeable space */
4197 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4198 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004199
4200 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4201 GFP_NOFS);
4202 if (!devices_info)
4203 return -ENOMEM;
4204
Arne Jansen73c5de02011-04-12 12:07:57 +02004205 cur = fs_devices->alloc_list.next;
4206
4207 /*
4208 * in the first pass through the devices list, we gather information
4209 * about the available holes on each device.
4210 */
4211 ndevs = 0;
4212 while (cur != &fs_devices->alloc_list) {
4213 struct btrfs_device *device;
4214 u64 max_avail;
4215 u64 dev_offset;
4216
4217 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4218
4219 cur = cur->next;
4220
4221 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004222 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004223 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004224 continue;
4225 }
4226
Stefan Behrens63a212a2012-11-05 18:29:28 +01004227 if (!device->in_fs_metadata ||
4228 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004229 continue;
4230
4231 if (device->total_bytes > device->bytes_used)
4232 total_avail = device->total_bytes - device->bytes_used;
4233 else
4234 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004235
4236 /* If there is no space on this device, skip it. */
4237 if (total_avail == 0)
4238 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004239
Josef Bacik6df9a952013-06-27 13:22:46 -04004240 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004241 max_stripe_size * dev_stripes,
4242 &dev_offset, &max_avail);
4243 if (ret && ret != -ENOSPC)
4244 goto error;
4245
4246 if (ret == 0)
4247 max_avail = max_stripe_size * dev_stripes;
4248
4249 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4250 continue;
4251
Eric Sandeen063d0062013-01-31 00:55:01 +00004252 if (ndevs == fs_devices->rw_devices) {
4253 WARN(1, "%s: found more than %llu devices\n",
4254 __func__, fs_devices->rw_devices);
4255 break;
4256 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004257 devices_info[ndevs].dev_offset = dev_offset;
4258 devices_info[ndevs].max_avail = max_avail;
4259 devices_info[ndevs].total_avail = total_avail;
4260 devices_info[ndevs].dev = device;
4261 ++ndevs;
4262 }
4263
4264 /*
4265 * now sort the devices by hole size / available space
4266 */
4267 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4268 btrfs_cmp_device_info, NULL);
4269
4270 /* round down to number of usable stripes */
4271 ndevs -= ndevs % devs_increment;
4272
4273 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4274 ret = -ENOSPC;
4275 goto error;
4276 }
4277
4278 if (devs_max && ndevs > devs_max)
4279 ndevs = devs_max;
4280 /*
4281 * the primary goal is to maximize the number of stripes, so use as many
4282 * devices as possible, even if the stripes are not maximum sized.
4283 */
4284 stripe_size = devices_info[ndevs-1].max_avail;
4285 num_stripes = ndevs * dev_stripes;
4286
David Woodhouse53b381b2013-01-29 18:40:14 -05004287 /*
4288 * this will have to be fixed for RAID1 and RAID10 over
4289 * more drives
4290 */
4291 data_stripes = num_stripes / ncopies;
4292
David Woodhouse53b381b2013-01-29 18:40:14 -05004293 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4294 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4295 btrfs_super_stripesize(info->super_copy));
4296 data_stripes = num_stripes - 1;
4297 }
4298 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4299 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4300 btrfs_super_stripesize(info->super_copy));
4301 data_stripes = num_stripes - 2;
4302 }
Chris Mason86db2572013-02-20 16:23:40 -05004303
4304 /*
4305 * Use the number of data stripes to figure out how big this chunk
4306 * is really going to be in terms of logical address space,
4307 * and compare that answer with the max chunk size
4308 */
4309 if (stripe_size * data_stripes > max_chunk_size) {
4310 u64 mask = (1ULL << 24) - 1;
4311 stripe_size = max_chunk_size;
4312 do_div(stripe_size, data_stripes);
4313
4314 /* bump the answer up to a 16MB boundary */
4315 stripe_size = (stripe_size + mask) & ~mask;
4316
4317 /* but don't go higher than the limits we found
4318 * while searching for free extents
4319 */
4320 if (stripe_size > devices_info[ndevs-1].max_avail)
4321 stripe_size = devices_info[ndevs-1].max_avail;
4322 }
4323
Arne Jansen73c5de02011-04-12 12:07:57 +02004324 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004325
4326 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004327 do_div(stripe_size, raid_stripe_len);
4328 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004329
Miao Xieb2117a32011-01-05 10:07:28 +00004330 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4331 if (!map) {
4332 ret = -ENOMEM;
4333 goto error;
4334 }
4335 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004336
Arne Jansen73c5de02011-04-12 12:07:57 +02004337 for (i = 0; i < ndevs; ++i) {
4338 for (j = 0; j < dev_stripes; ++j) {
4339 int s = i * dev_stripes + j;
4340 map->stripes[s].dev = devices_info[i].dev;
4341 map->stripes[s].physical = devices_info[i].dev_offset +
4342 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004343 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004344 }
Chris Mason593060d2008-03-25 16:50:33 -04004345 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004346 map->stripe_len = raid_stripe_len;
4347 map->io_align = raid_stripe_len;
4348 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004349 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004350 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004351
David Woodhouse53b381b2013-01-29 18:40:14 -05004352 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004353
Arne Jansen73c5de02011-04-12 12:07:57 +02004354 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004355
David Sterba172ddd62011-04-21 00:48:27 +02004356 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004357 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004358 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004359 ret = -ENOMEM;
4360 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004361 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004362 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004363 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004364 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004365 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004366 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004367 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004368 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004369
Chris Mason0b86a832008-03-24 15:01:56 -04004370 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004371 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004372 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004373 if (!ret) {
4374 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4375 atomic_inc(&em->refs);
4376 }
Chris Mason890871b2009-09-02 16:24:52 -04004377 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004378 if (ret) {
4379 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004380 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004381 }
Yan Zheng2b820322008-11-17 21:11:30 -05004382
Josef Bacik04487482013-01-29 15:03:37 -05004383 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4384 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4385 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004386 if (ret)
4387 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004388
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004389 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004390 check_raid56_incompat_flag(extent_root->fs_info, type);
4391
Miao Xieb2117a32011-01-05 10:07:28 +00004392 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004393 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004394
Josef Bacik6df9a952013-06-27 13:22:46 -04004395error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004396 write_lock(&em_tree->lock);
4397 remove_extent_mapping(em_tree, em);
4398 write_unlock(&em_tree->lock);
4399
4400 /* One for our allocation */
4401 free_extent_map(em);
4402 /* One for the tree reference */
4403 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004404error:
Miao Xieb2117a32011-01-05 10:07:28 +00004405 kfree(devices_info);
4406 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004407}
4408
Josef Bacik6df9a952013-06-27 13:22:46 -04004409int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004410 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004411 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004412{
Yan Zheng2b820322008-11-17 21:11:30 -05004413 struct btrfs_key key;
4414 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4415 struct btrfs_device *device;
4416 struct btrfs_chunk *chunk;
4417 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004418 struct extent_map_tree *em_tree;
4419 struct extent_map *em;
4420 struct map_lookup *map;
4421 size_t item_size;
4422 u64 dev_offset;
4423 u64 stripe_size;
4424 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004425 int ret;
4426
Josef Bacik6df9a952013-06-27 13:22:46 -04004427 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4428 read_lock(&em_tree->lock);
4429 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4430 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004431
Josef Bacik6df9a952013-06-27 13:22:46 -04004432 if (!em) {
4433 btrfs_crit(extent_root->fs_info, "unable to find logical "
4434 "%Lu len %Lu", chunk_offset, chunk_size);
4435 return -EINVAL;
4436 }
4437
4438 if (em->start != chunk_offset || em->len != chunk_size) {
4439 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004440 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004441 chunk_size, em->start, em->len);
4442 free_extent_map(em);
4443 return -EINVAL;
4444 }
4445
4446 map = (struct map_lookup *)em->bdev;
4447 item_size = btrfs_chunk_item_size(map->num_stripes);
4448 stripe_size = em->orig_block_len;
4449
4450 chunk = kzalloc(item_size, GFP_NOFS);
4451 if (!chunk) {
4452 ret = -ENOMEM;
4453 goto out;
4454 }
4455
4456 for (i = 0; i < map->num_stripes; i++) {
4457 device = map->stripes[i].dev;
4458 dev_offset = map->stripes[i].physical;
4459
Yan Zheng2b820322008-11-17 21:11:30 -05004460 device->bytes_used += stripe_size;
4461 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004462 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004463 goto out;
4464 ret = btrfs_alloc_dev_extent(trans, device,
4465 chunk_root->root_key.objectid,
4466 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4467 chunk_offset, dev_offset,
4468 stripe_size);
4469 if (ret)
4470 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004471 }
4472
Josef Bacik2bf64752011-09-26 17:12:22 -04004473 spin_lock(&extent_root->fs_info->free_chunk_lock);
4474 extent_root->fs_info->free_chunk_space -= (stripe_size *
4475 map->num_stripes);
4476 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4477
Yan Zheng2b820322008-11-17 21:11:30 -05004478 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004479 for (i = 0; i < map->num_stripes; i++) {
4480 device = map->stripes[i].dev;
4481 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004482
4483 btrfs_set_stack_stripe_devid(stripe, device->devid);
4484 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4485 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4486 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004487 }
4488
4489 btrfs_set_stack_chunk_length(chunk, chunk_size);
4490 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4491 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4492 btrfs_set_stack_chunk_type(chunk, map->type);
4493 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4494 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4495 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4496 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4497 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4498
4499 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4500 key.type = BTRFS_CHUNK_ITEM_KEY;
4501 key.offset = chunk_offset;
4502
4503 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004504 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4505 /*
4506 * TODO: Cleanup of inserted chunk root in case of
4507 * failure.
4508 */
Li Zefan125ccb02011-12-08 15:07:24 +08004509 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004510 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004511 }
liubo1abe9b82011-03-24 11:18:59 +00004512
Josef Bacik6df9a952013-06-27 13:22:46 -04004513out:
Yan Zheng2b820322008-11-17 21:11:30 -05004514 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004515 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004516 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004517}
4518
4519/*
4520 * Chunk allocation falls into two parts. The first part does works
4521 * that make the new allocated chunk useable, but not do any operation
4522 * that modifies the chunk tree. The second part does the works that
4523 * require modifying the chunk tree. This division is important for the
4524 * bootstrap process of adding storage to a seed btrfs.
4525 */
4526int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4527 struct btrfs_root *extent_root, u64 type)
4528{
4529 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004530
Josef Bacik6df9a952013-06-27 13:22:46 -04004531 chunk_offset = find_next_chunk(extent_root->fs_info);
4532 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004533}
4534
Chris Masond3977122009-01-05 21:25:51 -05004535static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004536 struct btrfs_root *root,
4537 struct btrfs_device *device)
4538{
4539 u64 chunk_offset;
4540 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004541 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004542 struct btrfs_fs_info *fs_info = root->fs_info;
4543 struct btrfs_root *extent_root = fs_info->extent_root;
4544 int ret;
4545
Josef Bacik6df9a952013-06-27 13:22:46 -04004546 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004547 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004548 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4549 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004550 if (ret)
4551 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004552
Josef Bacik6df9a952013-06-27 13:22:46 -04004553 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004554 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004555 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4556 alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004557 if (ret) {
4558 btrfs_abort_transaction(trans, root, ret);
4559 goto out;
4560 }
Yan Zheng2b820322008-11-17 21:11:30 -05004561
4562 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004563 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004564 btrfs_abort_transaction(trans, root, ret);
David Sterba005d6422012-09-18 07:52:32 -06004565out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004566 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004567}
4568
Miao Xied20983b2014-07-03 18:22:13 +08004569static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4570{
4571 int max_errors;
4572
4573 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4574 BTRFS_BLOCK_GROUP_RAID10 |
4575 BTRFS_BLOCK_GROUP_RAID5 |
4576 BTRFS_BLOCK_GROUP_DUP)) {
4577 max_errors = 1;
4578 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4579 max_errors = 2;
4580 } else {
4581 max_errors = 0;
4582 }
4583
4584 return max_errors;
4585}
4586
Yan Zheng2b820322008-11-17 21:11:30 -05004587int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4588{
4589 struct extent_map *em;
4590 struct map_lookup *map;
4591 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4592 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004593 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004594 int i;
4595
Chris Mason890871b2009-09-02 16:24:52 -04004596 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004597 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004598 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004599 if (!em)
4600 return 1;
4601
4602 map = (struct map_lookup *)em->bdev;
4603 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004604 if (map->stripes[i].dev->missing) {
4605 miss_ndevs++;
4606 continue;
4607 }
4608
Yan Zheng2b820322008-11-17 21:11:30 -05004609 if (!map->stripes[i].dev->writeable) {
4610 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004611 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004612 }
4613 }
Miao Xied20983b2014-07-03 18:22:13 +08004614
4615 /*
4616 * If the number of missing devices is larger than max errors,
4617 * we can not write the data into that chunk successfully, so
4618 * set it readonly.
4619 */
4620 if (miss_ndevs > btrfs_chunk_max_errors(map))
4621 readonly = 1;
4622end:
Yan Zheng2b820322008-11-17 21:11:30 -05004623 free_extent_map(em);
4624 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004625}
4626
4627void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4628{
David Sterbaa8067e02011-04-21 00:34:43 +02004629 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004630}
4631
4632void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4633{
4634 struct extent_map *em;
4635
Chris Masond3977122009-01-05 21:25:51 -05004636 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004637 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004638 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4639 if (em)
4640 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004641 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004642 if (!em)
4643 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004644 /* once for us */
4645 free_extent_map(em);
4646 /* once for the tree */
4647 free_extent_map(em);
4648 }
4649}
4650
Stefan Behrens5d964052012-11-05 14:59:07 +01004651int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004652{
Stefan Behrens5d964052012-11-05 14:59:07 +01004653 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004654 struct extent_map *em;
4655 struct map_lookup *map;
4656 struct extent_map_tree *em_tree = &map_tree->map_tree;
4657 int ret;
4658
Chris Mason890871b2009-09-02 16:24:52 -04004659 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004660 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004661 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004662
Josef Bacikfb7669b2013-04-23 10:53:18 -04004663 /*
4664 * We could return errors for these cases, but that could get ugly and
4665 * we'd probably do the same thing which is just not do anything else
4666 * and exit, so return 1 so the callers don't try to use other copies.
4667 */
4668 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004669 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004670 logical+len);
4671 return 1;
4672 }
4673
4674 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004675 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004676 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004677 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004678 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004679 return 1;
4680 }
4681
Chris Masonf1885912008-04-09 16:28:12 -04004682 map = (struct map_lookup *)em->bdev;
4683 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4684 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004685 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4686 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004687 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4688 ret = 2;
4689 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4690 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004691 else
4692 ret = 1;
4693 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004694
4695 btrfs_dev_replace_lock(&fs_info->dev_replace);
4696 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4697 ret++;
4698 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4699
Chris Masonf1885912008-04-09 16:28:12 -04004700 return ret;
4701}
4702
David Woodhouse53b381b2013-01-29 18:40:14 -05004703unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4704 struct btrfs_mapping_tree *map_tree,
4705 u64 logical)
4706{
4707 struct extent_map *em;
4708 struct map_lookup *map;
4709 struct extent_map_tree *em_tree = &map_tree->map_tree;
4710 unsigned long len = root->sectorsize;
4711
4712 read_lock(&em_tree->lock);
4713 em = lookup_extent_mapping(em_tree, logical, len);
4714 read_unlock(&em_tree->lock);
4715 BUG_ON(!em);
4716
4717 BUG_ON(em->start > logical || em->start + em->len < logical);
4718 map = (struct map_lookup *)em->bdev;
4719 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4720 BTRFS_BLOCK_GROUP_RAID6)) {
4721 len = map->stripe_len * nr_data_stripes(map);
4722 }
4723 free_extent_map(em);
4724 return len;
4725}
4726
4727int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4728 u64 logical, u64 len, int mirror_num)
4729{
4730 struct extent_map *em;
4731 struct map_lookup *map;
4732 struct extent_map_tree *em_tree = &map_tree->map_tree;
4733 int ret = 0;
4734
4735 read_lock(&em_tree->lock);
4736 em = lookup_extent_mapping(em_tree, logical, len);
4737 read_unlock(&em_tree->lock);
4738 BUG_ON(!em);
4739
4740 BUG_ON(em->start > logical || em->start + em->len < logical);
4741 map = (struct map_lookup *)em->bdev;
4742 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4743 BTRFS_BLOCK_GROUP_RAID6))
4744 ret = 1;
4745 free_extent_map(em);
4746 return ret;
4747}
4748
Stefan Behrens30d98612012-11-06 14:52:18 +01004749static int find_live_mirror(struct btrfs_fs_info *fs_info,
4750 struct map_lookup *map, int first, int num,
4751 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004752{
4753 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004754 int tolerance;
4755 struct btrfs_device *srcdev;
4756
4757 if (dev_replace_is_ongoing &&
4758 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4759 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4760 srcdev = fs_info->dev_replace.srcdev;
4761 else
4762 srcdev = NULL;
4763
4764 /*
4765 * try to avoid the drive that is the source drive for a
4766 * dev-replace procedure, only choose it if no other non-missing
4767 * mirror is available
4768 */
4769 for (tolerance = 0; tolerance < 2; tolerance++) {
4770 if (map->stripes[optimal].dev->bdev &&
4771 (tolerance || map->stripes[optimal].dev != srcdev))
4772 return optimal;
4773 for (i = first; i < first + num; i++) {
4774 if (map->stripes[i].dev->bdev &&
4775 (tolerance || map->stripes[i].dev != srcdev))
4776 return i;
4777 }
Chris Masondfe25022008-05-13 13:46:40 -04004778 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004779
Chris Masondfe25022008-05-13 13:46:40 -04004780 /* we couldn't find one that doesn't fail. Just return something
4781 * and the io error handling code will clean up eventually
4782 */
4783 return optimal;
4784}
4785
David Woodhouse53b381b2013-01-29 18:40:14 -05004786static inline int parity_smaller(u64 a, u64 b)
4787{
4788 return a > b;
4789}
4790
4791/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4792static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4793{
4794 struct btrfs_bio_stripe s;
4795 int i;
4796 u64 l;
4797 int again = 1;
4798
4799 while (again) {
4800 again = 0;
4801 for (i = 0; i < bbio->num_stripes - 1; i++) {
4802 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4803 s = bbio->stripes[i];
4804 l = raid_map[i];
4805 bbio->stripes[i] = bbio->stripes[i+1];
4806 raid_map[i] = raid_map[i+1];
4807 bbio->stripes[i+1] = s;
4808 raid_map[i+1] = l;
4809 again = 1;
4810 }
4811 }
4812 }
4813}
4814
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004815static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004816 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004817 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004818 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004819{
4820 struct extent_map *em;
4821 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004822 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004823 struct extent_map_tree *em_tree = &map_tree->map_tree;
4824 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004825 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004826 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004827 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004828 u64 stripe_nr_orig;
4829 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004830 u64 stripe_len;
4831 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004832 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004833 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004834 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004835 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004836 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004837 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004838 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4839 int dev_replace_is_ongoing = 0;
4840 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004841 int patch_the_first_stripe_for_dev_replace = 0;
4842 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004843 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004844
Chris Mason890871b2009-09-02 16:24:52 -04004845 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004846 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004847 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004848
Chris Mason3b951512008-04-17 11:29:12 -04004849 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004850 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004851 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004852 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004853 }
Chris Mason0b86a832008-03-24 15:01:56 -04004854
Josef Bacik9bb91872013-04-19 23:45:33 -04004855 if (em->start > logical || em->start + em->len < logical) {
4856 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004857 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004858 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004859 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004860 return -EINVAL;
4861 }
4862
Chris Mason0b86a832008-03-24 15:01:56 -04004863 map = (struct map_lookup *)em->bdev;
4864 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004865
David Woodhouse53b381b2013-01-29 18:40:14 -05004866 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004867 stripe_nr = offset;
4868 /*
4869 * stripe_nr counts the total number of stripes we have to stride
4870 * to get to this block
4871 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004872 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004873
David Woodhouse53b381b2013-01-29 18:40:14 -05004874 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004875 BUG_ON(offset < stripe_offset);
4876
4877 /* stripe_offset is the offset of this block in its stripe*/
4878 stripe_offset = offset - stripe_offset;
4879
David Woodhouse53b381b2013-01-29 18:40:14 -05004880 /* if we're here for raid56, we need to know the stripe aligned start */
4881 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4882 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4883 raid56_full_stripe_start = offset;
4884
4885 /* allow a write of a full stripe, but make sure we don't
4886 * allow straddling of stripes
4887 */
4888 do_div(raid56_full_stripe_start, full_stripe_len);
4889 raid56_full_stripe_start *= full_stripe_len;
4890 }
4891
4892 if (rw & REQ_DISCARD) {
4893 /* we don't discard raid56 yet */
4894 if (map->type &
4895 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4896 ret = -EOPNOTSUPP;
4897 goto out;
4898 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004899 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004900 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4901 u64 max_len;
4902 /* For writes to RAID[56], allow a full stripeset across all disks.
4903 For other RAID types and for RAID[56] reads, just allow a single
4904 stripe (on a single disk). */
4905 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4906 (rw & REQ_WRITE)) {
4907 max_len = stripe_len * nr_data_stripes(map) -
4908 (offset - raid56_full_stripe_start);
4909 } else {
4910 /* we limit the length of each bio to what fits in a stripe */
4911 max_len = stripe_len - stripe_offset;
4912 }
4913 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004914 } else {
4915 *length = em->len - offset;
4916 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004917
David Woodhouse53b381b2013-01-29 18:40:14 -05004918 /* This is for when we're called from btrfs_merge_bio_hook() and all
4919 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004920 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004921 goto out;
4922
Stefan Behrens472262f2012-11-06 14:43:46 +01004923 btrfs_dev_replace_lock(dev_replace);
4924 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4925 if (!dev_replace_is_ongoing)
4926 btrfs_dev_replace_unlock(dev_replace);
4927
Stefan Behrensad6d6202012-11-06 15:06:47 +01004928 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4929 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4930 dev_replace->tgtdev != NULL) {
4931 /*
4932 * in dev-replace case, for repair case (that's the only
4933 * case where the mirror is selected explicitly when
4934 * calling btrfs_map_block), blocks left of the left cursor
4935 * can also be read from the target drive.
4936 * For REQ_GET_READ_MIRRORS, the target drive is added as
4937 * the last one to the array of stripes. For READ, it also
4938 * needs to be supported using the same mirror number.
4939 * If the requested block is not left of the left cursor,
4940 * EIO is returned. This can happen because btrfs_num_copies()
4941 * returns one more in the dev-replace case.
4942 */
4943 u64 tmp_length = *length;
4944 struct btrfs_bio *tmp_bbio = NULL;
4945 int tmp_num_stripes;
4946 u64 srcdev_devid = dev_replace->srcdev->devid;
4947 int index_srcdev = 0;
4948 int found = 0;
4949 u64 physical_of_found = 0;
4950
4951 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004952 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004953 if (ret) {
4954 WARN_ON(tmp_bbio != NULL);
4955 goto out;
4956 }
4957
4958 tmp_num_stripes = tmp_bbio->num_stripes;
4959 if (mirror_num > tmp_num_stripes) {
4960 /*
4961 * REQ_GET_READ_MIRRORS does not contain this
4962 * mirror, that means that the requested area
4963 * is not left of the left cursor
4964 */
4965 ret = -EIO;
4966 kfree(tmp_bbio);
4967 goto out;
4968 }
4969
4970 /*
4971 * process the rest of the function using the mirror_num
4972 * of the source drive. Therefore look it up first.
4973 * At the end, patch the device pointer to the one of the
4974 * target drive.
4975 */
4976 for (i = 0; i < tmp_num_stripes; i++) {
4977 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4978 /*
4979 * In case of DUP, in order to keep it
4980 * simple, only add the mirror with the
4981 * lowest physical address
4982 */
4983 if (found &&
4984 physical_of_found <=
4985 tmp_bbio->stripes[i].physical)
4986 continue;
4987 index_srcdev = i;
4988 found = 1;
4989 physical_of_found =
4990 tmp_bbio->stripes[i].physical;
4991 }
4992 }
4993
4994 if (found) {
4995 mirror_num = index_srcdev + 1;
4996 patch_the_first_stripe_for_dev_replace = 1;
4997 physical_to_patch_in_first_stripe = physical_of_found;
4998 } else {
4999 WARN_ON(1);
5000 ret = -EIO;
5001 kfree(tmp_bbio);
5002 goto out;
5003 }
5004
5005 kfree(tmp_bbio);
5006 } else if (mirror_num > map->num_stripes) {
5007 mirror_num = 0;
5008 }
5009
Chris Masonf2d8d742008-04-21 10:03:05 -04005010 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005011 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005012 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005013 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005014 do_div(stripe_nr_end, map->stripe_len);
5015 stripe_end_offset = stripe_nr_end * map->stripe_len -
5016 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005017
Li Dongyangfce3bb92011-03-24 10:24:26 +00005018 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5019 if (rw & REQ_DISCARD)
5020 num_stripes = min_t(u64, map->num_stripes,
5021 stripe_nr_end - stripe_nr_orig);
5022 stripe_index = do_div(stripe_nr, map->num_stripes);
5023 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005024 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005025 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005026 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005027 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005028 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005029 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005030 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005031 current->pid % map->num_stripes,
5032 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005033 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005034 }
Chris Mason2fff7342008-04-29 14:12:09 -04005035
Chris Mason611f0e02008-04-03 16:29:03 -04005036 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005037 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005038 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005039 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005040 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005041 } else {
5042 mirror_num = 1;
5043 }
Chris Mason2fff7342008-04-29 14:12:09 -04005044
Chris Mason321aecc2008-04-16 10:49:51 -04005045 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5046 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005047
5048 stripe_index = do_div(stripe_nr, factor);
5049 stripe_index *= map->sub_stripes;
5050
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005051 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005052 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005053 else if (rw & REQ_DISCARD)
5054 num_stripes = min_t(u64, map->sub_stripes *
5055 (stripe_nr_end - stripe_nr_orig),
5056 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005057 else if (mirror_num)
5058 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005059 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005060 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005061 stripe_index = find_live_mirror(fs_info, map,
5062 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005063 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005064 current->pid % map->sub_stripes,
5065 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005066 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005067 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005068
5069 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5070 BTRFS_BLOCK_GROUP_RAID6)) {
5071 u64 tmp;
5072
5073 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
5074 && raid_map_ret) {
5075 int i, rot;
5076
5077 /* push stripe_nr back to the start of the full stripe */
5078 stripe_nr = raid56_full_stripe_start;
5079 do_div(stripe_nr, stripe_len);
5080
5081 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5082
5083 /* RAID[56] write or recovery. Return all stripes */
5084 num_stripes = map->num_stripes;
5085 max_errors = nr_parity_stripes(map);
5086
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05305087 raid_map = kmalloc_array(num_stripes, sizeof(u64),
David Woodhouse53b381b2013-01-29 18:40:14 -05005088 GFP_NOFS);
5089 if (!raid_map) {
5090 ret = -ENOMEM;
5091 goto out;
5092 }
5093
5094 /* Work out the disk rotation on this stripe-set */
5095 tmp = stripe_nr;
5096 rot = do_div(tmp, num_stripes);
5097
5098 /* Fill in the logical address of each stripe */
5099 tmp = stripe_nr * nr_data_stripes(map);
5100 for (i = 0; i < nr_data_stripes(map); i++)
5101 raid_map[(i+rot) % num_stripes] =
5102 em->start + (tmp + i) * map->stripe_len;
5103
5104 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5105 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5106 raid_map[(i+rot+1) % num_stripes] =
5107 RAID6_Q_STRIPE;
5108
5109 *length = map->stripe_len;
5110 stripe_index = 0;
5111 stripe_offset = 0;
5112 } else {
5113 /*
5114 * Mirror #0 or #1 means the original data block.
5115 * Mirror #2 is RAID5 parity block.
5116 * Mirror #3 is RAID6 Q block.
5117 */
5118 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5119 if (mirror_num > 1)
5120 stripe_index = nr_data_stripes(map) +
5121 mirror_num - 2;
5122
5123 /* We distribute the parity blocks across stripes */
5124 tmp = stripe_nr + stripe_index;
5125 stripe_index = do_div(tmp, map->num_stripes);
5126 }
Chris Mason8790d502008-04-03 16:29:03 -04005127 } else {
5128 /*
5129 * after this do_div call, stripe_nr is the number of stripes
5130 * on this device we have to walk to find the data, and
5131 * stripe_index is the number of our device in the stripe array
5132 */
5133 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005134 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005135 }
Chris Mason593060d2008-03-25 16:50:33 -04005136 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005137
Stefan Behrens472262f2012-11-06 14:43:46 +01005138 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005139 if (dev_replace_is_ongoing) {
5140 if (rw & (REQ_WRITE | REQ_DISCARD))
5141 num_alloc_stripes <<= 1;
5142 if (rw & REQ_GET_READ_MIRRORS)
5143 num_alloc_stripes++;
5144 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005145 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08005146 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04005147 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08005148 ret = -ENOMEM;
5149 goto out;
5150 }
5151 atomic_set(&bbio->error, 0);
5152
Li Dongyangfce3bb92011-03-24 10:24:26 +00005153 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08005154 int factor = 0;
5155 int sub_stripes = 0;
5156 u64 stripes_per_dev = 0;
5157 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005158 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005159
5160 if (map->type &
5161 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5162 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5163 sub_stripes = 1;
5164 else
5165 sub_stripes = map->sub_stripes;
5166
5167 factor = map->num_stripes / sub_stripes;
5168 stripes_per_dev = div_u64_rem(stripe_nr_end -
5169 stripe_nr_orig,
5170 factor,
5171 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005172 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5173 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005174 }
5175
Li Dongyangfce3bb92011-03-24 10:24:26 +00005176 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005177 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005178 map->stripes[stripe_index].physical +
5179 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005180 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005181
Li Zefanec9ef7a2011-12-01 14:06:42 +08005182 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5183 BTRFS_BLOCK_GROUP_RAID10)) {
5184 bbio->stripes[i].length = stripes_per_dev *
5185 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005186
Li Zefanec9ef7a2011-12-01 14:06:42 +08005187 if (i / sub_stripes < remaining_stripes)
5188 bbio->stripes[i].length +=
5189 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005190
5191 /*
5192 * Special for the first stripe and
5193 * the last stripe:
5194 *
5195 * |-------|...|-------|
5196 * |----------|
5197 * off end_off
5198 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005199 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005200 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005201 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005202
5203 if (stripe_index >= last_stripe &&
5204 stripe_index <= (last_stripe +
5205 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005206 bbio->stripes[i].length -=
5207 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005208
Li Zefanec9ef7a2011-12-01 14:06:42 +08005209 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005210 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005211 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005212 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005213
5214 stripe_index++;
5215 if (stripe_index == map->num_stripes) {
5216 /* This could only happen for RAID0/10 */
5217 stripe_index = 0;
5218 stripe_nr++;
5219 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005220 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005221 } else {
5222 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005223 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005224 map->stripes[stripe_index].physical +
5225 stripe_offset +
5226 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005227 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005228 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005229 stripe_index++;
5230 }
Chris Mason593060d2008-03-25 16:50:33 -04005231 }
Li Zefande11cc12011-12-01 12:55:47 +08005232
Miao Xied20983b2014-07-03 18:22:13 +08005233 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5234 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005235
Stefan Behrens472262f2012-11-06 14:43:46 +01005236 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5237 dev_replace->tgtdev != NULL) {
5238 int index_where_to_add;
5239 u64 srcdev_devid = dev_replace->srcdev->devid;
5240
5241 /*
5242 * duplicate the write operations while the dev replace
5243 * procedure is running. Since the copying of the old disk
5244 * to the new disk takes place at run time while the
5245 * filesystem is mounted writable, the regular write
5246 * operations to the old disk have to be duplicated to go
5247 * to the new disk as well.
5248 * Note that device->missing is handled by the caller, and
5249 * that the write to the old disk is already set up in the
5250 * stripes array.
5251 */
5252 index_where_to_add = num_stripes;
5253 for (i = 0; i < num_stripes; i++) {
5254 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5255 /* write to new disk, too */
5256 struct btrfs_bio_stripe *new =
5257 bbio->stripes + index_where_to_add;
5258 struct btrfs_bio_stripe *old =
5259 bbio->stripes + i;
5260
5261 new->physical = old->physical;
5262 new->length = old->length;
5263 new->dev = dev_replace->tgtdev;
5264 index_where_to_add++;
5265 max_errors++;
5266 }
5267 }
5268 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005269 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5270 dev_replace->tgtdev != NULL) {
5271 u64 srcdev_devid = dev_replace->srcdev->devid;
5272 int index_srcdev = 0;
5273 int found = 0;
5274 u64 physical_of_found = 0;
5275
5276 /*
5277 * During the dev-replace procedure, the target drive can
5278 * also be used to read data in case it is needed to repair
5279 * a corrupt block elsewhere. This is possible if the
5280 * requested area is left of the left cursor. In this area,
5281 * the target drive is a full copy of the source drive.
5282 */
5283 for (i = 0; i < num_stripes; i++) {
5284 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5285 /*
5286 * In case of DUP, in order to keep it
5287 * simple, only add the mirror with the
5288 * lowest physical address
5289 */
5290 if (found &&
5291 physical_of_found <=
5292 bbio->stripes[i].physical)
5293 continue;
5294 index_srcdev = i;
5295 found = 1;
5296 physical_of_found = bbio->stripes[i].physical;
5297 }
5298 }
5299 if (found) {
5300 u64 length = map->stripe_len;
5301
5302 if (physical_of_found + length <=
5303 dev_replace->cursor_left) {
5304 struct btrfs_bio_stripe *tgtdev_stripe =
5305 bbio->stripes + num_stripes;
5306
5307 tgtdev_stripe->physical = physical_of_found;
5308 tgtdev_stripe->length =
5309 bbio->stripes[index_srcdev].length;
5310 tgtdev_stripe->dev = dev_replace->tgtdev;
5311
5312 num_stripes++;
5313 }
5314 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005315 }
5316
Li Zefande11cc12011-12-01 12:55:47 +08005317 *bbio_ret = bbio;
5318 bbio->num_stripes = num_stripes;
5319 bbio->max_errors = max_errors;
5320 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005321
5322 /*
5323 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5324 * mirror_num == num_stripes + 1 && dev_replace target drive is
5325 * available as a mirror
5326 */
5327 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5328 WARN_ON(num_stripes > 1);
5329 bbio->stripes[0].dev = dev_replace->tgtdev;
5330 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5331 bbio->mirror_num = map->num_stripes + 1;
5332 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005333 if (raid_map) {
5334 sort_parity_stripes(bbio, raid_map);
5335 *raid_map_ret = raid_map;
5336 }
Chris Masoncea9e442008-04-09 16:28:12 -04005337out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005338 if (dev_replace_is_ongoing)
5339 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005340 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005341 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005342}
5343
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005344int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005345 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005346 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005347{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005348 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005349 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005350}
5351
Yan Zhenga512bbf2008-12-08 16:46:26 -05005352int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5353 u64 chunk_start, u64 physical, u64 devid,
5354 u64 **logical, int *naddrs, int *stripe_len)
5355{
5356 struct extent_map_tree *em_tree = &map_tree->map_tree;
5357 struct extent_map *em;
5358 struct map_lookup *map;
5359 u64 *buf;
5360 u64 bytenr;
5361 u64 length;
5362 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005363 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005364 int i, j, nr = 0;
5365
Chris Mason890871b2009-09-02 16:24:52 -04005366 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005367 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005368 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005369
Josef Bacik835d9742013-03-19 12:13:25 -04005370 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005371 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005372 chunk_start);
5373 return -EIO;
5374 }
5375
5376 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005377 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005378 em->start, chunk_start);
5379 free_extent_map(em);
5380 return -EIO;
5381 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005382 map = (struct map_lookup *)em->bdev;
5383
5384 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005385 rmap_len = map->stripe_len;
5386
Yan Zhenga512bbf2008-12-08 16:46:26 -05005387 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5388 do_div(length, map->num_stripes / map->sub_stripes);
5389 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5390 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005391 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5392 BTRFS_BLOCK_GROUP_RAID6)) {
5393 do_div(length, nr_data_stripes(map));
5394 rmap_len = map->stripe_len * nr_data_stripes(map);
5395 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005396
5397 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005398 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005399
5400 for (i = 0; i < map->num_stripes; i++) {
5401 if (devid && map->stripes[i].dev->devid != devid)
5402 continue;
5403 if (map->stripes[i].physical > physical ||
5404 map->stripes[i].physical + length <= physical)
5405 continue;
5406
5407 stripe_nr = physical - map->stripes[i].physical;
5408 do_div(stripe_nr, map->stripe_len);
5409
5410 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5411 stripe_nr = stripe_nr * map->num_stripes + i;
5412 do_div(stripe_nr, map->sub_stripes);
5413 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5414 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005415 } /* else if RAID[56], multiply by nr_data_stripes().
5416 * Alternatively, just use rmap_len below instead of
5417 * map->stripe_len */
5418
5419 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005420 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005421 for (j = 0; j < nr; j++) {
5422 if (buf[j] == bytenr)
5423 break;
5424 }
Chris Mason934d3752008-12-08 16:43:10 -05005425 if (j == nr) {
5426 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005427 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005428 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005429 }
5430
Yan Zhenga512bbf2008-12-08 16:46:26 -05005431 *logical = buf;
5432 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005433 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005434
5435 free_extent_map(em);
5436 return 0;
5437}
5438
Miao Xie8408c712014-06-19 10:42:55 +08005439static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5440{
5441 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5442 bio_endio_nodec(bio, err);
5443 else
5444 bio_endio(bio, err);
5445 kfree(bbio);
5446}
5447
Jan Schmidta1d3c472011-08-04 17:15:33 +02005448static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005449{
Chris Mason9be33952013-05-17 18:30:14 -04005450 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005451 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005452 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005453
Stefan Behrens442a4f62012-05-25 16:06:08 +02005454 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005455 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005456 if (err == -EIO || err == -EREMOTEIO) {
5457 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005458 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005459
5460 BUG_ON(stripe_index >= bbio->num_stripes);
5461 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005462 if (dev->bdev) {
5463 if (bio->bi_rw & WRITE)
5464 btrfs_dev_stat_inc(dev,
5465 BTRFS_DEV_STAT_WRITE_ERRS);
5466 else
5467 btrfs_dev_stat_inc(dev,
5468 BTRFS_DEV_STAT_READ_ERRS);
5469 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5470 btrfs_dev_stat_inc(dev,
5471 BTRFS_DEV_STAT_FLUSH_ERRS);
5472 btrfs_dev_stat_print_on_error(dev);
5473 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005474 }
5475 }
Chris Mason8790d502008-04-03 16:29:03 -04005476
Jan Schmidta1d3c472011-08-04 17:15:33 +02005477 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005478 is_orig_bio = 1;
5479
Miao Xiec404e0d2014-01-30 16:46:55 +08005480 btrfs_bio_counter_dec(bbio->fs_info);
5481
Jan Schmidta1d3c472011-08-04 17:15:33 +02005482 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005483 if (!is_orig_bio) {
5484 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005485 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005486 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005487
Jan Schmidta1d3c472011-08-04 17:15:33 +02005488 bio->bi_private = bbio->private;
5489 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005490 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005491 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005492 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005493 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005494 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005495 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005496 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005497 /*
5498 * this bio is actually up to date, we didn't
5499 * go over the max number of errors
5500 */
5501 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005502 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005503 }
Miao Xiec55f1392014-06-19 10:42:54 +08005504
Miao Xie8408c712014-06-19 10:42:55 +08005505 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005506 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005507 bio_put(bio);
5508 }
Chris Mason8790d502008-04-03 16:29:03 -04005509}
5510
Chris Mason8b712842008-06-11 16:50:36 -04005511/*
5512 * see run_scheduled_bios for a description of why bios are collected for
5513 * async submit.
5514 *
5515 * This will add one bio to the pending list for a device and make sure
5516 * the work struct is scheduled.
5517 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005518static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5519 struct btrfs_device *device,
5520 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005521{
5522 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005523 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005524
David Woodhouse53b381b2013-01-29 18:40:14 -05005525 if (device->missing || !device->bdev) {
5526 bio_endio(bio, -EIO);
5527 return;
5528 }
5529
Chris Mason8b712842008-06-11 16:50:36 -04005530 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005531 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005532 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005533 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005534 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005535 return;
Chris Mason8b712842008-06-11 16:50:36 -04005536 }
5537
5538 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005539 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005540 * higher layers. Otherwise, the async bio makes it appear we have
5541 * made progress against dirty pages when we've really just put it
5542 * on a queue for later
5543 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005544 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005545 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005546 bio->bi_next = NULL;
5547 bio->bi_rw |= rw;
5548
5549 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005550 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005551 pending_bios = &device->pending_sync_bios;
5552 else
5553 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005554
Chris Masonffbd5172009-04-20 15:50:09 -04005555 if (pending_bios->tail)
5556 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005557
Chris Masonffbd5172009-04-20 15:50:09 -04005558 pending_bios->tail = bio;
5559 if (!pending_bios->head)
5560 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005561 if (device->running_pending)
5562 should_queue = 0;
5563
5564 spin_unlock(&device->io_lock);
5565
5566 if (should_queue)
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005567 btrfs_queue_work(root->fs_info->submit_workers,
5568 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005569}
5570
Josef Bacikde1ee922012-10-19 16:50:56 -04005571static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5572 sector_t sector)
5573{
5574 struct bio_vec *prev;
5575 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005576 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005577 struct bvec_merge_data bvm = {
5578 .bi_bdev = bdev,
5579 .bi_sector = sector,
5580 .bi_rw = bio->bi_rw,
5581 };
5582
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305583 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005584 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005585
5586 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005587 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005588 return 0;
5589
5590 if (!q->merge_bvec_fn)
5591 return 1;
5592
Kent Overstreet4f024f32013-10-11 15:44:27 -07005593 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005594 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5595 return 0;
5596 return 1;
5597}
5598
5599static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5600 struct bio *bio, u64 physical, int dev_nr,
5601 int rw, int async)
5602{
5603 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5604
5605 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005606 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005607 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005608 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005609#ifdef DEBUG
5610 {
5611 struct rcu_string *name;
5612
5613 rcu_read_lock();
5614 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005615 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005616 "(%s id %llu), size=%u\n", rw,
5617 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5618 name->str, dev->devid, bio->bi_size);
5619 rcu_read_unlock();
5620 }
5621#endif
5622 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005623
5624 btrfs_bio_counter_inc_noblocked(root->fs_info);
5625
Josef Bacikde1ee922012-10-19 16:50:56 -04005626 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005627 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005628 else
5629 btrfsic_submit_bio(rw, bio);
5630}
5631
5632static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5633 struct bio *first_bio, struct btrfs_device *dev,
5634 int dev_nr, int rw, int async)
5635{
5636 struct bio_vec *bvec = first_bio->bi_io_vec;
5637 struct bio *bio;
5638 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5639 u64 physical = bbio->stripes[dev_nr].physical;
5640
5641again:
5642 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5643 if (!bio)
5644 return -ENOMEM;
5645
5646 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5647 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5648 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005649 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005650
5651 atomic_inc(&bbio->stripes_pending);
5652 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5653 rw, async);
5654 physical += len;
5655 goto again;
5656 }
5657 bvec++;
5658 }
5659
5660 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5661 return 0;
5662}
5663
5664static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5665{
5666 atomic_inc(&bbio->error);
5667 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005668 /* Shoud be the original bio. */
5669 WARN_ON(bio != bbio->orig_bio);
5670
Josef Bacikde1ee922012-10-19 16:50:56 -04005671 bio->bi_private = bbio->private;
5672 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005673 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005674 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005675
5676 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005677 }
5678}
5679
Chris Masonf1885912008-04-09 16:28:12 -04005680int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005681 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005682{
Chris Mason0b86a832008-03-24 15:01:56 -04005683 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005684 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005685 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005686 u64 length = 0;
5687 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005688 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005689 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005690 int dev_nr = 0;
5691 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005692 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005693
Kent Overstreet4f024f32013-10-11 15:44:27 -07005694 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005695 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005696
Miao Xiec404e0d2014-01-30 16:46:55 +08005697 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005698 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5699 mirror_num, &raid_map);
Miao Xiec404e0d2014-01-30 16:46:55 +08005700 if (ret) {
5701 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005702 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005703 }
Chris Masoncea9e442008-04-09 16:28:12 -04005704
Jan Schmidta1d3c472011-08-04 17:15:33 +02005705 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005706 bbio->orig_bio = first_bio;
5707 bbio->private = first_bio->bi_private;
5708 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005709 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005710 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5711
5712 if (raid_map) {
5713 /* In this case, map_length has been set to the length of
5714 a single stripe; not the whole write */
5715 if (rw & WRITE) {
Miao Xiec404e0d2014-01-30 16:46:55 +08005716 ret = raid56_parity_write(root, bio, bbio,
5717 raid_map, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005718 } else {
Miao Xiec404e0d2014-01-30 16:46:55 +08005719 ret = raid56_parity_recover(root, bio, bbio,
5720 raid_map, map_length,
5721 mirror_num);
David Woodhouse53b381b2013-01-29 18:40:14 -05005722 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005723 /*
5724 * FIXME, replace dosen't support raid56 yet, please fix
5725 * it in the future.
5726 */
5727 btrfs_bio_counter_dec(root->fs_info);
5728 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005729 }
5730
Chris Masoncea9e442008-04-09 16:28:12 -04005731 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005732 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005733 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005734 BUG();
5735 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005736
Chris Masond3977122009-01-05 21:25:51 -05005737 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005738 dev = bbio->stripes[dev_nr].dev;
5739 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5740 bbio_error(bbio, first_bio, logical);
5741 dev_nr++;
5742 continue;
5743 }
5744
5745 /*
5746 * Check and see if we're ok with this bio based on it's size
5747 * and offset with the given device.
5748 */
5749 if (!bio_size_ok(dev->bdev, first_bio,
5750 bbio->stripes[dev_nr].physical >> 9)) {
5751 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5752 dev_nr, rw, async_submit);
5753 BUG_ON(ret);
5754 dev_nr++;
5755 continue;
5756 }
5757
Jan Schmidta1d3c472011-08-04 17:15:33 +02005758 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005759 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005760 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005761 } else {
5762 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005763 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005764 }
Josef Bacik606686e2012-06-04 14:03:51 -04005765
Josef Bacikde1ee922012-10-19 16:50:56 -04005766 submit_stripe_bio(root, bbio, bio,
5767 bbio->stripes[dev_nr].physical, dev_nr, rw,
5768 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005769 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005770 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005771 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005772 return 0;
5773}
5774
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005775struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005776 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005777{
Yan Zheng2b820322008-11-17 21:11:30 -05005778 struct btrfs_device *device;
5779 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005780
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005781 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005782 while (cur_devices) {
5783 if (!fsid ||
5784 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5785 device = __find_device(&cur_devices->devices,
5786 devid, uuid);
5787 if (device)
5788 return device;
5789 }
5790 cur_devices = cur_devices->seed;
5791 }
5792 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005793}
5794
Chris Masondfe25022008-05-13 13:46:40 -04005795static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5796 u64 devid, u8 *dev_uuid)
5797{
5798 struct btrfs_device *device;
5799 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5800
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005801 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5802 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005803 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005804
5805 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005806 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005807 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005808
5809 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005810 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005811
Chris Masondfe25022008-05-13 13:46:40 -04005812 return device;
5813}
5814
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005815/**
5816 * btrfs_alloc_device - allocate struct btrfs_device
5817 * @fs_info: used only for generating a new devid, can be NULL if
5818 * devid is provided (i.e. @devid != NULL).
5819 * @devid: a pointer to devid for this device. If NULL a new devid
5820 * is generated.
5821 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5822 * is generated.
5823 *
5824 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5825 * on error. Returned struct is not linked onto any lists and can be
5826 * destroyed with kfree() right away.
5827 */
5828struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5829 const u64 *devid,
5830 const u8 *uuid)
5831{
5832 struct btrfs_device *dev;
5833 u64 tmp;
5834
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305835 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005836 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005837
5838 dev = __alloc_device();
5839 if (IS_ERR(dev))
5840 return dev;
5841
5842 if (devid)
5843 tmp = *devid;
5844 else {
5845 int ret;
5846
5847 ret = find_next_devid(fs_info, &tmp);
5848 if (ret) {
5849 kfree(dev);
5850 return ERR_PTR(ret);
5851 }
5852 }
5853 dev->devid = tmp;
5854
5855 if (uuid)
5856 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5857 else
5858 generate_random_uuid(dev->uuid);
5859
Liu Bo9e0af232014-08-15 23:36:53 +08005860 btrfs_init_work(&dev->work, btrfs_submit_helper,
5861 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005862
5863 return dev;
5864}
5865
Chris Mason0b86a832008-03-24 15:01:56 -04005866static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5867 struct extent_buffer *leaf,
5868 struct btrfs_chunk *chunk)
5869{
5870 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5871 struct map_lookup *map;
5872 struct extent_map *em;
5873 u64 logical;
5874 u64 length;
5875 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005876 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005877 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005878 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005879 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005880
Chris Masone17cade2008-04-15 15:41:47 -04005881 logical = key->offset;
5882 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005883
Chris Mason890871b2009-09-02 16:24:52 -04005884 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005885 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005886 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005887
5888 /* already mapped? */
5889 if (em && em->start <= logical && em->start + em->len > logical) {
5890 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005891 return 0;
5892 } else if (em) {
5893 free_extent_map(em);
5894 }
Chris Mason0b86a832008-03-24 15:01:56 -04005895
David Sterba172ddd62011-04-21 00:48:27 +02005896 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005897 if (!em)
5898 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005899 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5900 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005901 if (!map) {
5902 free_extent_map(em);
5903 return -ENOMEM;
5904 }
5905
Wang Shilong298a8f92014-06-19 10:42:52 +08005906 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04005907 em->bdev = (struct block_device *)map;
5908 em->start = logical;
5909 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005910 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005911 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005912 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005913
Chris Mason593060d2008-03-25 16:50:33 -04005914 map->num_stripes = num_stripes;
5915 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5916 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5917 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5918 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5919 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005920 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005921 for (i = 0; i < num_stripes; i++) {
5922 map->stripes[i].physical =
5923 btrfs_stripe_offset_nr(leaf, chunk, i);
5924 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005925 read_extent_buffer(leaf, uuid, (unsigned long)
5926 btrfs_stripe_dev_uuid_nr(chunk, i),
5927 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005928 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5929 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005930 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005931 free_extent_map(em);
5932 return -EIO;
5933 }
Chris Masondfe25022008-05-13 13:46:40 -04005934 if (!map->stripes[i].dev) {
5935 map->stripes[i].dev =
5936 add_missing_dev(root, devid, uuid);
5937 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04005938 free_extent_map(em);
5939 return -EIO;
5940 }
5941 }
5942 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005943 }
5944
Chris Mason890871b2009-09-02 16:24:52 -04005945 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005946 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005947 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005948 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005949 free_extent_map(em);
5950
5951 return 0;
5952}
5953
Jeff Mahoney143bede2012-03-01 14:56:26 +01005954static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005955 struct btrfs_dev_item *dev_item,
5956 struct btrfs_device *device)
5957{
5958 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005959
5960 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005961 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5962 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005963 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5964 device->type = btrfs_device_type(leaf, dev_item);
5965 device->io_align = btrfs_device_io_align(leaf, dev_item);
5966 device->io_width = btrfs_device_io_width(leaf, dev_item);
5967 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005968 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005969 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005970
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02005971 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005972 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005973}
5974
Yan Zheng2b820322008-11-17 21:11:30 -05005975static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5976{
5977 struct btrfs_fs_devices *fs_devices;
5978 int ret;
5979
Li Zefanb367e472011-12-07 11:38:24 +08005980 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005981
5982 fs_devices = root->fs_info->fs_devices->seed;
5983 while (fs_devices) {
5984 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5985 ret = 0;
5986 goto out;
5987 }
5988 fs_devices = fs_devices->seed;
5989 }
5990
5991 fs_devices = find_fsid(fsid);
5992 if (!fs_devices) {
5993 ret = -ENOENT;
5994 goto out;
5995 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005996
5997 fs_devices = clone_fs_devices(fs_devices);
5998 if (IS_ERR(fs_devices)) {
5999 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006000 goto out;
6001 }
6002
Christoph Hellwig97288f22008-12-02 06:36:09 -05006003 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006004 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006005 if (ret) {
6006 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006007 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006008 }
Yan Zheng2b820322008-11-17 21:11:30 -05006009
6010 if (!fs_devices->seeding) {
6011 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006012 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006013 ret = -EINVAL;
6014 goto out;
6015 }
6016
6017 fs_devices->seed = root->fs_info->fs_devices->seed;
6018 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006019out:
Yan Zheng2b820322008-11-17 21:11:30 -05006020 return ret;
6021}
6022
Chris Mason0d81ba52008-03-24 15:02:07 -04006023static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006024 struct extent_buffer *leaf,
6025 struct btrfs_dev_item *dev_item)
6026{
6027 struct btrfs_device *device;
6028 u64 devid;
6029 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006030 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006031 u8 dev_uuid[BTRFS_UUID_SIZE];
6032
Chris Mason0b86a832008-03-24 15:01:56 -04006033 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006034 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006035 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006036 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006037 BTRFS_UUID_SIZE);
6038
6039 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6040 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05006041 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006042 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006043 }
6044
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006045 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006046 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006047 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006048 return -EIO;
6049
6050 if (!device) {
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006051 btrfs_warn(root->fs_info, "devid %llu missing", devid);
Yan Zheng2b820322008-11-17 21:11:30 -05006052 device = add_missing_dev(root, devid, dev_uuid);
6053 if (!device)
6054 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05006055 } else if (!device->missing) {
6056 /*
6057 * this happens when a device that was properly setup
6058 * in the device info lists suddenly goes bad.
6059 * device->bdev is NULL, and so we have to set
6060 * device->missing to one here
6061 */
6062 root->fs_info->fs_devices->missing_devices++;
6063 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006064 }
6065 }
6066
6067 if (device->fs_devices != root->fs_info->fs_devices) {
6068 BUG_ON(device->writeable);
6069 if (device->generation !=
6070 btrfs_device_generation(leaf, dev_item))
6071 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006072 }
Chris Mason0b86a832008-03-24 15:01:56 -04006073
6074 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006075 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006076 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006077 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006078 spin_lock(&root->fs_info->free_chunk_lock);
6079 root->fs_info->free_chunk_space += device->total_bytes -
6080 device->bytes_used;
6081 spin_unlock(&root->fs_info->free_chunk_lock);
6082 }
Chris Mason0b86a832008-03-24 15:01:56 -04006083 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006084 return ret;
6085}
6086
Yan Zhenge4404d62008-12-12 10:03:26 -05006087int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006088{
David Sterba6c417612011-04-13 15:41:04 +02006089 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006090 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006091 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006092 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04006093 u8 *ptr;
6094 unsigned long sb_ptr;
6095 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006096 u32 num_stripes;
6097 u32 array_size;
6098 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006099 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04006100 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006101
Yan Zhenge4404d62008-12-12 10:03:26 -05006102 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04006103 BTRFS_SUPER_INFO_SIZE);
6104 if (!sb)
6105 return -ENOMEM;
6106 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006107 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006108 /*
6109 * The sb extent buffer is artifical and just used to read the system array.
6110 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6111 * pages up-to-date when the page is larger: extent does not cover the
6112 * whole page and consequently check_page_uptodate does not find all
6113 * the page's extents up-to-date (the hole beyond sb),
6114 * write_extent_buffer then triggers a WARN_ON.
6115 *
6116 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6117 * but sb spans only this function. Add an explicit SetPageUptodate call
6118 * to silence the warning eg. on PowerPC 64.
6119 */
6120 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006121 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006122
Chris Masona061fc82008-05-07 11:43:44 -04006123 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006124 array_size = btrfs_super_sys_array_size(super_copy);
6125
Chris Mason0b86a832008-03-24 15:01:56 -04006126 ptr = super_copy->sys_chunk_array;
6127 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
6128 cur = 0;
6129
6130 while (cur < array_size) {
6131 disk_key = (struct btrfs_disk_key *)ptr;
6132 btrfs_disk_key_to_cpu(&key, disk_key);
6133
Chris Masona061fc82008-05-07 11:43:44 -04006134 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006135 sb_ptr += len;
6136 cur += len;
6137
Chris Mason0d81ba52008-03-24 15:02:07 -04006138 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04006139 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04006140 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006141 if (ret)
6142 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006143 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6144 len = btrfs_chunk_item_size(num_stripes);
6145 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006146 ret = -EIO;
6147 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006148 }
6149 ptr += len;
6150 sb_ptr += len;
6151 cur += len;
6152 }
Chris Masona061fc82008-05-07 11:43:44 -04006153 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006154 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006155}
6156
6157int btrfs_read_chunk_tree(struct btrfs_root *root)
6158{
6159 struct btrfs_path *path;
6160 struct extent_buffer *leaf;
6161 struct btrfs_key key;
6162 struct btrfs_key found_key;
6163 int ret;
6164 int slot;
6165
6166 root = root->fs_info->chunk_root;
6167
6168 path = btrfs_alloc_path();
6169 if (!path)
6170 return -ENOMEM;
6171
Li Zefanb367e472011-12-07 11:38:24 +08006172 mutex_lock(&uuid_mutex);
6173 lock_chunks(root);
6174
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006175 /*
6176 * Read all device items, and then all the chunk items. All
6177 * device items are found before any chunk item (their object id
6178 * is smaller than the lowest possible object id for a chunk
6179 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006180 */
6181 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6182 key.offset = 0;
6183 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006184 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006185 if (ret < 0)
6186 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006187 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006188 leaf = path->nodes[0];
6189 slot = path->slots[0];
6190 if (slot >= btrfs_header_nritems(leaf)) {
6191 ret = btrfs_next_leaf(root, path);
6192 if (ret == 0)
6193 continue;
6194 if (ret < 0)
6195 goto error;
6196 break;
6197 }
6198 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006199 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6200 struct btrfs_dev_item *dev_item;
6201 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006202 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006203 ret = read_one_dev(root, leaf, dev_item);
6204 if (ret)
6205 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006206 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6207 struct btrfs_chunk *chunk;
6208 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6209 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006210 if (ret)
6211 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006212 }
6213 path->slots[0]++;
6214 }
Chris Mason0b86a832008-03-24 15:01:56 -04006215 ret = 0;
6216error:
Li Zefanb367e472011-12-07 11:38:24 +08006217 unlock_chunks(root);
6218 mutex_unlock(&uuid_mutex);
6219
Yan Zheng2b820322008-11-17 21:11:30 -05006220 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006221 return ret;
6222}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006223
Miao Xiecb517ea2013-05-15 07:48:19 +00006224void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6225{
6226 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6227 struct btrfs_device *device;
6228
Liu Bo29cc83f2014-05-11 23:14:59 +08006229 while (fs_devices) {
6230 mutex_lock(&fs_devices->device_list_mutex);
6231 list_for_each_entry(device, &fs_devices->devices, dev_list)
6232 device->dev_root = fs_info->dev_root;
6233 mutex_unlock(&fs_devices->device_list_mutex);
6234
6235 fs_devices = fs_devices->seed;
6236 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006237}
6238
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006239static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6240{
6241 int i;
6242
6243 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6244 btrfs_dev_stat_reset(dev, i);
6245}
6246
6247int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6248{
6249 struct btrfs_key key;
6250 struct btrfs_key found_key;
6251 struct btrfs_root *dev_root = fs_info->dev_root;
6252 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6253 struct extent_buffer *eb;
6254 int slot;
6255 int ret = 0;
6256 struct btrfs_device *device;
6257 struct btrfs_path *path = NULL;
6258 int i;
6259
6260 path = btrfs_alloc_path();
6261 if (!path) {
6262 ret = -ENOMEM;
6263 goto out;
6264 }
6265
6266 mutex_lock(&fs_devices->device_list_mutex);
6267 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6268 int item_size;
6269 struct btrfs_dev_stats_item *ptr;
6270
6271 key.objectid = 0;
6272 key.type = BTRFS_DEV_STATS_KEY;
6273 key.offset = device->devid;
6274 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6275 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006276 __btrfs_reset_dev_stats(device);
6277 device->dev_stats_valid = 1;
6278 btrfs_release_path(path);
6279 continue;
6280 }
6281 slot = path->slots[0];
6282 eb = path->nodes[0];
6283 btrfs_item_key_to_cpu(eb, &found_key, slot);
6284 item_size = btrfs_item_size_nr(eb, slot);
6285
6286 ptr = btrfs_item_ptr(eb, slot,
6287 struct btrfs_dev_stats_item);
6288
6289 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6290 if (item_size >= (1 + i) * sizeof(__le64))
6291 btrfs_dev_stat_set(device, i,
6292 btrfs_dev_stats_value(eb, ptr, i));
6293 else
6294 btrfs_dev_stat_reset(device, i);
6295 }
6296
6297 device->dev_stats_valid = 1;
6298 btrfs_dev_stat_print_on_load(device);
6299 btrfs_release_path(path);
6300 }
6301 mutex_unlock(&fs_devices->device_list_mutex);
6302
6303out:
6304 btrfs_free_path(path);
6305 return ret < 0 ? ret : 0;
6306}
6307
6308static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6309 struct btrfs_root *dev_root,
6310 struct btrfs_device *device)
6311{
6312 struct btrfs_path *path;
6313 struct btrfs_key key;
6314 struct extent_buffer *eb;
6315 struct btrfs_dev_stats_item *ptr;
6316 int ret;
6317 int i;
6318
6319 key.objectid = 0;
6320 key.type = BTRFS_DEV_STATS_KEY;
6321 key.offset = device->devid;
6322
6323 path = btrfs_alloc_path();
6324 BUG_ON(!path);
6325 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6326 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006327 printk_in_rcu(KERN_WARNING "BTRFS: "
6328 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006329 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006330 goto out;
6331 }
6332
6333 if (ret == 0 &&
6334 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6335 /* need to delete old one and insert a new one */
6336 ret = btrfs_del_item(trans, dev_root, path);
6337 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006338 printk_in_rcu(KERN_WARNING "BTRFS: "
6339 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006340 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006341 goto out;
6342 }
6343 ret = 1;
6344 }
6345
6346 if (ret == 1) {
6347 /* need to insert a new item */
6348 btrfs_release_path(path);
6349 ret = btrfs_insert_empty_item(trans, dev_root, path,
6350 &key, sizeof(*ptr));
6351 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006352 printk_in_rcu(KERN_WARNING "BTRFS: "
6353 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006354 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006355 goto out;
6356 }
6357 }
6358
6359 eb = path->nodes[0];
6360 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6361 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6362 btrfs_set_dev_stats_value(eb, ptr, i,
6363 btrfs_dev_stat_read(device, i));
6364 btrfs_mark_buffer_dirty(eb);
6365
6366out:
6367 btrfs_free_path(path);
6368 return ret;
6369}
6370
6371/*
6372 * called from commit_transaction. Writes all changed device stats to disk.
6373 */
6374int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6375 struct btrfs_fs_info *fs_info)
6376{
6377 struct btrfs_root *dev_root = fs_info->dev_root;
6378 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6379 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006380 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006381 int ret = 0;
6382
6383 mutex_lock(&fs_devices->device_list_mutex);
6384 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006385 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006386 continue;
6387
Miao Xieaddc3fa2014-07-24 11:37:11 +08006388 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006389 ret = update_dev_stat_item(trans, dev_root, device);
6390 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006391 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006392 }
6393 mutex_unlock(&fs_devices->device_list_mutex);
6394
6395 return ret;
6396}
6397
Stefan Behrens442a4f62012-05-25 16:06:08 +02006398void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6399{
6400 btrfs_dev_stat_inc(dev, index);
6401 btrfs_dev_stat_print_on_error(dev);
6402}
6403
Eric Sandeen48a3b632013-04-25 20:41:01 +00006404static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006405{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006406 if (!dev->dev_stats_valid)
6407 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006408 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6409 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006410 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006411 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6412 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6413 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006414 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6415 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006416}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006417
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006418static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6419{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006420 int i;
6421
6422 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6423 if (btrfs_dev_stat_read(dev, i) != 0)
6424 break;
6425 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6426 return; /* all values == 0, suppress message */
6427
Frank Holtonefe120a2013-12-20 11:37:06 -05006428 printk_in_rcu(KERN_INFO "BTRFS: "
6429 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006430 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006431 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6432 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6433 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6434 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6435 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6436}
6437
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006438int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006439 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006440{
6441 struct btrfs_device *dev;
6442 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6443 int i;
6444
6445 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006446 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006447 mutex_unlock(&fs_devices->device_list_mutex);
6448
6449 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006450 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006451 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006452 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006453 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006454 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006455 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006456 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6457 if (stats->nr_items > i)
6458 stats->values[i] =
6459 btrfs_dev_stat_read_and_reset(dev, i);
6460 else
6461 btrfs_dev_stat_reset(dev, i);
6462 }
6463 } else {
6464 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6465 if (stats->nr_items > i)
6466 stats->values[i] = btrfs_dev_stat_read(dev, i);
6467 }
6468 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6469 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6470 return 0;
6471}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006472
6473int btrfs_scratch_superblock(struct btrfs_device *device)
6474{
6475 struct buffer_head *bh;
6476 struct btrfs_super_block *disk_super;
6477
6478 bh = btrfs_read_dev_super(device->bdev);
6479 if (!bh)
6480 return -EINVAL;
6481 disk_super = (struct btrfs_super_block *)bh->b_data;
6482
6483 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6484 set_buffer_dirty(bh);
6485 sync_dirty_buffer(bh);
6486 brelse(bh);
6487
6488 return 0;
6489}