blob: 5700ab03e84bba10a4b5c4920eadced5099a8fc5 [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 */
Anand Jain77bdae42014-07-03 18:22:06 +0800535 if (fs_devices->opened) {
Anand Jainb96de002014-07-03 18:22:05 +0800536 return -EBUSY;
Anand Jain77bdae42014-07-03 18:22:06 +0800537 } else {
538 /*
539 * That is if the FS is _not_ mounted and if you
540 * are here, that means there is more than one
541 * disk with same uuid and devid.We keep the one
542 * with larger generation number or the last-in if
543 * generation are equal.
544 */
545 if (found_transid < device->generation)
546 return -EEXIST;
547 }
Anand Jainb96de002014-07-03 18:22:05 +0800548
Josef Bacik606686e2012-06-04 14:03:51 -0400549 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000550 if (!name)
551 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400552 rcu_string_free(device->name);
553 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500554 if (device->missing) {
555 fs_devices->missing_devices--;
556 device->missing = 0;
557 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400558 }
559
Anand Jain77bdae42014-07-03 18:22:06 +0800560 /*
561 * Unmount does not free the btrfs_device struct but would zero
562 * generation along with most of the other members. So just update
563 * it back. We need it to pick the disk with largest generation
564 * (as above).
565 */
566 if (!fs_devices->opened)
567 device->generation = found_transid;
568
Chris Mason8a4b83c2008-03-24 15:02:07 -0400569 if (found_transid > fs_devices->latest_trans) {
570 fs_devices->latest_devid = devid;
571 fs_devices->latest_trans = found_transid;
572 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400573 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100574
575 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400576}
577
Yan Zhenge4404d62008-12-12 10:03:26 -0500578static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
579{
580 struct btrfs_fs_devices *fs_devices;
581 struct btrfs_device *device;
582 struct btrfs_device *orig_dev;
583
Ilya Dryomov2208a372013-08-12 14:33:03 +0300584 fs_devices = alloc_fs_devices(orig->fsid);
585 if (IS_ERR(fs_devices))
586 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500587
Yan Zhenge4404d62008-12-12 10:03:26 -0500588 fs_devices->latest_devid = orig->latest_devid;
589 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400590 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500591
Xiao Guangrong46224702011-04-20 10:08:47 +0000592 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500593 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400594 struct rcu_string *name;
595
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300596 device = btrfs_alloc_device(NULL, &orig_dev->devid,
597 orig_dev->uuid);
598 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500599 goto error;
600
Josef Bacik606686e2012-06-04 14:03:51 -0400601 /*
602 * This is ok to do without rcu read locked because we hold the
603 * uuid mutex so nothing we touch in here is going to disappear.
604 */
Anand Jaine755f782014-06-30 17:12:47 +0800605 if (orig_dev->name) {
606 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
607 if (!name) {
608 kfree(device);
609 goto error;
610 }
611 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400612 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500613
Yan Zhenge4404d62008-12-12 10:03:26 -0500614 list_add(&device->dev_list, &fs_devices->devices);
615 device->fs_devices = fs_devices;
616 fs_devices->num_devices++;
617 }
618 return fs_devices;
619error:
620 free_fs_devices(fs_devices);
621 return ERR_PTR(-ENOMEM);
622}
623
Stefan Behrens8dabb742012-11-06 13:15:27 +0100624void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
625 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400626{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500627 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400628
Chris Masona6b0d5c2012-02-20 20:53:43 -0500629 struct block_device *latest_bdev = NULL;
630 u64 latest_devid = 0;
631 u64 latest_transid = 0;
632
Chris Masondfe25022008-05-13 13:46:40 -0400633 mutex_lock(&uuid_mutex);
634again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000635 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500636 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500637 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100638 if (!device->is_tgtdev_for_dev_replace &&
639 (!latest_transid ||
640 device->generation > latest_transid)) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500641 latest_devid = device->devid;
642 latest_transid = device->generation;
643 latest_bdev = device->bdev;
644 }
Yan Zheng2b820322008-11-17 21:11:30 -0500645 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500646 }
Yan Zheng2b820322008-11-17 21:11:30 -0500647
Stefan Behrens8dabb742012-11-06 13:15:27 +0100648 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
649 /*
650 * In the first step, keep the device which has
651 * the correct fsid and the devid that is used
652 * for the dev_replace procedure.
653 * In the second step, the dev_replace state is
654 * read from the device tree and it is known
655 * whether the procedure is really active or
656 * not, which means whether this device is
657 * used or whether it should be removed.
658 */
659 if (step == 0 || device->is_tgtdev_for_dev_replace) {
660 continue;
661 }
662 }
Yan Zheng2b820322008-11-17 21:11:30 -0500663 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100664 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500665 device->bdev = NULL;
666 fs_devices->open_devices--;
667 }
668 if (device->writeable) {
669 list_del_init(&device->dev_alloc_list);
670 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100671 if (!device->is_tgtdev_for_dev_replace)
672 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500673 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500674 list_del_init(&device->dev_list);
675 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400676 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500677 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400678 }
Yan Zheng2b820322008-11-17 21:11:30 -0500679
680 if (fs_devices->seed) {
681 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500682 goto again;
683 }
684
Chris Masona6b0d5c2012-02-20 20:53:43 -0500685 fs_devices->latest_bdev = latest_bdev;
686 fs_devices->latest_devid = latest_devid;
687 fs_devices->latest_trans = latest_transid;
688
Chris Masondfe25022008-05-13 13:46:40 -0400689 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400690}
Chris Masona0af4692008-05-13 16:03:06 -0400691
Xiao Guangrong1f781602011-04-20 10:09:16 +0000692static void __free_device(struct work_struct *work)
693{
694 struct btrfs_device *device;
695
696 device = container_of(work, struct btrfs_device, rcu_work);
697
698 if (device->bdev)
699 blkdev_put(device->bdev, device->mode);
700
Josef Bacik606686e2012-06-04 14:03:51 -0400701 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000702 kfree(device);
703}
704
705static void free_device(struct rcu_head *head)
706{
707 struct btrfs_device *device;
708
709 device = container_of(head, struct btrfs_device, rcu);
710
711 INIT_WORK(&device->rcu_work, __free_device);
712 schedule_work(&device->rcu_work);
713}
714
Yan Zheng2b820322008-11-17 21:11:30 -0500715static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400716{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400717 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500718
Yan Zheng2b820322008-11-17 21:11:30 -0500719 if (--fs_devices->opened > 0)
720 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400721
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000722 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500723 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000724 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400725 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000726
727 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400728 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000729
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300730 if (device->writeable &&
731 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500732 list_del_init(&device->dev_alloc_list);
733 fs_devices->rw_devices--;
734 }
735
Josef Bacikd5e20032011-08-04 14:52:27 +0000736 if (device->can_discard)
737 fs_devices->num_can_discard--;
Josef Bacik726551e2013-08-26 13:45:53 -0400738 if (device->missing)
739 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000740
Ilya Dryomova1e87802013-08-12 14:33:04 +0300741 new_device = btrfs_alloc_device(NULL, &device->devid,
742 device->uuid);
743 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400744
745 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400746 if (device->name) {
747 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300748 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400749 rcu_assign_pointer(new_device->name, name);
750 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300751
Xiao Guangrong1f781602011-04-20 10:09:16 +0000752 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300753 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000754
755 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400756 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000757 mutex_unlock(&fs_devices->device_list_mutex);
758
Yan Zhenge4404d62008-12-12 10:03:26 -0500759 WARN_ON(fs_devices->open_devices);
760 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500761 fs_devices->opened = 0;
762 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500763
Chris Mason8a4b83c2008-03-24 15:02:07 -0400764 return 0;
765}
766
Yan Zheng2b820322008-11-17 21:11:30 -0500767int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
768{
Yan Zhenge4404d62008-12-12 10:03:26 -0500769 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500770 int ret;
771
772 mutex_lock(&uuid_mutex);
773 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500774 if (!fs_devices->opened) {
775 seed_devices = fs_devices->seed;
776 fs_devices->seed = NULL;
777 }
Yan Zheng2b820322008-11-17 21:11:30 -0500778 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500779
780 while (seed_devices) {
781 fs_devices = seed_devices;
782 seed_devices = fs_devices->seed;
783 __btrfs_close_devices(fs_devices);
784 free_fs_devices(fs_devices);
785 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000786 /*
787 * Wait for rcu kworkers under __btrfs_close_devices
788 * to finish all blkdev_puts so device is really
789 * free when umount is done.
790 */
791 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500792 return ret;
793}
794
Yan Zhenge4404d62008-12-12 10:03:26 -0500795static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
796 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400797{
Josef Bacikd5e20032011-08-04 14:52:27 +0000798 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400799 struct block_device *bdev;
800 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400801 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400802 struct block_device *latest_bdev = NULL;
803 struct buffer_head *bh;
804 struct btrfs_super_block *disk_super;
805 u64 latest_devid = 0;
806 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400807 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500808 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400809 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400810
Tejun Heod4d77622010-11-13 11:55:18 +0100811 flags |= FMODE_EXCL;
812
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500813 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400814 if (device->bdev)
815 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400816 if (!device->name)
817 continue;
818
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000819 /* Just open everything we can; ignore failures here */
820 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
821 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100822 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400823
824 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000825 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400826 if (devid != device->devid)
827 goto error_brelse;
828
Yan Zheng2b820322008-11-17 21:11:30 -0500829 if (memcmp(device->uuid, disk_super->dev_item.uuid,
830 BTRFS_UUID_SIZE))
831 goto error_brelse;
832
833 device->generation = btrfs_super_generation(disk_super);
834 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400835 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500836 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400837 latest_bdev = bdev;
838 }
839
Yan Zheng2b820322008-11-17 21:11:30 -0500840 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
841 device->writeable = 0;
842 } else {
843 device->writeable = !bdev_read_only(bdev);
844 seeding = 0;
845 }
846
Josef Bacikd5e20032011-08-04 14:52:27 +0000847 q = bdev_get_queue(bdev);
848 if (blk_queue_discard(q)) {
849 device->can_discard = 1;
850 fs_devices->num_can_discard++;
851 }
852
Chris Mason8a4b83c2008-03-24 15:02:07 -0400853 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400854 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500855 device->mode = flags;
856
Chris Masonc2898112009-06-10 09:51:32 -0400857 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
858 fs_devices->rotating = 1;
859
Chris Masona0af4692008-05-13 16:03:06 -0400860 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300861 if (device->writeable &&
862 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500863 fs_devices->rw_devices++;
864 list_add(&device->dev_alloc_list,
865 &fs_devices->alloc_list);
866 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000867 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400868 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400869
Chris Masona0af4692008-05-13 16:03:06 -0400870error_brelse:
871 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100872 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400873 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400874 }
Chris Masona0af4692008-05-13 16:03:06 -0400875 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300876 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400877 goto out;
878 }
Yan Zheng2b820322008-11-17 21:11:30 -0500879 fs_devices->seeding = seeding;
880 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400881 fs_devices->latest_bdev = latest_bdev;
882 fs_devices->latest_devid = latest_devid;
883 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500884 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400885out:
Yan Zheng2b820322008-11-17 21:11:30 -0500886 return ret;
887}
888
889int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500890 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500891{
892 int ret;
893
894 mutex_lock(&uuid_mutex);
895 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500896 fs_devices->opened++;
897 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500898 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500899 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500900 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400901 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400902 return ret;
903}
904
David Sterba6f60cbd2013-02-15 11:31:02 -0700905/*
906 * Look for a btrfs signature on a device. This may be called out of the mount path
907 * and we are not allowed to call set_blocksize during the scan. The superblock
908 * is read via pagecache
909 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500910int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400911 struct btrfs_fs_devices **fs_devices_ret)
912{
913 struct btrfs_super_block *disk_super;
914 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700915 struct page *page;
916 void *p;
917 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400918 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400919 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400920 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700921 u64 bytenr;
922 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400923
David Sterba6f60cbd2013-02-15 11:31:02 -0700924 /*
925 * we would like to check all the supers, but that would make
926 * a btrfs mount succeed after a mkfs from a different FS.
927 * So, we need to add a special mount option to scan for
928 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
929 */
930 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100931 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500932 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700933
934 bdev = blkdev_get_by_path(path, flags, holder);
935
936 if (IS_ERR(bdev)) {
937 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100938 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700939 }
940
941 /* make sure our super fits in the device */
942 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
943 goto error_bdev_put;
944
945 /* make sure our super fits in the page */
946 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
947 goto error_bdev_put;
948
949 /* make sure our super doesn't straddle pages on disk */
950 index = bytenr >> PAGE_CACHE_SHIFT;
951 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
952 goto error_bdev_put;
953
954 /* pull in the page with our super */
955 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
956 index, GFP_NOFS);
957
958 if (IS_ERR_OR_NULL(page))
959 goto error_bdev_put;
960
961 p = kmap(page);
962
963 /* align our pointer to the offset of the super block */
964 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
965
966 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800967 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700968 goto error_unmap;
969
Xiao Guangronga3438322010-01-06 11:48:18 +0000970 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400971 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400972 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700973
Chris Mason8a4b83c2008-03-24 15:02:07 -0400974 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100975 if (ret > 0) {
976 if (disk_super->label[0]) {
977 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
978 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
979 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
980 } else {
981 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
982 }
983
984 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
985 ret = 0;
986 }
Josef Bacik02db0842012-06-21 16:03:58 -0400987 if (!ret && fs_devices_ret)
988 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700989
990error_unmap:
991 kunmap(page);
992 page_cache_release(page);
993
994error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100995 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400996error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100997 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400998 return ret;
999}
Chris Mason0b86a832008-03-24 15:01:56 -04001000
Miao Xie6d07bce2011-01-05 10:07:31 +00001001/* helper to account the used device space in the range */
1002int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1003 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001004{
1005 struct btrfs_key key;
1006 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001007 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001008 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001009 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001010 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001011 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001012 struct extent_buffer *l;
1013
Miao Xie6d07bce2011-01-05 10:07:31 +00001014 *length = 0;
1015
Stefan Behrens63a212a2012-11-05 18:29:28 +01001016 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +00001017 return 0;
1018
Yan Zheng2b820322008-11-17 21:11:30 -05001019 path = btrfs_alloc_path();
1020 if (!path)
1021 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001022 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001023
Chris Mason0b86a832008-03-24 15:01:56 -04001024 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001025 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001026 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001027
1028 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001029 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001030 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001031 if (ret > 0) {
1032 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1033 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001034 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001035 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001036
Chris Mason0b86a832008-03-24 15:01:56 -04001037 while (1) {
1038 l = path->nodes[0];
1039 slot = path->slots[0];
1040 if (slot >= btrfs_header_nritems(l)) {
1041 ret = btrfs_next_leaf(root, path);
1042 if (ret == 0)
1043 continue;
1044 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001045 goto out;
1046
1047 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001048 }
1049 btrfs_item_key_to_cpu(l, &key, slot);
1050
1051 if (key.objectid < device->devid)
1052 goto next;
1053
1054 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001055 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001056
Chris Masond3977122009-01-05 21:25:51 -05001057 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001058 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001059
Chris Mason0b86a832008-03-24 15:01:56 -04001060 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001061 extent_end = key.offset + btrfs_dev_extent_length(l,
1062 dev_extent);
1063 if (key.offset <= start && extent_end > end) {
1064 *length = end - start + 1;
1065 break;
1066 } else if (key.offset <= start && extent_end > start)
1067 *length += extent_end - start;
1068 else if (key.offset > start && extent_end <= end)
1069 *length += extent_end - key.offset;
1070 else if (key.offset > start && key.offset <= end) {
1071 *length += end - key.offset + 1;
1072 break;
1073 } else if (key.offset > end)
1074 break;
1075
1076next:
1077 path->slots[0]++;
1078 }
1079 ret = 0;
1080out:
1081 btrfs_free_path(path);
1082 return ret;
1083}
1084
Josef Bacik6df9a952013-06-27 13:22:46 -04001085static int contains_pending_extent(struct btrfs_trans_handle *trans,
1086 struct btrfs_device *device,
1087 u64 *start, u64 len)
1088{
1089 struct extent_map *em;
1090 int ret = 0;
1091
1092 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
1093 struct map_lookup *map;
1094 int i;
1095
1096 map = (struct map_lookup *)em->bdev;
1097 for (i = 0; i < map->num_stripes; i++) {
1098 if (map->stripes[i].dev != device)
1099 continue;
1100 if (map->stripes[i].physical >= *start + len ||
1101 map->stripes[i].physical + em->orig_block_len <=
1102 *start)
1103 continue;
1104 *start = map->stripes[i].physical +
1105 em->orig_block_len;
1106 ret = 1;
1107 }
1108 }
1109
1110 return ret;
1111}
1112
1113
Chris Mason0b86a832008-03-24 15:01:56 -04001114/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001115 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001116 * @device: the device which we search the free space in
1117 * @num_bytes: the size of the free space that we need
1118 * @start: store the start of the free space.
1119 * @len: the size of the free space. that we find, or the size of the max
1120 * free space if we don't find suitable free space
1121 *
Chris Mason0b86a832008-03-24 15:01:56 -04001122 * this uses a pretty simple search, the expectation is that it is
1123 * called very infrequently and that a given device has a small number
1124 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001125 *
1126 * @start is used to store the start of the free space if we find. But if we
1127 * don't find suitable free space, it will be used to store the start position
1128 * of the max free space.
1129 *
1130 * @len is used to store the size of the free space that we find.
1131 * But if we don't find suitable free space, it is used to store the size of
1132 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001133 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001134int find_free_dev_extent(struct btrfs_trans_handle *trans,
1135 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001136 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001137{
1138 struct btrfs_key key;
1139 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001140 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001141 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001142 u64 hole_size;
1143 u64 max_hole_start;
1144 u64 max_hole_size;
1145 u64 extent_end;
1146 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001147 u64 search_end = device->total_bytes;
1148 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001149 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001150 struct extent_buffer *l;
1151
Chris Mason0b86a832008-03-24 15:01:56 -04001152 /* FIXME use last free of some kind */
1153
1154 /* we don't want to overwrite the superblock on the drive,
1155 * so we make sure to start at an offset of at least 1MB
1156 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001157 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001158
Josef Bacik6df9a952013-06-27 13:22:46 -04001159 path = btrfs_alloc_path();
1160 if (!path)
1161 return -ENOMEM;
1162again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001163 max_hole_start = search_start;
1164 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001165 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001166
Stefan Behrens63a212a2012-11-05 18:29:28 +01001167 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001168 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001169 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001170 }
1171
Miao Xie7bfc8372011-01-05 10:07:26 +00001172 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001173 path->search_commit_root = 1;
1174 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001175
Chris Mason0b86a832008-03-24 15:01:56 -04001176 key.objectid = device->devid;
1177 key.offset = search_start;
1178 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001179
Li Zefan125ccb02011-12-08 15:07:24 +08001180 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001181 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001182 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001183 if (ret > 0) {
1184 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1185 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001186 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001187 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001188
Chris Mason0b86a832008-03-24 15:01:56 -04001189 while (1) {
1190 l = path->nodes[0];
1191 slot = path->slots[0];
1192 if (slot >= btrfs_header_nritems(l)) {
1193 ret = btrfs_next_leaf(root, path);
1194 if (ret == 0)
1195 continue;
1196 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001197 goto out;
1198
1199 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001200 }
1201 btrfs_item_key_to_cpu(l, &key, slot);
1202
1203 if (key.objectid < device->devid)
1204 goto next;
1205
1206 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001207 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001208
Chris Mason0b86a832008-03-24 15:01:56 -04001209 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1210 goto next;
1211
Miao Xie7bfc8372011-01-05 10:07:26 +00001212 if (key.offset > search_start) {
1213 hole_size = key.offset - search_start;
1214
Josef Bacik6df9a952013-06-27 13:22:46 -04001215 /*
1216 * Have to check before we set max_hole_start, otherwise
1217 * we could end up sending back this offset anyway.
1218 */
1219 if (contains_pending_extent(trans, device,
1220 &search_start,
1221 hole_size))
1222 hole_size = 0;
1223
Miao Xie7bfc8372011-01-05 10:07:26 +00001224 if (hole_size > max_hole_size) {
1225 max_hole_start = search_start;
1226 max_hole_size = hole_size;
1227 }
1228
1229 /*
1230 * If this free space is greater than which we need,
1231 * it must be the max free space that we have found
1232 * until now, so max_hole_start must point to the start
1233 * of this free space and the length of this free space
1234 * is stored in max_hole_size. Thus, we return
1235 * max_hole_start and max_hole_size and go back to the
1236 * caller.
1237 */
1238 if (hole_size >= num_bytes) {
1239 ret = 0;
1240 goto out;
1241 }
1242 }
1243
Chris Mason0b86a832008-03-24 15:01:56 -04001244 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001245 extent_end = key.offset + btrfs_dev_extent_length(l,
1246 dev_extent);
1247 if (extent_end > search_start)
1248 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001249next:
1250 path->slots[0]++;
1251 cond_resched();
1252 }
Chris Mason0b86a832008-03-24 15:01:56 -04001253
liubo38c01b92011-08-02 02:39:03 +00001254 /*
1255 * At this point, search_start should be the end of
1256 * allocated dev extents, and when shrinking the device,
1257 * search_end may be smaller than search_start.
1258 */
1259 if (search_end > search_start)
1260 hole_size = search_end - search_start;
1261
Miao Xie7bfc8372011-01-05 10:07:26 +00001262 if (hole_size > max_hole_size) {
1263 max_hole_start = search_start;
1264 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001265 }
Chris Mason0b86a832008-03-24 15:01:56 -04001266
Josef Bacik6df9a952013-06-27 13:22:46 -04001267 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1268 btrfs_release_path(path);
1269 goto again;
1270 }
1271
Miao Xie7bfc8372011-01-05 10:07:26 +00001272 /* See above. */
1273 if (hole_size < num_bytes)
1274 ret = -ENOSPC;
1275 else
1276 ret = 0;
1277
1278out:
Yan Zheng2b820322008-11-17 21:11:30 -05001279 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001280 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001281 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001282 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001283 return ret;
1284}
1285
Christoph Hellwigb2950862008-12-02 09:54:17 -05001286static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001287 struct btrfs_device *device,
1288 u64 start)
1289{
1290 int ret;
1291 struct btrfs_path *path;
1292 struct btrfs_root *root = device->dev_root;
1293 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001294 struct btrfs_key found_key;
1295 struct extent_buffer *leaf = NULL;
1296 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001297
1298 path = btrfs_alloc_path();
1299 if (!path)
1300 return -ENOMEM;
1301
1302 key.objectid = device->devid;
1303 key.offset = start;
1304 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001305again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001306 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001307 if (ret > 0) {
1308 ret = btrfs_previous_item(root, path, key.objectid,
1309 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001310 if (ret)
1311 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001312 leaf = path->nodes[0];
1313 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1314 extent = btrfs_item_ptr(leaf, path->slots[0],
1315 struct btrfs_dev_extent);
1316 BUG_ON(found_key.offset > start || found_key.offset +
1317 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001318 key = found_key;
1319 btrfs_release_path(path);
1320 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001321 } else if (ret == 0) {
1322 leaf = path->nodes[0];
1323 extent = btrfs_item_ptr(leaf, path->slots[0],
1324 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001325 } else {
1326 btrfs_error(root->fs_info, ret, "Slot search failed");
1327 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001328 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001329
Josef Bacik2bf64752011-09-26 17:12:22 -04001330 if (device->bytes_used > 0) {
1331 u64 len = btrfs_dev_extent_length(leaf, extent);
1332 device->bytes_used -= len;
1333 spin_lock(&root->fs_info->free_chunk_lock);
1334 root->fs_info->free_chunk_space += len;
1335 spin_unlock(&root->fs_info->free_chunk_lock);
1336 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001337 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001338 if (ret) {
1339 btrfs_error(root->fs_info, ret,
1340 "Failed to remove dev extent item");
1341 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001342out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001343 btrfs_free_path(path);
1344 return ret;
1345}
1346
Eric Sandeen48a3b632013-04-25 20:41:01 +00001347static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1348 struct btrfs_device *device,
1349 u64 chunk_tree, u64 chunk_objectid,
1350 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001351{
1352 int ret;
1353 struct btrfs_path *path;
1354 struct btrfs_root *root = device->dev_root;
1355 struct btrfs_dev_extent *extent;
1356 struct extent_buffer *leaf;
1357 struct btrfs_key key;
1358
Chris Masondfe25022008-05-13 13:46:40 -04001359 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001360 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001361 path = btrfs_alloc_path();
1362 if (!path)
1363 return -ENOMEM;
1364
Chris Mason0b86a832008-03-24 15:01:56 -04001365 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001366 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001367 key.type = BTRFS_DEV_EXTENT_KEY;
1368 ret = btrfs_insert_empty_item(trans, root, path, &key,
1369 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001370 if (ret)
1371 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001372
1373 leaf = path->nodes[0];
1374 extent = btrfs_item_ptr(leaf, path->slots[0],
1375 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001376 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1377 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1378 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1379
1380 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001381 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001382
Chris Mason0b86a832008-03-24 15:01:56 -04001383 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1384 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001385out:
Chris Mason0b86a832008-03-24 15:01:56 -04001386 btrfs_free_path(path);
1387 return ret;
1388}
1389
Josef Bacik6df9a952013-06-27 13:22:46 -04001390static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001391{
Josef Bacik6df9a952013-06-27 13:22:46 -04001392 struct extent_map_tree *em_tree;
1393 struct extent_map *em;
1394 struct rb_node *n;
1395 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001396
Josef Bacik6df9a952013-06-27 13:22:46 -04001397 em_tree = &fs_info->mapping_tree.map_tree;
1398 read_lock(&em_tree->lock);
1399 n = rb_last(&em_tree->map);
1400 if (n) {
1401 em = rb_entry(n, struct extent_map, rb_node);
1402 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001403 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001404 read_unlock(&em_tree->lock);
1405
Chris Mason0b86a832008-03-24 15:01:56 -04001406 return ret;
1407}
1408
Ilya Dryomov53f10652013-08-12 14:33:01 +03001409static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1410 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001411{
1412 int ret;
1413 struct btrfs_key key;
1414 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001415 struct btrfs_path *path;
1416
Yan Zheng2b820322008-11-17 21:11:30 -05001417 path = btrfs_alloc_path();
1418 if (!path)
1419 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001420
1421 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1422 key.type = BTRFS_DEV_ITEM_KEY;
1423 key.offset = (u64)-1;
1424
Ilya Dryomov53f10652013-08-12 14:33:01 +03001425 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001426 if (ret < 0)
1427 goto error;
1428
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001429 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001430
Ilya Dryomov53f10652013-08-12 14:33:01 +03001431 ret = btrfs_previous_item(fs_info->chunk_root, path,
1432 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001433 BTRFS_DEV_ITEM_KEY);
1434 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001435 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001436 } else {
1437 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1438 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001439 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001440 }
1441 ret = 0;
1442error:
Yan Zheng2b820322008-11-17 21:11:30 -05001443 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001444 return ret;
1445}
1446
1447/*
1448 * the device information is stored in the chunk root
1449 * the btrfs_device struct should be fully filled in
1450 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001451static int btrfs_add_device(struct btrfs_trans_handle *trans,
1452 struct btrfs_root *root,
1453 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001454{
1455 int ret;
1456 struct btrfs_path *path;
1457 struct btrfs_dev_item *dev_item;
1458 struct extent_buffer *leaf;
1459 struct btrfs_key key;
1460 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001461
1462 root = root->fs_info->chunk_root;
1463
1464 path = btrfs_alloc_path();
1465 if (!path)
1466 return -ENOMEM;
1467
Chris Mason0b86a832008-03-24 15:01:56 -04001468 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1469 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001470 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001471
1472 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001473 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001474 if (ret)
1475 goto out;
1476
1477 leaf = path->nodes[0];
1478 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1479
1480 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001481 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001482 btrfs_set_device_type(leaf, dev_item, device->type);
1483 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1484 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1485 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001486 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1487 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001488 btrfs_set_device_group(leaf, dev_item, 0);
1489 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1490 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001491 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001492
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001493 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001494 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001495 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001496 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001497 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001498
Yan Zheng2b820322008-11-17 21:11:30 -05001499 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001500out:
1501 btrfs_free_path(path);
1502 return ret;
1503}
Chris Mason8f18cf12008-04-25 16:53:30 -04001504
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001505/*
1506 * Function to update ctime/mtime for a given device path.
1507 * Mainly used for ctime/mtime based probe like libblkid.
1508 */
1509static void update_dev_time(char *path_name)
1510{
1511 struct file *filp;
1512
1513 filp = filp_open(path_name, O_RDWR, 0);
1514 if (!filp)
1515 return;
1516 file_update_time(filp);
1517 filp_close(filp, NULL);
1518 return;
1519}
1520
Chris Masona061fc82008-05-07 11:43:44 -04001521static int btrfs_rm_dev_item(struct btrfs_root *root,
1522 struct btrfs_device *device)
1523{
1524 int ret;
1525 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001526 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001527 struct btrfs_trans_handle *trans;
1528
1529 root = root->fs_info->chunk_root;
1530
1531 path = btrfs_alloc_path();
1532 if (!path)
1533 return -ENOMEM;
1534
Yan, Zhenga22285a2010-05-16 10:48:46 -04001535 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001536 if (IS_ERR(trans)) {
1537 btrfs_free_path(path);
1538 return PTR_ERR(trans);
1539 }
Chris Masona061fc82008-05-07 11:43:44 -04001540 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1541 key.type = BTRFS_DEV_ITEM_KEY;
1542 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001543 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001544
1545 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1546 if (ret < 0)
1547 goto out;
1548
1549 if (ret > 0) {
1550 ret = -ENOENT;
1551 goto out;
1552 }
1553
1554 ret = btrfs_del_item(trans, root, path);
1555 if (ret)
1556 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001557out:
1558 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001559 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001560 btrfs_commit_transaction(trans, root);
1561 return ret;
1562}
1563
1564int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1565{
1566 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001567 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001568 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001569 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001570 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001571 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001572 u64 all_avail;
1573 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001574 u64 num_devices;
1575 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001576 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001577 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001578 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001579
Chris Masona061fc82008-05-07 11:43:44 -04001580 mutex_lock(&uuid_mutex);
1581
Miao Xiede98ced2013-01-29 10:13:12 +00001582 do {
1583 seq = read_seqbegin(&root->fs_info->profiles_lock);
1584
1585 all_avail = root->fs_info->avail_data_alloc_bits |
1586 root->fs_info->avail_system_alloc_bits |
1587 root->fs_info->avail_metadata_alloc_bits;
1588 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001589
Stefan Behrens8dabb742012-11-06 13:15:27 +01001590 num_devices = root->fs_info->fs_devices->num_devices;
1591 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1592 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1593 WARN_ON(num_devices < 1);
1594 num_devices--;
1595 }
1596 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1597
1598 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001599 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001600 goto out;
1601 }
1602
Stefan Behrens8dabb742012-11-06 13:15:27 +01001603 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001604 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001605 goto out;
1606 }
1607
David Woodhouse53b381b2013-01-29 18:40:14 -05001608 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1609 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001610 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001611 goto out;
1612 }
1613 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1614 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001615 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001616 goto out;
1617 }
1618
Chris Masondfe25022008-05-13 13:46:40 -04001619 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001620 struct list_head *devices;
1621 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001622
Chris Masondfe25022008-05-13 13:46:40 -04001623 device = NULL;
1624 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001625 /*
1626 * It is safe to read the devices since the volume_mutex
1627 * is held.
1628 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001629 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001630 if (tmp->in_fs_metadata &&
1631 !tmp->is_tgtdev_for_dev_replace &&
1632 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001633 device = tmp;
1634 break;
1635 }
1636 }
1637 bdev = NULL;
1638 bh = NULL;
1639 disk_super = NULL;
1640 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001641 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001642 goto out;
1643 }
Chris Masondfe25022008-05-13 13:46:40 -04001644 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001645 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001646 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001647 root->fs_info->bdev_holder, 0,
1648 &bdev, &bh);
1649 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001650 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001651 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001652 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001653 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001654 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001655 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001656 if (!device) {
1657 ret = -ENOENT;
1658 goto error_brelse;
1659 }
Chris Masondfe25022008-05-13 13:46:40 -04001660 }
Yan Zheng2b820322008-11-17 21:11:30 -05001661
Stefan Behrens63a212a2012-11-05 18:29:28 +01001662 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001663 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001664 goto error_brelse;
1665 }
1666
Yan Zheng2b820322008-11-17 21:11:30 -05001667 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001668 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001669 goto error_brelse;
1670 }
1671
1672 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001673 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001674 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001675 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001676 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001677 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001678 }
Chris Masona061fc82008-05-07 11:43:44 -04001679
Carey Underwoodd7901552013-03-04 16:37:06 -06001680 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001681 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001682 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001683 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001684 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001685
Stefan Behrens63a212a2012-11-05 18:29:28 +01001686 /*
1687 * TODO: the superblock still includes this device in its num_devices
1688 * counter although write_all_supers() is not locked out. This
1689 * could give a filesystem state which requires a degraded mount.
1690 */
Chris Masona061fc82008-05-07 11:43:44 -04001691 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1692 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001693 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001694
Josef Bacik2bf64752011-09-26 17:12:22 -04001695 spin_lock(&root->fs_info->free_chunk_lock);
1696 root->fs_info->free_chunk_space = device->total_bytes -
1697 device->bytes_used;
1698 spin_unlock(&root->fs_info->free_chunk_lock);
1699
Yan Zheng2b820322008-11-17 21:11:30 -05001700 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001701 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001702
1703 /*
1704 * the device list mutex makes sure that we don't change
1705 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001706 * the device supers. Whoever is writing all supers, should
1707 * lock the device list mutex before getting the number of
1708 * devices in the super block (super_copy). Conversely,
1709 * whoever updates the number of devices in the super block
1710 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001711 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001712
1713 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001714 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001715 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001716
Yan Zhenge4404d62008-12-12 10:03:26 -05001717 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001718 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001719
Chris Masoncd02dca2010-12-13 14:56:23 -05001720 if (device->missing)
1721 root->fs_info->fs_devices->missing_devices--;
1722
Yan Zheng2b820322008-11-17 21:11:30 -05001723 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1724 struct btrfs_device, dev_list);
1725 if (device->bdev == root->fs_info->sb->s_bdev)
1726 root->fs_info->sb->s_bdev = next_device->bdev;
1727 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1728 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1729
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001730 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001731 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001732 /* remove sysfs entry */
1733 btrfs_kobj_rm_device(root->fs_info, device);
1734 }
Anand Jain99994cd2014-06-03 11:36:00 +08001735
Xiao Guangrong1f781602011-04-20 10:09:16 +00001736 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001737
David Sterba6c417612011-04-13 15:41:04 +02001738 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1739 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001740 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001741
Xiao Guangrong1f781602011-04-20 10:09:16 +00001742 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001743 struct btrfs_fs_devices *fs_devices;
1744 fs_devices = root->fs_info->fs_devices;
1745 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001746 if (fs_devices->seed == cur_devices) {
1747 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001748 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001749 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001750 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001751 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001752 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001753 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001754 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001755 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001756 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001757 }
1758
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001759 root->fs_info->num_tolerated_disk_barrier_failures =
1760 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1761
Yan Zheng2b820322008-11-17 21:11:30 -05001762 /*
1763 * at this point, the device is zero sized. We want to
1764 * remove it from the devices list and zero out the old super
1765 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001766 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001767 u64 bytenr;
1768 int i;
1769
Chris Masondfe25022008-05-13 13:46:40 -04001770 /* make sure this device isn't detected as part of
1771 * the FS anymore
1772 */
1773 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1774 set_buffer_dirty(bh);
1775 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001776
1777 /* clear the mirror copies of super block on the disk
1778 * being removed, 0th copy is been taken care above and
1779 * the below would take of the rest
1780 */
1781 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1782 bytenr = btrfs_sb_offset(i);
1783 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1784 i_size_read(bdev->bd_inode))
1785 break;
1786
1787 brelse(bh);
1788 bh = __bread(bdev, bytenr / 4096,
1789 BTRFS_SUPER_INFO_SIZE);
1790 if (!bh)
1791 continue;
1792
1793 disk_super = (struct btrfs_super_block *)bh->b_data;
1794
1795 if (btrfs_super_bytenr(disk_super) != bytenr ||
1796 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1797 continue;
1798 }
1799 memset(&disk_super->magic, 0,
1800 sizeof(disk_super->magic));
1801 set_buffer_dirty(bh);
1802 sync_dirty_buffer(bh);
1803 }
Chris Masondfe25022008-05-13 13:46:40 -04001804 }
Chris Masona061fc82008-05-07 11:43:44 -04001805
Chris Masona061fc82008-05-07 11:43:44 -04001806 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001807
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001808 if (bdev) {
1809 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001810 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001811
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001812 /* Update ctime/mtime for device path for libblkid */
1813 update_dev_time(device_path);
1814 }
1815
Chris Masona061fc82008-05-07 11:43:44 -04001816error_brelse:
1817 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001818 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001819 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001820out:
1821 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001822 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001823error_undo:
1824 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001825 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001826 list_add(&device->dev_alloc_list,
1827 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001828 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001829 root->fs_info->fs_devices->rw_devices++;
1830 }
1831 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001832}
1833
Stefan Behrense93c89c2012-11-05 17:33:06 +01001834void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1835 struct btrfs_device *srcdev)
1836{
1837 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001838
Stefan Behrense93c89c2012-11-05 17:33:06 +01001839 list_del_rcu(&srcdev->dev_list);
1840 list_del_rcu(&srcdev->dev_alloc_list);
1841 fs_info->fs_devices->num_devices--;
1842 if (srcdev->missing) {
1843 fs_info->fs_devices->missing_devices--;
1844 fs_info->fs_devices->rw_devices++;
1845 }
1846 if (srcdev->can_discard)
1847 fs_info->fs_devices->num_can_discard--;
Ilya Dryomov13572722013-10-02 20:41:01 +03001848 if (srcdev->bdev) {
Stefan Behrense93c89c2012-11-05 17:33:06 +01001849 fs_info->fs_devices->open_devices--;
1850
Ilya Dryomov13572722013-10-02 20:41:01 +03001851 /* zero out the old super */
1852 btrfs_scratch_superblock(srcdev);
1853 }
1854
Stefan Behrense93c89c2012-11-05 17:33:06 +01001855 call_rcu(&srcdev->rcu, free_device);
1856}
1857
1858void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1859 struct btrfs_device *tgtdev)
1860{
1861 struct btrfs_device *next_device;
1862
1863 WARN_ON(!tgtdev);
1864 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1865 if (tgtdev->bdev) {
1866 btrfs_scratch_superblock(tgtdev);
1867 fs_info->fs_devices->open_devices--;
1868 }
1869 fs_info->fs_devices->num_devices--;
1870 if (tgtdev->can_discard)
1871 fs_info->fs_devices->num_can_discard++;
1872
1873 next_device = list_entry(fs_info->fs_devices->devices.next,
1874 struct btrfs_device, dev_list);
1875 if (tgtdev->bdev == fs_info->sb->s_bdev)
1876 fs_info->sb->s_bdev = next_device->bdev;
1877 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1878 fs_info->fs_devices->latest_bdev = next_device->bdev;
1879 list_del_rcu(&tgtdev->dev_list);
1880
1881 call_rcu(&tgtdev->rcu, free_device);
1882
1883 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1884}
1885
Eric Sandeen48a3b632013-04-25 20:41:01 +00001886static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1887 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001888{
1889 int ret = 0;
1890 struct btrfs_super_block *disk_super;
1891 u64 devid;
1892 u8 *dev_uuid;
1893 struct block_device *bdev;
1894 struct buffer_head *bh;
1895
1896 *device = NULL;
1897 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1898 root->fs_info->bdev_holder, 0, &bdev, &bh);
1899 if (ret)
1900 return ret;
1901 disk_super = (struct btrfs_super_block *)bh->b_data;
1902 devid = btrfs_stack_device_id(&disk_super->dev_item);
1903 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001904 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001905 disk_super->fsid);
1906 brelse(bh);
1907 if (!*device)
1908 ret = -ENOENT;
1909 blkdev_put(bdev, FMODE_READ);
1910 return ret;
1911}
1912
1913int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1914 char *device_path,
1915 struct btrfs_device **device)
1916{
1917 *device = NULL;
1918 if (strcmp(device_path, "missing") == 0) {
1919 struct list_head *devices;
1920 struct btrfs_device *tmp;
1921
1922 devices = &root->fs_info->fs_devices->devices;
1923 /*
1924 * It is safe to read the devices since the volume_mutex
1925 * is held by the caller.
1926 */
1927 list_for_each_entry(tmp, devices, dev_list) {
1928 if (tmp->in_fs_metadata && !tmp->bdev) {
1929 *device = tmp;
1930 break;
1931 }
1932 }
1933
1934 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001935 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001936 return -ENOENT;
1937 }
1938
1939 return 0;
1940 } else {
1941 return btrfs_find_device_by_path(root, device_path, device);
1942 }
1943}
1944
Yan Zheng2b820322008-11-17 21:11:30 -05001945/*
1946 * does all the dirty work required for changing file system's UUID.
1947 */
Li Zefan125ccb02011-12-08 15:07:24 +08001948static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001949{
1950 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1951 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001952 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001953 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001954 struct btrfs_device *device;
1955 u64 super_flags;
1956
1957 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001958 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001959 return -EINVAL;
1960
Ilya Dryomov2208a372013-08-12 14:33:03 +03001961 seed_devices = __alloc_fs_devices();
1962 if (IS_ERR(seed_devices))
1963 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001964
Yan Zhenge4404d62008-12-12 10:03:26 -05001965 old_devices = clone_fs_devices(fs_devices);
1966 if (IS_ERR(old_devices)) {
1967 kfree(seed_devices);
1968 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001969 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001970
Yan Zheng2b820322008-11-17 21:11:30 -05001971 list_add(&old_devices->list, &fs_uuids);
1972
Yan Zhenge4404d62008-12-12 10:03:26 -05001973 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1974 seed_devices->opened = 1;
1975 INIT_LIST_HEAD(&seed_devices->devices);
1976 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001977 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001978
1979 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001980 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1981 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001982
Yan Zhenge4404d62008-12-12 10:03:26 -05001983 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1984 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1985 device->fs_devices = seed_devices;
1986 }
1987
Yan Zheng2b820322008-11-17 21:11:30 -05001988 fs_devices->seeding = 0;
1989 fs_devices->num_devices = 0;
1990 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001991 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001992
1993 generate_random_uuid(fs_devices->fsid);
1994 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1995 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01001996 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1997
Yan Zheng2b820322008-11-17 21:11:30 -05001998 super_flags = btrfs_super_flags(disk_super) &
1999 ~BTRFS_SUPER_FLAG_SEEDING;
2000 btrfs_set_super_flags(disk_super, super_flags);
2001
2002 return 0;
2003}
2004
2005/*
2006 * strore the expected generation for seed devices in device items.
2007 */
2008static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2009 struct btrfs_root *root)
2010{
2011 struct btrfs_path *path;
2012 struct extent_buffer *leaf;
2013 struct btrfs_dev_item *dev_item;
2014 struct btrfs_device *device;
2015 struct btrfs_key key;
2016 u8 fs_uuid[BTRFS_UUID_SIZE];
2017 u8 dev_uuid[BTRFS_UUID_SIZE];
2018 u64 devid;
2019 int ret;
2020
2021 path = btrfs_alloc_path();
2022 if (!path)
2023 return -ENOMEM;
2024
2025 root = root->fs_info->chunk_root;
2026 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2027 key.offset = 0;
2028 key.type = BTRFS_DEV_ITEM_KEY;
2029
2030 while (1) {
2031 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2032 if (ret < 0)
2033 goto error;
2034
2035 leaf = path->nodes[0];
2036next_slot:
2037 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2038 ret = btrfs_next_leaf(root, path);
2039 if (ret > 0)
2040 break;
2041 if (ret < 0)
2042 goto error;
2043 leaf = path->nodes[0];
2044 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002045 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002046 continue;
2047 }
2048
2049 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2050 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2051 key.type != BTRFS_DEV_ITEM_KEY)
2052 break;
2053
2054 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2055 struct btrfs_dev_item);
2056 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002057 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002058 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002059 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002060 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002061 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2062 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002063 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002064
2065 if (device->fs_devices->seeding) {
2066 btrfs_set_device_generation(leaf, dev_item,
2067 device->generation);
2068 btrfs_mark_buffer_dirty(leaf);
2069 }
2070
2071 path->slots[0]++;
2072 goto next_slot;
2073 }
2074 ret = 0;
2075error:
2076 btrfs_free_path(path);
2077 return ret;
2078}
2079
Chris Mason788f20e2008-04-28 15:29:42 -04002080int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2081{
Josef Bacikd5e20032011-08-04 14:52:27 +00002082 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002083 struct btrfs_trans_handle *trans;
2084 struct btrfs_device *device;
2085 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002086 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002087 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002088 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04002089 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002090 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002091 int ret = 0;
2092
Yan Zheng2b820322008-11-17 21:11:30 -05002093 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002094 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002095
Li Zefana5d16332011-12-07 20:08:40 -05002096 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002097 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002098 if (IS_ERR(bdev))
2099 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002100
Yan Zheng2b820322008-11-17 21:11:30 -05002101 if (root->fs_info->fs_devices->seeding) {
2102 seeding_dev = 1;
2103 down_write(&sb->s_umount);
2104 mutex_lock(&uuid_mutex);
2105 }
2106
Chris Mason8c8bee12008-09-29 11:19:10 -04002107 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002108
Chris Mason788f20e2008-04-28 15:29:42 -04002109 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002110
2111 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002112 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002113 if (device->bdev == bdev) {
2114 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002115 mutex_unlock(
2116 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002117 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002118 }
2119 }
Liu Bod25628b2012-11-14 14:35:30 +00002120 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002121
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002122 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2123 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002124 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002125 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002126 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002127 }
2128
Josef Bacik606686e2012-06-04 14:03:51 -04002129 name = rcu_string_strdup(device_path, GFP_NOFS);
2130 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002131 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002132 ret = -ENOMEM;
2133 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002134 }
Josef Bacik606686e2012-06-04 14:03:51 -04002135 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002136
Yan, Zhenga22285a2010-05-16 10:48:46 -04002137 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002138 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002139 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002140 kfree(device);
2141 ret = PTR_ERR(trans);
2142 goto error;
2143 }
2144
Yan Zheng2b820322008-11-17 21:11:30 -05002145 lock_chunks(root);
2146
Josef Bacikd5e20032011-08-04 14:52:27 +00002147 q = bdev_get_queue(bdev);
2148 if (blk_queue_discard(q))
2149 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002150 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002151 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002152 device->io_width = root->sectorsize;
2153 device->io_align = root->sectorsize;
2154 device->sector_size = root->sectorsize;
2155 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002156 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002157 device->dev_root = root->fs_info->dev_root;
2158 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002159 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002160 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002161 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002162 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002163 set_blocksize(device->bdev, 4096);
2164
Yan Zheng2b820322008-11-17 21:11:30 -05002165 if (seeding_dev) {
2166 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002167 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002168 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002169 }
2170
2171 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002172
Chris Masone5e9a522009-06-10 15:17:02 -04002173 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002174 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002175 list_add(&device->dev_alloc_list,
2176 &root->fs_info->fs_devices->alloc_list);
2177 root->fs_info->fs_devices->num_devices++;
2178 root->fs_info->fs_devices->open_devices++;
2179 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002180 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002181 if (device->can_discard)
2182 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002183 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2184
Josef Bacik2bf64752011-09-26 17:12:22 -04002185 spin_lock(&root->fs_info->free_chunk_lock);
2186 root->fs_info->free_chunk_space += device->total_bytes;
2187 spin_unlock(&root->fs_info->free_chunk_lock);
2188
Chris Masonc2898112009-06-10 09:51:32 -04002189 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2190 root->fs_info->fs_devices->rotating = 1;
2191
David Sterba6c417612011-04-13 15:41:04 +02002192 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2193 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002194 total_bytes + device->total_bytes);
2195
David Sterba6c417612011-04-13 15:41:04 +02002196 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2197 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002198 total_bytes + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002199
2200 /* add sysfs device entry */
2201 btrfs_kobj_add_device(root->fs_info, device);
2202
Chris Masone5e9a522009-06-10 15:17:02 -04002203 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002204
Yan Zheng2b820322008-11-17 21:11:30 -05002205 if (seeding_dev) {
Anand Jainb2373f22014-06-03 11:36:03 +08002206 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002207 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002208 if (ret) {
2209 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002210 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002211 }
Yan Zheng2b820322008-11-17 21:11:30 -05002212 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002213 if (ret) {
2214 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002215 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002216 }
Anand Jainb2373f22014-06-03 11:36:03 +08002217
2218 /* Sprouting would change fsid of the mounted root,
2219 * so rename the fsid on the sysfs
2220 */
2221 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2222 root->fs_info->fsid);
2223 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2224 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002225 } else {
2226 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002227 if (ret) {
2228 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002229 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002230 }
Yan Zheng2b820322008-11-17 21:11:30 -05002231 }
2232
Chris Mason913d9522009-03-10 13:17:18 -04002233 /*
2234 * we've got more storage, clear any full flags on the space
2235 * infos
2236 */
2237 btrfs_clear_space_info_full(root->fs_info);
2238
Chris Mason7d9eb122008-07-08 14:19:17 -04002239 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002240 root->fs_info->num_tolerated_disk_barrier_failures =
2241 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002242 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002243
2244 if (seeding_dev) {
2245 mutex_unlock(&uuid_mutex);
2246 up_write(&sb->s_umount);
2247
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002248 if (ret) /* transaction commit */
2249 return ret;
2250
Yan Zheng2b820322008-11-17 21:11:30 -05002251 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002252 if (ret < 0)
2253 btrfs_error(root->fs_info, ret,
2254 "Failed to relocate sys chunks after "
2255 "device initialization. This can be fixed "
2256 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002257 trans = btrfs_attach_transaction(root);
2258 if (IS_ERR(trans)) {
2259 if (PTR_ERR(trans) == -ENOENT)
2260 return 0;
2261 return PTR_ERR(trans);
2262 }
2263 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002264 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002265
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002266 /* Update ctime/mtime for libblkid */
2267 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002268 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002269
2270error_trans:
2271 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002272 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002273 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002274 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002275 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002276error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002277 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002278 if (seeding_dev) {
2279 mutex_unlock(&uuid_mutex);
2280 up_write(&sb->s_umount);
2281 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002282 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002283}
2284
Stefan Behrense93c89c2012-11-05 17:33:06 +01002285int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2286 struct btrfs_device **device_out)
2287{
2288 struct request_queue *q;
2289 struct btrfs_device *device;
2290 struct block_device *bdev;
2291 struct btrfs_fs_info *fs_info = root->fs_info;
2292 struct list_head *devices;
2293 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002294 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002295 int ret = 0;
2296
2297 *device_out = NULL;
2298 if (fs_info->fs_devices->seeding)
2299 return -EINVAL;
2300
2301 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2302 fs_info->bdev_holder);
2303 if (IS_ERR(bdev))
2304 return PTR_ERR(bdev);
2305
2306 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2307
2308 devices = &fs_info->fs_devices->devices;
2309 list_for_each_entry(device, devices, dev_list) {
2310 if (device->bdev == bdev) {
2311 ret = -EEXIST;
2312 goto error;
2313 }
2314 }
2315
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002316 device = btrfs_alloc_device(NULL, &devid, NULL);
2317 if (IS_ERR(device)) {
2318 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002319 goto error;
2320 }
2321
2322 name = rcu_string_strdup(device_path, GFP_NOFS);
2323 if (!name) {
2324 kfree(device);
2325 ret = -ENOMEM;
2326 goto error;
2327 }
2328 rcu_assign_pointer(device->name, name);
2329
2330 q = bdev_get_queue(bdev);
2331 if (blk_queue_discard(q))
2332 device->can_discard = 1;
2333 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2334 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002335 device->generation = 0;
2336 device->io_width = root->sectorsize;
2337 device->io_align = root->sectorsize;
2338 device->sector_size = root->sectorsize;
2339 device->total_bytes = i_size_read(bdev->bd_inode);
2340 device->disk_total_bytes = device->total_bytes;
2341 device->dev_root = fs_info->dev_root;
2342 device->bdev = bdev;
2343 device->in_fs_metadata = 1;
2344 device->is_tgtdev_for_dev_replace = 1;
2345 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002346 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002347 set_blocksize(device->bdev, 4096);
2348 device->fs_devices = fs_info->fs_devices;
2349 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2350 fs_info->fs_devices->num_devices++;
2351 fs_info->fs_devices->open_devices++;
2352 if (device->can_discard)
2353 fs_info->fs_devices->num_can_discard++;
2354 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2355
2356 *device_out = device;
2357 return ret;
2358
2359error:
2360 blkdev_put(bdev, FMODE_EXCL);
2361 return ret;
2362}
2363
2364void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2365 struct btrfs_device *tgtdev)
2366{
2367 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2368 tgtdev->io_width = fs_info->dev_root->sectorsize;
2369 tgtdev->io_align = fs_info->dev_root->sectorsize;
2370 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2371 tgtdev->dev_root = fs_info->dev_root;
2372 tgtdev->in_fs_metadata = 1;
2373}
2374
Chris Masond3977122009-01-05 21:25:51 -05002375static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2376 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002377{
2378 int ret;
2379 struct btrfs_path *path;
2380 struct btrfs_root *root;
2381 struct btrfs_dev_item *dev_item;
2382 struct extent_buffer *leaf;
2383 struct btrfs_key key;
2384
2385 root = device->dev_root->fs_info->chunk_root;
2386
2387 path = btrfs_alloc_path();
2388 if (!path)
2389 return -ENOMEM;
2390
2391 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2392 key.type = BTRFS_DEV_ITEM_KEY;
2393 key.offset = device->devid;
2394
2395 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2396 if (ret < 0)
2397 goto out;
2398
2399 if (ret > 0) {
2400 ret = -ENOENT;
2401 goto out;
2402 }
2403
2404 leaf = path->nodes[0];
2405 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2406
2407 btrfs_set_device_id(leaf, dev_item, device->devid);
2408 btrfs_set_device_type(leaf, dev_item, device->type);
2409 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2410 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2411 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002412 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002413 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2414 btrfs_mark_buffer_dirty(leaf);
2415
2416out:
2417 btrfs_free_path(path);
2418 return ret;
2419}
2420
Chris Mason7d9eb122008-07-08 14:19:17 -04002421static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002422 struct btrfs_device *device, u64 new_size)
2423{
2424 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002425 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002426 u64 old_total = btrfs_super_total_bytes(super_copy);
2427 u64 diff = new_size - device->total_bytes;
2428
Yan Zheng2b820322008-11-17 21:11:30 -05002429 if (!device->writeable)
2430 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002431 if (new_size <= device->total_bytes ||
2432 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002433 return -EINVAL;
2434
Chris Mason8f18cf12008-04-25 16:53:30 -04002435 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002436 device->fs_devices->total_rw_bytes += diff;
2437
2438 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002439 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002440 btrfs_clear_space_info_full(device->dev_root->fs_info);
2441
Chris Mason8f18cf12008-04-25 16:53:30 -04002442 return btrfs_update_device(trans, device);
2443}
2444
Chris Mason7d9eb122008-07-08 14:19:17 -04002445int btrfs_grow_device(struct btrfs_trans_handle *trans,
2446 struct btrfs_device *device, u64 new_size)
2447{
2448 int ret;
2449 lock_chunks(device->dev_root);
2450 ret = __btrfs_grow_device(trans, device, new_size);
2451 unlock_chunks(device->dev_root);
2452 return ret;
2453}
2454
Chris Mason8f18cf12008-04-25 16:53:30 -04002455static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2456 struct btrfs_root *root,
2457 u64 chunk_tree, u64 chunk_objectid,
2458 u64 chunk_offset)
2459{
2460 int ret;
2461 struct btrfs_path *path;
2462 struct btrfs_key key;
2463
2464 root = root->fs_info->chunk_root;
2465 path = btrfs_alloc_path();
2466 if (!path)
2467 return -ENOMEM;
2468
2469 key.objectid = chunk_objectid;
2470 key.offset = chunk_offset;
2471 key.type = BTRFS_CHUNK_ITEM_KEY;
2472
2473 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002474 if (ret < 0)
2475 goto out;
2476 else if (ret > 0) { /* Logic error or corruption */
2477 btrfs_error(root->fs_info, -ENOENT,
2478 "Failed lookup while freeing chunk.");
2479 ret = -ENOENT;
2480 goto out;
2481 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002482
2483 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002484 if (ret < 0)
2485 btrfs_error(root->fs_info, ret,
2486 "Failed to delete chunk item.");
2487out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002488 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002489 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002490}
2491
Christoph Hellwigb2950862008-12-02 09:54:17 -05002492static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002493 chunk_offset)
2494{
David Sterba6c417612011-04-13 15:41:04 +02002495 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002496 struct btrfs_disk_key *disk_key;
2497 struct btrfs_chunk *chunk;
2498 u8 *ptr;
2499 int ret = 0;
2500 u32 num_stripes;
2501 u32 array_size;
2502 u32 len = 0;
2503 u32 cur;
2504 struct btrfs_key key;
2505
2506 array_size = btrfs_super_sys_array_size(super_copy);
2507
2508 ptr = super_copy->sys_chunk_array;
2509 cur = 0;
2510
2511 while (cur < array_size) {
2512 disk_key = (struct btrfs_disk_key *)ptr;
2513 btrfs_disk_key_to_cpu(&key, disk_key);
2514
2515 len = sizeof(*disk_key);
2516
2517 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2518 chunk = (struct btrfs_chunk *)(ptr + len);
2519 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2520 len += btrfs_chunk_item_size(num_stripes);
2521 } else {
2522 ret = -EIO;
2523 break;
2524 }
2525 if (key.objectid == chunk_objectid &&
2526 key.offset == chunk_offset) {
2527 memmove(ptr, ptr + len, array_size - (cur + len));
2528 array_size -= len;
2529 btrfs_set_super_sys_array_size(super_copy, array_size);
2530 } else {
2531 ptr += len;
2532 cur += len;
2533 }
2534 }
2535 return ret;
2536}
2537
Christoph Hellwigb2950862008-12-02 09:54:17 -05002538static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002539 u64 chunk_tree, u64 chunk_objectid,
2540 u64 chunk_offset)
2541{
2542 struct extent_map_tree *em_tree;
2543 struct btrfs_root *extent_root;
2544 struct btrfs_trans_handle *trans;
2545 struct extent_map *em;
2546 struct map_lookup *map;
2547 int ret;
2548 int i;
2549
2550 root = root->fs_info->chunk_root;
2551 extent_root = root->fs_info->extent_root;
2552 em_tree = &root->fs_info->mapping_tree.map_tree;
2553
Josef Bacikba1bf482009-09-11 16:11:19 -04002554 ret = btrfs_can_relocate(extent_root, chunk_offset);
2555 if (ret)
2556 return -ENOSPC;
2557
Chris Mason8f18cf12008-04-25 16:53:30 -04002558 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002559 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002560 if (ret)
2561 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002562
Yan, Zhenga22285a2010-05-16 10:48:46 -04002563 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002564 if (IS_ERR(trans)) {
2565 ret = PTR_ERR(trans);
2566 btrfs_std_error(root->fs_info, ret);
2567 return ret;
2568 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002569
Chris Mason7d9eb122008-07-08 14:19:17 -04002570 lock_chunks(root);
2571
Chris Mason8f18cf12008-04-25 16:53:30 -04002572 /*
2573 * step two, delete the device extents and the
2574 * chunk tree entries
2575 */
Chris Mason890871b2009-09-02 16:24:52 -04002576 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002577 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002578 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002579
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002580 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002581 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002582 map = (struct map_lookup *)em->bdev;
2583
2584 for (i = 0; i < map->num_stripes; i++) {
2585 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2586 map->stripes[i].physical);
2587 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002588
Chris Masondfe25022008-05-13 13:46:40 -04002589 if (map->stripes[i].dev) {
2590 ret = btrfs_update_device(trans, map->stripes[i].dev);
2591 BUG_ON(ret);
2592 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002593 }
2594 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2595 chunk_offset);
2596
2597 BUG_ON(ret);
2598
liubo1abe9b82011-03-24 11:18:59 +00002599 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2600
Chris Mason8f18cf12008-04-25 16:53:30 -04002601 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2602 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2603 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002604 }
2605
Zheng Yan1a40e232008-09-26 10:09:34 -04002606 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2607 BUG_ON(ret);
2608
Chris Mason890871b2009-09-02 16:24:52 -04002609 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002610 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002611 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002612
Chris Mason8f18cf12008-04-25 16:53:30 -04002613 /* once for the tree */
2614 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002615 /* once for us */
2616 free_extent_map(em);
2617
Chris Mason7d9eb122008-07-08 14:19:17 -04002618 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002619 btrfs_end_transaction(trans, root);
2620 return 0;
2621}
2622
Yan Zheng2b820322008-11-17 21:11:30 -05002623static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2624{
2625 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2626 struct btrfs_path *path;
2627 struct extent_buffer *leaf;
2628 struct btrfs_chunk *chunk;
2629 struct btrfs_key key;
2630 struct btrfs_key found_key;
2631 u64 chunk_tree = chunk_root->root_key.objectid;
2632 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002633 bool retried = false;
2634 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002635 int ret;
2636
2637 path = btrfs_alloc_path();
2638 if (!path)
2639 return -ENOMEM;
2640
Josef Bacikba1bf482009-09-11 16:11:19 -04002641again:
Yan Zheng2b820322008-11-17 21:11:30 -05002642 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2643 key.offset = (u64)-1;
2644 key.type = BTRFS_CHUNK_ITEM_KEY;
2645
2646 while (1) {
2647 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2648 if (ret < 0)
2649 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002650 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002651
2652 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2653 key.type);
2654 if (ret < 0)
2655 goto error;
2656 if (ret > 0)
2657 break;
2658
2659 leaf = path->nodes[0];
2660 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2661
2662 chunk = btrfs_item_ptr(leaf, path->slots[0],
2663 struct btrfs_chunk);
2664 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002665 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002666
2667 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2668 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2669 found_key.objectid,
2670 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002671 if (ret == -ENOSPC)
2672 failed++;
2673 else if (ret)
2674 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002675 }
2676
2677 if (found_key.offset == 0)
2678 break;
2679 key.offset = found_key.offset - 1;
2680 }
2681 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002682 if (failed && !retried) {
2683 failed = 0;
2684 retried = true;
2685 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302686 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002687 ret = -ENOSPC;
2688 }
Yan Zheng2b820322008-11-17 21:11:30 -05002689error:
2690 btrfs_free_path(path);
2691 return ret;
2692}
2693
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002694static int insert_balance_item(struct btrfs_root *root,
2695 struct btrfs_balance_control *bctl)
2696{
2697 struct btrfs_trans_handle *trans;
2698 struct btrfs_balance_item *item;
2699 struct btrfs_disk_balance_args disk_bargs;
2700 struct btrfs_path *path;
2701 struct extent_buffer *leaf;
2702 struct btrfs_key key;
2703 int ret, err;
2704
2705 path = btrfs_alloc_path();
2706 if (!path)
2707 return -ENOMEM;
2708
2709 trans = btrfs_start_transaction(root, 0);
2710 if (IS_ERR(trans)) {
2711 btrfs_free_path(path);
2712 return PTR_ERR(trans);
2713 }
2714
2715 key.objectid = BTRFS_BALANCE_OBJECTID;
2716 key.type = BTRFS_BALANCE_ITEM_KEY;
2717 key.offset = 0;
2718
2719 ret = btrfs_insert_empty_item(trans, root, path, &key,
2720 sizeof(*item));
2721 if (ret)
2722 goto out;
2723
2724 leaf = path->nodes[0];
2725 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2726
2727 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2728
2729 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2730 btrfs_set_balance_data(leaf, item, &disk_bargs);
2731 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2732 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2733 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2734 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2735
2736 btrfs_set_balance_flags(leaf, item, bctl->flags);
2737
2738 btrfs_mark_buffer_dirty(leaf);
2739out:
2740 btrfs_free_path(path);
2741 err = btrfs_commit_transaction(trans, root);
2742 if (err && !ret)
2743 ret = err;
2744 return ret;
2745}
2746
2747static int del_balance_item(struct btrfs_root *root)
2748{
2749 struct btrfs_trans_handle *trans;
2750 struct btrfs_path *path;
2751 struct btrfs_key key;
2752 int ret, err;
2753
2754 path = btrfs_alloc_path();
2755 if (!path)
2756 return -ENOMEM;
2757
2758 trans = btrfs_start_transaction(root, 0);
2759 if (IS_ERR(trans)) {
2760 btrfs_free_path(path);
2761 return PTR_ERR(trans);
2762 }
2763
2764 key.objectid = BTRFS_BALANCE_OBJECTID;
2765 key.type = BTRFS_BALANCE_ITEM_KEY;
2766 key.offset = 0;
2767
2768 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2769 if (ret < 0)
2770 goto out;
2771 if (ret > 0) {
2772 ret = -ENOENT;
2773 goto out;
2774 }
2775
2776 ret = btrfs_del_item(trans, root, path);
2777out:
2778 btrfs_free_path(path);
2779 err = btrfs_commit_transaction(trans, root);
2780 if (err && !ret)
2781 ret = err;
2782 return ret;
2783}
2784
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002785/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002786 * This is a heuristic used to reduce the number of chunks balanced on
2787 * resume after balance was interrupted.
2788 */
2789static void update_balance_args(struct btrfs_balance_control *bctl)
2790{
2791 /*
2792 * Turn on soft mode for chunk types that were being converted.
2793 */
2794 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2795 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2796 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2797 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2798 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2799 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2800
2801 /*
2802 * Turn on usage filter if is not already used. The idea is
2803 * that chunks that we have already balanced should be
2804 * reasonably full. Don't do it for chunks that are being
2805 * converted - that will keep us from relocating unconverted
2806 * (albeit full) chunks.
2807 */
2808 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2809 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2810 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2811 bctl->data.usage = 90;
2812 }
2813 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2814 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2815 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2816 bctl->sys.usage = 90;
2817 }
2818 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2819 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2820 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2821 bctl->meta.usage = 90;
2822 }
2823}
2824
2825/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002826 * Should be called with both balance and volume mutexes held to
2827 * serialize other volume operations (add_dev/rm_dev/resize) with
2828 * restriper. Same goes for unset_balance_control.
2829 */
2830static void set_balance_control(struct btrfs_balance_control *bctl)
2831{
2832 struct btrfs_fs_info *fs_info = bctl->fs_info;
2833
2834 BUG_ON(fs_info->balance_ctl);
2835
2836 spin_lock(&fs_info->balance_lock);
2837 fs_info->balance_ctl = bctl;
2838 spin_unlock(&fs_info->balance_lock);
2839}
2840
2841static void unset_balance_control(struct btrfs_fs_info *fs_info)
2842{
2843 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2844
2845 BUG_ON(!fs_info->balance_ctl);
2846
2847 spin_lock(&fs_info->balance_lock);
2848 fs_info->balance_ctl = NULL;
2849 spin_unlock(&fs_info->balance_lock);
2850
2851 kfree(bctl);
2852}
2853
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002854/*
2855 * Balance filters. Return 1 if chunk should be filtered out
2856 * (should not be balanced).
2857 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002858static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002859 struct btrfs_balance_args *bargs)
2860{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002861 chunk_type = chunk_to_extended(chunk_type) &
2862 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002863
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002864 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002865 return 0;
2866
2867 return 1;
2868}
2869
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002870static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2871 struct btrfs_balance_args *bargs)
2872{
2873 struct btrfs_block_group_cache *cache;
2874 u64 chunk_used, user_thresh;
2875 int ret = 1;
2876
2877 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2878 chunk_used = btrfs_block_group_used(&cache->item);
2879
Ilya Dryomova105bb82013-01-21 15:15:56 +02002880 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002881 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002882 else if (bargs->usage > 100)
2883 user_thresh = cache->key.offset;
2884 else
2885 user_thresh = div_factor_fine(cache->key.offset,
2886 bargs->usage);
2887
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002888 if (chunk_used < user_thresh)
2889 ret = 0;
2890
2891 btrfs_put_block_group(cache);
2892 return ret;
2893}
2894
Ilya Dryomov409d4042012-01-16 22:04:47 +02002895static int chunk_devid_filter(struct extent_buffer *leaf,
2896 struct btrfs_chunk *chunk,
2897 struct btrfs_balance_args *bargs)
2898{
2899 struct btrfs_stripe *stripe;
2900 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2901 int i;
2902
2903 for (i = 0; i < num_stripes; i++) {
2904 stripe = btrfs_stripe_nr(chunk, i);
2905 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2906 return 0;
2907 }
2908
2909 return 1;
2910}
2911
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002912/* [pstart, pend) */
2913static int chunk_drange_filter(struct extent_buffer *leaf,
2914 struct btrfs_chunk *chunk,
2915 u64 chunk_offset,
2916 struct btrfs_balance_args *bargs)
2917{
2918 struct btrfs_stripe *stripe;
2919 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2920 u64 stripe_offset;
2921 u64 stripe_length;
2922 int factor;
2923 int i;
2924
2925 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2926 return 0;
2927
2928 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002929 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2930 factor = num_stripes / 2;
2931 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2932 factor = num_stripes - 1;
2933 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2934 factor = num_stripes - 2;
2935 } else {
2936 factor = num_stripes;
2937 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002938
2939 for (i = 0; i < num_stripes; i++) {
2940 stripe = btrfs_stripe_nr(chunk, i);
2941 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2942 continue;
2943
2944 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2945 stripe_length = btrfs_chunk_length(leaf, chunk);
2946 do_div(stripe_length, factor);
2947
2948 if (stripe_offset < bargs->pend &&
2949 stripe_offset + stripe_length > bargs->pstart)
2950 return 0;
2951 }
2952
2953 return 1;
2954}
2955
Ilya Dryomovea671762012-01-16 22:04:48 +02002956/* [vstart, vend) */
2957static int chunk_vrange_filter(struct extent_buffer *leaf,
2958 struct btrfs_chunk *chunk,
2959 u64 chunk_offset,
2960 struct btrfs_balance_args *bargs)
2961{
2962 if (chunk_offset < bargs->vend &&
2963 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2964 /* at least part of the chunk is inside this vrange */
2965 return 0;
2966
2967 return 1;
2968}
2969
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002970static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002971 struct btrfs_balance_args *bargs)
2972{
2973 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2974 return 0;
2975
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002976 chunk_type = chunk_to_extended(chunk_type) &
2977 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002978
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002979 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002980 return 1;
2981
2982 return 0;
2983}
2984
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002985static int should_balance_chunk(struct btrfs_root *root,
2986 struct extent_buffer *leaf,
2987 struct btrfs_chunk *chunk, u64 chunk_offset)
2988{
2989 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2990 struct btrfs_balance_args *bargs = NULL;
2991 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2992
2993 /* type filter */
2994 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2995 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2996 return 0;
2997 }
2998
2999 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3000 bargs = &bctl->data;
3001 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3002 bargs = &bctl->sys;
3003 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3004 bargs = &bctl->meta;
3005
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003006 /* profiles filter */
3007 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3008 chunk_profiles_filter(chunk_type, bargs)) {
3009 return 0;
3010 }
3011
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003012 /* usage filter */
3013 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3014 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3015 return 0;
3016 }
3017
Ilya Dryomov409d4042012-01-16 22:04:47 +02003018 /* devid filter */
3019 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3020 chunk_devid_filter(leaf, chunk, bargs)) {
3021 return 0;
3022 }
3023
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003024 /* drange filter, makes sense only with devid filter */
3025 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3026 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3027 return 0;
3028 }
3029
Ilya Dryomovea671762012-01-16 22:04:48 +02003030 /* vrange filter */
3031 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3032 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3033 return 0;
3034 }
3035
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003036 /* soft profile changing mode */
3037 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3038 chunk_soft_convert_filter(chunk_type, bargs)) {
3039 return 0;
3040 }
3041
David Sterba7d824b62014-05-07 17:37:51 +02003042 /*
3043 * limited by count, must be the last filter
3044 */
3045 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3046 if (bargs->limit == 0)
3047 return 0;
3048 else
3049 bargs->limit--;
3050 }
3051
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003052 return 1;
3053}
3054
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003055static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003056{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003057 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003058 struct btrfs_root *chunk_root = fs_info->chunk_root;
3059 struct btrfs_root *dev_root = fs_info->dev_root;
3060 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003061 struct btrfs_device *device;
3062 u64 old_size;
3063 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003064 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003065 struct btrfs_path *path;
3066 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003067 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003068 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003069 struct extent_buffer *leaf;
3070 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003071 int ret;
3072 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003073 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003074 u64 limit_data = bctl->data.limit;
3075 u64 limit_meta = bctl->meta.limit;
3076 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003077
Chris Masonec44a352008-04-28 15:29:52 -04003078 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003079 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003080 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04003081 old_size = device->total_bytes;
3082 size_to_free = div_factor(old_size, 1);
3083 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003084 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003085 device->total_bytes - device->bytes_used > size_to_free ||
3086 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003087 continue;
3088
3089 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003090 if (ret == -ENOSPC)
3091 break;
Chris Masonec44a352008-04-28 15:29:52 -04003092 BUG_ON(ret);
3093
Yan, Zhenga22285a2010-05-16 10:48:46 -04003094 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003095 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003096
3097 ret = btrfs_grow_device(trans, device, old_size);
3098 BUG_ON(ret);
3099
3100 btrfs_end_transaction(trans, dev_root);
3101 }
3102
3103 /* step two, relocate all the chunks */
3104 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003105 if (!path) {
3106 ret = -ENOMEM;
3107 goto error;
3108 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003109
3110 /* zero out stat counters */
3111 spin_lock(&fs_info->balance_lock);
3112 memset(&bctl->stat, 0, sizeof(bctl->stat));
3113 spin_unlock(&fs_info->balance_lock);
3114again:
David Sterba7d824b62014-05-07 17:37:51 +02003115 if (!counting) {
3116 bctl->data.limit = limit_data;
3117 bctl->meta.limit = limit_meta;
3118 bctl->sys.limit = limit_sys;
3119 }
Chris Masonec44a352008-04-28 15:29:52 -04003120 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3121 key.offset = (u64)-1;
3122 key.type = BTRFS_CHUNK_ITEM_KEY;
3123
Chris Masond3977122009-01-05 21:25:51 -05003124 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003125 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003126 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003127 ret = -ECANCELED;
3128 goto error;
3129 }
3130
Chris Masonec44a352008-04-28 15:29:52 -04003131 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3132 if (ret < 0)
3133 goto error;
3134
3135 /*
3136 * this shouldn't happen, it means the last relocate
3137 * failed
3138 */
3139 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003140 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003141
3142 ret = btrfs_previous_item(chunk_root, path, 0,
3143 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003144 if (ret) {
3145 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003146 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003147 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003148
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003149 leaf = path->nodes[0];
3150 slot = path->slots[0];
3151 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3152
Chris Masonec44a352008-04-28 15:29:52 -04003153 if (found_key.objectid != key.objectid)
3154 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003155
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003156 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3157
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003158 if (!counting) {
3159 spin_lock(&fs_info->balance_lock);
3160 bctl->stat.considered++;
3161 spin_unlock(&fs_info->balance_lock);
3162 }
3163
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003164 ret = should_balance_chunk(chunk_root, leaf, chunk,
3165 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003166 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003167 if (!ret)
3168 goto loop;
3169
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003170 if (counting) {
3171 spin_lock(&fs_info->balance_lock);
3172 bctl->stat.expected++;
3173 spin_unlock(&fs_info->balance_lock);
3174 goto loop;
3175 }
3176
Chris Masonec44a352008-04-28 15:29:52 -04003177 ret = btrfs_relocate_chunk(chunk_root,
3178 chunk_root->root_key.objectid,
3179 found_key.objectid,
3180 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003181 if (ret && ret != -ENOSPC)
3182 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003183 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003184 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003185 } else {
3186 spin_lock(&fs_info->balance_lock);
3187 bctl->stat.completed++;
3188 spin_unlock(&fs_info->balance_lock);
3189 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003190loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003191 if (found_key.offset == 0)
3192 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003193 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003194 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003195
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003196 if (counting) {
3197 btrfs_release_path(path);
3198 counting = false;
3199 goto again;
3200 }
Chris Masonec44a352008-04-28 15:29:52 -04003201error:
3202 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003203 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003204 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003205 enospc_errors);
3206 if (!ret)
3207 ret = -ENOSPC;
3208 }
3209
Chris Masonec44a352008-04-28 15:29:52 -04003210 return ret;
3211}
3212
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003213/**
3214 * alloc_profile_is_valid - see if a given profile is valid and reduced
3215 * @flags: profile to validate
3216 * @extended: if true @flags is treated as an extended profile
3217 */
3218static int alloc_profile_is_valid(u64 flags, int extended)
3219{
3220 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3221 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3222
3223 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3224
3225 /* 1) check that all other bits are zeroed */
3226 if (flags & ~mask)
3227 return 0;
3228
3229 /* 2) see if profile is reduced */
3230 if (flags == 0)
3231 return !extended; /* "0" is valid for usual profiles */
3232
3233 /* true if exactly one bit set */
3234 return (flags & (flags - 1)) == 0;
3235}
3236
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003237static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3238{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003239 /* cancel requested || normal exit path */
3240 return atomic_read(&fs_info->balance_cancel_req) ||
3241 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3242 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003243}
3244
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003245static void __cancel_balance(struct btrfs_fs_info *fs_info)
3246{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003247 int ret;
3248
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003249 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003250 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003251 if (ret)
3252 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003253
3254 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003255}
3256
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003257/*
3258 * Should be called with both balance and volume mutexes held
3259 */
3260int btrfs_balance(struct btrfs_balance_control *bctl,
3261 struct btrfs_ioctl_balance_args *bargs)
3262{
3263 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003264 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003265 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003266 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003267 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003268 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003269
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003270 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003271 atomic_read(&fs_info->balance_pause_req) ||
3272 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003273 ret = -EINVAL;
3274 goto out;
3275 }
3276
Ilya Dryomove4837f82012-03-27 17:09:17 +03003277 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3278 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3279 mixed = 1;
3280
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003281 /*
3282 * In case of mixed groups both data and meta should be picked,
3283 * and identical options should be given for both of them.
3284 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003285 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3286 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003287 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3288 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3289 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003290 btrfs_err(fs_info, "with mixed groups data and "
3291 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003292 ret = -EINVAL;
3293 goto out;
3294 }
3295 }
3296
Stefan Behrens8dabb742012-11-06 13:15:27 +01003297 num_devices = fs_info->fs_devices->num_devices;
3298 btrfs_dev_replace_lock(&fs_info->dev_replace);
3299 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3300 BUG_ON(num_devices < 1);
3301 num_devices--;
3302 }
3303 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003304 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003305 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003306 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003307 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003308 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003309 if (num_devices > 2)
3310 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3311 if (num_devices > 3)
3312 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3313 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003314 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3315 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3316 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003317 btrfs_err(fs_info, "unable to start balance with target "
3318 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003319 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003320 ret = -EINVAL;
3321 goto out;
3322 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003323 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3324 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3325 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003326 btrfs_err(fs_info,
3327 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003328 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003329 ret = -EINVAL;
3330 goto out;
3331 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003332 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3333 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3334 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003335 btrfs_err(fs_info,
3336 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003337 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003338 ret = -EINVAL;
3339 goto out;
3340 }
3341
Ilya Dryomove4837f82012-03-27 17:09:17 +03003342 /* allow dup'ed data chunks only in mixed mode */
3343 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003344 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003345 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003346 ret = -EINVAL;
3347 goto out;
3348 }
3349
3350 /* allow to reduce meta or sys integrity only if force set */
3351 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003352 BTRFS_BLOCK_GROUP_RAID10 |
3353 BTRFS_BLOCK_GROUP_RAID5 |
3354 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003355 do {
3356 seq = read_seqbegin(&fs_info->profiles_lock);
3357
3358 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3359 (fs_info->avail_system_alloc_bits & allowed) &&
3360 !(bctl->sys.target & allowed)) ||
3361 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3362 (fs_info->avail_metadata_alloc_bits & allowed) &&
3363 !(bctl->meta.target & allowed))) {
3364 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003365 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003366 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003367 btrfs_err(fs_info, "balance will reduce metadata "
3368 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003369 ret = -EINVAL;
3370 goto out;
3371 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003372 }
Miao Xiede98ced2013-01-29 10:13:12 +00003373 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003374
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003375 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3376 int num_tolerated_disk_barrier_failures;
3377 u64 target = bctl->sys.target;
3378
3379 num_tolerated_disk_barrier_failures =
3380 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3381 if (num_tolerated_disk_barrier_failures > 0 &&
3382 (target &
3383 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3384 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3385 num_tolerated_disk_barrier_failures = 0;
3386 else if (num_tolerated_disk_barrier_failures > 1 &&
3387 (target &
3388 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3389 num_tolerated_disk_barrier_failures = 1;
3390
3391 fs_info->num_tolerated_disk_barrier_failures =
3392 num_tolerated_disk_barrier_failures;
3393 }
3394
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003395 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003396 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003397 goto out;
3398
Ilya Dryomov59641012012-01-16 22:04:48 +02003399 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3400 BUG_ON(ret == -EEXIST);
3401 set_balance_control(bctl);
3402 } else {
3403 BUG_ON(ret != -EEXIST);
3404 spin_lock(&fs_info->balance_lock);
3405 update_balance_args(bctl);
3406 spin_unlock(&fs_info->balance_lock);
3407 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003408
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003409 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003410 mutex_unlock(&fs_info->balance_mutex);
3411
3412 ret = __btrfs_balance(fs_info);
3413
3414 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003415 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003416
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003417 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3418 fs_info->num_tolerated_disk_barrier_failures =
3419 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3420 }
3421
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003422 if (bargs) {
3423 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003424 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003425 }
3426
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003427 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3428 balance_need_close(fs_info)) {
3429 __cancel_balance(fs_info);
3430 }
3431
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003432 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003433
3434 return ret;
3435out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003436 if (bctl->flags & BTRFS_BALANCE_RESUME)
3437 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003438 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003439 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003440 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3441 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003442 return ret;
3443}
3444
3445static int balance_kthread(void *data)
3446{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003447 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003448 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003449
3450 mutex_lock(&fs_info->volume_mutex);
3451 mutex_lock(&fs_info->balance_mutex);
3452
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003453 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003454 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003455 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003456 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003457
3458 mutex_unlock(&fs_info->balance_mutex);
3459 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003460
Ilya Dryomov59641012012-01-16 22:04:48 +02003461 return ret;
3462}
3463
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003464int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3465{
3466 struct task_struct *tsk;
3467
3468 spin_lock(&fs_info->balance_lock);
3469 if (!fs_info->balance_ctl) {
3470 spin_unlock(&fs_info->balance_lock);
3471 return 0;
3472 }
3473 spin_unlock(&fs_info->balance_lock);
3474
3475 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003476 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003477 return 0;
3478 }
3479
3480 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303481 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003482}
3483
Ilya Dryomov68310a52012-06-22 12:24:12 -06003484int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003485{
Ilya Dryomov59641012012-01-16 22:04:48 +02003486 struct btrfs_balance_control *bctl;
3487 struct btrfs_balance_item *item;
3488 struct btrfs_disk_balance_args disk_bargs;
3489 struct btrfs_path *path;
3490 struct extent_buffer *leaf;
3491 struct btrfs_key key;
3492 int ret;
3493
3494 path = btrfs_alloc_path();
3495 if (!path)
3496 return -ENOMEM;
3497
Ilya Dryomov68310a52012-06-22 12:24:12 -06003498 key.objectid = BTRFS_BALANCE_OBJECTID;
3499 key.type = BTRFS_BALANCE_ITEM_KEY;
3500 key.offset = 0;
3501
3502 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3503 if (ret < 0)
3504 goto out;
3505 if (ret > 0) { /* ret = -ENOENT; */
3506 ret = 0;
3507 goto out;
3508 }
3509
Ilya Dryomov59641012012-01-16 22:04:48 +02003510 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3511 if (!bctl) {
3512 ret = -ENOMEM;
3513 goto out;
3514 }
3515
Ilya Dryomov59641012012-01-16 22:04:48 +02003516 leaf = path->nodes[0];
3517 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3518
Ilya Dryomov68310a52012-06-22 12:24:12 -06003519 bctl->fs_info = fs_info;
3520 bctl->flags = btrfs_balance_flags(leaf, item);
3521 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003522
3523 btrfs_balance_data(leaf, item, &disk_bargs);
3524 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3525 btrfs_balance_meta(leaf, item, &disk_bargs);
3526 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3527 btrfs_balance_sys(leaf, item, &disk_bargs);
3528 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3529
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003530 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3531
Ilya Dryomov68310a52012-06-22 12:24:12 -06003532 mutex_lock(&fs_info->volume_mutex);
3533 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003534
Ilya Dryomov68310a52012-06-22 12:24:12 -06003535 set_balance_control(bctl);
3536
3537 mutex_unlock(&fs_info->balance_mutex);
3538 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003539out:
3540 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003541 return ret;
3542}
3543
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003544int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3545{
3546 int ret = 0;
3547
3548 mutex_lock(&fs_info->balance_mutex);
3549 if (!fs_info->balance_ctl) {
3550 mutex_unlock(&fs_info->balance_mutex);
3551 return -ENOTCONN;
3552 }
3553
3554 if (atomic_read(&fs_info->balance_running)) {
3555 atomic_inc(&fs_info->balance_pause_req);
3556 mutex_unlock(&fs_info->balance_mutex);
3557
3558 wait_event(fs_info->balance_wait_q,
3559 atomic_read(&fs_info->balance_running) == 0);
3560
3561 mutex_lock(&fs_info->balance_mutex);
3562 /* we are good with balance_ctl ripped off from under us */
3563 BUG_ON(atomic_read(&fs_info->balance_running));
3564 atomic_dec(&fs_info->balance_pause_req);
3565 } else {
3566 ret = -ENOTCONN;
3567 }
3568
3569 mutex_unlock(&fs_info->balance_mutex);
3570 return ret;
3571}
3572
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003573int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3574{
Ilya Dryomove649e582013-10-10 20:40:21 +03003575 if (fs_info->sb->s_flags & MS_RDONLY)
3576 return -EROFS;
3577
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003578 mutex_lock(&fs_info->balance_mutex);
3579 if (!fs_info->balance_ctl) {
3580 mutex_unlock(&fs_info->balance_mutex);
3581 return -ENOTCONN;
3582 }
3583
3584 atomic_inc(&fs_info->balance_cancel_req);
3585 /*
3586 * if we are running just wait and return, balance item is
3587 * deleted in btrfs_balance in this case
3588 */
3589 if (atomic_read(&fs_info->balance_running)) {
3590 mutex_unlock(&fs_info->balance_mutex);
3591 wait_event(fs_info->balance_wait_q,
3592 atomic_read(&fs_info->balance_running) == 0);
3593 mutex_lock(&fs_info->balance_mutex);
3594 } else {
3595 /* __cancel_balance needs volume_mutex */
3596 mutex_unlock(&fs_info->balance_mutex);
3597 mutex_lock(&fs_info->volume_mutex);
3598 mutex_lock(&fs_info->balance_mutex);
3599
3600 if (fs_info->balance_ctl)
3601 __cancel_balance(fs_info);
3602
3603 mutex_unlock(&fs_info->volume_mutex);
3604 }
3605
3606 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3607 atomic_dec(&fs_info->balance_cancel_req);
3608 mutex_unlock(&fs_info->balance_mutex);
3609 return 0;
3610}
3611
Stefan Behrens803b2f52013-08-15 17:11:21 +02003612static int btrfs_uuid_scan_kthread(void *data)
3613{
3614 struct btrfs_fs_info *fs_info = data;
3615 struct btrfs_root *root = fs_info->tree_root;
3616 struct btrfs_key key;
3617 struct btrfs_key max_key;
3618 struct btrfs_path *path = NULL;
3619 int ret = 0;
3620 struct extent_buffer *eb;
3621 int slot;
3622 struct btrfs_root_item root_item;
3623 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003624 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003625
3626 path = btrfs_alloc_path();
3627 if (!path) {
3628 ret = -ENOMEM;
3629 goto out;
3630 }
3631
3632 key.objectid = 0;
3633 key.type = BTRFS_ROOT_ITEM_KEY;
3634 key.offset = 0;
3635
3636 max_key.objectid = (u64)-1;
3637 max_key.type = BTRFS_ROOT_ITEM_KEY;
3638 max_key.offset = (u64)-1;
3639
3640 path->keep_locks = 1;
3641
3642 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003643 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003644 if (ret) {
3645 if (ret > 0)
3646 ret = 0;
3647 break;
3648 }
3649
3650 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3651 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3652 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3653 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3654 goto skip;
3655
3656 eb = path->nodes[0];
3657 slot = path->slots[0];
3658 item_size = btrfs_item_size_nr(eb, slot);
3659 if (item_size < sizeof(root_item))
3660 goto skip;
3661
Stefan Behrens803b2f52013-08-15 17:11:21 +02003662 read_extent_buffer(eb, &root_item,
3663 btrfs_item_ptr_offset(eb, slot),
3664 (int)sizeof(root_item));
3665 if (btrfs_root_refs(&root_item) == 0)
3666 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003667
3668 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3669 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3670 if (trans)
3671 goto update_tree;
3672
3673 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003674 /*
3675 * 1 - subvol uuid item
3676 * 1 - received_subvol uuid item
3677 */
3678 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3679 if (IS_ERR(trans)) {
3680 ret = PTR_ERR(trans);
3681 break;
3682 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003683 continue;
3684 } else {
3685 goto skip;
3686 }
3687update_tree:
3688 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003689 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3690 root_item.uuid,
3691 BTRFS_UUID_KEY_SUBVOL,
3692 key.objectid);
3693 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003694 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003695 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003696 break;
3697 }
3698 }
3699
3700 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003701 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3702 root_item.received_uuid,
3703 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3704 key.objectid);
3705 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003706 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003707 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003708 break;
3709 }
3710 }
3711
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003712skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003713 if (trans) {
3714 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003715 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003716 if (ret)
3717 break;
3718 }
3719
Stefan Behrens803b2f52013-08-15 17:11:21 +02003720 btrfs_release_path(path);
3721 if (key.offset < (u64)-1) {
3722 key.offset++;
3723 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3724 key.offset = 0;
3725 key.type = BTRFS_ROOT_ITEM_KEY;
3726 } else if (key.objectid < (u64)-1) {
3727 key.offset = 0;
3728 key.type = BTRFS_ROOT_ITEM_KEY;
3729 key.objectid++;
3730 } else {
3731 break;
3732 }
3733 cond_resched();
3734 }
3735
3736out:
3737 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003738 if (trans && !IS_ERR(trans))
3739 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003740 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003741 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003742 else
3743 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003744 up(&fs_info->uuid_tree_rescan_sem);
3745 return 0;
3746}
3747
Stefan Behrens70f80172013-08-15 17:11:23 +02003748/*
3749 * Callback for btrfs_uuid_tree_iterate().
3750 * returns:
3751 * 0 check succeeded, the entry is not outdated.
3752 * < 0 if an error occured.
3753 * > 0 if the check failed, which means the caller shall remove the entry.
3754 */
3755static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3756 u8 *uuid, u8 type, u64 subid)
3757{
3758 struct btrfs_key key;
3759 int ret = 0;
3760 struct btrfs_root *subvol_root;
3761
3762 if (type != BTRFS_UUID_KEY_SUBVOL &&
3763 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3764 goto out;
3765
3766 key.objectid = subid;
3767 key.type = BTRFS_ROOT_ITEM_KEY;
3768 key.offset = (u64)-1;
3769 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3770 if (IS_ERR(subvol_root)) {
3771 ret = PTR_ERR(subvol_root);
3772 if (ret == -ENOENT)
3773 ret = 1;
3774 goto out;
3775 }
3776
3777 switch (type) {
3778 case BTRFS_UUID_KEY_SUBVOL:
3779 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3780 ret = 1;
3781 break;
3782 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3783 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3784 BTRFS_UUID_SIZE))
3785 ret = 1;
3786 break;
3787 }
3788
3789out:
3790 return ret;
3791}
3792
3793static int btrfs_uuid_rescan_kthread(void *data)
3794{
3795 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3796 int ret;
3797
3798 /*
3799 * 1st step is to iterate through the existing UUID tree and
3800 * to delete all entries that contain outdated data.
3801 * 2nd step is to add all missing entries to the UUID tree.
3802 */
3803 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3804 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003805 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003806 up(&fs_info->uuid_tree_rescan_sem);
3807 return ret;
3808 }
3809 return btrfs_uuid_scan_kthread(data);
3810}
3811
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003812int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3813{
3814 struct btrfs_trans_handle *trans;
3815 struct btrfs_root *tree_root = fs_info->tree_root;
3816 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003817 struct task_struct *task;
3818 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003819
3820 /*
3821 * 1 - root node
3822 * 1 - root item
3823 */
3824 trans = btrfs_start_transaction(tree_root, 2);
3825 if (IS_ERR(trans))
3826 return PTR_ERR(trans);
3827
3828 uuid_root = btrfs_create_tree(trans, fs_info,
3829 BTRFS_UUID_TREE_OBJECTID);
3830 if (IS_ERR(uuid_root)) {
3831 btrfs_abort_transaction(trans, tree_root,
3832 PTR_ERR(uuid_root));
3833 return PTR_ERR(uuid_root);
3834 }
3835
3836 fs_info->uuid_root = uuid_root;
3837
Stefan Behrens803b2f52013-08-15 17:11:21 +02003838 ret = btrfs_commit_transaction(trans, tree_root);
3839 if (ret)
3840 return ret;
3841
3842 down(&fs_info->uuid_tree_rescan_sem);
3843 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3844 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003845 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003846 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003847 up(&fs_info->uuid_tree_rescan_sem);
3848 return PTR_ERR(task);
3849 }
3850
3851 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003852}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003853
Stefan Behrens70f80172013-08-15 17:11:23 +02003854int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3855{
3856 struct task_struct *task;
3857
3858 down(&fs_info->uuid_tree_rescan_sem);
3859 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3860 if (IS_ERR(task)) {
3861 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003862 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003863 up(&fs_info->uuid_tree_rescan_sem);
3864 return PTR_ERR(task);
3865 }
3866
3867 return 0;
3868}
3869
Chris Mason8f18cf12008-04-25 16:53:30 -04003870/*
3871 * shrinking a device means finding all of the device extents past
3872 * the new size, and then following the back refs to the chunks.
3873 * The chunk relocation code actually frees the device extent
3874 */
3875int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3876{
3877 struct btrfs_trans_handle *trans;
3878 struct btrfs_root *root = device->dev_root;
3879 struct btrfs_dev_extent *dev_extent = NULL;
3880 struct btrfs_path *path;
3881 u64 length;
3882 u64 chunk_tree;
3883 u64 chunk_objectid;
3884 u64 chunk_offset;
3885 int ret;
3886 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003887 int failed = 0;
3888 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003889 struct extent_buffer *l;
3890 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003891 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003892 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003893 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003894 u64 diff = device->total_bytes - new_size;
3895
Stefan Behrens63a212a2012-11-05 18:29:28 +01003896 if (device->is_tgtdev_for_dev_replace)
3897 return -EINVAL;
3898
Chris Mason8f18cf12008-04-25 16:53:30 -04003899 path = btrfs_alloc_path();
3900 if (!path)
3901 return -ENOMEM;
3902
Chris Mason8f18cf12008-04-25 16:53:30 -04003903 path->reada = 2;
3904
Chris Mason7d9eb122008-07-08 14:19:17 -04003905 lock_chunks(root);
3906
Chris Mason8f18cf12008-04-25 16:53:30 -04003907 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003908 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003909 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003910 spin_lock(&root->fs_info->free_chunk_lock);
3911 root->fs_info->free_chunk_space -= diff;
3912 spin_unlock(&root->fs_info->free_chunk_lock);
3913 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003914 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003915
Josef Bacikba1bf482009-09-11 16:11:19 -04003916again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003917 key.objectid = device->devid;
3918 key.offset = (u64)-1;
3919 key.type = BTRFS_DEV_EXTENT_KEY;
3920
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003921 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003922 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3923 if (ret < 0)
3924 goto done;
3925
3926 ret = btrfs_previous_item(root, path, 0, key.type);
3927 if (ret < 0)
3928 goto done;
3929 if (ret) {
3930 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003931 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003932 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003933 }
3934
3935 l = path->nodes[0];
3936 slot = path->slots[0];
3937 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3938
Josef Bacikba1bf482009-09-11 16:11:19 -04003939 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003940 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003941 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003942 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003943
3944 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3945 length = btrfs_dev_extent_length(l, dev_extent);
3946
Josef Bacikba1bf482009-09-11 16:11:19 -04003947 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003948 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003949 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003950 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003951
3952 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3953 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3954 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003955 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003956
3957 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3958 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003959 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003960 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003961 if (ret == -ENOSPC)
3962 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003963 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003964
3965 if (failed && !retried) {
3966 failed = 0;
3967 retried = true;
3968 goto again;
3969 } else if (failed && retried) {
3970 ret = -ENOSPC;
3971 lock_chunks(root);
3972
3973 device->total_bytes = old_size;
3974 if (device->writeable)
3975 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003976 spin_lock(&root->fs_info->free_chunk_lock);
3977 root->fs_info->free_chunk_space += diff;
3978 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003979 unlock_chunks(root);
3980 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003981 }
3982
Chris Balld6397ba2009-04-27 07:29:03 -04003983 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003984 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003985 if (IS_ERR(trans)) {
3986 ret = PTR_ERR(trans);
3987 goto done;
3988 }
3989
Chris Balld6397ba2009-04-27 07:29:03 -04003990 lock_chunks(root);
3991
3992 device->disk_total_bytes = new_size;
3993 /* Now btrfs_update_device() will change the on-disk size. */
3994 ret = btrfs_update_device(trans, device);
3995 if (ret) {
3996 unlock_chunks(root);
3997 btrfs_end_transaction(trans, root);
3998 goto done;
3999 }
4000 WARN_ON(diff > old_total);
4001 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4002 unlock_chunks(root);
4003 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004004done:
4005 btrfs_free_path(path);
4006 return ret;
4007}
4008
Li Zefan125ccb02011-12-08 15:07:24 +08004009static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004010 struct btrfs_key *key,
4011 struct btrfs_chunk *chunk, int item_size)
4012{
David Sterba6c417612011-04-13 15:41:04 +02004013 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004014 struct btrfs_disk_key disk_key;
4015 u32 array_size;
4016 u8 *ptr;
4017
4018 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004019 if (array_size + item_size + sizeof(disk_key)
4020 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
Chris Mason0b86a832008-03-24 15:01:56 -04004021 return -EFBIG;
4022
4023 ptr = super_copy->sys_chunk_array + array_size;
4024 btrfs_cpu_key_to_disk(&disk_key, key);
4025 memcpy(ptr, &disk_key, sizeof(disk_key));
4026 ptr += sizeof(disk_key);
4027 memcpy(ptr, chunk, item_size);
4028 item_size += sizeof(disk_key);
4029 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4030 return 0;
4031}
4032
Miao Xieb2117a32011-01-05 10:07:28 +00004033/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004034 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004035 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004036static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004037{
Arne Jansen73c5de02011-04-12 12:07:57 +02004038 const struct btrfs_device_info *di_a = a;
4039 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004040
Arne Jansen73c5de02011-04-12 12:07:57 +02004041 if (di_a->max_avail > di_b->max_avail)
4042 return -1;
4043 if (di_a->max_avail < di_b->max_avail)
4044 return 1;
4045 if (di_a->total_avail > di_b->total_avail)
4046 return -1;
4047 if (di_a->total_avail < di_b->total_avail)
4048 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004049 return 0;
4050}
4051
Eric Sandeen48a3b632013-04-25 20:41:01 +00004052static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004053 [BTRFS_RAID_RAID10] = {
4054 .sub_stripes = 2,
4055 .dev_stripes = 1,
4056 .devs_max = 0, /* 0 == as many as possible */
4057 .devs_min = 4,
4058 .devs_increment = 2,
4059 .ncopies = 2,
4060 },
4061 [BTRFS_RAID_RAID1] = {
4062 .sub_stripes = 1,
4063 .dev_stripes = 1,
4064 .devs_max = 2,
4065 .devs_min = 2,
4066 .devs_increment = 2,
4067 .ncopies = 2,
4068 },
4069 [BTRFS_RAID_DUP] = {
4070 .sub_stripes = 1,
4071 .dev_stripes = 2,
4072 .devs_max = 1,
4073 .devs_min = 1,
4074 .devs_increment = 1,
4075 .ncopies = 2,
4076 },
4077 [BTRFS_RAID_RAID0] = {
4078 .sub_stripes = 1,
4079 .dev_stripes = 1,
4080 .devs_max = 0,
4081 .devs_min = 2,
4082 .devs_increment = 1,
4083 .ncopies = 1,
4084 },
4085 [BTRFS_RAID_SINGLE] = {
4086 .sub_stripes = 1,
4087 .dev_stripes = 1,
4088 .devs_max = 1,
4089 .devs_min = 1,
4090 .devs_increment = 1,
4091 .ncopies = 1,
4092 },
Chris Masone942f882013-02-20 14:06:05 -05004093 [BTRFS_RAID_RAID5] = {
4094 .sub_stripes = 1,
4095 .dev_stripes = 1,
4096 .devs_max = 0,
4097 .devs_min = 2,
4098 .devs_increment = 1,
4099 .ncopies = 2,
4100 },
4101 [BTRFS_RAID_RAID6] = {
4102 .sub_stripes = 1,
4103 .dev_stripes = 1,
4104 .devs_max = 0,
4105 .devs_min = 3,
4106 .devs_increment = 1,
4107 .ncopies = 3,
4108 },
Liu Bo31e50222012-11-21 14:18:10 +00004109};
4110
David Woodhouse53b381b2013-01-29 18:40:14 -05004111static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4112{
4113 /* TODO allow them to set a preferred stripe size */
4114 return 64 * 1024;
4115}
4116
4117static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4118{
David Woodhouse53b381b2013-01-29 18:40:14 -05004119 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
4120 return;
4121
Miao Xieceda0862013-04-11 10:30:16 +00004122 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004123}
4124
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004125#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4126 - sizeof(struct btrfs_item) \
4127 - sizeof(struct btrfs_chunk)) \
4128 / sizeof(struct btrfs_stripe) + 1)
4129
4130#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4131 - 2 * sizeof(struct btrfs_disk_key) \
4132 - 2 * sizeof(struct btrfs_chunk)) \
4133 / sizeof(struct btrfs_stripe) + 1)
4134
Miao Xieb2117a32011-01-05 10:07:28 +00004135static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004136 struct btrfs_root *extent_root, u64 start,
4137 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004138{
4139 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004140 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4141 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004142 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004143 struct extent_map_tree *em_tree;
4144 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004145 struct btrfs_device_info *devices_info = NULL;
4146 u64 total_avail;
4147 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004148 int data_stripes; /* number of stripes that count for
4149 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004150 int sub_stripes; /* sub_stripes info for map */
4151 int dev_stripes; /* stripes per dev */
4152 int devs_max; /* max devs to use */
4153 int devs_min; /* min devs needed */
4154 int devs_increment; /* ndevs has to be a multiple of this */
4155 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004156 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004157 u64 max_stripe_size;
4158 u64 max_chunk_size;
4159 u64 stripe_size;
4160 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004161 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004162 int ndevs;
4163 int i;
4164 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004165 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004166
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004167 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004168
Miao Xieb2117a32011-01-05 10:07:28 +00004169 if (list_empty(&fs_devices->alloc_list))
4170 return -ENOSPC;
4171
Liu Bo31e50222012-11-21 14:18:10 +00004172 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004173
Liu Bo31e50222012-11-21 14:18:10 +00004174 sub_stripes = btrfs_raid_array[index].sub_stripes;
4175 dev_stripes = btrfs_raid_array[index].dev_stripes;
4176 devs_max = btrfs_raid_array[index].devs_max;
4177 devs_min = btrfs_raid_array[index].devs_min;
4178 devs_increment = btrfs_raid_array[index].devs_increment;
4179 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004180
4181 if (type & BTRFS_BLOCK_GROUP_DATA) {
4182 max_stripe_size = 1024 * 1024 * 1024;
4183 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004184 if (!devs_max)
4185 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004186 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004187 /* for larger filesystems, use larger metadata chunks */
4188 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4189 max_stripe_size = 1024 * 1024 * 1024;
4190 else
4191 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004192 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004193 if (!devs_max)
4194 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004195 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004196 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004197 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004198 if (!devs_max)
4199 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004200 } else {
David Sterba351fd352014-05-15 16:48:20 +02004201 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004202 type);
4203 BUG_ON(1);
4204 }
4205
4206 /* we don't want a chunk larger than 10% of writeable space */
4207 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4208 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004209
4210 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4211 GFP_NOFS);
4212 if (!devices_info)
4213 return -ENOMEM;
4214
Arne Jansen73c5de02011-04-12 12:07:57 +02004215 cur = fs_devices->alloc_list.next;
4216
4217 /*
4218 * in the first pass through the devices list, we gather information
4219 * about the available holes on each device.
4220 */
4221 ndevs = 0;
4222 while (cur != &fs_devices->alloc_list) {
4223 struct btrfs_device *device;
4224 u64 max_avail;
4225 u64 dev_offset;
4226
4227 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4228
4229 cur = cur->next;
4230
4231 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004232 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004233 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004234 continue;
4235 }
4236
Stefan Behrens63a212a2012-11-05 18:29:28 +01004237 if (!device->in_fs_metadata ||
4238 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004239 continue;
4240
4241 if (device->total_bytes > device->bytes_used)
4242 total_avail = device->total_bytes - device->bytes_used;
4243 else
4244 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004245
4246 /* If there is no space on this device, skip it. */
4247 if (total_avail == 0)
4248 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004249
Josef Bacik6df9a952013-06-27 13:22:46 -04004250 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004251 max_stripe_size * dev_stripes,
4252 &dev_offset, &max_avail);
4253 if (ret && ret != -ENOSPC)
4254 goto error;
4255
4256 if (ret == 0)
4257 max_avail = max_stripe_size * dev_stripes;
4258
4259 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4260 continue;
4261
Eric Sandeen063d0062013-01-31 00:55:01 +00004262 if (ndevs == fs_devices->rw_devices) {
4263 WARN(1, "%s: found more than %llu devices\n",
4264 __func__, fs_devices->rw_devices);
4265 break;
4266 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004267 devices_info[ndevs].dev_offset = dev_offset;
4268 devices_info[ndevs].max_avail = max_avail;
4269 devices_info[ndevs].total_avail = total_avail;
4270 devices_info[ndevs].dev = device;
4271 ++ndevs;
4272 }
4273
4274 /*
4275 * now sort the devices by hole size / available space
4276 */
4277 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4278 btrfs_cmp_device_info, NULL);
4279
4280 /* round down to number of usable stripes */
4281 ndevs -= ndevs % devs_increment;
4282
4283 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4284 ret = -ENOSPC;
4285 goto error;
4286 }
4287
4288 if (devs_max && ndevs > devs_max)
4289 ndevs = devs_max;
4290 /*
4291 * the primary goal is to maximize the number of stripes, so use as many
4292 * devices as possible, even if the stripes are not maximum sized.
4293 */
4294 stripe_size = devices_info[ndevs-1].max_avail;
4295 num_stripes = ndevs * dev_stripes;
4296
David Woodhouse53b381b2013-01-29 18:40:14 -05004297 /*
4298 * this will have to be fixed for RAID1 and RAID10 over
4299 * more drives
4300 */
4301 data_stripes = num_stripes / ncopies;
4302
David Woodhouse53b381b2013-01-29 18:40:14 -05004303 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4304 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4305 btrfs_super_stripesize(info->super_copy));
4306 data_stripes = num_stripes - 1;
4307 }
4308 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4309 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4310 btrfs_super_stripesize(info->super_copy));
4311 data_stripes = num_stripes - 2;
4312 }
Chris Mason86db2572013-02-20 16:23:40 -05004313
4314 /*
4315 * Use the number of data stripes to figure out how big this chunk
4316 * is really going to be in terms of logical address space,
4317 * and compare that answer with the max chunk size
4318 */
4319 if (stripe_size * data_stripes > max_chunk_size) {
4320 u64 mask = (1ULL << 24) - 1;
4321 stripe_size = max_chunk_size;
4322 do_div(stripe_size, data_stripes);
4323
4324 /* bump the answer up to a 16MB boundary */
4325 stripe_size = (stripe_size + mask) & ~mask;
4326
4327 /* but don't go higher than the limits we found
4328 * while searching for free extents
4329 */
4330 if (stripe_size > devices_info[ndevs-1].max_avail)
4331 stripe_size = devices_info[ndevs-1].max_avail;
4332 }
4333
Arne Jansen73c5de02011-04-12 12:07:57 +02004334 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004335
4336 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004337 do_div(stripe_size, raid_stripe_len);
4338 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004339
Miao Xieb2117a32011-01-05 10:07:28 +00004340 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4341 if (!map) {
4342 ret = -ENOMEM;
4343 goto error;
4344 }
4345 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004346
Arne Jansen73c5de02011-04-12 12:07:57 +02004347 for (i = 0; i < ndevs; ++i) {
4348 for (j = 0; j < dev_stripes; ++j) {
4349 int s = i * dev_stripes + j;
4350 map->stripes[s].dev = devices_info[i].dev;
4351 map->stripes[s].physical = devices_info[i].dev_offset +
4352 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004353 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004354 }
Chris Mason593060d2008-03-25 16:50:33 -04004355 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004356 map->stripe_len = raid_stripe_len;
4357 map->io_align = raid_stripe_len;
4358 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004359 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004360 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004361
David Woodhouse53b381b2013-01-29 18:40:14 -05004362 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004363
Arne Jansen73c5de02011-04-12 12:07:57 +02004364 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004365
David Sterba172ddd62011-04-21 00:48:27 +02004366 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004367 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004368 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004369 ret = -ENOMEM;
4370 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004371 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004372 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004373 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004374 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004375 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004376 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004377 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004378 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004379
Chris Mason0b86a832008-03-24 15:01:56 -04004380 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004381 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004382 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004383 if (!ret) {
4384 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4385 atomic_inc(&em->refs);
4386 }
Chris Mason890871b2009-09-02 16:24:52 -04004387 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004388 if (ret) {
4389 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004390 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004391 }
Yan Zheng2b820322008-11-17 21:11:30 -05004392
Josef Bacik04487482013-01-29 15:03:37 -05004393 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4394 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4395 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004396 if (ret)
4397 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004398
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004399 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004400 check_raid56_incompat_flag(extent_root->fs_info, type);
4401
Miao Xieb2117a32011-01-05 10:07:28 +00004402 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004403 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004404
Josef Bacik6df9a952013-06-27 13:22:46 -04004405error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004406 write_lock(&em_tree->lock);
4407 remove_extent_mapping(em_tree, em);
4408 write_unlock(&em_tree->lock);
4409
4410 /* One for our allocation */
4411 free_extent_map(em);
4412 /* One for the tree reference */
4413 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004414error:
Miao Xieb2117a32011-01-05 10:07:28 +00004415 kfree(devices_info);
4416 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004417}
4418
Josef Bacik6df9a952013-06-27 13:22:46 -04004419int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004420 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004421 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004422{
Yan Zheng2b820322008-11-17 21:11:30 -05004423 struct btrfs_key key;
4424 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4425 struct btrfs_device *device;
4426 struct btrfs_chunk *chunk;
4427 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004428 struct extent_map_tree *em_tree;
4429 struct extent_map *em;
4430 struct map_lookup *map;
4431 size_t item_size;
4432 u64 dev_offset;
4433 u64 stripe_size;
4434 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004435 int ret;
4436
Josef Bacik6df9a952013-06-27 13:22:46 -04004437 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4438 read_lock(&em_tree->lock);
4439 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4440 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004441
Josef Bacik6df9a952013-06-27 13:22:46 -04004442 if (!em) {
4443 btrfs_crit(extent_root->fs_info, "unable to find logical "
4444 "%Lu len %Lu", chunk_offset, chunk_size);
4445 return -EINVAL;
4446 }
4447
4448 if (em->start != chunk_offset || em->len != chunk_size) {
4449 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004450 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004451 chunk_size, em->start, em->len);
4452 free_extent_map(em);
4453 return -EINVAL;
4454 }
4455
4456 map = (struct map_lookup *)em->bdev;
4457 item_size = btrfs_chunk_item_size(map->num_stripes);
4458 stripe_size = em->orig_block_len;
4459
4460 chunk = kzalloc(item_size, GFP_NOFS);
4461 if (!chunk) {
4462 ret = -ENOMEM;
4463 goto out;
4464 }
4465
4466 for (i = 0; i < map->num_stripes; i++) {
4467 device = map->stripes[i].dev;
4468 dev_offset = map->stripes[i].physical;
4469
Yan Zheng2b820322008-11-17 21:11:30 -05004470 device->bytes_used += stripe_size;
4471 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004472 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004473 goto out;
4474 ret = btrfs_alloc_dev_extent(trans, device,
4475 chunk_root->root_key.objectid,
4476 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4477 chunk_offset, dev_offset,
4478 stripe_size);
4479 if (ret)
4480 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004481 }
4482
Josef Bacik2bf64752011-09-26 17:12:22 -04004483 spin_lock(&extent_root->fs_info->free_chunk_lock);
4484 extent_root->fs_info->free_chunk_space -= (stripe_size *
4485 map->num_stripes);
4486 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4487
Yan Zheng2b820322008-11-17 21:11:30 -05004488 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004489 for (i = 0; i < map->num_stripes; i++) {
4490 device = map->stripes[i].dev;
4491 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004492
4493 btrfs_set_stack_stripe_devid(stripe, device->devid);
4494 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4495 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4496 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004497 }
4498
4499 btrfs_set_stack_chunk_length(chunk, chunk_size);
4500 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4501 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4502 btrfs_set_stack_chunk_type(chunk, map->type);
4503 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4504 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4505 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4506 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4507 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4508
4509 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4510 key.type = BTRFS_CHUNK_ITEM_KEY;
4511 key.offset = chunk_offset;
4512
4513 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004514 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4515 /*
4516 * TODO: Cleanup of inserted chunk root in case of
4517 * failure.
4518 */
Li Zefan125ccb02011-12-08 15:07:24 +08004519 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004520 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004521 }
liubo1abe9b82011-03-24 11:18:59 +00004522
Josef Bacik6df9a952013-06-27 13:22:46 -04004523out:
Yan Zheng2b820322008-11-17 21:11:30 -05004524 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004525 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004526 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004527}
4528
4529/*
4530 * Chunk allocation falls into two parts. The first part does works
4531 * that make the new allocated chunk useable, but not do any operation
4532 * that modifies the chunk tree. The second part does the works that
4533 * require modifying the chunk tree. This division is important for the
4534 * bootstrap process of adding storage to a seed btrfs.
4535 */
4536int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4537 struct btrfs_root *extent_root, u64 type)
4538{
4539 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004540
Josef Bacik6df9a952013-06-27 13:22:46 -04004541 chunk_offset = find_next_chunk(extent_root->fs_info);
4542 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004543}
4544
Chris Masond3977122009-01-05 21:25:51 -05004545static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004546 struct btrfs_root *root,
4547 struct btrfs_device *device)
4548{
4549 u64 chunk_offset;
4550 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004551 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004552 struct btrfs_fs_info *fs_info = root->fs_info;
4553 struct btrfs_root *extent_root = fs_info->extent_root;
4554 int ret;
4555
Josef Bacik6df9a952013-06-27 13:22:46 -04004556 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004557 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004558 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4559 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004560 if (ret)
4561 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004562
Josef Bacik6df9a952013-06-27 13:22:46 -04004563 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004564 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004565 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4566 alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004567 if (ret) {
4568 btrfs_abort_transaction(trans, root, ret);
4569 goto out;
4570 }
Yan Zheng2b820322008-11-17 21:11:30 -05004571
4572 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004573 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004574 btrfs_abort_transaction(trans, root, ret);
David Sterba005d6422012-09-18 07:52:32 -06004575out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004576 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004577}
4578
4579int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4580{
4581 struct extent_map *em;
4582 struct map_lookup *map;
4583 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4584 int readonly = 0;
4585 int i;
4586
Chris Mason890871b2009-09-02 16:24:52 -04004587 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004588 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004589 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004590 if (!em)
4591 return 1;
4592
Josef Bacikf48b9072010-01-27 02:07:59 +00004593 if (btrfs_test_opt(root, DEGRADED)) {
4594 free_extent_map(em);
4595 return 0;
4596 }
4597
Yan Zheng2b820322008-11-17 21:11:30 -05004598 map = (struct map_lookup *)em->bdev;
4599 for (i = 0; i < map->num_stripes; i++) {
4600 if (!map->stripes[i].dev->writeable) {
4601 readonly = 1;
4602 break;
4603 }
4604 }
4605 free_extent_map(em);
4606 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004607}
4608
4609void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4610{
David Sterbaa8067e02011-04-21 00:34:43 +02004611 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004612}
4613
4614void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4615{
4616 struct extent_map *em;
4617
Chris Masond3977122009-01-05 21:25:51 -05004618 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004619 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004620 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4621 if (em)
4622 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004623 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004624 if (!em)
4625 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004626 /* once for us */
4627 free_extent_map(em);
4628 /* once for the tree */
4629 free_extent_map(em);
4630 }
4631}
4632
Stefan Behrens5d964052012-11-05 14:59:07 +01004633int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004634{
Stefan Behrens5d964052012-11-05 14:59:07 +01004635 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004636 struct extent_map *em;
4637 struct map_lookup *map;
4638 struct extent_map_tree *em_tree = &map_tree->map_tree;
4639 int ret;
4640
Chris Mason890871b2009-09-02 16:24:52 -04004641 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004642 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004643 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004644
Josef Bacikfb7669b2013-04-23 10:53:18 -04004645 /*
4646 * We could return errors for these cases, but that could get ugly and
4647 * we'd probably do the same thing which is just not do anything else
4648 * and exit, so return 1 so the callers don't try to use other copies.
4649 */
4650 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004651 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004652 logical+len);
4653 return 1;
4654 }
4655
4656 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004657 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004658 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004659 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004660 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004661 return 1;
4662 }
4663
Chris Masonf1885912008-04-09 16:28:12 -04004664 map = (struct map_lookup *)em->bdev;
4665 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4666 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004667 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4668 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004669 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4670 ret = 2;
4671 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4672 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004673 else
4674 ret = 1;
4675 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004676
4677 btrfs_dev_replace_lock(&fs_info->dev_replace);
4678 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4679 ret++;
4680 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4681
Chris Masonf1885912008-04-09 16:28:12 -04004682 return ret;
4683}
4684
David Woodhouse53b381b2013-01-29 18:40:14 -05004685unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4686 struct btrfs_mapping_tree *map_tree,
4687 u64 logical)
4688{
4689 struct extent_map *em;
4690 struct map_lookup *map;
4691 struct extent_map_tree *em_tree = &map_tree->map_tree;
4692 unsigned long len = root->sectorsize;
4693
4694 read_lock(&em_tree->lock);
4695 em = lookup_extent_mapping(em_tree, logical, len);
4696 read_unlock(&em_tree->lock);
4697 BUG_ON(!em);
4698
4699 BUG_ON(em->start > logical || em->start + em->len < logical);
4700 map = (struct map_lookup *)em->bdev;
4701 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4702 BTRFS_BLOCK_GROUP_RAID6)) {
4703 len = map->stripe_len * nr_data_stripes(map);
4704 }
4705 free_extent_map(em);
4706 return len;
4707}
4708
4709int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4710 u64 logical, u64 len, int mirror_num)
4711{
4712 struct extent_map *em;
4713 struct map_lookup *map;
4714 struct extent_map_tree *em_tree = &map_tree->map_tree;
4715 int ret = 0;
4716
4717 read_lock(&em_tree->lock);
4718 em = lookup_extent_mapping(em_tree, logical, len);
4719 read_unlock(&em_tree->lock);
4720 BUG_ON(!em);
4721
4722 BUG_ON(em->start > logical || em->start + em->len < logical);
4723 map = (struct map_lookup *)em->bdev;
4724 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4725 BTRFS_BLOCK_GROUP_RAID6))
4726 ret = 1;
4727 free_extent_map(em);
4728 return ret;
4729}
4730
Stefan Behrens30d98612012-11-06 14:52:18 +01004731static int find_live_mirror(struct btrfs_fs_info *fs_info,
4732 struct map_lookup *map, int first, int num,
4733 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004734{
4735 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004736 int tolerance;
4737 struct btrfs_device *srcdev;
4738
4739 if (dev_replace_is_ongoing &&
4740 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4741 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4742 srcdev = fs_info->dev_replace.srcdev;
4743 else
4744 srcdev = NULL;
4745
4746 /*
4747 * try to avoid the drive that is the source drive for a
4748 * dev-replace procedure, only choose it if no other non-missing
4749 * mirror is available
4750 */
4751 for (tolerance = 0; tolerance < 2; tolerance++) {
4752 if (map->stripes[optimal].dev->bdev &&
4753 (tolerance || map->stripes[optimal].dev != srcdev))
4754 return optimal;
4755 for (i = first; i < first + num; i++) {
4756 if (map->stripes[i].dev->bdev &&
4757 (tolerance || map->stripes[i].dev != srcdev))
4758 return i;
4759 }
Chris Masondfe25022008-05-13 13:46:40 -04004760 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004761
Chris Masondfe25022008-05-13 13:46:40 -04004762 /* we couldn't find one that doesn't fail. Just return something
4763 * and the io error handling code will clean up eventually
4764 */
4765 return optimal;
4766}
4767
David Woodhouse53b381b2013-01-29 18:40:14 -05004768static inline int parity_smaller(u64 a, u64 b)
4769{
4770 return a > b;
4771}
4772
4773/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4774static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4775{
4776 struct btrfs_bio_stripe s;
4777 int i;
4778 u64 l;
4779 int again = 1;
4780
4781 while (again) {
4782 again = 0;
4783 for (i = 0; i < bbio->num_stripes - 1; i++) {
4784 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4785 s = bbio->stripes[i];
4786 l = raid_map[i];
4787 bbio->stripes[i] = bbio->stripes[i+1];
4788 raid_map[i] = raid_map[i+1];
4789 bbio->stripes[i+1] = s;
4790 raid_map[i+1] = l;
4791 again = 1;
4792 }
4793 }
4794 }
4795}
4796
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004797static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004798 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004799 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004800 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004801{
4802 struct extent_map *em;
4803 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004804 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004805 struct extent_map_tree *em_tree = &map_tree->map_tree;
4806 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004807 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004808 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004809 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004810 u64 stripe_nr_orig;
4811 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004812 u64 stripe_len;
4813 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004814 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004815 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004816 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004817 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004818 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004819 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004820 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4821 int dev_replace_is_ongoing = 0;
4822 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004823 int patch_the_first_stripe_for_dev_replace = 0;
4824 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004825 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004826
Chris Mason890871b2009-09-02 16:24:52 -04004827 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004828 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004829 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004830
Chris Mason3b951512008-04-17 11:29:12 -04004831 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004832 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004833 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004834 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004835 }
Chris Mason0b86a832008-03-24 15:01:56 -04004836
Josef Bacik9bb91872013-04-19 23:45:33 -04004837 if (em->start > logical || em->start + em->len < logical) {
4838 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004839 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004840 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004841 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004842 return -EINVAL;
4843 }
4844
Chris Mason0b86a832008-03-24 15:01:56 -04004845 map = (struct map_lookup *)em->bdev;
4846 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004847
David Woodhouse53b381b2013-01-29 18:40:14 -05004848 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004849 stripe_nr = offset;
4850 /*
4851 * stripe_nr counts the total number of stripes we have to stride
4852 * to get to this block
4853 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004854 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004855
David Woodhouse53b381b2013-01-29 18:40:14 -05004856 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004857 BUG_ON(offset < stripe_offset);
4858
4859 /* stripe_offset is the offset of this block in its stripe*/
4860 stripe_offset = offset - stripe_offset;
4861
David Woodhouse53b381b2013-01-29 18:40:14 -05004862 /* if we're here for raid56, we need to know the stripe aligned start */
4863 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4864 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4865 raid56_full_stripe_start = offset;
4866
4867 /* allow a write of a full stripe, but make sure we don't
4868 * allow straddling of stripes
4869 */
4870 do_div(raid56_full_stripe_start, full_stripe_len);
4871 raid56_full_stripe_start *= full_stripe_len;
4872 }
4873
4874 if (rw & REQ_DISCARD) {
4875 /* we don't discard raid56 yet */
4876 if (map->type &
4877 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4878 ret = -EOPNOTSUPP;
4879 goto out;
4880 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004881 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004882 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4883 u64 max_len;
4884 /* For writes to RAID[56], allow a full stripeset across all disks.
4885 For other RAID types and for RAID[56] reads, just allow a single
4886 stripe (on a single disk). */
4887 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4888 (rw & REQ_WRITE)) {
4889 max_len = stripe_len * nr_data_stripes(map) -
4890 (offset - raid56_full_stripe_start);
4891 } else {
4892 /* we limit the length of each bio to what fits in a stripe */
4893 max_len = stripe_len - stripe_offset;
4894 }
4895 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004896 } else {
4897 *length = em->len - offset;
4898 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004899
David Woodhouse53b381b2013-01-29 18:40:14 -05004900 /* This is for when we're called from btrfs_merge_bio_hook() and all
4901 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004902 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004903 goto out;
4904
Stefan Behrens472262f2012-11-06 14:43:46 +01004905 btrfs_dev_replace_lock(dev_replace);
4906 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4907 if (!dev_replace_is_ongoing)
4908 btrfs_dev_replace_unlock(dev_replace);
4909
Stefan Behrensad6d6202012-11-06 15:06:47 +01004910 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4911 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4912 dev_replace->tgtdev != NULL) {
4913 /*
4914 * in dev-replace case, for repair case (that's the only
4915 * case where the mirror is selected explicitly when
4916 * calling btrfs_map_block), blocks left of the left cursor
4917 * can also be read from the target drive.
4918 * For REQ_GET_READ_MIRRORS, the target drive is added as
4919 * the last one to the array of stripes. For READ, it also
4920 * needs to be supported using the same mirror number.
4921 * If the requested block is not left of the left cursor,
4922 * EIO is returned. This can happen because btrfs_num_copies()
4923 * returns one more in the dev-replace case.
4924 */
4925 u64 tmp_length = *length;
4926 struct btrfs_bio *tmp_bbio = NULL;
4927 int tmp_num_stripes;
4928 u64 srcdev_devid = dev_replace->srcdev->devid;
4929 int index_srcdev = 0;
4930 int found = 0;
4931 u64 physical_of_found = 0;
4932
4933 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004934 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004935 if (ret) {
4936 WARN_ON(tmp_bbio != NULL);
4937 goto out;
4938 }
4939
4940 tmp_num_stripes = tmp_bbio->num_stripes;
4941 if (mirror_num > tmp_num_stripes) {
4942 /*
4943 * REQ_GET_READ_MIRRORS does not contain this
4944 * mirror, that means that the requested area
4945 * is not left of the left cursor
4946 */
4947 ret = -EIO;
4948 kfree(tmp_bbio);
4949 goto out;
4950 }
4951
4952 /*
4953 * process the rest of the function using the mirror_num
4954 * of the source drive. Therefore look it up first.
4955 * At the end, patch the device pointer to the one of the
4956 * target drive.
4957 */
4958 for (i = 0; i < tmp_num_stripes; i++) {
4959 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4960 /*
4961 * In case of DUP, in order to keep it
4962 * simple, only add the mirror with the
4963 * lowest physical address
4964 */
4965 if (found &&
4966 physical_of_found <=
4967 tmp_bbio->stripes[i].physical)
4968 continue;
4969 index_srcdev = i;
4970 found = 1;
4971 physical_of_found =
4972 tmp_bbio->stripes[i].physical;
4973 }
4974 }
4975
4976 if (found) {
4977 mirror_num = index_srcdev + 1;
4978 patch_the_first_stripe_for_dev_replace = 1;
4979 physical_to_patch_in_first_stripe = physical_of_found;
4980 } else {
4981 WARN_ON(1);
4982 ret = -EIO;
4983 kfree(tmp_bbio);
4984 goto out;
4985 }
4986
4987 kfree(tmp_bbio);
4988 } else if (mirror_num > map->num_stripes) {
4989 mirror_num = 0;
4990 }
4991
Chris Masonf2d8d742008-04-21 10:03:05 -04004992 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004993 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004994 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00004995 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00004996 do_div(stripe_nr_end, map->stripe_len);
4997 stripe_end_offset = stripe_nr_end * map->stripe_len -
4998 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004999
Li Dongyangfce3bb92011-03-24 10:24:26 +00005000 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5001 if (rw & REQ_DISCARD)
5002 num_stripes = min_t(u64, map->num_stripes,
5003 stripe_nr_end - stripe_nr_orig);
5004 stripe_index = do_div(stripe_nr, map->num_stripes);
5005 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005006 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005007 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005008 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005009 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005010 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005011 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005012 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005013 current->pid % map->num_stripes,
5014 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005015 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005016 }
Chris Mason2fff7342008-04-29 14:12:09 -04005017
Chris Mason611f0e02008-04-03 16:29:03 -04005018 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005019 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005020 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005021 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005022 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005023 } else {
5024 mirror_num = 1;
5025 }
Chris Mason2fff7342008-04-29 14:12:09 -04005026
Chris Mason321aecc2008-04-16 10:49:51 -04005027 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5028 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005029
5030 stripe_index = do_div(stripe_nr, factor);
5031 stripe_index *= map->sub_stripes;
5032
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005033 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005034 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005035 else if (rw & REQ_DISCARD)
5036 num_stripes = min_t(u64, map->sub_stripes *
5037 (stripe_nr_end - stripe_nr_orig),
5038 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005039 else if (mirror_num)
5040 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005041 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005042 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005043 stripe_index = find_live_mirror(fs_info, map,
5044 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005045 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005046 current->pid % map->sub_stripes,
5047 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005048 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005049 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005050
5051 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5052 BTRFS_BLOCK_GROUP_RAID6)) {
5053 u64 tmp;
5054
5055 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
5056 && raid_map_ret) {
5057 int i, rot;
5058
5059 /* push stripe_nr back to the start of the full stripe */
5060 stripe_nr = raid56_full_stripe_start;
5061 do_div(stripe_nr, stripe_len);
5062
5063 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5064
5065 /* RAID[56] write or recovery. Return all stripes */
5066 num_stripes = map->num_stripes;
5067 max_errors = nr_parity_stripes(map);
5068
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05305069 raid_map = kmalloc_array(num_stripes, sizeof(u64),
David Woodhouse53b381b2013-01-29 18:40:14 -05005070 GFP_NOFS);
5071 if (!raid_map) {
5072 ret = -ENOMEM;
5073 goto out;
5074 }
5075
5076 /* Work out the disk rotation on this stripe-set */
5077 tmp = stripe_nr;
5078 rot = do_div(tmp, num_stripes);
5079
5080 /* Fill in the logical address of each stripe */
5081 tmp = stripe_nr * nr_data_stripes(map);
5082 for (i = 0; i < nr_data_stripes(map); i++)
5083 raid_map[(i+rot) % num_stripes] =
5084 em->start + (tmp + i) * map->stripe_len;
5085
5086 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5087 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5088 raid_map[(i+rot+1) % num_stripes] =
5089 RAID6_Q_STRIPE;
5090
5091 *length = map->stripe_len;
5092 stripe_index = 0;
5093 stripe_offset = 0;
5094 } else {
5095 /*
5096 * Mirror #0 or #1 means the original data block.
5097 * Mirror #2 is RAID5 parity block.
5098 * Mirror #3 is RAID6 Q block.
5099 */
5100 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5101 if (mirror_num > 1)
5102 stripe_index = nr_data_stripes(map) +
5103 mirror_num - 2;
5104
5105 /* We distribute the parity blocks across stripes */
5106 tmp = stripe_nr + stripe_index;
5107 stripe_index = do_div(tmp, map->num_stripes);
5108 }
Chris Mason8790d502008-04-03 16:29:03 -04005109 } else {
5110 /*
5111 * after this do_div call, stripe_nr is the number of stripes
5112 * on this device we have to walk to find the data, and
5113 * stripe_index is the number of our device in the stripe array
5114 */
5115 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005116 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005117 }
Chris Mason593060d2008-03-25 16:50:33 -04005118 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005119
Stefan Behrens472262f2012-11-06 14:43:46 +01005120 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005121 if (dev_replace_is_ongoing) {
5122 if (rw & (REQ_WRITE | REQ_DISCARD))
5123 num_alloc_stripes <<= 1;
5124 if (rw & REQ_GET_READ_MIRRORS)
5125 num_alloc_stripes++;
5126 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005127 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08005128 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04005129 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08005130 ret = -ENOMEM;
5131 goto out;
5132 }
5133 atomic_set(&bbio->error, 0);
5134
Li Dongyangfce3bb92011-03-24 10:24:26 +00005135 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08005136 int factor = 0;
5137 int sub_stripes = 0;
5138 u64 stripes_per_dev = 0;
5139 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005140 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005141
5142 if (map->type &
5143 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5144 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5145 sub_stripes = 1;
5146 else
5147 sub_stripes = map->sub_stripes;
5148
5149 factor = map->num_stripes / sub_stripes;
5150 stripes_per_dev = div_u64_rem(stripe_nr_end -
5151 stripe_nr_orig,
5152 factor,
5153 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005154 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5155 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005156 }
5157
Li Dongyangfce3bb92011-03-24 10:24:26 +00005158 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005159 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005160 map->stripes[stripe_index].physical +
5161 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005162 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005163
Li Zefanec9ef7a2011-12-01 14:06:42 +08005164 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5165 BTRFS_BLOCK_GROUP_RAID10)) {
5166 bbio->stripes[i].length = stripes_per_dev *
5167 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005168
Li Zefanec9ef7a2011-12-01 14:06:42 +08005169 if (i / sub_stripes < remaining_stripes)
5170 bbio->stripes[i].length +=
5171 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005172
5173 /*
5174 * Special for the first stripe and
5175 * the last stripe:
5176 *
5177 * |-------|...|-------|
5178 * |----------|
5179 * off end_off
5180 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005181 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005182 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005183 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005184
5185 if (stripe_index >= last_stripe &&
5186 stripe_index <= (last_stripe +
5187 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005188 bbio->stripes[i].length -=
5189 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005190
Li Zefanec9ef7a2011-12-01 14:06:42 +08005191 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005192 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005193 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005194 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005195
5196 stripe_index++;
5197 if (stripe_index == map->num_stripes) {
5198 /* This could only happen for RAID0/10 */
5199 stripe_index = 0;
5200 stripe_nr++;
5201 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005202 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005203 } else {
5204 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005205 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005206 map->stripes[stripe_index].physical +
5207 stripe_offset +
5208 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005209 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005210 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005211 stripe_index++;
5212 }
Chris Mason593060d2008-03-25 16:50:33 -04005213 }
Li Zefande11cc12011-12-01 12:55:47 +08005214
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005215 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
Li Zefande11cc12011-12-01 12:55:47 +08005216 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5217 BTRFS_BLOCK_GROUP_RAID10 |
David Woodhouse53b381b2013-01-29 18:40:14 -05005218 BTRFS_BLOCK_GROUP_RAID5 |
Li Zefande11cc12011-12-01 12:55:47 +08005219 BTRFS_BLOCK_GROUP_DUP)) {
5220 max_errors = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005221 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5222 max_errors = 2;
Li Zefande11cc12011-12-01 12:55:47 +08005223 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005224 }
Li Zefande11cc12011-12-01 12:55:47 +08005225
Stefan Behrens472262f2012-11-06 14:43:46 +01005226 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5227 dev_replace->tgtdev != NULL) {
5228 int index_where_to_add;
5229 u64 srcdev_devid = dev_replace->srcdev->devid;
5230
5231 /*
5232 * duplicate the write operations while the dev replace
5233 * procedure is running. Since the copying of the old disk
5234 * to the new disk takes place at run time while the
5235 * filesystem is mounted writable, the regular write
5236 * operations to the old disk have to be duplicated to go
5237 * to the new disk as well.
5238 * Note that device->missing is handled by the caller, and
5239 * that the write to the old disk is already set up in the
5240 * stripes array.
5241 */
5242 index_where_to_add = num_stripes;
5243 for (i = 0; i < num_stripes; i++) {
5244 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5245 /* write to new disk, too */
5246 struct btrfs_bio_stripe *new =
5247 bbio->stripes + index_where_to_add;
5248 struct btrfs_bio_stripe *old =
5249 bbio->stripes + i;
5250
5251 new->physical = old->physical;
5252 new->length = old->length;
5253 new->dev = dev_replace->tgtdev;
5254 index_where_to_add++;
5255 max_errors++;
5256 }
5257 }
5258 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005259 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5260 dev_replace->tgtdev != NULL) {
5261 u64 srcdev_devid = dev_replace->srcdev->devid;
5262 int index_srcdev = 0;
5263 int found = 0;
5264 u64 physical_of_found = 0;
5265
5266 /*
5267 * During the dev-replace procedure, the target drive can
5268 * also be used to read data in case it is needed to repair
5269 * a corrupt block elsewhere. This is possible if the
5270 * requested area is left of the left cursor. In this area,
5271 * the target drive is a full copy of the source drive.
5272 */
5273 for (i = 0; i < num_stripes; i++) {
5274 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5275 /*
5276 * In case of DUP, in order to keep it
5277 * simple, only add the mirror with the
5278 * lowest physical address
5279 */
5280 if (found &&
5281 physical_of_found <=
5282 bbio->stripes[i].physical)
5283 continue;
5284 index_srcdev = i;
5285 found = 1;
5286 physical_of_found = bbio->stripes[i].physical;
5287 }
5288 }
5289 if (found) {
5290 u64 length = map->stripe_len;
5291
5292 if (physical_of_found + length <=
5293 dev_replace->cursor_left) {
5294 struct btrfs_bio_stripe *tgtdev_stripe =
5295 bbio->stripes + num_stripes;
5296
5297 tgtdev_stripe->physical = physical_of_found;
5298 tgtdev_stripe->length =
5299 bbio->stripes[index_srcdev].length;
5300 tgtdev_stripe->dev = dev_replace->tgtdev;
5301
5302 num_stripes++;
5303 }
5304 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005305 }
5306
Li Zefande11cc12011-12-01 12:55:47 +08005307 *bbio_ret = bbio;
5308 bbio->num_stripes = num_stripes;
5309 bbio->max_errors = max_errors;
5310 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005311
5312 /*
5313 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5314 * mirror_num == num_stripes + 1 && dev_replace target drive is
5315 * available as a mirror
5316 */
5317 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5318 WARN_ON(num_stripes > 1);
5319 bbio->stripes[0].dev = dev_replace->tgtdev;
5320 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5321 bbio->mirror_num = map->num_stripes + 1;
5322 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005323 if (raid_map) {
5324 sort_parity_stripes(bbio, raid_map);
5325 *raid_map_ret = raid_map;
5326 }
Chris Masoncea9e442008-04-09 16:28:12 -04005327out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005328 if (dev_replace_is_ongoing)
5329 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005330 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005331 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005332}
5333
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005334int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005335 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005336 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005337{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005338 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005339 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005340}
5341
Yan Zhenga512bbf2008-12-08 16:46:26 -05005342int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5343 u64 chunk_start, u64 physical, u64 devid,
5344 u64 **logical, int *naddrs, int *stripe_len)
5345{
5346 struct extent_map_tree *em_tree = &map_tree->map_tree;
5347 struct extent_map *em;
5348 struct map_lookup *map;
5349 u64 *buf;
5350 u64 bytenr;
5351 u64 length;
5352 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005353 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005354 int i, j, nr = 0;
5355
Chris Mason890871b2009-09-02 16:24:52 -04005356 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005357 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005358 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005359
Josef Bacik835d9742013-03-19 12:13:25 -04005360 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005361 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005362 chunk_start);
5363 return -EIO;
5364 }
5365
5366 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005367 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005368 em->start, chunk_start);
5369 free_extent_map(em);
5370 return -EIO;
5371 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005372 map = (struct map_lookup *)em->bdev;
5373
5374 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005375 rmap_len = map->stripe_len;
5376
Yan Zhenga512bbf2008-12-08 16:46:26 -05005377 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5378 do_div(length, map->num_stripes / map->sub_stripes);
5379 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5380 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005381 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5382 BTRFS_BLOCK_GROUP_RAID6)) {
5383 do_div(length, nr_data_stripes(map));
5384 rmap_len = map->stripe_len * nr_data_stripes(map);
5385 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005386
5387 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005388 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005389
5390 for (i = 0; i < map->num_stripes; i++) {
5391 if (devid && map->stripes[i].dev->devid != devid)
5392 continue;
5393 if (map->stripes[i].physical > physical ||
5394 map->stripes[i].physical + length <= physical)
5395 continue;
5396
5397 stripe_nr = physical - map->stripes[i].physical;
5398 do_div(stripe_nr, map->stripe_len);
5399
5400 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5401 stripe_nr = stripe_nr * map->num_stripes + i;
5402 do_div(stripe_nr, map->sub_stripes);
5403 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5404 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005405 } /* else if RAID[56], multiply by nr_data_stripes().
5406 * Alternatively, just use rmap_len below instead of
5407 * map->stripe_len */
5408
5409 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005410 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005411 for (j = 0; j < nr; j++) {
5412 if (buf[j] == bytenr)
5413 break;
5414 }
Chris Mason934d3752008-12-08 16:43:10 -05005415 if (j == nr) {
5416 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005417 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005418 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005419 }
5420
Yan Zhenga512bbf2008-12-08 16:46:26 -05005421 *logical = buf;
5422 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005423 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005424
5425 free_extent_map(em);
5426 return 0;
5427}
5428
Miao Xie8408c712014-06-19 10:42:55 +08005429static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5430{
5431 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5432 bio_endio_nodec(bio, err);
5433 else
5434 bio_endio(bio, err);
5435 kfree(bbio);
5436}
5437
Jan Schmidta1d3c472011-08-04 17:15:33 +02005438static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005439{
Chris Mason9be33952013-05-17 18:30:14 -04005440 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005441 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005442 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005443
Stefan Behrens442a4f62012-05-25 16:06:08 +02005444 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005445 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005446 if (err == -EIO || err == -EREMOTEIO) {
5447 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005448 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005449
5450 BUG_ON(stripe_index >= bbio->num_stripes);
5451 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005452 if (dev->bdev) {
5453 if (bio->bi_rw & WRITE)
5454 btrfs_dev_stat_inc(dev,
5455 BTRFS_DEV_STAT_WRITE_ERRS);
5456 else
5457 btrfs_dev_stat_inc(dev,
5458 BTRFS_DEV_STAT_READ_ERRS);
5459 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5460 btrfs_dev_stat_inc(dev,
5461 BTRFS_DEV_STAT_FLUSH_ERRS);
5462 btrfs_dev_stat_print_on_error(dev);
5463 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005464 }
5465 }
Chris Mason8790d502008-04-03 16:29:03 -04005466
Jan Schmidta1d3c472011-08-04 17:15:33 +02005467 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005468 is_orig_bio = 1;
5469
Miao Xiec404e0d2014-01-30 16:46:55 +08005470 btrfs_bio_counter_dec(bbio->fs_info);
5471
Jan Schmidta1d3c472011-08-04 17:15:33 +02005472 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005473 if (!is_orig_bio) {
5474 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005475 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005476 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005477
Jan Schmidta1d3c472011-08-04 17:15:33 +02005478 bio->bi_private = bbio->private;
5479 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005480 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005481 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005482 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005483 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005484 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005485 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005486 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005487 /*
5488 * this bio is actually up to date, we didn't
5489 * go over the max number of errors
5490 */
5491 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005492 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005493 }
Miao Xiec55f1392014-06-19 10:42:54 +08005494
Miao Xie8408c712014-06-19 10:42:55 +08005495 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005496 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005497 bio_put(bio);
5498 }
Chris Mason8790d502008-04-03 16:29:03 -04005499}
5500
Chris Mason8b712842008-06-11 16:50:36 -04005501/*
5502 * see run_scheduled_bios for a description of why bios are collected for
5503 * async submit.
5504 *
5505 * This will add one bio to the pending list for a device and make sure
5506 * the work struct is scheduled.
5507 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005508static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5509 struct btrfs_device *device,
5510 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005511{
5512 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005513 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005514
David Woodhouse53b381b2013-01-29 18:40:14 -05005515 if (device->missing || !device->bdev) {
5516 bio_endio(bio, -EIO);
5517 return;
5518 }
5519
Chris Mason8b712842008-06-11 16:50:36 -04005520 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005521 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005522 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005523 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005524 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005525 return;
Chris Mason8b712842008-06-11 16:50:36 -04005526 }
5527
5528 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005529 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005530 * higher layers. Otherwise, the async bio makes it appear we have
5531 * made progress against dirty pages when we've really just put it
5532 * on a queue for later
5533 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005534 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005535 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005536 bio->bi_next = NULL;
5537 bio->bi_rw |= rw;
5538
5539 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005540 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005541 pending_bios = &device->pending_sync_bios;
5542 else
5543 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005544
Chris Masonffbd5172009-04-20 15:50:09 -04005545 if (pending_bios->tail)
5546 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005547
Chris Masonffbd5172009-04-20 15:50:09 -04005548 pending_bios->tail = bio;
5549 if (!pending_bios->head)
5550 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005551 if (device->running_pending)
5552 should_queue = 0;
5553
5554 spin_unlock(&device->io_lock);
5555
5556 if (should_queue)
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005557 btrfs_queue_work(root->fs_info->submit_workers,
5558 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005559}
5560
Josef Bacikde1ee922012-10-19 16:50:56 -04005561static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5562 sector_t sector)
5563{
5564 struct bio_vec *prev;
5565 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005566 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005567 struct bvec_merge_data bvm = {
5568 .bi_bdev = bdev,
5569 .bi_sector = sector,
5570 .bi_rw = bio->bi_rw,
5571 };
5572
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305573 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005574 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005575
5576 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005577 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005578 return 0;
5579
5580 if (!q->merge_bvec_fn)
5581 return 1;
5582
Kent Overstreet4f024f32013-10-11 15:44:27 -07005583 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005584 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5585 return 0;
5586 return 1;
5587}
5588
5589static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5590 struct bio *bio, u64 physical, int dev_nr,
5591 int rw, int async)
5592{
5593 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5594
5595 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005596 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005597 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005598 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005599#ifdef DEBUG
5600 {
5601 struct rcu_string *name;
5602
5603 rcu_read_lock();
5604 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005605 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005606 "(%s id %llu), size=%u\n", rw,
5607 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5608 name->str, dev->devid, bio->bi_size);
5609 rcu_read_unlock();
5610 }
5611#endif
5612 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005613
5614 btrfs_bio_counter_inc_noblocked(root->fs_info);
5615
Josef Bacikde1ee922012-10-19 16:50:56 -04005616 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005617 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005618 else
5619 btrfsic_submit_bio(rw, bio);
5620}
5621
5622static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5623 struct bio *first_bio, struct btrfs_device *dev,
5624 int dev_nr, int rw, int async)
5625{
5626 struct bio_vec *bvec = first_bio->bi_io_vec;
5627 struct bio *bio;
5628 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5629 u64 physical = bbio->stripes[dev_nr].physical;
5630
5631again:
5632 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5633 if (!bio)
5634 return -ENOMEM;
5635
5636 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5637 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5638 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005639 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005640
5641 atomic_inc(&bbio->stripes_pending);
5642 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5643 rw, async);
5644 physical += len;
5645 goto again;
5646 }
5647 bvec++;
5648 }
5649
5650 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5651 return 0;
5652}
5653
5654static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5655{
5656 atomic_inc(&bbio->error);
5657 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005658 /* Shoud be the original bio. */
5659 WARN_ON(bio != bbio->orig_bio);
5660
Josef Bacikde1ee922012-10-19 16:50:56 -04005661 bio->bi_private = bbio->private;
5662 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005663 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005664 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005665
5666 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005667 }
5668}
5669
Chris Masonf1885912008-04-09 16:28:12 -04005670int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005671 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005672{
Chris Mason0b86a832008-03-24 15:01:56 -04005673 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005674 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005675 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005676 u64 length = 0;
5677 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005678 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005679 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005680 int dev_nr = 0;
5681 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005682 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005683
Kent Overstreet4f024f32013-10-11 15:44:27 -07005684 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005685 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005686
Miao Xiec404e0d2014-01-30 16:46:55 +08005687 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005688 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5689 mirror_num, &raid_map);
Miao Xiec404e0d2014-01-30 16:46:55 +08005690 if (ret) {
5691 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005692 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005693 }
Chris Masoncea9e442008-04-09 16:28:12 -04005694
Jan Schmidta1d3c472011-08-04 17:15:33 +02005695 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005696 bbio->orig_bio = first_bio;
5697 bbio->private = first_bio->bi_private;
5698 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005699 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005700 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5701
5702 if (raid_map) {
5703 /* In this case, map_length has been set to the length of
5704 a single stripe; not the whole write */
5705 if (rw & WRITE) {
Miao Xiec404e0d2014-01-30 16:46:55 +08005706 ret = raid56_parity_write(root, bio, bbio,
5707 raid_map, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005708 } else {
Miao Xiec404e0d2014-01-30 16:46:55 +08005709 ret = raid56_parity_recover(root, bio, bbio,
5710 raid_map, map_length,
5711 mirror_num);
David Woodhouse53b381b2013-01-29 18:40:14 -05005712 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005713 /*
5714 * FIXME, replace dosen't support raid56 yet, please fix
5715 * it in the future.
5716 */
5717 btrfs_bio_counter_dec(root->fs_info);
5718 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005719 }
5720
Chris Masoncea9e442008-04-09 16:28:12 -04005721 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005722 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005723 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005724 BUG();
5725 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005726
Chris Masond3977122009-01-05 21:25:51 -05005727 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005728 dev = bbio->stripes[dev_nr].dev;
5729 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5730 bbio_error(bbio, first_bio, logical);
5731 dev_nr++;
5732 continue;
5733 }
5734
5735 /*
5736 * Check and see if we're ok with this bio based on it's size
5737 * and offset with the given device.
5738 */
5739 if (!bio_size_ok(dev->bdev, first_bio,
5740 bbio->stripes[dev_nr].physical >> 9)) {
5741 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5742 dev_nr, rw, async_submit);
5743 BUG_ON(ret);
5744 dev_nr++;
5745 continue;
5746 }
5747
Jan Schmidta1d3c472011-08-04 17:15:33 +02005748 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005749 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005750 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005751 } else {
5752 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005753 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005754 }
Josef Bacik606686e2012-06-04 14:03:51 -04005755
Josef Bacikde1ee922012-10-19 16:50:56 -04005756 submit_stripe_bio(root, bbio, bio,
5757 bbio->stripes[dev_nr].physical, dev_nr, rw,
5758 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005759 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005760 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005761 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005762 return 0;
5763}
5764
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005765struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005766 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005767{
Yan Zheng2b820322008-11-17 21:11:30 -05005768 struct btrfs_device *device;
5769 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005770
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005771 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005772 while (cur_devices) {
5773 if (!fsid ||
5774 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5775 device = __find_device(&cur_devices->devices,
5776 devid, uuid);
5777 if (device)
5778 return device;
5779 }
5780 cur_devices = cur_devices->seed;
5781 }
5782 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005783}
5784
Chris Masondfe25022008-05-13 13:46:40 -04005785static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5786 u64 devid, u8 *dev_uuid)
5787{
5788 struct btrfs_device *device;
5789 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5790
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005791 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5792 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005793 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005794
5795 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005796 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005797 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005798
5799 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005800 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005801
Chris Masondfe25022008-05-13 13:46:40 -04005802 return device;
5803}
5804
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005805/**
5806 * btrfs_alloc_device - allocate struct btrfs_device
5807 * @fs_info: used only for generating a new devid, can be NULL if
5808 * devid is provided (i.e. @devid != NULL).
5809 * @devid: a pointer to devid for this device. If NULL a new devid
5810 * is generated.
5811 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5812 * is generated.
5813 *
5814 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5815 * on error. Returned struct is not linked onto any lists and can be
5816 * destroyed with kfree() right away.
5817 */
5818struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5819 const u64 *devid,
5820 const u8 *uuid)
5821{
5822 struct btrfs_device *dev;
5823 u64 tmp;
5824
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305825 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005826 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005827
5828 dev = __alloc_device();
5829 if (IS_ERR(dev))
5830 return dev;
5831
5832 if (devid)
5833 tmp = *devid;
5834 else {
5835 int ret;
5836
5837 ret = find_next_devid(fs_info, &tmp);
5838 if (ret) {
5839 kfree(dev);
5840 return ERR_PTR(ret);
5841 }
5842 }
5843 dev->devid = tmp;
5844
5845 if (uuid)
5846 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5847 else
5848 generate_random_uuid(dev->uuid);
5849
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005850 btrfs_init_work(&dev->work, pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005851
5852 return dev;
5853}
5854
Chris Mason0b86a832008-03-24 15:01:56 -04005855static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5856 struct extent_buffer *leaf,
5857 struct btrfs_chunk *chunk)
5858{
5859 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5860 struct map_lookup *map;
5861 struct extent_map *em;
5862 u64 logical;
5863 u64 length;
5864 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005865 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005866 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005867 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005868 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005869
Chris Masone17cade2008-04-15 15:41:47 -04005870 logical = key->offset;
5871 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005872
Chris Mason890871b2009-09-02 16:24:52 -04005873 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005874 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005875 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005876
5877 /* already mapped? */
5878 if (em && em->start <= logical && em->start + em->len > logical) {
5879 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005880 return 0;
5881 } else if (em) {
5882 free_extent_map(em);
5883 }
Chris Mason0b86a832008-03-24 15:01:56 -04005884
David Sterba172ddd62011-04-21 00:48:27 +02005885 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005886 if (!em)
5887 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005888 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5889 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005890 if (!map) {
5891 free_extent_map(em);
5892 return -ENOMEM;
5893 }
5894
Wang Shilong298a8f92014-06-19 10:42:52 +08005895 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04005896 em->bdev = (struct block_device *)map;
5897 em->start = logical;
5898 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005899 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005900 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005901 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005902
Chris Mason593060d2008-03-25 16:50:33 -04005903 map->num_stripes = num_stripes;
5904 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5905 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5906 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5907 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5908 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005909 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005910 for (i = 0; i < num_stripes; i++) {
5911 map->stripes[i].physical =
5912 btrfs_stripe_offset_nr(leaf, chunk, i);
5913 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005914 read_extent_buffer(leaf, uuid, (unsigned long)
5915 btrfs_stripe_dev_uuid_nr(chunk, i),
5916 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005917 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5918 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005919 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005920 free_extent_map(em);
5921 return -EIO;
5922 }
Chris Masondfe25022008-05-13 13:46:40 -04005923 if (!map->stripes[i].dev) {
5924 map->stripes[i].dev =
5925 add_missing_dev(root, devid, uuid);
5926 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04005927 free_extent_map(em);
5928 return -EIO;
5929 }
5930 }
5931 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005932 }
5933
Chris Mason890871b2009-09-02 16:24:52 -04005934 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005935 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005936 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005937 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005938 free_extent_map(em);
5939
5940 return 0;
5941}
5942
Jeff Mahoney143bede2012-03-01 14:56:26 +01005943static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005944 struct btrfs_dev_item *dev_item,
5945 struct btrfs_device *device)
5946{
5947 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005948
5949 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005950 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5951 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005952 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5953 device->type = btrfs_device_type(leaf, dev_item);
5954 device->io_align = btrfs_device_io_align(leaf, dev_item);
5955 device->io_width = btrfs_device_io_width(leaf, dev_item);
5956 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005957 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005958 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005959
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02005960 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005961 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005962}
5963
Yan Zheng2b820322008-11-17 21:11:30 -05005964static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5965{
5966 struct btrfs_fs_devices *fs_devices;
5967 int ret;
5968
Li Zefanb367e472011-12-07 11:38:24 +08005969 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005970
5971 fs_devices = root->fs_info->fs_devices->seed;
5972 while (fs_devices) {
5973 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5974 ret = 0;
5975 goto out;
5976 }
5977 fs_devices = fs_devices->seed;
5978 }
5979
5980 fs_devices = find_fsid(fsid);
5981 if (!fs_devices) {
5982 ret = -ENOENT;
5983 goto out;
5984 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005985
5986 fs_devices = clone_fs_devices(fs_devices);
5987 if (IS_ERR(fs_devices)) {
5988 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005989 goto out;
5990 }
5991
Christoph Hellwig97288f22008-12-02 06:36:09 -05005992 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05005993 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02005994 if (ret) {
5995 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005996 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02005997 }
Yan Zheng2b820322008-11-17 21:11:30 -05005998
5999 if (!fs_devices->seeding) {
6000 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006001 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006002 ret = -EINVAL;
6003 goto out;
6004 }
6005
6006 fs_devices->seed = root->fs_info->fs_devices->seed;
6007 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006008out:
Yan Zheng2b820322008-11-17 21:11:30 -05006009 return ret;
6010}
6011
Chris Mason0d81ba52008-03-24 15:02:07 -04006012static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006013 struct extent_buffer *leaf,
6014 struct btrfs_dev_item *dev_item)
6015{
6016 struct btrfs_device *device;
6017 u64 devid;
6018 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006019 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006020 u8 dev_uuid[BTRFS_UUID_SIZE];
6021
Chris Mason0b86a832008-03-24 15:01:56 -04006022 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006023 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006024 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006025 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006026 BTRFS_UUID_SIZE);
6027
6028 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6029 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05006030 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006031 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006032 }
6033
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006034 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006035 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006036 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006037 return -EIO;
6038
6039 if (!device) {
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006040 btrfs_warn(root->fs_info, "devid %llu missing", devid);
Yan Zheng2b820322008-11-17 21:11:30 -05006041 device = add_missing_dev(root, devid, dev_uuid);
6042 if (!device)
6043 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05006044 } else if (!device->missing) {
6045 /*
6046 * this happens when a device that was properly setup
6047 * in the device info lists suddenly goes bad.
6048 * device->bdev is NULL, and so we have to set
6049 * device->missing to one here
6050 */
6051 root->fs_info->fs_devices->missing_devices++;
6052 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006053 }
6054 }
6055
6056 if (device->fs_devices != root->fs_info->fs_devices) {
6057 BUG_ON(device->writeable);
6058 if (device->generation !=
6059 btrfs_device_generation(leaf, dev_item))
6060 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006061 }
Chris Mason0b86a832008-03-24 15:01:56 -04006062
6063 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006064 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006065 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006066 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006067 spin_lock(&root->fs_info->free_chunk_lock);
6068 root->fs_info->free_chunk_space += device->total_bytes -
6069 device->bytes_used;
6070 spin_unlock(&root->fs_info->free_chunk_lock);
6071 }
Chris Mason0b86a832008-03-24 15:01:56 -04006072 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006073 return ret;
6074}
6075
Yan Zhenge4404d62008-12-12 10:03:26 -05006076int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006077{
David Sterba6c417612011-04-13 15:41:04 +02006078 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006079 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006080 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006081 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04006082 u8 *ptr;
6083 unsigned long sb_ptr;
6084 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006085 u32 num_stripes;
6086 u32 array_size;
6087 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006088 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04006089 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006090
Yan Zhenge4404d62008-12-12 10:03:26 -05006091 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04006092 BTRFS_SUPER_INFO_SIZE);
6093 if (!sb)
6094 return -ENOMEM;
6095 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006096 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006097 /*
6098 * The sb extent buffer is artifical and just used to read the system array.
6099 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6100 * pages up-to-date when the page is larger: extent does not cover the
6101 * whole page and consequently check_page_uptodate does not find all
6102 * the page's extents up-to-date (the hole beyond sb),
6103 * write_extent_buffer then triggers a WARN_ON.
6104 *
6105 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6106 * but sb spans only this function. Add an explicit SetPageUptodate call
6107 * to silence the warning eg. on PowerPC 64.
6108 */
6109 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006110 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006111
Chris Masona061fc82008-05-07 11:43:44 -04006112 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006113 array_size = btrfs_super_sys_array_size(super_copy);
6114
Chris Mason0b86a832008-03-24 15:01:56 -04006115 ptr = super_copy->sys_chunk_array;
6116 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
6117 cur = 0;
6118
6119 while (cur < array_size) {
6120 disk_key = (struct btrfs_disk_key *)ptr;
6121 btrfs_disk_key_to_cpu(&key, disk_key);
6122
Chris Masona061fc82008-05-07 11:43:44 -04006123 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006124 sb_ptr += len;
6125 cur += len;
6126
Chris Mason0d81ba52008-03-24 15:02:07 -04006127 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04006128 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04006129 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006130 if (ret)
6131 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006132 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6133 len = btrfs_chunk_item_size(num_stripes);
6134 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006135 ret = -EIO;
6136 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006137 }
6138 ptr += len;
6139 sb_ptr += len;
6140 cur += len;
6141 }
Chris Masona061fc82008-05-07 11:43:44 -04006142 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006143 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006144}
6145
6146int btrfs_read_chunk_tree(struct btrfs_root *root)
6147{
6148 struct btrfs_path *path;
6149 struct extent_buffer *leaf;
6150 struct btrfs_key key;
6151 struct btrfs_key found_key;
6152 int ret;
6153 int slot;
6154
6155 root = root->fs_info->chunk_root;
6156
6157 path = btrfs_alloc_path();
6158 if (!path)
6159 return -ENOMEM;
6160
Li Zefanb367e472011-12-07 11:38:24 +08006161 mutex_lock(&uuid_mutex);
6162 lock_chunks(root);
6163
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006164 /*
6165 * Read all device items, and then all the chunk items. All
6166 * device items are found before any chunk item (their object id
6167 * is smaller than the lowest possible object id for a chunk
6168 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006169 */
6170 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6171 key.offset = 0;
6172 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006173 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006174 if (ret < 0)
6175 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006176 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006177 leaf = path->nodes[0];
6178 slot = path->slots[0];
6179 if (slot >= btrfs_header_nritems(leaf)) {
6180 ret = btrfs_next_leaf(root, path);
6181 if (ret == 0)
6182 continue;
6183 if (ret < 0)
6184 goto error;
6185 break;
6186 }
6187 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006188 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6189 struct btrfs_dev_item *dev_item;
6190 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006191 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006192 ret = read_one_dev(root, leaf, dev_item);
6193 if (ret)
6194 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006195 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6196 struct btrfs_chunk *chunk;
6197 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6198 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006199 if (ret)
6200 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006201 }
6202 path->slots[0]++;
6203 }
Chris Mason0b86a832008-03-24 15:01:56 -04006204 ret = 0;
6205error:
Li Zefanb367e472011-12-07 11:38:24 +08006206 unlock_chunks(root);
6207 mutex_unlock(&uuid_mutex);
6208
Yan Zheng2b820322008-11-17 21:11:30 -05006209 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006210 return ret;
6211}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006212
Miao Xiecb517ea2013-05-15 07:48:19 +00006213void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6214{
6215 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6216 struct btrfs_device *device;
6217
Liu Bo29cc83f2014-05-11 23:14:59 +08006218 while (fs_devices) {
6219 mutex_lock(&fs_devices->device_list_mutex);
6220 list_for_each_entry(device, &fs_devices->devices, dev_list)
6221 device->dev_root = fs_info->dev_root;
6222 mutex_unlock(&fs_devices->device_list_mutex);
6223
6224 fs_devices = fs_devices->seed;
6225 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006226}
6227
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006228static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6229{
6230 int i;
6231
6232 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6233 btrfs_dev_stat_reset(dev, i);
6234}
6235
6236int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6237{
6238 struct btrfs_key key;
6239 struct btrfs_key found_key;
6240 struct btrfs_root *dev_root = fs_info->dev_root;
6241 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6242 struct extent_buffer *eb;
6243 int slot;
6244 int ret = 0;
6245 struct btrfs_device *device;
6246 struct btrfs_path *path = NULL;
6247 int i;
6248
6249 path = btrfs_alloc_path();
6250 if (!path) {
6251 ret = -ENOMEM;
6252 goto out;
6253 }
6254
6255 mutex_lock(&fs_devices->device_list_mutex);
6256 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6257 int item_size;
6258 struct btrfs_dev_stats_item *ptr;
6259
6260 key.objectid = 0;
6261 key.type = BTRFS_DEV_STATS_KEY;
6262 key.offset = device->devid;
6263 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6264 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006265 __btrfs_reset_dev_stats(device);
6266 device->dev_stats_valid = 1;
6267 btrfs_release_path(path);
6268 continue;
6269 }
6270 slot = path->slots[0];
6271 eb = path->nodes[0];
6272 btrfs_item_key_to_cpu(eb, &found_key, slot);
6273 item_size = btrfs_item_size_nr(eb, slot);
6274
6275 ptr = btrfs_item_ptr(eb, slot,
6276 struct btrfs_dev_stats_item);
6277
6278 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6279 if (item_size >= (1 + i) * sizeof(__le64))
6280 btrfs_dev_stat_set(device, i,
6281 btrfs_dev_stats_value(eb, ptr, i));
6282 else
6283 btrfs_dev_stat_reset(device, i);
6284 }
6285
6286 device->dev_stats_valid = 1;
6287 btrfs_dev_stat_print_on_load(device);
6288 btrfs_release_path(path);
6289 }
6290 mutex_unlock(&fs_devices->device_list_mutex);
6291
6292out:
6293 btrfs_free_path(path);
6294 return ret < 0 ? ret : 0;
6295}
6296
6297static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6298 struct btrfs_root *dev_root,
6299 struct btrfs_device *device)
6300{
6301 struct btrfs_path *path;
6302 struct btrfs_key key;
6303 struct extent_buffer *eb;
6304 struct btrfs_dev_stats_item *ptr;
6305 int ret;
6306 int i;
6307
6308 key.objectid = 0;
6309 key.type = BTRFS_DEV_STATS_KEY;
6310 key.offset = device->devid;
6311
6312 path = btrfs_alloc_path();
6313 BUG_ON(!path);
6314 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6315 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006316 printk_in_rcu(KERN_WARNING "BTRFS: "
6317 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006318 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006319 goto out;
6320 }
6321
6322 if (ret == 0 &&
6323 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6324 /* need to delete old one and insert a new one */
6325 ret = btrfs_del_item(trans, dev_root, path);
6326 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006327 printk_in_rcu(KERN_WARNING "BTRFS: "
6328 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006329 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006330 goto out;
6331 }
6332 ret = 1;
6333 }
6334
6335 if (ret == 1) {
6336 /* need to insert a new item */
6337 btrfs_release_path(path);
6338 ret = btrfs_insert_empty_item(trans, dev_root, path,
6339 &key, sizeof(*ptr));
6340 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006341 printk_in_rcu(KERN_WARNING "BTRFS: "
6342 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006343 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006344 goto out;
6345 }
6346 }
6347
6348 eb = path->nodes[0];
6349 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6350 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6351 btrfs_set_dev_stats_value(eb, ptr, i,
6352 btrfs_dev_stat_read(device, i));
6353 btrfs_mark_buffer_dirty(eb);
6354
6355out:
6356 btrfs_free_path(path);
6357 return ret;
6358}
6359
6360/*
6361 * called from commit_transaction. Writes all changed device stats to disk.
6362 */
6363int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6364 struct btrfs_fs_info *fs_info)
6365{
6366 struct btrfs_root *dev_root = fs_info->dev_root;
6367 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6368 struct btrfs_device *device;
6369 int ret = 0;
6370
6371 mutex_lock(&fs_devices->device_list_mutex);
6372 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6373 if (!device->dev_stats_valid || !device->dev_stats_dirty)
6374 continue;
6375
6376 ret = update_dev_stat_item(trans, dev_root, device);
6377 if (!ret)
6378 device->dev_stats_dirty = 0;
6379 }
6380 mutex_unlock(&fs_devices->device_list_mutex);
6381
6382 return ret;
6383}
6384
Stefan Behrens442a4f62012-05-25 16:06:08 +02006385void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6386{
6387 btrfs_dev_stat_inc(dev, index);
6388 btrfs_dev_stat_print_on_error(dev);
6389}
6390
Eric Sandeen48a3b632013-04-25 20:41:01 +00006391static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006392{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006393 if (!dev->dev_stats_valid)
6394 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006395 printk_ratelimited_in_rcu(KERN_ERR "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 Behrens442a4f62012-05-25 16:06:08 +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),
Frank Holtonefe120a2013-12-20 11:37:06 -05006401 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6402 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006403}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006404
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006405static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6406{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006407 int i;
6408
6409 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6410 if (btrfs_dev_stat_read(dev, i) != 0)
6411 break;
6412 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6413 return; /* all values == 0, suppress message */
6414
Frank Holtonefe120a2013-12-20 11:37:06 -05006415 printk_in_rcu(KERN_INFO "BTRFS: "
6416 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006417 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006418 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6419 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6420 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6421 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6422 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6423}
6424
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006425int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006426 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006427{
6428 struct btrfs_device *dev;
6429 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6430 int i;
6431
6432 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006433 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006434 mutex_unlock(&fs_devices->device_list_mutex);
6435
6436 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006437 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006438 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006439 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006440 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006441 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006442 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006443 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6444 if (stats->nr_items > i)
6445 stats->values[i] =
6446 btrfs_dev_stat_read_and_reset(dev, i);
6447 else
6448 btrfs_dev_stat_reset(dev, i);
6449 }
6450 } else {
6451 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6452 if (stats->nr_items > i)
6453 stats->values[i] = btrfs_dev_stat_read(dev, i);
6454 }
6455 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6456 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6457 return 0;
6458}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006459
6460int btrfs_scratch_superblock(struct btrfs_device *device)
6461{
6462 struct buffer_head *bh;
6463 struct btrfs_super_block *disk_super;
6464
6465 bh = btrfs_read_dev_super(device->bdev);
6466 if (!bh)
6467 return -EINVAL;
6468 disk_super = (struct btrfs_super_block *)bh->b_data;
6469
6470 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6471 set_buffer_dirty(bh);
6472 sync_dirty_buffer(bh);
6473 brelse(bh);
6474
6475 return 0;
6476}