blob: e9676a42430946aa5f6cec00cf63912140021353 [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020026#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020027#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050028#include <linux/raid/pq.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020029#include <linux/semaphore.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050030#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "ctree.h"
32#include "extent_map.h"
33#include "disk-io.h"
34#include "transaction.h"
35#include "print-tree.h"
36#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050037#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040038#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010039#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040040#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060041#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010042#include "dev-replace.h"
Anand Jain99994cd2014-06-03 11:36:00 +080043#include "sysfs.h"
Chris Mason0b86a832008-03-24 15:01:56 -040044
Yan Zheng2b820322008-11-17 21:11:30 -050045static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020049static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +000050static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020051static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050052
Chris Mason8a4b83c2008-03-24 15:02:07 -040053static DEFINE_MUTEX(uuid_mutex);
54static LIST_HEAD(fs_uuids);
55
Chris Mason7d9eb122008-07-08 14:19:17 -040056static void lock_chunks(struct btrfs_root *root)
57{
Chris Mason7d9eb122008-07-08 14:19:17 -040058 mutex_lock(&root->fs_info->chunk_mutex);
59}
60
61static void unlock_chunks(struct btrfs_root *root)
62{
Chris Mason7d9eb122008-07-08 14:19:17 -040063 mutex_unlock(&root->fs_info->chunk_mutex);
64}
65
Ilya Dryomov2208a372013-08-12 14:33:03 +030066static struct btrfs_fs_devices *__alloc_fs_devices(void)
67{
68 struct btrfs_fs_devices *fs_devs;
69
70 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
71 if (!fs_devs)
72 return ERR_PTR(-ENOMEM);
73
74 mutex_init(&fs_devs->device_list_mutex);
75
76 INIT_LIST_HEAD(&fs_devs->devices);
77 INIT_LIST_HEAD(&fs_devs->alloc_list);
78 INIT_LIST_HEAD(&fs_devs->list);
79
80 return fs_devs;
81}
82
83/**
84 * alloc_fs_devices - allocate struct btrfs_fs_devices
85 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
86 * generated.
87 *
88 * Return: a pointer to a new &struct btrfs_fs_devices on success;
89 * ERR_PTR() on error. Returned struct is not linked onto any lists and
90 * can be destroyed with kfree() right away.
91 */
92static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
93{
94 struct btrfs_fs_devices *fs_devs;
95
96 fs_devs = __alloc_fs_devices();
97 if (IS_ERR(fs_devs))
98 return fs_devs;
99
100 if (fsid)
101 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
102 else
103 generate_random_uuid(fs_devs->fsid);
104
105 return fs_devs;
106}
107
Yan Zhenge4404d62008-12-12 10:03:26 -0500108static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
109{
110 struct btrfs_device *device;
111 WARN_ON(fs_devices->opened);
112 while (!list_empty(&fs_devices->devices)) {
113 device = list_entry(fs_devices->devices.next,
114 struct btrfs_device, dev_list);
115 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400116 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500117 kfree(device);
118 }
119 kfree(fs_devices);
120}
121
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000122static void btrfs_kobject_uevent(struct block_device *bdev,
123 enum kobject_action action)
124{
125 int ret;
126
127 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
128 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500129 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000130 action,
131 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
132 &disk_to_dev(bdev->bd_disk)->kobj);
133}
134
Jeff Mahoney143bede2012-03-01 14:56:26 +0100135void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400136{
137 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400138
Yan Zheng2b820322008-11-17 21:11:30 -0500139 while (!list_empty(&fs_uuids)) {
140 fs_devices = list_entry(fs_uuids.next,
141 struct btrfs_fs_devices, list);
142 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500143 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400144 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400145}
146
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300147static struct btrfs_device *__alloc_device(void)
148{
149 struct btrfs_device *dev;
150
151 dev = kzalloc(sizeof(*dev), GFP_NOFS);
152 if (!dev)
153 return ERR_PTR(-ENOMEM);
154
155 INIT_LIST_HEAD(&dev->dev_list);
156 INIT_LIST_HEAD(&dev->dev_alloc_list);
157
158 spin_lock_init(&dev->io_lock);
159
160 spin_lock_init(&dev->reada_lock);
161 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800162 atomic_set(&dev->dev_stats_ccnt, 0);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300163 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
164 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
165
166 return dev;
167}
168
Chris Masona1b32a52008-09-05 16:09:51 -0400169static noinline struct btrfs_device *__find_device(struct list_head *head,
170 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400171{
172 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400173
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500174 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400175 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400176 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400177 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400178 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400179 }
180 return NULL;
181}
182
Chris Masona1b32a52008-09-05 16:09:51 -0400183static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400184{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400185 struct btrfs_fs_devices *fs_devices;
186
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500187 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400188 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
189 return fs_devices;
190 }
191 return NULL;
192}
193
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100194static int
195btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
196 int flush, struct block_device **bdev,
197 struct buffer_head **bh)
198{
199 int ret;
200
201 *bdev = blkdev_get_by_path(device_path, flags, holder);
202
203 if (IS_ERR(*bdev)) {
204 ret = PTR_ERR(*bdev);
Frank Holtonefe120a2013-12-20 11:37:06 -0500205 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100206 goto error;
207 }
208
209 if (flush)
210 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
211 ret = set_blocksize(*bdev, 4096);
212 if (ret) {
213 blkdev_put(*bdev, flags);
214 goto error;
215 }
216 invalidate_bdev(*bdev);
217 *bh = btrfs_read_dev_super(*bdev);
218 if (!*bh) {
219 ret = -EINVAL;
220 blkdev_put(*bdev, flags);
221 goto error;
222 }
223
224 return 0;
225
226error:
227 *bdev = NULL;
228 *bh = NULL;
229 return ret;
230}
231
Chris Masonffbd5172009-04-20 15:50:09 -0400232static void requeue_list(struct btrfs_pending_bios *pending_bios,
233 struct bio *head, struct bio *tail)
234{
235
236 struct bio *old_head;
237
238 old_head = pending_bios->head;
239 pending_bios->head = head;
240 if (pending_bios->tail)
241 tail->bi_next = old_head;
242 else
243 pending_bios->tail = tail;
244}
245
Chris Mason8b712842008-06-11 16:50:36 -0400246/*
247 * we try to collect pending bios for a device so we don't get a large
248 * number of procs sending bios down to the same device. This greatly
249 * improves the schedulers ability to collect and merge the bios.
250 *
251 * But, it also turns into a long list of bios to process and that is sure
252 * to eventually make the worker thread block. The solution here is to
253 * make some progress and then put this work struct back at the end of
254 * the list if the block device is congested. This way, multiple devices
255 * can make progress from a single worker thread.
256 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100257static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400258{
259 struct bio *pending;
260 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400261 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400262 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400263 struct bio *tail;
264 struct bio *cur;
265 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400266 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400267 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400268 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400269 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400270 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000271 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400272 struct blk_plug plug;
273
274 /*
275 * this function runs all the bios we've collected for
276 * a particular device. We don't want to wander off to
277 * another device without first sending all of these down.
278 * So, setup a plug here and finish it off before we return
279 */
280 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400281
Chris Masonbedf7622009-04-03 10:32:58 -0400282 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400283 fs_info = device->dev_root->fs_info;
284 limit = btrfs_async_submit_limit(fs_info);
285 limit = limit * 2 / 3;
286
Chris Mason8b712842008-06-11 16:50:36 -0400287loop:
288 spin_lock(&device->io_lock);
289
Chris Masona6837052009-02-04 09:19:41 -0500290loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400291 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400292
Chris Mason8b712842008-06-11 16:50:36 -0400293 /* take all the bios off the list at once and process them
294 * later on (without the lock held). But, remember the
295 * tail and other pointers so the bios can be properly reinserted
296 * into the list if we hit congestion
297 */
Chris Masond84275c2009-06-09 15:39:08 -0400298 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400299 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400300 force_reg = 1;
301 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400302 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400303 force_reg = 0;
304 }
Chris Masonffbd5172009-04-20 15:50:09 -0400305
306 pending = pending_bios->head;
307 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400308 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400309
310 /*
311 * if pending was null this time around, no bios need processing
312 * at all and we can stop. Otherwise it'll loop back up again
313 * and do an additional check so no bios are missed.
314 *
315 * device->running_pending is used to synchronize with the
316 * schedule_bio code.
317 */
Chris Masonffbd5172009-04-20 15:50:09 -0400318 if (device->pending_sync_bios.head == NULL &&
319 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400320 again = 0;
321 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400322 } else {
323 again = 1;
324 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400325 }
Chris Masonffbd5172009-04-20 15:50:09 -0400326
327 pending_bios->head = NULL;
328 pending_bios->tail = NULL;
329
Chris Mason8b712842008-06-11 16:50:36 -0400330 spin_unlock(&device->io_lock);
331
Chris Masond3977122009-01-05 21:25:51 -0500332 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400333
334 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400335 /* we want to work on both lists, but do more bios on the
336 * sync list than the regular list
337 */
338 if ((num_run > 32 &&
339 pending_bios != &device->pending_sync_bios &&
340 device->pending_sync_bios.head) ||
341 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
342 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400343 spin_lock(&device->io_lock);
344 requeue_list(pending_bios, pending, tail);
345 goto loop_lock;
346 }
347
Chris Mason8b712842008-06-11 16:50:36 -0400348 cur = pending;
349 pending = pending->bi_next;
350 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400351
Josef Bacik66657b32012-08-01 15:36:24 -0400352 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400353 waitqueue_active(&fs_info->async_submit_wait))
354 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400355
356 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400357
Chris Mason2ab1ba62011-08-04 14:28:36 -0400358 /*
359 * if we're doing the sync list, record that our
360 * plug has some sync requests on it
361 *
362 * If we're doing the regular list and there are
363 * sync requests sitting around, unplug before
364 * we add more
365 */
366 if (pending_bios == &device->pending_sync_bios) {
367 sync_pending = 1;
368 } else if (sync_pending) {
369 blk_finish_plug(&plug);
370 blk_start_plug(&plug);
371 sync_pending = 0;
372 }
373
Stefan Behrens21adbd52011-11-09 13:44:05 +0100374 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400375 num_run++;
376 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100377 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400378 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400379
380 /*
381 * we made progress, there is more work to do and the bdi
382 * is now congested. Back off and let other work structs
383 * run instead
384 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400385 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500386 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400387 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400388
Chris Masonb765ead2009-04-03 10:27:10 -0400389 ioc = current->io_context;
390
391 /*
392 * the main goal here is that we don't want to
393 * block if we're going to be able to submit
394 * more requests without blocking.
395 *
396 * This code does two great things, it pokes into
397 * the elevator code from a filesystem _and_
398 * it makes assumptions about how batching works.
399 */
400 if (ioc && ioc->nr_batch_requests > 0 &&
401 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
402 (last_waited == 0 ||
403 ioc->last_waited == last_waited)) {
404 /*
405 * we want to go through our batch of
406 * requests and stop. So, we copy out
407 * the ioc->last_waited time and test
408 * against it before looping
409 */
410 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100411 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400412 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400413 continue;
414 }
Chris Mason8b712842008-06-11 16:50:36 -0400415 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400416 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500417 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400418
419 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d42014-02-28 10:46:08 +0800420 btrfs_queue_work(fs_info->submit_workers,
421 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400422 goto done;
423 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500424 /* unplug every 64 requests just for good measure */
425 if (batch_run % 64 == 0) {
426 blk_finish_plug(&plug);
427 blk_start_plug(&plug);
428 sync_pending = 0;
429 }
Chris Mason8b712842008-06-11 16:50:36 -0400430 }
Chris Masonffbd5172009-04-20 15:50:09 -0400431
Chris Mason51684082010-03-10 15:33:32 -0500432 cond_resched();
433 if (again)
434 goto loop;
435
436 spin_lock(&device->io_lock);
437 if (device->pending_bios.head || device->pending_sync_bios.head)
438 goto loop_lock;
439 spin_unlock(&device->io_lock);
440
Chris Mason8b712842008-06-11 16:50:36 -0400441done:
Chris Mason211588a2011-04-19 20:12:40 -0400442 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400443}
444
Christoph Hellwigb2950862008-12-02 09:54:17 -0500445static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400446{
447 struct btrfs_device *device;
448
449 device = container_of(work, struct btrfs_device, work);
450 run_scheduled_bios(device);
451}
452
David Sterba60999ca2014-03-26 18:26:36 +0100453/*
454 * Add new device to list of registered devices
455 *
456 * Returns:
457 * 1 - first time device is seen
458 * 0 - device already known
459 * < 0 - error
460 */
Chris Masona1b32a52008-09-05 16:09:51 -0400461static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400462 struct btrfs_super_block *disk_super,
463 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
464{
465 struct btrfs_device *device;
466 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400467 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100468 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400469 u64 found_transid = btrfs_super_generation(disk_super);
470
471 fs_devices = find_fsid(disk_super->fsid);
472 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300473 fs_devices = alloc_fs_devices(disk_super->fsid);
474 if (IS_ERR(fs_devices))
475 return PTR_ERR(fs_devices);
476
Chris Mason8a4b83c2008-03-24 15:02:07 -0400477 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300478
Chris Mason8a4b83c2008-03-24 15:02:07 -0400479 device = NULL;
480 } else {
Chris Masona4437552008-04-18 10:29:38 -0400481 device = __find_device(&fs_devices->devices, devid,
482 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400483 }
Miao Xie443f24f2014-07-24 11:37:15 +0800484
Chris Mason8a4b83c2008-03-24 15:02:07 -0400485 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500486 if (fs_devices->opened)
487 return -EBUSY;
488
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300489 device = btrfs_alloc_device(NULL, &devid,
490 disk_super->dev_item.uuid);
491 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400492 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300493 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400494 }
Josef Bacik606686e2012-06-04 14:03:51 -0400495
496 name = rcu_string_strdup(path, GFP_NOFS);
497 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400498 kfree(device);
499 return -ENOMEM;
500 }
Josef Bacik606686e2012-06-04 14:03:51 -0400501 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200502
Chris Masone5e9a522009-06-10 15:17:02 -0400503 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000504 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100505 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400506 mutex_unlock(&fs_devices->device_list_mutex);
507
David Sterba60999ca2014-03-26 18:26:36 +0100508 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500509 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400510 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800511 /*
512 * When FS is already mounted.
513 * 1. If you are here and if the device->name is NULL that
514 * means this device was missing at time of FS mount.
515 * 2. If you are here and if the device->name is different
516 * from 'path' that means either
517 * a. The same device disappeared and reappeared with
518 * different name. or
519 * b. The missing-disk-which-was-replaced, has
520 * reappeared now.
521 *
522 * We must allow 1 and 2a above. But 2b would be a spurious
523 * and unintentional.
524 *
525 * Further in case of 1 and 2a above, the disk at 'path'
526 * would have missed some transaction when it was away and
527 * in case of 2a the stale bdev has to be updated as well.
528 * 2b must not be allowed at all time.
529 */
530
531 /*
532 * As of now don't allow update to btrfs_fs_device through
533 * the btrfs dev scan cli, after FS has been mounted.
534 */
Anand Jain77bdae42014-07-03 18:22:06 +0800535 if (fs_devices->opened) {
Anand Jainb96de002014-07-03 18:22:05 +0800536 return -EBUSY;
Anand Jain77bdae42014-07-03 18:22:06 +0800537 } else {
538 /*
539 * That is if the FS is _not_ mounted and if you
540 * are here, that means there is more than one
541 * disk with same uuid and devid.We keep the one
542 * with larger generation number or the last-in if
543 * generation are equal.
544 */
545 if (found_transid < device->generation)
546 return -EEXIST;
547 }
Anand Jainb96de002014-07-03 18:22:05 +0800548
Josef Bacik606686e2012-06-04 14:03:51 -0400549 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000550 if (!name)
551 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400552 rcu_string_free(device->name);
553 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500554 if (device->missing) {
555 fs_devices->missing_devices--;
556 device->missing = 0;
557 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400558 }
559
Anand Jain77bdae42014-07-03 18:22:06 +0800560 /*
561 * Unmount does not free the btrfs_device struct but would zero
562 * generation along with most of the other members. So just update
563 * it back. We need it to pick the disk with largest generation
564 * (as above).
565 */
566 if (!fs_devices->opened)
567 device->generation = found_transid;
568
Chris Mason8a4b83c2008-03-24 15:02:07 -0400569 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100570
571 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400572}
573
Yan Zhenge4404d62008-12-12 10:03:26 -0500574static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
575{
576 struct btrfs_fs_devices *fs_devices;
577 struct btrfs_device *device;
578 struct btrfs_device *orig_dev;
579
Ilya Dryomov2208a372013-08-12 14:33:03 +0300580 fs_devices = alloc_fs_devices(orig->fsid);
581 if (IS_ERR(fs_devices))
582 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500583
Josef Bacik02db0842012-06-21 16:03:58 -0400584 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500585
Xiao Guangrong46224702011-04-20 10:08:47 +0000586 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500587 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400588 struct rcu_string *name;
589
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300590 device = btrfs_alloc_device(NULL, &orig_dev->devid,
591 orig_dev->uuid);
592 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500593 goto error;
594
Josef Bacik606686e2012-06-04 14:03:51 -0400595 /*
596 * This is ok to do without rcu read locked because we hold the
597 * uuid mutex so nothing we touch in here is going to disappear.
598 */
Anand Jaine755f782014-06-30 17:12:47 +0800599 if (orig_dev->name) {
600 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
601 if (!name) {
602 kfree(device);
603 goto error;
604 }
605 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400606 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500607
Yan Zhenge4404d62008-12-12 10:03:26 -0500608 list_add(&device->dev_list, &fs_devices->devices);
609 device->fs_devices = fs_devices;
610 fs_devices->num_devices++;
611 }
612 return fs_devices;
613error:
614 free_fs_devices(fs_devices);
615 return ERR_PTR(-ENOMEM);
616}
617
Stefan Behrens8dabb742012-11-06 13:15:27 +0100618void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
619 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400620{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500621 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800622 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500623
Chris Masondfe25022008-05-13 13:46:40 -0400624 mutex_lock(&uuid_mutex);
625again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000626 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500627 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500628 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100629 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800630 (!latest_dev ||
631 device->generation > latest_dev->generation)) {
632 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500633 }
Yan Zheng2b820322008-11-17 21:11:30 -0500634 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500635 }
Yan Zheng2b820322008-11-17 21:11:30 -0500636
Stefan Behrens8dabb742012-11-06 13:15:27 +0100637 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
638 /*
639 * In the first step, keep the device which has
640 * the correct fsid and the devid that is used
641 * for the dev_replace procedure.
642 * In the second step, the dev_replace state is
643 * read from the device tree and it is known
644 * whether the procedure is really active or
645 * not, which means whether this device is
646 * used or whether it should be removed.
647 */
648 if (step == 0 || device->is_tgtdev_for_dev_replace) {
649 continue;
650 }
651 }
Yan Zheng2b820322008-11-17 21:11:30 -0500652 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100653 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500654 device->bdev = NULL;
655 fs_devices->open_devices--;
656 }
657 if (device->writeable) {
658 list_del_init(&device->dev_alloc_list);
659 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100660 if (!device->is_tgtdev_for_dev_replace)
661 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500662 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500663 list_del_init(&device->dev_list);
664 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400665 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500666 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400667 }
Yan Zheng2b820322008-11-17 21:11:30 -0500668
669 if (fs_devices->seed) {
670 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500671 goto again;
672 }
673
Miao Xie443f24f2014-07-24 11:37:15 +0800674 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500675
Chris Masondfe25022008-05-13 13:46:40 -0400676 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400677}
Chris Masona0af4692008-05-13 16:03:06 -0400678
Xiao Guangrong1f781602011-04-20 10:09:16 +0000679static void __free_device(struct work_struct *work)
680{
681 struct btrfs_device *device;
682
683 device = container_of(work, struct btrfs_device, rcu_work);
684
685 if (device->bdev)
686 blkdev_put(device->bdev, device->mode);
687
Josef Bacik606686e2012-06-04 14:03:51 -0400688 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000689 kfree(device);
690}
691
692static void free_device(struct rcu_head *head)
693{
694 struct btrfs_device *device;
695
696 device = container_of(head, struct btrfs_device, rcu);
697
698 INIT_WORK(&device->rcu_work, __free_device);
699 schedule_work(&device->rcu_work);
700}
701
Yan Zheng2b820322008-11-17 21:11:30 -0500702static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400703{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400704 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500705
Yan Zheng2b820322008-11-17 21:11:30 -0500706 if (--fs_devices->opened > 0)
707 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400708
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000709 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500710 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000711 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400712 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000713
714 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400715 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000716
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300717 if (device->writeable &&
718 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500719 list_del_init(&device->dev_alloc_list);
720 fs_devices->rw_devices--;
721 }
722
Josef Bacikd5e20032011-08-04 14:52:27 +0000723 if (device->can_discard)
724 fs_devices->num_can_discard--;
Josef Bacik726551e2013-08-26 13:45:53 -0400725 if (device->missing)
726 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000727
Ilya Dryomova1e87802013-08-12 14:33:04 +0300728 new_device = btrfs_alloc_device(NULL, &device->devid,
729 device->uuid);
730 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400731
732 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400733 if (device->name) {
734 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300735 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400736 rcu_assign_pointer(new_device->name, name);
737 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300738
Xiao Guangrong1f781602011-04-20 10:09:16 +0000739 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300740 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000741
742 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400743 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000744 mutex_unlock(&fs_devices->device_list_mutex);
745
Yan Zhenge4404d62008-12-12 10:03:26 -0500746 WARN_ON(fs_devices->open_devices);
747 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500748 fs_devices->opened = 0;
749 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500750
Chris Mason8a4b83c2008-03-24 15:02:07 -0400751 return 0;
752}
753
Yan Zheng2b820322008-11-17 21:11:30 -0500754int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
755{
Yan Zhenge4404d62008-12-12 10:03:26 -0500756 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500757 int ret;
758
759 mutex_lock(&uuid_mutex);
760 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500761 if (!fs_devices->opened) {
762 seed_devices = fs_devices->seed;
763 fs_devices->seed = NULL;
764 }
Yan Zheng2b820322008-11-17 21:11:30 -0500765 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500766
767 while (seed_devices) {
768 fs_devices = seed_devices;
769 seed_devices = fs_devices->seed;
770 __btrfs_close_devices(fs_devices);
771 free_fs_devices(fs_devices);
772 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000773 /*
774 * Wait for rcu kworkers under __btrfs_close_devices
775 * to finish all blkdev_puts so device is really
776 * free when umount is done.
777 */
778 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500779 return ret;
780}
781
Yan Zhenge4404d62008-12-12 10:03:26 -0500782static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
783 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400784{
Josef Bacikd5e20032011-08-04 14:52:27 +0000785 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400786 struct block_device *bdev;
787 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400788 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800789 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400790 struct buffer_head *bh;
791 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400792 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500793 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400794 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400795
Tejun Heod4d77622010-11-13 11:55:18 +0100796 flags |= FMODE_EXCL;
797
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500798 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400799 if (device->bdev)
800 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400801 if (!device->name)
802 continue;
803
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000804 /* Just open everything we can; ignore failures here */
805 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
806 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100807 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400808
809 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000810 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400811 if (devid != device->devid)
812 goto error_brelse;
813
Yan Zheng2b820322008-11-17 21:11:30 -0500814 if (memcmp(device->uuid, disk_super->dev_item.uuid,
815 BTRFS_UUID_SIZE))
816 goto error_brelse;
817
818 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800819 if (!latest_dev ||
820 device->generation > latest_dev->generation)
821 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400822
Yan Zheng2b820322008-11-17 21:11:30 -0500823 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
824 device->writeable = 0;
825 } else {
826 device->writeable = !bdev_read_only(bdev);
827 seeding = 0;
828 }
829
Josef Bacikd5e20032011-08-04 14:52:27 +0000830 q = bdev_get_queue(bdev);
831 if (blk_queue_discard(q)) {
832 device->can_discard = 1;
833 fs_devices->num_can_discard++;
834 }
835
Chris Mason8a4b83c2008-03-24 15:02:07 -0400836 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400837 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500838 device->mode = flags;
839
Chris Masonc2898112009-06-10 09:51:32 -0400840 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
841 fs_devices->rotating = 1;
842
Chris Masona0af4692008-05-13 16:03:06 -0400843 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300844 if (device->writeable &&
845 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500846 fs_devices->rw_devices++;
847 list_add(&device->dev_alloc_list,
848 &fs_devices->alloc_list);
849 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000850 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400851 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400852
Chris Masona0af4692008-05-13 16:03:06 -0400853error_brelse:
854 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100855 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400856 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400857 }
Chris Masona0af4692008-05-13 16:03:06 -0400858 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300859 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400860 goto out;
861 }
Yan Zheng2b820322008-11-17 21:11:30 -0500862 fs_devices->seeding = seeding;
863 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800864 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500865 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400866out:
Yan Zheng2b820322008-11-17 21:11:30 -0500867 return ret;
868}
869
870int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500871 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500872{
873 int ret;
874
875 mutex_lock(&uuid_mutex);
876 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500877 fs_devices->opened++;
878 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500879 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500880 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500881 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400882 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400883 return ret;
884}
885
David Sterba6f60cbd2013-02-15 11:31:02 -0700886/*
887 * Look for a btrfs signature on a device. This may be called out of the mount path
888 * and we are not allowed to call set_blocksize during the scan. The superblock
889 * is read via pagecache
890 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500891int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400892 struct btrfs_fs_devices **fs_devices_ret)
893{
894 struct btrfs_super_block *disk_super;
895 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700896 struct page *page;
897 void *p;
898 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400899 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400900 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400901 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700902 u64 bytenr;
903 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400904
David Sterba6f60cbd2013-02-15 11:31:02 -0700905 /*
906 * we would like to check all the supers, but that would make
907 * a btrfs mount succeed after a mkfs from a different FS.
908 * So, we need to add a special mount option to scan for
909 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
910 */
911 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100912 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500913 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700914
915 bdev = blkdev_get_by_path(path, flags, holder);
916
917 if (IS_ERR(bdev)) {
918 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100919 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700920 }
921
922 /* make sure our super fits in the device */
923 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
924 goto error_bdev_put;
925
926 /* make sure our super fits in the page */
927 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
928 goto error_bdev_put;
929
930 /* make sure our super doesn't straddle pages on disk */
931 index = bytenr >> PAGE_CACHE_SHIFT;
932 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
933 goto error_bdev_put;
934
935 /* pull in the page with our super */
936 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
937 index, GFP_NOFS);
938
939 if (IS_ERR_OR_NULL(page))
940 goto error_bdev_put;
941
942 p = kmap(page);
943
944 /* align our pointer to the offset of the super block */
945 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
946
947 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800948 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700949 goto error_unmap;
950
Xiao Guangronga3438322010-01-06 11:48:18 +0000951 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400952 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400953 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700954
Chris Mason8a4b83c2008-03-24 15:02:07 -0400955 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100956 if (ret > 0) {
957 if (disk_super->label[0]) {
958 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
959 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
960 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
961 } else {
962 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
963 }
964
965 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
966 ret = 0;
967 }
Josef Bacik02db0842012-06-21 16:03:58 -0400968 if (!ret && fs_devices_ret)
969 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700970
971error_unmap:
972 kunmap(page);
973 page_cache_release(page);
974
975error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100976 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400977error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100978 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400979 return ret;
980}
Chris Mason0b86a832008-03-24 15:01:56 -0400981
Miao Xie6d07bce2011-01-05 10:07:31 +0000982/* helper to account the used device space in the range */
983int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
984 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400985{
986 struct btrfs_key key;
987 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000988 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500989 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000990 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400991 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000992 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400993 struct extent_buffer *l;
994
Miao Xie6d07bce2011-01-05 10:07:31 +0000995 *length = 0;
996
Stefan Behrens63a212a2012-11-05 18:29:28 +0100997 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000998 return 0;
999
Yan Zheng2b820322008-11-17 21:11:30 -05001000 path = btrfs_alloc_path();
1001 if (!path)
1002 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001003 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001004
Chris Mason0b86a832008-03-24 15:01:56 -04001005 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001006 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001007 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001008
1009 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001010 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001011 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001012 if (ret > 0) {
1013 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1014 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001015 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001016 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001017
Chris Mason0b86a832008-03-24 15:01:56 -04001018 while (1) {
1019 l = path->nodes[0];
1020 slot = path->slots[0];
1021 if (slot >= btrfs_header_nritems(l)) {
1022 ret = btrfs_next_leaf(root, path);
1023 if (ret == 0)
1024 continue;
1025 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001026 goto out;
1027
1028 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001029 }
1030 btrfs_item_key_to_cpu(l, &key, slot);
1031
1032 if (key.objectid < device->devid)
1033 goto next;
1034
1035 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001036 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001037
David Sterba962a2982014-06-04 18:41:45 +02001038 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001039 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001040
Chris Mason0b86a832008-03-24 15:01:56 -04001041 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001042 extent_end = key.offset + btrfs_dev_extent_length(l,
1043 dev_extent);
1044 if (key.offset <= start && extent_end > end) {
1045 *length = end - start + 1;
1046 break;
1047 } else if (key.offset <= start && extent_end > start)
1048 *length += extent_end - start;
1049 else if (key.offset > start && extent_end <= end)
1050 *length += extent_end - key.offset;
1051 else if (key.offset > start && key.offset <= end) {
1052 *length += end - key.offset + 1;
1053 break;
1054 } else if (key.offset > end)
1055 break;
1056
1057next:
1058 path->slots[0]++;
1059 }
1060 ret = 0;
1061out:
1062 btrfs_free_path(path);
1063 return ret;
1064}
1065
Josef Bacik6df9a952013-06-27 13:22:46 -04001066static int contains_pending_extent(struct btrfs_trans_handle *trans,
1067 struct btrfs_device *device,
1068 u64 *start, u64 len)
1069{
1070 struct extent_map *em;
1071 int ret = 0;
1072
1073 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
1074 struct map_lookup *map;
1075 int i;
1076
1077 map = (struct map_lookup *)em->bdev;
1078 for (i = 0; i < map->num_stripes; i++) {
1079 if (map->stripes[i].dev != device)
1080 continue;
1081 if (map->stripes[i].physical >= *start + len ||
1082 map->stripes[i].physical + em->orig_block_len <=
1083 *start)
1084 continue;
1085 *start = map->stripes[i].physical +
1086 em->orig_block_len;
1087 ret = 1;
1088 }
1089 }
1090
1091 return ret;
1092}
1093
1094
Chris Mason0b86a832008-03-24 15:01:56 -04001095/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001096 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001097 * @device: the device which we search the free space in
1098 * @num_bytes: the size of the free space that we need
1099 * @start: store the start of the free space.
1100 * @len: the size of the free space. that we find, or the size of the max
1101 * free space if we don't find suitable free space
1102 *
Chris Mason0b86a832008-03-24 15:01:56 -04001103 * this uses a pretty simple search, the expectation is that it is
1104 * called very infrequently and that a given device has a small number
1105 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001106 *
1107 * @start is used to store the start of the free space if we find. But if we
1108 * don't find suitable free space, it will be used to store the start position
1109 * of the max free space.
1110 *
1111 * @len is used to store the size of the free space that we find.
1112 * But if we don't find suitable free space, it is used to store the size of
1113 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001114 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001115int find_free_dev_extent(struct btrfs_trans_handle *trans,
1116 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001117 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001118{
1119 struct btrfs_key key;
1120 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001121 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001122 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001123 u64 hole_size;
1124 u64 max_hole_start;
1125 u64 max_hole_size;
1126 u64 extent_end;
1127 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001128 u64 search_end = device->total_bytes;
1129 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001130 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001131 struct extent_buffer *l;
1132
Chris Mason0b86a832008-03-24 15:01:56 -04001133 /* FIXME use last free of some kind */
1134
1135 /* we don't want to overwrite the superblock on the drive,
1136 * so we make sure to start at an offset of at least 1MB
1137 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001138 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001139
Josef Bacik6df9a952013-06-27 13:22:46 -04001140 path = btrfs_alloc_path();
1141 if (!path)
1142 return -ENOMEM;
1143again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001144 max_hole_start = search_start;
1145 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001146 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001147
Stefan Behrens63a212a2012-11-05 18:29:28 +01001148 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001149 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001150 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001151 }
1152
Miao Xie7bfc8372011-01-05 10:07:26 +00001153 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001154 path->search_commit_root = 1;
1155 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001156
Chris Mason0b86a832008-03-24 15:01:56 -04001157 key.objectid = device->devid;
1158 key.offset = search_start;
1159 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001160
Li Zefan125ccb02011-12-08 15:07:24 +08001161 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001162 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001163 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001164 if (ret > 0) {
1165 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1166 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001167 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001168 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001169
Chris Mason0b86a832008-03-24 15:01:56 -04001170 while (1) {
1171 l = path->nodes[0];
1172 slot = path->slots[0];
1173 if (slot >= btrfs_header_nritems(l)) {
1174 ret = btrfs_next_leaf(root, path);
1175 if (ret == 0)
1176 continue;
1177 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001178 goto out;
1179
1180 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001181 }
1182 btrfs_item_key_to_cpu(l, &key, slot);
1183
1184 if (key.objectid < device->devid)
1185 goto next;
1186
1187 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001188 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001189
David Sterba962a2982014-06-04 18:41:45 +02001190 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001191 goto next;
1192
Miao Xie7bfc8372011-01-05 10:07:26 +00001193 if (key.offset > search_start) {
1194 hole_size = key.offset - search_start;
1195
Josef Bacik6df9a952013-06-27 13:22:46 -04001196 /*
1197 * Have to check before we set max_hole_start, otherwise
1198 * we could end up sending back this offset anyway.
1199 */
1200 if (contains_pending_extent(trans, device,
1201 &search_start,
1202 hole_size))
1203 hole_size = 0;
1204
Miao Xie7bfc8372011-01-05 10:07:26 +00001205 if (hole_size > max_hole_size) {
1206 max_hole_start = search_start;
1207 max_hole_size = hole_size;
1208 }
1209
1210 /*
1211 * If this free space is greater than which we need,
1212 * it must be the max free space that we have found
1213 * until now, so max_hole_start must point to the start
1214 * of this free space and the length of this free space
1215 * is stored in max_hole_size. Thus, we return
1216 * max_hole_start and max_hole_size and go back to the
1217 * caller.
1218 */
1219 if (hole_size >= num_bytes) {
1220 ret = 0;
1221 goto out;
1222 }
1223 }
1224
Chris Mason0b86a832008-03-24 15:01:56 -04001225 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001226 extent_end = key.offset + btrfs_dev_extent_length(l,
1227 dev_extent);
1228 if (extent_end > search_start)
1229 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001230next:
1231 path->slots[0]++;
1232 cond_resched();
1233 }
Chris Mason0b86a832008-03-24 15:01:56 -04001234
liubo38c01b92011-08-02 02:39:03 +00001235 /*
1236 * At this point, search_start should be the end of
1237 * allocated dev extents, and when shrinking the device,
1238 * search_end may be smaller than search_start.
1239 */
1240 if (search_end > search_start)
1241 hole_size = search_end - search_start;
1242
Miao Xie7bfc8372011-01-05 10:07:26 +00001243 if (hole_size > max_hole_size) {
1244 max_hole_start = search_start;
1245 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001246 }
Chris Mason0b86a832008-03-24 15:01:56 -04001247
Josef Bacik6df9a952013-06-27 13:22:46 -04001248 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1249 btrfs_release_path(path);
1250 goto again;
1251 }
1252
Miao Xie7bfc8372011-01-05 10:07:26 +00001253 /* See above. */
1254 if (hole_size < num_bytes)
1255 ret = -ENOSPC;
1256 else
1257 ret = 0;
1258
1259out:
Yan Zheng2b820322008-11-17 21:11:30 -05001260 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001261 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001262 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001263 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001264 return ret;
1265}
1266
Christoph Hellwigb2950862008-12-02 09:54:17 -05001267static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001268 struct btrfs_device *device,
1269 u64 start)
1270{
1271 int ret;
1272 struct btrfs_path *path;
1273 struct btrfs_root *root = device->dev_root;
1274 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001275 struct btrfs_key found_key;
1276 struct extent_buffer *leaf = NULL;
1277 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001278
1279 path = btrfs_alloc_path();
1280 if (!path)
1281 return -ENOMEM;
1282
1283 key.objectid = device->devid;
1284 key.offset = start;
1285 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001286again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001287 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001288 if (ret > 0) {
1289 ret = btrfs_previous_item(root, path, key.objectid,
1290 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001291 if (ret)
1292 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001293 leaf = path->nodes[0];
1294 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1295 extent = btrfs_item_ptr(leaf, path->slots[0],
1296 struct btrfs_dev_extent);
1297 BUG_ON(found_key.offset > start || found_key.offset +
1298 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001299 key = found_key;
1300 btrfs_release_path(path);
1301 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001302 } else if (ret == 0) {
1303 leaf = path->nodes[0];
1304 extent = btrfs_item_ptr(leaf, path->slots[0],
1305 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001306 } else {
1307 btrfs_error(root->fs_info, ret, "Slot search failed");
1308 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001309 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001310
Josef Bacik2bf64752011-09-26 17:12:22 -04001311 if (device->bytes_used > 0) {
1312 u64 len = btrfs_dev_extent_length(leaf, extent);
1313 device->bytes_used -= len;
1314 spin_lock(&root->fs_info->free_chunk_lock);
1315 root->fs_info->free_chunk_space += len;
1316 spin_unlock(&root->fs_info->free_chunk_lock);
1317 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001318 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001319 if (ret) {
1320 btrfs_error(root->fs_info, ret,
1321 "Failed to remove dev extent item");
1322 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001323out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001324 btrfs_free_path(path);
1325 return ret;
1326}
1327
Eric Sandeen48a3b632013-04-25 20:41:01 +00001328static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1329 struct btrfs_device *device,
1330 u64 chunk_tree, u64 chunk_objectid,
1331 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001332{
1333 int ret;
1334 struct btrfs_path *path;
1335 struct btrfs_root *root = device->dev_root;
1336 struct btrfs_dev_extent *extent;
1337 struct extent_buffer *leaf;
1338 struct btrfs_key key;
1339
Chris Masondfe25022008-05-13 13:46:40 -04001340 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001341 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001342 path = btrfs_alloc_path();
1343 if (!path)
1344 return -ENOMEM;
1345
Chris Mason0b86a832008-03-24 15:01:56 -04001346 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001347 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001348 key.type = BTRFS_DEV_EXTENT_KEY;
1349 ret = btrfs_insert_empty_item(trans, root, path, &key,
1350 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001351 if (ret)
1352 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001353
1354 leaf = path->nodes[0];
1355 extent = btrfs_item_ptr(leaf, path->slots[0],
1356 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001357 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1358 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1359 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1360
1361 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001362 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001363
Chris Mason0b86a832008-03-24 15:01:56 -04001364 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1365 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001366out:
Chris Mason0b86a832008-03-24 15:01:56 -04001367 btrfs_free_path(path);
1368 return ret;
1369}
1370
Josef Bacik6df9a952013-06-27 13:22:46 -04001371static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001372{
Josef Bacik6df9a952013-06-27 13:22:46 -04001373 struct extent_map_tree *em_tree;
1374 struct extent_map *em;
1375 struct rb_node *n;
1376 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001377
Josef Bacik6df9a952013-06-27 13:22:46 -04001378 em_tree = &fs_info->mapping_tree.map_tree;
1379 read_lock(&em_tree->lock);
1380 n = rb_last(&em_tree->map);
1381 if (n) {
1382 em = rb_entry(n, struct extent_map, rb_node);
1383 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001384 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001385 read_unlock(&em_tree->lock);
1386
Chris Mason0b86a832008-03-24 15:01:56 -04001387 return ret;
1388}
1389
Ilya Dryomov53f10652013-08-12 14:33:01 +03001390static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1391 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001392{
1393 int ret;
1394 struct btrfs_key key;
1395 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001396 struct btrfs_path *path;
1397
Yan Zheng2b820322008-11-17 21:11:30 -05001398 path = btrfs_alloc_path();
1399 if (!path)
1400 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001401
1402 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1403 key.type = BTRFS_DEV_ITEM_KEY;
1404 key.offset = (u64)-1;
1405
Ilya Dryomov53f10652013-08-12 14:33:01 +03001406 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001407 if (ret < 0)
1408 goto error;
1409
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001410 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001411
Ilya Dryomov53f10652013-08-12 14:33:01 +03001412 ret = btrfs_previous_item(fs_info->chunk_root, path,
1413 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001414 BTRFS_DEV_ITEM_KEY);
1415 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001416 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001417 } else {
1418 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1419 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001420 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001421 }
1422 ret = 0;
1423error:
Yan Zheng2b820322008-11-17 21:11:30 -05001424 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001425 return ret;
1426}
1427
1428/*
1429 * the device information is stored in the chunk root
1430 * the btrfs_device struct should be fully filled in
1431 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001432static int btrfs_add_device(struct btrfs_trans_handle *trans,
1433 struct btrfs_root *root,
1434 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001435{
1436 int ret;
1437 struct btrfs_path *path;
1438 struct btrfs_dev_item *dev_item;
1439 struct extent_buffer *leaf;
1440 struct btrfs_key key;
1441 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001442
1443 root = root->fs_info->chunk_root;
1444
1445 path = btrfs_alloc_path();
1446 if (!path)
1447 return -ENOMEM;
1448
Chris Mason0b86a832008-03-24 15:01:56 -04001449 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1450 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001451 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001452
1453 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001454 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001455 if (ret)
1456 goto out;
1457
1458 leaf = path->nodes[0];
1459 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1460
1461 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001462 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001463 btrfs_set_device_type(leaf, dev_item, device->type);
1464 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1465 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1466 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7df69d32014-07-24 11:37:13 +08001467 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001468 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001469 btrfs_set_device_group(leaf, dev_item, 0);
1470 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1471 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001472 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001473
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001474 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001475 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001476 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001477 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001478 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001479
Yan Zheng2b820322008-11-17 21:11:30 -05001480 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001481out:
1482 btrfs_free_path(path);
1483 return ret;
1484}
Chris Mason8f18cf12008-04-25 16:53:30 -04001485
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001486/*
1487 * Function to update ctime/mtime for a given device path.
1488 * Mainly used for ctime/mtime based probe like libblkid.
1489 */
1490static void update_dev_time(char *path_name)
1491{
1492 struct file *filp;
1493
1494 filp = filp_open(path_name, O_RDWR, 0);
1495 if (!filp)
1496 return;
1497 file_update_time(filp);
1498 filp_close(filp, NULL);
1499 return;
1500}
1501
Chris Masona061fc82008-05-07 11:43:44 -04001502static int btrfs_rm_dev_item(struct btrfs_root *root,
1503 struct btrfs_device *device)
1504{
1505 int ret;
1506 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001507 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001508 struct btrfs_trans_handle *trans;
1509
1510 root = root->fs_info->chunk_root;
1511
1512 path = btrfs_alloc_path();
1513 if (!path)
1514 return -ENOMEM;
1515
Yan, Zhenga22285a2010-05-16 10:48:46 -04001516 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001517 if (IS_ERR(trans)) {
1518 btrfs_free_path(path);
1519 return PTR_ERR(trans);
1520 }
Chris Masona061fc82008-05-07 11:43:44 -04001521 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1522 key.type = BTRFS_DEV_ITEM_KEY;
1523 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001524 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001525
1526 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1527 if (ret < 0)
1528 goto out;
1529
1530 if (ret > 0) {
1531 ret = -ENOENT;
1532 goto out;
1533 }
1534
1535 ret = btrfs_del_item(trans, root, path);
1536 if (ret)
1537 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001538out:
1539 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001540 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001541 btrfs_commit_transaction(trans, root);
1542 return ret;
1543}
1544
1545int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1546{
1547 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001548 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001549 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001550 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001551 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001552 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001553 u64 all_avail;
1554 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001555 u64 num_devices;
1556 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001557 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001558 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001559 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001560
Chris Masona061fc82008-05-07 11:43:44 -04001561 mutex_lock(&uuid_mutex);
1562
Miao Xiede98ced2013-01-29 10:13:12 +00001563 do {
1564 seq = read_seqbegin(&root->fs_info->profiles_lock);
1565
1566 all_avail = root->fs_info->avail_data_alloc_bits |
1567 root->fs_info->avail_system_alloc_bits |
1568 root->fs_info->avail_metadata_alloc_bits;
1569 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001570
Stefan Behrens8dabb742012-11-06 13:15:27 +01001571 num_devices = root->fs_info->fs_devices->num_devices;
1572 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1573 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1574 WARN_ON(num_devices < 1);
1575 num_devices--;
1576 }
1577 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1578
1579 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001580 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001581 goto out;
1582 }
1583
Stefan Behrens8dabb742012-11-06 13:15:27 +01001584 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001585 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001586 goto out;
1587 }
1588
David Woodhouse53b381b2013-01-29 18:40:14 -05001589 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1590 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001591 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001592 goto out;
1593 }
1594 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1595 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001596 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001597 goto out;
1598 }
1599
Chris Masondfe25022008-05-13 13:46:40 -04001600 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001601 struct list_head *devices;
1602 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001603
Chris Masondfe25022008-05-13 13:46:40 -04001604 device = NULL;
1605 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001606 /*
1607 * It is safe to read the devices since the volume_mutex
1608 * is held.
1609 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001610 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001611 if (tmp->in_fs_metadata &&
1612 !tmp->is_tgtdev_for_dev_replace &&
1613 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001614 device = tmp;
1615 break;
1616 }
1617 }
1618 bdev = NULL;
1619 bh = NULL;
1620 disk_super = NULL;
1621 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001622 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001623 goto out;
1624 }
Chris Masondfe25022008-05-13 13:46:40 -04001625 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001626 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001627 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001628 root->fs_info->bdev_holder, 0,
1629 &bdev, &bh);
1630 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001631 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001632 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001633 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001634 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001635 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001636 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001637 if (!device) {
1638 ret = -ENOENT;
1639 goto error_brelse;
1640 }
Chris Masondfe25022008-05-13 13:46:40 -04001641 }
Yan Zheng2b820322008-11-17 21:11:30 -05001642
Stefan Behrens63a212a2012-11-05 18:29:28 +01001643 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001644 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001645 goto error_brelse;
1646 }
1647
Yan Zheng2b820322008-11-17 21:11:30 -05001648 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001649 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001650 goto error_brelse;
1651 }
1652
1653 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001654 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001655 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001656 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001657 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001658 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001659 }
Chris Masona061fc82008-05-07 11:43:44 -04001660
Carey Underwoodd7901552013-03-04 16:37:06 -06001661 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001662 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001663 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001664 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001665 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001666
Stefan Behrens63a212a2012-11-05 18:29:28 +01001667 /*
1668 * TODO: the superblock still includes this device in its num_devices
1669 * counter although write_all_supers() is not locked out. This
1670 * could give a filesystem state which requires a degraded mount.
1671 */
Chris Masona061fc82008-05-07 11:43:44 -04001672 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1673 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001674 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001675
Josef Bacik2bf64752011-09-26 17:12:22 -04001676 spin_lock(&root->fs_info->free_chunk_lock);
1677 root->fs_info->free_chunk_space = device->total_bytes -
1678 device->bytes_used;
1679 spin_unlock(&root->fs_info->free_chunk_lock);
1680
Yan Zheng2b820322008-11-17 21:11:30 -05001681 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001682 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001683
1684 /*
1685 * the device list mutex makes sure that we don't change
1686 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001687 * the device supers. Whoever is writing all supers, should
1688 * lock the device list mutex before getting the number of
1689 * devices in the super block (super_copy). Conversely,
1690 * whoever updates the number of devices in the super block
1691 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001692 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001693
1694 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001695 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001696 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001697
Yan Zhenge4404d62008-12-12 10:03:26 -05001698 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001699 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001700
Chris Masoncd02dca2010-12-13 14:56:23 -05001701 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001702 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001703
Yan Zheng2b820322008-11-17 21:11:30 -05001704 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1705 struct btrfs_device, dev_list);
1706 if (device->bdev == root->fs_info->sb->s_bdev)
1707 root->fs_info->sb->s_bdev = next_device->bdev;
1708 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1709 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1710
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001711 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001712 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001713 /* remove sysfs entry */
1714 btrfs_kobj_rm_device(root->fs_info, device);
1715 }
Anand Jain99994cd2014-06-03 11:36:00 +08001716
Xiao Guangrong1f781602011-04-20 10:09:16 +00001717 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001718
David Sterba6c417612011-04-13 15:41:04 +02001719 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1720 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001721 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001722
Xiao Guangrong1f781602011-04-20 10:09:16 +00001723 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001724 struct btrfs_fs_devices *fs_devices;
1725 fs_devices = root->fs_info->fs_devices;
1726 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001727 if (fs_devices->seed == cur_devices) {
1728 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001729 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001730 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001731 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001732 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001733 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001734 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001735 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001736 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001737 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001738 }
1739
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001740 root->fs_info->num_tolerated_disk_barrier_failures =
1741 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1742
Yan Zheng2b820322008-11-17 21:11:30 -05001743 /*
1744 * at this point, the device is zero sized. We want to
1745 * remove it from the devices list and zero out the old super
1746 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001747 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001748 u64 bytenr;
1749 int i;
1750
Chris Masondfe25022008-05-13 13:46:40 -04001751 /* make sure this device isn't detected as part of
1752 * the FS anymore
1753 */
1754 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1755 set_buffer_dirty(bh);
1756 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001757
1758 /* clear the mirror copies of super block on the disk
1759 * being removed, 0th copy is been taken care above and
1760 * the below would take of the rest
1761 */
1762 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1763 bytenr = btrfs_sb_offset(i);
1764 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1765 i_size_read(bdev->bd_inode))
1766 break;
1767
1768 brelse(bh);
1769 bh = __bread(bdev, bytenr / 4096,
1770 BTRFS_SUPER_INFO_SIZE);
1771 if (!bh)
1772 continue;
1773
1774 disk_super = (struct btrfs_super_block *)bh->b_data;
1775
1776 if (btrfs_super_bytenr(disk_super) != bytenr ||
1777 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1778 continue;
1779 }
1780 memset(&disk_super->magic, 0,
1781 sizeof(disk_super->magic));
1782 set_buffer_dirty(bh);
1783 sync_dirty_buffer(bh);
1784 }
Chris Masondfe25022008-05-13 13:46:40 -04001785 }
Chris Masona061fc82008-05-07 11:43:44 -04001786
Chris Masona061fc82008-05-07 11:43:44 -04001787 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001788
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001789 if (bdev) {
1790 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001791 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001792
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001793 /* Update ctime/mtime for device path for libblkid */
1794 update_dev_time(device_path);
1795 }
1796
Chris Masona061fc82008-05-07 11:43:44 -04001797error_brelse:
1798 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001799 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001800 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001801out:
1802 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001803 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001804error_undo:
1805 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001806 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001807 list_add(&device->dev_alloc_list,
1808 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001809 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001810 root->fs_info->fs_devices->rw_devices++;
1811 }
1812 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001813}
1814
Stefan Behrense93c89c2012-11-05 17:33:06 +01001815void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1816 struct btrfs_device *srcdev)
1817{
Anand Jaind51908c2014-08-13 14:24:19 +08001818 struct btrfs_fs_devices *fs_devices;
1819
Stefan Behrense93c89c2012-11-05 17:33:06 +01001820 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001821
Anand Jain25e8e912014-08-20 10:56:56 +08001822 /*
1823 * in case of fs with no seed, srcdev->fs_devices will point
1824 * to fs_devices of fs_info. However when the dev being replaced is
1825 * a seed dev it will point to the seed's local fs_devices. In short
1826 * srcdev will have its correct fs_devices in both the cases.
1827 */
1828 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001829
Stefan Behrense93c89c2012-11-05 17:33:06 +01001830 list_del_rcu(&srcdev->dev_list);
1831 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001832 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001833 if (srcdev->missing) {
Anand Jaind51908c2014-08-13 14:24:19 +08001834 fs_devices->missing_devices--;
Anand Jainb2efedc2014-08-13 14:24:24 +08001835 if (!fs_devices->seeding)
1836 fs_devices->rw_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001837 }
1838 if (srcdev->can_discard)
Anand Jaind51908c2014-08-13 14:24:19 +08001839 fs_devices->num_can_discard--;
Ilya Dryomov13572722013-10-02 20:41:01 +03001840 if (srcdev->bdev) {
Anand Jaind51908c2014-08-13 14:24:19 +08001841 fs_devices->open_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001842
Miao Xieff61d172014-07-24 11:37:06 +08001843 /*
1844 * zero out the old super if it is not writable
1845 * (e.g. seed device)
1846 */
1847 if (srcdev->writeable)
1848 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03001849 }
1850
Stefan Behrense93c89c2012-11-05 17:33:06 +01001851 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001852
1853 /*
1854 * unless fs_devices is seed fs, num_devices shouldn't go
1855 * zero
1856 */
1857 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1858
1859 /* if this is no devs we rather delete the fs_devices */
1860 if (!fs_devices->num_devices) {
1861 struct btrfs_fs_devices *tmp_fs_devices;
1862
1863 tmp_fs_devices = fs_info->fs_devices;
1864 while (tmp_fs_devices) {
1865 if (tmp_fs_devices->seed == fs_devices) {
1866 tmp_fs_devices->seed = fs_devices->seed;
1867 break;
1868 }
1869 tmp_fs_devices = tmp_fs_devices->seed;
1870 }
1871 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08001872 __btrfs_close_devices(fs_devices);
1873 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001874 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01001875}
1876
1877void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1878 struct btrfs_device *tgtdev)
1879{
1880 struct btrfs_device *next_device;
1881
1882 WARN_ON(!tgtdev);
1883 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1884 if (tgtdev->bdev) {
1885 btrfs_scratch_superblock(tgtdev);
1886 fs_info->fs_devices->open_devices--;
1887 }
1888 fs_info->fs_devices->num_devices--;
1889 if (tgtdev->can_discard)
1890 fs_info->fs_devices->num_can_discard++;
1891
1892 next_device = list_entry(fs_info->fs_devices->devices.next,
1893 struct btrfs_device, dev_list);
1894 if (tgtdev->bdev == fs_info->sb->s_bdev)
1895 fs_info->sb->s_bdev = next_device->bdev;
1896 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1897 fs_info->fs_devices->latest_bdev = next_device->bdev;
1898 list_del_rcu(&tgtdev->dev_list);
1899
1900 call_rcu(&tgtdev->rcu, free_device);
1901
1902 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1903}
1904
Eric Sandeen48a3b632013-04-25 20:41:01 +00001905static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1906 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001907{
1908 int ret = 0;
1909 struct btrfs_super_block *disk_super;
1910 u64 devid;
1911 u8 *dev_uuid;
1912 struct block_device *bdev;
1913 struct buffer_head *bh;
1914
1915 *device = NULL;
1916 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1917 root->fs_info->bdev_holder, 0, &bdev, &bh);
1918 if (ret)
1919 return ret;
1920 disk_super = (struct btrfs_super_block *)bh->b_data;
1921 devid = btrfs_stack_device_id(&disk_super->dev_item);
1922 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001923 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001924 disk_super->fsid);
1925 brelse(bh);
1926 if (!*device)
1927 ret = -ENOENT;
1928 blkdev_put(bdev, FMODE_READ);
1929 return ret;
1930}
1931
1932int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1933 char *device_path,
1934 struct btrfs_device **device)
1935{
1936 *device = NULL;
1937 if (strcmp(device_path, "missing") == 0) {
1938 struct list_head *devices;
1939 struct btrfs_device *tmp;
1940
1941 devices = &root->fs_info->fs_devices->devices;
1942 /*
1943 * It is safe to read the devices since the volume_mutex
1944 * is held by the caller.
1945 */
1946 list_for_each_entry(tmp, devices, dev_list) {
1947 if (tmp->in_fs_metadata && !tmp->bdev) {
1948 *device = tmp;
1949 break;
1950 }
1951 }
1952
1953 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001954 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001955 return -ENOENT;
1956 }
1957
1958 return 0;
1959 } else {
1960 return btrfs_find_device_by_path(root, device_path, device);
1961 }
1962}
1963
Yan Zheng2b820322008-11-17 21:11:30 -05001964/*
1965 * does all the dirty work required for changing file system's UUID.
1966 */
Li Zefan125ccb02011-12-08 15:07:24 +08001967static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001968{
1969 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1970 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001971 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001972 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001973 struct btrfs_device *device;
1974 u64 super_flags;
1975
1976 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001977 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001978 return -EINVAL;
1979
Ilya Dryomov2208a372013-08-12 14:33:03 +03001980 seed_devices = __alloc_fs_devices();
1981 if (IS_ERR(seed_devices))
1982 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001983
Yan Zhenge4404d62008-12-12 10:03:26 -05001984 old_devices = clone_fs_devices(fs_devices);
1985 if (IS_ERR(old_devices)) {
1986 kfree(seed_devices);
1987 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001988 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001989
Yan Zheng2b820322008-11-17 21:11:30 -05001990 list_add(&old_devices->list, &fs_uuids);
1991
Yan Zhenge4404d62008-12-12 10:03:26 -05001992 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1993 seed_devices->opened = 1;
1994 INIT_LIST_HEAD(&seed_devices->devices);
1995 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001996 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001997
1998 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001999 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2000 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002001
Yan Zhenge4404d62008-12-12 10:03:26 -05002002 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2003 list_for_each_entry(device, &seed_devices->devices, dev_list) {
2004 device->fs_devices = seed_devices;
2005 }
2006
Yan Zheng2b820322008-11-17 21:11:30 -05002007 fs_devices->seeding = 0;
2008 fs_devices->num_devices = 0;
2009 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002010 fs_devices->missing_devices = 0;
2011 fs_devices->num_can_discard = 0;
2012 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002013 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002014
2015 generate_random_uuid(fs_devices->fsid);
2016 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2017 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002018 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2019
Yan Zheng2b820322008-11-17 21:11:30 -05002020 super_flags = btrfs_super_flags(disk_super) &
2021 ~BTRFS_SUPER_FLAG_SEEDING;
2022 btrfs_set_super_flags(disk_super, super_flags);
2023
2024 return 0;
2025}
2026
2027/*
2028 * strore the expected generation for seed devices in device items.
2029 */
2030static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2031 struct btrfs_root *root)
2032{
2033 struct btrfs_path *path;
2034 struct extent_buffer *leaf;
2035 struct btrfs_dev_item *dev_item;
2036 struct btrfs_device *device;
2037 struct btrfs_key key;
2038 u8 fs_uuid[BTRFS_UUID_SIZE];
2039 u8 dev_uuid[BTRFS_UUID_SIZE];
2040 u64 devid;
2041 int ret;
2042
2043 path = btrfs_alloc_path();
2044 if (!path)
2045 return -ENOMEM;
2046
2047 root = root->fs_info->chunk_root;
2048 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2049 key.offset = 0;
2050 key.type = BTRFS_DEV_ITEM_KEY;
2051
2052 while (1) {
2053 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2054 if (ret < 0)
2055 goto error;
2056
2057 leaf = path->nodes[0];
2058next_slot:
2059 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2060 ret = btrfs_next_leaf(root, path);
2061 if (ret > 0)
2062 break;
2063 if (ret < 0)
2064 goto error;
2065 leaf = path->nodes[0];
2066 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002067 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002068 continue;
2069 }
2070
2071 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2072 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2073 key.type != BTRFS_DEV_ITEM_KEY)
2074 break;
2075
2076 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2077 struct btrfs_dev_item);
2078 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002079 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002080 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002081 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002082 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002083 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2084 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002085 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002086
2087 if (device->fs_devices->seeding) {
2088 btrfs_set_device_generation(leaf, dev_item,
2089 device->generation);
2090 btrfs_mark_buffer_dirty(leaf);
2091 }
2092
2093 path->slots[0]++;
2094 goto next_slot;
2095 }
2096 ret = 0;
2097error:
2098 btrfs_free_path(path);
2099 return ret;
2100}
2101
Chris Mason788f20e2008-04-28 15:29:42 -04002102int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2103{
Josef Bacikd5e20032011-08-04 14:52:27 +00002104 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002105 struct btrfs_trans_handle *trans;
2106 struct btrfs_device *device;
2107 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002108 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002109 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002110 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002111 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002112 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002113 int ret = 0;
2114
Yan Zheng2b820322008-11-17 21:11:30 -05002115 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002116 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002117
Li Zefana5d16332011-12-07 20:08:40 -05002118 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002119 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002120 if (IS_ERR(bdev))
2121 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002122
Yan Zheng2b820322008-11-17 21:11:30 -05002123 if (root->fs_info->fs_devices->seeding) {
2124 seeding_dev = 1;
2125 down_write(&sb->s_umount);
2126 mutex_lock(&uuid_mutex);
2127 }
2128
Chris Mason8c8bee12008-09-29 11:19:10 -04002129 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002130
Chris Mason788f20e2008-04-28 15:29:42 -04002131 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002132
2133 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002134 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002135 if (device->bdev == bdev) {
2136 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002137 mutex_unlock(
2138 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002139 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002140 }
2141 }
Liu Bod25628b2012-11-14 14:35:30 +00002142 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002143
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002144 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2145 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002146 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002147 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002148 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002149 }
2150
Josef Bacik606686e2012-06-04 14:03:51 -04002151 name = rcu_string_strdup(device_path, GFP_NOFS);
2152 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002153 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002154 ret = -ENOMEM;
2155 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002156 }
Josef Bacik606686e2012-06-04 14:03:51 -04002157 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002158
Yan, Zhenga22285a2010-05-16 10:48:46 -04002159 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002160 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002161 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002162 kfree(device);
2163 ret = PTR_ERR(trans);
2164 goto error;
2165 }
2166
Yan Zheng2b820322008-11-17 21:11:30 -05002167 lock_chunks(root);
2168
Josef Bacikd5e20032011-08-04 14:52:27 +00002169 q = bdev_get_queue(bdev);
2170 if (blk_queue_discard(q))
2171 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002172 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002173 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002174 device->io_width = root->sectorsize;
2175 device->io_align = root->sectorsize;
2176 device->sector_size = root->sectorsize;
2177 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002178 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002179 device->dev_root = root->fs_info->dev_root;
2180 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002181 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002182 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002183 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002184 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002185 set_blocksize(device->bdev, 4096);
2186
Yan Zheng2b820322008-11-17 21:11:30 -05002187 if (seeding_dev) {
2188 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002189 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002190 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002191 }
2192
2193 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002194
Chris Masone5e9a522009-06-10 15:17:02 -04002195 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002196 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002197 list_add(&device->dev_alloc_list,
2198 &root->fs_info->fs_devices->alloc_list);
2199 root->fs_info->fs_devices->num_devices++;
2200 root->fs_info->fs_devices->open_devices++;
2201 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002202 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002203 if (device->can_discard)
2204 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002205 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2206
Josef Bacik2bf64752011-09-26 17:12:22 -04002207 spin_lock(&root->fs_info->free_chunk_lock);
2208 root->fs_info->free_chunk_space += device->total_bytes;
2209 spin_unlock(&root->fs_info->free_chunk_lock);
2210
Chris Masonc2898112009-06-10 09:51:32 -04002211 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2212 root->fs_info->fs_devices->rotating = 1;
2213
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002214 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002215 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002216 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002217
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002218 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002219 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002220 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002221
2222 /* add sysfs device entry */
2223 btrfs_kobj_add_device(root->fs_info, device);
2224
Chris Masone5e9a522009-06-10 15:17:02 -04002225 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002226
Yan Zheng2b820322008-11-17 21:11:30 -05002227 if (seeding_dev) {
Anand Jainb2373f22014-06-03 11:36:03 +08002228 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002229 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002230 if (ret) {
2231 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002232 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002233 }
Yan Zheng2b820322008-11-17 21:11:30 -05002234 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002235 if (ret) {
2236 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002237 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002238 }
Anand Jainb2373f22014-06-03 11:36:03 +08002239
2240 /* Sprouting would change fsid of the mounted root,
2241 * so rename the fsid on the sysfs
2242 */
2243 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2244 root->fs_info->fsid);
2245 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2246 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002247 } else {
2248 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002249 if (ret) {
2250 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002251 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002252 }
Yan Zheng2b820322008-11-17 21:11:30 -05002253 }
2254
Chris Mason913d9522009-03-10 13:17:18 -04002255 /*
2256 * we've got more storage, clear any full flags on the space
2257 * infos
2258 */
2259 btrfs_clear_space_info_full(root->fs_info);
2260
Chris Mason7d9eb122008-07-08 14:19:17 -04002261 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002262 root->fs_info->num_tolerated_disk_barrier_failures =
2263 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002264 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002265
2266 if (seeding_dev) {
2267 mutex_unlock(&uuid_mutex);
2268 up_write(&sb->s_umount);
2269
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002270 if (ret) /* transaction commit */
2271 return ret;
2272
Yan Zheng2b820322008-11-17 21:11:30 -05002273 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002274 if (ret < 0)
2275 btrfs_error(root->fs_info, ret,
2276 "Failed to relocate sys chunks after "
2277 "device initialization. This can be fixed "
2278 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002279 trans = btrfs_attach_transaction(root);
2280 if (IS_ERR(trans)) {
2281 if (PTR_ERR(trans) == -ENOENT)
2282 return 0;
2283 return PTR_ERR(trans);
2284 }
2285 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002286 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002287
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002288 /* Update ctime/mtime for libblkid */
2289 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002290 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002291
2292error_trans:
2293 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002294 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002295 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002296 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002297 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002298error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002299 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002300 if (seeding_dev) {
2301 mutex_unlock(&uuid_mutex);
2302 up_write(&sb->s_umount);
2303 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002304 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002305}
2306
Stefan Behrense93c89c2012-11-05 17:33:06 +01002307int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2308 struct btrfs_device **device_out)
2309{
2310 struct request_queue *q;
2311 struct btrfs_device *device;
2312 struct block_device *bdev;
2313 struct btrfs_fs_info *fs_info = root->fs_info;
2314 struct list_head *devices;
2315 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002316 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002317 int ret = 0;
2318
2319 *device_out = NULL;
2320 if (fs_info->fs_devices->seeding)
2321 return -EINVAL;
2322
2323 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2324 fs_info->bdev_holder);
2325 if (IS_ERR(bdev))
2326 return PTR_ERR(bdev);
2327
2328 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2329
2330 devices = &fs_info->fs_devices->devices;
2331 list_for_each_entry(device, devices, dev_list) {
2332 if (device->bdev == bdev) {
2333 ret = -EEXIST;
2334 goto error;
2335 }
2336 }
2337
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002338 device = btrfs_alloc_device(NULL, &devid, NULL);
2339 if (IS_ERR(device)) {
2340 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002341 goto error;
2342 }
2343
2344 name = rcu_string_strdup(device_path, GFP_NOFS);
2345 if (!name) {
2346 kfree(device);
2347 ret = -ENOMEM;
2348 goto error;
2349 }
2350 rcu_assign_pointer(device->name, name);
2351
2352 q = bdev_get_queue(bdev);
2353 if (blk_queue_discard(q))
2354 device->can_discard = 1;
2355 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2356 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002357 device->generation = 0;
2358 device->io_width = root->sectorsize;
2359 device->io_align = root->sectorsize;
2360 device->sector_size = root->sectorsize;
2361 device->total_bytes = i_size_read(bdev->bd_inode);
2362 device->disk_total_bytes = device->total_bytes;
2363 device->dev_root = fs_info->dev_root;
2364 device->bdev = bdev;
2365 device->in_fs_metadata = 1;
2366 device->is_tgtdev_for_dev_replace = 1;
2367 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002368 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002369 set_blocksize(device->bdev, 4096);
2370 device->fs_devices = fs_info->fs_devices;
2371 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2372 fs_info->fs_devices->num_devices++;
2373 fs_info->fs_devices->open_devices++;
2374 if (device->can_discard)
2375 fs_info->fs_devices->num_can_discard++;
2376 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2377
2378 *device_out = device;
2379 return ret;
2380
2381error:
2382 blkdev_put(bdev, FMODE_EXCL);
2383 return ret;
2384}
2385
2386void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2387 struct btrfs_device *tgtdev)
2388{
2389 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2390 tgtdev->io_width = fs_info->dev_root->sectorsize;
2391 tgtdev->io_align = fs_info->dev_root->sectorsize;
2392 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2393 tgtdev->dev_root = fs_info->dev_root;
2394 tgtdev->in_fs_metadata = 1;
2395}
2396
Chris Masond3977122009-01-05 21:25:51 -05002397static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2398 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002399{
2400 int ret;
2401 struct btrfs_path *path;
2402 struct btrfs_root *root;
2403 struct btrfs_dev_item *dev_item;
2404 struct extent_buffer *leaf;
2405 struct btrfs_key key;
2406
2407 root = device->dev_root->fs_info->chunk_root;
2408
2409 path = btrfs_alloc_path();
2410 if (!path)
2411 return -ENOMEM;
2412
2413 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2414 key.type = BTRFS_DEV_ITEM_KEY;
2415 key.offset = device->devid;
2416
2417 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2418 if (ret < 0)
2419 goto out;
2420
2421 if (ret > 0) {
2422 ret = -ENOENT;
2423 goto out;
2424 }
2425
2426 leaf = path->nodes[0];
2427 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2428
2429 btrfs_set_device_id(leaf, dev_item, device->devid);
2430 btrfs_set_device_type(leaf, dev_item, device->type);
2431 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2432 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2433 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002434 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002435 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2436 btrfs_mark_buffer_dirty(leaf);
2437
2438out:
2439 btrfs_free_path(path);
2440 return ret;
2441}
2442
Chris Mason7d9eb122008-07-08 14:19:17 -04002443static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002444 struct btrfs_device *device, u64 new_size)
2445{
2446 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002447 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002448 u64 old_total = btrfs_super_total_bytes(super_copy);
2449 u64 diff = new_size - device->total_bytes;
2450
Yan Zheng2b820322008-11-17 21:11:30 -05002451 if (!device->writeable)
2452 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002453 if (new_size <= device->total_bytes ||
2454 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002455 return -EINVAL;
2456
Chris Mason8f18cf12008-04-25 16:53:30 -04002457 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002458 device->fs_devices->total_rw_bytes += diff;
2459
2460 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002461 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002462 btrfs_clear_space_info_full(device->dev_root->fs_info);
2463
Chris Mason8f18cf12008-04-25 16:53:30 -04002464 return btrfs_update_device(trans, device);
2465}
2466
Chris Mason7d9eb122008-07-08 14:19:17 -04002467int btrfs_grow_device(struct btrfs_trans_handle *trans,
2468 struct btrfs_device *device, u64 new_size)
2469{
2470 int ret;
2471 lock_chunks(device->dev_root);
2472 ret = __btrfs_grow_device(trans, device, new_size);
2473 unlock_chunks(device->dev_root);
2474 return ret;
2475}
2476
Chris Mason8f18cf12008-04-25 16:53:30 -04002477static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2478 struct btrfs_root *root,
2479 u64 chunk_tree, u64 chunk_objectid,
2480 u64 chunk_offset)
2481{
2482 int ret;
2483 struct btrfs_path *path;
2484 struct btrfs_key key;
2485
2486 root = root->fs_info->chunk_root;
2487 path = btrfs_alloc_path();
2488 if (!path)
2489 return -ENOMEM;
2490
2491 key.objectid = chunk_objectid;
2492 key.offset = chunk_offset;
2493 key.type = BTRFS_CHUNK_ITEM_KEY;
2494
2495 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002496 if (ret < 0)
2497 goto out;
2498 else if (ret > 0) { /* Logic error or corruption */
2499 btrfs_error(root->fs_info, -ENOENT,
2500 "Failed lookup while freeing chunk.");
2501 ret = -ENOENT;
2502 goto out;
2503 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002504
2505 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002506 if (ret < 0)
2507 btrfs_error(root->fs_info, ret,
2508 "Failed to delete chunk item.");
2509out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002510 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002511 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002512}
2513
Christoph Hellwigb2950862008-12-02 09:54:17 -05002514static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002515 chunk_offset)
2516{
David Sterba6c417612011-04-13 15:41:04 +02002517 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002518 struct btrfs_disk_key *disk_key;
2519 struct btrfs_chunk *chunk;
2520 u8 *ptr;
2521 int ret = 0;
2522 u32 num_stripes;
2523 u32 array_size;
2524 u32 len = 0;
2525 u32 cur;
2526 struct btrfs_key key;
2527
2528 array_size = btrfs_super_sys_array_size(super_copy);
2529
2530 ptr = super_copy->sys_chunk_array;
2531 cur = 0;
2532
2533 while (cur < array_size) {
2534 disk_key = (struct btrfs_disk_key *)ptr;
2535 btrfs_disk_key_to_cpu(&key, disk_key);
2536
2537 len = sizeof(*disk_key);
2538
2539 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2540 chunk = (struct btrfs_chunk *)(ptr + len);
2541 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2542 len += btrfs_chunk_item_size(num_stripes);
2543 } else {
2544 ret = -EIO;
2545 break;
2546 }
2547 if (key.objectid == chunk_objectid &&
2548 key.offset == chunk_offset) {
2549 memmove(ptr, ptr + len, array_size - (cur + len));
2550 array_size -= len;
2551 btrfs_set_super_sys_array_size(super_copy, array_size);
2552 } else {
2553 ptr += len;
2554 cur += len;
2555 }
2556 }
2557 return ret;
2558}
2559
Christoph Hellwigb2950862008-12-02 09:54:17 -05002560static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002561 u64 chunk_tree, u64 chunk_objectid,
2562 u64 chunk_offset)
2563{
2564 struct extent_map_tree *em_tree;
2565 struct btrfs_root *extent_root;
2566 struct btrfs_trans_handle *trans;
2567 struct extent_map *em;
2568 struct map_lookup *map;
2569 int ret;
2570 int i;
2571
2572 root = root->fs_info->chunk_root;
2573 extent_root = root->fs_info->extent_root;
2574 em_tree = &root->fs_info->mapping_tree.map_tree;
2575
Josef Bacikba1bf482009-09-11 16:11:19 -04002576 ret = btrfs_can_relocate(extent_root, chunk_offset);
2577 if (ret)
2578 return -ENOSPC;
2579
Chris Mason8f18cf12008-04-25 16:53:30 -04002580 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002581 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002582 if (ret)
2583 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002584
Yan, Zhenga22285a2010-05-16 10:48:46 -04002585 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002586 if (IS_ERR(trans)) {
2587 ret = PTR_ERR(trans);
2588 btrfs_std_error(root->fs_info, ret);
2589 return ret;
2590 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002591
Chris Mason7d9eb122008-07-08 14:19:17 -04002592 lock_chunks(root);
2593
Chris Mason8f18cf12008-04-25 16:53:30 -04002594 /*
2595 * step two, delete the device extents and the
2596 * chunk tree entries
2597 */
Chris Mason890871b2009-09-02 16:24:52 -04002598 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002599 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002600 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002601
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002602 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002603 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002604 map = (struct map_lookup *)em->bdev;
2605
2606 for (i = 0; i < map->num_stripes; i++) {
2607 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2608 map->stripes[i].physical);
2609 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002610
Chris Masondfe25022008-05-13 13:46:40 -04002611 if (map->stripes[i].dev) {
2612 ret = btrfs_update_device(trans, map->stripes[i].dev);
2613 BUG_ON(ret);
2614 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002615 }
2616 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2617 chunk_offset);
2618
2619 BUG_ON(ret);
2620
liubo1abe9b82011-03-24 11:18:59 +00002621 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2622
Chris Mason8f18cf12008-04-25 16:53:30 -04002623 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2624 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2625 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002626 }
2627
Zheng Yan1a40e232008-09-26 10:09:34 -04002628 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2629 BUG_ON(ret);
2630
Chris Mason890871b2009-09-02 16:24:52 -04002631 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002632 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002633 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002634
Chris Mason8f18cf12008-04-25 16:53:30 -04002635 /* once for the tree */
2636 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002637 /* once for us */
2638 free_extent_map(em);
2639
Chris Mason7d9eb122008-07-08 14:19:17 -04002640 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002641 btrfs_end_transaction(trans, root);
2642 return 0;
2643}
2644
Yan Zheng2b820322008-11-17 21:11:30 -05002645static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2646{
2647 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2648 struct btrfs_path *path;
2649 struct extent_buffer *leaf;
2650 struct btrfs_chunk *chunk;
2651 struct btrfs_key key;
2652 struct btrfs_key found_key;
2653 u64 chunk_tree = chunk_root->root_key.objectid;
2654 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002655 bool retried = false;
2656 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002657 int ret;
2658
2659 path = btrfs_alloc_path();
2660 if (!path)
2661 return -ENOMEM;
2662
Josef Bacikba1bf482009-09-11 16:11:19 -04002663again:
Yan Zheng2b820322008-11-17 21:11:30 -05002664 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2665 key.offset = (u64)-1;
2666 key.type = BTRFS_CHUNK_ITEM_KEY;
2667
2668 while (1) {
2669 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2670 if (ret < 0)
2671 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002672 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002673
2674 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2675 key.type);
2676 if (ret < 0)
2677 goto error;
2678 if (ret > 0)
2679 break;
2680
2681 leaf = path->nodes[0];
2682 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2683
2684 chunk = btrfs_item_ptr(leaf, path->slots[0],
2685 struct btrfs_chunk);
2686 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002687 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002688
2689 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2690 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2691 found_key.objectid,
2692 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002693 if (ret == -ENOSPC)
2694 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302695 else
2696 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002697 }
2698
2699 if (found_key.offset == 0)
2700 break;
2701 key.offset = found_key.offset - 1;
2702 }
2703 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002704 if (failed && !retried) {
2705 failed = 0;
2706 retried = true;
2707 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302708 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002709 ret = -ENOSPC;
2710 }
Yan Zheng2b820322008-11-17 21:11:30 -05002711error:
2712 btrfs_free_path(path);
2713 return ret;
2714}
2715
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002716static int insert_balance_item(struct btrfs_root *root,
2717 struct btrfs_balance_control *bctl)
2718{
2719 struct btrfs_trans_handle *trans;
2720 struct btrfs_balance_item *item;
2721 struct btrfs_disk_balance_args disk_bargs;
2722 struct btrfs_path *path;
2723 struct extent_buffer *leaf;
2724 struct btrfs_key key;
2725 int ret, err;
2726
2727 path = btrfs_alloc_path();
2728 if (!path)
2729 return -ENOMEM;
2730
2731 trans = btrfs_start_transaction(root, 0);
2732 if (IS_ERR(trans)) {
2733 btrfs_free_path(path);
2734 return PTR_ERR(trans);
2735 }
2736
2737 key.objectid = BTRFS_BALANCE_OBJECTID;
2738 key.type = BTRFS_BALANCE_ITEM_KEY;
2739 key.offset = 0;
2740
2741 ret = btrfs_insert_empty_item(trans, root, path, &key,
2742 sizeof(*item));
2743 if (ret)
2744 goto out;
2745
2746 leaf = path->nodes[0];
2747 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2748
2749 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2750
2751 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2752 btrfs_set_balance_data(leaf, item, &disk_bargs);
2753 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2754 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2755 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2756 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2757
2758 btrfs_set_balance_flags(leaf, item, bctl->flags);
2759
2760 btrfs_mark_buffer_dirty(leaf);
2761out:
2762 btrfs_free_path(path);
2763 err = btrfs_commit_transaction(trans, root);
2764 if (err && !ret)
2765 ret = err;
2766 return ret;
2767}
2768
2769static int del_balance_item(struct btrfs_root *root)
2770{
2771 struct btrfs_trans_handle *trans;
2772 struct btrfs_path *path;
2773 struct btrfs_key key;
2774 int ret, err;
2775
2776 path = btrfs_alloc_path();
2777 if (!path)
2778 return -ENOMEM;
2779
2780 trans = btrfs_start_transaction(root, 0);
2781 if (IS_ERR(trans)) {
2782 btrfs_free_path(path);
2783 return PTR_ERR(trans);
2784 }
2785
2786 key.objectid = BTRFS_BALANCE_OBJECTID;
2787 key.type = BTRFS_BALANCE_ITEM_KEY;
2788 key.offset = 0;
2789
2790 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2791 if (ret < 0)
2792 goto out;
2793 if (ret > 0) {
2794 ret = -ENOENT;
2795 goto out;
2796 }
2797
2798 ret = btrfs_del_item(trans, root, path);
2799out:
2800 btrfs_free_path(path);
2801 err = btrfs_commit_transaction(trans, root);
2802 if (err && !ret)
2803 ret = err;
2804 return ret;
2805}
2806
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002807/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002808 * This is a heuristic used to reduce the number of chunks balanced on
2809 * resume after balance was interrupted.
2810 */
2811static void update_balance_args(struct btrfs_balance_control *bctl)
2812{
2813 /*
2814 * Turn on soft mode for chunk types that were being converted.
2815 */
2816 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2817 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2818 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2819 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2820 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2821 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2822
2823 /*
2824 * Turn on usage filter if is not already used. The idea is
2825 * that chunks that we have already balanced should be
2826 * reasonably full. Don't do it for chunks that are being
2827 * converted - that will keep us from relocating unconverted
2828 * (albeit full) chunks.
2829 */
2830 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2831 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2832 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2833 bctl->data.usage = 90;
2834 }
2835 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2836 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2837 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2838 bctl->sys.usage = 90;
2839 }
2840 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2841 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2842 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2843 bctl->meta.usage = 90;
2844 }
2845}
2846
2847/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002848 * Should be called with both balance and volume mutexes held to
2849 * serialize other volume operations (add_dev/rm_dev/resize) with
2850 * restriper. Same goes for unset_balance_control.
2851 */
2852static void set_balance_control(struct btrfs_balance_control *bctl)
2853{
2854 struct btrfs_fs_info *fs_info = bctl->fs_info;
2855
2856 BUG_ON(fs_info->balance_ctl);
2857
2858 spin_lock(&fs_info->balance_lock);
2859 fs_info->balance_ctl = bctl;
2860 spin_unlock(&fs_info->balance_lock);
2861}
2862
2863static void unset_balance_control(struct btrfs_fs_info *fs_info)
2864{
2865 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2866
2867 BUG_ON(!fs_info->balance_ctl);
2868
2869 spin_lock(&fs_info->balance_lock);
2870 fs_info->balance_ctl = NULL;
2871 spin_unlock(&fs_info->balance_lock);
2872
2873 kfree(bctl);
2874}
2875
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002876/*
2877 * Balance filters. Return 1 if chunk should be filtered out
2878 * (should not be balanced).
2879 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002880static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002881 struct btrfs_balance_args *bargs)
2882{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002883 chunk_type = chunk_to_extended(chunk_type) &
2884 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002885
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002886 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002887 return 0;
2888
2889 return 1;
2890}
2891
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002892static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2893 struct btrfs_balance_args *bargs)
2894{
2895 struct btrfs_block_group_cache *cache;
2896 u64 chunk_used, user_thresh;
2897 int ret = 1;
2898
2899 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2900 chunk_used = btrfs_block_group_used(&cache->item);
2901
Ilya Dryomova105bb82013-01-21 15:15:56 +02002902 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002903 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002904 else if (bargs->usage > 100)
2905 user_thresh = cache->key.offset;
2906 else
2907 user_thresh = div_factor_fine(cache->key.offset,
2908 bargs->usage);
2909
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002910 if (chunk_used < user_thresh)
2911 ret = 0;
2912
2913 btrfs_put_block_group(cache);
2914 return ret;
2915}
2916
Ilya Dryomov409d4042012-01-16 22:04:47 +02002917static int chunk_devid_filter(struct extent_buffer *leaf,
2918 struct btrfs_chunk *chunk,
2919 struct btrfs_balance_args *bargs)
2920{
2921 struct btrfs_stripe *stripe;
2922 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2923 int i;
2924
2925 for (i = 0; i < num_stripes; i++) {
2926 stripe = btrfs_stripe_nr(chunk, i);
2927 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2928 return 0;
2929 }
2930
2931 return 1;
2932}
2933
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002934/* [pstart, pend) */
2935static int chunk_drange_filter(struct extent_buffer *leaf,
2936 struct btrfs_chunk *chunk,
2937 u64 chunk_offset,
2938 struct btrfs_balance_args *bargs)
2939{
2940 struct btrfs_stripe *stripe;
2941 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2942 u64 stripe_offset;
2943 u64 stripe_length;
2944 int factor;
2945 int i;
2946
2947 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2948 return 0;
2949
2950 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002951 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2952 factor = num_stripes / 2;
2953 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2954 factor = num_stripes - 1;
2955 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2956 factor = num_stripes - 2;
2957 } else {
2958 factor = num_stripes;
2959 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002960
2961 for (i = 0; i < num_stripes; i++) {
2962 stripe = btrfs_stripe_nr(chunk, i);
2963 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2964 continue;
2965
2966 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2967 stripe_length = btrfs_chunk_length(leaf, chunk);
2968 do_div(stripe_length, factor);
2969
2970 if (stripe_offset < bargs->pend &&
2971 stripe_offset + stripe_length > bargs->pstart)
2972 return 0;
2973 }
2974
2975 return 1;
2976}
2977
Ilya Dryomovea671762012-01-16 22:04:48 +02002978/* [vstart, vend) */
2979static int chunk_vrange_filter(struct extent_buffer *leaf,
2980 struct btrfs_chunk *chunk,
2981 u64 chunk_offset,
2982 struct btrfs_balance_args *bargs)
2983{
2984 if (chunk_offset < bargs->vend &&
2985 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2986 /* at least part of the chunk is inside this vrange */
2987 return 0;
2988
2989 return 1;
2990}
2991
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002992static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002993 struct btrfs_balance_args *bargs)
2994{
2995 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2996 return 0;
2997
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002998 chunk_type = chunk_to_extended(chunk_type) &
2999 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003000
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003001 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003002 return 1;
3003
3004 return 0;
3005}
3006
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003007static int should_balance_chunk(struct btrfs_root *root,
3008 struct extent_buffer *leaf,
3009 struct btrfs_chunk *chunk, u64 chunk_offset)
3010{
3011 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3012 struct btrfs_balance_args *bargs = NULL;
3013 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3014
3015 /* type filter */
3016 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3017 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3018 return 0;
3019 }
3020
3021 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3022 bargs = &bctl->data;
3023 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3024 bargs = &bctl->sys;
3025 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3026 bargs = &bctl->meta;
3027
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003028 /* profiles filter */
3029 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3030 chunk_profiles_filter(chunk_type, bargs)) {
3031 return 0;
3032 }
3033
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003034 /* usage filter */
3035 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3036 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3037 return 0;
3038 }
3039
Ilya Dryomov409d4042012-01-16 22:04:47 +02003040 /* devid filter */
3041 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3042 chunk_devid_filter(leaf, chunk, bargs)) {
3043 return 0;
3044 }
3045
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003046 /* drange filter, makes sense only with devid filter */
3047 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3048 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3049 return 0;
3050 }
3051
Ilya Dryomovea671762012-01-16 22:04:48 +02003052 /* vrange filter */
3053 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3054 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3055 return 0;
3056 }
3057
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003058 /* soft profile changing mode */
3059 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3060 chunk_soft_convert_filter(chunk_type, bargs)) {
3061 return 0;
3062 }
3063
David Sterba7d824b62014-05-07 17:37:51 +02003064 /*
3065 * limited by count, must be the last filter
3066 */
3067 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3068 if (bargs->limit == 0)
3069 return 0;
3070 else
3071 bargs->limit--;
3072 }
3073
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003074 return 1;
3075}
3076
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003077static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003078{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003079 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003080 struct btrfs_root *chunk_root = fs_info->chunk_root;
3081 struct btrfs_root *dev_root = fs_info->dev_root;
3082 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003083 struct btrfs_device *device;
3084 u64 old_size;
3085 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003086 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003087 struct btrfs_path *path;
3088 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003089 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003090 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003091 struct extent_buffer *leaf;
3092 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003093 int ret;
3094 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003095 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003096 u64 limit_data = bctl->data.limit;
3097 u64 limit_meta = bctl->meta.limit;
3098 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003099
Chris Masonec44a352008-04-28 15:29:52 -04003100 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003101 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003102 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04003103 old_size = device->total_bytes;
3104 size_to_free = div_factor(old_size, 1);
3105 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003106 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003107 device->total_bytes - device->bytes_used > size_to_free ||
3108 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003109 continue;
3110
3111 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003112 if (ret == -ENOSPC)
3113 break;
Chris Masonec44a352008-04-28 15:29:52 -04003114 BUG_ON(ret);
3115
Yan, Zhenga22285a2010-05-16 10:48:46 -04003116 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003117 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003118
3119 ret = btrfs_grow_device(trans, device, old_size);
3120 BUG_ON(ret);
3121
3122 btrfs_end_transaction(trans, dev_root);
3123 }
3124
3125 /* step two, relocate all the chunks */
3126 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003127 if (!path) {
3128 ret = -ENOMEM;
3129 goto error;
3130 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003131
3132 /* zero out stat counters */
3133 spin_lock(&fs_info->balance_lock);
3134 memset(&bctl->stat, 0, sizeof(bctl->stat));
3135 spin_unlock(&fs_info->balance_lock);
3136again:
David Sterba7d824b62014-05-07 17:37:51 +02003137 if (!counting) {
3138 bctl->data.limit = limit_data;
3139 bctl->meta.limit = limit_meta;
3140 bctl->sys.limit = limit_sys;
3141 }
Chris Masonec44a352008-04-28 15:29:52 -04003142 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3143 key.offset = (u64)-1;
3144 key.type = BTRFS_CHUNK_ITEM_KEY;
3145
Chris Masond3977122009-01-05 21:25:51 -05003146 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003147 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003148 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003149 ret = -ECANCELED;
3150 goto error;
3151 }
3152
Chris Masonec44a352008-04-28 15:29:52 -04003153 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3154 if (ret < 0)
3155 goto error;
3156
3157 /*
3158 * this shouldn't happen, it means the last relocate
3159 * failed
3160 */
3161 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003162 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003163
3164 ret = btrfs_previous_item(chunk_root, path, 0,
3165 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003166 if (ret) {
3167 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003168 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003169 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003170
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003171 leaf = path->nodes[0];
3172 slot = path->slots[0];
3173 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3174
Chris Masonec44a352008-04-28 15:29:52 -04003175 if (found_key.objectid != key.objectid)
3176 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003177
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003178 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3179
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003180 if (!counting) {
3181 spin_lock(&fs_info->balance_lock);
3182 bctl->stat.considered++;
3183 spin_unlock(&fs_info->balance_lock);
3184 }
3185
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003186 ret = should_balance_chunk(chunk_root, leaf, chunk,
3187 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003188 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003189 if (!ret)
3190 goto loop;
3191
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003192 if (counting) {
3193 spin_lock(&fs_info->balance_lock);
3194 bctl->stat.expected++;
3195 spin_unlock(&fs_info->balance_lock);
3196 goto loop;
3197 }
3198
Chris Masonec44a352008-04-28 15:29:52 -04003199 ret = btrfs_relocate_chunk(chunk_root,
3200 chunk_root->root_key.objectid,
3201 found_key.objectid,
3202 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003203 if (ret && ret != -ENOSPC)
3204 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003205 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003206 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003207 } else {
3208 spin_lock(&fs_info->balance_lock);
3209 bctl->stat.completed++;
3210 spin_unlock(&fs_info->balance_lock);
3211 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003212loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003213 if (found_key.offset == 0)
3214 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003215 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003216 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003217
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003218 if (counting) {
3219 btrfs_release_path(path);
3220 counting = false;
3221 goto again;
3222 }
Chris Masonec44a352008-04-28 15:29:52 -04003223error:
3224 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003225 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003226 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003227 enospc_errors);
3228 if (!ret)
3229 ret = -ENOSPC;
3230 }
3231
Chris Masonec44a352008-04-28 15:29:52 -04003232 return ret;
3233}
3234
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003235/**
3236 * alloc_profile_is_valid - see if a given profile is valid and reduced
3237 * @flags: profile to validate
3238 * @extended: if true @flags is treated as an extended profile
3239 */
3240static int alloc_profile_is_valid(u64 flags, int extended)
3241{
3242 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3243 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3244
3245 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3246
3247 /* 1) check that all other bits are zeroed */
3248 if (flags & ~mask)
3249 return 0;
3250
3251 /* 2) see if profile is reduced */
3252 if (flags == 0)
3253 return !extended; /* "0" is valid for usual profiles */
3254
3255 /* true if exactly one bit set */
3256 return (flags & (flags - 1)) == 0;
3257}
3258
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003259static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3260{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003261 /* cancel requested || normal exit path */
3262 return atomic_read(&fs_info->balance_cancel_req) ||
3263 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3264 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003265}
3266
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003267static void __cancel_balance(struct btrfs_fs_info *fs_info)
3268{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003269 int ret;
3270
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003271 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003272 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003273 if (ret)
3274 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003275
3276 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003277}
3278
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003279/*
3280 * Should be called with both balance and volume mutexes held
3281 */
3282int btrfs_balance(struct btrfs_balance_control *bctl,
3283 struct btrfs_ioctl_balance_args *bargs)
3284{
3285 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003286 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003287 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003288 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003289 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003290 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003291
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003292 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003293 atomic_read(&fs_info->balance_pause_req) ||
3294 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003295 ret = -EINVAL;
3296 goto out;
3297 }
3298
Ilya Dryomove4837f82012-03-27 17:09:17 +03003299 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3300 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3301 mixed = 1;
3302
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003303 /*
3304 * In case of mixed groups both data and meta should be picked,
3305 * and identical options should be given for both of them.
3306 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003307 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3308 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003309 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3310 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3311 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003312 btrfs_err(fs_info, "with mixed groups data and "
3313 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003314 ret = -EINVAL;
3315 goto out;
3316 }
3317 }
3318
Stefan Behrens8dabb742012-11-06 13:15:27 +01003319 num_devices = fs_info->fs_devices->num_devices;
3320 btrfs_dev_replace_lock(&fs_info->dev_replace);
3321 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3322 BUG_ON(num_devices < 1);
3323 num_devices--;
3324 }
3325 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003326 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003327 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003328 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003329 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003330 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003331 if (num_devices > 2)
3332 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3333 if (num_devices > 3)
3334 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3335 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003336 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3337 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3338 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003339 btrfs_err(fs_info, "unable to start balance with target "
3340 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003341 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003342 ret = -EINVAL;
3343 goto out;
3344 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003345 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3346 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3347 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003348 btrfs_err(fs_info,
3349 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003350 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003351 ret = -EINVAL;
3352 goto out;
3353 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003354 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3355 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3356 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003357 btrfs_err(fs_info,
3358 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003359 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003360 ret = -EINVAL;
3361 goto out;
3362 }
3363
Ilya Dryomove4837f82012-03-27 17:09:17 +03003364 /* allow dup'ed data chunks only in mixed mode */
3365 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003366 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003367 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003368 ret = -EINVAL;
3369 goto out;
3370 }
3371
3372 /* allow to reduce meta or sys integrity only if force set */
3373 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003374 BTRFS_BLOCK_GROUP_RAID10 |
3375 BTRFS_BLOCK_GROUP_RAID5 |
3376 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003377 do {
3378 seq = read_seqbegin(&fs_info->profiles_lock);
3379
3380 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3381 (fs_info->avail_system_alloc_bits & allowed) &&
3382 !(bctl->sys.target & allowed)) ||
3383 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3384 (fs_info->avail_metadata_alloc_bits & allowed) &&
3385 !(bctl->meta.target & allowed))) {
3386 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003387 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003388 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003389 btrfs_err(fs_info, "balance will reduce metadata "
3390 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003391 ret = -EINVAL;
3392 goto out;
3393 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003394 }
Miao Xiede98ced2013-01-29 10:13:12 +00003395 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003396
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003397 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3398 int num_tolerated_disk_barrier_failures;
3399 u64 target = bctl->sys.target;
3400
3401 num_tolerated_disk_barrier_failures =
3402 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3403 if (num_tolerated_disk_barrier_failures > 0 &&
3404 (target &
3405 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3406 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3407 num_tolerated_disk_barrier_failures = 0;
3408 else if (num_tolerated_disk_barrier_failures > 1 &&
3409 (target &
3410 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3411 num_tolerated_disk_barrier_failures = 1;
3412
3413 fs_info->num_tolerated_disk_barrier_failures =
3414 num_tolerated_disk_barrier_failures;
3415 }
3416
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003417 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003418 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003419 goto out;
3420
Ilya Dryomov59641012012-01-16 22:04:48 +02003421 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3422 BUG_ON(ret == -EEXIST);
3423 set_balance_control(bctl);
3424 } else {
3425 BUG_ON(ret != -EEXIST);
3426 spin_lock(&fs_info->balance_lock);
3427 update_balance_args(bctl);
3428 spin_unlock(&fs_info->balance_lock);
3429 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003430
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003431 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003432 mutex_unlock(&fs_info->balance_mutex);
3433
3434 ret = __btrfs_balance(fs_info);
3435
3436 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003437 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003438
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003439 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3440 fs_info->num_tolerated_disk_barrier_failures =
3441 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3442 }
3443
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003444 if (bargs) {
3445 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003446 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003447 }
3448
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003449 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3450 balance_need_close(fs_info)) {
3451 __cancel_balance(fs_info);
3452 }
3453
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003454 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003455
3456 return ret;
3457out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003458 if (bctl->flags & BTRFS_BALANCE_RESUME)
3459 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003460 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003461 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003462 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3463 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003464 return ret;
3465}
3466
3467static int balance_kthread(void *data)
3468{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003469 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003470 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003471
3472 mutex_lock(&fs_info->volume_mutex);
3473 mutex_lock(&fs_info->balance_mutex);
3474
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003475 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003476 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003477 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003478 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003479
3480 mutex_unlock(&fs_info->balance_mutex);
3481 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003482
Ilya Dryomov59641012012-01-16 22:04:48 +02003483 return ret;
3484}
3485
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003486int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3487{
3488 struct task_struct *tsk;
3489
3490 spin_lock(&fs_info->balance_lock);
3491 if (!fs_info->balance_ctl) {
3492 spin_unlock(&fs_info->balance_lock);
3493 return 0;
3494 }
3495 spin_unlock(&fs_info->balance_lock);
3496
3497 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003498 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003499 return 0;
3500 }
3501
3502 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303503 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003504}
3505
Ilya Dryomov68310a52012-06-22 12:24:12 -06003506int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003507{
Ilya Dryomov59641012012-01-16 22:04:48 +02003508 struct btrfs_balance_control *bctl;
3509 struct btrfs_balance_item *item;
3510 struct btrfs_disk_balance_args disk_bargs;
3511 struct btrfs_path *path;
3512 struct extent_buffer *leaf;
3513 struct btrfs_key key;
3514 int ret;
3515
3516 path = btrfs_alloc_path();
3517 if (!path)
3518 return -ENOMEM;
3519
Ilya Dryomov68310a52012-06-22 12:24:12 -06003520 key.objectid = BTRFS_BALANCE_OBJECTID;
3521 key.type = BTRFS_BALANCE_ITEM_KEY;
3522 key.offset = 0;
3523
3524 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3525 if (ret < 0)
3526 goto out;
3527 if (ret > 0) { /* ret = -ENOENT; */
3528 ret = 0;
3529 goto out;
3530 }
3531
Ilya Dryomov59641012012-01-16 22:04:48 +02003532 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3533 if (!bctl) {
3534 ret = -ENOMEM;
3535 goto out;
3536 }
3537
Ilya Dryomov59641012012-01-16 22:04:48 +02003538 leaf = path->nodes[0];
3539 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3540
Ilya Dryomov68310a52012-06-22 12:24:12 -06003541 bctl->fs_info = fs_info;
3542 bctl->flags = btrfs_balance_flags(leaf, item);
3543 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003544
3545 btrfs_balance_data(leaf, item, &disk_bargs);
3546 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3547 btrfs_balance_meta(leaf, item, &disk_bargs);
3548 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3549 btrfs_balance_sys(leaf, item, &disk_bargs);
3550 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3551
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003552 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3553
Ilya Dryomov68310a52012-06-22 12:24:12 -06003554 mutex_lock(&fs_info->volume_mutex);
3555 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003556
Ilya Dryomov68310a52012-06-22 12:24:12 -06003557 set_balance_control(bctl);
3558
3559 mutex_unlock(&fs_info->balance_mutex);
3560 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003561out:
3562 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003563 return ret;
3564}
3565
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003566int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3567{
3568 int ret = 0;
3569
3570 mutex_lock(&fs_info->balance_mutex);
3571 if (!fs_info->balance_ctl) {
3572 mutex_unlock(&fs_info->balance_mutex);
3573 return -ENOTCONN;
3574 }
3575
3576 if (atomic_read(&fs_info->balance_running)) {
3577 atomic_inc(&fs_info->balance_pause_req);
3578 mutex_unlock(&fs_info->balance_mutex);
3579
3580 wait_event(fs_info->balance_wait_q,
3581 atomic_read(&fs_info->balance_running) == 0);
3582
3583 mutex_lock(&fs_info->balance_mutex);
3584 /* we are good with balance_ctl ripped off from under us */
3585 BUG_ON(atomic_read(&fs_info->balance_running));
3586 atomic_dec(&fs_info->balance_pause_req);
3587 } else {
3588 ret = -ENOTCONN;
3589 }
3590
3591 mutex_unlock(&fs_info->balance_mutex);
3592 return ret;
3593}
3594
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003595int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3596{
Ilya Dryomove649e582013-10-10 20:40:21 +03003597 if (fs_info->sb->s_flags & MS_RDONLY)
3598 return -EROFS;
3599
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003600 mutex_lock(&fs_info->balance_mutex);
3601 if (!fs_info->balance_ctl) {
3602 mutex_unlock(&fs_info->balance_mutex);
3603 return -ENOTCONN;
3604 }
3605
3606 atomic_inc(&fs_info->balance_cancel_req);
3607 /*
3608 * if we are running just wait and return, balance item is
3609 * deleted in btrfs_balance in this case
3610 */
3611 if (atomic_read(&fs_info->balance_running)) {
3612 mutex_unlock(&fs_info->balance_mutex);
3613 wait_event(fs_info->balance_wait_q,
3614 atomic_read(&fs_info->balance_running) == 0);
3615 mutex_lock(&fs_info->balance_mutex);
3616 } else {
3617 /* __cancel_balance needs volume_mutex */
3618 mutex_unlock(&fs_info->balance_mutex);
3619 mutex_lock(&fs_info->volume_mutex);
3620 mutex_lock(&fs_info->balance_mutex);
3621
3622 if (fs_info->balance_ctl)
3623 __cancel_balance(fs_info);
3624
3625 mutex_unlock(&fs_info->volume_mutex);
3626 }
3627
3628 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3629 atomic_dec(&fs_info->balance_cancel_req);
3630 mutex_unlock(&fs_info->balance_mutex);
3631 return 0;
3632}
3633
Stefan Behrens803b2f52013-08-15 17:11:21 +02003634static int btrfs_uuid_scan_kthread(void *data)
3635{
3636 struct btrfs_fs_info *fs_info = data;
3637 struct btrfs_root *root = fs_info->tree_root;
3638 struct btrfs_key key;
3639 struct btrfs_key max_key;
3640 struct btrfs_path *path = NULL;
3641 int ret = 0;
3642 struct extent_buffer *eb;
3643 int slot;
3644 struct btrfs_root_item root_item;
3645 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003646 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003647
3648 path = btrfs_alloc_path();
3649 if (!path) {
3650 ret = -ENOMEM;
3651 goto out;
3652 }
3653
3654 key.objectid = 0;
3655 key.type = BTRFS_ROOT_ITEM_KEY;
3656 key.offset = 0;
3657
3658 max_key.objectid = (u64)-1;
3659 max_key.type = BTRFS_ROOT_ITEM_KEY;
3660 max_key.offset = (u64)-1;
3661
Stefan Behrens803b2f52013-08-15 17:11:21 +02003662 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003663 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003664 if (ret) {
3665 if (ret > 0)
3666 ret = 0;
3667 break;
3668 }
3669
3670 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3671 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3672 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3673 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3674 goto skip;
3675
3676 eb = path->nodes[0];
3677 slot = path->slots[0];
3678 item_size = btrfs_item_size_nr(eb, slot);
3679 if (item_size < sizeof(root_item))
3680 goto skip;
3681
Stefan Behrens803b2f52013-08-15 17:11:21 +02003682 read_extent_buffer(eb, &root_item,
3683 btrfs_item_ptr_offset(eb, slot),
3684 (int)sizeof(root_item));
3685 if (btrfs_root_refs(&root_item) == 0)
3686 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003687
3688 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3689 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3690 if (trans)
3691 goto update_tree;
3692
3693 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003694 /*
3695 * 1 - subvol uuid item
3696 * 1 - received_subvol uuid item
3697 */
3698 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3699 if (IS_ERR(trans)) {
3700 ret = PTR_ERR(trans);
3701 break;
3702 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003703 continue;
3704 } else {
3705 goto skip;
3706 }
3707update_tree:
3708 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003709 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3710 root_item.uuid,
3711 BTRFS_UUID_KEY_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
3720 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003721 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3722 root_item.received_uuid,
3723 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3724 key.objectid);
3725 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003726 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003727 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003728 break;
3729 }
3730 }
3731
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003732skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003733 if (trans) {
3734 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003735 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003736 if (ret)
3737 break;
3738 }
3739
Stefan Behrens803b2f52013-08-15 17:11:21 +02003740 btrfs_release_path(path);
3741 if (key.offset < (u64)-1) {
3742 key.offset++;
3743 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3744 key.offset = 0;
3745 key.type = BTRFS_ROOT_ITEM_KEY;
3746 } else if (key.objectid < (u64)-1) {
3747 key.offset = 0;
3748 key.type = BTRFS_ROOT_ITEM_KEY;
3749 key.objectid++;
3750 } else {
3751 break;
3752 }
3753 cond_resched();
3754 }
3755
3756out:
3757 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003758 if (trans && !IS_ERR(trans))
3759 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003760 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003761 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003762 else
3763 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003764 up(&fs_info->uuid_tree_rescan_sem);
3765 return 0;
3766}
3767
Stefan Behrens70f80172013-08-15 17:11:23 +02003768/*
3769 * Callback for btrfs_uuid_tree_iterate().
3770 * returns:
3771 * 0 check succeeded, the entry is not outdated.
3772 * < 0 if an error occured.
3773 * > 0 if the check failed, which means the caller shall remove the entry.
3774 */
3775static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3776 u8 *uuid, u8 type, u64 subid)
3777{
3778 struct btrfs_key key;
3779 int ret = 0;
3780 struct btrfs_root *subvol_root;
3781
3782 if (type != BTRFS_UUID_KEY_SUBVOL &&
3783 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3784 goto out;
3785
3786 key.objectid = subid;
3787 key.type = BTRFS_ROOT_ITEM_KEY;
3788 key.offset = (u64)-1;
3789 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3790 if (IS_ERR(subvol_root)) {
3791 ret = PTR_ERR(subvol_root);
3792 if (ret == -ENOENT)
3793 ret = 1;
3794 goto out;
3795 }
3796
3797 switch (type) {
3798 case BTRFS_UUID_KEY_SUBVOL:
3799 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3800 ret = 1;
3801 break;
3802 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3803 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3804 BTRFS_UUID_SIZE))
3805 ret = 1;
3806 break;
3807 }
3808
3809out:
3810 return ret;
3811}
3812
3813static int btrfs_uuid_rescan_kthread(void *data)
3814{
3815 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3816 int ret;
3817
3818 /*
3819 * 1st step is to iterate through the existing UUID tree and
3820 * to delete all entries that contain outdated data.
3821 * 2nd step is to add all missing entries to the UUID tree.
3822 */
3823 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3824 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003825 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003826 up(&fs_info->uuid_tree_rescan_sem);
3827 return ret;
3828 }
3829 return btrfs_uuid_scan_kthread(data);
3830}
3831
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003832int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3833{
3834 struct btrfs_trans_handle *trans;
3835 struct btrfs_root *tree_root = fs_info->tree_root;
3836 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003837 struct task_struct *task;
3838 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003839
3840 /*
3841 * 1 - root node
3842 * 1 - root item
3843 */
3844 trans = btrfs_start_transaction(tree_root, 2);
3845 if (IS_ERR(trans))
3846 return PTR_ERR(trans);
3847
3848 uuid_root = btrfs_create_tree(trans, fs_info,
3849 BTRFS_UUID_TREE_OBJECTID);
3850 if (IS_ERR(uuid_root)) {
3851 btrfs_abort_transaction(trans, tree_root,
3852 PTR_ERR(uuid_root));
3853 return PTR_ERR(uuid_root);
3854 }
3855
3856 fs_info->uuid_root = uuid_root;
3857
Stefan Behrens803b2f52013-08-15 17:11:21 +02003858 ret = btrfs_commit_transaction(trans, tree_root);
3859 if (ret)
3860 return ret;
3861
3862 down(&fs_info->uuid_tree_rescan_sem);
3863 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3864 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003865 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003866 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003867 up(&fs_info->uuid_tree_rescan_sem);
3868 return PTR_ERR(task);
3869 }
3870
3871 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003872}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003873
Stefan Behrens70f80172013-08-15 17:11:23 +02003874int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3875{
3876 struct task_struct *task;
3877
3878 down(&fs_info->uuid_tree_rescan_sem);
3879 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3880 if (IS_ERR(task)) {
3881 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003882 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003883 up(&fs_info->uuid_tree_rescan_sem);
3884 return PTR_ERR(task);
3885 }
3886
3887 return 0;
3888}
3889
Chris Mason8f18cf12008-04-25 16:53:30 -04003890/*
3891 * shrinking a device means finding all of the device extents past
3892 * the new size, and then following the back refs to the chunks.
3893 * The chunk relocation code actually frees the device extent
3894 */
3895int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3896{
3897 struct btrfs_trans_handle *trans;
3898 struct btrfs_root *root = device->dev_root;
3899 struct btrfs_dev_extent *dev_extent = NULL;
3900 struct btrfs_path *path;
3901 u64 length;
3902 u64 chunk_tree;
3903 u64 chunk_objectid;
3904 u64 chunk_offset;
3905 int ret;
3906 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003907 int failed = 0;
3908 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003909 struct extent_buffer *l;
3910 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003911 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003912 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003913 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003914 u64 diff = device->total_bytes - new_size;
3915
Stefan Behrens63a212a2012-11-05 18:29:28 +01003916 if (device->is_tgtdev_for_dev_replace)
3917 return -EINVAL;
3918
Chris Mason8f18cf12008-04-25 16:53:30 -04003919 path = btrfs_alloc_path();
3920 if (!path)
3921 return -ENOMEM;
3922
Chris Mason8f18cf12008-04-25 16:53:30 -04003923 path->reada = 2;
3924
Chris Mason7d9eb122008-07-08 14:19:17 -04003925 lock_chunks(root);
3926
Chris Mason8f18cf12008-04-25 16:53:30 -04003927 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003928 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003929 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003930 spin_lock(&root->fs_info->free_chunk_lock);
3931 root->fs_info->free_chunk_space -= diff;
3932 spin_unlock(&root->fs_info->free_chunk_lock);
3933 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003934 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003935
Josef Bacikba1bf482009-09-11 16:11:19 -04003936again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003937 key.objectid = device->devid;
3938 key.offset = (u64)-1;
3939 key.type = BTRFS_DEV_EXTENT_KEY;
3940
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003941 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003942 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3943 if (ret < 0)
3944 goto done;
3945
3946 ret = btrfs_previous_item(root, path, 0, key.type);
3947 if (ret < 0)
3948 goto done;
3949 if (ret) {
3950 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003951 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003952 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003953 }
3954
3955 l = path->nodes[0];
3956 slot = path->slots[0];
3957 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3958
Josef Bacikba1bf482009-09-11 16:11:19 -04003959 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003960 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003961 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003962 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003963
3964 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3965 length = btrfs_dev_extent_length(l, dev_extent);
3966
Josef Bacikba1bf482009-09-11 16:11:19 -04003967 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003968 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003969 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003970 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003971
3972 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3973 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3974 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003975 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003976
3977 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3978 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003979 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003980 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003981 if (ret == -ENOSPC)
3982 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003983 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003984
3985 if (failed && !retried) {
3986 failed = 0;
3987 retried = true;
3988 goto again;
3989 } else if (failed && retried) {
3990 ret = -ENOSPC;
3991 lock_chunks(root);
3992
3993 device->total_bytes = old_size;
3994 if (device->writeable)
3995 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003996 spin_lock(&root->fs_info->free_chunk_lock);
3997 root->fs_info->free_chunk_space += diff;
3998 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003999 unlock_chunks(root);
4000 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004001 }
4002
Chris Balld6397ba2009-04-27 07:29:03 -04004003 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004004 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004005 if (IS_ERR(trans)) {
4006 ret = PTR_ERR(trans);
4007 goto done;
4008 }
4009
Chris Balld6397ba2009-04-27 07:29:03 -04004010 lock_chunks(root);
4011
4012 device->disk_total_bytes = new_size;
4013 /* Now btrfs_update_device() will change the on-disk size. */
4014 ret = btrfs_update_device(trans, device);
4015 if (ret) {
4016 unlock_chunks(root);
4017 btrfs_end_transaction(trans, root);
4018 goto done;
4019 }
4020 WARN_ON(diff > old_total);
4021 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4022 unlock_chunks(root);
4023 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004024done:
4025 btrfs_free_path(path);
4026 return ret;
4027}
4028
Li Zefan125ccb02011-12-08 15:07:24 +08004029static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004030 struct btrfs_key *key,
4031 struct btrfs_chunk *chunk, int item_size)
4032{
David Sterba6c417612011-04-13 15:41:04 +02004033 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004034 struct btrfs_disk_key disk_key;
4035 u32 array_size;
4036 u8 *ptr;
4037
4038 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004039 if (array_size + item_size + sizeof(disk_key)
4040 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
Chris Mason0b86a832008-03-24 15:01:56 -04004041 return -EFBIG;
4042
4043 ptr = super_copy->sys_chunk_array + array_size;
4044 btrfs_cpu_key_to_disk(&disk_key, key);
4045 memcpy(ptr, &disk_key, sizeof(disk_key));
4046 ptr += sizeof(disk_key);
4047 memcpy(ptr, chunk, item_size);
4048 item_size += sizeof(disk_key);
4049 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4050 return 0;
4051}
4052
Miao Xieb2117a32011-01-05 10:07:28 +00004053/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004054 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004055 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004056static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004057{
Arne Jansen73c5de02011-04-12 12:07:57 +02004058 const struct btrfs_device_info *di_a = a;
4059 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004060
Arne Jansen73c5de02011-04-12 12:07:57 +02004061 if (di_a->max_avail > di_b->max_avail)
4062 return -1;
4063 if (di_a->max_avail < di_b->max_avail)
4064 return 1;
4065 if (di_a->total_avail > di_b->total_avail)
4066 return -1;
4067 if (di_a->total_avail < di_b->total_avail)
4068 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004069 return 0;
4070}
4071
Eric Sandeen48a3b632013-04-25 20:41:01 +00004072static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004073 [BTRFS_RAID_RAID10] = {
4074 .sub_stripes = 2,
4075 .dev_stripes = 1,
4076 .devs_max = 0, /* 0 == as many as possible */
4077 .devs_min = 4,
4078 .devs_increment = 2,
4079 .ncopies = 2,
4080 },
4081 [BTRFS_RAID_RAID1] = {
4082 .sub_stripes = 1,
4083 .dev_stripes = 1,
4084 .devs_max = 2,
4085 .devs_min = 2,
4086 .devs_increment = 2,
4087 .ncopies = 2,
4088 },
4089 [BTRFS_RAID_DUP] = {
4090 .sub_stripes = 1,
4091 .dev_stripes = 2,
4092 .devs_max = 1,
4093 .devs_min = 1,
4094 .devs_increment = 1,
4095 .ncopies = 2,
4096 },
4097 [BTRFS_RAID_RAID0] = {
4098 .sub_stripes = 1,
4099 .dev_stripes = 1,
4100 .devs_max = 0,
4101 .devs_min = 2,
4102 .devs_increment = 1,
4103 .ncopies = 1,
4104 },
4105 [BTRFS_RAID_SINGLE] = {
4106 .sub_stripes = 1,
4107 .dev_stripes = 1,
4108 .devs_max = 1,
4109 .devs_min = 1,
4110 .devs_increment = 1,
4111 .ncopies = 1,
4112 },
Chris Masone942f882013-02-20 14:06:05 -05004113 [BTRFS_RAID_RAID5] = {
4114 .sub_stripes = 1,
4115 .dev_stripes = 1,
4116 .devs_max = 0,
4117 .devs_min = 2,
4118 .devs_increment = 1,
4119 .ncopies = 2,
4120 },
4121 [BTRFS_RAID_RAID6] = {
4122 .sub_stripes = 1,
4123 .dev_stripes = 1,
4124 .devs_max = 0,
4125 .devs_min = 3,
4126 .devs_increment = 1,
4127 .ncopies = 3,
4128 },
Liu Bo31e50222012-11-21 14:18:10 +00004129};
4130
David Woodhouse53b381b2013-01-29 18:40:14 -05004131static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4132{
4133 /* TODO allow them to set a preferred stripe size */
4134 return 64 * 1024;
4135}
4136
4137static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4138{
David Woodhouse53b381b2013-01-29 18:40:14 -05004139 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
4140 return;
4141
Miao Xieceda0862013-04-11 10:30:16 +00004142 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004143}
4144
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004145#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4146 - sizeof(struct btrfs_item) \
4147 - sizeof(struct btrfs_chunk)) \
4148 / sizeof(struct btrfs_stripe) + 1)
4149
4150#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4151 - 2 * sizeof(struct btrfs_disk_key) \
4152 - 2 * sizeof(struct btrfs_chunk)) \
4153 / sizeof(struct btrfs_stripe) + 1)
4154
Miao Xieb2117a32011-01-05 10:07:28 +00004155static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004156 struct btrfs_root *extent_root, u64 start,
4157 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004158{
4159 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004160 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4161 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004162 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004163 struct extent_map_tree *em_tree;
4164 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004165 struct btrfs_device_info *devices_info = NULL;
4166 u64 total_avail;
4167 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004168 int data_stripes; /* number of stripes that count for
4169 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004170 int sub_stripes; /* sub_stripes info for map */
4171 int dev_stripes; /* stripes per dev */
4172 int devs_max; /* max devs to use */
4173 int devs_min; /* min devs needed */
4174 int devs_increment; /* ndevs has to be a multiple of this */
4175 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004176 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004177 u64 max_stripe_size;
4178 u64 max_chunk_size;
4179 u64 stripe_size;
4180 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004181 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004182 int ndevs;
4183 int i;
4184 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004185 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004186
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004187 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004188
Miao Xieb2117a32011-01-05 10:07:28 +00004189 if (list_empty(&fs_devices->alloc_list))
4190 return -ENOSPC;
4191
Liu Bo31e50222012-11-21 14:18:10 +00004192 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004193
Liu Bo31e50222012-11-21 14:18:10 +00004194 sub_stripes = btrfs_raid_array[index].sub_stripes;
4195 dev_stripes = btrfs_raid_array[index].dev_stripes;
4196 devs_max = btrfs_raid_array[index].devs_max;
4197 devs_min = btrfs_raid_array[index].devs_min;
4198 devs_increment = btrfs_raid_array[index].devs_increment;
4199 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004200
4201 if (type & BTRFS_BLOCK_GROUP_DATA) {
4202 max_stripe_size = 1024 * 1024 * 1024;
4203 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004204 if (!devs_max)
4205 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004206 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004207 /* for larger filesystems, use larger metadata chunks */
4208 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4209 max_stripe_size = 1024 * 1024 * 1024;
4210 else
4211 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004212 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004213 if (!devs_max)
4214 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004215 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004216 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004217 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004218 if (!devs_max)
4219 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004220 } else {
David Sterba351fd352014-05-15 16:48:20 +02004221 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004222 type);
4223 BUG_ON(1);
4224 }
4225
4226 /* we don't want a chunk larger than 10% of writeable space */
4227 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4228 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004229
4230 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4231 GFP_NOFS);
4232 if (!devices_info)
4233 return -ENOMEM;
4234
Arne Jansen73c5de02011-04-12 12:07:57 +02004235 cur = fs_devices->alloc_list.next;
4236
4237 /*
4238 * in the first pass through the devices list, we gather information
4239 * about the available holes on each device.
4240 */
4241 ndevs = 0;
4242 while (cur != &fs_devices->alloc_list) {
4243 struct btrfs_device *device;
4244 u64 max_avail;
4245 u64 dev_offset;
4246
4247 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4248
4249 cur = cur->next;
4250
4251 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004252 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004253 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004254 continue;
4255 }
4256
Stefan Behrens63a212a2012-11-05 18:29:28 +01004257 if (!device->in_fs_metadata ||
4258 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004259 continue;
4260
4261 if (device->total_bytes > device->bytes_used)
4262 total_avail = device->total_bytes - device->bytes_used;
4263 else
4264 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004265
4266 /* If there is no space on this device, skip it. */
4267 if (total_avail == 0)
4268 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004269
Josef Bacik6df9a952013-06-27 13:22:46 -04004270 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004271 max_stripe_size * dev_stripes,
4272 &dev_offset, &max_avail);
4273 if (ret && ret != -ENOSPC)
4274 goto error;
4275
4276 if (ret == 0)
4277 max_avail = max_stripe_size * dev_stripes;
4278
4279 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4280 continue;
4281
Eric Sandeen063d0062013-01-31 00:55:01 +00004282 if (ndevs == fs_devices->rw_devices) {
4283 WARN(1, "%s: found more than %llu devices\n",
4284 __func__, fs_devices->rw_devices);
4285 break;
4286 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004287 devices_info[ndevs].dev_offset = dev_offset;
4288 devices_info[ndevs].max_avail = max_avail;
4289 devices_info[ndevs].total_avail = total_avail;
4290 devices_info[ndevs].dev = device;
4291 ++ndevs;
4292 }
4293
4294 /*
4295 * now sort the devices by hole size / available space
4296 */
4297 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4298 btrfs_cmp_device_info, NULL);
4299
4300 /* round down to number of usable stripes */
4301 ndevs -= ndevs % devs_increment;
4302
4303 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4304 ret = -ENOSPC;
4305 goto error;
4306 }
4307
4308 if (devs_max && ndevs > devs_max)
4309 ndevs = devs_max;
4310 /*
4311 * the primary goal is to maximize the number of stripes, so use as many
4312 * devices as possible, even if the stripes are not maximum sized.
4313 */
4314 stripe_size = devices_info[ndevs-1].max_avail;
4315 num_stripes = ndevs * dev_stripes;
4316
David Woodhouse53b381b2013-01-29 18:40:14 -05004317 /*
4318 * this will have to be fixed for RAID1 and RAID10 over
4319 * more drives
4320 */
4321 data_stripes = num_stripes / ncopies;
4322
David Woodhouse53b381b2013-01-29 18:40:14 -05004323 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4324 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4325 btrfs_super_stripesize(info->super_copy));
4326 data_stripes = num_stripes - 1;
4327 }
4328 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4329 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4330 btrfs_super_stripesize(info->super_copy));
4331 data_stripes = num_stripes - 2;
4332 }
Chris Mason86db2572013-02-20 16:23:40 -05004333
4334 /*
4335 * Use the number of data stripes to figure out how big this chunk
4336 * is really going to be in terms of logical address space,
4337 * and compare that answer with the max chunk size
4338 */
4339 if (stripe_size * data_stripes > max_chunk_size) {
4340 u64 mask = (1ULL << 24) - 1;
4341 stripe_size = max_chunk_size;
4342 do_div(stripe_size, data_stripes);
4343
4344 /* bump the answer up to a 16MB boundary */
4345 stripe_size = (stripe_size + mask) & ~mask;
4346
4347 /* but don't go higher than the limits we found
4348 * while searching for free extents
4349 */
4350 if (stripe_size > devices_info[ndevs-1].max_avail)
4351 stripe_size = devices_info[ndevs-1].max_avail;
4352 }
4353
Arne Jansen73c5de02011-04-12 12:07:57 +02004354 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004355
4356 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004357 do_div(stripe_size, raid_stripe_len);
4358 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004359
Miao Xieb2117a32011-01-05 10:07:28 +00004360 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4361 if (!map) {
4362 ret = -ENOMEM;
4363 goto error;
4364 }
4365 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004366
Arne Jansen73c5de02011-04-12 12:07:57 +02004367 for (i = 0; i < ndevs; ++i) {
4368 for (j = 0; j < dev_stripes; ++j) {
4369 int s = i * dev_stripes + j;
4370 map->stripes[s].dev = devices_info[i].dev;
4371 map->stripes[s].physical = devices_info[i].dev_offset +
4372 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004373 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004374 }
Chris Mason593060d2008-03-25 16:50:33 -04004375 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004376 map->stripe_len = raid_stripe_len;
4377 map->io_align = raid_stripe_len;
4378 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004379 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004380 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004381
David Woodhouse53b381b2013-01-29 18:40:14 -05004382 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004383
Arne Jansen73c5de02011-04-12 12:07:57 +02004384 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004385
David Sterba172ddd62011-04-21 00:48:27 +02004386 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004387 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004388 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004389 ret = -ENOMEM;
4390 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004391 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004392 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004393 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004394 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004395 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004396 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004397 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004398 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004399
Chris Mason0b86a832008-03-24 15:01:56 -04004400 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004401 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004402 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004403 if (!ret) {
4404 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4405 atomic_inc(&em->refs);
4406 }
Chris Mason890871b2009-09-02 16:24:52 -04004407 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004408 if (ret) {
4409 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004410 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004411 }
Yan Zheng2b820322008-11-17 21:11:30 -05004412
Josef Bacik04487482013-01-29 15:03:37 -05004413 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4414 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4415 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004416 if (ret)
4417 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004418
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004419 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004420 check_raid56_incompat_flag(extent_root->fs_info, type);
4421
Miao Xieb2117a32011-01-05 10:07:28 +00004422 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004423 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004424
Josef Bacik6df9a952013-06-27 13:22:46 -04004425error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004426 write_lock(&em_tree->lock);
4427 remove_extent_mapping(em_tree, em);
4428 write_unlock(&em_tree->lock);
4429
4430 /* One for our allocation */
4431 free_extent_map(em);
4432 /* One for the tree reference */
4433 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004434error:
Miao Xieb2117a32011-01-05 10:07:28 +00004435 kfree(devices_info);
4436 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004437}
4438
Josef Bacik6df9a952013-06-27 13:22:46 -04004439int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004440 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004441 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004442{
Yan Zheng2b820322008-11-17 21:11:30 -05004443 struct btrfs_key key;
4444 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4445 struct btrfs_device *device;
4446 struct btrfs_chunk *chunk;
4447 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004448 struct extent_map_tree *em_tree;
4449 struct extent_map *em;
4450 struct map_lookup *map;
4451 size_t item_size;
4452 u64 dev_offset;
4453 u64 stripe_size;
4454 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004455 int ret;
4456
Josef Bacik6df9a952013-06-27 13:22:46 -04004457 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4458 read_lock(&em_tree->lock);
4459 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4460 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004461
Josef Bacik6df9a952013-06-27 13:22:46 -04004462 if (!em) {
4463 btrfs_crit(extent_root->fs_info, "unable to find logical "
4464 "%Lu len %Lu", chunk_offset, chunk_size);
4465 return -EINVAL;
4466 }
4467
4468 if (em->start != chunk_offset || em->len != chunk_size) {
4469 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004470 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004471 chunk_size, em->start, em->len);
4472 free_extent_map(em);
4473 return -EINVAL;
4474 }
4475
4476 map = (struct map_lookup *)em->bdev;
4477 item_size = btrfs_chunk_item_size(map->num_stripes);
4478 stripe_size = em->orig_block_len;
4479
4480 chunk = kzalloc(item_size, GFP_NOFS);
4481 if (!chunk) {
4482 ret = -ENOMEM;
4483 goto out;
4484 }
4485
4486 for (i = 0; i < map->num_stripes; i++) {
4487 device = map->stripes[i].dev;
4488 dev_offset = map->stripes[i].physical;
4489
Yan Zheng2b820322008-11-17 21:11:30 -05004490 device->bytes_used += stripe_size;
4491 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004492 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004493 goto out;
4494 ret = btrfs_alloc_dev_extent(trans, device,
4495 chunk_root->root_key.objectid,
4496 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4497 chunk_offset, dev_offset,
4498 stripe_size);
4499 if (ret)
4500 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004501 }
4502
Josef Bacik2bf64752011-09-26 17:12:22 -04004503 spin_lock(&extent_root->fs_info->free_chunk_lock);
4504 extent_root->fs_info->free_chunk_space -= (stripe_size *
4505 map->num_stripes);
4506 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4507
Yan Zheng2b820322008-11-17 21:11:30 -05004508 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004509 for (i = 0; i < map->num_stripes; i++) {
4510 device = map->stripes[i].dev;
4511 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004512
4513 btrfs_set_stack_stripe_devid(stripe, device->devid);
4514 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4515 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4516 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004517 }
4518
4519 btrfs_set_stack_chunk_length(chunk, chunk_size);
4520 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4521 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4522 btrfs_set_stack_chunk_type(chunk, map->type);
4523 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4524 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4525 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4526 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4527 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4528
4529 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4530 key.type = BTRFS_CHUNK_ITEM_KEY;
4531 key.offset = chunk_offset;
4532
4533 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004534 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4535 /*
4536 * TODO: Cleanup of inserted chunk root in case of
4537 * failure.
4538 */
Li Zefan125ccb02011-12-08 15:07:24 +08004539 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004540 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004541 }
liubo1abe9b82011-03-24 11:18:59 +00004542
Josef Bacik6df9a952013-06-27 13:22:46 -04004543out:
Yan Zheng2b820322008-11-17 21:11:30 -05004544 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004545 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004546 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004547}
4548
4549/*
4550 * Chunk allocation falls into two parts. The first part does works
4551 * that make the new allocated chunk useable, but not do any operation
4552 * that modifies the chunk tree. The second part does the works that
4553 * require modifying the chunk tree. This division is important for the
4554 * bootstrap process of adding storage to a seed btrfs.
4555 */
4556int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4557 struct btrfs_root *extent_root, u64 type)
4558{
4559 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004560
Josef Bacik6df9a952013-06-27 13:22:46 -04004561 chunk_offset = find_next_chunk(extent_root->fs_info);
4562 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004563}
4564
Chris Masond3977122009-01-05 21:25:51 -05004565static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004566 struct btrfs_root *root,
4567 struct btrfs_device *device)
4568{
4569 u64 chunk_offset;
4570 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004571 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004572 struct btrfs_fs_info *fs_info = root->fs_info;
4573 struct btrfs_root *extent_root = fs_info->extent_root;
4574 int ret;
4575
Josef Bacik6df9a952013-06-27 13:22:46 -04004576 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004577 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004578 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4579 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004580 if (ret)
4581 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004582
Josef Bacik6df9a952013-06-27 13:22:46 -04004583 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004584 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004585 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4586 alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004587 if (ret) {
4588 btrfs_abort_transaction(trans, root, ret);
4589 goto out;
4590 }
Yan Zheng2b820322008-11-17 21:11:30 -05004591
4592 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004593 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004594 btrfs_abort_transaction(trans, root, ret);
David Sterba005d6422012-09-18 07:52:32 -06004595out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004596 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004597}
4598
Miao Xied20983b2014-07-03 18:22:13 +08004599static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4600{
4601 int max_errors;
4602
4603 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4604 BTRFS_BLOCK_GROUP_RAID10 |
4605 BTRFS_BLOCK_GROUP_RAID5 |
4606 BTRFS_BLOCK_GROUP_DUP)) {
4607 max_errors = 1;
4608 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4609 max_errors = 2;
4610 } else {
4611 max_errors = 0;
4612 }
4613
4614 return max_errors;
4615}
4616
Yan Zheng2b820322008-11-17 21:11:30 -05004617int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4618{
4619 struct extent_map *em;
4620 struct map_lookup *map;
4621 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4622 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004623 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004624 int i;
4625
Chris Mason890871b2009-09-02 16:24:52 -04004626 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004627 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004628 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004629 if (!em)
4630 return 1;
4631
4632 map = (struct map_lookup *)em->bdev;
4633 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004634 if (map->stripes[i].dev->missing) {
4635 miss_ndevs++;
4636 continue;
4637 }
4638
Yan Zheng2b820322008-11-17 21:11:30 -05004639 if (!map->stripes[i].dev->writeable) {
4640 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004641 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004642 }
4643 }
Miao Xied20983b2014-07-03 18:22:13 +08004644
4645 /*
4646 * If the number of missing devices is larger than max errors,
4647 * we can not write the data into that chunk successfully, so
4648 * set it readonly.
4649 */
4650 if (miss_ndevs > btrfs_chunk_max_errors(map))
4651 readonly = 1;
4652end:
Yan Zheng2b820322008-11-17 21:11:30 -05004653 free_extent_map(em);
4654 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004655}
4656
4657void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4658{
David Sterbaa8067e02011-04-21 00:34:43 +02004659 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004660}
4661
4662void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4663{
4664 struct extent_map *em;
4665
Chris Masond3977122009-01-05 21:25:51 -05004666 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004667 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004668 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4669 if (em)
4670 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004671 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004672 if (!em)
4673 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004674 /* once for us */
4675 free_extent_map(em);
4676 /* once for the tree */
4677 free_extent_map(em);
4678 }
4679}
4680
Stefan Behrens5d964052012-11-05 14:59:07 +01004681int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004682{
Stefan Behrens5d964052012-11-05 14:59:07 +01004683 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004684 struct extent_map *em;
4685 struct map_lookup *map;
4686 struct extent_map_tree *em_tree = &map_tree->map_tree;
4687 int ret;
4688
Chris Mason890871b2009-09-02 16:24:52 -04004689 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004690 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004691 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004692
Josef Bacikfb7669b2013-04-23 10:53:18 -04004693 /*
4694 * We could return errors for these cases, but that could get ugly and
4695 * we'd probably do the same thing which is just not do anything else
4696 * and exit, so return 1 so the callers don't try to use other copies.
4697 */
4698 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004699 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004700 logical+len);
4701 return 1;
4702 }
4703
4704 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004705 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004706 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004707 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004708 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004709 return 1;
4710 }
4711
Chris Masonf1885912008-04-09 16:28:12 -04004712 map = (struct map_lookup *)em->bdev;
4713 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4714 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004715 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4716 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004717 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4718 ret = 2;
4719 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4720 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004721 else
4722 ret = 1;
4723 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004724
4725 btrfs_dev_replace_lock(&fs_info->dev_replace);
4726 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4727 ret++;
4728 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4729
Chris Masonf1885912008-04-09 16:28:12 -04004730 return ret;
4731}
4732
David Woodhouse53b381b2013-01-29 18:40:14 -05004733unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4734 struct btrfs_mapping_tree *map_tree,
4735 u64 logical)
4736{
4737 struct extent_map *em;
4738 struct map_lookup *map;
4739 struct extent_map_tree *em_tree = &map_tree->map_tree;
4740 unsigned long len = root->sectorsize;
4741
4742 read_lock(&em_tree->lock);
4743 em = lookup_extent_mapping(em_tree, logical, len);
4744 read_unlock(&em_tree->lock);
4745 BUG_ON(!em);
4746
4747 BUG_ON(em->start > logical || em->start + em->len < logical);
4748 map = (struct map_lookup *)em->bdev;
4749 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4750 BTRFS_BLOCK_GROUP_RAID6)) {
4751 len = map->stripe_len * nr_data_stripes(map);
4752 }
4753 free_extent_map(em);
4754 return len;
4755}
4756
4757int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4758 u64 logical, u64 len, int mirror_num)
4759{
4760 struct extent_map *em;
4761 struct map_lookup *map;
4762 struct extent_map_tree *em_tree = &map_tree->map_tree;
4763 int ret = 0;
4764
4765 read_lock(&em_tree->lock);
4766 em = lookup_extent_mapping(em_tree, logical, len);
4767 read_unlock(&em_tree->lock);
4768 BUG_ON(!em);
4769
4770 BUG_ON(em->start > logical || em->start + em->len < logical);
4771 map = (struct map_lookup *)em->bdev;
4772 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4773 BTRFS_BLOCK_GROUP_RAID6))
4774 ret = 1;
4775 free_extent_map(em);
4776 return ret;
4777}
4778
Stefan Behrens30d98612012-11-06 14:52:18 +01004779static int find_live_mirror(struct btrfs_fs_info *fs_info,
4780 struct map_lookup *map, int first, int num,
4781 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004782{
4783 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004784 int tolerance;
4785 struct btrfs_device *srcdev;
4786
4787 if (dev_replace_is_ongoing &&
4788 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4789 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4790 srcdev = fs_info->dev_replace.srcdev;
4791 else
4792 srcdev = NULL;
4793
4794 /*
4795 * try to avoid the drive that is the source drive for a
4796 * dev-replace procedure, only choose it if no other non-missing
4797 * mirror is available
4798 */
4799 for (tolerance = 0; tolerance < 2; tolerance++) {
4800 if (map->stripes[optimal].dev->bdev &&
4801 (tolerance || map->stripes[optimal].dev != srcdev))
4802 return optimal;
4803 for (i = first; i < first + num; i++) {
4804 if (map->stripes[i].dev->bdev &&
4805 (tolerance || map->stripes[i].dev != srcdev))
4806 return i;
4807 }
Chris Masondfe25022008-05-13 13:46:40 -04004808 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004809
Chris Masondfe25022008-05-13 13:46:40 -04004810 /* we couldn't find one that doesn't fail. Just return something
4811 * and the io error handling code will clean up eventually
4812 */
4813 return optimal;
4814}
4815
David Woodhouse53b381b2013-01-29 18:40:14 -05004816static inline int parity_smaller(u64 a, u64 b)
4817{
4818 return a > b;
4819}
4820
4821/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4822static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4823{
4824 struct btrfs_bio_stripe s;
4825 int i;
4826 u64 l;
4827 int again = 1;
4828
4829 while (again) {
4830 again = 0;
4831 for (i = 0; i < bbio->num_stripes - 1; i++) {
4832 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4833 s = bbio->stripes[i];
4834 l = raid_map[i];
4835 bbio->stripes[i] = bbio->stripes[i+1];
4836 raid_map[i] = raid_map[i+1];
4837 bbio->stripes[i+1] = s;
4838 raid_map[i+1] = l;
4839 again = 1;
4840 }
4841 }
4842 }
4843}
4844
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004845static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004846 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004847 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004848 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004849{
4850 struct extent_map *em;
4851 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004852 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004853 struct extent_map_tree *em_tree = &map_tree->map_tree;
4854 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004855 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004856 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004857 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004858 u64 stripe_nr_orig;
4859 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004860 u64 stripe_len;
4861 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004862 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004863 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004864 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004865 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004866 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004867 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004868 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4869 int dev_replace_is_ongoing = 0;
4870 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004871 int patch_the_first_stripe_for_dev_replace = 0;
4872 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004873 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004874
Chris Mason890871b2009-09-02 16:24:52 -04004875 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004876 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004877 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004878
Chris Mason3b951512008-04-17 11:29:12 -04004879 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004880 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004881 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004882 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004883 }
Chris Mason0b86a832008-03-24 15:01:56 -04004884
Josef Bacik9bb91872013-04-19 23:45:33 -04004885 if (em->start > logical || em->start + em->len < logical) {
4886 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004887 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004888 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004889 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004890 return -EINVAL;
4891 }
4892
Chris Mason0b86a832008-03-24 15:01:56 -04004893 map = (struct map_lookup *)em->bdev;
4894 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004895
David Woodhouse53b381b2013-01-29 18:40:14 -05004896 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004897 stripe_nr = offset;
4898 /*
4899 * stripe_nr counts the total number of stripes we have to stride
4900 * to get to this block
4901 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004902 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004903
David Woodhouse53b381b2013-01-29 18:40:14 -05004904 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004905 BUG_ON(offset < stripe_offset);
4906
4907 /* stripe_offset is the offset of this block in its stripe*/
4908 stripe_offset = offset - stripe_offset;
4909
David Woodhouse53b381b2013-01-29 18:40:14 -05004910 /* if we're here for raid56, we need to know the stripe aligned start */
4911 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4912 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4913 raid56_full_stripe_start = offset;
4914
4915 /* allow a write of a full stripe, but make sure we don't
4916 * allow straddling of stripes
4917 */
4918 do_div(raid56_full_stripe_start, full_stripe_len);
4919 raid56_full_stripe_start *= full_stripe_len;
4920 }
4921
4922 if (rw & REQ_DISCARD) {
4923 /* we don't discard raid56 yet */
4924 if (map->type &
4925 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4926 ret = -EOPNOTSUPP;
4927 goto out;
4928 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004929 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004930 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4931 u64 max_len;
4932 /* For writes to RAID[56], allow a full stripeset across all disks.
4933 For other RAID types and for RAID[56] reads, just allow a single
4934 stripe (on a single disk). */
4935 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4936 (rw & REQ_WRITE)) {
4937 max_len = stripe_len * nr_data_stripes(map) -
4938 (offset - raid56_full_stripe_start);
4939 } else {
4940 /* we limit the length of each bio to what fits in a stripe */
4941 max_len = stripe_len - stripe_offset;
4942 }
4943 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004944 } else {
4945 *length = em->len - offset;
4946 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004947
David Woodhouse53b381b2013-01-29 18:40:14 -05004948 /* This is for when we're called from btrfs_merge_bio_hook() and all
4949 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004950 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004951 goto out;
4952
Stefan Behrens472262f2012-11-06 14:43:46 +01004953 btrfs_dev_replace_lock(dev_replace);
4954 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4955 if (!dev_replace_is_ongoing)
4956 btrfs_dev_replace_unlock(dev_replace);
4957
Stefan Behrensad6d6202012-11-06 15:06:47 +01004958 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4959 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4960 dev_replace->tgtdev != NULL) {
4961 /*
4962 * in dev-replace case, for repair case (that's the only
4963 * case where the mirror is selected explicitly when
4964 * calling btrfs_map_block), blocks left of the left cursor
4965 * can also be read from the target drive.
4966 * For REQ_GET_READ_MIRRORS, the target drive is added as
4967 * the last one to the array of stripes. For READ, it also
4968 * needs to be supported using the same mirror number.
4969 * If the requested block is not left of the left cursor,
4970 * EIO is returned. This can happen because btrfs_num_copies()
4971 * returns one more in the dev-replace case.
4972 */
4973 u64 tmp_length = *length;
4974 struct btrfs_bio *tmp_bbio = NULL;
4975 int tmp_num_stripes;
4976 u64 srcdev_devid = dev_replace->srcdev->devid;
4977 int index_srcdev = 0;
4978 int found = 0;
4979 u64 physical_of_found = 0;
4980
4981 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004982 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004983 if (ret) {
4984 WARN_ON(tmp_bbio != NULL);
4985 goto out;
4986 }
4987
4988 tmp_num_stripes = tmp_bbio->num_stripes;
4989 if (mirror_num > tmp_num_stripes) {
4990 /*
4991 * REQ_GET_READ_MIRRORS does not contain this
4992 * mirror, that means that the requested area
4993 * is not left of the left cursor
4994 */
4995 ret = -EIO;
4996 kfree(tmp_bbio);
4997 goto out;
4998 }
4999
5000 /*
5001 * process the rest of the function using the mirror_num
5002 * of the source drive. Therefore look it up first.
5003 * At the end, patch the device pointer to the one of the
5004 * target drive.
5005 */
5006 for (i = 0; i < tmp_num_stripes; i++) {
5007 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5008 /*
5009 * In case of DUP, in order to keep it
5010 * simple, only add the mirror with the
5011 * lowest physical address
5012 */
5013 if (found &&
5014 physical_of_found <=
5015 tmp_bbio->stripes[i].physical)
5016 continue;
5017 index_srcdev = i;
5018 found = 1;
5019 physical_of_found =
5020 tmp_bbio->stripes[i].physical;
5021 }
5022 }
5023
5024 if (found) {
5025 mirror_num = index_srcdev + 1;
5026 patch_the_first_stripe_for_dev_replace = 1;
5027 physical_to_patch_in_first_stripe = physical_of_found;
5028 } else {
5029 WARN_ON(1);
5030 ret = -EIO;
5031 kfree(tmp_bbio);
5032 goto out;
5033 }
5034
5035 kfree(tmp_bbio);
5036 } else if (mirror_num > map->num_stripes) {
5037 mirror_num = 0;
5038 }
5039
Chris Masonf2d8d742008-04-21 10:03:05 -04005040 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005041 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005042 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005043 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005044 do_div(stripe_nr_end, map->stripe_len);
5045 stripe_end_offset = stripe_nr_end * map->stripe_len -
5046 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005047
Li Dongyangfce3bb92011-03-24 10:24:26 +00005048 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5049 if (rw & REQ_DISCARD)
5050 num_stripes = min_t(u64, map->num_stripes,
5051 stripe_nr_end - stripe_nr_orig);
5052 stripe_index = do_div(stripe_nr, map->num_stripes);
5053 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005054 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005055 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005056 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005057 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005058 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005059 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005060 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005061 current->pid % map->num_stripes,
5062 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005063 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005064 }
Chris Mason2fff7342008-04-29 14:12:09 -04005065
Chris Mason611f0e02008-04-03 16:29:03 -04005066 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005067 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005068 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005069 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005070 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005071 } else {
5072 mirror_num = 1;
5073 }
Chris Mason2fff7342008-04-29 14:12:09 -04005074
Chris Mason321aecc2008-04-16 10:49:51 -04005075 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5076 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005077
5078 stripe_index = do_div(stripe_nr, factor);
5079 stripe_index *= map->sub_stripes;
5080
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005081 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005082 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005083 else if (rw & REQ_DISCARD)
5084 num_stripes = min_t(u64, map->sub_stripes *
5085 (stripe_nr_end - stripe_nr_orig),
5086 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005087 else if (mirror_num)
5088 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005089 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005090 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005091 stripe_index = find_live_mirror(fs_info, map,
5092 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005093 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005094 current->pid % map->sub_stripes,
5095 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005096 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005097 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005098
5099 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5100 BTRFS_BLOCK_GROUP_RAID6)) {
5101 u64 tmp;
5102
5103 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
5104 && raid_map_ret) {
5105 int i, rot;
5106
5107 /* push stripe_nr back to the start of the full stripe */
5108 stripe_nr = raid56_full_stripe_start;
5109 do_div(stripe_nr, stripe_len);
5110
5111 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5112
5113 /* RAID[56] write or recovery. Return all stripes */
5114 num_stripes = map->num_stripes;
5115 max_errors = nr_parity_stripes(map);
5116
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05305117 raid_map = kmalloc_array(num_stripes, sizeof(u64),
David Woodhouse53b381b2013-01-29 18:40:14 -05005118 GFP_NOFS);
5119 if (!raid_map) {
5120 ret = -ENOMEM;
5121 goto out;
5122 }
5123
5124 /* Work out the disk rotation on this stripe-set */
5125 tmp = stripe_nr;
5126 rot = do_div(tmp, num_stripes);
5127
5128 /* Fill in the logical address of each stripe */
5129 tmp = stripe_nr * nr_data_stripes(map);
5130 for (i = 0; i < nr_data_stripes(map); i++)
5131 raid_map[(i+rot) % num_stripes] =
5132 em->start + (tmp + i) * map->stripe_len;
5133
5134 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5135 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5136 raid_map[(i+rot+1) % num_stripes] =
5137 RAID6_Q_STRIPE;
5138
5139 *length = map->stripe_len;
5140 stripe_index = 0;
5141 stripe_offset = 0;
5142 } else {
5143 /*
5144 * Mirror #0 or #1 means the original data block.
5145 * Mirror #2 is RAID5 parity block.
5146 * Mirror #3 is RAID6 Q block.
5147 */
5148 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5149 if (mirror_num > 1)
5150 stripe_index = nr_data_stripes(map) +
5151 mirror_num - 2;
5152
5153 /* We distribute the parity blocks across stripes */
5154 tmp = stripe_nr + stripe_index;
5155 stripe_index = do_div(tmp, map->num_stripes);
5156 }
Chris Mason8790d502008-04-03 16:29:03 -04005157 } else {
5158 /*
5159 * after this do_div call, stripe_nr is the number of stripes
5160 * on this device we have to walk to find the data, and
5161 * stripe_index is the number of our device in the stripe array
5162 */
5163 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005164 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005165 }
Chris Mason593060d2008-03-25 16:50:33 -04005166 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005167
Stefan Behrens472262f2012-11-06 14:43:46 +01005168 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005169 if (dev_replace_is_ongoing) {
5170 if (rw & (REQ_WRITE | REQ_DISCARD))
5171 num_alloc_stripes <<= 1;
5172 if (rw & REQ_GET_READ_MIRRORS)
5173 num_alloc_stripes++;
5174 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005175 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08005176 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04005177 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08005178 ret = -ENOMEM;
5179 goto out;
5180 }
5181 atomic_set(&bbio->error, 0);
5182
Li Dongyangfce3bb92011-03-24 10:24:26 +00005183 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08005184 int factor = 0;
5185 int sub_stripes = 0;
5186 u64 stripes_per_dev = 0;
5187 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005188 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005189
5190 if (map->type &
5191 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5192 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5193 sub_stripes = 1;
5194 else
5195 sub_stripes = map->sub_stripes;
5196
5197 factor = map->num_stripes / sub_stripes;
5198 stripes_per_dev = div_u64_rem(stripe_nr_end -
5199 stripe_nr_orig,
5200 factor,
5201 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005202 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5203 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005204 }
5205
Li Dongyangfce3bb92011-03-24 10:24:26 +00005206 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005207 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005208 map->stripes[stripe_index].physical +
5209 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005210 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005211
Li Zefanec9ef7a2011-12-01 14:06:42 +08005212 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5213 BTRFS_BLOCK_GROUP_RAID10)) {
5214 bbio->stripes[i].length = stripes_per_dev *
5215 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005216
Li Zefanec9ef7a2011-12-01 14:06:42 +08005217 if (i / sub_stripes < remaining_stripes)
5218 bbio->stripes[i].length +=
5219 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005220
5221 /*
5222 * Special for the first stripe and
5223 * the last stripe:
5224 *
5225 * |-------|...|-------|
5226 * |----------|
5227 * off end_off
5228 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005229 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005230 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005231 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005232
5233 if (stripe_index >= last_stripe &&
5234 stripe_index <= (last_stripe +
5235 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005236 bbio->stripes[i].length -=
5237 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005238
Li Zefanec9ef7a2011-12-01 14:06:42 +08005239 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005240 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005241 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005242 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005243
5244 stripe_index++;
5245 if (stripe_index == map->num_stripes) {
5246 /* This could only happen for RAID0/10 */
5247 stripe_index = 0;
5248 stripe_nr++;
5249 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005250 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005251 } else {
5252 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005253 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005254 map->stripes[stripe_index].physical +
5255 stripe_offset +
5256 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005257 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005258 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005259 stripe_index++;
5260 }
Chris Mason593060d2008-03-25 16:50:33 -04005261 }
Li Zefande11cc12011-12-01 12:55:47 +08005262
Miao Xied20983b2014-07-03 18:22:13 +08005263 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5264 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005265
Stefan Behrens472262f2012-11-06 14:43:46 +01005266 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5267 dev_replace->tgtdev != NULL) {
5268 int index_where_to_add;
5269 u64 srcdev_devid = dev_replace->srcdev->devid;
5270
5271 /*
5272 * duplicate the write operations while the dev replace
5273 * procedure is running. Since the copying of the old disk
5274 * to the new disk takes place at run time while the
5275 * filesystem is mounted writable, the regular write
5276 * operations to the old disk have to be duplicated to go
5277 * to the new disk as well.
5278 * Note that device->missing is handled by the caller, and
5279 * that the write to the old disk is already set up in the
5280 * stripes array.
5281 */
5282 index_where_to_add = num_stripes;
5283 for (i = 0; i < num_stripes; i++) {
5284 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5285 /* write to new disk, too */
5286 struct btrfs_bio_stripe *new =
5287 bbio->stripes + index_where_to_add;
5288 struct btrfs_bio_stripe *old =
5289 bbio->stripes + i;
5290
5291 new->physical = old->physical;
5292 new->length = old->length;
5293 new->dev = dev_replace->tgtdev;
5294 index_where_to_add++;
5295 max_errors++;
5296 }
5297 }
5298 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005299 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5300 dev_replace->tgtdev != NULL) {
5301 u64 srcdev_devid = dev_replace->srcdev->devid;
5302 int index_srcdev = 0;
5303 int found = 0;
5304 u64 physical_of_found = 0;
5305
5306 /*
5307 * During the dev-replace procedure, the target drive can
5308 * also be used to read data in case it is needed to repair
5309 * a corrupt block elsewhere. This is possible if the
5310 * requested area is left of the left cursor. In this area,
5311 * the target drive is a full copy of the source drive.
5312 */
5313 for (i = 0; i < num_stripes; i++) {
5314 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5315 /*
5316 * In case of DUP, in order to keep it
5317 * simple, only add the mirror with the
5318 * lowest physical address
5319 */
5320 if (found &&
5321 physical_of_found <=
5322 bbio->stripes[i].physical)
5323 continue;
5324 index_srcdev = i;
5325 found = 1;
5326 physical_of_found = bbio->stripes[i].physical;
5327 }
5328 }
5329 if (found) {
5330 u64 length = map->stripe_len;
5331
5332 if (physical_of_found + length <=
5333 dev_replace->cursor_left) {
5334 struct btrfs_bio_stripe *tgtdev_stripe =
5335 bbio->stripes + num_stripes;
5336
5337 tgtdev_stripe->physical = physical_of_found;
5338 tgtdev_stripe->length =
5339 bbio->stripes[index_srcdev].length;
5340 tgtdev_stripe->dev = dev_replace->tgtdev;
5341
5342 num_stripes++;
5343 }
5344 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005345 }
5346
Li Zefande11cc12011-12-01 12:55:47 +08005347 *bbio_ret = bbio;
5348 bbio->num_stripes = num_stripes;
5349 bbio->max_errors = max_errors;
5350 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005351
5352 /*
5353 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5354 * mirror_num == num_stripes + 1 && dev_replace target drive is
5355 * available as a mirror
5356 */
5357 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5358 WARN_ON(num_stripes > 1);
5359 bbio->stripes[0].dev = dev_replace->tgtdev;
5360 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5361 bbio->mirror_num = map->num_stripes + 1;
5362 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005363 if (raid_map) {
5364 sort_parity_stripes(bbio, raid_map);
5365 *raid_map_ret = raid_map;
5366 }
Chris Masoncea9e442008-04-09 16:28:12 -04005367out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005368 if (dev_replace_is_ongoing)
5369 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005370 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005371 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005372}
5373
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005374int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005375 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005376 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005377{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005378 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005379 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005380}
5381
Yan Zhenga512bbf2008-12-08 16:46:26 -05005382int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5383 u64 chunk_start, u64 physical, u64 devid,
5384 u64 **logical, int *naddrs, int *stripe_len)
5385{
5386 struct extent_map_tree *em_tree = &map_tree->map_tree;
5387 struct extent_map *em;
5388 struct map_lookup *map;
5389 u64 *buf;
5390 u64 bytenr;
5391 u64 length;
5392 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005393 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005394 int i, j, nr = 0;
5395
Chris Mason890871b2009-09-02 16:24:52 -04005396 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005397 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005398 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005399
Josef Bacik835d9742013-03-19 12:13:25 -04005400 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005401 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005402 chunk_start);
5403 return -EIO;
5404 }
5405
5406 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005407 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005408 em->start, chunk_start);
5409 free_extent_map(em);
5410 return -EIO;
5411 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005412 map = (struct map_lookup *)em->bdev;
5413
5414 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005415 rmap_len = map->stripe_len;
5416
Yan Zhenga512bbf2008-12-08 16:46:26 -05005417 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5418 do_div(length, map->num_stripes / map->sub_stripes);
5419 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5420 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005421 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5422 BTRFS_BLOCK_GROUP_RAID6)) {
5423 do_div(length, nr_data_stripes(map));
5424 rmap_len = map->stripe_len * nr_data_stripes(map);
5425 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005426
5427 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005428 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005429
5430 for (i = 0; i < map->num_stripes; i++) {
5431 if (devid && map->stripes[i].dev->devid != devid)
5432 continue;
5433 if (map->stripes[i].physical > physical ||
5434 map->stripes[i].physical + length <= physical)
5435 continue;
5436
5437 stripe_nr = physical - map->stripes[i].physical;
5438 do_div(stripe_nr, map->stripe_len);
5439
5440 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5441 stripe_nr = stripe_nr * map->num_stripes + i;
5442 do_div(stripe_nr, map->sub_stripes);
5443 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5444 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005445 } /* else if RAID[56], multiply by nr_data_stripes().
5446 * Alternatively, just use rmap_len below instead of
5447 * map->stripe_len */
5448
5449 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005450 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005451 for (j = 0; j < nr; j++) {
5452 if (buf[j] == bytenr)
5453 break;
5454 }
Chris Mason934d3752008-12-08 16:43:10 -05005455 if (j == nr) {
5456 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005457 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005458 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005459 }
5460
Yan Zhenga512bbf2008-12-08 16:46:26 -05005461 *logical = buf;
5462 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005463 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005464
5465 free_extent_map(em);
5466 return 0;
5467}
5468
Miao Xie8408c712014-06-19 10:42:55 +08005469static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5470{
5471 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5472 bio_endio_nodec(bio, err);
5473 else
5474 bio_endio(bio, err);
5475 kfree(bbio);
5476}
5477
Jan Schmidta1d3c472011-08-04 17:15:33 +02005478static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005479{
Chris Mason9be33952013-05-17 18:30:14 -04005480 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005481 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005482 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005483
Stefan Behrens442a4f62012-05-25 16:06:08 +02005484 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005485 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005486 if (err == -EIO || err == -EREMOTEIO) {
5487 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005488 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005489
5490 BUG_ON(stripe_index >= bbio->num_stripes);
5491 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005492 if (dev->bdev) {
5493 if (bio->bi_rw & WRITE)
5494 btrfs_dev_stat_inc(dev,
5495 BTRFS_DEV_STAT_WRITE_ERRS);
5496 else
5497 btrfs_dev_stat_inc(dev,
5498 BTRFS_DEV_STAT_READ_ERRS);
5499 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5500 btrfs_dev_stat_inc(dev,
5501 BTRFS_DEV_STAT_FLUSH_ERRS);
5502 btrfs_dev_stat_print_on_error(dev);
5503 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005504 }
5505 }
Chris Mason8790d502008-04-03 16:29:03 -04005506
Jan Schmidta1d3c472011-08-04 17:15:33 +02005507 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005508 is_orig_bio = 1;
5509
Miao Xiec404e0d2014-01-30 16:46:55 +08005510 btrfs_bio_counter_dec(bbio->fs_info);
5511
Jan Schmidta1d3c472011-08-04 17:15:33 +02005512 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005513 if (!is_orig_bio) {
5514 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005515 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005516 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005517
Jan Schmidta1d3c472011-08-04 17:15:33 +02005518 bio->bi_private = bbio->private;
5519 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005520 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005521 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005522 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005523 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005524 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005525 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005526 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005527 /*
5528 * this bio is actually up to date, we didn't
5529 * go over the max number of errors
5530 */
5531 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005532 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005533 }
Miao Xiec55f1392014-06-19 10:42:54 +08005534
Miao Xie8408c712014-06-19 10:42:55 +08005535 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005536 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005537 bio_put(bio);
5538 }
Chris Mason8790d502008-04-03 16:29:03 -04005539}
5540
Chris Mason8b712842008-06-11 16:50:36 -04005541/*
5542 * see run_scheduled_bios for a description of why bios are collected for
5543 * async submit.
5544 *
5545 * This will add one bio to the pending list for a device and make sure
5546 * the work struct is scheduled.
5547 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005548static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5549 struct btrfs_device *device,
5550 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005551{
5552 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005553 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005554
David Woodhouse53b381b2013-01-29 18:40:14 -05005555 if (device->missing || !device->bdev) {
5556 bio_endio(bio, -EIO);
5557 return;
5558 }
5559
Chris Mason8b712842008-06-11 16:50:36 -04005560 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005561 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005562 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005563 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005564 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005565 return;
Chris Mason8b712842008-06-11 16:50:36 -04005566 }
5567
5568 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005569 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005570 * higher layers. Otherwise, the async bio makes it appear we have
5571 * made progress against dirty pages when we've really just put it
5572 * on a queue for later
5573 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005574 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005575 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005576 bio->bi_next = NULL;
5577 bio->bi_rw |= rw;
5578
5579 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005580 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005581 pending_bios = &device->pending_sync_bios;
5582 else
5583 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005584
Chris Masonffbd5172009-04-20 15:50:09 -04005585 if (pending_bios->tail)
5586 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005587
Chris Masonffbd5172009-04-20 15:50:09 -04005588 pending_bios->tail = bio;
5589 if (!pending_bios->head)
5590 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005591 if (device->running_pending)
5592 should_queue = 0;
5593
5594 spin_unlock(&device->io_lock);
5595
5596 if (should_queue)
Qu Wenruoa8c93d42014-02-28 10:46:08 +08005597 btrfs_queue_work(root->fs_info->submit_workers,
5598 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005599}
5600
Josef Bacikde1ee922012-10-19 16:50:56 -04005601static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5602 sector_t sector)
5603{
5604 struct bio_vec *prev;
5605 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005606 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005607 struct bvec_merge_data bvm = {
5608 .bi_bdev = bdev,
5609 .bi_sector = sector,
5610 .bi_rw = bio->bi_rw,
5611 };
5612
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305613 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005614 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005615
5616 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005617 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005618 return 0;
5619
5620 if (!q->merge_bvec_fn)
5621 return 1;
5622
Kent Overstreet4f024f32013-10-11 15:44:27 -07005623 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005624 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5625 return 0;
5626 return 1;
5627}
5628
5629static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5630 struct bio *bio, u64 physical, int dev_nr,
5631 int rw, int async)
5632{
5633 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5634
5635 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005636 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005637 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005638 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005639#ifdef DEBUG
5640 {
5641 struct rcu_string *name;
5642
5643 rcu_read_lock();
5644 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005645 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005646 "(%s id %llu), size=%u\n", rw,
5647 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5648 name->str, dev->devid, bio->bi_size);
5649 rcu_read_unlock();
5650 }
5651#endif
5652 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005653
5654 btrfs_bio_counter_inc_noblocked(root->fs_info);
5655
Josef Bacikde1ee922012-10-19 16:50:56 -04005656 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005657 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005658 else
5659 btrfsic_submit_bio(rw, bio);
5660}
5661
5662static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5663 struct bio *first_bio, struct btrfs_device *dev,
5664 int dev_nr, int rw, int async)
5665{
5666 struct bio_vec *bvec = first_bio->bi_io_vec;
5667 struct bio *bio;
5668 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5669 u64 physical = bbio->stripes[dev_nr].physical;
5670
5671again:
5672 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5673 if (!bio)
5674 return -ENOMEM;
5675
5676 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5677 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5678 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005679 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005680
5681 atomic_inc(&bbio->stripes_pending);
5682 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5683 rw, async);
5684 physical += len;
5685 goto again;
5686 }
5687 bvec++;
5688 }
5689
5690 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5691 return 0;
5692}
5693
5694static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5695{
5696 atomic_inc(&bbio->error);
5697 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005698 /* Shoud be the original bio. */
5699 WARN_ON(bio != bbio->orig_bio);
5700
Josef Bacikde1ee922012-10-19 16:50:56 -04005701 bio->bi_private = bbio->private;
5702 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005703 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005704 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005705
5706 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005707 }
5708}
5709
Chris Masonf1885912008-04-09 16:28:12 -04005710int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005711 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005712{
Chris Mason0b86a832008-03-24 15:01:56 -04005713 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005714 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005715 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005716 u64 length = 0;
5717 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005718 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005719 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005720 int dev_nr = 0;
5721 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005722 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005723
Kent Overstreet4f024f32013-10-11 15:44:27 -07005724 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005725 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005726
Miao Xiec404e0d2014-01-30 16:46:55 +08005727 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005728 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5729 mirror_num, &raid_map);
Miao Xiec404e0d2014-01-30 16:46:55 +08005730 if (ret) {
5731 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005732 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005733 }
Chris Masoncea9e442008-04-09 16:28:12 -04005734
Jan Schmidta1d3c472011-08-04 17:15:33 +02005735 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005736 bbio->orig_bio = first_bio;
5737 bbio->private = first_bio->bi_private;
5738 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005739 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005740 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5741
5742 if (raid_map) {
5743 /* In this case, map_length has been set to the length of
5744 a single stripe; not the whole write */
5745 if (rw & WRITE) {
Miao Xiec404e0d2014-01-30 16:46:55 +08005746 ret = raid56_parity_write(root, bio, bbio,
5747 raid_map, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005748 } else {
Miao Xiec404e0d2014-01-30 16:46:55 +08005749 ret = raid56_parity_recover(root, bio, bbio,
5750 raid_map, map_length,
5751 mirror_num);
David Woodhouse53b381b2013-01-29 18:40:14 -05005752 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005753 /*
5754 * FIXME, replace dosen't support raid56 yet, please fix
5755 * it in the future.
5756 */
5757 btrfs_bio_counter_dec(root->fs_info);
5758 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005759 }
5760
Chris Masoncea9e442008-04-09 16:28:12 -04005761 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005762 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005763 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005764 BUG();
5765 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005766
Chris Masond3977122009-01-05 21:25:51 -05005767 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005768 dev = bbio->stripes[dev_nr].dev;
5769 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5770 bbio_error(bbio, first_bio, logical);
5771 dev_nr++;
5772 continue;
5773 }
5774
5775 /*
5776 * Check and see if we're ok with this bio based on it's size
5777 * and offset with the given device.
5778 */
5779 if (!bio_size_ok(dev->bdev, first_bio,
5780 bbio->stripes[dev_nr].physical >> 9)) {
5781 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5782 dev_nr, rw, async_submit);
5783 BUG_ON(ret);
5784 dev_nr++;
5785 continue;
5786 }
5787
Jan Schmidta1d3c472011-08-04 17:15:33 +02005788 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005789 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005790 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005791 } else {
5792 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005793 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005794 }
Josef Bacik606686e2012-06-04 14:03:51 -04005795
Josef Bacikde1ee922012-10-19 16:50:56 -04005796 submit_stripe_bio(root, bbio, bio,
5797 bbio->stripes[dev_nr].physical, dev_nr, rw,
5798 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005799 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005800 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005801 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005802 return 0;
5803}
5804
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005805struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005806 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005807{
Yan Zheng2b820322008-11-17 21:11:30 -05005808 struct btrfs_device *device;
5809 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005810
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005811 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005812 while (cur_devices) {
5813 if (!fsid ||
5814 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5815 device = __find_device(&cur_devices->devices,
5816 devid, uuid);
5817 if (device)
5818 return device;
5819 }
5820 cur_devices = cur_devices->seed;
5821 }
5822 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005823}
5824
Chris Masondfe25022008-05-13 13:46:40 -04005825static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5826 u64 devid, u8 *dev_uuid)
5827{
5828 struct btrfs_device *device;
5829 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5830
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005831 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5832 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005833 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005834
5835 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005836 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005837 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005838
5839 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005840 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005841
Chris Masondfe25022008-05-13 13:46:40 -04005842 return device;
5843}
5844
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005845/**
5846 * btrfs_alloc_device - allocate struct btrfs_device
5847 * @fs_info: used only for generating a new devid, can be NULL if
5848 * devid is provided (i.e. @devid != NULL).
5849 * @devid: a pointer to devid for this device. If NULL a new devid
5850 * is generated.
5851 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5852 * is generated.
5853 *
5854 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5855 * on error. Returned struct is not linked onto any lists and can be
5856 * destroyed with kfree() right away.
5857 */
5858struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5859 const u64 *devid,
5860 const u8 *uuid)
5861{
5862 struct btrfs_device *dev;
5863 u64 tmp;
5864
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305865 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005866 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005867
5868 dev = __alloc_device();
5869 if (IS_ERR(dev))
5870 return dev;
5871
5872 if (devid)
5873 tmp = *devid;
5874 else {
5875 int ret;
5876
5877 ret = find_next_devid(fs_info, &tmp);
5878 if (ret) {
5879 kfree(dev);
5880 return ERR_PTR(ret);
5881 }
5882 }
5883 dev->devid = tmp;
5884
5885 if (uuid)
5886 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5887 else
5888 generate_random_uuid(dev->uuid);
5889
Liu Bo9e0af232014-08-15 23:36:53 +08005890 btrfs_init_work(&dev->work, btrfs_submit_helper,
5891 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005892
5893 return dev;
5894}
5895
Chris Mason0b86a832008-03-24 15:01:56 -04005896static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5897 struct extent_buffer *leaf,
5898 struct btrfs_chunk *chunk)
5899{
5900 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5901 struct map_lookup *map;
5902 struct extent_map *em;
5903 u64 logical;
5904 u64 length;
5905 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005906 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005907 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005908 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005909 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005910
Chris Masone17cade2008-04-15 15:41:47 -04005911 logical = key->offset;
5912 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005913
Chris Mason890871b2009-09-02 16:24:52 -04005914 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005915 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005916 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005917
5918 /* already mapped? */
5919 if (em && em->start <= logical && em->start + em->len > logical) {
5920 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005921 return 0;
5922 } else if (em) {
5923 free_extent_map(em);
5924 }
Chris Mason0b86a832008-03-24 15:01:56 -04005925
David Sterba172ddd62011-04-21 00:48:27 +02005926 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005927 if (!em)
5928 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005929 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5930 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005931 if (!map) {
5932 free_extent_map(em);
5933 return -ENOMEM;
5934 }
5935
Wang Shilong298a8f92014-06-19 10:42:52 +08005936 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04005937 em->bdev = (struct block_device *)map;
5938 em->start = logical;
5939 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005940 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005941 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005942 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005943
Chris Mason593060d2008-03-25 16:50:33 -04005944 map->num_stripes = num_stripes;
5945 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5946 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5947 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5948 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5949 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005950 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005951 for (i = 0; i < num_stripes; i++) {
5952 map->stripes[i].physical =
5953 btrfs_stripe_offset_nr(leaf, chunk, i);
5954 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005955 read_extent_buffer(leaf, uuid, (unsigned long)
5956 btrfs_stripe_dev_uuid_nr(chunk, i),
5957 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005958 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5959 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005960 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005961 free_extent_map(em);
5962 return -EIO;
5963 }
Chris Masondfe25022008-05-13 13:46:40 -04005964 if (!map->stripes[i].dev) {
5965 map->stripes[i].dev =
5966 add_missing_dev(root, devid, uuid);
5967 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04005968 free_extent_map(em);
5969 return -EIO;
5970 }
5971 }
5972 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005973 }
5974
Chris Mason890871b2009-09-02 16:24:52 -04005975 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005976 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005977 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005978 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005979 free_extent_map(em);
5980
5981 return 0;
5982}
5983
Jeff Mahoney143bede2012-03-01 14:56:26 +01005984static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005985 struct btrfs_dev_item *dev_item,
5986 struct btrfs_device *device)
5987{
5988 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005989
5990 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005991 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5992 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005993 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5994 device->type = btrfs_device_type(leaf, dev_item);
5995 device->io_align = btrfs_device_io_align(leaf, dev_item);
5996 device->io_width = btrfs_device_io_width(leaf, dev_item);
5997 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005998 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005999 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006000
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006001 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006002 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006003}
6004
Yan Zheng2b820322008-11-17 21:11:30 -05006005static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
6006{
6007 struct btrfs_fs_devices *fs_devices;
6008 int ret;
6009
Li Zefanb367e472011-12-07 11:38:24 +08006010 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006011
6012 fs_devices = root->fs_info->fs_devices->seed;
6013 while (fs_devices) {
6014 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6015 ret = 0;
6016 goto out;
6017 }
6018 fs_devices = fs_devices->seed;
6019 }
6020
6021 fs_devices = find_fsid(fsid);
6022 if (!fs_devices) {
6023 ret = -ENOENT;
6024 goto out;
6025 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006026
6027 fs_devices = clone_fs_devices(fs_devices);
6028 if (IS_ERR(fs_devices)) {
6029 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006030 goto out;
6031 }
6032
Christoph Hellwig97288f22008-12-02 06:36:09 -05006033 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006034 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006035 if (ret) {
6036 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006037 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006038 }
Yan Zheng2b820322008-11-17 21:11:30 -05006039
6040 if (!fs_devices->seeding) {
6041 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006042 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006043 ret = -EINVAL;
6044 goto out;
6045 }
6046
6047 fs_devices->seed = root->fs_info->fs_devices->seed;
6048 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006049out:
Yan Zheng2b820322008-11-17 21:11:30 -05006050 return ret;
6051}
6052
Chris Mason0d81ba52008-03-24 15:02:07 -04006053static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006054 struct extent_buffer *leaf,
6055 struct btrfs_dev_item *dev_item)
6056{
6057 struct btrfs_device *device;
6058 u64 devid;
6059 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006060 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006061 u8 dev_uuid[BTRFS_UUID_SIZE];
6062
Chris Mason0b86a832008-03-24 15:01:56 -04006063 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006064 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006065 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006066 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006067 BTRFS_UUID_SIZE);
6068
6069 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6070 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05006071 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006072 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006073 }
6074
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006075 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006076 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006077 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006078 return -EIO;
6079
6080 if (!device) {
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006081 btrfs_warn(root->fs_info, "devid %llu missing", devid);
Yan Zheng2b820322008-11-17 21:11:30 -05006082 device = add_missing_dev(root, devid, dev_uuid);
6083 if (!device)
6084 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05006085 } else if (!device->missing) {
6086 /*
6087 * this happens when a device that was properly setup
6088 * in the device info lists suddenly goes bad.
6089 * device->bdev is NULL, and so we have to set
6090 * device->missing to one here
6091 */
6092 root->fs_info->fs_devices->missing_devices++;
6093 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006094 }
6095 }
6096
6097 if (device->fs_devices != root->fs_info->fs_devices) {
6098 BUG_ON(device->writeable);
6099 if (device->generation !=
6100 btrfs_device_generation(leaf, dev_item))
6101 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006102 }
Chris Mason0b86a832008-03-24 15:01:56 -04006103
6104 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006105 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006106 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006107 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006108 spin_lock(&root->fs_info->free_chunk_lock);
6109 root->fs_info->free_chunk_space += device->total_bytes -
6110 device->bytes_used;
6111 spin_unlock(&root->fs_info->free_chunk_lock);
6112 }
Chris Mason0b86a832008-03-24 15:01:56 -04006113 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006114 return ret;
6115}
6116
Yan Zhenge4404d62008-12-12 10:03:26 -05006117int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006118{
David Sterba6c417612011-04-13 15:41:04 +02006119 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006120 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006121 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006122 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04006123 u8 *ptr;
6124 unsigned long sb_ptr;
6125 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006126 u32 num_stripes;
6127 u32 array_size;
6128 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006129 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04006130 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006131
Yan Zhenge4404d62008-12-12 10:03:26 -05006132 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04006133 BTRFS_SUPER_INFO_SIZE);
6134 if (!sb)
6135 return -ENOMEM;
6136 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006137 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006138 /*
6139 * The sb extent buffer is artifical and just used to read the system array.
6140 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6141 * pages up-to-date when the page is larger: extent does not cover the
6142 * whole page and consequently check_page_uptodate does not find all
6143 * the page's extents up-to-date (the hole beyond sb),
6144 * write_extent_buffer then triggers a WARN_ON.
6145 *
6146 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6147 * but sb spans only this function. Add an explicit SetPageUptodate call
6148 * to silence the warning eg. on PowerPC 64.
6149 */
6150 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006151 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006152
Chris Masona061fc82008-05-07 11:43:44 -04006153 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006154 array_size = btrfs_super_sys_array_size(super_copy);
6155
Chris Mason0b86a832008-03-24 15:01:56 -04006156 ptr = super_copy->sys_chunk_array;
6157 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
6158 cur = 0;
6159
6160 while (cur < array_size) {
6161 disk_key = (struct btrfs_disk_key *)ptr;
6162 btrfs_disk_key_to_cpu(&key, disk_key);
6163
Chris Masona061fc82008-05-07 11:43:44 -04006164 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006165 sb_ptr += len;
6166 cur += len;
6167
Chris Mason0d81ba52008-03-24 15:02:07 -04006168 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04006169 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04006170 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006171 if (ret)
6172 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006173 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6174 len = btrfs_chunk_item_size(num_stripes);
6175 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006176 ret = -EIO;
6177 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006178 }
6179 ptr += len;
6180 sb_ptr += len;
6181 cur += len;
6182 }
Chris Masona061fc82008-05-07 11:43:44 -04006183 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006184 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006185}
6186
6187int btrfs_read_chunk_tree(struct btrfs_root *root)
6188{
6189 struct btrfs_path *path;
6190 struct extent_buffer *leaf;
6191 struct btrfs_key key;
6192 struct btrfs_key found_key;
6193 int ret;
6194 int slot;
6195
6196 root = root->fs_info->chunk_root;
6197
6198 path = btrfs_alloc_path();
6199 if (!path)
6200 return -ENOMEM;
6201
Li Zefanb367e472011-12-07 11:38:24 +08006202 mutex_lock(&uuid_mutex);
6203 lock_chunks(root);
6204
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006205 /*
6206 * Read all device items, and then all the chunk items. All
6207 * device items are found before any chunk item (their object id
6208 * is smaller than the lowest possible object id for a chunk
6209 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006210 */
6211 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6212 key.offset = 0;
6213 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006214 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006215 if (ret < 0)
6216 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006217 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006218 leaf = path->nodes[0];
6219 slot = path->slots[0];
6220 if (slot >= btrfs_header_nritems(leaf)) {
6221 ret = btrfs_next_leaf(root, path);
6222 if (ret == 0)
6223 continue;
6224 if (ret < 0)
6225 goto error;
6226 break;
6227 }
6228 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006229 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6230 struct btrfs_dev_item *dev_item;
6231 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006232 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006233 ret = read_one_dev(root, leaf, dev_item);
6234 if (ret)
6235 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006236 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6237 struct btrfs_chunk *chunk;
6238 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6239 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006240 if (ret)
6241 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006242 }
6243 path->slots[0]++;
6244 }
Chris Mason0b86a832008-03-24 15:01:56 -04006245 ret = 0;
6246error:
Li Zefanb367e472011-12-07 11:38:24 +08006247 unlock_chunks(root);
6248 mutex_unlock(&uuid_mutex);
6249
Yan Zheng2b820322008-11-17 21:11:30 -05006250 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006251 return ret;
6252}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006253
Miao Xiecb517ea2013-05-15 07:48:19 +00006254void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6255{
6256 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6257 struct btrfs_device *device;
6258
Liu Bo29cc83f2014-05-11 23:14:59 +08006259 while (fs_devices) {
6260 mutex_lock(&fs_devices->device_list_mutex);
6261 list_for_each_entry(device, &fs_devices->devices, dev_list)
6262 device->dev_root = fs_info->dev_root;
6263 mutex_unlock(&fs_devices->device_list_mutex);
6264
6265 fs_devices = fs_devices->seed;
6266 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006267}
6268
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006269static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6270{
6271 int i;
6272
6273 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6274 btrfs_dev_stat_reset(dev, i);
6275}
6276
6277int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6278{
6279 struct btrfs_key key;
6280 struct btrfs_key found_key;
6281 struct btrfs_root *dev_root = fs_info->dev_root;
6282 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6283 struct extent_buffer *eb;
6284 int slot;
6285 int ret = 0;
6286 struct btrfs_device *device;
6287 struct btrfs_path *path = NULL;
6288 int i;
6289
6290 path = btrfs_alloc_path();
6291 if (!path) {
6292 ret = -ENOMEM;
6293 goto out;
6294 }
6295
6296 mutex_lock(&fs_devices->device_list_mutex);
6297 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6298 int item_size;
6299 struct btrfs_dev_stats_item *ptr;
6300
6301 key.objectid = 0;
6302 key.type = BTRFS_DEV_STATS_KEY;
6303 key.offset = device->devid;
6304 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6305 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006306 __btrfs_reset_dev_stats(device);
6307 device->dev_stats_valid = 1;
6308 btrfs_release_path(path);
6309 continue;
6310 }
6311 slot = path->slots[0];
6312 eb = path->nodes[0];
6313 btrfs_item_key_to_cpu(eb, &found_key, slot);
6314 item_size = btrfs_item_size_nr(eb, slot);
6315
6316 ptr = btrfs_item_ptr(eb, slot,
6317 struct btrfs_dev_stats_item);
6318
6319 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6320 if (item_size >= (1 + i) * sizeof(__le64))
6321 btrfs_dev_stat_set(device, i,
6322 btrfs_dev_stats_value(eb, ptr, i));
6323 else
6324 btrfs_dev_stat_reset(device, i);
6325 }
6326
6327 device->dev_stats_valid = 1;
6328 btrfs_dev_stat_print_on_load(device);
6329 btrfs_release_path(path);
6330 }
6331 mutex_unlock(&fs_devices->device_list_mutex);
6332
6333out:
6334 btrfs_free_path(path);
6335 return ret < 0 ? ret : 0;
6336}
6337
6338static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6339 struct btrfs_root *dev_root,
6340 struct btrfs_device *device)
6341{
6342 struct btrfs_path *path;
6343 struct btrfs_key key;
6344 struct extent_buffer *eb;
6345 struct btrfs_dev_stats_item *ptr;
6346 int ret;
6347 int i;
6348
6349 key.objectid = 0;
6350 key.type = BTRFS_DEV_STATS_KEY;
6351 key.offset = device->devid;
6352
6353 path = btrfs_alloc_path();
6354 BUG_ON(!path);
6355 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6356 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006357 printk_in_rcu(KERN_WARNING "BTRFS: "
6358 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006359 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006360 goto out;
6361 }
6362
6363 if (ret == 0 &&
6364 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6365 /* need to delete old one and insert a new one */
6366 ret = btrfs_del_item(trans, dev_root, path);
6367 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006368 printk_in_rcu(KERN_WARNING "BTRFS: "
6369 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006370 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006371 goto out;
6372 }
6373 ret = 1;
6374 }
6375
6376 if (ret == 1) {
6377 /* need to insert a new item */
6378 btrfs_release_path(path);
6379 ret = btrfs_insert_empty_item(trans, dev_root, path,
6380 &key, sizeof(*ptr));
6381 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006382 printk_in_rcu(KERN_WARNING "BTRFS: "
6383 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006384 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006385 goto out;
6386 }
6387 }
6388
6389 eb = path->nodes[0];
6390 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6391 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6392 btrfs_set_dev_stats_value(eb, ptr, i,
6393 btrfs_dev_stat_read(device, i));
6394 btrfs_mark_buffer_dirty(eb);
6395
6396out:
6397 btrfs_free_path(path);
6398 return ret;
6399}
6400
6401/*
6402 * called from commit_transaction. Writes all changed device stats to disk.
6403 */
6404int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6405 struct btrfs_fs_info *fs_info)
6406{
6407 struct btrfs_root *dev_root = fs_info->dev_root;
6408 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6409 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006410 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006411 int ret = 0;
6412
6413 mutex_lock(&fs_devices->device_list_mutex);
6414 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006415 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006416 continue;
6417
Miao Xieaddc3fa2014-07-24 11:37:11 +08006418 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006419 ret = update_dev_stat_item(trans, dev_root, device);
6420 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006421 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006422 }
6423 mutex_unlock(&fs_devices->device_list_mutex);
6424
6425 return ret;
6426}
6427
Stefan Behrens442a4f62012-05-25 16:06:08 +02006428void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6429{
6430 btrfs_dev_stat_inc(dev, index);
6431 btrfs_dev_stat_print_on_error(dev);
6432}
6433
Eric Sandeen48a3b632013-04-25 20:41:01 +00006434static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006435{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006436 if (!dev->dev_stats_valid)
6437 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006438 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6439 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006440 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006441 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6442 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6443 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006444 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6445 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006446}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006447
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006448static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6449{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006450 int i;
6451
6452 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6453 if (btrfs_dev_stat_read(dev, i) != 0)
6454 break;
6455 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6456 return; /* all values == 0, suppress message */
6457
Frank Holtonefe120a2013-12-20 11:37:06 -05006458 printk_in_rcu(KERN_INFO "BTRFS: "
6459 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006460 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006461 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6462 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6463 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6464 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6465 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6466}
6467
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006468int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006469 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006470{
6471 struct btrfs_device *dev;
6472 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6473 int i;
6474
6475 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006476 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006477 mutex_unlock(&fs_devices->device_list_mutex);
6478
6479 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006480 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006481 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006482 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006483 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006484 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006485 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006486 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6487 if (stats->nr_items > i)
6488 stats->values[i] =
6489 btrfs_dev_stat_read_and_reset(dev, i);
6490 else
6491 btrfs_dev_stat_reset(dev, i);
6492 }
6493 } else {
6494 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6495 if (stats->nr_items > i)
6496 stats->values[i] = btrfs_dev_stat_read(dev, i);
6497 }
6498 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6499 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6500 return 0;
6501}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006502
6503int btrfs_scratch_superblock(struct btrfs_device *device)
6504{
6505 struct buffer_head *bh;
6506 struct btrfs_super_block *disk_super;
6507
6508 bh = btrfs_read_dev_super(device->bdev);
6509 if (!bh)
6510 return -EINVAL;
6511 disk_super = (struct btrfs_super_block *)bh->b_data;
6512
6513 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6514 set_buffer_dirty(bh);
6515 sync_dirty_buffer(bh);
6516 brelse(bh);
6517
6518 return 0;
6519}