blob: d4cd4059bdedcc2698a4053a780683d5864f4241 [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
Anand Jain4fde46f2015-06-17 21:10:48 +0800448
449void btrfs_free_stale_device(struct btrfs_device *cur_dev)
450{
451 struct btrfs_fs_devices *fs_devs;
452 struct btrfs_device *dev;
453
454 if (!cur_dev->name)
455 return;
456
457 list_for_each_entry(fs_devs, &fs_uuids, list) {
458 int del = 1;
459
460 if (fs_devs->opened)
461 continue;
462 if (fs_devs->seeding)
463 continue;
464
465 list_for_each_entry(dev, &fs_devs->devices, dev_list) {
466
467 if (dev == cur_dev)
468 continue;
469 if (!dev->name)
470 continue;
471
472 /*
473 * Todo: This won't be enough. What if the same device
474 * comes back (with new uuid and) with its mapper path?
475 * But for now, this does help as mostly an admin will
476 * either use mapper or non mapper path throughout.
477 */
478 rcu_read_lock();
479 del = strcmp(rcu_str_deref(dev->name),
480 rcu_str_deref(cur_dev->name));
481 rcu_read_unlock();
482 if (!del)
483 break;
484 }
485
486 if (!del) {
487 /* delete the stale device */
488 if (fs_devs->num_devices == 1) {
489 btrfs_sysfs_remove_fsid(fs_devs);
490 list_del(&fs_devs->list);
491 free_fs_devices(fs_devs);
492 } else {
493 fs_devs->num_devices--;
494 list_del(&dev->dev_list);
495 rcu_string_free(dev->name);
496 kfree(dev);
497 }
498 break;
499 }
500 }
501}
502
David Sterba60999ca2014-03-26 18:26:36 +0100503/*
504 * Add new device to list of registered devices
505 *
506 * Returns:
507 * 1 - first time device is seen
508 * 0 - device already known
509 * < 0 - error
510 */
Chris Masona1b32a52008-09-05 16:09:51 -0400511static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400512 struct btrfs_super_block *disk_super,
513 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
514{
515 struct btrfs_device *device;
516 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400517 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100518 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400519 u64 found_transid = btrfs_super_generation(disk_super);
520
521 fs_devices = find_fsid(disk_super->fsid);
522 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300523 fs_devices = alloc_fs_devices(disk_super->fsid);
524 if (IS_ERR(fs_devices))
525 return PTR_ERR(fs_devices);
526
Chris Mason8a4b83c2008-03-24 15:02:07 -0400527 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300528
Chris Mason8a4b83c2008-03-24 15:02:07 -0400529 device = NULL;
530 } else {
Chris Masona4437552008-04-18 10:29:38 -0400531 device = __find_device(&fs_devices->devices, devid,
532 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400533 }
Miao Xie443f24f2014-07-24 11:37:15 +0800534
Chris Mason8a4b83c2008-03-24 15:02:07 -0400535 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500536 if (fs_devices->opened)
537 return -EBUSY;
538
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300539 device = btrfs_alloc_device(NULL, &devid,
540 disk_super->dev_item.uuid);
541 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400542 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300543 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400544 }
Josef Bacik606686e2012-06-04 14:03:51 -0400545
546 name = rcu_string_strdup(path, GFP_NOFS);
547 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400548 kfree(device);
549 return -ENOMEM;
550 }
Josef Bacik606686e2012-06-04 14:03:51 -0400551 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200552
Chris Masone5e9a522009-06-10 15:17:02 -0400553 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000554 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100555 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400556 mutex_unlock(&fs_devices->device_list_mutex);
557
David Sterba60999ca2014-03-26 18:26:36 +0100558 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500559 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400560 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800561 /*
562 * When FS is already mounted.
563 * 1. If you are here and if the device->name is NULL that
564 * means this device was missing at time of FS mount.
565 * 2. If you are here and if the device->name is different
566 * from 'path' that means either
567 * a. The same device disappeared and reappeared with
568 * different name. or
569 * b. The missing-disk-which-was-replaced, has
570 * reappeared now.
571 *
572 * We must allow 1 and 2a above. But 2b would be a spurious
573 * and unintentional.
574 *
575 * Further in case of 1 and 2a above, the disk at 'path'
576 * would have missed some transaction when it was away and
577 * in case of 2a the stale bdev has to be updated as well.
578 * 2b must not be allowed at all time.
579 */
580
581 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700582 * For now, we do allow update to btrfs_fs_device through the
583 * btrfs dev scan cli after FS has been mounted. We're still
584 * tracking a problem where systems fail mount by subvolume id
585 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800586 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700587 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800588 /*
589 * That is if the FS is _not_ mounted and if you
590 * are here, that means there is more than one
591 * disk with same uuid and devid.We keep the one
592 * with larger generation number or the last-in if
593 * generation are equal.
594 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700595 return -EEXIST;
Anand Jain77bdae42014-07-03 18:22:06 +0800596 }
Anand Jainb96de002014-07-03 18:22:05 +0800597
Josef Bacik606686e2012-06-04 14:03:51 -0400598 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000599 if (!name)
600 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400601 rcu_string_free(device->name);
602 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500603 if (device->missing) {
604 fs_devices->missing_devices--;
605 device->missing = 0;
606 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400607 }
608
Anand Jain77bdae42014-07-03 18:22:06 +0800609 /*
610 * Unmount does not free the btrfs_device struct but would zero
611 * generation along with most of the other members. So just update
612 * it back. We need it to pick the disk with largest generation
613 * (as above).
614 */
615 if (!fs_devices->opened)
616 device->generation = found_transid;
617
Anand Jain4fde46f2015-06-17 21:10:48 +0800618 /*
619 * if there is new btrfs on an already registered device,
620 * then remove the stale device entry.
621 */
622 btrfs_free_stale_device(device);
623
Chris Mason8a4b83c2008-03-24 15:02:07 -0400624 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100625
626 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400627}
628
Yan Zhenge4404d62008-12-12 10:03:26 -0500629static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
630{
631 struct btrfs_fs_devices *fs_devices;
632 struct btrfs_device *device;
633 struct btrfs_device *orig_dev;
634
Ilya Dryomov2208a372013-08-12 14:33:03 +0300635 fs_devices = alloc_fs_devices(orig->fsid);
636 if (IS_ERR(fs_devices))
637 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500638
Miao Xieadbbb862014-09-03 21:35:42 +0800639 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400640 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500641
Xiao Guangrong46224702011-04-20 10:08:47 +0000642 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500643 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400644 struct rcu_string *name;
645
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300646 device = btrfs_alloc_device(NULL, &orig_dev->devid,
647 orig_dev->uuid);
648 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500649 goto error;
650
Josef Bacik606686e2012-06-04 14:03:51 -0400651 /*
652 * This is ok to do without rcu read locked because we hold the
653 * uuid mutex so nothing we touch in here is going to disappear.
654 */
Anand Jaine755f782014-06-30 17:12:47 +0800655 if (orig_dev->name) {
656 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
657 if (!name) {
658 kfree(device);
659 goto error;
660 }
661 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400662 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500663
Yan Zhenge4404d62008-12-12 10:03:26 -0500664 list_add(&device->dev_list, &fs_devices->devices);
665 device->fs_devices = fs_devices;
666 fs_devices->num_devices++;
667 }
Miao Xieadbbb862014-09-03 21:35:42 +0800668 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500669 return fs_devices;
670error:
Miao Xieadbbb862014-09-03 21:35:42 +0800671 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500672 free_fs_devices(fs_devices);
673 return ERR_PTR(-ENOMEM);
674}
675
Eric Sandeen9eaed212014-08-01 18:12:35 -0500676void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400677{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500678 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800679 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500680
Chris Masondfe25022008-05-13 13:46:40 -0400681 mutex_lock(&uuid_mutex);
682again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000683 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500684 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500685 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100686 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800687 (!latest_dev ||
688 device->generation > latest_dev->generation)) {
689 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500690 }
Yan Zheng2b820322008-11-17 21:11:30 -0500691 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500692 }
Yan Zheng2b820322008-11-17 21:11:30 -0500693
Stefan Behrens8dabb742012-11-06 13:15:27 +0100694 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
695 /*
696 * In the first step, keep the device which has
697 * the correct fsid and the devid that is used
698 * for the dev_replace procedure.
699 * In the second step, the dev_replace state is
700 * read from the device tree and it is known
701 * whether the procedure is really active or
702 * not, which means whether this device is
703 * used or whether it should be removed.
704 */
705 if (step == 0 || device->is_tgtdev_for_dev_replace) {
706 continue;
707 }
708 }
Yan Zheng2b820322008-11-17 21:11:30 -0500709 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100710 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500711 device->bdev = NULL;
712 fs_devices->open_devices--;
713 }
714 if (device->writeable) {
715 list_del_init(&device->dev_alloc_list);
716 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100717 if (!device->is_tgtdev_for_dev_replace)
718 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500719 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500720 list_del_init(&device->dev_list);
721 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400722 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500723 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400724 }
Yan Zheng2b820322008-11-17 21:11:30 -0500725
726 if (fs_devices->seed) {
727 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500728 goto again;
729 }
730
Miao Xie443f24f2014-07-24 11:37:15 +0800731 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500732
Chris Masondfe25022008-05-13 13:46:40 -0400733 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400734}
Chris Masona0af4692008-05-13 16:03:06 -0400735
Xiao Guangrong1f781602011-04-20 10:09:16 +0000736static void __free_device(struct work_struct *work)
737{
738 struct btrfs_device *device;
739
740 device = container_of(work, struct btrfs_device, rcu_work);
741
742 if (device->bdev)
743 blkdev_put(device->bdev, device->mode);
744
Josef Bacik606686e2012-06-04 14:03:51 -0400745 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000746 kfree(device);
747}
748
749static void free_device(struct rcu_head *head)
750{
751 struct btrfs_device *device;
752
753 device = container_of(head, struct btrfs_device, rcu);
754
755 INIT_WORK(&device->rcu_work, __free_device);
756 schedule_work(&device->rcu_work);
757}
758
Yan Zheng2b820322008-11-17 21:11:30 -0500759static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400760{
Sasha Levin2037a092015-05-12 19:31:37 -0400761 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -0500762
Yan Zheng2b820322008-11-17 21:11:30 -0500763 if (--fs_devices->opened > 0)
764 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400765
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000766 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -0400767 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000768 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400769 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000770
771 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400772 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000773
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300774 if (device->writeable &&
775 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500776 list_del_init(&device->dev_alloc_list);
777 fs_devices->rw_devices--;
778 }
779
Josef Bacik726551e2013-08-26 13:45:53 -0400780 if (device->missing)
781 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000782
Ilya Dryomova1e87802013-08-12 14:33:04 +0300783 new_device = btrfs_alloc_device(NULL, &device->devid,
784 device->uuid);
785 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400786
787 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400788 if (device->name) {
789 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300790 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400791 rcu_assign_pointer(new_device->name, name);
792 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300793
Xiao Guangrong1f781602011-04-20 10:09:16 +0000794 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300795 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000796
797 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400798 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000799 mutex_unlock(&fs_devices->device_list_mutex);
800
Yan Zhenge4404d62008-12-12 10:03:26 -0500801 WARN_ON(fs_devices->open_devices);
802 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500803 fs_devices->opened = 0;
804 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500805
Chris Mason8a4b83c2008-03-24 15:02:07 -0400806 return 0;
807}
808
Yan Zheng2b820322008-11-17 21:11:30 -0500809int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
810{
Yan Zhenge4404d62008-12-12 10:03:26 -0500811 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500812 int ret;
813
814 mutex_lock(&uuid_mutex);
815 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500816 if (!fs_devices->opened) {
817 seed_devices = fs_devices->seed;
818 fs_devices->seed = NULL;
819 }
Yan Zheng2b820322008-11-17 21:11:30 -0500820 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500821
822 while (seed_devices) {
823 fs_devices = seed_devices;
824 seed_devices = fs_devices->seed;
825 __btrfs_close_devices(fs_devices);
826 free_fs_devices(fs_devices);
827 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000828 /*
829 * Wait for rcu kworkers under __btrfs_close_devices
830 * to finish all blkdev_puts so device is really
831 * free when umount is done.
832 */
833 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500834 return ret;
835}
836
Yan Zhenge4404d62008-12-12 10:03:26 -0500837static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
838 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400839{
Josef Bacikd5e20032011-08-04 14:52:27 +0000840 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400841 struct block_device *bdev;
842 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400843 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800844 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400845 struct buffer_head *bh;
846 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400847 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500848 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400849 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400850
Tejun Heod4d77622010-11-13 11:55:18 +0100851 flags |= FMODE_EXCL;
852
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500853 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400854 if (device->bdev)
855 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400856 if (!device->name)
857 continue;
858
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000859 /* Just open everything we can; ignore failures here */
860 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
861 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100862 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400863
864 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000865 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400866 if (devid != device->devid)
867 goto error_brelse;
868
Yan Zheng2b820322008-11-17 21:11:30 -0500869 if (memcmp(device->uuid, disk_super->dev_item.uuid,
870 BTRFS_UUID_SIZE))
871 goto error_brelse;
872
873 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800874 if (!latest_dev ||
875 device->generation > latest_dev->generation)
876 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400877
Yan Zheng2b820322008-11-17 21:11:30 -0500878 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
879 device->writeable = 0;
880 } else {
881 device->writeable = !bdev_read_only(bdev);
882 seeding = 0;
883 }
884
Josef Bacikd5e20032011-08-04 14:52:27 +0000885 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +0800886 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +0000887 device->can_discard = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +0000888
Chris Mason8a4b83c2008-03-24 15:02:07 -0400889 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400890 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500891 device->mode = flags;
892
Chris Masonc2898112009-06-10 09:51:32 -0400893 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
894 fs_devices->rotating = 1;
895
Chris Masona0af4692008-05-13 16:03:06 -0400896 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300897 if (device->writeable &&
898 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500899 fs_devices->rw_devices++;
900 list_add(&device->dev_alloc_list,
901 &fs_devices->alloc_list);
902 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000903 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400904 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400905
Chris Masona0af4692008-05-13 16:03:06 -0400906error_brelse:
907 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100908 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400909 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400910 }
Chris Masona0af4692008-05-13 16:03:06 -0400911 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300912 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400913 goto out;
914 }
Yan Zheng2b820322008-11-17 21:11:30 -0500915 fs_devices->seeding = seeding;
916 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800917 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500918 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400919out:
Yan Zheng2b820322008-11-17 21:11:30 -0500920 return ret;
921}
922
923int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500924 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500925{
926 int ret;
927
928 mutex_lock(&uuid_mutex);
929 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500930 fs_devices->opened++;
931 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500932 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500933 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500934 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400935 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400936 return ret;
937}
938
David Sterba6f60cbd2013-02-15 11:31:02 -0700939/*
940 * Look for a btrfs signature on a device. This may be called out of the mount path
941 * and we are not allowed to call set_blocksize during the scan. The superblock
942 * is read via pagecache
943 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500944int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400945 struct btrfs_fs_devices **fs_devices_ret)
946{
947 struct btrfs_super_block *disk_super;
948 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700949 struct page *page;
950 void *p;
951 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400952 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400953 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400954 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700955 u64 bytenr;
956 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400957
David Sterba6f60cbd2013-02-15 11:31:02 -0700958 /*
959 * we would like to check all the supers, but that would make
960 * a btrfs mount succeed after a mkfs from a different FS.
961 * So, we need to add a special mount option to scan for
962 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
963 */
964 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100965 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500966 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700967
968 bdev = blkdev_get_by_path(path, flags, holder);
969
970 if (IS_ERR(bdev)) {
971 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100972 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700973 }
974
975 /* make sure our super fits in the device */
976 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
977 goto error_bdev_put;
978
979 /* make sure our super fits in the page */
980 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
981 goto error_bdev_put;
982
983 /* make sure our super doesn't straddle pages on disk */
984 index = bytenr >> PAGE_CACHE_SHIFT;
985 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
986 goto error_bdev_put;
987
988 /* pull in the page with our super */
989 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
990 index, GFP_NOFS);
991
992 if (IS_ERR_OR_NULL(page))
993 goto error_bdev_put;
994
995 p = kmap(page);
996
997 /* align our pointer to the offset of the super block */
998 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
999
1000 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +08001001 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -07001002 goto error_unmap;
1003
Xiao Guangronga3438322010-01-06 11:48:18 +00001004 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -04001005 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -04001006 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001007
Chris Mason8a4b83c2008-03-24 15:02:07 -04001008 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +01001009 if (ret > 0) {
1010 if (disk_super->label[0]) {
1011 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
1012 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
1013 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
1014 } else {
1015 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
1016 }
1017
1018 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
1019 ret = 0;
1020 }
Josef Bacik02db0842012-06-21 16:03:58 -04001021 if (!ret && fs_devices_ret)
1022 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001023
1024error_unmap:
1025 kunmap(page);
1026 page_cache_release(page);
1027
1028error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001029 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001030error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001031 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001032 return ret;
1033}
Chris Mason0b86a832008-03-24 15:01:56 -04001034
Miao Xie6d07bce2011-01-05 10:07:31 +00001035/* helper to account the used device space in the range */
1036int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1037 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001038{
1039 struct btrfs_key key;
1040 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001041 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001042 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001043 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001044 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001045 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001046 struct extent_buffer *l;
1047
Miao Xie6d07bce2011-01-05 10:07:31 +00001048 *length = 0;
1049
Stefan Behrens63a212a2012-11-05 18:29:28 +01001050 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +00001051 return 0;
1052
Yan Zheng2b820322008-11-17 21:11:30 -05001053 path = btrfs_alloc_path();
1054 if (!path)
1055 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001056 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001057
Chris Mason0b86a832008-03-24 15:01:56 -04001058 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001059 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001060 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001061
1062 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001063 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001064 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001065 if (ret > 0) {
1066 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1067 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001068 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001069 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001070
Chris Mason0b86a832008-03-24 15:01:56 -04001071 while (1) {
1072 l = path->nodes[0];
1073 slot = path->slots[0];
1074 if (slot >= btrfs_header_nritems(l)) {
1075 ret = btrfs_next_leaf(root, path);
1076 if (ret == 0)
1077 continue;
1078 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001079 goto out;
1080
1081 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001082 }
1083 btrfs_item_key_to_cpu(l, &key, slot);
1084
1085 if (key.objectid < device->devid)
1086 goto next;
1087
1088 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001089 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001090
David Sterba962a2982014-06-04 18:41:45 +02001091 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001092 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001093
Chris Mason0b86a832008-03-24 15:01:56 -04001094 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001095 extent_end = key.offset + btrfs_dev_extent_length(l,
1096 dev_extent);
1097 if (key.offset <= start && extent_end > end) {
1098 *length = end - start + 1;
1099 break;
1100 } else if (key.offset <= start && extent_end > start)
1101 *length += extent_end - start;
1102 else if (key.offset > start && extent_end <= end)
1103 *length += extent_end - key.offset;
1104 else if (key.offset > start && key.offset <= end) {
1105 *length += end - key.offset + 1;
1106 break;
1107 } else if (key.offset > end)
1108 break;
1109
1110next:
1111 path->slots[0]++;
1112 }
1113 ret = 0;
1114out:
1115 btrfs_free_path(path);
1116 return ret;
1117}
1118
Josef Bacik6df9a952013-06-27 13:22:46 -04001119static int contains_pending_extent(struct btrfs_trans_handle *trans,
1120 struct btrfs_device *device,
1121 u64 *start, u64 len)
1122{
1123 struct extent_map *em;
Filipe Manana04216822014-11-27 21:14:15 +00001124 struct list_head *search_list = &trans->transaction->pending_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001125 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001126 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001127
Filipe Manana04216822014-11-27 21:14:15 +00001128again:
1129 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001130 struct map_lookup *map;
1131 int i;
1132
1133 map = (struct map_lookup *)em->bdev;
1134 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001135 u64 end;
1136
Josef Bacik6df9a952013-06-27 13:22:46 -04001137 if (map->stripes[i].dev != device)
1138 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001139 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001140 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001141 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001142 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001143 /*
1144 * Make sure that while processing the pinned list we do
1145 * not override our *start with a lower value, because
1146 * we can have pinned chunks that fall within this
1147 * device hole and that have lower physical addresses
1148 * than the pending chunks we processed before. If we
1149 * do not take this special care we can end up getting
1150 * 2 pending chunks that start at the same physical
1151 * device offsets because the end offset of a pinned
1152 * chunk can be equal to the start offset of some
1153 * pending chunk.
1154 */
1155 end = map->stripes[i].physical + em->orig_block_len;
1156 if (end > *start) {
1157 *start = end;
1158 ret = 1;
1159 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001160 }
1161 }
Filipe Manana04216822014-11-27 21:14:15 +00001162 if (search_list == &trans->transaction->pending_chunks) {
1163 search_list = &trans->root->fs_info->pinned_chunks;
1164 goto again;
1165 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001166
1167 return ret;
1168}
1169
1170
Chris Mason0b86a832008-03-24 15:01:56 -04001171/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001172 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001173 * @device: the device which we search the free space in
1174 * @num_bytes: the size of the free space that we need
1175 * @start: store the start of the free space.
1176 * @len: the size of the free space. that we find, or the size of the max
1177 * free space if we don't find suitable free space
1178 *
Chris Mason0b86a832008-03-24 15:01:56 -04001179 * this uses a pretty simple search, the expectation is that it is
1180 * called very infrequently and that a given device has a small number
1181 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001182 *
1183 * @start is used to store the start of the free space if we find. But if we
1184 * don't find suitable free space, it will be used to store the start position
1185 * of the max free space.
1186 *
1187 * @len is used to store the size of the free space that we find.
1188 * But if we don't find suitable free space, it is used to store the size of
1189 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001190 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001191int find_free_dev_extent(struct btrfs_trans_handle *trans,
1192 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001193 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001194{
1195 struct btrfs_key key;
1196 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001197 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001198 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001199 u64 hole_size;
1200 u64 max_hole_start;
1201 u64 max_hole_size;
1202 u64 extent_end;
1203 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001204 u64 search_end = device->total_bytes;
1205 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001206 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001207 struct extent_buffer *l;
1208
Chris Mason0b86a832008-03-24 15:01:56 -04001209 /* FIXME use last free of some kind */
1210
1211 /* we don't want to overwrite the superblock on the drive,
1212 * so we make sure to start at an offset of at least 1MB
1213 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001214 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001215
Josef Bacik6df9a952013-06-27 13:22:46 -04001216 path = btrfs_alloc_path();
1217 if (!path)
1218 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001219
Miao Xie7bfc8372011-01-05 10:07:26 +00001220 max_hole_start = search_start;
1221 max_hole_size = 0;
1222
Zhao Leif2ab7612015-02-16 18:52:17 +08001223again:
Stefan Behrens63a212a2012-11-05 18:29:28 +01001224 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001225 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001226 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001227 }
1228
Miao Xie7bfc8372011-01-05 10:07:26 +00001229 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001230 path->search_commit_root = 1;
1231 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001232
Chris Mason0b86a832008-03-24 15:01:56 -04001233 key.objectid = device->devid;
1234 key.offset = search_start;
1235 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001236
Li Zefan125ccb02011-12-08 15:07:24 +08001237 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001238 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001239 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001240 if (ret > 0) {
1241 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1242 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001243 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001244 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001245
Chris Mason0b86a832008-03-24 15:01:56 -04001246 while (1) {
1247 l = path->nodes[0];
1248 slot = path->slots[0];
1249 if (slot >= btrfs_header_nritems(l)) {
1250 ret = btrfs_next_leaf(root, path);
1251 if (ret == 0)
1252 continue;
1253 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001254 goto out;
1255
1256 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001257 }
1258 btrfs_item_key_to_cpu(l, &key, slot);
1259
1260 if (key.objectid < device->devid)
1261 goto next;
1262
1263 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001264 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001265
David Sterba962a2982014-06-04 18:41:45 +02001266 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001267 goto next;
1268
Miao Xie7bfc8372011-01-05 10:07:26 +00001269 if (key.offset > search_start) {
1270 hole_size = key.offset - search_start;
1271
Josef Bacik6df9a952013-06-27 13:22:46 -04001272 /*
1273 * Have to check before we set max_hole_start, otherwise
1274 * we could end up sending back this offset anyway.
1275 */
1276 if (contains_pending_extent(trans, device,
1277 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001278 hole_size)) {
1279 if (key.offset >= search_start) {
1280 hole_size = key.offset - search_start;
1281 } else {
1282 WARN_ON_ONCE(1);
1283 hole_size = 0;
1284 }
1285 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001286
Miao Xie7bfc8372011-01-05 10:07:26 +00001287 if (hole_size > max_hole_size) {
1288 max_hole_start = search_start;
1289 max_hole_size = hole_size;
1290 }
1291
1292 /*
1293 * If this free space is greater than which we need,
1294 * it must be the max free space that we have found
1295 * until now, so max_hole_start must point to the start
1296 * of this free space and the length of this free space
1297 * is stored in max_hole_size. Thus, we return
1298 * max_hole_start and max_hole_size and go back to the
1299 * caller.
1300 */
1301 if (hole_size >= num_bytes) {
1302 ret = 0;
1303 goto out;
1304 }
1305 }
1306
Chris Mason0b86a832008-03-24 15:01:56 -04001307 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001308 extent_end = key.offset + btrfs_dev_extent_length(l,
1309 dev_extent);
1310 if (extent_end > search_start)
1311 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001312next:
1313 path->slots[0]++;
1314 cond_resched();
1315 }
Chris Mason0b86a832008-03-24 15:01:56 -04001316
liubo38c01b92011-08-02 02:39:03 +00001317 /*
1318 * At this point, search_start should be the end of
1319 * allocated dev extents, and when shrinking the device,
1320 * search_end may be smaller than search_start.
1321 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001322 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001323 hole_size = search_end - search_start;
1324
Zhao Leif2ab7612015-02-16 18:52:17 +08001325 if (contains_pending_extent(trans, device, &search_start,
1326 hole_size)) {
1327 btrfs_release_path(path);
1328 goto again;
1329 }
Chris Mason0b86a832008-03-24 15:01:56 -04001330
Zhao Leif2ab7612015-02-16 18:52:17 +08001331 if (hole_size > max_hole_size) {
1332 max_hole_start = search_start;
1333 max_hole_size = hole_size;
1334 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001335 }
1336
Miao Xie7bfc8372011-01-05 10:07:26 +00001337 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001338 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001339 ret = -ENOSPC;
1340 else
1341 ret = 0;
1342
1343out:
Yan Zheng2b820322008-11-17 21:11:30 -05001344 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001345 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001346 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001347 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001348 return ret;
1349}
1350
Christoph Hellwigb2950862008-12-02 09:54:17 -05001351static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001352 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001353 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001354{
1355 int ret;
1356 struct btrfs_path *path;
1357 struct btrfs_root *root = device->dev_root;
1358 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001359 struct btrfs_key found_key;
1360 struct extent_buffer *leaf = NULL;
1361 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001362
1363 path = btrfs_alloc_path();
1364 if (!path)
1365 return -ENOMEM;
1366
1367 key.objectid = device->devid;
1368 key.offset = start;
1369 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001370again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001371 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001372 if (ret > 0) {
1373 ret = btrfs_previous_item(root, path, key.objectid,
1374 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001375 if (ret)
1376 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001377 leaf = path->nodes[0];
1378 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1379 extent = btrfs_item_ptr(leaf, path->slots[0],
1380 struct btrfs_dev_extent);
1381 BUG_ON(found_key.offset > start || found_key.offset +
1382 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001383 key = found_key;
1384 btrfs_release_path(path);
1385 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001386 } else if (ret == 0) {
1387 leaf = path->nodes[0];
1388 extent = btrfs_item_ptr(leaf, path->slots[0],
1389 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001390 } else {
1391 btrfs_error(root->fs_info, ret, "Slot search failed");
1392 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001393 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001394
Miao Xie2196d6e2014-09-03 21:35:41 +08001395 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1396
Chris Mason8f18cf12008-04-25 16:53:30 -04001397 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001398 if (ret) {
1399 btrfs_error(root->fs_info, ret,
1400 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001401 } else {
1402 trans->transaction->have_free_bgs = 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001403 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001404out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001405 btrfs_free_path(path);
1406 return ret;
1407}
1408
Eric Sandeen48a3b632013-04-25 20:41:01 +00001409static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1410 struct btrfs_device *device,
1411 u64 chunk_tree, u64 chunk_objectid,
1412 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001413{
1414 int ret;
1415 struct btrfs_path *path;
1416 struct btrfs_root *root = device->dev_root;
1417 struct btrfs_dev_extent *extent;
1418 struct extent_buffer *leaf;
1419 struct btrfs_key key;
1420
Chris Masondfe25022008-05-13 13:46:40 -04001421 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001422 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001423 path = btrfs_alloc_path();
1424 if (!path)
1425 return -ENOMEM;
1426
Chris Mason0b86a832008-03-24 15:01:56 -04001427 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001428 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001429 key.type = BTRFS_DEV_EXTENT_KEY;
1430 ret = btrfs_insert_empty_item(trans, root, path, &key,
1431 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001432 if (ret)
1433 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001434
1435 leaf = path->nodes[0];
1436 extent = btrfs_item_ptr(leaf, path->slots[0],
1437 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001438 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1439 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1440 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1441
1442 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001443 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001444
Chris Mason0b86a832008-03-24 15:01:56 -04001445 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1446 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001447out:
Chris Mason0b86a832008-03-24 15:01:56 -04001448 btrfs_free_path(path);
1449 return ret;
1450}
1451
Josef Bacik6df9a952013-06-27 13:22:46 -04001452static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001453{
Josef Bacik6df9a952013-06-27 13:22:46 -04001454 struct extent_map_tree *em_tree;
1455 struct extent_map *em;
1456 struct rb_node *n;
1457 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001458
Josef Bacik6df9a952013-06-27 13:22:46 -04001459 em_tree = &fs_info->mapping_tree.map_tree;
1460 read_lock(&em_tree->lock);
1461 n = rb_last(&em_tree->map);
1462 if (n) {
1463 em = rb_entry(n, struct extent_map, rb_node);
1464 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001465 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001466 read_unlock(&em_tree->lock);
1467
Chris Mason0b86a832008-03-24 15:01:56 -04001468 return ret;
1469}
1470
Ilya Dryomov53f10652013-08-12 14:33:01 +03001471static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1472 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001473{
1474 int ret;
1475 struct btrfs_key key;
1476 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001477 struct btrfs_path *path;
1478
Yan Zheng2b820322008-11-17 21:11:30 -05001479 path = btrfs_alloc_path();
1480 if (!path)
1481 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001482
1483 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1484 key.type = BTRFS_DEV_ITEM_KEY;
1485 key.offset = (u64)-1;
1486
Ilya Dryomov53f10652013-08-12 14:33:01 +03001487 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001488 if (ret < 0)
1489 goto error;
1490
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001491 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001492
Ilya Dryomov53f10652013-08-12 14:33:01 +03001493 ret = btrfs_previous_item(fs_info->chunk_root, path,
1494 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001495 BTRFS_DEV_ITEM_KEY);
1496 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001497 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001498 } else {
1499 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1500 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001501 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001502 }
1503 ret = 0;
1504error:
Yan Zheng2b820322008-11-17 21:11:30 -05001505 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001506 return ret;
1507}
1508
1509/*
1510 * the device information is stored in the chunk root
1511 * the btrfs_device struct should be fully filled in
1512 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001513static int btrfs_add_device(struct btrfs_trans_handle *trans,
1514 struct btrfs_root *root,
1515 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001516{
1517 int ret;
1518 struct btrfs_path *path;
1519 struct btrfs_dev_item *dev_item;
1520 struct extent_buffer *leaf;
1521 struct btrfs_key key;
1522 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001523
1524 root = root->fs_info->chunk_root;
1525
1526 path = btrfs_alloc_path();
1527 if (!path)
1528 return -ENOMEM;
1529
Chris Mason0b86a832008-03-24 15:01:56 -04001530 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1531 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001532 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001533
1534 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001535 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001536 if (ret)
1537 goto out;
1538
1539 leaf = path->nodes[0];
1540 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1541
1542 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001543 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001544 btrfs_set_device_type(leaf, dev_item, device->type);
1545 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1546 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1547 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001548 btrfs_set_device_total_bytes(leaf, dev_item,
1549 btrfs_device_get_disk_total_bytes(device));
1550 btrfs_set_device_bytes_used(leaf, dev_item,
1551 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001552 btrfs_set_device_group(leaf, dev_item, 0);
1553 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1554 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001555 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001556
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001557 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001558 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001559 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001560 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001561 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001562
Yan Zheng2b820322008-11-17 21:11:30 -05001563 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001564out:
1565 btrfs_free_path(path);
1566 return ret;
1567}
Chris Mason8f18cf12008-04-25 16:53:30 -04001568
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001569/*
1570 * Function to update ctime/mtime for a given device path.
1571 * Mainly used for ctime/mtime based probe like libblkid.
1572 */
1573static void update_dev_time(char *path_name)
1574{
1575 struct file *filp;
1576
1577 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001578 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001579 return;
1580 file_update_time(filp);
1581 filp_close(filp, NULL);
1582 return;
1583}
1584
Chris Masona061fc82008-05-07 11:43:44 -04001585static int btrfs_rm_dev_item(struct btrfs_root *root,
1586 struct btrfs_device *device)
1587{
1588 int ret;
1589 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001590 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001591 struct btrfs_trans_handle *trans;
1592
1593 root = root->fs_info->chunk_root;
1594
1595 path = btrfs_alloc_path();
1596 if (!path)
1597 return -ENOMEM;
1598
Yan, Zhenga22285a2010-05-16 10:48:46 -04001599 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001600 if (IS_ERR(trans)) {
1601 btrfs_free_path(path);
1602 return PTR_ERR(trans);
1603 }
Chris Masona061fc82008-05-07 11:43:44 -04001604 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1605 key.type = BTRFS_DEV_ITEM_KEY;
1606 key.offset = device->devid;
1607
1608 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1609 if (ret < 0)
1610 goto out;
1611
1612 if (ret > 0) {
1613 ret = -ENOENT;
1614 goto out;
1615 }
1616
1617 ret = btrfs_del_item(trans, root, path);
1618 if (ret)
1619 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001620out:
1621 btrfs_free_path(path);
1622 btrfs_commit_transaction(trans, root);
1623 return ret;
1624}
1625
1626int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1627{
1628 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001629 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001630 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001631 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001632 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001633 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001634 u64 all_avail;
1635 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001636 u64 num_devices;
1637 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001638 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001639 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001640 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001641
Chris Masona061fc82008-05-07 11:43:44 -04001642 mutex_lock(&uuid_mutex);
1643
Miao Xiede98ced2013-01-29 10:13:12 +00001644 do {
1645 seq = read_seqbegin(&root->fs_info->profiles_lock);
1646
1647 all_avail = root->fs_info->avail_data_alloc_bits |
1648 root->fs_info->avail_system_alloc_bits |
1649 root->fs_info->avail_metadata_alloc_bits;
1650 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001651
Stefan Behrens8dabb742012-11-06 13:15:27 +01001652 num_devices = root->fs_info->fs_devices->num_devices;
1653 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1654 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1655 WARN_ON(num_devices < 1);
1656 num_devices--;
1657 }
1658 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1659
1660 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001661 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001662 goto out;
1663 }
1664
Stefan Behrens8dabb742012-11-06 13:15:27 +01001665 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001666 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001667 goto out;
1668 }
1669
David Woodhouse53b381b2013-01-29 18:40:14 -05001670 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1671 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001672 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001673 goto out;
1674 }
1675 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1676 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001677 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001678 goto out;
1679 }
1680
Chris Masondfe25022008-05-13 13:46:40 -04001681 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001682 struct list_head *devices;
1683 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001684
Chris Masondfe25022008-05-13 13:46:40 -04001685 device = NULL;
1686 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001687 /*
1688 * It is safe to read the devices since the volume_mutex
1689 * is held.
1690 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001691 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001692 if (tmp->in_fs_metadata &&
1693 !tmp->is_tgtdev_for_dev_replace &&
1694 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001695 device = tmp;
1696 break;
1697 }
1698 }
1699 bdev = NULL;
1700 bh = NULL;
1701 disk_super = NULL;
1702 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001703 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001704 goto out;
1705 }
Chris Masondfe25022008-05-13 13:46:40 -04001706 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001707 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001708 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001709 root->fs_info->bdev_holder, 0,
1710 &bdev, &bh);
1711 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001712 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001713 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001714 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001715 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001716 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001717 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001718 if (!device) {
1719 ret = -ENOENT;
1720 goto error_brelse;
1721 }
Chris Masondfe25022008-05-13 13:46:40 -04001722 }
Yan Zheng2b820322008-11-17 21:11:30 -05001723
Stefan Behrens63a212a2012-11-05 18:29:28 +01001724 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001725 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001726 goto error_brelse;
1727 }
1728
Yan Zheng2b820322008-11-17 21:11:30 -05001729 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001730 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001731 goto error_brelse;
1732 }
1733
1734 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001735 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001736 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001737 device->fs_devices->rw_devices--;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001738 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001739 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001740 }
Chris Masona061fc82008-05-07 11:43:44 -04001741
Carey Underwoodd7901552013-03-04 16:37:06 -06001742 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001743 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001744 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001745 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001746 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001747
Stefan Behrens63a212a2012-11-05 18:29:28 +01001748 /*
1749 * TODO: the superblock still includes this device in its num_devices
1750 * counter although write_all_supers() is not locked out. This
1751 * could give a filesystem state which requires a degraded mount.
1752 */
Chris Masona061fc82008-05-07 11:43:44 -04001753 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1754 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001755 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001756
Yan Zheng2b820322008-11-17 21:11:30 -05001757 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001758 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001759
1760 /*
1761 * the device list mutex makes sure that we don't change
1762 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001763 * the device supers. Whoever is writing all supers, should
1764 * lock the device list mutex before getting the number of
1765 * devices in the super block (super_copy). Conversely,
1766 * whoever updates the number of devices in the super block
1767 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001768 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001769
1770 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001771 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001772 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001773
Yan Zhenge4404d62008-12-12 10:03:26 -05001774 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001775 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001776
Chris Masoncd02dca2010-12-13 14:56:23 -05001777 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001778 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001779
Yan Zheng2b820322008-11-17 21:11:30 -05001780 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1781 struct btrfs_device, dev_list);
1782 if (device->bdev == root->fs_info->sb->s_bdev)
1783 root->fs_info->sb->s_bdev = next_device->bdev;
1784 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1785 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1786
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001787 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001788 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001789 /* remove sysfs entry */
Anand Jain6c14a162015-03-10 06:38:34 +08001790 btrfs_kobj_rm_device(root->fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001791 }
Anand Jain99994cd2014-06-03 11:36:00 +08001792
Xiao Guangrong1f781602011-04-20 10:09:16 +00001793 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001794
David Sterba6c417612011-04-13 15:41:04 +02001795 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1796 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001797 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001798
Xiao Guangrong1f781602011-04-20 10:09:16 +00001799 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001800 struct btrfs_fs_devices *fs_devices;
1801 fs_devices = root->fs_info->fs_devices;
1802 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001803 if (fs_devices->seed == cur_devices) {
1804 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001805 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001806 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001807 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001808 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001809 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001810 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001811 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001812 }
1813
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001814 root->fs_info->num_tolerated_disk_barrier_failures =
1815 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1816
Yan Zheng2b820322008-11-17 21:11:30 -05001817 /*
1818 * at this point, the device is zero sized. We want to
1819 * remove it from the devices list and zero out the old super
1820 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001821 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001822 u64 bytenr;
1823 int i;
1824
Chris Masondfe25022008-05-13 13:46:40 -04001825 /* make sure this device isn't detected as part of
1826 * the FS anymore
1827 */
1828 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1829 set_buffer_dirty(bh);
1830 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001831
1832 /* clear the mirror copies of super block on the disk
1833 * being removed, 0th copy is been taken care above and
1834 * the below would take of the rest
1835 */
1836 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1837 bytenr = btrfs_sb_offset(i);
1838 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1839 i_size_read(bdev->bd_inode))
1840 break;
1841
1842 brelse(bh);
1843 bh = __bread(bdev, bytenr / 4096,
1844 BTRFS_SUPER_INFO_SIZE);
1845 if (!bh)
1846 continue;
1847
1848 disk_super = (struct btrfs_super_block *)bh->b_data;
1849
1850 if (btrfs_super_bytenr(disk_super) != bytenr ||
1851 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1852 continue;
1853 }
1854 memset(&disk_super->magic, 0,
1855 sizeof(disk_super->magic));
1856 set_buffer_dirty(bh);
1857 sync_dirty_buffer(bh);
1858 }
Chris Masondfe25022008-05-13 13:46:40 -04001859 }
Chris Masona061fc82008-05-07 11:43:44 -04001860
Chris Masona061fc82008-05-07 11:43:44 -04001861 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001862
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001863 if (bdev) {
1864 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001865 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001866
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001867 /* Update ctime/mtime for device path for libblkid */
1868 update_dev_time(device_path);
1869 }
1870
Chris Masona061fc82008-05-07 11:43:44 -04001871error_brelse:
1872 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001873 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001874 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001875out:
1876 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001877 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001878error_undo:
1879 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001880 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001881 list_add(&device->dev_alloc_list,
1882 &root->fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001883 device->fs_devices->rw_devices++;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001884 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001885 }
1886 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001887}
1888
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001889void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1890 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01001891{
Anand Jaind51908c2014-08-13 14:24:19 +08001892 struct btrfs_fs_devices *fs_devices;
1893
Stefan Behrense93c89c2012-11-05 17:33:06 +01001894 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001895
Anand Jain25e8e912014-08-20 10:56:56 +08001896 /*
1897 * in case of fs with no seed, srcdev->fs_devices will point
1898 * to fs_devices of fs_info. However when the dev being replaced is
1899 * a seed dev it will point to the seed's local fs_devices. In short
1900 * srcdev will have its correct fs_devices in both the cases.
1901 */
1902 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001903
Stefan Behrense93c89c2012-11-05 17:33:06 +01001904 list_del_rcu(&srcdev->dev_list);
1905 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001906 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08001907 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08001908 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001909
Miao Xie82372bc2014-09-03 21:35:44 +08001910 if (srcdev->writeable) {
1911 fs_devices->rw_devices--;
1912 /* zero out the old super if it is writable */
1913 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03001914 }
1915
Miao Xie82372bc2014-09-03 21:35:44 +08001916 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08001917 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001918}
1919
1920void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
1921 struct btrfs_device *srcdev)
1922{
1923 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001924
Stefan Behrense93c89c2012-11-05 17:33:06 +01001925 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001926
1927 /*
1928 * unless fs_devices is seed fs, num_devices shouldn't go
1929 * zero
1930 */
1931 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1932
1933 /* if this is no devs we rather delete the fs_devices */
1934 if (!fs_devices->num_devices) {
1935 struct btrfs_fs_devices *tmp_fs_devices;
1936
1937 tmp_fs_devices = fs_info->fs_devices;
1938 while (tmp_fs_devices) {
1939 if (tmp_fs_devices->seed == fs_devices) {
1940 tmp_fs_devices->seed = fs_devices->seed;
1941 break;
1942 }
1943 tmp_fs_devices = tmp_fs_devices->seed;
1944 }
1945 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08001946 __btrfs_close_devices(fs_devices);
1947 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001948 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01001949}
1950
1951void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1952 struct btrfs_device *tgtdev)
1953{
1954 struct btrfs_device *next_device;
1955
Miao Xie67a2c452014-09-03 21:35:43 +08001956 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001957 WARN_ON(!tgtdev);
1958 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08001959
1960 btrfs_kobj_rm_device(fs_info->fs_devices, tgtdev);
1961
Stefan Behrense93c89c2012-11-05 17:33:06 +01001962 if (tgtdev->bdev) {
1963 btrfs_scratch_superblock(tgtdev);
1964 fs_info->fs_devices->open_devices--;
1965 }
1966 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001967
1968 next_device = list_entry(fs_info->fs_devices->devices.next,
1969 struct btrfs_device, dev_list);
1970 if (tgtdev->bdev == fs_info->sb->s_bdev)
1971 fs_info->sb->s_bdev = next_device->bdev;
1972 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1973 fs_info->fs_devices->latest_bdev = next_device->bdev;
1974 list_del_rcu(&tgtdev->dev_list);
1975
1976 call_rcu(&tgtdev->rcu, free_device);
1977
1978 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08001979 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001980}
1981
Eric Sandeen48a3b632013-04-25 20:41:01 +00001982static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1983 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001984{
1985 int ret = 0;
1986 struct btrfs_super_block *disk_super;
1987 u64 devid;
1988 u8 *dev_uuid;
1989 struct block_device *bdev;
1990 struct buffer_head *bh;
1991
1992 *device = NULL;
1993 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1994 root->fs_info->bdev_holder, 0, &bdev, &bh);
1995 if (ret)
1996 return ret;
1997 disk_super = (struct btrfs_super_block *)bh->b_data;
1998 devid = btrfs_stack_device_id(&disk_super->dev_item);
1999 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002000 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002001 disk_super->fsid);
2002 brelse(bh);
2003 if (!*device)
2004 ret = -ENOENT;
2005 blkdev_put(bdev, FMODE_READ);
2006 return ret;
2007}
2008
2009int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
2010 char *device_path,
2011 struct btrfs_device **device)
2012{
2013 *device = NULL;
2014 if (strcmp(device_path, "missing") == 0) {
2015 struct list_head *devices;
2016 struct btrfs_device *tmp;
2017
2018 devices = &root->fs_info->fs_devices->devices;
2019 /*
2020 * It is safe to read the devices since the volume_mutex
2021 * is held by the caller.
2022 */
2023 list_for_each_entry(tmp, devices, dev_list) {
2024 if (tmp->in_fs_metadata && !tmp->bdev) {
2025 *device = tmp;
2026 break;
2027 }
2028 }
2029
2030 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002031 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002032 return -ENOENT;
2033 }
2034
2035 return 0;
2036 } else {
2037 return btrfs_find_device_by_path(root, device_path, device);
2038 }
2039}
2040
Yan Zheng2b820322008-11-17 21:11:30 -05002041/*
2042 * does all the dirty work required for changing file system's UUID.
2043 */
Li Zefan125ccb02011-12-08 15:07:24 +08002044static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05002045{
2046 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2047 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002048 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02002049 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002050 struct btrfs_device *device;
2051 u64 super_flags;
2052
2053 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05002054 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002055 return -EINVAL;
2056
Ilya Dryomov2208a372013-08-12 14:33:03 +03002057 seed_devices = __alloc_fs_devices();
2058 if (IS_ERR(seed_devices))
2059 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002060
Yan Zhenge4404d62008-12-12 10:03:26 -05002061 old_devices = clone_fs_devices(fs_devices);
2062 if (IS_ERR(old_devices)) {
2063 kfree(seed_devices);
2064 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002065 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002066
Yan Zheng2b820322008-11-17 21:11:30 -05002067 list_add(&old_devices->list, &fs_uuids);
2068
Yan Zhenge4404d62008-12-12 10:03:26 -05002069 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2070 seed_devices->opened = 1;
2071 INIT_LIST_HEAD(&seed_devices->devices);
2072 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002073 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002074
2075 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002076 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2077 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002078 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002079 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002080
2081 lock_chunks(root);
2082 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2083 unlock_chunks(root);
Yan Zhenge4404d62008-12-12 10:03:26 -05002084
Yan Zheng2b820322008-11-17 21:11:30 -05002085 fs_devices->seeding = 0;
2086 fs_devices->num_devices = 0;
2087 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002088 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002089 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002090 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002091
2092 generate_random_uuid(fs_devices->fsid);
2093 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2094 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002095 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2096
Yan Zheng2b820322008-11-17 21:11:30 -05002097 super_flags = btrfs_super_flags(disk_super) &
2098 ~BTRFS_SUPER_FLAG_SEEDING;
2099 btrfs_set_super_flags(disk_super, super_flags);
2100
2101 return 0;
2102}
2103
2104/*
2105 * strore the expected generation for seed devices in device items.
2106 */
2107static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2108 struct btrfs_root *root)
2109{
2110 struct btrfs_path *path;
2111 struct extent_buffer *leaf;
2112 struct btrfs_dev_item *dev_item;
2113 struct btrfs_device *device;
2114 struct btrfs_key key;
2115 u8 fs_uuid[BTRFS_UUID_SIZE];
2116 u8 dev_uuid[BTRFS_UUID_SIZE];
2117 u64 devid;
2118 int ret;
2119
2120 path = btrfs_alloc_path();
2121 if (!path)
2122 return -ENOMEM;
2123
2124 root = root->fs_info->chunk_root;
2125 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2126 key.offset = 0;
2127 key.type = BTRFS_DEV_ITEM_KEY;
2128
2129 while (1) {
2130 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2131 if (ret < 0)
2132 goto error;
2133
2134 leaf = path->nodes[0];
2135next_slot:
2136 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2137 ret = btrfs_next_leaf(root, path);
2138 if (ret > 0)
2139 break;
2140 if (ret < 0)
2141 goto error;
2142 leaf = path->nodes[0];
2143 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002144 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002145 continue;
2146 }
2147
2148 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2149 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2150 key.type != BTRFS_DEV_ITEM_KEY)
2151 break;
2152
2153 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2154 struct btrfs_dev_item);
2155 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002156 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002157 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002158 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002159 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002160 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2161 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002162 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002163
2164 if (device->fs_devices->seeding) {
2165 btrfs_set_device_generation(leaf, dev_item,
2166 device->generation);
2167 btrfs_mark_buffer_dirty(leaf);
2168 }
2169
2170 path->slots[0]++;
2171 goto next_slot;
2172 }
2173 ret = 0;
2174error:
2175 btrfs_free_path(path);
2176 return ret;
2177}
2178
Chris Mason788f20e2008-04-28 15:29:42 -04002179int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2180{
Josef Bacikd5e20032011-08-04 14:52:27 +00002181 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002182 struct btrfs_trans_handle *trans;
2183 struct btrfs_device *device;
2184 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002185 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002186 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002187 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002188 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002189 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002190 int ret = 0;
2191
Yan Zheng2b820322008-11-17 21:11:30 -05002192 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002193 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002194
Li Zefana5d163332011-12-07 20:08:40 -05002195 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002196 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002197 if (IS_ERR(bdev))
2198 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002199
Yan Zheng2b820322008-11-17 21:11:30 -05002200 if (root->fs_info->fs_devices->seeding) {
2201 seeding_dev = 1;
2202 down_write(&sb->s_umount);
2203 mutex_lock(&uuid_mutex);
2204 }
2205
Chris Mason8c8bee12008-09-29 11:19:10 -04002206 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002207
Chris Mason788f20e2008-04-28 15:29:42 -04002208 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002209
2210 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002211 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002212 if (device->bdev == bdev) {
2213 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002214 mutex_unlock(
2215 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002216 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002217 }
2218 }
Liu Bod25628b2012-11-14 14:35:30 +00002219 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002220
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002221 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2222 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002223 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002224 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002225 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002226 }
2227
Josef Bacik606686e2012-06-04 14:03:51 -04002228 name = rcu_string_strdup(device_path, GFP_NOFS);
2229 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002230 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002231 ret = -ENOMEM;
2232 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002233 }
Josef Bacik606686e2012-06-04 14:03:51 -04002234 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002235
Yan, Zhenga22285a2010-05-16 10:48:46 -04002236 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002237 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002238 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002239 kfree(device);
2240 ret = PTR_ERR(trans);
2241 goto error;
2242 }
2243
Josef Bacikd5e20032011-08-04 14:52:27 +00002244 q = bdev_get_queue(bdev);
2245 if (blk_queue_discard(q))
2246 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002247 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002248 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002249 device->io_width = root->sectorsize;
2250 device->io_align = root->sectorsize;
2251 device->sector_size = root->sectorsize;
2252 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002253 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002254 device->commit_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002255 device->dev_root = root->fs_info->dev_root;
2256 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002257 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002258 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002259 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002260 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002261 set_blocksize(device->bdev, 4096);
2262
Yan Zheng2b820322008-11-17 21:11:30 -05002263 if (seeding_dev) {
2264 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002265 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002266 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002267 }
2268
2269 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002270
Chris Masone5e9a522009-06-10 15:17:02 -04002271 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002272 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002273 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002274 list_add(&device->dev_alloc_list,
2275 &root->fs_info->fs_devices->alloc_list);
2276 root->fs_info->fs_devices->num_devices++;
2277 root->fs_info->fs_devices->open_devices++;
2278 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002279 root->fs_info->fs_devices->total_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -05002280 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2281
Josef Bacik2bf64752011-09-26 17:12:22 -04002282 spin_lock(&root->fs_info->free_chunk_lock);
2283 root->fs_info->free_chunk_space += device->total_bytes;
2284 spin_unlock(&root->fs_info->free_chunk_lock);
2285
Chris Masonc2898112009-06-10 09:51:32 -04002286 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2287 root->fs_info->fs_devices->rotating = 1;
2288
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002289 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002290 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002291 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002292
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002293 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002294 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002295 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002296
2297 /* add sysfs device entry */
Anand Jain1ba43812015-03-10 06:38:33 +08002298 btrfs_kobj_add_device(root->fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002299
Miao Xie2196d6e2014-09-03 21:35:41 +08002300 /*
2301 * we've got more storage, clear any full flags on the space
2302 * infos
2303 */
2304 btrfs_clear_space_info_full(root->fs_info);
2305
2306 unlock_chunks(root);
Chris Masone5e9a522009-06-10 15:17:02 -04002307 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002308
Yan Zheng2b820322008-11-17 21:11:30 -05002309 if (seeding_dev) {
Miao Xie2196d6e2014-09-03 21:35:41 +08002310 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05002311 ret = init_first_rw_device(trans, root, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002312 unlock_chunks(root);
David Sterba005d6422012-09-18 07:52:32 -06002313 if (ret) {
2314 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002315 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002316 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002317 }
2318
2319 ret = btrfs_add_device(trans, root, device);
2320 if (ret) {
2321 btrfs_abort_transaction(trans, root, ret);
2322 goto error_trans;
2323 }
2324
2325 if (seeding_dev) {
2326 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2327
Yan Zheng2b820322008-11-17 21:11:30 -05002328 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002329 if (ret) {
2330 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002331 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002332 }
Anand Jainb2373f22014-06-03 11:36:03 +08002333
2334 /* Sprouting would change fsid of the mounted root,
2335 * so rename the fsid on the sysfs
2336 */
2337 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2338 root->fs_info->fsid);
Anand Jain2e7910d2015-03-10 06:38:29 +08002339 if (kobject_rename(&root->fs_info->fs_devices->super_kobj,
2340 fsid_buf))
Anand Jain2421a8c2015-03-10 06:38:40 +08002341 pr_warn("BTRFS: sysfs: failed to create fsid for sprout\n");
Yan Zheng2b820322008-11-17 21:11:30 -05002342 }
2343
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002344 root->fs_info->num_tolerated_disk_barrier_failures =
2345 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002346 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002347
2348 if (seeding_dev) {
2349 mutex_unlock(&uuid_mutex);
2350 up_write(&sb->s_umount);
2351
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002352 if (ret) /* transaction commit */
2353 return ret;
2354
Yan Zheng2b820322008-11-17 21:11:30 -05002355 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002356 if (ret < 0)
2357 btrfs_error(root->fs_info, ret,
2358 "Failed to relocate sys chunks after "
2359 "device initialization. This can be fixed "
2360 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002361 trans = btrfs_attach_transaction(root);
2362 if (IS_ERR(trans)) {
2363 if (PTR_ERR(trans) == -ENOENT)
2364 return 0;
2365 return PTR_ERR(trans);
2366 }
2367 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002368 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002369
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002370 /* Update ctime/mtime for libblkid */
2371 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002372 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002373
2374error_trans:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002375 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002376 rcu_string_free(device->name);
Anand Jain6c14a162015-03-10 06:38:34 +08002377 btrfs_kobj_rm_device(root->fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002378 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002379error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002380 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002381 if (seeding_dev) {
2382 mutex_unlock(&uuid_mutex);
2383 up_write(&sb->s_umount);
2384 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002385 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002386}
2387
Stefan Behrense93c89c2012-11-05 17:33:06 +01002388int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002389 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002390 struct btrfs_device **device_out)
2391{
2392 struct request_queue *q;
2393 struct btrfs_device *device;
2394 struct block_device *bdev;
2395 struct btrfs_fs_info *fs_info = root->fs_info;
2396 struct list_head *devices;
2397 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002398 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002399 int ret = 0;
2400
2401 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002402 if (fs_info->fs_devices->seeding) {
2403 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002404 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002405 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002406
2407 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2408 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002409 if (IS_ERR(bdev)) {
2410 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002411 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002412 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002413
2414 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2415
2416 devices = &fs_info->fs_devices->devices;
2417 list_for_each_entry(device, devices, dev_list) {
2418 if (device->bdev == bdev) {
Miao Xie1c433662014-09-03 21:35:32 +08002419 btrfs_err(fs_info, "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002420 ret = -EEXIST;
2421 goto error;
2422 }
2423 }
2424
Miao Xie1c433662014-09-03 21:35:32 +08002425
Miao Xie7cc8e582014-09-03 21:35:38 +08002426 if (i_size_read(bdev->bd_inode) <
2427 btrfs_device_get_total_bytes(srcdev)) {
Miao Xie1c433662014-09-03 21:35:32 +08002428 btrfs_err(fs_info, "target device is smaller than source device!");
2429 ret = -EINVAL;
2430 goto error;
2431 }
2432
2433
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002434 device = btrfs_alloc_device(NULL, &devid, NULL);
2435 if (IS_ERR(device)) {
2436 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002437 goto error;
2438 }
2439
2440 name = rcu_string_strdup(device_path, GFP_NOFS);
2441 if (!name) {
2442 kfree(device);
2443 ret = -ENOMEM;
2444 goto error;
2445 }
2446 rcu_assign_pointer(device->name, name);
2447
2448 q = bdev_get_queue(bdev);
2449 if (blk_queue_discard(q))
2450 device->can_discard = 1;
2451 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2452 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002453 device->generation = 0;
2454 device->io_width = root->sectorsize;
2455 device->io_align = root->sectorsize;
2456 device->sector_size = root->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002457 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2458 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2459 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002460 ASSERT(list_empty(&srcdev->resized_list));
2461 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002462 device->commit_bytes_used = device->bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002463 device->dev_root = fs_info->dev_root;
2464 device->bdev = bdev;
2465 device->in_fs_metadata = 1;
2466 device->is_tgtdev_for_dev_replace = 1;
2467 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002468 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002469 set_blocksize(device->bdev, 4096);
2470 device->fs_devices = fs_info->fs_devices;
2471 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2472 fs_info->fs_devices->num_devices++;
2473 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002474 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2475
2476 *device_out = device;
2477 return ret;
2478
2479error:
2480 blkdev_put(bdev, FMODE_EXCL);
2481 return ret;
2482}
2483
2484void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2485 struct btrfs_device *tgtdev)
2486{
2487 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2488 tgtdev->io_width = fs_info->dev_root->sectorsize;
2489 tgtdev->io_align = fs_info->dev_root->sectorsize;
2490 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2491 tgtdev->dev_root = fs_info->dev_root;
2492 tgtdev->in_fs_metadata = 1;
2493}
2494
Chris Masond3977122009-01-05 21:25:51 -05002495static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2496 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002497{
2498 int ret;
2499 struct btrfs_path *path;
2500 struct btrfs_root *root;
2501 struct btrfs_dev_item *dev_item;
2502 struct extent_buffer *leaf;
2503 struct btrfs_key key;
2504
2505 root = device->dev_root->fs_info->chunk_root;
2506
2507 path = btrfs_alloc_path();
2508 if (!path)
2509 return -ENOMEM;
2510
2511 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2512 key.type = BTRFS_DEV_ITEM_KEY;
2513 key.offset = device->devid;
2514
2515 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2516 if (ret < 0)
2517 goto out;
2518
2519 if (ret > 0) {
2520 ret = -ENOENT;
2521 goto out;
2522 }
2523
2524 leaf = path->nodes[0];
2525 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2526
2527 btrfs_set_device_id(leaf, dev_item, device->devid);
2528 btrfs_set_device_type(leaf, dev_item, device->type);
2529 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2530 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2531 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002532 btrfs_set_device_total_bytes(leaf, dev_item,
2533 btrfs_device_get_disk_total_bytes(device));
2534 btrfs_set_device_bytes_used(leaf, dev_item,
2535 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002536 btrfs_mark_buffer_dirty(leaf);
2537
2538out:
2539 btrfs_free_path(path);
2540 return ret;
2541}
2542
Miao Xie2196d6e2014-09-03 21:35:41 +08002543int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002544 struct btrfs_device *device, u64 new_size)
2545{
2546 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002547 device->dev_root->fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002548 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002549 u64 old_total;
2550 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002551
Yan Zheng2b820322008-11-17 21:11:30 -05002552 if (!device->writeable)
2553 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002554
2555 lock_chunks(device->dev_root);
2556 old_total = btrfs_super_total_bytes(super_copy);
2557 diff = new_size - device->total_bytes;
2558
Stefan Behrens63a212a2012-11-05 18:29:28 +01002559 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002560 device->is_tgtdev_for_dev_replace) {
2561 unlock_chunks(device->dev_root);
Yan Zheng2b820322008-11-17 21:11:30 -05002562 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002563 }
Yan Zheng2b820322008-11-17 21:11:30 -05002564
Miao Xie935e5cc2014-09-03 21:35:33 +08002565 fs_devices = device->dev_root->fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002566
2567 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002568 device->fs_devices->total_rw_bytes += diff;
2569
Miao Xie7cc8e582014-09-03 21:35:38 +08002570 btrfs_device_set_total_bytes(device, new_size);
2571 btrfs_device_set_disk_total_bytes(device, new_size);
Chris Mason4184ea72009-03-10 12:39:20 -04002572 btrfs_clear_space_info_full(device->dev_root->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002573 if (list_empty(&device->resized_list))
2574 list_add_tail(&device->resized_list,
2575 &fs_devices->resized_devices);
Miao Xie2196d6e2014-09-03 21:35:41 +08002576 unlock_chunks(device->dev_root);
Chris Mason4184ea72009-03-10 12:39:20 -04002577
Chris Mason8f18cf12008-04-25 16:53:30 -04002578 return btrfs_update_device(trans, device);
2579}
2580
2581static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Zhao Leia688a042015-02-09 20:31:44 +08002582 struct btrfs_root *root, u64 chunk_objectid,
Chris Mason8f18cf12008-04-25 16:53:30 -04002583 u64 chunk_offset)
2584{
2585 int ret;
2586 struct btrfs_path *path;
2587 struct btrfs_key key;
2588
2589 root = root->fs_info->chunk_root;
2590 path = btrfs_alloc_path();
2591 if (!path)
2592 return -ENOMEM;
2593
2594 key.objectid = chunk_objectid;
2595 key.offset = chunk_offset;
2596 key.type = BTRFS_CHUNK_ITEM_KEY;
2597
2598 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002599 if (ret < 0)
2600 goto out;
2601 else if (ret > 0) { /* Logic error or corruption */
2602 btrfs_error(root->fs_info, -ENOENT,
2603 "Failed lookup while freeing chunk.");
2604 ret = -ENOENT;
2605 goto out;
2606 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002607
2608 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002609 if (ret < 0)
2610 btrfs_error(root->fs_info, ret,
2611 "Failed to delete chunk item.");
2612out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002613 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002614 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002615}
2616
Christoph Hellwigb2950862008-12-02 09:54:17 -05002617static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002618 chunk_offset)
2619{
David Sterba6c417612011-04-13 15:41:04 +02002620 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002621 struct btrfs_disk_key *disk_key;
2622 struct btrfs_chunk *chunk;
2623 u8 *ptr;
2624 int ret = 0;
2625 u32 num_stripes;
2626 u32 array_size;
2627 u32 len = 0;
2628 u32 cur;
2629 struct btrfs_key key;
2630
Miao Xie2196d6e2014-09-03 21:35:41 +08002631 lock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002632 array_size = btrfs_super_sys_array_size(super_copy);
2633
2634 ptr = super_copy->sys_chunk_array;
2635 cur = 0;
2636
2637 while (cur < array_size) {
2638 disk_key = (struct btrfs_disk_key *)ptr;
2639 btrfs_disk_key_to_cpu(&key, disk_key);
2640
2641 len = sizeof(*disk_key);
2642
2643 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2644 chunk = (struct btrfs_chunk *)(ptr + len);
2645 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2646 len += btrfs_chunk_item_size(num_stripes);
2647 } else {
2648 ret = -EIO;
2649 break;
2650 }
2651 if (key.objectid == chunk_objectid &&
2652 key.offset == chunk_offset) {
2653 memmove(ptr, ptr + len, array_size - (cur + len));
2654 array_size -= len;
2655 btrfs_set_super_sys_array_size(super_copy, array_size);
2656 } else {
2657 ptr += len;
2658 cur += len;
2659 }
2660 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002661 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002662 return ret;
2663}
2664
Josef Bacik47ab2a62014-09-18 11:20:02 -04002665int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2666 struct btrfs_root *root, u64 chunk_offset)
2667{
2668 struct extent_map_tree *em_tree;
2669 struct extent_map *em;
2670 struct btrfs_root *extent_root = root->fs_info->extent_root;
2671 struct map_lookup *map;
2672 u64 dev_extent_len = 0;
2673 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002674 int i, ret = 0;
2675
2676 /* Just in case */
2677 root = root->fs_info->chunk_root;
2678 em_tree = &root->fs_info->mapping_tree.map_tree;
2679
2680 read_lock(&em_tree->lock);
2681 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2682 read_unlock(&em_tree->lock);
2683
2684 if (!em || em->start > chunk_offset ||
2685 em->start + em->len < chunk_offset) {
2686 /*
2687 * This is a logic error, but we don't want to just rely on the
2688 * user having built with ASSERT enabled, so if ASSERT doens't
2689 * do anything we still error out.
2690 */
2691 ASSERT(0);
2692 if (em)
2693 free_extent_map(em);
2694 return -EINVAL;
2695 }
2696 map = (struct map_lookup *)em->bdev;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002697 lock_chunks(root->fs_info->chunk_root);
Filipe Manana4617ea32015-06-09 17:48:21 +01002698 check_system_chunk(trans, extent_root, map->type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002699 unlock_chunks(root->fs_info->chunk_root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002700
2701 for (i = 0; i < map->num_stripes; i++) {
2702 struct btrfs_device *device = map->stripes[i].dev;
2703 ret = btrfs_free_dev_extent(trans, device,
2704 map->stripes[i].physical,
2705 &dev_extent_len);
2706 if (ret) {
2707 btrfs_abort_transaction(trans, root, ret);
2708 goto out;
2709 }
2710
2711 if (device->bytes_used > 0) {
2712 lock_chunks(root);
2713 btrfs_device_set_bytes_used(device,
2714 device->bytes_used - dev_extent_len);
2715 spin_lock(&root->fs_info->free_chunk_lock);
2716 root->fs_info->free_chunk_space += dev_extent_len;
2717 spin_unlock(&root->fs_info->free_chunk_lock);
2718 btrfs_clear_space_info_full(root->fs_info);
2719 unlock_chunks(root);
2720 }
2721
2722 if (map->stripes[i].dev) {
2723 ret = btrfs_update_device(trans, map->stripes[i].dev);
2724 if (ret) {
2725 btrfs_abort_transaction(trans, root, ret);
2726 goto out;
2727 }
2728 }
2729 }
Zhao Leia688a042015-02-09 20:31:44 +08002730 ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002731 if (ret) {
2732 btrfs_abort_transaction(trans, root, ret);
2733 goto out;
2734 }
2735
2736 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2737
2738 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2739 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2740 if (ret) {
2741 btrfs_abort_transaction(trans, root, ret);
2742 goto out;
2743 }
2744 }
2745
Filipe Manana04216822014-11-27 21:14:15 +00002746 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002747 if (ret) {
2748 btrfs_abort_transaction(trans, extent_root, ret);
2749 goto out;
2750 }
2751
Josef Bacik47ab2a62014-09-18 11:20:02 -04002752out:
2753 /* once for us */
2754 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002755 return ret;
2756}
2757
Christoph Hellwigb2950862008-12-02 09:54:17 -05002758static int btrfs_relocate_chunk(struct btrfs_root *root,
Zhao Leia688a042015-02-09 20:31:44 +08002759 u64 chunk_objectid,
2760 u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002761{
Chris Mason8f18cf12008-04-25 16:53:30 -04002762 struct btrfs_root *extent_root;
2763 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002764 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002765
2766 root = root->fs_info->chunk_root;
2767 extent_root = root->fs_info->extent_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002768
Josef Bacikba1bf482009-09-11 16:11:19 -04002769 ret = btrfs_can_relocate(extent_root, chunk_offset);
2770 if (ret)
2771 return -ENOSPC;
2772
Chris Mason8f18cf12008-04-25 16:53:30 -04002773 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002774 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002775 if (ret)
2776 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002777
Yan, Zhenga22285a2010-05-16 10:48:46 -04002778 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002779 if (IS_ERR(trans)) {
2780 ret = PTR_ERR(trans);
2781 btrfs_std_error(root->fs_info, ret);
2782 return ret;
2783 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002784
2785 /*
2786 * step two, delete the device extents and the
2787 * chunk tree entries
2788 */
Josef Bacik47ab2a62014-09-18 11:20:02 -04002789 ret = btrfs_remove_chunk(trans, root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002790 btrfs_end_transaction(trans, root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002791 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002792}
2793
Yan Zheng2b820322008-11-17 21:11:30 -05002794static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2795{
2796 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2797 struct btrfs_path *path;
2798 struct extent_buffer *leaf;
2799 struct btrfs_chunk *chunk;
2800 struct btrfs_key key;
2801 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002802 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002803 bool retried = false;
2804 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002805 int ret;
2806
2807 path = btrfs_alloc_path();
2808 if (!path)
2809 return -ENOMEM;
2810
Josef Bacikba1bf482009-09-11 16:11:19 -04002811again:
Yan Zheng2b820322008-11-17 21:11:30 -05002812 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2813 key.offset = (u64)-1;
2814 key.type = BTRFS_CHUNK_ITEM_KEY;
2815
2816 while (1) {
2817 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2818 if (ret < 0)
2819 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002820 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002821
2822 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2823 key.type);
2824 if (ret < 0)
2825 goto error;
2826 if (ret > 0)
2827 break;
2828
2829 leaf = path->nodes[0];
2830 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2831
2832 chunk = btrfs_item_ptr(leaf, path->slots[0],
2833 struct btrfs_chunk);
2834 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002835 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002836
2837 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Zhao Leia688a042015-02-09 20:31:44 +08002838 ret = btrfs_relocate_chunk(chunk_root,
Yan Zheng2b820322008-11-17 21:11:30 -05002839 found_key.objectid,
2840 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002841 if (ret == -ENOSPC)
2842 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302843 else
2844 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002845 }
2846
2847 if (found_key.offset == 0)
2848 break;
2849 key.offset = found_key.offset - 1;
2850 }
2851 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002852 if (failed && !retried) {
2853 failed = 0;
2854 retried = true;
2855 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302856 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002857 ret = -ENOSPC;
2858 }
Yan Zheng2b820322008-11-17 21:11:30 -05002859error:
2860 btrfs_free_path(path);
2861 return ret;
2862}
2863
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002864static int insert_balance_item(struct btrfs_root *root,
2865 struct btrfs_balance_control *bctl)
2866{
2867 struct btrfs_trans_handle *trans;
2868 struct btrfs_balance_item *item;
2869 struct btrfs_disk_balance_args disk_bargs;
2870 struct btrfs_path *path;
2871 struct extent_buffer *leaf;
2872 struct btrfs_key key;
2873 int ret, err;
2874
2875 path = btrfs_alloc_path();
2876 if (!path)
2877 return -ENOMEM;
2878
2879 trans = btrfs_start_transaction(root, 0);
2880 if (IS_ERR(trans)) {
2881 btrfs_free_path(path);
2882 return PTR_ERR(trans);
2883 }
2884
2885 key.objectid = BTRFS_BALANCE_OBJECTID;
2886 key.type = BTRFS_BALANCE_ITEM_KEY;
2887 key.offset = 0;
2888
2889 ret = btrfs_insert_empty_item(trans, root, path, &key,
2890 sizeof(*item));
2891 if (ret)
2892 goto out;
2893
2894 leaf = path->nodes[0];
2895 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2896
2897 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2898
2899 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2900 btrfs_set_balance_data(leaf, item, &disk_bargs);
2901 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2902 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2903 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2904 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2905
2906 btrfs_set_balance_flags(leaf, item, bctl->flags);
2907
2908 btrfs_mark_buffer_dirty(leaf);
2909out:
2910 btrfs_free_path(path);
2911 err = btrfs_commit_transaction(trans, root);
2912 if (err && !ret)
2913 ret = err;
2914 return ret;
2915}
2916
2917static int del_balance_item(struct btrfs_root *root)
2918{
2919 struct btrfs_trans_handle *trans;
2920 struct btrfs_path *path;
2921 struct btrfs_key key;
2922 int ret, err;
2923
2924 path = btrfs_alloc_path();
2925 if (!path)
2926 return -ENOMEM;
2927
2928 trans = btrfs_start_transaction(root, 0);
2929 if (IS_ERR(trans)) {
2930 btrfs_free_path(path);
2931 return PTR_ERR(trans);
2932 }
2933
2934 key.objectid = BTRFS_BALANCE_OBJECTID;
2935 key.type = BTRFS_BALANCE_ITEM_KEY;
2936 key.offset = 0;
2937
2938 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2939 if (ret < 0)
2940 goto out;
2941 if (ret > 0) {
2942 ret = -ENOENT;
2943 goto out;
2944 }
2945
2946 ret = btrfs_del_item(trans, root, path);
2947out:
2948 btrfs_free_path(path);
2949 err = btrfs_commit_transaction(trans, root);
2950 if (err && !ret)
2951 ret = err;
2952 return ret;
2953}
2954
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002955/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002956 * This is a heuristic used to reduce the number of chunks balanced on
2957 * resume after balance was interrupted.
2958 */
2959static void update_balance_args(struct btrfs_balance_control *bctl)
2960{
2961 /*
2962 * Turn on soft mode for chunk types that were being converted.
2963 */
2964 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2965 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2966 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2967 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2968 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2969 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2970
2971 /*
2972 * Turn on usage filter if is not already used. The idea is
2973 * that chunks that we have already balanced should be
2974 * reasonably full. Don't do it for chunks that are being
2975 * converted - that will keep us from relocating unconverted
2976 * (albeit full) chunks.
2977 */
2978 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2979 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2980 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2981 bctl->data.usage = 90;
2982 }
2983 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2984 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2985 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2986 bctl->sys.usage = 90;
2987 }
2988 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2989 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2990 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2991 bctl->meta.usage = 90;
2992 }
2993}
2994
2995/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002996 * Should be called with both balance and volume mutexes held to
2997 * serialize other volume operations (add_dev/rm_dev/resize) with
2998 * restriper. Same goes for unset_balance_control.
2999 */
3000static void set_balance_control(struct btrfs_balance_control *bctl)
3001{
3002 struct btrfs_fs_info *fs_info = bctl->fs_info;
3003
3004 BUG_ON(fs_info->balance_ctl);
3005
3006 spin_lock(&fs_info->balance_lock);
3007 fs_info->balance_ctl = bctl;
3008 spin_unlock(&fs_info->balance_lock);
3009}
3010
3011static void unset_balance_control(struct btrfs_fs_info *fs_info)
3012{
3013 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3014
3015 BUG_ON(!fs_info->balance_ctl);
3016
3017 spin_lock(&fs_info->balance_lock);
3018 fs_info->balance_ctl = NULL;
3019 spin_unlock(&fs_info->balance_lock);
3020
3021 kfree(bctl);
3022}
3023
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003024/*
3025 * Balance filters. Return 1 if chunk should be filtered out
3026 * (should not be balanced).
3027 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003028static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003029 struct btrfs_balance_args *bargs)
3030{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003031 chunk_type = chunk_to_extended(chunk_type) &
3032 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003033
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003034 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003035 return 0;
3036
3037 return 1;
3038}
3039
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003040static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
3041 struct btrfs_balance_args *bargs)
3042{
3043 struct btrfs_block_group_cache *cache;
3044 u64 chunk_used, user_thresh;
3045 int ret = 1;
3046
3047 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3048 chunk_used = btrfs_block_group_used(&cache->item);
3049
Ilya Dryomova105bb82013-01-21 15:15:56 +02003050 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003051 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003052 else if (bargs->usage > 100)
3053 user_thresh = cache->key.offset;
3054 else
3055 user_thresh = div_factor_fine(cache->key.offset,
3056 bargs->usage);
3057
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003058 if (chunk_used < user_thresh)
3059 ret = 0;
3060
3061 btrfs_put_block_group(cache);
3062 return ret;
3063}
3064
Ilya Dryomov409d4042012-01-16 22:04:47 +02003065static int chunk_devid_filter(struct extent_buffer *leaf,
3066 struct btrfs_chunk *chunk,
3067 struct btrfs_balance_args *bargs)
3068{
3069 struct btrfs_stripe *stripe;
3070 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3071 int i;
3072
3073 for (i = 0; i < num_stripes; i++) {
3074 stripe = btrfs_stripe_nr(chunk, i);
3075 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3076 return 0;
3077 }
3078
3079 return 1;
3080}
3081
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003082/* [pstart, pend) */
3083static int chunk_drange_filter(struct extent_buffer *leaf,
3084 struct btrfs_chunk *chunk,
3085 u64 chunk_offset,
3086 struct btrfs_balance_args *bargs)
3087{
3088 struct btrfs_stripe *stripe;
3089 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3090 u64 stripe_offset;
3091 u64 stripe_length;
3092 int factor;
3093 int i;
3094
3095 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3096 return 0;
3097
3098 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003099 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3100 factor = num_stripes / 2;
3101 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3102 factor = num_stripes - 1;
3103 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3104 factor = num_stripes - 2;
3105 } else {
3106 factor = num_stripes;
3107 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003108
3109 for (i = 0; i < num_stripes; i++) {
3110 stripe = btrfs_stripe_nr(chunk, i);
3111 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3112 continue;
3113
3114 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3115 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003116 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003117
3118 if (stripe_offset < bargs->pend &&
3119 stripe_offset + stripe_length > bargs->pstart)
3120 return 0;
3121 }
3122
3123 return 1;
3124}
3125
Ilya Dryomovea671762012-01-16 22:04:48 +02003126/* [vstart, vend) */
3127static int chunk_vrange_filter(struct extent_buffer *leaf,
3128 struct btrfs_chunk *chunk,
3129 u64 chunk_offset,
3130 struct btrfs_balance_args *bargs)
3131{
3132 if (chunk_offset < bargs->vend &&
3133 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3134 /* at least part of the chunk is inside this vrange */
3135 return 0;
3136
3137 return 1;
3138}
3139
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003140static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003141 struct btrfs_balance_args *bargs)
3142{
3143 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3144 return 0;
3145
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003146 chunk_type = chunk_to_extended(chunk_type) &
3147 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003148
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003149 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003150 return 1;
3151
3152 return 0;
3153}
3154
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003155static int should_balance_chunk(struct btrfs_root *root,
3156 struct extent_buffer *leaf,
3157 struct btrfs_chunk *chunk, u64 chunk_offset)
3158{
3159 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3160 struct btrfs_balance_args *bargs = NULL;
3161 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3162
3163 /* type filter */
3164 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3165 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3166 return 0;
3167 }
3168
3169 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3170 bargs = &bctl->data;
3171 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3172 bargs = &bctl->sys;
3173 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3174 bargs = &bctl->meta;
3175
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003176 /* profiles filter */
3177 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3178 chunk_profiles_filter(chunk_type, bargs)) {
3179 return 0;
3180 }
3181
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003182 /* usage filter */
3183 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3184 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3185 return 0;
3186 }
3187
Ilya Dryomov409d4042012-01-16 22:04:47 +02003188 /* devid filter */
3189 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3190 chunk_devid_filter(leaf, chunk, bargs)) {
3191 return 0;
3192 }
3193
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003194 /* drange filter, makes sense only with devid filter */
3195 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3196 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3197 return 0;
3198 }
3199
Ilya Dryomovea671762012-01-16 22:04:48 +02003200 /* vrange filter */
3201 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3202 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3203 return 0;
3204 }
3205
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003206 /* soft profile changing mode */
3207 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3208 chunk_soft_convert_filter(chunk_type, bargs)) {
3209 return 0;
3210 }
3211
David Sterba7d824b62014-05-07 17:37:51 +02003212 /*
3213 * limited by count, must be the last filter
3214 */
3215 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3216 if (bargs->limit == 0)
3217 return 0;
3218 else
3219 bargs->limit--;
3220 }
3221
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003222 return 1;
3223}
3224
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003225static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003226{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003227 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003228 struct btrfs_root *chunk_root = fs_info->chunk_root;
3229 struct btrfs_root *dev_root = fs_info->dev_root;
3230 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003231 struct btrfs_device *device;
3232 u64 old_size;
3233 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003234 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003235 struct btrfs_path *path;
3236 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003237 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003238 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003239 struct extent_buffer *leaf;
3240 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003241 int ret;
3242 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003243 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003244 u64 limit_data = bctl->data.limit;
3245 u64 limit_meta = bctl->meta.limit;
3246 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003247
Chris Masonec44a352008-04-28 15:29:52 -04003248 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003249 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003250 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003251 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003252 size_to_free = div_factor(old_size, 1);
3253 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003254 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003255 btrfs_device_get_total_bytes(device) -
3256 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003257 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003258 continue;
3259
3260 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003261 if (ret == -ENOSPC)
3262 break;
Chris Masonec44a352008-04-28 15:29:52 -04003263 BUG_ON(ret);
3264
Yan, Zhenga22285a2010-05-16 10:48:46 -04003265 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003266 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003267
3268 ret = btrfs_grow_device(trans, device, old_size);
3269 BUG_ON(ret);
3270
3271 btrfs_end_transaction(trans, dev_root);
3272 }
3273
3274 /* step two, relocate all the chunks */
3275 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003276 if (!path) {
3277 ret = -ENOMEM;
3278 goto error;
3279 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003280
3281 /* zero out stat counters */
3282 spin_lock(&fs_info->balance_lock);
3283 memset(&bctl->stat, 0, sizeof(bctl->stat));
3284 spin_unlock(&fs_info->balance_lock);
3285again:
David Sterba7d824b62014-05-07 17:37:51 +02003286 if (!counting) {
3287 bctl->data.limit = limit_data;
3288 bctl->meta.limit = limit_meta;
3289 bctl->sys.limit = limit_sys;
3290 }
Chris Masonec44a352008-04-28 15:29:52 -04003291 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3292 key.offset = (u64)-1;
3293 key.type = BTRFS_CHUNK_ITEM_KEY;
3294
Chris Masond3977122009-01-05 21:25:51 -05003295 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003296 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003297 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003298 ret = -ECANCELED;
3299 goto error;
3300 }
3301
Chris Masonec44a352008-04-28 15:29:52 -04003302 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3303 if (ret < 0)
3304 goto error;
3305
3306 /*
3307 * this shouldn't happen, it means the last relocate
3308 * failed
3309 */
3310 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003311 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003312
3313 ret = btrfs_previous_item(chunk_root, path, 0,
3314 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003315 if (ret) {
3316 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003317 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003318 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003319
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003320 leaf = path->nodes[0];
3321 slot = path->slots[0];
3322 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3323
Chris Masonec44a352008-04-28 15:29:52 -04003324 if (found_key.objectid != key.objectid)
3325 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003326
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003327 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3328
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003329 if (!counting) {
3330 spin_lock(&fs_info->balance_lock);
3331 bctl->stat.considered++;
3332 spin_unlock(&fs_info->balance_lock);
3333 }
3334
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003335 ret = should_balance_chunk(chunk_root, leaf, chunk,
3336 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003337 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003338 if (!ret)
3339 goto loop;
3340
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003341 if (counting) {
3342 spin_lock(&fs_info->balance_lock);
3343 bctl->stat.expected++;
3344 spin_unlock(&fs_info->balance_lock);
3345 goto loop;
3346 }
3347
Chris Masonec44a352008-04-28 15:29:52 -04003348 ret = btrfs_relocate_chunk(chunk_root,
Chris Masonec44a352008-04-28 15:29:52 -04003349 found_key.objectid,
3350 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003351 if (ret && ret != -ENOSPC)
3352 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003353 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003354 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003355 } else {
3356 spin_lock(&fs_info->balance_lock);
3357 bctl->stat.completed++;
3358 spin_unlock(&fs_info->balance_lock);
3359 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003360loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003361 if (found_key.offset == 0)
3362 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003363 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003364 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003365
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003366 if (counting) {
3367 btrfs_release_path(path);
3368 counting = false;
3369 goto again;
3370 }
Chris Masonec44a352008-04-28 15:29:52 -04003371error:
3372 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003373 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003374 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003375 enospc_errors);
3376 if (!ret)
3377 ret = -ENOSPC;
3378 }
3379
Chris Masonec44a352008-04-28 15:29:52 -04003380 return ret;
3381}
3382
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003383/**
3384 * alloc_profile_is_valid - see if a given profile is valid and reduced
3385 * @flags: profile to validate
3386 * @extended: if true @flags is treated as an extended profile
3387 */
3388static int alloc_profile_is_valid(u64 flags, int extended)
3389{
3390 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3391 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3392
3393 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3394
3395 /* 1) check that all other bits are zeroed */
3396 if (flags & ~mask)
3397 return 0;
3398
3399 /* 2) see if profile is reduced */
3400 if (flags == 0)
3401 return !extended; /* "0" is valid for usual profiles */
3402
3403 /* true if exactly one bit set */
3404 return (flags & (flags - 1)) == 0;
3405}
3406
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003407static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3408{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003409 /* cancel requested || normal exit path */
3410 return atomic_read(&fs_info->balance_cancel_req) ||
3411 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3412 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003413}
3414
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003415static void __cancel_balance(struct btrfs_fs_info *fs_info)
3416{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003417 int ret;
3418
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003419 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003420 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003421 if (ret)
3422 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003423
3424 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003425}
3426
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003427/*
3428 * Should be called with both balance and volume mutexes held
3429 */
3430int btrfs_balance(struct btrfs_balance_control *bctl,
3431 struct btrfs_ioctl_balance_args *bargs)
3432{
3433 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003434 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003435 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003436 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003437 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003438 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003439
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003440 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003441 atomic_read(&fs_info->balance_pause_req) ||
3442 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003443 ret = -EINVAL;
3444 goto out;
3445 }
3446
Ilya Dryomove4837f82012-03-27 17:09:17 +03003447 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3448 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3449 mixed = 1;
3450
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003451 /*
3452 * In case of mixed groups both data and meta should be picked,
3453 * and identical options should be given for both of them.
3454 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003455 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3456 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003457 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3458 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3459 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003460 btrfs_err(fs_info, "with mixed groups data and "
3461 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003462 ret = -EINVAL;
3463 goto out;
3464 }
3465 }
3466
Stefan Behrens8dabb742012-11-06 13:15:27 +01003467 num_devices = fs_info->fs_devices->num_devices;
3468 btrfs_dev_replace_lock(&fs_info->dev_replace);
3469 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3470 BUG_ON(num_devices < 1);
3471 num_devices--;
3472 }
3473 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003474 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003475 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003476 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003477 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003478 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003479 if (num_devices > 2)
3480 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3481 if (num_devices > 3)
3482 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3483 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003484 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3485 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3486 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003487 btrfs_err(fs_info, "unable to start balance with target "
3488 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003489 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003490 ret = -EINVAL;
3491 goto out;
3492 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003493 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3494 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3495 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003496 btrfs_err(fs_info,
3497 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003498 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003499 ret = -EINVAL;
3500 goto out;
3501 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003502 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3503 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3504 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003505 btrfs_err(fs_info,
3506 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003507 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003508 ret = -EINVAL;
3509 goto out;
3510 }
3511
Ilya Dryomove4837f82012-03-27 17:09:17 +03003512 /* allow dup'ed data chunks only in mixed mode */
3513 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003514 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003515 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003516 ret = -EINVAL;
3517 goto out;
3518 }
3519
3520 /* allow to reduce meta or sys integrity only if force set */
3521 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003522 BTRFS_BLOCK_GROUP_RAID10 |
3523 BTRFS_BLOCK_GROUP_RAID5 |
3524 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003525 do {
3526 seq = read_seqbegin(&fs_info->profiles_lock);
3527
3528 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3529 (fs_info->avail_system_alloc_bits & allowed) &&
3530 !(bctl->sys.target & allowed)) ||
3531 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3532 (fs_info->avail_metadata_alloc_bits & allowed) &&
3533 !(bctl->meta.target & allowed))) {
3534 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003535 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003536 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003537 btrfs_err(fs_info, "balance will reduce metadata "
3538 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003539 ret = -EINVAL;
3540 goto out;
3541 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003542 }
Miao Xiede98ced2013-01-29 10:13:12 +00003543 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003544
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003545 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3546 int num_tolerated_disk_barrier_failures;
3547 u64 target = bctl->sys.target;
3548
3549 num_tolerated_disk_barrier_failures =
3550 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3551 if (num_tolerated_disk_barrier_failures > 0 &&
3552 (target &
3553 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3554 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3555 num_tolerated_disk_barrier_failures = 0;
3556 else if (num_tolerated_disk_barrier_failures > 1 &&
3557 (target &
3558 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3559 num_tolerated_disk_barrier_failures = 1;
3560
3561 fs_info->num_tolerated_disk_barrier_failures =
3562 num_tolerated_disk_barrier_failures;
3563 }
3564
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003565 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003566 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003567 goto out;
3568
Ilya Dryomov59641012012-01-16 22:04:48 +02003569 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3570 BUG_ON(ret == -EEXIST);
3571 set_balance_control(bctl);
3572 } else {
3573 BUG_ON(ret != -EEXIST);
3574 spin_lock(&fs_info->balance_lock);
3575 update_balance_args(bctl);
3576 spin_unlock(&fs_info->balance_lock);
3577 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003578
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003579 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003580 mutex_unlock(&fs_info->balance_mutex);
3581
3582 ret = __btrfs_balance(fs_info);
3583
3584 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003585 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003586
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003587 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3588 fs_info->num_tolerated_disk_barrier_failures =
3589 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3590 }
3591
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003592 if (bargs) {
3593 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003594 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003595 }
3596
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003597 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3598 balance_need_close(fs_info)) {
3599 __cancel_balance(fs_info);
3600 }
3601
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003602 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003603
3604 return ret;
3605out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003606 if (bctl->flags & BTRFS_BALANCE_RESUME)
3607 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003608 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003609 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003610 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3611 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003612 return ret;
3613}
3614
3615static int balance_kthread(void *data)
3616{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003617 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003618 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003619
3620 mutex_lock(&fs_info->volume_mutex);
3621 mutex_lock(&fs_info->balance_mutex);
3622
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003623 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003624 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003625 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003626 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003627
3628 mutex_unlock(&fs_info->balance_mutex);
3629 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003630
Ilya Dryomov59641012012-01-16 22:04:48 +02003631 return ret;
3632}
3633
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003634int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3635{
3636 struct task_struct *tsk;
3637
3638 spin_lock(&fs_info->balance_lock);
3639 if (!fs_info->balance_ctl) {
3640 spin_unlock(&fs_info->balance_lock);
3641 return 0;
3642 }
3643 spin_unlock(&fs_info->balance_lock);
3644
3645 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003646 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003647 return 0;
3648 }
3649
3650 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303651 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003652}
3653
Ilya Dryomov68310a52012-06-22 12:24:12 -06003654int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003655{
Ilya Dryomov59641012012-01-16 22:04:48 +02003656 struct btrfs_balance_control *bctl;
3657 struct btrfs_balance_item *item;
3658 struct btrfs_disk_balance_args disk_bargs;
3659 struct btrfs_path *path;
3660 struct extent_buffer *leaf;
3661 struct btrfs_key key;
3662 int ret;
3663
3664 path = btrfs_alloc_path();
3665 if (!path)
3666 return -ENOMEM;
3667
Ilya Dryomov68310a52012-06-22 12:24:12 -06003668 key.objectid = BTRFS_BALANCE_OBJECTID;
3669 key.type = BTRFS_BALANCE_ITEM_KEY;
3670 key.offset = 0;
3671
3672 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3673 if (ret < 0)
3674 goto out;
3675 if (ret > 0) { /* ret = -ENOENT; */
3676 ret = 0;
3677 goto out;
3678 }
3679
Ilya Dryomov59641012012-01-16 22:04:48 +02003680 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3681 if (!bctl) {
3682 ret = -ENOMEM;
3683 goto out;
3684 }
3685
Ilya Dryomov59641012012-01-16 22:04:48 +02003686 leaf = path->nodes[0];
3687 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3688
Ilya Dryomov68310a52012-06-22 12:24:12 -06003689 bctl->fs_info = fs_info;
3690 bctl->flags = btrfs_balance_flags(leaf, item);
3691 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003692
3693 btrfs_balance_data(leaf, item, &disk_bargs);
3694 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3695 btrfs_balance_meta(leaf, item, &disk_bargs);
3696 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3697 btrfs_balance_sys(leaf, item, &disk_bargs);
3698 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3699
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003700 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3701
Ilya Dryomov68310a52012-06-22 12:24:12 -06003702 mutex_lock(&fs_info->volume_mutex);
3703 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003704
Ilya Dryomov68310a52012-06-22 12:24:12 -06003705 set_balance_control(bctl);
3706
3707 mutex_unlock(&fs_info->balance_mutex);
3708 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003709out:
3710 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003711 return ret;
3712}
3713
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003714int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3715{
3716 int ret = 0;
3717
3718 mutex_lock(&fs_info->balance_mutex);
3719 if (!fs_info->balance_ctl) {
3720 mutex_unlock(&fs_info->balance_mutex);
3721 return -ENOTCONN;
3722 }
3723
3724 if (atomic_read(&fs_info->balance_running)) {
3725 atomic_inc(&fs_info->balance_pause_req);
3726 mutex_unlock(&fs_info->balance_mutex);
3727
3728 wait_event(fs_info->balance_wait_q,
3729 atomic_read(&fs_info->balance_running) == 0);
3730
3731 mutex_lock(&fs_info->balance_mutex);
3732 /* we are good with balance_ctl ripped off from under us */
3733 BUG_ON(atomic_read(&fs_info->balance_running));
3734 atomic_dec(&fs_info->balance_pause_req);
3735 } else {
3736 ret = -ENOTCONN;
3737 }
3738
3739 mutex_unlock(&fs_info->balance_mutex);
3740 return ret;
3741}
3742
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003743int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3744{
Ilya Dryomove649e582013-10-10 20:40:21 +03003745 if (fs_info->sb->s_flags & MS_RDONLY)
3746 return -EROFS;
3747
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003748 mutex_lock(&fs_info->balance_mutex);
3749 if (!fs_info->balance_ctl) {
3750 mutex_unlock(&fs_info->balance_mutex);
3751 return -ENOTCONN;
3752 }
3753
3754 atomic_inc(&fs_info->balance_cancel_req);
3755 /*
3756 * if we are running just wait and return, balance item is
3757 * deleted in btrfs_balance in this case
3758 */
3759 if (atomic_read(&fs_info->balance_running)) {
3760 mutex_unlock(&fs_info->balance_mutex);
3761 wait_event(fs_info->balance_wait_q,
3762 atomic_read(&fs_info->balance_running) == 0);
3763 mutex_lock(&fs_info->balance_mutex);
3764 } else {
3765 /* __cancel_balance needs volume_mutex */
3766 mutex_unlock(&fs_info->balance_mutex);
3767 mutex_lock(&fs_info->volume_mutex);
3768 mutex_lock(&fs_info->balance_mutex);
3769
3770 if (fs_info->balance_ctl)
3771 __cancel_balance(fs_info);
3772
3773 mutex_unlock(&fs_info->volume_mutex);
3774 }
3775
3776 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3777 atomic_dec(&fs_info->balance_cancel_req);
3778 mutex_unlock(&fs_info->balance_mutex);
3779 return 0;
3780}
3781
Stefan Behrens803b2f52013-08-15 17:11:21 +02003782static int btrfs_uuid_scan_kthread(void *data)
3783{
3784 struct btrfs_fs_info *fs_info = data;
3785 struct btrfs_root *root = fs_info->tree_root;
3786 struct btrfs_key key;
3787 struct btrfs_key max_key;
3788 struct btrfs_path *path = NULL;
3789 int ret = 0;
3790 struct extent_buffer *eb;
3791 int slot;
3792 struct btrfs_root_item root_item;
3793 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003794 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003795
3796 path = btrfs_alloc_path();
3797 if (!path) {
3798 ret = -ENOMEM;
3799 goto out;
3800 }
3801
3802 key.objectid = 0;
3803 key.type = BTRFS_ROOT_ITEM_KEY;
3804 key.offset = 0;
3805
3806 max_key.objectid = (u64)-1;
3807 max_key.type = BTRFS_ROOT_ITEM_KEY;
3808 max_key.offset = (u64)-1;
3809
Stefan Behrens803b2f52013-08-15 17:11:21 +02003810 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003811 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003812 if (ret) {
3813 if (ret > 0)
3814 ret = 0;
3815 break;
3816 }
3817
3818 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3819 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3820 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3821 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3822 goto skip;
3823
3824 eb = path->nodes[0];
3825 slot = path->slots[0];
3826 item_size = btrfs_item_size_nr(eb, slot);
3827 if (item_size < sizeof(root_item))
3828 goto skip;
3829
Stefan Behrens803b2f52013-08-15 17:11:21 +02003830 read_extent_buffer(eb, &root_item,
3831 btrfs_item_ptr_offset(eb, slot),
3832 (int)sizeof(root_item));
3833 if (btrfs_root_refs(&root_item) == 0)
3834 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003835
3836 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3837 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3838 if (trans)
3839 goto update_tree;
3840
3841 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003842 /*
3843 * 1 - subvol uuid item
3844 * 1 - received_subvol uuid item
3845 */
3846 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3847 if (IS_ERR(trans)) {
3848 ret = PTR_ERR(trans);
3849 break;
3850 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003851 continue;
3852 } else {
3853 goto skip;
3854 }
3855update_tree:
3856 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003857 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3858 root_item.uuid,
3859 BTRFS_UUID_KEY_SUBVOL,
3860 key.objectid);
3861 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003862 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003863 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003864 break;
3865 }
3866 }
3867
3868 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003869 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3870 root_item.received_uuid,
3871 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3872 key.objectid);
3873 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003874 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003875 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003876 break;
3877 }
3878 }
3879
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003880skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003881 if (trans) {
3882 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003883 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003884 if (ret)
3885 break;
3886 }
3887
Stefan Behrens803b2f52013-08-15 17:11:21 +02003888 btrfs_release_path(path);
3889 if (key.offset < (u64)-1) {
3890 key.offset++;
3891 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3892 key.offset = 0;
3893 key.type = BTRFS_ROOT_ITEM_KEY;
3894 } else if (key.objectid < (u64)-1) {
3895 key.offset = 0;
3896 key.type = BTRFS_ROOT_ITEM_KEY;
3897 key.objectid++;
3898 } else {
3899 break;
3900 }
3901 cond_resched();
3902 }
3903
3904out:
3905 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003906 if (trans && !IS_ERR(trans))
3907 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003908 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003909 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003910 else
3911 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003912 up(&fs_info->uuid_tree_rescan_sem);
3913 return 0;
3914}
3915
Stefan Behrens70f80172013-08-15 17:11:23 +02003916/*
3917 * Callback for btrfs_uuid_tree_iterate().
3918 * returns:
3919 * 0 check succeeded, the entry is not outdated.
3920 * < 0 if an error occured.
3921 * > 0 if the check failed, which means the caller shall remove the entry.
3922 */
3923static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3924 u8 *uuid, u8 type, u64 subid)
3925{
3926 struct btrfs_key key;
3927 int ret = 0;
3928 struct btrfs_root *subvol_root;
3929
3930 if (type != BTRFS_UUID_KEY_SUBVOL &&
3931 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3932 goto out;
3933
3934 key.objectid = subid;
3935 key.type = BTRFS_ROOT_ITEM_KEY;
3936 key.offset = (u64)-1;
3937 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3938 if (IS_ERR(subvol_root)) {
3939 ret = PTR_ERR(subvol_root);
3940 if (ret == -ENOENT)
3941 ret = 1;
3942 goto out;
3943 }
3944
3945 switch (type) {
3946 case BTRFS_UUID_KEY_SUBVOL:
3947 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3948 ret = 1;
3949 break;
3950 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3951 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3952 BTRFS_UUID_SIZE))
3953 ret = 1;
3954 break;
3955 }
3956
3957out:
3958 return ret;
3959}
3960
3961static int btrfs_uuid_rescan_kthread(void *data)
3962{
3963 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3964 int ret;
3965
3966 /*
3967 * 1st step is to iterate through the existing UUID tree and
3968 * to delete all entries that contain outdated data.
3969 * 2nd step is to add all missing entries to the UUID tree.
3970 */
3971 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3972 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003973 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003974 up(&fs_info->uuid_tree_rescan_sem);
3975 return ret;
3976 }
3977 return btrfs_uuid_scan_kthread(data);
3978}
3979
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003980int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3981{
3982 struct btrfs_trans_handle *trans;
3983 struct btrfs_root *tree_root = fs_info->tree_root;
3984 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003985 struct task_struct *task;
3986 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003987
3988 /*
3989 * 1 - root node
3990 * 1 - root item
3991 */
3992 trans = btrfs_start_transaction(tree_root, 2);
3993 if (IS_ERR(trans))
3994 return PTR_ERR(trans);
3995
3996 uuid_root = btrfs_create_tree(trans, fs_info,
3997 BTRFS_UUID_TREE_OBJECTID);
3998 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02003999 ret = PTR_ERR(uuid_root);
4000 btrfs_abort_transaction(trans, tree_root, ret);
4001 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004002 }
4003
4004 fs_info->uuid_root = uuid_root;
4005
Stefan Behrens803b2f52013-08-15 17:11:21 +02004006 ret = btrfs_commit_transaction(trans, tree_root);
4007 if (ret)
4008 return ret;
4009
4010 down(&fs_info->uuid_tree_rescan_sem);
4011 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4012 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004013 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004014 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004015 up(&fs_info->uuid_tree_rescan_sem);
4016 return PTR_ERR(task);
4017 }
4018
4019 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004020}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004021
Stefan Behrens70f80172013-08-15 17:11:23 +02004022int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4023{
4024 struct task_struct *task;
4025
4026 down(&fs_info->uuid_tree_rescan_sem);
4027 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4028 if (IS_ERR(task)) {
4029 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004030 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004031 up(&fs_info->uuid_tree_rescan_sem);
4032 return PTR_ERR(task);
4033 }
4034
4035 return 0;
4036}
4037
Chris Mason8f18cf12008-04-25 16:53:30 -04004038/*
4039 * shrinking a device means finding all of the device extents past
4040 * the new size, and then following the back refs to the chunks.
4041 * The chunk relocation code actually frees the device extent
4042 */
4043int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4044{
4045 struct btrfs_trans_handle *trans;
4046 struct btrfs_root *root = device->dev_root;
4047 struct btrfs_dev_extent *dev_extent = NULL;
4048 struct btrfs_path *path;
4049 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004050 u64 chunk_objectid;
4051 u64 chunk_offset;
4052 int ret;
4053 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004054 int failed = 0;
4055 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004056 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004057 struct extent_buffer *l;
4058 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02004059 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004060 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004061 u64 old_size = btrfs_device_get_total_bytes(device);
4062 u64 diff = old_size - new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04004063
Stefan Behrens63a212a2012-11-05 18:29:28 +01004064 if (device->is_tgtdev_for_dev_replace)
4065 return -EINVAL;
4066
Chris Mason8f18cf12008-04-25 16:53:30 -04004067 path = btrfs_alloc_path();
4068 if (!path)
4069 return -ENOMEM;
4070
Chris Mason8f18cf12008-04-25 16:53:30 -04004071 path->reada = 2;
4072
Chris Mason7d9eb122008-07-08 14:19:17 -04004073 lock_chunks(root);
4074
Miao Xie7cc8e582014-09-03 21:35:38 +08004075 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04004076 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004077 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004078 spin_lock(&root->fs_info->free_chunk_lock);
4079 root->fs_info->free_chunk_space -= diff;
4080 spin_unlock(&root->fs_info->free_chunk_lock);
4081 }
Chris Mason7d9eb122008-07-08 14:19:17 -04004082 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004083
Josef Bacikba1bf482009-09-11 16:11:19 -04004084again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004085 key.objectid = device->devid;
4086 key.offset = (u64)-1;
4087 key.type = BTRFS_DEV_EXTENT_KEY;
4088
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004089 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04004090 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4091 if (ret < 0)
4092 goto done;
4093
4094 ret = btrfs_previous_item(root, path, 0, key.type);
4095 if (ret < 0)
4096 goto done;
4097 if (ret) {
4098 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004099 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004100 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004101 }
4102
4103 l = path->nodes[0];
4104 slot = path->slots[0];
4105 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4106
Josef Bacikba1bf482009-09-11 16:11:19 -04004107 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004108 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004109 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004110 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004111
4112 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4113 length = btrfs_dev_extent_length(l, dev_extent);
4114
Josef Bacikba1bf482009-09-11 16:11:19 -04004115 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004116 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004117 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004118 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004119
Chris Mason8f18cf12008-04-25 16:53:30 -04004120 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
4121 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004122 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004123
Zhao Leia688a042015-02-09 20:31:44 +08004124 ret = btrfs_relocate_chunk(root, chunk_objectid, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04004125 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004126 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004127 if (ret == -ENOSPC)
4128 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004129 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004130
4131 if (failed && !retried) {
4132 failed = 0;
4133 retried = true;
4134 goto again;
4135 } else if (failed && retried) {
4136 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004137 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004138 }
4139
Chris Balld6397ba2009-04-27 07:29:03 -04004140 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004141 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004142 if (IS_ERR(trans)) {
4143 ret = PTR_ERR(trans);
4144 goto done;
4145 }
4146
Chris Balld6397ba2009-04-27 07:29:03 -04004147 lock_chunks(root);
Filipe Manana53e489b2015-06-02 14:43:21 +01004148
4149 /*
4150 * We checked in the above loop all device extents that were already in
4151 * the device tree. However before we have updated the device's
4152 * total_bytes to the new size, we might have had chunk allocations that
4153 * have not complete yet (new block groups attached to transaction
4154 * handles), and therefore their device extents were not yet in the
4155 * device tree and we missed them in the loop above. So if we have any
4156 * pending chunk using a device extent that overlaps the device range
4157 * that we can not use anymore, commit the current transaction and
4158 * repeat the search on the device tree - this way we guarantee we will
4159 * not have chunks using device extents that end beyond 'new_size'.
4160 */
4161 if (!checked_pending_chunks) {
4162 u64 start = new_size;
4163 u64 len = old_size - new_size;
4164
4165 if (contains_pending_extent(trans, device, &start, len)) {
4166 unlock_chunks(root);
4167 checked_pending_chunks = true;
4168 failed = 0;
4169 retried = false;
4170 ret = btrfs_commit_transaction(trans, root);
4171 if (ret)
4172 goto done;
4173 goto again;
4174 }
4175 }
4176
Miao Xie7cc8e582014-09-03 21:35:38 +08004177 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004178 if (list_empty(&device->resized_list))
4179 list_add_tail(&device->resized_list,
4180 &root->fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004181
Chris Balld6397ba2009-04-27 07:29:03 -04004182 WARN_ON(diff > old_total);
4183 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4184 unlock_chunks(root);
Miao Xie2196d6e2014-09-03 21:35:41 +08004185
4186 /* Now btrfs_update_device() will change the on-disk size. */
4187 ret = btrfs_update_device(trans, device);
Chris Balld6397ba2009-04-27 07:29:03 -04004188 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004189done:
4190 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004191 if (ret) {
4192 lock_chunks(root);
4193 btrfs_device_set_total_bytes(device, old_size);
4194 if (device->writeable)
4195 device->fs_devices->total_rw_bytes += diff;
4196 spin_lock(&root->fs_info->free_chunk_lock);
4197 root->fs_info->free_chunk_space += diff;
4198 spin_unlock(&root->fs_info->free_chunk_lock);
4199 unlock_chunks(root);
4200 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004201 return ret;
4202}
4203
Li Zefan125ccb02011-12-08 15:07:24 +08004204static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004205 struct btrfs_key *key,
4206 struct btrfs_chunk *chunk, int item_size)
4207{
David Sterba6c417612011-04-13 15:41:04 +02004208 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004209 struct btrfs_disk_key disk_key;
4210 u32 array_size;
4211 u8 *ptr;
4212
Miao Xiefe48a5c2014-09-03 21:35:39 +08004213 lock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004214 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004215 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004216 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4217 unlock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004218 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004219 }
Chris Mason0b86a832008-03-24 15:01:56 -04004220
4221 ptr = super_copy->sys_chunk_array + array_size;
4222 btrfs_cpu_key_to_disk(&disk_key, key);
4223 memcpy(ptr, &disk_key, sizeof(disk_key));
4224 ptr += sizeof(disk_key);
4225 memcpy(ptr, chunk, item_size);
4226 item_size += sizeof(disk_key);
4227 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004228 unlock_chunks(root);
4229
Chris Mason0b86a832008-03-24 15:01:56 -04004230 return 0;
4231}
4232
Miao Xieb2117a32011-01-05 10:07:28 +00004233/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004234 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004235 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004236static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004237{
Arne Jansen73c5de02011-04-12 12:07:57 +02004238 const struct btrfs_device_info *di_a = a;
4239 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004240
Arne Jansen73c5de02011-04-12 12:07:57 +02004241 if (di_a->max_avail > di_b->max_avail)
4242 return -1;
4243 if (di_a->max_avail < di_b->max_avail)
4244 return 1;
4245 if (di_a->total_avail > di_b->total_avail)
4246 return -1;
4247 if (di_a->total_avail < di_b->total_avail)
4248 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004249 return 0;
4250}
4251
David Sterbae8c9f182015-01-02 18:23:10 +01004252static const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004253 [BTRFS_RAID_RAID10] = {
4254 .sub_stripes = 2,
4255 .dev_stripes = 1,
4256 .devs_max = 0, /* 0 == as many as possible */
4257 .devs_min = 4,
4258 .devs_increment = 2,
4259 .ncopies = 2,
4260 },
4261 [BTRFS_RAID_RAID1] = {
4262 .sub_stripes = 1,
4263 .dev_stripes = 1,
4264 .devs_max = 2,
4265 .devs_min = 2,
4266 .devs_increment = 2,
4267 .ncopies = 2,
4268 },
4269 [BTRFS_RAID_DUP] = {
4270 .sub_stripes = 1,
4271 .dev_stripes = 2,
4272 .devs_max = 1,
4273 .devs_min = 1,
4274 .devs_increment = 1,
4275 .ncopies = 2,
4276 },
4277 [BTRFS_RAID_RAID0] = {
4278 .sub_stripes = 1,
4279 .dev_stripes = 1,
4280 .devs_max = 0,
4281 .devs_min = 2,
4282 .devs_increment = 1,
4283 .ncopies = 1,
4284 },
4285 [BTRFS_RAID_SINGLE] = {
4286 .sub_stripes = 1,
4287 .dev_stripes = 1,
4288 .devs_max = 1,
4289 .devs_min = 1,
4290 .devs_increment = 1,
4291 .ncopies = 1,
4292 },
Chris Masone942f882013-02-20 14:06:05 -05004293 [BTRFS_RAID_RAID5] = {
4294 .sub_stripes = 1,
4295 .dev_stripes = 1,
4296 .devs_max = 0,
4297 .devs_min = 2,
4298 .devs_increment = 1,
4299 .ncopies = 2,
4300 },
4301 [BTRFS_RAID_RAID6] = {
4302 .sub_stripes = 1,
4303 .dev_stripes = 1,
4304 .devs_max = 0,
4305 .devs_min = 3,
4306 .devs_increment = 1,
4307 .ncopies = 3,
4308 },
Liu Bo31e50222012-11-21 14:18:10 +00004309};
4310
David Woodhouse53b381b2013-01-29 18:40:14 -05004311static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4312{
4313 /* TODO allow them to set a preferred stripe size */
4314 return 64 * 1024;
4315}
4316
4317static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4318{
Zhao Leiffe2d202015-01-20 15:11:44 +08004319 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004320 return;
4321
Miao Xieceda0862013-04-11 10:30:16 +00004322 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004323}
4324
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004325#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4326 - sizeof(struct btrfs_item) \
4327 - sizeof(struct btrfs_chunk)) \
4328 / sizeof(struct btrfs_stripe) + 1)
4329
4330#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4331 - 2 * sizeof(struct btrfs_disk_key) \
4332 - 2 * sizeof(struct btrfs_chunk)) \
4333 / sizeof(struct btrfs_stripe) + 1)
4334
Miao Xieb2117a32011-01-05 10:07:28 +00004335static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004336 struct btrfs_root *extent_root, u64 start,
4337 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004338{
4339 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004340 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4341 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004342 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004343 struct extent_map_tree *em_tree;
4344 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004345 struct btrfs_device_info *devices_info = NULL;
4346 u64 total_avail;
4347 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004348 int data_stripes; /* number of stripes that count for
4349 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004350 int sub_stripes; /* sub_stripes info for map */
4351 int dev_stripes; /* stripes per dev */
4352 int devs_max; /* max devs to use */
4353 int devs_min; /* min devs needed */
4354 int devs_increment; /* ndevs has to be a multiple of this */
4355 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004356 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004357 u64 max_stripe_size;
4358 u64 max_chunk_size;
4359 u64 stripe_size;
4360 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004361 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004362 int ndevs;
4363 int i;
4364 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004365 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004366
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004367 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004368
Miao Xieb2117a32011-01-05 10:07:28 +00004369 if (list_empty(&fs_devices->alloc_list))
4370 return -ENOSPC;
4371
Liu Bo31e50222012-11-21 14:18:10 +00004372 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004373
Liu Bo31e50222012-11-21 14:18:10 +00004374 sub_stripes = btrfs_raid_array[index].sub_stripes;
4375 dev_stripes = btrfs_raid_array[index].dev_stripes;
4376 devs_max = btrfs_raid_array[index].devs_max;
4377 devs_min = btrfs_raid_array[index].devs_min;
4378 devs_increment = btrfs_raid_array[index].devs_increment;
4379 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004380
4381 if (type & BTRFS_BLOCK_GROUP_DATA) {
4382 max_stripe_size = 1024 * 1024 * 1024;
4383 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004384 if (!devs_max)
4385 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004386 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004387 /* for larger filesystems, use larger metadata chunks */
4388 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4389 max_stripe_size = 1024 * 1024 * 1024;
4390 else
4391 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004392 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004393 if (!devs_max)
4394 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004395 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004396 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004397 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004398 if (!devs_max)
4399 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004400 } else {
David Sterba351fd352014-05-15 16:48:20 +02004401 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004402 type);
4403 BUG_ON(1);
4404 }
4405
4406 /* we don't want a chunk larger than 10% of writeable space */
4407 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4408 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004409
David Sterba31e818f2015-02-20 18:00:26 +01004410 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004411 GFP_NOFS);
4412 if (!devices_info)
4413 return -ENOMEM;
4414
Arne Jansen73c5de02011-04-12 12:07:57 +02004415 cur = fs_devices->alloc_list.next;
4416
4417 /*
4418 * in the first pass through the devices list, we gather information
4419 * about the available holes on each device.
4420 */
4421 ndevs = 0;
4422 while (cur != &fs_devices->alloc_list) {
4423 struct btrfs_device *device;
4424 u64 max_avail;
4425 u64 dev_offset;
4426
4427 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4428
4429 cur = cur->next;
4430
4431 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004432 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004433 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004434 continue;
4435 }
4436
Stefan Behrens63a212a2012-11-05 18:29:28 +01004437 if (!device->in_fs_metadata ||
4438 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004439 continue;
4440
4441 if (device->total_bytes > device->bytes_used)
4442 total_avail = device->total_bytes - device->bytes_used;
4443 else
4444 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004445
4446 /* If there is no space on this device, skip it. */
4447 if (total_avail == 0)
4448 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004449
Josef Bacik6df9a952013-06-27 13:22:46 -04004450 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004451 max_stripe_size * dev_stripes,
4452 &dev_offset, &max_avail);
4453 if (ret && ret != -ENOSPC)
4454 goto error;
4455
4456 if (ret == 0)
4457 max_avail = max_stripe_size * dev_stripes;
4458
4459 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4460 continue;
4461
Eric Sandeen063d0062013-01-31 00:55:01 +00004462 if (ndevs == fs_devices->rw_devices) {
4463 WARN(1, "%s: found more than %llu devices\n",
4464 __func__, fs_devices->rw_devices);
4465 break;
4466 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004467 devices_info[ndevs].dev_offset = dev_offset;
4468 devices_info[ndevs].max_avail = max_avail;
4469 devices_info[ndevs].total_avail = total_avail;
4470 devices_info[ndevs].dev = device;
4471 ++ndevs;
4472 }
4473
4474 /*
4475 * now sort the devices by hole size / available space
4476 */
4477 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4478 btrfs_cmp_device_info, NULL);
4479
4480 /* round down to number of usable stripes */
4481 ndevs -= ndevs % devs_increment;
4482
4483 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4484 ret = -ENOSPC;
4485 goto error;
4486 }
4487
4488 if (devs_max && ndevs > devs_max)
4489 ndevs = devs_max;
4490 /*
4491 * the primary goal is to maximize the number of stripes, so use as many
4492 * devices as possible, even if the stripes are not maximum sized.
4493 */
4494 stripe_size = devices_info[ndevs-1].max_avail;
4495 num_stripes = ndevs * dev_stripes;
4496
David Woodhouse53b381b2013-01-29 18:40:14 -05004497 /*
4498 * this will have to be fixed for RAID1 and RAID10 over
4499 * more drives
4500 */
4501 data_stripes = num_stripes / ncopies;
4502
David Woodhouse53b381b2013-01-29 18:40:14 -05004503 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4504 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4505 btrfs_super_stripesize(info->super_copy));
4506 data_stripes = num_stripes - 1;
4507 }
4508 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4509 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4510 btrfs_super_stripesize(info->super_copy));
4511 data_stripes = num_stripes - 2;
4512 }
Chris Mason86db2572013-02-20 16:23:40 -05004513
4514 /*
4515 * Use the number of data stripes to figure out how big this chunk
4516 * is really going to be in terms of logical address space,
4517 * and compare that answer with the max chunk size
4518 */
4519 if (stripe_size * data_stripes > max_chunk_size) {
4520 u64 mask = (1ULL << 24) - 1;
David Sterbab8b93ad2015-01-16 17:26:13 +01004521
4522 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004523
4524 /* bump the answer up to a 16MB boundary */
4525 stripe_size = (stripe_size + mask) & ~mask;
4526
4527 /* but don't go higher than the limits we found
4528 * while searching for free extents
4529 */
4530 if (stripe_size > devices_info[ndevs-1].max_avail)
4531 stripe_size = devices_info[ndevs-1].max_avail;
4532 }
4533
David Sterbab8b93ad2015-01-16 17:26:13 +01004534 stripe_size = div_u64(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004535
4536 /* align to BTRFS_STRIPE_LEN */
David Sterbab8b93ad2015-01-16 17:26:13 +01004537 stripe_size = div_u64(stripe_size, raid_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05004538 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004539
Miao Xieb2117a32011-01-05 10:07:28 +00004540 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4541 if (!map) {
4542 ret = -ENOMEM;
4543 goto error;
4544 }
4545 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004546
Arne Jansen73c5de02011-04-12 12:07:57 +02004547 for (i = 0; i < ndevs; ++i) {
4548 for (j = 0; j < dev_stripes; ++j) {
4549 int s = i * dev_stripes + j;
4550 map->stripes[s].dev = devices_info[i].dev;
4551 map->stripes[s].physical = devices_info[i].dev_offset +
4552 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004553 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004554 }
Chris Mason593060d2008-03-25 16:50:33 -04004555 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004556 map->stripe_len = raid_stripe_len;
4557 map->io_align = raid_stripe_len;
4558 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004559 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004560 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004561
David Woodhouse53b381b2013-01-29 18:40:14 -05004562 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004563
Arne Jansen73c5de02011-04-12 12:07:57 +02004564 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004565
David Sterba172ddd62011-04-21 00:48:27 +02004566 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004567 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004568 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004569 ret = -ENOMEM;
4570 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004571 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004572 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004573 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004574 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004575 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004576 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004577 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004578 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004579
Chris Mason0b86a832008-03-24 15:01:56 -04004580 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004581 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004582 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004583 if (!ret) {
4584 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4585 atomic_inc(&em->refs);
4586 }
Chris Mason890871b2009-09-02 16:24:52 -04004587 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004588 if (ret) {
4589 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004590 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004591 }
Yan Zheng2b820322008-11-17 21:11:30 -05004592
Josef Bacik04487482013-01-29 15:03:37 -05004593 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4594 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4595 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004596 if (ret)
4597 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004598
Miao Xie7cc8e582014-09-03 21:35:38 +08004599 for (i = 0; i < map->num_stripes; i++) {
4600 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4601 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4602 }
Miao Xie43530c42014-09-03 21:35:36 +08004603
Miao Xie1c116182014-09-03 21:35:37 +08004604 spin_lock(&extent_root->fs_info->free_chunk_lock);
4605 extent_root->fs_info->free_chunk_space -= (stripe_size *
4606 map->num_stripes);
4607 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4608
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004609 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004610 check_raid56_incompat_flag(extent_root->fs_info, type);
4611
Miao Xieb2117a32011-01-05 10:07:28 +00004612 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004613 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004614
Josef Bacik6df9a952013-06-27 13:22:46 -04004615error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004616 write_lock(&em_tree->lock);
4617 remove_extent_mapping(em_tree, em);
4618 write_unlock(&em_tree->lock);
4619
4620 /* One for our allocation */
4621 free_extent_map(em);
4622 /* One for the tree reference */
4623 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004624 /* One for the pending_chunks list reference */
4625 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004626error:
Miao Xieb2117a32011-01-05 10:07:28 +00004627 kfree(devices_info);
4628 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004629}
4630
Josef Bacik6df9a952013-06-27 13:22:46 -04004631int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004632 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004633 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004634{
Yan Zheng2b820322008-11-17 21:11:30 -05004635 struct btrfs_key key;
4636 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4637 struct btrfs_device *device;
4638 struct btrfs_chunk *chunk;
4639 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004640 struct extent_map_tree *em_tree;
4641 struct extent_map *em;
4642 struct map_lookup *map;
4643 size_t item_size;
4644 u64 dev_offset;
4645 u64 stripe_size;
4646 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004647 int ret;
4648
Josef Bacik6df9a952013-06-27 13:22:46 -04004649 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4650 read_lock(&em_tree->lock);
4651 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4652 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004653
Josef Bacik6df9a952013-06-27 13:22:46 -04004654 if (!em) {
4655 btrfs_crit(extent_root->fs_info, "unable to find logical "
4656 "%Lu len %Lu", chunk_offset, chunk_size);
4657 return -EINVAL;
4658 }
4659
4660 if (em->start != chunk_offset || em->len != chunk_size) {
4661 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004662 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004663 chunk_size, em->start, em->len);
4664 free_extent_map(em);
4665 return -EINVAL;
4666 }
4667
4668 map = (struct map_lookup *)em->bdev;
4669 item_size = btrfs_chunk_item_size(map->num_stripes);
4670 stripe_size = em->orig_block_len;
4671
4672 chunk = kzalloc(item_size, GFP_NOFS);
4673 if (!chunk) {
4674 ret = -ENOMEM;
4675 goto out;
4676 }
4677
4678 for (i = 0; i < map->num_stripes; i++) {
4679 device = map->stripes[i].dev;
4680 dev_offset = map->stripes[i].physical;
4681
Yan Zheng2b820322008-11-17 21:11:30 -05004682 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004683 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004684 goto out;
4685 ret = btrfs_alloc_dev_extent(trans, device,
4686 chunk_root->root_key.objectid,
4687 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4688 chunk_offset, dev_offset,
4689 stripe_size);
4690 if (ret)
4691 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004692 }
4693
Yan Zheng2b820322008-11-17 21:11:30 -05004694 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004695 for (i = 0; i < map->num_stripes; i++) {
4696 device = map->stripes[i].dev;
4697 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004698
4699 btrfs_set_stack_stripe_devid(stripe, device->devid);
4700 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4701 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4702 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004703 }
4704
4705 btrfs_set_stack_chunk_length(chunk, chunk_size);
4706 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4707 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4708 btrfs_set_stack_chunk_type(chunk, map->type);
4709 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4710 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4711 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4712 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4713 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4714
4715 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4716 key.type = BTRFS_CHUNK_ITEM_KEY;
4717 key.offset = chunk_offset;
4718
4719 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004720 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4721 /*
4722 * TODO: Cleanup of inserted chunk root in case of
4723 * failure.
4724 */
Li Zefan125ccb02011-12-08 15:07:24 +08004725 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004726 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004727 }
liubo1abe9b82011-03-24 11:18:59 +00004728
Josef Bacik6df9a952013-06-27 13:22:46 -04004729out:
Yan Zheng2b820322008-11-17 21:11:30 -05004730 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004731 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004732 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004733}
4734
4735/*
4736 * Chunk allocation falls into two parts. The first part does works
4737 * that make the new allocated chunk useable, but not do any operation
4738 * that modifies the chunk tree. The second part does the works that
4739 * require modifying the chunk tree. This division is important for the
4740 * bootstrap process of adding storage to a seed btrfs.
4741 */
4742int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4743 struct btrfs_root *extent_root, u64 type)
4744{
4745 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004746
Filipe Mananaa9629592015-05-18 19:11:40 +01004747 ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex));
Josef Bacik6df9a952013-06-27 13:22:46 -04004748 chunk_offset = find_next_chunk(extent_root->fs_info);
4749 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004750}
4751
Chris Masond3977122009-01-05 21:25:51 -05004752static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004753 struct btrfs_root *root,
4754 struct btrfs_device *device)
4755{
4756 u64 chunk_offset;
4757 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004758 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004759 struct btrfs_fs_info *fs_info = root->fs_info;
4760 struct btrfs_root *extent_root = fs_info->extent_root;
4761 int ret;
4762
Josef Bacik6df9a952013-06-27 13:22:46 -04004763 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004764 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004765 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4766 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004767 if (ret)
4768 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004769
Josef Bacik6df9a952013-06-27 13:22:46 -04004770 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004771 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004772 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4773 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004774 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004775}
4776
Miao Xied20983b2014-07-03 18:22:13 +08004777static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4778{
4779 int max_errors;
4780
4781 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4782 BTRFS_BLOCK_GROUP_RAID10 |
4783 BTRFS_BLOCK_GROUP_RAID5 |
4784 BTRFS_BLOCK_GROUP_DUP)) {
4785 max_errors = 1;
4786 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4787 max_errors = 2;
4788 } else {
4789 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004790 }
4791
Miao Xied20983b2014-07-03 18:22:13 +08004792 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05004793}
4794
4795int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4796{
4797 struct extent_map *em;
4798 struct map_lookup *map;
4799 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4800 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004801 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004802 int i;
4803
Chris Mason890871b2009-09-02 16:24:52 -04004804 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004805 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004806 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004807 if (!em)
4808 return 1;
4809
4810 map = (struct map_lookup *)em->bdev;
4811 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004812 if (map->stripes[i].dev->missing) {
4813 miss_ndevs++;
4814 continue;
4815 }
4816
Yan Zheng2b820322008-11-17 21:11:30 -05004817 if (!map->stripes[i].dev->writeable) {
4818 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004819 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004820 }
4821 }
Miao Xied20983b2014-07-03 18:22:13 +08004822
4823 /*
4824 * If the number of missing devices is larger than max errors,
4825 * we can not write the data into that chunk successfully, so
4826 * set it readonly.
4827 */
4828 if (miss_ndevs > btrfs_chunk_max_errors(map))
4829 readonly = 1;
4830end:
Yan Zheng2b820322008-11-17 21:11:30 -05004831 free_extent_map(em);
4832 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004833}
4834
4835void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4836{
David Sterbaa8067e02011-04-21 00:34:43 +02004837 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004838}
4839
4840void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4841{
4842 struct extent_map *em;
4843
Chris Masond3977122009-01-05 21:25:51 -05004844 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004845 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004846 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4847 if (em)
4848 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004849 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004850 if (!em)
4851 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004852 /* once for us */
4853 free_extent_map(em);
4854 /* once for the tree */
4855 free_extent_map(em);
4856 }
4857}
4858
Stefan Behrens5d964052012-11-05 14:59:07 +01004859int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004860{
Stefan Behrens5d964052012-11-05 14:59:07 +01004861 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004862 struct extent_map *em;
4863 struct map_lookup *map;
4864 struct extent_map_tree *em_tree = &map_tree->map_tree;
4865 int ret;
4866
Chris Mason890871b2009-09-02 16:24:52 -04004867 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004868 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004869 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004870
Josef Bacikfb7669b2013-04-23 10:53:18 -04004871 /*
4872 * We could return errors for these cases, but that could get ugly and
4873 * we'd probably do the same thing which is just not do anything else
4874 * and exit, so return 1 so the callers don't try to use other copies.
4875 */
4876 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004877 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004878 logical+len);
4879 return 1;
4880 }
4881
4882 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004883 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004884 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004885 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004886 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004887 return 1;
4888 }
4889
Chris Masonf1885912008-04-09 16:28:12 -04004890 map = (struct map_lookup *)em->bdev;
4891 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4892 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004893 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4894 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004895 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4896 ret = 2;
4897 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4898 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004899 else
4900 ret = 1;
4901 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004902
4903 btrfs_dev_replace_lock(&fs_info->dev_replace);
4904 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4905 ret++;
4906 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4907
Chris Masonf1885912008-04-09 16:28:12 -04004908 return ret;
4909}
4910
David Woodhouse53b381b2013-01-29 18:40:14 -05004911unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4912 struct btrfs_mapping_tree *map_tree,
4913 u64 logical)
4914{
4915 struct extent_map *em;
4916 struct map_lookup *map;
4917 struct extent_map_tree *em_tree = &map_tree->map_tree;
4918 unsigned long len = root->sectorsize;
4919
4920 read_lock(&em_tree->lock);
4921 em = lookup_extent_mapping(em_tree, logical, len);
4922 read_unlock(&em_tree->lock);
4923 BUG_ON(!em);
4924
4925 BUG_ON(em->start > logical || em->start + em->len < logical);
4926 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08004927 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05004928 len = map->stripe_len * nr_data_stripes(map);
David Woodhouse53b381b2013-01-29 18:40:14 -05004929 free_extent_map(em);
4930 return len;
4931}
4932
4933int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4934 u64 logical, u64 len, int mirror_num)
4935{
4936 struct extent_map *em;
4937 struct map_lookup *map;
4938 struct extent_map_tree *em_tree = &map_tree->map_tree;
4939 int ret = 0;
4940
4941 read_lock(&em_tree->lock);
4942 em = lookup_extent_mapping(em_tree, logical, len);
4943 read_unlock(&em_tree->lock);
4944 BUG_ON(!em);
4945
4946 BUG_ON(em->start > logical || em->start + em->len < logical);
4947 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08004948 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05004949 ret = 1;
4950 free_extent_map(em);
4951 return ret;
4952}
4953
Stefan Behrens30d98612012-11-06 14:52:18 +01004954static int find_live_mirror(struct btrfs_fs_info *fs_info,
4955 struct map_lookup *map, int first, int num,
4956 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004957{
4958 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004959 int tolerance;
4960 struct btrfs_device *srcdev;
4961
4962 if (dev_replace_is_ongoing &&
4963 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4964 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4965 srcdev = fs_info->dev_replace.srcdev;
4966 else
4967 srcdev = NULL;
4968
4969 /*
4970 * try to avoid the drive that is the source drive for a
4971 * dev-replace procedure, only choose it if no other non-missing
4972 * mirror is available
4973 */
4974 for (tolerance = 0; tolerance < 2; tolerance++) {
4975 if (map->stripes[optimal].dev->bdev &&
4976 (tolerance || map->stripes[optimal].dev != srcdev))
4977 return optimal;
4978 for (i = first; i < first + num; i++) {
4979 if (map->stripes[i].dev->bdev &&
4980 (tolerance || map->stripes[i].dev != srcdev))
4981 return i;
4982 }
Chris Masondfe25022008-05-13 13:46:40 -04004983 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004984
Chris Masondfe25022008-05-13 13:46:40 -04004985 /* we couldn't find one that doesn't fail. Just return something
4986 * and the io error handling code will clean up eventually
4987 */
4988 return optimal;
4989}
4990
David Woodhouse53b381b2013-01-29 18:40:14 -05004991static inline int parity_smaller(u64 a, u64 b)
4992{
4993 return a > b;
4994}
4995
4996/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004997static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05004998{
4999 struct btrfs_bio_stripe s;
5000 int i;
5001 u64 l;
5002 int again = 1;
5003
5004 while (again) {
5005 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005006 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005007 if (parity_smaller(bbio->raid_map[i],
5008 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005009 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005010 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005011 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005012 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005013 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005014 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005015
David Woodhouse53b381b2013-01-29 18:40:14 -05005016 again = 1;
5017 }
5018 }
5019 }
5020}
5021
Zhao Lei6e9606d2015-01-20 15:11:34 +08005022static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5023{
5024 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005025 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005026 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005027 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005028 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005029 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005030 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005031 /*
5032 * plus the raid_map, which includes both the tgt dev
5033 * and the stripes
5034 */
5035 sizeof(u64) * (total_stripes),
Zhao Lei6e9606d2015-01-20 15:11:34 +08005036 GFP_NOFS);
5037 if (!bbio)
5038 return NULL;
5039
5040 atomic_set(&bbio->error, 0);
5041 atomic_set(&bbio->refs, 1);
5042
5043 return bbio;
5044}
5045
5046void btrfs_get_bbio(struct btrfs_bio *bbio)
5047{
5048 WARN_ON(!atomic_read(&bbio->refs));
5049 atomic_inc(&bbio->refs);
5050}
5051
5052void btrfs_put_bbio(struct btrfs_bio *bbio)
5053{
5054 if (!bbio)
5055 return;
5056 if (atomic_dec_and_test(&bbio->refs))
5057 kfree(bbio);
5058}
5059
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005060static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005061 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005062 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005063 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005064{
5065 struct extent_map *em;
5066 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005067 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04005068 struct extent_map_tree *em_tree = &map_tree->map_tree;
5069 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005070 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005071 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04005072 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005073 u64 stripe_nr_orig;
5074 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05005075 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005076 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005077 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005078 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005079 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005080 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005081 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005082 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005083 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5084 int dev_replace_is_ongoing = 0;
5085 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005086 int patch_the_first_stripe_for_dev_replace = 0;
5087 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005088 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005089
Chris Mason890871b2009-09-02 16:24:52 -04005090 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005091 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04005092 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04005093
Chris Mason3b951512008-04-17 11:29:12 -04005094 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005095 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005096 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04005097 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04005098 }
Chris Mason0b86a832008-03-24 15:01:56 -04005099
Josef Bacik9bb91872013-04-19 23:45:33 -04005100 if (em->start > logical || em->start + em->len < logical) {
5101 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02005102 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04005103 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08005104 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005105 return -EINVAL;
5106 }
5107
Chris Mason0b86a832008-03-24 15:01:56 -04005108 map = (struct map_lookup *)em->bdev;
5109 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005110
David Woodhouse53b381b2013-01-29 18:40:14 -05005111 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005112 stripe_nr = offset;
5113 /*
5114 * stripe_nr counts the total number of stripes we have to stride
5115 * to get to this block
5116 */
David Sterba47c57132015-02-20 18:43:47 +01005117 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005118
David Woodhouse53b381b2013-01-29 18:40:14 -05005119 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005120 BUG_ON(offset < stripe_offset);
5121
5122 /* stripe_offset is the offset of this block in its stripe*/
5123 stripe_offset = offset - stripe_offset;
5124
David Woodhouse53b381b2013-01-29 18:40:14 -05005125 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005126 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005127 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5128 raid56_full_stripe_start = offset;
5129
5130 /* allow a write of a full stripe, but make sure we don't
5131 * allow straddling of stripes
5132 */
David Sterba47c57132015-02-20 18:43:47 +01005133 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5134 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005135 raid56_full_stripe_start *= full_stripe_len;
5136 }
5137
5138 if (rw & REQ_DISCARD) {
5139 /* we don't discard raid56 yet */
Zhao Leiffe2d202015-01-20 15:11:44 +08005140 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005141 ret = -EOPNOTSUPP;
5142 goto out;
5143 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005144 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005145 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5146 u64 max_len;
5147 /* For writes to RAID[56], allow a full stripeset across all disks.
5148 For other RAID types and for RAID[56] reads, just allow a single
5149 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005150 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
David Woodhouse53b381b2013-01-29 18:40:14 -05005151 (rw & REQ_WRITE)) {
5152 max_len = stripe_len * nr_data_stripes(map) -
5153 (offset - raid56_full_stripe_start);
5154 } else {
5155 /* we limit the length of each bio to what fits in a stripe */
5156 max_len = stripe_len - stripe_offset;
5157 }
5158 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005159 } else {
5160 *length = em->len - offset;
5161 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005162
David Woodhouse53b381b2013-01-29 18:40:14 -05005163 /* This is for when we're called from btrfs_merge_bio_hook() and all
5164 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005165 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005166 goto out;
5167
Stefan Behrens472262f2012-11-06 14:43:46 +01005168 btrfs_dev_replace_lock(dev_replace);
5169 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5170 if (!dev_replace_is_ongoing)
5171 btrfs_dev_replace_unlock(dev_replace);
5172
Stefan Behrensad6d6202012-11-06 15:06:47 +01005173 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5174 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5175 dev_replace->tgtdev != NULL) {
5176 /*
5177 * in dev-replace case, for repair case (that's the only
5178 * case where the mirror is selected explicitly when
5179 * calling btrfs_map_block), blocks left of the left cursor
5180 * can also be read from the target drive.
5181 * For REQ_GET_READ_MIRRORS, the target drive is added as
5182 * the last one to the array of stripes. For READ, it also
5183 * needs to be supported using the same mirror number.
5184 * If the requested block is not left of the left cursor,
5185 * EIO is returned. This can happen because btrfs_num_copies()
5186 * returns one more in the dev-replace case.
5187 */
5188 u64 tmp_length = *length;
5189 struct btrfs_bio *tmp_bbio = NULL;
5190 int tmp_num_stripes;
5191 u64 srcdev_devid = dev_replace->srcdev->devid;
5192 int index_srcdev = 0;
5193 int found = 0;
5194 u64 physical_of_found = 0;
5195
5196 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005197 logical, &tmp_length, &tmp_bbio, 0, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005198 if (ret) {
5199 WARN_ON(tmp_bbio != NULL);
5200 goto out;
5201 }
5202
5203 tmp_num_stripes = tmp_bbio->num_stripes;
5204 if (mirror_num > tmp_num_stripes) {
5205 /*
5206 * REQ_GET_READ_MIRRORS does not contain this
5207 * mirror, that means that the requested area
5208 * is not left of the left cursor
5209 */
5210 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005211 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005212 goto out;
5213 }
5214
5215 /*
5216 * process the rest of the function using the mirror_num
5217 * of the source drive. Therefore look it up first.
5218 * At the end, patch the device pointer to the one of the
5219 * target drive.
5220 */
5221 for (i = 0; i < tmp_num_stripes; i++) {
5222 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5223 /*
5224 * In case of DUP, in order to keep it
5225 * simple, only add the mirror with the
5226 * lowest physical address
5227 */
5228 if (found &&
5229 physical_of_found <=
5230 tmp_bbio->stripes[i].physical)
5231 continue;
5232 index_srcdev = i;
5233 found = 1;
5234 physical_of_found =
5235 tmp_bbio->stripes[i].physical;
5236 }
5237 }
5238
5239 if (found) {
5240 mirror_num = index_srcdev + 1;
5241 patch_the_first_stripe_for_dev_replace = 1;
5242 physical_to_patch_in_first_stripe = physical_of_found;
5243 } else {
5244 WARN_ON(1);
5245 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005246 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005247 goto out;
5248 }
5249
Zhao Lei6e9606d2015-01-20 15:11:34 +08005250 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005251 } else if (mirror_num > map->num_stripes) {
5252 mirror_num = 0;
5253 }
5254
Chris Masonf2d8d742008-04-21 10:03:05 -04005255 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005256 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005257 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005258 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01005259 stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005260 stripe_end_offset = stripe_nr_end * map->stripe_len -
5261 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005262
Li Dongyangfce3bb92011-03-24 10:24:26 +00005263 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5264 if (rw & REQ_DISCARD)
5265 num_stripes = min_t(u64, map->num_stripes,
5266 stripe_nr_end - stripe_nr_orig);
David Sterba47c57132015-02-20 18:43:47 +01005267 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5268 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005269 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5270 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005271 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005272 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005273 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005274 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005275 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005276 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005277 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005278 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005279 current->pid % map->num_stripes,
5280 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005281 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005282 }
Chris Mason2fff7342008-04-29 14:12:09 -04005283
Chris Mason611f0e02008-04-03 16:29:03 -04005284 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005285 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005286 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005287 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005288 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005289 } else {
5290 mirror_num = 1;
5291 }
Chris Mason2fff7342008-04-29 14:12:09 -04005292
Chris Mason321aecc2008-04-16 10:49:51 -04005293 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005294 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005295
David Sterba47c57132015-02-20 18:43:47 +01005296 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005297 stripe_index *= map->sub_stripes;
5298
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005299 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005300 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005301 else if (rw & REQ_DISCARD)
5302 num_stripes = min_t(u64, map->sub_stripes *
5303 (stripe_nr_end - stripe_nr_orig),
5304 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005305 else if (mirror_num)
5306 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005307 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005308 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005309 stripe_index = find_live_mirror(fs_info, map,
5310 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005311 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005312 current->pid % map->sub_stripes,
5313 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005314 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005315 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005316
Zhao Leiffe2d202015-01-20 15:11:44 +08005317 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005318 if (need_raid_map &&
Miao Xieaf8e2d12014-10-23 14:42:50 +08005319 ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5320 mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005321 /* push stripe_nr back to the start of the full stripe */
David Sterbab8b93ad2015-01-16 17:26:13 +01005322 stripe_nr = div_u64(raid56_full_stripe_start,
5323 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005324
5325 /* RAID[56] write or recovery. Return all stripes */
5326 num_stripes = map->num_stripes;
5327 max_errors = nr_parity_stripes(map);
5328
David Woodhouse53b381b2013-01-29 18:40:14 -05005329 *length = map->stripe_len;
5330 stripe_index = 0;
5331 stripe_offset = 0;
5332 } else {
5333 /*
5334 * Mirror #0 or #1 means the original data block.
5335 * Mirror #2 is RAID5 parity block.
5336 * Mirror #3 is RAID6 Q block.
5337 */
David Sterba47c57132015-02-20 18:43:47 +01005338 stripe_nr = div_u64_rem(stripe_nr,
5339 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005340 if (mirror_num > 1)
5341 stripe_index = nr_data_stripes(map) +
5342 mirror_num - 2;
5343
5344 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005345 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5346 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005347 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5348 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5349 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005350 }
Chris Mason8790d502008-04-03 16:29:03 -04005351 } else {
5352 /*
David Sterba47c57132015-02-20 18:43:47 +01005353 * after this, stripe_nr is the number of stripes on this
5354 * device we have to walk to find the data, and stripe_index is
5355 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005356 */
David Sterba47c57132015-02-20 18:43:47 +01005357 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5358 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005359 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005360 }
Chris Mason593060d2008-03-25 16:50:33 -04005361 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005362
Stefan Behrens472262f2012-11-06 14:43:46 +01005363 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005364 if (dev_replace_is_ongoing) {
5365 if (rw & (REQ_WRITE | REQ_DISCARD))
5366 num_alloc_stripes <<= 1;
5367 if (rw & REQ_GET_READ_MIRRORS)
5368 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005369 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005370 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005371
Zhao Lei6e9606d2015-01-20 15:11:34 +08005372 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005373 if (!bbio) {
5374 ret = -ENOMEM;
5375 goto out;
5376 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005377 if (dev_replace_is_ongoing)
5378 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005379
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005380 /* build raid_map */
Zhao Leiffe2d202015-01-20 15:11:44 +08005381 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005382 need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5383 mirror_num > 1)) {
5384 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005385 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005386
5387 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5388 sizeof(struct btrfs_bio_stripe) *
5389 num_alloc_stripes +
5390 sizeof(int) * tgtdev_indexes);
5391
5392 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005393 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005394
5395 /* Fill in the logical address of each stripe */
5396 tmp = stripe_nr * nr_data_stripes(map);
5397 for (i = 0; i < nr_data_stripes(map); i++)
5398 bbio->raid_map[(i+rot) % num_stripes] =
5399 em->start + (tmp + i) * map->stripe_len;
5400
5401 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5402 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5403 bbio->raid_map[(i+rot+1) % num_stripes] =
5404 RAID6_Q_STRIPE;
5405 }
5406
Li Dongyangfce3bb92011-03-24 10:24:26 +00005407 if (rw & REQ_DISCARD) {
David Sterba9d644a62015-02-20 18:42:11 +01005408 u32 factor = 0;
5409 u32 sub_stripes = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005410 u64 stripes_per_dev = 0;
5411 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005412 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005413
5414 if (map->type &
5415 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5416 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5417 sub_stripes = 1;
5418 else
5419 sub_stripes = map->sub_stripes;
5420
5421 factor = map->num_stripes / sub_stripes;
5422 stripes_per_dev = div_u64_rem(stripe_nr_end -
5423 stripe_nr_orig,
5424 factor,
5425 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005426 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5427 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005428 }
5429
Li Dongyangfce3bb92011-03-24 10:24:26 +00005430 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005431 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005432 map->stripes[stripe_index].physical +
5433 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005434 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005435
Li Zefanec9ef7a2011-12-01 14:06:42 +08005436 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5437 BTRFS_BLOCK_GROUP_RAID10)) {
5438 bbio->stripes[i].length = stripes_per_dev *
5439 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005440
Li Zefanec9ef7a2011-12-01 14:06:42 +08005441 if (i / sub_stripes < remaining_stripes)
5442 bbio->stripes[i].length +=
5443 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005444
5445 /*
5446 * Special for the first stripe and
5447 * the last stripe:
5448 *
5449 * |-------|...|-------|
5450 * |----------|
5451 * off end_off
5452 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005453 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005454 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005455 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005456
5457 if (stripe_index >= last_stripe &&
5458 stripe_index <= (last_stripe +
5459 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005460 bbio->stripes[i].length -=
5461 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005462
Li Zefanec9ef7a2011-12-01 14:06:42 +08005463 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005464 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005465 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005466 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005467
5468 stripe_index++;
5469 if (stripe_index == map->num_stripes) {
5470 /* This could only happen for RAID0/10 */
5471 stripe_index = 0;
5472 stripe_nr++;
5473 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005474 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005475 } else {
5476 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005477 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005478 map->stripes[stripe_index].physical +
5479 stripe_offset +
5480 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005481 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005482 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005483 stripe_index++;
5484 }
Chris Mason593060d2008-03-25 16:50:33 -04005485 }
Li Zefande11cc12011-12-01 12:55:47 +08005486
Miao Xied20983b2014-07-03 18:22:13 +08005487 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5488 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005489
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005490 if (bbio->raid_map)
5491 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005492
Miao Xie2c8cdd62014-11-14 16:06:25 +08005493 tgtdev_indexes = 0;
Stefan Behrens472262f2012-11-06 14:43:46 +01005494 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5495 dev_replace->tgtdev != NULL) {
5496 int index_where_to_add;
5497 u64 srcdev_devid = dev_replace->srcdev->devid;
5498
5499 /*
5500 * duplicate the write operations while the dev replace
5501 * procedure is running. Since the copying of the old disk
5502 * to the new disk takes place at run time while the
5503 * filesystem is mounted writable, the regular write
5504 * operations to the old disk have to be duplicated to go
5505 * to the new disk as well.
5506 * Note that device->missing is handled by the caller, and
5507 * that the write to the old disk is already set up in the
5508 * stripes array.
5509 */
5510 index_where_to_add = num_stripes;
5511 for (i = 0; i < num_stripes; i++) {
5512 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5513 /* write to new disk, too */
5514 struct btrfs_bio_stripe *new =
5515 bbio->stripes + index_where_to_add;
5516 struct btrfs_bio_stripe *old =
5517 bbio->stripes + i;
5518
5519 new->physical = old->physical;
5520 new->length = old->length;
5521 new->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005522 bbio->tgtdev_map[i] = index_where_to_add;
Stefan Behrens472262f2012-11-06 14:43:46 +01005523 index_where_to_add++;
5524 max_errors++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005525 tgtdev_indexes++;
Stefan Behrens472262f2012-11-06 14:43:46 +01005526 }
5527 }
5528 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005529 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5530 dev_replace->tgtdev != NULL) {
5531 u64 srcdev_devid = dev_replace->srcdev->devid;
5532 int index_srcdev = 0;
5533 int found = 0;
5534 u64 physical_of_found = 0;
5535
5536 /*
5537 * During the dev-replace procedure, the target drive can
5538 * also be used to read data in case it is needed to repair
5539 * a corrupt block elsewhere. This is possible if the
5540 * requested area is left of the left cursor. In this area,
5541 * the target drive is a full copy of the source drive.
5542 */
5543 for (i = 0; i < num_stripes; i++) {
5544 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5545 /*
5546 * In case of DUP, in order to keep it
5547 * simple, only add the mirror with the
5548 * lowest physical address
5549 */
5550 if (found &&
5551 physical_of_found <=
5552 bbio->stripes[i].physical)
5553 continue;
5554 index_srcdev = i;
5555 found = 1;
5556 physical_of_found = bbio->stripes[i].physical;
5557 }
5558 }
5559 if (found) {
David Sterba258ece02015-02-24 19:45:15 +01005560 if (physical_of_found + map->stripe_len <=
Stefan Behrensad6d6202012-11-06 15:06:47 +01005561 dev_replace->cursor_left) {
5562 struct btrfs_bio_stripe *tgtdev_stripe =
5563 bbio->stripes + num_stripes;
5564
5565 tgtdev_stripe->physical = physical_of_found;
5566 tgtdev_stripe->length =
5567 bbio->stripes[index_srcdev].length;
5568 tgtdev_stripe->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005569 bbio->tgtdev_map[index_srcdev] = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005570
Miao Xie2c8cdd62014-11-14 16:06:25 +08005571 tgtdev_indexes++;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005572 num_stripes++;
5573 }
5574 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005575 }
5576
Li Zefande11cc12011-12-01 12:55:47 +08005577 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005578 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005579 bbio->num_stripes = num_stripes;
5580 bbio->max_errors = max_errors;
5581 bbio->mirror_num = mirror_num;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005582 bbio->num_tgtdevs = tgtdev_indexes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005583
5584 /*
5585 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5586 * mirror_num == num_stripes + 1 && dev_replace target drive is
5587 * available as a mirror
5588 */
5589 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5590 WARN_ON(num_stripes > 1);
5591 bbio->stripes[0].dev = dev_replace->tgtdev;
5592 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5593 bbio->mirror_num = map->num_stripes + 1;
5594 }
Chris Masoncea9e442008-04-09 16:28:12 -04005595out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005596 if (dev_replace_is_ongoing)
5597 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005598 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005599 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005600}
5601
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005602int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005603 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005604 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005605{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005606 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005607 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005608}
5609
Miao Xieaf8e2d12014-10-23 14:42:50 +08005610/* For Scrub/replace */
5611int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw,
5612 u64 logical, u64 *length,
5613 struct btrfs_bio **bbio_ret, int mirror_num,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005614 int need_raid_map)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005615{
5616 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005617 mirror_num, need_raid_map);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005618}
5619
Yan Zhenga512bbf2008-12-08 16:46:26 -05005620int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5621 u64 chunk_start, u64 physical, u64 devid,
5622 u64 **logical, int *naddrs, int *stripe_len)
5623{
5624 struct extent_map_tree *em_tree = &map_tree->map_tree;
5625 struct extent_map *em;
5626 struct map_lookup *map;
5627 u64 *buf;
5628 u64 bytenr;
5629 u64 length;
5630 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005631 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005632 int i, j, nr = 0;
5633
Chris Mason890871b2009-09-02 16:24:52 -04005634 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005635 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005636 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005637
Josef Bacik835d9742013-03-19 12:13:25 -04005638 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005639 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005640 chunk_start);
5641 return -EIO;
5642 }
5643
5644 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005645 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005646 em->start, chunk_start);
5647 free_extent_map(em);
5648 return -EIO;
5649 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005650 map = (struct map_lookup *)em->bdev;
5651
5652 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005653 rmap_len = map->stripe_len;
5654
Yan Zhenga512bbf2008-12-08 16:46:26 -05005655 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005656 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005657 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005658 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005659 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005660 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005661 rmap_len = map->stripe_len * nr_data_stripes(map);
5662 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005663
David Sterba31e818f2015-02-20 18:00:26 +01005664 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005665 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005666
5667 for (i = 0; i < map->num_stripes; i++) {
5668 if (devid && map->stripes[i].dev->devid != devid)
5669 continue;
5670 if (map->stripes[i].physical > physical ||
5671 map->stripes[i].physical + length <= physical)
5672 continue;
5673
5674 stripe_nr = physical - map->stripes[i].physical;
David Sterbab8b93ad2015-01-16 17:26:13 +01005675 stripe_nr = div_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005676
5677 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5678 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01005679 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005680 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5681 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005682 } /* else if RAID[56], multiply by nr_data_stripes().
5683 * Alternatively, just use rmap_len below instead of
5684 * map->stripe_len */
5685
5686 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005687 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005688 for (j = 0; j < nr; j++) {
5689 if (buf[j] == bytenr)
5690 break;
5691 }
Chris Mason934d3752008-12-08 16:43:10 -05005692 if (j == nr) {
5693 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005694 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005695 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005696 }
5697
Yan Zhenga512bbf2008-12-08 16:46:26 -05005698 *logical = buf;
5699 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005700 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005701
5702 free_extent_map(em);
5703 return 0;
5704}
5705
Miao Xie8408c712014-06-19 10:42:55 +08005706static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5707{
5708 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5709 bio_endio_nodec(bio, err);
5710 else
5711 bio_endio(bio, err);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005712 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005713}
5714
Jan Schmidta1d3c472011-08-04 17:15:33 +02005715static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005716{
Chris Mason9be33952013-05-17 18:30:14 -04005717 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005718 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005719
Stefan Behrens442a4f62012-05-25 16:06:08 +02005720 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005721 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005722 if (err == -EIO || err == -EREMOTEIO) {
5723 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005724 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08005725 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005726
5727 BUG_ON(stripe_index >= bbio->num_stripes);
5728 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005729 if (dev->bdev) {
5730 if (bio->bi_rw & WRITE)
5731 btrfs_dev_stat_inc(dev,
5732 BTRFS_DEV_STAT_WRITE_ERRS);
5733 else
5734 btrfs_dev_stat_inc(dev,
5735 BTRFS_DEV_STAT_READ_ERRS);
5736 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5737 btrfs_dev_stat_inc(dev,
5738 BTRFS_DEV_STAT_FLUSH_ERRS);
5739 btrfs_dev_stat_print_on_error(dev);
5740 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005741 }
5742 }
Chris Mason8790d502008-04-03 16:29:03 -04005743
Jan Schmidta1d3c472011-08-04 17:15:33 +02005744 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005745 is_orig_bio = 1;
5746
Miao Xiec404e0d2014-01-30 16:46:55 +08005747 btrfs_bio_counter_dec(bbio->fs_info);
5748
Jan Schmidta1d3c472011-08-04 17:15:33 +02005749 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005750 if (!is_orig_bio) {
5751 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005752 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005753 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005754
Jan Schmidta1d3c472011-08-04 17:15:33 +02005755 bio->bi_private = bbio->private;
5756 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005757 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005758 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005759 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005760 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005761 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005762 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005763 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005764 /*
5765 * this bio is actually up to date, we didn't
5766 * go over the max number of errors
5767 */
5768 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005769 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005770 }
Miao Xiec55f1392014-06-19 10:42:54 +08005771
Miao Xie8408c712014-06-19 10:42:55 +08005772 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005773 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005774 bio_put(bio);
5775 }
Chris Mason8790d502008-04-03 16:29:03 -04005776}
5777
Chris Mason8b712842008-06-11 16:50:36 -04005778/*
5779 * see run_scheduled_bios for a description of why bios are collected for
5780 * async submit.
5781 *
5782 * This will add one bio to the pending list for a device and make sure
5783 * the work struct is scheduled.
5784 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005785static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5786 struct btrfs_device *device,
5787 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005788{
5789 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005790 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005791
David Woodhouse53b381b2013-01-29 18:40:14 -05005792 if (device->missing || !device->bdev) {
5793 bio_endio(bio, -EIO);
5794 return;
5795 }
5796
Chris Mason8b712842008-06-11 16:50:36 -04005797 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005798 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005799 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005800 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005801 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005802 return;
Chris Mason8b712842008-06-11 16:50:36 -04005803 }
5804
5805 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005806 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005807 * higher layers. Otherwise, the async bio makes it appear we have
5808 * made progress against dirty pages when we've really just put it
5809 * on a queue for later
5810 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005811 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005812 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005813 bio->bi_next = NULL;
5814 bio->bi_rw |= rw;
5815
5816 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005817 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005818 pending_bios = &device->pending_sync_bios;
5819 else
5820 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005821
Chris Masonffbd5172009-04-20 15:50:09 -04005822 if (pending_bios->tail)
5823 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005824
Chris Masonffbd5172009-04-20 15:50:09 -04005825 pending_bios->tail = bio;
5826 if (!pending_bios->head)
5827 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005828 if (device->running_pending)
5829 should_queue = 0;
5830
5831 spin_unlock(&device->io_lock);
5832
5833 if (should_queue)
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005834 btrfs_queue_work(root->fs_info->submit_workers,
5835 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005836}
5837
Josef Bacikde1ee922012-10-19 16:50:56 -04005838static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5839 sector_t sector)
5840{
5841 struct bio_vec *prev;
5842 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005843 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005844 struct bvec_merge_data bvm = {
5845 .bi_bdev = bdev,
5846 .bi_sector = sector,
5847 .bi_rw = bio->bi_rw,
5848 };
5849
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305850 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005851 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005852
5853 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005854 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005855 return 0;
5856
5857 if (!q->merge_bvec_fn)
5858 return 1;
5859
Kent Overstreet4f024f32013-10-11 15:44:27 -07005860 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005861 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5862 return 0;
5863 return 1;
5864}
5865
5866static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5867 struct bio *bio, u64 physical, int dev_nr,
5868 int rw, int async)
5869{
5870 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5871
5872 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005873 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005874 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005875 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005876#ifdef DEBUG
5877 {
5878 struct rcu_string *name;
5879
5880 rcu_read_lock();
5881 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005882 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005883 "(%s id %llu), size=%u\n", rw,
Fabian Frederick1b6e4462014-09-24 20:23:05 +02005884 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
5885 name->str, dev->devid, bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04005886 rcu_read_unlock();
5887 }
5888#endif
5889 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005890
5891 btrfs_bio_counter_inc_noblocked(root->fs_info);
5892
Josef Bacikde1ee922012-10-19 16:50:56 -04005893 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005894 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005895 else
5896 btrfsic_submit_bio(rw, bio);
5897}
5898
5899static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5900 struct bio *first_bio, struct btrfs_device *dev,
5901 int dev_nr, int rw, int async)
5902{
5903 struct bio_vec *bvec = first_bio->bi_io_vec;
5904 struct bio *bio;
5905 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5906 u64 physical = bbio->stripes[dev_nr].physical;
5907
5908again:
5909 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5910 if (!bio)
5911 return -ENOMEM;
5912
5913 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5914 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5915 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005916 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005917
5918 atomic_inc(&bbio->stripes_pending);
5919 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5920 rw, async);
5921 physical += len;
5922 goto again;
5923 }
5924 bvec++;
5925 }
5926
5927 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5928 return 0;
5929}
5930
5931static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5932{
5933 atomic_inc(&bbio->error);
5934 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005935 /* Shoud be the original bio. */
5936 WARN_ON(bio != bbio->orig_bio);
5937
Josef Bacikde1ee922012-10-19 16:50:56 -04005938 bio->bi_private = bbio->private;
5939 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005940 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005941 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005942
5943 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005944 }
5945}
5946
Chris Masonf1885912008-04-09 16:28:12 -04005947int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005948 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005949{
Chris Mason0b86a832008-03-24 15:01:56 -04005950 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005951 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005952 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005953 u64 length = 0;
5954 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04005955 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08005956 int dev_nr;
5957 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005958 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005959
Kent Overstreet4f024f32013-10-11 15:44:27 -07005960 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005961 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005962
Miao Xiec404e0d2014-01-30 16:46:55 +08005963 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005964 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005965 mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08005966 if (ret) {
5967 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005968 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005969 }
Chris Masoncea9e442008-04-09 16:28:12 -04005970
Jan Schmidta1d3c472011-08-04 17:15:33 +02005971 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005972 bbio->orig_bio = first_bio;
5973 bbio->private = first_bio->bi_private;
5974 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005975 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005976 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5977
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005978 if (bbio->raid_map) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005979 /* In this case, map_length has been set to the length of
5980 a single stripe; not the whole write */
5981 if (rw & WRITE) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005982 ret = raid56_parity_write(root, bio, bbio, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005983 } else {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005984 ret = raid56_parity_recover(root, bio, bbio, map_length,
Miao Xie42452152014-11-25 16:39:28 +08005985 mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05005986 }
Miao Xie42452152014-11-25 16:39:28 +08005987
Miao Xiec404e0d2014-01-30 16:46:55 +08005988 btrfs_bio_counter_dec(root->fs_info);
5989 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005990 }
5991
Chris Masoncea9e442008-04-09 16:28:12 -04005992 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005993 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005994 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005995 BUG();
5996 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005997
Zhao Lei08da7572015-02-12 15:42:16 +08005998 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005999 dev = bbio->stripes[dev_nr].dev;
6000 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
6001 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006002 continue;
6003 }
6004
6005 /*
6006 * Check and see if we're ok with this bio based on it's size
6007 * and offset with the given device.
6008 */
6009 if (!bio_size_ok(dev->bdev, first_bio,
6010 bbio->stripes[dev_nr].physical >> 9)) {
6011 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
6012 dev_nr, rw, async_submit);
6013 BUG_ON(ret);
Josef Bacikde1ee922012-10-19 16:50:56 -04006014 continue;
6015 }
6016
Jan Schmidta1d3c472011-08-04 17:15:33 +02006017 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04006018 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006019 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006020 } else {
6021 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08006022 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04006023 }
Josef Bacik606686e2012-06-04 14:03:51 -04006024
Josef Bacikde1ee922012-10-19 16:50:56 -04006025 submit_stripe_bio(root, bbio, bio,
6026 bbio->stripes[dev_nr].physical, dev_nr, rw,
6027 async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006028 }
Miao Xiec404e0d2014-01-30 16:46:55 +08006029 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04006030 return 0;
6031}
6032
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006033struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006034 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006035{
Yan Zheng2b820322008-11-17 21:11:30 -05006036 struct btrfs_device *device;
6037 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006038
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006039 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006040 while (cur_devices) {
6041 if (!fsid ||
6042 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6043 device = __find_device(&cur_devices->devices,
6044 devid, uuid);
6045 if (device)
6046 return device;
6047 }
6048 cur_devices = cur_devices->seed;
6049 }
6050 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006051}
6052
Chris Masondfe25022008-05-13 13:46:40 -04006053static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
Miao Xie5f375832014-09-03 21:35:46 +08006054 struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006055 u64 devid, u8 *dev_uuid)
6056{
6057 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006058
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006059 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6060 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05006061 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006062
6063 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006064 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006065 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006066
6067 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05006068 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006069
Chris Masondfe25022008-05-13 13:46:40 -04006070 return device;
6071}
6072
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006073/**
6074 * btrfs_alloc_device - allocate struct btrfs_device
6075 * @fs_info: used only for generating a new devid, can be NULL if
6076 * devid is provided (i.e. @devid != NULL).
6077 * @devid: a pointer to devid for this device. If NULL a new devid
6078 * is generated.
6079 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6080 * is generated.
6081 *
6082 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6083 * on error. Returned struct is not linked onto any lists and can be
6084 * destroyed with kfree() right away.
6085 */
6086struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6087 const u64 *devid,
6088 const u8 *uuid)
6089{
6090 struct btrfs_device *dev;
6091 u64 tmp;
6092
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306093 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006094 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006095
6096 dev = __alloc_device();
6097 if (IS_ERR(dev))
6098 return dev;
6099
6100 if (devid)
6101 tmp = *devid;
6102 else {
6103 int ret;
6104
6105 ret = find_next_devid(fs_info, &tmp);
6106 if (ret) {
6107 kfree(dev);
6108 return ERR_PTR(ret);
6109 }
6110 }
6111 dev->devid = tmp;
6112
6113 if (uuid)
6114 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6115 else
6116 generate_random_uuid(dev->uuid);
6117
Liu Bo9e0af232014-08-15 23:36:53 +08006118 btrfs_init_work(&dev->work, btrfs_submit_helper,
6119 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006120
6121 return dev;
6122}
6123
Chris Mason0b86a832008-03-24 15:01:56 -04006124static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6125 struct extent_buffer *leaf,
6126 struct btrfs_chunk *chunk)
6127{
6128 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6129 struct map_lookup *map;
6130 struct extent_map *em;
6131 u64 logical;
6132 u64 length;
6133 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006134 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006135 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006136 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006137 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006138
Chris Masone17cade2008-04-15 15:41:47 -04006139 logical = key->offset;
6140 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04006141
Chris Mason890871b2009-09-02 16:24:52 -04006142 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006143 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006144 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006145
6146 /* already mapped? */
6147 if (em && em->start <= logical && em->start + em->len > logical) {
6148 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006149 return 0;
6150 } else if (em) {
6151 free_extent_map(em);
6152 }
Chris Mason0b86a832008-03-24 15:01:56 -04006153
David Sterba172ddd62011-04-21 00:48:27 +02006154 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006155 if (!em)
6156 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006157 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6158 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006159 if (!map) {
6160 free_extent_map(em);
6161 return -ENOMEM;
6162 }
6163
Wang Shilong298a8f92014-06-19 10:42:52 +08006164 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04006165 em->bdev = (struct block_device *)map;
6166 em->start = logical;
6167 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006168 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006169 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006170 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006171
Chris Mason593060d2008-03-25 16:50:33 -04006172 map->num_stripes = num_stripes;
6173 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6174 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6175 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6176 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6177 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006178 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006179 for (i = 0; i < num_stripes; i++) {
6180 map->stripes[i].physical =
6181 btrfs_stripe_offset_nr(leaf, chunk, i);
6182 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006183 read_extent_buffer(leaf, uuid, (unsigned long)
6184 btrfs_stripe_dev_uuid_nr(chunk, i),
6185 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006186 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6187 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04006188 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006189 free_extent_map(em);
6190 return -EIO;
6191 }
Chris Masondfe25022008-05-13 13:46:40 -04006192 if (!map->stripes[i].dev) {
6193 map->stripes[i].dev =
Miao Xie5f375832014-09-03 21:35:46 +08006194 add_missing_dev(root, root->fs_info->fs_devices,
6195 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006196 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04006197 free_extent_map(em);
6198 return -EIO;
6199 }
Anand Jain816fceb2015-04-27 12:46:18 +08006200 btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing",
6201 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006202 }
6203 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006204 }
6205
Chris Mason890871b2009-09-02 16:24:52 -04006206 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006207 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006208 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006209 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006210 free_extent_map(em);
6211
6212 return 0;
6213}
6214
Jeff Mahoney143bede2012-03-01 14:56:26 +01006215static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006216 struct btrfs_dev_item *dev_item,
6217 struct btrfs_device *device)
6218{
6219 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006220
6221 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006222 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6223 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006224 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006225 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006226 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006227 device->type = btrfs_device_type(leaf, dev_item);
6228 device->io_align = btrfs_device_io_align(leaf, dev_item);
6229 device->io_width = btrfs_device_io_width(leaf, dev_item);
6230 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006231 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006232 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006233
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006234 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006235 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006236}
6237
Miao Xie5f375832014-09-03 21:35:46 +08006238static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6239 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006240{
6241 struct btrfs_fs_devices *fs_devices;
6242 int ret;
6243
Li Zefanb367e472011-12-07 11:38:24 +08006244 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006245
6246 fs_devices = root->fs_info->fs_devices->seed;
6247 while (fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006248 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6249 return fs_devices;
6250
Yan Zheng2b820322008-11-17 21:11:30 -05006251 fs_devices = fs_devices->seed;
6252 }
6253
6254 fs_devices = find_fsid(fsid);
6255 if (!fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006256 if (!btrfs_test_opt(root, DEGRADED))
6257 return ERR_PTR(-ENOENT);
6258
6259 fs_devices = alloc_fs_devices(fsid);
6260 if (IS_ERR(fs_devices))
6261 return fs_devices;
6262
6263 fs_devices->seeding = 1;
6264 fs_devices->opened = 1;
6265 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006266 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006267
6268 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006269 if (IS_ERR(fs_devices))
6270 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006271
Christoph Hellwig97288f22008-12-02 06:36:09 -05006272 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006273 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006274 if (ret) {
6275 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006276 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006277 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006278 }
Yan Zheng2b820322008-11-17 21:11:30 -05006279
6280 if (!fs_devices->seeding) {
6281 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006282 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006283 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006284 goto out;
6285 }
6286
6287 fs_devices->seed = root->fs_info->fs_devices->seed;
6288 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006289out:
Miao Xie5f375832014-09-03 21:35:46 +08006290 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006291}
6292
Chris Mason0d81ba52008-03-24 15:02:07 -04006293static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006294 struct extent_buffer *leaf,
6295 struct btrfs_dev_item *dev_item)
6296{
Miao Xie5f375832014-09-03 21:35:46 +08006297 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006298 struct btrfs_device *device;
6299 u64 devid;
6300 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006301 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006302 u8 dev_uuid[BTRFS_UUID_SIZE];
6303
Chris Mason0b86a832008-03-24 15:01:56 -04006304 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006305 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006306 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006307 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006308 BTRFS_UUID_SIZE);
6309
6310 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
Miao Xie5f375832014-09-03 21:35:46 +08006311 fs_devices = open_seed_devices(root, fs_uuid);
6312 if (IS_ERR(fs_devices))
6313 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006314 }
6315
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006316 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006317 if (!device) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006318 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006319 return -EIO;
6320
Miao Xie5f375832014-09-03 21:35:46 +08006321 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6322 if (!device)
6323 return -ENOMEM;
Anand Jain33b97e42015-05-08 04:34:35 +08006324 btrfs_warn(root->fs_info, "devid %llu uuid %pU missing",
6325 devid, dev_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006326 } else {
6327 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6328 return -EIO;
6329
6330 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006331 /*
6332 * this happens when a device that was properly setup
6333 * in the device info lists suddenly goes bad.
6334 * device->bdev is NULL, and so we have to set
6335 * device->missing to one here
6336 */
Miao Xie5f375832014-09-03 21:35:46 +08006337 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006338 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006339 }
Miao Xie5f375832014-09-03 21:35:46 +08006340
6341 /* Move the device to its own fs_devices */
6342 if (device->fs_devices != fs_devices) {
6343 ASSERT(device->missing);
6344
6345 list_move(&device->dev_list, &fs_devices->devices);
6346 device->fs_devices->num_devices--;
6347 fs_devices->num_devices++;
6348
6349 device->fs_devices->missing_devices--;
6350 fs_devices->missing_devices++;
6351
6352 device->fs_devices = fs_devices;
6353 }
Yan Zheng2b820322008-11-17 21:11:30 -05006354 }
6355
6356 if (device->fs_devices != root->fs_info->fs_devices) {
6357 BUG_ON(device->writeable);
6358 if (device->generation !=
6359 btrfs_device_generation(leaf, dev_item))
6360 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006361 }
Chris Mason0b86a832008-03-24 15:01:56 -04006362
6363 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006364 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006365 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006366 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006367 spin_lock(&root->fs_info->free_chunk_lock);
6368 root->fs_info->free_chunk_space += device->total_bytes -
6369 device->bytes_used;
6370 spin_unlock(&root->fs_info->free_chunk_lock);
6371 }
Chris Mason0b86a832008-03-24 15:01:56 -04006372 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006373 return ret;
6374}
6375
Yan Zhenge4404d62008-12-12 10:03:26 -05006376int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006377{
David Sterba6c417612011-04-13 15:41:04 +02006378 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006379 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006380 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006381 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006382 u8 *array_ptr;
6383 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006384 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006385 u32 num_stripes;
6386 u32 array_size;
6387 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006388 u32 cur_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006389 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006390
David Sterbaa83fffb2014-06-15 02:39:54 +02006391 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
6392 /*
6393 * This will create extent buffer of nodesize, superblock size is
6394 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6395 * overallocate but we can keep it as-is, only the first page is used.
6396 */
6397 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
Chris Masona061fc82008-05-07 11:43:44 -04006398 if (!sb)
6399 return -ENOMEM;
6400 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006401 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006402 /*
6403 * The sb extent buffer is artifical and just used to read the system array.
6404 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6405 * pages up-to-date when the page is larger: extent does not cover the
6406 * whole page and consequently check_page_uptodate does not find all
6407 * the page's extents up-to-date (the hole beyond sb),
6408 * write_extent_buffer then triggers a WARN_ON.
6409 *
6410 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6411 * but sb spans only this function. Add an explicit SetPageUptodate call
6412 * to silence the warning eg. on PowerPC 64.
6413 */
6414 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006415 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006416
Chris Masona061fc82008-05-07 11:43:44 -04006417 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006418 array_size = btrfs_super_sys_array_size(super_copy);
6419
David Sterba1ffb22c2014-10-31 19:02:42 +01006420 array_ptr = super_copy->sys_chunk_array;
6421 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6422 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006423
David Sterba1ffb22c2014-10-31 19:02:42 +01006424 while (cur_offset < array_size) {
6425 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006426 len = sizeof(*disk_key);
6427 if (cur_offset + len > array_size)
6428 goto out_short_read;
6429
Chris Mason0b86a832008-03-24 15:01:56 -04006430 btrfs_disk_key_to_cpu(&key, disk_key);
6431
David Sterba1ffb22c2014-10-31 19:02:42 +01006432 array_ptr += len;
6433 sb_array_offset += len;
6434 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006435
Chris Mason0d81ba52008-03-24 15:02:07 -04006436 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006437 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006438 /*
6439 * At least one btrfs_chunk with one stripe must be
6440 * present, exact stripe count check comes afterwards
6441 */
6442 len = btrfs_chunk_item_size(1);
6443 if (cur_offset + len > array_size)
6444 goto out_short_read;
6445
6446 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6447 len = btrfs_chunk_item_size(num_stripes);
6448 if (cur_offset + len > array_size)
6449 goto out_short_read;
6450
Chris Mason0d81ba52008-03-24 15:02:07 -04006451 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006452 if (ret)
6453 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006454 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006455 ret = -EIO;
6456 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006457 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006458 array_ptr += len;
6459 sb_array_offset += len;
6460 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006461 }
Chris Masona061fc82008-05-07 11:43:44 -04006462 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006463 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006464
6465out_short_read:
6466 printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n",
6467 len, cur_offset);
6468 free_extent_buffer(sb);
6469 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006470}
6471
6472int btrfs_read_chunk_tree(struct btrfs_root *root)
6473{
6474 struct btrfs_path *path;
6475 struct extent_buffer *leaf;
6476 struct btrfs_key key;
6477 struct btrfs_key found_key;
6478 int ret;
6479 int slot;
6480
6481 root = root->fs_info->chunk_root;
6482
6483 path = btrfs_alloc_path();
6484 if (!path)
6485 return -ENOMEM;
6486
Li Zefanb367e472011-12-07 11:38:24 +08006487 mutex_lock(&uuid_mutex);
6488 lock_chunks(root);
6489
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006490 /*
6491 * Read all device items, and then all the chunk items. All
6492 * device items are found before any chunk item (their object id
6493 * is smaller than the lowest possible object id for a chunk
6494 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006495 */
6496 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6497 key.offset = 0;
6498 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006499 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006500 if (ret < 0)
6501 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006502 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006503 leaf = path->nodes[0];
6504 slot = path->slots[0];
6505 if (slot >= btrfs_header_nritems(leaf)) {
6506 ret = btrfs_next_leaf(root, path);
6507 if (ret == 0)
6508 continue;
6509 if (ret < 0)
6510 goto error;
6511 break;
6512 }
6513 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006514 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6515 struct btrfs_dev_item *dev_item;
6516 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006517 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006518 ret = read_one_dev(root, leaf, dev_item);
6519 if (ret)
6520 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006521 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6522 struct btrfs_chunk *chunk;
6523 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6524 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006525 if (ret)
6526 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006527 }
6528 path->slots[0]++;
6529 }
Chris Mason0b86a832008-03-24 15:01:56 -04006530 ret = 0;
6531error:
Li Zefanb367e472011-12-07 11:38:24 +08006532 unlock_chunks(root);
6533 mutex_unlock(&uuid_mutex);
6534
Yan Zheng2b820322008-11-17 21:11:30 -05006535 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006536 return ret;
6537}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006538
Miao Xiecb517ea2013-05-15 07:48:19 +00006539void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6540{
6541 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6542 struct btrfs_device *device;
6543
Liu Bo29cc83f2014-05-11 23:14:59 +08006544 while (fs_devices) {
6545 mutex_lock(&fs_devices->device_list_mutex);
6546 list_for_each_entry(device, &fs_devices->devices, dev_list)
6547 device->dev_root = fs_info->dev_root;
6548 mutex_unlock(&fs_devices->device_list_mutex);
6549
6550 fs_devices = fs_devices->seed;
6551 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006552}
6553
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006554static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6555{
6556 int i;
6557
6558 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6559 btrfs_dev_stat_reset(dev, i);
6560}
6561
6562int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6563{
6564 struct btrfs_key key;
6565 struct btrfs_key found_key;
6566 struct btrfs_root *dev_root = fs_info->dev_root;
6567 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6568 struct extent_buffer *eb;
6569 int slot;
6570 int ret = 0;
6571 struct btrfs_device *device;
6572 struct btrfs_path *path = NULL;
6573 int i;
6574
6575 path = btrfs_alloc_path();
6576 if (!path) {
6577 ret = -ENOMEM;
6578 goto out;
6579 }
6580
6581 mutex_lock(&fs_devices->device_list_mutex);
6582 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6583 int item_size;
6584 struct btrfs_dev_stats_item *ptr;
6585
6586 key.objectid = 0;
6587 key.type = BTRFS_DEV_STATS_KEY;
6588 key.offset = device->devid;
6589 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6590 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006591 __btrfs_reset_dev_stats(device);
6592 device->dev_stats_valid = 1;
6593 btrfs_release_path(path);
6594 continue;
6595 }
6596 slot = path->slots[0];
6597 eb = path->nodes[0];
6598 btrfs_item_key_to_cpu(eb, &found_key, slot);
6599 item_size = btrfs_item_size_nr(eb, slot);
6600
6601 ptr = btrfs_item_ptr(eb, slot,
6602 struct btrfs_dev_stats_item);
6603
6604 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6605 if (item_size >= (1 + i) * sizeof(__le64))
6606 btrfs_dev_stat_set(device, i,
6607 btrfs_dev_stats_value(eb, ptr, i));
6608 else
6609 btrfs_dev_stat_reset(device, i);
6610 }
6611
6612 device->dev_stats_valid = 1;
6613 btrfs_dev_stat_print_on_load(device);
6614 btrfs_release_path(path);
6615 }
6616 mutex_unlock(&fs_devices->device_list_mutex);
6617
6618out:
6619 btrfs_free_path(path);
6620 return ret < 0 ? ret : 0;
6621}
6622
6623static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6624 struct btrfs_root *dev_root,
6625 struct btrfs_device *device)
6626{
6627 struct btrfs_path *path;
6628 struct btrfs_key key;
6629 struct extent_buffer *eb;
6630 struct btrfs_dev_stats_item *ptr;
6631 int ret;
6632 int i;
6633
6634 key.objectid = 0;
6635 key.type = BTRFS_DEV_STATS_KEY;
6636 key.offset = device->devid;
6637
6638 path = btrfs_alloc_path();
6639 BUG_ON(!path);
6640 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6641 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006642 printk_in_rcu(KERN_WARNING "BTRFS: "
6643 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006644 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006645 goto out;
6646 }
6647
6648 if (ret == 0 &&
6649 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6650 /* need to delete old one and insert a new one */
6651 ret = btrfs_del_item(trans, dev_root, path);
6652 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006653 printk_in_rcu(KERN_WARNING "BTRFS: "
6654 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006655 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006656 goto out;
6657 }
6658 ret = 1;
6659 }
6660
6661 if (ret == 1) {
6662 /* need to insert a new item */
6663 btrfs_release_path(path);
6664 ret = btrfs_insert_empty_item(trans, dev_root, path,
6665 &key, sizeof(*ptr));
6666 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006667 printk_in_rcu(KERN_WARNING "BTRFS: "
6668 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006669 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006670 goto out;
6671 }
6672 }
6673
6674 eb = path->nodes[0];
6675 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6676 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6677 btrfs_set_dev_stats_value(eb, ptr, i,
6678 btrfs_dev_stat_read(device, i));
6679 btrfs_mark_buffer_dirty(eb);
6680
6681out:
6682 btrfs_free_path(path);
6683 return ret;
6684}
6685
6686/*
6687 * called from commit_transaction. Writes all changed device stats to disk.
6688 */
6689int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6690 struct btrfs_fs_info *fs_info)
6691{
6692 struct btrfs_root *dev_root = fs_info->dev_root;
6693 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6694 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006695 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006696 int ret = 0;
6697
6698 mutex_lock(&fs_devices->device_list_mutex);
6699 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006700 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006701 continue;
6702
Miao Xieaddc3fa2014-07-24 11:37:11 +08006703 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006704 ret = update_dev_stat_item(trans, dev_root, device);
6705 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006706 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006707 }
6708 mutex_unlock(&fs_devices->device_list_mutex);
6709
6710 return ret;
6711}
6712
Stefan Behrens442a4f62012-05-25 16:06:08 +02006713void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6714{
6715 btrfs_dev_stat_inc(dev, index);
6716 btrfs_dev_stat_print_on_error(dev);
6717}
6718
Eric Sandeen48a3b632013-04-25 20:41:01 +00006719static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006720{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006721 if (!dev->dev_stats_valid)
6722 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006723 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6724 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006725 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006726 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6727 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6728 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006729 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6730 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006731}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006732
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006733static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6734{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006735 int i;
6736
6737 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6738 if (btrfs_dev_stat_read(dev, i) != 0)
6739 break;
6740 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6741 return; /* all values == 0, suppress message */
6742
Frank Holtonefe120a2013-12-20 11:37:06 -05006743 printk_in_rcu(KERN_INFO "BTRFS: "
6744 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006745 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006746 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6747 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6748 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6749 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6750 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6751}
6752
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006753int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006754 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006755{
6756 struct btrfs_device *dev;
6757 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6758 int i;
6759
6760 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006761 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006762 mutex_unlock(&fs_devices->device_list_mutex);
6763
6764 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006765 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006766 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006767 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006768 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006769 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006770 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006771 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6772 if (stats->nr_items > i)
6773 stats->values[i] =
6774 btrfs_dev_stat_read_and_reset(dev, i);
6775 else
6776 btrfs_dev_stat_reset(dev, i);
6777 }
6778 } else {
6779 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6780 if (stats->nr_items > i)
6781 stats->values[i] = btrfs_dev_stat_read(dev, i);
6782 }
6783 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6784 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6785 return 0;
6786}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006787
6788int btrfs_scratch_superblock(struct btrfs_device *device)
6789{
6790 struct buffer_head *bh;
6791 struct btrfs_super_block *disk_super;
6792
6793 bh = btrfs_read_dev_super(device->bdev);
6794 if (!bh)
6795 return -EINVAL;
6796 disk_super = (struct btrfs_super_block *)bh->b_data;
6797
6798 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6799 set_buffer_dirty(bh);
6800 sync_dirty_buffer(bh);
6801 brelse(bh);
6802
6803 return 0;
6804}
Miao Xie935e5cc2014-09-03 21:35:33 +08006805
6806/*
6807 * Update the size of all devices, which is used for writing out the
6808 * super blocks.
6809 */
6810void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6811{
6812 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6813 struct btrfs_device *curr, *next;
6814
6815 if (list_empty(&fs_devices->resized_devices))
6816 return;
6817
6818 mutex_lock(&fs_devices->device_list_mutex);
6819 lock_chunks(fs_info->dev_root);
6820 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6821 resized_list) {
6822 list_del_init(&curr->resized_list);
6823 curr->commit_total_bytes = curr->disk_total_bytes;
6824 }
6825 unlock_chunks(fs_info->dev_root);
6826 mutex_unlock(&fs_devices->device_list_mutex);
6827}
Miao Xiece7213c2014-09-03 21:35:34 +08006828
6829/* Must be invoked during the transaction commit */
6830void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6831 struct btrfs_transaction *transaction)
6832{
6833 struct extent_map *em;
6834 struct map_lookup *map;
6835 struct btrfs_device *dev;
6836 int i;
6837
6838 if (list_empty(&transaction->pending_chunks))
6839 return;
6840
6841 /* In order to kick the device replace finish process */
6842 lock_chunks(root);
6843 list_for_each_entry(em, &transaction->pending_chunks, list) {
6844 map = (struct map_lookup *)em->bdev;
6845
6846 for (i = 0; i < map->num_stripes; i++) {
6847 dev = map->stripes[i].dev;
6848 dev->commit_bytes_used = dev->bytes_used;
6849 }
6850 }
6851 unlock_chunks(root);
6852}
Anand Jain5a13f432015-03-10 06:38:31 +08006853
6854void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
6855{
6856 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6857 while (fs_devices) {
6858 fs_devices->fs_info = fs_info;
6859 fs_devices = fs_devices->seed;
6860 }
6861}
6862
6863void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
6864{
6865 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6866 while (fs_devices) {
6867 fs_devices->fs_info = NULL;
6868 fs_devices = fs_devices->seed;
6869 }
6870}