blob: 46cd29ad31566abe31bdbd3ea8c8e4d0c8d256b0 [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{
1818 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001819
Stefan Behrense93c89c2012-11-05 17:33:06 +01001820 list_del_rcu(&srcdev->dev_list);
1821 list_del_rcu(&srcdev->dev_alloc_list);
1822 fs_info->fs_devices->num_devices--;
1823 if (srcdev->missing) {
1824 fs_info->fs_devices->missing_devices--;
1825 fs_info->fs_devices->rw_devices++;
1826 }
1827 if (srcdev->can_discard)
1828 fs_info->fs_devices->num_can_discard--;
Ilya Dryomov13572722013-10-02 20:41:01 +03001829 if (srcdev->bdev) {
Stefan Behrense93c89c2012-11-05 17:33:06 +01001830 fs_info->fs_devices->open_devices--;
1831
Miao Xieff61d172014-07-24 11:37:06 +08001832 /*
1833 * zero out the old super if it is not writable
1834 * (e.g. seed device)
1835 */
1836 if (srcdev->writeable)
1837 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03001838 }
1839
Stefan Behrense93c89c2012-11-05 17:33:06 +01001840 call_rcu(&srcdev->rcu, free_device);
1841}
1842
1843void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1844 struct btrfs_device *tgtdev)
1845{
1846 struct btrfs_device *next_device;
1847
1848 WARN_ON(!tgtdev);
1849 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1850 if (tgtdev->bdev) {
1851 btrfs_scratch_superblock(tgtdev);
1852 fs_info->fs_devices->open_devices--;
1853 }
1854 fs_info->fs_devices->num_devices--;
1855 if (tgtdev->can_discard)
1856 fs_info->fs_devices->num_can_discard++;
1857
1858 next_device = list_entry(fs_info->fs_devices->devices.next,
1859 struct btrfs_device, dev_list);
1860 if (tgtdev->bdev == fs_info->sb->s_bdev)
1861 fs_info->sb->s_bdev = next_device->bdev;
1862 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1863 fs_info->fs_devices->latest_bdev = next_device->bdev;
1864 list_del_rcu(&tgtdev->dev_list);
1865
1866 call_rcu(&tgtdev->rcu, free_device);
1867
1868 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1869}
1870
Eric Sandeen48a3b632013-04-25 20:41:01 +00001871static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1872 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001873{
1874 int ret = 0;
1875 struct btrfs_super_block *disk_super;
1876 u64 devid;
1877 u8 *dev_uuid;
1878 struct block_device *bdev;
1879 struct buffer_head *bh;
1880
1881 *device = NULL;
1882 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1883 root->fs_info->bdev_holder, 0, &bdev, &bh);
1884 if (ret)
1885 return ret;
1886 disk_super = (struct btrfs_super_block *)bh->b_data;
1887 devid = btrfs_stack_device_id(&disk_super->dev_item);
1888 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001889 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001890 disk_super->fsid);
1891 brelse(bh);
1892 if (!*device)
1893 ret = -ENOENT;
1894 blkdev_put(bdev, FMODE_READ);
1895 return ret;
1896}
1897
1898int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1899 char *device_path,
1900 struct btrfs_device **device)
1901{
1902 *device = NULL;
1903 if (strcmp(device_path, "missing") == 0) {
1904 struct list_head *devices;
1905 struct btrfs_device *tmp;
1906
1907 devices = &root->fs_info->fs_devices->devices;
1908 /*
1909 * It is safe to read the devices since the volume_mutex
1910 * is held by the caller.
1911 */
1912 list_for_each_entry(tmp, devices, dev_list) {
1913 if (tmp->in_fs_metadata && !tmp->bdev) {
1914 *device = tmp;
1915 break;
1916 }
1917 }
1918
1919 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001920 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001921 return -ENOENT;
1922 }
1923
1924 return 0;
1925 } else {
1926 return btrfs_find_device_by_path(root, device_path, device);
1927 }
1928}
1929
Yan Zheng2b820322008-11-17 21:11:30 -05001930/*
1931 * does all the dirty work required for changing file system's UUID.
1932 */
Li Zefan125ccb02011-12-08 15:07:24 +08001933static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001934{
1935 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1936 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001937 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001938 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001939 struct btrfs_device *device;
1940 u64 super_flags;
1941
1942 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001943 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001944 return -EINVAL;
1945
Ilya Dryomov2208a372013-08-12 14:33:03 +03001946 seed_devices = __alloc_fs_devices();
1947 if (IS_ERR(seed_devices))
1948 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001949
Yan Zhenge4404d62008-12-12 10:03:26 -05001950 old_devices = clone_fs_devices(fs_devices);
1951 if (IS_ERR(old_devices)) {
1952 kfree(seed_devices);
1953 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001954 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001955
Yan Zheng2b820322008-11-17 21:11:30 -05001956 list_add(&old_devices->list, &fs_uuids);
1957
Yan Zhenge4404d62008-12-12 10:03:26 -05001958 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1959 seed_devices->opened = 1;
1960 INIT_LIST_HEAD(&seed_devices->devices);
1961 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001962 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001963
1964 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001965 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1966 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001967
Yan Zhenge4404d62008-12-12 10:03:26 -05001968 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1969 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1970 device->fs_devices = seed_devices;
1971 }
1972
Yan Zheng2b820322008-11-17 21:11:30 -05001973 fs_devices->seeding = 0;
1974 fs_devices->num_devices = 0;
1975 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08001976 fs_devices->missing_devices = 0;
1977 fs_devices->num_can_discard = 0;
1978 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001979 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001980
1981 generate_random_uuid(fs_devices->fsid);
1982 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1983 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01001984 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1985
Yan Zheng2b820322008-11-17 21:11:30 -05001986 super_flags = btrfs_super_flags(disk_super) &
1987 ~BTRFS_SUPER_FLAG_SEEDING;
1988 btrfs_set_super_flags(disk_super, super_flags);
1989
1990 return 0;
1991}
1992
1993/*
1994 * strore the expected generation for seed devices in device items.
1995 */
1996static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1997 struct btrfs_root *root)
1998{
1999 struct btrfs_path *path;
2000 struct extent_buffer *leaf;
2001 struct btrfs_dev_item *dev_item;
2002 struct btrfs_device *device;
2003 struct btrfs_key key;
2004 u8 fs_uuid[BTRFS_UUID_SIZE];
2005 u8 dev_uuid[BTRFS_UUID_SIZE];
2006 u64 devid;
2007 int ret;
2008
2009 path = btrfs_alloc_path();
2010 if (!path)
2011 return -ENOMEM;
2012
2013 root = root->fs_info->chunk_root;
2014 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2015 key.offset = 0;
2016 key.type = BTRFS_DEV_ITEM_KEY;
2017
2018 while (1) {
2019 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2020 if (ret < 0)
2021 goto error;
2022
2023 leaf = path->nodes[0];
2024next_slot:
2025 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2026 ret = btrfs_next_leaf(root, path);
2027 if (ret > 0)
2028 break;
2029 if (ret < 0)
2030 goto error;
2031 leaf = path->nodes[0];
2032 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002033 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002034 continue;
2035 }
2036
2037 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2038 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2039 key.type != BTRFS_DEV_ITEM_KEY)
2040 break;
2041
2042 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2043 struct btrfs_dev_item);
2044 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002045 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002046 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002047 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002048 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002049 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2050 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002051 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002052
2053 if (device->fs_devices->seeding) {
2054 btrfs_set_device_generation(leaf, dev_item,
2055 device->generation);
2056 btrfs_mark_buffer_dirty(leaf);
2057 }
2058
2059 path->slots[0]++;
2060 goto next_slot;
2061 }
2062 ret = 0;
2063error:
2064 btrfs_free_path(path);
2065 return ret;
2066}
2067
Chris Mason788f20e2008-04-28 15:29:42 -04002068int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2069{
Josef Bacikd5e20032011-08-04 14:52:27 +00002070 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002071 struct btrfs_trans_handle *trans;
2072 struct btrfs_device *device;
2073 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002074 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002075 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002076 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04002077 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002078 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002079 int ret = 0;
2080
Yan Zheng2b820322008-11-17 21:11:30 -05002081 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002082 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002083
Li Zefana5d16332011-12-07 20:08:40 -05002084 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002085 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002086 if (IS_ERR(bdev))
2087 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002088
Yan Zheng2b820322008-11-17 21:11:30 -05002089 if (root->fs_info->fs_devices->seeding) {
2090 seeding_dev = 1;
2091 down_write(&sb->s_umount);
2092 mutex_lock(&uuid_mutex);
2093 }
2094
Chris Mason8c8bee12008-09-29 11:19:10 -04002095 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002096
Chris Mason788f20e2008-04-28 15:29:42 -04002097 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002098
2099 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002100 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002101 if (device->bdev == bdev) {
2102 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002103 mutex_unlock(
2104 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002105 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002106 }
2107 }
Liu Bod25628b2012-11-14 14:35:30 +00002108 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002109
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002110 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2111 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002112 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002113 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002114 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002115 }
2116
Josef Bacik606686e2012-06-04 14:03:51 -04002117 name = rcu_string_strdup(device_path, GFP_NOFS);
2118 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002119 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002120 ret = -ENOMEM;
2121 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002122 }
Josef Bacik606686e2012-06-04 14:03:51 -04002123 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002124
Yan, Zhenga22285a2010-05-16 10:48:46 -04002125 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002126 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002127 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002128 kfree(device);
2129 ret = PTR_ERR(trans);
2130 goto error;
2131 }
2132
Yan Zheng2b820322008-11-17 21:11:30 -05002133 lock_chunks(root);
2134
Josef Bacikd5e20032011-08-04 14:52:27 +00002135 q = bdev_get_queue(bdev);
2136 if (blk_queue_discard(q))
2137 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002138 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002139 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002140 device->io_width = root->sectorsize;
2141 device->io_align = root->sectorsize;
2142 device->sector_size = root->sectorsize;
2143 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002144 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002145 device->dev_root = root->fs_info->dev_root;
2146 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002147 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002148 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002149 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002150 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002151 set_blocksize(device->bdev, 4096);
2152
Yan Zheng2b820322008-11-17 21:11:30 -05002153 if (seeding_dev) {
2154 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002155 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002156 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002157 }
2158
2159 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002160
Chris Masone5e9a522009-06-10 15:17:02 -04002161 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002162 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002163 list_add(&device->dev_alloc_list,
2164 &root->fs_info->fs_devices->alloc_list);
2165 root->fs_info->fs_devices->num_devices++;
2166 root->fs_info->fs_devices->open_devices++;
2167 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002168 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002169 if (device->can_discard)
2170 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002171 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2172
Josef Bacik2bf64752011-09-26 17:12:22 -04002173 spin_lock(&root->fs_info->free_chunk_lock);
2174 root->fs_info->free_chunk_space += device->total_bytes;
2175 spin_unlock(&root->fs_info->free_chunk_lock);
2176
Chris Masonc2898112009-06-10 09:51:32 -04002177 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2178 root->fs_info->fs_devices->rotating = 1;
2179
David Sterba6c417612011-04-13 15:41:04 +02002180 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2181 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002182 total_bytes + device->total_bytes);
2183
David Sterba6c417612011-04-13 15:41:04 +02002184 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2185 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002186 total_bytes + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002187
2188 /* add sysfs device entry */
2189 btrfs_kobj_add_device(root->fs_info, device);
2190
Chris Masone5e9a522009-06-10 15:17:02 -04002191 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002192
Yan Zheng2b820322008-11-17 21:11:30 -05002193 if (seeding_dev) {
Anand Jainb2373f22014-06-03 11:36:03 +08002194 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002195 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002196 if (ret) {
2197 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002198 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002199 }
Yan Zheng2b820322008-11-17 21:11:30 -05002200 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002201 if (ret) {
2202 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002203 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002204 }
Anand Jainb2373f22014-06-03 11:36:03 +08002205
2206 /* Sprouting would change fsid of the mounted root,
2207 * so rename the fsid on the sysfs
2208 */
2209 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2210 root->fs_info->fsid);
2211 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2212 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002213 } else {
2214 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002215 if (ret) {
2216 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002217 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002218 }
Yan Zheng2b820322008-11-17 21:11:30 -05002219 }
2220
Chris Mason913d9522009-03-10 13:17:18 -04002221 /*
2222 * we've got more storage, clear any full flags on the space
2223 * infos
2224 */
2225 btrfs_clear_space_info_full(root->fs_info);
2226
Chris Mason7d9eb122008-07-08 14:19:17 -04002227 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002228 root->fs_info->num_tolerated_disk_barrier_failures =
2229 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002230 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002231
2232 if (seeding_dev) {
2233 mutex_unlock(&uuid_mutex);
2234 up_write(&sb->s_umount);
2235
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002236 if (ret) /* transaction commit */
2237 return ret;
2238
Yan Zheng2b820322008-11-17 21:11:30 -05002239 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002240 if (ret < 0)
2241 btrfs_error(root->fs_info, ret,
2242 "Failed to relocate sys chunks after "
2243 "device initialization. This can be fixed "
2244 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002245 trans = btrfs_attach_transaction(root);
2246 if (IS_ERR(trans)) {
2247 if (PTR_ERR(trans) == -ENOENT)
2248 return 0;
2249 return PTR_ERR(trans);
2250 }
2251 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002252 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002253
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002254 /* Update ctime/mtime for libblkid */
2255 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002256 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002257
2258error_trans:
2259 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002260 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002261 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002262 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002263 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002264error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002265 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002266 if (seeding_dev) {
2267 mutex_unlock(&uuid_mutex);
2268 up_write(&sb->s_umount);
2269 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002270 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002271}
2272
Stefan Behrense93c89c2012-11-05 17:33:06 +01002273int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2274 struct btrfs_device **device_out)
2275{
2276 struct request_queue *q;
2277 struct btrfs_device *device;
2278 struct block_device *bdev;
2279 struct btrfs_fs_info *fs_info = root->fs_info;
2280 struct list_head *devices;
2281 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002282 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002283 int ret = 0;
2284
2285 *device_out = NULL;
2286 if (fs_info->fs_devices->seeding)
2287 return -EINVAL;
2288
2289 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2290 fs_info->bdev_holder);
2291 if (IS_ERR(bdev))
2292 return PTR_ERR(bdev);
2293
2294 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2295
2296 devices = &fs_info->fs_devices->devices;
2297 list_for_each_entry(device, devices, dev_list) {
2298 if (device->bdev == bdev) {
2299 ret = -EEXIST;
2300 goto error;
2301 }
2302 }
2303
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002304 device = btrfs_alloc_device(NULL, &devid, NULL);
2305 if (IS_ERR(device)) {
2306 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002307 goto error;
2308 }
2309
2310 name = rcu_string_strdup(device_path, GFP_NOFS);
2311 if (!name) {
2312 kfree(device);
2313 ret = -ENOMEM;
2314 goto error;
2315 }
2316 rcu_assign_pointer(device->name, name);
2317
2318 q = bdev_get_queue(bdev);
2319 if (blk_queue_discard(q))
2320 device->can_discard = 1;
2321 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2322 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002323 device->generation = 0;
2324 device->io_width = root->sectorsize;
2325 device->io_align = root->sectorsize;
2326 device->sector_size = root->sectorsize;
2327 device->total_bytes = i_size_read(bdev->bd_inode);
2328 device->disk_total_bytes = device->total_bytes;
2329 device->dev_root = fs_info->dev_root;
2330 device->bdev = bdev;
2331 device->in_fs_metadata = 1;
2332 device->is_tgtdev_for_dev_replace = 1;
2333 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002334 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002335 set_blocksize(device->bdev, 4096);
2336 device->fs_devices = fs_info->fs_devices;
2337 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2338 fs_info->fs_devices->num_devices++;
2339 fs_info->fs_devices->open_devices++;
2340 if (device->can_discard)
2341 fs_info->fs_devices->num_can_discard++;
2342 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2343
2344 *device_out = device;
2345 return ret;
2346
2347error:
2348 blkdev_put(bdev, FMODE_EXCL);
2349 return ret;
2350}
2351
2352void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2353 struct btrfs_device *tgtdev)
2354{
2355 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2356 tgtdev->io_width = fs_info->dev_root->sectorsize;
2357 tgtdev->io_align = fs_info->dev_root->sectorsize;
2358 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2359 tgtdev->dev_root = fs_info->dev_root;
2360 tgtdev->in_fs_metadata = 1;
2361}
2362
Chris Masond3977122009-01-05 21:25:51 -05002363static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2364 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002365{
2366 int ret;
2367 struct btrfs_path *path;
2368 struct btrfs_root *root;
2369 struct btrfs_dev_item *dev_item;
2370 struct extent_buffer *leaf;
2371 struct btrfs_key key;
2372
2373 root = device->dev_root->fs_info->chunk_root;
2374
2375 path = btrfs_alloc_path();
2376 if (!path)
2377 return -ENOMEM;
2378
2379 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2380 key.type = BTRFS_DEV_ITEM_KEY;
2381 key.offset = device->devid;
2382
2383 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2384 if (ret < 0)
2385 goto out;
2386
2387 if (ret > 0) {
2388 ret = -ENOENT;
2389 goto out;
2390 }
2391
2392 leaf = path->nodes[0];
2393 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2394
2395 btrfs_set_device_id(leaf, dev_item, device->devid);
2396 btrfs_set_device_type(leaf, dev_item, device->type);
2397 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2398 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2399 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002400 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002401 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2402 btrfs_mark_buffer_dirty(leaf);
2403
2404out:
2405 btrfs_free_path(path);
2406 return ret;
2407}
2408
Chris Mason7d9eb122008-07-08 14:19:17 -04002409static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002410 struct btrfs_device *device, u64 new_size)
2411{
2412 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002413 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002414 u64 old_total = btrfs_super_total_bytes(super_copy);
2415 u64 diff = new_size - device->total_bytes;
2416
Yan Zheng2b820322008-11-17 21:11:30 -05002417 if (!device->writeable)
2418 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002419 if (new_size <= device->total_bytes ||
2420 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002421 return -EINVAL;
2422
Chris Mason8f18cf12008-04-25 16:53:30 -04002423 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002424 device->fs_devices->total_rw_bytes += diff;
2425
2426 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002427 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002428 btrfs_clear_space_info_full(device->dev_root->fs_info);
2429
Chris Mason8f18cf12008-04-25 16:53:30 -04002430 return btrfs_update_device(trans, device);
2431}
2432
Chris Mason7d9eb122008-07-08 14:19:17 -04002433int btrfs_grow_device(struct btrfs_trans_handle *trans,
2434 struct btrfs_device *device, u64 new_size)
2435{
2436 int ret;
2437 lock_chunks(device->dev_root);
2438 ret = __btrfs_grow_device(trans, device, new_size);
2439 unlock_chunks(device->dev_root);
2440 return ret;
2441}
2442
Chris Mason8f18cf12008-04-25 16:53:30 -04002443static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2444 struct btrfs_root *root,
2445 u64 chunk_tree, u64 chunk_objectid,
2446 u64 chunk_offset)
2447{
2448 int ret;
2449 struct btrfs_path *path;
2450 struct btrfs_key key;
2451
2452 root = root->fs_info->chunk_root;
2453 path = btrfs_alloc_path();
2454 if (!path)
2455 return -ENOMEM;
2456
2457 key.objectid = chunk_objectid;
2458 key.offset = chunk_offset;
2459 key.type = BTRFS_CHUNK_ITEM_KEY;
2460
2461 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002462 if (ret < 0)
2463 goto out;
2464 else if (ret > 0) { /* Logic error or corruption */
2465 btrfs_error(root->fs_info, -ENOENT,
2466 "Failed lookup while freeing chunk.");
2467 ret = -ENOENT;
2468 goto out;
2469 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002470
2471 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002472 if (ret < 0)
2473 btrfs_error(root->fs_info, ret,
2474 "Failed to delete chunk item.");
2475out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002476 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002477 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002478}
2479
Christoph Hellwigb2950862008-12-02 09:54:17 -05002480static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002481 chunk_offset)
2482{
David Sterba6c417612011-04-13 15:41:04 +02002483 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002484 struct btrfs_disk_key *disk_key;
2485 struct btrfs_chunk *chunk;
2486 u8 *ptr;
2487 int ret = 0;
2488 u32 num_stripes;
2489 u32 array_size;
2490 u32 len = 0;
2491 u32 cur;
2492 struct btrfs_key key;
2493
2494 array_size = btrfs_super_sys_array_size(super_copy);
2495
2496 ptr = super_copy->sys_chunk_array;
2497 cur = 0;
2498
2499 while (cur < array_size) {
2500 disk_key = (struct btrfs_disk_key *)ptr;
2501 btrfs_disk_key_to_cpu(&key, disk_key);
2502
2503 len = sizeof(*disk_key);
2504
2505 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2506 chunk = (struct btrfs_chunk *)(ptr + len);
2507 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2508 len += btrfs_chunk_item_size(num_stripes);
2509 } else {
2510 ret = -EIO;
2511 break;
2512 }
2513 if (key.objectid == chunk_objectid &&
2514 key.offset == chunk_offset) {
2515 memmove(ptr, ptr + len, array_size - (cur + len));
2516 array_size -= len;
2517 btrfs_set_super_sys_array_size(super_copy, array_size);
2518 } else {
2519 ptr += len;
2520 cur += len;
2521 }
2522 }
2523 return ret;
2524}
2525
Christoph Hellwigb2950862008-12-02 09:54:17 -05002526static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002527 u64 chunk_tree, u64 chunk_objectid,
2528 u64 chunk_offset)
2529{
2530 struct extent_map_tree *em_tree;
2531 struct btrfs_root *extent_root;
2532 struct btrfs_trans_handle *trans;
2533 struct extent_map *em;
2534 struct map_lookup *map;
2535 int ret;
2536 int i;
2537
2538 root = root->fs_info->chunk_root;
2539 extent_root = root->fs_info->extent_root;
2540 em_tree = &root->fs_info->mapping_tree.map_tree;
2541
Josef Bacikba1bf482009-09-11 16:11:19 -04002542 ret = btrfs_can_relocate(extent_root, chunk_offset);
2543 if (ret)
2544 return -ENOSPC;
2545
Chris Mason8f18cf12008-04-25 16:53:30 -04002546 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002547 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002548 if (ret)
2549 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002550
Yan, Zhenga22285a2010-05-16 10:48:46 -04002551 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002552 if (IS_ERR(trans)) {
2553 ret = PTR_ERR(trans);
2554 btrfs_std_error(root->fs_info, ret);
2555 return ret;
2556 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002557
Chris Mason7d9eb122008-07-08 14:19:17 -04002558 lock_chunks(root);
2559
Chris Mason8f18cf12008-04-25 16:53:30 -04002560 /*
2561 * step two, delete the device extents and the
2562 * chunk tree entries
2563 */
Chris Mason890871b2009-09-02 16:24:52 -04002564 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002565 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002566 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002567
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002568 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002569 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002570 map = (struct map_lookup *)em->bdev;
2571
2572 for (i = 0; i < map->num_stripes; i++) {
2573 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2574 map->stripes[i].physical);
2575 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002576
Chris Masondfe25022008-05-13 13:46:40 -04002577 if (map->stripes[i].dev) {
2578 ret = btrfs_update_device(trans, map->stripes[i].dev);
2579 BUG_ON(ret);
2580 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002581 }
2582 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2583 chunk_offset);
2584
2585 BUG_ON(ret);
2586
liubo1abe9b82011-03-24 11:18:59 +00002587 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2588
Chris Mason8f18cf12008-04-25 16:53:30 -04002589 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2590 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2591 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002592 }
2593
Zheng Yan1a40e232008-09-26 10:09:34 -04002594 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2595 BUG_ON(ret);
2596
Chris Mason890871b2009-09-02 16:24:52 -04002597 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002598 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002599 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002600
Chris Mason8f18cf12008-04-25 16:53:30 -04002601 /* once for the tree */
2602 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002603 /* once for us */
2604 free_extent_map(em);
2605
Chris Mason7d9eb122008-07-08 14:19:17 -04002606 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002607 btrfs_end_transaction(trans, root);
2608 return 0;
2609}
2610
Yan Zheng2b820322008-11-17 21:11:30 -05002611static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2612{
2613 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2614 struct btrfs_path *path;
2615 struct extent_buffer *leaf;
2616 struct btrfs_chunk *chunk;
2617 struct btrfs_key key;
2618 struct btrfs_key found_key;
2619 u64 chunk_tree = chunk_root->root_key.objectid;
2620 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002621 bool retried = false;
2622 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002623 int ret;
2624
2625 path = btrfs_alloc_path();
2626 if (!path)
2627 return -ENOMEM;
2628
Josef Bacikba1bf482009-09-11 16:11:19 -04002629again:
Yan Zheng2b820322008-11-17 21:11:30 -05002630 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2631 key.offset = (u64)-1;
2632 key.type = BTRFS_CHUNK_ITEM_KEY;
2633
2634 while (1) {
2635 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2636 if (ret < 0)
2637 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002638 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002639
2640 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2641 key.type);
2642 if (ret < 0)
2643 goto error;
2644 if (ret > 0)
2645 break;
2646
2647 leaf = path->nodes[0];
2648 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2649
2650 chunk = btrfs_item_ptr(leaf, path->slots[0],
2651 struct btrfs_chunk);
2652 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002653 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002654
2655 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2656 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2657 found_key.objectid,
2658 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002659 if (ret == -ENOSPC)
2660 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302661 else
2662 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002663 }
2664
2665 if (found_key.offset == 0)
2666 break;
2667 key.offset = found_key.offset - 1;
2668 }
2669 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002670 if (failed && !retried) {
2671 failed = 0;
2672 retried = true;
2673 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302674 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002675 ret = -ENOSPC;
2676 }
Yan Zheng2b820322008-11-17 21:11:30 -05002677error:
2678 btrfs_free_path(path);
2679 return ret;
2680}
2681
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002682static int insert_balance_item(struct btrfs_root *root,
2683 struct btrfs_balance_control *bctl)
2684{
2685 struct btrfs_trans_handle *trans;
2686 struct btrfs_balance_item *item;
2687 struct btrfs_disk_balance_args disk_bargs;
2688 struct btrfs_path *path;
2689 struct extent_buffer *leaf;
2690 struct btrfs_key key;
2691 int ret, err;
2692
2693 path = btrfs_alloc_path();
2694 if (!path)
2695 return -ENOMEM;
2696
2697 trans = btrfs_start_transaction(root, 0);
2698 if (IS_ERR(trans)) {
2699 btrfs_free_path(path);
2700 return PTR_ERR(trans);
2701 }
2702
2703 key.objectid = BTRFS_BALANCE_OBJECTID;
2704 key.type = BTRFS_BALANCE_ITEM_KEY;
2705 key.offset = 0;
2706
2707 ret = btrfs_insert_empty_item(trans, root, path, &key,
2708 sizeof(*item));
2709 if (ret)
2710 goto out;
2711
2712 leaf = path->nodes[0];
2713 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2714
2715 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2716
2717 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2718 btrfs_set_balance_data(leaf, item, &disk_bargs);
2719 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2720 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2721 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2722 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2723
2724 btrfs_set_balance_flags(leaf, item, bctl->flags);
2725
2726 btrfs_mark_buffer_dirty(leaf);
2727out:
2728 btrfs_free_path(path);
2729 err = btrfs_commit_transaction(trans, root);
2730 if (err && !ret)
2731 ret = err;
2732 return ret;
2733}
2734
2735static int del_balance_item(struct btrfs_root *root)
2736{
2737 struct btrfs_trans_handle *trans;
2738 struct btrfs_path *path;
2739 struct btrfs_key key;
2740 int ret, err;
2741
2742 path = btrfs_alloc_path();
2743 if (!path)
2744 return -ENOMEM;
2745
2746 trans = btrfs_start_transaction(root, 0);
2747 if (IS_ERR(trans)) {
2748 btrfs_free_path(path);
2749 return PTR_ERR(trans);
2750 }
2751
2752 key.objectid = BTRFS_BALANCE_OBJECTID;
2753 key.type = BTRFS_BALANCE_ITEM_KEY;
2754 key.offset = 0;
2755
2756 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2757 if (ret < 0)
2758 goto out;
2759 if (ret > 0) {
2760 ret = -ENOENT;
2761 goto out;
2762 }
2763
2764 ret = btrfs_del_item(trans, root, path);
2765out:
2766 btrfs_free_path(path);
2767 err = btrfs_commit_transaction(trans, root);
2768 if (err && !ret)
2769 ret = err;
2770 return ret;
2771}
2772
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002773/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002774 * This is a heuristic used to reduce the number of chunks balanced on
2775 * resume after balance was interrupted.
2776 */
2777static void update_balance_args(struct btrfs_balance_control *bctl)
2778{
2779 /*
2780 * Turn on soft mode for chunk types that were being converted.
2781 */
2782 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2783 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2784 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2785 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2786 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2787 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2788
2789 /*
2790 * Turn on usage filter if is not already used. The idea is
2791 * that chunks that we have already balanced should be
2792 * reasonably full. Don't do it for chunks that are being
2793 * converted - that will keep us from relocating unconverted
2794 * (albeit full) chunks.
2795 */
2796 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2797 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2798 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2799 bctl->data.usage = 90;
2800 }
2801 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2802 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2803 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2804 bctl->sys.usage = 90;
2805 }
2806 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2807 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2808 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2809 bctl->meta.usage = 90;
2810 }
2811}
2812
2813/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002814 * Should be called with both balance and volume mutexes held to
2815 * serialize other volume operations (add_dev/rm_dev/resize) with
2816 * restriper. Same goes for unset_balance_control.
2817 */
2818static void set_balance_control(struct btrfs_balance_control *bctl)
2819{
2820 struct btrfs_fs_info *fs_info = bctl->fs_info;
2821
2822 BUG_ON(fs_info->balance_ctl);
2823
2824 spin_lock(&fs_info->balance_lock);
2825 fs_info->balance_ctl = bctl;
2826 spin_unlock(&fs_info->balance_lock);
2827}
2828
2829static void unset_balance_control(struct btrfs_fs_info *fs_info)
2830{
2831 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2832
2833 BUG_ON(!fs_info->balance_ctl);
2834
2835 spin_lock(&fs_info->balance_lock);
2836 fs_info->balance_ctl = NULL;
2837 spin_unlock(&fs_info->balance_lock);
2838
2839 kfree(bctl);
2840}
2841
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002842/*
2843 * Balance filters. Return 1 if chunk should be filtered out
2844 * (should not be balanced).
2845 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002846static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002847 struct btrfs_balance_args *bargs)
2848{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002849 chunk_type = chunk_to_extended(chunk_type) &
2850 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002851
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002852 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002853 return 0;
2854
2855 return 1;
2856}
2857
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002858static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2859 struct btrfs_balance_args *bargs)
2860{
2861 struct btrfs_block_group_cache *cache;
2862 u64 chunk_used, user_thresh;
2863 int ret = 1;
2864
2865 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2866 chunk_used = btrfs_block_group_used(&cache->item);
2867
Ilya Dryomova105bb82013-01-21 15:15:56 +02002868 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002869 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002870 else if (bargs->usage > 100)
2871 user_thresh = cache->key.offset;
2872 else
2873 user_thresh = div_factor_fine(cache->key.offset,
2874 bargs->usage);
2875
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002876 if (chunk_used < user_thresh)
2877 ret = 0;
2878
2879 btrfs_put_block_group(cache);
2880 return ret;
2881}
2882
Ilya Dryomov409d4042012-01-16 22:04:47 +02002883static int chunk_devid_filter(struct extent_buffer *leaf,
2884 struct btrfs_chunk *chunk,
2885 struct btrfs_balance_args *bargs)
2886{
2887 struct btrfs_stripe *stripe;
2888 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2889 int i;
2890
2891 for (i = 0; i < num_stripes; i++) {
2892 stripe = btrfs_stripe_nr(chunk, i);
2893 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2894 return 0;
2895 }
2896
2897 return 1;
2898}
2899
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002900/* [pstart, pend) */
2901static int chunk_drange_filter(struct extent_buffer *leaf,
2902 struct btrfs_chunk *chunk,
2903 u64 chunk_offset,
2904 struct btrfs_balance_args *bargs)
2905{
2906 struct btrfs_stripe *stripe;
2907 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2908 u64 stripe_offset;
2909 u64 stripe_length;
2910 int factor;
2911 int i;
2912
2913 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2914 return 0;
2915
2916 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002917 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2918 factor = num_stripes / 2;
2919 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2920 factor = num_stripes - 1;
2921 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2922 factor = num_stripes - 2;
2923 } else {
2924 factor = num_stripes;
2925 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002926
2927 for (i = 0; i < num_stripes; i++) {
2928 stripe = btrfs_stripe_nr(chunk, i);
2929 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2930 continue;
2931
2932 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2933 stripe_length = btrfs_chunk_length(leaf, chunk);
2934 do_div(stripe_length, factor);
2935
2936 if (stripe_offset < bargs->pend &&
2937 stripe_offset + stripe_length > bargs->pstart)
2938 return 0;
2939 }
2940
2941 return 1;
2942}
2943
Ilya Dryomovea671762012-01-16 22:04:48 +02002944/* [vstart, vend) */
2945static int chunk_vrange_filter(struct extent_buffer *leaf,
2946 struct btrfs_chunk *chunk,
2947 u64 chunk_offset,
2948 struct btrfs_balance_args *bargs)
2949{
2950 if (chunk_offset < bargs->vend &&
2951 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2952 /* at least part of the chunk is inside this vrange */
2953 return 0;
2954
2955 return 1;
2956}
2957
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002958static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002959 struct btrfs_balance_args *bargs)
2960{
2961 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2962 return 0;
2963
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002964 chunk_type = chunk_to_extended(chunk_type) &
2965 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002966
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002967 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002968 return 1;
2969
2970 return 0;
2971}
2972
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002973static int should_balance_chunk(struct btrfs_root *root,
2974 struct extent_buffer *leaf,
2975 struct btrfs_chunk *chunk, u64 chunk_offset)
2976{
2977 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2978 struct btrfs_balance_args *bargs = NULL;
2979 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2980
2981 /* type filter */
2982 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2983 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2984 return 0;
2985 }
2986
2987 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2988 bargs = &bctl->data;
2989 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2990 bargs = &bctl->sys;
2991 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2992 bargs = &bctl->meta;
2993
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002994 /* profiles filter */
2995 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2996 chunk_profiles_filter(chunk_type, bargs)) {
2997 return 0;
2998 }
2999
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003000 /* usage filter */
3001 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3002 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3003 return 0;
3004 }
3005
Ilya Dryomov409d4042012-01-16 22:04:47 +02003006 /* devid filter */
3007 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3008 chunk_devid_filter(leaf, chunk, bargs)) {
3009 return 0;
3010 }
3011
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003012 /* drange filter, makes sense only with devid filter */
3013 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3014 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3015 return 0;
3016 }
3017
Ilya Dryomovea671762012-01-16 22:04:48 +02003018 /* vrange filter */
3019 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3020 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3021 return 0;
3022 }
3023
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003024 /* soft profile changing mode */
3025 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3026 chunk_soft_convert_filter(chunk_type, bargs)) {
3027 return 0;
3028 }
3029
David Sterba7d824b62014-05-07 17:37:51 +02003030 /*
3031 * limited by count, must be the last filter
3032 */
3033 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3034 if (bargs->limit == 0)
3035 return 0;
3036 else
3037 bargs->limit--;
3038 }
3039
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003040 return 1;
3041}
3042
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003043static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003044{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003045 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003046 struct btrfs_root *chunk_root = fs_info->chunk_root;
3047 struct btrfs_root *dev_root = fs_info->dev_root;
3048 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003049 struct btrfs_device *device;
3050 u64 old_size;
3051 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003052 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003053 struct btrfs_path *path;
3054 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003055 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003056 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003057 struct extent_buffer *leaf;
3058 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003059 int ret;
3060 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003061 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003062 u64 limit_data = bctl->data.limit;
3063 u64 limit_meta = bctl->meta.limit;
3064 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003065
Chris Masonec44a352008-04-28 15:29:52 -04003066 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003067 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003068 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04003069 old_size = device->total_bytes;
3070 size_to_free = div_factor(old_size, 1);
3071 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003072 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003073 device->total_bytes - device->bytes_used > size_to_free ||
3074 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003075 continue;
3076
3077 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003078 if (ret == -ENOSPC)
3079 break;
Chris Masonec44a352008-04-28 15:29:52 -04003080 BUG_ON(ret);
3081
Yan, Zhenga22285a2010-05-16 10:48:46 -04003082 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003083 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003084
3085 ret = btrfs_grow_device(trans, device, old_size);
3086 BUG_ON(ret);
3087
3088 btrfs_end_transaction(trans, dev_root);
3089 }
3090
3091 /* step two, relocate all the chunks */
3092 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003093 if (!path) {
3094 ret = -ENOMEM;
3095 goto error;
3096 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003097
3098 /* zero out stat counters */
3099 spin_lock(&fs_info->balance_lock);
3100 memset(&bctl->stat, 0, sizeof(bctl->stat));
3101 spin_unlock(&fs_info->balance_lock);
3102again:
David Sterba7d824b62014-05-07 17:37:51 +02003103 if (!counting) {
3104 bctl->data.limit = limit_data;
3105 bctl->meta.limit = limit_meta;
3106 bctl->sys.limit = limit_sys;
3107 }
Chris Masonec44a352008-04-28 15:29:52 -04003108 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3109 key.offset = (u64)-1;
3110 key.type = BTRFS_CHUNK_ITEM_KEY;
3111
Chris Masond3977122009-01-05 21:25:51 -05003112 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003113 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003114 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003115 ret = -ECANCELED;
3116 goto error;
3117 }
3118
Chris Masonec44a352008-04-28 15:29:52 -04003119 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3120 if (ret < 0)
3121 goto error;
3122
3123 /*
3124 * this shouldn't happen, it means the last relocate
3125 * failed
3126 */
3127 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003128 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003129
3130 ret = btrfs_previous_item(chunk_root, path, 0,
3131 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003132 if (ret) {
3133 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003134 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003135 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003136
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003137 leaf = path->nodes[0];
3138 slot = path->slots[0];
3139 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3140
Chris Masonec44a352008-04-28 15:29:52 -04003141 if (found_key.objectid != key.objectid)
3142 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003143
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003144 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3145
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003146 if (!counting) {
3147 spin_lock(&fs_info->balance_lock);
3148 bctl->stat.considered++;
3149 spin_unlock(&fs_info->balance_lock);
3150 }
3151
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003152 ret = should_balance_chunk(chunk_root, leaf, chunk,
3153 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003154 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003155 if (!ret)
3156 goto loop;
3157
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003158 if (counting) {
3159 spin_lock(&fs_info->balance_lock);
3160 bctl->stat.expected++;
3161 spin_unlock(&fs_info->balance_lock);
3162 goto loop;
3163 }
3164
Chris Masonec44a352008-04-28 15:29:52 -04003165 ret = btrfs_relocate_chunk(chunk_root,
3166 chunk_root->root_key.objectid,
3167 found_key.objectid,
3168 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003169 if (ret && ret != -ENOSPC)
3170 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003171 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003172 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003173 } else {
3174 spin_lock(&fs_info->balance_lock);
3175 bctl->stat.completed++;
3176 spin_unlock(&fs_info->balance_lock);
3177 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003178loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003179 if (found_key.offset == 0)
3180 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003181 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003182 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003183
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003184 if (counting) {
3185 btrfs_release_path(path);
3186 counting = false;
3187 goto again;
3188 }
Chris Masonec44a352008-04-28 15:29:52 -04003189error:
3190 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003191 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003192 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003193 enospc_errors);
3194 if (!ret)
3195 ret = -ENOSPC;
3196 }
3197
Chris Masonec44a352008-04-28 15:29:52 -04003198 return ret;
3199}
3200
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003201/**
3202 * alloc_profile_is_valid - see if a given profile is valid and reduced
3203 * @flags: profile to validate
3204 * @extended: if true @flags is treated as an extended profile
3205 */
3206static int alloc_profile_is_valid(u64 flags, int extended)
3207{
3208 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3209 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3210
3211 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3212
3213 /* 1) check that all other bits are zeroed */
3214 if (flags & ~mask)
3215 return 0;
3216
3217 /* 2) see if profile is reduced */
3218 if (flags == 0)
3219 return !extended; /* "0" is valid for usual profiles */
3220
3221 /* true if exactly one bit set */
3222 return (flags & (flags - 1)) == 0;
3223}
3224
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003225static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3226{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003227 /* cancel requested || normal exit path */
3228 return atomic_read(&fs_info->balance_cancel_req) ||
3229 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3230 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003231}
3232
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003233static void __cancel_balance(struct btrfs_fs_info *fs_info)
3234{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003235 int ret;
3236
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003237 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003238 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003239 if (ret)
3240 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003241
3242 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003243}
3244
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003245/*
3246 * Should be called with both balance and volume mutexes held
3247 */
3248int btrfs_balance(struct btrfs_balance_control *bctl,
3249 struct btrfs_ioctl_balance_args *bargs)
3250{
3251 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003252 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003253 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003254 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003255 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003256 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003257
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003258 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003259 atomic_read(&fs_info->balance_pause_req) ||
3260 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003261 ret = -EINVAL;
3262 goto out;
3263 }
3264
Ilya Dryomove4837f82012-03-27 17:09:17 +03003265 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3266 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3267 mixed = 1;
3268
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003269 /*
3270 * In case of mixed groups both data and meta should be picked,
3271 * and identical options should be given for both of them.
3272 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003273 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3274 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003275 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3276 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3277 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003278 btrfs_err(fs_info, "with mixed groups data and "
3279 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003280 ret = -EINVAL;
3281 goto out;
3282 }
3283 }
3284
Stefan Behrens8dabb742012-11-06 13:15:27 +01003285 num_devices = fs_info->fs_devices->num_devices;
3286 btrfs_dev_replace_lock(&fs_info->dev_replace);
3287 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3288 BUG_ON(num_devices < 1);
3289 num_devices--;
3290 }
3291 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003292 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003293 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003294 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003295 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003296 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003297 if (num_devices > 2)
3298 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3299 if (num_devices > 3)
3300 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3301 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003302 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3303 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3304 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003305 btrfs_err(fs_info, "unable to start balance with target "
3306 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003307 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003308 ret = -EINVAL;
3309 goto out;
3310 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003311 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3312 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3313 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003314 btrfs_err(fs_info,
3315 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003316 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003317 ret = -EINVAL;
3318 goto out;
3319 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003320 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3321 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3322 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003323 btrfs_err(fs_info,
3324 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003325 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003326 ret = -EINVAL;
3327 goto out;
3328 }
3329
Ilya Dryomove4837f82012-03-27 17:09:17 +03003330 /* allow dup'ed data chunks only in mixed mode */
3331 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003332 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003333 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003334 ret = -EINVAL;
3335 goto out;
3336 }
3337
3338 /* allow to reduce meta or sys integrity only if force set */
3339 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003340 BTRFS_BLOCK_GROUP_RAID10 |
3341 BTRFS_BLOCK_GROUP_RAID5 |
3342 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003343 do {
3344 seq = read_seqbegin(&fs_info->profiles_lock);
3345
3346 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3347 (fs_info->avail_system_alloc_bits & allowed) &&
3348 !(bctl->sys.target & allowed)) ||
3349 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3350 (fs_info->avail_metadata_alloc_bits & allowed) &&
3351 !(bctl->meta.target & allowed))) {
3352 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003353 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003354 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003355 btrfs_err(fs_info, "balance will reduce metadata "
3356 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003357 ret = -EINVAL;
3358 goto out;
3359 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003360 }
Miao Xiede98ced2013-01-29 10:13:12 +00003361 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003362
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003363 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3364 int num_tolerated_disk_barrier_failures;
3365 u64 target = bctl->sys.target;
3366
3367 num_tolerated_disk_barrier_failures =
3368 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3369 if (num_tolerated_disk_barrier_failures > 0 &&
3370 (target &
3371 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3372 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3373 num_tolerated_disk_barrier_failures = 0;
3374 else if (num_tolerated_disk_barrier_failures > 1 &&
3375 (target &
3376 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3377 num_tolerated_disk_barrier_failures = 1;
3378
3379 fs_info->num_tolerated_disk_barrier_failures =
3380 num_tolerated_disk_barrier_failures;
3381 }
3382
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003383 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003384 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003385 goto out;
3386
Ilya Dryomov59641012012-01-16 22:04:48 +02003387 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3388 BUG_ON(ret == -EEXIST);
3389 set_balance_control(bctl);
3390 } else {
3391 BUG_ON(ret != -EEXIST);
3392 spin_lock(&fs_info->balance_lock);
3393 update_balance_args(bctl);
3394 spin_unlock(&fs_info->balance_lock);
3395 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003396
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003397 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003398 mutex_unlock(&fs_info->balance_mutex);
3399
3400 ret = __btrfs_balance(fs_info);
3401
3402 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003403 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003404
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003405 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3406 fs_info->num_tolerated_disk_barrier_failures =
3407 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3408 }
3409
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003410 if (bargs) {
3411 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003412 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003413 }
3414
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003415 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3416 balance_need_close(fs_info)) {
3417 __cancel_balance(fs_info);
3418 }
3419
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003420 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003421
3422 return ret;
3423out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003424 if (bctl->flags & BTRFS_BALANCE_RESUME)
3425 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003426 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003427 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003428 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3429 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003430 return ret;
3431}
3432
3433static int balance_kthread(void *data)
3434{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003435 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003436 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003437
3438 mutex_lock(&fs_info->volume_mutex);
3439 mutex_lock(&fs_info->balance_mutex);
3440
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003441 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003442 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003443 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003444 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003445
3446 mutex_unlock(&fs_info->balance_mutex);
3447 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003448
Ilya Dryomov59641012012-01-16 22:04:48 +02003449 return ret;
3450}
3451
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003452int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3453{
3454 struct task_struct *tsk;
3455
3456 spin_lock(&fs_info->balance_lock);
3457 if (!fs_info->balance_ctl) {
3458 spin_unlock(&fs_info->balance_lock);
3459 return 0;
3460 }
3461 spin_unlock(&fs_info->balance_lock);
3462
3463 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003464 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003465 return 0;
3466 }
3467
3468 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303469 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003470}
3471
Ilya Dryomov68310a52012-06-22 12:24:12 -06003472int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003473{
Ilya Dryomov59641012012-01-16 22:04:48 +02003474 struct btrfs_balance_control *bctl;
3475 struct btrfs_balance_item *item;
3476 struct btrfs_disk_balance_args disk_bargs;
3477 struct btrfs_path *path;
3478 struct extent_buffer *leaf;
3479 struct btrfs_key key;
3480 int ret;
3481
3482 path = btrfs_alloc_path();
3483 if (!path)
3484 return -ENOMEM;
3485
Ilya Dryomov68310a52012-06-22 12:24:12 -06003486 key.objectid = BTRFS_BALANCE_OBJECTID;
3487 key.type = BTRFS_BALANCE_ITEM_KEY;
3488 key.offset = 0;
3489
3490 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3491 if (ret < 0)
3492 goto out;
3493 if (ret > 0) { /* ret = -ENOENT; */
3494 ret = 0;
3495 goto out;
3496 }
3497
Ilya Dryomov59641012012-01-16 22:04:48 +02003498 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3499 if (!bctl) {
3500 ret = -ENOMEM;
3501 goto out;
3502 }
3503
Ilya Dryomov59641012012-01-16 22:04:48 +02003504 leaf = path->nodes[0];
3505 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3506
Ilya Dryomov68310a52012-06-22 12:24:12 -06003507 bctl->fs_info = fs_info;
3508 bctl->flags = btrfs_balance_flags(leaf, item);
3509 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003510
3511 btrfs_balance_data(leaf, item, &disk_bargs);
3512 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3513 btrfs_balance_meta(leaf, item, &disk_bargs);
3514 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3515 btrfs_balance_sys(leaf, item, &disk_bargs);
3516 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3517
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003518 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3519
Ilya Dryomov68310a52012-06-22 12:24:12 -06003520 mutex_lock(&fs_info->volume_mutex);
3521 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003522
Ilya Dryomov68310a52012-06-22 12:24:12 -06003523 set_balance_control(bctl);
3524
3525 mutex_unlock(&fs_info->balance_mutex);
3526 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003527out:
3528 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003529 return ret;
3530}
3531
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003532int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3533{
3534 int ret = 0;
3535
3536 mutex_lock(&fs_info->balance_mutex);
3537 if (!fs_info->balance_ctl) {
3538 mutex_unlock(&fs_info->balance_mutex);
3539 return -ENOTCONN;
3540 }
3541
3542 if (atomic_read(&fs_info->balance_running)) {
3543 atomic_inc(&fs_info->balance_pause_req);
3544 mutex_unlock(&fs_info->balance_mutex);
3545
3546 wait_event(fs_info->balance_wait_q,
3547 atomic_read(&fs_info->balance_running) == 0);
3548
3549 mutex_lock(&fs_info->balance_mutex);
3550 /* we are good with balance_ctl ripped off from under us */
3551 BUG_ON(atomic_read(&fs_info->balance_running));
3552 atomic_dec(&fs_info->balance_pause_req);
3553 } else {
3554 ret = -ENOTCONN;
3555 }
3556
3557 mutex_unlock(&fs_info->balance_mutex);
3558 return ret;
3559}
3560
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003561int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3562{
Ilya Dryomove649e582013-10-10 20:40:21 +03003563 if (fs_info->sb->s_flags & MS_RDONLY)
3564 return -EROFS;
3565
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003566 mutex_lock(&fs_info->balance_mutex);
3567 if (!fs_info->balance_ctl) {
3568 mutex_unlock(&fs_info->balance_mutex);
3569 return -ENOTCONN;
3570 }
3571
3572 atomic_inc(&fs_info->balance_cancel_req);
3573 /*
3574 * if we are running just wait and return, balance item is
3575 * deleted in btrfs_balance in this case
3576 */
3577 if (atomic_read(&fs_info->balance_running)) {
3578 mutex_unlock(&fs_info->balance_mutex);
3579 wait_event(fs_info->balance_wait_q,
3580 atomic_read(&fs_info->balance_running) == 0);
3581 mutex_lock(&fs_info->balance_mutex);
3582 } else {
3583 /* __cancel_balance needs volume_mutex */
3584 mutex_unlock(&fs_info->balance_mutex);
3585 mutex_lock(&fs_info->volume_mutex);
3586 mutex_lock(&fs_info->balance_mutex);
3587
3588 if (fs_info->balance_ctl)
3589 __cancel_balance(fs_info);
3590
3591 mutex_unlock(&fs_info->volume_mutex);
3592 }
3593
3594 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3595 atomic_dec(&fs_info->balance_cancel_req);
3596 mutex_unlock(&fs_info->balance_mutex);
3597 return 0;
3598}
3599
Stefan Behrens803b2f52013-08-15 17:11:21 +02003600static int btrfs_uuid_scan_kthread(void *data)
3601{
3602 struct btrfs_fs_info *fs_info = data;
3603 struct btrfs_root *root = fs_info->tree_root;
3604 struct btrfs_key key;
3605 struct btrfs_key max_key;
3606 struct btrfs_path *path = NULL;
3607 int ret = 0;
3608 struct extent_buffer *eb;
3609 int slot;
3610 struct btrfs_root_item root_item;
3611 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003612 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003613
3614 path = btrfs_alloc_path();
3615 if (!path) {
3616 ret = -ENOMEM;
3617 goto out;
3618 }
3619
3620 key.objectid = 0;
3621 key.type = BTRFS_ROOT_ITEM_KEY;
3622 key.offset = 0;
3623
3624 max_key.objectid = (u64)-1;
3625 max_key.type = BTRFS_ROOT_ITEM_KEY;
3626 max_key.offset = (u64)-1;
3627
3628 path->keep_locks = 1;
3629
3630 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003631 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003632 if (ret) {
3633 if (ret > 0)
3634 ret = 0;
3635 break;
3636 }
3637
3638 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3639 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3640 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3641 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3642 goto skip;
3643
3644 eb = path->nodes[0];
3645 slot = path->slots[0];
3646 item_size = btrfs_item_size_nr(eb, slot);
3647 if (item_size < sizeof(root_item))
3648 goto skip;
3649
Stefan Behrens803b2f52013-08-15 17:11:21 +02003650 read_extent_buffer(eb, &root_item,
3651 btrfs_item_ptr_offset(eb, slot),
3652 (int)sizeof(root_item));
3653 if (btrfs_root_refs(&root_item) == 0)
3654 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003655
3656 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3657 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3658 if (trans)
3659 goto update_tree;
3660
3661 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003662 /*
3663 * 1 - subvol uuid item
3664 * 1 - received_subvol uuid item
3665 */
3666 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3667 if (IS_ERR(trans)) {
3668 ret = PTR_ERR(trans);
3669 break;
3670 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003671 continue;
3672 } else {
3673 goto skip;
3674 }
3675update_tree:
3676 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003677 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3678 root_item.uuid,
3679 BTRFS_UUID_KEY_SUBVOL,
3680 key.objectid);
3681 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003682 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003683 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003684 break;
3685 }
3686 }
3687
3688 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003689 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3690 root_item.received_uuid,
3691 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3692 key.objectid);
3693 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003694 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003695 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003696 break;
3697 }
3698 }
3699
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003700skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003701 if (trans) {
3702 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003703 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003704 if (ret)
3705 break;
3706 }
3707
Stefan Behrens803b2f52013-08-15 17:11:21 +02003708 btrfs_release_path(path);
3709 if (key.offset < (u64)-1) {
3710 key.offset++;
3711 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3712 key.offset = 0;
3713 key.type = BTRFS_ROOT_ITEM_KEY;
3714 } else if (key.objectid < (u64)-1) {
3715 key.offset = 0;
3716 key.type = BTRFS_ROOT_ITEM_KEY;
3717 key.objectid++;
3718 } else {
3719 break;
3720 }
3721 cond_resched();
3722 }
3723
3724out:
3725 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003726 if (trans && !IS_ERR(trans))
3727 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003728 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003729 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003730 else
3731 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003732 up(&fs_info->uuid_tree_rescan_sem);
3733 return 0;
3734}
3735
Stefan Behrens70f80172013-08-15 17:11:23 +02003736/*
3737 * Callback for btrfs_uuid_tree_iterate().
3738 * returns:
3739 * 0 check succeeded, the entry is not outdated.
3740 * < 0 if an error occured.
3741 * > 0 if the check failed, which means the caller shall remove the entry.
3742 */
3743static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3744 u8 *uuid, u8 type, u64 subid)
3745{
3746 struct btrfs_key key;
3747 int ret = 0;
3748 struct btrfs_root *subvol_root;
3749
3750 if (type != BTRFS_UUID_KEY_SUBVOL &&
3751 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3752 goto out;
3753
3754 key.objectid = subid;
3755 key.type = BTRFS_ROOT_ITEM_KEY;
3756 key.offset = (u64)-1;
3757 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3758 if (IS_ERR(subvol_root)) {
3759 ret = PTR_ERR(subvol_root);
3760 if (ret == -ENOENT)
3761 ret = 1;
3762 goto out;
3763 }
3764
3765 switch (type) {
3766 case BTRFS_UUID_KEY_SUBVOL:
3767 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3768 ret = 1;
3769 break;
3770 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3771 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3772 BTRFS_UUID_SIZE))
3773 ret = 1;
3774 break;
3775 }
3776
3777out:
3778 return ret;
3779}
3780
3781static int btrfs_uuid_rescan_kthread(void *data)
3782{
3783 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3784 int ret;
3785
3786 /*
3787 * 1st step is to iterate through the existing UUID tree and
3788 * to delete all entries that contain outdated data.
3789 * 2nd step is to add all missing entries to the UUID tree.
3790 */
3791 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3792 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003793 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003794 up(&fs_info->uuid_tree_rescan_sem);
3795 return ret;
3796 }
3797 return btrfs_uuid_scan_kthread(data);
3798}
3799
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003800int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3801{
3802 struct btrfs_trans_handle *trans;
3803 struct btrfs_root *tree_root = fs_info->tree_root;
3804 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003805 struct task_struct *task;
3806 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003807
3808 /*
3809 * 1 - root node
3810 * 1 - root item
3811 */
3812 trans = btrfs_start_transaction(tree_root, 2);
3813 if (IS_ERR(trans))
3814 return PTR_ERR(trans);
3815
3816 uuid_root = btrfs_create_tree(trans, fs_info,
3817 BTRFS_UUID_TREE_OBJECTID);
3818 if (IS_ERR(uuid_root)) {
3819 btrfs_abort_transaction(trans, tree_root,
3820 PTR_ERR(uuid_root));
3821 return PTR_ERR(uuid_root);
3822 }
3823
3824 fs_info->uuid_root = uuid_root;
3825
Stefan Behrens803b2f52013-08-15 17:11:21 +02003826 ret = btrfs_commit_transaction(trans, tree_root);
3827 if (ret)
3828 return ret;
3829
3830 down(&fs_info->uuid_tree_rescan_sem);
3831 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3832 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003833 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003834 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003835 up(&fs_info->uuid_tree_rescan_sem);
3836 return PTR_ERR(task);
3837 }
3838
3839 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003840}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003841
Stefan Behrens70f80172013-08-15 17:11:23 +02003842int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3843{
3844 struct task_struct *task;
3845
3846 down(&fs_info->uuid_tree_rescan_sem);
3847 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3848 if (IS_ERR(task)) {
3849 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003850 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003851 up(&fs_info->uuid_tree_rescan_sem);
3852 return PTR_ERR(task);
3853 }
3854
3855 return 0;
3856}
3857
Chris Mason8f18cf12008-04-25 16:53:30 -04003858/*
3859 * shrinking a device means finding all of the device extents past
3860 * the new size, and then following the back refs to the chunks.
3861 * The chunk relocation code actually frees the device extent
3862 */
3863int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3864{
3865 struct btrfs_trans_handle *trans;
3866 struct btrfs_root *root = device->dev_root;
3867 struct btrfs_dev_extent *dev_extent = NULL;
3868 struct btrfs_path *path;
3869 u64 length;
3870 u64 chunk_tree;
3871 u64 chunk_objectid;
3872 u64 chunk_offset;
3873 int ret;
3874 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003875 int failed = 0;
3876 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003877 struct extent_buffer *l;
3878 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003879 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003880 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003881 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003882 u64 diff = device->total_bytes - new_size;
3883
Stefan Behrens63a212a2012-11-05 18:29:28 +01003884 if (device->is_tgtdev_for_dev_replace)
3885 return -EINVAL;
3886
Chris Mason8f18cf12008-04-25 16:53:30 -04003887 path = btrfs_alloc_path();
3888 if (!path)
3889 return -ENOMEM;
3890
Chris Mason8f18cf12008-04-25 16:53:30 -04003891 path->reada = 2;
3892
Chris Mason7d9eb122008-07-08 14:19:17 -04003893 lock_chunks(root);
3894
Chris Mason8f18cf12008-04-25 16:53:30 -04003895 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003896 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003897 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003898 spin_lock(&root->fs_info->free_chunk_lock);
3899 root->fs_info->free_chunk_space -= diff;
3900 spin_unlock(&root->fs_info->free_chunk_lock);
3901 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003902 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003903
Josef Bacikba1bf482009-09-11 16:11:19 -04003904again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003905 key.objectid = device->devid;
3906 key.offset = (u64)-1;
3907 key.type = BTRFS_DEV_EXTENT_KEY;
3908
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003909 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003910 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3911 if (ret < 0)
3912 goto done;
3913
3914 ret = btrfs_previous_item(root, path, 0, key.type);
3915 if (ret < 0)
3916 goto done;
3917 if (ret) {
3918 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003919 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003920 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003921 }
3922
3923 l = path->nodes[0];
3924 slot = path->slots[0];
3925 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3926
Josef Bacikba1bf482009-09-11 16:11:19 -04003927 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003928 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003929 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003930 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003931
3932 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3933 length = btrfs_dev_extent_length(l, dev_extent);
3934
Josef Bacikba1bf482009-09-11 16:11:19 -04003935 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003936 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003937 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003938 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003939
3940 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3941 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3942 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003943 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003944
3945 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3946 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003947 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003948 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003949 if (ret == -ENOSPC)
3950 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003951 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003952
3953 if (failed && !retried) {
3954 failed = 0;
3955 retried = true;
3956 goto again;
3957 } else if (failed && retried) {
3958 ret = -ENOSPC;
3959 lock_chunks(root);
3960
3961 device->total_bytes = old_size;
3962 if (device->writeable)
3963 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003964 spin_lock(&root->fs_info->free_chunk_lock);
3965 root->fs_info->free_chunk_space += diff;
3966 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003967 unlock_chunks(root);
3968 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003969 }
3970
Chris Balld6397ba2009-04-27 07:29:03 -04003971 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003972 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003973 if (IS_ERR(trans)) {
3974 ret = PTR_ERR(trans);
3975 goto done;
3976 }
3977
Chris Balld6397ba2009-04-27 07:29:03 -04003978 lock_chunks(root);
3979
3980 device->disk_total_bytes = new_size;
3981 /* Now btrfs_update_device() will change the on-disk size. */
3982 ret = btrfs_update_device(trans, device);
3983 if (ret) {
3984 unlock_chunks(root);
3985 btrfs_end_transaction(trans, root);
3986 goto done;
3987 }
3988 WARN_ON(diff > old_total);
3989 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3990 unlock_chunks(root);
3991 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003992done:
3993 btrfs_free_path(path);
3994 return ret;
3995}
3996
Li Zefan125ccb02011-12-08 15:07:24 +08003997static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003998 struct btrfs_key *key,
3999 struct btrfs_chunk *chunk, int item_size)
4000{
David Sterba6c417612011-04-13 15:41:04 +02004001 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004002 struct btrfs_disk_key disk_key;
4003 u32 array_size;
4004 u8 *ptr;
4005
4006 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004007 if (array_size + item_size + sizeof(disk_key)
4008 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
Chris Mason0b86a832008-03-24 15:01:56 -04004009 return -EFBIG;
4010
4011 ptr = super_copy->sys_chunk_array + array_size;
4012 btrfs_cpu_key_to_disk(&disk_key, key);
4013 memcpy(ptr, &disk_key, sizeof(disk_key));
4014 ptr += sizeof(disk_key);
4015 memcpy(ptr, chunk, item_size);
4016 item_size += sizeof(disk_key);
4017 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4018 return 0;
4019}
4020
Miao Xieb2117a32011-01-05 10:07:28 +00004021/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004022 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004023 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004024static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004025{
Arne Jansen73c5de02011-04-12 12:07:57 +02004026 const struct btrfs_device_info *di_a = a;
4027 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004028
Arne Jansen73c5de02011-04-12 12:07:57 +02004029 if (di_a->max_avail > di_b->max_avail)
4030 return -1;
4031 if (di_a->max_avail < di_b->max_avail)
4032 return 1;
4033 if (di_a->total_avail > di_b->total_avail)
4034 return -1;
4035 if (di_a->total_avail < di_b->total_avail)
4036 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004037 return 0;
4038}
4039
Eric Sandeen48a3b632013-04-25 20:41:01 +00004040static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004041 [BTRFS_RAID_RAID10] = {
4042 .sub_stripes = 2,
4043 .dev_stripes = 1,
4044 .devs_max = 0, /* 0 == as many as possible */
4045 .devs_min = 4,
4046 .devs_increment = 2,
4047 .ncopies = 2,
4048 },
4049 [BTRFS_RAID_RAID1] = {
4050 .sub_stripes = 1,
4051 .dev_stripes = 1,
4052 .devs_max = 2,
4053 .devs_min = 2,
4054 .devs_increment = 2,
4055 .ncopies = 2,
4056 },
4057 [BTRFS_RAID_DUP] = {
4058 .sub_stripes = 1,
4059 .dev_stripes = 2,
4060 .devs_max = 1,
4061 .devs_min = 1,
4062 .devs_increment = 1,
4063 .ncopies = 2,
4064 },
4065 [BTRFS_RAID_RAID0] = {
4066 .sub_stripes = 1,
4067 .dev_stripes = 1,
4068 .devs_max = 0,
4069 .devs_min = 2,
4070 .devs_increment = 1,
4071 .ncopies = 1,
4072 },
4073 [BTRFS_RAID_SINGLE] = {
4074 .sub_stripes = 1,
4075 .dev_stripes = 1,
4076 .devs_max = 1,
4077 .devs_min = 1,
4078 .devs_increment = 1,
4079 .ncopies = 1,
4080 },
Chris Masone942f882013-02-20 14:06:05 -05004081 [BTRFS_RAID_RAID5] = {
4082 .sub_stripes = 1,
4083 .dev_stripes = 1,
4084 .devs_max = 0,
4085 .devs_min = 2,
4086 .devs_increment = 1,
4087 .ncopies = 2,
4088 },
4089 [BTRFS_RAID_RAID6] = {
4090 .sub_stripes = 1,
4091 .dev_stripes = 1,
4092 .devs_max = 0,
4093 .devs_min = 3,
4094 .devs_increment = 1,
4095 .ncopies = 3,
4096 },
Liu Bo31e50222012-11-21 14:18:10 +00004097};
4098
David Woodhouse53b381b2013-01-29 18:40:14 -05004099static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4100{
4101 /* TODO allow them to set a preferred stripe size */
4102 return 64 * 1024;
4103}
4104
4105static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4106{
David Woodhouse53b381b2013-01-29 18:40:14 -05004107 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
4108 return;
4109
Miao Xieceda0862013-04-11 10:30:16 +00004110 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004111}
4112
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004113#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4114 - sizeof(struct btrfs_item) \
4115 - sizeof(struct btrfs_chunk)) \
4116 / sizeof(struct btrfs_stripe) + 1)
4117
4118#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4119 - 2 * sizeof(struct btrfs_disk_key) \
4120 - 2 * sizeof(struct btrfs_chunk)) \
4121 / sizeof(struct btrfs_stripe) + 1)
4122
Miao Xieb2117a32011-01-05 10:07:28 +00004123static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004124 struct btrfs_root *extent_root, u64 start,
4125 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004126{
4127 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004128 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4129 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004130 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004131 struct extent_map_tree *em_tree;
4132 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004133 struct btrfs_device_info *devices_info = NULL;
4134 u64 total_avail;
4135 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004136 int data_stripes; /* number of stripes that count for
4137 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004138 int sub_stripes; /* sub_stripes info for map */
4139 int dev_stripes; /* stripes per dev */
4140 int devs_max; /* max devs to use */
4141 int devs_min; /* min devs needed */
4142 int devs_increment; /* ndevs has to be a multiple of this */
4143 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004144 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004145 u64 max_stripe_size;
4146 u64 max_chunk_size;
4147 u64 stripe_size;
4148 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004149 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004150 int ndevs;
4151 int i;
4152 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004153 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004154
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004155 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004156
Miao Xieb2117a32011-01-05 10:07:28 +00004157 if (list_empty(&fs_devices->alloc_list))
4158 return -ENOSPC;
4159
Liu Bo31e50222012-11-21 14:18:10 +00004160 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004161
Liu Bo31e50222012-11-21 14:18:10 +00004162 sub_stripes = btrfs_raid_array[index].sub_stripes;
4163 dev_stripes = btrfs_raid_array[index].dev_stripes;
4164 devs_max = btrfs_raid_array[index].devs_max;
4165 devs_min = btrfs_raid_array[index].devs_min;
4166 devs_increment = btrfs_raid_array[index].devs_increment;
4167 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004168
4169 if (type & BTRFS_BLOCK_GROUP_DATA) {
4170 max_stripe_size = 1024 * 1024 * 1024;
4171 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004172 if (!devs_max)
4173 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004174 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004175 /* for larger filesystems, use larger metadata chunks */
4176 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4177 max_stripe_size = 1024 * 1024 * 1024;
4178 else
4179 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004180 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004181 if (!devs_max)
4182 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004183 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004184 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004185 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004186 if (!devs_max)
4187 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004188 } else {
David Sterba351fd352014-05-15 16:48:20 +02004189 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004190 type);
4191 BUG_ON(1);
4192 }
4193
4194 /* we don't want a chunk larger than 10% of writeable space */
4195 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4196 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004197
4198 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4199 GFP_NOFS);
4200 if (!devices_info)
4201 return -ENOMEM;
4202
Arne Jansen73c5de02011-04-12 12:07:57 +02004203 cur = fs_devices->alloc_list.next;
4204
4205 /*
4206 * in the first pass through the devices list, we gather information
4207 * about the available holes on each device.
4208 */
4209 ndevs = 0;
4210 while (cur != &fs_devices->alloc_list) {
4211 struct btrfs_device *device;
4212 u64 max_avail;
4213 u64 dev_offset;
4214
4215 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4216
4217 cur = cur->next;
4218
4219 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004220 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004221 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004222 continue;
4223 }
4224
Stefan Behrens63a212a2012-11-05 18:29:28 +01004225 if (!device->in_fs_metadata ||
4226 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004227 continue;
4228
4229 if (device->total_bytes > device->bytes_used)
4230 total_avail = device->total_bytes - device->bytes_used;
4231 else
4232 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004233
4234 /* If there is no space on this device, skip it. */
4235 if (total_avail == 0)
4236 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004237
Josef Bacik6df9a952013-06-27 13:22:46 -04004238 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004239 max_stripe_size * dev_stripes,
4240 &dev_offset, &max_avail);
4241 if (ret && ret != -ENOSPC)
4242 goto error;
4243
4244 if (ret == 0)
4245 max_avail = max_stripe_size * dev_stripes;
4246
4247 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4248 continue;
4249
Eric Sandeen063d0062013-01-31 00:55:01 +00004250 if (ndevs == fs_devices->rw_devices) {
4251 WARN(1, "%s: found more than %llu devices\n",
4252 __func__, fs_devices->rw_devices);
4253 break;
4254 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004255 devices_info[ndevs].dev_offset = dev_offset;
4256 devices_info[ndevs].max_avail = max_avail;
4257 devices_info[ndevs].total_avail = total_avail;
4258 devices_info[ndevs].dev = device;
4259 ++ndevs;
4260 }
4261
4262 /*
4263 * now sort the devices by hole size / available space
4264 */
4265 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4266 btrfs_cmp_device_info, NULL);
4267
4268 /* round down to number of usable stripes */
4269 ndevs -= ndevs % devs_increment;
4270
4271 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4272 ret = -ENOSPC;
4273 goto error;
4274 }
4275
4276 if (devs_max && ndevs > devs_max)
4277 ndevs = devs_max;
4278 /*
4279 * the primary goal is to maximize the number of stripes, so use as many
4280 * devices as possible, even if the stripes are not maximum sized.
4281 */
4282 stripe_size = devices_info[ndevs-1].max_avail;
4283 num_stripes = ndevs * dev_stripes;
4284
David Woodhouse53b381b2013-01-29 18:40:14 -05004285 /*
4286 * this will have to be fixed for RAID1 and RAID10 over
4287 * more drives
4288 */
4289 data_stripes = num_stripes / ncopies;
4290
David Woodhouse53b381b2013-01-29 18:40:14 -05004291 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4292 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4293 btrfs_super_stripesize(info->super_copy));
4294 data_stripes = num_stripes - 1;
4295 }
4296 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4297 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4298 btrfs_super_stripesize(info->super_copy));
4299 data_stripes = num_stripes - 2;
4300 }
Chris Mason86db2572013-02-20 16:23:40 -05004301
4302 /*
4303 * Use the number of data stripes to figure out how big this chunk
4304 * is really going to be in terms of logical address space,
4305 * and compare that answer with the max chunk size
4306 */
4307 if (stripe_size * data_stripes > max_chunk_size) {
4308 u64 mask = (1ULL << 24) - 1;
4309 stripe_size = max_chunk_size;
4310 do_div(stripe_size, data_stripes);
4311
4312 /* bump the answer up to a 16MB boundary */
4313 stripe_size = (stripe_size + mask) & ~mask;
4314
4315 /* but don't go higher than the limits we found
4316 * while searching for free extents
4317 */
4318 if (stripe_size > devices_info[ndevs-1].max_avail)
4319 stripe_size = devices_info[ndevs-1].max_avail;
4320 }
4321
Arne Jansen73c5de02011-04-12 12:07:57 +02004322 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004323
4324 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004325 do_div(stripe_size, raid_stripe_len);
4326 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004327
Miao Xieb2117a32011-01-05 10:07:28 +00004328 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4329 if (!map) {
4330 ret = -ENOMEM;
4331 goto error;
4332 }
4333 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004334
Arne Jansen73c5de02011-04-12 12:07:57 +02004335 for (i = 0; i < ndevs; ++i) {
4336 for (j = 0; j < dev_stripes; ++j) {
4337 int s = i * dev_stripes + j;
4338 map->stripes[s].dev = devices_info[i].dev;
4339 map->stripes[s].physical = devices_info[i].dev_offset +
4340 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004341 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004342 }
Chris Mason593060d2008-03-25 16:50:33 -04004343 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004344 map->stripe_len = raid_stripe_len;
4345 map->io_align = raid_stripe_len;
4346 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004347 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004348 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004349
David Woodhouse53b381b2013-01-29 18:40:14 -05004350 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004351
Arne Jansen73c5de02011-04-12 12:07:57 +02004352 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004353
David Sterba172ddd62011-04-21 00:48:27 +02004354 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004355 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004356 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004357 ret = -ENOMEM;
4358 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004359 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004360 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004361 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004362 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004363 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004364 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004365 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004366 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004367
Chris Mason0b86a832008-03-24 15:01:56 -04004368 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004369 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004370 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004371 if (!ret) {
4372 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4373 atomic_inc(&em->refs);
4374 }
Chris Mason890871b2009-09-02 16:24:52 -04004375 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004376 if (ret) {
4377 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004378 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004379 }
Yan Zheng2b820322008-11-17 21:11:30 -05004380
Josef Bacik04487482013-01-29 15:03:37 -05004381 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4382 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4383 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004384 if (ret)
4385 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004386
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004387 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004388 check_raid56_incompat_flag(extent_root->fs_info, type);
4389
Miao Xieb2117a32011-01-05 10:07:28 +00004390 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004391 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004392
Josef Bacik6df9a952013-06-27 13:22:46 -04004393error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004394 write_lock(&em_tree->lock);
4395 remove_extent_mapping(em_tree, em);
4396 write_unlock(&em_tree->lock);
4397
4398 /* One for our allocation */
4399 free_extent_map(em);
4400 /* One for the tree reference */
4401 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004402error:
Miao Xieb2117a32011-01-05 10:07:28 +00004403 kfree(devices_info);
4404 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004405}
4406
Josef Bacik6df9a952013-06-27 13:22:46 -04004407int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004408 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004409 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004410{
Yan Zheng2b820322008-11-17 21:11:30 -05004411 struct btrfs_key key;
4412 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4413 struct btrfs_device *device;
4414 struct btrfs_chunk *chunk;
4415 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004416 struct extent_map_tree *em_tree;
4417 struct extent_map *em;
4418 struct map_lookup *map;
4419 size_t item_size;
4420 u64 dev_offset;
4421 u64 stripe_size;
4422 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004423 int ret;
4424
Josef Bacik6df9a952013-06-27 13:22:46 -04004425 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4426 read_lock(&em_tree->lock);
4427 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4428 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004429
Josef Bacik6df9a952013-06-27 13:22:46 -04004430 if (!em) {
4431 btrfs_crit(extent_root->fs_info, "unable to find logical "
4432 "%Lu len %Lu", chunk_offset, chunk_size);
4433 return -EINVAL;
4434 }
4435
4436 if (em->start != chunk_offset || em->len != chunk_size) {
4437 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004438 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004439 chunk_size, em->start, em->len);
4440 free_extent_map(em);
4441 return -EINVAL;
4442 }
4443
4444 map = (struct map_lookup *)em->bdev;
4445 item_size = btrfs_chunk_item_size(map->num_stripes);
4446 stripe_size = em->orig_block_len;
4447
4448 chunk = kzalloc(item_size, GFP_NOFS);
4449 if (!chunk) {
4450 ret = -ENOMEM;
4451 goto out;
4452 }
4453
4454 for (i = 0; i < map->num_stripes; i++) {
4455 device = map->stripes[i].dev;
4456 dev_offset = map->stripes[i].physical;
4457
Yan Zheng2b820322008-11-17 21:11:30 -05004458 device->bytes_used += stripe_size;
4459 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004460 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004461 goto out;
4462 ret = btrfs_alloc_dev_extent(trans, device,
4463 chunk_root->root_key.objectid,
4464 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4465 chunk_offset, dev_offset,
4466 stripe_size);
4467 if (ret)
4468 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004469 }
4470
Josef Bacik2bf64752011-09-26 17:12:22 -04004471 spin_lock(&extent_root->fs_info->free_chunk_lock);
4472 extent_root->fs_info->free_chunk_space -= (stripe_size *
4473 map->num_stripes);
4474 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4475
Yan Zheng2b820322008-11-17 21:11:30 -05004476 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004477 for (i = 0; i < map->num_stripes; i++) {
4478 device = map->stripes[i].dev;
4479 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004480
4481 btrfs_set_stack_stripe_devid(stripe, device->devid);
4482 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4483 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4484 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004485 }
4486
4487 btrfs_set_stack_chunk_length(chunk, chunk_size);
4488 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4489 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4490 btrfs_set_stack_chunk_type(chunk, map->type);
4491 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4492 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4493 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4494 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4495 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4496
4497 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4498 key.type = BTRFS_CHUNK_ITEM_KEY;
4499 key.offset = chunk_offset;
4500
4501 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004502 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4503 /*
4504 * TODO: Cleanup of inserted chunk root in case of
4505 * failure.
4506 */
Li Zefan125ccb02011-12-08 15:07:24 +08004507 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004508 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004509 }
liubo1abe9b82011-03-24 11:18:59 +00004510
Josef Bacik6df9a952013-06-27 13:22:46 -04004511out:
Yan Zheng2b820322008-11-17 21:11:30 -05004512 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004513 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004514 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004515}
4516
4517/*
4518 * Chunk allocation falls into two parts. The first part does works
4519 * that make the new allocated chunk useable, but not do any operation
4520 * that modifies the chunk tree. The second part does the works that
4521 * require modifying the chunk tree. This division is important for the
4522 * bootstrap process of adding storage to a seed btrfs.
4523 */
4524int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4525 struct btrfs_root *extent_root, u64 type)
4526{
4527 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004528
Josef Bacik6df9a952013-06-27 13:22:46 -04004529 chunk_offset = find_next_chunk(extent_root->fs_info);
4530 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004531}
4532
Chris Masond3977122009-01-05 21:25:51 -05004533static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004534 struct btrfs_root *root,
4535 struct btrfs_device *device)
4536{
4537 u64 chunk_offset;
4538 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004539 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004540 struct btrfs_fs_info *fs_info = root->fs_info;
4541 struct btrfs_root *extent_root = fs_info->extent_root;
4542 int ret;
4543
Josef Bacik6df9a952013-06-27 13:22:46 -04004544 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004545 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004546 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4547 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004548 if (ret)
4549 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004550
Josef Bacik6df9a952013-06-27 13:22:46 -04004551 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004552 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004553 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4554 alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004555 if (ret) {
4556 btrfs_abort_transaction(trans, root, ret);
4557 goto out;
4558 }
Yan Zheng2b820322008-11-17 21:11:30 -05004559
4560 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004561 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004562 btrfs_abort_transaction(trans, root, ret);
David Sterba005d6422012-09-18 07:52:32 -06004563out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004564 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004565}
4566
Miao Xied20983b2014-07-03 18:22:13 +08004567static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4568{
4569 int max_errors;
4570
4571 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4572 BTRFS_BLOCK_GROUP_RAID10 |
4573 BTRFS_BLOCK_GROUP_RAID5 |
4574 BTRFS_BLOCK_GROUP_DUP)) {
4575 max_errors = 1;
4576 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4577 max_errors = 2;
4578 } else {
4579 max_errors = 0;
4580 }
4581
4582 return max_errors;
4583}
4584
Yan Zheng2b820322008-11-17 21:11:30 -05004585int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4586{
4587 struct extent_map *em;
4588 struct map_lookup *map;
4589 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4590 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004591 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004592 int i;
4593
Chris Mason890871b2009-09-02 16:24:52 -04004594 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004595 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004596 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004597 if (!em)
4598 return 1;
4599
4600 map = (struct map_lookup *)em->bdev;
4601 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004602 if (map->stripes[i].dev->missing) {
4603 miss_ndevs++;
4604 continue;
4605 }
4606
Yan Zheng2b820322008-11-17 21:11:30 -05004607 if (!map->stripes[i].dev->writeable) {
4608 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004609 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004610 }
4611 }
Miao Xied20983b2014-07-03 18:22:13 +08004612
4613 /*
4614 * If the number of missing devices is larger than max errors,
4615 * we can not write the data into that chunk successfully, so
4616 * set it readonly.
4617 */
4618 if (miss_ndevs > btrfs_chunk_max_errors(map))
4619 readonly = 1;
4620end:
Yan Zheng2b820322008-11-17 21:11:30 -05004621 free_extent_map(em);
4622 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004623}
4624
4625void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4626{
David Sterbaa8067e02011-04-21 00:34:43 +02004627 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004628}
4629
4630void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4631{
4632 struct extent_map *em;
4633
Chris Masond3977122009-01-05 21:25:51 -05004634 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004635 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004636 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4637 if (em)
4638 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004639 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004640 if (!em)
4641 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004642 /* once for us */
4643 free_extent_map(em);
4644 /* once for the tree */
4645 free_extent_map(em);
4646 }
4647}
4648
Stefan Behrens5d964052012-11-05 14:59:07 +01004649int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004650{
Stefan Behrens5d964052012-11-05 14:59:07 +01004651 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004652 struct extent_map *em;
4653 struct map_lookup *map;
4654 struct extent_map_tree *em_tree = &map_tree->map_tree;
4655 int ret;
4656
Chris Mason890871b2009-09-02 16:24:52 -04004657 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004658 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004659 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004660
Josef Bacikfb7669b2013-04-23 10:53:18 -04004661 /*
4662 * We could return errors for these cases, but that could get ugly and
4663 * we'd probably do the same thing which is just not do anything else
4664 * and exit, so return 1 so the callers don't try to use other copies.
4665 */
4666 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004667 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004668 logical+len);
4669 return 1;
4670 }
4671
4672 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004673 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004674 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004675 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004676 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004677 return 1;
4678 }
4679
Chris Masonf1885912008-04-09 16:28:12 -04004680 map = (struct map_lookup *)em->bdev;
4681 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4682 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004683 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4684 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004685 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4686 ret = 2;
4687 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4688 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004689 else
4690 ret = 1;
4691 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004692
4693 btrfs_dev_replace_lock(&fs_info->dev_replace);
4694 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4695 ret++;
4696 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4697
Chris Masonf1885912008-04-09 16:28:12 -04004698 return ret;
4699}
4700
David Woodhouse53b381b2013-01-29 18:40:14 -05004701unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4702 struct btrfs_mapping_tree *map_tree,
4703 u64 logical)
4704{
4705 struct extent_map *em;
4706 struct map_lookup *map;
4707 struct extent_map_tree *em_tree = &map_tree->map_tree;
4708 unsigned long len = root->sectorsize;
4709
4710 read_lock(&em_tree->lock);
4711 em = lookup_extent_mapping(em_tree, logical, len);
4712 read_unlock(&em_tree->lock);
4713 BUG_ON(!em);
4714
4715 BUG_ON(em->start > logical || em->start + em->len < logical);
4716 map = (struct map_lookup *)em->bdev;
4717 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4718 BTRFS_BLOCK_GROUP_RAID6)) {
4719 len = map->stripe_len * nr_data_stripes(map);
4720 }
4721 free_extent_map(em);
4722 return len;
4723}
4724
4725int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4726 u64 logical, u64 len, int mirror_num)
4727{
4728 struct extent_map *em;
4729 struct map_lookup *map;
4730 struct extent_map_tree *em_tree = &map_tree->map_tree;
4731 int ret = 0;
4732
4733 read_lock(&em_tree->lock);
4734 em = lookup_extent_mapping(em_tree, logical, len);
4735 read_unlock(&em_tree->lock);
4736 BUG_ON(!em);
4737
4738 BUG_ON(em->start > logical || em->start + em->len < logical);
4739 map = (struct map_lookup *)em->bdev;
4740 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4741 BTRFS_BLOCK_GROUP_RAID6))
4742 ret = 1;
4743 free_extent_map(em);
4744 return ret;
4745}
4746
Stefan Behrens30d98612012-11-06 14:52:18 +01004747static int find_live_mirror(struct btrfs_fs_info *fs_info,
4748 struct map_lookup *map, int first, int num,
4749 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004750{
4751 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004752 int tolerance;
4753 struct btrfs_device *srcdev;
4754
4755 if (dev_replace_is_ongoing &&
4756 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4757 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4758 srcdev = fs_info->dev_replace.srcdev;
4759 else
4760 srcdev = NULL;
4761
4762 /*
4763 * try to avoid the drive that is the source drive for a
4764 * dev-replace procedure, only choose it if no other non-missing
4765 * mirror is available
4766 */
4767 for (tolerance = 0; tolerance < 2; tolerance++) {
4768 if (map->stripes[optimal].dev->bdev &&
4769 (tolerance || map->stripes[optimal].dev != srcdev))
4770 return optimal;
4771 for (i = first; i < first + num; i++) {
4772 if (map->stripes[i].dev->bdev &&
4773 (tolerance || map->stripes[i].dev != srcdev))
4774 return i;
4775 }
Chris Masondfe25022008-05-13 13:46:40 -04004776 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004777
Chris Masondfe25022008-05-13 13:46:40 -04004778 /* we couldn't find one that doesn't fail. Just return something
4779 * and the io error handling code will clean up eventually
4780 */
4781 return optimal;
4782}
4783
David Woodhouse53b381b2013-01-29 18:40:14 -05004784static inline int parity_smaller(u64 a, u64 b)
4785{
4786 return a > b;
4787}
4788
4789/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4790static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4791{
4792 struct btrfs_bio_stripe s;
4793 int i;
4794 u64 l;
4795 int again = 1;
4796
4797 while (again) {
4798 again = 0;
4799 for (i = 0; i < bbio->num_stripes - 1; i++) {
4800 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4801 s = bbio->stripes[i];
4802 l = raid_map[i];
4803 bbio->stripes[i] = bbio->stripes[i+1];
4804 raid_map[i] = raid_map[i+1];
4805 bbio->stripes[i+1] = s;
4806 raid_map[i+1] = l;
4807 again = 1;
4808 }
4809 }
4810 }
4811}
4812
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004813static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004814 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004815 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004816 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004817{
4818 struct extent_map *em;
4819 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004820 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004821 struct extent_map_tree *em_tree = &map_tree->map_tree;
4822 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004823 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004824 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004825 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004826 u64 stripe_nr_orig;
4827 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004828 u64 stripe_len;
4829 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004830 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004831 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004832 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004833 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004834 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004835 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004836 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4837 int dev_replace_is_ongoing = 0;
4838 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004839 int patch_the_first_stripe_for_dev_replace = 0;
4840 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004841 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004842
Chris Mason890871b2009-09-02 16:24:52 -04004843 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004844 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004845 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004846
Chris Mason3b951512008-04-17 11:29:12 -04004847 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004848 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004849 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004850 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004851 }
Chris Mason0b86a832008-03-24 15:01:56 -04004852
Josef Bacik9bb91872013-04-19 23:45:33 -04004853 if (em->start > logical || em->start + em->len < logical) {
4854 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004855 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004856 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004857 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004858 return -EINVAL;
4859 }
4860
Chris Mason0b86a832008-03-24 15:01:56 -04004861 map = (struct map_lookup *)em->bdev;
4862 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004863
David Woodhouse53b381b2013-01-29 18:40:14 -05004864 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004865 stripe_nr = offset;
4866 /*
4867 * stripe_nr counts the total number of stripes we have to stride
4868 * to get to this block
4869 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004870 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004871
David Woodhouse53b381b2013-01-29 18:40:14 -05004872 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004873 BUG_ON(offset < stripe_offset);
4874
4875 /* stripe_offset is the offset of this block in its stripe*/
4876 stripe_offset = offset - stripe_offset;
4877
David Woodhouse53b381b2013-01-29 18:40:14 -05004878 /* if we're here for raid56, we need to know the stripe aligned start */
4879 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4880 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4881 raid56_full_stripe_start = offset;
4882
4883 /* allow a write of a full stripe, but make sure we don't
4884 * allow straddling of stripes
4885 */
4886 do_div(raid56_full_stripe_start, full_stripe_len);
4887 raid56_full_stripe_start *= full_stripe_len;
4888 }
4889
4890 if (rw & REQ_DISCARD) {
4891 /* we don't discard raid56 yet */
4892 if (map->type &
4893 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4894 ret = -EOPNOTSUPP;
4895 goto out;
4896 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004897 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004898 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4899 u64 max_len;
4900 /* For writes to RAID[56], allow a full stripeset across all disks.
4901 For other RAID types and for RAID[56] reads, just allow a single
4902 stripe (on a single disk). */
4903 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4904 (rw & REQ_WRITE)) {
4905 max_len = stripe_len * nr_data_stripes(map) -
4906 (offset - raid56_full_stripe_start);
4907 } else {
4908 /* we limit the length of each bio to what fits in a stripe */
4909 max_len = stripe_len - stripe_offset;
4910 }
4911 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004912 } else {
4913 *length = em->len - offset;
4914 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004915
David Woodhouse53b381b2013-01-29 18:40:14 -05004916 /* This is for when we're called from btrfs_merge_bio_hook() and all
4917 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004918 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004919 goto out;
4920
Stefan Behrens472262f2012-11-06 14:43:46 +01004921 btrfs_dev_replace_lock(dev_replace);
4922 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4923 if (!dev_replace_is_ongoing)
4924 btrfs_dev_replace_unlock(dev_replace);
4925
Stefan Behrensad6d6202012-11-06 15:06:47 +01004926 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4927 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4928 dev_replace->tgtdev != NULL) {
4929 /*
4930 * in dev-replace case, for repair case (that's the only
4931 * case where the mirror is selected explicitly when
4932 * calling btrfs_map_block), blocks left of the left cursor
4933 * can also be read from the target drive.
4934 * For REQ_GET_READ_MIRRORS, the target drive is added as
4935 * the last one to the array of stripes. For READ, it also
4936 * needs to be supported using the same mirror number.
4937 * If the requested block is not left of the left cursor,
4938 * EIO is returned. This can happen because btrfs_num_copies()
4939 * returns one more in the dev-replace case.
4940 */
4941 u64 tmp_length = *length;
4942 struct btrfs_bio *tmp_bbio = NULL;
4943 int tmp_num_stripes;
4944 u64 srcdev_devid = dev_replace->srcdev->devid;
4945 int index_srcdev = 0;
4946 int found = 0;
4947 u64 physical_of_found = 0;
4948
4949 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004950 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004951 if (ret) {
4952 WARN_ON(tmp_bbio != NULL);
4953 goto out;
4954 }
4955
4956 tmp_num_stripes = tmp_bbio->num_stripes;
4957 if (mirror_num > tmp_num_stripes) {
4958 /*
4959 * REQ_GET_READ_MIRRORS does not contain this
4960 * mirror, that means that the requested area
4961 * is not left of the left cursor
4962 */
4963 ret = -EIO;
4964 kfree(tmp_bbio);
4965 goto out;
4966 }
4967
4968 /*
4969 * process the rest of the function using the mirror_num
4970 * of the source drive. Therefore look it up first.
4971 * At the end, patch the device pointer to the one of the
4972 * target drive.
4973 */
4974 for (i = 0; i < tmp_num_stripes; i++) {
4975 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4976 /*
4977 * In case of DUP, in order to keep it
4978 * simple, only add the mirror with the
4979 * lowest physical address
4980 */
4981 if (found &&
4982 physical_of_found <=
4983 tmp_bbio->stripes[i].physical)
4984 continue;
4985 index_srcdev = i;
4986 found = 1;
4987 physical_of_found =
4988 tmp_bbio->stripes[i].physical;
4989 }
4990 }
4991
4992 if (found) {
4993 mirror_num = index_srcdev + 1;
4994 patch_the_first_stripe_for_dev_replace = 1;
4995 physical_to_patch_in_first_stripe = physical_of_found;
4996 } else {
4997 WARN_ON(1);
4998 ret = -EIO;
4999 kfree(tmp_bbio);
5000 goto out;
5001 }
5002
5003 kfree(tmp_bbio);
5004 } else if (mirror_num > map->num_stripes) {
5005 mirror_num = 0;
5006 }
5007
Chris Masonf2d8d742008-04-21 10:03:05 -04005008 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005009 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005010 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005011 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005012 do_div(stripe_nr_end, map->stripe_len);
5013 stripe_end_offset = stripe_nr_end * map->stripe_len -
5014 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005015
Li Dongyangfce3bb92011-03-24 10:24:26 +00005016 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5017 if (rw & REQ_DISCARD)
5018 num_stripes = min_t(u64, map->num_stripes,
5019 stripe_nr_end - stripe_nr_orig);
5020 stripe_index = do_div(stripe_nr, map->num_stripes);
5021 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005022 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005023 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005024 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005025 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005026 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005027 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005028 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005029 current->pid % map->num_stripes,
5030 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005031 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005032 }
Chris Mason2fff7342008-04-29 14:12:09 -04005033
Chris Mason611f0e02008-04-03 16:29:03 -04005034 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005035 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005036 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005037 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005038 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005039 } else {
5040 mirror_num = 1;
5041 }
Chris Mason2fff7342008-04-29 14:12:09 -04005042
Chris Mason321aecc2008-04-16 10:49:51 -04005043 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5044 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005045
5046 stripe_index = do_div(stripe_nr, factor);
5047 stripe_index *= map->sub_stripes;
5048
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005049 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005050 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005051 else if (rw & REQ_DISCARD)
5052 num_stripes = min_t(u64, map->sub_stripes *
5053 (stripe_nr_end - stripe_nr_orig),
5054 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005055 else if (mirror_num)
5056 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005057 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005058 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005059 stripe_index = find_live_mirror(fs_info, map,
5060 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005061 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005062 current->pid % map->sub_stripes,
5063 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005064 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005065 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005066
5067 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5068 BTRFS_BLOCK_GROUP_RAID6)) {
5069 u64 tmp;
5070
5071 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
5072 && raid_map_ret) {
5073 int i, rot;
5074
5075 /* push stripe_nr back to the start of the full stripe */
5076 stripe_nr = raid56_full_stripe_start;
5077 do_div(stripe_nr, stripe_len);
5078
5079 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5080
5081 /* RAID[56] write or recovery. Return all stripes */
5082 num_stripes = map->num_stripes;
5083 max_errors = nr_parity_stripes(map);
5084
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05305085 raid_map = kmalloc_array(num_stripes, sizeof(u64),
David Woodhouse53b381b2013-01-29 18:40:14 -05005086 GFP_NOFS);
5087 if (!raid_map) {
5088 ret = -ENOMEM;
5089 goto out;
5090 }
5091
5092 /* Work out the disk rotation on this stripe-set */
5093 tmp = stripe_nr;
5094 rot = do_div(tmp, num_stripes);
5095
5096 /* Fill in the logical address of each stripe */
5097 tmp = stripe_nr * nr_data_stripes(map);
5098 for (i = 0; i < nr_data_stripes(map); i++)
5099 raid_map[(i+rot) % num_stripes] =
5100 em->start + (tmp + i) * map->stripe_len;
5101
5102 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5103 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5104 raid_map[(i+rot+1) % num_stripes] =
5105 RAID6_Q_STRIPE;
5106
5107 *length = map->stripe_len;
5108 stripe_index = 0;
5109 stripe_offset = 0;
5110 } else {
5111 /*
5112 * Mirror #0 or #1 means the original data block.
5113 * Mirror #2 is RAID5 parity block.
5114 * Mirror #3 is RAID6 Q block.
5115 */
5116 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5117 if (mirror_num > 1)
5118 stripe_index = nr_data_stripes(map) +
5119 mirror_num - 2;
5120
5121 /* We distribute the parity blocks across stripes */
5122 tmp = stripe_nr + stripe_index;
5123 stripe_index = do_div(tmp, map->num_stripes);
5124 }
Chris Mason8790d502008-04-03 16:29:03 -04005125 } else {
5126 /*
5127 * after this do_div call, stripe_nr is the number of stripes
5128 * on this device we have to walk to find the data, and
5129 * stripe_index is the number of our device in the stripe array
5130 */
5131 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005132 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005133 }
Chris Mason593060d2008-03-25 16:50:33 -04005134 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005135
Stefan Behrens472262f2012-11-06 14:43:46 +01005136 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005137 if (dev_replace_is_ongoing) {
5138 if (rw & (REQ_WRITE | REQ_DISCARD))
5139 num_alloc_stripes <<= 1;
5140 if (rw & REQ_GET_READ_MIRRORS)
5141 num_alloc_stripes++;
5142 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005143 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08005144 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04005145 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08005146 ret = -ENOMEM;
5147 goto out;
5148 }
5149 atomic_set(&bbio->error, 0);
5150
Li Dongyangfce3bb92011-03-24 10:24:26 +00005151 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08005152 int factor = 0;
5153 int sub_stripes = 0;
5154 u64 stripes_per_dev = 0;
5155 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005156 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005157
5158 if (map->type &
5159 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5160 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5161 sub_stripes = 1;
5162 else
5163 sub_stripes = map->sub_stripes;
5164
5165 factor = map->num_stripes / sub_stripes;
5166 stripes_per_dev = div_u64_rem(stripe_nr_end -
5167 stripe_nr_orig,
5168 factor,
5169 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005170 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5171 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005172 }
5173
Li Dongyangfce3bb92011-03-24 10:24:26 +00005174 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005175 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005176 map->stripes[stripe_index].physical +
5177 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005178 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005179
Li Zefanec9ef7a2011-12-01 14:06:42 +08005180 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5181 BTRFS_BLOCK_GROUP_RAID10)) {
5182 bbio->stripes[i].length = stripes_per_dev *
5183 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005184
Li Zefanec9ef7a2011-12-01 14:06:42 +08005185 if (i / sub_stripes < remaining_stripes)
5186 bbio->stripes[i].length +=
5187 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005188
5189 /*
5190 * Special for the first stripe and
5191 * the last stripe:
5192 *
5193 * |-------|...|-------|
5194 * |----------|
5195 * off end_off
5196 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005197 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005198 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005199 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005200
5201 if (stripe_index >= last_stripe &&
5202 stripe_index <= (last_stripe +
5203 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005204 bbio->stripes[i].length -=
5205 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005206
Li Zefanec9ef7a2011-12-01 14:06:42 +08005207 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005208 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005209 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005210 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005211
5212 stripe_index++;
5213 if (stripe_index == map->num_stripes) {
5214 /* This could only happen for RAID0/10 */
5215 stripe_index = 0;
5216 stripe_nr++;
5217 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005218 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005219 } else {
5220 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005221 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005222 map->stripes[stripe_index].physical +
5223 stripe_offset +
5224 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005225 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005226 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005227 stripe_index++;
5228 }
Chris Mason593060d2008-03-25 16:50:33 -04005229 }
Li Zefande11cc12011-12-01 12:55:47 +08005230
Miao Xied20983b2014-07-03 18:22:13 +08005231 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5232 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005233
Stefan Behrens472262f2012-11-06 14:43:46 +01005234 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5235 dev_replace->tgtdev != NULL) {
5236 int index_where_to_add;
5237 u64 srcdev_devid = dev_replace->srcdev->devid;
5238
5239 /*
5240 * duplicate the write operations while the dev replace
5241 * procedure is running. Since the copying of the old disk
5242 * to the new disk takes place at run time while the
5243 * filesystem is mounted writable, the regular write
5244 * operations to the old disk have to be duplicated to go
5245 * to the new disk as well.
5246 * Note that device->missing is handled by the caller, and
5247 * that the write to the old disk is already set up in the
5248 * stripes array.
5249 */
5250 index_where_to_add = num_stripes;
5251 for (i = 0; i < num_stripes; i++) {
5252 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5253 /* write to new disk, too */
5254 struct btrfs_bio_stripe *new =
5255 bbio->stripes + index_where_to_add;
5256 struct btrfs_bio_stripe *old =
5257 bbio->stripes + i;
5258
5259 new->physical = old->physical;
5260 new->length = old->length;
5261 new->dev = dev_replace->tgtdev;
5262 index_where_to_add++;
5263 max_errors++;
5264 }
5265 }
5266 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005267 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5268 dev_replace->tgtdev != NULL) {
5269 u64 srcdev_devid = dev_replace->srcdev->devid;
5270 int index_srcdev = 0;
5271 int found = 0;
5272 u64 physical_of_found = 0;
5273
5274 /*
5275 * During the dev-replace procedure, the target drive can
5276 * also be used to read data in case it is needed to repair
5277 * a corrupt block elsewhere. This is possible if the
5278 * requested area is left of the left cursor. In this area,
5279 * the target drive is a full copy of the source drive.
5280 */
5281 for (i = 0; i < num_stripes; i++) {
5282 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5283 /*
5284 * In case of DUP, in order to keep it
5285 * simple, only add the mirror with the
5286 * lowest physical address
5287 */
5288 if (found &&
5289 physical_of_found <=
5290 bbio->stripes[i].physical)
5291 continue;
5292 index_srcdev = i;
5293 found = 1;
5294 physical_of_found = bbio->stripes[i].physical;
5295 }
5296 }
5297 if (found) {
5298 u64 length = map->stripe_len;
5299
5300 if (physical_of_found + length <=
5301 dev_replace->cursor_left) {
5302 struct btrfs_bio_stripe *tgtdev_stripe =
5303 bbio->stripes + num_stripes;
5304
5305 tgtdev_stripe->physical = physical_of_found;
5306 tgtdev_stripe->length =
5307 bbio->stripes[index_srcdev].length;
5308 tgtdev_stripe->dev = dev_replace->tgtdev;
5309
5310 num_stripes++;
5311 }
5312 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005313 }
5314
Li Zefande11cc12011-12-01 12:55:47 +08005315 *bbio_ret = bbio;
5316 bbio->num_stripes = num_stripes;
5317 bbio->max_errors = max_errors;
5318 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005319
5320 /*
5321 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5322 * mirror_num == num_stripes + 1 && dev_replace target drive is
5323 * available as a mirror
5324 */
5325 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5326 WARN_ON(num_stripes > 1);
5327 bbio->stripes[0].dev = dev_replace->tgtdev;
5328 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5329 bbio->mirror_num = map->num_stripes + 1;
5330 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005331 if (raid_map) {
5332 sort_parity_stripes(bbio, raid_map);
5333 *raid_map_ret = raid_map;
5334 }
Chris Masoncea9e442008-04-09 16:28:12 -04005335out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005336 if (dev_replace_is_ongoing)
5337 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005338 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005339 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005340}
5341
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005342int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005343 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005344 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005345{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005346 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005347 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005348}
5349
Yan Zhenga512bbf2008-12-08 16:46:26 -05005350int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5351 u64 chunk_start, u64 physical, u64 devid,
5352 u64 **logical, int *naddrs, int *stripe_len)
5353{
5354 struct extent_map_tree *em_tree = &map_tree->map_tree;
5355 struct extent_map *em;
5356 struct map_lookup *map;
5357 u64 *buf;
5358 u64 bytenr;
5359 u64 length;
5360 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005361 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005362 int i, j, nr = 0;
5363
Chris Mason890871b2009-09-02 16:24:52 -04005364 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005365 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005366 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005367
Josef Bacik835d9742013-03-19 12:13:25 -04005368 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005369 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005370 chunk_start);
5371 return -EIO;
5372 }
5373
5374 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005375 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005376 em->start, chunk_start);
5377 free_extent_map(em);
5378 return -EIO;
5379 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005380 map = (struct map_lookup *)em->bdev;
5381
5382 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005383 rmap_len = map->stripe_len;
5384
Yan Zhenga512bbf2008-12-08 16:46:26 -05005385 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5386 do_div(length, map->num_stripes / map->sub_stripes);
5387 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5388 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005389 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5390 BTRFS_BLOCK_GROUP_RAID6)) {
5391 do_div(length, nr_data_stripes(map));
5392 rmap_len = map->stripe_len * nr_data_stripes(map);
5393 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005394
5395 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005396 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005397
5398 for (i = 0; i < map->num_stripes; i++) {
5399 if (devid && map->stripes[i].dev->devid != devid)
5400 continue;
5401 if (map->stripes[i].physical > physical ||
5402 map->stripes[i].physical + length <= physical)
5403 continue;
5404
5405 stripe_nr = physical - map->stripes[i].physical;
5406 do_div(stripe_nr, map->stripe_len);
5407
5408 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5409 stripe_nr = stripe_nr * map->num_stripes + i;
5410 do_div(stripe_nr, map->sub_stripes);
5411 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5412 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005413 } /* else if RAID[56], multiply by nr_data_stripes().
5414 * Alternatively, just use rmap_len below instead of
5415 * map->stripe_len */
5416
5417 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005418 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005419 for (j = 0; j < nr; j++) {
5420 if (buf[j] == bytenr)
5421 break;
5422 }
Chris Mason934d3752008-12-08 16:43:10 -05005423 if (j == nr) {
5424 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005425 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005426 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005427 }
5428
Yan Zhenga512bbf2008-12-08 16:46:26 -05005429 *logical = buf;
5430 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005431 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005432
5433 free_extent_map(em);
5434 return 0;
5435}
5436
Miao Xie8408c712014-06-19 10:42:55 +08005437static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5438{
5439 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5440 bio_endio_nodec(bio, err);
5441 else
5442 bio_endio(bio, err);
5443 kfree(bbio);
5444}
5445
Jan Schmidta1d3c472011-08-04 17:15:33 +02005446static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005447{
Chris Mason9be33952013-05-17 18:30:14 -04005448 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005449 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005450 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005451
Stefan Behrens442a4f62012-05-25 16:06:08 +02005452 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005453 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005454 if (err == -EIO || err == -EREMOTEIO) {
5455 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005456 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005457
5458 BUG_ON(stripe_index >= bbio->num_stripes);
5459 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005460 if (dev->bdev) {
5461 if (bio->bi_rw & WRITE)
5462 btrfs_dev_stat_inc(dev,
5463 BTRFS_DEV_STAT_WRITE_ERRS);
5464 else
5465 btrfs_dev_stat_inc(dev,
5466 BTRFS_DEV_STAT_READ_ERRS);
5467 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5468 btrfs_dev_stat_inc(dev,
5469 BTRFS_DEV_STAT_FLUSH_ERRS);
5470 btrfs_dev_stat_print_on_error(dev);
5471 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005472 }
5473 }
Chris Mason8790d502008-04-03 16:29:03 -04005474
Jan Schmidta1d3c472011-08-04 17:15:33 +02005475 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005476 is_orig_bio = 1;
5477
Miao Xiec404e0d2014-01-30 16:46:55 +08005478 btrfs_bio_counter_dec(bbio->fs_info);
5479
Jan Schmidta1d3c472011-08-04 17:15:33 +02005480 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005481 if (!is_orig_bio) {
5482 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005483 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005484 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005485
Jan Schmidta1d3c472011-08-04 17:15:33 +02005486 bio->bi_private = bbio->private;
5487 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005488 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005489 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005490 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005491 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005492 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005493 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005494 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005495 /*
5496 * this bio is actually up to date, we didn't
5497 * go over the max number of errors
5498 */
5499 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005500 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005501 }
Miao Xiec55f1392014-06-19 10:42:54 +08005502
Miao Xie8408c712014-06-19 10:42:55 +08005503 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005504 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005505 bio_put(bio);
5506 }
Chris Mason8790d502008-04-03 16:29:03 -04005507}
5508
Chris Mason8b712842008-06-11 16:50:36 -04005509/*
5510 * see run_scheduled_bios for a description of why bios are collected for
5511 * async submit.
5512 *
5513 * This will add one bio to the pending list for a device and make sure
5514 * the work struct is scheduled.
5515 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005516static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5517 struct btrfs_device *device,
5518 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005519{
5520 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005521 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005522
David Woodhouse53b381b2013-01-29 18:40:14 -05005523 if (device->missing || !device->bdev) {
5524 bio_endio(bio, -EIO);
5525 return;
5526 }
5527
Chris Mason8b712842008-06-11 16:50:36 -04005528 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005529 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005530 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005531 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005532 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005533 return;
Chris Mason8b712842008-06-11 16:50:36 -04005534 }
5535
5536 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005537 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005538 * higher layers. Otherwise, the async bio makes it appear we have
5539 * made progress against dirty pages when we've really just put it
5540 * on a queue for later
5541 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005542 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005543 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005544 bio->bi_next = NULL;
5545 bio->bi_rw |= rw;
5546
5547 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005548 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005549 pending_bios = &device->pending_sync_bios;
5550 else
5551 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005552
Chris Masonffbd5172009-04-20 15:50:09 -04005553 if (pending_bios->tail)
5554 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005555
Chris Masonffbd5172009-04-20 15:50:09 -04005556 pending_bios->tail = bio;
5557 if (!pending_bios->head)
5558 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005559 if (device->running_pending)
5560 should_queue = 0;
5561
5562 spin_unlock(&device->io_lock);
5563
5564 if (should_queue)
Qu Wenruoa8c93d42014-02-28 10:46:08 +08005565 btrfs_queue_work(root->fs_info->submit_workers,
5566 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005567}
5568
Josef Bacikde1ee922012-10-19 16:50:56 -04005569static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5570 sector_t sector)
5571{
5572 struct bio_vec *prev;
5573 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005574 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005575 struct bvec_merge_data bvm = {
5576 .bi_bdev = bdev,
5577 .bi_sector = sector,
5578 .bi_rw = bio->bi_rw,
5579 };
5580
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305581 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005582 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005583
5584 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005585 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005586 return 0;
5587
5588 if (!q->merge_bvec_fn)
5589 return 1;
5590
Kent Overstreet4f024f32013-10-11 15:44:27 -07005591 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005592 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5593 return 0;
5594 return 1;
5595}
5596
5597static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5598 struct bio *bio, u64 physical, int dev_nr,
5599 int rw, int async)
5600{
5601 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5602
5603 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005604 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005605 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005606 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005607#ifdef DEBUG
5608 {
5609 struct rcu_string *name;
5610
5611 rcu_read_lock();
5612 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005613 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005614 "(%s id %llu), size=%u\n", rw,
5615 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5616 name->str, dev->devid, bio->bi_size);
5617 rcu_read_unlock();
5618 }
5619#endif
5620 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005621
5622 btrfs_bio_counter_inc_noblocked(root->fs_info);
5623
Josef Bacikde1ee922012-10-19 16:50:56 -04005624 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005625 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005626 else
5627 btrfsic_submit_bio(rw, bio);
5628}
5629
5630static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5631 struct bio *first_bio, struct btrfs_device *dev,
5632 int dev_nr, int rw, int async)
5633{
5634 struct bio_vec *bvec = first_bio->bi_io_vec;
5635 struct bio *bio;
5636 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5637 u64 physical = bbio->stripes[dev_nr].physical;
5638
5639again:
5640 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5641 if (!bio)
5642 return -ENOMEM;
5643
5644 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5645 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5646 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005647 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005648
5649 atomic_inc(&bbio->stripes_pending);
5650 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5651 rw, async);
5652 physical += len;
5653 goto again;
5654 }
5655 bvec++;
5656 }
5657
5658 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5659 return 0;
5660}
5661
5662static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5663{
5664 atomic_inc(&bbio->error);
5665 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005666 /* Shoud be the original bio. */
5667 WARN_ON(bio != bbio->orig_bio);
5668
Josef Bacikde1ee922012-10-19 16:50:56 -04005669 bio->bi_private = bbio->private;
5670 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005671 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005672 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005673
5674 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005675 }
5676}
5677
Chris Masonf1885912008-04-09 16:28:12 -04005678int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005679 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005680{
Chris Mason0b86a832008-03-24 15:01:56 -04005681 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005682 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005683 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005684 u64 length = 0;
5685 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005686 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005687 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005688 int dev_nr = 0;
5689 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005690 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005691
Kent Overstreet4f024f32013-10-11 15:44:27 -07005692 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005693 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005694
Miao Xiec404e0d2014-01-30 16:46:55 +08005695 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005696 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5697 mirror_num, &raid_map);
Miao Xiec404e0d2014-01-30 16:46:55 +08005698 if (ret) {
5699 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005700 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005701 }
Chris Masoncea9e442008-04-09 16:28:12 -04005702
Jan Schmidta1d3c472011-08-04 17:15:33 +02005703 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005704 bbio->orig_bio = first_bio;
5705 bbio->private = first_bio->bi_private;
5706 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005707 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005708 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5709
5710 if (raid_map) {
5711 /* In this case, map_length has been set to the length of
5712 a single stripe; not the whole write */
5713 if (rw & WRITE) {
Miao Xiec404e0d2014-01-30 16:46:55 +08005714 ret = raid56_parity_write(root, bio, bbio,
5715 raid_map, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005716 } else {
Miao Xiec404e0d2014-01-30 16:46:55 +08005717 ret = raid56_parity_recover(root, bio, bbio,
5718 raid_map, map_length,
5719 mirror_num);
David Woodhouse53b381b2013-01-29 18:40:14 -05005720 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005721 /*
5722 * FIXME, replace dosen't support raid56 yet, please fix
5723 * it in the future.
5724 */
5725 btrfs_bio_counter_dec(root->fs_info);
5726 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005727 }
5728
Chris Masoncea9e442008-04-09 16:28:12 -04005729 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005730 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005731 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005732 BUG();
5733 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005734
Chris Masond3977122009-01-05 21:25:51 -05005735 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005736 dev = bbio->stripes[dev_nr].dev;
5737 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5738 bbio_error(bbio, first_bio, logical);
5739 dev_nr++;
5740 continue;
5741 }
5742
5743 /*
5744 * Check and see if we're ok with this bio based on it's size
5745 * and offset with the given device.
5746 */
5747 if (!bio_size_ok(dev->bdev, first_bio,
5748 bbio->stripes[dev_nr].physical >> 9)) {
5749 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5750 dev_nr, rw, async_submit);
5751 BUG_ON(ret);
5752 dev_nr++;
5753 continue;
5754 }
5755
Jan Schmidta1d3c472011-08-04 17:15:33 +02005756 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005757 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005758 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005759 } else {
5760 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005761 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005762 }
Josef Bacik606686e2012-06-04 14:03:51 -04005763
Josef Bacikde1ee922012-10-19 16:50:56 -04005764 submit_stripe_bio(root, bbio, bio,
5765 bbio->stripes[dev_nr].physical, dev_nr, rw,
5766 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005767 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005768 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005769 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005770 return 0;
5771}
5772
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005773struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005774 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005775{
Yan Zheng2b820322008-11-17 21:11:30 -05005776 struct btrfs_device *device;
5777 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005778
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005779 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005780 while (cur_devices) {
5781 if (!fsid ||
5782 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5783 device = __find_device(&cur_devices->devices,
5784 devid, uuid);
5785 if (device)
5786 return device;
5787 }
5788 cur_devices = cur_devices->seed;
5789 }
5790 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005791}
5792
Chris Masondfe25022008-05-13 13:46:40 -04005793static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5794 u64 devid, u8 *dev_uuid)
5795{
5796 struct btrfs_device *device;
5797 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5798
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005799 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5800 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005801 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005802
5803 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005804 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005805 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005806
5807 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005808 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005809
Chris Masondfe25022008-05-13 13:46:40 -04005810 return device;
5811}
5812
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005813/**
5814 * btrfs_alloc_device - allocate struct btrfs_device
5815 * @fs_info: used only for generating a new devid, can be NULL if
5816 * devid is provided (i.e. @devid != NULL).
5817 * @devid: a pointer to devid for this device. If NULL a new devid
5818 * is generated.
5819 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5820 * is generated.
5821 *
5822 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5823 * on error. Returned struct is not linked onto any lists and can be
5824 * destroyed with kfree() right away.
5825 */
5826struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5827 const u64 *devid,
5828 const u8 *uuid)
5829{
5830 struct btrfs_device *dev;
5831 u64 tmp;
5832
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305833 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005834 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005835
5836 dev = __alloc_device();
5837 if (IS_ERR(dev))
5838 return dev;
5839
5840 if (devid)
5841 tmp = *devid;
5842 else {
5843 int ret;
5844
5845 ret = find_next_devid(fs_info, &tmp);
5846 if (ret) {
5847 kfree(dev);
5848 return ERR_PTR(ret);
5849 }
5850 }
5851 dev->devid = tmp;
5852
5853 if (uuid)
5854 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5855 else
5856 generate_random_uuid(dev->uuid);
5857
Liu Bo9e0af232014-08-15 23:36:53 +08005858 btrfs_init_work(&dev->work, btrfs_submit_helper,
5859 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005860
5861 return dev;
5862}
5863
Chris Mason0b86a832008-03-24 15:01:56 -04005864static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5865 struct extent_buffer *leaf,
5866 struct btrfs_chunk *chunk)
5867{
5868 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5869 struct map_lookup *map;
5870 struct extent_map *em;
5871 u64 logical;
5872 u64 length;
5873 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005874 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005875 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005876 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005877 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005878
Chris Masone17cade2008-04-15 15:41:47 -04005879 logical = key->offset;
5880 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005881
Chris Mason890871b2009-09-02 16:24:52 -04005882 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005883 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005884 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005885
5886 /* already mapped? */
5887 if (em && em->start <= logical && em->start + em->len > logical) {
5888 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005889 return 0;
5890 } else if (em) {
5891 free_extent_map(em);
5892 }
Chris Mason0b86a832008-03-24 15:01:56 -04005893
David Sterba172ddd62011-04-21 00:48:27 +02005894 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005895 if (!em)
5896 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005897 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5898 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005899 if (!map) {
5900 free_extent_map(em);
5901 return -ENOMEM;
5902 }
5903
Wang Shilong298a8f92014-06-19 10:42:52 +08005904 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04005905 em->bdev = (struct block_device *)map;
5906 em->start = logical;
5907 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005908 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005909 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005910 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005911
Chris Mason593060d2008-03-25 16:50:33 -04005912 map->num_stripes = num_stripes;
5913 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5914 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5915 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5916 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5917 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005918 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005919 for (i = 0; i < num_stripes; i++) {
5920 map->stripes[i].physical =
5921 btrfs_stripe_offset_nr(leaf, chunk, i);
5922 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005923 read_extent_buffer(leaf, uuid, (unsigned long)
5924 btrfs_stripe_dev_uuid_nr(chunk, i),
5925 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005926 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5927 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005928 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005929 free_extent_map(em);
5930 return -EIO;
5931 }
Chris Masondfe25022008-05-13 13:46:40 -04005932 if (!map->stripes[i].dev) {
5933 map->stripes[i].dev =
5934 add_missing_dev(root, devid, uuid);
5935 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04005936 free_extent_map(em);
5937 return -EIO;
5938 }
5939 }
5940 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005941 }
5942
Chris Mason890871b2009-09-02 16:24:52 -04005943 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005944 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005945 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005946 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005947 free_extent_map(em);
5948
5949 return 0;
5950}
5951
Jeff Mahoney143bede2012-03-01 14:56:26 +01005952static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005953 struct btrfs_dev_item *dev_item,
5954 struct btrfs_device *device)
5955{
5956 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005957
5958 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005959 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5960 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005961 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5962 device->type = btrfs_device_type(leaf, dev_item);
5963 device->io_align = btrfs_device_io_align(leaf, dev_item);
5964 device->io_width = btrfs_device_io_width(leaf, dev_item);
5965 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005966 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005967 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005968
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02005969 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005970 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005971}
5972
Yan Zheng2b820322008-11-17 21:11:30 -05005973static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5974{
5975 struct btrfs_fs_devices *fs_devices;
5976 int ret;
5977
Li Zefanb367e472011-12-07 11:38:24 +08005978 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005979
5980 fs_devices = root->fs_info->fs_devices->seed;
5981 while (fs_devices) {
5982 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5983 ret = 0;
5984 goto out;
5985 }
5986 fs_devices = fs_devices->seed;
5987 }
5988
5989 fs_devices = find_fsid(fsid);
5990 if (!fs_devices) {
5991 ret = -ENOENT;
5992 goto out;
5993 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005994
5995 fs_devices = clone_fs_devices(fs_devices);
5996 if (IS_ERR(fs_devices)) {
5997 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005998 goto out;
5999 }
6000
Christoph Hellwig97288f22008-12-02 06:36:09 -05006001 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006002 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006003 if (ret) {
6004 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006005 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006006 }
Yan Zheng2b820322008-11-17 21:11:30 -05006007
6008 if (!fs_devices->seeding) {
6009 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006010 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006011 ret = -EINVAL;
6012 goto out;
6013 }
6014
6015 fs_devices->seed = root->fs_info->fs_devices->seed;
6016 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006017out:
Yan Zheng2b820322008-11-17 21:11:30 -05006018 return ret;
6019}
6020
Chris Mason0d81ba52008-03-24 15:02:07 -04006021static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006022 struct extent_buffer *leaf,
6023 struct btrfs_dev_item *dev_item)
6024{
6025 struct btrfs_device *device;
6026 u64 devid;
6027 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006028 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006029 u8 dev_uuid[BTRFS_UUID_SIZE];
6030
Chris Mason0b86a832008-03-24 15:01:56 -04006031 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006032 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006033 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006034 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006035 BTRFS_UUID_SIZE);
6036
6037 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6038 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05006039 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006040 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006041 }
6042
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006043 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006044 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006045 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006046 return -EIO;
6047
6048 if (!device) {
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006049 btrfs_warn(root->fs_info, "devid %llu missing", devid);
Yan Zheng2b820322008-11-17 21:11:30 -05006050 device = add_missing_dev(root, devid, dev_uuid);
6051 if (!device)
6052 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05006053 } else if (!device->missing) {
6054 /*
6055 * this happens when a device that was properly setup
6056 * in the device info lists suddenly goes bad.
6057 * device->bdev is NULL, and so we have to set
6058 * device->missing to one here
6059 */
6060 root->fs_info->fs_devices->missing_devices++;
6061 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006062 }
6063 }
6064
6065 if (device->fs_devices != root->fs_info->fs_devices) {
6066 BUG_ON(device->writeable);
6067 if (device->generation !=
6068 btrfs_device_generation(leaf, dev_item))
6069 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006070 }
Chris Mason0b86a832008-03-24 15:01:56 -04006071
6072 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006073 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006074 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006075 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006076 spin_lock(&root->fs_info->free_chunk_lock);
6077 root->fs_info->free_chunk_space += device->total_bytes -
6078 device->bytes_used;
6079 spin_unlock(&root->fs_info->free_chunk_lock);
6080 }
Chris Mason0b86a832008-03-24 15:01:56 -04006081 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006082 return ret;
6083}
6084
Yan Zhenge4404d62008-12-12 10:03:26 -05006085int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006086{
David Sterba6c417612011-04-13 15:41:04 +02006087 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006088 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006089 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006090 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04006091 u8 *ptr;
6092 unsigned long sb_ptr;
6093 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006094 u32 num_stripes;
6095 u32 array_size;
6096 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006097 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04006098 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006099
Yan Zhenge4404d62008-12-12 10:03:26 -05006100 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04006101 BTRFS_SUPER_INFO_SIZE);
6102 if (!sb)
6103 return -ENOMEM;
6104 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006105 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006106 /*
6107 * The sb extent buffer is artifical and just used to read the system array.
6108 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6109 * pages up-to-date when the page is larger: extent does not cover the
6110 * whole page and consequently check_page_uptodate does not find all
6111 * the page's extents up-to-date (the hole beyond sb),
6112 * write_extent_buffer then triggers a WARN_ON.
6113 *
6114 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6115 * but sb spans only this function. Add an explicit SetPageUptodate call
6116 * to silence the warning eg. on PowerPC 64.
6117 */
6118 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006119 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006120
Chris Masona061fc82008-05-07 11:43:44 -04006121 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006122 array_size = btrfs_super_sys_array_size(super_copy);
6123
Chris Mason0b86a832008-03-24 15:01:56 -04006124 ptr = super_copy->sys_chunk_array;
6125 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
6126 cur = 0;
6127
6128 while (cur < array_size) {
6129 disk_key = (struct btrfs_disk_key *)ptr;
6130 btrfs_disk_key_to_cpu(&key, disk_key);
6131
Chris Masona061fc82008-05-07 11:43:44 -04006132 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006133 sb_ptr += len;
6134 cur += len;
6135
Chris Mason0d81ba52008-03-24 15:02:07 -04006136 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04006137 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04006138 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006139 if (ret)
6140 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006141 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6142 len = btrfs_chunk_item_size(num_stripes);
6143 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006144 ret = -EIO;
6145 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006146 }
6147 ptr += len;
6148 sb_ptr += len;
6149 cur += len;
6150 }
Chris Masona061fc82008-05-07 11:43:44 -04006151 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006152 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006153}
6154
6155int btrfs_read_chunk_tree(struct btrfs_root *root)
6156{
6157 struct btrfs_path *path;
6158 struct extent_buffer *leaf;
6159 struct btrfs_key key;
6160 struct btrfs_key found_key;
6161 int ret;
6162 int slot;
6163
6164 root = root->fs_info->chunk_root;
6165
6166 path = btrfs_alloc_path();
6167 if (!path)
6168 return -ENOMEM;
6169
Li Zefanb367e472011-12-07 11:38:24 +08006170 mutex_lock(&uuid_mutex);
6171 lock_chunks(root);
6172
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006173 /*
6174 * Read all device items, and then all the chunk items. All
6175 * device items are found before any chunk item (their object id
6176 * is smaller than the lowest possible object id for a chunk
6177 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006178 */
6179 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6180 key.offset = 0;
6181 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006182 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006183 if (ret < 0)
6184 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006185 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006186 leaf = path->nodes[0];
6187 slot = path->slots[0];
6188 if (slot >= btrfs_header_nritems(leaf)) {
6189 ret = btrfs_next_leaf(root, path);
6190 if (ret == 0)
6191 continue;
6192 if (ret < 0)
6193 goto error;
6194 break;
6195 }
6196 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006197 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6198 struct btrfs_dev_item *dev_item;
6199 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006200 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006201 ret = read_one_dev(root, leaf, dev_item);
6202 if (ret)
6203 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006204 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6205 struct btrfs_chunk *chunk;
6206 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6207 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006208 if (ret)
6209 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006210 }
6211 path->slots[0]++;
6212 }
Chris Mason0b86a832008-03-24 15:01:56 -04006213 ret = 0;
6214error:
Li Zefanb367e472011-12-07 11:38:24 +08006215 unlock_chunks(root);
6216 mutex_unlock(&uuid_mutex);
6217
Yan Zheng2b820322008-11-17 21:11:30 -05006218 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006219 return ret;
6220}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006221
Miao Xiecb517ea2013-05-15 07:48:19 +00006222void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6223{
6224 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6225 struct btrfs_device *device;
6226
Liu Bo29cc83f2014-05-11 23:14:59 +08006227 while (fs_devices) {
6228 mutex_lock(&fs_devices->device_list_mutex);
6229 list_for_each_entry(device, &fs_devices->devices, dev_list)
6230 device->dev_root = fs_info->dev_root;
6231 mutex_unlock(&fs_devices->device_list_mutex);
6232
6233 fs_devices = fs_devices->seed;
6234 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006235}
6236
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006237static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6238{
6239 int i;
6240
6241 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6242 btrfs_dev_stat_reset(dev, i);
6243}
6244
6245int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6246{
6247 struct btrfs_key key;
6248 struct btrfs_key found_key;
6249 struct btrfs_root *dev_root = fs_info->dev_root;
6250 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6251 struct extent_buffer *eb;
6252 int slot;
6253 int ret = 0;
6254 struct btrfs_device *device;
6255 struct btrfs_path *path = NULL;
6256 int i;
6257
6258 path = btrfs_alloc_path();
6259 if (!path) {
6260 ret = -ENOMEM;
6261 goto out;
6262 }
6263
6264 mutex_lock(&fs_devices->device_list_mutex);
6265 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6266 int item_size;
6267 struct btrfs_dev_stats_item *ptr;
6268
6269 key.objectid = 0;
6270 key.type = BTRFS_DEV_STATS_KEY;
6271 key.offset = device->devid;
6272 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6273 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006274 __btrfs_reset_dev_stats(device);
6275 device->dev_stats_valid = 1;
6276 btrfs_release_path(path);
6277 continue;
6278 }
6279 slot = path->slots[0];
6280 eb = path->nodes[0];
6281 btrfs_item_key_to_cpu(eb, &found_key, slot);
6282 item_size = btrfs_item_size_nr(eb, slot);
6283
6284 ptr = btrfs_item_ptr(eb, slot,
6285 struct btrfs_dev_stats_item);
6286
6287 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6288 if (item_size >= (1 + i) * sizeof(__le64))
6289 btrfs_dev_stat_set(device, i,
6290 btrfs_dev_stats_value(eb, ptr, i));
6291 else
6292 btrfs_dev_stat_reset(device, i);
6293 }
6294
6295 device->dev_stats_valid = 1;
6296 btrfs_dev_stat_print_on_load(device);
6297 btrfs_release_path(path);
6298 }
6299 mutex_unlock(&fs_devices->device_list_mutex);
6300
6301out:
6302 btrfs_free_path(path);
6303 return ret < 0 ? ret : 0;
6304}
6305
6306static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6307 struct btrfs_root *dev_root,
6308 struct btrfs_device *device)
6309{
6310 struct btrfs_path *path;
6311 struct btrfs_key key;
6312 struct extent_buffer *eb;
6313 struct btrfs_dev_stats_item *ptr;
6314 int ret;
6315 int i;
6316
6317 key.objectid = 0;
6318 key.type = BTRFS_DEV_STATS_KEY;
6319 key.offset = device->devid;
6320
6321 path = btrfs_alloc_path();
6322 BUG_ON(!path);
6323 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6324 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006325 printk_in_rcu(KERN_WARNING "BTRFS: "
6326 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006327 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006328 goto out;
6329 }
6330
6331 if (ret == 0 &&
6332 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6333 /* need to delete old one and insert a new one */
6334 ret = btrfs_del_item(trans, dev_root, path);
6335 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006336 printk_in_rcu(KERN_WARNING "BTRFS: "
6337 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006338 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006339 goto out;
6340 }
6341 ret = 1;
6342 }
6343
6344 if (ret == 1) {
6345 /* need to insert a new item */
6346 btrfs_release_path(path);
6347 ret = btrfs_insert_empty_item(trans, dev_root, path,
6348 &key, sizeof(*ptr));
6349 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006350 printk_in_rcu(KERN_WARNING "BTRFS: "
6351 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006352 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006353 goto out;
6354 }
6355 }
6356
6357 eb = path->nodes[0];
6358 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6359 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6360 btrfs_set_dev_stats_value(eb, ptr, i,
6361 btrfs_dev_stat_read(device, i));
6362 btrfs_mark_buffer_dirty(eb);
6363
6364out:
6365 btrfs_free_path(path);
6366 return ret;
6367}
6368
6369/*
6370 * called from commit_transaction. Writes all changed device stats to disk.
6371 */
6372int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6373 struct btrfs_fs_info *fs_info)
6374{
6375 struct btrfs_root *dev_root = fs_info->dev_root;
6376 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6377 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006378 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006379 int ret = 0;
6380
6381 mutex_lock(&fs_devices->device_list_mutex);
6382 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006383 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006384 continue;
6385
Miao Xieaddc3fa2014-07-24 11:37:11 +08006386 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006387 ret = update_dev_stat_item(trans, dev_root, device);
6388 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006389 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006390 }
6391 mutex_unlock(&fs_devices->device_list_mutex);
6392
6393 return ret;
6394}
6395
Stefan Behrens442a4f62012-05-25 16:06:08 +02006396void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6397{
6398 btrfs_dev_stat_inc(dev, index);
6399 btrfs_dev_stat_print_on_error(dev);
6400}
6401
Eric Sandeen48a3b632013-04-25 20:41:01 +00006402static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006403{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006404 if (!dev->dev_stats_valid)
6405 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006406 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6407 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006408 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006409 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6410 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6411 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006412 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6413 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006414}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006415
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006416static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6417{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006418 int i;
6419
6420 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6421 if (btrfs_dev_stat_read(dev, i) != 0)
6422 break;
6423 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6424 return; /* all values == 0, suppress message */
6425
Frank Holtonefe120a2013-12-20 11:37:06 -05006426 printk_in_rcu(KERN_INFO "BTRFS: "
6427 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006428 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006429 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6430 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6431 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6432 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6433 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6434}
6435
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006436int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006437 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006438{
6439 struct btrfs_device *dev;
6440 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6441 int i;
6442
6443 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006444 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006445 mutex_unlock(&fs_devices->device_list_mutex);
6446
6447 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006448 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006449 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006450 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006451 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006452 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006453 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006454 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6455 if (stats->nr_items > i)
6456 stats->values[i] =
6457 btrfs_dev_stat_read_and_reset(dev, i);
6458 else
6459 btrfs_dev_stat_reset(dev, i);
6460 }
6461 } else {
6462 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6463 if (stats->nr_items > i)
6464 stats->values[i] = btrfs_dev_stat_read(dev, i);
6465 }
6466 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6467 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6468 return 0;
6469}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006470
6471int btrfs_scratch_superblock(struct btrfs_device *device)
6472{
6473 struct buffer_head *bh;
6474 struct btrfs_super_block *disk_super;
6475
6476 bh = btrfs_read_dev_super(device->bdev);
6477 if (!bh)
6478 return -EINVAL;
6479 disk_super = (struct btrfs_super_block *)bh->b_data;
6480
6481 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6482 set_buffer_dirty(bh);
6483 sync_dirty_buffer(bh);
6484 brelse(bh);
6485
6486 return 0;
6487}