blob: e04424490f123ef589e7312329bacd8cd911399d [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 Wenruoa8c93d4e2014-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);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400478 fs_devices->latest_devid = devid;
479 fs_devices->latest_trans = found_transid;
Ilya Dryomov2208a372013-08-12 14:33:03 +0300480
Chris Mason8a4b83c2008-03-24 15:02:07 -0400481 device = NULL;
482 } else {
Chris Masona4437552008-04-18 10:29:38 -0400483 device = __find_device(&fs_devices->devices, devid,
484 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400485 }
486 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500487 if (fs_devices->opened)
488 return -EBUSY;
489
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300490 device = btrfs_alloc_device(NULL, &devid,
491 disk_super->dev_item.uuid);
492 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400493 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300494 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400495 }
Josef Bacik606686e2012-06-04 14:03:51 -0400496
497 name = rcu_string_strdup(path, GFP_NOFS);
498 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400499 kfree(device);
500 return -ENOMEM;
501 }
Josef Bacik606686e2012-06-04 14:03:51 -0400502 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200503
Chris Masone5e9a522009-06-10 15:17:02 -0400504 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000505 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100506 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400507 mutex_unlock(&fs_devices->device_list_mutex);
508
David Sterba60999ca2014-03-26 18:26:36 +0100509 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500510 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400511 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800512 /*
513 * When FS is already mounted.
514 * 1. If you are here and if the device->name is NULL that
515 * means this device was missing at time of FS mount.
516 * 2. If you are here and if the device->name is different
517 * from 'path' that means either
518 * a. The same device disappeared and reappeared with
519 * different name. or
520 * b. The missing-disk-which-was-replaced, has
521 * reappeared now.
522 *
523 * We must allow 1 and 2a above. But 2b would be a spurious
524 * and unintentional.
525 *
526 * Further in case of 1 and 2a above, the disk at 'path'
527 * would have missed some transaction when it was away and
528 * in case of 2a the stale bdev has to be updated as well.
529 * 2b must not be allowed at all time.
530 */
531
532 /*
533 * As of now don't allow update to btrfs_fs_device through
534 * the btrfs dev scan cli, after FS has been mounted.
535 */
Anand Jain77bdae42014-07-03 18:22:06 +0800536 if (fs_devices->opened) {
Anand Jainb96de002014-07-03 18:22:05 +0800537 return -EBUSY;
Anand Jain77bdae42014-07-03 18:22:06 +0800538 } else {
539 /*
540 * That is if the FS is _not_ mounted and if you
541 * are here, that means there is more than one
542 * disk with same uuid and devid.We keep the one
543 * with larger generation number or the last-in if
544 * generation are equal.
545 */
546 if (found_transid < device->generation)
547 return -EEXIST;
548 }
Anand Jainb96de002014-07-03 18:22:05 +0800549
Josef Bacik606686e2012-06-04 14:03:51 -0400550 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000551 if (!name)
552 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400553 rcu_string_free(device->name);
554 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500555 if (device->missing) {
556 fs_devices->missing_devices--;
557 device->missing = 0;
558 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400559 }
560
Anand Jain77bdae42014-07-03 18:22:06 +0800561 /*
562 * Unmount does not free the btrfs_device struct but would zero
563 * generation along with most of the other members. So just update
564 * it back. We need it to pick the disk with largest generation
565 * (as above).
566 */
567 if (!fs_devices->opened)
568 device->generation = found_transid;
569
Chris Mason8a4b83c2008-03-24 15:02:07 -0400570 if (found_transid > fs_devices->latest_trans) {
571 fs_devices->latest_devid = devid;
572 fs_devices->latest_trans = found_transid;
573 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400574 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100575
576 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400577}
578
Yan Zhenge4404d62008-12-12 10:03:26 -0500579static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
580{
581 struct btrfs_fs_devices *fs_devices;
582 struct btrfs_device *device;
583 struct btrfs_device *orig_dev;
584
Ilya Dryomov2208a372013-08-12 14:33:03 +0300585 fs_devices = alloc_fs_devices(orig->fsid);
586 if (IS_ERR(fs_devices))
587 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500588
Yan Zhenge4404d62008-12-12 10:03:26 -0500589 fs_devices->latest_devid = orig->latest_devid;
590 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400591 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500592
Xiao Guangrong46224702011-04-20 10:08:47 +0000593 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500594 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400595 struct rcu_string *name;
596
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300597 device = btrfs_alloc_device(NULL, &orig_dev->devid,
598 orig_dev->uuid);
599 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500600 goto error;
601
Josef Bacik606686e2012-06-04 14:03:51 -0400602 /*
603 * This is ok to do without rcu read locked because we hold the
604 * uuid mutex so nothing we touch in here is going to disappear.
605 */
Anand Jaine755f782014-06-30 17:12:47 +0800606 if (orig_dev->name) {
607 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
608 if (!name) {
609 kfree(device);
610 goto error;
611 }
612 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400613 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500614
Yan Zhenge4404d62008-12-12 10:03:26 -0500615 list_add(&device->dev_list, &fs_devices->devices);
616 device->fs_devices = fs_devices;
617 fs_devices->num_devices++;
618 }
619 return fs_devices;
620error:
621 free_fs_devices(fs_devices);
622 return ERR_PTR(-ENOMEM);
623}
624
Stefan Behrens8dabb742012-11-06 13:15:27 +0100625void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
626 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400627{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500628 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400629
Chris Masona6b0d5c2012-02-20 20:53:43 -0500630 struct block_device *latest_bdev = NULL;
631 u64 latest_devid = 0;
632 u64 latest_transid = 0;
633
Chris Masondfe25022008-05-13 13:46:40 -0400634 mutex_lock(&uuid_mutex);
635again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000636 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500637 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500638 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100639 if (!device->is_tgtdev_for_dev_replace &&
640 (!latest_transid ||
641 device->generation > latest_transid)) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500642 latest_devid = device->devid;
643 latest_transid = device->generation;
644 latest_bdev = device->bdev;
645 }
Yan Zheng2b820322008-11-17 21:11:30 -0500646 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500647 }
Yan Zheng2b820322008-11-17 21:11:30 -0500648
Stefan Behrens8dabb742012-11-06 13:15:27 +0100649 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
650 /*
651 * In the first step, keep the device which has
652 * the correct fsid and the devid that is used
653 * for the dev_replace procedure.
654 * In the second step, the dev_replace state is
655 * read from the device tree and it is known
656 * whether the procedure is really active or
657 * not, which means whether this device is
658 * used or whether it should be removed.
659 */
660 if (step == 0 || device->is_tgtdev_for_dev_replace) {
661 continue;
662 }
663 }
Yan Zheng2b820322008-11-17 21:11:30 -0500664 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100665 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500666 device->bdev = NULL;
667 fs_devices->open_devices--;
668 }
669 if (device->writeable) {
670 list_del_init(&device->dev_alloc_list);
671 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100672 if (!device->is_tgtdev_for_dev_replace)
673 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500674 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500675 list_del_init(&device->dev_list);
676 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400677 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500678 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400679 }
Yan Zheng2b820322008-11-17 21:11:30 -0500680
681 if (fs_devices->seed) {
682 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500683 goto again;
684 }
685
Chris Masona6b0d5c2012-02-20 20:53:43 -0500686 fs_devices->latest_bdev = latest_bdev;
687 fs_devices->latest_devid = latest_devid;
688 fs_devices->latest_trans = latest_transid;
689
Chris Masondfe25022008-05-13 13:46:40 -0400690 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400691}
Chris Masona0af4692008-05-13 16:03:06 -0400692
Xiao Guangrong1f781602011-04-20 10:09:16 +0000693static void __free_device(struct work_struct *work)
694{
695 struct btrfs_device *device;
696
697 device = container_of(work, struct btrfs_device, rcu_work);
698
699 if (device->bdev)
700 blkdev_put(device->bdev, device->mode);
701
Josef Bacik606686e2012-06-04 14:03:51 -0400702 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000703 kfree(device);
704}
705
706static void free_device(struct rcu_head *head)
707{
708 struct btrfs_device *device;
709
710 device = container_of(head, struct btrfs_device, rcu);
711
712 INIT_WORK(&device->rcu_work, __free_device);
713 schedule_work(&device->rcu_work);
714}
715
Yan Zheng2b820322008-11-17 21:11:30 -0500716static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400717{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400718 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500719
Yan Zheng2b820322008-11-17 21:11:30 -0500720 if (--fs_devices->opened > 0)
721 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400722
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000723 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500724 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000725 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400726 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000727
728 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400729 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000730
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300731 if (device->writeable &&
732 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500733 list_del_init(&device->dev_alloc_list);
734 fs_devices->rw_devices--;
735 }
736
Josef Bacikd5e20032011-08-04 14:52:27 +0000737 if (device->can_discard)
738 fs_devices->num_can_discard--;
Josef Bacik726551e2013-08-26 13:45:53 -0400739 if (device->missing)
740 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000741
Ilya Dryomova1e87802013-08-12 14:33:04 +0300742 new_device = btrfs_alloc_device(NULL, &device->devid,
743 device->uuid);
744 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400745
746 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400747 if (device->name) {
748 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300749 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400750 rcu_assign_pointer(new_device->name, name);
751 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300752
Xiao Guangrong1f781602011-04-20 10:09:16 +0000753 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300754 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000755
756 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400757 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000758 mutex_unlock(&fs_devices->device_list_mutex);
759
Yan Zhenge4404d62008-12-12 10:03:26 -0500760 WARN_ON(fs_devices->open_devices);
761 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500762 fs_devices->opened = 0;
763 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500764
Chris Mason8a4b83c2008-03-24 15:02:07 -0400765 return 0;
766}
767
Yan Zheng2b820322008-11-17 21:11:30 -0500768int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
769{
Yan Zhenge4404d62008-12-12 10:03:26 -0500770 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500771 int ret;
772
773 mutex_lock(&uuid_mutex);
774 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500775 if (!fs_devices->opened) {
776 seed_devices = fs_devices->seed;
777 fs_devices->seed = NULL;
778 }
Yan Zheng2b820322008-11-17 21:11:30 -0500779 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500780
781 while (seed_devices) {
782 fs_devices = seed_devices;
783 seed_devices = fs_devices->seed;
784 __btrfs_close_devices(fs_devices);
785 free_fs_devices(fs_devices);
786 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000787 /*
788 * Wait for rcu kworkers under __btrfs_close_devices
789 * to finish all blkdev_puts so device is really
790 * free when umount is done.
791 */
792 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500793 return ret;
794}
795
Yan Zhenge4404d62008-12-12 10:03:26 -0500796static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
797 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400798{
Josef Bacikd5e20032011-08-04 14:52:27 +0000799 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400800 struct block_device *bdev;
801 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400802 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400803 struct block_device *latest_bdev = NULL;
804 struct buffer_head *bh;
805 struct btrfs_super_block *disk_super;
806 u64 latest_devid = 0;
807 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400808 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500809 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400810 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400811
Tejun Heod4d77622010-11-13 11:55:18 +0100812 flags |= FMODE_EXCL;
813
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500814 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400815 if (device->bdev)
816 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400817 if (!device->name)
818 continue;
819
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000820 /* Just open everything we can; ignore failures here */
821 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
822 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100823 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400824
825 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000826 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400827 if (devid != device->devid)
828 goto error_brelse;
829
Yan Zheng2b820322008-11-17 21:11:30 -0500830 if (memcmp(device->uuid, disk_super->dev_item.uuid,
831 BTRFS_UUID_SIZE))
832 goto error_brelse;
833
834 device->generation = btrfs_super_generation(disk_super);
835 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400836 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500837 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400838 latest_bdev = bdev;
839 }
840
Yan Zheng2b820322008-11-17 21:11:30 -0500841 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
842 device->writeable = 0;
843 } else {
844 device->writeable = !bdev_read_only(bdev);
845 seeding = 0;
846 }
847
Josef Bacikd5e20032011-08-04 14:52:27 +0000848 q = bdev_get_queue(bdev);
849 if (blk_queue_discard(q)) {
850 device->can_discard = 1;
851 fs_devices->num_can_discard++;
852 }
853
Chris Mason8a4b83c2008-03-24 15:02:07 -0400854 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400855 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500856 device->mode = flags;
857
Chris Masonc2898112009-06-10 09:51:32 -0400858 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
859 fs_devices->rotating = 1;
860
Chris Masona0af4692008-05-13 16:03:06 -0400861 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300862 if (device->writeable &&
863 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500864 fs_devices->rw_devices++;
865 list_add(&device->dev_alloc_list,
866 &fs_devices->alloc_list);
867 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000868 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400869 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400870
Chris Masona0af4692008-05-13 16:03:06 -0400871error_brelse:
872 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100873 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400874 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400875 }
Chris Masona0af4692008-05-13 16:03:06 -0400876 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300877 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400878 goto out;
879 }
Yan Zheng2b820322008-11-17 21:11:30 -0500880 fs_devices->seeding = seeding;
881 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400882 fs_devices->latest_bdev = latest_bdev;
883 fs_devices->latest_devid = latest_devid;
884 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500885 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400886out:
Yan Zheng2b820322008-11-17 21:11:30 -0500887 return ret;
888}
889
890int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500891 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500892{
893 int ret;
894
895 mutex_lock(&uuid_mutex);
896 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500897 fs_devices->opened++;
898 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500899 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500900 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500901 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400902 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400903 return ret;
904}
905
David Sterba6f60cbd2013-02-15 11:31:02 -0700906/*
907 * Look for a btrfs signature on a device. This may be called out of the mount path
908 * and we are not allowed to call set_blocksize during the scan. The superblock
909 * is read via pagecache
910 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500911int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400912 struct btrfs_fs_devices **fs_devices_ret)
913{
914 struct btrfs_super_block *disk_super;
915 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700916 struct page *page;
917 void *p;
918 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400919 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400920 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400921 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700922 u64 bytenr;
923 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400924
David Sterba6f60cbd2013-02-15 11:31:02 -0700925 /*
926 * we would like to check all the supers, but that would make
927 * a btrfs mount succeed after a mkfs from a different FS.
928 * So, we need to add a special mount option to scan for
929 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
930 */
931 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100932 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500933 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700934
935 bdev = blkdev_get_by_path(path, flags, holder);
936
937 if (IS_ERR(bdev)) {
938 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100939 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700940 }
941
942 /* make sure our super fits in the device */
943 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
944 goto error_bdev_put;
945
946 /* make sure our super fits in the page */
947 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
948 goto error_bdev_put;
949
950 /* make sure our super doesn't straddle pages on disk */
951 index = bytenr >> PAGE_CACHE_SHIFT;
952 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
953 goto error_bdev_put;
954
955 /* pull in the page with our super */
956 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
957 index, GFP_NOFS);
958
959 if (IS_ERR_OR_NULL(page))
960 goto error_bdev_put;
961
962 p = kmap(page);
963
964 /* align our pointer to the offset of the super block */
965 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
966
967 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800968 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700969 goto error_unmap;
970
Xiao Guangronga3438322010-01-06 11:48:18 +0000971 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400972 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400973 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700974
Chris Mason8a4b83c2008-03-24 15:02:07 -0400975 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100976 if (ret > 0) {
977 if (disk_super->label[0]) {
978 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
979 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
980 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
981 } else {
982 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
983 }
984
985 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
986 ret = 0;
987 }
Josef Bacik02db0842012-06-21 16:03:58 -0400988 if (!ret && fs_devices_ret)
989 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700990
991error_unmap:
992 kunmap(page);
993 page_cache_release(page);
994
995error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100996 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400997error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100998 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400999 return ret;
1000}
Chris Mason0b86a832008-03-24 15:01:56 -04001001
Miao Xie6d07bce2011-01-05 10:07:31 +00001002/* helper to account the used device space in the range */
1003int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1004 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001005{
1006 struct btrfs_key key;
1007 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001008 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001009 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001010 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001011 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001012 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001013 struct extent_buffer *l;
1014
Miao Xie6d07bce2011-01-05 10:07:31 +00001015 *length = 0;
1016
Stefan Behrens63a212a2012-11-05 18:29:28 +01001017 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +00001018 return 0;
1019
Yan Zheng2b820322008-11-17 21:11:30 -05001020 path = btrfs_alloc_path();
1021 if (!path)
1022 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001023 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001024
Chris Mason0b86a832008-03-24 15:01:56 -04001025 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001026 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001027 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001028
1029 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001030 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001031 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001032 if (ret > 0) {
1033 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1034 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001035 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001036 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001037
Chris Mason0b86a832008-03-24 15:01:56 -04001038 while (1) {
1039 l = path->nodes[0];
1040 slot = path->slots[0];
1041 if (slot >= btrfs_header_nritems(l)) {
1042 ret = btrfs_next_leaf(root, path);
1043 if (ret == 0)
1044 continue;
1045 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001046 goto out;
1047
1048 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001049 }
1050 btrfs_item_key_to_cpu(l, &key, slot);
1051
1052 if (key.objectid < device->devid)
1053 goto next;
1054
1055 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001056 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001057
David Sterba962a2982014-06-04 18:41:45 +02001058 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001059 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001060
Chris Mason0b86a832008-03-24 15:01:56 -04001061 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001062 extent_end = key.offset + btrfs_dev_extent_length(l,
1063 dev_extent);
1064 if (key.offset <= start && extent_end > end) {
1065 *length = end - start + 1;
1066 break;
1067 } else if (key.offset <= start && extent_end > start)
1068 *length += extent_end - start;
1069 else if (key.offset > start && extent_end <= end)
1070 *length += extent_end - key.offset;
1071 else if (key.offset > start && key.offset <= end) {
1072 *length += end - key.offset + 1;
1073 break;
1074 } else if (key.offset > end)
1075 break;
1076
1077next:
1078 path->slots[0]++;
1079 }
1080 ret = 0;
1081out:
1082 btrfs_free_path(path);
1083 return ret;
1084}
1085
Josef Bacik6df9a952013-06-27 13:22:46 -04001086static int contains_pending_extent(struct btrfs_trans_handle *trans,
1087 struct btrfs_device *device,
1088 u64 *start, u64 len)
1089{
1090 struct extent_map *em;
1091 int ret = 0;
1092
1093 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
1094 struct map_lookup *map;
1095 int i;
1096
1097 map = (struct map_lookup *)em->bdev;
1098 for (i = 0; i < map->num_stripes; i++) {
1099 if (map->stripes[i].dev != device)
1100 continue;
1101 if (map->stripes[i].physical >= *start + len ||
1102 map->stripes[i].physical + em->orig_block_len <=
1103 *start)
1104 continue;
1105 *start = map->stripes[i].physical +
1106 em->orig_block_len;
1107 ret = 1;
1108 }
1109 }
1110
1111 return ret;
1112}
1113
1114
Chris Mason0b86a832008-03-24 15:01:56 -04001115/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001116 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001117 * @device: the device which we search the free space in
1118 * @num_bytes: the size of the free space that we need
1119 * @start: store the start of the free space.
1120 * @len: the size of the free space. that we find, or the size of the max
1121 * free space if we don't find suitable free space
1122 *
Chris Mason0b86a832008-03-24 15:01:56 -04001123 * this uses a pretty simple search, the expectation is that it is
1124 * called very infrequently and that a given device has a small number
1125 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001126 *
1127 * @start is used to store the start of the free space if we find. But if we
1128 * don't find suitable free space, it will be used to store the start position
1129 * of the max free space.
1130 *
1131 * @len is used to store the size of the free space that we find.
1132 * But if we don't find suitable free space, it is used to store the size of
1133 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001134 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001135int find_free_dev_extent(struct btrfs_trans_handle *trans,
1136 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001137 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001138{
1139 struct btrfs_key key;
1140 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001141 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001142 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001143 u64 hole_size;
1144 u64 max_hole_start;
1145 u64 max_hole_size;
1146 u64 extent_end;
1147 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001148 u64 search_end = device->total_bytes;
1149 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001150 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001151 struct extent_buffer *l;
1152
Chris Mason0b86a832008-03-24 15:01:56 -04001153 /* FIXME use last free of some kind */
1154
1155 /* we don't want to overwrite the superblock on the drive,
1156 * so we make sure to start at an offset of at least 1MB
1157 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001158 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001159
Josef Bacik6df9a952013-06-27 13:22:46 -04001160 path = btrfs_alloc_path();
1161 if (!path)
1162 return -ENOMEM;
1163again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001164 max_hole_start = search_start;
1165 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001166 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001167
Stefan Behrens63a212a2012-11-05 18:29:28 +01001168 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001169 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001170 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001171 }
1172
Miao Xie7bfc8372011-01-05 10:07:26 +00001173 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001174 path->search_commit_root = 1;
1175 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001176
Chris Mason0b86a832008-03-24 15:01:56 -04001177 key.objectid = device->devid;
1178 key.offset = search_start;
1179 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001180
Li Zefan125ccb02011-12-08 15:07:24 +08001181 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001182 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001183 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001184 if (ret > 0) {
1185 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1186 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001187 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001188 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001189
Chris Mason0b86a832008-03-24 15:01:56 -04001190 while (1) {
1191 l = path->nodes[0];
1192 slot = path->slots[0];
1193 if (slot >= btrfs_header_nritems(l)) {
1194 ret = btrfs_next_leaf(root, path);
1195 if (ret == 0)
1196 continue;
1197 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001198 goto out;
1199
1200 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001201 }
1202 btrfs_item_key_to_cpu(l, &key, slot);
1203
1204 if (key.objectid < device->devid)
1205 goto next;
1206
1207 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001208 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001209
David Sterba962a2982014-06-04 18:41:45 +02001210 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001211 goto next;
1212
Miao Xie7bfc8372011-01-05 10:07:26 +00001213 if (key.offset > search_start) {
1214 hole_size = key.offset - search_start;
1215
Josef Bacik6df9a952013-06-27 13:22:46 -04001216 /*
1217 * Have to check before we set max_hole_start, otherwise
1218 * we could end up sending back this offset anyway.
1219 */
1220 if (contains_pending_extent(trans, device,
1221 &search_start,
1222 hole_size))
1223 hole_size = 0;
1224
Miao Xie7bfc8372011-01-05 10:07:26 +00001225 if (hole_size > max_hole_size) {
1226 max_hole_start = search_start;
1227 max_hole_size = hole_size;
1228 }
1229
1230 /*
1231 * If this free space is greater than which we need,
1232 * it must be the max free space that we have found
1233 * until now, so max_hole_start must point to the start
1234 * of this free space and the length of this free space
1235 * is stored in max_hole_size. Thus, we return
1236 * max_hole_start and max_hole_size and go back to the
1237 * caller.
1238 */
1239 if (hole_size >= num_bytes) {
1240 ret = 0;
1241 goto out;
1242 }
1243 }
1244
Chris Mason0b86a832008-03-24 15:01:56 -04001245 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001246 extent_end = key.offset + btrfs_dev_extent_length(l,
1247 dev_extent);
1248 if (extent_end > search_start)
1249 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001250next:
1251 path->slots[0]++;
1252 cond_resched();
1253 }
Chris Mason0b86a832008-03-24 15:01:56 -04001254
liubo38c01b92011-08-02 02:39:03 +00001255 /*
1256 * At this point, search_start should be the end of
1257 * allocated dev extents, and when shrinking the device,
1258 * search_end may be smaller than search_start.
1259 */
1260 if (search_end > search_start)
1261 hole_size = search_end - search_start;
1262
Miao Xie7bfc8372011-01-05 10:07:26 +00001263 if (hole_size > max_hole_size) {
1264 max_hole_start = search_start;
1265 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001266 }
Chris Mason0b86a832008-03-24 15:01:56 -04001267
Josef Bacik6df9a952013-06-27 13:22:46 -04001268 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1269 btrfs_release_path(path);
1270 goto again;
1271 }
1272
Miao Xie7bfc8372011-01-05 10:07:26 +00001273 /* See above. */
1274 if (hole_size < num_bytes)
1275 ret = -ENOSPC;
1276 else
1277 ret = 0;
1278
1279out:
Yan Zheng2b820322008-11-17 21:11:30 -05001280 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001281 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001282 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001283 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001284 return ret;
1285}
1286
Christoph Hellwigb2950862008-12-02 09:54:17 -05001287static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001288 struct btrfs_device *device,
1289 u64 start)
1290{
1291 int ret;
1292 struct btrfs_path *path;
1293 struct btrfs_root *root = device->dev_root;
1294 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001295 struct btrfs_key found_key;
1296 struct extent_buffer *leaf = NULL;
1297 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001298
1299 path = btrfs_alloc_path();
1300 if (!path)
1301 return -ENOMEM;
1302
1303 key.objectid = device->devid;
1304 key.offset = start;
1305 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001306again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001307 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001308 if (ret > 0) {
1309 ret = btrfs_previous_item(root, path, key.objectid,
1310 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001311 if (ret)
1312 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001313 leaf = path->nodes[0];
1314 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1315 extent = btrfs_item_ptr(leaf, path->slots[0],
1316 struct btrfs_dev_extent);
1317 BUG_ON(found_key.offset > start || found_key.offset +
1318 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001319 key = found_key;
1320 btrfs_release_path(path);
1321 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001322 } else if (ret == 0) {
1323 leaf = path->nodes[0];
1324 extent = btrfs_item_ptr(leaf, path->slots[0],
1325 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001326 } else {
1327 btrfs_error(root->fs_info, ret, "Slot search failed");
1328 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001329 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001330
Josef Bacik2bf64752011-09-26 17:12:22 -04001331 if (device->bytes_used > 0) {
1332 u64 len = btrfs_dev_extent_length(leaf, extent);
1333 device->bytes_used -= len;
1334 spin_lock(&root->fs_info->free_chunk_lock);
1335 root->fs_info->free_chunk_space += len;
1336 spin_unlock(&root->fs_info->free_chunk_lock);
1337 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001338 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001339 if (ret) {
1340 btrfs_error(root->fs_info, ret,
1341 "Failed to remove dev extent item");
1342 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001343out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001344 btrfs_free_path(path);
1345 return ret;
1346}
1347
Eric Sandeen48a3b632013-04-25 20:41:01 +00001348static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1349 struct btrfs_device *device,
1350 u64 chunk_tree, u64 chunk_objectid,
1351 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001352{
1353 int ret;
1354 struct btrfs_path *path;
1355 struct btrfs_root *root = device->dev_root;
1356 struct btrfs_dev_extent *extent;
1357 struct extent_buffer *leaf;
1358 struct btrfs_key key;
1359
Chris Masondfe25022008-05-13 13:46:40 -04001360 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001361 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001362 path = btrfs_alloc_path();
1363 if (!path)
1364 return -ENOMEM;
1365
Chris Mason0b86a832008-03-24 15:01:56 -04001366 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001367 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001368 key.type = BTRFS_DEV_EXTENT_KEY;
1369 ret = btrfs_insert_empty_item(trans, root, path, &key,
1370 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001371 if (ret)
1372 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001373
1374 leaf = path->nodes[0];
1375 extent = btrfs_item_ptr(leaf, path->slots[0],
1376 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001377 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1378 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1379 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1380
1381 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001382 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001383
Chris Mason0b86a832008-03-24 15:01:56 -04001384 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1385 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001386out:
Chris Mason0b86a832008-03-24 15:01:56 -04001387 btrfs_free_path(path);
1388 return ret;
1389}
1390
Josef Bacik6df9a952013-06-27 13:22:46 -04001391static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001392{
Josef Bacik6df9a952013-06-27 13:22:46 -04001393 struct extent_map_tree *em_tree;
1394 struct extent_map *em;
1395 struct rb_node *n;
1396 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001397
Josef Bacik6df9a952013-06-27 13:22:46 -04001398 em_tree = &fs_info->mapping_tree.map_tree;
1399 read_lock(&em_tree->lock);
1400 n = rb_last(&em_tree->map);
1401 if (n) {
1402 em = rb_entry(n, struct extent_map, rb_node);
1403 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001404 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001405 read_unlock(&em_tree->lock);
1406
Chris Mason0b86a832008-03-24 15:01:56 -04001407 return ret;
1408}
1409
Ilya Dryomov53f10652013-08-12 14:33:01 +03001410static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1411 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001412{
1413 int ret;
1414 struct btrfs_key key;
1415 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001416 struct btrfs_path *path;
1417
Yan Zheng2b820322008-11-17 21:11:30 -05001418 path = btrfs_alloc_path();
1419 if (!path)
1420 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001421
1422 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1423 key.type = BTRFS_DEV_ITEM_KEY;
1424 key.offset = (u64)-1;
1425
Ilya Dryomov53f10652013-08-12 14:33:01 +03001426 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001427 if (ret < 0)
1428 goto error;
1429
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001430 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001431
Ilya Dryomov53f10652013-08-12 14:33:01 +03001432 ret = btrfs_previous_item(fs_info->chunk_root, path,
1433 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001434 BTRFS_DEV_ITEM_KEY);
1435 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001436 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001437 } else {
1438 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1439 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001440 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001441 }
1442 ret = 0;
1443error:
Yan Zheng2b820322008-11-17 21:11:30 -05001444 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001445 return ret;
1446}
1447
1448/*
1449 * the device information is stored in the chunk root
1450 * the btrfs_device struct should be fully filled in
1451 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001452static int btrfs_add_device(struct btrfs_trans_handle *trans,
1453 struct btrfs_root *root,
1454 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001455{
1456 int ret;
1457 struct btrfs_path *path;
1458 struct btrfs_dev_item *dev_item;
1459 struct extent_buffer *leaf;
1460 struct btrfs_key key;
1461 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001462
1463 root = root->fs_info->chunk_root;
1464
1465 path = btrfs_alloc_path();
1466 if (!path)
1467 return -ENOMEM;
1468
Chris Mason0b86a832008-03-24 15:01:56 -04001469 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1470 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001471 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001472
1473 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001474 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001475 if (ret)
1476 goto out;
1477
1478 leaf = path->nodes[0];
1479 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1480
1481 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001482 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001483 btrfs_set_device_type(leaf, dev_item, device->type);
1484 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1485 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1486 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7df69d32014-07-24 11:37:13 +08001487 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001488 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001489 btrfs_set_device_group(leaf, dev_item, 0);
1490 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1491 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001492 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001493
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001494 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001495 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001496 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001497 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001498 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001499
Yan Zheng2b820322008-11-17 21:11:30 -05001500 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001501out:
1502 btrfs_free_path(path);
1503 return ret;
1504}
Chris Mason8f18cf12008-04-25 16:53:30 -04001505
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001506/*
1507 * Function to update ctime/mtime for a given device path.
1508 * Mainly used for ctime/mtime based probe like libblkid.
1509 */
1510static void update_dev_time(char *path_name)
1511{
1512 struct file *filp;
1513
1514 filp = filp_open(path_name, O_RDWR, 0);
1515 if (!filp)
1516 return;
1517 file_update_time(filp);
1518 filp_close(filp, NULL);
1519 return;
1520}
1521
Chris Masona061fc82008-05-07 11:43:44 -04001522static int btrfs_rm_dev_item(struct btrfs_root *root,
1523 struct btrfs_device *device)
1524{
1525 int ret;
1526 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001527 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001528 struct btrfs_trans_handle *trans;
1529
1530 root = root->fs_info->chunk_root;
1531
1532 path = btrfs_alloc_path();
1533 if (!path)
1534 return -ENOMEM;
1535
Yan, Zhenga22285a2010-05-16 10:48:46 -04001536 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001537 if (IS_ERR(trans)) {
1538 btrfs_free_path(path);
1539 return PTR_ERR(trans);
1540 }
Chris Masona061fc82008-05-07 11:43:44 -04001541 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1542 key.type = BTRFS_DEV_ITEM_KEY;
1543 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001544 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001545
1546 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1547 if (ret < 0)
1548 goto out;
1549
1550 if (ret > 0) {
1551 ret = -ENOENT;
1552 goto out;
1553 }
1554
1555 ret = btrfs_del_item(trans, root, path);
1556 if (ret)
1557 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001558out:
1559 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001560 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001561 btrfs_commit_transaction(trans, root);
1562 return ret;
1563}
1564
1565int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1566{
1567 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001568 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001569 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001570 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001571 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001572 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001573 u64 all_avail;
1574 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001575 u64 num_devices;
1576 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001577 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001578 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001579 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001580
Chris Masona061fc82008-05-07 11:43:44 -04001581 mutex_lock(&uuid_mutex);
1582
Miao Xiede98ced2013-01-29 10:13:12 +00001583 do {
1584 seq = read_seqbegin(&root->fs_info->profiles_lock);
1585
1586 all_avail = root->fs_info->avail_data_alloc_bits |
1587 root->fs_info->avail_system_alloc_bits |
1588 root->fs_info->avail_metadata_alloc_bits;
1589 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001590
Stefan Behrens8dabb742012-11-06 13:15:27 +01001591 num_devices = root->fs_info->fs_devices->num_devices;
1592 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1593 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1594 WARN_ON(num_devices < 1);
1595 num_devices--;
1596 }
1597 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1598
1599 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001600 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001601 goto out;
1602 }
1603
Stefan Behrens8dabb742012-11-06 13:15:27 +01001604 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001605 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001606 goto out;
1607 }
1608
David Woodhouse53b381b2013-01-29 18:40:14 -05001609 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1610 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001611 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001612 goto out;
1613 }
1614 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1615 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001616 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001617 goto out;
1618 }
1619
Chris Masondfe25022008-05-13 13:46:40 -04001620 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001621 struct list_head *devices;
1622 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001623
Chris Masondfe25022008-05-13 13:46:40 -04001624 device = NULL;
1625 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001626 /*
1627 * It is safe to read the devices since the volume_mutex
1628 * is held.
1629 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001630 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001631 if (tmp->in_fs_metadata &&
1632 !tmp->is_tgtdev_for_dev_replace &&
1633 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001634 device = tmp;
1635 break;
1636 }
1637 }
1638 bdev = NULL;
1639 bh = NULL;
1640 disk_super = NULL;
1641 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001642 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001643 goto out;
1644 }
Chris Masondfe25022008-05-13 13:46:40 -04001645 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001646 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001647 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001648 root->fs_info->bdev_holder, 0,
1649 &bdev, &bh);
1650 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001651 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001652 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001653 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001654 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001655 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001656 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001657 if (!device) {
1658 ret = -ENOENT;
1659 goto error_brelse;
1660 }
Chris Masondfe25022008-05-13 13:46:40 -04001661 }
Yan Zheng2b820322008-11-17 21:11:30 -05001662
Stefan Behrens63a212a2012-11-05 18:29:28 +01001663 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001664 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001665 goto error_brelse;
1666 }
1667
Yan Zheng2b820322008-11-17 21:11:30 -05001668 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001669 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001670 goto error_brelse;
1671 }
1672
1673 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001674 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001675 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001676 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001677 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001678 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001679 }
Chris Masona061fc82008-05-07 11:43:44 -04001680
Carey Underwoodd7901552013-03-04 16:37:06 -06001681 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001682 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001683 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001684 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001685 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001686
Stefan Behrens63a212a2012-11-05 18:29:28 +01001687 /*
1688 * TODO: the superblock still includes this device in its num_devices
1689 * counter although write_all_supers() is not locked out. This
1690 * could give a filesystem state which requires a degraded mount.
1691 */
Chris Masona061fc82008-05-07 11:43:44 -04001692 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1693 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001694 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001695
Josef Bacik2bf64752011-09-26 17:12:22 -04001696 spin_lock(&root->fs_info->free_chunk_lock);
1697 root->fs_info->free_chunk_space = device->total_bytes -
1698 device->bytes_used;
1699 spin_unlock(&root->fs_info->free_chunk_lock);
1700
Yan Zheng2b820322008-11-17 21:11:30 -05001701 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001702 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001703
1704 /*
1705 * the device list mutex makes sure that we don't change
1706 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001707 * the device supers. Whoever is writing all supers, should
1708 * lock the device list mutex before getting the number of
1709 * devices in the super block (super_copy). Conversely,
1710 * whoever updates the number of devices in the super block
1711 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001712 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001713
1714 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001715 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001716 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001717
Yan Zhenge4404d62008-12-12 10:03:26 -05001718 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001719 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001720
Chris Masoncd02dca2010-12-13 14:56:23 -05001721 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001722 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001723
Yan Zheng2b820322008-11-17 21:11:30 -05001724 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1725 struct btrfs_device, dev_list);
1726 if (device->bdev == root->fs_info->sb->s_bdev)
1727 root->fs_info->sb->s_bdev = next_device->bdev;
1728 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1729 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1730
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001731 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001732 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001733 /* remove sysfs entry */
1734 btrfs_kobj_rm_device(root->fs_info, device);
1735 }
Anand Jain99994cd2014-06-03 11:36:00 +08001736
Xiao Guangrong1f781602011-04-20 10:09:16 +00001737 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001738
David Sterba6c417612011-04-13 15:41:04 +02001739 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1740 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001741 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001742
Xiao Guangrong1f781602011-04-20 10:09:16 +00001743 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001744 struct btrfs_fs_devices *fs_devices;
1745 fs_devices = root->fs_info->fs_devices;
1746 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001747 if (fs_devices->seed == cur_devices) {
1748 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001749 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001750 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001751 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001752 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001753 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001754 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001755 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001756 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001757 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001758 }
1759
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001760 root->fs_info->num_tolerated_disk_barrier_failures =
1761 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1762
Yan Zheng2b820322008-11-17 21:11:30 -05001763 /*
1764 * at this point, the device is zero sized. We want to
1765 * remove it from the devices list and zero out the old super
1766 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001767 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001768 u64 bytenr;
1769 int i;
1770
Chris Masondfe25022008-05-13 13:46:40 -04001771 /* make sure this device isn't detected as part of
1772 * the FS anymore
1773 */
1774 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1775 set_buffer_dirty(bh);
1776 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001777
1778 /* clear the mirror copies of super block on the disk
1779 * being removed, 0th copy is been taken care above and
1780 * the below would take of the rest
1781 */
1782 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1783 bytenr = btrfs_sb_offset(i);
1784 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1785 i_size_read(bdev->bd_inode))
1786 break;
1787
1788 brelse(bh);
1789 bh = __bread(bdev, bytenr / 4096,
1790 BTRFS_SUPER_INFO_SIZE);
1791 if (!bh)
1792 continue;
1793
1794 disk_super = (struct btrfs_super_block *)bh->b_data;
1795
1796 if (btrfs_super_bytenr(disk_super) != bytenr ||
1797 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1798 continue;
1799 }
1800 memset(&disk_super->magic, 0,
1801 sizeof(disk_super->magic));
1802 set_buffer_dirty(bh);
1803 sync_dirty_buffer(bh);
1804 }
Chris Masondfe25022008-05-13 13:46:40 -04001805 }
Chris Masona061fc82008-05-07 11:43:44 -04001806
Chris Masona061fc82008-05-07 11:43:44 -04001807 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001808
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001809 if (bdev) {
1810 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001811 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001812
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001813 /* Update ctime/mtime for device path for libblkid */
1814 update_dev_time(device_path);
1815 }
1816
Chris Masona061fc82008-05-07 11:43:44 -04001817error_brelse:
1818 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001819 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001820 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001821out:
1822 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001823 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001824error_undo:
1825 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001826 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001827 list_add(&device->dev_alloc_list,
1828 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001829 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001830 root->fs_info->fs_devices->rw_devices++;
1831 }
1832 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001833}
1834
Stefan Behrense93c89c2012-11-05 17:33:06 +01001835void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1836 struct btrfs_device *srcdev)
1837{
1838 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001839
Stefan Behrense93c89c2012-11-05 17:33:06 +01001840 list_del_rcu(&srcdev->dev_list);
1841 list_del_rcu(&srcdev->dev_alloc_list);
1842 fs_info->fs_devices->num_devices--;
1843 if (srcdev->missing) {
1844 fs_info->fs_devices->missing_devices--;
1845 fs_info->fs_devices->rw_devices++;
1846 }
1847 if (srcdev->can_discard)
1848 fs_info->fs_devices->num_can_discard--;
Ilya Dryomov13572722013-10-02 20:41:01 +03001849 if (srcdev->bdev) {
Stefan Behrense93c89c2012-11-05 17:33:06 +01001850 fs_info->fs_devices->open_devices--;
1851
Miao Xieff61d172014-07-24 11:37:06 +08001852 /*
1853 * zero out the old super if it is not writable
1854 * (e.g. seed device)
1855 */
1856 if (srcdev->writeable)
1857 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03001858 }
1859
Stefan Behrense93c89c2012-11-05 17:33:06 +01001860 call_rcu(&srcdev->rcu, free_device);
1861}
1862
1863void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1864 struct btrfs_device *tgtdev)
1865{
1866 struct btrfs_device *next_device;
1867
1868 WARN_ON(!tgtdev);
1869 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1870 if (tgtdev->bdev) {
1871 btrfs_scratch_superblock(tgtdev);
1872 fs_info->fs_devices->open_devices--;
1873 }
1874 fs_info->fs_devices->num_devices--;
1875 if (tgtdev->can_discard)
1876 fs_info->fs_devices->num_can_discard++;
1877
1878 next_device = list_entry(fs_info->fs_devices->devices.next,
1879 struct btrfs_device, dev_list);
1880 if (tgtdev->bdev == fs_info->sb->s_bdev)
1881 fs_info->sb->s_bdev = next_device->bdev;
1882 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1883 fs_info->fs_devices->latest_bdev = next_device->bdev;
1884 list_del_rcu(&tgtdev->dev_list);
1885
1886 call_rcu(&tgtdev->rcu, free_device);
1887
1888 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1889}
1890
Eric Sandeen48a3b632013-04-25 20:41:01 +00001891static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1892 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001893{
1894 int ret = 0;
1895 struct btrfs_super_block *disk_super;
1896 u64 devid;
1897 u8 *dev_uuid;
1898 struct block_device *bdev;
1899 struct buffer_head *bh;
1900
1901 *device = NULL;
1902 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1903 root->fs_info->bdev_holder, 0, &bdev, &bh);
1904 if (ret)
1905 return ret;
1906 disk_super = (struct btrfs_super_block *)bh->b_data;
1907 devid = btrfs_stack_device_id(&disk_super->dev_item);
1908 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001909 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001910 disk_super->fsid);
1911 brelse(bh);
1912 if (!*device)
1913 ret = -ENOENT;
1914 blkdev_put(bdev, FMODE_READ);
1915 return ret;
1916}
1917
1918int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1919 char *device_path,
1920 struct btrfs_device **device)
1921{
1922 *device = NULL;
1923 if (strcmp(device_path, "missing") == 0) {
1924 struct list_head *devices;
1925 struct btrfs_device *tmp;
1926
1927 devices = &root->fs_info->fs_devices->devices;
1928 /*
1929 * It is safe to read the devices since the volume_mutex
1930 * is held by the caller.
1931 */
1932 list_for_each_entry(tmp, devices, dev_list) {
1933 if (tmp->in_fs_metadata && !tmp->bdev) {
1934 *device = tmp;
1935 break;
1936 }
1937 }
1938
1939 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001940 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001941 return -ENOENT;
1942 }
1943
1944 return 0;
1945 } else {
1946 return btrfs_find_device_by_path(root, device_path, device);
1947 }
1948}
1949
Yan Zheng2b820322008-11-17 21:11:30 -05001950/*
1951 * does all the dirty work required for changing file system's UUID.
1952 */
Li Zefan125ccb02011-12-08 15:07:24 +08001953static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001954{
1955 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1956 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001957 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001958 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001959 struct btrfs_device *device;
1960 u64 super_flags;
1961
1962 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001963 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001964 return -EINVAL;
1965
Ilya Dryomov2208a372013-08-12 14:33:03 +03001966 seed_devices = __alloc_fs_devices();
1967 if (IS_ERR(seed_devices))
1968 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001969
Yan Zhenge4404d62008-12-12 10:03:26 -05001970 old_devices = clone_fs_devices(fs_devices);
1971 if (IS_ERR(old_devices)) {
1972 kfree(seed_devices);
1973 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001974 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001975
Yan Zheng2b820322008-11-17 21:11:30 -05001976 list_add(&old_devices->list, &fs_uuids);
1977
Yan Zhenge4404d62008-12-12 10:03:26 -05001978 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1979 seed_devices->opened = 1;
1980 INIT_LIST_HEAD(&seed_devices->devices);
1981 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001982 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001983
1984 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001985 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1986 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001987
Yan Zhenge4404d62008-12-12 10:03:26 -05001988 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1989 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1990 device->fs_devices = seed_devices;
1991 }
1992
Yan Zheng2b820322008-11-17 21:11:30 -05001993 fs_devices->seeding = 0;
1994 fs_devices->num_devices = 0;
1995 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08001996 fs_devices->missing_devices = 0;
1997 fs_devices->num_can_discard = 0;
1998 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001999 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002000
2001 generate_random_uuid(fs_devices->fsid);
2002 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2003 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002004 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2005
Yan Zheng2b820322008-11-17 21:11:30 -05002006 super_flags = btrfs_super_flags(disk_super) &
2007 ~BTRFS_SUPER_FLAG_SEEDING;
2008 btrfs_set_super_flags(disk_super, super_flags);
2009
2010 return 0;
2011}
2012
2013/*
2014 * strore the expected generation for seed devices in device items.
2015 */
2016static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2017 struct btrfs_root *root)
2018{
2019 struct btrfs_path *path;
2020 struct extent_buffer *leaf;
2021 struct btrfs_dev_item *dev_item;
2022 struct btrfs_device *device;
2023 struct btrfs_key key;
2024 u8 fs_uuid[BTRFS_UUID_SIZE];
2025 u8 dev_uuid[BTRFS_UUID_SIZE];
2026 u64 devid;
2027 int ret;
2028
2029 path = btrfs_alloc_path();
2030 if (!path)
2031 return -ENOMEM;
2032
2033 root = root->fs_info->chunk_root;
2034 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2035 key.offset = 0;
2036 key.type = BTRFS_DEV_ITEM_KEY;
2037
2038 while (1) {
2039 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2040 if (ret < 0)
2041 goto error;
2042
2043 leaf = path->nodes[0];
2044next_slot:
2045 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2046 ret = btrfs_next_leaf(root, path);
2047 if (ret > 0)
2048 break;
2049 if (ret < 0)
2050 goto error;
2051 leaf = path->nodes[0];
2052 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002053 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002054 continue;
2055 }
2056
2057 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2058 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2059 key.type != BTRFS_DEV_ITEM_KEY)
2060 break;
2061
2062 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2063 struct btrfs_dev_item);
2064 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002065 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002066 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002067 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002068 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002069 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2070 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002071 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002072
2073 if (device->fs_devices->seeding) {
2074 btrfs_set_device_generation(leaf, dev_item,
2075 device->generation);
2076 btrfs_mark_buffer_dirty(leaf);
2077 }
2078
2079 path->slots[0]++;
2080 goto next_slot;
2081 }
2082 ret = 0;
2083error:
2084 btrfs_free_path(path);
2085 return ret;
2086}
2087
Chris Mason788f20e2008-04-28 15:29:42 -04002088int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2089{
Josef Bacikd5e20032011-08-04 14:52:27 +00002090 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002091 struct btrfs_trans_handle *trans;
2092 struct btrfs_device *device;
2093 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002094 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002095 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002096 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04002097 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002098 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002099 int ret = 0;
2100
Yan Zheng2b820322008-11-17 21:11:30 -05002101 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002102 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002103
Li Zefana5d16332011-12-07 20:08:40 -05002104 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002105 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002106 if (IS_ERR(bdev))
2107 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002108
Yan Zheng2b820322008-11-17 21:11:30 -05002109 if (root->fs_info->fs_devices->seeding) {
2110 seeding_dev = 1;
2111 down_write(&sb->s_umount);
2112 mutex_lock(&uuid_mutex);
2113 }
2114
Chris Mason8c8bee12008-09-29 11:19:10 -04002115 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002116
Chris Mason788f20e2008-04-28 15:29:42 -04002117 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002118
2119 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002120 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002121 if (device->bdev == bdev) {
2122 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002123 mutex_unlock(
2124 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002125 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002126 }
2127 }
Liu Bod25628b2012-11-14 14:35:30 +00002128 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002129
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002130 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2131 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002132 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002133 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002134 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002135 }
2136
Josef Bacik606686e2012-06-04 14:03:51 -04002137 name = rcu_string_strdup(device_path, GFP_NOFS);
2138 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002139 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002140 ret = -ENOMEM;
2141 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002142 }
Josef Bacik606686e2012-06-04 14:03:51 -04002143 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002144
Yan, Zhenga22285a2010-05-16 10:48:46 -04002145 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002146 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002147 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002148 kfree(device);
2149 ret = PTR_ERR(trans);
2150 goto error;
2151 }
2152
Yan Zheng2b820322008-11-17 21:11:30 -05002153 lock_chunks(root);
2154
Josef Bacikd5e20032011-08-04 14:52:27 +00002155 q = bdev_get_queue(bdev);
2156 if (blk_queue_discard(q))
2157 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002158 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002159 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002160 device->io_width = root->sectorsize;
2161 device->io_align = root->sectorsize;
2162 device->sector_size = root->sectorsize;
2163 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002164 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002165 device->dev_root = root->fs_info->dev_root;
2166 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002167 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002168 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002169 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002170 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002171 set_blocksize(device->bdev, 4096);
2172
Yan Zheng2b820322008-11-17 21:11:30 -05002173 if (seeding_dev) {
2174 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002175 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002176 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002177 }
2178
2179 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002180
Chris Masone5e9a522009-06-10 15:17:02 -04002181 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002182 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002183 list_add(&device->dev_alloc_list,
2184 &root->fs_info->fs_devices->alloc_list);
2185 root->fs_info->fs_devices->num_devices++;
2186 root->fs_info->fs_devices->open_devices++;
2187 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002188 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002189 if (device->can_discard)
2190 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002191 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2192
Josef Bacik2bf64752011-09-26 17:12:22 -04002193 spin_lock(&root->fs_info->free_chunk_lock);
2194 root->fs_info->free_chunk_space += device->total_bytes;
2195 spin_unlock(&root->fs_info->free_chunk_lock);
2196
Chris Masonc2898112009-06-10 09:51:32 -04002197 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2198 root->fs_info->fs_devices->rotating = 1;
2199
David Sterba6c417612011-04-13 15:41:04 +02002200 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2201 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002202 total_bytes + device->total_bytes);
2203
David Sterba6c417612011-04-13 15:41:04 +02002204 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2205 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002206 total_bytes + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002207
2208 /* add sysfs device entry */
2209 btrfs_kobj_add_device(root->fs_info, device);
2210
Chris Masone5e9a522009-06-10 15:17:02 -04002211 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002212
Yan Zheng2b820322008-11-17 21:11:30 -05002213 if (seeding_dev) {
Anand Jainb2373f22014-06-03 11:36:03 +08002214 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002215 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002216 if (ret) {
2217 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002218 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002219 }
Yan Zheng2b820322008-11-17 21:11:30 -05002220 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002221 if (ret) {
2222 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002223 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002224 }
Anand Jainb2373f22014-06-03 11:36:03 +08002225
2226 /* Sprouting would change fsid of the mounted root,
2227 * so rename the fsid on the sysfs
2228 */
2229 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2230 root->fs_info->fsid);
2231 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2232 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002233 } else {
2234 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002235 if (ret) {
2236 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002237 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002238 }
Yan Zheng2b820322008-11-17 21:11:30 -05002239 }
2240
Chris Mason913d9522009-03-10 13:17:18 -04002241 /*
2242 * we've got more storage, clear any full flags on the space
2243 * infos
2244 */
2245 btrfs_clear_space_info_full(root->fs_info);
2246
Chris Mason7d9eb122008-07-08 14:19:17 -04002247 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002248 root->fs_info->num_tolerated_disk_barrier_failures =
2249 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002250 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002251
2252 if (seeding_dev) {
2253 mutex_unlock(&uuid_mutex);
2254 up_write(&sb->s_umount);
2255
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002256 if (ret) /* transaction commit */
2257 return ret;
2258
Yan Zheng2b820322008-11-17 21:11:30 -05002259 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002260 if (ret < 0)
2261 btrfs_error(root->fs_info, ret,
2262 "Failed to relocate sys chunks after "
2263 "device initialization. This can be fixed "
2264 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002265 trans = btrfs_attach_transaction(root);
2266 if (IS_ERR(trans)) {
2267 if (PTR_ERR(trans) == -ENOENT)
2268 return 0;
2269 return PTR_ERR(trans);
2270 }
2271 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002272 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002273
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002274 /* Update ctime/mtime for libblkid */
2275 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002276 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002277
2278error_trans:
2279 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002280 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002281 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002282 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002283 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002284error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002285 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002286 if (seeding_dev) {
2287 mutex_unlock(&uuid_mutex);
2288 up_write(&sb->s_umount);
2289 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002290 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002291}
2292
Stefan Behrense93c89c2012-11-05 17:33:06 +01002293int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2294 struct btrfs_device **device_out)
2295{
2296 struct request_queue *q;
2297 struct btrfs_device *device;
2298 struct block_device *bdev;
2299 struct btrfs_fs_info *fs_info = root->fs_info;
2300 struct list_head *devices;
2301 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002302 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002303 int ret = 0;
2304
2305 *device_out = NULL;
2306 if (fs_info->fs_devices->seeding)
2307 return -EINVAL;
2308
2309 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2310 fs_info->bdev_holder);
2311 if (IS_ERR(bdev))
2312 return PTR_ERR(bdev);
2313
2314 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2315
2316 devices = &fs_info->fs_devices->devices;
2317 list_for_each_entry(device, devices, dev_list) {
2318 if (device->bdev == bdev) {
2319 ret = -EEXIST;
2320 goto error;
2321 }
2322 }
2323
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002324 device = btrfs_alloc_device(NULL, &devid, NULL);
2325 if (IS_ERR(device)) {
2326 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002327 goto error;
2328 }
2329
2330 name = rcu_string_strdup(device_path, GFP_NOFS);
2331 if (!name) {
2332 kfree(device);
2333 ret = -ENOMEM;
2334 goto error;
2335 }
2336 rcu_assign_pointer(device->name, name);
2337
2338 q = bdev_get_queue(bdev);
2339 if (blk_queue_discard(q))
2340 device->can_discard = 1;
2341 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2342 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002343 device->generation = 0;
2344 device->io_width = root->sectorsize;
2345 device->io_align = root->sectorsize;
2346 device->sector_size = root->sectorsize;
2347 device->total_bytes = i_size_read(bdev->bd_inode);
2348 device->disk_total_bytes = device->total_bytes;
2349 device->dev_root = fs_info->dev_root;
2350 device->bdev = bdev;
2351 device->in_fs_metadata = 1;
2352 device->is_tgtdev_for_dev_replace = 1;
2353 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002354 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002355 set_blocksize(device->bdev, 4096);
2356 device->fs_devices = fs_info->fs_devices;
2357 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2358 fs_info->fs_devices->num_devices++;
2359 fs_info->fs_devices->open_devices++;
2360 if (device->can_discard)
2361 fs_info->fs_devices->num_can_discard++;
2362 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2363
2364 *device_out = device;
2365 return ret;
2366
2367error:
2368 blkdev_put(bdev, FMODE_EXCL);
2369 return ret;
2370}
2371
2372void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2373 struct btrfs_device *tgtdev)
2374{
2375 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2376 tgtdev->io_width = fs_info->dev_root->sectorsize;
2377 tgtdev->io_align = fs_info->dev_root->sectorsize;
2378 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2379 tgtdev->dev_root = fs_info->dev_root;
2380 tgtdev->in_fs_metadata = 1;
2381}
2382
Chris Masond3977122009-01-05 21:25:51 -05002383static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2384 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002385{
2386 int ret;
2387 struct btrfs_path *path;
2388 struct btrfs_root *root;
2389 struct btrfs_dev_item *dev_item;
2390 struct extent_buffer *leaf;
2391 struct btrfs_key key;
2392
2393 root = device->dev_root->fs_info->chunk_root;
2394
2395 path = btrfs_alloc_path();
2396 if (!path)
2397 return -ENOMEM;
2398
2399 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2400 key.type = BTRFS_DEV_ITEM_KEY;
2401 key.offset = device->devid;
2402
2403 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2404 if (ret < 0)
2405 goto out;
2406
2407 if (ret > 0) {
2408 ret = -ENOENT;
2409 goto out;
2410 }
2411
2412 leaf = path->nodes[0];
2413 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2414
2415 btrfs_set_device_id(leaf, dev_item, device->devid);
2416 btrfs_set_device_type(leaf, dev_item, device->type);
2417 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2418 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2419 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002420 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002421 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2422 btrfs_mark_buffer_dirty(leaf);
2423
2424out:
2425 btrfs_free_path(path);
2426 return ret;
2427}
2428
Chris Mason7d9eb122008-07-08 14:19:17 -04002429static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002430 struct btrfs_device *device, u64 new_size)
2431{
2432 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002433 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002434 u64 old_total = btrfs_super_total_bytes(super_copy);
2435 u64 diff = new_size - device->total_bytes;
2436
Yan Zheng2b820322008-11-17 21:11:30 -05002437 if (!device->writeable)
2438 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002439 if (new_size <= device->total_bytes ||
2440 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002441 return -EINVAL;
2442
Chris Mason8f18cf12008-04-25 16:53:30 -04002443 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002444 device->fs_devices->total_rw_bytes += diff;
2445
2446 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002447 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002448 btrfs_clear_space_info_full(device->dev_root->fs_info);
2449
Chris Mason8f18cf12008-04-25 16:53:30 -04002450 return btrfs_update_device(trans, device);
2451}
2452
Chris Mason7d9eb122008-07-08 14:19:17 -04002453int btrfs_grow_device(struct btrfs_trans_handle *trans,
2454 struct btrfs_device *device, u64 new_size)
2455{
2456 int ret;
2457 lock_chunks(device->dev_root);
2458 ret = __btrfs_grow_device(trans, device, new_size);
2459 unlock_chunks(device->dev_root);
2460 return ret;
2461}
2462
Chris Mason8f18cf12008-04-25 16:53:30 -04002463static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2464 struct btrfs_root *root,
2465 u64 chunk_tree, u64 chunk_objectid,
2466 u64 chunk_offset)
2467{
2468 int ret;
2469 struct btrfs_path *path;
2470 struct btrfs_key key;
2471
2472 root = root->fs_info->chunk_root;
2473 path = btrfs_alloc_path();
2474 if (!path)
2475 return -ENOMEM;
2476
2477 key.objectid = chunk_objectid;
2478 key.offset = chunk_offset;
2479 key.type = BTRFS_CHUNK_ITEM_KEY;
2480
2481 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002482 if (ret < 0)
2483 goto out;
2484 else if (ret > 0) { /* Logic error or corruption */
2485 btrfs_error(root->fs_info, -ENOENT,
2486 "Failed lookup while freeing chunk.");
2487 ret = -ENOENT;
2488 goto out;
2489 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002490
2491 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002492 if (ret < 0)
2493 btrfs_error(root->fs_info, ret,
2494 "Failed to delete chunk item.");
2495out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002496 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002497 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002498}
2499
Christoph Hellwigb2950862008-12-02 09:54:17 -05002500static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002501 chunk_offset)
2502{
David Sterba6c417612011-04-13 15:41:04 +02002503 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002504 struct btrfs_disk_key *disk_key;
2505 struct btrfs_chunk *chunk;
2506 u8 *ptr;
2507 int ret = 0;
2508 u32 num_stripes;
2509 u32 array_size;
2510 u32 len = 0;
2511 u32 cur;
2512 struct btrfs_key key;
2513
2514 array_size = btrfs_super_sys_array_size(super_copy);
2515
2516 ptr = super_copy->sys_chunk_array;
2517 cur = 0;
2518
2519 while (cur < array_size) {
2520 disk_key = (struct btrfs_disk_key *)ptr;
2521 btrfs_disk_key_to_cpu(&key, disk_key);
2522
2523 len = sizeof(*disk_key);
2524
2525 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2526 chunk = (struct btrfs_chunk *)(ptr + len);
2527 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2528 len += btrfs_chunk_item_size(num_stripes);
2529 } else {
2530 ret = -EIO;
2531 break;
2532 }
2533 if (key.objectid == chunk_objectid &&
2534 key.offset == chunk_offset) {
2535 memmove(ptr, ptr + len, array_size - (cur + len));
2536 array_size -= len;
2537 btrfs_set_super_sys_array_size(super_copy, array_size);
2538 } else {
2539 ptr += len;
2540 cur += len;
2541 }
2542 }
2543 return ret;
2544}
2545
Christoph Hellwigb2950862008-12-02 09:54:17 -05002546static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002547 u64 chunk_tree, u64 chunk_objectid,
2548 u64 chunk_offset)
2549{
2550 struct extent_map_tree *em_tree;
2551 struct btrfs_root *extent_root;
2552 struct btrfs_trans_handle *trans;
2553 struct extent_map *em;
2554 struct map_lookup *map;
2555 int ret;
2556 int i;
2557
2558 root = root->fs_info->chunk_root;
2559 extent_root = root->fs_info->extent_root;
2560 em_tree = &root->fs_info->mapping_tree.map_tree;
2561
Josef Bacikba1bf482009-09-11 16:11:19 -04002562 ret = btrfs_can_relocate(extent_root, chunk_offset);
2563 if (ret)
2564 return -ENOSPC;
2565
Chris Mason8f18cf12008-04-25 16:53:30 -04002566 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002567 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002568 if (ret)
2569 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002570
Yan, Zhenga22285a2010-05-16 10:48:46 -04002571 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002572 if (IS_ERR(trans)) {
2573 ret = PTR_ERR(trans);
2574 btrfs_std_error(root->fs_info, ret);
2575 return ret;
2576 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002577
Chris Mason7d9eb122008-07-08 14:19:17 -04002578 lock_chunks(root);
2579
Chris Mason8f18cf12008-04-25 16:53:30 -04002580 /*
2581 * step two, delete the device extents and the
2582 * chunk tree entries
2583 */
Chris Mason890871b2009-09-02 16:24:52 -04002584 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002585 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002586 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002587
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002588 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002589 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002590 map = (struct map_lookup *)em->bdev;
2591
2592 for (i = 0; i < map->num_stripes; i++) {
2593 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2594 map->stripes[i].physical);
2595 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002596
Chris Masondfe25022008-05-13 13:46:40 -04002597 if (map->stripes[i].dev) {
2598 ret = btrfs_update_device(trans, map->stripes[i].dev);
2599 BUG_ON(ret);
2600 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002601 }
2602 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2603 chunk_offset);
2604
2605 BUG_ON(ret);
2606
liubo1abe9b82011-03-24 11:18:59 +00002607 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2608
Chris Mason8f18cf12008-04-25 16:53:30 -04002609 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2610 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2611 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002612 }
2613
Zheng Yan1a40e232008-09-26 10:09:34 -04002614 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2615 BUG_ON(ret);
2616
Chris Mason890871b2009-09-02 16:24:52 -04002617 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002618 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002619 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002620
Chris Mason8f18cf12008-04-25 16:53:30 -04002621 /* once for the tree */
2622 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002623 /* once for us */
2624 free_extent_map(em);
2625
Chris Mason7d9eb122008-07-08 14:19:17 -04002626 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002627 btrfs_end_transaction(trans, root);
2628 return 0;
2629}
2630
Yan Zheng2b820322008-11-17 21:11:30 -05002631static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2632{
2633 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2634 struct btrfs_path *path;
2635 struct extent_buffer *leaf;
2636 struct btrfs_chunk *chunk;
2637 struct btrfs_key key;
2638 struct btrfs_key found_key;
2639 u64 chunk_tree = chunk_root->root_key.objectid;
2640 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002641 bool retried = false;
2642 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002643 int ret;
2644
2645 path = btrfs_alloc_path();
2646 if (!path)
2647 return -ENOMEM;
2648
Josef Bacikba1bf482009-09-11 16:11:19 -04002649again:
Yan Zheng2b820322008-11-17 21:11:30 -05002650 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2651 key.offset = (u64)-1;
2652 key.type = BTRFS_CHUNK_ITEM_KEY;
2653
2654 while (1) {
2655 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2656 if (ret < 0)
2657 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002658 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002659
2660 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2661 key.type);
2662 if (ret < 0)
2663 goto error;
2664 if (ret > 0)
2665 break;
2666
2667 leaf = path->nodes[0];
2668 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2669
2670 chunk = btrfs_item_ptr(leaf, path->slots[0],
2671 struct btrfs_chunk);
2672 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002673 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002674
2675 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2676 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2677 found_key.objectid,
2678 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002679 if (ret == -ENOSPC)
2680 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302681 else
2682 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002683 }
2684
2685 if (found_key.offset == 0)
2686 break;
2687 key.offset = found_key.offset - 1;
2688 }
2689 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002690 if (failed && !retried) {
2691 failed = 0;
2692 retried = true;
2693 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302694 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002695 ret = -ENOSPC;
2696 }
Yan Zheng2b820322008-11-17 21:11:30 -05002697error:
2698 btrfs_free_path(path);
2699 return ret;
2700}
2701
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002702static int insert_balance_item(struct btrfs_root *root,
2703 struct btrfs_balance_control *bctl)
2704{
2705 struct btrfs_trans_handle *trans;
2706 struct btrfs_balance_item *item;
2707 struct btrfs_disk_balance_args disk_bargs;
2708 struct btrfs_path *path;
2709 struct extent_buffer *leaf;
2710 struct btrfs_key key;
2711 int ret, err;
2712
2713 path = btrfs_alloc_path();
2714 if (!path)
2715 return -ENOMEM;
2716
2717 trans = btrfs_start_transaction(root, 0);
2718 if (IS_ERR(trans)) {
2719 btrfs_free_path(path);
2720 return PTR_ERR(trans);
2721 }
2722
2723 key.objectid = BTRFS_BALANCE_OBJECTID;
2724 key.type = BTRFS_BALANCE_ITEM_KEY;
2725 key.offset = 0;
2726
2727 ret = btrfs_insert_empty_item(trans, root, path, &key,
2728 sizeof(*item));
2729 if (ret)
2730 goto out;
2731
2732 leaf = path->nodes[0];
2733 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2734
2735 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2736
2737 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2738 btrfs_set_balance_data(leaf, item, &disk_bargs);
2739 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2740 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2741 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2742 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2743
2744 btrfs_set_balance_flags(leaf, item, bctl->flags);
2745
2746 btrfs_mark_buffer_dirty(leaf);
2747out:
2748 btrfs_free_path(path);
2749 err = btrfs_commit_transaction(trans, root);
2750 if (err && !ret)
2751 ret = err;
2752 return ret;
2753}
2754
2755static int del_balance_item(struct btrfs_root *root)
2756{
2757 struct btrfs_trans_handle *trans;
2758 struct btrfs_path *path;
2759 struct btrfs_key key;
2760 int ret, err;
2761
2762 path = btrfs_alloc_path();
2763 if (!path)
2764 return -ENOMEM;
2765
2766 trans = btrfs_start_transaction(root, 0);
2767 if (IS_ERR(trans)) {
2768 btrfs_free_path(path);
2769 return PTR_ERR(trans);
2770 }
2771
2772 key.objectid = BTRFS_BALANCE_OBJECTID;
2773 key.type = BTRFS_BALANCE_ITEM_KEY;
2774 key.offset = 0;
2775
2776 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2777 if (ret < 0)
2778 goto out;
2779 if (ret > 0) {
2780 ret = -ENOENT;
2781 goto out;
2782 }
2783
2784 ret = btrfs_del_item(trans, root, path);
2785out:
2786 btrfs_free_path(path);
2787 err = btrfs_commit_transaction(trans, root);
2788 if (err && !ret)
2789 ret = err;
2790 return ret;
2791}
2792
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002793/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002794 * This is a heuristic used to reduce the number of chunks balanced on
2795 * resume after balance was interrupted.
2796 */
2797static void update_balance_args(struct btrfs_balance_control *bctl)
2798{
2799 /*
2800 * Turn on soft mode for chunk types that were being converted.
2801 */
2802 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2803 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2804 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2805 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2806 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2807 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2808
2809 /*
2810 * Turn on usage filter if is not already used. The idea is
2811 * that chunks that we have already balanced should be
2812 * reasonably full. Don't do it for chunks that are being
2813 * converted - that will keep us from relocating unconverted
2814 * (albeit full) chunks.
2815 */
2816 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2817 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2818 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2819 bctl->data.usage = 90;
2820 }
2821 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2822 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2823 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2824 bctl->sys.usage = 90;
2825 }
2826 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2827 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2828 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2829 bctl->meta.usage = 90;
2830 }
2831}
2832
2833/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002834 * Should be called with both balance and volume mutexes held to
2835 * serialize other volume operations (add_dev/rm_dev/resize) with
2836 * restriper. Same goes for unset_balance_control.
2837 */
2838static void set_balance_control(struct btrfs_balance_control *bctl)
2839{
2840 struct btrfs_fs_info *fs_info = bctl->fs_info;
2841
2842 BUG_ON(fs_info->balance_ctl);
2843
2844 spin_lock(&fs_info->balance_lock);
2845 fs_info->balance_ctl = bctl;
2846 spin_unlock(&fs_info->balance_lock);
2847}
2848
2849static void unset_balance_control(struct btrfs_fs_info *fs_info)
2850{
2851 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2852
2853 BUG_ON(!fs_info->balance_ctl);
2854
2855 spin_lock(&fs_info->balance_lock);
2856 fs_info->balance_ctl = NULL;
2857 spin_unlock(&fs_info->balance_lock);
2858
2859 kfree(bctl);
2860}
2861
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002862/*
2863 * Balance filters. Return 1 if chunk should be filtered out
2864 * (should not be balanced).
2865 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002866static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002867 struct btrfs_balance_args *bargs)
2868{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002869 chunk_type = chunk_to_extended(chunk_type) &
2870 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002871
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002872 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002873 return 0;
2874
2875 return 1;
2876}
2877
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002878static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2879 struct btrfs_balance_args *bargs)
2880{
2881 struct btrfs_block_group_cache *cache;
2882 u64 chunk_used, user_thresh;
2883 int ret = 1;
2884
2885 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2886 chunk_used = btrfs_block_group_used(&cache->item);
2887
Ilya Dryomova105bb82013-01-21 15:15:56 +02002888 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002889 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002890 else if (bargs->usage > 100)
2891 user_thresh = cache->key.offset;
2892 else
2893 user_thresh = div_factor_fine(cache->key.offset,
2894 bargs->usage);
2895
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002896 if (chunk_used < user_thresh)
2897 ret = 0;
2898
2899 btrfs_put_block_group(cache);
2900 return ret;
2901}
2902
Ilya Dryomov409d4042012-01-16 22:04:47 +02002903static int chunk_devid_filter(struct extent_buffer *leaf,
2904 struct btrfs_chunk *chunk,
2905 struct btrfs_balance_args *bargs)
2906{
2907 struct btrfs_stripe *stripe;
2908 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2909 int i;
2910
2911 for (i = 0; i < num_stripes; i++) {
2912 stripe = btrfs_stripe_nr(chunk, i);
2913 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2914 return 0;
2915 }
2916
2917 return 1;
2918}
2919
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002920/* [pstart, pend) */
2921static int chunk_drange_filter(struct extent_buffer *leaf,
2922 struct btrfs_chunk *chunk,
2923 u64 chunk_offset,
2924 struct btrfs_balance_args *bargs)
2925{
2926 struct btrfs_stripe *stripe;
2927 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2928 u64 stripe_offset;
2929 u64 stripe_length;
2930 int factor;
2931 int i;
2932
2933 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2934 return 0;
2935
2936 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002937 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2938 factor = num_stripes / 2;
2939 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2940 factor = num_stripes - 1;
2941 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2942 factor = num_stripes - 2;
2943 } else {
2944 factor = num_stripes;
2945 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002946
2947 for (i = 0; i < num_stripes; i++) {
2948 stripe = btrfs_stripe_nr(chunk, i);
2949 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2950 continue;
2951
2952 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2953 stripe_length = btrfs_chunk_length(leaf, chunk);
2954 do_div(stripe_length, factor);
2955
2956 if (stripe_offset < bargs->pend &&
2957 stripe_offset + stripe_length > bargs->pstart)
2958 return 0;
2959 }
2960
2961 return 1;
2962}
2963
Ilya Dryomovea671762012-01-16 22:04:48 +02002964/* [vstart, vend) */
2965static int chunk_vrange_filter(struct extent_buffer *leaf,
2966 struct btrfs_chunk *chunk,
2967 u64 chunk_offset,
2968 struct btrfs_balance_args *bargs)
2969{
2970 if (chunk_offset < bargs->vend &&
2971 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2972 /* at least part of the chunk is inside this vrange */
2973 return 0;
2974
2975 return 1;
2976}
2977
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002978static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002979 struct btrfs_balance_args *bargs)
2980{
2981 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2982 return 0;
2983
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002984 chunk_type = chunk_to_extended(chunk_type) &
2985 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002986
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002987 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002988 return 1;
2989
2990 return 0;
2991}
2992
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002993static int should_balance_chunk(struct btrfs_root *root,
2994 struct extent_buffer *leaf,
2995 struct btrfs_chunk *chunk, u64 chunk_offset)
2996{
2997 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2998 struct btrfs_balance_args *bargs = NULL;
2999 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3000
3001 /* type filter */
3002 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3003 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3004 return 0;
3005 }
3006
3007 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3008 bargs = &bctl->data;
3009 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3010 bargs = &bctl->sys;
3011 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3012 bargs = &bctl->meta;
3013
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003014 /* profiles filter */
3015 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3016 chunk_profiles_filter(chunk_type, bargs)) {
3017 return 0;
3018 }
3019
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003020 /* usage filter */
3021 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3022 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3023 return 0;
3024 }
3025
Ilya Dryomov409d4042012-01-16 22:04:47 +02003026 /* devid filter */
3027 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3028 chunk_devid_filter(leaf, chunk, bargs)) {
3029 return 0;
3030 }
3031
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003032 /* drange filter, makes sense only with devid filter */
3033 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3034 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3035 return 0;
3036 }
3037
Ilya Dryomovea671762012-01-16 22:04:48 +02003038 /* vrange filter */
3039 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3040 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3041 return 0;
3042 }
3043
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003044 /* soft profile changing mode */
3045 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3046 chunk_soft_convert_filter(chunk_type, bargs)) {
3047 return 0;
3048 }
3049
David Sterba7d824b62014-05-07 17:37:51 +02003050 /*
3051 * limited by count, must be the last filter
3052 */
3053 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3054 if (bargs->limit == 0)
3055 return 0;
3056 else
3057 bargs->limit--;
3058 }
3059
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003060 return 1;
3061}
3062
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003063static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003064{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003065 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003066 struct btrfs_root *chunk_root = fs_info->chunk_root;
3067 struct btrfs_root *dev_root = fs_info->dev_root;
3068 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003069 struct btrfs_device *device;
3070 u64 old_size;
3071 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003072 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003073 struct btrfs_path *path;
3074 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003075 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003076 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003077 struct extent_buffer *leaf;
3078 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003079 int ret;
3080 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003081 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003082 u64 limit_data = bctl->data.limit;
3083 u64 limit_meta = bctl->meta.limit;
3084 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003085
Chris Masonec44a352008-04-28 15:29:52 -04003086 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003087 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003088 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04003089 old_size = device->total_bytes;
3090 size_to_free = div_factor(old_size, 1);
3091 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003092 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003093 device->total_bytes - device->bytes_used > size_to_free ||
3094 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003095 continue;
3096
3097 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003098 if (ret == -ENOSPC)
3099 break;
Chris Masonec44a352008-04-28 15:29:52 -04003100 BUG_ON(ret);
3101
Yan, Zhenga22285a2010-05-16 10:48:46 -04003102 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003103 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003104
3105 ret = btrfs_grow_device(trans, device, old_size);
3106 BUG_ON(ret);
3107
3108 btrfs_end_transaction(trans, dev_root);
3109 }
3110
3111 /* step two, relocate all the chunks */
3112 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003113 if (!path) {
3114 ret = -ENOMEM;
3115 goto error;
3116 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003117
3118 /* zero out stat counters */
3119 spin_lock(&fs_info->balance_lock);
3120 memset(&bctl->stat, 0, sizeof(bctl->stat));
3121 spin_unlock(&fs_info->balance_lock);
3122again:
David Sterba7d824b62014-05-07 17:37:51 +02003123 if (!counting) {
3124 bctl->data.limit = limit_data;
3125 bctl->meta.limit = limit_meta;
3126 bctl->sys.limit = limit_sys;
3127 }
Chris Masonec44a352008-04-28 15:29:52 -04003128 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3129 key.offset = (u64)-1;
3130 key.type = BTRFS_CHUNK_ITEM_KEY;
3131
Chris Masond3977122009-01-05 21:25:51 -05003132 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003133 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003134 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003135 ret = -ECANCELED;
3136 goto error;
3137 }
3138
Chris Masonec44a352008-04-28 15:29:52 -04003139 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3140 if (ret < 0)
3141 goto error;
3142
3143 /*
3144 * this shouldn't happen, it means the last relocate
3145 * failed
3146 */
3147 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003148 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003149
3150 ret = btrfs_previous_item(chunk_root, path, 0,
3151 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003152 if (ret) {
3153 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003154 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003155 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003156
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003157 leaf = path->nodes[0];
3158 slot = path->slots[0];
3159 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3160
Chris Masonec44a352008-04-28 15:29:52 -04003161 if (found_key.objectid != key.objectid)
3162 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003163
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003164 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3165
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003166 if (!counting) {
3167 spin_lock(&fs_info->balance_lock);
3168 bctl->stat.considered++;
3169 spin_unlock(&fs_info->balance_lock);
3170 }
3171
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003172 ret = should_balance_chunk(chunk_root, leaf, chunk,
3173 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003174 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003175 if (!ret)
3176 goto loop;
3177
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003178 if (counting) {
3179 spin_lock(&fs_info->balance_lock);
3180 bctl->stat.expected++;
3181 spin_unlock(&fs_info->balance_lock);
3182 goto loop;
3183 }
3184
Chris Masonec44a352008-04-28 15:29:52 -04003185 ret = btrfs_relocate_chunk(chunk_root,
3186 chunk_root->root_key.objectid,
3187 found_key.objectid,
3188 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003189 if (ret && ret != -ENOSPC)
3190 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003191 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003192 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003193 } else {
3194 spin_lock(&fs_info->balance_lock);
3195 bctl->stat.completed++;
3196 spin_unlock(&fs_info->balance_lock);
3197 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003198loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003199 if (found_key.offset == 0)
3200 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003201 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003202 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003203
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003204 if (counting) {
3205 btrfs_release_path(path);
3206 counting = false;
3207 goto again;
3208 }
Chris Masonec44a352008-04-28 15:29:52 -04003209error:
3210 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003211 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003212 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003213 enospc_errors);
3214 if (!ret)
3215 ret = -ENOSPC;
3216 }
3217
Chris Masonec44a352008-04-28 15:29:52 -04003218 return ret;
3219}
3220
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003221/**
3222 * alloc_profile_is_valid - see if a given profile is valid and reduced
3223 * @flags: profile to validate
3224 * @extended: if true @flags is treated as an extended profile
3225 */
3226static int alloc_profile_is_valid(u64 flags, int extended)
3227{
3228 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3229 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3230
3231 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3232
3233 /* 1) check that all other bits are zeroed */
3234 if (flags & ~mask)
3235 return 0;
3236
3237 /* 2) see if profile is reduced */
3238 if (flags == 0)
3239 return !extended; /* "0" is valid for usual profiles */
3240
3241 /* true if exactly one bit set */
3242 return (flags & (flags - 1)) == 0;
3243}
3244
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003245static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3246{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003247 /* cancel requested || normal exit path */
3248 return atomic_read(&fs_info->balance_cancel_req) ||
3249 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3250 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003251}
3252
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003253static void __cancel_balance(struct btrfs_fs_info *fs_info)
3254{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003255 int ret;
3256
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003257 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003258 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003259 if (ret)
3260 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003261
3262 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003263}
3264
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003265/*
3266 * Should be called with both balance and volume mutexes held
3267 */
3268int btrfs_balance(struct btrfs_balance_control *bctl,
3269 struct btrfs_ioctl_balance_args *bargs)
3270{
3271 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003272 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003273 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003274 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003275 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003276 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003277
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003278 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003279 atomic_read(&fs_info->balance_pause_req) ||
3280 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003281 ret = -EINVAL;
3282 goto out;
3283 }
3284
Ilya Dryomove4837f82012-03-27 17:09:17 +03003285 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3286 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3287 mixed = 1;
3288
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003289 /*
3290 * In case of mixed groups both data and meta should be picked,
3291 * and identical options should be given for both of them.
3292 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003293 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3294 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003295 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3296 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3297 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003298 btrfs_err(fs_info, "with mixed groups data and "
3299 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003300 ret = -EINVAL;
3301 goto out;
3302 }
3303 }
3304
Stefan Behrens8dabb742012-11-06 13:15:27 +01003305 num_devices = fs_info->fs_devices->num_devices;
3306 btrfs_dev_replace_lock(&fs_info->dev_replace);
3307 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3308 BUG_ON(num_devices < 1);
3309 num_devices--;
3310 }
3311 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003312 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003313 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003314 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003315 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003316 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003317 if (num_devices > 2)
3318 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3319 if (num_devices > 3)
3320 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3321 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003322 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3323 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3324 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003325 btrfs_err(fs_info, "unable to start balance with target "
3326 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003327 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003328 ret = -EINVAL;
3329 goto out;
3330 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003331 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3332 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3333 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003334 btrfs_err(fs_info,
3335 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003336 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003337 ret = -EINVAL;
3338 goto out;
3339 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003340 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3341 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3342 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003343 btrfs_err(fs_info,
3344 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003345 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003346 ret = -EINVAL;
3347 goto out;
3348 }
3349
Ilya Dryomove4837f82012-03-27 17:09:17 +03003350 /* allow dup'ed data chunks only in mixed mode */
3351 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003352 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003353 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003354 ret = -EINVAL;
3355 goto out;
3356 }
3357
3358 /* allow to reduce meta or sys integrity only if force set */
3359 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003360 BTRFS_BLOCK_GROUP_RAID10 |
3361 BTRFS_BLOCK_GROUP_RAID5 |
3362 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003363 do {
3364 seq = read_seqbegin(&fs_info->profiles_lock);
3365
3366 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3367 (fs_info->avail_system_alloc_bits & allowed) &&
3368 !(bctl->sys.target & allowed)) ||
3369 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3370 (fs_info->avail_metadata_alloc_bits & allowed) &&
3371 !(bctl->meta.target & allowed))) {
3372 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003373 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003374 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003375 btrfs_err(fs_info, "balance will reduce metadata "
3376 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003377 ret = -EINVAL;
3378 goto out;
3379 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003380 }
Miao Xiede98ced2013-01-29 10:13:12 +00003381 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003382
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003383 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3384 int num_tolerated_disk_barrier_failures;
3385 u64 target = bctl->sys.target;
3386
3387 num_tolerated_disk_barrier_failures =
3388 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3389 if (num_tolerated_disk_barrier_failures > 0 &&
3390 (target &
3391 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3392 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3393 num_tolerated_disk_barrier_failures = 0;
3394 else if (num_tolerated_disk_barrier_failures > 1 &&
3395 (target &
3396 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3397 num_tolerated_disk_barrier_failures = 1;
3398
3399 fs_info->num_tolerated_disk_barrier_failures =
3400 num_tolerated_disk_barrier_failures;
3401 }
3402
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003403 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003404 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003405 goto out;
3406
Ilya Dryomov59641012012-01-16 22:04:48 +02003407 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3408 BUG_ON(ret == -EEXIST);
3409 set_balance_control(bctl);
3410 } else {
3411 BUG_ON(ret != -EEXIST);
3412 spin_lock(&fs_info->balance_lock);
3413 update_balance_args(bctl);
3414 spin_unlock(&fs_info->balance_lock);
3415 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003416
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003417 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003418 mutex_unlock(&fs_info->balance_mutex);
3419
3420 ret = __btrfs_balance(fs_info);
3421
3422 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003423 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003424
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003425 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3426 fs_info->num_tolerated_disk_barrier_failures =
3427 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3428 }
3429
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003430 if (bargs) {
3431 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003432 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003433 }
3434
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003435 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3436 balance_need_close(fs_info)) {
3437 __cancel_balance(fs_info);
3438 }
3439
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003440 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003441
3442 return ret;
3443out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003444 if (bctl->flags & BTRFS_BALANCE_RESUME)
3445 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003446 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003447 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003448 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3449 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003450 return ret;
3451}
3452
3453static int balance_kthread(void *data)
3454{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003455 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003456 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003457
3458 mutex_lock(&fs_info->volume_mutex);
3459 mutex_lock(&fs_info->balance_mutex);
3460
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003461 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003462 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003463 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003464 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003465
3466 mutex_unlock(&fs_info->balance_mutex);
3467 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003468
Ilya Dryomov59641012012-01-16 22:04:48 +02003469 return ret;
3470}
3471
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003472int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3473{
3474 struct task_struct *tsk;
3475
3476 spin_lock(&fs_info->balance_lock);
3477 if (!fs_info->balance_ctl) {
3478 spin_unlock(&fs_info->balance_lock);
3479 return 0;
3480 }
3481 spin_unlock(&fs_info->balance_lock);
3482
3483 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003484 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003485 return 0;
3486 }
3487
3488 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303489 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003490}
3491
Ilya Dryomov68310a52012-06-22 12:24:12 -06003492int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003493{
Ilya Dryomov59641012012-01-16 22:04:48 +02003494 struct btrfs_balance_control *bctl;
3495 struct btrfs_balance_item *item;
3496 struct btrfs_disk_balance_args disk_bargs;
3497 struct btrfs_path *path;
3498 struct extent_buffer *leaf;
3499 struct btrfs_key key;
3500 int ret;
3501
3502 path = btrfs_alloc_path();
3503 if (!path)
3504 return -ENOMEM;
3505
Ilya Dryomov68310a52012-06-22 12:24:12 -06003506 key.objectid = BTRFS_BALANCE_OBJECTID;
3507 key.type = BTRFS_BALANCE_ITEM_KEY;
3508 key.offset = 0;
3509
3510 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3511 if (ret < 0)
3512 goto out;
3513 if (ret > 0) { /* ret = -ENOENT; */
3514 ret = 0;
3515 goto out;
3516 }
3517
Ilya Dryomov59641012012-01-16 22:04:48 +02003518 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3519 if (!bctl) {
3520 ret = -ENOMEM;
3521 goto out;
3522 }
3523
Ilya Dryomov59641012012-01-16 22:04:48 +02003524 leaf = path->nodes[0];
3525 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3526
Ilya Dryomov68310a52012-06-22 12:24:12 -06003527 bctl->fs_info = fs_info;
3528 bctl->flags = btrfs_balance_flags(leaf, item);
3529 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003530
3531 btrfs_balance_data(leaf, item, &disk_bargs);
3532 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3533 btrfs_balance_meta(leaf, item, &disk_bargs);
3534 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3535 btrfs_balance_sys(leaf, item, &disk_bargs);
3536 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3537
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003538 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3539
Ilya Dryomov68310a52012-06-22 12:24:12 -06003540 mutex_lock(&fs_info->volume_mutex);
3541 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003542
Ilya Dryomov68310a52012-06-22 12:24:12 -06003543 set_balance_control(bctl);
3544
3545 mutex_unlock(&fs_info->balance_mutex);
3546 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003547out:
3548 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003549 return ret;
3550}
3551
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003552int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3553{
3554 int ret = 0;
3555
3556 mutex_lock(&fs_info->balance_mutex);
3557 if (!fs_info->balance_ctl) {
3558 mutex_unlock(&fs_info->balance_mutex);
3559 return -ENOTCONN;
3560 }
3561
3562 if (atomic_read(&fs_info->balance_running)) {
3563 atomic_inc(&fs_info->balance_pause_req);
3564 mutex_unlock(&fs_info->balance_mutex);
3565
3566 wait_event(fs_info->balance_wait_q,
3567 atomic_read(&fs_info->balance_running) == 0);
3568
3569 mutex_lock(&fs_info->balance_mutex);
3570 /* we are good with balance_ctl ripped off from under us */
3571 BUG_ON(atomic_read(&fs_info->balance_running));
3572 atomic_dec(&fs_info->balance_pause_req);
3573 } else {
3574 ret = -ENOTCONN;
3575 }
3576
3577 mutex_unlock(&fs_info->balance_mutex);
3578 return ret;
3579}
3580
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003581int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3582{
Ilya Dryomove649e582013-10-10 20:40:21 +03003583 if (fs_info->sb->s_flags & MS_RDONLY)
3584 return -EROFS;
3585
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003586 mutex_lock(&fs_info->balance_mutex);
3587 if (!fs_info->balance_ctl) {
3588 mutex_unlock(&fs_info->balance_mutex);
3589 return -ENOTCONN;
3590 }
3591
3592 atomic_inc(&fs_info->balance_cancel_req);
3593 /*
3594 * if we are running just wait and return, balance item is
3595 * deleted in btrfs_balance in this case
3596 */
3597 if (atomic_read(&fs_info->balance_running)) {
3598 mutex_unlock(&fs_info->balance_mutex);
3599 wait_event(fs_info->balance_wait_q,
3600 atomic_read(&fs_info->balance_running) == 0);
3601 mutex_lock(&fs_info->balance_mutex);
3602 } else {
3603 /* __cancel_balance needs volume_mutex */
3604 mutex_unlock(&fs_info->balance_mutex);
3605 mutex_lock(&fs_info->volume_mutex);
3606 mutex_lock(&fs_info->balance_mutex);
3607
3608 if (fs_info->balance_ctl)
3609 __cancel_balance(fs_info);
3610
3611 mutex_unlock(&fs_info->volume_mutex);
3612 }
3613
3614 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3615 atomic_dec(&fs_info->balance_cancel_req);
3616 mutex_unlock(&fs_info->balance_mutex);
3617 return 0;
3618}
3619
Stefan Behrens803b2f52013-08-15 17:11:21 +02003620static int btrfs_uuid_scan_kthread(void *data)
3621{
3622 struct btrfs_fs_info *fs_info = data;
3623 struct btrfs_root *root = fs_info->tree_root;
3624 struct btrfs_key key;
3625 struct btrfs_key max_key;
3626 struct btrfs_path *path = NULL;
3627 int ret = 0;
3628 struct extent_buffer *eb;
3629 int slot;
3630 struct btrfs_root_item root_item;
3631 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003632 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003633
3634 path = btrfs_alloc_path();
3635 if (!path) {
3636 ret = -ENOMEM;
3637 goto out;
3638 }
3639
3640 key.objectid = 0;
3641 key.type = BTRFS_ROOT_ITEM_KEY;
3642 key.offset = 0;
3643
3644 max_key.objectid = (u64)-1;
3645 max_key.type = BTRFS_ROOT_ITEM_KEY;
3646 max_key.offset = (u64)-1;
3647
3648 path->keep_locks = 1;
3649
3650 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003651 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003652 if (ret) {
3653 if (ret > 0)
3654 ret = 0;
3655 break;
3656 }
3657
3658 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3659 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3660 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3661 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3662 goto skip;
3663
3664 eb = path->nodes[0];
3665 slot = path->slots[0];
3666 item_size = btrfs_item_size_nr(eb, slot);
3667 if (item_size < sizeof(root_item))
3668 goto skip;
3669
Stefan Behrens803b2f52013-08-15 17:11:21 +02003670 read_extent_buffer(eb, &root_item,
3671 btrfs_item_ptr_offset(eb, slot),
3672 (int)sizeof(root_item));
3673 if (btrfs_root_refs(&root_item) == 0)
3674 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003675
3676 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3677 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3678 if (trans)
3679 goto update_tree;
3680
3681 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003682 /*
3683 * 1 - subvol uuid item
3684 * 1 - received_subvol uuid item
3685 */
3686 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3687 if (IS_ERR(trans)) {
3688 ret = PTR_ERR(trans);
3689 break;
3690 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003691 continue;
3692 } else {
3693 goto skip;
3694 }
3695update_tree:
3696 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003697 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3698 root_item.uuid,
3699 BTRFS_UUID_KEY_SUBVOL,
3700 key.objectid);
3701 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003702 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003703 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003704 break;
3705 }
3706 }
3707
3708 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003709 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3710 root_item.received_uuid,
3711 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3712 key.objectid);
3713 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003714 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003715 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003716 break;
3717 }
3718 }
3719
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003720skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003721 if (trans) {
3722 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003723 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003724 if (ret)
3725 break;
3726 }
3727
Stefan Behrens803b2f52013-08-15 17:11:21 +02003728 btrfs_release_path(path);
3729 if (key.offset < (u64)-1) {
3730 key.offset++;
3731 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3732 key.offset = 0;
3733 key.type = BTRFS_ROOT_ITEM_KEY;
3734 } else if (key.objectid < (u64)-1) {
3735 key.offset = 0;
3736 key.type = BTRFS_ROOT_ITEM_KEY;
3737 key.objectid++;
3738 } else {
3739 break;
3740 }
3741 cond_resched();
3742 }
3743
3744out:
3745 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003746 if (trans && !IS_ERR(trans))
3747 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003748 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003749 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003750 else
3751 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003752 up(&fs_info->uuid_tree_rescan_sem);
3753 return 0;
3754}
3755
Stefan Behrens70f80172013-08-15 17:11:23 +02003756/*
3757 * Callback for btrfs_uuid_tree_iterate().
3758 * returns:
3759 * 0 check succeeded, the entry is not outdated.
3760 * < 0 if an error occured.
3761 * > 0 if the check failed, which means the caller shall remove the entry.
3762 */
3763static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3764 u8 *uuid, u8 type, u64 subid)
3765{
3766 struct btrfs_key key;
3767 int ret = 0;
3768 struct btrfs_root *subvol_root;
3769
3770 if (type != BTRFS_UUID_KEY_SUBVOL &&
3771 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3772 goto out;
3773
3774 key.objectid = subid;
3775 key.type = BTRFS_ROOT_ITEM_KEY;
3776 key.offset = (u64)-1;
3777 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3778 if (IS_ERR(subvol_root)) {
3779 ret = PTR_ERR(subvol_root);
3780 if (ret == -ENOENT)
3781 ret = 1;
3782 goto out;
3783 }
3784
3785 switch (type) {
3786 case BTRFS_UUID_KEY_SUBVOL:
3787 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3788 ret = 1;
3789 break;
3790 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3791 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3792 BTRFS_UUID_SIZE))
3793 ret = 1;
3794 break;
3795 }
3796
3797out:
3798 return ret;
3799}
3800
3801static int btrfs_uuid_rescan_kthread(void *data)
3802{
3803 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3804 int ret;
3805
3806 /*
3807 * 1st step is to iterate through the existing UUID tree and
3808 * to delete all entries that contain outdated data.
3809 * 2nd step is to add all missing entries to the UUID tree.
3810 */
3811 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3812 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003813 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003814 up(&fs_info->uuid_tree_rescan_sem);
3815 return ret;
3816 }
3817 return btrfs_uuid_scan_kthread(data);
3818}
3819
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003820int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3821{
3822 struct btrfs_trans_handle *trans;
3823 struct btrfs_root *tree_root = fs_info->tree_root;
3824 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003825 struct task_struct *task;
3826 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003827
3828 /*
3829 * 1 - root node
3830 * 1 - root item
3831 */
3832 trans = btrfs_start_transaction(tree_root, 2);
3833 if (IS_ERR(trans))
3834 return PTR_ERR(trans);
3835
3836 uuid_root = btrfs_create_tree(trans, fs_info,
3837 BTRFS_UUID_TREE_OBJECTID);
3838 if (IS_ERR(uuid_root)) {
3839 btrfs_abort_transaction(trans, tree_root,
3840 PTR_ERR(uuid_root));
3841 return PTR_ERR(uuid_root);
3842 }
3843
3844 fs_info->uuid_root = uuid_root;
3845
Stefan Behrens803b2f52013-08-15 17:11:21 +02003846 ret = btrfs_commit_transaction(trans, tree_root);
3847 if (ret)
3848 return ret;
3849
3850 down(&fs_info->uuid_tree_rescan_sem);
3851 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3852 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003853 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003854 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003855 up(&fs_info->uuid_tree_rescan_sem);
3856 return PTR_ERR(task);
3857 }
3858
3859 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003860}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003861
Stefan Behrens70f80172013-08-15 17:11:23 +02003862int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3863{
3864 struct task_struct *task;
3865
3866 down(&fs_info->uuid_tree_rescan_sem);
3867 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3868 if (IS_ERR(task)) {
3869 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003870 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003871 up(&fs_info->uuid_tree_rescan_sem);
3872 return PTR_ERR(task);
3873 }
3874
3875 return 0;
3876}
3877
Chris Mason8f18cf12008-04-25 16:53:30 -04003878/*
3879 * shrinking a device means finding all of the device extents past
3880 * the new size, and then following the back refs to the chunks.
3881 * The chunk relocation code actually frees the device extent
3882 */
3883int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3884{
3885 struct btrfs_trans_handle *trans;
3886 struct btrfs_root *root = device->dev_root;
3887 struct btrfs_dev_extent *dev_extent = NULL;
3888 struct btrfs_path *path;
3889 u64 length;
3890 u64 chunk_tree;
3891 u64 chunk_objectid;
3892 u64 chunk_offset;
3893 int ret;
3894 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003895 int failed = 0;
3896 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003897 struct extent_buffer *l;
3898 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003899 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003900 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003901 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003902 u64 diff = device->total_bytes - new_size;
3903
Stefan Behrens63a212a2012-11-05 18:29:28 +01003904 if (device->is_tgtdev_for_dev_replace)
3905 return -EINVAL;
3906
Chris Mason8f18cf12008-04-25 16:53:30 -04003907 path = btrfs_alloc_path();
3908 if (!path)
3909 return -ENOMEM;
3910
Chris Mason8f18cf12008-04-25 16:53:30 -04003911 path->reada = 2;
3912
Chris Mason7d9eb122008-07-08 14:19:17 -04003913 lock_chunks(root);
3914
Chris Mason8f18cf12008-04-25 16:53:30 -04003915 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003916 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003917 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003918 spin_lock(&root->fs_info->free_chunk_lock);
3919 root->fs_info->free_chunk_space -= diff;
3920 spin_unlock(&root->fs_info->free_chunk_lock);
3921 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003922 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003923
Josef Bacikba1bf482009-09-11 16:11:19 -04003924again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003925 key.objectid = device->devid;
3926 key.offset = (u64)-1;
3927 key.type = BTRFS_DEV_EXTENT_KEY;
3928
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003929 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003930 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3931 if (ret < 0)
3932 goto done;
3933
3934 ret = btrfs_previous_item(root, path, 0, key.type);
3935 if (ret < 0)
3936 goto done;
3937 if (ret) {
3938 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003939 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003940 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003941 }
3942
3943 l = path->nodes[0];
3944 slot = path->slots[0];
3945 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3946
Josef Bacikba1bf482009-09-11 16:11:19 -04003947 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003948 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003949 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003950 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003951
3952 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3953 length = btrfs_dev_extent_length(l, dev_extent);
3954
Josef Bacikba1bf482009-09-11 16:11:19 -04003955 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003956 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003957 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003958 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003959
3960 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3961 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3962 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003963 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003964
3965 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3966 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003967 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003968 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003969 if (ret == -ENOSPC)
3970 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003971 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003972
3973 if (failed && !retried) {
3974 failed = 0;
3975 retried = true;
3976 goto again;
3977 } else if (failed && retried) {
3978 ret = -ENOSPC;
3979 lock_chunks(root);
3980
3981 device->total_bytes = old_size;
3982 if (device->writeable)
3983 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003984 spin_lock(&root->fs_info->free_chunk_lock);
3985 root->fs_info->free_chunk_space += diff;
3986 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003987 unlock_chunks(root);
3988 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003989 }
3990
Chris Balld6397ba2009-04-27 07:29:03 -04003991 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003992 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003993 if (IS_ERR(trans)) {
3994 ret = PTR_ERR(trans);
3995 goto done;
3996 }
3997
Chris Balld6397ba2009-04-27 07:29:03 -04003998 lock_chunks(root);
3999
4000 device->disk_total_bytes = new_size;
4001 /* Now btrfs_update_device() will change the on-disk size. */
4002 ret = btrfs_update_device(trans, device);
4003 if (ret) {
4004 unlock_chunks(root);
4005 btrfs_end_transaction(trans, root);
4006 goto done;
4007 }
4008 WARN_ON(diff > old_total);
4009 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4010 unlock_chunks(root);
4011 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004012done:
4013 btrfs_free_path(path);
4014 return ret;
4015}
4016
Li Zefan125ccb02011-12-08 15:07:24 +08004017static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004018 struct btrfs_key *key,
4019 struct btrfs_chunk *chunk, int item_size)
4020{
David Sterba6c417612011-04-13 15:41:04 +02004021 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004022 struct btrfs_disk_key disk_key;
4023 u32 array_size;
4024 u8 *ptr;
4025
4026 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004027 if (array_size + item_size + sizeof(disk_key)
4028 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
Chris Mason0b86a832008-03-24 15:01:56 -04004029 return -EFBIG;
4030
4031 ptr = super_copy->sys_chunk_array + array_size;
4032 btrfs_cpu_key_to_disk(&disk_key, key);
4033 memcpy(ptr, &disk_key, sizeof(disk_key));
4034 ptr += sizeof(disk_key);
4035 memcpy(ptr, chunk, item_size);
4036 item_size += sizeof(disk_key);
4037 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4038 return 0;
4039}
4040
Miao Xieb2117a32011-01-05 10:07:28 +00004041/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004042 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004043 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004044static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004045{
Arne Jansen73c5de02011-04-12 12:07:57 +02004046 const struct btrfs_device_info *di_a = a;
4047 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004048
Arne Jansen73c5de02011-04-12 12:07:57 +02004049 if (di_a->max_avail > di_b->max_avail)
4050 return -1;
4051 if (di_a->max_avail < di_b->max_avail)
4052 return 1;
4053 if (di_a->total_avail > di_b->total_avail)
4054 return -1;
4055 if (di_a->total_avail < di_b->total_avail)
4056 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004057 return 0;
4058}
4059
Eric Sandeen48a3b632013-04-25 20:41:01 +00004060static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004061 [BTRFS_RAID_RAID10] = {
4062 .sub_stripes = 2,
4063 .dev_stripes = 1,
4064 .devs_max = 0, /* 0 == as many as possible */
4065 .devs_min = 4,
4066 .devs_increment = 2,
4067 .ncopies = 2,
4068 },
4069 [BTRFS_RAID_RAID1] = {
4070 .sub_stripes = 1,
4071 .dev_stripes = 1,
4072 .devs_max = 2,
4073 .devs_min = 2,
4074 .devs_increment = 2,
4075 .ncopies = 2,
4076 },
4077 [BTRFS_RAID_DUP] = {
4078 .sub_stripes = 1,
4079 .dev_stripes = 2,
4080 .devs_max = 1,
4081 .devs_min = 1,
4082 .devs_increment = 1,
4083 .ncopies = 2,
4084 },
4085 [BTRFS_RAID_RAID0] = {
4086 .sub_stripes = 1,
4087 .dev_stripes = 1,
4088 .devs_max = 0,
4089 .devs_min = 2,
4090 .devs_increment = 1,
4091 .ncopies = 1,
4092 },
4093 [BTRFS_RAID_SINGLE] = {
4094 .sub_stripes = 1,
4095 .dev_stripes = 1,
4096 .devs_max = 1,
4097 .devs_min = 1,
4098 .devs_increment = 1,
4099 .ncopies = 1,
4100 },
Chris Masone942f882013-02-20 14:06:05 -05004101 [BTRFS_RAID_RAID5] = {
4102 .sub_stripes = 1,
4103 .dev_stripes = 1,
4104 .devs_max = 0,
4105 .devs_min = 2,
4106 .devs_increment = 1,
4107 .ncopies = 2,
4108 },
4109 [BTRFS_RAID_RAID6] = {
4110 .sub_stripes = 1,
4111 .dev_stripes = 1,
4112 .devs_max = 0,
4113 .devs_min = 3,
4114 .devs_increment = 1,
4115 .ncopies = 3,
4116 },
Liu Bo31e50222012-11-21 14:18:10 +00004117};
4118
David Woodhouse53b381b2013-01-29 18:40:14 -05004119static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4120{
4121 /* TODO allow them to set a preferred stripe size */
4122 return 64 * 1024;
4123}
4124
4125static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4126{
David Woodhouse53b381b2013-01-29 18:40:14 -05004127 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
4128 return;
4129
Miao Xieceda0862013-04-11 10:30:16 +00004130 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004131}
4132
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004133#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4134 - sizeof(struct btrfs_item) \
4135 - sizeof(struct btrfs_chunk)) \
4136 / sizeof(struct btrfs_stripe) + 1)
4137
4138#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4139 - 2 * sizeof(struct btrfs_disk_key) \
4140 - 2 * sizeof(struct btrfs_chunk)) \
4141 / sizeof(struct btrfs_stripe) + 1)
4142
Miao Xieb2117a32011-01-05 10:07:28 +00004143static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004144 struct btrfs_root *extent_root, u64 start,
4145 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004146{
4147 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004148 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4149 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004150 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004151 struct extent_map_tree *em_tree;
4152 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004153 struct btrfs_device_info *devices_info = NULL;
4154 u64 total_avail;
4155 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004156 int data_stripes; /* number of stripes that count for
4157 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004158 int sub_stripes; /* sub_stripes info for map */
4159 int dev_stripes; /* stripes per dev */
4160 int devs_max; /* max devs to use */
4161 int devs_min; /* min devs needed */
4162 int devs_increment; /* ndevs has to be a multiple of this */
4163 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004164 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004165 u64 max_stripe_size;
4166 u64 max_chunk_size;
4167 u64 stripe_size;
4168 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004169 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004170 int ndevs;
4171 int i;
4172 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004173 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004174
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004175 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004176
Miao Xieb2117a32011-01-05 10:07:28 +00004177 if (list_empty(&fs_devices->alloc_list))
4178 return -ENOSPC;
4179
Liu Bo31e50222012-11-21 14:18:10 +00004180 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004181
Liu Bo31e50222012-11-21 14:18:10 +00004182 sub_stripes = btrfs_raid_array[index].sub_stripes;
4183 dev_stripes = btrfs_raid_array[index].dev_stripes;
4184 devs_max = btrfs_raid_array[index].devs_max;
4185 devs_min = btrfs_raid_array[index].devs_min;
4186 devs_increment = btrfs_raid_array[index].devs_increment;
4187 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004188
4189 if (type & BTRFS_BLOCK_GROUP_DATA) {
4190 max_stripe_size = 1024 * 1024 * 1024;
4191 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004192 if (!devs_max)
4193 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004194 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004195 /* for larger filesystems, use larger metadata chunks */
4196 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4197 max_stripe_size = 1024 * 1024 * 1024;
4198 else
4199 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004200 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004201 if (!devs_max)
4202 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004203 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004204 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004205 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004206 if (!devs_max)
4207 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004208 } else {
David Sterba351fd352014-05-15 16:48:20 +02004209 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004210 type);
4211 BUG_ON(1);
4212 }
4213
4214 /* we don't want a chunk larger than 10% of writeable space */
4215 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4216 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004217
4218 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4219 GFP_NOFS);
4220 if (!devices_info)
4221 return -ENOMEM;
4222
Arne Jansen73c5de02011-04-12 12:07:57 +02004223 cur = fs_devices->alloc_list.next;
4224
4225 /*
4226 * in the first pass through the devices list, we gather information
4227 * about the available holes on each device.
4228 */
4229 ndevs = 0;
4230 while (cur != &fs_devices->alloc_list) {
4231 struct btrfs_device *device;
4232 u64 max_avail;
4233 u64 dev_offset;
4234
4235 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4236
4237 cur = cur->next;
4238
4239 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004240 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004241 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004242 continue;
4243 }
4244
Stefan Behrens63a212a2012-11-05 18:29:28 +01004245 if (!device->in_fs_metadata ||
4246 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004247 continue;
4248
4249 if (device->total_bytes > device->bytes_used)
4250 total_avail = device->total_bytes - device->bytes_used;
4251 else
4252 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004253
4254 /* If there is no space on this device, skip it. */
4255 if (total_avail == 0)
4256 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004257
Josef Bacik6df9a952013-06-27 13:22:46 -04004258 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004259 max_stripe_size * dev_stripes,
4260 &dev_offset, &max_avail);
4261 if (ret && ret != -ENOSPC)
4262 goto error;
4263
4264 if (ret == 0)
4265 max_avail = max_stripe_size * dev_stripes;
4266
4267 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4268 continue;
4269
Eric Sandeen063d0062013-01-31 00:55:01 +00004270 if (ndevs == fs_devices->rw_devices) {
4271 WARN(1, "%s: found more than %llu devices\n",
4272 __func__, fs_devices->rw_devices);
4273 break;
4274 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004275 devices_info[ndevs].dev_offset = dev_offset;
4276 devices_info[ndevs].max_avail = max_avail;
4277 devices_info[ndevs].total_avail = total_avail;
4278 devices_info[ndevs].dev = device;
4279 ++ndevs;
4280 }
4281
4282 /*
4283 * now sort the devices by hole size / available space
4284 */
4285 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4286 btrfs_cmp_device_info, NULL);
4287
4288 /* round down to number of usable stripes */
4289 ndevs -= ndevs % devs_increment;
4290
4291 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4292 ret = -ENOSPC;
4293 goto error;
4294 }
4295
4296 if (devs_max && ndevs > devs_max)
4297 ndevs = devs_max;
4298 /*
4299 * the primary goal is to maximize the number of stripes, so use as many
4300 * devices as possible, even if the stripes are not maximum sized.
4301 */
4302 stripe_size = devices_info[ndevs-1].max_avail;
4303 num_stripes = ndevs * dev_stripes;
4304
David Woodhouse53b381b2013-01-29 18:40:14 -05004305 /*
4306 * this will have to be fixed for RAID1 and RAID10 over
4307 * more drives
4308 */
4309 data_stripes = num_stripes / ncopies;
4310
David Woodhouse53b381b2013-01-29 18:40:14 -05004311 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4312 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4313 btrfs_super_stripesize(info->super_copy));
4314 data_stripes = num_stripes - 1;
4315 }
4316 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4317 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4318 btrfs_super_stripesize(info->super_copy));
4319 data_stripes = num_stripes - 2;
4320 }
Chris Mason86db2572013-02-20 16:23:40 -05004321
4322 /*
4323 * Use the number of data stripes to figure out how big this chunk
4324 * is really going to be in terms of logical address space,
4325 * and compare that answer with the max chunk size
4326 */
4327 if (stripe_size * data_stripes > max_chunk_size) {
4328 u64 mask = (1ULL << 24) - 1;
4329 stripe_size = max_chunk_size;
4330 do_div(stripe_size, data_stripes);
4331
4332 /* bump the answer up to a 16MB boundary */
4333 stripe_size = (stripe_size + mask) & ~mask;
4334
4335 /* but don't go higher than the limits we found
4336 * while searching for free extents
4337 */
4338 if (stripe_size > devices_info[ndevs-1].max_avail)
4339 stripe_size = devices_info[ndevs-1].max_avail;
4340 }
4341
Arne Jansen73c5de02011-04-12 12:07:57 +02004342 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004343
4344 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004345 do_div(stripe_size, raid_stripe_len);
4346 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004347
Miao Xieb2117a32011-01-05 10:07:28 +00004348 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4349 if (!map) {
4350 ret = -ENOMEM;
4351 goto error;
4352 }
4353 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004354
Arne Jansen73c5de02011-04-12 12:07:57 +02004355 for (i = 0; i < ndevs; ++i) {
4356 for (j = 0; j < dev_stripes; ++j) {
4357 int s = i * dev_stripes + j;
4358 map->stripes[s].dev = devices_info[i].dev;
4359 map->stripes[s].physical = devices_info[i].dev_offset +
4360 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004361 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004362 }
Chris Mason593060d2008-03-25 16:50:33 -04004363 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004364 map->stripe_len = raid_stripe_len;
4365 map->io_align = raid_stripe_len;
4366 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004367 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004368 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004369
David Woodhouse53b381b2013-01-29 18:40:14 -05004370 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004371
Arne Jansen73c5de02011-04-12 12:07:57 +02004372 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004373
David Sterba172ddd62011-04-21 00:48:27 +02004374 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004375 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004376 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004377 ret = -ENOMEM;
4378 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004379 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004380 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004381 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004382 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004383 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004384 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004385 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004386 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004387
Chris Mason0b86a832008-03-24 15:01:56 -04004388 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004389 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004390 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004391 if (!ret) {
4392 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4393 atomic_inc(&em->refs);
4394 }
Chris Mason890871b2009-09-02 16:24:52 -04004395 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004396 if (ret) {
4397 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004398 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004399 }
Yan Zheng2b820322008-11-17 21:11:30 -05004400
Josef Bacik04487482013-01-29 15:03:37 -05004401 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4402 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4403 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004404 if (ret)
4405 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004406
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004407 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004408 check_raid56_incompat_flag(extent_root->fs_info, type);
4409
Miao Xieb2117a32011-01-05 10:07:28 +00004410 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004411 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004412
Josef Bacik6df9a952013-06-27 13:22:46 -04004413error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004414 write_lock(&em_tree->lock);
4415 remove_extent_mapping(em_tree, em);
4416 write_unlock(&em_tree->lock);
4417
4418 /* One for our allocation */
4419 free_extent_map(em);
4420 /* One for the tree reference */
4421 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004422error:
Miao Xieb2117a32011-01-05 10:07:28 +00004423 kfree(devices_info);
4424 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004425}
4426
Josef Bacik6df9a952013-06-27 13:22:46 -04004427int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004428 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004429 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004430{
Yan Zheng2b820322008-11-17 21:11:30 -05004431 struct btrfs_key key;
4432 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4433 struct btrfs_device *device;
4434 struct btrfs_chunk *chunk;
4435 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004436 struct extent_map_tree *em_tree;
4437 struct extent_map *em;
4438 struct map_lookup *map;
4439 size_t item_size;
4440 u64 dev_offset;
4441 u64 stripe_size;
4442 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004443 int ret;
4444
Josef Bacik6df9a952013-06-27 13:22:46 -04004445 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4446 read_lock(&em_tree->lock);
4447 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4448 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004449
Josef Bacik6df9a952013-06-27 13:22:46 -04004450 if (!em) {
4451 btrfs_crit(extent_root->fs_info, "unable to find logical "
4452 "%Lu len %Lu", chunk_offset, chunk_size);
4453 return -EINVAL;
4454 }
4455
4456 if (em->start != chunk_offset || em->len != chunk_size) {
4457 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004458 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004459 chunk_size, em->start, em->len);
4460 free_extent_map(em);
4461 return -EINVAL;
4462 }
4463
4464 map = (struct map_lookup *)em->bdev;
4465 item_size = btrfs_chunk_item_size(map->num_stripes);
4466 stripe_size = em->orig_block_len;
4467
4468 chunk = kzalloc(item_size, GFP_NOFS);
4469 if (!chunk) {
4470 ret = -ENOMEM;
4471 goto out;
4472 }
4473
4474 for (i = 0; i < map->num_stripes; i++) {
4475 device = map->stripes[i].dev;
4476 dev_offset = map->stripes[i].physical;
4477
Yan Zheng2b820322008-11-17 21:11:30 -05004478 device->bytes_used += stripe_size;
4479 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004480 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004481 goto out;
4482 ret = btrfs_alloc_dev_extent(trans, device,
4483 chunk_root->root_key.objectid,
4484 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4485 chunk_offset, dev_offset,
4486 stripe_size);
4487 if (ret)
4488 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004489 }
4490
Josef Bacik2bf64752011-09-26 17:12:22 -04004491 spin_lock(&extent_root->fs_info->free_chunk_lock);
4492 extent_root->fs_info->free_chunk_space -= (stripe_size *
4493 map->num_stripes);
4494 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4495
Yan Zheng2b820322008-11-17 21:11:30 -05004496 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004497 for (i = 0; i < map->num_stripes; i++) {
4498 device = map->stripes[i].dev;
4499 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004500
4501 btrfs_set_stack_stripe_devid(stripe, device->devid);
4502 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4503 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4504 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004505 }
4506
4507 btrfs_set_stack_chunk_length(chunk, chunk_size);
4508 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4509 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4510 btrfs_set_stack_chunk_type(chunk, map->type);
4511 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4512 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4513 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4514 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4515 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4516
4517 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4518 key.type = BTRFS_CHUNK_ITEM_KEY;
4519 key.offset = chunk_offset;
4520
4521 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004522 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4523 /*
4524 * TODO: Cleanup of inserted chunk root in case of
4525 * failure.
4526 */
Li Zefan125ccb02011-12-08 15:07:24 +08004527 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004528 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004529 }
liubo1abe9b82011-03-24 11:18:59 +00004530
Josef Bacik6df9a952013-06-27 13:22:46 -04004531out:
Yan Zheng2b820322008-11-17 21:11:30 -05004532 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004533 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004534 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004535}
4536
4537/*
4538 * Chunk allocation falls into two parts. The first part does works
4539 * that make the new allocated chunk useable, but not do any operation
4540 * that modifies the chunk tree. The second part does the works that
4541 * require modifying the chunk tree. This division is important for the
4542 * bootstrap process of adding storage to a seed btrfs.
4543 */
4544int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4545 struct btrfs_root *extent_root, u64 type)
4546{
4547 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004548
Josef Bacik6df9a952013-06-27 13:22:46 -04004549 chunk_offset = find_next_chunk(extent_root->fs_info);
4550 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004551}
4552
Chris Masond3977122009-01-05 21:25:51 -05004553static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004554 struct btrfs_root *root,
4555 struct btrfs_device *device)
4556{
4557 u64 chunk_offset;
4558 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004559 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004560 struct btrfs_fs_info *fs_info = root->fs_info;
4561 struct btrfs_root *extent_root = fs_info->extent_root;
4562 int ret;
4563
Josef Bacik6df9a952013-06-27 13:22:46 -04004564 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004565 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004566 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4567 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004568 if (ret)
4569 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004570
Josef Bacik6df9a952013-06-27 13:22:46 -04004571 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004572 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004573 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4574 alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004575 if (ret) {
4576 btrfs_abort_transaction(trans, root, ret);
4577 goto out;
4578 }
Yan Zheng2b820322008-11-17 21:11:30 -05004579
4580 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004581 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004582 btrfs_abort_transaction(trans, root, ret);
David Sterba005d6422012-09-18 07:52:32 -06004583out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004584 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004585}
4586
Miao Xied20983b2014-07-03 18:22:13 +08004587static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4588{
4589 int max_errors;
4590
4591 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4592 BTRFS_BLOCK_GROUP_RAID10 |
4593 BTRFS_BLOCK_GROUP_RAID5 |
4594 BTRFS_BLOCK_GROUP_DUP)) {
4595 max_errors = 1;
4596 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4597 max_errors = 2;
4598 } else {
4599 max_errors = 0;
4600 }
4601
4602 return max_errors;
4603}
4604
Yan Zheng2b820322008-11-17 21:11:30 -05004605int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4606{
4607 struct extent_map *em;
4608 struct map_lookup *map;
4609 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4610 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004611 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004612 int i;
4613
Chris Mason890871b2009-09-02 16:24:52 -04004614 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004615 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004616 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004617 if (!em)
4618 return 1;
4619
4620 map = (struct map_lookup *)em->bdev;
4621 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004622 if (map->stripes[i].dev->missing) {
4623 miss_ndevs++;
4624 continue;
4625 }
4626
Yan Zheng2b820322008-11-17 21:11:30 -05004627 if (!map->stripes[i].dev->writeable) {
4628 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004629 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004630 }
4631 }
Miao Xied20983b2014-07-03 18:22:13 +08004632
4633 /*
4634 * If the number of missing devices is larger than max errors,
4635 * we can not write the data into that chunk successfully, so
4636 * set it readonly.
4637 */
4638 if (miss_ndevs > btrfs_chunk_max_errors(map))
4639 readonly = 1;
4640end:
Yan Zheng2b820322008-11-17 21:11:30 -05004641 free_extent_map(em);
4642 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004643}
4644
4645void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4646{
David Sterbaa8067e02011-04-21 00:34:43 +02004647 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004648}
4649
4650void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4651{
4652 struct extent_map *em;
4653
Chris Masond3977122009-01-05 21:25:51 -05004654 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004655 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004656 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4657 if (em)
4658 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004659 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004660 if (!em)
4661 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004662 /* once for us */
4663 free_extent_map(em);
4664 /* once for the tree */
4665 free_extent_map(em);
4666 }
4667}
4668
Stefan Behrens5d964052012-11-05 14:59:07 +01004669int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004670{
Stefan Behrens5d964052012-11-05 14:59:07 +01004671 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004672 struct extent_map *em;
4673 struct map_lookup *map;
4674 struct extent_map_tree *em_tree = &map_tree->map_tree;
4675 int ret;
4676
Chris Mason890871b2009-09-02 16:24:52 -04004677 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004678 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004679 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004680
Josef Bacikfb7669b2013-04-23 10:53:18 -04004681 /*
4682 * We could return errors for these cases, but that could get ugly and
4683 * we'd probably do the same thing which is just not do anything else
4684 * and exit, so return 1 so the callers don't try to use other copies.
4685 */
4686 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004687 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004688 logical+len);
4689 return 1;
4690 }
4691
4692 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004693 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004694 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004695 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004696 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004697 return 1;
4698 }
4699
Chris Masonf1885912008-04-09 16:28:12 -04004700 map = (struct map_lookup *)em->bdev;
4701 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4702 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004703 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4704 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004705 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4706 ret = 2;
4707 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4708 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004709 else
4710 ret = 1;
4711 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004712
4713 btrfs_dev_replace_lock(&fs_info->dev_replace);
4714 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4715 ret++;
4716 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4717
Chris Masonf1885912008-04-09 16:28:12 -04004718 return ret;
4719}
4720
David Woodhouse53b381b2013-01-29 18:40:14 -05004721unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4722 struct btrfs_mapping_tree *map_tree,
4723 u64 logical)
4724{
4725 struct extent_map *em;
4726 struct map_lookup *map;
4727 struct extent_map_tree *em_tree = &map_tree->map_tree;
4728 unsigned long len = root->sectorsize;
4729
4730 read_lock(&em_tree->lock);
4731 em = lookup_extent_mapping(em_tree, logical, len);
4732 read_unlock(&em_tree->lock);
4733 BUG_ON(!em);
4734
4735 BUG_ON(em->start > logical || em->start + em->len < logical);
4736 map = (struct map_lookup *)em->bdev;
4737 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4738 BTRFS_BLOCK_GROUP_RAID6)) {
4739 len = map->stripe_len * nr_data_stripes(map);
4740 }
4741 free_extent_map(em);
4742 return len;
4743}
4744
4745int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4746 u64 logical, u64 len, int mirror_num)
4747{
4748 struct extent_map *em;
4749 struct map_lookup *map;
4750 struct extent_map_tree *em_tree = &map_tree->map_tree;
4751 int ret = 0;
4752
4753 read_lock(&em_tree->lock);
4754 em = lookup_extent_mapping(em_tree, logical, len);
4755 read_unlock(&em_tree->lock);
4756 BUG_ON(!em);
4757
4758 BUG_ON(em->start > logical || em->start + em->len < logical);
4759 map = (struct map_lookup *)em->bdev;
4760 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4761 BTRFS_BLOCK_GROUP_RAID6))
4762 ret = 1;
4763 free_extent_map(em);
4764 return ret;
4765}
4766
Stefan Behrens30d98612012-11-06 14:52:18 +01004767static int find_live_mirror(struct btrfs_fs_info *fs_info,
4768 struct map_lookup *map, int first, int num,
4769 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004770{
4771 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004772 int tolerance;
4773 struct btrfs_device *srcdev;
4774
4775 if (dev_replace_is_ongoing &&
4776 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4777 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4778 srcdev = fs_info->dev_replace.srcdev;
4779 else
4780 srcdev = NULL;
4781
4782 /*
4783 * try to avoid the drive that is the source drive for a
4784 * dev-replace procedure, only choose it if no other non-missing
4785 * mirror is available
4786 */
4787 for (tolerance = 0; tolerance < 2; tolerance++) {
4788 if (map->stripes[optimal].dev->bdev &&
4789 (tolerance || map->stripes[optimal].dev != srcdev))
4790 return optimal;
4791 for (i = first; i < first + num; i++) {
4792 if (map->stripes[i].dev->bdev &&
4793 (tolerance || map->stripes[i].dev != srcdev))
4794 return i;
4795 }
Chris Masondfe25022008-05-13 13:46:40 -04004796 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004797
Chris Masondfe25022008-05-13 13:46:40 -04004798 /* we couldn't find one that doesn't fail. Just return something
4799 * and the io error handling code will clean up eventually
4800 */
4801 return optimal;
4802}
4803
David Woodhouse53b381b2013-01-29 18:40:14 -05004804static inline int parity_smaller(u64 a, u64 b)
4805{
4806 return a > b;
4807}
4808
4809/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4810static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4811{
4812 struct btrfs_bio_stripe s;
4813 int i;
4814 u64 l;
4815 int again = 1;
4816
4817 while (again) {
4818 again = 0;
4819 for (i = 0; i < bbio->num_stripes - 1; i++) {
4820 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4821 s = bbio->stripes[i];
4822 l = raid_map[i];
4823 bbio->stripes[i] = bbio->stripes[i+1];
4824 raid_map[i] = raid_map[i+1];
4825 bbio->stripes[i+1] = s;
4826 raid_map[i+1] = l;
4827 again = 1;
4828 }
4829 }
4830 }
4831}
4832
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004833static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004834 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004835 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004836 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004837{
4838 struct extent_map *em;
4839 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004840 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004841 struct extent_map_tree *em_tree = &map_tree->map_tree;
4842 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004843 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004844 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004845 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004846 u64 stripe_nr_orig;
4847 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004848 u64 stripe_len;
4849 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004850 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004851 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004852 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004853 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004854 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004855 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004856 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4857 int dev_replace_is_ongoing = 0;
4858 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004859 int patch_the_first_stripe_for_dev_replace = 0;
4860 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004861 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004862
Chris Mason890871b2009-09-02 16:24:52 -04004863 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004864 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004865 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004866
Chris Mason3b951512008-04-17 11:29:12 -04004867 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004868 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004869 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004870 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004871 }
Chris Mason0b86a832008-03-24 15:01:56 -04004872
Josef Bacik9bb91872013-04-19 23:45:33 -04004873 if (em->start > logical || em->start + em->len < logical) {
4874 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004875 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004876 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004877 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004878 return -EINVAL;
4879 }
4880
Chris Mason0b86a832008-03-24 15:01:56 -04004881 map = (struct map_lookup *)em->bdev;
4882 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004883
David Woodhouse53b381b2013-01-29 18:40:14 -05004884 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004885 stripe_nr = offset;
4886 /*
4887 * stripe_nr counts the total number of stripes we have to stride
4888 * to get to this block
4889 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004890 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004891
David Woodhouse53b381b2013-01-29 18:40:14 -05004892 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004893 BUG_ON(offset < stripe_offset);
4894
4895 /* stripe_offset is the offset of this block in its stripe*/
4896 stripe_offset = offset - stripe_offset;
4897
David Woodhouse53b381b2013-01-29 18:40:14 -05004898 /* if we're here for raid56, we need to know the stripe aligned start */
4899 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4900 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4901 raid56_full_stripe_start = offset;
4902
4903 /* allow a write of a full stripe, but make sure we don't
4904 * allow straddling of stripes
4905 */
4906 do_div(raid56_full_stripe_start, full_stripe_len);
4907 raid56_full_stripe_start *= full_stripe_len;
4908 }
4909
4910 if (rw & REQ_DISCARD) {
4911 /* we don't discard raid56 yet */
4912 if (map->type &
4913 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4914 ret = -EOPNOTSUPP;
4915 goto out;
4916 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004917 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004918 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4919 u64 max_len;
4920 /* For writes to RAID[56], allow a full stripeset across all disks.
4921 For other RAID types and for RAID[56] reads, just allow a single
4922 stripe (on a single disk). */
4923 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4924 (rw & REQ_WRITE)) {
4925 max_len = stripe_len * nr_data_stripes(map) -
4926 (offset - raid56_full_stripe_start);
4927 } else {
4928 /* we limit the length of each bio to what fits in a stripe */
4929 max_len = stripe_len - stripe_offset;
4930 }
4931 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004932 } else {
4933 *length = em->len - offset;
4934 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004935
David Woodhouse53b381b2013-01-29 18:40:14 -05004936 /* This is for when we're called from btrfs_merge_bio_hook() and all
4937 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004938 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004939 goto out;
4940
Stefan Behrens472262f2012-11-06 14:43:46 +01004941 btrfs_dev_replace_lock(dev_replace);
4942 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4943 if (!dev_replace_is_ongoing)
4944 btrfs_dev_replace_unlock(dev_replace);
4945
Stefan Behrensad6d6202012-11-06 15:06:47 +01004946 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4947 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4948 dev_replace->tgtdev != NULL) {
4949 /*
4950 * in dev-replace case, for repair case (that's the only
4951 * case where the mirror is selected explicitly when
4952 * calling btrfs_map_block), blocks left of the left cursor
4953 * can also be read from the target drive.
4954 * For REQ_GET_READ_MIRRORS, the target drive is added as
4955 * the last one to the array of stripes. For READ, it also
4956 * needs to be supported using the same mirror number.
4957 * If the requested block is not left of the left cursor,
4958 * EIO is returned. This can happen because btrfs_num_copies()
4959 * returns one more in the dev-replace case.
4960 */
4961 u64 tmp_length = *length;
4962 struct btrfs_bio *tmp_bbio = NULL;
4963 int tmp_num_stripes;
4964 u64 srcdev_devid = dev_replace->srcdev->devid;
4965 int index_srcdev = 0;
4966 int found = 0;
4967 u64 physical_of_found = 0;
4968
4969 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004970 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004971 if (ret) {
4972 WARN_ON(tmp_bbio != NULL);
4973 goto out;
4974 }
4975
4976 tmp_num_stripes = tmp_bbio->num_stripes;
4977 if (mirror_num > tmp_num_stripes) {
4978 /*
4979 * REQ_GET_READ_MIRRORS does not contain this
4980 * mirror, that means that the requested area
4981 * is not left of the left cursor
4982 */
4983 ret = -EIO;
4984 kfree(tmp_bbio);
4985 goto out;
4986 }
4987
4988 /*
4989 * process the rest of the function using the mirror_num
4990 * of the source drive. Therefore look it up first.
4991 * At the end, patch the device pointer to the one of the
4992 * target drive.
4993 */
4994 for (i = 0; i < tmp_num_stripes; i++) {
4995 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4996 /*
4997 * In case of DUP, in order to keep it
4998 * simple, only add the mirror with the
4999 * lowest physical address
5000 */
5001 if (found &&
5002 physical_of_found <=
5003 tmp_bbio->stripes[i].physical)
5004 continue;
5005 index_srcdev = i;
5006 found = 1;
5007 physical_of_found =
5008 tmp_bbio->stripes[i].physical;
5009 }
5010 }
5011
5012 if (found) {
5013 mirror_num = index_srcdev + 1;
5014 patch_the_first_stripe_for_dev_replace = 1;
5015 physical_to_patch_in_first_stripe = physical_of_found;
5016 } else {
5017 WARN_ON(1);
5018 ret = -EIO;
5019 kfree(tmp_bbio);
5020 goto out;
5021 }
5022
5023 kfree(tmp_bbio);
5024 } else if (mirror_num > map->num_stripes) {
5025 mirror_num = 0;
5026 }
5027
Chris Masonf2d8d742008-04-21 10:03:05 -04005028 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005029 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005030 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005031 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005032 do_div(stripe_nr_end, map->stripe_len);
5033 stripe_end_offset = stripe_nr_end * map->stripe_len -
5034 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005035
Li Dongyangfce3bb92011-03-24 10:24:26 +00005036 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5037 if (rw & REQ_DISCARD)
5038 num_stripes = min_t(u64, map->num_stripes,
5039 stripe_nr_end - stripe_nr_orig);
5040 stripe_index = do_div(stripe_nr, map->num_stripes);
5041 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005042 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005043 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005044 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005045 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005046 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005047 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005048 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005049 current->pid % map->num_stripes,
5050 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005051 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005052 }
Chris Mason2fff7342008-04-29 14:12:09 -04005053
Chris Mason611f0e02008-04-03 16:29:03 -04005054 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005055 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005056 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005057 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005058 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005059 } else {
5060 mirror_num = 1;
5061 }
Chris Mason2fff7342008-04-29 14:12:09 -04005062
Chris Mason321aecc2008-04-16 10:49:51 -04005063 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5064 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005065
5066 stripe_index = do_div(stripe_nr, factor);
5067 stripe_index *= map->sub_stripes;
5068
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005069 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005070 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005071 else if (rw & REQ_DISCARD)
5072 num_stripes = min_t(u64, map->sub_stripes *
5073 (stripe_nr_end - stripe_nr_orig),
5074 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005075 else if (mirror_num)
5076 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005077 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005078 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005079 stripe_index = find_live_mirror(fs_info, map,
5080 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005081 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005082 current->pid % map->sub_stripes,
5083 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005084 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005085 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005086
5087 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5088 BTRFS_BLOCK_GROUP_RAID6)) {
5089 u64 tmp;
5090
5091 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
5092 && raid_map_ret) {
5093 int i, rot;
5094
5095 /* push stripe_nr back to the start of the full stripe */
5096 stripe_nr = raid56_full_stripe_start;
5097 do_div(stripe_nr, stripe_len);
5098
5099 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5100
5101 /* RAID[56] write or recovery. Return all stripes */
5102 num_stripes = map->num_stripes;
5103 max_errors = nr_parity_stripes(map);
5104
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05305105 raid_map = kmalloc_array(num_stripes, sizeof(u64),
David Woodhouse53b381b2013-01-29 18:40:14 -05005106 GFP_NOFS);
5107 if (!raid_map) {
5108 ret = -ENOMEM;
5109 goto out;
5110 }
5111
5112 /* Work out the disk rotation on this stripe-set */
5113 tmp = stripe_nr;
5114 rot = do_div(tmp, num_stripes);
5115
5116 /* Fill in the logical address of each stripe */
5117 tmp = stripe_nr * nr_data_stripes(map);
5118 for (i = 0; i < nr_data_stripes(map); i++)
5119 raid_map[(i+rot) % num_stripes] =
5120 em->start + (tmp + i) * map->stripe_len;
5121
5122 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5123 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5124 raid_map[(i+rot+1) % num_stripes] =
5125 RAID6_Q_STRIPE;
5126
5127 *length = map->stripe_len;
5128 stripe_index = 0;
5129 stripe_offset = 0;
5130 } else {
5131 /*
5132 * Mirror #0 or #1 means the original data block.
5133 * Mirror #2 is RAID5 parity block.
5134 * Mirror #3 is RAID6 Q block.
5135 */
5136 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5137 if (mirror_num > 1)
5138 stripe_index = nr_data_stripes(map) +
5139 mirror_num - 2;
5140
5141 /* We distribute the parity blocks across stripes */
5142 tmp = stripe_nr + stripe_index;
5143 stripe_index = do_div(tmp, map->num_stripes);
5144 }
Chris Mason8790d502008-04-03 16:29:03 -04005145 } else {
5146 /*
5147 * after this do_div call, stripe_nr is the number of stripes
5148 * on this device we have to walk to find the data, and
5149 * stripe_index is the number of our device in the stripe array
5150 */
5151 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005152 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005153 }
Chris Mason593060d2008-03-25 16:50:33 -04005154 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005155
Stefan Behrens472262f2012-11-06 14:43:46 +01005156 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005157 if (dev_replace_is_ongoing) {
5158 if (rw & (REQ_WRITE | REQ_DISCARD))
5159 num_alloc_stripes <<= 1;
5160 if (rw & REQ_GET_READ_MIRRORS)
5161 num_alloc_stripes++;
5162 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005163 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08005164 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04005165 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08005166 ret = -ENOMEM;
5167 goto out;
5168 }
5169 atomic_set(&bbio->error, 0);
5170
Li Dongyangfce3bb92011-03-24 10:24:26 +00005171 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08005172 int factor = 0;
5173 int sub_stripes = 0;
5174 u64 stripes_per_dev = 0;
5175 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005176 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005177
5178 if (map->type &
5179 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5180 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5181 sub_stripes = 1;
5182 else
5183 sub_stripes = map->sub_stripes;
5184
5185 factor = map->num_stripes / sub_stripes;
5186 stripes_per_dev = div_u64_rem(stripe_nr_end -
5187 stripe_nr_orig,
5188 factor,
5189 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005190 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5191 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005192 }
5193
Li Dongyangfce3bb92011-03-24 10:24:26 +00005194 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005195 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005196 map->stripes[stripe_index].physical +
5197 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005198 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005199
Li Zefanec9ef7a2011-12-01 14:06:42 +08005200 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5201 BTRFS_BLOCK_GROUP_RAID10)) {
5202 bbio->stripes[i].length = stripes_per_dev *
5203 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005204
Li Zefanec9ef7a2011-12-01 14:06:42 +08005205 if (i / sub_stripes < remaining_stripes)
5206 bbio->stripes[i].length +=
5207 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005208
5209 /*
5210 * Special for the first stripe and
5211 * the last stripe:
5212 *
5213 * |-------|...|-------|
5214 * |----------|
5215 * off end_off
5216 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005217 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005218 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005219 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005220
5221 if (stripe_index >= last_stripe &&
5222 stripe_index <= (last_stripe +
5223 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005224 bbio->stripes[i].length -=
5225 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005226
Li Zefanec9ef7a2011-12-01 14:06:42 +08005227 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005228 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005229 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005230 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005231
5232 stripe_index++;
5233 if (stripe_index == map->num_stripes) {
5234 /* This could only happen for RAID0/10 */
5235 stripe_index = 0;
5236 stripe_nr++;
5237 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005238 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005239 } else {
5240 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005241 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005242 map->stripes[stripe_index].physical +
5243 stripe_offset +
5244 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005245 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005246 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005247 stripe_index++;
5248 }
Chris Mason593060d2008-03-25 16:50:33 -04005249 }
Li Zefande11cc12011-12-01 12:55:47 +08005250
Miao Xied20983b2014-07-03 18:22:13 +08005251 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5252 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005253
Stefan Behrens472262f2012-11-06 14:43:46 +01005254 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5255 dev_replace->tgtdev != NULL) {
5256 int index_where_to_add;
5257 u64 srcdev_devid = dev_replace->srcdev->devid;
5258
5259 /*
5260 * duplicate the write operations while the dev replace
5261 * procedure is running. Since the copying of the old disk
5262 * to the new disk takes place at run time while the
5263 * filesystem is mounted writable, the regular write
5264 * operations to the old disk have to be duplicated to go
5265 * to the new disk as well.
5266 * Note that device->missing is handled by the caller, and
5267 * that the write to the old disk is already set up in the
5268 * stripes array.
5269 */
5270 index_where_to_add = num_stripes;
5271 for (i = 0; i < num_stripes; i++) {
5272 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5273 /* write to new disk, too */
5274 struct btrfs_bio_stripe *new =
5275 bbio->stripes + index_where_to_add;
5276 struct btrfs_bio_stripe *old =
5277 bbio->stripes + i;
5278
5279 new->physical = old->physical;
5280 new->length = old->length;
5281 new->dev = dev_replace->tgtdev;
5282 index_where_to_add++;
5283 max_errors++;
5284 }
5285 }
5286 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005287 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5288 dev_replace->tgtdev != NULL) {
5289 u64 srcdev_devid = dev_replace->srcdev->devid;
5290 int index_srcdev = 0;
5291 int found = 0;
5292 u64 physical_of_found = 0;
5293
5294 /*
5295 * During the dev-replace procedure, the target drive can
5296 * also be used to read data in case it is needed to repair
5297 * a corrupt block elsewhere. This is possible if the
5298 * requested area is left of the left cursor. In this area,
5299 * the target drive is a full copy of the source drive.
5300 */
5301 for (i = 0; i < num_stripes; i++) {
5302 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5303 /*
5304 * In case of DUP, in order to keep it
5305 * simple, only add the mirror with the
5306 * lowest physical address
5307 */
5308 if (found &&
5309 physical_of_found <=
5310 bbio->stripes[i].physical)
5311 continue;
5312 index_srcdev = i;
5313 found = 1;
5314 physical_of_found = bbio->stripes[i].physical;
5315 }
5316 }
5317 if (found) {
5318 u64 length = map->stripe_len;
5319
5320 if (physical_of_found + length <=
5321 dev_replace->cursor_left) {
5322 struct btrfs_bio_stripe *tgtdev_stripe =
5323 bbio->stripes + num_stripes;
5324
5325 tgtdev_stripe->physical = physical_of_found;
5326 tgtdev_stripe->length =
5327 bbio->stripes[index_srcdev].length;
5328 tgtdev_stripe->dev = dev_replace->tgtdev;
5329
5330 num_stripes++;
5331 }
5332 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005333 }
5334
Li Zefande11cc12011-12-01 12:55:47 +08005335 *bbio_ret = bbio;
5336 bbio->num_stripes = num_stripes;
5337 bbio->max_errors = max_errors;
5338 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005339
5340 /*
5341 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5342 * mirror_num == num_stripes + 1 && dev_replace target drive is
5343 * available as a mirror
5344 */
5345 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5346 WARN_ON(num_stripes > 1);
5347 bbio->stripes[0].dev = dev_replace->tgtdev;
5348 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5349 bbio->mirror_num = map->num_stripes + 1;
5350 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005351 if (raid_map) {
5352 sort_parity_stripes(bbio, raid_map);
5353 *raid_map_ret = raid_map;
5354 }
Chris Masoncea9e442008-04-09 16:28:12 -04005355out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005356 if (dev_replace_is_ongoing)
5357 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005358 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005359 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005360}
5361
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005362int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005363 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005364 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005365{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005366 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005367 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005368}
5369
Yan Zhenga512bbf2008-12-08 16:46:26 -05005370int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5371 u64 chunk_start, u64 physical, u64 devid,
5372 u64 **logical, int *naddrs, int *stripe_len)
5373{
5374 struct extent_map_tree *em_tree = &map_tree->map_tree;
5375 struct extent_map *em;
5376 struct map_lookup *map;
5377 u64 *buf;
5378 u64 bytenr;
5379 u64 length;
5380 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005381 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005382 int i, j, nr = 0;
5383
Chris Mason890871b2009-09-02 16:24:52 -04005384 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005385 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005386 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005387
Josef Bacik835d9742013-03-19 12:13:25 -04005388 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005389 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005390 chunk_start);
5391 return -EIO;
5392 }
5393
5394 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005395 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005396 em->start, chunk_start);
5397 free_extent_map(em);
5398 return -EIO;
5399 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005400 map = (struct map_lookup *)em->bdev;
5401
5402 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005403 rmap_len = map->stripe_len;
5404
Yan Zhenga512bbf2008-12-08 16:46:26 -05005405 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5406 do_div(length, map->num_stripes / map->sub_stripes);
5407 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5408 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005409 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5410 BTRFS_BLOCK_GROUP_RAID6)) {
5411 do_div(length, nr_data_stripes(map));
5412 rmap_len = map->stripe_len * nr_data_stripes(map);
5413 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005414
5415 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005416 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005417
5418 for (i = 0; i < map->num_stripes; i++) {
5419 if (devid && map->stripes[i].dev->devid != devid)
5420 continue;
5421 if (map->stripes[i].physical > physical ||
5422 map->stripes[i].physical + length <= physical)
5423 continue;
5424
5425 stripe_nr = physical - map->stripes[i].physical;
5426 do_div(stripe_nr, map->stripe_len);
5427
5428 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5429 stripe_nr = stripe_nr * map->num_stripes + i;
5430 do_div(stripe_nr, map->sub_stripes);
5431 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5432 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005433 } /* else if RAID[56], multiply by nr_data_stripes().
5434 * Alternatively, just use rmap_len below instead of
5435 * map->stripe_len */
5436
5437 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005438 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005439 for (j = 0; j < nr; j++) {
5440 if (buf[j] == bytenr)
5441 break;
5442 }
Chris Mason934d3752008-12-08 16:43:10 -05005443 if (j == nr) {
5444 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005445 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005446 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005447 }
5448
Yan Zhenga512bbf2008-12-08 16:46:26 -05005449 *logical = buf;
5450 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005451 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005452
5453 free_extent_map(em);
5454 return 0;
5455}
5456
Miao Xie8408c712014-06-19 10:42:55 +08005457static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5458{
5459 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5460 bio_endio_nodec(bio, err);
5461 else
5462 bio_endio(bio, err);
5463 kfree(bbio);
5464}
5465
Jan Schmidta1d3c472011-08-04 17:15:33 +02005466static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005467{
Chris Mason9be33952013-05-17 18:30:14 -04005468 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005469 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005470 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005471
Stefan Behrens442a4f62012-05-25 16:06:08 +02005472 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005473 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005474 if (err == -EIO || err == -EREMOTEIO) {
5475 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005476 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005477
5478 BUG_ON(stripe_index >= bbio->num_stripes);
5479 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005480 if (dev->bdev) {
5481 if (bio->bi_rw & WRITE)
5482 btrfs_dev_stat_inc(dev,
5483 BTRFS_DEV_STAT_WRITE_ERRS);
5484 else
5485 btrfs_dev_stat_inc(dev,
5486 BTRFS_DEV_STAT_READ_ERRS);
5487 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5488 btrfs_dev_stat_inc(dev,
5489 BTRFS_DEV_STAT_FLUSH_ERRS);
5490 btrfs_dev_stat_print_on_error(dev);
5491 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005492 }
5493 }
Chris Mason8790d502008-04-03 16:29:03 -04005494
Jan Schmidta1d3c472011-08-04 17:15:33 +02005495 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005496 is_orig_bio = 1;
5497
Miao Xiec404e0d2014-01-30 16:46:55 +08005498 btrfs_bio_counter_dec(bbio->fs_info);
5499
Jan Schmidta1d3c472011-08-04 17:15:33 +02005500 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005501 if (!is_orig_bio) {
5502 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005503 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005504 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005505
Jan Schmidta1d3c472011-08-04 17:15:33 +02005506 bio->bi_private = bbio->private;
5507 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005508 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005509 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005510 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005511 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005512 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005513 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005514 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005515 /*
5516 * this bio is actually up to date, we didn't
5517 * go over the max number of errors
5518 */
5519 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005520 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005521 }
Miao Xiec55f1392014-06-19 10:42:54 +08005522
Miao Xie8408c712014-06-19 10:42:55 +08005523 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005524 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005525 bio_put(bio);
5526 }
Chris Mason8790d502008-04-03 16:29:03 -04005527}
5528
Chris Mason8b712842008-06-11 16:50:36 -04005529/*
5530 * see run_scheduled_bios for a description of why bios are collected for
5531 * async submit.
5532 *
5533 * This will add one bio to the pending list for a device and make sure
5534 * the work struct is scheduled.
5535 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005536static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5537 struct btrfs_device *device,
5538 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005539{
5540 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005541 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005542
David Woodhouse53b381b2013-01-29 18:40:14 -05005543 if (device->missing || !device->bdev) {
5544 bio_endio(bio, -EIO);
5545 return;
5546 }
5547
Chris Mason8b712842008-06-11 16:50:36 -04005548 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005549 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005550 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005551 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005552 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005553 return;
Chris Mason8b712842008-06-11 16:50:36 -04005554 }
5555
5556 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005557 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005558 * higher layers. Otherwise, the async bio makes it appear we have
5559 * made progress against dirty pages when we've really just put it
5560 * on a queue for later
5561 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005562 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005563 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005564 bio->bi_next = NULL;
5565 bio->bi_rw |= rw;
5566
5567 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005568 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005569 pending_bios = &device->pending_sync_bios;
5570 else
5571 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005572
Chris Masonffbd5172009-04-20 15:50:09 -04005573 if (pending_bios->tail)
5574 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005575
Chris Masonffbd5172009-04-20 15:50:09 -04005576 pending_bios->tail = bio;
5577 if (!pending_bios->head)
5578 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005579 if (device->running_pending)
5580 should_queue = 0;
5581
5582 spin_unlock(&device->io_lock);
5583
5584 if (should_queue)
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005585 btrfs_queue_work(root->fs_info->submit_workers,
5586 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005587}
5588
Josef Bacikde1ee922012-10-19 16:50:56 -04005589static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5590 sector_t sector)
5591{
5592 struct bio_vec *prev;
5593 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005594 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005595 struct bvec_merge_data bvm = {
5596 .bi_bdev = bdev,
5597 .bi_sector = sector,
5598 .bi_rw = bio->bi_rw,
5599 };
5600
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305601 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005602 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005603
5604 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005605 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005606 return 0;
5607
5608 if (!q->merge_bvec_fn)
5609 return 1;
5610
Kent Overstreet4f024f32013-10-11 15:44:27 -07005611 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005612 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5613 return 0;
5614 return 1;
5615}
5616
5617static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5618 struct bio *bio, u64 physical, int dev_nr,
5619 int rw, int async)
5620{
5621 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5622
5623 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005624 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005625 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005626 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005627#ifdef DEBUG
5628 {
5629 struct rcu_string *name;
5630
5631 rcu_read_lock();
5632 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005633 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005634 "(%s id %llu), size=%u\n", rw,
5635 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5636 name->str, dev->devid, bio->bi_size);
5637 rcu_read_unlock();
5638 }
5639#endif
5640 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005641
5642 btrfs_bio_counter_inc_noblocked(root->fs_info);
5643
Josef Bacikde1ee922012-10-19 16:50:56 -04005644 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005645 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005646 else
5647 btrfsic_submit_bio(rw, bio);
5648}
5649
5650static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5651 struct bio *first_bio, struct btrfs_device *dev,
5652 int dev_nr, int rw, int async)
5653{
5654 struct bio_vec *bvec = first_bio->bi_io_vec;
5655 struct bio *bio;
5656 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5657 u64 physical = bbio->stripes[dev_nr].physical;
5658
5659again:
5660 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5661 if (!bio)
5662 return -ENOMEM;
5663
5664 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5665 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5666 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005667 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005668
5669 atomic_inc(&bbio->stripes_pending);
5670 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5671 rw, async);
5672 physical += len;
5673 goto again;
5674 }
5675 bvec++;
5676 }
5677
5678 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5679 return 0;
5680}
5681
5682static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5683{
5684 atomic_inc(&bbio->error);
5685 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005686 /* Shoud be the original bio. */
5687 WARN_ON(bio != bbio->orig_bio);
5688
Josef Bacikde1ee922012-10-19 16:50:56 -04005689 bio->bi_private = bbio->private;
5690 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005691 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005692 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005693
5694 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005695 }
5696}
5697
Chris Masonf1885912008-04-09 16:28:12 -04005698int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005699 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005700{
Chris Mason0b86a832008-03-24 15:01:56 -04005701 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005702 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005703 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005704 u64 length = 0;
5705 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005706 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005707 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005708 int dev_nr = 0;
5709 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005710 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005711
Kent Overstreet4f024f32013-10-11 15:44:27 -07005712 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005713 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005714
Miao Xiec404e0d2014-01-30 16:46:55 +08005715 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005716 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5717 mirror_num, &raid_map);
Miao Xiec404e0d2014-01-30 16:46:55 +08005718 if (ret) {
5719 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005720 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005721 }
Chris Masoncea9e442008-04-09 16:28:12 -04005722
Jan Schmidta1d3c472011-08-04 17:15:33 +02005723 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005724 bbio->orig_bio = first_bio;
5725 bbio->private = first_bio->bi_private;
5726 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005727 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005728 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5729
5730 if (raid_map) {
5731 /* In this case, map_length has been set to the length of
5732 a single stripe; not the whole write */
5733 if (rw & WRITE) {
Miao Xiec404e0d2014-01-30 16:46:55 +08005734 ret = raid56_parity_write(root, bio, bbio,
5735 raid_map, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005736 } else {
Miao Xiec404e0d2014-01-30 16:46:55 +08005737 ret = raid56_parity_recover(root, bio, bbio,
5738 raid_map, map_length,
5739 mirror_num);
David Woodhouse53b381b2013-01-29 18:40:14 -05005740 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005741 /*
5742 * FIXME, replace dosen't support raid56 yet, please fix
5743 * it in the future.
5744 */
5745 btrfs_bio_counter_dec(root->fs_info);
5746 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005747 }
5748
Chris Masoncea9e442008-04-09 16:28:12 -04005749 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005750 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005751 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005752 BUG();
5753 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005754
Chris Masond3977122009-01-05 21:25:51 -05005755 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005756 dev = bbio->stripes[dev_nr].dev;
5757 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5758 bbio_error(bbio, first_bio, logical);
5759 dev_nr++;
5760 continue;
5761 }
5762
5763 /*
5764 * Check and see if we're ok with this bio based on it's size
5765 * and offset with the given device.
5766 */
5767 if (!bio_size_ok(dev->bdev, first_bio,
5768 bbio->stripes[dev_nr].physical >> 9)) {
5769 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5770 dev_nr, rw, async_submit);
5771 BUG_ON(ret);
5772 dev_nr++;
5773 continue;
5774 }
5775
Jan Schmidta1d3c472011-08-04 17:15:33 +02005776 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005777 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005778 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005779 } else {
5780 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005781 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005782 }
Josef Bacik606686e2012-06-04 14:03:51 -04005783
Josef Bacikde1ee922012-10-19 16:50:56 -04005784 submit_stripe_bio(root, bbio, bio,
5785 bbio->stripes[dev_nr].physical, dev_nr, rw,
5786 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005787 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005788 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005789 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005790 return 0;
5791}
5792
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005793struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005794 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005795{
Yan Zheng2b820322008-11-17 21:11:30 -05005796 struct btrfs_device *device;
5797 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005798
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005799 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005800 while (cur_devices) {
5801 if (!fsid ||
5802 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5803 device = __find_device(&cur_devices->devices,
5804 devid, uuid);
5805 if (device)
5806 return device;
5807 }
5808 cur_devices = cur_devices->seed;
5809 }
5810 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005811}
5812
Chris Masondfe25022008-05-13 13:46:40 -04005813static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5814 u64 devid, u8 *dev_uuid)
5815{
5816 struct btrfs_device *device;
5817 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5818
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005819 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5820 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005821 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005822
5823 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005824 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005825 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005826
5827 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005828 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005829
Chris Masondfe25022008-05-13 13:46:40 -04005830 return device;
5831}
5832
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005833/**
5834 * btrfs_alloc_device - allocate struct btrfs_device
5835 * @fs_info: used only for generating a new devid, can be NULL if
5836 * devid is provided (i.e. @devid != NULL).
5837 * @devid: a pointer to devid for this device. If NULL a new devid
5838 * is generated.
5839 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5840 * is generated.
5841 *
5842 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5843 * on error. Returned struct is not linked onto any lists and can be
5844 * destroyed with kfree() right away.
5845 */
5846struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5847 const u64 *devid,
5848 const u8 *uuid)
5849{
5850 struct btrfs_device *dev;
5851 u64 tmp;
5852
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305853 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005854 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005855
5856 dev = __alloc_device();
5857 if (IS_ERR(dev))
5858 return dev;
5859
5860 if (devid)
5861 tmp = *devid;
5862 else {
5863 int ret;
5864
5865 ret = find_next_devid(fs_info, &tmp);
5866 if (ret) {
5867 kfree(dev);
5868 return ERR_PTR(ret);
5869 }
5870 }
5871 dev->devid = tmp;
5872
5873 if (uuid)
5874 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5875 else
5876 generate_random_uuid(dev->uuid);
5877
Liu Bo9e0af232014-08-15 23:36:53 +08005878 btrfs_init_work(&dev->work, btrfs_submit_helper,
5879 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005880
5881 return dev;
5882}
5883
Chris Mason0b86a832008-03-24 15:01:56 -04005884static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5885 struct extent_buffer *leaf,
5886 struct btrfs_chunk *chunk)
5887{
5888 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5889 struct map_lookup *map;
5890 struct extent_map *em;
5891 u64 logical;
5892 u64 length;
5893 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005894 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005895 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005896 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005897 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005898
Chris Masone17cade2008-04-15 15:41:47 -04005899 logical = key->offset;
5900 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005901
Chris Mason890871b2009-09-02 16:24:52 -04005902 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005903 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005904 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005905
5906 /* already mapped? */
5907 if (em && em->start <= logical && em->start + em->len > logical) {
5908 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005909 return 0;
5910 } else if (em) {
5911 free_extent_map(em);
5912 }
Chris Mason0b86a832008-03-24 15:01:56 -04005913
David Sterba172ddd62011-04-21 00:48:27 +02005914 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005915 if (!em)
5916 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005917 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5918 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005919 if (!map) {
5920 free_extent_map(em);
5921 return -ENOMEM;
5922 }
5923
Wang Shilong298a8f92014-06-19 10:42:52 +08005924 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04005925 em->bdev = (struct block_device *)map;
5926 em->start = logical;
5927 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005928 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005929 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005930 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005931
Chris Mason593060d2008-03-25 16:50:33 -04005932 map->num_stripes = num_stripes;
5933 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5934 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5935 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5936 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5937 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005938 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005939 for (i = 0; i < num_stripes; i++) {
5940 map->stripes[i].physical =
5941 btrfs_stripe_offset_nr(leaf, chunk, i);
5942 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005943 read_extent_buffer(leaf, uuid, (unsigned long)
5944 btrfs_stripe_dev_uuid_nr(chunk, i),
5945 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005946 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5947 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005948 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005949 free_extent_map(em);
5950 return -EIO;
5951 }
Chris Masondfe25022008-05-13 13:46:40 -04005952 if (!map->stripes[i].dev) {
5953 map->stripes[i].dev =
5954 add_missing_dev(root, devid, uuid);
5955 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04005956 free_extent_map(em);
5957 return -EIO;
5958 }
5959 }
5960 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005961 }
5962
Chris Mason890871b2009-09-02 16:24:52 -04005963 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005964 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005965 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005966 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005967 free_extent_map(em);
5968
5969 return 0;
5970}
5971
Jeff Mahoney143bede2012-03-01 14:56:26 +01005972static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005973 struct btrfs_dev_item *dev_item,
5974 struct btrfs_device *device)
5975{
5976 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005977
5978 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005979 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5980 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005981 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5982 device->type = btrfs_device_type(leaf, dev_item);
5983 device->io_align = btrfs_device_io_align(leaf, dev_item);
5984 device->io_width = btrfs_device_io_width(leaf, dev_item);
5985 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005986 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005987 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005988
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02005989 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005990 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005991}
5992
Yan Zheng2b820322008-11-17 21:11:30 -05005993static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5994{
5995 struct btrfs_fs_devices *fs_devices;
5996 int ret;
5997
Li Zefanb367e472011-12-07 11:38:24 +08005998 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005999
6000 fs_devices = root->fs_info->fs_devices->seed;
6001 while (fs_devices) {
6002 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6003 ret = 0;
6004 goto out;
6005 }
6006 fs_devices = fs_devices->seed;
6007 }
6008
6009 fs_devices = find_fsid(fsid);
6010 if (!fs_devices) {
6011 ret = -ENOENT;
6012 goto out;
6013 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006014
6015 fs_devices = clone_fs_devices(fs_devices);
6016 if (IS_ERR(fs_devices)) {
6017 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006018 goto out;
6019 }
6020
Christoph Hellwig97288f22008-12-02 06:36:09 -05006021 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006022 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006023 if (ret) {
6024 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006025 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006026 }
Yan Zheng2b820322008-11-17 21:11:30 -05006027
6028 if (!fs_devices->seeding) {
6029 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006030 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006031 ret = -EINVAL;
6032 goto out;
6033 }
6034
6035 fs_devices->seed = root->fs_info->fs_devices->seed;
6036 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006037out:
Yan Zheng2b820322008-11-17 21:11:30 -05006038 return ret;
6039}
6040
Chris Mason0d81ba52008-03-24 15:02:07 -04006041static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006042 struct extent_buffer *leaf,
6043 struct btrfs_dev_item *dev_item)
6044{
6045 struct btrfs_device *device;
6046 u64 devid;
6047 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006048 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006049 u8 dev_uuid[BTRFS_UUID_SIZE];
6050
Chris Mason0b86a832008-03-24 15:01:56 -04006051 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006052 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006053 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006054 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006055 BTRFS_UUID_SIZE);
6056
6057 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6058 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05006059 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006060 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006061 }
6062
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006063 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006064 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006065 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006066 return -EIO;
6067
6068 if (!device) {
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006069 btrfs_warn(root->fs_info, "devid %llu missing", devid);
Yan Zheng2b820322008-11-17 21:11:30 -05006070 device = add_missing_dev(root, devid, dev_uuid);
6071 if (!device)
6072 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05006073 } else if (!device->missing) {
6074 /*
6075 * this happens when a device that was properly setup
6076 * in the device info lists suddenly goes bad.
6077 * device->bdev is NULL, and so we have to set
6078 * device->missing to one here
6079 */
6080 root->fs_info->fs_devices->missing_devices++;
6081 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006082 }
6083 }
6084
6085 if (device->fs_devices != root->fs_info->fs_devices) {
6086 BUG_ON(device->writeable);
6087 if (device->generation !=
6088 btrfs_device_generation(leaf, dev_item))
6089 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006090 }
Chris Mason0b86a832008-03-24 15:01:56 -04006091
6092 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006093 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006094 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006095 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006096 spin_lock(&root->fs_info->free_chunk_lock);
6097 root->fs_info->free_chunk_space += device->total_bytes -
6098 device->bytes_used;
6099 spin_unlock(&root->fs_info->free_chunk_lock);
6100 }
Chris Mason0b86a832008-03-24 15:01:56 -04006101 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006102 return ret;
6103}
6104
Yan Zhenge4404d62008-12-12 10:03:26 -05006105int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006106{
David Sterba6c417612011-04-13 15:41:04 +02006107 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006108 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006109 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006110 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04006111 u8 *ptr;
6112 unsigned long sb_ptr;
6113 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006114 u32 num_stripes;
6115 u32 array_size;
6116 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006117 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04006118 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006119
Yan Zhenge4404d62008-12-12 10:03:26 -05006120 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04006121 BTRFS_SUPER_INFO_SIZE);
6122 if (!sb)
6123 return -ENOMEM;
6124 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006125 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006126 /*
6127 * The sb extent buffer is artifical and just used to read the system array.
6128 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6129 * pages up-to-date when the page is larger: extent does not cover the
6130 * whole page and consequently check_page_uptodate does not find all
6131 * the page's extents up-to-date (the hole beyond sb),
6132 * write_extent_buffer then triggers a WARN_ON.
6133 *
6134 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6135 * but sb spans only this function. Add an explicit SetPageUptodate call
6136 * to silence the warning eg. on PowerPC 64.
6137 */
6138 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006139 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006140
Chris Masona061fc82008-05-07 11:43:44 -04006141 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006142 array_size = btrfs_super_sys_array_size(super_copy);
6143
Chris Mason0b86a832008-03-24 15:01:56 -04006144 ptr = super_copy->sys_chunk_array;
6145 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
6146 cur = 0;
6147
6148 while (cur < array_size) {
6149 disk_key = (struct btrfs_disk_key *)ptr;
6150 btrfs_disk_key_to_cpu(&key, disk_key);
6151
Chris Masona061fc82008-05-07 11:43:44 -04006152 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006153 sb_ptr += len;
6154 cur += len;
6155
Chris Mason0d81ba52008-03-24 15:02:07 -04006156 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04006157 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04006158 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006159 if (ret)
6160 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006161 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6162 len = btrfs_chunk_item_size(num_stripes);
6163 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006164 ret = -EIO;
6165 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006166 }
6167 ptr += len;
6168 sb_ptr += len;
6169 cur += len;
6170 }
Chris Masona061fc82008-05-07 11:43:44 -04006171 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006172 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006173}
6174
6175int btrfs_read_chunk_tree(struct btrfs_root *root)
6176{
6177 struct btrfs_path *path;
6178 struct extent_buffer *leaf;
6179 struct btrfs_key key;
6180 struct btrfs_key found_key;
6181 int ret;
6182 int slot;
6183
6184 root = root->fs_info->chunk_root;
6185
6186 path = btrfs_alloc_path();
6187 if (!path)
6188 return -ENOMEM;
6189
Li Zefanb367e472011-12-07 11:38:24 +08006190 mutex_lock(&uuid_mutex);
6191 lock_chunks(root);
6192
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006193 /*
6194 * Read all device items, and then all the chunk items. All
6195 * device items are found before any chunk item (their object id
6196 * is smaller than the lowest possible object id for a chunk
6197 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006198 */
6199 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6200 key.offset = 0;
6201 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006202 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006203 if (ret < 0)
6204 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006205 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006206 leaf = path->nodes[0];
6207 slot = path->slots[0];
6208 if (slot >= btrfs_header_nritems(leaf)) {
6209 ret = btrfs_next_leaf(root, path);
6210 if (ret == 0)
6211 continue;
6212 if (ret < 0)
6213 goto error;
6214 break;
6215 }
6216 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006217 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6218 struct btrfs_dev_item *dev_item;
6219 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006220 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006221 ret = read_one_dev(root, leaf, dev_item);
6222 if (ret)
6223 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006224 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6225 struct btrfs_chunk *chunk;
6226 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6227 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006228 if (ret)
6229 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006230 }
6231 path->slots[0]++;
6232 }
Chris Mason0b86a832008-03-24 15:01:56 -04006233 ret = 0;
6234error:
Li Zefanb367e472011-12-07 11:38:24 +08006235 unlock_chunks(root);
6236 mutex_unlock(&uuid_mutex);
6237
Yan Zheng2b820322008-11-17 21:11:30 -05006238 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006239 return ret;
6240}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006241
Miao Xiecb517ea2013-05-15 07:48:19 +00006242void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6243{
6244 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6245 struct btrfs_device *device;
6246
Liu Bo29cc83f2014-05-11 23:14:59 +08006247 while (fs_devices) {
6248 mutex_lock(&fs_devices->device_list_mutex);
6249 list_for_each_entry(device, &fs_devices->devices, dev_list)
6250 device->dev_root = fs_info->dev_root;
6251 mutex_unlock(&fs_devices->device_list_mutex);
6252
6253 fs_devices = fs_devices->seed;
6254 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006255}
6256
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006257static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6258{
6259 int i;
6260
6261 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6262 btrfs_dev_stat_reset(dev, i);
6263}
6264
6265int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6266{
6267 struct btrfs_key key;
6268 struct btrfs_key found_key;
6269 struct btrfs_root *dev_root = fs_info->dev_root;
6270 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6271 struct extent_buffer *eb;
6272 int slot;
6273 int ret = 0;
6274 struct btrfs_device *device;
6275 struct btrfs_path *path = NULL;
6276 int i;
6277
6278 path = btrfs_alloc_path();
6279 if (!path) {
6280 ret = -ENOMEM;
6281 goto out;
6282 }
6283
6284 mutex_lock(&fs_devices->device_list_mutex);
6285 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6286 int item_size;
6287 struct btrfs_dev_stats_item *ptr;
6288
6289 key.objectid = 0;
6290 key.type = BTRFS_DEV_STATS_KEY;
6291 key.offset = device->devid;
6292 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6293 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006294 __btrfs_reset_dev_stats(device);
6295 device->dev_stats_valid = 1;
6296 btrfs_release_path(path);
6297 continue;
6298 }
6299 slot = path->slots[0];
6300 eb = path->nodes[0];
6301 btrfs_item_key_to_cpu(eb, &found_key, slot);
6302 item_size = btrfs_item_size_nr(eb, slot);
6303
6304 ptr = btrfs_item_ptr(eb, slot,
6305 struct btrfs_dev_stats_item);
6306
6307 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6308 if (item_size >= (1 + i) * sizeof(__le64))
6309 btrfs_dev_stat_set(device, i,
6310 btrfs_dev_stats_value(eb, ptr, i));
6311 else
6312 btrfs_dev_stat_reset(device, i);
6313 }
6314
6315 device->dev_stats_valid = 1;
6316 btrfs_dev_stat_print_on_load(device);
6317 btrfs_release_path(path);
6318 }
6319 mutex_unlock(&fs_devices->device_list_mutex);
6320
6321out:
6322 btrfs_free_path(path);
6323 return ret < 0 ? ret : 0;
6324}
6325
6326static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6327 struct btrfs_root *dev_root,
6328 struct btrfs_device *device)
6329{
6330 struct btrfs_path *path;
6331 struct btrfs_key key;
6332 struct extent_buffer *eb;
6333 struct btrfs_dev_stats_item *ptr;
6334 int ret;
6335 int i;
6336
6337 key.objectid = 0;
6338 key.type = BTRFS_DEV_STATS_KEY;
6339 key.offset = device->devid;
6340
6341 path = btrfs_alloc_path();
6342 BUG_ON(!path);
6343 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6344 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006345 printk_in_rcu(KERN_WARNING "BTRFS: "
6346 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006347 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006348 goto out;
6349 }
6350
6351 if (ret == 0 &&
6352 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6353 /* need to delete old one and insert a new one */
6354 ret = btrfs_del_item(trans, dev_root, path);
6355 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006356 printk_in_rcu(KERN_WARNING "BTRFS: "
6357 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006358 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006359 goto out;
6360 }
6361 ret = 1;
6362 }
6363
6364 if (ret == 1) {
6365 /* need to insert a new item */
6366 btrfs_release_path(path);
6367 ret = btrfs_insert_empty_item(trans, dev_root, path,
6368 &key, sizeof(*ptr));
6369 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006370 printk_in_rcu(KERN_WARNING "BTRFS: "
6371 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006372 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006373 goto out;
6374 }
6375 }
6376
6377 eb = path->nodes[0];
6378 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6379 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6380 btrfs_set_dev_stats_value(eb, ptr, i,
6381 btrfs_dev_stat_read(device, i));
6382 btrfs_mark_buffer_dirty(eb);
6383
6384out:
6385 btrfs_free_path(path);
6386 return ret;
6387}
6388
6389/*
6390 * called from commit_transaction. Writes all changed device stats to disk.
6391 */
6392int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6393 struct btrfs_fs_info *fs_info)
6394{
6395 struct btrfs_root *dev_root = fs_info->dev_root;
6396 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6397 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006398 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006399 int ret = 0;
6400
6401 mutex_lock(&fs_devices->device_list_mutex);
6402 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006403 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006404 continue;
6405
Miao Xieaddc3fa2014-07-24 11:37:11 +08006406 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006407 ret = update_dev_stat_item(trans, dev_root, device);
6408 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006409 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006410 }
6411 mutex_unlock(&fs_devices->device_list_mutex);
6412
6413 return ret;
6414}
6415
Stefan Behrens442a4f62012-05-25 16:06:08 +02006416void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6417{
6418 btrfs_dev_stat_inc(dev, index);
6419 btrfs_dev_stat_print_on_error(dev);
6420}
6421
Eric Sandeen48a3b632013-04-25 20:41:01 +00006422static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006423{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006424 if (!dev->dev_stats_valid)
6425 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006426 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6427 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006428 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006429 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6430 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6431 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006432 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6433 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006434}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006435
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006436static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6437{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006438 int i;
6439
6440 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6441 if (btrfs_dev_stat_read(dev, i) != 0)
6442 break;
6443 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6444 return; /* all values == 0, suppress message */
6445
Frank Holtonefe120a2013-12-20 11:37:06 -05006446 printk_in_rcu(KERN_INFO "BTRFS: "
6447 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006448 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006449 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6450 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6451 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6452 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6453 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6454}
6455
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006456int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006457 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006458{
6459 struct btrfs_device *dev;
6460 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6461 int i;
6462
6463 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006464 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006465 mutex_unlock(&fs_devices->device_list_mutex);
6466
6467 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006468 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006469 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006470 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006471 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006472 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006473 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006474 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6475 if (stats->nr_items > i)
6476 stats->values[i] =
6477 btrfs_dev_stat_read_and_reset(dev, i);
6478 else
6479 btrfs_dev_stat_reset(dev, i);
6480 }
6481 } else {
6482 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6483 if (stats->nr_items > i)
6484 stats->values[i] = btrfs_dev_stat_read(dev, i);
6485 }
6486 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6487 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6488 return 0;
6489}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006490
6491int btrfs_scratch_superblock(struct btrfs_device *device)
6492{
6493 struct buffer_head *bh;
6494 struct btrfs_super_block *disk_super;
6495
6496 bh = btrfs_read_dev_super(device->bdev);
6497 if (!bh)
6498 return -EINVAL;
6499 disk_super = (struct btrfs_super_block *)bh->b_data;
6500
6501 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6502 set_buffer_dirty(bh);
6503 sync_dirty_buffer(bh);
6504 brelse(bh);
6505
6506 return 0;
6507}