blob: 483fc6d45299c7074c1b9734eebcc07e6303f0d6 [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 Bacik726551e2013-08-26 13:45:53 -0400723 if (device->missing)
724 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000725
Ilya Dryomova1e87802013-08-12 14:33:04 +0300726 new_device = btrfs_alloc_device(NULL, &device->devid,
727 device->uuid);
728 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400729
730 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400731 if (device->name) {
732 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300733 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400734 rcu_assign_pointer(new_device->name, name);
735 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300736
Xiao Guangrong1f781602011-04-20 10:09:16 +0000737 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300738 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000739
740 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400741 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000742 mutex_unlock(&fs_devices->device_list_mutex);
743
Yan Zhenge4404d62008-12-12 10:03:26 -0500744 WARN_ON(fs_devices->open_devices);
745 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500746 fs_devices->opened = 0;
747 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500748
Chris Mason8a4b83c2008-03-24 15:02:07 -0400749 return 0;
750}
751
Yan Zheng2b820322008-11-17 21:11:30 -0500752int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
753{
Yan Zhenge4404d62008-12-12 10:03:26 -0500754 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500755 int ret;
756
757 mutex_lock(&uuid_mutex);
758 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500759 if (!fs_devices->opened) {
760 seed_devices = fs_devices->seed;
761 fs_devices->seed = NULL;
762 }
Yan Zheng2b820322008-11-17 21:11:30 -0500763 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500764
765 while (seed_devices) {
766 fs_devices = seed_devices;
767 seed_devices = fs_devices->seed;
768 __btrfs_close_devices(fs_devices);
769 free_fs_devices(fs_devices);
770 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000771 /*
772 * Wait for rcu kworkers under __btrfs_close_devices
773 * to finish all blkdev_puts so device is really
774 * free when umount is done.
775 */
776 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500777 return ret;
778}
779
Yan Zhenge4404d62008-12-12 10:03:26 -0500780static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
781 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400782{
Josef Bacikd5e20032011-08-04 14:52:27 +0000783 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400784 struct block_device *bdev;
785 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400786 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800787 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400788 struct buffer_head *bh;
789 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400790 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500791 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400792 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400793
Tejun Heod4d77622010-11-13 11:55:18 +0100794 flags |= FMODE_EXCL;
795
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500796 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400797 if (device->bdev)
798 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400799 if (!device->name)
800 continue;
801
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000802 /* Just open everything we can; ignore failures here */
803 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
804 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100805 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400806
807 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000808 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400809 if (devid != device->devid)
810 goto error_brelse;
811
Yan Zheng2b820322008-11-17 21:11:30 -0500812 if (memcmp(device->uuid, disk_super->dev_item.uuid,
813 BTRFS_UUID_SIZE))
814 goto error_brelse;
815
816 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800817 if (!latest_dev ||
818 device->generation > latest_dev->generation)
819 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400820
Yan Zheng2b820322008-11-17 21:11:30 -0500821 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
822 device->writeable = 0;
823 } else {
824 device->writeable = !bdev_read_only(bdev);
825 seeding = 0;
826 }
827
Josef Bacikd5e20032011-08-04 14:52:27 +0000828 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +0800829 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +0000830 device->can_discard = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +0000831
Chris Mason8a4b83c2008-03-24 15:02:07 -0400832 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400833 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500834 device->mode = flags;
835
Chris Masonc2898112009-06-10 09:51:32 -0400836 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
837 fs_devices->rotating = 1;
838
Chris Masona0af4692008-05-13 16:03:06 -0400839 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300840 if (device->writeable &&
841 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500842 fs_devices->rw_devices++;
843 list_add(&device->dev_alloc_list,
844 &fs_devices->alloc_list);
845 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000846 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400847 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400848
Chris Masona0af4692008-05-13 16:03:06 -0400849error_brelse:
850 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100851 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400852 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400853 }
Chris Masona0af4692008-05-13 16:03:06 -0400854 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300855 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400856 goto out;
857 }
Yan Zheng2b820322008-11-17 21:11:30 -0500858 fs_devices->seeding = seeding;
859 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800860 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500861 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400862out:
Yan Zheng2b820322008-11-17 21:11:30 -0500863 return ret;
864}
865
866int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500867 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500868{
869 int ret;
870
871 mutex_lock(&uuid_mutex);
872 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500873 fs_devices->opened++;
874 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500875 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500876 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500877 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400878 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400879 return ret;
880}
881
David Sterba6f60cbd2013-02-15 11:31:02 -0700882/*
883 * Look for a btrfs signature on a device. This may be called out of the mount path
884 * and we are not allowed to call set_blocksize during the scan. The superblock
885 * is read via pagecache
886 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500887int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400888 struct btrfs_fs_devices **fs_devices_ret)
889{
890 struct btrfs_super_block *disk_super;
891 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700892 struct page *page;
893 void *p;
894 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400895 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400896 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400897 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700898 u64 bytenr;
899 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400900
David Sterba6f60cbd2013-02-15 11:31:02 -0700901 /*
902 * we would like to check all the supers, but that would make
903 * a btrfs mount succeed after a mkfs from a different FS.
904 * So, we need to add a special mount option to scan for
905 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
906 */
907 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100908 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500909 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700910
911 bdev = blkdev_get_by_path(path, flags, holder);
912
913 if (IS_ERR(bdev)) {
914 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100915 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700916 }
917
918 /* make sure our super fits in the device */
919 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
920 goto error_bdev_put;
921
922 /* make sure our super fits in the page */
923 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
924 goto error_bdev_put;
925
926 /* make sure our super doesn't straddle pages on disk */
927 index = bytenr >> PAGE_CACHE_SHIFT;
928 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
929 goto error_bdev_put;
930
931 /* pull in the page with our super */
932 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
933 index, GFP_NOFS);
934
935 if (IS_ERR_OR_NULL(page))
936 goto error_bdev_put;
937
938 p = kmap(page);
939
940 /* align our pointer to the offset of the super block */
941 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
942
943 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800944 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700945 goto error_unmap;
946
Xiao Guangronga3438322010-01-06 11:48:18 +0000947 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400948 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400949 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700950
Chris Mason8a4b83c2008-03-24 15:02:07 -0400951 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100952 if (ret > 0) {
953 if (disk_super->label[0]) {
954 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
955 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
956 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
957 } else {
958 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
959 }
960
961 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
962 ret = 0;
963 }
Josef Bacik02db0842012-06-21 16:03:58 -0400964 if (!ret && fs_devices_ret)
965 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700966
967error_unmap:
968 kunmap(page);
969 page_cache_release(page);
970
971error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100972 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400973error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100974 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400975 return ret;
976}
Chris Mason0b86a832008-03-24 15:01:56 -0400977
Miao Xie6d07bce2011-01-05 10:07:31 +0000978/* helper to account the used device space in the range */
979int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
980 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400981{
982 struct btrfs_key key;
983 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000984 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500985 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000986 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400987 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000988 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400989 struct extent_buffer *l;
990
Miao Xie6d07bce2011-01-05 10:07:31 +0000991 *length = 0;
992
Stefan Behrens63a212a2012-11-05 18:29:28 +0100993 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000994 return 0;
995
Yan Zheng2b820322008-11-17 21:11:30 -0500996 path = btrfs_alloc_path();
997 if (!path)
998 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400999 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001000
Chris Mason0b86a832008-03-24 15:01:56 -04001001 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001002 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001003 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001004
1005 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001006 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001007 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001008 if (ret > 0) {
1009 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1010 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001011 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001012 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001013
Chris Mason0b86a832008-03-24 15:01:56 -04001014 while (1) {
1015 l = path->nodes[0];
1016 slot = path->slots[0];
1017 if (slot >= btrfs_header_nritems(l)) {
1018 ret = btrfs_next_leaf(root, path);
1019 if (ret == 0)
1020 continue;
1021 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001022 goto out;
1023
1024 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001025 }
1026 btrfs_item_key_to_cpu(l, &key, slot);
1027
1028 if (key.objectid < device->devid)
1029 goto next;
1030
1031 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001032 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001033
David Sterba962a2982014-06-04 18:41:45 +02001034 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001035 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001036
Chris Mason0b86a832008-03-24 15:01:56 -04001037 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001038 extent_end = key.offset + btrfs_dev_extent_length(l,
1039 dev_extent);
1040 if (key.offset <= start && extent_end > end) {
1041 *length = end - start + 1;
1042 break;
1043 } else if (key.offset <= start && extent_end > start)
1044 *length += extent_end - start;
1045 else if (key.offset > start && extent_end <= end)
1046 *length += extent_end - key.offset;
1047 else if (key.offset > start && key.offset <= end) {
1048 *length += end - key.offset + 1;
1049 break;
1050 } else if (key.offset > end)
1051 break;
1052
1053next:
1054 path->slots[0]++;
1055 }
1056 ret = 0;
1057out:
1058 btrfs_free_path(path);
1059 return ret;
1060}
1061
Josef Bacik6df9a952013-06-27 13:22:46 -04001062static int contains_pending_extent(struct btrfs_trans_handle *trans,
1063 struct btrfs_device *device,
1064 u64 *start, u64 len)
1065{
1066 struct extent_map *em;
1067 int ret = 0;
1068
1069 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
1070 struct map_lookup *map;
1071 int i;
1072
1073 map = (struct map_lookup *)em->bdev;
1074 for (i = 0; i < map->num_stripes; i++) {
1075 if (map->stripes[i].dev != device)
1076 continue;
1077 if (map->stripes[i].physical >= *start + len ||
1078 map->stripes[i].physical + em->orig_block_len <=
1079 *start)
1080 continue;
1081 *start = map->stripes[i].physical +
1082 em->orig_block_len;
1083 ret = 1;
1084 }
1085 }
1086
1087 return ret;
1088}
1089
1090
Chris Mason0b86a832008-03-24 15:01:56 -04001091/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001092 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001093 * @device: the device which we search the free space in
1094 * @num_bytes: the size of the free space that we need
1095 * @start: store the start of the free space.
1096 * @len: the size of the free space. that we find, or the size of the max
1097 * free space if we don't find suitable free space
1098 *
Chris Mason0b86a832008-03-24 15:01:56 -04001099 * this uses a pretty simple search, the expectation is that it is
1100 * called very infrequently and that a given device has a small number
1101 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001102 *
1103 * @start is used to store the start of the free space if we find. But if we
1104 * don't find suitable free space, it will be used to store the start position
1105 * of the max free space.
1106 *
1107 * @len is used to store the size of the free space that we find.
1108 * But if we don't find suitable free space, it is used to store the size of
1109 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001110 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001111int find_free_dev_extent(struct btrfs_trans_handle *trans,
1112 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001113 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001114{
1115 struct btrfs_key key;
1116 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001117 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001118 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001119 u64 hole_size;
1120 u64 max_hole_start;
1121 u64 max_hole_size;
1122 u64 extent_end;
1123 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001124 u64 search_end = device->total_bytes;
1125 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001126 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001127 struct extent_buffer *l;
1128
Chris Mason0b86a832008-03-24 15:01:56 -04001129 /* FIXME use last free of some kind */
1130
1131 /* we don't want to overwrite the superblock on the drive,
1132 * so we make sure to start at an offset of at least 1MB
1133 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001134 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001135
Josef Bacik6df9a952013-06-27 13:22:46 -04001136 path = btrfs_alloc_path();
1137 if (!path)
1138 return -ENOMEM;
1139again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001140 max_hole_start = search_start;
1141 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001142 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001143
Stefan Behrens63a212a2012-11-05 18:29:28 +01001144 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001145 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001146 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001147 }
1148
Miao Xie7bfc8372011-01-05 10:07:26 +00001149 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001150 path->search_commit_root = 1;
1151 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001152
Chris Mason0b86a832008-03-24 15:01:56 -04001153 key.objectid = device->devid;
1154 key.offset = search_start;
1155 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001156
Li Zefan125ccb02011-12-08 15:07:24 +08001157 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001158 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001159 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001160 if (ret > 0) {
1161 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1162 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001163 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001164 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001165
Chris Mason0b86a832008-03-24 15:01:56 -04001166 while (1) {
1167 l = path->nodes[0];
1168 slot = path->slots[0];
1169 if (slot >= btrfs_header_nritems(l)) {
1170 ret = btrfs_next_leaf(root, path);
1171 if (ret == 0)
1172 continue;
1173 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001174 goto out;
1175
1176 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001177 }
1178 btrfs_item_key_to_cpu(l, &key, slot);
1179
1180 if (key.objectid < device->devid)
1181 goto next;
1182
1183 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001184 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001185
David Sterba962a2982014-06-04 18:41:45 +02001186 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001187 goto next;
1188
Miao Xie7bfc8372011-01-05 10:07:26 +00001189 if (key.offset > search_start) {
1190 hole_size = key.offset - search_start;
1191
Josef Bacik6df9a952013-06-27 13:22:46 -04001192 /*
1193 * Have to check before we set max_hole_start, otherwise
1194 * we could end up sending back this offset anyway.
1195 */
1196 if (contains_pending_extent(trans, device,
1197 &search_start,
1198 hole_size))
1199 hole_size = 0;
1200
Miao Xie7bfc8372011-01-05 10:07:26 +00001201 if (hole_size > max_hole_size) {
1202 max_hole_start = search_start;
1203 max_hole_size = hole_size;
1204 }
1205
1206 /*
1207 * If this free space is greater than which we need,
1208 * it must be the max free space that we have found
1209 * until now, so max_hole_start must point to the start
1210 * of this free space and the length of this free space
1211 * is stored in max_hole_size. Thus, we return
1212 * max_hole_start and max_hole_size and go back to the
1213 * caller.
1214 */
1215 if (hole_size >= num_bytes) {
1216 ret = 0;
1217 goto out;
1218 }
1219 }
1220
Chris Mason0b86a832008-03-24 15:01:56 -04001221 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001222 extent_end = key.offset + btrfs_dev_extent_length(l,
1223 dev_extent);
1224 if (extent_end > search_start)
1225 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001226next:
1227 path->slots[0]++;
1228 cond_resched();
1229 }
Chris Mason0b86a832008-03-24 15:01:56 -04001230
liubo38c01b92011-08-02 02:39:03 +00001231 /*
1232 * At this point, search_start should be the end of
1233 * allocated dev extents, and when shrinking the device,
1234 * search_end may be smaller than search_start.
1235 */
1236 if (search_end > search_start)
1237 hole_size = search_end - search_start;
1238
Miao Xie7bfc8372011-01-05 10:07:26 +00001239 if (hole_size > max_hole_size) {
1240 max_hole_start = search_start;
1241 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001242 }
Chris Mason0b86a832008-03-24 15:01:56 -04001243
Josef Bacik6df9a952013-06-27 13:22:46 -04001244 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1245 btrfs_release_path(path);
1246 goto again;
1247 }
1248
Miao Xie7bfc8372011-01-05 10:07:26 +00001249 /* See above. */
1250 if (hole_size < num_bytes)
1251 ret = -ENOSPC;
1252 else
1253 ret = 0;
1254
1255out:
Yan Zheng2b820322008-11-17 21:11:30 -05001256 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001257 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001258 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001259 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001260 return ret;
1261}
1262
Christoph Hellwigb2950862008-12-02 09:54:17 -05001263static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001264 struct btrfs_device *device,
1265 u64 start)
1266{
1267 int ret;
1268 struct btrfs_path *path;
1269 struct btrfs_root *root = device->dev_root;
1270 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001271 struct btrfs_key found_key;
1272 struct extent_buffer *leaf = NULL;
1273 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001274
1275 path = btrfs_alloc_path();
1276 if (!path)
1277 return -ENOMEM;
1278
1279 key.objectid = device->devid;
1280 key.offset = start;
1281 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001282again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001283 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001284 if (ret > 0) {
1285 ret = btrfs_previous_item(root, path, key.objectid,
1286 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001287 if (ret)
1288 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001289 leaf = path->nodes[0];
1290 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1291 extent = btrfs_item_ptr(leaf, path->slots[0],
1292 struct btrfs_dev_extent);
1293 BUG_ON(found_key.offset > start || found_key.offset +
1294 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001295 key = found_key;
1296 btrfs_release_path(path);
1297 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001298 } else if (ret == 0) {
1299 leaf = path->nodes[0];
1300 extent = btrfs_item_ptr(leaf, path->slots[0],
1301 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001302 } else {
1303 btrfs_error(root->fs_info, ret, "Slot search failed");
1304 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001305 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001306
Josef Bacik2bf64752011-09-26 17:12:22 -04001307 if (device->bytes_used > 0) {
1308 u64 len = btrfs_dev_extent_length(leaf, extent);
1309 device->bytes_used -= len;
1310 spin_lock(&root->fs_info->free_chunk_lock);
1311 root->fs_info->free_chunk_space += len;
1312 spin_unlock(&root->fs_info->free_chunk_lock);
1313 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001314 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001315 if (ret) {
1316 btrfs_error(root->fs_info, ret,
1317 "Failed to remove dev extent item");
1318 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001319out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001320 btrfs_free_path(path);
1321 return ret;
1322}
1323
Eric Sandeen48a3b632013-04-25 20:41:01 +00001324static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1325 struct btrfs_device *device,
1326 u64 chunk_tree, u64 chunk_objectid,
1327 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001328{
1329 int ret;
1330 struct btrfs_path *path;
1331 struct btrfs_root *root = device->dev_root;
1332 struct btrfs_dev_extent *extent;
1333 struct extent_buffer *leaf;
1334 struct btrfs_key key;
1335
Chris Masondfe25022008-05-13 13:46:40 -04001336 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001337 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001338 path = btrfs_alloc_path();
1339 if (!path)
1340 return -ENOMEM;
1341
Chris Mason0b86a832008-03-24 15:01:56 -04001342 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001343 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001344 key.type = BTRFS_DEV_EXTENT_KEY;
1345 ret = btrfs_insert_empty_item(trans, root, path, &key,
1346 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001347 if (ret)
1348 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001349
1350 leaf = path->nodes[0];
1351 extent = btrfs_item_ptr(leaf, path->slots[0],
1352 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001353 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1354 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1355 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1356
1357 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001358 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001359
Chris Mason0b86a832008-03-24 15:01:56 -04001360 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1361 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001362out:
Chris Mason0b86a832008-03-24 15:01:56 -04001363 btrfs_free_path(path);
1364 return ret;
1365}
1366
Josef Bacik6df9a952013-06-27 13:22:46 -04001367static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001368{
Josef Bacik6df9a952013-06-27 13:22:46 -04001369 struct extent_map_tree *em_tree;
1370 struct extent_map *em;
1371 struct rb_node *n;
1372 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001373
Josef Bacik6df9a952013-06-27 13:22:46 -04001374 em_tree = &fs_info->mapping_tree.map_tree;
1375 read_lock(&em_tree->lock);
1376 n = rb_last(&em_tree->map);
1377 if (n) {
1378 em = rb_entry(n, struct extent_map, rb_node);
1379 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001380 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001381 read_unlock(&em_tree->lock);
1382
Chris Mason0b86a832008-03-24 15:01:56 -04001383 return ret;
1384}
1385
Ilya Dryomov53f10652013-08-12 14:33:01 +03001386static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1387 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001388{
1389 int ret;
1390 struct btrfs_key key;
1391 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001392 struct btrfs_path *path;
1393
Yan Zheng2b820322008-11-17 21:11:30 -05001394 path = btrfs_alloc_path();
1395 if (!path)
1396 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001397
1398 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1399 key.type = BTRFS_DEV_ITEM_KEY;
1400 key.offset = (u64)-1;
1401
Ilya Dryomov53f10652013-08-12 14:33:01 +03001402 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001403 if (ret < 0)
1404 goto error;
1405
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001406 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001407
Ilya Dryomov53f10652013-08-12 14:33:01 +03001408 ret = btrfs_previous_item(fs_info->chunk_root, path,
1409 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001410 BTRFS_DEV_ITEM_KEY);
1411 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001412 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001413 } else {
1414 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1415 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001416 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001417 }
1418 ret = 0;
1419error:
Yan Zheng2b820322008-11-17 21:11:30 -05001420 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001421 return ret;
1422}
1423
1424/*
1425 * the device information is stored in the chunk root
1426 * the btrfs_device struct should be fully filled in
1427 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001428static int btrfs_add_device(struct btrfs_trans_handle *trans,
1429 struct btrfs_root *root,
1430 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001431{
1432 int ret;
1433 struct btrfs_path *path;
1434 struct btrfs_dev_item *dev_item;
1435 struct extent_buffer *leaf;
1436 struct btrfs_key key;
1437 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001438
1439 root = root->fs_info->chunk_root;
1440
1441 path = btrfs_alloc_path();
1442 if (!path)
1443 return -ENOMEM;
1444
Chris Mason0b86a832008-03-24 15:01:56 -04001445 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1446 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001447 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001448
1449 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001450 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001451 if (ret)
1452 goto out;
1453
1454 leaf = path->nodes[0];
1455 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1456
1457 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001458 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001459 btrfs_set_device_type(leaf, dev_item, device->type);
1460 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1461 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1462 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7df69d32014-07-24 11:37:13 +08001463 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001464 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001465 btrfs_set_device_group(leaf, dev_item, 0);
1466 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1467 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001468 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001469
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001470 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001471 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001472 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001473 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001474 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001475
Yan Zheng2b820322008-11-17 21:11:30 -05001476 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001477out:
1478 btrfs_free_path(path);
1479 return ret;
1480}
Chris Mason8f18cf12008-04-25 16:53:30 -04001481
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001482/*
1483 * Function to update ctime/mtime for a given device path.
1484 * Mainly used for ctime/mtime based probe like libblkid.
1485 */
1486static void update_dev_time(char *path_name)
1487{
1488 struct file *filp;
1489
1490 filp = filp_open(path_name, O_RDWR, 0);
1491 if (!filp)
1492 return;
1493 file_update_time(filp);
1494 filp_close(filp, NULL);
1495 return;
1496}
1497
Chris Masona061fc82008-05-07 11:43:44 -04001498static int btrfs_rm_dev_item(struct btrfs_root *root,
1499 struct btrfs_device *device)
1500{
1501 int ret;
1502 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001503 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001504 struct btrfs_trans_handle *trans;
1505
1506 root = root->fs_info->chunk_root;
1507
1508 path = btrfs_alloc_path();
1509 if (!path)
1510 return -ENOMEM;
1511
Yan, Zhenga22285a2010-05-16 10:48:46 -04001512 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001513 if (IS_ERR(trans)) {
1514 btrfs_free_path(path);
1515 return PTR_ERR(trans);
1516 }
Chris Masona061fc82008-05-07 11:43:44 -04001517 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1518 key.type = BTRFS_DEV_ITEM_KEY;
1519 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001520 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001521
1522 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1523 if (ret < 0)
1524 goto out;
1525
1526 if (ret > 0) {
1527 ret = -ENOENT;
1528 goto out;
1529 }
1530
1531 ret = btrfs_del_item(trans, root, path);
1532 if (ret)
1533 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001534out:
1535 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001536 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001537 btrfs_commit_transaction(trans, root);
1538 return ret;
1539}
1540
1541int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1542{
1543 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001544 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001545 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001546 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001547 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001548 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001549 u64 all_avail;
1550 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001551 u64 num_devices;
1552 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001553 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001554 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001555 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001556
Chris Masona061fc82008-05-07 11:43:44 -04001557 mutex_lock(&uuid_mutex);
1558
Miao Xiede98ced2013-01-29 10:13:12 +00001559 do {
1560 seq = read_seqbegin(&root->fs_info->profiles_lock);
1561
1562 all_avail = root->fs_info->avail_data_alloc_bits |
1563 root->fs_info->avail_system_alloc_bits |
1564 root->fs_info->avail_metadata_alloc_bits;
1565 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001566
Stefan Behrens8dabb742012-11-06 13:15:27 +01001567 num_devices = root->fs_info->fs_devices->num_devices;
1568 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1569 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1570 WARN_ON(num_devices < 1);
1571 num_devices--;
1572 }
1573 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1574
1575 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001576 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001577 goto out;
1578 }
1579
Stefan Behrens8dabb742012-11-06 13:15:27 +01001580 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001581 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001582 goto out;
1583 }
1584
David Woodhouse53b381b2013-01-29 18:40:14 -05001585 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1586 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001587 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001588 goto out;
1589 }
1590 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1591 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001592 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001593 goto out;
1594 }
1595
Chris Masondfe25022008-05-13 13:46:40 -04001596 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001597 struct list_head *devices;
1598 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001599
Chris Masondfe25022008-05-13 13:46:40 -04001600 device = NULL;
1601 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001602 /*
1603 * It is safe to read the devices since the volume_mutex
1604 * is held.
1605 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001606 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001607 if (tmp->in_fs_metadata &&
1608 !tmp->is_tgtdev_for_dev_replace &&
1609 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001610 device = tmp;
1611 break;
1612 }
1613 }
1614 bdev = NULL;
1615 bh = NULL;
1616 disk_super = NULL;
1617 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001618 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001619 goto out;
1620 }
Chris Masondfe25022008-05-13 13:46:40 -04001621 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001622 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001623 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001624 root->fs_info->bdev_holder, 0,
1625 &bdev, &bh);
1626 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001627 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001628 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001629 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001630 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001631 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001632 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001633 if (!device) {
1634 ret = -ENOENT;
1635 goto error_brelse;
1636 }
Chris Masondfe25022008-05-13 13:46:40 -04001637 }
Yan Zheng2b820322008-11-17 21:11:30 -05001638
Stefan Behrens63a212a2012-11-05 18:29:28 +01001639 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001640 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001641 goto error_brelse;
1642 }
1643
Yan Zheng2b820322008-11-17 21:11:30 -05001644 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001645 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001646 goto error_brelse;
1647 }
1648
1649 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001650 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001651 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001652 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001653 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001654 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001655 }
Chris Masona061fc82008-05-07 11:43:44 -04001656
Carey Underwoodd7901552013-03-04 16:37:06 -06001657 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001658 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001659 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001660 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001661 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001662
Stefan Behrens63a212a2012-11-05 18:29:28 +01001663 /*
1664 * TODO: the superblock still includes this device in its num_devices
1665 * counter although write_all_supers() is not locked out. This
1666 * could give a filesystem state which requires a degraded mount.
1667 */
Chris Masona061fc82008-05-07 11:43:44 -04001668 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1669 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001670 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001671
Josef Bacik2bf64752011-09-26 17:12:22 -04001672 spin_lock(&root->fs_info->free_chunk_lock);
1673 root->fs_info->free_chunk_space = device->total_bytes -
1674 device->bytes_used;
1675 spin_unlock(&root->fs_info->free_chunk_lock);
1676
Yan Zheng2b820322008-11-17 21:11:30 -05001677 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001678 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001679
1680 /*
1681 * the device list mutex makes sure that we don't change
1682 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001683 * the device supers. Whoever is writing all supers, should
1684 * lock the device list mutex before getting the number of
1685 * devices in the super block (super_copy). Conversely,
1686 * whoever updates the number of devices in the super block
1687 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001688 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001689
1690 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001691 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001692 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001693
Yan Zhenge4404d62008-12-12 10:03:26 -05001694 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001695 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001696
Chris Masoncd02dca2010-12-13 14:56:23 -05001697 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001698 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001699
Yan Zheng2b820322008-11-17 21:11:30 -05001700 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1701 struct btrfs_device, dev_list);
1702 if (device->bdev == root->fs_info->sb->s_bdev)
1703 root->fs_info->sb->s_bdev = next_device->bdev;
1704 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1705 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1706
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001707 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001708 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001709 /* remove sysfs entry */
1710 btrfs_kobj_rm_device(root->fs_info, device);
1711 }
Anand Jain99994cd2014-06-03 11:36:00 +08001712
Xiao Guangrong1f781602011-04-20 10:09:16 +00001713 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001714
David Sterba6c417612011-04-13 15:41:04 +02001715 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1716 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001717 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001718
Xiao Guangrong1f781602011-04-20 10:09:16 +00001719 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001720 struct btrfs_fs_devices *fs_devices;
1721 fs_devices = root->fs_info->fs_devices;
1722 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001723 if (fs_devices->seed == cur_devices) {
1724 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001725 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001726 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001727 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001728 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001729 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001730 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001731 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001732 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001733 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001734 }
1735
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001736 root->fs_info->num_tolerated_disk_barrier_failures =
1737 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1738
Yan Zheng2b820322008-11-17 21:11:30 -05001739 /*
1740 * at this point, the device is zero sized. We want to
1741 * remove it from the devices list and zero out the old super
1742 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001743 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001744 u64 bytenr;
1745 int i;
1746
Chris Masondfe25022008-05-13 13:46:40 -04001747 /* make sure this device isn't detected as part of
1748 * the FS anymore
1749 */
1750 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1751 set_buffer_dirty(bh);
1752 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001753
1754 /* clear the mirror copies of super block on the disk
1755 * being removed, 0th copy is been taken care above and
1756 * the below would take of the rest
1757 */
1758 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1759 bytenr = btrfs_sb_offset(i);
1760 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1761 i_size_read(bdev->bd_inode))
1762 break;
1763
1764 brelse(bh);
1765 bh = __bread(bdev, bytenr / 4096,
1766 BTRFS_SUPER_INFO_SIZE);
1767 if (!bh)
1768 continue;
1769
1770 disk_super = (struct btrfs_super_block *)bh->b_data;
1771
1772 if (btrfs_super_bytenr(disk_super) != bytenr ||
1773 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1774 continue;
1775 }
1776 memset(&disk_super->magic, 0,
1777 sizeof(disk_super->magic));
1778 set_buffer_dirty(bh);
1779 sync_dirty_buffer(bh);
1780 }
Chris Masondfe25022008-05-13 13:46:40 -04001781 }
Chris Masona061fc82008-05-07 11:43:44 -04001782
Chris Masona061fc82008-05-07 11:43:44 -04001783 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001784
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001785 if (bdev) {
1786 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001787 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001788
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001789 /* Update ctime/mtime for device path for libblkid */
1790 update_dev_time(device_path);
1791 }
1792
Chris Masona061fc82008-05-07 11:43:44 -04001793error_brelse:
1794 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001795 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001796 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001797out:
1798 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001799 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001800error_undo:
1801 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001802 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001803 list_add(&device->dev_alloc_list,
1804 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001805 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001806 root->fs_info->fs_devices->rw_devices++;
1807 }
1808 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001809}
1810
Stefan Behrense93c89c2012-11-05 17:33:06 +01001811void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1812 struct btrfs_device *srcdev)
1813{
Anand Jaind51908c2014-08-13 14:24:19 +08001814 struct btrfs_fs_devices *fs_devices;
1815
Stefan Behrense93c89c2012-11-05 17:33:06 +01001816 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001817
Anand Jain25e8e912014-08-20 10:56:56 +08001818 /*
1819 * in case of fs with no seed, srcdev->fs_devices will point
1820 * to fs_devices of fs_info. However when the dev being replaced is
1821 * a seed dev it will point to the seed's local fs_devices. In short
1822 * srcdev will have its correct fs_devices in both the cases.
1823 */
1824 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001825
Stefan Behrense93c89c2012-11-05 17:33:06 +01001826 list_del_rcu(&srcdev->dev_list);
1827 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001828 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001829 if (srcdev->missing) {
Anand Jaind51908c2014-08-13 14:24:19 +08001830 fs_devices->missing_devices--;
Anand Jainb2efedc2014-08-13 14:24:24 +08001831 if (!fs_devices->seeding)
1832 fs_devices->rw_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001833 }
Miao Xie90180da2014-09-03 21:35:30 +08001834
Ilya Dryomov13572722013-10-02 20:41:01 +03001835 if (srcdev->bdev) {
Anand Jaind51908c2014-08-13 14:24:19 +08001836 fs_devices->open_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001837
Miao Xieff61d172014-07-24 11:37:06 +08001838 /*
1839 * zero out the old super if it is not writable
1840 * (e.g. seed device)
1841 */
1842 if (srcdev->writeable)
1843 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03001844 }
1845
Stefan Behrense93c89c2012-11-05 17:33:06 +01001846 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001847
1848 /*
1849 * unless fs_devices is seed fs, num_devices shouldn't go
1850 * zero
1851 */
1852 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1853
1854 /* if this is no devs we rather delete the fs_devices */
1855 if (!fs_devices->num_devices) {
1856 struct btrfs_fs_devices *tmp_fs_devices;
1857
1858 tmp_fs_devices = fs_info->fs_devices;
1859 while (tmp_fs_devices) {
1860 if (tmp_fs_devices->seed == fs_devices) {
1861 tmp_fs_devices->seed = fs_devices->seed;
1862 break;
1863 }
1864 tmp_fs_devices = tmp_fs_devices->seed;
1865 }
1866 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08001867 __btrfs_close_devices(fs_devices);
1868 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001869 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01001870}
1871
1872void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1873 struct btrfs_device *tgtdev)
1874{
1875 struct btrfs_device *next_device;
1876
1877 WARN_ON(!tgtdev);
1878 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1879 if (tgtdev->bdev) {
1880 btrfs_scratch_superblock(tgtdev);
1881 fs_info->fs_devices->open_devices--;
1882 }
1883 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001884
1885 next_device = list_entry(fs_info->fs_devices->devices.next,
1886 struct btrfs_device, dev_list);
1887 if (tgtdev->bdev == fs_info->sb->s_bdev)
1888 fs_info->sb->s_bdev = next_device->bdev;
1889 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1890 fs_info->fs_devices->latest_bdev = next_device->bdev;
1891 list_del_rcu(&tgtdev->dev_list);
1892
1893 call_rcu(&tgtdev->rcu, free_device);
1894
1895 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1896}
1897
Eric Sandeen48a3b632013-04-25 20:41:01 +00001898static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1899 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001900{
1901 int ret = 0;
1902 struct btrfs_super_block *disk_super;
1903 u64 devid;
1904 u8 *dev_uuid;
1905 struct block_device *bdev;
1906 struct buffer_head *bh;
1907
1908 *device = NULL;
1909 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1910 root->fs_info->bdev_holder, 0, &bdev, &bh);
1911 if (ret)
1912 return ret;
1913 disk_super = (struct btrfs_super_block *)bh->b_data;
1914 devid = btrfs_stack_device_id(&disk_super->dev_item);
1915 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001916 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001917 disk_super->fsid);
1918 brelse(bh);
1919 if (!*device)
1920 ret = -ENOENT;
1921 blkdev_put(bdev, FMODE_READ);
1922 return ret;
1923}
1924
1925int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1926 char *device_path,
1927 struct btrfs_device **device)
1928{
1929 *device = NULL;
1930 if (strcmp(device_path, "missing") == 0) {
1931 struct list_head *devices;
1932 struct btrfs_device *tmp;
1933
1934 devices = &root->fs_info->fs_devices->devices;
1935 /*
1936 * It is safe to read the devices since the volume_mutex
1937 * is held by the caller.
1938 */
1939 list_for_each_entry(tmp, devices, dev_list) {
1940 if (tmp->in_fs_metadata && !tmp->bdev) {
1941 *device = tmp;
1942 break;
1943 }
1944 }
1945
1946 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001947 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001948 return -ENOENT;
1949 }
1950
1951 return 0;
1952 } else {
1953 return btrfs_find_device_by_path(root, device_path, device);
1954 }
1955}
1956
Yan Zheng2b820322008-11-17 21:11:30 -05001957/*
1958 * does all the dirty work required for changing file system's UUID.
1959 */
Li Zefan125ccb02011-12-08 15:07:24 +08001960static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001961{
1962 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1963 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001964 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001965 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001966 struct btrfs_device *device;
1967 u64 super_flags;
1968
1969 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001970 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001971 return -EINVAL;
1972
Ilya Dryomov2208a372013-08-12 14:33:03 +03001973 seed_devices = __alloc_fs_devices();
1974 if (IS_ERR(seed_devices))
1975 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001976
Yan Zhenge4404d62008-12-12 10:03:26 -05001977 old_devices = clone_fs_devices(fs_devices);
1978 if (IS_ERR(old_devices)) {
1979 kfree(seed_devices);
1980 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001981 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001982
Yan Zheng2b820322008-11-17 21:11:30 -05001983 list_add(&old_devices->list, &fs_uuids);
1984
Yan Zhenge4404d62008-12-12 10:03:26 -05001985 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1986 seed_devices->opened = 1;
1987 INIT_LIST_HEAD(&seed_devices->devices);
1988 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001989 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001990
1991 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001992 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1993 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001994
Yan Zhenge4404d62008-12-12 10:03:26 -05001995 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1996 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1997 device->fs_devices = seed_devices;
1998 }
1999
Yan Zheng2b820322008-11-17 21:11:30 -05002000 fs_devices->seeding = 0;
2001 fs_devices->num_devices = 0;
2002 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002003 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002004 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002005 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002006
2007 generate_random_uuid(fs_devices->fsid);
2008 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2009 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002010 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2011
Yan Zheng2b820322008-11-17 21:11:30 -05002012 super_flags = btrfs_super_flags(disk_super) &
2013 ~BTRFS_SUPER_FLAG_SEEDING;
2014 btrfs_set_super_flags(disk_super, super_flags);
2015
2016 return 0;
2017}
2018
2019/*
2020 * strore the expected generation for seed devices in device items.
2021 */
2022static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2023 struct btrfs_root *root)
2024{
2025 struct btrfs_path *path;
2026 struct extent_buffer *leaf;
2027 struct btrfs_dev_item *dev_item;
2028 struct btrfs_device *device;
2029 struct btrfs_key key;
2030 u8 fs_uuid[BTRFS_UUID_SIZE];
2031 u8 dev_uuid[BTRFS_UUID_SIZE];
2032 u64 devid;
2033 int ret;
2034
2035 path = btrfs_alloc_path();
2036 if (!path)
2037 return -ENOMEM;
2038
2039 root = root->fs_info->chunk_root;
2040 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2041 key.offset = 0;
2042 key.type = BTRFS_DEV_ITEM_KEY;
2043
2044 while (1) {
2045 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2046 if (ret < 0)
2047 goto error;
2048
2049 leaf = path->nodes[0];
2050next_slot:
2051 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2052 ret = btrfs_next_leaf(root, path);
2053 if (ret > 0)
2054 break;
2055 if (ret < 0)
2056 goto error;
2057 leaf = path->nodes[0];
2058 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002059 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002060 continue;
2061 }
2062
2063 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2064 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2065 key.type != BTRFS_DEV_ITEM_KEY)
2066 break;
2067
2068 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2069 struct btrfs_dev_item);
2070 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002071 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002072 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002073 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002074 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002075 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2076 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002077 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002078
2079 if (device->fs_devices->seeding) {
2080 btrfs_set_device_generation(leaf, dev_item,
2081 device->generation);
2082 btrfs_mark_buffer_dirty(leaf);
2083 }
2084
2085 path->slots[0]++;
2086 goto next_slot;
2087 }
2088 ret = 0;
2089error:
2090 btrfs_free_path(path);
2091 return ret;
2092}
2093
Chris Mason788f20e2008-04-28 15:29:42 -04002094int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2095{
Josef Bacikd5e20032011-08-04 14:52:27 +00002096 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002097 struct btrfs_trans_handle *trans;
2098 struct btrfs_device *device;
2099 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002100 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002101 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002102 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002103 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002104 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002105 int ret = 0;
2106
Yan Zheng2b820322008-11-17 21:11:30 -05002107 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002108 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002109
Li Zefana5d16332011-12-07 20:08:40 -05002110 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002111 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002112 if (IS_ERR(bdev))
2113 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002114
Yan Zheng2b820322008-11-17 21:11:30 -05002115 if (root->fs_info->fs_devices->seeding) {
2116 seeding_dev = 1;
2117 down_write(&sb->s_umount);
2118 mutex_lock(&uuid_mutex);
2119 }
2120
Chris Mason8c8bee12008-09-29 11:19:10 -04002121 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002122
Chris Mason788f20e2008-04-28 15:29:42 -04002123 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002124
2125 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002126 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002127 if (device->bdev == bdev) {
2128 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002129 mutex_unlock(
2130 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002131 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002132 }
2133 }
Liu Bod25628b2012-11-14 14:35:30 +00002134 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002135
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002136 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2137 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002138 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002139 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002140 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002141 }
2142
Josef Bacik606686e2012-06-04 14:03:51 -04002143 name = rcu_string_strdup(device_path, GFP_NOFS);
2144 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002145 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002146 ret = -ENOMEM;
2147 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002148 }
Josef Bacik606686e2012-06-04 14:03:51 -04002149 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002150
Yan, Zhenga22285a2010-05-16 10:48:46 -04002151 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002152 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002153 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002154 kfree(device);
2155 ret = PTR_ERR(trans);
2156 goto error;
2157 }
2158
Yan Zheng2b820322008-11-17 21:11:30 -05002159 lock_chunks(root);
2160
Josef Bacikd5e20032011-08-04 14:52:27 +00002161 q = bdev_get_queue(bdev);
2162 if (blk_queue_discard(q))
2163 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002164 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002165 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002166 device->io_width = root->sectorsize;
2167 device->io_align = root->sectorsize;
2168 device->sector_size = root->sectorsize;
2169 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002170 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002171 device->dev_root = root->fs_info->dev_root;
2172 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002173 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002174 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002175 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002176 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002177 set_blocksize(device->bdev, 4096);
2178
Yan Zheng2b820322008-11-17 21:11:30 -05002179 if (seeding_dev) {
2180 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002181 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002182 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002183 }
2184
2185 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002186
Chris Masone5e9a522009-06-10 15:17:02 -04002187 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002188 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002189 list_add(&device->dev_alloc_list,
2190 &root->fs_info->fs_devices->alloc_list);
2191 root->fs_info->fs_devices->num_devices++;
2192 root->fs_info->fs_devices->open_devices++;
2193 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002194 root->fs_info->fs_devices->total_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -05002195 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2196
Josef Bacik2bf64752011-09-26 17:12:22 -04002197 spin_lock(&root->fs_info->free_chunk_lock);
2198 root->fs_info->free_chunk_space += device->total_bytes;
2199 spin_unlock(&root->fs_info->free_chunk_lock);
2200
Chris Masonc2898112009-06-10 09:51:32 -04002201 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2202 root->fs_info->fs_devices->rotating = 1;
2203
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002204 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002205 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002206 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002207
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002208 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002209 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002210 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002211
2212 /* add sysfs device entry */
2213 btrfs_kobj_add_device(root->fs_info, device);
2214
Chris Masone5e9a522009-06-10 15:17:02 -04002215 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002216
Yan Zheng2b820322008-11-17 21:11:30 -05002217 if (seeding_dev) {
Anand Jainb2373f22014-06-03 11:36:03 +08002218 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002219 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002220 if (ret) {
2221 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002222 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002223 }
Yan Zheng2b820322008-11-17 21:11:30 -05002224 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002225 if (ret) {
2226 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002227 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002228 }
Anand Jainb2373f22014-06-03 11:36:03 +08002229
2230 /* Sprouting would change fsid of the mounted root,
2231 * so rename the fsid on the sysfs
2232 */
2233 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2234 root->fs_info->fsid);
2235 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2236 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002237 } else {
2238 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002239 if (ret) {
2240 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002241 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002242 }
Yan Zheng2b820322008-11-17 21:11:30 -05002243 }
2244
Chris Mason913d9522009-03-10 13:17:18 -04002245 /*
2246 * we've got more storage, clear any full flags on the space
2247 * infos
2248 */
2249 btrfs_clear_space_info_full(root->fs_info);
2250
Chris Mason7d9eb122008-07-08 14:19:17 -04002251 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002252 root->fs_info->num_tolerated_disk_barrier_failures =
2253 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002254 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002255
2256 if (seeding_dev) {
2257 mutex_unlock(&uuid_mutex);
2258 up_write(&sb->s_umount);
2259
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002260 if (ret) /* transaction commit */
2261 return ret;
2262
Yan Zheng2b820322008-11-17 21:11:30 -05002263 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002264 if (ret < 0)
2265 btrfs_error(root->fs_info, ret,
2266 "Failed to relocate sys chunks after "
2267 "device initialization. This can be fixed "
2268 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002269 trans = btrfs_attach_transaction(root);
2270 if (IS_ERR(trans)) {
2271 if (PTR_ERR(trans) == -ENOENT)
2272 return 0;
2273 return PTR_ERR(trans);
2274 }
2275 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002276 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002277
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002278 /* Update ctime/mtime for libblkid */
2279 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002280 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002281
2282error_trans:
2283 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002284 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002285 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002286 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002287 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002288error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002289 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002290 if (seeding_dev) {
2291 mutex_unlock(&uuid_mutex);
2292 up_write(&sb->s_umount);
2293 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002294 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002295}
2296
Stefan Behrense93c89c2012-11-05 17:33:06 +01002297int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2298 struct btrfs_device **device_out)
2299{
2300 struct request_queue *q;
2301 struct btrfs_device *device;
2302 struct block_device *bdev;
2303 struct btrfs_fs_info *fs_info = root->fs_info;
2304 struct list_head *devices;
2305 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002306 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002307 int ret = 0;
2308
2309 *device_out = NULL;
2310 if (fs_info->fs_devices->seeding)
2311 return -EINVAL;
2312
2313 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2314 fs_info->bdev_holder);
2315 if (IS_ERR(bdev))
2316 return PTR_ERR(bdev);
2317
2318 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2319
2320 devices = &fs_info->fs_devices->devices;
2321 list_for_each_entry(device, devices, dev_list) {
2322 if (device->bdev == bdev) {
2323 ret = -EEXIST;
2324 goto error;
2325 }
2326 }
2327
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002328 device = btrfs_alloc_device(NULL, &devid, NULL);
2329 if (IS_ERR(device)) {
2330 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002331 goto error;
2332 }
2333
2334 name = rcu_string_strdup(device_path, GFP_NOFS);
2335 if (!name) {
2336 kfree(device);
2337 ret = -ENOMEM;
2338 goto error;
2339 }
2340 rcu_assign_pointer(device->name, name);
2341
2342 q = bdev_get_queue(bdev);
2343 if (blk_queue_discard(q))
2344 device->can_discard = 1;
2345 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2346 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002347 device->generation = 0;
2348 device->io_width = root->sectorsize;
2349 device->io_align = root->sectorsize;
2350 device->sector_size = root->sectorsize;
2351 device->total_bytes = i_size_read(bdev->bd_inode);
2352 device->disk_total_bytes = device->total_bytes;
2353 device->dev_root = fs_info->dev_root;
2354 device->bdev = bdev;
2355 device->in_fs_metadata = 1;
2356 device->is_tgtdev_for_dev_replace = 1;
2357 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002358 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002359 set_blocksize(device->bdev, 4096);
2360 device->fs_devices = fs_info->fs_devices;
2361 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2362 fs_info->fs_devices->num_devices++;
2363 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002364 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2365
2366 *device_out = device;
2367 return ret;
2368
2369error:
2370 blkdev_put(bdev, FMODE_EXCL);
2371 return ret;
2372}
2373
2374void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2375 struct btrfs_device *tgtdev)
2376{
2377 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2378 tgtdev->io_width = fs_info->dev_root->sectorsize;
2379 tgtdev->io_align = fs_info->dev_root->sectorsize;
2380 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2381 tgtdev->dev_root = fs_info->dev_root;
2382 tgtdev->in_fs_metadata = 1;
2383}
2384
Chris Masond3977122009-01-05 21:25:51 -05002385static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2386 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002387{
2388 int ret;
2389 struct btrfs_path *path;
2390 struct btrfs_root *root;
2391 struct btrfs_dev_item *dev_item;
2392 struct extent_buffer *leaf;
2393 struct btrfs_key key;
2394
2395 root = device->dev_root->fs_info->chunk_root;
2396
2397 path = btrfs_alloc_path();
2398 if (!path)
2399 return -ENOMEM;
2400
2401 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2402 key.type = BTRFS_DEV_ITEM_KEY;
2403 key.offset = device->devid;
2404
2405 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2406 if (ret < 0)
2407 goto out;
2408
2409 if (ret > 0) {
2410 ret = -ENOENT;
2411 goto out;
2412 }
2413
2414 leaf = path->nodes[0];
2415 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2416
2417 btrfs_set_device_id(leaf, dev_item, device->devid);
2418 btrfs_set_device_type(leaf, dev_item, device->type);
2419 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2420 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2421 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002422 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002423 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2424 btrfs_mark_buffer_dirty(leaf);
2425
2426out:
2427 btrfs_free_path(path);
2428 return ret;
2429}
2430
Chris Mason7d9eb122008-07-08 14:19:17 -04002431static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002432 struct btrfs_device *device, u64 new_size)
2433{
2434 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002435 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002436 u64 old_total = btrfs_super_total_bytes(super_copy);
2437 u64 diff = new_size - device->total_bytes;
2438
Yan Zheng2b820322008-11-17 21:11:30 -05002439 if (!device->writeable)
2440 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002441 if (new_size <= device->total_bytes ||
2442 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002443 return -EINVAL;
2444
Chris Mason8f18cf12008-04-25 16:53:30 -04002445 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002446 device->fs_devices->total_rw_bytes += diff;
2447
2448 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002449 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002450 btrfs_clear_space_info_full(device->dev_root->fs_info);
2451
Chris Mason8f18cf12008-04-25 16:53:30 -04002452 return btrfs_update_device(trans, device);
2453}
2454
Chris Mason7d9eb122008-07-08 14:19:17 -04002455int btrfs_grow_device(struct btrfs_trans_handle *trans,
2456 struct btrfs_device *device, u64 new_size)
2457{
2458 int ret;
2459 lock_chunks(device->dev_root);
2460 ret = __btrfs_grow_device(trans, device, new_size);
2461 unlock_chunks(device->dev_root);
2462 return ret;
2463}
2464
Chris Mason8f18cf12008-04-25 16:53:30 -04002465static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2466 struct btrfs_root *root,
2467 u64 chunk_tree, u64 chunk_objectid,
2468 u64 chunk_offset)
2469{
2470 int ret;
2471 struct btrfs_path *path;
2472 struct btrfs_key key;
2473
2474 root = root->fs_info->chunk_root;
2475 path = btrfs_alloc_path();
2476 if (!path)
2477 return -ENOMEM;
2478
2479 key.objectid = chunk_objectid;
2480 key.offset = chunk_offset;
2481 key.type = BTRFS_CHUNK_ITEM_KEY;
2482
2483 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002484 if (ret < 0)
2485 goto out;
2486 else if (ret > 0) { /* Logic error or corruption */
2487 btrfs_error(root->fs_info, -ENOENT,
2488 "Failed lookup while freeing chunk.");
2489 ret = -ENOENT;
2490 goto out;
2491 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002492
2493 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002494 if (ret < 0)
2495 btrfs_error(root->fs_info, ret,
2496 "Failed to delete chunk item.");
2497out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002498 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002499 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002500}
2501
Christoph Hellwigb2950862008-12-02 09:54:17 -05002502static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002503 chunk_offset)
2504{
David Sterba6c417612011-04-13 15:41:04 +02002505 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002506 struct btrfs_disk_key *disk_key;
2507 struct btrfs_chunk *chunk;
2508 u8 *ptr;
2509 int ret = 0;
2510 u32 num_stripes;
2511 u32 array_size;
2512 u32 len = 0;
2513 u32 cur;
2514 struct btrfs_key key;
2515
2516 array_size = btrfs_super_sys_array_size(super_copy);
2517
2518 ptr = super_copy->sys_chunk_array;
2519 cur = 0;
2520
2521 while (cur < array_size) {
2522 disk_key = (struct btrfs_disk_key *)ptr;
2523 btrfs_disk_key_to_cpu(&key, disk_key);
2524
2525 len = sizeof(*disk_key);
2526
2527 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2528 chunk = (struct btrfs_chunk *)(ptr + len);
2529 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2530 len += btrfs_chunk_item_size(num_stripes);
2531 } else {
2532 ret = -EIO;
2533 break;
2534 }
2535 if (key.objectid == chunk_objectid &&
2536 key.offset == chunk_offset) {
2537 memmove(ptr, ptr + len, array_size - (cur + len));
2538 array_size -= len;
2539 btrfs_set_super_sys_array_size(super_copy, array_size);
2540 } else {
2541 ptr += len;
2542 cur += len;
2543 }
2544 }
2545 return ret;
2546}
2547
Christoph Hellwigb2950862008-12-02 09:54:17 -05002548static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002549 u64 chunk_tree, u64 chunk_objectid,
2550 u64 chunk_offset)
2551{
2552 struct extent_map_tree *em_tree;
2553 struct btrfs_root *extent_root;
2554 struct btrfs_trans_handle *trans;
2555 struct extent_map *em;
2556 struct map_lookup *map;
2557 int ret;
2558 int i;
2559
2560 root = root->fs_info->chunk_root;
2561 extent_root = root->fs_info->extent_root;
2562 em_tree = &root->fs_info->mapping_tree.map_tree;
2563
Josef Bacikba1bf482009-09-11 16:11:19 -04002564 ret = btrfs_can_relocate(extent_root, chunk_offset);
2565 if (ret)
2566 return -ENOSPC;
2567
Chris Mason8f18cf12008-04-25 16:53:30 -04002568 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002569 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002570 if (ret)
2571 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002572
Yan, Zhenga22285a2010-05-16 10:48:46 -04002573 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002574 if (IS_ERR(trans)) {
2575 ret = PTR_ERR(trans);
2576 btrfs_std_error(root->fs_info, ret);
2577 return ret;
2578 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002579
Chris Mason7d9eb122008-07-08 14:19:17 -04002580 lock_chunks(root);
2581
Chris Mason8f18cf12008-04-25 16:53:30 -04002582 /*
2583 * step two, delete the device extents and the
2584 * chunk tree entries
2585 */
Chris Mason890871b2009-09-02 16:24:52 -04002586 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002587 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002588 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002589
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002590 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002591 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002592 map = (struct map_lookup *)em->bdev;
2593
2594 for (i = 0; i < map->num_stripes; i++) {
2595 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2596 map->stripes[i].physical);
2597 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002598
Chris Masondfe25022008-05-13 13:46:40 -04002599 if (map->stripes[i].dev) {
2600 ret = btrfs_update_device(trans, map->stripes[i].dev);
2601 BUG_ON(ret);
2602 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002603 }
2604 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2605 chunk_offset);
2606
2607 BUG_ON(ret);
2608
liubo1abe9b82011-03-24 11:18:59 +00002609 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2610
Chris Mason8f18cf12008-04-25 16:53:30 -04002611 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2612 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2613 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002614 }
2615
Zheng Yan1a40e232008-09-26 10:09:34 -04002616 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2617 BUG_ON(ret);
2618
Chris Mason890871b2009-09-02 16:24:52 -04002619 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002620 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002621 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002622
Chris Mason8f18cf12008-04-25 16:53:30 -04002623 /* once for the tree */
2624 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002625 /* once for us */
2626 free_extent_map(em);
2627
Chris Mason7d9eb122008-07-08 14:19:17 -04002628 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002629 btrfs_end_transaction(trans, root);
2630 return 0;
2631}
2632
Yan Zheng2b820322008-11-17 21:11:30 -05002633static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2634{
2635 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2636 struct btrfs_path *path;
2637 struct extent_buffer *leaf;
2638 struct btrfs_chunk *chunk;
2639 struct btrfs_key key;
2640 struct btrfs_key found_key;
2641 u64 chunk_tree = chunk_root->root_key.objectid;
2642 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002643 bool retried = false;
2644 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002645 int ret;
2646
2647 path = btrfs_alloc_path();
2648 if (!path)
2649 return -ENOMEM;
2650
Josef Bacikba1bf482009-09-11 16:11:19 -04002651again:
Yan Zheng2b820322008-11-17 21:11:30 -05002652 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2653 key.offset = (u64)-1;
2654 key.type = BTRFS_CHUNK_ITEM_KEY;
2655
2656 while (1) {
2657 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2658 if (ret < 0)
2659 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002660 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002661
2662 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2663 key.type);
2664 if (ret < 0)
2665 goto error;
2666 if (ret > 0)
2667 break;
2668
2669 leaf = path->nodes[0];
2670 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2671
2672 chunk = btrfs_item_ptr(leaf, path->slots[0],
2673 struct btrfs_chunk);
2674 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002675 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002676
2677 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2678 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2679 found_key.objectid,
2680 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002681 if (ret == -ENOSPC)
2682 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302683 else
2684 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002685 }
2686
2687 if (found_key.offset == 0)
2688 break;
2689 key.offset = found_key.offset - 1;
2690 }
2691 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002692 if (failed && !retried) {
2693 failed = 0;
2694 retried = true;
2695 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302696 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002697 ret = -ENOSPC;
2698 }
Yan Zheng2b820322008-11-17 21:11:30 -05002699error:
2700 btrfs_free_path(path);
2701 return ret;
2702}
2703
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002704static int insert_balance_item(struct btrfs_root *root,
2705 struct btrfs_balance_control *bctl)
2706{
2707 struct btrfs_trans_handle *trans;
2708 struct btrfs_balance_item *item;
2709 struct btrfs_disk_balance_args disk_bargs;
2710 struct btrfs_path *path;
2711 struct extent_buffer *leaf;
2712 struct btrfs_key key;
2713 int ret, err;
2714
2715 path = btrfs_alloc_path();
2716 if (!path)
2717 return -ENOMEM;
2718
2719 trans = btrfs_start_transaction(root, 0);
2720 if (IS_ERR(trans)) {
2721 btrfs_free_path(path);
2722 return PTR_ERR(trans);
2723 }
2724
2725 key.objectid = BTRFS_BALANCE_OBJECTID;
2726 key.type = BTRFS_BALANCE_ITEM_KEY;
2727 key.offset = 0;
2728
2729 ret = btrfs_insert_empty_item(trans, root, path, &key,
2730 sizeof(*item));
2731 if (ret)
2732 goto out;
2733
2734 leaf = path->nodes[0];
2735 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2736
2737 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2738
2739 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2740 btrfs_set_balance_data(leaf, item, &disk_bargs);
2741 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2742 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2743 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2744 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2745
2746 btrfs_set_balance_flags(leaf, item, bctl->flags);
2747
2748 btrfs_mark_buffer_dirty(leaf);
2749out:
2750 btrfs_free_path(path);
2751 err = btrfs_commit_transaction(trans, root);
2752 if (err && !ret)
2753 ret = err;
2754 return ret;
2755}
2756
2757static int del_balance_item(struct btrfs_root *root)
2758{
2759 struct btrfs_trans_handle *trans;
2760 struct btrfs_path *path;
2761 struct btrfs_key key;
2762 int ret, err;
2763
2764 path = btrfs_alloc_path();
2765 if (!path)
2766 return -ENOMEM;
2767
2768 trans = btrfs_start_transaction(root, 0);
2769 if (IS_ERR(trans)) {
2770 btrfs_free_path(path);
2771 return PTR_ERR(trans);
2772 }
2773
2774 key.objectid = BTRFS_BALANCE_OBJECTID;
2775 key.type = BTRFS_BALANCE_ITEM_KEY;
2776 key.offset = 0;
2777
2778 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2779 if (ret < 0)
2780 goto out;
2781 if (ret > 0) {
2782 ret = -ENOENT;
2783 goto out;
2784 }
2785
2786 ret = btrfs_del_item(trans, root, path);
2787out:
2788 btrfs_free_path(path);
2789 err = btrfs_commit_transaction(trans, root);
2790 if (err && !ret)
2791 ret = err;
2792 return ret;
2793}
2794
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002795/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002796 * This is a heuristic used to reduce the number of chunks balanced on
2797 * resume after balance was interrupted.
2798 */
2799static void update_balance_args(struct btrfs_balance_control *bctl)
2800{
2801 /*
2802 * Turn on soft mode for chunk types that were being converted.
2803 */
2804 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2805 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2806 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2807 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2808 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2809 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2810
2811 /*
2812 * Turn on usage filter if is not already used. The idea is
2813 * that chunks that we have already balanced should be
2814 * reasonably full. Don't do it for chunks that are being
2815 * converted - that will keep us from relocating unconverted
2816 * (albeit full) chunks.
2817 */
2818 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2819 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2820 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2821 bctl->data.usage = 90;
2822 }
2823 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2824 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2825 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2826 bctl->sys.usage = 90;
2827 }
2828 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2829 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2830 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2831 bctl->meta.usage = 90;
2832 }
2833}
2834
2835/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002836 * Should be called with both balance and volume mutexes held to
2837 * serialize other volume operations (add_dev/rm_dev/resize) with
2838 * restriper. Same goes for unset_balance_control.
2839 */
2840static void set_balance_control(struct btrfs_balance_control *bctl)
2841{
2842 struct btrfs_fs_info *fs_info = bctl->fs_info;
2843
2844 BUG_ON(fs_info->balance_ctl);
2845
2846 spin_lock(&fs_info->balance_lock);
2847 fs_info->balance_ctl = bctl;
2848 spin_unlock(&fs_info->balance_lock);
2849}
2850
2851static void unset_balance_control(struct btrfs_fs_info *fs_info)
2852{
2853 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2854
2855 BUG_ON(!fs_info->balance_ctl);
2856
2857 spin_lock(&fs_info->balance_lock);
2858 fs_info->balance_ctl = NULL;
2859 spin_unlock(&fs_info->balance_lock);
2860
2861 kfree(bctl);
2862}
2863
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002864/*
2865 * Balance filters. Return 1 if chunk should be filtered out
2866 * (should not be balanced).
2867 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002868static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002869 struct btrfs_balance_args *bargs)
2870{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002871 chunk_type = chunk_to_extended(chunk_type) &
2872 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002873
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002874 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002875 return 0;
2876
2877 return 1;
2878}
2879
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002880static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2881 struct btrfs_balance_args *bargs)
2882{
2883 struct btrfs_block_group_cache *cache;
2884 u64 chunk_used, user_thresh;
2885 int ret = 1;
2886
2887 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2888 chunk_used = btrfs_block_group_used(&cache->item);
2889
Ilya Dryomova105bb82013-01-21 15:15:56 +02002890 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002891 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002892 else if (bargs->usage > 100)
2893 user_thresh = cache->key.offset;
2894 else
2895 user_thresh = div_factor_fine(cache->key.offset,
2896 bargs->usage);
2897
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002898 if (chunk_used < user_thresh)
2899 ret = 0;
2900
2901 btrfs_put_block_group(cache);
2902 return ret;
2903}
2904
Ilya Dryomov409d4042012-01-16 22:04:47 +02002905static int chunk_devid_filter(struct extent_buffer *leaf,
2906 struct btrfs_chunk *chunk,
2907 struct btrfs_balance_args *bargs)
2908{
2909 struct btrfs_stripe *stripe;
2910 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2911 int i;
2912
2913 for (i = 0; i < num_stripes; i++) {
2914 stripe = btrfs_stripe_nr(chunk, i);
2915 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2916 return 0;
2917 }
2918
2919 return 1;
2920}
2921
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002922/* [pstart, pend) */
2923static int chunk_drange_filter(struct extent_buffer *leaf,
2924 struct btrfs_chunk *chunk,
2925 u64 chunk_offset,
2926 struct btrfs_balance_args *bargs)
2927{
2928 struct btrfs_stripe *stripe;
2929 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2930 u64 stripe_offset;
2931 u64 stripe_length;
2932 int factor;
2933 int i;
2934
2935 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2936 return 0;
2937
2938 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002939 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2940 factor = num_stripes / 2;
2941 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2942 factor = num_stripes - 1;
2943 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2944 factor = num_stripes - 2;
2945 } else {
2946 factor = num_stripes;
2947 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002948
2949 for (i = 0; i < num_stripes; i++) {
2950 stripe = btrfs_stripe_nr(chunk, i);
2951 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2952 continue;
2953
2954 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2955 stripe_length = btrfs_chunk_length(leaf, chunk);
2956 do_div(stripe_length, factor);
2957
2958 if (stripe_offset < bargs->pend &&
2959 stripe_offset + stripe_length > bargs->pstart)
2960 return 0;
2961 }
2962
2963 return 1;
2964}
2965
Ilya Dryomovea671762012-01-16 22:04:48 +02002966/* [vstart, vend) */
2967static int chunk_vrange_filter(struct extent_buffer *leaf,
2968 struct btrfs_chunk *chunk,
2969 u64 chunk_offset,
2970 struct btrfs_balance_args *bargs)
2971{
2972 if (chunk_offset < bargs->vend &&
2973 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2974 /* at least part of the chunk is inside this vrange */
2975 return 0;
2976
2977 return 1;
2978}
2979
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002980static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002981 struct btrfs_balance_args *bargs)
2982{
2983 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2984 return 0;
2985
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002986 chunk_type = chunk_to_extended(chunk_type) &
2987 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002988
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002989 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002990 return 1;
2991
2992 return 0;
2993}
2994
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002995static int should_balance_chunk(struct btrfs_root *root,
2996 struct extent_buffer *leaf,
2997 struct btrfs_chunk *chunk, u64 chunk_offset)
2998{
2999 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3000 struct btrfs_balance_args *bargs = NULL;
3001 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3002
3003 /* type filter */
3004 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3005 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3006 return 0;
3007 }
3008
3009 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3010 bargs = &bctl->data;
3011 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3012 bargs = &bctl->sys;
3013 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3014 bargs = &bctl->meta;
3015
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003016 /* profiles filter */
3017 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3018 chunk_profiles_filter(chunk_type, bargs)) {
3019 return 0;
3020 }
3021
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003022 /* usage filter */
3023 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3024 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3025 return 0;
3026 }
3027
Ilya Dryomov409d4042012-01-16 22:04:47 +02003028 /* devid filter */
3029 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3030 chunk_devid_filter(leaf, chunk, bargs)) {
3031 return 0;
3032 }
3033
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003034 /* drange filter, makes sense only with devid filter */
3035 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3036 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3037 return 0;
3038 }
3039
Ilya Dryomovea671762012-01-16 22:04:48 +02003040 /* vrange filter */
3041 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3042 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3043 return 0;
3044 }
3045
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003046 /* soft profile changing mode */
3047 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3048 chunk_soft_convert_filter(chunk_type, bargs)) {
3049 return 0;
3050 }
3051
David Sterba7d824b62014-05-07 17:37:51 +02003052 /*
3053 * limited by count, must be the last filter
3054 */
3055 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3056 if (bargs->limit == 0)
3057 return 0;
3058 else
3059 bargs->limit--;
3060 }
3061
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003062 return 1;
3063}
3064
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003065static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003066{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003067 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003068 struct btrfs_root *chunk_root = fs_info->chunk_root;
3069 struct btrfs_root *dev_root = fs_info->dev_root;
3070 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003071 struct btrfs_device *device;
3072 u64 old_size;
3073 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003074 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003075 struct btrfs_path *path;
3076 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003077 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003078 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003079 struct extent_buffer *leaf;
3080 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003081 int ret;
3082 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003083 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003084 u64 limit_data = bctl->data.limit;
3085 u64 limit_meta = bctl->meta.limit;
3086 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003087
Chris Masonec44a352008-04-28 15:29:52 -04003088 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003089 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003090 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04003091 old_size = device->total_bytes;
3092 size_to_free = div_factor(old_size, 1);
3093 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003094 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003095 device->total_bytes - device->bytes_used > size_to_free ||
3096 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003097 continue;
3098
3099 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003100 if (ret == -ENOSPC)
3101 break;
Chris Masonec44a352008-04-28 15:29:52 -04003102 BUG_ON(ret);
3103
Yan, Zhenga22285a2010-05-16 10:48:46 -04003104 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003105 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003106
3107 ret = btrfs_grow_device(trans, device, old_size);
3108 BUG_ON(ret);
3109
3110 btrfs_end_transaction(trans, dev_root);
3111 }
3112
3113 /* step two, relocate all the chunks */
3114 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003115 if (!path) {
3116 ret = -ENOMEM;
3117 goto error;
3118 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003119
3120 /* zero out stat counters */
3121 spin_lock(&fs_info->balance_lock);
3122 memset(&bctl->stat, 0, sizeof(bctl->stat));
3123 spin_unlock(&fs_info->balance_lock);
3124again:
David Sterba7d824b62014-05-07 17:37:51 +02003125 if (!counting) {
3126 bctl->data.limit = limit_data;
3127 bctl->meta.limit = limit_meta;
3128 bctl->sys.limit = limit_sys;
3129 }
Chris Masonec44a352008-04-28 15:29:52 -04003130 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3131 key.offset = (u64)-1;
3132 key.type = BTRFS_CHUNK_ITEM_KEY;
3133
Chris Masond3977122009-01-05 21:25:51 -05003134 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003135 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003136 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003137 ret = -ECANCELED;
3138 goto error;
3139 }
3140
Chris Masonec44a352008-04-28 15:29:52 -04003141 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3142 if (ret < 0)
3143 goto error;
3144
3145 /*
3146 * this shouldn't happen, it means the last relocate
3147 * failed
3148 */
3149 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003150 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003151
3152 ret = btrfs_previous_item(chunk_root, path, 0,
3153 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003154 if (ret) {
3155 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003156 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003157 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003158
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003159 leaf = path->nodes[0];
3160 slot = path->slots[0];
3161 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3162
Chris Masonec44a352008-04-28 15:29:52 -04003163 if (found_key.objectid != key.objectid)
3164 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003165
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003166 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3167
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003168 if (!counting) {
3169 spin_lock(&fs_info->balance_lock);
3170 bctl->stat.considered++;
3171 spin_unlock(&fs_info->balance_lock);
3172 }
3173
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003174 ret = should_balance_chunk(chunk_root, leaf, chunk,
3175 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003176 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003177 if (!ret)
3178 goto loop;
3179
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003180 if (counting) {
3181 spin_lock(&fs_info->balance_lock);
3182 bctl->stat.expected++;
3183 spin_unlock(&fs_info->balance_lock);
3184 goto loop;
3185 }
3186
Chris Masonec44a352008-04-28 15:29:52 -04003187 ret = btrfs_relocate_chunk(chunk_root,
3188 chunk_root->root_key.objectid,
3189 found_key.objectid,
3190 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003191 if (ret && ret != -ENOSPC)
3192 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003193 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003194 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003195 } else {
3196 spin_lock(&fs_info->balance_lock);
3197 bctl->stat.completed++;
3198 spin_unlock(&fs_info->balance_lock);
3199 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003200loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003201 if (found_key.offset == 0)
3202 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003203 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003204 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003205
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003206 if (counting) {
3207 btrfs_release_path(path);
3208 counting = false;
3209 goto again;
3210 }
Chris Masonec44a352008-04-28 15:29:52 -04003211error:
3212 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003213 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003214 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003215 enospc_errors);
3216 if (!ret)
3217 ret = -ENOSPC;
3218 }
3219
Chris Masonec44a352008-04-28 15:29:52 -04003220 return ret;
3221}
3222
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003223/**
3224 * alloc_profile_is_valid - see if a given profile is valid and reduced
3225 * @flags: profile to validate
3226 * @extended: if true @flags is treated as an extended profile
3227 */
3228static int alloc_profile_is_valid(u64 flags, int extended)
3229{
3230 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3231 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3232
3233 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3234
3235 /* 1) check that all other bits are zeroed */
3236 if (flags & ~mask)
3237 return 0;
3238
3239 /* 2) see if profile is reduced */
3240 if (flags == 0)
3241 return !extended; /* "0" is valid for usual profiles */
3242
3243 /* true if exactly one bit set */
3244 return (flags & (flags - 1)) == 0;
3245}
3246
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003247static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3248{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003249 /* cancel requested || normal exit path */
3250 return atomic_read(&fs_info->balance_cancel_req) ||
3251 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3252 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003253}
3254
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003255static void __cancel_balance(struct btrfs_fs_info *fs_info)
3256{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003257 int ret;
3258
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003259 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003260 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003261 if (ret)
3262 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003263
3264 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003265}
3266
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003267/*
3268 * Should be called with both balance and volume mutexes held
3269 */
3270int btrfs_balance(struct btrfs_balance_control *bctl,
3271 struct btrfs_ioctl_balance_args *bargs)
3272{
3273 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003274 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003275 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003276 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003277 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003278 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003279
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003280 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003281 atomic_read(&fs_info->balance_pause_req) ||
3282 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003283 ret = -EINVAL;
3284 goto out;
3285 }
3286
Ilya Dryomove4837f82012-03-27 17:09:17 +03003287 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3288 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3289 mixed = 1;
3290
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003291 /*
3292 * In case of mixed groups both data and meta should be picked,
3293 * and identical options should be given for both of them.
3294 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003295 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3296 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003297 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3298 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3299 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003300 btrfs_err(fs_info, "with mixed groups data and "
3301 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003302 ret = -EINVAL;
3303 goto out;
3304 }
3305 }
3306
Stefan Behrens8dabb742012-11-06 13:15:27 +01003307 num_devices = fs_info->fs_devices->num_devices;
3308 btrfs_dev_replace_lock(&fs_info->dev_replace);
3309 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3310 BUG_ON(num_devices < 1);
3311 num_devices--;
3312 }
3313 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003314 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003315 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003316 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003317 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003318 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003319 if (num_devices > 2)
3320 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3321 if (num_devices > 3)
3322 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3323 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003324 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3325 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3326 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003327 btrfs_err(fs_info, "unable to start balance with target "
3328 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003329 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003330 ret = -EINVAL;
3331 goto out;
3332 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003333 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3334 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3335 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003336 btrfs_err(fs_info,
3337 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003338 bctl->meta.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->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3343 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3344 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003345 btrfs_err(fs_info,
3346 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003347 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003348 ret = -EINVAL;
3349 goto out;
3350 }
3351
Ilya Dryomove4837f82012-03-27 17:09:17 +03003352 /* allow dup'ed data chunks only in mixed mode */
3353 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003354 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003355 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003356 ret = -EINVAL;
3357 goto out;
3358 }
3359
3360 /* allow to reduce meta or sys integrity only if force set */
3361 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003362 BTRFS_BLOCK_GROUP_RAID10 |
3363 BTRFS_BLOCK_GROUP_RAID5 |
3364 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003365 do {
3366 seq = read_seqbegin(&fs_info->profiles_lock);
3367
3368 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3369 (fs_info->avail_system_alloc_bits & allowed) &&
3370 !(bctl->sys.target & allowed)) ||
3371 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3372 (fs_info->avail_metadata_alloc_bits & allowed) &&
3373 !(bctl->meta.target & allowed))) {
3374 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003375 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003376 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003377 btrfs_err(fs_info, "balance will reduce metadata "
3378 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003379 ret = -EINVAL;
3380 goto out;
3381 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003382 }
Miao Xiede98ced2013-01-29 10:13:12 +00003383 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003384
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003385 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3386 int num_tolerated_disk_barrier_failures;
3387 u64 target = bctl->sys.target;
3388
3389 num_tolerated_disk_barrier_failures =
3390 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3391 if (num_tolerated_disk_barrier_failures > 0 &&
3392 (target &
3393 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3394 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3395 num_tolerated_disk_barrier_failures = 0;
3396 else if (num_tolerated_disk_barrier_failures > 1 &&
3397 (target &
3398 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3399 num_tolerated_disk_barrier_failures = 1;
3400
3401 fs_info->num_tolerated_disk_barrier_failures =
3402 num_tolerated_disk_barrier_failures;
3403 }
3404
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003405 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003406 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003407 goto out;
3408
Ilya Dryomov59641012012-01-16 22:04:48 +02003409 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3410 BUG_ON(ret == -EEXIST);
3411 set_balance_control(bctl);
3412 } else {
3413 BUG_ON(ret != -EEXIST);
3414 spin_lock(&fs_info->balance_lock);
3415 update_balance_args(bctl);
3416 spin_unlock(&fs_info->balance_lock);
3417 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003418
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003419 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003420 mutex_unlock(&fs_info->balance_mutex);
3421
3422 ret = __btrfs_balance(fs_info);
3423
3424 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003425 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003426
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003427 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3428 fs_info->num_tolerated_disk_barrier_failures =
3429 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3430 }
3431
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003432 if (bargs) {
3433 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003434 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003435 }
3436
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003437 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3438 balance_need_close(fs_info)) {
3439 __cancel_balance(fs_info);
3440 }
3441
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003442 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003443
3444 return ret;
3445out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003446 if (bctl->flags & BTRFS_BALANCE_RESUME)
3447 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003448 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003449 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003450 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3451 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003452 return ret;
3453}
3454
3455static int balance_kthread(void *data)
3456{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003457 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003458 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003459
3460 mutex_lock(&fs_info->volume_mutex);
3461 mutex_lock(&fs_info->balance_mutex);
3462
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003463 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003464 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003465 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003466 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003467
3468 mutex_unlock(&fs_info->balance_mutex);
3469 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003470
Ilya Dryomov59641012012-01-16 22:04:48 +02003471 return ret;
3472}
3473
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003474int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3475{
3476 struct task_struct *tsk;
3477
3478 spin_lock(&fs_info->balance_lock);
3479 if (!fs_info->balance_ctl) {
3480 spin_unlock(&fs_info->balance_lock);
3481 return 0;
3482 }
3483 spin_unlock(&fs_info->balance_lock);
3484
3485 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003486 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003487 return 0;
3488 }
3489
3490 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303491 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003492}
3493
Ilya Dryomov68310a52012-06-22 12:24:12 -06003494int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003495{
Ilya Dryomov59641012012-01-16 22:04:48 +02003496 struct btrfs_balance_control *bctl;
3497 struct btrfs_balance_item *item;
3498 struct btrfs_disk_balance_args disk_bargs;
3499 struct btrfs_path *path;
3500 struct extent_buffer *leaf;
3501 struct btrfs_key key;
3502 int ret;
3503
3504 path = btrfs_alloc_path();
3505 if (!path)
3506 return -ENOMEM;
3507
Ilya Dryomov68310a52012-06-22 12:24:12 -06003508 key.objectid = BTRFS_BALANCE_OBJECTID;
3509 key.type = BTRFS_BALANCE_ITEM_KEY;
3510 key.offset = 0;
3511
3512 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3513 if (ret < 0)
3514 goto out;
3515 if (ret > 0) { /* ret = -ENOENT; */
3516 ret = 0;
3517 goto out;
3518 }
3519
Ilya Dryomov59641012012-01-16 22:04:48 +02003520 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3521 if (!bctl) {
3522 ret = -ENOMEM;
3523 goto out;
3524 }
3525
Ilya Dryomov59641012012-01-16 22:04:48 +02003526 leaf = path->nodes[0];
3527 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3528
Ilya Dryomov68310a52012-06-22 12:24:12 -06003529 bctl->fs_info = fs_info;
3530 bctl->flags = btrfs_balance_flags(leaf, item);
3531 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003532
3533 btrfs_balance_data(leaf, item, &disk_bargs);
3534 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3535 btrfs_balance_meta(leaf, item, &disk_bargs);
3536 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3537 btrfs_balance_sys(leaf, item, &disk_bargs);
3538 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3539
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003540 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3541
Ilya Dryomov68310a52012-06-22 12:24:12 -06003542 mutex_lock(&fs_info->volume_mutex);
3543 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003544
Ilya Dryomov68310a52012-06-22 12:24:12 -06003545 set_balance_control(bctl);
3546
3547 mutex_unlock(&fs_info->balance_mutex);
3548 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003549out:
3550 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003551 return ret;
3552}
3553
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003554int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3555{
3556 int ret = 0;
3557
3558 mutex_lock(&fs_info->balance_mutex);
3559 if (!fs_info->balance_ctl) {
3560 mutex_unlock(&fs_info->balance_mutex);
3561 return -ENOTCONN;
3562 }
3563
3564 if (atomic_read(&fs_info->balance_running)) {
3565 atomic_inc(&fs_info->balance_pause_req);
3566 mutex_unlock(&fs_info->balance_mutex);
3567
3568 wait_event(fs_info->balance_wait_q,
3569 atomic_read(&fs_info->balance_running) == 0);
3570
3571 mutex_lock(&fs_info->balance_mutex);
3572 /* we are good with balance_ctl ripped off from under us */
3573 BUG_ON(atomic_read(&fs_info->balance_running));
3574 atomic_dec(&fs_info->balance_pause_req);
3575 } else {
3576 ret = -ENOTCONN;
3577 }
3578
3579 mutex_unlock(&fs_info->balance_mutex);
3580 return ret;
3581}
3582
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003583int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3584{
Ilya Dryomove649e582013-10-10 20:40:21 +03003585 if (fs_info->sb->s_flags & MS_RDONLY)
3586 return -EROFS;
3587
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003588 mutex_lock(&fs_info->balance_mutex);
3589 if (!fs_info->balance_ctl) {
3590 mutex_unlock(&fs_info->balance_mutex);
3591 return -ENOTCONN;
3592 }
3593
3594 atomic_inc(&fs_info->balance_cancel_req);
3595 /*
3596 * if we are running just wait and return, balance item is
3597 * deleted in btrfs_balance in this case
3598 */
3599 if (atomic_read(&fs_info->balance_running)) {
3600 mutex_unlock(&fs_info->balance_mutex);
3601 wait_event(fs_info->balance_wait_q,
3602 atomic_read(&fs_info->balance_running) == 0);
3603 mutex_lock(&fs_info->balance_mutex);
3604 } else {
3605 /* __cancel_balance needs volume_mutex */
3606 mutex_unlock(&fs_info->balance_mutex);
3607 mutex_lock(&fs_info->volume_mutex);
3608 mutex_lock(&fs_info->balance_mutex);
3609
3610 if (fs_info->balance_ctl)
3611 __cancel_balance(fs_info);
3612
3613 mutex_unlock(&fs_info->volume_mutex);
3614 }
3615
3616 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3617 atomic_dec(&fs_info->balance_cancel_req);
3618 mutex_unlock(&fs_info->balance_mutex);
3619 return 0;
3620}
3621
Stefan Behrens803b2f52013-08-15 17:11:21 +02003622static int btrfs_uuid_scan_kthread(void *data)
3623{
3624 struct btrfs_fs_info *fs_info = data;
3625 struct btrfs_root *root = fs_info->tree_root;
3626 struct btrfs_key key;
3627 struct btrfs_key max_key;
3628 struct btrfs_path *path = NULL;
3629 int ret = 0;
3630 struct extent_buffer *eb;
3631 int slot;
3632 struct btrfs_root_item root_item;
3633 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003634 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003635
3636 path = btrfs_alloc_path();
3637 if (!path) {
3638 ret = -ENOMEM;
3639 goto out;
3640 }
3641
3642 key.objectid = 0;
3643 key.type = BTRFS_ROOT_ITEM_KEY;
3644 key.offset = 0;
3645
3646 max_key.objectid = (u64)-1;
3647 max_key.type = BTRFS_ROOT_ITEM_KEY;
3648 max_key.offset = (u64)-1;
3649
Stefan Behrens803b2f52013-08-15 17:11:21 +02003650 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003651 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003652 if (ret) {
3653 if (ret > 0)
3654 ret = 0;
3655 break;
3656 }
3657
3658 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3659 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3660 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3661 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3662 goto skip;
3663
3664 eb = path->nodes[0];
3665 slot = path->slots[0];
3666 item_size = btrfs_item_size_nr(eb, slot);
3667 if (item_size < sizeof(root_item))
3668 goto skip;
3669
Stefan Behrens803b2f52013-08-15 17:11:21 +02003670 read_extent_buffer(eb, &root_item,
3671 btrfs_item_ptr_offset(eb, slot),
3672 (int)sizeof(root_item));
3673 if (btrfs_root_refs(&root_item) == 0)
3674 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003675
3676 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3677 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3678 if (trans)
3679 goto update_tree;
3680
3681 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003682 /*
3683 * 1 - subvol uuid item
3684 * 1 - received_subvol uuid item
3685 */
3686 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3687 if (IS_ERR(trans)) {
3688 ret = PTR_ERR(trans);
3689 break;
3690 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003691 continue;
3692 } else {
3693 goto skip;
3694 }
3695update_tree:
3696 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003697 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3698 root_item.uuid,
3699 BTRFS_UUID_KEY_SUBVOL,
3700 key.objectid);
3701 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003702 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003703 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003704 break;
3705 }
3706 }
3707
3708 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003709 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3710 root_item.received_uuid,
3711 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3712 key.objectid);
3713 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003714 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003715 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003716 break;
3717 }
3718 }
3719
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003720skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003721 if (trans) {
3722 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003723 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003724 if (ret)
3725 break;
3726 }
3727
Stefan Behrens803b2f52013-08-15 17:11:21 +02003728 btrfs_release_path(path);
3729 if (key.offset < (u64)-1) {
3730 key.offset++;
3731 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3732 key.offset = 0;
3733 key.type = BTRFS_ROOT_ITEM_KEY;
3734 } else if (key.objectid < (u64)-1) {
3735 key.offset = 0;
3736 key.type = BTRFS_ROOT_ITEM_KEY;
3737 key.objectid++;
3738 } else {
3739 break;
3740 }
3741 cond_resched();
3742 }
3743
3744out:
3745 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003746 if (trans && !IS_ERR(trans))
3747 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003748 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003749 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003750 else
3751 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003752 up(&fs_info->uuid_tree_rescan_sem);
3753 return 0;
3754}
3755
Stefan Behrens70f80172013-08-15 17:11:23 +02003756/*
3757 * Callback for btrfs_uuid_tree_iterate().
3758 * returns:
3759 * 0 check succeeded, the entry is not outdated.
3760 * < 0 if an error occured.
3761 * > 0 if the check failed, which means the caller shall remove the entry.
3762 */
3763static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3764 u8 *uuid, u8 type, u64 subid)
3765{
3766 struct btrfs_key key;
3767 int ret = 0;
3768 struct btrfs_root *subvol_root;
3769
3770 if (type != BTRFS_UUID_KEY_SUBVOL &&
3771 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3772 goto out;
3773
3774 key.objectid = subid;
3775 key.type = BTRFS_ROOT_ITEM_KEY;
3776 key.offset = (u64)-1;
3777 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3778 if (IS_ERR(subvol_root)) {
3779 ret = PTR_ERR(subvol_root);
3780 if (ret == -ENOENT)
3781 ret = 1;
3782 goto out;
3783 }
3784
3785 switch (type) {
3786 case BTRFS_UUID_KEY_SUBVOL:
3787 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3788 ret = 1;
3789 break;
3790 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3791 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3792 BTRFS_UUID_SIZE))
3793 ret = 1;
3794 break;
3795 }
3796
3797out:
3798 return ret;
3799}
3800
3801static int btrfs_uuid_rescan_kthread(void *data)
3802{
3803 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3804 int ret;
3805
3806 /*
3807 * 1st step is to iterate through the existing UUID tree and
3808 * to delete all entries that contain outdated data.
3809 * 2nd step is to add all missing entries to the UUID tree.
3810 */
3811 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3812 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003813 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003814 up(&fs_info->uuid_tree_rescan_sem);
3815 return ret;
3816 }
3817 return btrfs_uuid_scan_kthread(data);
3818}
3819
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003820int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3821{
3822 struct btrfs_trans_handle *trans;
3823 struct btrfs_root *tree_root = fs_info->tree_root;
3824 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003825 struct task_struct *task;
3826 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003827
3828 /*
3829 * 1 - root node
3830 * 1 - root item
3831 */
3832 trans = btrfs_start_transaction(tree_root, 2);
3833 if (IS_ERR(trans))
3834 return PTR_ERR(trans);
3835
3836 uuid_root = btrfs_create_tree(trans, fs_info,
3837 BTRFS_UUID_TREE_OBJECTID);
3838 if (IS_ERR(uuid_root)) {
3839 btrfs_abort_transaction(trans, tree_root,
3840 PTR_ERR(uuid_root));
3841 return PTR_ERR(uuid_root);
3842 }
3843
3844 fs_info->uuid_root = uuid_root;
3845
Stefan Behrens803b2f52013-08-15 17:11:21 +02003846 ret = btrfs_commit_transaction(trans, tree_root);
3847 if (ret)
3848 return ret;
3849
3850 down(&fs_info->uuid_tree_rescan_sem);
3851 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3852 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003853 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003854 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003855 up(&fs_info->uuid_tree_rescan_sem);
3856 return PTR_ERR(task);
3857 }
3858
3859 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003860}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003861
Stefan Behrens70f80172013-08-15 17:11:23 +02003862int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3863{
3864 struct task_struct *task;
3865
3866 down(&fs_info->uuid_tree_rescan_sem);
3867 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3868 if (IS_ERR(task)) {
3869 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003870 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003871 up(&fs_info->uuid_tree_rescan_sem);
3872 return PTR_ERR(task);
3873 }
3874
3875 return 0;
3876}
3877
Chris Mason8f18cf12008-04-25 16:53:30 -04003878/*
3879 * shrinking a device means finding all of the device extents past
3880 * the new size, and then following the back refs to the chunks.
3881 * The chunk relocation code actually frees the device extent
3882 */
3883int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3884{
3885 struct btrfs_trans_handle *trans;
3886 struct btrfs_root *root = device->dev_root;
3887 struct btrfs_dev_extent *dev_extent = NULL;
3888 struct btrfs_path *path;
3889 u64 length;
3890 u64 chunk_tree;
3891 u64 chunk_objectid;
3892 u64 chunk_offset;
3893 int ret;
3894 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003895 int failed = 0;
3896 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003897 struct extent_buffer *l;
3898 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003899 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003900 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003901 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003902 u64 diff = device->total_bytes - new_size;
3903
Stefan Behrens63a212a2012-11-05 18:29:28 +01003904 if (device->is_tgtdev_for_dev_replace)
3905 return -EINVAL;
3906
Chris Mason8f18cf12008-04-25 16:53:30 -04003907 path = btrfs_alloc_path();
3908 if (!path)
3909 return -ENOMEM;
3910
Chris Mason8f18cf12008-04-25 16:53:30 -04003911 path->reada = 2;
3912
Chris Mason7d9eb122008-07-08 14:19:17 -04003913 lock_chunks(root);
3914
Chris Mason8f18cf12008-04-25 16:53:30 -04003915 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003916 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003917 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003918 spin_lock(&root->fs_info->free_chunk_lock);
3919 root->fs_info->free_chunk_space -= diff;
3920 spin_unlock(&root->fs_info->free_chunk_lock);
3921 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003922 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003923
Josef Bacikba1bf482009-09-11 16:11:19 -04003924again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003925 key.objectid = device->devid;
3926 key.offset = (u64)-1;
3927 key.type = BTRFS_DEV_EXTENT_KEY;
3928
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003929 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003930 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3931 if (ret < 0)
3932 goto done;
3933
3934 ret = btrfs_previous_item(root, path, 0, key.type);
3935 if (ret < 0)
3936 goto done;
3937 if (ret) {
3938 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003939 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003940 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003941 }
3942
3943 l = path->nodes[0];
3944 slot = path->slots[0];
3945 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3946
Josef Bacikba1bf482009-09-11 16:11:19 -04003947 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003948 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003949 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003950 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003951
3952 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3953 length = btrfs_dev_extent_length(l, dev_extent);
3954
Josef Bacikba1bf482009-09-11 16:11:19 -04003955 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003956 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003957 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003958 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003959
3960 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3961 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3962 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003963 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003964
3965 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3966 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003967 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003968 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003969 if (ret == -ENOSPC)
3970 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003971 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003972
3973 if (failed && !retried) {
3974 failed = 0;
3975 retried = true;
3976 goto again;
3977 } else if (failed && retried) {
3978 ret = -ENOSPC;
3979 lock_chunks(root);
3980
3981 device->total_bytes = old_size;
3982 if (device->writeable)
3983 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003984 spin_lock(&root->fs_info->free_chunk_lock);
3985 root->fs_info->free_chunk_space += diff;
3986 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003987 unlock_chunks(root);
3988 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003989 }
3990
Chris Balld6397ba2009-04-27 07:29:03 -04003991 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003992 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003993 if (IS_ERR(trans)) {
3994 ret = PTR_ERR(trans);
3995 goto done;
3996 }
3997
Chris Balld6397ba2009-04-27 07:29:03 -04003998 lock_chunks(root);
3999
4000 device->disk_total_bytes = new_size;
4001 /* Now btrfs_update_device() will change the on-disk size. */
4002 ret = btrfs_update_device(trans, device);
4003 if (ret) {
4004 unlock_chunks(root);
4005 btrfs_end_transaction(trans, root);
4006 goto done;
4007 }
4008 WARN_ON(diff > old_total);
4009 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4010 unlock_chunks(root);
4011 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004012done:
4013 btrfs_free_path(path);
4014 return ret;
4015}
4016
Li Zefan125ccb02011-12-08 15:07:24 +08004017static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004018 struct btrfs_key *key,
4019 struct btrfs_chunk *chunk, int item_size)
4020{
David Sterba6c417612011-04-13 15:41:04 +02004021 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004022 struct btrfs_disk_key disk_key;
4023 u32 array_size;
4024 u8 *ptr;
4025
4026 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004027 if (array_size + item_size + sizeof(disk_key)
4028 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
Chris Mason0b86a832008-03-24 15:01:56 -04004029 return -EFBIG;
4030
4031 ptr = super_copy->sys_chunk_array + array_size;
4032 btrfs_cpu_key_to_disk(&disk_key, key);
4033 memcpy(ptr, &disk_key, sizeof(disk_key));
4034 ptr += sizeof(disk_key);
4035 memcpy(ptr, chunk, item_size);
4036 item_size += sizeof(disk_key);
4037 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4038 return 0;
4039}
4040
Miao Xieb2117a32011-01-05 10:07:28 +00004041/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004042 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004043 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004044static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004045{
Arne Jansen73c5de02011-04-12 12:07:57 +02004046 const struct btrfs_device_info *di_a = a;
4047 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004048
Arne Jansen73c5de02011-04-12 12:07:57 +02004049 if (di_a->max_avail > di_b->max_avail)
4050 return -1;
4051 if (di_a->max_avail < di_b->max_avail)
4052 return 1;
4053 if (di_a->total_avail > di_b->total_avail)
4054 return -1;
4055 if (di_a->total_avail < di_b->total_avail)
4056 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004057 return 0;
4058}
4059
Eric Sandeen48a3b632013-04-25 20:41:01 +00004060static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004061 [BTRFS_RAID_RAID10] = {
4062 .sub_stripes = 2,
4063 .dev_stripes = 1,
4064 .devs_max = 0, /* 0 == as many as possible */
4065 .devs_min = 4,
4066 .devs_increment = 2,
4067 .ncopies = 2,
4068 },
4069 [BTRFS_RAID_RAID1] = {
4070 .sub_stripes = 1,
4071 .dev_stripes = 1,
4072 .devs_max = 2,
4073 .devs_min = 2,
4074 .devs_increment = 2,
4075 .ncopies = 2,
4076 },
4077 [BTRFS_RAID_DUP] = {
4078 .sub_stripes = 1,
4079 .dev_stripes = 2,
4080 .devs_max = 1,
4081 .devs_min = 1,
4082 .devs_increment = 1,
4083 .ncopies = 2,
4084 },
4085 [BTRFS_RAID_RAID0] = {
4086 .sub_stripes = 1,
4087 .dev_stripes = 1,
4088 .devs_max = 0,
4089 .devs_min = 2,
4090 .devs_increment = 1,
4091 .ncopies = 1,
4092 },
4093 [BTRFS_RAID_SINGLE] = {
4094 .sub_stripes = 1,
4095 .dev_stripes = 1,
4096 .devs_max = 1,
4097 .devs_min = 1,
4098 .devs_increment = 1,
4099 .ncopies = 1,
4100 },
Chris Masone942f882013-02-20 14:06:05 -05004101 [BTRFS_RAID_RAID5] = {
4102 .sub_stripes = 1,
4103 .dev_stripes = 1,
4104 .devs_max = 0,
4105 .devs_min = 2,
4106 .devs_increment = 1,
4107 .ncopies = 2,
4108 },
4109 [BTRFS_RAID_RAID6] = {
4110 .sub_stripes = 1,
4111 .dev_stripes = 1,
4112 .devs_max = 0,
4113 .devs_min = 3,
4114 .devs_increment = 1,
4115 .ncopies = 3,
4116 },
Liu Bo31e50222012-11-21 14:18:10 +00004117};
4118
David Woodhouse53b381b2013-01-29 18:40:14 -05004119static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4120{
4121 /* TODO allow them to set a preferred stripe size */
4122 return 64 * 1024;
4123}
4124
4125static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4126{
David Woodhouse53b381b2013-01-29 18:40:14 -05004127 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
4128 return;
4129
Miao Xieceda0862013-04-11 10:30:16 +00004130 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004131}
4132
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004133#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4134 - sizeof(struct btrfs_item) \
4135 - sizeof(struct btrfs_chunk)) \
4136 / sizeof(struct btrfs_stripe) + 1)
4137
4138#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4139 - 2 * sizeof(struct btrfs_disk_key) \
4140 - 2 * sizeof(struct btrfs_chunk)) \
4141 / sizeof(struct btrfs_stripe) + 1)
4142
Miao Xieb2117a32011-01-05 10:07:28 +00004143static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004144 struct btrfs_root *extent_root, u64 start,
4145 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004146{
4147 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004148 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4149 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004150 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004151 struct extent_map_tree *em_tree;
4152 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004153 struct btrfs_device_info *devices_info = NULL;
4154 u64 total_avail;
4155 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004156 int data_stripes; /* number of stripes that count for
4157 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004158 int sub_stripes; /* sub_stripes info for map */
4159 int dev_stripes; /* stripes per dev */
4160 int devs_max; /* max devs to use */
4161 int devs_min; /* min devs needed */
4162 int devs_increment; /* ndevs has to be a multiple of this */
4163 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004164 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004165 u64 max_stripe_size;
4166 u64 max_chunk_size;
4167 u64 stripe_size;
4168 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004169 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004170 int ndevs;
4171 int i;
4172 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004173 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004174
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004175 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004176
Miao Xieb2117a32011-01-05 10:07:28 +00004177 if (list_empty(&fs_devices->alloc_list))
4178 return -ENOSPC;
4179
Liu Bo31e50222012-11-21 14:18:10 +00004180 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004181
Liu Bo31e50222012-11-21 14:18:10 +00004182 sub_stripes = btrfs_raid_array[index].sub_stripes;
4183 dev_stripes = btrfs_raid_array[index].dev_stripes;
4184 devs_max = btrfs_raid_array[index].devs_max;
4185 devs_min = btrfs_raid_array[index].devs_min;
4186 devs_increment = btrfs_raid_array[index].devs_increment;
4187 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004188
4189 if (type & BTRFS_BLOCK_GROUP_DATA) {
4190 max_stripe_size = 1024 * 1024 * 1024;
4191 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004192 if (!devs_max)
4193 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004194 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004195 /* for larger filesystems, use larger metadata chunks */
4196 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4197 max_stripe_size = 1024 * 1024 * 1024;
4198 else
4199 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004200 max_chunk_size = 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_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004204 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004205 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004206 if (!devs_max)
4207 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004208 } else {
David Sterba351fd352014-05-15 16:48:20 +02004209 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004210 type);
4211 BUG_ON(1);
4212 }
4213
4214 /* we don't want a chunk larger than 10% of writeable space */
4215 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4216 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004217
4218 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4219 GFP_NOFS);
4220 if (!devices_info)
4221 return -ENOMEM;
4222
Arne Jansen73c5de02011-04-12 12:07:57 +02004223 cur = fs_devices->alloc_list.next;
4224
4225 /*
4226 * in the first pass through the devices list, we gather information
4227 * about the available holes on each device.
4228 */
4229 ndevs = 0;
4230 while (cur != &fs_devices->alloc_list) {
4231 struct btrfs_device *device;
4232 u64 max_avail;
4233 u64 dev_offset;
4234
4235 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4236
4237 cur = cur->next;
4238
4239 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004240 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004241 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004242 continue;
4243 }
4244
Stefan Behrens63a212a2012-11-05 18:29:28 +01004245 if (!device->in_fs_metadata ||
4246 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004247 continue;
4248
4249 if (device->total_bytes > device->bytes_used)
4250 total_avail = device->total_bytes - device->bytes_used;
4251 else
4252 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004253
4254 /* If there is no space on this device, skip it. */
4255 if (total_avail == 0)
4256 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004257
Josef Bacik6df9a952013-06-27 13:22:46 -04004258 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004259 max_stripe_size * dev_stripes,
4260 &dev_offset, &max_avail);
4261 if (ret && ret != -ENOSPC)
4262 goto error;
4263
4264 if (ret == 0)
4265 max_avail = max_stripe_size * dev_stripes;
4266
4267 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4268 continue;
4269
Eric Sandeen063d0062013-01-31 00:55:01 +00004270 if (ndevs == fs_devices->rw_devices) {
4271 WARN(1, "%s: found more than %llu devices\n",
4272 __func__, fs_devices->rw_devices);
4273 break;
4274 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004275 devices_info[ndevs].dev_offset = dev_offset;
4276 devices_info[ndevs].max_avail = max_avail;
4277 devices_info[ndevs].total_avail = total_avail;
4278 devices_info[ndevs].dev = device;
4279 ++ndevs;
4280 }
4281
4282 /*
4283 * now sort the devices by hole size / available space
4284 */
4285 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4286 btrfs_cmp_device_info, NULL);
4287
4288 /* round down to number of usable stripes */
4289 ndevs -= ndevs % devs_increment;
4290
4291 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4292 ret = -ENOSPC;
4293 goto error;
4294 }
4295
4296 if (devs_max && ndevs > devs_max)
4297 ndevs = devs_max;
4298 /*
4299 * the primary goal is to maximize the number of stripes, so use as many
4300 * devices as possible, even if the stripes are not maximum sized.
4301 */
4302 stripe_size = devices_info[ndevs-1].max_avail;
4303 num_stripes = ndevs * dev_stripes;
4304
David Woodhouse53b381b2013-01-29 18:40:14 -05004305 /*
4306 * this will have to be fixed for RAID1 and RAID10 over
4307 * more drives
4308 */
4309 data_stripes = num_stripes / ncopies;
4310
David Woodhouse53b381b2013-01-29 18:40:14 -05004311 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4312 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4313 btrfs_super_stripesize(info->super_copy));
4314 data_stripes = num_stripes - 1;
4315 }
4316 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4317 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4318 btrfs_super_stripesize(info->super_copy));
4319 data_stripes = num_stripes - 2;
4320 }
Chris Mason86db2572013-02-20 16:23:40 -05004321
4322 /*
4323 * Use the number of data stripes to figure out how big this chunk
4324 * is really going to be in terms of logical address space,
4325 * and compare that answer with the max chunk size
4326 */
4327 if (stripe_size * data_stripes > max_chunk_size) {
4328 u64 mask = (1ULL << 24) - 1;
4329 stripe_size = max_chunk_size;
4330 do_div(stripe_size, data_stripes);
4331
4332 /* bump the answer up to a 16MB boundary */
4333 stripe_size = (stripe_size + mask) & ~mask;
4334
4335 /* but don't go higher than the limits we found
4336 * while searching for free extents
4337 */
4338 if (stripe_size > devices_info[ndevs-1].max_avail)
4339 stripe_size = devices_info[ndevs-1].max_avail;
4340 }
4341
Arne Jansen73c5de02011-04-12 12:07:57 +02004342 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004343
4344 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004345 do_div(stripe_size, raid_stripe_len);
4346 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004347
Miao Xieb2117a32011-01-05 10:07:28 +00004348 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4349 if (!map) {
4350 ret = -ENOMEM;
4351 goto error;
4352 }
4353 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004354
Arne Jansen73c5de02011-04-12 12:07:57 +02004355 for (i = 0; i < ndevs; ++i) {
4356 for (j = 0; j < dev_stripes; ++j) {
4357 int s = i * dev_stripes + j;
4358 map->stripes[s].dev = devices_info[i].dev;
4359 map->stripes[s].physical = devices_info[i].dev_offset +
4360 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004361 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004362 }
Chris Mason593060d2008-03-25 16:50:33 -04004363 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004364 map->stripe_len = raid_stripe_len;
4365 map->io_align = raid_stripe_len;
4366 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004367 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004368 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004369
David Woodhouse53b381b2013-01-29 18:40:14 -05004370 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004371
Arne Jansen73c5de02011-04-12 12:07:57 +02004372 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004373
David Sterba172ddd62011-04-21 00:48:27 +02004374 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004375 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004376 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004377 ret = -ENOMEM;
4378 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004379 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004380 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004381 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004382 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004383 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004384 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004385 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004386 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004387
Chris Mason0b86a832008-03-24 15:01:56 -04004388 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004389 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004390 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004391 if (!ret) {
4392 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4393 atomic_inc(&em->refs);
4394 }
Chris Mason890871b2009-09-02 16:24:52 -04004395 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004396 if (ret) {
4397 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004398 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004399 }
Yan Zheng2b820322008-11-17 21:11:30 -05004400
Josef Bacik04487482013-01-29 15:03:37 -05004401 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4402 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4403 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004404 if (ret)
4405 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004406
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004407 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004408 check_raid56_incompat_flag(extent_root->fs_info, type);
4409
Miao Xieb2117a32011-01-05 10:07:28 +00004410 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004411 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004412
Josef Bacik6df9a952013-06-27 13:22:46 -04004413error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004414 write_lock(&em_tree->lock);
4415 remove_extent_mapping(em_tree, em);
4416 write_unlock(&em_tree->lock);
4417
4418 /* One for our allocation */
4419 free_extent_map(em);
4420 /* One for the tree reference */
4421 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004422error:
Miao Xieb2117a32011-01-05 10:07:28 +00004423 kfree(devices_info);
4424 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004425}
4426
Josef Bacik6df9a952013-06-27 13:22:46 -04004427int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004428 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004429 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004430{
Yan Zheng2b820322008-11-17 21:11:30 -05004431 struct btrfs_key key;
4432 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4433 struct btrfs_device *device;
4434 struct btrfs_chunk *chunk;
4435 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004436 struct extent_map_tree *em_tree;
4437 struct extent_map *em;
4438 struct map_lookup *map;
4439 size_t item_size;
4440 u64 dev_offset;
4441 u64 stripe_size;
4442 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004443 int ret;
4444
Josef Bacik6df9a952013-06-27 13:22:46 -04004445 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4446 read_lock(&em_tree->lock);
4447 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4448 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004449
Josef Bacik6df9a952013-06-27 13:22:46 -04004450 if (!em) {
4451 btrfs_crit(extent_root->fs_info, "unable to find logical "
4452 "%Lu len %Lu", chunk_offset, chunk_size);
4453 return -EINVAL;
4454 }
4455
4456 if (em->start != chunk_offset || em->len != chunk_size) {
4457 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004458 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004459 chunk_size, em->start, em->len);
4460 free_extent_map(em);
4461 return -EINVAL;
4462 }
4463
4464 map = (struct map_lookup *)em->bdev;
4465 item_size = btrfs_chunk_item_size(map->num_stripes);
4466 stripe_size = em->orig_block_len;
4467
4468 chunk = kzalloc(item_size, GFP_NOFS);
4469 if (!chunk) {
4470 ret = -ENOMEM;
4471 goto out;
4472 }
4473
4474 for (i = 0; i < map->num_stripes; i++) {
4475 device = map->stripes[i].dev;
4476 dev_offset = map->stripes[i].physical;
4477
Yan Zheng2b820322008-11-17 21:11:30 -05004478 device->bytes_used += stripe_size;
4479 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004480 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004481 goto out;
4482 ret = btrfs_alloc_dev_extent(trans, device,
4483 chunk_root->root_key.objectid,
4484 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4485 chunk_offset, dev_offset,
4486 stripe_size);
4487 if (ret)
4488 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004489 }
4490
Josef Bacik2bf64752011-09-26 17:12:22 -04004491 spin_lock(&extent_root->fs_info->free_chunk_lock);
4492 extent_root->fs_info->free_chunk_space -= (stripe_size *
4493 map->num_stripes);
4494 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4495
Yan Zheng2b820322008-11-17 21:11:30 -05004496 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004497 for (i = 0; i < map->num_stripes; i++) {
4498 device = map->stripes[i].dev;
4499 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004500
4501 btrfs_set_stack_stripe_devid(stripe, device->devid);
4502 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4503 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4504 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004505 }
4506
4507 btrfs_set_stack_chunk_length(chunk, chunk_size);
4508 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4509 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4510 btrfs_set_stack_chunk_type(chunk, map->type);
4511 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4512 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4513 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4514 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4515 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4516
4517 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4518 key.type = BTRFS_CHUNK_ITEM_KEY;
4519 key.offset = chunk_offset;
4520
4521 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004522 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4523 /*
4524 * TODO: Cleanup of inserted chunk root in case of
4525 * failure.
4526 */
Li Zefan125ccb02011-12-08 15:07:24 +08004527 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004528 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004529 }
liubo1abe9b82011-03-24 11:18:59 +00004530
Josef Bacik6df9a952013-06-27 13:22:46 -04004531out:
Yan Zheng2b820322008-11-17 21:11:30 -05004532 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004533 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004534 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004535}
4536
4537/*
4538 * Chunk allocation falls into two parts. The first part does works
4539 * that make the new allocated chunk useable, but not do any operation
4540 * that modifies the chunk tree. The second part does the works that
4541 * require modifying the chunk tree. This division is important for the
4542 * bootstrap process of adding storage to a seed btrfs.
4543 */
4544int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4545 struct btrfs_root *extent_root, u64 type)
4546{
4547 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004548
Josef Bacik6df9a952013-06-27 13:22:46 -04004549 chunk_offset = find_next_chunk(extent_root->fs_info);
4550 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004551}
4552
Chris Masond3977122009-01-05 21:25:51 -05004553static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004554 struct btrfs_root *root,
4555 struct btrfs_device *device)
4556{
4557 u64 chunk_offset;
4558 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004559 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004560 struct btrfs_fs_info *fs_info = root->fs_info;
4561 struct btrfs_root *extent_root = fs_info->extent_root;
4562 int ret;
4563
Josef Bacik6df9a952013-06-27 13:22:46 -04004564 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004565 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004566 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4567 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004568 if (ret)
4569 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004570
Josef Bacik6df9a952013-06-27 13:22:46 -04004571 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004572 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004573 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4574 alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004575 if (ret) {
4576 btrfs_abort_transaction(trans, root, ret);
4577 goto out;
4578 }
Yan Zheng2b820322008-11-17 21:11:30 -05004579
4580 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004581 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004582 btrfs_abort_transaction(trans, root, ret);
David Sterba005d6422012-09-18 07:52:32 -06004583out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004584 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004585}
4586
Miao Xied20983b2014-07-03 18:22:13 +08004587static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4588{
4589 int max_errors;
4590
4591 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4592 BTRFS_BLOCK_GROUP_RAID10 |
4593 BTRFS_BLOCK_GROUP_RAID5 |
4594 BTRFS_BLOCK_GROUP_DUP)) {
4595 max_errors = 1;
4596 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4597 max_errors = 2;
4598 } else {
4599 max_errors = 0;
4600 }
4601
4602 return max_errors;
4603}
4604
Yan Zheng2b820322008-11-17 21:11:30 -05004605int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4606{
4607 struct extent_map *em;
4608 struct map_lookup *map;
4609 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4610 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004611 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004612 int i;
4613
Chris Mason890871b2009-09-02 16:24:52 -04004614 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004615 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004616 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004617 if (!em)
4618 return 1;
4619
4620 map = (struct map_lookup *)em->bdev;
4621 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004622 if (map->stripes[i].dev->missing) {
4623 miss_ndevs++;
4624 continue;
4625 }
4626
Yan Zheng2b820322008-11-17 21:11:30 -05004627 if (!map->stripes[i].dev->writeable) {
4628 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004629 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004630 }
4631 }
Miao Xied20983b2014-07-03 18:22:13 +08004632
4633 /*
4634 * If the number of missing devices is larger than max errors,
4635 * we can not write the data into that chunk successfully, so
4636 * set it readonly.
4637 */
4638 if (miss_ndevs > btrfs_chunk_max_errors(map))
4639 readonly = 1;
4640end:
Yan Zheng2b820322008-11-17 21:11:30 -05004641 free_extent_map(em);
4642 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004643}
4644
4645void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4646{
David Sterbaa8067e02011-04-21 00:34:43 +02004647 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004648}
4649
4650void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4651{
4652 struct extent_map *em;
4653
Chris Masond3977122009-01-05 21:25:51 -05004654 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004655 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004656 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4657 if (em)
4658 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004659 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004660 if (!em)
4661 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004662 /* once for us */
4663 free_extent_map(em);
4664 /* once for the tree */
4665 free_extent_map(em);
4666 }
4667}
4668
Stefan Behrens5d964052012-11-05 14:59:07 +01004669int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004670{
Stefan Behrens5d964052012-11-05 14:59:07 +01004671 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004672 struct extent_map *em;
4673 struct map_lookup *map;
4674 struct extent_map_tree *em_tree = &map_tree->map_tree;
4675 int ret;
4676
Chris Mason890871b2009-09-02 16:24:52 -04004677 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004678 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004679 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004680
Josef Bacikfb7669b2013-04-23 10:53:18 -04004681 /*
4682 * We could return errors for these cases, but that could get ugly and
4683 * we'd probably do the same thing which is just not do anything else
4684 * and exit, so return 1 so the callers don't try to use other copies.
4685 */
4686 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004687 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004688 logical+len);
4689 return 1;
4690 }
4691
4692 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004693 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004694 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004695 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004696 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004697 return 1;
4698 }
4699
Chris Masonf1885912008-04-09 16:28:12 -04004700 map = (struct map_lookup *)em->bdev;
4701 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4702 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004703 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4704 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004705 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4706 ret = 2;
4707 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4708 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004709 else
4710 ret = 1;
4711 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004712
4713 btrfs_dev_replace_lock(&fs_info->dev_replace);
4714 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4715 ret++;
4716 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4717
Chris Masonf1885912008-04-09 16:28:12 -04004718 return ret;
4719}
4720
David Woodhouse53b381b2013-01-29 18:40:14 -05004721unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4722 struct btrfs_mapping_tree *map_tree,
4723 u64 logical)
4724{
4725 struct extent_map *em;
4726 struct map_lookup *map;
4727 struct extent_map_tree *em_tree = &map_tree->map_tree;
4728 unsigned long len = root->sectorsize;
4729
4730 read_lock(&em_tree->lock);
4731 em = lookup_extent_mapping(em_tree, logical, len);
4732 read_unlock(&em_tree->lock);
4733 BUG_ON(!em);
4734
4735 BUG_ON(em->start > logical || em->start + em->len < logical);
4736 map = (struct map_lookup *)em->bdev;
4737 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4738 BTRFS_BLOCK_GROUP_RAID6)) {
4739 len = map->stripe_len * nr_data_stripes(map);
4740 }
4741 free_extent_map(em);
4742 return len;
4743}
4744
4745int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4746 u64 logical, u64 len, int mirror_num)
4747{
4748 struct extent_map *em;
4749 struct map_lookup *map;
4750 struct extent_map_tree *em_tree = &map_tree->map_tree;
4751 int ret = 0;
4752
4753 read_lock(&em_tree->lock);
4754 em = lookup_extent_mapping(em_tree, logical, len);
4755 read_unlock(&em_tree->lock);
4756 BUG_ON(!em);
4757
4758 BUG_ON(em->start > logical || em->start + em->len < logical);
4759 map = (struct map_lookup *)em->bdev;
4760 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4761 BTRFS_BLOCK_GROUP_RAID6))
4762 ret = 1;
4763 free_extent_map(em);
4764 return ret;
4765}
4766
Stefan Behrens30d98612012-11-06 14:52:18 +01004767static int find_live_mirror(struct btrfs_fs_info *fs_info,
4768 struct map_lookup *map, int first, int num,
4769 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004770{
4771 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004772 int tolerance;
4773 struct btrfs_device *srcdev;
4774
4775 if (dev_replace_is_ongoing &&
4776 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4777 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4778 srcdev = fs_info->dev_replace.srcdev;
4779 else
4780 srcdev = NULL;
4781
4782 /*
4783 * try to avoid the drive that is the source drive for a
4784 * dev-replace procedure, only choose it if no other non-missing
4785 * mirror is available
4786 */
4787 for (tolerance = 0; tolerance < 2; tolerance++) {
4788 if (map->stripes[optimal].dev->bdev &&
4789 (tolerance || map->stripes[optimal].dev != srcdev))
4790 return optimal;
4791 for (i = first; i < first + num; i++) {
4792 if (map->stripes[i].dev->bdev &&
4793 (tolerance || map->stripes[i].dev != srcdev))
4794 return i;
4795 }
Chris Masondfe25022008-05-13 13:46:40 -04004796 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004797
Chris Masondfe25022008-05-13 13:46:40 -04004798 /* we couldn't find one that doesn't fail. Just return something
4799 * and the io error handling code will clean up eventually
4800 */
4801 return optimal;
4802}
4803
David Woodhouse53b381b2013-01-29 18:40:14 -05004804static inline int parity_smaller(u64 a, u64 b)
4805{
4806 return a > b;
4807}
4808
4809/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4810static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4811{
4812 struct btrfs_bio_stripe s;
4813 int i;
4814 u64 l;
4815 int again = 1;
4816
4817 while (again) {
4818 again = 0;
4819 for (i = 0; i < bbio->num_stripes - 1; i++) {
4820 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4821 s = bbio->stripes[i];
4822 l = raid_map[i];
4823 bbio->stripes[i] = bbio->stripes[i+1];
4824 raid_map[i] = raid_map[i+1];
4825 bbio->stripes[i+1] = s;
4826 raid_map[i+1] = l;
4827 again = 1;
4828 }
4829 }
4830 }
4831}
4832
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004833static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004834 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004835 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004836 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004837{
4838 struct extent_map *em;
4839 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004840 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004841 struct extent_map_tree *em_tree = &map_tree->map_tree;
4842 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004843 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004844 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004845 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004846 u64 stripe_nr_orig;
4847 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004848 u64 stripe_len;
4849 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004850 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004851 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004852 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004853 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004854 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004855 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004856 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4857 int dev_replace_is_ongoing = 0;
4858 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004859 int patch_the_first_stripe_for_dev_replace = 0;
4860 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004861 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004862
Chris Mason890871b2009-09-02 16:24:52 -04004863 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004864 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004865 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004866
Chris Mason3b951512008-04-17 11:29:12 -04004867 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004868 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004869 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004870 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004871 }
Chris Mason0b86a832008-03-24 15:01:56 -04004872
Josef Bacik9bb91872013-04-19 23:45:33 -04004873 if (em->start > logical || em->start + em->len < logical) {
4874 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004875 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004876 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004877 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004878 return -EINVAL;
4879 }
4880
Chris Mason0b86a832008-03-24 15:01:56 -04004881 map = (struct map_lookup *)em->bdev;
4882 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004883
David Woodhouse53b381b2013-01-29 18:40:14 -05004884 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004885 stripe_nr = offset;
4886 /*
4887 * stripe_nr counts the total number of stripes we have to stride
4888 * to get to this block
4889 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004890 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004891
David Woodhouse53b381b2013-01-29 18:40:14 -05004892 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004893 BUG_ON(offset < stripe_offset);
4894
4895 /* stripe_offset is the offset of this block in its stripe*/
4896 stripe_offset = offset - stripe_offset;
4897
David Woodhouse53b381b2013-01-29 18:40:14 -05004898 /* if we're here for raid56, we need to know the stripe aligned start */
4899 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4900 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4901 raid56_full_stripe_start = offset;
4902
4903 /* allow a write of a full stripe, but make sure we don't
4904 * allow straddling of stripes
4905 */
4906 do_div(raid56_full_stripe_start, full_stripe_len);
4907 raid56_full_stripe_start *= full_stripe_len;
4908 }
4909
4910 if (rw & REQ_DISCARD) {
4911 /* we don't discard raid56 yet */
4912 if (map->type &
4913 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4914 ret = -EOPNOTSUPP;
4915 goto out;
4916 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004917 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004918 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4919 u64 max_len;
4920 /* For writes to RAID[56], allow a full stripeset across all disks.
4921 For other RAID types and for RAID[56] reads, just allow a single
4922 stripe (on a single disk). */
4923 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4924 (rw & REQ_WRITE)) {
4925 max_len = stripe_len * nr_data_stripes(map) -
4926 (offset - raid56_full_stripe_start);
4927 } else {
4928 /* we limit the length of each bio to what fits in a stripe */
4929 max_len = stripe_len - stripe_offset;
4930 }
4931 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004932 } else {
4933 *length = em->len - offset;
4934 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004935
David Woodhouse53b381b2013-01-29 18:40:14 -05004936 /* This is for when we're called from btrfs_merge_bio_hook() and all
4937 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004938 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004939 goto out;
4940
Stefan Behrens472262f2012-11-06 14:43:46 +01004941 btrfs_dev_replace_lock(dev_replace);
4942 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4943 if (!dev_replace_is_ongoing)
4944 btrfs_dev_replace_unlock(dev_replace);
4945
Stefan Behrensad6d6202012-11-06 15:06:47 +01004946 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4947 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4948 dev_replace->tgtdev != NULL) {
4949 /*
4950 * in dev-replace case, for repair case (that's the only
4951 * case where the mirror is selected explicitly when
4952 * calling btrfs_map_block), blocks left of the left cursor
4953 * can also be read from the target drive.
4954 * For REQ_GET_READ_MIRRORS, the target drive is added as
4955 * the last one to the array of stripes. For READ, it also
4956 * needs to be supported using the same mirror number.
4957 * If the requested block is not left of the left cursor,
4958 * EIO is returned. This can happen because btrfs_num_copies()
4959 * returns one more in the dev-replace case.
4960 */
4961 u64 tmp_length = *length;
4962 struct btrfs_bio *tmp_bbio = NULL;
4963 int tmp_num_stripes;
4964 u64 srcdev_devid = dev_replace->srcdev->devid;
4965 int index_srcdev = 0;
4966 int found = 0;
4967 u64 physical_of_found = 0;
4968
4969 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004970 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004971 if (ret) {
4972 WARN_ON(tmp_bbio != NULL);
4973 goto out;
4974 }
4975
4976 tmp_num_stripes = tmp_bbio->num_stripes;
4977 if (mirror_num > tmp_num_stripes) {
4978 /*
4979 * REQ_GET_READ_MIRRORS does not contain this
4980 * mirror, that means that the requested area
4981 * is not left of the left cursor
4982 */
4983 ret = -EIO;
4984 kfree(tmp_bbio);
4985 goto out;
4986 }
4987
4988 /*
4989 * process the rest of the function using the mirror_num
4990 * of the source drive. Therefore look it up first.
4991 * At the end, patch the device pointer to the one of the
4992 * target drive.
4993 */
4994 for (i = 0; i < tmp_num_stripes; i++) {
4995 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4996 /*
4997 * In case of DUP, in order to keep it
4998 * simple, only add the mirror with the
4999 * lowest physical address
5000 */
5001 if (found &&
5002 physical_of_found <=
5003 tmp_bbio->stripes[i].physical)
5004 continue;
5005 index_srcdev = i;
5006 found = 1;
5007 physical_of_found =
5008 tmp_bbio->stripes[i].physical;
5009 }
5010 }
5011
5012 if (found) {
5013 mirror_num = index_srcdev + 1;
5014 patch_the_first_stripe_for_dev_replace = 1;
5015 physical_to_patch_in_first_stripe = physical_of_found;
5016 } else {
5017 WARN_ON(1);
5018 ret = -EIO;
5019 kfree(tmp_bbio);
5020 goto out;
5021 }
5022
5023 kfree(tmp_bbio);
5024 } else if (mirror_num > map->num_stripes) {
5025 mirror_num = 0;
5026 }
5027
Chris Masonf2d8d742008-04-21 10:03:05 -04005028 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005029 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005030 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005031 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005032 do_div(stripe_nr_end, map->stripe_len);
5033 stripe_end_offset = stripe_nr_end * map->stripe_len -
5034 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005035
Li Dongyangfce3bb92011-03-24 10:24:26 +00005036 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5037 if (rw & REQ_DISCARD)
5038 num_stripes = min_t(u64, map->num_stripes,
5039 stripe_nr_end - stripe_nr_orig);
5040 stripe_index = do_div(stripe_nr, map->num_stripes);
5041 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005042 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005043 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005044 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005045 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005046 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005047 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005048 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005049 current->pid % map->num_stripes,
5050 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005051 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005052 }
Chris Mason2fff7342008-04-29 14:12:09 -04005053
Chris Mason611f0e02008-04-03 16:29:03 -04005054 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005055 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005056 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005057 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005058 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005059 } else {
5060 mirror_num = 1;
5061 }
Chris Mason2fff7342008-04-29 14:12:09 -04005062
Chris Mason321aecc2008-04-16 10:49:51 -04005063 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5064 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005065
5066 stripe_index = do_div(stripe_nr, factor);
5067 stripe_index *= map->sub_stripes;
5068
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005069 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005070 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005071 else if (rw & REQ_DISCARD)
5072 num_stripes = min_t(u64, map->sub_stripes *
5073 (stripe_nr_end - stripe_nr_orig),
5074 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005075 else if (mirror_num)
5076 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005077 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005078 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005079 stripe_index = find_live_mirror(fs_info, map,
5080 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005081 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005082 current->pid % map->sub_stripes,
5083 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005084 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005085 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005086
5087 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5088 BTRFS_BLOCK_GROUP_RAID6)) {
5089 u64 tmp;
5090
5091 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
5092 && raid_map_ret) {
5093 int i, rot;
5094
5095 /* push stripe_nr back to the start of the full stripe */
5096 stripe_nr = raid56_full_stripe_start;
5097 do_div(stripe_nr, stripe_len);
5098
5099 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5100
5101 /* RAID[56] write or recovery. Return all stripes */
5102 num_stripes = map->num_stripes;
5103 max_errors = nr_parity_stripes(map);
5104
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05305105 raid_map = kmalloc_array(num_stripes, sizeof(u64),
David Woodhouse53b381b2013-01-29 18:40:14 -05005106 GFP_NOFS);
5107 if (!raid_map) {
5108 ret = -ENOMEM;
5109 goto out;
5110 }
5111
5112 /* Work out the disk rotation on this stripe-set */
5113 tmp = stripe_nr;
5114 rot = do_div(tmp, num_stripes);
5115
5116 /* Fill in the logical address of each stripe */
5117 tmp = stripe_nr * nr_data_stripes(map);
5118 for (i = 0; i < nr_data_stripes(map); i++)
5119 raid_map[(i+rot) % num_stripes] =
5120 em->start + (tmp + i) * map->stripe_len;
5121
5122 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5123 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5124 raid_map[(i+rot+1) % num_stripes] =
5125 RAID6_Q_STRIPE;
5126
5127 *length = map->stripe_len;
5128 stripe_index = 0;
5129 stripe_offset = 0;
5130 } else {
5131 /*
5132 * Mirror #0 or #1 means the original data block.
5133 * Mirror #2 is RAID5 parity block.
5134 * Mirror #3 is RAID6 Q block.
5135 */
5136 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5137 if (mirror_num > 1)
5138 stripe_index = nr_data_stripes(map) +
5139 mirror_num - 2;
5140
5141 /* We distribute the parity blocks across stripes */
5142 tmp = stripe_nr + stripe_index;
5143 stripe_index = do_div(tmp, map->num_stripes);
5144 }
Chris Mason8790d502008-04-03 16:29:03 -04005145 } else {
5146 /*
5147 * after this do_div call, stripe_nr is the number of stripes
5148 * on this device we have to walk to find the data, and
5149 * stripe_index is the number of our device in the stripe array
5150 */
5151 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005152 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005153 }
Chris Mason593060d2008-03-25 16:50:33 -04005154 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005155
Stefan Behrens472262f2012-11-06 14:43:46 +01005156 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005157 if (dev_replace_is_ongoing) {
5158 if (rw & (REQ_WRITE | REQ_DISCARD))
5159 num_alloc_stripes <<= 1;
5160 if (rw & REQ_GET_READ_MIRRORS)
5161 num_alloc_stripes++;
5162 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005163 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08005164 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04005165 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08005166 ret = -ENOMEM;
5167 goto out;
5168 }
5169 atomic_set(&bbio->error, 0);
5170
Li Dongyangfce3bb92011-03-24 10:24:26 +00005171 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08005172 int factor = 0;
5173 int sub_stripes = 0;
5174 u64 stripes_per_dev = 0;
5175 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005176 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005177
5178 if (map->type &
5179 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5180 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5181 sub_stripes = 1;
5182 else
5183 sub_stripes = map->sub_stripes;
5184
5185 factor = map->num_stripes / sub_stripes;
5186 stripes_per_dev = div_u64_rem(stripe_nr_end -
5187 stripe_nr_orig,
5188 factor,
5189 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005190 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5191 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005192 }
5193
Li Dongyangfce3bb92011-03-24 10:24:26 +00005194 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005195 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005196 map->stripes[stripe_index].physical +
5197 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005198 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005199
Li Zefanec9ef7a2011-12-01 14:06:42 +08005200 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5201 BTRFS_BLOCK_GROUP_RAID10)) {
5202 bbio->stripes[i].length = stripes_per_dev *
5203 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005204
Li Zefanec9ef7a2011-12-01 14:06:42 +08005205 if (i / sub_stripes < remaining_stripes)
5206 bbio->stripes[i].length +=
5207 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005208
5209 /*
5210 * Special for the first stripe and
5211 * the last stripe:
5212 *
5213 * |-------|...|-------|
5214 * |----------|
5215 * off end_off
5216 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005217 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005218 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005219 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005220
5221 if (stripe_index >= last_stripe &&
5222 stripe_index <= (last_stripe +
5223 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005224 bbio->stripes[i].length -=
5225 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005226
Li Zefanec9ef7a2011-12-01 14:06:42 +08005227 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005228 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005229 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005230 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005231
5232 stripe_index++;
5233 if (stripe_index == map->num_stripes) {
5234 /* This could only happen for RAID0/10 */
5235 stripe_index = 0;
5236 stripe_nr++;
5237 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005238 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005239 } else {
5240 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005241 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005242 map->stripes[stripe_index].physical +
5243 stripe_offset +
5244 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005245 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005246 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005247 stripe_index++;
5248 }
Chris Mason593060d2008-03-25 16:50:33 -04005249 }
Li Zefande11cc12011-12-01 12:55:47 +08005250
Miao Xied20983b2014-07-03 18:22:13 +08005251 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5252 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005253
Stefan Behrens472262f2012-11-06 14:43:46 +01005254 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5255 dev_replace->tgtdev != NULL) {
5256 int index_where_to_add;
5257 u64 srcdev_devid = dev_replace->srcdev->devid;
5258
5259 /*
5260 * duplicate the write operations while the dev replace
5261 * procedure is running. Since the copying of the old disk
5262 * to the new disk takes place at run time while the
5263 * filesystem is mounted writable, the regular write
5264 * operations to the old disk have to be duplicated to go
5265 * to the new disk as well.
5266 * Note that device->missing is handled by the caller, and
5267 * that the write to the old disk is already set up in the
5268 * stripes array.
5269 */
5270 index_where_to_add = num_stripes;
5271 for (i = 0; i < num_stripes; i++) {
5272 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5273 /* write to new disk, too */
5274 struct btrfs_bio_stripe *new =
5275 bbio->stripes + index_where_to_add;
5276 struct btrfs_bio_stripe *old =
5277 bbio->stripes + i;
5278
5279 new->physical = old->physical;
5280 new->length = old->length;
5281 new->dev = dev_replace->tgtdev;
5282 index_where_to_add++;
5283 max_errors++;
5284 }
5285 }
5286 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005287 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5288 dev_replace->tgtdev != NULL) {
5289 u64 srcdev_devid = dev_replace->srcdev->devid;
5290 int index_srcdev = 0;
5291 int found = 0;
5292 u64 physical_of_found = 0;
5293
5294 /*
5295 * During the dev-replace procedure, the target drive can
5296 * also be used to read data in case it is needed to repair
5297 * a corrupt block elsewhere. This is possible if the
5298 * requested area is left of the left cursor. In this area,
5299 * the target drive is a full copy of the source drive.
5300 */
5301 for (i = 0; i < num_stripes; i++) {
5302 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5303 /*
5304 * In case of DUP, in order to keep it
5305 * simple, only add the mirror with the
5306 * lowest physical address
5307 */
5308 if (found &&
5309 physical_of_found <=
5310 bbio->stripes[i].physical)
5311 continue;
5312 index_srcdev = i;
5313 found = 1;
5314 physical_of_found = bbio->stripes[i].physical;
5315 }
5316 }
5317 if (found) {
5318 u64 length = map->stripe_len;
5319
5320 if (physical_of_found + length <=
5321 dev_replace->cursor_left) {
5322 struct btrfs_bio_stripe *tgtdev_stripe =
5323 bbio->stripes + num_stripes;
5324
5325 tgtdev_stripe->physical = physical_of_found;
5326 tgtdev_stripe->length =
5327 bbio->stripes[index_srcdev].length;
5328 tgtdev_stripe->dev = dev_replace->tgtdev;
5329
5330 num_stripes++;
5331 }
5332 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005333 }
5334
Li Zefande11cc12011-12-01 12:55:47 +08005335 *bbio_ret = bbio;
5336 bbio->num_stripes = num_stripes;
5337 bbio->max_errors = max_errors;
5338 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005339
5340 /*
5341 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5342 * mirror_num == num_stripes + 1 && dev_replace target drive is
5343 * available as a mirror
5344 */
5345 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5346 WARN_ON(num_stripes > 1);
5347 bbio->stripes[0].dev = dev_replace->tgtdev;
5348 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5349 bbio->mirror_num = map->num_stripes + 1;
5350 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005351 if (raid_map) {
5352 sort_parity_stripes(bbio, raid_map);
5353 *raid_map_ret = raid_map;
5354 }
Chris Masoncea9e442008-04-09 16:28:12 -04005355out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005356 if (dev_replace_is_ongoing)
5357 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005358 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005359 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005360}
5361
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005362int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005363 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005364 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005365{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005366 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005367 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005368}
5369
Yan Zhenga512bbf2008-12-08 16:46:26 -05005370int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5371 u64 chunk_start, u64 physical, u64 devid,
5372 u64 **logical, int *naddrs, int *stripe_len)
5373{
5374 struct extent_map_tree *em_tree = &map_tree->map_tree;
5375 struct extent_map *em;
5376 struct map_lookup *map;
5377 u64 *buf;
5378 u64 bytenr;
5379 u64 length;
5380 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005381 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005382 int i, j, nr = 0;
5383
Chris Mason890871b2009-09-02 16:24:52 -04005384 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005385 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005386 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005387
Josef Bacik835d9742013-03-19 12:13:25 -04005388 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005389 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005390 chunk_start);
5391 return -EIO;
5392 }
5393
5394 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005395 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005396 em->start, chunk_start);
5397 free_extent_map(em);
5398 return -EIO;
5399 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005400 map = (struct map_lookup *)em->bdev;
5401
5402 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005403 rmap_len = map->stripe_len;
5404
Yan Zhenga512bbf2008-12-08 16:46:26 -05005405 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5406 do_div(length, map->num_stripes / map->sub_stripes);
5407 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5408 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005409 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5410 BTRFS_BLOCK_GROUP_RAID6)) {
5411 do_div(length, nr_data_stripes(map));
5412 rmap_len = map->stripe_len * nr_data_stripes(map);
5413 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005414
5415 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005416 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005417
5418 for (i = 0; i < map->num_stripes; i++) {
5419 if (devid && map->stripes[i].dev->devid != devid)
5420 continue;
5421 if (map->stripes[i].physical > physical ||
5422 map->stripes[i].physical + length <= physical)
5423 continue;
5424
5425 stripe_nr = physical - map->stripes[i].physical;
5426 do_div(stripe_nr, map->stripe_len);
5427
5428 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5429 stripe_nr = stripe_nr * map->num_stripes + i;
5430 do_div(stripe_nr, map->sub_stripes);
5431 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5432 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005433 } /* else if RAID[56], multiply by nr_data_stripes().
5434 * Alternatively, just use rmap_len below instead of
5435 * map->stripe_len */
5436
5437 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005438 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005439 for (j = 0; j < nr; j++) {
5440 if (buf[j] == bytenr)
5441 break;
5442 }
Chris Mason934d3752008-12-08 16:43:10 -05005443 if (j == nr) {
5444 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005445 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005446 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005447 }
5448
Yan Zhenga512bbf2008-12-08 16:46:26 -05005449 *logical = buf;
5450 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005451 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005452
5453 free_extent_map(em);
5454 return 0;
5455}
5456
Miao Xie8408c712014-06-19 10:42:55 +08005457static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5458{
5459 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5460 bio_endio_nodec(bio, err);
5461 else
5462 bio_endio(bio, err);
5463 kfree(bbio);
5464}
5465
Jan Schmidta1d3c472011-08-04 17:15:33 +02005466static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005467{
Chris Mason9be33952013-05-17 18:30:14 -04005468 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005469 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005470 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005471
Stefan Behrens442a4f62012-05-25 16:06:08 +02005472 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005473 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005474 if (err == -EIO || err == -EREMOTEIO) {
5475 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005476 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005477
5478 BUG_ON(stripe_index >= bbio->num_stripes);
5479 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005480 if (dev->bdev) {
5481 if (bio->bi_rw & WRITE)
5482 btrfs_dev_stat_inc(dev,
5483 BTRFS_DEV_STAT_WRITE_ERRS);
5484 else
5485 btrfs_dev_stat_inc(dev,
5486 BTRFS_DEV_STAT_READ_ERRS);
5487 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5488 btrfs_dev_stat_inc(dev,
5489 BTRFS_DEV_STAT_FLUSH_ERRS);
5490 btrfs_dev_stat_print_on_error(dev);
5491 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005492 }
5493 }
Chris Mason8790d502008-04-03 16:29:03 -04005494
Jan Schmidta1d3c472011-08-04 17:15:33 +02005495 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005496 is_orig_bio = 1;
5497
Miao Xiec404e0d2014-01-30 16:46:55 +08005498 btrfs_bio_counter_dec(bbio->fs_info);
5499
Jan Schmidta1d3c472011-08-04 17:15:33 +02005500 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005501 if (!is_orig_bio) {
5502 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005503 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005504 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005505
Jan Schmidta1d3c472011-08-04 17:15:33 +02005506 bio->bi_private = bbio->private;
5507 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005508 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005509 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005510 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005511 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005512 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005513 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005514 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005515 /*
5516 * this bio is actually up to date, we didn't
5517 * go over the max number of errors
5518 */
5519 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005520 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005521 }
Miao Xiec55f1392014-06-19 10:42:54 +08005522
Miao Xie8408c712014-06-19 10:42:55 +08005523 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005524 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005525 bio_put(bio);
5526 }
Chris Mason8790d502008-04-03 16:29:03 -04005527}
5528
Chris Mason8b712842008-06-11 16:50:36 -04005529/*
5530 * see run_scheduled_bios for a description of why bios are collected for
5531 * async submit.
5532 *
5533 * This will add one bio to the pending list for a device and make sure
5534 * the work struct is scheduled.
5535 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005536static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5537 struct btrfs_device *device,
5538 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005539{
5540 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005541 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005542
David Woodhouse53b381b2013-01-29 18:40:14 -05005543 if (device->missing || !device->bdev) {
5544 bio_endio(bio, -EIO);
5545 return;
5546 }
5547
Chris Mason8b712842008-06-11 16:50:36 -04005548 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005549 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005550 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005551 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005552 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005553 return;
Chris Mason8b712842008-06-11 16:50:36 -04005554 }
5555
5556 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005557 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005558 * higher layers. Otherwise, the async bio makes it appear we have
5559 * made progress against dirty pages when we've really just put it
5560 * on a queue for later
5561 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005562 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005563 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005564 bio->bi_next = NULL;
5565 bio->bi_rw |= rw;
5566
5567 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005568 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005569 pending_bios = &device->pending_sync_bios;
5570 else
5571 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005572
Chris Masonffbd5172009-04-20 15:50:09 -04005573 if (pending_bios->tail)
5574 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005575
Chris Masonffbd5172009-04-20 15:50:09 -04005576 pending_bios->tail = bio;
5577 if (!pending_bios->head)
5578 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005579 if (device->running_pending)
5580 should_queue = 0;
5581
5582 spin_unlock(&device->io_lock);
5583
5584 if (should_queue)
Qu Wenruoa8c93d42014-02-28 10:46:08 +08005585 btrfs_queue_work(root->fs_info->submit_workers,
5586 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005587}
5588
Josef Bacikde1ee922012-10-19 16:50:56 -04005589static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5590 sector_t sector)
5591{
5592 struct bio_vec *prev;
5593 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005594 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005595 struct bvec_merge_data bvm = {
5596 .bi_bdev = bdev,
5597 .bi_sector = sector,
5598 .bi_rw = bio->bi_rw,
5599 };
5600
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305601 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005602 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005603
5604 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005605 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005606 return 0;
5607
5608 if (!q->merge_bvec_fn)
5609 return 1;
5610
Kent Overstreet4f024f32013-10-11 15:44:27 -07005611 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005612 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5613 return 0;
5614 return 1;
5615}
5616
5617static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5618 struct bio *bio, u64 physical, int dev_nr,
5619 int rw, int async)
5620{
5621 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5622
5623 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005624 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005625 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005626 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005627#ifdef DEBUG
5628 {
5629 struct rcu_string *name;
5630
5631 rcu_read_lock();
5632 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005633 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005634 "(%s id %llu), size=%u\n", rw,
5635 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5636 name->str, dev->devid, bio->bi_size);
5637 rcu_read_unlock();
5638 }
5639#endif
5640 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005641
5642 btrfs_bio_counter_inc_noblocked(root->fs_info);
5643
Josef Bacikde1ee922012-10-19 16:50:56 -04005644 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005645 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005646 else
5647 btrfsic_submit_bio(rw, bio);
5648}
5649
5650static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5651 struct bio *first_bio, struct btrfs_device *dev,
5652 int dev_nr, int rw, int async)
5653{
5654 struct bio_vec *bvec = first_bio->bi_io_vec;
5655 struct bio *bio;
5656 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5657 u64 physical = bbio->stripes[dev_nr].physical;
5658
5659again:
5660 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5661 if (!bio)
5662 return -ENOMEM;
5663
5664 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5665 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5666 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005667 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005668
5669 atomic_inc(&bbio->stripes_pending);
5670 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5671 rw, async);
5672 physical += len;
5673 goto again;
5674 }
5675 bvec++;
5676 }
5677
5678 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5679 return 0;
5680}
5681
5682static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5683{
5684 atomic_inc(&bbio->error);
5685 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005686 /* Shoud be the original bio. */
5687 WARN_ON(bio != bbio->orig_bio);
5688
Josef Bacikde1ee922012-10-19 16:50:56 -04005689 bio->bi_private = bbio->private;
5690 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005691 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005692 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005693
5694 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005695 }
5696}
5697
Chris Masonf1885912008-04-09 16:28:12 -04005698int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005699 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005700{
Chris Mason0b86a832008-03-24 15:01:56 -04005701 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005702 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005703 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005704 u64 length = 0;
5705 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005706 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005707 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005708 int dev_nr = 0;
5709 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005710 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005711
Kent Overstreet4f024f32013-10-11 15:44:27 -07005712 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005713 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005714
Miao Xiec404e0d2014-01-30 16:46:55 +08005715 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005716 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5717 mirror_num, &raid_map);
Miao Xiec404e0d2014-01-30 16:46:55 +08005718 if (ret) {
5719 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005720 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005721 }
Chris Masoncea9e442008-04-09 16:28:12 -04005722
Jan Schmidta1d3c472011-08-04 17:15:33 +02005723 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005724 bbio->orig_bio = first_bio;
5725 bbio->private = first_bio->bi_private;
5726 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005727 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005728 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5729
5730 if (raid_map) {
5731 /* In this case, map_length has been set to the length of
5732 a single stripe; not the whole write */
5733 if (rw & WRITE) {
Miao Xiec404e0d2014-01-30 16:46:55 +08005734 ret = raid56_parity_write(root, bio, bbio,
5735 raid_map, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005736 } else {
Miao Xiec404e0d2014-01-30 16:46:55 +08005737 ret = raid56_parity_recover(root, bio, bbio,
5738 raid_map, map_length,
5739 mirror_num);
David Woodhouse53b381b2013-01-29 18:40:14 -05005740 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005741 /*
5742 * FIXME, replace dosen't support raid56 yet, please fix
5743 * it in the future.
5744 */
5745 btrfs_bio_counter_dec(root->fs_info);
5746 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005747 }
5748
Chris Masoncea9e442008-04-09 16:28:12 -04005749 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005750 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005751 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005752 BUG();
5753 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005754
Chris Masond3977122009-01-05 21:25:51 -05005755 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005756 dev = bbio->stripes[dev_nr].dev;
5757 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5758 bbio_error(bbio, first_bio, logical);
5759 dev_nr++;
5760 continue;
5761 }
5762
5763 /*
5764 * Check and see if we're ok with this bio based on it's size
5765 * and offset with the given device.
5766 */
5767 if (!bio_size_ok(dev->bdev, first_bio,
5768 bbio->stripes[dev_nr].physical >> 9)) {
5769 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5770 dev_nr, rw, async_submit);
5771 BUG_ON(ret);
5772 dev_nr++;
5773 continue;
5774 }
5775
Jan Schmidta1d3c472011-08-04 17:15:33 +02005776 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005777 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005778 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005779 } else {
5780 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005781 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005782 }
Josef Bacik606686e2012-06-04 14:03:51 -04005783
Josef Bacikde1ee922012-10-19 16:50:56 -04005784 submit_stripe_bio(root, bbio, bio,
5785 bbio->stripes[dev_nr].physical, dev_nr, rw,
5786 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005787 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005788 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005789 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005790 return 0;
5791}
5792
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005793struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005794 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005795{
Yan Zheng2b820322008-11-17 21:11:30 -05005796 struct btrfs_device *device;
5797 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005798
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005799 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005800 while (cur_devices) {
5801 if (!fsid ||
5802 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5803 device = __find_device(&cur_devices->devices,
5804 devid, uuid);
5805 if (device)
5806 return device;
5807 }
5808 cur_devices = cur_devices->seed;
5809 }
5810 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005811}
5812
Chris Masondfe25022008-05-13 13:46:40 -04005813static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5814 u64 devid, u8 *dev_uuid)
5815{
5816 struct btrfs_device *device;
5817 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5818
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005819 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5820 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005821 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005822
5823 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005824 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005825 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005826
5827 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005828 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005829
Chris Masondfe25022008-05-13 13:46:40 -04005830 return device;
5831}
5832
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005833/**
5834 * btrfs_alloc_device - allocate struct btrfs_device
5835 * @fs_info: used only for generating a new devid, can be NULL if
5836 * devid is provided (i.e. @devid != NULL).
5837 * @devid: a pointer to devid for this device. If NULL a new devid
5838 * is generated.
5839 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5840 * is generated.
5841 *
5842 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5843 * on error. Returned struct is not linked onto any lists and can be
5844 * destroyed with kfree() right away.
5845 */
5846struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5847 const u64 *devid,
5848 const u8 *uuid)
5849{
5850 struct btrfs_device *dev;
5851 u64 tmp;
5852
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305853 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005854 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005855
5856 dev = __alloc_device();
5857 if (IS_ERR(dev))
5858 return dev;
5859
5860 if (devid)
5861 tmp = *devid;
5862 else {
5863 int ret;
5864
5865 ret = find_next_devid(fs_info, &tmp);
5866 if (ret) {
5867 kfree(dev);
5868 return ERR_PTR(ret);
5869 }
5870 }
5871 dev->devid = tmp;
5872
5873 if (uuid)
5874 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5875 else
5876 generate_random_uuid(dev->uuid);
5877
Liu Bo9e0af232014-08-15 23:36:53 +08005878 btrfs_init_work(&dev->work, btrfs_submit_helper,
5879 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005880
5881 return dev;
5882}
5883
Chris Mason0b86a832008-03-24 15:01:56 -04005884static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5885 struct extent_buffer *leaf,
5886 struct btrfs_chunk *chunk)
5887{
5888 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5889 struct map_lookup *map;
5890 struct extent_map *em;
5891 u64 logical;
5892 u64 length;
5893 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005894 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005895 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005896 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005897 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005898
Chris Masone17cade2008-04-15 15:41:47 -04005899 logical = key->offset;
5900 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005901
Chris Mason890871b2009-09-02 16:24:52 -04005902 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005903 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005904 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005905
5906 /* already mapped? */
5907 if (em && em->start <= logical && em->start + em->len > logical) {
5908 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005909 return 0;
5910 } else if (em) {
5911 free_extent_map(em);
5912 }
Chris Mason0b86a832008-03-24 15:01:56 -04005913
David Sterba172ddd62011-04-21 00:48:27 +02005914 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005915 if (!em)
5916 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005917 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5918 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005919 if (!map) {
5920 free_extent_map(em);
5921 return -ENOMEM;
5922 }
5923
Wang Shilong298a8f92014-06-19 10:42:52 +08005924 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04005925 em->bdev = (struct block_device *)map;
5926 em->start = logical;
5927 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005928 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005929 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005930 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005931
Chris Mason593060d2008-03-25 16:50:33 -04005932 map->num_stripes = num_stripes;
5933 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5934 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5935 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5936 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5937 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005938 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005939 for (i = 0; i < num_stripes; i++) {
5940 map->stripes[i].physical =
5941 btrfs_stripe_offset_nr(leaf, chunk, i);
5942 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005943 read_extent_buffer(leaf, uuid, (unsigned long)
5944 btrfs_stripe_dev_uuid_nr(chunk, i),
5945 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005946 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5947 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005948 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005949 free_extent_map(em);
5950 return -EIO;
5951 }
Chris Masondfe25022008-05-13 13:46:40 -04005952 if (!map->stripes[i].dev) {
5953 map->stripes[i].dev =
5954 add_missing_dev(root, devid, uuid);
5955 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04005956 free_extent_map(em);
5957 return -EIO;
5958 }
5959 }
5960 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005961 }
5962
Chris Mason890871b2009-09-02 16:24:52 -04005963 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005964 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005965 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005966 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005967 free_extent_map(em);
5968
5969 return 0;
5970}
5971
Jeff Mahoney143bede2012-03-01 14:56:26 +01005972static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005973 struct btrfs_dev_item *dev_item,
5974 struct btrfs_device *device)
5975{
5976 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005977
5978 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005979 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5980 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005981 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5982 device->type = btrfs_device_type(leaf, dev_item);
5983 device->io_align = btrfs_device_io_align(leaf, dev_item);
5984 device->io_width = btrfs_device_io_width(leaf, dev_item);
5985 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005986 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005987 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005988
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02005989 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005990 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005991}
5992
Yan Zheng2b820322008-11-17 21:11:30 -05005993static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5994{
5995 struct btrfs_fs_devices *fs_devices;
5996 int ret;
5997
Li Zefanb367e472011-12-07 11:38:24 +08005998 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005999
6000 fs_devices = root->fs_info->fs_devices->seed;
6001 while (fs_devices) {
6002 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6003 ret = 0;
6004 goto out;
6005 }
6006 fs_devices = fs_devices->seed;
6007 }
6008
6009 fs_devices = find_fsid(fsid);
6010 if (!fs_devices) {
6011 ret = -ENOENT;
6012 goto out;
6013 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006014
6015 fs_devices = clone_fs_devices(fs_devices);
6016 if (IS_ERR(fs_devices)) {
6017 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006018 goto out;
6019 }
6020
Christoph Hellwig97288f22008-12-02 06:36:09 -05006021 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006022 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006023 if (ret) {
6024 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006025 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006026 }
Yan Zheng2b820322008-11-17 21:11:30 -05006027
6028 if (!fs_devices->seeding) {
6029 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006030 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006031 ret = -EINVAL;
6032 goto out;
6033 }
6034
6035 fs_devices->seed = root->fs_info->fs_devices->seed;
6036 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006037out:
Yan Zheng2b820322008-11-17 21:11:30 -05006038 return ret;
6039}
6040
Chris Mason0d81ba52008-03-24 15:02:07 -04006041static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006042 struct extent_buffer *leaf,
6043 struct btrfs_dev_item *dev_item)
6044{
6045 struct btrfs_device *device;
6046 u64 devid;
6047 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006048 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006049 u8 dev_uuid[BTRFS_UUID_SIZE];
6050
Chris Mason0b86a832008-03-24 15:01:56 -04006051 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006052 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006053 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006054 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006055 BTRFS_UUID_SIZE);
6056
6057 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6058 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05006059 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006060 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006061 }
6062
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006063 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006064 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006065 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006066 return -EIO;
6067
6068 if (!device) {
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006069 btrfs_warn(root->fs_info, "devid %llu missing", devid);
Yan Zheng2b820322008-11-17 21:11:30 -05006070 device = add_missing_dev(root, devid, dev_uuid);
6071 if (!device)
6072 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05006073 } else if (!device->missing) {
6074 /*
6075 * this happens when a device that was properly setup
6076 * in the device info lists suddenly goes bad.
6077 * device->bdev is NULL, and so we have to set
6078 * device->missing to one here
6079 */
6080 root->fs_info->fs_devices->missing_devices++;
6081 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006082 }
6083 }
6084
6085 if (device->fs_devices != root->fs_info->fs_devices) {
6086 BUG_ON(device->writeable);
6087 if (device->generation !=
6088 btrfs_device_generation(leaf, dev_item))
6089 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006090 }
Chris Mason0b86a832008-03-24 15:01:56 -04006091
6092 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006093 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006094 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006095 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006096 spin_lock(&root->fs_info->free_chunk_lock);
6097 root->fs_info->free_chunk_space += device->total_bytes -
6098 device->bytes_used;
6099 spin_unlock(&root->fs_info->free_chunk_lock);
6100 }
Chris Mason0b86a832008-03-24 15:01:56 -04006101 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006102 return ret;
6103}
6104
Yan Zhenge4404d62008-12-12 10:03:26 -05006105int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006106{
David Sterba6c417612011-04-13 15:41:04 +02006107 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006108 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006109 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006110 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04006111 u8 *ptr;
6112 unsigned long sb_ptr;
6113 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006114 u32 num_stripes;
6115 u32 array_size;
6116 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006117 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04006118 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006119
Yan Zhenge4404d62008-12-12 10:03:26 -05006120 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04006121 BTRFS_SUPER_INFO_SIZE);
6122 if (!sb)
6123 return -ENOMEM;
6124 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006125 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006126 /*
6127 * The sb extent buffer is artifical and just used to read the system array.
6128 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6129 * pages up-to-date when the page is larger: extent does not cover the
6130 * whole page and consequently check_page_uptodate does not find all
6131 * the page's extents up-to-date (the hole beyond sb),
6132 * write_extent_buffer then triggers a WARN_ON.
6133 *
6134 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6135 * but sb spans only this function. Add an explicit SetPageUptodate call
6136 * to silence the warning eg. on PowerPC 64.
6137 */
6138 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006139 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006140
Chris Masona061fc82008-05-07 11:43:44 -04006141 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006142 array_size = btrfs_super_sys_array_size(super_copy);
6143
Chris Mason0b86a832008-03-24 15:01:56 -04006144 ptr = super_copy->sys_chunk_array;
6145 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
6146 cur = 0;
6147
6148 while (cur < array_size) {
6149 disk_key = (struct btrfs_disk_key *)ptr;
6150 btrfs_disk_key_to_cpu(&key, disk_key);
6151
Chris Masona061fc82008-05-07 11:43:44 -04006152 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006153 sb_ptr += len;
6154 cur += len;
6155
Chris Mason0d81ba52008-03-24 15:02:07 -04006156 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04006157 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04006158 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006159 if (ret)
6160 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006161 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6162 len = btrfs_chunk_item_size(num_stripes);
6163 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006164 ret = -EIO;
6165 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006166 }
6167 ptr += len;
6168 sb_ptr += len;
6169 cur += len;
6170 }
Chris Masona061fc82008-05-07 11:43:44 -04006171 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006172 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006173}
6174
6175int btrfs_read_chunk_tree(struct btrfs_root *root)
6176{
6177 struct btrfs_path *path;
6178 struct extent_buffer *leaf;
6179 struct btrfs_key key;
6180 struct btrfs_key found_key;
6181 int ret;
6182 int slot;
6183
6184 root = root->fs_info->chunk_root;
6185
6186 path = btrfs_alloc_path();
6187 if (!path)
6188 return -ENOMEM;
6189
Li Zefanb367e472011-12-07 11:38:24 +08006190 mutex_lock(&uuid_mutex);
6191 lock_chunks(root);
6192
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006193 /*
6194 * Read all device items, and then all the chunk items. All
6195 * device items are found before any chunk item (their object id
6196 * is smaller than the lowest possible object id for a chunk
6197 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006198 */
6199 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6200 key.offset = 0;
6201 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006202 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006203 if (ret < 0)
6204 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006205 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006206 leaf = path->nodes[0];
6207 slot = path->slots[0];
6208 if (slot >= btrfs_header_nritems(leaf)) {
6209 ret = btrfs_next_leaf(root, path);
6210 if (ret == 0)
6211 continue;
6212 if (ret < 0)
6213 goto error;
6214 break;
6215 }
6216 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006217 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6218 struct btrfs_dev_item *dev_item;
6219 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006220 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006221 ret = read_one_dev(root, leaf, dev_item);
6222 if (ret)
6223 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006224 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6225 struct btrfs_chunk *chunk;
6226 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6227 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006228 if (ret)
6229 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006230 }
6231 path->slots[0]++;
6232 }
Chris Mason0b86a832008-03-24 15:01:56 -04006233 ret = 0;
6234error:
Li Zefanb367e472011-12-07 11:38:24 +08006235 unlock_chunks(root);
6236 mutex_unlock(&uuid_mutex);
6237
Yan Zheng2b820322008-11-17 21:11:30 -05006238 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006239 return ret;
6240}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006241
Miao Xiecb517ea2013-05-15 07:48:19 +00006242void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6243{
6244 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6245 struct btrfs_device *device;
6246
Liu Bo29cc83f2014-05-11 23:14:59 +08006247 while (fs_devices) {
6248 mutex_lock(&fs_devices->device_list_mutex);
6249 list_for_each_entry(device, &fs_devices->devices, dev_list)
6250 device->dev_root = fs_info->dev_root;
6251 mutex_unlock(&fs_devices->device_list_mutex);
6252
6253 fs_devices = fs_devices->seed;
6254 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006255}
6256
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006257static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6258{
6259 int i;
6260
6261 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6262 btrfs_dev_stat_reset(dev, i);
6263}
6264
6265int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6266{
6267 struct btrfs_key key;
6268 struct btrfs_key found_key;
6269 struct btrfs_root *dev_root = fs_info->dev_root;
6270 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6271 struct extent_buffer *eb;
6272 int slot;
6273 int ret = 0;
6274 struct btrfs_device *device;
6275 struct btrfs_path *path = NULL;
6276 int i;
6277
6278 path = btrfs_alloc_path();
6279 if (!path) {
6280 ret = -ENOMEM;
6281 goto out;
6282 }
6283
6284 mutex_lock(&fs_devices->device_list_mutex);
6285 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6286 int item_size;
6287 struct btrfs_dev_stats_item *ptr;
6288
6289 key.objectid = 0;
6290 key.type = BTRFS_DEV_STATS_KEY;
6291 key.offset = device->devid;
6292 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6293 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006294 __btrfs_reset_dev_stats(device);
6295 device->dev_stats_valid = 1;
6296 btrfs_release_path(path);
6297 continue;
6298 }
6299 slot = path->slots[0];
6300 eb = path->nodes[0];
6301 btrfs_item_key_to_cpu(eb, &found_key, slot);
6302 item_size = btrfs_item_size_nr(eb, slot);
6303
6304 ptr = btrfs_item_ptr(eb, slot,
6305 struct btrfs_dev_stats_item);
6306
6307 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6308 if (item_size >= (1 + i) * sizeof(__le64))
6309 btrfs_dev_stat_set(device, i,
6310 btrfs_dev_stats_value(eb, ptr, i));
6311 else
6312 btrfs_dev_stat_reset(device, i);
6313 }
6314
6315 device->dev_stats_valid = 1;
6316 btrfs_dev_stat_print_on_load(device);
6317 btrfs_release_path(path);
6318 }
6319 mutex_unlock(&fs_devices->device_list_mutex);
6320
6321out:
6322 btrfs_free_path(path);
6323 return ret < 0 ? ret : 0;
6324}
6325
6326static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6327 struct btrfs_root *dev_root,
6328 struct btrfs_device *device)
6329{
6330 struct btrfs_path *path;
6331 struct btrfs_key key;
6332 struct extent_buffer *eb;
6333 struct btrfs_dev_stats_item *ptr;
6334 int ret;
6335 int i;
6336
6337 key.objectid = 0;
6338 key.type = BTRFS_DEV_STATS_KEY;
6339 key.offset = device->devid;
6340
6341 path = btrfs_alloc_path();
6342 BUG_ON(!path);
6343 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6344 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006345 printk_in_rcu(KERN_WARNING "BTRFS: "
6346 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006347 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006348 goto out;
6349 }
6350
6351 if (ret == 0 &&
6352 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6353 /* need to delete old one and insert a new one */
6354 ret = btrfs_del_item(trans, dev_root, path);
6355 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006356 printk_in_rcu(KERN_WARNING "BTRFS: "
6357 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006358 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006359 goto out;
6360 }
6361 ret = 1;
6362 }
6363
6364 if (ret == 1) {
6365 /* need to insert a new item */
6366 btrfs_release_path(path);
6367 ret = btrfs_insert_empty_item(trans, dev_root, path,
6368 &key, sizeof(*ptr));
6369 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006370 printk_in_rcu(KERN_WARNING "BTRFS: "
6371 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006372 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006373 goto out;
6374 }
6375 }
6376
6377 eb = path->nodes[0];
6378 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6379 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6380 btrfs_set_dev_stats_value(eb, ptr, i,
6381 btrfs_dev_stat_read(device, i));
6382 btrfs_mark_buffer_dirty(eb);
6383
6384out:
6385 btrfs_free_path(path);
6386 return ret;
6387}
6388
6389/*
6390 * called from commit_transaction. Writes all changed device stats to disk.
6391 */
6392int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6393 struct btrfs_fs_info *fs_info)
6394{
6395 struct btrfs_root *dev_root = fs_info->dev_root;
6396 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6397 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006398 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006399 int ret = 0;
6400
6401 mutex_lock(&fs_devices->device_list_mutex);
6402 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006403 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006404 continue;
6405
Miao Xieaddc3fa2014-07-24 11:37:11 +08006406 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006407 ret = update_dev_stat_item(trans, dev_root, device);
6408 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006409 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006410 }
6411 mutex_unlock(&fs_devices->device_list_mutex);
6412
6413 return ret;
6414}
6415
Stefan Behrens442a4f62012-05-25 16:06:08 +02006416void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6417{
6418 btrfs_dev_stat_inc(dev, index);
6419 btrfs_dev_stat_print_on_error(dev);
6420}
6421
Eric Sandeen48a3b632013-04-25 20:41:01 +00006422static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006423{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006424 if (!dev->dev_stats_valid)
6425 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006426 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6427 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006428 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006429 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6430 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6431 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006432 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6433 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006434}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006435
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006436static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6437{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006438 int i;
6439
6440 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6441 if (btrfs_dev_stat_read(dev, i) != 0)
6442 break;
6443 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6444 return; /* all values == 0, suppress message */
6445
Frank Holtonefe120a2013-12-20 11:37:06 -05006446 printk_in_rcu(KERN_INFO "BTRFS: "
6447 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006448 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006449 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6450 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6451 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6452 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6453 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6454}
6455
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006456int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006457 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006458{
6459 struct btrfs_device *dev;
6460 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6461 int i;
6462
6463 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006464 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006465 mutex_unlock(&fs_devices->device_list_mutex);
6466
6467 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006468 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006469 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006470 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006471 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006472 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006473 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006474 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6475 if (stats->nr_items > i)
6476 stats->values[i] =
6477 btrfs_dev_stat_read_and_reset(dev, i);
6478 else
6479 btrfs_dev_stat_reset(dev, i);
6480 }
6481 } else {
6482 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6483 if (stats->nr_items > i)
6484 stats->values[i] = btrfs_dev_stat_read(dev, i);
6485 }
6486 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6487 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6488 return 0;
6489}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006490
6491int btrfs_scratch_superblock(struct btrfs_device *device)
6492{
6493 struct buffer_head *bh;
6494 struct btrfs_super_block *disk_super;
6495
6496 bh = btrfs_read_dev_super(device->bdev);
6497 if (!bh)
6498 return -EINVAL;
6499 disk_super = (struct btrfs_super_block *)bh->b_data;
6500
6501 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6502 set_buffer_dirty(bh);
6503 sync_dirty_buffer(bh);
6504 brelse(bh);
6505
6506 return 0;
6507}