blob: 27a14f1c5a7c34813514f8d55bc1f39e544dc428 [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020026#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020027#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050028#include <linux/raid/pq.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020029#include <linux/semaphore.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050030#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "ctree.h"
32#include "extent_map.h"
33#include "disk-io.h"
34#include "transaction.h"
35#include "print-tree.h"
36#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050037#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040038#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010039#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040040#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060041#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010042#include "dev-replace.h"
Anand Jain99994cd2014-06-03 11:36:00 +080043#include "sysfs.h"
Chris Mason0b86a832008-03-24 15:01:56 -040044
Yan Zheng2b820322008-11-17 21:11:30 -050045static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020049static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +000050static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020051static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050052
Chris Mason8a4b83c2008-03-24 15:02:07 -040053static DEFINE_MUTEX(uuid_mutex);
54static LIST_HEAD(fs_uuids);
55
Chris Mason7d9eb122008-07-08 14:19:17 -040056static void lock_chunks(struct btrfs_root *root)
57{
Chris Mason7d9eb122008-07-08 14:19:17 -040058 mutex_lock(&root->fs_info->chunk_mutex);
59}
60
61static void unlock_chunks(struct btrfs_root *root)
62{
Chris Mason7d9eb122008-07-08 14:19:17 -040063 mutex_unlock(&root->fs_info->chunk_mutex);
64}
65
Ilya Dryomov2208a372013-08-12 14:33:03 +030066static struct btrfs_fs_devices *__alloc_fs_devices(void)
67{
68 struct btrfs_fs_devices *fs_devs;
69
70 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
71 if (!fs_devs)
72 return ERR_PTR(-ENOMEM);
73
74 mutex_init(&fs_devs->device_list_mutex);
75
76 INIT_LIST_HEAD(&fs_devs->devices);
77 INIT_LIST_HEAD(&fs_devs->alloc_list);
78 INIT_LIST_HEAD(&fs_devs->list);
79
80 return fs_devs;
81}
82
83/**
84 * alloc_fs_devices - allocate struct btrfs_fs_devices
85 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
86 * generated.
87 *
88 * Return: a pointer to a new &struct btrfs_fs_devices on success;
89 * ERR_PTR() on error. Returned struct is not linked onto any lists and
90 * can be destroyed with kfree() right away.
91 */
92static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
93{
94 struct btrfs_fs_devices *fs_devs;
95
96 fs_devs = __alloc_fs_devices();
97 if (IS_ERR(fs_devs))
98 return fs_devs;
99
100 if (fsid)
101 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
102 else
103 generate_random_uuid(fs_devs->fsid);
104
105 return fs_devs;
106}
107
Yan Zhenge4404d62008-12-12 10:03:26 -0500108static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
109{
110 struct btrfs_device *device;
111 WARN_ON(fs_devices->opened);
112 while (!list_empty(&fs_devices->devices)) {
113 device = list_entry(fs_devices->devices.next,
114 struct btrfs_device, dev_list);
115 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400116 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500117 kfree(device);
118 }
119 kfree(fs_devices);
120}
121
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000122static void btrfs_kobject_uevent(struct block_device *bdev,
123 enum kobject_action action)
124{
125 int ret;
126
127 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
128 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500129 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000130 action,
131 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
132 &disk_to_dev(bdev->bd_disk)->kobj);
133}
134
Jeff Mahoney143bede2012-03-01 14:56:26 +0100135void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400136{
137 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400138
Yan Zheng2b820322008-11-17 21:11:30 -0500139 while (!list_empty(&fs_uuids)) {
140 fs_devices = list_entry(fs_uuids.next,
141 struct btrfs_fs_devices, list);
142 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500143 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400144 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400145}
146
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300147static struct btrfs_device *__alloc_device(void)
148{
149 struct btrfs_device *dev;
150
151 dev = kzalloc(sizeof(*dev), GFP_NOFS);
152 if (!dev)
153 return ERR_PTR(-ENOMEM);
154
155 INIT_LIST_HEAD(&dev->dev_list);
156 INIT_LIST_HEAD(&dev->dev_alloc_list);
157
158 spin_lock_init(&dev->io_lock);
159
160 spin_lock_init(&dev->reada_lock);
161 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800162 atomic_set(&dev->dev_stats_ccnt, 0);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300163 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
164 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
165
166 return dev;
167}
168
Chris Masona1b32a52008-09-05 16:09:51 -0400169static noinline struct btrfs_device *__find_device(struct list_head *head,
170 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400171{
172 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400173
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500174 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400175 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400176 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400177 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400178 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400179 }
180 return NULL;
181}
182
Chris Masona1b32a52008-09-05 16:09:51 -0400183static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400184{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400185 struct btrfs_fs_devices *fs_devices;
186
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500187 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400188 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
189 return fs_devices;
190 }
191 return NULL;
192}
193
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100194static int
195btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
196 int flush, struct block_device **bdev,
197 struct buffer_head **bh)
198{
199 int ret;
200
201 *bdev = blkdev_get_by_path(device_path, flags, holder);
202
203 if (IS_ERR(*bdev)) {
204 ret = PTR_ERR(*bdev);
Frank Holtonefe120a2013-12-20 11:37:06 -0500205 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100206 goto error;
207 }
208
209 if (flush)
210 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
211 ret = set_blocksize(*bdev, 4096);
212 if (ret) {
213 blkdev_put(*bdev, flags);
214 goto error;
215 }
216 invalidate_bdev(*bdev);
217 *bh = btrfs_read_dev_super(*bdev);
218 if (!*bh) {
219 ret = -EINVAL;
220 blkdev_put(*bdev, flags);
221 goto error;
222 }
223
224 return 0;
225
226error:
227 *bdev = NULL;
228 *bh = NULL;
229 return ret;
230}
231
Chris Masonffbd5172009-04-20 15:50:09 -0400232static void requeue_list(struct btrfs_pending_bios *pending_bios,
233 struct bio *head, struct bio *tail)
234{
235
236 struct bio *old_head;
237
238 old_head = pending_bios->head;
239 pending_bios->head = head;
240 if (pending_bios->tail)
241 tail->bi_next = old_head;
242 else
243 pending_bios->tail = tail;
244}
245
Chris Mason8b712842008-06-11 16:50:36 -0400246/*
247 * we try to collect pending bios for a device so we don't get a large
248 * number of procs sending bios down to the same device. This greatly
249 * improves the schedulers ability to collect and merge the bios.
250 *
251 * But, it also turns into a long list of bios to process and that is sure
252 * to eventually make the worker thread block. The solution here is to
253 * make some progress and then put this work struct back at the end of
254 * the list if the block device is congested. This way, multiple devices
255 * can make progress from a single worker thread.
256 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100257static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400258{
259 struct bio *pending;
260 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400261 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400262 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400263 struct bio *tail;
264 struct bio *cur;
265 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400266 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400267 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400268 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400269 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400270 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000271 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400272 struct blk_plug plug;
273
274 /*
275 * this function runs all the bios we've collected for
276 * a particular device. We don't want to wander off to
277 * another device without first sending all of these down.
278 * So, setup a plug here and finish it off before we return
279 */
280 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400281
Chris Masonbedf7622009-04-03 10:32:58 -0400282 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400283 fs_info = device->dev_root->fs_info;
284 limit = btrfs_async_submit_limit(fs_info);
285 limit = limit * 2 / 3;
286
Chris Mason8b712842008-06-11 16:50:36 -0400287loop:
288 spin_lock(&device->io_lock);
289
Chris Masona6837052009-02-04 09:19:41 -0500290loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400291 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400292
Chris Mason8b712842008-06-11 16:50:36 -0400293 /* take all the bios off the list at once and process them
294 * later on (without the lock held). But, remember the
295 * tail and other pointers so the bios can be properly reinserted
296 * into the list if we hit congestion
297 */
Chris Masond84275c2009-06-09 15:39:08 -0400298 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400299 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400300 force_reg = 1;
301 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400302 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400303 force_reg = 0;
304 }
Chris Masonffbd5172009-04-20 15:50:09 -0400305
306 pending = pending_bios->head;
307 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400308 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400309
310 /*
311 * if pending was null this time around, no bios need processing
312 * at all and we can stop. Otherwise it'll loop back up again
313 * and do an additional check so no bios are missed.
314 *
315 * device->running_pending is used to synchronize with the
316 * schedule_bio code.
317 */
Chris Masonffbd5172009-04-20 15:50:09 -0400318 if (device->pending_sync_bios.head == NULL &&
319 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400320 again = 0;
321 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400322 } else {
323 again = 1;
324 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400325 }
Chris Masonffbd5172009-04-20 15:50:09 -0400326
327 pending_bios->head = NULL;
328 pending_bios->tail = NULL;
329
Chris Mason8b712842008-06-11 16:50:36 -0400330 spin_unlock(&device->io_lock);
331
Chris Masond3977122009-01-05 21:25:51 -0500332 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400333
334 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400335 /* we want to work on both lists, but do more bios on the
336 * sync list than the regular list
337 */
338 if ((num_run > 32 &&
339 pending_bios != &device->pending_sync_bios &&
340 device->pending_sync_bios.head) ||
341 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
342 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400343 spin_lock(&device->io_lock);
344 requeue_list(pending_bios, pending, tail);
345 goto loop_lock;
346 }
347
Chris Mason8b712842008-06-11 16:50:36 -0400348 cur = pending;
349 pending = pending->bi_next;
350 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400351
Josef Bacik66657b32012-08-01 15:36:24 -0400352 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400353 waitqueue_active(&fs_info->async_submit_wait))
354 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400355
356 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400357
Chris Mason2ab1ba62011-08-04 14:28:36 -0400358 /*
359 * if we're doing the sync list, record that our
360 * plug has some sync requests on it
361 *
362 * If we're doing the regular list and there are
363 * sync requests sitting around, unplug before
364 * we add more
365 */
366 if (pending_bios == &device->pending_sync_bios) {
367 sync_pending = 1;
368 } else if (sync_pending) {
369 blk_finish_plug(&plug);
370 blk_start_plug(&plug);
371 sync_pending = 0;
372 }
373
Stefan Behrens21adbd52011-11-09 13:44:05 +0100374 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400375 num_run++;
376 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100377 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400378 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400379
380 /*
381 * we made progress, there is more work to do and the bdi
382 * is now congested. Back off and let other work structs
383 * run instead
384 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400385 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500386 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400387 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400388
Chris Masonb765ead2009-04-03 10:27:10 -0400389 ioc = current->io_context;
390
391 /*
392 * the main goal here is that we don't want to
393 * block if we're going to be able to submit
394 * more requests without blocking.
395 *
396 * This code does two great things, it pokes into
397 * the elevator code from a filesystem _and_
398 * it makes assumptions about how batching works.
399 */
400 if (ioc && ioc->nr_batch_requests > 0 &&
401 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
402 (last_waited == 0 ||
403 ioc->last_waited == last_waited)) {
404 /*
405 * we want to go through our batch of
406 * requests and stop. So, we copy out
407 * the ioc->last_waited time and test
408 * against it before looping
409 */
410 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100411 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400412 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400413 continue;
414 }
Chris Mason8b712842008-06-11 16:50:36 -0400415 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400416 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500417 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400418
419 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d42014-02-28 10:46:08 +0800420 btrfs_queue_work(fs_info->submit_workers,
421 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400422 goto done;
423 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500424 /* unplug every 64 requests just for good measure */
425 if (batch_run % 64 == 0) {
426 blk_finish_plug(&plug);
427 blk_start_plug(&plug);
428 sync_pending = 0;
429 }
Chris Mason8b712842008-06-11 16:50:36 -0400430 }
Chris Masonffbd5172009-04-20 15:50:09 -0400431
Chris Mason51684082010-03-10 15:33:32 -0500432 cond_resched();
433 if (again)
434 goto loop;
435
436 spin_lock(&device->io_lock);
437 if (device->pending_bios.head || device->pending_sync_bios.head)
438 goto loop_lock;
439 spin_unlock(&device->io_lock);
440
Chris Mason8b712842008-06-11 16:50:36 -0400441done:
Chris Mason211588a2011-04-19 20:12:40 -0400442 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400443}
444
Christoph Hellwigb2950862008-12-02 09:54:17 -0500445static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400446{
447 struct btrfs_device *device;
448
449 device = container_of(work, struct btrfs_device, work);
450 run_scheduled_bios(device);
451}
452
David Sterba60999ca2014-03-26 18:26:36 +0100453/*
454 * Add new device to list of registered devices
455 *
456 * Returns:
457 * 1 - first time device is seen
458 * 0 - device already known
459 * < 0 - error
460 */
Chris Masona1b32a52008-09-05 16:09:51 -0400461static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400462 struct btrfs_super_block *disk_super,
463 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
464{
465 struct btrfs_device *device;
466 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400467 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100468 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400469 u64 found_transid = btrfs_super_generation(disk_super);
470
471 fs_devices = find_fsid(disk_super->fsid);
472 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300473 fs_devices = alloc_fs_devices(disk_super->fsid);
474 if (IS_ERR(fs_devices))
475 return PTR_ERR(fs_devices);
476
Chris Mason8a4b83c2008-03-24 15:02:07 -0400477 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300478
Chris Mason8a4b83c2008-03-24 15:02:07 -0400479 device = NULL;
480 } else {
Chris Masona4437552008-04-18 10:29:38 -0400481 device = __find_device(&fs_devices->devices, devid,
482 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400483 }
Miao Xie443f24f2014-07-24 11:37:15 +0800484
Chris Mason8a4b83c2008-03-24 15:02:07 -0400485 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500486 if (fs_devices->opened)
487 return -EBUSY;
488
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300489 device = btrfs_alloc_device(NULL, &devid,
490 disk_super->dev_item.uuid);
491 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400492 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300493 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400494 }
Josef Bacik606686e2012-06-04 14:03:51 -0400495
496 name = rcu_string_strdup(path, GFP_NOFS);
497 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400498 kfree(device);
499 return -ENOMEM;
500 }
Josef Bacik606686e2012-06-04 14:03:51 -0400501 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200502
Chris Masone5e9a522009-06-10 15:17:02 -0400503 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000504 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100505 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400506 mutex_unlock(&fs_devices->device_list_mutex);
507
David Sterba60999ca2014-03-26 18:26:36 +0100508 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500509 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400510 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800511 /*
512 * When FS is already mounted.
513 * 1. If you are here and if the device->name is NULL that
514 * means this device was missing at time of FS mount.
515 * 2. If you are here and if the device->name is different
516 * from 'path' that means either
517 * a. The same device disappeared and reappeared with
518 * different name. or
519 * b. The missing-disk-which-was-replaced, has
520 * reappeared now.
521 *
522 * We must allow 1 and 2a above. But 2b would be a spurious
523 * and unintentional.
524 *
525 * Further in case of 1 and 2a above, the disk at 'path'
526 * would have missed some transaction when it was away and
527 * in case of 2a the stale bdev has to be updated as well.
528 * 2b must not be allowed at all time.
529 */
530
531 /*
532 * As of now don't allow update to btrfs_fs_device through
533 * the btrfs dev scan cli, after FS has been mounted.
534 */
Anand Jain77bdae42014-07-03 18:22:06 +0800535 if (fs_devices->opened) {
Anand Jainb96de002014-07-03 18:22:05 +0800536 return -EBUSY;
Anand Jain77bdae42014-07-03 18:22:06 +0800537 } else {
538 /*
539 * That is if the FS is _not_ mounted and if you
540 * are here, that means there is more than one
541 * disk with same uuid and devid.We keep the one
542 * with larger generation number or the last-in if
543 * generation are equal.
544 */
545 if (found_transid < device->generation)
546 return -EEXIST;
547 }
Anand Jainb96de002014-07-03 18:22:05 +0800548
Josef Bacik606686e2012-06-04 14:03:51 -0400549 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000550 if (!name)
551 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400552 rcu_string_free(device->name);
553 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500554 if (device->missing) {
555 fs_devices->missing_devices--;
556 device->missing = 0;
557 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400558 }
559
Anand Jain77bdae42014-07-03 18:22:06 +0800560 /*
561 * Unmount does not free the btrfs_device struct but would zero
562 * generation along with most of the other members. So just update
563 * it back. We need it to pick the disk with largest generation
564 * (as above).
565 */
566 if (!fs_devices->opened)
567 device->generation = found_transid;
568
Chris Mason8a4b83c2008-03-24 15:02:07 -0400569 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100570
571 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400572}
573
Yan Zhenge4404d62008-12-12 10:03:26 -0500574static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
575{
576 struct btrfs_fs_devices *fs_devices;
577 struct btrfs_device *device;
578 struct btrfs_device *orig_dev;
579
Ilya Dryomov2208a372013-08-12 14:33:03 +0300580 fs_devices = alloc_fs_devices(orig->fsid);
581 if (IS_ERR(fs_devices))
582 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500583
Josef Bacik02db0842012-06-21 16:03:58 -0400584 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500585
Xiao Guangrong46224702011-04-20 10:08:47 +0000586 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500587 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400588 struct rcu_string *name;
589
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300590 device = btrfs_alloc_device(NULL, &orig_dev->devid,
591 orig_dev->uuid);
592 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500593 goto error;
594
Josef Bacik606686e2012-06-04 14:03:51 -0400595 /*
596 * This is ok to do without rcu read locked because we hold the
597 * uuid mutex so nothing we touch in here is going to disappear.
598 */
Anand Jaine755f782014-06-30 17:12:47 +0800599 if (orig_dev->name) {
600 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
601 if (!name) {
602 kfree(device);
603 goto error;
604 }
605 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400606 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500607
Yan Zhenge4404d62008-12-12 10:03:26 -0500608 list_add(&device->dev_list, &fs_devices->devices);
609 device->fs_devices = fs_devices;
610 fs_devices->num_devices++;
611 }
612 return fs_devices;
613error:
614 free_fs_devices(fs_devices);
615 return ERR_PTR(-ENOMEM);
616}
617
Stefan Behrens8dabb742012-11-06 13:15:27 +0100618void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
619 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400620{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500621 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800622 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500623
Chris Masondfe25022008-05-13 13:46:40 -0400624 mutex_lock(&uuid_mutex);
625again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000626 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500627 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500628 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100629 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800630 (!latest_dev ||
631 device->generation > latest_dev->generation)) {
632 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500633 }
Yan Zheng2b820322008-11-17 21:11:30 -0500634 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500635 }
Yan Zheng2b820322008-11-17 21:11:30 -0500636
Stefan Behrens8dabb742012-11-06 13:15:27 +0100637 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
638 /*
639 * In the first step, keep the device which has
640 * the correct fsid and the devid that is used
641 * for the dev_replace procedure.
642 * In the second step, the dev_replace state is
643 * read from the device tree and it is known
644 * whether the procedure is really active or
645 * not, which means whether this device is
646 * used or whether it should be removed.
647 */
648 if (step == 0 || device->is_tgtdev_for_dev_replace) {
649 continue;
650 }
651 }
Yan Zheng2b820322008-11-17 21:11:30 -0500652 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100653 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500654 device->bdev = NULL;
655 fs_devices->open_devices--;
656 }
657 if (device->writeable) {
658 list_del_init(&device->dev_alloc_list);
659 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100660 if (!device->is_tgtdev_for_dev_replace)
661 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500662 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500663 list_del_init(&device->dev_list);
664 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400665 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500666 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400667 }
Yan Zheng2b820322008-11-17 21:11:30 -0500668
669 if (fs_devices->seed) {
670 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500671 goto again;
672 }
673
Miao Xie443f24f2014-07-24 11:37:15 +0800674 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500675
Chris Masondfe25022008-05-13 13:46:40 -0400676 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400677}
Chris Masona0af4692008-05-13 16:03:06 -0400678
Xiao Guangrong1f781602011-04-20 10:09:16 +0000679static void __free_device(struct work_struct *work)
680{
681 struct btrfs_device *device;
682
683 device = container_of(work, struct btrfs_device, rcu_work);
684
685 if (device->bdev)
686 blkdev_put(device->bdev, device->mode);
687
Josef Bacik606686e2012-06-04 14:03:51 -0400688 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000689 kfree(device);
690}
691
692static void free_device(struct rcu_head *head)
693{
694 struct btrfs_device *device;
695
696 device = container_of(head, struct btrfs_device, rcu);
697
698 INIT_WORK(&device->rcu_work, __free_device);
699 schedule_work(&device->rcu_work);
700}
701
Yan Zheng2b820322008-11-17 21:11:30 -0500702static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400703{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400704 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500705
Yan Zheng2b820322008-11-17 21:11:30 -0500706 if (--fs_devices->opened > 0)
707 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400708
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000709 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500710 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000711 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400712 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000713
714 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400715 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000716
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300717 if (device->writeable &&
718 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500719 list_del_init(&device->dev_alloc_list);
720 fs_devices->rw_devices--;
721 }
722
Josef Bacikd5e20032011-08-04 14:52:27 +0000723 if (device->can_discard)
724 fs_devices->num_can_discard--;
Josef Bacik726551e2013-08-26 13:45:53 -0400725 if (device->missing)
726 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000727
Ilya Dryomova1e87802013-08-12 14:33:04 +0300728 new_device = btrfs_alloc_device(NULL, &device->devid,
729 device->uuid);
730 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400731
732 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400733 if (device->name) {
734 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300735 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400736 rcu_assign_pointer(new_device->name, name);
737 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300738
Xiao Guangrong1f781602011-04-20 10:09:16 +0000739 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300740 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000741
742 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400743 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000744 mutex_unlock(&fs_devices->device_list_mutex);
745
Yan Zhenge4404d62008-12-12 10:03:26 -0500746 WARN_ON(fs_devices->open_devices);
747 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500748 fs_devices->opened = 0;
749 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500750
Chris Mason8a4b83c2008-03-24 15:02:07 -0400751 return 0;
752}
753
Yan Zheng2b820322008-11-17 21:11:30 -0500754int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
755{
Yan Zhenge4404d62008-12-12 10:03:26 -0500756 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500757 int ret;
758
759 mutex_lock(&uuid_mutex);
760 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500761 if (!fs_devices->opened) {
762 seed_devices = fs_devices->seed;
763 fs_devices->seed = NULL;
764 }
Yan Zheng2b820322008-11-17 21:11:30 -0500765 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500766
767 while (seed_devices) {
768 fs_devices = seed_devices;
769 seed_devices = fs_devices->seed;
770 __btrfs_close_devices(fs_devices);
771 free_fs_devices(fs_devices);
772 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000773 /*
774 * Wait for rcu kworkers under __btrfs_close_devices
775 * to finish all blkdev_puts so device is really
776 * free when umount is done.
777 */
778 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500779 return ret;
780}
781
Yan Zhenge4404d62008-12-12 10:03:26 -0500782static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
783 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400784{
Josef Bacikd5e20032011-08-04 14:52:27 +0000785 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400786 struct block_device *bdev;
787 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400788 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800789 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400790 struct buffer_head *bh;
791 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400792 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500793 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400794 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400795
Tejun Heod4d77622010-11-13 11:55:18 +0100796 flags |= FMODE_EXCL;
797
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500798 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400799 if (device->bdev)
800 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400801 if (!device->name)
802 continue;
803
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000804 /* Just open everything we can; ignore failures here */
805 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
806 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100807 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400808
809 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000810 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400811 if (devid != device->devid)
812 goto error_brelse;
813
Yan Zheng2b820322008-11-17 21:11:30 -0500814 if (memcmp(device->uuid, disk_super->dev_item.uuid,
815 BTRFS_UUID_SIZE))
816 goto error_brelse;
817
818 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800819 if (!latest_dev ||
820 device->generation > latest_dev->generation)
821 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400822
Yan Zheng2b820322008-11-17 21:11:30 -0500823 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
824 device->writeable = 0;
825 } else {
826 device->writeable = !bdev_read_only(bdev);
827 seeding = 0;
828 }
829
Josef Bacikd5e20032011-08-04 14:52:27 +0000830 q = bdev_get_queue(bdev);
831 if (blk_queue_discard(q)) {
832 device->can_discard = 1;
833 fs_devices->num_can_discard++;
834 }
835
Chris Mason8a4b83c2008-03-24 15:02:07 -0400836 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400837 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500838 device->mode = flags;
839
Chris Masonc2898112009-06-10 09:51:32 -0400840 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
841 fs_devices->rotating = 1;
842
Chris Masona0af4692008-05-13 16:03:06 -0400843 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300844 if (device->writeable &&
845 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500846 fs_devices->rw_devices++;
847 list_add(&device->dev_alloc_list,
848 &fs_devices->alloc_list);
849 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000850 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400851 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400852
Chris Masona0af4692008-05-13 16:03:06 -0400853error_brelse:
854 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100855 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400856 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400857 }
Chris Masona0af4692008-05-13 16:03:06 -0400858 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300859 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400860 goto out;
861 }
Yan Zheng2b820322008-11-17 21:11:30 -0500862 fs_devices->seeding = seeding;
863 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800864 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500865 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400866out:
Yan Zheng2b820322008-11-17 21:11:30 -0500867 return ret;
868}
869
870int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500871 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500872{
873 int ret;
874
875 mutex_lock(&uuid_mutex);
876 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500877 fs_devices->opened++;
878 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500879 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500880 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500881 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400882 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400883 return ret;
884}
885
David Sterba6f60cbd2013-02-15 11:31:02 -0700886/*
887 * Look for a btrfs signature on a device. This may be called out of the mount path
888 * and we are not allowed to call set_blocksize during the scan. The superblock
889 * is read via pagecache
890 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500891int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400892 struct btrfs_fs_devices **fs_devices_ret)
893{
894 struct btrfs_super_block *disk_super;
895 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700896 struct page *page;
897 void *p;
898 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400899 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400900 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400901 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700902 u64 bytenr;
903 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400904
David Sterba6f60cbd2013-02-15 11:31:02 -0700905 /*
906 * we would like to check all the supers, but that would make
907 * a btrfs mount succeed after a mkfs from a different FS.
908 * So, we need to add a special mount option to scan for
909 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
910 */
911 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100912 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500913 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700914
915 bdev = blkdev_get_by_path(path, flags, holder);
916
917 if (IS_ERR(bdev)) {
918 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100919 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700920 }
921
922 /* make sure our super fits in the device */
923 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
924 goto error_bdev_put;
925
926 /* make sure our super fits in the page */
927 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
928 goto error_bdev_put;
929
930 /* make sure our super doesn't straddle pages on disk */
931 index = bytenr >> PAGE_CACHE_SHIFT;
932 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
933 goto error_bdev_put;
934
935 /* pull in the page with our super */
936 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
937 index, GFP_NOFS);
938
939 if (IS_ERR_OR_NULL(page))
940 goto error_bdev_put;
941
942 p = kmap(page);
943
944 /* align our pointer to the offset of the super block */
945 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
946
947 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800948 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700949 goto error_unmap;
950
Xiao Guangronga3438322010-01-06 11:48:18 +0000951 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400952 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400953 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700954
Chris Mason8a4b83c2008-03-24 15:02:07 -0400955 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100956 if (ret > 0) {
957 if (disk_super->label[0]) {
958 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
959 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
960 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
961 } else {
962 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
963 }
964
965 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
966 ret = 0;
967 }
Josef Bacik02db0842012-06-21 16:03:58 -0400968 if (!ret && fs_devices_ret)
969 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700970
971error_unmap:
972 kunmap(page);
973 page_cache_release(page);
974
975error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100976 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400977error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100978 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400979 return ret;
980}
Chris Mason0b86a832008-03-24 15:01:56 -0400981
Miao Xie6d07bce2011-01-05 10:07:31 +0000982/* helper to account the used device space in the range */
983int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
984 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400985{
986 struct btrfs_key key;
987 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000988 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500989 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000990 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400991 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000992 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400993 struct extent_buffer *l;
994
Miao Xie6d07bce2011-01-05 10:07:31 +0000995 *length = 0;
996
Stefan Behrens63a212a2012-11-05 18:29:28 +0100997 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000998 return 0;
999
Yan Zheng2b820322008-11-17 21:11:30 -05001000 path = btrfs_alloc_path();
1001 if (!path)
1002 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001003 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001004
Chris Mason0b86a832008-03-24 15:01:56 -04001005 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001006 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001007 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001008
1009 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001010 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001011 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001012 if (ret > 0) {
1013 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1014 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001015 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001016 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001017
Chris Mason0b86a832008-03-24 15:01:56 -04001018 while (1) {
1019 l = path->nodes[0];
1020 slot = path->slots[0];
1021 if (slot >= btrfs_header_nritems(l)) {
1022 ret = btrfs_next_leaf(root, path);
1023 if (ret == 0)
1024 continue;
1025 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001026 goto out;
1027
1028 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001029 }
1030 btrfs_item_key_to_cpu(l, &key, slot);
1031
1032 if (key.objectid < device->devid)
1033 goto next;
1034
1035 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001036 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001037
David Sterba962a2982014-06-04 18:41:45 +02001038 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001039 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001040
Chris Mason0b86a832008-03-24 15:01:56 -04001041 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001042 extent_end = key.offset + btrfs_dev_extent_length(l,
1043 dev_extent);
1044 if (key.offset <= start && extent_end > end) {
1045 *length = end - start + 1;
1046 break;
1047 } else if (key.offset <= start && extent_end > start)
1048 *length += extent_end - start;
1049 else if (key.offset > start && extent_end <= end)
1050 *length += extent_end - key.offset;
1051 else if (key.offset > start && key.offset <= end) {
1052 *length += end - key.offset + 1;
1053 break;
1054 } else if (key.offset > end)
1055 break;
1056
1057next:
1058 path->slots[0]++;
1059 }
1060 ret = 0;
1061out:
1062 btrfs_free_path(path);
1063 return ret;
1064}
1065
Josef Bacik6df9a952013-06-27 13:22:46 -04001066static int contains_pending_extent(struct btrfs_trans_handle *trans,
1067 struct btrfs_device *device,
1068 u64 *start, u64 len)
1069{
1070 struct extent_map *em;
1071 int ret = 0;
1072
1073 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
1074 struct map_lookup *map;
1075 int i;
1076
1077 map = (struct map_lookup *)em->bdev;
1078 for (i = 0; i < map->num_stripes; i++) {
1079 if (map->stripes[i].dev != device)
1080 continue;
1081 if (map->stripes[i].physical >= *start + len ||
1082 map->stripes[i].physical + em->orig_block_len <=
1083 *start)
1084 continue;
1085 *start = map->stripes[i].physical +
1086 em->orig_block_len;
1087 ret = 1;
1088 }
1089 }
1090
1091 return ret;
1092}
1093
1094
Chris Mason0b86a832008-03-24 15:01:56 -04001095/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001096 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001097 * @device: the device which we search the free space in
1098 * @num_bytes: the size of the free space that we need
1099 * @start: store the start of the free space.
1100 * @len: the size of the free space. that we find, or the size of the max
1101 * free space if we don't find suitable free space
1102 *
Chris Mason0b86a832008-03-24 15:01:56 -04001103 * this uses a pretty simple search, the expectation is that it is
1104 * called very infrequently and that a given device has a small number
1105 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001106 *
1107 * @start is used to store the start of the free space if we find. But if we
1108 * don't find suitable free space, it will be used to store the start position
1109 * of the max free space.
1110 *
1111 * @len is used to store the size of the free space that we find.
1112 * But if we don't find suitable free space, it is used to store the size of
1113 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001114 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001115int find_free_dev_extent(struct btrfs_trans_handle *trans,
1116 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001117 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001118{
1119 struct btrfs_key key;
1120 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001121 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001122 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001123 u64 hole_size;
1124 u64 max_hole_start;
1125 u64 max_hole_size;
1126 u64 extent_end;
1127 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001128 u64 search_end = device->total_bytes;
1129 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001130 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001131 struct extent_buffer *l;
1132
Chris Mason0b86a832008-03-24 15:01:56 -04001133 /* FIXME use last free of some kind */
1134
1135 /* we don't want to overwrite the superblock on the drive,
1136 * so we make sure to start at an offset of at least 1MB
1137 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001138 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001139
Josef Bacik6df9a952013-06-27 13:22:46 -04001140 path = btrfs_alloc_path();
1141 if (!path)
1142 return -ENOMEM;
1143again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001144 max_hole_start = search_start;
1145 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001146 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001147
Stefan Behrens63a212a2012-11-05 18:29:28 +01001148 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001149 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001150 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001151 }
1152
Miao Xie7bfc8372011-01-05 10:07:26 +00001153 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001154 path->search_commit_root = 1;
1155 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001156
Chris Mason0b86a832008-03-24 15:01:56 -04001157 key.objectid = device->devid;
1158 key.offset = search_start;
1159 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001160
Li Zefan125ccb02011-12-08 15:07:24 +08001161 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001162 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001163 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001164 if (ret > 0) {
1165 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1166 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001167 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001168 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001169
Chris Mason0b86a832008-03-24 15:01:56 -04001170 while (1) {
1171 l = path->nodes[0];
1172 slot = path->slots[0];
1173 if (slot >= btrfs_header_nritems(l)) {
1174 ret = btrfs_next_leaf(root, path);
1175 if (ret == 0)
1176 continue;
1177 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001178 goto out;
1179
1180 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001181 }
1182 btrfs_item_key_to_cpu(l, &key, slot);
1183
1184 if (key.objectid < device->devid)
1185 goto next;
1186
1187 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001188 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001189
David Sterba962a2982014-06-04 18:41:45 +02001190 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001191 goto next;
1192
Miao Xie7bfc8372011-01-05 10:07:26 +00001193 if (key.offset > search_start) {
1194 hole_size = key.offset - search_start;
1195
Josef Bacik6df9a952013-06-27 13:22:46 -04001196 /*
1197 * Have to check before we set max_hole_start, otherwise
1198 * we could end up sending back this offset anyway.
1199 */
1200 if (contains_pending_extent(trans, device,
1201 &search_start,
1202 hole_size))
1203 hole_size = 0;
1204
Miao Xie7bfc8372011-01-05 10:07:26 +00001205 if (hole_size > max_hole_size) {
1206 max_hole_start = search_start;
1207 max_hole_size = hole_size;
1208 }
1209
1210 /*
1211 * If this free space is greater than which we need,
1212 * it must be the max free space that we have found
1213 * until now, so max_hole_start must point to the start
1214 * of this free space and the length of this free space
1215 * is stored in max_hole_size. Thus, we return
1216 * max_hole_start and max_hole_size and go back to the
1217 * caller.
1218 */
1219 if (hole_size >= num_bytes) {
1220 ret = 0;
1221 goto out;
1222 }
1223 }
1224
Chris Mason0b86a832008-03-24 15:01:56 -04001225 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001226 extent_end = key.offset + btrfs_dev_extent_length(l,
1227 dev_extent);
1228 if (extent_end > search_start)
1229 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001230next:
1231 path->slots[0]++;
1232 cond_resched();
1233 }
Chris Mason0b86a832008-03-24 15:01:56 -04001234
liubo38c01b92011-08-02 02:39:03 +00001235 /*
1236 * At this point, search_start should be the end of
1237 * allocated dev extents, and when shrinking the device,
1238 * search_end may be smaller than search_start.
1239 */
1240 if (search_end > search_start)
1241 hole_size = search_end - search_start;
1242
Miao Xie7bfc8372011-01-05 10:07:26 +00001243 if (hole_size > max_hole_size) {
1244 max_hole_start = search_start;
1245 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001246 }
Chris Mason0b86a832008-03-24 15:01:56 -04001247
Josef Bacik6df9a952013-06-27 13:22:46 -04001248 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1249 btrfs_release_path(path);
1250 goto again;
1251 }
1252
Miao Xie7bfc8372011-01-05 10:07:26 +00001253 /* See above. */
1254 if (hole_size < num_bytes)
1255 ret = -ENOSPC;
1256 else
1257 ret = 0;
1258
1259out:
Yan Zheng2b820322008-11-17 21:11:30 -05001260 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001261 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001262 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001263 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001264 return ret;
1265}
1266
Christoph Hellwigb2950862008-12-02 09:54:17 -05001267static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001268 struct btrfs_device *device,
1269 u64 start)
1270{
1271 int ret;
1272 struct btrfs_path *path;
1273 struct btrfs_root *root = device->dev_root;
1274 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001275 struct btrfs_key found_key;
1276 struct extent_buffer *leaf = NULL;
1277 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001278
1279 path = btrfs_alloc_path();
1280 if (!path)
1281 return -ENOMEM;
1282
1283 key.objectid = device->devid;
1284 key.offset = start;
1285 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001286again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001287 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001288 if (ret > 0) {
1289 ret = btrfs_previous_item(root, path, key.objectid,
1290 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001291 if (ret)
1292 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001293 leaf = path->nodes[0];
1294 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1295 extent = btrfs_item_ptr(leaf, path->slots[0],
1296 struct btrfs_dev_extent);
1297 BUG_ON(found_key.offset > start || found_key.offset +
1298 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001299 key = found_key;
1300 btrfs_release_path(path);
1301 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001302 } else if (ret == 0) {
1303 leaf = path->nodes[0];
1304 extent = btrfs_item_ptr(leaf, path->slots[0],
1305 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001306 } else {
1307 btrfs_error(root->fs_info, ret, "Slot search failed");
1308 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001309 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001310
Josef Bacik2bf64752011-09-26 17:12:22 -04001311 if (device->bytes_used > 0) {
1312 u64 len = btrfs_dev_extent_length(leaf, extent);
1313 device->bytes_used -= len;
1314 spin_lock(&root->fs_info->free_chunk_lock);
1315 root->fs_info->free_chunk_space += len;
1316 spin_unlock(&root->fs_info->free_chunk_lock);
1317 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001318 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001319 if (ret) {
1320 btrfs_error(root->fs_info, ret,
1321 "Failed to remove dev extent item");
1322 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001323out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001324 btrfs_free_path(path);
1325 return ret;
1326}
1327
Eric Sandeen48a3b632013-04-25 20:41:01 +00001328static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1329 struct btrfs_device *device,
1330 u64 chunk_tree, u64 chunk_objectid,
1331 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001332{
1333 int ret;
1334 struct btrfs_path *path;
1335 struct btrfs_root *root = device->dev_root;
1336 struct btrfs_dev_extent *extent;
1337 struct extent_buffer *leaf;
1338 struct btrfs_key key;
1339
Chris Masondfe25022008-05-13 13:46:40 -04001340 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001341 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001342 path = btrfs_alloc_path();
1343 if (!path)
1344 return -ENOMEM;
1345
Chris Mason0b86a832008-03-24 15:01:56 -04001346 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001347 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001348 key.type = BTRFS_DEV_EXTENT_KEY;
1349 ret = btrfs_insert_empty_item(trans, root, path, &key,
1350 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001351 if (ret)
1352 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001353
1354 leaf = path->nodes[0];
1355 extent = btrfs_item_ptr(leaf, path->slots[0],
1356 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001357 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1358 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1359 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1360
1361 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001362 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001363
Chris Mason0b86a832008-03-24 15:01:56 -04001364 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1365 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001366out:
Chris Mason0b86a832008-03-24 15:01:56 -04001367 btrfs_free_path(path);
1368 return ret;
1369}
1370
Josef Bacik6df9a952013-06-27 13:22:46 -04001371static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001372{
Josef Bacik6df9a952013-06-27 13:22:46 -04001373 struct extent_map_tree *em_tree;
1374 struct extent_map *em;
1375 struct rb_node *n;
1376 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001377
Josef Bacik6df9a952013-06-27 13:22:46 -04001378 em_tree = &fs_info->mapping_tree.map_tree;
1379 read_lock(&em_tree->lock);
1380 n = rb_last(&em_tree->map);
1381 if (n) {
1382 em = rb_entry(n, struct extent_map, rb_node);
1383 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001384 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001385 read_unlock(&em_tree->lock);
1386
Chris Mason0b86a832008-03-24 15:01:56 -04001387 return ret;
1388}
1389
Ilya Dryomov53f10652013-08-12 14:33:01 +03001390static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1391 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001392{
1393 int ret;
1394 struct btrfs_key key;
1395 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001396 struct btrfs_path *path;
1397
Yan Zheng2b820322008-11-17 21:11:30 -05001398 path = btrfs_alloc_path();
1399 if (!path)
1400 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001401
1402 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1403 key.type = BTRFS_DEV_ITEM_KEY;
1404 key.offset = (u64)-1;
1405
Ilya Dryomov53f10652013-08-12 14:33:01 +03001406 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001407 if (ret < 0)
1408 goto error;
1409
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001410 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001411
Ilya Dryomov53f10652013-08-12 14:33:01 +03001412 ret = btrfs_previous_item(fs_info->chunk_root, path,
1413 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001414 BTRFS_DEV_ITEM_KEY);
1415 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001416 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001417 } else {
1418 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1419 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001420 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001421 }
1422 ret = 0;
1423error:
Yan Zheng2b820322008-11-17 21:11:30 -05001424 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001425 return ret;
1426}
1427
1428/*
1429 * the device information is stored in the chunk root
1430 * the btrfs_device struct should be fully filled in
1431 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001432static int btrfs_add_device(struct btrfs_trans_handle *trans,
1433 struct btrfs_root *root,
1434 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001435{
1436 int ret;
1437 struct btrfs_path *path;
1438 struct btrfs_dev_item *dev_item;
1439 struct extent_buffer *leaf;
1440 struct btrfs_key key;
1441 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001442
1443 root = root->fs_info->chunk_root;
1444
1445 path = btrfs_alloc_path();
1446 if (!path)
1447 return -ENOMEM;
1448
Chris Mason0b86a832008-03-24 15:01:56 -04001449 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1450 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001451 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001452
1453 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001454 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001455 if (ret)
1456 goto out;
1457
1458 leaf = path->nodes[0];
1459 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1460
1461 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001462 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001463 btrfs_set_device_type(leaf, dev_item, device->type);
1464 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1465 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1466 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7df69d32014-07-24 11:37:13 +08001467 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001468 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001469 btrfs_set_device_group(leaf, dev_item, 0);
1470 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1471 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001472 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001473
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001474 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001475 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001476 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001477 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001478 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001479
Yan Zheng2b820322008-11-17 21:11:30 -05001480 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001481out:
1482 btrfs_free_path(path);
1483 return ret;
1484}
Chris Mason8f18cf12008-04-25 16:53:30 -04001485
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001486/*
1487 * Function to update ctime/mtime for a given device path.
1488 * Mainly used for ctime/mtime based probe like libblkid.
1489 */
1490static void update_dev_time(char *path_name)
1491{
1492 struct file *filp;
1493
1494 filp = filp_open(path_name, O_RDWR, 0);
1495 if (!filp)
1496 return;
1497 file_update_time(filp);
1498 filp_close(filp, NULL);
1499 return;
1500}
1501
Chris Masona061fc82008-05-07 11:43:44 -04001502static int btrfs_rm_dev_item(struct btrfs_root *root,
1503 struct btrfs_device *device)
1504{
1505 int ret;
1506 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001507 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001508 struct btrfs_trans_handle *trans;
1509
1510 root = root->fs_info->chunk_root;
1511
1512 path = btrfs_alloc_path();
1513 if (!path)
1514 return -ENOMEM;
1515
Yan, Zhenga22285a2010-05-16 10:48:46 -04001516 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001517 if (IS_ERR(trans)) {
1518 btrfs_free_path(path);
1519 return PTR_ERR(trans);
1520 }
Chris Masona061fc82008-05-07 11:43:44 -04001521 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1522 key.type = BTRFS_DEV_ITEM_KEY;
1523 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001524 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001525
1526 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1527 if (ret < 0)
1528 goto out;
1529
1530 if (ret > 0) {
1531 ret = -ENOENT;
1532 goto out;
1533 }
1534
1535 ret = btrfs_del_item(trans, root, path);
1536 if (ret)
1537 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001538out:
1539 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001540 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001541 btrfs_commit_transaction(trans, root);
1542 return ret;
1543}
1544
1545int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1546{
1547 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001548 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001549 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001550 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001551 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001552 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001553 u64 all_avail;
1554 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001555 u64 num_devices;
1556 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001557 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001558 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001559 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001560
Chris Masona061fc82008-05-07 11:43:44 -04001561 mutex_lock(&uuid_mutex);
1562
Miao Xiede98ced2013-01-29 10:13:12 +00001563 do {
1564 seq = read_seqbegin(&root->fs_info->profiles_lock);
1565
1566 all_avail = root->fs_info->avail_data_alloc_bits |
1567 root->fs_info->avail_system_alloc_bits |
1568 root->fs_info->avail_metadata_alloc_bits;
1569 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001570
Stefan Behrens8dabb742012-11-06 13:15:27 +01001571 num_devices = root->fs_info->fs_devices->num_devices;
1572 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1573 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1574 WARN_ON(num_devices < 1);
1575 num_devices--;
1576 }
1577 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1578
1579 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001580 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001581 goto out;
1582 }
1583
Stefan Behrens8dabb742012-11-06 13:15:27 +01001584 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001585 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001586 goto out;
1587 }
1588
David Woodhouse53b381b2013-01-29 18:40:14 -05001589 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1590 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001591 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001592 goto out;
1593 }
1594 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1595 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001596 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001597 goto out;
1598 }
1599
Chris Masondfe25022008-05-13 13:46:40 -04001600 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001601 struct list_head *devices;
1602 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001603
Chris Masondfe25022008-05-13 13:46:40 -04001604 device = NULL;
1605 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001606 /*
1607 * It is safe to read the devices since the volume_mutex
1608 * is held.
1609 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001610 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001611 if (tmp->in_fs_metadata &&
1612 !tmp->is_tgtdev_for_dev_replace &&
1613 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001614 device = tmp;
1615 break;
1616 }
1617 }
1618 bdev = NULL;
1619 bh = NULL;
1620 disk_super = NULL;
1621 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001622 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001623 goto out;
1624 }
Chris Masondfe25022008-05-13 13:46:40 -04001625 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001626 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001627 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001628 root->fs_info->bdev_holder, 0,
1629 &bdev, &bh);
1630 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001631 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001632 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001633 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001634 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001635 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001636 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001637 if (!device) {
1638 ret = -ENOENT;
1639 goto error_brelse;
1640 }
Chris Masondfe25022008-05-13 13:46:40 -04001641 }
Yan Zheng2b820322008-11-17 21:11:30 -05001642
Stefan Behrens63a212a2012-11-05 18:29:28 +01001643 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001644 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001645 goto error_brelse;
1646 }
1647
Yan Zheng2b820322008-11-17 21:11:30 -05001648 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001649 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001650 goto error_brelse;
1651 }
1652
1653 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001654 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001655 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001656 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001657 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001658 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001659 }
Chris Masona061fc82008-05-07 11:43:44 -04001660
Carey Underwoodd7901552013-03-04 16:37:06 -06001661 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001662 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001663 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001664 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001665 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001666
Stefan Behrens63a212a2012-11-05 18:29:28 +01001667 /*
1668 * TODO: the superblock still includes this device in its num_devices
1669 * counter although write_all_supers() is not locked out. This
1670 * could give a filesystem state which requires a degraded mount.
1671 */
Chris Masona061fc82008-05-07 11:43:44 -04001672 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1673 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001674 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001675
Josef Bacik2bf64752011-09-26 17:12:22 -04001676 spin_lock(&root->fs_info->free_chunk_lock);
1677 root->fs_info->free_chunk_space = device->total_bytes -
1678 device->bytes_used;
1679 spin_unlock(&root->fs_info->free_chunk_lock);
1680
Yan Zheng2b820322008-11-17 21:11:30 -05001681 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001682 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001683
1684 /*
1685 * the device list mutex makes sure that we don't change
1686 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001687 * the device supers. Whoever is writing all supers, should
1688 * lock the device list mutex before getting the number of
1689 * devices in the super block (super_copy). Conversely,
1690 * whoever updates the number of devices in the super block
1691 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001692 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001693
1694 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001695 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001696 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001697
Yan Zhenge4404d62008-12-12 10:03:26 -05001698 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001699 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001700
Chris Masoncd02dca2010-12-13 14:56:23 -05001701 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001702 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001703
Yan Zheng2b820322008-11-17 21:11:30 -05001704 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1705 struct btrfs_device, dev_list);
1706 if (device->bdev == root->fs_info->sb->s_bdev)
1707 root->fs_info->sb->s_bdev = next_device->bdev;
1708 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1709 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1710
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001711 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001712 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001713 /* remove sysfs entry */
1714 btrfs_kobj_rm_device(root->fs_info, device);
1715 }
Anand Jain99994cd2014-06-03 11:36:00 +08001716
Xiao Guangrong1f781602011-04-20 10:09:16 +00001717 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001718
David Sterba6c417612011-04-13 15:41:04 +02001719 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1720 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001721 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001722
Xiao Guangrong1f781602011-04-20 10:09:16 +00001723 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001724 struct btrfs_fs_devices *fs_devices;
1725 fs_devices = root->fs_info->fs_devices;
1726 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001727 if (fs_devices->seed == cur_devices) {
1728 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001729 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001730 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001731 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001732 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001733 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001734 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001735 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001736 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001737 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001738 }
1739
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001740 root->fs_info->num_tolerated_disk_barrier_failures =
1741 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1742
Yan Zheng2b820322008-11-17 21:11:30 -05001743 /*
1744 * at this point, the device is zero sized. We want to
1745 * remove it from the devices list and zero out the old super
1746 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001747 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001748 u64 bytenr;
1749 int i;
1750
Chris Masondfe25022008-05-13 13:46:40 -04001751 /* make sure this device isn't detected as part of
1752 * the FS anymore
1753 */
1754 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1755 set_buffer_dirty(bh);
1756 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001757
1758 /* clear the mirror copies of super block on the disk
1759 * being removed, 0th copy is been taken care above and
1760 * the below would take of the rest
1761 */
1762 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1763 bytenr = btrfs_sb_offset(i);
1764 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1765 i_size_read(bdev->bd_inode))
1766 break;
1767
1768 brelse(bh);
1769 bh = __bread(bdev, bytenr / 4096,
1770 BTRFS_SUPER_INFO_SIZE);
1771 if (!bh)
1772 continue;
1773
1774 disk_super = (struct btrfs_super_block *)bh->b_data;
1775
1776 if (btrfs_super_bytenr(disk_super) != bytenr ||
1777 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1778 continue;
1779 }
1780 memset(&disk_super->magic, 0,
1781 sizeof(disk_super->magic));
1782 set_buffer_dirty(bh);
1783 sync_dirty_buffer(bh);
1784 }
Chris Masondfe25022008-05-13 13:46:40 -04001785 }
Chris Masona061fc82008-05-07 11:43:44 -04001786
Chris Masona061fc82008-05-07 11:43:44 -04001787 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001788
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001789 if (bdev) {
1790 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001791 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001792
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001793 /* Update ctime/mtime for device path for libblkid */
1794 update_dev_time(device_path);
1795 }
1796
Chris Masona061fc82008-05-07 11:43:44 -04001797error_brelse:
1798 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001799 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001800 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001801out:
1802 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001803 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001804error_undo:
1805 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001806 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001807 list_add(&device->dev_alloc_list,
1808 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001809 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001810 root->fs_info->fs_devices->rw_devices++;
1811 }
1812 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001813}
1814
Stefan Behrense93c89c2012-11-05 17:33:06 +01001815void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1816 struct btrfs_device *srcdev)
1817{
Anand Jaind51908c2014-08-13 14:24:19 +08001818 struct btrfs_fs_devices *fs_devices;
1819
Stefan Behrense93c89c2012-11-05 17:33:06 +01001820 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001821
Anand Jain25e8e912014-08-20 10:56:56 +08001822 /*
1823 * in case of fs with no seed, srcdev->fs_devices will point
1824 * to fs_devices of fs_info. However when the dev being replaced is
1825 * a seed dev it will point to the seed's local fs_devices. In short
1826 * srcdev will have its correct fs_devices in both the cases.
1827 */
1828 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001829
Stefan Behrense93c89c2012-11-05 17:33:06 +01001830 list_del_rcu(&srcdev->dev_list);
1831 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001832 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001833 if (srcdev->missing) {
Anand Jaind51908c2014-08-13 14:24:19 +08001834 fs_devices->missing_devices--;
1835 fs_devices->rw_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001836 }
1837 if (srcdev->can_discard)
Anand Jaind51908c2014-08-13 14:24:19 +08001838 fs_devices->num_can_discard--;
Ilya Dryomov13572722013-10-02 20:41:01 +03001839 if (srcdev->bdev) {
Anand Jaind51908c2014-08-13 14:24:19 +08001840 fs_devices->open_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001841
Miao Xieff61d172014-07-24 11:37:06 +08001842 /*
1843 * zero out the old super if it is not writable
1844 * (e.g. seed device)
1845 */
1846 if (srcdev->writeable)
1847 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03001848 }
1849
Stefan Behrense93c89c2012-11-05 17:33:06 +01001850 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001851
1852 /*
1853 * unless fs_devices is seed fs, num_devices shouldn't go
1854 * zero
1855 */
1856 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1857
1858 /* if this is no devs we rather delete the fs_devices */
1859 if (!fs_devices->num_devices) {
1860 struct btrfs_fs_devices *tmp_fs_devices;
1861
1862 tmp_fs_devices = fs_info->fs_devices;
1863 while (tmp_fs_devices) {
1864 if (tmp_fs_devices->seed == fs_devices) {
1865 tmp_fs_devices->seed = fs_devices->seed;
1866 break;
1867 }
1868 tmp_fs_devices = tmp_fs_devices->seed;
1869 }
1870 fs_devices->seed = NULL;
1871 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01001872}
1873
1874void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1875 struct btrfs_device *tgtdev)
1876{
1877 struct btrfs_device *next_device;
1878
1879 WARN_ON(!tgtdev);
1880 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1881 if (tgtdev->bdev) {
1882 btrfs_scratch_superblock(tgtdev);
1883 fs_info->fs_devices->open_devices--;
1884 }
1885 fs_info->fs_devices->num_devices--;
1886 if (tgtdev->can_discard)
1887 fs_info->fs_devices->num_can_discard++;
1888
1889 next_device = list_entry(fs_info->fs_devices->devices.next,
1890 struct btrfs_device, dev_list);
1891 if (tgtdev->bdev == fs_info->sb->s_bdev)
1892 fs_info->sb->s_bdev = next_device->bdev;
1893 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1894 fs_info->fs_devices->latest_bdev = next_device->bdev;
1895 list_del_rcu(&tgtdev->dev_list);
1896
1897 call_rcu(&tgtdev->rcu, free_device);
1898
1899 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1900}
1901
Eric Sandeen48a3b632013-04-25 20:41:01 +00001902static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1903 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001904{
1905 int ret = 0;
1906 struct btrfs_super_block *disk_super;
1907 u64 devid;
1908 u8 *dev_uuid;
1909 struct block_device *bdev;
1910 struct buffer_head *bh;
1911
1912 *device = NULL;
1913 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1914 root->fs_info->bdev_holder, 0, &bdev, &bh);
1915 if (ret)
1916 return ret;
1917 disk_super = (struct btrfs_super_block *)bh->b_data;
1918 devid = btrfs_stack_device_id(&disk_super->dev_item);
1919 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001920 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001921 disk_super->fsid);
1922 brelse(bh);
1923 if (!*device)
1924 ret = -ENOENT;
1925 blkdev_put(bdev, FMODE_READ);
1926 return ret;
1927}
1928
1929int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1930 char *device_path,
1931 struct btrfs_device **device)
1932{
1933 *device = NULL;
1934 if (strcmp(device_path, "missing") == 0) {
1935 struct list_head *devices;
1936 struct btrfs_device *tmp;
1937
1938 devices = &root->fs_info->fs_devices->devices;
1939 /*
1940 * It is safe to read the devices since the volume_mutex
1941 * is held by the caller.
1942 */
1943 list_for_each_entry(tmp, devices, dev_list) {
1944 if (tmp->in_fs_metadata && !tmp->bdev) {
1945 *device = tmp;
1946 break;
1947 }
1948 }
1949
1950 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001951 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001952 return -ENOENT;
1953 }
1954
1955 return 0;
1956 } else {
1957 return btrfs_find_device_by_path(root, device_path, device);
1958 }
1959}
1960
Yan Zheng2b820322008-11-17 21:11:30 -05001961/*
1962 * does all the dirty work required for changing file system's UUID.
1963 */
Li Zefan125ccb02011-12-08 15:07:24 +08001964static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001965{
1966 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1967 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001968 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001969 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001970 struct btrfs_device *device;
1971 u64 super_flags;
1972
1973 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001974 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001975 return -EINVAL;
1976
Ilya Dryomov2208a372013-08-12 14:33:03 +03001977 seed_devices = __alloc_fs_devices();
1978 if (IS_ERR(seed_devices))
1979 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001980
Yan Zhenge4404d62008-12-12 10:03:26 -05001981 old_devices = clone_fs_devices(fs_devices);
1982 if (IS_ERR(old_devices)) {
1983 kfree(seed_devices);
1984 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001985 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001986
Yan Zheng2b820322008-11-17 21:11:30 -05001987 list_add(&old_devices->list, &fs_uuids);
1988
Yan Zhenge4404d62008-12-12 10:03:26 -05001989 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1990 seed_devices->opened = 1;
1991 INIT_LIST_HEAD(&seed_devices->devices);
1992 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001993 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001994
1995 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001996 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1997 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001998
Yan Zhenge4404d62008-12-12 10:03:26 -05001999 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2000 list_for_each_entry(device, &seed_devices->devices, dev_list) {
2001 device->fs_devices = seed_devices;
2002 }
2003
Yan Zheng2b820322008-11-17 21:11:30 -05002004 fs_devices->seeding = 0;
2005 fs_devices->num_devices = 0;
2006 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002007 fs_devices->missing_devices = 0;
2008 fs_devices->num_can_discard = 0;
2009 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002010 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002011
2012 generate_random_uuid(fs_devices->fsid);
2013 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2014 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002015 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2016
Yan Zheng2b820322008-11-17 21:11:30 -05002017 super_flags = btrfs_super_flags(disk_super) &
2018 ~BTRFS_SUPER_FLAG_SEEDING;
2019 btrfs_set_super_flags(disk_super, super_flags);
2020
2021 return 0;
2022}
2023
2024/*
2025 * strore the expected generation for seed devices in device items.
2026 */
2027static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2028 struct btrfs_root *root)
2029{
2030 struct btrfs_path *path;
2031 struct extent_buffer *leaf;
2032 struct btrfs_dev_item *dev_item;
2033 struct btrfs_device *device;
2034 struct btrfs_key key;
2035 u8 fs_uuid[BTRFS_UUID_SIZE];
2036 u8 dev_uuid[BTRFS_UUID_SIZE];
2037 u64 devid;
2038 int ret;
2039
2040 path = btrfs_alloc_path();
2041 if (!path)
2042 return -ENOMEM;
2043
2044 root = root->fs_info->chunk_root;
2045 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2046 key.offset = 0;
2047 key.type = BTRFS_DEV_ITEM_KEY;
2048
2049 while (1) {
2050 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2051 if (ret < 0)
2052 goto error;
2053
2054 leaf = path->nodes[0];
2055next_slot:
2056 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2057 ret = btrfs_next_leaf(root, path);
2058 if (ret > 0)
2059 break;
2060 if (ret < 0)
2061 goto error;
2062 leaf = path->nodes[0];
2063 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002064 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002065 continue;
2066 }
2067
2068 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2069 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2070 key.type != BTRFS_DEV_ITEM_KEY)
2071 break;
2072
2073 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2074 struct btrfs_dev_item);
2075 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002076 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002077 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002078 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002079 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002080 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2081 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002082 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002083
2084 if (device->fs_devices->seeding) {
2085 btrfs_set_device_generation(leaf, dev_item,
2086 device->generation);
2087 btrfs_mark_buffer_dirty(leaf);
2088 }
2089
2090 path->slots[0]++;
2091 goto next_slot;
2092 }
2093 ret = 0;
2094error:
2095 btrfs_free_path(path);
2096 return ret;
2097}
2098
Chris Mason788f20e2008-04-28 15:29:42 -04002099int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2100{
Josef Bacikd5e20032011-08-04 14:52:27 +00002101 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002102 struct btrfs_trans_handle *trans;
2103 struct btrfs_device *device;
2104 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002105 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002106 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002107 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04002108 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002109 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002110 int ret = 0;
2111
Yan Zheng2b820322008-11-17 21:11:30 -05002112 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002113 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002114
Li Zefana5d16332011-12-07 20:08:40 -05002115 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002116 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002117 if (IS_ERR(bdev))
2118 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002119
Yan Zheng2b820322008-11-17 21:11:30 -05002120 if (root->fs_info->fs_devices->seeding) {
2121 seeding_dev = 1;
2122 down_write(&sb->s_umount);
2123 mutex_lock(&uuid_mutex);
2124 }
2125
Chris Mason8c8bee12008-09-29 11:19:10 -04002126 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002127
Chris Mason788f20e2008-04-28 15:29:42 -04002128 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002129
2130 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002131 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002132 if (device->bdev == bdev) {
2133 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002134 mutex_unlock(
2135 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002136 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002137 }
2138 }
Liu Bod25628b2012-11-14 14:35:30 +00002139 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002140
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002141 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2142 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002143 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002144 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002145 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002146 }
2147
Josef Bacik606686e2012-06-04 14:03:51 -04002148 name = rcu_string_strdup(device_path, GFP_NOFS);
2149 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002150 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002151 ret = -ENOMEM;
2152 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002153 }
Josef Bacik606686e2012-06-04 14:03:51 -04002154 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002155
Yan, Zhenga22285a2010-05-16 10:48:46 -04002156 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002157 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002158 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002159 kfree(device);
2160 ret = PTR_ERR(trans);
2161 goto error;
2162 }
2163
Yan Zheng2b820322008-11-17 21:11:30 -05002164 lock_chunks(root);
2165
Josef Bacikd5e20032011-08-04 14:52:27 +00002166 q = bdev_get_queue(bdev);
2167 if (blk_queue_discard(q))
2168 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002169 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002170 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002171 device->io_width = root->sectorsize;
2172 device->io_align = root->sectorsize;
2173 device->sector_size = root->sectorsize;
2174 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002175 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002176 device->dev_root = root->fs_info->dev_root;
2177 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002178 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002179 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002180 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002181 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002182 set_blocksize(device->bdev, 4096);
2183
Yan Zheng2b820322008-11-17 21:11:30 -05002184 if (seeding_dev) {
2185 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002186 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002187 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002188 }
2189
2190 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002191
Chris Masone5e9a522009-06-10 15:17:02 -04002192 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002193 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002194 list_add(&device->dev_alloc_list,
2195 &root->fs_info->fs_devices->alloc_list);
2196 root->fs_info->fs_devices->num_devices++;
2197 root->fs_info->fs_devices->open_devices++;
2198 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002199 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002200 if (device->can_discard)
2201 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002202 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2203
Josef Bacik2bf64752011-09-26 17:12:22 -04002204 spin_lock(&root->fs_info->free_chunk_lock);
2205 root->fs_info->free_chunk_space += device->total_bytes;
2206 spin_unlock(&root->fs_info->free_chunk_lock);
2207
Chris Masonc2898112009-06-10 09:51:32 -04002208 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2209 root->fs_info->fs_devices->rotating = 1;
2210
David Sterba6c417612011-04-13 15:41:04 +02002211 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2212 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002213 total_bytes + device->total_bytes);
2214
David Sterba6c417612011-04-13 15:41:04 +02002215 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2216 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002217 total_bytes + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002218
2219 /* add sysfs device entry */
2220 btrfs_kobj_add_device(root->fs_info, device);
2221
Chris Masone5e9a522009-06-10 15:17:02 -04002222 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002223
Yan Zheng2b820322008-11-17 21:11:30 -05002224 if (seeding_dev) {
Anand Jainb2373f22014-06-03 11:36:03 +08002225 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002226 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002227 if (ret) {
2228 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002229 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002230 }
Yan Zheng2b820322008-11-17 21:11:30 -05002231 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002232 if (ret) {
2233 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002234 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002235 }
Anand Jainb2373f22014-06-03 11:36:03 +08002236
2237 /* Sprouting would change fsid of the mounted root,
2238 * so rename the fsid on the sysfs
2239 */
2240 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2241 root->fs_info->fsid);
2242 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2243 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002244 } else {
2245 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002246 if (ret) {
2247 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002248 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002249 }
Yan Zheng2b820322008-11-17 21:11:30 -05002250 }
2251
Chris Mason913d9522009-03-10 13:17:18 -04002252 /*
2253 * we've got more storage, clear any full flags on the space
2254 * infos
2255 */
2256 btrfs_clear_space_info_full(root->fs_info);
2257
Chris Mason7d9eb122008-07-08 14:19:17 -04002258 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002259 root->fs_info->num_tolerated_disk_barrier_failures =
2260 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002261 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002262
2263 if (seeding_dev) {
2264 mutex_unlock(&uuid_mutex);
2265 up_write(&sb->s_umount);
2266
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002267 if (ret) /* transaction commit */
2268 return ret;
2269
Yan Zheng2b820322008-11-17 21:11:30 -05002270 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002271 if (ret < 0)
2272 btrfs_error(root->fs_info, ret,
2273 "Failed to relocate sys chunks after "
2274 "device initialization. This can be fixed "
2275 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002276 trans = btrfs_attach_transaction(root);
2277 if (IS_ERR(trans)) {
2278 if (PTR_ERR(trans) == -ENOENT)
2279 return 0;
2280 return PTR_ERR(trans);
2281 }
2282 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002283 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002284
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002285 /* Update ctime/mtime for libblkid */
2286 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002287 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002288
2289error_trans:
2290 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002291 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002292 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002293 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002294 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002295error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002296 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002297 if (seeding_dev) {
2298 mutex_unlock(&uuid_mutex);
2299 up_write(&sb->s_umount);
2300 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002301 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002302}
2303
Stefan Behrense93c89c2012-11-05 17:33:06 +01002304int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2305 struct btrfs_device **device_out)
2306{
2307 struct request_queue *q;
2308 struct btrfs_device *device;
2309 struct block_device *bdev;
2310 struct btrfs_fs_info *fs_info = root->fs_info;
2311 struct list_head *devices;
2312 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002313 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002314 int ret = 0;
2315
2316 *device_out = NULL;
2317 if (fs_info->fs_devices->seeding)
2318 return -EINVAL;
2319
2320 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2321 fs_info->bdev_holder);
2322 if (IS_ERR(bdev))
2323 return PTR_ERR(bdev);
2324
2325 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2326
2327 devices = &fs_info->fs_devices->devices;
2328 list_for_each_entry(device, devices, dev_list) {
2329 if (device->bdev == bdev) {
2330 ret = -EEXIST;
2331 goto error;
2332 }
2333 }
2334
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002335 device = btrfs_alloc_device(NULL, &devid, NULL);
2336 if (IS_ERR(device)) {
2337 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002338 goto error;
2339 }
2340
2341 name = rcu_string_strdup(device_path, GFP_NOFS);
2342 if (!name) {
2343 kfree(device);
2344 ret = -ENOMEM;
2345 goto error;
2346 }
2347 rcu_assign_pointer(device->name, name);
2348
2349 q = bdev_get_queue(bdev);
2350 if (blk_queue_discard(q))
2351 device->can_discard = 1;
2352 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2353 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002354 device->generation = 0;
2355 device->io_width = root->sectorsize;
2356 device->io_align = root->sectorsize;
2357 device->sector_size = root->sectorsize;
2358 device->total_bytes = i_size_read(bdev->bd_inode);
2359 device->disk_total_bytes = device->total_bytes;
2360 device->dev_root = fs_info->dev_root;
2361 device->bdev = bdev;
2362 device->in_fs_metadata = 1;
2363 device->is_tgtdev_for_dev_replace = 1;
2364 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002365 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002366 set_blocksize(device->bdev, 4096);
2367 device->fs_devices = fs_info->fs_devices;
2368 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2369 fs_info->fs_devices->num_devices++;
2370 fs_info->fs_devices->open_devices++;
2371 if (device->can_discard)
2372 fs_info->fs_devices->num_can_discard++;
2373 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2374
2375 *device_out = device;
2376 return ret;
2377
2378error:
2379 blkdev_put(bdev, FMODE_EXCL);
2380 return ret;
2381}
2382
2383void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2384 struct btrfs_device *tgtdev)
2385{
2386 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2387 tgtdev->io_width = fs_info->dev_root->sectorsize;
2388 tgtdev->io_align = fs_info->dev_root->sectorsize;
2389 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2390 tgtdev->dev_root = fs_info->dev_root;
2391 tgtdev->in_fs_metadata = 1;
2392}
2393
Chris Masond3977122009-01-05 21:25:51 -05002394static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2395 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002396{
2397 int ret;
2398 struct btrfs_path *path;
2399 struct btrfs_root *root;
2400 struct btrfs_dev_item *dev_item;
2401 struct extent_buffer *leaf;
2402 struct btrfs_key key;
2403
2404 root = device->dev_root->fs_info->chunk_root;
2405
2406 path = btrfs_alloc_path();
2407 if (!path)
2408 return -ENOMEM;
2409
2410 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2411 key.type = BTRFS_DEV_ITEM_KEY;
2412 key.offset = device->devid;
2413
2414 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2415 if (ret < 0)
2416 goto out;
2417
2418 if (ret > 0) {
2419 ret = -ENOENT;
2420 goto out;
2421 }
2422
2423 leaf = path->nodes[0];
2424 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2425
2426 btrfs_set_device_id(leaf, dev_item, device->devid);
2427 btrfs_set_device_type(leaf, dev_item, device->type);
2428 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2429 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2430 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002431 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002432 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2433 btrfs_mark_buffer_dirty(leaf);
2434
2435out:
2436 btrfs_free_path(path);
2437 return ret;
2438}
2439
Chris Mason7d9eb122008-07-08 14:19:17 -04002440static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002441 struct btrfs_device *device, u64 new_size)
2442{
2443 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002444 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002445 u64 old_total = btrfs_super_total_bytes(super_copy);
2446 u64 diff = new_size - device->total_bytes;
2447
Yan Zheng2b820322008-11-17 21:11:30 -05002448 if (!device->writeable)
2449 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002450 if (new_size <= device->total_bytes ||
2451 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002452 return -EINVAL;
2453
Chris Mason8f18cf12008-04-25 16:53:30 -04002454 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002455 device->fs_devices->total_rw_bytes += diff;
2456
2457 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002458 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002459 btrfs_clear_space_info_full(device->dev_root->fs_info);
2460
Chris Mason8f18cf12008-04-25 16:53:30 -04002461 return btrfs_update_device(trans, device);
2462}
2463
Chris Mason7d9eb122008-07-08 14:19:17 -04002464int btrfs_grow_device(struct btrfs_trans_handle *trans,
2465 struct btrfs_device *device, u64 new_size)
2466{
2467 int ret;
2468 lock_chunks(device->dev_root);
2469 ret = __btrfs_grow_device(trans, device, new_size);
2470 unlock_chunks(device->dev_root);
2471 return ret;
2472}
2473
Chris Mason8f18cf12008-04-25 16:53:30 -04002474static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2475 struct btrfs_root *root,
2476 u64 chunk_tree, u64 chunk_objectid,
2477 u64 chunk_offset)
2478{
2479 int ret;
2480 struct btrfs_path *path;
2481 struct btrfs_key key;
2482
2483 root = root->fs_info->chunk_root;
2484 path = btrfs_alloc_path();
2485 if (!path)
2486 return -ENOMEM;
2487
2488 key.objectid = chunk_objectid;
2489 key.offset = chunk_offset;
2490 key.type = BTRFS_CHUNK_ITEM_KEY;
2491
2492 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002493 if (ret < 0)
2494 goto out;
2495 else if (ret > 0) { /* Logic error or corruption */
2496 btrfs_error(root->fs_info, -ENOENT,
2497 "Failed lookup while freeing chunk.");
2498 ret = -ENOENT;
2499 goto out;
2500 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002501
2502 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002503 if (ret < 0)
2504 btrfs_error(root->fs_info, ret,
2505 "Failed to delete chunk item.");
2506out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002507 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002508 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002509}
2510
Christoph Hellwigb2950862008-12-02 09:54:17 -05002511static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002512 chunk_offset)
2513{
David Sterba6c417612011-04-13 15:41:04 +02002514 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002515 struct btrfs_disk_key *disk_key;
2516 struct btrfs_chunk *chunk;
2517 u8 *ptr;
2518 int ret = 0;
2519 u32 num_stripes;
2520 u32 array_size;
2521 u32 len = 0;
2522 u32 cur;
2523 struct btrfs_key key;
2524
2525 array_size = btrfs_super_sys_array_size(super_copy);
2526
2527 ptr = super_copy->sys_chunk_array;
2528 cur = 0;
2529
2530 while (cur < array_size) {
2531 disk_key = (struct btrfs_disk_key *)ptr;
2532 btrfs_disk_key_to_cpu(&key, disk_key);
2533
2534 len = sizeof(*disk_key);
2535
2536 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2537 chunk = (struct btrfs_chunk *)(ptr + len);
2538 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2539 len += btrfs_chunk_item_size(num_stripes);
2540 } else {
2541 ret = -EIO;
2542 break;
2543 }
2544 if (key.objectid == chunk_objectid &&
2545 key.offset == chunk_offset) {
2546 memmove(ptr, ptr + len, array_size - (cur + len));
2547 array_size -= len;
2548 btrfs_set_super_sys_array_size(super_copy, array_size);
2549 } else {
2550 ptr += len;
2551 cur += len;
2552 }
2553 }
2554 return ret;
2555}
2556
Christoph Hellwigb2950862008-12-02 09:54:17 -05002557static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002558 u64 chunk_tree, u64 chunk_objectid,
2559 u64 chunk_offset)
2560{
2561 struct extent_map_tree *em_tree;
2562 struct btrfs_root *extent_root;
2563 struct btrfs_trans_handle *trans;
2564 struct extent_map *em;
2565 struct map_lookup *map;
2566 int ret;
2567 int i;
2568
2569 root = root->fs_info->chunk_root;
2570 extent_root = root->fs_info->extent_root;
2571 em_tree = &root->fs_info->mapping_tree.map_tree;
2572
Josef Bacikba1bf482009-09-11 16:11:19 -04002573 ret = btrfs_can_relocate(extent_root, chunk_offset);
2574 if (ret)
2575 return -ENOSPC;
2576
Chris Mason8f18cf12008-04-25 16:53:30 -04002577 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002578 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002579 if (ret)
2580 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002581
Yan, Zhenga22285a2010-05-16 10:48:46 -04002582 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002583 if (IS_ERR(trans)) {
2584 ret = PTR_ERR(trans);
2585 btrfs_std_error(root->fs_info, ret);
2586 return ret;
2587 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002588
Chris Mason7d9eb122008-07-08 14:19:17 -04002589 lock_chunks(root);
2590
Chris Mason8f18cf12008-04-25 16:53:30 -04002591 /*
2592 * step two, delete the device extents and the
2593 * chunk tree entries
2594 */
Chris Mason890871b2009-09-02 16:24:52 -04002595 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002596 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002597 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002598
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002599 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002600 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002601 map = (struct map_lookup *)em->bdev;
2602
2603 for (i = 0; i < map->num_stripes; i++) {
2604 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2605 map->stripes[i].physical);
2606 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002607
Chris Masondfe25022008-05-13 13:46:40 -04002608 if (map->stripes[i].dev) {
2609 ret = btrfs_update_device(trans, map->stripes[i].dev);
2610 BUG_ON(ret);
2611 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002612 }
2613 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2614 chunk_offset);
2615
2616 BUG_ON(ret);
2617
liubo1abe9b82011-03-24 11:18:59 +00002618 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2619
Chris Mason8f18cf12008-04-25 16:53:30 -04002620 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2621 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2622 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002623 }
2624
Zheng Yan1a40e232008-09-26 10:09:34 -04002625 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2626 BUG_ON(ret);
2627
Chris Mason890871b2009-09-02 16:24:52 -04002628 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002629 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002630 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002631
Chris Mason8f18cf12008-04-25 16:53:30 -04002632 /* once for the tree */
2633 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002634 /* once for us */
2635 free_extent_map(em);
2636
Chris Mason7d9eb122008-07-08 14:19:17 -04002637 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002638 btrfs_end_transaction(trans, root);
2639 return 0;
2640}
2641
Yan Zheng2b820322008-11-17 21:11:30 -05002642static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2643{
2644 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2645 struct btrfs_path *path;
2646 struct extent_buffer *leaf;
2647 struct btrfs_chunk *chunk;
2648 struct btrfs_key key;
2649 struct btrfs_key found_key;
2650 u64 chunk_tree = chunk_root->root_key.objectid;
2651 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002652 bool retried = false;
2653 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002654 int ret;
2655
2656 path = btrfs_alloc_path();
2657 if (!path)
2658 return -ENOMEM;
2659
Josef Bacikba1bf482009-09-11 16:11:19 -04002660again:
Yan Zheng2b820322008-11-17 21:11:30 -05002661 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2662 key.offset = (u64)-1;
2663 key.type = BTRFS_CHUNK_ITEM_KEY;
2664
2665 while (1) {
2666 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2667 if (ret < 0)
2668 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002669 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002670
2671 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2672 key.type);
2673 if (ret < 0)
2674 goto error;
2675 if (ret > 0)
2676 break;
2677
2678 leaf = path->nodes[0];
2679 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2680
2681 chunk = btrfs_item_ptr(leaf, path->slots[0],
2682 struct btrfs_chunk);
2683 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002684 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002685
2686 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2687 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2688 found_key.objectid,
2689 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002690 if (ret == -ENOSPC)
2691 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302692 else
2693 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002694 }
2695
2696 if (found_key.offset == 0)
2697 break;
2698 key.offset = found_key.offset - 1;
2699 }
2700 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002701 if (failed && !retried) {
2702 failed = 0;
2703 retried = true;
2704 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302705 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002706 ret = -ENOSPC;
2707 }
Yan Zheng2b820322008-11-17 21:11:30 -05002708error:
2709 btrfs_free_path(path);
2710 return ret;
2711}
2712
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002713static int insert_balance_item(struct btrfs_root *root,
2714 struct btrfs_balance_control *bctl)
2715{
2716 struct btrfs_trans_handle *trans;
2717 struct btrfs_balance_item *item;
2718 struct btrfs_disk_balance_args disk_bargs;
2719 struct btrfs_path *path;
2720 struct extent_buffer *leaf;
2721 struct btrfs_key key;
2722 int ret, err;
2723
2724 path = btrfs_alloc_path();
2725 if (!path)
2726 return -ENOMEM;
2727
2728 trans = btrfs_start_transaction(root, 0);
2729 if (IS_ERR(trans)) {
2730 btrfs_free_path(path);
2731 return PTR_ERR(trans);
2732 }
2733
2734 key.objectid = BTRFS_BALANCE_OBJECTID;
2735 key.type = BTRFS_BALANCE_ITEM_KEY;
2736 key.offset = 0;
2737
2738 ret = btrfs_insert_empty_item(trans, root, path, &key,
2739 sizeof(*item));
2740 if (ret)
2741 goto out;
2742
2743 leaf = path->nodes[0];
2744 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2745
2746 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2747
2748 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2749 btrfs_set_balance_data(leaf, item, &disk_bargs);
2750 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2751 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2752 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2753 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2754
2755 btrfs_set_balance_flags(leaf, item, bctl->flags);
2756
2757 btrfs_mark_buffer_dirty(leaf);
2758out:
2759 btrfs_free_path(path);
2760 err = btrfs_commit_transaction(trans, root);
2761 if (err && !ret)
2762 ret = err;
2763 return ret;
2764}
2765
2766static int del_balance_item(struct btrfs_root *root)
2767{
2768 struct btrfs_trans_handle *trans;
2769 struct btrfs_path *path;
2770 struct btrfs_key key;
2771 int ret, err;
2772
2773 path = btrfs_alloc_path();
2774 if (!path)
2775 return -ENOMEM;
2776
2777 trans = btrfs_start_transaction(root, 0);
2778 if (IS_ERR(trans)) {
2779 btrfs_free_path(path);
2780 return PTR_ERR(trans);
2781 }
2782
2783 key.objectid = BTRFS_BALANCE_OBJECTID;
2784 key.type = BTRFS_BALANCE_ITEM_KEY;
2785 key.offset = 0;
2786
2787 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2788 if (ret < 0)
2789 goto out;
2790 if (ret > 0) {
2791 ret = -ENOENT;
2792 goto out;
2793 }
2794
2795 ret = btrfs_del_item(trans, root, path);
2796out:
2797 btrfs_free_path(path);
2798 err = btrfs_commit_transaction(trans, root);
2799 if (err && !ret)
2800 ret = err;
2801 return ret;
2802}
2803
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002804/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002805 * This is a heuristic used to reduce the number of chunks balanced on
2806 * resume after balance was interrupted.
2807 */
2808static void update_balance_args(struct btrfs_balance_control *bctl)
2809{
2810 /*
2811 * Turn on soft mode for chunk types that were being converted.
2812 */
2813 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2814 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2815 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2816 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2817 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2818 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2819
2820 /*
2821 * Turn on usage filter if is not already used. The idea is
2822 * that chunks that we have already balanced should be
2823 * reasonably full. Don't do it for chunks that are being
2824 * converted - that will keep us from relocating unconverted
2825 * (albeit full) chunks.
2826 */
2827 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2828 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2829 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2830 bctl->data.usage = 90;
2831 }
2832 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2833 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2834 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2835 bctl->sys.usage = 90;
2836 }
2837 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2838 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2839 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2840 bctl->meta.usage = 90;
2841 }
2842}
2843
2844/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002845 * Should be called with both balance and volume mutexes held to
2846 * serialize other volume operations (add_dev/rm_dev/resize) with
2847 * restriper. Same goes for unset_balance_control.
2848 */
2849static void set_balance_control(struct btrfs_balance_control *bctl)
2850{
2851 struct btrfs_fs_info *fs_info = bctl->fs_info;
2852
2853 BUG_ON(fs_info->balance_ctl);
2854
2855 spin_lock(&fs_info->balance_lock);
2856 fs_info->balance_ctl = bctl;
2857 spin_unlock(&fs_info->balance_lock);
2858}
2859
2860static void unset_balance_control(struct btrfs_fs_info *fs_info)
2861{
2862 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2863
2864 BUG_ON(!fs_info->balance_ctl);
2865
2866 spin_lock(&fs_info->balance_lock);
2867 fs_info->balance_ctl = NULL;
2868 spin_unlock(&fs_info->balance_lock);
2869
2870 kfree(bctl);
2871}
2872
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002873/*
2874 * Balance filters. Return 1 if chunk should be filtered out
2875 * (should not be balanced).
2876 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002877static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002878 struct btrfs_balance_args *bargs)
2879{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002880 chunk_type = chunk_to_extended(chunk_type) &
2881 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002882
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002883 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002884 return 0;
2885
2886 return 1;
2887}
2888
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002889static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2890 struct btrfs_balance_args *bargs)
2891{
2892 struct btrfs_block_group_cache *cache;
2893 u64 chunk_used, user_thresh;
2894 int ret = 1;
2895
2896 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2897 chunk_used = btrfs_block_group_used(&cache->item);
2898
Ilya Dryomova105bb82013-01-21 15:15:56 +02002899 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002900 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002901 else if (bargs->usage > 100)
2902 user_thresh = cache->key.offset;
2903 else
2904 user_thresh = div_factor_fine(cache->key.offset,
2905 bargs->usage);
2906
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002907 if (chunk_used < user_thresh)
2908 ret = 0;
2909
2910 btrfs_put_block_group(cache);
2911 return ret;
2912}
2913
Ilya Dryomov409d4042012-01-16 22:04:47 +02002914static int chunk_devid_filter(struct extent_buffer *leaf,
2915 struct btrfs_chunk *chunk,
2916 struct btrfs_balance_args *bargs)
2917{
2918 struct btrfs_stripe *stripe;
2919 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2920 int i;
2921
2922 for (i = 0; i < num_stripes; i++) {
2923 stripe = btrfs_stripe_nr(chunk, i);
2924 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2925 return 0;
2926 }
2927
2928 return 1;
2929}
2930
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002931/* [pstart, pend) */
2932static int chunk_drange_filter(struct extent_buffer *leaf,
2933 struct btrfs_chunk *chunk,
2934 u64 chunk_offset,
2935 struct btrfs_balance_args *bargs)
2936{
2937 struct btrfs_stripe *stripe;
2938 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2939 u64 stripe_offset;
2940 u64 stripe_length;
2941 int factor;
2942 int i;
2943
2944 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2945 return 0;
2946
2947 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002948 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2949 factor = num_stripes / 2;
2950 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2951 factor = num_stripes - 1;
2952 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2953 factor = num_stripes - 2;
2954 } else {
2955 factor = num_stripes;
2956 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002957
2958 for (i = 0; i < num_stripes; i++) {
2959 stripe = btrfs_stripe_nr(chunk, i);
2960 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2961 continue;
2962
2963 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2964 stripe_length = btrfs_chunk_length(leaf, chunk);
2965 do_div(stripe_length, factor);
2966
2967 if (stripe_offset < bargs->pend &&
2968 stripe_offset + stripe_length > bargs->pstart)
2969 return 0;
2970 }
2971
2972 return 1;
2973}
2974
Ilya Dryomovea671762012-01-16 22:04:48 +02002975/* [vstart, vend) */
2976static int chunk_vrange_filter(struct extent_buffer *leaf,
2977 struct btrfs_chunk *chunk,
2978 u64 chunk_offset,
2979 struct btrfs_balance_args *bargs)
2980{
2981 if (chunk_offset < bargs->vend &&
2982 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2983 /* at least part of the chunk is inside this vrange */
2984 return 0;
2985
2986 return 1;
2987}
2988
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002989static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002990 struct btrfs_balance_args *bargs)
2991{
2992 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2993 return 0;
2994
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002995 chunk_type = chunk_to_extended(chunk_type) &
2996 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002997
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002998 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002999 return 1;
3000
3001 return 0;
3002}
3003
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003004static int should_balance_chunk(struct btrfs_root *root,
3005 struct extent_buffer *leaf,
3006 struct btrfs_chunk *chunk, u64 chunk_offset)
3007{
3008 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3009 struct btrfs_balance_args *bargs = NULL;
3010 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3011
3012 /* type filter */
3013 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3014 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3015 return 0;
3016 }
3017
3018 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3019 bargs = &bctl->data;
3020 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3021 bargs = &bctl->sys;
3022 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3023 bargs = &bctl->meta;
3024
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003025 /* profiles filter */
3026 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3027 chunk_profiles_filter(chunk_type, bargs)) {
3028 return 0;
3029 }
3030
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003031 /* usage filter */
3032 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3033 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3034 return 0;
3035 }
3036
Ilya Dryomov409d4042012-01-16 22:04:47 +02003037 /* devid filter */
3038 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3039 chunk_devid_filter(leaf, chunk, bargs)) {
3040 return 0;
3041 }
3042
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003043 /* drange filter, makes sense only with devid filter */
3044 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3045 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3046 return 0;
3047 }
3048
Ilya Dryomovea671762012-01-16 22:04:48 +02003049 /* vrange filter */
3050 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3051 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3052 return 0;
3053 }
3054
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003055 /* soft profile changing mode */
3056 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3057 chunk_soft_convert_filter(chunk_type, bargs)) {
3058 return 0;
3059 }
3060
David Sterba7d824b62014-05-07 17:37:51 +02003061 /*
3062 * limited by count, must be the last filter
3063 */
3064 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3065 if (bargs->limit == 0)
3066 return 0;
3067 else
3068 bargs->limit--;
3069 }
3070
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003071 return 1;
3072}
3073
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003074static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003075{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003076 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003077 struct btrfs_root *chunk_root = fs_info->chunk_root;
3078 struct btrfs_root *dev_root = fs_info->dev_root;
3079 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003080 struct btrfs_device *device;
3081 u64 old_size;
3082 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003083 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003084 struct btrfs_path *path;
3085 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003086 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003087 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003088 struct extent_buffer *leaf;
3089 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003090 int ret;
3091 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003092 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003093 u64 limit_data = bctl->data.limit;
3094 u64 limit_meta = bctl->meta.limit;
3095 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003096
Chris Masonec44a352008-04-28 15:29:52 -04003097 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003098 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003099 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04003100 old_size = device->total_bytes;
3101 size_to_free = div_factor(old_size, 1);
3102 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003103 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003104 device->total_bytes - device->bytes_used > size_to_free ||
3105 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003106 continue;
3107
3108 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003109 if (ret == -ENOSPC)
3110 break;
Chris Masonec44a352008-04-28 15:29:52 -04003111 BUG_ON(ret);
3112
Yan, Zhenga22285a2010-05-16 10:48:46 -04003113 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003114 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003115
3116 ret = btrfs_grow_device(trans, device, old_size);
3117 BUG_ON(ret);
3118
3119 btrfs_end_transaction(trans, dev_root);
3120 }
3121
3122 /* step two, relocate all the chunks */
3123 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003124 if (!path) {
3125 ret = -ENOMEM;
3126 goto error;
3127 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003128
3129 /* zero out stat counters */
3130 spin_lock(&fs_info->balance_lock);
3131 memset(&bctl->stat, 0, sizeof(bctl->stat));
3132 spin_unlock(&fs_info->balance_lock);
3133again:
David Sterba7d824b62014-05-07 17:37:51 +02003134 if (!counting) {
3135 bctl->data.limit = limit_data;
3136 bctl->meta.limit = limit_meta;
3137 bctl->sys.limit = limit_sys;
3138 }
Chris Masonec44a352008-04-28 15:29:52 -04003139 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3140 key.offset = (u64)-1;
3141 key.type = BTRFS_CHUNK_ITEM_KEY;
3142
Chris Masond3977122009-01-05 21:25:51 -05003143 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003144 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003145 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003146 ret = -ECANCELED;
3147 goto error;
3148 }
3149
Chris Masonec44a352008-04-28 15:29:52 -04003150 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3151 if (ret < 0)
3152 goto error;
3153
3154 /*
3155 * this shouldn't happen, it means the last relocate
3156 * failed
3157 */
3158 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003159 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003160
3161 ret = btrfs_previous_item(chunk_root, path, 0,
3162 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003163 if (ret) {
3164 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003165 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003166 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003167
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003168 leaf = path->nodes[0];
3169 slot = path->slots[0];
3170 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3171
Chris Masonec44a352008-04-28 15:29:52 -04003172 if (found_key.objectid != key.objectid)
3173 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003174
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003175 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3176
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003177 if (!counting) {
3178 spin_lock(&fs_info->balance_lock);
3179 bctl->stat.considered++;
3180 spin_unlock(&fs_info->balance_lock);
3181 }
3182
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003183 ret = should_balance_chunk(chunk_root, leaf, chunk,
3184 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003185 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003186 if (!ret)
3187 goto loop;
3188
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003189 if (counting) {
3190 spin_lock(&fs_info->balance_lock);
3191 bctl->stat.expected++;
3192 spin_unlock(&fs_info->balance_lock);
3193 goto loop;
3194 }
3195
Chris Masonec44a352008-04-28 15:29:52 -04003196 ret = btrfs_relocate_chunk(chunk_root,
3197 chunk_root->root_key.objectid,
3198 found_key.objectid,
3199 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003200 if (ret && ret != -ENOSPC)
3201 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003202 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003203 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003204 } else {
3205 spin_lock(&fs_info->balance_lock);
3206 bctl->stat.completed++;
3207 spin_unlock(&fs_info->balance_lock);
3208 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003209loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003210 if (found_key.offset == 0)
3211 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003212 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003213 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003214
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003215 if (counting) {
3216 btrfs_release_path(path);
3217 counting = false;
3218 goto again;
3219 }
Chris Masonec44a352008-04-28 15:29:52 -04003220error:
3221 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003222 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003223 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003224 enospc_errors);
3225 if (!ret)
3226 ret = -ENOSPC;
3227 }
3228
Chris Masonec44a352008-04-28 15:29:52 -04003229 return ret;
3230}
3231
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003232/**
3233 * alloc_profile_is_valid - see if a given profile is valid and reduced
3234 * @flags: profile to validate
3235 * @extended: if true @flags is treated as an extended profile
3236 */
3237static int alloc_profile_is_valid(u64 flags, int extended)
3238{
3239 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3240 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3241
3242 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3243
3244 /* 1) check that all other bits are zeroed */
3245 if (flags & ~mask)
3246 return 0;
3247
3248 /* 2) see if profile is reduced */
3249 if (flags == 0)
3250 return !extended; /* "0" is valid for usual profiles */
3251
3252 /* true if exactly one bit set */
3253 return (flags & (flags - 1)) == 0;
3254}
3255
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003256static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3257{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003258 /* cancel requested || normal exit path */
3259 return atomic_read(&fs_info->balance_cancel_req) ||
3260 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3261 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003262}
3263
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003264static void __cancel_balance(struct btrfs_fs_info *fs_info)
3265{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003266 int ret;
3267
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003268 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003269 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003270 if (ret)
3271 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003272
3273 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003274}
3275
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003276/*
3277 * Should be called with both balance and volume mutexes held
3278 */
3279int btrfs_balance(struct btrfs_balance_control *bctl,
3280 struct btrfs_ioctl_balance_args *bargs)
3281{
3282 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003283 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003284 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003285 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003286 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003287 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003288
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003289 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003290 atomic_read(&fs_info->balance_pause_req) ||
3291 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003292 ret = -EINVAL;
3293 goto out;
3294 }
3295
Ilya Dryomove4837f82012-03-27 17:09:17 +03003296 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3297 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3298 mixed = 1;
3299
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003300 /*
3301 * In case of mixed groups both data and meta should be picked,
3302 * and identical options should be given for both of them.
3303 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003304 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3305 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003306 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3307 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3308 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003309 btrfs_err(fs_info, "with mixed groups data and "
3310 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003311 ret = -EINVAL;
3312 goto out;
3313 }
3314 }
3315
Stefan Behrens8dabb742012-11-06 13:15:27 +01003316 num_devices = fs_info->fs_devices->num_devices;
3317 btrfs_dev_replace_lock(&fs_info->dev_replace);
3318 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3319 BUG_ON(num_devices < 1);
3320 num_devices--;
3321 }
3322 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003323 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003324 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003325 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003326 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003327 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003328 if (num_devices > 2)
3329 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3330 if (num_devices > 3)
3331 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3332 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003333 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3334 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3335 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003336 btrfs_err(fs_info, "unable to start balance with target "
3337 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003338 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003339 ret = -EINVAL;
3340 goto out;
3341 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003342 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3343 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3344 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003345 btrfs_err(fs_info,
3346 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003347 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003348 ret = -EINVAL;
3349 goto out;
3350 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003351 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3352 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3353 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003354 btrfs_err(fs_info,
3355 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003356 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003357 ret = -EINVAL;
3358 goto out;
3359 }
3360
Ilya Dryomove4837f82012-03-27 17:09:17 +03003361 /* allow dup'ed data chunks only in mixed mode */
3362 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003363 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003364 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003365 ret = -EINVAL;
3366 goto out;
3367 }
3368
3369 /* allow to reduce meta or sys integrity only if force set */
3370 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003371 BTRFS_BLOCK_GROUP_RAID10 |
3372 BTRFS_BLOCK_GROUP_RAID5 |
3373 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003374 do {
3375 seq = read_seqbegin(&fs_info->profiles_lock);
3376
3377 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3378 (fs_info->avail_system_alloc_bits & allowed) &&
3379 !(bctl->sys.target & allowed)) ||
3380 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3381 (fs_info->avail_metadata_alloc_bits & allowed) &&
3382 !(bctl->meta.target & allowed))) {
3383 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003384 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003385 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003386 btrfs_err(fs_info, "balance will reduce metadata "
3387 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003388 ret = -EINVAL;
3389 goto out;
3390 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003391 }
Miao Xiede98ced2013-01-29 10:13:12 +00003392 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003393
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003394 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3395 int num_tolerated_disk_barrier_failures;
3396 u64 target = bctl->sys.target;
3397
3398 num_tolerated_disk_barrier_failures =
3399 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3400 if (num_tolerated_disk_barrier_failures > 0 &&
3401 (target &
3402 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3403 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3404 num_tolerated_disk_barrier_failures = 0;
3405 else if (num_tolerated_disk_barrier_failures > 1 &&
3406 (target &
3407 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3408 num_tolerated_disk_barrier_failures = 1;
3409
3410 fs_info->num_tolerated_disk_barrier_failures =
3411 num_tolerated_disk_barrier_failures;
3412 }
3413
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003414 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003415 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003416 goto out;
3417
Ilya Dryomov59641012012-01-16 22:04:48 +02003418 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3419 BUG_ON(ret == -EEXIST);
3420 set_balance_control(bctl);
3421 } else {
3422 BUG_ON(ret != -EEXIST);
3423 spin_lock(&fs_info->balance_lock);
3424 update_balance_args(bctl);
3425 spin_unlock(&fs_info->balance_lock);
3426 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003427
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003428 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003429 mutex_unlock(&fs_info->balance_mutex);
3430
3431 ret = __btrfs_balance(fs_info);
3432
3433 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003434 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003435
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003436 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3437 fs_info->num_tolerated_disk_barrier_failures =
3438 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3439 }
3440
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003441 if (bargs) {
3442 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003443 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003444 }
3445
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003446 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3447 balance_need_close(fs_info)) {
3448 __cancel_balance(fs_info);
3449 }
3450
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003451 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003452
3453 return ret;
3454out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003455 if (bctl->flags & BTRFS_BALANCE_RESUME)
3456 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003457 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003458 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003459 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3460 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003461 return ret;
3462}
3463
3464static int balance_kthread(void *data)
3465{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003466 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003467 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003468
3469 mutex_lock(&fs_info->volume_mutex);
3470 mutex_lock(&fs_info->balance_mutex);
3471
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003472 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003473 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003474 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003475 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003476
3477 mutex_unlock(&fs_info->balance_mutex);
3478 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003479
Ilya Dryomov59641012012-01-16 22:04:48 +02003480 return ret;
3481}
3482
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003483int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3484{
3485 struct task_struct *tsk;
3486
3487 spin_lock(&fs_info->balance_lock);
3488 if (!fs_info->balance_ctl) {
3489 spin_unlock(&fs_info->balance_lock);
3490 return 0;
3491 }
3492 spin_unlock(&fs_info->balance_lock);
3493
3494 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003495 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003496 return 0;
3497 }
3498
3499 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303500 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003501}
3502
Ilya Dryomov68310a52012-06-22 12:24:12 -06003503int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003504{
Ilya Dryomov59641012012-01-16 22:04:48 +02003505 struct btrfs_balance_control *bctl;
3506 struct btrfs_balance_item *item;
3507 struct btrfs_disk_balance_args disk_bargs;
3508 struct btrfs_path *path;
3509 struct extent_buffer *leaf;
3510 struct btrfs_key key;
3511 int ret;
3512
3513 path = btrfs_alloc_path();
3514 if (!path)
3515 return -ENOMEM;
3516
Ilya Dryomov68310a52012-06-22 12:24:12 -06003517 key.objectid = BTRFS_BALANCE_OBJECTID;
3518 key.type = BTRFS_BALANCE_ITEM_KEY;
3519 key.offset = 0;
3520
3521 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3522 if (ret < 0)
3523 goto out;
3524 if (ret > 0) { /* ret = -ENOENT; */
3525 ret = 0;
3526 goto out;
3527 }
3528
Ilya Dryomov59641012012-01-16 22:04:48 +02003529 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3530 if (!bctl) {
3531 ret = -ENOMEM;
3532 goto out;
3533 }
3534
Ilya Dryomov59641012012-01-16 22:04:48 +02003535 leaf = path->nodes[0];
3536 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3537
Ilya Dryomov68310a52012-06-22 12:24:12 -06003538 bctl->fs_info = fs_info;
3539 bctl->flags = btrfs_balance_flags(leaf, item);
3540 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003541
3542 btrfs_balance_data(leaf, item, &disk_bargs);
3543 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3544 btrfs_balance_meta(leaf, item, &disk_bargs);
3545 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3546 btrfs_balance_sys(leaf, item, &disk_bargs);
3547 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3548
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003549 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3550
Ilya Dryomov68310a52012-06-22 12:24:12 -06003551 mutex_lock(&fs_info->volume_mutex);
3552 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003553
Ilya Dryomov68310a52012-06-22 12:24:12 -06003554 set_balance_control(bctl);
3555
3556 mutex_unlock(&fs_info->balance_mutex);
3557 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003558out:
3559 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003560 return ret;
3561}
3562
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003563int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3564{
3565 int ret = 0;
3566
3567 mutex_lock(&fs_info->balance_mutex);
3568 if (!fs_info->balance_ctl) {
3569 mutex_unlock(&fs_info->balance_mutex);
3570 return -ENOTCONN;
3571 }
3572
3573 if (atomic_read(&fs_info->balance_running)) {
3574 atomic_inc(&fs_info->balance_pause_req);
3575 mutex_unlock(&fs_info->balance_mutex);
3576
3577 wait_event(fs_info->balance_wait_q,
3578 atomic_read(&fs_info->balance_running) == 0);
3579
3580 mutex_lock(&fs_info->balance_mutex);
3581 /* we are good with balance_ctl ripped off from under us */
3582 BUG_ON(atomic_read(&fs_info->balance_running));
3583 atomic_dec(&fs_info->balance_pause_req);
3584 } else {
3585 ret = -ENOTCONN;
3586 }
3587
3588 mutex_unlock(&fs_info->balance_mutex);
3589 return ret;
3590}
3591
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003592int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3593{
Ilya Dryomove649e582013-10-10 20:40:21 +03003594 if (fs_info->sb->s_flags & MS_RDONLY)
3595 return -EROFS;
3596
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003597 mutex_lock(&fs_info->balance_mutex);
3598 if (!fs_info->balance_ctl) {
3599 mutex_unlock(&fs_info->balance_mutex);
3600 return -ENOTCONN;
3601 }
3602
3603 atomic_inc(&fs_info->balance_cancel_req);
3604 /*
3605 * if we are running just wait and return, balance item is
3606 * deleted in btrfs_balance in this case
3607 */
3608 if (atomic_read(&fs_info->balance_running)) {
3609 mutex_unlock(&fs_info->balance_mutex);
3610 wait_event(fs_info->balance_wait_q,
3611 atomic_read(&fs_info->balance_running) == 0);
3612 mutex_lock(&fs_info->balance_mutex);
3613 } else {
3614 /* __cancel_balance needs volume_mutex */
3615 mutex_unlock(&fs_info->balance_mutex);
3616 mutex_lock(&fs_info->volume_mutex);
3617 mutex_lock(&fs_info->balance_mutex);
3618
3619 if (fs_info->balance_ctl)
3620 __cancel_balance(fs_info);
3621
3622 mutex_unlock(&fs_info->volume_mutex);
3623 }
3624
3625 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3626 atomic_dec(&fs_info->balance_cancel_req);
3627 mutex_unlock(&fs_info->balance_mutex);
3628 return 0;
3629}
3630
Stefan Behrens803b2f52013-08-15 17:11:21 +02003631static int btrfs_uuid_scan_kthread(void *data)
3632{
3633 struct btrfs_fs_info *fs_info = data;
3634 struct btrfs_root *root = fs_info->tree_root;
3635 struct btrfs_key key;
3636 struct btrfs_key max_key;
3637 struct btrfs_path *path = NULL;
3638 int ret = 0;
3639 struct extent_buffer *eb;
3640 int slot;
3641 struct btrfs_root_item root_item;
3642 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003643 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003644
3645 path = btrfs_alloc_path();
3646 if (!path) {
3647 ret = -ENOMEM;
3648 goto out;
3649 }
3650
3651 key.objectid = 0;
3652 key.type = BTRFS_ROOT_ITEM_KEY;
3653 key.offset = 0;
3654
3655 max_key.objectid = (u64)-1;
3656 max_key.type = BTRFS_ROOT_ITEM_KEY;
3657 max_key.offset = (u64)-1;
3658
Stefan Behrens803b2f52013-08-15 17:11:21 +02003659 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003660 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003661 if (ret) {
3662 if (ret > 0)
3663 ret = 0;
3664 break;
3665 }
3666
3667 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3668 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3669 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3670 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3671 goto skip;
3672
3673 eb = path->nodes[0];
3674 slot = path->slots[0];
3675 item_size = btrfs_item_size_nr(eb, slot);
3676 if (item_size < sizeof(root_item))
3677 goto skip;
3678
Stefan Behrens803b2f52013-08-15 17:11:21 +02003679 read_extent_buffer(eb, &root_item,
3680 btrfs_item_ptr_offset(eb, slot),
3681 (int)sizeof(root_item));
3682 if (btrfs_root_refs(&root_item) == 0)
3683 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003684
3685 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3686 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3687 if (trans)
3688 goto update_tree;
3689
3690 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003691 /*
3692 * 1 - subvol uuid item
3693 * 1 - received_subvol uuid item
3694 */
3695 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3696 if (IS_ERR(trans)) {
3697 ret = PTR_ERR(trans);
3698 break;
3699 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003700 continue;
3701 } else {
3702 goto skip;
3703 }
3704update_tree:
3705 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003706 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3707 root_item.uuid,
3708 BTRFS_UUID_KEY_SUBVOL,
3709 key.objectid);
3710 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003711 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003712 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003713 break;
3714 }
3715 }
3716
3717 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003718 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3719 root_item.received_uuid,
3720 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3721 key.objectid);
3722 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003723 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003724 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003725 break;
3726 }
3727 }
3728
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003729skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003730 if (trans) {
3731 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003732 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003733 if (ret)
3734 break;
3735 }
3736
Stefan Behrens803b2f52013-08-15 17:11:21 +02003737 btrfs_release_path(path);
3738 if (key.offset < (u64)-1) {
3739 key.offset++;
3740 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3741 key.offset = 0;
3742 key.type = BTRFS_ROOT_ITEM_KEY;
3743 } else if (key.objectid < (u64)-1) {
3744 key.offset = 0;
3745 key.type = BTRFS_ROOT_ITEM_KEY;
3746 key.objectid++;
3747 } else {
3748 break;
3749 }
3750 cond_resched();
3751 }
3752
3753out:
3754 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003755 if (trans && !IS_ERR(trans))
3756 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003757 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003758 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003759 else
3760 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003761 up(&fs_info->uuid_tree_rescan_sem);
3762 return 0;
3763}
3764
Stefan Behrens70f80172013-08-15 17:11:23 +02003765/*
3766 * Callback for btrfs_uuid_tree_iterate().
3767 * returns:
3768 * 0 check succeeded, the entry is not outdated.
3769 * < 0 if an error occured.
3770 * > 0 if the check failed, which means the caller shall remove the entry.
3771 */
3772static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3773 u8 *uuid, u8 type, u64 subid)
3774{
3775 struct btrfs_key key;
3776 int ret = 0;
3777 struct btrfs_root *subvol_root;
3778
3779 if (type != BTRFS_UUID_KEY_SUBVOL &&
3780 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3781 goto out;
3782
3783 key.objectid = subid;
3784 key.type = BTRFS_ROOT_ITEM_KEY;
3785 key.offset = (u64)-1;
3786 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3787 if (IS_ERR(subvol_root)) {
3788 ret = PTR_ERR(subvol_root);
3789 if (ret == -ENOENT)
3790 ret = 1;
3791 goto out;
3792 }
3793
3794 switch (type) {
3795 case BTRFS_UUID_KEY_SUBVOL:
3796 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3797 ret = 1;
3798 break;
3799 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3800 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3801 BTRFS_UUID_SIZE))
3802 ret = 1;
3803 break;
3804 }
3805
3806out:
3807 return ret;
3808}
3809
3810static int btrfs_uuid_rescan_kthread(void *data)
3811{
3812 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3813 int ret;
3814
3815 /*
3816 * 1st step is to iterate through the existing UUID tree and
3817 * to delete all entries that contain outdated data.
3818 * 2nd step is to add all missing entries to the UUID tree.
3819 */
3820 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3821 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003822 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003823 up(&fs_info->uuid_tree_rescan_sem);
3824 return ret;
3825 }
3826 return btrfs_uuid_scan_kthread(data);
3827}
3828
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003829int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3830{
3831 struct btrfs_trans_handle *trans;
3832 struct btrfs_root *tree_root = fs_info->tree_root;
3833 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003834 struct task_struct *task;
3835 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003836
3837 /*
3838 * 1 - root node
3839 * 1 - root item
3840 */
3841 trans = btrfs_start_transaction(tree_root, 2);
3842 if (IS_ERR(trans))
3843 return PTR_ERR(trans);
3844
3845 uuid_root = btrfs_create_tree(trans, fs_info,
3846 BTRFS_UUID_TREE_OBJECTID);
3847 if (IS_ERR(uuid_root)) {
3848 btrfs_abort_transaction(trans, tree_root,
3849 PTR_ERR(uuid_root));
3850 return PTR_ERR(uuid_root);
3851 }
3852
3853 fs_info->uuid_root = uuid_root;
3854
Stefan Behrens803b2f52013-08-15 17:11:21 +02003855 ret = btrfs_commit_transaction(trans, tree_root);
3856 if (ret)
3857 return ret;
3858
3859 down(&fs_info->uuid_tree_rescan_sem);
3860 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3861 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003862 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003863 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003864 up(&fs_info->uuid_tree_rescan_sem);
3865 return PTR_ERR(task);
3866 }
3867
3868 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003869}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003870
Stefan Behrens70f80172013-08-15 17:11:23 +02003871int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3872{
3873 struct task_struct *task;
3874
3875 down(&fs_info->uuid_tree_rescan_sem);
3876 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3877 if (IS_ERR(task)) {
3878 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003879 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003880 up(&fs_info->uuid_tree_rescan_sem);
3881 return PTR_ERR(task);
3882 }
3883
3884 return 0;
3885}
3886
Chris Mason8f18cf12008-04-25 16:53:30 -04003887/*
3888 * shrinking a device means finding all of the device extents past
3889 * the new size, and then following the back refs to the chunks.
3890 * The chunk relocation code actually frees the device extent
3891 */
3892int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3893{
3894 struct btrfs_trans_handle *trans;
3895 struct btrfs_root *root = device->dev_root;
3896 struct btrfs_dev_extent *dev_extent = NULL;
3897 struct btrfs_path *path;
3898 u64 length;
3899 u64 chunk_tree;
3900 u64 chunk_objectid;
3901 u64 chunk_offset;
3902 int ret;
3903 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003904 int failed = 0;
3905 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003906 struct extent_buffer *l;
3907 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003908 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003909 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003910 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003911 u64 diff = device->total_bytes - new_size;
3912
Stefan Behrens63a212a2012-11-05 18:29:28 +01003913 if (device->is_tgtdev_for_dev_replace)
3914 return -EINVAL;
3915
Chris Mason8f18cf12008-04-25 16:53:30 -04003916 path = btrfs_alloc_path();
3917 if (!path)
3918 return -ENOMEM;
3919
Chris Mason8f18cf12008-04-25 16:53:30 -04003920 path->reada = 2;
3921
Chris Mason7d9eb122008-07-08 14:19:17 -04003922 lock_chunks(root);
3923
Chris Mason8f18cf12008-04-25 16:53:30 -04003924 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003925 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003926 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003927 spin_lock(&root->fs_info->free_chunk_lock);
3928 root->fs_info->free_chunk_space -= diff;
3929 spin_unlock(&root->fs_info->free_chunk_lock);
3930 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003931 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003932
Josef Bacikba1bf482009-09-11 16:11:19 -04003933again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003934 key.objectid = device->devid;
3935 key.offset = (u64)-1;
3936 key.type = BTRFS_DEV_EXTENT_KEY;
3937
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003938 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003939 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3940 if (ret < 0)
3941 goto done;
3942
3943 ret = btrfs_previous_item(root, path, 0, key.type);
3944 if (ret < 0)
3945 goto done;
3946 if (ret) {
3947 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003948 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003949 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003950 }
3951
3952 l = path->nodes[0];
3953 slot = path->slots[0];
3954 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3955
Josef Bacikba1bf482009-09-11 16:11:19 -04003956 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003957 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003958 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003959 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003960
3961 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3962 length = btrfs_dev_extent_length(l, dev_extent);
3963
Josef Bacikba1bf482009-09-11 16:11:19 -04003964 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003965 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003966 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003967 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003968
3969 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3970 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3971 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003972 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003973
3974 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3975 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003976 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003977 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003978 if (ret == -ENOSPC)
3979 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003980 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003981
3982 if (failed && !retried) {
3983 failed = 0;
3984 retried = true;
3985 goto again;
3986 } else if (failed && retried) {
3987 ret = -ENOSPC;
3988 lock_chunks(root);
3989
3990 device->total_bytes = old_size;
3991 if (device->writeable)
3992 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003993 spin_lock(&root->fs_info->free_chunk_lock);
3994 root->fs_info->free_chunk_space += diff;
3995 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003996 unlock_chunks(root);
3997 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003998 }
3999
Chris Balld6397ba2009-04-27 07:29:03 -04004000 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004001 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004002 if (IS_ERR(trans)) {
4003 ret = PTR_ERR(trans);
4004 goto done;
4005 }
4006
Chris Balld6397ba2009-04-27 07:29:03 -04004007 lock_chunks(root);
4008
4009 device->disk_total_bytes = new_size;
4010 /* Now btrfs_update_device() will change the on-disk size. */
4011 ret = btrfs_update_device(trans, device);
4012 if (ret) {
4013 unlock_chunks(root);
4014 btrfs_end_transaction(trans, root);
4015 goto done;
4016 }
4017 WARN_ON(diff > old_total);
4018 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4019 unlock_chunks(root);
4020 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004021done:
4022 btrfs_free_path(path);
4023 return ret;
4024}
4025
Li Zefan125ccb02011-12-08 15:07:24 +08004026static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004027 struct btrfs_key *key,
4028 struct btrfs_chunk *chunk, int item_size)
4029{
David Sterba6c417612011-04-13 15:41:04 +02004030 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004031 struct btrfs_disk_key disk_key;
4032 u32 array_size;
4033 u8 *ptr;
4034
4035 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004036 if (array_size + item_size + sizeof(disk_key)
4037 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
Chris Mason0b86a832008-03-24 15:01:56 -04004038 return -EFBIG;
4039
4040 ptr = super_copy->sys_chunk_array + array_size;
4041 btrfs_cpu_key_to_disk(&disk_key, key);
4042 memcpy(ptr, &disk_key, sizeof(disk_key));
4043 ptr += sizeof(disk_key);
4044 memcpy(ptr, chunk, item_size);
4045 item_size += sizeof(disk_key);
4046 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4047 return 0;
4048}
4049
Miao Xieb2117a32011-01-05 10:07:28 +00004050/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004051 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004052 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004053static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004054{
Arne Jansen73c5de02011-04-12 12:07:57 +02004055 const struct btrfs_device_info *di_a = a;
4056 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004057
Arne Jansen73c5de02011-04-12 12:07:57 +02004058 if (di_a->max_avail > di_b->max_avail)
4059 return -1;
4060 if (di_a->max_avail < di_b->max_avail)
4061 return 1;
4062 if (di_a->total_avail > di_b->total_avail)
4063 return -1;
4064 if (di_a->total_avail < di_b->total_avail)
4065 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004066 return 0;
4067}
4068
Eric Sandeen48a3b632013-04-25 20:41:01 +00004069static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004070 [BTRFS_RAID_RAID10] = {
4071 .sub_stripes = 2,
4072 .dev_stripes = 1,
4073 .devs_max = 0, /* 0 == as many as possible */
4074 .devs_min = 4,
4075 .devs_increment = 2,
4076 .ncopies = 2,
4077 },
4078 [BTRFS_RAID_RAID1] = {
4079 .sub_stripes = 1,
4080 .dev_stripes = 1,
4081 .devs_max = 2,
4082 .devs_min = 2,
4083 .devs_increment = 2,
4084 .ncopies = 2,
4085 },
4086 [BTRFS_RAID_DUP] = {
4087 .sub_stripes = 1,
4088 .dev_stripes = 2,
4089 .devs_max = 1,
4090 .devs_min = 1,
4091 .devs_increment = 1,
4092 .ncopies = 2,
4093 },
4094 [BTRFS_RAID_RAID0] = {
4095 .sub_stripes = 1,
4096 .dev_stripes = 1,
4097 .devs_max = 0,
4098 .devs_min = 2,
4099 .devs_increment = 1,
4100 .ncopies = 1,
4101 },
4102 [BTRFS_RAID_SINGLE] = {
4103 .sub_stripes = 1,
4104 .dev_stripes = 1,
4105 .devs_max = 1,
4106 .devs_min = 1,
4107 .devs_increment = 1,
4108 .ncopies = 1,
4109 },
Chris Masone942f882013-02-20 14:06:05 -05004110 [BTRFS_RAID_RAID5] = {
4111 .sub_stripes = 1,
4112 .dev_stripes = 1,
4113 .devs_max = 0,
4114 .devs_min = 2,
4115 .devs_increment = 1,
4116 .ncopies = 2,
4117 },
4118 [BTRFS_RAID_RAID6] = {
4119 .sub_stripes = 1,
4120 .dev_stripes = 1,
4121 .devs_max = 0,
4122 .devs_min = 3,
4123 .devs_increment = 1,
4124 .ncopies = 3,
4125 },
Liu Bo31e50222012-11-21 14:18:10 +00004126};
4127
David Woodhouse53b381b2013-01-29 18:40:14 -05004128static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4129{
4130 /* TODO allow them to set a preferred stripe size */
4131 return 64 * 1024;
4132}
4133
4134static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4135{
David Woodhouse53b381b2013-01-29 18:40:14 -05004136 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
4137 return;
4138
Miao Xieceda0862013-04-11 10:30:16 +00004139 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004140}
4141
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004142#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4143 - sizeof(struct btrfs_item) \
4144 - sizeof(struct btrfs_chunk)) \
4145 / sizeof(struct btrfs_stripe) + 1)
4146
4147#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4148 - 2 * sizeof(struct btrfs_disk_key) \
4149 - 2 * sizeof(struct btrfs_chunk)) \
4150 / sizeof(struct btrfs_stripe) + 1)
4151
Miao Xieb2117a32011-01-05 10:07:28 +00004152static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004153 struct btrfs_root *extent_root, u64 start,
4154 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004155{
4156 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004157 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4158 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004159 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004160 struct extent_map_tree *em_tree;
4161 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004162 struct btrfs_device_info *devices_info = NULL;
4163 u64 total_avail;
4164 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004165 int data_stripes; /* number of stripes that count for
4166 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004167 int sub_stripes; /* sub_stripes info for map */
4168 int dev_stripes; /* stripes per dev */
4169 int devs_max; /* max devs to use */
4170 int devs_min; /* min devs needed */
4171 int devs_increment; /* ndevs has to be a multiple of this */
4172 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004173 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004174 u64 max_stripe_size;
4175 u64 max_chunk_size;
4176 u64 stripe_size;
4177 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004178 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004179 int ndevs;
4180 int i;
4181 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004182 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004183
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004184 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004185
Miao Xieb2117a32011-01-05 10:07:28 +00004186 if (list_empty(&fs_devices->alloc_list))
4187 return -ENOSPC;
4188
Liu Bo31e50222012-11-21 14:18:10 +00004189 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004190
Liu Bo31e50222012-11-21 14:18:10 +00004191 sub_stripes = btrfs_raid_array[index].sub_stripes;
4192 dev_stripes = btrfs_raid_array[index].dev_stripes;
4193 devs_max = btrfs_raid_array[index].devs_max;
4194 devs_min = btrfs_raid_array[index].devs_min;
4195 devs_increment = btrfs_raid_array[index].devs_increment;
4196 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004197
4198 if (type & BTRFS_BLOCK_GROUP_DATA) {
4199 max_stripe_size = 1024 * 1024 * 1024;
4200 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004201 if (!devs_max)
4202 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004203 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004204 /* for larger filesystems, use larger metadata chunks */
4205 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4206 max_stripe_size = 1024 * 1024 * 1024;
4207 else
4208 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004209 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004210 if (!devs_max)
4211 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004212 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004213 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004214 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004215 if (!devs_max)
4216 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004217 } else {
David Sterba351fd352014-05-15 16:48:20 +02004218 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004219 type);
4220 BUG_ON(1);
4221 }
4222
4223 /* we don't want a chunk larger than 10% of writeable space */
4224 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4225 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004226
4227 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4228 GFP_NOFS);
4229 if (!devices_info)
4230 return -ENOMEM;
4231
Arne Jansen73c5de02011-04-12 12:07:57 +02004232 cur = fs_devices->alloc_list.next;
4233
4234 /*
4235 * in the first pass through the devices list, we gather information
4236 * about the available holes on each device.
4237 */
4238 ndevs = 0;
4239 while (cur != &fs_devices->alloc_list) {
4240 struct btrfs_device *device;
4241 u64 max_avail;
4242 u64 dev_offset;
4243
4244 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4245
4246 cur = cur->next;
4247
4248 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004249 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004250 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004251 continue;
4252 }
4253
Stefan Behrens63a212a2012-11-05 18:29:28 +01004254 if (!device->in_fs_metadata ||
4255 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004256 continue;
4257
4258 if (device->total_bytes > device->bytes_used)
4259 total_avail = device->total_bytes - device->bytes_used;
4260 else
4261 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004262
4263 /* If there is no space on this device, skip it. */
4264 if (total_avail == 0)
4265 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004266
Josef Bacik6df9a952013-06-27 13:22:46 -04004267 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004268 max_stripe_size * dev_stripes,
4269 &dev_offset, &max_avail);
4270 if (ret && ret != -ENOSPC)
4271 goto error;
4272
4273 if (ret == 0)
4274 max_avail = max_stripe_size * dev_stripes;
4275
4276 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4277 continue;
4278
Eric Sandeen063d0062013-01-31 00:55:01 +00004279 if (ndevs == fs_devices->rw_devices) {
4280 WARN(1, "%s: found more than %llu devices\n",
4281 __func__, fs_devices->rw_devices);
4282 break;
4283 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004284 devices_info[ndevs].dev_offset = dev_offset;
4285 devices_info[ndevs].max_avail = max_avail;
4286 devices_info[ndevs].total_avail = total_avail;
4287 devices_info[ndevs].dev = device;
4288 ++ndevs;
4289 }
4290
4291 /*
4292 * now sort the devices by hole size / available space
4293 */
4294 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4295 btrfs_cmp_device_info, NULL);
4296
4297 /* round down to number of usable stripes */
4298 ndevs -= ndevs % devs_increment;
4299
4300 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4301 ret = -ENOSPC;
4302 goto error;
4303 }
4304
4305 if (devs_max && ndevs > devs_max)
4306 ndevs = devs_max;
4307 /*
4308 * the primary goal is to maximize the number of stripes, so use as many
4309 * devices as possible, even if the stripes are not maximum sized.
4310 */
4311 stripe_size = devices_info[ndevs-1].max_avail;
4312 num_stripes = ndevs * dev_stripes;
4313
David Woodhouse53b381b2013-01-29 18:40:14 -05004314 /*
4315 * this will have to be fixed for RAID1 and RAID10 over
4316 * more drives
4317 */
4318 data_stripes = num_stripes / ncopies;
4319
David Woodhouse53b381b2013-01-29 18:40:14 -05004320 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4321 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4322 btrfs_super_stripesize(info->super_copy));
4323 data_stripes = num_stripes - 1;
4324 }
4325 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4326 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4327 btrfs_super_stripesize(info->super_copy));
4328 data_stripes = num_stripes - 2;
4329 }
Chris Mason86db2572013-02-20 16:23:40 -05004330
4331 /*
4332 * Use the number of data stripes to figure out how big this chunk
4333 * is really going to be in terms of logical address space,
4334 * and compare that answer with the max chunk size
4335 */
4336 if (stripe_size * data_stripes > max_chunk_size) {
4337 u64 mask = (1ULL << 24) - 1;
4338 stripe_size = max_chunk_size;
4339 do_div(stripe_size, data_stripes);
4340
4341 /* bump the answer up to a 16MB boundary */
4342 stripe_size = (stripe_size + mask) & ~mask;
4343
4344 /* but don't go higher than the limits we found
4345 * while searching for free extents
4346 */
4347 if (stripe_size > devices_info[ndevs-1].max_avail)
4348 stripe_size = devices_info[ndevs-1].max_avail;
4349 }
4350
Arne Jansen73c5de02011-04-12 12:07:57 +02004351 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004352
4353 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004354 do_div(stripe_size, raid_stripe_len);
4355 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004356
Miao Xieb2117a32011-01-05 10:07:28 +00004357 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4358 if (!map) {
4359 ret = -ENOMEM;
4360 goto error;
4361 }
4362 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004363
Arne Jansen73c5de02011-04-12 12:07:57 +02004364 for (i = 0; i < ndevs; ++i) {
4365 for (j = 0; j < dev_stripes; ++j) {
4366 int s = i * dev_stripes + j;
4367 map->stripes[s].dev = devices_info[i].dev;
4368 map->stripes[s].physical = devices_info[i].dev_offset +
4369 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004370 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004371 }
Chris Mason593060d2008-03-25 16:50:33 -04004372 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004373 map->stripe_len = raid_stripe_len;
4374 map->io_align = raid_stripe_len;
4375 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004376 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004377 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004378
David Woodhouse53b381b2013-01-29 18:40:14 -05004379 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004380
Arne Jansen73c5de02011-04-12 12:07:57 +02004381 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004382
David Sterba172ddd62011-04-21 00:48:27 +02004383 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004384 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004385 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004386 ret = -ENOMEM;
4387 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004388 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004389 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004390 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004391 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004392 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004393 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004394 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004395 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004396
Chris Mason0b86a832008-03-24 15:01:56 -04004397 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004398 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004399 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004400 if (!ret) {
4401 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4402 atomic_inc(&em->refs);
4403 }
Chris Mason890871b2009-09-02 16:24:52 -04004404 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004405 if (ret) {
4406 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004407 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004408 }
Yan Zheng2b820322008-11-17 21:11:30 -05004409
Josef Bacik04487482013-01-29 15:03:37 -05004410 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4411 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4412 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004413 if (ret)
4414 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004415
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004416 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004417 check_raid56_incompat_flag(extent_root->fs_info, type);
4418
Miao Xieb2117a32011-01-05 10:07:28 +00004419 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004420 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004421
Josef Bacik6df9a952013-06-27 13:22:46 -04004422error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004423 write_lock(&em_tree->lock);
4424 remove_extent_mapping(em_tree, em);
4425 write_unlock(&em_tree->lock);
4426
4427 /* One for our allocation */
4428 free_extent_map(em);
4429 /* One for the tree reference */
4430 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004431error:
Miao Xieb2117a32011-01-05 10:07:28 +00004432 kfree(devices_info);
4433 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004434}
4435
Josef Bacik6df9a952013-06-27 13:22:46 -04004436int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004437 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004438 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004439{
Yan Zheng2b820322008-11-17 21:11:30 -05004440 struct btrfs_key key;
4441 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4442 struct btrfs_device *device;
4443 struct btrfs_chunk *chunk;
4444 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004445 struct extent_map_tree *em_tree;
4446 struct extent_map *em;
4447 struct map_lookup *map;
4448 size_t item_size;
4449 u64 dev_offset;
4450 u64 stripe_size;
4451 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004452 int ret;
4453
Josef Bacik6df9a952013-06-27 13:22:46 -04004454 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4455 read_lock(&em_tree->lock);
4456 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4457 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004458
Josef Bacik6df9a952013-06-27 13:22:46 -04004459 if (!em) {
4460 btrfs_crit(extent_root->fs_info, "unable to find logical "
4461 "%Lu len %Lu", chunk_offset, chunk_size);
4462 return -EINVAL;
4463 }
4464
4465 if (em->start != chunk_offset || em->len != chunk_size) {
4466 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004467 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004468 chunk_size, em->start, em->len);
4469 free_extent_map(em);
4470 return -EINVAL;
4471 }
4472
4473 map = (struct map_lookup *)em->bdev;
4474 item_size = btrfs_chunk_item_size(map->num_stripes);
4475 stripe_size = em->orig_block_len;
4476
4477 chunk = kzalloc(item_size, GFP_NOFS);
4478 if (!chunk) {
4479 ret = -ENOMEM;
4480 goto out;
4481 }
4482
4483 for (i = 0; i < map->num_stripes; i++) {
4484 device = map->stripes[i].dev;
4485 dev_offset = map->stripes[i].physical;
4486
Yan Zheng2b820322008-11-17 21:11:30 -05004487 device->bytes_used += stripe_size;
4488 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004489 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004490 goto out;
4491 ret = btrfs_alloc_dev_extent(trans, device,
4492 chunk_root->root_key.objectid,
4493 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4494 chunk_offset, dev_offset,
4495 stripe_size);
4496 if (ret)
4497 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004498 }
4499
Josef Bacik2bf64752011-09-26 17:12:22 -04004500 spin_lock(&extent_root->fs_info->free_chunk_lock);
4501 extent_root->fs_info->free_chunk_space -= (stripe_size *
4502 map->num_stripes);
4503 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4504
Yan Zheng2b820322008-11-17 21:11:30 -05004505 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004506 for (i = 0; i < map->num_stripes; i++) {
4507 device = map->stripes[i].dev;
4508 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004509
4510 btrfs_set_stack_stripe_devid(stripe, device->devid);
4511 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4512 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4513 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004514 }
4515
4516 btrfs_set_stack_chunk_length(chunk, chunk_size);
4517 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4518 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4519 btrfs_set_stack_chunk_type(chunk, map->type);
4520 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4521 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4522 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4523 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4524 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4525
4526 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4527 key.type = BTRFS_CHUNK_ITEM_KEY;
4528 key.offset = chunk_offset;
4529
4530 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004531 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4532 /*
4533 * TODO: Cleanup of inserted chunk root in case of
4534 * failure.
4535 */
Li Zefan125ccb02011-12-08 15:07:24 +08004536 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004537 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004538 }
liubo1abe9b82011-03-24 11:18:59 +00004539
Josef Bacik6df9a952013-06-27 13:22:46 -04004540out:
Yan Zheng2b820322008-11-17 21:11:30 -05004541 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004542 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004543 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004544}
4545
4546/*
4547 * Chunk allocation falls into two parts. The first part does works
4548 * that make the new allocated chunk useable, but not do any operation
4549 * that modifies the chunk tree. The second part does the works that
4550 * require modifying the chunk tree. This division is important for the
4551 * bootstrap process of adding storage to a seed btrfs.
4552 */
4553int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4554 struct btrfs_root *extent_root, u64 type)
4555{
4556 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004557
Josef Bacik6df9a952013-06-27 13:22:46 -04004558 chunk_offset = find_next_chunk(extent_root->fs_info);
4559 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004560}
4561
Chris Masond3977122009-01-05 21:25:51 -05004562static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004563 struct btrfs_root *root,
4564 struct btrfs_device *device)
4565{
4566 u64 chunk_offset;
4567 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004568 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004569 struct btrfs_fs_info *fs_info = root->fs_info;
4570 struct btrfs_root *extent_root = fs_info->extent_root;
4571 int ret;
4572
Josef Bacik6df9a952013-06-27 13:22:46 -04004573 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004574 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004575 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4576 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004577 if (ret)
4578 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004579
Josef Bacik6df9a952013-06-27 13:22:46 -04004580 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004581 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004582 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4583 alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004584 if (ret) {
4585 btrfs_abort_transaction(trans, root, ret);
4586 goto out;
4587 }
Yan Zheng2b820322008-11-17 21:11:30 -05004588
4589 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004590 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004591 btrfs_abort_transaction(trans, root, ret);
David Sterba005d6422012-09-18 07:52:32 -06004592out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004593 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004594}
4595
Miao Xied20983b2014-07-03 18:22:13 +08004596static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4597{
4598 int max_errors;
4599
4600 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4601 BTRFS_BLOCK_GROUP_RAID10 |
4602 BTRFS_BLOCK_GROUP_RAID5 |
4603 BTRFS_BLOCK_GROUP_DUP)) {
4604 max_errors = 1;
4605 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4606 max_errors = 2;
4607 } else {
4608 max_errors = 0;
4609 }
4610
4611 return max_errors;
4612}
4613
Yan Zheng2b820322008-11-17 21:11:30 -05004614int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4615{
4616 struct extent_map *em;
4617 struct map_lookup *map;
4618 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4619 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004620 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004621 int i;
4622
Chris Mason890871b2009-09-02 16:24:52 -04004623 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004624 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004625 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004626 if (!em)
4627 return 1;
4628
4629 map = (struct map_lookup *)em->bdev;
4630 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004631 if (map->stripes[i].dev->missing) {
4632 miss_ndevs++;
4633 continue;
4634 }
4635
Yan Zheng2b820322008-11-17 21:11:30 -05004636 if (!map->stripes[i].dev->writeable) {
4637 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004638 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004639 }
4640 }
Miao Xied20983b2014-07-03 18:22:13 +08004641
4642 /*
4643 * If the number of missing devices is larger than max errors,
4644 * we can not write the data into that chunk successfully, so
4645 * set it readonly.
4646 */
4647 if (miss_ndevs > btrfs_chunk_max_errors(map))
4648 readonly = 1;
4649end:
Yan Zheng2b820322008-11-17 21:11:30 -05004650 free_extent_map(em);
4651 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004652}
4653
4654void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4655{
David Sterbaa8067e02011-04-21 00:34:43 +02004656 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004657}
4658
4659void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4660{
4661 struct extent_map *em;
4662
Chris Masond3977122009-01-05 21:25:51 -05004663 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004664 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004665 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4666 if (em)
4667 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004668 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004669 if (!em)
4670 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004671 /* once for us */
4672 free_extent_map(em);
4673 /* once for the tree */
4674 free_extent_map(em);
4675 }
4676}
4677
Stefan Behrens5d964052012-11-05 14:59:07 +01004678int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004679{
Stefan Behrens5d964052012-11-05 14:59:07 +01004680 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004681 struct extent_map *em;
4682 struct map_lookup *map;
4683 struct extent_map_tree *em_tree = &map_tree->map_tree;
4684 int ret;
4685
Chris Mason890871b2009-09-02 16:24:52 -04004686 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004687 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004688 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004689
Josef Bacikfb7669b2013-04-23 10:53:18 -04004690 /*
4691 * We could return errors for these cases, but that could get ugly and
4692 * we'd probably do the same thing which is just not do anything else
4693 * and exit, so return 1 so the callers don't try to use other copies.
4694 */
4695 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004696 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004697 logical+len);
4698 return 1;
4699 }
4700
4701 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004702 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004703 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004704 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004705 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004706 return 1;
4707 }
4708
Chris Masonf1885912008-04-09 16:28:12 -04004709 map = (struct map_lookup *)em->bdev;
4710 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4711 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004712 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4713 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004714 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4715 ret = 2;
4716 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4717 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004718 else
4719 ret = 1;
4720 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004721
4722 btrfs_dev_replace_lock(&fs_info->dev_replace);
4723 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4724 ret++;
4725 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4726
Chris Masonf1885912008-04-09 16:28:12 -04004727 return ret;
4728}
4729
David Woodhouse53b381b2013-01-29 18:40:14 -05004730unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4731 struct btrfs_mapping_tree *map_tree,
4732 u64 logical)
4733{
4734 struct extent_map *em;
4735 struct map_lookup *map;
4736 struct extent_map_tree *em_tree = &map_tree->map_tree;
4737 unsigned long len = root->sectorsize;
4738
4739 read_lock(&em_tree->lock);
4740 em = lookup_extent_mapping(em_tree, logical, len);
4741 read_unlock(&em_tree->lock);
4742 BUG_ON(!em);
4743
4744 BUG_ON(em->start > logical || em->start + em->len < logical);
4745 map = (struct map_lookup *)em->bdev;
4746 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4747 BTRFS_BLOCK_GROUP_RAID6)) {
4748 len = map->stripe_len * nr_data_stripes(map);
4749 }
4750 free_extent_map(em);
4751 return len;
4752}
4753
4754int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4755 u64 logical, u64 len, int mirror_num)
4756{
4757 struct extent_map *em;
4758 struct map_lookup *map;
4759 struct extent_map_tree *em_tree = &map_tree->map_tree;
4760 int ret = 0;
4761
4762 read_lock(&em_tree->lock);
4763 em = lookup_extent_mapping(em_tree, logical, len);
4764 read_unlock(&em_tree->lock);
4765 BUG_ON(!em);
4766
4767 BUG_ON(em->start > logical || em->start + em->len < logical);
4768 map = (struct map_lookup *)em->bdev;
4769 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4770 BTRFS_BLOCK_GROUP_RAID6))
4771 ret = 1;
4772 free_extent_map(em);
4773 return ret;
4774}
4775
Stefan Behrens30d98612012-11-06 14:52:18 +01004776static int find_live_mirror(struct btrfs_fs_info *fs_info,
4777 struct map_lookup *map, int first, int num,
4778 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004779{
4780 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004781 int tolerance;
4782 struct btrfs_device *srcdev;
4783
4784 if (dev_replace_is_ongoing &&
4785 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4786 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4787 srcdev = fs_info->dev_replace.srcdev;
4788 else
4789 srcdev = NULL;
4790
4791 /*
4792 * try to avoid the drive that is the source drive for a
4793 * dev-replace procedure, only choose it if no other non-missing
4794 * mirror is available
4795 */
4796 for (tolerance = 0; tolerance < 2; tolerance++) {
4797 if (map->stripes[optimal].dev->bdev &&
4798 (tolerance || map->stripes[optimal].dev != srcdev))
4799 return optimal;
4800 for (i = first; i < first + num; i++) {
4801 if (map->stripes[i].dev->bdev &&
4802 (tolerance || map->stripes[i].dev != srcdev))
4803 return i;
4804 }
Chris Masondfe25022008-05-13 13:46:40 -04004805 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004806
Chris Masondfe25022008-05-13 13:46:40 -04004807 /* we couldn't find one that doesn't fail. Just return something
4808 * and the io error handling code will clean up eventually
4809 */
4810 return optimal;
4811}
4812
David Woodhouse53b381b2013-01-29 18:40:14 -05004813static inline int parity_smaller(u64 a, u64 b)
4814{
4815 return a > b;
4816}
4817
4818/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4819static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4820{
4821 struct btrfs_bio_stripe s;
4822 int i;
4823 u64 l;
4824 int again = 1;
4825
4826 while (again) {
4827 again = 0;
4828 for (i = 0; i < bbio->num_stripes - 1; i++) {
4829 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4830 s = bbio->stripes[i];
4831 l = raid_map[i];
4832 bbio->stripes[i] = bbio->stripes[i+1];
4833 raid_map[i] = raid_map[i+1];
4834 bbio->stripes[i+1] = s;
4835 raid_map[i+1] = l;
4836 again = 1;
4837 }
4838 }
4839 }
4840}
4841
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004842static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004843 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004844 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004845 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004846{
4847 struct extent_map *em;
4848 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004849 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004850 struct extent_map_tree *em_tree = &map_tree->map_tree;
4851 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004852 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004853 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004854 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004855 u64 stripe_nr_orig;
4856 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004857 u64 stripe_len;
4858 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004859 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004860 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004861 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004862 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004863 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004864 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004865 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4866 int dev_replace_is_ongoing = 0;
4867 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004868 int patch_the_first_stripe_for_dev_replace = 0;
4869 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004870 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004871
Chris Mason890871b2009-09-02 16:24:52 -04004872 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004873 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004874 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004875
Chris Mason3b951512008-04-17 11:29:12 -04004876 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004877 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004878 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004879 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004880 }
Chris Mason0b86a832008-03-24 15:01:56 -04004881
Josef Bacik9bb91872013-04-19 23:45:33 -04004882 if (em->start > logical || em->start + em->len < logical) {
4883 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004884 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004885 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004886 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004887 return -EINVAL;
4888 }
4889
Chris Mason0b86a832008-03-24 15:01:56 -04004890 map = (struct map_lookup *)em->bdev;
4891 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004892
David Woodhouse53b381b2013-01-29 18:40:14 -05004893 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004894 stripe_nr = offset;
4895 /*
4896 * stripe_nr counts the total number of stripes we have to stride
4897 * to get to this block
4898 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004899 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004900
David Woodhouse53b381b2013-01-29 18:40:14 -05004901 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004902 BUG_ON(offset < stripe_offset);
4903
4904 /* stripe_offset is the offset of this block in its stripe*/
4905 stripe_offset = offset - stripe_offset;
4906
David Woodhouse53b381b2013-01-29 18:40:14 -05004907 /* if we're here for raid56, we need to know the stripe aligned start */
4908 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4909 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4910 raid56_full_stripe_start = offset;
4911
4912 /* allow a write of a full stripe, but make sure we don't
4913 * allow straddling of stripes
4914 */
4915 do_div(raid56_full_stripe_start, full_stripe_len);
4916 raid56_full_stripe_start *= full_stripe_len;
4917 }
4918
4919 if (rw & REQ_DISCARD) {
4920 /* we don't discard raid56 yet */
4921 if (map->type &
4922 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4923 ret = -EOPNOTSUPP;
4924 goto out;
4925 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004926 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004927 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4928 u64 max_len;
4929 /* For writes to RAID[56], allow a full stripeset across all disks.
4930 For other RAID types and for RAID[56] reads, just allow a single
4931 stripe (on a single disk). */
4932 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4933 (rw & REQ_WRITE)) {
4934 max_len = stripe_len * nr_data_stripes(map) -
4935 (offset - raid56_full_stripe_start);
4936 } else {
4937 /* we limit the length of each bio to what fits in a stripe */
4938 max_len = stripe_len - stripe_offset;
4939 }
4940 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004941 } else {
4942 *length = em->len - offset;
4943 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004944
David Woodhouse53b381b2013-01-29 18:40:14 -05004945 /* This is for when we're called from btrfs_merge_bio_hook() and all
4946 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004947 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004948 goto out;
4949
Stefan Behrens472262f2012-11-06 14:43:46 +01004950 btrfs_dev_replace_lock(dev_replace);
4951 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4952 if (!dev_replace_is_ongoing)
4953 btrfs_dev_replace_unlock(dev_replace);
4954
Stefan Behrensad6d6202012-11-06 15:06:47 +01004955 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4956 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4957 dev_replace->tgtdev != NULL) {
4958 /*
4959 * in dev-replace case, for repair case (that's the only
4960 * case where the mirror is selected explicitly when
4961 * calling btrfs_map_block), blocks left of the left cursor
4962 * can also be read from the target drive.
4963 * For REQ_GET_READ_MIRRORS, the target drive is added as
4964 * the last one to the array of stripes. For READ, it also
4965 * needs to be supported using the same mirror number.
4966 * If the requested block is not left of the left cursor,
4967 * EIO is returned. This can happen because btrfs_num_copies()
4968 * returns one more in the dev-replace case.
4969 */
4970 u64 tmp_length = *length;
4971 struct btrfs_bio *tmp_bbio = NULL;
4972 int tmp_num_stripes;
4973 u64 srcdev_devid = dev_replace->srcdev->devid;
4974 int index_srcdev = 0;
4975 int found = 0;
4976 u64 physical_of_found = 0;
4977
4978 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004979 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004980 if (ret) {
4981 WARN_ON(tmp_bbio != NULL);
4982 goto out;
4983 }
4984
4985 tmp_num_stripes = tmp_bbio->num_stripes;
4986 if (mirror_num > tmp_num_stripes) {
4987 /*
4988 * REQ_GET_READ_MIRRORS does not contain this
4989 * mirror, that means that the requested area
4990 * is not left of the left cursor
4991 */
4992 ret = -EIO;
4993 kfree(tmp_bbio);
4994 goto out;
4995 }
4996
4997 /*
4998 * process the rest of the function using the mirror_num
4999 * of the source drive. Therefore look it up first.
5000 * At the end, patch the device pointer to the one of the
5001 * target drive.
5002 */
5003 for (i = 0; i < tmp_num_stripes; i++) {
5004 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5005 /*
5006 * In case of DUP, in order to keep it
5007 * simple, only add the mirror with the
5008 * lowest physical address
5009 */
5010 if (found &&
5011 physical_of_found <=
5012 tmp_bbio->stripes[i].physical)
5013 continue;
5014 index_srcdev = i;
5015 found = 1;
5016 physical_of_found =
5017 tmp_bbio->stripes[i].physical;
5018 }
5019 }
5020
5021 if (found) {
5022 mirror_num = index_srcdev + 1;
5023 patch_the_first_stripe_for_dev_replace = 1;
5024 physical_to_patch_in_first_stripe = physical_of_found;
5025 } else {
5026 WARN_ON(1);
5027 ret = -EIO;
5028 kfree(tmp_bbio);
5029 goto out;
5030 }
5031
5032 kfree(tmp_bbio);
5033 } else if (mirror_num > map->num_stripes) {
5034 mirror_num = 0;
5035 }
5036
Chris Masonf2d8d742008-04-21 10:03:05 -04005037 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005038 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005039 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005040 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005041 do_div(stripe_nr_end, map->stripe_len);
5042 stripe_end_offset = stripe_nr_end * map->stripe_len -
5043 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005044
Li Dongyangfce3bb92011-03-24 10:24:26 +00005045 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5046 if (rw & REQ_DISCARD)
5047 num_stripes = min_t(u64, map->num_stripes,
5048 stripe_nr_end - stripe_nr_orig);
5049 stripe_index = do_div(stripe_nr, map->num_stripes);
5050 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005051 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005052 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005053 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005054 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005055 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005056 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005057 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005058 current->pid % map->num_stripes,
5059 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005060 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005061 }
Chris Mason2fff7342008-04-29 14:12:09 -04005062
Chris Mason611f0e02008-04-03 16:29:03 -04005063 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005064 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005065 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005066 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005067 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005068 } else {
5069 mirror_num = 1;
5070 }
Chris Mason2fff7342008-04-29 14:12:09 -04005071
Chris Mason321aecc2008-04-16 10:49:51 -04005072 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5073 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005074
5075 stripe_index = do_div(stripe_nr, factor);
5076 stripe_index *= map->sub_stripes;
5077
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005078 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005079 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005080 else if (rw & REQ_DISCARD)
5081 num_stripes = min_t(u64, map->sub_stripes *
5082 (stripe_nr_end - stripe_nr_orig),
5083 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005084 else if (mirror_num)
5085 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005086 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005087 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005088 stripe_index = find_live_mirror(fs_info, map,
5089 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005090 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005091 current->pid % map->sub_stripes,
5092 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005093 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005094 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005095
5096 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5097 BTRFS_BLOCK_GROUP_RAID6)) {
5098 u64 tmp;
5099
5100 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
5101 && raid_map_ret) {
5102 int i, rot;
5103
5104 /* push stripe_nr back to the start of the full stripe */
5105 stripe_nr = raid56_full_stripe_start;
5106 do_div(stripe_nr, stripe_len);
5107
5108 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5109
5110 /* RAID[56] write or recovery. Return all stripes */
5111 num_stripes = map->num_stripes;
5112 max_errors = nr_parity_stripes(map);
5113
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05305114 raid_map = kmalloc_array(num_stripes, sizeof(u64),
David Woodhouse53b381b2013-01-29 18:40:14 -05005115 GFP_NOFS);
5116 if (!raid_map) {
5117 ret = -ENOMEM;
5118 goto out;
5119 }
5120
5121 /* Work out the disk rotation on this stripe-set */
5122 tmp = stripe_nr;
5123 rot = do_div(tmp, num_stripes);
5124
5125 /* Fill in the logical address of each stripe */
5126 tmp = stripe_nr * nr_data_stripes(map);
5127 for (i = 0; i < nr_data_stripes(map); i++)
5128 raid_map[(i+rot) % num_stripes] =
5129 em->start + (tmp + i) * map->stripe_len;
5130
5131 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5132 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5133 raid_map[(i+rot+1) % num_stripes] =
5134 RAID6_Q_STRIPE;
5135
5136 *length = map->stripe_len;
5137 stripe_index = 0;
5138 stripe_offset = 0;
5139 } else {
5140 /*
5141 * Mirror #0 or #1 means the original data block.
5142 * Mirror #2 is RAID5 parity block.
5143 * Mirror #3 is RAID6 Q block.
5144 */
5145 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5146 if (mirror_num > 1)
5147 stripe_index = nr_data_stripes(map) +
5148 mirror_num - 2;
5149
5150 /* We distribute the parity blocks across stripes */
5151 tmp = stripe_nr + stripe_index;
5152 stripe_index = do_div(tmp, map->num_stripes);
5153 }
Chris Mason8790d502008-04-03 16:29:03 -04005154 } else {
5155 /*
5156 * after this do_div call, stripe_nr is the number of stripes
5157 * on this device we have to walk to find the data, and
5158 * stripe_index is the number of our device in the stripe array
5159 */
5160 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005161 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005162 }
Chris Mason593060d2008-03-25 16:50:33 -04005163 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005164
Stefan Behrens472262f2012-11-06 14:43:46 +01005165 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005166 if (dev_replace_is_ongoing) {
5167 if (rw & (REQ_WRITE | REQ_DISCARD))
5168 num_alloc_stripes <<= 1;
5169 if (rw & REQ_GET_READ_MIRRORS)
5170 num_alloc_stripes++;
5171 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005172 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08005173 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04005174 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08005175 ret = -ENOMEM;
5176 goto out;
5177 }
5178 atomic_set(&bbio->error, 0);
5179
Li Dongyangfce3bb92011-03-24 10:24:26 +00005180 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08005181 int factor = 0;
5182 int sub_stripes = 0;
5183 u64 stripes_per_dev = 0;
5184 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005185 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005186
5187 if (map->type &
5188 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5189 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5190 sub_stripes = 1;
5191 else
5192 sub_stripes = map->sub_stripes;
5193
5194 factor = map->num_stripes / sub_stripes;
5195 stripes_per_dev = div_u64_rem(stripe_nr_end -
5196 stripe_nr_orig,
5197 factor,
5198 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005199 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5200 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005201 }
5202
Li Dongyangfce3bb92011-03-24 10:24:26 +00005203 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005204 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005205 map->stripes[stripe_index].physical +
5206 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005207 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005208
Li Zefanec9ef7a2011-12-01 14:06:42 +08005209 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5210 BTRFS_BLOCK_GROUP_RAID10)) {
5211 bbio->stripes[i].length = stripes_per_dev *
5212 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005213
Li Zefanec9ef7a2011-12-01 14:06:42 +08005214 if (i / sub_stripes < remaining_stripes)
5215 bbio->stripes[i].length +=
5216 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005217
5218 /*
5219 * Special for the first stripe and
5220 * the last stripe:
5221 *
5222 * |-------|...|-------|
5223 * |----------|
5224 * off end_off
5225 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005226 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005227 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005228 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005229
5230 if (stripe_index >= last_stripe &&
5231 stripe_index <= (last_stripe +
5232 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005233 bbio->stripes[i].length -=
5234 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005235
Li Zefanec9ef7a2011-12-01 14:06:42 +08005236 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005237 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005238 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005239 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005240
5241 stripe_index++;
5242 if (stripe_index == map->num_stripes) {
5243 /* This could only happen for RAID0/10 */
5244 stripe_index = 0;
5245 stripe_nr++;
5246 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005247 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005248 } else {
5249 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005250 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005251 map->stripes[stripe_index].physical +
5252 stripe_offset +
5253 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005254 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005255 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005256 stripe_index++;
5257 }
Chris Mason593060d2008-03-25 16:50:33 -04005258 }
Li Zefande11cc12011-12-01 12:55:47 +08005259
Miao Xied20983b2014-07-03 18:22:13 +08005260 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5261 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005262
Stefan Behrens472262f2012-11-06 14:43:46 +01005263 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5264 dev_replace->tgtdev != NULL) {
5265 int index_where_to_add;
5266 u64 srcdev_devid = dev_replace->srcdev->devid;
5267
5268 /*
5269 * duplicate the write operations while the dev replace
5270 * procedure is running. Since the copying of the old disk
5271 * to the new disk takes place at run time while the
5272 * filesystem is mounted writable, the regular write
5273 * operations to the old disk have to be duplicated to go
5274 * to the new disk as well.
5275 * Note that device->missing is handled by the caller, and
5276 * that the write to the old disk is already set up in the
5277 * stripes array.
5278 */
5279 index_where_to_add = num_stripes;
5280 for (i = 0; i < num_stripes; i++) {
5281 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5282 /* write to new disk, too */
5283 struct btrfs_bio_stripe *new =
5284 bbio->stripes + index_where_to_add;
5285 struct btrfs_bio_stripe *old =
5286 bbio->stripes + i;
5287
5288 new->physical = old->physical;
5289 new->length = old->length;
5290 new->dev = dev_replace->tgtdev;
5291 index_where_to_add++;
5292 max_errors++;
5293 }
5294 }
5295 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005296 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5297 dev_replace->tgtdev != NULL) {
5298 u64 srcdev_devid = dev_replace->srcdev->devid;
5299 int index_srcdev = 0;
5300 int found = 0;
5301 u64 physical_of_found = 0;
5302
5303 /*
5304 * During the dev-replace procedure, the target drive can
5305 * also be used to read data in case it is needed to repair
5306 * a corrupt block elsewhere. This is possible if the
5307 * requested area is left of the left cursor. In this area,
5308 * the target drive is a full copy of the source drive.
5309 */
5310 for (i = 0; i < num_stripes; i++) {
5311 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5312 /*
5313 * In case of DUP, in order to keep it
5314 * simple, only add the mirror with the
5315 * lowest physical address
5316 */
5317 if (found &&
5318 physical_of_found <=
5319 bbio->stripes[i].physical)
5320 continue;
5321 index_srcdev = i;
5322 found = 1;
5323 physical_of_found = bbio->stripes[i].physical;
5324 }
5325 }
5326 if (found) {
5327 u64 length = map->stripe_len;
5328
5329 if (physical_of_found + length <=
5330 dev_replace->cursor_left) {
5331 struct btrfs_bio_stripe *tgtdev_stripe =
5332 bbio->stripes + num_stripes;
5333
5334 tgtdev_stripe->physical = physical_of_found;
5335 tgtdev_stripe->length =
5336 bbio->stripes[index_srcdev].length;
5337 tgtdev_stripe->dev = dev_replace->tgtdev;
5338
5339 num_stripes++;
5340 }
5341 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005342 }
5343
Li Zefande11cc12011-12-01 12:55:47 +08005344 *bbio_ret = bbio;
5345 bbio->num_stripes = num_stripes;
5346 bbio->max_errors = max_errors;
5347 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005348
5349 /*
5350 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5351 * mirror_num == num_stripes + 1 && dev_replace target drive is
5352 * available as a mirror
5353 */
5354 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5355 WARN_ON(num_stripes > 1);
5356 bbio->stripes[0].dev = dev_replace->tgtdev;
5357 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5358 bbio->mirror_num = map->num_stripes + 1;
5359 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005360 if (raid_map) {
5361 sort_parity_stripes(bbio, raid_map);
5362 *raid_map_ret = raid_map;
5363 }
Chris Masoncea9e442008-04-09 16:28:12 -04005364out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005365 if (dev_replace_is_ongoing)
5366 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005367 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005368 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005369}
5370
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005371int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005372 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005373 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005374{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005375 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005376 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005377}
5378
Yan Zhenga512bbf2008-12-08 16:46:26 -05005379int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5380 u64 chunk_start, u64 physical, u64 devid,
5381 u64 **logical, int *naddrs, int *stripe_len)
5382{
5383 struct extent_map_tree *em_tree = &map_tree->map_tree;
5384 struct extent_map *em;
5385 struct map_lookup *map;
5386 u64 *buf;
5387 u64 bytenr;
5388 u64 length;
5389 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005390 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005391 int i, j, nr = 0;
5392
Chris Mason890871b2009-09-02 16:24:52 -04005393 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005394 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005395 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005396
Josef Bacik835d9742013-03-19 12:13:25 -04005397 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005398 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005399 chunk_start);
5400 return -EIO;
5401 }
5402
5403 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005404 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005405 em->start, chunk_start);
5406 free_extent_map(em);
5407 return -EIO;
5408 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005409 map = (struct map_lookup *)em->bdev;
5410
5411 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005412 rmap_len = map->stripe_len;
5413
Yan Zhenga512bbf2008-12-08 16:46:26 -05005414 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5415 do_div(length, map->num_stripes / map->sub_stripes);
5416 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5417 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005418 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5419 BTRFS_BLOCK_GROUP_RAID6)) {
5420 do_div(length, nr_data_stripes(map));
5421 rmap_len = map->stripe_len * nr_data_stripes(map);
5422 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005423
5424 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005425 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005426
5427 for (i = 0; i < map->num_stripes; i++) {
5428 if (devid && map->stripes[i].dev->devid != devid)
5429 continue;
5430 if (map->stripes[i].physical > physical ||
5431 map->stripes[i].physical + length <= physical)
5432 continue;
5433
5434 stripe_nr = physical - map->stripes[i].physical;
5435 do_div(stripe_nr, map->stripe_len);
5436
5437 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5438 stripe_nr = stripe_nr * map->num_stripes + i;
5439 do_div(stripe_nr, map->sub_stripes);
5440 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5441 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005442 } /* else if RAID[56], multiply by nr_data_stripes().
5443 * Alternatively, just use rmap_len below instead of
5444 * map->stripe_len */
5445
5446 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005447 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005448 for (j = 0; j < nr; j++) {
5449 if (buf[j] == bytenr)
5450 break;
5451 }
Chris Mason934d3752008-12-08 16:43:10 -05005452 if (j == nr) {
5453 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005454 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005455 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005456 }
5457
Yan Zhenga512bbf2008-12-08 16:46:26 -05005458 *logical = buf;
5459 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005460 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005461
5462 free_extent_map(em);
5463 return 0;
5464}
5465
Miao Xie8408c712014-06-19 10:42:55 +08005466static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5467{
5468 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5469 bio_endio_nodec(bio, err);
5470 else
5471 bio_endio(bio, err);
5472 kfree(bbio);
5473}
5474
Jan Schmidta1d3c472011-08-04 17:15:33 +02005475static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005476{
Chris Mason9be33952013-05-17 18:30:14 -04005477 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005478 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005479 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005480
Stefan Behrens442a4f62012-05-25 16:06:08 +02005481 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005482 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005483 if (err == -EIO || err == -EREMOTEIO) {
5484 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005485 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005486
5487 BUG_ON(stripe_index >= bbio->num_stripes);
5488 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005489 if (dev->bdev) {
5490 if (bio->bi_rw & WRITE)
5491 btrfs_dev_stat_inc(dev,
5492 BTRFS_DEV_STAT_WRITE_ERRS);
5493 else
5494 btrfs_dev_stat_inc(dev,
5495 BTRFS_DEV_STAT_READ_ERRS);
5496 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5497 btrfs_dev_stat_inc(dev,
5498 BTRFS_DEV_STAT_FLUSH_ERRS);
5499 btrfs_dev_stat_print_on_error(dev);
5500 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005501 }
5502 }
Chris Mason8790d502008-04-03 16:29:03 -04005503
Jan Schmidta1d3c472011-08-04 17:15:33 +02005504 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005505 is_orig_bio = 1;
5506
Miao Xiec404e0d2014-01-30 16:46:55 +08005507 btrfs_bio_counter_dec(bbio->fs_info);
5508
Jan Schmidta1d3c472011-08-04 17:15:33 +02005509 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005510 if (!is_orig_bio) {
5511 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005512 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005513 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005514
Jan Schmidta1d3c472011-08-04 17:15:33 +02005515 bio->bi_private = bbio->private;
5516 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005517 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005518 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005519 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005520 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005521 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005522 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005523 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005524 /*
5525 * this bio is actually up to date, we didn't
5526 * go over the max number of errors
5527 */
5528 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005529 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005530 }
Miao Xiec55f1392014-06-19 10:42:54 +08005531
Miao Xie8408c712014-06-19 10:42:55 +08005532 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005533 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005534 bio_put(bio);
5535 }
Chris Mason8790d502008-04-03 16:29:03 -04005536}
5537
Chris Mason8b712842008-06-11 16:50:36 -04005538/*
5539 * see run_scheduled_bios for a description of why bios are collected for
5540 * async submit.
5541 *
5542 * This will add one bio to the pending list for a device and make sure
5543 * the work struct is scheduled.
5544 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005545static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5546 struct btrfs_device *device,
5547 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005548{
5549 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005550 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005551
David Woodhouse53b381b2013-01-29 18:40:14 -05005552 if (device->missing || !device->bdev) {
5553 bio_endio(bio, -EIO);
5554 return;
5555 }
5556
Chris Mason8b712842008-06-11 16:50:36 -04005557 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005558 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005559 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005560 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005561 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005562 return;
Chris Mason8b712842008-06-11 16:50:36 -04005563 }
5564
5565 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005566 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005567 * higher layers. Otherwise, the async bio makes it appear we have
5568 * made progress against dirty pages when we've really just put it
5569 * on a queue for later
5570 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005571 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005572 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005573 bio->bi_next = NULL;
5574 bio->bi_rw |= rw;
5575
5576 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005577 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005578 pending_bios = &device->pending_sync_bios;
5579 else
5580 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005581
Chris Masonffbd5172009-04-20 15:50:09 -04005582 if (pending_bios->tail)
5583 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005584
Chris Masonffbd5172009-04-20 15:50:09 -04005585 pending_bios->tail = bio;
5586 if (!pending_bios->head)
5587 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005588 if (device->running_pending)
5589 should_queue = 0;
5590
5591 spin_unlock(&device->io_lock);
5592
5593 if (should_queue)
Qu Wenruoa8c93d42014-02-28 10:46:08 +08005594 btrfs_queue_work(root->fs_info->submit_workers,
5595 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005596}
5597
Josef Bacikde1ee922012-10-19 16:50:56 -04005598static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5599 sector_t sector)
5600{
5601 struct bio_vec *prev;
5602 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005603 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005604 struct bvec_merge_data bvm = {
5605 .bi_bdev = bdev,
5606 .bi_sector = sector,
5607 .bi_rw = bio->bi_rw,
5608 };
5609
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305610 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005611 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005612
5613 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005614 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005615 return 0;
5616
5617 if (!q->merge_bvec_fn)
5618 return 1;
5619
Kent Overstreet4f024f32013-10-11 15:44:27 -07005620 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005621 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5622 return 0;
5623 return 1;
5624}
5625
5626static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5627 struct bio *bio, u64 physical, int dev_nr,
5628 int rw, int async)
5629{
5630 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5631
5632 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005633 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005634 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005635 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005636#ifdef DEBUG
5637 {
5638 struct rcu_string *name;
5639
5640 rcu_read_lock();
5641 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005642 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005643 "(%s id %llu), size=%u\n", rw,
5644 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5645 name->str, dev->devid, bio->bi_size);
5646 rcu_read_unlock();
5647 }
5648#endif
5649 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005650
5651 btrfs_bio_counter_inc_noblocked(root->fs_info);
5652
Josef Bacikde1ee922012-10-19 16:50:56 -04005653 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005654 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005655 else
5656 btrfsic_submit_bio(rw, bio);
5657}
5658
5659static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5660 struct bio *first_bio, struct btrfs_device *dev,
5661 int dev_nr, int rw, int async)
5662{
5663 struct bio_vec *bvec = first_bio->bi_io_vec;
5664 struct bio *bio;
5665 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5666 u64 physical = bbio->stripes[dev_nr].physical;
5667
5668again:
5669 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5670 if (!bio)
5671 return -ENOMEM;
5672
5673 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5674 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5675 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005676 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005677
5678 atomic_inc(&bbio->stripes_pending);
5679 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5680 rw, async);
5681 physical += len;
5682 goto again;
5683 }
5684 bvec++;
5685 }
5686
5687 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5688 return 0;
5689}
5690
5691static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5692{
5693 atomic_inc(&bbio->error);
5694 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005695 /* Shoud be the original bio. */
5696 WARN_ON(bio != bbio->orig_bio);
5697
Josef Bacikde1ee922012-10-19 16:50:56 -04005698 bio->bi_private = bbio->private;
5699 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005700 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005701 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005702
5703 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005704 }
5705}
5706
Chris Masonf1885912008-04-09 16:28:12 -04005707int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005708 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005709{
Chris Mason0b86a832008-03-24 15:01:56 -04005710 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005711 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005712 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005713 u64 length = 0;
5714 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005715 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005716 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005717 int dev_nr = 0;
5718 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005719 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005720
Kent Overstreet4f024f32013-10-11 15:44:27 -07005721 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005722 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005723
Miao Xiec404e0d2014-01-30 16:46:55 +08005724 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005725 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5726 mirror_num, &raid_map);
Miao Xiec404e0d2014-01-30 16:46:55 +08005727 if (ret) {
5728 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005729 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005730 }
Chris Masoncea9e442008-04-09 16:28:12 -04005731
Jan Schmidta1d3c472011-08-04 17:15:33 +02005732 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005733 bbio->orig_bio = first_bio;
5734 bbio->private = first_bio->bi_private;
5735 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005736 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005737 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5738
5739 if (raid_map) {
5740 /* In this case, map_length has been set to the length of
5741 a single stripe; not the whole write */
5742 if (rw & WRITE) {
Miao Xiec404e0d2014-01-30 16:46:55 +08005743 ret = raid56_parity_write(root, bio, bbio,
5744 raid_map, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005745 } else {
Miao Xiec404e0d2014-01-30 16:46:55 +08005746 ret = raid56_parity_recover(root, bio, bbio,
5747 raid_map, map_length,
5748 mirror_num);
David Woodhouse53b381b2013-01-29 18:40:14 -05005749 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005750 /*
5751 * FIXME, replace dosen't support raid56 yet, please fix
5752 * it in the future.
5753 */
5754 btrfs_bio_counter_dec(root->fs_info);
5755 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005756 }
5757
Chris Masoncea9e442008-04-09 16:28:12 -04005758 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005759 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005760 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005761 BUG();
5762 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005763
Chris Masond3977122009-01-05 21:25:51 -05005764 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005765 dev = bbio->stripes[dev_nr].dev;
5766 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5767 bbio_error(bbio, first_bio, logical);
5768 dev_nr++;
5769 continue;
5770 }
5771
5772 /*
5773 * Check and see if we're ok with this bio based on it's size
5774 * and offset with the given device.
5775 */
5776 if (!bio_size_ok(dev->bdev, first_bio,
5777 bbio->stripes[dev_nr].physical >> 9)) {
5778 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5779 dev_nr, rw, async_submit);
5780 BUG_ON(ret);
5781 dev_nr++;
5782 continue;
5783 }
5784
Jan Schmidta1d3c472011-08-04 17:15:33 +02005785 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005786 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005787 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005788 } else {
5789 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005790 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005791 }
Josef Bacik606686e2012-06-04 14:03:51 -04005792
Josef Bacikde1ee922012-10-19 16:50:56 -04005793 submit_stripe_bio(root, bbio, bio,
5794 bbio->stripes[dev_nr].physical, dev_nr, rw,
5795 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005796 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005797 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005798 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005799 return 0;
5800}
5801
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005802struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005803 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005804{
Yan Zheng2b820322008-11-17 21:11:30 -05005805 struct btrfs_device *device;
5806 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005807
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005808 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005809 while (cur_devices) {
5810 if (!fsid ||
5811 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5812 device = __find_device(&cur_devices->devices,
5813 devid, uuid);
5814 if (device)
5815 return device;
5816 }
5817 cur_devices = cur_devices->seed;
5818 }
5819 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005820}
5821
Chris Masondfe25022008-05-13 13:46:40 -04005822static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5823 u64 devid, u8 *dev_uuid)
5824{
5825 struct btrfs_device *device;
5826 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5827
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005828 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5829 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005830 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005831
5832 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005833 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005834 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005835
5836 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005837 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005838
Chris Masondfe25022008-05-13 13:46:40 -04005839 return device;
5840}
5841
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005842/**
5843 * btrfs_alloc_device - allocate struct btrfs_device
5844 * @fs_info: used only for generating a new devid, can be NULL if
5845 * devid is provided (i.e. @devid != NULL).
5846 * @devid: a pointer to devid for this device. If NULL a new devid
5847 * is generated.
5848 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5849 * is generated.
5850 *
5851 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5852 * on error. Returned struct is not linked onto any lists and can be
5853 * destroyed with kfree() right away.
5854 */
5855struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5856 const u64 *devid,
5857 const u8 *uuid)
5858{
5859 struct btrfs_device *dev;
5860 u64 tmp;
5861
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305862 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005863 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005864
5865 dev = __alloc_device();
5866 if (IS_ERR(dev))
5867 return dev;
5868
5869 if (devid)
5870 tmp = *devid;
5871 else {
5872 int ret;
5873
5874 ret = find_next_devid(fs_info, &tmp);
5875 if (ret) {
5876 kfree(dev);
5877 return ERR_PTR(ret);
5878 }
5879 }
5880 dev->devid = tmp;
5881
5882 if (uuid)
5883 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5884 else
5885 generate_random_uuid(dev->uuid);
5886
Liu Bo9e0af232014-08-15 23:36:53 +08005887 btrfs_init_work(&dev->work, btrfs_submit_helper,
5888 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005889
5890 return dev;
5891}
5892
Chris Mason0b86a832008-03-24 15:01:56 -04005893static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5894 struct extent_buffer *leaf,
5895 struct btrfs_chunk *chunk)
5896{
5897 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5898 struct map_lookup *map;
5899 struct extent_map *em;
5900 u64 logical;
5901 u64 length;
5902 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005903 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005904 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005905 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005906 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005907
Chris Masone17cade2008-04-15 15:41:47 -04005908 logical = key->offset;
5909 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005910
Chris Mason890871b2009-09-02 16:24:52 -04005911 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005912 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005913 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005914
5915 /* already mapped? */
5916 if (em && em->start <= logical && em->start + em->len > logical) {
5917 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005918 return 0;
5919 } else if (em) {
5920 free_extent_map(em);
5921 }
Chris Mason0b86a832008-03-24 15:01:56 -04005922
David Sterba172ddd62011-04-21 00:48:27 +02005923 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005924 if (!em)
5925 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005926 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5927 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005928 if (!map) {
5929 free_extent_map(em);
5930 return -ENOMEM;
5931 }
5932
Wang Shilong298a8f92014-06-19 10:42:52 +08005933 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04005934 em->bdev = (struct block_device *)map;
5935 em->start = logical;
5936 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005937 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005938 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005939 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005940
Chris Mason593060d2008-03-25 16:50:33 -04005941 map->num_stripes = num_stripes;
5942 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5943 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5944 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5945 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5946 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005947 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005948 for (i = 0; i < num_stripes; i++) {
5949 map->stripes[i].physical =
5950 btrfs_stripe_offset_nr(leaf, chunk, i);
5951 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005952 read_extent_buffer(leaf, uuid, (unsigned long)
5953 btrfs_stripe_dev_uuid_nr(chunk, i),
5954 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005955 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5956 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005957 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005958 free_extent_map(em);
5959 return -EIO;
5960 }
Chris Masondfe25022008-05-13 13:46:40 -04005961 if (!map->stripes[i].dev) {
5962 map->stripes[i].dev =
5963 add_missing_dev(root, devid, uuid);
5964 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04005965 free_extent_map(em);
5966 return -EIO;
5967 }
5968 }
5969 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005970 }
5971
Chris Mason890871b2009-09-02 16:24:52 -04005972 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005973 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005974 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005975 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005976 free_extent_map(em);
5977
5978 return 0;
5979}
5980
Jeff Mahoney143bede2012-03-01 14:56:26 +01005981static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005982 struct btrfs_dev_item *dev_item,
5983 struct btrfs_device *device)
5984{
5985 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005986
5987 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005988 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5989 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005990 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5991 device->type = btrfs_device_type(leaf, dev_item);
5992 device->io_align = btrfs_device_io_align(leaf, dev_item);
5993 device->io_width = btrfs_device_io_width(leaf, dev_item);
5994 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005995 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005996 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005997
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02005998 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005999 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006000}
6001
Yan Zheng2b820322008-11-17 21:11:30 -05006002static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
6003{
6004 struct btrfs_fs_devices *fs_devices;
6005 int ret;
6006
Li Zefanb367e472011-12-07 11:38:24 +08006007 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006008
6009 fs_devices = root->fs_info->fs_devices->seed;
6010 while (fs_devices) {
6011 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6012 ret = 0;
6013 goto out;
6014 }
6015 fs_devices = fs_devices->seed;
6016 }
6017
6018 fs_devices = find_fsid(fsid);
6019 if (!fs_devices) {
6020 ret = -ENOENT;
6021 goto out;
6022 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006023
6024 fs_devices = clone_fs_devices(fs_devices);
6025 if (IS_ERR(fs_devices)) {
6026 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006027 goto out;
6028 }
6029
Christoph Hellwig97288f22008-12-02 06:36:09 -05006030 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006031 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006032 if (ret) {
6033 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006034 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006035 }
Yan Zheng2b820322008-11-17 21:11:30 -05006036
6037 if (!fs_devices->seeding) {
6038 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006039 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006040 ret = -EINVAL;
6041 goto out;
6042 }
6043
6044 fs_devices->seed = root->fs_info->fs_devices->seed;
6045 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006046out:
Yan Zheng2b820322008-11-17 21:11:30 -05006047 return ret;
6048}
6049
Chris Mason0d81ba52008-03-24 15:02:07 -04006050static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006051 struct extent_buffer *leaf,
6052 struct btrfs_dev_item *dev_item)
6053{
6054 struct btrfs_device *device;
6055 u64 devid;
6056 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006057 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006058 u8 dev_uuid[BTRFS_UUID_SIZE];
6059
Chris Mason0b86a832008-03-24 15:01:56 -04006060 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006061 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006062 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006063 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006064 BTRFS_UUID_SIZE);
6065
6066 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6067 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05006068 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006069 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006070 }
6071
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006072 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006073 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006074 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006075 return -EIO;
6076
6077 if (!device) {
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006078 btrfs_warn(root->fs_info, "devid %llu missing", devid);
Yan Zheng2b820322008-11-17 21:11:30 -05006079 device = add_missing_dev(root, devid, dev_uuid);
6080 if (!device)
6081 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05006082 } else if (!device->missing) {
6083 /*
6084 * this happens when a device that was properly setup
6085 * in the device info lists suddenly goes bad.
6086 * device->bdev is NULL, and so we have to set
6087 * device->missing to one here
6088 */
6089 root->fs_info->fs_devices->missing_devices++;
6090 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006091 }
6092 }
6093
6094 if (device->fs_devices != root->fs_info->fs_devices) {
6095 BUG_ON(device->writeable);
6096 if (device->generation !=
6097 btrfs_device_generation(leaf, dev_item))
6098 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006099 }
Chris Mason0b86a832008-03-24 15:01:56 -04006100
6101 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006102 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006103 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006104 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006105 spin_lock(&root->fs_info->free_chunk_lock);
6106 root->fs_info->free_chunk_space += device->total_bytes -
6107 device->bytes_used;
6108 spin_unlock(&root->fs_info->free_chunk_lock);
6109 }
Chris Mason0b86a832008-03-24 15:01:56 -04006110 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006111 return ret;
6112}
6113
Yan Zhenge4404d62008-12-12 10:03:26 -05006114int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006115{
David Sterba6c417612011-04-13 15:41:04 +02006116 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006117 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006118 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006119 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04006120 u8 *ptr;
6121 unsigned long sb_ptr;
6122 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006123 u32 num_stripes;
6124 u32 array_size;
6125 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006126 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04006127 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006128
Yan Zhenge4404d62008-12-12 10:03:26 -05006129 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04006130 BTRFS_SUPER_INFO_SIZE);
6131 if (!sb)
6132 return -ENOMEM;
6133 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006134 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006135 /*
6136 * The sb extent buffer is artifical and just used to read the system array.
6137 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6138 * pages up-to-date when the page is larger: extent does not cover the
6139 * whole page and consequently check_page_uptodate does not find all
6140 * the page's extents up-to-date (the hole beyond sb),
6141 * write_extent_buffer then triggers a WARN_ON.
6142 *
6143 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6144 * but sb spans only this function. Add an explicit SetPageUptodate call
6145 * to silence the warning eg. on PowerPC 64.
6146 */
6147 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006148 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006149
Chris Masona061fc82008-05-07 11:43:44 -04006150 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006151 array_size = btrfs_super_sys_array_size(super_copy);
6152
Chris Mason0b86a832008-03-24 15:01:56 -04006153 ptr = super_copy->sys_chunk_array;
6154 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
6155 cur = 0;
6156
6157 while (cur < array_size) {
6158 disk_key = (struct btrfs_disk_key *)ptr;
6159 btrfs_disk_key_to_cpu(&key, disk_key);
6160
Chris Masona061fc82008-05-07 11:43:44 -04006161 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006162 sb_ptr += len;
6163 cur += len;
6164
Chris Mason0d81ba52008-03-24 15:02:07 -04006165 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04006166 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04006167 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006168 if (ret)
6169 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006170 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6171 len = btrfs_chunk_item_size(num_stripes);
6172 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006173 ret = -EIO;
6174 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006175 }
6176 ptr += len;
6177 sb_ptr += len;
6178 cur += len;
6179 }
Chris Masona061fc82008-05-07 11:43:44 -04006180 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006181 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006182}
6183
6184int btrfs_read_chunk_tree(struct btrfs_root *root)
6185{
6186 struct btrfs_path *path;
6187 struct extent_buffer *leaf;
6188 struct btrfs_key key;
6189 struct btrfs_key found_key;
6190 int ret;
6191 int slot;
6192
6193 root = root->fs_info->chunk_root;
6194
6195 path = btrfs_alloc_path();
6196 if (!path)
6197 return -ENOMEM;
6198
Li Zefanb367e472011-12-07 11:38:24 +08006199 mutex_lock(&uuid_mutex);
6200 lock_chunks(root);
6201
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006202 /*
6203 * Read all device items, and then all the chunk items. All
6204 * device items are found before any chunk item (their object id
6205 * is smaller than the lowest possible object id for a chunk
6206 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006207 */
6208 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6209 key.offset = 0;
6210 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006211 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006212 if (ret < 0)
6213 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006214 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006215 leaf = path->nodes[0];
6216 slot = path->slots[0];
6217 if (slot >= btrfs_header_nritems(leaf)) {
6218 ret = btrfs_next_leaf(root, path);
6219 if (ret == 0)
6220 continue;
6221 if (ret < 0)
6222 goto error;
6223 break;
6224 }
6225 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006226 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6227 struct btrfs_dev_item *dev_item;
6228 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006229 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006230 ret = read_one_dev(root, leaf, dev_item);
6231 if (ret)
6232 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006233 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6234 struct btrfs_chunk *chunk;
6235 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6236 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006237 if (ret)
6238 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006239 }
6240 path->slots[0]++;
6241 }
Chris Mason0b86a832008-03-24 15:01:56 -04006242 ret = 0;
6243error:
Li Zefanb367e472011-12-07 11:38:24 +08006244 unlock_chunks(root);
6245 mutex_unlock(&uuid_mutex);
6246
Yan Zheng2b820322008-11-17 21:11:30 -05006247 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006248 return ret;
6249}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006250
Miao Xiecb517ea2013-05-15 07:48:19 +00006251void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6252{
6253 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6254 struct btrfs_device *device;
6255
Liu Bo29cc83f2014-05-11 23:14:59 +08006256 while (fs_devices) {
6257 mutex_lock(&fs_devices->device_list_mutex);
6258 list_for_each_entry(device, &fs_devices->devices, dev_list)
6259 device->dev_root = fs_info->dev_root;
6260 mutex_unlock(&fs_devices->device_list_mutex);
6261
6262 fs_devices = fs_devices->seed;
6263 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006264}
6265
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006266static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6267{
6268 int i;
6269
6270 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6271 btrfs_dev_stat_reset(dev, i);
6272}
6273
6274int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6275{
6276 struct btrfs_key key;
6277 struct btrfs_key found_key;
6278 struct btrfs_root *dev_root = fs_info->dev_root;
6279 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6280 struct extent_buffer *eb;
6281 int slot;
6282 int ret = 0;
6283 struct btrfs_device *device;
6284 struct btrfs_path *path = NULL;
6285 int i;
6286
6287 path = btrfs_alloc_path();
6288 if (!path) {
6289 ret = -ENOMEM;
6290 goto out;
6291 }
6292
6293 mutex_lock(&fs_devices->device_list_mutex);
6294 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6295 int item_size;
6296 struct btrfs_dev_stats_item *ptr;
6297
6298 key.objectid = 0;
6299 key.type = BTRFS_DEV_STATS_KEY;
6300 key.offset = device->devid;
6301 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6302 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006303 __btrfs_reset_dev_stats(device);
6304 device->dev_stats_valid = 1;
6305 btrfs_release_path(path);
6306 continue;
6307 }
6308 slot = path->slots[0];
6309 eb = path->nodes[0];
6310 btrfs_item_key_to_cpu(eb, &found_key, slot);
6311 item_size = btrfs_item_size_nr(eb, slot);
6312
6313 ptr = btrfs_item_ptr(eb, slot,
6314 struct btrfs_dev_stats_item);
6315
6316 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6317 if (item_size >= (1 + i) * sizeof(__le64))
6318 btrfs_dev_stat_set(device, i,
6319 btrfs_dev_stats_value(eb, ptr, i));
6320 else
6321 btrfs_dev_stat_reset(device, i);
6322 }
6323
6324 device->dev_stats_valid = 1;
6325 btrfs_dev_stat_print_on_load(device);
6326 btrfs_release_path(path);
6327 }
6328 mutex_unlock(&fs_devices->device_list_mutex);
6329
6330out:
6331 btrfs_free_path(path);
6332 return ret < 0 ? ret : 0;
6333}
6334
6335static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6336 struct btrfs_root *dev_root,
6337 struct btrfs_device *device)
6338{
6339 struct btrfs_path *path;
6340 struct btrfs_key key;
6341 struct extent_buffer *eb;
6342 struct btrfs_dev_stats_item *ptr;
6343 int ret;
6344 int i;
6345
6346 key.objectid = 0;
6347 key.type = BTRFS_DEV_STATS_KEY;
6348 key.offset = device->devid;
6349
6350 path = btrfs_alloc_path();
6351 BUG_ON(!path);
6352 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6353 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006354 printk_in_rcu(KERN_WARNING "BTRFS: "
6355 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006356 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006357 goto out;
6358 }
6359
6360 if (ret == 0 &&
6361 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6362 /* need to delete old one and insert a new one */
6363 ret = btrfs_del_item(trans, dev_root, path);
6364 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006365 printk_in_rcu(KERN_WARNING "BTRFS: "
6366 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006367 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006368 goto out;
6369 }
6370 ret = 1;
6371 }
6372
6373 if (ret == 1) {
6374 /* need to insert a new item */
6375 btrfs_release_path(path);
6376 ret = btrfs_insert_empty_item(trans, dev_root, path,
6377 &key, sizeof(*ptr));
6378 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006379 printk_in_rcu(KERN_WARNING "BTRFS: "
6380 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006381 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006382 goto out;
6383 }
6384 }
6385
6386 eb = path->nodes[0];
6387 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6388 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6389 btrfs_set_dev_stats_value(eb, ptr, i,
6390 btrfs_dev_stat_read(device, i));
6391 btrfs_mark_buffer_dirty(eb);
6392
6393out:
6394 btrfs_free_path(path);
6395 return ret;
6396}
6397
6398/*
6399 * called from commit_transaction. Writes all changed device stats to disk.
6400 */
6401int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6402 struct btrfs_fs_info *fs_info)
6403{
6404 struct btrfs_root *dev_root = fs_info->dev_root;
6405 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6406 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006407 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006408 int ret = 0;
6409
6410 mutex_lock(&fs_devices->device_list_mutex);
6411 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006412 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006413 continue;
6414
Miao Xieaddc3fa2014-07-24 11:37:11 +08006415 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006416 ret = update_dev_stat_item(trans, dev_root, device);
6417 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006418 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006419 }
6420 mutex_unlock(&fs_devices->device_list_mutex);
6421
6422 return ret;
6423}
6424
Stefan Behrens442a4f62012-05-25 16:06:08 +02006425void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6426{
6427 btrfs_dev_stat_inc(dev, index);
6428 btrfs_dev_stat_print_on_error(dev);
6429}
6430
Eric Sandeen48a3b632013-04-25 20:41:01 +00006431static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006432{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006433 if (!dev->dev_stats_valid)
6434 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006435 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6436 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006437 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006438 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6439 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6440 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006441 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6442 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006443}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006444
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006445static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6446{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006447 int i;
6448
6449 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6450 if (btrfs_dev_stat_read(dev, i) != 0)
6451 break;
6452 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6453 return; /* all values == 0, suppress message */
6454
Frank Holtonefe120a2013-12-20 11:37:06 -05006455 printk_in_rcu(KERN_INFO "BTRFS: "
6456 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006457 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006458 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6459 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6460 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6461 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6462 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6463}
6464
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006465int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006466 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006467{
6468 struct btrfs_device *dev;
6469 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6470 int i;
6471
6472 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006473 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006474 mutex_unlock(&fs_devices->device_list_mutex);
6475
6476 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006477 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006478 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006479 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006480 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006481 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006482 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006483 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6484 if (stats->nr_items > i)
6485 stats->values[i] =
6486 btrfs_dev_stat_read_and_reset(dev, i);
6487 else
6488 btrfs_dev_stat_reset(dev, i);
6489 }
6490 } else {
6491 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6492 if (stats->nr_items > i)
6493 stats->values[i] = btrfs_dev_stat_read(dev, i);
6494 }
6495 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6496 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6497 return 0;
6498}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006499
6500int btrfs_scratch_superblock(struct btrfs_device *device)
6501{
6502 struct buffer_head *bh;
6503 struct btrfs_super_block *disk_super;
6504
6505 bh = btrfs_read_dev_super(device->bdev);
6506 if (!bh)
6507 return -EINVAL;
6508 disk_super = (struct btrfs_super_block *)bh->b_data;
6509
6510 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6511 set_buffer_dirty(bh);
6512 sync_dirty_buffer(bh);
6513 brelse(bh);
6514
6515 return 0;
6516}