blob: 7c538f65214bb23f5a813009c793ac0b3ab3cf71 [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);
162 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
163 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
164
165 return dev;
166}
167
Chris Masona1b32a52008-09-05 16:09:51 -0400168static noinline struct btrfs_device *__find_device(struct list_head *head,
169 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400170{
171 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400172
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500173 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400174 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400175 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400176 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400177 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400178 }
179 return NULL;
180}
181
Chris Masona1b32a52008-09-05 16:09:51 -0400182static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400183{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400184 struct btrfs_fs_devices *fs_devices;
185
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500186 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400187 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
188 return fs_devices;
189 }
190 return NULL;
191}
192
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100193static int
194btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
195 int flush, struct block_device **bdev,
196 struct buffer_head **bh)
197{
198 int ret;
199
200 *bdev = blkdev_get_by_path(device_path, flags, holder);
201
202 if (IS_ERR(*bdev)) {
203 ret = PTR_ERR(*bdev);
Frank Holtonefe120a2013-12-20 11:37:06 -0500204 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100205 goto error;
206 }
207
208 if (flush)
209 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
210 ret = set_blocksize(*bdev, 4096);
211 if (ret) {
212 blkdev_put(*bdev, flags);
213 goto error;
214 }
215 invalidate_bdev(*bdev);
216 *bh = btrfs_read_dev_super(*bdev);
217 if (!*bh) {
218 ret = -EINVAL;
219 blkdev_put(*bdev, flags);
220 goto error;
221 }
222
223 return 0;
224
225error:
226 *bdev = NULL;
227 *bh = NULL;
228 return ret;
229}
230
Chris Masonffbd5172009-04-20 15:50:09 -0400231static void requeue_list(struct btrfs_pending_bios *pending_bios,
232 struct bio *head, struct bio *tail)
233{
234
235 struct bio *old_head;
236
237 old_head = pending_bios->head;
238 pending_bios->head = head;
239 if (pending_bios->tail)
240 tail->bi_next = old_head;
241 else
242 pending_bios->tail = tail;
243}
244
Chris Mason8b712842008-06-11 16:50:36 -0400245/*
246 * we try to collect pending bios for a device so we don't get a large
247 * number of procs sending bios down to the same device. This greatly
248 * improves the schedulers ability to collect and merge the bios.
249 *
250 * But, it also turns into a long list of bios to process and that is sure
251 * to eventually make the worker thread block. The solution here is to
252 * make some progress and then put this work struct back at the end of
253 * the list if the block device is congested. This way, multiple devices
254 * can make progress from a single worker thread.
255 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100256static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400257{
258 struct bio *pending;
259 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400260 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400261 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400262 struct bio *tail;
263 struct bio *cur;
264 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400265 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400266 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400267 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400268 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400269 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000270 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400271 struct blk_plug plug;
272
273 /*
274 * this function runs all the bios we've collected for
275 * a particular device. We don't want to wander off to
276 * another device without first sending all of these down.
277 * So, setup a plug here and finish it off before we return
278 */
279 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400280
Chris Masonbedf7622009-04-03 10:32:58 -0400281 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400282 fs_info = device->dev_root->fs_info;
283 limit = btrfs_async_submit_limit(fs_info);
284 limit = limit * 2 / 3;
285
Chris Mason8b712842008-06-11 16:50:36 -0400286loop:
287 spin_lock(&device->io_lock);
288
Chris Masona6837052009-02-04 09:19:41 -0500289loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400290 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400291
Chris Mason8b712842008-06-11 16:50:36 -0400292 /* take all the bios off the list at once and process them
293 * later on (without the lock held). But, remember the
294 * tail and other pointers so the bios can be properly reinserted
295 * into the list if we hit congestion
296 */
Chris Masond84275c2009-06-09 15:39:08 -0400297 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400298 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400299 force_reg = 1;
300 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400301 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400302 force_reg = 0;
303 }
Chris Masonffbd5172009-04-20 15:50:09 -0400304
305 pending = pending_bios->head;
306 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400307 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400308
309 /*
310 * if pending was null this time around, no bios need processing
311 * at all and we can stop. Otherwise it'll loop back up again
312 * and do an additional check so no bios are missed.
313 *
314 * device->running_pending is used to synchronize with the
315 * schedule_bio code.
316 */
Chris Masonffbd5172009-04-20 15:50:09 -0400317 if (device->pending_sync_bios.head == NULL &&
318 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400319 again = 0;
320 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400321 } else {
322 again = 1;
323 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400324 }
Chris Masonffbd5172009-04-20 15:50:09 -0400325
326 pending_bios->head = NULL;
327 pending_bios->tail = NULL;
328
Chris Mason8b712842008-06-11 16:50:36 -0400329 spin_unlock(&device->io_lock);
330
Chris Masond3977122009-01-05 21:25:51 -0500331 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400332
333 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400334 /* we want to work on both lists, but do more bios on the
335 * sync list than the regular list
336 */
337 if ((num_run > 32 &&
338 pending_bios != &device->pending_sync_bios &&
339 device->pending_sync_bios.head) ||
340 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
341 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400342 spin_lock(&device->io_lock);
343 requeue_list(pending_bios, pending, tail);
344 goto loop_lock;
345 }
346
Chris Mason8b712842008-06-11 16:50:36 -0400347 cur = pending;
348 pending = pending->bi_next;
349 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400350
Josef Bacik66657b32012-08-01 15:36:24 -0400351 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400352 waitqueue_active(&fs_info->async_submit_wait))
353 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400354
355 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400356
Chris Mason2ab1ba62011-08-04 14:28:36 -0400357 /*
358 * if we're doing the sync list, record that our
359 * plug has some sync requests on it
360 *
361 * If we're doing the regular list and there are
362 * sync requests sitting around, unplug before
363 * we add more
364 */
365 if (pending_bios == &device->pending_sync_bios) {
366 sync_pending = 1;
367 } else if (sync_pending) {
368 blk_finish_plug(&plug);
369 blk_start_plug(&plug);
370 sync_pending = 0;
371 }
372
Stefan Behrens21adbd52011-11-09 13:44:05 +0100373 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400374 num_run++;
375 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100376 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400377 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400378
379 /*
380 * we made progress, there is more work to do and the bdi
381 * is now congested. Back off and let other work structs
382 * run instead
383 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400384 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500385 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400386 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400387
Chris Masonb765ead2009-04-03 10:27:10 -0400388 ioc = current->io_context;
389
390 /*
391 * the main goal here is that we don't want to
392 * block if we're going to be able to submit
393 * more requests without blocking.
394 *
395 * This code does two great things, it pokes into
396 * the elevator code from a filesystem _and_
397 * it makes assumptions about how batching works.
398 */
399 if (ioc && ioc->nr_batch_requests > 0 &&
400 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
401 (last_waited == 0 ||
402 ioc->last_waited == last_waited)) {
403 /*
404 * we want to go through our batch of
405 * requests and stop. So, we copy out
406 * the ioc->last_waited time and test
407 * against it before looping
408 */
409 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100410 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400411 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400412 continue;
413 }
Chris Mason8b712842008-06-11 16:50:36 -0400414 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400415 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500416 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400417
418 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800419 btrfs_queue_work(fs_info->submit_workers,
420 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400421 goto done;
422 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500423 /* unplug every 64 requests just for good measure */
424 if (batch_run % 64 == 0) {
425 blk_finish_plug(&plug);
426 blk_start_plug(&plug);
427 sync_pending = 0;
428 }
Chris Mason8b712842008-06-11 16:50:36 -0400429 }
Chris Masonffbd5172009-04-20 15:50:09 -0400430
Chris Mason51684082010-03-10 15:33:32 -0500431 cond_resched();
432 if (again)
433 goto loop;
434
435 spin_lock(&device->io_lock);
436 if (device->pending_bios.head || device->pending_sync_bios.head)
437 goto loop_lock;
438 spin_unlock(&device->io_lock);
439
Chris Mason8b712842008-06-11 16:50:36 -0400440done:
Chris Mason211588a2011-04-19 20:12:40 -0400441 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400442}
443
Christoph Hellwigb2950862008-12-02 09:54:17 -0500444static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400445{
446 struct btrfs_device *device;
447
448 device = container_of(work, struct btrfs_device, work);
449 run_scheduled_bios(device);
450}
451
David Sterba60999ca2014-03-26 18:26:36 +0100452/*
453 * Add new device to list of registered devices
454 *
455 * Returns:
456 * 1 - first time device is seen
457 * 0 - device already known
458 * < 0 - error
459 */
Chris Masona1b32a52008-09-05 16:09:51 -0400460static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400461 struct btrfs_super_block *disk_super,
462 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
463{
464 struct btrfs_device *device;
465 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400466 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100467 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400468 u64 found_transid = btrfs_super_generation(disk_super);
469
470 fs_devices = find_fsid(disk_super->fsid);
471 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300472 fs_devices = alloc_fs_devices(disk_super->fsid);
473 if (IS_ERR(fs_devices))
474 return PTR_ERR(fs_devices);
475
Chris Mason8a4b83c2008-03-24 15:02:07 -0400476 list_add(&fs_devices->list, &fs_uuids);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400477 fs_devices->latest_devid = devid;
478 fs_devices->latest_trans = found_transid;
Ilya Dryomov2208a372013-08-12 14:33:03 +0300479
Chris Mason8a4b83c2008-03-24 15:02:07 -0400480 device = NULL;
481 } else {
Chris Masona4437552008-04-18 10:29:38 -0400482 device = __find_device(&fs_devices->devices, devid,
483 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400484 }
485 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500486 if (fs_devices->opened)
487 return -EBUSY;
488
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300489 device = btrfs_alloc_device(NULL, &devid,
490 disk_super->dev_item.uuid);
491 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400492 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300493 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400494 }
Josef Bacik606686e2012-06-04 14:03:51 -0400495
496 name = rcu_string_strdup(path, GFP_NOFS);
497 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400498 kfree(device);
499 return -ENOMEM;
500 }
Josef Bacik606686e2012-06-04 14:03:51 -0400501 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200502
Chris Masone5e9a522009-06-10 15:17:02 -0400503 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000504 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100505 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400506 mutex_unlock(&fs_devices->device_list_mutex);
507
David Sterba60999ca2014-03-26 18:26:36 +0100508 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500509 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400510 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800511 /*
512 * When FS is already mounted.
513 * 1. If you are here and if the device->name is NULL that
514 * means this device was missing at time of FS mount.
515 * 2. If you are here and if the device->name is different
516 * from 'path' that means either
517 * a. The same device disappeared and reappeared with
518 * different name. or
519 * b. The missing-disk-which-was-replaced, has
520 * reappeared now.
521 *
522 * We must allow 1 and 2a above. But 2b would be a spurious
523 * and unintentional.
524 *
525 * Further in case of 1 and 2a above, the disk at 'path'
526 * would have missed some transaction when it was away and
527 * in case of 2a the stale bdev has to be updated as well.
528 * 2b must not be allowed at all time.
529 */
530
531 /*
532 * As of now don't allow update to btrfs_fs_device through
533 * the btrfs dev scan cli, after FS has been mounted.
534 */
535 if (fs_devices->opened)
536 return -EBUSY;
537
Josef Bacik606686e2012-06-04 14:03:51 -0400538 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000539 if (!name)
540 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400541 rcu_string_free(device->name);
542 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500543 if (device->missing) {
544 fs_devices->missing_devices--;
545 device->missing = 0;
546 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400547 }
548
549 if (found_transid > fs_devices->latest_trans) {
550 fs_devices->latest_devid = devid;
551 fs_devices->latest_trans = found_transid;
552 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400553 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100554
555 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400556}
557
Yan Zhenge4404d62008-12-12 10:03:26 -0500558static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
559{
560 struct btrfs_fs_devices *fs_devices;
561 struct btrfs_device *device;
562 struct btrfs_device *orig_dev;
563
Ilya Dryomov2208a372013-08-12 14:33:03 +0300564 fs_devices = alloc_fs_devices(orig->fsid);
565 if (IS_ERR(fs_devices))
566 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500567
Yan Zhenge4404d62008-12-12 10:03:26 -0500568 fs_devices->latest_devid = orig->latest_devid;
569 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400570 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500571
Xiao Guangrong46224702011-04-20 10:08:47 +0000572 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500573 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400574 struct rcu_string *name;
575
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300576 device = btrfs_alloc_device(NULL, &orig_dev->devid,
577 orig_dev->uuid);
578 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500579 goto error;
580
Josef Bacik606686e2012-06-04 14:03:51 -0400581 /*
582 * This is ok to do without rcu read locked because we hold the
583 * uuid mutex so nothing we touch in here is going to disappear.
584 */
Anand Jaine755f782014-06-30 17:12:47 +0800585 if (orig_dev->name) {
586 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
587 if (!name) {
588 kfree(device);
589 goto error;
590 }
591 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400592 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500593
Yan Zhenge4404d62008-12-12 10:03:26 -0500594 list_add(&device->dev_list, &fs_devices->devices);
595 device->fs_devices = fs_devices;
596 fs_devices->num_devices++;
597 }
598 return fs_devices;
599error:
600 free_fs_devices(fs_devices);
601 return ERR_PTR(-ENOMEM);
602}
603
Stefan Behrens8dabb742012-11-06 13:15:27 +0100604void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
605 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400606{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500607 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400608
Chris Masona6b0d5c2012-02-20 20:53:43 -0500609 struct block_device *latest_bdev = NULL;
610 u64 latest_devid = 0;
611 u64 latest_transid = 0;
612
Chris Masondfe25022008-05-13 13:46:40 -0400613 mutex_lock(&uuid_mutex);
614again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000615 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500616 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500617 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100618 if (!device->is_tgtdev_for_dev_replace &&
619 (!latest_transid ||
620 device->generation > latest_transid)) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500621 latest_devid = device->devid;
622 latest_transid = device->generation;
623 latest_bdev = device->bdev;
624 }
Yan Zheng2b820322008-11-17 21:11:30 -0500625 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500626 }
Yan Zheng2b820322008-11-17 21:11:30 -0500627
Stefan Behrens8dabb742012-11-06 13:15:27 +0100628 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
629 /*
630 * In the first step, keep the device which has
631 * the correct fsid and the devid that is used
632 * for the dev_replace procedure.
633 * In the second step, the dev_replace state is
634 * read from the device tree and it is known
635 * whether the procedure is really active or
636 * not, which means whether this device is
637 * used or whether it should be removed.
638 */
639 if (step == 0 || device->is_tgtdev_for_dev_replace) {
640 continue;
641 }
642 }
Yan Zheng2b820322008-11-17 21:11:30 -0500643 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100644 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500645 device->bdev = NULL;
646 fs_devices->open_devices--;
647 }
648 if (device->writeable) {
649 list_del_init(&device->dev_alloc_list);
650 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100651 if (!device->is_tgtdev_for_dev_replace)
652 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500653 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500654 list_del_init(&device->dev_list);
655 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400656 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500657 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400658 }
Yan Zheng2b820322008-11-17 21:11:30 -0500659
660 if (fs_devices->seed) {
661 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500662 goto again;
663 }
664
Chris Masona6b0d5c2012-02-20 20:53:43 -0500665 fs_devices->latest_bdev = latest_bdev;
666 fs_devices->latest_devid = latest_devid;
667 fs_devices->latest_trans = latest_transid;
668
Chris Masondfe25022008-05-13 13:46:40 -0400669 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400670}
Chris Masona0af4692008-05-13 16:03:06 -0400671
Xiao Guangrong1f781602011-04-20 10:09:16 +0000672static void __free_device(struct work_struct *work)
673{
674 struct btrfs_device *device;
675
676 device = container_of(work, struct btrfs_device, rcu_work);
677
678 if (device->bdev)
679 blkdev_put(device->bdev, device->mode);
680
Josef Bacik606686e2012-06-04 14:03:51 -0400681 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000682 kfree(device);
683}
684
685static void free_device(struct rcu_head *head)
686{
687 struct btrfs_device *device;
688
689 device = container_of(head, struct btrfs_device, rcu);
690
691 INIT_WORK(&device->rcu_work, __free_device);
692 schedule_work(&device->rcu_work);
693}
694
Yan Zheng2b820322008-11-17 21:11:30 -0500695static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400697 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500698
Yan Zheng2b820322008-11-17 21:11:30 -0500699 if (--fs_devices->opened > 0)
700 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400701
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000702 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500703 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000704 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400705 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000706
707 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400708 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000709
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300710 if (device->writeable &&
711 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500712 list_del_init(&device->dev_alloc_list);
713 fs_devices->rw_devices--;
714 }
715
Josef Bacikd5e20032011-08-04 14:52:27 +0000716 if (device->can_discard)
717 fs_devices->num_can_discard--;
Josef Bacik726551e2013-08-26 13:45:53 -0400718 if (device->missing)
719 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000720
Ilya Dryomova1e87802013-08-12 14:33:04 +0300721 new_device = btrfs_alloc_device(NULL, &device->devid,
722 device->uuid);
723 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400724
725 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400726 if (device->name) {
727 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300728 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400729 rcu_assign_pointer(new_device->name, name);
730 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300731
Xiao Guangrong1f781602011-04-20 10:09:16 +0000732 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300733 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000734
735 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400736 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000737 mutex_unlock(&fs_devices->device_list_mutex);
738
Yan Zhenge4404d62008-12-12 10:03:26 -0500739 WARN_ON(fs_devices->open_devices);
740 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500741 fs_devices->opened = 0;
742 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500743
Chris Mason8a4b83c2008-03-24 15:02:07 -0400744 return 0;
745}
746
Yan Zheng2b820322008-11-17 21:11:30 -0500747int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
748{
Yan Zhenge4404d62008-12-12 10:03:26 -0500749 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500750 int ret;
751
752 mutex_lock(&uuid_mutex);
753 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500754 if (!fs_devices->opened) {
755 seed_devices = fs_devices->seed;
756 fs_devices->seed = NULL;
757 }
Yan Zheng2b820322008-11-17 21:11:30 -0500758 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500759
760 while (seed_devices) {
761 fs_devices = seed_devices;
762 seed_devices = fs_devices->seed;
763 __btrfs_close_devices(fs_devices);
764 free_fs_devices(fs_devices);
765 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000766 /*
767 * Wait for rcu kworkers under __btrfs_close_devices
768 * to finish all blkdev_puts so device is really
769 * free when umount is done.
770 */
771 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500772 return ret;
773}
774
Yan Zhenge4404d62008-12-12 10:03:26 -0500775static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
776 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400777{
Josef Bacikd5e20032011-08-04 14:52:27 +0000778 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400779 struct block_device *bdev;
780 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400781 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400782 struct block_device *latest_bdev = NULL;
783 struct buffer_head *bh;
784 struct btrfs_super_block *disk_super;
785 u64 latest_devid = 0;
786 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400787 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500788 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400789 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400790
Tejun Heod4d77622010-11-13 11:55:18 +0100791 flags |= FMODE_EXCL;
792
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500793 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400794 if (device->bdev)
795 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400796 if (!device->name)
797 continue;
798
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000799 /* Just open everything we can; ignore failures here */
800 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
801 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100802 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400803
804 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000805 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400806 if (devid != device->devid)
807 goto error_brelse;
808
Yan Zheng2b820322008-11-17 21:11:30 -0500809 if (memcmp(device->uuid, disk_super->dev_item.uuid,
810 BTRFS_UUID_SIZE))
811 goto error_brelse;
812
813 device->generation = btrfs_super_generation(disk_super);
814 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400815 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500816 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400817 latest_bdev = bdev;
818 }
819
Yan Zheng2b820322008-11-17 21:11:30 -0500820 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
821 device->writeable = 0;
822 } else {
823 device->writeable = !bdev_read_only(bdev);
824 seeding = 0;
825 }
826
Josef Bacikd5e20032011-08-04 14:52:27 +0000827 q = bdev_get_queue(bdev);
828 if (blk_queue_discard(q)) {
829 device->can_discard = 1;
830 fs_devices->num_can_discard++;
831 }
832
Chris Mason8a4b83c2008-03-24 15:02:07 -0400833 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400834 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500835 device->mode = flags;
836
Chris Masonc2898112009-06-10 09:51:32 -0400837 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
838 fs_devices->rotating = 1;
839
Chris Masona0af4692008-05-13 16:03:06 -0400840 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300841 if (device->writeable &&
842 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500843 fs_devices->rw_devices++;
844 list_add(&device->dev_alloc_list,
845 &fs_devices->alloc_list);
846 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000847 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400848 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400849
Chris Masona0af4692008-05-13 16:03:06 -0400850error_brelse:
851 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100852 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400853 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400854 }
Chris Masona0af4692008-05-13 16:03:06 -0400855 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300856 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400857 goto out;
858 }
Yan Zheng2b820322008-11-17 21:11:30 -0500859 fs_devices->seeding = seeding;
860 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400861 fs_devices->latest_bdev = latest_bdev;
862 fs_devices->latest_devid = latest_devid;
863 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500864 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400865out:
Yan Zheng2b820322008-11-17 21:11:30 -0500866 return ret;
867}
868
869int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500870 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500871{
872 int ret;
873
874 mutex_lock(&uuid_mutex);
875 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500876 fs_devices->opened++;
877 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500878 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500879 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500880 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400881 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400882 return ret;
883}
884
David Sterba6f60cbd2013-02-15 11:31:02 -0700885/*
886 * Look for a btrfs signature on a device. This may be called out of the mount path
887 * and we are not allowed to call set_blocksize during the scan. The superblock
888 * is read via pagecache
889 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500890int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400891 struct btrfs_fs_devices **fs_devices_ret)
892{
893 struct btrfs_super_block *disk_super;
894 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700895 struct page *page;
896 void *p;
897 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400898 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400899 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400900 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700901 u64 bytenr;
902 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400903
David Sterba6f60cbd2013-02-15 11:31:02 -0700904 /*
905 * we would like to check all the supers, but that would make
906 * a btrfs mount succeed after a mkfs from a different FS.
907 * So, we need to add a special mount option to scan for
908 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
909 */
910 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100911 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500912 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700913
914 bdev = blkdev_get_by_path(path, flags, holder);
915
916 if (IS_ERR(bdev)) {
917 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100918 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700919 }
920
921 /* make sure our super fits in the device */
922 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
923 goto error_bdev_put;
924
925 /* make sure our super fits in the page */
926 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
927 goto error_bdev_put;
928
929 /* make sure our super doesn't straddle pages on disk */
930 index = bytenr >> PAGE_CACHE_SHIFT;
931 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
932 goto error_bdev_put;
933
934 /* pull in the page with our super */
935 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
936 index, GFP_NOFS);
937
938 if (IS_ERR_OR_NULL(page))
939 goto error_bdev_put;
940
941 p = kmap(page);
942
943 /* align our pointer to the offset of the super block */
944 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
945
946 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800947 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700948 goto error_unmap;
949
Xiao Guangronga3438322010-01-06 11:48:18 +0000950 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400951 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400952 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700953
Chris Mason8a4b83c2008-03-24 15:02:07 -0400954 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100955 if (ret > 0) {
956 if (disk_super->label[0]) {
957 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
958 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
959 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
960 } else {
961 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
962 }
963
964 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
965 ret = 0;
966 }
Josef Bacik02db0842012-06-21 16:03:58 -0400967 if (!ret && fs_devices_ret)
968 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700969
970error_unmap:
971 kunmap(page);
972 page_cache_release(page);
973
974error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100975 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400976error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100977 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400978 return ret;
979}
Chris Mason0b86a832008-03-24 15:01:56 -0400980
Miao Xie6d07bce2011-01-05 10:07:31 +0000981/* helper to account the used device space in the range */
982int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
983 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400984{
985 struct btrfs_key key;
986 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000987 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500988 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000989 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400990 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000991 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400992 struct extent_buffer *l;
993
Miao Xie6d07bce2011-01-05 10:07:31 +0000994 *length = 0;
995
Stefan Behrens63a212a2012-11-05 18:29:28 +0100996 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000997 return 0;
998
Yan Zheng2b820322008-11-17 21:11:30 -0500999 path = btrfs_alloc_path();
1000 if (!path)
1001 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001002 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001003
Chris Mason0b86a832008-03-24 15:01:56 -04001004 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001005 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001006 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001007
1008 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001009 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001010 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001011 if (ret > 0) {
1012 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1013 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001014 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001015 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001016
Chris Mason0b86a832008-03-24 15:01:56 -04001017 while (1) {
1018 l = path->nodes[0];
1019 slot = path->slots[0];
1020 if (slot >= btrfs_header_nritems(l)) {
1021 ret = btrfs_next_leaf(root, path);
1022 if (ret == 0)
1023 continue;
1024 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001025 goto out;
1026
1027 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001028 }
1029 btrfs_item_key_to_cpu(l, &key, slot);
1030
1031 if (key.objectid < device->devid)
1032 goto next;
1033
1034 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001035 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001036
Chris Masond3977122009-01-05 21:25:51 -05001037 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001038 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001039
Chris Mason0b86a832008-03-24 15:01:56 -04001040 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001041 extent_end = key.offset + btrfs_dev_extent_length(l,
1042 dev_extent);
1043 if (key.offset <= start && extent_end > end) {
1044 *length = end - start + 1;
1045 break;
1046 } else if (key.offset <= start && extent_end > start)
1047 *length += extent_end - start;
1048 else if (key.offset > start && extent_end <= end)
1049 *length += extent_end - key.offset;
1050 else if (key.offset > start && key.offset <= end) {
1051 *length += end - key.offset + 1;
1052 break;
1053 } else if (key.offset > end)
1054 break;
1055
1056next:
1057 path->slots[0]++;
1058 }
1059 ret = 0;
1060out:
1061 btrfs_free_path(path);
1062 return ret;
1063}
1064
Josef Bacik6df9a952013-06-27 13:22:46 -04001065static int contains_pending_extent(struct btrfs_trans_handle *trans,
1066 struct btrfs_device *device,
1067 u64 *start, u64 len)
1068{
1069 struct extent_map *em;
1070 int ret = 0;
1071
1072 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
1073 struct map_lookup *map;
1074 int i;
1075
1076 map = (struct map_lookup *)em->bdev;
1077 for (i = 0; i < map->num_stripes; i++) {
1078 if (map->stripes[i].dev != device)
1079 continue;
1080 if (map->stripes[i].physical >= *start + len ||
1081 map->stripes[i].physical + em->orig_block_len <=
1082 *start)
1083 continue;
1084 *start = map->stripes[i].physical +
1085 em->orig_block_len;
1086 ret = 1;
1087 }
1088 }
1089
1090 return ret;
1091}
1092
1093
Chris Mason0b86a832008-03-24 15:01:56 -04001094/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001095 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001096 * @device: the device which we search the free space in
1097 * @num_bytes: the size of the free space that we need
1098 * @start: store the start of the free space.
1099 * @len: the size of the free space. that we find, or the size of the max
1100 * free space if we don't find suitable free space
1101 *
Chris Mason0b86a832008-03-24 15:01:56 -04001102 * this uses a pretty simple search, the expectation is that it is
1103 * called very infrequently and that a given device has a small number
1104 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001105 *
1106 * @start is used to store the start of the free space if we find. But if we
1107 * don't find suitable free space, it will be used to store the start position
1108 * of the max free space.
1109 *
1110 * @len is used to store the size of the free space that we find.
1111 * But if we don't find suitable free space, it is used to store the size of
1112 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001113 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001114int find_free_dev_extent(struct btrfs_trans_handle *trans,
1115 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001116 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001117{
1118 struct btrfs_key key;
1119 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001120 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001121 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001122 u64 hole_size;
1123 u64 max_hole_start;
1124 u64 max_hole_size;
1125 u64 extent_end;
1126 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001127 u64 search_end = device->total_bytes;
1128 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001129 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001130 struct extent_buffer *l;
1131
Chris Mason0b86a832008-03-24 15:01:56 -04001132 /* FIXME use last free of some kind */
1133
1134 /* we don't want to overwrite the superblock on the drive,
1135 * so we make sure to start at an offset of at least 1MB
1136 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001137 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001138
Josef Bacik6df9a952013-06-27 13:22:46 -04001139 path = btrfs_alloc_path();
1140 if (!path)
1141 return -ENOMEM;
1142again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001143 max_hole_start = search_start;
1144 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001145 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001146
Stefan Behrens63a212a2012-11-05 18:29:28 +01001147 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001148 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001149 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001150 }
1151
Miao Xie7bfc8372011-01-05 10:07:26 +00001152 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001153 path->search_commit_root = 1;
1154 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001155
Chris Mason0b86a832008-03-24 15:01:56 -04001156 key.objectid = device->devid;
1157 key.offset = search_start;
1158 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001159
Li Zefan125ccb02011-12-08 15:07:24 +08001160 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001161 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001162 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001163 if (ret > 0) {
1164 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1165 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001166 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001167 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001168
Chris Mason0b86a832008-03-24 15:01:56 -04001169 while (1) {
1170 l = path->nodes[0];
1171 slot = path->slots[0];
1172 if (slot >= btrfs_header_nritems(l)) {
1173 ret = btrfs_next_leaf(root, path);
1174 if (ret == 0)
1175 continue;
1176 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001177 goto out;
1178
1179 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001180 }
1181 btrfs_item_key_to_cpu(l, &key, slot);
1182
1183 if (key.objectid < device->devid)
1184 goto next;
1185
1186 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001187 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001188
Chris Mason0b86a832008-03-24 15:01:56 -04001189 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1190 goto next;
1191
Miao Xie7bfc8372011-01-05 10:07:26 +00001192 if (key.offset > search_start) {
1193 hole_size = key.offset - search_start;
1194
Josef Bacik6df9a952013-06-27 13:22:46 -04001195 /*
1196 * Have to check before we set max_hole_start, otherwise
1197 * we could end up sending back this offset anyway.
1198 */
1199 if (contains_pending_extent(trans, device,
1200 &search_start,
1201 hole_size))
1202 hole_size = 0;
1203
Miao Xie7bfc8372011-01-05 10:07:26 +00001204 if (hole_size > max_hole_size) {
1205 max_hole_start = search_start;
1206 max_hole_size = hole_size;
1207 }
1208
1209 /*
1210 * If this free space is greater than which we need,
1211 * it must be the max free space that we have found
1212 * until now, so max_hole_start must point to the start
1213 * of this free space and the length of this free space
1214 * is stored in max_hole_size. Thus, we return
1215 * max_hole_start and max_hole_size and go back to the
1216 * caller.
1217 */
1218 if (hole_size >= num_bytes) {
1219 ret = 0;
1220 goto out;
1221 }
1222 }
1223
Chris Mason0b86a832008-03-24 15:01:56 -04001224 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001225 extent_end = key.offset + btrfs_dev_extent_length(l,
1226 dev_extent);
1227 if (extent_end > search_start)
1228 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001229next:
1230 path->slots[0]++;
1231 cond_resched();
1232 }
Chris Mason0b86a832008-03-24 15:01:56 -04001233
liubo38c01b92011-08-02 02:39:03 +00001234 /*
1235 * At this point, search_start should be the end of
1236 * allocated dev extents, and when shrinking the device,
1237 * search_end may be smaller than search_start.
1238 */
1239 if (search_end > search_start)
1240 hole_size = search_end - search_start;
1241
Miao Xie7bfc8372011-01-05 10:07:26 +00001242 if (hole_size > max_hole_size) {
1243 max_hole_start = search_start;
1244 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001245 }
Chris Mason0b86a832008-03-24 15:01:56 -04001246
Josef Bacik6df9a952013-06-27 13:22:46 -04001247 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1248 btrfs_release_path(path);
1249 goto again;
1250 }
1251
Miao Xie7bfc8372011-01-05 10:07:26 +00001252 /* See above. */
1253 if (hole_size < num_bytes)
1254 ret = -ENOSPC;
1255 else
1256 ret = 0;
1257
1258out:
Yan Zheng2b820322008-11-17 21:11:30 -05001259 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001260 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001261 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001262 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001263 return ret;
1264}
1265
Christoph Hellwigb2950862008-12-02 09:54:17 -05001266static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001267 struct btrfs_device *device,
1268 u64 start)
1269{
1270 int ret;
1271 struct btrfs_path *path;
1272 struct btrfs_root *root = device->dev_root;
1273 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001274 struct btrfs_key found_key;
1275 struct extent_buffer *leaf = NULL;
1276 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001277
1278 path = btrfs_alloc_path();
1279 if (!path)
1280 return -ENOMEM;
1281
1282 key.objectid = device->devid;
1283 key.offset = start;
1284 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001285again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001286 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001287 if (ret > 0) {
1288 ret = btrfs_previous_item(root, path, key.objectid,
1289 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001290 if (ret)
1291 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001292 leaf = path->nodes[0];
1293 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1294 extent = btrfs_item_ptr(leaf, path->slots[0],
1295 struct btrfs_dev_extent);
1296 BUG_ON(found_key.offset > start || found_key.offset +
1297 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001298 key = found_key;
1299 btrfs_release_path(path);
1300 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001301 } else if (ret == 0) {
1302 leaf = path->nodes[0];
1303 extent = btrfs_item_ptr(leaf, path->slots[0],
1304 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001305 } else {
1306 btrfs_error(root->fs_info, ret, "Slot search failed");
1307 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001308 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001309
Josef Bacik2bf64752011-09-26 17:12:22 -04001310 if (device->bytes_used > 0) {
1311 u64 len = btrfs_dev_extent_length(leaf, extent);
1312 device->bytes_used -= len;
1313 spin_lock(&root->fs_info->free_chunk_lock);
1314 root->fs_info->free_chunk_space += len;
1315 spin_unlock(&root->fs_info->free_chunk_lock);
1316 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001317 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001318 if (ret) {
1319 btrfs_error(root->fs_info, ret,
1320 "Failed to remove dev extent item");
1321 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001322out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001323 btrfs_free_path(path);
1324 return ret;
1325}
1326
Eric Sandeen48a3b632013-04-25 20:41:01 +00001327static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1328 struct btrfs_device *device,
1329 u64 chunk_tree, u64 chunk_objectid,
1330 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001331{
1332 int ret;
1333 struct btrfs_path *path;
1334 struct btrfs_root *root = device->dev_root;
1335 struct btrfs_dev_extent *extent;
1336 struct extent_buffer *leaf;
1337 struct btrfs_key key;
1338
Chris Masondfe25022008-05-13 13:46:40 -04001339 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001340 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001341 path = btrfs_alloc_path();
1342 if (!path)
1343 return -ENOMEM;
1344
Chris Mason0b86a832008-03-24 15:01:56 -04001345 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001346 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001347 key.type = BTRFS_DEV_EXTENT_KEY;
1348 ret = btrfs_insert_empty_item(trans, root, path, &key,
1349 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001350 if (ret)
1351 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001352
1353 leaf = path->nodes[0];
1354 extent = btrfs_item_ptr(leaf, path->slots[0],
1355 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001356 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1357 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1358 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1359
1360 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001361 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001362
Chris Mason0b86a832008-03-24 15:01:56 -04001363 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1364 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001365out:
Chris Mason0b86a832008-03-24 15:01:56 -04001366 btrfs_free_path(path);
1367 return ret;
1368}
1369
Josef Bacik6df9a952013-06-27 13:22:46 -04001370static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001371{
Josef Bacik6df9a952013-06-27 13:22:46 -04001372 struct extent_map_tree *em_tree;
1373 struct extent_map *em;
1374 struct rb_node *n;
1375 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001376
Josef Bacik6df9a952013-06-27 13:22:46 -04001377 em_tree = &fs_info->mapping_tree.map_tree;
1378 read_lock(&em_tree->lock);
1379 n = rb_last(&em_tree->map);
1380 if (n) {
1381 em = rb_entry(n, struct extent_map, rb_node);
1382 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001383 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001384 read_unlock(&em_tree->lock);
1385
Chris Mason0b86a832008-03-24 15:01:56 -04001386 return ret;
1387}
1388
Ilya Dryomov53f10652013-08-12 14:33:01 +03001389static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1390 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001391{
1392 int ret;
1393 struct btrfs_key key;
1394 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001395 struct btrfs_path *path;
1396
Yan Zheng2b820322008-11-17 21:11:30 -05001397 path = btrfs_alloc_path();
1398 if (!path)
1399 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001400
1401 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1402 key.type = BTRFS_DEV_ITEM_KEY;
1403 key.offset = (u64)-1;
1404
Ilya Dryomov53f10652013-08-12 14:33:01 +03001405 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001406 if (ret < 0)
1407 goto error;
1408
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001409 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001410
Ilya Dryomov53f10652013-08-12 14:33:01 +03001411 ret = btrfs_previous_item(fs_info->chunk_root, path,
1412 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001413 BTRFS_DEV_ITEM_KEY);
1414 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001415 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001416 } else {
1417 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1418 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001419 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001420 }
1421 ret = 0;
1422error:
Yan Zheng2b820322008-11-17 21:11:30 -05001423 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001424 return ret;
1425}
1426
1427/*
1428 * the device information is stored in the chunk root
1429 * the btrfs_device struct should be fully filled in
1430 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001431static int btrfs_add_device(struct btrfs_trans_handle *trans,
1432 struct btrfs_root *root,
1433 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001434{
1435 int ret;
1436 struct btrfs_path *path;
1437 struct btrfs_dev_item *dev_item;
1438 struct extent_buffer *leaf;
1439 struct btrfs_key key;
1440 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001441
1442 root = root->fs_info->chunk_root;
1443
1444 path = btrfs_alloc_path();
1445 if (!path)
1446 return -ENOMEM;
1447
Chris Mason0b86a832008-03-24 15:01:56 -04001448 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1449 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001450 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001451
1452 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001453 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001454 if (ret)
1455 goto out;
1456
1457 leaf = path->nodes[0];
1458 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1459
1460 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001461 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001462 btrfs_set_device_type(leaf, dev_item, device->type);
1463 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1464 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1465 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001466 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1467 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001468 btrfs_set_device_group(leaf, dev_item, 0);
1469 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1470 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001471 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001472
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001473 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001474 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001475 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001476 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001477 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001478
Yan Zheng2b820322008-11-17 21:11:30 -05001479 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001480out:
1481 btrfs_free_path(path);
1482 return ret;
1483}
Chris Mason8f18cf12008-04-25 16:53:30 -04001484
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001485/*
1486 * Function to update ctime/mtime for a given device path.
1487 * Mainly used for ctime/mtime based probe like libblkid.
1488 */
1489static void update_dev_time(char *path_name)
1490{
1491 struct file *filp;
1492
1493 filp = filp_open(path_name, O_RDWR, 0);
1494 if (!filp)
1495 return;
1496 file_update_time(filp);
1497 filp_close(filp, NULL);
1498 return;
1499}
1500
Chris Masona061fc82008-05-07 11:43:44 -04001501static int btrfs_rm_dev_item(struct btrfs_root *root,
1502 struct btrfs_device *device)
1503{
1504 int ret;
1505 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001506 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001507 struct btrfs_trans_handle *trans;
1508
1509 root = root->fs_info->chunk_root;
1510
1511 path = btrfs_alloc_path();
1512 if (!path)
1513 return -ENOMEM;
1514
Yan, Zhenga22285a2010-05-16 10:48:46 -04001515 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001516 if (IS_ERR(trans)) {
1517 btrfs_free_path(path);
1518 return PTR_ERR(trans);
1519 }
Chris Masona061fc82008-05-07 11:43:44 -04001520 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1521 key.type = BTRFS_DEV_ITEM_KEY;
1522 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001523 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001524
1525 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1526 if (ret < 0)
1527 goto out;
1528
1529 if (ret > 0) {
1530 ret = -ENOENT;
1531 goto out;
1532 }
1533
1534 ret = btrfs_del_item(trans, root, path);
1535 if (ret)
1536 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001537out:
1538 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001539 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001540 btrfs_commit_transaction(trans, root);
1541 return ret;
1542}
1543
1544int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1545{
1546 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001547 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001548 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001549 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001550 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001551 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001552 u64 all_avail;
1553 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001554 u64 num_devices;
1555 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001556 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001557 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001558 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001559
Chris Masona061fc82008-05-07 11:43:44 -04001560 mutex_lock(&uuid_mutex);
1561
Miao Xiede98ced2013-01-29 10:13:12 +00001562 do {
1563 seq = read_seqbegin(&root->fs_info->profiles_lock);
1564
1565 all_avail = root->fs_info->avail_data_alloc_bits |
1566 root->fs_info->avail_system_alloc_bits |
1567 root->fs_info->avail_metadata_alloc_bits;
1568 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001569
Stefan Behrens8dabb742012-11-06 13:15:27 +01001570 num_devices = root->fs_info->fs_devices->num_devices;
1571 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1572 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1573 WARN_ON(num_devices < 1);
1574 num_devices--;
1575 }
1576 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1577
1578 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001579 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001580 goto out;
1581 }
1582
Stefan Behrens8dabb742012-11-06 13:15:27 +01001583 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001584 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001585 goto out;
1586 }
1587
David Woodhouse53b381b2013-01-29 18:40:14 -05001588 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1589 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001590 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001591 goto out;
1592 }
1593 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1594 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001595 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001596 goto out;
1597 }
1598
Chris Masondfe25022008-05-13 13:46:40 -04001599 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001600 struct list_head *devices;
1601 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001602
Chris Masondfe25022008-05-13 13:46:40 -04001603 device = NULL;
1604 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001605 /*
1606 * It is safe to read the devices since the volume_mutex
1607 * is held.
1608 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001609 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001610 if (tmp->in_fs_metadata &&
1611 !tmp->is_tgtdev_for_dev_replace &&
1612 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001613 device = tmp;
1614 break;
1615 }
1616 }
1617 bdev = NULL;
1618 bh = NULL;
1619 disk_super = NULL;
1620 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001621 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001622 goto out;
1623 }
Chris Masondfe25022008-05-13 13:46:40 -04001624 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001625 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001626 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001627 root->fs_info->bdev_holder, 0,
1628 &bdev, &bh);
1629 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001630 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001631 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001632 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001633 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001634 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001635 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001636 if (!device) {
1637 ret = -ENOENT;
1638 goto error_brelse;
1639 }
Chris Masondfe25022008-05-13 13:46:40 -04001640 }
Yan Zheng2b820322008-11-17 21:11:30 -05001641
Stefan Behrens63a212a2012-11-05 18:29:28 +01001642 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001643 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001644 goto error_brelse;
1645 }
1646
Yan Zheng2b820322008-11-17 21:11:30 -05001647 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001648 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001649 goto error_brelse;
1650 }
1651
1652 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001653 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001654 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001655 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001656 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001657 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001658 }
Chris Masona061fc82008-05-07 11:43:44 -04001659
Carey Underwoodd7901552013-03-04 16:37:06 -06001660 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001661 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001662 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001663 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001664 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001665
Stefan Behrens63a212a2012-11-05 18:29:28 +01001666 /*
1667 * TODO: the superblock still includes this device in its num_devices
1668 * counter although write_all_supers() is not locked out. This
1669 * could give a filesystem state which requires a degraded mount.
1670 */
Chris Masona061fc82008-05-07 11:43:44 -04001671 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1672 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001673 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001674
Josef Bacik2bf64752011-09-26 17:12:22 -04001675 spin_lock(&root->fs_info->free_chunk_lock);
1676 root->fs_info->free_chunk_space = device->total_bytes -
1677 device->bytes_used;
1678 spin_unlock(&root->fs_info->free_chunk_lock);
1679
Yan Zheng2b820322008-11-17 21:11:30 -05001680 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001681 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001682
1683 /*
1684 * the device list mutex makes sure that we don't change
1685 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001686 * the device supers. Whoever is writing all supers, should
1687 * lock the device list mutex before getting the number of
1688 * devices in the super block (super_copy). Conversely,
1689 * whoever updates the number of devices in the super block
1690 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001691 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001692
1693 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001694 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001695 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001696
Yan Zhenge4404d62008-12-12 10:03:26 -05001697 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001698 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001699
Chris Masoncd02dca2010-12-13 14:56:23 -05001700 if (device->missing)
1701 root->fs_info->fs_devices->missing_devices--;
1702
Yan Zheng2b820322008-11-17 21:11:30 -05001703 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1704 struct btrfs_device, dev_list);
1705 if (device->bdev == root->fs_info->sb->s_bdev)
1706 root->fs_info->sb->s_bdev = next_device->bdev;
1707 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1708 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1709
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001710 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001711 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001712 /* remove sysfs entry */
1713 btrfs_kobj_rm_device(root->fs_info, device);
1714 }
Anand Jain99994cd2014-06-03 11:36:00 +08001715
Xiao Guangrong1f781602011-04-20 10:09:16 +00001716 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001717
David Sterba6c417612011-04-13 15:41:04 +02001718 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1719 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001720 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001721
Xiao Guangrong1f781602011-04-20 10:09:16 +00001722 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001723 struct btrfs_fs_devices *fs_devices;
1724 fs_devices = root->fs_info->fs_devices;
1725 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001726 if (fs_devices->seed == cur_devices) {
1727 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001728 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001729 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001730 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001731 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001732 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001733 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001734 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001735 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001736 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001737 }
1738
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001739 root->fs_info->num_tolerated_disk_barrier_failures =
1740 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1741
Yan Zheng2b820322008-11-17 21:11:30 -05001742 /*
1743 * at this point, the device is zero sized. We want to
1744 * remove it from the devices list and zero out the old super
1745 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001746 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001747 u64 bytenr;
1748 int i;
1749
Chris Masondfe25022008-05-13 13:46:40 -04001750 /* make sure this device isn't detected as part of
1751 * the FS anymore
1752 */
1753 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1754 set_buffer_dirty(bh);
1755 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001756
1757 /* clear the mirror copies of super block on the disk
1758 * being removed, 0th copy is been taken care above and
1759 * the below would take of the rest
1760 */
1761 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1762 bytenr = btrfs_sb_offset(i);
1763 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1764 i_size_read(bdev->bd_inode))
1765 break;
1766
1767 brelse(bh);
1768 bh = __bread(bdev, bytenr / 4096,
1769 BTRFS_SUPER_INFO_SIZE);
1770 if (!bh)
1771 continue;
1772
1773 disk_super = (struct btrfs_super_block *)bh->b_data;
1774
1775 if (btrfs_super_bytenr(disk_super) != bytenr ||
1776 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1777 continue;
1778 }
1779 memset(&disk_super->magic, 0,
1780 sizeof(disk_super->magic));
1781 set_buffer_dirty(bh);
1782 sync_dirty_buffer(bh);
1783 }
Chris Masondfe25022008-05-13 13:46:40 -04001784 }
Chris Masona061fc82008-05-07 11:43:44 -04001785
Chris Masona061fc82008-05-07 11:43:44 -04001786 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001787
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001788 if (bdev) {
1789 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001790 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001791
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001792 /* Update ctime/mtime for device path for libblkid */
1793 update_dev_time(device_path);
1794 }
1795
Chris Masona061fc82008-05-07 11:43:44 -04001796error_brelse:
1797 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001798 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001799 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001800out:
1801 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001802 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001803error_undo:
1804 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001805 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001806 list_add(&device->dev_alloc_list,
1807 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001808 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001809 root->fs_info->fs_devices->rw_devices++;
1810 }
1811 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001812}
1813
Stefan Behrense93c89c2012-11-05 17:33:06 +01001814void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1815 struct btrfs_device *srcdev)
1816{
1817 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001818
Stefan Behrense93c89c2012-11-05 17:33:06 +01001819 list_del_rcu(&srcdev->dev_list);
1820 list_del_rcu(&srcdev->dev_alloc_list);
1821 fs_info->fs_devices->num_devices--;
1822 if (srcdev->missing) {
1823 fs_info->fs_devices->missing_devices--;
1824 fs_info->fs_devices->rw_devices++;
1825 }
1826 if (srcdev->can_discard)
1827 fs_info->fs_devices->num_can_discard--;
Ilya Dryomov13572722013-10-02 20:41:01 +03001828 if (srcdev->bdev) {
Stefan Behrense93c89c2012-11-05 17:33:06 +01001829 fs_info->fs_devices->open_devices--;
1830
Ilya Dryomov13572722013-10-02 20:41:01 +03001831 /* zero out the old super */
1832 btrfs_scratch_superblock(srcdev);
1833 }
1834
Stefan Behrense93c89c2012-11-05 17:33:06 +01001835 call_rcu(&srcdev->rcu, free_device);
1836}
1837
1838void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1839 struct btrfs_device *tgtdev)
1840{
1841 struct btrfs_device *next_device;
1842
1843 WARN_ON(!tgtdev);
1844 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1845 if (tgtdev->bdev) {
1846 btrfs_scratch_superblock(tgtdev);
1847 fs_info->fs_devices->open_devices--;
1848 }
1849 fs_info->fs_devices->num_devices--;
1850 if (tgtdev->can_discard)
1851 fs_info->fs_devices->num_can_discard++;
1852
1853 next_device = list_entry(fs_info->fs_devices->devices.next,
1854 struct btrfs_device, dev_list);
1855 if (tgtdev->bdev == fs_info->sb->s_bdev)
1856 fs_info->sb->s_bdev = next_device->bdev;
1857 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1858 fs_info->fs_devices->latest_bdev = next_device->bdev;
1859 list_del_rcu(&tgtdev->dev_list);
1860
1861 call_rcu(&tgtdev->rcu, free_device);
1862
1863 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1864}
1865
Eric Sandeen48a3b632013-04-25 20:41:01 +00001866static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1867 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001868{
1869 int ret = 0;
1870 struct btrfs_super_block *disk_super;
1871 u64 devid;
1872 u8 *dev_uuid;
1873 struct block_device *bdev;
1874 struct buffer_head *bh;
1875
1876 *device = NULL;
1877 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1878 root->fs_info->bdev_holder, 0, &bdev, &bh);
1879 if (ret)
1880 return ret;
1881 disk_super = (struct btrfs_super_block *)bh->b_data;
1882 devid = btrfs_stack_device_id(&disk_super->dev_item);
1883 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001884 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001885 disk_super->fsid);
1886 brelse(bh);
1887 if (!*device)
1888 ret = -ENOENT;
1889 blkdev_put(bdev, FMODE_READ);
1890 return ret;
1891}
1892
1893int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1894 char *device_path,
1895 struct btrfs_device **device)
1896{
1897 *device = NULL;
1898 if (strcmp(device_path, "missing") == 0) {
1899 struct list_head *devices;
1900 struct btrfs_device *tmp;
1901
1902 devices = &root->fs_info->fs_devices->devices;
1903 /*
1904 * It is safe to read the devices since the volume_mutex
1905 * is held by the caller.
1906 */
1907 list_for_each_entry(tmp, devices, dev_list) {
1908 if (tmp->in_fs_metadata && !tmp->bdev) {
1909 *device = tmp;
1910 break;
1911 }
1912 }
1913
1914 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001915 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001916 return -ENOENT;
1917 }
1918
1919 return 0;
1920 } else {
1921 return btrfs_find_device_by_path(root, device_path, device);
1922 }
1923}
1924
Yan Zheng2b820322008-11-17 21:11:30 -05001925/*
1926 * does all the dirty work required for changing file system's UUID.
1927 */
Li Zefan125ccb02011-12-08 15:07:24 +08001928static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001929{
1930 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1931 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001932 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001933 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001934 struct btrfs_device *device;
1935 u64 super_flags;
1936
1937 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001938 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001939 return -EINVAL;
1940
Ilya Dryomov2208a372013-08-12 14:33:03 +03001941 seed_devices = __alloc_fs_devices();
1942 if (IS_ERR(seed_devices))
1943 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001944
Yan Zhenge4404d62008-12-12 10:03:26 -05001945 old_devices = clone_fs_devices(fs_devices);
1946 if (IS_ERR(old_devices)) {
1947 kfree(seed_devices);
1948 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001949 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001950
Yan Zheng2b820322008-11-17 21:11:30 -05001951 list_add(&old_devices->list, &fs_uuids);
1952
Yan Zhenge4404d62008-12-12 10:03:26 -05001953 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1954 seed_devices->opened = 1;
1955 INIT_LIST_HEAD(&seed_devices->devices);
1956 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001957 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001958
1959 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001960 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1961 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001962
Yan Zhenge4404d62008-12-12 10:03:26 -05001963 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1964 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1965 device->fs_devices = seed_devices;
1966 }
1967
Yan Zheng2b820322008-11-17 21:11:30 -05001968 fs_devices->seeding = 0;
1969 fs_devices->num_devices = 0;
1970 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001971 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001972
1973 generate_random_uuid(fs_devices->fsid);
1974 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1975 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01001976 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1977
Yan Zheng2b820322008-11-17 21:11:30 -05001978 super_flags = btrfs_super_flags(disk_super) &
1979 ~BTRFS_SUPER_FLAG_SEEDING;
1980 btrfs_set_super_flags(disk_super, super_flags);
1981
1982 return 0;
1983}
1984
1985/*
1986 * strore the expected generation for seed devices in device items.
1987 */
1988static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1989 struct btrfs_root *root)
1990{
1991 struct btrfs_path *path;
1992 struct extent_buffer *leaf;
1993 struct btrfs_dev_item *dev_item;
1994 struct btrfs_device *device;
1995 struct btrfs_key key;
1996 u8 fs_uuid[BTRFS_UUID_SIZE];
1997 u8 dev_uuid[BTRFS_UUID_SIZE];
1998 u64 devid;
1999 int ret;
2000
2001 path = btrfs_alloc_path();
2002 if (!path)
2003 return -ENOMEM;
2004
2005 root = root->fs_info->chunk_root;
2006 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2007 key.offset = 0;
2008 key.type = BTRFS_DEV_ITEM_KEY;
2009
2010 while (1) {
2011 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2012 if (ret < 0)
2013 goto error;
2014
2015 leaf = path->nodes[0];
2016next_slot:
2017 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2018 ret = btrfs_next_leaf(root, path);
2019 if (ret > 0)
2020 break;
2021 if (ret < 0)
2022 goto error;
2023 leaf = path->nodes[0];
2024 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002025 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002026 continue;
2027 }
2028
2029 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2030 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2031 key.type != BTRFS_DEV_ITEM_KEY)
2032 break;
2033
2034 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2035 struct btrfs_dev_item);
2036 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002037 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002038 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002039 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002040 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002041 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2042 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002043 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002044
2045 if (device->fs_devices->seeding) {
2046 btrfs_set_device_generation(leaf, dev_item,
2047 device->generation);
2048 btrfs_mark_buffer_dirty(leaf);
2049 }
2050
2051 path->slots[0]++;
2052 goto next_slot;
2053 }
2054 ret = 0;
2055error:
2056 btrfs_free_path(path);
2057 return ret;
2058}
2059
Chris Mason788f20e2008-04-28 15:29:42 -04002060int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2061{
Josef Bacikd5e20032011-08-04 14:52:27 +00002062 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002063 struct btrfs_trans_handle *trans;
2064 struct btrfs_device *device;
2065 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002066 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002067 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002068 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04002069 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002070 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002071 int ret = 0;
2072
Yan Zheng2b820322008-11-17 21:11:30 -05002073 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002074 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002075
Li Zefana5d16332011-12-07 20:08:40 -05002076 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002077 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002078 if (IS_ERR(bdev))
2079 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002080
Yan Zheng2b820322008-11-17 21:11:30 -05002081 if (root->fs_info->fs_devices->seeding) {
2082 seeding_dev = 1;
2083 down_write(&sb->s_umount);
2084 mutex_lock(&uuid_mutex);
2085 }
2086
Chris Mason8c8bee12008-09-29 11:19:10 -04002087 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002088
Chris Mason788f20e2008-04-28 15:29:42 -04002089 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002090
2091 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002092 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002093 if (device->bdev == bdev) {
2094 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002095 mutex_unlock(
2096 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002097 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002098 }
2099 }
Liu Bod25628b2012-11-14 14:35:30 +00002100 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002101
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002102 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2103 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002104 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002105 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002106 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002107 }
2108
Josef Bacik606686e2012-06-04 14:03:51 -04002109 name = rcu_string_strdup(device_path, GFP_NOFS);
2110 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002111 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002112 ret = -ENOMEM;
2113 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002114 }
Josef Bacik606686e2012-06-04 14:03:51 -04002115 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002116
Yan, Zhenga22285a2010-05-16 10:48:46 -04002117 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002118 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002119 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002120 kfree(device);
2121 ret = PTR_ERR(trans);
2122 goto error;
2123 }
2124
Yan Zheng2b820322008-11-17 21:11:30 -05002125 lock_chunks(root);
2126
Josef Bacikd5e20032011-08-04 14:52:27 +00002127 q = bdev_get_queue(bdev);
2128 if (blk_queue_discard(q))
2129 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002130 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002131 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002132 device->io_width = root->sectorsize;
2133 device->io_align = root->sectorsize;
2134 device->sector_size = root->sectorsize;
2135 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002136 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002137 device->dev_root = root->fs_info->dev_root;
2138 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002139 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002140 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002141 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002142 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002143 set_blocksize(device->bdev, 4096);
2144
Yan Zheng2b820322008-11-17 21:11:30 -05002145 if (seeding_dev) {
2146 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002147 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002148 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002149 }
2150
2151 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002152
Chris Masone5e9a522009-06-10 15:17:02 -04002153 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002154 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002155 list_add(&device->dev_alloc_list,
2156 &root->fs_info->fs_devices->alloc_list);
2157 root->fs_info->fs_devices->num_devices++;
2158 root->fs_info->fs_devices->open_devices++;
2159 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002160 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002161 if (device->can_discard)
2162 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002163 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2164
Josef Bacik2bf64752011-09-26 17:12:22 -04002165 spin_lock(&root->fs_info->free_chunk_lock);
2166 root->fs_info->free_chunk_space += device->total_bytes;
2167 spin_unlock(&root->fs_info->free_chunk_lock);
2168
Chris Masonc2898112009-06-10 09:51:32 -04002169 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2170 root->fs_info->fs_devices->rotating = 1;
2171
David Sterba6c417612011-04-13 15:41:04 +02002172 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2173 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002174 total_bytes + device->total_bytes);
2175
David Sterba6c417612011-04-13 15:41:04 +02002176 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2177 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002178 total_bytes + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002179
2180 /* add sysfs device entry */
2181 btrfs_kobj_add_device(root->fs_info, device);
2182
Chris Masone5e9a522009-06-10 15:17:02 -04002183 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002184
Yan Zheng2b820322008-11-17 21:11:30 -05002185 if (seeding_dev) {
Anand Jainb2373f22014-06-03 11:36:03 +08002186 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002187 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002188 if (ret) {
2189 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002190 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002191 }
Yan Zheng2b820322008-11-17 21:11:30 -05002192 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002193 if (ret) {
2194 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002195 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002196 }
Anand Jainb2373f22014-06-03 11:36:03 +08002197
2198 /* Sprouting would change fsid of the mounted root,
2199 * so rename the fsid on the sysfs
2200 */
2201 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2202 root->fs_info->fsid);
2203 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2204 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002205 } else {
2206 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002207 if (ret) {
2208 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002209 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002210 }
Yan Zheng2b820322008-11-17 21:11:30 -05002211 }
2212
Chris Mason913d9522009-03-10 13:17:18 -04002213 /*
2214 * we've got more storage, clear any full flags on the space
2215 * infos
2216 */
2217 btrfs_clear_space_info_full(root->fs_info);
2218
Chris Mason7d9eb122008-07-08 14:19:17 -04002219 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002220 root->fs_info->num_tolerated_disk_barrier_failures =
2221 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002222 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002223
2224 if (seeding_dev) {
2225 mutex_unlock(&uuid_mutex);
2226 up_write(&sb->s_umount);
2227
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002228 if (ret) /* transaction commit */
2229 return ret;
2230
Yan Zheng2b820322008-11-17 21:11:30 -05002231 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002232 if (ret < 0)
2233 btrfs_error(root->fs_info, ret,
2234 "Failed to relocate sys chunks after "
2235 "device initialization. This can be fixed "
2236 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002237 trans = btrfs_attach_transaction(root);
2238 if (IS_ERR(trans)) {
2239 if (PTR_ERR(trans) == -ENOENT)
2240 return 0;
2241 return PTR_ERR(trans);
2242 }
2243 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002244 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002245
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002246 /* Update ctime/mtime for libblkid */
2247 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002248 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002249
2250error_trans:
2251 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002252 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002253 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002254 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002255 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002256error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002257 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002258 if (seeding_dev) {
2259 mutex_unlock(&uuid_mutex);
2260 up_write(&sb->s_umount);
2261 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002262 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002263}
2264
Stefan Behrense93c89c2012-11-05 17:33:06 +01002265int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2266 struct btrfs_device **device_out)
2267{
2268 struct request_queue *q;
2269 struct btrfs_device *device;
2270 struct block_device *bdev;
2271 struct btrfs_fs_info *fs_info = root->fs_info;
2272 struct list_head *devices;
2273 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002274 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002275 int ret = 0;
2276
2277 *device_out = NULL;
2278 if (fs_info->fs_devices->seeding)
2279 return -EINVAL;
2280
2281 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2282 fs_info->bdev_holder);
2283 if (IS_ERR(bdev))
2284 return PTR_ERR(bdev);
2285
2286 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2287
2288 devices = &fs_info->fs_devices->devices;
2289 list_for_each_entry(device, devices, dev_list) {
2290 if (device->bdev == bdev) {
2291 ret = -EEXIST;
2292 goto error;
2293 }
2294 }
2295
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002296 device = btrfs_alloc_device(NULL, &devid, NULL);
2297 if (IS_ERR(device)) {
2298 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002299 goto error;
2300 }
2301
2302 name = rcu_string_strdup(device_path, GFP_NOFS);
2303 if (!name) {
2304 kfree(device);
2305 ret = -ENOMEM;
2306 goto error;
2307 }
2308 rcu_assign_pointer(device->name, name);
2309
2310 q = bdev_get_queue(bdev);
2311 if (blk_queue_discard(q))
2312 device->can_discard = 1;
2313 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2314 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002315 device->generation = 0;
2316 device->io_width = root->sectorsize;
2317 device->io_align = root->sectorsize;
2318 device->sector_size = root->sectorsize;
2319 device->total_bytes = i_size_read(bdev->bd_inode);
2320 device->disk_total_bytes = device->total_bytes;
2321 device->dev_root = fs_info->dev_root;
2322 device->bdev = bdev;
2323 device->in_fs_metadata = 1;
2324 device->is_tgtdev_for_dev_replace = 1;
2325 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002326 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002327 set_blocksize(device->bdev, 4096);
2328 device->fs_devices = fs_info->fs_devices;
2329 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2330 fs_info->fs_devices->num_devices++;
2331 fs_info->fs_devices->open_devices++;
2332 if (device->can_discard)
2333 fs_info->fs_devices->num_can_discard++;
2334 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2335
2336 *device_out = device;
2337 return ret;
2338
2339error:
2340 blkdev_put(bdev, FMODE_EXCL);
2341 return ret;
2342}
2343
2344void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2345 struct btrfs_device *tgtdev)
2346{
2347 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2348 tgtdev->io_width = fs_info->dev_root->sectorsize;
2349 tgtdev->io_align = fs_info->dev_root->sectorsize;
2350 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2351 tgtdev->dev_root = fs_info->dev_root;
2352 tgtdev->in_fs_metadata = 1;
2353}
2354
Chris Masond3977122009-01-05 21:25:51 -05002355static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2356 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002357{
2358 int ret;
2359 struct btrfs_path *path;
2360 struct btrfs_root *root;
2361 struct btrfs_dev_item *dev_item;
2362 struct extent_buffer *leaf;
2363 struct btrfs_key key;
2364
2365 root = device->dev_root->fs_info->chunk_root;
2366
2367 path = btrfs_alloc_path();
2368 if (!path)
2369 return -ENOMEM;
2370
2371 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2372 key.type = BTRFS_DEV_ITEM_KEY;
2373 key.offset = device->devid;
2374
2375 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2376 if (ret < 0)
2377 goto out;
2378
2379 if (ret > 0) {
2380 ret = -ENOENT;
2381 goto out;
2382 }
2383
2384 leaf = path->nodes[0];
2385 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2386
2387 btrfs_set_device_id(leaf, dev_item, device->devid);
2388 btrfs_set_device_type(leaf, dev_item, device->type);
2389 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2390 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2391 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002392 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002393 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2394 btrfs_mark_buffer_dirty(leaf);
2395
2396out:
2397 btrfs_free_path(path);
2398 return ret;
2399}
2400
Chris Mason7d9eb122008-07-08 14:19:17 -04002401static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002402 struct btrfs_device *device, u64 new_size)
2403{
2404 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002405 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002406 u64 old_total = btrfs_super_total_bytes(super_copy);
2407 u64 diff = new_size - device->total_bytes;
2408
Yan Zheng2b820322008-11-17 21:11:30 -05002409 if (!device->writeable)
2410 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002411 if (new_size <= device->total_bytes ||
2412 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002413 return -EINVAL;
2414
Chris Mason8f18cf12008-04-25 16:53:30 -04002415 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002416 device->fs_devices->total_rw_bytes += diff;
2417
2418 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002419 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002420 btrfs_clear_space_info_full(device->dev_root->fs_info);
2421
Chris Mason8f18cf12008-04-25 16:53:30 -04002422 return btrfs_update_device(trans, device);
2423}
2424
Chris Mason7d9eb122008-07-08 14:19:17 -04002425int btrfs_grow_device(struct btrfs_trans_handle *trans,
2426 struct btrfs_device *device, u64 new_size)
2427{
2428 int ret;
2429 lock_chunks(device->dev_root);
2430 ret = __btrfs_grow_device(trans, device, new_size);
2431 unlock_chunks(device->dev_root);
2432 return ret;
2433}
2434
Chris Mason8f18cf12008-04-25 16:53:30 -04002435static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2436 struct btrfs_root *root,
2437 u64 chunk_tree, u64 chunk_objectid,
2438 u64 chunk_offset)
2439{
2440 int ret;
2441 struct btrfs_path *path;
2442 struct btrfs_key key;
2443
2444 root = root->fs_info->chunk_root;
2445 path = btrfs_alloc_path();
2446 if (!path)
2447 return -ENOMEM;
2448
2449 key.objectid = chunk_objectid;
2450 key.offset = chunk_offset;
2451 key.type = BTRFS_CHUNK_ITEM_KEY;
2452
2453 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002454 if (ret < 0)
2455 goto out;
2456 else if (ret > 0) { /* Logic error or corruption */
2457 btrfs_error(root->fs_info, -ENOENT,
2458 "Failed lookup while freeing chunk.");
2459 ret = -ENOENT;
2460 goto out;
2461 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002462
2463 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002464 if (ret < 0)
2465 btrfs_error(root->fs_info, ret,
2466 "Failed to delete chunk item.");
2467out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002468 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002469 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002470}
2471
Christoph Hellwigb2950862008-12-02 09:54:17 -05002472static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002473 chunk_offset)
2474{
David Sterba6c417612011-04-13 15:41:04 +02002475 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002476 struct btrfs_disk_key *disk_key;
2477 struct btrfs_chunk *chunk;
2478 u8 *ptr;
2479 int ret = 0;
2480 u32 num_stripes;
2481 u32 array_size;
2482 u32 len = 0;
2483 u32 cur;
2484 struct btrfs_key key;
2485
2486 array_size = btrfs_super_sys_array_size(super_copy);
2487
2488 ptr = super_copy->sys_chunk_array;
2489 cur = 0;
2490
2491 while (cur < array_size) {
2492 disk_key = (struct btrfs_disk_key *)ptr;
2493 btrfs_disk_key_to_cpu(&key, disk_key);
2494
2495 len = sizeof(*disk_key);
2496
2497 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2498 chunk = (struct btrfs_chunk *)(ptr + len);
2499 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2500 len += btrfs_chunk_item_size(num_stripes);
2501 } else {
2502 ret = -EIO;
2503 break;
2504 }
2505 if (key.objectid == chunk_objectid &&
2506 key.offset == chunk_offset) {
2507 memmove(ptr, ptr + len, array_size - (cur + len));
2508 array_size -= len;
2509 btrfs_set_super_sys_array_size(super_copy, array_size);
2510 } else {
2511 ptr += len;
2512 cur += len;
2513 }
2514 }
2515 return ret;
2516}
2517
Christoph Hellwigb2950862008-12-02 09:54:17 -05002518static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002519 u64 chunk_tree, u64 chunk_objectid,
2520 u64 chunk_offset)
2521{
2522 struct extent_map_tree *em_tree;
2523 struct btrfs_root *extent_root;
2524 struct btrfs_trans_handle *trans;
2525 struct extent_map *em;
2526 struct map_lookup *map;
2527 int ret;
2528 int i;
2529
2530 root = root->fs_info->chunk_root;
2531 extent_root = root->fs_info->extent_root;
2532 em_tree = &root->fs_info->mapping_tree.map_tree;
2533
Josef Bacikba1bf482009-09-11 16:11:19 -04002534 ret = btrfs_can_relocate(extent_root, chunk_offset);
2535 if (ret)
2536 return -ENOSPC;
2537
Chris Mason8f18cf12008-04-25 16:53:30 -04002538 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002539 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002540 if (ret)
2541 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002542
Yan, Zhenga22285a2010-05-16 10:48:46 -04002543 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002544 if (IS_ERR(trans)) {
2545 ret = PTR_ERR(trans);
2546 btrfs_std_error(root->fs_info, ret);
2547 return ret;
2548 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002549
Chris Mason7d9eb122008-07-08 14:19:17 -04002550 lock_chunks(root);
2551
Chris Mason8f18cf12008-04-25 16:53:30 -04002552 /*
2553 * step two, delete the device extents and the
2554 * chunk tree entries
2555 */
Chris Mason890871b2009-09-02 16:24:52 -04002556 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002557 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002558 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002559
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002560 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002561 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002562 map = (struct map_lookup *)em->bdev;
2563
2564 for (i = 0; i < map->num_stripes; i++) {
2565 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2566 map->stripes[i].physical);
2567 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002568
Chris Masondfe25022008-05-13 13:46:40 -04002569 if (map->stripes[i].dev) {
2570 ret = btrfs_update_device(trans, map->stripes[i].dev);
2571 BUG_ON(ret);
2572 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002573 }
2574 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2575 chunk_offset);
2576
2577 BUG_ON(ret);
2578
liubo1abe9b82011-03-24 11:18:59 +00002579 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2580
Chris Mason8f18cf12008-04-25 16:53:30 -04002581 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2582 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2583 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002584 }
2585
Zheng Yan1a40e232008-09-26 10:09:34 -04002586 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2587 BUG_ON(ret);
2588
Chris Mason890871b2009-09-02 16:24:52 -04002589 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002590 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002591 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002592
Chris Mason8f18cf12008-04-25 16:53:30 -04002593 /* once for the tree */
2594 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002595 /* once for us */
2596 free_extent_map(em);
2597
Chris Mason7d9eb122008-07-08 14:19:17 -04002598 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002599 btrfs_end_transaction(trans, root);
2600 return 0;
2601}
2602
Yan Zheng2b820322008-11-17 21:11:30 -05002603static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2604{
2605 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2606 struct btrfs_path *path;
2607 struct extent_buffer *leaf;
2608 struct btrfs_chunk *chunk;
2609 struct btrfs_key key;
2610 struct btrfs_key found_key;
2611 u64 chunk_tree = chunk_root->root_key.objectid;
2612 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002613 bool retried = false;
2614 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002615 int ret;
2616
2617 path = btrfs_alloc_path();
2618 if (!path)
2619 return -ENOMEM;
2620
Josef Bacikba1bf482009-09-11 16:11:19 -04002621again:
Yan Zheng2b820322008-11-17 21:11:30 -05002622 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2623 key.offset = (u64)-1;
2624 key.type = BTRFS_CHUNK_ITEM_KEY;
2625
2626 while (1) {
2627 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2628 if (ret < 0)
2629 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002630 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002631
2632 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2633 key.type);
2634 if (ret < 0)
2635 goto error;
2636 if (ret > 0)
2637 break;
2638
2639 leaf = path->nodes[0];
2640 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2641
2642 chunk = btrfs_item_ptr(leaf, path->slots[0],
2643 struct btrfs_chunk);
2644 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002645 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002646
2647 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2648 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2649 found_key.objectid,
2650 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002651 if (ret == -ENOSPC)
2652 failed++;
2653 else if (ret)
2654 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002655 }
2656
2657 if (found_key.offset == 0)
2658 break;
2659 key.offset = found_key.offset - 1;
2660 }
2661 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002662 if (failed && !retried) {
2663 failed = 0;
2664 retried = true;
2665 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302666 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002667 ret = -ENOSPC;
2668 }
Yan Zheng2b820322008-11-17 21:11:30 -05002669error:
2670 btrfs_free_path(path);
2671 return ret;
2672}
2673
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002674static int insert_balance_item(struct btrfs_root *root,
2675 struct btrfs_balance_control *bctl)
2676{
2677 struct btrfs_trans_handle *trans;
2678 struct btrfs_balance_item *item;
2679 struct btrfs_disk_balance_args disk_bargs;
2680 struct btrfs_path *path;
2681 struct extent_buffer *leaf;
2682 struct btrfs_key key;
2683 int ret, err;
2684
2685 path = btrfs_alloc_path();
2686 if (!path)
2687 return -ENOMEM;
2688
2689 trans = btrfs_start_transaction(root, 0);
2690 if (IS_ERR(trans)) {
2691 btrfs_free_path(path);
2692 return PTR_ERR(trans);
2693 }
2694
2695 key.objectid = BTRFS_BALANCE_OBJECTID;
2696 key.type = BTRFS_BALANCE_ITEM_KEY;
2697 key.offset = 0;
2698
2699 ret = btrfs_insert_empty_item(trans, root, path, &key,
2700 sizeof(*item));
2701 if (ret)
2702 goto out;
2703
2704 leaf = path->nodes[0];
2705 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2706
2707 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2708
2709 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2710 btrfs_set_balance_data(leaf, item, &disk_bargs);
2711 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2712 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2713 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2714 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2715
2716 btrfs_set_balance_flags(leaf, item, bctl->flags);
2717
2718 btrfs_mark_buffer_dirty(leaf);
2719out:
2720 btrfs_free_path(path);
2721 err = btrfs_commit_transaction(trans, root);
2722 if (err && !ret)
2723 ret = err;
2724 return ret;
2725}
2726
2727static int del_balance_item(struct btrfs_root *root)
2728{
2729 struct btrfs_trans_handle *trans;
2730 struct btrfs_path *path;
2731 struct btrfs_key key;
2732 int ret, err;
2733
2734 path = btrfs_alloc_path();
2735 if (!path)
2736 return -ENOMEM;
2737
2738 trans = btrfs_start_transaction(root, 0);
2739 if (IS_ERR(trans)) {
2740 btrfs_free_path(path);
2741 return PTR_ERR(trans);
2742 }
2743
2744 key.objectid = BTRFS_BALANCE_OBJECTID;
2745 key.type = BTRFS_BALANCE_ITEM_KEY;
2746 key.offset = 0;
2747
2748 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2749 if (ret < 0)
2750 goto out;
2751 if (ret > 0) {
2752 ret = -ENOENT;
2753 goto out;
2754 }
2755
2756 ret = btrfs_del_item(trans, root, path);
2757out:
2758 btrfs_free_path(path);
2759 err = btrfs_commit_transaction(trans, root);
2760 if (err && !ret)
2761 ret = err;
2762 return ret;
2763}
2764
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002765/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002766 * This is a heuristic used to reduce the number of chunks balanced on
2767 * resume after balance was interrupted.
2768 */
2769static void update_balance_args(struct btrfs_balance_control *bctl)
2770{
2771 /*
2772 * Turn on soft mode for chunk types that were being converted.
2773 */
2774 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2775 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2776 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2777 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2778 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2779 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2780
2781 /*
2782 * Turn on usage filter if is not already used. The idea is
2783 * that chunks that we have already balanced should be
2784 * reasonably full. Don't do it for chunks that are being
2785 * converted - that will keep us from relocating unconverted
2786 * (albeit full) chunks.
2787 */
2788 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2789 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2790 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2791 bctl->data.usage = 90;
2792 }
2793 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2794 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2795 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2796 bctl->sys.usage = 90;
2797 }
2798 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2799 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2800 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2801 bctl->meta.usage = 90;
2802 }
2803}
2804
2805/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002806 * Should be called with both balance and volume mutexes held to
2807 * serialize other volume operations (add_dev/rm_dev/resize) with
2808 * restriper. Same goes for unset_balance_control.
2809 */
2810static void set_balance_control(struct btrfs_balance_control *bctl)
2811{
2812 struct btrfs_fs_info *fs_info = bctl->fs_info;
2813
2814 BUG_ON(fs_info->balance_ctl);
2815
2816 spin_lock(&fs_info->balance_lock);
2817 fs_info->balance_ctl = bctl;
2818 spin_unlock(&fs_info->balance_lock);
2819}
2820
2821static void unset_balance_control(struct btrfs_fs_info *fs_info)
2822{
2823 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2824
2825 BUG_ON(!fs_info->balance_ctl);
2826
2827 spin_lock(&fs_info->balance_lock);
2828 fs_info->balance_ctl = NULL;
2829 spin_unlock(&fs_info->balance_lock);
2830
2831 kfree(bctl);
2832}
2833
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002834/*
2835 * Balance filters. Return 1 if chunk should be filtered out
2836 * (should not be balanced).
2837 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002838static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002839 struct btrfs_balance_args *bargs)
2840{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002841 chunk_type = chunk_to_extended(chunk_type) &
2842 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002843
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002844 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002845 return 0;
2846
2847 return 1;
2848}
2849
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002850static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2851 struct btrfs_balance_args *bargs)
2852{
2853 struct btrfs_block_group_cache *cache;
2854 u64 chunk_used, user_thresh;
2855 int ret = 1;
2856
2857 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2858 chunk_used = btrfs_block_group_used(&cache->item);
2859
Ilya Dryomova105bb82013-01-21 15:15:56 +02002860 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002861 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002862 else if (bargs->usage > 100)
2863 user_thresh = cache->key.offset;
2864 else
2865 user_thresh = div_factor_fine(cache->key.offset,
2866 bargs->usage);
2867
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002868 if (chunk_used < user_thresh)
2869 ret = 0;
2870
2871 btrfs_put_block_group(cache);
2872 return ret;
2873}
2874
Ilya Dryomov409d4042012-01-16 22:04:47 +02002875static int chunk_devid_filter(struct extent_buffer *leaf,
2876 struct btrfs_chunk *chunk,
2877 struct btrfs_balance_args *bargs)
2878{
2879 struct btrfs_stripe *stripe;
2880 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2881 int i;
2882
2883 for (i = 0; i < num_stripes; i++) {
2884 stripe = btrfs_stripe_nr(chunk, i);
2885 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2886 return 0;
2887 }
2888
2889 return 1;
2890}
2891
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002892/* [pstart, pend) */
2893static int chunk_drange_filter(struct extent_buffer *leaf,
2894 struct btrfs_chunk *chunk,
2895 u64 chunk_offset,
2896 struct btrfs_balance_args *bargs)
2897{
2898 struct btrfs_stripe *stripe;
2899 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2900 u64 stripe_offset;
2901 u64 stripe_length;
2902 int factor;
2903 int i;
2904
2905 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2906 return 0;
2907
2908 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002909 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2910 factor = num_stripes / 2;
2911 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2912 factor = num_stripes - 1;
2913 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2914 factor = num_stripes - 2;
2915 } else {
2916 factor = num_stripes;
2917 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002918
2919 for (i = 0; i < num_stripes; i++) {
2920 stripe = btrfs_stripe_nr(chunk, i);
2921 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2922 continue;
2923
2924 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2925 stripe_length = btrfs_chunk_length(leaf, chunk);
2926 do_div(stripe_length, factor);
2927
2928 if (stripe_offset < bargs->pend &&
2929 stripe_offset + stripe_length > bargs->pstart)
2930 return 0;
2931 }
2932
2933 return 1;
2934}
2935
Ilya Dryomovea671762012-01-16 22:04:48 +02002936/* [vstart, vend) */
2937static int chunk_vrange_filter(struct extent_buffer *leaf,
2938 struct btrfs_chunk *chunk,
2939 u64 chunk_offset,
2940 struct btrfs_balance_args *bargs)
2941{
2942 if (chunk_offset < bargs->vend &&
2943 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2944 /* at least part of the chunk is inside this vrange */
2945 return 0;
2946
2947 return 1;
2948}
2949
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002950static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002951 struct btrfs_balance_args *bargs)
2952{
2953 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2954 return 0;
2955
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002956 chunk_type = chunk_to_extended(chunk_type) &
2957 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002958
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002959 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002960 return 1;
2961
2962 return 0;
2963}
2964
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002965static int should_balance_chunk(struct btrfs_root *root,
2966 struct extent_buffer *leaf,
2967 struct btrfs_chunk *chunk, u64 chunk_offset)
2968{
2969 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2970 struct btrfs_balance_args *bargs = NULL;
2971 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2972
2973 /* type filter */
2974 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2975 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2976 return 0;
2977 }
2978
2979 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2980 bargs = &bctl->data;
2981 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2982 bargs = &bctl->sys;
2983 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2984 bargs = &bctl->meta;
2985
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002986 /* profiles filter */
2987 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2988 chunk_profiles_filter(chunk_type, bargs)) {
2989 return 0;
2990 }
2991
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002992 /* usage filter */
2993 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2994 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2995 return 0;
2996 }
2997
Ilya Dryomov409d4042012-01-16 22:04:47 +02002998 /* devid filter */
2999 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3000 chunk_devid_filter(leaf, chunk, bargs)) {
3001 return 0;
3002 }
3003
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003004 /* drange filter, makes sense only with devid filter */
3005 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3006 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3007 return 0;
3008 }
3009
Ilya Dryomovea671762012-01-16 22:04:48 +02003010 /* vrange filter */
3011 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3012 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3013 return 0;
3014 }
3015
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003016 /* soft profile changing mode */
3017 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3018 chunk_soft_convert_filter(chunk_type, bargs)) {
3019 return 0;
3020 }
3021
David Sterba7d824b62014-05-07 17:37:51 +02003022 /*
3023 * limited by count, must be the last filter
3024 */
3025 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3026 if (bargs->limit == 0)
3027 return 0;
3028 else
3029 bargs->limit--;
3030 }
3031
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003032 return 1;
3033}
3034
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003035static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003036{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003037 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003038 struct btrfs_root *chunk_root = fs_info->chunk_root;
3039 struct btrfs_root *dev_root = fs_info->dev_root;
3040 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003041 struct btrfs_device *device;
3042 u64 old_size;
3043 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003044 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003045 struct btrfs_path *path;
3046 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003047 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003048 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003049 struct extent_buffer *leaf;
3050 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003051 int ret;
3052 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003053 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003054 u64 limit_data = bctl->data.limit;
3055 u64 limit_meta = bctl->meta.limit;
3056 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003057
Chris Masonec44a352008-04-28 15:29:52 -04003058 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003059 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003060 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04003061 old_size = device->total_bytes;
3062 size_to_free = div_factor(old_size, 1);
3063 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003064 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003065 device->total_bytes - device->bytes_used > size_to_free ||
3066 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003067 continue;
3068
3069 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003070 if (ret == -ENOSPC)
3071 break;
Chris Masonec44a352008-04-28 15:29:52 -04003072 BUG_ON(ret);
3073
Yan, Zhenga22285a2010-05-16 10:48:46 -04003074 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003075 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003076
3077 ret = btrfs_grow_device(trans, device, old_size);
3078 BUG_ON(ret);
3079
3080 btrfs_end_transaction(trans, dev_root);
3081 }
3082
3083 /* step two, relocate all the chunks */
3084 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003085 if (!path) {
3086 ret = -ENOMEM;
3087 goto error;
3088 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003089
3090 /* zero out stat counters */
3091 spin_lock(&fs_info->balance_lock);
3092 memset(&bctl->stat, 0, sizeof(bctl->stat));
3093 spin_unlock(&fs_info->balance_lock);
3094again:
David Sterba7d824b62014-05-07 17:37:51 +02003095 if (!counting) {
3096 bctl->data.limit = limit_data;
3097 bctl->meta.limit = limit_meta;
3098 bctl->sys.limit = limit_sys;
3099 }
Chris Masonec44a352008-04-28 15:29:52 -04003100 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3101 key.offset = (u64)-1;
3102 key.type = BTRFS_CHUNK_ITEM_KEY;
3103
Chris Masond3977122009-01-05 21:25:51 -05003104 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003105 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003106 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003107 ret = -ECANCELED;
3108 goto error;
3109 }
3110
Chris Masonec44a352008-04-28 15:29:52 -04003111 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3112 if (ret < 0)
3113 goto error;
3114
3115 /*
3116 * this shouldn't happen, it means the last relocate
3117 * failed
3118 */
3119 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003120 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003121
3122 ret = btrfs_previous_item(chunk_root, path, 0,
3123 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003124 if (ret) {
3125 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003126 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003127 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003128
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003129 leaf = path->nodes[0];
3130 slot = path->slots[0];
3131 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3132
Chris Masonec44a352008-04-28 15:29:52 -04003133 if (found_key.objectid != key.objectid)
3134 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003135
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003136 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3137
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003138 if (!counting) {
3139 spin_lock(&fs_info->balance_lock);
3140 bctl->stat.considered++;
3141 spin_unlock(&fs_info->balance_lock);
3142 }
3143
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003144 ret = should_balance_chunk(chunk_root, leaf, chunk,
3145 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003146 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003147 if (!ret)
3148 goto loop;
3149
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003150 if (counting) {
3151 spin_lock(&fs_info->balance_lock);
3152 bctl->stat.expected++;
3153 spin_unlock(&fs_info->balance_lock);
3154 goto loop;
3155 }
3156
Chris Masonec44a352008-04-28 15:29:52 -04003157 ret = btrfs_relocate_chunk(chunk_root,
3158 chunk_root->root_key.objectid,
3159 found_key.objectid,
3160 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003161 if (ret && ret != -ENOSPC)
3162 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003163 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003164 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003165 } else {
3166 spin_lock(&fs_info->balance_lock);
3167 bctl->stat.completed++;
3168 spin_unlock(&fs_info->balance_lock);
3169 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003170loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003171 if (found_key.offset == 0)
3172 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003173 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003174 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003175
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003176 if (counting) {
3177 btrfs_release_path(path);
3178 counting = false;
3179 goto again;
3180 }
Chris Masonec44a352008-04-28 15:29:52 -04003181error:
3182 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003183 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003184 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003185 enospc_errors);
3186 if (!ret)
3187 ret = -ENOSPC;
3188 }
3189
Chris Masonec44a352008-04-28 15:29:52 -04003190 return ret;
3191}
3192
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003193/**
3194 * alloc_profile_is_valid - see if a given profile is valid and reduced
3195 * @flags: profile to validate
3196 * @extended: if true @flags is treated as an extended profile
3197 */
3198static int alloc_profile_is_valid(u64 flags, int extended)
3199{
3200 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3201 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3202
3203 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3204
3205 /* 1) check that all other bits are zeroed */
3206 if (flags & ~mask)
3207 return 0;
3208
3209 /* 2) see if profile is reduced */
3210 if (flags == 0)
3211 return !extended; /* "0" is valid for usual profiles */
3212
3213 /* true if exactly one bit set */
3214 return (flags & (flags - 1)) == 0;
3215}
3216
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003217static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3218{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003219 /* cancel requested || normal exit path */
3220 return atomic_read(&fs_info->balance_cancel_req) ||
3221 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3222 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003223}
3224
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003225static void __cancel_balance(struct btrfs_fs_info *fs_info)
3226{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003227 int ret;
3228
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003229 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003230 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003231 if (ret)
3232 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003233
3234 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003235}
3236
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003237/*
3238 * Should be called with both balance and volume mutexes held
3239 */
3240int btrfs_balance(struct btrfs_balance_control *bctl,
3241 struct btrfs_ioctl_balance_args *bargs)
3242{
3243 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003244 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003245 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003246 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003247 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003248 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003249
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003250 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003251 atomic_read(&fs_info->balance_pause_req) ||
3252 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003253 ret = -EINVAL;
3254 goto out;
3255 }
3256
Ilya Dryomove4837f82012-03-27 17:09:17 +03003257 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3258 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3259 mixed = 1;
3260
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003261 /*
3262 * In case of mixed groups both data and meta should be picked,
3263 * and identical options should be given for both of them.
3264 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003265 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3266 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003267 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3268 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3269 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003270 btrfs_err(fs_info, "with mixed groups data and "
3271 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003272 ret = -EINVAL;
3273 goto out;
3274 }
3275 }
3276
Stefan Behrens8dabb742012-11-06 13:15:27 +01003277 num_devices = fs_info->fs_devices->num_devices;
3278 btrfs_dev_replace_lock(&fs_info->dev_replace);
3279 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3280 BUG_ON(num_devices < 1);
3281 num_devices--;
3282 }
3283 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003284 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003285 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003286 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003287 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003288 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003289 if (num_devices > 2)
3290 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3291 if (num_devices > 3)
3292 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3293 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003294 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3295 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3296 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003297 btrfs_err(fs_info, "unable to start balance with target "
3298 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003299 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003300 ret = -EINVAL;
3301 goto out;
3302 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003303 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3304 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3305 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003306 btrfs_err(fs_info,
3307 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003308 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003309 ret = -EINVAL;
3310 goto out;
3311 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003312 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3313 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3314 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003315 btrfs_err(fs_info,
3316 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003317 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003318 ret = -EINVAL;
3319 goto out;
3320 }
3321
Ilya Dryomove4837f82012-03-27 17:09:17 +03003322 /* allow dup'ed data chunks only in mixed mode */
3323 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003324 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003325 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003326 ret = -EINVAL;
3327 goto out;
3328 }
3329
3330 /* allow to reduce meta or sys integrity only if force set */
3331 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003332 BTRFS_BLOCK_GROUP_RAID10 |
3333 BTRFS_BLOCK_GROUP_RAID5 |
3334 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003335 do {
3336 seq = read_seqbegin(&fs_info->profiles_lock);
3337
3338 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3339 (fs_info->avail_system_alloc_bits & allowed) &&
3340 !(bctl->sys.target & allowed)) ||
3341 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3342 (fs_info->avail_metadata_alloc_bits & allowed) &&
3343 !(bctl->meta.target & allowed))) {
3344 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003345 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003346 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003347 btrfs_err(fs_info, "balance will reduce metadata "
3348 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003349 ret = -EINVAL;
3350 goto out;
3351 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003352 }
Miao Xiede98ced2013-01-29 10:13:12 +00003353 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003354
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003355 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3356 int num_tolerated_disk_barrier_failures;
3357 u64 target = bctl->sys.target;
3358
3359 num_tolerated_disk_barrier_failures =
3360 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3361 if (num_tolerated_disk_barrier_failures > 0 &&
3362 (target &
3363 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3364 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3365 num_tolerated_disk_barrier_failures = 0;
3366 else if (num_tolerated_disk_barrier_failures > 1 &&
3367 (target &
3368 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3369 num_tolerated_disk_barrier_failures = 1;
3370
3371 fs_info->num_tolerated_disk_barrier_failures =
3372 num_tolerated_disk_barrier_failures;
3373 }
3374
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003375 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003376 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003377 goto out;
3378
Ilya Dryomov59641012012-01-16 22:04:48 +02003379 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3380 BUG_ON(ret == -EEXIST);
3381 set_balance_control(bctl);
3382 } else {
3383 BUG_ON(ret != -EEXIST);
3384 spin_lock(&fs_info->balance_lock);
3385 update_balance_args(bctl);
3386 spin_unlock(&fs_info->balance_lock);
3387 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003388
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003389 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003390 mutex_unlock(&fs_info->balance_mutex);
3391
3392 ret = __btrfs_balance(fs_info);
3393
3394 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003395 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003396
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003397 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3398 fs_info->num_tolerated_disk_barrier_failures =
3399 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3400 }
3401
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003402 if (bargs) {
3403 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003404 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003405 }
3406
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003407 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3408 balance_need_close(fs_info)) {
3409 __cancel_balance(fs_info);
3410 }
3411
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003412 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003413
3414 return ret;
3415out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003416 if (bctl->flags & BTRFS_BALANCE_RESUME)
3417 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003418 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003419 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003420 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3421 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003422 return ret;
3423}
3424
3425static int balance_kthread(void *data)
3426{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003427 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003428 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003429
3430 mutex_lock(&fs_info->volume_mutex);
3431 mutex_lock(&fs_info->balance_mutex);
3432
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003433 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003434 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003435 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003436 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003437
3438 mutex_unlock(&fs_info->balance_mutex);
3439 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003440
Ilya Dryomov59641012012-01-16 22:04:48 +02003441 return ret;
3442}
3443
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003444int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3445{
3446 struct task_struct *tsk;
3447
3448 spin_lock(&fs_info->balance_lock);
3449 if (!fs_info->balance_ctl) {
3450 spin_unlock(&fs_info->balance_lock);
3451 return 0;
3452 }
3453 spin_unlock(&fs_info->balance_lock);
3454
3455 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003456 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003457 return 0;
3458 }
3459
3460 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303461 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003462}
3463
Ilya Dryomov68310a52012-06-22 12:24:12 -06003464int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003465{
Ilya Dryomov59641012012-01-16 22:04:48 +02003466 struct btrfs_balance_control *bctl;
3467 struct btrfs_balance_item *item;
3468 struct btrfs_disk_balance_args disk_bargs;
3469 struct btrfs_path *path;
3470 struct extent_buffer *leaf;
3471 struct btrfs_key key;
3472 int ret;
3473
3474 path = btrfs_alloc_path();
3475 if (!path)
3476 return -ENOMEM;
3477
Ilya Dryomov68310a52012-06-22 12:24:12 -06003478 key.objectid = BTRFS_BALANCE_OBJECTID;
3479 key.type = BTRFS_BALANCE_ITEM_KEY;
3480 key.offset = 0;
3481
3482 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3483 if (ret < 0)
3484 goto out;
3485 if (ret > 0) { /* ret = -ENOENT; */
3486 ret = 0;
3487 goto out;
3488 }
3489
Ilya Dryomov59641012012-01-16 22:04:48 +02003490 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3491 if (!bctl) {
3492 ret = -ENOMEM;
3493 goto out;
3494 }
3495
Ilya Dryomov59641012012-01-16 22:04:48 +02003496 leaf = path->nodes[0];
3497 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3498
Ilya Dryomov68310a52012-06-22 12:24:12 -06003499 bctl->fs_info = fs_info;
3500 bctl->flags = btrfs_balance_flags(leaf, item);
3501 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003502
3503 btrfs_balance_data(leaf, item, &disk_bargs);
3504 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3505 btrfs_balance_meta(leaf, item, &disk_bargs);
3506 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3507 btrfs_balance_sys(leaf, item, &disk_bargs);
3508 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3509
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003510 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3511
Ilya Dryomov68310a52012-06-22 12:24:12 -06003512 mutex_lock(&fs_info->volume_mutex);
3513 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003514
Ilya Dryomov68310a52012-06-22 12:24:12 -06003515 set_balance_control(bctl);
3516
3517 mutex_unlock(&fs_info->balance_mutex);
3518 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003519out:
3520 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003521 return ret;
3522}
3523
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003524int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3525{
3526 int ret = 0;
3527
3528 mutex_lock(&fs_info->balance_mutex);
3529 if (!fs_info->balance_ctl) {
3530 mutex_unlock(&fs_info->balance_mutex);
3531 return -ENOTCONN;
3532 }
3533
3534 if (atomic_read(&fs_info->balance_running)) {
3535 atomic_inc(&fs_info->balance_pause_req);
3536 mutex_unlock(&fs_info->balance_mutex);
3537
3538 wait_event(fs_info->balance_wait_q,
3539 atomic_read(&fs_info->balance_running) == 0);
3540
3541 mutex_lock(&fs_info->balance_mutex);
3542 /* we are good with balance_ctl ripped off from under us */
3543 BUG_ON(atomic_read(&fs_info->balance_running));
3544 atomic_dec(&fs_info->balance_pause_req);
3545 } else {
3546 ret = -ENOTCONN;
3547 }
3548
3549 mutex_unlock(&fs_info->balance_mutex);
3550 return ret;
3551}
3552
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003553int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3554{
Ilya Dryomove649e582013-10-10 20:40:21 +03003555 if (fs_info->sb->s_flags & MS_RDONLY)
3556 return -EROFS;
3557
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003558 mutex_lock(&fs_info->balance_mutex);
3559 if (!fs_info->balance_ctl) {
3560 mutex_unlock(&fs_info->balance_mutex);
3561 return -ENOTCONN;
3562 }
3563
3564 atomic_inc(&fs_info->balance_cancel_req);
3565 /*
3566 * if we are running just wait and return, balance item is
3567 * deleted in btrfs_balance in this case
3568 */
3569 if (atomic_read(&fs_info->balance_running)) {
3570 mutex_unlock(&fs_info->balance_mutex);
3571 wait_event(fs_info->balance_wait_q,
3572 atomic_read(&fs_info->balance_running) == 0);
3573 mutex_lock(&fs_info->balance_mutex);
3574 } else {
3575 /* __cancel_balance needs volume_mutex */
3576 mutex_unlock(&fs_info->balance_mutex);
3577 mutex_lock(&fs_info->volume_mutex);
3578 mutex_lock(&fs_info->balance_mutex);
3579
3580 if (fs_info->balance_ctl)
3581 __cancel_balance(fs_info);
3582
3583 mutex_unlock(&fs_info->volume_mutex);
3584 }
3585
3586 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3587 atomic_dec(&fs_info->balance_cancel_req);
3588 mutex_unlock(&fs_info->balance_mutex);
3589 return 0;
3590}
3591
Stefan Behrens803b2f52013-08-15 17:11:21 +02003592static int btrfs_uuid_scan_kthread(void *data)
3593{
3594 struct btrfs_fs_info *fs_info = data;
3595 struct btrfs_root *root = fs_info->tree_root;
3596 struct btrfs_key key;
3597 struct btrfs_key max_key;
3598 struct btrfs_path *path = NULL;
3599 int ret = 0;
3600 struct extent_buffer *eb;
3601 int slot;
3602 struct btrfs_root_item root_item;
3603 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003604 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003605
3606 path = btrfs_alloc_path();
3607 if (!path) {
3608 ret = -ENOMEM;
3609 goto out;
3610 }
3611
3612 key.objectid = 0;
3613 key.type = BTRFS_ROOT_ITEM_KEY;
3614 key.offset = 0;
3615
3616 max_key.objectid = (u64)-1;
3617 max_key.type = BTRFS_ROOT_ITEM_KEY;
3618 max_key.offset = (u64)-1;
3619
3620 path->keep_locks = 1;
3621
3622 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003623 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003624 if (ret) {
3625 if (ret > 0)
3626 ret = 0;
3627 break;
3628 }
3629
3630 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3631 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3632 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3633 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3634 goto skip;
3635
3636 eb = path->nodes[0];
3637 slot = path->slots[0];
3638 item_size = btrfs_item_size_nr(eb, slot);
3639 if (item_size < sizeof(root_item))
3640 goto skip;
3641
Stefan Behrens803b2f52013-08-15 17:11:21 +02003642 read_extent_buffer(eb, &root_item,
3643 btrfs_item_ptr_offset(eb, slot),
3644 (int)sizeof(root_item));
3645 if (btrfs_root_refs(&root_item) == 0)
3646 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003647
3648 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3649 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3650 if (trans)
3651 goto update_tree;
3652
3653 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003654 /*
3655 * 1 - subvol uuid item
3656 * 1 - received_subvol uuid item
3657 */
3658 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3659 if (IS_ERR(trans)) {
3660 ret = PTR_ERR(trans);
3661 break;
3662 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003663 continue;
3664 } else {
3665 goto skip;
3666 }
3667update_tree:
3668 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003669 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3670 root_item.uuid,
3671 BTRFS_UUID_KEY_SUBVOL,
3672 key.objectid);
3673 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003674 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003675 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003676 break;
3677 }
3678 }
3679
3680 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003681 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3682 root_item.received_uuid,
3683 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3684 key.objectid);
3685 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003686 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003687 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003688 break;
3689 }
3690 }
3691
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003692skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003693 if (trans) {
3694 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003695 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003696 if (ret)
3697 break;
3698 }
3699
Stefan Behrens803b2f52013-08-15 17:11:21 +02003700 btrfs_release_path(path);
3701 if (key.offset < (u64)-1) {
3702 key.offset++;
3703 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3704 key.offset = 0;
3705 key.type = BTRFS_ROOT_ITEM_KEY;
3706 } else if (key.objectid < (u64)-1) {
3707 key.offset = 0;
3708 key.type = BTRFS_ROOT_ITEM_KEY;
3709 key.objectid++;
3710 } else {
3711 break;
3712 }
3713 cond_resched();
3714 }
3715
3716out:
3717 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003718 if (trans && !IS_ERR(trans))
3719 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003720 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003721 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003722 else
3723 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003724 up(&fs_info->uuid_tree_rescan_sem);
3725 return 0;
3726}
3727
Stefan Behrens70f80172013-08-15 17:11:23 +02003728/*
3729 * Callback for btrfs_uuid_tree_iterate().
3730 * returns:
3731 * 0 check succeeded, the entry is not outdated.
3732 * < 0 if an error occured.
3733 * > 0 if the check failed, which means the caller shall remove the entry.
3734 */
3735static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3736 u8 *uuid, u8 type, u64 subid)
3737{
3738 struct btrfs_key key;
3739 int ret = 0;
3740 struct btrfs_root *subvol_root;
3741
3742 if (type != BTRFS_UUID_KEY_SUBVOL &&
3743 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3744 goto out;
3745
3746 key.objectid = subid;
3747 key.type = BTRFS_ROOT_ITEM_KEY;
3748 key.offset = (u64)-1;
3749 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3750 if (IS_ERR(subvol_root)) {
3751 ret = PTR_ERR(subvol_root);
3752 if (ret == -ENOENT)
3753 ret = 1;
3754 goto out;
3755 }
3756
3757 switch (type) {
3758 case BTRFS_UUID_KEY_SUBVOL:
3759 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3760 ret = 1;
3761 break;
3762 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3763 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3764 BTRFS_UUID_SIZE))
3765 ret = 1;
3766 break;
3767 }
3768
3769out:
3770 return ret;
3771}
3772
3773static int btrfs_uuid_rescan_kthread(void *data)
3774{
3775 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3776 int ret;
3777
3778 /*
3779 * 1st step is to iterate through the existing UUID tree and
3780 * to delete all entries that contain outdated data.
3781 * 2nd step is to add all missing entries to the UUID tree.
3782 */
3783 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3784 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003785 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003786 up(&fs_info->uuid_tree_rescan_sem);
3787 return ret;
3788 }
3789 return btrfs_uuid_scan_kthread(data);
3790}
3791
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003792int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3793{
3794 struct btrfs_trans_handle *trans;
3795 struct btrfs_root *tree_root = fs_info->tree_root;
3796 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003797 struct task_struct *task;
3798 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003799
3800 /*
3801 * 1 - root node
3802 * 1 - root item
3803 */
3804 trans = btrfs_start_transaction(tree_root, 2);
3805 if (IS_ERR(trans))
3806 return PTR_ERR(trans);
3807
3808 uuid_root = btrfs_create_tree(trans, fs_info,
3809 BTRFS_UUID_TREE_OBJECTID);
3810 if (IS_ERR(uuid_root)) {
3811 btrfs_abort_transaction(trans, tree_root,
3812 PTR_ERR(uuid_root));
3813 return PTR_ERR(uuid_root);
3814 }
3815
3816 fs_info->uuid_root = uuid_root;
3817
Stefan Behrens803b2f52013-08-15 17:11:21 +02003818 ret = btrfs_commit_transaction(trans, tree_root);
3819 if (ret)
3820 return ret;
3821
3822 down(&fs_info->uuid_tree_rescan_sem);
3823 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3824 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003825 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003826 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003827 up(&fs_info->uuid_tree_rescan_sem);
3828 return PTR_ERR(task);
3829 }
3830
3831 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003832}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003833
Stefan Behrens70f80172013-08-15 17:11:23 +02003834int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3835{
3836 struct task_struct *task;
3837
3838 down(&fs_info->uuid_tree_rescan_sem);
3839 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3840 if (IS_ERR(task)) {
3841 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003842 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003843 up(&fs_info->uuid_tree_rescan_sem);
3844 return PTR_ERR(task);
3845 }
3846
3847 return 0;
3848}
3849
Chris Mason8f18cf12008-04-25 16:53:30 -04003850/*
3851 * shrinking a device means finding all of the device extents past
3852 * the new size, and then following the back refs to the chunks.
3853 * The chunk relocation code actually frees the device extent
3854 */
3855int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3856{
3857 struct btrfs_trans_handle *trans;
3858 struct btrfs_root *root = device->dev_root;
3859 struct btrfs_dev_extent *dev_extent = NULL;
3860 struct btrfs_path *path;
3861 u64 length;
3862 u64 chunk_tree;
3863 u64 chunk_objectid;
3864 u64 chunk_offset;
3865 int ret;
3866 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003867 int failed = 0;
3868 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003869 struct extent_buffer *l;
3870 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003871 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003872 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003873 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003874 u64 diff = device->total_bytes - new_size;
3875
Stefan Behrens63a212a2012-11-05 18:29:28 +01003876 if (device->is_tgtdev_for_dev_replace)
3877 return -EINVAL;
3878
Chris Mason8f18cf12008-04-25 16:53:30 -04003879 path = btrfs_alloc_path();
3880 if (!path)
3881 return -ENOMEM;
3882
Chris Mason8f18cf12008-04-25 16:53:30 -04003883 path->reada = 2;
3884
Chris Mason7d9eb122008-07-08 14:19:17 -04003885 lock_chunks(root);
3886
Chris Mason8f18cf12008-04-25 16:53:30 -04003887 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003888 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003889 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003890 spin_lock(&root->fs_info->free_chunk_lock);
3891 root->fs_info->free_chunk_space -= diff;
3892 spin_unlock(&root->fs_info->free_chunk_lock);
3893 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003894 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003895
Josef Bacikba1bf482009-09-11 16:11:19 -04003896again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003897 key.objectid = device->devid;
3898 key.offset = (u64)-1;
3899 key.type = BTRFS_DEV_EXTENT_KEY;
3900
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003901 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003902 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3903 if (ret < 0)
3904 goto done;
3905
3906 ret = btrfs_previous_item(root, path, 0, key.type);
3907 if (ret < 0)
3908 goto done;
3909 if (ret) {
3910 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003911 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003912 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003913 }
3914
3915 l = path->nodes[0];
3916 slot = path->slots[0];
3917 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3918
Josef Bacikba1bf482009-09-11 16:11:19 -04003919 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003920 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003921 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003922 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003923
3924 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3925 length = btrfs_dev_extent_length(l, dev_extent);
3926
Josef Bacikba1bf482009-09-11 16:11:19 -04003927 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003928 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003929 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003930 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003931
3932 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3933 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3934 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003935 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003936
3937 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3938 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003939 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003940 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003941 if (ret == -ENOSPC)
3942 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003943 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003944
3945 if (failed && !retried) {
3946 failed = 0;
3947 retried = true;
3948 goto again;
3949 } else if (failed && retried) {
3950 ret = -ENOSPC;
3951 lock_chunks(root);
3952
3953 device->total_bytes = old_size;
3954 if (device->writeable)
3955 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003956 spin_lock(&root->fs_info->free_chunk_lock);
3957 root->fs_info->free_chunk_space += diff;
3958 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003959 unlock_chunks(root);
3960 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003961 }
3962
Chris Balld6397ba2009-04-27 07:29:03 -04003963 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003964 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003965 if (IS_ERR(trans)) {
3966 ret = PTR_ERR(trans);
3967 goto done;
3968 }
3969
Chris Balld6397ba2009-04-27 07:29:03 -04003970 lock_chunks(root);
3971
3972 device->disk_total_bytes = new_size;
3973 /* Now btrfs_update_device() will change the on-disk size. */
3974 ret = btrfs_update_device(trans, device);
3975 if (ret) {
3976 unlock_chunks(root);
3977 btrfs_end_transaction(trans, root);
3978 goto done;
3979 }
3980 WARN_ON(diff > old_total);
3981 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3982 unlock_chunks(root);
3983 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003984done:
3985 btrfs_free_path(path);
3986 return ret;
3987}
3988
Li Zefan125ccb02011-12-08 15:07:24 +08003989static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003990 struct btrfs_key *key,
3991 struct btrfs_chunk *chunk, int item_size)
3992{
David Sterba6c417612011-04-13 15:41:04 +02003993 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003994 struct btrfs_disk_key disk_key;
3995 u32 array_size;
3996 u8 *ptr;
3997
3998 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08003999 if (array_size + item_size + sizeof(disk_key)
4000 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
Chris Mason0b86a832008-03-24 15:01:56 -04004001 return -EFBIG;
4002
4003 ptr = super_copy->sys_chunk_array + array_size;
4004 btrfs_cpu_key_to_disk(&disk_key, key);
4005 memcpy(ptr, &disk_key, sizeof(disk_key));
4006 ptr += sizeof(disk_key);
4007 memcpy(ptr, chunk, item_size);
4008 item_size += sizeof(disk_key);
4009 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4010 return 0;
4011}
4012
Miao Xieb2117a32011-01-05 10:07:28 +00004013/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004014 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004015 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004016static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004017{
Arne Jansen73c5de02011-04-12 12:07:57 +02004018 const struct btrfs_device_info *di_a = a;
4019 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004020
Arne Jansen73c5de02011-04-12 12:07:57 +02004021 if (di_a->max_avail > di_b->max_avail)
4022 return -1;
4023 if (di_a->max_avail < di_b->max_avail)
4024 return 1;
4025 if (di_a->total_avail > di_b->total_avail)
4026 return -1;
4027 if (di_a->total_avail < di_b->total_avail)
4028 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004029 return 0;
4030}
4031
Eric Sandeen48a3b632013-04-25 20:41:01 +00004032static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004033 [BTRFS_RAID_RAID10] = {
4034 .sub_stripes = 2,
4035 .dev_stripes = 1,
4036 .devs_max = 0, /* 0 == as many as possible */
4037 .devs_min = 4,
4038 .devs_increment = 2,
4039 .ncopies = 2,
4040 },
4041 [BTRFS_RAID_RAID1] = {
4042 .sub_stripes = 1,
4043 .dev_stripes = 1,
4044 .devs_max = 2,
4045 .devs_min = 2,
4046 .devs_increment = 2,
4047 .ncopies = 2,
4048 },
4049 [BTRFS_RAID_DUP] = {
4050 .sub_stripes = 1,
4051 .dev_stripes = 2,
4052 .devs_max = 1,
4053 .devs_min = 1,
4054 .devs_increment = 1,
4055 .ncopies = 2,
4056 },
4057 [BTRFS_RAID_RAID0] = {
4058 .sub_stripes = 1,
4059 .dev_stripes = 1,
4060 .devs_max = 0,
4061 .devs_min = 2,
4062 .devs_increment = 1,
4063 .ncopies = 1,
4064 },
4065 [BTRFS_RAID_SINGLE] = {
4066 .sub_stripes = 1,
4067 .dev_stripes = 1,
4068 .devs_max = 1,
4069 .devs_min = 1,
4070 .devs_increment = 1,
4071 .ncopies = 1,
4072 },
Chris Masone942f882013-02-20 14:06:05 -05004073 [BTRFS_RAID_RAID5] = {
4074 .sub_stripes = 1,
4075 .dev_stripes = 1,
4076 .devs_max = 0,
4077 .devs_min = 2,
4078 .devs_increment = 1,
4079 .ncopies = 2,
4080 },
4081 [BTRFS_RAID_RAID6] = {
4082 .sub_stripes = 1,
4083 .dev_stripes = 1,
4084 .devs_max = 0,
4085 .devs_min = 3,
4086 .devs_increment = 1,
4087 .ncopies = 3,
4088 },
Liu Bo31e50222012-11-21 14:18:10 +00004089};
4090
David Woodhouse53b381b2013-01-29 18:40:14 -05004091static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4092{
4093 /* TODO allow them to set a preferred stripe size */
4094 return 64 * 1024;
4095}
4096
4097static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4098{
David Woodhouse53b381b2013-01-29 18:40:14 -05004099 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
4100 return;
4101
Miao Xieceda0862013-04-11 10:30:16 +00004102 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004103}
4104
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004105#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4106 - sizeof(struct btrfs_item) \
4107 - sizeof(struct btrfs_chunk)) \
4108 / sizeof(struct btrfs_stripe) + 1)
4109
4110#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4111 - 2 * sizeof(struct btrfs_disk_key) \
4112 - 2 * sizeof(struct btrfs_chunk)) \
4113 / sizeof(struct btrfs_stripe) + 1)
4114
Miao Xieb2117a32011-01-05 10:07:28 +00004115static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004116 struct btrfs_root *extent_root, u64 start,
4117 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004118{
4119 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004120 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4121 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004122 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004123 struct extent_map_tree *em_tree;
4124 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004125 struct btrfs_device_info *devices_info = NULL;
4126 u64 total_avail;
4127 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004128 int data_stripes; /* number of stripes that count for
4129 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004130 int sub_stripes; /* sub_stripes info for map */
4131 int dev_stripes; /* stripes per dev */
4132 int devs_max; /* max devs to use */
4133 int devs_min; /* min devs needed */
4134 int devs_increment; /* ndevs has to be a multiple of this */
4135 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004136 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004137 u64 max_stripe_size;
4138 u64 max_chunk_size;
4139 u64 stripe_size;
4140 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004141 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004142 int ndevs;
4143 int i;
4144 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004145 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004146
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004147 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004148
Miao Xieb2117a32011-01-05 10:07:28 +00004149 if (list_empty(&fs_devices->alloc_list))
4150 return -ENOSPC;
4151
Liu Bo31e50222012-11-21 14:18:10 +00004152 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004153
Liu Bo31e50222012-11-21 14:18:10 +00004154 sub_stripes = btrfs_raid_array[index].sub_stripes;
4155 dev_stripes = btrfs_raid_array[index].dev_stripes;
4156 devs_max = btrfs_raid_array[index].devs_max;
4157 devs_min = btrfs_raid_array[index].devs_min;
4158 devs_increment = btrfs_raid_array[index].devs_increment;
4159 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004160
4161 if (type & BTRFS_BLOCK_GROUP_DATA) {
4162 max_stripe_size = 1024 * 1024 * 1024;
4163 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004164 if (!devs_max)
4165 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004166 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004167 /* for larger filesystems, use larger metadata chunks */
4168 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4169 max_stripe_size = 1024 * 1024 * 1024;
4170 else
4171 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004172 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004173 if (!devs_max)
4174 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004175 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004176 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004177 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004178 if (!devs_max)
4179 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004180 } else {
David Sterba351fd352014-05-15 16:48:20 +02004181 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004182 type);
4183 BUG_ON(1);
4184 }
4185
4186 /* we don't want a chunk larger than 10% of writeable space */
4187 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4188 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004189
4190 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4191 GFP_NOFS);
4192 if (!devices_info)
4193 return -ENOMEM;
4194
Arne Jansen73c5de02011-04-12 12:07:57 +02004195 cur = fs_devices->alloc_list.next;
4196
4197 /*
4198 * in the first pass through the devices list, we gather information
4199 * about the available holes on each device.
4200 */
4201 ndevs = 0;
4202 while (cur != &fs_devices->alloc_list) {
4203 struct btrfs_device *device;
4204 u64 max_avail;
4205 u64 dev_offset;
4206
4207 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4208
4209 cur = cur->next;
4210
4211 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004212 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004213 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004214 continue;
4215 }
4216
Stefan Behrens63a212a2012-11-05 18:29:28 +01004217 if (!device->in_fs_metadata ||
4218 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004219 continue;
4220
4221 if (device->total_bytes > device->bytes_used)
4222 total_avail = device->total_bytes - device->bytes_used;
4223 else
4224 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004225
4226 /* If there is no space on this device, skip it. */
4227 if (total_avail == 0)
4228 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004229
Josef Bacik6df9a952013-06-27 13:22:46 -04004230 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004231 max_stripe_size * dev_stripes,
4232 &dev_offset, &max_avail);
4233 if (ret && ret != -ENOSPC)
4234 goto error;
4235
4236 if (ret == 0)
4237 max_avail = max_stripe_size * dev_stripes;
4238
4239 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4240 continue;
4241
Eric Sandeen063d0062013-01-31 00:55:01 +00004242 if (ndevs == fs_devices->rw_devices) {
4243 WARN(1, "%s: found more than %llu devices\n",
4244 __func__, fs_devices->rw_devices);
4245 break;
4246 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004247 devices_info[ndevs].dev_offset = dev_offset;
4248 devices_info[ndevs].max_avail = max_avail;
4249 devices_info[ndevs].total_avail = total_avail;
4250 devices_info[ndevs].dev = device;
4251 ++ndevs;
4252 }
4253
4254 /*
4255 * now sort the devices by hole size / available space
4256 */
4257 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4258 btrfs_cmp_device_info, NULL);
4259
4260 /* round down to number of usable stripes */
4261 ndevs -= ndevs % devs_increment;
4262
4263 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4264 ret = -ENOSPC;
4265 goto error;
4266 }
4267
4268 if (devs_max && ndevs > devs_max)
4269 ndevs = devs_max;
4270 /*
4271 * the primary goal is to maximize the number of stripes, so use as many
4272 * devices as possible, even if the stripes are not maximum sized.
4273 */
4274 stripe_size = devices_info[ndevs-1].max_avail;
4275 num_stripes = ndevs * dev_stripes;
4276
David Woodhouse53b381b2013-01-29 18:40:14 -05004277 /*
4278 * this will have to be fixed for RAID1 and RAID10 over
4279 * more drives
4280 */
4281 data_stripes = num_stripes / ncopies;
4282
David Woodhouse53b381b2013-01-29 18:40:14 -05004283 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4284 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4285 btrfs_super_stripesize(info->super_copy));
4286 data_stripes = num_stripes - 1;
4287 }
4288 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4289 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4290 btrfs_super_stripesize(info->super_copy));
4291 data_stripes = num_stripes - 2;
4292 }
Chris Mason86db2572013-02-20 16:23:40 -05004293
4294 /*
4295 * Use the number of data stripes to figure out how big this chunk
4296 * is really going to be in terms of logical address space,
4297 * and compare that answer with the max chunk size
4298 */
4299 if (stripe_size * data_stripes > max_chunk_size) {
4300 u64 mask = (1ULL << 24) - 1;
4301 stripe_size = max_chunk_size;
4302 do_div(stripe_size, data_stripes);
4303
4304 /* bump the answer up to a 16MB boundary */
4305 stripe_size = (stripe_size + mask) & ~mask;
4306
4307 /* but don't go higher than the limits we found
4308 * while searching for free extents
4309 */
4310 if (stripe_size > devices_info[ndevs-1].max_avail)
4311 stripe_size = devices_info[ndevs-1].max_avail;
4312 }
4313
Arne Jansen73c5de02011-04-12 12:07:57 +02004314 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004315
4316 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004317 do_div(stripe_size, raid_stripe_len);
4318 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004319
Miao Xieb2117a32011-01-05 10:07:28 +00004320 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4321 if (!map) {
4322 ret = -ENOMEM;
4323 goto error;
4324 }
4325 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004326
Arne Jansen73c5de02011-04-12 12:07:57 +02004327 for (i = 0; i < ndevs; ++i) {
4328 for (j = 0; j < dev_stripes; ++j) {
4329 int s = i * dev_stripes + j;
4330 map->stripes[s].dev = devices_info[i].dev;
4331 map->stripes[s].physical = devices_info[i].dev_offset +
4332 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004333 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004334 }
Chris Mason593060d2008-03-25 16:50:33 -04004335 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004336 map->stripe_len = raid_stripe_len;
4337 map->io_align = raid_stripe_len;
4338 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004339 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004340 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004341
David Woodhouse53b381b2013-01-29 18:40:14 -05004342 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004343
Arne Jansen73c5de02011-04-12 12:07:57 +02004344 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004345
David Sterba172ddd62011-04-21 00:48:27 +02004346 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004347 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004348 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004349 ret = -ENOMEM;
4350 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004351 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004352 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004353 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004354 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004355 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004356 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004357 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004358 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004359
Chris Mason0b86a832008-03-24 15:01:56 -04004360 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004361 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004362 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004363 if (!ret) {
4364 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4365 atomic_inc(&em->refs);
4366 }
Chris Mason890871b2009-09-02 16:24:52 -04004367 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004368 if (ret) {
4369 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004370 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004371 }
Yan Zheng2b820322008-11-17 21:11:30 -05004372
Josef Bacik04487482013-01-29 15:03:37 -05004373 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4374 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4375 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004376 if (ret)
4377 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004378
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004379 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004380 check_raid56_incompat_flag(extent_root->fs_info, type);
4381
Miao Xieb2117a32011-01-05 10:07:28 +00004382 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004383 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004384
Josef Bacik6df9a952013-06-27 13:22:46 -04004385error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004386 write_lock(&em_tree->lock);
4387 remove_extent_mapping(em_tree, em);
4388 write_unlock(&em_tree->lock);
4389
4390 /* One for our allocation */
4391 free_extent_map(em);
4392 /* One for the tree reference */
4393 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004394error:
Miao Xieb2117a32011-01-05 10:07:28 +00004395 kfree(devices_info);
4396 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004397}
4398
Josef Bacik6df9a952013-06-27 13:22:46 -04004399int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004400 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004401 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004402{
Yan Zheng2b820322008-11-17 21:11:30 -05004403 struct btrfs_key key;
4404 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4405 struct btrfs_device *device;
4406 struct btrfs_chunk *chunk;
4407 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004408 struct extent_map_tree *em_tree;
4409 struct extent_map *em;
4410 struct map_lookup *map;
4411 size_t item_size;
4412 u64 dev_offset;
4413 u64 stripe_size;
4414 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004415 int ret;
4416
Josef Bacik6df9a952013-06-27 13:22:46 -04004417 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4418 read_lock(&em_tree->lock);
4419 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4420 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004421
Josef Bacik6df9a952013-06-27 13:22:46 -04004422 if (!em) {
4423 btrfs_crit(extent_root->fs_info, "unable to find logical "
4424 "%Lu len %Lu", chunk_offset, chunk_size);
4425 return -EINVAL;
4426 }
4427
4428 if (em->start != chunk_offset || em->len != chunk_size) {
4429 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004430 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004431 chunk_size, em->start, em->len);
4432 free_extent_map(em);
4433 return -EINVAL;
4434 }
4435
4436 map = (struct map_lookup *)em->bdev;
4437 item_size = btrfs_chunk_item_size(map->num_stripes);
4438 stripe_size = em->orig_block_len;
4439
4440 chunk = kzalloc(item_size, GFP_NOFS);
4441 if (!chunk) {
4442 ret = -ENOMEM;
4443 goto out;
4444 }
4445
4446 for (i = 0; i < map->num_stripes; i++) {
4447 device = map->stripes[i].dev;
4448 dev_offset = map->stripes[i].physical;
4449
Yan Zheng2b820322008-11-17 21:11:30 -05004450 device->bytes_used += stripe_size;
4451 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004452 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004453 goto out;
4454 ret = btrfs_alloc_dev_extent(trans, device,
4455 chunk_root->root_key.objectid,
4456 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4457 chunk_offset, dev_offset,
4458 stripe_size);
4459 if (ret)
4460 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004461 }
4462
Josef Bacik2bf64752011-09-26 17:12:22 -04004463 spin_lock(&extent_root->fs_info->free_chunk_lock);
4464 extent_root->fs_info->free_chunk_space -= (stripe_size *
4465 map->num_stripes);
4466 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4467
Yan Zheng2b820322008-11-17 21:11:30 -05004468 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004469 for (i = 0; i < map->num_stripes; i++) {
4470 device = map->stripes[i].dev;
4471 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004472
4473 btrfs_set_stack_stripe_devid(stripe, device->devid);
4474 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4475 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4476 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004477 }
4478
4479 btrfs_set_stack_chunk_length(chunk, chunk_size);
4480 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4481 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4482 btrfs_set_stack_chunk_type(chunk, map->type);
4483 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4484 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4485 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4486 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4487 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4488
4489 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4490 key.type = BTRFS_CHUNK_ITEM_KEY;
4491 key.offset = chunk_offset;
4492
4493 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004494 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4495 /*
4496 * TODO: Cleanup of inserted chunk root in case of
4497 * failure.
4498 */
Li Zefan125ccb02011-12-08 15:07:24 +08004499 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004500 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004501 }
liubo1abe9b82011-03-24 11:18:59 +00004502
Josef Bacik6df9a952013-06-27 13:22:46 -04004503out:
Yan Zheng2b820322008-11-17 21:11:30 -05004504 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004505 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004506 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004507}
4508
4509/*
4510 * Chunk allocation falls into two parts. The first part does works
4511 * that make the new allocated chunk useable, but not do any operation
4512 * that modifies the chunk tree. The second part does the works that
4513 * require modifying the chunk tree. This division is important for the
4514 * bootstrap process of adding storage to a seed btrfs.
4515 */
4516int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4517 struct btrfs_root *extent_root, u64 type)
4518{
4519 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004520
Josef Bacik6df9a952013-06-27 13:22:46 -04004521 chunk_offset = find_next_chunk(extent_root->fs_info);
4522 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004523}
4524
Chris Masond3977122009-01-05 21:25:51 -05004525static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004526 struct btrfs_root *root,
4527 struct btrfs_device *device)
4528{
4529 u64 chunk_offset;
4530 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004531 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004532 struct btrfs_fs_info *fs_info = root->fs_info;
4533 struct btrfs_root *extent_root = fs_info->extent_root;
4534 int ret;
4535
Josef Bacik6df9a952013-06-27 13:22:46 -04004536 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004537 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004538 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4539 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004540 if (ret)
4541 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004542
Josef Bacik6df9a952013-06-27 13:22:46 -04004543 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004544 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004545 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4546 alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004547 if (ret) {
4548 btrfs_abort_transaction(trans, root, ret);
4549 goto out;
4550 }
Yan Zheng2b820322008-11-17 21:11:30 -05004551
4552 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004553 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004554 btrfs_abort_transaction(trans, root, ret);
David Sterba005d6422012-09-18 07:52:32 -06004555out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004556 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004557}
4558
4559int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4560{
4561 struct extent_map *em;
4562 struct map_lookup *map;
4563 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4564 int readonly = 0;
4565 int i;
4566
Chris Mason890871b2009-09-02 16:24:52 -04004567 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004568 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004569 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004570 if (!em)
4571 return 1;
4572
Josef Bacikf48b9072010-01-27 02:07:59 +00004573 if (btrfs_test_opt(root, DEGRADED)) {
4574 free_extent_map(em);
4575 return 0;
4576 }
4577
Yan Zheng2b820322008-11-17 21:11:30 -05004578 map = (struct map_lookup *)em->bdev;
4579 for (i = 0; i < map->num_stripes; i++) {
4580 if (!map->stripes[i].dev->writeable) {
4581 readonly = 1;
4582 break;
4583 }
4584 }
4585 free_extent_map(em);
4586 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004587}
4588
4589void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4590{
David Sterbaa8067e02011-04-21 00:34:43 +02004591 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004592}
4593
4594void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4595{
4596 struct extent_map *em;
4597
Chris Masond3977122009-01-05 21:25:51 -05004598 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004599 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004600 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4601 if (em)
4602 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004603 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004604 if (!em)
4605 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004606 /* once for us */
4607 free_extent_map(em);
4608 /* once for the tree */
4609 free_extent_map(em);
4610 }
4611}
4612
Stefan Behrens5d964052012-11-05 14:59:07 +01004613int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004614{
Stefan Behrens5d964052012-11-05 14:59:07 +01004615 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004616 struct extent_map *em;
4617 struct map_lookup *map;
4618 struct extent_map_tree *em_tree = &map_tree->map_tree;
4619 int ret;
4620
Chris Mason890871b2009-09-02 16:24:52 -04004621 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004622 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004623 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004624
Josef Bacikfb7669b2013-04-23 10:53:18 -04004625 /*
4626 * We could return errors for these cases, but that could get ugly and
4627 * we'd probably do the same thing which is just not do anything else
4628 * and exit, so return 1 so the callers don't try to use other copies.
4629 */
4630 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004631 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004632 logical+len);
4633 return 1;
4634 }
4635
4636 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004637 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004638 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004639 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004640 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004641 return 1;
4642 }
4643
Chris Masonf1885912008-04-09 16:28:12 -04004644 map = (struct map_lookup *)em->bdev;
4645 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4646 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004647 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4648 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004649 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4650 ret = 2;
4651 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4652 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004653 else
4654 ret = 1;
4655 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004656
4657 btrfs_dev_replace_lock(&fs_info->dev_replace);
4658 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4659 ret++;
4660 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4661
Chris Masonf1885912008-04-09 16:28:12 -04004662 return ret;
4663}
4664
David Woodhouse53b381b2013-01-29 18:40:14 -05004665unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4666 struct btrfs_mapping_tree *map_tree,
4667 u64 logical)
4668{
4669 struct extent_map *em;
4670 struct map_lookup *map;
4671 struct extent_map_tree *em_tree = &map_tree->map_tree;
4672 unsigned long len = root->sectorsize;
4673
4674 read_lock(&em_tree->lock);
4675 em = lookup_extent_mapping(em_tree, logical, len);
4676 read_unlock(&em_tree->lock);
4677 BUG_ON(!em);
4678
4679 BUG_ON(em->start > logical || em->start + em->len < logical);
4680 map = (struct map_lookup *)em->bdev;
4681 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4682 BTRFS_BLOCK_GROUP_RAID6)) {
4683 len = map->stripe_len * nr_data_stripes(map);
4684 }
4685 free_extent_map(em);
4686 return len;
4687}
4688
4689int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4690 u64 logical, u64 len, int mirror_num)
4691{
4692 struct extent_map *em;
4693 struct map_lookup *map;
4694 struct extent_map_tree *em_tree = &map_tree->map_tree;
4695 int ret = 0;
4696
4697 read_lock(&em_tree->lock);
4698 em = lookup_extent_mapping(em_tree, logical, len);
4699 read_unlock(&em_tree->lock);
4700 BUG_ON(!em);
4701
4702 BUG_ON(em->start > logical || em->start + em->len < logical);
4703 map = (struct map_lookup *)em->bdev;
4704 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4705 BTRFS_BLOCK_GROUP_RAID6))
4706 ret = 1;
4707 free_extent_map(em);
4708 return ret;
4709}
4710
Stefan Behrens30d98612012-11-06 14:52:18 +01004711static int find_live_mirror(struct btrfs_fs_info *fs_info,
4712 struct map_lookup *map, int first, int num,
4713 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004714{
4715 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004716 int tolerance;
4717 struct btrfs_device *srcdev;
4718
4719 if (dev_replace_is_ongoing &&
4720 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4721 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4722 srcdev = fs_info->dev_replace.srcdev;
4723 else
4724 srcdev = NULL;
4725
4726 /*
4727 * try to avoid the drive that is the source drive for a
4728 * dev-replace procedure, only choose it if no other non-missing
4729 * mirror is available
4730 */
4731 for (tolerance = 0; tolerance < 2; tolerance++) {
4732 if (map->stripes[optimal].dev->bdev &&
4733 (tolerance || map->stripes[optimal].dev != srcdev))
4734 return optimal;
4735 for (i = first; i < first + num; i++) {
4736 if (map->stripes[i].dev->bdev &&
4737 (tolerance || map->stripes[i].dev != srcdev))
4738 return i;
4739 }
Chris Masondfe25022008-05-13 13:46:40 -04004740 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004741
Chris Masondfe25022008-05-13 13:46:40 -04004742 /* we couldn't find one that doesn't fail. Just return something
4743 * and the io error handling code will clean up eventually
4744 */
4745 return optimal;
4746}
4747
David Woodhouse53b381b2013-01-29 18:40:14 -05004748static inline int parity_smaller(u64 a, u64 b)
4749{
4750 return a > b;
4751}
4752
4753/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4754static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4755{
4756 struct btrfs_bio_stripe s;
4757 int i;
4758 u64 l;
4759 int again = 1;
4760
4761 while (again) {
4762 again = 0;
4763 for (i = 0; i < bbio->num_stripes - 1; i++) {
4764 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4765 s = bbio->stripes[i];
4766 l = raid_map[i];
4767 bbio->stripes[i] = bbio->stripes[i+1];
4768 raid_map[i] = raid_map[i+1];
4769 bbio->stripes[i+1] = s;
4770 raid_map[i+1] = l;
4771 again = 1;
4772 }
4773 }
4774 }
4775}
4776
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004777static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004778 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004779 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004780 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004781{
4782 struct extent_map *em;
4783 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004784 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004785 struct extent_map_tree *em_tree = &map_tree->map_tree;
4786 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004787 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004788 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004789 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004790 u64 stripe_nr_orig;
4791 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004792 u64 stripe_len;
4793 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004794 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004795 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004796 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004797 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004798 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004799 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004800 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4801 int dev_replace_is_ongoing = 0;
4802 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004803 int patch_the_first_stripe_for_dev_replace = 0;
4804 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004805 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004806
Chris Mason890871b2009-09-02 16:24:52 -04004807 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004808 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004809 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004810
Chris Mason3b951512008-04-17 11:29:12 -04004811 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004812 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004813 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004814 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004815 }
Chris Mason0b86a832008-03-24 15:01:56 -04004816
Josef Bacik9bb91872013-04-19 23:45:33 -04004817 if (em->start > logical || em->start + em->len < logical) {
4818 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004819 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004820 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004821 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004822 return -EINVAL;
4823 }
4824
Chris Mason0b86a832008-03-24 15:01:56 -04004825 map = (struct map_lookup *)em->bdev;
4826 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004827
David Woodhouse53b381b2013-01-29 18:40:14 -05004828 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004829 stripe_nr = offset;
4830 /*
4831 * stripe_nr counts the total number of stripes we have to stride
4832 * to get to this block
4833 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004834 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004835
David Woodhouse53b381b2013-01-29 18:40:14 -05004836 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004837 BUG_ON(offset < stripe_offset);
4838
4839 /* stripe_offset is the offset of this block in its stripe*/
4840 stripe_offset = offset - stripe_offset;
4841
David Woodhouse53b381b2013-01-29 18:40:14 -05004842 /* if we're here for raid56, we need to know the stripe aligned start */
4843 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4844 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4845 raid56_full_stripe_start = offset;
4846
4847 /* allow a write of a full stripe, but make sure we don't
4848 * allow straddling of stripes
4849 */
4850 do_div(raid56_full_stripe_start, full_stripe_len);
4851 raid56_full_stripe_start *= full_stripe_len;
4852 }
4853
4854 if (rw & REQ_DISCARD) {
4855 /* we don't discard raid56 yet */
4856 if (map->type &
4857 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4858 ret = -EOPNOTSUPP;
4859 goto out;
4860 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004861 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004862 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4863 u64 max_len;
4864 /* For writes to RAID[56], allow a full stripeset across all disks.
4865 For other RAID types and for RAID[56] reads, just allow a single
4866 stripe (on a single disk). */
4867 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4868 (rw & REQ_WRITE)) {
4869 max_len = stripe_len * nr_data_stripes(map) -
4870 (offset - raid56_full_stripe_start);
4871 } else {
4872 /* we limit the length of each bio to what fits in a stripe */
4873 max_len = stripe_len - stripe_offset;
4874 }
4875 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004876 } else {
4877 *length = em->len - offset;
4878 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004879
David Woodhouse53b381b2013-01-29 18:40:14 -05004880 /* This is for when we're called from btrfs_merge_bio_hook() and all
4881 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004882 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004883 goto out;
4884
Stefan Behrens472262f2012-11-06 14:43:46 +01004885 btrfs_dev_replace_lock(dev_replace);
4886 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4887 if (!dev_replace_is_ongoing)
4888 btrfs_dev_replace_unlock(dev_replace);
4889
Stefan Behrensad6d6202012-11-06 15:06:47 +01004890 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4891 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4892 dev_replace->tgtdev != NULL) {
4893 /*
4894 * in dev-replace case, for repair case (that's the only
4895 * case where the mirror is selected explicitly when
4896 * calling btrfs_map_block), blocks left of the left cursor
4897 * can also be read from the target drive.
4898 * For REQ_GET_READ_MIRRORS, the target drive is added as
4899 * the last one to the array of stripes. For READ, it also
4900 * needs to be supported using the same mirror number.
4901 * If the requested block is not left of the left cursor,
4902 * EIO is returned. This can happen because btrfs_num_copies()
4903 * returns one more in the dev-replace case.
4904 */
4905 u64 tmp_length = *length;
4906 struct btrfs_bio *tmp_bbio = NULL;
4907 int tmp_num_stripes;
4908 u64 srcdev_devid = dev_replace->srcdev->devid;
4909 int index_srcdev = 0;
4910 int found = 0;
4911 u64 physical_of_found = 0;
4912
4913 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004914 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004915 if (ret) {
4916 WARN_ON(tmp_bbio != NULL);
4917 goto out;
4918 }
4919
4920 tmp_num_stripes = tmp_bbio->num_stripes;
4921 if (mirror_num > tmp_num_stripes) {
4922 /*
4923 * REQ_GET_READ_MIRRORS does not contain this
4924 * mirror, that means that the requested area
4925 * is not left of the left cursor
4926 */
4927 ret = -EIO;
4928 kfree(tmp_bbio);
4929 goto out;
4930 }
4931
4932 /*
4933 * process the rest of the function using the mirror_num
4934 * of the source drive. Therefore look it up first.
4935 * At the end, patch the device pointer to the one of the
4936 * target drive.
4937 */
4938 for (i = 0; i < tmp_num_stripes; i++) {
4939 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4940 /*
4941 * In case of DUP, in order to keep it
4942 * simple, only add the mirror with the
4943 * lowest physical address
4944 */
4945 if (found &&
4946 physical_of_found <=
4947 tmp_bbio->stripes[i].physical)
4948 continue;
4949 index_srcdev = i;
4950 found = 1;
4951 physical_of_found =
4952 tmp_bbio->stripes[i].physical;
4953 }
4954 }
4955
4956 if (found) {
4957 mirror_num = index_srcdev + 1;
4958 patch_the_first_stripe_for_dev_replace = 1;
4959 physical_to_patch_in_first_stripe = physical_of_found;
4960 } else {
4961 WARN_ON(1);
4962 ret = -EIO;
4963 kfree(tmp_bbio);
4964 goto out;
4965 }
4966
4967 kfree(tmp_bbio);
4968 } else if (mirror_num > map->num_stripes) {
4969 mirror_num = 0;
4970 }
4971
Chris Masonf2d8d742008-04-21 10:03:05 -04004972 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004973 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004974 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00004975 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00004976 do_div(stripe_nr_end, map->stripe_len);
4977 stripe_end_offset = stripe_nr_end * map->stripe_len -
4978 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004979
Li Dongyangfce3bb92011-03-24 10:24:26 +00004980 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4981 if (rw & REQ_DISCARD)
4982 num_stripes = min_t(u64, map->num_stripes,
4983 stripe_nr_end - stripe_nr_orig);
4984 stripe_index = do_div(stripe_nr, map->num_stripes);
4985 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004986 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004987 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04004988 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04004989 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004990 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01004991 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04004992 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01004993 current->pid % map->num_stripes,
4994 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004995 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004996 }
Chris Mason2fff7342008-04-29 14:12:09 -04004997
Chris Mason611f0e02008-04-03 16:29:03 -04004998 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004999 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005000 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005001 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005002 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005003 } else {
5004 mirror_num = 1;
5005 }
Chris Mason2fff7342008-04-29 14:12:09 -04005006
Chris Mason321aecc2008-04-16 10:49:51 -04005007 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5008 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005009
5010 stripe_index = do_div(stripe_nr, factor);
5011 stripe_index *= map->sub_stripes;
5012
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005013 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005014 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005015 else if (rw & REQ_DISCARD)
5016 num_stripes = min_t(u64, map->sub_stripes *
5017 (stripe_nr_end - stripe_nr_orig),
5018 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005019 else if (mirror_num)
5020 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005021 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005022 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005023 stripe_index = find_live_mirror(fs_info, map,
5024 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005025 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005026 current->pid % map->sub_stripes,
5027 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005028 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005029 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005030
5031 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5032 BTRFS_BLOCK_GROUP_RAID6)) {
5033 u64 tmp;
5034
5035 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
5036 && raid_map_ret) {
5037 int i, rot;
5038
5039 /* push stripe_nr back to the start of the full stripe */
5040 stripe_nr = raid56_full_stripe_start;
5041 do_div(stripe_nr, stripe_len);
5042
5043 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5044
5045 /* RAID[56] write or recovery. Return all stripes */
5046 num_stripes = map->num_stripes;
5047 max_errors = nr_parity_stripes(map);
5048
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05305049 raid_map = kmalloc_array(num_stripes, sizeof(u64),
David Woodhouse53b381b2013-01-29 18:40:14 -05005050 GFP_NOFS);
5051 if (!raid_map) {
5052 ret = -ENOMEM;
5053 goto out;
5054 }
5055
5056 /* Work out the disk rotation on this stripe-set */
5057 tmp = stripe_nr;
5058 rot = do_div(tmp, num_stripes);
5059
5060 /* Fill in the logical address of each stripe */
5061 tmp = stripe_nr * nr_data_stripes(map);
5062 for (i = 0; i < nr_data_stripes(map); i++)
5063 raid_map[(i+rot) % num_stripes] =
5064 em->start + (tmp + i) * map->stripe_len;
5065
5066 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5067 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5068 raid_map[(i+rot+1) % num_stripes] =
5069 RAID6_Q_STRIPE;
5070
5071 *length = map->stripe_len;
5072 stripe_index = 0;
5073 stripe_offset = 0;
5074 } else {
5075 /*
5076 * Mirror #0 or #1 means the original data block.
5077 * Mirror #2 is RAID5 parity block.
5078 * Mirror #3 is RAID6 Q block.
5079 */
5080 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5081 if (mirror_num > 1)
5082 stripe_index = nr_data_stripes(map) +
5083 mirror_num - 2;
5084
5085 /* We distribute the parity blocks across stripes */
5086 tmp = stripe_nr + stripe_index;
5087 stripe_index = do_div(tmp, map->num_stripes);
5088 }
Chris Mason8790d502008-04-03 16:29:03 -04005089 } else {
5090 /*
5091 * after this do_div call, stripe_nr is the number of stripes
5092 * on this device we have to walk to find the data, and
5093 * stripe_index is the number of our device in the stripe array
5094 */
5095 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005096 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005097 }
Chris Mason593060d2008-03-25 16:50:33 -04005098 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005099
Stefan Behrens472262f2012-11-06 14:43:46 +01005100 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005101 if (dev_replace_is_ongoing) {
5102 if (rw & (REQ_WRITE | REQ_DISCARD))
5103 num_alloc_stripes <<= 1;
5104 if (rw & REQ_GET_READ_MIRRORS)
5105 num_alloc_stripes++;
5106 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005107 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08005108 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04005109 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08005110 ret = -ENOMEM;
5111 goto out;
5112 }
5113 atomic_set(&bbio->error, 0);
5114
Li Dongyangfce3bb92011-03-24 10:24:26 +00005115 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08005116 int factor = 0;
5117 int sub_stripes = 0;
5118 u64 stripes_per_dev = 0;
5119 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005120 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005121
5122 if (map->type &
5123 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5124 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5125 sub_stripes = 1;
5126 else
5127 sub_stripes = map->sub_stripes;
5128
5129 factor = map->num_stripes / sub_stripes;
5130 stripes_per_dev = div_u64_rem(stripe_nr_end -
5131 stripe_nr_orig,
5132 factor,
5133 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005134 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5135 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005136 }
5137
Li Dongyangfce3bb92011-03-24 10:24:26 +00005138 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005139 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005140 map->stripes[stripe_index].physical +
5141 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005142 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005143
Li Zefanec9ef7a2011-12-01 14:06:42 +08005144 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5145 BTRFS_BLOCK_GROUP_RAID10)) {
5146 bbio->stripes[i].length = stripes_per_dev *
5147 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005148
Li Zefanec9ef7a2011-12-01 14:06:42 +08005149 if (i / sub_stripes < remaining_stripes)
5150 bbio->stripes[i].length +=
5151 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005152
5153 /*
5154 * Special for the first stripe and
5155 * the last stripe:
5156 *
5157 * |-------|...|-------|
5158 * |----------|
5159 * off end_off
5160 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005161 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005162 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005163 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005164
5165 if (stripe_index >= last_stripe &&
5166 stripe_index <= (last_stripe +
5167 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005168 bbio->stripes[i].length -=
5169 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005170
Li Zefanec9ef7a2011-12-01 14:06:42 +08005171 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005172 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005173 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005174 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005175
5176 stripe_index++;
5177 if (stripe_index == map->num_stripes) {
5178 /* This could only happen for RAID0/10 */
5179 stripe_index = 0;
5180 stripe_nr++;
5181 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005182 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005183 } else {
5184 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005185 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005186 map->stripes[stripe_index].physical +
5187 stripe_offset +
5188 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005189 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005190 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005191 stripe_index++;
5192 }
Chris Mason593060d2008-03-25 16:50:33 -04005193 }
Li Zefande11cc12011-12-01 12:55:47 +08005194
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005195 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
Li Zefande11cc12011-12-01 12:55:47 +08005196 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5197 BTRFS_BLOCK_GROUP_RAID10 |
David Woodhouse53b381b2013-01-29 18:40:14 -05005198 BTRFS_BLOCK_GROUP_RAID5 |
Li Zefande11cc12011-12-01 12:55:47 +08005199 BTRFS_BLOCK_GROUP_DUP)) {
5200 max_errors = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005201 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5202 max_errors = 2;
Li Zefande11cc12011-12-01 12:55:47 +08005203 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005204 }
Li Zefande11cc12011-12-01 12:55:47 +08005205
Stefan Behrens472262f2012-11-06 14:43:46 +01005206 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5207 dev_replace->tgtdev != NULL) {
5208 int index_where_to_add;
5209 u64 srcdev_devid = dev_replace->srcdev->devid;
5210
5211 /*
5212 * duplicate the write operations while the dev replace
5213 * procedure is running. Since the copying of the old disk
5214 * to the new disk takes place at run time while the
5215 * filesystem is mounted writable, the regular write
5216 * operations to the old disk have to be duplicated to go
5217 * to the new disk as well.
5218 * Note that device->missing is handled by the caller, and
5219 * that the write to the old disk is already set up in the
5220 * stripes array.
5221 */
5222 index_where_to_add = num_stripes;
5223 for (i = 0; i < num_stripes; i++) {
5224 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5225 /* write to new disk, too */
5226 struct btrfs_bio_stripe *new =
5227 bbio->stripes + index_where_to_add;
5228 struct btrfs_bio_stripe *old =
5229 bbio->stripes + i;
5230
5231 new->physical = old->physical;
5232 new->length = old->length;
5233 new->dev = dev_replace->tgtdev;
5234 index_where_to_add++;
5235 max_errors++;
5236 }
5237 }
5238 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005239 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5240 dev_replace->tgtdev != NULL) {
5241 u64 srcdev_devid = dev_replace->srcdev->devid;
5242 int index_srcdev = 0;
5243 int found = 0;
5244 u64 physical_of_found = 0;
5245
5246 /*
5247 * During the dev-replace procedure, the target drive can
5248 * also be used to read data in case it is needed to repair
5249 * a corrupt block elsewhere. This is possible if the
5250 * requested area is left of the left cursor. In this area,
5251 * the target drive is a full copy of the source drive.
5252 */
5253 for (i = 0; i < num_stripes; i++) {
5254 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5255 /*
5256 * In case of DUP, in order to keep it
5257 * simple, only add the mirror with the
5258 * lowest physical address
5259 */
5260 if (found &&
5261 physical_of_found <=
5262 bbio->stripes[i].physical)
5263 continue;
5264 index_srcdev = i;
5265 found = 1;
5266 physical_of_found = bbio->stripes[i].physical;
5267 }
5268 }
5269 if (found) {
5270 u64 length = map->stripe_len;
5271
5272 if (physical_of_found + length <=
5273 dev_replace->cursor_left) {
5274 struct btrfs_bio_stripe *tgtdev_stripe =
5275 bbio->stripes + num_stripes;
5276
5277 tgtdev_stripe->physical = physical_of_found;
5278 tgtdev_stripe->length =
5279 bbio->stripes[index_srcdev].length;
5280 tgtdev_stripe->dev = dev_replace->tgtdev;
5281
5282 num_stripes++;
5283 }
5284 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005285 }
5286
Li Zefande11cc12011-12-01 12:55:47 +08005287 *bbio_ret = bbio;
5288 bbio->num_stripes = num_stripes;
5289 bbio->max_errors = max_errors;
5290 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005291
5292 /*
5293 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5294 * mirror_num == num_stripes + 1 && dev_replace target drive is
5295 * available as a mirror
5296 */
5297 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5298 WARN_ON(num_stripes > 1);
5299 bbio->stripes[0].dev = dev_replace->tgtdev;
5300 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5301 bbio->mirror_num = map->num_stripes + 1;
5302 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005303 if (raid_map) {
5304 sort_parity_stripes(bbio, raid_map);
5305 *raid_map_ret = raid_map;
5306 }
Chris Masoncea9e442008-04-09 16:28:12 -04005307out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005308 if (dev_replace_is_ongoing)
5309 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005310 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005311 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005312}
5313
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005314int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005315 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005316 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005317{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005318 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005319 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005320}
5321
Yan Zhenga512bbf2008-12-08 16:46:26 -05005322int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5323 u64 chunk_start, u64 physical, u64 devid,
5324 u64 **logical, int *naddrs, int *stripe_len)
5325{
5326 struct extent_map_tree *em_tree = &map_tree->map_tree;
5327 struct extent_map *em;
5328 struct map_lookup *map;
5329 u64 *buf;
5330 u64 bytenr;
5331 u64 length;
5332 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005333 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005334 int i, j, nr = 0;
5335
Chris Mason890871b2009-09-02 16:24:52 -04005336 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005337 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005338 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005339
Josef Bacik835d9742013-03-19 12:13:25 -04005340 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005341 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005342 chunk_start);
5343 return -EIO;
5344 }
5345
5346 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005347 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005348 em->start, chunk_start);
5349 free_extent_map(em);
5350 return -EIO;
5351 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005352 map = (struct map_lookup *)em->bdev;
5353
5354 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005355 rmap_len = map->stripe_len;
5356
Yan Zhenga512bbf2008-12-08 16:46:26 -05005357 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5358 do_div(length, map->num_stripes / map->sub_stripes);
5359 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5360 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005361 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5362 BTRFS_BLOCK_GROUP_RAID6)) {
5363 do_div(length, nr_data_stripes(map));
5364 rmap_len = map->stripe_len * nr_data_stripes(map);
5365 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005366
5367 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005368 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005369
5370 for (i = 0; i < map->num_stripes; i++) {
5371 if (devid && map->stripes[i].dev->devid != devid)
5372 continue;
5373 if (map->stripes[i].physical > physical ||
5374 map->stripes[i].physical + length <= physical)
5375 continue;
5376
5377 stripe_nr = physical - map->stripes[i].physical;
5378 do_div(stripe_nr, map->stripe_len);
5379
5380 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5381 stripe_nr = stripe_nr * map->num_stripes + i;
5382 do_div(stripe_nr, map->sub_stripes);
5383 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5384 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005385 } /* else if RAID[56], multiply by nr_data_stripes().
5386 * Alternatively, just use rmap_len below instead of
5387 * map->stripe_len */
5388
5389 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005390 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005391 for (j = 0; j < nr; j++) {
5392 if (buf[j] == bytenr)
5393 break;
5394 }
Chris Mason934d3752008-12-08 16:43:10 -05005395 if (j == nr) {
5396 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005397 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005398 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005399 }
5400
Yan Zhenga512bbf2008-12-08 16:46:26 -05005401 *logical = buf;
5402 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005403 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005404
5405 free_extent_map(em);
5406 return 0;
5407}
5408
Miao Xie8408c712014-06-19 10:42:55 +08005409static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5410{
5411 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5412 bio_endio_nodec(bio, err);
5413 else
5414 bio_endio(bio, err);
5415 kfree(bbio);
5416}
5417
Jan Schmidta1d3c472011-08-04 17:15:33 +02005418static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005419{
Chris Mason9be33952013-05-17 18:30:14 -04005420 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005421 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005422 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005423
Stefan Behrens442a4f62012-05-25 16:06:08 +02005424 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005425 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005426 if (err == -EIO || err == -EREMOTEIO) {
5427 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005428 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005429
5430 BUG_ON(stripe_index >= bbio->num_stripes);
5431 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005432 if (dev->bdev) {
5433 if (bio->bi_rw & WRITE)
5434 btrfs_dev_stat_inc(dev,
5435 BTRFS_DEV_STAT_WRITE_ERRS);
5436 else
5437 btrfs_dev_stat_inc(dev,
5438 BTRFS_DEV_STAT_READ_ERRS);
5439 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5440 btrfs_dev_stat_inc(dev,
5441 BTRFS_DEV_STAT_FLUSH_ERRS);
5442 btrfs_dev_stat_print_on_error(dev);
5443 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005444 }
5445 }
Chris Mason8790d502008-04-03 16:29:03 -04005446
Jan Schmidta1d3c472011-08-04 17:15:33 +02005447 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005448 is_orig_bio = 1;
5449
Miao Xiec404e0d2014-01-30 16:46:55 +08005450 btrfs_bio_counter_dec(bbio->fs_info);
5451
Jan Schmidta1d3c472011-08-04 17:15:33 +02005452 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005453 if (!is_orig_bio) {
5454 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005455 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005456 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005457
Jan Schmidta1d3c472011-08-04 17:15:33 +02005458 bio->bi_private = bbio->private;
5459 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005460 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005461 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005462 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005463 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005464 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005465 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005466 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005467 /*
5468 * this bio is actually up to date, we didn't
5469 * go over the max number of errors
5470 */
5471 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005472 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005473 }
Miao Xiec55f1392014-06-19 10:42:54 +08005474
Miao Xie8408c712014-06-19 10:42:55 +08005475 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005476 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005477 bio_put(bio);
5478 }
Chris Mason8790d502008-04-03 16:29:03 -04005479}
5480
Chris Mason8b712842008-06-11 16:50:36 -04005481/*
5482 * see run_scheduled_bios for a description of why bios are collected for
5483 * async submit.
5484 *
5485 * This will add one bio to the pending list for a device and make sure
5486 * the work struct is scheduled.
5487 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005488static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5489 struct btrfs_device *device,
5490 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005491{
5492 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005493 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005494
David Woodhouse53b381b2013-01-29 18:40:14 -05005495 if (device->missing || !device->bdev) {
5496 bio_endio(bio, -EIO);
5497 return;
5498 }
5499
Chris Mason8b712842008-06-11 16:50:36 -04005500 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005501 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005502 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005503 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005504 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005505 return;
Chris Mason8b712842008-06-11 16:50:36 -04005506 }
5507
5508 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005509 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005510 * higher layers. Otherwise, the async bio makes it appear we have
5511 * made progress against dirty pages when we've really just put it
5512 * on a queue for later
5513 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005514 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005515 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005516 bio->bi_next = NULL;
5517 bio->bi_rw |= rw;
5518
5519 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005520 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005521 pending_bios = &device->pending_sync_bios;
5522 else
5523 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005524
Chris Masonffbd5172009-04-20 15:50:09 -04005525 if (pending_bios->tail)
5526 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005527
Chris Masonffbd5172009-04-20 15:50:09 -04005528 pending_bios->tail = bio;
5529 if (!pending_bios->head)
5530 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005531 if (device->running_pending)
5532 should_queue = 0;
5533
5534 spin_unlock(&device->io_lock);
5535
5536 if (should_queue)
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005537 btrfs_queue_work(root->fs_info->submit_workers,
5538 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005539}
5540
Josef Bacikde1ee922012-10-19 16:50:56 -04005541static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5542 sector_t sector)
5543{
5544 struct bio_vec *prev;
5545 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005546 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005547 struct bvec_merge_data bvm = {
5548 .bi_bdev = bdev,
5549 .bi_sector = sector,
5550 .bi_rw = bio->bi_rw,
5551 };
5552
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305553 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005554 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005555
5556 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005557 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005558 return 0;
5559
5560 if (!q->merge_bvec_fn)
5561 return 1;
5562
Kent Overstreet4f024f32013-10-11 15:44:27 -07005563 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005564 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5565 return 0;
5566 return 1;
5567}
5568
5569static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5570 struct bio *bio, u64 physical, int dev_nr,
5571 int rw, int async)
5572{
5573 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5574
5575 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005576 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005577 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005578 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005579#ifdef DEBUG
5580 {
5581 struct rcu_string *name;
5582
5583 rcu_read_lock();
5584 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005585 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005586 "(%s id %llu), size=%u\n", rw,
5587 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5588 name->str, dev->devid, bio->bi_size);
5589 rcu_read_unlock();
5590 }
5591#endif
5592 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005593
5594 btrfs_bio_counter_inc_noblocked(root->fs_info);
5595
Josef Bacikde1ee922012-10-19 16:50:56 -04005596 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005597 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005598 else
5599 btrfsic_submit_bio(rw, bio);
5600}
5601
5602static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5603 struct bio *first_bio, struct btrfs_device *dev,
5604 int dev_nr, int rw, int async)
5605{
5606 struct bio_vec *bvec = first_bio->bi_io_vec;
5607 struct bio *bio;
5608 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5609 u64 physical = bbio->stripes[dev_nr].physical;
5610
5611again:
5612 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5613 if (!bio)
5614 return -ENOMEM;
5615
5616 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5617 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5618 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005619 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005620
5621 atomic_inc(&bbio->stripes_pending);
5622 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5623 rw, async);
5624 physical += len;
5625 goto again;
5626 }
5627 bvec++;
5628 }
5629
5630 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5631 return 0;
5632}
5633
5634static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5635{
5636 atomic_inc(&bbio->error);
5637 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005638 /* Shoud be the original bio. */
5639 WARN_ON(bio != bbio->orig_bio);
5640
Josef Bacikde1ee922012-10-19 16:50:56 -04005641 bio->bi_private = bbio->private;
5642 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005643 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005644 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005645
5646 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005647 }
5648}
5649
Chris Masonf1885912008-04-09 16:28:12 -04005650int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005651 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005652{
Chris Mason0b86a832008-03-24 15:01:56 -04005653 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005654 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005655 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005656 u64 length = 0;
5657 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005658 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005659 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005660 int dev_nr = 0;
5661 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005662 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005663
Kent Overstreet4f024f32013-10-11 15:44:27 -07005664 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005665 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005666
Miao Xiec404e0d2014-01-30 16:46:55 +08005667 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005668 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5669 mirror_num, &raid_map);
Miao Xiec404e0d2014-01-30 16:46:55 +08005670 if (ret) {
5671 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005672 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005673 }
Chris Masoncea9e442008-04-09 16:28:12 -04005674
Jan Schmidta1d3c472011-08-04 17:15:33 +02005675 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005676 bbio->orig_bio = first_bio;
5677 bbio->private = first_bio->bi_private;
5678 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005679 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005680 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5681
5682 if (raid_map) {
5683 /* In this case, map_length has been set to the length of
5684 a single stripe; not the whole write */
5685 if (rw & WRITE) {
Miao Xiec404e0d2014-01-30 16:46:55 +08005686 ret = raid56_parity_write(root, bio, bbio,
5687 raid_map, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005688 } else {
Miao Xiec404e0d2014-01-30 16:46:55 +08005689 ret = raid56_parity_recover(root, bio, bbio,
5690 raid_map, map_length,
5691 mirror_num);
David Woodhouse53b381b2013-01-29 18:40:14 -05005692 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005693 /*
5694 * FIXME, replace dosen't support raid56 yet, please fix
5695 * it in the future.
5696 */
5697 btrfs_bio_counter_dec(root->fs_info);
5698 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005699 }
5700
Chris Masoncea9e442008-04-09 16:28:12 -04005701 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005702 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005703 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005704 BUG();
5705 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005706
Chris Masond3977122009-01-05 21:25:51 -05005707 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005708 dev = bbio->stripes[dev_nr].dev;
5709 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5710 bbio_error(bbio, first_bio, logical);
5711 dev_nr++;
5712 continue;
5713 }
5714
5715 /*
5716 * Check and see if we're ok with this bio based on it's size
5717 * and offset with the given device.
5718 */
5719 if (!bio_size_ok(dev->bdev, first_bio,
5720 bbio->stripes[dev_nr].physical >> 9)) {
5721 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5722 dev_nr, rw, async_submit);
5723 BUG_ON(ret);
5724 dev_nr++;
5725 continue;
5726 }
5727
Jan Schmidta1d3c472011-08-04 17:15:33 +02005728 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005729 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005730 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005731 } else {
5732 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005733 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005734 }
Josef Bacik606686e2012-06-04 14:03:51 -04005735
Josef Bacikde1ee922012-10-19 16:50:56 -04005736 submit_stripe_bio(root, bbio, bio,
5737 bbio->stripes[dev_nr].physical, dev_nr, rw,
5738 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005739 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005740 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005741 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005742 return 0;
5743}
5744
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005745struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005746 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005747{
Yan Zheng2b820322008-11-17 21:11:30 -05005748 struct btrfs_device *device;
5749 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005750
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005751 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005752 while (cur_devices) {
5753 if (!fsid ||
5754 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5755 device = __find_device(&cur_devices->devices,
5756 devid, uuid);
5757 if (device)
5758 return device;
5759 }
5760 cur_devices = cur_devices->seed;
5761 }
5762 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005763}
5764
Chris Masondfe25022008-05-13 13:46:40 -04005765static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5766 u64 devid, u8 *dev_uuid)
5767{
5768 struct btrfs_device *device;
5769 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5770
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005771 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5772 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005773 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005774
5775 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005776 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005777 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005778
5779 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005780 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005781
Chris Masondfe25022008-05-13 13:46:40 -04005782 return device;
5783}
5784
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005785/**
5786 * btrfs_alloc_device - allocate struct btrfs_device
5787 * @fs_info: used only for generating a new devid, can be NULL if
5788 * devid is provided (i.e. @devid != NULL).
5789 * @devid: a pointer to devid for this device. If NULL a new devid
5790 * is generated.
5791 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5792 * is generated.
5793 *
5794 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5795 * on error. Returned struct is not linked onto any lists and can be
5796 * destroyed with kfree() right away.
5797 */
5798struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5799 const u64 *devid,
5800 const u8 *uuid)
5801{
5802 struct btrfs_device *dev;
5803 u64 tmp;
5804
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305805 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005806 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005807
5808 dev = __alloc_device();
5809 if (IS_ERR(dev))
5810 return dev;
5811
5812 if (devid)
5813 tmp = *devid;
5814 else {
5815 int ret;
5816
5817 ret = find_next_devid(fs_info, &tmp);
5818 if (ret) {
5819 kfree(dev);
5820 return ERR_PTR(ret);
5821 }
5822 }
5823 dev->devid = tmp;
5824
5825 if (uuid)
5826 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5827 else
5828 generate_random_uuid(dev->uuid);
5829
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005830 btrfs_init_work(&dev->work, pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005831
5832 return dev;
5833}
5834
Chris Mason0b86a832008-03-24 15:01:56 -04005835static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5836 struct extent_buffer *leaf,
5837 struct btrfs_chunk *chunk)
5838{
5839 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5840 struct map_lookup *map;
5841 struct extent_map *em;
5842 u64 logical;
5843 u64 length;
5844 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005845 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005846 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005847 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005848 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005849
Chris Masone17cade2008-04-15 15:41:47 -04005850 logical = key->offset;
5851 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005852
Chris Mason890871b2009-09-02 16:24:52 -04005853 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005854 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005855 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005856
5857 /* already mapped? */
5858 if (em && em->start <= logical && em->start + em->len > logical) {
5859 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005860 return 0;
5861 } else if (em) {
5862 free_extent_map(em);
5863 }
Chris Mason0b86a832008-03-24 15:01:56 -04005864
David Sterba172ddd62011-04-21 00:48:27 +02005865 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005866 if (!em)
5867 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005868 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5869 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005870 if (!map) {
5871 free_extent_map(em);
5872 return -ENOMEM;
5873 }
5874
Wang Shilong298a8f92014-06-19 10:42:52 +08005875 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04005876 em->bdev = (struct block_device *)map;
5877 em->start = logical;
5878 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005879 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005880 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005881 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005882
Chris Mason593060d2008-03-25 16:50:33 -04005883 map->num_stripes = num_stripes;
5884 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5885 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5886 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5887 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5888 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005889 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005890 for (i = 0; i < num_stripes; i++) {
5891 map->stripes[i].physical =
5892 btrfs_stripe_offset_nr(leaf, chunk, i);
5893 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005894 read_extent_buffer(leaf, uuid, (unsigned long)
5895 btrfs_stripe_dev_uuid_nr(chunk, i),
5896 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005897 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5898 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005899 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005900 free_extent_map(em);
5901 return -EIO;
5902 }
Chris Masondfe25022008-05-13 13:46:40 -04005903 if (!map->stripes[i].dev) {
5904 map->stripes[i].dev =
5905 add_missing_dev(root, devid, uuid);
5906 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04005907 free_extent_map(em);
5908 return -EIO;
5909 }
5910 }
5911 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005912 }
5913
Chris Mason890871b2009-09-02 16:24:52 -04005914 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005915 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005916 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005917 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005918 free_extent_map(em);
5919
5920 return 0;
5921}
5922
Jeff Mahoney143bede2012-03-01 14:56:26 +01005923static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005924 struct btrfs_dev_item *dev_item,
5925 struct btrfs_device *device)
5926{
5927 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005928
5929 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005930 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5931 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005932 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5933 device->type = btrfs_device_type(leaf, dev_item);
5934 device->io_align = btrfs_device_io_align(leaf, dev_item);
5935 device->io_width = btrfs_device_io_width(leaf, dev_item);
5936 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005937 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005938 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005939
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02005940 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005941 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005942}
5943
Yan Zheng2b820322008-11-17 21:11:30 -05005944static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5945{
5946 struct btrfs_fs_devices *fs_devices;
5947 int ret;
5948
Li Zefanb367e472011-12-07 11:38:24 +08005949 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005950
5951 fs_devices = root->fs_info->fs_devices->seed;
5952 while (fs_devices) {
5953 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5954 ret = 0;
5955 goto out;
5956 }
5957 fs_devices = fs_devices->seed;
5958 }
5959
5960 fs_devices = find_fsid(fsid);
5961 if (!fs_devices) {
5962 ret = -ENOENT;
5963 goto out;
5964 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005965
5966 fs_devices = clone_fs_devices(fs_devices);
5967 if (IS_ERR(fs_devices)) {
5968 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005969 goto out;
5970 }
5971
Christoph Hellwig97288f22008-12-02 06:36:09 -05005972 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05005973 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02005974 if (ret) {
5975 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005976 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02005977 }
Yan Zheng2b820322008-11-17 21:11:30 -05005978
5979 if (!fs_devices->seeding) {
5980 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005981 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005982 ret = -EINVAL;
5983 goto out;
5984 }
5985
5986 fs_devices->seed = root->fs_info->fs_devices->seed;
5987 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005988out:
Yan Zheng2b820322008-11-17 21:11:30 -05005989 return ret;
5990}
5991
Chris Mason0d81ba52008-03-24 15:02:07 -04005992static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04005993 struct extent_buffer *leaf,
5994 struct btrfs_dev_item *dev_item)
5995{
5996 struct btrfs_device *device;
5997 u64 devid;
5998 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005999 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006000 u8 dev_uuid[BTRFS_UUID_SIZE];
6001
Chris Mason0b86a832008-03-24 15:01:56 -04006002 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006003 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006004 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006005 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006006 BTRFS_UUID_SIZE);
6007
6008 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6009 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05006010 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006011 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006012 }
6013
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006014 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006015 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006016 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006017 return -EIO;
6018
6019 if (!device) {
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006020 btrfs_warn(root->fs_info, "devid %llu missing", devid);
Yan Zheng2b820322008-11-17 21:11:30 -05006021 device = add_missing_dev(root, devid, dev_uuid);
6022 if (!device)
6023 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05006024 } else if (!device->missing) {
6025 /*
6026 * this happens when a device that was properly setup
6027 * in the device info lists suddenly goes bad.
6028 * device->bdev is NULL, and so we have to set
6029 * device->missing to one here
6030 */
6031 root->fs_info->fs_devices->missing_devices++;
6032 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006033 }
6034 }
6035
6036 if (device->fs_devices != root->fs_info->fs_devices) {
6037 BUG_ON(device->writeable);
6038 if (device->generation !=
6039 btrfs_device_generation(leaf, dev_item))
6040 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006041 }
Chris Mason0b86a832008-03-24 15:01:56 -04006042
6043 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006044 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006045 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006046 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006047 spin_lock(&root->fs_info->free_chunk_lock);
6048 root->fs_info->free_chunk_space += device->total_bytes -
6049 device->bytes_used;
6050 spin_unlock(&root->fs_info->free_chunk_lock);
6051 }
Chris Mason0b86a832008-03-24 15:01:56 -04006052 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006053 return ret;
6054}
6055
Yan Zhenge4404d62008-12-12 10:03:26 -05006056int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006057{
David Sterba6c417612011-04-13 15:41:04 +02006058 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006059 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006060 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006061 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04006062 u8 *ptr;
6063 unsigned long sb_ptr;
6064 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006065 u32 num_stripes;
6066 u32 array_size;
6067 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006068 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04006069 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006070
Yan Zhenge4404d62008-12-12 10:03:26 -05006071 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04006072 BTRFS_SUPER_INFO_SIZE);
6073 if (!sb)
6074 return -ENOMEM;
6075 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006076 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006077 /*
6078 * The sb extent buffer is artifical and just used to read the system array.
6079 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6080 * pages up-to-date when the page is larger: extent does not cover the
6081 * whole page and consequently check_page_uptodate does not find all
6082 * the page's extents up-to-date (the hole beyond sb),
6083 * write_extent_buffer then triggers a WARN_ON.
6084 *
6085 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6086 * but sb spans only this function. Add an explicit SetPageUptodate call
6087 * to silence the warning eg. on PowerPC 64.
6088 */
6089 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006090 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006091
Chris Masona061fc82008-05-07 11:43:44 -04006092 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006093 array_size = btrfs_super_sys_array_size(super_copy);
6094
Chris Mason0b86a832008-03-24 15:01:56 -04006095 ptr = super_copy->sys_chunk_array;
6096 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
6097 cur = 0;
6098
6099 while (cur < array_size) {
6100 disk_key = (struct btrfs_disk_key *)ptr;
6101 btrfs_disk_key_to_cpu(&key, disk_key);
6102
Chris Masona061fc82008-05-07 11:43:44 -04006103 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006104 sb_ptr += len;
6105 cur += len;
6106
Chris Mason0d81ba52008-03-24 15:02:07 -04006107 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04006108 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04006109 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006110 if (ret)
6111 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006112 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6113 len = btrfs_chunk_item_size(num_stripes);
6114 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006115 ret = -EIO;
6116 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006117 }
6118 ptr += len;
6119 sb_ptr += len;
6120 cur += len;
6121 }
Chris Masona061fc82008-05-07 11:43:44 -04006122 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006123 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006124}
6125
6126int btrfs_read_chunk_tree(struct btrfs_root *root)
6127{
6128 struct btrfs_path *path;
6129 struct extent_buffer *leaf;
6130 struct btrfs_key key;
6131 struct btrfs_key found_key;
6132 int ret;
6133 int slot;
6134
6135 root = root->fs_info->chunk_root;
6136
6137 path = btrfs_alloc_path();
6138 if (!path)
6139 return -ENOMEM;
6140
Li Zefanb367e472011-12-07 11:38:24 +08006141 mutex_lock(&uuid_mutex);
6142 lock_chunks(root);
6143
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006144 /*
6145 * Read all device items, and then all the chunk items. All
6146 * device items are found before any chunk item (their object id
6147 * is smaller than the lowest possible object id for a chunk
6148 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006149 */
6150 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6151 key.offset = 0;
6152 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006153 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006154 if (ret < 0)
6155 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006156 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006157 leaf = path->nodes[0];
6158 slot = path->slots[0];
6159 if (slot >= btrfs_header_nritems(leaf)) {
6160 ret = btrfs_next_leaf(root, path);
6161 if (ret == 0)
6162 continue;
6163 if (ret < 0)
6164 goto error;
6165 break;
6166 }
6167 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006168 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6169 struct btrfs_dev_item *dev_item;
6170 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006171 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006172 ret = read_one_dev(root, leaf, dev_item);
6173 if (ret)
6174 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006175 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6176 struct btrfs_chunk *chunk;
6177 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6178 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006179 if (ret)
6180 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006181 }
6182 path->slots[0]++;
6183 }
Chris Mason0b86a832008-03-24 15:01:56 -04006184 ret = 0;
6185error:
Li Zefanb367e472011-12-07 11:38:24 +08006186 unlock_chunks(root);
6187 mutex_unlock(&uuid_mutex);
6188
Yan Zheng2b820322008-11-17 21:11:30 -05006189 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006190 return ret;
6191}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006192
Miao Xiecb517ea2013-05-15 07:48:19 +00006193void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6194{
6195 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6196 struct btrfs_device *device;
6197
Liu Bo29cc83f2014-05-11 23:14:59 +08006198 while (fs_devices) {
6199 mutex_lock(&fs_devices->device_list_mutex);
6200 list_for_each_entry(device, &fs_devices->devices, dev_list)
6201 device->dev_root = fs_info->dev_root;
6202 mutex_unlock(&fs_devices->device_list_mutex);
6203
6204 fs_devices = fs_devices->seed;
6205 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006206}
6207
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006208static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6209{
6210 int i;
6211
6212 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6213 btrfs_dev_stat_reset(dev, i);
6214}
6215
6216int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6217{
6218 struct btrfs_key key;
6219 struct btrfs_key found_key;
6220 struct btrfs_root *dev_root = fs_info->dev_root;
6221 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6222 struct extent_buffer *eb;
6223 int slot;
6224 int ret = 0;
6225 struct btrfs_device *device;
6226 struct btrfs_path *path = NULL;
6227 int i;
6228
6229 path = btrfs_alloc_path();
6230 if (!path) {
6231 ret = -ENOMEM;
6232 goto out;
6233 }
6234
6235 mutex_lock(&fs_devices->device_list_mutex);
6236 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6237 int item_size;
6238 struct btrfs_dev_stats_item *ptr;
6239
6240 key.objectid = 0;
6241 key.type = BTRFS_DEV_STATS_KEY;
6242 key.offset = device->devid;
6243 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6244 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006245 __btrfs_reset_dev_stats(device);
6246 device->dev_stats_valid = 1;
6247 btrfs_release_path(path);
6248 continue;
6249 }
6250 slot = path->slots[0];
6251 eb = path->nodes[0];
6252 btrfs_item_key_to_cpu(eb, &found_key, slot);
6253 item_size = btrfs_item_size_nr(eb, slot);
6254
6255 ptr = btrfs_item_ptr(eb, slot,
6256 struct btrfs_dev_stats_item);
6257
6258 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6259 if (item_size >= (1 + i) * sizeof(__le64))
6260 btrfs_dev_stat_set(device, i,
6261 btrfs_dev_stats_value(eb, ptr, i));
6262 else
6263 btrfs_dev_stat_reset(device, i);
6264 }
6265
6266 device->dev_stats_valid = 1;
6267 btrfs_dev_stat_print_on_load(device);
6268 btrfs_release_path(path);
6269 }
6270 mutex_unlock(&fs_devices->device_list_mutex);
6271
6272out:
6273 btrfs_free_path(path);
6274 return ret < 0 ? ret : 0;
6275}
6276
6277static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6278 struct btrfs_root *dev_root,
6279 struct btrfs_device *device)
6280{
6281 struct btrfs_path *path;
6282 struct btrfs_key key;
6283 struct extent_buffer *eb;
6284 struct btrfs_dev_stats_item *ptr;
6285 int ret;
6286 int i;
6287
6288 key.objectid = 0;
6289 key.type = BTRFS_DEV_STATS_KEY;
6290 key.offset = device->devid;
6291
6292 path = btrfs_alloc_path();
6293 BUG_ON(!path);
6294 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6295 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006296 printk_in_rcu(KERN_WARNING "BTRFS: "
6297 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006298 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006299 goto out;
6300 }
6301
6302 if (ret == 0 &&
6303 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6304 /* need to delete old one and insert a new one */
6305 ret = btrfs_del_item(trans, dev_root, path);
6306 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006307 printk_in_rcu(KERN_WARNING "BTRFS: "
6308 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006309 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006310 goto out;
6311 }
6312 ret = 1;
6313 }
6314
6315 if (ret == 1) {
6316 /* need to insert a new item */
6317 btrfs_release_path(path);
6318 ret = btrfs_insert_empty_item(trans, dev_root, path,
6319 &key, sizeof(*ptr));
6320 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006321 printk_in_rcu(KERN_WARNING "BTRFS: "
6322 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006323 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006324 goto out;
6325 }
6326 }
6327
6328 eb = path->nodes[0];
6329 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6330 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6331 btrfs_set_dev_stats_value(eb, ptr, i,
6332 btrfs_dev_stat_read(device, i));
6333 btrfs_mark_buffer_dirty(eb);
6334
6335out:
6336 btrfs_free_path(path);
6337 return ret;
6338}
6339
6340/*
6341 * called from commit_transaction. Writes all changed device stats to disk.
6342 */
6343int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6344 struct btrfs_fs_info *fs_info)
6345{
6346 struct btrfs_root *dev_root = fs_info->dev_root;
6347 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6348 struct btrfs_device *device;
6349 int ret = 0;
6350
6351 mutex_lock(&fs_devices->device_list_mutex);
6352 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6353 if (!device->dev_stats_valid || !device->dev_stats_dirty)
6354 continue;
6355
6356 ret = update_dev_stat_item(trans, dev_root, device);
6357 if (!ret)
6358 device->dev_stats_dirty = 0;
6359 }
6360 mutex_unlock(&fs_devices->device_list_mutex);
6361
6362 return ret;
6363}
6364
Stefan Behrens442a4f62012-05-25 16:06:08 +02006365void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6366{
6367 btrfs_dev_stat_inc(dev, index);
6368 btrfs_dev_stat_print_on_error(dev);
6369}
6370
Eric Sandeen48a3b632013-04-25 20:41:01 +00006371static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006372{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006373 if (!dev->dev_stats_valid)
6374 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006375 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6376 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006377 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006378 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6379 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6380 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006381 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6382 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006383}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006384
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006385static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6386{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006387 int i;
6388
6389 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6390 if (btrfs_dev_stat_read(dev, i) != 0)
6391 break;
6392 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6393 return; /* all values == 0, suppress message */
6394
Frank Holtonefe120a2013-12-20 11:37:06 -05006395 printk_in_rcu(KERN_INFO "BTRFS: "
6396 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006397 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006398 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6399 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6400 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6401 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6402 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6403}
6404
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006405int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006406 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006407{
6408 struct btrfs_device *dev;
6409 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6410 int i;
6411
6412 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006413 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006414 mutex_unlock(&fs_devices->device_list_mutex);
6415
6416 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006417 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006418 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006419 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006420 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006421 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006422 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006423 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6424 if (stats->nr_items > i)
6425 stats->values[i] =
6426 btrfs_dev_stat_read_and_reset(dev, i);
6427 else
6428 btrfs_dev_stat_reset(dev, i);
6429 }
6430 } else {
6431 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6432 if (stats->nr_items > i)
6433 stats->values[i] = btrfs_dev_stat_read(dev, i);
6434 }
6435 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6436 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6437 return 0;
6438}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006439
6440int btrfs_scratch_superblock(struct btrfs_device *device)
6441{
6442 struct buffer_head *bh;
6443 struct btrfs_super_block *disk_super;
6444
6445 bh = btrfs_read_dev_super(device->bdev);
6446 if (!bh)
6447 return -EINVAL;
6448 disk_super = (struct btrfs_super_block *)bh->b_data;
6449
6450 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6451 set_buffer_dirty(bh);
6452 sync_dirty_buffer(bh);
6453 brelse(bh);
6454
6455 return 0;
6456}