blob: b5cc129bfa4b26f2cb433cc7829a00ca528166a1 [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
Miao Xie67a2c452014-09-03 21:35:43 +080053DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -040054static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +080055struct list_head *btrfs_get_fs_uuids(void)
56{
57 return &fs_uuids;
58}
Chris Mason8a4b83c2008-03-24 15:02:07 -040059
Ilya Dryomov2208a372013-08-12 14:33:03 +030060static struct btrfs_fs_devices *__alloc_fs_devices(void)
61{
62 struct btrfs_fs_devices *fs_devs;
63
64 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
65 if (!fs_devs)
66 return ERR_PTR(-ENOMEM);
67
68 mutex_init(&fs_devs->device_list_mutex);
69
70 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +080071 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +030072 INIT_LIST_HEAD(&fs_devs->alloc_list);
73 INIT_LIST_HEAD(&fs_devs->list);
74
75 return fs_devs;
76}
77
78/**
79 * alloc_fs_devices - allocate struct btrfs_fs_devices
80 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
81 * generated.
82 *
83 * Return: a pointer to a new &struct btrfs_fs_devices on success;
84 * ERR_PTR() on error. Returned struct is not linked onto any lists and
85 * can be destroyed with kfree() right away.
86 */
87static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
88{
89 struct btrfs_fs_devices *fs_devs;
90
91 fs_devs = __alloc_fs_devices();
92 if (IS_ERR(fs_devs))
93 return fs_devs;
94
95 if (fsid)
96 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
97 else
98 generate_random_uuid(fs_devs->fsid);
99
100 return fs_devs;
101}
102
Yan Zhenge4404d62008-12-12 10:03:26 -0500103static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
104{
105 struct btrfs_device *device;
106 WARN_ON(fs_devices->opened);
107 while (!list_empty(&fs_devices->devices)) {
108 device = list_entry(fs_devices->devices.next,
109 struct btrfs_device, dev_list);
110 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400111 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500112 kfree(device);
113 }
114 kfree(fs_devices);
115}
116
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000117static void btrfs_kobject_uevent(struct block_device *bdev,
118 enum kobject_action action)
119{
120 int ret;
121
122 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
123 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500124 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000125 action,
126 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
127 &disk_to_dev(bdev->bd_disk)->kobj);
128}
129
Jeff Mahoney143bede2012-03-01 14:56:26 +0100130void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400131{
132 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400133
Yan Zheng2b820322008-11-17 21:11:30 -0500134 while (!list_empty(&fs_uuids)) {
135 fs_devices = list_entry(fs_uuids.next,
136 struct btrfs_fs_devices, list);
137 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500138 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400139 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400140}
141
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300142static struct btrfs_device *__alloc_device(void)
143{
144 struct btrfs_device *dev;
145
146 dev = kzalloc(sizeof(*dev), GFP_NOFS);
147 if (!dev)
148 return ERR_PTR(-ENOMEM);
149
150 INIT_LIST_HEAD(&dev->dev_list);
151 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800152 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300153
154 spin_lock_init(&dev->io_lock);
155
156 spin_lock_init(&dev->reada_lock);
157 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800158 atomic_set(&dev->dev_stats_ccnt, 0);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300159 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
160 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
161
162 return dev;
163}
164
Chris Masona1b32a52008-09-05 16:09:51 -0400165static noinline struct btrfs_device *__find_device(struct list_head *head,
166 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400167{
168 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400169
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500170 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400171 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400172 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400173 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400174 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400175 }
176 return NULL;
177}
178
Chris Masona1b32a52008-09-05 16:09:51 -0400179static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400180{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400181 struct btrfs_fs_devices *fs_devices;
182
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500183 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400184 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
185 return fs_devices;
186 }
187 return NULL;
188}
189
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100190static int
191btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
192 int flush, struct block_device **bdev,
193 struct buffer_head **bh)
194{
195 int ret;
196
197 *bdev = blkdev_get_by_path(device_path, flags, holder);
198
199 if (IS_ERR(*bdev)) {
200 ret = PTR_ERR(*bdev);
Frank Holtonefe120a2013-12-20 11:37:06 -0500201 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100202 goto error;
203 }
204
205 if (flush)
206 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
207 ret = set_blocksize(*bdev, 4096);
208 if (ret) {
209 blkdev_put(*bdev, flags);
210 goto error;
211 }
212 invalidate_bdev(*bdev);
213 *bh = btrfs_read_dev_super(*bdev);
214 if (!*bh) {
215 ret = -EINVAL;
216 blkdev_put(*bdev, flags);
217 goto error;
218 }
219
220 return 0;
221
222error:
223 *bdev = NULL;
224 *bh = NULL;
225 return ret;
226}
227
Chris Masonffbd5172009-04-20 15:50:09 -0400228static void requeue_list(struct btrfs_pending_bios *pending_bios,
229 struct bio *head, struct bio *tail)
230{
231
232 struct bio *old_head;
233
234 old_head = pending_bios->head;
235 pending_bios->head = head;
236 if (pending_bios->tail)
237 tail->bi_next = old_head;
238 else
239 pending_bios->tail = tail;
240}
241
Chris Mason8b712842008-06-11 16:50:36 -0400242/*
243 * we try to collect pending bios for a device so we don't get a large
244 * number of procs sending bios down to the same device. This greatly
245 * improves the schedulers ability to collect and merge the bios.
246 *
247 * But, it also turns into a long list of bios to process and that is sure
248 * to eventually make the worker thread block. The solution here is to
249 * make some progress and then put this work struct back at the end of
250 * the list if the block device is congested. This way, multiple devices
251 * can make progress from a single worker thread.
252 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100253static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400254{
255 struct bio *pending;
256 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400257 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400258 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400259 struct bio *tail;
260 struct bio *cur;
261 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400262 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400263 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400264 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400265 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400266 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000267 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400268 struct blk_plug plug;
269
270 /*
271 * this function runs all the bios we've collected for
272 * a particular device. We don't want to wander off to
273 * another device without first sending all of these down.
274 * So, setup a plug here and finish it off before we return
275 */
276 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400277
Chris Masonbedf7622009-04-03 10:32:58 -0400278 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400279 fs_info = device->dev_root->fs_info;
280 limit = btrfs_async_submit_limit(fs_info);
281 limit = limit * 2 / 3;
282
Chris Mason8b712842008-06-11 16:50:36 -0400283loop:
284 spin_lock(&device->io_lock);
285
Chris Masona6837052009-02-04 09:19:41 -0500286loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400287 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400288
Chris Mason8b712842008-06-11 16:50:36 -0400289 /* take all the bios off the list at once and process them
290 * later on (without the lock held). But, remember the
291 * tail and other pointers so the bios can be properly reinserted
292 * into the list if we hit congestion
293 */
Chris Masond84275c2009-06-09 15:39:08 -0400294 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400295 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400296 force_reg = 1;
297 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400298 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400299 force_reg = 0;
300 }
Chris Masonffbd5172009-04-20 15:50:09 -0400301
302 pending = pending_bios->head;
303 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400304 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400305
306 /*
307 * if pending was null this time around, no bios need processing
308 * at all and we can stop. Otherwise it'll loop back up again
309 * and do an additional check so no bios are missed.
310 *
311 * device->running_pending is used to synchronize with the
312 * schedule_bio code.
313 */
Chris Masonffbd5172009-04-20 15:50:09 -0400314 if (device->pending_sync_bios.head == NULL &&
315 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400316 again = 0;
317 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400318 } else {
319 again = 1;
320 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400321 }
Chris Masonffbd5172009-04-20 15:50:09 -0400322
323 pending_bios->head = NULL;
324 pending_bios->tail = NULL;
325
Chris Mason8b712842008-06-11 16:50:36 -0400326 spin_unlock(&device->io_lock);
327
Chris Masond3977122009-01-05 21:25:51 -0500328 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400329
330 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400331 /* we want to work on both lists, but do more bios on the
332 * sync list than the regular list
333 */
334 if ((num_run > 32 &&
335 pending_bios != &device->pending_sync_bios &&
336 device->pending_sync_bios.head) ||
337 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
338 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400339 spin_lock(&device->io_lock);
340 requeue_list(pending_bios, pending, tail);
341 goto loop_lock;
342 }
343
Chris Mason8b712842008-06-11 16:50:36 -0400344 cur = pending;
345 pending = pending->bi_next;
346 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400347
Josef Bacik66657b32012-08-01 15:36:24 -0400348 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400349 waitqueue_active(&fs_info->async_submit_wait))
350 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400351
352 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400353
Chris Mason2ab1ba62011-08-04 14:28:36 -0400354 /*
355 * if we're doing the sync list, record that our
356 * plug has some sync requests on it
357 *
358 * If we're doing the regular list and there are
359 * sync requests sitting around, unplug before
360 * we add more
361 */
362 if (pending_bios == &device->pending_sync_bios) {
363 sync_pending = 1;
364 } else if (sync_pending) {
365 blk_finish_plug(&plug);
366 blk_start_plug(&plug);
367 sync_pending = 0;
368 }
369
Stefan Behrens21adbd52011-11-09 13:44:05 +0100370 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400371 num_run++;
372 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100373
374 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400375
376 /*
377 * we made progress, there is more work to do and the bdi
378 * is now congested. Back off and let other work structs
379 * run instead
380 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400381 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500382 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400383 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400384
Chris Masonb765ead2009-04-03 10:27:10 -0400385 ioc = current->io_context;
386
387 /*
388 * the main goal here is that we don't want to
389 * block if we're going to be able to submit
390 * more requests without blocking.
391 *
392 * This code does two great things, it pokes into
393 * the elevator code from a filesystem _and_
394 * it makes assumptions about how batching works.
395 */
396 if (ioc && ioc->nr_batch_requests > 0 &&
397 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
398 (last_waited == 0 ||
399 ioc->last_waited == last_waited)) {
400 /*
401 * we want to go through our batch of
402 * requests and stop. So, we copy out
403 * the ioc->last_waited time and test
404 * against it before looping
405 */
406 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100407 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400408 continue;
409 }
Chris Mason8b712842008-06-11 16:50:36 -0400410 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400411 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500412 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400413
414 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800415 btrfs_queue_work(fs_info->submit_workers,
416 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400417 goto done;
418 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500419 /* unplug every 64 requests just for good measure */
420 if (batch_run % 64 == 0) {
421 blk_finish_plug(&plug);
422 blk_start_plug(&plug);
423 sync_pending = 0;
424 }
Chris Mason8b712842008-06-11 16:50:36 -0400425 }
Chris Masonffbd5172009-04-20 15:50:09 -0400426
Chris Mason51684082010-03-10 15:33:32 -0500427 cond_resched();
428 if (again)
429 goto loop;
430
431 spin_lock(&device->io_lock);
432 if (device->pending_bios.head || device->pending_sync_bios.head)
433 goto loop_lock;
434 spin_unlock(&device->io_lock);
435
Chris Mason8b712842008-06-11 16:50:36 -0400436done:
Chris Mason211588a2011-04-19 20:12:40 -0400437 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400438}
439
Christoph Hellwigb2950862008-12-02 09:54:17 -0500440static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400441{
442 struct btrfs_device *device;
443
444 device = container_of(work, struct btrfs_device, work);
445 run_scheduled_bios(device);
446}
447
David Sterba60999ca2014-03-26 18:26:36 +0100448/*
449 * Add new device to list of registered devices
450 *
451 * Returns:
452 * 1 - first time device is seen
453 * 0 - device already known
454 * < 0 - error
455 */
Chris Masona1b32a52008-09-05 16:09:51 -0400456static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400457 struct btrfs_super_block *disk_super,
458 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
459{
460 struct btrfs_device *device;
461 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400462 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100463 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400464 u64 found_transid = btrfs_super_generation(disk_super);
465
466 fs_devices = find_fsid(disk_super->fsid);
467 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300468 fs_devices = alloc_fs_devices(disk_super->fsid);
469 if (IS_ERR(fs_devices))
470 return PTR_ERR(fs_devices);
471
Chris Mason8a4b83c2008-03-24 15:02:07 -0400472 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300473
Chris Mason8a4b83c2008-03-24 15:02:07 -0400474 device = NULL;
475 } else {
Chris Masona4437552008-04-18 10:29:38 -0400476 device = __find_device(&fs_devices->devices, devid,
477 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400478 }
Miao Xie443f24f2014-07-24 11:37:15 +0800479
Chris Mason8a4b83c2008-03-24 15:02:07 -0400480 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500481 if (fs_devices->opened)
482 return -EBUSY;
483
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300484 device = btrfs_alloc_device(NULL, &devid,
485 disk_super->dev_item.uuid);
486 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400487 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300488 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400489 }
Josef Bacik606686e2012-06-04 14:03:51 -0400490
491 name = rcu_string_strdup(path, GFP_NOFS);
492 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400493 kfree(device);
494 return -ENOMEM;
495 }
Josef Bacik606686e2012-06-04 14:03:51 -0400496 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200497
Chris Masone5e9a522009-06-10 15:17:02 -0400498 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000499 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100500 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400501 mutex_unlock(&fs_devices->device_list_mutex);
502
David Sterba60999ca2014-03-26 18:26:36 +0100503 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500504 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400505 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800506 /*
507 * When FS is already mounted.
508 * 1. If you are here and if the device->name is NULL that
509 * means this device was missing at time of FS mount.
510 * 2. If you are here and if the device->name is different
511 * from 'path' that means either
512 * a. The same device disappeared and reappeared with
513 * different name. or
514 * b. The missing-disk-which-was-replaced, has
515 * reappeared now.
516 *
517 * We must allow 1 and 2a above. But 2b would be a spurious
518 * and unintentional.
519 *
520 * Further in case of 1 and 2a above, the disk at 'path'
521 * would have missed some transaction when it was away and
522 * in case of 2a the stale bdev has to be updated as well.
523 * 2b must not be allowed at all time.
524 */
525
526 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700527 * For now, we do allow update to btrfs_fs_device through the
528 * btrfs dev scan cli after FS has been mounted. We're still
529 * tracking a problem where systems fail mount by subvolume id
530 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800531 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700532 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800533 /*
534 * That is if the FS is _not_ mounted and if you
535 * are here, that means there is more than one
536 * disk with same uuid and devid.We keep the one
537 * with larger generation number or the last-in if
538 * generation are equal.
539 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700540 return -EEXIST;
Anand Jain77bdae42014-07-03 18:22:06 +0800541 }
Anand Jainb96de002014-07-03 18:22:05 +0800542
Josef Bacik606686e2012-06-04 14:03:51 -0400543 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000544 if (!name)
545 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400546 rcu_string_free(device->name);
547 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500548 if (device->missing) {
549 fs_devices->missing_devices--;
550 device->missing = 0;
551 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400552 }
553
Anand Jain77bdae42014-07-03 18:22:06 +0800554 /*
555 * Unmount does not free the btrfs_device struct but would zero
556 * generation along with most of the other members. So just update
557 * it back. We need it to pick the disk with largest generation
558 * (as above).
559 */
560 if (!fs_devices->opened)
561 device->generation = found_transid;
562
Chris Mason8a4b83c2008-03-24 15:02:07 -0400563 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100564
565 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400566}
567
Yan Zhenge4404d62008-12-12 10:03:26 -0500568static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
569{
570 struct btrfs_fs_devices *fs_devices;
571 struct btrfs_device *device;
572 struct btrfs_device *orig_dev;
573
Ilya Dryomov2208a372013-08-12 14:33:03 +0300574 fs_devices = alloc_fs_devices(orig->fsid);
575 if (IS_ERR(fs_devices))
576 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500577
Miao Xieadbbb862014-09-03 21:35:42 +0800578 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400579 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500580
Xiao Guangrong46224702011-04-20 10:08:47 +0000581 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500582 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400583 struct rcu_string *name;
584
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300585 device = btrfs_alloc_device(NULL, &orig_dev->devid,
586 orig_dev->uuid);
587 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500588 goto error;
589
Josef Bacik606686e2012-06-04 14:03:51 -0400590 /*
591 * This is ok to do without rcu read locked because we hold the
592 * uuid mutex so nothing we touch in here is going to disappear.
593 */
Anand Jaine755f782014-06-30 17:12:47 +0800594 if (orig_dev->name) {
595 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
596 if (!name) {
597 kfree(device);
598 goto error;
599 }
600 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400601 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500602
Yan Zhenge4404d62008-12-12 10:03:26 -0500603 list_add(&device->dev_list, &fs_devices->devices);
604 device->fs_devices = fs_devices;
605 fs_devices->num_devices++;
606 }
Miao Xieadbbb862014-09-03 21:35:42 +0800607 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500608 return fs_devices;
609error:
Miao Xieadbbb862014-09-03 21:35:42 +0800610 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500611 free_fs_devices(fs_devices);
612 return ERR_PTR(-ENOMEM);
613}
614
Eric Sandeen9eaed212014-08-01 18:12:35 -0500615void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400616{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500617 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800618 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500619
Chris Masondfe25022008-05-13 13:46:40 -0400620 mutex_lock(&uuid_mutex);
621again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000622 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500623 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500624 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100625 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800626 (!latest_dev ||
627 device->generation > latest_dev->generation)) {
628 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500629 }
Yan Zheng2b820322008-11-17 21:11:30 -0500630 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500631 }
Yan Zheng2b820322008-11-17 21:11:30 -0500632
Stefan Behrens8dabb742012-11-06 13:15:27 +0100633 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
634 /*
635 * In the first step, keep the device which has
636 * the correct fsid and the devid that is used
637 * for the dev_replace procedure.
638 * In the second step, the dev_replace state is
639 * read from the device tree and it is known
640 * whether the procedure is really active or
641 * not, which means whether this device is
642 * used or whether it should be removed.
643 */
644 if (step == 0 || device->is_tgtdev_for_dev_replace) {
645 continue;
646 }
647 }
Yan Zheng2b820322008-11-17 21:11:30 -0500648 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100649 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500650 device->bdev = NULL;
651 fs_devices->open_devices--;
652 }
653 if (device->writeable) {
654 list_del_init(&device->dev_alloc_list);
655 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100656 if (!device->is_tgtdev_for_dev_replace)
657 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500658 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500659 list_del_init(&device->dev_list);
660 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400661 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500662 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400663 }
Yan Zheng2b820322008-11-17 21:11:30 -0500664
665 if (fs_devices->seed) {
666 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500667 goto again;
668 }
669
Miao Xie443f24f2014-07-24 11:37:15 +0800670 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500671
Chris Masondfe25022008-05-13 13:46:40 -0400672 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400673}
Chris Masona0af4692008-05-13 16:03:06 -0400674
Xiao Guangrong1f781602011-04-20 10:09:16 +0000675static void __free_device(struct work_struct *work)
676{
677 struct btrfs_device *device;
678
679 device = container_of(work, struct btrfs_device, rcu_work);
680
681 if (device->bdev)
682 blkdev_put(device->bdev, device->mode);
683
Josef Bacik606686e2012-06-04 14:03:51 -0400684 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000685 kfree(device);
686}
687
688static void free_device(struct rcu_head *head)
689{
690 struct btrfs_device *device;
691
692 device = container_of(head, struct btrfs_device, rcu);
693
694 INIT_WORK(&device->rcu_work, __free_device);
695 schedule_work(&device->rcu_work);
696}
697
Yan Zheng2b820322008-11-17 21:11:30 -0500698static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400699{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400700 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500701
Yan Zheng2b820322008-11-17 21:11:30 -0500702 if (--fs_devices->opened > 0)
703 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400704
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000705 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500706 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000707 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400708 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000709
710 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400711 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000712
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300713 if (device->writeable &&
714 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500715 list_del_init(&device->dev_alloc_list);
716 fs_devices->rw_devices--;
717 }
718
Josef Bacik726551e2013-08-26 13:45:53 -0400719 if (device->missing)
720 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000721
Ilya Dryomova1e87802013-08-12 14:33:04 +0300722 new_device = btrfs_alloc_device(NULL, &device->devid,
723 device->uuid);
724 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400725
726 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400727 if (device->name) {
728 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300729 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400730 rcu_assign_pointer(new_device->name, name);
731 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300732
Xiao Guangrong1f781602011-04-20 10:09:16 +0000733 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300734 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000735
736 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400737 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000738 mutex_unlock(&fs_devices->device_list_mutex);
739
Yan Zhenge4404d62008-12-12 10:03:26 -0500740 WARN_ON(fs_devices->open_devices);
741 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500742 fs_devices->opened = 0;
743 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500744
Chris Mason8a4b83c2008-03-24 15:02:07 -0400745 return 0;
746}
747
Yan Zheng2b820322008-11-17 21:11:30 -0500748int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
749{
Yan Zhenge4404d62008-12-12 10:03:26 -0500750 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500751 int ret;
752
753 mutex_lock(&uuid_mutex);
754 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500755 if (!fs_devices->opened) {
756 seed_devices = fs_devices->seed;
757 fs_devices->seed = NULL;
758 }
Yan Zheng2b820322008-11-17 21:11:30 -0500759 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500760
761 while (seed_devices) {
762 fs_devices = seed_devices;
763 seed_devices = fs_devices->seed;
764 __btrfs_close_devices(fs_devices);
765 free_fs_devices(fs_devices);
766 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000767 /*
768 * Wait for rcu kworkers under __btrfs_close_devices
769 * to finish all blkdev_puts so device is really
770 * free when umount is done.
771 */
772 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500773 return ret;
774}
775
Yan Zhenge4404d62008-12-12 10:03:26 -0500776static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
777 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400778{
Josef Bacikd5e20032011-08-04 14:52:27 +0000779 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400780 struct block_device *bdev;
781 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400782 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800783 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400784 struct buffer_head *bh;
785 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400786 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500787 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400788 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400789
Tejun Heod4d77622010-11-13 11:55:18 +0100790 flags |= FMODE_EXCL;
791
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500792 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400793 if (device->bdev)
794 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400795 if (!device->name)
796 continue;
797
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000798 /* Just open everything we can; ignore failures here */
799 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
800 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100801 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400802
803 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000804 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400805 if (devid != device->devid)
806 goto error_brelse;
807
Yan Zheng2b820322008-11-17 21:11:30 -0500808 if (memcmp(device->uuid, disk_super->dev_item.uuid,
809 BTRFS_UUID_SIZE))
810 goto error_brelse;
811
812 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800813 if (!latest_dev ||
814 device->generation > latest_dev->generation)
815 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400816
Yan Zheng2b820322008-11-17 21:11:30 -0500817 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
818 device->writeable = 0;
819 } else {
820 device->writeable = !bdev_read_only(bdev);
821 seeding = 0;
822 }
823
Josef Bacikd5e20032011-08-04 14:52:27 +0000824 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +0800825 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +0000826 device->can_discard = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +0000827
Chris Mason8a4b83c2008-03-24 15:02:07 -0400828 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400829 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500830 device->mode = flags;
831
Chris Masonc2898112009-06-10 09:51:32 -0400832 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
833 fs_devices->rotating = 1;
834
Chris Masona0af4692008-05-13 16:03:06 -0400835 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300836 if (device->writeable &&
837 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500838 fs_devices->rw_devices++;
839 list_add(&device->dev_alloc_list,
840 &fs_devices->alloc_list);
841 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000842 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400843 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400844
Chris Masona0af4692008-05-13 16:03:06 -0400845error_brelse:
846 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100847 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400848 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400849 }
Chris Masona0af4692008-05-13 16:03:06 -0400850 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300851 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400852 goto out;
853 }
Yan Zheng2b820322008-11-17 21:11:30 -0500854 fs_devices->seeding = seeding;
855 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800856 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500857 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400858out:
Yan Zheng2b820322008-11-17 21:11:30 -0500859 return ret;
860}
861
862int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500863 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500864{
865 int ret;
866
867 mutex_lock(&uuid_mutex);
868 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500869 fs_devices->opened++;
870 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500871 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500872 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500873 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400874 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400875 return ret;
876}
877
David Sterba6f60cbd2013-02-15 11:31:02 -0700878/*
879 * Look for a btrfs signature on a device. This may be called out of the mount path
880 * and we are not allowed to call set_blocksize during the scan. The superblock
881 * is read via pagecache
882 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500883int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400884 struct btrfs_fs_devices **fs_devices_ret)
885{
886 struct btrfs_super_block *disk_super;
887 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700888 struct page *page;
889 void *p;
890 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400891 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400892 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400893 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700894 u64 bytenr;
895 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400896
David Sterba6f60cbd2013-02-15 11:31:02 -0700897 /*
898 * we would like to check all the supers, but that would make
899 * a btrfs mount succeed after a mkfs from a different FS.
900 * So, we need to add a special mount option to scan for
901 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
902 */
903 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100904 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500905 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700906
907 bdev = blkdev_get_by_path(path, flags, holder);
908
909 if (IS_ERR(bdev)) {
910 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100911 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700912 }
913
914 /* make sure our super fits in the device */
915 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
916 goto error_bdev_put;
917
918 /* make sure our super fits in the page */
919 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
920 goto error_bdev_put;
921
922 /* make sure our super doesn't straddle pages on disk */
923 index = bytenr >> PAGE_CACHE_SHIFT;
924 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
925 goto error_bdev_put;
926
927 /* pull in the page with our super */
928 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
929 index, GFP_NOFS);
930
931 if (IS_ERR_OR_NULL(page))
932 goto error_bdev_put;
933
934 p = kmap(page);
935
936 /* align our pointer to the offset of the super block */
937 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
938
939 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800940 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700941 goto error_unmap;
942
Xiao Guangronga3438322010-01-06 11:48:18 +0000943 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400944 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400945 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700946
Chris Mason8a4b83c2008-03-24 15:02:07 -0400947 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100948 if (ret > 0) {
949 if (disk_super->label[0]) {
950 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
951 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
952 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
953 } else {
954 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
955 }
956
957 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
958 ret = 0;
959 }
Josef Bacik02db0842012-06-21 16:03:58 -0400960 if (!ret && fs_devices_ret)
961 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700962
963error_unmap:
964 kunmap(page);
965 page_cache_release(page);
966
967error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100968 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400969error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100970 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400971 return ret;
972}
Chris Mason0b86a832008-03-24 15:01:56 -0400973
Miao Xie6d07bce2011-01-05 10:07:31 +0000974/* helper to account the used device space in the range */
975int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
976 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400977{
978 struct btrfs_key key;
979 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000980 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500981 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000982 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400983 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000984 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400985 struct extent_buffer *l;
986
Miao Xie6d07bce2011-01-05 10:07:31 +0000987 *length = 0;
988
Stefan Behrens63a212a2012-11-05 18:29:28 +0100989 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000990 return 0;
991
Yan Zheng2b820322008-11-17 21:11:30 -0500992 path = btrfs_alloc_path();
993 if (!path)
994 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400995 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400996
Chris Mason0b86a832008-03-24 15:01:56 -0400997 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000998 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400999 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001000
1001 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001002 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001003 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001004 if (ret > 0) {
1005 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1006 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001007 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001008 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001009
Chris Mason0b86a832008-03-24 15:01:56 -04001010 while (1) {
1011 l = path->nodes[0];
1012 slot = path->slots[0];
1013 if (slot >= btrfs_header_nritems(l)) {
1014 ret = btrfs_next_leaf(root, path);
1015 if (ret == 0)
1016 continue;
1017 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001018 goto out;
1019
1020 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001021 }
1022 btrfs_item_key_to_cpu(l, &key, slot);
1023
1024 if (key.objectid < device->devid)
1025 goto next;
1026
1027 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001028 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001029
David Sterba962a2982014-06-04 18:41:45 +02001030 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001031 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001032
Chris Mason0b86a832008-03-24 15:01:56 -04001033 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001034 extent_end = key.offset + btrfs_dev_extent_length(l,
1035 dev_extent);
1036 if (key.offset <= start && extent_end > end) {
1037 *length = end - start + 1;
1038 break;
1039 } else if (key.offset <= start && extent_end > start)
1040 *length += extent_end - start;
1041 else if (key.offset > start && extent_end <= end)
1042 *length += extent_end - key.offset;
1043 else if (key.offset > start && key.offset <= end) {
1044 *length += end - key.offset + 1;
1045 break;
1046 } else if (key.offset > end)
1047 break;
1048
1049next:
1050 path->slots[0]++;
1051 }
1052 ret = 0;
1053out:
1054 btrfs_free_path(path);
1055 return ret;
1056}
1057
Josef Bacik6df9a952013-06-27 13:22:46 -04001058static int contains_pending_extent(struct btrfs_trans_handle *trans,
1059 struct btrfs_device *device,
1060 u64 *start, u64 len)
1061{
1062 struct extent_map *em;
Filipe Manana04216822014-11-27 21:14:15 +00001063 struct list_head *search_list = &trans->transaction->pending_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001064 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001065 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001066
Filipe Manana04216822014-11-27 21:14:15 +00001067again:
1068 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001069 struct map_lookup *map;
1070 int i;
1071
1072 map = (struct map_lookup *)em->bdev;
1073 for (i = 0; i < map->num_stripes; i++) {
1074 if (map->stripes[i].dev != device)
1075 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001076 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001077 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001078 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001079 continue;
1080 *start = map->stripes[i].physical +
1081 em->orig_block_len;
1082 ret = 1;
1083 }
1084 }
Filipe Manana04216822014-11-27 21:14:15 +00001085 if (search_list == &trans->transaction->pending_chunks) {
1086 search_list = &trans->root->fs_info->pinned_chunks;
1087 goto again;
1088 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001089
1090 return ret;
1091}
1092
1093
Chris Mason0b86a832008-03-24 15:01:56 -04001094/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001095 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001096 * @device: the device which we search the free space in
1097 * @num_bytes: the size of the free space that we need
1098 * @start: store the start of the free space.
1099 * @len: the size of the free space. that we find, or the size of the max
1100 * free space if we don't find suitable free space
1101 *
Chris Mason0b86a832008-03-24 15:01:56 -04001102 * this uses a pretty simple search, the expectation is that it is
1103 * called very infrequently and that a given device has a small number
1104 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001105 *
1106 * @start is used to store the start of the free space if we find. But if we
1107 * don't find suitable free space, it will be used to store the start position
1108 * of the max free space.
1109 *
1110 * @len is used to store the size of the free space that we find.
1111 * But if we don't find suitable free space, it is used to store the size of
1112 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001113 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001114int find_free_dev_extent(struct btrfs_trans_handle *trans,
1115 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001116 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001117{
1118 struct btrfs_key key;
1119 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001120 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001121 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001122 u64 hole_size;
1123 u64 max_hole_start;
1124 u64 max_hole_size;
1125 u64 extent_end;
1126 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001127 u64 search_end = device->total_bytes;
1128 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001129 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001130 struct extent_buffer *l;
1131
Chris Mason0b86a832008-03-24 15:01:56 -04001132 /* FIXME use last free of some kind */
1133
1134 /* we don't want to overwrite the superblock on the drive,
1135 * so we make sure to start at an offset of at least 1MB
1136 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001137 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001138
Josef Bacik6df9a952013-06-27 13:22:46 -04001139 path = btrfs_alloc_path();
1140 if (!path)
1141 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001142
Miao Xie7bfc8372011-01-05 10:07:26 +00001143 max_hole_start = search_start;
1144 max_hole_size = 0;
1145
Zhao Leif2ab7612015-02-16 18:52:17 +08001146again:
Stefan Behrens63a212a2012-11-05 18:29:28 +01001147 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001148 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001149 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001150 }
1151
Miao Xie7bfc8372011-01-05 10:07:26 +00001152 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001153 path->search_commit_root = 1;
1154 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001155
Chris Mason0b86a832008-03-24 15:01:56 -04001156 key.objectid = device->devid;
1157 key.offset = search_start;
1158 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001159
Li Zefan125ccb02011-12-08 15:07:24 +08001160 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001161 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001162 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001163 if (ret > 0) {
1164 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1165 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001166 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001167 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001168
Chris Mason0b86a832008-03-24 15:01:56 -04001169 while (1) {
1170 l = path->nodes[0];
1171 slot = path->slots[0];
1172 if (slot >= btrfs_header_nritems(l)) {
1173 ret = btrfs_next_leaf(root, path);
1174 if (ret == 0)
1175 continue;
1176 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001177 goto out;
1178
1179 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001180 }
1181 btrfs_item_key_to_cpu(l, &key, slot);
1182
1183 if (key.objectid < device->devid)
1184 goto next;
1185
1186 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001187 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001188
David Sterba962a2982014-06-04 18:41:45 +02001189 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001190 goto next;
1191
Miao Xie7bfc8372011-01-05 10:07:26 +00001192 if (key.offset > search_start) {
1193 hole_size = key.offset - search_start;
1194
Josef Bacik6df9a952013-06-27 13:22:46 -04001195 /*
1196 * Have to check before we set max_hole_start, otherwise
1197 * we could end up sending back this offset anyway.
1198 */
1199 if (contains_pending_extent(trans, device,
1200 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001201 hole_size)) {
1202 if (key.offset >= search_start) {
1203 hole_size = key.offset - search_start;
1204 } else {
1205 WARN_ON_ONCE(1);
1206 hole_size = 0;
1207 }
1208 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001209
Miao Xie7bfc8372011-01-05 10:07:26 +00001210 if (hole_size > max_hole_size) {
1211 max_hole_start = search_start;
1212 max_hole_size = hole_size;
1213 }
1214
1215 /*
1216 * If this free space is greater than which we need,
1217 * it must be the max free space that we have found
1218 * until now, so max_hole_start must point to the start
1219 * of this free space and the length of this free space
1220 * is stored in max_hole_size. Thus, we return
1221 * max_hole_start and max_hole_size and go back to the
1222 * caller.
1223 */
1224 if (hole_size >= num_bytes) {
1225 ret = 0;
1226 goto out;
1227 }
1228 }
1229
Chris Mason0b86a832008-03-24 15:01:56 -04001230 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001231 extent_end = key.offset + btrfs_dev_extent_length(l,
1232 dev_extent);
1233 if (extent_end > search_start)
1234 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001235next:
1236 path->slots[0]++;
1237 cond_resched();
1238 }
Chris Mason0b86a832008-03-24 15:01:56 -04001239
liubo38c01b92011-08-02 02:39:03 +00001240 /*
1241 * At this point, search_start should be the end of
1242 * allocated dev extents, and when shrinking the device,
1243 * search_end may be smaller than search_start.
1244 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001245 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001246 hole_size = search_end - search_start;
1247
Zhao Leif2ab7612015-02-16 18:52:17 +08001248 if (contains_pending_extent(trans, device, &search_start,
1249 hole_size)) {
1250 btrfs_release_path(path);
1251 goto again;
1252 }
Chris Mason0b86a832008-03-24 15:01:56 -04001253
Zhao Leif2ab7612015-02-16 18:52:17 +08001254 if (hole_size > max_hole_size) {
1255 max_hole_start = search_start;
1256 max_hole_size = hole_size;
1257 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001258 }
1259
Miao Xie7bfc8372011-01-05 10:07:26 +00001260 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001261 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001262 ret = -ENOSPC;
1263 else
1264 ret = 0;
1265
1266out:
Yan Zheng2b820322008-11-17 21:11:30 -05001267 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001268 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001269 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001270 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001271 return ret;
1272}
1273
Christoph Hellwigb2950862008-12-02 09:54:17 -05001274static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001275 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001276 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001277{
1278 int ret;
1279 struct btrfs_path *path;
1280 struct btrfs_root *root = device->dev_root;
1281 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001282 struct btrfs_key found_key;
1283 struct extent_buffer *leaf = NULL;
1284 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001285
1286 path = btrfs_alloc_path();
1287 if (!path)
1288 return -ENOMEM;
1289
1290 key.objectid = device->devid;
1291 key.offset = start;
1292 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001293again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001294 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001295 if (ret > 0) {
1296 ret = btrfs_previous_item(root, path, key.objectid,
1297 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001298 if (ret)
1299 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001300 leaf = path->nodes[0];
1301 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1302 extent = btrfs_item_ptr(leaf, path->slots[0],
1303 struct btrfs_dev_extent);
1304 BUG_ON(found_key.offset > start || found_key.offset +
1305 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001306 key = found_key;
1307 btrfs_release_path(path);
1308 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001309 } else if (ret == 0) {
1310 leaf = path->nodes[0];
1311 extent = btrfs_item_ptr(leaf, path->slots[0],
1312 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001313 } else {
1314 btrfs_error(root->fs_info, ret, "Slot search failed");
1315 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001316 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001317
Miao Xie2196d6e2014-09-03 21:35:41 +08001318 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1319
Chris Mason8f18cf12008-04-25 16:53:30 -04001320 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001321 if (ret) {
1322 btrfs_error(root->fs_info, ret,
1323 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001324 } else {
1325 trans->transaction->have_free_bgs = 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001326 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001327out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001328 btrfs_free_path(path);
1329 return ret;
1330}
1331
Eric Sandeen48a3b632013-04-25 20:41:01 +00001332static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1333 struct btrfs_device *device,
1334 u64 chunk_tree, u64 chunk_objectid,
1335 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001336{
1337 int ret;
1338 struct btrfs_path *path;
1339 struct btrfs_root *root = device->dev_root;
1340 struct btrfs_dev_extent *extent;
1341 struct extent_buffer *leaf;
1342 struct btrfs_key key;
1343
Chris Masondfe25022008-05-13 13:46:40 -04001344 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001345 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001346 path = btrfs_alloc_path();
1347 if (!path)
1348 return -ENOMEM;
1349
Chris Mason0b86a832008-03-24 15:01:56 -04001350 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001351 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001352 key.type = BTRFS_DEV_EXTENT_KEY;
1353 ret = btrfs_insert_empty_item(trans, root, path, &key,
1354 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001355 if (ret)
1356 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001357
1358 leaf = path->nodes[0];
1359 extent = btrfs_item_ptr(leaf, path->slots[0],
1360 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001361 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1362 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1363 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1364
1365 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001366 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001367
Chris Mason0b86a832008-03-24 15:01:56 -04001368 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1369 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001370out:
Chris Mason0b86a832008-03-24 15:01:56 -04001371 btrfs_free_path(path);
1372 return ret;
1373}
1374
Josef Bacik6df9a952013-06-27 13:22:46 -04001375static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001376{
Josef Bacik6df9a952013-06-27 13:22:46 -04001377 struct extent_map_tree *em_tree;
1378 struct extent_map *em;
1379 struct rb_node *n;
1380 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001381
Josef Bacik6df9a952013-06-27 13:22:46 -04001382 em_tree = &fs_info->mapping_tree.map_tree;
1383 read_lock(&em_tree->lock);
1384 n = rb_last(&em_tree->map);
1385 if (n) {
1386 em = rb_entry(n, struct extent_map, rb_node);
1387 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001388 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001389 read_unlock(&em_tree->lock);
1390
Chris Mason0b86a832008-03-24 15:01:56 -04001391 return ret;
1392}
1393
Ilya Dryomov53f10652013-08-12 14:33:01 +03001394static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1395 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001396{
1397 int ret;
1398 struct btrfs_key key;
1399 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001400 struct btrfs_path *path;
1401
Yan Zheng2b820322008-11-17 21:11:30 -05001402 path = btrfs_alloc_path();
1403 if (!path)
1404 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001405
1406 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1407 key.type = BTRFS_DEV_ITEM_KEY;
1408 key.offset = (u64)-1;
1409
Ilya Dryomov53f10652013-08-12 14:33:01 +03001410 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001411 if (ret < 0)
1412 goto error;
1413
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001414 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001415
Ilya Dryomov53f10652013-08-12 14:33:01 +03001416 ret = btrfs_previous_item(fs_info->chunk_root, path,
1417 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001418 BTRFS_DEV_ITEM_KEY);
1419 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001420 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001421 } else {
1422 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1423 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001424 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001425 }
1426 ret = 0;
1427error:
Yan Zheng2b820322008-11-17 21:11:30 -05001428 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001429 return ret;
1430}
1431
1432/*
1433 * the device information is stored in the chunk root
1434 * the btrfs_device struct should be fully filled in
1435 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001436static int btrfs_add_device(struct btrfs_trans_handle *trans,
1437 struct btrfs_root *root,
1438 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001439{
1440 int ret;
1441 struct btrfs_path *path;
1442 struct btrfs_dev_item *dev_item;
1443 struct extent_buffer *leaf;
1444 struct btrfs_key key;
1445 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001446
1447 root = root->fs_info->chunk_root;
1448
1449 path = btrfs_alloc_path();
1450 if (!path)
1451 return -ENOMEM;
1452
Chris Mason0b86a832008-03-24 15:01:56 -04001453 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1454 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001455 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001456
1457 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001458 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001459 if (ret)
1460 goto out;
1461
1462 leaf = path->nodes[0];
1463 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1464
1465 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001466 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001467 btrfs_set_device_type(leaf, dev_item, device->type);
1468 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1469 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1470 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001471 btrfs_set_device_total_bytes(leaf, dev_item,
1472 btrfs_device_get_disk_total_bytes(device));
1473 btrfs_set_device_bytes_used(leaf, dev_item,
1474 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001475 btrfs_set_device_group(leaf, dev_item, 0);
1476 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1477 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001478 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001479
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001480 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001481 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001482 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001483 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001484 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001485
Yan Zheng2b820322008-11-17 21:11:30 -05001486 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001487out:
1488 btrfs_free_path(path);
1489 return ret;
1490}
Chris Mason8f18cf12008-04-25 16:53:30 -04001491
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001492/*
1493 * Function to update ctime/mtime for a given device path.
1494 * Mainly used for ctime/mtime based probe like libblkid.
1495 */
1496static void update_dev_time(char *path_name)
1497{
1498 struct file *filp;
1499
1500 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001501 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001502 return;
1503 file_update_time(filp);
1504 filp_close(filp, NULL);
1505 return;
1506}
1507
Chris Masona061fc82008-05-07 11:43:44 -04001508static int btrfs_rm_dev_item(struct btrfs_root *root,
1509 struct btrfs_device *device)
1510{
1511 int ret;
1512 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001513 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001514 struct btrfs_trans_handle *trans;
1515
1516 root = root->fs_info->chunk_root;
1517
1518 path = btrfs_alloc_path();
1519 if (!path)
1520 return -ENOMEM;
1521
Yan, Zhenga22285a2010-05-16 10:48:46 -04001522 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001523 if (IS_ERR(trans)) {
1524 btrfs_free_path(path);
1525 return PTR_ERR(trans);
1526 }
Chris Masona061fc82008-05-07 11:43:44 -04001527 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1528 key.type = BTRFS_DEV_ITEM_KEY;
1529 key.offset = device->devid;
1530
1531 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1532 if (ret < 0)
1533 goto out;
1534
1535 if (ret > 0) {
1536 ret = -ENOENT;
1537 goto out;
1538 }
1539
1540 ret = btrfs_del_item(trans, root, path);
1541 if (ret)
1542 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001543out:
1544 btrfs_free_path(path);
1545 btrfs_commit_transaction(trans, root);
1546 return ret;
1547}
1548
1549int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1550{
1551 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001552 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001553 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001554 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001555 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001556 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001557 u64 all_avail;
1558 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001559 u64 num_devices;
1560 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001561 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001562 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001563 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001564
Chris Masona061fc82008-05-07 11:43:44 -04001565 mutex_lock(&uuid_mutex);
1566
Miao Xiede98ced2013-01-29 10:13:12 +00001567 do {
1568 seq = read_seqbegin(&root->fs_info->profiles_lock);
1569
1570 all_avail = root->fs_info->avail_data_alloc_bits |
1571 root->fs_info->avail_system_alloc_bits |
1572 root->fs_info->avail_metadata_alloc_bits;
1573 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001574
Stefan Behrens8dabb742012-11-06 13:15:27 +01001575 num_devices = root->fs_info->fs_devices->num_devices;
1576 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1577 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1578 WARN_ON(num_devices < 1);
1579 num_devices--;
1580 }
1581 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1582
1583 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001584 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001585 goto out;
1586 }
1587
Stefan Behrens8dabb742012-11-06 13:15:27 +01001588 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001589 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001590 goto out;
1591 }
1592
David Woodhouse53b381b2013-01-29 18:40:14 -05001593 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1594 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001595 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001596 goto out;
1597 }
1598 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1599 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001600 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001601 goto out;
1602 }
1603
Chris Masondfe25022008-05-13 13:46:40 -04001604 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001605 struct list_head *devices;
1606 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001607
Chris Masondfe25022008-05-13 13:46:40 -04001608 device = NULL;
1609 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001610 /*
1611 * It is safe to read the devices since the volume_mutex
1612 * is held.
1613 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001614 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001615 if (tmp->in_fs_metadata &&
1616 !tmp->is_tgtdev_for_dev_replace &&
1617 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001618 device = tmp;
1619 break;
1620 }
1621 }
1622 bdev = NULL;
1623 bh = NULL;
1624 disk_super = NULL;
1625 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001626 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001627 goto out;
1628 }
Chris Masondfe25022008-05-13 13:46:40 -04001629 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001630 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001631 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001632 root->fs_info->bdev_holder, 0,
1633 &bdev, &bh);
1634 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001635 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001636 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001637 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001638 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001639 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001640 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001641 if (!device) {
1642 ret = -ENOENT;
1643 goto error_brelse;
1644 }
Chris Masondfe25022008-05-13 13:46:40 -04001645 }
Yan Zheng2b820322008-11-17 21:11:30 -05001646
Stefan Behrens63a212a2012-11-05 18:29:28 +01001647 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001648 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001649 goto error_brelse;
1650 }
1651
Yan Zheng2b820322008-11-17 21:11:30 -05001652 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001653 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001654 goto error_brelse;
1655 }
1656
1657 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001658 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001659 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001660 device->fs_devices->rw_devices--;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001661 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001662 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001663 }
Chris Masona061fc82008-05-07 11:43:44 -04001664
Carey Underwoodd7901552013-03-04 16:37:06 -06001665 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001666 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001667 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001668 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001669 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001670
Stefan Behrens63a212a2012-11-05 18:29:28 +01001671 /*
1672 * TODO: the superblock still includes this device in its num_devices
1673 * counter although write_all_supers() is not locked out. This
1674 * could give a filesystem state which requires a degraded mount.
1675 */
Chris Masona061fc82008-05-07 11:43:44 -04001676 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1677 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001678 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001679
Yan Zheng2b820322008-11-17 21:11:30 -05001680 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001681 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001682
1683 /*
1684 * the device list mutex makes sure that we don't change
1685 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001686 * the device supers. Whoever is writing all supers, should
1687 * lock the device list mutex before getting the number of
1688 * devices in the super block (super_copy). Conversely,
1689 * whoever updates the number of devices in the super block
1690 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001691 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001692
1693 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001694 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001695 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001696
Yan Zhenge4404d62008-12-12 10:03:26 -05001697 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001698 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001699
Chris Masoncd02dca2010-12-13 14:56:23 -05001700 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001701 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001702
Yan Zheng2b820322008-11-17 21:11:30 -05001703 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1704 struct btrfs_device, dev_list);
1705 if (device->bdev == root->fs_info->sb->s_bdev)
1706 root->fs_info->sb->s_bdev = next_device->bdev;
1707 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1708 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1709
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001710 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001711 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001712 /* remove sysfs entry */
Anand Jain6c14a162015-03-10 06:38:34 +08001713 btrfs_kobj_rm_device(root->fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001714 }
Anand Jain99994cd2014-06-03 11:36:00 +08001715
Xiao Guangrong1f781602011-04-20 10:09:16 +00001716 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001717
David Sterba6c417612011-04-13 15:41:04 +02001718 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1719 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001720 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001721
Xiao Guangrong1f781602011-04-20 10:09:16 +00001722 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001723 struct btrfs_fs_devices *fs_devices;
1724 fs_devices = root->fs_info->fs_devices;
1725 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001726 if (fs_devices->seed == cur_devices) {
1727 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001728 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001729 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001730 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001731 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001732 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001733 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001734 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001735 }
1736
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001737 root->fs_info->num_tolerated_disk_barrier_failures =
1738 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1739
Yan Zheng2b820322008-11-17 21:11:30 -05001740 /*
1741 * at this point, the device is zero sized. We want to
1742 * remove it from the devices list and zero out the old super
1743 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001744 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001745 u64 bytenr;
1746 int i;
1747
Chris Masondfe25022008-05-13 13:46:40 -04001748 /* make sure this device isn't detected as part of
1749 * the FS anymore
1750 */
1751 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1752 set_buffer_dirty(bh);
1753 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001754
1755 /* clear the mirror copies of super block on the disk
1756 * being removed, 0th copy is been taken care above and
1757 * the below would take of the rest
1758 */
1759 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1760 bytenr = btrfs_sb_offset(i);
1761 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1762 i_size_read(bdev->bd_inode))
1763 break;
1764
1765 brelse(bh);
1766 bh = __bread(bdev, bytenr / 4096,
1767 BTRFS_SUPER_INFO_SIZE);
1768 if (!bh)
1769 continue;
1770
1771 disk_super = (struct btrfs_super_block *)bh->b_data;
1772
1773 if (btrfs_super_bytenr(disk_super) != bytenr ||
1774 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1775 continue;
1776 }
1777 memset(&disk_super->magic, 0,
1778 sizeof(disk_super->magic));
1779 set_buffer_dirty(bh);
1780 sync_dirty_buffer(bh);
1781 }
Chris Masondfe25022008-05-13 13:46:40 -04001782 }
Chris Masona061fc82008-05-07 11:43:44 -04001783
Chris Masona061fc82008-05-07 11:43:44 -04001784 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001785
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001786 if (bdev) {
1787 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001788 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001789
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001790 /* Update ctime/mtime for device path for libblkid */
1791 update_dev_time(device_path);
1792 }
1793
Chris Masona061fc82008-05-07 11:43:44 -04001794error_brelse:
1795 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001796 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001797 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001798out:
1799 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001800 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001801error_undo:
1802 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001803 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001804 list_add(&device->dev_alloc_list,
1805 &root->fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001806 device->fs_devices->rw_devices++;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001807 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001808 }
1809 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001810}
1811
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001812void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1813 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01001814{
Anand Jaind51908c2014-08-13 14:24:19 +08001815 struct btrfs_fs_devices *fs_devices;
1816
Stefan Behrense93c89c2012-11-05 17:33:06 +01001817 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001818
Anand Jain25e8e912014-08-20 10:56:56 +08001819 /*
1820 * in case of fs with no seed, srcdev->fs_devices will point
1821 * to fs_devices of fs_info. However when the dev being replaced is
1822 * a seed dev it will point to the seed's local fs_devices. In short
1823 * srcdev will have its correct fs_devices in both the cases.
1824 */
1825 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001826
Stefan Behrense93c89c2012-11-05 17:33:06 +01001827 list_del_rcu(&srcdev->dev_list);
1828 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001829 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08001830 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08001831 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001832
Miao Xie82372bc2014-09-03 21:35:44 +08001833 if (srcdev->writeable) {
1834 fs_devices->rw_devices--;
1835 /* zero out the old super if it is writable */
1836 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03001837 }
1838
Miao Xie82372bc2014-09-03 21:35:44 +08001839 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08001840 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001841}
1842
1843void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
1844 struct btrfs_device *srcdev)
1845{
1846 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001847
Stefan Behrense93c89c2012-11-05 17:33:06 +01001848 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001849
1850 /*
1851 * unless fs_devices is seed fs, num_devices shouldn't go
1852 * zero
1853 */
1854 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1855
1856 /* if this is no devs we rather delete the fs_devices */
1857 if (!fs_devices->num_devices) {
1858 struct btrfs_fs_devices *tmp_fs_devices;
1859
1860 tmp_fs_devices = fs_info->fs_devices;
1861 while (tmp_fs_devices) {
1862 if (tmp_fs_devices->seed == fs_devices) {
1863 tmp_fs_devices->seed = fs_devices->seed;
1864 break;
1865 }
1866 tmp_fs_devices = tmp_fs_devices->seed;
1867 }
1868 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08001869 __btrfs_close_devices(fs_devices);
1870 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001871 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01001872}
1873
1874void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1875 struct btrfs_device *tgtdev)
1876{
1877 struct btrfs_device *next_device;
1878
Miao Xie67a2c452014-09-03 21:35:43 +08001879 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001880 WARN_ON(!tgtdev);
1881 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1882 if (tgtdev->bdev) {
1883 btrfs_scratch_superblock(tgtdev);
1884 fs_info->fs_devices->open_devices--;
1885 }
1886 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001887
1888 next_device = list_entry(fs_info->fs_devices->devices.next,
1889 struct btrfs_device, dev_list);
1890 if (tgtdev->bdev == fs_info->sb->s_bdev)
1891 fs_info->sb->s_bdev = next_device->bdev;
1892 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1893 fs_info->fs_devices->latest_bdev = next_device->bdev;
1894 list_del_rcu(&tgtdev->dev_list);
1895
1896 call_rcu(&tgtdev->rcu, free_device);
1897
1898 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08001899 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001900}
1901
Eric Sandeen48a3b632013-04-25 20:41:01 +00001902static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1903 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001904{
1905 int ret = 0;
1906 struct btrfs_super_block *disk_super;
1907 u64 devid;
1908 u8 *dev_uuid;
1909 struct block_device *bdev;
1910 struct buffer_head *bh;
1911
1912 *device = NULL;
1913 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1914 root->fs_info->bdev_holder, 0, &bdev, &bh);
1915 if (ret)
1916 return ret;
1917 disk_super = (struct btrfs_super_block *)bh->b_data;
1918 devid = btrfs_stack_device_id(&disk_super->dev_item);
1919 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001920 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001921 disk_super->fsid);
1922 brelse(bh);
1923 if (!*device)
1924 ret = -ENOENT;
1925 blkdev_put(bdev, FMODE_READ);
1926 return ret;
1927}
1928
1929int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1930 char *device_path,
1931 struct btrfs_device **device)
1932{
1933 *device = NULL;
1934 if (strcmp(device_path, "missing") == 0) {
1935 struct list_head *devices;
1936 struct btrfs_device *tmp;
1937
1938 devices = &root->fs_info->fs_devices->devices;
1939 /*
1940 * It is safe to read the devices since the volume_mutex
1941 * is held by the caller.
1942 */
1943 list_for_each_entry(tmp, devices, dev_list) {
1944 if (tmp->in_fs_metadata && !tmp->bdev) {
1945 *device = tmp;
1946 break;
1947 }
1948 }
1949
1950 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001951 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001952 return -ENOENT;
1953 }
1954
1955 return 0;
1956 } else {
1957 return btrfs_find_device_by_path(root, device_path, device);
1958 }
1959}
1960
Yan Zheng2b820322008-11-17 21:11:30 -05001961/*
1962 * does all the dirty work required for changing file system's UUID.
1963 */
Li Zefan125ccb02011-12-08 15:07:24 +08001964static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001965{
1966 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1967 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001968 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001969 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001970 struct btrfs_device *device;
1971 u64 super_flags;
1972
1973 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001974 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001975 return -EINVAL;
1976
Ilya Dryomov2208a372013-08-12 14:33:03 +03001977 seed_devices = __alloc_fs_devices();
1978 if (IS_ERR(seed_devices))
1979 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001980
Yan Zhenge4404d62008-12-12 10:03:26 -05001981 old_devices = clone_fs_devices(fs_devices);
1982 if (IS_ERR(old_devices)) {
1983 kfree(seed_devices);
1984 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001985 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001986
Yan Zheng2b820322008-11-17 21:11:30 -05001987 list_add(&old_devices->list, &fs_uuids);
1988
Yan Zhenge4404d62008-12-12 10:03:26 -05001989 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1990 seed_devices->opened = 1;
1991 INIT_LIST_HEAD(&seed_devices->devices);
1992 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001993 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001994
1995 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001996 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1997 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08001998 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05001999 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002000
2001 lock_chunks(root);
2002 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2003 unlock_chunks(root);
Yan Zhenge4404d62008-12-12 10:03:26 -05002004
Yan Zheng2b820322008-11-17 21:11:30 -05002005 fs_devices->seeding = 0;
2006 fs_devices->num_devices = 0;
2007 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002008 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002009 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002010 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002011
2012 generate_random_uuid(fs_devices->fsid);
2013 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2014 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002015 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2016
Yan Zheng2b820322008-11-17 21:11:30 -05002017 super_flags = btrfs_super_flags(disk_super) &
2018 ~BTRFS_SUPER_FLAG_SEEDING;
2019 btrfs_set_super_flags(disk_super, super_flags);
2020
2021 return 0;
2022}
2023
2024/*
2025 * strore the expected generation for seed devices in device items.
2026 */
2027static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2028 struct btrfs_root *root)
2029{
2030 struct btrfs_path *path;
2031 struct extent_buffer *leaf;
2032 struct btrfs_dev_item *dev_item;
2033 struct btrfs_device *device;
2034 struct btrfs_key key;
2035 u8 fs_uuid[BTRFS_UUID_SIZE];
2036 u8 dev_uuid[BTRFS_UUID_SIZE];
2037 u64 devid;
2038 int ret;
2039
2040 path = btrfs_alloc_path();
2041 if (!path)
2042 return -ENOMEM;
2043
2044 root = root->fs_info->chunk_root;
2045 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2046 key.offset = 0;
2047 key.type = BTRFS_DEV_ITEM_KEY;
2048
2049 while (1) {
2050 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2051 if (ret < 0)
2052 goto error;
2053
2054 leaf = path->nodes[0];
2055next_slot:
2056 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2057 ret = btrfs_next_leaf(root, path);
2058 if (ret > 0)
2059 break;
2060 if (ret < 0)
2061 goto error;
2062 leaf = path->nodes[0];
2063 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002064 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002065 continue;
2066 }
2067
2068 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2069 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2070 key.type != BTRFS_DEV_ITEM_KEY)
2071 break;
2072
2073 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2074 struct btrfs_dev_item);
2075 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002076 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002077 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002078 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002079 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002080 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2081 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002082 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002083
2084 if (device->fs_devices->seeding) {
2085 btrfs_set_device_generation(leaf, dev_item,
2086 device->generation);
2087 btrfs_mark_buffer_dirty(leaf);
2088 }
2089
2090 path->slots[0]++;
2091 goto next_slot;
2092 }
2093 ret = 0;
2094error:
2095 btrfs_free_path(path);
2096 return ret;
2097}
2098
Chris Mason788f20e2008-04-28 15:29:42 -04002099int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2100{
Josef Bacikd5e20032011-08-04 14:52:27 +00002101 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002102 struct btrfs_trans_handle *trans;
2103 struct btrfs_device *device;
2104 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002105 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002106 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002107 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002108 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002109 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002110 int ret = 0;
2111
Yan Zheng2b820322008-11-17 21:11:30 -05002112 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002113 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002114
Li Zefana5d163332011-12-07 20:08:40 -05002115 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002116 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002117 if (IS_ERR(bdev))
2118 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002119
Yan Zheng2b820322008-11-17 21:11:30 -05002120 if (root->fs_info->fs_devices->seeding) {
2121 seeding_dev = 1;
2122 down_write(&sb->s_umount);
2123 mutex_lock(&uuid_mutex);
2124 }
2125
Chris Mason8c8bee12008-09-29 11:19:10 -04002126 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002127
Chris Mason788f20e2008-04-28 15:29:42 -04002128 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002129
2130 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002131 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002132 if (device->bdev == bdev) {
2133 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002134 mutex_unlock(
2135 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002136 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002137 }
2138 }
Liu Bod25628b2012-11-14 14:35:30 +00002139 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002140
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002141 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2142 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002143 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002144 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002145 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002146 }
2147
Josef Bacik606686e2012-06-04 14:03:51 -04002148 name = rcu_string_strdup(device_path, GFP_NOFS);
2149 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002150 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002151 ret = -ENOMEM;
2152 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002153 }
Josef Bacik606686e2012-06-04 14:03:51 -04002154 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002155
Yan, Zhenga22285a2010-05-16 10:48:46 -04002156 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002157 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002158 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002159 kfree(device);
2160 ret = PTR_ERR(trans);
2161 goto error;
2162 }
2163
Josef Bacikd5e20032011-08-04 14:52:27 +00002164 q = bdev_get_queue(bdev);
2165 if (blk_queue_discard(q))
2166 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002167 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002168 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002169 device->io_width = root->sectorsize;
2170 device->io_align = root->sectorsize;
2171 device->sector_size = root->sectorsize;
2172 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002173 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002174 device->commit_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002175 device->dev_root = root->fs_info->dev_root;
2176 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002177 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002178 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002179 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002180 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002181 set_blocksize(device->bdev, 4096);
2182
Yan Zheng2b820322008-11-17 21:11:30 -05002183 if (seeding_dev) {
2184 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002185 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002186 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002187 }
2188
2189 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002190
Chris Masone5e9a522009-06-10 15:17:02 -04002191 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002192 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002193 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002194 list_add(&device->dev_alloc_list,
2195 &root->fs_info->fs_devices->alloc_list);
2196 root->fs_info->fs_devices->num_devices++;
2197 root->fs_info->fs_devices->open_devices++;
2198 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002199 root->fs_info->fs_devices->total_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -05002200 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2201
Josef Bacik2bf64752011-09-26 17:12:22 -04002202 spin_lock(&root->fs_info->free_chunk_lock);
2203 root->fs_info->free_chunk_space += device->total_bytes;
2204 spin_unlock(&root->fs_info->free_chunk_lock);
2205
Chris Masonc2898112009-06-10 09:51:32 -04002206 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2207 root->fs_info->fs_devices->rotating = 1;
2208
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002209 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002210 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002211 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002212
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002213 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002214 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002215 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002216
2217 /* add sysfs device entry */
Anand Jain1ba43812015-03-10 06:38:33 +08002218 btrfs_kobj_add_device(root->fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002219
Miao Xie2196d6e2014-09-03 21:35:41 +08002220 /*
2221 * we've got more storage, clear any full flags on the space
2222 * infos
2223 */
2224 btrfs_clear_space_info_full(root->fs_info);
2225
2226 unlock_chunks(root);
Chris Masone5e9a522009-06-10 15:17:02 -04002227 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002228
Yan Zheng2b820322008-11-17 21:11:30 -05002229 if (seeding_dev) {
Miao Xie2196d6e2014-09-03 21:35:41 +08002230 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05002231 ret = init_first_rw_device(trans, root, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002232 unlock_chunks(root);
David Sterba005d6422012-09-18 07:52:32 -06002233 if (ret) {
2234 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002235 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002236 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002237 }
2238
2239 ret = btrfs_add_device(trans, root, device);
2240 if (ret) {
2241 btrfs_abort_transaction(trans, root, ret);
2242 goto error_trans;
2243 }
2244
2245 if (seeding_dev) {
2246 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2247
Yan Zheng2b820322008-11-17 21:11:30 -05002248 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002249 if (ret) {
2250 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002251 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002252 }
Anand Jainb2373f22014-06-03 11:36:03 +08002253
2254 /* Sprouting would change fsid of the mounted root,
2255 * so rename the fsid on the sysfs
2256 */
2257 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2258 root->fs_info->fsid);
Anand Jain2e7910d2015-03-10 06:38:29 +08002259 if (kobject_rename(&root->fs_info->fs_devices->super_kobj,
2260 fsid_buf))
Anand Jainb2373f22014-06-03 11:36:03 +08002261 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002262 }
2263
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002264 root->fs_info->num_tolerated_disk_barrier_failures =
2265 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002266 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002267
2268 if (seeding_dev) {
2269 mutex_unlock(&uuid_mutex);
2270 up_write(&sb->s_umount);
2271
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002272 if (ret) /* transaction commit */
2273 return ret;
2274
Yan Zheng2b820322008-11-17 21:11:30 -05002275 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002276 if (ret < 0)
2277 btrfs_error(root->fs_info, ret,
2278 "Failed to relocate sys chunks after "
2279 "device initialization. This can be fixed "
2280 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002281 trans = btrfs_attach_transaction(root);
2282 if (IS_ERR(trans)) {
2283 if (PTR_ERR(trans) == -ENOENT)
2284 return 0;
2285 return PTR_ERR(trans);
2286 }
2287 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002288 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002289
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002290 /* Update ctime/mtime for libblkid */
2291 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002292 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002293
2294error_trans:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002295 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002296 rcu_string_free(device->name);
Anand Jain6c14a162015-03-10 06:38:34 +08002297 btrfs_kobj_rm_device(root->fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002298 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002299error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002300 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002301 if (seeding_dev) {
2302 mutex_unlock(&uuid_mutex);
2303 up_write(&sb->s_umount);
2304 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002305 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002306}
2307
Stefan Behrense93c89c2012-11-05 17:33:06 +01002308int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002309 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002310 struct btrfs_device **device_out)
2311{
2312 struct request_queue *q;
2313 struct btrfs_device *device;
2314 struct block_device *bdev;
2315 struct btrfs_fs_info *fs_info = root->fs_info;
2316 struct list_head *devices;
2317 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002318 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002319 int ret = 0;
2320
2321 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002322 if (fs_info->fs_devices->seeding) {
2323 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002324 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002325 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002326
2327 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2328 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002329 if (IS_ERR(bdev)) {
2330 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002331 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002332 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002333
2334 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2335
2336 devices = &fs_info->fs_devices->devices;
2337 list_for_each_entry(device, devices, dev_list) {
2338 if (device->bdev == bdev) {
Miao Xie1c433662014-09-03 21:35:32 +08002339 btrfs_err(fs_info, "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002340 ret = -EEXIST;
2341 goto error;
2342 }
2343 }
2344
Miao Xie1c433662014-09-03 21:35:32 +08002345
Miao Xie7cc8e582014-09-03 21:35:38 +08002346 if (i_size_read(bdev->bd_inode) <
2347 btrfs_device_get_total_bytes(srcdev)) {
Miao Xie1c433662014-09-03 21:35:32 +08002348 btrfs_err(fs_info, "target device is smaller than source device!");
2349 ret = -EINVAL;
2350 goto error;
2351 }
2352
2353
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002354 device = btrfs_alloc_device(NULL, &devid, NULL);
2355 if (IS_ERR(device)) {
2356 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002357 goto error;
2358 }
2359
2360 name = rcu_string_strdup(device_path, GFP_NOFS);
2361 if (!name) {
2362 kfree(device);
2363 ret = -ENOMEM;
2364 goto error;
2365 }
2366 rcu_assign_pointer(device->name, name);
2367
2368 q = bdev_get_queue(bdev);
2369 if (blk_queue_discard(q))
2370 device->can_discard = 1;
2371 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2372 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002373 device->generation = 0;
2374 device->io_width = root->sectorsize;
2375 device->io_align = root->sectorsize;
2376 device->sector_size = root->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002377 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2378 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2379 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002380 ASSERT(list_empty(&srcdev->resized_list));
2381 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002382 device->commit_bytes_used = device->bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002383 device->dev_root = fs_info->dev_root;
2384 device->bdev = bdev;
2385 device->in_fs_metadata = 1;
2386 device->is_tgtdev_for_dev_replace = 1;
2387 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002388 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002389 set_blocksize(device->bdev, 4096);
2390 device->fs_devices = fs_info->fs_devices;
2391 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2392 fs_info->fs_devices->num_devices++;
2393 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002394 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2395
2396 *device_out = device;
2397 return ret;
2398
2399error:
2400 blkdev_put(bdev, FMODE_EXCL);
2401 return ret;
2402}
2403
2404void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2405 struct btrfs_device *tgtdev)
2406{
2407 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2408 tgtdev->io_width = fs_info->dev_root->sectorsize;
2409 tgtdev->io_align = fs_info->dev_root->sectorsize;
2410 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2411 tgtdev->dev_root = fs_info->dev_root;
2412 tgtdev->in_fs_metadata = 1;
2413}
2414
Chris Masond3977122009-01-05 21:25:51 -05002415static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2416 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002417{
2418 int ret;
2419 struct btrfs_path *path;
2420 struct btrfs_root *root;
2421 struct btrfs_dev_item *dev_item;
2422 struct extent_buffer *leaf;
2423 struct btrfs_key key;
2424
2425 root = device->dev_root->fs_info->chunk_root;
2426
2427 path = btrfs_alloc_path();
2428 if (!path)
2429 return -ENOMEM;
2430
2431 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2432 key.type = BTRFS_DEV_ITEM_KEY;
2433 key.offset = device->devid;
2434
2435 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2436 if (ret < 0)
2437 goto out;
2438
2439 if (ret > 0) {
2440 ret = -ENOENT;
2441 goto out;
2442 }
2443
2444 leaf = path->nodes[0];
2445 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2446
2447 btrfs_set_device_id(leaf, dev_item, device->devid);
2448 btrfs_set_device_type(leaf, dev_item, device->type);
2449 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2450 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2451 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002452 btrfs_set_device_total_bytes(leaf, dev_item,
2453 btrfs_device_get_disk_total_bytes(device));
2454 btrfs_set_device_bytes_used(leaf, dev_item,
2455 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002456 btrfs_mark_buffer_dirty(leaf);
2457
2458out:
2459 btrfs_free_path(path);
2460 return ret;
2461}
2462
Miao Xie2196d6e2014-09-03 21:35:41 +08002463int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002464 struct btrfs_device *device, u64 new_size)
2465{
2466 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002467 device->dev_root->fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002468 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002469 u64 old_total;
2470 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002471
Yan Zheng2b820322008-11-17 21:11:30 -05002472 if (!device->writeable)
2473 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002474
2475 lock_chunks(device->dev_root);
2476 old_total = btrfs_super_total_bytes(super_copy);
2477 diff = new_size - device->total_bytes;
2478
Stefan Behrens63a212a2012-11-05 18:29:28 +01002479 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002480 device->is_tgtdev_for_dev_replace) {
2481 unlock_chunks(device->dev_root);
Yan Zheng2b820322008-11-17 21:11:30 -05002482 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002483 }
Yan Zheng2b820322008-11-17 21:11:30 -05002484
Miao Xie935e5cc2014-09-03 21:35:33 +08002485 fs_devices = device->dev_root->fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002486
2487 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002488 device->fs_devices->total_rw_bytes += diff;
2489
Miao Xie7cc8e582014-09-03 21:35:38 +08002490 btrfs_device_set_total_bytes(device, new_size);
2491 btrfs_device_set_disk_total_bytes(device, new_size);
Chris Mason4184ea72009-03-10 12:39:20 -04002492 btrfs_clear_space_info_full(device->dev_root->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002493 if (list_empty(&device->resized_list))
2494 list_add_tail(&device->resized_list,
2495 &fs_devices->resized_devices);
Miao Xie2196d6e2014-09-03 21:35:41 +08002496 unlock_chunks(device->dev_root);
Chris Mason4184ea72009-03-10 12:39:20 -04002497
Chris Mason8f18cf12008-04-25 16:53:30 -04002498 return btrfs_update_device(trans, device);
2499}
2500
2501static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Zhao Leia688a042015-02-09 20:31:44 +08002502 struct btrfs_root *root, u64 chunk_objectid,
Chris Mason8f18cf12008-04-25 16:53:30 -04002503 u64 chunk_offset)
2504{
2505 int ret;
2506 struct btrfs_path *path;
2507 struct btrfs_key key;
2508
2509 root = root->fs_info->chunk_root;
2510 path = btrfs_alloc_path();
2511 if (!path)
2512 return -ENOMEM;
2513
2514 key.objectid = chunk_objectid;
2515 key.offset = chunk_offset;
2516 key.type = BTRFS_CHUNK_ITEM_KEY;
2517
2518 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002519 if (ret < 0)
2520 goto out;
2521 else if (ret > 0) { /* Logic error or corruption */
2522 btrfs_error(root->fs_info, -ENOENT,
2523 "Failed lookup while freeing chunk.");
2524 ret = -ENOENT;
2525 goto out;
2526 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002527
2528 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002529 if (ret < 0)
2530 btrfs_error(root->fs_info, ret,
2531 "Failed to delete chunk item.");
2532out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002533 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002534 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002535}
2536
Christoph Hellwigb2950862008-12-02 09:54:17 -05002537static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002538 chunk_offset)
2539{
David Sterba6c417612011-04-13 15:41:04 +02002540 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002541 struct btrfs_disk_key *disk_key;
2542 struct btrfs_chunk *chunk;
2543 u8 *ptr;
2544 int ret = 0;
2545 u32 num_stripes;
2546 u32 array_size;
2547 u32 len = 0;
2548 u32 cur;
2549 struct btrfs_key key;
2550
Miao Xie2196d6e2014-09-03 21:35:41 +08002551 lock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002552 array_size = btrfs_super_sys_array_size(super_copy);
2553
2554 ptr = super_copy->sys_chunk_array;
2555 cur = 0;
2556
2557 while (cur < array_size) {
2558 disk_key = (struct btrfs_disk_key *)ptr;
2559 btrfs_disk_key_to_cpu(&key, disk_key);
2560
2561 len = sizeof(*disk_key);
2562
2563 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2564 chunk = (struct btrfs_chunk *)(ptr + len);
2565 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2566 len += btrfs_chunk_item_size(num_stripes);
2567 } else {
2568 ret = -EIO;
2569 break;
2570 }
2571 if (key.objectid == chunk_objectid &&
2572 key.offset == chunk_offset) {
2573 memmove(ptr, ptr + len, array_size - (cur + len));
2574 array_size -= len;
2575 btrfs_set_super_sys_array_size(super_copy, array_size);
2576 } else {
2577 ptr += len;
2578 cur += len;
2579 }
2580 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002581 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002582 return ret;
2583}
2584
Josef Bacik47ab2a62014-09-18 11:20:02 -04002585int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2586 struct btrfs_root *root, u64 chunk_offset)
2587{
2588 struct extent_map_tree *em_tree;
2589 struct extent_map *em;
2590 struct btrfs_root *extent_root = root->fs_info->extent_root;
2591 struct map_lookup *map;
2592 u64 dev_extent_len = 0;
2593 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002594 int i, ret = 0;
2595
2596 /* Just in case */
2597 root = root->fs_info->chunk_root;
2598 em_tree = &root->fs_info->mapping_tree.map_tree;
2599
2600 read_lock(&em_tree->lock);
2601 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2602 read_unlock(&em_tree->lock);
2603
2604 if (!em || em->start > chunk_offset ||
2605 em->start + em->len < chunk_offset) {
2606 /*
2607 * This is a logic error, but we don't want to just rely on the
2608 * user having built with ASSERT enabled, so if ASSERT doens't
2609 * do anything we still error out.
2610 */
2611 ASSERT(0);
2612 if (em)
2613 free_extent_map(em);
2614 return -EINVAL;
2615 }
2616 map = (struct map_lookup *)em->bdev;
2617
2618 for (i = 0; i < map->num_stripes; i++) {
2619 struct btrfs_device *device = map->stripes[i].dev;
2620 ret = btrfs_free_dev_extent(trans, device,
2621 map->stripes[i].physical,
2622 &dev_extent_len);
2623 if (ret) {
2624 btrfs_abort_transaction(trans, root, ret);
2625 goto out;
2626 }
2627
2628 if (device->bytes_used > 0) {
2629 lock_chunks(root);
2630 btrfs_device_set_bytes_used(device,
2631 device->bytes_used - dev_extent_len);
2632 spin_lock(&root->fs_info->free_chunk_lock);
2633 root->fs_info->free_chunk_space += dev_extent_len;
2634 spin_unlock(&root->fs_info->free_chunk_lock);
2635 btrfs_clear_space_info_full(root->fs_info);
2636 unlock_chunks(root);
2637 }
2638
2639 if (map->stripes[i].dev) {
2640 ret = btrfs_update_device(trans, map->stripes[i].dev);
2641 if (ret) {
2642 btrfs_abort_transaction(trans, root, ret);
2643 goto out;
2644 }
2645 }
2646 }
Zhao Leia688a042015-02-09 20:31:44 +08002647 ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002648 if (ret) {
2649 btrfs_abort_transaction(trans, root, ret);
2650 goto out;
2651 }
2652
2653 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2654
2655 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2656 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2657 if (ret) {
2658 btrfs_abort_transaction(trans, root, ret);
2659 goto out;
2660 }
2661 }
2662
Filipe Manana04216822014-11-27 21:14:15 +00002663 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002664 if (ret) {
2665 btrfs_abort_transaction(trans, extent_root, ret);
2666 goto out;
2667 }
2668
Josef Bacik47ab2a62014-09-18 11:20:02 -04002669out:
2670 /* once for us */
2671 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002672 return ret;
2673}
2674
Christoph Hellwigb2950862008-12-02 09:54:17 -05002675static int btrfs_relocate_chunk(struct btrfs_root *root,
Zhao Leia688a042015-02-09 20:31:44 +08002676 u64 chunk_objectid,
2677 u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002678{
Chris Mason8f18cf12008-04-25 16:53:30 -04002679 struct btrfs_root *extent_root;
2680 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002681 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002682
2683 root = root->fs_info->chunk_root;
2684 extent_root = root->fs_info->extent_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002685
Josef Bacikba1bf482009-09-11 16:11:19 -04002686 ret = btrfs_can_relocate(extent_root, chunk_offset);
2687 if (ret)
2688 return -ENOSPC;
2689
Chris Mason8f18cf12008-04-25 16:53:30 -04002690 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002691 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002692 if (ret)
2693 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002694
Yan, Zhenga22285a2010-05-16 10:48:46 -04002695 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002696 if (IS_ERR(trans)) {
2697 ret = PTR_ERR(trans);
2698 btrfs_std_error(root->fs_info, ret);
2699 return ret;
2700 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002701
2702 /*
2703 * step two, delete the device extents and the
2704 * chunk tree entries
2705 */
Josef Bacik47ab2a62014-09-18 11:20:02 -04002706 ret = btrfs_remove_chunk(trans, root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002707 btrfs_end_transaction(trans, root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002708 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002709}
2710
Yan Zheng2b820322008-11-17 21:11:30 -05002711static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2712{
2713 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2714 struct btrfs_path *path;
2715 struct extent_buffer *leaf;
2716 struct btrfs_chunk *chunk;
2717 struct btrfs_key key;
2718 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002719 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002720 bool retried = false;
2721 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002722 int ret;
2723
2724 path = btrfs_alloc_path();
2725 if (!path)
2726 return -ENOMEM;
2727
Josef Bacikba1bf482009-09-11 16:11:19 -04002728again:
Yan Zheng2b820322008-11-17 21:11:30 -05002729 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2730 key.offset = (u64)-1;
2731 key.type = BTRFS_CHUNK_ITEM_KEY;
2732
2733 while (1) {
2734 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2735 if (ret < 0)
2736 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002737 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002738
2739 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2740 key.type);
2741 if (ret < 0)
2742 goto error;
2743 if (ret > 0)
2744 break;
2745
2746 leaf = path->nodes[0];
2747 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2748
2749 chunk = btrfs_item_ptr(leaf, path->slots[0],
2750 struct btrfs_chunk);
2751 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002752 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002753
2754 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Zhao Leia688a042015-02-09 20:31:44 +08002755 ret = btrfs_relocate_chunk(chunk_root,
Yan Zheng2b820322008-11-17 21:11:30 -05002756 found_key.objectid,
2757 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002758 if (ret == -ENOSPC)
2759 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302760 else
2761 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002762 }
2763
2764 if (found_key.offset == 0)
2765 break;
2766 key.offset = found_key.offset - 1;
2767 }
2768 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002769 if (failed && !retried) {
2770 failed = 0;
2771 retried = true;
2772 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302773 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002774 ret = -ENOSPC;
2775 }
Yan Zheng2b820322008-11-17 21:11:30 -05002776error:
2777 btrfs_free_path(path);
2778 return ret;
2779}
2780
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002781static int insert_balance_item(struct btrfs_root *root,
2782 struct btrfs_balance_control *bctl)
2783{
2784 struct btrfs_trans_handle *trans;
2785 struct btrfs_balance_item *item;
2786 struct btrfs_disk_balance_args disk_bargs;
2787 struct btrfs_path *path;
2788 struct extent_buffer *leaf;
2789 struct btrfs_key key;
2790 int ret, err;
2791
2792 path = btrfs_alloc_path();
2793 if (!path)
2794 return -ENOMEM;
2795
2796 trans = btrfs_start_transaction(root, 0);
2797 if (IS_ERR(trans)) {
2798 btrfs_free_path(path);
2799 return PTR_ERR(trans);
2800 }
2801
2802 key.objectid = BTRFS_BALANCE_OBJECTID;
2803 key.type = BTRFS_BALANCE_ITEM_KEY;
2804 key.offset = 0;
2805
2806 ret = btrfs_insert_empty_item(trans, root, path, &key,
2807 sizeof(*item));
2808 if (ret)
2809 goto out;
2810
2811 leaf = path->nodes[0];
2812 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2813
2814 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2815
2816 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2817 btrfs_set_balance_data(leaf, item, &disk_bargs);
2818 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2819 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2820 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2821 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2822
2823 btrfs_set_balance_flags(leaf, item, bctl->flags);
2824
2825 btrfs_mark_buffer_dirty(leaf);
2826out:
2827 btrfs_free_path(path);
2828 err = btrfs_commit_transaction(trans, root);
2829 if (err && !ret)
2830 ret = err;
2831 return ret;
2832}
2833
2834static int del_balance_item(struct btrfs_root *root)
2835{
2836 struct btrfs_trans_handle *trans;
2837 struct btrfs_path *path;
2838 struct btrfs_key key;
2839 int ret, err;
2840
2841 path = btrfs_alloc_path();
2842 if (!path)
2843 return -ENOMEM;
2844
2845 trans = btrfs_start_transaction(root, 0);
2846 if (IS_ERR(trans)) {
2847 btrfs_free_path(path);
2848 return PTR_ERR(trans);
2849 }
2850
2851 key.objectid = BTRFS_BALANCE_OBJECTID;
2852 key.type = BTRFS_BALANCE_ITEM_KEY;
2853 key.offset = 0;
2854
2855 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2856 if (ret < 0)
2857 goto out;
2858 if (ret > 0) {
2859 ret = -ENOENT;
2860 goto out;
2861 }
2862
2863 ret = btrfs_del_item(trans, root, path);
2864out:
2865 btrfs_free_path(path);
2866 err = btrfs_commit_transaction(trans, root);
2867 if (err && !ret)
2868 ret = err;
2869 return ret;
2870}
2871
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002872/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002873 * This is a heuristic used to reduce the number of chunks balanced on
2874 * resume after balance was interrupted.
2875 */
2876static void update_balance_args(struct btrfs_balance_control *bctl)
2877{
2878 /*
2879 * Turn on soft mode for chunk types that were being converted.
2880 */
2881 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2882 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2883 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2884 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2885 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2886 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2887
2888 /*
2889 * Turn on usage filter if is not already used. The idea is
2890 * that chunks that we have already balanced should be
2891 * reasonably full. Don't do it for chunks that are being
2892 * converted - that will keep us from relocating unconverted
2893 * (albeit full) chunks.
2894 */
2895 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2896 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2897 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2898 bctl->data.usage = 90;
2899 }
2900 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2901 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2902 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2903 bctl->sys.usage = 90;
2904 }
2905 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2906 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2907 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2908 bctl->meta.usage = 90;
2909 }
2910}
2911
2912/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002913 * Should be called with both balance and volume mutexes held to
2914 * serialize other volume operations (add_dev/rm_dev/resize) with
2915 * restriper. Same goes for unset_balance_control.
2916 */
2917static void set_balance_control(struct btrfs_balance_control *bctl)
2918{
2919 struct btrfs_fs_info *fs_info = bctl->fs_info;
2920
2921 BUG_ON(fs_info->balance_ctl);
2922
2923 spin_lock(&fs_info->balance_lock);
2924 fs_info->balance_ctl = bctl;
2925 spin_unlock(&fs_info->balance_lock);
2926}
2927
2928static void unset_balance_control(struct btrfs_fs_info *fs_info)
2929{
2930 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2931
2932 BUG_ON(!fs_info->balance_ctl);
2933
2934 spin_lock(&fs_info->balance_lock);
2935 fs_info->balance_ctl = NULL;
2936 spin_unlock(&fs_info->balance_lock);
2937
2938 kfree(bctl);
2939}
2940
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002941/*
2942 * Balance filters. Return 1 if chunk should be filtered out
2943 * (should not be balanced).
2944 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002945static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002946 struct btrfs_balance_args *bargs)
2947{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002948 chunk_type = chunk_to_extended(chunk_type) &
2949 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002950
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002951 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002952 return 0;
2953
2954 return 1;
2955}
2956
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002957static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2958 struct btrfs_balance_args *bargs)
2959{
2960 struct btrfs_block_group_cache *cache;
2961 u64 chunk_used, user_thresh;
2962 int ret = 1;
2963
2964 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2965 chunk_used = btrfs_block_group_used(&cache->item);
2966
Ilya Dryomova105bb82013-01-21 15:15:56 +02002967 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002968 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002969 else if (bargs->usage > 100)
2970 user_thresh = cache->key.offset;
2971 else
2972 user_thresh = div_factor_fine(cache->key.offset,
2973 bargs->usage);
2974
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002975 if (chunk_used < user_thresh)
2976 ret = 0;
2977
2978 btrfs_put_block_group(cache);
2979 return ret;
2980}
2981
Ilya Dryomov409d4042012-01-16 22:04:47 +02002982static int chunk_devid_filter(struct extent_buffer *leaf,
2983 struct btrfs_chunk *chunk,
2984 struct btrfs_balance_args *bargs)
2985{
2986 struct btrfs_stripe *stripe;
2987 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2988 int i;
2989
2990 for (i = 0; i < num_stripes; i++) {
2991 stripe = btrfs_stripe_nr(chunk, i);
2992 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2993 return 0;
2994 }
2995
2996 return 1;
2997}
2998
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002999/* [pstart, pend) */
3000static int chunk_drange_filter(struct extent_buffer *leaf,
3001 struct btrfs_chunk *chunk,
3002 u64 chunk_offset,
3003 struct btrfs_balance_args *bargs)
3004{
3005 struct btrfs_stripe *stripe;
3006 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3007 u64 stripe_offset;
3008 u64 stripe_length;
3009 int factor;
3010 int i;
3011
3012 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3013 return 0;
3014
3015 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003016 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3017 factor = num_stripes / 2;
3018 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3019 factor = num_stripes - 1;
3020 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3021 factor = num_stripes - 2;
3022 } else {
3023 factor = num_stripes;
3024 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003025
3026 for (i = 0; i < num_stripes; i++) {
3027 stripe = btrfs_stripe_nr(chunk, i);
3028 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3029 continue;
3030
3031 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3032 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003033 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003034
3035 if (stripe_offset < bargs->pend &&
3036 stripe_offset + stripe_length > bargs->pstart)
3037 return 0;
3038 }
3039
3040 return 1;
3041}
3042
Ilya Dryomovea671762012-01-16 22:04:48 +02003043/* [vstart, vend) */
3044static int chunk_vrange_filter(struct extent_buffer *leaf,
3045 struct btrfs_chunk *chunk,
3046 u64 chunk_offset,
3047 struct btrfs_balance_args *bargs)
3048{
3049 if (chunk_offset < bargs->vend &&
3050 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3051 /* at least part of the chunk is inside this vrange */
3052 return 0;
3053
3054 return 1;
3055}
3056
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003057static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003058 struct btrfs_balance_args *bargs)
3059{
3060 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3061 return 0;
3062
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003063 chunk_type = chunk_to_extended(chunk_type) &
3064 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003065
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003066 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003067 return 1;
3068
3069 return 0;
3070}
3071
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003072static int should_balance_chunk(struct btrfs_root *root,
3073 struct extent_buffer *leaf,
3074 struct btrfs_chunk *chunk, u64 chunk_offset)
3075{
3076 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3077 struct btrfs_balance_args *bargs = NULL;
3078 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3079
3080 /* type filter */
3081 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3082 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3083 return 0;
3084 }
3085
3086 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3087 bargs = &bctl->data;
3088 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3089 bargs = &bctl->sys;
3090 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3091 bargs = &bctl->meta;
3092
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003093 /* profiles filter */
3094 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3095 chunk_profiles_filter(chunk_type, bargs)) {
3096 return 0;
3097 }
3098
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003099 /* usage filter */
3100 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3101 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3102 return 0;
3103 }
3104
Ilya Dryomov409d4042012-01-16 22:04:47 +02003105 /* devid filter */
3106 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3107 chunk_devid_filter(leaf, chunk, bargs)) {
3108 return 0;
3109 }
3110
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003111 /* drange filter, makes sense only with devid filter */
3112 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3113 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3114 return 0;
3115 }
3116
Ilya Dryomovea671762012-01-16 22:04:48 +02003117 /* vrange filter */
3118 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3119 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3120 return 0;
3121 }
3122
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003123 /* soft profile changing mode */
3124 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3125 chunk_soft_convert_filter(chunk_type, bargs)) {
3126 return 0;
3127 }
3128
David Sterba7d824b62014-05-07 17:37:51 +02003129 /*
3130 * limited by count, must be the last filter
3131 */
3132 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3133 if (bargs->limit == 0)
3134 return 0;
3135 else
3136 bargs->limit--;
3137 }
3138
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003139 return 1;
3140}
3141
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003142static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003143{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003144 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003145 struct btrfs_root *chunk_root = fs_info->chunk_root;
3146 struct btrfs_root *dev_root = fs_info->dev_root;
3147 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003148 struct btrfs_device *device;
3149 u64 old_size;
3150 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003151 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003152 struct btrfs_path *path;
3153 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003154 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003155 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003156 struct extent_buffer *leaf;
3157 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003158 int ret;
3159 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003160 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003161 u64 limit_data = bctl->data.limit;
3162 u64 limit_meta = bctl->meta.limit;
3163 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003164
Chris Masonec44a352008-04-28 15:29:52 -04003165 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003166 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003167 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003168 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003169 size_to_free = div_factor(old_size, 1);
3170 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003171 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003172 btrfs_device_get_total_bytes(device) -
3173 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003174 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003175 continue;
3176
3177 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003178 if (ret == -ENOSPC)
3179 break;
Chris Masonec44a352008-04-28 15:29:52 -04003180 BUG_ON(ret);
3181
Yan, Zhenga22285a2010-05-16 10:48:46 -04003182 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003183 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003184
3185 ret = btrfs_grow_device(trans, device, old_size);
3186 BUG_ON(ret);
3187
3188 btrfs_end_transaction(trans, dev_root);
3189 }
3190
3191 /* step two, relocate all the chunks */
3192 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003193 if (!path) {
3194 ret = -ENOMEM;
3195 goto error;
3196 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003197
3198 /* zero out stat counters */
3199 spin_lock(&fs_info->balance_lock);
3200 memset(&bctl->stat, 0, sizeof(bctl->stat));
3201 spin_unlock(&fs_info->balance_lock);
3202again:
David Sterba7d824b62014-05-07 17:37:51 +02003203 if (!counting) {
3204 bctl->data.limit = limit_data;
3205 bctl->meta.limit = limit_meta;
3206 bctl->sys.limit = limit_sys;
3207 }
Chris Masonec44a352008-04-28 15:29:52 -04003208 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3209 key.offset = (u64)-1;
3210 key.type = BTRFS_CHUNK_ITEM_KEY;
3211
Chris Masond3977122009-01-05 21:25:51 -05003212 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003213 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003214 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003215 ret = -ECANCELED;
3216 goto error;
3217 }
3218
Chris Masonec44a352008-04-28 15:29:52 -04003219 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3220 if (ret < 0)
3221 goto error;
3222
3223 /*
3224 * this shouldn't happen, it means the last relocate
3225 * failed
3226 */
3227 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003228 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003229
3230 ret = btrfs_previous_item(chunk_root, path, 0,
3231 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003232 if (ret) {
3233 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003234 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003235 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003236
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003237 leaf = path->nodes[0];
3238 slot = path->slots[0];
3239 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3240
Chris Masonec44a352008-04-28 15:29:52 -04003241 if (found_key.objectid != key.objectid)
3242 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003243
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003244 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3245
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003246 if (!counting) {
3247 spin_lock(&fs_info->balance_lock);
3248 bctl->stat.considered++;
3249 spin_unlock(&fs_info->balance_lock);
3250 }
3251
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003252 ret = should_balance_chunk(chunk_root, leaf, chunk,
3253 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003254 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003255 if (!ret)
3256 goto loop;
3257
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003258 if (counting) {
3259 spin_lock(&fs_info->balance_lock);
3260 bctl->stat.expected++;
3261 spin_unlock(&fs_info->balance_lock);
3262 goto loop;
3263 }
3264
Chris Masonec44a352008-04-28 15:29:52 -04003265 ret = btrfs_relocate_chunk(chunk_root,
Chris Masonec44a352008-04-28 15:29:52 -04003266 found_key.objectid,
3267 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003268 if (ret && ret != -ENOSPC)
3269 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003270 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003271 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003272 } else {
3273 spin_lock(&fs_info->balance_lock);
3274 bctl->stat.completed++;
3275 spin_unlock(&fs_info->balance_lock);
3276 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003277loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003278 if (found_key.offset == 0)
3279 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003280 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003281 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003282
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003283 if (counting) {
3284 btrfs_release_path(path);
3285 counting = false;
3286 goto again;
3287 }
Chris Masonec44a352008-04-28 15:29:52 -04003288error:
3289 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003290 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003291 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003292 enospc_errors);
3293 if (!ret)
3294 ret = -ENOSPC;
3295 }
3296
Chris Masonec44a352008-04-28 15:29:52 -04003297 return ret;
3298}
3299
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003300/**
3301 * alloc_profile_is_valid - see if a given profile is valid and reduced
3302 * @flags: profile to validate
3303 * @extended: if true @flags is treated as an extended profile
3304 */
3305static int alloc_profile_is_valid(u64 flags, int extended)
3306{
3307 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3308 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3309
3310 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3311
3312 /* 1) check that all other bits are zeroed */
3313 if (flags & ~mask)
3314 return 0;
3315
3316 /* 2) see if profile is reduced */
3317 if (flags == 0)
3318 return !extended; /* "0" is valid for usual profiles */
3319
3320 /* true if exactly one bit set */
3321 return (flags & (flags - 1)) == 0;
3322}
3323
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003324static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3325{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003326 /* cancel requested || normal exit path */
3327 return atomic_read(&fs_info->balance_cancel_req) ||
3328 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3329 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003330}
3331
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003332static void __cancel_balance(struct btrfs_fs_info *fs_info)
3333{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003334 int ret;
3335
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003336 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003337 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003338 if (ret)
3339 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003340
3341 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003342}
3343
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003344/*
3345 * Should be called with both balance and volume mutexes held
3346 */
3347int btrfs_balance(struct btrfs_balance_control *bctl,
3348 struct btrfs_ioctl_balance_args *bargs)
3349{
3350 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003351 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003352 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003353 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003354 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003355 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003356
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003357 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003358 atomic_read(&fs_info->balance_pause_req) ||
3359 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003360 ret = -EINVAL;
3361 goto out;
3362 }
3363
Ilya Dryomove4837f82012-03-27 17:09:17 +03003364 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3365 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3366 mixed = 1;
3367
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003368 /*
3369 * In case of mixed groups both data and meta should be picked,
3370 * and identical options should be given for both of them.
3371 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003372 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3373 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003374 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3375 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3376 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003377 btrfs_err(fs_info, "with mixed groups data and "
3378 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003379 ret = -EINVAL;
3380 goto out;
3381 }
3382 }
3383
Stefan Behrens8dabb742012-11-06 13:15:27 +01003384 num_devices = fs_info->fs_devices->num_devices;
3385 btrfs_dev_replace_lock(&fs_info->dev_replace);
3386 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3387 BUG_ON(num_devices < 1);
3388 num_devices--;
3389 }
3390 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003391 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003392 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003393 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003394 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003395 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003396 if (num_devices > 2)
3397 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3398 if (num_devices > 3)
3399 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3400 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003401 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3402 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3403 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003404 btrfs_err(fs_info, "unable to start balance with target "
3405 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003406 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003407 ret = -EINVAL;
3408 goto out;
3409 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003410 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3411 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3412 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003413 btrfs_err(fs_info,
3414 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003415 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003416 ret = -EINVAL;
3417 goto out;
3418 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003419 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3420 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3421 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003422 btrfs_err(fs_info,
3423 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003424 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003425 ret = -EINVAL;
3426 goto out;
3427 }
3428
Ilya Dryomove4837f82012-03-27 17:09:17 +03003429 /* allow dup'ed data chunks only in mixed mode */
3430 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003431 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003432 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003433 ret = -EINVAL;
3434 goto out;
3435 }
3436
3437 /* allow to reduce meta or sys integrity only if force set */
3438 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003439 BTRFS_BLOCK_GROUP_RAID10 |
3440 BTRFS_BLOCK_GROUP_RAID5 |
3441 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003442 do {
3443 seq = read_seqbegin(&fs_info->profiles_lock);
3444
3445 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3446 (fs_info->avail_system_alloc_bits & allowed) &&
3447 !(bctl->sys.target & allowed)) ||
3448 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3449 (fs_info->avail_metadata_alloc_bits & allowed) &&
3450 !(bctl->meta.target & allowed))) {
3451 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003452 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003453 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003454 btrfs_err(fs_info, "balance will reduce metadata "
3455 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003456 ret = -EINVAL;
3457 goto out;
3458 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003459 }
Miao Xiede98ced2013-01-29 10:13:12 +00003460 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003461
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003462 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3463 int num_tolerated_disk_barrier_failures;
3464 u64 target = bctl->sys.target;
3465
3466 num_tolerated_disk_barrier_failures =
3467 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3468 if (num_tolerated_disk_barrier_failures > 0 &&
3469 (target &
3470 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3471 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3472 num_tolerated_disk_barrier_failures = 0;
3473 else if (num_tolerated_disk_barrier_failures > 1 &&
3474 (target &
3475 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3476 num_tolerated_disk_barrier_failures = 1;
3477
3478 fs_info->num_tolerated_disk_barrier_failures =
3479 num_tolerated_disk_barrier_failures;
3480 }
3481
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003482 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003483 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003484 goto out;
3485
Ilya Dryomov59641012012-01-16 22:04:48 +02003486 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3487 BUG_ON(ret == -EEXIST);
3488 set_balance_control(bctl);
3489 } else {
3490 BUG_ON(ret != -EEXIST);
3491 spin_lock(&fs_info->balance_lock);
3492 update_balance_args(bctl);
3493 spin_unlock(&fs_info->balance_lock);
3494 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003495
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003496 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003497 mutex_unlock(&fs_info->balance_mutex);
3498
3499 ret = __btrfs_balance(fs_info);
3500
3501 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003502 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003503
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003504 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3505 fs_info->num_tolerated_disk_barrier_failures =
3506 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3507 }
3508
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003509 if (bargs) {
3510 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003511 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003512 }
3513
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003514 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3515 balance_need_close(fs_info)) {
3516 __cancel_balance(fs_info);
3517 }
3518
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003519 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003520
3521 return ret;
3522out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003523 if (bctl->flags & BTRFS_BALANCE_RESUME)
3524 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003525 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003526 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003527 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3528 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003529 return ret;
3530}
3531
3532static int balance_kthread(void *data)
3533{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003534 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003535 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003536
3537 mutex_lock(&fs_info->volume_mutex);
3538 mutex_lock(&fs_info->balance_mutex);
3539
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003540 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003541 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003542 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003543 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003544
3545 mutex_unlock(&fs_info->balance_mutex);
3546 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003547
Ilya Dryomov59641012012-01-16 22:04:48 +02003548 return ret;
3549}
3550
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003551int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3552{
3553 struct task_struct *tsk;
3554
3555 spin_lock(&fs_info->balance_lock);
3556 if (!fs_info->balance_ctl) {
3557 spin_unlock(&fs_info->balance_lock);
3558 return 0;
3559 }
3560 spin_unlock(&fs_info->balance_lock);
3561
3562 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003563 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003564 return 0;
3565 }
3566
3567 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303568 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003569}
3570
Ilya Dryomov68310a52012-06-22 12:24:12 -06003571int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003572{
Ilya Dryomov59641012012-01-16 22:04:48 +02003573 struct btrfs_balance_control *bctl;
3574 struct btrfs_balance_item *item;
3575 struct btrfs_disk_balance_args disk_bargs;
3576 struct btrfs_path *path;
3577 struct extent_buffer *leaf;
3578 struct btrfs_key key;
3579 int ret;
3580
3581 path = btrfs_alloc_path();
3582 if (!path)
3583 return -ENOMEM;
3584
Ilya Dryomov68310a52012-06-22 12:24:12 -06003585 key.objectid = BTRFS_BALANCE_OBJECTID;
3586 key.type = BTRFS_BALANCE_ITEM_KEY;
3587 key.offset = 0;
3588
3589 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3590 if (ret < 0)
3591 goto out;
3592 if (ret > 0) { /* ret = -ENOENT; */
3593 ret = 0;
3594 goto out;
3595 }
3596
Ilya Dryomov59641012012-01-16 22:04:48 +02003597 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3598 if (!bctl) {
3599 ret = -ENOMEM;
3600 goto out;
3601 }
3602
Ilya Dryomov59641012012-01-16 22:04:48 +02003603 leaf = path->nodes[0];
3604 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3605
Ilya Dryomov68310a52012-06-22 12:24:12 -06003606 bctl->fs_info = fs_info;
3607 bctl->flags = btrfs_balance_flags(leaf, item);
3608 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003609
3610 btrfs_balance_data(leaf, item, &disk_bargs);
3611 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3612 btrfs_balance_meta(leaf, item, &disk_bargs);
3613 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3614 btrfs_balance_sys(leaf, item, &disk_bargs);
3615 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3616
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003617 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3618
Ilya Dryomov68310a52012-06-22 12:24:12 -06003619 mutex_lock(&fs_info->volume_mutex);
3620 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003621
Ilya Dryomov68310a52012-06-22 12:24:12 -06003622 set_balance_control(bctl);
3623
3624 mutex_unlock(&fs_info->balance_mutex);
3625 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003626out:
3627 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003628 return ret;
3629}
3630
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003631int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3632{
3633 int ret = 0;
3634
3635 mutex_lock(&fs_info->balance_mutex);
3636 if (!fs_info->balance_ctl) {
3637 mutex_unlock(&fs_info->balance_mutex);
3638 return -ENOTCONN;
3639 }
3640
3641 if (atomic_read(&fs_info->balance_running)) {
3642 atomic_inc(&fs_info->balance_pause_req);
3643 mutex_unlock(&fs_info->balance_mutex);
3644
3645 wait_event(fs_info->balance_wait_q,
3646 atomic_read(&fs_info->balance_running) == 0);
3647
3648 mutex_lock(&fs_info->balance_mutex);
3649 /* we are good with balance_ctl ripped off from under us */
3650 BUG_ON(atomic_read(&fs_info->balance_running));
3651 atomic_dec(&fs_info->balance_pause_req);
3652 } else {
3653 ret = -ENOTCONN;
3654 }
3655
3656 mutex_unlock(&fs_info->balance_mutex);
3657 return ret;
3658}
3659
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003660int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3661{
Ilya Dryomove649e582013-10-10 20:40:21 +03003662 if (fs_info->sb->s_flags & MS_RDONLY)
3663 return -EROFS;
3664
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003665 mutex_lock(&fs_info->balance_mutex);
3666 if (!fs_info->balance_ctl) {
3667 mutex_unlock(&fs_info->balance_mutex);
3668 return -ENOTCONN;
3669 }
3670
3671 atomic_inc(&fs_info->balance_cancel_req);
3672 /*
3673 * if we are running just wait and return, balance item is
3674 * deleted in btrfs_balance in this case
3675 */
3676 if (atomic_read(&fs_info->balance_running)) {
3677 mutex_unlock(&fs_info->balance_mutex);
3678 wait_event(fs_info->balance_wait_q,
3679 atomic_read(&fs_info->balance_running) == 0);
3680 mutex_lock(&fs_info->balance_mutex);
3681 } else {
3682 /* __cancel_balance needs volume_mutex */
3683 mutex_unlock(&fs_info->balance_mutex);
3684 mutex_lock(&fs_info->volume_mutex);
3685 mutex_lock(&fs_info->balance_mutex);
3686
3687 if (fs_info->balance_ctl)
3688 __cancel_balance(fs_info);
3689
3690 mutex_unlock(&fs_info->volume_mutex);
3691 }
3692
3693 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3694 atomic_dec(&fs_info->balance_cancel_req);
3695 mutex_unlock(&fs_info->balance_mutex);
3696 return 0;
3697}
3698
Stefan Behrens803b2f52013-08-15 17:11:21 +02003699static int btrfs_uuid_scan_kthread(void *data)
3700{
3701 struct btrfs_fs_info *fs_info = data;
3702 struct btrfs_root *root = fs_info->tree_root;
3703 struct btrfs_key key;
3704 struct btrfs_key max_key;
3705 struct btrfs_path *path = NULL;
3706 int ret = 0;
3707 struct extent_buffer *eb;
3708 int slot;
3709 struct btrfs_root_item root_item;
3710 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003711 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003712
3713 path = btrfs_alloc_path();
3714 if (!path) {
3715 ret = -ENOMEM;
3716 goto out;
3717 }
3718
3719 key.objectid = 0;
3720 key.type = BTRFS_ROOT_ITEM_KEY;
3721 key.offset = 0;
3722
3723 max_key.objectid = (u64)-1;
3724 max_key.type = BTRFS_ROOT_ITEM_KEY;
3725 max_key.offset = (u64)-1;
3726
Stefan Behrens803b2f52013-08-15 17:11:21 +02003727 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003728 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003729 if (ret) {
3730 if (ret > 0)
3731 ret = 0;
3732 break;
3733 }
3734
3735 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3736 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3737 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3738 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3739 goto skip;
3740
3741 eb = path->nodes[0];
3742 slot = path->slots[0];
3743 item_size = btrfs_item_size_nr(eb, slot);
3744 if (item_size < sizeof(root_item))
3745 goto skip;
3746
Stefan Behrens803b2f52013-08-15 17:11:21 +02003747 read_extent_buffer(eb, &root_item,
3748 btrfs_item_ptr_offset(eb, slot),
3749 (int)sizeof(root_item));
3750 if (btrfs_root_refs(&root_item) == 0)
3751 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003752
3753 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3754 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3755 if (trans)
3756 goto update_tree;
3757
3758 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003759 /*
3760 * 1 - subvol uuid item
3761 * 1 - received_subvol uuid item
3762 */
3763 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3764 if (IS_ERR(trans)) {
3765 ret = PTR_ERR(trans);
3766 break;
3767 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003768 continue;
3769 } else {
3770 goto skip;
3771 }
3772update_tree:
3773 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003774 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3775 root_item.uuid,
3776 BTRFS_UUID_KEY_SUBVOL,
3777 key.objectid);
3778 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003779 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003780 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003781 break;
3782 }
3783 }
3784
3785 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003786 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3787 root_item.received_uuid,
3788 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3789 key.objectid);
3790 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003791 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003792 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003793 break;
3794 }
3795 }
3796
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003797skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003798 if (trans) {
3799 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003800 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003801 if (ret)
3802 break;
3803 }
3804
Stefan Behrens803b2f52013-08-15 17:11:21 +02003805 btrfs_release_path(path);
3806 if (key.offset < (u64)-1) {
3807 key.offset++;
3808 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3809 key.offset = 0;
3810 key.type = BTRFS_ROOT_ITEM_KEY;
3811 } else if (key.objectid < (u64)-1) {
3812 key.offset = 0;
3813 key.type = BTRFS_ROOT_ITEM_KEY;
3814 key.objectid++;
3815 } else {
3816 break;
3817 }
3818 cond_resched();
3819 }
3820
3821out:
3822 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003823 if (trans && !IS_ERR(trans))
3824 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003825 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003826 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003827 else
3828 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003829 up(&fs_info->uuid_tree_rescan_sem);
3830 return 0;
3831}
3832
Stefan Behrens70f80172013-08-15 17:11:23 +02003833/*
3834 * Callback for btrfs_uuid_tree_iterate().
3835 * returns:
3836 * 0 check succeeded, the entry is not outdated.
3837 * < 0 if an error occured.
3838 * > 0 if the check failed, which means the caller shall remove the entry.
3839 */
3840static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3841 u8 *uuid, u8 type, u64 subid)
3842{
3843 struct btrfs_key key;
3844 int ret = 0;
3845 struct btrfs_root *subvol_root;
3846
3847 if (type != BTRFS_UUID_KEY_SUBVOL &&
3848 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3849 goto out;
3850
3851 key.objectid = subid;
3852 key.type = BTRFS_ROOT_ITEM_KEY;
3853 key.offset = (u64)-1;
3854 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3855 if (IS_ERR(subvol_root)) {
3856 ret = PTR_ERR(subvol_root);
3857 if (ret == -ENOENT)
3858 ret = 1;
3859 goto out;
3860 }
3861
3862 switch (type) {
3863 case BTRFS_UUID_KEY_SUBVOL:
3864 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3865 ret = 1;
3866 break;
3867 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3868 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3869 BTRFS_UUID_SIZE))
3870 ret = 1;
3871 break;
3872 }
3873
3874out:
3875 return ret;
3876}
3877
3878static int btrfs_uuid_rescan_kthread(void *data)
3879{
3880 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3881 int ret;
3882
3883 /*
3884 * 1st step is to iterate through the existing UUID tree and
3885 * to delete all entries that contain outdated data.
3886 * 2nd step is to add all missing entries to the UUID tree.
3887 */
3888 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3889 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003890 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003891 up(&fs_info->uuid_tree_rescan_sem);
3892 return ret;
3893 }
3894 return btrfs_uuid_scan_kthread(data);
3895}
3896
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003897int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3898{
3899 struct btrfs_trans_handle *trans;
3900 struct btrfs_root *tree_root = fs_info->tree_root;
3901 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003902 struct task_struct *task;
3903 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003904
3905 /*
3906 * 1 - root node
3907 * 1 - root item
3908 */
3909 trans = btrfs_start_transaction(tree_root, 2);
3910 if (IS_ERR(trans))
3911 return PTR_ERR(trans);
3912
3913 uuid_root = btrfs_create_tree(trans, fs_info,
3914 BTRFS_UUID_TREE_OBJECTID);
3915 if (IS_ERR(uuid_root)) {
3916 btrfs_abort_transaction(trans, tree_root,
3917 PTR_ERR(uuid_root));
3918 return PTR_ERR(uuid_root);
3919 }
3920
3921 fs_info->uuid_root = uuid_root;
3922
Stefan Behrens803b2f52013-08-15 17:11:21 +02003923 ret = btrfs_commit_transaction(trans, tree_root);
3924 if (ret)
3925 return ret;
3926
3927 down(&fs_info->uuid_tree_rescan_sem);
3928 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3929 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003930 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003931 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003932 up(&fs_info->uuid_tree_rescan_sem);
3933 return PTR_ERR(task);
3934 }
3935
3936 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003937}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003938
Stefan Behrens70f80172013-08-15 17:11:23 +02003939int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3940{
3941 struct task_struct *task;
3942
3943 down(&fs_info->uuid_tree_rescan_sem);
3944 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3945 if (IS_ERR(task)) {
3946 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003947 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003948 up(&fs_info->uuid_tree_rescan_sem);
3949 return PTR_ERR(task);
3950 }
3951
3952 return 0;
3953}
3954
Chris Mason8f18cf12008-04-25 16:53:30 -04003955/*
3956 * shrinking a device means finding all of the device extents past
3957 * the new size, and then following the back refs to the chunks.
3958 * The chunk relocation code actually frees the device extent
3959 */
3960int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3961{
3962 struct btrfs_trans_handle *trans;
3963 struct btrfs_root *root = device->dev_root;
3964 struct btrfs_dev_extent *dev_extent = NULL;
3965 struct btrfs_path *path;
3966 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04003967 u64 chunk_objectid;
3968 u64 chunk_offset;
3969 int ret;
3970 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003971 int failed = 0;
3972 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003973 struct extent_buffer *l;
3974 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003975 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003976 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08003977 u64 old_size = btrfs_device_get_total_bytes(device);
3978 u64 diff = old_size - new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04003979
Stefan Behrens63a212a2012-11-05 18:29:28 +01003980 if (device->is_tgtdev_for_dev_replace)
3981 return -EINVAL;
3982
Chris Mason8f18cf12008-04-25 16:53:30 -04003983 path = btrfs_alloc_path();
3984 if (!path)
3985 return -ENOMEM;
3986
Chris Mason8f18cf12008-04-25 16:53:30 -04003987 path->reada = 2;
3988
Chris Mason7d9eb122008-07-08 14:19:17 -04003989 lock_chunks(root);
3990
Miao Xie7cc8e582014-09-03 21:35:38 +08003991 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04003992 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003993 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003994 spin_lock(&root->fs_info->free_chunk_lock);
3995 root->fs_info->free_chunk_space -= diff;
3996 spin_unlock(&root->fs_info->free_chunk_lock);
3997 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003998 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003999
Josef Bacikba1bf482009-09-11 16:11:19 -04004000again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004001 key.objectid = device->devid;
4002 key.offset = (u64)-1;
4003 key.type = BTRFS_DEV_EXTENT_KEY;
4004
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004005 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04004006 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4007 if (ret < 0)
4008 goto done;
4009
4010 ret = btrfs_previous_item(root, path, 0, key.type);
4011 if (ret < 0)
4012 goto done;
4013 if (ret) {
4014 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004015 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004016 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004017 }
4018
4019 l = path->nodes[0];
4020 slot = path->slots[0];
4021 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4022
Josef Bacikba1bf482009-09-11 16:11:19 -04004023 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004024 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004025 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004026 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004027
4028 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4029 length = btrfs_dev_extent_length(l, dev_extent);
4030
Josef Bacikba1bf482009-09-11 16:11:19 -04004031 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004032 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004033 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004034 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004035
Chris Mason8f18cf12008-04-25 16:53:30 -04004036 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
4037 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004038 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004039
Zhao Leia688a042015-02-09 20:31:44 +08004040 ret = btrfs_relocate_chunk(root, chunk_objectid, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04004041 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004042 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004043 if (ret == -ENOSPC)
4044 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004045 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004046
4047 if (failed && !retried) {
4048 failed = 0;
4049 retried = true;
4050 goto again;
4051 } else if (failed && retried) {
4052 ret = -ENOSPC;
4053 lock_chunks(root);
4054
Miao Xie7cc8e582014-09-03 21:35:38 +08004055 btrfs_device_set_total_bytes(device, old_size);
Josef Bacikba1bf482009-09-11 16:11:19 -04004056 if (device->writeable)
4057 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004058 spin_lock(&root->fs_info->free_chunk_lock);
4059 root->fs_info->free_chunk_space += diff;
4060 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04004061 unlock_chunks(root);
4062 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004063 }
4064
Chris Balld6397ba2009-04-27 07:29:03 -04004065 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004066 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004067 if (IS_ERR(trans)) {
4068 ret = PTR_ERR(trans);
4069 goto done;
4070 }
4071
Chris Balld6397ba2009-04-27 07:29:03 -04004072 lock_chunks(root);
Miao Xie7cc8e582014-09-03 21:35:38 +08004073 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004074 if (list_empty(&device->resized_list))
4075 list_add_tail(&device->resized_list,
4076 &root->fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004077
Chris Balld6397ba2009-04-27 07:29:03 -04004078 WARN_ON(diff > old_total);
4079 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4080 unlock_chunks(root);
Miao Xie2196d6e2014-09-03 21:35:41 +08004081
4082 /* Now btrfs_update_device() will change the on-disk size. */
4083 ret = btrfs_update_device(trans, device);
Chris Balld6397ba2009-04-27 07:29:03 -04004084 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004085done:
4086 btrfs_free_path(path);
4087 return ret;
4088}
4089
Li Zefan125ccb02011-12-08 15:07:24 +08004090static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004091 struct btrfs_key *key,
4092 struct btrfs_chunk *chunk, int item_size)
4093{
David Sterba6c417612011-04-13 15:41:04 +02004094 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004095 struct btrfs_disk_key disk_key;
4096 u32 array_size;
4097 u8 *ptr;
4098
Miao Xiefe48a5c2014-09-03 21:35:39 +08004099 lock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004100 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004101 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004102 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4103 unlock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004104 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004105 }
Chris Mason0b86a832008-03-24 15:01:56 -04004106
4107 ptr = super_copy->sys_chunk_array + array_size;
4108 btrfs_cpu_key_to_disk(&disk_key, key);
4109 memcpy(ptr, &disk_key, sizeof(disk_key));
4110 ptr += sizeof(disk_key);
4111 memcpy(ptr, chunk, item_size);
4112 item_size += sizeof(disk_key);
4113 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004114 unlock_chunks(root);
4115
Chris Mason0b86a832008-03-24 15:01:56 -04004116 return 0;
4117}
4118
Miao Xieb2117a32011-01-05 10:07:28 +00004119/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004120 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004121 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004122static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004123{
Arne Jansen73c5de02011-04-12 12:07:57 +02004124 const struct btrfs_device_info *di_a = a;
4125 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004126
Arne Jansen73c5de02011-04-12 12:07:57 +02004127 if (di_a->max_avail > di_b->max_avail)
4128 return -1;
4129 if (di_a->max_avail < di_b->max_avail)
4130 return 1;
4131 if (di_a->total_avail > di_b->total_avail)
4132 return -1;
4133 if (di_a->total_avail < di_b->total_avail)
4134 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004135 return 0;
4136}
4137
David Sterbae8c9f182015-01-02 18:23:10 +01004138static const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004139 [BTRFS_RAID_RAID10] = {
4140 .sub_stripes = 2,
4141 .dev_stripes = 1,
4142 .devs_max = 0, /* 0 == as many as possible */
4143 .devs_min = 4,
4144 .devs_increment = 2,
4145 .ncopies = 2,
4146 },
4147 [BTRFS_RAID_RAID1] = {
4148 .sub_stripes = 1,
4149 .dev_stripes = 1,
4150 .devs_max = 2,
4151 .devs_min = 2,
4152 .devs_increment = 2,
4153 .ncopies = 2,
4154 },
4155 [BTRFS_RAID_DUP] = {
4156 .sub_stripes = 1,
4157 .dev_stripes = 2,
4158 .devs_max = 1,
4159 .devs_min = 1,
4160 .devs_increment = 1,
4161 .ncopies = 2,
4162 },
4163 [BTRFS_RAID_RAID0] = {
4164 .sub_stripes = 1,
4165 .dev_stripes = 1,
4166 .devs_max = 0,
4167 .devs_min = 2,
4168 .devs_increment = 1,
4169 .ncopies = 1,
4170 },
4171 [BTRFS_RAID_SINGLE] = {
4172 .sub_stripes = 1,
4173 .dev_stripes = 1,
4174 .devs_max = 1,
4175 .devs_min = 1,
4176 .devs_increment = 1,
4177 .ncopies = 1,
4178 },
Chris Masone942f882013-02-20 14:06:05 -05004179 [BTRFS_RAID_RAID5] = {
4180 .sub_stripes = 1,
4181 .dev_stripes = 1,
4182 .devs_max = 0,
4183 .devs_min = 2,
4184 .devs_increment = 1,
4185 .ncopies = 2,
4186 },
4187 [BTRFS_RAID_RAID6] = {
4188 .sub_stripes = 1,
4189 .dev_stripes = 1,
4190 .devs_max = 0,
4191 .devs_min = 3,
4192 .devs_increment = 1,
4193 .ncopies = 3,
4194 },
Liu Bo31e50222012-11-21 14:18:10 +00004195};
4196
David Woodhouse53b381b2013-01-29 18:40:14 -05004197static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4198{
4199 /* TODO allow them to set a preferred stripe size */
4200 return 64 * 1024;
4201}
4202
4203static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4204{
Zhao Leiffe2d202015-01-20 15:11:44 +08004205 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004206 return;
4207
Miao Xieceda0862013-04-11 10:30:16 +00004208 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004209}
4210
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004211#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4212 - sizeof(struct btrfs_item) \
4213 - sizeof(struct btrfs_chunk)) \
4214 / sizeof(struct btrfs_stripe) + 1)
4215
4216#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4217 - 2 * sizeof(struct btrfs_disk_key) \
4218 - 2 * sizeof(struct btrfs_chunk)) \
4219 / sizeof(struct btrfs_stripe) + 1)
4220
Miao Xieb2117a32011-01-05 10:07:28 +00004221static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004222 struct btrfs_root *extent_root, u64 start,
4223 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004224{
4225 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004226 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4227 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004228 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004229 struct extent_map_tree *em_tree;
4230 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004231 struct btrfs_device_info *devices_info = NULL;
4232 u64 total_avail;
4233 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004234 int data_stripes; /* number of stripes that count for
4235 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004236 int sub_stripes; /* sub_stripes info for map */
4237 int dev_stripes; /* stripes per dev */
4238 int devs_max; /* max devs to use */
4239 int devs_min; /* min devs needed */
4240 int devs_increment; /* ndevs has to be a multiple of this */
4241 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004242 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004243 u64 max_stripe_size;
4244 u64 max_chunk_size;
4245 u64 stripe_size;
4246 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004247 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004248 int ndevs;
4249 int i;
4250 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004251 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004252
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004253 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004254
Miao Xieb2117a32011-01-05 10:07:28 +00004255 if (list_empty(&fs_devices->alloc_list))
4256 return -ENOSPC;
4257
Liu Bo31e50222012-11-21 14:18:10 +00004258 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004259
Liu Bo31e50222012-11-21 14:18:10 +00004260 sub_stripes = btrfs_raid_array[index].sub_stripes;
4261 dev_stripes = btrfs_raid_array[index].dev_stripes;
4262 devs_max = btrfs_raid_array[index].devs_max;
4263 devs_min = btrfs_raid_array[index].devs_min;
4264 devs_increment = btrfs_raid_array[index].devs_increment;
4265 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004266
4267 if (type & BTRFS_BLOCK_GROUP_DATA) {
4268 max_stripe_size = 1024 * 1024 * 1024;
4269 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004270 if (!devs_max)
4271 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004272 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004273 /* for larger filesystems, use larger metadata chunks */
4274 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4275 max_stripe_size = 1024 * 1024 * 1024;
4276 else
4277 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004278 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004279 if (!devs_max)
4280 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004281 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004282 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004283 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004284 if (!devs_max)
4285 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004286 } else {
David Sterba351fd352014-05-15 16:48:20 +02004287 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004288 type);
4289 BUG_ON(1);
4290 }
4291
4292 /* we don't want a chunk larger than 10% of writeable space */
4293 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4294 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004295
David Sterba31e818f2015-02-20 18:00:26 +01004296 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004297 GFP_NOFS);
4298 if (!devices_info)
4299 return -ENOMEM;
4300
Arne Jansen73c5de02011-04-12 12:07:57 +02004301 cur = fs_devices->alloc_list.next;
4302
4303 /*
4304 * in the first pass through the devices list, we gather information
4305 * about the available holes on each device.
4306 */
4307 ndevs = 0;
4308 while (cur != &fs_devices->alloc_list) {
4309 struct btrfs_device *device;
4310 u64 max_avail;
4311 u64 dev_offset;
4312
4313 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4314
4315 cur = cur->next;
4316
4317 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004318 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004319 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004320 continue;
4321 }
4322
Stefan Behrens63a212a2012-11-05 18:29:28 +01004323 if (!device->in_fs_metadata ||
4324 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004325 continue;
4326
4327 if (device->total_bytes > device->bytes_used)
4328 total_avail = device->total_bytes - device->bytes_used;
4329 else
4330 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004331
4332 /* If there is no space on this device, skip it. */
4333 if (total_avail == 0)
4334 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004335
Josef Bacik6df9a952013-06-27 13:22:46 -04004336 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004337 max_stripe_size * dev_stripes,
4338 &dev_offset, &max_avail);
4339 if (ret && ret != -ENOSPC)
4340 goto error;
4341
4342 if (ret == 0)
4343 max_avail = max_stripe_size * dev_stripes;
4344
4345 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4346 continue;
4347
Eric Sandeen063d0062013-01-31 00:55:01 +00004348 if (ndevs == fs_devices->rw_devices) {
4349 WARN(1, "%s: found more than %llu devices\n",
4350 __func__, fs_devices->rw_devices);
4351 break;
4352 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004353 devices_info[ndevs].dev_offset = dev_offset;
4354 devices_info[ndevs].max_avail = max_avail;
4355 devices_info[ndevs].total_avail = total_avail;
4356 devices_info[ndevs].dev = device;
4357 ++ndevs;
4358 }
4359
4360 /*
4361 * now sort the devices by hole size / available space
4362 */
4363 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4364 btrfs_cmp_device_info, NULL);
4365
4366 /* round down to number of usable stripes */
4367 ndevs -= ndevs % devs_increment;
4368
4369 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4370 ret = -ENOSPC;
4371 goto error;
4372 }
4373
4374 if (devs_max && ndevs > devs_max)
4375 ndevs = devs_max;
4376 /*
4377 * the primary goal is to maximize the number of stripes, so use as many
4378 * devices as possible, even if the stripes are not maximum sized.
4379 */
4380 stripe_size = devices_info[ndevs-1].max_avail;
4381 num_stripes = ndevs * dev_stripes;
4382
David Woodhouse53b381b2013-01-29 18:40:14 -05004383 /*
4384 * this will have to be fixed for RAID1 and RAID10 over
4385 * more drives
4386 */
4387 data_stripes = num_stripes / ncopies;
4388
David Woodhouse53b381b2013-01-29 18:40:14 -05004389 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4390 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4391 btrfs_super_stripesize(info->super_copy));
4392 data_stripes = num_stripes - 1;
4393 }
4394 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4395 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4396 btrfs_super_stripesize(info->super_copy));
4397 data_stripes = num_stripes - 2;
4398 }
Chris Mason86db2572013-02-20 16:23:40 -05004399
4400 /*
4401 * Use the number of data stripes to figure out how big this chunk
4402 * is really going to be in terms of logical address space,
4403 * and compare that answer with the max chunk size
4404 */
4405 if (stripe_size * data_stripes > max_chunk_size) {
4406 u64 mask = (1ULL << 24) - 1;
David Sterbab8b93ad2015-01-16 17:26:13 +01004407
4408 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004409
4410 /* bump the answer up to a 16MB boundary */
4411 stripe_size = (stripe_size + mask) & ~mask;
4412
4413 /* but don't go higher than the limits we found
4414 * while searching for free extents
4415 */
4416 if (stripe_size > devices_info[ndevs-1].max_avail)
4417 stripe_size = devices_info[ndevs-1].max_avail;
4418 }
4419
David Sterbab8b93ad2015-01-16 17:26:13 +01004420 stripe_size = div_u64(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004421
4422 /* align to BTRFS_STRIPE_LEN */
David Sterbab8b93ad2015-01-16 17:26:13 +01004423 stripe_size = div_u64(stripe_size, raid_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05004424 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004425
Miao Xieb2117a32011-01-05 10:07:28 +00004426 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4427 if (!map) {
4428 ret = -ENOMEM;
4429 goto error;
4430 }
4431 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004432
Arne Jansen73c5de02011-04-12 12:07:57 +02004433 for (i = 0; i < ndevs; ++i) {
4434 for (j = 0; j < dev_stripes; ++j) {
4435 int s = i * dev_stripes + j;
4436 map->stripes[s].dev = devices_info[i].dev;
4437 map->stripes[s].physical = devices_info[i].dev_offset +
4438 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004439 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004440 }
Chris Mason593060d2008-03-25 16:50:33 -04004441 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004442 map->stripe_len = raid_stripe_len;
4443 map->io_align = raid_stripe_len;
4444 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004445 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004446 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004447
David Woodhouse53b381b2013-01-29 18:40:14 -05004448 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004449
Arne Jansen73c5de02011-04-12 12:07:57 +02004450 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004451
David Sterba172ddd62011-04-21 00:48:27 +02004452 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004453 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004454 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004455 ret = -ENOMEM;
4456 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004457 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004458 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004459 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004460 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004461 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004462 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004463 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004464 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004465
Chris Mason0b86a832008-03-24 15:01:56 -04004466 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004467 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004468 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004469 if (!ret) {
4470 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4471 atomic_inc(&em->refs);
4472 }
Chris Mason890871b2009-09-02 16:24:52 -04004473 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004474 if (ret) {
4475 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004476 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004477 }
Yan Zheng2b820322008-11-17 21:11:30 -05004478
Josef Bacik04487482013-01-29 15:03:37 -05004479 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4480 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4481 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004482 if (ret)
4483 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004484
Miao Xie7cc8e582014-09-03 21:35:38 +08004485 for (i = 0; i < map->num_stripes; i++) {
4486 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4487 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4488 }
Miao Xie43530c42014-09-03 21:35:36 +08004489
Miao Xie1c116182014-09-03 21:35:37 +08004490 spin_lock(&extent_root->fs_info->free_chunk_lock);
4491 extent_root->fs_info->free_chunk_space -= (stripe_size *
4492 map->num_stripes);
4493 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4494
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004495 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004496 check_raid56_incompat_flag(extent_root->fs_info, type);
4497
Miao Xieb2117a32011-01-05 10:07:28 +00004498 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004499 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004500
Josef Bacik6df9a952013-06-27 13:22:46 -04004501error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004502 write_lock(&em_tree->lock);
4503 remove_extent_mapping(em_tree, em);
4504 write_unlock(&em_tree->lock);
4505
4506 /* One for our allocation */
4507 free_extent_map(em);
4508 /* One for the tree reference */
4509 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004510 /* One for the pending_chunks list reference */
4511 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004512error:
Miao Xieb2117a32011-01-05 10:07:28 +00004513 kfree(devices_info);
4514 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004515}
4516
Josef Bacik6df9a952013-06-27 13:22:46 -04004517int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004518 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004519 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004520{
Yan Zheng2b820322008-11-17 21:11:30 -05004521 struct btrfs_key key;
4522 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4523 struct btrfs_device *device;
4524 struct btrfs_chunk *chunk;
4525 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004526 struct extent_map_tree *em_tree;
4527 struct extent_map *em;
4528 struct map_lookup *map;
4529 size_t item_size;
4530 u64 dev_offset;
4531 u64 stripe_size;
4532 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004533 int ret;
4534
Josef Bacik6df9a952013-06-27 13:22:46 -04004535 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4536 read_lock(&em_tree->lock);
4537 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4538 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004539
Josef Bacik6df9a952013-06-27 13:22:46 -04004540 if (!em) {
4541 btrfs_crit(extent_root->fs_info, "unable to find logical "
4542 "%Lu len %Lu", chunk_offset, chunk_size);
4543 return -EINVAL;
4544 }
4545
4546 if (em->start != chunk_offset || em->len != chunk_size) {
4547 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004548 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004549 chunk_size, em->start, em->len);
4550 free_extent_map(em);
4551 return -EINVAL;
4552 }
4553
4554 map = (struct map_lookup *)em->bdev;
4555 item_size = btrfs_chunk_item_size(map->num_stripes);
4556 stripe_size = em->orig_block_len;
4557
4558 chunk = kzalloc(item_size, GFP_NOFS);
4559 if (!chunk) {
4560 ret = -ENOMEM;
4561 goto out;
4562 }
4563
4564 for (i = 0; i < map->num_stripes; i++) {
4565 device = map->stripes[i].dev;
4566 dev_offset = map->stripes[i].physical;
4567
Yan Zheng2b820322008-11-17 21:11:30 -05004568 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004569 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004570 goto out;
4571 ret = btrfs_alloc_dev_extent(trans, device,
4572 chunk_root->root_key.objectid,
4573 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4574 chunk_offset, dev_offset,
4575 stripe_size);
4576 if (ret)
4577 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004578 }
4579
Yan Zheng2b820322008-11-17 21:11:30 -05004580 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004581 for (i = 0; i < map->num_stripes; i++) {
4582 device = map->stripes[i].dev;
4583 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004584
4585 btrfs_set_stack_stripe_devid(stripe, device->devid);
4586 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4587 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4588 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004589 }
4590
4591 btrfs_set_stack_chunk_length(chunk, chunk_size);
4592 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4593 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4594 btrfs_set_stack_chunk_type(chunk, map->type);
4595 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4596 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4597 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4598 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4599 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4600
4601 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4602 key.type = BTRFS_CHUNK_ITEM_KEY;
4603 key.offset = chunk_offset;
4604
4605 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004606 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4607 /*
4608 * TODO: Cleanup of inserted chunk root in case of
4609 * failure.
4610 */
Li Zefan125ccb02011-12-08 15:07:24 +08004611 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004612 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004613 }
liubo1abe9b82011-03-24 11:18:59 +00004614
Josef Bacik6df9a952013-06-27 13:22:46 -04004615out:
Yan Zheng2b820322008-11-17 21:11:30 -05004616 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004617 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004618 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004619}
4620
4621/*
4622 * Chunk allocation falls into two parts. The first part does works
4623 * that make the new allocated chunk useable, but not do any operation
4624 * that modifies the chunk tree. The second part does the works that
4625 * require modifying the chunk tree. This division is important for the
4626 * bootstrap process of adding storage to a seed btrfs.
4627 */
4628int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4629 struct btrfs_root *extent_root, u64 type)
4630{
4631 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004632
Filipe Mananaa9629592015-05-18 19:11:40 +01004633 ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex));
Josef Bacik6df9a952013-06-27 13:22:46 -04004634 chunk_offset = find_next_chunk(extent_root->fs_info);
4635 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004636}
4637
Chris Masond3977122009-01-05 21:25:51 -05004638static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004639 struct btrfs_root *root,
4640 struct btrfs_device *device)
4641{
4642 u64 chunk_offset;
4643 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004644 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004645 struct btrfs_fs_info *fs_info = root->fs_info;
4646 struct btrfs_root *extent_root = fs_info->extent_root;
4647 int ret;
4648
Josef Bacik6df9a952013-06-27 13:22:46 -04004649 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004650 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004651 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4652 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004653 if (ret)
4654 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004655
Josef Bacik6df9a952013-06-27 13:22:46 -04004656 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004657 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004658 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4659 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004660 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004661}
4662
Miao Xied20983b2014-07-03 18:22:13 +08004663static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4664{
4665 int max_errors;
4666
4667 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4668 BTRFS_BLOCK_GROUP_RAID10 |
4669 BTRFS_BLOCK_GROUP_RAID5 |
4670 BTRFS_BLOCK_GROUP_DUP)) {
4671 max_errors = 1;
4672 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4673 max_errors = 2;
4674 } else {
4675 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004676 }
4677
Miao Xied20983b2014-07-03 18:22:13 +08004678 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05004679}
4680
4681int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4682{
4683 struct extent_map *em;
4684 struct map_lookup *map;
4685 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4686 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004687 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004688 int i;
4689
Chris Mason890871b2009-09-02 16:24:52 -04004690 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004691 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004692 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004693 if (!em)
4694 return 1;
4695
4696 map = (struct map_lookup *)em->bdev;
4697 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004698 if (map->stripes[i].dev->missing) {
4699 miss_ndevs++;
4700 continue;
4701 }
4702
Yan Zheng2b820322008-11-17 21:11:30 -05004703 if (!map->stripes[i].dev->writeable) {
4704 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004705 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004706 }
4707 }
Miao Xied20983b2014-07-03 18:22:13 +08004708
4709 /*
4710 * If the number of missing devices is larger than max errors,
4711 * we can not write the data into that chunk successfully, so
4712 * set it readonly.
4713 */
4714 if (miss_ndevs > btrfs_chunk_max_errors(map))
4715 readonly = 1;
4716end:
Yan Zheng2b820322008-11-17 21:11:30 -05004717 free_extent_map(em);
4718 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004719}
4720
4721void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4722{
David Sterbaa8067e02011-04-21 00:34:43 +02004723 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004724}
4725
4726void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4727{
4728 struct extent_map *em;
4729
Chris Masond3977122009-01-05 21:25:51 -05004730 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004731 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004732 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4733 if (em)
4734 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004735 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004736 if (!em)
4737 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004738 /* once for us */
4739 free_extent_map(em);
4740 /* once for the tree */
4741 free_extent_map(em);
4742 }
4743}
4744
Stefan Behrens5d964052012-11-05 14:59:07 +01004745int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004746{
Stefan Behrens5d964052012-11-05 14:59:07 +01004747 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004748 struct extent_map *em;
4749 struct map_lookup *map;
4750 struct extent_map_tree *em_tree = &map_tree->map_tree;
4751 int ret;
4752
Chris Mason890871b2009-09-02 16:24:52 -04004753 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004754 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004755 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004756
Josef Bacikfb7669b2013-04-23 10:53:18 -04004757 /*
4758 * We could return errors for these cases, but that could get ugly and
4759 * we'd probably do the same thing which is just not do anything else
4760 * and exit, so return 1 so the callers don't try to use other copies.
4761 */
4762 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004763 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004764 logical+len);
4765 return 1;
4766 }
4767
4768 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004769 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004770 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004771 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004772 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004773 return 1;
4774 }
4775
Chris Masonf1885912008-04-09 16:28:12 -04004776 map = (struct map_lookup *)em->bdev;
4777 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4778 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004779 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4780 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004781 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4782 ret = 2;
4783 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4784 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004785 else
4786 ret = 1;
4787 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004788
4789 btrfs_dev_replace_lock(&fs_info->dev_replace);
4790 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4791 ret++;
4792 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4793
Chris Masonf1885912008-04-09 16:28:12 -04004794 return ret;
4795}
4796
David Woodhouse53b381b2013-01-29 18:40:14 -05004797unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4798 struct btrfs_mapping_tree *map_tree,
4799 u64 logical)
4800{
4801 struct extent_map *em;
4802 struct map_lookup *map;
4803 struct extent_map_tree *em_tree = &map_tree->map_tree;
4804 unsigned long len = root->sectorsize;
4805
4806 read_lock(&em_tree->lock);
4807 em = lookup_extent_mapping(em_tree, logical, len);
4808 read_unlock(&em_tree->lock);
4809 BUG_ON(!em);
4810
4811 BUG_ON(em->start > logical || em->start + em->len < logical);
4812 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08004813 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05004814 len = map->stripe_len * nr_data_stripes(map);
David Woodhouse53b381b2013-01-29 18:40:14 -05004815 free_extent_map(em);
4816 return len;
4817}
4818
4819int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4820 u64 logical, u64 len, int mirror_num)
4821{
4822 struct extent_map *em;
4823 struct map_lookup *map;
4824 struct extent_map_tree *em_tree = &map_tree->map_tree;
4825 int ret = 0;
4826
4827 read_lock(&em_tree->lock);
4828 em = lookup_extent_mapping(em_tree, logical, len);
4829 read_unlock(&em_tree->lock);
4830 BUG_ON(!em);
4831
4832 BUG_ON(em->start > logical || em->start + em->len < logical);
4833 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08004834 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05004835 ret = 1;
4836 free_extent_map(em);
4837 return ret;
4838}
4839
Stefan Behrens30d98612012-11-06 14:52:18 +01004840static int find_live_mirror(struct btrfs_fs_info *fs_info,
4841 struct map_lookup *map, int first, int num,
4842 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004843{
4844 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004845 int tolerance;
4846 struct btrfs_device *srcdev;
4847
4848 if (dev_replace_is_ongoing &&
4849 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4850 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4851 srcdev = fs_info->dev_replace.srcdev;
4852 else
4853 srcdev = NULL;
4854
4855 /*
4856 * try to avoid the drive that is the source drive for a
4857 * dev-replace procedure, only choose it if no other non-missing
4858 * mirror is available
4859 */
4860 for (tolerance = 0; tolerance < 2; tolerance++) {
4861 if (map->stripes[optimal].dev->bdev &&
4862 (tolerance || map->stripes[optimal].dev != srcdev))
4863 return optimal;
4864 for (i = first; i < first + num; i++) {
4865 if (map->stripes[i].dev->bdev &&
4866 (tolerance || map->stripes[i].dev != srcdev))
4867 return i;
4868 }
Chris Masondfe25022008-05-13 13:46:40 -04004869 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004870
Chris Masondfe25022008-05-13 13:46:40 -04004871 /* we couldn't find one that doesn't fail. Just return something
4872 * and the io error handling code will clean up eventually
4873 */
4874 return optimal;
4875}
4876
David Woodhouse53b381b2013-01-29 18:40:14 -05004877static inline int parity_smaller(u64 a, u64 b)
4878{
4879 return a > b;
4880}
4881
4882/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004883static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05004884{
4885 struct btrfs_bio_stripe s;
4886 int i;
4887 u64 l;
4888 int again = 1;
4889
4890 while (again) {
4891 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08004892 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004893 if (parity_smaller(bbio->raid_map[i],
4894 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05004895 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004896 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05004897 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004898 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05004899 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004900 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08004901
David Woodhouse53b381b2013-01-29 18:40:14 -05004902 again = 1;
4903 }
4904 }
4905 }
4906}
4907
Zhao Lei6e9606d2015-01-20 15:11:34 +08004908static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
4909{
4910 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08004911 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08004912 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08004913 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08004914 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08004915 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08004916 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08004917 /*
4918 * plus the raid_map, which includes both the tgt dev
4919 * and the stripes
4920 */
4921 sizeof(u64) * (total_stripes),
Zhao Lei6e9606d2015-01-20 15:11:34 +08004922 GFP_NOFS);
4923 if (!bbio)
4924 return NULL;
4925
4926 atomic_set(&bbio->error, 0);
4927 atomic_set(&bbio->refs, 1);
4928
4929 return bbio;
4930}
4931
4932void btrfs_get_bbio(struct btrfs_bio *bbio)
4933{
4934 WARN_ON(!atomic_read(&bbio->refs));
4935 atomic_inc(&bbio->refs);
4936}
4937
4938void btrfs_put_bbio(struct btrfs_bio *bbio)
4939{
4940 if (!bbio)
4941 return;
4942 if (atomic_dec_and_test(&bbio->refs))
4943 kfree(bbio);
4944}
4945
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004946static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004947 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004948 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004949 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04004950{
4951 struct extent_map *em;
4952 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004953 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004954 struct extent_map_tree *em_tree = &map_tree->map_tree;
4955 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004956 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004957 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004958 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004959 u64 stripe_nr_orig;
4960 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004961 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01004962 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004963 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004964 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004965 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004966 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08004967 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004968 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004969 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4970 int dev_replace_is_ongoing = 0;
4971 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004972 int patch_the_first_stripe_for_dev_replace = 0;
4973 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004974 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004975
Chris Mason890871b2009-09-02 16:24:52 -04004976 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004977 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004978 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004979
Chris Mason3b951512008-04-17 11:29:12 -04004980 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004981 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004982 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004983 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004984 }
Chris Mason0b86a832008-03-24 15:01:56 -04004985
Josef Bacik9bb91872013-04-19 23:45:33 -04004986 if (em->start > logical || em->start + em->len < logical) {
4987 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004988 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004989 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004990 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004991 return -EINVAL;
4992 }
4993
Chris Mason0b86a832008-03-24 15:01:56 -04004994 map = (struct map_lookup *)em->bdev;
4995 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004996
David Woodhouse53b381b2013-01-29 18:40:14 -05004997 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004998 stripe_nr = offset;
4999 /*
5000 * stripe_nr counts the total number of stripes we have to stride
5001 * to get to this block
5002 */
David Sterba47c57132015-02-20 18:43:47 +01005003 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005004
David Woodhouse53b381b2013-01-29 18:40:14 -05005005 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005006 BUG_ON(offset < stripe_offset);
5007
5008 /* stripe_offset is the offset of this block in its stripe*/
5009 stripe_offset = offset - stripe_offset;
5010
David Woodhouse53b381b2013-01-29 18:40:14 -05005011 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005012 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005013 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5014 raid56_full_stripe_start = offset;
5015
5016 /* allow a write of a full stripe, but make sure we don't
5017 * allow straddling of stripes
5018 */
David Sterba47c57132015-02-20 18:43:47 +01005019 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5020 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005021 raid56_full_stripe_start *= full_stripe_len;
5022 }
5023
5024 if (rw & REQ_DISCARD) {
5025 /* we don't discard raid56 yet */
Zhao Leiffe2d202015-01-20 15:11:44 +08005026 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005027 ret = -EOPNOTSUPP;
5028 goto out;
5029 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005030 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005031 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5032 u64 max_len;
5033 /* For writes to RAID[56], allow a full stripeset across all disks.
5034 For other RAID types and for RAID[56] reads, just allow a single
5035 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005036 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
David Woodhouse53b381b2013-01-29 18:40:14 -05005037 (rw & REQ_WRITE)) {
5038 max_len = stripe_len * nr_data_stripes(map) -
5039 (offset - raid56_full_stripe_start);
5040 } else {
5041 /* we limit the length of each bio to what fits in a stripe */
5042 max_len = stripe_len - stripe_offset;
5043 }
5044 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005045 } else {
5046 *length = em->len - offset;
5047 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005048
David Woodhouse53b381b2013-01-29 18:40:14 -05005049 /* This is for when we're called from btrfs_merge_bio_hook() and all
5050 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005051 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005052 goto out;
5053
Stefan Behrens472262f2012-11-06 14:43:46 +01005054 btrfs_dev_replace_lock(dev_replace);
5055 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5056 if (!dev_replace_is_ongoing)
5057 btrfs_dev_replace_unlock(dev_replace);
5058
Stefan Behrensad6d6202012-11-06 15:06:47 +01005059 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5060 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5061 dev_replace->tgtdev != NULL) {
5062 /*
5063 * in dev-replace case, for repair case (that's the only
5064 * case where the mirror is selected explicitly when
5065 * calling btrfs_map_block), blocks left of the left cursor
5066 * can also be read from the target drive.
5067 * For REQ_GET_READ_MIRRORS, the target drive is added as
5068 * the last one to the array of stripes. For READ, it also
5069 * needs to be supported using the same mirror number.
5070 * If the requested block is not left of the left cursor,
5071 * EIO is returned. This can happen because btrfs_num_copies()
5072 * returns one more in the dev-replace case.
5073 */
5074 u64 tmp_length = *length;
5075 struct btrfs_bio *tmp_bbio = NULL;
5076 int tmp_num_stripes;
5077 u64 srcdev_devid = dev_replace->srcdev->devid;
5078 int index_srcdev = 0;
5079 int found = 0;
5080 u64 physical_of_found = 0;
5081
5082 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005083 logical, &tmp_length, &tmp_bbio, 0, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005084 if (ret) {
5085 WARN_ON(tmp_bbio != NULL);
5086 goto out;
5087 }
5088
5089 tmp_num_stripes = tmp_bbio->num_stripes;
5090 if (mirror_num > tmp_num_stripes) {
5091 /*
5092 * REQ_GET_READ_MIRRORS does not contain this
5093 * mirror, that means that the requested area
5094 * is not left of the left cursor
5095 */
5096 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005097 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005098 goto out;
5099 }
5100
5101 /*
5102 * process the rest of the function using the mirror_num
5103 * of the source drive. Therefore look it up first.
5104 * At the end, patch the device pointer to the one of the
5105 * target drive.
5106 */
5107 for (i = 0; i < tmp_num_stripes; i++) {
5108 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5109 /*
5110 * In case of DUP, in order to keep it
5111 * simple, only add the mirror with the
5112 * lowest physical address
5113 */
5114 if (found &&
5115 physical_of_found <=
5116 tmp_bbio->stripes[i].physical)
5117 continue;
5118 index_srcdev = i;
5119 found = 1;
5120 physical_of_found =
5121 tmp_bbio->stripes[i].physical;
5122 }
5123 }
5124
5125 if (found) {
5126 mirror_num = index_srcdev + 1;
5127 patch_the_first_stripe_for_dev_replace = 1;
5128 physical_to_patch_in_first_stripe = physical_of_found;
5129 } else {
5130 WARN_ON(1);
5131 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005132 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005133 goto out;
5134 }
5135
Zhao Lei6e9606d2015-01-20 15:11:34 +08005136 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005137 } else if (mirror_num > map->num_stripes) {
5138 mirror_num = 0;
5139 }
5140
Chris Masonf2d8d742008-04-21 10:03:05 -04005141 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005142 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005143 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005144 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01005145 stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005146 stripe_end_offset = stripe_nr_end * map->stripe_len -
5147 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005148
Li Dongyangfce3bb92011-03-24 10:24:26 +00005149 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5150 if (rw & REQ_DISCARD)
5151 num_stripes = min_t(u64, map->num_stripes,
5152 stripe_nr_end - stripe_nr_orig);
David Sterba47c57132015-02-20 18:43:47 +01005153 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5154 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005155 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5156 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005157 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005158 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005159 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005160 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005161 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005162 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005163 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005164 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005165 current->pid % map->num_stripes,
5166 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005167 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005168 }
Chris Mason2fff7342008-04-29 14:12:09 -04005169
Chris Mason611f0e02008-04-03 16:29:03 -04005170 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005171 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005172 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005173 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005174 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005175 } else {
5176 mirror_num = 1;
5177 }
Chris Mason2fff7342008-04-29 14:12:09 -04005178
Chris Mason321aecc2008-04-16 10:49:51 -04005179 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005180 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005181
David Sterba47c57132015-02-20 18:43:47 +01005182 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005183 stripe_index *= map->sub_stripes;
5184
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005185 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005186 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005187 else if (rw & REQ_DISCARD)
5188 num_stripes = min_t(u64, map->sub_stripes *
5189 (stripe_nr_end - stripe_nr_orig),
5190 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005191 else if (mirror_num)
5192 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005193 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005194 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005195 stripe_index = find_live_mirror(fs_info, map,
5196 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005197 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005198 current->pid % map->sub_stripes,
5199 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005200 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005201 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005202
Zhao Leiffe2d202015-01-20 15:11:44 +08005203 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005204 if (need_raid_map &&
Miao Xieaf8e2d12014-10-23 14:42:50 +08005205 ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5206 mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005207 /* push stripe_nr back to the start of the full stripe */
David Sterbab8b93ad2015-01-16 17:26:13 +01005208 stripe_nr = div_u64(raid56_full_stripe_start,
5209 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005210
5211 /* RAID[56] write or recovery. Return all stripes */
5212 num_stripes = map->num_stripes;
5213 max_errors = nr_parity_stripes(map);
5214
David Woodhouse53b381b2013-01-29 18:40:14 -05005215 *length = map->stripe_len;
5216 stripe_index = 0;
5217 stripe_offset = 0;
5218 } else {
5219 /*
5220 * Mirror #0 or #1 means the original data block.
5221 * Mirror #2 is RAID5 parity block.
5222 * Mirror #3 is RAID6 Q block.
5223 */
David Sterba47c57132015-02-20 18:43:47 +01005224 stripe_nr = div_u64_rem(stripe_nr,
5225 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005226 if (mirror_num > 1)
5227 stripe_index = nr_data_stripes(map) +
5228 mirror_num - 2;
5229
5230 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005231 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5232 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005233 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5234 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5235 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005236 }
Chris Mason8790d502008-04-03 16:29:03 -04005237 } else {
5238 /*
David Sterba47c57132015-02-20 18:43:47 +01005239 * after this, stripe_nr is the number of stripes on this
5240 * device we have to walk to find the data, and stripe_index is
5241 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005242 */
David Sterba47c57132015-02-20 18:43:47 +01005243 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5244 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005245 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005246 }
Chris Mason593060d2008-03-25 16:50:33 -04005247 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005248
Stefan Behrens472262f2012-11-06 14:43:46 +01005249 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005250 if (dev_replace_is_ongoing) {
5251 if (rw & (REQ_WRITE | REQ_DISCARD))
5252 num_alloc_stripes <<= 1;
5253 if (rw & REQ_GET_READ_MIRRORS)
5254 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005255 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005256 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005257
Zhao Lei6e9606d2015-01-20 15:11:34 +08005258 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005259 if (!bbio) {
5260 ret = -ENOMEM;
5261 goto out;
5262 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005263 if (dev_replace_is_ongoing)
5264 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005265
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005266 /* build raid_map */
Zhao Leiffe2d202015-01-20 15:11:44 +08005267 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005268 need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5269 mirror_num > 1)) {
5270 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005271 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005272
5273 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5274 sizeof(struct btrfs_bio_stripe) *
5275 num_alloc_stripes +
5276 sizeof(int) * tgtdev_indexes);
5277
5278 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005279 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005280
5281 /* Fill in the logical address of each stripe */
5282 tmp = stripe_nr * nr_data_stripes(map);
5283 for (i = 0; i < nr_data_stripes(map); i++)
5284 bbio->raid_map[(i+rot) % num_stripes] =
5285 em->start + (tmp + i) * map->stripe_len;
5286
5287 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5288 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5289 bbio->raid_map[(i+rot+1) % num_stripes] =
5290 RAID6_Q_STRIPE;
5291 }
5292
Li Dongyangfce3bb92011-03-24 10:24:26 +00005293 if (rw & REQ_DISCARD) {
David Sterba9d644a62015-02-20 18:42:11 +01005294 u32 factor = 0;
5295 u32 sub_stripes = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005296 u64 stripes_per_dev = 0;
5297 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005298 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005299
5300 if (map->type &
5301 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5302 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5303 sub_stripes = 1;
5304 else
5305 sub_stripes = map->sub_stripes;
5306
5307 factor = map->num_stripes / sub_stripes;
5308 stripes_per_dev = div_u64_rem(stripe_nr_end -
5309 stripe_nr_orig,
5310 factor,
5311 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005312 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5313 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005314 }
5315
Li Dongyangfce3bb92011-03-24 10:24:26 +00005316 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005317 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005318 map->stripes[stripe_index].physical +
5319 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005320 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005321
Li Zefanec9ef7a2011-12-01 14:06:42 +08005322 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5323 BTRFS_BLOCK_GROUP_RAID10)) {
5324 bbio->stripes[i].length = stripes_per_dev *
5325 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005326
Li Zefanec9ef7a2011-12-01 14:06:42 +08005327 if (i / sub_stripes < remaining_stripes)
5328 bbio->stripes[i].length +=
5329 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005330
5331 /*
5332 * Special for the first stripe and
5333 * the last stripe:
5334 *
5335 * |-------|...|-------|
5336 * |----------|
5337 * off end_off
5338 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005339 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005340 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005341 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005342
5343 if (stripe_index >= last_stripe &&
5344 stripe_index <= (last_stripe +
5345 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005346 bbio->stripes[i].length -=
5347 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005348
Li Zefanec9ef7a2011-12-01 14:06:42 +08005349 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005350 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005351 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005352 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005353
5354 stripe_index++;
5355 if (stripe_index == map->num_stripes) {
5356 /* This could only happen for RAID0/10 */
5357 stripe_index = 0;
5358 stripe_nr++;
5359 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005360 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005361 } else {
5362 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005363 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005364 map->stripes[stripe_index].physical +
5365 stripe_offset +
5366 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005367 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005368 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005369 stripe_index++;
5370 }
Chris Mason593060d2008-03-25 16:50:33 -04005371 }
Li Zefande11cc12011-12-01 12:55:47 +08005372
Miao Xied20983b2014-07-03 18:22:13 +08005373 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5374 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005375
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005376 if (bbio->raid_map)
5377 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005378
Miao Xie2c8cdd62014-11-14 16:06:25 +08005379 tgtdev_indexes = 0;
Stefan Behrens472262f2012-11-06 14:43:46 +01005380 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5381 dev_replace->tgtdev != NULL) {
5382 int index_where_to_add;
5383 u64 srcdev_devid = dev_replace->srcdev->devid;
5384
5385 /*
5386 * duplicate the write operations while the dev replace
5387 * procedure is running. Since the copying of the old disk
5388 * to the new disk takes place at run time while the
5389 * filesystem is mounted writable, the regular write
5390 * operations to the old disk have to be duplicated to go
5391 * to the new disk as well.
5392 * Note that device->missing is handled by the caller, and
5393 * that the write to the old disk is already set up in the
5394 * stripes array.
5395 */
5396 index_where_to_add = num_stripes;
5397 for (i = 0; i < num_stripes; i++) {
5398 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5399 /* write to new disk, too */
5400 struct btrfs_bio_stripe *new =
5401 bbio->stripes + index_where_to_add;
5402 struct btrfs_bio_stripe *old =
5403 bbio->stripes + i;
5404
5405 new->physical = old->physical;
5406 new->length = old->length;
5407 new->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005408 bbio->tgtdev_map[i] = index_where_to_add;
Stefan Behrens472262f2012-11-06 14:43:46 +01005409 index_where_to_add++;
5410 max_errors++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005411 tgtdev_indexes++;
Stefan Behrens472262f2012-11-06 14:43:46 +01005412 }
5413 }
5414 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005415 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5416 dev_replace->tgtdev != NULL) {
5417 u64 srcdev_devid = dev_replace->srcdev->devid;
5418 int index_srcdev = 0;
5419 int found = 0;
5420 u64 physical_of_found = 0;
5421
5422 /*
5423 * During the dev-replace procedure, the target drive can
5424 * also be used to read data in case it is needed to repair
5425 * a corrupt block elsewhere. This is possible if the
5426 * requested area is left of the left cursor. In this area,
5427 * the target drive is a full copy of the source drive.
5428 */
5429 for (i = 0; i < num_stripes; i++) {
5430 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5431 /*
5432 * In case of DUP, in order to keep it
5433 * simple, only add the mirror with the
5434 * lowest physical address
5435 */
5436 if (found &&
5437 physical_of_found <=
5438 bbio->stripes[i].physical)
5439 continue;
5440 index_srcdev = i;
5441 found = 1;
5442 physical_of_found = bbio->stripes[i].physical;
5443 }
5444 }
5445 if (found) {
David Sterba258ece02015-02-24 19:45:15 +01005446 if (physical_of_found + map->stripe_len <=
Stefan Behrensad6d6202012-11-06 15:06:47 +01005447 dev_replace->cursor_left) {
5448 struct btrfs_bio_stripe *tgtdev_stripe =
5449 bbio->stripes + num_stripes;
5450
5451 tgtdev_stripe->physical = physical_of_found;
5452 tgtdev_stripe->length =
5453 bbio->stripes[index_srcdev].length;
5454 tgtdev_stripe->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005455 bbio->tgtdev_map[index_srcdev] = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005456
Miao Xie2c8cdd62014-11-14 16:06:25 +08005457 tgtdev_indexes++;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005458 num_stripes++;
5459 }
5460 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005461 }
5462
Li Zefande11cc12011-12-01 12:55:47 +08005463 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005464 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005465 bbio->num_stripes = num_stripes;
5466 bbio->max_errors = max_errors;
5467 bbio->mirror_num = mirror_num;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005468 bbio->num_tgtdevs = tgtdev_indexes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005469
5470 /*
5471 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5472 * mirror_num == num_stripes + 1 && dev_replace target drive is
5473 * available as a mirror
5474 */
5475 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5476 WARN_ON(num_stripes > 1);
5477 bbio->stripes[0].dev = dev_replace->tgtdev;
5478 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5479 bbio->mirror_num = map->num_stripes + 1;
5480 }
Chris Masoncea9e442008-04-09 16:28:12 -04005481out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005482 if (dev_replace_is_ongoing)
5483 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005484 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005485 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005486}
5487
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005488int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005489 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005490 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005491{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005492 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005493 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005494}
5495
Miao Xieaf8e2d12014-10-23 14:42:50 +08005496/* For Scrub/replace */
5497int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw,
5498 u64 logical, u64 *length,
5499 struct btrfs_bio **bbio_ret, int mirror_num,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005500 int need_raid_map)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005501{
5502 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005503 mirror_num, need_raid_map);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005504}
5505
Yan Zhenga512bbf2008-12-08 16:46:26 -05005506int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5507 u64 chunk_start, u64 physical, u64 devid,
5508 u64 **logical, int *naddrs, int *stripe_len)
5509{
5510 struct extent_map_tree *em_tree = &map_tree->map_tree;
5511 struct extent_map *em;
5512 struct map_lookup *map;
5513 u64 *buf;
5514 u64 bytenr;
5515 u64 length;
5516 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005517 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005518 int i, j, nr = 0;
5519
Chris Mason890871b2009-09-02 16:24:52 -04005520 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005521 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005522 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005523
Josef Bacik835d9742013-03-19 12:13:25 -04005524 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005525 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005526 chunk_start);
5527 return -EIO;
5528 }
5529
5530 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005531 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005532 em->start, chunk_start);
5533 free_extent_map(em);
5534 return -EIO;
5535 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005536 map = (struct map_lookup *)em->bdev;
5537
5538 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005539 rmap_len = map->stripe_len;
5540
Yan Zhenga512bbf2008-12-08 16:46:26 -05005541 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005542 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005543 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005544 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005545 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005546 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005547 rmap_len = map->stripe_len * nr_data_stripes(map);
5548 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005549
David Sterba31e818f2015-02-20 18:00:26 +01005550 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005551 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005552
5553 for (i = 0; i < map->num_stripes; i++) {
5554 if (devid && map->stripes[i].dev->devid != devid)
5555 continue;
5556 if (map->stripes[i].physical > physical ||
5557 map->stripes[i].physical + length <= physical)
5558 continue;
5559
5560 stripe_nr = physical - map->stripes[i].physical;
David Sterbab8b93ad2015-01-16 17:26:13 +01005561 stripe_nr = div_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005562
5563 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5564 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01005565 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005566 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5567 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005568 } /* else if RAID[56], multiply by nr_data_stripes().
5569 * Alternatively, just use rmap_len below instead of
5570 * map->stripe_len */
5571
5572 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005573 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005574 for (j = 0; j < nr; j++) {
5575 if (buf[j] == bytenr)
5576 break;
5577 }
Chris Mason934d3752008-12-08 16:43:10 -05005578 if (j == nr) {
5579 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005580 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005581 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005582 }
5583
Yan Zhenga512bbf2008-12-08 16:46:26 -05005584 *logical = buf;
5585 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005586 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005587
5588 free_extent_map(em);
5589 return 0;
5590}
5591
Miao Xie8408c712014-06-19 10:42:55 +08005592static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5593{
5594 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5595 bio_endio_nodec(bio, err);
5596 else
5597 bio_endio(bio, err);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005598 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005599}
5600
Jan Schmidta1d3c472011-08-04 17:15:33 +02005601static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005602{
Chris Mason9be33952013-05-17 18:30:14 -04005603 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005604 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005605 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005606
Stefan Behrens442a4f62012-05-25 16:06:08 +02005607 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005608 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005609 if (err == -EIO || err == -EREMOTEIO) {
5610 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005611 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005612
5613 BUG_ON(stripe_index >= bbio->num_stripes);
5614 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005615 if (dev->bdev) {
5616 if (bio->bi_rw & WRITE)
5617 btrfs_dev_stat_inc(dev,
5618 BTRFS_DEV_STAT_WRITE_ERRS);
5619 else
5620 btrfs_dev_stat_inc(dev,
5621 BTRFS_DEV_STAT_READ_ERRS);
5622 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5623 btrfs_dev_stat_inc(dev,
5624 BTRFS_DEV_STAT_FLUSH_ERRS);
5625 btrfs_dev_stat_print_on_error(dev);
5626 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005627 }
5628 }
Chris Mason8790d502008-04-03 16:29:03 -04005629
Jan Schmidta1d3c472011-08-04 17:15:33 +02005630 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005631 is_orig_bio = 1;
5632
Miao Xiec404e0d2014-01-30 16:46:55 +08005633 btrfs_bio_counter_dec(bbio->fs_info);
5634
Jan Schmidta1d3c472011-08-04 17:15:33 +02005635 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005636 if (!is_orig_bio) {
5637 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005638 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005639 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005640
Jan Schmidta1d3c472011-08-04 17:15:33 +02005641 bio->bi_private = bbio->private;
5642 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005643 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005644 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005645 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005646 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005647 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005648 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005649 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005650 /*
5651 * this bio is actually up to date, we didn't
5652 * go over the max number of errors
5653 */
5654 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005655 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005656 }
Miao Xiec55f1392014-06-19 10:42:54 +08005657
Miao Xie8408c712014-06-19 10:42:55 +08005658 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005659 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005660 bio_put(bio);
5661 }
Chris Mason8790d502008-04-03 16:29:03 -04005662}
5663
Chris Mason8b712842008-06-11 16:50:36 -04005664/*
5665 * see run_scheduled_bios for a description of why bios are collected for
5666 * async submit.
5667 *
5668 * This will add one bio to the pending list for a device and make sure
5669 * the work struct is scheduled.
5670 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005671static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5672 struct btrfs_device *device,
5673 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005674{
5675 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005676 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005677
David Woodhouse53b381b2013-01-29 18:40:14 -05005678 if (device->missing || !device->bdev) {
5679 bio_endio(bio, -EIO);
5680 return;
5681 }
5682
Chris Mason8b712842008-06-11 16:50:36 -04005683 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005684 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005685 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005686 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005687 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005688 return;
Chris Mason8b712842008-06-11 16:50:36 -04005689 }
5690
5691 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005692 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005693 * higher layers. Otherwise, the async bio makes it appear we have
5694 * made progress against dirty pages when we've really just put it
5695 * on a queue for later
5696 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005697 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005698 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005699 bio->bi_next = NULL;
5700 bio->bi_rw |= rw;
5701
5702 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005703 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005704 pending_bios = &device->pending_sync_bios;
5705 else
5706 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005707
Chris Masonffbd5172009-04-20 15:50:09 -04005708 if (pending_bios->tail)
5709 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005710
Chris Masonffbd5172009-04-20 15:50:09 -04005711 pending_bios->tail = bio;
5712 if (!pending_bios->head)
5713 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005714 if (device->running_pending)
5715 should_queue = 0;
5716
5717 spin_unlock(&device->io_lock);
5718
5719 if (should_queue)
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005720 btrfs_queue_work(root->fs_info->submit_workers,
5721 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005722}
5723
Josef Bacikde1ee922012-10-19 16:50:56 -04005724static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5725 sector_t sector)
5726{
5727 struct bio_vec *prev;
5728 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005729 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005730 struct bvec_merge_data bvm = {
5731 .bi_bdev = bdev,
5732 .bi_sector = sector,
5733 .bi_rw = bio->bi_rw,
5734 };
5735
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305736 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005737 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005738
5739 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005740 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005741 return 0;
5742
5743 if (!q->merge_bvec_fn)
5744 return 1;
5745
Kent Overstreet4f024f32013-10-11 15:44:27 -07005746 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005747 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5748 return 0;
5749 return 1;
5750}
5751
5752static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5753 struct bio *bio, u64 physical, int dev_nr,
5754 int rw, int async)
5755{
5756 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5757
5758 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005759 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005760 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005761 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005762#ifdef DEBUG
5763 {
5764 struct rcu_string *name;
5765
5766 rcu_read_lock();
5767 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005768 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005769 "(%s id %llu), size=%u\n", rw,
Fabian Frederick1b6e4462014-09-24 20:23:05 +02005770 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
5771 name->str, dev->devid, bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04005772 rcu_read_unlock();
5773 }
5774#endif
5775 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005776
5777 btrfs_bio_counter_inc_noblocked(root->fs_info);
5778
Josef Bacikde1ee922012-10-19 16:50:56 -04005779 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005780 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005781 else
5782 btrfsic_submit_bio(rw, bio);
5783}
5784
5785static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5786 struct bio *first_bio, struct btrfs_device *dev,
5787 int dev_nr, int rw, int async)
5788{
5789 struct bio_vec *bvec = first_bio->bi_io_vec;
5790 struct bio *bio;
5791 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5792 u64 physical = bbio->stripes[dev_nr].physical;
5793
5794again:
5795 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5796 if (!bio)
5797 return -ENOMEM;
5798
5799 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5800 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5801 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005802 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005803
5804 atomic_inc(&bbio->stripes_pending);
5805 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5806 rw, async);
5807 physical += len;
5808 goto again;
5809 }
5810 bvec++;
5811 }
5812
5813 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5814 return 0;
5815}
5816
5817static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5818{
5819 atomic_inc(&bbio->error);
5820 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005821 /* Shoud be the original bio. */
5822 WARN_ON(bio != bbio->orig_bio);
5823
Josef Bacikde1ee922012-10-19 16:50:56 -04005824 bio->bi_private = bbio->private;
5825 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005826 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005827 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005828
5829 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005830 }
5831}
5832
Chris Masonf1885912008-04-09 16:28:12 -04005833int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005834 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005835{
Chris Mason0b86a832008-03-24 15:01:56 -04005836 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005837 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005838 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005839 u64 length = 0;
5840 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04005841 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08005842 int dev_nr;
5843 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005844 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005845
Kent Overstreet4f024f32013-10-11 15:44:27 -07005846 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005847 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005848
Miao Xiec404e0d2014-01-30 16:46:55 +08005849 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005850 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005851 mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08005852 if (ret) {
5853 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005854 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005855 }
Chris Masoncea9e442008-04-09 16:28:12 -04005856
Jan Schmidta1d3c472011-08-04 17:15:33 +02005857 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005858 bbio->orig_bio = first_bio;
5859 bbio->private = first_bio->bi_private;
5860 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005861 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005862 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5863
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005864 if (bbio->raid_map) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005865 /* In this case, map_length has been set to the length of
5866 a single stripe; not the whole write */
5867 if (rw & WRITE) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005868 ret = raid56_parity_write(root, bio, bbio, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005869 } else {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005870 ret = raid56_parity_recover(root, bio, bbio, map_length,
Miao Xie42452152014-11-25 16:39:28 +08005871 mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05005872 }
Miao Xie42452152014-11-25 16:39:28 +08005873
Miao Xiec404e0d2014-01-30 16:46:55 +08005874 btrfs_bio_counter_dec(root->fs_info);
5875 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005876 }
5877
Chris Masoncea9e442008-04-09 16:28:12 -04005878 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005879 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005880 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005881 BUG();
5882 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005883
Zhao Lei08da7572015-02-12 15:42:16 +08005884 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005885 dev = bbio->stripes[dev_nr].dev;
5886 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5887 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04005888 continue;
5889 }
5890
5891 /*
5892 * Check and see if we're ok with this bio based on it's size
5893 * and offset with the given device.
5894 */
5895 if (!bio_size_ok(dev->bdev, first_bio,
5896 bbio->stripes[dev_nr].physical >> 9)) {
5897 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5898 dev_nr, rw, async_submit);
5899 BUG_ON(ret);
Josef Bacikde1ee922012-10-19 16:50:56 -04005900 continue;
5901 }
5902
Jan Schmidta1d3c472011-08-04 17:15:33 +02005903 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005904 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005905 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005906 } else {
5907 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005908 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005909 }
Josef Bacik606686e2012-06-04 14:03:51 -04005910
Josef Bacikde1ee922012-10-19 16:50:56 -04005911 submit_stripe_bio(root, bbio, bio,
5912 bbio->stripes[dev_nr].physical, dev_nr, rw,
5913 async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04005914 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005915 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005916 return 0;
5917}
5918
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005919struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005920 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005921{
Yan Zheng2b820322008-11-17 21:11:30 -05005922 struct btrfs_device *device;
5923 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005924
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005925 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005926 while (cur_devices) {
5927 if (!fsid ||
5928 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5929 device = __find_device(&cur_devices->devices,
5930 devid, uuid);
5931 if (device)
5932 return device;
5933 }
5934 cur_devices = cur_devices->seed;
5935 }
5936 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005937}
5938
Chris Masondfe25022008-05-13 13:46:40 -04005939static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
Miao Xie5f375832014-09-03 21:35:46 +08005940 struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04005941 u64 devid, u8 *dev_uuid)
5942{
5943 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04005944
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005945 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5946 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005947 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005948
5949 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005950 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005951 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005952
5953 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005954 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005955
Chris Masondfe25022008-05-13 13:46:40 -04005956 return device;
5957}
5958
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005959/**
5960 * btrfs_alloc_device - allocate struct btrfs_device
5961 * @fs_info: used only for generating a new devid, can be NULL if
5962 * devid is provided (i.e. @devid != NULL).
5963 * @devid: a pointer to devid for this device. If NULL a new devid
5964 * is generated.
5965 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5966 * is generated.
5967 *
5968 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5969 * on error. Returned struct is not linked onto any lists and can be
5970 * destroyed with kfree() right away.
5971 */
5972struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5973 const u64 *devid,
5974 const u8 *uuid)
5975{
5976 struct btrfs_device *dev;
5977 u64 tmp;
5978
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305979 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005980 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005981
5982 dev = __alloc_device();
5983 if (IS_ERR(dev))
5984 return dev;
5985
5986 if (devid)
5987 tmp = *devid;
5988 else {
5989 int ret;
5990
5991 ret = find_next_devid(fs_info, &tmp);
5992 if (ret) {
5993 kfree(dev);
5994 return ERR_PTR(ret);
5995 }
5996 }
5997 dev->devid = tmp;
5998
5999 if (uuid)
6000 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6001 else
6002 generate_random_uuid(dev->uuid);
6003
Liu Bo9e0af232014-08-15 23:36:53 +08006004 btrfs_init_work(&dev->work, btrfs_submit_helper,
6005 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006006
6007 return dev;
6008}
6009
Chris Mason0b86a832008-03-24 15:01:56 -04006010static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6011 struct extent_buffer *leaf,
6012 struct btrfs_chunk *chunk)
6013{
6014 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6015 struct map_lookup *map;
6016 struct extent_map *em;
6017 u64 logical;
6018 u64 length;
6019 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006020 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006021 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006022 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006023 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006024
Chris Masone17cade2008-04-15 15:41:47 -04006025 logical = key->offset;
6026 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04006027
Chris Mason890871b2009-09-02 16:24:52 -04006028 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006029 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006030 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006031
6032 /* already mapped? */
6033 if (em && em->start <= logical && em->start + em->len > logical) {
6034 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006035 return 0;
6036 } else if (em) {
6037 free_extent_map(em);
6038 }
Chris Mason0b86a832008-03-24 15:01:56 -04006039
David Sterba172ddd62011-04-21 00:48:27 +02006040 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006041 if (!em)
6042 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006043 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6044 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006045 if (!map) {
6046 free_extent_map(em);
6047 return -ENOMEM;
6048 }
6049
Wang Shilong298a8f92014-06-19 10:42:52 +08006050 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04006051 em->bdev = (struct block_device *)map;
6052 em->start = logical;
6053 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006054 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006055 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006056 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006057
Chris Mason593060d2008-03-25 16:50:33 -04006058 map->num_stripes = num_stripes;
6059 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6060 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6061 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6062 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6063 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006064 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006065 for (i = 0; i < num_stripes; i++) {
6066 map->stripes[i].physical =
6067 btrfs_stripe_offset_nr(leaf, chunk, i);
6068 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006069 read_extent_buffer(leaf, uuid, (unsigned long)
6070 btrfs_stripe_dev_uuid_nr(chunk, i),
6071 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006072 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6073 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04006074 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006075 free_extent_map(em);
6076 return -EIO;
6077 }
Chris Masondfe25022008-05-13 13:46:40 -04006078 if (!map->stripes[i].dev) {
6079 map->stripes[i].dev =
Miao Xie5f375832014-09-03 21:35:46 +08006080 add_missing_dev(root, root->fs_info->fs_devices,
6081 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006082 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04006083 free_extent_map(em);
6084 return -EIO;
6085 }
6086 }
6087 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006088 }
6089
Chris Mason890871b2009-09-02 16:24:52 -04006090 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006091 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006092 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006093 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006094 free_extent_map(em);
6095
6096 return 0;
6097}
6098
Jeff Mahoney143bede2012-03-01 14:56:26 +01006099static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006100 struct btrfs_dev_item *dev_item,
6101 struct btrfs_device *device)
6102{
6103 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006104
6105 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006106 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6107 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006108 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006109 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006110 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006111 device->type = btrfs_device_type(leaf, dev_item);
6112 device->io_align = btrfs_device_io_align(leaf, dev_item);
6113 device->io_width = btrfs_device_io_width(leaf, dev_item);
6114 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006115 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006116 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006117
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006118 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006119 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006120}
6121
Miao Xie5f375832014-09-03 21:35:46 +08006122static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6123 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006124{
6125 struct btrfs_fs_devices *fs_devices;
6126 int ret;
6127
Li Zefanb367e472011-12-07 11:38:24 +08006128 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006129
6130 fs_devices = root->fs_info->fs_devices->seed;
6131 while (fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006132 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6133 return fs_devices;
6134
Yan Zheng2b820322008-11-17 21:11:30 -05006135 fs_devices = fs_devices->seed;
6136 }
6137
6138 fs_devices = find_fsid(fsid);
6139 if (!fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006140 if (!btrfs_test_opt(root, DEGRADED))
6141 return ERR_PTR(-ENOENT);
6142
6143 fs_devices = alloc_fs_devices(fsid);
6144 if (IS_ERR(fs_devices))
6145 return fs_devices;
6146
6147 fs_devices->seeding = 1;
6148 fs_devices->opened = 1;
6149 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006150 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006151
6152 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006153 if (IS_ERR(fs_devices))
6154 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006155
Christoph Hellwig97288f22008-12-02 06:36:09 -05006156 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006157 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006158 if (ret) {
6159 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006160 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006161 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006162 }
Yan Zheng2b820322008-11-17 21:11:30 -05006163
6164 if (!fs_devices->seeding) {
6165 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006166 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006167 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006168 goto out;
6169 }
6170
6171 fs_devices->seed = root->fs_info->fs_devices->seed;
6172 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006173out:
Miao Xie5f375832014-09-03 21:35:46 +08006174 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006175}
6176
Chris Mason0d81ba52008-03-24 15:02:07 -04006177static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006178 struct extent_buffer *leaf,
6179 struct btrfs_dev_item *dev_item)
6180{
Miao Xie5f375832014-09-03 21:35:46 +08006181 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006182 struct btrfs_device *device;
6183 u64 devid;
6184 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006185 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006186 u8 dev_uuid[BTRFS_UUID_SIZE];
6187
Chris Mason0b86a832008-03-24 15:01:56 -04006188 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006189 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006190 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006191 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006192 BTRFS_UUID_SIZE);
6193
6194 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
Miao Xie5f375832014-09-03 21:35:46 +08006195 fs_devices = open_seed_devices(root, fs_uuid);
6196 if (IS_ERR(fs_devices))
6197 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006198 }
6199
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006200 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006201 if (!device) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006202 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006203 return -EIO;
6204
Miao Xie5f375832014-09-03 21:35:46 +08006205 btrfs_warn(root->fs_info, "devid %llu missing", devid);
6206 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6207 if (!device)
6208 return -ENOMEM;
6209 } else {
6210 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6211 return -EIO;
6212
6213 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006214 /*
6215 * this happens when a device that was properly setup
6216 * in the device info lists suddenly goes bad.
6217 * device->bdev is NULL, and so we have to set
6218 * device->missing to one here
6219 */
Miao Xie5f375832014-09-03 21:35:46 +08006220 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006221 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006222 }
Miao Xie5f375832014-09-03 21:35:46 +08006223
6224 /* Move the device to its own fs_devices */
6225 if (device->fs_devices != fs_devices) {
6226 ASSERT(device->missing);
6227
6228 list_move(&device->dev_list, &fs_devices->devices);
6229 device->fs_devices->num_devices--;
6230 fs_devices->num_devices++;
6231
6232 device->fs_devices->missing_devices--;
6233 fs_devices->missing_devices++;
6234
6235 device->fs_devices = fs_devices;
6236 }
Yan Zheng2b820322008-11-17 21:11:30 -05006237 }
6238
6239 if (device->fs_devices != root->fs_info->fs_devices) {
6240 BUG_ON(device->writeable);
6241 if (device->generation !=
6242 btrfs_device_generation(leaf, dev_item))
6243 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006244 }
Chris Mason0b86a832008-03-24 15:01:56 -04006245
6246 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006247 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006248 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006249 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006250 spin_lock(&root->fs_info->free_chunk_lock);
6251 root->fs_info->free_chunk_space += device->total_bytes -
6252 device->bytes_used;
6253 spin_unlock(&root->fs_info->free_chunk_lock);
6254 }
Chris Mason0b86a832008-03-24 15:01:56 -04006255 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006256 return ret;
6257}
6258
Yan Zhenge4404d62008-12-12 10:03:26 -05006259int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006260{
David Sterba6c417612011-04-13 15:41:04 +02006261 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006262 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006263 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006264 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006265 u8 *array_ptr;
6266 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006267 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006268 u32 num_stripes;
6269 u32 array_size;
6270 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006271 u32 cur_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006272 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006273
David Sterbaa83fffb2014-06-15 02:39:54 +02006274 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
6275 /*
6276 * This will create extent buffer of nodesize, superblock size is
6277 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6278 * overallocate but we can keep it as-is, only the first page is used.
6279 */
6280 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
Chris Masona061fc82008-05-07 11:43:44 -04006281 if (!sb)
6282 return -ENOMEM;
6283 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006284 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006285 /*
6286 * The sb extent buffer is artifical and just used to read the system array.
6287 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6288 * pages up-to-date when the page is larger: extent does not cover the
6289 * whole page and consequently check_page_uptodate does not find all
6290 * the page's extents up-to-date (the hole beyond sb),
6291 * write_extent_buffer then triggers a WARN_ON.
6292 *
6293 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6294 * but sb spans only this function. Add an explicit SetPageUptodate call
6295 * to silence the warning eg. on PowerPC 64.
6296 */
6297 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006298 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006299
Chris Masona061fc82008-05-07 11:43:44 -04006300 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006301 array_size = btrfs_super_sys_array_size(super_copy);
6302
David Sterba1ffb22c2014-10-31 19:02:42 +01006303 array_ptr = super_copy->sys_chunk_array;
6304 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6305 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006306
David Sterba1ffb22c2014-10-31 19:02:42 +01006307 while (cur_offset < array_size) {
6308 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006309 len = sizeof(*disk_key);
6310 if (cur_offset + len > array_size)
6311 goto out_short_read;
6312
Chris Mason0b86a832008-03-24 15:01:56 -04006313 btrfs_disk_key_to_cpu(&key, disk_key);
6314
David Sterba1ffb22c2014-10-31 19:02:42 +01006315 array_ptr += len;
6316 sb_array_offset += len;
6317 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006318
Chris Mason0d81ba52008-03-24 15:02:07 -04006319 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006320 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006321 /*
6322 * At least one btrfs_chunk with one stripe must be
6323 * present, exact stripe count check comes afterwards
6324 */
6325 len = btrfs_chunk_item_size(1);
6326 if (cur_offset + len > array_size)
6327 goto out_short_read;
6328
6329 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6330 len = btrfs_chunk_item_size(num_stripes);
6331 if (cur_offset + len > array_size)
6332 goto out_short_read;
6333
Chris Mason0d81ba52008-03-24 15:02:07 -04006334 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006335 if (ret)
6336 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006337 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006338 ret = -EIO;
6339 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006340 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006341 array_ptr += len;
6342 sb_array_offset += len;
6343 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006344 }
Chris Masona061fc82008-05-07 11:43:44 -04006345 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006346 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006347
6348out_short_read:
6349 printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n",
6350 len, cur_offset);
6351 free_extent_buffer(sb);
6352 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006353}
6354
6355int btrfs_read_chunk_tree(struct btrfs_root *root)
6356{
6357 struct btrfs_path *path;
6358 struct extent_buffer *leaf;
6359 struct btrfs_key key;
6360 struct btrfs_key found_key;
6361 int ret;
6362 int slot;
6363
6364 root = root->fs_info->chunk_root;
6365
6366 path = btrfs_alloc_path();
6367 if (!path)
6368 return -ENOMEM;
6369
Li Zefanb367e472011-12-07 11:38:24 +08006370 mutex_lock(&uuid_mutex);
6371 lock_chunks(root);
6372
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006373 /*
6374 * Read all device items, and then all the chunk items. All
6375 * device items are found before any chunk item (their object id
6376 * is smaller than the lowest possible object id for a chunk
6377 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006378 */
6379 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6380 key.offset = 0;
6381 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006382 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006383 if (ret < 0)
6384 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006385 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006386 leaf = path->nodes[0];
6387 slot = path->slots[0];
6388 if (slot >= btrfs_header_nritems(leaf)) {
6389 ret = btrfs_next_leaf(root, path);
6390 if (ret == 0)
6391 continue;
6392 if (ret < 0)
6393 goto error;
6394 break;
6395 }
6396 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006397 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6398 struct btrfs_dev_item *dev_item;
6399 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006400 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006401 ret = read_one_dev(root, leaf, dev_item);
6402 if (ret)
6403 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006404 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6405 struct btrfs_chunk *chunk;
6406 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6407 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006408 if (ret)
6409 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006410 }
6411 path->slots[0]++;
6412 }
Chris Mason0b86a832008-03-24 15:01:56 -04006413 ret = 0;
6414error:
Li Zefanb367e472011-12-07 11:38:24 +08006415 unlock_chunks(root);
6416 mutex_unlock(&uuid_mutex);
6417
Yan Zheng2b820322008-11-17 21:11:30 -05006418 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006419 return ret;
6420}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006421
Miao Xiecb517ea2013-05-15 07:48:19 +00006422void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6423{
6424 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6425 struct btrfs_device *device;
6426
Liu Bo29cc83f2014-05-11 23:14:59 +08006427 while (fs_devices) {
6428 mutex_lock(&fs_devices->device_list_mutex);
6429 list_for_each_entry(device, &fs_devices->devices, dev_list)
6430 device->dev_root = fs_info->dev_root;
6431 mutex_unlock(&fs_devices->device_list_mutex);
6432
6433 fs_devices = fs_devices->seed;
6434 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006435}
6436
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006437static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6438{
6439 int i;
6440
6441 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6442 btrfs_dev_stat_reset(dev, i);
6443}
6444
6445int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6446{
6447 struct btrfs_key key;
6448 struct btrfs_key found_key;
6449 struct btrfs_root *dev_root = fs_info->dev_root;
6450 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6451 struct extent_buffer *eb;
6452 int slot;
6453 int ret = 0;
6454 struct btrfs_device *device;
6455 struct btrfs_path *path = NULL;
6456 int i;
6457
6458 path = btrfs_alloc_path();
6459 if (!path) {
6460 ret = -ENOMEM;
6461 goto out;
6462 }
6463
6464 mutex_lock(&fs_devices->device_list_mutex);
6465 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6466 int item_size;
6467 struct btrfs_dev_stats_item *ptr;
6468
6469 key.objectid = 0;
6470 key.type = BTRFS_DEV_STATS_KEY;
6471 key.offset = device->devid;
6472 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6473 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006474 __btrfs_reset_dev_stats(device);
6475 device->dev_stats_valid = 1;
6476 btrfs_release_path(path);
6477 continue;
6478 }
6479 slot = path->slots[0];
6480 eb = path->nodes[0];
6481 btrfs_item_key_to_cpu(eb, &found_key, slot);
6482 item_size = btrfs_item_size_nr(eb, slot);
6483
6484 ptr = btrfs_item_ptr(eb, slot,
6485 struct btrfs_dev_stats_item);
6486
6487 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6488 if (item_size >= (1 + i) * sizeof(__le64))
6489 btrfs_dev_stat_set(device, i,
6490 btrfs_dev_stats_value(eb, ptr, i));
6491 else
6492 btrfs_dev_stat_reset(device, i);
6493 }
6494
6495 device->dev_stats_valid = 1;
6496 btrfs_dev_stat_print_on_load(device);
6497 btrfs_release_path(path);
6498 }
6499 mutex_unlock(&fs_devices->device_list_mutex);
6500
6501out:
6502 btrfs_free_path(path);
6503 return ret < 0 ? ret : 0;
6504}
6505
6506static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6507 struct btrfs_root *dev_root,
6508 struct btrfs_device *device)
6509{
6510 struct btrfs_path *path;
6511 struct btrfs_key key;
6512 struct extent_buffer *eb;
6513 struct btrfs_dev_stats_item *ptr;
6514 int ret;
6515 int i;
6516
6517 key.objectid = 0;
6518 key.type = BTRFS_DEV_STATS_KEY;
6519 key.offset = device->devid;
6520
6521 path = btrfs_alloc_path();
6522 BUG_ON(!path);
6523 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6524 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006525 printk_in_rcu(KERN_WARNING "BTRFS: "
6526 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006527 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006528 goto out;
6529 }
6530
6531 if (ret == 0 &&
6532 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6533 /* need to delete old one and insert a new one */
6534 ret = btrfs_del_item(trans, dev_root, path);
6535 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006536 printk_in_rcu(KERN_WARNING "BTRFS: "
6537 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006538 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006539 goto out;
6540 }
6541 ret = 1;
6542 }
6543
6544 if (ret == 1) {
6545 /* need to insert a new item */
6546 btrfs_release_path(path);
6547 ret = btrfs_insert_empty_item(trans, dev_root, path,
6548 &key, sizeof(*ptr));
6549 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006550 printk_in_rcu(KERN_WARNING "BTRFS: "
6551 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006552 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006553 goto out;
6554 }
6555 }
6556
6557 eb = path->nodes[0];
6558 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6559 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6560 btrfs_set_dev_stats_value(eb, ptr, i,
6561 btrfs_dev_stat_read(device, i));
6562 btrfs_mark_buffer_dirty(eb);
6563
6564out:
6565 btrfs_free_path(path);
6566 return ret;
6567}
6568
6569/*
6570 * called from commit_transaction. Writes all changed device stats to disk.
6571 */
6572int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6573 struct btrfs_fs_info *fs_info)
6574{
6575 struct btrfs_root *dev_root = fs_info->dev_root;
6576 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6577 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006578 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006579 int ret = 0;
6580
6581 mutex_lock(&fs_devices->device_list_mutex);
6582 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006583 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006584 continue;
6585
Miao Xieaddc3fa2014-07-24 11:37:11 +08006586 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006587 ret = update_dev_stat_item(trans, dev_root, device);
6588 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006589 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006590 }
6591 mutex_unlock(&fs_devices->device_list_mutex);
6592
6593 return ret;
6594}
6595
Stefan Behrens442a4f62012-05-25 16:06:08 +02006596void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6597{
6598 btrfs_dev_stat_inc(dev, index);
6599 btrfs_dev_stat_print_on_error(dev);
6600}
6601
Eric Sandeen48a3b632013-04-25 20:41:01 +00006602static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006603{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006604 if (!dev->dev_stats_valid)
6605 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006606 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6607 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006608 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006609 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6610 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6611 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006612 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6613 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006614}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006615
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006616static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6617{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006618 int i;
6619
6620 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6621 if (btrfs_dev_stat_read(dev, i) != 0)
6622 break;
6623 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6624 return; /* all values == 0, suppress message */
6625
Frank Holtonefe120a2013-12-20 11:37:06 -05006626 printk_in_rcu(KERN_INFO "BTRFS: "
6627 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006628 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006629 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6630 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6631 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6632 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6633 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6634}
6635
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006636int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006637 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006638{
6639 struct btrfs_device *dev;
6640 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6641 int i;
6642
6643 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006644 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006645 mutex_unlock(&fs_devices->device_list_mutex);
6646
6647 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006648 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006649 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006650 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006651 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006652 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006653 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006654 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6655 if (stats->nr_items > i)
6656 stats->values[i] =
6657 btrfs_dev_stat_read_and_reset(dev, i);
6658 else
6659 btrfs_dev_stat_reset(dev, i);
6660 }
6661 } else {
6662 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6663 if (stats->nr_items > i)
6664 stats->values[i] = btrfs_dev_stat_read(dev, i);
6665 }
6666 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6667 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6668 return 0;
6669}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006670
6671int btrfs_scratch_superblock(struct btrfs_device *device)
6672{
6673 struct buffer_head *bh;
6674 struct btrfs_super_block *disk_super;
6675
6676 bh = btrfs_read_dev_super(device->bdev);
6677 if (!bh)
6678 return -EINVAL;
6679 disk_super = (struct btrfs_super_block *)bh->b_data;
6680
6681 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6682 set_buffer_dirty(bh);
6683 sync_dirty_buffer(bh);
6684 brelse(bh);
6685
6686 return 0;
6687}
Miao Xie935e5cc2014-09-03 21:35:33 +08006688
6689/*
6690 * Update the size of all devices, which is used for writing out the
6691 * super blocks.
6692 */
6693void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6694{
6695 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6696 struct btrfs_device *curr, *next;
6697
6698 if (list_empty(&fs_devices->resized_devices))
6699 return;
6700
6701 mutex_lock(&fs_devices->device_list_mutex);
6702 lock_chunks(fs_info->dev_root);
6703 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6704 resized_list) {
6705 list_del_init(&curr->resized_list);
6706 curr->commit_total_bytes = curr->disk_total_bytes;
6707 }
6708 unlock_chunks(fs_info->dev_root);
6709 mutex_unlock(&fs_devices->device_list_mutex);
6710}
Miao Xiece7213c2014-09-03 21:35:34 +08006711
6712/* Must be invoked during the transaction commit */
6713void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6714 struct btrfs_transaction *transaction)
6715{
6716 struct extent_map *em;
6717 struct map_lookup *map;
6718 struct btrfs_device *dev;
6719 int i;
6720
6721 if (list_empty(&transaction->pending_chunks))
6722 return;
6723
6724 /* In order to kick the device replace finish process */
6725 lock_chunks(root);
6726 list_for_each_entry(em, &transaction->pending_chunks, list) {
6727 map = (struct map_lookup *)em->bdev;
6728
6729 for (i = 0; i < map->num_stripes; i++) {
6730 dev = map->stripes[i].dev;
6731 dev->commit_bytes_used = dev->bytes_used;
6732 }
6733 }
6734 unlock_chunks(root);
6735}
Anand Jain5a13f432015-03-10 06:38:31 +08006736
6737void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
6738{
6739 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6740 while (fs_devices) {
6741 fs_devices->fs_info = fs_info;
6742 fs_devices = fs_devices->seed;
6743 }
6744}
6745
6746void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
6747{
6748 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6749 while (fs_devices) {
6750 fs_devices->fs_info = NULL;
6751 fs_devices = fs_devices->seed;
6752 }
6753}